• 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 #ifndef OHOS_DHCP_CLIENT_SERVICE_IMPL_H
16 #define OHOS_DHCP_CLIENT_SERVICE_IMPL_H
17 
18 #include <map>
19 #include <list>
20 #include <thread>
21 #include <mutex>
22 #include "i_dhcp_client_callback.h"
23 #include "dhcp_client_state_machine.h"
24 #ifdef OHOS_ARCH_LITE
25 #include "dhcp_client_stub_lite.h"
26 #else
27 #include "system_ability.h"
28 #include "iremote_object.h"
29 #include "dhcp_client_stub.h"
30 #endif
31 
32 namespace OHOS {
33 namespace Wifi {
34 enum ClientServiceRunningState { STATE_NOT_START, STATE_RUNNING };
35 #ifdef OHOS_ARCH_LITE
36 class DhcpClientServiceImpl : public DhcpClientStub {
37 #else
38 class DhcpClientServiceImpl : public SystemAbility, public DhcpClientStub {
39 #endif
40 
41 #ifndef OHOS_ARCH_LITE
42     DECLARE_SYSTEM_ABILITY(DhcpClientServiceImpl);
43 #endif
44 
45 public:
46     DhcpClientServiceImpl();
47     virtual ~DhcpClientServiceImpl();
48 #ifdef OHOS_ARCH_LITE
49     static std::shared_ptr<DhcpClientServiceImpl> GetInstance();
50     void OnStart();
51     void OnStop();
52 #else
53     static sptr<DhcpClientServiceImpl> GetInstance();
54     void OnStart() override;
55     void OnStop() override;
56 #endif
57 
58 #ifdef OHOS_ARCH_LITE
59     ErrCode RegisterDhcpClientCallBack(const std::string& ifname, const std::shared_ptr<IDhcpClientCallBack> &clientCallback) override;
60 #else
61     ErrCode RegisterDhcpClientCallBack(const std::string& ifname, const sptr<IDhcpClientCallBack> &clientCallback) override;
62 #endif
63     ErrCode StartDhcpClient(const std::string& ifname, bool bIpv6) override;
64     ErrCode StopDhcpClient(const std::string& ifname, bool bIpv6) override;
65     ErrCode RenewDhcpClient(const std::string& ifname) override;
66 #ifndef OHOS_ARCH_LITE
67     void StartServiceAbility(int sleepS);
68 #endif
69     bool IsRemoteDied(void) override;
70     ErrCode StartOldClient(const std::string& ifname, bool bIpv6, DhcpClient &dhcpClient);
71     ErrCode StartNewClient(const std::string& ifname, bool bIpv6);
72 
73     int DhcpIpv4ResultSuccess(const std::vector<std::string> &splits);
74     int DhcpIpv4ResultFail(const std::vector<std::string> &splits);
75     int DhcpIpv4ResultTimeOut(const std::string &ifname);
76     void DhcpIpv6ResulCallback(const std::string ifname, DhcpIpv6Info &info);
77     void PushDhcpResult(const std::string &ifname, OHOS::Wifi::DhcpResult &result);
78     bool CheckDhcpResultExist(const std::string &ifname, OHOS::Wifi::DhcpResult &result);
79 public:
80     // manager multi-instance
81     std::mutex m_clientServiceMutex;
82     std::map<std::string, DhcpClient> m_mapClientService;
83 
84     // ip result info
85     std::mutex m_dhcpResultMutex;
86     std::map<std::string, std::vector<DhcpResult>> m_mapDhcpResult;
87 
88     // client call back
89     std::mutex m_clientCallBackMutex;
90 #ifdef OHOS_ARCH_LITE
91     std::map<std::string, std::shared_ptr<IDhcpClientCallBack>> m_mapClientCallBack;
92 #else
93     std::map<std::string, sptr<IDhcpClientCallBack>> m_mapClientCallBack;
94 #endif
95 private:
96     bool Init();
97     bool IsNativeProcess();
98     bool IsGlobalIPv6Address(std::string ipAddress);
99     bool mPublishFlag;
100     static std::mutex g_instanceLock;
101     ClientServiceRunningState mState;
102 #ifdef OHOS_ARCH_LITE
103     static std::shared_ptr<DhcpClientServiceImpl> g_instance;
104 #else
105     static sptr<DhcpClientServiceImpl> g_instance;
106 #endif
107 };
108 }  // namespace Wifi
109 }  // namespace OHOS
110 #endif