fsfw/datapool/PoolRawAccessHelper.cpp

184 lines
5.9 KiB
C++
Raw Normal View History

2019-12-24 01:41:04 +01:00
/**
* @file PoolRawAccessHelper.cpp
*
* @date 22.12.2019
2019-12-24 22:15:39 +01:00
* @author R. Mueller
2019-12-24 01:41:04 +01:00
*/
#include <framework/datapool/PoolRawAccessHelper.h>
2020-02-16 21:04:17 +01:00
#include <cmath>
#include <framework/datapool/DataSet.h>
2019-12-24 01:41:04 +01:00
PoolRawAccessHelper::PoolRawAccessHelper(uint32_t * poolIdBuffer_,
uint8_t numberOfParameters_):
poolIdBuffer(reinterpret_cast<uint8_t * >(poolIdBuffer_)),
numberOfParameters(numberOfParameters_), validBufferIndex(0),
validBufferIndexBit(1) {
2019-12-24 01:41:04 +01:00
}
PoolRawAccessHelper::~PoolRawAccessHelper() {
}
ReturnValue_t PoolRawAccessHelper::serialize(uint8_t **buffer, size_t *size,
const size_t max_size, bool bigEndian) {
SerializationArgs serializationArgs = {buffer, size, max_size, bigEndian};
2020-04-22 01:01:32 +02:00
ReturnValue_t result = RETURN_OK;
size_t remainingParametersSize = numberOfParameters * 4;
2019-12-24 01:41:04 +01:00
for(uint8_t count=0; count < numberOfParameters; count++) {
result = serializeCurrentPoolEntryIntoBuffer(serializationArgs,
&remainingParametersSize, false);
2019-12-24 22:15:39 +01:00
if(result != RETURN_OK) {
return result;
}
2019-12-24 01:41:04 +01:00
}
if(remainingParametersSize != 0) {
2020-04-23 20:04:48 +02:00
sif::debug << "Pool Raw Access: Remaining parameters size not 0 !" << std::endl;
result = RETURN_FAILED;
}
return result;
2019-12-24 01:41:04 +01:00
}
ReturnValue_t PoolRawAccessHelper::serializeWithValidityMask(uint8_t ** buffer,
size_t * size, const size_t max_size, bool bigEndian) {
2020-04-22 01:01:32 +02:00
ReturnValue_t result = RETURN_OK;
SerializationArgs argStruct = {buffer, size, max_size, bigEndian};
size_t remainingParametersSize = numberOfParameters * 4;
2020-02-16 21:04:17 +01:00
uint8_t validityMaskSize = ceil((float)numberOfParameters/8.0);
2019-12-24 22:15:39 +01:00
uint8_t validityMask[validityMaskSize];
memset(validityMask,0, validityMaskSize);
for(uint8_t count = 0; count < numberOfParameters; count++) {
result = serializeCurrentPoolEntryIntoBuffer(argStruct,
&remainingParametersSize,true,validityMask);
2019-12-24 22:15:39 +01:00
if (result != RETURN_OK) {
return result;
}
}
if(remainingParametersSize != 0) {
2020-04-23 20:04:48 +02:00
sif::debug << "Pool Raw Access: Remaining parameters size not 0 !" << std::endl;
result = RETURN_FAILED;
}
2020-02-16 21:04:17 +01:00
memcpy(*argStruct.buffer, validityMask, validityMaskSize);
2019-12-24 22:15:39 +01:00
*size += validityMaskSize;
validBufferIndex = 1;
validBufferIndexBit = 0;
return result;
2019-12-24 01:41:04 +01:00
}
ReturnValue_t PoolRawAccessHelper::serializeCurrentPoolEntryIntoBuffer(
SerializationArgs argStruct, size_t * remainingParameters,
bool withValidMask, uint8_t * validityMask) {
2019-12-24 01:41:04 +01:00
uint32_t currentPoolId;
// Deserialize current pool ID from pool ID buffer
ReturnValue_t result = AutoSerializeAdapter::deSerialize(&currentPoolId,
2020-04-30 15:42:33 +02:00
&poolIdBuffer,remainingParameters, false);
2019-12-24 01:41:04 +01:00
if(result != RETURN_OK) {
2020-04-23 20:04:48 +02:00
sif::debug << std::hex << "Pool Raw Access Helper: Error deSeralizing "
"pool IDs" << std::dec << std::endl;
2019-12-24 22:15:39 +01:00
return result;
2019-12-24 01:41:04 +01:00
}
result = handlePoolEntrySerialization(currentPoolId, argStruct,
withValidMask, validityMask);
return result;
}
ReturnValue_t PoolRawAccessHelper::handlePoolEntrySerialization(
uint32_t currentPoolId,SerializationArgs argStruct, bool withValidMask,
uint8_t * validityMask) {
ReturnValue_t result = RETURN_FAILED;
uint8_t arrayPosition = 0;
uint8_t counter = 0;
bool poolEntrySerialized = false;
//debug << "Pool Raw Access Helper: Handling Pool ID: "
// << std::hex << currentPoolId << std::endl;
while(not poolEntrySerialized) {
if(counter > DataSet::DATA_SET_MAX_SIZE) {
2020-04-23 20:04:48 +02:00
sif::error << "Pool Raw Access Helper: Config error, "
"max. number of possible data set variables exceeded"
<< std::endl;
return result;
}
counter ++;
DataSet currentDataSet = DataSet();
//debug << "Current array position: " << (int)arrayPosition << std::endl;
PoolRawAccess currentPoolRawAccess(currentPoolId,arrayPosition,
&currentDataSet,PoolVariableIF::VAR_READ);
result = currentDataSet.read();
if (result != RETURN_OK) {
2020-04-23 20:04:48 +02:00
sif::debug << std::hex << "Pool Raw Access Helper: Error reading raw "
"dataset with returncode 0x"
<< result << std::dec << std::endl;
return result;
}
result = checkRemainingSize(&currentPoolRawAccess, &poolEntrySerialized,
&arrayPosition);
if(result != RETURN_OK) {
2020-04-23 20:04:48 +02:00
sif::error << "Pool Raw Access Helper: Configuration Error at pool ID "
<< std::hex << currentPoolId
<< ". Size till end smaller than 0" << std::dec << std::endl;
return result;
}
// set valid mask bit if necessary
if(withValidMask) {
if(currentPoolRawAccess.isValid()) {
handleMaskModification(validityMask);
2019-12-24 22:15:39 +01:00
}
2020-02-16 21:04:17 +01:00
validBufferIndexBit ++;
2019-12-24 22:15:39 +01:00
}
result = currentDataSet.serialize(argStruct.buffer, argStruct.size,
argStruct.max_size, argStruct.bigEndian);
if (result != RETURN_OK) {
2020-04-23 20:04:48 +02:00
sif::debug << "Pool Raw Access Helper: Error serializing pool data with "
"ID 0x" << std::hex << currentPoolId << " into send buffer "
"with return code " << result << std::dec << std::endl;
return result;
}
2019-12-24 22:15:39 +01:00
}
return result;
}
2019-12-24 22:15:39 +01:00
ReturnValue_t PoolRawAccessHelper::checkRemainingSize(PoolRawAccess*
currentPoolRawAccess, bool * isSerialized, uint8_t * arrayPosition) {
int8_t remainingSize = currentPoolRawAccess->getSizeTillEnd() -
currentPoolRawAccess->getSizeOfType();
if(remainingSize == 0) {
*isSerialized = true;
}
else if(remainingSize > 0) {
*arrayPosition += 1;
}
else {
return RETURN_FAILED;
}
return RETURN_OK;
}
void PoolRawAccessHelper::handleMaskModification(uint8_t * validityMask) {
validityMask[validBufferIndex] =
bitSetter(validityMask[validBufferIndex], validBufferIndexBit, true);
if(validBufferIndexBit == 8) {
validBufferIndex ++;
validBufferIndexBit = 1;
2019-12-24 01:41:04 +01:00
}
}
uint8_t PoolRawAccessHelper::bitSetter(uint8_t byte, uint8_t position,
bool value) {
2019-12-24 01:41:04 +01:00
if(position < 1 or position > 8) {
2020-04-23 20:04:48 +02:00
sif::debug << "Pool Raw Access: Bit setting invalid position" << std::endl;
2019-12-24 01:41:04 +01:00
return byte;
}
uint8_t shiftNumber = position + (6 - 2 * (position - 1));
2020-02-16 21:04:17 +01:00
byte |= 1UL << shiftNumber;
2019-12-24 01:41:04 +01:00
return byte;
}