8000 Check if AP exists before adding it by dav1901 · Pull Request #5373 · esp8266/Arduino · GitHub
[go: up one dir, main page]

Skip to content

Check if AP exists before adding it #5373

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Nov 28, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions libraries/ESP8266WiFi/keywords.txt
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@ softAPgetStationNum KEYWORD2

#ESP8266WiFiMulti
addAP KEYWORD2
existsAP KEYWORD2
run KEYWORD2

#ESP8266WiFiScan
Expand Down
39 changes: 35 additions & 4 deletions libraries/ESP8266WiFi/src/ESP8266WiFiMulti.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,10 @@ bool ESP8266WiFiMulti::addAP(const char* ssid, const char *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 status = WiFi.status();
Expand Down Expand Up @@ -184,18 +188,23 @@ bool ESP8266WiFiMulti::APlistAdd(const char* ssid, const char *passphrase) {
WifiAPEntry newAP;

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

//for passphrase, max is 63 ascii + null. For psk, 64hex + null.
if(passphrase && strlen(passphrase) > 64) {
// fail passphrase to long!
DEBUG_WIFI_MULTI("[WIFI][APlistAdd] passphrase to long\n");
// fail passphrase too long!
DEBUG_WIFI_MULTI("[WIFI][APlistAdd] passphrase too long\n");
return false;
}

if(APlistExists(ssid, passphrase)) {
DEBUG_WIFI_MULTI("[WIFI][APlistAdd] SSID: %s already exists\n", ssid);
return true;
}

newAP.ssid = strdup(ssid);

if(!newAP.ssid) {
Expand All @@ -220,6 +229,28 @@ bool ESP8266WiFiMulti::APlistAdd(const char* ssid, const char *passphrase) {
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) {
for(auto entry : APlist) {
if(entry.ssid) {
Expand Down
2 changes: 2 additions & 0 deletions libraries/ESP8266WiFi/src/ESP8266WiFiMulti.h
Original file line number Diff line number Diff line change
Expand Up @@ -53,12 +53,14 @@ class ESP8266WiFiMulti {
~ESP8266WiFiMulti();

bool addAP(const char* ssid, const char *passphrase = NULL);
bool existsAP(const char* ssid, const char *passphrase = NULL);

wl_status_t run(void);

private:
WifiAPlist APlist;
bool APlistAdd(const char* ssid, const char *passphrase = NULL);
bool APlistExists(const char* ssid, const char *passphrase = NULL);
void APlistClean(void);

};
Expand Down
0