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