From cf316efee5885abcbe3a2490218e79bdb8e0a388 Mon Sep 17 00:00:00 2001 From: Johnathon Slightham Date: Wed, 22 Oct 2025 23:07:41 -0400 Subject: [PATCH] Cleanup rpc interfaces --- components/config/ConfigManager.cpp | 10 ++++ components/config/include/ConfigManager.h | 3 ++ components/config/include/enums.h | 12 +++++ .../constants/include/constants/config.h | 16 ++++--- components/ptrQueue/include/PtrQueue.h | 6 ++- components/rpc/CMakeLists.txt | 8 +++- components/rpc/CommunicationFactory.cpp | 47 +++++++++++++++++++ components/rpc/CommunicationRouter.cpp | 19 +++----- components/rpc/MessagingInterface.cpp | 1 + components/rpc/include/CommunicationFactory.h | 27 +++++++++++ components/rpc/include/CommunicationRouter.h | 24 ++++++---- .../{IWifiManager.h => IConnectionManager.h} | 8 ++-- components/rpc/include/IDiscoveryService.h | 6 +-- components/rpc/include/MessagingInterface.h | 4 +- components/rpc/include/RPCFactory.h | 14 ------ components/rpc/include/mDNSDiscoveryService.h | 19 -------- .../rpc/include/{ => wireless}/TCPServer.h | 2 +- .../rpc/include/{ => wireless}/WifiManager.h | 4 +- .../include/wireless/mDNSDiscoveryService.h | 20 ++++++++ components/rpc/{ => wireless}/TCPServer.cpp | 6 +-- components/rpc/{ => wireless}/WifiManager.cpp | 5 +- .../{ => wireless}/mDNSDiscoveryService.cpp | 8 +++- main/LoopManager.cpp | 7 ++- main/include/LoopManager.h | 14 ++---- main/include/control/ISensor.h | 14 ++++++ main/include/control/SensorFactory.h | 13 +++++ main/include/control/Servo1Actuator.h | 1 + main/main.cpp | 2 - 28 files changed, 223 insertions(+), 97 deletions(-) create mode 100644 components/config/include/enums.h create mode 100644 components/rpc/CommunicationFactory.cpp create mode 100644 components/rpc/include/CommunicationFactory.h rename components/rpc/include/{IWifiManager.h => IConnectionManager.h} (55%) delete mode 100644 components/rpc/include/RPCFactory.h delete mode 100644 components/rpc/include/mDNSDiscoveryService.h rename components/rpc/include/{ => wireless}/TCPServer.h (95%) rename components/rpc/include/{ => wireless}/WifiManager.h (94%) create mode 100644 components/rpc/include/wireless/mDNSDiscoveryService.h rename components/rpc/{ => wireless}/TCPServer.cpp (98%) rename components/rpc/{ => wireless}/WifiManager.cpp (98%) rename components/rpc/{ => wireless}/mDNSDiscoveryService.cpp (91%) create mode 100644 main/include/control/ISensor.h create mode 100644 main/include/control/SensorFactory.h diff --git a/components/config/ConfigManager.cpp b/components/config/ConfigManager.cpp index d1271b2..6e5ce46 100644 --- a/components/config/ConfigManager.cpp +++ b/components/config/ConfigManager.cpp @@ -52,6 +52,12 @@ std::string ConfigManager::get_wifi_password() const { return str; } +CommunicationMethod ConfigManager::get_communication_method() const { + uint16_t type = DEFAULT_COMMUNICATION_METHOD; + get_uint16(COMMUNICATION_METHOD_KEY, &type); + return static_cast(type); +} + 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; @@ -133,6 +139,10 @@ void ConfigManager::set_wifi_password(const char* password) { set(WIFI_PASSWORD_KEY, password); } +void ConfigManager::set_communication_method(const CommunicationMethod method) { + set(COMMUNICATION_METHOD_KEY, method); +} + // Func - the esp write function to call (ie. nvs_set_u16) // T - the type of the value in the key value pair template diff --git a/components/config/include/ConfigManager.h b/components/config/include/ConfigManager.h index 137a6a0..88a177e 100644 --- a/components/config/include/ConfigManager.h +++ b/components/config/include/ConfigManager.h @@ -9,6 +9,7 @@ #include #include +#include "enums.h" #include "esp_check.h" #include "nvs.h" #include "flatbuffers_generated/RobotModule_generated.h" @@ -30,11 +31,13 @@ public: ModuleType get_module_type() const; std::string get_wifi_ssid() const; std::string get_wifi_password() const; + CommunicationMethod get_communication_method() 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); + void set_communication_method(CommunicationMethod method); private: ConfigManager() = default; diff --git a/components/config/include/enums.h b/components/config/include/enums.h new file mode 100644 index 0000000..66e747c --- /dev/null +++ b/components/config/include/enums.h @@ -0,0 +1,12 @@ +// +// Created by Johnathon Slightham on 2025-10-22. +// + +#ifndef ENUMS_H +#define ENUMS_H + +enum CommunicationMethod { + Wireless, +}; + +#endif //ENUMS_H diff --git a/components/constants/include/constants/config.h b/components/constants/include/constants/config.h index f5d14be..905e13f 100644 --- a/components/constants/include/constants/config.h +++ b/components/constants/include/constants/config.h @@ -5,13 +5,15 @@ #ifndef CONFIG_H #define CONFIG_H -#define NVS_FLASH_NAMESPACE "app" -#define DEFAULT_MODULE_ID 1 -#define DEFAULT_MODULE_TYPE 0 +#define NVS_FLASH_NAMESPACE "app" +#define DEFAULT_MODULE_ID 1 +#define DEFAULT_MODULE_TYPE 0 +#define DEFAULT_COMMUNICATION_METHOD 0 -#define MODULE_ID_KEY "module_id" -#define MODULE_TYPE_KEY "module_type" -#define WIFI_SSID_KEY "wifi_ssid" -#define WIFI_PASSWORD_KEY "wifi_password" +#define MODULE_ID_KEY "module_id" +#define MODULE_TYPE_KEY "module_type" +#define WIFI_SSID_KEY "wifi_ssid" +#define WIFI_PASSWORD_KEY "wifi_password" +#define COMMUNICATION_METHOD_KEY "comm_method" #endif //CONFIG_H diff --git a/components/ptrQueue/include/PtrQueue.h b/components/ptrQueue/include/PtrQueue.h index 4cf2210..a44b86a 100644 --- a/components/ptrQueue/include/PtrQueue.h +++ b/components/ptrQueue/include/PtrQueue.h @@ -6,7 +6,11 @@ #define PTRQUEUE_H #include -#include + +#include "freertos/FreeRTOS.h" +#include "portmacro.h" +#include "freertos/projdefs.h" +#include "freertos/queue.h" // Wrapped FreeRTOS queue to support unique_ptr diff --git a/components/rpc/CMakeLists.txt b/components/rpc/CMakeLists.txt index 411da10..f82acdc 100644 --- a/components/rpc/CMakeLists.txt +++ b/components/rpc/CMakeLists.txt @@ -1,4 +1,10 @@ -idf_component_register(SRCS "WifiManager.cpp" "mDNSDiscoveryService.cpp" "TCPServer.cpp" "CommunicationRouter.cpp" "MessagingInterface.cpp" "OrientationDetection.cpp" +file(GLOB_RECURSE ALL_SRCS + "${CMAKE_CURRENT_SOURCE_DIR}/*.c" + "${CMAKE_CURRENT_SOURCE_DIR}/*.cpp" + "${CMAKE_CURRENT_SOURCE_DIR}/*.S" +) + +idf_component_register(SRCS ${ALL_SRCS} PRIV_REQUIRES driver esp_event nvs_flash esp_netif espressif__mdns constants config flatbuffers dataLink rmt REQUIRES ptrQueue esp_wifi INCLUDE_DIRS "include") diff --git a/components/rpc/CommunicationFactory.cpp b/components/rpc/CommunicationFactory.cpp new file mode 100644 index 0000000..875b784 --- /dev/null +++ b/components/rpc/CommunicationFactory.cpp @@ -0,0 +1,47 @@ +// +// Created by Johnathon Slightham on 2025-10-22. +// + +#include + +#include "CommunicationFactory.h" +#include "constants/tcp.h" +#include "wireless/mDNSDiscoveryService.h" +#include "wireless/TCPServer.h" +#include "wireless/WifiManager.h" + +std::unique_ptr CommunicationFactory::create_connection_manager(const CommunicationMethod type) { + switch (type) { + case Wireless: + return std::make_unique(); + default: + return nullptr; + } +} + +std::unique_ptr CommunicationFactory::create_discovery_service(const CommunicationMethod type) { + switch (type) { + case Wireless: + return std::make_unique(); + default: + return nullptr; + } +} + +std::unique_ptr CommunicationFactory::create_lossy_server(const CommunicationMethod type, const std::shared_ptr>>& rx_queue) { + switch (type) { + case Wireless: + return std::make_unique(TCP_PORT, rx_queue); // todo: replace with udp server + default: + return nullptr; + } +} + +std::unique_ptr CommunicationFactory::create_lossless_server(const CommunicationMethod type, const std::shared_ptr>>& rx_queue) { + switch (type) { + case Wireless: + return std::make_unique(TCP_PORT, rx_queue); + default: + return nullptr; + } +} diff --git a/components/rpc/CommunicationRouter.cpp b/components/rpc/CommunicationRouter.cpp index be5fc7f..7d60024 100644 --- a/components/rpc/CommunicationRouter.cpp +++ b/components/rpc/CommunicationRouter.cpp @@ -2,9 +2,9 @@ #include "CommunicationRouter.h" #include "AngleControlMessageBuilder.h" -#include "mDNSDiscoveryService.h" +#include "include/wireless/mDNSDiscoveryService.h" #include "MPIMessageBuilder.h" -#include "WifiManager.h" +#include "include/wireless/WifiManager.h" #include "freertos/FreeRTOS.h" #include "Tables.h" #include "PtrQueue.h" @@ -19,7 +19,6 @@ CommunicationRouter::~CommunicationRouter() { // todo: we really need to change all char to uint8_t everywhere // todo: get rid of copying going on, need to pass around sharedptrs/uniqueptrs -// todo: this needs to be combined with the 4 rmt threads [[noreturn]] void CommunicationRouter::router_thread(void *args) { const auto that = static_cast(args); @@ -40,7 +39,6 @@ CommunicationRouter::~CommunicationRouter() { that->m_data_link_manager->start_receive_frames(channel); while (true) { char buffer[512]; - // todo: very c style function calls const auto err = that->m_data_link_manager->receive(reinterpret_cast(buffer), 512, &bytes_received, channel); that->m_data_link_manager->start_receive_frames(channel); @@ -66,7 +64,6 @@ int CommunicationRouter::send_msg(char* buffer, const size_t length) const { return 0; } -// todo: the number of things this is doing in so many different places is crazy... void CommunicationRouter::update_leader() { RIPRow_public table[RIP_MAX_ROUTES]; size_t table_size = RIP_MAX_ROUTES; @@ -95,26 +92,24 @@ void CommunicationRouter::update_leader() { this->m_leader = max; if (this->m_leader == m_module_id) { - mDNSDiscoveryService::set_connected_boards(connected_module_ids); + this->m_discovery_service->set_connected_boards(connected_module_ids); } } void CommunicationRouter::route(uint8_t* buffer, const size_t length) const { flatbuffers::Verifier verifier(buffer, length); - bool ok = Messaging::VerifyMPIMessageBuffer(verifier); - if (!ok) { // This could be moved to just be called on wireline data to save cpu cycles. + // This could be moved to just be called on wireline data to save cpu cycles. + if (bool ok = Messaging::VerifyMPIMessageBuffer(verifier); !ok) { ESP_LOGW(TAG, "route: got an invalid MPI message, disregarding"); return; } - const auto& mpi_message = Flatbuffers::MPIMessageBuilder::parse_mpi_message(buffer); - - if (mpi_message->destination() == m_module_id) { + if (const auto& mpi_message = Flatbuffers::MPIMessageBuilder::parse_mpi_message(buffer); mpi_message->destination() == m_module_id) { ESP_LOGD(TAG, "Routing to this module [dest: %d, length: %d]", static_cast(mpi_message->destination()), length); this->m_rx_callback(reinterpret_cast(buffer), 512); } else if (mpi_message->destination() == PC_ADDR && this->m_leader == m_module_id) { ESP_LOGD(TAG, "Routing to wifi [dest: %d, length: %d]", static_cast(mpi_message->destination()), length); - this->m_tcp_server->send_msg(reinterpret_cast(buffer), 512); + this->m_lossless_server->send_msg(reinterpret_cast(buffer), 512); } else if (mpi_message->destination() == PC_ADDR) { ESP_LOGD(TAG, "Routing to wireline for wifi [dest: %d, length: %d]", static_cast(mpi_message->destination()), length); this->m_data_link_manager->send(this->m_leader, buffer, length, FrameType::MOTOR_TYPE, 0); diff --git a/components/rpc/MessagingInterface.cpp b/components/rpc/MessagingInterface.cpp index 485f77b..99bef0e 100644 --- a/components/rpc/MessagingInterface.cpp +++ b/components/rpc/MessagingInterface.cpp @@ -5,6 +5,7 @@ #include "MessagingInterface.h" #include "AngleControlMessageBuilder.h" #include "ConfigManager.h" +#include "freertos/FreeRTOS.h" #include "freertos/queue.h" #include "freertos/semphr.h" #include "MPIMessageBuilder.h" diff --git a/components/rpc/include/CommunicationFactory.h b/components/rpc/include/CommunicationFactory.h new file mode 100644 index 0000000..d5c5ac5 --- /dev/null +++ b/components/rpc/include/CommunicationFactory.h @@ -0,0 +1,27 @@ +// +// Created by Johnathon Slightham on 2025-10-22. +// + +// Although we currently only support wireless, this is architected to be easy to add other connection methods + +#ifndef COMMUNICATIONFACTORY_H +#define COMMUNICATIONFACTORY_H + +#include +#include + +#include "IConnectionManager.h" +#include "IDiscoveryService.h" +#include "IRPCServer.h" +#include "PtrQueue.h" +#include "enums.h" + +class CommunicationFactory { +public: + static std::unique_ptr create_connection_manager(CommunicationMethod type); + static std::unique_ptr create_discovery_service(CommunicationMethod type); + static std::unique_ptr create_lossy_server(CommunicationMethod type, const std::shared_ptr>> &rx_queue); + static std::unique_ptr create_lossless_server(CommunicationMethod type, const std::shared_ptr>>& rx_queue); +}; + +#endif //COMMUNICATIONFACTORY_H diff --git a/components/rpc/include/CommunicationRouter.h b/components/rpc/include/CommunicationRouter.h index ae1cab0..dbf425f 100644 --- a/components/rpc/include/CommunicationRouter.h +++ b/components/rpc/include/CommunicationRouter.h @@ -2,6 +2,8 @@ // Created by Johnathon Slightham on 2025-05-25. // +// todo: this class is getting a bit large + #ifndef COMMUNICATIONROUTER_H #define COMMUNICATIONROUTER_H @@ -9,13 +11,13 @@ #include #include +#include "CommunicationFactory.h" +#include "IDiscoveryService.h" #include "ConfigManager.h" #include "OrientationDetection.h" -#include "WifiManager.h" -#include "freertos/FreeRTOS.h" -#include "TCPServer.h" +#include "wireless/WifiManager.h" +#include "wireless/TCPServer.h" #include "DataLinkManager.h" -#include "constants/tcp.h" #include "constants/module.h" #include "PtrQueue.h" @@ -29,15 +31,16 @@ class CommunicationRouter { }; public: - CommunicationRouter(const std::function &rx_callback, std::unique_ptr&& pc_connection) + explicit CommunicationRouter(const std::function &rx_callback) : 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_pc_connection(CommunicationFactory::create_connection_manager(m_config_manager.get_communication_method())), + m_lossless_server(CommunicationFactory::create_lossless_server(m_config_manager.get_communication_method(), m_tcp_rx_queue)), 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(m_config_manager.get_module_id()), - m_last_leader_updated(std::chrono::system_clock::now()){ + m_last_leader_updated(std::chrono::system_clock::now()), + m_discovery_service(CommunicationFactory::create_discovery_service(m_config_manager.get_communication_method())){ OrientationDetection::init(); update_leader(); @@ -67,13 +70,14 @@ public: private: TaskHandle_t m_router_thread = nullptr; ConfigManager &m_config_manager; + std::unique_ptr m_pc_connection; std::vector m_link_layer_threads; - std::unique_ptr m_tcp_server; // todo: dependency injection + std::unique_ptr m_lossless_server; std::unique_ptr m_data_link_manager; - std::unique_ptr m_pc_connection; // todo: change to dependency inject uint8_t m_leader = 0; uint8_t m_module_id; std::chrono::time_point m_last_leader_updated; + std::unique_ptr m_discovery_service; }; #endif //COMMUNICATIONROUTER_H diff --git a/components/rpc/include/IWifiManager.h b/components/rpc/include/IConnectionManager.h similarity index 55% rename from components/rpc/include/IWifiManager.h rename to components/rpc/include/IConnectionManager.h index 30ed6f1..1065612 100644 --- a/components/rpc/include/IWifiManager.h +++ b/components/rpc/include/IConnectionManager.h @@ -2,12 +2,12 @@ // Created by Johnathon Slightham on 2025-05-26. // -#ifndef IWIFIMANAGER_H -#define IWIFIMANAGER_H +#ifndef ICONNECTIONMANAGER_H +#define ICONNECTIONMANAGER_H -class IWifiManager { +class IConnectionManager{ public: - virtual ~IWifiManager() = default; + virtual ~IConnectionManager() = default; virtual int connect() = 0; virtual int disconnect() = 0; }; diff --git a/components/rpc/include/IDiscoveryService.h b/components/rpc/include/IDiscoveryService.h index fcf0fd2..2453d44 100644 --- a/components/rpc/include/IDiscoveryService.h +++ b/components/rpc/include/IDiscoveryService.h @@ -8,8 +8,8 @@ class IDiscoveryService { public: virtual ~IDiscoveryService() = default; - virtual static void setup() = 0; - virtual static void set_connected_boards(const std::vector& boards) = 0; -} + + virtual void set_connected_boards(const std::vector& boards) = 0; +}; #endif //IDISCOVERYSERVICE_H diff --git a/components/rpc/include/MessagingInterface.h b/components/rpc/include/MessagingInterface.h index f4d5091..7fdbab2 100644 --- a/components/rpc/include/MessagingInterface.h +++ b/components/rpc/include/MessagingInterface.h @@ -14,10 +14,10 @@ class MessagingInterface { public: - explicit MessagingInterface(std::unique_ptr&& pc_connection) + explicit MessagingInterface() : 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_router(std::make_unique([this](const char* buffer, const int size) { handleRecv(buffer, size); })), m_map_semaphore(xSemaphoreCreateMutex()) {}; ~MessagingInterface(); diff --git a/components/rpc/include/RPCFactory.h b/components/rpc/include/RPCFactory.h deleted file mode 100644 index 803e931..0000000 --- a/components/rpc/include/RPCFactory.h +++ /dev/null @@ -1,14 +0,0 @@ -// -// Created by Johnathon Slightham on 2025-05-25. -// - -#ifndef RPCFACTORY_H -#define RPCFACTORY_H - -class RPCFactory { -public: -static std::shared_ptr createRPCServer() { - return std::make_shared(); -} - -#endif //RPCFACTORY_H diff --git a/components/rpc/include/mDNSDiscoveryService.h b/components/rpc/include/mDNSDiscoveryService.h deleted file mode 100644 index 5695e80..0000000 --- a/components/rpc/include/mDNSDiscoveryService.h +++ /dev/null @@ -1,19 +0,0 @@ -// -// Created by Johnathon Slightham on 2025-05-25. -// - -#ifndef DISCOVERYSERVICE_H -#define DISCOVERYSERVICE_H - -#include - -class mDNSDiscoveryService final { -public: - mDNSDiscoveryService() = delete; - ~mDNSDiscoveryService() = delete; - - static void setup(); - static void set_connected_boards(const std::vector& boards); -}; - -#endif //DISCOVERYSERVICE_H diff --git a/components/rpc/include/TCPServer.h b/components/rpc/include/wireless/TCPServer.h similarity index 95% rename from components/rpc/include/TCPServer.h rename to components/rpc/include/wireless/TCPServer.h index 7d9a555..299fb81 100644 --- a/components/rpc/include/TCPServer.h +++ b/components/rpc/include/wireless/TCPServer.h @@ -13,7 +13,7 @@ #include "IRPCServer.h" #include "PtrQueue.h" -class TCPServer final : IRPCServer { +class TCPServer final : public IRPCServer { public: TCPServer(int port, const std::shared_ptr>>& rx_queue); ~TCPServer() override; diff --git a/components/rpc/include/WifiManager.h b/components/rpc/include/wireless/WifiManager.h similarity index 94% rename from components/rpc/include/WifiManager.h rename to components/rpc/include/wireless/WifiManager.h index bae3d7f..718796e 100644 --- a/components/rpc/include/WifiManager.h +++ b/components/rpc/include/wireless/WifiManager.h @@ -8,9 +8,9 @@ #include "freertos/FreeRTOS.h" #include "freertos/semphr.h" #include "ConfigManager.h" -#include "IWifiManager.h" +#include "IConnectionManager.h" -class WifiManager final : IWifiManager { +class WifiManager final : public IConnectionManager { public: WifiManager() : m_config_manager(ConfigManager::get_instance()), m_mutex(xSemaphoreCreateMutex()), diff --git a/components/rpc/include/wireless/mDNSDiscoveryService.h b/components/rpc/include/wireless/mDNSDiscoveryService.h new file mode 100644 index 0000000..628d4a3 --- /dev/null +++ b/components/rpc/include/wireless/mDNSDiscoveryService.h @@ -0,0 +1,20 @@ +// +// Created by Johnathon Slightham on 2025-05-25. +// + +#ifndef DISCOVERYSERVICE_H +#define DISCOVERYSERVICE_H + +#include + +#include "IDiscoveryService.h" + +class mDNSDiscoveryService final : public IDiscoveryService { +public: + mDNSDiscoveryService(); + ~mDNSDiscoveryService() override; + + void set_connected_boards(const std::vector& boards) override; +}; + +#endif //DISCOVERYSERVICE_H diff --git a/components/rpc/TCPServer.cpp b/components/rpc/wireless/TCPServer.cpp similarity index 98% rename from components/rpc/TCPServer.cpp rename to components/rpc/wireless/TCPServer.cpp index d9f4229..2577b6a 100644 --- a/components/rpc/TCPServer.cpp +++ b/components/rpc/wireless/TCPServer.cpp @@ -11,7 +11,7 @@ #include "lwip/sockets.h" #include "lwip/sys.h" #include "lwip/netdb.h" -#include "TCPServer.h" +#include "wireless/TCPServer.h" #include "bits/shared_ptr_base.h" #include "constants/app_comms.h" #include "constants/tcp.h" @@ -212,11 +212,11 @@ bool TCPServer::is_network_connected() { } bool TCPServer::authenticate_client(int sock) { - // todo: authentication (wait for a passphrase from the client) + // todo: authentication (key?) return 0; } -int TCPServer::send_msg(char *buffer, uint32_t length) const { +int TCPServer::send_msg(char *buffer, const uint32_t length) const { if (!is_network_connected()) { return -1; } diff --git a/components/rpc/WifiManager.cpp b/components/rpc/wireless/WifiManager.cpp similarity index 98% rename from components/rpc/WifiManager.cpp rename to components/rpc/wireless/WifiManager.cpp index 784966e..97340e3 100644 --- a/components/rpc/WifiManager.cpp +++ b/components/rpc/wireless/WifiManager.cpp @@ -1,9 +1,8 @@ #include -#include "WifiManager.h" +#include "wireless/WifiManager.h" #include "ConfigManager.h" #include "constants/wifi.h" -#include "mDNSDiscoveryService.h" #define TAG "WifiManager" #define SOFTAP_SCAN_FREQUENCY_MS 30000 @@ -219,7 +218,6 @@ void WifiManager::wifi_event_handler(void *event_handler_arg, esp_event_base_t e } else if (WIFI_EVENT_STA_CONNECTED == event_id) { ESP_LOGI(TAG, "Connected to wifi in station mode\n"); that->update_state(wifi_state::connected); - mDNSDiscoveryService::setup(); } else if (WIFI_EVENT_STA_DISCONNECTED == event_id) { ESP_LOGI(TAG, "Station mode shutdown\n"); xSemaphoreTake(that->m_mutex, portMAX_DELAY); @@ -236,7 +234,6 @@ void WifiManager::wifi_event_handler(void *event_handler_arg, esp_event_base_t e } else if (WIFI_EVENT_AP_STADISCONNECTED == event_id) { ESP_LOGI(TAG, "User disconnected from AP\n"); } else if (WIFI_EVENT_AP_START == event_id) { - mDNSDiscoveryService::setup(); ESP_LOGI(TAG, "AP started\n"); } } diff --git a/components/rpc/mDNSDiscoveryService.cpp b/components/rpc/wireless/mDNSDiscoveryService.cpp similarity index 91% rename from components/rpc/mDNSDiscoveryService.cpp rename to components/rpc/wireless/mDNSDiscoveryService.cpp index b5f9598..5ca4435 100644 --- a/components/rpc/mDNSDiscoveryService.cpp +++ b/components/rpc/wireless/mDNSDiscoveryService.cpp @@ -9,10 +9,10 @@ #include "mdns.h" #include "ConfigManager.h" -#include "mDNSDiscoveryService.h" +#include "wireless/mDNSDiscoveryService.h" #include "constants/tcp.h" -void mDNSDiscoveryService::setup() { +mDNSDiscoveryService::mDNSDiscoveryService() { mdns_init(); const auto& config_manager = ConfigManager::get_instance(); @@ -32,6 +32,10 @@ void mDNSDiscoveryService::setup() { mdns_service_txt_set("_robotcontrol", "_tcp", service_txt_data, 3); } +mDNSDiscoveryService::~mDNSDiscoveryService() { + mdns_free(); +} + void mDNSDiscoveryService::set_connected_boards(const std::vector& boards) { std::stringstream ss; diff --git a/main/LoopManager.cpp b/main/LoopManager.cpp index a06f299..b0cbad9 100644 --- a/main/LoopManager.cpp +++ b/main/LoopManager.cpp @@ -18,7 +18,7 @@ #define METADATA_PERIOD_MS 1000 [[noreturn]] void LoopManager::control_loop() const { - const auto actuator = ActuatorFactory::create_actuator(m_config_manager.get_module_type()); + const auto actuator = ActuatorFactory::create_actuator(m_config_manager.get_module_type()); // todo: this needs to be moved higher up with one factory that returns shared ptr for both actuator and sensor. uint8_t buffer[512]; while (true) { @@ -27,6 +27,11 @@ } } + +[[noreturn]] void LoopManager::sensor_loop() const { + // todo: impl +} + [[noreturn]] void LoopManager::metadata_tx_loop(char * args) { const auto that = reinterpret_cast(args); const auto topology_message_builder = std::make_unique(); diff --git a/main/include/LoopManager.h b/main/include/LoopManager.h index 15c38bb..8a27c43 100644 --- a/main/include/LoopManager.h +++ b/main/include/LoopManager.h @@ -12,19 +12,15 @@ class LoopManager { public: 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); + m_messaging_interface(std::make_unique()) {} + [[noreturn]] void control_loop() const; // gets control commands + [[noreturn]] void sensor_loop() const; // sends sensor data commands continually + [[noreturn]] static void metadata_tx_loop(char * args); // sends metadata continually (low duty cycle) + [[noreturn]] static void metadata_rx_loop(char * args); // gets other commands from PC (ie. f/w updates, nvs updates) private: ConfigManager& m_config_manager; std::unique_ptr m_messaging_interface; - -private: - }; - - #endif //LOOPMANAGER_H diff --git a/main/include/control/ISensor.h b/main/include/control/ISensor.h new file mode 100644 index 0000000..a43af33 --- /dev/null +++ b/main/include/control/ISensor.h @@ -0,0 +1,14 @@ +// +// Created by Johnathon Slightham on 2025-10-16. +// + +#ifndef ISENSOR_H +#define ISENSOR_H + +class ISensor { +public: + virtual ~ISensor() {} + virtual void get_reading() = 0; // todo: return a flatbuffer object +}; + +#endif //ISENSOR_H diff --git a/main/include/control/SensorFactory.h b/main/include/control/SensorFactory.h new file mode 100644 index 0000000..cb8cedc --- /dev/null +++ b/main/include/control/SensorFactory.h @@ -0,0 +1,13 @@ +// +// Created by Johnathon Slightham on 2025-10-16. +// + +#ifndef SENSORFACTORY_H +#define SENSORFACTORY_H + +class SensorFactory { +public: + static std::unique_ptr create_sensor(ModuleType type); +}; + +#endif //SENSORFACTORY_H diff --git a/main/include/control/Servo1Actuator.h b/main/include/control/Servo1Actuator.h index 2c10820..471ff5b 100644 --- a/main/include/control/Servo1Actuator.h +++ b/main/include/control/Servo1Actuator.h @@ -9,6 +9,7 @@ #include #include "IActuator.h" +#include "ISensor.h" class Servo1Actuator final : public IActuator { public: diff --git a/main/main.cpp b/main/main.cpp index ad303c7..d85d9e9 100644 --- a/main/main.cpp +++ b/main/main.cpp @@ -5,8 +5,6 @@ #include "sdkconfig.h" #include "ConfigManager.h" #include "LoopManager.h" -#include "TCPServer.h" -#include "WifiManager.h" #include "esp_log.h" extern "C" [[noreturn]] void app_main(void) {