From 55a238d5533e1ee7c3477d570cd4ac82683afea0 Mon Sep 17 00:00:00 2001 From: Robin Mueller Date: Sat, 23 Jul 2022 10:24:56 +0200 Subject: [PATCH] extended serialize if further --- src/fsfw/serialize/SerializeIF.h | 26 +++++++++++++++++++ .../tmtcpacket/ccsds/SpacePacketCreator.cpp | 4 +-- .../tmtcpacket/ccsds/SpacePacketCreator.h | 4 ++- src/fsfw/tmtcpacket/pus/tm/PusTmCreator.cpp | 2 +- unittests/tmtcpacket/testCcsds.cpp | 11 +++++--- unittests/tmtcpacket/testCcsdsCreator.cpp | 14 +++++----- 6 files changed, 46 insertions(+), 15 deletions(-) diff --git a/src/fsfw/serialize/SerializeIF.h b/src/fsfw/serialize/SerializeIF.h index 3f4fdc38..82becb26 100644 --- a/src/fsfw/serialize/SerializeIF.h +++ b/src/fsfw/serialize/SerializeIF.h @@ -62,6 +62,20 @@ class SerializeIF { virtual ReturnValue_t serialize(uint8_t **buffer, size_t *size, size_t maxSize, Endianness streamEndianness) const = 0; + /** + * Forwards to regular @serialize call with network endianness + */ + virtual ReturnValue_t serializeNe(uint8_t** buffer, size_t* size, size_t maxSize) { + return serialize(buffer, size, maxSize, SerializeIF::Endianness::NETWORK); + } + + /** + * If endianness is not explicitly specified, use machine endianness + */ + virtual ReturnValue_t serialize(uint8_t **buffer, size_t *size, size_t maxSize) { + return serialize(buffer, size, maxSize, SerializeIF::Endianness::MACHINE); + } + /** * Gets the size of a object if it would be serialized in a buffer * @return Size of serialized object @@ -90,6 +104,18 @@ class SerializeIF { */ virtual ReturnValue_t deSerialize(const uint8_t **buffer, size_t *size, Endianness streamEndianness) = 0; + /** + * Forwards to regular @deSerialize call with network endianness + */ + virtual ReturnValue_t deSerializeNe(const uint8_t** buffer, size_t* size, size_t maxSize) { + return deSerialize(buffer, size, SerializeIF::Endianness::NETWORK); + } + /** + * If endianness is not explicitly specified, use machine endianness + */ + virtual ReturnValue_t deSerialize(const uint8_t** buffer, size_t *size, size_t maxSize) { + return deSerialize(buffer, size, SerializeIF::Endianness::MACHINE); + } /** * Helper method which can be used if serialization should be performed without any additional diff --git a/src/fsfw/tmtcpacket/ccsds/SpacePacketCreator.cpp b/src/fsfw/tmtcpacket/ccsds/SpacePacketCreator.cpp index 7ae8656f..05da88b4 100644 --- a/src/fsfw/tmtcpacket/ccsds/SpacePacketCreator.cpp +++ b/src/fsfw/tmtcpacket/ccsds/SpacePacketCreator.cpp @@ -48,9 +48,6 @@ ReturnValue_t SpacePacketCreator::deSerialize(const uint8_t **buffer, size_t *si } bool SpacePacketCreator::isValid() const { return valid; } -ReturnValue_t SpacePacketCreator::serialize(uint8_t **buffer, size_t *size, size_t maxSize) const { - return serialize(buffer, size, maxSize, SerializeIF::Endianness::NETWORK); -} void SpacePacketCreator::setApid(uint16_t apid) { if (apid < ccsds::LIMIT_APID) { params.packetId.apid = apid; @@ -83,3 +80,4 @@ SpacePacketParams &SpacePacketCreator::getParams() { return params; } void SpacePacketCreator::setPacketType(ccsds::PacketType type) { params.packetId.packetType = type; } +bool SpacePacketCreator::operator==(const SpacePacketCreator &other) const { return false; } diff --git a/src/fsfw/tmtcpacket/ccsds/SpacePacketCreator.h b/src/fsfw/tmtcpacket/ccsds/SpacePacketCreator.h index de62283f..d30457ea 100644 --- a/src/fsfw/tmtcpacket/ccsds/SpacePacketCreator.h +++ b/src/fsfw/tmtcpacket/ccsds/SpacePacketCreator.h @@ -24,6 +24,9 @@ class SpacePacketCreator : public SpacePacketIF, public SerializeIF { public: SpacePacketCreator() = default; explicit SpacePacketCreator(SpacePacketParams params); + + bool operator==(const SpacePacketCreator &other) const; + SpacePacketCreator(ccsds::PacketType packetType, bool secHeaderFlag, uint16_t apid, ccsds::SequenceFlags seqFlags, uint16_t seqCount, uint16_t dataLen, uint8_t version = 0); @@ -41,7 +44,6 @@ class SpacePacketCreator : public SpacePacketIF, public SerializeIF { void setSeqFlags(ccsds::SequenceFlags flags); void setDataLen(uint16_t dataLen); - ReturnValue_t serialize(uint8_t **buffer, size_t *size, size_t maxSize) const; ReturnValue_t serialize(uint8_t **buffer, size_t *size, size_t maxSize, Endianness streamEndianness) const override; diff --git a/src/fsfw/tmtcpacket/pus/tm/PusTmCreator.cpp b/src/fsfw/tmtcpacket/pus/tm/PusTmCreator.cpp index b8ef0852..03cf2fbf 100644 --- a/src/fsfw/tmtcpacket/pus/tm/PusTmCreator.cpp +++ b/src/fsfw/tmtcpacket/pus/tm/PusTmCreator.cpp @@ -43,7 +43,7 @@ ReturnValue_t PusTmCreator::serialize(uint8_t** buffer, size_t* size, size_t max if (*size + getSerializedSize() > maxSize) { return SerializeIF::BUFFER_TOO_SHORT; } - ReturnValue_t result = spCreator.serialize(buffer, size, maxSize); + ReturnValue_t result = spCreator.serialize(buffer, size, maxSize, streamEndianness); if (result != HasReturnvaluesIF::RETURN_OK) { return result; } diff --git a/unittests/tmtcpacket/testCcsds.cpp b/unittests/tmtcpacket/testCcsds.cpp index a2679896..7f337b6d 100644 --- a/unittests/tmtcpacket/testCcsds.cpp +++ b/unittests/tmtcpacket/testCcsds.cpp @@ -36,18 +36,23 @@ TEST_CASE("CCSDS Low Level", "[ccsds-ll]") { 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(buf.data(), buf.size(), SerializeIF::Endianness::NETWORK) == + REQUIRE(packetId.SerializeIF::serialize(buf.data(), buf.size(), SerializeIF::Endianness::NETWORK) == HasReturnvaluesIF::RETURN_OK); REQUIRE(buf[0] == 0x1); REQUIRE(buf[1] == 0xff); } + + SECTION("From Raw") { + auto newId = PacketId(ccsds::PacketType::TC, true, 0x2ff); + uint16_t rawId = newId.raw(); + REQUIRE(rawId == 0x1aff); + REQUIRE(PacketId::fromRaw(rawId) == newId); + } } TEST_CASE("CCSDS Packet Seq Ctrl", "[ccsds-packet-seq-ctrl]") {} diff --git a/unittests/tmtcpacket/testCcsdsCreator.cpp b/unittests/tmtcpacket/testCcsdsCreator.cpp index 2043d6a6..91122ef9 100644 --- a/unittests/tmtcpacket/testCcsdsCreator.cpp +++ b/unittests/tmtcpacket/testCcsdsCreator.cpp @@ -40,7 +40,7 @@ TEST_CASE("CCSDS Creator", "[ccsds-creator]") { } SECTION("Raw Output") { - REQUIRE(base.serialize(&bufPtr, &serLen, buf.size()) == HasReturnvaluesIF::RETURN_OK); + REQUIRE(base.serializeNe(&bufPtr, &serLen, buf.size()) == HasReturnvaluesIF::RETURN_OK); // TC, and secondary header flag is set -> 0b0001100 -> 0x18 REQUIRE(buf[0] == 0x18); // APID 0x02 @@ -62,7 +62,7 @@ TEST_CASE("CCSDS Creator", "[ccsds-creator]") { base.setSeqFlags(ccsds::SequenceFlags::UNSEGMENTED); base.setDataLen(static_cast(std::pow(2, 16)) - 1); REQUIRE(base.isValid()); - REQUIRE(base.serialize(&bufPtr, &serLen, buf.size()) == HasReturnvaluesIF::RETURN_OK); + REQUIRE(base.serializeNe(&bufPtr, &serLen, buf.size()) == HasReturnvaluesIF::RETURN_OK); REQUIRE(buf[0] == 0x1F); REQUIRE(buf[1] == 0xFF); REQUIRE(buf[2] == 0xFF); @@ -75,28 +75,28 @@ TEST_CASE("CCSDS Creator", "[ccsds-creator]") { SpacePacketCreator invalid = SpacePacketCreator( ccsds::PacketType::TC, true, 0xFFFF, ccsds::SequenceFlags::FIRST_SEGMENT, 0x34, 0x16); REQUIRE(not invalid.isValid()); - REQUIRE(invalid.serialize(&bufPtr, &serLen, buf.size()) == HasReturnvaluesIF::RETURN_FAILED); + REQUIRE(invalid.serializeNe(&bufPtr, &serLen, buf.size()) == HasReturnvaluesIF::RETURN_FAILED); } SECTION("Invalid Seq Count") { SpacePacketCreator invalid = SpacePacketCreator( ccsds::PacketType::TC, true, 0x02, ccsds::SequenceFlags::FIRST_SEGMENT, 0xFFFF, 0x16); REQUIRE(not invalid.isValid()); - REQUIRE(invalid.serialize(&bufPtr, &serLen, buf.size()) == HasReturnvaluesIF::RETURN_FAILED); + REQUIRE(invalid.serializeNe(&bufPtr, &serLen, buf.size()) == HasReturnvaluesIF::RETURN_FAILED); } SECTION("Invalid Buf Size 1") { serLen = 2; - REQUIRE(base.serialize(&bufPtr, &serLen, buf.size()) == SerializeIF::BUFFER_TOO_SHORT); + REQUIRE(base.serializeNe(&bufPtr, &serLen, buf.size()) == SerializeIF::BUFFER_TOO_SHORT); } SECTION("Invalid Buf Size 2") { serLen = 4; - REQUIRE(base.serialize(&bufPtr, &serLen, buf.size()) == SerializeIF::BUFFER_TOO_SHORT); + REQUIRE(base.serializeNe(&bufPtr, &serLen, buf.size()) == SerializeIF::BUFFER_TOO_SHORT); } SECTION("Invalid Buf Size 3") { serLen = 6; - REQUIRE(base.serialize(&bufPtr, &serLen, buf.size()) == SerializeIF::BUFFER_TOO_SHORT); + REQUIRE(base.serializeNe(&bufPtr, &serLen, buf.size()) == SerializeIF::BUFFER_TOO_SHORT); } }