Integrate RPC with RMT

This commit is contained in:
2025-07-12 22:45:57 -04:00
parent aaf82d37d2
commit 0df0101265
22 changed files with 444 additions and 70 deletions

View File

@@ -1,3 +1,4 @@
idf_component_register(SRCS "WifiManager.cpp" "mDNSDiscoveryService.cpp" "TCPServer.cpp" "CommunicationRouter.cpp" "MessagingInterface.cpp"
PRIV_REQUIRES driver esp_event nvs_flash esp_netif esp_wifi espressif__mdns constants config flatbuffers
INCLUDE_DIRS "include")
PRIV_REQUIRES driver esp_event nvs_flash esp_netif esp_wifi espressif__mdns constants config flatbuffers dataLink rmt
REQUIRES ptrQueue
INCLUDE_DIRS "include")

View File

@@ -1,26 +1,115 @@
#include "CommunicationRouter.h"
#include <iostream>
#include "mDNSDiscoveryService.h"
#include "MPIMessageBuilder.h"
#include "WifiManager.h"
#include "freertos/FreeRTOS.h"
#include "freertos/queue.h"
#include "Tables.h"
#include "PtrQueue.h"
CommunicationRouter::~CommunicationRouter() {
vTaskDelete(m_router_thread);
vQueueDelete(m_tcp_rx_queue);
}
// 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<CommunicationRouter *>(args);
// todo: change to queue set
char buffer[512];
while (true) {
xQueueReceive(that->m_tcp_rx_queue, buffer, portMAX_DELAY);
that->m_rx_callback(buffer, 512);
std::cout << "callback" << std::endl;
// todo: strange to have this here, but i dont want 4 threads calling it or a thread just for it.
if (std::chrono::system_clock::now() - that->m_last_leader_updated > std::chrono::seconds(15)) {
that->update_leader();
}
const auto buffer = that->m_tcp_rx_queue->dequeue();
std::cout << "dequeued buffer" << std::endl;
that->m_rx_callback(reinterpret_cast<char *>(buffer->data()), buffer->size());
std::cout << "WiFi callback" << std::endl;
}
}
[[noreturn]] void CommunicationRouter::link_layer_thread(void *args) {
const auto* params = static_cast<link_layer_thread_params *>(args);
const auto that = params->router;
const auto channel = params->channel;
delete params;
char buffer[512];
size_t bytes_received = 0;
that->m_data_link_manager->start_receive_frames(channel);
while (true) {
// todo: very c style function calls
const auto err = that->m_data_link_manager->receive(reinterpret_cast<uint8_t *>(buffer), 512, &bytes_received, channel);
that->m_data_link_manager->start_receive_frames(channel);
if (ESP_OK != err) {
continue;
}
that->route(reinterpret_cast<uint8_t *>(buffer), bytes_received);
std::cout << "RMT callback" << std::endl;
}
}
int CommunicationRouter::send_msg(char* buffer, const size_t length) const {
return this->m_tcp_server->send_msg(buffer, length);
route(reinterpret_cast<uint8_t *>(buffer), length);
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;
this->m_data_link_manager->get_routing_table(table, &table_size);
// Leader election (just get the highest id in rip)
std::vector<int> connected_module_ids;
uint8_t max = m_module_id;
for (int i = 0; i < table_size; i++) {
const auto id = table[i].info.board_id;
connected_module_ids.emplace_back(id);
if (max > id) {
max = id;
}
}
// Leader has changed, we may need to change PC connection state
if (this->m_leader != max) {
if (max == m_module_id) {
m_pc_connection->connect();
} else if (this->m_leader == m_module_id) {
m_pc_connection->disconnect();
}
}
this->m_leader = max;
if (this->m_leader == m_module_id) {
mDNSDiscoveryService::set_connected_boards(connected_module_ids);
}
this->m_last_leader_updated = std::chrono::system_clock::now();
}
void CommunicationRouter::route(uint8_t* buffer, const size_t length) const {
const auto& mpi_message = Flatbuffers::MPIMessageBuilder::parse_mpi_message(buffer);
if (mpi_message->destination() == m_module_id) {
this->m_rx_callback(reinterpret_cast<char *>(buffer), 512);
} else if (mpi_message->destination() == PC_ADDR && this->m_leader == m_module_id) {
this->m_tcp_server->send_msg(reinterpret_cast<char *>(buffer), 512);
} else {
this->m_data_link_manager->send(mpi_message->destination(), buffer, length, FrameType::MOTOR_TYPE, 0);
}
}

