1 /*
2 * Copyright (c) 2021 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
16 #include "wlan_hal_proxy.h"
17 #include "Iwifi_hal.h"
18 #include "wlan_hdi_service_stub.h"
19 #include "iservmgr_hdi.h"
20 #include "securec.h"
21 #include "hdf_log.h"
22 #include <osal_mem.h>
23 #include <message_parcel.h>
24 #include <cstring>
25
26 using namespace OHOS;
27 using namespace OHOS::HDI::WLAN::V1_0;
28 using namespace std;
29 #define ETH_ADDR_LEN 6
30
31 std::function<int32_t(int32_t event, struct HdfSBuf *sbuf)> WlanInterfaceProxy::callback_ = nullptr;
32
Get(const std::string & serviceName)33 sptr<IWlan> IWlan::Get(const std::string& serviceName)
34 {
35 do {
36 using namespace OHOS::HDI::ServiceManager::V1_0;
37 auto servMgr = IServiceManager::Get();
38 if (servMgr == nullptr) {
39 break;
40 }
41 sptr<IRemoteObject> remote = servMgr->GetService(serviceName.c_str());
42 if (remote != nullptr) {
43 return iface_cast<IWlan>(remote);
44 }
45 } while(false);
46 HDF_LOGE("%s: get DataWlanService failed!", __func__);
47 return nullptr;
48 }
49
wifiConstruct()50 int32_t WlanInterfaceProxy::wifiConstruct()
51 {
52 MessageParcel data;
53 MessageParcel reply;
54 MessageOption option;
55
56 int32_t ret = Remote()->SendRequest(WLAN_SERVICE_CONSTRUCT, data, reply, option);
57 if (ret != HDF_SUCCESS) {
58 HDF_LOGE("%s: SendRequest failed, error code is %d", __func__, ret);
59 }
60 return ret;
61 }
62
wifiDestruct()63 int32_t WlanInterfaceProxy::wifiDestruct()
64 {
65 MessageParcel data;
66 MessageParcel reply;
67 MessageOption option;
68
69 int32_t ret = Remote()->SendRequest(WLAN_SERVICE_DECONSTRUCT, data, reply, option);
70 if (ret != HDF_SUCCESS) {
71 HDF_LOGE("%s: SendRequest failed, error code is %d", __func__, ret);
72 }
73 return ret;
74 }
75
start()76 int32_t WlanInterfaceProxy::start()
77 {
78 MessageParcel data;
79 MessageParcel reply;
80 MessageOption option;
81
82 int32_t ret = Remote()->SendRequest(WLAN_SERVICE_START, data, reply, option);
83 if (ret != HDF_SUCCESS) {
84 HDF_LOGE("%s: SendRequest failed, error code is %d", __func__, ret);
85 }
86 return ret;
87 }
88
stop()89 int32_t WlanInterfaceProxy::stop()
90 {
91 MessageParcel data;
92 MessageParcel reply;
93 MessageOption option;
94
95 int32_t ret = Remote()->SendRequest(WLAN_SERVICE_STOP, data, reply, option);
96 if (ret != HDF_SUCCESS) {
97 HDF_LOGE("%s: SendRequest failed, error code is %d", __func__, ret);
98 }
99 return ret;
100 }
101
createFeature(int32_t type,std::shared_ptr<WifiFeatureInfo> & ifeature)102 int32_t WlanInterfaceProxy::createFeature(int32_t type, std::shared_ptr<WifiFeatureInfo>& ifeature)
103 {
104 MessageParcel data;
105 MessageParcel reply;
106 MessageOption option;
107
108 if (!data.WriteInt32(type)) {
109 HDF_LOGE("%s: write type failed!", __func__);
110 return HDF_ERR_INVALID_PARAM;
111 }
112 ifeature = std::make_shared<WifiFeatureInfo>();
113 int32_t ret = Remote()->SendRequest(WLAN_SERVICE_CREATE_FEATURE, data, reply, option);
114 if (ret != HDF_SUCCESS) {
115 HDF_LOGE("%s: SendRequest failed, error code is %d", __func__, ret);
116 }
117 char *name = (char *)reply.ReadCString();
118 ifeature->ifName = strdup(name);
119 int32_t wlan_type = reply.ReadInt32();
120 ifeature->type = wlan_type;
121 return ret;
122 }
123
getSupportFeature(std::vector<uint8_t> & supType)124 int32_t WlanInterfaceProxy::getSupportFeature(std::vector<uint8_t>& supType)
125 {
126 MessageParcel data;
127 MessageParcel reply;
128 MessageOption option;
129
130 int32_t ret = Remote()->SendRequest(WLAN_SERVICE_GET_SUPPORT_FEATURE, data, reply, option);
131 if (ret != HDF_SUCCESS) {
132 HDF_LOGE("%s: SendRequest failed, error code is %d", __func__, ret);
133 }
134 uint32_t size = reply.ReadUint32();
135 uint8_t *supTypeCp = (uint8_t *)reply.ReadUnpadBuffer(size);
136 supType.assign(supTypeCp, supTypeCp + size);
137 return ret;
138 }
139
getSupportCombo(std::vector<uint64_t> & combo)140 int32_t WlanInterfaceProxy::getSupportCombo(std::vector<uint64_t>& combo)
141 {
142 MessageParcel data;
143 MessageParcel reply;
144 MessageOption option;
145
146 int32_t ret = Remote()->SendRequest(WLAN_SERVICE_GET_SUPPORT_COMBO, data, reply, option);
147 if (ret == HDF_ERR_NOT_SUPPORT) {
148 HDF_LOGW("%s: not support to getting combo!", __func__);
149 return HDF_ERR_NOT_SUPPORT;
150 } else if (ret == HDF_SUCCESS) {
151 for (int i = 0; i < ETH_ADDR_LEN; i++) {
152 combo.push_back(reply.ReadUint64());
153 }
154 }
155 return ret;
156 }
157
getFeatureByIfName(std::string & ifName,std::shared_ptr<WifiFeatureInfo> & ifeature)158 int32_t WlanInterfaceProxy::getFeatureByIfName(std::string& ifName, std::shared_ptr<WifiFeatureInfo>& ifeature)
159 {
160 MessageParcel data;
161 MessageParcel reply;
162 MessageOption option;
163
164 if (!data.WriteCString(ifName.c_str())) {
165 HDF_LOGE("%s: write ifname failed!", __func__);
166 return HDF_ERR_INVALID_PARAM;
167 }
168 int32_t ret = Remote()->SendRequest(WLAN_SERVICE_GET_FEATURE_NAME, data, reply, option);
169 if (ret != HDF_SUCCESS) {
170 HDF_LOGE("%s: SendRequest failed, error code is %d", __func__, ret);
171 }
172 int32_t wlan_type = reply.ReadInt32();
173 ifeature->type = wlan_type;
174 return ret;
175 }
176
CallbackWlanProxy(int32_t event,struct HdfSBuf * reqData)177 int32_t WlanInterfaceProxy::CallbackWlanProxy(int32_t event, struct HdfSBuf *reqData)
178 {
179 HDF_LOGI("%s: enter", __func__);
180 if (reqData == NULL) {
181 HDF_LOGE("%s: ptr NULL", __func__);
182 return HDF_ERR_INVALID_PARAM;
183 }
184 callback_(event, reqData);
185 return HDF_SUCCESS;
186 }
187
OnRemoteRequest(uint32_t code,OHOS::MessageParcel & data,OHOS::MessageParcel & reply,OHOS::MessageOption & option)188 int IPCObjectStubWlan::OnRemoteRequest(uint32_t code, OHOS::MessageParcel &data,
189 OHOS::MessageParcel &reply, OHOS::MessageOption &option)
190 {
191 int32_t ret = 0;
192 int32_t status = -1;
193
194 HDF_LOGI("IPCObjectStubWlan::OnRemoteRequest called, code = %d", code);
195 struct HdfSBuf *dataSbuf = HdfSBufTypedObtain(SBUF_IPC);
196 if (dataSbuf == NULL) {
197 HDF_LOGE("%s: dataSbuf malloc failed!", __func__);
198 HdfSBufRecycle(dataSbuf);
199 }
200 const char *name = data.ReadCString();
201 HDF_LOGI("IPCObjectStubWlan::OnRemoteRequest called, ifName = %{public}s", name);
202 status = data.ReadInt32();
203 HdfSbufWriteInt32(dataSbuf, status);
204 switch (code) {
205 case WIFI_EVENT_RESET_DRIVER:
206 ret = WlanInterfaceProxy::CallbackWlanProxy((int32_t)code, dataSbuf);
207 if (ret != 0) {
208 HDF_LOGE("%s: failed, error code is %d", __func__, ret);
209 }
210 break;
211 default:
212 HDF_LOGE("%s: unknown code:%d", __func__, code);
213 ret = HDF_FAILURE;
214 break;
215 }
216 HdfSBufRecycle(dataSbuf);
217 return ret;
218 }
219
registerEventCallback(std::function<int32_t (int32_t event,struct HdfSBuf * sbuf)> cb)220 int32_t WlanInterfaceProxy::registerEventCallback(std::function<int32_t(int32_t event, struct HdfSBuf *sbuf)> cb)
221 {
222 sptr<IPCObjectStub> callback = new IPCObjectStubWlan();
223 OHOS::MessageParcel data;
224 OHOS::MessageParcel reply;
225 OHOS::MessageOption option;
226
227 int32_t ret = data.WriteRemoteObject(callback);
228 if (!ret) {
229 HDF_LOGE("%s: set callback write remote obj failed!", __func__);
230 return HDF_FAILURE;
231 }
232 ret = Remote()->SendRequest(WLAN_SERVICE_REGISTER_CALLBACK, data, reply, option);
233 if (ret != HDF_SUCCESS) {
234 HDF_LOGE("%s: SendRequest failed, error code is %d", __func__, ret);
235 }
236 callback_ = cb;
237 return ret;
238 }
239
unregisterEventCallback()240 int32_t WlanInterfaceProxy::unregisterEventCallback()
241 {
242 MessageParcel data;
243 MessageParcel reply;
244 MessageOption option;
245
246 int32_t ret = Remote()->SendRequest(WLAN_SERVICE_UNREGISTER_CALLBACK, data, reply, option);
247 if (ret != HDF_SUCCESS) {
248 HDF_LOGE("%s: SendRequest failed, error code is %d", __func__, ret);
249 }
250 return ret;
251 }
252
destroyFeature(std::shared_ptr<WifiFeatureInfo> ifeature)253 int32_t WlanInterfaceProxy::destroyFeature(std::shared_ptr<WifiFeatureInfo> ifeature)
254 {
255 MessageParcel data;
256 MessageParcel reply;
257 MessageOption option;
258
259 if (!data.WriteCString(ifeature->ifName)) {
260 HDF_LOGE("%s: write ifname failed!", __func__);
261 return HDF_ERR_INVALID_PARAM;
262 }
263 if (!data.WriteInt32(ifeature->type)) {
264 HDF_LOGE("%s: write type failed!", __func__);
265 return HDF_ERR_INVALID_PARAM;
266 }
267 int32_t ret = Remote()->SendRequest(WLAN_SERVICE_DESTROY_FEATURE, data, reply, option);
268 if (ret != HDF_SUCCESS) {
269 HDF_LOGE("%s: SendRequest failed, error code is %d", __func__, ret);
270 }
271 if (ifeature->ifName != NULL) {
272 free(ifeature->ifName);
273 }
274 return ret;
275 }
276
resetDriver(const uint8_t chipId)277 int32_t WlanInterfaceProxy::resetDriver(const uint8_t chipId)
278 {
279 MessageParcel data;
280 MessageParcel reply;
281 MessageOption option;
282
283 if (!data.WriteUint8(chipId)) {
284 HDF_LOGE("%s: write chipid failed!", __func__);
285 return HDF_ERR_INVALID_PARAM;
286 }
287 int32_t ret = Remote()->SendRequest(WLAN_SERVICE_RESET_DRIVER, data, reply, option);
288 if (ret != HDF_SUCCESS) {
289 HDF_LOGE("%s: SendRequest failed, error code is %d", __func__, ret);
290 }
291 return ret;
292 }
293
getAsscociatedStas(std::shared_ptr<WifiFeatureInfo> ifeature,std::shared_ptr<StaInfo> staInfo,uint32_t count,std::vector<uint32_t> & num)294 int32_t WlanInterfaceProxy::getAsscociatedStas(std::shared_ptr<WifiFeatureInfo> ifeature, std::shared_ptr<StaInfo> staInfo,
295 uint32_t count, std::vector<uint32_t>& num)
296 {
297 MessageParcel data;
298 MessageParcel reply;
299 MessageOption option;
300
301 HDF_LOGI("%s: enter GetAsscociatedStas", __func__);
302 if (!data.WriteCString(ifeature->ifName)) {
303 HDF_LOGE("%s: write ifname failed!", __func__);
304 return HDF_ERR_INVALID_PARAM;
305 }
306 int32_t ret = Remote()->SendRequest(WLAN_SERVICE_GET_ASSCOCIATE_STA, data, reply, option);
307 if (ret != HDF_SUCCESS) {
308 HDF_LOGE("%s: SendRequest failed, error code is %d", __func__, ret);
309 }
310 count = reply.ReadUint32();
311 return ret;
312 }
313
setCountryCode(std::shared_ptr<WifiFeatureInfo> ifeature,std::string & code,uint32_t len)314 int32_t WlanInterfaceProxy::setCountryCode(std::shared_ptr<WifiFeatureInfo> ifeature, std::string& code, uint32_t len)
315 {
316 MessageParcel data;
317 MessageParcel reply;
318 MessageOption option;
319
320 if (!data.WriteCString(ifeature->ifName)) {
321 HDF_LOGE("%s: write ifname failed!", __func__);
322 return HDF_ERR_INVALID_PARAM;
323 }
324 if (!data.WriteCString(code.c_str())) {
325 HDF_LOGE("%s: write code failed!", __func__);
326 return HDF_ERR_INVALID_PARAM;
327 }
328 int32_t ret = Remote()->SendRequest(WLAN_SERVICE_SET_COUNTRY_CODE, data, reply, option);
329 if (ret != HDF_SUCCESS) {
330 HDF_LOGE("%s: SendRequest failed, error code is %d", __func__, ret);
331 }
332 return ret;
333 }
334
getNetworkIfaceName(std::shared_ptr<WifiFeatureInfo> ifeature)335 int32_t WlanInterfaceProxy::getNetworkIfaceName(std::shared_ptr<WifiFeatureInfo> ifeature)
336 {
337 MessageParcel data;
338 MessageParcel reply;
339 MessageOption option;
340
341 if (!data.WriteCString(ifeature->ifName)) {
342 HDF_LOGE("%s: write ifname failed!", __func__);
343 return HDF_ERR_INVALID_PARAM;
344 }
345 int32_t ret = Remote()->SendRequest(WLAN_SERVICE_GET_NETWORK_NAME, data, reply, option);
346 if (ret != HDF_SUCCESS) {
347 HDF_LOGE("%s: SendRequest failed, error code is %d", __func__, ret);
348 }
349 char *name = (char *)reply.ReadCString();
350 ifeature->ifName = name;
351 return ret;
352 }
353
getFeatureType(std::shared_ptr<WifiFeatureInfo> ifeature)354 int32_t WlanInterfaceProxy::getFeatureType(std::shared_ptr<WifiFeatureInfo> ifeature)
355 {
356 MessageParcel data;
357 MessageParcel reply;
358 MessageOption option;
359
360 if (!data.WriteInt32(ifeature->type)) {
361 HDF_LOGE("%s: write type failed!", __func__);
362 return HDF_ERR_INVALID_PARAM;
363 }
364 int32_t ret = Remote()->SendRequest(WLAN_SERVICE_GET_FEATURE_TYPE, data, reply, option);
365 if (ret != HDF_SUCCESS) {
366 HDF_LOGE("%s: SendRequest failed, error code is %d", __func__, ret);
367 }
368 int32_t wifi_type = reply.ReadInt32();
369 ifeature->type = wifi_type;
370 return ret;
371 }
372
setMacAddress(std::shared_ptr<WifiFeatureInfo> ifeature,std::vector<uint8_t> & mac)373 int32_t WlanInterfaceProxy::setMacAddress(std::shared_ptr<WifiFeatureInfo> ifeature, std::vector<uint8_t>& mac)
374 {
375 MessageParcel data;
376 MessageParcel reply;
377 MessageOption option;
378
379 if (!data.WriteCString(ifeature->ifName)) {
380 HDF_LOGE("%s: write ifname failed!", __func__);
381 return HDF_ERR_INVALID_PARAM;
382 }
383 for (uint8_t i = 0; i < mac.size(); i++) {
384 data.WriteUint8(mac[i]);
385 HDF_LOGE("%s: mac addr is %d!", __func__, mac[i]);
386 }
387 int32_t ret = Remote()->SendRequest(WLAN_SERVICE_SET_MAC_ADDR, data, reply, option);
388 if (ret != HDF_SUCCESS) {
389 HDF_LOGE("%s: SendRequest failed, error code is %d", __func__, ret);
390 }
391 return ret;
392 }
393
getDeviceMacAddress(std::shared_ptr<WifiFeatureInfo> ifeature,std::vector<uint8_t> & mac,uint8_t len)394 int32_t WlanInterfaceProxy::getDeviceMacAddress(std::shared_ptr<WifiFeatureInfo> ifeature, std::vector<uint8_t>& mac,
395 uint8_t len)
396 {
397 MessageParcel data;
398 MessageParcel reply;
399 MessageOption option;
400
401 if (!data.WriteCString(ifeature->ifName)) {
402 HDF_LOGE("%s: write ifname failed!", __func__);
403 return HDF_ERR_INVALID_PARAM;
404 }
405 if (!data.WriteInt32(ifeature->type)) {
406 HDF_LOGE("%s: write type failed!", __func__);
407 return HDF_ERR_INVALID_PARAM;
408 }
409 int32_t ret = Remote()->SendRequest(WLAN_SERVICE_GET_MAC_ADDR, data, reply, option);
410 if (ret != HDF_SUCCESS) {
411 HDF_LOGE("%s: SendRequest failed, error code is %d", __func__, ret);
412 }
413 uint8_t *macBuff = (uint8_t *)reply.ReadUnpadBuffer(len);
414 mac.assign(macBuff, macBuff + len);
415 return ret;
416 }
417
getFreqsWithBand(std::shared_ptr<WifiFeatureInfo> ifeature,int32_t band,std::vector<int32_t> freq,uint32_t count,uint32_t & num)418 int32_t WlanInterfaceProxy::getFreqsWithBand(std::shared_ptr<WifiFeatureInfo> ifeature, int32_t band,
419 std::vector<int32_t> freq, uint32_t count, uint32_t& num)
420 {
421 MessageParcel data;
422 MessageParcel reply;
423 MessageOption option;
424
425 if (!data.WriteCString(ifeature->ifName)) {
426 HDF_LOGE("%s: write ifname failed!", __func__);
427 return HDF_ERR_INVALID_PARAM;
428 }
429 if (!data.WriteInt32(band)) {
430 HDF_LOGE("%s: write ifname failed!", __func__);
431 return HDF_ERR_INVALID_PARAM;
432 }
433 int32_t ret = Remote()->SendRequest(WLAN_SERVICE_GET_FREQ_WITHBAND, data, reply, option);
434 if (ret != HDF_SUCCESS) {
435 HDF_LOGE("%s: SendRequest failed, error code is %d", __func__, ret);
436 }
437 num = reply.ReadUint32();
438 for (uint32_t i = 0; i < num; i++) {
439 freq.push_back(reply.ReadInt32());
440 HDF_LOGI("%s: freqs[%d] is %d", __func__, i, freq[i]);
441 }
442 return ret;
443 }
444
setTxPower(std::shared_ptr<WifiFeatureInfo> ifeature,int32_t power)445 int32_t WlanInterfaceProxy::setTxPower(std::shared_ptr<WifiFeatureInfo> ifeature, int32_t power)
446 {
447 MessageParcel data;
448 MessageParcel reply;
449 MessageOption option;
450
451 if (!data.WriteCString(ifeature->ifName)) {
452 HDF_LOGE("%s: write ifname failed!", __func__);
453 return HDF_ERR_INVALID_PARAM;
454 }
455 if (!data.WriteInt32(power)) {
456 HDF_LOGE("%s: write power failed!", __func__);
457 return HDF_ERR_INVALID_PARAM;
458 }
459 int32_t ret = Remote()->SendRequest(WLAN_SERVICE_SET_TX_POWR, data, reply, option);
460 if (ret != HDF_SUCCESS) {
461 HDF_LOGE("%s: SendRequest failed, error code is %d", __func__, ret);
462 }
463 return ret;
464 }
465
getChipId(std::shared_ptr<WifiFeatureInfo> ifeature,uint8_t & chipId)466 int32_t WlanInterfaceProxy::getChipId(std::shared_ptr<WifiFeatureInfo> ifeature, uint8_t& chipId)
467 {
468 MessageParcel data;
469 MessageParcel reply;
470 MessageOption option;
471
472 if (!data.WriteCString(ifeature->ifName)) {
473 HDF_LOGE("%s: write ifname failed!", __func__);
474 return HDF_ERR_INVALID_PARAM;
475 }
476 if (!data.WriteInt32(ifeature->type)) {
477 HDF_LOGE("%s: write type failed!", __func__);
478 return HDF_ERR_INVALID_PARAM;
479 }
480 int32_t ret = Remote()->SendRequest(WLAN_SERVICE_GET_CHIPID, data, reply, option);
481 if (ret != HDF_SUCCESS) {
482 HDF_LOGE("%s: SendRequest failed, error code is %d", __func__, ret);
483 }
484 chipId = reply.ReadUint8();
485 return ret;
486 }
487
getIfNamesByChipId(const uint8_t chipId,std::string & ifNames,uint32_t & num)488 int32_t WlanInterfaceProxy::getIfNamesByChipId(const uint8_t chipId, std::string& ifNames, uint32_t& num)
489 {
490 MessageParcel data;
491 MessageParcel reply;
492 MessageOption option;
493 char *name = NULL;
494
495 if (!data.WriteUint8(chipId)) {
496 HDF_LOGE("%s: write chipid failed!", __func__);
497 return HDF_ERR_INVALID_PARAM;
498 }
499 int32_t ret = Remote()->SendRequest(WLAN_SERVICE_GET_NAME_BYCHIPID, data, reply, option);
500 if (ret != HDF_SUCCESS) {
501 HDF_LOGE("%s: SendRequest failed, error code is %d", __func__, ret);
502 }
503 name = (char *)reply.ReadCString();
504 num =reply.ReadUint32();
505 ifNames = name;
506 return ret;
507 }
508
setScanningMacAddress(std::shared_ptr<WifiFeatureInfo> ifeature,std::vector<uint8_t> & scanMac,uint8_t len)509 int32_t WlanInterfaceProxy::setScanningMacAddress(std::shared_ptr<WifiFeatureInfo> ifeature,
510 std::vector<uint8_t>& scanMac, uint8_t len)
511 {
512 MessageParcel data;
513 MessageParcel reply;
514 MessageOption option;
515
516 if (!data.WriteCString(ifeature->ifName)) {
517 HDF_LOGE("%s: write ifname failed!", __func__);
518 return HDF_ERR_INVALID_PARAM;
519 }
520 data.WriteUint8(len);
521 for (int i = 0; i < ETH_ADDR_LEN; i++) {
522 data.WriteUint8(scanMac[i]);
523 HDF_LOGE("%s: mac addr is %x!", __func__, scanMac[i]);
524 }
525 int32_t ret = Remote()->SendRequest(WLAN_SERVICE_SET_SACN_MACADDR, data, reply, option);
526 if (ret != HDF_ERR_NOT_SUPPORT) {
527 HDF_LOGE("%s: SendRequest failed, error code is %d", __func__, ret);
528 }
529 return ret;
530 }