Reworked PoolRawAccess to use EndianConverter instead of converting by itself

This commit is contained in:
Ulrich Mohr 2020-07-03 15:47:05 +02:00
parent 89accf8940
commit 45430e8586
2 changed files with 59 additions and 75 deletions

View File

@ -2,12 +2,13 @@
#include <framework/datapool/PoolEntryIF.h> #include <framework/datapool/PoolEntryIF.h>
#include <framework/datapool/PoolRawAccess.h> #include <framework/datapool/PoolRawAccess.h>
#include <framework/serviceinterface/ServiceInterfaceStream.h> #include <framework/serviceinterface/ServiceInterfaceStream.h>
#include <framework/osal/Endiness.h> #include <framework/serialize/EndianConverter.h>
PoolRawAccess::PoolRawAccess(uint32_t set_id, uint8_t setArrayEntry, PoolRawAccess::PoolRawAccess(uint32_t set_id, uint8_t setArrayEntry,
DataSetIF* data_set, ReadWriteMode_t setReadWriteMode) : DataSetIF *data_set, ReadWriteMode_t setReadWriteMode) :
dataPoolId(set_id), arrayEntry(setArrayEntry), valid(false), type(Type::UNKNOWN_TYPE), typeSize( dataPoolId(set_id), arrayEntry(setArrayEntry), valid(false), type(
0), arraySize(0), sizeTillEnd(0), readWriteMode(setReadWriteMode) { Type::UNKNOWN_TYPE), typeSize(0), arraySize(0), sizeTillEnd(0), readWriteMode(
setReadWriteMode) {
memset(value, 0, sizeof(value)); memset(value, 0, sizeof(value));
if (data_set != NULL) { if (data_set != NULL) {
data_set->registerVariable(this); data_set->registerVariable(this);
@ -19,7 +20,7 @@ PoolRawAccess::~PoolRawAccess() {
} }
ReturnValue_t PoolRawAccess::read() { ReturnValue_t PoolRawAccess::read() {
PoolEntryIF* read_out = ::dataPool.getRawData(dataPoolId); PoolEntryIF *read_out = ::dataPool.getRawData(dataPoolId);
if (read_out != NULL) { if (read_out != NULL) {
valid = read_out->getValid(); valid = read_out->getValid();
if (read_out->getSize() > arrayEntry) { if (read_out->getSize() > arrayEntry) {
@ -29,7 +30,7 @@ ReturnValue_t PoolRawAccess::read() {
if (typeSize <= sizeof(value)) { if (typeSize <= sizeof(value)) {
uint16_t arrayPosition = arrayEntry * typeSize; uint16_t arrayPosition = arrayEntry * typeSize;
sizeTillEnd = read_out->getByteSize() - arrayPosition; sizeTillEnd = read_out->getByteSize() - arrayPosition;
uint8_t* ptr = uint8_t *ptr =
&((uint8_t*) read_out->getRawData())[arrayPosition]; &((uint8_t*) read_out->getRawData())[arrayPosition];
memcpy(value, ptr, typeSize); memcpy(value, ptr, typeSize);
return HasReturnvaluesIF::RETURN_OK; return HasReturnvaluesIF::RETURN_OK;
@ -42,8 +43,8 @@ ReturnValue_t PoolRawAccess::read() {
} else { } else {
//Error entry does not exist. //Error entry does not exist.
} }
sif::error << "PoolRawAccess: read of DP Variable 0x" << std::hex << dataPoolId sif::error << "PoolRawAccess: read of DP Variable 0x" << std::hex
<< std::dec << " failed." << std::endl; << dataPoolId << std::dec << " failed." << std::endl;
valid = INVALID; valid = INVALID;
typeSize = 0; typeSize = 0;
sizeTillEnd = 0; sizeTillEnd = 0;
@ -52,11 +53,11 @@ ReturnValue_t PoolRawAccess::read() {
} }
ReturnValue_t PoolRawAccess::commit() { ReturnValue_t PoolRawAccess::commit() {
PoolEntryIF* write_back = ::dataPool.getRawData(dataPoolId); PoolEntryIF *write_back = ::dataPool.getRawData(dataPoolId);
if ((write_back != NULL) && (readWriteMode != VAR_READ)) { if ((write_back != NULL) && (readWriteMode != VAR_READ)) {
write_back->setValid(valid); write_back->setValid(valid);
uint8_t array_position = arrayEntry * typeSize; uint8_t array_position = arrayEntry * typeSize;
uint8_t* ptr = &((uint8_t*) write_back->getRawData())[array_position]; uint8_t *ptr = &((uint8_t*) write_back->getRawData())[array_position];
memcpy(ptr, value, typeSize); memcpy(ptr, value, typeSize);
return HasReturnvaluesIF::RETURN_OK; return HasReturnvaluesIF::RETURN_OK;
} else { } else {
@ -68,23 +69,17 @@ uint8_t* PoolRawAccess::getEntry() {
return value; return value;
} }
ReturnValue_t PoolRawAccess::getEntryEndianSafe(uint8_t* buffer, ReturnValue_t PoolRawAccess::getEntryEndianSafe(uint8_t *buffer,
uint32_t* writtenBytes, uint32_t maxSize) { size_t *writtenBytes, size_t maxSize) {
uint8_t* data_ptr = getEntry(); uint8_t *data_ptr = getEntry();
// debug << "PoolRawAccess::getEntry: Array position: " << index * size_of_type << " Size of T: " << (int)size_of_type << " ByteSize: " << byte_size << " Position: " << *size << std::endl; // debug << "PoolRawAccess::getEntry: Array position: " << index * size_of_type << " Size of T: " << (int)size_of_type << " ByteSize: " << byte_size << " Position: " << *size << std::endl;
if (typeSize == 0) if (typeSize == 0) {
return DATA_POOL_ACCESS_FAILED; return DATA_POOL_ACCESS_FAILED;
if (typeSize > maxSize)
return INCORRECT_SIZE;
#ifndef BYTE_ORDER_SYSTEM
#error BYTE_ORDER_SYSTEM not defined
#elif BYTE_ORDER_SYSTEM == LITTLE_ENDIAN
for (uint8_t count = 0; count < typeSize; count++) {
buffer[count] = data_ptr[typeSize - count - 1];
} }
#elif BYTE_ORDER_SYSTEM == BIG_ENDIAN if (typeSize > maxSize) {
memcpy(buffer, data_ptr, typeSize); return INCORRECT_SIZE;
#endif }
EndianConverter::convertBigEndian(buffer, data_ptr, typeSize);
*writtenBytes = typeSize; *writtenBytes = typeSize;
return HasReturnvaluesIF::RETURN_OK; return HasReturnvaluesIF::RETURN_OK;
} }
@ -93,11 +88,11 @@ Type PoolRawAccess::getType() {
return type; return type;
} }
uint8_t PoolRawAccess::getSizeOfType() { size_t PoolRawAccess::getSizeOfType() {
return typeSize; return typeSize;
} }
uint8_t PoolRawAccess::getArraySize(){ size_t PoolRawAccess::getArraySize() {
return arraySize; return arraySize;
} }
@ -109,21 +104,14 @@ PoolVariableIF::ReadWriteMode_t PoolRawAccess::getReadWriteMode() const {
return readWriteMode; return readWriteMode;
} }
ReturnValue_t PoolRawAccess::setEntryFromBigEndian(const uint8_t* buffer, ReturnValue_t PoolRawAccess::setEntryFromBigEndian(const uint8_t *buffer,
uint32_t setSize) { size_t setSize) {
if (typeSize == setSize) { if (typeSize == setSize) {
#ifndef BYTE_ORDER_SYSTEM EndianConverter::convertBigEndian(value, buffer, typeSize);
#error BYTE_ORDER_SYSTEM not defined
#elif BYTE_ORDER_SYSTEM == LITTLE_ENDIAN
for (uint8_t count = 0; count < typeSize; count++) {
value[count] = buffer[typeSize - count - 1];
}
#elif BYTE_ORDER_SYSTEM == BIG_ENDIAN
memcpy(value, buffer, typeSize);
#endif
return HasReturnvaluesIF::RETURN_OK; return HasReturnvaluesIF::RETURN_OK;
} else { } else {
sif::error << "PoolRawAccess::setEntryFromBigEndian: Illegal sizes: Internal" sif::error
<< "PoolRawAccess::setEntryFromBigEndian: Illegal sizes: Internal"
<< (uint32_t) typeSize << ", Requested: " << setSize << (uint32_t) typeSize << ", Requested: " << setSize
<< std::endl; << std::endl;
return INCORRECT_SIZE; return INCORRECT_SIZE;
@ -141,27 +129,24 @@ void PoolRawAccess::setValid(uint8_t valid) {
this->valid = valid; this->valid = valid;
} }
uint16_t PoolRawAccess::getSizeTillEnd() const { size_t PoolRawAccess::getSizeTillEnd() const {
return sizeTillEnd; return sizeTillEnd;
} }
ReturnValue_t PoolRawAccess::serialize(uint8_t** buffer, size_t* size, ReturnValue_t PoolRawAccess::serialize(uint8_t **buffer, size_t *size,
size_t maxSize, Endianness streamEndianness) const { size_t maxSize, Endianness streamEndianness) const {
//TODO integer overflow
if (typeSize + *size <= maxSize) { if (typeSize + *size <= maxSize) {
#warning use endian swapper switch (streamEndianness) {
if (1) { case (Endianness::BIG):
#ifndef BYTE_ORDER_SYSTEM EndianConverter::convertBigEndian(*buffer, value, typeSize);
#error BYTE_ORDER_SYSTEM not defined break;
#elif BYTE_ORDER_SYSTEM == LITTLE_ENDIAN case (Endianness::LITTLE):
for (uint8_t count = 0; count < typeSize; count++) { EndianConverter::convertLittleEndian(*buffer, value, typeSize);
(*buffer)[count] = value[typeSize - count - 1]; break;
} default:
#elif BYTE_ORDER_SYSTEM == BIG_ENDIAN case (Endianness::MACHINE):
memcpy(*buffer, value, typeSize);
#endif
} else {
memcpy(*buffer, value, typeSize); memcpy(*buffer, value, typeSize);
break;
} }
*size += typeSize; *size += typeSize;
(*buffer) += typeSize; (*buffer) += typeSize;
@ -175,24 +160,23 @@ size_t PoolRawAccess::getSerializedSize() const {
return typeSize; return typeSize;
} }
ReturnValue_t PoolRawAccess::deSerialize(const uint8_t** buffer, size_t* size, ReturnValue_t PoolRawAccess::deSerialize(const uint8_t **buffer, size_t *size,
Endianness streamEndianness) { Endianness streamEndianness) {
if (*size >= typeSize) { if (*size >= typeSize) {
*size -= typeSize; switch (streamEndianness) {
if (1) { case (Endianness::BIG):
#ifndef BYTE_ORDER_SYSTEM EndianConverter::convertBigEndian(value, *buffer, typeSize);
#error BYTE_ORDER_SYSTEM not defined break;
#elif BYTE_ORDER_SYSTEM == LITTLE_ENDIAN case (Endianness::LITTLE):
for (uint8_t count = 0; count < typeSize; count++) { EndianConverter::convertLittleEndian(value, *buffer, typeSize);
value[count] = (*buffer)[typeSize - count - 1]; break;
} default:
#elif BYTE_ORDER_SYSTEM == BIG_ENDIAN case (Endianness::MACHINE):
memcpy(value, *buffer, typeSize);
#endif
} else {
memcpy(value, *buffer, typeSize); memcpy(value, *buffer, typeSize);
break;
} }
*size -= typeSize;
*buffer += typeSize; *buffer += typeSize;
return HasReturnvaluesIF::RETURN_OK; return HasReturnvaluesIF::RETURN_OK;
} else { } else {

View File

@ -32,15 +32,15 @@ private:
/** /**
* \brief This value contains the size of the data pool entry in bytes. * \brief This value contains the size of the data pool entry in bytes.
*/ */
uint8_t typeSize; size_t typeSize;
/** /**
* The size of the DP array (single values return 1) * The size of the DP array (single values return 1)
*/ */
uint8_t arraySize; size_t arraySize;
/** /**
* The size (in bytes) from the selected entry till the end of this DataPool variable. * The size (in bytes) from the selected entry till the end of this DataPool variable.
*/ */
uint16_t sizeTillEnd; size_t sizeTillEnd;
/** /**
* \brief The information whether the class is read-write or read-only is stored here. * \brief The information whether the class is read-write or read-only is stored here.
*/ */
@ -97,8 +97,8 @@ public:
* \return - \c RETURN_OK if entry could be acquired * \return - \c RETURN_OK if entry could be acquired
* - \c RETURN_FAILED else. * - \c RETURN_FAILED else.
*/ */
ReturnValue_t getEntryEndianSafe(uint8_t *buffer, uint32_t *size, ReturnValue_t getEntryEndianSafe(uint8_t *buffer, size_t *size,
uint32_t maxSize); size_t maxSize);
/** /**
* With this method, the content can be set from a big endian buffer safely. * With this method, the content can be set from a big endian buffer safely.
* @param buffer Pointer to the data to set * @param buffer Pointer to the data to set
@ -107,7 +107,7 @@ public:
* - \c RETURN_FAILED on failure * - \c RETURN_FAILED on failure
*/ */
ReturnValue_t setEntryFromBigEndian(const uint8_t *buffer, ReturnValue_t setEntryFromBigEndian(const uint8_t *buffer,
uint32_t setSize); size_t setSize);
/** /**
* \brief This operation returns the type of the entry currently stored. * \brief This operation returns the type of the entry currently stored.
*/ */
@ -115,12 +115,12 @@ public:
/** /**
* \brief This operation returns the size of the entry currently stored. * \brief This operation returns the size of the entry currently stored.
*/ */
uint8_t getSizeOfType(); size_t getSizeOfType();
/** /**
* *
* @return the size of the datapool array * @return the size of the datapool array
*/ */
uint8_t getArraySize(); size_t getArraySize();
/** /**
* \brief This operation returns the data pool id of the variable. * \brief This operation returns the data pool id of the variable.
*/ */
@ -138,7 +138,7 @@ public:
/** /**
* Getter for the remaining size. * Getter for the remaining size.
*/ */
uint16_t getSizeTillEnd() const; size_t getSizeTillEnd() const;
ReturnValue_t serialize(uint8_t **buffer, size_t *size, size_t maxSize, ReturnValue_t serialize(uint8_t **buffer, size_t *size, size_t maxSize,
Endianness streamEndianness) const override; Endianness streamEndianness) const override;