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