fsfw/src/fsfw/cfdp/tlv/FilestoreTlvBase.h
Robin Mueller 0ac1d1c1ca 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:33:07 +01:00

177 lines
5.4 KiB
C++

#ifndef FSFW_SRC_FSFW_CFDP_FILESTOREREQUESTBASE_H_
#define FSFW_SRC_FSFW_CFDP_FILESTOREREQUESTBASE_H_
#include <fsfw/cfdp/definitions.h>
#include <fsfw/cfdp/tlv/Lv.h>
#include <fsfw/cfdp/tlv/Tlv.h>
#include <fsfw/cfdp/tlv/TlvIF.h>
#include <fsfw/serialize/SerializeIF.h>
#include <cstddef>
#include <cstdint>
namespace cfdp {
enum FilestoreActionCode {
CREATE_FILE = 0b0000,
DELETE_FILE = 0b0001,
// Second file name present
RENAME_FILE = 0b0010,
// Second file name present
APPEND_FILE = 0b0011,
// Second file name present
REPLACE_FILE = 0b0100,
CREATE_DIRECTORY = 0b0101,
REMOVE_DIRECTORY = 0b0110,
// Delete file if present
DENY_FILE = 0b0111,
// Remove directory if present
DNEY_DIRECTORY = 0b1000,
INVALID = 0b1111
};
// FSR = Filestore Response
static constexpr uint8_t FSR_SUCCESS = 0b0000;
static constexpr uint8_t FSR_NOT_PERFORMED = 0b1111;
static constexpr uint8_t FSR_CREATE_NOT_ALLOWED = 0b0001;
static constexpr uint8_t FSR_DEL_FILE_NOT_EXISTS = 0b0001;
static constexpr uint8_t FSR_DEL_NOT_ALLOWED = 0b0010;
static constexpr uint8_t FSR_RENAME_OLD_FILE_NOT_EXISTS = 0b0001;
static constexpr uint8_t FSR_RENAME_NEW_FILE_ALREADY_EXISTS = 0b0010;
static constexpr uint8_t FSR_RENAME_NOT_ALLOWED = 0b0011;
static constexpr uint8_t FSR_APPEND_FILE_1_NOT_EXISTS = 0b0001;
static constexpr uint8_t FSR_APPEND_FILE_2_NOT_EXISTS = 0b0010;
static constexpr uint8_t FSR_APPEND_NOT_ALLOWED = 0b0011;
static constexpr uint8_t FSR_REPLACE_FILE_1_NOT_EXISTS = 0b0001;
static constexpr uint8_t FSR_REPLACE_FILE_2_NOT_EXISTS = 0b0010;
static constexpr uint8_t FSR_REPLACE_REPLACE_NOT_ALLOWED = 0b0011;
static constexpr uint8_t FSR_CREATE_DIR_NOT_POSSIBLE = 0b0001;
static constexpr uint8_t FSR_DELDIR_NOT_EXISTS = 0b0001;
static constexpr uint8_t FSR_DELDIR_NOT_ALLOWED = 0b0010;
static constexpr uint8_t FSR_DENY_FILE_NOT_ALLOWED = 0b0010;
static constexpr uint8_t FSR_DENY_DIR_NOT_ALLOWED = 0b0010;
class FilestoreTlvBase: public TlvIF {
public:
FilestoreTlvBase(cfdp::Lv& firstFileName): firstFileName(firstFileName) {};
FilestoreTlvBase(FilestoreActionCode actionCode, cfdp::Lv& firstFileName):
actionCode(actionCode), firstFileName(firstFileName) {};
ReturnValue_t commonSerialize(uint8_t **buffer, size_t *size,
size_t maxSize, Endianness streamEndianness,
bool isResponse = false, uint8_t responseStatusCode = 0) const {
if(buffer == nullptr or size == nullptr) {
return HasReturnvaluesIF::RETURN_FAILED;
}
if(maxSize < 3) {
return SerializeIF::BUFFER_TOO_SHORT;
}
**buffer = getType();
*buffer += 1;
*size += 1;
**buffer = getLengthField();
*buffer += 1;
*size += 1;
**buffer = this->actionCode << 4;
if(isResponse) {
**buffer |= responseStatusCode;
}
*buffer += 1;
*size += 1;
return HasReturnvaluesIF::RETURN_OK;
}
ReturnValue_t commonDeserialize(const uint8_t **buffer, size_t *size,
SerializeIF::Endianness streamEndianness) {
if(buffer == nullptr or size == nullptr) {
return HasReturnvaluesIF::RETURN_FAILED;
}
if(*size < 3) {
return SerializeIF::STREAM_TOO_SHORT;
}
cfdp::TlvTypes type = static_cast<cfdp::TlvTypes>(**buffer);
if(type != getType()) {
return cfdp::INVALID_TLV_TYPE;
}
*size -= 1;
*buffer += 1;
size_t remainingLength = **buffer;
*size -= 1;
*buffer += 1;
if(remainingLength == 0) {
return SerializeIF::STREAM_TOO_SHORT;
}
return HasReturnvaluesIF::RETURN_OK;
}
bool requiresSecondFileName() const {
using namespace cfdp;
if(actionCode == FilestoreActionCode::RENAME_FILE or
actionCode == FilestoreActionCode::APPEND_FILE or
actionCode == FilestoreActionCode::REPLACE_FILE) {
return true;
}
return false;
}
void secondFileNameMissing() const {
#if FSFW_VERBOSE_LEVEL >= 1
#if FSFW_CPP_OSTREAM_ENABLED == 1
sif::warning << "FilestoreRequestTlv::deSerialize: Second file name required"
" but TLV pointer not set" << std::endl;
#else
sif::printWarning("FilestoreRequestTlv::deSerialize: Second file name required"
" but TLV pointer not set\n");
#endif
#endif
}
FilestoreActionCode getActionCode() const {
return actionCode;
}
void setActionCode(FilestoreActionCode actionCode) {
this->actionCode = actionCode;
}
cfdp::Lv& getFirstFileName() {
return firstFileName;
}
ReturnValue_t convertToTlv(cfdp::Tlv& tlv, uint8_t *buffer, size_t maxSize,
Endianness streamEndianness) {
size_t serSize = 0;
uint8_t* valueStart = buffer + 2;
ReturnValue_t result = this->serialize(&buffer, &serSize, maxSize, streamEndianness);
if(result != HasReturnvaluesIF::RETURN_OK) {
return result;
}
tlv.setValue(valueStart, serSize - 2);
tlv.setType(getType());
return result;
}
size_t getSerializedSize() const override {
return getLengthField() + 2;
}
protected:
FilestoreActionCode actionCode = FilestoreActionCode::INVALID;
cfdp::Lv& firstFileName;
};
}
#endif /* FSFW_SRC_FSFW_CFDP_FILESTOREREQUESTBASE_H_ */