Today's the day. Renamed platform to framework.
This commit is contained in:
45
serialize/EndianSwapper.h
Normal file
45
serialize/EndianSwapper.h
Normal file
@ -0,0 +1,45 @@
|
||||
#ifndef ENDIANSWAPPER_H_
|
||||
#define ENDIANSWAPPER_H_
|
||||
|
||||
#include <framework/osal/OSAL.h>
|
||||
//#include <endian.h> //for testing on x86
|
||||
|
||||
|
||||
class EndianSwapper {
|
||||
private:
|
||||
EndianSwapper() {
|
||||
}
|
||||
;
|
||||
public:
|
||||
template<typename T>
|
||||
static T swap(T in) {
|
||||
#ifndef BYTE_ORDER
|
||||
#error BYTE_ORDER not defined
|
||||
#elif BYTE_ORDER == LITTLE_ENDIAN
|
||||
T tmp;
|
||||
uint8_t *pointerOut = (uint8_t *) &tmp;
|
||||
uint8_t *pointerIn = (uint8_t *) ∈
|
||||
for (uint8_t count = 0; count < sizeof(T); count++) {
|
||||
pointerOut[sizeof(T) - count - 1] = pointerIn[count];
|
||||
}
|
||||
return tmp;
|
||||
#elif BYTE_ORDER == BIG_ENDIAN
|
||||
return in;
|
||||
#endif
|
||||
}
|
||||
static void swap(uint8_t* out, const uint8_t* in, uint32_t size) {
|
||||
#ifndef BYTE_ORDER
|
||||
#error BYTE_ORDER not defined
|
||||
#elif BYTE_ORDER == LITTLE_ENDIAN
|
||||
for (uint8_t count = 0; count < size; count++) {
|
||||
out[size - count - 1] = in[count];
|
||||
}
|
||||
return;
|
||||
#elif BYTE_ORDER == BIG_ENDIAN
|
||||
memcpy(out, in, size);
|
||||
return;
|
||||
#endif
|
||||
}
|
||||
};
|
||||
|
||||
#endif /* ENDIANSWAPPER_H_ */
|
80
serialize/SerialArrayListAdapter.h
Normal file
80
serialize/SerialArrayListAdapter.h
Normal file
@ -0,0 +1,80 @@
|
||||
/**
|
||||
* @file SerialArrayListAdapter.h
|
||||
* @brief This file defines the SerialArrayListAdapter class.
|
||||
* @date 22.07.2014
|
||||
* @author baetz
|
||||
*/
|
||||
#ifndef SERIALARRAYLISTADAPTER_H_
|
||||
#define SERIALARRAYLISTADAPTER_H_
|
||||
|
||||
#include <framework/container/ArrayList.h>
|
||||
#include <framework/serialize/SerializeIF.h>
|
||||
#include <utility>
|
||||
|
||||
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, uint32_t* size,
|
||||
const uint32_t max_size, bool bigEndian) const {
|
||||
return serialize(adaptee, buffer, size, max_size, bigEndian);
|
||||
}
|
||||
|
||||
static ReturnValue_t serialize(const ArrayList<T, count_t>* list, uint8_t** buffer, uint32_t* size,
|
||||
const uint32_t max_size, bool bigEndian) {
|
||||
ReturnValue_t result = SerializeAdapter<count_t>::serialize(&list->size,
|
||||
buffer, size, max_size, bigEndian);
|
||||
count_t i = 0;
|
||||
while ((result == HasReturnvaluesIF::RETURN_OK) && (i < list->size)) {
|
||||
result = SerializeAdapter<T>::serialize(&list->entries[i], buffer, size,
|
||||
max_size, bigEndian);
|
||||
++i;
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
virtual uint32_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<T>::getSerializedSize(&list->entries[i]);
|
||||
}
|
||||
|
||||
return printSize;
|
||||
}
|
||||
|
||||
virtual ReturnValue_t deSerialize(const uint8_t** buffer, int32_t* size,
|
||||
bool bigEndian) {
|
||||
return deSerialize(adaptee, buffer, size, bigEndian);
|
||||
}
|
||||
|
||||
static ReturnValue_t deSerialize(ArrayList<T, count_t>* list, const uint8_t** buffer, int32_t* size,
|
||||
bool bigEndian) {
|
||||
ReturnValue_t result = SerializeAdapter<count_t>::deSerialize(&list->size,
|
||||
buffer, size, bigEndian);
|
||||
if (list->size > list->maxSize()) {
|
||||
return SerializeIF::TOO_MANY_ELEMENTS;
|
||||
}
|
||||
count_t i = 0;
|
||||
while ((result == HasReturnvaluesIF::RETURN_OK) && (i < list->size)) {
|
||||
result = SerializeAdapter<T>::deSerialize(
|
||||
&list->front()[i], buffer, size,
|
||||
bigEndian);
|
||||
++i;
|
||||
}
|
||||
return result;
|
||||
}
|
||||
private:
|
||||
ArrayList<T, count_t> *adaptee;
|
||||
};
|
||||
|
||||
|
||||
|
||||
#endif /* SERIALARRAYLISTADAPTER_H_ */
|
52
serialize/SerialBufferAdapter.cpp
Normal file
52
serialize/SerialBufferAdapter.cpp
Normal file
@ -0,0 +1,52 @@
|
||||
#include <framework/serialize/SerialBufferAdapter.h>
|
||||
#include <cstring>
|
||||
|
||||
SerialBufferAdapter::SerialBufferAdapter(const uint8_t* buffer,
|
||||
uint32_t bufferLength) :
|
||||
constBuffer(buffer), buffer(NULL), bufferLength(bufferLength) {
|
||||
}
|
||||
|
||||
SerialBufferAdapter::SerialBufferAdapter(uint8_t* buffer, uint32_t bufferLength) :
|
||||
constBuffer(NULL), buffer(buffer), bufferLength(bufferLength) {
|
||||
}
|
||||
|
||||
SerialBufferAdapter::~SerialBufferAdapter() {
|
||||
}
|
||||
|
||||
ReturnValue_t SerialBufferAdapter::serialize(uint8_t** buffer, uint32_t* size,
|
||||
const uint32_t max_size, bool bigEndian) const {
|
||||
if (*size + bufferLength > max_size) {
|
||||
return BUFFER_TOO_SHORT;
|
||||
} else {
|
||||
if (this->constBuffer != NULL) {
|
||||
memcpy(*buffer, this->constBuffer, bufferLength);
|
||||
} else if (this->buffer != NULL) {
|
||||
memcpy(*buffer, this->buffer, bufferLength);
|
||||
} else {
|
||||
return HasReturnvaluesIF::RETURN_FAILED;
|
||||
}
|
||||
*size += bufferLength;
|
||||
buffer += bufferLength;
|
||||
return HasReturnvaluesIF::RETURN_OK;
|
||||
}
|
||||
}
|
||||
|
||||
uint32_t SerialBufferAdapter::getSerializedSize() const {
|
||||
return bufferLength;
|
||||
}
|
||||
|
||||
ReturnValue_t SerialBufferAdapter::deSerialize(const uint8_t** buffer,
|
||||
int32_t* size, bool bigEndian) {
|
||||
if (buffer != NULL) {
|
||||
if (*size - bufferLength >= 0) {
|
||||
*size -= bufferLength;
|
||||
memcpy(this->buffer, *buffer, bufferLength);
|
||||
buffer += bufferLength;
|
||||
return HasReturnvaluesIF::RETURN_OK;
|
||||
} else {
|
||||
return STREAM_TOO_SHORT;
|
||||
}
|
||||
} else {
|
||||
return HasReturnvaluesIF::RETURN_FAILED;
|
||||
}
|
||||
}
|
26
serialize/SerialBufferAdapter.h
Normal file
26
serialize/SerialBufferAdapter.h
Normal file
@ -0,0 +1,26 @@
|
||||
#ifndef SERIALBUFFERADAPTER_H_
|
||||
#define SERIALBUFFERADAPTER_H_
|
||||
|
||||
#include <framework/serialize/SerializeIF.h>
|
||||
|
||||
class SerialBufferAdapter: public SerializeIF {
|
||||
public:
|
||||
SerialBufferAdapter(const uint8_t * buffer, uint32_t bufferLength);
|
||||
SerialBufferAdapter(uint8_t* buffer, uint32_t bufferLength);
|
||||
|
||||
virtual ~SerialBufferAdapter();
|
||||
|
||||
virtual ReturnValue_t serialize(uint8_t** buffer, uint32_t* size,
|
||||
const uint32_t max_size, bool bigEndian) const;
|
||||
|
||||
virtual uint32_t getSerializedSize() const;
|
||||
|
||||
virtual ReturnValue_t deSerialize(const uint8_t** buffer, int32_t* size,
|
||||
bool bigEndian);
|
||||
private:
|
||||
const uint8_t *constBuffer;
|
||||
uint8_t *buffer;
|
||||
uint32_t bufferLength;
|
||||
};
|
||||
|
||||
#endif /* SERIALBUFFERADAPTER_H_ */
|
35
serialize/SerialFixedArrayListAdapter.h
Normal file
35
serialize/SerialFixedArrayListAdapter.h
Normal file
@ -0,0 +1,35 @@
|
||||
/*
|
||||
* SerialFixedArrayListAdapter.h
|
||||
*
|
||||
* Created on: 22.07.2014
|
||||
* Author: baetz
|
||||
*/
|
||||
|
||||
#ifndef SERIALFIXEDARRAYLISTADAPTER_H_
|
||||
#define SERIALFIXEDARRAYLISTADAPTER_H_
|
||||
|
||||
#include <framework/container/FixedArrayList.h>
|
||||
#include <framework/serialize/SerialArrayListAdapter.h>
|
||||
|
||||
template<typename T, uint32_t MAX_SIZE, typename count_t = uint8_t>
|
||||
class SerialFixedArrayListAdapter : public FixedArrayList<T, MAX_SIZE, count_t>, public SerializeIF {
|
||||
public:
|
||||
template<typename... Args>
|
||||
SerialFixedArrayListAdapter(Args... args) : FixedArrayList<T, MAX_SIZE, count_t>(std::forward<Args>(args)...) {
|
||||
}
|
||||
ReturnValue_t serialize(uint8_t** buffer, uint32_t* size,
|
||||
const uint32_t max_size, bool bigEndian) const {
|
||||
return SerialArrayListAdapter<T, count_t>::serialize(this, buffer, size, max_size, bigEndian);
|
||||
}
|
||||
uint32_t getSerializedSize() const {
|
||||
return SerialArrayListAdapter<T, count_t>::getSerializedSize(this);
|
||||
}
|
||||
ReturnValue_t deSerialize(const uint8_t** buffer, int32_t* size,
|
||||
bool bigEndian) {
|
||||
return SerialArrayListAdapter<T, count_t>::deSerialize(this, buffer, size, bigEndian);
|
||||
}
|
||||
};
|
||||
|
||||
|
||||
|
||||
#endif /* SERIALFIXEDARRAYLISTADAPTER_H_ */
|
91
serialize/SerialLinkedListAdapter.h
Normal file
91
serialize/SerialLinkedListAdapter.h
Normal file
@ -0,0 +1,91 @@
|
||||
/**
|
||||
* @file SerialLinkedListAdapter.h
|
||||
* @brief This file defines the SerialLinkedListAdapter class.
|
||||
* @date 22.07.2014
|
||||
* @author baetz
|
||||
*/
|
||||
#ifndef SERIALLINKEDLISTADAPTER_H_
|
||||
#define SERIALLINKEDLISTADAPTER_H_
|
||||
|
||||
#include <framework/container/SinglyLinkedList.h>
|
||||
#include <framework/serialize/SerializeAdapter.h>
|
||||
#include <framework/serialize/SerializeElement.h>
|
||||
#include <framework/serialize/SerializeIF.h>
|
||||
//This is where we need the SerializeAdapter!
|
||||
template<typename T, typename count_t = uint8_t>
|
||||
class SerialLinkedListAdapter: public SinglyLinkedList<T>, public SerializeIF {
|
||||
public:
|
||||
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, uint32_t* size,
|
||||
const uint32_t max_size, bool bigEndian) const {
|
||||
if (printCount) {
|
||||
count_t mySize = SinglyLinkedList<T>::getSize();
|
||||
ReturnValue_t result = SerializeAdapter<count_t>::serialize(&mySize,
|
||||
buffer, size, max_size, bigEndian);
|
||||
if (result != HasReturnvaluesIF::RETURN_OK) {
|
||||
return result;
|
||||
}
|
||||
}
|
||||
return serialize(SinglyLinkedList<T>::start, buffer, size, max_size,
|
||||
bigEndian);
|
||||
}
|
||||
|
||||
static ReturnValue_t serialize(const LinkedElement<T>* element,
|
||||
uint8_t** buffer, uint32_t* size, const uint32_t max_size,
|
||||
bool bigEndian) {
|
||||
ReturnValue_t result = HasReturnvaluesIF::RETURN_OK;
|
||||
while ((result == HasReturnvaluesIF::RETURN_OK) && (element != NULL)) {
|
||||
result = element->value->serialize(buffer, size, max_size,
|
||||
bigEndian);
|
||||
element = element->getNext();
|
||||
}
|
||||
return result;
|
||||
}
|
||||
virtual uint32_t getSerializedSize() const {
|
||||
if (printCount) {
|
||||
return SerialLinkedListAdapter<T>::getSerializedSize()
|
||||
+ sizeof(count_t);
|
||||
} else {
|
||||
return getSerializedSize(SinglyLinkedList<T>::start);
|
||||
}
|
||||
}
|
||||
static uint32_t getSerializedSize(const LinkedElement<T> *element) {
|
||||
uint32_t size = 0;
|
||||
while (element != NULL) {
|
||||
size += element->value->getSerializedSize();
|
||||
element = element->getNext();
|
||||
}
|
||||
return size;
|
||||
}
|
||||
|
||||
virtual ReturnValue_t deSerialize(const uint8_t** buffer, int32_t* size,
|
||||
bool bigEndian) {
|
||||
return deSerialize(SinglyLinkedList<T>::start, buffer, size, bigEndian);
|
||||
}
|
||||
|
||||
static ReturnValue_t deSerialize(LinkedElement<T>* element,
|
||||
const uint8_t** buffer, int32_t* size, bool bigEndian) {
|
||||
ReturnValue_t result = HasReturnvaluesIF::RETURN_OK;
|
||||
while ((result == HasReturnvaluesIF::RETURN_OK) && (element != NULL)) {
|
||||
result = element->value->deSerialize(buffer, size, bigEndian);
|
||||
element = element->getNext();
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
bool printCount;
|
||||
|
||||
};
|
||||
|
||||
#endif /* SERIALLINKEDLISTADAPTER_H_ */
|
107
serialize/SerializeAdapter.h
Normal file
107
serialize/SerializeAdapter.h
Normal file
@ -0,0 +1,107 @@
|
||||
/*
|
||||
* SerializeAdapter.h
|
||||
*
|
||||
* Created on: 19.03.2014
|
||||
* Author: baetz
|
||||
*/
|
||||
|
||||
#ifndef SERIALIZEADAPTER_H_
|
||||
#define SERIALIZEADAPTER_H_
|
||||
|
||||
#include <framework/container/IsDerivedFrom.h>
|
||||
#include <framework/returnvalues/HasReturnvaluesIF.h>
|
||||
#include <framework/serialize/EndianSwapper.h>
|
||||
#include <framework/serialize/SerializeIF.h>
|
||||
#include <string.h>
|
||||
|
||||
template<typename T, int>
|
||||
class SerializeAdapter_ {
|
||||
public:
|
||||
static ReturnValue_t serialize(const T* object, uint8_t** buffer,
|
||||
uint32_t* size, const uint32_t max_size, bool bigEndian) {
|
||||
uint32_t ignoredSize = 0;
|
||||
if (size == NULL) {
|
||||
size = &ignoredSize;
|
||||
}
|
||||
if (sizeof(T) + *size <= max_size) {
|
||||
T tmp;
|
||||
if (bigEndian) {
|
||||
tmp = EndianSwapper::swap<T>(*object);
|
||||
} else {
|
||||
tmp = *object;
|
||||
}
|
||||
memcpy(*buffer, &tmp, sizeof(T));
|
||||
*size += sizeof(T);
|
||||
(*buffer) += sizeof(T);
|
||||
return HasReturnvaluesIF::RETURN_OK;
|
||||
} else {
|
||||
return SerializeIF::BUFFER_TOO_SHORT;
|
||||
}
|
||||
}
|
||||
|
||||
ReturnValue_t deSerialize(T* object, const uint8_t** buffer, int32_t* size,
|
||||
bool bigEndian) {
|
||||
T tmp;
|
||||
*size -= sizeof(T);
|
||||
if (*size >= 0) {
|
||||
memcpy(&tmp, *buffer, sizeof(T));
|
||||
if (bigEndian) {
|
||||
*object = EndianSwapper::swap<T>(tmp);
|
||||
} else {
|
||||
*object = tmp;
|
||||
}
|
||||
*buffer += sizeof(T);
|
||||
return HasReturnvaluesIF::RETURN_OK;
|
||||
} else {
|
||||
return SerializeIF::STREAM_TOO_SHORT;
|
||||
}
|
||||
}
|
||||
|
||||
uint32_t getSerializedSize(const T * object) {
|
||||
return sizeof(T);
|
||||
}
|
||||
|
||||
};
|
||||
|
||||
template<typename T>
|
||||
class SerializeAdapter_<T, 1> {
|
||||
public:
|
||||
ReturnValue_t serialize(const T* object, uint8_t** buffer, uint32_t* size,
|
||||
const uint32_t max_size, bool bigEndian) const {
|
||||
uint32_t ignoredSize = 0;
|
||||
if (size == NULL) {
|
||||
size = &ignoredSize;
|
||||
}
|
||||
return object->serialize(buffer, size, max_size, bigEndian);
|
||||
}
|
||||
uint32_t getSerializedSize(const T* object) const {
|
||||
return object->getSerializedSize();
|
||||
}
|
||||
|
||||
ReturnValue_t deSerialize(T* object, const uint8_t** buffer, int32_t* size,
|
||||
bool bigEndian) {
|
||||
return object->deSerialize(buffer, size, bigEndian);
|
||||
}
|
||||
};
|
||||
|
||||
template<typename T>
|
||||
class SerializeAdapter {
|
||||
public:
|
||||
static ReturnValue_t serialize(const T* object, uint8_t** buffer,
|
||||
uint32_t* size, const uint32_t max_size, bool bigEndian) {
|
||||
SerializeAdapter_<T, IsDerivedFrom<T, SerializeIF>::Is> adapter;
|
||||
return adapter.serialize(object, buffer, size, max_size, bigEndian);
|
||||
}
|
||||
static uint32_t getSerializedSize(const T* object) {
|
||||
SerializeAdapter_<T, IsDerivedFrom<T, SerializeIF>::Is> adapter;
|
||||
return adapter.getSerializedSize(object);
|
||||
}
|
||||
|
||||
static ReturnValue_t deSerialize(T* object, const uint8_t** buffer,
|
||||
int32_t* size, bool bigEndian) {
|
||||
SerializeAdapter_<T, IsDerivedFrom<T, SerializeIF>::Is> adapter;
|
||||
return adapter.deSerialize(object, buffer, size, bigEndian);
|
||||
}
|
||||
};
|
||||
|
||||
#endif /* SERIALIZEADAPTER_H_ */
|
53
serialize/SerializeElement.h
Normal file
53
serialize/SerializeElement.h
Normal file
@ -0,0 +1,53 @@
|
||||
/*
|
||||
* SerializeElement.h
|
||||
*
|
||||
* Created on: 24.03.2014
|
||||
* Author: baetz
|
||||
*/
|
||||
|
||||
#ifndef SERIALIZEELEMENT_H_
|
||||
#define SERIALIZEELEMENT_H_
|
||||
|
||||
#include <framework/container/SinglyLinkedList.h>
|
||||
#include <framework/serialize/SerializeAdapter.h>
|
||||
#include <utility>
|
||||
|
||||
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, uint32_t* size,
|
||||
const uint32_t max_size, bool bigEndian) const {
|
||||
return SerializeAdapter<T>::serialize(&entry, buffer, size, max_size, bigEndian);
|
||||
}
|
||||
|
||||
uint32_t getSerializedSize() const {
|
||||
return SerializeAdapter<T>::getSerializedSize(&entry);
|
||||
}
|
||||
|
||||
virtual ReturnValue_t deSerialize(const uint8_t** buffer, int32_t* size,
|
||||
bool bigEndian) {
|
||||
return SerializeAdapter<T>::deSerialize(&entry, buffer, size, bigEndian);
|
||||
}
|
||||
operator T() {
|
||||
return entry;
|
||||
}
|
||||
|
||||
SerializeElement<T> &operator=(T newValue) {
|
||||
entry = newValue;
|
||||
return *this;
|
||||
}
|
||||
T *operator->() {
|
||||
return &entry;
|
||||
}
|
||||
};
|
||||
|
||||
|
||||
|
||||
#endif /* SERIALIZEELEMENT_H_ */
|
33
serialize/SerializeIF.h
Normal file
33
serialize/SerializeIF.h
Normal file
@ -0,0 +1,33 @@
|
||||
/*
|
||||
* SerializeIF.h
|
||||
*
|
||||
* Created on: 19.03.2014
|
||||
* Author: baetz
|
||||
*/
|
||||
|
||||
#ifndef SERIALIZEIF_H_
|
||||
#define SERIALIZEIF_H_
|
||||
|
||||
#include <framework/returnvalues/HasReturnvaluesIF.h>
|
||||
|
||||
class SerializeIF {
|
||||
public:
|
||||
static const uint8_t INTERFACE_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, uint32_t* size,
|
||||
const uint32_t max_size, bool bigEndian) const = 0;
|
||||
|
||||
virtual uint32_t getSerializedSize() const = 0;
|
||||
|
||||
virtual ReturnValue_t deSerialize(const uint8_t** buffer, int32_t* size,
|
||||
bool bigEndian) = 0;
|
||||
|
||||
};
|
||||
|
||||
#endif /* SERIALIZEIF_H_ */
|
Reference in New Issue
Block a user