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