From b1508420397f4916be62b7755631bd80a75a992a Mon Sep 17 00:00:00 2001 From: alufers Date: Fri, 3 May 2024 15:31:58 +0200 Subject: [PATCH] Fix disabled clearing of network credentials. After the removal of StorageAccessor there was no way to access the NVS from the berry PSRAM task. Fixed that issue by emitting a phony ESP32 event loop event. --- src/core/main/app/CoreBindings.cpp | 5 ++- src/core/main/app/include/Connectivity.h | 3 +- src/core/main/app/include/EventBus.h | 3 +- src/targets/cli/main.cpp | 2 +- .../esp32/main/app/ESP32Connectivity.cpp | 32 +++++++++++++++++++ .../main/app/include/ESP32Connectivity.h | 27 ++++++++++++---- 6 files changed, 59 insertions(+), 13 deletions(-) diff --git a/src/core/main/app/CoreBindings.cpp b/src/core/main/app/CoreBindings.cpp index c00d5629..974d1830 100644 --- a/src/core/main/app/CoreBindings.cpp +++ b/src/core/main/app/CoreBindings.cpp @@ -191,6 +191,7 @@ void CoreBindings::_deleteConfigFiles() { void CoreBindings::_restart() { EUPH_LOG(info, TAG, "Restarting the application..."); + BELL_SLEEP_MS(100); // Wait for any unfinished asynchronous business #ifdef ESP_PLATFORM esp_restart(); #else @@ -199,9 +200,7 @@ void CoreBindings::_restart() { } void CoreBindings::_clearWifiConfig() { - // @TODO bring back - // this->ctx->storage->executeFromTask( - // [this]() { this->ctx->connectivity->clearConfig(); }); + this->ctx->connectivity->requestClearConfig(); } void CoreBindings::_tripEmergencyMode(std::string message) { diff --git a/src/core/main/app/include/Connectivity.h b/src/core/main/app/include/Connectivity.h index 58c00302..200d568b 100644 --- a/src/core/main/app/include/Connectivity.h +++ b/src/core/main/app/include/Connectivity.h @@ -110,6 +110,7 @@ class Connectivity { virtual void registerHandlers(std::shared_ptr http) = 0; - virtual void clearConfig() = 0; + + virtual void requestClearConfig() = 0; }; } // namespace euph diff --git a/src/core/main/app/include/EventBus.h b/src/core/main/app/include/EventBus.h index 94de93c0..0a9f3b75 100644 --- a/src/core/main/app/include/EventBus.h +++ b/src/core/main/app/include/EventBus.h @@ -17,7 +17,8 @@ enum class EventType : uint32_t { VM_ERROR_EVENT, CONNECTIVITY_EVENT, WEBSOCKET_EVENT, - PLAYBACK_EVENT + PLAYBACK_EVENT, + CONNECTIVITY_REQUEST_EVENT, }; class Event { diff --git a/src/targets/cli/main.cpp b/src/targets/cli/main.cpp index 28d20533..131fbf05 100644 --- a/src/targets/cli/main.cpp +++ b/src/targets/cli/main.cpp @@ -133,7 +133,7 @@ class FakeConnectivity : public euph::Connectivity, public bell::Task { sendStateUpdate(); } } - void clearConfig() override { + void requestClearConfig() override { } }; diff --git a/src/targets/esp32/main/app/ESP32Connectivity.cpp b/src/targets/esp32/main/app/ESP32Connectivity.cpp index 8d8eb5d3..5acca976 100644 --- a/src/targets/esp32/main/app/ESP32Connectivity.cpp +++ b/src/targets/esp32/main/app/ESP32Connectivity.cpp @@ -10,6 +10,27 @@ using namespace euph; +/** + * @brief Internal event base for dispatching custom events to the ESP32 event loop. + * + * + */ +ESP_EVENT_DEFINE_BASE(EUPH_ESP32_CONNECTIVITY_EVENT); + +/** + * @brief Event types for EUPH_ESP32_CONNECTIVITY_EVENT. + * + */ +enum class EuphEsp32ConnectivityEvent : int32_t { + + /** + * @brief Request to clear the WiFi configuration. It will be cleared from the NVS storage. + * + * The configuration is cleared from the event handler, because it is not safe to access the NVS storage from a PSRAM task. + */ + CLEAR_CONFIG, +}; + static void wifiEventHandler(void* arg, esp_event_base_t event_base, int32_t event_id, void* event_data) { ESP32Connectivity* self = static_cast(arg); @@ -88,6 +109,12 @@ void ESP32Connectivity::persistConfig() { } } +void ESP32Connectivity::requestClearConfig() { + ESP_ERROR_CHECK(esp_event_post(EUPH_ESP32_CONNECTIVITY_EVENT, + (int32_t)EuphEsp32ConnectivityEvent::CLEAR_CONFIG, + NULL, 0, portMAX_DELAY)); +} + void ESP32Connectivity::clearConfig() { esp_err_t err; std::unique_ptr handle = @@ -120,6 +147,8 @@ void ESP32Connectivity::initializeWiFiStack() { WIFI_EVENT, ESP_EVENT_ANY_ID, &wifiEventHandler, this, NULL)); ESP_ERROR_CHECK(esp_event_handler_instance_register( IP_EVENT, IP_EVENT_STA_GOT_IP, &wifiEventHandler, this, NULL)); + ESP_ERROR_CHECK(esp_event_handler_instance_register( + EUPH_ESP32_CONNECTIVITY_EVENT, ESP_EVENT_ANY_ID, &wifiEventHandler, this, NULL)); ESP_ERROR_CHECK(esp_wifi_set_mode(WIFI_MODE_STA)); } @@ -317,6 +346,9 @@ void ESP32Connectivity::handleEvent(esp_event_base_t event_base, } } } + } else if (event_base == EUPH_ESP32_CONNECTIVITY_EVENT && + event_id == (int32_t)EuphEsp32ConnectivityEvent::CLEAR_CONFIG) { + this->clearConfig(); } } diff --git a/src/targets/esp32/main/app/include/ESP32Connectivity.h b/src/targets/esp32/main/app/include/ESP32Connectivity.h index 245a5ce5..7e24c10f 100644 --- a/src/targets/esp32/main/app/include/ESP32Connectivity.h +++ b/src/targets/esp32/main/app/include/ESP32Connectivity.h @@ -4,18 +4,18 @@ #include #include +#include #include "esp_event.h" #include "esp_wifi.h" +#include "fmt/format.h" +#include "nlohmann/json.hpp" #include "nvs.h" #include "nvs_flash.h" #include "nvs_handle.hpp" -#include "nlohmann/json.hpp" -#include "fmt/format.h" -#include #include "BellTask.h" -#include "WrappedSemaphore.h" #include "CaptivePortalTask.h" +#include "WrappedSemaphore.h" #include "Connectivity.h" #include "EuphLogger.h" @@ -31,7 +31,6 @@ class ESP32Connectivity : public Connectivity, public bell::Task { void persistConfig(); void requestScan(); - void clearConfig() override; void attemptConnect(const std::string& ssid, const std::string& passwd); void handleEvent(esp_event_base_t event_base, int32_t event_id, void* event_data); @@ -40,8 +39,14 @@ class ESP32Connectivity : public Connectivity, public bell::Task { void initializeSTA(); void displayNameLoaded(std::string& name) override; - void runTask() override; + /** + * @brief Request an asynchronous clearing of the wifi configuration. + * + * Can be called from PSRAM tasks. + */ + void requestClearConfig() override; + void runTask() override; private: std::string TAG = "ESP32Connectivity"; @@ -49,7 +54,7 @@ class ESP32Connectivity : public Connectivity, public bell::Task { uint8_t DEFAULT_SCAN_LIST_SIZE = 10; uint8_t MAX_CONNECTION_ATTEMPTS = 3; wifi_ap_record_t scanInfo[10]; - + std::string apName = "Euphonium"; std::string nvsWiFiKey = "wifi_settings"; std::string displayName; @@ -66,5 +71,13 @@ class ESP32Connectivity : public Connectivity, public bell::Task { std::unique_ptr dataUpdateSemaphore; std::unique_ptr captivePortalDNS; + + std::atomic clearConfigRequested = false; + /** + * @brief Clears wifi configuration from NVS. + * + * Shall only be called from a non-PSRAM task. + */ + void clearConfig(); }; } // namespace euph