Skip to content

Commit 8bba2fe

Browse files
committed
upd
remove comment about passphrase & wpa2, retrieve 64byte psk from the sdk update softAP to accept 64byte psk rename password -> psk update docs
1 parent 467584f commit 8bba2fe

File tree

3 files changed

+34
-28
lines changed

3 files changed

+34
-28
lines changed

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

Lines changed: 4 additions & 4 deletions
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

5353
- ``ssid`` - character string containing network SSID (max. 32 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).
54+
- ``psk`` - optional character string with a pre-shared key. For WPA2-PSK network it hould 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 psk 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

Lines changed: 28 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -89,12 +89,12 @@ static bool softap_config_equal(const softap_config& lhs, const softap_config& r
8989
/**
9090
* Set up an access point
9191
* @param ssid Pointer to the SSID (max 32 char).
92-
* @param passphrase For WPA2 min 8 char, for open use NULL (max 63 char).
92+
* @param psk For WPA2 min 8 char max 64 char, for open use "" or NULL.
9393
* @param channel WiFi channel number, 1 - 13.
9494
* @param ssid_hidden Network cloaking (0 = broadcast SSID, 1 = hide SSID)
9595
* @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
9696
*/
97-
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) {
9898

9999
if(!WiFi.enableAP(true)) {
100100
// enable AP failed
@@ -103,16 +103,14 @@ bool ESP8266WiFiAPClass::softAP(const char* ssid, const char* passphrase, int ch
103103
}
104104

105105
size_t ssid_len = strlen(ssid);
106-
if(!ssid || ssid_len == 0 || ssid_len > 32) {
107-
// fail SSID too long or missing!
108-
DEBUG_WIFI("[AP] SSID too long or missing!\n");
106+
if(ssid_len == 0 || ssid_len > 32) {
107+
DEBUG_WIFI("[AP] SSID length %u, too long or missing!\n", ssid_len);
109108
return false;
110109
}
111110

112-
size_t pass_len = strlen(passphrase);
113-
if(passphrase && pass_len > 0 && (pass_len > 63 || pass_len < 8)) {
114-
// fail passphrase to long or short!
115-
DEBUG_WIFI("[AP] fail passphrase too long or short!\n");
111+
size_t psk_len = strlen(psk);
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);
116114
return false;
117115
}
118116

@@ -121,16 +119,19 @@ bool ESP8266WiFiAPClass::softAP(const char* ssid, const char* passphrase, int ch
121119
struct softap_config conf;
122120
memcpy(reinterpret_cast<char*>(conf.ssid), ssid, ssid_len);
123121
if (ssid_len < 32) {
124-
conf.ssid[ssid_len] = '\0';
122+
conf.ssid[ssid_len] = 0;
125123
}
126124
conf.ssid_len = ssid_len;
127125

128-
if(!passphrase || pass_len == 0) {
129-
conf.authmode = AUTH_OPEN;
130-
*conf.password = 0;
131-
} else {
126+
if(psk_len) {
132127
conf.authmode = AUTH_WPA2_PSK;
133-
strcpy(reinterpret_cast<char*>(conf.password), passphrase);
128+
memcpy(reinterpret_cast<char*>(conf.password), psk, psk_len);
129+
if (psk_len < 63) {
130+
conf.password[psk_len] = 0;
131+
}
132+
} else {
133+
conf.authmode = AUTH_OPEN;
134+
conf.password[0] = 0;
134135
}
135136

136137
conf.channel = channel;
@@ -190,8 +191,8 @@ bool ESP8266WiFiAPClass::softAP(const char* ssid, const char* passphrase, int ch
190191
return ret;
191192
}
192193

193-
bool ESP8266WiFiAPClass::softAP(const String& ssid, const String& passphrase, int channel, int ssid_hidden, int max_connection) {
194-
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);
195196
}
196197

197198
/**
@@ -367,19 +368,24 @@ String ESP8266WiFiAPClass::softAPmacAddress(void) {
367368
String ESP8266WiFiAPClass::softAPSSID() const {
368369
struct softap_config config;
369370
wifi_softap_get_config(&config);
371+
370372
String ssid;
371373
ssid.concat(reinterpret_cast<const char*>(config.ssid), config.ssid_len);
374+
372375
return ssid;
373376
}
374377

375378
/**
376-
* Get the configured(Not-In-Flash) softAP PASSWORD.
377-
* @return String password.
379+
* Get the configured(Not-In-Flash) softAP PSK.
380+
* @return String psk.
378381
*/
379382
String ESP8266WiFiAPClass::softAPPSK() const {
380383
struct softap_config config;
381384
wifi_softap_get_config(&config);
382-
char* pass = reinterpret_cast<char*>(config.password);
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;
385+
386+
char* ptr = reinterpret_cast<char*>(config.password);
387+
String psk;
388+
psk.concat(ptr, strnlen(ptr, sizeof(config.password));
389+
390+
return psk;
385391
}

libraries/ESP8266WiFi/src/ESP8266WiFiAP.h

Lines changed: 2 additions & 2 deletions
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)