2020-08-13 20:53:35 +02:00
|
|
|
#include "ParameterWrapper.h"
|
2016-06-15 23:48:41 +02:00
|
|
|
|
|
|
|
ParameterWrapper::ParameterWrapper() :
|
2020-08-28 18:46:47 +02:00
|
|
|
pointsToStream(false), type(Type::UNKNOWN_TYPE) {
|
2016-06-15 23:48:41 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
ParameterWrapper::ParameterWrapper(Type type, uint8_t rows, uint8_t columns,
|
2020-04-21 22:28:43 +02:00
|
|
|
void *data) :
|
2020-08-28 18:46:47 +02:00
|
|
|
pointsToStream(false), type(type), rows(rows), columns(columns),
|
|
|
|
data(data), readonlyData(data) {
|
2016-06-15 23:48:41 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
ParameterWrapper::ParameterWrapper(Type type, uint8_t rows, uint8_t columns,
|
2020-04-21 22:28:43 +02:00
|
|
|
const void *data) :
|
2020-08-28 18:46:47 +02:00
|
|
|
pointsToStream(false), type(type), rows(rows), columns(columns),
|
|
|
|
data(nullptr), readonlyData(data) {
|
2016-06-15 23:48:41 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
ParameterWrapper::~ParameterWrapper() {
|
|
|
|
}
|
|
|
|
|
2020-04-21 22:28:43 +02:00
|
|
|
ReturnValue_t ParameterWrapper::serialize(uint8_t **buffer, size_t *size,
|
|
|
|
size_t maxSize, Endianness streamEndianness) const {
|
2016-06-15 23:48:41 +02:00
|
|
|
ReturnValue_t result;
|
|
|
|
|
2020-04-21 22:28:43 +02:00
|
|
|
result = SerializeAdapter::serialize(&type, buffer, size, maxSize,
|
|
|
|
streamEndianness);
|
2016-06-15 23:48:41 +02:00
|
|
|
if (result != HasReturnvaluesIF::RETURN_OK) {
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2020-04-21 22:28:43 +02:00
|
|
|
result = SerializeAdapter::serialize(&columns, buffer, size, maxSize,
|
|
|
|
streamEndianness);
|
2016-06-15 23:48:41 +02:00
|
|
|
if (result != HasReturnvaluesIF::RETURN_OK) {
|
|
|
|
return result;
|
|
|
|
}
|
2020-04-21 22:28:43 +02:00
|
|
|
result = SerializeAdapter::serialize(&rows, buffer, size, maxSize,
|
|
|
|
streamEndianness);
|
2016-06-15 23:48:41 +02:00
|
|
|
if (result != HasReturnvaluesIF::RETURN_OK) {
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
//serialize uses readonlyData, as it is always valid
|
|
|
|
if (readonlyData == NULL) {
|
|
|
|
return NOT_SET;
|
|
|
|
}
|
|
|
|
switch (type) {
|
|
|
|
case Type::UINT8_T:
|
2020-04-21 22:28:43 +02:00
|
|
|
result = serializeData<uint8_t>(buffer, size, maxSize,
|
|
|
|
streamEndianness);
|
2016-06-15 23:48:41 +02:00
|
|
|
break;
|
|
|
|
case Type::INT8_T:
|
2020-04-21 22:28:43 +02:00
|
|
|
result = serializeData<int8_t>(buffer, size, maxSize, streamEndianness);
|
2016-06-15 23:48:41 +02:00
|
|
|
break;
|
|
|
|
case Type::UINT16_T:
|
2020-04-21 22:28:43 +02:00
|
|
|
result = serializeData<uint16_t>(buffer, size, maxSize,
|
|
|
|
streamEndianness);
|
2016-06-15 23:48:41 +02:00
|
|
|
break;
|
|
|
|
case Type::INT16_T:
|
2020-04-21 22:28:43 +02:00
|
|
|
result = serializeData<int16_t>(buffer, size, maxSize,
|
|
|
|
streamEndianness);
|
2016-06-15 23:48:41 +02:00
|
|
|
break;
|
|
|
|
case Type::UINT32_T:
|
2020-04-21 22:28:43 +02:00
|
|
|
result = serializeData<uint32_t>(buffer, size, maxSize,
|
|
|
|
streamEndianness);
|
2016-06-15 23:48:41 +02:00
|
|
|
break;
|
|
|
|
case Type::INT32_T:
|
2020-04-21 22:28:43 +02:00
|
|
|
result = serializeData<int32_t>(buffer, size, maxSize,
|
|
|
|
streamEndianness);
|
2016-06-15 23:48:41 +02:00
|
|
|
break;
|
|
|
|
case Type::FLOAT:
|
2020-04-21 22:28:43 +02:00
|
|
|
result = serializeData<float>(buffer, size, maxSize, streamEndianness);
|
2016-06-15 23:48:41 +02:00
|
|
|
break;
|
|
|
|
case Type::DOUBLE:
|
2020-04-21 22:28:43 +02:00
|
|
|
result = serializeData<double>(buffer, size, maxSize, streamEndianness);
|
2016-06-15 23:48:41 +02:00
|
|
|
break;
|
|
|
|
default:
|
|
|
|
result = UNKNOW_DATATYPE;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2020-04-21 22:28:43 +02:00
|
|
|
size_t ParameterWrapper::getSerializedSize() const {
|
2016-06-15 23:48:41 +02:00
|
|
|
uint32_t serializedSize = 0;
|
|
|
|
serializedSize += type.getSerializedSize();
|
|
|
|
serializedSize += sizeof(rows);
|
|
|
|
serializedSize += sizeof(columns);
|
|
|
|
serializedSize += rows * columns * type.getSize();
|
|
|
|
|
|
|
|
return serializedSize;
|
|
|
|
}
|
|
|
|
|
|
|
|
template<typename T>
|
2020-04-21 22:28:43 +02:00
|
|
|
ReturnValue_t ParameterWrapper::serializeData(uint8_t **buffer, size_t *size,
|
|
|
|
size_t maxSize, Endianness streamEndianness) const {
|
2016-06-15 23:48:41 +02:00
|
|
|
const T *element = (const T*) readonlyData;
|
2020-04-24 17:05:34 +02:00
|
|
|
ReturnValue_t result = HasReturnvaluesIF::RETURN_OK;
|
2016-06-15 23:48:41 +02:00
|
|
|
uint16_t dataSize = columns * rows;
|
|
|
|
while (dataSize != 0) {
|
2020-04-21 22:28:43 +02:00
|
|
|
result = SerializeAdapter::serialize(element, buffer, size, maxSize,
|
|
|
|
streamEndianness);
|
2016-06-15 23:48:41 +02:00
|
|
|
if (result != HasReturnvaluesIF::RETURN_OK) {
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
element++;
|
|
|
|
dataSize--;
|
|
|
|
}
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
template<typename T>
|
|
|
|
ReturnValue_t ParameterWrapper::deSerializeData(uint8_t startingRow,
|
|
|
|
uint8_t startingColumn, const void *from, uint8_t fromRows,
|
|
|
|
uint8_t fromColumns) {
|
|
|
|
|
|
|
|
//treat from as a continuous Stream as we copy all of it
|
2020-04-21 22:28:43 +02:00
|
|
|
const uint8_t *fromAsStream = (const uint8_t*) from;
|
|
|
|
size_t streamSize = fromRows * fromColumns * sizeof(T);
|
2016-06-15 23:48:41 +02:00
|
|
|
|
2018-07-12 16:29:32 +02:00
|
|
|
ReturnValue_t result = HasReturnvaluesIF::RETURN_OK;
|
2016-06-15 23:48:41 +02:00
|
|
|
|
|
|
|
for (uint8_t fromRow = 0; fromRow < fromRows; fromRow++) {
|
|
|
|
|
|
|
|
//get the start element of this row in data
|
2020-04-21 22:28:43 +02:00
|
|
|
T *dataWithDataType = ((T*) data)
|
2016-06-15 23:48:41 +02:00
|
|
|
+ (((startingRow + fromRow) * columns) + startingColumn);
|
|
|
|
|
|
|
|
for (uint8_t fromColumn = 0; fromColumn < fromColumns; fromColumn++) {
|
2020-04-21 21:34:03 +02:00
|
|
|
result = SerializeAdapter::deSerialize(
|
2016-06-15 23:48:41 +02:00
|
|
|
dataWithDataType + fromColumn, &fromAsStream, &streamSize,
|
2020-04-21 22:28:43 +02:00
|
|
|
SerializeIF::Endianness::BIG);
|
2016-06-15 23:48:41 +02:00
|
|
|
if (result != HasReturnvaluesIF::RETURN_OK) {
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return result;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2020-08-28 18:46:47 +02:00
|
|
|
|
2020-04-21 22:28:43 +02:00
|
|
|
ReturnValue_t ParameterWrapper::deSerialize(const uint8_t **buffer,
|
|
|
|
size_t *size, Endianness streamEndianness) {
|
|
|
|
return deSerialize(buffer, size, streamEndianness, 0);
|
2016-06-15 23:48:41 +02:00
|
|
|
}
|
|
|
|
|
2020-04-21 22:28:43 +02:00
|
|
|
ReturnValue_t ParameterWrapper::deSerialize(const uint8_t **buffer,
|
|
|
|
size_t *size, Endianness streamEndianness,
|
|
|
|
uint16_t startWritingAtIndex) {
|
2016-06-15 23:48:41 +02:00
|
|
|
ParameterWrapper streamDescription;
|
|
|
|
|
|
|
|
ReturnValue_t result = streamDescription.set(*buffer, *size, buffer, size);
|
|
|
|
if (result != HasReturnvaluesIF::RETURN_OK) {
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
return copyFrom(&streamDescription, startWritingAtIndex);
|
|
|
|
}
|
|
|
|
|
2020-04-21 22:28:43 +02:00
|
|
|
ReturnValue_t ParameterWrapper::set(const uint8_t *stream, size_t streamSize,
|
|
|
|
const uint8_t **remainingStream, size_t *remainingSize) {
|
2020-04-21 21:34:03 +02:00
|
|
|
ReturnValue_t result = SerializeAdapter::deSerialize(&type, &stream,
|
2020-04-21 22:28:43 +02:00
|
|
|
&streamSize, SerializeIF::Endianness::BIG);
|
2016-06-15 23:48:41 +02:00
|
|
|
if (result != HasReturnvaluesIF::RETURN_OK) {
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2020-04-21 22:28:43 +02:00
|
|
|
result = SerializeAdapter::deSerialize(&columns, &stream, &streamSize,
|
|
|
|
SerializeIF::Endianness::BIG);
|
2016-06-15 23:48:41 +02:00
|
|
|
if (result != HasReturnvaluesIF::RETURN_OK) {
|
|
|
|
return result;
|
|
|
|
}
|
2020-04-21 21:34:03 +02:00
|
|
|
result = SerializeAdapter::deSerialize(&rows, &stream, &streamSize,
|
2020-04-21 22:28:43 +02:00
|
|
|
SerializeIF::Endianness::BIG);
|
2016-06-15 23:48:41 +02:00
|
|
|
if (result != HasReturnvaluesIF::RETURN_OK) {
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2020-04-21 22:28:43 +02:00
|
|
|
size_t dataSize = type.getSize() * rows * columns;
|
2016-06-15 23:48:41 +02:00
|
|
|
|
|
|
|
if (streamSize < dataSize) {
|
|
|
|
return SerializeIF::STREAM_TOO_SHORT;
|
|
|
|
}
|
|
|
|
|
2020-08-28 18:46:47 +02:00
|
|
|
data = nullptr;
|
2016-06-15 23:48:41 +02:00
|
|
|
readonlyData = stream;
|
|
|
|
pointsToStream = true;
|
|
|
|
|
|
|
|
stream += dataSize;
|
2020-08-28 18:46:47 +02:00
|
|
|
if (remainingStream != nullptr) {
|
2016-06-15 23:48:41 +02:00
|
|
|
*remainingStream = stream;
|
|
|
|
}
|
|
|
|
streamSize -= dataSize;
|
2020-08-28 18:46:47 +02:00
|
|
|
if (remainingSize != nullptr) {
|
2016-06-15 23:48:41 +02:00
|
|
|
*remainingSize = streamSize;
|
|
|
|
}
|
|
|
|
|
|
|
|
return HasReturnvaluesIF::RETURN_OK;
|
|
|
|
}
|
|
|
|
|
2020-04-21 22:28:43 +02:00
|
|
|
ReturnValue_t ParameterWrapper::copyFrom(const ParameterWrapper *from,
|
2016-06-15 23:48:41 +02:00
|
|
|
uint16_t startWritingAtIndex) {
|
|
|
|
if (data == NULL) {
|
|
|
|
return READONLY;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (from->readonlyData == NULL) {
|
|
|
|
return SOURCE_NOT_SET;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (type != from->type) {
|
|
|
|
return DATATYPE_MISSMATCH;
|
|
|
|
}
|
|
|
|
|
|
|
|
//check if from fits into this
|
|
|
|
uint8_t startingRow = startWritingAtIndex / columns;
|
|
|
|
uint8_t startingColumn = startWritingAtIndex % columns;
|
|
|
|
|
|
|
|
if ((from->rows > (rows - startingRow))
|
|
|
|
|| (from->columns > (columns - startingColumn))) {
|
|
|
|
return TOO_BIG;
|
|
|
|
}
|
|
|
|
|
|
|
|
uint8_t typeSize = type.getSize();
|
|
|
|
|
2018-07-12 16:29:32 +02:00
|
|
|
ReturnValue_t result = HasReturnvaluesIF::RETURN_FAILED;
|
2016-06-15 23:48:41 +02:00
|
|
|
//copy data
|
|
|
|
if (from->pointsToStream) {
|
|
|
|
switch (type) {
|
|
|
|
case Type::UINT8_T:
|
|
|
|
result = deSerializeData<uint8_t>(startingRow, startingColumn,
|
|
|
|
from->readonlyData, from->rows, from->columns);
|
|
|
|
break;
|
|
|
|
case Type::INT8_T:
|
|
|
|
result = deSerializeData<int8_t>(startingRow, startingColumn,
|
|
|
|
from->readonlyData, from->rows, from->columns);
|
|
|
|
break;
|
|
|
|
case Type::UINT16_T:
|
|
|
|
result = deSerializeData<uint16_t>(startingRow, startingColumn,
|
|
|
|
from->readonlyData, from->rows, from->columns);
|
|
|
|
break;
|
|
|
|
case Type::INT16_T:
|
|
|
|
result = deSerializeData<int16_t>(startingRow, startingColumn,
|
|
|
|
from->readonlyData, from->rows, from->columns);
|
|
|
|
break;
|
|
|
|
case Type::UINT32_T:
|
|
|
|
result = deSerializeData<uint32_t>(startingRow, startingColumn,
|
|
|
|
from->readonlyData, from->rows, from->columns);
|
|
|
|
break;
|
|
|
|
case Type::INT32_T:
|
|
|
|
result = deSerializeData<int32_t>(startingRow, startingColumn,
|
|
|
|
from->readonlyData, from->rows, from->columns);
|
|
|
|
break;
|
|
|
|
case Type::FLOAT:
|
|
|
|
result = deSerializeData<float>(startingRow, startingColumn,
|
|
|
|
from->readonlyData, from->rows, from->columns);
|
|
|
|
break;
|
|
|
|
case Type::DOUBLE:
|
|
|
|
result = deSerializeData<double>(startingRow, startingColumn,
|
|
|
|
from->readonlyData, from->rows, from->columns);
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
result = UNKNOW_DATATYPE;
|
|
|
|
break;
|
|
|
|
}
|
2020-08-28 18:46:47 +02:00
|
|
|
}
|
|
|
|
else {
|
2016-06-15 23:48:41 +02:00
|
|
|
//need a type to do arithmetic
|
2020-08-28 18:46:47 +02:00
|
|
|
uint8_t* typedData = static_cast<uint8_t*>(data);
|
2016-06-15 23:48:41 +02:00
|
|
|
for (uint8_t fromRow = 0; fromRow < from->rows; fromRow++) {
|
2020-08-28 18:46:47 +02:00
|
|
|
uint8_t offset = (((startingRow + fromRow) * columns) + startingColumn) * typeSize;
|
|
|
|
std::memcpy(typedData + offset, from->readonlyData,
|
|
|
|
typeSize * from->columns);
|
2016-06-15 23:48:41 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-07-12 16:29:32 +02:00
|
|
|
return result;
|
2016-06-15 23:48:41 +02:00
|
|
|
}
|