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 <unistd.h>
16 #include "dhcp_client_proxy.h"
17 #include "dhcp_manager_service_ipc_interface_code.h"
18 #include "dhcp_client_callback_stub.h"
19 #include "dhcp_c_utils.h"
20 #include "dhcp_errcode.h"
21 #include "dhcp_logger.h"
22
23 DEFINE_DHCPLOG_DHCP_LABEL("DhcpClientProxy");
24
25 namespace OHOS {
26 namespace DHCP {
27 static sptr<DhcpClientCallBackStub> g_dhcpClientCallBackStub =
28 sptr<DhcpClientCallBackStub>(new (std::nothrow)DhcpClientCallBackStub());
29
DhcpClientProxy(const sptr<IRemoteObject> & impl)30 DhcpClientProxy::DhcpClientProxy(const sptr<IRemoteObject> &impl) : IRemoteProxy<IDhcpClient>(impl),
31 remote_(nullptr), mRemoteDied(false), deathRecipient_(nullptr)
32 {
33 std::lock_guard<std::mutex> lock(mutex_);
34 if (impl) {
35 if (!impl->IsProxyObject()) {
36 DHCP_LOGW("not proxy object!");
37 return;
38 }
39 deathRecipient_ = new (std::nothrow)DhcpClientDeathRecipient(*this);
40 if (deathRecipient_ == nullptr) {
41 DHCP_LOGW("deathRecipient_ is nullptr!");
42 }
43 if (!impl->AddDeathRecipient(deathRecipient_)) {
44 DHCP_LOGW("AddDeathRecipient failed!");
45 return;
46 }
47 remote_ = impl;
48 DHCP_LOGI("AddDeathRecipient success! ");
49 }
50 }
51
~DhcpClientProxy()52 DhcpClientProxy::~DhcpClientProxy()
53 {
54 DHCP_LOGI("enter ~DhcpClientProxy!");
55 RemoveDeathRecipient();
56 }
57
RemoveDeathRecipient(void)58 void DhcpClientProxy::RemoveDeathRecipient(void)
59 {
60 DHCP_LOGI("enter RemoveDeathRecipient!");
61 std::lock_guard<std::mutex> lock(mutex_);
62 if (remote_ == nullptr) {
63 DHCP_LOGI("remote_ is nullptr!");
64 return;
65 }
66 if (deathRecipient_ == nullptr) {
67 DHCP_LOGI("deathRecipient_ is nullptr!");
68 return;
69 }
70 remote_->RemoveDeathRecipient(deathRecipient_);
71 remote_ = nullptr;
72 }
73
OnRemoteDied(const wptr<IRemoteObject> & remoteObject)74 void DhcpClientProxy::OnRemoteDied(const wptr<IRemoteObject> &remoteObject)
75 {
76 DHCP_LOGW("Remote service is died! remoteObject: %{private}p", &remoteObject);
77 mRemoteDied = true;
78 RemoveDeathRecipient();
79 if (g_dhcpClientCallBackStub == nullptr) {
80 DHCP_LOGE("g_deviceCallBackStub is nullptr");
81 return;
82 }
83 if (g_dhcpClientCallBackStub != nullptr) {
84 g_dhcpClientCallBackStub->SetRemoteDied(true);
85 }
86 }
87
IsRemoteDied(void)88 bool DhcpClientProxy::IsRemoteDied(void)
89 {
90 if (mRemoteDied) {
91 DHCP_LOGW("IsRemoteDied! remote is died now!");
92 }
93 return mRemoteDied;
94 }
95
RegisterDhcpClientCallBack(const std::string & ifname,const sptr<IDhcpClientCallBack> & callback)96 ErrCode DhcpClientProxy::RegisterDhcpClientCallBack(const std::string& ifname,
97 const sptr<IDhcpClientCallBack> &callback)
98 {
99 if (mRemoteDied) {
100 DHCP_LOGE("failed to `%{public}s`,remote service is died!", __func__);
101 return DHCP_E_FAILED;
102 }
103 MessageOption option;
104 MessageParcel data, reply;
105 if (!data.WriteInterfaceToken(GetDescriptor())) {
106 DHCP_LOGE("Write interface token error: %{public}s", __func__);
107 return DHCP_E_FAILED;
108 }
109 data.WriteInt32(0);
110
111 if (g_dhcpClientCallBackStub == nullptr) {
112 DHCP_LOGE("g_dhcpClientCallBackStub is nullptr");
113 return DHCP_E_FAILED;
114 }
115 g_dhcpClientCallBackStub->RegisterCallBack(callback);
116
117 if (!data.WriteRemoteObject(g_dhcpClientCallBackStub->AsObject())) {
118 DHCP_LOGE("WriteRemoteObject failed!");
119 return DHCP_E_FAILED;
120 }
121
122 data.WriteString(ifname);
123 DHCP_LOGI("%{public}s, calling uid:%{public}d, ifname:%{public}s", __func__, GetCallingUid(), ifname.c_str());
124 int error = Remote()->SendRequest(static_cast<uint32_t>(DhcpClientInterfaceCode::DHCP_CLIENT_SVR_CMD_REG_CALL_BACK),
125 data, reply, option);
126 if (error != ERR_NONE) {
127 DHCP_LOGE("Set Attr(%{public}d) failed, code is %{public}d",
128 static_cast<int32_t>(DhcpClientInterfaceCode::DHCP_CLIENT_SVR_CMD_REG_CALL_BACK), error);
129 return DHCP_E_FAILED;
130 }
131 int exception = reply.ReadInt32();
132 if (exception) {
133 DHCP_LOGE("exception failed, exception:%{public}d", exception);
134 return DHCP_E_FAILED;
135 }
136 DHCP_LOGI("RegisterDhcpClientCallBack ok, exception:%{public}d", exception);
137 return DHCP_E_SUCCESS;
138 }
139
StartDhcpClient(const RouterConfig & config)140 ErrCode DhcpClientProxy::StartDhcpClient(const RouterConfig &config)
141 {
142 DHCP_LOGI("DhcpClientProxy enter StartDhcpClient mRemoteDied:%{public}d", mRemoteDied);
143 if (mRemoteDied) {
144 DHCP_LOGE("failed to `%{public}s`,remote service is died!", __func__);
145 return DHCP_E_FAILED;
146 }
147
148 MessageOption option;
149 MessageParcel data, reply;
150 if (!data.WriteInterfaceToken(GetDescriptor())) {
151 DHCP_LOGE("Write interface token error: %{public}s", __func__);
152 return DHCP_E_FAILED;
153 }
154 data.WriteInt32(0);
155 data.WriteString(config.ifname);
156 data.WriteString(config.bssid);
157 data.WriteBool(config.prohibitUseCacheIp);
158 data.WriteBool(config.bIpv6);
159 data.WriteBool(config.bSpecificNetwork);
160 data.WriteBool(config.isStaticIpv4);
161 data.WriteBool(config.bIpv4);
162 DHCP_LOGI("%{public}s, calling uid:%{public}d, ifname:%{public}s, prohibitUseCacheIp:%{public}d, bIpv6:%{public}d"\
163 "bSpecificNetwork:%{public}d", __func__, GetCallingUid(), config.ifname.c_str(), config.prohibitUseCacheIp,
164 config.bIpv6, config.bSpecificNetwork);
165 int error = Remote()->SendRequest(
166 static_cast<uint32_t>(DhcpClientInterfaceCode::DHCP_CLIENT_SVR_CMD_START_DHCP_CLIENT), data, reply, option);
167 if (error != ERR_NONE) {
168 DHCP_LOGE("Set Attr(%{public}d) failed, code is %{public}d",
169 static_cast<int32_t>(DhcpClientInterfaceCode::DHCP_CLIENT_SVR_CMD_START_DHCP_CLIENT), error);
170 return DHCP_E_FAILED;
171 }
172 int exception = reply.ReadInt32();
173 if (exception) {
174 DHCP_LOGE("exception failed, exception:%{public}d", exception);
175 return DHCP_E_FAILED;
176 }
177 DHCP_LOGI("StartDhcpClient ok, exception:%{public}d", exception);
178 return DHCP_E_SUCCESS;
179 }
180
DealWifiDhcpCache(int32_t cmd,const IpCacheInfo & ipCacheInfo)181 ErrCode DhcpClientProxy::DealWifiDhcpCache(int32_t cmd, const IpCacheInfo &ipCacheInfo)
182 {
183 DHCP_LOGI("DhcpClientProxy enter DealWifiDhcpCache mRemoteDied:%{public}d", mRemoteDied);
184 if (mRemoteDied) {
185 DHCP_LOGE("failed to `%{public}s`,remote service is died!", __func__);
186 return DHCP_E_FAILED;
187 }
188
189 MessageOption option;
190 MessageParcel data;
191 MessageParcel reply;
192 if (!data.WriteInterfaceToken(GetDescriptor())) {
193 DHCP_LOGE("Write interface token error: %{public}s", __func__);
194 return DHCP_E_FAILED;
195 }
196 data.WriteInt32(0);
197 data.WriteInt32(cmd);
198 data.WriteString(ipCacheInfo.ssid);
199 data.WriteString(ipCacheInfo.bssid);
200 int error = Remote()->SendRequest(
201 static_cast<uint32_t>(DhcpClientInterfaceCode::DHCP_CLIENT_SVR_CMD_DEAL_DHCP_CACHE), data, reply, option);
202 if (error != ERR_NONE) {
203 DHCP_LOGE("Set Attr(%{public}d) failed, code is %{public}d",
204 static_cast<int32_t>(DhcpClientInterfaceCode::DHCP_CLIENT_SVR_CMD_DEAL_DHCP_CACHE), error);
205 return DHCP_E_FAILED;
206 }
207 int exception = reply.ReadInt32();
208 if (exception) {
209 DHCP_LOGE("exception failed, exception:%{public}d", exception);
210 return DHCP_E_FAILED;
211 }
212 DHCP_LOGI("DealWifiDhcpCache ok, exception:%{public}d", exception);
213 return DHCP_E_SUCCESS;
214 }
215
StopDhcpClient(const std::string & ifname,bool bIpv6,bool bIpv4)216 ErrCode DhcpClientProxy::StopDhcpClient(const std::string& ifname, bool bIpv6, bool bIpv4)
217 {
218 DHCP_LOGI("DhcpClientProxy enter StopDhcpClient mRemoteDied:%{public}d", mRemoteDied);
219 if (mRemoteDied) {
220 DHCP_LOGI("failed to `%{public}s`,remote service is died!", __func__);
221 return DHCP_E_FAILED;
222 }
223 MessageOption option;
224 MessageParcel data, reply;
225 if (!data.WriteInterfaceToken(GetDescriptor())) {
226 DHCP_LOGI("Write interface token error: %{public}s", __func__);
227 return DHCP_E_FAILED;
228 }
229 data.WriteInt32(0);
230 data.WriteString(ifname);
231 data.WriteBool(bIpv6);
232 data.WriteBool(bIpv4);
233 DHCP_LOGI("%{public}s, calling uid:%{public}d, ifname:%{public}s, bIpv6:%{public}d", __func__, GetCallingUid(),
234 ifname.c_str(), bIpv6);
235 int error = Remote()->SendRequest(
236 static_cast<uint32_t>(DhcpClientInterfaceCode::DHCP_CLIENT_SVR_CMD_STOP_DHCP_CLIENT), data, reply, option);
237 if (error != ERR_NONE) {
238 DHCP_LOGI("Set Attr(%{public}d) failed, code is %{public}d",
239 static_cast<int32_t>(DhcpClientInterfaceCode::DHCP_CLIENT_SVR_CMD_STOP_DHCP_CLIENT), error);
240 return DHCP_E_FAILED;
241 }
242 int exception = reply.ReadInt32();
243 if (exception) {
244 DHCP_LOGI("exception failed, exception:%{public}d", exception);
245 return DHCP_E_FAILED;
246 }
247 DHCP_LOGI("StopDhcpClient ok, exception:%{public}d", exception);
248 return DHCP_E_SUCCESS;
249 }
250
StopClientSa(void)251 ErrCode DhcpClientProxy::StopClientSa(void)
252 {
253 DHCP_LOGI("DhcpClientProxy enter StopClientSa mRemoteDied:%{public}d", mRemoteDied);
254 if (mRemoteDied) {
255 DHCP_LOGI("failed to %{public}s,remote service is died!", __func__);
256 return DHCP_E_FAILED;
257 }
258 MessageOption option;
259 MessageParcel data;
260 MessageParcel reply;
261 if (!data.WriteInterfaceToken(GetDescriptor())) {
262 DHCP_LOGI("Write interface token error: %{public}s", __func__);
263 return DHCP_E_FAILED;
264 }
265 data.WriteInt32(0);
266 int error = Remote()->SendRequest(
267 static_cast<uint32_t>(DhcpClientInterfaceCode::DHCP_CLIENT_SVR_CMD_STOP_SA), data, reply, option);
268 if (error != ERR_NONE) {
269 DHCP_LOGI("Set Attr(%{public}d) failed, code is %{public}d",
270 static_cast<int32_t>(DhcpClientInterfaceCode::DHCP_CLIENT_SVR_CMD_STOP_SA), error);
271 return DHCP_E_FAILED;
272 }
273 int exception = reply.ReadInt32();
274 if (exception) {
275 DHCP_LOGI("exception failed, exception:%{public}d", exception);
276 return DHCP_E_FAILED;
277 }
278 DHCP_LOGI("StopClientSa ok, exception:%{public}d", exception);
279 return DHCP_E_SUCCESS;
280 }
281 } // namespace DHCP
282 } // namespace OHOS
283