1
0
mirror of https://github.com/esp8266/Arduino.git synced 2025-04-19 23:22:16 +03:00

WiFi: clean up AP SSID setter & getter, support 32 chars (#7941)

This commit is contained in:
Max Prokhorov 2021-05-15 22:28:22 +03:00 committed by GitHub
parent 5f21c61c7c
commit 4436e32a50
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 50 additions and 41 deletions

View File

@ -40,18 +40,18 @@ terms <https://en.wikipedia.org/wiki/Function_overloading>`__) of this function
WiFi.softAP(ssid) WiFi.softAP(ssid)
To set up password protected network, or to configure additional network parameters, use the following overload: To set up pre-shared key protected network, or to configure additional network parameters, use the following overload:
.. code:: cpp .. code:: cpp
WiFi.softAP(ssid, password, channel, hidden, max_connection) WiFi.softAP(ssid, psk, channel, hidden, max_connection)
The first parameter of this function is required, remaining four are optional. The first parameter of this function is required, remaining four are optional.
Meaning of all parameters is as follows: Meaning of all parameters is as follows:
- ``ssid`` - character string containing network SSID (max. 31 characters) - ``ssid`` - character string containing network SSID (max. 32 characters)
- ``password`` - optional character string with a password. For WPA2-PSK network it should be at least 8 character long. If not specified, the access point will be open for anybody to connect, (max. 63 characters). - ``psk`` - optional character string with a pre-shared key. For WPA2-PSK network it should be minimum 8 characters long and not longer than 64 characters. If not specified, the access point will be open for anybody to connect.
- ``channel`` - optional parameter to set Wi-Fi channel, from 1 to 13. Default channel = 1. - ``channel`` - optional parameter to set Wi-Fi channel, from 1 to 13. Default channel = 1.
- ``hidden`` - optional parameter, if set to ``true`` will hide SSID. - ``hidden`` - optional parameter, if set to ``true`` will hide SSID.
- ``max_connection`` - optional parameter to set max simultaneous connected stations, `from 0 to 8 <https://bbs.espressif.com/viewtopic.php?f=46&t=481&p=1832&hilit=max_connection#p1832>`__. Defaults to 4. Once the max number has been reached, any other station that wants to connect will be forced to wait until an already connected station disconnects. - ``max_connection`` - optional parameter to set max simultaneous connected stations, `from 0 to 8 <https://bbs.espressif.com/viewtopic.php?f=46&t=481&p=1832&hilit=max_connection#p1832>`__. Defaults to 4. Once the max number has been reached, any other station that wants to connect will be forced to wait until an already connected station disconnects.
@ -152,7 +152,7 @@ Disconnect stations from the network established by the soft-AP.
WiFi.softAPdisconnect(wifioff) WiFi.softAPdisconnect(wifioff)
Function will set currently configured SSID and password of the soft-AP to null values. The parameter ``wifioff`` is optional. If set to ``true`` it will switch the soft-AP mode off. Function will set currently configured SSID and pre-shared key of the soft-AP to null values. The parameter ``wifioff`` is optional. If set to ``true`` it will switch the soft-AP mode off.
Function will return ``true`` if operation was successful or ``false`` if otherwise. Function will return ``true`` if operation was successful or ``false`` if otherwise.

View File

@ -54,10 +54,13 @@ static bool softap_config_equal(const softap_config& lhs, const softap_config& r
* @return equal * @return equal
*/ */
static bool softap_config_equal(const softap_config& lhs, const softap_config& rhs) { static bool softap_config_equal(const softap_config& lhs, const softap_config& rhs) {
if(strcmp(reinterpret_cast<const char*>(lhs.ssid), reinterpret_cast<const char*>(rhs.ssid)) != 0) { if(lhs.ssid_len != rhs.ssid_len) {
return false; return false;
} }
if(strcmp(reinterpret_cast<const char*>(lhs.password), reinterpret_cast<const char*>(rhs.password)) != 0) { if(memcmp(lhs.ssid, rhs.ssid, lhs.ssid_len) != 0) {
return false;
}
if(strncmp(reinterpret_cast<const char*>(lhs.password), reinterpret_cast<const char*>(rhs.password), sizeof(softap_config::password)) != 0) {
return false; return false;
} }
if(lhs.channel != rhs.channel) { if(lhs.channel != rhs.channel) {
@ -85,13 +88,13 @@ static bool softap_config_equal(const softap_config& lhs, const softap_config& r
/** /**
* Set up an access point * Set up an access point
* @param ssid Pointer to the SSID (max 31 char). * @param ssid Pointer to the SSID (max 32 char).
* @param passphrase For WPA2 min 8 char, for open use NULL (max 63 char). * @param psk For WPA2 min 8 char max 64 char, for open use "" or NULL.
* @param channel WiFi channel number, 1 - 13. * @param channel WiFi channel number, 1 - 13.
* @param ssid_hidden Network cloaking (0 = broadcast SSID, 1 = hide SSID) * @param ssid_hidden Network cloaking (0 = broadcast SSID, 1 = hide SSID)
* @param max_connection Max simultaneous connected clients, 0 - 8. https://bbs.espressif.com/viewtopic.php?f=46&t=481&p=1832&hilit=max_connection#p1832 * @param max_connection Max simultaneous connected clients, 0 - 8. https://bbs.espressif.com/viewtopic.php?f=46&t=481&p=1832&hilit=max_connection#p1832
*/ */
bool ESP8266WiFiAPClass::softAP(const char* ssid, const char* passphrase, int channel, int ssid_hidden, int max_connection) { bool ESP8266WiFiAPClass::softAP(const char* ssid, const char* psk, int channel, int ssid_hidden, int max_connection) {
if(!WiFi.enableAP(true)) { if(!WiFi.enableAP(true)) {
// enable AP failed // enable AP failed
@ -99,36 +102,43 @@ bool ESP8266WiFiAPClass::softAP(const char* ssid, const char* passphrase, int ch
return false; return false;
} }
if(!ssid || strlen(ssid) == 0 || strlen(ssid) > 31) { size_t ssid_len = ssid ? strlen(ssid) : 0;
// fail SSID too long or missing! if(ssid_len == 0 || ssid_len > 32) {
DEBUG_WIFI("[AP] SSID too long or missing!\n"); DEBUG_WIFI("[AP] SSID length %u, too long or missing!\n", ssid_len);
return false; return false;
} }
if(passphrase && strlen(passphrase) > 0 && (strlen(passphrase) > 63 || strlen(passphrase) < 8)) { size_t psk_len = psk ? strlen(psk) : 0;
// fail passphrase to long or short! if(psk_len > 0 && (psk_len > 64 || psk_len < 8)) {
DEBUG_WIFI("[AP] fail passphrase too long or short!\n"); DEBUG_WIFI("[AP] fail psk length %u, too long or short!\n", psk_len);
return false; return false;
} }
bool ret = true; bool ret = true;
struct softap_config conf; struct softap_config conf;
strcpy(reinterpret_cast<char*>(conf.ssid), ssid); memcpy(reinterpret_cast<char*>(conf.ssid), ssid, ssid_len);
if (ssid_len < sizeof(conf.ssid)) {
conf.ssid[ssid_len] = 0;
}
conf.ssid_len = ssid_len;
if(psk_len) {
conf.authmode = AUTH_WPA2_PSK;
memcpy(reinterpret_cast<char*>(conf.password), psk, psk_len);
if (psk_len < sizeof(conf.password)) {
conf.password[psk_len] = 0;
}
} else {
conf.authmode = AUTH_OPEN;
conf.password[0] = 0;
}
conf.channel = channel; conf.channel = channel;
conf.ssid_len = strlen(ssid);
conf.ssid_hidden = ssid_hidden; conf.ssid_hidden = ssid_hidden;
conf.max_connection = max_connection; conf.max_connection = max_connection;
conf.beacon_interval = 100; conf.beacon_interval = 100;
if(!passphrase || strlen(passphrase) == 0) {
conf.authmode = AUTH_OPEN;
*conf.password = 0;
} else {
conf.authmode = AUTH_WPA2_PSK;
strcpy(reinterpret_cast<char*>(conf.password), passphrase);
}
struct softap_config conf_compare; struct softap_config conf_compare;
if(WiFi._persistent){ if(WiFi._persistent){
wifi_softap_get_config_default(&conf_compare); wifi_softap_get_config_default(&conf_compare);
@ -181,8 +191,8 @@ bool ESP8266WiFiAPClass::softAP(const char* ssid, const char* passphrase, int ch
return ret; return ret;
} }
bool ESP8266WiFiAPClass::softAP(const String& ssid, const String& passphrase, int channel, int ssid_hidden, int max_connection) { bool ESP8266WiFiAPClass::softAP(const String& ssid, const String& psk, int channel, int ssid_hidden, int max_connection) {
return softAP(ssid.c_str(), passphrase.c_str(), channel, ssid_hidden, max_connection); return softAP(ssid.c_str(), psk.c_str(), channel, ssid_hidden, max_connection);
} }
/** /**
@ -358,25 +368,24 @@ String ESP8266WiFiAPClass::softAPmacAddress(void) {
String ESP8266WiFiAPClass::softAPSSID() const { String ESP8266WiFiAPClass::softAPSSID() const {
struct softap_config config; struct softap_config config;
wifi_softap_get_config(&config); wifi_softap_get_config(&config);
char* name = reinterpret_cast<char*>(config.ssid);
char ssid[sizeof(config.ssid) + 1];
memcpy(ssid, name, sizeof(config.ssid));
ssid[sizeof(config.ssid)] = '\0';
return String(ssid); String ssid;
ssid.concat(reinterpret_cast<const char*>(config.ssid), config.ssid_len);
return ssid;
} }
/** /**
* Get the configured(Not-In-Flash) softAP PSK or PASSWORD. * Get the configured(Not-In-Flash) softAP PSK.
* @return String psk. * @return String psk.
*/ */
String ESP8266WiFiAPClass::softAPPSK() const { String ESP8266WiFiAPClass::softAPPSK() const {
struct softap_config config; struct softap_config config;
wifi_softap_get_config(&config); wifi_softap_get_config(&config);
char* pass = reinterpret_cast<char*>(config.password);
char psk[sizeof(config.password) + 1];
memcpy(psk, pass, sizeof(config.password));
psk[sizeof(config.password)] = '\0';
return String(psk); char* ptr = reinterpret_cast<char*>(config.password);
String psk;
psk.concat(ptr, strnlen(ptr, sizeof(config.password)));
return psk;
} }

View File

@ -36,8 +36,8 @@ class ESP8266WiFiAPClass {
public: public:
bool softAP(const char* ssid, const char* passphrase = NULL, int channel = 1, int ssid_hidden = 0, int max_connection = 4); bool softAP(const char* ssid, const char* psk = NULL, int channel = 1, int ssid_hidden = 0, int max_connection = 4);
bool softAP(const String& ssid,const String& passphrase = emptyString,int channel = 1,int ssid_hidden = 0,int max_connection = 4); bool softAP(const String& ssid,const String& psk = emptyString,int channel = 1,int ssid_hidden = 0,int max_connection = 4);
bool softAPConfig(IPAddress local_ip, IPAddress gateway, IPAddress subnet); bool softAPConfig(IPAddress local_ip, IPAddress gateway, IPAddress subnet);
bool softAPdisconnect(bool wifioff = false); bool softAPdisconnect(bool wifioff = false);