From cb118176a02ac536bd94173548732319fac11ae8 Mon Sep 17 00:00:00 2001 From: Robin Mueller Date: Thu, 28 Jul 2022 13:24:50 +0200 Subject: [PATCH] completed ZcWriter unittests --- src/fsfw/tmtcpacket/pus/tc/PusTcCreator.h | 6 ++++ src/fsfw/tmtcpacket/pus/tm/PusTmCreator.h | 10 +++++++ src/fsfw/tmtcpacket/pus/tm/PusTmReader.cpp | 2 +- src/fsfw/tmtcpacket/pus/tm/PusTmReader.h | 31 ++++++++++++++++++++ src/fsfw/tmtcpacket/pus/tm/PusTmZcWriter.cpp | 6 ++++ src/fsfw/tmtcpacket/pus/tm/PusTmZcWriter.h | 7 +++++ unittests/tmtcpacket/testZcTmWriter.cpp | 21 ++++++++++++- 7 files changed, 81 insertions(+), 2 deletions(-) diff --git a/src/fsfw/tmtcpacket/pus/tc/PusTcCreator.h b/src/fsfw/tmtcpacket/pus/tc/PusTcCreator.h index 63a5d45f..3a51fb68 100644 --- a/src/fsfw/tmtcpacket/pus/tc/PusTcCreator.h +++ b/src/fsfw/tmtcpacket/pus/tc/PusTcCreator.h @@ -19,6 +19,12 @@ struct PusTcParams { uint8_t pusVersion = ecss::PusVersion::PUS_C; }; +/** + * This class provides a high-level interface to create PUS TC packets and then @serialize + * them into a raw byte format. It implements @SerializeIF for that purpose. + * A custom time stamper can be set, with the implementation of @TimeStamperIF as the only + * requirement. + */ class PusTcCreator : public PusTcIF, public SerializeIF, public CustomUserDataIF { public: PusTcCreator(SpacePacketParams initSpParams, PusTcParams initPusParams); diff --git a/src/fsfw/tmtcpacket/pus/tm/PusTmCreator.h b/src/fsfw/tmtcpacket/pus/tm/PusTmCreator.h index a7905858..9f3dcd24 100644 --- a/src/fsfw/tmtcpacket/pus/tm/PusTmCreator.h +++ b/src/fsfw/tmtcpacket/pus/tm/PusTmCreator.h @@ -39,8 +39,18 @@ struct PusTmParams { class TimeStamperIF; +/** + * This class provides a high-level interface to create PUS TM packets and then @serialize + * them into a raw byte format. It implements @SerializeIF for that purpose. + * A custom time stamper can be set, with the implementation of @TimeStamperIF as the only + * requirement. + */ class PusTmCreator : public SerializeIF, public PusTmIF, public CustomUserDataIF { public: + /** + * Empty creator with all-default parameters. Please note that serializing this will + * generate an invalid PUS packet with no timestamp. + */ PusTmCreator(); PusTmCreator(SpacePacketParams initSpParams, PusTmParams initPusParams); ~PusTmCreator() override = default; diff --git a/src/fsfw/tmtcpacket/pus/tm/PusTmReader.cpp b/src/fsfw/tmtcpacket/pus/tm/PusTmReader.cpp index ca40562b..75abdbf6 100644 --- a/src/fsfw/tmtcpacket/pus/tm/PusTmReader.cpp +++ b/src/fsfw/tmtcpacket/pus/tm/PusTmReader.cpp @@ -86,6 +86,6 @@ ReturnValue_t PusTmReader::parseData(bool crcCheck) { } return HasReturnvaluesIF::RETURN_OK; } -bool PusTmReader::isNull() const { return spReader.isNull(); } +bool PusTmReader::isNull() const { return spReader.isNull() or pointers.secHeaderStart == nullptr; } PusTmReader::operator bool() const { return not isNull(); } diff --git a/src/fsfw/tmtcpacket/pus/tm/PusTmReader.h b/src/fsfw/tmtcpacket/pus/tm/PusTmReader.h index c95fefae..c12bb4c8 100644 --- a/src/fsfw/tmtcpacket/pus/tm/PusTmReader.h +++ b/src/fsfw/tmtcpacket/pus/tm/PusTmReader.h @@ -8,6 +8,18 @@ #include "fsfw/tmtcpacket/pus/RawUserDataReaderIF.h" #include "fsfw/tmtcpacket/pus/tm/PusTmIF.h" +/** + * This object can be used to read existing PUS TM packets in raw byte format. + * It is a zero-copy object, so reading a TM packet with will not copy anything. + * + * Please note that a parser function must be called after the constructor. This will also check + * the packet for validity. + * + * There are two parser function, where one does not perform the CRC check. This is useful + * if the CRC calculation will is performed in a separate step. + * This object also requires an explicit time stamp reader to allow flexibility in the used + * timestamp. + */ class PusTmReader : public PusTmIF, public RawUserDataReaderIF, public ReadablePacketIF, @@ -17,11 +29,30 @@ class PusTmReader : public PusTmIF, PusTmReader(const uint8_t* data, size_t size); PusTmReader(TimeReaderIF* timeReader, const uint8_t* data, size_t size); + /** + * No CRC check will be performed + * @return + */ ReturnValue_t parseDataWithoutCrcCheck(); + /** + * Performs a CRC check on the data as well + * @return + * - HasReturnvaluesIF::RETURN_OK: Successfully parsed the packet + * - SerializeIF::STREAM_TOO_SHORT: Stream too short for detected packet size + * - PusIF::INVALID_CRC_16 on invalid CRC + */ ReturnValue_t parseDataWithCrcCheck(); [[nodiscard]] const uint8_t* getFullData() const override; + /** + * Returns @isNull + * @return + */ explicit operator bool() const; + /** + * No (valid) data was set yet or the parse function was not called yet. + * @return + */ [[nodiscard]] bool isNull() const; void setTimeReader(TimeReaderIF* timeReader); TimeReaderIF* getTimeReader(); diff --git a/src/fsfw/tmtcpacket/pus/tm/PusTmZcWriter.cpp b/src/fsfw/tmtcpacket/pus/tm/PusTmZcWriter.cpp index 6ca78378..6a9f6235 100644 --- a/src/fsfw/tmtcpacket/pus/tm/PusTmZcWriter.cpp +++ b/src/fsfw/tmtcpacket/pus/tm/PusTmZcWriter.cpp @@ -6,12 +6,18 @@ PusTmZeroCopyWriter::PusTmZeroCopyWriter(TimeReaderIF& timeReader, uint8_t* data : PusTmReader(&timeReader, data, size) {} void PusTmZeroCopyWriter::setSequenceCount(uint16_t seqCount) { + if (isNull()) { + return; + } auto* spHeader = reinterpret_cast(const_cast((pointers.spHeaderStart))); ccsds::setSequenceCount(*spHeader, seqCount); } void PusTmZeroCopyWriter::updateErrorControl() { + if (isNull()) { + return; + } auto* crcStart = const_cast((pointers.crcStart)); uint16_t crc16 = CRC::crc16ccitt(PusTmReader::getFullData(), getFullPacketLen() - sizeof(ecss::PusChecksumT)); diff --git a/src/fsfw/tmtcpacket/pus/tm/PusTmZcWriter.h b/src/fsfw/tmtcpacket/pus/tm/PusTmZcWriter.h index 331bda7f..36c43f51 100644 --- a/src/fsfw/tmtcpacket/pus/tm/PusTmZcWriter.h +++ b/src/fsfw/tmtcpacket/pus/tm/PusTmZcWriter.h @@ -2,6 +2,13 @@ #define FSFW_EXAMPLE_HOSTED_PUSTMZCWRITER_H #include "PusTmReader.h" + +/** + * This packet allows to update specific fields of a PUS TM packet where it is useful or necessary + * to update them in a second step. Otherwise, it offers the same interface as @PusTmReader. + * + * Right now, this class supports updating the CCSDS Sequence Count and the Error Control. + */ class PusTmZeroCopyWriter : public PusTmReader { public: PusTmZeroCopyWriter(TimeReaderIF& timeReader, uint8_t* data, size_t size); diff --git a/unittests/tmtcpacket/testZcTmWriter.cpp b/unittests/tmtcpacket/testZcTmWriter.cpp index c26b0069..fc013590 100644 --- a/unittests/tmtcpacket/testZcTmWriter.cpp +++ b/unittests/tmtcpacket/testZcTmWriter.cpp @@ -1,5 +1,6 @@ #include +#include "fsfw/globalfunctions/CRC.h" #include "fsfw/tmtcpacket/pus/tm.h" #include "fsfw/tmtcpacket/pus/tm/PusTmZcWriter.h" #include "mocks/CdsShortTimestamperMock.h" @@ -16,8 +17,26 @@ TEST_CASE("TM ZC Helper", "[tm-zc-helper]") { uint8_t* dataPtr = buf.data(); size_t serLen = 0; + SECTION("No Crash For Uninitialized Object") { + REQUIRE(creator.serialize(dataPtr, serLen, buf.size()) == result::OK); + PusTmZeroCopyWriter writer(timeStamper, dataPtr, serLen); + REQUIRE(writer.getSequenceCount() == 22); + writer.setSequenceCount(23); + // Can't set anything, parse function was not called + REQUIRE(writer.getSequenceCount() == 22); + writer.updateErrorControl(); + } + SECTION("Basic") { REQUIRE(creator.serialize(dataPtr, serLen, buf.size()) == result::OK); - PusTmZeroCopyWriter(timeStamper, dataPtr, serLen); + PusTmZeroCopyWriter writer(timeStamper, dataPtr, serLen); + REQUIRE(writer.parseDataWithoutCrcCheck() == result::OK); + REQUIRE(writer.getSequenceCount() == 22); + writer.setSequenceCount(23); + REQUIRE(writer.getSequenceCount() == 23); + // CRC is invalid now + REQUIRE(CRC::crc16ccitt(dataPtr, serLen) != 0); + writer.updateErrorControl(); + REQUIRE(CRC::crc16ccitt(dataPtr, serLen) == 0); } } \ No newline at end of file