fsfw/src/fsfw/cfdp/VarLenFields.h

136 lines
4.1 KiB
C++

#ifndef FSFW_CFDP_PDU_VARLENFIELD_H_
#define FSFW_CFDP_PDU_VARLENFIELD_H_
#include <cstddef>
#include <cstdint>
#include <utility>
#include "fsfw/cfdp/definitions.h"
#include "fsfw/serialize/SerializeIF.h"
#include "fsfw/serviceinterface.h"
#include "fsfw/util/UnsignedByteField.h"
namespace cfdp {
class VarLenField : public SerializeIF {
public:
union LengthFieldLen {
uint8_t oneByte;
uint16_t twoBytes;
uint32_t fourBytes;
uint64_t eightBytes;
};
VarLenField();
template <typename T>
explicit VarLenField(UnsignedByteField<T> byteField);
VarLenField(cfdp::WidthInBytes width, uint64_t value);
bool operator==(const VarLenField &other) const;
bool operator!=(const VarLenField &other) const;
bool operator<(const VarLenField &other) const;
ReturnValue_t setValueAndWidth(cfdp::WidthInBytes width, uint64_t value);
void setWidth(cfdp::WidthInBytes width);
ReturnValue_t setValue(uint64_t value);
ReturnValue_t serialize(uint8_t **buffer, size_t *size, size_t maxSize,
Endianness streamEndianness) const override;
[[nodiscard]] size_t getSerializedSize() const override;
ReturnValue_t deSerialize(cfdp::WidthInBytes width, const uint8_t **buffer, size_t *size,
Endianness streamEndianness);
[[nodiscard]] cfdp::WidthInBytes getWidth() const;
[[nodiscard]] uint64_t getValue() const;
#if FSFW_CPP_OSTREAM_ENABLED == 1
friend std::ostream &operator<<(std::ostream &os, const VarLenField &id) {
os << "dec: " << id.getValue() << ", hex: " << std::hex << std::setw(id.getWidth())
<< std::setfill('0') << id.getValue() << std::dec << std::setfill('0');
return os;
}
#endif
private:
ReturnValue_t deSerialize(const uint8_t **buffer, size_t *size,
Endianness streamEndianness) override;
cfdp::WidthInBytes width;
LengthFieldLen value{};
};
template <typename T>
cfdp::VarLenField::VarLenField(UnsignedByteField<T> byteField)
: width(static_cast<cfdp::WidthInBytes>(sizeof(T))) {
static_assert((sizeof(T) % 2) == 0);
setValueAndWidth(width, byteField.getValue());
}
struct EntityId : public VarLenField {
public:
EntityId() : VarLenField() {}
template <typename T>
explicit EntityId(UnsignedByteField<T> byteField) : VarLenField(byteField) {}
EntityId(cfdp::WidthInBytes width, size_t entityId) : VarLenField(width, entityId) {}
ReturnValue_t serializeAsLv(uint8_t **buffer, size_t *size, size_t maxSize) const {
if (buffer == nullptr or size == nullptr) {
return returnvalue::FAILED;
}
if (*size + 1 + getWidth() > maxSize) {
return SerializeIF::BUFFER_TOO_SHORT;
}
**buffer = getWidth();
*buffer += 1;
*size += 1;
return serialize(buffer, size, maxSize, SerializeIF::Endianness::NETWORK);
}
ReturnValue_t deSerializeFromLv(const uint8_t **buffer, size_t *deserLen) {
if (buffer == nullptr or deserLen == nullptr) {
return returnvalue::FAILED;
}
if (*deserLen < 2) {
return SerializeIF::STREAM_TOO_SHORT;
}
auto width = static_cast<WidthInBytes>(**buffer);
*buffer += 1;
*deserLen -= 1;
return VarLenField::deSerialize(width, buffer, deserLen, SerializeIF::Endianness::NETWORK);
}
};
struct TransactionSeqNum : public VarLenField {
public:
TransactionSeqNum() : VarLenField() {}
template <typename T>
explicit TransactionSeqNum(UnsignedByteField<T> byteField) : VarLenField(byteField) {}
TransactionSeqNum(cfdp::WidthInBytes width, size_t seqNum) : VarLenField(width, seqNum) {}
};
struct TransactionId {
TransactionId() = default;
TransactionId(EntityId entityId, TransactionSeqNum seqNum)
: entityId(std::move(entityId)), seqNum(std::move(seqNum)) {}
bool operator==(const TransactionId &other) const {
return entityId == other.entityId and seqNum == other.seqNum;
}
#if FSFW_CPP_OSTREAM_ENABLED == 1
friend std::ostream &operator<<(std::ostream &os, const TransactionId &id) {
os << "Source ID { " << id.entityId << " }, Sequence Number " << id.seqNum.getValue();
return os;
}
#endif
EntityId entityId;
TransactionSeqNum seqNum;
};
} // namespace cfdp
#endif /* FSFW_CFDP_PDU_VARLENFIELD_H_ */