diff --git a/Platformio/HAL/HardwareModules/wifiHandlerInterface.h b/Platformio/HAL/HardwareModules/wifiHandlerInterface.h index 1bd93d1..71e5b95 100644 --- a/Platformio/HAL/HardwareModules/wifiHandlerInterface.h +++ b/Platformio/HAL/HardwareModules/wifiHandlerInterface.h @@ -5,8 +5,9 @@ class wifiHandlerInterface { public: + wifiHandlerInterface() = default; struct WifiInfo { - WifiInfo(){}; + WifiInfo() = default; WifiInfo(std::string aSsid, int aRssi) : ssid(aSsid), rssi(aRssi) {} std::string ssid = ""; @@ -14,10 +15,11 @@ public: }; struct wifiStatus { + wifiStatus() = default; wifiStatus(bool aConnected, std::string aIp, std::string aSsid) : isConnected(aConnected), IP(aIp), ssid(aSsid){}; - bool isConnected; + bool isConnected = false; std::string IP = ""; std::string ssid = ""; }; @@ -25,15 +27,21 @@ public: typedef std::vector ScanDoneDataTy; typedef Notification ScanNotificationTy; + /// @brief Initialize the wifi handler virtual void begin() = 0; + /// @brief Trigger a scan scan for wifi networks virtual void scan() = 0; + /// @brief Attempt a connection to the wifi using the provided credentials virtual void connect(std::string ssid, std::string password) = 0; - virtual bool isAvailable() = 0; + /// @brief Get the status of the current wifi connection + virtual wifiStatus GetStatus() = 0; + // Register for Scan Notification to handle when scans are completed std::shared_ptr ScanCompleteNotification() { return mScanNotification; }; + // Register for Status notifications to handle changes in status std::shared_ptr> WifiStatusNotification() { return mStatusUpdate; }; diff --git a/Platformio/HAL/Targets/ESP32/wifiHandler/wifihandler.cpp b/Platformio/HAL/Targets/ESP32/wifiHandler/wifihandler.cpp index b25bf9d..1b0d18d 100644 --- a/Platformio/HAL/Targets/ESP32/wifiHandler/wifihandler.cpp +++ b/Platformio/HAL/Targets/ESP32/wifiHandler/wifihandler.cpp @@ -1,63 +1,10 @@ #include "wifihandler.hpp" #include "HardwareAbstract.hpp" -#include "WiFi.h" #include #include +#include std::shared_ptr wifiHandler::mInstance = nullptr; - -// WiFi status event -void wifiHandler::WiFiEvent(WiFiEvent_t event) { - int no_networks = 0; - switch (event) { - case ARDUINO_EVENT_WIFI_SCAN_DONE: { - Serial.println("WIFI scan done\n"); - no_networks = WiFi.scanComplete(); - Serial.println("making notify vector"); - auto info = std::vector(no_networks); - Serial.println("loaing notify vector"); - for (int i = 0; i < no_networks; i++) { - auto ssid = - WiFi.SSID(i).c_str() ? std::string(WiFi.SSID(i).c_str()) : "No SSID"; - info[i] = WifiInfo(ssid, WiFi.RSSI(i)); - } - if (no_networks < 0) { - Serial.println("Scan failed"); - } else { - // TODO Convert To callbacks - // this->display.clear_wifi_networks(); - Serial.print(no_networks); - Serial.print(" found\n"); - // this->display.wifi_scan_complete( no_networks); - } - Serial.println("notifying"); - mScanNotification->notify(info); - Serial.println("notified"); - if (WiFi.isConnected() == false) { - WiFi.reconnect(); - } - break; - } - case ARDUINO_EVENT_WIFI_STA_GOT_IP: - case ARDUINO_EVENT_WIFI_STA_GOT_IP6: - this->update_credentials(); - case ARDUINO_EVENT_WIFI_STA_DISCONNECTED: - case ARDUINO_EVENT_WIFI_STA_LOST_IP: - case ARDUINO_EVENT_WIFI_STA_STOP: - this->update_status(); - default: - Serial.print("Wifi Status: "); - Serial.println(WiFi.status()); - break; - } - if (WiFi.status() == WL_CONNECT_FAILED) { - Serial.println("connection failed."); - WiFi.disconnect(); - } - Serial.println(WiFi.status()); -} - -bool wifiHandler::isAvailable() { return true; } std::shared_ptr wifiHandler::getInstance() { if (mInstance) { return mInstance; @@ -66,42 +13,67 @@ std::shared_ptr wifiHandler::getInstance() { return mInstance; }; -wifiHandler::wifiHandler() { - this->password = ""; - this->SSID = ""; +void wifiHandler::WiFiEvent(WiFiEvent_t event) { + int no_networks = 0; + switch (event) { + case ARDUINO_EVENT_WIFI_SCAN_DONE: { + no_networks = WiFi.scanComplete(); + auto info = std::vector(no_networks); + for (int i = 0; i < no_networks; i++) { + auto ssid = + WiFi.SSID(i).c_str() ? std::string(WiFi.SSID(i).c_str()) : "No SSID"; + info[i] = WifiInfo(ssid, WiFi.RSSI(i)); + } + mScanNotification->notify(info); + if (WiFi.isConnected() == false) { + WiFi.reconnect(); + } + break; + } + case ARDUINO_EVENT_WIFI_STA_GOT_IP: + case ARDUINO_EVENT_WIFI_STA_GOT_IP6: + this->StoreCredentials(); + break; + case ARDUINO_EVENT_WIFI_STA_CONNECTED: + case ARDUINO_EVENT_WIFI_STA_DISCONNECTED: + case ARDUINO_EVENT_WIFI_STA_LOST_IP: + case ARDUINO_EVENT_WIFI_STA_STOP: + UpdateStatus(); + break; + default: + break; + } + if (WiFi.status() == WL_CONNECT_FAILED) { + WiFi.disconnect(); + } } -void wifiHandler::update_status() { - Serial.println("update_status"); - auto isConnected = WiFi.isConnected(); - std::string ip(WiFi.localIP().toString().c_str()); - std::string ssid(WiFi.SSID().c_str()); +void wifiHandler::UpdateStatus() { + Serial.println("UpdateStatus"); + mCurrentStatus.isConnected = WiFi.isConnected(); + mCurrentStatus.IP = WiFi.localIP().toString().c_str(); + mCurrentStatus.ssid = WiFi.SSID().c_str(); - wifiStatus status = wifiStatus(isConnected, ip, ssid); - - // Serial.println(WiFi.localIP()); - mStatusUpdate->notify(status); + mStatusUpdate->notify(mCurrentStatus); } -void wifiHandler::update_credentials() { +void wifiHandler::StoreCredentials() { // No connection was attempted so don't try to to save the creds - if (!connect_attempt) { + if (!mIsConnectionAttempt) { return; } + mPassword = mConnectionAttemptPassword; + mSSID = mConnectionAttemptSSID; - if (temporary_password == password || temporary_ssid == SSID) { - password = temporary_password; - SSID = temporary_ssid; + Preferences preferences; + preferences.begin("wifiSettings", false); + preferences.putString("password", mPassword.c_str()); + preferences.putString("SSID", mSSID.c_str()); + preferences.end(); - Preferences preferences; - preferences.begin("wifiSettings", false); - String tempString = temporary_password.c_str(); - preferences.putString("password", tempString); - tempString = temporary_ssid.c_str(); - preferences.putString("SSID", tempString); - preferences.end(); - } - this->connect_attempt = false; + mConnectionAttemptPassword.clear(); + mConnectionAttemptSSID.clear(); + mIsConnectionAttempt = false; } void wifiHandler::scan() { @@ -111,10 +83,8 @@ void wifiHandler::scan() { } void wifiHandler::begin() { - // this->display = display; WiFi.setHostname("OMOTE"); WiFi.mode(WIFI_STA); - // WiFi.onEvent([this] (WiFiEvent_t event) {mInstance->WiFiEvent(event);}); WiFi.onEvent([](WiFiEvent_t event) { mInstance->WiFiEvent(event); }); Preferences preferences; @@ -123,19 +93,11 @@ void wifiHandler::begin() { String password = preferences.getString("password"); preferences.end(); - /* If the SSID is not empty, there was a value stored in the preferences and - * we try to use it.*/ + // Attempt Connection with stored Credentials if (!ssid.isEmpty()) { - Serial.print("Connecting to wifi "); - Serial.println(ssid); - this->SSID = ssid.c_str(); - this->password = password.c_str(); - this->connect(SSID, this->password); + connect(mSSID, mPassword); } else { Serial.println("no SSID or password stored"); - /*Set first character to \0 indicates an empty string*/ - this->SSID[0] = '\0'; - this->password[0] = '\0'; WiFi.disconnect(); } @@ -143,21 +105,9 @@ void wifiHandler::begin() { } void wifiHandler::connect(std::string ssid, std::string password) { - this->connect_attempt = true; - // this->temporary_password = password; - // this->temporary_ssid = ssid; + Serial.printf("Attempting Wifi Connection To %s \n", mSSID.c_str()); + mIsConnectionAttempt = true; + mConnectionAttemptPassword = password; + mConnectionAttemptSSID = ssid; WiFi.begin(ssid.c_str(), password.c_str()); } - -void wifiHandler::turnOff() { - WiFi.disconnect(); - WiFi.mode(WIFI_OFF); -} - -void wifiHandler::disconnect() { WiFi.disconnect(); } - -bool wifiHandler::isConnected() { return WiFi.isConnected(); } - -std::string wifiHandler::getIP() { - return std::string(WiFi.localIP().toString().c_str()); -} \ No newline at end of file diff --git a/Platformio/HAL/Targets/ESP32/wifiHandler/wifihandler.hpp b/Platformio/HAL/Targets/ESP32/wifiHandler/wifihandler.hpp index cb09ed5..bf3bfda 100644 --- a/Platformio/HAL/Targets/ESP32/wifiHandler/wifihandler.hpp +++ b/Platformio/HAL/Targets/ESP32/wifiHandler/wifihandler.hpp @@ -6,71 +6,48 @@ class wifiHandler : public wifiHandlerInterface { public: - wifiHandler(); static std::shared_ptr getInstance(); - /** - * @brief Function to initialize the wifi handler - */ - void begin(); - /** - * @brief Connect to the wifi using the provided credetials - */ + // wifiHandlerInterface Implementation + void begin() override; + void scan() override; void connect(std::string ssid, std::string password) override; + wifiStatus GetStatus() override { return mCurrentStatus; }; + // + +protected: + wifiHandler() = default; + static std::shared_ptr mInstance; /** - * @brief function to trigger asynchronous scan for wifi networks + * @brief Function to store the credentials when we have had a + * successful connection */ - void scan(); - bool isAvailable(); + void StoreCredentials(); private: /** - * @brief Function to update the wifi credentials. This function is called in - * the wifi event callback function after a connection is established. Only - * then is the new credentials stored and the old stored credentials - * overwritten. - * - * @param temporary_ssid - * @param temporary_password + * @brief Handler for incoming arduino wifi events + * @param event - a Wifi event */ - void update_credentials(); - void WiFiEvent(WiFiEvent_t event); /** - * @brief Function to turn off wifi + * @brief Update Internal status and send out a notification */ - void turnOff(); - /** - * @brief Function to get the IP address of this device - * @return String IP Address of the device - */ - std::string getIP(); - static std::shared_ptr mInstance; - bool connect_attempt = false; - std::string temporary_password; - std::string temporary_ssid; - - std::string password; - std::string SSID; - - void update_status(); + void UpdateStatus(); + wifiStatus mCurrentStatus; /** - * @brief Function to disconnect from the network + * @brief Variables used to track wifi connection attempts */ - void disconnect(); + bool mIsConnectionAttempt = false; + std::string mConnectionAttemptPassword; + std::string mConnectionAttemptSSID; /** - * @brief Function to determine wether or not we are connected to a network - * - * @return true Device is connected to wifi network - * @return false Device is not connected to wifi network - */ - bool isConnected(); - /** - * @brief Internal variable to store the wifi SSID - * + * @brief Verified Working User and Pass to Wifi network */ + std::string mPassword; + std::string mSSID; }; \ No newline at end of file diff --git a/Platformio/platformio.ini b/Platformio/platformio.ini index 3dc87e7..f6169e6 100644 --- a/Platformio/platformio.ini +++ b/Platformio/platformio.ini @@ -22,7 +22,7 @@ build_flags = -D LV_CONF_SKIP ;------------- LVGL ------------------------------------------ -D LV_MEM_CUSTOM=1 - -D LV_MEM_SIZE="\(48U * 1024U\)" + -D LV_MEM_SIZE="(48U * 1024U)" -D LV_FONT_MONTSERRAT_12=1 -D LV_FONT_MONTSERRAT_16=1 -D LV_FONT_MONTSERRAT_24=1