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 DHCP_LOGI("%{public}s, calling uid:%{public}d, ifname:%{public}s, prohibitUseCacheIp:%{public}d, bIpv6:%{public}d"\
161 "bSpecificNetwork:%{public}d", __func__, GetCallingUid(), config.ifname.c_str(), config.prohibitUseCacheIp,
162 config.bIpv6, config.bSpecificNetwork);
163 int error = Remote()->SendRequest(
164 static_cast<uint32_t>(DhcpClientInterfaceCode::DHCP_CLIENT_SVR_CMD_START_DHCP_CLIENT), data, reply, option);
165 if (error != ERR_NONE) {
166 DHCP_LOGE("Set Attr(%{public}d) failed, code is %{public}d",
167 static_cast<int32_t>(DhcpClientInterfaceCode::DHCP_CLIENT_SVR_CMD_START_DHCP_CLIENT), error);
168 return DHCP_E_FAILED;
169 }
170 int exception = reply.ReadInt32();
171 if (exception) {
172 DHCP_LOGE("exception failed, exception:%{public}d", exception);
173 return DHCP_E_FAILED;
174 }
175 DHCP_LOGI("StartDhcpClient ok, exception:%{public}d", exception);
176 return DHCP_E_SUCCESS;
177 }
178
DealWifiDhcpCache(int32_t cmd,const IpCacheInfo & ipCacheInfo)179 ErrCode DhcpClientProxy::DealWifiDhcpCache(int32_t cmd, const IpCacheInfo &ipCacheInfo)
180 {
181 DHCP_LOGI("DhcpClientProxy enter DealWifiDhcpCache mRemoteDied:%{public}d", mRemoteDied);
182 if (mRemoteDied) {
183 DHCP_LOGE("failed to `%{public}s`,remote service is died!", __func__);
184 return DHCP_E_FAILED;
185 }
186
187 MessageOption option;
188 MessageParcel data;
189 MessageParcel reply;
190 if (!data.WriteInterfaceToken(GetDescriptor())) {
191 DHCP_LOGE("Write interface token error: %{public}s", __func__);
192 return DHCP_E_FAILED;
193 }
194 data.WriteInt32(0);
195 data.WriteInt32(cmd);
196 data.WriteString(ipCacheInfo.ssid);
197 data.WriteString(ipCacheInfo.bssid);
198 int error = Remote()->SendRequest(
199 static_cast<uint32_t>(DhcpClientInterfaceCode::DHCP_CLIENT_SVR_CMD_DEAL_DHCP_CACHE), data, reply, option);
200 if (error != ERR_NONE) {
201 DHCP_LOGE("Set Attr(%{public}d) failed, code is %{public}d",
202 static_cast<int32_t>(DhcpClientInterfaceCode::DHCP_CLIENT_SVR_CMD_DEAL_DHCP_CACHE), error);
203 return DHCP_E_FAILED;
204 }
205 int exception = reply.ReadInt32();
206 if (exception) {
207 DHCP_LOGE("exception failed, exception:%{public}d", exception);
208 return DHCP_E_FAILED;
209 }
210 DHCP_LOGI("DealWifiDhcpCache ok, exception:%{public}d", exception);
211 return DHCP_E_SUCCESS;
212 }
213
StopDhcpClient(const std::string & ifname,bool bIpv6)214 ErrCode DhcpClientProxy::StopDhcpClient(const std::string& ifname, bool bIpv6)
215 {
216 DHCP_LOGI("DhcpClientProxy enter StopDhcpClient mRemoteDied:%{public}d", mRemoteDied);
217 if (mRemoteDied) {
218 DHCP_LOGI("failed to `%{public}s`,remote service is died!", __func__);
219 return DHCP_E_FAILED;
220 }
221 MessageOption option;
222 MessageParcel data, reply;
223 if (!data.WriteInterfaceToken(GetDescriptor())) {
224 DHCP_LOGI("Write interface token error: %{public}s", __func__);
225 return DHCP_E_FAILED;
226 }
227 data.WriteInt32(0);
228 data.WriteString(ifname);
229 data.WriteBool(bIpv6);
230 DHCP_LOGI("%{public}s, calling uid:%{public}d, ifname:%{public}s, bIpv6:%{public}d", __func__, GetCallingUid(),
231 ifname.c_str(), bIpv6);
232 int error = Remote()->SendRequest(
233 static_cast<uint32_t>(DhcpClientInterfaceCode::DHCP_CLIENT_SVR_CMD_STOP_DHCP_CLIENT), data, reply, option);
234 if (error != ERR_NONE) {
235 DHCP_LOGI("Set Attr(%{public}d) failed, code is %{public}d",
236 static_cast<int32_t>(DhcpClientInterfaceCode::DHCP_CLIENT_SVR_CMD_STOP_DHCP_CLIENT), error);
237 return DHCP_E_FAILED;
238 }
239 int exception = reply.ReadInt32();
240 if (exception) {
241 DHCP_LOGI("exception failed, exception:%{public}d", exception);
242 return DHCP_E_FAILED;
243 }
244 DHCP_LOGI("StopDhcpClient ok, exception:%{public}d", exception);
245 return DHCP_E_SUCCESS;
246 }
247
StopClientSa(void)248 ErrCode DhcpClientProxy::StopClientSa(void)
249 {
250 DHCP_LOGI("DhcpClientProxy enter StopClientSa mRemoteDied:%{public}d", mRemoteDied);
251 if (mRemoteDied) {
252 DHCP_LOGI("failed to %{public}s,remote service is died!", __func__);
253 return DHCP_E_FAILED;
254 }
255 MessageOption option;
256 MessageParcel data;
257 MessageParcel reply;
258 if (!data.WriteInterfaceToken(GetDescriptor())) {
259 DHCP_LOGI("Write interface token error: %{public}s", __func__);
260 return DHCP_E_FAILED;
261 }
262 data.WriteInt32(0);
263 int error = Remote()->SendRequest(
264 static_cast<uint32_t>(DhcpClientInterfaceCode::DHCP_CLIENT_SVR_CMD_STOP_SA), data, reply, option);
265 if (error != ERR_NONE) {
266 DHCP_LOGI("Set Attr(%{public}d) failed, code is %{public}d",
267 static_cast<int32_t>(DhcpClientInterfaceCode::DHCP_CLIENT_SVR_CMD_STOP_SA), error);
268 return DHCP_E_FAILED;
269 }
270 int exception = reply.ReadInt32();
271 if (exception) {
272 DHCP_LOGI("exception failed, exception:%{public}d", exception);
273 return DHCP_E_FAILED;
274 }
275 DHCP_LOGI("StopClientSa ok, exception:%{public}d", exception);
276 return DHCP_E_SUCCESS;
277 }
278 } // namespace DHCP
279 } // namespace OHOS
280