clean up CFDP stack a bit
fsfw/fsfw/pipeline/pr-development There was a failure building this commit Details

This commit is contained in:
Robin Müller 2022-08-08 18:29:32 +02:00
parent 202d9341d8
commit 904abfba28
No known key found for this signature in database
GPG Key ID: 71B58F8A3CDFA9AC
18 changed files with 96 additions and 53 deletions

View File

@ -1,5 +1,5 @@
#ifndef FSFW_SRC_FSFW_CFDP_FILESIZE_H_
#define FSFW_SRC_FSFW_CFDP_FILESIZE_H_
#ifndef FSFW_CFDP_FILESIZE_H_
#define FSFW_CFDP_FILESIZE_H_
#include "fsfw/serialize/SerializeAdapter.h"
#include "fsfw/serialize/SerializeIF.h"
@ -10,7 +10,7 @@ struct FileSize : public SerializeIF {
public:
FileSize() : largeFile(false){};
FileSize(uint64_t fileSize, bool isLarge = false) { setFileSize(fileSize, isLarge); };
explicit FileSize(uint64_t fileSize, bool isLarge = false) { setFileSize(fileSize, isLarge); };
ReturnValue_t serialize(bool isLarge, uint8_t **buffer, size_t *size, size_t maxSize,
Endianness streamEndianness) {
@ -27,7 +27,7 @@ struct FileSize : public SerializeIF {
return SerializeAdapter::serialize(&fileSize, buffer, size, maxSize, streamEndianness);
}
size_t getSerializedSize() const override {
[[nodiscard]] size_t getSerializedSize() const override {
if (largeFile) {
return 8;
} else {
@ -60,7 +60,7 @@ struct FileSize : public SerializeIF {
return HasReturnvaluesIF::RETURN_OK;
}
bool isLargeFile() const { return largeFile; }
[[nodiscard]] bool isLargeFile() const { return largeFile; }
uint64_t getSize(bool *largeFile = nullptr) const {
if (largeFile != nullptr) {
*largeFile = this->largeFile;
@ -75,4 +75,4 @@ struct FileSize : public SerializeIF {
} // namespace cfdp
#endif /* FSFW_SRC_FSFW_CFDP_FILESIZE_H_ */
#endif /* FSFW_CFDP_FILESIZE_H_ */

View File

@ -12,7 +12,7 @@ ReturnValue_t AckPduDeserializer::parseData() {
if (currentIdx + 2 > this->maxSize) {
return SerializeIF::BUFFER_TOO_SHORT;
}
if (not checkAndSetCodes(rawPtr[currentIdx], rawPtr[currentIdx + 1])) {
if (not checkAndSetCodes(pointers.rawPtr[currentIdx], pointers.rawPtr[currentIdx + 1])) {
return cfdp::INVALID_ACK_DIRECTIVE_FIELDS;
}
return HasReturnvaluesIF::RETURN_OK;

View File

@ -12,7 +12,7 @@ ReturnValue_t EofPduDeserializer::parseData() {
return result;
}
const uint8_t* bufPtr = rawPtr;
const uint8_t* bufPtr = pointers.rawPtr;
size_t expectedFileFieldLen = 4;
if (this->getLargeFileFlag()) {
expectedFileFieldLen = 8;

View File

@ -9,7 +9,7 @@ ReturnValue_t FileDataReader::parseData() {
return result;
}
size_t currentIdx = HeaderReader::getHeaderSize();
const uint8_t* buf = rawPtr + currentIdx;
const uint8_t* buf = pointers.rawPtr + currentIdx;
size_t remSize = HeaderReader::getWholePduSize() - currentIdx;
if (remSize < 1) {
return SerializeIF::STREAM_TOO_SHORT;

View File

@ -17,10 +17,10 @@ ReturnValue_t FileDirectiveReader::parseData() {
return SerializeIF::STREAM_TOO_SHORT;
}
size_t currentIdx = HeaderReader::getHeaderSize();
if (not checkFileDirective(rawPtr[currentIdx])) {
if (not checkFileDirective(pointers.rawPtr[currentIdx])) {
return cfdp::INVALID_DIRECTIVE_FIELDS;
}
setFileDirective(static_cast<cfdp::FileDirectives>(rawPtr[currentIdx]));
setFileDirective(static_cast<cfdp::FileDirectives>(pointers.rawPtr[currentIdx]));
return HasReturnvaluesIF::RETURN_OK;
}

View File

@ -10,7 +10,7 @@ ReturnValue_t FinishPduDeserializer::parseData() {
return result;
}
size_t currentIdx = FileDirectiveReader::getHeaderSize();
const uint8_t* buf = rawPtr + currentIdx;
const uint8_t* buf = pointers.rawPtr + currentIdx;
size_t remSize = FileDirectiveReader::getWholePduSize() - currentIdx;
if (remSize < 1) {
return SerializeIF::STREAM_TOO_SHORT;

View File

@ -4,41 +4,42 @@
#include <cstring>
HeaderReader::HeaderReader(const uint8_t *pduBuf, size_t maxSize)
: rawPtr(pduBuf), maxSize(maxSize) {}
HeaderReader::HeaderReader(const uint8_t *pduBuf, size_t maxSize) { setData(pduBuf, maxSize); }
ReturnValue_t HeaderReader::parseData() {
if (isNull()) {
return HasReturnvaluesIF::RETURN_FAILED;
}
if (maxSize < 7) {
return SerializeIF::STREAM_TOO_SHORT;
}
return setData(const_cast<uint8_t *>(rawPtr), maxSize);
}
ReturnValue_t HeaderReader::setData(uint8_t *dataPtr, size_t maxSize_, void *args) {
if (dataPtr == nullptr) {
// Allowed for now
this->fixedHeader = nullptr;
return HasReturnvaluesIF::RETURN_OK;
}
this->fixedHeader = reinterpret_cast<PduHeaderFixedStruct *>(const_cast<uint8_t *>(dataPtr));
sourceIdRaw = static_cast<uint8_t *>(&fixedHeader->variableFieldsStart);
sourceIdRaw = static_cast<uint8_t *>(&pointers.fixedHeader->variableFieldsStart);
cfdp::WidthInBytes widthEntityIds = getLenEntityIds();
cfdp::WidthInBytes widthSeqNum = getLenSeqNum();
seqNumRaw = static_cast<uint8_t *>(sourceIdRaw) + static_cast<uint8_t>(widthEntityIds);
destIdRaw = static_cast<uint8_t *>(seqNumRaw) + static_cast<uint8_t>(widthSeqNum);
return HasReturnvaluesIF::RETURN_OK;
}
ReturnValue_t HeaderReader::setData(uint8_t *dataPtr, size_t maxSize_, void *args) {
if (maxSize_ < 7) {
return SerializeIF::STREAM_TOO_SHORT;
}
pointers.rawPtr = dataPtr;
pointers.fixedHeader = reinterpret_cast<PduHeaderFixedStruct *>(const_cast<uint8_t *>(dataPtr));
maxSize = maxSize_;
return HasReturnvaluesIF::RETURN_OK;
}
size_t HeaderReader::getHeaderSize() const {
if (fixedHeader != nullptr) {
if (pointers.fixedHeader != nullptr) {
return getLenEntityIds() * 2 + getLenSeqNum() + 4;
}
return 0;
}
size_t HeaderReader::getPduDataFieldLen() const {
return (fixedHeader->pduDataFieldLenH << 8) | fixedHeader->pduDataFieldLenL;
return (pointers.fixedHeader->pduDataFieldLenH << 8) | pointers.fixedHeader->pduDataFieldLenL;
}
size_t HeaderReader::getWholePduSize() const {
@ -46,35 +47,35 @@ size_t HeaderReader::getWholePduSize() const {
}
cfdp::PduType HeaderReader::getPduType() const {
return static_cast<cfdp::PduType>((fixedHeader->firstByte >> 4) & 0x01);
return static_cast<cfdp::PduType>((pointers.fixedHeader->firstByte >> 4) & 0x01);
}
cfdp::Direction HeaderReader::getDirection() const {
return static_cast<cfdp::Direction>((fixedHeader->firstByte >> 3) & 0x01);
return static_cast<cfdp::Direction>((pointers.fixedHeader->firstByte >> 3) & 0x01);
}
cfdp::TransmissionModes HeaderReader::getTransmissionMode() const {
return static_cast<cfdp::TransmissionModes>((fixedHeader->firstByte >> 2) & 0x01);
return static_cast<cfdp::TransmissionModes>((pointers.fixedHeader->firstByte >> 2) & 0x01);
}
bool HeaderReader::getCrcFlag() const { return (fixedHeader->firstByte >> 1) & 0x01; }
bool HeaderReader::getCrcFlag() const { return (pointers.fixedHeader->firstByte >> 1) & 0x01; }
bool HeaderReader::getLargeFileFlag() const { return fixedHeader->firstByte & 0x01; }
bool HeaderReader::getLargeFileFlag() const { return pointers.fixedHeader->firstByte & 0x01; }
cfdp::SegmentationControl HeaderReader::getSegmentationControl() const {
return static_cast<cfdp::SegmentationControl>((fixedHeader->fourthByte >> 7) & 0x01);
return static_cast<cfdp::SegmentationControl>((pointers.fixedHeader->fourthByte >> 7) & 0x01);
}
cfdp::WidthInBytes HeaderReader::getLenEntityIds() const {
return static_cast<cfdp::WidthInBytes>((fixedHeader->fourthByte >> 4) & 0x07);
return static_cast<cfdp::WidthInBytes>((pointers.fixedHeader->fourthByte >> 4) & 0x07);
}
cfdp::WidthInBytes HeaderReader::getLenSeqNum() const {
return static_cast<cfdp::WidthInBytes>(fixedHeader->fourthByte & 0x07);
return static_cast<cfdp::WidthInBytes>(pointers.fixedHeader->fourthByte & 0x07);
}
cfdp::SegmentMetadataFlag HeaderReader::getSegmentMetadataFlag() const {
return static_cast<cfdp::SegmentMetadataFlag>((fixedHeader->fourthByte >> 3) & 0x01);
return static_cast<cfdp::SegmentMetadataFlag>((pointers.fixedHeader->fourthByte >> 3) & 0x01);
}
void HeaderReader::getSourceId(cfdp::EntityId &sourceId) const {
@ -129,3 +130,8 @@ bool HeaderReader::hasSegmentMetadataFlag() const {
ReturnValue_t HeaderReader::setData(const uint8_t *dataPtr, size_t maxSize_) {
return setData(const_cast<uint8_t *>(dataPtr), maxSize_, nullptr);
}
bool HeaderReader::isNull() const {
return pointers.rawPtr == nullptr or pointers.fixedHeader == nullptr;
}
HeaderReader::operator bool() const { return isNull(); }

View File

@ -42,6 +42,9 @@ class HeaderReader : public RedirectableDataPointerIF, public PduHeaderIF {
* - SerializeIF::BUFFER_TOO_SHORT if buffer is shorter than expected
*/
virtual ReturnValue_t parseData();
explicit operator bool() const;
[[nodiscard]] bool isNull() const;
[[nodiscard]] virtual size_t getHeaderSize() const;
[[nodiscard]] size_t getPduDataFieldLen() const override;
@ -77,8 +80,12 @@ class HeaderReader : public RedirectableDataPointerIF, public PduHeaderIF {
ReturnValue_t setData(const uint8_t* dataPtr, size_t maxSize);
protected:
PduHeaderFixedStruct* fixedHeader = nullptr;
const uint8_t* rawPtr = nullptr;
struct Pointers {
PduHeaderFixedStruct* fixedHeader = nullptr;
const uint8_t* rawPtr = nullptr;
};
Pointers pointers;
size_t maxSize = 0;
private:

View File

@ -11,7 +11,7 @@ ReturnValue_t KeepAlivePduDeserializer::parseData() {
}
size_t currentIdx = FileDirectiveReader::getHeaderSize();
size_t remLen = FileDirectiveReader::getWholePduSize() - currentIdx;
const uint8_t* buffer = rawPtr + currentIdx;
const uint8_t* buffer = pointers.rawPtr + currentIdx;
return progress.deSerialize(&buffer, &remLen, getEndianness());
}

View File

@ -9,7 +9,7 @@ ReturnValue_t MetadataPduReader::parseData() {
return result;
}
size_t currentIdx = FileDirectiveReader::getHeaderSize();
const uint8_t* buf = rawPtr + currentIdx;
const uint8_t* buf = pointers.rawPtr + currentIdx;
size_t remSize = FileDirectiveReader::getWholePduSize() - currentIdx;
if (remSize < 1) {
return SerializeIF::STREAM_TOO_SHORT;

View File

@ -9,7 +9,7 @@ ReturnValue_t NakPduDeserializer::parseData() {
return result;
}
size_t currentIdx = FileDirectiveReader::getHeaderSize();
const uint8_t* buffer = rawPtr + currentIdx;
const uint8_t* buffer = pointers.rawPtr + currentIdx;
size_t remSize = FileDirectiveReader::getWholePduSize() - currentIdx;
if (remSize < 1) {
return SerializeIF::STREAM_TOO_SHORT;

View File

@ -16,6 +16,6 @@ ReturnValue_t PromptPduDeserializer::parseData() {
return SerializeIF::STREAM_TOO_SHORT;
}
responseRequired = static_cast<cfdp::PromptResponseRequired>(
(rawPtr[FileDirectiveReader::getHeaderSize()] >> 7) & 0x01);
(pointers.rawPtr[FileDirectiveReader::getHeaderSize()] >> 7) & 0x01);
return HasReturnvaluesIF::RETURN_OK;
}

View File

@ -6,6 +6,13 @@ cfdp::Lv::Lv(const uint8_t* value, size_t size) : value(value, size, true) {
}
}
cfdp::Lv::Lv(const char* value, size_t size)
: value(reinterpret_cast<const uint8_t*>(value), size, true) {
if (size > 0) {
zeroLen = false;
}
}
cfdp::Lv::Lv() : value(static_cast<uint8_t*>(nullptr), 0, true) {}
cfdp::Lv::Lv(const Lv& other)

View File

@ -13,6 +13,7 @@ namespace cfdp {
class Lv : public SerializeIF {
public:
Lv(const uint8_t* value, size_t size);
Lv(const char* value, size_t size);
Lv();
// Delete copy ctor and assingment ctor for now because this class contains a reference to
@ -23,7 +24,7 @@ class Lv : public SerializeIF {
ReturnValue_t serialize(uint8_t** buffer, size_t* size, size_t maxSize,
Endianness streamEndianness) const override;
size_t getSerializedSize() const override;
[[nodiscard]] size_t getSerializedSize() const override;
/**
* @brief Deserialize a LV field from a raw buffer

View File

@ -88,7 +88,7 @@ class LocalPool : public SystemObject, public StorageManagerIF {
*/
ReturnValue_t addData(store_address_t* storeId, const uint8_t* data, size_t size,
bool ignoreFault = false) override;
ReturnValue_t getFreeElement(store_address_t* storeId, const size_t size, uint8_t** pData,
ReturnValue_t getFreeElement(store_address_t* storeId, size_t size, uint8_t** pData,
bool ignoreFault = false) override;
ConstAccessorPair getData(store_address_t storeId) override;

View File

@ -12,9 +12,24 @@ TEST_CASE("CFDP Distributor", "[cfdp][distributor]") {
auto queue = MessageQueueMock(1);
CfdpDistribCfg distribCfg(1, pool, &queue);
auto distributor = CfdpDistributor(distribCfg);
auto entityId = cfdp::EntityId(UnsignedByteField<uint16_t>(2));
auto obswEntityId = cfdp::EntityId(UnsignedByteField<uint16_t>(2));
auto groundEntityId = cfdp::EntityId(UnsignedByteField<uint16_t>(1));
MessageQueueId_t acceptorQueueId = 3;
auto tcAcceptor = AcceptsTcMock("TC Acceptor", 0, acceptorQueueId);
cfdp::FileSize fileSize(12);
const cfdp::EntityId& sourceId(groundEntityId);
const cfdp::EntityId& destId(obswEntityId);
cfdp::TransactionSeqNum seqNum(UnsignedByteField<uint16_t>(12));
auto pduConf = PduConfig(sourceId, destId, cfdp::TransmissionModes::UNACKNOWLEDGED, seqNum);
std::string sourceFileString = "hello.txt";
cfdp::Lv sourceFileName(sourceFileString.c_str(), sourceFileString.size());
std::string destFileString = "hello2.txt";
cfdp::Lv destFileName(destFileString.c_str(), sourceFileString.size());
MetadataInfo metadataInfo(false, cfdp::ChecksumType::CRC_32, fileSize, sourceFileName,
destFileName);
MetadataPduCreator creator(pduConf, metadataInfo);
uint8_t* dataPtr = nullptr;
SECTION("State") {
CHECK(distributor.initialize() == result::OK);
CHECK(std::strcmp(distributor.getName(), "CFDP Distributor") == 0);
@ -24,7 +39,14 @@ TEST_CASE("CFDP Distributor", "[cfdp][distributor]") {
SECTION("Register Listener") {
CHECK(distributor.initialize() == result::OK);
CHECK(distributor.registerTcDestination(entityId, tcAcceptor));
// queue.addReceivedMessage()
CHECK(distributor.registerTcDestination(obswEntityId, tcAcceptor) == result::OK);
size_t serLen = 0;
store_address_t storeId;
CHECK(pool.getFreeElement(&storeId, creator.getSerializedSize(), &dataPtr) == result::OK);
REQUIRE(creator.SerializeIF::serializeBe(dataPtr, serLen, creator.getSerializedSize()) ==
result::OK);
TmTcMessage msg(storeId);
queue.addReceivedMessage(msg);
CHECK(distributor.performOperation(0) == result::OK);
}
}

View File

@ -22,7 +22,7 @@ TEST_CASE("Metadata PDU", "[cfdp][pdu]") {
std::string firstFileName = "hello.txt";
cfdp::Lv sourceFileName(reinterpret_cast<const uint8_t*>(firstFileName.data()),
firstFileName.size());
cfdp::Lv destFileName(nullptr, 0);
cfdp::Lv destFileName;
FileSize fileSize(35);
MetadataInfo info(false, ChecksumType::MODULAR, fileSize, sourceFileName, destFileName);
@ -72,7 +72,7 @@ TEST_CASE("Metadata PDU", "[cfdp][pdu]") {
otherFileName.size());
info.setSourceFileName(otherFileNameLv);
size_t sizeOfOptions = options.size();
info.setOptionsArray(*options.data(), &sizeOfOptions, &sizeOfOptions);
info.setOptionsArray(options.data(), &sizeOfOptions, &sizeOfOptions);
REQUIRE(info.getMaxOptionsLen() == 2);
info.setMaxOptionsLen(3);
REQUIRE(info.getMaxOptionsLen() == 3);
@ -130,7 +130,7 @@ TEST_CASE("Metadata PDU", "[cfdp][pdu]") {
}
size_t sizeOfOptions = options.size();
size_t maxSize = 4;
info.setOptionsArray(reinterpret_cast<Tlv*>(options.data()), &sizeOfOptions, &maxSize);
info.setOptionsArray(options.data(), &sizeOfOptions, &maxSize);
REQUIRE(info.getOptionsLen() == 2);
info.setChecksumType(cfdp::ChecksumType::CRC_32C);
info.setClosureRequested(true);
@ -169,7 +169,7 @@ TEST_CASE("Metadata PDU", "[cfdp][pdu]") {
mdBuffer[2] = 36 & 0xff;
info.setOptionsArray(nullptr, nullptr, nullptr);
REQUIRE(deserializer2.parseData() == cfdp::METADATA_CANT_PARSE_OPTIONS);
info.setOptionsArray(reinterpret_cast<Tlv*>(options.data()), &sizeOfOptions, nullptr);
info.setOptionsArray(options.data(), &sizeOfOptions, nullptr);
for (size_t maxSz = 0; maxSz < 46; maxSz++) {
MetadataPduReader invalidSzDeser(mdBuffer.data(), maxSz, info);
result = invalidSzDeser.parseData();

View File

@ -158,7 +158,7 @@ TEST_CASE("CFDP TLV LV", "[cfdp]") {
SerializeIF::Endianness::BIG);
REQUIRE(sourceIdRaw == 0x0ff0);
auto lvEmpty = Lv(nullptr, 0);
auto lvEmpty = Lv();
REQUIRE(lvEmpty.getSerializedSize() == 1);
serPtr = rawBuf.data();
deserSize = 0;
@ -191,7 +191,7 @@ TEST_CASE("CFDP TLV LV", "[cfdp]") {
SerializeIF::Endianness::BIG);
REQUIRE(sourceIdRaw == 0x0ff0);
auto lvEmpty = Lv(nullptr, 0);
auto lvEmpty = Lv();
REQUIRE(lvEmpty.getSerializedSize() == 1);
serPtr = rawBuf.data();
deserSize = 0;