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 Wifi {
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 std::string & ifname,bool bIpv6)140 ErrCode DhcpClientProxy::StartDhcpClient(const std::string& ifname, bool bIpv6)
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(ifname);
156 data.WriteBool(bIpv6);
157 DHCP_LOGI("%{public}s, calling uid:%{public}d, ifname:%{public}s, bIpv6:%{public}d", __func__, GetCallingUid(),
158 ifname.c_str(), bIpv6);
159 int error = Remote()->SendRequest(
160 static_cast<uint32_t>(DhcpClientInterfaceCode::DHCP_CLIENT_SVR_CMD_START_DHCP_CLIENT), data, reply, option);
161 if (error != ERR_NONE) {
162 DHCP_LOGE("Set Attr(%{public}d) failed, code is %{public}d",
163 static_cast<int32_t>(DhcpClientInterfaceCode::DHCP_CLIENT_SVR_CMD_START_DHCP_CLIENT), error);
164 return DHCP_E_FAILED;
165 }
166 int exception = reply.ReadInt32();
167 if (exception) {
168 DHCP_LOGE("exception failed, exception:%{public}d", exception);
169 return DHCP_E_FAILED;
170 }
171 DHCP_LOGI("StartDhcpClient ok, exception:%{public}d", exception);
172 return DHCP_E_SUCCESS;
173 }
174
StopDhcpClient(const std::string & ifname,bool bIpv6)175 ErrCode DhcpClientProxy::StopDhcpClient(const std::string& ifname, bool bIpv6)
176 {
177 DHCP_LOGI("DhcpClientProxy enter StopDhcpClient mRemoteDied:%{public}d", mRemoteDied);
178 if (mRemoteDied) {
179 DHCP_LOGI("failed to `%{public}s`,remote service is died!", __func__);
180 return DHCP_E_FAILED;
181 }
182 MessageOption option;
183 MessageParcel data, reply;
184 if (!data.WriteInterfaceToken(GetDescriptor())) {
185 DHCP_LOGI("Write interface token error: %{public}s", __func__);
186 return DHCP_E_FAILED;
187 }
188 data.WriteInt32(0);
189 data.WriteString(ifname);
190 data.WriteBool(bIpv6);
191 DHCP_LOGI("%{public}s, calling uid:%{public}d, ifname:%{public}s, bIpv6:%{public}d", __func__, GetCallingUid(),
192 ifname.c_str(), bIpv6);
193 int error = Remote()->SendRequest(
194 static_cast<uint32_t>(DhcpClientInterfaceCode::DHCP_CLIENT_SVR_CMD_STOP_DHCP_CLIENT), data, reply, option);
195 if (error != ERR_NONE) {
196 DHCP_LOGI("Set Attr(%{public}d) failed, code is %{public}d",
197 static_cast<int32_t>(DhcpClientInterfaceCode::DHCP_CLIENT_SVR_CMD_STOP_DHCP_CLIENT), error);
198 return DHCP_E_FAILED;
199 }
200 int exception = reply.ReadInt32();
201 if (exception) {
202 DHCP_LOGI("exception failed, exception:%{public}d", exception);
203 return DHCP_E_FAILED;
204 }
205 DHCP_LOGI("StopDhcpClient ok, exception:%{public}d", exception);
206 return DHCP_E_SUCCESS;
207 }
208
RenewDhcpClient(const std::string & ifname)209 ErrCode DhcpClientProxy::RenewDhcpClient(const std::string& ifname)
210 {
211 if (mRemoteDied) {
212 DHCP_LOGE("failed to `%{public}s`,remote service is died!", __func__);
213 return DHCP_E_FAILED;
214 }
215
216 MessageOption option;
217 MessageParcel data, reply;
218 if (!data.WriteInterfaceToken(GetDescriptor())) {
219 DHCP_LOGE("Write interface token error: %{public}s", __func__);
220 return DHCP_E_FAILED;
221 }
222 data.WriteInt32(0);
223 data.WriteString(ifname);
224 DHCP_LOGI("%{public}s, calling uid:%{public}d, ifname:%{public}s", __func__, GetCallingUid(), ifname.c_str());
225 int error = Remote()->SendRequest(
226 static_cast<uint32_t>(DhcpClientInterfaceCode::DHCP_CLIENT_SVR_CMD_RENEW_DHCP_CLIENT), data, reply, option);
227 if (error != ERR_NONE) {
228 DHCP_LOGE("Set Attr(%{public}d) failed, code is %{public}d",
229 static_cast<int32_t>(DhcpClientInterfaceCode::DHCP_CLIENT_SVR_CMD_RENEW_DHCP_CLIENT), error);
230 return DHCP_E_FAILED;
231 }
232 int exception = reply.ReadInt32();
233 if (exception) {
234 DHCP_LOGE("exception failed, exception:%{public}d", exception);
235 return DHCP_E_FAILED;
236 }
237 DHCP_LOGI("RenewDhcpClient ok, exception:%{public}d", exception);
238 return DHCP_E_SUCCESS;
239 }
240 } // namespace Wifi
241 } // namespace OHOS
242