• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2021-2022 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 <stdlib.h>
17 
18 #include "hdf_log.h"
19 #include "sbuf_common_adapter.h"
20 #include "securec.h"
21 
22 #ifdef __cplusplus
23 #if __cplusplus
24 extern "C" {
25 #endif
26 #endif
27 
28 #define DRIVER_SERVICE_NAME "hdfwifi"
29 static struct HdfDevEventlistener g_wifiDevEventListener;
30 static bool g_isHasRegisterListener = false;
31 
ParserNetworkInfo(struct HdfSBuf * reply,struct NetworkInfoResult * result)32 static int32_t ParserNetworkInfo(struct HdfSBuf *reply, struct NetworkInfoResult *result)
33 {
34     uint32_t i;
35     const char *ifName = NULL;
36     uint8_t *mode = NULL;
37     uint32_t replayDataSize;
38 
39     if (!HdfSbufReadUint32(reply, &result->nums)) {
40         HDF_LOGE("%s: get networkNum failed", __FUNCTION__);
41         return RET_CODE_FAILURE;
42     }
43     if (result->nums > MAX_IFACE_NUM) {
44         result->nums = MAX_IFACE_NUM;
45     }
46     for (i = 0; i < result->nums; i++) {
47         ifName = HdfSbufReadString(reply);
48         if (ifName == NULL) {
49             HDF_LOGE("%s: get ifName failed", __FUNCTION__);
50             return RET_CODE_FAILURE;
51         }
52         if (!HdfSbufReadBuffer(reply, (const void **)&mode, &replayDataSize) || mode == NULL ||
53             replayDataSize != WIFI_IFTYPE_MAX) {
54             HDF_LOGE("%s: get mode failed", __FUNCTION__);
55             return RET_CODE_FAILURE;
56         }
57         if (strncpy_s(result->infos[i].name, IFNAMSIZ, ifName, strlen(ifName)) != EOK) {
58             HDF_LOGE("%s: memcpy_s name failed", __FUNCTION__);
59             return RET_CODE_FAILURE;
60         }
61         if (memcpy_s(result->infos[i].supportMode, WIFI_IFTYPE_MAX, mode, replayDataSize) != EOK) {
62             HDF_LOGE("%s: memcpy_s supportMode failed", __FUNCTION__);
63             return RET_CODE_FAILURE;
64         }
65     }
66     return RET_CODE_SUCCESS;
67 }
68 
ParserDeviceMacAddr(struct HdfSBuf * reply,uint8_t * mac,uint8_t len)69 static int32_t ParserDeviceMacAddr(struct HdfSBuf *reply, uint8_t *mac, uint8_t len)
70 {
71     uint8_t isEfuseSavedMac;
72     uint32_t replayDataSize = 0;
73     const uint8_t *replayData = 0;
74 
75     if (!HdfSbufReadUint8(reply, &isEfuseSavedMac)) {
76         HDF_LOGE("%s: HdfSbufReadUint8 failed", __FUNCTION__);
77         return RET_CODE_FAILURE;
78     }
79     if (!isEfuseSavedMac) {
80         HDF_LOGE("%s: not support to get device mac addr", __FUNCTION__);
81         return RET_CODE_NOT_SUPPORT;
82     }
83     if (!HdfSbufReadBuffer(reply, (const void **)(&replayData), &replayDataSize) || replayDataSize != len) {
84         HDF_LOGE("%s: HdfSbufReadBuffer failed", __FUNCTION__);
85         return RET_CODE_FAILURE;
86     }
87     if (memcpy_s(mac, len, replayData, replayDataSize) != EOK) {
88         HDF_LOGE("%s: memcpy failed", __FUNCTION__);
89         return RET_CODE_FAILURE;
90     }
91     return RET_CODE_SUCCESS;
92 }
93 
ParserFreqInfo(struct HdfSBuf * reply,struct FreqInfoResult * result,uint32_t size)94 static int32_t ParserFreqInfo(struct HdfSBuf *reply, struct FreqInfoResult *result, uint32_t size)
95 {
96     uint32_t replayDataSize = 0;
97     const uint8_t *replayData = 0;
98 
99     if (result == NULL || result->freqs == NULL || result->txPower == NULL) {
100         HDF_LOGE("%s:  Invalid input parameter", __FUNCTION__);
101         return RET_CODE_INVALID_PARAM;
102     }
103 
104     if (!HdfSbufReadUint32(reply, &result->nums)) {
105         HDF_LOGE("%s: read num failed", __FUNCTION__);
106         return RET_CODE_FAILURE;
107     }
108     if (result->nums > size) {
109         HDF_LOGE("%s: num valid", __FUNCTION__);
110         return RET_CODE_FAILURE;
111     }
112     if (!HdfSbufReadBuffer(reply, (const void **)(&replayData), &replayDataSize)) {
113         HDF_LOGE("%s: read freqs failed", __FUNCTION__);
114         return RET_CODE_FAILURE;
115     }
116     if (memcpy_s(result->freqs, size * sizeof(uint32_t), replayData, replayDataSize) != EOK) {
117         HDF_LOGE("%s: memcpy failed", __FUNCTION__);
118         return RET_CODE_FAILURE;
119     }
120     return RET_CODE_SUCCESS;
121 }
122 
ParserAssociatedStas(struct HdfSBuf * reply,struct AssocStaInfoResult * result)123 static int32_t ParserAssociatedStas(struct HdfSBuf *reply, struct AssocStaInfoResult *result)
124 {
125     uint32_t replayDataSize = 0;
126     const uint8_t *replayData = 0;
127 
128     if (!HdfSbufReadUint32(reply, &result->num)) {
129         HDF_LOGE("%s: read num failed", __FUNCTION__);
130         return RET_CODE_FAILURE;
131     }
132     if (result->num > MAX_ASSOC_STA_NUM) {
133         HDF_LOGE("%s: num invalid", __FUNCTION__);
134         return RET_CODE_FAILURE;
135     }
136     if (result->num != 0) {
137         if (!HdfSbufReadBuffer(reply, (const void **)(&replayData), &replayDataSize) ||
138             replayDataSize > sizeof(result->infos)) {
139             HDF_LOGE("%s: read AssocStaInfo failed", __FUNCTION__);
140             return RET_CODE_FAILURE;
141         }
142         if (memcpy_s(result->infos, sizeof(result->infos), replayData, replayDataSize) != EOK) {
143             HDF_LOGE("%s: memcpy failed", __FUNCTION__);
144             return RET_CODE_FAILURE;
145         }
146     }
147     return RET_CODE_SUCCESS;
148 }
149 
HdfSbufObtainDefault(struct HdfSBuf ** data,struct HdfSBuf ** reply)150 static int32_t HdfSbufObtainDefault(struct HdfSBuf **data, struct HdfSBuf **reply)
151 {
152     *data = HdfSbufObtainDefaultSize();
153     if (*data == NULL) {
154         return RET_CODE_FAILURE;
155     }
156     *reply = HdfSbufObtainDefaultSize();
157     if (*reply == NULL) {
158         HdfSbufRecycle(*data);
159         return RET_CODE_FAILURE;
160     }
161     return RET_CODE_SUCCESS;
162 }
163 
WifiMsgRegisterEventListener(struct HdfDevEventlistener * listener)164 static int32_t WifiMsgRegisterEventListener(struct HdfDevEventlistener *listener)
165 {
166     struct HdfIoService *wifiService = GetWifiService();
167     if (wifiService == NULL || listener == NULL) {
168         HDF_LOGE("%s: At least one param is null", __FUNCTION__);
169         return RET_CODE_FAILURE;
170     }
171     if (HdfDeviceRegisterEventListener(wifiService, listener) != RET_CODE_SUCCESS) {
172         HDF_LOGE("%s: fail to register event listener, line: %d", __FUNCTION__, __LINE__);
173         return RET_CODE_FAILURE;
174     }
175     g_isHasRegisterListener = true;
176     return RET_CODE_SUCCESS;
177 }
178 
WifiMsgUnregisterEventListener(struct HdfDevEventlistener * listener)179 static void WifiMsgUnregisterEventListener(struct HdfDevEventlistener *listener)
180 {
181     struct HdfIoService *wifiService = GetWifiService();
182     if (listener == NULL) {
183         return;
184     }
185     if (HdfDeviceUnregisterEventListener(wifiService, listener) != HDF_SUCCESS) {
186         HDF_LOGE("%s: fail to unregister event listener, line: %d", __FUNCTION__, __LINE__);
187     }
188     g_isHasRegisterListener = false;
189 }
190 
WifiDriverClientInit(void)191 int32_t WifiDriverClientInit(void)
192 {
193     int32_t ret;
194     struct HdfIoService *wifiService = InitWifiService(DRIVER_SERVICE_NAME);
195     if (wifiService == NULL) {
196         HDF_LOGE("%s: fail to get remote service!", __FUNCTION__);
197         return RET_CODE_FAILURE;
198     }
199     g_wifiDevEventListener.onReceive = OnWiFiEvents;
200     if (g_isHasRegisterListener) {
201         HDF_LOGI("%s:has register listener!", __FUNCTION__);
202         return RET_CODE_SUCCESS;
203     }
204     ret = WifiMsgRegisterEventListener(&g_wifiDevEventListener);
205     if (ret != RET_CODE_SUCCESS) {
206         HDF_LOGE("%s: register event listener failed, line: %d", __FUNCTION__, __LINE__);
207     }
208     return ret;
209 }
210 
WifiDriverClientDeinit(void)211 void WifiDriverClientDeinit(void)
212 {
213     struct HdfIoService *wifiService = GetWifiService();
214     if (wifiService == NULL) {
215         return;
216     }
217     WifiMsgUnregisterEventListener(&g_wifiDevEventListener);
218     if (HdfIoserviceGetListenerCount(wifiService) != 0) {
219         HDF_LOGE("%s: the current EventListener is not empty. cancel the listener registration first.",
220             __FUNCTION__);
221         return;
222     }
223     ReleaseWifiService();
224 }
225 
GetUsableNetworkInfo(struct NetworkInfoResult * result)226 int32_t GetUsableNetworkInfo(struct NetworkInfoResult *result)
227 {
228     int32_t ret;
229     struct HdfSBuf *data = NULL;
230     struct HdfSBuf *reply = NULL;
231 
232     if (result == NULL) {
233         HDF_LOGE("%s params is NULL", __FUNCTION__);
234         return RET_CODE_INVALID_PARAM;
235     }
236     if (HdfSbufObtainDefault(&data, &reply) != RET_CODE_SUCCESS) {
237         return RET_CODE_FAILURE;
238     }
239     ret = SendCmdSync(WIFI_HAL_CMD_GET_NETWORK_INFO, data, reply);
240     if (ret == RET_CODE_SUCCESS) {
241         ret = ParserNetworkInfo(reply, result);
242     } else {
243         ret = RET_CODE_FAILURE;
244     }
245     HdfSbufRecycle(data);
246     HdfSbufRecycle(reply);
247     return ret;
248 }
249 
IsSupportCombo(uint8_t * isSupportCombo)250 int32_t IsSupportCombo(uint8_t *isSupportCombo)
251 {
252     int32_t ret;
253     struct HdfSBuf *data = NULL;
254     struct HdfSBuf *reply = NULL;
255 
256     if (isSupportCombo == NULL) {
257         HDF_LOGE("%s params is NULL", __FUNCTION__);
258         return RET_CODE_INVALID_PARAM;
259     }
260     if (HdfSbufObtainDefault(&data, &reply) != RET_CODE_SUCCESS) {
261         return RET_CODE_FAILURE;
262     }
263     ret = SendCmdSync(WIFI_HAL_CMD_IS_SUPPORT_COMBO, data, reply);
264     do {
265         if (ret != RET_CODE_SUCCESS) {
266             break;
267         }
268         if (!HdfSbufReadUint8(reply, isSupportCombo)) {
269             HDF_LOGE("%s: HdfSbufReadUint8 failed", __FUNCTION__);
270             ret = RET_CODE_FAILURE;
271         } else {
272             ret = RET_CODE_SUCCESS;
273         }
274     } while (0);
275     HdfSbufRecycle(data);
276     HdfSbufRecycle(reply);
277     return ret;
278 }
279 
GetComboInfo(uint64_t * comboInfo,uint32_t size)280 int32_t GetComboInfo(uint64_t *comboInfo, uint32_t size)
281 {
282     int32_t ret;
283     uint8_t isComboValid;
284     uint32_t replayDataSize = 0;
285     const uint8_t *replayData = 0;
286     struct HdfSBuf *data = NULL;
287     struct HdfSBuf *reply = NULL;
288 
289     if (comboInfo == NULL) {
290         HDF_LOGE("%s params is NULL", __FUNCTION__);
291         return RET_CODE_INVALID_PARAM;
292     }
293     if (HdfSbufObtainDefault(&data, &reply) != RET_CODE_SUCCESS) {
294         return RET_CODE_FAILURE;
295     }
296     ret = SendCmdSync(WIFI_HAL_CMD_GET_SUPPORT_COMBO, data, reply);
297     do {
298         if (ret != RET_CODE_SUCCESS) {
299             ret = RET_CODE_FAILURE;
300             break;
301         }
302         if (!HdfSbufReadUint8(reply, &isComboValid)) {
303             HDF_LOGE("%s: read combo valid flag failed", __FUNCTION__);
304             ret = RET_CODE_FAILURE;
305             break;
306         }
307         if (!isComboValid) {
308             HDF_LOGE("%s: not support combo mode", __FUNCTION__);
309             ret = RET_CODE_NOT_SUPPORT;
310             break;
311         }
312         if (!HdfSbufReadBuffer(reply, (const void **)(&replayData), &replayDataSize)) {
313             HDF_LOGE("%s: HdfSbufReadBuffer failed", __FUNCTION__);
314             ret = RET_CODE_FAILURE;
315             break;
316         }
317         if (memcpy_s(comboInfo, size, replayData, replayDataSize) != EOK) {
318             HDF_LOGE("%s: memcpy failed", __FUNCTION__);
319             ret = RET_CODE_FAILURE;
320             break;
321         }
322     } while (0);
323     HdfSbufRecycle(data);
324     HdfSbufRecycle(reply);
325     return ret;
326 }
327 
SetMacAddr(const char * ifName,unsigned char * mac,uint8_t len)328 int32_t SetMacAddr(const char *ifName, unsigned char *mac, uint8_t len)
329 {
330     int32_t ret;
331     struct HdfSBuf *data = NULL;
332     struct HdfSBuf *reply = NULL;
333 
334     if (ifName == NULL || mac == NULL) {
335         HDF_LOGE("%s params is NULL", __FUNCTION__);
336         return RET_CODE_INVALID_PARAM;
337     }
338     if (HdfSbufObtainDefault(&data, &reply) != RET_CODE_SUCCESS) {
339         return RET_CODE_FAILURE;
340     }
341     do {
342         if (!HdfSbufWriteString(data, ifName)) {
343             HDF_LOGE("%s: write ifName failed", __FUNCTION__);
344             ret = RET_CODE_FAILURE;
345             break;
346         }
347         if (!HdfSbufWriteBuffer(data, mac, len)) {
348             HDF_LOGE("%s: write mac failed", __FUNCTION__);
349             ret = RET_CODE_FAILURE;
350             break;
351         }
352         ret = SendCmdSync(WIFI_HAL_CMD_SET_MAC_ADDR, data, reply);
353     } while (0);
354     HdfSbufRecycle(data);
355     HdfSbufRecycle(reply);
356     return ret;
357 }
358 
GetDevMacAddr(const char * ifName,int32_t type,uint8_t * mac,uint8_t len)359 int32_t GetDevMacAddr(const char *ifName, int32_t type, uint8_t *mac, uint8_t len)
360 {
361     int32_t ret;
362     struct HdfSBuf *data = NULL;
363     struct HdfSBuf *reply = NULL;
364 
365     if (ifName == NULL || mac == NULL) {
366         HDF_LOGE("%s params is NULL", __FUNCTION__);
367         return RET_CODE_INVALID_PARAM;
368     }
369     if (HdfSbufObtainDefault(&data, &reply) != RET_CODE_SUCCESS) {
370         return RET_CODE_FAILURE;
371     }
372     do {
373         if (!HdfSbufWriteString(data, ifName)) {
374             HDF_LOGE("%s: write ifName failed", __FUNCTION__);
375             ret = RET_CODE_FAILURE;
376             break;
377         }
378         if (!HdfSbufWriteInt32(data, type)) {
379             HDF_LOGE("%s: write type failed", __FUNCTION__);
380             ret = RET_CODE_FAILURE;
381             break;
382         }
383         ret = SendCmdSync(WIFI_HAL_CMD_GET_DEV_MAC_ADDR, data, reply);
384         if (ret != RET_CODE_SUCCESS) {
385             ret = RET_CODE_FAILURE;
386             break;
387         }
388         ret = ParserDeviceMacAddr(reply, mac, len);
389     } while (0);
390     HdfSbufRecycle(data);
391     HdfSbufRecycle(reply);
392     return ret;
393 }
394 
GetValidFreqByBand(const char * ifName,int32_t band,struct FreqInfoResult * result,uint32_t size)395 int32_t GetValidFreqByBand(const char *ifName, int32_t band, struct FreqInfoResult *result, uint32_t size)
396 {
397     int32_t ret;
398     struct HdfSBuf *data = NULL;
399     struct HdfSBuf *reply = NULL;
400 
401     if (ifName == NULL || result == NULL || band >= IEEE80211_NUM_BANDS) {
402         HDF_LOGE("%s params is NULL", __FUNCTION__);
403         return RET_CODE_INVALID_PARAM;
404     }
405     if (HdfSbufObtainDefault(&data, &reply) != RET_CODE_SUCCESS) {
406         return RET_CODE_FAILURE;
407     }
408     do {
409         if (!HdfSbufWriteString(data, ifName)) {
410             HDF_LOGE("%s: write ifName failed", __FUNCTION__);
411             ret = RET_CODE_FAILURE;
412             break;
413         }
414         if (!HdfSbufWriteInt32(data, band)) {
415             HDF_LOGE("%s: write band failed", __FUNCTION__);
416             ret = RET_CODE_FAILURE;
417             break;
418         }
419         ret = SendCmdSync(WIFI_HAL_CMD_GET_VALID_FREQ, data, reply);
420         if (ret != RET_CODE_SUCCESS) {
421             ret = RET_CODE_FAILURE;
422             break;
423         }
424         ret = ParserFreqInfo(reply, result, size);
425     } while (0);
426     HdfSbufRecycle(data);
427     HdfSbufRecycle(reply);
428     return ret;
429 }
430 
SetTxPower(const char * ifName,int32_t power)431 int32_t SetTxPower(const char *ifName, int32_t power)
432 {
433     int32_t ret;
434     struct HdfSBuf *data = NULL;
435     struct HdfSBuf *reply = NULL;
436 
437     if (ifName == NULL) {
438         HDF_LOGE("%s params is NULL", __FUNCTION__);
439         return RET_CODE_INVALID_PARAM;
440     }
441     if (HdfSbufObtainDefault(&data, &reply) != RET_CODE_SUCCESS) {
442         return RET_CODE_FAILURE;
443     }
444     do {
445         if (!HdfSbufWriteString(data, ifName)) {
446             HDF_LOGE("%s: write ifName failed", __FUNCTION__);
447             ret = RET_CODE_FAILURE;
448             break;
449         }
450         if (!HdfSbufWriteInt32(data, power)) {
451             HDF_LOGE("%s: HdfSbufWriteInt32 failed", __FUNCTION__);
452             ret = RET_CODE_FAILURE;
453             break;
454         }
455         ret = SendCmdSync(WIFI_HAL_CMD_SET_TX_POWER, data, reply);
456     } while (0);
457     HdfSbufRecycle(data);
458     HdfSbufRecycle(reply);
459     return ret;
460 }
461 
GetAssociatedStas(const char * ifName,struct AssocStaInfoResult * result)462 int32_t GetAssociatedStas(const char *ifName, struct AssocStaInfoResult *result)
463 {
464     int32_t ret;
465     struct HdfSBuf *data = NULL;
466     struct HdfSBuf *reply = NULL;
467 
468     if (ifName == NULL || result == NULL) {
469         HDF_LOGE("%s params is NULL", __FUNCTION__);
470         return RET_CODE_INVALID_PARAM;
471     }
472     if (HdfSbufObtainDefault(&data, &reply) != RET_CODE_SUCCESS) {
473         return RET_CODE_FAILURE;
474     }
475     do {
476         if (!HdfSbufWriteString(data, ifName)) {
477             HDF_LOGE("%s: write ifName failed", __FUNCTION__);
478             ret = RET_CODE_FAILURE;
479             break;
480         }
481         ret = SendCmdSync(WIFI_HAL_CMD_GET_ASSOC_STA, data, reply);
482         if (ret != RET_CODE_SUCCESS) {
483             ret = RET_CODE_FAILURE;
484             break;
485         }
486         ret = ParserAssociatedStas(reply, result);
487     } while (0);
488     HdfSbufRecycle(data);
489     HdfSbufRecycle(reply);
490     return ret;
491 }
492 
WifiSetCountryCode(const char * ifName,const char * code,uint32_t len)493 int32_t WifiSetCountryCode(const char *ifName, const char *code, uint32_t len)
494 {
495     int32_t ret;
496     struct HdfSBuf *data = NULL;
497     struct HdfSBuf *reply = NULL;
498 
499     if (ifName == NULL || code == NULL) {
500         HDF_LOGE("%s params is NULL", __FUNCTION__);
501         return RET_CODE_INVALID_PARAM;
502     }
503     if (HdfSbufObtainDefault(&data, &reply) != RET_CODE_SUCCESS) {
504         return RET_CODE_FAILURE;
505     }
506     do {
507         if (!HdfSbufWriteString(data, ifName)) {
508             HDF_LOGE("%s: write ifName failed", __FUNCTION__);
509             ret = RET_CODE_FAILURE;
510             break;
511         }
512         if (!HdfSbufWriteBuffer(data, code, len)) {
513             HDF_LOGE("%s: write code failed", __FUNCTION__);
514             ret = RET_CODE_FAILURE;
515             break;
516         }
517         ret = SendCmdSync(WIFI_HAL_CMD_SET_COUNTRY_CODE, data, reply);
518     } while (0);
519     HdfSbufRecycle(data);
520     HdfSbufRecycle(reply);
521     return ret;
522 }
523 
SetScanMacAddr(const char * ifName,uint8_t * scanMac,uint8_t len)524 int32_t SetScanMacAddr(const char *ifName, uint8_t *scanMac, uint8_t len)
525 {
526     int32_t ret;
527     uint8_t isFuncValid;
528     struct HdfSBuf *data = NULL;
529     struct HdfSBuf *reply = NULL;
530 
531     if (ifName == NULL || scanMac == NULL) {
532         HDF_LOGE("%s params is NULL", __FUNCTION__);
533         return RET_CODE_INVALID_PARAM;
534     }
535     if (HdfSbufObtainDefault(&data, &reply) != RET_CODE_SUCCESS) {
536         return RET_CODE_FAILURE;
537     }
538     do {
539         if (!HdfSbufWriteString(data, ifName)) {
540             HDF_LOGE("%s: write ifName failed", __FUNCTION__);
541             ret = RET_CODE_FAILURE;
542             break;
543         }
544         if (!HdfSbufWriteBuffer(data, scanMac, len)) {
545             HDF_LOGE("%s: write scan mac failed", __FUNCTION__);
546             ret = RET_CODE_FAILURE;
547             break;
548         }
549         ret = SendCmdSync(WIFI_HAL_CMD_SET_SCAN_MAC_ADDR, data, reply);
550         if (ret != RET_CODE_SUCCESS) {
551             break;
552         }
553         if (!HdfSbufReadUint8(reply, &isFuncValid)) {
554             HDF_LOGE("%s: read valid flag failed", __FUNCTION__);
555             ret = RET_CODE_FAILURE;
556             break;
557         }
558         if (!isFuncValid) {
559             HDF_LOGE("%s: not support to set scan mac addr", __FUNCTION__);
560             ret = RET_CODE_NOT_SUPPORT;
561             break;
562         }
563     } while (0);
564     HdfSbufRecycle(data);
565     HdfSbufRecycle(reply);
566     return ret;
567 }
568 
AcquireChipId(const char * ifName,uint8_t * chipId)569 int32_t AcquireChipId(const char *ifName, uint8_t *chipId)
570 {
571     int32_t ret;
572     struct HdfSBuf *data = NULL;
573     struct HdfSBuf *reply = NULL;
574 
575     if (ifName == NULL || chipId == NULL) {
576         HDF_LOGE("%s params is NULL", __FUNCTION__);
577         return RET_CODE_INVALID_PARAM;
578     }
579     if (HdfSbufObtainDefault(&data, &reply) != RET_CODE_SUCCESS) {
580         return RET_CODE_FAILURE;
581     }
582     do {
583         if (!HdfSbufWriteString(data, ifName)) {
584             HDF_LOGE("%s: HdfSbufWriteString failed", __FUNCTION__);
585             ret = RET_CODE_FAILURE;
586             break;
587         }
588         ret = SendCmdSync(WIFI_HAL_CMD_GET_CHIPID, data, reply);
589         if (ret != RET_CODE_SUCCESS) {
590             break;
591         }
592         if (!HdfSbufReadUint8(reply, chipId)) {
593             HDF_LOGE("%s:  HdfSbufReadUint8 failed", __FUNCTION__);
594             ret = RET_CODE_FAILURE;
595             break;
596         }
597     } while (0);
598     HdfSbufRecycle(data);
599     HdfSbufRecycle(reply);
600     return ret;
601 }
602 
GetIfNames(struct HdfSBuf * reply,char ** ifNames,uint32_t * num)603 static int32_t GetIfNames(struct HdfSBuf *reply, char **ifNames, uint32_t *num)
604 {
605     uint32_t i;
606     uint32_t replayDataSize = 0;
607     const char *replayData = NULL;
608 
609     if (!HdfSbufReadUint32(reply, num)) {
610         HDF_LOGE("%s: HdfSbufReadUint32 failed", __FUNCTION__);
611         return RET_CODE_FAILURE;
612     }
613     *ifNames = (char *)calloc(*num, IFNAMSIZ);
614     if (*ifNames == NULL) {
615         HDF_LOGE("%s: calloc failed", __FUNCTION__);
616         return RET_CODE_FAILURE;
617     }
618 
619     if (!HdfSbufReadBuffer(reply, (const void **)(&replayData), &replayDataSize) ||
620         replayDataSize < (*num * IFNAMSIZ)) {
621         HDF_LOGE("%s: HdfSbufReadBuffer failed", __FUNCTION__);
622         free(*ifNames);
623         *ifNames = NULL;
624         return RET_CODE_FAILURE;
625     }
626 
627     for (i = 0; i < *num; i++) {
628         if (memcpy_s(*ifNames + i * IFNAMSIZ, IFNAMSIZ, replayData + i * IFNAMSIZ, replayDataSize) != EOK) {
629             HDF_LOGE("%s: memcpy failed", __FUNCTION__);
630             free(*ifNames);
631             *ifNames = NULL;
632             return RET_CODE_FAILURE;
633         }
634     }
635     return RET_CODE_SUCCESS;
636 }
637 
GetIfNamesByChipId(const uint8_t chipId,char ** ifNames,uint32_t * num)638 int32_t GetIfNamesByChipId(const uint8_t chipId, char **ifNames, uint32_t *num)
639 {
640     int32_t ret;
641     struct HdfSBuf *data = NULL;
642     struct HdfSBuf *reply = NULL;
643 
644     if (ifNames == NULL || num == NULL) {
645         HDF_LOGE("%s params is NULL", __FUNCTION__);
646         return RET_CODE_INVALID_PARAM;
647     }
648     if (HdfSbufObtainDefault(&data, &reply) != RET_CODE_SUCCESS) {
649         return RET_CODE_FAILURE;
650     }
651     do {
652         if (!HdfSbufWriteUint8(data, chipId)) {
653             HDF_LOGE("%s: HdfSbufWriteUint8 failed", __FUNCTION__);
654             ret = RET_CODE_FAILURE;
655             break;
656         }
657         ret = SendCmdSync(WIFI_HAL_CMD_GET_IFNAMES, data, reply);
658         if (ret != RET_CODE_SUCCESS) {
659             break;
660         }
661         ret = GetIfNames(reply, ifNames, num);
662     } while (0);
663     HdfSbufRecycle(data);
664     HdfSbufRecycle(reply);
665     return ret;
666 }
667 
SetResetDriver(const uint8_t chipId,const char * ifName)668 int32_t SetResetDriver(const uint8_t chipId, const char *ifName)
669 {
670     int32_t ret;
671     struct HdfSBuf *data = NULL;
672     struct HdfSBuf *reply = NULL;
673 
674     if (HdfSbufObtainDefault(&data, &reply) != RET_CODE_SUCCESS) {
675         return RET_CODE_FAILURE;
676     }
677     do {
678         if (!HdfSbufWriteUint8(data, chipId)) {
679             HDF_LOGE("%s: HdfSbufWriteUint8 failed", __FUNCTION__);
680             ret = RET_CODE_FAILURE;
681             break;
682         }
683         if (!HdfSbufWriteString(data, ifName)) {
684             HDF_LOGE("%s: Serialize failed!", __FUNCTION__);
685             ret = RET_CODE_FAILURE;
686             break;
687         }
688         ret = SendCmdSync(WIFI_HAL_CMD_RESET_DRIVER, data, reply);
689     } while (0);
690     HdfSbufRecycle(data);
691     HdfSbufRecycle(reply);
692     return ret;
693 }
694 
GetNetDeviceInfo(struct NetDeviceInfoResult * netDeviceInfoResult)695 int32_t GetNetDeviceInfo(struct NetDeviceInfoResult *netDeviceInfoResult)
696 {
697     int32_t ret;
698     struct HdfSBuf *data = NULL;
699     struct HdfSBuf *reply = NULL;
700     uint32_t netdevNum = 0;
701     uint32_t ifNameSize;
702     uint32_t macSize;
703     uint32_t i;
704     const uint8_t *replayData = NULL;
705     const char *ifName = NULL;
706 
707     if (netDeviceInfoResult == NULL) {
708         HDF_LOGE("%s: params is NULL", __FUNCTION__);
709         return RET_CODE_INVALID_PARAM;
710     }
711     if (HdfSbufObtainDefault(&data, &reply) != RET_CODE_SUCCESS) {
712         return RET_CODE_FAILURE;
713     }
714     do {
715         ret = SendCmdSync(WIFI_HAL_CMD_GET_NETDEV_INFO, data, reply);
716         if (ret != RET_CODE_SUCCESS) {
717             break;
718         }
719         if (!HdfSbufReadUint32(reply, &netdevNum)) {
720             HDF_LOGE("%s: HdfSbufReadUint32 failed", __FUNCTION__);
721             ret = RET_CODE_FAILURE;
722             break;
723         }
724         for (i = 0; i < netdevNum; i++) {
725             if (!HdfSbufReadUint32(reply, &(netDeviceInfoResult->deviceInfos[i].index)) ||
726                 !HdfSbufReadBuffer(reply, (const void **)(&ifName), &ifNameSize) ||
727                 !HdfSbufReadUint8(reply, &(netDeviceInfoResult->deviceInfos[i].iftype)) ||
728                 !HdfSbufReadBuffer(reply, (const void **)(&replayData), &macSize)) {
729                 HDF_LOGE("%s: read fail!", __FUNCTION__);
730                 ret = RET_CODE_FAILURE;
731                 break;
732             }
733             if (memcpy_s(netDeviceInfoResult->deviceInfos[i].ifName, ifNameSize, ifName, ifNameSize) != EOK) {
734                 HDF_LOGE("%s: memcpy failed", __FUNCTION__);
735                 ret = RET_CODE_FAILURE;
736                 break;
737             }
738             if (memcpy_s(netDeviceInfoResult->deviceInfos[i].mac, macSize, replayData, macSize) != EOK) {
739                 HDF_LOGE("%s: memcpy failed", __FUNCTION__);
740                 ret = RET_CODE_FAILURE;
741                 break;
742             }
743         }
744     } while (0);
745     HdfSbufRecycle(data);
746     HdfSbufRecycle(reply);
747     return ret;
748 }
749 
GetCurrentPowerMode(const char * ifName,uint8_t * mode)750 int32_t GetCurrentPowerMode(const char *ifName, uint8_t *mode)
751 {
752     int32_t ret;
753     struct HdfSBuf *data = NULL;
754     struct HdfSBuf *reply = NULL;
755 
756     if (HdfSbufObtainDefault(&data, &reply) != RET_CODE_SUCCESS) {
757         HDF_LOGE("%s: HdfSbufObtainDefault fail", __FUNCTION__);
758         return RET_CODE_FAILURE;
759     }
760 
761     do {
762         if (!HdfSbufWriteString(data, ifName)) {
763             HDF_LOGE("%s: write ifName fail!", __FUNCTION__);
764             ret = RET_CODE_FAILURE;
765             break;
766         }
767         ret = SendCmdSync(WIFI_HAL_CMD_GET_POWER_MODE, data, reply);
768         if (ret != RET_CODE_SUCCESS) {
769             HDF_LOGE("%s: SendCmdSync fail!", __FUNCTION__);
770             break;
771         }
772         if (!HdfSbufReadUint8(reply, mode)) {
773             HDF_LOGE("%s: HdfSbufReadUint8 failed", __FUNCTION__);
774             ret = RET_CODE_FAILURE;
775             break;
776         }
777     } while (0);
778 
779     HdfSbufRecycle(data);
780     HdfSbufRecycle(reply);
781     return ret;
782 }
783 
SetPowerMode(const char * ifName,uint8_t mode)784 int32_t SetPowerMode(const char *ifName, uint8_t mode)
785 {
786     int32_t ret = RET_CODE_FAILURE;
787     struct HdfSBuf *data = NULL;
788 
789     data = HdfSbufObtainDefaultSize();
790     if (data == NULL) {
791         HDF_LOGE("%s: HdfSbufObtainDefaultSize fail!", __FUNCTION__);
792         return ret;
793     }
794 
795     do {
796         if (!HdfSbufWriteString(data, ifName)) {
797             HDF_LOGE("%s: write ifName fail!", __FUNCTION__);
798             break;
799         }
800         if (!HdfSbufWriteUint8(data, mode)) {
801             HDF_LOGE("%s: write ifName fail!", __FUNCTION__);
802             break;
803         }
804         ret = SendCmdSync(WIFI_HAL_CMD_SET_POWER_MODE, data, NULL);
805     } while (0);
806 
807     HdfSbufRecycle(data);
808     return ret;
809 }
810 
WifiCmdScan(const char * ifName,WifiScan * scan)811 int32_t WifiCmdScan(const char *ifName, WifiScan *scan)
812 {
813     int32_t ret;
814     struct HdfSBuf *data = NULL;
815 
816     if (ifName == NULL || scan == NULL) {
817         HDF_LOGE("%s: Input param is null", __FUNCTION__);
818         return RET_CODE_INVALID_PARAM;
819     }
820     data = HdfSbufObtainDefaultSize();
821     if (data == NULL) {
822         HDF_LOGE("%s: HdfSbufObtainDefaultSize fail", __FUNCTION__);
823         return RET_CODE_FAILURE;
824     }
825     bool isSerializeFailed = false;
826     isSerializeFailed = isSerializeFailed || !HdfSbufWriteString(data, ifName);
827     if (scan->bssid == NULL) {
828         isSerializeFailed = isSerializeFailed || !HdfSbufWriteBuffer(data, scan->bssid, 0);
829     } else {
830         isSerializeFailed = isSerializeFailed || !HdfSbufWriteBuffer(data, scan->bssid, ETH_ADDR_LEN);
831     }
832     isSerializeFailed =
833         isSerializeFailed || !HdfSbufWriteBuffer(data, scan->ssids, sizeof(scan->ssids[0]) * scan->numSsids);
834     isSerializeFailed = isSerializeFailed || !HdfSbufWriteBuffer(data, scan->extraIes, scan->extraIesLen);
835     isSerializeFailed =
836         isSerializeFailed || !HdfSbufWriteBuffer(data, scan->freqs, sizeof(scan->freqs[0]) * scan->numFreqs);
837     isSerializeFailed = isSerializeFailed || !HdfSbufWriteUint8(data, scan->prefixSsidScanFlag);
838     isSerializeFailed = isSerializeFailed || !HdfSbufWriteUint8(data, scan->fastConnectFlag);
839     if (isSerializeFailed) {
840         HDF_LOGE("%s: Serialize failed!", __FUNCTION__);
841         ret = RET_CODE_FAILURE;
842     } else {
843         ret = SendCmdSync(WIFI_WPA_CMD_SCAN, data, NULL);
844     }
845     HdfSbufRecycle(data);
846     return ret;
847 }
848 
StartChannelMeas(const char * ifName,const struct MeasParam * measParam)849 int32_t StartChannelMeas(const char *ifName, const struct MeasParam *measParam)
850 {
851     int32_t ret = RET_CODE_FAILURE;
852     struct HdfSBuf *data = NULL;
853 
854     data = HdfSbufObtainDefaultSize();
855     if (data == NULL) {
856         HDF_LOGE("%s: HdfSbufObtainDefaultSize fail!", __FUNCTION__);
857         return ret;
858     }
859 
860     do {
861         if (!HdfSbufWriteString(data, ifName)) {
862             HDF_LOGE("%s: write ifName fail!", __FUNCTION__);
863             break;
864         }
865         if (!HdfSbufWriteBuffer(data, measParam, sizeof(struct MeasParam))) {
866             HDF_LOGE("%s: write paramBuf fail!", __FUNCTION__);
867             break;
868         }
869         ret = SendCmdSync(WIFI_HAL_CMD_START_CHANNEL_MEAS, data, NULL);
870     } while (0);
871 
872     HdfSbufRecycle(data);
873     return ret;
874 }
875 
SetProjectionScreenParam(const char * ifName,const ProjScrnCmdParam * param)876 int32_t SetProjectionScreenParam(const char *ifName, const ProjScrnCmdParam *param)
877 {
878     int32_t ret = RET_CODE_FAILURE;
879     struct HdfSBuf *req = NULL;
880 
881     req = HdfSbufObtainDefaultSize();
882     if (req == NULL) {
883         HDF_LOGE("%{public}s: HdfSbufObtainDefaultSize fail!", __FUNCTION__);
884         return ret;
885     }
886 
887     do {
888         if (!HdfSbufWriteString(req, ifName)) {
889             HDF_LOGE("%{public}s: write ifName fail!", __FUNCTION__);
890             break;
891         }
892         if (!HdfSbufWriteInt32(req, param->cmdId)) {
893             HDF_LOGE("%{public}s: write cmd fail!", __FUNCTION__);
894             break;
895         }
896         if (!HdfSbufWriteBuffer(req, param->buf, param->bufLen)) {
897             HDF_LOGE("%{public}s: write buffer data fail!", __FUNCTION__);
898             break;
899         }
900         ret = SendCmdSync(WIFI_HAL_CMD_CONFIG_PROJECTION_SCREEN, req, NULL);
901         if (ret != RET_CODE_SUCCESS) {
902             HDF_LOGE("%{public}s: SendCmdSync fail, ret = %{public}d!", __FUNCTION__, ret);
903         }
904     } while (0);
905 
906     HdfSbufRecycle(req);
907     return ret;
908 }
909 
SendCmdIoctl(const char * ifName,int32_t cmdId,const int8_t * paramBuf,uint32_t paramBufLen)910 int32_t SendCmdIoctl(const char *ifName, int32_t cmdId, const int8_t *paramBuf, uint32_t paramBufLen)
911 {
912     int ret = RET_CODE_FAILURE;
913     struct HdfSBuf *req = NULL;
914 
915     req = HdfSbufObtainDefaultSize();
916     if (req == NULL) {
917         HDF_LOGE("%{public}s: HdfSbufObtainDefaultSize fail!", __FUNCTION__);
918         return ret;
919     }
920 
921     do {
922         if (!HdfSbufWriteString(req, ifName)) {
923             HDF_LOGE("%{public}s: write ifName fail!", __FUNCTION__);
924             break;
925         }
926         if (!HdfSbufWriteInt32(req, cmdId)) {
927             HDF_LOGE("%{public}s: write cmd fail!", __FUNCTION__);
928             break;
929         }
930         if (!HdfSbufWriteBuffer(req, paramBuf, paramBufLen)) {
931             HDF_LOGE("%{public}s: write buffer data fail!", __FUNCTION__);
932             break;
933         }
934         ret = SendCmdSync(WIFI_HAL_CMD_SET_CMD_IOCTL, req, NULL);
935         if (ret != RET_CODE_SUCCESS) {
936             HDF_LOGE("%{public}s: SendCmdSync fail, ret = %{public}d!", __FUNCTION__, ret);
937         }
938     } while (0);
939 
940     HdfSbufRecycle(req);
941     return ret;
942 }
943 
GetStationInfo(const char * ifName,StationInfo * info,const uint8_t * mac,uint32_t macLen)944 int32_t GetStationInfo(const char *ifName, StationInfo *info, const uint8_t *mac, uint32_t macLen)
945 {
946     int32_t ret = RET_CODE_FAILURE;
947 
948     struct HdfSBuf *data = NULL;
949     struct HdfSBuf *reply = NULL;
950     const uint8_t *replayData = NULL;
951     uint32_t size;
952 
953     if (HdfSbufObtainDefault(&data, &reply) != RET_CODE_SUCCESS) {
954         return RET_CODE_FAILURE;
955     }
956 
957     do {
958         if (!HdfSbufWriteString(data, ifName)) {
959             HDF_LOGE("%{public}s: write ifName fail!", __FUNCTION__);
960             break;
961         }
962         if (!HdfSbufWriteBuffer(data, mac, macLen)) {
963             HDF_LOGE("%{public}s: write mac address fail!", __FUNCTION__);
964             break;
965         }
966         ret = SendCmdSync(WIFI_HAL_CMD_GET_STATION_INFO, data, reply);
967         if (ret != RET_CODE_SUCCESS) {
968             HDF_LOGE("%{public}s: SendCmdSync fail, ret = %{public}d!", __FUNCTION__, ret);
969             break;
970         }
971         if (!HdfSbufReadBuffer(reply, (const void **)(&replayData), &size)) {
972             HDF_LOGE("%{public}s: read station information fail!", __FUNCTION__);
973             ret = RET_CODE_FAILURE;
974             break;
975         }
976         if (memcpy_s(info, sizeof(StationInfo), replayData, size) != EOK) {
977             HDF_LOGE("%{public}s: memcpy_s fail", __FUNCTION__);
978             ret = RET_CODE_FAILURE;
979         }
980     } while (0);
981     HdfSbufRecycle(data);
982     HdfSbufRecycle(reply);
983     return ret;
984 }
985 #ifdef __cplusplus
986 #if __cplusplus
987 }
988 #endif
989 #endif
990