View File

@@ -13,7 +13,9 @@
#include "TCPServer.h"
#include <memory>
#include <bits/shared_ptr_base.h>
#include <constants/app_comms.h>
#include "constants/tcp.h"
@@ -21,7 +23,7 @@
// - authenticate (don't just return true from the auth function)
// - tx from board
TCPServer::TCPServer(const int port, const QueueHandle_t rx_queue) {
TCPServer::TCPServer(const int port, const std::shared_ptr<PtrQueue<std::vector<uint8_t>>>& rx_queue) {
this->m_port = port;
this->m_mutex = xSemaphoreCreateMutex();
this->m_clients = std::unordered_set<int>();
@@ -143,9 +145,10 @@ TCPServer::~TCPServer() {
for (int sock : that->m_clients) {
if (FD_ISSET(sock, &readfds)) {
// Handle socket
char buffer[512];
int len = recv(sock, buffer, sizeof(buffer) - 1, 0); // temp: for the null terminator
auto buffer = std::make_unique<std::vector<uint8_t>>();
buffer->resize(MAX_RX_BUFFER_SIZE);
int len = recv(sock, buffer->data(), MAX_RX_BUFFER_SIZE, 0); // temp: for the null terminator
if (len < 0) {
printf("Error occurred during receiving: errno %d\n", errno);
to_remove.emplace_back(sock);
@@ -155,7 +158,8 @@ TCPServer::~TCPServer() {
to_remove.emplace_back(sock);
} else {
printf("TCP Server Received %d bytes\n", len);
xQueueSendToBack(that->m_rx_queue, buffer, 0);
buffer->resize(len);
that->m_rx_queue->enqueue(std::move(buffer));
}
}
}

View File

@@ -4,6 +4,7 @@
#include <esp_event.h>
#include <freertos/semphr.h>
#include <cstring>
#include <mDNSDiscoveryService.h>
#include "freertos/FreeRTOS.h"
#include "freertos/semphr.h"
@@ -188,6 +189,7 @@ void WifiManager::wifi_event_handler(void *event_handler_arg, esp_event_base_t e
} else if (WIFI_EVENT_STA_CONNECTED == event_id) {
printf("Connected to wifi in station mode\n");
that->update_state(wifi_state::connected);
mDNSDiscoveryService::setup();
} else if (WIFI_EVENT_STA_DISCONNECTED == event_id) {
printf("Station mode shutdown\n");
xSemaphoreTake(that->m_mutex, portMAX_DELAY);
@@ -204,5 +206,8 @@ void WifiManager::wifi_event_handler(void *event_handler_arg, esp_event_base_t e
printf("User connected to AP\n");
} else if (WIFI_EVENT_AP_STADISCONNECTED == event_id) {
printf("User disconnected from AP\n");
} else if (WIFI_EVENT_AP_START == event_id) {
mDNSDiscoveryService::setup();
printf("AP started\n");
}
}

View File

@@ -5,39 +5,74 @@
#ifndef COMMUNICATIONROUTER_H
#define COMMUNICATIONROUTER_H
#include <ConfigManager.h>
#include <functional>
#include <memory>
#include <chrono>
#include <WifiManager.h>
#include "freertos/FreeRTOS.h"
#include "freertos/queue.h"
#include "TCPServer.h"
#include "DataLinkManager.h"
#include "constants/tcp.h"
#include "constants/app_comms.h"
#include "constants/module.h"
#include"PtrQueue.h"
class CommunicationRouter {
class link_layer_thread_params {
public:
link_layer_thread_params(CommunicationRouter* router, const uint8_t channel) : router(router), channel(channel) {};
CommunicationRouter *router;
uint8_t channel;
};
public:
explicit CommunicationRouter(const std::function<void(char*, int)> &rx_callback)
: m_tcp_rx_queue(xQueueCreate(RX_QUEUE_SIZE, MAX_RX_BUFFER_SIZE)),
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_tcp_server(std::make_unique<TCPServer>(TCP_PORT, m_tcp_rx_queue)) {
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_pc_connection(std::move(pc_connection)),
m_module_id(ConfigManager::get_module_id()),
m_last_leader_updated(std::chrono::system_clock::now()){
update_leader();
xTaskCreate(router_thread, "communication_router", 2048, this, 3, &this->m_router_thread);
const auto num_channels = MODULE_TO_NUM_CHANNELS_MAP[ConfigManager::get_module_type()];
this->m_link_layer_threads.resize(num_channels);
for (uint8_t i = 0; i < num_channels; i++) {
auto *params = new link_layer_thread_params(this, i);
xTaskCreate(link_layer_thread, "communication_router_rmt", 3096, params, 3, &this->m_link_layer_threads[i]);
}
}
~CommunicationRouter();
[[noreturn]] static void router_thread(void *args);
[[noreturn]] static void link_layer_thread(void *args);
int send_msg(char* buffer, size_t length) const;
void update_leader();
void route(uint8_t *buffer, size_t length) const;
// todo: does this really need to be here (so i can access from thread)?
QueueHandle_t m_tcp_rx_queue;
std::shared_ptr<PtrQueue<std::vector<uint8_t>>> m_tcp_rx_queue;
std::function<void(char*, int)> m_rx_callback;
private:
TaskHandle_t m_router_thread;
std::vector<TaskHandle_t> m_link_layer_threads;
std::unique_ptr<TCPServer> m_tcp_server;
std::unique_ptr<DataLinkManager> m_data_link_manager;
std::unique_ptr<WifiManager> m_pc_connection; // todo: change to dependency inject
uint8_t m_leader = 0;
uint8_t m_module_id;
std::chrono::time_point<std::chrono::system_clock> m_last_leader_updated;
};
#endif //COMMUNICATIONROUTER_H

View File

@@ -7,14 +7,15 @@
#include <memory>
#include <unordered_map>
#include <constants/app_comms.h>
#include "CommunicationRouter.h"
class MessagingInterface {
public:
MessagingInterface()
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); })),
m_router(std::make_unique<CommunicationRouter>([this](char* buffer, const int size) { handleRecv(buffer, size); }, std::move(pc_connection))),
m_map_semaphore(xSemaphoreCreateMutex()) {};
~MessagingInterface();

View File

@@ -5,6 +5,7 @@
#ifndef TCPSERVER_H
#define TCPSERVER_H
#include <memory>
#include <vector>
#include <unordered_set>
@@ -12,9 +13,11 @@
#include "IRPCServer.h"
#include "PtrQueue.h"
class TCPServer : IRPCServer {
public:
TCPServer(int port, QueueHandle_t rx_queue);
TCPServer(int port, const std::shared_ptr<PtrQueue<std::vector<uint8_t>>>& rx_queue);
~TCPServer();
int send_msg(char* buffer, size_t length) const;
@@ -31,7 +34,7 @@ private:
TaskHandle_t m_task;
TaskHandle_t m_rx_task;
QueueHandle_t m_rx_queue;
std::shared_ptr<PtrQueue<std::vector<uint8_t>>> m_rx_queue;
SemaphoreHandle_t m_mutex;
std::unordered_set<int> m_clients;

View File

@@ -5,12 +5,15 @@
#ifndef DISCOVERYSERVICE_H
#define DISCOVERYSERVICE_H
#include <vector>
class mDNSDiscoveryService final {
public:
mDNSDiscoveryService() = delete;
~mDNSDiscoveryService() = delete;
static void setup();
static void set_connected_boards(const std::vector<int>& boards);
};
#endif //DISCOVERYSERVICE_H

View File

@@ -12,6 +12,7 @@
#include <string>
#include <format>
#include <sstream>
// todo: clean this up (strange to be a constructor) also need to add more details, need to add to routing table
void mDNSDiscoveryService::setup() {
@@ -25,8 +26,26 @@ void mDNSDiscoveryService::setup() {
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()},
{"connected_modules","2,3,4,5,6,7,8,9"},
{"connected_modules",""},
};
mdns_service_txt_set("_robotcontrol", "_tcp", service_txt_data, 3);
}
void mDNSDiscoveryService::set_connected_boards(const std::vector<int>& boards) {
std::stringstream ss;
for (int i = 0; i < boards.size(); i++) {
ss << boards[i];
if (i < boards.size() - 1) {
ss << ',';
}
}
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()},
{"connected_modules",ss.str().c_str()},
};
mdns_service_txt_set("_robotcontrol", "_tcp", service_txt_data, 3);
}