• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 pResultNotify - 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 : remove the dhcp result of specified interface asynchronously.
95      *
96      * @param dhcp - dhcp result notify [in]
97      * @Return : success - DHCP_OPT_SUCCESS, failed - others.
98      */
99     int RemoveDhcpResult(IDhcpResultNotify *pResultNotify) override;
100 
101     /**
102      * @Description : Obtain the dhcp info of specified interface synchronously.
103      *
104      * @param ifname - interface name, eg:wlan0 [in]
105      * @param dhcp - dhcp info [out]
106      * @Return : success - DHCP_OPT_SUCCESS, failed - others.
107      */
108     int GetDhcpInfo(const std::string& ifname, DhcpServiceInfo& dhcp) override;
109 
110     /**
111      * @Description : Renew 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 RenewDhcpClient(const std::string& ifname) override;
117 
118     /**
119      * @Description : Release dhcp client service of specified interface.
120      *
121      * @param ifname - interface name, eg:wlan0 [in]
122      * @Return : success - DHCP_OPT_SUCCESS, failed - others.
123      */
124     int ReleaseDhcpClient(const std::string &ifname) override;
125 
126     /**
127      * @Description : Handle dhcp result.
128      *
129      * @param second - sleep second number [out]
130      */
131     void DhcpResultHandle(uint32_t &second);
132 
133     /**
134      * @Description : Get the dhcp client process pid of specified interface.
135      *
136      * @param ifname - interface name, eg:wlan0 [in]
137      * @Return : The dhcp client process pid.
138      */
139     pid_t GetDhcpClientProPid(const std::string &ifname);
140 
141     /**
142      * @Description : Check the dhcp client process of specified interface is or not running.
143      *
144      * @param ifname - interface name, eg:wlan0 [in]
145      * @Return : success - DHCP_OPT_SUCCESS, failed - others.
146      */
147     int CheckDhcpClientRunning(const std::string &ifname);
148 
149     /**
150      * @Description : Get dhcp event success ipv4 result.
151      *
152      * @param splits - dhcp event result vector [in]
153      * @Return : success - DHCP_OPT_SUCCESS, failed - others.
154      */
155     static int GetSuccessIpv4Result(const std::vector<std::string> &splits);
156 
157     /**
158      * @Description : Get dhcp event ipv4 result.
159      *
160      * @param code - dhcp event result code [in]
161      * @param splits - dhcp event result vector [in]
162      * @Return : success - DHCP_OPT_SUCCESS, failed - others.
163      */
164     static int GetDhcpEventIpv4Result(const int code, const std::vector<std::string> &splits);
165 
166     /**
167      * @Description : Handle dhcp event result string.
168      *
169      * @param code - dhcp event result code [in]
170      * @param data - dhcp event result string [in]
171      * @Return : success - DHCP_OPT_SUCCESS, failed - others.
172      */
173     static int DhcpEventResultHandle(const int code, const std::string &data);
174 
175 public:
176     static std::map<std::string, DhcpResult> m_mapDhcpResult;
177     static std::map<std::string, DhcpServiceInfo> m_mapDhcpInfo;
178 
179 private:
180     /**
181      * @Description : Start dhcp result handle threads.
182      *
183      * @Return : success - DHCP_OPT_SUCCESS, failed - others.
184      */
185     int InitDhcpMgrThread();
186     /**
187      * @Description : Exit dhcp result handle threads and recv msg threads.
188      *
189      */
190     void ExitDhcpMgrThread();
191     /**
192      * @Description : Check dhcp result req is or not timeout.
193      *
194      */
195     void CheckTimeout();
196     /**
197      * @Description : Dhcp result handle threads execution function.
198      *
199      */
200     void RunDhcpResultHandleThreadFunc();
201 #ifdef OHOS_ARCH_LITE
202     /**
203      * @Description : Dhcp recv msg threads execution function.
204      *
205      * @param ifname - interface name, eg:wlan0 [in]
206      */
207     void RunDhcpRecvMsgThreadFunc(const std::string& ifname);
208 
209     /**
210      * @Description : Handle dhcp packet info.
211      *
212      * @param ifname - interface name, eg:wlan0 [in]
213      * @param packetResult - dhcp packet result [in]
214      * @param success - get success is true, get failed is false [in]
215      */
216     void DhcpPacketInfoHandle(const std::string& ifname, struct DhcpPacketResult &packetResult, bool success = true);
217 #endif
218     /**
219      * @Description : Fork child process function for start or stop dhcp process.
220      *
221      * @param ifname - interface name, eg:wlan0 [in]
222      * @param bIpv6 - can or not get ipv6 [in]
223      * @Return : success - DHCP_OPT_SUCCESS, failed - others.
224      */
225     int ForkExecChildProcess(const std::string& ifname, bool bIpv6, bool bStart = false);
226     /**
227      * @Description : Fork parent process function for handle dhcp function.
228      *
229      * @param ifname - interface name, eg:wlan0 [in]
230      * @param bIpv6 - can or not get ipv6 [in]
231      * @Return : success - DHCP_OPT_SUCCESS, failed - others.
232      */
233     int ForkExecParentProcess(const std::string& ifname, bool bIpv6, bool bStart = false, pid_t pid = 0);
234 
235     /**
236      * @Description : Subscribe dhcp event.
237      *
238      * @param strAction - event action [in]
239      * @Return : success - DHCP_OPT_SUCCESS, failed - others.
240      */
241     int SubscribeDhcpEvent(const std::string &strAction);
242     /**
243      * @Description : Unsubscribe dhcp event.
244      *
245      * @param strAction - event action [in]
246      * @Return : success - DHCP_OPT_SUCCESS, failed - others.
247      */
248     int UnsubscribeDhcpEvent(const std::string &strAction);
249 
250     /**
251      * @Description : release result notify memory.
252      *
253      */
254     void ReleaseResultNotifyMemory();
255 
256     /**
257      * @Description : Unsubscribe all dhcp event.
258      *
259      * @Return : success - DHCP_OPT_SUCCESS, failed - others.
260      */
261     int UnsubscribeAllDhcpEvent();
262 
263 private:
264     std::mutex mResultNotifyMutex;
265     bool isExitDhcpResultHandleThread;
266     std::thread *pDhcpResultHandleThread;
267 #ifdef OHOS_ARCH_LITE
268     std::mutex mRecvMsgThreadMutex;
269     std::map<std::string, std::thread *> m_mapDhcpRecvMsgThread;
270 #endif
271     std::map<std::string, std::list<DhcpResultReq*>> m_mapDhcpResultNotify;
272 
273     std::mutex m_subscriberMutex;
274     std::map<std::string, std::shared_ptr<OHOS::Wifi::DhcpEventSubscriber>> m_mapEventSubscriber;
275 };
276 }  // namespace Wifi
277 }  // namespace OHOS
278 #endif