From 08e0b0f1a0eafcc0c5facb26631f8b3cff6ddeb9 Mon Sep 17 00:00:00 2001 From: Robin Mueller Date: Thu, 21 Jul 2022 14:40:00 +0200 Subject: [PATCH] added sp reader unittests --- src/fsfw/tmtcpacket/ccsds/SpacePacketIF.h | 6 +++ .../tmtcpacket/ccsds/SpacePacketReader.cpp | 7 ++- src/fsfw/tmtcpacket/ccsds/SpacePacketReader.h | 5 -- src/fsfw/tmtcpacket/ccsds/header.cpp | 2 +- unittests/tmtcpacket/testCcsdsReader.cpp | 51 ++++++++++++++++++- 5 files changed, 63 insertions(+), 8 deletions(-) diff --git a/src/fsfw/tmtcpacket/ccsds/SpacePacketIF.h b/src/fsfw/tmtcpacket/ccsds/SpacePacketIF.h index 068e04d9..de01e819 100644 --- a/src/fsfw/tmtcpacket/ccsds/SpacePacketIF.h +++ b/src/fsfw/tmtcpacket/ccsds/SpacePacketIF.h @@ -22,6 +22,12 @@ class SpacePacketIF { virtual ~SpacePacketIF() = default; + /** + * CCSDS header always has 6 bytes + * @return + */ + static constexpr size_t getHeaderLen() { return ccsds::HEADER_LEN; } + /** * Returns the complete first two bytes of the packet, which together form * the CCSDS packet ID diff --git a/src/fsfw/tmtcpacket/ccsds/SpacePacketReader.cpp b/src/fsfw/tmtcpacket/ccsds/SpacePacketReader.cpp index c528d0a7..76f92107 100644 --- a/src/fsfw/tmtcpacket/ccsds/SpacePacketReader.cpp +++ b/src/fsfw/tmtcpacket/ccsds/SpacePacketReader.cpp @@ -8,6 +8,9 @@ SpacePacketReader::SpacePacketReader(const uint8_t* setAddress, size_t maxSize_) } ReturnValue_t SpacePacketReader::checkSize() const { + if (isNull()) { + return HasReturnvaluesIF::RETURN_FAILED; + } if (getFullPacketLen() > bufSize) { return SerializeIF::STREAM_TOO_SHORT; } @@ -33,7 +36,9 @@ uint16_t SpacePacketReader::getPacketDataLen() const { return ccsds::getPacketLe ReturnValue_t SpacePacketReader::setInternalFields(const uint8_t* data, size_t maxSize_) { bufSize = maxSize_; spHeader = reinterpret_cast(data); - packetDataField = data + ccsds::HEADER_LEN; + if (maxSize_ > 6) { + packetDataField = data + ccsds::HEADER_LEN; + } return checkSize(); } diff --git a/src/fsfw/tmtcpacket/ccsds/SpacePacketReader.h b/src/fsfw/tmtcpacket/ccsds/SpacePacketReader.h index b35b813b..0d075bae 100644 --- a/src/fsfw/tmtcpacket/ccsds/SpacePacketReader.h +++ b/src/fsfw/tmtcpacket/ccsds/SpacePacketReader.h @@ -62,11 +62,6 @@ class SpacePacketReader : public SpacePacketIF, */ [[nodiscard]] size_t getBufSize() const; - /** - * CCSDS header always has 6 bytes - * @return - */ - static constexpr size_t getHeaderLen() { return ccsds::HEADER_LEN; } [[nodiscard]] uint16_t getPacketIdRaw() const override; [[nodiscard]] uint16_t getPacketSeqCtrlRaw() const override; [[nodiscard]] uint16_t getPacketDataLen() const override; diff --git a/src/fsfw/tmtcpacket/ccsds/header.cpp b/src/fsfw/tmtcpacket/ccsds/header.cpp index b7aa270e..b87ab5b3 100644 --- a/src/fsfw/tmtcpacket/ccsds/header.cpp +++ b/src/fsfw/tmtcpacket/ccsds/header.cpp @@ -1,7 +1,7 @@ #include "header.h" uint16_t ccsds::getPacketId(const PrimaryHeader &header) { - return ((header.packetIdHAndVersion & 0x1F00) << 8) + header.packetIdL; + return ((header.packetIdHAndVersion & 0x1F) << 8) + header.packetIdL; } uint16_t ccsds::getPacketSeqCtrl(const PrimaryHeader &header) { diff --git a/unittests/tmtcpacket/testCcsdsReader.cpp b/unittests/tmtcpacket/testCcsdsReader.cpp index 89ee5a46..655c34e2 100644 --- a/unittests/tmtcpacket/testCcsdsReader.cpp +++ b/unittests/tmtcpacket/testCcsdsReader.cpp @@ -1,5 +1,54 @@ +#include #include +#include "fsfw/tmtcpacket/ccsds/SpacePacketCreator.h" #include "fsfw/tmtcpacket/ccsds/SpacePacketReader.h" -TEST_CASE("CCSDS Reader", "[ccsds-reader]") {} \ No newline at end of file +TEST_CASE("CCSDS Reader", "[ccsds-reader]") { + auto params = SpacePacketParams(PacketId(ccsds::PacketType::TC, true, 0x02), + PacketSeqCtrl(ccsds::SequenceFlags::FIRST_SEGMENT, 0x34), 0x16); + SpacePacketCreator base = SpacePacketCreator(params); + // This is enough to hold 0x16 (22) bytes + 6 (SP header length) + 1 as defined as the full packet + // length derived from the length field + std::array buf{}; + uint8_t* bufPtr = buf.data(); + size_t serLen = 0; + SpacePacketReader reader; + SECTION("Empty Reader") { + REQUIRE(reader.isNull()); + REQUIRE(reader.checkSize() == HasReturnvaluesIF::RETURN_FAILED); + } + + SECTION("Basic Read") { + REQUIRE(base.serialize(&bufPtr, &serLen, buf.size(), SerializeIF::Endianness::NETWORK) == + HasReturnvaluesIF::RETURN_OK); + reader.setReadOnlyData(buf.data(), SpacePacketIF::getHeaderLen()); + REQUIRE(reader.getPacketDataLen() == 0x16); + REQUIRE(reader.getFullData() == buf.data()); + REQUIRE(reader.getFullPacketLen() == 0x16 + SpacePacketReader::getHeaderLen() + 1); + REQUIRE(reader.getPacketIdRaw() == 0x1802); + REQUIRE(reader.getSequenceFlags() == ccsds::SequenceFlags::FIRST_SEGMENT); + REQUIRE(reader.getSequenceCount() == 0x34); + REQUIRE(reader.getPacketSeqCtrlRaw() == 0x4034); + REQUIRE(reader.hasSecHeader()); + REQUIRE(reader.getApid() == 0x02); + REQUIRE(not reader.isNull()); + // We only serialized the 6 bytes of the header, so the packer data should be invalid + REQUIRE(reader.getPacketData() == nullptr); + } + + SECTION("Read with additional data") { + REQUIRE(base.serialize(&bufPtr, &serLen, buf.size(), SerializeIF::Endianness::NETWORK) == + HasReturnvaluesIF::RETURN_OK); + REQUIRE(reader.setReadOnlyData(buf.data(), buf.size()) == HasReturnvaluesIF::RETURN_OK); + REQUIRE(reader.getPacketData() == buf.data() + SpacePacketIF::getHeaderLen()); + } + + SECTION("Invalid Size") { + for (size_t i = 0; i < 5; i++) { + REQUIRE(reader.setReadOnlyData(buf.data(), i) == SerializeIF::STREAM_TOO_SHORT); + REQUIRE(not reader.isNull()); + REQUIRE(reader.getPacketData() == nullptr); + } + } +} \ No newline at end of file