From 6dfa89794926a723ce6e559667bbab596cb992ef Mon Sep 17 00:00:00 2001 From: devyte Date: Sun, 19 Nov 2017 00:46:36 -0300 Subject: [PATCH 1/2] refactor of WiFiMulti::run to reduce complexity and remove dead code --- .../ESP8266WiFi/src/ESP8266WiFiMulti.cpp | 116 ++++++++++-------- 1 file changed, 66 insertions(+), 50 deletions(-) diff --git a/libraries/ESP8266WiFi/src/ESP8266WiFiMulti.cpp b/libraries/ESP8266WiFi/src/ESP8266WiFiMulti.cpp index b7fba7ddb0..0800739072 100644 --- a/libraries/ESP8266WiFi/src/ESP8266WiFiMulti.cpp +++ b/libraries/ESP8266WiFi/src/ESP8266WiFiMulti.cpp @@ -40,16 +40,33 @@ bool ESP8266WiFiMulti::addAP(const char* ssid, const char *passphrase) { wl_status_t ESP8266WiFiMulti::run(void) { - int8_t scanResult; wl_status_t status = WiFi.status(); if(status == WL_DISCONNECTED || status == WL_NO_SSID_AVAIL || status == WL_IDLE_STATUS || status == WL_CONNECT_FAILED) { - scanResult = WiFi.scanComplete(); + int8_t scanResult = WiFi.scanComplete(); + if(scanResult == WIFI_SCAN_RUNNING) { - // scan is running - return WL_NO_SSID_AVAIL; - } else if(scanResult > 0) { - // scan done analyze + // scan is running, do nothing yet + status = WL_NO_SSID_AVAIL; + return status; + } + + if(scanResult == 0) { + // scan done, no ssids found. Start another scan. + DEBUG_WIFI_MULTI("[WIFI] scan done\n"); + DEBUG_WIFI_MULTI("[WIFI] no networks found\n"); + WiFi.scanDelete(); + DEBUG_WIFI_MULTI("\n\n"); + delay(0); + WiFi.disconnect(); + DEBUG_WIFI_MULTI("[WIFI] start scan\n"); + // scan wifi async mode + WiFi.scanNetworks(true); + return status; + } + + if(scanResult > 0) { + // scan done, analyze WifiAPlist_t bestNetwork { NULL, NULL }; int bestNetworkDb = INT_MIN; uint8 bestBSSID[6]; @@ -58,48 +75,44 @@ wl_status_t ESP8266WiFiMulti::run(void) { DEBUG_WIFI_MULTI("[WIFI] scan done\n"); delay(0); - if(scanResult <= 0) { - DEBUG_WIFI_MULTI("[WIFI] no networks found\n"); - } else { - DEBUG_WIFI_MULTI("[WIFI] %d networks found\n", scanResult); - for(int8_t i = 0; i < scanResult; ++i) { - - String ssid_scan; - int32_t rssi_scan; - uint8_t sec_scan; - uint8_t* BSSID_scan; - int32_t chan_scan; - bool hidden_scan; - - WiFi.getNetworkInfo(i, ssid_scan, sec_scan, rssi_scan, BSSID_scan, chan_scan, hidden_scan); - - bool known = false; - for(uint32_t x = 0; x < APlist.size(); x++) { - WifiAPlist_t entry = APlist[x]; - - if(ssid_scan == entry.ssid) { // SSID match - known = true; - if(rssi_scan > bestNetworkDb) { // best network - if(sec_scan == ENC_TYPE_NONE || entry.passphrase) { // check for passphrase if not open wlan - bestNetworkDb = rssi_scan; - bestChannel = chan_scan; - memcpy((void*) &bestNetwork, (void*) &entry, sizeof(bestNetwork)); - memcpy((void*) &bestBSSID, (void*) BSSID_scan, sizeof(bestBSSID)); - } + DEBUG_WIFI_MULTI("[WIFI] %d networks found\n", scanResult); + for(int8_t i = 0; i < scanResult; ++i) { + + String ssid_scan; + int32_t rssi_scan; + uint8_t sec_scan; + uint8_t* BSSID_scan; + int32_t chan_scan; + bool hidden_scan; + + WiFi.getNetworkInfo(i, ssid_scan, sec_scan, rssi_scan, BSSID_scan, chan_scan, hidden_scan); + + bool known = false; + for(uint32_t x = 0; x < APlist.size(); x++) { + WifiAPlist_t entry = APlist[x]; + + if(ssid_scan == entry.ssid) { // SSID match + known = true; + if(rssi_scan > bestNetworkDb) { // best network + if(sec_scan == ENC_TYPE_NONE || entry.passphrase) { // check for passphrase if not open wlan + bestNetworkDb = rssi_scan; + bestChannel = chan_scan; + memcpy((void*) &bestNetwork, (void*) &entry, sizeof(bestNetwork)); + memcpy((void*) &bestBSSID, (void*) BSSID_scan, sizeof(bestBSSID)); } - break; } + break; } + } - if(known) { - DEBUG_WIFI_MULTI(" ---> "); - } else { - DEBUG_WIFI_MULTI(" "); - } - - DEBUG_WIFI_MULTI(" %d: [%d][%02X:%02X:%02X:%02X:%02X:%02X] %s (%d) %c\n", i, chan_scan, BSSID_scan[0], BSSID_scan[1], BSSID_scan[2], BSSID_scan[3], BSSID_scan[4], BSSID_scan[5], ssid_scan.c_str(), rssi_scan, (sec_scan == ENC_TYPE_NONE) ? ' ' : '*'); - delay(0); + if(known) { + DEBUG_WIFI_MULTI(" ---> "); + } else { + DEBUG_WIFI_MULTI(" "); } + + DEBUG_WIFI_MULTI(" %d: [%d][%02X:%02X:%02X:%02X:%02X:%02X] %s (%d) %c\n", i, chan_scan, BSSID_scan[0], BSSID_scan[1], BSSID_scan[2], BSSID_scan[3], BSSID_scan[4], BSSID_scan[5], ssid_scan.c_str(), rssi_scan, (sec_scan == ENC_TYPE_NONE) ? ' ' : '*'); + delay(0); } // clean up ram @@ -146,15 +159,18 @@ wl_status_t ESP8266WiFiMulti::run(void) { } else { DEBUG_WIFI_MULTI("[WIFI] no matching wifi found!\n"); } - } else { - // start scan - DEBUG_WIFI_MULTI("[WIFI] delete old wifi config...\n"); - WiFi.disconnect(); - DEBUG_WIFI_MULTI("[WIFI] start scan\n"); - // scan wifi async mode - WiFi.scanNetworks(true); + return status; } + + + // scan failed, or some other condition not handled above. Start another scan. + DEBUG_WIFI_MULTI("[WIFI] delete old wifi config...\n"); + WiFi.disconnect(); + + DEBUG_WIFI_MULTI("[WIFI] start scan\n"); + // scan wifi async mode + WiFi.scanNetworks(true); } return status; } From ce0dd3c01bffcb7d5983f86e39d9386ee356de1b Mon Sep 17 00:00:00 2001 From: devyte Date: Sun, 19 Nov 2017 03:00:49 -0300 Subject: [PATCH 2/2] Clear dirty flag in begin and reallocate only if necessary, add getConstDataPtr method --- libraries/EEPROM/EEPROM.cpp | 13 +++++++++++-- libraries/EEPROM/EEPROM.h | 1 + 2 files changed, 12 insertions(+), 2 deletions(-) diff --git a/libraries/EEPROM/EEPROM.cpp b/libraries/EEPROM/EEPROM.cpp index b7909d0c3c..b5b8bfc157 100644 --- a/libraries/EEPROM/EEPROM.cpp +++ b/libraries/EEPROM/EEPROM.cpp @@ -56,16 +56,21 @@ void EEPROMClass::begin(size_t size) { size = (size + 3) & (~3); - if (_data) { + //In case begin() is called a 2nd+ time, don't reallocate if size is the same + if(_data && size != _size) { delete[] _data; + _data = new uint8_t[size]; + } else if(!_data) { + _data = new uint8_t[size]; } - _data = new uint8_t[size]; _size = size; noInterrupts(); spi_flash_read(_sector * SPI_FLASH_SEC_SIZE, reinterpret_cast(_data), _size); interrupts(); + + _dirty = false; //make sure dirty is cleared in case begin() is called 2nd+ time } void EEPROMClass::end() { @@ -131,6 +136,10 @@ uint8_t * EEPROMClass::getDataPtr() { return &_data[0]; } +uint8_t const * EEPROMClass::getConstDataPtr() { + return &_data[0]; +} + #if !defined(NO_GLOBAL_INSTANCES) && !defined(NO_GLOBAL_EEPROM) EEPROMClass EEPROM; #endif diff --git a/libraries/EEPROM/EEPROM.h b/libraries/EEPROM/EEPROM.h index 3a26d02e64..934a995a03 100644 --- a/libraries/EEPROM/EEPROM.h +++ b/libraries/EEPROM/EEPROM.h @@ -38,6 +38,7 @@ class EEPROMClass { void end(); uint8_t * getDataPtr(); + uint8_t const * getConstDataPtr(); template T &get(int address, T &t) {