• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2020-2021 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 "eapol.h"
10 #include "hdf_wlan_services.h"
11 #include "hdf_wlan_utils.h"
12 #include "hdf_wifi_event.h"
13 #include "hdf_wlan_chipdriver_manager.h"
14 #include "hdf_wlan_config.h"
15 #include "message/message_router.h"
16 #include "message/sidecar.h"
17 #include "securec.h"
18 #include "wifi_mac80211_ops.h"
19 #include "wifi_module.h"
20 #include "../hdf_wifi_core.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 
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(struct NetDevice *netDev, uint8_t keyIndex, bool pairwise, const uint8_t *macAddr,
76     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     return ret;
209 }
210 
WifiCmdDelKey(const RequestContext * context,struct HdfSBuf * reqData,struct HdfSBuf * rspData)211 static int32_t WifiCmdDelKey(const RequestContext *context, struct HdfSBuf *reqData, struct HdfSBuf *rspData)
212 {
213     int8_t pairwise;
214     struct NetDevice *netDev = NULL;
215     WifiKeyExt *keyExt = NULL;
216     const char *ifName = NULL;
217     int32_t ret = 0;
218 
219     (void)context;
220     (void)rspData;
221     if (reqData == NULL) {
222         HDF_LOGE("%s: reqData is NULL", __func__);
223         return HDF_ERR_INVALID_PARAM;
224     }
225     ifName = HdfSbufReadString(reqData);
226     if (ifName == NULL) {
227         HDF_LOGE("%s: %s!ParamName=%s", __func__, ERROR_DESC_READ_REQ_FAILED, "ifName");
228         return HDF_FAILURE;
229     }
230     keyExt = (WifiKeyExt *)OsalMemCalloc(sizeof(WifiKeyExt));
231     if (keyExt == NULL) {
232         HDF_LOGE("%s:keyExt OsalMemCalloc failed", __func__);
233         return HDF_FAILURE;
234     }
235     if (WifiCmdFillKeyInner(reqData, keyExt) != HDF_SUCCESS) {
236         HDF_LOGE("%s: %s!ParamName=%s", __func__, ERROR_DESC_READ_REQ_FAILED, "keyExt");
237         OsalMemFree(keyExt);
238         return HDF_FAILURE;
239     }
240 
241     netDev = NetDeviceGetInstByName(ifName);
242     if (netDev == NULL) {
243         HDF_LOGE("%s:netDev not found!ifName=%s", __func__, ifName);
244         OsalMemFree(keyExt);
245         return HDF_FAILURE;
246     }
247 
248     pairwise = (keyExt->type == WIFI_KEYTYPE_PAIRWISE);
249     ret = DelKey(netDev, keyExt->keyIdx, pairwise, keyExt->addr);
250     OsalMemFree(keyExt);
251     return ret;
252 }
253 
WifiGetMulticast(WifiKeyExt * keyExt)254 static uint8_t WifiGetMulticast(WifiKeyExt *keyExt)
255 {
256     uint8_t multicast = FALSE;
257 
258     if (keyExt->def == TRUE) {
259         multicast = TRUE;
260     }
261     if (keyExt->defMgmt == TRUE) {
262         multicast = TRUE;
263     }
264     if (keyExt->defaultTypes == WIFI_KEY_DEFAULT_TYPE_MULTICAST) {
265         multicast = TRUE;
266     }
267     return multicast;
268 }
269 
WifiCmdSetKey(const RequestContext * context,struct HdfSBuf * reqData,struct HdfSBuf * rspData)270 static int32_t WifiCmdSetKey(const RequestContext *context, struct HdfSBuf *reqData, struct HdfSBuf *rspData)
271 {
272     uint8_t index;
273     uint8_t unicast;
274     uint8_t multicast;
275     struct NetDevice *netDev = NULL;
276     WifiKeyExt *keyExt = NULL;
277     const char *ifName = NULL;
278     int32_t ret = 0;
279 
280     (void)context;
281     (void)rspData;
282     if (reqData == NULL) {
283         HDF_LOGE("%s: reqData is NULL", __func__);
284         return HDF_ERR_INVALID_PARAM;
285     }
286     ifName = HdfSbufReadString(reqData);
287     if (ifName == NULL) {
288         HDF_LOGE("%s: %s!ParamName=%s", __func__, ERROR_DESC_READ_REQ_FAILED, "ifName");
289         return HDF_FAILURE;
290     }
291     keyExt = (WifiKeyExt *)OsalMemCalloc(sizeof(WifiKeyExt));
292     if (keyExt == NULL) {
293         HDF_LOGE("%s:keyExt OsalMemCalloc failed", __func__);
294         return HDF_FAILURE;
295     }
296     if (WifiCmdFillKeyInner(reqData, keyExt) != HDF_SUCCESS) {
297         HDF_LOGE("%s: %s!ParamName=%s", __func__, ERROR_DESC_READ_REQ_FAILED, "keyExt");
298         OsalMemFree(keyExt);
299         return HDF_FAILURE;
300     }
301 
302     netDev = NetDeviceGetInstByName(ifName);
303     if (netDev == NULL) {
304         HDF_LOGE("%s:netDev not found!ifName=%s", __func__, ifName);
305         OsalMemFree(keyExt);
306         return HDF_FAILURE;
307     }
308 
309     index = (uint8_t)keyExt->keyIdx;
310     unicast = TRUE;
311     multicast = WifiGetMulticast(keyExt);
312     ret = SetDefaultKey(netDev, index, unicast, multicast);
313     OsalMemFree(keyExt);
314     return ret;
315 }
316 
WifiCmdSendEapol(const RequestContext * context,struct HdfSBuf * reqData,struct HdfSBuf * rspData)317 static int32_t WifiCmdSendEapol(const RequestContext *context, struct HdfSBuf *reqData, struct HdfSBuf *rspData)
318 {
319     WifiTxEapol eapol = { 0 };
320     struct NetDevice *netdev = NULL;
321     const char *ifName = NULL;
322 
323     (void)context;
324     (void)rspData;
325     if (reqData == NULL) {
326         HDF_LOGE("%s: reqData is NULL", __func__);
327         return HDF_ERR_INVALID_PARAM;
328     }
329     ifName = HdfSbufReadString(reqData);
330     if (ifName == NULL) {
331         HDF_LOGE("%s: %s!ParamName=%s", __func__, ERROR_DESC_READ_REQ_FAILED, "ifName");
332         return HDF_FAILURE;
333     }
334 
335     if (!HdfSbufReadBuffer(reqData, (const void **)&(eapol.buf), &(eapol.len))) {
336         HDF_LOGE("%s: %s!ParamName=%s,readSize=%u", __func__, ERROR_DESC_READ_REQ_FAILED, "buf", eapol.len);
337         return HDF_FAILURE;
338     }
339 
340     netdev = NetDeviceGetInstByName(ifName);
341     if (netdev == NULL) {
342         HDF_LOGE("%s:netdev not found!ifName=%s", __func__, ifName);
343         return HDF_FAILURE;
344     }
345 
346     return SendEapol(netdev, &eapol);
347 }
348 
WifiCmdReceiveEapol(const RequestContext * context,struct HdfSBuf * reqData,struct HdfSBuf * rspData)349 static int32_t WifiCmdReceiveEapol(const RequestContext *context, struct HdfSBuf *reqData, struct HdfSBuf *rspData)
350 {
351     struct NetDevice *netdev = NULL;
352     WifiRxEapol eapol = { 0 };
353     const char *ifName = NULL;
354     int32_t ret;
355 
356     (void)context;
357     if (reqData == NULL || rspData == NULL) {
358         HDF_LOGE("%s: reqData or rspData is NULL", __func__);
359         return HDF_ERR_INVALID_PARAM;
360     }
361     ifName = HdfSbufReadString(reqData);
362     if (ifName == NULL) {
363         HDF_LOGE("%s: %s!ParamName=%s", __func__, ERROR_DESC_READ_REQ_FAILED, "ifName");
364         return HDF_FAILURE;
365     }
366 
367     netdev = NetDeviceGetInstByName(ifName);
368     if (netdev == NULL) {
369         HDF_LOGE("%s:netdev not found!ifName=%s", __func__, ifName);
370         return HDF_FAILURE;
371     }
372 
373     eapol.len = DEFAULT_EAPOL_PACKAGE_SIZE;
374     eapol.buf = OsalMemCalloc(DEFAULT_EAPOL_PACKAGE_SIZE);
375     if (eapol.buf == NULL) {
376         HDF_LOGE("%s: oom", __func__);
377         return HDF_FAILURE;
378     }
379 
380     ret = ReceiveEapol(netdev, &eapol);
381     HDF_LOGI("%s:receiveEapol ret=%d", __func__, ret);
382     if (!ret) {
383         if (!HdfSbufWriteBuffer(rspData, eapol.buf, eapol.len)) {
384             HDF_LOGE("%s: %s!", __func__, ERROR_DESC_WRITE_RSP_FAILED);
385             ret = HDF_ERR_IO;
386         }
387     }
388     OsalMemFree(eapol.buf);
389     return ret;
390 }
391 
WifiCmdEnableEapol(const RequestContext * context,struct HdfSBuf * reqData,struct HdfSBuf * rspData)392 static int32_t WifiCmdEnableEapol(const RequestContext *context, struct HdfSBuf *reqData, struct HdfSBuf *rspData)
393 {
394     struct NetDevice *netdev = NULL;
395     const char *ifName = NULL;
396     WifiEnableEapol eapol;
397 
398     (void)context;
399     (void)rspData;
400     if (reqData == NULL) {
401         HDF_LOGE("%s: reqData is NULL", __func__);
402         return HDF_ERR_INVALID_PARAM;
403     }
404     ifName = HdfSbufReadString(reqData);
405     if (ifName == NULL) {
406         HDF_LOGE("%s: %s!ParamName=%s", __func__, ERROR_DESC_READ_REQ_FAILED, "ifName");
407         return HDF_FAILURE;
408     }
409     netdev = NetDeviceGetInstByName(ifName);
410     if (netdev == NULL) {
411         HDF_LOGE("%s:netdev not found!ifName=%s", __func__, ifName);
412         return HDF_FAILURE;
413     }
414     eapol.callback = (void *)HdfWifiEventEapolRecv;
415     eapol.context = NULL;
416 
417     return EnableEapol(netdev, &eapol);
418 }
419 
WifiCmdDisableEapol(const RequestContext * context,struct HdfSBuf * reqData,struct HdfSBuf * rspData)420 static int32_t WifiCmdDisableEapol(const RequestContext *context, struct HdfSBuf *reqData, struct HdfSBuf *rspData)
421 {
422     struct NetDevice *netdev = NULL;
423     const char *ifName = NULL;
424     (void)context;
425     (void)rspData;
426     if (reqData == NULL) {
427         HDF_LOGE("%s: reqData is NULL", __func__);
428         return HDF_ERR_INVALID_PARAM;
429     }
430     ifName = HdfSbufReadString(reqData);
431     if (ifName == NULL) {
432         HDF_LOGE("%s: %s!ParamName=%s", __func__, ERROR_DESC_READ_REQ_FAILED, "ifName");
433         return HDF_FAILURE;
434     }
435     netdev = NetDeviceGetInstByName(ifName);
436     if (netdev == NULL) {
437         HDF_LOGE("%s:netdev not found!ifName=%s", __func__, ifName);
438         return HDF_FAILURE;
439     }
440 
441     return DisableEapol(netdev);
442 }
443 
WifiCmdGetAddr(const RequestContext * context,struct HdfSBuf * reqData,struct HdfSBuf * rspData)444 static int32_t WifiCmdGetAddr(const RequestContext *context, struct HdfSBuf *reqData, struct HdfSBuf *rspData)
445 {
446     struct NetDevice *netdev = NULL;
447     const char *ifName = NULL;
448     int32_t ret = HDF_SUCCESS;
449     (void)context;
450     if (reqData == NULL || rspData == NULL) {
451         HDF_LOGE("%s: reqData or rspData is NULL", __func__);
452         return HDF_ERR_INVALID_PARAM;
453     }
454     ifName = HdfSbufReadString(reqData);
455     if (ifName == NULL) {
456         HDF_LOGE("%s: %s!ParamName=%s", __func__, ERROR_DESC_READ_REQ_FAILED, "ifName");
457         return HDF_FAILURE;
458     }
459     netdev = NetDeviceGetInstByName(ifName);
460     if (netdev == NULL) {
461         HDF_LOGE("%s: invalid netdev", __func__);
462         return HDF_FAILURE;
463     }
464     if (!HdfSbufWriteBuffer(rspData, netdev->macAddr, ETH_ADDR_LEN)) {
465         HDF_LOGE("%s: %s!", __func__, ERROR_DESC_WRITE_RSP_FAILED);
466         ret = HDF_ERR_IO;
467     }
468     return ret;
469 }
470 
WifiCmdSetMode(const RequestContext * context,struct HdfSBuf * reqData,struct HdfSBuf * rspData)471 static int32_t WifiCmdSetMode(const RequestContext *context, struct HdfSBuf *reqData, struct HdfSBuf *rspData)
472 {
473     struct NetDevice *netdev = NULL;
474     WifiSetMode *mode = NULL;
475     const char *ifName = NULL;
476     uint32_t dataSize = 0;
477     int32_t ret;
478 
479     (void)context;
480     (void)rspData;
481     if (reqData == NULL) {
482         HDF_LOGE("%s: reqData is NULL", __func__);
483         return HDF_ERR_INVALID_PARAM;
484     }
485     ifName = HdfSbufReadString(reqData);
486     if (ifName == NULL) {
487         HDF_LOGE("%s: %s!ParamName=%s", __func__, ERROR_DESC_READ_REQ_FAILED, "ifName");
488         return HDF_FAILURE;
489     }
490     if (!HdfSbufReadBuffer(reqData, (const void **)&mode, &dataSize) || mode == NULL ||
491         dataSize != sizeof(WifiSetMode)) {
492         HDF_LOGE("%s: %s!ParamName=%s,readSize=%u", __func__, ERROR_DESC_READ_REQ_FAILED, "mode", dataSize);
493         return HDF_FAILURE;
494     }
495     netdev = NetDeviceGetInstByName(ifName);
496     if (netdev == NULL) {
497         HDF_LOGE("%s: invalid netdev", __func__);
498         return HDF_FAILURE;
499     }
500     HDF_LOGW("%s:%s changing mode to %u ...", __func__, ifName, mode->iftype);
501     ret = SetMode(netdev, mode->iftype);
502     if (ret != HDF_SUCCESS) {
503         HDF_LOGE("%s: fail to do change intf,%d", __func__, ret);
504     }
505     return ret;
506 }
507 
WifiGetChannelData(struct WlanBand * band,WifiHwFeatureData ** featureData,struct WlanHwCapability * capability,uint32_t iee80211band)508 static void WifiGetChannelData(struct WlanBand *band, WifiHwFeatureData **featureData,
509     struct WlanHwCapability *capability, uint32_t iee80211band)
510 {
511     uint32_t loop;
512     if (band == NULL || featureData == NULL || *featureData == NULL) {
513         HDF_LOGE("%s: band or featureData is NULL", __func__);
514         return;
515     }
516 
517     (*featureData)->bands[iee80211band].channelNum = band->channelCount;
518     (*featureData)->htCapab = capability->htCapability;
519 
520     for (loop = 0; loop < band->channelCount; ++loop) {
521         (*featureData)->bands[iee80211band].iee80211Channel[loop].freq = band->channels[loop].centerFreq;
522         (*featureData)->bands[iee80211band].iee80211Channel[loop].flags = band->channels[loop].flags;
523         (*featureData)->bands[iee80211band].iee80211Channel[loop].channel = band->channels[loop].channelId;
524     }
525 }
526 
WifiFillHwFeature(struct NetDevice * netdev,WifiHwFeatureData * featureData)527 static int32_t WifiFillHwFeature(struct NetDevice *netdev, WifiHwFeatureData *featureData)
528 {
529     int32_t ret = HDF_SUCCESS;
530     struct WlanHwCapability *capability = GetHwCapability(netdev);
531     if (capability == NULL) {
532         HDF_LOGE("%s:GetHwCapability failed!", __func__);
533         return HDF_FAILURE;
534     }
535     do {
536         uint32_t loop;
537         if (capability->supportedRateCount > MAX_SUPPORTED_RATE) {
538             HDF_LOGE("%s: bitrates %u out of range", __func__, capability->supportedRateCount);
539             ret = HDF_FAILURE;
540             break;
541         }
542         for (loop = 0; loop < capability->supportedRateCount; ++loop) {
543             HDF_LOGV("%s: supported rate %u", __func__, capability->supportedRates[loop]);
544             featureData->bitrate[loop] = capability->supportedRates[loop];
545         }
546 
547         if (capability->bands[IEEE80211_BAND_2GHZ] != NULL) {
548             struct WlanBand *band = capability->bands[IEEE80211_BAND_2GHZ];
549             if (band->channelCount > WIFI_24G_CHANNEL_NUM) {
550                 HDF_LOGE("%s: channels %u out of range", __func__, band->channelCount);
551                 ret = HDF_FAILURE;
552                 break;
553             }
554             WifiGetChannelData(band, &featureData, capability, IEEE80211_BAND_2GHZ);
555         }
556         if (capability->bands[IEEE80211_BAND_5GHZ] != NULL) {
557             struct WlanBand *band = capability->bands[IEEE80211_BAND_5GHZ];
558             if (band->channelCount > WIFI_MAX_CHANNEL_NUM) {
559                 HDF_LOGE("%s: channels %u out of range", __func__, band->channelCount);
560                 ret = HDF_FAILURE;
561                 break;
562             }
563             WifiGetChannelData(band, &featureData, capability, IEEE80211_BAND_5GHZ);
564         }
565     } while (false);
566 
567     if (capability->Release != NULL) {
568         capability->Release(capability);
569         capability = NULL;
570     }
571     return ret;
572 }
573 
GetDeviceMacAddr(struct NetDevice * netdev,int32_t type,uint8_t * mac,uint8_t len)574 static uint32_t GetDeviceMacAddr(struct NetDevice *netdev, int32_t type, uint8_t *mac, uint8_t len)
575 {
576     struct HdfChipDriver *chipDriver = GetChipDriver(netdev);
577     if (chipDriver == NULL) {
578         HDF_LOGE("%s:bad net device found!", __func__);
579         return HDF_FAILURE;
580     }
581 
582     RETURN_IF_CHIPOPS_NOT_IMPLEMENT(chipDriver->ops, GetDeviceMacAddr);
583     return chipDriver->ops->GetDeviceMacAddr(netdev, type, mac, len);
584 }
585 
SetMacAddr(struct NetDevice * netdev,uint8_t * mac,uint8_t len)586 static uint32_t SetMacAddr(struct NetDevice *netdev, uint8_t *mac, uint8_t len)
587 {
588     struct HdfChipDriver *chipDriver = GetChipDriver(netdev);
589     if (chipDriver == NULL) {
590         HDF_LOGE("%s:bad net device found!", __func__);
591         return HDF_FAILURE;
592     }
593     RETURN_IF_CHIPOPS_NOT_IMPLEMENT(chipDriver->ops, SetMacAddr);
594     return chipDriver->ops->SetMacAddr(netdev, mac, len);
595 }
596 
SetTxPower(struct NetDevice * netdev,int32_t power)597 static uint32_t SetTxPower(struct NetDevice *netdev, int32_t power)
598 {
599     struct HdfChipDriver *chipDriver = GetChipDriver(netdev);
600     if (chipDriver == NULL) {
601         HDF_LOGE("%s:bad net device found!", __func__);
602         return HDF_FAILURE;
603     }
604 
605     RETURN_IF_CHIPOPS_NOT_IMPLEMENT(chipDriver->ops, SetTxPower);
606     return chipDriver->ops->SetTxPower(netdev, power);
607 }
608 
GetValidFreqsWithBand(struct NetDevice * netdev,int32_t band,int32_t * freqs,uint32_t * num)609 static uint32_t GetValidFreqsWithBand(struct NetDevice *netdev, int32_t band, int32_t *freqs, uint32_t *num)
610 {
611     struct HdfChipDriver *chipDriver = GetChipDriver(netdev);
612     if (chipDriver == NULL) {
613         HDF_LOGE("%s:bad net device found!", __func__);
614         return HDF_FAILURE;
615     }
616 
617     RETURN_IF_CHIPOPS_NOT_IMPLEMENT(chipDriver->ops, GetValidFreqsWithBand);
618     return chipDriver->ops->GetValidFreqsWithBand(netdev, band, freqs, num);
619 }
620 
WifiCmdGetHwFeature(const RequestContext * context,struct HdfSBuf * reqData,struct HdfSBuf * rspData)621 static int32_t WifiCmdGetHwFeature(const RequestContext *context, struct HdfSBuf *reqData, struct HdfSBuf *rspData)
622 {
623     struct NetDevice *netdev = NULL;
624     WifiHwFeatureData featureData = { 0 };
625     const char *ifName = NULL;
626     int32_t ret;
627 
628     (void)context;
629     if (reqData == NULL || rspData == NULL) {
630         HDF_LOGE("%s: reqData or rspData is NULL", __func__);
631         return HDF_ERR_INVALID_PARAM;
632     }
633     ifName = HdfSbufReadString(reqData);
634     if (ifName == NULL) {
635         HDF_LOGE("%s: %s!ParamName=%s", __func__, ERROR_DESC_READ_REQ_FAILED, "ifName");
636         return HDF_FAILURE;
637     }
638     netdev = NetDeviceGetInstByName(ifName);
639     if (netdev == NULL) {
640         HDF_LOGE("%s: invalid netdev", __func__);
641         return HDF_FAILURE;
642     }
643     ret = WifiFillHwFeature(netdev, &featureData);
644     if (ret != HDF_SUCCESS) {
645         HDF_LOGE("%s: WifiFillHwFeature failed!ret=%d", __func__, ret);
646         return HDF_FAILURE;
647     }
648     if (!HdfSbufWriteBuffer(rspData, &featureData, sizeof(featureData))) {
649         HDF_LOGE("%s: %s!", __func__, ERROR_DESC_WRITE_RSP_FAILED);
650         ret = HDF_ERR_IO;
651     }
652     return ret;
653 }
654 
SetNetIfInfo(struct NetDevice * netdev,uint32_t type)655 static int32_t SetNetIfInfo(struct NetDevice *netdev, uint32_t type)
656 {
657     int ret = 0;
658     if (netdev != NULL) {
659         ret = netdev->netDeviceIf->open(netdev);
660         (void)NetIfSetStatus(netdev, NETIF_UP);
661     }
662     if (type == WIFI_IFTYPE_AP) {
663         if (NetIfDhcpsStart(netdev, NULL, 0) == 0) {
664             HDF_LOGI("dhcp servier start ok.");
665         }
666     }
667     return ret;
668 }
669 
UnsetNetIfInfo(struct NetDevice * netdev)670 static int32_t UnsetNetIfInfo(struct NetDevice *netdev)
671 {
672     (void)netdev->netDeviceIf->stop(netdev);
673     (void)NetIfDhcpStop(netdev);
674     (void)NetIfDhcpsStop(netdev);
675     return NetIfSetStatus(netdev, NETIF_DOWN);
676 }
677 
SetNetworkAddr(struct NetDevice * netdev,uint32_t type)678 static void SetNetworkAddr(struct NetDevice *netdev, uint32_t type)
679 {
680     IpV4Addr ip;
681     IpV4Addr netmask;
682     IpV4Addr gw;
683     if (type == WIFI_IFTYPE_STATION) {
684         ip.addr = 0x00000000UL;
685         netmask.addr = 0x00000000UL;
686         gw.addr = 0x00000000UL;
687     } else {
688         ip.addr = 0x010ca8c0UL;
689         netmask.addr = 0x00ffffffUL;
690         gw.addr = 0x010ca8c0UL;
691     }
692 
693     if (netdev != NULL) {
694         NetIfSetAddr(netdev, &ip, &netmask, &gw);
695     }
696 }
697 
UnsetNetworkAddr(struct NetDevice * netdev)698 static void UnsetNetworkAddr(struct NetDevice *netdev)
699 {
700     IpV4Addr ip = { 0x00000000UL };
701     IpV4Addr netmask = { 0x00000000UL };
702     IpV4Addr gw = { 0x00000000UL };
703     if (netdev != NULL) {
704         NetIfSetAddr(netdev, &ip, &netmask, &gw);
705     }
706 }
707 
WifiCmdSetNetdev(const RequestContext * context,struct HdfSBuf * reqData,struct HdfSBuf * rspData)708 static int32_t WifiCmdSetNetdev(const RequestContext *context, struct HdfSBuf *reqData, struct HdfSBuf *rspData)
709 {
710     WifiSetNewDev *info = NULL;
711     const char *ifName = NULL;
712     uint32_t dataSize = 0;
713     int ret = 0;
714     struct NetDevice *netdev = NULL;
715 
716     (void)context;
717     (void)rspData;
718     if (reqData == NULL) {
719         HDF_LOGE("%s: reqData is NULL", __func__);
720         return HDF_ERR_INVALID_PARAM;
721     }
722     ifName = HdfSbufReadString(reqData);
723     if (ifName == NULL) {
724         HDF_LOGE("%s: %s!ParamName=%s", __func__, ERROR_DESC_READ_REQ_FAILED, "ifName");
725         return HDF_FAILURE;
726     }
727 
728     if (!HdfSbufReadBuffer(reqData, (const void **)&info, &dataSize) || info == NULL ||
729         dataSize != sizeof(WifiSetNewDev)) {
730         HDF_LOGE("%s: %s!ParamName=%s,readSize=%u", __func__, ERROR_DESC_READ_REQ_FAILED, "devinfo", dataSize);
731         return HDF_FAILURE;
732     }
733 
734     netdev = NetDeviceGetInstByName(ifName);
735     if (netdev == NULL || netdev->netDeviceIf == NULL) {
736         HDF_LOGE("%s:netdev not found!ifName=%s", __func__, ifName);
737         return HDF_FAILURE;
738     }
739 
740     if (info->status == FALSE && NetDeviceIsInstRunning(netdev) && netdev->netDeviceIf->stop != NULL) {
741         UnsetNetworkAddr(netdev);
742         ret = UnsetNetIfInfo(netdev);
743     } else if (info->status == TRUE && !NetDeviceIsInstRunning(netdev) && netdev->netDeviceIf->open != NULL) {
744         SetNetworkAddr(netdev, info->ifType);
745         ret = SetNetIfInfo(netdev, info->ifType);
746     }
747     return ret;
748 }
749 
WifiSendMlme(const RequestContext * context,struct HdfSBuf * reqData,struct HdfSBuf * rspData)750 static int32_t WifiSendMlme(const RequestContext *context, struct HdfSBuf *reqData, struct HdfSBuf *rspData)
751 {
752     (void)context;
753     (void)reqData;
754     (void)rspData;
755     return HDF_SUCCESS;
756 }
757 
SendAction(struct NetDevice * netdev,WifiActionData * actionData)758 int32_t SendAction(struct NetDevice *netdev, WifiActionData *actionData)
759 {
760     struct HdfChipDriver *chipDriver = GetChipDriver(netdev);
761     if (chipDriver == NULL) {
762         HDF_LOGE("%s:bad net device found!", __func__);
763         return HDF_FAILURE;
764     }
765 
766     RETURN_IF_CHIPOPS_NOT_IMPLEMENT(chipDriver->ops, SendAction);
767     return chipDriver->ops->SendAction(netdev, actionData);
768 }
769 
WifiCmdSendAction(const RequestContext * context,struct HdfSBuf * reqData,struct HdfSBuf * rspData)770 static int32_t WifiCmdSendAction(const RequestContext *context, struct HdfSBuf *reqData, struct HdfSBuf *rspData)
771 {
772     WifiActionData actionData = {0};
773     const char *ifName = NULL;
774     uint32_t dataSize = 0;
775     struct NetDevice *netdev = NULL;
776     int ret;
777     (void)context;
778     (void)rspData;
779     if (reqData == NULL) {
780         HDF_LOGE("%s:reqData is NULL", __func__);
781         return HDF_ERR_INVALID_PARAM;
782     }
783     ifName = HdfSbufReadString(reqData);
784     if (ifName == NULL) {
785         HDF_LOGE("%s: %s!ParamName=%s", __func__, ERROR_DESC_READ_REQ_FAILED, "ifName");
786         return HDF_FAILURE;
787     }
788 
789     if (!HdfSbufReadBuffer(reqData, (const void **)&(actionData.bssid), &dataSize) ||
790         dataSize != ETH_ADDR_LEN) {
791         HDF_LOGE("%s: %s!ParamName=%s,readSize=%u", __func__, ERROR_DESC_READ_REQ_FAILED, "bssid", dataSize);
792         return HDF_FAILURE;
793     }
794 
795     if (!HdfSbufReadBuffer(reqData, (const void **)&(actionData.dst), &dataSize) ||
796         dataSize != ETH_ADDR_LEN) {
797         HDF_LOGE("%s: %s!ParamName=%s,readSize=%u", __func__, ERROR_DESC_READ_REQ_FAILED, "dst", dataSize);
798         return HDF_FAILURE;
799     }
800 
801     if (!HdfSbufReadBuffer(reqData, (const void **)&(actionData.src), &dataSize) ||
802         dataSize != ETH_ADDR_LEN) {
803         HDF_LOGE("%s: %s!ParamName=%s,readSize=%u", __func__, ERROR_DESC_READ_REQ_FAILED, "src", dataSize);
804         return HDF_FAILURE;
805     }
806 
807     if (!HdfSbufReadBuffer(reqData, (const void **)&(actionData.data), &(actionData.dataLen))) {
808         HDF_LOGE("%s: %s!ParamName=%s,readSize=%u", __func__, ERROR_DESC_READ_REQ_FAILED, "data", actionData.dataLen);
809         return HDF_FAILURE;
810     }
811     netdev = NetDeviceGetInstByName(ifName);
812     if (netdev == NULL || netdev->netDeviceIf == NULL) {
813         HDF_LOGE("%s:netdev not found!ifName=%s", __func__, ifName);
814         return HDF_FAILURE;
815     }
816 
817     ret = SendAction(netdev, &actionData);
818     if (ret != HDF_SUCCESS) {
819         HDF_LOGE("%s: fail to remain on channel,%d", __func__, ret);
820     }
821     return ret;
822 }
823 
WifiCmdGetNetworkInfo(const RequestContext * context,struct HdfSBuf * reqData,struct HdfSBuf * rspData)824 static int32_t WifiCmdGetNetworkInfo(const RequestContext *context, struct HdfSBuf *reqData, struct HdfSBuf *rspData)
825 {
826     uint32_t netDevNum;
827     char *defaultIfName = "wlan0";
828     uint8_t supportMode[PROTOCOL_80211_IFTYPE_NUM] = {0};
829     netDevNum = 1;
830     (void)context;
831     (void)reqData;
832 
833     if (!HdfSbufWriteUint32(rspData, netDevNum)) {
834         HDF_LOGE("%s: %s!", __func__, ERROR_DESC_WRITE_RSP_FAILED);
835         return HDF_FAILURE;
836     }
837     if (!HdfSbufWriteString(rspData, defaultIfName)) {
838         HDF_LOGE("%s: %s!", __func__, ERROR_DESC_WRITE_RSP_FAILED);
839         return HDF_FAILURE;
840     }
841     supportMode[PROTOCOL_80211_IFTYPE_STATION] = 1;
842     supportMode[PROTOCOL_80211_IFTYPE_AP] = 1;
843     if (!HdfSbufWriteBuffer(rspData, supportMode, PROTOCOL_80211_IFTYPE_NUM)) {
844         HDF_LOGE("%s: %s!", __func__, ERROR_DESC_WRITE_RSP_FAILED);
845         return HDF_FAILURE;
846     }
847     return HDF_SUCCESS;
848 }
849 
WifiCmdIsSupportCombo(const RequestContext * context,struct HdfSBuf * reqData,struct HdfSBuf * rspData)850 static int32_t WifiCmdIsSupportCombo(const RequestContext *context, struct HdfSBuf *reqData, struct HdfSBuf *rspData)
851 {
852     uint8_t isComboValid;
853     (void)context;
854     if (reqData == NULL || rspData == NULL) {
855         return HDF_ERR_INVALID_PARAM;
856     }
857     isComboValid = false;
858     if (!HdfSbufWriteUint8(rspData, isComboValid)) {
859         HDF_LOGE("%s: %s!", __func__, ERROR_DESC_WRITE_RSP_FAILED);
860         return HDF_FAILURE;
861     }
862     return HDF_SUCCESS;
863 }
864 
WifiCmdGetSupportCombo(const RequestContext * context,struct HdfSBuf * reqData,struct HdfSBuf * rspData)865 static int32_t WifiCmdGetSupportCombo(const RequestContext *context, struct HdfSBuf *reqData, struct HdfSBuf *rspData)
866 {
867     uint8_t isComboValid;
868     (void)context;
869     if (reqData == NULL || rspData == NULL) {
870         return HDF_ERR_INVALID_PARAM;
871     }
872     isComboValid = false;
873     if (!HdfSbufWriteUint8(rspData, isComboValid)) {
874         HDF_LOGE("%s: write fail", __func__);
875         return HDF_FAILURE;
876     }
877     return HDF_SUCCESS;
878 }
879 
WifiCmdGetDevMacAddr(const RequestContext * context,struct HdfSBuf * reqData,struct HdfSBuf * rspData)880 static int32_t WifiCmdGetDevMacAddr(const RequestContext *context, struct HdfSBuf *reqData, struct HdfSBuf *rspData)
881 {
882     int32_t ret;
883     struct NetDevice *netdev = NULL;
884     const char *ifName = NULL;
885     uint8_t isEfuseSavedMac;
886     int32_t type;
887     unsigned char devMac[IEEE80211_MAC_ADDR_LEN] = {0};
888 
889     (void)context;
890     if (reqData == NULL || rspData == NULL) {
891         return HDF_ERR_INVALID_PARAM;
892     }
893     ifName = HdfSbufReadString(reqData);
894     if (ifName == NULL) {
895         HDF_LOGE("%s: %s!ParamName=%s", __func__, ERROR_DESC_READ_REQ_FAILED, "ifName");
896         return HDF_FAILURE;
897     }
898     netdev = NetDeviceGetInstByName(ifName);
899     if (netdev == NULL) {
900         HDF_LOGE("%s:netdev not found!ifName=%s", __func__, ifName);
901         return HDF_FAILURE;
902     }
903     if (!HdfSbufReadInt32(reqData, &type)) {
904         HDF_LOGE("%s: %s!ParamName=%s", __func__, ERROR_DESC_READ_REQ_FAILED, "type");
905         return HDF_FAILURE;
906     }
907     ret = GetDeviceMacAddr(netdev, type, devMac, IEEE80211_MAC_ADDR_LEN);
908     if (ret && ret != HDF_ERR_NOT_SUPPORT) {
909         HDF_LOGE("%s: fail to get device mac addr,%d", __func__, ret);
910         return ret;
911     }
912     isEfuseSavedMac = (ret == HDF_ERR_NOT_SUPPORT) ? false : true;
913     if (!HdfSbufWriteUint8(rspData, isEfuseSavedMac)) {
914         HDF_LOGE("%s: %s!", __func__, ERROR_DESC_WRITE_RSP_FAILED);
915         return HDF_FAILURE;
916     }
917     if (!HdfSbufWriteBuffer(rspData, devMac, IEEE80211_MAC_ADDR_LEN)) {
918         HDF_LOGE("%s: %s!", __func__, ERROR_DESC_WRITE_RSP_FAILED);
919         ret = HDF_FAILURE;
920     }
921     return HDF_SUCCESS;
922 }
923 
WifiCmdSetMacAddr(const RequestContext * context,struct HdfSBuf * reqData,struct HdfSBuf * rspData)924 static int32_t WifiCmdSetMacAddr(const RequestContext *context, struct HdfSBuf *reqData, struct HdfSBuf *rspData)
925 {
926     int32_t ret;
927     struct NetDevice *netdev = NULL;
928     const char *ifName = NULL;
929     unsigned char *mac = NULL;
930     uint32_t replayDataSize;
931 
932     (void)context;
933     if (reqData == NULL || rspData == NULL) {
934         return HDF_ERR_INVALID_PARAM;
935     }
936     ifName = HdfSbufReadString(reqData);
937     if (ifName == NULL) {
938         HDF_LOGE("%s: %s!ParamName=%s", __func__, ERROR_DESC_READ_REQ_FAILED, "ifName");
939         return HDF_FAILURE;
940     }
941     netdev = NetDeviceGetInstByName(ifName);
942     if (netdev == NULL) {
943         HDF_LOGE("%s:netdev not found!ifName=%s", __func__, ifName);
944         return HDF_FAILURE;
945     }
946     if (!HdfSbufReadBuffer(reqData, (const void **)&mac, &replayDataSize) || mac == NULL ||
947         replayDataSize != IEEE80211_MAC_ADDR_LEN) {
948         HDF_LOGE("%s: %s!ParamName=%s", __func__, ERROR_DESC_READ_REQ_FAILED, "mac");
949         return HDF_FAILURE;
950     }
951     ret = SetMacAddr(netdev, mac, IEEE80211_MAC_ADDR_LEN);
952     if (ret != HDF_SUCCESS) {
953         HDF_LOGE("%s: fail to set mac addr,%d", __func__, ret);
954     }
955     return ret;
956 }
957 
WifiCmdGetValidFreqsWithBand(const RequestContext * context,struct HdfSBuf * reqData,struct HdfSBuf * rspData)958 static int32_t WifiCmdGetValidFreqsWithBand(const RequestContext *context, struct HdfSBuf *reqData,
959     struct HdfSBuf *rspData)
960 {
961     int32_t ret;
962     struct NetDevice *netdev = NULL;
963     const char *ifName = NULL;
964     int32_t band;
965     int32_t freqs[WIFI_24G_CHANNEL_NUM] = {0};
966     uint32_t num = 0;
967 
968     (void)context;
969     if (reqData == NULL || rspData == NULL) {
970         return HDF_ERR_INVALID_PARAM;
971     }
972     ifName = HdfSbufReadString(reqData);
973     if (ifName == NULL) {
974         HDF_LOGE("%s: %s!ParamName=%s", __func__, ERROR_DESC_READ_REQ_FAILED, "ifName");
975         return HDF_FAILURE;
976     }
977     netdev = NetDeviceGetInstByName(ifName);
978     if (netdev == NULL) {
979         HDF_LOGE("%s:netdev not found!ifName=%s", __func__, ifName);
980         return HDF_FAILURE;
981     }
982     if (!HdfSbufReadInt32(reqData, &band)) {
983         HDF_LOGE("%s: %s!ParamName=%s", __func__, ERROR_DESC_READ_REQ_FAILED, "band");
984         return HDF_FAILURE;
985     }
986     ret = GetValidFreqsWithBand(netdev, band, freqs, &num);
987     if (ret != HDF_SUCCESS) {
988         HDF_LOGE("%s: fail to get valid freqs,%d", __func__, ret);
989         return ret;
990     }
991     if (!HdfSbufWriteUint32(rspData, num)) {
992         HDF_LOGE("%s: %s!", __func__, ERROR_DESC_WRITE_RSP_FAILED);
993         return HDF_FAILURE;
994     }
995     if (!HdfSbufWriteBuffer(rspData, freqs, num * sizeof(int32_t))) {
996         HDF_LOGE("%s: %s!", __func__, ERROR_DESC_WRITE_RSP_FAILED);
997         return HDF_FAILURE;
998     }
999     return ret;
1000 }
1001 
WifiCmdSetTxPower(const RequestContext * context,struct HdfSBuf * reqData,struct HdfSBuf * rspData)1002 static int32_t WifiCmdSetTxPower(const RequestContext *context, struct HdfSBuf *reqData, struct HdfSBuf *rspData)
1003 {
1004     int32_t ret;
1005     struct NetDevice *netdev = NULL;
1006     const char *ifName = NULL;
1007     int32_t power;
1008 
1009     (void)context;
1010     if (reqData == NULL || rspData == NULL) {
1011         return HDF_ERR_INVALID_PARAM;
1012     }
1013     ifName = HdfSbufReadString(reqData);
1014     if (ifName == NULL) {
1015         HDF_LOGE("%s: %s!ParamName=%s", __func__, ERROR_DESC_READ_REQ_FAILED, "ifName");
1016         return HDF_FAILURE;
1017     }
1018     netdev = NetDeviceGetInstByName(ifName);
1019     if (netdev == NULL) {
1020         HDF_LOGE("%s:netdev not found!ifName=%s", __func__, ifName);
1021         return HDF_FAILURE;
1022     }
1023     if (!HdfSbufReadInt32(reqData, &power) || power < 0) {
1024         HDF_LOGE("%s: %s!ParamName=%s", __func__, ERROR_DESC_READ_REQ_FAILED, "power");
1025         return HDF_FAILURE;
1026     }
1027     ret = SetTxPower(netdev, power);
1028     if (ret != HDF_SUCCESS) {
1029         HDF_LOGE("%s: fail to set tx power,%d", __func__, ret);
1030     }
1031     return ret;
1032 }
1033 
WifiCmdSetClient(const RequestContext * context,struct HdfSBuf * reqData,struct HdfSBuf * rspData)1034 static int32_t WifiCmdSetClient(const RequestContext *context, struct HdfSBuf *reqData, struct HdfSBuf *rspData)
1035 {
1036     uint32_t clientNum = 0;
1037     struct HdfWifiEventToClientMap *eventToClientMap = NULL;
1038     (void)rspData;
1039     if (reqData == NULL || context == NULL) {
1040         return HDF_ERR_INVALID_PARAM;
1041     }
1042 
1043     if (!HdfSbufReadUint32(reqData, &clientNum)) {
1044         HDF_LOGE("%s: %s!ParamName=%s", __func__, ERROR_DESC_READ_REQ_FAILED, "clientNum");
1045         return HDF_FAILURE;
1046     }
1047 
1048     eventToClientMap = HdfWifiGetEventToClientMap();
1049     if (eventToClientMap == NULL) {
1050         HDF_LOGE("%s:get HdfWifiEventToClientMap failed", __func__);
1051         return HDF_FAILURE;
1052     }
1053 
1054     if (clientNum == WIFI_KERNEL_TO_WPA_CLIENT) {
1055         eventToClientMap->wpaClient = context->client;
1056     } else if (clientNum == WIFI_KERNEL_TO_HAL_CLIENT) {
1057         eventToClientMap->halClient = context->client;
1058     }
1059     return HDF_SUCCESS;
1060 }
1061 
HdfdWlanGetChipId(const char * ifName,uint8_t * chipId)1062 static int32_t HdfdWlanGetChipId(const char *ifName, uint8_t *chipId)
1063 {
1064     struct NetDevice *netdev = NULL;
1065     struct HdfWifiNetDeviceData *netDeviceData = NULL;
1066 
1067     if (ifName == NULL || chipId == NULL) {
1068         HDF_LOGE("%s: para is NULL", __func__);
1069         return HDF_FAILURE;
1070     }
1071     netdev = NetDeviceGetInstByName(ifName);
1072     if (netdev == NULL) {
1073         HDF_LOGE("%s:netdev not found!ifName=%s.", __func__, ifName);
1074         return HDF_FAILURE;
1075     }
1076     netDeviceData = GetPlatformData(netdev);
1077     if (netDeviceData == NULL) {
1078         HDF_LOGE("%s:platform netDeviceData is NULL!", __func__);
1079         return HDF_FAILURE;
1080     }
1081 
1082     *chipId = netDeviceData->device->id;
1083     return HDF_SUCCESS;
1084 }
1085 
WifiCmdGetChipId(const RequestContext * context,struct HdfSBuf * reqData,struct HdfSBuf * rspData)1086 static int32_t WifiCmdGetChipId(const RequestContext *context, struct HdfSBuf *reqData, struct HdfSBuf *rspData)
1087 {
1088     int32_t ret;
1089     const char *ifName = NULL;
1090     uint8_t chipId = 0;
1091 
1092     (void)context;
1093     if (reqData == NULL || rspData == NULL) {
1094         return HDF_ERR_INVALID_PARAM;
1095     }
1096     ifName = HdfSbufReadString(reqData);
1097     if (ifName == NULL) {
1098         HDF_LOGE("%s: %s!ParamName=%s", __func__, ERROR_DESC_READ_REQ_FAILED, "ifName");
1099         return HDF_FAILURE;
1100     }
1101 
1102     ret = HdfdWlanGetChipId(ifName, &chipId);
1103     if (ret != HDF_SUCCESS) {
1104         HDF_LOGE("%s: fail to get chipId, %d", __func__, ret);
1105         return HDF_FAILURE;
1106     }
1107     HDF_LOGE("%s: chipid = %d.", __func__, chipId);
1108     if (!HdfSbufWriteUint8(rspData, chipId)) {
1109         HDF_LOGE("%s: %s!", __func__, ERROR_DESC_WRITE_RSP_FAILED);
1110         return HDF_FAILURE;
1111     }
1112     return ret;
1113 }
1114 
WifiCmdGetIfNamesByChipId(const RequestContext * context,struct HdfSBuf * reqData,struct HdfSBuf * rspData)1115 static int32_t WifiCmdGetIfNamesByChipId(const RequestContext *context, struct HdfSBuf *reqData,
1116     struct HdfSBuf *rspData)
1117 {
1118     uint8_t chipId;
1119     uint8_t ifNameCount = 0;
1120     char *ifNames = NULL;
1121     int32_t ret = HDF_SUCCESS;
1122 
1123     (void)context;
1124     if (reqData == NULL || rspData == NULL) {
1125         return HDF_ERR_INVALID_PARAM;
1126     }
1127     if (!HdfSbufReadUint8(reqData, &chipId)) {
1128         HDF_LOGE("%s: %s!ParamName=%s", __func__, ERROR_DESC_READ_REQ_FAILED, "chipId");
1129         return HDF_FAILURE;
1130     }
1131 
1132     ifNames = HdfWlanGetIfNames(chipId, &ifNameCount);
1133     if (ifNames == NULL) {
1134         HDF_LOGE("%s: fail to get ifnames!", __func__);
1135         return HDF_FAILURE;
1136     }
1137     HDF_LOGI("%s: get ifName num: %d.\n", __func__, ifNameCount);
1138     do {
1139         if (!HdfSbufWriteUint32(rspData, ifNameCount)) {
1140             HDF_LOGE("%s: %s!", __func__, ERROR_DESC_WRITE_RSP_FAILED);
1141             ret = HDF_FAILURE;
1142             break;
1143         }
1144 
1145         if (!HdfSbufWriteBuffer(rspData, ifNames, ifNameCount * IFNAMSIZ)) {
1146             HDF_LOGE("%s: %s!", __func__, ERROR_DESC_WRITE_RSP_FAILED);
1147             ret = HDF_FAILURE;
1148             break;
1149         }
1150     } while (false);
1151     OsalMemFree(ifNames);
1152     return ret;
1153 }
1154 
WifiResetEntranceCheck(const uint8_t chipId)1155 static int32_t WifiResetEntranceCheck(const uint8_t chipId)
1156 {
1157     uint8_t i;
1158     int32_t ret = HDF_SUCCESS;
1159     uint8_t ifNameCount = 0;
1160     char *ifNames = NULL;
1161     struct NetDevice *netdev = NULL;
1162 
1163     ifNames = HdfWlanGetIfNames(chipId, &ifNameCount);
1164     if (ifNames == NULL) {
1165         HDF_LOGE("%s: HdfWlanGetIfNames failed!", __func__);
1166         return HDF_FAILURE;
1167     }
1168     /* check the netDevice is busy or not, do reset when it is free. */
1169     for (i = 0; i < ifNameCount; i++) {
1170         netdev = NetDeviceGetInstByName(ifNames + i * IFNAMSIZ);
1171         if (netdev == NULL) {
1172             HDF_LOGE("%s:netdev not found!ifName=%s", __func__, ifNames + i * IFNAMSIZ);
1173             continue;
1174         }
1175         if (NetDeviceIsInstRunning(netdev)) {
1176             HDF_LOGE("%s: the netdevice is using!", __func__);
1177             ret = ERR_NETDEVICE_IS_USING;
1178             break;
1179         }
1180     }
1181     OsalMemFree(ifNames);
1182     return ret;
1183 }
1184 
ResetParaCheck(const RequestContext * context,struct HdfSBuf * reqData,struct HdfSBuf * rspData)1185 static int32_t ResetParaCheck(const RequestContext *context, struct HdfSBuf *reqData, struct HdfSBuf *rspData)
1186 {
1187     if (context == NULL || reqData == NULL || rspData == NULL) {
1188         HDF_LOGE("%s: para is null!", __func__);
1189         return HDF_FAILURE;
1190     }
1191 
1192     if (context->senderId != BASE_SERVICE_ID) {
1193         HDF_LOGE("%s: the senderId is error!", __func__);
1194         return HDF_FAILURE;
1195     }
1196     return HDF_SUCCESS;
1197 }
1198 
WifiCmdDoResetChip(const RequestContext * context,struct HdfSBuf * reqData,struct HdfSBuf * rspData)1199 static int32_t WifiCmdDoResetChip(const RequestContext *context, struct HdfSBuf *reqData, struct HdfSBuf *rspData)
1200 {
1201     int32_t ret;
1202     uint8_t chipId;
1203     const char *ifName = NULL;
1204     struct HdfWlanDevice *wlanDevice = NULL;
1205 
1206     ret = ResetParaCheck(context, reqData, rspData);
1207     if (ret != HDF_SUCCESS) {
1208         return HDF_FAILURE;
1209     }
1210 
1211     if (!HdfSbufReadUint8(reqData, &chipId)) {
1212         HDF_LOGE("%s: %s!ParamName=%s", __func__, ERROR_DESC_READ_REQ_FAILED, "chipId");
1213         return HDF_FAILURE;
1214     }
1215     ifName = HdfSbufReadString(reqData);
1216     if (ifName == NULL) {
1217         HDF_LOGE("%s: %s!ParamName=%s", __func__, ERROR_DESC_READ_REQ_FAILED, "ifName");
1218         return HDF_FAILURE;
1219     }
1220     /* callback function use chipId */
1221     if (!HdfSbufWriteUint8(rspData, chipId)) {
1222         HDF_LOGE("%s: %s!", __func__, ERROR_DESC_WRITE_RSP_FAILED);
1223         return HDF_FAILURE;
1224     }
1225     if (!HdfSbufWriteString(rspData, ifName)) {
1226         HDF_LOGE("%s: Serialize failed!", __func__);
1227         return HDF_FAILURE;
1228     }
1229 
1230     ret = WifiResetEntranceCheck(chipId);
1231     if (ret != HDF_SUCCESS) {
1232         return ret;
1233     }
1234 
1235     wlanDevice = HdfWlanGetWlanDevice(chipId);
1236     if (wlanDevice == NULL || wlanDevice->reset == NULL) {
1237         HDF_LOGE("%s: wlanDevice or wlanDevice->reset is NULL!", __func__);
1238         return HDF_FAILURE;
1239     }
1240 
1241     ret = HdfWifiDeinitDevice(wlanDevice);
1242     if (ret != HDF_SUCCESS) {
1243         return ret;
1244     }
1245 
1246     /* power reset */
1247     ret = wlanDevice->reset->Reset(wlanDevice->reset);
1248     if (ret != HDF_SUCCESS) {
1249         HDF_LOGE("%s:power reset failed!", __func__);
1250         return ERR_POWER_RESET_FAIL;
1251     }
1252     ret = HdfWifiInitDevice(wlanDevice);
1253     return ret;
1254 }
1255 
SendMessageResetDriverCallBack(const RequestContext * context,struct HdfSBuf * reqData,struct HdfSBuf * rspData,ErrorCode rspCode)1256 void SendMessageResetDriverCallBack(const RequestContext *context, struct HdfSBuf *reqData, struct HdfSBuf *rspData,
1257     ErrorCode rspCode)
1258 {
1259     uint8_t chipId;
1260     int32_t ret;
1261     const char *ifName = NULL;
1262     (void)context;
1263 
1264     if (rspData == NULL || reqData == NULL) {
1265         HDF_LOGE("%s: para is null!", __func__);
1266         return;
1267     }
1268 
1269     if (!HdfSbufReadUint8(rspData, &chipId)) {
1270         HDF_LOGE("%s: read data failed! ParamName=%s", __func__, "chipId");
1271         return;
1272     }
1273     ifName = HdfSbufReadString(rspData);
1274     if (ifName == NULL) {
1275         HDF_LOGE("%s: %s!ParamName=%s", __func__, ERROR_DESC_READ_REQ_FAILED, "ifName");
1276         return;
1277     }
1278 
1279     ret = HdfWifiEventResetResult(chipId, rspCode, ifName);
1280     if (ret != HDF_SUCCESS) {
1281         HDF_LOGE("%s: send resetDriver event fail!", __func__);
1282     }
1283     return;
1284 }
1285 
WifiCmdResetDriver(const RequestContext * context,struct HdfSBuf * reqData,struct HdfSBuf * rspData)1286 static int32_t WifiCmdResetDriver(const RequestContext *context, struct HdfSBuf *reqData, struct HdfSBuf *rspData)
1287 {
1288     int32_t ret;
1289     struct HdfSBuf *data = NULL;
1290     (void)context;
1291     if (reqData == NULL || rspData == NULL) {
1292         return HDF_ERR_INVALID_PARAM;
1293     }
1294 
1295     data = HdfSbufCopy(reqData);
1296     if (data == NULL) {
1297         HDF_LOGE("%s: sbuf copy fail", __func__);
1298         return HDF_FAILURE;
1299     }
1300 
1301     ret = g_baseService->SendAsyncMessage(g_baseService, BASE_SERVICE_ID, CMD_BASE_DO_RESET_PRIVATE, data,
1302         SendMessageResetDriverCallBack);
1303     if (ret != HDF_SUCCESS) {
1304         HdfSbufRecycle(data);
1305         HDF_LOGE("%s: fail to reset the driver,%d", __func__, ret);
1306     }
1307     return ret;
1308 }
1309 
GetIftype(struct NetDevice * netdev,uint8_t * iftype)1310 static uint32_t GetIftype(struct NetDevice *netdev, uint8_t *iftype)
1311 {
1312     struct HdfChipDriver *chipDriver = GetChipDriver(netdev);
1313     if (chipDriver == NULL) {
1314         HDF_LOGE("%s:bad net device found!", __func__);
1315         return HDF_FAILURE;
1316     }
1317     RETURN_IF_CHIPOPS_NOT_IMPLEMENT(chipDriver->ops, GetIftype);
1318     return chipDriver->ops->GetIftype(netdev, iftype);
1319 }
1320 
1321 #define MAX_NETDEVICE_COUNT 20
WifiCmdGetNetDevInfo(const RequestContext * context,struct HdfSBuf * reqData,struct HdfSBuf * rspData)1322 static int32_t WifiCmdGetNetDevInfo(const RequestContext *context, struct HdfSBuf *reqData, struct HdfSBuf *rspData)
1323 {
1324     uint32_t i;
1325     uint32_t netdevNum;
1326     uint8_t iftype;
1327     struct NetDevice *netDev = NULL;
1328     (void)context;
1329     (void)reqData;
1330 
1331     netdevNum = NetDevGetRegisterCount();
1332     if (!HdfSbufWriteUint32(rspData, netdevNum)) {
1333         HDF_LOGE("%s: %s!", __func__, ERROR_DESC_WRITE_RSP_FAILED);
1334         return HDF_FAILURE;
1335     }
1336     for (i = 0; i < MAX_NETDEVICE_COUNT; i++) {
1337         netDev = NetDeviceGetInstByIndex(i);
1338         if (netDev != NULL) {
1339             if (GetIftype(netDev, &iftype) != HDF_SUCCESS) {
1340                 iftype = 0;
1341             }
1342             if (!HdfSbufWriteUint32(rspData, i) ||
1343                 !HdfSbufWriteBuffer(rspData, netDev->name, strlen(netDev->name) + 1) ||
1344                 !HdfSbufWriteUint8(rspData, iftype) ||
1345                 !HdfSbufWriteBuffer(rspData, GET_NET_DEV_MAC_ADDR(netDev), ETH_ADDR_LEN)) {
1346                 HDF_LOGE("%s: %s!", __func__, ERROR_DESC_WRITE_RSP_FAILED);
1347                 return HDF_FAILURE;
1348             }
1349         }
1350     }
1351     return HDF_SUCCESS;
1352 }
1353 
1354 static struct MessageDef g_wifiBaseFeatureCmds[] = {
1355     DUEMessage(CMD_BASE_NEW_KEY, WifiCmdNewKey, 0),
1356     DUEMessage(CMD_BASE_DEL_KEY, WifiCmdDelKey, 0),
1357     DUEMessage(CMD_BASE_SET_DEFAULT_KEY, WifiCmdSetKey, 0),
1358     DUEMessage(CMD_BASE_SEND_MLME, WifiSendMlme, 0),
1359     DUEMessage(CMD_BASE_SEND_EAPOL, WifiCmdSendEapol, 0),
1360     DUEMessage(CMD_BASE_RECEIVE_EAPOL, WifiCmdReceiveEapol, 0),
1361     DUEMessage(CMD_BASE_ENALBE_EAPOL, WifiCmdEnableEapol, 0),
1362     DUEMessage(CMD_BASE_DISABLE_EAPOL, WifiCmdDisableEapol, 0),
1363     DUEMessage(CMD_BASE_GET_ADDR, WifiCmdGetAddr, 0),
1364     DUEMessage(CMD_BASE_SET_MODE, WifiCmdSetMode, 0),
1365     DUEMessage(CMD_BASE_GET_HW_FEATURE, WifiCmdGetHwFeature, 0),
1366     DUEMessage(CMD_BASE_SET_NETDEV, WifiCmdSetNetdev, 0),
1367     DUEMessage(CMD_BASE_SEND_ACTION, WifiCmdSendAction, 0),
1368     DUEMessage(CMD_BASE_SET_CLIENT, WifiCmdSetClient, 0),
1369     DUEMessage(CMD_BASE_GET_NETWORK_INFO, WifiCmdGetNetworkInfo, 0),
1370     DUEMessage(CMD_BASE_IS_SUPPORT_COMBO, WifiCmdIsSupportCombo, 0),
1371     DUEMessage(CMD_BASE_GET_SUPPORT_COMBO, WifiCmdGetSupportCombo, 0),
1372     DUEMessage(CMD_BASE_GET_DEV_MAC_ADDR, WifiCmdGetDevMacAddr, 0),
1373     DUEMessage(CMD_BASE_SET_MAC_ADDR, WifiCmdSetMacAddr, 0),
1374     DUEMessage(CMD_BASE_GET_VALID_FREQ, WifiCmdGetValidFreqsWithBand, 0),
1375     DUEMessage(CMD_BASE_SET_TX_POWER, WifiCmdSetTxPower, 0),
1376     DUEMessage(CMD_BASE_GET_CHIPID, WifiCmdGetChipId, 0),
1377     DUEMessage(CMD_BASE_GET_IFNAMES, WifiCmdGetIfNamesByChipId, 0),
1378     DUEMessage(CMD_BASE_RESET_DRIVER, WifiCmdResetDriver, 0),
1379     DUEMessage(CMD_BASE_GET_NETDEV_INFO, WifiCmdGetNetDevInfo, 0),
1380     DUEMessage(CMD_BASE_DO_RESET_PRIVATE, WifiCmdDoResetChip, 0),
1381 };
1382 ServiceDefine(BaseService, BASE_SERVICE_ID, g_wifiBaseFeatureCmds);
1383 
BaseInit()1384 int32_t BaseInit()
1385 {
1386     if (g_baseService == NULL) {
1387         ServiceCfg cfg = {
1388             .dispatcherId = DEFAULT_DISPATCHER_ID
1389         };
1390         g_baseService = CreateService(BaseService, &cfg);
1391         if (g_baseService == NULL) {
1392             return HDF_FAILURE;
1393         }
1394     }
1395     return HDF_SUCCESS;
1396 }
1397 
BaseDeinit()1398 int32_t BaseDeinit()
1399 {
1400     if (g_baseService != NULL && g_baseService->Destroy != NULL) {
1401         g_baseService->Destroy(g_baseService);
1402         g_baseService = NULL;
1403     }
1404     return HDF_SUCCESS;
1405 }
1406