From 739dc8ad254cb0d1a6d369c84683ae8b9b35977b Mon Sep 17 00:00:00 2001 From: zetingxu Date: Wed, 10 Mar 2021 14:22:16 +0800 Subject: [PATCH 01/13] optiomize wifi connect --- components/drivers/wlan/wlan_mgnt.c | 8 ++++++-- components/drivers/wlan/wlan_mgnt.h | 1 + 2 files changed, 7 insertions(+), 2 deletions(-) mode change 100644 => 100755 components/drivers/wlan/wlan_mgnt.c mode change 100644 => 100755 components/drivers/wlan/wlan_mgnt.h diff --git a/components/drivers/wlan/wlan_mgnt.c b/components/drivers/wlan/wlan_mgnt.c old mode 100644 new mode 100755 index fefe700acc4..b1cd8e7b630 --- a/components/drivers/wlan/wlan_mgnt.c +++ b/components/drivers/wlan/wlan_mgnt.c @@ -1080,6 +1080,7 @@ rt_err_t rt_wlan_connect(const char *ssid, const char *password) /* get info from cache */ INVALID_INFO(&info); MGNT_LOCK(); +#if 0 while (scan_retry-- && rt_wlan_find_best_by_cache(ssid, &info) != RT_TRUE) { rt_wlan_scan_sync(); @@ -1088,14 +1089,17 @@ rt_err_t rt_wlan_connect(const char *ssid, const char *password) if (info.ssid.len <= 0) { - RT_WLAN_LOG_W("not find ap! ssid:%s", ssid); + RT_WLAN_LOG_W("not find ap! ssid:%s,info.ssid.len=%d", ssid,info.ssid.len); MGNT_UNLOCK(); return -RT_ERROR; } RT_WLAN_LOG_D("find best info ssid:%s mac: %02x %02x %02x %02x %02x %02x", info.ssid.val, info.bssid[0], info.bssid[1], info.bssid[2], info.bssid[3], info.bssid[4], info.bssid[5]); - +#else + memcpy(&info.ssid.val[0],ssid,strlen(ssid)); + info.ssid.len = strlen(ssid); +#endif /* create event wait complete */ complete = rt_wlan_complete_create("join"); if (complete == RT_NULL) diff --git a/components/drivers/wlan/wlan_mgnt.h b/components/drivers/wlan/wlan_mgnt.h old mode 100644 new mode 100755 index df0827bb7b0..31d777739f5 --- a/components/drivers/wlan/wlan_mgnt.h +++ b/components/drivers/wlan/wlan_mgnt.h @@ -12,6 +12,7 @@ #define __WLAN_MGNT_H__ #include +#include #ifdef __cplusplus extern "C" { From ef211fbbcd281519ca32906fae6baef7d1b525ca Mon Sep 17 00:00:00 2001 From: liu2guang Date: Wed, 31 Mar 2021 17:39:01 +0800 Subject: [PATCH 02/13] =?UTF-8?q?=E3=80=90=E4=BF=AE=E5=A4=8D=E3=80=91?= =?UTF-8?q?=E4=BF=AE=E5=A4=8D=E8=87=AA=E5=8A=A8=E5=9B=9E=E8=BF=9E=E4=B8=8D?= =?UTF-8?q?=E4=B8=80=E5=AE=9A=E4=BC=9A=E6=89=BE=E6=9C=80=E5=90=8E=E4=B8=80?= =?UTF-8?q?=E4=B8=AA=E9=93=BE=E6=8E=A5=E7=9A=84=E7=83=AD=E7=82=B9=E7=9A=84?= =?UTF-8?q?=E9=97=AE=E9=A2=98:?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: liu2guang --- components/drivers/wlan/wlan_mgnt.c | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/components/drivers/wlan/wlan_mgnt.c b/components/drivers/wlan/wlan_mgnt.c index b1cd8e7b630..dfab93d6ab1 100755 --- a/components/drivers/wlan/wlan_mgnt.c +++ b/components/drivers/wlan/wlan_mgnt.c @@ -53,6 +53,7 @@ #ifdef RT_WLAN_AUTO_CONNECT_ENABLE #define TIME_STOP() (rt_timer_stop(&reconnect_time)) #define TIME_START() (rt_timer_start(&reconnect_time)) +static rt_uint32_t id = 0; #else #define TIME_STOP() #define TIME_START() @@ -500,7 +501,6 @@ static rt_err_t rt_wlan_sta_info_del_all(int timeout) #ifdef RT_WLAN_AUTO_CONNECT_ENABLE static void rt_wlan_auto_connect_run(struct rt_work *work, void *parameter) { - static rt_uint32_t id = 0; struct rt_wlan_cfg_info cfg_info; char *password = RT_NULL; rt_base_t level; @@ -584,6 +584,9 @@ static void rt_wlan_event_dispatch(struct rt_wlan_device *device, rt_wlan_dev_ev case RT_WLAN_DEV_EVT_CONNECT: { RT_WLAN_LOG_D("event: CONNECT"); +#ifdef RT_WLAN_AUTO_CONNECT_ENABLE + id = 0; +#endif _sta_mgnt.state |= RT_WLAN_STATE_CONNECT; _sta_mgnt.state &= ~RT_WLAN_STATE_CONNECTING; user_event = RT_WLAN_EVT_STA_CONNECTED; From f1906c4e9f97cd6569479c7c354e929760cc749f Mon Sep 17 00:00:00 2001 From: guodi Date: Thu, 15 Apr 2021 14:54:00 +0800 Subject: [PATCH 03/13] Modify the wifi scan cmd Signed-off-by: guodi --- components/drivers/wlan/wlan_cmd.c | 102 ++++++++++++++++++----------- 1 file changed, 65 insertions(+), 37 deletions(-) diff --git a/components/drivers/wlan/wlan_cmd.c b/components/drivers/wlan/wlan_cmd.c index fead6a8bf23..51bb2ab7d07 100644 --- a/components/drivers/wlan/wlan_cmd.c +++ b/components/drivers/wlan/wlan_cmd.c @@ -142,46 +142,33 @@ static int wifi_status(int argc, char *argv[]) return 0; } -static int wifi_scan(int argc, char *argv[]) +// static void print_ap_info(struct rt_wlan_info *info,int index) +static void print_ap_info(int event, struct rt_wlan_buff *buff, void *parameter) { - struct rt_wlan_scan_result *scan_result = RT_NULL; - struct rt_wlan_info *info = RT_NULL; - struct rt_wlan_info filter; - - if (argc > 3) - return -1; + int index = 0; + char *security; + struct rt_wlan_info *info = (struct rt_wlan_info *)buff->data; + - if (argc == 3) - { - INVALID_INFO(&filter); - SSID_SET(&filter, argv[2]); - info = &filter; - } + index = *((int *)(parameter)); - /* clean scan result */ - rt_wlan_scan_result_clean(); - /* scan ap info */ - scan_result = rt_wlan_scan_with_info(info); - if (scan_result) - { - int index, num; - char *security; + if(index == 0) + { + rt_kprintf(" SSID MAC security rssi chn Mbps\n"); + rt_kprintf("------------------------------- ----------------- -------------- ---- --- ----\n"); + } - num = scan_result->num; - rt_kprintf(" SSID MAC security rssi chn Mbps\n"); - rt_kprintf("------------------------------- ----------------- -------------- ---- --- ----\n"); - for (index = 0; index < num; index ++) { - rt_kprintf("%-32.32s", &scan_result->info[index].ssid.val[0]); + rt_kprintf("%-32.32s", &(info->ssid.val[0])); rt_kprintf("%02x:%02x:%02x:%02x:%02x:%02x ", - scan_result->info[index].bssid[0], - scan_result->info[index].bssid[1], - scan_result->info[index].bssid[2], - scan_result->info[index].bssid[3], - scan_result->info[index].bssid[4], - scan_result->info[index].bssid[5] + info->bssid[0], + info->bssid[1], + info->bssid[2], + info->bssid[3], + info->bssid[4], + info->bssid[5] ); - switch (scan_result->info[index].security) + switch (info->security) { case SECURITY_OPEN: security = "OPEN"; @@ -218,16 +205,57 @@ static int wifi_scan(int argc, char *argv[]) break; } rt_kprintf("%-14.14s ", security); - rt_kprintf("%-4d ", scan_result->info[index].rssi); - rt_kprintf("%3d ", scan_result->info[index].channel); - rt_kprintf("%4d\n", scan_result->info[index].datarate / 1000000); + rt_kprintf("%-4d ", info->rssi); + rt_kprintf("%3d ", info->channel); + rt_kprintf("%4d\n", info->datarate / 1000000); + } + + index++; + *((int *)(parameter)) = index; + +} + +static int wifi_scan(int argc, char *argv[]) +{ + struct rt_wlan_info *info = RT_NULL; + struct rt_wlan_scan_result *scan_result = RT_NULL; + struct rt_wlan_info filter; + int i = 0; + + if (argc > 3) + return -1; + + if (argc == 3) + { + INVALID_INFO(&filter); + SSID_SET(&filter, argv[2]); + info = &filter; + } + rt_wlan_register_event_handler(RT_WLAN_EVT_SCAN_REPORT,print_ap_info,&i); + + scan_result = rt_wlan_scan_with_info(info); + + if (scan_result) + { + int index = 0, num = 0; + struct rt_wlan_buff buff; + + num = scan_result->num; + for(index = 0;index < num;index++) + { + buff.len = sizeof(struct rt_wlan_info); + buff.data =&(scan_result->info[index]); + + // print_ap_info(index,&buff,RT_NULL); } - rt_wlan_scan_result_clean(); } else { rt_kprintf("wifi scan result is null\n"); } + + /* clean scan result */ + rt_wlan_scan_result_clean(); return 0; } From 3692ef9b71f49890202b57c4bbc5182147d28598 Mon Sep 17 00:00:00 2001 From: guodi Date: Thu, 15 Apr 2021 18:04:12 +0800 Subject: [PATCH 04/13] delete the scan reulst Signed-off-by: guodi --- components/drivers/wlan/wlan_cmd.c | 2 +- components/drivers/wlan/wlan_mgnt.c | 43 +++++++++++++++++------------ 2 files changed, 26 insertions(+), 19 deletions(-) diff --git a/components/drivers/wlan/wlan_cmd.c b/components/drivers/wlan/wlan_cmd.c index 51bb2ab7d07..a126ab90ac6 100644 --- a/components/drivers/wlan/wlan_cmd.c +++ b/components/drivers/wlan/wlan_cmd.c @@ -255,7 +255,7 @@ static int wifi_scan(int argc, char *argv[]) } /* clean scan result */ - rt_wlan_scan_result_clean(); + // rt_wlan_scan_result_clean(); return 0; } diff --git a/components/drivers/wlan/wlan_mgnt.c b/components/drivers/wlan/wlan_mgnt.c index dfab93d6ab1..f95b3cddb5d 100755 --- a/components/drivers/wlan/wlan_mgnt.c +++ b/components/drivers/wlan/wlan_mgnt.c @@ -38,8 +38,8 @@ #define STA_DEVICE() (_sta_mgnt.device) #define AP_DEVICE() (_ap_mgnt.device) -#define SRESULT_LOCK() (rt_mutex_take(&scan_result_mutex, RT_WAITING_FOREVER)) -#define SRESULT_UNLOCK() (rt_mutex_release(&scan_result_mutex)) +// #define SRESULT_LOCK() (rt_mutex_take(&scan_result_mutex, RT_WAITING_FOREVER)) +// #define SRESULT_UNLOCK() (rt_mutex_release(&scan_result_mutex)) #define STAINFO_LOCK() (rt_mutex_take(&sta_info_mutex, RT_WAITING_FOREVER)) #define STAINFO_UNLOCK() (rt_mutex_release(&sta_info_mutex)) @@ -109,8 +109,8 @@ static struct rt_mutex mgnt_mutex; static struct rt_wlan_mgnt_des _sta_mgnt; static struct rt_wlan_mgnt_des _ap_mgnt; -static struct rt_wlan_scan_result scan_result; -static struct rt_mutex scan_result_mutex; +// static struct rt_wlan_scan_result scan_result; +// static struct rt_mutex scan_result_mutex; static struct rt_wlan_sta_des sta_info; static struct rt_mutex sta_info_mutex; @@ -277,6 +277,7 @@ static rt_err_t rt_wlan_send_to_thread(rt_wlan_event_t event, void *buff, int le } #endif +#if 0 static rt_err_t rt_wlan_scan_result_cache(struct rt_wlan_info *info, int timeout) { struct rt_wlan_info *ptable; @@ -400,7 +401,7 @@ static rt_err_t rt_wlan_scan_result_cache(struct rt_wlan_info *info, int timeout rt_mutex_release(&scan_result_mutex); return err; } - +#endif static rt_err_t rt_wlan_sta_info_add(struct rt_wlan_info *info, int timeout) { struct rt_wlan_sta_list *sta_list; @@ -685,14 +686,14 @@ static void rt_wlan_event_dispatch(struct rt_wlan_device *device, rt_wlan_dev_ev user_event = RT_WLAN_EVT_SCAN_REPORT; if (user_buff.len != sizeof(struct rt_wlan_info)) break; - rt_wlan_scan_result_cache(user_buff.data, 0); + // rt_wlan_scan_result_cache(user_buff.data, 0); break; } case RT_WLAN_DEV_EVT_SCAN_DONE: { RT_WLAN_LOG_D("event: SCAN_DONE"); - user_buff.data = &scan_result; - user_buff.len = sizeof(scan_result); + // user_buff.data = &scan_result; + // user_buff.len = sizeof(scan_result); user_event = RT_WLAN_EVT_SCAN_DONE; break; } @@ -992,7 +993,7 @@ rt_wlan_mode_t rt_wlan_get_mode(const char *dev_name) return mode; } - +#if 0 rt_bool_t rt_wlan_find_best_by_cache(const char *ssid, struct rt_wlan_info *info) { int i, ssid_len; @@ -1046,6 +1047,7 @@ rt_bool_t rt_wlan_find_best_by_cache(const char *ssid, struct rt_wlan_info *info *info = *info_best; return RT_TRUE; } +#endif rt_err_t rt_wlan_connect(const char *ssid, const char *password) { @@ -1738,6 +1740,7 @@ rt_err_t rt_wlan_scan(void) return err; } +#if 0 struct rt_wlan_scan_result *rt_wlan_scan_sync(void) { struct rt_wlan_scan_result *result; @@ -1748,6 +1751,7 @@ struct rt_wlan_scan_result *rt_wlan_scan_sync(void) MGNT_UNLOCK(); return result; } +#endif struct rt_wlan_scan_result *rt_wlan_scan_with_info(struct rt_wlan_info *info) { @@ -1756,7 +1760,7 @@ struct rt_wlan_scan_result *rt_wlan_scan_with_info(struct rt_wlan_info *info) rt_uint32_t set = 0, recved = 0; static struct rt_wlan_info scan_filter_info; rt_base_t level; - struct rt_wlan_scan_result *result; + // struct rt_wlan_scan_result *result; if (_sta_is_null()) { @@ -1775,7 +1779,8 @@ struct rt_wlan_scan_result *rt_wlan_scan_with_info(struct rt_wlan_info *info) if (complete == RT_NULL) { MGNT_UNLOCK(); - return &scan_result; + // return &scan_result; + return RT_NULL; } /* add scan info filter */ @@ -1793,7 +1798,7 @@ struct rt_wlan_scan_result *rt_wlan_scan_with_info(struct rt_wlan_info *info) { rt_wlan_complete_delete(complete); RT_WLAN_LOG_E("scan sync fail"); - result = RT_NULL; + // result = RT_NULL; goto scan_exit; } @@ -1807,7 +1812,7 @@ struct rt_wlan_scan_result *rt_wlan_scan_with_info(struct rt_wlan_info *info) if (!(recved & set)) { RT_WLAN_LOG_E("scan wait timeout!"); - result = &scan_result; + // result = &scan_result; goto scan_exit; } @@ -1816,10 +1821,11 @@ struct rt_wlan_scan_result *rt_wlan_scan_with_info(struct rt_wlan_info *info) level = rt_hw_interrupt_disable(); scan_filter = RT_NULL; rt_hw_interrupt_enable(level); - result = &scan_result; - return result; + // result = &scan_result; + return RT_NULL; } +#if 0 int rt_wlan_scan_get_info_num(void) { int num = 0; @@ -1875,6 +1881,7 @@ int rt_wlan_scan_find_cache(struct rt_wlan_info *info, struct rt_wlan_info *out_ return 0; } SRESULT_LOCK(); + /* Traversing the cache to find a qualified hot spot information */ for (i = 0; (i < scan_result.num) && (count < num); i++) { @@ -1891,7 +1898,7 @@ int rt_wlan_scan_find_cache(struct rt_wlan_info *info, struct rt_wlan_info *out_ return count; } - +#endif rt_err_t rt_wlan_set_powersave(int level) { rt_err_t err = RT_EOK; @@ -2016,10 +2023,10 @@ int rt_wlan_init(void) { rt_memset(&_sta_mgnt, 0, sizeof(struct rt_wlan_mgnt_des)); rt_memset(&_ap_mgnt, 0, sizeof(struct rt_wlan_mgnt_des)); - rt_memset(&scan_result, 0, sizeof(struct rt_wlan_scan_result)); + // rt_memset(&scan_result, 0, sizeof(struct rt_wlan_scan_result)); rt_memset(&sta_info, 0, sizeof(struct rt_wlan_sta_des)); rt_mutex_init(&mgnt_mutex, "mgnt", RT_IPC_FLAG_FIFO); - rt_mutex_init(&scan_result_mutex, "scan", RT_IPC_FLAG_FIFO); + // rt_mutex_init(&scan_result_mutex, "scan", RT_IPC_FLAG_FIFO); rt_mutex_init(&sta_info_mutex, "sta", RT_IPC_FLAG_FIFO); rt_mutex_init(&complete_mutex, "complete", RT_IPC_FLAG_FIFO); #ifdef RT_WLAN_AUTO_CONNECT_ENABLE From f158a51fecd4eaac7044778817d3eaba328c89f3 Mon Sep 17 00:00:00 2001 From: guodi Date: Thu, 15 Apr 2021 18:30:14 +0800 Subject: [PATCH 05/13] Move the cache function to the wifi cmd file Signed-off-by: guodi --- components/drivers/wlan/wlan_cmd.c | 183 ++++++++++++++++++++++++++--- 1 file changed, 165 insertions(+), 18 deletions(-) diff --git a/components/drivers/wlan/wlan_cmd.c b/components/drivers/wlan/wlan_cmd.c index a126ab90ac6..79f21dda1f2 100644 --- a/components/drivers/wlan/wlan_cmd.c +++ b/components/drivers/wlan/wlan_cmd.c @@ -13,6 +13,15 @@ #include #include +#define DBG_TAG "WLAN.cmd" +#ifdef RT_WLAN_MGNT_DEBUG +#define DBG_LVL DBG_LOG +#else +#define DBG_LVL DBG_INFO +#endif /* RT_WLAN_MGNT_DEBUG */ +#include + + #if defined(RT_WLAN_MANAGE_ENABLE) && defined(RT_WLAN_MSH_CMD_ENABLE) struct wifi_cmd_des @@ -142,15 +151,138 @@ static int wifi_status(int argc, char *argv[]) return 0; } -// static void print_ap_info(struct rt_wlan_info *info,int index) -static void print_ap_info(int event, struct rt_wlan_buff *buff, void *parameter) +static struct rt_wlan_scan_result scan_result; +/*ToDo:what is the scan_filter.*/ +static struct rt_wlan_info *scan_filter; +static rt_err_t rt_wlan_scan_result_cache(struct rt_wlan_info *info) { - int index = 0; + struct rt_wlan_info *ptable; + rt_err_t err = RT_EOK; + int i, insert = -1; + rt_base_t level; + + // if (_sta_is_null() || (info == RT_NULL) || (info->ssid.len == 0)) return RT_EOK; + + if ((info == RT_NULL) || (info->ssid.len == 0)) return RT_EOK; + LOG_D("ssid:%s len:%d mac:%02x:%02x:%02x:%02x:%02x:%02x", info->ssid.val, info->ssid.len, + info->bssid[0], info->bssid[1], info->bssid[2], info->bssid[3], info->bssid[4], info->bssid[5]); + + if (err != RT_EOK) + return err; + + /* scanning result filtering */ + level = rt_hw_interrupt_disable(); + if (scan_filter) + { + struct rt_wlan_info _tmp_info = *scan_filter; + rt_hw_interrupt_enable(level); + if (rt_wlan_info_isequ(&_tmp_info, info) != RT_TRUE) + { + return RT_EOK; + } + } + else + { + rt_hw_interrupt_enable(level); + } + + /* de-duplicatio */ + for (i = 0; i < scan_result.num; i++) + { + if ((info->ssid.len == scan_result.info[i].ssid.len) && + (rt_memcmp(&info->bssid[0], &scan_result.info[i].bssid[0], RT_WLAN_BSSID_MAX_LENGTH) == 0)) + { + return RT_EOK; + } +#ifdef RT_WLAN_SCAN_SORT + if (insert >= 0) + { + continue; + } + /* Signal intensity comparison */ + if ((info->rssi < 0) && (scan_result.info[i].rssi < 0)) + { + if (info->rssi > scan_result.info[i].rssi) + { + insert = i; + continue; + } + else if (info->rssi < scan_result.info[i].rssi) + { + continue; + } + } + + /* Channel comparison */ + if (info->channel < scan_result.info[i].channel) + { + insert = i; + continue; + } + else if (info->channel > scan_result.info[i].channel) + { + continue; + } + + /* data rate comparison */ + if ((info->datarate > scan_result.info[i].datarate)) + { + insert = i; + continue; + } + else if (info->datarate < scan_result.info[i].datarate) + { + continue; + } +#endif + } + + /* Insert the end */ + if (insert == -1) + insert = scan_result.num; + + if (scan_result.num >= RT_WLAN_SCAN_CACHE_NUM) + return RT_EOK; + + /* malloc memory */ + ptable = rt_malloc(sizeof(struct rt_wlan_info) * (scan_result.num + 1)); + if (ptable == RT_NULL) + { + LOG_E("wlan info malloc failed!"); + return -RT_ENOMEM; + } + scan_result.num ++; + + /* copy info */ + for (i = 0; i < scan_result.num; i++) + { + if (i < insert) + { + ptable[i] = scan_result.info[i]; + } + else if (i > insert) + { + ptable[i] = scan_result.info[i - 1]; + } + else if (i == insert) + { + ptable[i] = *info; + } + } + rt_free(scan_result.info); + scan_result.info = ptable; + return err; +} + +static void print_ap_info(struct rt_wlan_info *info,int index) +// static void print_ap_info(int event, struct rt_wlan_buff *buff, void *parameter) +{ + // int index = 0; char *security; - struct rt_wlan_info *info = (struct rt_wlan_info *)buff->data; + // struct rt_wlan_info *info = (struct rt_wlan_info *)buff->data; - index = *((int *)(parameter)); + // index = *((int *)(parameter)); if(index == 0) { @@ -210,15 +342,29 @@ static void print_ap_info(int event, struct rt_wlan_buff *buff, void *parameter) rt_kprintf("%4d\n", info->datarate / 1000000); } - index++; - *((int *)(parameter)) = index; + // index++; + // *((int *)(parameter)) = index; } +static void user_ap_info_callback(int event, struct rt_wlan_buff *buff, void *parameter) +{ + struct rt_wlan_info *info = (struct rt_wlan_info *)buff->data; + int index = 0; + + index = *((int *)(parameter)); + + rt_wlan_scan_result_cache(info); + + print_ap_info(info,index); + + index++; + *((int *)(parameter)) = index; +} static int wifi_scan(int argc, char *argv[]) { struct rt_wlan_info *info = RT_NULL; - struct rt_wlan_scan_result *scan_result = RT_NULL; + // struct rt_wlan_scan_result *scan_result = RT_NULL; struct rt_wlan_info filter; int i = 0; @@ -231,27 +377,28 @@ static int wifi_scan(int argc, char *argv[]) SSID_SET(&filter, argv[2]); info = &filter; } - rt_wlan_register_event_handler(RT_WLAN_EVT_SCAN_REPORT,print_ap_info,&i); + rt_wlan_register_event_handler(RT_WLAN_EVT_SCAN_REPORT,user_ap_info_callback,&i); - scan_result = rt_wlan_scan_with_info(info); + // scan_result = rt_wlan_scan_with_info(info); + rt_wlan_scan_with_info(info); - if (scan_result) + // if (scan_result) { int index = 0, num = 0; - struct rt_wlan_buff buff; + // struct rt_wlan_buff buff; - num = scan_result->num; + num = scan_result.num; for(index = 0;index < num;index++) { - buff.len = sizeof(struct rt_wlan_info); - buff.data =&(scan_result->info[index]); + // buff.len = sizeof(struct rt_wlan_info); + // buff.data =&(scan_result->info[index]); - // print_ap_info(index,&buff,RT_NULL); + print_ap_info(&(scan_result.info[index]),index); } } - else + // else { - rt_kprintf("wifi scan result is null\n"); + // rt_kprintf("wifi scan result is null\n"); } /* clean scan result */ From 8d56f812b20b4a4aa4410c663116b9f731b926b9 Mon Sep 17 00:00:00 2001 From: guodi Date: Fri, 16 Apr 2021 16:41:04 +0800 Subject: [PATCH 06/13] update scan function Signed-off-by: guodi --- components/drivers/wlan/wlan_cmd.c | 133 +++++++++++++++++++--------- components/drivers/wlan/wlan_mgnt.c | 76 +++------------- components/drivers/wlan/wlan_mgnt.h | 14 +-- 3 files changed, 113 insertions(+), 110 deletions(-) diff --git a/components/drivers/wlan/wlan_cmd.c b/components/drivers/wlan/wlan_cmd.c index 79f21dda1f2..e5b6a190356 100644 --- a/components/drivers/wlan/wlan_cmd.c +++ b/components/drivers/wlan/wlan_cmd.c @@ -153,7 +153,42 @@ static int wifi_status(int argc, char *argv[]) static struct rt_wlan_scan_result scan_result; /*ToDo:what is the scan_filter.*/ -static struct rt_wlan_info *scan_filter; +static struct rt_wlan_info *scan_filter = RT_NULL; + +static rt_bool_t rt_wlan_info_isequ(struct rt_wlan_info *info1, struct rt_wlan_info *info2) +{ + rt_bool_t is_equ = 1; + rt_uint8_t bssid_zero[RT_WLAN_BSSID_MAX_LENGTH] = { 0 }; + + if (is_equ && (info1->security != SECURITY_UNKNOWN) && (info2->security != SECURITY_UNKNOWN)) + { + is_equ &= info2->security == info1->security; + } + if (is_equ && ((info1->ssid.len > 0) && (info2->ssid.len > 0))) + { + is_equ &= info1->ssid.len == info2->ssid.len; + is_equ &= rt_memcmp(&info2->ssid.val[0], &info1->ssid.val[0], info1->ssid.len) == 0; + } + if (is_equ && (rt_memcmp(&info1->bssid[0], bssid_zero, RT_WLAN_BSSID_MAX_LENGTH)) && + (rt_memcmp(&info2->bssid[0], bssid_zero, RT_WLAN_BSSID_MAX_LENGTH))) + { + is_equ &= rt_memcmp(&info1->bssid[0], &info2->bssid[0], RT_WLAN_BSSID_MAX_LENGTH) == 0; + } + if (is_equ && info1->datarate && info2->datarate) + { + is_equ &= info1->datarate == info2->datarate; + } + if (is_equ && (info1->channel >= 0) && (info2->channel >= 0)) + { + is_equ &= info1->channel == info2->channel; + } + if (is_equ && (info1->rssi < 0) && (info2->rssi < 0)) + { + is_equ &= info1->rssi == info2->rssi; + } + return is_equ; +} + static rt_err_t rt_wlan_scan_result_cache(struct rt_wlan_info *info) { struct rt_wlan_info *ptable; @@ -161,15 +196,11 @@ static rt_err_t rt_wlan_scan_result_cache(struct rt_wlan_info *info) int i, insert = -1; rt_base_t level; - // if (_sta_is_null() || (info == RT_NULL) || (info->ssid.len == 0)) return RT_EOK; + if (_sta_is_null() || (info == RT_NULL) || (info->ssid.len == 0)) return -RT_EINVAL; - if ((info == RT_NULL) || (info->ssid.len == 0)) return RT_EOK; LOG_D("ssid:%s len:%d mac:%02x:%02x:%02x:%02x:%02x:%02x", info->ssid.val, info->ssid.len, info->bssid[0], info->bssid[1], info->bssid[2], info->bssid[3], info->bssid[4], info->bssid[5]); - if (err != RT_EOK) - return err; - /* scanning result filtering */ level = rt_hw_interrupt_disable(); if (scan_filter) @@ -274,16 +305,24 @@ static rt_err_t rt_wlan_scan_result_cache(struct rt_wlan_info *info) return err; } + + +static void rt_wlan_scan_result_clean(void) +{ + + /* If there is data */ + if (scan_result.num) + { + scan_result.num = 0; + rt_free(scan_result.info); + scan_result.info = RT_NULL; + } +} + static void print_ap_info(struct rt_wlan_info *info,int index) -// static void print_ap_info(int event, struct rt_wlan_buff *buff, void *parameter) { - // int index = 0; char *security; - // struct rt_wlan_info *info = (struct rt_wlan_info *)buff->data; - - // index = *((int *)(parameter)); - if(index == 0) { rt_kprintf(" SSID MAC security rssi chn Mbps\n"); @@ -342,30 +381,47 @@ static void print_ap_info(struct rt_wlan_info *info,int index) rt_kprintf("%4d\n", info->datarate / 1000000); } - // index++; - // *((int *)(parameter)) = index; - } static void user_ap_info_callback(int event, struct rt_wlan_buff *buff, void *parameter) { - struct rt_wlan_info *info = (struct rt_wlan_info *)buff->data; + struct rt_wlan_info *info = RT_NULL; int index = 0; + int ret = RT_EOK; - index = *((int *)(parameter)); + RT_ASSERT(event == RT_WLAN_EVT_SCAN_REPORT); + RT_ASSERT(buff != NULL); + RT_ASSERT(parameter != NULL); - rt_wlan_scan_result_cache(info); + info = (struct rt_wlan_info *)buff->data; + index = *((int *)(parameter)); - print_ap_info(info,index); + ret = rt_wlan_scan_result_cache(info); + // if(ret != RT_EOK) + // { + // LOG_E("Can't cache this ap info ret:%d,%s.",ret,&(info->ssid.val[0])); + // } + if(ret == RT_EOK) + { + if(scan_filter == RT_NULL || + (scan_filter != RT_NULL && + scan_filter->ssid.len == info->ssid.len && + rt_memcmp(&scan_filter->ssid.val[0], &info->ssid.val[0], scan_filter->ssid.len) == 0)) + { + /*Print the info*/ + print_ap_info(info,index); + + index++; + *((int *)(parameter)) = index; + } + } - index++; - *((int *)(parameter)) = index; } static int wifi_scan(int argc, char *argv[]) { struct rt_wlan_info *info = RT_NULL; - // struct rt_wlan_scan_result *scan_result = RT_NULL; struct rt_wlan_info filter; + int ret = 0; int i = 0; if (argc > 3) @@ -378,31 +434,26 @@ static int wifi_scan(int argc, char *argv[]) info = &filter; } rt_wlan_register_event_handler(RT_WLAN_EVT_SCAN_REPORT,user_ap_info_callback,&i); - - // scan_result = rt_wlan_scan_with_info(info); - rt_wlan_scan_with_info(info); - - // if (scan_result) + + if(info) { - int index = 0, num = 0; - // struct rt_wlan_buff buff; + scan_filter = info; + } - num = scan_result.num; - for(index = 0;index < num;index++) - { - // buff.len = sizeof(struct rt_wlan_info); - // buff.data =&(scan_result->info[index]); - print_ap_info(&(scan_result.info[index]),index); - } - } - // else + /*Todo: what can i do for it return val */ + ret = rt_wlan_scan_with_info(info); + if(ret != RT_EOK) { - // rt_kprintf("wifi scan result is null\n"); + LOG_E("Scan with info error:%d!\n",ret); } - + /* clean scan result */ - // rt_wlan_scan_result_clean(); + rt_wlan_scan_result_clean(); + if(info) + { + scan_filter = RT_NULL; + } return 0; } diff --git a/components/drivers/wlan/wlan_mgnt.c b/components/drivers/wlan/wlan_mgnt.c index f95b3cddb5d..3b389ae585f 100755 --- a/components/drivers/wlan/wlan_mgnt.c +++ b/components/drivers/wlan/wlan_mgnt.c @@ -119,13 +119,13 @@ static struct rt_wlan_event_desc event_tab[RT_WLAN_EVT_MAX]; static struct rt_wlan_complete_des *complete_tab[5]; static struct rt_mutex complete_mutex; -static struct rt_wlan_info *scan_filter; +// static struct rt_wlan_info *scan_filter; #ifdef RT_WLAN_AUTO_CONNECT_ENABLE static struct rt_timer reconnect_time; #endif -rt_inline int _sta_is_null(void) +int _sta_is_null(void) { if (_sta_mgnt.device == RT_NULL) { @@ -134,7 +134,7 @@ rt_inline int _sta_is_null(void) return 0; } -rt_inline int _ap_is_null(void) +int _ap_is_null(void) { if (_ap_mgnt.device == RT_NULL) { @@ -143,7 +143,7 @@ rt_inline int _ap_is_null(void) return 0; } -rt_inline rt_bool_t _is_do_connect(void) +rt_bool_t _is_do_connect(void) { if ((rt_wlan_get_autoreconnect_mode() == RT_FALSE) || (rt_wlan_is_connected() == RT_TRUE) || @@ -156,40 +156,6 @@ rt_inline rt_bool_t _is_do_connect(void) #ifdef RT_WLAN_WORK_THREAD_ENABLE -static rt_bool_t rt_wlan_info_isequ(struct rt_wlan_info *info1, struct rt_wlan_info *info2) -{ - rt_bool_t is_equ = 1; - rt_uint8_t bssid_zero[RT_WLAN_BSSID_MAX_LENGTH] = { 0 }; - - if (is_equ && (info1->security != SECURITY_UNKNOWN) && (info2->security != SECURITY_UNKNOWN)) - { - is_equ &= info2->security == info1->security; - } - if (is_equ && ((info1->ssid.len > 0) && (info2->ssid.len > 0))) - { - is_equ &= info1->ssid.len == info2->ssid.len; - is_equ &= rt_memcmp(&info2->ssid.val[0], &info1->ssid.val[0], info1->ssid.len) == 0; - } - if (is_equ && (rt_memcmp(&info1->bssid[0], bssid_zero, RT_WLAN_BSSID_MAX_LENGTH)) && - (rt_memcmp(&info2->bssid[0], bssid_zero, RT_WLAN_BSSID_MAX_LENGTH))) - { - is_equ &= rt_memcmp(&info1->bssid[0], &info2->bssid[0], RT_WLAN_BSSID_MAX_LENGTH) == 0; - } - if (is_equ && info1->datarate && info2->datarate) - { - is_equ &= info1->datarate == info2->datarate; - } - if (is_equ && (info1->channel >= 0) && (info2->channel >= 0)) - { - is_equ &= info1->channel == info2->channel; - } - if (is_equ && (info1->rssi < 0) && (info2->rssi < 0)) - { - is_equ &= info1->rssi == info2->rssi; - } - return is_equ; -} - static void rt_wlan_mgnt_work(void *parameter) { struct rt_wlan_msg *msg = parameter; @@ -1753,24 +1719,22 @@ struct rt_wlan_scan_result *rt_wlan_scan_sync(void) } #endif -struct rt_wlan_scan_result *rt_wlan_scan_with_info(struct rt_wlan_info *info) +rt_err_t rt_wlan_scan_with_info(struct rt_wlan_info *info) { rt_err_t err = RT_EOK; struct rt_wlan_complete_des *complete; rt_uint32_t set = 0, recved = 0; static struct rt_wlan_info scan_filter_info; - rt_base_t level; - // struct rt_wlan_scan_result *result; if (_sta_is_null()) { - return RT_NULL; + return -RT_EINVAL; } RT_WLAN_LOG_D("%s is run", __FUNCTION__); if (info != RT_NULL && info->ssid.len > RT_WLAN_SSID_MAX_LENGTH) { RT_WLAN_LOG_E("ssid is to long!"); - return RT_NULL; + return -RT_EINVAL; } /* Create an event that needs to wait. */ @@ -1780,18 +1744,9 @@ struct rt_wlan_scan_result *rt_wlan_scan_with_info(struct rt_wlan_info *info) { MGNT_UNLOCK(); // return &scan_result; - return RT_NULL; - } - - /* add scan info filter */ - if (info) - { - scan_filter_info = *info; - level = rt_hw_interrupt_disable(); - scan_filter = &scan_filter_info; - rt_hw_interrupt_enable(level); + return -RT_EIO; } - + /* run scan */ err = rt_wlan_dev_scan(STA_DEVICE(), info); if (err != RT_EOK) @@ -1799,7 +1754,8 @@ struct rt_wlan_scan_result *rt_wlan_scan_with_info(struct rt_wlan_info *info) rt_wlan_complete_delete(complete); RT_WLAN_LOG_E("scan sync fail"); // result = RT_NULL; - goto scan_exit; + MGNT_UNLOCK(); + return -RT_ERROR; } /* Initializing events that need to wait */ @@ -1813,16 +1769,12 @@ struct rt_wlan_scan_result *rt_wlan_scan_with_info(struct rt_wlan_info *info) { RT_WLAN_LOG_E("scan wait timeout!"); // result = &scan_result; - goto scan_exit; + MGNT_UNLOCK(); + return -RT_ETIMEOUT; } -scan_exit: MGNT_UNLOCK(); - level = rt_hw_interrupt_disable(); - scan_filter = RT_NULL; - rt_hw_interrupt_enable(level); - // result = &scan_result; - return RT_NULL; + return RT_EOK; } #if 0 diff --git a/components/drivers/wlan/wlan_mgnt.h b/components/drivers/wlan/wlan_mgnt.h index 31d777739f5..faa6dd0b843 100755 --- a/components/drivers/wlan/wlan_mgnt.h +++ b/components/drivers/wlan/wlan_mgnt.h @@ -120,13 +120,13 @@ rt_country_code_t rt_wlan_ap_get_country(void); */ rt_err_t rt_wlan_scan(void); struct rt_wlan_scan_result *rt_wlan_scan_sync(void); -struct rt_wlan_scan_result *rt_wlan_scan_with_info(struct rt_wlan_info *info); -int rt_wlan_scan_get_info_num(void); -int rt_wlan_scan_get_info(struct rt_wlan_info *info, int num); -struct rt_wlan_scan_result *rt_wlan_scan_get_result(void); -void rt_wlan_scan_result_clean(void); -int rt_wlan_scan_find_cache(struct rt_wlan_info *info, struct rt_wlan_info *out_info, int num); -rt_bool_t rt_wlan_find_best_by_cache(const char *ssid, struct rt_wlan_info *info); +rt_err_t rt_wlan_scan_with_info(struct rt_wlan_info *info); +// int rt_wlan_scan_get_info_num(void); +// int rt_wlan_scan_get_info(struct rt_wlan_info *info, int num); +// struct rt_wlan_scan_result *rt_wlan_scan_get_result(void); +// void rt_wlan_scan_result_clean(void); +// int rt_wlan_scan_find_cache(struct rt_wlan_info *info, struct rt_wlan_info *out_info, int num); +// rt_bool_t rt_wlan_find_best_by_cache(const char *ssid, struct rt_wlan_info *info); /* * wifi auto connect interface From fa5e04d98310d6104e2cdd64693ee8b0535951cd Mon Sep 17 00:00:00 2001 From: guodi Date: Fri, 16 Apr 2021 16:42:07 +0800 Subject: [PATCH 07/13] update the bes wifi scan function Signed-off-by: guodi --- components/drivers/wlan/wlan_cmd.c | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/components/drivers/wlan/wlan_cmd.c b/components/drivers/wlan/wlan_cmd.c index e5b6a190356..a00636affc0 100644 --- a/components/drivers/wlan/wlan_cmd.c +++ b/components/drivers/wlan/wlan_cmd.c @@ -433,8 +433,14 @@ static int wifi_scan(int argc, char *argv[]) SSID_SET(&filter, argv[2]); info = &filter; } - rt_wlan_register_event_handler(RT_WLAN_EVT_SCAN_REPORT,user_ap_info_callback,&i); - + + ret = rt_wlan_register_event_handler(RT_WLAN_EVT_SCAN_REPORT,user_ap_info_callback,&i); + if(ret != RT_EOK) + { + LOG_E("Scan register user callback error:%d!\n",ret); + return 0; + } + if(info) { scan_filter = info; From d070ecfb9d80d25187a2b1e232ba06ed6bd328b1 Mon Sep 17 00:00:00 2001 From: guodi Date: Fri, 16 Apr 2021 19:36:56 +0800 Subject: [PATCH 08/13] add join scan funtion Signed-off-by: guodi --- components/drivers/wlan/wlan_mgnt.c | 64 +++++++++++++++++++++++++---- 1 file changed, 56 insertions(+), 8 deletions(-) diff --git a/components/drivers/wlan/wlan_mgnt.c b/components/drivers/wlan/wlan_mgnt.c index 3b389ae585f..8d888f5bbfc 100755 --- a/components/drivers/wlan/wlan_mgnt.c +++ b/components/drivers/wlan/wlan_mgnt.c @@ -1015,6 +1015,42 @@ rt_bool_t rt_wlan_find_best_by_cache(const char *ssid, struct rt_wlan_info *info } #endif +static void rt_wlan_join_scan_callback(int event, struct rt_wlan_buff *buff, void *parameter) +{ + struct rt_wlan_info *info = RT_NULL; + struct rt_wlan_info *tgt_info = RT_NULL; + int ret = RT_EOK; + + RT_ASSERT(event == RT_WLAN_EVT_SCAN_REPORT); + RT_ASSERT(buff != NULL); + RT_ASSERT(parameter != NULL); + + info = (struct rt_wlan_info *)buff->data; + tgt_info = (struct rt_wlan_info *)parameter; + + + RT_WLAN_LOG_D("%s info len:%d tgt info len:%d", __FUNCTION__,info->ssid.len,tgt_info->ssid.len); + RT_WLAN_LOG_D("%s info ssid:%s tgt info ssid:%s", __FUNCTION__,&info->ssid.val[0],&tgt_info->ssid.val[0]); + + if(rt_memcmp(&info->ssid.val[0], &tgt_info->ssid.val[0], info->ssid.len) == 0 && + info->ssid.len == tgt_info->ssid.len) + { + /*Get the rssi the max ap*/ + if(info->rssi > tgt_info->rssi) + { + tgt_info->security = info->security; + tgt_info->band = info->band; + tgt_info->datarate = info->datarate; + tgt_info->channel = info->channel; + tgt_info->rssi = info->rssi; + tgt_info->hidden = info->hidden; + /* hwaddr */ + rt_memcmp(tgt_info->bssid,info->bssid,RT_WLAN_BSSID_MAX_LENGTH); + } + } +} + + rt_err_t rt_wlan_connect(const char *ssid, const char *password) { rt_err_t err = RT_EOK; @@ -1051,14 +1087,28 @@ rt_err_t rt_wlan_connect(const char *ssid, const char *password) /* get info from cache */ INVALID_INFO(&info); MGNT_LOCK(); -#if 0 - while (scan_retry-- && rt_wlan_find_best_by_cache(ssid, &info) != RT_TRUE) + + rt_memcpy(&info.ssid.val[0],ssid,rt_strlen(ssid)); + info.ssid.len = rt_strlen(ssid); + +#define RT_WLAN_JOIN_SCAN_BY_MGNT +#ifdef RT_WLAN_JOIN_SCAN_BY_MGNT + err = rt_wlan_register_event_handler(RT_WLAN_EVT_SCAN_REPORT,rt_wlan_join_scan_callback,&info); + if(err != RT_EOK) { - rt_wlan_scan_sync(); + LOG_E("Scan register user callback error:%d!\n",err); + return err; } - rt_wlan_scan_result_clean(); - if (info.ssid.len <= 0) + err = rt_wlan_scan_with_info(&info); + if(err != RT_EOK) + { + LOG_E("Scan with info error:%d!\n",err); + return err; + } + + // if (info.ssid.len <= 0 || info.rssi <= 0) + if (info.channel <= 0) { RT_WLAN_LOG_W("not find ap! ssid:%s,info.ssid.len=%d", ssid,info.ssid.len); MGNT_UNLOCK(); @@ -1067,10 +1117,8 @@ rt_err_t rt_wlan_connect(const char *ssid, const char *password) RT_WLAN_LOG_D("find best info ssid:%s mac: %02x %02x %02x %02x %02x %02x", info.ssid.val, info.bssid[0], info.bssid[1], info.bssid[2], info.bssid[3], info.bssid[4], info.bssid[5]); -#else - memcpy(&info.ssid.val[0],ssid,strlen(ssid)); - info.ssid.len = strlen(ssid); #endif + /* create event wait complete */ complete = rt_wlan_complete_create("join"); if (complete == RT_NULL) From 831a2dd203451da2fea868387411e7a9aab72149 Mon Sep 17 00:00:00 2001 From: geniusgogo Date: Fri, 13 May 2022 14:17:47 +0800 Subject: [PATCH 09/13] update auto connect use sys work. --- components/drivers/wlan/wlan_mgnt.c | 37 ++++++++++++++++++++--------- 1 file changed, 26 insertions(+), 11 deletions(-) diff --git a/components/drivers/wlan/wlan_mgnt.c b/components/drivers/wlan/wlan_mgnt.c index 552d84abe91..455796c85a5 100644 --- a/components/drivers/wlan/wlan_mgnt.c +++ b/components/drivers/wlan/wlan_mgnt.c @@ -51,6 +51,7 @@ #ifdef RT_WLAN_AUTO_CONNECT_ENABLE #define TIME_STOP() (rt_timer_stop(&reconnect_time)) #define TIME_START() (rt_timer_start(&reconnect_time)) +static rt_uint32_t id = 0; #else #define TIME_STOP() #define TIME_START() @@ -337,7 +338,6 @@ static rt_err_t rt_wlan_sta_info_del_all(int timeout) #ifdef RT_WLAN_AUTO_CONNECT_ENABLE static void rt_wlan_auto_connect_run(struct rt_work *work, void *parameter) { - static rt_uint32_t id = 0; struct rt_wlan_cfg_info cfg_info; char *password = RT_NULL; rt_base_t level; @@ -381,24 +381,19 @@ static void rt_wlan_auto_connect_run(struct rt_work *work, void *parameter) static void rt_wlan_cyclic_check(void *parameter) { - struct rt_workqueue *workqueue; static struct rt_work work; rt_base_t level; if ((_is_do_connect() == RT_TRUE) && (work.work_func == RT_NULL)) { - workqueue = rt_wlan_get_workqueue(); - if (workqueue != RT_NULL) + level = rt_hw_interrupt_disable(); + rt_work_init(&work, rt_wlan_auto_connect_run, RT_NULL); + rt_hw_interrupt_enable(level); + if(rt_work_submit(&work,RT_TICK_PER_SECOND) != RT_EOK) { level = rt_hw_interrupt_disable(); - rt_work_init(&work, rt_wlan_auto_connect_run, RT_NULL); + rt_memset(&work, 0, sizeof(struct rt_work)); rt_hw_interrupt_enable(level); - if (rt_workqueue_dowork(workqueue, &work) != RT_EOK) - { - level = rt_hw_interrupt_disable(); - rt_memset(&work, 0, sizeof(struct rt_work)); - rt_hw_interrupt_enable(level); - } } } } @@ -421,6 +416,9 @@ static void rt_wlan_event_dispatch(struct rt_wlan_device *device, rt_wlan_dev_ev case RT_WLAN_DEV_EVT_CONNECT: { RT_WLAN_LOG_D("event: CONNECT"); +#ifdef RT_WLAN_AUTO_CONNECT_ENABLE + id = 0; +#endif _sta_mgnt.state |= RT_WLAN_STATE_CONNECT; _sta_mgnt.state &= ~RT_WLAN_STATE_CONNECTING; user_event = RT_WLAN_EVT_STA_CONNECTED; @@ -428,6 +426,23 @@ static void rt_wlan_event_dispatch(struct rt_wlan_device *device, rt_wlan_dev_ev user_buff.data = &_sta_mgnt.info; user_buff.len = sizeof(struct rt_wlan_info); RT_WLAN_LOG_I("wifi connect success ssid:%s", &_sta_mgnt.info.ssid.val[0]); + +#ifdef RT_WLAN_CFG_ENABLE + { + struct rt_wlan_cfg_info cfg_info; + rt_memset(&cfg_info, 0, sizeof(cfg_info)); + /* save config */ + if (rt_wlan_is_connected() == RT_TRUE) + { + rt_enter_critical(); + cfg_info.info = _sta_mgnt.info; + cfg_info.key = _sta_mgnt.key; + rt_exit_critical(); + RT_WLAN_LOG_D("run save config! ssid:%s len%d", _sta_mgnt.info.ssid.val, _sta_mgnt.info.ssid.len); + rt_wlan_cfg_save(&cfg_info); + } + } +#endif break; } case RT_WLAN_DEV_EVT_CONNECT_FAIL: From 8894f5644739e655269891f52ce43a7d24793d4e Mon Sep 17 00:00:00 2001 From: geniusgogo Date: Fri, 13 May 2022 14:20:49 +0800 Subject: [PATCH 10/13] add fast connect. --- components/drivers/wlan/wlan_dev.c | 66 +++++++++++++++++++++++++++++ components/drivers/wlan/wlan_dev.h | 7 ++- components/drivers/wlan/wlan_mgnt.c | 23 ++++++---- 3 files changed, 86 insertions(+), 10 deletions(-) diff --git a/components/drivers/wlan/wlan_dev.c b/components/drivers/wlan/wlan_dev.c index 49c39f1fd19..31e93e9f84c 100644 --- a/components/drivers/wlan/wlan_dev.c +++ b/components/drivers/wlan/wlan_dev.c @@ -120,6 +120,42 @@ rt_err_t rt_wlan_dev_connect(struct rt_wlan_device *device, struct rt_wlan_info return result; } +rt_err_t rt_wlan_dev_fast_connect(struct rt_wlan_device *device, struct rt_wlan_info *info, const char *password, int password_len) +{ + rt_err_t result = RT_EOK; + struct rt_wlan_buff buff; + + int len = 0; + + if (device == RT_NULL) + { + return -RT_EIO; + } + if (info == RT_NULL) + { + return -RT_ERROR; + } + + if ((password_len > RT_WLAN_PASSWORD_MAX_LENGTH) || + (info->ssid.len > RT_WLAN_SSID_MAX_LENGTH)) + { + LOG_E("L:%d password or ssid is too long", __LINE__); + return -RT_ERROR; + } + + + buff.len = rt_device_control(RT_DEVICE(device), RT_WLAN_CMD_GET_FAST_CONNECT_INFO, buff.data); + if(buff.len < 0) + { + LOG_D("L:%d Can't get fast connect info", __LINE__); + return buff.len; + } + + result = rt_device_control(RT_DEVICE(device), RT_WLAN_CMD_FAST_CONNECT, &buff); + + return result; +} + rt_err_t rt_wlan_dev_disconnect(struct rt_wlan_device *device) { rt_err_t result = RT_EOK; @@ -848,6 +884,36 @@ static rt_err_t _rt_wlan_dev_control(rt_device_t dev, int cmd, void *args) err = wlan->ops->wlan_get_mac(wlan, mac); break; } + case RT_WLAN_CMD_GET_FAST_CONNECT_INFO: + { + + LOG_D("%s %d cmd[%d]:%s run......", __FUNCTION__, __LINE__, RT_WLAN_CMD_GET_FAST_INFO, "RT_WLAN_CMD_GET_FAST_INFO"); + if (wlan->ops->wlan_get_fast_info) + { + err = wlan->ops->wlan_get_fast_info(args); + } + else + { + err = -RT_EEMPTY; + } + break; + } + case RT_WLAN_CMD_FAST_CONNECT: + { + struct rt_wlan_buff *buff = (struct rt_wlan_buff *)args; + LOG_D("%s %d cmd[%d]:%s run......", __FUNCTION__, __LINE__, RT_WLAN_CMD_FAST_CONNECT, "RT_WLAN_CMD_FAST_CONNECT"); + if (wlan->ops->wlan_get_fast_info) + { + err = wlan->ops->wlan_fast_connect(buff->data,buff->len); + } + else + { + err = -RT_EEMPTY; + } + break; + + } + default: LOG_D("%s %d cmd[%d]:%s run......", __FUNCTION__, __LINE__, -1, "UNKUOWN"); break; diff --git a/components/drivers/wlan/wlan_dev.h b/components/drivers/wlan/wlan_dev.h index 9fce22b7c99..d89dc60ef31 100644 --- a/components/drivers/wlan/wlan_dev.h +++ b/components/drivers/wlan/wlan_dev.h @@ -44,7 +44,9 @@ typedef enum RT_WLAN_CMD_SET_COUNTRY, RT_WLAN_CMD_GET_COUNTRY, RT_WLAN_CMD_SET_MAC, - RT_WLAN_CMD_GET_MAC + RT_WLAN_CMD_GET_MAC, + RT_WLAN_CMD_GET_FAST_CONNECT_INFO, + RT_WLAN_CMD_FAST_CONNECT, } rt_wlan_cmd_t; typedef enum @@ -509,6 +511,8 @@ struct rt_wlan_dev_ops int (*wlan_recv)(struct rt_wlan_device *wlan, void *buff, int len); int (*wlan_send)(struct rt_wlan_device *wlan, void *buff, int len); int (*wlan_send_raw_frame)(struct rt_wlan_device *wlan, void *buff, int len); + int (*wlan_get_fast_info)(void *data); + rt_err_t (*wlan_fast_connect)(void *data,rt_int32_t len); }; /* @@ -520,6 +524,7 @@ rt_err_t rt_wlan_dev_init(struct rt_wlan_device *device, rt_wlan_mode_t mode); * wlan device station interface */ rt_err_t rt_wlan_dev_connect(struct rt_wlan_device *device, struct rt_wlan_info *info, const char *password, int password_len); +rt_err_t rt_wlan_dev_fast_connect(struct rt_wlan_device *device, struct rt_wlan_info *info, const char *password, int password_len); rt_err_t rt_wlan_dev_disconnect(struct rt_wlan_device *device); int rt_wlan_dev_get_rssi(struct rt_wlan_device *device); diff --git a/components/drivers/wlan/wlan_mgnt.c b/components/drivers/wlan/wlan_mgnt.c index 455796c85a5..810a0bb9e0d 100644 --- a/components/drivers/wlan/wlan_mgnt.c +++ b/components/drivers/wlan/wlan_mgnt.c @@ -1035,16 +1035,21 @@ rt_err_t rt_wlan_connect_adv(struct rt_wlan_info *info, const char *password) rt_exit_critical(); /* run wifi connect */ _sta_mgnt.state |= RT_WLAN_STATE_CONNECTING; - err = rt_wlan_dev_connect(_sta_mgnt.device, info, password, password_len); - if (err != RT_EOK) + + err = rt_wlan_dev_fast_connect(_sta_mgnt.device, info, password, password_len); + if(err != RT_EOK) { - rt_enter_critical(); - rt_memset(&_sta_mgnt.info, 0, sizeof(struct rt_wlan_ssid)); - rt_memset(&_sta_mgnt.key, 0, sizeof(struct rt_wlan_key)); - rt_exit_critical(); - _sta_mgnt.state &= ~RT_WLAN_STATE_CONNECTING; - MGNT_UNLOCK(); - return err; + err = rt_wlan_dev_connect(_sta_mgnt.device, info, password, password_len); + if (err != RT_EOK) + { + rt_enter_critical(); + rt_memset(&_sta_mgnt.info, 0, sizeof(struct rt_wlan_ssid)); + rt_memset(&_sta_mgnt.key, 0, sizeof(struct rt_wlan_key)); + rt_exit_critical(); + _sta_mgnt.state &= ~RT_WLAN_STATE_CONNECTING; + MGNT_UNLOCK(); + return err; + } } MGNT_UNLOCK(); From b101e5d9ffb4ee5e66874ade33a78a278bc57dd8 Mon Sep 17 00:00:00 2001 From: geniusgogo Date: Fri, 13 May 2022 14:21:46 +0800 Subject: [PATCH 11/13] update wlan cmd, support scan result report user-level callback. --- components/drivers/wlan/wlan_cmd.c | 296 +++++++++++++++++++++++++---- 1 file changed, 262 insertions(+), 34 deletions(-) diff --git a/components/drivers/wlan/wlan_cmd.c b/components/drivers/wlan/wlan_cmd.c index 52c25f6755a..485745961cc 100644 --- a/components/drivers/wlan/wlan_cmd.c +++ b/components/drivers/wlan/wlan_cmd.c @@ -9,10 +9,22 @@ */ #include +#include #include #include #include +#define DBG_TAG "WLAN.cmd" +#ifdef RT_WLAN_MGNT_DEBUG +#define DBG_LVL DBG_LOG +#else +#define DBG_LVL DBG_INFO +#endif /* RT_WLAN_MGNT_DEBUG */ +#include + +static struct rt_wlan_scan_result scan_result; +static struct rt_wlan_info *scan_filter = RT_NULL; + #if defined(RT_WLAN_MANAGE_ENABLE) && defined(RT_WLAN_MSH_CMD_ENABLE) struct wifi_cmd_des @@ -142,46 +154,192 @@ static int wifi_status(int argc, char *argv[]) return 0; } -static int wifi_scan(int argc, char *argv[]) + +static rt_bool_t wifi_info_isequ(struct rt_wlan_info *info1, struct rt_wlan_info *info2) { - struct rt_wlan_scan_result *scan_result = RT_NULL; - struct rt_wlan_info *info = RT_NULL; - struct rt_wlan_info filter; + rt_bool_t is_equ = 1; + rt_uint8_t bssid_zero[RT_WLAN_BSSID_MAX_LENGTH] = { 0 }; - if (argc > 3) - return -1; + if (is_equ && (info1->security != SECURITY_UNKNOWN) && (info2->security != SECURITY_UNKNOWN)) + { + is_equ &= info2->security == info1->security; + } + if (is_equ && ((info1->ssid.len > 0) && (info2->ssid.len > 0))) + { + is_equ &= info1->ssid.len == info2->ssid.len; + is_equ &= rt_memcmp(&info2->ssid.val[0], &info1->ssid.val[0], info1->ssid.len) == 0; + } + if (is_equ && (rt_memcmp(&info1->bssid[0], bssid_zero, RT_WLAN_BSSID_MAX_LENGTH)) && + (rt_memcmp(&info2->bssid[0], bssid_zero, RT_WLAN_BSSID_MAX_LENGTH))) + { + is_equ &= rt_memcmp(&info1->bssid[0], &info2->bssid[0], RT_WLAN_BSSID_MAX_LENGTH) == 0; + } + if (is_equ && info1->datarate && info2->datarate) + { + is_equ &= info1->datarate == info2->datarate; + } + if (is_equ && (info1->channel >= 0) && (info2->channel >= 0)) + { + is_equ &= info1->channel == info2->channel; + } + if (is_equ && (info1->rssi < 0) && (info2->rssi < 0)) + { + is_equ &= info1->rssi == info2->rssi; + } + return is_equ; +} - if (argc == 3) +static rt_err_t wifi_scan_result_cache(struct rt_wlan_info *info) +{ + struct rt_wlan_info *ptable; + rt_err_t err = RT_EOK; + int i, insert = -1; + rt_base_t level; + + if (_sta_is_null() || (info == RT_NULL) || (info->ssid.len == 0)) return -RT_EINVAL; + + LOG_D("ssid:%s len:%d mac:%02x:%02x:%02x:%02x:%02x:%02x", info->ssid.val, info->ssid.len, + info->bssid[0], info->bssid[1], info->bssid[2], info->bssid[3], info->bssid[4], info->bssid[5]); + + /* scanning result filtering */ + level = rt_hw_interrupt_disable(); + if (scan_filter) { - INVALID_INFO(&filter); - SSID_SET(&filter, argv[2]); - info = &filter; + struct rt_wlan_info _tmp_info = *scan_filter; + rt_hw_interrupt_enable(level); + if (wifi_info_isequ(&_tmp_info, info) != RT_TRUE) + { + return RT_EOK; + } + } + else + { + rt_hw_interrupt_enable(level); } - /* clean scan result */ - rt_wlan_scan_result_clean(); - /* scan ap info */ - scan_result = rt_wlan_scan_with_info(info); - if (scan_result) + /* de-duplicatio */ + for (i = 0; i < scan_result.num; i++) { - int index, num; + if ((info->ssid.len == scan_result.info[i].ssid.len) && + (rt_memcmp(&info->bssid[0], &scan_result.info[i].bssid[0], RT_WLAN_BSSID_MAX_LENGTH) == 0)) + { + return RT_EOK; + } +#ifdef RT_WLAN_SCAN_SORT + if (insert >= 0) + { + continue; + } + /* Signal intensity comparison */ + if ((info->rssi < 0) && (scan_result.info[i].rssi < 0)) + { + if (info->rssi > scan_result.info[i].rssi) + { + insert = i; + continue; + } + else if (info->rssi < scan_result.info[i].rssi) + { + continue; + } + } + + /* Channel comparison */ + if (info->channel < scan_result.info[i].channel) + { + insert = i; + continue; + } + else if (info->channel > scan_result.info[i].channel) + { + continue; + } + + /* data rate comparison */ + if ((info->datarate > scan_result.info[i].datarate)) + { + insert = i; + continue; + } + else if (info->datarate < scan_result.info[i].datarate) + { + continue; + } +#endif + } + + /* Insert the end */ + if (insert == -1) + insert = scan_result.num; + + if (scan_result.num >= RT_WLAN_SCAN_CACHE_NUM) + return RT_EOK; + + /* malloc memory */ + ptable = rt_malloc(sizeof(struct rt_wlan_info) * (scan_result.num + 1)); + if (ptable == RT_NULL) + { + LOG_E("wlan info malloc failed!"); + return -RT_ENOMEM; + } + scan_result.num ++; + + /* copy info */ + for (i = 0; i < scan_result.num; i++) + { + if (i < insert) + { + ptable[i] = scan_result.info[i]; + } + else if (i > insert) + { + ptable[i] = scan_result.info[i - 1]; + } + else if (i == insert) + { + ptable[i] = *info; + } + } + rt_free(scan_result.info); + scan_result.info = ptable; + return err; +} + + + +static void wifi_scan_result_clean(void) +{ + + /* If there is data */ + if (scan_result.num) + { + scan_result.num = 0; + rt_free(scan_result.info); + scan_result.info = RT_NULL; + } +} + +static void print_ap_info(struct rt_wlan_info *info,int index) +{ char *security; + + if(index == 0) + { + rt_kprintf(" SSID MAC security rssi chn Mbps\n"); + rt_kprintf("------------------------------- ----------------- -------------- ---- --- ----\n"); + } - num = scan_result->num; - rt_kprintf(" SSID MAC security rssi chn Mbps\n"); - rt_kprintf("------------------------------- ----------------- -------------- ---- --- ----\n"); - for (index = 0; index < num; index ++) { - rt_kprintf("%-32.32s", &scan_result->info[index].ssid.val[0]); + rt_kprintf("%-32.32s", &(info->ssid.val[0])); rt_kprintf("%02x:%02x:%02x:%02x:%02x:%02x ", - scan_result->info[index].bssid[0], - scan_result->info[index].bssid[1], - scan_result->info[index].bssid[2], - scan_result->info[index].bssid[3], - scan_result->info[index].bssid[4], - scan_result->info[index].bssid[5] + info->bssid[0], + info->bssid[1], + info->bssid[2], + info->bssid[3], + info->bssid[4], + info->bssid[5] ); - switch (scan_result->info[index].security) + switch (info->security) { case SECURITY_OPEN: security = "OPEN"; @@ -218,15 +376,85 @@ static int wifi_scan(int argc, char *argv[]) break; } rt_kprintf("%-14.14s ", security); - rt_kprintf("%-4d ", scan_result->info[index].rssi); - rt_kprintf("%3d ", scan_result->info[index].channel); - rt_kprintf("%4d\n", scan_result->info[index].datarate / 1000000); + rt_kprintf("%-4d ", info->rssi); + rt_kprintf("%3d ", info->channel); + rt_kprintf("%4d\n", info->datarate / 1000000); + } + +} + +static void user_ap_info_callback(int event, struct rt_wlan_buff *buff, void *parameter) +{ + struct rt_wlan_info *info = RT_NULL; + int index = 0; + int ret = RT_EOK; + + RT_ASSERT(event == RT_WLAN_EVT_SCAN_REPORT); + RT_ASSERT(buff != NULL); + RT_ASSERT(parameter != NULL); + + info = (struct rt_wlan_info *)buff->data; + index = *((int *)(parameter)); + + ret = wifi_scan_result_cache(info); + if(ret == RT_EOK) + { + if(scan_filter == RT_NULL || + (scan_filter != RT_NULL && + scan_filter->ssid.len == info->ssid.len && + rt_memcmp(&scan_filter->ssid.val[0], &info->ssid.val[0], scan_filter->ssid.len) == 0)) + { + /*Print the info*/ + print_ap_info(info,index); + + index++; + *((int *)(parameter)) = index; } - rt_wlan_scan_result_clean(); } - else + +} +static int wifi_scan(int argc, char *argv[]) +{ + struct rt_wlan_info *info = RT_NULL; + struct rt_wlan_info filter; + int ret = 0; + int i = 0; + + if (argc > 3) + return -1; + + if (argc == 3) + { + INVALID_INFO(&filter); + SSID_SET(&filter, argv[2]); + info = &filter; + } + + ret = rt_wlan_register_event_handler(RT_WLAN_EVT_SCAN_REPORT,user_ap_info_callback,&i); + if(ret != RT_EOK) + { + LOG_E("Scan register user callback error:%d!\n",ret); + return 0; + } + + if(info) + { + scan_filter = info; + } + + + /*Todo: what can i do for it return val */ + ret = rt_wlan_scan_with_info(info); + if(ret != RT_EOK) + { + LOG_E("Scan with info error:%d!\n",ret); + } + + /* clean scan result */ + wifi_scan_result_clean(); + if(info) { - rt_kprintf("wifi scan result is null\n"); + scan_filter = RT_NULL; } return 0; } From c77925b0ee455c95ff32e7cf320e4f70996b860b Mon Sep 17 00:00:00 2001 From: geniusgogo Date: Fri, 13 May 2022 14:54:39 +0800 Subject: [PATCH 12/13] delete extra space at the end of this line --- components/drivers/wlan/wlan_cmd.c | 8 ++++---- components/drivers/wlan/wlan_dev.c | 4 +--- components/drivers/wlan/wlan_mgnt.c | 4 ++-- 3 files changed, 7 insertions(+), 9 deletions(-) diff --git a/components/drivers/wlan/wlan_cmd.c b/components/drivers/wlan/wlan_cmd.c index 485745961cc..149cfa4bb19 100644 --- a/components/drivers/wlan/wlan_cmd.c +++ b/components/drivers/wlan/wlan_cmd.c @@ -322,7 +322,7 @@ static void wifi_scan_result_clean(void) static void print_ap_info(struct rt_wlan_info *info,int index) { char *security; - + if(index == 0) { rt_kprintf(" SSID MAC security rssi chn Mbps\n"); @@ -399,14 +399,14 @@ static void user_ap_info_callback(int event, struct rt_wlan_buff *buff, void *pa ret = wifi_scan_result_cache(info); if(ret == RT_EOK) { - if(scan_filter == RT_NULL || + if(scan_filter == RT_NULL || (scan_filter != RT_NULL && - scan_filter->ssid.len == info->ssid.len && + scan_filter->ssid.len == info->ssid.len && rt_memcmp(&scan_filter->ssid.val[0], &info->ssid.val[0], scan_filter->ssid.len) == 0)) { /*Print the info*/ print_ap_info(info,index); - + index++; *((int *)(parameter)) = index; } diff --git a/components/drivers/wlan/wlan_dev.c b/components/drivers/wlan/wlan_dev.c index 31e93e9f84c..8e3af17e574 100644 --- a/components/drivers/wlan/wlan_dev.c +++ b/components/drivers/wlan/wlan_dev.c @@ -124,7 +124,7 @@ rt_err_t rt_wlan_dev_fast_connect(struct rt_wlan_device *device, struct rt_wlan_ { rt_err_t result = RT_EOK; struct rt_wlan_buff buff; - + int len = 0; if (device == RT_NULL) @@ -142,7 +142,6 @@ rt_err_t rt_wlan_dev_fast_connect(struct rt_wlan_device *device, struct rt_wlan_ LOG_E("L:%d password or ssid is too long", __LINE__); return -RT_ERROR; } - buff.len = rt_device_control(RT_DEVICE(device), RT_WLAN_CMD_GET_FAST_CONNECT_INFO, buff.data); if(buff.len < 0) @@ -911,7 +910,6 @@ static rt_err_t _rt_wlan_dev_control(rt_device_t dev, int cmd, void *args) err = -RT_EEMPTY; } break; - } default: diff --git a/components/drivers/wlan/wlan_mgnt.c b/components/drivers/wlan/wlan_mgnt.c index 810a0bb9e0d..c1745a96d95 100644 --- a/components/drivers/wlan/wlan_mgnt.c +++ b/components/drivers/wlan/wlan_mgnt.c @@ -440,7 +440,7 @@ static void rt_wlan_event_dispatch(struct rt_wlan_device *device, rt_wlan_dev_ev rt_exit_critical(); RT_WLAN_LOG_D("run save config! ssid:%s len%d", _sta_mgnt.info.ssid.val, _sta_mgnt.info.ssid.len); rt_wlan_cfg_save(&cfg_info); - } + } } #endif break; @@ -854,7 +854,7 @@ static void rt_wlan_join_scan_callback(int event, struct rt_wlan_buff *buff, voi RT_WLAN_LOG_D("%s info len:%d tgt info len:%d", __FUNCTION__,info->ssid.len,tgt_info->ssid.len); RT_WLAN_LOG_D("%s info ssid:%s tgt info ssid:%s", __FUNCTION__,&info->ssid.val[0],&tgt_info->ssid.val[0]); - + if(rt_memcmp(&info->ssid.val[0], &tgt_info->ssid.val[0], info->ssid.len) == 0 && info->ssid.len == tgt_info->ssid.len) { From 1975cdcfa46a184c145133cad47ee56850e1d27a Mon Sep 17 00:00:00 2001 From: geniusgogo Date: Fri, 13 May 2022 17:15:17 +0800 Subject: [PATCH 13/13] fixed NULL to RT_NULL. --- components/drivers/wlan/wlan_cmd.c | 6 +++--- components/drivers/wlan/wlan_mgnt.c | 4 ++-- 2 files changed, 5 insertions(+), 5 deletions(-) diff --git a/components/drivers/wlan/wlan_cmd.c b/components/drivers/wlan/wlan_cmd.c index 149cfa4bb19..9089b3b124e 100644 --- a/components/drivers/wlan/wlan_cmd.c +++ b/components/drivers/wlan/wlan_cmd.c @@ -196,7 +196,7 @@ static rt_err_t wifi_scan_result_cache(struct rt_wlan_info *info) int i, insert = -1; rt_base_t level; - if (_sta_is_null() || (info == RT_NULL) || (info->ssid.len == 0)) return -RT_EINVAL; + if ((info == RT_NULL) || (info->ssid.len == 0)) return -RT_EINVAL; LOG_D("ssid:%s len:%d mac:%02x:%02x:%02x:%02x:%02x:%02x", info->ssid.val, info->ssid.len, info->bssid[0], info->bssid[1], info->bssid[2], info->bssid[3], info->bssid[4], info->bssid[5]); @@ -390,8 +390,8 @@ static void user_ap_info_callback(int event, struct rt_wlan_buff *buff, void *pa int ret = RT_EOK; RT_ASSERT(event == RT_WLAN_EVT_SCAN_REPORT); - RT_ASSERT(buff != NULL); - RT_ASSERT(parameter != NULL); + RT_ASSERT(buff != RT_NULL); + RT_ASSERT(parameter != RT_NULL); info = (struct rt_wlan_info *)buff->data; index = *((int *)(parameter)); diff --git a/components/drivers/wlan/wlan_mgnt.c b/components/drivers/wlan/wlan_mgnt.c index c1745a96d95..768d3420696 100644 --- a/components/drivers/wlan/wlan_mgnt.c +++ b/components/drivers/wlan/wlan_mgnt.c @@ -845,8 +845,8 @@ static void rt_wlan_join_scan_callback(int event, struct rt_wlan_buff *buff, voi int ret = RT_EOK; RT_ASSERT(event == RT_WLAN_EVT_SCAN_REPORT); - RT_ASSERT(buff != NULL); - RT_ASSERT(parameter != NULL); + RT_ASSERT(buff != RT_NULL); + RT_ASSERT(parameter != RT_NULL); info = (struct rt_wlan_info *)buff->data; tgt_info = (struct rt_wlan_info *)parameter;