Reduce DataLink latency

This commit is contained in:
Johnathon Slightham
2026-01-27 22:16:24 -05:00
committed by Johnathon Slightham
parent b9c6a99260
commit 2e9004ee34
25 changed files with 801 additions and 757 deletions

View File

@@ -4,7 +4,7 @@
/**
* @brief Initializes the RIP table
*
*
*/
void DataLinkManager::init_rip(){
for (size_t i = 0; i < RIP_MAX_ROUTES; i++){
@@ -54,11 +54,11 @@ esp_err_t DataLinkManager::rip_add_entry(uint8_t board_id, uint8_t hops, uint8_t
(*entry)->ttl = RIP_TTL_START;
(*entry)->valid = 1;
// ESP_LOGI(DEBUG_LINK_TAG, "board_id %d now has hops %d from channel %d", (*entry)->info.board_id, (*entry)->info.hops, channel);
xSemaphoreGive((*entry)->row_sem);
if (uxQueueMessagesWaiting(manual_broadcasts) == 0){
bool dummy = true;
xQueueSend(manual_broadcasts, &dummy, 0); //new row - send broadcast
@@ -108,9 +108,9 @@ esp_err_t DataLinkManager::rip_update_entry(uint8_t new_hop, uint8_t channel, RI
(*entry)->channel = channel;
// ESP_LOGI(DEBUG_LINK_TAG, "updated board_id %d now has hops %d from channel %d", (*entry)->info.board_id, (*entry)->info.hops, channel);
}
(*entry)->ttl = RIP_TTL_START;
(*entry)->valid = 1;
(*entry)->valid = 1;
// ESP_LOGI(DEBUG_LINK_TAG, "refreshed board_id %d ttl", (*entry)->info.board_id);
@@ -128,10 +128,10 @@ esp_err_t DataLinkManager::rip_update_entry(uint8_t new_hop, uint8_t channel, RI
/**
* @brief Finds the board_id in the table if it exists and stores that row in `entry`
* TODO: use an unordered map instead of an array?
*
* @param board_id
* @param entry
* @return esp_err_t
*
* @param board_id
* @param entry
* @return esp_err_t
*/
esp_err_t DataLinkManager::rip_find_entry(uint8_t board_id, RIPRow** entry, bool reserve_row = false){
RIPRow* free_slot = nullptr;
@@ -144,7 +144,7 @@ esp_err_t DataLinkManager::rip_find_entry(uint8_t board_id, RIPRow** entry, bool
xSemaphoreGive(rip_table[i].row_sem);
// ESP_LOGI(DEBUG_LINK_TAG, "Found %d in table at row %d", board_id, i);
return ESP_OK;
}
}
if (rip_table[i].valid == RIP_INVALID_ROW && free_slot == nullptr){
free_slot = &rip_table[i];
}
@@ -176,10 +176,10 @@ esp_err_t DataLinkManager::rip_find_entry(uint8_t board_id, RIPRow** entry, bool
/**
* @brief Returns the associated RIP Table row by row number. Information returned is read only.
*
* @param entry
* @param row_num
* @return esp_err_t
*
* @param entry
* @param row_num
* @return esp_err_t
*/
esp_err_t DataLinkManager::rip_get_row(RIPRow** entry, uint8_t row_num){
if (entry == nullptr){
@@ -216,18 +216,16 @@ esp_err_t DataLinkManager::rip_get_row(RIPRow** entry, uint8_t row_num){
/**
* @brief Sends RIP frame
*
*
* @param broadcast True - broadcasts (sends rip table to all available channels); False - sends rip table via routing based on `dest_id`
* @param dest_id Destination board (requesting board) to send the rip table to (ignored if `broadcast is true`)
* @return esp_err_t
* @return esp_err_t
*/
esp_err_t DataLinkManager::send_rip_frame(bool broadcast, uint8_t dest_id){
//use the control frame for the demo (as the number of rows increase, we will need to use the generic frame)
//data will be [board_id (1), hops (1), board_id (2), hops (2), ...]
uint8_t rip_message[RIP_MAX_ROUTES*2] = {};
uint16_t message_idx = 0;
esp_err_t res;
RIPRow* entry = nullptr;
@@ -235,43 +233,40 @@ esp_err_t DataLinkManager::send_rip_frame(bool broadcast, uint8_t dest_id){
if(broadcast){
for (size_t channel = 0; channel < num_channels; channel++){
auto rip_message = std::make_unique<std::vector<uint8_t>>();
rip_message->resize(RIP_MAX_ROUTES * 2);
for (size_t i = 0; i < RIP_MAX_ROUTES; i++){
res = rip_get_row(&entry, i);
if (res != ESP_OK){
continue;
}
if (entry == nullptr){
continue;
}
// ESP_LOGI(DEBUG_LINK_TAG, "Found entry for board %d with hops %d", entry->info.board_id, entry->info.hops);
if (entry->channel == channel){
//poisoned reverse
rip_message[message_idx++] = entry->info.board_id;
rip_message[message_idx++] = RIP_MAX_HOPS + 1;
rip_message->at(message_idx++) = entry->info.board_id;
rip_message->at(message_idx++) = RIP_MAX_HOPS + 1;
} else {
rip_message[message_idx++] = entry->info.board_id;
rip_message[message_idx++] = entry->info.hops;
rip_message->at(message_idx++) = entry->info.board_id;
rip_message->at(message_idx++) = entry->info.hops;
}
}
uint8_t* send_data = (uint8_t*)pvPortMalloc(message_idx);
if (send_data == nullptr){
ESP_LOGE(DEBUG_LINK_TAG, "Failed to malloc when trying to send rip frame broadcast on channel %d", channel);
continue;
}
memset(send_data, 0, message_idx);
memcpy(send_data, rip_message, message_idx);
rip_message->resize(message_idx);
res = get_inc_sequence_num(BROADCAST_ADDR, &seq_num);
if (res != ESP_OK){
ESP_LOGE(DEBUG_LINK_TAG, "Failed atomic get increment sequence number map");
return res;
}
SchedulerMetadata metadata = {
.header = {
.preamble = START_OF_FRAME,
@@ -284,8 +279,7 @@ esp_err_t DataLinkManager::send_rip_frame(bool broadcast, uint8_t dest_id){
},
.generic_frame_data_offset = 0,
.enqueue_time_ns = 0,
.data = send_data,
.len = message_idx,
.data = std::move(rip_message),
.last_ack = 0,
.curr_fragment = 0,
.timeout = 0,
@@ -296,9 +290,11 @@ esp_err_t DataLinkManager::send_rip_frame(bool broadcast, uint8_t dest_id){
ESP_LOGE(DEBUG_LINK_TAG, "Failed to schedule rip frame from send_rip_frame for channel %d", channel);
}
message_idx = 0;
memset(rip_message, 0, sizeof(rip_message));
}
} else {
auto rip_message = std::make_unique<std::vector<uint8_t>>();
rip_message->resize(RIP_MAX_ROUTES * 2);
for (size_t i = 0; i < RIP_MAX_ROUTES; i++){
res = rip_get_row(&entry, i);
@@ -309,11 +305,11 @@ esp_err_t DataLinkManager::send_rip_frame(bool broadcast, uint8_t dest_id){
if (entry == nullptr){
continue;
}
rip_message[message_idx++] = entry->info.board_id;
rip_message[message_idx++] = entry->info.hops;
rip_message->data()[message_idx++] = entry->info.board_id;
rip_message->data()[message_idx++] = entry->info.hops;
}
ESP_LOGI(DEBUG_LINK_TAG, "replying to discovery request to board %d", dest_id);
res = send(dest_id, rip_message, message_idx, FrameType::RIP_TABLE_CONTROL, FLAG_DISCOVERY);
res = send(dest_id, std::move(rip_message), FrameType::RIP_TABLE_CONTROL, FLAG_DISCOVERY);
if (res != ESP_OK){
ESP_LOGE(DEBUG_LINK_TAG, "Failed to send rip frame from send_rip_frame");
}
@@ -324,10 +320,10 @@ esp_err_t DataLinkManager::send_rip_frame(bool broadcast, uint8_t dest_id){
/**
* @brief Determines which channel to route the frame to, depending on the dest (board) id
*
* @param dest_id
* @param channel_to_send
* @return esp_err_t
*
* @param dest_id
* @param channel_to_send
* @return esp_err_t
*/
esp_err_t DataLinkManager::route_frame(uint8_t dest_id, uint8_t* channel_to_send){
RIPRow* entry = nullptr;
@@ -350,12 +346,12 @@ esp_err_t DataLinkManager::route_frame(uint8_t dest_id, uint8_t* channel_to_send
/**
* @brief Fetches the current routing table at the perspective of the host board
*
*
* @details The routing table is based off of RIP
*
* @param table
* @param table_size
* @return esp_err_t
*
* @param table
* @param table_size
* @return esp_err_t
*/
esp_err_t DataLinkManager::get_routing_table(RIPRow_public* table, size_t* table_size){
if (table == nullptr){
@@ -383,7 +379,7 @@ esp_err_t DataLinkManager::get_routing_table(RIPRow_public* table, size_t* table
table[i].info = rip_table[i].info;
table[i].channel = rip_table[i].channel;
curr_size++;
}
}
xSemaphoreGive(rip_table[i].row_sem);
}
@@ -446,7 +442,7 @@ esp_err_t DataLinkManager::get_routing_table(RIPRow_public* table, size_t* table
link_layer_obj->rip_table[i].ttl_flush = RIP_FLUSH_COUNT;
broadcast = true;
}
}
}
xSemaphoreGive(link_layer_obj->rip_table[i].row_sem);
}
@@ -472,4 +468,4 @@ void DataLinkManager::start_rip_tasks(){
xTaskCreate(DataLinkManager::rip_broadcast_timer_function, "RIPBroadcast", 4096, static_cast<void*>(this), 5, &rip_broadcast_task);
ESP_LOGI(DEBUG_LINK_TAG, "Starting RIP TTL task");
xTaskCreate(DataLinkManager::rip_ttl_decrement_task, "RIPTTL", 4096, static_cast<void*>(this), 5, &rip_ttl_task);
}
}