1 /* 2 * Copyright (C) 2021-2022 Huawei Device Co., Ltd. 3 * Licensed under the Apache License, Version 2.0 (the "License"); 4 * you may not use this file except in compliance with the License. 5 * You may obtain a copy of the License at 6 * 7 * http://www.apache.org/licenses/LICENSE-2.0 8 * 9 * Unless required by applicable law or agreed to in writing, software 10 * distributed under the License is distributed on an "AS IS" BASIS, 11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 * See the License for the specific language governing permissions and 13 * limitations under the License. 14 */ 15 16 #ifndef OHOS_DHCP_CLIENT_SERVICE_IMPL_H 17 #define OHOS_DHCP_CLIENT_SERVICE_IMPL_H 18 19 #include <map> 20 #include <list> 21 #include <thread> 22 #include <mutex> 23 24 #include "i_dhcp_client_service.h" 25 #include "dhcp_define.h" 26 27 28 namespace OHOS { 29 namespace Wifi { 30 struct DhcpResultReq { 31 int timeouts; 32 int getTimestamp; 33 IDhcpResultNotify *pResultNotify; 34 DhcpResultReqDhcpResultReq35 DhcpResultReq() 36 { 37 timeouts = RECEIVER_TIMEOUT; 38 getTimestamp = 0; 39 pResultNotify = nullptr; 40 } 41 }; 42 class DhcpEventSubscriber; 43 class DhcpClientServiceImpl : public IDhcpClientService { 44 public: 45 /** 46 * @Description : Construct a new dhcp client service object. 47 * 48 */ 49 DhcpClientServiceImpl(); 50 51 /** 52 * @Description : Destroy the dhcp client service object. 53 * 54 */ 55 ~DhcpClientServiceImpl() override; 56 57 /** 58 * @Description : Start dhcp client service of specified interface. 59 * 60 * @param ifname - interface name, eg:wlan0 [in] 61 * @param bIpv6 - can or not get ipv6 [in] 62 * @Return : success - DHCP_OPT_SUCCESS, failed - others. 63 */ 64 int StartDhcpClient(const std::string& ifname, bool bIpv6) override; 65 66 /** 67 * @Description : Stop dhcp client service of specified interface. 68 * 69 * @param ifname - interface name, eg:wlan0 [in] 70 * @param bIpv6 - can or not get ipv6 [in] 71 * @Return : success - DHCP_OPT_SUCCESS, failed - others. 72 */ 73 int StopDhcpClient(const std::string& ifname, bool bIpv6) override; 74 75 /** 76 * @Description : Get dhcp client service running status of specified interface. 77 * 78 * @param ifname - interface name, eg:wlan0 [in] 79 * @Return : 0 - not start, 1 - normal started, -1 - not normal. 80 */ 81 int GetDhcpStatus(const std::string& ifname) override; 82 83 /** 84 * @Description : Obtain the dhcp result of specified interface asynchronously. 85 * 86 * @param ifname - interface name, eg:wlan0 [in] 87 * @param dhcp - dhcp result notify [in] 88 * @param timeouts - timeout interval [in] 89 * @Return : success - DHCP_OPT_SUCCESS, failed - others. 90 */ 91 int GetDhcpResult(const std::string& ifname, IDhcpResultNotify *pResultNotify, int timeouts) override; 92 93 /** 94 * @Description : Obtain the dhcp info of specified interface synchronously. 95 * 96 * @param ifname - interface name, eg:wlan0 [in] 97 * @param dhcp - dhcp info [out] 98 * @Return : success - DHCP_OPT_SUCCESS, failed - others. 99 */ 100 int GetDhcpInfo(const std::string& ifname, DhcpServiceInfo& dhcp) override; 101 102 /** 103 * @Description : Renew dhcp client service of specified interface. 104 * 105 * @param ifname - interface name, eg:wlan0 [in] 106 * @Return : success - DHCP_OPT_SUCCESS, failed - others. 107 */ 108 int RenewDhcpClient(const std::string& ifname) override; 109 110 /** 111 * @Description : Release dhcp client service of specified interface. 112 * 113 * @param ifname - interface name, eg:wlan0 [in] 114 * @Return : success - DHCP_OPT_SUCCESS, failed - others. 115 */ 116 int ReleaseDhcpClient(const std::string &ifname) override; 117 118 /** 119 * @Description : Handle dhcp result. 120 * 121 * @param second - sleep second number [out] 122 */ 123 void DhcpResultHandle(uint32_t &second); 124 125 /** 126 * @Description : Get the dhcp client process pid of specified interface. 127 * 128 * @param ifname - interface name, eg:wlan0 [in] 129 * @Return : The dhcp client process pid. 130 */ 131 pid_t GetDhcpClientProPid(const std::string &ifname); 132 133 /** 134 * @Description : Check the dhcp client process of specified interface is or not running. 135 * 136 * @param ifname - interface name, eg:wlan0 [in] 137 * @Return : success - DHCP_OPT_SUCCESS, failed - others. 138 */ 139 int CheckDhcpClientRunning(const std::string &ifname); 140 141 /** 142 * @Description : Get dhcp event success ipv4 result. 143 * 144 * @param splits - dhcp event result vector [in] 145 * @Return : success - DHCP_OPT_SUCCESS, failed - others. 146 */ 147 static int GetSuccessIpv4Result(const std::vector<std::string> &splits); 148 149 /** 150 * @Description : Get dhcp event ipv4 result. 151 * 152 * @param code - dhcp event result code [in] 153 * @param splits - dhcp event result vector [in] 154 * @Return : success - DHCP_OPT_SUCCESS, failed - others. 155 */ 156 static int GetDhcpEventIpv4Result(const int code, const std::vector<std::string> &splits); 157 158 /** 159 * @Description : Handle dhcp event result string. 160 * 161 * @param code - dhcp event result code [in] 162 * @param data - dhcp event result string [in] 163 * @Return : success - DHCP_OPT_SUCCESS, failed - others. 164 */ 165 static int DhcpEventResultHandle(const int code, const std::string &data); 166 167 public: 168 static std::map<std::string, DhcpResult> m_mapDhcpResult; 169 static std::map<std::string, DhcpServiceInfo> m_mapDhcpInfo; 170 171 private: 172 /** 173 * @Description : Start dhcp result handle threads. 174 * 175 * @Return : success - DHCP_OPT_SUCCESS, failed - others. 176 */ 177 int InitDhcpMgrThread(); 178 /** 179 * @Description : Exit dhcp result handle threads and recv msg threads. 180 * 181 */ 182 void ExitDhcpMgrThread(); 183 /** 184 * @Description : Check dhcp result req is or not timeout. 185 * 186 */ 187 void CheckTimeout(); 188 /** 189 * @Description : Dhcp result handle threads execution function. 190 * 191 */ 192 void RunDhcpResultHandleThreadFunc(); 193 /** 194 * @Description : Fork child process function for start or stop dhcp process. 195 * 196 * @param ifname - interface name, eg:wlan0 [in] 197 * @param bIpv6 - can or not get ipv6 [in] 198 * @Return : success - DHCP_OPT_SUCCESS, failed - others. 199 */ 200 int ForkExecChildProcess(const std::string& ifname, bool bIpv6, bool bStart = false); 201 /** 202 * @Description : Fork parent process function for handle dhcp function. 203 * 204 * @param ifname - interface name, eg:wlan0 [in] 205 * @param bIpv6 - can or not get ipv6 [in] 206 * @Return : success - DHCP_OPT_SUCCESS, failed - others. 207 */ 208 int ForkExecParentProcess(const std::string& ifname, bool bIpv6, bool bStart = false, pid_t pid = 0); 209 210 /** 211 * @Description : Subscribe dhcp event. 212 * 213 * @param strAction - event action [in] 214 * @Return : success - DHCP_OPT_SUCCESS, failed - others. 215 */ 216 int SubscribeDhcpEvent(const std::string &strAction); 217 /** 218 * @Description : Unsubscribe dhcp event. 219 * 220 * @param strAction - event action [in] 221 * @Return : success - DHCP_OPT_SUCCESS, failed - others. 222 */ 223 int UnsubscribeDhcpEvent(const std::string &strAction); 224 225 /** 226 * @Description : release result notify memory. 227 * 228 */ 229 void ReleaseResultNotifyMemory(); 230 231 /** 232 * @Description : Unsubscribe all dhcp event. 233 * 234 * @param strAction - event action [in] 235 * @Return : success - DHCP_OPT_SUCCESS, failed - others. 236 */ 237 int UnsubscribeAllDhcpEvent(); 238 239 private: 240 std::mutex mResultNotifyMutex; 241 bool isExitDhcpResultHandleThread; 242 std::thread *pDhcpResultHandleThread; 243 244 std::map<std::string, std::list<DhcpResultReq*>> m_mapDhcpResultNotify; 245 std::map<std::string, std::shared_ptr<OHOS::Wifi::DhcpEventSubscriber>> m_mapEventSubscriber; 246 }; 247 } // namespace Wifi 248 } // namespace OHOS 249 #endif