2020-08-25 14:14:58 +02:00
|
|
|
#ifndef FSFW_SERIALIZE_SERIALIZEIF_H_
|
|
|
|
#define FSFW_SERIALIZE_SERIALIZEIF_H_
|
2016-06-15 23:48:41 +02:00
|
|
|
|
2020-09-04 15:40:42 +02:00
|
|
|
#include <cstddef>
|
2016-06-15 23:48:41 +02:00
|
|
|
|
2022-07-23 11:10:44 +02:00
|
|
|
#include "fsfw/returnvalues/HasReturnvaluesIF.h"
|
2022-02-02 10:29:30 +01:00
|
|
|
|
2019-08-28 14:50:24 +02:00
|
|
|
/**
|
2020-09-01 11:34:28 +02:00
|
|
|
* @defgroup serialize Serialization
|
2020-08-25 14:14:58 +02:00
|
|
|
* Contains serialization services.
|
2019-08-28 14:50:24 +02:00
|
|
|
*/
|
|
|
|
|
|
|
|
/**
|
2020-09-04 15:40:42 +02:00
|
|
|
* @brief Translation of objects into data streams and from data streams.
|
|
|
|
* @details
|
|
|
|
* Also provides options to convert from/to data with different endianness.
|
|
|
|
* variables.
|
2020-09-01 11:34:28 +02:00
|
|
|
* @ingroup serialize
|
2019-08-28 14:50:24 +02:00
|
|
|
*/
|
2016-06-15 23:48:41 +02:00
|
|
|
class SerializeIF {
|
2022-02-02 10:29:30 +01:00
|
|
|
public:
|
|
|
|
enum class Endianness : uint8_t {
|
|
|
|
BIG,
|
|
|
|
LITTLE,
|
|
|
|
MACHINE,
|
|
|
|
NETWORK = BIG // Added for convenience like htons on sockets
|
|
|
|
};
|
2016-06-15 23:48:41 +02:00
|
|
|
|
2022-02-02 10:29:30 +01:00
|
|
|
static const uint8_t INTERFACE_ID = CLASS_ID::SERIALIZE_IF;
|
|
|
|
static const ReturnValue_t BUFFER_TOO_SHORT =
|
|
|
|
MAKE_RETURN_CODE(1); // !< The given buffer in serialize is too short
|
|
|
|
static const ReturnValue_t STREAM_TOO_SHORT =
|
|
|
|
MAKE_RETURN_CODE(2); // !< The input stream in deserialize is too short
|
|
|
|
static const ReturnValue_t TOO_MANY_ELEMENTS =
|
|
|
|
MAKE_RETURN_CODE(3); // !< There are too many elements to be deserialized
|
2016-06-15 23:48:41 +02:00
|
|
|
|
2022-07-23 10:06:42 +02:00
|
|
|
virtual ~SerializeIF() = default;
|
2022-02-02 10:29:30 +01:00
|
|
|
/**
|
|
|
|
* @brief
|
|
|
|
* Function to serialize the object into a buffer with maxSize. Size represents the written
|
|
|
|
* amount. If a part of the buffer has been used already, size must be set to the used amount of
|
|
|
|
* bytes.
|
|
|
|
*
|
|
|
|
* @details
|
|
|
|
* Implementations of this function must increase the size variable and move the buffer pointer.
|
|
|
|
* MaxSize must be checked by implementations of this function
|
|
|
|
* and BUFFER_TOO_SHORT has to be returned if size would be larger than maxSize.
|
|
|
|
*
|
|
|
|
* Custom implementations might use additional return values.
|
|
|
|
*
|
|
|
|
* @param[in/out] buffer Buffer to serialize into, will be set to the current write location
|
|
|
|
* @param[in/out] size Size that has been used in the buffer already, will be increased by the
|
|
|
|
* function
|
|
|
|
* @param[in] maxSize The size of the buffer that is allowed to be used for serialize.
|
|
|
|
* @param[in] streamEndianness Endianness of the serialized data according to
|
|
|
|
* SerializeIF::Endianness
|
|
|
|
* @return
|
|
|
|
* - @c BUFFER_TOO_SHORT The given buffer in is too short
|
|
|
|
* - @c RETURN_FAILED Generic error
|
|
|
|
* - @c RETURN_OK Successful serialization
|
|
|
|
*/
|
2022-07-25 14:44:19 +02:00
|
|
|
[[nodiscard]] virtual ReturnValue_t serialize(uint8_t **buffer, size_t *size, size_t maxSize,
|
2022-07-25 14:44:49 +02:00
|
|
|
Endianness streamEndianness) const = 0;
|
2022-07-23 10:28:01 +02:00
|
|
|
/**
|
2022-07-25 11:33:12 +02:00
|
|
|
* Forwards to regular @serialize call with big (network) endianness
|
2022-07-23 10:28:01 +02:00
|
|
|
*/
|
2022-07-25 14:44:49 +02:00
|
|
|
[[nodiscard]] virtual ReturnValue_t serializeBe(uint8_t **buffer, size_t *size,
|
|
|
|
size_t maxSize) const {
|
2022-07-23 10:28:01 +02:00
|
|
|
return serialize(buffer, size, maxSize, SerializeIF::Endianness::NETWORK);
|
|
|
|
}
|
|
|
|
/**
|
|
|
|
* If endianness is not explicitly specified, use machine endianness
|
|
|
|
*/
|
2022-07-25 14:44:49 +02:00
|
|
|
[[nodiscard]] virtual ReturnValue_t serialize(uint8_t **buffer, size_t *size,
|
|
|
|
size_t maxSize) const {
|
2022-07-23 10:28:01 +02:00
|
|
|
return serialize(buffer, size, maxSize, SerializeIF::Endianness::MACHINE);
|
|
|
|
}
|
2016-06-15 23:48:41 +02:00
|
|
|
|
2022-02-02 10:29:30 +01:00
|
|
|
/**
|
|
|
|
* Gets the size of a object if it would be serialized in a buffer
|
|
|
|
* @return Size of serialized object
|
|
|
|
*/
|
2022-07-23 10:06:42 +02:00
|
|
|
[[nodiscard]] virtual size_t getSerializedSize() const = 0;
|
2016-06-15 23:48:41 +02:00
|
|
|
|
2022-02-02 10:29:30 +01:00
|
|
|
/**
|
|
|
|
* @brief
|
|
|
|
* Deserializes a object from a given buffer of given size.
|
|
|
|
*
|
|
|
|
* @details
|
|
|
|
* Buffer must be moved to the current read location by the implementation
|
|
|
|
* of this function. Size must be decreased by the implementation.
|
|
|
|
* Implementations are not allowed to alter the buffer as indicated by const pointer.
|
|
|
|
*
|
|
|
|
* Custom implementations might use additional return values.
|
|
|
|
*
|
|
|
|
* @param[in/out] buffer Buffer to deSerialize from. Will be moved by the function.
|
|
|
|
* @param[in/out] size Remaining size of the buffer to read from. Will be decreased by function.
|
|
|
|
* @param[in] streamEndianness Endianness as in according to SerializeIF::Endianness
|
|
|
|
* @return
|
|
|
|
* - @c STREAM_TOO_SHORT The input stream is too short to deSerialize the object
|
|
|
|
* - @c TOO_MANY_ELEMENTS The buffer has more inputs than expected
|
|
|
|
* - @c RETURN_FAILED Generic Error
|
|
|
|
* - @c RETURN_OK Successful deserialization
|
|
|
|
*/
|
|
|
|
virtual ReturnValue_t deSerialize(const uint8_t **buffer, size_t *size,
|
|
|
|
Endianness streamEndianness) = 0;
|
2022-07-23 10:28:01 +02:00
|
|
|
/**
|
2022-07-25 11:33:12 +02:00
|
|
|
* Forwards to regular @deSerialize call with big (network) endianness
|
2022-07-23 10:28:01 +02:00
|
|
|
*/
|
2022-07-25 14:44:49 +02:00
|
|
|
virtual ReturnValue_t deSerializeBe(const uint8_t **buffer, size_t *size) {
|
2022-07-23 10:28:01 +02:00
|
|
|
return deSerialize(buffer, size, SerializeIF::Endianness::NETWORK);
|
|
|
|
}
|
|
|
|
/**
|
|
|
|
* If endianness is not explicitly specified, use machine endianness
|
|
|
|
*/
|
2022-07-25 14:44:49 +02:00
|
|
|
virtual ReturnValue_t deSerialize(const uint8_t **buffer, size_t *size) {
|
2022-07-23 10:28:01 +02:00
|
|
|
return deSerialize(buffer, size, SerializeIF::Endianness::MACHINE);
|
|
|
|
}
|
2022-07-23 10:03:42 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Helper method which can be used if serialization should be performed without any additional
|
|
|
|
* pointer arithmetic on a passed buffer pointer
|
|
|
|
* @param buffer
|
|
|
|
* @param maxSize
|
|
|
|
* @param streamEndianness
|
|
|
|
* @return
|
|
|
|
*/
|
2022-07-25 14:44:19 +02:00
|
|
|
[[nodiscard]] virtual ReturnValue_t serialize(uint8_t *buffer, size_t maxSize,
|
2022-07-25 14:44:49 +02:00
|
|
|
Endianness streamEndianness) const {
|
2022-07-23 10:03:42 +02:00
|
|
|
size_t tmpSize = 0;
|
|
|
|
return serialize(&buffer, &tmpSize, maxSize, streamEndianness);
|
|
|
|
}
|
2022-07-23 10:28:01 +02:00
|
|
|
/**
|
2022-07-25 11:33:12 +02:00
|
|
|
* Forwards to regular @serialize call with big (network) endianness
|
2022-07-23 10:28:01 +02:00
|
|
|
*/
|
2022-07-25 14:44:19 +02:00
|
|
|
[[nodiscard]] virtual ReturnValue_t serializeBe(uint8_t *buffer, size_t maxSize) const {
|
2022-07-23 10:28:01 +02:00
|
|
|
return serialize(buffer, maxSize, SerializeIF::Endianness::NETWORK);
|
|
|
|
}
|
|
|
|
/**
|
|
|
|
* If endianness is not explicitly specified, use machine endianness
|
|
|
|
*/
|
2022-07-25 14:44:19 +02:00
|
|
|
[[nodiscard]] virtual ReturnValue_t serialize(uint8_t *buffer, size_t maxSize) const {
|
2022-07-23 10:28:01 +02:00
|
|
|
return serialize(buffer, maxSize, SerializeIF::Endianness::MACHINE);
|
|
|
|
}
|
2022-07-23 10:03:42 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Helper methods which can be used if deserialization should be performed without any additional
|
|
|
|
* pointer arithmetic on a passed buffer pointer
|
|
|
|
* @param buffer
|
|
|
|
* @param maxSize
|
|
|
|
* @param streamEndianness
|
|
|
|
* @return
|
|
|
|
*/
|
2022-07-23 10:06:42 +02:00
|
|
|
virtual ReturnValue_t deSerialize(const uint8_t *buffer, size_t maxSize,
|
2022-07-23 10:03:42 +02:00
|
|
|
Endianness streamEndianness) {
|
|
|
|
return deSerialize(&buffer, &maxSize, streamEndianness);
|
|
|
|
}
|
2022-07-23 10:28:01 +02:00
|
|
|
/**
|
2022-07-25 11:33:12 +02:00
|
|
|
* Forwards to regular @serialize call with big (network) endianness
|
2022-07-23 10:28:01 +02:00
|
|
|
*/
|
2022-07-25 11:33:12 +02:00
|
|
|
virtual ReturnValue_t deSerializeBe(const uint8_t *buffer, size_t maxSize) {
|
2022-07-23 11:10:44 +02:00
|
|
|
return deSerialize(buffer, maxSize, SerializeIF::Endianness::NETWORK);
|
2022-07-23 10:28:01 +02:00
|
|
|
}
|
|
|
|
/**
|
|
|
|
* If endianness is not explicitly specified, use machine endianness
|
|
|
|
*/
|
2022-07-23 11:10:44 +02:00
|
|
|
virtual ReturnValue_t deSerialize(const uint8_t *buffer, size_t maxSize) {
|
|
|
|
return deSerialize(buffer, maxSize, SerializeIF::Endianness::MACHINE);
|
2022-07-23 10:28:01 +02:00
|
|
|
}
|
2016-06-15 23:48:41 +02:00
|
|
|
};
|
|
|
|
|
2020-08-25 14:14:58 +02:00
|
|
|
#endif /* FSFW_SERIALIZE_SERIALIZEIF_H_ */
|