fsfw/container/SharedRingBuffer.h

96 lines
3.0 KiB
C
Raw Permalink Normal View History

2020-09-04 15:11:53 +02:00
#ifndef FSFW_CONTAINER_SHAREDRINGBUFFER_H_
#define FSFW_CONTAINER_SHAREDRINGBUFFER_H_
2020-08-18 11:25:57 +02:00
2020-09-04 15:11:53 +02:00
#include "SimpleRingBuffer.h"
2020-09-22 16:24:40 +02:00
#include "DynamicFIFO.h"
2020-09-04 15:10:44 +02:00
#include "../ipc/MutexIF.h"
#include "../objectmanager/SystemObject.h"
#include "../timemanager/Clock.h"
2020-08-18 11:25:57 +02:00
/**
* @brief Ring buffer which can be shared among multiple objects
* @details
* This class offers a mutex to perform thread-safe operation on the ring
* buffer. It is still up to the developer to actually perform the lock
* and unlock operations.
*/
class SharedRingBuffer: public SystemObject,
public SimpleRingBuffer {
2020-08-18 11:25:57 +02:00
public:
/**
* This constructor allocates a new internal buffer with the supplied size.
* @param size
* @param overwriteOld
* If the ring buffer is overflowing at a write operartion, the oldest data
* will be overwritten.
*/
SharedRingBuffer(object_id_t objectId, const size_t size,
bool overwriteOld, size_t maxExcessBytes);
2021-04-11 21:54:48 +02:00
/**
* This constructor takes an external buffer with the specified size.
* @param buffer
* @param size
* @param overwriteOld
* If the ring buffer is overflowing at a write operartion, the oldest data
* will be overwritten.
*/
SharedRingBuffer(object_id_t objectId, uint8_t* buffer, const size_t size,
bool overwriteOld, size_t maxExcessBytes);
virtual~ SharedRingBuffer();
2020-08-18 11:25:57 +02:00
/**
* @brief This function can be used to add an optional FIFO to the class
* @details
* This FIFO will be allocated in the initialize function (and will
* have a fixed maximum size after that). It can be used to store
* values like packet sizes, for example for a shared ring buffer
* used by producer/consumer tasks.
*/
void setToUseReceiveSizeFIFO(size_t fifoDepth);
2020-09-22 16:22:37 +02:00
2021-04-11 21:54:48 +02:00
2020-08-18 11:25:57 +02:00
/**
* Unless a read-only constant value is read, all operations on the
* shared ring buffer should be protected by calling this function.
* @param timeoutType
* @param timeout
* @return
*/
virtual ReturnValue_t lockRingBufferMutex(MutexIF::TimeoutType timeoutType,
dur_millis_t timeout);
/**
* Any locked mutex also has to be unlocked, otherwise, access to the
* shared ring buffer will be blocked.
* @return
*/
virtual ReturnValue_t unlockRingBufferMutex();
2020-08-18 11:25:57 +02:00
/**
* The mutex handle can be accessed directly, for example to perform
2021-03-09 11:30:00 +01:00
* the lock with the #MutexGuard for a RAII compliant lock operation.
* @return
*/
MutexIF* getMutexHandle() const;
2020-09-22 16:22:37 +02:00
ReturnValue_t initialize() override;
2020-09-22 16:22:37 +02:00
/**
* If the shared ring buffer was configured to have a sizes FIFO, a handle
* to that FIFO can be retrieved with this function.
* Do not forget to protect access with a lock if required!
* @return
*/
DynamicFIFO<size_t>* getReceiveSizesFIFO();
2020-08-18 11:25:57 +02:00
private:
MutexIF* mutex = nullptr;
2020-09-22 16:22:37 +02:00
size_t fifoDepth = 0;
DynamicFIFO<size_t>* receiveSizesFIFO = nullptr;
2020-08-18 11:25:57 +02:00
};
2020-09-04 15:11:53 +02:00
#endif /* FSFW_CONTAINER_SHAREDRINGBUFFER_H_ */