• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2022 ASR Microelectronics (Shanghai) Co., 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 
16 #include "station_info.h"
17 #include "wifi_device.h"
18 #include "wifi_device_config.h"
19 #include "wifi_error_code.h"
20 #include "wifi_event.h"
21 #include "wifi_hotspot.h"
22 #include "wifi_hotspot_config.h"
23 #include "wifi_linked_info.h"
24 #include "wifi_scan_info.h"
25 #include "lega_wlan_api_aos.h"
26 #ifdef ALIOS_SUPPORT
27 #include "lega_rhino.h"
28 #else
29 #include "lega_rtos.h"
30 #endif
31 #include "duet_flash_kv.h"
32 #include "lwip/inet.h"
33 #ifdef HARMONYOS_TEMP
34 // uint8_t wifi_ready = 0;
35 // uint8_t ble_open = 0;
36 // uint16_t ble_con_interval = 50;
37 #endif
38 
39 typedef enum {
40     STA_MODE_E = 0x01,
41     SAP_MODE_E,
42     SNIFFER_MODE_E,
43     TEST_MODE_E,
44 } current_iftype_e;
45 extern uint32_t current_iftype;
46 
WifiFreqToChannel(int freq)47 int WifiFreqToChannel(int freq)
48 {
49     int channel = 0;
50 
51     // Check if frequency is in the expected range
52     if ((freq < 2412) || (freq > 2484)) {
53         return channel;
54     }
55 
56     // Compute the channel number
57     if (freq == 2484) {
58         channel = 14;
59     } else {
60         channel = (freq - 2407) / 5;
61     }
62 
63     return channel;
64 }
65 
EnableWifi(void)66 WifiErrorCode EnableWifi(void)
67 {
68     int ret;
69 
70     printf("%s\r\n", __func__);
71 
72     if (current_iftype != 0xFF) {
73         return ERROR_WIFI_BUSY;
74     }
75 
76     lega_wlan_init_type_t init_param = {0};
77 
78     init_param.wifi_mode = STA;
79 
80     ret = lega_wlan_open(&init_param);
81     if (ret == 0) {
82         return WIFI_SUCCESS;
83     } else {
84         return ERROR_WIFI_UNKNOWN;
85     }
86 }
87 
lalala_test(void)88 void lalala_test(void)
89 {
90     lega_wlan_init_type_t init_param = {0};
91     char *ssid = "asr-guest";
92     char *pwd = "asr123456";
93     memcpy(init_param.wifi_ssid, ssid, sizeof(init_param.wifi_ssid));
94     memcpy(init_param.wifi_key,  pwd, sizeof(init_param.wifi_key));
95     init_param.wifi_mode = STA;
96     lega_wlan_open(&init_param);
97     (void)memset_s(&init_param, sizeof(init_param), 0, sizeof(init_param));
98 }
99 
lalala_test2(void)100 void lalala_test2(void)
101 {
102     lega_wlan_init_type_t init_param = {0};
103     char *ssid = "LALALALALA";
104     char *pwd = "12345678";
105     memcpy(init_param.wifi_ssid, ssid, sizeof(init_param.wifi_ssid));
106     memcpy(init_param.wifi_key,  pwd, sizeof(init_param.wifi_key));
107     init_param.wifi_mode = SOFTAP;
108     lega_wlan_open(&init_param);
109     (void)memset_s(&init_param, sizeof(init_param), 0, sizeof(init_param));
110 }
111 
112 extern lega_semaphore_t lega_wlan_vendor_close_sta_semaphore;
DisableWifi(void)113 WifiErrorCode DisableWifi(void)
114 {
115     int ret;
116 
117     printf("%s\r\n", __func__);
118 
119     if (current_iftype != STA_MODE_E) {
120         return ERROR_WIFI_NOT_STARTED;
121     }
122 
123     lega_rtos_init_semaphore(&lega_wlan_vendor_close_sta_semaphore, 0);
124     ret = lega_wlan_close();
125     if (ret) {
126         printf("%s fail\r\n", __func__);
127         lega_rtos_deinit_semaphore(&lega_wlan_vendor_close_sta_semaphore);
128         lega_wlan_vendor_close_sta_semaphore = 0;
129         return ERROR_WIFI_UNKNOWN;
130     }
131     lega_rtos_get_semaphore(&lega_wlan_vendor_close_sta_semaphore, LEGA_NEVER_TIMEOUT);
132     lega_rtos_deinit_semaphore(&lega_wlan_vendor_close_sta_semaphore);
133     lega_wlan_vendor_close_sta_semaphore = 0;
134 
135     return WIFI_SUCCESS;
136 }
137 
IsWifiActive(void)138 int IsWifiActive(void)
139 {
140     printf("%s\r\n", __func__);
141 
142     if (current_iftype == STA_MODE_E) {
143         return WIFI_STATE_AVAILABLE;
144     } else {
145         return WIFI_STATE_NOT_AVAILABLE;
146     }
147 }
148 
149 typedef struct {
150     uint8_t is_scan_adv;
151     char ap_num;       /* *< The number of access points found in scanning. */
152     struct {
153         char    ssid[32 + 1]; /* ssid max len:32. +1 is for '\0'. when ssidlen is 32  */
154         char    ap_power;     /* *< Signal strength, min:0, max:100. */
155         char    bssid[6];     /* The BSSID of an access point. */
156         char    channel;      /* The RF frequency, 1-13 */
157         uint8_t security;     /* Security type, @ref wlan_sec_type_t */
158     } ap_list[WIFI_SCAN_HOTSPOT_LIMIT];
159 } lega_wlan_scan_result_store_t;
160 
161 extern lega_semaphore_t lega_wlan_vendor_scan_semaphore;
162 extern lega_wlan_cb_scan_compeleted lega_wlan_vendor_scan_comp_callback;
163 lega_wlan_scan_result_store_t *lega_wlan_scan_result_store_ptr = NULL;
ScanCompCallback(lega_wlan_scan_result_t * scan_result)164 void ScanCompCallback(lega_wlan_scan_result_t *scan_result)
165 {
166     int i;
167 
168     printf("%s\r\n", __func__);
169 
170     if (lega_wlan_scan_result_store_ptr == NULL) {
171         printf("%s fail\r\n", __func__);
172         return;
173     }
174 
175     lega_wlan_scan_result_store_ptr->is_scan_adv = scan_result->is_scan_adv;
176     for (i = 0; (i < scan_result->ap_num) && (i < WIFI_SCAN_HOTSPOT_LIMIT); i++) {
177         memcpy(&(lega_wlan_scan_result_store_ptr->ap_list[i]), scan_result->ap_list,
178                sizeof(lega_wlan_scan_result_store_ptr->ap_list[i]));
179     }
180     lega_wlan_scan_result_store_ptr->ap_num = i;
181 }
182 
Scan(void)183 WifiErrorCode Scan(void)
184 {
185     int ret;
186 
187     printf("%s\r\n", __func__);
188 
189     if (current_iftype != STA_MODE_E) {
190         return ERROR_WIFI_IFACE_INVALID;
191     }
192 
193     if (lega_wlan_scan_result_store_ptr == NULL) {
194         lega_wlan_scan_result_store_ptr = lega_rtos_malloc(sizeof(lega_wlan_scan_result_store_t));
195     }
196 
197     lega_wlan_vendor_scan_comp_callback = ScanCompCallback;
198 
199     lega_rtos_init_semaphore(&lega_wlan_vendor_scan_semaphore, 0);
200     ret = lega_wlan_start_scan_adv();
201     if (ret) {
202         printf("%s fail\r\n", __func__);
203         lega_rtos_deinit_semaphore(&lega_wlan_vendor_scan_semaphore);
204         lega_wlan_vendor_scan_semaphore = 0;
205         return ERROR_WIFI_UNKNOWN;
206     }
207     lega_rtos_get_semaphore(&lega_wlan_vendor_scan_semaphore, LEGA_NEVER_TIMEOUT);
208     lega_rtos_deinit_semaphore(&lega_wlan_vendor_scan_semaphore);
209     lega_wlan_vendor_scan_semaphore = 0;
210 
211     return WIFI_SUCCESS;
212 }
213 
AdvanceScan(WifiScanParams * params)214 WifiErrorCode AdvanceScan(WifiScanParams *params)
215 {
216     int ret;
217     int channel = 0;
218     char *ssid_ptr = 0;
219     char *bssid_ptr = 0;
220     char bssid_null[WIFI_MAC_LEN] = {0};
221 
222     printf("%s\r\n", __func__);
223 
224     if (current_iftype != STA_MODE_E) {
225         return ERROR_WIFI_IFACE_INVALID;
226     }
227 
228     if (params == NULL) {
229         return ERROR_WIFI_UNKNOWN;
230     }
231 
232     if ((params->scanType < WIFI_FREQ_SCAN) || (params->scanType > WIFI_BAND_SCAN)) {
233         // return WIFI_SUCCESS;
234         // adapter for xts
235         params->scanType = WIFI_BAND_SCAN;
236     }
237 
238     if (params->scanType == WIFI_FREQ_SCAN) {
239         channel = WifiFreqToChannel(params->freqs);
240         if (channel == 0) {
241             return ERROR_WIFI_UNKNOWN;
242         }
243     }
244 
245     if (params->scanType == WIFI_SSID_SCAN) {
246         if (params->ssidLen == 0) {
247             return ERROR_WIFI_UNKNOWN;
248         }
249         ssid_ptr = lega_rtos_malloc(WIFI_MAX_SSID_LEN);
250         if (ssid_ptr) {
251             memcpy(ssid_ptr, params->ssid, WIFI_MAX_SSID_LEN);
252         }
253     }
254 
255     if (params->scanType == WIFI_BSSID_SCAN) {
256         if (memcmp(bssid_null, params->bssid, 6) == 0) {
257             return ERROR_WIFI_UNKNOWN;
258         }
259         bssid_ptr = lega_rtos_malloc(WIFI_MAC_LEN);
260         if (bssid_ptr) {
261             memcpy(bssid_ptr, params->bssid, WIFI_MAC_LEN);
262         }
263     }
264 
265     if (lega_wlan_scan_result_store_ptr == NULL) {
266         lega_wlan_scan_result_store_ptr = lega_rtos_malloc(sizeof(lega_wlan_scan_result_store_t));
267     }
268 
269     lega_wlan_vendor_scan_comp_callback = ScanCompCallback;
270 
271     lega_rtos_init_semaphore(&lega_wlan_vendor_scan_semaphore, 0);
272 
273     ret = lega_wlan_start_scan_detail(ssid_ptr, channel, bssid_ptr);
274 
275     if (ssid_ptr) {
276         lega_rtos_free(ssid_ptr);
277     }
278 
279     if (bssid_ptr) {
280         lega_rtos_free(bssid_ptr);
281     }
282 
283     if (ret) {
284         printf("%s fail\r\n", __func__);
285         lega_rtos_deinit_semaphore(&lega_wlan_vendor_scan_semaphore);
286         lega_wlan_vendor_scan_semaphore = 0;
287         return ERROR_WIFI_UNKNOWN;
288     }
289     lega_rtos_get_semaphore(&lega_wlan_vendor_scan_semaphore, LEGA_NEVER_TIMEOUT);
290     lega_rtos_deinit_semaphore(&lega_wlan_vendor_scan_semaphore);
291     lega_wlan_vendor_scan_semaphore = 0;
292 
293     return WIFI_SUCCESS;
294 }
295 
GetScanInfoList(WifiScanInfo * result,unsigned int * size)296 WifiErrorCode GetScanInfoList(WifiScanInfo *result, unsigned int *size)
297 {
298     int i;
299 
300     printf("%s\r\n", __func__);
301 
302     if (lega_wlan_scan_result_store_ptr == NULL) {
303         *size = 0;
304         return WIFI_SUCCESS;
305     }
306 
307     for (i = 0; i < lega_wlan_scan_result_store_ptr->ap_num; i++) {
308         result->band = 0;
309         result->frequency = lega_wlan_scan_result_store_ptr->ap_list[i].channel;
310         result->rssi = 0;
311         result->securityType = lega_wlan_scan_result_store_ptr->ap_list[i].security;
312         memcpy(result->ssid, lega_wlan_scan_result_store_ptr->ap_list[i].ssid, WIFI_MAX_SSID_LEN);
313         memcpy(result->bssid, lega_wlan_scan_result_store_ptr->ap_list[i].bssid, WIFI_MAC_LEN);
314     }
315     *size = i;
316 
317     lega_rtos_free(lega_wlan_scan_result_store_ptr);
318     lega_wlan_scan_result_store_ptr = NULL;
319 
320     return WIFI_SUCCESS;
321 }
322 
323 uint8_t lega_wifi_device_config_flag_array[WIFI_MAX_CONFIG_SIZE] = {0};
324 WifiDeviceConfig lega_wifi_device_config_array[WIFI_MAX_CONFIG_SIZE] = {0};
325 // "wifi_device_config_flag_array"  "wifi_device_config_array"
AddDeviceConfig(const WifiDeviceConfig * config,int * result)326 WifiErrorCode AddDeviceConfig(const WifiDeviceConfig *config, int *result)
327 {
328     int i;
329     int32_t len;
330 
331     printf("%s\r\n", __func__);
332 
333     len = sizeof(WifiDeviceConfig) * WIFI_MAX_CONFIG_SIZE;
334     duet_flash_kv_get("wifi_device_config_array", lega_wifi_device_config_array, &len);
335     len = WIFI_MAX_CONFIG_SIZE;
336     duet_flash_kv_get("wifi_device_config_flag_array", lega_wifi_device_config_flag_array, &len);
337 
338     for (i = 0; i < WIFI_MAX_CONFIG_SIZE; i++) {
339         if (lega_wifi_device_config_flag_array[i] == 0) {
340             break;
341         }
342     }
343 
344     if (i >= WIFI_MAX_CONFIG_SIZE) {
345         printf("%s ERROR_WIFI_BUSY\r\n", __func__);
346         return ERROR_WIFI_BUSY;
347     }
348 
349     lega_wifi_device_config_flag_array[i] = 1;
350     memcpy(&lega_wifi_device_config_array[i], config, sizeof(WifiDeviceConfig));
351     // netID == 0, means invalid
352     lega_wifi_device_config_array[i].netId = i + 1;;
353     *result = i + 1;
354 
355     duet_flash_kv_set("wifi_device_config_array", lega_wifi_device_config_array,
356                       sizeof(WifiDeviceConfig)*WIFI_MAX_CONFIG_SIZE, 1);
357     duet_flash_kv_set("wifi_device_config_flag_array", lega_wifi_device_config_flag_array, WIFI_MAX_CONFIG_SIZE, 1);
358 
359     return WIFI_SUCCESS;
360 }
361 
GetDeviceConfigs(WifiDeviceConfig * result,unsigned int * size)362 WifiErrorCode GetDeviceConfigs(WifiDeviceConfig *result, unsigned int *size)
363 {
364     int i;
365     int32_t len;
366 
367     printf("%s %d\r\n", __func__, WIFI_MAX_CONFIG_SIZE);
368 
369     len = sizeof(WifiDeviceConfig) * WIFI_MAX_CONFIG_SIZE;
370     duet_flash_kv_get("wifi_device_config_array", lega_wifi_device_config_array, &len);
371     len = WIFI_MAX_CONFIG_SIZE;
372     duet_flash_kv_get("wifi_device_config_flag_array", lega_wifi_device_config_flag_array, &len);
373 
374     for (i = 0; i < WIFI_MAX_CONFIG_SIZE; i++) {
375         printf("%s flag: %d\r\n", __func__, lega_wifi_device_config_flag_array[i]);
376         if (lega_wifi_device_config_flag_array[i]) {
377             break;
378         }
379     }
380 
381     if (i >= WIFI_MAX_CONFIG_SIZE) {
382         printf("%s not found\r\n", __func__);
383         memset(result, 0, sizeof(WifiDeviceConfig)*WIFI_MAX_CONFIG_SIZE);
384         return ERROR_WIFI_NOT_AVAILABLE;
385     }
386 
387     memcpy(result, lega_wifi_device_config_array, sizeof(WifiDeviceConfig)*WIFI_MAX_CONFIG_SIZE);
388     *size = WIFI_MAX_CONFIG_SIZE;
389 
390     return WIFI_SUCCESS;
391 }
392 
RemoveDevice(int networkId)393 WifiErrorCode RemoveDevice(int networkId)
394 {
395     printf("%s %d\r\n", __func__, networkId);
396 
397     if ((networkId <= 0) || (networkId > WIFI_MAX_CONFIG_SIZE)) {
398         printf("%s ERROR_WIFI_NOT_AVAILABLE\r\n", __func__);
399         return ERROR_WIFI_NOT_AVAILABLE;
400     }
401 
402     lega_wifi_device_config_flag_array[networkId - 1] = 0;
403     memset(&lega_wifi_device_config_array[networkId - 1], 0, sizeof(WifiDeviceConfig));
404 
405     duet_flash_kv_set("wifi_device_config_array", lega_wifi_device_config_array,
406                       sizeof(WifiDeviceConfig)*WIFI_MAX_CONFIG_SIZE, 1);
407     duet_flash_kv_set("wifi_device_config_flag_array", lega_wifi_device_config_flag_array, WIFI_MAX_CONFIG_SIZE, 1);
408 
409     return WIFI_SUCCESS;
410 }
411 
lalala_sta_device_config_set(void)412 void lalala_sta_device_config_set(void)
413 {
414     WifiDeviceConfig config;
415     int result = 1;
416     memcpy(config.ssid, "AP2311", sizeof("AP2311"));
417     memcpy(config.preSharedKey, "12345678", sizeof("12345678"));
418     RemoveDevice(result);
419     AddDeviceConfig(&config, &result);
420     RemoveDevice(result);
421 }
422 
ConnectTo(int networkId)423 WifiErrorCode ConnectTo(int networkId)
424 {
425     lega_wlan_init_type_t init_param = {0};
426     int ret;
427 
428     printf("%s\r\n", __func__);
429 
430     if (current_iftype != STA_MODE_E) {
431         return ERROR_WIFI_NOT_STARTED;
432     }
433 
434     if ((networkId <= 0) || (networkId > WIFI_MAX_CONFIG_SIZE)) {
435         return ERROR_WIFI_NOT_AVAILABLE;
436     }
437 
438     if (lega_wifi_device_config_flag_array[networkId - 1]) {
439         init_param.wifi_mode = STA;
440         memcpy(init_param.wifi_ssid, lega_wifi_device_config_array[networkId - 1].ssid, sizeof(init_param.wifi_ssid));
441         memcpy(init_param.wifi_key,  lega_wifi_device_config_array[networkId - 1].preSharedKey, sizeof(init_param.wifi_key));
442         if ((lega_wifi_device_config_array[networkId - 1].freq >= 1)
443             && (lega_wifi_device_config_array[networkId - 1].freq <= 14)) {
444             init_param.channel = (char)lega_wifi_device_config_array[networkId - 1].freq;
445         }
446         ret = lega_wlan_open(&init_param);
447         if (ret == 0) {
448             return WIFI_SUCCESS;
449         } else {
450             return ERROR_WIFI_UNKNOWN;
451         }
452     } else {
453         return ERROR_WIFI_NOT_AVAILABLE;
454     }
455 }
456 
Disconnect(void)457 WifiErrorCode Disconnect(void)
458 {
459     int ret;
460 
461     printf("%s\r\n", __func__);
462 
463     if (current_iftype != STA_MODE_E) {
464         return ERROR_WIFI_NOT_STARTED;
465     }
466 
467     ret = lega_wlan_suspend_sta();
468     if (ret == 0) {
469         return WIFI_SUCCESS;
470     } else {
471         return ERROR_WIFI_UNKNOWN;
472     }
473 }
474 
GetLinkedInfo(WifiLinkedInfo * result)475 WifiErrorCode GetLinkedInfo(WifiLinkedInfo *result)
476 {
477     int ret;
478     lega_wlan_link_stat_t link_status;
479     lega_wlan_ip_stat_t *stat;
480     ip_addr_t ipaddr;
481 
482     printf("%s\r\n", __func__);
483 
484     if ((current_iftype != STA_MODE_E) || !result) {
485         return ERROR_WIFI_NOT_STARTED;
486     }
487 
488     ret = lega_wlan_get_link_status(&link_status);
489 
490     if (ret) {
491         return ERROR_WIFI_UNKNOWN;
492     }
493 
494     result->connState = link_status.is_connected;
495     memcpy(result->ssid, link_status.ssid, WIFI_MAX_SSID_LEN);
496     memcpy(result->bssid, link_status.bssid, WIFI_MAC_LEN);
497     result->rssi = link_status.wifi_strength;
498 
499     stat = lega_wlan_get_ip_status();
500 
501     if (stat == NULL) {
502         return ERROR_WIFI_UNKNOWN;
503     } else {
504         inet_aton(stat->ip, &ipaddr);
505         result->ipAddress = *(int *)(&ipaddr);
506         return WIFI_SUCCESS;
507     }
508 }
509 
GetDeviceMacAddress(unsigned char * result)510 WifiErrorCode GetDeviceMacAddress(unsigned char *result)
511 {
512     int ret;
513 
514     printf("%s\r\n", __func__);
515 
516     ret = lega_wlan_get_mac_address(result);
517     if (ret == 0) {
518         return WIFI_SUCCESS;
519     } else {
520         return ERROR_WIFI_UNKNOWN;
521     }
522 }
523 
524 static int is_wifi_connected = 0;
lega_wlan_get_connected_status(void)525 int lega_wlan_get_connected_status(void)
526 {
527     return is_wifi_connected;
528 }
529 
530 WifiEvent *lega_wifi_event_ptr = 0;
531 #ifdef AOS_COMP_MSMART
532 extern void wlan_service_event(lega_wlan_event_e event);
533 #else
wlan_service_event(lega_wlan_event_e event)534 void wlan_service_event(lega_wlan_event_e event)
535 {
536 
537 }
538 #endif
539 #ifdef CFG_MRFOTA_TEST
540 extern uint8_t rfota_wifi_test;
541 #endif
wifi_event_cb(lega_wlan_event_e evt,void * info)542 void wifi_event_cb(lega_wlan_event_e evt, void *info)
543 {
544     printf("w_evt_cb 0x%x\n", evt);
545 #ifdef CFG_MRFOTA_TEST
546     if (rfota_wifi_test == 1) {
547         if (evt == WLAN_EVENT_IP_GOT) {
548             lega_wlan_ip_stat_t *p_in_ip_stat = (lega_wlan_ip_stat_t *)info;
549             printf("Got ip: %s, gw: %s, mask: %s\n", p_in_ip_stat->ip, p_in_ip_stat->gate, p_in_ip_stat->mask);
550         }
551         return;
552     }
553 #endif
554     switch (evt) {
555         case WLAN_EVENT_SCAN_COMPLETED:
556             if ((lega_wifi_event_ptr == NULL) || (lega_wifi_event_ptr->OnWifiScanStateChanged == NULL)) {
557                 return;
558             }
559             lega_wifi_event_ptr->OnWifiScanStateChanged(1, 0);
560             break;
561         case WLAN_EVENT_ASSOCIATED:
562             is_wifi_connected = 0;
563             break;
564         case WLAN_EVENT_CONNECTED:
565             if ((lega_wifi_event_ptr == NULL) || (lega_wifi_event_ptr->OnWifiScanStateChanged == NULL)) {
566                 return;
567             }
568             lega_wifi_event_ptr->OnWifiScanStateChanged(1, 0);
569             break;
570         case WLAN_EVENT_IP_GOT: {
571             lega_wlan_ip_stat_t *p_in_ip_stat = (lega_wlan_ip_stat_t *)info;
572             printf("Got ip: %s, gw: %s, mask: %s\r\n", p_in_ip_stat->ip, p_in_ip_stat->gate, p_in_ip_stat->mask);
573             /* enable wifi power save */
574             lega_wlan_set_ps_mode(1);
575 #ifdef CFG_DATA_ELEM
576             lega_wlan_dataelem_start(0);
577 #endif
578             wlan_service_event(WLAN_EVENT_IP_GOT);
579             is_wifi_connected = 1;
580         }
581         break;
582         case WLAN_EVENT_DISCONNECTED:
583             if ((lega_wifi_event_ptr) && (lega_wifi_event_ptr->OnWifiScanStateChanged)) {
584                 lega_wifi_event_ptr->OnWifiScanStateChanged(0, 0);
585             }
586             wlan_service_event(WLAN_EVENT_DISCONNECTED);
587             is_wifi_connected = 0;
588             break;
589         case WLAN_EVENT_AP_UP:
590             if ((lega_wifi_event_ptr) && (lega_wifi_event_ptr->OnHotspotStateChanged)) {
591                 lega_wifi_event_ptr->OnHotspotStateChanged(1);
592             }
593             wlan_service_event(WLAN_EVENT_AP_UP);
594             break;
595         case WLAN_EVENT_AP_DOWN:
596             if ((lega_wifi_event_ptr) && (lega_wifi_event_ptr->OnHotspotStateChanged)) {
597                 lega_wifi_event_ptr->OnHotspotStateChanged(0);
598             }
599             wlan_service_event(WLAN_EVENT_AP_DOWN);
600             break;
601         case WLAN_EVENT_CONNECT_FAILED:
602             if ((lega_wifi_event_ptr) && (lega_wifi_event_ptr->OnWifiScanStateChanged)) {
603                 lega_wifi_event_ptr->OnWifiScanStateChanged(0, 0);
604             }
605             wlan_service_event(WLAN_EVENT_CONNECT_FAILED);
606             break;
607         case WLAN_EVENT_SCAN_FAILED:
608             if ((lega_wifi_event_ptr == NULL) || (lega_wifi_event_ptr->OnWifiScanStateChanged == NULL)) {
609                 return;
610             }
611             lega_wifi_event_ptr->OnWifiScanStateChanged(0, 0);
612             break;
613         case WLAN_EVENT_AP_PEER_UP: {
614             if ((lega_wifi_event_ptr == NULL) || (lega_wifi_event_ptr->OnHotspotStaJoin == NULL)) {
615                 return;
616             }
617             StationInfo sta_info = {0};
618             sta_info.disconnectedReason = 0;
619             memcpy(sta_info.macAddress, ((lega_wlan_client_addr_info_t *)info)->sta_mac_addr, WIFI_MAC_LEN);
620             sta_info.ipAddress = ((lega_wlan_client_addr_info_t *)info)->sta_ip_addr;
621             lega_wifi_event_ptr->OnHotspotStaJoin(&sta_info);
622         }
623         break;
624         case WLAN_EVENT_AP_PEER_DOWN: {
625             if ((lega_wifi_event_ptr == NULL) || (lega_wifi_event_ptr->OnHotspotStaLeave == NULL)) {
626                 return;
627             }
628             StationInfo sta_info = {0};
629             sta_info.disconnectedReason = 0;
630             memcpy(sta_info.macAddress, ((lega_wlan_client_addr_info_t *)info)->sta_mac_addr, WIFI_MAC_LEN);
631             sta_info.ipAddress = ((lega_wlan_client_addr_info_t *)info)->sta_ip_addr;
632             lega_wifi_event_ptr->OnHotspotStaLeave(&sta_info);
633         }
634         break;
635         default:
636             // lega_wifi_event_ptr->OnHotspotStaJoin
637             // lega_wifi_event_ptr->OnHotspotStaLeave
638             printf("WiFi HAL %s EVENT[%d] not implemeted yet!\r\n", __func__, evt);
639             break;
640     }
641 }
642 
RegisterWifiEvent(WifiEvent * event)643 WifiErrorCode RegisterWifiEvent(WifiEvent *event)
644 {
645     printf("%s\r\n", __func__);
646 
647     lega_wifi_event_ptr = event;
648 
649     return WIFI_SUCCESS;
650 }
651 
UnRegisterWifiEvent(const WifiEvent * event)652 WifiErrorCode UnRegisterWifiEvent(const WifiEvent *event)
653 {
654     printf("%s\r\n", __func__);
655 
656     if (lega_wifi_event_ptr == event) {
657         lega_wifi_event_ptr = 0;
658     } else {
659         printf("%s fail 0x%x 0x%x\r\n", __func__, (unsigned int)lega_wifi_event_ptr, (unsigned int)event);
660     }
661 
662     return WIFI_SUCCESS;
663 }
664 
665 HotspotConfig lega_hotspot_config = {0};
666 uint8_t lega_hotspot_config_flag = 0;
667 // "hotspot_config" "hotspot_config_flag"
EnableHotspot(void)668 WifiErrorCode EnableHotspot(void)
669 {
670     int ret;
671     lega_wlan_init_type_t init_param = {0};
672 
673     printf("%s\r\n", __func__);
674 
675     if ((current_iftype != 0xFF) || (lega_hotspot_config_flag == 0)) {
676         return ERROR_WIFI_BUSY;
677     }
678 
679     init_param.wifi_mode = SOFTAP;
680     memcpy(init_param.wifi_ssid, lega_hotspot_config.ssid, 32);
681     memcpy(init_param.wifi_key,  lega_hotspot_config.preSharedKey, 64);
682     init_param.channel = lega_hotspot_config.channelNum;
683     ret = lega_wlan_open(&init_param);
684     if (ret) {
685         return ERROR_WIFI_UNKNOWN;
686     } else {
687         return WIFI_SUCCESS;
688     }
689 }
690 
691 extern lega_semaphore_t lega_wlan_vendor_close_ap_semaphore;
DisableHotspot(void)692 WifiErrorCode DisableHotspot(void)
693 {
694     printf("%s\r\n", __func__);
695 
696     if (current_iftype != SAP_MODE_E) {
697         return ERROR_WIFI_NOT_STARTED;
698     }
699     lega_rtos_init_semaphore(&lega_wlan_vendor_close_ap_semaphore, 0);
700     lega_wlan_close();
701     if (lega_rtos_get_semaphore(&lega_wlan_vendor_close_ap_semaphore, LEGA_NEVER_TIMEOUT)) {
702         // MS_LOGI("%s timeout\n",__FUNCTION__);
703     }
704     lega_rtos_deinit_semaphore(&lega_wlan_vendor_close_ap_semaphore);
705     lega_wlan_vendor_close_ap_semaphore = 0;
706 
707     return WIFI_SUCCESS;
708 }
709 
SetHotspotConfig(const HotspotConfig * config)710 WifiErrorCode SetHotspotConfig(const HotspotConfig *config)
711 {
712     printf("%s\r\n", __func__);
713 
714     lega_hotspot_config_flag = 1;
715     lega_hotspot_config = *config;
716 
717     duet_flash_kv_set("hotspot_config", &lega_hotspot_config, sizeof(HotspotConfig), 1);
718     duet_flash_kv_set("hotspot_config_flag", &lega_hotspot_config_flag, 1, 1);
719     return WIFI_SUCCESS;
720 }
721 
GetHotspotConfig(HotspotConfig * result)722 WifiErrorCode GetHotspotConfig(HotspotConfig *result)
723 {
724     int32_t len;
725     printf("%s\r\n", __func__);
726 
727     len = sizeof(HotspotConfig);
728     duet_flash_kv_get("hotspot_config", &lega_hotspot_config, &len);
729     len = 1;
730     duet_flash_kv_get("hotspot_config_flag", &lega_hotspot_config_flag, &len);
731 
732     if (lega_hotspot_config_flag) {
733         lega_hotspot_config.band = HOTSPOT_BAND_TYPE_2G;
734         *result = lega_hotspot_config;
735         return WIFI_SUCCESS;
736     } else {
737         return ERROR_WIFI_UNKNOWN;
738     }
739 }
740 
741 static int last_set_band = 0;
SetBand(int band)742 WifiErrorCode SetBand(int band)
743 {
744     printf("%s\r\n", __func__);
745 
746     if (band != HOTSPOT_BAND_TYPE_2G) {
747         last_set_band = band;
748         return ERROR_WIFI_NOT_SUPPORTED;
749     }
750 
751     lega_hotspot_config.band = band;
752 
753     return WIFI_SUCCESS;
754 }
755 
GetBand(int * result)756 WifiErrorCode GetBand(int *result)
757 {
758     printf("%s\r\n", __func__);
759 
760     if (last_set_band != 0) {
761         *result = last_set_band;
762         last_set_band = 0;
763         return ERROR_WIFI_UNKNOWN;
764     }
765 
766     if (lega_hotspot_config.band == 0) {
767         return ERROR_WIFI_NOT_AVAILABLE;
768     }
769 
770     *result = HOTSPOT_BAND_TYPE_2G;
771 
772     return WIFI_SUCCESS;
773 }
774 
IsHotspotActive(void)775 int IsHotspotActive(void)
776 {
777     printf("%s\r\n", __func__);
778 
779     if (current_iftype == SAP_MODE_E) {
780         return WIFI_STATE_AVAILABLE;
781     } else {
782         return WIFI_STATE_NOT_AVAILABLE;
783     }
784 }
785 
GetStationList(StationInfo * result,unsigned int * size)786 WifiErrorCode GetStationList(StationInfo *result, unsigned int *size)
787 {
788     int i;
789     lega_wlan_ap_client_info_t client_info;
790 
791     printf("%s\r\n", __func__);
792 
793     lega_get_client_ip_mac(&client_info);
794 
795     printf("%s : %d\r\n", __func__, client_info.client_num);
796 
797     *size = client_info.client_num;
798     for (i = 0; i < client_info.client_num; i++) {
799         memcpy(result[i].macAddress, client_info.sta[i].sta_mac_addr, WIFI_MAC_LEN);
800         result[i].disconnectedReason = 0;
801     }
802 
803     return WIFI_STATE_NOT_AVAILABLE;
804 }
805 
806 #define RSSI_LEVEL_4_2_G (-65)
807 #define RSSI_LEVEL_3_2_G (-75)
808 #define RSSI_LEVEL_2_2_G (-82)
809 #define RSSI_LEVEL_1_2_G (-88)
810 
811 #define RSSI_LEVEL_4_5_G (-65)
812 #define RSSI_LEVEL_3_5_G (-72)
813 #define RSSI_LEVEL_2_5_G (-79)
814 #define RSSI_LEVEL_1_5_G (-85)
815 
GetSignalLevel(int rssi,int band)816 int GetSignalLevel(int rssi, int band)
817 {
818     printf("%s\r\n", __func__);
819 
820     if (band == HOTSPOT_BAND_TYPE_2G) {
821         if (rssi >= RSSI_LEVEL_4_2_G) {
822             return RSSI_LEVEL_4;
823         }
824         if (rssi >= RSSI_LEVEL_3_2_G) {
825             return RSSI_LEVEL_3;
826         }
827         if (rssi >= RSSI_LEVEL_2_2_G) {
828             return RSSI_LEVEL_2;
829         }
830         if (rssi >= RSSI_LEVEL_1_2_G) {
831             return RSSI_LEVEL_1;
832         }
833     }
834 
835     if (band == HOTSPOT_BAND_TYPE_5G) {
836         if (rssi >= RSSI_LEVEL_4_5_G) {
837             return RSSI_LEVEL_4;
838         }
839         if (rssi >= RSSI_LEVEL_3_5_G) {
840             return RSSI_LEVEL_3;
841         }
842         if (rssi >= RSSI_LEVEL_2_5_G) {
843             return RSSI_LEVEL_2;
844         }
845         if (rssi >= RSSI_LEVEL_1_5_G) {
846             return RSSI_LEVEL_1;
847         }
848     }
849 
850     return ERROR_WIFI_INVALID_ARGS;
851 }
852 
lega_wlan_get_ip_addr(char * ip,int len)853 int lega_wlan_get_ip_addr(char *ip, int len)
854 {
855     lega_wlan_ip_stat_t *stat;
856 
857     stat = lega_wlan_get_ip_status();
858 
859     if (stat == NULL) {
860         return -1;
861     } else {
862         memcpy(ip, stat->ip, len);
863         return 0;
864     }
865 }
866 
867 extern int lega_wlan_softap_deauth_peer(uint8_t *mac);
DisassociateSta(unsigned char * mac,int macLen)868 WifiErrorCode DisassociateSta(unsigned char *mac, int macLen)
869 {
870     lega_wlan_softap_deauth_peer(mac);
871     return WIFI_SUCCESS;
872 }
873 
AddTxPowerInfo(int power)874 WifiErrorCode AddTxPowerInfo(int power)
875 {
876     return ERROR_WIFI_UNKNOWN;
877 }
878 
879 /** @} */
880