update link layer manager + rip + rmt

This commit is contained in:
Justin Chow
2025-07-10 00:48:37 -04:00
committed by Johnathon Slightham
parent 3691ca4697
commit 732f141e45
9 changed files with 342 additions and 271 deletions

View File

@@ -8,9 +8,9 @@
*
* @param board_id Board ID of the current board. Will be written to the NVM under key "board" if not already written.
*/
DataLinkManager::DataLinkManager(uint8_t board_id){
DataLinkManager::DataLinkManager(uint8_t board_id, uint8_t num_channels = MAX_CHANNELS){
//init table for this board and set up link layer priority queue
phys_comms = std::make_unique<RMTManager>();
phys_comms = std::make_unique<RMTManager>(num_channels);
if (phys_comms == nullptr){
ESP_LOGE(DEBUG_LINK_TAG, "RMT object was not created. Link layer communications will not function.");
return;
@@ -22,11 +22,13 @@ DataLinkManager::DataLinkManager(uint8_t 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);
}
// 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->num_channels = num_channels;
init_rip();
}
@@ -41,7 +43,6 @@ esp_err_t DataLinkManager::set_board_id(uint8_t board_id){
return ESP_FAIL;
}
nvs_handle_t handle;
esp_err_t res = nvs_open("board", NVS_READWRITE, &handle);
if (res != ESP_OK){
@@ -118,10 +119,12 @@ esp_err_t DataLinkManager::send(uint8_t dest_board, uint8_t* data, uint16_t data
return ESP_FAIL;
}
if (curr_channel >= MAX_CHANNELS){
if (curr_channel >= num_channels){
return ESP_FAIL;
}
esp_err_t res;
if (IS_CONTROL_FRAME(static_cast<uint8_t>(type))){
//control frame
if (data_len > MAX_CONTROL_DATA_LEN){
@@ -157,6 +160,7 @@ esp_err_t DataLinkManager::send(uint8_t dest_board, uint8_t* data, uint16_t data
send_data[offset++] = (new_frame.seq_num >> 8) & 0xFF;
send_data[offset++] = new_frame.type_flag;
send_data[offset++] = new_frame.data_len;
send_data[offset++] = (new_frame.data_len >> 8) & 0xFF;
memcpy(&send_data[offset], data, new_frame.data_len);
@@ -178,7 +182,18 @@ esp_err_t DataLinkManager::send(uint8_t dest_board, uint8_t* data, uint16_t data
// printf("sending message:\n");
// print_buffer_binary(send_data, frame_size);
phys_comms->send(send_data, offset, &config, curr_channel);
uint8_t channel_to_route = MAX_CHANNELS;
if (new_frame.receiver_id == BROADCAST_ADDR){
channel_to_route = curr_channel;
} else {
res = route_frame(new_frame.receiver_id, &channel_to_route);
if (res != ESP_OK){
ESP_LOGE(DEBUG_LINK_TAG, "Failed to find entry for %d", new_frame.receiver_id);
return ESP_FAIL;
}
}
phys_comms->send(send_data, offset, &config, channel_to_route);
//can wait for the rmt to finish
// esp_err_t res = phys_comms->wait_until_send_complete(curr_channel); //this cannot be here in deployment but until the RMT manager can hold this copy of data this will have to be here
@@ -219,7 +234,7 @@ void DataLinkManager::print_buffer_binary(const uint8_t* buffer, size_t length)
* @return esp_err_t
*/
esp_err_t DataLinkManager::start_receive_frames(uint8_t curr_channel){
if (curr_channel >= MAX_CHANNELS){
if (curr_channel >= num_channels){
return ESP_FAIL;
}
return phys_comms->start_receiving(curr_channel);
@@ -231,11 +246,13 @@ esp_err_t DataLinkManager::receive(uint8_t* data, size_t data_len, size_t* recv_
return ESP_FAIL;
}
if (curr_channel >= MAX_CHANNELS){
if (curr_channel >= num_channels){
ESP_LOGE(DEBUG_LINK_TAG, "Invalid channel");
return ESP_FAIL;
}
if (data_len < MAX_CONTROL_DATA_LEN + CONTROL_FRAME_OVERHEAD){
ESP_LOGE(DEBUG_LINK_TAG, "Receive data buffer len is too small");
return ESP_FAIL;
}
@@ -253,35 +270,46 @@ esp_err_t DataLinkManager::receive(uint8_t* data, size_t data_len, size_t* recv_
return ESP_FAIL;
}
uint8_t* message = (uint8_t*)pvPortMalloc(CONTROL_FRAME_OVERHEAD + MAX_CONTROL_DATA_LEN);
if (message == nullptr){
ESP_LOGE(DEBUG_LINK_TAG, "Failed to malloc for receive");
return ESP_FAIL;
}
memset(message, 0, sizeof(message));
size_t message_size = 0;
frame_header header;
res = get_data_from_frame(data, *recv_len, message, &message_size, &header);
if (res != ESP_OK){
return ESP_FAIL;
}
ESP_LOGI(DEBUG_LINK_TAG, "Received frame destined for board %d", header.receiver_id);
//check for a rip frame
if (static_cast<FrameType>((data[5])) == FrameType::RIP_TABLE_CONTROL){
if (static_cast<FrameType>(header.type_flag) == FrameType::RIP_TABLE_CONTROL){
printf("Got a RIP frame\n");
uint8_t rip_message[rip_table_valid_rows*2] = {};
size_t rip_message_size = 0;
res = get_data_from_frame(data, *recv_len, rip_message, &rip_message_size);
if (res != ESP_OK){
return ESP_FAIL; //crc or data len failed
}
for (size_t i = 0; i < rip_message_size-1; i+=2){
uint8_t board_id = rip_message[i];
uint8_t hops = rip_message[i+1];
printf("Received: board_id %d and number of hops %d on channel %d\n", board_id, hops, curr_channel);
for (size_t i = 0; i < message_size-1; i+=2){
uint8_t board_id = message[i];
uint8_t hops = message[i+1];
ESP_LOGI(DEBUG_LINK_TAG, "Received: board_id %d and number of hops %d on channel %d", board_id, hops, curr_channel);
RIPRow* entry = nullptr;
res = rip_find_entry(board_id, &entry);
res = rip_find_entry(board_id, &entry, true);
if (res != ESP_OK){
return ESP_FAIL;
}
if (entry == nullptr){
printf("rip pointer\n");
return ESP_FAIL; //no room for more entries in the table
}
if (entry->valid == RIP_INVALID_ROW){
if (entry->valid == RIP_NEW_ROW){
//adding a new entry
rip_add_entry(board_id, hops + 1, curr_channel, &entry);
} else {
@@ -292,10 +320,18 @@ esp_err_t DataLinkManager::receive(uint8_t* data, size_t data_len, size_t* recv_
*recv_len = 0;
}
//got frame but not destined for this board
if (header.receiver_id != this_board_id && header.receiver_id != BROADCAST_ADDR && header.seq_num != sequence_num_map[header.receiver_id]){
ESP_LOGI(DEBUG_LINK_TAG, "Sending message to board %d with message %s", header.receiver_id, message);
res = send(header.receiver_id, message, message_size, FrameType::DEBUG_CONTROL_TYPE, curr_channel);
*recv_len = 0;
return res;
}
return ESP_OK;
}
esp_err_t DataLinkManager::get_data_from_frame(uint8_t* data, size_t data_len, uint8_t* message, size_t* message_size){
esp_err_t DataLinkManager::get_data_from_frame(uint8_t* data, size_t data_len, uint8_t* message, size_t* message_size, frame_header* header){
if (data == nullptr){
ESP_LOGE(DEBUG_LINK_TAG, "Invalid data array");
return ESP_FAIL;
@@ -308,29 +344,31 @@ esp_err_t DataLinkManager::get_data_from_frame(uint8_t* data, size_t data_len, u
ESP_LOGE(DEBUG_LINK_TAG, "Invalid message size ptr");
return ESP_FAIL;
}
if (header == nullptr){
ESP_LOGE(DEBUG_LINK_TAG, "Invalid header ptr");
return ESP_FAIL;
}
if (IS_CONTROL_FRAME(data[5])){
control_frame temp = {0};
header->preamble = data[0];
header->sender_id = data[1];
header->receiver_id = data[2];
header->seq_num = (uint16_t)data[3] | ((uint16_t)data[4] << 8);
header->type_flag = data[5];
header->data_len = (uint16_t)data[6] | ((uint16_t)data[7] << 8);
temp.preamble = data[0];
temp.sender_id = data[1];
temp.receiver_id = data[2];
temp.seq_num = (uint16_t)data[3] | ((uint16_t)data[4] << 8);
temp.type_flag = data[5];
temp.data_len = data[6];
if (temp.data_len > data_len){
if (header->data_len > data_len){
ESP_LOGE(DEBUG_LINK_TAG, "Mismatch data length in control frame");
return ESP_FAIL;
}
*message_size = temp.data_len;
*message_size = header->data_len;
memcpy(temp.data, &data[7], temp.data_len);
memcpy(message, &data[7], temp.data_len);
memcpy(message, &data[8], header->data_len);
geneate_crc_16(data, 7*sizeof(uint8_t) + temp.data_len, &temp.crc_16);
geneate_crc_16(data, 8*sizeof(uint8_t) + header->data_len, &header->crc_16);
if (((uint16_t)data[7 + temp.data_len] | ((uint16_t)data[8 + temp.data_len] << 8)) != temp.crc_16){
if (((uint16_t)data[8 + header->data_len] | ((uint16_t)data[9 + header->data_len] << 8)) != header->crc_16){
//CRC mismatch
ESP_LOGE(DEBUG_LINK_TAG, "CRC Mismatch");
return ESP_FAIL;
@@ -382,8 +420,10 @@ esp_err_t DataLinkManager::print_frame_info(uint8_t* data, size_t data_len, uint
size_t message_size;
frame_header temp;
// print_buffer_binary(data, data_len);
return get_data_from_frame(data, data_len, message, &message_size);
return get_data_from_frame(data, data_len, message, &message_size, &temp);
}
/**
@@ -400,6 +440,7 @@ void DataLinkManager::init_rip(){
.channel = MAX_CHANNELS + 1, //invalid channels
.ttl = 0,
.valid = RIP_INVALID_ROW,
.ttl_flush = 0,
.row_sem = NULL
};
@@ -415,26 +456,22 @@ void DataLinkManager::init_rip(){
rip_table[0].ttl = RIP_TTL_START;
rip_table[0].valid = 1;
rip_table_valid_rows++;
//temp debug
rip_table[1].info = {
.board_id = 69,
.hops = 69,
};
rip_table[1].channel = MAX_CHANNELS + 1;
rip_table[1].ttl = RIP_TTL_START;
rip_table[1].valid = 1;
rip_table_valid_rows++;
// rip_table[1].info = {
// .board_id = 2,
// .hops = 1,
// };
// rip_table[1].channel = 0;
// rip_table[1].ttl = RIP_TTL_START;
// rip_table[1].valid = 1;
rip_table[2].info = {
.board_id = 3,
.hops = 2,
};
rip_table[2].channel = MAX_CHANNELS + 1,
rip_table[2].ttl = RIP_TTL_START,
rip_table[2].valid = 1;
rip_table_valid_rows++;
// rip_table[2].info = {
// .board_id = 1,
// .hops = 2,
// };
// rip_table[2].channel = 0,
// rip_table[2].ttl = RIP_TTL_START,
// rip_table[2].valid = 1;
start_rip_tasks();
}
@@ -468,7 +505,7 @@ esp_err_t DataLinkManager::rip_reset_entry_ttl(uint8_t board_id){
esp_err_t res;
res = rip_find_entry(board_id, &entry);
res = rip_find_entry(board_id, &entry, false);
if (res != ESP_OK){
return ESP_FAIL;
}
@@ -497,14 +534,15 @@ esp_err_t DataLinkManager::rip_update_entry(uint8_t new_hop, uint8_t channel, RI
return ESP_FAIL;
}
if (new_hop < (*entry)->info.hops){
if ((*entry)->info.hops > new_hop && (*entry)->info.hops != RIP_MAX_HOPS + 1){ //no count to infinity if path is invalid
(*entry)->info.hops = new_hop;
(*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;
ESP_LOGI(DEBUG_LINK_TAG, "board_id %d now has hops %d from channel %d", (*entry)->info.board_id, (*entry)->info.hops, channel);
ESP_LOGI(DEBUG_LINK_TAG, "refreshed board_id %d ttl", (*entry)->info.board_id);
xSemaphoreGive((*entry)->row_sem);
@@ -512,37 +550,59 @@ esp_err_t DataLinkManager::rip_update_entry(uint8_t new_hop, uint8_t channel, RI
}
/**
* @brief Finds the board_id in the table. If board_id does not exist in the table, `entry` will contain an empty row to write into.
* @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
*/
esp_err_t DataLinkManager::rip_find_entry(uint8_t board_id, RIPRow** entry){
esp_err_t DataLinkManager::rip_find_entry(uint8_t board_id, RIPRow** entry, bool reserve_row = false){
RIPRow* free_slot = nullptr;
for (size_t i = 0; i < RIP_MAX_ROUTES; i++){
if (xSemaphoreTake(rip_table[i].row_sem, (TickType_t)RIP_MAX_SEM_WAIT) != pdTRUE){
return ESP_FAIL;
}
if (rip_table[i].valid != RIP_INVALID_ROW && rip_table[i].info.board_id == board_id){
if (rip_table[i].valid == RIP_VALID_ROW && rip_table[i].info.board_id == board_id){
*entry = &rip_table[i];
xSemaphoreGive(rip_table[i].row_sem);
break;
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){
*entry = &rip_table[i];
if (rip_table[i].valid == RIP_INVALID_ROW && free_slot == nullptr){
free_slot = &rip_table[i];
}
xSemaphoreGive(rip_table[i].row_sem);
}
ESP_LOGI(DEBUG_LINK_TAG, "Finished looking for %d in table", board_id);
if (!reserve_row){
return ESP_OK;
}
if (free_slot != nullptr){
if (xSemaphoreTake(free_slot->row_sem, RIP_MAX_SEM_WAIT) != pdTRUE) {
return ESP_FAIL;
}
// IMPORTANT: Mark it as taken so others don't grab it
free_slot->valid = RIP_NEW_ROW; // Or some other init state
free_slot->info.board_id = board_id;
*entry = free_slot;
xSemaphoreGive(free_slot->row_sem);
ESP_LOGI(DEBUG_LINK_TAG, "Reserved new entry for board %d", board_id);
}
return ESP_OK;
}
esp_err_t DataLinkManager::broadcast_rip_frame(){
esp_err_t DataLinkManager::broadcast_rip_frame(bool manual_broadcast = false){
//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_table_valid_rows*2] = {};
uint8_t rip_message[RIP_MAX_ROUTES*2] = {};
size_t message_idx = 0;
for (size_t i = 0; i < RIP_MAX_ROUTES; i++){
@@ -552,15 +612,31 @@ esp_err_t DataLinkManager::broadcast_rip_frame(){
continue;
}
if (rip_table[i].info.hops == RIP_MAX_HOPS + 1){
//invalid hop, decrement counter
rip_table[i].ttl_flush--;
if (rip_table[i].ttl_flush == 0 && !manual_broadcast){
rip_table[i].valid = RIP_INVALID_ROW;
xSemaphoreGive(rip_table[i].row_sem);
continue;
}
}
// //test to ensure routing works
// if (rip_table[i].info.board_id == 25){
// rip_message[message_idx++] = 25;
// rip_message[message_idx++] = 10;
// } else {
rip_message[message_idx++] = rip_table[i].info.board_id;
rip_message[message_idx++] = rip_table[i].info.hops;
// }
xSemaphoreGive(rip_table[i].row_sem);
}
esp_err_t res;
for (uint8_t channel = 0; channel < MAX_CHANNELS; channel++){
ESP_LOGI(DEBUG_LINK_TAG, "sending type %x",static_cast<uint8_t>(FrameType::RIP_TABLE_CONTROL));
for (uint8_t channel = 0; channel < num_channels; channel++){
res = send(BROADCAST_ADDR, rip_message, message_idx, FrameType::RIP_TABLE_CONTROL, channel);
if (res != ESP_OK){
ESP_LOGE(DEBUG_LINK_TAG, "Failed to send rip frame on channel %d", channel);
@@ -570,9 +646,35 @@ esp_err_t DataLinkManager::broadcast_rip_frame(){
return ESP_OK;
}
/**
* @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
*/
esp_err_t DataLinkManager::route_frame(uint8_t dest_id, uint8_t* channel_to_send){
RIPRow* entry = nullptr;
esp_err_t res;
res = rip_find_entry(dest_id, &entry, false);
if (entry == nullptr){
return ESP_FAIL;
}
if (res != ESP_OK){
return res;
}
*channel_to_send = entry->channel;
return ESP_OK;
}
[[noreturn]] void DataLinkManager::rip_broadcast_timer_function(void* args){
DataLinkManager* link_layer_obj = static_cast<DataLinkManager*>(args);
if (link_layer_obj == nullptr){
if (link_layer_obj == nullptr || link_layer_obj->manual_broadcasts == nullptr){
ESP_LOGE(DEBUG_LINK_TAG, "RIP Broadacst task failed to start due to invalid pointer");
vTaskDelete(nullptr);
}
@@ -581,9 +683,10 @@ esp_err_t DataLinkManager::broadcast_rip_frame(){
esp_err_t res;
while(true){
vTaskDelay(pdMS_TO_TICKS(RIP_BROADCAST_INTERVAL)); //wait RIP_BROADCAST_INTERVAL ms
bool dummy;
xQueueReceive(link_layer_obj->manual_broadcasts, &dummy, pdMS_TO_TICKS(RIP_BROADCAST_INTERVAL)); //wait up to RIP_BROADCAST_INTERVAL ms
ESP_LOGI(DEBUG_LINK_TAG, "Broadcasting table..."); //debug
res = link_layer_obj->broadcast_rip_frame();
res = link_layer_obj->broadcast_rip_frame(true);
if (res != ESP_OK){
ESP_LOGE(DEBUG_LINK_TAG, "Failed to broadcast rip frame");
}
@@ -592,16 +695,16 @@ esp_err_t DataLinkManager::broadcast_rip_frame(){
[[noreturn]] void DataLinkManager::rip_ttl_decrement_task(void* args){
DataLinkManager* link_layer_obj = static_cast<DataLinkManager*>(args);
if (link_layer_obj == nullptr){
if (link_layer_obj == nullptr || link_layer_obj->manual_broadcasts == nullptr){
ESP_LOGE(DEBUG_LINK_TAG, "RIP Broadacst task failed to start due to invalid pointer");
vTaskDelete(nullptr);
}
ESP_LOGI(DEBUG_LINK_TAG, "Starting RIP ttl decrement task");
bool broadcast = false;
while(true){
vTaskDelay(pdMS_TO_TICKS(RIP_MS_TO_SEC)); //run every second
for (size_t i = 0; i < RIP_MAX_ROUTES; i++){
ESP_LOGI(DEBUG_LINK_TAG, "Decrementing ttl on entry %d", i);
// ESP_LOGI(DEBUG_LINK_TAG, "Decrementing ttl on entry %d", i);
if (xSemaphoreTake(link_layer_obj->rip_table[i].row_sem, (TickType_t)RIP_MAX_SEM_WAIT) !=pdTRUE){
ESP_LOGE(DEBUG_LINK_TAG, "Failed to get sem from entry %d", i);
continue;
@@ -610,14 +713,26 @@ esp_err_t DataLinkManager::broadcast_rip_frame(){
xSemaphoreGive(link_layer_obj->rip_table[i].row_sem);
continue;
}
if (link_layer_obj->rip_table[i].ttl == 0){
link_layer_obj->rip_table[i].valid = RIP_INVALID_ROW;
} else {
if (link_layer_obj->rip_table[i].ttl != 0){
// link_layer_obj->rip_table[i].valid = RIP_INVALID_ROW;
// ESP_LOGI(DEBUG_LINK_TAG, "Entry %d now has ttl %d", i, link_layer_obj->rip_table[i].ttl);
// } else {
link_layer_obj->rip_table[i].ttl--;
}
ESP_LOGI(DEBUG_LINK_TAG, "Entry %d now has ttl %d", i, link_layer_obj->rip_table[i].ttl);
if (link_layer_obj->rip_table[i].ttl == 0){
link_layer_obj->rip_table[i].info.hops = RIP_MAX_HOPS + 1;
link_layer_obj->rip_table[i].ttl_flush = RIP_FLUSH_COUNT;
broadcast = true;
}
}
xSemaphoreGive(link_layer_obj->rip_table[i].row_sem);
}
if (broadcast && uxQueueMessagesWaiting(link_layer_obj->manual_broadcasts) == 0){
broadcast = false;
bool dummy = true;
xQueueSend(link_layer_obj->manual_broadcasts, &dummy, 0);
}
}
}
@@ -628,8 +743,10 @@ esp_err_t DataLinkManager::broadcast_rip_frame(){
* - start a task to periodically decrement the ttl values of each row in the RIP table (WIP) - this will require some sort of mutex on the table itself
*/
void DataLinkManager::start_rip_tasks(){
manual_broadcasts = xQueueCreate(1, sizeof(bool));
ESP_LOGI(DEBUG_LINK_TAG, "Starting RIP Broadcast task");
xTaskCreate(DataLinkManager::rip_broadcast_timer_function, "RIPBroadcastTask", 4096, static_cast<void*>(this), 5, NULL);
xTaskCreate(DataLinkManager::rip_broadcast_timer_function, "RIPBroadcast", 2048, static_cast<void*>(this), 5, NULL);
ESP_LOGI(DEBUG_LINK_TAG, "Starting RIP TTL task");
xTaskCreate(DataLinkManager::rip_ttl_decrement_task, "RIPTTLTask", 4096, static_cast<void*>(this), 5, NULL);
xTaskCreate(DataLinkManager::rip_ttl_decrement_task, "RIPTTL", 2048, static_cast<void*>(this), 5, NULL);
}

View File

@@ -39,7 +39,7 @@ static const uint16_t crc16_table[256] = {
class DataLinkManager{
public:
DataLinkManager(uint8_t board_id);
DataLinkManager(uint8_t board_id, uint8_t num_channels);
~DataLinkManager();
esp_err_t send(uint8_t dest_board, uint8_t* data, uint16_t data_len, FrameType type, uint8_t curr_channel);
esp_err_t start_receive_frames(uint8_t curr_channel);
@@ -48,7 +48,8 @@ class DataLinkManager{
esp_err_t send_discover_frame();
private:
uint8_t this_board_id = 0;
std::priority_queue<Frame, std::vector<Frame>, FrameCompare> frame_queue; //create a priority queue
uint8_t num_channels = MAX_CHANNELS;
//std::priority_queue<Frame, std::vector<Frame>, FrameCompare> frame_queue; //create a priority queue - not in use
std::unique_ptr<RMTManager> phys_comms;
std::unordered_map<uint8_t, uint16_t> sequence_num_map;
@@ -56,7 +57,7 @@ class DataLinkManager{
esp_err_t get_board_id(uint8_t& board_id);
void print_binary(uint8_t byte);
void print_buffer_binary(const uint8_t* buffer, size_t length);
esp_err_t get_data_from_frame(uint8_t* data, size_t data_len, uint8_t* message, size_t* message_size);
esp_err_t get_data_from_frame(uint8_t* data, size_t data_len, uint8_t* message, size_t* message_size, frame_header* header);
esp_err_t geneate_crc_16(uint8_t* data, size_t data_len, uint16_t* crc);
//==== RIP related functions ====
@@ -69,7 +70,7 @@ class DataLinkManager{
*/
void init_rip();
esp_err_t rip_find_entry(uint8_t board_id, RIPRow** entry);
esp_err_t rip_find_entry(uint8_t board_id, RIPRow** entry, bool reserve_row);
esp_err_t rip_update_entry(uint8_t new_hop, uint8_t channel, RIPRow** entry);
esp_err_t rip_add_entry(uint8_t board_id, uint8_t hops, uint8_t channel, RIPRow** entry);
esp_err_t rip_reset_entry_ttl(uint8_t board_id);
@@ -77,13 +78,12 @@ class DataLinkManager{
//this is stored locally with metadata `ttl`
// std::unordered_map<uint8_t, RIPRow> rip_table; //using a hash map to store the routes to other boards - will be used as we scale up
RIPRow rip_table[RIP_MAX_ROUTES]; //temp using a static array
uint8_t rip_table_valid_rows = 0;
void start_rip_tasks();
esp_err_t broadcast_rip_frame();
esp_err_t broadcast_rip_frame(bool manual_broadcast);
[[noreturn]] static void rip_broadcast_timer_function(void* args);
[[noreturn]] static void rip_ttl_decrement_task(void* args);
QueueHandle_t manual_broadcasts;
esp_err_t route_frame(uint8_t dest_id, uint8_t* channel_to_send);
};

View File

@@ -9,8 +9,8 @@
#define START_OF_FRAME 0xAB //0b1010_1011 - denotes the start of frame
#define MAX_GENERIC_DATA_LEN (180) //Max 180B
#define MAX_CONTROL_DATA_LEN (1 << 5) // Max 32B
#define MAX_GENERIC_DATA_LEN (1 << 16) //Max 65.5KiB
#define MAX_CONTROL_DATA_LEN (1 << 8) // Max 256B
//Flags
#define FLAG_FRAG 0x8 //0b1000 //this fragmented frame is part of a larger frame
@@ -24,6 +24,7 @@
#define IS_CONTROL_FRAME(x) (((x) & 0x80) != 0)
#define CONTROL_FRAME_OVERHEAD 9
#define GENERIC_FRAME_OVERHEAD 12
#define CONTROL_FRAME_TYPE 0x80 //if the frame type MSB is set to 1, use the control frame
//Types (total 2^4 = 16 different types)
@@ -45,10 +46,10 @@ typedef struct _control_frame{
uint8_t receiver_id; //receiver board id
uint16_t seq_num; //sequence number to differentiate frames being sent from sender to receiver
uint8_t type_flag; //(type << 4) | flag - both are 4 bits
uint8_t data_len; //Data Length (max 32B)
uint16_t data_len; //Data Length (max 256B)
uint8_t data[MAX_CONTROL_DATA_LEN]; //Variable Length of Data
uint16_t crc_16; //CRC-16
} control_frame; //this will have a max size of 9 + 32B = 41B
} control_frame; //this will have a max size of 9 + 256B = 265B
typedef struct _data_link_frame{
uint8_t preamble; //Start of Frame
@@ -60,39 +61,17 @@ typedef struct _data_link_frame{
uint16_t data_len; //Data Length (max 178B)
uint8_t data[MAX_GENERIC_DATA_LEN]; //Variable Length of Data
uint16_t crc_16; //CRC-16
} data_link_frame; //this will have a max size of 12 + 180 B = 192B
} data_link_frame; //this will have a max size of ~65.5KiB
#pragma pack(pop)
using Frame = std::variant<control_frame, data_link_frame>;
//defining a comparison operation for comparing two frames -- not tested
struct FrameCompare {
bool operator()(const Frame& a, const Frame& b) const {
auto msb_set = [](uint8_t type_flag) {
return (type_flag & 0x80) != 0; // 0x80 == 1000 0000
};
auto get_type_flag = [](const Frame& pkt) -> uint8_t {
return std::visit([](auto&& p) -> uint8_t {
return p.type_flag;
}, pkt);
};
uint8_t type_flag_a = get_type_flag(a);
uint8_t type_flag_b = get_type_flag(b);
bool a_msb = msb_set(type_flag_a);
bool b_msb = msb_set(type_flag_b);
if (a_msb != b_msb) {
return !a_msb; // Frame with MSB set (true) should come first
}
// Tie-breaker: use seq_num if MSB is the same
return std::visit([](auto&& p1, auto&& p2) {
return p1.seq_num > p2.seq_num; // smaller seq_num = higher priority (older)
}, a, b);
}
};
typedef struct _header{
uint8_t preamble; //Start of Frame
uint8_t sender_id; //sender board id
uint8_t receiver_id; //receiver board id
uint16_t seq_num; //sequence number to differentiate frames being sent from sender to receiver
uint8_t type_flag; //(type << 4) | flag - both are 4 bits
uint16_t frag_info; //(total_frag_num << 8) | frag_num - total_frag_num denotes the total number of fragmented frames to expect for this sequence number(?) and frag_num denotes the fragment frame num
uint16_t data_len; //Data Length (max 178B)
uint16_t crc_16; //CRC-16
} frame_header;
#endif //DATA_LINK

View File

@@ -5,11 +5,14 @@
#define RIP_MAX_HOPS 15 //16 or more is infinite
#define RIP_MAX_ROUTES 10 //for the demo we will use up to 10 boards in total (9 other boards will be connected = 9 rows)
#define RIP_INVALID_ROW 0
// #define RIP_BROADCAST_INTERVAL 30000 //broadcast every 30 seconds (30000ms)
#define RIP_BROADCAST_INTERVAL 3000 //temp broadcast every 3 seconds (3000ms)
#define RIP_VALID_ROW 1
#define RIP_NEW_ROW 2
#define RIP_BROADCAST_INTERVAL 30000 //broadcast every 30 seconds (30000ms)
// #define RIP_BROADCAST_INTERVAL 3000 //temp broadcast every 3 seconds (3000ms)
#define RIP_TTL_START 180 //seconds
#define RIP_MS_TO_SEC 1000 //1000 ms to 1 sec
#define RIP_MAX_SEM_WAIT 30
#define RIP_FLUSH_COUNT 8 //flush after 8*30 seconds = 240 seconds
/**
* @brief Routing data to a board
@@ -25,6 +28,7 @@ typedef struct _rip_row{
uint8_t channel; //rmt channel
uint8_t ttl; //how long this entry is valid for. starting value is 180 seconds
uint8_t valid; //is this a valid entry?
uint8_t ttl_flush; //if hops is invalid, this would count the amount of time until this entry would be invalid (in multiples of 30 seconds)
StaticSemaphore_t mutex_buf; //where mutex state is stored
SemaphoreHandle_t row_sem; //mutex sem handle of mutex_buf
} RIPRow;

View File

@@ -7,22 +7,31 @@
#include "freertos/semphr.h"
#include "esp_log.h"
RMTManager::RMTManager(){
/**
* @brief Construct a new RMTManager::RMTManager object
*
* @param num_channels Number of channels to init (1-4) inclusive
*/
RMTManager::RMTManager(uint8_t num_channels = MAX_CHANNELS){
if (num_channels > MAX_CHANNELS || num_channels == 0){
ESP_LOGE(DEBUG_TAG, "Invalid number of channels to init");
return;
}
this->num_channels = num_channels;
esp_err_t res = init();
if (res != ESP_OK){
//failed
ESP_LOGE(DEBUG_TAG, "Failed to initialize the RMTManager");
return;
}
ESP_LOGD(DEBUG_TAG, "RMTManager has been initialized");
ESP_LOGI(DEBUG_TAG, "RMTManager has been initialized");
}
esp_err_t RMTManager::init_tx_channel(){
esp_err_t res_tx = ESP_FAIL;
//setup encoder config
for (uint8_t i = 0; i < MAX_CHANNELS; i++){
for (uint8_t i = 0; i < num_channels; i++){
//setup encoder config
reset_encoder_context(&channels[i].encoder_context); //ensure the encoder context is initialized
rmt_simple_encoder_config_t encoder_config = {
.callback = encoder_callback,
@@ -118,7 +127,7 @@ esp_err_t RMTManager::init_tx_channel(){
continue;
}
printf("Successfully enabled TX channel %d\n", i);
ESP_LOGI(DEBUG_TAG, "Successfully enabled TX channel %d", i);
}
return ESP_OK;
@@ -155,7 +164,7 @@ bool RMTManager::rmt_tx_done_callback(rmt_channel_handle_t channel, const rmt_tx
}
esp_err_t RMTManager::wait_until_send_complete(uint8_t channel_num){
if (channel_num >= MAX_CHANNELS){
if (channel_num >= num_channels){
ESP_LOGE(DEBUG_TAG, "Invalid channel number");
return ESP_FAIL;
}
@@ -186,7 +195,7 @@ bool RMTManager::rmt_rx_done_callback(rmt_channel_handle_t channel, const rmt_rx
}
esp_err_t RMTManager::init_rx_channel(){
for (uint8_t i = 0; i < MAX_CHANNELS; i++){
for (uint8_t i = 0; i < num_channels; i++){
rmt_rx_channel_config_t rx_channel_config = {
.gpio_num = rx_gpio[i],
.clk_src = RMT_CLK_SRC_DEFAULT,
@@ -229,6 +238,8 @@ esp_err_t RMTManager::init_rx_channel(){
ESP_LOGE(DEBUG_TAG, "Failed to enable RX channel");
return ESP_FAIL;
}
ESP_LOGI(DEBUG_TAG, "Enabled RX Channel %d", i);
}
return ESP_OK;
@@ -250,7 +261,7 @@ esp_err_t RMTManager::init(){
return ESP_FAIL;
}
for (uint8_t i = 0; i < MAX_CHANNELS; i++){
for (uint8_t i = 0; i < num_channels; i++){
if (channels[i].tx_rmt_handle != NULL && channels[i].rx_rmt_handle != NULL && channels[i].tx_done_semaphore != NULL && channels[i].rx_queue != NULL){
channels[i].status = CHANNEL_READY_STATUS;
}
@@ -371,13 +382,15 @@ void RMTManager::reset_encoder_context(rmt_encoder_context_t* ctx){
* @param config
* @return int
*/
int RMTManager::send(uint8_t* data, size_t size, rmt_transmit_config_t* config, uint8_t channel_num){
if (channel_num >= MAX_CHANNELS){
esp_err_t RMTManager::send(uint8_t* data, size_t size, rmt_transmit_config_t* config, uint8_t channel_num){
if (channel_num >= num_channels){
ESP_LOGE(DEBUG_TAG, "send() error: invalid channel number");
return ESP_FAIL;
}
if (channels[channel_num].status == CHANNEL_NOT_READY_STATUS){
ESP_LOGE(DEBUG_TAG, "send() error: Channel %d is not ready", channel_num);
return ESP_FAIL;
}
if (this->channels[channel_num].tx_rmt_handle == nullptr) {
@@ -638,7 +651,7 @@ int RMTManager::convert_symbols_to_char(rmt_symbol_word_t* symbols, size_t num,
* @return esp_err_t
*/
esp_err_t RMTManager::start_receiving(uint8_t channel_num){
if (channel_num >= MAX_CHANNELS){
if (channel_num >= num_channels){
return ESP_FAIL;
}
@@ -673,8 +686,8 @@ esp_err_t RMTManager::start_receiving(uint8_t channel_num){
*
* @return int
*/
int RMTManager::receive(uint8_t* recv_buf, size_t size, size_t* output_size, uint8_t channel_num){
if (channel_num >= MAX_CHANNELS){
esp_err_t RMTManager::receive(uint8_t* recv_buf, size_t size, size_t* output_size, uint8_t channel_num){
if (channel_num >= num_channels){
return ESP_FAIL;
}
@@ -717,7 +730,7 @@ int RMTManager::receive(uint8_t* recv_buf, size_t size, size_t* output_size, uin
}
RMTManager::~RMTManager(){
for (uint8_t i = 0; i < MAX_CHANNELS; i++){
for (uint8_t i = 0; i < num_channels; i++){
if (this->channels[i].tx_rmt_handle) {
rmt_disable(this->channels[i].tx_rmt_handle);
rmt_del_channel(this->channels[i].tx_rmt_handle);

View File

@@ -8,6 +8,7 @@
#include "driver/rmt_tx.h"
#include "driver/rmt_rx.h"
#include "soc/gpio_num.h"
#include "RMTSymbols.h"
#include <cstring>
@@ -61,10 +62,10 @@ typedef struct _rmt_channel{
class RMTManager{
public:
RMTManager();
RMTManager(uint8_t num_channels);
~RMTManager();
int send(uint8_t* data, size_t size, rmt_transmit_config_t* config, uint8_t channel_num); //temp function to send some string data
int receive(uint8_t* recv_buf, size_t size, size_t* output_size, uint8_t channel_num);
esp_err_t send(uint8_t* data, size_t size, rmt_transmit_config_t* config, uint8_t channel_num); //temp function to send some string data
esp_err_t receive(uint8_t* recv_buf, size_t size, size_t* output_size, uint8_t channel_num);
static size_t encoder_callback(const void* data, size_t data_size, size_t symbols_written,
size_t symbols_free, rmt_symbol_word_t* symbols, bool* done, void* arg);
@@ -77,6 +78,7 @@ class RMTManager{
esp_err_t wait_until_send_complete(uint8_t channel_num);
private:
uint8_t num_channels; //number of channels initalized
esp_err_t init();
void reset_encoder_context(rmt_encoder_context_t* ctx);
esp_err_t init_tx_channel();