1
0
mirror of https://github.com/esp8266/Arduino.git synced 2025-06-29 16:03:14 +03:00

Check if AP exists before adding it (#5373)

This commit is contained in:
dav1901
2018-11-28 03:01:15 +02:00
committed by Develo
parent 5c4db3acf4
commit 5fcb8f1dac
3 changed files with 38 additions and 4 deletions

View File

@ -69,6 +69,7 @@ softAPgetStationNum KEYWORD2
#ESP8266WiFiMulti #ESP8266WiFiMulti
addAP KEYWORD2 addAP KEYWORD2
existsAP KEYWORD2
run KEYWORD2 run KEYWORD2
#ESP8266WiFiScan #ESP8266WiFiScan

View File

@ -38,6 +38,10 @@ bool ESP8266WiFiMulti::addAP(const char* ssid, const char *passphrase) {
return APlistAdd(ssid, passphrase); return APlistAdd(ssid, passphrase);
} }
bool ESP8266WiFiMulti::existsAP(const char* ssid, const char *passphrase) {
return APlistExists(ssid, passphrase);
}
wl_status_t ESP8266WiFiMulti::run(void) { wl_status_t ESP8266WiFiMulti::run(void) {
wl_status_t status = WiFi.status(); wl_status_t status = WiFi.status();
@ -184,18 +188,23 @@ bool ESP8266WiFiMulti::APlistAdd(const char* ssid, const char *passphrase) {
WifiAPEntry newAP; WifiAPEntry newAP;
if(!ssid || *ssid == 0x00 || strlen(ssid) > 31) { if(!ssid || *ssid == 0x00 || strlen(ssid) > 31) {
// fail SSID to long or missing! // fail SSID too long or missing!
DEBUG_WIFI_MULTI("[WIFI][APlistAdd] no ssid or ssid to long\n"); DEBUG_WIFI_MULTI("[WIFI][APlistAdd] no ssid or ssid too long\n");
return false; return false;
} }
//for passphrase, max is 63 ascii + null. For psk, 64hex + null. //for passphrase, max is 63 ascii + null. For psk, 64hex + null.
if(passphrase && strlen(passphrase) > 64) { if(passphrase && strlen(passphrase) > 64) {
// fail passphrase to long! // fail passphrase too long!
DEBUG_WIFI_MULTI("[WIFI][APlistAdd] passphrase to long\n"); DEBUG_WIFI_MULTI("[WIFI][APlistAdd] passphrase too long\n");
return false; return false;
} }
if(APlistExists(ssid, passphrase)) {
DEBUG_WIFI_MULTI("[WIFI][APlistAdd] SSID: %s already exists\n", ssid);
return true;
}
newAP.ssid = strdup(ssid); newAP.ssid = strdup(ssid);
if(!newAP.ssid) { if(!newAP.ssid) {
@ -220,6 +229,28 @@ bool ESP8266WiFiMulti::APlistAdd(const char* ssid, const char *passphrase) {
return true; return true;
} }
bool ESP8266WiFiMulti::APlistExists(const char* ssid, const char *passphrase) {
if(!ssid || *ssid == 0x00 || strlen(ssid) > 31) {
// fail SSID too long or missing!
DEBUG_WIFI_MULTI("[WIFI][APlistExists] no ssid or ssid too long\n");
return false;
}
for(auto entry : APlist) {
if(!strcmp(entry.ssid, ssid)) {
if(!passphrase) {
if(!strcmp(entry.passphrase, "")) {
return true;
}
} else {
if(!strcmp(entry.passphrase, passphrase)) {
return true;
}
}
}
}
return false;
}
void ESP8266WiFiMulti::APlistClean(void) { void ESP8266WiFiMulti::APlistClean(void) {
for(auto entry : APlist) { for(auto entry : APlist) {
if(entry.ssid) { if(entry.ssid) {

View File

@ -53,12 +53,14 @@ class ESP8266WiFiMulti {
~ESP8266WiFiMulti(); ~ESP8266WiFiMulti();
bool addAP(const char* ssid, const char *passphrase = NULL); bool addAP(const char* ssid, const char *passphrase = NULL);
bool existsAP(const char* ssid, const char *passphrase = NULL);
wl_status_t run(void); wl_status_t run(void);
private: private:
WifiAPlist APlist; WifiAPlist APlist;
bool APlistAdd(const char* ssid, const char *passphrase = NULL); bool APlistAdd(const char* ssid, const char *passphrase = NULL);
bool APlistExists(const char* ssid, const char *passphrase = NULL);
void APlistClean(void); void APlistClean(void);
}; };