Skip to content

Commit 4436e32

Browse files
authored
WiFi: clean up AP SSID setter & getter, support 32 chars (#7941)
1 parent 5f21c61 commit 4436e32

File tree

3 files changed

+50
-41
lines changed

3 files changed

+50
-41
lines changed

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

+5-5
Original file line numberDiff line numberDiff line change
@@ -40,18 +40,18 @@ terms <https://en.wikipedia.org/wiki/Function_overloading>`__) of this function
4040
4141
WiFi.softAP(ssid)
4242
43-
To set up password protected network, or to configure additional network parameters, use the following overload:
43+
To set up pre-shared key protected network, or to configure additional network parameters, use the following overload:
4444

4545
.. code:: cpp
4646
47-
WiFi.softAP(ssid, password, channel, hidden, max_connection)
47+
WiFi.softAP(ssid, psk, channel, hidden, max_connection)
4848
4949
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)
54-
- ``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).
53+
- ``ssid`` - character string containing network SSID (max. 32 characters)
54+
- ``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.
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.
5757
- ``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.
152152
153153
WiFi.softAPdisconnect(wifioff)
154154
155-
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.
155+
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.
156156

157157
Function will return ``true`` if operation was successful or ``false`` if otherwise.
158158

libraries/ESP8266WiFi/src/ESP8266WiFiAP.cpp

