2020-09-04 15:56:08 +02:00
|
|
|
#ifndef FSFW_SERIALIZE_SERIALIZEADAPTER_H_
|
|
|
|
#define FSFW_SERIALIZE_SERIALIZEADAPTER_H_
|
2016-06-15 23:48:41 +02:00
|
|
|
|
2020-08-13 20:53:35 +02:00
|
|
|
#include "EndianConverter.h"
|
|
|
|
#include "SerializeIF.h"
|
2016-06-15 23:48:41 +02:00
|
|
|
|
2020-09-04 15:56:08 +02:00
|
|
|
#include "../container/IsDerivedFrom.h"
|
|
|
|
#include "../returnvalues/HasReturnvaluesIF.h"
|
|
|
|
#include <cstring>
|
2020-04-21 21:34:03 +02:00
|
|
|
|
2020-09-04 15:56:08 +02:00
|
|
|
/**
|
|
|
|
* @brief These adapters provides an interface to use the SerializeIF functions
|
|
|
|
* with arbitrary template objects to facilitate and simplify the
|
|
|
|
* serialization of classes with different multiple different data types
|
|
|
|
* into buffers and vice-versa.
|
|
|
|
* @details
|
|
|
|
* The correct serialization or deserialization function is chosen at
|
|
|
|
* compile time with template type deduction.
|
|
|
|
*
|
|
|
|
* @ingroup serialize
|
|
|
|
*/
|
2020-04-21 21:34:03 +02:00
|
|
|
class SerializeAdapter {
|
2016-06-15 23:48:41 +02:00
|
|
|
public:
|
2020-04-21 21:34:03 +02:00
|
|
|
template<typename T>
|
|
|
|
static ReturnValue_t serialize(const T *object, uint8_t **buffer,
|
|
|
|
size_t *size, size_t maxSize, SerializeIF::Endianness streamEndianness) {
|
|
|
|
InternalSerializeAdapter<T, IsDerivedFrom<T, SerializeIF>::Is> adapter;
|
|
|
|
return adapter.serialize(object, buffer, size, maxSize,
|
|
|
|
streamEndianness);
|
|
|
|
}
|
|
|
|
template<typename T>
|
|
|
|
static uint32_t getSerializedSize(const T *object) {
|
|
|
|
InternalSerializeAdapter<T, IsDerivedFrom<T, SerializeIF>::Is> adapter;
|
|
|
|
return adapter.getSerializedSize(object);
|
|
|
|
}
|
|
|
|
template<typename T>
|
|
|
|
static ReturnValue_t deSerialize(T *object, const uint8_t **buffer,
|
|
|
|
size_t *size, SerializeIF::Endianness streamEndianness) {
|
|
|
|
InternalSerializeAdapter<T, IsDerivedFrom<T, SerializeIF>::Is> adapter;
|
|
|
|
return adapter.deSerialize(object, buffer, size, streamEndianness);
|
|
|
|
}
|
|
|
|
private:
|
|
|
|
template<typename T, int>
|
|
|
|
class InternalSerializeAdapter {
|
|
|
|
public:
|
|
|
|
static ReturnValue_t serialize(const T *object, uint8_t **buffer,
|
2020-09-04 15:56:08 +02:00
|
|
|
size_t *size, size_t max_size,
|
|
|
|
SerializeIF::Endianness streamEndianness) {
|
2020-04-21 21:34:03 +02:00
|
|
|
size_t ignoredSize = 0;
|
2020-09-04 15:56:08 +02:00
|
|
|
if (size == nullptr) {
|
2020-04-21 21:34:03 +02:00
|
|
|
size = &ignoredSize;
|
|
|
|
}
|
|
|
|
//TODO check integer overflow of *size
|
|
|
|
if (sizeof(T) + *size <= max_size) {
|
|
|
|
T tmp;
|
|
|
|
switch (streamEndianness) {
|
|
|
|
case SerializeIF::Endianness::BIG:
|
|
|
|
tmp = EndianConverter::convertBigEndian<T>(*object);
|
|
|
|
break;
|
|
|
|
case SerializeIF::Endianness::LITTLE:
|
|
|
|
tmp = EndianConverter::convertLittleEndian<T>(*object);
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
case SerializeIF::Endianness::MACHINE:
|
|
|
|
tmp = *object;
|
|
|
|
break;
|
|
|
|
}
|
2020-09-04 15:56:08 +02:00
|
|
|
std::memcpy(*buffer, &tmp, sizeof(T));
|
2020-04-21 21:34:03 +02:00
|
|
|
*size += sizeof(T);
|
|
|
|
(*buffer) += sizeof(T);
|
|
|
|
return HasReturnvaluesIF::RETURN_OK;
|
2016-06-15 23:48:41 +02:00
|
|
|
} else {
|
2020-04-21 21:34:03 +02:00
|
|
|
return SerializeIF::BUFFER_TOO_SHORT;
|
2016-06-15 23:48:41 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-04-21 21:34:03 +02:00
|
|
|
ReturnValue_t deSerialize(T *object, const uint8_t **buffer,
|
|
|
|
size_t *size, SerializeIF::Endianness streamEndianness) {
|
|
|
|
T tmp;
|
|
|
|
if (*size >= sizeof(T)) {
|
|
|
|
*size -= sizeof(T);
|
2020-09-04 15:56:08 +02:00
|
|
|
std::memcpy(&tmp, *buffer, sizeof(T));
|
2020-04-21 21:34:03 +02:00
|
|
|
switch (streamEndianness) {
|
|
|
|
case SerializeIF::Endianness::BIG:
|
|
|
|
*object = EndianConverter::convertBigEndian<T>(tmp);
|
|
|
|
break;
|
|
|
|
case SerializeIF::Endianness::LITTLE:
|
|
|
|
*object = EndianConverter::convertLittleEndian<T>(tmp);
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
case SerializeIF::Endianness::MACHINE:
|
|
|
|
*object = tmp;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
*buffer += sizeof(T);
|
|
|
|
return HasReturnvaluesIF::RETURN_OK;
|
2016-06-15 23:48:41 +02:00
|
|
|
} else {
|
2020-04-21 21:34:03 +02:00
|
|
|
return SerializeIF::STREAM_TOO_SHORT;
|
2016-06-15 23:48:41 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-04-21 21:34:03 +02:00
|
|
|
uint32_t getSerializedSize(const T *object) {
|
|
|
|
return sizeof(T);
|
2016-06-15 23:48:41 +02:00
|
|
|
}
|
|
|
|
|
2020-04-21 21:34:03 +02:00
|
|
|
};
|
2018-07-12 16:29:32 +02:00
|
|
|
|
|
|
|
template<typename T>
|
2020-04-21 21:34:03 +02:00
|
|
|
class InternalSerializeAdapter<T, 1> {
|
|
|
|
public:
|
|
|
|
ReturnValue_t serialize(const T *object, uint8_t **buffer,
|
|
|
|
size_t *size, size_t max_size,
|
|
|
|
SerializeIF::Endianness streamEndianness) const {
|
|
|
|
size_t ignoredSize = 0;
|
2020-09-04 15:56:08 +02:00
|
|
|
if (size == nullptr) {
|
2020-04-21 21:34:03 +02:00
|
|
|
size = &ignoredSize;
|
|
|
|
}
|
|
|
|
return object->serialize(buffer, size, max_size, streamEndianness);
|
|
|
|
}
|
|
|
|
uint32_t getSerializedSize(const T *object) const {
|
|
|
|
return object->getSerializedSize();
|
|
|
|
}
|
|
|
|
|
|
|
|
ReturnValue_t deSerialize(T *object, const uint8_t **buffer,
|
|
|
|
size_t *size, SerializeIF::Endianness streamEndianness) {
|
|
|
|
return object->deSerialize(buffer, size, streamEndianness);
|
|
|
|
}
|
|
|
|
};
|
2018-07-12 16:29:32 +02:00
|
|
|
};
|
|
|
|
|
2016-06-15 23:48:41 +02:00
|
|
|
#endif /* SERIALIZEADAPTER_H_ */
|