Added test folder.

This commit is contained in:
2020-04-13 16:27:05 +02:00
parent 69e9710bf1
commit a0ee010926
7 changed files with 338 additions and 34 deletions

View File

@ -11,9 +11,7 @@
*/
class EndianSwapper {
private:
EndianSwapper() {
}
;
EndianSwapper() {};
public:
template<typename T>
static T swap(T in) {

View File

@ -12,8 +12,8 @@ SerialBufferAdapter<count_t>::SerialBufferAdapter(const void* buffer,
}
template<typename count_t>
SerialBufferAdapter<count_t>::SerialBufferAdapter(void* buffer, count_t bufferLength,
bool serializeLength) :
SerialBufferAdapter<count_t>::SerialBufferAdapter(void* buffer,
count_t bufferLength, bool serializeLength) :
serializeLength(serializeLength), bufferLength(bufferLength) {
uint8_t * member_buffer = static_cast<uint8_t *>(buffer);
m_buffer = member_buffer;
@ -26,8 +26,8 @@ SerialBufferAdapter<count_t>::~SerialBufferAdapter() {
}
template<typename count_t>
ReturnValue_t SerialBufferAdapter<count_t>::serialize(uint8_t** buffer, size_t* size,
const size_t max_size, bool bigEndian) const {
ReturnValue_t SerialBufferAdapter<count_t>::serialize(uint8_t** buffer,
size_t* size, const size_t max_size, bool bigEndian) const {
uint32_t serializedLength = bufferLength;
if (serializeLength) {
serializedLength += AutoSerializeAdapter::getSerializedSize(
@ -98,7 +98,8 @@ ReturnValue_t SerialBufferAdapter<count_t>::deSerialize(const uint8_t** buffer,
template<typename count_t>
uint8_t * SerialBufferAdapter<count_t>::getBuffer() {
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 m_buffer;
@ -107,14 +108,16 @@ uint8_t * SerialBufferAdapter<count_t>::getBuffer() {
template<typename count_t>
const uint8_t * SerialBufferAdapter<count_t>::getConstBuffer() {
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 constBuffer;
}
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);
bufferLength = buffer_length;
}

View File

@ -8,11 +8,13 @@
* 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.
* 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)>> serialBufferElement.
* Right now, the SerialBufferAdapter must always be initialized with the buffer and size !
* SerialElement<SerialBufferAdapter<bufferLengthType(will be uint8_t mostly)>>.
* Right now, the SerialBufferAdapter must always
* be initialized with the buffer and size !
*
* \ingroup serialize
*/
@ -27,14 +29,16 @@ public:
* @param bufferLength
* @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.
* @param buffer
* @param bufferLength
* @param serializeLength
* @param serializeLength Length field will be serialized with size count_t
*/
SerialBufferAdapter(void* buffer, count_t bufferLength, bool serializeLength = false);
@ -58,6 +62,4 @@ private:
count_t bufferLength = 0;
};
#endif /* SERIALBUFFERADAPTER_H_ */

View File

@ -9,18 +9,21 @@
/**
* @brief This adapter 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.
* with arbitrary template objects to facilitate and simplify the
* serialization of classes with different multiple different data types
* into buffers and vice-versa.
* @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.
* 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
* 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.
*
* If the target architecture is little endian (ARM), any data types created might
@ -50,8 +53,8 @@
* memcpy(&data,buffer + positionOfTargetByte1,sizeof(data));
* data = EndianSwapper::swap(data);
*
* When serializing for downlink, the packets are generally serialized assuming big endian data format
* like seen in TmPacketStored.cpp for example.
* When serializing for downlink, the packets are generally serialized assuming
* big endian data format like seen in TmPacketStored.cpp for example.
*
* @ingroup serialize
*/
@ -83,8 +86,10 @@ public:
/**
* Deserialize buffer into object
* @param object [out] Object to be deserialized with buffer data
* @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 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
*/
@ -106,7 +111,7 @@ public:
}
}
uint32_t getSerializedSize(const T * object) {
size_t getSerializedSize(const T * object) {
return sizeof(T);
}
@ -123,7 +128,7 @@ public:
}
return object->serialize(buffer, size, max_size, bigEndian);
}
uint32_t getSerializedSize(const T* object) const {
size_t getSerializedSize(const T* object) const {
return object->getSerializedSize();
}
@ -163,7 +168,7 @@ public:
return adapter.serialize(object, buffer, size, max_size, bigEndian);
}
template<typename T>
static uint32_t getSerializedSize(const T* object) {
static size_t getSerializedSize(const T* object) {
SerializeAdapter_<T, IsDerivedFrom<T, SerializeIF>::Is> adapter;
return adapter.getSerializedSize(object);
}