From 133894f4ba06dda17346fddaa4d448fb95a51776 Mon Sep 17 00:00:00 2001 From: Robin Mueller Date: Fri, 22 Jul 2022 18:46:58 +0200 Subject: [PATCH] add basic CCSDS tests --- CMakeLists.txt | 2 +- src/fsfw/tmtcpacket/ccsds/PacketId.h | 16 +++++++-- unittests/tmtcpacket/CMakeLists.txt | 1 + unittests/tmtcpacket/testCcsds.cpp | 53 ++++++++++++++++++++++++++++ 4 files changed, 69 insertions(+), 3 deletions(-) create mode 100644 unittests/tmtcpacket/testCcsds.cpp diff --git a/CMakeLists.txt b/CMakeLists.txt index e59eb4c5..acc061fc 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -168,7 +168,7 @@ if(FSFW_BUILD_TESTS) configure_file(unittests/testcfg/TestsConfig.h.in tests/TestsConfig.h) project(${FSFW_TEST_TGT} CXX C) - add_executable(${FSFW_TEST_TGT}) + add_executable(${FSFW_TEST_TGT} unittests/tmtcpacket/testCcsds.cpp) if(IPO_SUPPORTED AND FSFW_ENABLE_IPO) set_property(TARGET ${FSFW_TEST_TGT} PROPERTY INTERPROCEDURAL_OPTIMIZATION TRUE) diff --git a/src/fsfw/tmtcpacket/ccsds/PacketId.h b/src/fsfw/tmtcpacket/ccsds/PacketId.h index 29b82f13..2c77652c 100644 --- a/src/fsfw/tmtcpacket/ccsds/PacketId.h +++ b/src/fsfw/tmtcpacket/ccsds/PacketId.h @@ -42,8 +42,20 @@ struct PacketId : public SerializeIF { ReturnValue_t serialize(uint8_t **buffer, size_t *size, size_t maxSize, Endianness streamEndianness) const override { - uint16_t pscRaw = raw(); - return SerializeAdapter::serialize(&pscRaw, buffer, size, maxSize, streamEndianness); + if (*size + getSerializedSize() > maxSize) { + return SerializeIF::BUFFER_TOO_SHORT; + } + uint16_t idRaw = raw(); + // Leave the first three bits untouched, they could generally contain the CCSDS version, + // or more generally, the packet ID is a 13 bit field + **buffer &= ~0x1f; + **buffer |= (idRaw >> 8) & 0x1f; + *size += 1; + *buffer += 1; + **buffer = idRaw & 0xff; + *size += 1; + *buffer += 1; + return HasReturnvaluesIF::RETURN_OK; } [[nodiscard]] size_t getSerializedSize() const override { return 2; } diff --git a/unittests/tmtcpacket/CMakeLists.txt b/unittests/tmtcpacket/CMakeLists.txt index f47c6370..dd0100a6 100644 --- a/unittests/tmtcpacket/CMakeLists.txt +++ b/unittests/tmtcpacket/CMakeLists.txt @@ -5,4 +5,5 @@ target_sources(${FSFW_TEST_TGT} PRIVATE testPusTcReader.cpp testPusTmCreator.cpp testPusTmReader.cpp + testCcsds.cpp ) diff --git a/unittests/tmtcpacket/testCcsds.cpp b/unittests/tmtcpacket/testCcsds.cpp new file mode 100644 index 00000000..28b255e5 --- /dev/null +++ b/unittests/tmtcpacket/testCcsds.cpp @@ -0,0 +1,53 @@ +#include +#include + +#include "fsfw/tmtcpacket/ccsds/PacketId.h" +#include "fsfw/tmtcpacket/ccsds/header.h" + +TEST_CASE("CCSDS Low Level", "[ccsds-ll]") { + SECTION("Lowlevel Header Packet ID test") { + ccsds::PrimaryHeader header{}; + uint16_t packetIdRaw = 0x3ff; + ccsds::setPacketId(header, packetIdRaw); + REQUIRE(header.packetIdHAndVersion == 3); + REQUIRE(header.packetIdL == 0xff); + REQUIRE(ccsds::getPacketId(header) == 0x3ff); + header.packetIdHAndVersion |= 0b00100000; + REQUIRE(ccsds::getPacketId(header) == 0x3ff); + REQUIRE(ccsds::getVersion(header) == 0b001); + } + + SECTION("Lowlevel Header APID Test") { + ccsds::PrimaryHeader header{}; + uint16_t packetIdRaw = 0x3ff; + ccsds::setPacketId(header, packetIdRaw); + ccsds::setApid(header, 0x1ff); + REQUIRE(ccsds::getPacketId(header) == 0x1ff); + } + + SECTION("Lowlevel Packet Length Test") { + ccsds::PrimaryHeader header{}; + header.packetLenH = 0x02; + header.packetLenL = 0x03; + REQUIRE(ccsds::getPacketLen(header) == 0x0203); + } +} + +TEST_CASE("CCSDS Packet ID", "[ccsds-packet-id]") { + PacketId packetId; + std::array buf{}; + size_t serLen = 0; + uint8_t* ptr = buf.data(); + SECTION("Basic") { + packetId.apid = 0x1ff; + packetId.secHeaderFlag = false; + packetId.packetType = ccsds::PacketType::TM; + REQUIRE(packetId.raw() == 0x1ff); + REQUIRE(packetId.serialize(&ptr, &serLen, buf.size(), SerializeIF::Endianness::NETWORK) == + HasReturnvaluesIF::RETURN_OK); + REQUIRE(buf[0] == 0x1); + REQUIRE(buf[1] == 0xff); + } +} + +TEST_CASE("CCSDS Packet Seq Ctrl", "[ccsds-packet-seq-ctrl]") {}