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
|
|
|
*/
|
|
|
|
|
2020-08-18 13:09:15 +02:00
|
|
|
#include "../datapool/PoolRawAccessHelper.h"
|
|
|
|
#include "../datapoolglob/GlobalDataSet.h"
|
|
|
|
#include "../serialize/SerializeAdapter.h"
|
|
|
|
#include "../serviceinterface/ServiceInterfaceStream.h"
|
2020-02-16 21:04:17 +01:00
|
|
|
|
|
|
|
#include <cmath>
|
2020-05-17 23:41:28 +02:00
|
|
|
#include <cstring>
|
2019-12-24 01:41:04 +01:00
|
|
|
|
2020-01-09 19:04:33 +01:00
|
|
|
PoolRawAccessHelper::PoolRawAccessHelper(uint32_t * poolIdBuffer_,
|
2019-12-26 16:44:50 +01:00
|
|
|
uint8_t numberOfParameters_):
|
2020-01-09 19:04:33 +01:00
|
|
|
poolIdBuffer(reinterpret_cast<uint8_t * >(poolIdBuffer_)),
|
2020-04-05 17:58:39 +02:00
|
|
|
numberOfParameters(numberOfParameters_), validBufferIndex(0),
|
|
|
|
validBufferIndexBit(1) {
|
2019-12-24 01:41:04 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
PoolRawAccessHelper::~PoolRawAccessHelper() {
|
|
|
|
}
|
|
|
|
|
2020-04-05 17:58:39 +02:00
|
|
|
ReturnValue_t PoolRawAccessHelper::serialize(uint8_t **buffer, size_t *size,
|
2020-07-01 14:17:55 +02:00
|
|
|
const size_t max_size, SerializeIF::Endianness streamEndianness) {
|
|
|
|
SerializationArgs serializationArgs = {buffer, size, max_size,
|
|
|
|
streamEndianness};
|
2020-04-22 01:01:32 +02:00
|
|
|
ReturnValue_t result = RETURN_OK;
|
2020-04-15 15:53:46 +02:00
|
|
|
size_t remainingParametersSize = numberOfParameters * 4;
|
2019-12-24 01:41:04 +01:00
|
|
|
for(uint8_t count=0; count < numberOfParameters; count++) {
|
2019-12-29 01:59:02 +01:00
|
|
|
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
|
|
|
}
|
2019-12-26 16:44:50 +01:00
|
|
|
if(remainingParametersSize != 0) {
|
2020-04-30 21:37:02 +02:00
|
|
|
sif::debug << "PoolRawAccessHelper: "
|
|
|
|
"Remaining parameters size not 0 !" << std::endl;
|
2019-12-26 16:44:50 +01:00
|
|
|
result = RETURN_FAILED;
|
|
|
|
}
|
|
|
|
return result;
|
2019-12-24 01:41:04 +01:00
|
|
|
}
|
|
|
|
|
2020-04-05 17:58:39 +02:00
|
|
|
ReturnValue_t PoolRawAccessHelper::serializeWithValidityMask(uint8_t ** buffer,
|
2020-07-01 14:17:55 +02:00
|
|
|
size_t * size, const size_t max_size,
|
|
|
|
SerializeIF::Endianness streamEndianness) {
|
2020-04-22 01:01:32 +02:00
|
|
|
ReturnValue_t result = RETURN_OK;
|
2020-07-01 14:17:55 +02:00
|
|
|
SerializationArgs argStruct = {buffer, size, max_size, streamEndianness};
|
2020-04-15 15:53:46 +02:00
|
|
|
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);
|
2019-12-26 16:44:50 +01:00
|
|
|
for(uint8_t count = 0; count < numberOfParameters; count++) {
|
2019-12-29 01:59:02 +01:00
|
|
|
result = serializeCurrentPoolEntryIntoBuffer(argStruct,
|
|
|
|
&remainingParametersSize,true,validityMask);
|
2019-12-24 22:15:39 +01:00
|
|
|
if (result != RETURN_OK) {
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
}
|
2019-12-26 16:44:50 +01:00
|
|
|
if(remainingParametersSize != 0) {
|
2020-04-30 21:37:02 +02:00
|
|
|
sif::debug << "PoolRawAccessHelper: Remaining "
|
|
|
|
"parameters size not 0 !" << std::endl;
|
2019-12-26 16:44:50 +01:00
|
|
|
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;
|
2019-12-26 16:44:50 +01:00
|
|
|
return result;
|
2019-12-24 01:41:04 +01:00
|
|
|
}
|
|
|
|
|
2020-04-15 15:53:46 +02: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
|
2020-07-01 14:17:55 +02:00
|
|
|
ReturnValue_t result = SerializeAdapter::deSerialize(¤tPoolId,
|
|
|
|
&poolIdBuffer,remainingParameters, SerializeIF::Endianness::MACHINE);
|
2019-12-24 01:41:04 +01:00
|
|
|
if(result != RETURN_OK) {
|
2020-04-30 21:37:02 +02:00
|
|
|
sif::debug << std::hex << "PoolRawAccessHelper: Error deSeralizing "
|
2020-04-15 15:53:46 +02:00
|
|
|
"pool IDs" << std::dec << std::endl;
|
2019-12-24 22:15:39 +01:00
|
|
|
return result;
|
2019-12-24 01:41:04 +01:00
|
|
|
}
|
2020-04-15 15:53:46 +02:00
|
|
|
result = handlePoolEntrySerialization(currentPoolId, argStruct,
|
|
|
|
withValidMask, validityMask);
|
2019-12-29 01:59:02 +01:00
|
|
|
return result;
|
|
|
|
}
|
2019-12-26 16:44:50 +01:00
|
|
|
|
2020-04-15 15:53:46 +02:00
|
|
|
ReturnValue_t PoolRawAccessHelper::handlePoolEntrySerialization(
|
|
|
|
uint32_t currentPoolId,SerializationArgs argStruct, bool withValidMask,
|
|
|
|
uint8_t * validityMask) {
|
2020-01-10 00:57:09 +01:00
|
|
|
ReturnValue_t result = RETURN_FAILED;
|
2019-12-29 01:59:02 +01:00
|
|
|
uint8_t arrayPosition = 0;
|
2020-01-10 00:57:09 +01:00
|
|
|
uint8_t counter = 0;
|
2019-12-29 01:59:02 +01:00
|
|
|
bool poolEntrySerialized = false;
|
2020-04-15 15:53:46 +02:00
|
|
|
//debug << "Pool Raw Access Helper: Handling Pool ID: "
|
|
|
|
// << std::hex << currentPoolId << std::endl;
|
2019-12-29 01:59:02 +01:00
|
|
|
while(not poolEntrySerialized) {
|
2020-01-10 00:57:09 +01:00
|
|
|
|
2020-05-17 01:17:11 +02:00
|
|
|
if(counter > GlobDataSet::DATA_SET_MAX_SIZE) {
|
2020-04-30 21:37:02 +02:00
|
|
|
sif::error << "PoolRawAccessHelper: Config error, "
|
2020-04-15 15:53:46 +02:00
|
|
|
"max. number of possible data set variables exceeded"
|
|
|
|
<< std::endl;
|
2020-01-10 00:57:09 +01:00
|
|
|
return result;
|
|
|
|
}
|
|
|
|
counter ++;
|
|
|
|
|
2020-05-17 01:17:11 +02:00
|
|
|
GlobDataSet currentDataSet;
|
2020-01-14 01:39:47 +01:00
|
|
|
//debug << "Current array position: " << (int)arrayPosition << std::endl;
|
2020-07-01 14:17:55 +02:00
|
|
|
PoolRawAccess currentPoolRawAccess(currentPoolId, arrayPosition,
|
|
|
|
¤tDataSet, PoolVariableIF::VAR_READ);
|
2020-01-10 00:57:09 +01:00
|
|
|
|
2019-12-29 01:59:02 +01:00
|
|
|
result = currentDataSet.read();
|
|
|
|
if (result != RETURN_OK) {
|
2020-04-30 21:37:02 +02:00
|
|
|
sif::debug << std::hex << "PoolRawAccessHelper: Error reading raw "
|
|
|
|
"dataset with returncode 0x" << result << std::dec << std::endl;
|
2019-12-29 01:59:02 +01:00
|
|
|
return result;
|
|
|
|
}
|
2020-01-10 00:57:09 +01:00
|
|
|
|
2020-04-15 15:53:46 +02:00
|
|
|
result = checkRemainingSize(¤tPoolRawAccess, &poolEntrySerialized,
|
|
|
|
&arrayPosition);
|
2020-01-10 00:57:09 +01:00
|
|
|
if(result != RETURN_OK) {
|
2020-04-23 20:04:48 +02:00
|
|
|
sif::error << "Pool Raw Access Helper: Configuration Error at pool ID "
|
2020-04-15 15:53:46 +02:00
|
|
|
<< std::hex << currentPoolId
|
2020-01-10 00:57:09 +01:00
|
|
|
<< ". Size till end smaller than 0" << std::dec << std::endl;
|
2019-12-29 01:59:02 +01:00
|
|
|
return result;
|
|
|
|
}
|
2020-01-10 00:57:09 +01:00
|
|
|
|
2019-12-29 01:59:02 +01:00
|
|
|
// 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
|
|
|
}
|
2020-01-10 00:57:09 +01:00
|
|
|
|
2019-12-29 01:59:02 +01:00
|
|
|
result = currentDataSet.serialize(argStruct.buffer, argStruct.size,
|
2020-07-01 14:17:55 +02:00
|
|
|
argStruct.max_size, argStruct.streamEndianness);
|
2019-12-29 01:59:02 +01:00
|
|
|
if (result != RETURN_OK) {
|
2020-04-23 20:04:48 +02:00
|
|
|
sif::debug << "Pool Raw Access Helper: Error serializing pool data with "
|
2020-04-15 15:53:46 +02:00
|
|
|
"ID 0x" << std::hex << currentPoolId << " into send buffer "
|
|
|
|
"with return code " << result << std::dec << std::endl;
|
2019-12-29 01:59:02 +01:00
|
|
|
return result;
|
|
|
|
}
|
2020-01-10 00:57:09 +01:00
|
|
|
|
2019-12-24 22:15:39 +01:00
|
|
|
}
|
2019-12-29 01:59:02 +01:00
|
|
|
return result;
|
|
|
|
}
|
2019-12-24 22:15:39 +01:00
|
|
|
|
2020-04-15 15:53:46 +02:00
|
|
|
ReturnValue_t PoolRawAccessHelper::checkRemainingSize(PoolRawAccess*
|
|
|
|
currentPoolRawAccess, bool * isSerialized, uint8_t * arrayPosition) {
|
|
|
|
int8_t remainingSize = currentPoolRawAccess->getSizeTillEnd() -
|
|
|
|
currentPoolRawAccess->getSizeOfType();
|
2020-01-10 00:57:09 +01:00
|
|
|
if(remainingSize == 0) {
|
|
|
|
*isSerialized = true;
|
|
|
|
}
|
|
|
|
else if(remainingSize > 0) {
|
2020-01-14 00:49:09 +01:00
|
|
|
*arrayPosition += 1;
|
2020-01-10 00:57:09 +01:00
|
|
|
}
|
|
|
|
else {
|
|
|
|
return RETURN_FAILED;
|
|
|
|
}
|
|
|
|
return RETURN_OK;
|
|
|
|
}
|
|
|
|
|
2019-12-29 01:59:02 +01:00
|
|
|
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
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-04-15 15:53:46 +02: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;
|
|
|
|
}
|