mirror of
https://github.com/BotChain-Robots/firmware.git
synced 2026-07-08 17:47:21 +02:00
Integrate RPC with RMT
This commit is contained in:
@@ -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
|
||||
|
||||
@@ -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();
|
||||
|
||||
@@ -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;
|
||||
|
||||
@@ -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
|
||||
|
||||
Reference in New Issue
Block a user