|
Comms CCF
This is a simple communication layer based on COBS, CBOR and FNV-1A
|
Buffer for queuing received bytes. More...
#include "types.hpp"#include "ndebug.hpp"#include <stddef.h>#include <stdint.h>#include <array>#include <bit>#include <optional>#include <type_traits>#include <utility>#include "debug_end.hpp"Go to the source code of this file.
Classes | |
| class | CircularBuffer< Value, Size, MaxPacketSize > |
| class | CircularBuffer< Value, Size, MaxPacketSize >::Iterator |
| class | CircularBuffer< Value, Size, MaxPacketSize >::Frame |
Buffer for queuing received bytes.
As data arrives in the interrupt of the driver, it needs to be queued for an application thread to process it. This class is a simple circular buffer that can support either bytes for a pre-allocated buffer, or pointers for a dynamically sized buffer.
Though because it includes the size of the data, it is optimised for storing data not pointers – as it would just waste half of the space for pointers as size of a pointer is known. This is done because even though we know the total readable size of the buffer, the buffer might contain multiple packets, and we want to know where each packet starts and ends.
The basis of the circular buffer is based on the Lamport Queue, the basic Lamport Queue uses two cursors, one for read and one for write, and a buffer which is a size of power of two (see XXX). It is a concurrent, single producer single consumer queue, which means there are some subtle considerations:
The modification we have done to the standard Lamport Queue is having another cursor which is always bounded by the read and write cursors, and is used to keep track of where the next packet is which we haven't notified the consumer of. This allows us to store the length of the packet at the start of the packet, and also to drop packets which cannot fit into the buffer.
This does mean that we need to know how big a packet can be (specifically, how many bytes to allocate for the packet size pointer), but this is fine for now.