-
Notifications
You must be signed in to change notification settings - Fork 0
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
Rx Queue #2
base: main
Are you sure you want to change the base?
Rx Queue #2
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
#ifndef CANARD_RX_QUEUE_H | ||
#define CANARD_RX_QUEUE_H | ||
|
||
#include <stdlib.h> | ||
#include <canard.h> | ||
|
||
struct CanardRxQueueItem { | ||
struct CanardRxQueueItem* next; | ||
CanardCANFrame frame; | ||
}; | ||
|
||
extern struct CanardRxQueueItem* rxQueueHead; | ||
extern struct CanardRxQueueItem* rxQueueTail; | ||
|
||
void enqueueRxFrame(CanardCANFrame* rx_frame); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. more comments to describe what the function does, its parameters, and return value are greatly appreciated. |
||
struct CanardRxQueueItem* dequeueRxFrame(); | ||
|
||
#endif |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
#include <stdio.h> | ||
#include <stdlib.h> | ||
#include <canard.h> | ||
#include "canardRxQueue.h" | ||
|
||
struct CanardRxQueueItem* rxQueueHead = NULL; | ||
struct CanardRxQueueItem* rxQueueTail = NULL; | ||
|
||
void enqueueRxFrame(CanardCANFrame* rx_frame) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Currently, this enqueue function returns nothing. It would be so much better if this function could return some sort of error code. For instance, |
||
|
||
// dynamically allocate memory | ||
struct CanardRxQueueItem* new = (struct CanardRxQueueItem*)malloc(sizeof(struct CanardRxQueueItem)); | ||
|
||
if (new == NULL) { | ||
printf("CanardRxQueueItem memory allocation failed"); | ||
return; | ||
} | ||
|
||
new->next = NULL; | ||
new->frame = *rx_frame; | ||
|
||
if (rxQueueTail) { | ||
rxQueueTail->next = new; | ||
} else { | ||
rxQueueHead = new; | ||
} | ||
|
||
rxQueueTail = new; | ||
} | ||
|
||
|
||
struct CanardRxQueueItem* dequeueRxFrame() { | ||
|
||
if (!rxQueueHead) { | ||
return NULL; // queue is empty | ||
} | ||
|
||
struct CanardRxQueueItem* dequeuedItem = rxQueueHead; | ||
rxQueueHead = rxQueueHead->next; | ||
|
||
if (!rxQueueHead) { | ||
rxQueueTail = NULL; // after dequeuing, the queue is empty | ||
} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Since you dynamically allocated the queue item, the allocated memory should be handled carefully when we are done with it. You should really explicitly write a comment somewhere to let people know they need to free the memory block themselves after they pop it from the queue. Or, more preferably, you handle it for the user. |
||
|
||
return dequeuedItem; | ||
} | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
What is the size of the queue? If there is no boundary for the size of the queue, then what if the rx processing speed is slower than the rx incoming speed? I guess the system would run out of the memory and hard fault.
I think it will be better to keep track of the length of the queue somehow.