Reliability fixes for wired comms

This commit is contained in:
2026-03-02 11:20:45 -05:00
parent b1ca6da6a1
commit da858d1ca6
4 changed files with 27 additions and 11 deletions

View File

@@ -151,7 +151,14 @@ esp_err_t DataLinkManager::complete_fragment(uint16_t board_id, uint16_t sequenc
if (metadata.num_fragments_rx != metadata.fragments.size()){
return ESP_ERR_INVALID_STATE;
}
uint16_t total_data_len = metadata.num_fragments_rx * MAX_GENERIC_DATA_LEN; //max data size with n fragments
// Compute the exact total size from actual fragment data lengths, not the
// theoretical maximum (num_fragments * MAX_GENERIC_DATA_LEN). The last fragment
// is almost always shorter, so over-allocating would leave garbage bytes at the
// end that consumers reading combined_data->size() would see.
uint16_t total_data_len = 0;
for (size_t i = 0; i < metadata.num_fragments_rx; i++){
total_data_len += metadata.fragments[i].data_len;
}
xSemaphoreGive(rx_fragment_mutex[channel]);
auto combined_data = std::make_unique<std::vector<uint8_t>>();
@@ -181,6 +188,7 @@ esp_err_t DataLinkManager::complete_fragment(uint16_t board_id, uint16_t sequenc
memcpy(&combined_data->data()[prev_index], metadata.fragments[i].data, metadata.fragments[i].data_len);
prev_index += metadata.fragments[i].data_len;
}
combined_data->resize(prev_index); // ensure vector size == actual data (no garbage tail bytes)
xSemaphoreGive(rx_fragment_mutex[channel]);