• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 RouterConfig & config)137 ErrCode DhcpClientProxy::StartDhcpClient(const RouterConfig &config)
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, config.ifname.c_str());
157     (void)WriteString(&req, config.bssid.c_str());
158     (void)WriteBool(&req, config.prohibitUseCacheIp);
159     (void)WriteBool(&req, config.bIpv6);
160     (void)WriteBool(&req, config.bSpecificNetwork);
161     owner.funcId = static_cast<int32_t>(DhcpClientInterfaceCode::DHCP_CLIENT_SVR_CMD_START_DHCP_CLIENT);
162     int error = remote_->Invoke(remote_,
163         static_cast<int32_t>(DhcpClientInterfaceCode::DHCP_CLIENT_SVR_CMD_START_DHCP_CLIENT), &req,
164         &owner, IpcCallback);
165     if (error != EC_SUCCESS) {
166         DHCP_LOGE("Set Attr(%{public}d) failed,error 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     if (owner.exception) {
171         DHCP_LOGE("exception failed, exception:%{public}d", owner.exception);
172         return DHCP_E_FAILED;
173     }
174     DHCP_LOGI("StartDhcpClient ok, exception:%{public}d", owner.exception);
175     return DHCP_E_SUCCESS;
176 }
177 
DealWifiDhcpCache(int32_t cmd,const IpCacheInfo & ipCacheInfo)178 ErrCode DhcpClientProxy::DealWifiDhcpCache(int32_t cmd, const IpCacheInfo &ipCacheInfo)
179 {
180     if (remoteDied_ || remote_ == nullptr) {
181         DHCP_LOGE("failed to %{public}s, remoteDied_: %{public}d, remote_: %{public}d",
182             __func__, remoteDied_, remote_ == nullptr);
183         return DHCP_OPT_FAILED;
184     }
185 
186     IpcIo req;
187     char data[IPC_DATA_SIZE_SMALL];
188     struct IpcOwner owner = {.exception = -1, .retCode = 0, .variable = nullptr};
189 
190     IpcIoInit(&req, data, IPC_DATA_SIZE_SMALL, MAX_IPC_OBJ_COUNT);
191     if (!WriteInterfaceToken(&req, DECLARE_INTERFACE_DESCRIPTOR_L1, DECLARE_INTERFACE_DESCRIPTOR_L1_LENGTH)) {
192         DHCP_LOGE("Write interface token error: %{public}s", __func__);
193         return DHCP_OPT_FAILED;
194     }
195 
196     (void)WriteInt32(&req, 0);
197     (void)WriteInt32(&req, cmd);
198     (void)WriteString(&req, ipCacheInfo.ssid.c_str());
199     (void)WriteString(&req, ipCacheInfo.bssid.c_str());
200     owner.funcId = static_cast<int32_t>(DhcpClientInterfaceCode::DHCP_CLIENT_SVR_CMD_DEAL_DHCP_CACHE);
201     int error = remote_->Invoke(remote_,
202         static_cast<int32_t>(DhcpClientInterfaceCode::DHCP_CLIENT_SVR_CMD_DEAL_DHCP_CACHE), &req,
203         &owner, IpcCallback);
204     if (error != EC_SUCCESS) {
205         DHCP_LOGE("Set Attr(%{public}d) failed,error code is %{public}d",
206             static_cast<int32_t>(DhcpClientInterfaceCode::DHCP_CLIENT_SVR_CMD_DEAL_DHCP_CACHE), error);
207         return DHCP_E_FAILED;
208     }
209     if (owner.exception) {
210         DHCP_LOGE("exception failed, exception:%{public}d", owner.exception);
211         return DHCP_E_FAILED;
212     }
213     DHCP_LOGI("DealWifiDhcpCache ok, exception:%{public}d", owner.exception);
214     return DHCP_E_SUCCESS;
215 }
216 
StopDhcpClient(const std::string & ifname,bool bIpv6)217 ErrCode DhcpClientProxy::StopDhcpClient(const std::string& ifname, bool bIpv6)
218 {
219     if (remoteDied_ || remote_ == nullptr) {
220         DHCP_LOGE("failed to %{public}s, remoteDied_: %{public}d, remote_: %{public}d",
221             __func__, remoteDied_, remote_ == nullptr);
222         return DHCP_OPT_FAILED;
223     }
224     IpcIo req;
225     char data[IPC_DATA_SIZE_SMALL];
226     struct IpcOwner owner = {.exception = -1, .retCode = 0, .variable = nullptr};
227 
228     IpcIoInit(&req, data, IPC_DATA_SIZE_SMALL, MAX_IPC_OBJ_COUNT);
229     if (!WriteInterfaceToken(&req, DECLARE_INTERFACE_DESCRIPTOR_L1, DECLARE_INTERFACE_DESCRIPTOR_L1_LENGTH)) {
230         DHCP_LOGE("Write interface token error: %{public}s", __func__);
231         return DHCP_OPT_FAILED;
232     }
233 
234     (void)WriteInt32(&req, 0);
235     (void)WriteString(&req, ifname.c_str());
236     (void)WriteBool(&req, bIpv6);
237     owner.funcId = static_cast<int32_t>(DhcpClientInterfaceCode::DHCP_CLIENT_SVR_CMD_STOP_DHCP_CLIENT);
238     int error = remote_->Invoke(remote_,
239         static_cast<int32_t>(DhcpClientInterfaceCode::DHCP_CLIENT_SVR_CMD_STOP_DHCP_CLIENT), &req,
240         &owner, IpcCallback);
241     if (error != EC_SUCCESS) {
242         DHCP_LOGE("Set Attr(%{public}d) failed,error code is %{public}d",
243             static_cast<int32_t>(DhcpClientInterfaceCode::DHCP_CLIENT_SVR_CMD_STOP_DHCP_CLIENT), error);
244         return DHCP_E_FAILED;
245     }
246 
247     if (owner.exception) {
248         DHCP_LOGE("exception failed, exception:%{public}d", owner.exception);
249         return DHCP_E_FAILED;
250     }
251     DHCP_LOGI("StopDhcpClient ok, exception:%{public}d", owner.exception);
252     return DHCP_E_SUCCESS;
253 }
254 }  // namespace DHCP
255 }  // namespace OHOS
256