mueller_Serialization #34

Closed
muellerr wants to merge 2 commits from KSat:mueller_Serialization into master
10 changed files with 339 additions and 235 deletions

View File

@ -11,10 +11,14 @@
*/ */
class EndianSwapper { class EndianSwapper {
private: private:
EndianSwapper() { EndianSwapper() {};
}
;
public: public:
/**
* Swap the endianness of a variable with arbitrary type
* @tparam T Type of variable
* @param in variable
* @return Variable with swapped endianness
*/
template<typename T> template<typename T>
static T swap(T in) { static T swap(T in) {
#ifndef BYTE_ORDER_SYSTEM #ifndef BYTE_ORDER_SYSTEM
@ -33,7 +37,14 @@ public:
#error Unknown Byte Order #error Unknown Byte Order
#endif #endif
} }
static void swap(uint8_t* out, const uint8_t* in, uint32_t size) {
/**
* Swap the endianness of a buffer.
* @param out
* @param in
* @param size
*/
static void swap(uint8_t* out, const uint8_t* in, size_t size) {
#ifndef BYTE_ORDER_SYSTEM #ifndef BYTE_ORDER_SYSTEM
#error BYTE_ORDER_SYSTEM not defined #error BYTE_ORDER_SYSTEM not defined
#elif BYTE_ORDER_SYSTEM == LITTLE_ENDIAN #elif BYTE_ORDER_SYSTEM == LITTLE_ENDIAN
@ -49,21 +60,23 @@ public:
/** /**
* Swap endianness of buffer entries * Swap endianness of buffer entries
* Template argument specifies buffer type * Template argument specifies buffer type. The number of entries
* (not the buffer size!) must be supplied
* @param out * @param out
* @param in * @param in
* @param size * @param size Number of buffer entries (not size of buffer in bytes!)
*/ */
template<typename T> template<typename T>
static void swap(T * out, const T * in, uint32_t size) { static void swap(T * out, const T * in, uint32_t entries) {
#ifndef BYTE_ORDER_SYSTEM #ifndef BYTE_ORDER_SYSTEM
#error BYTE_ORDER_SYSTEM not defined #error BYTE_ORDER_SYSTEM not defined
#elif BYTE_ORDER_SYSTEM == LITTLE_ENDIAN #elif BYTE_ORDER_SYSTEM == LITTLE_ENDIAN
const uint8_t * in_buffer = reinterpret_cast<const uint8_t *>(in); const uint8_t * in_buffer = reinterpret_cast<const uint8_t *>(in);
uint8_t * out_buffer = reinterpret_cast<uint8_t *>(out); uint8_t * out_buffer = reinterpret_cast<uint8_t *>(out);
for (uint8_t count = 0; count < size; count++) { for (uint8_t count = 0; count < entries; count++) {
for(uint8_t i = 0; i < sizeof(T);i++) { for(uint8_t i = 0; i < sizeof(T);i++) {
out_buffer[sizeof(T)* (count + 1) - i - 1] = in_buffer[count * sizeof(T) + i]; out_buffer[sizeof(T)* (count + 1) - i - 1] =
in_buffer[count * sizeof(T) + i];
} }
} }
return; return;

View File

@ -13,7 +13,7 @@
/** /**
* Also serializes length field ! * Also serializes length field !
* \ingroup serialize * @ingroup serialize
*/ */
template<typename T, typename count_t = uint8_t> template<typename T, typename count_t = uint8_t>
class SerialArrayListAdapter : public SerializeIF { class SerialArrayListAdapter : public SerializeIF {
@ -22,7 +22,7 @@ public:
} }
virtual ReturnValue_t serialize(uint8_t** buffer, size_t* size, virtual ReturnValue_t serialize(uint8_t** buffer, size_t* size,
const size_t max_size, bool bigEndian) const { const size_t max_size, bool bigEndian) const override {
return serialize(adaptee, buffer, size, max_size, bigEndian); return serialize(adaptee, buffer, size, max_size, bigEndian);
} }
@ -41,7 +41,7 @@ public:
return result; return result;
} }
virtual size_t getSerializedSize() const { virtual size_t getSerializedSize() const override {
return getSerializedSize(adaptee); return getSerializedSize(adaptee);
} }
@ -56,16 +56,19 @@ public:
return printSize; return printSize;
} }
virtual ReturnValue_t deSerialize(const uint8_t** buffer, ssize_t* size, virtual ReturnValue_t deSerialize(const uint8_t** buffer, size_t* size,
bool bigEndian) { bool bigEndian) override {
return deSerialize(adaptee, buffer, size, bigEndian); return deSerialize(adaptee, buffer, size, bigEndian);
} }
static ReturnValue_t deSerialize(ArrayList<T, count_t>* list, static ReturnValue_t deSerialize(ArrayList<T, count_t>* list,
const uint8_t** buffer, ssize_t* size, bool bigEndian) { const uint8_t** buffer, size_t* size, bool bigEndian) {
count_t tempSize = 0; count_t tempSize = 0;
ReturnValue_t result = SerializeAdapter<count_t>::deSerialize(&tempSize, ReturnValue_t result = SerializeAdapter<count_t>::deSerialize(&tempSize,
buffer, size, bigEndian); buffer, size, bigEndian);
if(result != HasReturnvaluesIF::RETURN_OK) {
return result;
}
if (tempSize > list->maxSize()) { if (tempSize > list->maxSize()) {
return SerializeIF::TOO_MANY_ELEMENTS; return SerializeIF::TOO_MANY_ELEMENTS;
} }

View File

@ -12,8 +12,8 @@ SerialBufferAdapter<count_t>::SerialBufferAdapter(const void* buffer,
} }
template<typename count_t> template<typename count_t>
SerialBufferAdapter<count_t>::SerialBufferAdapter(void* buffer, count_t bufferLength, SerialBufferAdapter<count_t>::SerialBufferAdapter(void* buffer,
bool serializeLength) : count_t bufferLength, bool serializeLength) :
serializeLength(serializeLength), bufferLength(bufferLength) { serializeLength(serializeLength), bufferLength(bufferLength) {
uint8_t * member_buffer = static_cast<uint8_t *>(buffer); uint8_t * member_buffer = static_cast<uint8_t *>(buffer);
m_buffer = member_buffer; m_buffer = member_buffer;
@ -26,8 +26,8 @@ SerialBufferAdapter<count_t>::~SerialBufferAdapter() {
} }
template<typename count_t> template<typename count_t>
ReturnValue_t SerialBufferAdapter<count_t>::serialize(uint8_t** buffer, size_t* size, ReturnValue_t SerialBufferAdapter<count_t>::serialize(uint8_t** buffer,
const size_t max_size, bool bigEndian) const { size_t* size, const size_t max_size, bool bigEndian) const {
uint32_t serializedLength = bufferLength; uint32_t serializedLength = bufferLength;
if (serializeLength) { if (serializeLength) {
serializedLength += AutoSerializeAdapter::getSerializedSize( serializedLength += AutoSerializeAdapter::getSerializedSize(
@ -61,36 +61,38 @@ size_t SerialBufferAdapter<count_t>::getSerializedSize() const {
return bufferLength; return bufferLength;
} }
} }
template<typename count_t> template<typename count_t>
ReturnValue_t SerialBufferAdapter<count_t>::deSerialize(const uint8_t** buffer, ReturnValue_t SerialBufferAdapter<count_t>::deSerialize(const uint8_t** buffer,
ssize_t* size, bool bigEndian) { size_t* size, bool bigEndian) {
//TODO Ignores Endian flag! //TODO Ignores Endian flag!
if (buffer != NULL) { // (Robin) the one of the buffer? wouldn't that be an issue for serialize
if(serializeLength){ // as well? SerialFixedArrayListAdapter implements swapping of buffer
// Suggestion (would require removing rest of the block inside this if clause !): // fields (if buffer type is not uint8_t)
//ReturnValue_t result = AutoSerializeAdapter::deSerialize(&bufferLength,buffer,size,bigEndian); if (buffer != nullptr) {
//if (result != HasReturnvaluesIF::RETURN_OK) { if(serializeLength) {
// return result;
//}
count_t serializedSize = AutoSerializeAdapter::getSerializedSize( count_t serializedSize = AutoSerializeAdapter::getSerializedSize(
&bufferLength); &bufferLength);
if((*size - bufferLength - serializedSize) >= 0){ if(bufferLength + serializedSize >= *size) {
*buffer += serializedSize; *buffer += serializedSize;
*size -= serializedSize; *size -= serializedSize;
}else{ }
else {
return STREAM_TOO_SHORT; return STREAM_TOO_SHORT;
} }
} }
//No Else If, go on with buffer //No Else If, go on with buffer
if (*size - bufferLength >= 0) { if (bufferLength >= *size) {
*size -= bufferLength; *size -= bufferLength;
memcpy(m_buffer, *buffer, bufferLength); memcpy(m_buffer, *buffer, bufferLength);
(*buffer) += bufferLength; (*buffer) += bufferLength;
return HasReturnvaluesIF::RETURN_OK; return HasReturnvaluesIF::RETURN_OK;
} else { }
else {
return STREAM_TOO_SHORT; return STREAM_TOO_SHORT;
} }
} else { }
else {
return HasReturnvaluesIF::RETURN_FAILED; return HasReturnvaluesIF::RETURN_FAILED;
} }
} }
@ -98,7 +100,8 @@ ReturnValue_t SerialBufferAdapter<count_t>::deSerialize(const uint8_t** buffer,
template<typename count_t> template<typename count_t>
uint8_t * SerialBufferAdapter<count_t>::getBuffer() { uint8_t * SerialBufferAdapter<count_t>::getBuffer() {
if(m_buffer == nullptr) { if(m_buffer == nullptr) {
error << "Wrong access function for stored type ! Use getConstBuffer()" << std::endl; error << "Wrong access function for stored type !"
" Use getConstBuffer()" << std::endl;
return nullptr; return nullptr;
} }
return m_buffer; return m_buffer;
@ -107,14 +110,16 @@ uint8_t * SerialBufferAdapter<count_t>::getBuffer() {
template<typename count_t> template<typename count_t>
const uint8_t * SerialBufferAdapter<count_t>::getConstBuffer() { const uint8_t * SerialBufferAdapter<count_t>::getConstBuffer() {
if(constBuffer == nullptr) { if(constBuffer == nullptr) {
error << "Wrong access function for stored type ! Use getBuffer()" << std::endl; error << "Wrong access function for stored type !"
" Use getBuffer()" << std::endl;
return nullptr; return nullptr;
} }
return constBuffer; return constBuffer;
} }
template<typename count_t> template<typename count_t>
void SerialBufferAdapter<count_t>::setBuffer(void * buffer, count_t buffer_length) { void SerialBufferAdapter<count_t>::setBuffer(void * buffer,
count_t buffer_length) {
m_buffer = static_cast<uint8_t *>(buffer); m_buffer = static_cast<uint8_t *>(buffer);
bufferLength = buffer_length; bufferLength = buffer_length;
} }

View File

@ -8,11 +8,13 @@
* This adapter provides an interface for SerializeIF to serialize or deserialize * This adapter provides an interface for SerializeIF to serialize or deserialize
* buffers with no length header but a known size. * buffers with no length header but a known size.
* *
* Additionally, the buffer length can be serialized too and will be put in front of the serialized buffer. * Additionally, the buffer length can be serialized too and will be put in
* front of the serialized buffer.
* *
* Can be used with SerialLinkedListAdapter by declaring a SerializeElement with * Can be used with SerialLinkedListAdapter by declaring a SerializeElement with
* SerialElement<SerialBufferAdapter<bufferLengthType(will be uint8_t mostly)>> serialBufferElement. * SerialElement<SerialBufferAdapter<bufferLengthType(will be uint8_t mostly)>>.
* Right now, the SerialBufferAdapter must always be initialized with the buffer and size ! * Right now, the SerialBufferAdapter must always
* be initialized with the buffer and size !
* *
* \ingroup serialize * \ingroup serialize
*/ */
@ -27,26 +29,40 @@ public:
* @param bufferLength * @param bufferLength
* @param serializeLength * @param serializeLength
*/ */
SerialBufferAdapter(const void* buffer, count_t bufferLength, bool serializeLength = false); SerialBufferAdapter(const void* buffer, count_t bufferLength,
bool serializeLength = false);
/** /**
* Constructor for non-constant uint8_t buffer. Length field can be serialized optionally. * Constructor for non-constant uint8_t buffer.
* Length field can be serialized optionally.
* Type of length can be supplied as template type. * Type of length can be supplied as template type.
* @param buffer * @param buffer
* @param bufferLength * @param bufferLength
* @param serializeLength * @param serializeLength Length field will be serialized with size count_t
*/ */
SerialBufferAdapter(void* buffer, count_t bufferLength, bool serializeLength = false); SerialBufferAdapter(void* buffer, count_t bufferLength, bool serializeLength = false);
virtual ~SerialBufferAdapter(); virtual ~SerialBufferAdapter();
virtual ReturnValue_t serialize(uint8_t** buffer, size_t* size, virtual ReturnValue_t serialize(uint8_t** buffer, size_t* size,
const size_t max_size, bool bigEndian) const; const size_t max_size, bool bigEndian) const override;
virtual size_t getSerializedSize() const; virtual size_t getSerializedSize() const;
virtual ReturnValue_t deSerialize(const uint8_t** buffer, ssize_t* size, /**
bool bigEndian); * @brief This function deserializes a buffer into the member buffer.
* @details
* If a length field is present, it is ignored, as the size should have
* been set in the constructor. If the size is not known beforehand,
* consider using SerialFixedArrayListAdapter instead.
* @param buffer [out] Resulting buffer
* @param size remaining size to deserialize, should be larger than buffer
* + size field size
* @param bigEndian
* @return
*/
virtual ReturnValue_t deSerialize(const uint8_t** buffer, size_t* size,
bool bigEndian) override;
uint8_t * getBuffer(); uint8_t * getBuffer();
const uint8_t * getConstBuffer(); const uint8_t * getConstBuffer();
@ -58,6 +74,4 @@ private:
count_t bufferLength = 0; count_t bufferLength = 0;
}; };
#endif /* SERIALBUFFERADAPTER_H_ */ #endif /* SERIALBUFFERADAPTER_H_ */

View File

@ -5,49 +5,62 @@
#include <framework/serialize/SerialArrayListAdapter.h> #include <framework/serialize/SerialArrayListAdapter.h>
/** /**
* @brief This adapter provides an interface for SerializeIF to serialize and deserialize * @brief This adapter provides an interface for SerializeIF to serialize and
* buffers with a header containing the buffer length. * deserialize buffers with a header containing the buffer length.
* @details * @details
*
* Can be used by SerialLinkedListAdapter by declaring * Can be used by SerialLinkedListAdapter by declaring
* as a linked element with SerializeElement<SerialFixedArrayListAdapter<...>>. * as a linked element with SerializeElement<SerialFixedArrayListAdapter<...>>.
* The sequence of objects is defined in the constructor by using the setStart and setNext functions. * The sequence of objects is defined in the constructor by
* using the setStart and setNext functions.
* *
* 1. Buffers with a size header inside that class can be declared with * 1. Buffers with a size header inside that class can be declared with
* SerialFixedArrayListAdapter<BUFFER_TYPE, MAX_BUFFER_LENGTH, LENGTH_FIELD_TYPE>. * @code
* 2. MAX_BUFFER_LENGTH specifies the maximum allowed number of elements in FixedArrayList * SerialFixedArrayListAdapter<BUFFER_TYPE,
* 3. LENGTH_FIELD_TYPE specifies the data type of the buffer header containing the buffer size * MAX_BUFFER_LENGTH, LENGTH_FIELD_TYPE> mySerialFixedArrayList(...).
* (defaults to 1 byte length field) that follows * @endcode
* 2. MAX_BUFFER_LENGTH specifies the maximum allowed number of elements
* in FixedArrayList.
* 3. LENGTH_FIELD_TYPE specifies the data type of the buffer header
* containing the buffer size (defaults to 1 byte length field)
* that follows.
* *
* @ingroup serialize * @ingroup serialize
*/ */
template<typename BUFFER_TYPE, uint32_t MAX_SIZE, typename count_t = uint8_t> template<typename BUFFER_TYPE, uint32_t MAX_SIZE, typename count_t = uint8_t>
class SerialFixedArrayListAdapter : public FixedArrayList<BUFFER_TYPE, MAX_SIZE, count_t>, public SerializeIF { class SerialFixedArrayListAdapter :
public FixedArrayList<BUFFER_TYPE, MAX_SIZE, count_t>,
public SerializeIF {
public: public:
/** /**
* Constructor Arguments are forwarded to FixedArrayList constructor * Constructor Arguments are forwarded to FixedArrayList constructor.
* Refer to the fixed array list constructors for different options.
* @param args * @param args
*/ */
template<typename... Args> template<typename... Args>
SerialFixedArrayListAdapter(Args... args) : FixedArrayList<BUFFER_TYPE, MAX_SIZE, count_t>(std::forward<Args>(args)...) { SerialFixedArrayListAdapter(Args... args) :
} FixedArrayList<BUFFER_TYPE, MAX_SIZE, count_t>(std::forward<Args>(args)...)
{}
ReturnValue_t serialize(uint8_t** buffer, size_t* size, virtual ReturnValue_t serialize(uint8_t** buffer, size_t* size,
const size_t max_size, bool bigEndian) const { const size_t max_size, bool bigEndian) const override {
return SerialArrayListAdapter<BUFFER_TYPE, count_t>::serialize(this, buffer, size, max_size, bigEndian); return SerialArrayListAdapter<BUFFER_TYPE, count_t>::serialize(this,
buffer, size, max_size, bigEndian);
} }
size_t getSerializedSize() const { size_t getSerializedSize() const {
return SerialArrayListAdapter<BUFFER_TYPE, count_t>::getSerializedSize(this); return SerialArrayListAdapter<BUFFER_TYPE, count_t>::
getSerializedSize(this);
} }
ReturnValue_t deSerialize(const uint8_t** buffer, ssize_t* size,
bool bigEndian) { virtual ReturnValue_t deSerialize(const uint8_t** buffer, size_t* size,
bool bigEndian) override {
return SerialArrayListAdapter<BUFFER_TYPE, count_t>::deSerialize(this, return SerialArrayListAdapter<BUFFER_TYPE, count_t>::deSerialize(this,
buffer, size, bigEndian); buffer, size, bigEndian);
} }
void swapArrayListEndianness() { void swapArrayListEndianness() {
SerialArrayListAdapter<BUFFER_TYPE, count_t>::swapArrayListEndianness(this); SerialArrayListAdapter<BUFFER_TYPE, count_t>::
swapArrayListEndianness(this);
} }
}; };

View File

@ -18,23 +18,25 @@
* or vice-versa, using linked lists. * or vice-versa, using linked lists.
* @details * @details
* An alternative to the AutoSerializeAdapter functions * An alternative to the AutoSerializeAdapter functions
* - All object members with a datatype are declared as SerializeElement<element_type> * - All object members with a datatype are declared as
* members inside the class implementing this adapter. * SerializeElement<element_type> members inside the class
* - The element type can also be a SerialBufferAdapter to de-/serialize buffers, * implementing this adapter.
* with a known size, where the size can also be serialized * - The element type can also be a SerialBufferAdapter to
* - The element type can also be a SerialFixedArrayListAdapter to de-/serialize buffers * de-/serialize buffers with a known size
* with a size header, which is scanned automatically * - The element type can also be a SerialFixedArrayListAdapter to
* de-/serialize buffers with a size header, which is scanned automatically.
* *
* The sequence of objects is defined in the constructor by using * The sequence of objects is defined in the constructor by using
* the setStart and setNext functions. * the setStart and setNext functions.
* *
* - The serialization process is done by instantiating the class and * 1. The serialization process is done by instantiating the class and
* calling serializ after all SerializeElement entries have been set by * calling serialize after all SerializeElement entries have been set by
* using the constructor or setter functions. An additional size variable can be supplied * using the constructor or setter functions. An additional size variable
* which is calculated/incremented automatically * can be supplied which is calculated/incremented automatically.
* - The deserialization process is done by instantiating the class and supplying * 2. The deserialization process is done by instantiating the class and
* a buffer with the data which is converted into an object. The size of * supplying a buffer with the data which is converted into an object.
* data to serialize can be supplied and is decremented in the function * The size of data to serialize can be supplied and is
* decremented in the function. Range checking is done internally.
* *
* @ingroup serialize * @ingroup serialize
*/ */
@ -55,10 +57,12 @@ public:
bool printCount = false) : bool printCount = false) :
SinglyLinkedList<T>(start), printCount(printCount) { SinglyLinkedList<T>(start), printCount(printCount) {
} }
SerialLinkedListAdapter(LinkedElement<T>* first, bool printCount = false) : SerialLinkedListAdapter(LinkedElement<T>* first, bool printCount = false) :
SinglyLinkedList<T>(first), printCount(printCount) { SinglyLinkedList<T>(first), printCount(printCount) {
} }
SerialLinkedListAdapter(bool printCount = false) : SerialLinkedListAdapter(bool printCount = false) :
SinglyLinkedList<T>(), printCount(printCount) { SinglyLinkedList<T>(), printCount(printCount) {
} }
@ -66,15 +70,16 @@ public:
/** /**
* Serialize object implementing this adapter into the supplied buffer * Serialize object implementing this adapter into the supplied buffer
* and calculate the serialized size * and calculate the serialized size
* @param buffer [out] Object is serialized into this buffer. Note that the buffer pointer * @param buffer [out] Object is serialized into this buffer.
* *buffer is incremented automatically inside the respective serialize functions * Note that the buffer pointer *buffer is incremented automatically
* inside the respective serialize functions
* @param size [out] Calculated serialized size. Don't forget to set to 0. * @param size [out] Calculated serialized size. Don't forget to set to 0.
* @param max_size * @param max_size
* @param bigEndian Specify endianness * @param bigEndian Specify endianness
* @return * @return
*/ */
virtual ReturnValue_t serialize(uint8_t** buffer, size_t* size, virtual ReturnValue_t serialize(uint8_t** buffer, size_t* size,
const size_t max_size, bool bigEndian) const { const size_t max_size, bool bigEndian) const override{
if (printCount) { if (printCount) {
count_t mySize = SinglyLinkedList<T>::getSize(); count_t mySize = SinglyLinkedList<T>::getSize();
ReturnValue_t result = SerializeAdapter<count_t>::serialize(&mySize, ReturnValue_t result = SerializeAdapter<count_t>::serialize(&mySize,
@ -98,7 +103,9 @@ public:
} }
return result; return result;
} }
virtual size_t getSerializedSize() const {
virtual size_t getSerializedSize() const override {
if (printCount) { if (printCount) {
return SerialLinkedListAdapter<T>::getSerializedSize() return SerialLinkedListAdapter<T>::getSerializedSize()
+ sizeof(count_t); + sizeof(count_t);
@ -106,6 +113,7 @@ public:
return getSerializedSize(SinglyLinkedList<T>::start); return getSerializedSize(SinglyLinkedList<T>::start);
} }
} }
static uint32_t getSerializedSize(const LinkedElement<T> *element) { static uint32_t getSerializedSize(const LinkedElement<T> *element) {
uint32_t size = 0; uint32_t size = 0;
while (element != NULL) { while (element != NULL) {
@ -115,20 +123,22 @@ public:
return size; return size;
} }
/** /**
* Deserialize supplied buffer with supplied size into object implementing this adapter * Deserialize supplied buffer with supplied size into object
* implementing this adapter.
* @param buffer * @param buffer
* @param size Decremented in respective deSerialize functions automatically * @param size Decremented in respective deSerialize functions automatically
* @param bigEndian Specify endianness * @param bigEndian Specify endianness
* @return * @return
*/ */
virtual ReturnValue_t deSerialize(const uint8_t** buffer, ssize_t* size, virtual ReturnValue_t deSerialize(const uint8_t** buffer, size_t* size,
bool bigEndian) { bool bigEndian) override {
return deSerialize(SinglyLinkedList<T>::start, buffer, size, bigEndian); return deSerialize(SinglyLinkedList<T>::start, buffer, size, bigEndian);
} }
static ReturnValue_t deSerialize(LinkedElement<T>* element, static ReturnValue_t deSerialize(LinkedElement<T>* element,
const uint8_t** buffer, ssize_t* size, bool bigEndian) { const uint8_t** buffer, size_t* size, bool bigEndian) {
ReturnValue_t result = HasReturnvaluesIF::RETURN_OK; ReturnValue_t result = HasReturnvaluesIF::RETURN_OK;
while ((result == HasReturnvaluesIF::RETURN_OK) && (element != NULL)) { while ((result == HasReturnvaluesIF::RETURN_OK) && (element != NULL)) {
result = element->value->deSerialize(buffer, size, bigEndian); result = element->value->deSerialize(buffer, size, bigEndian);
@ -138,7 +148,6 @@ public:
} }
bool printCount; bool printCount;
}; };
#endif /* SERIALLINKEDLISTADAPTER_H_ */ #endif /* SERIALLINKEDLISTADAPTER_H_ */

View File

@ -1,135 +1,86 @@
#ifndef SERIALIZEADAPTER_H_ #ifndef SERIALIZEADAPTER_H_
#define SERIALIZEADAPTER_H_ #define SERIALIZEADAPTER_H_
#include <framework/container/IsDerivedFrom.h>
#include <framework/returnvalues/HasReturnvaluesIF.h> #include <framework/returnvalues/HasReturnvaluesIF.h>
#include <framework/serialize/EndianSwapper.h>
#include <framework/serialize/SerializeIF.h> #include <framework/serialize/SerializeIF.h>
#include <string.h> #include <framework/serialize/SerializeAdapterInternal.h>
#include <type_traits>
/** /**
* @brief This adapter provides an interface to use the SerializeIF functions * @brief These adapters provides an interface to use the SerializeIF functions
* with arbitrary template objects to facilitate and simplify the serialization of classes * with arbitrary template objects to facilitate and simplify the
* with different multiple different data types into buffers and vice-versa. * serialization of classes with different multiple different data types
* into buffers and vice-versa.
* @details * @details
* Examples:
* A report class is converted into a TM buffer. The report class implements a 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 * A report class is converted into a TM buffer. The report class implements a
* to retrieve data out of a buffer directly into a class variable with data type T while being able to specify endianness. * serialize functions and calls the AutoSerializeAdapter::serialize function
* The boolean bigEndian specifies whether an endian swap is performed on the data before * 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. * serialization or deserialization.
* *
* If the target architecture is little endian (ARM), any data types created might
* have the wrong endiness if they are to be used for the FSFW.
* There are three ways to retrieve data out of a buffer to be used in the FSFW * There are three ways to retrieve data out of a buffer to be used in the FSFW
* to use regular aligned (big endian) data. * to use regular aligned (big endian) data. Examples:
* This can also be applied to uint32_t and uint64_t:
* *
* 1. Use the AutoSerializeAdapter::deSerialize function * 1. Use the AutoSerializeAdapter::deSerialize function
* The pointer *buffer will be incremented automatically by the typeSize of the object, * The pointer *buffer will be incremented automatically by the typeSize
* so this function can be called on &buffer repeatedly without adjusting pointer position. * of the object, so this function can be called on &buffer repeatedly
* Set bigEndian parameter to true to perform endian swapping. * without adjusting pointer position. Set bigEndian parameter to true
* * to perform endian swapping, if necessary
* @code
* uint16_t data; * uint16_t data;
* int32_t dataLen = sizeof(data); * int32_t dataLen = sizeof(data);
* ReturnValue_t result = AutoSerializeAdapter::deSerialize(&data,&buffer,&dataLen,true); * ReturnValue_t result =
* * AutoSerializeAdapter::deSerialize(&data,&buffer,&dataLen,true);
* 2. Perform a bitshift operation. Perform endian swapping if necessary: * @endcode
* *
* 2. Perform a bitshift operation. Watch for for endianness:
* @code
* uint16_t data; * uint16_t data;
* data = buffer[targetByte1] << 8 | buffer[targetByte2]; * data = buffer[targetByte1] << 8 | buffer[targetByte2];
* data = EndianSwapper::swap(data); * data = EndianSwapper::swap(data); //optional, or swap order above
* * @endcode
* 3. Memcpy can be used when data is little-endian. Perform endian-swapping if necessary.
* *
* 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; * uint16_t data;
* memcpy(&data,buffer + positionOfTargetByte1,sizeof(data)); * memcpy(&data,buffer + positionOfTargetByte1,sizeof(data));
* data = EndianSwapper::swap(data); * data = EndianSwapper::swap(data); //optional
* @endcode
* *
* When serializing for downlink, the packets are generally serialized assuming big endian data format * When serializing for downlink, the packets are generally serialized assuming
* like seen in TmPacketStored.cpp for example. * big endian data format like seen in TmPacketStored.cpp for example.
* *
* @ingroup serialize * @ingroup serialize
*/ */
template<typename T, int>
class SerializeAdapter_ { // No type specification necessary here.
class AutoSerializeAdapter {
public: public:
template<typename T>
static ReturnValue_t serialize(const T* object, uint8_t** buffer, static ReturnValue_t serialize(const T* object, uint8_t** buffer,
size_t* size, const size_t max_size, bool bigEndian) { size_t* size, const size_t max_size, bool bigEndian) {
size_t ignoredSize = 0; SerializeAdapter_<T, IsDerivedFrom<T, SerializeIF>::Is> adapter;
if (size == NULL) { return adapter.serialize(object, buffer, size, max_size, bigEndian);
size = &ignoredSize;
}
if (sizeof(T) + *size <= max_size) {
T tmp;
if (bigEndian) {
tmp = EndianSwapper::swap<T>(*object);
} else {
tmp = *object;
}
memcpy(*buffer, &tmp, sizeof(T));
*size += sizeof(T);
(*buffer) += sizeof(T);
return HasReturnvaluesIF::RETURN_OK;
} else {
return SerializeIF::BUFFER_TOO_SHORT;
}
} }
template<typename T>
/** static size_t getSerializedSize(const T* object) {
* Deserialize buffer into object SerializeAdapter_<T, IsDerivedFrom<T, SerializeIF>::Is> adapter;
* @param object [out] Object to be deserialized with buffer data return adapter.getSerializedSize(object);
* @param buffer buffer containing the data. Non-Const pointer to non-const pointer to const buffer.
* @param size int32_t type to allow value to be values smaller than 0, needed for range/size checking
* @param bigEndian Specify endianness
* @return
*/
ReturnValue_t deSerialize(T* object, const uint8_t** buffer, ssize_t* size,
bool bigEndian) {
T tmp;
*size -= sizeof(T);
if (*size >= 0) {
memcpy(&tmp, *buffer, sizeof(T));
if (bigEndian) {
*object = EndianSwapper::swap<T>(tmp);
} else {
*object = tmp;
}
*buffer += sizeof(T);
return HasReturnvaluesIF::RETURN_OK;
} else {
return SerializeIF::STREAM_TOO_SHORT;
}
} }
template<typename T>
uint32_t getSerializedSize(const T * object) { static ReturnValue_t deSerialize(T* object, const uint8_t** buffer,
return sizeof(T); size_t* size, bool bigEndian) {
} SerializeAdapter_<T, IsDerivedFrom<T, SerializeIF>::Is> adapter;
return adapter.deSerialize(object, buffer, size, bigEndian);
};
template<typename T>
class SerializeAdapter_<T, 1> {
public:
ReturnValue_t serialize(const T* object, uint8_t** buffer, size_t* size,
const size_t max_size, bool bigEndian) const {
size_t ignoredSize = 0;
if (size == NULL) {
size = &ignoredSize;
}
return object->serialize(buffer, size, max_size, bigEndian);
}
uint32_t getSerializedSize(const T* object) const {
return object->getSerializedSize();
}
ReturnValue_t deSerialize(T* object, const uint8_t** buffer, ssize_t* size,
bool bigEndian) {
return object->deSerialize(buffer, size, bigEndian);
} }
}; };
@ -147,29 +98,7 @@ public:
} }
static ReturnValue_t deSerialize(T* object, const uint8_t** buffer, static ReturnValue_t deSerialize(T* object, const uint8_t** buffer,
ssize_t* size, bool bigEndian) { size_t* size, bool bigEndian) {
SerializeAdapter_<T, IsDerivedFrom<T, SerializeIF>::Is> adapter;
return adapter.deSerialize(object, buffer, size, bigEndian);
}
};
// No type specification necessary here.
class AutoSerializeAdapter {
public:
template<typename T>
static ReturnValue_t serialize(const T* object, uint8_t** buffer,
size_t* size, const size_t max_size, bool bigEndian) {
SerializeAdapter_<T, IsDerivedFrom<T, SerializeIF>::Is> adapter;
return adapter.serialize(object, buffer, size, max_size, bigEndian);
}
template<typename T>
static uint32_t getSerializedSize(const T* object) {
SerializeAdapter_<T, IsDerivedFrom<T, SerializeIF>::Is> adapter;
return adapter.getSerializedSize(object);
}
template<typename T>
static ReturnValue_t deSerialize(T* object, const uint8_t** buffer,
ssize_t* size, bool bigEndian) {
SerializeAdapter_<T, IsDerivedFrom<T, SerializeIF>::Is> adapter; SerializeAdapter_<T, IsDerivedFrom<T, SerializeIF>::Is> adapter;
return adapter.deSerialize(object, buffer, size, bigEndian); return adapter.deSerialize(object, buffer, size, bigEndian);
} }

View File

@ -0,0 +1,118 @@
/**
* @file SerializeAdapterInternal.h
*
* @date 13.04.2020
* @author R. Mueller
*/
#ifndef FRAMEWORK_SERIALIZE_SERIALIZEADAPTERINTERNAL_H_
#define FRAMEWORK_SERIALIZE_SERIALIZEADAPTERINTERNAL_H_
#include <framework/returnvalues/HasReturnvaluesIF.h>
#include <framework/container/IsDerivedFrom.h>
#include <framework/serialize/EndianSwapper.h>
/**
* This template specialization will be chosen for fundamental types or
* anything else not implementing SerializeIF, based on partial
* template specialization.
* @tparam T
* @tparam
*/
template<typename T, int>
class SerializeAdapter_ {
public:
/**
*
* @param object
* @param buffer
* @param size
* @param max_size
* @param bigEndian
* @return
*/
static ReturnValue_t serialize(const T* object, uint8_t** buffer,
size_t* size, const size_t max_size, bool bigEndian) {
// function eventuelly serializes structs here.
// does this work on every architecture?
// static_assert(std::is_fundamental<T>::value);
size_t ignoredSize = 0;
if (size == nullptr) {
size = &ignoredSize;
}
if (sizeof(T) + *size <= max_size) {
T tmp;
if (bigEndian) {
tmp = EndianSwapper::swap<T>(*object);
} else {
tmp = *object;
}
memcpy(*buffer, &tmp, sizeof(T));
*size += sizeof(T);
(*buffer) += sizeof(T);
return HasReturnvaluesIF::RETURN_OK;
} else {
return SerializeIF::BUFFER_TOO_SHORT;
}
}
/**
* Deserialize buffer into object
* @param object [out] Object to be deserialized with buffer data
* @param buffer contains the data. Non-Const pointer to non-const
* pointer to const data.
* @param size Size to deSerialize. wil be decremented by sizeof(T)
* @param bigEndian Specify endianness
* @return
*/
ReturnValue_t deSerialize(T* object, const uint8_t** buffer, size_t* size,
bool bigEndian) {
T tmp;
if (*size >= sizeof(T)) {
*size -= sizeof(T);
memcpy(&tmp, *buffer, sizeof(T));
if (bigEndian) {
*object = EndianSwapper::swap<T>(tmp);
} else {
*object = tmp;
}
*buffer += sizeof(T);
return HasReturnvaluesIF::RETURN_OK;
} else {
return SerializeIF::STREAM_TOO_SHORT;
}
}
size_t getSerializedSize(const T * object) {
return sizeof(T);
}
};
/**
* This template specialization will be chosen for class derived from
* SerializeIF, based on partial template specialization.
* @tparam T
* @tparam
*/
template<typename T>
class SerializeAdapter_<T, true> {
public:
ReturnValue_t serialize(const T* object, uint8_t** buffer, size_t* size,
const size_t max_size, bool bigEndian) const {
size_t ignoredSize = 0;
if (size == NULL) {
size = &ignoredSize;
}
return object->serialize(buffer, size, max_size, bigEndian);
}
size_t getSerializedSize(const T* object) const {
return object->getSerializedSize();
}
ReturnValue_t deSerialize(T* object, const uint8_t** buffer, size_t* size,
bool bigEndian) {
return object->deSerialize(buffer, size, bigEndian);
}
};
#endif /* FRAMEWORK_SERIALIZE_SERIALIZEADAPTERINTERNAL_H_ */

View File

@ -24,25 +24,26 @@ public:
* @param args * @param args
*/ */
template<typename... Args> template<typename... Args>
SerializeElement(Args... args) : LinkedElement<SerializeIF>(this), entry(std::forward<Args>(args)...) { SerializeElement(Args... args):
LinkedElement<SerializeIF>(this),
entry(std::forward<Args>(args)...) {}
} SerializeElement() : LinkedElement<SerializeIF>(this) {}
SerializeElement() : LinkedElement<SerializeIF>(this) {
}
T entry; T entry;
ReturnValue_t serialize(uint8_t** buffer, size_t* size, virtual ReturnValue_t serialize(uint8_t** buffer, size_t* size,
const size_t max_size, bool bigEndian) const { const size_t max_size, bool bigEndian) const override {
return SerializeAdapter<T>::serialize(&entry, buffer, size, max_size, bigEndian); return SerializeAdapter<T>::serialize(&entry, buffer, size,
max_size, bigEndian);
} }
size_t getSerializedSize() const { size_t getSerializedSize() const {
return SerializeAdapter<T>::getSerializedSize(&entry); return SerializeAdapter<T>::getSerializedSize(&entry);
} }
virtual ReturnValue_t deSerialize(const uint8_t** buffer, ssize_t* size, virtual ReturnValue_t deSerialize(const uint8_t** buffer, size_t* size,
bool bigEndian) { bool bigEndian) override {
return SerializeAdapter<T>::deSerialize(&entry, buffer, size, bigEndian); return SerializeAdapter<T>::deSerialize(&entry, buffer, size, bigEndian);
} }

View File

@ -3,11 +3,6 @@
#include <framework/returnvalues/HasReturnvaluesIF.h> #include <framework/returnvalues/HasReturnvaluesIF.h>
#include <cstddef> #include <cstddef>
#include <type_traits>
#ifndef ssize_t
typedef std::make_signed<std::size_t>::type ssize_t;
#endif
/** /**
* @defgroup serialize Serialization * @defgroup serialize Serialization
@ -18,17 +13,21 @@ typedef std::make_signed<std::size_t>::type ssize_t;
* @brief An interface for alle classes which require * @brief An interface for alle classes which require
* translation of objects data into data streams and vice-versa. * translation of objects data into data streams and vice-versa.
* @details * @details
* If the target architecture is little endian (e.g. ARM), any data types created might * If the target architecture is little endian (e.g. ARM), any data types
* have the wrong endiness if they are to be used for the FSFW. * created might have the wrong endianess if they are to be used for the FSFW.
* There are three ways to retrieve data out of a buffer to be used in the FSFW to use * Depending on the system architecture, endian correctness must be assured,
* regular aligned (big endian) data. This can also be applied to uint32_t and uint64_t: * This is important for incoming and outgoing data. The internal handling
* of data should be performed in the native system endianness.
* There are three ways to copy data (with different options to ensure
* endian correctness):
* *
* 1. Use the @c AutoSerializeAdapter::deSerialize function with @c bigEndian = true * 1. Use the @c AutoSerializeAdapter::deSerialize function (with
* 2. Perform a bitshift operation * the endian flag)
* 3. @c memcpy can be used when data is in little-endian format. Otherwise, @c EndianSwapper has to be used in conjuction. * 2. Perform a bitshift operation (with correct order)
* 3. @c memcpy (with @c EndianSwapper if necessary)
* *
* When serializing for downlink, the packets are generally serialized assuming big endian data format * When serializing for downlink, the packets are generally serialized
* like seen in TmPacketStored.cpp for example. * assuming big endian data format like seen in TmPacketStored.cpp for example.
* *
* @ingroup serialize * @ingroup serialize
*/ */
@ -47,7 +46,7 @@ public:
virtual size_t getSerializedSize() const = 0; virtual size_t getSerializedSize() const = 0;
virtual ReturnValue_t deSerialize(const uint8_t** buffer, ssize_t* size, virtual ReturnValue_t deSerialize(const uint8_t** buffer, size_t* size,
bool bigEndian) = 0; bool bigEndian) = 0;
}; };