• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2021 Chipsea Technologies (Shenzhen) Corp., Ltd. All rights reserved.
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at
6  *
7  *     http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14  */
15 #include "wifi_config.h"
16 #include "wifi_host.h"
17 #include "wifi_host_config.h"
18 #include "wlan_if.h"
19 #include "porting_net_def.h"
20 #include "rwnx_defs.h"
21 #include "wifi_cfg_nx.h"
22 #include "securec.h"
23 
24 #define DEFAULT_STA_VIF 0
25 #define MAX_WIFI_EVENT_NUM 10
26 #define MAX_WIFI_KEY_LEN 64
27 
28 static ChipseaWifiStaData g_wifiData = {0};
29 
30 rtos_mutex g_wifiMutex = NULL;
31 
32 #define WIFI_STATE_INVALID_CHECK(sta) do { \
33     if (g_wifiData.state == (sta)) {       \
34         return ERROR_WIFI_UNKNOWN;   \
35     }                                      \
36 } while(0)
37 
38 #define WIFI_CLOSE_LINK(link) do { \
39     if ((link) != NULL) { \
40         fhost_cntrl_cfgrwnx_link_close(link); \
41         (link) = NULL; \
42     } \
43 } while(0)
44 
45 void WifiDeviceEntry(void *arg);
46 
SecureHm2Chipsea(int hmType,uint32_t * akm)47 void SecureHm2Chipsea(int hmType, uint32_t *akm)
48 {
49     *akm = 0;
50     switch (hmType) {
51         case WIFI_SEC_TYPE_WEP:
52             *akm |= CO_BIT(MAC_AKM_PRE_RSN);
53             break;
54         case WIFI_SEC_TYPE_PSK:
55             *akm |= CO_BIT(MAC_AKM_PRE_RSN) | CO_BIT(MAC_AKM_PSK);
56             break;
57         case WIFI_SEC_TYPE_SAE:
58             *akm |= CO_BIT(MAC_AKM_SAE);
59             break;
60         default:
61             *akm |= CO_BIT(MAC_AKM_NONE);
62     }
63     dbg("SecureHm2Chipsea hmType = %d,akm = %d\r\n", hmType, *akm);
64 }
65 
SecureChipsea2Hm(int * hmType,uint32_t akm)66 void SecureChipsea2Hm(int *hmType, uint32_t akm)
67 {
68     if (akm & CO_BIT(MAC_AKM_PRE_RSN)) {
69         *hmType = WIFI_SEC_TYPE_WEP;
70     } else if (akm & CO_BIT(MAC_AKM_PSK)) {
71         *hmType = WIFI_SEC_TYPE_WEP;
72     } else if (akm & CO_BIT(MAC_AKM_SAE)) {
73         *hmType = WIFI_SEC_TYPE_SAE;
74     } else {
75         *hmType = WIFI_SEC_TYPE_OPEN;
76     }
77     dbg("SecureChipsea2Hm akm = %d,hmType = %d\r\n", akm, *hmType);
78 }
79 
WifiCreateLock()80 int32_t WifiCreateLock()
81 {
82     if (g_wifiMutex == NULL) {
83         if (rtos_mutex_create(&g_wifiMutex) != 0) {
84             dbg("wifiDevice:mutex create err\r\n");
85             return ERROR_WIFI_NOT_AVAILABLE;
86         }
87     }
88     if (g_wifiMutex != NULL) {
89         if (rtos_mutex_lock(g_wifiMutex, osWaitForever) != 0) {
90             dbg("wifiDevice:mutex lock err\r\n");
91             return ERROR_WIFI_NOT_AVAILABLE;
92         }
93     }
94     return WIFI_SUCCESS;
95 }
96 
WifiUnlock()97 int32_t WifiUnlock()
98 {
99     if (g_wifiMutex == NULL) {
100         dbg("wifiDevice:mutex lock null\r\n");
101         return ERROR_WIFI_NOT_AVAILABLE;
102     }
103 
104     if (rtos_mutex_unlock(g_wifiMutex) != 0) {
105         dbg("wifiDevice:mutex unlock err\r\n");
106         return ERROR_WIFI_NOT_AVAILABLE;
107     }
108     return WIFI_SUCCESS;
109 }
110 
AddDeviceConfig(const WifiDeviceConfig * config,int * result)111 WifiErrorCode AddDeviceConfig(const WifiDeviceConfig *config, int *result)
112 {
113     PARAM_CHECK(config);
114     PARAM_CHECK(result);
115     uint8_t index;
116 
117     if (WifiCreateLock() != WIFI_SUCCESS) {
118         return ERROR_WIFI_NOT_AVAILABLE;
119     }
120 
121     for (index = 0; index < WIFI_MAX_CONFIG_SIZE; index++) {
122         if (g_wifiData.deviceTab[index].used == 0) {
123             memcpy(&g_wifiData.deviceTab[index].devConf, config, sizeof(WifiDeviceConfig));
124             g_wifiData.deviceTab[index].used = 1;
125             g_wifiData.deviceTab[index].devConf.netId = index;
126             *result = index;
127             WifiUnlock();
128             dbg("wifiDevice:AddDeviceConfig success netid = %d\r\n", *result);
129             return WIFI_SUCCESS;
130         }
131     }
132 
133     WifiUnlock();
134     dbg("wifiDevice:AddDeviceConfig err index = %d\r\n", index);
135     return ERROR_WIFI_BUSY;
136 }
137 
GetDeviceConfigs(WifiDeviceConfig * result,unsigned int * size)138 WifiErrorCode GetDeviceConfigs(WifiDeviceConfig *result, unsigned int *size)
139 {
140     PARAM_CHECK(result);
141     PARAM_CHECK(size);
142     uint8_t index;
143     uint8_t cnt;
144 
145     if (WifiCreateLock() != WIFI_SUCCESS) {
146         return ERROR_WIFI_NOT_AVAILABLE;
147     }
148 
149     for (index = 0, cnt = 0; index < WIFI_MAX_CONFIG_SIZE; index++) {
150         if (g_wifiData.deviceTab[index].used == 1) {
151             memcpy(result + cnt, &g_wifiData.deviceTab[index].devConf, sizeof(WifiDeviceConfig));
152             cnt++;
153         }
154     }
155 
156     *size = cnt;
157     WifiUnlock();
158     dbg("wifiDevice:GetDeviceConfigs size = %d\r\n", *size);
159     return (*size == 0) ? ERROR_WIFI_NOT_AVAILABLE : WIFI_SUCCESS;
160 }
161 
RemoveDevice(int networkId)162 WifiErrorCode RemoveDevice(int networkId)
163 {
164     memset(&g_wifiData.deviceTab[networkId], 0, sizeof(WifiConnectDevice));
165     dbg("wifiDevice:RemoveDevice networkId = %d\r\n", networkId);
166     return WIFI_SUCCESS;
167 }
168 
SetStaVif()169 static int32_t SetStaVif()
170 {
171     ipc_host_cntrl_start();
172 
173     WIFI_CLOSE_LINK(g_wifiData.consoleCntrlLink);
174     g_wifiData.consoleCntrlLink = fhost_cntrl_cfgrwnx_link_open();
175     if (g_wifiData.consoleCntrlLink == NULL) {
176         dbg("Failed to open link with control task\n");
177         return ERROR_WIFI_UNKNOWN;
178     }
179 
180     if (fhost_set_vif_type(g_wifiData.consoleCntrlLink, DEFAULT_STA_VIF, VIF_UNKNOWN, false) ||
181         fhost_set_vif_type(g_wifiData.consoleCntrlLink, DEFAULT_STA_VIF, VIF_STA, false)) {
182         WIFI_CLOSE_LINK(g_wifiData.consoleCntrlLink);
183         return ERROR_WIFI_UNKNOWN;
184     }
185 
186     return WIFI_SUCCESS;
187 }
188 
Scan(void)189 WifiErrorCode Scan(void)
190 {
191     WIFI_STATE_INVALID_CHECK(WIFI_INACTIVE);
192 
193     ChipseaWifiMsg msg = {
194         .eventId = WIFI_START_SCAN,
195         .payLoad = 0,
196     };
197 
198     if (WifiCreateLock() != WIFI_SUCCESS) {
199         return ERROR_WIFI_NOT_AVAILABLE;
200     }
201     if (rtos_queue_write(g_wifiData.wifiQueue, &msg, 1, false) != 0) {
202         dbg("wifiDevice:rtos_queue_write err\r\n");
203         WifiUnlock();
204         return ERROR_WIFI_NOT_AVAILABLE;
205     }
206     WifiUnlock();
207     return WIFI_SUCCESS;
208 }
209 
DoScan(void)210 int32_t DoScan(void)
211 {
212     WIFI_STATE_INVALID_CHECK(WIFI_STA_NOT_ACTIVE);
213 
214     g_wifiData.scanSize = 0;
215     if (SetStaVif() != WIFI_SUCCESS) {
216         dbg("SetStaVif err\r\n");
217         DoScanCallBack(WIFI_STATE_NOT_AVAILABLE, 0);
218         return ERROR_WIFI_NOT_AVAILABLE;
219     }
220 
221     g_wifiData.scanSize = fhost_scan(g_wifiData.consoleCntrlLink, DEFAULT_STA_VIF, NULL);
222     if (g_wifiData.scanSize < 0) {
223         dbg("wifiDevice:scan size err:%d\r\n", g_wifiData.scanSize);
224         WIFI_CLOSE_LINK(g_wifiData.consoleCntrlLink);
225         DoScanCallBack(WIFI_STATE_NOT_AVAILABLE, 0);
226         return ERROR_WIFI_NOT_AVAILABLE;
227     }
228 
229     DoScanCallBack(WIFI_STATE_AVAILABLE, g_wifiData.scanSize);
230     return WIFI_SUCCESS;
231 }
232 
AdvanceScan(WifiScanParams * params)233 WifiErrorCode AdvanceScan(WifiScanParams *params)
234 {
235     WIFI_STATE_INVALID_CHECK(WIFI_STA_NOT_ACTIVE);
236     PARAM_CHECK(params);
237 
238     dbg("AdvanceScan start = %d\r\n", params->scanType);
239 
240     WifiScanParams *paramSave = rtos_calloc(1, sizeof(WifiScanParams));
241     if (paramSave == NULL) {
242         return ERROR_WIFI_UNKNOWN;
243     }
244 
245     paramSave->scanType = params->scanType;
246     switch (params->scanType) {
247         case WIFI_FREQ_SCAN:
248             if (params->freqs == 0) {
249                 dbg("check freqs = 0\r\n");
250                 return ERROR_WIFI_UNKNOWN;
251             }
252             break;
253         case WIFI_SSID_SCAN:
254             if ((params->ssidLen == 0) || (memcmp(paramSave->ssid, params->ssid, params->ssidLen) == 0)) {
255                 dbg("ssid len = 0\r\n");
256                 return ERROR_WIFI_UNKNOWN;
257             }
258             paramSave->ssidLen = params->ssidLen;
259             memcpy_s(paramSave->ssid, WIFI_MAX_SSID_LEN, params->ssid, params->ssidLen);
260             break;
261         case WIFI_BSSID_SCAN:
262             if (memcmp(paramSave->bssid, params->bssid, sizeof(paramSave->bssid)) == 0) {
263                 dbg("bssid len = 0\r\n");
264                 return ERROR_WIFI_UNKNOWN;
265             }
266             memcpy_s(paramSave->bssid, WIFI_MAC_LEN, params->bssid, WIFI_MAC_LEN);
267             break;
268         case WIFI_BAND_SCAN:
269             paramSave->band = params->band;
270             break;
271         default:
272             paramSave->scanType = WIFI_BAND_SCAN;
273             paramSave->band = PHY_BAND_2G4;
274             dbg("default scan,use band 2g\r\n");
275             break;
276     }
277 
278     ChipseaWifiMsg msg = {
279         .eventId = WIFI_START_ADVANCE_SCAN,
280         .payLoad = (uint32_t)paramSave,
281     };
282 
283     if (WifiCreateLock() != WIFI_SUCCESS) {
284         return ERROR_WIFI_NOT_AVAILABLE;
285     }
286     if (rtos_queue_write(g_wifiData.wifiQueue, &msg, 1, false) != 0) {
287         dbg("wifiDevice:rtos_queue_write err\r\n");
288         WifiUnlock();
289         return ERROR_WIFI_NOT_AVAILABLE;
290     }
291     WifiUnlock();
292     return WIFI_SUCCESS;
293 }
294 
DoAdvanceScan(WifiScanParams * params)295 void DoAdvanceScan(WifiScanParams *params)
296 {
297     PARAM_CHECK(params);
298     struct fhost_super_scan_t scanParam = {0};
299     struct cfgrwnx_scan_ssid ssid = {0};
300 
301     g_wifiData.scanSize = 0;
302 
303     switch (params->scanType) {
304         case WIFI_FREQ_SCAN:
305             scanParam.band = -1;
306             scanParam.freqs = &params->freqs;
307             break;
308         case WIFI_SSID_SCAN:
309             ssid.len = params->ssidLen;
310             ssid.ssid = (uint8_t *)params->ssid;
311             scanParam.ssid_cnt = 1;
312             scanParam.ssids = &ssid;
313             break;
314         case WIFI_BSSID_SCAN:
315             scanParam.bssid = (uint8_t *)params->bssid;
316             break;
317         case WIFI_BAND_SCAN:
318             scanParam.band = params->band;
319             break;
320         default:
321             rtos_free(params);
322             DoScanCallBack(WIFI_STATE_AVAILABLE, 0);
323             return;
324         break;
325     }
326     rtos_free(params);
327 
328     if (SetStaVif() != WIFI_SUCCESS) {
329         dbg("SetStaVif err\r\n");
330         DoScanCallBack(WIFI_STATE_AVAILABLE, 0);
331         return;
332     }
333 
334     g_wifiData.scanSize = fhost_super_scan(g_wifiData.consoleCntrlLink, 0, &scanParam);
335     if (g_wifiData.scanSize < 0) {
336         WIFI_CLOSE_LINK(g_wifiData.consoleCntrlLink);
337         g_wifiData.scanSize = 0;
338         dbg("fhost_super_scan,no result!\r\n");
339     }
340 
341     DoScanCallBack(WIFI_STATE_AVAILABLE, g_wifiData.scanSize);
342 }
343 
GetScanInfoList(WifiScanInfo * result,unsigned int * size)344 WifiErrorCode GetScanInfoList(WifiScanInfo *result, unsigned int *size)
345 {
346     PARAM_CHECK(result);
347     PARAM_CHECK(size);
348 
349     if (WifiCreateLock() != WIFI_SUCCESS) {
350         return ERROR_WIFI_NOT_AVAILABLE;
351     }
352 
353     struct mac_scan_result scanRst;
354     int32_t rstIndex = 0;
355 
356     if (g_wifiData.scanSize <= 0 || g_wifiData.consoleCntrlLink == NULL) {
357         *size = 0;
358         WifiUnlock();
359         dbg("get scan list not init!\r\n");
360         return WIFI_SUCCESS;
361     }
362 
363     while(fhost_get_scan_results(g_wifiData.consoleCntrlLink, rstIndex, 1, &scanRst)) {
364         scanRst.ssid.array[scanRst.ssid.length] = '\0';
365 
366         strcpy((result + rstIndex)->ssid, (char *)scanRst.ssid.array);
367         memcpy(result->bssid, scanRst.bssid.array, WIFI_MAC_LEN);
368 
369         (result + rstIndex)->band = scanRst.chan->band;
370         (result + rstIndex)->frequency = scanRst.chan->freq;
371         SecureChipsea2Hm((&(result + rstIndex)->securityType), scanRst.akm);
372         (result + rstIndex)->rssi = (int8_t)scanRst.rssi;
373 
374         dbg("wifiDevice:(%3d dBm) CH=%3d BSSID=%02x:%02x:%02x:%02x:%02x:%02x SSID=%s,group_cipher = %d,akm = %d\n",
375             (int8_t)scanRst.rssi, phy_freq_to_channel(scanRst.chan->band, scanRst.chan->freq),
376             ((uint8_t *)scanRst.bssid.array)[0], ((uint8_t *)scanRst.bssid.array)[1],
377             ((uint8_t *)scanRst.bssid.array)[2], ((uint8_t *)scanRst.bssid.array)[3],
378             ((uint8_t *)scanRst.bssid.array)[4], ((uint8_t *)scanRst.bssid.array)[5],
379             (char *)scanRst.ssid.array, scanRst.group_cipher, scanRst.akm);
380         rstIndex++;
381     }
382 
383     WIFI_CLOSE_LINK(g_wifiData.consoleCntrlLink);
384     *size = rstIndex;
385     /* close already,scan again */
386     g_wifiData.state = WIFI_ACTIVE;
387     WifiUnlock();
388     return WIFI_SUCCESS;
389 }
390 
AfterConnect(int networkId,WifiConnectDevice * device)391 static void AfterConnect(int networkId, WifiConnectDevice *device)
392 {
393     WifiConnectedInfo *info;
394     info = &g_wifiData.connectedInfo;
395     memset(info, 0, sizeof(WifiConnectedInfo));
396     info->networkId = networkId;
397     memcpy(info->linkinfo.bssid, device->devConf.bssid, WIFI_MAC_LEN);
398     strcpy(info->linkinfo.ssid, device->devConf.ssid);
399     info->linkinfo.rssi = data_pkt_rssi_get();
400     DoStaConnectCallBack(WIFI_STATE_AVAILABLE, &info->linkinfo);
401 }
402 
StaConfig(WifiConnectDevice * device,struct fhost_vif_sta_cfg * staCfg)403 static int32_t StaConfig(WifiConnectDevice *device, struct fhost_vif_sta_cfg *staCfg)
404 {
405     memset(staCfg, 0, sizeof(struct fhost_vif_sta_cfg));
406     //timeout 30s
407     staCfg->timeout_ms = 30000;
408     if (memcpy_s(staCfg->ssid.array, MAC_SSID_LEN, device->devConf.ssid, strlen(device->devConf.ssid)) != EOK) {
409         dbg("set ssid fail!\rn");
410         return ERROR_WIFI_UNKNOWN;
411     }
412     staCfg->ssid.length = strlen(device->devConf.ssid);
413 
414     if (strcpy_s(staCfg->key, MAX_WIFI_KEY_LEN, device->devConf.preSharedKey) != EOK) {
415         return ERROR_WIFI_UNKNOWN;
416     }
417 
418     SecureHm2Chipsea(device->devConf.securityType, &staCfg->akm);
419     return WIFI_SUCCESS;
420 }
421 
ConnectTo(int networkId)422 WifiErrorCode ConnectTo(int networkId)
423 {
424     int32_t ret;
425     net_if_t *net_if = NULL;
426     WifiConnectDevice *device;
427     struct fhost_vif_sta_cfg staCfg;
428 
429     if (networkId >= WIFI_MAX_CONFIG_SIZE) {
430         goto connect_err;
431     }
432 
433     dbg("Connect to %d\r\n", networkId);
434     if (WifiCreateLock() != WIFI_SUCCESS) {
435         return ERROR_WIFI_NOT_AVAILABLE;
436     }
437 
438     if ((g_wifiData.state == WIFI_STA_NOT_ACTIVE) || (g_wifiData.state == WIFI_CONNECT)) {
439         dbg("device state err %d\r\n", g_wifiData.state);
440         goto connect_err;
441     }
442 
443     device = &g_wifiData.deviceTab[networkId];
444     if (device->used != 1) {
445         dbg("network id not used\r\n");
446         goto connect_err;
447     }
448 
449     ret = SetStaVif();
450     WIFI_CLOSE_LINK(g_wifiData.consoleCntrlLink);
451     if (ret != WIFI_SUCCESS) {
452         dbg("SetStaVif err\r\n");
453         goto connect_err;
454     }
455 
456     ret = StaConfig(device, &staCfg);
457     if (ret != WIFI_SUCCESS) {
458         dbg("StaConfig err\r\n");
459         goto connect_err;
460     }
461 
462     dbg("connect param ssid = %s,len = %d, key = %s,akm = %d\r\n",
463         staCfg.ssid.array,staCfg.ssid.length,staCfg.key,staCfg.akm);
464     if (fhost_sta_cfg(DEFAULT_STA_VIF, &staCfg)) {
465         dbg("enable wifi device err!\r\n");
466         goto connect_err;
467     }
468 
469     net_if = net_if_find_from_wifi_idx(DEFAULT_STA_VIF);
470     if (net_if == NULL) {
471         dbg("[CS] net_if_find_from_wifi_idx fail\r\n");
472         goto connect_err;
473     }
474 
475     if (wlan_dhcp(net_if)) {
476         wlan_disconnect_sta(DEFAULT_STA_VIF);
477         dbg("[CS] dhcp fail\r\n");
478         goto connect_err;
479     }
480 
481     net_if_set_default(net_if);
482 
483     g_wifiData.state = WIFI_CONNECT;
484 
485     AfterConnect(networkId, device);
486     WifiUnlock();
487     return WIFI_SUCCESS;
488 connect_err:
489     WifiUnlock();
490     DoStaConnectCallBack(WIFI_STATE_NOT_AVAILABLE, NULL);
491     return ERROR_WIFI_UNKNOWN;
492 }
493 
Disconnect(void)494 WifiErrorCode Disconnect(void)
495 {
496     dbg("Disconnect!\r\n");
497     if (WifiCreateLock() != WIFI_SUCCESS) {
498         return ERROR_WIFI_NOT_AVAILABLE;
499     }
500 
501     if (g_wifiData.state != WIFI_CONNECT) {
502         WifiUnlock();
503         return ERROR_WIFI_UNKNOWN;
504     }
505 
506     wlan_disconnect_sta(DEFAULT_STA_VIF);
507     (void)memset(&g_wifiData.connectedInfo, 0, sizeof(WifiConnectedInfo));
508     g_wifiData.state = WIFI_ACTIVE;
509     WifiUnlock();
510     return WIFI_SUCCESS;
511 }
512 
GetLinkedInfo(WifiLinkedInfo * result)513 WifiErrorCode GetLinkedInfo(WifiLinkedInfo *result)
514 {
515     PARAM_CHECK(result);
516     result->rssi = data_pkt_rssi_get();
517     memcpy(result, &g_wifiData.connectedInfo.linkinfo, sizeof(WifiLinkedInfo));
518     return WIFI_SUCCESS;
519 }
520 
GetDeviceMacAddress(unsigned char * result)521 WifiErrorCode GetDeviceMacAddress(unsigned char *result)
522 {
523     PARAM_CHECK(result);
524     unsigned char *mac = get_mac_address();
525     if (mac != NULL) {
526         (void)memcpy(result, mac, WIFI_MAC_LEN);
527         return WIFI_SUCCESS;
528     }
529     return ERROR_WIFI_UNKNOWN;
530 }
531 
SetCountryCode(const char * countryCode)532 WifiErrorCode SetCountryCode(const char *countryCode)
533 {
534     PARAM_CHECK(countryCode);
535     return WIFI_SUCCESS;
536 }
537 
GetCountryCode(char * countryCode,unsigned int * len)538 WifiErrorCode GetCountryCode(char *countryCode, unsigned int *len)
539 {
540     PARAM_CHECK(countryCode);
541     PARAM_CHECK(len);
542     return WIFI_SUCCESS;
543 }
544 
EnableWifi(void)545 WifiErrorCode EnableWifi(void)
546 {
547     dbg("wifiDevice:start wifi device!\r\n");
548     if (WifiCreateLock() != WIFI_SUCCESS) {
549         return ERROR_WIFI_NOT_AVAILABLE;
550     }
551 
552     if (g_wifiData.state != WIFI_INACTIVE) {
553         WifiUnlock();
554         dbg("wifiDevice:wifi device busy!\r\n");
555         return ERROR_WIFI_BUSY;
556     }
557 
558     g_wifiData.eventCnt = 0;
559 
560     if (g_wifiData.wifiQueue == NULL) {
561         if (rtos_queue_create(sizeof(ChipseaWifiMsg), MAX_WIFI_EVENT_NUM, &g_wifiData.wifiQueue) != 0) {
562             WifiUnlock();
563             dbg("wifiDevice:rtos_queue_create err!\r\n");
564             return ERROR_WIFI_NOT_AVAILABLE;
565         }
566     }
567 
568     if (g_wifiData.wifiTask == NULL) {
569         if (rtos_task_create(WifiDeviceEntry, "wifi device", 0, 0x500, NULL, RTOS_TASK_PRIORITY(2),
570             &g_wifiData.wifiTask)!= 0) {
571             dbg("wifiDevice:rtos_task_create err!\r\n");
572             WifiUnlock();
573             return ERROR_WIFI_NOT_AVAILABLE;
574         }
575     }
576 
577     g_wifiData.state = WIFI_ACTIVE;
578 
579     WifiUnlock();
580     return WIFI_SUCCESS;
581 }
582 
DisableWifi(void)583 WifiErrorCode DisableWifi(void)
584 {
585     dbg("Disable wifi!\r\n");
586     if (WifiCreateLock() != WIFI_SUCCESS) {
587         return ERROR_WIFI_NOT_AVAILABLE;
588     }
589 
590     if (g_wifiData.state == WIFI_INACTIVE) {
591         WifiUnlock();
592         return ERROR_WIFI_NOT_STARTED;
593     }
594 
595     if (g_wifiData.state == WIFI_CONNECT) {
596         wlan_disconnect_sta(DEFAULT_STA_VIF);
597         (void)memset(&g_wifiData.connectedInfo, 0, sizeof(WifiConnectedInfo));
598     }
599 
600     g_wifiData.state = WIFI_INACTIVE;
601     WifiUnlock();
602     return WIFI_SUCCESS;
603 }
604 
IsWifiActive(void)605 int IsWifiActive(void)
606 {
607     return (g_wifiData.state == WIFI_ACTIVE) ? WIFI_ACTIVE : WIFI_STA_NOT_ACTIVE;
608 }
609 
WifiDeviceEntry(void * arg)610 void WifiDeviceEntry(void *arg)
611 {
612     ChipseaWifiMsg wifiMsg;
613 
614     while (1) {
615         if (rtos_queue_read(g_wifiData.wifiQueue, &wifiMsg, osWaitForever, false) == 0) {
616             switch (wifiMsg.eventId) {
617                 case WIFI_START_SCAN:
618                     DoScan();
619                     break;
620                 case WIFI_START_ADVANCE_SCAN:
621                     DoAdvanceScan((WifiScanParams *)wifiMsg.payLoad);
622                     break;
623                 default :
624                     dbg("wifiDevice:not support cmd %d\r\n", wifiMsg.eventId);
625                     break;
626             }
627         }
628 
629         rtos_task_suspend(1);
630     }
631 }
632