1 #include <stdio.h>
2 #include <stdlib.h>
3 #include <string.h>
4 #include "aos/hal/wifi.h"
5 
6 #include <platform/platform_stdlib.h>
7 #include <wifi/wifi_conf.h>
8 #include <wifi/wifi_util.h>
9 #include <wifi/wifi_ind.h>
10 #include <lwip_netconf.h>
11 #include <osdep_service.h>
12 #include <uservice/uservice.h>
13 #include <uservice/eventid.h>
14 #include <devicevfs/devicevfs.h>
15 #include <lwip/inet.h>
16 
17 #if CONFIG_LWIP_LAYER
18 extern struct netif xnetif[NET_IF_NUM];
19 #endif
20 
21 monitor_data_cb_t g_promisc_data_callback = NULL;
22 monitor_data_cb_t g_mgnt_filter_callback = NULL;
23 wifi_promiscuous_cb_t g_promisc_user_callback = NULL;
24 static aos_sem_t scan_sem;
25 
26 int haas200_wifi_channel_list[64];
27 int haas200_wifi_channel_list_num = 0;
28 
haas200_wifi_init(netdev_t * dev)29 int haas200_wifi_init(netdev_t *dev)
30 {
31 #if CONFIG_LWIP_LAYER
32     LwIP_Init();
33 #endif
34     wifi_on(RTW_MODE_STA);
35     wifi_disable_powersave();
36 
37     if (0 != aos_sem_new(&scan_sem, 0))
38     {
39         return -1;
40     }
41     return 0;
42 }
43 
haas200_wifi_deinit(netdev_t * dev)44 int haas200_wifi_deinit(netdev_t *dev)
45 {
46     return wifi_off();
47 }
48 
haas200_wifi_set_mode(netdev_t * dev,wifi_mode_t mode)49 int haas200_wifi_set_mode(netdev_t *dev, wifi_mode_t mode)
50 {
51     return wifi_set_mode(mode);
52 }
53 
haas200_wifi_get_mode(netdev_t * dev,wifi_mode_t * devode)54 int haas200_wifi_get_mode(netdev_t *dev, wifi_mode_t *devode)
55 {
56     return wext_get_mode(WLAN0_NAME, (int *)devode);
57 }
58 
haas200_wifi_install_event_cb(netdev_t * dev,wifi_event_func * evt_cb)59 int haas200_wifi_install_event_cb(netdev_t *dev, wifi_event_func *evt_cb)
60 {
61     return 0;
62 }
63 
haas200_wifi_cancel_connect(netdev_t * dev)64 int haas200_wifi_cancel_connect(netdev_t *dev)
65 {
66     int ret = 0;
67     if((ret = wifi_disconnect()) < 0) {
68         printf("\n\rERROR: Operation failed!");
69         return ret;
70     }
71     return ret;
72 }
73 
haas200_wifi_set_mac_addr(netdev_t * dev,const uint8_t * devac)74 int haas200_wifi_set_mac_addr(netdev_t *dev, const uint8_t *devac)
75 {
76     return wifi_set_mac_address((char *)devac);
77 }
78 
haas200_wifi_get_mac_addr(netdev_t * dev,uint8_t * devac)79 int haas200_wifi_get_mac_addr(netdev_t *dev, uint8_t *devac)
80 {
81     char buf[32];
82     uint32_t mac[ETH_ALEN];
83     int i = 0;
84 
85     wifi_get_mac_address(buf);
86     sscanf(buf, "%02x:%02x:%02x:%02x:%02x:%02x", &mac[0], &mac[1], &mac[2], &mac[3], &mac[4], &mac[5]);
87 
88     for(i = 0; i < ETH_ALEN; i++)
89         devac[i] = mac[i] & 0xFF;
90 
91     return 0;
92 }
93 
haas200_wifi_set_lpm(netdev_t * dev,wifi_lpm_mode_t mode)94 int haas200_wifi_set_lpm(netdev_t *dev, wifi_lpm_mode_t mode)
95 {
96     wext_set_powersave_mode(mode);
97     return 0;
98 }
99 
haas200_wifi_get_lpm(netdev_t * dev,wifi_lpm_mode_t * mode)100 int haas200_wifi_get_lpm(netdev_t *dev, wifi_lpm_mode_t *mode)
101 {
102     wext_get_powersave_mode(mode);
103     return 0;
104 }
105 
haas200_wifi_notify_ip_state2drv(netdev_t * dev,wifi_ip_stat_t * out_net_para,wifi_mode_t wifi_type)106 int haas200_wifi_notify_ip_state2drv(netdev_t *dev, wifi_ip_stat_t *out_net_para, wifi_mode_t wifi_type)
107 {
108     ip4_addr_t ipaddr;
109     ip4_addr_t netmask;
110     ip4_addr_t gw;
111 
112     if (!out_net_para) {
113         printf("%s: invalid argument!\n", __func__);
114         return -1;
115     }
116 
117     if (wifi_type == RTW_MODE_STA) {
118         printf("set ip: %s\n", out_net_para->ip);
119         printf("set mask: %s\n", out_net_para->mask);
120         printf("set gw: %s\n", out_net_para->gate);
121         inet_aton(out_net_para->ip, &ipaddr);
122         inet_aton(out_net_para->mask, &netmask);
123         inet_aton(out_net_para->gate, &gw);
124         netif_set_addr(&xnetif[0], &ipaddr, &netmask, &gw);
125         return 0;
126     }
127 
128     return -1;
129 }
130 
print_scan_result(rtw_scan_result_t * record)131 static void print_scan_result( rtw_scan_result_t* record )
132 {
133     printf("%s\t ", ( record->bss_type == RTW_BSS_TYPE_ADHOC ) ? "Adhoc" : "Infra");
134     printf(MAC_FMT, MAC_ARG(record->BSSID.octet));
135     printf(" %d\t ", record->signal_strength);
136     printf(" %d\t  ", record->channel);
137     printf(" %d\t  ", record->wps_type);
138     printf("%s\t\t ", ( record->security == RTW_SECURITY_OPEN ) ? "Open" :
139                                  ( record->security == RTW_SECURITY_WEP_PSK ) ? "WEP" :
140                                  ( record->security == RTW_SECURITY_WPA_TKIP_PSK ) ? "WPA TKIP" :
141                                  ( record->security == RTW_SECURITY_WPA_AES_PSK ) ? "WPA AES" :
142                                  ( record->security == RTW_SECURITY_WPA_MIXED_PSK ) ? "WPA Mixed" :
143                                  ( record->security == RTW_SECURITY_WPA2_AES_PSK ) ? "WPA2 AES" :
144                                  ( record->security == RTW_SECURITY_WPA2_TKIP_PSK ) ? "WPA2 TKIP" :
145                                  ( record->security == RTW_SECURITY_WPA2_MIXED_PSK ) ? "WPA2 Mixed" :
146                                  ( record->security == RTW_SECURITY_WPA_WPA2_TKIP_PSK) ? "WPA/WPA2 TKIP" :
147                                  ( record->security == RTW_SECURITY_WPA_WPA2_AES_PSK) ? "WPA/WPA2 AES" :
148                                  ( record->security == RTW_SECURITY_WPA_WPA2_MIXED_PSK) ? "WPA/WPA2 Mixed" :
149 #ifdef CONFIG_SAE_SUPPORT
150                                  ( record->security == RTW_SECURITY_WPA3_AES_PSK) ? "WP3-SAE AES" :
151 #endif
152                                  "Unknown" );
153 
154     printf(" %s ", record->SSID.val);
155     printf("\r\n");
156 }
157 
scan_result_handler(rtw_scan_handler_result_t * malloced_scan_result)158 static int scan_result_handler(rtw_scan_handler_result_t *malloced_scan_result)
159 {
160     static int ApNum = 0;
161     if (malloced_scan_result->scan_complete != RTW_TRUE){
162         rtw_scan_result_t* record = &malloced_scan_result->ap_details;
163         record->SSID.val[record->SSID.len] = 0; /* Ensure the SSID is null terminated */
164         printf("%d\t ", ++ApNum);
165         print_scan_result(record);
166         if(malloced_scan_result->user_data)
167             rtw_memcpy((void *)((char *)malloced_scan_result->user_data + (ApNum-1)*sizeof(rtw_scan_result_t)),(char *)record,sizeof(rtw_scan_result_t));
168     } else {
169         int i = 0;
170         static wifi_scan_result_t result = {0};
171 
172         if(ApNum == 0)
173             goto end;
174 
175         result.ap_list = (ap_list_t *)malloc(ApNum * sizeof(ap_list_t));
176         if(result.ap_list == NULL) {
177             printf("ERROR: malloc failed!\n");
178             goto end;
179         }
180         for(i = 0; i < ApNum; i++) {
181             rtw_scan_result_t *res =  malloced_scan_result->user_data + i*sizeof(rtw_scan_result_t);
182             rtw_memcpy((result.ap_list+i)->ssid,res->SSID.val,res->SSID.len);
183             rtw_memcpy((result.ap_list+i)->bssid,res->BSSID.octet,6);
184             (result.ap_list+i)->ap_power = res->signal_strength;
185             (result.ap_list+i)->channel = res->channel;
186             (result.ap_list+i)->sec_type = res->security;
187         }
188         result.ap_num = ApNum;
189 end:
190         ApNum = 0;
191         event_publish(EVENT_WIFI_SCAN_DONE,&result);
192         if(malloced_scan_result->user_data)
193             rtw_free(malloced_scan_result->user_data);
194     }
195     return 0;
196 }
197 
haas200_wifi_start_scan(netdev_t * dev,wifi_scan_config_t * config,bool block)198 int haas200_wifi_start_scan(netdev_t *dev, wifi_scan_config_t *config, bool block)
199 {
200     printf("%s\n",__func__);
201     char *scan_buf;
202 
203     scan_buf = rtw_malloc(65 * sizeof(rtw_scan_result_t));
204     if(!scan_buf) {
205         printf("ERROR: malloc failed!\n");
206         return -1;
207     }
208     rtw_memset(scan_buf, 0, 65*sizeof(rtw_scan_result_t));
209     if(wifi_scan_networks((rtw_scan_result_handler_t)scan_result_handler, scan_buf) != 0) {
210         printf("ERROR: wifi scan failed!\n");
211         return -1;
212     }
213 
214     return 0;
215 }
216 
217 /*find ap with "ssid" from scan list*/
_find_ap_from_scan_buf(char * buf,int buflen,char * target_ssid,void * user_data)218 static int _find_ap_from_scan_buf(char*buf, int buflen, char *target_ssid, void *user_data)
219 {
220     rtw_wifi_setting_t *pwifi = (rtw_wifi_setting_t *)user_data;
221     int plen = 0;
222 
223     while(plen < buflen){
224         u8 len, ssid_len, security_mode;
225         char *ssid;
226 
227         // len offset = 0
228         len = (int)*(buf + plen);
229         // check end
230         if(len == 0) break;
231         // ssid offset = 14
232         ssid_len = len - 14;
233         ssid = buf + plen + 14 ;
234         if((ssid_len == strlen(target_ssid))
235             && (!memcmp(ssid, target_ssid, ssid_len)))
236         {
237             //DBG_8195A("find a ap \r\n");
238             strncpy((char*)pwifi->ssid, target_ssid, 33);
239             // channel offset = 13
240             pwifi->channel = *(buf + plen + 13);
241             // security_mode offset = 11
242             security_mode = (u8)*(buf + plen + 11);
243             switch (security_mode)
244             {
245             case IW_ENCODE_ALG_NONE:
246                 pwifi->security_type = SECURITY_TYPE_NONE;
247                 break;
248             case IW_ENCODE_ALG_WEP:
249                 pwifi->security_type = SECURITY_TYPE_WEP;
250                 break;
251             case IW_ENCODE_ALG_CCMP:
252                 pwifi->security_type = SECURITY_TYPE_WPA2_AES;
253                 break;
254             }
255 
256             if(IW_ENCODE_ALG_WEP != security_mode)
257                 break;
258         }
259         plen += len;
260     }
261     return 0;
262 }
263 
264 /*get ap security mode from scan list*/
_get_ap_security_mode(IN char * ssid,OUT rtw_security_t * security_mode,OUT u8 * channel)265 static int _get_ap_security_mode(IN char * ssid, OUT rtw_security_t *security_mode, OUT u8 * channel)
266 {
267     rtw_wifi_setting_t wifi;
268     u32 scan_buflen = 1000;
269 
270     memset(&wifi, 0, sizeof(wifi));
271 
272     if(wifi_scan_networks_with_ssid(_find_ap_from_scan_buf, (void*)&wifi, scan_buflen, ssid, strlen(ssid)) != RTW_SUCCESS){
273         printf("ERROR: wifi scan failed!\n");
274         return 0;
275     }
276 
277     if(strcmp(wifi.ssid, ssid) == 0){
278         //printf("Wifi scanned !\n");
279         *security_mode = wifi.security_type;
280         *channel = wifi.channel;
281         return 1;
282     }
283 
284     return 0;
285 }
286 
haas200_security_type_to_rtw(wifi_sec_type_t sec_type)287 rtw_security_t haas200_security_type_to_rtw(wifi_sec_type_t sec_type)
288 {
289     rtw_security_t security_type = RTW_SECURITY_UNKNOWN;
290     switch(sec_type) {
291     case SECURITY_TYPE_NONE:
292         security_type = RTW_SECURITY_OPEN;
293         break;
294     case SECURITY_TYPE_WEP:
295         security_type = RTW_SECURITY_WEP_PSK;
296         break;
297     case SECURITY_TYPE_WPA_TKIP:
298         security_type = RTW_SECURITY_WPA_TKIP_PSK;
299         break;
300     case SECURITY_TYPE_WPA_AES:
301         security_type = RTW_SECURITY_WPA_AES_PSK;
302         break;
303     case SECURITY_TYPE_WPA2_TKIP:
304         security_type = RTW_SECURITY_WPA2_TKIP_PSK;
305         break;
306     case SECURITY_TYPE_WPA2_AES:
307         security_type = RTW_SECURITY_WPA2_AES_PSK;
308         break;
309     case SECURITY_TYPE_WPA2_MIXED:
310         security_type = RTW_SECURITY_WPA2_MIXED_PSK;
311         break;
312     case SECURITY_TYPE_AUTO:
313         security_type = RTW_SECURITY_WPA2_AES_PSK;
314         break;
315     }
316     return security_type;
317 }
318 
haas200_wifi_connect(netdev_t * dev,wifi_config_t * config)319 int haas200_wifi_connect(netdev_t *dev, wifi_config_t *config)
320 {
321     int ret = -1;
322     int key_id = -1;
323     uint8_t ssid[MAX_SSID_SIZE + 1] = {0};
324     uint8_t password[MAX_PASSWD_SIZE + 1] = {0};
325     uint32_t ssid_len, passwd_len;
326     uint8_t channel;
327     uint8_t pscan_config = (PSCAN_ENABLE | PSCAN_FAST_SURVEY);
328     rtw_security_t security_type = RTW_SECURITY_UNKNOWN;
329     char empty_bssid[ETH_ALEN] = {0}, assoc_by_bssid = 0;
330 
331     if(!config)
332         return -1;
333 
334     if(config->mode != RTW_MODE_STA)
335         return -1;
336 
337 #if CONFIG_AUTO_RECONNECT
338     wifi_set_autoreconnect(0);
339 #endif
340 
341     ssid_len = strlen(config->ssid);
342     passwd_len = strlen(config->password);
343     channel = config->sta_config.channel;
344     rtw_memcpy(ssid, config->ssid, ssid_len);
345 
346     if(passwd_len == 0)
347         security_type = RTW_SECURITY_OPEN;
348     else {
349         rtw_memcpy(password, config->password, passwd_len);
350         security_type = haas200_security_type_to_rtw(config->sta_config.sec_type);
351     }
352 
353     if(!rtw_memcmp(config->sta_config.bssid, empty_bssid, ETH_ALEN))
354         assoc_by_bssid = 1;
355 
356     if(wifi_set_pscan_chan(&channel, &pscan_config, 1) < 0) {
357         printf("ERROR: Set channel pscan failed!");
358         return -1;
359     }
360 
361     if (security_type == RTW_SECURITY_WEP_PSK || security_type == RTW_SECURITY_WEP_SHARED)
362         key_id = 0;
363 
364     printf("\nconnect to ssid:%s,password:%s\n",ssid,password);
365 
366     if(assoc_by_bssid) {
367         printf("\n\rjoining BSS by BSSID %02x:%02x:%02x:%02x:%02x:%02x ...\n\r", MAC_ARG(config->sta_config.bssid));
368         ret = wifi_connect_bssid(config->sta_config.bssid, ssid, security_type, password, ETH_ALEN, ssid_len, passwd_len, key_id, NULL);
369     } else {
370         ret = wifi_connect(ssid, security_type, password, ssid_len, passwd_len, key_id, NULL);
371     }
372 
373     if(ret != 0) {
374         if(ret == RTW_INVALID_KEY)
375             printf("\n\rERROR:Invalid Key\n");
376         printf("\n\rERROR: Can't connect to AP,ret:%d\n",ret);
377         return ret;
378     }
379     printf("\r\nWifi_connect successed\n\r");
380     event_publish(EVENT_WIFI_CONNECTED, &xnetif[0]);
381 
382     return 0;
383 }
384 
haas200_wifi_disconnect(netdev_t * dev)385 int haas200_wifi_disconnect(netdev_t *dev)
386 {
387     char essid[MAX_SSID_SIZE + 1];
388     int ret = 0, timeout = 20;
389 
390     if(wext_get_ssid(WLAN0_NAME, (unsigned char *) essid) < 0) {
391         printf("\n\rnot connected yet");
392         return ret;
393     }
394     printf("\n\rDeassociating AP...\n\r");
395     if((ret = wifi_disconnect()) < 0) {
396         printf("\n\rERROR: Operation failed!\n");
397         return ret;
398     }
399 
400     while(1) {
401         if(wext_get_ssid(WLAN0_NAME, (unsigned char *) essid) < 0) {
402             printf("\n\rWIFI disconnect succeed\n");
403             break;
404         }
405 
406         if(timeout == 0) {
407             printf("\n\rERROR: Deassoc timeout!\n");
408             ret = -1;
409             break;
410         }
411         aos_msleep(1);
412         timeout--;
413     }
414 #if CONFIG_LWIP_LAYER
415     LwIP_ReleaseIP(WLAN0_IDX);
416 #endif
417     event_publish(EVENT_WIFI_DISCONNECTED, NULL);
418     return ret;
419 }
420 
haas200_wifi_sta_get_link_status(netdev_t * dev,wifi_ap_record_t * ap_info)421 int haas200_wifi_sta_get_link_status(netdev_t *dev, wifi_ap_record_t *ap_info)
422 {
423     rtw_wifi_setting_t setting;
424     int rssi;
425     wifi_get_setting(WLAN0_NAME,&setting);
426     wifi_get_rssi(&rssi);
427 
428     ap_info->link_status = setting.mode;
429     rtw_memcpy(ap_info->ssid, setting.ssid, strlen(setting.ssid));
430     ap_info->channel = setting.channel;
431     ap_info->rssi = (int8_t)rssi;
432     ap_info->encryptmode = setting.security_type;
433 
434     switch(setting.security_type){
435     case RTW_SECURITY_OPEN:
436         ap_info->authmode = WIFI_AUTH_OPEN;
437         break;
438     case RTW_SECURITY_WEP_PSK:
439     case RTW_SECURITY_WEP_SHARED:
440         ap_info->authmode = WIFI_AUTH_WEP;
441         break;
442     case RTW_SECURITY_WPA_TKIP_PSK:
443     case RTW_SECURITY_WPA_AES_PSK:
444         ap_info->authmode = WIFI_AUTH_WPA_PSK;
445         break;
446     case RTW_SECURITY_WPA2_AES_PSK:
447     case RTW_SECURITY_WPA2_TKIP_PSK:
448     case RTW_SECURITY_WPA2_MIXED_PSK:
449         ap_info->authmode = WIFI_AUTH_WPA2_PSK;
450         break;
451     case RTW_SECURITY_WPA_WPA2_MIXED_PSK:
452         ap_info->authmode = WIFI_AUTH_MAX;
453         break;
454     default:
455         printf("unknow security mode!\n");
456         break;
457     }
458 
459     return 0;
460 }
461 
haas200_wifi_ap_get_sta_list(netdev_t * dev,wifi_sta_list_t * sta)462 int haas200_wifi_ap_get_sta_list(netdev_t *dev, wifi_sta_list_t *sta)
463 {
464     return wifi_get_associated_client_list(sta,sizeof(sta));
465 }
466 
467 // callback for promisc packets, like rtk_start_parse_packet in SC, wf, 1021
wifi_promisc_rx_cb(unsigned char * buf,unsigned int len,void * user_data)468 static void wifi_promisc_rx_cb(unsigned char* buf, unsigned int len, void* user_data)
469 {
470     wifi_link_info_t link_info;
471     wifi_promiscuous_pkt_t *p_buf = NULL;
472 
473     link_info.rssi = ((ieee80211_frame_info_t *)user_data)->rssi;
474     if(g_promisc_data_callback) {
475         g_promisc_data_callback(buf, len, &link_info);
476     }
477     if(g_promisc_user_callback) {
478        p_buf = aos_malloc(sizeof(wifi_promiscuous_pkt_t)+len);
479        memcpy(p_buf->payload, buf ,len);
480        p_buf->rx_ctrl.sig_len = len;
481        p_buf->rx_ctrl.rssi = ((ieee80211_frame_info_t *)user_data)->rssi;
482 
483        g_promisc_user_callback(p_buf, 0);
484        aos_free(p_buf);
485     }
486 }
487 
haas200_wifi_start_monitor(netdev_t * dev,wifi_promiscuous_cb_t cb)488 int haas200_wifi_start_monitor(netdev_t *dev, wifi_promiscuous_cb_t cb)
489 {
490     printf("%s\n",__func__);
491 #if CONFIG_AUTO_RECONNECT
492     wifi_set_autoreconnect(RTW_AUTORECONNECT_DISABLE);
493 #endif
494     wifi_init_packet_filter();
495     wifi_enter_promisc_mode();
496     wifi_set_promisc(RTW_PROMISC_ENABLE_2, wifi_promisc_rx_cb, 0);
497     g_promisc_user_callback = cb;
498     return 0;
499 }
500 
haas200_wifi_stop_monitor(netdev_t * dev)501 int haas200_wifi_stop_monitor(netdev_t *dev)
502 {
503     printf("%s\n",__func__);
504     g_promisc_data_callback = NULL;
505     wifi_set_promisc(RTW_PROMISC_DISABLE, NULL, 0);
506     return 0;
507 }
508 
haas200_wifi_send_80211_raw_frame(netdev_t * dev,void * buffer,uint16_t len)509 int haas200_wifi_send_80211_raw_frame(netdev_t *dev, void *buffer, uint16_t len)
510 {
511     int ret = -2;
512     len = len - 4; /* remove fcs */
513     ret = wext_send_mgnt(WLAN0_NAME, (char*)buffer, len, 1);
514     return ret;
515 }
516 
haas200_wifi_get_channel(netdev_t * dev,int * channel)517 int haas200_wifi_get_channel(netdev_t *dev, int *channel)
518 {
519     return wifi_get_channel(channel);
520 }
521 
haas200_wifi_set_channel(netdev_t * dev,int channel)522 int haas200_wifi_set_channel(netdev_t *dev, int channel)
523 {
524     return wifi_set_channel(channel);
525 }
526 
specified_scan_result_handler(char * buf,int buflen,char * target_ssid,void * user_data)527 static int specified_scan_result_handler(char*buf, int buflen, char *target_ssid, void *user_data)
528 {
529     ap_list_t *ap_list = (ap_list_t *)user_data;
530     int cmp_rssi = -120;
531     int plen = 0;
532 
533     while(plen < buflen) {
534         int len, rssi, ssid_len, security_mode, i;
535         char *ssid, *mac, exchange = 0;
536 
537         /* len offset = 0 */
538         len = (int)*(buf + plen);
539         /* check end */
540         if(len == 0) break;
541         /* mac */
542         mac = buf + plen + 1;
543         /* rssi */
544         rssi = *(int *)(buf + plen + 7);
545         if(rssi > cmp_rssi) {
546             cmp_rssi = rssi;
547             exchange = 1;
548             strncpy(ap_list->bssid,mac,6);
549             printf("mac = ");
550             for(i=0; i<6; i++)
551                 printf("%02x ", (u8)*(mac+i));
552             printf(",\t");
553             ap_list->ap_power = rssi;
554             printf("rssi = %d,\t",rssi);
555         }
556         /* ssid offset = 14 */
557         ssid_len = len - 14;
558         ssid = buf + plen + 14 ;
559 
560         if((ssid_len == strlen(target_ssid))
561             && (!memcmp(ssid, target_ssid, ssid_len)) && exchange)
562         {
563             strncpy((char*)ap_list->ssid, target_ssid, 33);
564             printf("ssid = ");
565             for(i = 0; i < ssid_len; i++)
566                 printf("%c", *(ssid+i));
567             printf(",\t");
568             /* channel offset = 13 */
569             ap_list->channel = *(buf + plen + 13);
570             printf("channel = %d,\t",ap_list->channel);
571             /* security_mode offset = 11 */
572             security_mode = (u8)*(buf + plen + 11);
573             switch(security_mode){
574                 case IW_ENCODE_ALG_NONE:
575                     ap_list->sec_type = SECURITY_TYPE_NONE;
576                     printf("sec = open\n\r");
577                     break;
578                 case IW_ENCODE_ALG_WEP:
579                     ap_list->sec_type = SECURITY_TYPE_WEP;
580                     printf("sec = wep\n\r");
581                     break;
582                 case IW_ENCODE_ALG_CCMP:
583                     ap_list->sec_type = SECURITY_TYPE_WPA2_AES;
584                     printf("sec = wpa/wpa2\n\r");
585                     break;
586             }
587         }
588         plen += len;
589     }
590     aos_sem_signal((aos_sem_t *)&scan_sem);
591     return 0;
592 }
593 
haas200_wifi_start_specified_scan(netdev_t * dev,ap_list_t * ap_list,int ap_num)594 int haas200_wifi_start_specified_scan(netdev_t *dev, ap_list_t *ap_list, int ap_num)
595 {
596     static wifi_scan_result_t result = {0};
597     int i, scan_buf_len = 500;
598     uint8_t *pscan_config = NULL;
599     uint8_t *channel_list = NULL;
600 
601     if(!ap_num || !ap_list) {
602         printf("ERROR: error params\n");
603         return -1;
604     }
605 
606     if(result.ap_list == NULL) {
607         result.ap_list = (ap_list_t *)malloc(ap_num * sizeof(ap_list_t));
608     }
609 
610     if(!result.ap_list){
611         printf("ERROR: malloc for ap list failed!\n");
612         return -1;
613     }
614 
615     if(haas200_wifi_channel_list && haas200_wifi_channel_list_num > 0) {
616         channel_list = (uint8_t *)malloc(haas200_wifi_channel_list_num);
617         if(!channel_list) {
618             printf("ERROR: malloc for channel list failed!\n");
619             return -1;
620         }
621         pscan_config = (uint8_t *)malloc(haas200_wifi_channel_list_num);
622         if(!pscan_config) {
623             printf("ERROR: malloc for pscan config failed!\n");
624             return -1;
625         }
626 
627         for(int i = 0; i < haas200_wifi_channel_list_num; i++) {
628             *(channel_list + i) = (uint8_t)haas200_wifi_channel_list[i];
629             *(pscan_config + i) = PSCAN_ENABLE;
630         }
631 
632         if(wifi_set_pscan_chan(channel_list, pscan_config, haas200_wifi_channel_list_num) < 0) {
633             printf("ERROR: set channel pscan failed!");
634             return -1;
635         }
636     }
637 
638     result.ap_num = ap_num;
639     for(i = 0; i< ap_num; i++) {
640         ap_list_t ap = *(ap_list + i);
641         if(wifi_scan_networks_with_ssid(specified_scan_result_handler, &ap, scan_buf_len, ap.ssid, strlen(ap.ssid)) != 0){
642             printf("ERROR: wifi scan failed\n\r");
643             return -1;
644         }
645         aos_sem_wait((aos_sem_t *)&scan_sem, AOS_WAIT_FOREVER);
646         *(ap_list + i) = ap;
647         *(result.ap_list + i) = ap;
648     }
649 
650 end:
651     event_publish(EVENT_WIFI_SCAN_DONE,&result);
652     return 0;
653 }
654 
haas200_wifi_register_monitor_cb(netdev_t * dev,monitor_data_cb_t fn)655 int haas200_wifi_register_monitor_cb(netdev_t *dev, monitor_data_cb_t fn)
656 {
657     if(!fn)
658         return -1;
659     g_promisc_data_callback = fn;
660     return 0;
661 }
662 
haas200_wifi_start_mgnt_monitor(netdev_t * dev)663 int haas200_wifi_start_mgnt_monitor(netdev_t *dev)
664 {
665      wifi_set_indicate_mgnt(2);
666     return 0;
667 }
668 
haas200_wifi_stop_mgnt_monitor(netdev_t * dev)669 int haas200_wifi_stop_mgnt_monitor(netdev_t *dev)
670 {
671     g_mgnt_filter_callback = NULL;
672     wifi_set_indicate_mgnt(0);
673     return 0;
674 }
675 
wifi_rx_mgnt_hdl(u8 * buf,int len,int flags,void * user_data)676 static void wifi_rx_mgnt_hdl(u8 *buf, int len, int flags, void *user_data)
677 {
678     wifi_link_info_t link_info;
679     /* only deal with Probe Request*/
680     if(g_mgnt_filter_callback && buf[0] == 0x40)
681         g_mgnt_filter_callback((u8*)buf, len, &link_info);
682 }
683 
haas200_wifi_register_mgnt_monitor_cb(netdev_t * dev,monitor_data_cb_t fn)684 int haas200_wifi_register_mgnt_monitor_cb(netdev_t *dev, monitor_data_cb_t fn)
685 {
686     if(!fn)
687         return -1;
688 
689     g_mgnt_filter_callback = fn;
690     wifi_reg_event_handler(WIFI_EVENT_RX_MGNT, (rtw_event_handler_t)wifi_rx_mgnt_hdl, NULL);
691     return 0;
692 }
693 
haas200_wifi_set_channellist(netdev_t * dev,wifi_channel_list_t * channellist)694 int haas200_wifi_set_channellist(netdev_t *dev, wifi_channel_list_t *channellist)
695 {
696 
697     memset(&haas200_wifi_channel_list, 0, 64*sizeof(int));
698     memcpy(&haas200_wifi_channel_list, channellist->channel_list, channellist->channel_num*sizeof(int));
699     haas200_wifi_channel_list_num = channellist->channel_num;
700     return 0;
701 }
702 
haas200_wifi_get_channellist(netdev_t * dev,wifi_channel_list_t * channellist)703 int haas200_wifi_get_channellist(netdev_t *dev, wifi_channel_list_t *channellist)
704 {
705     channellist->channel_list = &haas200_wifi_channel_list;
706     channellist->channel_num = haas200_wifi_channel_list_num;
707     return 0;
708 }
709 
710 static wifi_driver_t haas200_wifi_driver = {
711 
712     /** driver info */
713     .base.os                    = "rtos",
714     .base.type                  = "solo",
715     .base.partner               = "AliOS Things Team",
716     .base.app_net               = "rtsp+rtp+rtcp",
717     .base.project               = "HAAS200",
718     .base.cloud                 = "aliyun",
719 
720     /** common APIs */
721     .init                       = haas200_wifi_init,
722     .deinit                     = haas200_wifi_deinit,
723     .set_mode                   = haas200_wifi_set_mode,
724     .get_mode                   = haas200_wifi_get_mode,
725 
726     .install_event_cb            = haas200_wifi_install_event_cb,
727 
728     /** conf APIs */
729     .set_mac_addr               = haas200_wifi_set_mac_addr,
730     .get_mac_addr               = haas200_wifi_get_mac_addr,
731     .set_lpm                    = haas200_wifi_set_lpm,
732     .get_lpm                    = haas200_wifi_get_lpm,
733     .notify_ip_state2drv        = haas200_wifi_notify_ip_state2drv,
734 
735     /** connection APIs */
736     .start_scan                 = haas200_wifi_start_scan,
737     .start_specified_scan       = haas200_wifi_start_specified_scan,
738     .connect                    = haas200_wifi_connect,
739     .cancel_connect             = haas200_wifi_cancel_connect,
740     .disconnect                 = haas200_wifi_disconnect,
741     .sta_get_link_status        = haas200_wifi_sta_get_link_status,
742     .ap_get_sta_list            = haas200_wifi_ap_get_sta_list,
743 
744 
745     /** promiscuous APIs */
746     .start_monitor              = haas200_wifi_start_monitor,
747     .stop_monitor               = haas200_wifi_stop_monitor,
748     .send_80211_raw_frame       = haas200_wifi_send_80211_raw_frame,
749     .set_channel                = haas200_wifi_set_channel,
750     .get_channel                = haas200_wifi_get_channel,
751     .register_monitor_cb        = haas200_wifi_register_monitor_cb,
752     .start_mgnt_monitor         = haas200_wifi_start_mgnt_monitor,
753     .stop_mgnt_monitor          = haas200_wifi_stop_mgnt_monitor,
754     .register_mgnt_monitor_cb   = haas200_wifi_register_mgnt_monitor_cb,
755 
756     .set_smartcfg = NULL,
757 
758     .set_channellist = haas200_wifi_set_channellist,
759     .get_channellist = haas200_wifi_get_channellist,
760 };
761 
haas200_wifi_register(void)762 int haas200_wifi_register(void)
763 {
764     vfs_wifi_dev_register(&haas200_wifi_driver,0);
765     return 0;
766 }
767 
768 VFS_DRIVER_ENTRY(haas200_wifi_register);
769