Loading...
Searching...
No Matches
chunked Ringbuffer

Implementation of a Ringbuffer to store chunks of data. More...

Detailed Description

Implementation of a Ringbuffer to store chunks of data.

Files

file  chunked_ringbuffer.h
 Chunked Ringbuffer.
 

Data Structures

struct  chunk_ringbuf_t
 A chunked ringbuffer. More...
 

Macros

#define CONFIG_CHUNK_NUM_MAX   (4)
 The maximum number of chunks that can be stored in a Chunked Ringbuffer.
 

Typedefs

typedef void(* crb_foreach_callback_t) (void *ctx, uint8_t *bytes, size_t len)
 Callback function for crb_chunk_foreach.
 

Functions

void crb_init (chunk_ringbuf_t *rb, void *buffer, size_t len)
 Initialize a Chunked Ringbuffer.
 
bool crb_end_chunk (chunk_ringbuf_t *rb, bool valid)
 Close the current chunk.
 
static bool crb_start_chunk (chunk_ringbuf_t *rb)
 Start a new chunk on the ringbuffer.
 
static bool crb_add_byte (chunk_ringbuf_t *rb, uint8_t b)
 Insert a byte into the current chunk.
 
bool crb_add_bytes (chunk_ringbuf_t *rb, const void *data, size_t len)
 Insert a number of bytes into the current chunk.
 
bool crb_add_chunk (chunk_ringbuf_t *rb, const void *data, size_t len)
 Add a complete chunk to the Ringbuffer.
 
bool crb_get_chunk_size (chunk_ringbuf_t *rb, size_t *len)
 Get the size of the first valid chunk.
 
bool crb_peek_bytes (chunk_ringbuf_t *rb, void *dst, size_t offset, size_t len)
 Get a number of bytes from the first valid chunk without consuming it.
 
bool crb_consume_chunk (chunk_ringbuf_t *rb, void *dst, size_t len)
 Remove a chunk from the valid chunk array.
 
bool crb_chunk_foreach (chunk_ringbuf_t *rb, crb_foreach_callback_t func, void *ctx)
 Execute a callback for each byte in the first valid chunk The callback function may be called twice if the chunk is non-continuous.
 

Macro Definition Documentation

◆ CONFIG_CHUNK_NUM_MAX

#define CONFIG_CHUNK_NUM_MAX   (4)

The maximum number of chunks that can be stored in a Chunked Ringbuffer.

Definition at line 39 of file chunked_ringbuffer.h.

Typedef Documentation

◆ crb_foreach_callback_t

typedef void(* crb_foreach_callback_t) (void *ctx, uint8_t *bytes, size_t len)

Callback function for crb_chunk_foreach.

Parameters
[in]ctxCallback context
[in]bytesChunk data
[in]lenLength of data

Definition at line 64 of file chunked_ringbuffer.h.

Function Documentation

◆ crb_add_byte()

static bool crb_add_byte ( chunk_ringbuf_t rb,
uint8_t  b 
)
inlinestatic

Insert a byte into the current chunk.

Note
This function is expected to be called in ISR context / with interrupts disabled.
Precondition
A new chunk has been started with crb_start_chunk
Parameters
[in]rbThe Ringbuffer to work on
[in]bThe byte to write
Returns
true If the byte could be written
false If the ringbuffer is full

Definition at line 138 of file chunked_ringbuffer.h.

◆ crb_add_bytes()

bool crb_add_bytes ( chunk_ringbuf_t rb,
const void *  data,
size_t  len 
)

Insert a number of bytes into the current chunk.

Note
This function is expected to be called in ISR context / with interrupts disabled.
Precondition
A new chunk has been started with crb_start_chunk
Parameters
[in]rbThe Ringbuffer to work on
[in]dataThe data to write
[in]lenSize of data
Returns
true If the bytes could be written
false If the ringbuffer is full

◆ crb_add_chunk()

bool crb_add_chunk ( chunk_ringbuf_t rb,
const void *  data,
size_t  len 
)

Add a complete chunk to the Ringbuffer.

Note
This function is expected to be called in ISR context / with interrupts disabled.

This is a convenience function that combines crb_start_chunk, crb_add_bytes and crb_end_chunk

Parameters
[in]rbThe Ringbuffer to work on
[in]dataThe data to write
[in]lenSize of data
Returns
true If the chunk could be added to the valid chunk array
false There was not enough space and the chunk was discarded

◆ crb_chunk_foreach()

bool crb_chunk_foreach ( chunk_ringbuf_t rb,
crb_foreach_callback_t  func,
void *  ctx 
)

Execute a callback for each byte in the first valid chunk The callback function may be called twice if the chunk is non-continuous.

This function will not consume the chunk.

Parameters
[in]rbThe Ringbuffer to work on
[in]funcThe function to call for each byte
[in]ctxOptional function argument
Returns
true If a valid chunk exits on which the function was executed
false If no valid chunk exists

◆ crb_consume_chunk()

bool crb_consume_chunk ( chunk_ringbuf_t rb,
void *  dst,
size_t  len 
)

Remove a chunk from the valid chunk array.

Parameters
[in]rbThe Ringbuffer to work on
[out]dstDestination where the chunk contents should be copied to. May be NULL, then the chunk is just discarded.
[in]lenMax number of bytes to read. If there are bytes left in the chunk beyond that, they will be discarded
Returns
true If a chunk was consumed
false If no valid chunk did exist

◆ crb_end_chunk()

bool crb_end_chunk ( chunk_ringbuf_t rb,
bool  valid 
)

Close the current chunk.

Note
This function is expected to be called in ISR context / with interrupts disabled.
Parameters
[in]rbThe Ringbuffer to work on
[in]validTrue if the chunk is valid and should be stored False if the current chunk should be discarded
Returns
true If the chunk could be stored in the valid chunk array
false If there is no more space in the valid chunk array

◆ crb_get_chunk_size()

bool crb_get_chunk_size ( chunk_ringbuf_t rb,
size_t *  len 
)

Get the size of the first valid chunk.

Parameters
[in]rbThe Ringbuffer to work on
[out]lenPointer to store the size of the first valid chunk
Returns
true If a valid chunk exists and size was written
false If no valid chunk exists

◆ crb_init()

void crb_init ( chunk_ringbuf_t rb,
void *  buffer,
size_t  len 
)

Initialize a Chunked Ringbuffer.

Parameters
[in]rbThe Ringbuffer to work on
bufferThe Ringbuffer work area
lenSize of the Ringbuffer work area

◆ crb_peek_bytes()

bool crb_peek_bytes ( chunk_ringbuf_t rb,
void *  dst,
size_t  offset,
size_t  len 
)

Get a number of bytes from the first valid chunk without consuming it.

Parameters
[in]rbThe Ringbuffer to work on
[out]dstDestination buffer
[in]offsetOffset to the start of the chunk
[in]lenNumber of bytes to read
Returns
true If the data could be read
false If no valid chunk exists or the bytes could not be read

◆ crb_start_chunk()

static bool crb_start_chunk ( chunk_ringbuf_t rb)
inlinestatic

Start a new chunk on the ringbuffer.

If an unfinished chunk already exists, it will be discarded.

Note
This function is expected to be called in ISR context / with interrupts disabled.
Parameters
[in]rbThe Ringbuffer to work on
Returns
true If a new chunk could be started
false If the ringbuffer is full

Definition at line 103 of file chunked_ringbuffer.h.