Skip to content

Commit

Permalink
Fix disabled clearing of network credentials.
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
alufers committed May 3, 2024
1 parent 8687f1e commit b150842
Show file tree
Hide file tree
Showing 6 changed files with 59 additions and 13 deletions.
5 changes: 2 additions & 3 deletions src/core/main/app/CoreBindings.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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) {
Expand Down
3 changes: 2 additions & 1 deletion src/core/main/app/include/Connectivity.h
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,7 @@ class Connectivity {

virtual void registerHandlers(std::shared_ptr<bell::BellHTTPServer> http) = 0;

virtual void clearConfig() = 0;

virtual void requestClearConfig() = 0;
};
} // namespace euph
3 changes: 2 additions & 1 deletion src/core/main/app/include/EventBus.h
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
2 changes: 1 addition & 1 deletion src/targets/cli/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,7 @@ class FakeConnectivity : public euph::Connectivity, public bell::Task {
sendStateUpdate();
}
}
void clearConfig() override {
void requestClearConfig() override {
}
};

Expand Down
32 changes: 32 additions & 0 deletions src/targets/esp32/main/app/ESP32Connectivity.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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<ESP32Connectivity*>(arg);
Expand Down Expand Up @@ -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<nvs::NVSHandle> handle =
Expand Down Expand Up @@ -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));
}
Expand Down Expand Up @@ -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();
}
}

Expand Down
27 changes: 20 additions & 7 deletions src/targets/esp32/main/app/include/ESP32Connectivity.h
Original file line number Diff line number Diff line change
Expand Up @@ -4,18 +4,18 @@
#include <string>
#include <string_view>

#include <lwip/ip4_addr.h>
#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 <lwip/ip4_addr.h>

#include "BellTask.h"
#include "WrappedSemaphore.h"
#include "CaptivePortalTask.h"
#include "WrappedSemaphore.h"

#include "Connectivity.h"
#include "EuphLogger.h"
Expand All @@ -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);
Expand All @@ -40,16 +39,22 @@ 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";

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;
Expand All @@ -66,5 +71,13 @@ class ESP32Connectivity : public Connectivity, public bell::Task {

std::unique_ptr<bell::WrappedSemaphore> dataUpdateSemaphore;
std::unique_ptr<euph::CaptivePortalTask> captivePortalDNS;

std::atomic<bool> clearConfigRequested = false;
/**
* @brief Clears wifi configuration from NVS.
*
* Shall only be called from a non-PSRAM task.
*/
void clearConfig();
};
} // namespace euph

0 comments on commit b150842

Please sign in to comment.