2016-06-15 23:48:41 +02:00
|
|
|
#ifndef SERIALBUFFERADAPTER_H_
|
|
|
|
#define SERIALBUFFERADAPTER_H_
|
|
|
|
|
|
|
|
#include <framework/serialize/SerializeIF.h>
|
2018-07-12 16:29:32 +02:00
|
|
|
#include <framework/serialize/SerializeAdapter.h>
|
2016-06-15 23:48:41 +02:00
|
|
|
|
2019-08-28 14:50:24 +02:00
|
|
|
/**
|
2019-11-02 23:30:12 +01:00
|
|
|
* This adapter provides an interface for SerializeIF to serialize or deserialize
|
|
|
|
* buffers with no length header but a known size.
|
|
|
|
*
|
2020-04-13 16:27:05 +02:00
|
|
|
* Additionally, the buffer length can be serialized too and will be put in
|
|
|
|
* front of the serialized buffer.
|
2019-11-02 23:30:12 +01:00
|
|
|
*
|
|
|
|
* Can be used with SerialLinkedListAdapter by declaring a SerializeElement with
|
2020-04-13 16:27:05 +02:00
|
|
|
* SerialElement<SerialBufferAdapter<bufferLengthType(will be uint8_t mostly)>>.
|
|
|
|
* Right now, the SerialBufferAdapter must always
|
|
|
|
* be initialized with the buffer and size !
|
2019-11-02 23:30:12 +01:00
|
|
|
*
|
2019-08-28 14:50:24 +02:00
|
|
|
* \ingroup serialize
|
|
|
|
*/
|
2020-04-05 15:30:31 +02:00
|
|
|
template<typename count_t>
|
2016-06-15 23:48:41 +02:00
|
|
|
class SerialBufferAdapter: public SerializeIF {
|
|
|
|
public:
|
2020-04-05 15:30:31 +02:00
|
|
|
|
2019-12-01 17:48:05 +01:00
|
|
|
/**
|
|
|
|
* 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
|
|
|
|
*/
|
2020-06-05 18:20:21 +02:00
|
|
|
SerialBufferAdapter(const uint8_t* buffer, count_t bufferLength,
|
2020-04-13 16:27:05 +02:00
|
|
|
bool serializeLength = false);
|
2019-12-01 17:48:05 +01:00
|
|
|
|
|
|
|
/**
|
2020-04-13 16:27:05 +02:00
|
|
|
* Constructor for non-constant uint8_t buffer.
|
|
|
|
* Length field can be serialized optionally.
|
2019-12-01 17:48:05 +01:00
|
|
|
* Type of length can be supplied as template type.
|
|
|
|
* @param buffer
|
|
|
|
* @param bufferLength
|
2020-04-13 16:27:05 +02:00
|
|
|
* @param serializeLength Length field will be serialized with size count_t
|
2019-12-01 17:48:05 +01:00
|
|
|
*/
|
2020-06-05 18:20:21 +02:00
|
|
|
SerialBufferAdapter(uint8_t* buffer, count_t bufferLength,
|
|
|
|
bool serializeLength = false);
|
2016-06-15 23:48:41 +02:00
|
|
|
|
|
|
|
virtual ~SerialBufferAdapter();
|
|
|
|
|
2020-06-11 16:19:22 +02:00
|
|
|
virtual ReturnValue_t serialize(uint8_t** buffer_, size_t* size,
|
2020-04-15 20:53:03 +02:00
|
|
|
const size_t max_size, bool bigEndian) const override;
|
2016-06-15 23:48:41 +02:00
|
|
|
|
2020-04-05 21:54:11 +02:00
|
|
|
virtual size_t getSerializedSize() const;
|
2016-06-15 23:48:41 +02:00
|
|
|
|
2020-04-15 15:53:46 +02:00
|
|
|
/**
|
|
|
|
* @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,
|
2020-04-15 20:53:03 +02:00
|
|
|
bool bigEndian) override;
|
2019-12-01 17:48:05 +01:00
|
|
|
|
|
|
|
uint8_t * getBuffer();
|
2020-01-13 01:21:53 +01:00
|
|
|
const uint8_t * getConstBuffer();
|
2020-06-05 18:20:21 +02:00
|
|
|
void setBuffer(uint8_t* buffer_, count_t bufferLength_);
|
2016-06-15 23:48:41 +02:00
|
|
|
private:
|
2020-04-07 22:16:43 +02:00
|
|
|
bool serializeLength = false;
|
|
|
|
const uint8_t *constBuffer = nullptr;
|
2020-06-05 18:20:21 +02:00
|
|
|
uint8_t *buffer = nullptr;
|
2020-04-07 22:16:43 +02:00
|
|
|
count_t bufferLength = 0;
|
2016-06-15 23:48:41 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
#endif /* SERIALBUFFERADAPTER_H_ */
|