diff --git a/components/config/ConfigManager.cpp b/components/config/ConfigManager.cpp index 0678897..5f7b63d 100644 --- a/components/config/ConfigManager.cpp +++ b/components/config/ConfigManager.cpp @@ -2,101 +2,182 @@ // Created by Johnathon Slightham on 2025-06-12. // + +#include +#include + #include "nvs_flash.h" #include "constants/config.h" - #include "ConfigManager.h" +static auto TAG = "ConfigManager"; + void ConfigManager::init_config() { - nvs_flash_init(); + std::unique_lock lock(rw_lock); + if (this->initialized) + return; + this->initialized = true; + lock.unlock(); - nvs_handle config_handle; - nvs_open(NVS_FLASH_NAMESPACE, NVS_READWRITE, &config_handle); - - uint16_t id; - if (ESP_ERR_NVS_NOT_FOUND == nvs_get_u16(config_handle, MODULE_ID_KEY, &id)) { - nvs_set_u16(config_handle, MODULE_ID_KEY, DEFAULT_MODULE_ID); + esp_err_t ret = nvs_flash_init(); + if (ret == ESP_ERR_NVS_NO_FREE_PAGES || ret == ESP_ERR_NVS_NEW_VERSION_FOUND) { + // NVS partition was truncated and needs to be erased, retry nvs_flash_init after erasing + ESP_ERROR_CHECK(nvs_flash_erase()); + ret = nvs_flash_init(); } - - uint16_t type; - if (ESP_ERR_NVS_NOT_FOUND == nvs_get_u16(config_handle, MODULE_TYPE_KEY, &type)) { - nvs_set_u16(config_handle, MODULE_TYPE_KEY, DEFAULT_MODULE_TYPE); - } - - nvs_close(config_handle); + ESP_ERROR_CHECK(ret); } -// todo: we should probably cache some of these things -uint16_t ConfigManager::get_module_id() { - nvs_handle config_handle; - nvs_open(NVS_FLASH_NAMESPACE, NVS_READONLY, &config_handle); - uint16_t id; - nvs_get_u16(config_handle, MODULE_ID_KEY, &id); - nvs_close(config_handle); - return id; +uint16_t ConfigManager::get_module_id() const { + uint16_t i = DEFAULT_MODULE_ID; + get_uint16(MODULE_ID_KEY, &i); + return i; } -ModuleType ConfigManager::get_module_type() { - nvs_handle config_handle; - nvs_open(NVS_FLASH_NAMESPACE, NVS_READONLY, &config_handle); - uint16_t type; - nvs_get_u16(config_handle, MODULE_TYPE_KEY, &type); - nvs_close(config_handle); +ModuleType ConfigManager::get_module_type() const { + uint16_t type = DEFAULT_MODULE_TYPE; + get_uint16(MODULE_TYPE_KEY, &type); return static_cast(type); } -std::string ConfigManager::get_wifi_ssid() { - return get_string(WIFI_SSID_KEY); -} - -std::string ConfigManager::get_wifi_password() { - return get_string(WIFI_PASSWORD_KEY); -} - -std::string ConfigManager::get_string(const char *key) { - nvs_handle config_handle; - nvs_open(NVS_FLASH_NAMESPACE, NVS_READONLY, &config_handle); - - size_t required_size; // get size of the string - if (ESP_ERR_NVS_NOT_FOUND == nvs_get_str(config_handle, key, nullptr, &required_size)) { - return ""; - } - +std::string ConfigManager::get_wifi_ssid() const { std::string str; - str.resize(required_size); - if (ESP_ERR_NVS_NOT_FOUND == nvs_get_str(config_handle, key, str.data(), &required_size)) { - return ""; - } - - nvs_close(config_handle); + get_string(WIFI_SSID_KEY, str); return str; } -void ConfigManager::set_module_id(const uint16_t id) { +std::string ConfigManager::get_wifi_password() const { + std::string str; + get_string(WIFI_PASSWORD_KEY, str); + return str; +} + +esp_err_t ConfigManager::get_string(const char *key, std::string& out) const { + std::shared_lock lock(rw_lock); + esp_err_t ret = ESP_OK; + if (const auto str = get_config_from_cache(key)) { + out = *str; + return ret; + } + + ESP_LOGD(TAG, "get_string cache miss"); + nvs_handle config_handle; - nvs_open(NVS_FLASH_NAMESPACE, NVS_READWRITE, &config_handle); - nvs_set_u16(config_handle, MODULE_ID_KEY, id); + if (ret = nvs_open(NVS_FLASH_NAMESPACE, NVS_READONLY, &config_handle); ret != ESP_OK) { + ESP_LOGE(TAG, "get_string Failed to open NVS"); + return ret; + } + + size_t required_size; // get size of the string + if (ret = nvs_get_str(config_handle, key, nullptr, &required_size); ret != ESP_OK) { + ESP_LOGE(TAG, "get_string failed to get string size"); + return ret; + } + + out.resize(required_size); + if (ret = nvs_get_str(config_handle, key, out.data(), &required_size); ret != ESP_OK) { + ESP_LOGE(TAG, "get_string failed to get string"); + return ret; + } + nvs_close(config_handle); + return ret; +} + +esp_err_t ConfigManager::get_uint16(const char *key, uint16_t *out) const { + esp_err_t ret = ESP_OK; + std::shared_lock lock(rw_lock); + if (const auto i = get_config_from_cache(key)) { + *out = i.value(); + return ret; + } + + ESP_LOGD(TAG, "get_uint16 cache miss"); + + nvs_handle config_handle; + if (ret = nvs_open(NVS_FLASH_NAMESPACE, NVS_READONLY, &config_handle); ret != ESP_OK) { + return ret; + } + + if (ret = nvs_get_u16(config_handle, key, out); ret != ESP_OK) { + return ret; + } + + nvs_close(config_handle); + return ret; +} + +template +std::optional ConfigManager::get_config_from_cache(const std::string& key) const { + // Not thread safe + if (const auto it = cache.find(key); it != cache.end()) { + if (auto val = std::get_if(&it->second)) + return *val; + } + return std::nullopt; +} + +void ConfigManager::set_module_id(const uint16_t id) { + set(MODULE_ID_KEY, id); } void ConfigManager::set_module_type(const ModuleType type) { - nvs_handle config_handle; - nvs_open(NVS_FLASH_NAMESPACE, NVS_READWRITE, &config_handle); - nvs_set_u16(config_handle, MODULE_TYPE_KEY, type); - nvs_close(config_handle); + set(MODULE_TYPE_KEY, type); } void ConfigManager::set_wifi_ssid(const char* ssid) { - nvs_handle config_handle; - nvs_open(NVS_FLASH_NAMESPACE, NVS_READWRITE, &config_handle); - nvs_set_str(config_handle, WIFI_SSID_KEY, ssid); - nvs_close(config_handle); + set(WIFI_SSID_KEY, ssid); } void ConfigManager::set_wifi_password(const char* password) { - nvs_handle config_handle; - nvs_open(NVS_FLASH_NAMESPACE, NVS_READWRITE, &config_handle); - nvs_set_str(config_handle, WIFI_PASSWORD_KEY, password); - nvs_close(config_handle); + set(WIFI_PASSWORD_KEY, password); } +// Func - the esp write function to call (ie. nvs_set_u16) +// T - the type of the value in the key value pair +template +esp_err_t ConfigManager::set(const char* key, const T value) { + std::unique_lock lock(rw_lock); + + if (write_to_cache(key, value)) { + return ESP_OK; + } + + esp_err_t ret = ESP_OK; + nvs_handle config_handle; + ret = nvs_open(NVS_FLASH_NAMESPACE, NVS_READWRITE, &config_handle); + if (ESP_OK != ret) { + ESP_LOGE(TAG, "set error opening NVS handle"); + return ret; + } + + ret = Func(config_handle, key, value); + if (ESP_OK != ret) { + ESP_LOGE(TAG, "set error writing to NVS key %s", key); + } + nvs_close(config_handle); + + return ret; +} + +// Returns true when we don't need to write through the cache +template +bool ConfigManager::write_to_cache(const std::string& key, const T value) { + const auto it = cache.find(key); + if (it != cache.end()) { + if (const auto existing = std::get_if(&it->second)) { + if (*existing == value) + return true; + } + } + cache[key] = value; + return false; +} + +esp_err_t ConfigManager::nvs_set_cpp_str(const nvs_handle_t handle, const std::string& key, const std::string& str) { + return nvs_set_str(handle, key.c_str(), str.c_str()); +} + +esp_err_t ConfigManager::nvs_get_cpp_str(const nvs_handle_t handle, const std::string& key, std::string& out_str, size_t *length) { + return nvs_get_str(handle, key.c_str(), out_str.data(), length); +} diff --git a/components/config/include/ConfigManager.h b/components/config/include/ConfigManager.h index 4fc69dd..603b5ba 100644 --- a/components/config/include/ConfigManager.h +++ b/components/config/include/ConfigManager.h @@ -5,26 +5,60 @@ #ifndef CONFIGMANAGER_H #define CONFIGMANAGER_H -#include "constants/config.h" +#include +#include +#include +#include +#include + #include "flatbuffers_generated/RobotModule_generated.h" +// Singleton to r/w config from the ESP32 nvs (thread safe and cached) + class ConfigManager { public: - static void init_config(); + ConfigManager(const ConfigManager&) = delete; + void operator=(ConfigManager &) = delete; - static uint16_t get_module_id(); - static ModuleType get_module_type(); - static std::string get_wifi_ssid(); - static std::string get_wifi_password(); + static ConfigManager& get_instance() { // Thread safe as of C++11 + static ConfigManager instance; + instance.init_config(); + return instance; + } - static void set_module_id(uint16_t id); - static void set_module_type(ModuleType type); - static void set_wifi_ssid(const char* ssid); - static void set_wifi_password(const char* password); + uint16_t get_module_id() const; + ModuleType get_module_type() const; + std::string get_wifi_ssid() const; + std::string get_wifi_password() const; + + void set_module_id(uint16_t id); + void set_module_type(ModuleType type); + void set_wifi_ssid(const char* ssid); + void set_wifi_password(const char* password); private: - static std::string get_string(const char* key); + ConfigManager() = default; + esp_err_t get_string(const char *key, std::string &out) const; + esp_err_t get_uint16(const char *key, uint16_t *out) const; + + static esp_err_t nvs_set_cpp_str(nvs_handle_t handle, const std::string& key, const std::string& str); + static esp_err_t nvs_get_cpp_str(nvs_handle_t handle, const std::string& key, std::string& out_str, size_t *length); + + template + std::optional get_config_from_cache(const std::string& key) const; + + template + esp_err_t set(const char* key, T value); + + template + bool write_to_cache(const std::string& key, T value); + + void init_config(); + + std::unordered_map> cache; + mutable std::shared_mutex rw_lock; + bool initialized = false; }; #endif //CONFIGMANAGER_H diff --git a/components/ptrQueue/include/PtrQueue.h b/components/ptrQueue/include/PtrQueue.h index ef58487..4cf2210 100644 --- a/components/ptrQueue/include/PtrQueue.h +++ b/components/ptrQueue/include/PtrQueue.h @@ -13,7 +13,7 @@ template class PtrQueue { public: - explicit PtrQueue(UBaseType_t queueLength) + explicit PtrQueue(const UBaseType_t queueLength) : queue(xQueueCreate(queueLength, sizeof(T*))) {} ~PtrQueue() { diff --git a/components/rpc/CMakeLists.txt b/components/rpc/CMakeLists.txt index cea7bd6..411da10 100644 --- a/components/rpc/CMakeLists.txt +++ b/components/rpc/CMakeLists.txt @@ -1,4 +1,6 @@ idf_component_register(SRCS "WifiManager.cpp" "mDNSDiscoveryService.cpp" "TCPServer.cpp" "CommunicationRouter.cpp" "MessagingInterface.cpp" "OrientationDetection.cpp" - PRIV_REQUIRES driver esp_event nvs_flash esp_netif esp_wifi espressif__mdns constants config flatbuffers dataLink rmt - REQUIRES ptrQueue + PRIV_REQUIRES driver esp_event nvs_flash esp_netif espressif__mdns constants config flatbuffers dataLink rmt + REQUIRES ptrQueue esp_wifi INCLUDE_DIRS "include") + +target_compile_options(${COMPONENT_LIB} PRIVATE -fexceptions) diff --git a/components/rpc/MessagingInterface.cpp b/components/rpc/MessagingInterface.cpp index 32cf666..4a07c6b 100644 --- a/components/rpc/MessagingInterface.cpp +++ b/components/rpc/MessagingInterface.cpp @@ -15,14 +15,14 @@ MessagingInterface::~MessagingInterface() { vQueueDelete(m_mpi_rx_queue); vSemaphoreDelete(m_map_semaphore); - for (const auto [_tag, queue] : m_tag_to_queue) { + for (const auto queue: m_tag_to_queue | std::views::values) { vQueueDelete(queue); } } -int MessagingInterface::send(char* buffer, int size, int destination, int tag, bool durable) { +int MessagingInterface::send(char* buffer, const int size, const int destination, const int tag, const bool durable) { Flatbuffers::MPIMessageBuilder builder; - const auto [mpi_buffer, mpi_size] = builder.build_mpi_message(Messaging::MessageType_PTP, ConfigManager::get_module_id(), destination, sequence_number++, durable, tag, std::vector(buffer, buffer + size)); + const auto [mpi_buffer, mpi_size] = builder.build_mpi_message(Messaging::MessageType_PTP, m_config_manager.get_module_id(), destination, m_sequence_number++, durable, tag, std::vector(buffer, buffer + size)); m_router->send_msg(static_cast(mpi_buffer), mpi_size); return 0; diff --git a/components/rpc/OrientationDetection.cpp b/components/rpc/OrientationDetection.cpp index f7b26b4..3f5f498 100644 --- a/components/rpc/OrientationDetection.cpp +++ b/components/rpc/OrientationDetection.cpp @@ -10,7 +10,8 @@ #include void OrientationDetection::init() { - for (int i = 0; i < MODULE_TO_NUM_CHANNELS_MAP[ConfigManager::get_module_type()]; i++) { + const auto& config_manager = ConfigManager::get_instance(); + for (int i = 0; i < MODULE_TO_NUM_CHANNELS_MAP[config_manager.get_module_type()]; i++) { setup_gpio(static_cast(CHANNEL_TO_0_DEG_MAP[i])); setup_gpio(static_cast(CHANNEL_TO_90_DEG_MAP[i])); setup_gpio(static_cast(CHANNEL_TO_180_DEG_MAP[i])); diff --git a/components/rpc/WifiManager.cpp b/components/rpc/WifiManager.cpp index 87ea6ae..69a97fd 100644 --- a/components/rpc/WifiManager.cpp +++ b/components/rpc/WifiManager.cpp @@ -1,30 +1,9 @@ -#include "WifiManager.h" - -#include -#include -#include -#include #include -#include -#include "freertos/FreeRTOS.h" -#include "freertos/semphr.h" -#include "esp_wifi.h" +#include "WifiManager.h" +#include "ConfigManager.h" #include "constants/wifi.h" - -WifiManager::WifiManager() { - esp_netif_init(); - esp_wifi_set_storage(WIFI_STORAGE_RAM); - esp_event_loop_create_default(); - - this->m_mutex = xSemaphoreCreateMutex(); - this->m_state = wifi_state::disconnected; - this->m_attempts = 0; - this->m_task = nullptr; - this->m_netif = nullptr; - - xTaskCreate(reinterpret_cast(s_manage), "wifi_task", 3096, this, 5, &m_task); -} +#include "mDNSDiscoveryService.h" WifiManager::~WifiManager() { this->handle_disconnect(); @@ -103,8 +82,8 @@ int WifiManager::init_connection() { } }; - std::string ssid = ConfigManager::get_wifi_ssid(); - std::string pass = ConfigManager::get_wifi_password(); + std::string ssid = m_config_manager.get_wifi_ssid(); + std::string pass = m_config_manager.get_wifi_password(); std::strncpy(reinterpret_cast(wifi_configuration.sta.ssid), ssid.c_str(), 32); std::strncpy(reinterpret_cast(wifi_configuration.sta.password), pass.c_str(), 64); wifi_configuration.sta.ssid[31] = '\0'; diff --git a/components/rpc/include/CommunicationRouter.h b/components/rpc/include/CommunicationRouter.h index 2142cc0..5504b6d 100644 --- a/components/rpc/include/CommunicationRouter.h +++ b/components/rpc/include/CommunicationRouter.h @@ -36,17 +36,18 @@ public: CommunicationRouter(const std::function &rx_callback, std::unique_ptr&& pc_connection) : m_tcp_rx_queue(std::make_shared>>(10)), m_rx_callback(rx_callback), + m_config_manager(ConfigManager::get_instance()), m_tcp_server(std::make_unique(TCP_PORT, m_tcp_rx_queue)), - m_data_link_manager(std::make_unique(ConfigManager::get_module_id(), MODULE_TO_NUM_CHANNELS_MAP[ConfigManager::get_module_type()])), + m_data_link_manager(std::make_unique(m_config_manager.get_module_id(), MODULE_TO_NUM_CHANNELS_MAP[m_config_manager.get_module_type()])), m_pc_connection(std::move(pc_connection)), - m_module_id(ConfigManager::get_module_id()), + m_module_id(m_config_manager.get_module_id()), m_last_leader_updated(std::chrono::system_clock::now()){ OrientationDetection::init(); update_leader(); xTaskCreate(router_thread, "communication_router", 4096, this, 3, &this->m_router_thread); - const auto num_channels = MODULE_TO_NUM_CHANNELS_MAP[ConfigManager::get_module_type()]; + const auto num_channels = MODULE_TO_NUM_CHANNELS_MAP[m_config_manager.get_module_type()]; this->m_link_layer_threads.resize(num_channels); for (int i = 0; i < num_channels; i++) { auto *params = new link_layer_thread_params(this, i); @@ -73,6 +74,7 @@ public: std::function m_rx_callback; private: TaskHandle_t m_router_thread = nullptr; + ConfigManager &m_config_manager; std::vector m_link_layer_threads; std::unique_ptr m_tcp_server; // todo: dependency injection std::unique_ptr m_data_link_manager; diff --git a/components/rpc/include/MessagingInterface.h b/components/rpc/include/MessagingInterface.h index 5f14df0..54315ff 100644 --- a/components/rpc/include/MessagingInterface.h +++ b/components/rpc/include/MessagingInterface.h @@ -14,8 +14,9 @@ class MessagingInterface { public: explicit MessagingInterface(std::unique_ptr&& pc_connection) - : m_mpi_rx_queue(xQueueCreate(MAX_RX_BUFFER_SIZE, RX_QUEUE_SIZE)), - m_router(std::make_unique([this](char* buffer, const int size) { handleRecv(buffer, size); }, std::move(pc_connection))), + : m_config_manager(ConfigManager::get_instance()), + m_mpi_rx_queue(xQueueCreate(MAX_RX_BUFFER_SIZE, RX_QUEUE_SIZE)), + m_router(std::make_unique([this](const char* buffer, const int size) { handleRecv(buffer, size); }, std::move(pc_connection))), m_map_semaphore(xSemaphoreCreateMutex()) {}; ~MessagingInterface(); @@ -31,7 +32,8 @@ private: void checkOrInsertTag(uint8_t tag); - uint16_t sequence_number = 0; + ConfigManager& m_config_manager; + uint16_t m_sequence_number = 0; QueueHandle_t m_mpi_rx_queue; // todo: maybe move this down classes more std::unique_ptr m_router; SemaphoreHandle_t m_map_semaphore; diff --git a/components/rpc/include/WifiManager.h b/components/rpc/include/WifiManager.h index 66a61b8..f389b2f 100644 --- a/components/rpc/include/WifiManager.h +++ b/components/rpc/include/WifiManager.h @@ -2,17 +2,27 @@ #define NETWORKMANAGER_H #include +#include +#include +#include +#include +#include -#include "esp_event.h" - -#include "freertos/FreeRTOS.h" -#include "freertos/semphr.h" - +#include "ConfigManager.h" #include "IWifiManager.h" class WifiManager final : IWifiManager { public: - WifiManager(); + WifiManager() : m_config_manager(ConfigManager::get_instance()), + m_mutex(xSemaphoreCreateMutex()), + m_state(wifi_state::disconnected) { + esp_netif_init(); + esp_wifi_set_storage(WIFI_STORAGE_RAM); + esp_event_loop_create_default(); + // todo: move all task metadata to a constants/config file + xTaskCreate(reinterpret_cast(s_manage), "wifi_task", 3096, this, 5, &m_task); + } + ~WifiManager() override; int connect() override; int disconnect() override; @@ -31,11 +41,12 @@ private: static void wifi_event_handler(void *event_handler_arg, esp_event_base_t event_base, int32_t event_id, void *event_data); + ConfigManager& m_config_manager; SemaphoreHandle_t m_mutex; wifi_state m_state; - TaskHandle_t m_task; - unsigned int m_attempts; - esp_netif_t *m_netif; + TaskHandle_t m_task = nullptr; + unsigned int m_attempts = 0; + esp_netif_t *m_netif = nullptr; }; diff --git a/components/rpc/mDNSDiscoveryService.cpp b/components/rpc/mDNSDiscoveryService.cpp index aab7041..dfe235c 100644 --- a/components/rpc/mDNSDiscoveryService.cpp +++ b/components/rpc/mDNSDiscoveryService.cpp @@ -18,15 +18,18 @@ // todo: clean this up (strange to be a constructor) also need to add more details, need to add to routing table void mDNSDiscoveryService::setup() { mdns_init(); - mdns_hostname_set(std::format("botchain-{}", ConfigManager::get_module_id()).c_str()); - mdns_instance_name_set(std::format("BotChain Robot Module {}", ConfigManager::get_module_id()).c_str()); + + const auto& config_manager = ConfigManager::get_instance(); + + mdns_hostname_set(std::format("botchain-{}", config_manager.get_module_id()).c_str()); + mdns_instance_name_set(std::format("BotChain Robot Module {}", config_manager.get_module_id()).c_str()); mdns_service_add(nullptr, "_robotcontrol", "_tcp", TCP_PORT, nullptr, 0); mdns_service_instance_name_set("_robotcontrol", "_tcp", "Robot Control TCP Server"); mdns_txt_item_t service_txt_data[3] = { - {"module_id",std::to_string(ConfigManager::get_module_id()).c_str()}, - {"module_type",std::to_string(ConfigManager::get_module_type()).c_str()}, + {"module_id",std::to_string(config_manager.get_module_id()).c_str()}, + {"module_type",std::to_string(config_manager.get_module_type()).c_str()}, {"connected_modules",""}, }; @@ -36,6 +39,8 @@ void mDNSDiscoveryService::setup() { void mDNSDiscoveryService::set_connected_boards(const std::vector& boards) { std::stringstream ss; + const auto& config_manager = ConfigManager::get_instance(); + for (int i = 0; i < boards.size(); i++) { ss << std::to_string(boards[i]); if (i < boards.size() - 1) { @@ -44,8 +49,8 @@ void mDNSDiscoveryService::set_connected_boards(const std::vector& boards) } mdns_txt_item_t service_txt_data[3] = { - {"module_id",std::to_string(ConfigManager::get_module_id()).c_str()}, - {"module_type",std::to_string(ConfigManager::get_module_type()).c_str()}, + {"module_id",std::to_string(config_manager.get_module_id()).c_str()}, + {"module_type",std::to_string(config_manager.get_module_type()).c_str()}, {"connected_modules",ss.str().c_str()}, }; mdns_service_txt_set("_robotcontrol", "_tcp", service_txt_data, 3); diff --git a/main/LoopManager.cpp b/main/LoopManager.cpp index 0d03f98..66f70d0 100644 --- a/main/LoopManager.cpp +++ b/main/LoopManager.cpp @@ -17,7 +17,7 @@ #define METADATA_PERIOD_MS 1000 [[noreturn]] void LoopManager::control_loop() const { - const auto actuator = ActuatorFactory::create_actuator(ConfigManager::get_module_type()); + const auto actuator = ActuatorFactory::create_actuator(m_config_manager.get_module_type()); uint8_t buffer[512]; while (true) { @@ -38,7 +38,7 @@ casted_orientations.emplace_back(orientation); } - const auto [data, size] = topology_message_builder->build_topology_message(ConfigManager::get_module_id(), ConfigManager::get_module_type(), module_ids, casted_orientations); + const auto [data, size] = topology_message_builder->build_topology_message(that->m_config_manager.get_module_id(), that->m_config_manager.get_module_type(), module_ids, casted_orientations); that->m_messaging_interface->send(static_cast(data), size, PC_ADDR, TOPOLOGY_CMD_TAG, true); vTaskDelay(METADATA_PERIOD_MS / portTICK_PERIOD_MS); } diff --git a/main/include/LoopManager.h b/main/include/LoopManager.h index cda0aec..3d6ff44 100644 --- a/main/include/LoopManager.h +++ b/main/include/LoopManager.h @@ -14,11 +14,14 @@ class LoopManager { public: - LoopManager() : m_messaging_interface(std::make_unique(std::make_unique())) {} + LoopManager() : m_config_manager(ConfigManager::get_instance()), + m_messaging_interface(std::make_unique(std::make_unique())) {} [[noreturn]] void control_loop() const; [[noreturn]] static void metadata_tx_loop(char * args); + [[noreturn]] static void metadata_rx_loop(char * args); private: + ConfigManager& m_config_manager; std::unique_ptr m_messaging_interface; private: diff --git a/main/main.cpp b/main/main.cpp index 9e03187..8d30a8f 100644 --- a/main/main.cpp +++ b/main/main.cpp @@ -1,25 +1,25 @@ #include #include -#include "sdkconfig.h" #include "freertos/FreeRTOS.h" +#include "sdkconfig.h" -#include "WifiManager.h" -#include "mDNSDiscoveryService.h" -#include "TCPServer.h" #include "ConfigManager.h" #include "LoopManager.h" +#include "TCPServer.h" +#include "WifiManager.h" #include "esp_log.h" +#include "mDNSDiscoveryService.h" extern "C" [[noreturn]] void app_main(void) { - ESP_LOGI("MEM", "Free internal RAM: %d", heap_caps_get_free_size(MALLOC_CAP_8BIT)); + ESP_LOGI("MEM", "Free internal RAM: %d", + heap_caps_get_free_size(MALLOC_CAP_8BIT)); ESP_LOGI("MEM", "Free PSRAM: %d", heap_caps_get_free_size(MALLOC_CAP_SPIRAM)); - ConfigManager::init_config(); - - + auto& config_manager = ConfigManager::get_instance(); // NOLINT - here for easily adding temporary config const auto loop_manager = std::make_unique(); - xTaskCreate(reinterpret_cast(LoopManager::metadata_tx_loop), "metadata_tx", 3096, loop_manager.get(), 3, nullptr); + xTaskCreate(reinterpret_cast(LoopManager::metadata_tx_loop), + "metadata_tx", 3096, loop_manager.get(), 3, nullptr); loop_manager->control_loop(); }