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 StopDhcpIpv4Client(const std::string& ifname); 67 ErrCode StopDhcpIpv6Client(const std::string& ifname); 68 ErrCode StopDhcpClient(const std::string& ifname, bool bIpv6, bool bIpv4 = true) override; 69 ErrCode StopClientSa(void) override; 70 bool IsRemoteDied(void) override; 71 ErrCode StartNewIpv4Client(const RouterConfig &config, DhcpClient &dhcpClient); 72 ErrCode StartNewIpv6Client(const RouterConfig &config, DhcpClient &dhcpClient); 73 ErrCode StartOldClient(const RouterConfig &config, DhcpClient &dhcpClient); 74 ErrCode StartNewClient(const RouterConfig &config); 75 76 int DhcpIpv4ResultSuccess(struct DhcpIpResult &ipResult); 77 #ifndef OHOS_ARCH_LITE 78 int DhcpOfferResultSuccess(struct DhcpIpResult &ipResult); 79 #endif 80 int DhcpIpv4ResultFail(struct DhcpIpResult &ipResult); 81 int DhcpIpv4ResultTimeOut(const std::string &ifname); 82 int DhcpIpv4ResultExpired(const std::string &ifname); 83 int DhcpIpv6ResultTimeOut(const std::string &ifname); 84 int DhcpFreeIpv6(const std::string ifname); 85 void DhcpIpv6ResulCallback(const std::string ifname, DhcpIpv6Info &info); 86 void PushDhcpResult(const std::string &ifname, OHOS::DHCP::DhcpResult &result); 87 bool CheckDhcpResultExist(const std::string &ifname, OHOS::DHCP::DhcpResult &result); 88 public: 89 // manager multi-instance 90 std::mutex m_clientServiceMutex; 91 std::map<std::string, DhcpClient> m_mapClientService; 92 93 // ip result info 94 std::mutex m_dhcpResultMutex; 95 std::map<std::string, std::vector<DhcpResult>> m_mapDhcpResult; 96 97 // client call back 98 std::mutex m_clientCallBackMutex; 99 #ifdef OHOS_ARCH_LITE 100 std::map<std::string, std::shared_ptr<IDhcpClientCallBack>> m_mapClientCallBack; 101 #else 102 std::map<std::string, sptr<IDhcpClientCallBack>> m_mapClientCallBack; 103 #endif 104 private: 105 bool Init(); 106 bool IsGlobalIPv6Address(std::string ipAddress); 107 bool mPublishFlag; 108 static std::mutex g_instanceLock; 109 ClientServiceRunningState mState; 110 #ifdef OHOS_ARCH_LITE 111 static std::shared_ptr<DhcpClientServiceImpl> g_instance; 112 #else 113 static sptr<DhcpClientServiceImpl> g_instance; 114 #endif 115 std::string m_bssid; 116 }; 117 } // namespace DHCP 118 } // namespace OHOS 119 #endif