+43-34
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,50 +88,57 @@ 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).
89-
* @param passphrase For WPA2 min 8 char, for open use NULL (max 63 char).
91+
* @param ssid Pointer to the SSID (max 32 char).
92+
* @param psk For WPA2 min 8 char max 64 char, for open use "" or NULL.
9093
* @param channel WiFi channel number, 1 - 13.
9194
* @param ssid_hidden Network cloaking (0 = broadcast SSID, 1 = hide SSID)
9295
* @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
9396
*/
94-
bool ESP8266WiFiAPClass::softAP(const char* ssid, const char* passphrase, int channel, int ssid_hidden, int max_connection) {
97+
bool ESP8266WiFiAPClass::softAP(const char* ssid, const char* psk, int channel, int ssid_hidden, int max_connection) {
9598

9699
if(!WiFi.enableAP(true)) {
97100
// enable AP failed
98101
DEBUG_WIFI("[AP] enableAP failed!\n");
99102
return false;
100103
}
101104

102-
if(!ssid || strlen(ssid) == 0 || strlen(ssid) > 31) {
103-
// fail SSID too long or missing!
104-
DEBUG_WIFI("[AP] SSID too long or missing!\n");
105+
size_t ssid_len = ssid ? strlen(ssid) : 0;
106+
if(ssid_len == 0 || ssid_len > 32) {
107+
DEBUG_WIFI("[AP] SSID length %u, too long or missing!\n", ssid_len);
105108
return false;
106109
}
107110

108-
if(passphrase && strlen(passphrase) > 0 && (strlen(passphrase) > 63 || strlen(passphrase) < 8)) {
109-
// fail passphrase to long or short!
110-
DEBUG_WIFI("[AP] fail passphrase too long or short!\n");
111+
size_t psk_len = psk ? strlen(psk) : 0;
112+
if(psk_len > 0 && (psk_len > 64 || psk_len < 8)) {
113+
DEBUG_WIFI("[AP] fail psk length %u, too long or short!\n", psk_len);
111114
return false;
112115
}
113116

114117
bool ret = true;
115118

116119
struct softap_config conf;
117-
strcpy(reinterpret_cast<char*>(conf.ssid), ssid);
120+
memcpy(reinterpret_cast<char*>(conf.ssid), ssid, ssid_len);
121+
if (ssid_len < sizeof(conf.ssid)) {
122+
conf.ssid[ssid_len] = 0;
123+
}
124+
conf.ssid_len = ssid_len;
125+
126+
if(psk_len) {
127+
conf.authmode = AUTH_WPA2_PSK;
128+
memcpy(reinterpret_cast<char*>(conf.password), psk, psk_len);
129+
if (psk_len < sizeof(conf.password)) {
130+
conf.password[psk_len] = 0;
131+
}
132+
} else {
133+
conf.authmode = AUTH_OPEN;
134+
conf.password[0] = 0;
135+
}
136+
118137
conf.channel = channel;
119-
conf.ssid_len = strlen(ssid);
120138
conf.ssid_hidden = ssid_hidden;
121139
conf.max_connection = max_connection;
122140
conf.beacon_interval = 100;
123141

124-
if(!passphrase || strlen(passphrase) == 0) {
125-
conf.authmode = AUTH_OPEN;
126-
*conf.password = 0;
127-
} else {
128-
conf.authmode = AUTH_WPA2_PSK;
129-
strcpy(reinterpret_cast<char*>(conf.password), passphrase);
130-
}
131-
132142
struct softap_config conf_compare;
133143
if(WiFi._persistent){
134144
wifi_softap_get_config_default(&conf_compare);
@@ -181,8 +191,8 @@ bool ESP8266WiFiAPClass::softAP(const char* ssid, const char* passphrase, int ch
181191
return ret;
182192
}
183193

184-
bool ESP8266WiFiAPClass::softAP(const String& ssid, const String& passphrase, int channel, int ssid_hidden, int max_connection) {
185-
return softAP(ssid.c_str(), passphrase.c_str(), channel, ssid_hidden, max_connection);
194+
bool ESP8266WiFiAPClass::softAP(const String& ssid, const String& psk, int channel, int ssid_hidden, int max_connection) {
195+
return softAP(ssid.c_str(), psk.c_str(), channel, ssid_hidden, max_connection);
186196
}
187197

188198
/**
@@ -358,25 +368,24 @@ String ESP8266WiFiAPClass::softAPmacAddress(void) {
358368
String ESP8266WiFiAPClass::softAPSSID() const {
359369
struct softap_config config;
360370
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';
365371

366-
return String(ssid);
372+
String ssid;
373+
ssid.concat(reinterpret_cast<const char*>(config.ssid), config.ssid_len);
374+
375+
return ssid;
367376
}
368377

369378
/**
370-
* Get the configured(Not-In-Flash) softAP PSK or PASSWORD.
379+
* Get the configured(Not-In-Flash) softAP PSK.
371380
* @return String psk.
372381
*/
373382
String ESP8266WiFiAPClass::softAPPSK() const {
374383
struct softap_config config;
375384
wifi_softap_get_config(&config);
376-
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';
380385

381-
return String(psk);
386+
char* ptr = reinterpret_cast<char*>(config.password);
387+
String psk;
388+
psk.concat(ptr, strnlen(ptr, sizeof(config.password)));
389+
390+
return psk;
382391
}

libraries/ESP8266WiFi/src/ESP8266WiFiAP.h

+2-2
Original file line numberDiff line numberDiff line change
@@ -36,8 +36,8 @@ class ESP8266WiFiAPClass {
3636

3737
public:
3838

39-
bool softAP(const char* ssid, const char* passphrase = NULL, int channel = 1, int ssid_hidden = 0, int max_connection = 4);
40-
bool softAP(const String& ssid,const String& passphrase = emptyString,int channel = 1,int ssid_hidden = 0,int max_connection = 4);
39+
bool softAP(const char* ssid, const char* psk = NULL, int channel = 1, int ssid_hidden = 0, int max_connection = 4);
40+
bool softAP(const String& ssid,const String& psk = emptyString,int channel = 1,int ssid_hidden = 0,int max_connection = 4);
4141
bool softAPConfig(IPAddress local_ip, IPAddress gateway, IPAddress subnet);
4242
bool softAPdisconnect(bool wifioff = false);
4343

0 commit comments

Comments
 (0)