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

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

Merged
merged 9 commits into from
May 15, 2021
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
10 changes: 5 additions & 5 deletions doc/esp8266wifi/soft-access-point-class.rst
Original file line number Diff line number Diff line change
Expand Up @@ -40,18 +40,18 @@ terms <https://en.wikipedia.org/wiki/Function_overloading>`__) of this function

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

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.

Meaning of all parameters is as follows:

- ``ssid`` - character string containing network SSID (max. 31 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).
- ``ssid`` - character string containing network SSID (max. 32 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.
- ``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.
Expand Down Expand Up @@ -152,7 +152,7 @@ Disconnect stations from the network established by the soft-AP.

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.

Expand Down
77 changes: 43 additions & 34 deletions libraries/ESP8266WiFi/src/ESP8266WiFiAP.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -54,10 +54,13 @@ static bool softap_config_equal(const softap_config& lhs, const softap_config& r
* @return equal
*/
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;
}
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;
}
if(lhs.channel != rhs.channel) {
Expand Down Expand Up @@ -85,50 +88,57 @@ static bool softap_config_equal(const softap_config& lhs, const softap_config& r

/**
* Set up an access point
* @param ssid Pointer to the SSID (max 31 char).
* @param passphrase For WPA2 min 8 char, for open use NULL (max 63 char).
* @param ssid Pointer to the SSID (max 32 char).
* @param psk For WPA2 min 8 char max 64 char, for open use "" or NULL.
* @param channel WiFi channel number, 1 - 13.
* @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
*/
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)) {
// enable AP failed
DEBUG_WIFI("[AP] enableAP failed!\n");
return false;
}

if(!ssid || strlen(ssid) == 0 || strlen(ssid) > 31) {
// fail SSID too long or missing!
DEBUG_WIFI("[AP] SSID too long or missing!\n");
size_t ssid_len = ssid ? strlen(ssid) : 0;
if(ssid_len == 0 || ssid_len > 32) {
DEBUG_WIFI("[AP] SSID length %u, too long or missing!\n", ssid_len);
return false;
}

if(passphrase && strlen(passphrase) > 0 && (strlen(passphrase) > 63 || strlen(passphrase) < 8)) {
// fail passphrase to long or short!
DEBUG_WIFI("[AP] fail passphrase too long or short!\n");
size_t psk_len = psk ? strlen(psk) : 0;
if(psk_len > 0 && (psk_len > 64 || psk_len < 8)) {
DEBUG_WIFI("[AP] fail psk length %u, too long or short!\n", psk_len);
return false;
}

bool ret = true;

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.ssid_len = strlen(ssid);
conf.ssid_hidden = ssid_hidden;
conf.max_connection = max_connection;
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;
if(WiFi._persistent){
wifi_softap_get_config_default(&conf_compare);
Expand Down Expand Up @@ -181,8 +191,8 @@ bool ESP8266WiFiAPClass::softAP(const char* ssid, const char* passphrase, int ch
return ret;
}

bool ESP8266WiFiAPClass::softAP(const String& ssid, const String& passphrase, int channel, int ssid_hidden, int max_connection) {
return softAP(ssid.c_str(), passphrase.c_str(), channel, ssid_hidden, max_connection);
bool ESP8266WiFiAPClass::softAP(const String& ssid, const String& psk, int channel, int ssid_hidden, int max_connection) {
return softAP(ssid.c_str(), psk.c_str(), channel, ssid_hidden, max_connection);
}

/**
Expand Down Expand Up @@ -358,25 +368,24 @@ String ESP8266WiFiAPClass::softAPmacAddress(void) {
String ESP8266WiFiAPClass::softAPSSID() const {
struct softap_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);
mcspr marked this conversation as resolved.
Show resolved Hide resolved

return ssid;
}

/**
* Get the configured(Not-In-Flash) softAP PSK or PASSWORD.
* Get the configured(Not-In-Flash) softAP PSK.
* @return String psk.
*/
String ESP8266WiFiAPClass::softAPPSK() const {
struct softap_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;
}
4 changes: 2 additions & 2 deletions libraries/ESP8266WiFi/src/ESP8266WiFiAP.h
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,8 @@ class ESP8266WiFiAPClass {

public:

bool softAP(const char* ssid, const char* passphrase = 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 char* ssid, const char* psk = NULL, 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 softAPdisconnect(bool wifioff = false);

Expand Down