Fix RIP bugs, add in UART

This commit is contained in:
Johnathon Slightham
2026-03-31 14:26:37 -04:00
committed by Johnathon Slightham
parent d4602012f1
commit 548e8db484
26 changed files with 704 additions and 434 deletions

View File

@@ -289,6 +289,15 @@ esp_err_t DataLinkManager::receive_rmt(uint8_t channel){
return ESP_ERR_INVALID_RESPONSE;
}
// Preamble sanity check — if byte 0 is not START_OF_FRAME this buffer
// contains a merged frame, a partial capture, or line noise. Discard
// immediately rather than letting it propagate to the CRC checker.
if (data[0] != START_OF_FRAME){
ESP_LOGW(DEBUG_LINK_TAG, "Discarding frame on ch %d: bad preamble 0x%02X (expected 0x%02X)",
channel, data[0], START_OF_FRAME);
return ESP_ERR_INVALID_RESPONSE;
}
// ---- LUT fast-path for control frames ----
// Raw wire layout for control frames:
// [0] preamble
@@ -301,7 +310,8 @@ esp_err_t DataLinkManager::receive_rmt(uint8_t channel){
// [12..] actual payload
// [-2..-1] CRC-16
bool is_control = IS_CONTROL_FRAME(data[5]);
if (is_control && recv_len >= (size_t)(CONTROL_FRAME_OVERHEAD + CONTROL_FRAME_HASH_SIZE)){
bool is_rip = (GET_TYPE(data[5]) == static_cast<uint8_t>(FrameType::RIP_TABLE_CONTROL));
if (is_control && !is_rip && recv_len >= (size_t)(CONTROL_FRAME_OVERHEAD + CONTROL_FRAME_HASH_SIZE)){
uint32_t peeked_hash = ((uint32_t)data[8] ) |
((uint32_t)data[9] << 8) |
((uint32_t)data[10] << 16) |
@@ -323,20 +333,36 @@ esp_err_t DataLinkManager::receive_rmt(uint8_t channel){
// RIP control frames are handled internally replay them too
if (static_cast<FrameType>(GET_TYPE(cached_header.type_flag)) == FrameType::RIP_TABLE_CONTROL){
// Re-run the RIP update using the cached message
for (size_t i = 0; i < cached_message.size() - 1; i += 2){
uint8_t board_id = cached_message[i];
uint8_t hops = cached_message[i + 1];
RIPRow* entry = nullptr;
// Re-run the RIP update using the cached message (3-byte stride: board_id, hops, sum_of_hops)
for (size_t i = 0; i + 2 < cached_message.size(); i += RIP_WIRE_STRIDE){
uint8_t board_id = cached_message[i];
uint8_t hops = cached_message[i + 1];
uint8_t sum_of_hops = cached_message[i + 2];
// Skip routes about ourselves
if (board_id == this_board_id){
continue;
}
RIPRow* entry = nullptr;
if (rip_find_entry(board_id, &entry, true) != ESP_OK || entry == nullptr){
continue;
}
if (entry->valid == RIP_NEW_ROW){
rip_add_entry(board_id, hops + 1, channel, &entry);
rip_add_entry(board_id, hops + 1, sum_of_hops, channel, &entry);
} else {
rip_update_entry(hops + 1, channel, &entry);
rip_update_entry(hops + 1, sum_of_hops, channel, &entry);
}
}
// If this was a discovery request, send our routing table back
if (GET_FLAG(cached_header.type_flag) == FLAG_DISCOVERY){
esp_err_t disc_res = send_rip_frame(false, cached_header.sender_id);
if (disc_res != ESP_OK){
ESP_LOGE(DEBUG_LINK_TAG, "Failed to send back rip table to board %d (LUT path)", cached_header.sender_id);
}
}
return ESP_OK;
}
@@ -407,9 +433,8 @@ esp_err_t DataLinkManager::receive_rmt(uint8_t channel){
return res;
}
// Control frame fully validated store in LUT for future replays
// Extract the hash that was embedded at data[8..11] (already validated in get_data_from_frame)
{
// Control frame fully validated store in LUT for future replays (skip RIP frames: their payload is dynamic)
if (!is_rip){
uint32_t validated_hash = ((uint32_t)data[8] ) |
((uint32_t)data[9] << 8) |
((uint32_t)data[10] << 16) |
@@ -424,10 +449,17 @@ esp_err_t DataLinkManager::receive_rmt(uint8_t channel){
if (static_cast<FrameType>(GET_TYPE(header.type_flag)) == FrameType::RIP_TABLE_CONTROL){
ESP_LOGI(DEBUG_LINK_TAG, "Got a RIP frame from channel %d", channel);
for (size_t i = 0; i < message_size-1; i+=2){
uint8_t board_id = message->data()[i];
uint8_t hops = message->data()[i+1];
ESP_LOGI(DEBUG_LINK_TAG, "Received: board_id %d and number of hops %d on channel %d", board_id, hops, channel);
for (size_t i = 0; i + 2 < message_size; i += RIP_WIRE_STRIDE){
uint8_t board_id = message->data()[i];
uint8_t hops = message->data()[i + 1];
uint8_t sum_of_hops = message->data()[i + 2];
// Skip routes about ourselves - we already know we're 0 hops away from ourselves
if (board_id == this_board_id){
continue;
}
ESP_LOGI(DEBUG_LINK_TAG, "Received: board_id %d hops %d sum_of_hops %d on channel %d", board_id, hops, sum_of_hops, channel);
RIPRow* entry = nullptr;
@@ -443,15 +475,14 @@ esp_err_t DataLinkManager::receive_rmt(uint8_t channel){
if (entry->valid == RIP_NEW_ROW){
//adding a new entry
rip_add_entry(board_id, hops + 1, channel, &entry);
rip_add_entry(board_id, hops + 1, sum_of_hops, channel, &entry);
} else {
//updating an entry
rip_update_entry(hops + 1, channel, &entry);
rip_update_entry(hops + 1, sum_of_hops, channel, &entry);
}
if (GET_FLAG(header.type_flag) == FLAG_DISCOVERY){
//discovery -> send routing table
// ESP_LOGI(DEBUG_LINK_TAG, "got discovery reply");
RIPRow_public row_queue = {
.info = entry->info,
.channel = entry->channel
@@ -461,7 +492,19 @@ esp_err_t DataLinkManager::receive_rmt(uint8_t channel){
}
}
if (message_size == RIP_DISCOVERY_MESSAGE_SIZE){
// Print the finalized RIP table after processing all received rows
ESP_LOGI(DEBUG_LINK_TAG, "--- RIP Table (after update from channel %d) ---", channel);
for (size_t i = 0; i < RIP_MAX_ROUTES; i++){
RIPRow* row = nullptr;
if (rip_get_row(&row, i) == ESP_OK && row != nullptr){
ESP_LOGI(DEBUG_LINK_TAG, " [%d] board_id=%d hops=%d sum_of_hops=%d channel=%d ttl=%d",
i, row->info.board_id, row->info.hops, row->info.sum_of_hops, row->channel, row->ttl);
}
}
ESP_LOGI(DEBUG_LINK_TAG, "---------------------------------------------------");
if (GET_FLAG(header.type_flag) == FLAG_DISCOVERY){
res = send_rip_frame(false, header.sender_id);
if (res != ESP_OK){
ESP_LOGE(DEBUG_LINK_TAG, "Failed to send back rip table to board %d", header.sender_id);