fsfw/container/SimpleRingBuffer.h

120 lines
3.9 KiB
C
Raw Normal View History

#ifndef FRAMEWORK_CONTAINER_SIMPLERINGBUFFER_H_
#define FRAMEWORK_CONTAINER_SIMPLERINGBUFFER_H_
#include <framework/container/RingBufferBase.h>
2020-07-07 17:42:37 +02:00
#include <cstddef>
2019-10-29 19:31:18 +01:00
/**
* @brief Circular buffer implementation, useful for buffering
* into data streams.
* @details
* Note that the deleteData() has to be called to increment the read pointer.
* This class allocated dynamically, so
2020-01-12 14:18:12 +01:00
* @ingroup containers
2019-10-29 19:31:18 +01:00
*/
class SimpleRingBuffer: public RingBufferBase<> {
public:
/**
* This constructor allocates a new internal buffer with the supplied size.
2020-08-04 00:59:19 +02:00
*
* @param size
2020-08-04 00:59:19 +02:00
* @param overwriteOld If the ring buffer is overflowing at a write
* operation, the oldest data will be overwritten.
* @param maxExcessBytes These additional bytes will be allocated in addtion
* to the specified size to accomodate contiguous write operations
* with getFreeElement.
*
*/
2020-08-04 00:59:19 +02:00
SimpleRingBuffer(const size_t size, bool overwriteOld,
size_t maxExcessBytes = 0);
/**
* This constructor takes an external buffer with the specified size.
* @param buffer
* @param size
* @param overwriteOld
2020-07-09 14:26:15 +02:00
* If the ring buffer is overflowing at a write operartion, the oldest data
* will be overwritten.
2020-08-04 00:59:19 +02:00
* @param maxExcessBytes
* If the buffer can accomodate additional bytes for contigous write
* operations with getFreeElement, this is the maximum allowed additional
* size
*/
2020-08-04 00:59:19 +02:00
SimpleRingBuffer(uint8_t* buffer, const size_t size, bool overwriteOld,
size_t maxExcessBytes = 0);
virtual ~SimpleRingBuffer();
2019-10-29 19:31:18 +01:00
/**
2020-07-09 14:26:15 +02:00
* Write to circular buffer and increment write pointer by amount.
2019-10-29 19:31:18 +01:00
* @param data
* @param amount
2020-07-09 14:26:15 +02:00
* @return -@c RETURN_OK if write operation was successfull
* -@c RETURN_FAILED if
2019-10-29 19:31:18 +01:00
*/
2020-07-09 14:26:15 +02:00
ReturnValue_t writeData(const uint8_t* data, size_t amount);
2019-10-29 19:31:18 +01:00
2020-08-04 00:59:19 +02:00
/**
* Returns a pointer to a free element. If the remaining buffer is
* not large enough, the data will be written past the actual size
* and the amount of excess bytes will be cached.
* @param writePointer Pointer to a pointer which can be used to write
* contiguous blocks into the ring buffer
* @param amount
* @return
*/
ReturnValue_t getFreeElement(uint8_t** writePointer, size_t amount);
2020-08-04 01:07:59 +02:00
size_t getExcessBytes() const;
/**
* Helper functions which moves any excess bytes to the start
* of the ring buffer.
* @return
*/
void moveExcessBytesToStart();
2019-10-29 19:31:18 +01:00
/**
2020-07-09 14:26:15 +02:00
* Read from circular buffer at read pointer.
2019-10-29 19:31:18 +01:00
* @param data
* @param amount
2020-07-09 14:26:15 +02:00
* @param incrementReadPtr
* If this is set to true, the read pointer will be incremented.
* If readRemaining is set to true, the read pointer will be incremented
* accordingly.
2019-10-29 19:31:18 +01:00
* @param readRemaining
2020-07-09 14:26:15 +02:00
* If this is set to true, the data will be read even if the amount
* specified exceeds the read data available.
* @param trueAmount [out]
* If readRemaining was set to true, the true amount read will be assigned
* to the passed value.
2019-10-29 19:31:18 +01:00
* @return
2020-07-09 14:26:15 +02:00
* - @c RETURN_OK if data was read successfully
* - @c RETURN_FAILED if not enough data was available and readRemaining
* was set to false.
2019-10-29 19:31:18 +01:00
*/
2020-07-09 14:26:15 +02:00
ReturnValue_t readData(uint8_t* data, size_t amount,
bool incrementReadPtr = false, bool readRemaining = false,
size_t* trueAmount = nullptr);
2019-10-29 19:31:18 +01:00
/**
2020-07-09 14:26:15 +02:00
* Delete data by incrementing read pointer.
2019-10-29 19:31:18 +01:00
* @param amount
* @param deleteRemaining
2020-07-09 14:26:15 +02:00
* If the amount specified is larger than the remaing size to read and this
* is set to true, the remaining amount will be deleted as well
* @param trueAmount [out]
* If deleteRemaining was set to true, the amount deleted will be assigned
* to the passed value.
2019-10-29 19:31:18 +01:00
* @return
*/
2020-07-09 14:26:15 +02:00
ReturnValue_t deleteData(size_t amount, bool deleteRemaining = false,
size_t* trueAmount = nullptr);
private:
static const uint8_t READ_PTR = 0;
uint8_t* buffer = nullptr;
2020-08-04 02:00:00 +02:00
size_t maxExcessBytes;
2020-08-04 00:59:19 +02:00
size_t excessBytes = 0;
};
#endif /* FRAMEWORK_CONTAINER_SIMPLERINGBUFFER_H_ */