fsfw/src/fsfw/container/SharedRingBuffer.h

92 lines
3.0 KiB
C
Raw 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-22 16:24:40 +02:00
#include "DynamicFIFO.h"
2022-02-02 10:29:30 +01:00
#include "SimpleRingBuffer.h"
2021-07-13 20:22:54 +02:00
#include "fsfw/ipc/MutexIF.h"
#include "fsfw/objectmanager/SystemObject.h"
#include "fsfw/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.
*/
2022-02-02 10:29:30 +01:00
class SharedRingBuffer : public SystemObject, public SimpleRingBuffer {
public:
/**
* This constructor allocates a new internal buffer with the supplied size.
* @param size
* @param overwriteOld
2024-03-01 10:50:55 +01:00
* If the ring buffer is overflowing at a write operation, the oldest data
2022-02-02 10:29:30 +01:00
* will be overwritten.
*/
SharedRingBuffer(object_id_t objectId, const size_t size, bool overwriteOld,
size_t maxExcessBytes);
/**
* This constructor takes an external buffer with the specified size.
* @param buffer
* @param size
* @param overwriteOld
2024-03-01 10:50:55 +01:00
* If the ring buffer is overflowing at a write operation, the oldest data
2022-02-02 10:29:30 +01:00
* will be overwritten.
*/
SharedRingBuffer(object_id_t objectId, uint8_t* buffer, const size_t size, bool overwriteOld,
size_t maxExcessBytes);
2021-04-11 21:54:48 +02:00
2022-02-02 10:29:30 +01:00
virtual ~SharedRingBuffer();
2020-08-18 11:25:57 +02:00
2022-02-02 10:29:30 +01: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
2022-02-02 10:29:30 +01: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();
2021-04-11 21:54:48 +02:00
2022-02-02 10:29:30 +01:00
/**
* The mutex handle can be accessed directly, for example to perform
* the lock with the #MutexGuard for a RAII compliant lock operation.
* @return
*/
MutexIF* getMutexHandle() const;
2020-08-18 11:25:57 +02:00
2022-02-02 10:29:30 +01:00
ReturnValue_t initialize() override;
2020-08-18 11:25:57 +02:00
2022-02-02 10:29:30 +01: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
*/
2024-03-21 16:10:45 +01:00
DynamicFIFO<size_t>* getReceiveSizesFifo();
2020-09-22 16:22:37 +02:00
2024-03-21 16:10:45 +01:00
ReturnValue_t fifoEmpty(bool& empty, MutexIF::TimeoutType timeoutType, uint32_t timeoutMs);
2020-09-22 16:22:37 +02:00
2022-02-02 10:29:30 +01:00
private:
MutexIF* mutex = nullptr;
2020-09-22 16:22:37 +02:00
2022-02-02 10:29:30 +01:00
size_t fifoDepth = 0;
2024-03-21 16:10:45 +01:00
DynamicFIFO<size_t>* receiveSizesFifo = nullptr;
2020-08-18 11:25:57 +02:00
};
2024-03-21 16:10:45 +01:00
#endif /* FSFW_CONTAINER_SHAREDRINGBUFFER_H_ */