Skip to content

Commit 467584f

Browse files
committed
WiFi: clean up AP SSID setter / getters, support 32 chars
1 parent fdc295d commit 467584f

File tree

2 files changed

+29
-26
lines changed

2 files changed

+29
-26
lines changed

doc/esp8266wifi/soft-access-point-class.rst

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ The first parameter of this function is required, remaining four are optional.
5050

5151
Meaning of all parameters is as follows:
5252

53-
- ``ssid`` - character string containing network SSID (max. 31 characters)
53+
- ``ssid`` - character string containing network SSID (max. 32 characters)
5454
- ``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).
5555
- ``channel`` - optional parameter to set Wi-Fi channel, from 1 to 13. Default channel = 1.
5656
- ``hidden`` - optional parameter, if set to ``true`` will hide SSID.

libraries/ESP8266WiFi/src/ESP8266WiFiAP.cpp

Lines changed: 28 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -54,10 +54,13 @@ static bool softap_config_equal(const softap_config& lhs, const softap_config& r
5454
* @return equal
5555
*/
5656
static bool softap_config_equal(const softap_config& lhs, const softap_config& rhs) {
57-
if(strcmp(reinterpret_cast<const char*>(lhs.ssid), reinterpret_cast<const char*>(rhs.ssid)) != 0) {
57+
if(lhs.ssid_len != rhs.ssid_len) {
5858
return false;
5959
}
60-
if(strcmp(reinterpret_cast<const char*>(lhs.password), reinterpret_cast<const char*>(rhs.password)) != 0) {
60+
if(memcmp(lhs.ssid, rhs.ssid, lhs.ssid_len) != 0) {
61+
return false;
62+
}
63+
if(strncmp(reinterpret_cast<const char*>(lhs.password), reinterpret_cast<const char*>(rhs.password), sizeof(softap_config::password)) != 0) {
6164
return false;
6265
}
6366
if(lhs.channel != rhs.channel) {
@@ -85,7 +88,7 @@ static bool softap_config_equal(const softap_config& lhs, const softap_config& r
8588

8689
/**
8790
* Set up an access point
88-
* @param ssid Pointer to the SSID (max 31 char).
91+
* @param ssid Pointer to the SSID (max 32 char).
8992
* @param passphrase For WPA2 min 8 char, for open use NULL (max 63 char).
9093
* @param channel WiFi channel number, 1 - 13.
9194
* @param ssid_hidden Network cloaking (0 = broadcast SSID, 1 = hide SSID)
@@ -99,13 +102,15 @@ bool ESP8266WiFiAPClass::softAP(const char* ssid, const char* passphrase, int ch
99102
return false;
100103
}
101104

102-
if(!ssid || strlen(ssid) == 0 || strlen(ssid) > 31) {
105+
size_t ssid_len = strlen(ssid);
106+
if(!ssid || ssid_len == 0 || ssid_len > 32) {
103107
// fail SSID too long or missing!
104108
DEBUG_WIFI("[AP] SSID too long or missing!\n");
105109
return false;
106110
}
107111

108-
if(passphrase && strlen(passphrase) > 0 && (strlen(passphrase) > 63 || strlen(passphrase) < 8)) {
112+
size_t pass_len = strlen(passphrase);
113+
if(passphrase && pass_len > 0 && (pass_len > 63 || pass_len < 8)) {
109114
// fail passphrase to long or short!
110115
DEBUG_WIFI("[AP] fail passphrase too long or short!\n");
111116
return false;
@@ -114,21 +119,25 @@ bool ESP8266WiFiAPClass::softAP(const char* ssid, const char* passphrase, int ch
114119
bool ret = true;
115120

116121
struct softap_config conf;
117-
strcpy(reinterpret_cast<char*>(conf.ssid), ssid);
118-
conf.channel = channel;
119-
conf.ssid_len = strlen(ssid);
120-
conf.ssid_hidden = ssid_hidden;
121-
conf.max_connection = max_connection;
122-
conf.beacon_interval = 100;
122+
memcpy(reinterpret_cast<char*>(conf.ssid), ssid, ssid_len);
123+
if (ssid_len < 32) {
124+
conf.ssid[ssid_len] = '\0';
125+
}
126+
conf.ssid_len = ssid_len;
123127

124-
if(!passphrase || strlen(passphrase) == 0) {
128+
if(!passphrase || pass_len == 0) {
125129
conf.authmode = AUTH_OPEN;
126130
*conf.password = 0;
127131
} else {
128132
conf.authmode = AUTH_WPA2_PSK;
129133
strcpy(reinterpret_cast<char*>(conf.password), passphrase);
130134
}
131135

136+
conf.channel = channel;
137+
conf.ssid_hidden = ssid_hidden;
138+
conf.max_connection = max_connection;
139+
conf.beacon_interval = 100;
140+
132141
struct softap_config conf_compare;
133142
if(WiFi._persistent){
134143
wifi_softap_get_config_default(&conf_compare);
@@ -358,25 +367,19 @@ String ESP8266WiFiAPClass::softAPmacAddress(void) {
358367
String ESP8266WiFiAPClass::softAPSSID() const {
359368
struct softap_config config;
360369
wifi_softap_get_config(&config);
361-
char* name = reinterpret_cast<char*>(config.ssid);
362-
char ssid[sizeof(config.ssid) + 1];
363-
memcpy(ssid, name, sizeof(config.ssid));
364-
ssid[sizeof(config.ssid)] = '\0';
365-
366-
return String(ssid);
370+
String ssid;
371+
ssid.concat(reinterpret_cast<const char*>(config.ssid), config.ssid_len);
372+
return ssid;
367373
}
368374

369375
/**
370-
* Get the configured(Not-In-Flash) softAP PSK or PASSWORD.
371-
* @return String psk.
376+
* Get the configured(Not-In-Flash) softAP PASSWORD.
377+
* @return String password.
372378
*/
373379
String ESP8266WiFiAPClass::softAPPSK() const {
374380
struct softap_config config;
375381
wifi_softap_get_config(&config);
376382
char* pass = reinterpret_cast<char*>(config.password);
377-
char psk[sizeof(config.password) + 1];
378-
memcpy(psk, pass, sizeof(config.password));
379-
psk[sizeof(config.password)] = '\0';
380-
381-
return String(psk);
383+
pass[sizeof(config.password) - 1] = '\0'; // it is impossible to set PSK through the Arduino API because we only support WPA2
384+
return pass;
382385
}

0 commit comments

Comments
 (0)