Merge branch 'mueller_framework' of https://egit.irs.uni-stuttgart.de/KSat/fsfw into mueller_framework

This commit is contained in:
Robin Müller 2020-04-16 09:30:26 +02:00
commit 7c360be1e0
10 changed files with 117 additions and 89 deletions

View File

@ -12,14 +12,26 @@ class FixedArrayList: public ArrayList<T, count_t> {
private: private:
T data[MAX_SIZE]; T data[MAX_SIZE];
public: public:
/**
* (Robin) Maybe we should also implement move assignment and move ctor.
* Or at least delete them.
*/
FixedArrayList() : FixedArrayList() :
ArrayList<T, count_t>(data, MAX_SIZE) { ArrayList<T, count_t>(data, MAX_SIZE) {
} }
//We could create a constructor to initialize the fixed array list with data and the known size field // (Robin): We could create a constructor to initialize the fixed array list with data and the known size field
//so it can be used for serialization too (with SerialFixedArrrayListAdapter) // so it can be used for serialization too (with SerialFixedArrrayListAdapter)
//is this feasible? // is this feasible?
FixedArrayList(T * data_, count_t count, bool swapArrayListEndianess = false): /**
* Initialize a fixed array list with data and number of data fields.
* Endianness of entries can be swapped optionally.
* @param data_
* @param count
* @param swapArrayListEndianess
*/
FixedArrayList(T * data_, count_t count,
bool swapArrayListEndianess = false):
ArrayList<T, count_t>(data, MAX_SIZE) { ArrayList<T, count_t>(data, MAX_SIZE) {
memcpy(this->data, data_, count * sizeof(T)); memcpy(this->data, data_, count * sizeof(T));
this->size = count; this->size = count;

View File

@ -13,6 +13,12 @@ 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
@ -31,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
@ -47,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);
} }
@ -57,7 +57,7 @@ public:
} }
virtual ReturnValue_t deSerialize(const uint8_t** buffer, size_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);
} }

View File

@ -45,7 +45,7 @@ public:
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;
@ -62,7 +62,7 @@ public:
* @return * @return
*/ */
virtual ReturnValue_t deSerialize(const uint8_t** buffer, size_t* size, virtual ReturnValue_t deSerialize(const uint8_t** buffer, size_t* size,
bool bigEndian); bool bigEndian) override;
uint8_t * getBuffer(); uint8_t * getBuffer();
const uint8_t * getConstBuffer(); const uint8_t * getConstBuffer();

View File

@ -5,19 +5,24 @@
#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
*/ */
@ -27,7 +32,8 @@ class SerialFixedArrayListAdapter :
public SerializeIF { 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>
@ -35,8 +41,8 @@ public:
FixedArrayList<BUFFER_TYPE, MAX_SIZE, count_t>(std::forward<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, return SerialArrayListAdapter<BUFFER_TYPE, count_t>::serialize(this,
buffer, size, max_size, bigEndian); buffer, size, max_size, bigEndian);
} }
@ -45,8 +51,9 @@ public:
return SerialArrayListAdapter<BUFFER_TYPE, count_t>:: return SerialArrayListAdapter<BUFFER_TYPE, count_t>::
getSerializedSize(this); getSerializedSize(this);
} }
ReturnValue_t deSerialize(const uint8_t** buffer, size_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);
} }

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,15 +123,17 @@ 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, size_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);
} }
@ -138,7 +148,6 @@ public:
} }
bool printCount; bool printCount;
}; };
#endif /* SERIALLINKEDLISTADAPTER_H_ */ #endif /* SERIALLINKEDLISTADAPTER_H_ */

View File

@ -12,8 +12,9 @@
#include <framework/serialize/EndianSwapper.h> #include <framework/serialize/EndianSwapper.h>
/** /**
* This template specialization will be chosen for fundamental types, * This template specialization will be chosen for fundamental types or
* based on partial template specialization * anything else not implementing SerializeIF, based on partial
* template specialization.
* @tparam T * @tparam T
* @tparam * @tparam
*/ */

View File

@ -1,16 +0,0 @@
/**
* @page serialPage Serialization Tools
* This page refers to the serialization tools included in framework/serialize.
* The serialization tools are a useful tool to convert object data into raw
* buffers and vice-versa. Here is a rough overview which tool to use for
* which purpose.
*
* @section endSwapper Endian Swapper
* This header file includes tools to do simple serial order swapping
*
* @section serialiezIF SerializeIF
*
* @section serElement SerializeElement
*
*/

View File

@ -24,17 +24,18 @@ 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 {
@ -42,7 +43,7 @@ public:
} }
virtual ReturnValue_t deSerialize(const uint8_t** buffer, size_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
*/ */