• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2021-2023 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 #include "dhcp_server_impl.h"
16 #include "dhcp_server_proxy.h"
17 #ifndef OHOS_ARCH_LITE
18 #include "dhcp_sa_manager.h"
19 #include "i_dhcp_server.h"
20 #include "iremote_broker.h"
21 #include "iremote_object.h"
22 #include "iservice_registry.h"
23 #endif
24 #include "dhcp_logger.h"
25 
26 DEFINE_DHCPLOG_DHCP_LABEL("DhcpServerImpl");
27 
28 namespace OHOS {
29 namespace Wifi {
30 #define RETURN_IF_FAIL(cond)                          \
31     do {                                              \
32         if (!(cond)) {                                \
33             DHCP_LOGI("'%{public}s' failed.", #cond); \
34             return DHCP_E_FAILED;                     \
35         }                                             \
36     } while (0)
37 
DhcpServerImpl()38 DhcpServerImpl::DhcpServerImpl() : client_(nullptr)
39 {}
40 
~DhcpServerImpl()41 DhcpServerImpl::~DhcpServerImpl()
42 {
43 #ifdef OHOS_ARCH_LITE
44     DhcpServerProxy::ReleaseInstance();
45 #endif
46 }
47 
Init(int systemAbilityId)48 bool DhcpServerImpl::Init(int systemAbilityId)
49 {
50     DHCP_LOGI("DhcpServerImpl enter Init!");
51 #ifdef OHOS_ARCH_LITE
52     DhcpServerProxy *serverProxy = DhcpServerProxy::GetInstance();
53     if (serverProxy == nullptr) {
54         DHCP_LOGE("get dhcp server proxy failed.");
55         return false;
56     }
57     if (clientProxy->Init() != DHCP_OPT_SUCCESS) {
58         DHCP_LOGE("dhcp server proxy init failed.");
59         DhcpServerProxy::ReleaseInstance();
60         return false;
61     }
62     client_ = clientProxy;
63     return true;
64 #else
65     systemAbilityId_ = systemAbilityId;
66     return true;
67 #endif
68 }
69 
GetDhcpServerProxy()70 bool DhcpServerImpl::GetDhcpServerProxy()
71 {
72 #ifdef OHOS_ARCH_LITE
73     return (client_ != nullptr);
74 #else
75     DhcpSaLoadManager::GetInstance().LoadWifiSa(systemAbilityId_);
76     if (IsRemoteDied() == false) {
77         DHCP_LOGE("remote died false, %{public}d", systemAbilityId_);
78         return true;
79     }
80     sptr<ISystemAbilityManager> sa_mgr = SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager();
81     if (sa_mgr == nullptr) {
82         DHCP_LOGE("failed to get SystemAbilityManager");
83         return false;
84     }
85     sptr<IRemoteObject> object = sa_mgr->GetSystemAbility(systemAbilityId_);
86     if (object == nullptr) {
87         DHCP_LOGE("failed to get DEVICE_SERVICE");
88         return false;
89     }
90     client_ = iface_cast<OHOS::Wifi::IDhcpServer>(object);
91     if (client_ == nullptr) {
92         client_ = new (std::nothrow)DhcpServerProxy(object);
93     }
94 
95     if (client_ == nullptr) {
96         DHCP_LOGE("wifi device init failed. %{public}d", systemAbilityId_);
97         return false;
98     }
99     DHCP_LOGI("DhcpServerImpl GetDhcpClientProxy ok");
100     return true;
101 #endif
102 }
103 
IsRemoteDied(void)104 bool DhcpServerImpl::IsRemoteDied(void)
105 {
106     return (client_ == nullptr) ? true : client_->IsRemoteDied();
107 }
108 
109 #ifdef OHOS_ARCH_LITE
RegisterDhcpServerCallBack(const std::string & ifname,const std::shared_ptr<IDhcpServerCallBack> & callback)110 ErrCode DhcpServerImpl::RegisterDhcpServerCallBack(const std::string& ifname,
111     const std::shared_ptr<IDhcpServerCallBack> &callback)
112 #else
113 ErrCode DhcpServerImpl::RegisterDhcpServerCallBack(const std::string& ifname,
114     const sptr<IDhcpServerCallBack> &callback)
115 #endif
116 {
117     std::lock_guard<std::mutex> lock(mutex_);
118     RETURN_IF_FAIL(GetDhcpServerProxy());
119     return client_->RegisterDhcpServerCallBack(ifname, callback);
120 }
121 
StartDhcpServer(const std::string & ifname)122 ErrCode DhcpServerImpl::StartDhcpServer(const std::string& ifname)
123 {
124     DHCP_LOGI("%{public}s  %{public}d  start", __func__, __LINE__);
125     std::lock_guard<std::mutex> lock(mutex_);
126     RETURN_IF_FAIL(GetDhcpServerProxy());
127     return client_->StartDhcpServer(ifname);
128 }
129 
StopDhcpServer(const std::string & ifname)130 ErrCode DhcpServerImpl::StopDhcpServer(const std::string& ifname)
131 {
132     std::lock_guard<std::mutex> lock(mutex_);
133     RETURN_IF_FAIL(GetDhcpServerProxy());
134     return client_->StopDhcpServer(ifname);
135 }
136 
PutDhcpRange(const std::string & tagName,const DhcpRange & range)137 ErrCode DhcpServerImpl::PutDhcpRange(const std::string& tagName, const DhcpRange& range)
138 {
139     std::lock_guard<std::mutex> lock(mutex_);
140     RETURN_IF_FAIL(GetDhcpServerProxy());
141     return client_->PutDhcpRange(tagName, range);
142 }
143 
RemoveDhcpRange(const std::string & tagName,const DhcpRange & range)144 ErrCode DhcpServerImpl::RemoveDhcpRange(const std::string& tagName, const DhcpRange& range)
145 {
146     std::lock_guard<std::mutex> lock(mutex_);
147     RETURN_IF_FAIL(GetDhcpServerProxy());
148     return client_->RemoveDhcpRange(tagName, range);
149 }
150 
RemoveAllDhcpRange(const std::string & tagName)151 ErrCode DhcpServerImpl::RemoveAllDhcpRange(const std::string& tagName)
152 {
153     std::lock_guard<std::mutex> lock(mutex_);
154     RETURN_IF_FAIL(GetDhcpServerProxy());
155     return client_->RemoveAllDhcpRange(tagName);
156 }
157 
SetDhcpRange(const std::string & ifname,const DhcpRange & range)158 ErrCode DhcpServerImpl::SetDhcpRange(const std::string& ifname, const DhcpRange& range)
159 {
160     std::lock_guard<std::mutex> lock(mutex_);
161     RETURN_IF_FAIL(GetDhcpServerProxy());
162     return client_->SetDhcpRange(ifname, range);
163 }
164 
SetDhcpName(const std::string & ifname,const std::string & tagName)165 ErrCode DhcpServerImpl::SetDhcpName(const std::string& ifname, const std::string& tagName)
166 {
167     std::lock_guard<std::mutex> lock(mutex_);
168     RETURN_IF_FAIL(GetDhcpServerProxy());
169     return client_->SetDhcpName(ifname, tagName);
170 }
171 
GetDhcpClientInfos(const std::string & ifname,std::vector<std::string> & dhcpClientInfo)172 ErrCode DhcpServerImpl::GetDhcpClientInfos(const std::string& ifname, std::vector<std::string>& dhcpClientInfo)
173 {
174     std::lock_guard<std::mutex> lock(mutex_);
175     RETURN_IF_FAIL(GetDhcpServerProxy());
176     return client_->GetDhcpClientInfos(ifname, dhcpClientInfo);
177 }
178 
UpdateLeasesTime(const std::string & leaseTime)179 ErrCode DhcpServerImpl::UpdateLeasesTime(const std::string& leaseTime)
180 {
181     std::lock_guard<std::mutex> lock(mutex_);
182     RETURN_IF_FAIL(GetDhcpServerProxy());
183     return client_->UpdateLeasesTime(leaseTime);
184 }
185 }  // namespace Wifi
186 }  // namespace OHOS
187