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 1 commit
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
2 changes: 1 addition & 1 deletion doc/esp8266wifi/soft-access-point-class.rst
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ 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)
- ``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).
- ``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.
Expand Down
53 changes: 28 additions & 25 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,7 +88,7 @@ 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 ssid Pointer to the SSID (max 32 char).
* @param passphrase For WPA2 min 8 char, for open use NULL (max 63 char).
* @param channel WiFi channel number, 1 - 13.
* @param ssid_hidden Network cloaking (0 = broadcast SSID, 1 = hide SSID)
Expand All @@ -99,13 +102,15 @@ bool ESP8266WiFiAPClass::softAP(const char* ssid, const char* passphrase, int ch
return false;
}

if(!ssid || strlen(ssid) == 0 || strlen(ssid) > 31) {
size_t ssid_len = strlen(ssid);
mcspr marked this conversation as resolved.
Show resolved Hide resolved
if(!ssid || ssid_len == 0 || ssid_len > 32) {
// fail SSID too long or missing!
DEBUG_WIFI("[AP] SSID too long or missing!\n");
return false;
}

if(passphrase && strlen(passphrase) > 0 && (strlen(passphrase) > 63 || strlen(passphrase) < 8)) {
size_t pass_len = strlen(passphrase);
mcspr marked this conversation as resolved.
Show resolved Hide resolved
if(passphrase && pass_len > 0 && (pass_len > 63 || pass_len < 8)) {
// fail passphrase to long or short!
DEBUG_WIFI("[AP] fail passphrase too long or short!\n");
return false;
Expand All @@ -114,21 +119,25 @@ bool ESP8266WiFiAPClass::softAP(const char* ssid, const char* passphrase, int ch
bool ret = true;

struct softap_config conf;
strcpy(reinterpret_cast<char*>(conf.ssid), ssid);
conf.channel = channel;
conf.ssid_len = strlen(ssid);
conf.ssid_hidden = ssid_hidden;
conf.max_connection = max_connection;
conf.beacon_interval = 100;
memcpy(reinterpret_cast<char*>(conf.ssid), ssid, ssid_len);
if (ssid_len < 32) {
conf.ssid[ssid_len] = '\0';
}
conf.ssid_len = ssid_len;

if(!passphrase || strlen(passphrase) == 0) {
if(!passphrase || pass_len == 0) {
conf.authmode = AUTH_OPEN;
*conf.password = 0;
} else {
conf.authmode = AUTH_WPA2_PSK;
strcpy(reinterpret_cast<char*>(conf.password), passphrase);
}

conf.channel = channel;
conf.ssid_hidden = ssid_hidden;
conf.max_connection = max_connection;
conf.beacon_interval = 100;

struct softap_config conf_compare;
if(WiFi._persistent){
wifi_softap_get_config_default(&conf_compare);
Expand Down Expand Up @@ -358,25 +367,19 @@ 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.
* @return String psk.
* Get the configured(Not-In-Flash) softAP PASSWORD.
* @return String password.
*/
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);
pass[sizeof(config.password) - 1] = '\0'; // it is impossible to set PSK through the Arduino API because we only support WPA2
return pass;
mcspr marked this conversation as resolved.
Show resolved Hide resolved
}