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_proxy.h"
16 #include "dhcp_manager_service_ipc_interface_code.h"
17 #include "dhcp_client_callback_stub_lite.h"
18 #include "dhcp_c_utils.h"
19 #include "dhcp_errcode.h"
20 #include "dhcp_logger.h"
21 
22 DEFINE_DHCPLOG_DHCP_LABEL("DhcpClientProxyLite");
23 
24 namespace OHOS {
25 namespace DHCP {
26 static SvcIdentity g_sid;
27 static IpcObjectStub g_objStub;
28 static DhcpClientCallBackStub g_dhcpClientCallBackStub;
29 
30 DhcpClientProxy *DhcpClientProxy::g_instance = nullptr;
DhcpClientProxy()31 DhcpClientProxy::DhcpClientProxy() : remoteDied_(false)
32 {
33     DHCP_LOGI("enter ~DhcpClientProxy!");
34 }
35 
~DhcpClientProxy()36 DhcpClientProxy::~DhcpClientProxy()
37 {
38     DHCP_LOGI("enter ~DhcpClientProxy!");
39 }
40 
GetInstance(void)41 DhcpClientProxy *DhcpClientProxy::GetInstance(void)
42 {
43     if (g_instance != nullptr) {
44         return g_instance;
45     }
46 
47     DhcpClientProxy *tempInstance = new(std::nothrow)DhcpClientProxy();
48     g_instance = tempInstance;
49     return g_instance;
50 }
51 
ReleaseInstance(void)52 void DhcpClientProxy::ReleaseInstance(void)
53 {
54     if (g_instance != nullptr) {
55         delete g_instance;
56         g_instance = nullptr;
57     }
58 }
59 
IpcCallback(void * owner,int code,IpcIo * reply)60 static int IpcCallback(void *owner, int code, IpcIo *reply)
61 {
62     if (code != 0 || owner == nullptr || reply == nullptr) {
63         DHCP_LOGE("Callback error, code:%{public}d, owner:%{public}d, reply:%{public}d",
64             code, owner == nullptr, reply == nullptr);
65         return ERR_FAILED;
66     }
67 
68     struct IpcOwner *data = (struct IpcOwner *)owner;
69     (void)ReadInt32(reply, &data->exception);
70     (void)ReadInt32(reply, &data->retCode);
71     if (data->exception != 0 || data->retCode != DHCP_OPT_SUCCESS || data->variable == nullptr) {
72         return ERR_FAILED;
73     }
74     return ERR_NONE;
75 }
76 
AsyncCallback(uint32_t code,IpcIo * data,IpcIo * reply,MessageOption option)77 static int AsyncCallback(uint32_t code, IpcIo *data, IpcIo *reply, MessageOption option)
78 {
79     if (data == nullptr) {
80         DHCP_LOGE("AsyncCallback error, data is null");
81         return DHCP_E_FAILED;
82     }
83     return g_dhcpClientCallBackStub.OnRemoteRequest(code, data);
84 }
85 
RegisterDhcpClientCallBack(const std::string & ifname,const std::shared_ptr<IDhcpClientCallBack> & callback)86 ErrCode DhcpClientProxy::RegisterDhcpClientCallBack(const std::string& ifname,
87     const std::shared_ptr<IDhcpClientCallBack> &callback)
88 {
89     if (remoteDied_ || remote_ == nullptr) {
90         DHCP_LOGE("failed to %{public}s, remoteDied_: %{public}d, remote_: %{public}d",
91             __func__, remoteDied_, remote_ == nullptr);
92         return DHCP_OPT_FAILED;
93     }
94     g_objStub.func = AsyncCallback;
95     g_objStub.args = nullptr;
96     g_objStub.isRemote = false;
97 
98     g_sid.handle = IPC_INVALID_HANDLE;
99     g_sid.token = SERVICE_TYPE_ANONYMOUS;
100     g_sid.cookie = (uintptr_t)&g_objStub;
101 
102     IpcIo req;
103     char data[IPC_DATA_SIZE_SMALL];
104     struct IpcOwner owner = {.exception = -1, .retCode = 0, .variable = nullptr};
105 
106     IpcIoInit(&req, data, IPC_DATA_SIZE_SMALL, MAX_IPC_OBJ_COUNT);
107     if (!WriteInterfaceToken(&req, DECLARE_INTERFACE_DESCRIPTOR_L1, DECLARE_INTERFACE_DESCRIPTOR_L1_LENGTH)) {
108         DHCP_LOGE("Write interface token error: %{public}s", __func__);
109         return DHCP_OPT_FAILED;
110     }
111     (void)WriteInt32(&req, 0);
112     bool writeRemote = WriteRemoteObject(&req, &g_sid);
113     if (!writeRemote) {
114         DHCP_LOGE("WriteRemoteObject failed.");
115         return DHCP_OPT_FAILED;
116     }
117 
118     (void)WriteString(&req, ifname.c_str());
119     owner.funcId = static_cast<int32_t>(DhcpClientInterfaceCode::DHCP_CLIENT_SVR_CMD_REG_CALL_BACK);
120     int error = remote_->Invoke(remote_,
121         static_cast<int32_t>(DhcpClientInterfaceCode::DHCP_CLIENT_SVR_CMD_REG_CALL_BACK),
122         &req, &owner, IpcCallback);
123     if (error != EC_SUCCESS) {
124         DHCP_LOGE("Set Attr(%{public}d) failed, code is %{public}d",
125             static_cast<int32_t>(DhcpClientInterfaceCode::DHCP_CLIENT_SVR_CMD_REG_CALL_BACK), error);
126         return DHCP_E_FAILED;
127     }
128     if (owner.exception) {
129         DHCP_LOGE("exception failed, exception:%{public}d", owner.exception);
130         return DHCP_E_FAILED;
131     }
132     g_dhcpClientCallBackStub.RegisterCallBack(callback);
133     DHCP_LOGI("RegisterDhcpClientCallBack ok, exception:%{public}d", owner.exception);
134     return DHCP_E_SUCCESS;
135 }
136 
StartDhcpClient(const std::string & ifname,bool bIpv6)137 ErrCode DhcpClientProxy::StartDhcpClient(const std::string& ifname, bool bIpv6)
138 {
139     if (remoteDied_ || remote_ == nullptr) {
140         DHCP_LOGE("failed to %{public}s, remoteDied_: %{public}d, remote_: %{public}d",
141             __func__, remoteDied_, remote_ == nullptr);
142         return DHCP_OPT_FAILED;
143     }
144 
145     IpcIo req;
146     char data[IPC_DATA_SIZE_SMALL];
147     struct IpcOwner owner = {.exception = -1, .retCode = 0, .variable = nullptr};
148 
149     IpcIoInit(&req, data, IPC_DATA_SIZE_SMALL, MAX_IPC_OBJ_COUNT);
150     if (!WriteInterfaceToken(&req, DECLARE_INTERFACE_DESCRIPTOR_L1, DECLARE_INTERFACE_DESCRIPTOR_L1_LENGTH)) {
151         DHCP_LOGE("Write interface token error: %{public}s", __func__);
152         return DHCP_OPT_FAILED;
153     }
154 
155     (void)WriteInt32(&req, 0);
156     (void)WriteString(&req, ifname.c_str());
157     (void)WriteBool(&req, bIpv6);
158     owner.funcId = static_cast<int32_t>(DhcpClientInterfaceCode::DHCP_CLIENT_SVR_CMD_START_DHCP_CLIENT);
159     int error = remote_->Invoke(remote_,
160         static_cast<int32_t>(DhcpClientInterfaceCode::DHCP_CLIENT_SVR_CMD_START_DHCP_CLIENT), &req,
161         &owner, IpcCallback);
162     if (error != EC_SUCCESS) {
163         DHCP_LOGE("Set Attr(%{public}d) failed,error code is %{public}d",
164             static_cast<int32_t>(DhcpClientInterfaceCode::DHCP_CLIENT_SVR_CMD_START_DHCP_CLIENT), error);
165         return DHCP_E_FAILED;
166     }
167     if (owner.exception) {
168         DHCP_LOGE("exception failed, exception:%{public}d", owner.exception);
169         return DHCP_E_FAILED;
170     }
171     DHCP_LOGI("StartDhcpClient ok, exception:%{public}d", owner.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     if (remoteDied_ || remote_ == nullptr) {
178         DHCP_LOGE("failed to %{public}s, remoteDied_: %{public}d, remote_: %{public}d",
179             __func__, remoteDied_, remote_ == nullptr);
180         return DHCP_OPT_FAILED;
181     }
182 
183     IpcIo req;
184     char data[IPC_DATA_SIZE_SMALL];
185     struct IpcOwner owner = {.exception = -1, .retCode = 0, .variable = nullptr};
186 
187     IpcIoInit(&req, data, IPC_DATA_SIZE_SMALL, MAX_IPC_OBJ_COUNT);
188     if (!WriteInterfaceToken(&req, DECLARE_INTERFACE_DESCRIPTOR_L1, DECLARE_INTERFACE_DESCRIPTOR_L1_LENGTH)) {
189         DHCP_LOGE("Write interface token error: %{public}s", __func__);
190         return DHCP_OPT_FAILED;
191     }
192 
193     (void)WriteInt32(&req, 0);
194     (void)WriteString(&req, ifname.c_str());
195     (void)WriteString(&req, config.bssid.c_str());
196     (void)WriteString(&req, config.prohibitUseCacheIp);
197     owner.funcId = static_cast<int32_t>(DhcpClientInterfaceCode::DHCP_CLIENT_SVR_CMD_SET_CONFIG);
198     int error = remote_->Invoke(remote_,
199         static_cast<int32_t>(DhcpClientInterfaceCode::DHCP_CLIENT_SVR_CMD_SET_CONFIG), &req,
200         &owner, IpcCallback);
201     if (error != EC_SUCCESS) {
202         DHCP_LOGE("Set Attr(%{public}d) failed,error code is %{public}d",
203             static_cast<int32_t>(DhcpClientInterfaceCode::DHCP_CLIENT_SVR_CMD_SET_CONFIG), error);
204         return DHCP_E_FAILED;
205     }
206     if (owner.exception) {
207         DHCP_LOGE("exception failed, exception:%{public}d", owner.exception);
208         return DHCP_E_FAILED;
209     }
210     DHCP_LOGI("SetConfiguration ok, exception:%{public}d", owner.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     if (remoteDied_ || remote_ == nullptr) {
217         DHCP_LOGE("failed to %{public}s, remoteDied_: %{public}d, remote_: %{public}d",
218             __func__, remoteDied_, remote_ == nullptr);
219         return DHCP_OPT_FAILED;
220     }
221     IpcIo req;
222     char data[IPC_DATA_SIZE_SMALL];
223     struct IpcOwner owner = {.exception = -1, .retCode = 0, .variable = nullptr};
224 
225     IpcIoInit(&req, data, IPC_DATA_SIZE_SMALL, MAX_IPC_OBJ_COUNT);
226     if (!WriteInterfaceToken(&req, DECLARE_INTERFACE_DESCRIPTOR_L1, DECLARE_INTERFACE_DESCRIPTOR_L1_LENGTH)) {
227         DHCP_LOGE("Write interface token error: %{public}s", __func__);
228         return DHCP_OPT_FAILED;
229     }
230 
231     (void)WriteInt32(&req, 0);
232     (void)WriteString(&req, ifname.c_str());
233     (void)WriteBool(&req, bIpv6);
234     owner.funcId = static_cast<int32_t>(DhcpClientInterfaceCode::DHCP_CLIENT_SVR_CMD_STOP_DHCP_CLIENT);
235     int error = remote_->Invoke(remote_,
236         static_cast<int32_t>(DhcpClientInterfaceCode::DHCP_CLIENT_SVR_CMD_STOP_DHCP_CLIENT), &req,
237         &owner, IpcCallback);
238     if (error != EC_SUCCESS) {
239         DHCP_LOGE("Set Attr(%{public}d) failed,error code is %{public}d",
240             static_cast<int32_t>(DhcpClientInterfaceCode::DHCP_CLIENT_SVR_CMD_STOP_DHCP_CLIENT), error);
241         return DHCP_E_FAILED;
242     }
243 
244     if (owner.exception) {
245         DHCP_LOGE("exception failed, exception:%{public}d", owner.exception);
246         return DHCP_E_FAILED;
247     }
248     DHCP_LOGI("StopDhcpClient ok, exception:%{public}d", owner.exception);
249     return DHCP_E_SUCCESS;
250 }
251 }  // namespace DHCP
252 }  // namespace OHOS
253