OpenLcbCLib 1.0 Alpha
OpenSource C Library to create OpenLcb/Lcc Nodes
Loading...
Searching...
No Matches
can_buffer_fifo.c File Reference

Circular FIFO queue for can_msg_t pointers. More...

Data Structures

struct  can_fifo_t
 Internal circular buffer for queuing can_msg_t pointers. More...
 

Functions

void CanBufferFifo_initialize (void)
 Clears all FIFO slots and resets head and tail to zero.
 
bool CanBufferFifo_push (can_msg_t *new_msg)
 Pushes a can_msg_t pointer onto the tail of the FIFO.
 
can_msg_tCanBufferFifo_pop (void)
 Removes and returns the oldest can_msg_t from the FIFO.
 
uint8_t CanBufferFifo_is_empty (void)
 Returns non-zero if the FIFO is empty, zero if messages are present.
 
uint16_t CanBufferFifo_get_allocated_count (void)
 Returns the number of can_msg_t pointers currently in the FIFO.
 

Detailed Description

Circular FIFO queue for can_msg_t pointers.

Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:

  • Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.
  • Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution.

THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

Uses one extra slot (USER_DEFINED_CAN_MSG_BUFFER_DEPTH + 1) so that head == tail always means empty without needing a separate counter.

Author
Jim Kueneman
Date
4 Mar 2026

Function Documentation

◆ CanBufferFifo_initialize()

void CanBufferFifo_initialize ( void )

Clears all FIFO slots and resets head and tail to zero.

Algorithm:

  1. Set all LEN_CAN_FIFO_BUFFER pointer slots to NULL.
  2. Reset head and tail indices to zero.

◆ CanBufferFifo_push()

bool CanBufferFifo_push ( can_msg_t * new_msg)

Pushes a can_msg_t pointer onto the tail of the FIFO.

Algorithm:

  1. Compute next head position with wraparound.
  2. If next != tail (not full): store pointer, advance head, return true.
  3. Otherwise return false (FIFO full).
* @param new_msg Pointer to an allocated @ref can_msg_t.
* 
Returns
true on success, false if the FIFO is full.

◆ CanBufferFifo_pop()

can_msg_t * CanBufferFifo_pop ( void )

Removes and returns the oldest can_msg_t from the FIFO.

Algorithm:

  1. If head != tail (not empty): save pointer at tail, clear slot, advance tail with wraparound, return saved pointer.
  2. Otherwise return NULL (FIFO empty).
Returns
Pointer to the oldest can_msg_t, or NULL if the FIFO is empty.

◆ CanBufferFifo_is_empty()

uint8_t CanBufferFifo_is_empty ( void )

Returns non-zero if the FIFO is empty, zero if messages are present.

◆ CanBufferFifo_get_allocated_count()

uint16_t CanBufferFifo_get_allocated_count ( void )

Returns the number of can_msg_t pointers currently in the FIFO.

Handles the wraparound case where tail > head.

Returns
Current FIFO occupancy count.

Copyright (c) 2026 Jim Kueneman all rights reserved. See the License