2020-08-13 20:53:35 +02:00
|
|
|
#include "DataPool.h"
|
|
|
|
#include "PoolEntryIF.h"
|
|
|
|
#include "PoolRawAccess.h"
|
|
|
|
#include "../serviceinterface/ServiceInterfaceStream.h"
|
|
|
|
#include "../serialize/EndianConverter.h"
|
2018-07-12 16:29:32 +02:00
|
|
|
|
2020-06-05 13:43:06 +02:00
|
|
|
#include <cstring>
|
|
|
|
|
2016-06-15 23:48:41 +02:00
|
|
|
PoolRawAccess::PoolRawAccess(uint32_t set_id, uint8_t setArrayEntry,
|
2020-07-03 15:47:05 +02:00
|
|
|
DataSetIF *data_set, ReadWriteMode_t setReadWriteMode) :
|
|
|
|
dataPoolId(set_id), arrayEntry(setArrayEntry), valid(false), type(
|
|
|
|
Type::UNKNOWN_TYPE), typeSize(0), arraySize(0), sizeTillEnd(0), readWriteMode(
|
|
|
|
setReadWriteMode) {
|
2016-06-15 23:48:41 +02:00
|
|
|
memset(value, 0, sizeof(value));
|
|
|
|
if (data_set != NULL) {
|
|
|
|
data_set->registerVariable(this);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
PoolRawAccess::~PoolRawAccess() {
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
ReturnValue_t PoolRawAccess::read() {
|
2020-07-03 15:47:05 +02:00
|
|
|
PoolEntryIF *read_out = ::dataPool.getRawData(dataPoolId);
|
2016-06-15 23:48:41 +02:00
|
|
|
if (read_out != NULL) {
|
|
|
|
valid = read_out->getValid();
|
|
|
|
if (read_out->getSize() > arrayEntry) {
|
2018-07-12 16:29:32 +02:00
|
|
|
arraySize = read_out->getSize();
|
2016-06-15 23:48:41 +02:00
|
|
|
typeSize = read_out->getByteSize() / read_out->getSize();
|
2018-07-12 16:29:32 +02:00
|
|
|
type = read_out->getType();
|
2016-06-15 23:48:41 +02:00
|
|
|
if (typeSize <= sizeof(value)) {
|
|
|
|
uint16_t arrayPosition = arrayEntry * typeSize;
|
|
|
|
sizeTillEnd = read_out->getByteSize() - arrayPosition;
|
2020-07-03 15:47:05 +02:00
|
|
|
uint8_t *ptr =
|
2016-06-15 23:48:41 +02:00
|
|
|
&((uint8_t*) read_out->getRawData())[arrayPosition];
|
|
|
|
memcpy(value, ptr, typeSize);
|
|
|
|
return HasReturnvaluesIF::RETURN_OK;
|
|
|
|
} else {
|
|
|
|
//Error value type too large.
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
//Error index requested too large
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
//Error entry does not exist.
|
|
|
|
}
|
2020-07-03 15:47:05 +02:00
|
|
|
sif::error << "PoolRawAccess: read of DP Variable 0x" << std::hex
|
|
|
|
<< dataPoolId << std::dec << " failed." << std::endl;
|
2016-06-15 23:48:41 +02:00
|
|
|
valid = INVALID;
|
|
|
|
typeSize = 0;
|
|
|
|
sizeTillEnd = 0;
|
|
|
|
memset(value, 0, sizeof(value));
|
|
|
|
return HasReturnvaluesIF::RETURN_FAILED;
|
|
|
|
}
|
|
|
|
|
|
|
|
ReturnValue_t PoolRawAccess::commit() {
|
2020-07-03 15:47:05 +02:00
|
|
|
PoolEntryIF *write_back = ::dataPool.getRawData(dataPoolId);
|
2016-06-15 23:48:41 +02:00
|
|
|
if ((write_back != NULL) && (readWriteMode != VAR_READ)) {
|
|
|
|
write_back->setValid(valid);
|
|
|
|
uint8_t array_position = arrayEntry * typeSize;
|
2020-07-03 15:47:05 +02:00
|
|
|
uint8_t *ptr = &((uint8_t*) write_back->getRawData())[array_position];
|
2016-06-15 23:48:41 +02:00
|
|
|
memcpy(ptr, value, typeSize);
|
|
|
|
return HasReturnvaluesIF::RETURN_OK;
|
|
|
|
} else {
|
|
|
|
return HasReturnvaluesIF::RETURN_FAILED;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
uint8_t* PoolRawAccess::getEntry() {
|
|
|
|
return value;
|
|
|
|
}
|
|
|
|
|
2020-07-03 15:47:05 +02:00
|
|
|
ReturnValue_t PoolRawAccess::getEntryEndianSafe(uint8_t *buffer,
|
|
|
|
size_t *writtenBytes, size_t maxSize) {
|
|
|
|
uint8_t *data_ptr = getEntry();
|
2016-06-15 23:48:41 +02:00
|
|
|
// debug << "PoolRawAccess::getEntry: Array position: " << index * size_of_type << " Size of T: " << (int)size_of_type << " ByteSize: " << byte_size << " Position: " << *size << std::endl;
|
2020-07-03 15:47:05 +02:00
|
|
|
if (typeSize == 0) {
|
2016-06-15 23:48:41 +02:00
|
|
|
return DATA_POOL_ACCESS_FAILED;
|
2020-07-03 15:47:05 +02:00
|
|
|
}
|
|
|
|
if (typeSize > maxSize) {
|
2016-06-15 23:48:41 +02:00
|
|
|
return INCORRECT_SIZE;
|
|
|
|
}
|
2020-07-03 15:47:05 +02:00
|
|
|
EndianConverter::convertBigEndian(buffer, data_ptr, typeSize);
|
2016-06-15 23:48:41 +02:00
|
|
|
*writtenBytes = typeSize;
|
|
|
|
return HasReturnvaluesIF::RETURN_OK;
|
|
|
|
}
|
|
|
|
|
2018-07-12 16:29:32 +02:00
|
|
|
Type PoolRawAccess::getType() {
|
|
|
|
return type;
|
|
|
|
}
|
|
|
|
|
2020-07-03 15:47:05 +02:00
|
|
|
size_t PoolRawAccess::getSizeOfType() {
|
2016-06-15 23:48:41 +02:00
|
|
|
return typeSize;
|
|
|
|
}
|
|
|
|
|
2020-07-03 15:47:05 +02:00
|
|
|
size_t PoolRawAccess::getArraySize() {
|
2018-07-12 16:29:32 +02:00
|
|
|
return arraySize;
|
|
|
|
}
|
|
|
|
|
2016-06-15 23:48:41 +02:00
|
|
|
uint32_t PoolRawAccess::getDataPoolId() const {
|
|
|
|
return dataPoolId;
|
|
|
|
}
|
|
|
|
|
|
|
|
PoolVariableIF::ReadWriteMode_t PoolRawAccess::getReadWriteMode() const {
|
|
|
|
return readWriteMode;
|
|
|
|
}
|
|
|
|
|
2020-07-03 15:47:05 +02:00
|
|
|
ReturnValue_t PoolRawAccess::setEntryFromBigEndian(const uint8_t *buffer,
|
|
|
|
size_t setSize) {
|
2016-06-15 23:48:41 +02:00
|
|
|
if (typeSize == setSize) {
|
2020-07-03 15:47:05 +02:00
|
|
|
EndianConverter::convertBigEndian(value, buffer, typeSize);
|
2016-06-15 23:48:41 +02:00
|
|
|
return HasReturnvaluesIF::RETURN_OK;
|
|
|
|
} else {
|
2020-07-03 15:47:05 +02:00
|
|
|
sif::error
|
|
|
|
<< "PoolRawAccess::setEntryFromBigEndian: Illegal sizes: Internal"
|
2016-06-15 23:48:41 +02:00
|
|
|
<< (uint32_t) typeSize << ", Requested: " << setSize
|
|
|
|
<< std::endl;
|
|
|
|
return INCORRECT_SIZE;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
bool PoolRawAccess::isValid() const {
|
|
|
|
if (valid != INVALID)
|
|
|
|
return true;
|
|
|
|
else
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
void PoolRawAccess::setValid(uint8_t valid) {
|
|
|
|
this->valid = valid;
|
|
|
|
}
|
|
|
|
|
2020-07-03 15:47:05 +02:00
|
|
|
size_t PoolRawAccess::getSizeTillEnd() const {
|
2016-06-15 23:48:41 +02:00
|
|
|
return sizeTillEnd;
|
|
|
|
}
|
|
|
|
|
2020-07-03 15:47:05 +02:00
|
|
|
ReturnValue_t PoolRawAccess::serialize(uint8_t **buffer, size_t *size,
|
2020-04-21 21:34:03 +02:00
|
|
|
size_t maxSize, Endianness streamEndianness) const {
|
|
|
|
if (typeSize + *size <= maxSize) {
|
2020-07-03 15:47:05 +02:00
|
|
|
switch (streamEndianness) {
|
|
|
|
case (Endianness::BIG):
|
|
|
|
EndianConverter::convertBigEndian(*buffer, value, typeSize);
|
|
|
|
break;
|
|
|
|
case (Endianness::LITTLE):
|
|
|
|
EndianConverter::convertLittleEndian(*buffer, value, typeSize);
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
case (Endianness::MACHINE):
|
2016-06-15 23:48:41 +02:00
|
|
|
memcpy(*buffer, value, typeSize);
|
2020-07-03 15:47:05 +02:00
|
|
|
break;
|
2016-06-15 23:48:41 +02:00
|
|
|
}
|
|
|
|
*size += typeSize;
|
|
|
|
(*buffer) += typeSize;
|
|
|
|
return HasReturnvaluesIF::RETURN_OK;
|
|
|
|
} else {
|
|
|
|
return SerializeIF::BUFFER_TOO_SHORT;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-04-21 21:34:03 +02:00
|
|
|
size_t PoolRawAccess::getSerializedSize() const {
|
2016-06-15 23:48:41 +02:00
|
|
|
return typeSize;
|
|
|
|
}
|
|
|
|
|
2020-07-03 15:47:05 +02:00
|
|
|
ReturnValue_t PoolRawAccess::deSerialize(const uint8_t **buffer, size_t *size,
|
2020-04-21 21:34:03 +02:00
|
|
|
Endianness streamEndianness) {
|
2016-06-15 23:48:41 +02:00
|
|
|
|
2020-04-21 21:34:03 +02:00
|
|
|
if (*size >= typeSize) {
|
2020-07-03 15:47:05 +02:00
|
|
|
switch (streamEndianness) {
|
|
|
|
case (Endianness::BIG):
|
|
|
|
EndianConverter::convertBigEndian(value, *buffer, typeSize);
|
|
|
|
break;
|
|
|
|
case (Endianness::LITTLE):
|
|
|
|
EndianConverter::convertLittleEndian(value, *buffer, typeSize);
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
case (Endianness::MACHINE):
|
2016-06-15 23:48:41 +02:00
|
|
|
memcpy(value, *buffer, typeSize);
|
2020-07-03 15:47:05 +02:00
|
|
|
break;
|
2016-06-15 23:48:41 +02:00
|
|
|
}
|
2020-07-03 15:47:05 +02:00
|
|
|
*size -= typeSize;
|
2016-06-15 23:48:41 +02:00
|
|
|
*buffer += typeSize;
|
|
|
|
return HasReturnvaluesIF::RETURN_OK;
|
|
|
|
} else {
|
|
|
|
return SerializeIF::STREAM_TOO_SHORT;
|
|
|
|
}
|
|
|
|
}
|