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