• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2020-2022 Huawei Device Co., Ltd.
3  *
4  * HDF is dual licensed: you can use it either under the terms of
5  * the GPL, or the BSD license, at your option.
6  * See the LICENSE file in the root of this repository for complete details.
7  */
8 #include "wifi_base.h"
9 #include "../hdf_wifi_core.h"
10 #include "eapol.h"
11 #include "hdf_wifi_event.h"
12 #include "hdf_wlan_chipdriver_manager.h"
13 #include "hdf_wlan_config.h"
14 #include "hdf_wlan_services.h"
15 #include "hdf_wlan_utils.h"
16 #include "message/message_router.h"
17 #include "message/sidecar.h"
18 #include "securec.h"
19 #include "wifi_mac80211_ops.h"
20 #include "wifi_module.h"
21 
22 #define HDF_LOG_TAG HDF_WIFI_CORE
23 
24 #ifndef TRUE
25 #define TRUE 1
26 #endif
27 #ifndef FALSE
28 #define FALSE 0
29 #endif
30 
31 #define WIFI_24G_CHANNEL_NUM       14
32 #define WIFI_MAX_CHANNEL_NUM       24
33 #define DEFAULT_EAPOL_PACKAGE_SIZE 800
34 #define RATE_LENGTH 6
35 Service *g_baseService = NULL;
36 
37 struct HdfWifiEventToClientMap g_hdfWifiEventToClientMap;
HdfWifiGetEventToClientMap(void)38 struct HdfWifiEventToClientMap *HdfWifiGetEventToClientMap(void)
39 {
40     return &g_hdfWifiEventToClientMap;
41 }
42 
EnableEapol(struct NetDevice * netdev,WifiEnableEapol * param)43 inline static uint32_t EnableEapol(struct NetDevice *netdev, WifiEnableEapol *param)
44 {
45     const struct Eapol *eapol = EapolGetInstance();
46     return eapol->eapolOp->enableEapol(netdev, (struct EapolEnable *)param);
47 }
DisableEapol(struct NetDevice * netdev)48 inline static uint32_t DisableEapol(struct NetDevice *netdev)
49 {
50     const struct Eapol *eapol = EapolGetInstance();
51     return eapol->eapolOp->disableEapol(netdev);
52 }
SendEapol(struct NetDevice * netdev,WifiTxEapol * txData)53 inline static uint32_t SendEapol(struct NetDevice *netdev, WifiTxEapol *txData)
54 {
55     const struct Eapol *eapol = EapolGetInstance();
56     return eapol->eapolOp->sendEapol(netdev, (struct EapolTx *)txData);
57 }
ReceiveEapol(struct NetDevice * netdev,WifiRxEapol * rxData)58 inline static uint32_t ReceiveEapol(struct NetDevice *netdev, WifiRxEapol *rxData)
59 {
60     const struct Eapol *eapol = EapolGetInstance();
61     return eapol->eapolOp->getEapol(netdev, (struct EapolRx *)rxData);
62 }
63 
SetMode(struct NetDevice * netDev,uint8_t iftype)64 static uint32_t SetMode(struct NetDevice *netDev, uint8_t iftype)
65 {
66     struct HdfChipDriver *chipDriver = GetChipDriver(netDev);
67     if (chipDriver == NULL) {
68         HDF_LOGE("%s:bad net device found!", __func__);
69         return HDF_FAILURE;
70     }
71     RETURN_IF_CHIPOPS_NOT_IMPLEMENT(chipDriver->ops, SetMode);
72     return chipDriver->ops->SetMode(netDev, iftype);
73 }
74 
AddKey(struct NetDevice * netDev,uint8_t keyIndex,bool pairwise,const uint8_t * macAddr,struct KeyParams * params)75 static uint32_t AddKey(
76     struct NetDevice *netDev, uint8_t keyIndex, bool pairwise, const uint8_t *macAddr, struct KeyParams *params)
77 {
78     struct HdfChipDriver *chipDriver = GetChipDriver(netDev);
79     if (chipDriver == NULL) {
80         HDF_LOGE("%s:bad net device found!", __func__);
81         return HDF_FAILURE;
82     }
83     RETURN_IF_CHIPOPS_NOT_IMPLEMENT(chipDriver->ops, AddKey);
84     return chipDriver->ops->AddKey(netDev, keyIndex, pairwise, macAddr, params);
85 }
86 
DelKey(struct NetDevice * netDev,uint8_t keyIndex,bool pairwise,const uint8_t * macAddr)87 static uint32_t DelKey(struct NetDevice *netDev, uint8_t keyIndex, bool pairwise, const uint8_t *macAddr)
88 {
89     struct HdfChipDriver *chipDriver = GetChipDriver(netDev);
90     if (chipDriver == NULL) {
91         HDF_LOGE("%s:bad net device found!", __func__);
92         return HDF_FAILURE;
93     }
94     RETURN_IF_CHIPOPS_NOT_IMPLEMENT(chipDriver->ops, DelKey);
95     return chipDriver->ops->DelKey(netDev, keyIndex, pairwise, macAddr);
96 }
97 
SetDefaultKey(struct NetDevice * netDev,uint8_t keyIndex,bool unicast,bool multicast)98 static uint32_t SetDefaultKey(struct NetDevice *netDev, uint8_t keyIndex, bool unicast, bool multicast)
99 {
100     struct HdfChipDriver *chipDriver = GetChipDriver(netDev);
101     if (chipDriver == NULL) {
102         HDF_LOGE("%s:bad net device found!", __func__);
103         return HDF_FAILURE;
104     }
105     RETURN_IF_CHIPOPS_NOT_IMPLEMENT(chipDriver->ops, SetDefaultKey);
106     return chipDriver->ops->SetDefaultKey(netDev, keyIndex, unicast, multicast);
107 }
108 
WifiSetKeyParams(const WifiKeyExt * keyExt,struct KeyParams * params,int8_t * pairwise)109 static void WifiSetKeyParams(const WifiKeyExt *keyExt, struct KeyParams *params, int8_t *pairwise)
110 {
111     params->key = (uint8_t *)(keyExt->key);
112     params->keyLen = (int32_t)keyExt->keyLen;
113     params->seqLen = (int32_t)keyExt->seqLen;
114     params->seq = keyExt->seq;
115     params->cipher = keyExt->cipher;
116     *pairwise = (keyExt->type == WIFI_KEYTYPE_PAIRWISE);
117 }
118 
WifiCmdFillKeyInner(struct HdfSBuf * reqData,WifiKeyExt * keyExt)119 static int32_t WifiCmdFillKeyInner(struct HdfSBuf *reqData, WifiKeyExt *keyExt)
120 {
121     uint32_t len = 0;
122 
123     if (!HdfSbufReadInt32(reqData, &(keyExt->type)) || keyExt == NULL) {
124         HDF_LOGE("%s: %s!ParamName=%s", __func__, ERROR_DESC_READ_REQ_FAILED, "type");
125         return HDF_FAILURE;
126     }
127     if (!HdfSbufReadUint32(reqData, &(keyExt->keyIdx)) || keyExt == NULL) {
128         HDF_LOGE("%s: %s!ParamName=%s", __func__, ERROR_DESC_READ_REQ_FAILED, "keyIdx");
129         return HDF_FAILURE;
130     }
131     if (!HdfSbufReadUint32(reqData, &(keyExt->cipher)) || keyExt == NULL) {
132         HDF_LOGE("%s: %s!ParamName=%s", __func__, ERROR_DESC_READ_REQ_FAILED, "cipher");
133         return HDF_FAILURE;
134     }
135     if (!HdfSbufReadUint8(reqData, &(keyExt->def)) || keyExt == NULL) {
136         HDF_LOGE("%s: %s!ParamName=%s", __func__, ERROR_DESC_READ_REQ_FAILED, "def");
137         return HDF_FAILURE;
138     }
139     if (!HdfSbufReadUint8(reqData, &(keyExt->defMgmt)) || keyExt == NULL) {
140         HDF_LOGE("%s: %s!ParamName=%s", __func__, ERROR_DESC_READ_REQ_FAILED, "defMgmt");
141         return HDF_FAILURE;
142     }
143     if (!HdfSbufReadUint8(reqData, &(keyExt->defaultTypes)) || keyExt == NULL) {
144         HDF_LOGE("%s: %s!ParamName=%s", __func__, ERROR_DESC_READ_REQ_FAILED, "defaultTypes");
145         return HDF_FAILURE;
146     }
147     if (!HdfSbufReadUint8(reqData, &(keyExt->resv)) || keyExt == NULL) {
148         HDF_LOGE("%s: %s!ParamName=%s", __func__, ERROR_DESC_READ_REQ_FAILED, "resv");
149         return HDF_FAILURE;
150     }
151     if (!HdfSbufReadBuffer(reqData, (const void **)&(keyExt->addr), &len)) {
152         HDF_LOGE("%s: %s!ParamName=%s", __func__, ERROR_DESC_READ_REQ_FAILED, "addr");
153         return HDF_FAILURE;
154     }
155     if (!HdfSbufReadBuffer(reqData, (const void **)&(keyExt->key), &(keyExt->keyLen))) {
156         HDF_LOGE("%s: %s!ParamName=%s", __func__, ERROR_DESC_READ_REQ_FAILED, "key");
157         return HDF_FAILURE;
158     }
159     if (!HdfSbufReadBuffer(reqData, (const void **)&(keyExt->seq), &(keyExt->seqLen))) {
160         HDF_LOGE("%s: %s!ParamName=%s", __func__, ERROR_DESC_READ_REQ_FAILED, "seq");
161         return HDF_FAILURE;
162     }
163     return HDF_SUCCESS;
164 }
165 
WifiCmdNewKey(const RequestContext * context,struct HdfSBuf * reqData,struct HdfSBuf * rspData)166 static int32_t WifiCmdNewKey(const RequestContext *context, struct HdfSBuf *reqData, struct HdfSBuf *rspData)
167 {
168     struct NetDevice *netDev = NULL;
169     struct KeyParams params;
170     int8_t pairwise;
171     WifiKeyExt *keyExt = NULL;
172     const char *ifName = NULL;
173     int32_t ret = 0;
174 
175     (void)context;
176     (void)rspData;
177     if (reqData == NULL) {
178         HDF_LOGE("%s: reqData is NULL", __func__);
179         return HDF_ERR_INVALID_PARAM;
180     }
181     ifName = HdfSbufReadString(reqData);
182     if (ifName == NULL) {
183         HDF_LOGE("%s: %s!ParamName=%s", __func__, ERROR_DESC_READ_REQ_FAILED, "ifName");
184         return HDF_FAILURE;
185     }
186     keyExt = (WifiKeyExt *)OsalMemCalloc(sizeof(WifiKeyExt));
187     if (keyExt == NULL) {
188         HDF_LOGE("%s:keyExt OsalMemCalloc failed", __func__);
189         return HDF_FAILURE;
190     }
191     if (WifiCmdFillKeyInner(reqData, keyExt) != HDF_SUCCESS) {
192         HDF_LOGE("%s: %s!ParamName=%s", __func__, ERROR_DESC_READ_REQ_FAILED, "keyExt");
193         OsalMemFree(keyExt);
194         return HDF_FAILURE;
195     }
196 
197     netDev = NetDeviceGetInstByName(ifName);
198     if (netDev == NULL) {
199         HDF_LOGE("%s:netDev not found!ifName=%s", __func__, ifName);
200         OsalMemFree(keyExt);
201         return HDF_FAILURE;
202     }
203 
204     (void)memset_s(&params, sizeof(struct KeyParams), 0, sizeof(struct KeyParams));
205     WifiSetKeyParams(keyExt, &params, &pairwise);
206     ret = AddKey(netDev, keyExt->keyIdx, pairwise, keyExt->addr, &params);
207     OsalMemFree(keyExt);
208     HDF_LOGD("%s: Wifi cmd newKey finished!", __func__);
209     return ret;
210 }
211 
WifiCmdDelKey(const RequestContext * context,struct HdfSBuf * reqData,struct HdfSBuf * rspData)212 static int32_t WifiCmdDelKey(const RequestContext *context, struct HdfSBuf *reqData, struct HdfSBuf *rspData)
213 {
214     int8_t pairwise;
215     struct NetDevice *netDev = NULL;
216     WifiKeyExt *keyExt = NULL;
217     const char *ifName = NULL;
218     int32_t ret = 0;
219 
220     (void)context;
221     (void)rspData;
222     if (reqData == NULL) {
223         HDF_LOGE("%s: reqData is NULL", __func__);
224         return HDF_ERR_INVALID_PARAM;
225     }
226     ifName = HdfSbufReadString(reqData);
227     if (ifName == NULL) {
228         HDF_LOGE("%s: %s!ParamName=%s", __func__, ERROR_DESC_READ_REQ_FAILED, "ifName");
229         return HDF_FAILURE;
230     }
231     keyExt = (WifiKeyExt *)OsalMemCalloc(sizeof(WifiKeyExt));
232     if (keyExt == NULL) {
233         HDF_LOGE("%s:keyExt OsalMemCalloc failed", __func__);
234         return HDF_FAILURE;
235     }
236     if (WifiCmdFillKeyInner(reqData, keyExt) != HDF_SUCCESS) {
237         HDF_LOGE("%s: %s!ParamName=%s", __func__, ERROR_DESC_READ_REQ_FAILED, "keyExt");
238         OsalMemFree(keyExt);
239         return HDF_FAILURE;
240     }
241 
242     netDev = NetDeviceGetInstByName(ifName);
243     if (netDev == NULL) {
244         HDF_LOGE("%s:netDev not found!ifName=%s", __func__, ifName);
245         OsalMemFree(keyExt);
246         return HDF_FAILURE;
247     }
248 
249     pairwise = (keyExt->type == WIFI_KEYTYPE_PAIRWISE);
250     ret = DelKey(netDev, keyExt->keyIdx, pairwise, keyExt->addr);
251     OsalMemFree(keyExt);
252     return ret;
253 }
254 
WifiGetMulticast(WifiKeyExt * keyExt)255 static uint8_t WifiGetMulticast(WifiKeyExt *keyExt)
256 {
257     uint8_t multicast = FALSE;
258 
259     if (keyExt->def == TRUE) {
260         multicast = TRUE;
261     }
262     if (keyExt->defMgmt == TRUE) {
263         multicast = TRUE;
264     }
265     if (keyExt->defaultTypes == WIFI_KEY_DEFAULT_TYPE_MULTICAST) {
266         multicast = TRUE;
267     }
268     HDF_LOGD("%s: WifiGetMulticast finished!", __func__);
269     return multicast;
270 }
271 
WifiCmdSetKey(const RequestContext * context,struct HdfSBuf * reqData,struct HdfSBuf * rspData)272 static int32_t WifiCmdSetKey(const RequestContext *context, struct HdfSBuf *reqData, struct HdfSBuf *rspData)
273 {
274     uint8_t index;
275     uint8_t unicast;
276     uint8_t multicast;
277     struct NetDevice *netDev = NULL;
278     WifiKeyExt *keyExt = NULL;
279     const char *ifName = NULL;
280     int32_t ret = 0;
281 
282     (void)context;
283     (void)rspData;
284     if (reqData == NULL) {
285         HDF_LOGE("%s: reqData is NULL", __func__);
286         return HDF_ERR_INVALID_PARAM;
287     }
288     ifName = HdfSbufReadString(reqData);
289     if (ifName == NULL) {
290         HDF_LOGE("%s: %s!ParamName=%s", __func__, ERROR_DESC_READ_REQ_FAILED, "ifName");
291         return HDF_FAILURE;
292     }
293     keyExt = (WifiKeyExt *)OsalMemCalloc(sizeof(WifiKeyExt));
294     if (keyExt == NULL) {
295         HDF_LOGE("%s:keyExt OsalMemCalloc failed", __func__);
296         return HDF_FAILURE;
297     }
298     if (WifiCmdFillKeyInner(reqData, keyExt) != HDF_SUCCESS) {
299         HDF_LOGE("%s: %s!ParamName=%s", __func__, ERROR_DESC_READ_REQ_FAILED, "keyExt");
300         OsalMemFree(keyExt);
301         return HDF_FAILURE;
302     }
303 
304     netDev = NetDeviceGetInstByName(ifName);
305     if (netDev == NULL) {
306         HDF_LOGE("%s:netDev not found!ifName=%s", __func__, ifName);
307         OsalMemFree(keyExt);
308         return HDF_FAILURE;
309     }
310 
311     index = (uint8_t)keyExt->keyIdx;
312     unicast = TRUE;
313     multicast = WifiGetMulticast(keyExt);
314     ret = SetDefaultKey(netDev, index, unicast, multicast);
315     OsalMemFree(keyExt);
316     HDF_LOGD("%s: Wifi cmd setKey finished!", __func__);
317     return ret;
318 }
319 
WifiCmdSendEapol(const RequestContext * context,struct HdfSBuf * reqData,struct HdfSBuf * rspData)320 static int32_t WifiCmdSendEapol(const RequestContext *context, struct HdfSBuf *reqData, struct HdfSBuf *rspData)
321 {
322     WifiTxEapol eapol = {0};
323     struct NetDevice *netdev = NULL;
324     const char *ifName = NULL;
325 
326     (void)context;
327     (void)rspData;
328     if (reqData == NULL) {
329         HDF_LOGE("%s: reqData is NULL", __func__);
330         return HDF_ERR_INVALID_PARAM;
331     }
332     ifName = HdfSbufReadString(reqData);
333     if (ifName == NULL) {
334         HDF_LOGE("%s: %s!ParamName=%s", __func__, ERROR_DESC_READ_REQ_FAILED, "ifName");
335         return HDF_FAILURE;
336     }
337 
338     if (!HdfSbufReadBuffer(reqData, (const void **)&(eapol.buf), &(eapol.len))) {
339         HDF_LOGE("%s: %s!ParamName=%s,readSize=%u", __func__, ERROR_DESC_READ_REQ_FAILED, "buf", eapol.len);
340         return HDF_FAILURE;
341     }
342 
343     netdev = NetDeviceGetInstByName(ifName);
344     if (netdev == NULL) {
345         HDF_LOGE("%s:netdev not found!ifName=%s", __func__, ifName);
346         return HDF_FAILURE;
347     }
348     HDF_LOGD("%s: Wifi cmd sendEapol finished!", __func__);
349     return SendEapol(netdev, &eapol);
350 }
351 
WifiCmdReceiveEapol(const RequestContext * context,struct HdfSBuf * reqData,struct HdfSBuf * rspData)352 static int32_t WifiCmdReceiveEapol(const RequestContext *context, struct HdfSBuf *reqData, struct HdfSBuf *rspData)
353 {
354     struct NetDevice *netdev = NULL;
355     WifiRxEapol eapol = {0};
356     const char *ifName = NULL;
357     int32_t ret;
358 
359     (void)context;
360     if (reqData == NULL || rspData == NULL) {
361         HDF_LOGE("%s: reqData or rspData is NULL", __func__);
362         return HDF_ERR_INVALID_PARAM;
363     }
364     ifName = HdfSbufReadString(reqData);
365     if (ifName == NULL) {
366         HDF_LOGE("%s: %s!ParamName=%s", __func__, ERROR_DESC_READ_REQ_FAILED, "ifName");
367         return HDF_FAILURE;
368     }
369 
370     netdev = NetDeviceGetInstByName(ifName);
371     if (netdev == NULL) {
372         HDF_LOGE("%s:netdev not found!ifName=%s", __func__, ifName);
373         return HDF_FAILURE;
374     }
375 
376     eapol.len = DEFAULT_EAPOL_PACKAGE_SIZE;
377     eapol.buf = OsalMemCalloc(DEFAULT_EAPOL_PACKAGE_SIZE);
378     if (eapol.buf == NULL) {
379         HDF_LOGE("%s: oom", __func__);
380         return HDF_FAILURE;
381     }
382 
383     ret = ReceiveEapol(netdev, &eapol);
384     HDF_LOGI("%s: ReceiveEapol ret=%d", __func__, ret);
385     if (!ret) {
386         if (!HdfSbufWriteBuffer(rspData, eapol.buf, eapol.len)) {
387             HDF_LOGE("%s: %s!", __func__, ERROR_DESC_WRITE_RSP_FAILED);
388             ret = HDF_ERR_IO;
389         }
390     }
391     OsalMemFree(eapol.buf);
392     return ret;
393 }
394 
WifiCmdEnableEapol(const RequestContext * context,struct HdfSBuf * reqData,struct HdfSBuf * rspData)395 static int32_t WifiCmdEnableEapol(const RequestContext *context, struct HdfSBuf *reqData, struct HdfSBuf *rspData)
396 {
397     struct NetDevice *netdev = NULL;
398     const char *ifName = NULL;
399     WifiEnableEapol eapol;
400 
401     (void)context;
402     (void)rspData;
403     if (reqData == NULL) {
404         HDF_LOGE("%s: reqData is NULL", __func__);
405         return HDF_ERR_INVALID_PARAM;
406     }
407     ifName = HdfSbufReadString(reqData);
408     if (ifName == NULL) {
409         HDF_LOGE("%s: %s!ParamName=%s", __func__, ERROR_DESC_READ_REQ_FAILED, "ifName");
410         return HDF_FAILURE;
411     }
412     netdev = NetDeviceGetInstByName(ifName);
413     if (netdev == NULL) {
414         HDF_LOGE("%s:netdev not found!ifName=%s", __func__, ifName);
415         return HDF_FAILURE;
416     }
417     eapol.callback = HdfWifiEventEapolRecv;
418     eapol.context = NULL;
419 
420     HDF_LOGD("%s: Wifi cmd EnableEapol finished", __func__);
421     return EnableEapol(netdev, &eapol);
422 }
423 
WifiCmdDisableEapol(const RequestContext * context,struct HdfSBuf * reqData,struct HdfSBuf * rspData)424 static int32_t WifiCmdDisableEapol(const RequestContext *context, struct HdfSBuf *reqData, struct HdfSBuf *rspData)
425 {
426     struct NetDevice *netdev = NULL;
427     const char *ifName = NULL;
428     (void)context;
429     (void)rspData;
430     if (reqData == NULL) {
431         HDF_LOGE("%s: reqData is NULL", __func__);
432         return HDF_ERR_INVALID_PARAM;
433     }
434     ifName = HdfSbufReadString(reqData);
435     if (ifName == NULL) {
436         HDF_LOGE("%s: %s!ParamName=%s", __func__, ERROR_DESC_READ_REQ_FAILED, "ifName");
437         return HDF_FAILURE;
438     }
439     netdev = NetDeviceGetInstByName(ifName);
440     if (netdev == NULL) {
441         HDF_LOGE("%s:netdev not found!ifName=%s", __func__, ifName);
442         return HDF_FAILURE;
443     }
444 
445     return DisableEapol(netdev);
446 }
447 
WifiCmdGetAddr(const RequestContext * context,struct HdfSBuf * reqData,struct HdfSBuf * rspData)448 static int32_t WifiCmdGetAddr(const RequestContext *context, struct HdfSBuf *reqData, struct HdfSBuf *rspData)
449 {
450     struct NetDevice *netdev = NULL;
451     const char *ifName = NULL;
452     int32_t ret = HDF_SUCCESS;
453     (void)context;
454     if (reqData == NULL || rspData == NULL) {
455         HDF_LOGE("%s: reqData or rspData is NULL", __func__);
456         return HDF_ERR_INVALID_PARAM;
457     }
458     ifName = HdfSbufReadString(reqData);
459     if (ifName == NULL) {
460         HDF_LOGE("%s: %s!ParamName=%s", __func__, ERROR_DESC_READ_REQ_FAILED, "ifName");
461         return HDF_FAILURE;
462     }
463     netdev = NetDeviceGetInstByName(ifName);
464     if (netdev == NULL) {
465         HDF_LOGE("%s: invalid netdev", __func__);
466         return HDF_FAILURE;
467     }
468     if (!HdfSbufWriteBuffer(rspData, netdev->macAddr, ETH_ADDR_LEN)) {
469         HDF_LOGE("%s: %s!", __func__, ERROR_DESC_WRITE_RSP_FAILED);
470         ret = HDF_ERR_IO;
471     }
472     HDF_LOGD("%s: Wifi cmd getAddr finished!", __func__);
473     return ret;
474 }
475 
WifiCmdSetMode(const RequestContext * context,struct HdfSBuf * reqData,struct HdfSBuf * rspData)476 static int32_t WifiCmdSetMode(const RequestContext *context, struct HdfSBuf *reqData, struct HdfSBuf *rspData)
477 {
478     struct NetDevice *netdev = NULL;
479     WifiSetMode *mode = NULL;
480     const char *ifName = NULL;
481     uint32_t dataSize = 0;
482     int32_t ret;
483 
484     (void)context;
485     (void)rspData;
486     if (reqData == NULL) {
487         HDF_LOGE("%s: reqData is NULL", __func__);
488         return HDF_ERR_INVALID_PARAM;
489     }
490     ifName = HdfSbufReadString(reqData);
491     if (ifName == NULL) {
492         HDF_LOGE("%s: %s!ParamName=%s", __func__, ERROR_DESC_READ_REQ_FAILED, "ifName");
493         return HDF_FAILURE;
494     }
495     if (!HdfSbufReadBuffer(reqData, (const void **)&mode, &dataSize) || mode == NULL ||
496         dataSize != sizeof(WifiSetMode)) {
497         HDF_LOGE("%s: %s!ParamName=%s,readSize=%u", __func__, ERROR_DESC_READ_REQ_FAILED, "mode", dataSize);
498         return HDF_FAILURE;
499     }
500     netdev = NetDeviceGetInstByName(ifName);
501     if (netdev == NULL) {
502         HDF_LOGE("%s: invalid netdev", __func__);
503         return HDF_FAILURE;
504     }
505     HDF_LOGI("%s: %s changing mode to %u ...", __func__, ifName, mode->iftype);
506     ret = SetMode(netdev, mode->iftype);
507     if (ret != HDF_SUCCESS) {
508         HDF_LOGE("%s: fail to do change intf,%d", __func__, ret);
509     } else {
510         HDF_LOGD("%s: Wifi cmd SetMode successful!", __func__);
511     }
512     return ret;
513 }
514 
WifiGetChannelData(struct WlanBand * band,WifiHwFeatureData ** featureData,struct WlanHwCapability * capability,uint32_t iee80211band)515 static void WifiGetChannelData(
516     struct WlanBand *band, WifiHwFeatureData **featureData, struct WlanHwCapability *capability, uint32_t iee80211band)
517 {
518     uint32_t loop;
519     if (band == NULL || featureData == NULL || *featureData == NULL) {
520         HDF_LOGE("%s: band or featureData is NULL", __func__);
521         return;
522     }
523 
524     (*featureData)->bands[iee80211band].channelNum = band->channelCount;
525     (*featureData)->htCapab = capability->htCapability;
526 
527     for (loop = 0; loop < band->channelCount; ++loop) {
528         (*featureData)->bands[iee80211band].iee80211Channel[loop].freq = band->channels[loop].centerFreq;
529         (*featureData)->bands[iee80211band].iee80211Channel[loop].flags = band->channels[loop].flags;
530         (*featureData)->bands[iee80211band].iee80211Channel[loop].channel = band->channels[loop].channelId;
531     }
532     HDF_LOGD("%s: WifiGetChannelData finished!", __func__);
533 }
534 
WifiFillHwFeature(struct NetDevice * netdev,WifiHwFeatureData * featureData)535 static int32_t WifiFillHwFeature(struct NetDevice *netdev, WifiHwFeatureData *featureData)
536 {
537     int32_t ret = HDF_SUCCESS;
538     struct WlanHwCapability *capability = GetHwCapability(netdev);
539     if (capability == NULL) {
540         HDF_LOGE("%s:GetHwCapability failed!", __func__);
541         return HDF_FAILURE;
542     }
543     do {
544         uint32_t loop;
545         char rate[100] = {0};
546         char *rateStr = rate;
547         if (capability->supportedRateCount > MAX_SUPPORTED_RATE) {
548             HDF_LOGE("%s: bitrates %u out of range", __func__, capability->supportedRateCount);
549             ret = HDF_FAILURE;
550             break;
551         }
552         for (loop = 0; loop < capability->supportedRateCount; ++loop) {
553             if (sprintf_s(rateStr, RATE_LENGTH, "%u,", capability->supportedRates[loop]) < 0) {
554                 HDF_LOGW("%s: Sprintf_s failed!", __func__);
555             }
556             rateStr = rate + strlen(rate);
557             featureData->bitrate[loop] = capability->supportedRates[loop];
558         }
559         HDF_LOGD("%s: supported rate %s", __func__, rate);
560 
561         if (capability->bands[IEEE80211_BAND_2GHZ] != NULL) {
562             struct WlanBand *band = capability->bands[IEEE80211_BAND_2GHZ];
563             if (band->channelCount > WIFI_24G_CHANNEL_NUM) {
564                 HDF_LOGE("%s: channels %u out of range", __func__, band->channelCount);
565                 ret = HDF_FAILURE;
566                 break;
567             }
568             WifiGetChannelData(band, &featureData, capability, IEEE80211_BAND_2GHZ);
569         }
570         if (capability->bands[IEEE80211_BAND_5GHZ] != NULL) {
571             struct WlanBand *band = capability->bands[IEEE80211_BAND_5GHZ];
572             if (band->channelCount > WIFI_MAX_CHANNEL_NUM) {
573                 HDF_LOGE("%s: channels %u out of range", __func__, band->channelCount);
574                 ret = HDF_FAILURE;
575                 break;
576             }
577             WifiGetChannelData(band, &featureData, capability, IEEE80211_BAND_5GHZ);
578         }
579     } while (false);
580 
581     if (capability->Release != NULL) {
582         capability->Release(capability);
583         capability = NULL;
584     }
585     HDF_LOGD("%s: WifiFillHwFeature finished!", __func__);
586     return ret;
587 }
588 
GetDeviceMacAddr(struct NetDevice * netdev,int32_t type,uint8_t * mac,uint8_t len)589 static uint32_t GetDeviceMacAddr(struct NetDevice *netdev, int32_t type, uint8_t *mac, uint8_t len)
590 {
591     struct HdfChipDriver *chipDriver = GetChipDriver(netdev);
592     if (chipDriver == NULL) {
593         HDF_LOGE("%s:bad net device found!", __func__);
594         return HDF_FAILURE;
595     }
596 
597     RETURN_IF_CHIPOPS_NOT_IMPLEMENT(chipDriver->ops, GetDeviceMacAddr);
598     return chipDriver->ops->GetDeviceMacAddr(netdev, type, mac, len);
599 }
600 
SetMacAddr(struct NetDevice * netdev,uint8_t * mac,uint8_t len)601 static uint32_t SetMacAddr(struct NetDevice *netdev, uint8_t *mac, uint8_t len)
602 {
603     struct HdfChipDriver *chipDriver = GetChipDriver(netdev);
604     if (chipDriver == NULL) {
605         HDF_LOGE("%s:bad net device found!", __func__);
606         return HDF_FAILURE;
607     }
608     RETURN_IF_CHIPOPS_NOT_IMPLEMENT(chipDriver->ops, SetMacAddr);
609     return chipDriver->ops->SetMacAddr(netdev, mac, len);
610 }
611 
SetTxPower(struct NetDevice * netdev,int32_t power)612 static uint32_t SetTxPower(struct NetDevice *netdev, int32_t power)
613 {
614     struct HdfChipDriver *chipDriver = GetChipDriver(netdev);
615     if (chipDriver == NULL) {
616         HDF_LOGE("%s:bad net device found!", __func__);
617         return HDF_FAILURE;
618     }
619 
620     RETURN_IF_CHIPOPS_NOT_IMPLEMENT(chipDriver->ops, SetTxPower);
621     return chipDriver->ops->SetTxPower(netdev, power);
622 }
623 
GetValidFreqsWithBand(struct NetDevice * netdev,int32_t band,int32_t * freqs,uint32_t * num)624 static uint32_t GetValidFreqsWithBand(struct NetDevice *netdev, int32_t band, int32_t *freqs, uint32_t *num)
625 {
626     struct HdfChipDriver *chipDriver = GetChipDriver(netdev);
627     if (chipDriver == NULL) {
628         HDF_LOGE("%s:bad net device found!", __func__);
629         return HDF_FAILURE;
630     }
631 
632     RETURN_IF_CHIPOPS_NOT_IMPLEMENT(chipDriver->ops, GetValidFreqsWithBand);
633     return chipDriver->ops->GetValidFreqsWithBand(netdev, band, freqs, num);
634 }
635 
WifiCmdGetHwFeature(const RequestContext * context,struct HdfSBuf * reqData,struct HdfSBuf * rspData)636 static int32_t WifiCmdGetHwFeature(const RequestContext *context, struct HdfSBuf *reqData, struct HdfSBuf *rspData)
637 {
638     struct NetDevice *netdev = NULL;
639     WifiHwFeatureData featureData = {0};
640     const char *ifName = NULL;
641     int32_t ret;
642 
643     (void)context;
644     if (reqData == NULL || rspData == NULL) {
645         HDF_LOGE("%s: reqData or rspData is NULL", __func__);
646         return HDF_ERR_INVALID_PARAM;
647     }
648     ifName = HdfSbufReadString(reqData);
649     if (ifName == NULL) {
650         HDF_LOGE("%s: %s!ParamName=%s", __func__, ERROR_DESC_READ_REQ_FAILED, "ifName");
651         return HDF_FAILURE;
652     }
653     netdev = NetDeviceGetInstByName(ifName);
654     if (netdev == NULL) {
655         HDF_LOGE("%s: invalid netdev", __func__);
656         return HDF_FAILURE;
657     }
658     ret = WifiFillHwFeature(netdev, &featureData);
659     if (ret != HDF_SUCCESS) {
660         HDF_LOGE("%s: WifiFillHwFeature failed!ret=%d", __func__, ret);
661         return HDF_FAILURE;
662     }
663     if (!HdfSbufWriteBuffer(rspData, &featureData, sizeof(featureData))) {
664         HDF_LOGE("%s: %s!", __func__, ERROR_DESC_WRITE_RSP_FAILED);
665         ret = HDF_ERR_IO;
666     }
667     HDF_LOGD("%s: WifiCmdGetHwFeature finished!", __func__);
668     return ret;
669 }
670 
SetNetIfInfo(struct NetDevice * netdev,uint32_t type)671 static int32_t SetNetIfInfo(struct NetDevice *netdev, uint32_t type)
672 {
673     int ret = 0;
674     if (netdev != NULL) {
675         ret = netdev->netDeviceIf->open(netdev);
676         (void)NetIfSetStatus(netdev, NETIF_UP);
677     }
678     if (type == WIFI_IFTYPE_AP) {
679         if (NetIfDhcpsStart(netdev, NULL, 0) == 0) {
680             HDF_LOGI("%s: Dhcp servier start ok.", __func__);
681         }
682     }
683     HDF_LOGD("%s: SetNetIfInfo finished!", __func__);
684     return ret;
685 }
686 
UnsetNetIfInfo(struct NetDevice * netdev)687 static int32_t UnsetNetIfInfo(struct NetDevice *netdev)
688 {
689     (void)netdev->netDeviceIf->stop(netdev);
690     (void)NetIfDhcpStop(netdev);
691     (void)NetIfDhcpsStop(netdev);
692     HDF_LOGD("%s: UnsetNetIfInfo finished!", __func__);
693     return NetIfSetStatus(netdev, NETIF_DOWN);
694 }
695 
SetNetworkAddr(struct NetDevice * netdev,uint32_t type)696 static void SetNetworkAddr(struct NetDevice *netdev, uint32_t type)
697 {
698     IpV4Addr ip;
699     IpV4Addr netmask;
700     IpV4Addr gw;
701     if (type == WIFI_IFTYPE_STATION) {
702         ip.addr = 0x00000000UL;
703         netmask.addr = 0x00000000UL;
704         gw.addr = 0x00000000UL;
705     } else {
706         ip.addr = 0x010ca8c0UL;
707         netmask.addr = 0x00ffffffUL;
708         gw.addr = 0x010ca8c0UL;
709     }
710 
711     if (netdev != NULL) {
712         NetIfSetAddr(netdev, &ip, &netmask, &gw);
713     }
714     HDF_LOGD("%s: SetNetworkAddr finished!", __func__);
715 }
716 
UnsetNetworkAddr(struct NetDevice * netdev)717 static void UnsetNetworkAddr(struct NetDevice *netdev)
718 {
719     IpV4Addr ip = {0x00000000UL};
720     IpV4Addr netmask = {0x00000000UL};
721     IpV4Addr gw = {0x00000000UL};
722     if (netdev != NULL) {
723         NetIfSetAddr(netdev, &ip, &netmask, &gw);
724     }
725     HDF_LOGD("%s: UnsetNetworkAddr finished!", __func__);
726 }
727 
WifiCmdSetNetdev(const RequestContext * context,struct HdfSBuf * reqData,struct HdfSBuf * rspData)728 static int32_t WifiCmdSetNetdev(const RequestContext *context, struct HdfSBuf *reqData, struct HdfSBuf *rspData)
729 {
730     WifiSetNewDev *info = NULL;
731     const char *ifName = NULL;
732     uint32_t dataSize = 0;
733     int ret = 0;
734     struct NetDevice *netdev = NULL;
735 
736     (void)context;
737     (void)rspData;
738     if (reqData == NULL) {
739         HDF_LOGE("%s: reqData is NULL", __func__);
740         return HDF_ERR_INVALID_PARAM;
741     }
742     ifName = HdfSbufReadString(reqData);
743     if (ifName == NULL) {
744         HDF_LOGE("%s: %s!ParamName=%s", __func__, ERROR_DESC_READ_REQ_FAILED, "ifName");
745         return HDF_FAILURE;
746     }
747 
748     if (!HdfSbufReadBuffer(reqData, (const void **)&info, &dataSize) || info == NULL ||
749         dataSize != sizeof(WifiSetNewDev)) {
750         HDF_LOGE("%s: %s!ParamName=%s,readSize=%u", __func__, ERROR_DESC_READ_REQ_FAILED, "devinfo", dataSize);
751         return HDF_FAILURE;
752     }
753 
754     netdev = NetDeviceGetInstByName(ifName);
755     if (netdev == NULL || netdev->netDeviceIf == NULL) {
756         HDF_LOGE("%s:netdev not found!ifName=%s", __func__, ifName);
757         return HDF_FAILURE;
758     }
759 
760     if (info->status == FALSE && NetDeviceIsInstRunning(netdev) && netdev->netDeviceIf->stop != NULL) {
761         UnsetNetworkAddr(netdev);
762         ret = UnsetNetIfInfo(netdev);
763     } else if (info->status == TRUE && !NetDeviceIsInstRunning(netdev) && netdev->netDeviceIf->open != NULL) {
764         SetNetworkAddr(netdev, info->ifType);
765         ret = SetNetIfInfo(netdev, info->ifType);
766     }
767     HDF_LOGD("%s: Wifi cmd setNetdev finished", __func__);
768     return ret;
769 }
770 
WifiSendMlme(const RequestContext * context,struct HdfSBuf * reqData,struct HdfSBuf * rspData)771 static int32_t WifiSendMlme(const RequestContext *context, struct HdfSBuf *reqData, struct HdfSBuf *rspData)
772 {
773     (void)context;
774     (void)reqData;
775     (void)rspData;
776     return HDF_SUCCESS;
777 }
778 
WifiFillActionData(struct HdfSBuf * reqData,WifiActionData * actionData)779 static int32_t WifiFillActionData(struct HdfSBuf *reqData, WifiActionData *actionData)
780 {
781     uint32_t dataSize = 0;
782 
783     if (actionData == NULL) {
784         HDF_LOGE("%s:actionData is NULL", __func__);
785         return HDF_ERR_INVALID_PARAM;
786     }
787     if (!HdfSbufReadBuffer(reqData, (const void **)&(actionData->bssid), &dataSize) || dataSize != ETH_ADDR_LEN) {
788         HDF_LOGE("%s: %s!ParamName=%s,readSize=%u", __func__, ERROR_DESC_READ_REQ_FAILED, "bssid", dataSize);
789         return HDF_FAILURE;
790     }
791 
792     if (!HdfSbufReadBuffer(reqData, (const void **)&(actionData->dst), &dataSize) || dataSize != ETH_ADDR_LEN) {
793         HDF_LOGE("%s: %s!ParamName=%s,readSize=%u", __func__, ERROR_DESC_READ_REQ_FAILED, "dst", dataSize);
794         return HDF_FAILURE;
795     }
796 
797     if (!HdfSbufReadBuffer(reqData, (const void **)&(actionData->src), &dataSize) || dataSize != ETH_ADDR_LEN) {
798         HDF_LOGE("%s: %s!ParamName=%s,readSize=%u", __func__, ERROR_DESC_READ_REQ_FAILED, "src", dataSize);
799         return HDF_FAILURE;
800     }
801 
802     if (!HdfSbufReadBuffer(reqData, (const void **)&(actionData->data), &(actionData->dataLen))) {
803         HDF_LOGE("%s: %s!ParamName=%s,readSize=%u", __func__, ERROR_DESC_READ_REQ_FAILED, "data", actionData->dataLen);
804         return HDF_FAILURE;
805     }
806     if (!HdfSbufReadUint32(reqData, &(actionData->freq)) || actionData == NULL) {
807         HDF_LOGE("%s: %s!ParamName=%s", __func__, ERROR_DESC_READ_REQ_FAILED, "freq");
808         return HDF_FAILURE;
809     }
810     if (!HdfSbufReadUint32(reqData, &(actionData->wait)) || actionData == NULL) {
811         HDF_LOGE("%s: %s!ParamName=%s", __func__, ERROR_DESC_READ_REQ_FAILED, "wait");
812         return HDF_FAILURE;
813     }
814     if (!HdfSbufReadInt32(reqData, &(actionData->noCck)) || actionData == NULL) {
815         HDF_LOGE("%s: %s!ParamName=%s", __func__, ERROR_DESC_READ_REQ_FAILED, "noCck");
816         return HDF_FAILURE;
817     }
818     HDF_LOGD("%s: actionData->freq=%d, actionData->wait=%d, actionData->noCck=%d", __func__, actionData->freq,
819         actionData->wait, actionData->noCck);
820     return HDF_SUCCESS;
821 }
822 
SendAction(struct NetDevice * netdev,WifiActionData * actionData)823 int32_t SendAction(struct NetDevice *netdev, WifiActionData *actionData)
824 {
825     struct HdfChipDriver *chipDriver = GetChipDriver(netdev);
826     if (chipDriver == NULL) {
827         HDF_LOGE("%s:bad net device found!", __func__);
828         return HDF_FAILURE;
829     }
830 
831     RETURN_IF_CHIPOPS_NOT_IMPLEMENT(chipDriver->ops, SendAction);
832     return chipDriver->ops->SendAction(netdev, actionData);
833 }
834 
WifiCmdSendAction(const RequestContext * context,struct HdfSBuf * reqData,struct HdfSBuf * rspData)835 static int32_t WifiCmdSendAction(const RequestContext *context, struct HdfSBuf *reqData, struct HdfSBuf *rspData)
836 {
837     WifiActionData actionData = {0};
838     const char *ifName = NULL;
839     struct NetDevice *netdev = NULL;
840     int ret;
841     (void)context;
842     (void)rspData;
843     if (reqData == NULL) {
844         HDF_LOGE("%s:reqData is NULL", __func__);
845         return HDF_ERR_INVALID_PARAM;
846     }
847     ifName = HdfSbufReadString(reqData);
848     if (ifName == NULL) {
849         HDF_LOGE("%s: %s!ParamName=%s", __func__, ERROR_DESC_READ_REQ_FAILED, "ifName");
850         return HDF_FAILURE;
851     }
852 
853     if (WifiFillActionData(reqData, &actionData) != HDF_SUCCESS) {
854         HDF_LOGE("%s: fill action data fail", __func__);
855         return HDF_FAILURE;
856     }
857     netdev = NetDeviceGetInstByName(ifName);
858     if (netdev == NULL || netdev->netDeviceIf == NULL) {
859         HDF_LOGE("%s:netdev not found!ifName=%s", __func__, ifName);
860         return HDF_FAILURE;
861     }
862 
863     ret = SendAction(netdev, &actionData);
864     if (ret != HDF_SUCCESS) {
865         HDF_LOGE("%s: fail to send action, ret=%d", __func__, ret);
866     } else {
867         HDF_LOGI("%s: send action success!", __func__);
868     }
869     return ret;
870 }
871 
WifiCmdGetNetworkInfo(const RequestContext * context,struct HdfSBuf * reqData,struct HdfSBuf * rspData)872 static int32_t WifiCmdGetNetworkInfo(const RequestContext *context, struct HdfSBuf *reqData, struct HdfSBuf *rspData)
873 {
874     uint32_t netDevNum;
875     char *defaultIfName = "wlan0";
876     uint8_t supportMode[PROTOCOL_80211_IFTYPE_NUM] = {0};
877     netDevNum = 1;
878     (void)context;
879     (void)reqData;
880 
881     if (!HdfSbufWriteUint32(rspData, netDevNum)) {
882         HDF_LOGE("%s: %s!", __func__, ERROR_DESC_WRITE_RSP_FAILED);
883         return HDF_FAILURE;
884     }
885     if (!HdfSbufWriteString(rspData, defaultIfName)) {
886         HDF_LOGE("%s: %s!", __func__, ERROR_DESC_WRITE_RSP_FAILED);
887         return HDF_FAILURE;
888     }
889     supportMode[PROTOCOL_80211_IFTYPE_STATION] = 1;
890     supportMode[PROTOCOL_80211_IFTYPE_AP] = 1;
891     if (!HdfSbufWriteBuffer(rspData, supportMode, PROTOCOL_80211_IFTYPE_NUM)) {
892         HDF_LOGE("%s: %s!", __func__, ERROR_DESC_WRITE_RSP_FAILED);
893         return HDF_FAILURE;
894     }
895     return HDF_SUCCESS;
896 }
897 
WifiCmdIsSupportCombo(const RequestContext * context,struct HdfSBuf * reqData,struct HdfSBuf * rspData)898 static int32_t WifiCmdIsSupportCombo(const RequestContext *context, struct HdfSBuf *reqData, struct HdfSBuf *rspData)
899 {
900     uint8_t isComboValid;
901     (void)context;
902     if (reqData == NULL || rspData == NULL) {
903         return HDF_ERR_INVALID_PARAM;
904     }
905     isComboValid = false;
906     if (!HdfSbufWriteUint8(rspData, isComboValid)) {
907         HDF_LOGE("%s: %s!", __func__, ERROR_DESC_WRITE_RSP_FAILED);
908         return HDF_FAILURE;
909     }
910     return HDF_SUCCESS;
911 }
912 
WifiCmdGetSupportCombo(const RequestContext * context,struct HdfSBuf * reqData,struct HdfSBuf * rspData)913 static int32_t WifiCmdGetSupportCombo(const RequestContext *context, struct HdfSBuf *reqData, struct HdfSBuf *rspData)
914 {
915     uint8_t isComboValid;
916     (void)context;
917     if (reqData == NULL || rspData == NULL) {
918         return HDF_ERR_INVALID_PARAM;
919     }
920     isComboValid = false;
921     if (!HdfSbufWriteUint8(rspData, isComboValid)) {
922         HDF_LOGE("%s: write fail", __func__);
923         return HDF_FAILURE;
924     }
925     return HDF_SUCCESS;
926 }
927 
WifiCmdGetDevMacAddr(const RequestContext * context,struct HdfSBuf * reqData,struct HdfSBuf * rspData)928 static int32_t WifiCmdGetDevMacAddr(const RequestContext *context, struct HdfSBuf *reqData, struct HdfSBuf *rspData)
929 {
930     int32_t ret;
931     struct NetDevice *netdev = NULL;
932     const char *ifName = NULL;
933     uint8_t isEfuseSavedMac;
934     int32_t type;
935     unsigned char devMac[IEEE80211_MAC_ADDR_LEN] = {0};
936 
937     (void)context;
938     if (reqData == NULL || rspData == NULL) {
939         return HDF_ERR_INVALID_PARAM;
940     }
941     ifName = HdfSbufReadString(reqData);
942     if (ifName == NULL) {
943         HDF_LOGE("%s: %s!ParamName=%s", __func__, ERROR_DESC_READ_REQ_FAILED, "ifName");
944         return HDF_FAILURE;
945     }
946     netdev = NetDeviceGetInstByName(ifName);
947     if (netdev == NULL) {
948         HDF_LOGE("%s:netdev not found!ifName=%s", __func__, ifName);
949         return HDF_FAILURE;
950     }
951     if (!HdfSbufReadInt32(reqData, &type)) {
952         HDF_LOGE("%s: %s!ParamName=%s", __func__, ERROR_DESC_READ_REQ_FAILED, "type");
953         return HDF_FAILURE;
954     }
955     ret = GetDeviceMacAddr(netdev, type, devMac, IEEE80211_MAC_ADDR_LEN);
956     if (ret && ret != HDF_ERR_NOT_SUPPORT) {
957         HDF_LOGE("%s: fail to get device mac addr,%d", __func__, ret);
958         return ret;
959     }
960     isEfuseSavedMac = (ret == HDF_ERR_NOT_SUPPORT) ? false : true;
961     if (!HdfSbufWriteUint8(rspData, isEfuseSavedMac)) {
962         HDF_LOGE("%s: %s!", __func__, ERROR_DESC_WRITE_RSP_FAILED);
963         return HDF_FAILURE;
964     }
965     if (!HdfSbufWriteBuffer(rspData, devMac, IEEE80211_MAC_ADDR_LEN)) {
966         HDF_LOGE("%s: %s!", __func__, ERROR_DESC_WRITE_RSP_FAILED);
967         ret = HDF_FAILURE;
968     }
969     return ret;
970 }
971 
WifiCmdSetMacAddr(const RequestContext * context,struct HdfSBuf * reqData,struct HdfSBuf * rspData)972 static int32_t WifiCmdSetMacAddr(const RequestContext *context, struct HdfSBuf *reqData, struct HdfSBuf *rspData)
973 {
974     int32_t ret;
975     struct NetDevice *netdev = NULL;
976     const char *ifName = NULL;
977     unsigned char *mac = NULL;
978     uint32_t replayDataSize;
979 
980     (void)context;
981     if (reqData == NULL || rspData == NULL) {
982         return HDF_ERR_INVALID_PARAM;
983     }
984     ifName = HdfSbufReadString(reqData);
985     if (ifName == NULL) {
986         HDF_LOGE("%s: %s!ParamName=%s", __func__, ERROR_DESC_READ_REQ_FAILED, "ifName");
987         return HDF_FAILURE;
988     }
989     netdev = NetDeviceGetInstByName(ifName);
990     if (netdev == NULL) {
991         HDF_LOGE("%s:netdev not found!ifName=%s", __func__, ifName);
992         return HDF_FAILURE;
993     }
994     if (!HdfSbufReadBuffer(reqData, (const void **)&mac, &replayDataSize) || mac == NULL ||
995         replayDataSize != IEEE80211_MAC_ADDR_LEN) {
996         HDF_LOGE("%s: %s!ParamName=%s", __func__, ERROR_DESC_READ_REQ_FAILED, "mac");
997         return HDF_FAILURE;
998     }
999     ret = SetMacAddr(netdev, mac, IEEE80211_MAC_ADDR_LEN);
1000     if (ret != HDF_SUCCESS) {
1001         HDF_LOGE("%s: fail to set mac addr,%d", __func__, ret);
1002     }
1003     return ret;
1004 }
1005 
WifiCmdGetValidFreqsWithBand(const RequestContext * context,struct HdfSBuf * reqData,struct HdfSBuf * rspData)1006 static int32_t WifiCmdGetValidFreqsWithBand(
1007     const RequestContext *context, struct HdfSBuf *reqData, struct HdfSBuf *rspData)
1008 {
1009     int32_t ret;
1010     struct NetDevice *netdev = NULL;
1011     const char *ifName = NULL;
1012     int32_t band;
1013     int32_t freqs[WIFI_24G_CHANNEL_NUM] = {0};
1014     uint32_t num = 0;
1015 
1016     (void)context;
1017     if (reqData == NULL || rspData == NULL) {
1018         return HDF_ERR_INVALID_PARAM;
1019     }
1020     ifName = HdfSbufReadString(reqData);
1021     if (ifName == NULL) {
1022         HDF_LOGE("%s: %s!ParamName=%s", __func__, ERROR_DESC_READ_REQ_FAILED, "ifName");
1023         return HDF_FAILURE;
1024     }
1025     netdev = NetDeviceGetInstByName(ifName);
1026     if (netdev == NULL) {
1027         HDF_LOGE("%s:netdev not found!ifName=%s", __func__, ifName);
1028         return HDF_FAILURE;
1029     }
1030     if (!HdfSbufReadInt32(reqData, &band)) {
1031         HDF_LOGE("%s: %s!ParamName=%s", __func__, ERROR_DESC_READ_REQ_FAILED, "band");
1032         return HDF_FAILURE;
1033     }
1034     ret = GetValidFreqsWithBand(netdev, band, freqs, &num);
1035     if (ret != HDF_SUCCESS) {
1036         HDF_LOGE("%s: fail to get valid freqs,%d", __func__, ret);
1037         return ret;
1038     }
1039     if (!HdfSbufWriteUint32(rspData, num)) {
1040         HDF_LOGE("%s: %s!", __func__, ERROR_DESC_WRITE_RSP_FAILED);
1041         return HDF_FAILURE;
1042     }
1043     if (!HdfSbufWriteBuffer(rspData, freqs, num * sizeof(int32_t))) {
1044         HDF_LOGE("%s: %s!", __func__, ERROR_DESC_WRITE_RSP_FAILED);
1045         return HDF_FAILURE;
1046     }
1047     return ret;
1048 }
1049 
WifiCmdSetTxPower(const RequestContext * context,struct HdfSBuf * reqData,struct HdfSBuf * rspData)1050 static int32_t WifiCmdSetTxPower(const RequestContext *context, struct HdfSBuf *reqData, struct HdfSBuf *rspData)
1051 {
1052     int32_t ret;
1053     struct NetDevice *netdev = NULL;
1054     const char *ifName = NULL;
1055     int32_t power;
1056 
1057     (void)context;
1058     if (reqData == NULL || rspData == NULL) {
1059         return HDF_ERR_INVALID_PARAM;
1060     }
1061     ifName = HdfSbufReadString(reqData);
1062     if (ifName == NULL) {
1063         HDF_LOGE("%s: %s!ParamName=%s", __func__, ERROR_DESC_READ_REQ_FAILED, "ifName");
1064         return HDF_FAILURE;
1065     }
1066     netdev = NetDeviceGetInstByName(ifName);
1067     if (netdev == NULL) {
1068         HDF_LOGE("%s:netdev not found!ifName=%s", __func__, ifName);
1069         return HDF_FAILURE;
1070     }
1071     if (!HdfSbufReadInt32(reqData, &power) || power < 0) {
1072         HDF_LOGE("%s: %s!ParamName=%s", __func__, ERROR_DESC_READ_REQ_FAILED, "power");
1073         return HDF_FAILURE;
1074     }
1075     ret = SetTxPower(netdev, power);
1076     if (ret != HDF_SUCCESS) {
1077         HDF_LOGE("%s: fail to set tx power,%d", __func__, ret);
1078     }
1079     return ret;
1080 }
1081 
WifiCmdSetClient(const RequestContext * context,struct HdfSBuf * reqData,struct HdfSBuf * rspData)1082 static int32_t WifiCmdSetClient(const RequestContext *context, struct HdfSBuf *reqData, struct HdfSBuf *rspData)
1083 {
1084     uint32_t clientNum = 0;
1085     struct HdfWifiEventToClientMap *eventToClientMap = NULL;
1086     (void)rspData;
1087     if (reqData == NULL || context == NULL) {
1088         return HDF_ERR_INVALID_PARAM;
1089     }
1090 
1091     if (!HdfSbufReadUint32(reqData, &clientNum)) {
1092         HDF_LOGE("%s: %s!ParamName=%s", __func__, ERROR_DESC_READ_REQ_FAILED, "clientNum");
1093         return HDF_FAILURE;
1094     }
1095 
1096     eventToClientMap = HdfWifiGetEventToClientMap();
1097     if (eventToClientMap == NULL) {
1098         HDF_LOGE("%s:get HdfWifiEventToClientMap failed", __func__);
1099         return HDF_FAILURE;
1100     }
1101 
1102     if (clientNum == WIFI_KERNEL_TO_WPA_CLIENT) {
1103         eventToClientMap->wpaClient = context->client;
1104     } else if (clientNum == WIFI_KERNEL_TO_HAL_CLIENT) {
1105         eventToClientMap->halClient = context->client;
1106     }
1107     return HDF_SUCCESS;
1108 }
1109 
HdfdWlanGetChipId(const char * ifName,uint8_t * chipId)1110 static int32_t HdfdWlanGetChipId(const char *ifName, uint8_t *chipId)
1111 {
1112     struct NetDevice *netdev = NULL;
1113     struct HdfWifiNetDeviceData *netDeviceData = NULL;
1114 
1115     if (ifName == NULL || chipId == NULL) {
1116         HDF_LOGE("%s: para is NULL", __func__);
1117         return HDF_FAILURE;
1118     }
1119     netdev = NetDeviceGetInstByName(ifName);
1120     if (netdev == NULL) {
1121         HDF_LOGE("%s:netdev not found!ifName=%s.", __func__, ifName);
1122         return HDF_FAILURE;
1123     }
1124     netDeviceData = GetPlatformData(netdev);
1125     if (netDeviceData == NULL) {
1126         HDF_LOGE("%s:platform netDeviceData is NULL!", __func__);
1127         return HDF_FAILURE;
1128     }
1129 
1130     *chipId = netDeviceData->device->id;
1131     return HDF_SUCCESS;
1132 }
1133 
WifiCmdGetChipId(const RequestContext * context,struct HdfSBuf * reqData,struct HdfSBuf * rspData)1134 static int32_t WifiCmdGetChipId(const RequestContext *context, struct HdfSBuf *reqData, struct HdfSBuf *rspData)
1135 {
1136     int32_t ret;
1137     const char *ifName = NULL;
1138     uint8_t chipId = 0;
1139 
1140     (void)context;
1141     if (reqData == NULL || rspData == NULL) {
1142         return HDF_ERR_INVALID_PARAM;
1143     }
1144     ifName = HdfSbufReadString(reqData);
1145     if (ifName == NULL) {
1146         HDF_LOGE("%s: %s!ParamName=%s", __func__, ERROR_DESC_READ_REQ_FAILED, "ifName");
1147         return HDF_FAILURE;
1148     }
1149 
1150     ret = HdfdWlanGetChipId(ifName, &chipId);
1151     if (ret != HDF_SUCCESS) {
1152         HDF_LOGE("%s: fail to get chipId, %d", __func__, ret);
1153         return HDF_FAILURE;
1154     }
1155     HDF_LOGE("%s: chipid = %hhu.", __func__, chipId);
1156     if (!HdfSbufWriteUint8(rspData, chipId)) {
1157         HDF_LOGE("%s: %s!", __func__, ERROR_DESC_WRITE_RSP_FAILED);
1158         return HDF_FAILURE;
1159     }
1160     return ret;
1161 }
1162 
WifiCmdGetIfNamesByChipId(const RequestContext * context,struct HdfSBuf * reqData,struct HdfSBuf * rspData)1163 static int32_t WifiCmdGetIfNamesByChipId(
1164     const RequestContext *context, struct HdfSBuf *reqData, struct HdfSBuf *rspData)
1165 {
1166     uint8_t chipId;
1167     uint8_t ifNameCount = 0;
1168     char *ifNames = NULL;
1169     int32_t ret = HDF_SUCCESS;
1170 
1171     (void)context;
1172     if (reqData == NULL || rspData == NULL) {
1173         return HDF_ERR_INVALID_PARAM;
1174     }
1175     if (!HdfSbufReadUint8(reqData, &chipId)) {
1176         HDF_LOGE("%s: %s!ParamName=%s", __func__, ERROR_DESC_READ_REQ_FAILED, "chipId");
1177         return HDF_FAILURE;
1178     }
1179 
1180     ifNames = HdfWlanGetIfNames(chipId, &ifNameCount);
1181     if (ifNames == NULL) {
1182         HDF_LOGE("%s: fail to get ifnames!", __func__);
1183         return HDF_FAILURE;
1184     }
1185     HDF_LOGI("%s: get ifName num: %hhu.\n", __func__, ifNameCount);
1186     do {
1187         if (!HdfSbufWriteUint32(rspData, ifNameCount)) {
1188             HDF_LOGE("%s: %s!", __func__, ERROR_DESC_WRITE_RSP_FAILED);
1189             ret = HDF_FAILURE;
1190             break;
1191         }
1192 
1193         if (!HdfSbufWriteBuffer(rspData, ifNames, ifNameCount * IFNAMSIZ)) {
1194             HDF_LOGE("%s: %s!", __func__, ERROR_DESC_WRITE_RSP_FAILED);
1195             ret = HDF_FAILURE;
1196             break;
1197         }
1198     } while (false);
1199     OsalMemFree(ifNames);
1200     return ret;
1201 }
1202 
WifiResetEntranceCheck(const uint8_t chipId)1203 static int32_t WifiResetEntranceCheck(const uint8_t chipId)
1204 {
1205     uint8_t i;
1206     int32_t ret = HDF_SUCCESS;
1207     uint8_t ifNameCount = 0;
1208     char *ifNames = NULL;
1209     struct NetDevice *netdev = NULL;
1210 
1211     ifNames = HdfWlanGetIfNames(chipId, &ifNameCount);
1212     if (ifNames == NULL) {
1213         HDF_LOGE("%s: HdfWlanGetIfNames failed!", __func__);
1214         return HDF_FAILURE;
1215     }
1216     /* check the netDevice is busy or not, do reset when it is free. */
1217     for (i = 0; i < ifNameCount; i++) {
1218         netdev = NetDeviceGetInstByName(ifNames + i * IFNAMSIZ);
1219         if (netdev == NULL) {
1220             HDF_LOGE("%s:netdev not found!ifName=%s", __func__, ifNames + i * IFNAMSIZ);
1221             continue;
1222         }
1223         if (NetDeviceIsInstRunning(netdev)) {
1224             HDF_LOGE("%s: the netdevice is using!", __func__);
1225             ret = ERR_NETDEVICE_IS_USING;
1226             break;
1227         }
1228     }
1229     OsalMemFree(ifNames);
1230     return ret;
1231 }
1232 
ResetParaCheck(const RequestContext * context,const struct HdfSBuf * reqData,const struct HdfSBuf * rspData)1233 static int32_t ResetParaCheck(const RequestContext *context, const struct HdfSBuf *reqData,
1234     const struct HdfSBuf *rspData)
1235 {
1236     if (context == NULL || reqData == NULL || rspData == NULL) {
1237         HDF_LOGE("%s: para is null!", __func__);
1238         return HDF_FAILURE;
1239     }
1240 
1241     if (context->senderId != BASE_SERVICE_ID) {
1242         HDF_LOGE("%s: the senderId is error!", __func__);
1243         return HDF_FAILURE;
1244     }
1245     return HDF_SUCCESS;
1246 }
1247 
WifiCmdDoResetChip(const RequestContext * context,struct HdfSBuf * reqData,struct HdfSBuf * rspData)1248 static int32_t WifiCmdDoResetChip(const RequestContext *context, struct HdfSBuf *reqData, struct HdfSBuf *rspData)
1249 {
1250     int32_t ret;
1251     uint8_t chipId;
1252     const char *ifName = NULL;
1253     struct HdfWlanDevice *wlanDevice = NULL;
1254 
1255     ret = ResetParaCheck(context, reqData, rspData);
1256     if (ret != HDF_SUCCESS) {
1257         return HDF_FAILURE;
1258     }
1259 
1260     if (!HdfSbufReadUint8(reqData, &chipId)) {
1261         HDF_LOGE("%s: %s!ParamName=%s", __func__, ERROR_DESC_READ_REQ_FAILED, "chipId");
1262         return HDF_FAILURE;
1263     }
1264     ifName = HdfSbufReadString(reqData);
1265     if (ifName == NULL) {
1266         HDF_LOGE("%s: %s!ParamName=%s", __func__, ERROR_DESC_READ_REQ_FAILED, "ifName");
1267         return HDF_FAILURE;
1268     }
1269     /* callback function use chipId */
1270     if (!HdfSbufWriteUint8(rspData, chipId)) {
1271         HDF_LOGE("%s: %s!", __func__, ERROR_DESC_WRITE_RSP_FAILED);
1272         return HDF_FAILURE;
1273     }
1274     if (!HdfSbufWriteString(rspData, ifName)) {
1275         HDF_LOGE("%s: Serialize failed!", __func__);
1276         return HDF_FAILURE;
1277     }
1278 
1279     ret = WifiResetEntranceCheck(chipId);
1280     if (ret != HDF_SUCCESS) {
1281         return ret;
1282     }
1283 
1284     wlanDevice = HdfWlanGetWlanDevice(chipId);
1285     if (wlanDevice == NULL || wlanDevice->reset == NULL) {
1286         HDF_LOGE("%s: wlanDevice or wlanDevice->reset is NULL!", __func__);
1287         return HDF_FAILURE;
1288     }
1289 
1290     ret = HdfWifiDeinitDevice(wlanDevice);
1291     if (ret != HDF_SUCCESS) {
1292         return ret;
1293     }
1294 
1295     /* power reset */
1296     ret = wlanDevice->reset->Reset(wlanDevice->reset);
1297     if (ret != HDF_SUCCESS) {
1298         HDF_LOGE("%s:power reset failed!", __func__);
1299         return ERR_POWER_RESET_FAIL;
1300     }
1301     ret = HdfWifiInitDevice(wlanDevice);
1302     return ret;
1303 }
1304 
SendMessageResetDriverCallBack(const RequestContext * context,struct HdfSBuf * reqData,struct HdfSBuf * rspData,ErrorCode rspCode)1305 void SendMessageResetDriverCallBack(
1306     const RequestContext *context, struct HdfSBuf *reqData, struct HdfSBuf *rspData, ErrorCode rspCode)
1307 {
1308     uint8_t chipId;
1309     int32_t ret;
1310     const char *ifName = NULL;
1311     (void)context;
1312 
1313     if (rspData == NULL || reqData == NULL) {
1314         HDF_LOGE("%s: para is null!", __func__);
1315         return;
1316     }
1317 
1318     if (!HdfSbufReadUint8(rspData, &chipId)) {
1319         HDF_LOGE("%s: read data failed! ParamName=%s", __func__, "chipId");
1320         return;
1321     }
1322     ifName = HdfSbufReadString(rspData);
1323     if (ifName == NULL) {
1324         HDF_LOGE("%s: %s!ParamName=%s", __func__, ERROR_DESC_READ_REQ_FAILED, "ifName");
1325         return;
1326     }
1327 
1328     ret = HdfWifiEventResetResult(chipId, rspCode, ifName);
1329     if (ret != HDF_SUCCESS) {
1330         HDF_LOGE("%s: send resetDriver event fail!", __func__);
1331     }
1332     return;
1333 }
1334 
WifiCmdResetDriver(const RequestContext * context,struct HdfSBuf * reqData,struct HdfSBuf * rspData)1335 static int32_t WifiCmdResetDriver(const RequestContext *context, struct HdfSBuf *reqData, struct HdfSBuf *rspData)
1336 {
1337     int32_t ret;
1338     struct HdfSBuf *data = NULL;
1339     (void)context;
1340     if (reqData == NULL || rspData == NULL) {
1341         return HDF_ERR_INVALID_PARAM;
1342     }
1343 
1344     data = HdfSbufCopy(reqData);
1345     if (data == NULL) {
1346         HDF_LOGE("%s: sbuf copy fail", __func__);
1347         return HDF_FAILURE;
1348     }
1349 
1350     ret = g_baseService->SendAsyncMessage(
1351         g_baseService, BASE_SERVICE_ID, CMD_BASE_DO_RESET_PRIVATE, data, SendMessageResetDriverCallBack);
1352     if (ret != HDF_SUCCESS) {
1353         HdfSbufRecycle(data);
1354         HDF_LOGE("%s: fail to reset the driver,%d", __func__, ret);
1355     }
1356     return ret;
1357 }
1358 
GetIftype(struct NetDevice * netdev,uint8_t * iftype)1359 static uint32_t GetIftype(struct NetDevice *netdev, uint8_t *iftype)
1360 {
1361     struct HdfChipDriver *chipDriver = GetChipDriver(netdev);
1362     if (chipDriver == NULL) {
1363         HDF_LOGE("%s:bad net device found!", __func__);
1364         return HDF_FAILURE;
1365     }
1366     RETURN_IF_CHIPOPS_NOT_IMPLEMENT(chipDriver->ops, GetIftype);
1367     return chipDriver->ops->GetIftype(netdev, iftype);
1368 }
1369 
1370 #define MAX_NETDEVICE_COUNT 20
WifiCmdGetNetDevInfo(const RequestContext * context,struct HdfSBuf * reqData,struct HdfSBuf * rspData)1371 static int32_t WifiCmdGetNetDevInfo(const RequestContext *context, struct HdfSBuf *reqData, struct HdfSBuf *rspData)
1372 {
1373     uint32_t i;
1374     uint32_t netdevNum;
1375     uint8_t iftype;
1376     struct NetDevice *netDev = NULL;
1377     (void)context;
1378     (void)reqData;
1379 
1380     netdevNum = NetDevGetRegisterCount();
1381     if (!HdfSbufWriteUint32(rspData, netdevNum)) {
1382         HDF_LOGE("%s: %s!", __func__, ERROR_DESC_WRITE_RSP_FAILED);
1383         return HDF_FAILURE;
1384     }
1385     for (i = 0; i < MAX_NETDEVICE_COUNT; i++) {
1386         netDev = NetDeviceGetInstByIndex(i);
1387         if (netDev != NULL) {
1388             if (GetIftype(netDev, &iftype) != HDF_SUCCESS) {
1389                 iftype = 0;
1390             }
1391             if (!HdfSbufWriteUint32(rspData, i) ||
1392                 !HdfSbufWriteBuffer(rspData, netDev->name, strlen(netDev->name) + 1) ||
1393                 !HdfSbufWriteUint8(rspData, iftype) ||
1394                 !HdfSbufWriteBuffer(rspData, GET_NET_DEV_MAC_ADDR(netDev), ETH_ADDR_LEN)) {
1395                 HDF_LOGE("%s: %s!", __func__, ERROR_DESC_WRITE_RSP_FAILED);
1396                 return HDF_FAILURE;
1397             }
1398         }
1399     }
1400     return HDF_SUCCESS;
1401 }
1402 
HdfdWlanGetPowerMode(const char * ifName,uint8_t * mode)1403 static uint32_t HdfdWlanGetPowerMode(const char *ifName, uint8_t *mode)
1404 {
1405     struct NetDevice *netdev = NULL;
1406 
1407     if (ifName == NULL || mode == NULL) {
1408         HDF_LOGE("%s: Invalid parameter", __func__);
1409         return HDF_FAILURE;
1410     }
1411     netdev = NetDeviceGetInstByName(ifName);
1412     if (netdev == NULL) {
1413         HDF_LOGE("%s:netdev not found!ifName=%s.", __func__, ifName);
1414         return HDF_FAILURE;
1415     }
1416     struct HdfChipDriver *chipDriver = GetChipDriver(netdev);
1417     if (chipDriver == NULL) {
1418         HDF_LOGE("%s:bad net device found!", __func__);
1419         return HDF_FAILURE;
1420     }
1421     if (chipDriver->ops == NULL) {
1422         return HDF_ERR_INVALID_OBJECT;
1423     }
1424     if (chipDriver->ops->GetPowerMode == NULL) {
1425         *mode = WIFI_POWER_MODE_GENERAL;
1426         HDF_LOGE("%s: chipDriver->ops->GetPowerMode is null\r\n", __func__);
1427         return HDF_ERR_NOT_SUPPORT;
1428     }
1429     return chipDriver->ops->GetPowerMode(netdev, mode);
1430 }
1431 
WifiCmdGetPowerMode(const RequestContext * context,struct HdfSBuf * reqData,struct HdfSBuf * rspData)1432 static int32_t WifiCmdGetPowerMode(const RequestContext *context, struct HdfSBuf *reqData, struct HdfSBuf *rspData)
1433 {
1434     int32_t ret;
1435     const char *ifName = NULL;
1436     uint8_t mode;
1437     (void)context;
1438 
1439     if (reqData == NULL || rspData == NULL) {
1440         return HDF_ERR_INVALID_PARAM;
1441     }
1442     ifName = HdfSbufReadString(reqData);
1443     if (ifName == NULL) {
1444         HDF_LOGE("%s: %s!ParamName=%s", __func__, ERROR_DESC_READ_REQ_FAILED, "ifName");
1445         return HDF_FAILURE;
1446     }
1447     ret = HdfdWlanGetPowerMode(ifName, &mode);
1448     if (ret != HDF_SUCCESS) {
1449         HDF_LOGE("%s: fail to get power mode, %d", __func__, ret);
1450         return ret;
1451     }
1452     if (!HdfSbufWriteUint8(rspData, mode)) {
1453         HDF_LOGE("%s: %s!", __func__, ERROR_DESC_WRITE_RSP_FAILED);
1454         return HDF_FAILURE;
1455     }
1456 
1457     return HDF_SUCCESS;
1458 }
1459 
HdfdWlanSetPowerMode(const char * ifName,uint8_t mode)1460 static uint32_t HdfdWlanSetPowerMode(const char *ifName, uint8_t mode)
1461 {
1462     struct NetDevice *netdev = NULL;
1463 
1464     if (ifName == NULL || mode >= WIFI_POWER_MODE_NUM) {
1465         HDF_LOGE("%s: Invalid parameter", __func__);
1466         return HDF_FAILURE;
1467     }
1468     netdev = NetDeviceGetInstByName(ifName);
1469     if (netdev == NULL) {
1470         HDF_LOGE("%s:netdev not found!ifName=%s.", __func__, ifName);
1471         return HDF_FAILURE;
1472     }
1473     struct HdfChipDriver *chipDriver = GetChipDriver(netdev);
1474     if (chipDriver == NULL) {
1475         HDF_LOGE("%s:bad net device found!", __func__);
1476         return HDF_FAILURE;
1477     }
1478     if (chipDriver->ops == NULL) {
1479         return HDF_ERR_INVALID_OBJECT;
1480     }
1481 
1482     if (chipDriver->ops->SetPowerMode == NULL) {
1483         HDF_LOGE("%s: chipDriver->ops->SetPowerMode is null\r\n", __func__);
1484         return HDF_ERR_NOT_SUPPORT;
1485     }
1486     return chipDriver->ops->SetPowerMode(netdev, mode);
1487 }
1488 
WifiCmdSetPowerMode(const RequestContext * context,struct HdfSBuf * reqData,struct HdfSBuf * rspData)1489 static int32_t WifiCmdSetPowerMode(const RequestContext *context, struct HdfSBuf *reqData, struct HdfSBuf *rspData)
1490 {
1491     int32_t ret;
1492     const char *ifName = NULL;
1493     uint8_t mode;
1494     (void)context;
1495 
1496     if (reqData == NULL || rspData == NULL) {
1497         return HDF_ERR_INVALID_PARAM;
1498     }
1499     ifName = HdfSbufReadString(reqData);
1500     if (ifName == NULL) {
1501         HDF_LOGE("%s: %s!ParamName=%s", __func__, ERROR_DESC_READ_REQ_FAILED, "ifName");
1502         return HDF_FAILURE;
1503     }
1504     if (!HdfSbufReadUint8(reqData, &mode)) {
1505         HDF_LOGE("%s: read data failed! ParamName=%s", __func__, "power mode");
1506         return HDF_FAILURE;
1507     }
1508     ret = HdfdWlanSetPowerMode(ifName, mode);
1509     if (ret != HDF_SUCCESS) {
1510         HDF_LOGE("%s: fail to set power mode, %d", __func__, ret);
1511     }
1512 
1513     return ret;
1514 }
1515 
HdfWlanStartChannelMeas(const char * ifName,const MeasParam * measParam)1516 static uint32_t HdfWlanStartChannelMeas(const char *ifName,  const MeasParam *measParam)
1517 {
1518     struct NetDevice *netdev = NULL;
1519 
1520     netdev = NetDeviceGetInstByName(ifName);
1521     if (netdev == NULL) {
1522         HDF_LOGE("%s:netdev not found!ifName=%s.", __func__, ifName);
1523         return HDF_FAILURE;
1524     }
1525     struct HdfChipDriver *chipDriver = GetChipDriver(netdev);
1526     if (chipDriver == NULL) {
1527         HDF_LOGE("%s:bad net device found!", __func__);
1528         return HDF_FAILURE;
1529     }
1530     if (chipDriver->ops == NULL) {
1531         HDF_LOGE("%s: chipDriver->ops is null", __func__);
1532         return HDF_ERR_INVALID_OBJECT;
1533     }
1534 
1535     if (chipDriver->ops->StartChannelMeas == NULL) {
1536         HDF_LOGE("%s: chipDriver->ops->StartChannelMeas is null", __func__);
1537         return HDF_ERR_NOT_SUPPORT;
1538     }
1539     return chipDriver->ops->StartChannelMeas(netdev, measParam);
1540 }
1541 
WifiCmdStartChannelMeas(const RequestContext * context,struct HdfSBuf * reqData,struct HdfSBuf * rspData)1542 static int32_t WifiCmdStartChannelMeas(const RequestContext *context, struct HdfSBuf *reqData, struct HdfSBuf *rspData)
1543 {
1544     const MeasParam *measParam = NULL;
1545     int32_t ret;
1546     uint32_t replayDataSize;
1547     const char *ifName = NULL;
1548 
1549     (void)context;
1550     if (reqData == NULL || rspData == NULL) {
1551         return HDF_ERR_INVALID_PARAM;
1552     }
1553     ifName = HdfSbufReadString(reqData);
1554     if (ifName == NULL) {
1555         HDF_LOGE("%s: read ifName failed!", __func__);
1556         return HDF_FAILURE;
1557     }
1558     if (!HdfSbufReadBuffer(reqData, (const void **)&measParam, &replayDataSize)||
1559         replayDataSize != sizeof(MeasParam)) {
1560         HDF_LOGE("%s: read commandId failed!", __func__);
1561         return HDF_FAILURE;
1562     }
1563     ret = HdfWlanStartChannelMeas(ifName, measParam);
1564     if (ret != HDF_SUCCESS) {
1565         HDF_LOGE("%s: fail to get channel meas result, %d", __func__, ret);
1566         return ret;
1567     }
1568 
1569     return ret;
1570 }
1571 
HdfWlanSetProjectionScreenParam(const char * ifName,int cmd,const int8_t * buf,uint32_t bufLen)1572 static int32_t HdfWlanSetProjectionScreenParam(const char *ifName, int cmd, const int8_t *buf, uint32_t bufLen)
1573 {
1574     struct NetDevice *netdev = NULL;
1575     struct HdfChipDriver *chipDriver = NULL;
1576 
1577     netdev = NetDeviceGetInstByName(ifName);
1578     if (netdev == NULL) {
1579         HDF_LOGE("%s:netdev not found!ifName=%s.", __func__, ifName);
1580         return HDF_FAILURE;
1581     }
1582     chipDriver = GetChipDriver(netdev);
1583     if (chipDriver == NULL) {
1584         HDF_LOGE("%s:bad net device found!", __func__);
1585         return HDF_FAILURE;
1586     }
1587     if (chipDriver->ops == NULL) {
1588         HDF_LOGE("%s: chipDriver->ops is null", __func__);
1589         return HDF_ERR_INVALID_OBJECT;
1590     }
1591 
1592     if (chipDriver->ops->SetProjectionScreenParam == NULL) {
1593         HDF_LOGE("%s: chipDriver->ops->SetProjectionScreenParam is null", __func__);
1594         return HDF_ERR_NOT_SUPPORT;
1595     }
1596     return chipDriver->ops->SetProjectionScreenParam(netdev, cmd, buf, bufLen);
1597 }
1598 
WifiSetProjectionScreenParam(const RequestContext * context,struct HdfSBuf * reqData,struct HdfSBuf * rspData)1599 static int32_t WifiSetProjectionScreenParam(const RequestContext *context, struct HdfSBuf *reqData,
1600     struct HdfSBuf *rspData)
1601 {
1602     int32_t ret = HDF_FAILURE;
1603     const char *ifName = NULL;
1604     int32_t cmd;
1605     int8_t *buf = NULL;
1606     uint32_t bufLen;
1607 
1608     (void)context;
1609     if (reqData == NULL || rspData == NULL) {
1610         return HDF_ERR_INVALID_PARAM;
1611     }
1612     ifName = HdfSbufReadString(reqData);
1613     if (ifName == NULL) {
1614         HDF_LOGE("%s: read ifName failed!", __func__);
1615         return ret;
1616     }
1617     if (!HdfSbufReadInt32(reqData, &cmd)) {
1618         HDF_LOGE("%s: read cmd failed!", __func__);
1619         return ret;
1620     }
1621     if (!HdfSbufReadBuffer(reqData, (const void **)&buf, &bufLen)) {
1622         HDF_LOGE("%s: read buf failed!", __func__);
1623         return ret;
1624     }
1625     ret = HdfWlanSetProjectionScreenParam(ifName, cmd, buf, bufLen);
1626     if (ret != HDF_SUCCESS) {
1627         HDF_LOGE("%s: fail to config projection screen, %d", __func__, ret);
1628     }
1629     return ret;
1630 }
1631 
HdfWlanSendCmdIoctl(const char * ifName,int32_t cmd,const int8_t * buf,uint32_t bufLen)1632 static int32_t HdfWlanSendCmdIoctl(const char *ifName, int32_t cmd, const int8_t *buf, uint32_t bufLen)
1633 {
1634     struct NetDevice *netdev = NULL;
1635     struct HdfChipDriver *chipDriver = NULL;
1636 
1637     netdev = NetDeviceGetInstByName(ifName);
1638     if (netdev == NULL) {
1639         HDF_LOGE("%s:netdev not found!ifName=%s.", __func__, ifName);
1640         return HDF_FAILURE;
1641     }
1642     chipDriver = GetChipDriver(netdev);
1643     if (chipDriver == NULL) {
1644         HDF_LOGE("%s:bad net device found!", __func__);
1645         return HDF_FAILURE;
1646     }
1647     if (chipDriver->ops == NULL) {
1648         HDF_LOGE("%s: chipDriver->ops is null", __func__);
1649         return HDF_ERR_INVALID_OBJECT;
1650     }
1651 
1652     if (chipDriver->ops->SendCmdIoctl == NULL) {
1653         HDF_LOGE("%s: chipDriver->ops->SendCmdIoctl is null", __func__);
1654         return HDF_ERR_NOT_SUPPORT;
1655     }
1656     return chipDriver->ops->SendCmdIoctl(netdev, cmd, buf, bufLen);
1657 }
1658 
WifiSendCmdIoctl(const RequestContext * context,struct HdfSBuf * reqData,struct HdfSBuf * rspData)1659 static int32_t WifiSendCmdIoctl(const RequestContext *context, struct HdfSBuf *reqData, struct HdfSBuf *rspData)
1660 {
1661     int32_t ret = HDF_FAILURE;
1662     const char *ifName = NULL;
1663     int32_t cmd;
1664     int8_t *buf = NULL;
1665     uint32_t bufLen;
1666 
1667     (void)context;
1668     if (reqData == NULL || rspData == NULL) {
1669         return HDF_ERR_INVALID_PARAM;
1670     }
1671     ifName = HdfSbufReadString(reqData);
1672     if (ifName == NULL) {
1673         HDF_LOGE("%s: read ifName failed!", __func__);
1674         return ret;
1675     }
1676     if (!HdfSbufReadInt32(reqData, &cmd)) {
1677         HDF_LOGE("%s: read cmd failed!", __func__);
1678         return ret;
1679     }
1680     if (!HdfSbufReadBuffer(reqData, (const void **)&buf, &bufLen)) {
1681         HDF_LOGE("%s: read buf failed!", __func__);
1682         return ret;
1683     }
1684     ret = HdfWlanSendCmdIoctl(ifName, cmd, buf, bufLen);
1685     if (ret != HDF_SUCCESS) {
1686         HDF_LOGE("%s: fail to config projection screen, %d", __func__, ret);
1687     }
1688     return ret;
1689 }
1690 
HdfWlanGetStationInfo(const char * ifName,StationInfo * info,const uint8_t * mac,uint32_t macLen)1691 static int32_t HdfWlanGetStationInfo(const char *ifName, StationInfo *info, const uint8_t *mac, uint32_t macLen)
1692 {
1693     struct NetDevice *netdev = NULL;
1694     struct HdfChipDriver *chipDriver = NULL;
1695 
1696     netdev = NetDeviceGetInstByName(ifName);
1697     if (netdev == NULL) {
1698         HDF_LOGE("%s:netdev not found!ifName=%s.", __func__, ifName);
1699         return HDF_FAILURE;
1700     }
1701     chipDriver = GetChipDriver(netdev);
1702     if (chipDriver == NULL) {
1703         HDF_LOGE("%s:bad net device found!", __func__);
1704         return HDF_FAILURE;
1705     }
1706     if (chipDriver->ops == NULL) {
1707         HDF_LOGE("%s: chipDriver->ops is null", __func__);
1708         return HDF_ERR_INVALID_OBJECT;
1709     }
1710 
1711     if (chipDriver->ops->GetStationInfo == NULL) {
1712         HDF_LOGE("%s: chipDriver->ops->GetStationInfo is null", __func__);
1713         return HDF_ERR_NOT_SUPPORT;
1714     }
1715     return chipDriver->ops->GetStationInfo(netdev, info, mac, macLen);
1716 }
1717 
WifiGetStationInfo(const RequestContext * context,struct HdfSBuf * reqData,struct HdfSBuf * rspData)1718 static int32_t WifiGetStationInfo(const RequestContext *context, struct HdfSBuf *reqData,
1719     struct HdfSBuf *rspData)
1720 {
1721     int32_t ret = HDF_FAILURE;
1722     const char *ifName = NULL;
1723     StationInfo info = {0};
1724     const uint8_t *mac = NULL;
1725     uint32_t macLen;
1726 
1727     (void)context;
1728     if (reqData == NULL || rspData == NULL) {
1729         return HDF_ERR_INVALID_PARAM;
1730     }
1731     ifName = HdfSbufReadString(reqData);
1732     if (ifName == NULL) {
1733         HDF_LOGE("%s: read ifName failed!", __func__);
1734         return ret;
1735     }
1736     if (!HdfSbufReadBuffer(reqData, (const void **)&mac, &macLen)) {
1737         HDF_LOGE("%s: read buf failed!", __func__);
1738         return ret;
1739     }
1740     ret = HdfWlanGetStationInfo(ifName, &info, mac, macLen);
1741     if (ret != HDF_SUCCESS) {
1742         HDF_LOGE("%s: fail to get station information, %d", __func__, ret);
1743         return ret;
1744     }
1745 
1746     if (!HdfSbufWriteBuffer(rspData, &info, sizeof(StationInfo))) {
1747         HDF_LOGE("%s: %s!", __func__, ERROR_DESC_WRITE_RSP_FAILED);
1748         ret = HDF_FAILURE;
1749     }
1750     return ret;
1751 }
1752 
1753 static struct MessageDef g_wifiBaseFeatureCmds[] = {
1754     DUEMessage(CMD_BASE_NEW_KEY, WifiCmdNewKey, 0),
1755     DUEMessage(CMD_BASE_DEL_KEY, WifiCmdDelKey, 0),
1756     DUEMessage(CMD_BASE_SET_DEFAULT_KEY, WifiCmdSetKey, 0),
1757     DUEMessage(CMD_BASE_SEND_MLME, WifiSendMlme, 0),
1758     DUEMessage(CMD_BASE_SEND_EAPOL, WifiCmdSendEapol, 0),
1759     DUEMessage(CMD_BASE_RECEIVE_EAPOL, WifiCmdReceiveEapol, 0),
1760     DUEMessage(CMD_BASE_ENALBE_EAPOL, WifiCmdEnableEapol, 0),
1761     DUEMessage(CMD_BASE_DISABLE_EAPOL, WifiCmdDisableEapol, 0),
1762     DUEMessage(CMD_BASE_GET_ADDR, WifiCmdGetAddr, 0),
1763     DUEMessage(CMD_BASE_SET_MODE, WifiCmdSetMode, 0),
1764     DUEMessage(CMD_BASE_GET_HW_FEATURE, WifiCmdGetHwFeature, 0),
1765     DUEMessage(CMD_BASE_SET_NETDEV, WifiCmdSetNetdev, 0),
1766     DUEMessage(CMD_BASE_SEND_ACTION, WifiCmdSendAction, 0),
1767     DUEMessage(CMD_BASE_SET_CLIENT, WifiCmdSetClient, 0),
1768     DUEMessage(CMD_BASE_GET_NETWORK_INFO, WifiCmdGetNetworkInfo, 0),
1769     DUEMessage(CMD_BASE_IS_SUPPORT_COMBO, WifiCmdIsSupportCombo, 0),
1770     DUEMessage(CMD_BASE_GET_SUPPORT_COMBO, WifiCmdGetSupportCombo, 0),
1771     DUEMessage(CMD_BASE_GET_DEV_MAC_ADDR, WifiCmdGetDevMacAddr, 0),
1772     DUEMessage(CMD_BASE_SET_MAC_ADDR, WifiCmdSetMacAddr, 0),
1773     DUEMessage(CMD_BASE_GET_VALID_FREQ, WifiCmdGetValidFreqsWithBand, 0),
1774     DUEMessage(CMD_BASE_SET_TX_POWER, WifiCmdSetTxPower, 0),
1775     DUEMessage(CMD_BASE_GET_CHIPID, WifiCmdGetChipId, 0),
1776     DUEMessage(CMD_BASE_GET_IFNAMES, WifiCmdGetIfNamesByChipId, 0),
1777     DUEMessage(CMD_BASE_RESET_DRIVER, WifiCmdResetDriver, 0),
1778     DUEMessage(CMD_BASE_GET_NETDEV_INFO, WifiCmdGetNetDevInfo, 0),
1779     DUEMessage(CMD_BASE_DO_RESET_PRIVATE, WifiCmdDoResetChip, 0),
1780     DUEMessage(CMD_BASE_GET_POWER_MODE, WifiCmdGetPowerMode, 0),
1781     DUEMessage(CMD_BASE_SET_POWER_MODE, WifiCmdSetPowerMode, 0),
1782     DUEMessage(CMD_BASE_START_CHANNEL_MEAS, WifiCmdStartChannelMeas, 0),
1783     DUEMessage(CMD_BASE_SET_PROJECTION_SCREEN_PARAM, WifiSetProjectionScreenParam, 0),
1784     DUEMessage(CMD_BASE_SEND_CMD_IOCTL, WifiSendCmdIoctl, 0),
1785     DUEMessage(CMD_BASE_GET_STATION_INFO, WifiGetStationInfo, 0),
1786 };
1787 ServiceDefine(BaseService, BASE_SERVICE_ID, g_wifiBaseFeatureCmds);
1788 
BaseInit(void)1789 int32_t BaseInit(void)
1790 {
1791     if (g_baseService == NULL) {
1792         ServiceCfg cfg = {.dispatcherId = DEFAULT_DISPATCHER_ID};
1793         g_baseService = CreateService(BaseService, &cfg);
1794         if (g_baseService == NULL) {
1795             HDF_LOGE("%s: The g_baseService is null, CreateService failed!", __func__);
1796             return HDF_FAILURE;
1797         }
1798     }
1799     return HDF_SUCCESS;
1800 }
1801 
BaseDeinit(void)1802 int32_t BaseDeinit(void)
1803 {
1804     if (g_baseService != NULL && g_baseService->Destroy != NULL) {
1805         g_baseService->Destroy(g_baseService);
1806         g_baseService = NULL;
1807     }
1808     return HDF_SUCCESS;
1809 }
1810