From 0eda8c66b20639e9d7f1cc62dd42f8ea594923a0 Mon Sep 17 00:00:00 2001 From: Jeff Edson Date: Mon, 23 Apr 2018 17:57:02 -0700 Subject: [PATCH] Prior to this change, BootWiFi::boot would hang on m_completeSemaphore.wait("boot") if WiFi::connectAP received SYSTEM_EVENT_STA_DISCONNECTED. This commit changes 2 items 1) after calling m_wifi.connectAP, calls m_completeSemaphore.give() to ensure that we dont hang 2) BootWiFi::boot returns ESP_OK if it successfully receives a SYSTEM_EVENT_STA_GOT_IP event. Otherwise it returns the wifi_err_reason_t so that the programmer can then handle it in a distinct manner. --- networking/bootwifi/BootWiFi.cpp | 24 +++++++++++++++++------- networking/bootwifi/BootWiFi.h | 3 ++- 2 files changed, 19 insertions(+), 8 deletions(-) diff --git a/networking/bootwifi/BootWiFi.cpp b/networking/bootwifi/BootWiFi.cpp index 8a62786a..cc07c898 100644 --- a/networking/bootwifi/BootWiFi.cpp +++ b/networking/bootwifi/BootWiFi.cpp @@ -243,6 +243,7 @@ class BootWifiEventHandler: public WiFiEventHandler { esp_err_t staGotIp(system_event_sta_got_ip_t event_sta_got_ip) { ESP_LOGD("BootWifiEventHandler", ">> staGotIP"); + m_pBootWiFi->m_apConnectionStatus = ESP_OK; // Set the status to ESP_OK m_pBootWiFi->m_completeSemaphore.give(); // If we got an IP address, then we can end the boot process. ESP_LOGD("BootWifiEventHandler", "<< staGotIP"); return ESP_OK; @@ -255,6 +256,11 @@ class BootWifiEventHandler: public WiFiEventHandler { /** * Boot WiFi + * + * @brief Get connected to WiFi + * + * @detailed If SSID & Password were previously saved, connect to the AP. Otherwise become an AP and start an HTTP server so that the user can set SSID & Password - then save it. + * */ void BootWiFi::bootWiFi2() { ESP_LOGD(LOG_TAG, ">> bootWiFi2"); @@ -286,10 +292,8 @@ void BootWiFi::bootWiFi2() { connectionInfo.ipInfo.netmask.addr ); - // Connect to the access point. - while(!m_wifi.connectAP(connectionInfo.ssid, connectionInfo.password)){ - ESP_LOGE(LOG_TAG, "Unable to connect to access point \"%s\" - trying again...", connectionInfo.ssid); - }; + m_apConnectionStatus = m_wifi.connectAP(connectionInfo.ssid, connectionInfo.password); // Try to connect to the access point. + m_completeSemaphore.give(); // end the boot process so we don't hang... } else { // We do NOT have connection information. Let us now become an access @@ -316,22 +320,28 @@ void BootWiFi::setAccessPointCredentials(std::string ssid, std::string password) /** - * @brief Main entry point into booting WiFi + * @brief Main entry point into booting WiFi - see BootWiFi2 for more detail. + * + * The event handler will be called back with the outcome of the connection. + * + * @returns ESP_OK if successfully connected to an access point. Otherwise returns wifi_err_reason_t - to print use GeneralUtils::wifiErrorToString */ -void BootWiFi::boot() { +uint8_t BootWiFi::boot() { ESP_LOGD(LOG_TAG, ">> boot"); ESP_LOGD(LOG_TAG, " +----------+"); ESP_LOGD(LOG_TAG, " | BootWiFi |"); ESP_LOGD(LOG_TAG, " +----------+"); ESP_LOGD(LOG_TAG, " Access point credentials: %s/%s", m_ssid.c_str(), m_password.c_str()); - m_completeSemaphore.take("boot"); // Take the semaphore which will be unlocked when we complete booting. + m_completeSemaphore.take("boot"); // Take the semaphore which will be unlocked when we complete booting. bootWiFi2(); m_completeSemaphore.wait("boot"); // Wait for the semaphore that indicated we have completed booting. m_wifi.setWifiEventHandler(nullptr); // Remove the WiFi boot handler when we have completed booting. ESP_LOGD(LOG_TAG, "<< boot"); + return m_apConnectionStatus; } // boot BootWiFi::BootWiFi() { m_httpServerStarted = false; + m_apConnectionStatus = UINT8_MAX; setAccessPointCredentials("esp32", "password"); // Default access point credentials } diff --git a/networking/bootwifi/BootWiFi.h b/networking/bootwifi/BootWiFi.h index 3d37dccd..9c4a17b6 100644 --- a/networking/bootwifi/BootWiFi.h +++ b/networking/bootwifi/BootWiFi.h @@ -24,12 +24,13 @@ class BootWiFi { bool m_httpServerStarted; std::string m_ssid; std::string m_password; + uint8_t m_apConnectionStatus; // receives the connection status. ESP_OK = received SYSTEM_EVENT_STA_GOT_IP event. FreeRTOS::Semaphore m_completeSemaphore = FreeRTOS::Semaphore("completeSemaphore"); public: BootWiFi(); void setAccessPointCredentials(std::string ssid, std::string password); - void boot(); + uint8_t boot(); }; #endif /* MAIN_BOOTWIFI_H_ */