From d515494d1b8b38f1bcd2884db17125dbb4c67118 Mon Sep 17 00:00:00 2001 From: Deng Xin Date: Thu, 28 Sep 2017 16:17:06 +0800 Subject: [PATCH] wifi: optimize scan feature 1. add the threshold of match AP in fast scan 2. add blacklist feature 3. make sure would wrong if password didn't set 4. add scan example --- components/esp32/include/esp_wifi_types.h | 6 ++ components/esp32/lib | 2 +- docs/api-guides/wifi.rst | 1 + examples/wifi/scan/Makefile | 9 ++ examples/wifi/scan/README.md | 11 ++ examples/wifi/scan/main/Kconfig.projbuild | 68 ++++++++++++ examples/wifi/scan/main/component.mk | 7 ++ examples/wifi/scan/main/scan.c | 122 ++++++++++++++++++++++ 8 files changed, 225 insertions(+), 1 deletion(-) create mode 100644 examples/wifi/scan/Makefile create mode 100644 examples/wifi/scan/README.md create mode 100644 examples/wifi/scan/main/Kconfig.projbuild create mode 100644 examples/wifi/scan/main/component.mk create mode 100644 examples/wifi/scan/main/scan.c diff --git a/components/esp32/include/esp_wifi_types.h b/components/esp32/include/esp_wifi_types.h index e91031a5b..61161037c 100755 --- a/components/esp32/include/esp_wifi_types.h +++ b/components/esp32/include/esp_wifi_types.h @@ -162,6 +162,11 @@ typedef enum { WIFI_CONNECT_AP_BY_SECURITY, /**< Sort match AP in scan list by security mode */ }wifi_sort_method_t; +typedef struct { + int8_t rssi; /**< The minimum rssi to accept in the fast scan mode */ + wifi_auth_mode_t authmode; /**< The weakest authmode to accept in the fast scan mode */ +}wifi_fast_scan_threshold_t; + typedef enum { WIFI_PS_NONE, /**< No power save */ WIFI_PS_MODEM, /**< Modem power save */ @@ -196,6 +201,7 @@ typedef struct { uint8_t bssid[6]; /**< MAC address of target AP*/ uint8_t channel; /**< channel of target AP. Set to 1~13 to scan starting from the specified channel before connecting to AP. If the channel of AP is unknown, set it to 0.*/ wifi_sort_method_t sort_method; /**< sort the connect AP in the list by rssi or security mode */ + wifi_fast_scan_threshold_t threshold; /**< When scan_method is set to WIFI_FAST_SCAN, only APs which have an auth mode that is more secure than the selected auth mode and a signal stronger than the minimum RSSI will be used. */ } wifi_sta_config_t; typedef union { diff --git a/components/esp32/lib b/components/esp32/lib index 70e346552..fa408a790 160000 --- a/components/esp32/lib +++ b/components/esp32/lib @@ -1 +1 @@ -Subproject commit 70e346552061b635d26079709348676446999cf9 +Subproject commit fa408a790773e0d8b51e620961c90708c560fe5b diff --git a/docs/api-guides/wifi.rst b/docs/api-guides/wifi.rst index 85bff8671..daafca4d2 100644 --- a/docs/api-guides/wifi.rst +++ b/docs/api-guides/wifi.rst @@ -20,6 +20,7 @@ ESP32 Wi-Fi Feature List - Up to 20 MBit/sec TCP throughput and 30 MBit/sec UDP throughput over the air - Supports Sniffer - Support set fast_crypto algorithm and normal algorithm switch which used in wifi connect +- Support both fast scan and all channel scan feature How To Write a Wi-Fi Application ---------------------------------- diff --git a/examples/wifi/scan/Makefile b/examples/wifi/scan/Makefile new file mode 100644 index 000000000..f560b7aae --- /dev/null +++ b/examples/wifi/scan/Makefile @@ -0,0 +1,9 @@ +# +# This is a project Makefile. It is assumed the directory this Makefile resides in is a +# project subdirectory. +# + +PROJECT_NAME := scan + +include $(IDF_PATH)/make/project.mk + diff --git a/examples/wifi/scan/README.md b/examples/wifi/scan/README.md new file mode 100644 index 000000000..9d146266e --- /dev/null +++ b/examples/wifi/scan/README.md @@ -0,0 +1,11 @@ +# Wifi SCAN Example + +This example shows how to use scan of ESP32. + +We have two way to scan, fast scan and all channel scan: + +* fast scan: in this mode, scan will finish after find match AP even didn't scan all the channel, you can set thresholds for signal and authmode, it will ignore the AP which below the thresholds. + +* all channel scan : scan will end after checked all the channel, it will store four of the whole matched AP, you can set the sort method base on rssi or authmode, after scan, it will choose the best one + +and try to connect. Because it need malloc dynamic memory to store match AP, and most of cases is to connect to better signal AP, so it needn't record all the AP matched. The number of matches is limited to 4 in order to limit dynamic memory usage. Four matches allows APs with the same SSID name and all possible auth modes - Open, WEP, WPA and WPA2. diff --git a/examples/wifi/scan/main/Kconfig.projbuild b/examples/wifi/scan/main/Kconfig.projbuild new file mode 100644 index 000000000..bd1dd9665 --- /dev/null +++ b/examples/wifi/scan/main/Kconfig.projbuild @@ -0,0 +1,68 @@ +menu "Example Configuration" + +config WIFI_SSID + string "WiFi SSID" + default "myssid" + help + SSID (network name) for the example to connect to. + +config WIFI_PASSWORD + string "WiFi Password" + default "mypassword" + help + WiFi password (WPA or WPA2) for the example to use. + +choice SCAN_METHOD + prompt "scan method" + default WIFI_FAST_SCAN + help + scan method for the esp32 to use + +config WIFI_FAST_SCAN + bool "fast" +config WIFI_ALL_CHANNEL_SCAN + bool "all" +endchoice + +choice SORT_METHOD + prompt "sort method" + default WIFI_CONNECT_AP_BY_SIGNAL + help + sort method for the esp32 to use + +config WIFI_CONNECT_AP_BY_SIGNAL + bool "rssi" +config WIFI_CONNECT_AP_BY_SECURITY + bool "authmode" +endchoice + +config FAST_SCAN_THRESHOLD + bool "fast scan threshold" + default y + help + wifi fast scan threshold + +config FAST_SCAN_MINIMUM_SIGNAL + int "fast scan minimum rssi" + depends on FAST_SCAN_THRESHOLD + range -127 0 + default -127 + help + rssi is use to measure the signal + +choice FAST_SCAN_WEAKEST_AUTHMODE + prompt "fast scan weakest authmode" + depends on FAST_SCAN_THRESHOLD + default EXAMPLE_OPEN + +config EXAMPLE_OPEN + bool "open" +config EXAMPLE_WEP + bool "wep" +config EXAMPLE_WPA + bool "wpa" +config EXAMPLE_WPA2 + bool "wpa2" +endchoice + +endmenu diff --git a/examples/wifi/scan/main/component.mk b/examples/wifi/scan/main/component.mk new file mode 100644 index 000000000..7d7b29bfd --- /dev/null +++ b/examples/wifi/scan/main/component.mk @@ -0,0 +1,7 @@ +# +# "main" pseudo-component makefile. +# +# (Uses default behaviour of compiling all source files in directory, adding 'include' to include path.) + + + diff --git a/examples/wifi/scan/main/scan.c b/examples/wifi/scan/main/scan.c new file mode 100644 index 000000000..f48464886 --- /dev/null +++ b/examples/wifi/scan/main/scan.c @@ -0,0 +1,122 @@ +/* Scan Example + + This example code is in the Public Domain (or CC0 licensed, at your option.) + + Unless required by applicable law or agreed to in writing, this + software is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR + CONDITIONS OF ANY KIND, either express or implied. +*/ + +/* + this example shows how to use all channel scan or fast scan to connect + In the fast scan mode, scan will end after find matched AP, in this mode, you + can set the threshold of auth mode and signal, the filter will ignore the AP + whose auth mode and rssi below the threshold. + In the all channel scan, scan will end after scan all the channel, and will + the best AP to connect, you can sort by signal or auth mode, auth mode is follow + the rule WPA2>WPA>WEP. +*/ +#include "freertos/FreeRTOS.h" +#include "freertos/event_groups.h" +#include "esp_wifi.h" +#include "esp_log.h" +#include "esp_event_loop.h" +#include "nvs_flash.h" + +/*set the ssid and password via "make menuconfig"*/ +#define DEFAULT_SSID CONFIG_WIFI_SSID +#define DEFAULT_PWD CONFIG_WIFI_PASSWORD + +#if CONFIG_WIFI_ALL_CHANNEL_SCAN +#define DEFAULT_SCAN_METHOD WIFI_ALL_CHANNEL_SCAN +#elif CONFIG_WIFI_FAST_SCAN +#define DEFAULT_SCAN_METHOD WIFI_FAST_SCAN +#else +#define DEFAULT_SCAN_METHOD WIFI_FAST_SCAN +#endif /*CONFIG_SCAN_METHOD*/ + +#if CONFIG_WIFI_CONNECT_AP_BY_SIGNAL +#define DEFAULT_SORT_METHOD WIFI_CONNECT_AP_BY_SIGNAL +#elif CONFIG_WIFI_CONNECT_AP_BY_SECURITY +#define DEFAULT_SORT_METHOD WIFI_CONNECT_AP_BY_SECURITY +#else +#define DEFAULT_SORT_METHOD WIFI_CONNECT_AP_BY_SIGNAL +#endif /*CONFIG_SORT_METHOD*/ + +#if CONFIG_FAST_SCAN_THRESHOLD +#define DEFAULT_RSSI CONFIG_FAST_SCAN_MINIMUM_SIGNAL +#if CONFIG_EXAMPLE_OPEN +#define DEFAULT_AUTHMODE WIFI_AUTH_OPEN +#elif CONFIG_EXAMPLE_WEP +#define DEFAULT_AUTHMODE WIFI_AUTH_WEP +#elif CONFIG_EXAMPLE_WPA +#define DEFAULT_AUTHMODE WIFI_AUTH_WPA_PSK +#elif CONFIG_EXAMPLE_WPA2 +#define DEFAULT_AUTHMODE WIFI_AUTH_WPA2_PSK +#else +#define DEFAULT_AUTHMODE WIFI_AUTH_OPEN +#endif +#else +#define DEFAULT_RSSI -127 +#define DEFAULT_AUTHMODE WIFI_AUTH_OPEN +#endif /*CONFIG_FAST_SCAN_THRESHOLD*/ + +static const char *TAG = "scan"; + +static esp_err_t event_handler(void *ctx, system_event_t *event) +{ + switch(event->event_id) { + case SYSTEM_EVENT_STA_START: + ESP_LOGI(TAG, "SYSTEM_EVENT_STA_START"); + ESP_ERROR_CHECK(esp_wifi_connect()); + break; + case SYSTEM_EVENT_STA_GOT_IP: + ESP_LOGI(TAG, "SYSTEM_EVENT_STA_GOT_IP"); + ESP_LOGI(TAG, "got ip:%s\n", + ip4addr_ntoa(&event->event_info.got_ip.ip_info.ip)); + break; + case SYSTEM_EVENT_STA_DISCONNECTED: + ESP_LOGI(TAG, "SYSTEM_EVENT_STA_DISCONNECTED"); + ESP_ERROR_CHECK(esp_wifi_connect()); + break; + default: + break; + } + return ESP_OK; +} + +/*init wifi as sta and set scan method*/ +static void wifi_scan(void) +{ + tcpip_adapter_init(); + ESP_ERROR_CHECK(esp_event_loop_init(event_handler, NULL)); + + wifi_init_config_t cfg = WIFI_INIT_CONFIG_DEFAULT(); + ESP_ERROR_CHECK(esp_wifi_init(&cfg)); + wifi_config_t wifi_config = { + .sta = { + .ssid = DEFAULT_SSID, + .password = DEFAULT_PWD, + .scan_method = DEFAULT_SCAN_METHOD, + .sort_method = DEFAULT_SORT_METHOD, + .threshold.rssi = DEFAULT_RSSI, + .threshold.authmode = DEFAULT_AUTHMODE, + }, + }; + ESP_ERROR_CHECK(esp_wifi_set_mode(WIFI_MODE_STA)); + ESP_ERROR_CHECK(esp_wifi_set_config(ESP_IF_WIFI_STA, &wifi_config)); + ESP_ERROR_CHECK(esp_wifi_start()); +} + +void app_main() +{ + // Initialize NVS + esp_err_t ret = nvs_flash_init(); + if (ret == ESP_ERR_NVS_NO_FREE_PAGES) { + ESP_ERROR_CHECK(nvs_flash_erase()); + ret = nvs_flash_init(); + } + ESP_ERROR_CHECK( ret ); + + wifi_scan(); +}