Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add more controls over WiFi.begin and WiFi.scan #6651

Merged
merged 1 commit into from
Apr 28, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
45 changes: 37 additions & 8 deletions libraries/WiFi/src/WiFiSTA.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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){
Expand All @@ -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;
Expand Down Expand Up @@ -243,12 +246,7 @@ wl_status_t WiFiSTAClass::begin(const char* ssid, const char *passphrase, int32_
_wifi_strncpy(reinterpret_cast<char*>(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, &current_conf) != ESP_OK){
Expand Down Expand Up @@ -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)
Expand Down
8 changes: 8 additions & 0 deletions libraries/WiFi/src/WiFiSTA.h
Original file line number Diff line number Diff line change
Expand Up @@ -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();

Expand Down Expand Up @@ -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);
Expand Down
6 changes: 3 additions & 3 deletions libraries/WiFi/src/WiFiScan.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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){
Expand Down
2 changes: 1 addition & 1 deletion libraries/WiFi/src/WiFiScan.h
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
2 changes: 2 additions & 0 deletions libraries/WiFi/src/WiFiType.h
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down