can_ring.c File Reference

Go to the source code of this file.

Functions

void can_ring_init (CAN_RING *ring, CAN_FRAME *frame_buf, int buf_len)
 Initializes the given CAN_RING struct with the allocated CAN_FRAME array/buffer and the length of that array/buffer.
int can_ring_pop (CAN_RING *ring, CAN_FRAME *frame)
 Pops the oldest CAN_FRAME into the allocated struct given by the pointer.
int can_ring_push (CAN_RING *ring, CAN_FRAME *frame)
 Pushes the CAN_FRAME onto the CAN_RING buffer.

Detailed Description

Author:
Tommy Craig and Jason Cortell

Ring buffers for CAN packets.

Definition in file can_ring.c.


Function Documentation

void can_ring_init ( CAN_RING *  ring,
CAN_FRAME *  frame_buf,
int  buf_len 
)

Initializes the given CAN_RING struct with the allocated CAN_FRAME array/buffer and the length of that array/buffer.

Parameters:
ring A pointer to the CAN_RING struct to setup.
frame_buf A pointer to an allocated array of CAN_FRAME structs.
buf_len The length of the given array.

Definition at line 26 of file can_ring.c.

00026                                                                        {
00027   ring->buf = frame_buf;
00028   ring->buf_len = buf_len;
00029   /*
00030   ring->in_idx = buf_len - 1;
00031   ring->out_idx = buf_len - 1;
00032   */
00033   ring->in_idx = 0;
00034   ring->out_idx = 0;
00035 }

int can_ring_pop ( CAN_RING *  ring,
CAN_FRAME *  frame 
)

Pops the oldest CAN_FRAME into the allocated struct given by the pointer.

Parameters:
ring A pointer to the CAN_RING struct.
frame An empty allocated frame to put the popped frame into.
Returns:
0 if successful, 1 if buffer is empty.

Definition at line 79 of file can_ring.c.

00079                                                     {
00080  
00081   if (ring->out_idx != ring->in_idx)
00082   {
00083     *frame = ring->buf[ring->out_idx];
00084     if (ring->out_idx == (ring->buf_len) - 1)
00085     {
00086       ring->out_idx = 0;
00087     }
00088     else
00089     {
00090       ++ring->out_idx;
00091     }
00092     return 0;   //frame popped successfully
00093   }
00094   else
00095   {
00096     return 1;   //buffer empty
00097   }
00098  
00099  /* int out_idx = ring->out_idx;
00100   int  ret;
00101   int next_out_idx = out_idx + 1;
00102 
00103   if(next_out_idx >= ring->buf_len){
00104     next_out_idx -= ring->buf_len;
00105   }
00106   if(out_idx == ring->in_idx){
00107     ret = 1;
00108   } else {
00109     *frame = ring->buf[next_out_idx];
00110     ret = 0;
00111     ring->out_idx = next_out_idx;
00112   }
00113   return ret;
00114 
00115   */
00116 }

int can_ring_push ( CAN_RING *  ring,
CAN_FRAME *  frame 
)

Pushes the CAN_FRAME onto the CAN_RING buffer.

If the buffer is full, new data is lost.

Parameters:
ring A pointer to the CAN_RING struct.
frame A pointer to the CAN_FRAME to add to the ring buffer.
Returns:
0 if successful, 1 if buffer is already full.

Definition at line 44 of file can_ring.c.

00044                                                       {
00045  // int  ret;
00046   int next_in_idx;
00047   /*
00048   int next_in_idx = ring->in_idx + 1;
00049 
00050   if(next_in_idx >= ring->buf_len){
00051     next_in_idx -= ring->buf_len;
00052   }  
00053   if(next_in_idx == ring->out_idx) {
00054     ret = 1;
00055   } else {
00056     ring->buf[next_in_idx] = *frame;
00057     ret = 0;
00058     ring->in_idx = next_in_idx;
00059   }
00060   */
00061 
00062   next_in_idx = ring->in_idx;
00063   if (++next_in_idx == ring->buf_len) {next_in_idx = 0;}
00064   if (next_in_idx == ring->out_idx) {return 1;} //buffer full
00065 
00066   ring->buf[next_in_idx] = *frame;
00067   ring->in_idx = next_in_idx;
00068 
00069   return 0;
00070 }

Generated on Tue Jun 29 16:36:14 2010 by  doxygen 1.6.3