Cleanup interfaces

This commit is contained in:
2025-10-08 17:35:32 -04:00
parent 9627b76183
commit e1435b53b5
9 changed files with 27 additions and 93 deletions

View File

@@ -1,12 +1,11 @@
#include "DataLinkManager.h"
#include "RMTManager.h"
#include "esp_log.h"
#include "nvs_flash.h"
/**
* @brief Construct a new Data Link Manager:: Data Link Manager object
*
* @param board_id Board ID of the current board. Will be written to the NVM under key "board" if not already written.
* @param board_id Board ID of the current board.
*/
DataLinkManager::DataLinkManager(uint8_t board_id, uint8_t num_channels = MAX_CHANNELS){
//init table for this board and set up link layer priority queue
@@ -16,18 +15,7 @@ DataLinkManager::DataLinkManager(uint8_t board_id, uint8_t num_channels = MAX_CH
return;
}
// if (get_board_id(this_board_id) != ESP_OK){
//failed to read from NVM for board id under key "board". Will write a new entry
this_board_id = board_id;
set_board_id(this_board_id);
// }
// if (this_board_id != board_id){
// //NVM board id is different from `board_id` -> update entry to the new board id
// this_board_id = board_id;
// set_board_id(this_board_id);
// }
this->this_board_id = board_id;
this->num_channels = num_channels;
init_rip();
@@ -37,63 +25,6 @@ DataLinkManager::~DataLinkManager(){
phys_comms.reset(); //not strictly necessary to do this explicitly
}
esp_err_t DataLinkManager::set_board_id(uint8_t board_id){
if (board_id == BROADCAST_ADDR || board_id == PC_ADDR){
ESP_LOGE(DEBUG_LINK_TAG, "Invalid board id");
return ESP_FAIL;
}
nvs_handle_t handle;
esp_err_t res = nvs_open("board", NVS_READWRITE, &handle);
if (res != ESP_OK){
ESP_LOGE(DEBUG_LINK_TAG, "Failed to open NVS Handle");
return res;
}
res = nvs_set_u8(handle, "id", board_id);
if (res != ESP_OK){
ESP_LOGE(DEBUG_LINK_TAG, "Failed to write ID %d to NVM", board_id);
nvs_close(handle);
return res;
}
res = nvs_commit(handle);
if (res != ESP_OK){
ESP_LOGE(DEBUG_LINK_TAG, "Failed to commit write");
nvs_close(handle);
return res;
}
this_board_id = board_id;
printf("Successfully wrote %d to NVM\n", board_id);
nvs_close(handle);
return ESP_OK;
}
esp_err_t DataLinkManager::get_board_id(uint8_t& board_id){
nvs_handle_t handle;
esp_err_t res = nvs_open("board", NVS_READWRITE, &handle);
if (res != ESP_OK){
ESP_LOGE(DEBUG_LINK_TAG, "Failed to open NVS Handle");
return res;
}
res = nvs_get_u8(handle, "id", &board_id);
if (res != ESP_OK){
ESP_LOGE(DEBUG_LINK_TAG, "Failed to get ID from NVM. Please make sure NVM is already assigned a board id!");
nvs_close(handle);
return res;
}
printf("Successfully got board id %d from NVM\n", board_id);
nvs_close(handle);
return ESP_OK;
}
/**
* @brief Sends a frame to another board (node to node communication) via RMT (physical layer)
*
@@ -274,7 +205,7 @@ esp_err_t DataLinkManager::receive(uint8_t* data, size_t data_len, size_t* recv_
esp_err_t res = phys_comms->receive(data, data_len, recv_len, curr_channel);
if (res != ESP_OK){
ESP_LOGE(DEBUG_LINK_TAG, "RMT Failed to receive");
ESP_LOGW(DEBUG_LINK_TAG, "RMT Failed to receive");
return ESP_ERR_TIMEOUT;
}

View File

@@ -30,8 +30,4 @@ namespace Flatbuffers {
return {builder_.GetBufferPointer(), builder_.GetSize()};
}
const Messaging::TopologyMessage* TopologyMessageBuilder::parse_topology_message(const uint8_t* buffer) {
return flatbuffers::GetRoot<Messaging::TopologyMessage>(buffer);
}
}

View File

@@ -23,8 +23,6 @@ namespace Flatbuffers {
const std::vector<uint8_t>& channel_to_module,
const std::vector<int8_t>& orientation_to_module);
static const Messaging::TopologyMessage* parse_topology_message(const uint8_t* buffer);
private:
flatbuffers::FlatBufferBuilder builder_;
};

View File

@@ -572,7 +572,7 @@ esp_err_t RMTManager::receive(uint8_t* recv_buf, size_t size, size_t* output_siz
rmt_rx_done_event_data_t rx_data;
if (xQueueReceive(channels[channel_num].rx_queue, &rx_data, pdMS_TO_TICKS(15000)) != pdTRUE){ //this will wait until a message has arrived or not
// printf("Timeout occurred while waiting for RX event\n");
ESP_LOGE(DEBUG_TAG, "Timeout occurred while waiting for RX event - didn't receive a message in time");
ESP_LOGW(DEBUG_TAG, "Timeout occurred while waiting for RX event - didn't receive a message in time");
return ESP_FAIL;
}

View File

@@ -100,6 +100,13 @@ void CommunicationRouter::update_leader() {
}
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.
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) {

View File

@@ -124,6 +124,8 @@ TCPServer::~TCPServer() {
const auto that = static_cast<TCPServer *>(args);
while (true) {
vTaskDelay(0); // Avoid starving other threads
fd_set readfds;
FD_ZERO(&readfds);
int max_fd = -1;
@@ -135,13 +137,18 @@ TCPServer::~TCPServer() {
}
xSemaphoreGive(that->m_mutex);
timeval timeout = {1, 0}; // 1 second timeout
// todo: Select seems to be timing out the watchdog, even though we have a 50ms timeout.
// Potentially select is not respecting our timeout?
timeval timeout = {0, 50000}; // 50 ms timeout
int ret = select(max_fd + 1, &readfds, nullptr, nullptr, &timeout);
vTaskDelay(0); // Avoid starving other threads
if (ret > 0) {
xSemaphoreTake(that->m_mutex, portMAX_DELAY);
std::vector<int> to_remove;
for (int sock : that->m_clients) {
vTaskDelay(0); // Avoid starving other threads
if (FD_ISSET(sock, &readfds)) {
// Handle socket
auto buffer = std::make_unique<std::vector<uint8_t>>();
@@ -159,7 +166,7 @@ TCPServer::~TCPServer() {
ESP_LOGE(TAG, "Error occurred during receiving: errno %d\n", errno);
to_remove.emplace_back(sock);
} else if (0 == len) {
ESP_LOGW(TAG, "Connection closed\n");
ESP_LOGI(TAG, "TCP Connection closed\n");
close(sock);
to_remove.emplace_back(sock);
} else {
@@ -210,8 +217,6 @@ bool TCPServer::authenticate_client(int sock) {
}
int TCPServer::send_msg(char *buffer, uint32_t length) const {
// todo: should we assign a unique rank to each pc?
if (!is_network_connected()) {
return -1;
}

View File

@@ -6,7 +6,10 @@
#define IDISCOVERYSERVICE_H
class IDiscoveryService {
public:
virtual ~IDiscoveryService() = default;
virtual static void setup() = 0;
virtual static void set_connected_boards(const std::vector<int>& boards) = 0;
}
#endif //IDISCOVERYSERVICE_H

View File

@@ -1,8 +0,0 @@
//
// Created by Johnathon Slightham on 2025-05-25.
//
#ifndef IMESSAGINGINTERFACE_H
#define IMESSAGINGINTERFACE_H
#endif //IMESSAGINGINTERFACE_H

View File

@@ -6,7 +6,9 @@
#define IRPCSERVER_H
class IRPCServer {
public:
virtual ~IRPCServer() = default;
virtual int send_msg(char* buffer, uint32_t length) const = 0;
};
#endif //IRPCSERVER_H