fsfw/datapoollocal/LocalPoolDataSetBase.cpp

287 lines
9.3 KiB
C++
Raw Normal View History

2020-10-01 12:05:24 +02:00
#include "LocalPoolDataSetBase.h"
#include "../datapoollocal/LocalDataPoolManager.h"
#include "../housekeeping/PeriodicHousekeepingHelper.h"
#include "../serialize/SerializeAdapter.h"
#include <cmath>
#include <cstring>
LocalPoolDataSetBase::LocalPoolDataSetBase(HasLocalDataPoolIF *hkOwner,
2020-12-03 13:00:04 +01:00
uint32_t setId, PoolVariableIF** registeredVariablesArray,
const size_t maxNumberOfVariables, bool periodicHandling):
2020-12-03 13:00:04 +01:00
PoolDataSetBase(registeredVariablesArray, maxNumberOfVariables) {
if(hkOwner == nullptr) {
// Configuration error.
sif::error << "LocalPoolDataSetBase::LocalPoolDataSetBase: Owner "
<< "invalid!" << std::endl;
return;
}
2020-10-01 12:05:24 +02:00
hkManager = hkOwner->getHkManagerHandle();
this->sid.objectId = hkOwner->getObjectId();
this->sid.ownerSetId = setId;
2020-12-03 13:00:04 +01:00
mutex = MutexFactory::instance()->createMutex();
2020-10-01 12:05:24 +02:00
// Data creators get a periodic helper for periodic HK data generation.
if(periodicHandling) {
2020-12-03 13:00:04 +01:00
periodicHelper = new PeriodicHousekeepingHelper(this);
2020-10-01 12:05:24 +02:00
}
}
LocalPoolDataSetBase::LocalPoolDataSetBase(sid_t sid,
PoolVariableIF** registeredVariablesArray,
const size_t maxNumberOfVariables):
PoolDataSetBase(registeredVariablesArray, maxNumberOfVariables) {
HasLocalDataPoolIF* hkOwner = objectManager->get<HasLocalDataPoolIF>(
sid.objectId);
if(hkOwner != nullptr) {
hkManager = hkOwner->getHkManagerHandle();
2020-10-01 12:05:24 +02:00
}
this->sid = sid;
2020-12-03 13:00:04 +01:00
mutex = MutexFactory::instance()->createMutex();
2020-10-01 12:05:24 +02:00
}
LocalPoolDataSetBase::~LocalPoolDataSetBase() {
}
ReturnValue_t LocalPoolDataSetBase::lockDataPool(uint32_t timeoutMs) {
if(hkManager != nullptr) {
MutexIF* mutex = hkManager->getMutexHandle();
return mutex->lockMutex(MutexIF::TimeoutType::WAITING, timeoutMs);
}
return HasReturnvaluesIF::RETURN_OK;
2020-10-01 12:05:24 +02:00
}
ReturnValue_t LocalPoolDataSetBase::serializeWithValidityBuffer(uint8_t **buffer,
size_t *size, size_t maxSize,
SerializeIF::Endianness streamEndianness) const {
ReturnValue_t result = HasReturnvaluesIF::RETURN_FAILED;
uint8_t validityMaskSize = std::ceil(static_cast<float>(fillCount)/8.0);
uint8_t validityMask[validityMaskSize];
uint8_t validBufferIndex = 0;
uint8_t validBufferIndexBit = 0;
for (uint16_t count = 0; count < fillCount; count++) {
if(registeredVariables[count]->isValid()) {
// set validity buffer here.
this->bitSetter(validityMask + validBufferIndex,
validBufferIndexBit);
if(validBufferIndexBit == 7) {
validBufferIndex ++;
validBufferIndexBit = 0;
}
else {
validBufferIndexBit ++;
}
}
result = registeredVariables[count]->serialize(buffer, size, maxSize,
streamEndianness);
if (result != HasReturnvaluesIF::RETURN_OK) {
return result;
}
}
2020-12-03 13:00:04 +01:00
if(*size + validityMaskSize > maxSize) {
return SerializeIF::BUFFER_TOO_SHORT;
}
2020-10-01 12:05:24 +02:00
// copy validity buffer to end
std::memcpy(*buffer, validityMask, validityMaskSize);
*size += validityMaskSize;
return result;
}
ReturnValue_t LocalPoolDataSetBase::deSerializeWithValidityBuffer(
const uint8_t **buffer, size_t *size,
SerializeIF::Endianness streamEndianness) {
ReturnValue_t result = HasReturnvaluesIF::RETURN_FAILED;
for (uint16_t count = 0; count < fillCount; count++) {
result = registeredVariables[count]->deSerialize(buffer, size,
2020-12-03 13:00:04 +01:00
streamEndianness);
2020-10-01 12:05:24 +02:00
if(result != HasReturnvaluesIF::RETURN_OK) {
return result;
}
}
2020-12-03 13:00:04 +01:00
if(*size < std::ceil(static_cast<float>(fillCount) / 8.0)) {
return SerializeIF::STREAM_TOO_SHORT;
}
2020-10-01 12:05:24 +02:00
uint8_t validBufferIndex = 0;
uint8_t validBufferIndexBit = 0;
for (uint16_t count = 0; count < fillCount; count++) {
// set validity buffer here.
bool nextVarValid = this->bitGetter(*buffer +
validBufferIndex, validBufferIndexBit);
registeredVariables[count]->setValid(nextVarValid);
if(validBufferIndexBit == 7) {
validBufferIndex ++;
validBufferIndexBit = 0;
}
else {
validBufferIndexBit ++;
}
}
return result;
}
2020-12-03 13:00:04 +01:00
2020-10-01 12:05:24 +02:00
ReturnValue_t LocalPoolDataSetBase::unlockDataPool() {
if(hkManager != nullptr) {
MutexIF* mutex = hkManager->getMutexHandle();
return mutex->unlockMutex();
}
return HasReturnvaluesIF::RETURN_OK;
2020-10-01 12:05:24 +02:00
}
ReturnValue_t LocalPoolDataSetBase::serializeLocalPoolIds(uint8_t** buffer,
size_t* size, size_t maxSize,SerializeIF::Endianness streamEndianness,
bool serializeFillCount) const {
// Serialize as uint8_t
uint8_t fillCount = this->fillCount;
if(serializeFillCount) {
SerializeAdapter::serialize(&fillCount, buffer, size, maxSize,
streamEndianness);
}
for (uint16_t count = 0; count < fillCount; count++) {
lp_id_t currentPoolId = registeredVariables[count]->getDataPoolId();
auto result = SerializeAdapter::serialize(&currentPoolId, buffer,
size, maxSize, streamEndianness);
if(result != HasReturnvaluesIF::RETURN_OK) {
sif::warning << "LocalDataSet::serializeLocalPoolIds: Serialization"
" error!" << std::endl;
return result;
}
}
return HasReturnvaluesIF::RETURN_OK;
}
uint8_t LocalPoolDataSetBase::getLocalPoolIdsSerializedSize(
bool serializeFillCount) const {
if(serializeFillCount) {
return fillCount * sizeof(lp_id_t) + sizeof(uint8_t);
}
else {
return fillCount * sizeof(lp_id_t);
}
}
size_t LocalPoolDataSetBase::getSerializedSize() const {
if(withValidityBuffer) {
uint8_t validityMaskSize = std::ceil(static_cast<float>(fillCount)/8.0);
return validityMaskSize + PoolDataSetBase::getSerializedSize();
}
else {
return PoolDataSetBase::getSerializedSize();
}
}
void LocalPoolDataSetBase::setValidityBufferGeneration(
bool withValidityBuffer) {
this->withValidityBuffer = withValidityBuffer;
}
ReturnValue_t LocalPoolDataSetBase::deSerialize(const uint8_t **buffer,
size_t *size, SerializeIF::Endianness streamEndianness) {
if(withValidityBuffer) {
return this->deSerializeWithValidityBuffer(buffer, size,
streamEndianness);
}
else {
return PoolDataSetBase::deSerialize(buffer, size, streamEndianness);
}
}
ReturnValue_t LocalPoolDataSetBase::serialize(uint8_t **buffer, size_t *size,
size_t maxSize, SerializeIF::Endianness streamEndianness) const {
if(withValidityBuffer) {
return this->serializeWithValidityBuffer(buffer, size,
maxSize, streamEndianness);
}
else {
return PoolDataSetBase::serialize(buffer, size, maxSize,
streamEndianness);
}
}
void LocalPoolDataSetBase::bitSetter(uint8_t* byte, uint8_t position) const {
if(position > 7) {
sif::debug << "Pool Raw Access: Bit setting invalid position"
2020-12-03 13:00:04 +01:00
<< std::endl;
2020-10-01 12:05:24 +02:00
return;
}
uint8_t shiftNumber = position + (7 - 2 * position);
*byte |= 1 << shiftNumber;
}
void LocalPoolDataSetBase::setDiagnostic(bool isDiagnostics) {
2020-12-03 13:00:04 +01:00
this->diagnostic = isDiagnostics;
2020-10-01 12:05:24 +02:00
}
bool LocalPoolDataSetBase::isDiagnostics() const {
2020-12-03 13:00:04 +01:00
return diagnostic;
2020-10-01 12:05:24 +02:00
}
void LocalPoolDataSetBase::setReportingEnabled(bool reportingEnabled) {
2020-12-03 13:00:04 +01:00
this->reportingEnabled = reportingEnabled;
2020-10-01 12:05:24 +02:00
}
bool LocalPoolDataSetBase::getReportingEnabled() const {
2020-12-03 13:00:04 +01:00
return reportingEnabled;
2020-10-01 12:05:24 +02:00
}
void LocalPoolDataSetBase::initializePeriodicHelper(
2020-12-03 13:00:04 +01:00
float collectionInterval, dur_millis_t minimumPeriodicInterval,
bool isDiagnostics, uint8_t nonDiagIntervalFactor) {
periodicHelper->initialize(collectionInterval, minimumPeriodicInterval,
isDiagnostics, nonDiagIntervalFactor);
2020-10-01 12:05:24 +02:00
}
void LocalPoolDataSetBase::setChanged(bool changed) {
2020-12-03 13:00:04 +01:00
// TODO: Make this configurable?
MutexHelper(mutex, MutexIF::TimeoutType::WAITING, 20);
this->changed = changed;
2020-10-01 12:05:24 +02:00
}
2020-12-03 13:00:04 +01:00
bool LocalPoolDataSetBase::hasChanged() const {
// TODO: Make this configurable?
MutexHelper(mutex, MutexIF::TimeoutType::WAITING, 20);
return changed;
2020-10-01 12:05:24 +02:00
}
sid_t LocalPoolDataSetBase::getSid() const {
2020-12-03 13:00:04 +01:00
return sid;
2020-10-01 12:05:24 +02:00
}
bool LocalPoolDataSetBase::bitGetter(const uint8_t* byte,
2020-12-03 13:00:04 +01:00
uint8_t position) const {
2020-10-01 12:05:24 +02:00
if(position > 7) {
sif::debug << "Pool Raw Access: Bit setting invalid position"
2020-12-03 13:00:04 +01:00
<< std::endl;
2020-10-01 12:05:24 +02:00
return false;
}
uint8_t shiftNumber = position + (7 - 2 * position);
return *byte & (1 << shiftNumber);
}
bool LocalPoolDataSetBase::isValid() const {
2020-12-03 13:00:04 +01:00
MutexHelper(mutex, MutexIF::TimeoutType::WAITING, 5);
2020-10-01 12:05:24 +02:00
return this->valid;
}
void LocalPoolDataSetBase::setValidity(bool valid, bool setEntriesRecursively) {
2020-12-03 13:00:04 +01:00
MutexHelper(mutex, MutexIF::TimeoutType::WAITING, 5);
if(setEntriesRecursively) {
for(size_t idx = 0; idx < this->getFillCount(); idx++) {
registeredVariables[idx] -> setValid(valid);
}
}
this->valid = valid;
2020-10-01 12:05:24 +02:00
}
void LocalPoolDataSetBase::setReadCommitProtectionBehaviour(
bool protectEveryReadCommit, uint32_t mutexTimeout) {
PoolDataSetBase::setReadCommitProtectionBehaviour(protectEveryReadCommit,
mutexTimeout);
}