Skip to content

Commit 02259a4

Browse files
authored
fixed support for psk in WiFiSTA, added support for psk to WiFiMulti, minor code cleanups (#4076)
1 parent 332e059 commit 02259a4

File tree

3 files changed

+24
-25
lines changed

3 files changed

+24
-25
lines changed

libraries/ESP8266WiFi/src/ESP8266WiFiMulti.cpp

+7-13
Original file line numberDiff line numberDiff line change
@@ -38,10 +38,6 @@ bool ESP8266WiFiMulti::addAP(const char* ssid, const char *passphrase) {
3838
return APlistAdd(ssid, passphrase);
3939
}
4040

41-
int ESP8266WiFiMulti::count(void) {
42-
return APlist.size();
43-
}
44-
4541
wl_status_t ESP8266WiFiMulti::run(void) {
4642

4743
wl_status_t status = WiFi.status();
@@ -71,7 +67,7 @@ wl_status_t ESP8266WiFiMulti::run(void) {
7167

7268
if(scanResult > 0) {
7369
// scan done, analyze
74-
WifiAPlist_t bestNetwork { NULL, NULL };
70+
WifiAPEntry bestNetwork { NULL, NULL };
7571
int bestNetworkDb = INT_MIN;
7672
uint8 bestBSSID[6];
7773
int32_t bestChannel;
@@ -92,16 +88,14 @@ wl_status_t ESP8266WiFiMulti::run(void) {
9288
WiFi.getNetworkInfo(i, ssid_scan, sec_scan, rssi_scan, BSSID_scan, chan_scan, hidden_scan);
9389

9490
bool known = false;
95-
for(uint32_t x = 0; x < APlist.size(); x++) {
96-
WifiAPlist_t entry = APlist[x];
97-
91+
for(auto entry : APlist) {
9892
if(ssid_scan == entry.ssid) { // SSID match
9993
known = true;
10094
if(rssi_scan > bestNetworkDb) { // best network
10195
if(sec_scan == ENC_TYPE_NONE || entry.passphrase) { // check for passphrase if not open wlan
10296
bestNetworkDb = rssi_scan;
10397
bestChannel = chan_scan;
104-
memcpy((void*) &bestNetwork, (void*) &entry, sizeof(bestNetwork));
98+
bestNetwork = entry;
10599
memcpy((void*) &bestBSSID, (void*) BSSID_scan, sizeof(bestBSSID));
106100
}
107101
}
@@ -183,15 +177,16 @@ wl_status_t ESP8266WiFiMulti::run(void) {
183177

184178
bool ESP8266WiFiMulti::APlistAdd(const char* ssid, const char *passphrase) {
185179

186-
WifiAPlist_t newAP;
180+
WifiAPEntry newAP;
187181

188182
if(!ssid || *ssid == 0x00 || strlen(ssid) > 31) {
189183
// fail SSID to long or missing!
190184
DEBUG_WIFI_MULTI("[WIFI][APlistAdd] no ssid or ssid to long\n");
191185
return false;
192186
}
193187

194-
if(passphrase && strlen(passphrase) > 63) {
188+
//for passphrase, max is 63 ascii + null. For psk, 64hex + null.
189+
if(passphrase && strlen(passphrase) > 64) {
195190
// fail passphrase to long!
196191
DEBUG_WIFI_MULTI("[WIFI][APlistAdd] passphrase to long\n");
197192
return false;
@@ -222,8 +217,7 @@ bool ESP8266WiFiMulti::APlistAdd(const char* ssid, const char *passphrase) {
222217
}
223218

224219
void ESP8266WiFiMulti::APlistClean(void) {
225-
for(uint32_t i = 0; i < APlist.size(); i++) {
226-
WifiAPlist_t entry = APlist[i];
220+
for(auto entry : APlist) {
227221
if(entry.ssid) {
228222
free(entry.ssid);
229223
}

libraries/ESP8266WiFi/src/ESP8266WiFiMulti.h

+8-8
Original file line numberDiff line numberDiff line change
@@ -28,8 +28,6 @@
2828
#define WIFICLIENTMULTI_H_
2929

3030
#include "ESP8266WiFi.h"
31-
#undef min
32-
#undef max
3331
#include <vector>
3432

3533
#ifdef DEBUG_ESP_WIFI
@@ -42,22 +40,24 @@
4240
#define DEBUG_WIFI_MULTI(...)
4341
#endif
4442

45-
typedef struct {
46-
char * ssid;
47-
char * passphrase;
48-
} WifiAPlist_t;
43+
struct WifiAPEntry {
44+
char * ssid;
45+
char * passphrase;
46+
};
47+
48+
typedef std::vector<WifiAPEntry> WifiAPlist;
4949

5050
class ESP8266WiFiMulti {
5151
public:
5252
ESP8266WiFiMulti();
5353
~ESP8266WiFiMulti();
5454

5555
bool addAP(const char* ssid, const char *passphrase = NULL);
56-
int count(void);
56+
5757
wl_status_t run(void);
5858

5959
private:
60-
std::vector<WifiAPlist_t> APlist;
60+
WifiAPlist APlist;
6161
bool APlistAdd(const char* ssid, const char *passphrase = NULL);
6262
void APlistClean(void);
6363

libraries/ESP8266WiFi/src/ESP8266WiFiSTA.cpp

+9-4
Original file line numberDiff line numberDiff line change
@@ -26,14 +26,15 @@
2626
#include "ESP8266WiFiGeneric.h"
2727
#include "ESP8266WiFiSTA.h"
2828

29-
extern "C" {
3029
#include "c_types.h"
3130
#include "ets_sys.h"
3231
#include "os_type.h"
3332
#include "osapi.h"
3433
#include "mem.h"
3534
#include "user_interface.h"
3635
#include "smartconfig.h"
36+
37+
extern "C" {
3738
#include "lwip/err.h"
3839
#include "lwip/dns.h"
3940
#include "lwip/init.h" // LWIP_VERSION_
@@ -62,7 +63,8 @@ static bool sta_config_equal(const station_config& lhs, const station_config& rh
6263
return false;
6364
}
6465

65-
if(strcmp(reinterpret_cast<const char*>(lhs.password), reinterpret_cast<const char*>(rhs.password)) != 0) {
66+
//in case of password, use strncmp with size 64 to cover 64byte psk case (no null term)
67+
if(strncmp(reinterpret_cast<const char*>(lhs.password), reinterpret_cast<const char*>(rhs.password), sizeof(lhs.password)) != 0) {
6668
return false;
6769
}
6870

@@ -116,7 +118,7 @@ wl_status_t ESP8266WiFiSTAClass::begin(const char* ssid, const char *passphrase,
116118
strcpy(reinterpret_cast<char*>(conf.ssid), ssid);
117119

118120
if(passphrase) {
119-
if (strlen(passphrase) == 64) // it's not a passphrase, is the PSK
121+
if (strlen(passphrase) == 64) // it's not a passphrase, is the PSK, which is copied into conf.password without null term
120122
memcpy(reinterpret_cast<char*>(conf.password), passphrase, 64);
121123
else
122124
strcpy(reinterpret_cast<char*>(conf.password), passphrase);
@@ -514,7 +516,10 @@ String ESP8266WiFiSTAClass::SSID() const {
514516
String ESP8266WiFiSTAClass::psk() const {
515517
struct station_config conf;
516518
wifi_station_get_config(&conf);
517-
return String(reinterpret_cast<char*>(conf.password));
519+
char tmp[65]; //psk is 64 bytes hex => plus null term
520+
memcpy(tmp, conf.password, sizeof(conf.password));
521+
tmp[64] = 0; //null term in case of 64 byte psk
522+
return String(reinterpret_cast<char*>(tmp));
518523
}
519524

520525
/**

0 commit comments

Comments
 (0)