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