fsfw/src/fsfw/cfdp/tlv/Tlv.cpp
Robin Mueller 5907f8ee9d
Added CFDP packet stack
This PR adds the packet stack for the CCSDS File Delivery Protocol.
It also refactors the existing TMTC infastructure to allow sending
of CFDP packets to the CCSDS handlers.

This includes the whole PDU (Protocol Data Unit) stack:

- File Data PDUs

and all file directive PDUs

- ACK PDU
- NAK PDU
- Metadata PDU
- Finished PDU
- Prompt PDU
- Keep Alive PDU
- EOF PDU

The PR includes a full set of unittests for the packet stack
with a coverage of 90+ %.

The refactoring of the existing TMTC infastructure includes non-ideal
solutions like diamond inheritance.
Avoiding this solution would require refactoring the packet stack.
This would be a good idea anyway because the existing stack is tightly
coupled to the FSFW, making reuse more difficult if only the stack is
planned to be used without the store functionalities etc.

The PDU implementation provided here is only weakly coupled to the FSFW,
only using components like returnvalues or the Serialization modules.
There are dedicated serializers and deserializers, which also helps in
creating small focused modules which are easy to test.

Some of the modules here were provied by Matthias Tompert.
2021-12-03 15:37:49 +01:00

115 lines
2.7 KiB
C++

#include "Tlv.h"
cfdp::Tlv::Tlv(TlvTypes type, const uint8_t *value, size_t size): type(type),
value(value, size, true) {
if(size > 0) {
zeroLen = false;
}
}
cfdp::Tlv::Tlv(): value(static_cast<uint8_t*>(nullptr), 0, true) {
}
ReturnValue_t cfdp::Tlv::serialize(uint8_t **buffer, size_t *size, size_t maxSize,
Endianness streamEndianness) const {
if(buffer == nullptr or size == nullptr) {
return HasReturnvaluesIF::RETURN_FAILED;
}
if(*size + 2 > maxSize) {
return BUFFER_TOO_SHORT;
}
if(type == TlvTypes::INVALID_TLV) {
return INVALID_TLV_TYPE;
}
**buffer = type;
*size += 1;
*buffer += 1;
if(zeroLen) {
**buffer = 0;
*size += 1;
*buffer += 1;
return HasReturnvaluesIF::RETURN_OK;
}
if(value.getConstBuffer() == nullptr) {
return HasReturnvaluesIF::RETURN_FAILED;
}
return value.serialize(buffer, size, maxSize, streamEndianness);
}
size_t cfdp::Tlv::getSerializedSize() const {
if(zeroLen) {
return 2;
}
else if (value.getConstBuffer() == nullptr) {
return 0;
}
return value.getSerializedSize() + 1;
}
ReturnValue_t cfdp::Tlv::deSerialize(const uint8_t **buffer, size_t *size,
Endianness streamEndianness) {
if(buffer == nullptr or size == nullptr) {
return HasReturnvaluesIF::RETURN_FAILED;
}
if(*size < 2) {
return STREAM_TOO_SHORT;
}
uint8_t rawType = **buffer;
if(not checkType(rawType)) {
return INVALID_TLV_TYPE;
}
type = static_cast<TlvTypes>(rawType);
*buffer += 1;
*size -= 1;
size_t lengthField = **buffer;
if(lengthField == 0) {
zeroLen = true;
*buffer += 1;
*size -= 1;
return HasReturnvaluesIF::RETURN_OK;
}
if(lengthField + 1 > *size) {
return SerializeIF::STREAM_TOO_SHORT;
}
zeroLen = false;
// Zero-copy implementation
value.setBuffer(const_cast<uint8_t*>(*buffer + 1), lengthField);
*buffer += 1 + lengthField;
*size -= 1 + lengthField;
return HasReturnvaluesIF::RETURN_OK;
}
const uint8_t* cfdp::Tlv::getValue() const {
return value.getConstBuffer();
}
cfdp::TlvTypes cfdp::Tlv::getType() const {
return type;
}
bool cfdp::Tlv::checkType(uint8_t rawType) {
if (rawType != 0x03 and rawType <= 6) {
return true;
}
return false;
}
void cfdp::Tlv::setValue(uint8_t *value, size_t len) {
if(len > 0) {
zeroLen = false;
}
this->value.setBuffer(value, len);
}
uint8_t cfdp::Tlv::getLengthField() const {
return this->value.getSerializedSize() - 1;
}
void cfdp::Tlv::setType(TlvTypes type) {
this->type = type;
}