2020-07-09 14:26:15 +02:00
|
|
|
#include <framework/container/SharedRingBuffer.h>
|
|
|
|
#include <framework/ipc/MutexFactory.h>
|
|
|
|
#include <framework/ipc/MutexHelper.h>
|
|
|
|
|
2020-07-09 15:47:50 +02:00
|
|
|
SharedRingBuffer::SharedRingBuffer(object_id_t objectId, const size_t size,
|
2020-08-04 02:25:10 +02:00
|
|
|
bool overwriteOld, size_t maxExcessBytes, dur_millis_t mutexTimeout):
|
|
|
|
SystemObject(objectId), SimpleRingBuffer(size, overwriteOld,
|
|
|
|
maxExcessBytes), mutexTimeout(mutexTimeout) {
|
2020-07-09 14:26:15 +02:00
|
|
|
mutex = MutexFactory::instance()->createMutex();
|
|
|
|
}
|
|
|
|
|
2020-07-09 15:47:50 +02:00
|
|
|
SharedRingBuffer::SharedRingBuffer(object_id_t objectId, uint8_t *buffer,
|
2020-08-04 02:25:10 +02:00
|
|
|
const size_t size, bool overwriteOld, size_t maxExcessBytes,
|
|
|
|
dur_millis_t mutexTimeout):
|
|
|
|
SystemObject(objectId), SimpleRingBuffer(buffer, size, overwriteOld,
|
|
|
|
maxExcessBytes), mutexTimeout(mutexTimeout) {
|
2020-07-09 14:26:15 +02:00
|
|
|
mutex = MutexFactory::instance()->createMutex();
|
|
|
|
}
|
|
|
|
|
2020-08-04 02:25:10 +02:00
|
|
|
ReturnValue_t SharedRingBuffer::getFreeElementProtected(uint8_t** writePtr,
|
|
|
|
size_t amount) {
|
2020-08-07 22:15:03 +02:00
|
|
|
MutexHelper(mutex, MutexIF::TimeoutType::WAITING, mutexTimeout);
|
2020-08-04 02:25:10 +02:00
|
|
|
return SimpleRingBuffer::getFreeElement(writePtr,amount);
|
|
|
|
}
|
|
|
|
|
2020-07-09 14:26:15 +02:00
|
|
|
ReturnValue_t SharedRingBuffer::writeDataProtected(const uint8_t *data,
|
2020-07-09 15:47:50 +02:00
|
|
|
size_t amount) {
|
2020-08-07 22:15:03 +02:00
|
|
|
MutexHelper(mutex, MutexIF::TimeoutType::WAITING, mutexTimeout);
|
2020-07-09 15:47:50 +02:00
|
|
|
return SimpleRingBuffer::writeData(data,amount);
|
2020-07-09 14:26:15 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
ReturnValue_t SharedRingBuffer::readDataProtected(uint8_t *data, size_t amount,
|
2020-07-09 15:47:50 +02:00
|
|
|
bool incrementReadPtr, bool readRemaining,
|
2020-07-09 14:26:15 +02:00
|
|
|
size_t *trueAmount) {
|
2020-08-07 22:15:03 +02:00
|
|
|
MutexHelper(mutex, MutexIF::TimeoutType::WAITING, mutexTimeout);
|
2020-07-09 15:47:50 +02:00
|
|
|
return SimpleRingBuffer::readData(data,amount, incrementReadPtr,
|
|
|
|
readRemaining, trueAmount);
|
2020-07-09 14:26:15 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
ReturnValue_t SharedRingBuffer::deleteDataProtected(size_t amount,
|
2020-07-09 15:47:50 +02:00
|
|
|
bool deleteRemaining, size_t *trueAmount) {
|
2020-08-07 22:15:03 +02:00
|
|
|
MutexHelper(mutex, MutexIF::TimeoutType::WAITING, mutexTimeout);
|
2020-07-09 15:47:50 +02:00
|
|
|
return SimpleRingBuffer::deleteData(amount, deleteRemaining, trueAmount);
|
|
|
|
}
|
|
|
|
|
2020-08-04 11:47:47 +02:00
|
|
|
size_t SharedRingBuffer::getExcessBytes() const {
|
2020-08-07 22:15:03 +02:00
|
|
|
MutexHelper(mutex, MutexIF::TimeoutType::WAITING, mutexTimeout);
|
2020-08-04 11:47:47 +02:00
|
|
|
return SimpleRingBuffer::getExcessBytes();
|
|
|
|
}
|
|
|
|
|
|
|
|
void SharedRingBuffer::moveExcessBytesToStart() {
|
2020-08-07 22:15:03 +02:00
|
|
|
MutexHelper(mutex, MutexIF::TimeoutType::WAITING, mutexTimeout);
|
2020-08-04 11:47:47 +02:00
|
|
|
return SimpleRingBuffer::moveExcessBytesToStart();
|
|
|
|
}
|
|
|
|
|
2020-07-09 15:47:50 +02:00
|
|
|
size_t SharedRingBuffer::getAvailableReadDataProtected(uint8_t n) const {
|
2020-08-07 22:15:03 +02:00
|
|
|
MutexHelper(mutex, MutexIF::TimeoutType::WAITING, mutexTimeout);
|
2020-07-09 15:47:50 +02:00
|
|
|
return ((write + size) - read[n]) % size;
|
2020-07-09 14:26:15 +02:00
|
|
|
}
|