renormalized line endings
This commit is contained in:
@ -1,124 +1,124 @@
|
||||
#ifndef ENDIANSWAPPER_H_
|
||||
#define ENDIANSWAPPER_H_
|
||||
|
||||
#include "../osal/Endiness.h"
|
||||
#include <cstring>
|
||||
#include <iostream>
|
||||
|
||||
/**
|
||||
* Helper class to convert variables or bitstreams between machine
|
||||
* endian and either big or little endian.
|
||||
* Machine endian is the endianness used by the machine running the
|
||||
* program and is one of big or little endian. As this is portable
|
||||
* code, it is not known at coding time which it is. At compile time
|
||||
* it is however, which is why this is implemented using compiler
|
||||
* macros and translates to a copy operation at runtime.
|
||||
*
|
||||
* This changes the layout of multi-byte variables in the machine's
|
||||
* memory. In most cases, you should not need to use this class.
|
||||
* Probably what you are looking for is the SerializeAdapter.
|
||||
* If you still decide you need this class, please read and understand
|
||||
* the code first.
|
||||
*
|
||||
* The order of the individual bytes of the multi-byte variable is
|
||||
* reversed, the byte at the highest address is moved to the lowest
|
||||
* address and vice versa, same for the bytes in between.
|
||||
*
|
||||
* Note that the conversion is also its inversion, that is converting
|
||||
* from machine to a specified endianness is the same operation as
|
||||
* converting from specified to machine (I looked it up, mathematicians
|
||||
* would call it an involution):
|
||||
*
|
||||
* X == convertBigEndian(convertBigEndian(X))
|
||||
*
|
||||
* Thus, there is only one function supplied to do the conversion.
|
||||
*/
|
||||
class EndianConverter {
|
||||
private:
|
||||
EndianConverter() {};
|
||||
public:
|
||||
/**
|
||||
* Convert a typed variable between big endian and machine endian.
|
||||
* Intended for plain old datatypes.
|
||||
*/
|
||||
template<typename T>
|
||||
static T convertBigEndian(T in) {
|
||||
#ifndef BYTE_ORDER_SYSTEM
|
||||
#error BYTE_ORDER_SYSTEM not defined
|
||||
#elif BYTE_ORDER_SYSTEM == LITTLE_ENDIAN
|
||||
T tmp;
|
||||
uint8_t *pointerOut = (uint8_t*) &tmp;
|
||||
uint8_t *pointerIn = (uint8_t*) ∈
|
||||
for (size_t count = 0; count < sizeof(T); count++) {
|
||||
pointerOut[sizeof(T) - count - 1] = pointerIn[count];
|
||||
}
|
||||
return tmp;
|
||||
#elif BYTE_ORDER_SYSTEM == BIG_ENDIAN
|
||||
return in;
|
||||
#else
|
||||
#error Unknown Byte Order
|
||||
#endif
|
||||
}
|
||||
|
||||
/**
|
||||
* convert a bytestream representing a single variable between big endian
|
||||
* and machine endian.
|
||||
*/
|
||||
static void convertBigEndian(uint8_t *out, const uint8_t *in,
|
||||
size_t size) {
|
||||
#ifndef BYTE_ORDER_SYSTEM
|
||||
#error BYTE_ORDER_SYSTEM not defined
|
||||
#elif BYTE_ORDER_SYSTEM == LITTLE_ENDIAN
|
||||
for (size_t count = 0; count < size; count++) {
|
||||
out[size - count - 1] = in[count];
|
||||
}
|
||||
return;
|
||||
#elif BYTE_ORDER_SYSTEM == BIG_ENDIAN
|
||||
memcpy(out, in, size);
|
||||
return;
|
||||
#endif
|
||||
}
|
||||
|
||||
/**
|
||||
* Convert a typed variable between little endian and machine endian.
|
||||
* Intended for plain old datatypes.
|
||||
*/
|
||||
template<typename T>
|
||||
static T convertLittleEndian(T in) {
|
||||
#ifndef BYTE_ORDER_SYSTEM
|
||||
#error BYTE_ORDER_SYSTEM not defined
|
||||
#elif BYTE_ORDER_SYSTEM == BIG_ENDIAN
|
||||
T tmp;
|
||||
uint8_t *pointerOut = (uint8_t *) &tmp;
|
||||
uint8_t *pointerIn = (uint8_t *) ∈
|
||||
for (size_t count = 0; count < sizeof(T); count++) {
|
||||
pointerOut[sizeof(T) - count - 1] = pointerIn[count];
|
||||
}
|
||||
return tmp;
|
||||
#elif BYTE_ORDER_SYSTEM == LITTLE_ENDIAN
|
||||
return in;
|
||||
#else
|
||||
#error Unknown Byte Order
|
||||
#endif
|
||||
}
|
||||
/**
|
||||
* convert a bytestream representing a single variable between little endian
|
||||
* and machine endian.
|
||||
*/
|
||||
static void convertLittleEndian(uint8_t *out, const uint8_t *in,
|
||||
size_t size) {
|
||||
#ifndef BYTE_ORDER_SYSTEM
|
||||
#error BYTE_ORDER_SYSTEM not defined
|
||||
#elif BYTE_ORDER_SYSTEM == BIG_ENDIAN
|
||||
for (size_t count = 0; count < size; count++) {
|
||||
out[size - count - 1] = in[count];
|
||||
}
|
||||
return;
|
||||
#elif BYTE_ORDER_SYSTEM == LITTLE_ENDIAN
|
||||
memcpy(out, in, size);
|
||||
return;
|
||||
#endif
|
||||
}
|
||||
};
|
||||
|
||||
#endif /* ENDIANSWAPPER_H_ */
|
||||
#ifndef ENDIANSWAPPER_H_
|
||||
#define ENDIANSWAPPER_H_
|
||||
|
||||
#include "../osal/Endiness.h"
|
||||
#include <cstring>
|
||||
#include <iostream>
|
||||
|
||||
/**
|
||||
* Helper class to convert variables or bitstreams between machine
|
||||
* endian and either big or little endian.
|
||||
* Machine endian is the endianness used by the machine running the
|
||||
* program and is one of big or little endian. As this is portable
|
||||
* code, it is not known at coding time which it is. At compile time
|
||||
* it is however, which is why this is implemented using compiler
|
||||
* macros and translates to a copy operation at runtime.
|
||||
*
|
||||
* This changes the layout of multi-byte variables in the machine's
|
||||
* memory. In most cases, you should not need to use this class.
|
||||
* Probably what you are looking for is the SerializeAdapter.
|
||||
* If you still decide you need this class, please read and understand
|
||||
* the code first.
|
||||
*
|
||||
* The order of the individual bytes of the multi-byte variable is
|
||||
* reversed, the byte at the highest address is moved to the lowest
|
||||
* address and vice versa, same for the bytes in between.
|
||||
*
|
||||
* Note that the conversion is also its inversion, that is converting
|
||||
* from machine to a specified endianness is the same operation as
|
||||
* converting from specified to machine (I looked it up, mathematicians
|
||||
* would call it an involution):
|
||||
*
|
||||
* X == convertBigEndian(convertBigEndian(X))
|
||||
*
|
||||
* Thus, there is only one function supplied to do the conversion.
|
||||
*/
|
||||
class EndianConverter {
|
||||
private:
|
||||
EndianConverter() {};
|
||||
public:
|
||||
/**
|
||||
* Convert a typed variable between big endian and machine endian.
|
||||
* Intended for plain old datatypes.
|
||||
*/
|
||||
template<typename T>
|
||||
static T convertBigEndian(T in) {
|
||||
#ifndef BYTE_ORDER_SYSTEM
|
||||
#error BYTE_ORDER_SYSTEM not defined
|
||||
#elif BYTE_ORDER_SYSTEM == LITTLE_ENDIAN
|
||||
T tmp;
|
||||
uint8_t *pointerOut = (uint8_t*) &tmp;
|
||||
uint8_t *pointerIn = (uint8_t*) ∈
|
||||
for (size_t count = 0; count < sizeof(T); count++) {
|
||||
pointerOut[sizeof(T) - count - 1] = pointerIn[count];
|
||||
}
|
||||
return tmp;
|
||||
#elif BYTE_ORDER_SYSTEM == BIG_ENDIAN
|
||||
return in;
|
||||
#else
|
||||
#error Unknown Byte Order
|
||||
#endif
|
||||
}
|
||||
|
||||
/**
|
||||
* convert a bytestream representing a single variable between big endian
|
||||
* and machine endian.
|
||||
*/
|
||||
static void convertBigEndian(uint8_t *out, const uint8_t *in,
|
||||
size_t size) {
|
||||
#ifndef BYTE_ORDER_SYSTEM
|
||||
#error BYTE_ORDER_SYSTEM not defined
|
||||
#elif BYTE_ORDER_SYSTEM == LITTLE_ENDIAN
|
||||
for (size_t count = 0; count < size; count++) {
|
||||
out[size - count - 1] = in[count];
|
||||
}
|
||||
return;
|
||||
#elif BYTE_ORDER_SYSTEM == BIG_ENDIAN
|
||||
memcpy(out, in, size);
|
||||
return;
|
||||
#endif
|
||||
}
|
||||
|
||||
/**
|
||||
* Convert a typed variable between little endian and machine endian.
|
||||
* Intended for plain old datatypes.
|
||||
*/
|
||||
template<typename T>
|
||||
static T convertLittleEndian(T in) {
|
||||
#ifndef BYTE_ORDER_SYSTEM
|
||||
#error BYTE_ORDER_SYSTEM not defined
|
||||
#elif BYTE_ORDER_SYSTEM == BIG_ENDIAN
|
||||
T tmp;
|
||||
uint8_t *pointerOut = (uint8_t *) &tmp;
|
||||
uint8_t *pointerIn = (uint8_t *) ∈
|
||||
for (size_t count = 0; count < sizeof(T); count++) {
|
||||
pointerOut[sizeof(T) - count - 1] = pointerIn[count];
|
||||
}
|
||||
return tmp;
|
||||
#elif BYTE_ORDER_SYSTEM == LITTLE_ENDIAN
|
||||
return in;
|
||||
#else
|
||||
#error Unknown Byte Order
|
||||
#endif
|
||||
}
|
||||
/**
|
||||
* convert a bytestream representing a single variable between little endian
|
||||
* and machine endian.
|
||||
*/
|
||||
static void convertLittleEndian(uint8_t *out, const uint8_t *in,
|
||||
size_t size) {
|
||||
#ifndef BYTE_ORDER_SYSTEM
|
||||
#error BYTE_ORDER_SYSTEM not defined
|
||||
#elif BYTE_ORDER_SYSTEM == BIG_ENDIAN
|
||||
for (size_t count = 0; count < size; count++) {
|
||||
out[size - count - 1] = in[count];
|
||||
}
|
||||
return;
|
||||
#elif BYTE_ORDER_SYSTEM == LITTLE_ENDIAN
|
||||
memcpy(out, in, size);
|
||||
return;
|
||||
#endif
|
||||
}
|
||||
};
|
||||
|
||||
#endif /* ENDIANSWAPPER_H_ */
|
||||
|
@ -1,91 +1,91 @@
|
||||
/**
|
||||
* @file SerialArrayListAdapter.h
|
||||
* @brief This file defines the SerialArrayListAdapter class.
|
||||
* @date 22.07.2014
|
||||
* @author baetz
|
||||
*/
|
||||
#ifndef SERIALARRAYLISTADAPTER_H_
|
||||
#define SERIALARRAYLISTADAPTER_H_
|
||||
|
||||
#include "../container/ArrayList.h"
|
||||
#include "../serialize/SerializeIF.h"
|
||||
#include <utility>
|
||||
|
||||
/**
|
||||
* Also serializes length field !
|
||||
* @ingroup serialize
|
||||
*/
|
||||
template<typename T, typename count_t = uint8_t>
|
||||
class SerialArrayListAdapter : public SerializeIF {
|
||||
public:
|
||||
SerialArrayListAdapter(ArrayList<T, count_t> *adaptee) : adaptee(adaptee) {
|
||||
}
|
||||
|
||||
virtual ReturnValue_t serialize(uint8_t** buffer, size_t* size,
|
||||
size_t maxSize, Endianness streamEndianness) const {
|
||||
return serialize(adaptee, buffer, size, maxSize, streamEndianness);
|
||||
}
|
||||
|
||||
static ReturnValue_t serialize(const ArrayList<T, count_t>* list, uint8_t** buffer, size_t* size,
|
||||
size_t maxSize, Endianness streamEndianness) {
|
||||
ReturnValue_t result = SerializeAdapter::serialize(&list->size,
|
||||
buffer, size, maxSize, streamEndianness);
|
||||
count_t i = 0;
|
||||
while ((result == HasReturnvaluesIF::RETURN_OK) && (i < list->size)) {
|
||||
result = SerializeAdapter::serialize(&list->entries[i], buffer, size,
|
||||
maxSize, streamEndianness);
|
||||
++i;
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
virtual size_t getSerializedSize() const {
|
||||
return getSerializedSize(adaptee);
|
||||
}
|
||||
|
||||
static uint32_t getSerializedSize(const ArrayList<T, count_t>* list) {
|
||||
uint32_t printSize = sizeof(count_t);
|
||||
count_t i = 0;
|
||||
|
||||
for (i = 0; i < list->size; ++i) {
|
||||
printSize += SerializeAdapter::getSerializedSize(&list->entries[i]);
|
||||
}
|
||||
|
||||
return printSize;
|
||||
}
|
||||
|
||||
virtual ReturnValue_t deSerialize(const uint8_t** buffer, size_t* size,
|
||||
Endianness streamEndianness) {
|
||||
return deSerialize(adaptee, buffer, size, streamEndianness);
|
||||
}
|
||||
|
||||
static ReturnValue_t deSerialize(ArrayList<T, count_t>* list, const uint8_t** buffer, size_t* size,
|
||||
Endianness streamEndianness) {
|
||||
count_t tempSize = 0;
|
||||
ReturnValue_t result = SerializeAdapter::deSerialize(&tempSize,
|
||||
buffer, size, streamEndianness);
|
||||
if (tempSize > list->maxSize()) {
|
||||
return SerializeIF::TOO_MANY_ELEMENTS;
|
||||
}
|
||||
list->size = tempSize;
|
||||
count_t i = 0;
|
||||
while ((result == HasReturnvaluesIF::RETURN_OK) && (i < list->size)) {
|
||||
result = SerializeAdapter::deSerialize(
|
||||
&list->front()[i], buffer, size,
|
||||
streamEndianness);
|
||||
++i;
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
|
||||
static void swapArrayListEndianness(ArrayList<T, count_t>* list) {
|
||||
list->swapArrayListEndianness();
|
||||
}
|
||||
private:
|
||||
ArrayList<T, count_t> *adaptee;
|
||||
};
|
||||
|
||||
|
||||
|
||||
#endif /* SERIALARRAYLISTADAPTER_H_ */
|
||||
/**
|
||||
* @file SerialArrayListAdapter.h
|
||||
* @brief This file defines the SerialArrayListAdapter class.
|
||||
* @date 22.07.2014
|
||||
* @author baetz
|
||||
*/
|
||||
#ifndef SERIALARRAYLISTADAPTER_H_
|
||||
#define SERIALARRAYLISTADAPTER_H_
|
||||
|
||||
#include "../container/ArrayList.h"
|
||||
#include "../serialize/SerializeIF.h"
|
||||
#include <utility>
|
||||
|
||||
/**
|
||||
* Also serializes length field !
|
||||
* @ingroup serialize
|
||||
*/
|
||||
template<typename T, typename count_t = uint8_t>
|
||||
class SerialArrayListAdapter : public SerializeIF {
|
||||
public:
|
||||
SerialArrayListAdapter(ArrayList<T, count_t> *adaptee) : adaptee(adaptee) {
|
||||
}
|
||||
|
||||
virtual ReturnValue_t serialize(uint8_t** buffer, size_t* size,
|
||||
size_t maxSize, Endianness streamEndianness) const {
|
||||
return serialize(adaptee, buffer, size, maxSize, streamEndianness);
|
||||
}
|
||||
|
||||
static ReturnValue_t serialize(const ArrayList<T, count_t>* list, uint8_t** buffer, size_t* size,
|
||||
size_t maxSize, Endianness streamEndianness) {
|
||||
ReturnValue_t result = SerializeAdapter::serialize(&list->size,
|
||||
buffer, size, maxSize, streamEndianness);
|
||||
count_t i = 0;
|
||||
while ((result == HasReturnvaluesIF::RETURN_OK) && (i < list->size)) {
|
||||
result = SerializeAdapter::serialize(&list->entries[i], buffer, size,
|
||||
maxSize, streamEndianness);
|
||||
++i;
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
virtual size_t getSerializedSize() const {
|
||||
return getSerializedSize(adaptee);
|
||||
}
|
||||
|
||||
static uint32_t getSerializedSize(const ArrayList<T, count_t>* list) {
|
||||
uint32_t printSize = sizeof(count_t);
|
||||
count_t i = 0;
|
||||
|
||||
for (i = 0; i < list->size; ++i) {
|
||||
printSize += SerializeAdapter::getSerializedSize(&list->entries[i]);
|
||||
}
|
||||
|
||||
return printSize;
|
||||
}
|
||||
|
||||
virtual ReturnValue_t deSerialize(const uint8_t** buffer, size_t* size,
|
||||
Endianness streamEndianness) {
|
||||
return deSerialize(adaptee, buffer, size, streamEndianness);
|
||||
}
|
||||
|
||||
static ReturnValue_t deSerialize(ArrayList<T, count_t>* list, const uint8_t** buffer, size_t* size,
|
||||
Endianness streamEndianness) {
|
||||
count_t tempSize = 0;
|
||||
ReturnValue_t result = SerializeAdapter::deSerialize(&tempSize,
|
||||
buffer, size, streamEndianness);
|
||||
if (tempSize > list->maxSize()) {
|
||||
return SerializeIF::TOO_MANY_ELEMENTS;
|
||||
}
|
||||
list->size = tempSize;
|
||||
count_t i = 0;
|
||||
while ((result == HasReturnvaluesIF::RETURN_OK) && (i < list->size)) {
|
||||
result = SerializeAdapter::deSerialize(
|
||||
&list->front()[i], buffer, size,
|
||||
streamEndianness);
|
||||
++i;
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
|
||||
static void swapArrayListEndianness(ArrayList<T, count_t>* list) {
|
||||
list->swapArrayListEndianness();
|
||||
}
|
||||
private:
|
||||
ArrayList<T, count_t> *adaptee;
|
||||
};
|
||||
|
||||
|
||||
|
||||
#endif /* SERIALARRAYLISTADAPTER_H_ */
|
||||
|
@ -1,129 +1,129 @@
|
||||
#include "../serialize/SerialBufferAdapter.h"
|
||||
#include "../serviceinterface/ServiceInterfaceStream.h"
|
||||
#include <cstring>
|
||||
|
||||
template<typename count_t>
|
||||
SerialBufferAdapter<count_t>::SerialBufferAdapter(const uint8_t* buffer,
|
||||
count_t bufferLength, bool serializeLength) :
|
||||
serializeLength(serializeLength),
|
||||
constBuffer(buffer), buffer(nullptr),
|
||||
bufferLength(bufferLength) {}
|
||||
|
||||
template<typename count_t>
|
||||
SerialBufferAdapter<count_t>::SerialBufferAdapter(uint8_t* buffer,
|
||||
count_t bufferLength, bool serializeLength) :
|
||||
serializeLength(serializeLength), constBuffer(buffer), buffer(buffer),
|
||||
bufferLength(bufferLength) {}
|
||||
|
||||
|
||||
template<typename count_t>
|
||||
SerialBufferAdapter<count_t>::~SerialBufferAdapter() {
|
||||
}
|
||||
|
||||
template<typename count_t>
|
||||
ReturnValue_t SerialBufferAdapter<count_t>::serialize(uint8_t** buffer,
|
||||
size_t* size, size_t maxSize, Endianness streamEndianness) const {
|
||||
if (serializeLength) {
|
||||
ReturnValue_t result = SerializeAdapter::serialize(&bufferLength,
|
||||
buffer, size, maxSize, streamEndianness);
|
||||
if(result != HasReturnvaluesIF::RETURN_OK) {
|
||||
return result;
|
||||
}
|
||||
}
|
||||
|
||||
if (*size + bufferLength > maxSize) {
|
||||
return BUFFER_TOO_SHORT;
|
||||
}
|
||||
|
||||
if (this->constBuffer != nullptr) {
|
||||
std::memcpy(*buffer, this->constBuffer, bufferLength);
|
||||
}
|
||||
else if (this->buffer != nullptr) {
|
||||
// This will propably be never reached, constBuffer should always be
|
||||
// set if non-const buffer is set.
|
||||
std::memcpy(*buffer, this->buffer, bufferLength);
|
||||
}
|
||||
else {
|
||||
return HasReturnvaluesIF::RETURN_FAILED;
|
||||
}
|
||||
*size += bufferLength;
|
||||
(*buffer) += bufferLength;
|
||||
return HasReturnvaluesIF::RETURN_OK;
|
||||
|
||||
}
|
||||
|
||||
template<typename count_t>
|
||||
size_t SerialBufferAdapter<count_t>::getSerializedSize() const {
|
||||
if (serializeLength) {
|
||||
return bufferLength + SerializeAdapter::getSerializedSize(&bufferLength);
|
||||
} else {
|
||||
return bufferLength;
|
||||
}
|
||||
}
|
||||
|
||||
template<typename count_t>
|
||||
ReturnValue_t SerialBufferAdapter<count_t>::deSerialize(const uint8_t** buffer,
|
||||
size_t* size, Endianness streamEndianness) {
|
||||
if (this->buffer == nullptr) {
|
||||
return HasReturnvaluesIF::RETURN_FAILED;
|
||||
}
|
||||
|
||||
if(serializeLength){
|
||||
count_t lengthField = 0;
|
||||
ReturnValue_t result = SerializeAdapter::deSerialize(&lengthField,
|
||||
buffer, size, streamEndianness);
|
||||
if(result != HasReturnvaluesIF::RETURN_OK) {
|
||||
return result;
|
||||
}
|
||||
if(lengthField > bufferLength) {
|
||||
return TOO_MANY_ELEMENTS;
|
||||
}
|
||||
bufferLength = lengthField;
|
||||
}
|
||||
|
||||
if (bufferLength <= *size) {
|
||||
*size -= bufferLength;
|
||||
std::memcpy(this->buffer, *buffer, bufferLength);
|
||||
(*buffer) += bufferLength;
|
||||
return HasReturnvaluesIF::RETURN_OK;
|
||||
}
|
||||
else {
|
||||
return STREAM_TOO_SHORT;
|
||||
}
|
||||
}
|
||||
|
||||
template<typename count_t>
|
||||
uint8_t * SerialBufferAdapter<count_t>::getBuffer() {
|
||||
if(buffer == nullptr) {
|
||||
sif::error << "Wrong access function for stored type !"
|
||||
" Use getConstBuffer()." << std::endl;
|
||||
return nullptr;
|
||||
}
|
||||
return buffer;
|
||||
}
|
||||
|
||||
template<typename count_t>
|
||||
const uint8_t * SerialBufferAdapter<count_t>::getConstBuffer() {
|
||||
if(constBuffer == nullptr) {
|
||||
sif::error << "SerialBufferAdapter::getConstBuffer:"
|
||||
" Buffers are unitialized!" << std::endl;
|
||||
return nullptr;
|
||||
}
|
||||
return constBuffer;
|
||||
}
|
||||
|
||||
template<typename count_t>
|
||||
void SerialBufferAdapter<count_t>::setBuffer(uint8_t* buffer,
|
||||
count_t bufferLength) {
|
||||
this->buffer = buffer;
|
||||
this->constBuffer = buffer;
|
||||
this->bufferLength = bufferLength;
|
||||
}
|
||||
|
||||
|
||||
//forward Template declaration for linker
|
||||
template class SerialBufferAdapter<uint8_t>;
|
||||
template class SerialBufferAdapter<uint16_t>;
|
||||
template class SerialBufferAdapter<uint32_t>;
|
||||
template class SerialBufferAdapter<uint64_t>;
|
||||
|
||||
#include "../serialize/SerialBufferAdapter.h"
|
||||
#include "../serviceinterface/ServiceInterfaceStream.h"
|
||||
#include <cstring>
|
||||
|
||||
template<typename count_t>
|
||||
SerialBufferAdapter<count_t>::SerialBufferAdapter(const uint8_t* buffer,
|
||||
count_t bufferLength, bool serializeLength) :
|
||||
serializeLength(serializeLength),
|
||||
constBuffer(buffer), buffer(nullptr),
|
||||
bufferLength(bufferLength) {}
|
||||
|
||||
template<typename count_t>
|
||||
SerialBufferAdapter<count_t>::SerialBufferAdapter(uint8_t* buffer,
|
||||
count_t bufferLength, bool serializeLength) :
|
||||
serializeLength(serializeLength), constBuffer(buffer), buffer(buffer),
|
||||
bufferLength(bufferLength) {}
|
||||
|
||||
|
||||
template<typename count_t>
|
||||
SerialBufferAdapter<count_t>::~SerialBufferAdapter() {
|
||||
}
|
||||
|
||||
template<typename count_t>
|
||||
ReturnValue_t SerialBufferAdapter<count_t>::serialize(uint8_t** buffer,
|
||||
size_t* size, size_t maxSize, Endianness streamEndianness) const {
|
||||
if (serializeLength) {
|
||||
ReturnValue_t result = SerializeAdapter::serialize(&bufferLength,
|
||||
buffer, size, maxSize, streamEndianness);
|
||||
if(result != HasReturnvaluesIF::RETURN_OK) {
|
||||
return result;
|
||||
}
|
||||
}
|
||||
|
||||
if (*size + bufferLength > maxSize) {
|
||||
return BUFFER_TOO_SHORT;
|
||||
}
|
||||
|
||||
if (this->constBuffer != nullptr) {
|
||||
std::memcpy(*buffer, this->constBuffer, bufferLength);
|
||||
}
|
||||
else if (this->buffer != nullptr) {
|
||||
// This will propably be never reached, constBuffer should always be
|
||||
// set if non-const buffer is set.
|
||||
std::memcpy(*buffer, this->buffer, bufferLength);
|
||||
}
|
||||
else {
|
||||
return HasReturnvaluesIF::RETURN_FAILED;
|
||||
}
|
||||
*size += bufferLength;
|
||||
(*buffer) += bufferLength;
|
||||
return HasReturnvaluesIF::RETURN_OK;
|
||||
|
||||
}
|
||||
|
||||
template<typename count_t>
|
||||
size_t SerialBufferAdapter<count_t>::getSerializedSize() const {
|
||||
if (serializeLength) {
|
||||
return bufferLength + SerializeAdapter::getSerializedSize(&bufferLength);
|
||||
} else {
|
||||
return bufferLength;
|
||||
}
|
||||
}
|
||||
|
||||
template<typename count_t>
|
||||
ReturnValue_t SerialBufferAdapter<count_t>::deSerialize(const uint8_t** buffer,
|
||||
size_t* size, Endianness streamEndianness) {
|
||||
if (this->buffer == nullptr) {
|
||||
return HasReturnvaluesIF::RETURN_FAILED;
|
||||
}
|
||||
|
||||
if(serializeLength){
|
||||
count_t lengthField = 0;
|
||||
ReturnValue_t result = SerializeAdapter::deSerialize(&lengthField,
|
||||
buffer, size, streamEndianness);
|
||||
if(result != HasReturnvaluesIF::RETURN_OK) {
|
||||
return result;
|
||||
}
|
||||
if(lengthField > bufferLength) {
|
||||
return TOO_MANY_ELEMENTS;
|
||||
}
|
||||
bufferLength = lengthField;
|
||||
}
|
||||
|
||||
if (bufferLength <= *size) {
|
||||
*size -= bufferLength;
|
||||
std::memcpy(this->buffer, *buffer, bufferLength);
|
||||
(*buffer) += bufferLength;
|
||||
return HasReturnvaluesIF::RETURN_OK;
|
||||
}
|
||||
else {
|
||||
return STREAM_TOO_SHORT;
|
||||
}
|
||||
}
|
||||
|
||||
template<typename count_t>
|
||||
uint8_t * SerialBufferAdapter<count_t>::getBuffer() {
|
||||
if(buffer == nullptr) {
|
||||
sif::error << "Wrong access function for stored type !"
|
||||
" Use getConstBuffer()." << std::endl;
|
||||
return nullptr;
|
||||
}
|
||||
return buffer;
|
||||
}
|
||||
|
||||
template<typename count_t>
|
||||
const uint8_t * SerialBufferAdapter<count_t>::getConstBuffer() {
|
||||
if(constBuffer == nullptr) {
|
||||
sif::error << "SerialBufferAdapter::getConstBuffer:"
|
||||
" Buffers are unitialized!" << std::endl;
|
||||
return nullptr;
|
||||
}
|
||||
return constBuffer;
|
||||
}
|
||||
|
||||
template<typename count_t>
|
||||
void SerialBufferAdapter<count_t>::setBuffer(uint8_t* buffer,
|
||||
count_t bufferLength) {
|
||||
this->buffer = buffer;
|
||||
this->constBuffer = buffer;
|
||||
this->bufferLength = bufferLength;
|
||||
}
|
||||
|
||||
|
||||
//forward Template declaration for linker
|
||||
template class SerialBufferAdapter<uint8_t>;
|
||||
template class SerialBufferAdapter<uint16_t>;
|
||||
template class SerialBufferAdapter<uint32_t>;
|
||||
template class SerialBufferAdapter<uint64_t>;
|
||||
|
||||
|
@ -1,78 +1,78 @@
|
||||
#ifndef SERIALBUFFERADAPTER_H_
|
||||
#define SERIALBUFFERADAPTER_H_
|
||||
|
||||
#include "../serialize/SerializeIF.h"
|
||||
#include "../serialize/SerializeAdapter.h"
|
||||
|
||||
/**
|
||||
* This adapter provides an interface for SerializeIF to serialize or deserialize
|
||||
* 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.
|
||||
*
|
||||
* Can be used with SerialLinkedListAdapter by declaring a SerializeElement with
|
||||
* SerialElement<SerialBufferAdapter<bufferLengthType(will be uint8_t mostly)>>.
|
||||
* Right now, the SerialBufferAdapter must always
|
||||
* be initialized with the buffer and size !
|
||||
*
|
||||
* \ingroup serialize
|
||||
*/
|
||||
template<typename count_t>
|
||||
class SerialBufferAdapter: public SerializeIF {
|
||||
public:
|
||||
|
||||
/**
|
||||
* Constructor for constant uint8_t buffer. Length field can be serialized optionally.
|
||||
* Type of length can be supplied as template type.
|
||||
* @param buffer
|
||||
* @param bufferLength
|
||||
* @param serializeLength
|
||||
*/
|
||||
SerialBufferAdapter(const uint8_t* buffer, count_t bufferLength,
|
||||
bool serializeLength = false);
|
||||
|
||||
/**
|
||||
* Constructor for non-constant uint8_t buffer.
|
||||
* Length field can be serialized optionally.
|
||||
* Type of length can be supplied as template type.
|
||||
* @param buffer
|
||||
* @param bufferLength
|
||||
* @param serializeLength Length field will be serialized with size count_t
|
||||
*/
|
||||
SerialBufferAdapter(uint8_t* buffer, count_t bufferLength,
|
||||
bool serializeLength = false);
|
||||
|
||||
virtual ~SerialBufferAdapter();
|
||||
|
||||
virtual ReturnValue_t serialize(uint8_t** buffer, size_t* size,
|
||||
size_t maxSize, Endianness streamEndianness) const override;
|
||||
|
||||
virtual size_t getSerializedSize() const override;
|
||||
|
||||
/**
|
||||
* @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,
|
||||
Endianness streamEndianness) override;
|
||||
|
||||
uint8_t * getBuffer();
|
||||
const uint8_t * getConstBuffer();
|
||||
void setBuffer(uint8_t* buffer, count_t bufferLength);
|
||||
private:
|
||||
bool serializeLength = false;
|
||||
const uint8_t *constBuffer = nullptr;
|
||||
uint8_t *buffer = nullptr;
|
||||
count_t bufferLength = 0;
|
||||
};
|
||||
|
||||
#endif /* SERIALBUFFERADAPTER_H_ */
|
||||
#ifndef SERIALBUFFERADAPTER_H_
|
||||
#define SERIALBUFFERADAPTER_H_
|
||||
|
||||
#include "../serialize/SerializeIF.h"
|
||||
#include "../serialize/SerializeAdapter.h"
|
||||
|
||||
/**
|
||||
* This adapter provides an interface for SerializeIF to serialize or deserialize
|
||||
* 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.
|
||||
*
|
||||
* Can be used with SerialLinkedListAdapter by declaring a SerializeElement with
|
||||
* SerialElement<SerialBufferAdapter<bufferLengthType(will be uint8_t mostly)>>.
|
||||
* Right now, the SerialBufferAdapter must always
|
||||
* be initialized with the buffer and size !
|
||||
*
|
||||
* \ingroup serialize
|
||||
*/
|
||||
template<typename count_t>
|
||||
class SerialBufferAdapter: public SerializeIF {
|
||||
public:
|
||||
|
||||
/**
|
||||
* Constructor for constant uint8_t buffer. Length field can be serialized optionally.
|
||||
* Type of length can be supplied as template type.
|
||||
* @param buffer
|
||||
* @param bufferLength
|
||||
* @param serializeLength
|
||||
*/
|
||||
SerialBufferAdapter(const uint8_t* buffer, count_t bufferLength,
|
||||
bool serializeLength = false);
|
||||
|
||||
/**
|
||||
* Constructor for non-constant uint8_t buffer.
|
||||
* Length field can be serialized optionally.
|
||||
* Type of length can be supplied as template type.
|
||||
* @param buffer
|
||||
* @param bufferLength
|
||||
* @param serializeLength Length field will be serialized with size count_t
|
||||
*/
|
||||
SerialBufferAdapter(uint8_t* buffer, count_t bufferLength,
|
||||
bool serializeLength = false);
|
||||
|
||||
virtual ~SerialBufferAdapter();
|
||||
|
||||
virtual ReturnValue_t serialize(uint8_t** buffer, size_t* size,
|
||||
size_t maxSize, Endianness streamEndianness) const override;
|
||||
|
||||
virtual size_t getSerializedSize() const override;
|
||||
|
||||
/**
|
||||
* @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,
|
||||
Endianness streamEndianness) override;
|
||||
|
||||
uint8_t * getBuffer();
|
||||
const uint8_t * getConstBuffer();
|
||||
void setBuffer(uint8_t* buffer, count_t bufferLength);
|
||||
private:
|
||||
bool serializeLength = false;
|
||||
const uint8_t *constBuffer = nullptr;
|
||||
uint8_t *buffer = nullptr;
|
||||
count_t bufferLength = 0;
|
||||
};
|
||||
|
||||
#endif /* SERIALBUFFERADAPTER_H_ */
|
||||
|
@ -1,63 +1,63 @@
|
||||
#ifndef SERIALFIXEDARRAYLISTADAPTER_H_
|
||||
#define SERIALFIXEDARRAYLISTADAPTER_H_
|
||||
|
||||
#include "../container/FixedArrayList.h"
|
||||
#include "../serialize/SerialArrayListAdapter.h"
|
||||
|
||||
/**
|
||||
* @brief This adapter provides an interface for SerializeIF to serialize and
|
||||
* deserialize buffers with a header containing the buffer length.
|
||||
* @details
|
||||
* Can be used by SerialLinkedListAdapter by declaring
|
||||
* as a linked element with SerializeElement<SerialFixedArrayListAdapter<...>>.
|
||||
* The sequence of objects is defined in the constructor by
|
||||
* using the setStart and setNext functions.
|
||||
*
|
||||
* - Buffers with a size header inside that class can be declared with
|
||||
* @code
|
||||
* SerialFixedArrayListAdapter<BUFFER_TYPE,
|
||||
* MAX_SIZE, count_t> mySerialFixedArrayList(...).
|
||||
* @endcode
|
||||
*
|
||||
* - MAX_SIZE: specifies the maximum allowed number of elements
|
||||
* in FixedArrayList.
|
||||
* - BUFFER_TYPE: specifies the data type of the buffer
|
||||
* - count_t: specifies the type/size of the length field
|
||||
* which defaults to one byte.
|
||||
*
|
||||
* @ingroup serialize
|
||||
*/
|
||||
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 {
|
||||
public:
|
||||
/**
|
||||
* Constructor Arguments are forwarded to FixedArrayList constructor.
|
||||
* Refer to the fixed array list constructors for different options.
|
||||
* @param args
|
||||
*/
|
||||
template<typename... Args>
|
||||
SerialFixedArrayListAdapter(Args... args) :
|
||||
FixedArrayList<BUFFER_TYPE, MAX_SIZE, count_t>(
|
||||
std::forward<Args>(args)...){}
|
||||
|
||||
ReturnValue_t serialize(uint8_t** buffer, size_t* size,
|
||||
size_t maxSize, Endianness streamEndianness) const {
|
||||
return SerialArrayListAdapter<BUFFER_TYPE, count_t>::serialize(this,
|
||||
buffer, size, maxSize, streamEndianness);
|
||||
}
|
||||
|
||||
size_t getSerializedSize() const {
|
||||
return SerialArrayListAdapter<BUFFER_TYPE, count_t>::getSerializedSize(this);
|
||||
}
|
||||
|
||||
ReturnValue_t deSerialize(const uint8_t** buffer, size_t* size,
|
||||
Endianness streamEndianness) {
|
||||
return SerialArrayListAdapter<BUFFER_TYPE, count_t>::deSerialize(this,
|
||||
buffer, size, streamEndianness);
|
||||
}
|
||||
|
||||
};
|
||||
|
||||
#endif /* SERIALFIXEDARRAYLISTADAPTER_H_ */
|
||||
#ifndef SERIALFIXEDARRAYLISTADAPTER_H_
|
||||
#define SERIALFIXEDARRAYLISTADAPTER_H_
|
||||
|
||||
#include "../container/FixedArrayList.h"
|
||||
#include "../serialize/SerialArrayListAdapter.h"
|
||||
|
||||
/**
|
||||
* @brief This adapter provides an interface for SerializeIF to serialize and
|
||||
* deserialize buffers with a header containing the buffer length.
|
||||
* @details
|
||||
* Can be used by SerialLinkedListAdapter by declaring
|
||||
* as a linked element with SerializeElement<SerialFixedArrayListAdapter<...>>.
|
||||
* The sequence of objects is defined in the constructor by
|
||||
* using the setStart and setNext functions.
|
||||
*
|
||||
* - Buffers with a size header inside that class can be declared with
|
||||
* @code
|
||||
* SerialFixedArrayListAdapter<BUFFER_TYPE,
|
||||
* MAX_SIZE, count_t> mySerialFixedArrayList(...).
|
||||
* @endcode
|
||||
*
|
||||
* - MAX_SIZE: specifies the maximum allowed number of elements
|
||||
* in FixedArrayList.
|
||||
* - BUFFER_TYPE: specifies the data type of the buffer
|
||||
* - count_t: specifies the type/size of the length field
|
||||
* which defaults to one byte.
|
||||
*
|
||||
* @ingroup serialize
|
||||
*/
|
||||
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 {
|
||||
public:
|
||||
/**
|
||||
* Constructor Arguments are forwarded to FixedArrayList constructor.
|
||||
* Refer to the fixed array list constructors for different options.
|
||||
* @param args
|
||||
*/
|
||||
template<typename... Args>
|
||||
SerialFixedArrayListAdapter(Args... args) :
|
||||
FixedArrayList<BUFFER_TYPE, MAX_SIZE, count_t>(
|
||||
std::forward<Args>(args)...){}
|
||||
|
||||
ReturnValue_t serialize(uint8_t** buffer, size_t* size,
|
||||
size_t maxSize, Endianness streamEndianness) const {
|
||||
return SerialArrayListAdapter<BUFFER_TYPE, count_t>::serialize(this,
|
||||
buffer, size, maxSize, streamEndianness);
|
||||
}
|
||||
|
||||
size_t getSerializedSize() const {
|
||||
return SerialArrayListAdapter<BUFFER_TYPE, count_t>::getSerializedSize(this);
|
||||
}
|
||||
|
||||
ReturnValue_t deSerialize(const uint8_t** buffer, size_t* size,
|
||||
Endianness streamEndianness) {
|
||||
return SerialArrayListAdapter<BUFFER_TYPE, count_t>::deSerialize(this,
|
||||
buffer, size, streamEndianness);
|
||||
}
|
||||
|
||||
};
|
||||
|
||||
#endif /* SERIALFIXEDARRAYLISTADAPTER_H_ */
|
||||
|
@ -1,133 +1,133 @@
|
||||
/**
|
||||
* @file SerialLinkedListAdapter.h
|
||||
* @brief This file defines the SerialLinkedListAdapter class.
|
||||
* @date 22.07.2014
|
||||
* @author baetz
|
||||
*/
|
||||
#ifndef SERIALLINKEDLISTADAPTER_H_
|
||||
#define SERIALLINKEDLISTADAPTER_H_
|
||||
|
||||
#include "../container/SinglyLinkedList.h"
|
||||
#include "../serialize/SerializeAdapter.h"
|
||||
#include "../serialize/SerializeElement.h"
|
||||
#include "../serialize/SerializeIF.h"
|
||||
//This is where we need the SerializeAdapter!
|
||||
|
||||
/**
|
||||
* @brief Implement the conversion of object data to data streams
|
||||
* or vice-versa, using linked lists.
|
||||
* @details
|
||||
* An alternative to the AutoSerializeAdapter functions
|
||||
* - All object members with a datatype are declared as
|
||||
* SerializeElement<element_type> members inside the class
|
||||
* implementing this adapter.
|
||||
* - The element type can also be a SerialBufferAdapter to
|
||||
* de-/serialize buffers with a known size
|
||||
* - 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 setStart and setNext functions.
|
||||
*
|
||||
* 1. The serialization process is done by instantiating the class and
|
||||
* calling serialize after all SerializeElement entries have been set by
|
||||
* using the constructor or setter functions. An additional size variable
|
||||
* can be supplied which is calculated/incremented automatically.
|
||||
* 2. The deserialization process is done by instantiating the class and
|
||||
* supplying a buffer with the data which is converted into an object.
|
||||
* The size of data to serialize can be supplied and is
|
||||
* decremented in the function. Range checking is done internally.
|
||||
*
|
||||
* @ingroup serialize
|
||||
*/
|
||||
template<typename T, typename count_t = uint8_t>
|
||||
class SerialLinkedListAdapter: public SinglyLinkedList<T>, public SerializeIF {
|
||||
public:
|
||||
/**
|
||||
* Copying is forbidden by deleting the copy constructor and the copy
|
||||
* assignment operator because of the pointers to the linked list members.
|
||||
* Unless the child class implements an own copy constructor or
|
||||
* copy assignment operator, these operation will throw a compiler error.
|
||||
* @param
|
||||
*/
|
||||
SerialLinkedListAdapter(const SerialLinkedListAdapter &) = delete;
|
||||
SerialLinkedListAdapter& operator=(const SerialLinkedListAdapter&) = delete;
|
||||
|
||||
SerialLinkedListAdapter(typename LinkedElement<T>::Iterator start,
|
||||
bool printCount = false) :
|
||||
SinglyLinkedList<T>(start), printCount(printCount) {
|
||||
}
|
||||
|
||||
SerialLinkedListAdapter(LinkedElement<T>* first, bool printCount = false) :
|
||||
SinglyLinkedList<T>(first), printCount(printCount) {
|
||||
|
||||
}
|
||||
|
||||
SerialLinkedListAdapter(bool printCount = false) :
|
||||
SinglyLinkedList<T>(), printCount(printCount) {
|
||||
}
|
||||
|
||||
virtual ReturnValue_t serialize(uint8_t** buffer, size_t* size,
|
||||
size_t maxSize, Endianness streamEndianness) const override {
|
||||
if (printCount) {
|
||||
count_t mySize = SinglyLinkedList<T>::getSize();
|
||||
ReturnValue_t result = SerializeAdapter::serialize(&mySize,
|
||||
buffer, size, maxSize, streamEndianness);
|
||||
if (result != HasReturnvaluesIF::RETURN_OK) {
|
||||
return result;
|
||||
}
|
||||
}
|
||||
return serialize(SinglyLinkedList<T>::start, buffer, size, maxSize,
|
||||
streamEndianness);
|
||||
}
|
||||
|
||||
static ReturnValue_t serialize(const LinkedElement<T>* element,
|
||||
uint8_t** buffer, size_t* size, size_t maxSize,
|
||||
Endianness streamEndianness) {
|
||||
ReturnValue_t result = HasReturnvaluesIF::RETURN_OK;
|
||||
while ((result == HasReturnvaluesIF::RETURN_OK) && (element != NULL)) {
|
||||
result = element->value->serialize(buffer, size, maxSize,
|
||||
streamEndianness);
|
||||
element = element->getNext();
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
virtual size_t getSerializedSize() const override {
|
||||
if (printCount) {
|
||||
return SerialLinkedListAdapter<T>::getSerializedSize()
|
||||
+ sizeof(count_t);
|
||||
} else {
|
||||
return getSerializedSize(SinglyLinkedList<T>::start);
|
||||
}
|
||||
}
|
||||
|
||||
static size_t getSerializedSize(const LinkedElement<T> *element) {
|
||||
size_t size = 0;
|
||||
while (element != NULL) {
|
||||
size += element->value->getSerializedSize();
|
||||
element = element->getNext();
|
||||
}
|
||||
return size;
|
||||
}
|
||||
|
||||
|
||||
virtual ReturnValue_t deSerialize(const uint8_t** buffer, size_t* size,
|
||||
Endianness streamEndianness) override {
|
||||
return deSerialize(SinglyLinkedList<T>::start, buffer, size, streamEndianness);
|
||||
}
|
||||
|
||||
static ReturnValue_t deSerialize(LinkedElement<T>* element,
|
||||
const uint8_t** buffer, size_t* size, Endianness streamEndianness) {
|
||||
ReturnValue_t result = HasReturnvaluesIF::RETURN_OK;
|
||||
while ((result == HasReturnvaluesIF::RETURN_OK) && (element != NULL)) {
|
||||
result = element->value->deSerialize(buffer, size, streamEndianness);
|
||||
element = element->getNext();
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
bool printCount;
|
||||
};
|
||||
|
||||
#endif /* SERIALLINKEDLISTADAPTER_H_ */
|
||||
/**
|
||||
* @file SerialLinkedListAdapter.h
|
||||
* @brief This file defines the SerialLinkedListAdapter class.
|
||||
* @date 22.07.2014
|
||||
* @author baetz
|
||||
*/
|
||||
#ifndef SERIALLINKEDLISTADAPTER_H_
|
||||
#define SERIALLINKEDLISTADAPTER_H_
|
||||
|
||||
#include "../container/SinglyLinkedList.h"
|
||||
#include "../serialize/SerializeAdapter.h"
|
||||
#include "../serialize/SerializeElement.h"
|
||||
#include "../serialize/SerializeIF.h"
|
||||
//This is where we need the SerializeAdapter!
|
||||
|
||||
/**
|
||||
* @brief Implement the conversion of object data to data streams
|
||||
* or vice-versa, using linked lists.
|
||||
* @details
|
||||
* An alternative to the AutoSerializeAdapter functions
|
||||
* - All object members with a datatype are declared as
|
||||
* SerializeElement<element_type> members inside the class
|
||||
* implementing this adapter.
|
||||
* - The element type can also be a SerialBufferAdapter to
|
||||
* de-/serialize buffers with a known size
|
||||
* - 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 setStart and setNext functions.
|
||||
*
|
||||
* 1. The serialization process is done by instantiating the class and
|
||||
* calling serialize after all SerializeElement entries have been set by
|
||||
* using the constructor or setter functions. An additional size variable
|
||||
* can be supplied which is calculated/incremented automatically.
|
||||
* 2. The deserialization process is done by instantiating the class and
|
||||
* supplying a buffer with the data which is converted into an object.
|
||||
* The size of data to serialize can be supplied and is
|
||||
* decremented in the function. Range checking is done internally.
|
||||
*
|
||||
* @ingroup serialize
|
||||
*/
|
||||
template<typename T, typename count_t = uint8_t>
|
||||
class SerialLinkedListAdapter: public SinglyLinkedList<T>, public SerializeIF {
|
||||
public:
|
||||
/**
|
||||
* Copying is forbidden by deleting the copy constructor and the copy
|
||||
* assignment operator because of the pointers to the linked list members.
|
||||
* Unless the child class implements an own copy constructor or
|
||||
* copy assignment operator, these operation will throw a compiler error.
|
||||
* @param
|
||||
*/
|
||||
SerialLinkedListAdapter(const SerialLinkedListAdapter &) = delete;
|
||||
SerialLinkedListAdapter& operator=(const SerialLinkedListAdapter&) = delete;
|
||||
|
||||
SerialLinkedListAdapter(typename LinkedElement<T>::Iterator start,
|
||||
bool printCount = false) :
|
||||
SinglyLinkedList<T>(start), printCount(printCount) {
|
||||
}
|
||||
|
||||
SerialLinkedListAdapter(LinkedElement<T>* first, bool printCount = false) :
|
||||
SinglyLinkedList<T>(first), printCount(printCount) {
|
||||
|
||||
}
|
||||
|
||||
SerialLinkedListAdapter(bool printCount = false) :
|
||||
SinglyLinkedList<T>(), printCount(printCount) {
|
||||
}
|
||||
|
||||
virtual ReturnValue_t serialize(uint8_t** buffer, size_t* size,
|
||||
size_t maxSize, Endianness streamEndianness) const override {
|
||||
if (printCount) {
|
||||
count_t mySize = SinglyLinkedList<T>::getSize();
|
||||
ReturnValue_t result = SerializeAdapter::serialize(&mySize,
|
||||
buffer, size, maxSize, streamEndianness);
|
||||
if (result != HasReturnvaluesIF::RETURN_OK) {
|
||||
return result;
|
||||
}
|
||||
}
|
||||
return serialize(SinglyLinkedList<T>::start, buffer, size, maxSize,
|
||||
streamEndianness);
|
||||
}
|
||||
|
||||
static ReturnValue_t serialize(const LinkedElement<T>* element,
|
||||
uint8_t** buffer, size_t* size, size_t maxSize,
|
||||
Endianness streamEndianness) {
|
||||
ReturnValue_t result = HasReturnvaluesIF::RETURN_OK;
|
||||
while ((result == HasReturnvaluesIF::RETURN_OK) && (element != NULL)) {
|
||||
result = element->value->serialize(buffer, size, maxSize,
|
||||
streamEndianness);
|
||||
element = element->getNext();
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
virtual size_t getSerializedSize() const override {
|
||||
if (printCount) {
|
||||
return SerialLinkedListAdapter<T>::getSerializedSize()
|
||||
+ sizeof(count_t);
|
||||
} else {
|
||||
return getSerializedSize(SinglyLinkedList<T>::start);
|
||||
}
|
||||
}
|
||||
|
||||
static size_t getSerializedSize(const LinkedElement<T> *element) {
|
||||
size_t size = 0;
|
||||
while (element != NULL) {
|
||||
size += element->value->getSerializedSize();
|
||||
element = element->getNext();
|
||||
}
|
||||
return size;
|
||||
}
|
||||
|
||||
|
||||
virtual ReturnValue_t deSerialize(const uint8_t** buffer, size_t* size,
|
||||
Endianness streamEndianness) override {
|
||||
return deSerialize(SinglyLinkedList<T>::start, buffer, size, streamEndianness);
|
||||
}
|
||||
|
||||
static ReturnValue_t deSerialize(LinkedElement<T>* element,
|
||||
const uint8_t** buffer, size_t* size, Endianness streamEndianness) {
|
||||
ReturnValue_t result = HasReturnvaluesIF::RETURN_OK;
|
||||
while ((result == HasReturnvaluesIF::RETURN_OK) && (element != NULL)) {
|
||||
result = element->value->deSerialize(buffer, size, streamEndianness);
|
||||
element = element->getNext();
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
bool printCount;
|
||||
};
|
||||
|
||||
#endif /* SERIALLINKEDLISTADAPTER_H_ */
|
||||
|
@ -1,175 +1,175 @@
|
||||
#ifndef SERIALIZEADAPTER_H_
|
||||
#define SERIALIZEADAPTER_H_
|
||||
|
||||
#include "../container/IsDerivedFrom.h"
|
||||
#include "../returnvalues/HasReturnvaluesIF.h"
|
||||
#include "../serialize/EndianConverter.h"
|
||||
#include "../serialize/SerializeIF.h"
|
||||
#include <type_traits>
|
||||
|
||||
/**
|
||||
* @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
|
||||
*
|
||||
* 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 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
|
||||
*/
|
||||
|
||||
class SerializeAdapter {
|
||||
public:
|
||||
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,
|
||||
size_t *size, size_t max_size, SerializeIF::Endianness streamEndianness) {
|
||||
size_t ignoredSize = 0;
|
||||
if (size == NULL) {
|
||||
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;
|
||||
}
|
||||
memcpy(*buffer, &tmp, sizeof(T));
|
||||
*size += sizeof(T);
|
||||
(*buffer) += sizeof(T);
|
||||
return HasReturnvaluesIF::RETURN_OK;
|
||||
} else {
|
||||
return SerializeIF::BUFFER_TOO_SHORT;
|
||||
}
|
||||
}
|
||||
|
||||
ReturnValue_t deSerialize(T *object, const uint8_t **buffer,
|
||||
size_t *size, SerializeIF::Endianness streamEndianness) {
|
||||
T tmp;
|
||||
if (*size >= sizeof(T)) {
|
||||
*size -= sizeof(T);
|
||||
memcpy(&tmp, *buffer, sizeof(T));
|
||||
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;
|
||||
} else {
|
||||
return SerializeIF::STREAM_TOO_SHORT;
|
||||
}
|
||||
}
|
||||
|
||||
uint32_t getSerializedSize(const T *object) {
|
||||
return sizeof(T);
|
||||
}
|
||||
|
||||
};
|
||||
|
||||
template<typename T>
|
||||
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;
|
||||
if (size == NULL) {
|
||||
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);
|
||||
}
|
||||
};
|
||||
};
|
||||
|
||||
#endif /* SERIALIZEADAPTER_H_ */
|
||||
#ifndef SERIALIZEADAPTER_H_
|
||||
#define SERIALIZEADAPTER_H_
|
||||
|
||||
#include "../container/IsDerivedFrom.h"
|
||||
#include "../returnvalues/HasReturnvaluesIF.h"
|
||||
#include "../serialize/EndianConverter.h"
|
||||
#include "../serialize/SerializeIF.h"
|
||||
#include <type_traits>
|
||||
|
||||
/**
|
||||
* @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
|
||||
*
|
||||
* 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 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
|
||||
*/
|
||||
|
||||
class SerializeAdapter {
|
||||
public:
|
||||
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,
|
||||
size_t *size, size_t max_size, SerializeIF::Endianness streamEndianness) {
|
||||
size_t ignoredSize = 0;
|
||||
if (size == NULL) {
|
||||
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;
|
||||
}
|
||||
memcpy(*buffer, &tmp, sizeof(T));
|
||||
*size += sizeof(T);
|
||||
(*buffer) += sizeof(T);
|
||||
return HasReturnvaluesIF::RETURN_OK;
|
||||
} else {
|
||||
return SerializeIF::BUFFER_TOO_SHORT;
|
||||
}
|
||||
}
|
||||
|
||||
ReturnValue_t deSerialize(T *object, const uint8_t **buffer,
|
||||
size_t *size, SerializeIF::Endianness streamEndianness) {
|
||||
T tmp;
|
||||
if (*size >= sizeof(T)) {
|
||||
*size -= sizeof(T);
|
||||
memcpy(&tmp, *buffer, sizeof(T));
|
||||
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;
|
||||
} else {
|
||||
return SerializeIF::STREAM_TOO_SHORT;
|
||||
}
|
||||
}
|
||||
|
||||
uint32_t getSerializedSize(const T *object) {
|
||||
return sizeof(T);
|
||||
}
|
||||
|
||||
};
|
||||
|
||||
template<typename T>
|
||||
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;
|
||||
if (size == NULL) {
|
||||
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);
|
||||
}
|
||||
};
|
||||
};
|
||||
|
||||
#endif /* SERIALIZEADAPTER_H_ */
|
||||
|
@ -1,60 +1,60 @@
|
||||
#ifndef SERIALIZEELEMENT_H_
|
||||
#define SERIALIZEELEMENT_H_
|
||||
|
||||
#include "../container/SinglyLinkedList.h"
|
||||
#include "../serialize/SerializeAdapter.h"
|
||||
#include <utility>
|
||||
|
||||
/**
|
||||
* @brief This class is used to mark datatypes for serialization with the
|
||||
* SerialLinkedListAdapter
|
||||
* @details
|
||||
* Used by declaring any arbitrary datatype with SerializeElement<T> myVariable,
|
||||
* inside a SerialLinkedListAdapter implementation and setting the sequence
|
||||
* of objects with setNext() and setStart().
|
||||
* Serilization and Deserialization is then performed automatically in
|
||||
* specified sequence order.
|
||||
* @ingroup serialize
|
||||
*/
|
||||
template<typename T>
|
||||
class SerializeElement: public SerializeIF, public LinkedElement<SerializeIF> {
|
||||
public:
|
||||
template<typename ... Args>
|
||||
SerializeElement(Args ... args) :
|
||||
LinkedElement<SerializeIF>(this), entry(std::forward<Args>(args)...) {
|
||||
|
||||
}
|
||||
SerializeElement() :
|
||||
LinkedElement<SerializeIF>(this) {
|
||||
}
|
||||
T entry;
|
||||
ReturnValue_t serialize(uint8_t **buffer, size_t *size, size_t maxSize,
|
||||
Endianness streamEndianness) const override {
|
||||
return SerializeAdapter::serialize(&entry, buffer, size, maxSize,
|
||||
streamEndianness);
|
||||
}
|
||||
|
||||
size_t getSerializedSize() const override {
|
||||
return SerializeAdapter::getSerializedSize(&entry);
|
||||
}
|
||||
|
||||
virtual ReturnValue_t deSerialize(const uint8_t **buffer, size_t *size,
|
||||
Endianness streamEndianness) override {
|
||||
return SerializeAdapter::deSerialize(&entry, buffer, size,
|
||||
streamEndianness);
|
||||
}
|
||||
|
||||
operator T() {
|
||||
return entry;
|
||||
}
|
||||
|
||||
SerializeElement<T>& operator=(T newValue) {
|
||||
entry = newValue;
|
||||
return *this;
|
||||
}
|
||||
T* operator->() {
|
||||
return &entry;
|
||||
}
|
||||
};
|
||||
|
||||
#endif /* SERIALIZEELEMENT_H_ */
|
||||
#ifndef SERIALIZEELEMENT_H_
|
||||
#define SERIALIZEELEMENT_H_
|
||||
|
||||
#include "../container/SinglyLinkedList.h"
|
||||
#include "../serialize/SerializeAdapter.h"
|
||||
#include <utility>
|
||||
|
||||
/**
|
||||
* @brief This class is used to mark datatypes for serialization with the
|
||||
* SerialLinkedListAdapter
|
||||
* @details
|
||||
* Used by declaring any arbitrary datatype with SerializeElement<T> myVariable,
|
||||
* inside a SerialLinkedListAdapter implementation and setting the sequence
|
||||
* of objects with setNext() and setStart().
|
||||
* Serilization and Deserialization is then performed automatically in
|
||||
* specified sequence order.
|
||||
* @ingroup serialize
|
||||
*/
|
||||
template<typename T>
|
||||
class SerializeElement: public SerializeIF, public LinkedElement<SerializeIF> {
|
||||
public:
|
||||
template<typename ... Args>
|
||||
SerializeElement(Args ... args) :
|
||||
LinkedElement<SerializeIF>(this), entry(std::forward<Args>(args)...) {
|
||||
|
||||
}
|
||||
SerializeElement() :
|
||||
LinkedElement<SerializeIF>(this) {
|
||||
}
|
||||
T entry;
|
||||
ReturnValue_t serialize(uint8_t **buffer, size_t *size, size_t maxSize,
|
||||
Endianness streamEndianness) const override {
|
||||
return SerializeAdapter::serialize(&entry, buffer, size, maxSize,
|
||||
streamEndianness);
|
||||
}
|
||||
|
||||
size_t getSerializedSize() const override {
|
||||
return SerializeAdapter::getSerializedSize(&entry);
|
||||
}
|
||||
|
||||
virtual ReturnValue_t deSerialize(const uint8_t **buffer, size_t *size,
|
||||
Endianness streamEndianness) override {
|
||||
return SerializeAdapter::deSerialize(&entry, buffer, size,
|
||||
streamEndianness);
|
||||
}
|
||||
|
||||
operator T() {
|
||||
return entry;
|
||||
}
|
||||
|
||||
SerializeElement<T>& operator=(T newValue) {
|
||||
entry = newValue;
|
||||
return *this;
|
||||
}
|
||||
T* operator->() {
|
||||
return &entry;
|
||||
}
|
||||
};
|
||||
|
||||
#endif /* SERIALIZEELEMENT_H_ */
|
||||
|
@ -1,58 +1,58 @@
|
||||
#ifndef SERIALIZEIF_H_
|
||||
#define SERIALIZEIF_H_
|
||||
|
||||
#include "../returnvalues/HasReturnvaluesIF.h"
|
||||
#include <cstddef>
|
||||
|
||||
/**
|
||||
* @defgroup serialize Serialization
|
||||
* Contains serialisation services.
|
||||
*/
|
||||
|
||||
/**
|
||||
* @brief An interface for alle classes which require
|
||||
* translation of objects data into data streams and vice-versa.
|
||||
* @details
|
||||
* If the target architecture is little endian (e.g. ARM), any data types
|
||||
* created might have the wrong endianess if they are to be used for the FSFW.
|
||||
* Depending on the system architecture, endian correctness must be assured,
|
||||
* 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
|
||||
* the endian flag)
|
||||
* 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 like seen in TmPacketStored.cpp for example.
|
||||
*
|
||||
* @ingroup serialize
|
||||
*/
|
||||
class SerializeIF {
|
||||
public:
|
||||
enum class Endianness : uint8_t {
|
||||
BIG, LITTLE, MACHINE
|
||||
};
|
||||
|
||||
static const uint8_t INTERFACE_ID = CLASS_ID::SERIALIZE_IF;
|
||||
static const ReturnValue_t BUFFER_TOO_SHORT = MAKE_RETURN_CODE(1);
|
||||
static const ReturnValue_t STREAM_TOO_SHORT = MAKE_RETURN_CODE(2);
|
||||
static const ReturnValue_t TOO_MANY_ELEMENTS = MAKE_RETURN_CODE(3);
|
||||
|
||||
virtual ~SerializeIF() {
|
||||
}
|
||||
|
||||
virtual ReturnValue_t serialize(uint8_t **buffer, size_t *size,
|
||||
size_t maxSize, Endianness streamEndianness) const = 0;
|
||||
|
||||
virtual size_t getSerializedSize() const = 0;
|
||||
|
||||
virtual ReturnValue_t deSerialize(const uint8_t **buffer, size_t *size,
|
||||
Endianness streamEndianness) = 0;
|
||||
|
||||
};
|
||||
|
||||
#endif /* SERIALIZEIF_H_ */
|
||||
#ifndef SERIALIZEIF_H_
|
||||
#define SERIALIZEIF_H_
|
||||
|
||||
#include "../returnvalues/HasReturnvaluesIF.h"
|
||||
#include <cstddef>
|
||||
|
||||
/**
|
||||
* @defgroup serialize Serialization
|
||||
* Contains serialisation services.
|
||||
*/
|
||||
|
||||
/**
|
||||
* @brief An interface for alle classes which require
|
||||
* translation of objects data into data streams and vice-versa.
|
||||
* @details
|
||||
* If the target architecture is little endian (e.g. ARM), any data types
|
||||
* created might have the wrong endianess if they are to be used for the FSFW.
|
||||
* Depending on the system architecture, endian correctness must be assured,
|
||||
* 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
|
||||
* the endian flag)
|
||||
* 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 like seen in TmPacketStored.cpp for example.
|
||||
*
|
||||
* @ingroup serialize
|
||||
*/
|
||||
class SerializeIF {
|
||||
public:
|
||||
enum class Endianness : uint8_t {
|
||||
BIG, LITTLE, MACHINE
|
||||
};
|
||||
|
||||
static const uint8_t INTERFACE_ID = CLASS_ID::SERIALIZE_IF;
|
||||
static const ReturnValue_t BUFFER_TOO_SHORT = MAKE_RETURN_CODE(1);
|
||||
static const ReturnValue_t STREAM_TOO_SHORT = MAKE_RETURN_CODE(2);
|
||||
static const ReturnValue_t TOO_MANY_ELEMENTS = MAKE_RETURN_CODE(3);
|
||||
|
||||
virtual ~SerializeIF() {
|
||||
}
|
||||
|
||||
virtual ReturnValue_t serialize(uint8_t **buffer, size_t *size,
|
||||
size_t maxSize, Endianness streamEndianness) const = 0;
|
||||
|
||||
virtual size_t getSerializedSize() const = 0;
|
||||
|
||||
virtual ReturnValue_t deSerialize(const uint8_t **buffer, size_t *size,
|
||||
Endianness streamEndianness) = 0;
|
||||
|
||||
};
|
||||
|
||||
#endif /* SERIALIZEIF_H_ */
|
||||
|
Reference in New Issue
Block a user