Configuration

This commit is contained in:
2025-06-12 20:13:04 -04:00
parent 96a83027ca
commit 84b65104fb
12 changed files with 124 additions and 39 deletions

View File

@@ -0,0 +1,3 @@
idf_component_register(SRCS "ConfigManager.cpp"
PRIV_REQUIRES constants nvs_flash
INCLUDE_DIRS "include")

View File

@@ -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<ModuleType>(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);
}

View File

@@ -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

View File

@@ -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

View File

@@ -13,4 +13,6 @@
#define SLEEP_AFTER_FAIL_MS 5000
#define TCP_PORT 3001
#endif //TCP_H

View File

@@ -1,3 +0,0 @@
idf_component_register(SRCS "logger.cpp"
PRIV_REQUIRES
INCLUDE_DIRS "include")

View File

@@ -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

View File

@@ -1,3 +0,0 @@
//
// Created by Johnathon Slightham on 2025-05-26.
//

View File

@@ -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")

View File

@@ -4,13 +4,21 @@
#include "mdns.h"
#include "ConfigManager.h"
#include "mDNSDiscoveryService.h"
#include "constants/tcp.h"
#include <string>
#include <format>
// 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");
}