fsfw/src/fsfw/serialize/SerialBufferAdapter.h

78 lines
2.6 KiB
C
Raw Normal View History

2020-09-18 13:15:14 +02:00
#ifndef SERIALBUFFERADAPTER_H_
#define SERIALBUFFERADAPTER_H_
2021-07-13 20:22:54 +02:00
#include "fsfw/serialize/SerializeAdapter.h"
2022-02-02 10:29:30 +01:00
#include "fsfw/serialize/SerializeIF.h"
2020-09-18 13:15:14 +02:00
/**
* 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
*/
2022-02-02 10:29:30 +01:00
template <typename count_t>
class SerialBufferAdapter : public SerializeIF {
public:
2022-09-05 15:30:53 +02:00
SerialBufferAdapter() = default;
2022-02-02 10:29:30 +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
*/
SerialBufferAdapter(const uint8_t* buffer, count_t bufferLength, bool serializeLength = false);
2020-09-18 13:15:14 +02:00
2022-02-02 10:29:30 +01:00
/**
* 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);
2020-09-18 13:15:14 +02:00
2022-08-10 09:39:57 +02:00
~SerialBufferAdapter() override;
2020-09-18 13:15:14 +02:00
2022-09-05 15:30:53 +02:00
ReturnValue_t serialize(uint8_t** buffer, size_t* size, size_t maxSize,
2022-08-10 09:39:57 +02:00
Endianness streamEndianness) const override;
2020-09-18 13:15:14 +02:00
2022-08-10 09:39:57 +02:00
[[nodiscard]] size_t getSerializedSize() const override;
2020-09-18 13:15:14 +02:00
2022-02-02 10:29:30 +01: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.
2022-08-10 09:39:57 +02:00
* @param buffer_ [out] Resulting buffer
2022-02-02 10:29:30 +01:00
* @param size remaining size to deserialize, should be larger than buffer
* + size field size
* @param bigEndian
* @return
*/
2022-09-05 15:30:53 +02:00
ReturnValue_t deSerialize(const uint8_t** buffer, size_t* size,
2022-08-10 09:39:57 +02:00
Endianness streamEndianness) override;
2020-09-18 13:15:14 +02:00
2022-02-02 10:29:30 +01:00
uint8_t* getBuffer();
2022-08-10 09:39:57 +02:00
[[nodiscard]] const uint8_t* getConstBuffer() const;
2022-09-05 16:25:02 +02:00
void setBuffer(const uint8_t* buf, count_t bufLen);
2020-09-18 13:15:14 +02:00
2022-02-02 10:29:30 +01:00
private:
bool serializeLength = false;
const uint8_t* constBuffer = nullptr;
uint8_t* buffer = nullptr;
count_t bufferLength = 0;
2020-09-18 13:15:14 +02:00
};
#endif /* SERIALBUFFERADAPTER_H_ */