From 476660f763bf334a194c2f30586a7be8245f9c39 Mon Sep 17 00:00:00 2001 From: Nathan Phillips Date: Tue, 9 Jul 2019 18:21:01 +0200 Subject: [PATCH] Cleanup WiFiMulti (#2955) * Use macros for switch case labels * Fixed spelling error in "too" * Fix spacing * Inline private functions that are only called once --- libraries/WiFi/src/WiFiMulti.cpp | 115 ++++++++++++++----------------- libraries/WiFi/src/WiFiMulti.h | 3 - 2 files changed, 50 insertions(+), 68 deletions(-) diff --git a/libraries/WiFi/src/WiFiMulti.cpp b/libraries/WiFi/src/WiFiMulti.cpp index 2bc27681..73085033 100644 --- a/libraries/WiFi/src/WiFiMulti.cpp +++ b/libraries/WiFi/src/WiFiMulti.cpp @@ -34,22 +34,64 @@ WiFiMulti::WiFiMulti() WiFiMulti::~WiFiMulti() { - APlistClean(); + for(uint32_t i = 0; i < APlist.size(); i++) { + WifiAPlist_t entry = APlist[i]; + if(entry.ssid) { + free(entry.ssid); + } + if(entry.passphrase) { + free(entry.passphrase); + } + } + APlist.clear(); } bool WiFiMulti::addAP(const char* ssid, const char *passphrase) { - return APlistAdd(ssid, passphrase); + WifiAPlist_t newAP; + + if(!ssid || *ssid == 0x00 || strlen(ssid) > 31) { + // fail SSID too long or missing! + log_e("[WIFI][APlistAdd] no ssid or ssid too long"); + return false; + } + + if(passphrase && strlen(passphrase) > 63) { + // fail passphrase too long! + log_e("[WIFI][APlistAdd] passphrase too long"); + return false; + } + + newAP.ssid = strdup(ssid); + + if(!newAP.ssid) { + log_e("[WIFI][APlistAdd] fail newAP.ssid == 0"); + return false; + } + + if(passphrase && *passphrase != 0x00) { + newAP.passphrase = strdup(passphrase); + if(!newAP.passphrase) { + log_e("[WIFI][APlistAdd] fail newAP.passphrase == 0"); + free(newAP.ssid); + return false; + } + } else { + newAP.passphrase = NULL; + } + + APlist.push_back(newAP); + log_i("[WIFI][APlistAdd] add SSID: %s", newAP.ssid); + return true; } uint8_t WiFiMulti::run(uint32_t connectTimeout) { - int8_t scanResult; uint8_t status = WiFi.status(); if(status == WL_CONNECTED) { for(uint32_t x = 0; x < APlist.size(); x++) { - if(WiFi.SSID()==APlist[x].ssid){ + if(WiFi.SSID()==APlist[x].ssid) { return status; } } @@ -119,7 +161,7 @@ uint8_t WiFiMulti::run(uint32_t connectTimeout) WiFi.begin(bestNetwork.ssid, bestNetwork.passphrase, bestChannel, bestBSSID); status = WiFi.status(); - + auto startTime = millis(); // wait for connection, fail, or timeout while(status != WL_CONNECTED && status != WL_NO_SSID_AVAIL && status != WL_CONNECT_FAILED && (millis() - startTime) <= connectTimeout) { @@ -128,17 +170,17 @@ uint8_t WiFiMulti::run(uint32_t connectTimeout) } switch(status) { - case 3: + case WL_CONNECTED: log_i("[WIFI] Connecting done."); log_d("[WIFI] SSID: %s", WiFi.SSID().c_str()); log_d("[WIFI] IP: %s", WiFi.localIP().toString().c_str()); log_d("[WIFI] MAC: %s", WiFi.BSSIDstr().c_str()); log_d("[WIFI] Channel: %d", WiFi.channel()); break; - case 1: + case WL_NO_SSID_AVAIL: log_e("[WIFI] Connecting Failed AP not found."); break; - case 4: + case WL_CONNECT_FAILED: log_e("[WIFI] Connecting Failed."); break; default: @@ -160,60 +202,3 @@ uint8_t WiFiMulti::run(uint32_t connectTimeout) return status; } - -// ################################################################################## - -bool WiFiMulti::APlistAdd(const char* ssid, const char *passphrase) -{ - - WifiAPlist_t newAP; - - if(!ssid || *ssid == 0x00 || strlen(ssid) > 31) { - // fail SSID to long or missing! - log_e("[WIFI][APlistAdd] no ssid or ssid to long"); - return false; - } - - if(passphrase && strlen(passphrase) > 63) { - // fail passphrase to long! - log_e("[WIFI][APlistAdd] passphrase to long"); - return false; - } - - newAP.ssid = strdup(ssid); - - if(!newAP.ssid) { - log_e("[WIFI][APlistAdd] fail newAP.ssid == 0"); - return false; - } - - if(passphrase && *passphrase != 0x00) { - newAP.passphrase = strdup(passphrase); - if(!newAP.passphrase) { - log_e("[WIFI][APlistAdd] fail newAP.passphrase == 0"); - free(newAP.ssid); - return false; - } - } else { - newAP.passphrase = NULL; - } - - APlist.push_back(newAP); - log_i("[WIFI][APlistAdd] add SSID: %s", newAP.ssid); - return true; -} - -void WiFiMulti::APlistClean(void) -{ - for(uint32_t i = 0; i < APlist.size(); i++) { - WifiAPlist_t entry = APlist[i]; - if(entry.ssid) { - free(entry.ssid); - } - if(entry.passphrase) { - free(entry.passphrase); - } - } - APlist.clear(); -} - diff --git a/libraries/WiFi/src/WiFiMulti.h b/libraries/WiFi/src/WiFiMulti.h index d628c444..38ddb5d9 100644 --- a/libraries/WiFi/src/WiFiMulti.h +++ b/libraries/WiFi/src/WiFiMulti.h @@ -46,9 +46,6 @@ public: private: std::vector APlist; - bool APlistAdd(const char* ssid, const char *passphrase = NULL); - void APlistClean(void); - }; #endif /* WIFICLIENTMULTI_H_ */