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_WIFI_P2P_SERVICE_IMPL_H 17 #define OHOS_WIFI_P2P_SERVICE_IMPL_H 18 19 #ifndef OHOS_ARCH_LITE 20 #include "system_ability.h" 21 #include "dhcp_server_stub.h" 22 #include "iremote_object.h" 23 #else 24 #include "dhcp_server_stub_lite.h" 25 #endif 26 #include <mutex> 27 #include <list> 28 #include <set> 29 #include <string> 30 31 namespace OHOS { 32 namespace DHCP { 33 constexpr int MAXRETRYTIMEOUT = 10; 34 35 enum ServerServiceRunningState { STATE_NOT_START, STATE_RUNNING }; 36 void DeviceConnectCallBack(const char* ifname); 37 38 class DhcpServerServiceImpl : 39 #ifndef OHOS_ARCH_LITE 40 public SystemAbility, 41 #endif 42 public DhcpServerStub { 43 #ifndef OHOS_ARCH_LITE 44 DECLARE_SYSTEM_ABILITY(DhcpServerServiceImpl); 45 #endif 46 47 public: 48 DhcpServerServiceImpl(); 49 virtual ~DhcpServerServiceImpl(); 50 #ifdef OHOS_ARCH_LITE 51 static std::shared_ptr<DhcpServerServiceImpl> GetInstance(); 52 void OnStart(); 53 void OnStop(); 54 #else 55 static sptr<DhcpServerServiceImpl> GetInstance(); 56 void OnStart() override; 57 void OnStop() override; 58 #endif 59 #ifdef OHOS_ARCH_LITE 60 ErrCode RegisterDhcpServerCallBack(const std::string& ifname, 61 const std::shared_ptr<IDhcpServerCallBack> &serverCallback) override; 62 #else 63 ErrCode RegisterDhcpServerCallBack(const std::string& ifname, 64 const sptr<IDhcpServerCallBack> &serverCallback) override; 65 #endif 66 ErrCode StartDhcpServer(const std::string& ifname) override; 67 ErrCode StopDhcpServer(const std::string& ifname) override; 68 ErrCode PutDhcpRange(const std::string& tagName, const DhcpRange& range) override; 69 ErrCode RemoveDhcpRange(const std::string& tagName, const DhcpRange& range) override; 70 ErrCode RemoveAllDhcpRange(const std::string& tagName) override; 71 ErrCode SetDhcpRange(const std::string& ifname, const DhcpRange& range) override; 72 ErrCode SetDhcpName(const std::string& ifname, const std::string& tagName) override; 73 ErrCode SetDhcpNameExt(const std::string& ifname, const std::string& tagName); 74 ErrCode GetDhcpClientInfos(const std::string& ifname, std::vector<std::string>& leases) override; 75 ErrCode UpdateLeasesTime(const std::string& leaseTime) override; 76 bool IsRemoteDied(void) override; 77 ErrCode DeleteLeaseFile(const std::string& ifname); 78 ErrCode StopServerSa(void) override; 79 80 /** 81 * @Description : Fork child process function for start dhcp server process. 82 * 83 * @param ifname - interface name, eg:wlan0 [in] 84 * @Return : success - DHCP_OPT_SUCCESS, failed - others. 85 */ 86 int ForkExecProcess(const std::string ifname, const std::string ip, const std::string mask, const std::string pool); 87 int DelSpecifiedInterface(const std::string& ifname); 88 void DeviceInfoCallBack(const std::string& ifname); 89 private: 90 bool Init(); 91 int CheckAndUpdateConf(const std::string& ifname); 92 bool CheckIpAddrRange(const DhcpRange& range); 93 int AddSpecifiedInterface(const std::string& ifname); 94 int GetUsingIpRange(const std::string ifname, std::string& ipRange); 95 int CreateDefaultConfigFile(const std::string strFile); 96 void ConvertLeasesToStationInfos(std::vector<std::string> &leases, std::vector<DhcpStationInfo>& stationInfos); 97 98 bool mPublishFlag; 99 static std::mutex g_instanceLock; 100 ServerServiceRunningState mState; 101 std::map<std::string, std::list<DhcpRange>> m_mapInfDhcpRange; /* dhcp server using ip range */ 102 std::map<std::string, std::list<DhcpRange>> m_mapTagDhcpRange; /* dhcp server can be used ip range */ 103 std::set<std::string> m_setInterfaces; /* the started specified interfaces */ 104 105 std::mutex m_serverCallBackMutex; 106 #ifdef OHOS_ARCH_LITE 107 std::map<std::string, std::shared_ptr<IDhcpServerCallBack>> m_mapServerCallBack; 108 #else 109 std::map<std::string, sptr<IDhcpServerCallBack>> m_mapServerCallBack; 110 #endif 111 112 private: 113 #ifdef OHOS_ARCH_LITE 114 static std::shared_ptr<DhcpServerServiceImpl> g_instance; 115 #else 116 static sptr<DhcpServerServiceImpl> g_instance; 117 #endif 118 static std::map<std::string, DhcpServerInfo> m_mapDhcpServer; 119 120 }; 121 } // namespace DHCP 122 } // namespace OHOS 123 #endif