From 81c07b2a49f541fb90c65c74a8f5d2c24db2164d Mon Sep 17 00:00:00 2001 From: me-no-dev Date: Thu, 28 Apr 2022 04:44:11 +0300 Subject: [PATCH] Add more controls over WiFi.begin and WiFi.scan Added options to WiFi STA for connect: - setMinSecurity: default WIFI_AUTH_WPA2_PSK - setScanMethod: default WIFI_FAST_SCAN - setSortMethod: default WIFI_CONNECT_AP_BY_SIGNAL (required all channels scan method) Added parameters for SSID and BSSID to WiFi.scanNetworks() --- libraries/WiFi/src/WiFiSTA.cpp | 45 +++++++++++++++++++++++++++------ libraries/WiFi/src/WiFiSTA.h | 8 ++++++ libraries/WiFi/src/WiFiScan.cpp | 6 ++--- libraries/WiFi/src/WiFiScan.h | 2 +- libraries/WiFi/src/WiFiType.h | 2 ++ 5 files changed, 51 insertions(+), 12 deletions(-) diff --git a/libraries/WiFi/src/WiFiSTA.cpp b/libraries/WiFi/src/WiFiSTA.cpp index 02e7185cd7e..7734f5d79b5 100644 --- a/libraries/WiFi/src/WiFiSTA.cpp +++ b/libraries/WiFi/src/WiFiSTA.cpp @@ -83,7 +83,7 @@ static bool sta_config_equal(const wifi_config_t& lhs, const wifi_config_t& rhs) return true; } -static void wifi_sta_config(wifi_config_t * wifi_config, const char * ssid=NULL, const char * password=NULL, const uint8_t * bssid=NULL, uint8_t channel=0, wifi_scan_method_t scan_method=WIFI_ALL_CHANNEL_SCAN, wifi_sort_method_t sort_method=WIFI_CONNECT_AP_BY_SIGNAL, uint16_t listen_interval=0, bool pmf_required=false){ +static void wifi_sta_config(wifi_config_t * wifi_config, const char * ssid=NULL, const char * password=NULL, const uint8_t * bssid=NULL, uint8_t channel=0, wifi_auth_mode_t min_security=WIFI_AUTH_WPA2_PSK, wifi_scan_method_t scan_method=WIFI_ALL_CHANNEL_SCAN, wifi_sort_method_t sort_method=WIFI_CONNECT_AP_BY_SIGNAL, uint16_t listen_interval=0, bool pmf_required=false){ wifi_config->sta.channel = channel; wifi_config->sta.listen_interval = listen_interval; wifi_config->sta.scan_method = scan_method;//WIFI_ALL_CHANNEL_SCAN or WIFI_FAST_SCAN @@ -99,7 +99,7 @@ static void wifi_sta_config(wifi_config_t * wifi_config, const char * ssid=NULL, if(ssid != NULL && ssid[0] != 0){ _wifi_strncpy((char*)wifi_config->sta.ssid, ssid, 32); if(password != NULL && password[0] != 0){ - wifi_config->sta.threshold.authmode = WIFI_AUTH_WPA2_PSK; + wifi_config->sta.threshold.authmode = min_security; _wifi_strncpy((char*)wifi_config->sta.password, password, 64); } if(bssid != NULL){ @@ -115,6 +115,9 @@ static void wifi_sta_config(wifi_config_t * wifi_config, const char * ssid=NULL, bool WiFiSTAClass::_autoReconnect = true; bool WiFiSTAClass::_useStaticIp = false; +wifi_auth_mode_t WiFiSTAClass::_minSecurity = WIFI_AUTH_WPA2_PSK; +wifi_scan_method_t WiFiSTAClass::_scanMethod = WIFI_FAST_SCAN; +wifi_sort_method_t WiFiSTAClass::_sortMethod = WIFI_CONNECT_AP_BY_SIGNAL; static wl_status_t _sta_status = WL_NO_SHIELD; static EventGroupHandle_t _sta_status_group = NULL; @@ -243,12 +246,7 @@ wl_status_t WiFiSTAClass::begin(const char* ssid, const char *passphrase, int32_ _wifi_strncpy(reinterpret_cast(conf.sta.password), passphrase, 64); } - if(channel == 0) { - // If no specific channel specified, then do an slower WIFI_ALL_CHANNEL_SCAN - wifi_sta_config(&conf, ssid, passphrase, bssid, channel, WIFI_ALL_CHANNEL_SCAN); - } - else - wifi_sta_config(&conf, ssid, passphrase, bssid, channel, WIFI_FAST_SCAN); + wifi_sta_config(&conf, ssid, passphrase, bssid, channel, _minSecurity, _scanMethod, _sortMethod); wifi_config_t current_conf; if(esp_wifi_get_config((wifi_interface_t)ESP_IF_WIFI_STA, ¤t_conf) != ESP_OK){ @@ -404,6 +402,37 @@ bool WiFiSTAClass::isConnected() return (status() == WL_CONNECTED); } +/** + * Set the minimum security for AP to be considered connectable + * Must be called before WiFi.begin() + * @param minSecurity wifi_auth_mode_t + */ +void WiFiSTAClass::setMinSecurity(wifi_auth_mode_t minSecurity) +{ + _minSecurity = minSecurity; +} + +/** + * Set the way that AP is chosen. + * First SSID match[WIFI_FAST_SCAN] or Sorted[WIFI_ALL_CHANNEL_SCAN] (RSSI or Security) + * Must be called before WiFi.begin() + * @param scanMethod wifi_scan_method_t + */ +void WiFiSTAClass::setScanMethod(wifi_scan_method_t scanMethod) +{ + _scanMethod = scanMethod; +} + +/** + * Set the way that AP is sorted. (requires scanMethod WIFI_ALL_CHANNEL_SCAN) + * By SSID[WIFI_CONNECT_AP_BY_SIGNAL] or Security[WIFI_CONNECT_AP_BY_SECURITY] + * Must be called before WiFi.begin() + * @param sortMethod wifi_sort_method_t + */ +void WiFiSTAClass::setSortMethod(wifi_sort_method_t sortMethod) +{ + _sortMethod = sortMethod; +} /** * Setting the ESP32 station to connect to the AP (which is recorded) diff --git a/libraries/WiFi/src/WiFiSTA.h b/libraries/WiFi/src/WiFiSTA.h index 613c37682b8..b8bb855c198 100644 --- a/libraries/WiFi/src/WiFiSTA.h +++ b/libraries/WiFi/src/WiFiSTA.h @@ -64,6 +64,11 @@ class WiFiSTAClass uint8_t waitForConnectResult(unsigned long timeoutLength = 60000); + // Next group functions must be called before WiFi.begin() + void setMinSecurity(wifi_auth_mode_t minSecurity);// Default is WIFI_AUTH_WPA2_PSK + void setScanMethod(wifi_scan_method_t scanMethod);// Default is WIFI_FAST_SCAN + void setSortMethod(wifi_sort_method_t sortMethod);// Default is WIFI_CONNECT_AP_BY_SIGNAL + // STA network info IPAddress localIP(); @@ -96,6 +101,9 @@ class WiFiSTAClass protected: static bool _useStaticIp; static bool _autoReconnect; + static wifi_auth_mode_t _minSecurity; + static wifi_scan_method_t _scanMethod; + static wifi_sort_method_t _sortMethod; public: bool beginSmartConfig(smartconfig_type_t type = SC_TYPE_ESPTOUCH, char* crypt_key = NULL); diff --git a/libraries/WiFi/src/WiFiScan.cpp b/libraries/WiFi/src/WiFiScan.cpp index f34badb16dc..5b0c6732a64 100644 --- a/libraries/WiFi/src/WiFiScan.cpp +++ b/libraries/WiFi/src/WiFiScan.cpp @@ -81,7 +81,7 @@ void* WiFiScanClass::_scanResult = 0; * @param show_hidden show hidden networks * @return Number of discovered networks */ -int16_t WiFiScanClass::scanNetworks(bool async, bool show_hidden, bool passive, uint32_t max_ms_per_chan, uint8_t channel) +int16_t WiFiScanClass::scanNetworks(bool async, bool show_hidden, bool passive, uint32_t max_ms_per_chan, uint8_t channel, const char * ssid, const uint8_t * bssid) { if(WiFiGenericClass::getStatusBits() & WIFI_SCANNING_BIT) { return WIFI_SCAN_RUNNING; @@ -95,8 +95,8 @@ int16_t WiFiScanClass::scanNetworks(bool async, bool show_hidden, bool passive, scanDelete(); wifi_scan_config_t config; - config.ssid = 0; - config.bssid = 0; + config.ssid = (uint8_t*)ssid; + config.bssid = (uint8_t*)bssid; config.channel = channel; config.show_hidden = show_hidden; if(passive){ diff --git a/libraries/WiFi/src/WiFiScan.h b/libraries/WiFi/src/WiFiScan.h index 1929e46627b..b838dc17574 100644 --- a/libraries/WiFi/src/WiFiScan.h +++ b/libraries/WiFi/src/WiFiScan.h @@ -31,7 +31,7 @@ class WiFiScanClass public: - int16_t scanNetworks(bool async = false, bool show_hidden = false, bool passive = false, uint32_t max_ms_per_chan = 300, uint8_t channel = 0); + int16_t scanNetworks(bool async = false, bool show_hidden = false, bool passive = false, uint32_t max_ms_per_chan = 300, uint8_t channel = 0, const char * ssid=nullptr, const uint8_t * bssid=nullptr); int16_t scanComplete(); void scanDelete(); diff --git a/libraries/WiFi/src/WiFiType.h b/libraries/WiFi/src/WiFiType.h index ee8cfdd2498..9b7b00e2c3a 100644 --- a/libraries/WiFi/src/WiFiType.h +++ b/libraries/WiFi/src/WiFiType.h @@ -23,6 +23,8 @@ #ifndef ESP32WIFITYPE_H_ #define ESP32WIFITYPE_H_ +#include "esp_wifi_types.h" + #define WIFI_SCAN_RUNNING (-1) #define WIFI_SCAN_FAILED (-2)