From e00e198ff150f089d9f00c2be8f963507ddd685f Mon Sep 17 00:00:00 2001 From: Robin Mueller Date: Fri, 21 Jul 2023 16:09:52 +0200 Subject: [PATCH] this seems to work --- src/fsfw/cfdp/handler/CMakeLists.txt | 6 +- src/fsfw/cfdp/handler/DestHandler.cpp | 9 ++- src/fsfw/cfdp/handler/MetadataParser.cpp | 1 + src/fsfw/cfdp/handler/MetadataParser.h | 12 ++++ src/fsfw/cfdp/handler/PutRequestReader.cpp | 1 + src/fsfw/cfdp/handler/PutRequestReader.h | 29 +++++++++ src/fsfw/cfdp/handler/SourceHandler.cpp | 2 +- src/fsfw/cfdp/handler/defs.h | 1 + src/fsfw/cfdp/pdu/MetadataInfo.cpp | 53 ---------------- src/fsfw/cfdp/pdu/MetadataInfo.h | 14 ----- src/fsfw/cfdp/pdu/MetadataPduCreator.cpp | 25 +++++--- src/fsfw/cfdp/pdu/MetadataPduCreator.h | 5 +- src/fsfw/cfdp/pdu/MetadataPduReader.cpp | 23 +++---- src/fsfw/cfdp/pdu/MetadataPduReader.h | 7 ++- unittests/cfdp/handler/testDestHandler.cpp | 2 +- unittests/cfdp/handler/testDistributor.cpp | 2 +- unittests/cfdp/pdu/testMetadataPdu.cpp | 70 ++++++++++++---------- 17 files changed, 134 insertions(+), 128 deletions(-) create mode 100644 src/fsfw/cfdp/handler/MetadataParser.cpp create mode 100644 src/fsfw/cfdp/handler/MetadataParser.h create mode 100644 src/fsfw/cfdp/handler/PutRequestReader.cpp create mode 100644 src/fsfw/cfdp/handler/PutRequestReader.h diff --git a/src/fsfw/cfdp/handler/CMakeLists.txt b/src/fsfw/cfdp/handler/CMakeLists.txt index 7ad995c0..540bceca 100644 --- a/src/fsfw/cfdp/handler/CMakeLists.txt +++ b/src/fsfw/cfdp/handler/CMakeLists.txt @@ -1,2 +1,4 @@ -target_sources(${LIB_FSFW_NAME} PRIVATE SourceHandler.cpp DestHandler.cpp - FaultHandlerBase.cpp UserBase.cpp) +target_sources( + ${LIB_FSFW_NAME} + PRIVATE SourceHandler.cpp DestHandler.cpp MetadataParser.cpp + PutRequestReader.cpp FaultHandlerBase.cpp UserBase.cpp) diff --git a/src/fsfw/cfdp/handler/DestHandler.cpp b/src/fsfw/cfdp/handler/DestHandler.cpp index f44f5d41..03a360b7 100644 --- a/src/fsfw/cfdp/handler/DestHandler.cpp +++ b/src/fsfw/cfdp/handler/DestHandler.cpp @@ -136,10 +136,8 @@ ReturnValue_t cfdp::DestHandler::handleMetadataPdu(const PacketInfo& info) { cfdp::StringLv sourceFileName; cfdp::StringLv destFileName; MetadataInfo metadataInfo(transactionParams.fileSize, sourceFileName, destFileName); - cfdp::Tlv* tlvArrayAsPtr = tlvVec.data(); - metadataInfo.setOptionsArray(&tlvArrayAsPtr, std::nullopt, tlvVec.size()); MetadataPduReader reader(constAccessorPair.second.data(), constAccessorPair.second.size(), - metadataInfo); + metadataInfo, nullptr, 0); ReturnValue_t result = reader.parseData(); // TODO: The standard does not really specify what happens if this kind of error happens // I think it might be a good idea to cache some sort of error code, which @@ -357,8 +355,9 @@ ReturnValue_t cfdp::DestHandler::startTransaction(MetadataPduReader& reader, Met params.fileSize = transactionParams.fileSize.getSize(); params.destFileName = transactionParams.destName.data(); params.sourceFileName = transactionParams.sourceName.data(); - params.msgsToUserArray = dynamic_cast(userTlvVec.data()); - params.msgsToUserLen = info.getOptionsLen(); + // TODO: Is this really correct? Implement this correctly.. + // params.msgsToUserArray = dynamic_cast(userTlvVec.data()); + // params.msgsToUserLen = reader.getNumberOfParsedOptions(); destParams.user.metadataRecvdIndication(params); return result; } diff --git a/src/fsfw/cfdp/handler/MetadataParser.cpp b/src/fsfw/cfdp/handler/MetadataParser.cpp new file mode 100644 index 00000000..63510efc --- /dev/null +++ b/src/fsfw/cfdp/handler/MetadataParser.cpp @@ -0,0 +1 @@ +#include "MetadataParser.h" diff --git a/src/fsfw/cfdp/handler/MetadataParser.h b/src/fsfw/cfdp/handler/MetadataParser.h new file mode 100644 index 00000000..914c34c7 --- /dev/null +++ b/src/fsfw/cfdp/handler/MetadataParser.h @@ -0,0 +1,12 @@ +#pragma once + +namespace cfdp { + +class MetadataParser { + public: + MetadataParser(); + + private: +}; + +} // namespace cfdp \ No newline at end of file diff --git a/src/fsfw/cfdp/handler/PutRequestReader.cpp b/src/fsfw/cfdp/handler/PutRequestReader.cpp new file mode 100644 index 00000000..0b262fc9 --- /dev/null +++ b/src/fsfw/cfdp/handler/PutRequestReader.cpp @@ -0,0 +1 @@ +#include "PutRequestReader.h" diff --git a/src/fsfw/cfdp/handler/PutRequestReader.h b/src/fsfw/cfdp/handler/PutRequestReader.h new file mode 100644 index 00000000..bcef8222 --- /dev/null +++ b/src/fsfw/cfdp/handler/PutRequestReader.h @@ -0,0 +1,29 @@ +#pragma once + +#include +#include + +#include "fsfw/cfdp/VarLenFields.h" +#include "fsfw/cfdp/tlv/FilestoreRequestTlv.h" +#include "fsfw/cfdp/tlv/MessageToUserTlv.h" + +namespace cfdp { + +class PutRequest { + public: + EntityId destId; + std::optional transmissionMode; + char destName[524]{}; + std::optional destNameSize; + char sourceName[524]{}; + std::optional sourceNameSize; + std::optional closureRequested; + std::vector messagesToUser; + std::vector fsRequest; + + [[nodiscard]] bool isMetadataOnly() const { + return !destNameSize.has_value() and !sourceNameSize.has_value(); + } +}; + +} // namespace cfdp diff --git a/src/fsfw/cfdp/handler/SourceHandler.cpp b/src/fsfw/cfdp/handler/SourceHandler.cpp index 74333a69..64fa6583 100644 --- a/src/fsfw/cfdp/handler/SourceHandler.cpp +++ b/src/fsfw/cfdp/handler/SourceHandler.cpp @@ -157,7 +157,7 @@ ReturnValue_t cfdp::SourceHandler::prepareAndSendMetadataPdu() { cfdp::StringLv sourceName(transactionParams.sourceName.data(), transactionParams.sourceNameSize); cfdp::StringLv destName(transactionParams.destName.data(), transactionParams.destNameSize); auto metadataInfo = MetadataInfo(transactionParams.fileSize, sourceName, destName); - auto metadataPdu = MetadataPduCreator(transactionParams.pduConf, metadataInfo); + auto metadataPdu = MetadataPduCreator(transactionParams.pduConf, metadataInfo, nullptr, 0); ReturnValue_t result = sendGenericPdu(metadataPdu); if (result != OK) { return result; diff --git a/src/fsfw/cfdp/handler/defs.h b/src/fsfw/cfdp/handler/defs.h index 81a8895c..97ef009c 100644 --- a/src/fsfw/cfdp/handler/defs.h +++ b/src/fsfw/cfdp/handler/defs.h @@ -57,6 +57,7 @@ struct PutRequestFull { char sourceName[524]{}; size_t sourceNameSize = 0; bool closureRequested = true; + // MessageToUserTlv }; enum class CallStatus { DONE, CALL_AFTER_DELAY, CALL_AGAIN }; diff --git a/src/fsfw/cfdp/pdu/MetadataInfo.cpp b/src/fsfw/cfdp/pdu/MetadataInfo.cpp index 180549e5..3ec38f41 100644 --- a/src/fsfw/cfdp/pdu/MetadataInfo.cpp +++ b/src/fsfw/cfdp/pdu/MetadataInfo.cpp @@ -12,17 +12,6 @@ MetadataInfo::MetadataInfo(cfdp::Fss& fileSize, cfdp::StringLv& sourceFileName, cfdp::StringLv& destFileName) : fileSize(fileSize), sourceFileName(sourceFileName), destFileName(destFileName) {} -void MetadataInfo::setOptionsArray(cfdp::Tlv** optionsArray_, std::optional optionsLen_, - std::optional maxOptionsLen_) { - this->optionsArray = optionsArray_; - if (maxOptionsLen_) { - this->maxOptionsLen = maxOptionsLen_.value(); - } - if (optionsLen_) { - this->optionsLen = optionsLen_.value(); - } -} - cfdp::ChecksumType MetadataInfo::getChecksumType() const { return checksumType; } void MetadataInfo::setChecksumType(cfdp::ChecksumType checksumType_) { @@ -39,35 +28,6 @@ cfdp::StringLv& MetadataInfo::getDestFileName() { return destFileName; } cfdp::Fss& MetadataInfo::getFileSize() { return fileSize; } -ReturnValue_t MetadataInfo::getOptions(cfdp::Tlv*** optionsArray_, size_t* optionsLen_, - size_t* maxOptsLen) { - if (optionsArray_ == nullptr or optionsArray == nullptr) { - return returnvalue::FAILED; - } - *optionsArray_ = optionsArray; - if (optionsLen_ != nullptr) { - *optionsLen_ = this->optionsLen; - } - if (maxOptsLen != nullptr) { - *maxOptsLen = this->maxOptionsLen; - } - return returnvalue::OK; -} - -bool MetadataInfo::hasOptions() const { - if (optionsArray != nullptr and optionsLen > 0) { - return true; - } - return false; -} - -bool MetadataInfo::canHoldOptions() const { - if (optionsArray != nullptr and maxOptionsLen > 0) { - return true; - } - return false; -} - size_t MetadataInfo::getSerializedSize(bool fssLarge) { // 1 byte + minimal FSS 4 bytes size_t size = 5; @@ -76,11 +36,6 @@ size_t MetadataInfo::getSerializedSize(bool fssLarge) { } size += sourceFileName.getSerializedSize(); size += destFileName.getSerializedSize(); - if (hasOptions()) { - for (size_t idx = 0; idx < optionsLen; idx++) { - size += optionsArray[idx]->getSerializedSize(); - } - } return size; } @@ -92,12 +47,4 @@ void MetadataInfo::setSourceFileName(cfdp::StringLv& sourceFileName_) { this->sourceFileName = sourceFileName_; } -size_t MetadataInfo::getMaxOptionsLen() const { return maxOptionsLen; } - -void MetadataInfo::setMaxOptionsLen(size_t maxOptionsLen_) { this->maxOptionsLen = maxOptionsLen_; } - -size_t MetadataInfo::getOptionsLen() const { return optionsLen; } - -void MetadataInfo::setOptionsLen(size_t optionsLen_) { this->optionsLen = optionsLen_; } - cfdp::StringLv& MetadataInfo::getSourceFileName() { return sourceFileName; } diff --git a/src/fsfw/cfdp/pdu/MetadataInfo.h b/src/fsfw/cfdp/pdu/MetadataInfo.h index 7ae269a9..eef1447e 100644 --- a/src/fsfw/cfdp/pdu/MetadataInfo.h +++ b/src/fsfw/cfdp/pdu/MetadataInfo.h @@ -17,8 +17,6 @@ class MetadataInfo { size_t getSerializedSize(bool fssLarge = false); - void setOptionsArray(cfdp::Tlv** optionsArray, std::optional optionsLen, - std::optional maxOptionsLen); [[nodiscard]] cfdp::ChecksumType getChecksumType() const; void setChecksumType(cfdp::ChecksumType checksumType); [[nodiscard]] bool isClosureRequested() const; @@ -31,24 +29,12 @@ class MetadataInfo { cfdp::StringLv& getSourceFileName(); cfdp::Fss& getFileSize(); - [[nodiscard]] bool hasOptions() const; - [[nodiscard]] bool canHoldOptions() const; - ReturnValue_t getOptions(cfdp::Tlv*** optionsArray, size_t* optionsLen, size_t* maxOptsLen); - void setOptionsLen(size_t optionsLen); - [[nodiscard]] size_t getOptionsLen() const; - void setMaxOptionsLen(size_t maxOptionsLen); - [[nodiscard]] size_t getMaxOptionsLen() const; - private: bool closureRequested = false; cfdp::ChecksumType checksumType = cfdp::ChecksumType::NULL_CHECKSUM; cfdp::Fss& fileSize; cfdp::StringLv& sourceFileName; cfdp::StringLv& destFileName; - - cfdp::Tlv** optionsArray = nullptr; - size_t optionsLen = 0; - size_t maxOptionsLen = 0; }; #endif /* FSFW_SRC_FSFW_CFDP_PDU_METADATAINFO_H_ */ diff --git a/src/fsfw/cfdp/pdu/MetadataPduCreator.cpp b/src/fsfw/cfdp/pdu/MetadataPduCreator.cpp index 536a48f4..e31e7903 100644 --- a/src/fsfw/cfdp/pdu/MetadataPduCreator.cpp +++ b/src/fsfw/cfdp/pdu/MetadataPduCreator.cpp @@ -1,12 +1,22 @@ #include "MetadataPduCreator.h" -MetadataPduCreator::MetadataPduCreator(PduConfig &conf, MetadataInfo &info) - : FileDirectiveCreator(conf, cfdp::FileDirective::METADATA, 5), info(info) { +MetadataPduCreator::MetadataPduCreator(PduConfig &conf, MetadataInfo &info, + cfdp::Tlv **optionsArray, size_t optionsLen) + : FileDirectiveCreator(conf, cfdp::FileDirective::METADATA, 5), + info(info), + optionsArray(optionsArray), + optionsLen(optionsLen) { updateDirectiveFieldLen(); } void MetadataPduCreator::updateDirectiveFieldLen() { - setDirectiveDataFieldLen(info.getSerializedSize(getLargeFileFlag())); + size_t dirFieldLen = info.getSerializedSize(HeaderCreator::getLargeFileFlag()); + if (optionsLen > 0 and optionsArray != nullptr) { + for (size_t idx = 0; idx < optionsLen; idx++) { + dirFieldLen += optionsArray[idx]->getSerializedSize(); + } + } + setDirectiveDataFieldLen(dirFieldLen); } size_t MetadataPduCreator::getSerializedSize() const { @@ -38,12 +48,9 @@ ReturnValue_t MetadataPduCreator::serialize(uint8_t **buffer, size_t *size, size return result; } - if (info.hasOptions()) { - cfdp::Tlv **optsArray = nullptr; - size_t optsLen = 0; - info.getOptions(&optsArray, &optsLen, nullptr); - for (size_t idx = 0; idx < optsLen; idx++) { - result = optsArray[idx]->serialize(buffer, size, maxSize, streamEndianness); + if (optionsLen > 0 and optionsArray != nullptr) { + for (size_t idx = 0; idx < optionsLen; idx++) { + result = optionsArray[idx]->serialize(buffer, size, maxSize, streamEndianness); if (result != returnvalue::OK) { return result; } diff --git a/src/fsfw/cfdp/pdu/MetadataPduCreator.h b/src/fsfw/cfdp/pdu/MetadataPduCreator.h index 4486a79c..a6f8331c 100644 --- a/src/fsfw/cfdp/pdu/MetadataPduCreator.h +++ b/src/fsfw/cfdp/pdu/MetadataPduCreator.h @@ -6,7 +6,8 @@ class MetadataPduCreator : public FileDirectiveCreator { public: - MetadataPduCreator(PduConfig& conf, MetadataInfo& info); + MetadataPduCreator(PduConfig& conf, MetadataInfo& info, cfdp::Tlv** optionsArray, + size_t optionsLen); void updateDirectiveFieldLen(); @@ -18,6 +19,8 @@ class MetadataPduCreator : public FileDirectiveCreator { private: MetadataInfo& info; + cfdp::Tlv** optionsArray; + size_t optionsLen; }; #endif /* FSFW_CFDP_PDU_METADATAPDUCREATOR_H_ */ diff --git a/src/fsfw/cfdp/pdu/MetadataPduReader.cpp b/src/fsfw/cfdp/pdu/MetadataPduReader.cpp index 66025140..104ef2ae 100644 --- a/src/fsfw/cfdp/pdu/MetadataPduReader.cpp +++ b/src/fsfw/cfdp/pdu/MetadataPduReader.cpp @@ -1,9 +1,14 @@ #include "MetadataPduReader.h" -MetadataPduReader::MetadataPduReader(const uint8_t* pduBuf, size_t maxSize, MetadataInfo& info) - : FileDirectiveReader(pduBuf, maxSize), info(info) {} +MetadataPduReader::MetadataPduReader(const uint8_t* pduBuf, size_t maxSize, MetadataInfo& info, + cfdp::Tlv* optionsArray, size_t optArrayMaxSize) + : FileDirectiveReader(pduBuf, maxSize), + info(info), + optionArray(optionsArray), + optionArrayMaxSize(optArrayMaxSize) {} ReturnValue_t MetadataPduReader::parseData() { + parsedOptions = 0; ReturnValue_t result = FileDirectiveReader::parseData(); if (result != returnvalue::OK) { return result; @@ -32,26 +37,24 @@ ReturnValue_t MetadataPduReader::parseData() { return result; } - info.setOptionsLen(0); if (remSize > 0) { - if (not info.canHoldOptions()) { + if (optionArrayMaxSize == 0 or optionArray == nullptr) { return cfdp::METADATA_CANT_PARSE_OPTIONS; } - cfdp::Tlv** optionsArray = nullptr; - size_t optsMaxLen = 0; size_t optsIdx = 0; - info.getOptions(&optionsArray, nullptr, &optsMaxLen); while (remSize > 0) { - if (optsIdx > optsMaxLen) { + if (optsIdx > optionArrayMaxSize) { return cfdp::METADATA_CANT_PARSE_OPTIONS; } - result = optionsArray[optsIdx]->deSerialize(&buf, &remSize, endianness); + result = optionArray[optsIdx].deSerialize(&buf, &remSize, endianness); if (result != returnvalue::OK) { return result; } optsIdx++; } - info.setOptionsLen(optsIdx); + parsedOptions = optsIdx; } return result; } + +size_t MetadataPduReader::getNumberOfParsedOptions() const { return parsedOptions; } diff --git a/src/fsfw/cfdp/pdu/MetadataPduReader.h b/src/fsfw/cfdp/pdu/MetadataPduReader.h index 3e8c0f30..ff5f52a8 100644 --- a/src/fsfw/cfdp/pdu/MetadataPduReader.h +++ b/src/fsfw/cfdp/pdu/MetadataPduReader.h @@ -6,12 +6,17 @@ class MetadataPduReader : public FileDirectiveReader { public: - MetadataPduReader(const uint8_t* pduBuf, size_t maxSize, MetadataInfo& info); + MetadataPduReader(const uint8_t* pduBuf, size_t maxSize, MetadataInfo& info, + cfdp::Tlv* optionsArray, size_t optArrayMaxSize); ReturnValue_t parseData() override; + [[nodiscard]] size_t getNumberOfParsedOptions() const; private: MetadataInfo& info; + cfdp::Tlv* optionArray; + size_t optionArrayMaxSize; + size_t parsedOptions = 0; }; #endif /* FSFW_CFDP_PDU_METADATAPDUREADER_H_ */ diff --git a/unittests/cfdp/handler/testDestHandler.cpp b/unittests/cfdp/handler/testDestHandler.cpp index 792d919c..752cc4f2 100644 --- a/unittests/cfdp/handler/testDestHandler.cpp +++ b/unittests/cfdp/handler/testDestHandler.cpp @@ -61,7 +61,7 @@ TEST_CASE("CFDP Dest Handler", "[cfdp]") { conf.destId = localId; conf.mode = TransmissionMode::UNACKNOWLEDGED; conf.seqNum = seqNum; - MetadataPduCreator metadataCreator(conf, info); + MetadataPduCreator metadataCreator(conf, info, nullptr, 0); REQUIRE(tcStore.getFreeElement(&storeId, metadataCreator.getSerializedSize(), &buf) == OK); REQUIRE(metadataCreator.serialize(buf, serLen, metadataCreator.getSerializedSize()) == OK); PacketInfo packetInfo(metadataCreator.getPduType(), storeId, diff --git a/unittests/cfdp/handler/testDistributor.cpp b/unittests/cfdp/handler/testDistributor.cpp index 04c035a7..7d38e5c5 100644 --- a/unittests/cfdp/handler/testDistributor.cpp +++ b/unittests/cfdp/handler/testDistributor.cpp @@ -32,7 +32,7 @@ TEST_CASE("CFDP Distributor", "[cfdp][distributor]") { cfdp::StringLv destFileName(destFileString); MetadataInfo metadataInfo(false, cfdp::ChecksumType::CRC_32, fileSize, sourceFileName, destFileName); - MetadataPduCreator creator(pduConf, metadataInfo); + MetadataPduCreator creator(pduConf, metadataInfo, nullptr, 0); uint8_t* dataPtr = nullptr; SECTION("State") { diff --git a/unittests/cfdp/pdu/testMetadataPdu.cpp b/unittests/cfdp/pdu/testMetadataPdu.cpp index 747a2dc3..ba2fb4da 100644 --- a/unittests/cfdp/pdu/testMetadataPdu.cpp +++ b/unittests/cfdp/pdu/testMetadataPdu.cpp @@ -6,11 +6,10 @@ #include "fsfw/cfdp/pdu/MetadataPduReader.h" #include "fsfw/cfdp/tlv/FilestoreResponseTlv.h" #include "fsfw/cfdp/tlv/MessageToUserTlv.h" -#include "fsfw/globalfunctions/arrayprinter.h" TEST_CASE("Metadata PDU", "[cfdp][pdu]") { using namespace cfdp; - ReturnValue_t result = returnvalue::OK; + ReturnValue_t result; std::array mdBuffer = {}; uint8_t* buffer = mdBuffer.data(); size_t sz = 0; @@ -30,15 +29,15 @@ TEST_CASE("Metadata PDU", "[cfdp][pdu]") { std::array msg = {0x41, 0x42, 0x43}; cfdp::Tlv responseTlv; std::array responseBuf = {}; - uint8_t* responseBufPtr = responseBuf.data(); response.convertToTlv(responseTlv, buffer, responseBuf.size(), SerializeIF::Endianness::MACHINE); MessageToUserTlv msgToUser(msg.data(), msg.size()); std::array options{&responseTlv, &msgToUser}; + std::array tlvDeser{}; REQUIRE(options[0]->getSerializedSize() == 2 + 1 + 10 + 1); REQUIRE(options[1]->getSerializedSize() == 5); SECTION("Serialize") { - MetadataPduCreator serializer(pduConf, info); + MetadataPduCreator serializer(pduConf, info, nullptr, 0); result = serializer.serialize(&buffer, &sz, mdBuffer.size(), SerializeIF::Endianness::NETWORK); REQUIRE(result == returnvalue::OK); REQUIRE(serializer.getWholePduSize() == 27); @@ -65,19 +64,15 @@ TEST_CASE("Metadata PDU", "[cfdp][pdu]") { REQUIRE(mdBuffer[24] == 'x'); REQUIRE(mdBuffer[25] == 't'); REQUIRE(mdBuffer[26] == 0); + } + SECTION("Serialize with 2 options") { std::string otherFileName = "hello2.txt"; cfdp::StringLv otherFileNameLv(otherFileName.data(), otherFileName.size()); info.setSourceFileName(otherFileNameLv); - size_t sizeOfOptions = options.size(); - info.setOptionsArray(options.data(), sizeOfOptions, sizeOfOptions); - REQUIRE(info.getMaxOptionsLen() == 2); - info.setMaxOptionsLen(3); - REQUIRE(info.getMaxOptionsLen() == 3); + MetadataPduCreator serializer(pduConf, info, options.data(), options.size()); info.setChecksumType(cfdp::ChecksumType::CRC_32C); info.setClosureRequested(true); - uint8_t* buffer = mdBuffer.data(); - size_t sz = 0; serializer.updateDirectiveFieldLen(); result = serializer.serialize(&buffer, &sz, mdBuffer.size(), SerializeIF::Endianness::NETWORK); @@ -98,14 +93,14 @@ TEST_CASE("Metadata PDU", "[cfdp][pdu]") { // TLV unittests REQUIRE(sz == 10 + 37); for (size_t maxSz = 0; maxSz < sz; maxSz++) { - uint8_t* buffer = mdBuffer.data(); - size_t sz = 0; + buffer = mdBuffer.data(); + sz = 0; result = serializer.serialize(&buffer, &sz, maxSz, SerializeIF::Endianness::NETWORK); REQUIRE(result == SerializeIF::BUFFER_TOO_SHORT); } for (size_t initSz = 1; initSz < 47; initSz++) { - uint8_t* buffer = mdBuffer.data(); - size_t sz = initSz; + buffer = mdBuffer.data(); + sz = initSz; result = serializer.serialize(&buffer, &sz, 46, SerializeIF::Endianness::NETWORK); REQUIRE(result == SerializeIF::BUFFER_TOO_SHORT); } @@ -113,34 +108,33 @@ TEST_CASE("Metadata PDU", "[cfdp][pdu]") { } SECTION("Deserialize") { - MetadataPduCreator serializer(pduConf, info); + MetadataPduCreator serializer(pduConf, info, nullptr, 0); result = serializer.serialize(&buffer, &sz, mdBuffer.size(), SerializeIF::Endianness::NETWORK); REQUIRE(result == returnvalue::OK); - MetadataPduReader deserializer(mdBuffer.data(), mdBuffer.size(), info); + MetadataPduReader deserializer(mdBuffer.data(), mdBuffer.size(), info, nullptr, 0); result = deserializer.parseData(); REQUIRE(result == returnvalue::OK); size_t fullSize = deserializer.getWholePduSize(); for (size_t maxSz = 0; maxSz < fullSize; maxSz++) { - MetadataPduReader invalidSzDeser(mdBuffer.data(), maxSz, info); + MetadataPduReader invalidSzDeser(mdBuffer.data(), maxSz, info, nullptr, 0); result = invalidSzDeser.parseData(); REQUIRE(result != returnvalue::OK); } - size_t sizeOfOptions = options.size(); - size_t maxSize = 4; - info.setOptionsArray(options.data(), sizeOfOptions, maxSize); - REQUIRE(info.getOptionsLen() == 2); + } + + SECTION("Deserialize with 2 options") { + MetadataPduCreator serializer(pduConf, info, options.data(), options.size()); info.setChecksumType(cfdp::ChecksumType::CRC_32C); info.setClosureRequested(true); - uint8_t* buffer = mdBuffer.data(); - size_t sz = 0; serializer.updateDirectiveFieldLen(); info.setSourceFileName(sourceFileName); result = serializer.serialize(&buffer, &sz, mdBuffer.size(), SerializeIF::Endianness::NETWORK); REQUIRE(result == returnvalue::OK); - MetadataPduReader deserializer2(mdBuffer.data(), mdBuffer.size(), info); + MetadataPduReader deserializer2(mdBuffer.data(), mdBuffer.size(), info, tlvDeser.data(), + tlvDeser.max_size()); result = deserializer2.parseData(); REQUIRE(result == returnvalue::OK); REQUIRE(options[0]->getType() == cfdp::TlvType::FILESTORE_RESPONSE); @@ -151,12 +145,15 @@ TEST_CASE("Metadata PDU", "[cfdp][pdu]") { for (size_t invalidFieldLen = 0; invalidFieldLen < 36; invalidFieldLen++) { mdBuffer[1] = (invalidFieldLen >> 8) & 0xff; mdBuffer[2] = invalidFieldLen & 0xff; + if (invalidFieldLen == 17) { + volatile uint32_t dummy = 0; + } result = deserializer2.parseData(); if (invalidFieldLen == 17) { - REQUIRE(info.getOptionsLen() == 0); + REQUIRE(deserializer2.getNumberOfParsedOptions() == 0); } if (invalidFieldLen == 31) { - REQUIRE(info.getOptionsLen() == 1); + REQUIRE(deserializer2.getNumberOfParsedOptions() == 1); } // This is the precise length where there are no options or one option if (invalidFieldLen != 17 and invalidFieldLen != 31) { @@ -165,11 +162,24 @@ TEST_CASE("Metadata PDU", "[cfdp][pdu]") { } mdBuffer[1] = (36 >> 8) & 0xff; mdBuffer[2] = 36 & 0xff; - info.setOptionsArray(nullptr, std::nullopt, std::nullopt); + } + + SECTION("Can not parse options") { + MetadataPduCreator serializer(pduConf, info, options.data(), options.size()); + info.setChecksumType(cfdp::ChecksumType::CRC_32C); + info.setClosureRequested(true); + buffer = mdBuffer.data(); + sz = 0; + serializer.updateDirectiveFieldLen(); + + info.setSourceFileName(sourceFileName); + result = serializer.serialize(&buffer, &sz, mdBuffer.size(), SerializeIF::Endianness::NETWORK); + REQUIRE(result == returnvalue::OK); + + MetadataPduReader deserializer2(mdBuffer.data(), mdBuffer.size(), info, nullptr, 0); REQUIRE(deserializer2.parseData() == cfdp::METADATA_CANT_PARSE_OPTIONS); - info.setOptionsArray(options.data(), sizeOfOptions, std::nullopt); for (size_t maxSz = 0; maxSz < 46; maxSz++) { - MetadataPduReader invalidSzDeser(mdBuffer.data(), maxSz, info); + MetadataPduReader invalidSzDeser(mdBuffer.data(), maxSz, info, nullptr, 0); if (not invalidSzDeser.isNull()) { result = invalidSzDeser.parseData(); REQUIRE(result == SerializeIF::STREAM_TOO_SHORT);