diff --git a/components/config/CMakeLists.txt b/components/config/CMakeLists.txt new file mode 100644 index 0000000..8cb522d --- /dev/null +++ b/components/config/CMakeLists.txt @@ -0,0 +1,3 @@ +idf_component_register(SRCS "ConfigManager.cpp" + PRIV_REQUIRES constants nvs_flash + INCLUDE_DIRS "include") diff --git a/components/config/ConfigManager.cpp b/components/config/ConfigManager.cpp new file mode 100644 index 0000000..1863e06 --- /dev/null +++ b/components/config/ConfigManager.cpp @@ -0,0 +1,59 @@ +// +// Created by Johnathon Slightham on 2025-06-12. +// + +#include "nvs_flash.h" +#include "constants/config.h" + +#include "ConfigManager.h" + +void ConfigManager::init_config() { + nvs_flash_init(); + + 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, "id", &id)) { + nvs_set_u16(config_handle, MODULE_ID_KEY, DEFAULT_MODULE_ID); + } + + uint16_t type; + if (ESP_ERR_NVS_NOT_FOUND == nvs_get_u16(config_handle, "id", &type)) { + nvs_set_u16(config_handle, MODULE_TYPE_KEY, DEFAULT_MODULE_TYPE); + } + + nvs_close(config_handle); +} + +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; +} + +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); + return static_cast(type); +} + +void ConfigManager::set_module_id(const uint16_t id) { + nvs_handle config_handle; + nvs_open(NVS_FLASH_NAMESPACE, NVS_READWRITE, &config_handle); + nvs_set_u16(config_handle, MODULE_ID_KEY, id); + nvs_close(config_handle); +} + +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); +} diff --git a/components/config/include/ConfigManager.h b/components/config/include/ConfigManager.h new file mode 100644 index 0000000..536b6a8 --- /dev/null +++ b/components/config/include/ConfigManager.h @@ -0,0 +1,21 @@ +// +// Created by Johnathon Slightham on 2025-06-12. +// + +#ifndef CONFIGMANAGER_H +#define CONFIGMANAGER_H + +#include "constants/config.h" + +class ConfigManager { +public: + static void init_config(); + + static uint16_t get_module_id(); + static ModuleType get_module_type(); + + static void set_module_id(uint16_t id); + static void set_module_type(ModuleType type); +}; + +#endif //CONFIGMANAGER_H diff --git a/components/constants/include/constants/config.h b/components/constants/include/constants/config.h new file mode 100644 index 0000000..32807fe --- /dev/null +++ b/components/constants/include/constants/config.h @@ -0,0 +1,20 @@ +// +// Created by Johnathon Slightham on 2025-06-12. +// + +#ifndef CONFIG_H +#define CONFIG_H + +#define NVS_FLASH_NAMESPACE "app" +#define DEFAULT_MODULE_ID 0 +#define DEFAULT_MODULE_TYPE 0 + +#define MODULE_ID_KEY "module_id" +#define MODULE_TYPE_KEY "module_type" + + +enum ModuleType { splitter }; // todo: maybe this should be flatbuffer + + + +#endif //CONFIG_H diff --git a/components/constants/include/constants/tcp.h b/components/constants/include/constants/tcp.h index 2714557..bd4ec8f 100644 --- a/components/constants/include/constants/tcp.h +++ b/components/constants/include/constants/tcp.h @@ -13,4 +13,6 @@ #define SLEEP_AFTER_FAIL_MS 5000 +#define TCP_PORT 3001 + #endif //TCP_H diff --git a/components/logger/CMakeLists.txt b/components/logger/CMakeLists.txt deleted file mode 100644 index 90e2e28..0000000 --- a/components/logger/CMakeLists.txt +++ /dev/null @@ -1,3 +0,0 @@ -idf_component_register(SRCS "logger.cpp" - PRIV_REQUIRES - INCLUDE_DIRS "include") diff --git a/components/logger/include/logger.h b/components/logger/include/logger.h deleted file mode 100644 index c2d9c95..0000000 --- a/components/logger/include/logger.h +++ /dev/null @@ -1,20 +0,0 @@ -// -// Created by Johnathon Slightham on 2025-05-26. -// - -#ifndef LOGGER_H -#define LOGGER_H - -class Logger { -public: - static Logger &getInstance() { - static Logger instance; - return instance; - } - -private: - virtual Logger(); - -} - -#endif //LOGGER_H diff --git a/components/logger/logger.cpp b/components/logger/logger.cpp deleted file mode 100644 index 32d6587..0000000 --- a/components/logger/logger.cpp +++ /dev/null @@ -1,3 +0,0 @@ -// -// Created by Johnathon Slightham on 2025-05-26. -// diff --git a/components/rpc/CMakeLists.txt b/components/rpc/CMakeLists.txt index 213ab93..deece47 100644 --- a/components/rpc/CMakeLists.txt +++ b/components/rpc/CMakeLists.txt @@ -1,3 +1,3 @@ -idf_component_register(SRCS "rpc.cpp" "WifiManager.cpp" "mDNSDiscoveryService.cpp" "TCPServer" - PRIV_REQUIRES driver esp_event nvs_flash esp_netif esp_wifi espressif__mdns constants +idf_component_register(SRCS "rpc.cpp" "WifiManager.cpp" "mDNSDiscoveryService.cpp" "TCPServer.cpp" + PRIV_REQUIRES driver esp_event nvs_flash esp_netif esp_wifi espressif__mdns constants config INCLUDE_DIRS "include") diff --git a/components/rpc/mDNSDiscoveryService.cpp b/components/rpc/mDNSDiscoveryService.cpp index bba0829..d4afb97 100644 --- a/components/rpc/mDNSDiscoveryService.cpp +++ b/components/rpc/mDNSDiscoveryService.cpp @@ -4,13 +4,21 @@ #include "mdns.h" +#include "ConfigManager.h" + #include "mDNSDiscoveryService.h" +#include "constants/tcp.h" + +#include +#include + +// todo: clean this up (strange to be a constructor) also need to add more details mDNSDiscoveryService::mDNSDiscoveryService() { mdns_init(); - mdns_hostname_set("botchain-0000"); - mdns_instance_name_set("BotChain Robot Module 0000"); + 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()); - mdns_service_add(NULL, "_robotcontrol", "_udp", 3000, NULL, 0); - mdns_service_instance_name_set("_robotcontrol", "_udp", "Robot Control UDP Server"); + mdns_service_add(nullptr, "_robotcontrol", "_tcp", TCP_PORT, nullptr, 0); + mdns_service_instance_name_set("_robotcontrol", "_tcp", "Robot Control TCP Server"); } diff --git a/main/CMakeLists.txt b/main/CMakeLists.txt index b550b8b..ff92540 100644 --- a/main/CMakeLists.txt +++ b/main/CMakeLists.txt @@ -1,3 +1,3 @@ idf_component_register(SRCS "main.cpp" - PRIV_REQUIRES spi_flash nvs_flash esp_event rpc + PRIV_REQUIRES spi_flash nvs_flash esp_event rpc constants config INCLUDE_DIRS "") diff --git a/main/main.cpp b/main/main.cpp index c2dd00d..de5485e 100644 --- a/main/main.cpp +++ b/main/main.cpp @@ -4,24 +4,22 @@ #include "sdkconfig.h" #include "freertos/FreeRTOS.h" #include "freertos/task.h" -#include "esp_flash.h" -#include "nvs_flash.h" #include "WifiManager.h" #include "mDNSDiscoveryService.h" #include "TCPServer.h" +#include "ConfigManager.h" +#include "constants/tcp.h" extern "C" [[noreturn]] void app_main(void) { - nvs_flash_init(); + ConfigManager::init_config(); const auto manager = std::make_unique(); manager->connect(); const auto discovery = std::make_unique(); - vTaskDelay(20000 / portTICK_PERIOD_MS); - - const auto tcp_server = std::make_unique(3001); + const auto tcp_server = std::make_unique(TCP_PORT); printf("Hello world!\n");