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_client_impl.h"
16 #include "i_dhcp_client.h"
17 #include "dhcp_client_proxy.h"
18 #ifndef OHOS_ARCH_LITE
19 #include "dhcp_sa_manager.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("DhcpClientImpl");
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
DhcpClientImpl()38 DhcpClientImpl::DhcpClientImpl() : client_(nullptr)
39 {}
40
~DhcpClientImpl()41 DhcpClientImpl::~DhcpClientImpl()
42 {
43 #ifdef OHOS_ARCH_LITE
44 DhcpClientProxy::ReleaseInstance();
45 #endif
46 }
47
Init(int systemAbilityId)48 bool DhcpClientImpl::Init(int systemAbilityId)
49 {
50 DHCP_LOGI("DhcpClientImpl enter Init!");
51 #ifdef OHOS_ARCH_LITE
52 DhcpClientProxy *clientProxy = DhcpClientProxy::GetInstance();
53 if (clientProxy == nullptr) {
54 DHCP_LOGE("get dhcp client proxy failed.");
55 return false;
56 }
57 if (clientProxy->Init() != DHCP_OPT_SUCCESS) {
58 DHCP_LOGE("dhcp client proxy init failed.");
59 DhcpClientProxy::ReleaseInstance();
60 return false;
61 }
62 client_ = clientProxy;
63 return true;
64 #else
65 systemAbilityId_ = systemAbilityId;
66 return true;
67 #endif
68 }
69
GetDhcpClientProxy()70 bool DhcpClientImpl::GetDhcpClientProxy()
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
81 sptr<ISystemAbilityManager> sa_mgr = SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager();
82 if (sa_mgr == nullptr) {
83 DHCP_LOGE("failed to get SystemAbilityManager");
84 return false;
85 }
86
87 sptr<IRemoteObject> object = sa_mgr->GetSystemAbility(systemAbilityId_);
88 if (object == nullptr) {
89 DHCP_LOGE("failed to get DEVICE_SERVICE");
90 return false;
91 }
92
93 client_ = iface_cast<OHOS::Wifi::IDhcpClient>(object);
94 if (client_ == nullptr) {
95 DHCP_LOGI("DhcpClientImpl new DhcpClientProxy");
96 client_ = new (std::nothrow)DhcpClientProxy(object);
97 }
98
99 if (client_ == nullptr) {
100 DHCP_LOGE("dhcp client init failed. %{public}d", systemAbilityId_);
101 return false;
102 }
103 DHCP_LOGI("DhcpClientImpl GetDhcpClientProxy ok");
104 return true;
105 #endif
106 }
107
IsRemoteDied(void)108 bool DhcpClientImpl::IsRemoteDied(void)
109 {
110 return (client_ == nullptr) ? true : client_->IsRemoteDied();
111 }
112
113 #ifdef OHOS_ARCH_LITE
RegisterDhcpClientCallBack(const std::string & ifname,const std::shared_ptr<IDhcpClientCallBack> & callback)114 ErrCode DhcpClientImpl::RegisterDhcpClientCallBack(const std::string& ifname,
115 const std::shared_ptr<IDhcpClientCallBack> &callback)
116 #else
117 ErrCode DhcpClientImpl::RegisterDhcpClientCallBack(const std::string& ifname,
118 const sptr<IDhcpClientCallBack> &callback)
119 #endif
120 {
121 std::lock_guard<std::mutex> lock(mutex_);
122 RETURN_IF_FAIL(GetDhcpClientProxy());
123 return client_->RegisterDhcpClientCallBack(ifname, callback);
124 }
125
StartDhcpClient(const std::string & ifname,bool bIpv6)126 ErrCode DhcpClientImpl::StartDhcpClient(const std::string& ifname, bool bIpv6)
127 {
128 std::lock_guard<std::mutex> lock(mutex_);
129 RETURN_IF_FAIL(GetDhcpClientProxy());
130 return client_->StartDhcpClient(ifname, bIpv6);
131 }
132
StopDhcpClient(const std::string & ifname,bool bIpv6)133 ErrCode DhcpClientImpl::StopDhcpClient(const std::string& ifname, bool bIpv6)
134 {
135 std::lock_guard<std::mutex> lock(mutex_);
136 RETURN_IF_FAIL(GetDhcpClientProxy());
137 return client_->StopDhcpClient(ifname, bIpv6);
138 }
139
RenewDhcpClient(const std::string & ifname)140 ErrCode DhcpClientImpl::RenewDhcpClient(const std::string& ifname)
141 {
142 std::lock_guard<std::mutex> lock(mutex_);
143 RETURN_IF_FAIL(GetDhcpClientProxy());
144 return client_->RenewDhcpClient(ifname);
145 }
146 } // namespace Wifi
147 } // namespace OHOS
148