c - How to make a FIFO buffer with can_frame structure inside? -


in moment working on project on few processors communication between using can bus. main controler (beagle bone) controls other device using can bus. using socket can linux framework wrote proccess reads can messages send other devices, , want put messages fifo bufer, , obrabotam messages. need write fifo buffer can_frame structure inside.

for example:

struct can_buffer {     struct can_frame *frames;     int head;     int tail;     int size; };  can_buffer new_can_buffer (size_t capacity) {     can_buffer rb = malloc(sizeof(struct can_buffer));     if (rb) {          /* 1 byte used detecting full condition. */         rb->size = capacity + 1;         rb->frames = malloc(rb->size * sizeof(struct can_frame));         if (rb->frames)             can_buffer_reset(rb);         else {             free(rb);             return 0;         }     }     return rb; }  size_t can_buffer_size(const struct can_buffer *rb) {     return rb->size; }  size_t can_buffer_capacity(const struct can_buffer *rb) {     return can_buffer_buffer_size(rb) - 1; }  size_t can_buffer_free(const struct can_buffer *rb) {     if (rb->head >= rb->tail)         return can_buffer_capacity(rb) - (rb->head - rb->tail);     else         return rb->tail - rb->head - 1; }  int can_buffer_is_full(const struct can_buffer *rb) {     return can_buffer_free(rb) == 0; }  int can_buffer_is_empty(const struct can_buffer *rb) {     return can_buffer_free(rb) ==can_buffer_capacity(rb); }  void can_buffer_reset(can_buffer rb) {     rb->head = rb->tail = 0; } 

......... ........

/* add message end of queue. */

void can_buffer_push(struct can_buffer *cb, struct can_frame *frame) {     memcpy(&cb->frames[cb->tail], frame, sizeof(struct can_frame));     cb->tail = (cb->tail + 1) % cb->size; }  /* retrieve message start of queue. */ can_frame *can_buffer_pop(struct can_buffer *cb) {     struct can_frame *frame;     memcpy(frame, &cb->frames[cb->head], sizeof(struct can_frame));     cb->head = (cb->head + 1) % cb->size;     return frame; } 

but canoot successfully. think problem every can_frame structure inside structure again,that problem (for example int, char etc), not know how solve issue.

how can make fifo buffer can store can_frame structure inside?

i need write in c lagnuage

in main call

can_buffer can_buff;  can_buff = new_can_buffer(100);  can_buffer_push(can_buff,frame); 

frame = can_frame received

can_buff = fifo buffer

well, have incompletely modified ringbuf routines. specifically, don't allocate enough space structures here:

if (rb) {      /* 1 byte used detecting full condition. */     rb->size = capacity + 1;     rb->frames = malloc(rb->size);     if (rb->frames)         ringbuf_reset(rb);     else {         free(rb);         return 0;     } } 

the malloc needs

rb->frames = malloc(rb->size * sizeof(struct can_frame)); 

and should update ringbuf_reset() call on next line renamed can_buffer_reset()


addendum:

i noticed need update ringbuf_reset() function rb->head = rb->tail = 0


addendum 2:

referencing newly added code, can_buffer_pop() not work correctly doesn't check message existing , doesn't allocate memory popped message.

there typo in can_buffer_capacity().

editorial: i suggest writing simple test program executes these functions. it's frustrating catch number of these small gotchas.


Comments

Popular posts from this blog

sql server - Cannot query correctly (MSSQL - PHP - JSON) -

php - trouble displaying mysqli database results in correct order -

C++ Linked List -