mirror of
https://github.com/BotChain-Robots/firmware.git
synced 2026-07-08 17:47:21 +02:00
Config manager fixes
This commit is contained in:
@@ -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)
|
||||
|
||||
@@ -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<uint8_t>(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<uint8_t>(buffer, buffer + size));
|
||||
|
||||
m_router->send_msg(static_cast<char *>(mpi_buffer), mpi_size);
|
||||
return 0;
|
||||
|
||||
@@ -10,7 +10,8 @@
|
||||
#include <bits/ostream.tcc>
|
||||
|
||||
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<gpio_num_t>(CHANNEL_TO_0_DEG_MAP[i]));
|
||||
setup_gpio(static_cast<gpio_num_t>(CHANNEL_TO_90_DEG_MAP[i]));
|
||||
setup_gpio(static_cast<gpio_num_t>(CHANNEL_TO_180_DEG_MAP[i]));
|
||||
|
||||
@@ -1,30 +1,9 @@
|
||||
#include "WifiManager.h"
|
||||
|
||||
#include <ConfigManager.h>
|
||||
#include <esp_netif.h>
|
||||
#include <esp_event.h>
|
||||
#include <freertos/semphr.h>
|
||||
#include <cstring>
|
||||
#include <mDNSDiscoveryService.h>
|
||||
|
||||
#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<TaskFunction_t>(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<char *>(wifi_configuration.sta.ssid), ssid.c_str(), 32);
|
||||
std::strncpy(reinterpret_cast<char *>(wifi_configuration.sta.password), pass.c_str(), 64);
|
||||
wifi_configuration.sta.ssid[31] = '\0';
|
||||
|
||||
@@ -36,17 +36,18 @@ public:
|
||||
CommunicationRouter(const std::function<void(char*, int)> &rx_callback, std::unique_ptr<WifiManager>&& pc_connection)
|
||||
: m_tcp_rx_queue(std::make_shared<PtrQueue<std::vector<uint8_t>>>(10)),
|
||||
m_rx_callback(rx_callback),
|
||||
m_config_manager(ConfigManager::get_instance()),
|
||||
m_tcp_server(std::make_unique<TCPServer>(TCP_PORT, m_tcp_rx_queue)),
|
||||
m_data_link_manager(std::make_unique<DataLinkManager>(ConfigManager::get_module_id(), MODULE_TO_NUM_CHANNELS_MAP[ConfigManager::get_module_type()])),
|
||||
m_data_link_manager(std::make_unique<DataLinkManager>(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<void(char*, int)> m_rx_callback;
|
||||
private:
|
||||
TaskHandle_t m_router_thread = nullptr;
|
||||
ConfigManager &m_config_manager;
|
||||
std::vector<TaskHandle_t> m_link_layer_threads;
|
||||
std::unique_ptr<TCPServer> m_tcp_server; // todo: dependency injection
|
||||
std::unique_ptr<DataLinkManager> m_data_link_manager;
|
||||
|
||||
@@ -14,8 +14,9 @@
|
||||
class MessagingInterface {
|
||||
public:
|
||||
explicit MessagingInterface(std::unique_ptr<WifiManager>&& pc_connection)
|
||||
: m_mpi_rx_queue(xQueueCreate(MAX_RX_BUFFER_SIZE, RX_QUEUE_SIZE)),
|
||||
m_router(std::make_unique<CommunicationRouter>([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<CommunicationRouter>([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<CommunicationRouter> m_router;
|
||||
SemaphoreHandle_t m_map_semaphore;
|
||||
|
||||
@@ -2,17 +2,27 @@
|
||||
#define NETWORKMANAGER_H
|
||||
|
||||
#include <esp_netif_types.h>
|
||||
#include <esp_event.h>
|
||||
#include <esp_netif.h>
|
||||
#include <esp_wifi.h>
|
||||
#include <freertos/FreeRTOS.h>
|
||||
#include <freertos/semphr.h>
|
||||
|
||||
#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<TaskFunction_t>(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;
|
||||
|
||||
};
|
||||
|
||||
|
||||
@@ -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<int>& 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<int>& 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);
|
||||
|
||||
Reference in New Issue
Block a user