fsfw/datapool/PoolDataSetBase.cpp

170 lines
5.1 KiB
C++
Raw Normal View History

#include "PoolDataSetBase.h"
#include "../serviceinterface/ServiceInterfaceStream.h"
2020-05-17 23:41:28 +02:00
2020-07-14 15:45:03 +02:00
PoolDataSetBase::PoolDataSetBase(PoolVariableIF** registeredVariablesArray,
const size_t maxFillCount):
registeredVariables(registeredVariablesArray),
maxFillCount(maxFillCount) {
2020-05-17 23:41:28 +02:00
}
2020-07-14 15:45:03 +02:00
PoolDataSetBase::~PoolDataSetBase() {}
2020-05-17 23:41:28 +02:00
2020-07-14 15:45:03 +02:00
ReturnValue_t PoolDataSetBase::registerVariable(
2020-05-17 23:41:28 +02:00
PoolVariableIF *variable) {
if (state != States::DATA_SET_UNINITIALISED) {
sif::error << "DataSet::registerVariable: "
"Call made in wrong position." << std::endl;
return DataSetIF::DATA_SET_UNINITIALISED;
}
if (variable == nullptr) {
sif::error << "DataSet::registerVariable: "
"Pool variable is nullptr." << std::endl;
return DataSetIF::POOL_VAR_NULL;
}
if (fillCount >= maxFillCount) {
2020-05-17 23:41:28 +02:00
sif::error << "DataSet::registerVariable: "
"DataSet is full." << std::endl;
return DataSetIF::DATA_SET_FULL;
}
registeredVariables[fillCount] = variable;
fillCount++;
return HasReturnvaluesIF::RETURN_OK;
}
2020-07-14 15:45:03 +02:00
ReturnValue_t PoolDataSetBase::read(uint32_t lockTimeout) {
2020-05-17 23:41:28 +02:00
ReturnValue_t result = HasReturnvaluesIF::RETURN_OK;
if (state == States::DATA_SET_UNINITIALISED) {
2020-06-05 20:28:06 +02:00
lockDataPool(lockTimeout);
2020-05-17 23:41:28 +02:00
for (uint16_t count = 0; count < fillCount; count++) {
2020-06-11 16:03:09 +02:00
result = readVariable(count);
2020-06-05 20:28:06 +02:00
if(result != RETURN_OK) {
break;
2020-05-17 23:41:28 +02:00
}
}
state = States::DATA_SET_WAS_READ;
unlockDataPool();
}
else {
sif::error << "DataSet::read(): "
2020-06-11 16:03:09 +02:00
"Call made in wrong position. Don't forget to commit"
" member datasets!" << std::endl;
2020-05-17 23:41:28 +02:00
result = SET_WAS_ALREADY_READ;
}
return result;
}
2020-07-14 15:45:03 +02:00
uint16_t PoolDataSetBase::getFillCount() const {
return fillCount;
}
2020-07-14 15:45:03 +02:00
ReturnValue_t PoolDataSetBase::readVariable(uint16_t count) {
2020-06-11 16:03:09 +02:00
ReturnValue_t result = HasReturnvaluesIF::RETURN_OK;
2020-06-05 20:28:06 +02:00
// These checks are often performed by the respective
// variable implementation too, but I guess a double check does not hurt.
if (registeredVariables[count]->getReadWriteMode() !=
PoolVariableIF::VAR_WRITE and
registeredVariables[count]->getDataPoolId()
!= PoolVariableIF::NO_PARAMETER)
{
result = registeredVariables[count]->readWithoutLock();
2020-06-11 16:03:09 +02:00
if(result != HasReturnvaluesIF::RETURN_OK) {
result = INVALID_PARAMETER_DEFINITION;
}
2020-06-05 20:28:06 +02:00
}
return result;
}
2020-07-14 15:45:03 +02:00
ReturnValue_t PoolDataSetBase::commit(uint32_t lockTimeout) {
2020-05-17 23:41:28 +02:00
if (state == States::DATA_SET_WAS_READ) {
2020-06-05 20:28:06 +02:00
handleAlreadyReadDatasetCommit(lockTimeout);
2020-05-17 23:41:28 +02:00
return HasReturnvaluesIF::RETURN_OK;
}
else {
2020-06-05 20:28:06 +02:00
return handleUnreadDatasetCommit(lockTimeout);
2020-05-17 23:41:28 +02:00
}
}
2020-07-14 15:45:03 +02:00
void PoolDataSetBase::handleAlreadyReadDatasetCommit(uint32_t lockTimeout) {
2020-06-05 20:28:06 +02:00
lockDataPool(lockTimeout);
2020-05-17 23:41:28 +02:00
for (uint16_t count = 0; count < fillCount; count++) {
if (registeredVariables[count]->getReadWriteMode()
!= PoolVariableIF::VAR_READ
&& registeredVariables[count]->getDataPoolId()
!= PoolVariableIF::NO_PARAMETER) {
2020-06-05 20:28:06 +02:00
registeredVariables[count]->commitWithoutLock();
2020-05-17 23:41:28 +02:00
}
}
state = States::DATA_SET_UNINITIALISED;
unlockDataPool();
}
2020-07-14 15:45:03 +02:00
ReturnValue_t PoolDataSetBase::handleUnreadDatasetCommit(uint32_t lockTimeout) {
2020-05-17 23:41:28 +02:00
ReturnValue_t result = HasReturnvaluesIF::RETURN_OK;
2020-06-05 20:28:06 +02:00
lockDataPool(lockTimeout);
2020-05-17 23:41:28 +02:00
for (uint16_t count = 0; count < fillCount; count++) {
if (registeredVariables[count]->getReadWriteMode()
== PoolVariableIF::VAR_WRITE
&& registeredVariables[count]->getDataPoolId()
!= PoolVariableIF::NO_PARAMETER) {
2020-06-05 20:28:06 +02:00
registeredVariables[count]->commitWithoutLock();
2020-05-17 23:41:28 +02:00
} else if (registeredVariables[count]->getDataPoolId()
!= PoolVariableIF::NO_PARAMETER) {
if (result != COMMITING_WITHOUT_READING) {
sif::error << "DataSet::commit(): commit-without-read call made "
"with non write-only variable." << std::endl;
result = COMMITING_WITHOUT_READING;
}
}
}
state = States::DATA_SET_UNINITIALISED;
unlockDataPool();
return result;
}
2020-06-05 20:28:06 +02:00
2020-07-14 15:45:03 +02:00
ReturnValue_t PoolDataSetBase::lockDataPool(uint32_t timeoutMs) {
2020-06-05 20:28:06 +02:00
return HasReturnvaluesIF::RETURN_OK;
}
2020-07-14 15:45:03 +02:00
ReturnValue_t PoolDataSetBase::unlockDataPool() {
2020-06-05 20:28:06 +02:00
return HasReturnvaluesIF::RETURN_OK;
}
2020-07-14 15:45:03 +02:00
ReturnValue_t PoolDataSetBase::serialize(uint8_t** buffer, size_t* size,
2020-07-01 14:17:55 +02:00
const size_t maxSize, SerializeIF::Endianness streamEndianness) const {
2020-05-17 23:41:28 +02:00
ReturnValue_t result = HasReturnvaluesIF::RETURN_FAILED;
for (uint16_t count = 0; count < fillCount; count++) {
result = registeredVariables[count]->serialize(buffer, size, maxSize,
2020-07-01 14:17:55 +02:00
streamEndianness);
2020-05-17 23:41:28 +02:00
if (result != HasReturnvaluesIF::RETURN_OK) {
return result;
}
}
return result;
}
2020-07-14 15:45:03 +02:00
ReturnValue_t PoolDataSetBase::deSerialize(const uint8_t** buffer, size_t* size,
2020-07-01 14:17:55 +02:00
SerializeIF::Endianness streamEndianness) {
2020-05-17 23:41:28 +02:00
ReturnValue_t result = HasReturnvaluesIF::RETURN_FAILED;
for (uint16_t count = 0; count < fillCount; count++) {
result = registeredVariables[count]->deSerialize(buffer, size,
2020-07-01 14:17:55 +02:00
streamEndianness);
2020-05-17 23:41:28 +02:00
if (result != HasReturnvaluesIF::RETURN_OK) {
return result;
}
}
return result;
}
2020-07-14 15:45:03 +02:00
size_t PoolDataSetBase::getSerializedSize() const {
2020-05-17 23:41:28 +02:00
uint32_t size = 0;
for (uint16_t count = 0; count < fillCount; count++) {
size += registeredVariables[count]->getSerializedSize();
}
return size;
}
2020-07-14 15:45:03 +02:00
void PoolDataSetBase::setContainer(PoolVariableIF **variablesContainer) {
this->registeredVariables = variablesContainer;
}