From c44c4d21e9ef9138a0178b381053b40575490c10 Mon Sep 17 00:00:00 2001 From: Krzysiek S Date: Mon, 12 Dec 2022 21:11:18 +0100 Subject: [PATCH 1/6] Add eraseap option to WiFi disconnect method. --- libraries/ESP8266WiFi/src/ESP8266WiFiSTA.cpp | 12 +++++++++--- libraries/ESP8266WiFi/src/ESP8266WiFiSTA.h | 2 +- 2 files changed, 10 insertions(+), 4 deletions(-) diff --git a/libraries/ESP8266WiFi/src/ESP8266WiFiSTA.cpp b/libraries/ESP8266WiFi/src/ESP8266WiFiSTA.cpp index 4a5d4d963f..ce4d54f2df 100644 --- a/libraries/ESP8266WiFi/src/ESP8266WiFiSTA.cpp +++ b/libraries/ESP8266WiFi/src/ESP8266WiFiSTA.cpp @@ -357,11 +357,17 @@ bool ESP8266WiFiSTAClass::reconnect() { * @param wifioff * @return one value of wl_status_t enum */ -bool ESP8266WiFiSTAClass::disconnect(bool wifioff) { +bool ESP8266WiFiSTAClass::disconnect(bool wifioff, bool eraseap) { bool ret = false; + + // Read current config. struct station_config conf; - *conf.ssid = 0; - *conf.password = 0; + wifi_station_get_config(&conf); + + if (eraseap) { + memset(&conf.ssid, 0, sizeof(conf.ssid)); + memset(&conf.password, 0, sizeof(conf.password)); + }; // API Reference: wifi_station_disconnect() need to be called after system initializes and the ESP8266 Station mode is enabled. if (WiFi.getMode() & WIFI_STA) diff --git a/libraries/ESP8266WiFi/src/ESP8266WiFiSTA.h b/libraries/ESP8266WiFi/src/ESP8266WiFiSTA.h index 32813a5377..b607b8849e 100644 --- a/libraries/ESP8266WiFi/src/ESP8266WiFiSTA.h +++ b/libraries/ESP8266WiFi/src/ESP8266WiFiSTA.h @@ -48,7 +48,7 @@ class ESP8266WiFiSTAClass: public LwipIntf { bool config(IPAddress local_ip, IPAddress gateway, IPAddress subnet, IPAddress dns1 = (uint32_t)0x00000000, IPAddress dns2 = (uint32_t)0x00000000); bool reconnect(); - bool disconnect(bool wifioff = false); + bool disconnect(bool wifioff = false, bool eraseap = false); bool isConnected(); From 21f41118f3321c00c4de76fa47ee8585ed03c6d6 Mon Sep 17 00:00:00 2001 From: Krzysiek S Date: Wed, 14 Dec 2022 11:35:57 +0100 Subject: [PATCH 2/6] Add another method as suggested in PR review. --- libraries/ESP8266WiFi/src/ESP8266WiFiSTA.cpp | 11 +++++++++++ libraries/ESP8266WiFi/src/ESP8266WiFiSTA.h | 2 ++ 2 files changed, 13 insertions(+) diff --git a/libraries/ESP8266WiFi/src/ESP8266WiFiSTA.cpp b/libraries/ESP8266WiFi/src/ESP8266WiFiSTA.cpp index ce4d54f2df..395a9f1bb7 100644 --- a/libraries/ESP8266WiFi/src/ESP8266WiFiSTA.cpp +++ b/libraries/ESP8266WiFi/src/ESP8266WiFiSTA.cpp @@ -357,6 +357,17 @@ bool ESP8266WiFiSTAClass::reconnect() { * @param wifioff * @return one value of wl_status_t enum */ +bool ESP8266WiFiSTAClass::disconnect(bool wifioff, bool eraseap) { +{ + return disconnect(wifioff, false); +} + +/** + * Disconnect from the network + * @param wifioff + * @param eraseap + * @return one value of wl_status_t enum + */ bool ESP8266WiFiSTAClass::disconnect(bool wifioff, bool eraseap) { bool ret = false; diff --git a/libraries/ESP8266WiFi/src/ESP8266WiFiSTA.h b/libraries/ESP8266WiFi/src/ESP8266WiFiSTA.h index b607b8849e..dda40da254 100644 --- a/libraries/ESP8266WiFi/src/ESP8266WiFiSTA.h +++ b/libraries/ESP8266WiFi/src/ESP8266WiFiSTA.h @@ -48,6 +48,8 @@ class ESP8266WiFiSTAClass: public LwipIntf { bool config(IPAddress local_ip, IPAddress gateway, IPAddress subnet, IPAddress dns1 = (uint32_t)0x00000000, IPAddress dns2 = (uint32_t)0x00000000); bool reconnect(); + + bool disconnect(bool wifioff = false); bool disconnect(bool wifioff = false, bool eraseap = false); bool isConnected(); From 006e17d973f86a35aa2d7a2513f116062843808b Mon Sep 17 00:00:00 2001 From: Krzysiek S Date: Wed, 14 Dec 2022 14:31:59 +0100 Subject: [PATCH 3/6] Fix typos --- libraries/ESP8266WiFi/src/ESP8266WiFiSTA.cpp | 2 +- libraries/ESP8266WiFi/src/ESP8266WiFiSTA.h | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/libraries/ESP8266WiFi/src/ESP8266WiFiSTA.cpp b/libraries/ESP8266WiFi/src/ESP8266WiFiSTA.cpp index 395a9f1bb7..35065fbfc4 100644 --- a/libraries/ESP8266WiFi/src/ESP8266WiFiSTA.cpp +++ b/libraries/ESP8266WiFi/src/ESP8266WiFiSTA.cpp @@ -357,7 +357,7 @@ bool ESP8266WiFiSTAClass::reconnect() { * @param wifioff * @return one value of wl_status_t enum */ -bool ESP8266WiFiSTAClass::disconnect(bool wifioff, bool eraseap) { +bool ESP8266WiFiSTAClass::disconnect(bool wifioff) { { return disconnect(wifioff, false); } diff --git a/libraries/ESP8266WiFi/src/ESP8266WiFiSTA.h b/libraries/ESP8266WiFi/src/ESP8266WiFiSTA.h index dda40da254..23bfe42fa0 100644 --- a/libraries/ESP8266WiFi/src/ESP8266WiFiSTA.h +++ b/libraries/ESP8266WiFi/src/ESP8266WiFiSTA.h @@ -50,7 +50,7 @@ class ESP8266WiFiSTAClass: public LwipIntf { bool reconnect(); bool disconnect(bool wifioff = false); - bool disconnect(bool wifioff = false, bool eraseap = false); + bool disconnect(bool wifioff, bool eraseap); bool isConnected(); From e2f50e50c0d1568fffe7051771266d6048f3b451 Mon Sep 17 00:00:00 2001 From: Krzysiek S Date: Wed, 14 Dec 2022 15:18:11 +0100 Subject: [PATCH 4/6] Remove unnecessary semicolon --- libraries/ESP8266WiFi/src/ESP8266WiFiSTA.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/libraries/ESP8266WiFi/src/ESP8266WiFiSTA.cpp b/libraries/ESP8266WiFi/src/ESP8266WiFiSTA.cpp index 35065fbfc4..09ef7b6603 100644 --- a/libraries/ESP8266WiFi/src/ESP8266WiFiSTA.cpp +++ b/libraries/ESP8266WiFi/src/ESP8266WiFiSTA.cpp @@ -378,7 +378,7 @@ bool ESP8266WiFiSTAClass::disconnect(bool wifioff, bool eraseap) { if (eraseap) { memset(&conf.ssid, 0, sizeof(conf.ssid)); memset(&conf.password, 0, sizeof(conf.password)); - }; + } // API Reference: wifi_station_disconnect() need to be called after system initializes and the ESP8266 Station mode is enabled. if (WiFi.getMode() & WIFI_STA) From 60631144820c32793a4ad9febe2c38b708746e53 Mon Sep 17 00:00:00 2001 From: Krzysiek S Date: Wed, 14 Dec 2022 21:10:00 +0100 Subject: [PATCH 5/6] Fix brackets --- libraries/ESP8266WiFi/src/ESP8266WiFiSTA.cpp | 1 - 1 file changed, 1 deletion(-) diff --git a/libraries/ESP8266WiFi/src/ESP8266WiFiSTA.cpp b/libraries/ESP8266WiFi/src/ESP8266WiFiSTA.cpp index 09ef7b6603..a75f720318 100644 --- a/libraries/ESP8266WiFi/src/ESP8266WiFiSTA.cpp +++ b/libraries/ESP8266WiFi/src/ESP8266WiFiSTA.cpp @@ -358,7 +358,6 @@ bool ESP8266WiFiSTAClass::reconnect() { * @return one value of wl_status_t enum */ bool ESP8266WiFiSTAClass::disconnect(bool wifioff) { -{ return disconnect(wifioff, false); } From 4ecaf68a8879a38ce716e6a23d69decf605eb1cc Mon Sep 17 00:00:00 2001 From: Krzysiek S Date: Thu, 15 Dec 2022 08:46:39 +0100 Subject: [PATCH 6/6] Fix default behavior of basic disconnect method variant. Change eraseap to meaningful name 'eraseCredentials'. Fix comments. --- libraries/ESP8266WiFi/src/ESP8266WiFiSTA.cpp | 15 ++++++++------- libraries/ESP8266WiFi/src/ESP8266WiFiSTA.h | 2 +- 2 files changed, 9 insertions(+), 8 deletions(-) diff --git a/libraries/ESP8266WiFi/src/ESP8266WiFiSTA.cpp b/libraries/ESP8266WiFi/src/ESP8266WiFiSTA.cpp index a75f720318..e73fc81987 100644 --- a/libraries/ESP8266WiFi/src/ESP8266WiFiSTA.cpp +++ b/libraries/ESP8266WiFi/src/ESP8266WiFiSTA.cpp @@ -353,28 +353,29 @@ bool ESP8266WiFiSTAClass::reconnect() { } /** - * Disconnect from the network - * @param wifioff + * Disconnect from the network with clearing saved credentials + * @param wifioff Bool indicating whether STA should be disabled. * @return one value of wl_status_t enum */ bool ESP8266WiFiSTAClass::disconnect(bool wifioff) { - return disconnect(wifioff, false); + // Disconnect with clearing saved credentials. + return disconnect(wifioff, true); } /** * Disconnect from the network - * @param wifioff - * @param eraseap + * @param wifioff Bool indicating whether STA should be disabled. + * @param eraseCredentials Bool indicating whether saved credentials should be erased. * @return one value of wl_status_t enum */ -bool ESP8266WiFiSTAClass::disconnect(bool wifioff, bool eraseap) { +bool ESP8266WiFiSTAClass::disconnect(bool wifioff, bool eraseCredentials) { bool ret = false; // Read current config. struct station_config conf; wifi_station_get_config(&conf); - if (eraseap) { + if (eraseCredentials) { memset(&conf.ssid, 0, sizeof(conf.ssid)); memset(&conf.password, 0, sizeof(conf.password)); } diff --git a/libraries/ESP8266WiFi/src/ESP8266WiFiSTA.h b/libraries/ESP8266WiFi/src/ESP8266WiFiSTA.h index 23bfe42fa0..0c1150a8e4 100644 --- a/libraries/ESP8266WiFi/src/ESP8266WiFiSTA.h +++ b/libraries/ESP8266WiFi/src/ESP8266WiFiSTA.h @@ -50,7 +50,7 @@ class ESP8266WiFiSTAClass: public LwipIntf { bool reconnect(); bool disconnect(bool wifioff = false); - bool disconnect(bool wifioff, bool eraseap); + bool disconnect(bool wifioff, bool eraseCredentials); bool isConnected();