taken over changes
This commit is contained in:
parent
f2f7a5de87
commit
305d8ef840
@ -1,11 +1,12 @@
|
|||||||
#ifndef SERIALIZEADAPTER_H_
|
#ifndef FSFW_SERIALIZE_SERIALIZEADAPTER_H_
|
||||||
#define SERIALIZEADAPTER_H_
|
#define FSFW_SERIALIZE_SERIALIZEADAPTER_H_
|
||||||
|
|
||||||
|
#include "EndianConverter.h"
|
||||||
|
#include "SerializeIF.h"
|
||||||
|
|
||||||
#include "../container/IsDerivedFrom.h"
|
#include "../container/IsDerivedFrom.h"
|
||||||
#include "../returnvalues/HasReturnvaluesIF.h"
|
#include "../returnvalues/HasReturnvaluesIF.h"
|
||||||
#include "../serialize/EndianConverter.h"
|
#include <cstring>
|
||||||
#include "../serialize/SerializeIF.h"
|
|
||||||
#include <type_traits>
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @brief These adapters provides an interface to use the SerializeIF functions
|
* @brief These adapters provides an interface to use the SerializeIF functions
|
||||||
@ -13,56 +14,11 @@
|
|||||||
* serialization of classes with different multiple different data types
|
* serialization of classes with different multiple different data types
|
||||||
* into buffers and vice-versa.
|
* into buffers and vice-versa.
|
||||||
* @details
|
* @details
|
||||||
*
|
* The correct serialization or deserialization function is chosen at
|
||||||
* A report class is converted into a TM buffer. The report class implements a
|
* compile time with template type deduction.
|
||||||
* serialize functions and calls the AutoSerializeAdapter::serialize function
|
|
||||||
* repeatedly on all object data fields. The getSerializedSize function is
|
|
||||||
* implemented by calling the AutoSerializeAdapter::getSerializedSize function
|
|
||||||
* repeatedly on all data fields.
|
|
||||||
*
|
|
||||||
* The AutoSerializeAdapter functions can also be used as an alternative to
|
|
||||||
* memcpy to retrieve data out of a buffer directly into a class variable
|
|
||||||
* with data type T while being able to specify endianness. The boolean
|
|
||||||
* bigEndian specifies whether an endian swap is performed on the data before
|
|
||||||
* serialization or deserialization.
|
|
||||||
*
|
|
||||||
* There are three ways to retrieve data out of a buffer to be used in the FSFW
|
|
||||||
* to use regular aligned (big endian) data. Examples:
|
|
||||||
*
|
|
||||||
* 1. Use the AutoSerializeAdapter::deSerialize function
|
|
||||||
* The pointer *buffer will be incremented automatically by the typeSize
|
|
||||||
* of the object, so this function can be called on &buffer repeatedly
|
|
||||||
* without adjusting pointer position. Set bigEndian parameter to true
|
|
||||||
* to perform endian swapping, if necessary
|
|
||||||
* @code
|
|
||||||
* uint16_t data;
|
|
||||||
* int32_t dataLen = sizeof(data);
|
|
||||||
* ReturnValue_t result =
|
|
||||||
* AutoSerializeAdapter::deSerialize(&data,&buffer,&dataLen,true);
|
|
||||||
* @endcode
|
|
||||||
*
|
|
||||||
* 2. Perform a bitshift operation. Watch for for endianness:
|
|
||||||
* @code
|
|
||||||
* uint16_t data;
|
|
||||||
* data = buffer[targetByte1] << 8 | buffer[targetByte2];
|
|
||||||
* data = EndianSwapper::swap(data); //optional, or swap order above
|
|
||||||
* @endcode
|
|
||||||
*
|
|
||||||
* 3. memcpy or std::copy can also be used, but watch out if system
|
|
||||||
* endianness is different from required data endianness.
|
|
||||||
* Perform endian-swapping if necessary.
|
|
||||||
* @code
|
|
||||||
* uint16_t data;
|
|
||||||
* memcpy(&data,buffer + positionOfTargetByte1,sizeof(data));
|
|
||||||
* data = EndianSwapper::swap(data); //optional
|
|
||||||
* @endcode
|
|
||||||
*
|
|
||||||
* When serializing for downlink, the packets are generally serialized assuming
|
|
||||||
* big endian data format like seen in TmPacketStored.cpp for example.
|
|
||||||
*
|
*
|
||||||
* @ingroup serialize
|
* @ingroup serialize
|
||||||
*/
|
*/
|
||||||
|
|
||||||
class SerializeAdapter {
|
class SerializeAdapter {
|
||||||
public:
|
public:
|
||||||
template<typename T>
|
template<typename T>
|
||||||
@ -88,9 +44,10 @@ private:
|
|||||||
class InternalSerializeAdapter {
|
class InternalSerializeAdapter {
|
||||||
public:
|
public:
|
||||||
static ReturnValue_t serialize(const T *object, uint8_t **buffer,
|
static ReturnValue_t serialize(const T *object, uint8_t **buffer,
|
||||||
size_t *size, size_t max_size, SerializeIF::Endianness streamEndianness) {
|
size_t *size, size_t max_size,
|
||||||
|
SerializeIF::Endianness streamEndianness) {
|
||||||
size_t ignoredSize = 0;
|
size_t ignoredSize = 0;
|
||||||
if (size == NULL) {
|
if (size == nullptr) {
|
||||||
size = &ignoredSize;
|
size = &ignoredSize;
|
||||||
}
|
}
|
||||||
//TODO check integer overflow of *size
|
//TODO check integer overflow of *size
|
||||||
@ -108,7 +65,7 @@ private:
|
|||||||
tmp = *object;
|
tmp = *object;
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
memcpy(*buffer, &tmp, sizeof(T));
|
std::memcpy(*buffer, &tmp, sizeof(T));
|
||||||
*size += sizeof(T);
|
*size += sizeof(T);
|
||||||
(*buffer) += sizeof(T);
|
(*buffer) += sizeof(T);
|
||||||
return HasReturnvaluesIF::RETURN_OK;
|
return HasReturnvaluesIF::RETURN_OK;
|
||||||
@ -122,7 +79,7 @@ private:
|
|||||||
T tmp;
|
T tmp;
|
||||||
if (*size >= sizeof(T)) {
|
if (*size >= sizeof(T)) {
|
||||||
*size -= sizeof(T);
|
*size -= sizeof(T);
|
||||||
memcpy(&tmp, *buffer, sizeof(T));
|
std::memcpy(&tmp, *buffer, sizeof(T));
|
||||||
switch (streamEndianness) {
|
switch (streamEndianness) {
|
||||||
case SerializeIF::Endianness::BIG:
|
case SerializeIF::Endianness::BIG:
|
||||||
*object = EndianConverter::convertBigEndian<T>(tmp);
|
*object = EndianConverter::convertBigEndian<T>(tmp);
|
||||||
@ -156,7 +113,7 @@ private:
|
|||||||
size_t *size, size_t max_size,
|
size_t *size, size_t max_size,
|
||||||
SerializeIF::Endianness streamEndianness) const {
|
SerializeIF::Endianness streamEndianness) const {
|
||||||
size_t ignoredSize = 0;
|
size_t ignoredSize = 0;
|
||||||
if (size == NULL) {
|
if (size == nullptr) {
|
||||||
size = &ignoredSize;
|
size = &ignoredSize;
|
||||||
}
|
}
|
||||||
return object->serialize(buffer, size, max_size, streamEndianness);
|
return object->serialize(buffer, size, max_size, streamEndianness);
|
||||||
|
Loading…
x
Reference in New Issue
Block a user