Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

prevent sending partial multi-frame transfers #72

Merged
merged 1 commit into from
Sep 24, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 15 additions & 1 deletion canard.c
Original file line number Diff line number Diff line change
Expand Up @@ -1206,6 +1206,20 @@ CANARD_INTERNAL int16_t enqueueTxFrames(CanardInstance* ins,
uint8_t toggle = 0;
uint8_t sot_eot = 0x80;

/*
see if we are going to be able to allocate enough blocks for
this transfer. If not then stop now, otherwise we will end
up doing a partial (corrupt) transfer which will just make
the situation worse as it will waste bus bandwidth
*/
const uint16_t total_bytes = transfer->payload_len + 2; // including CRC
const uint8_t bytes_per_frame = frame_max_data_len-1; // sot/eot byte consumes one byte
const uint16_t frames_needed = (total_bytes + (bytes_per_frame-1)) / bytes_per_frame;
const uint16_t blocks_available = ins->allocator.statistics.capacity_blocks - ins->allocator.statistics.current_usage_blocks;
if (blocks_available < frames_needed) {
return -CANARD_ERROR_OUT_OF_MEMORY;
}

CanardTxQueueItem* queue_item = NULL;

while (transfer->payload_len - data_index != 0)
Expand All @@ -1214,7 +1228,7 @@ CANARD_INTERNAL int16_t enqueueTxFrames(CanardInstance* ins,
if (queue_item == NULL)
{
CANARD_ASSERT(false);
return -CANARD_ERROR_OUT_OF_MEMORY; // TODO: Purge all frames enqueued so far
return -CANARD_ERROR_OUT_OF_MEMORY;
}

uint16_t i = 0;
Expand Down
Loading