From b485afea5786b44bd9a7b6206c306eb3ac502457 Mon Sep 17 00:00:00 2001 From: Robin Mueller Date: Wed, 27 Jul 2022 21:29:05 +0200 Subject: [PATCH] Simple SerializeIF adaptions - Returns serialized or deserialized size --- src/fsfw/serialize/SerializeIF.h | 31 +++++++++------ src/fsfw/tmtcpacket/pus/tc/PusTcCreator.cpp | 4 +- src/fsfw/tmtcpacket/pus/tc/PusTcCreator.h | 2 +- src/fsfw/tmtcpacket/pus/tm/PusTmCreator.cpp | 7 +++- src/fsfw/tmtcpacket/pus/tm/PusTmCreator.h | 2 +- unittests/serialize/testSerializeIF.cpp | 43 ++++++++++++++------- unittests/tmtcpacket/testCcsds.cpp | 41 ++++++++++++++------ unittests/tmtcpacket/testPusTcCreator.cpp | 12 ++++++ unittests/tmtcservices/testPsb.cpp | 6 ++- 9 files changed, 101 insertions(+), 47 deletions(-) diff --git a/src/fsfw/serialize/SerializeIF.h b/src/fsfw/serialize/SerializeIF.h index 47212ab1..5e7a2fb9 100644 --- a/src/fsfw/serialize/SerializeIF.h +++ b/src/fsfw/serialize/SerializeIF.h @@ -125,22 +125,26 @@ class SerializeIF { * @param streamEndianness * @return */ - [[nodiscard]] virtual ReturnValue_t serialize(uint8_t *buffer, size_t maxSize, + [[nodiscard]] virtual ReturnValue_t serialize(uint8_t *buffer, size_t &serLen, size_t maxSize, Endianness streamEndianness) const { size_t tmpSize = 0; - return serialize(&buffer, &tmpSize, maxSize, streamEndianness); + ReturnValue_t result = serialize(&buffer, &tmpSize, maxSize, streamEndianness); + serLen = tmpSize; + return result; } /** * Forwards to regular @serialize call with big (network) endianness */ - [[nodiscard]] virtual ReturnValue_t serializeBe(uint8_t *buffer, size_t maxSize) const { - return serialize(buffer, maxSize, SerializeIF::Endianness::NETWORK); + [[nodiscard]] virtual ReturnValue_t serializeBe(uint8_t *buffer, size_t &serLen, + size_t maxSize) const { + return serialize(buffer, serLen, maxSize, SerializeIF::Endianness::NETWORK); } /** * If endianness is not explicitly specified, use machine endianness */ - [[nodiscard]] virtual ReturnValue_t serialize(uint8_t *buffer, size_t maxSize) const { - return serialize(buffer, maxSize, SerializeIF::Endianness::MACHINE); + [[nodiscard]] virtual ReturnValue_t serialize(uint8_t *buffer, size_t &serLen, + size_t maxSize) const { + return serialize(buffer, serLen, maxSize, SerializeIF::Endianness::MACHINE); } /** @@ -151,21 +155,24 @@ class SerializeIF { * @param streamEndianness * @return */ - virtual ReturnValue_t deSerialize(const uint8_t *buffer, size_t maxSize, + virtual ReturnValue_t deSerialize(const uint8_t *buffer, size_t &deserSize, size_t maxSize, Endianness streamEndianness) { - return deSerialize(&buffer, &maxSize, streamEndianness); + size_t deserLen = maxSize; + ReturnValue_t result = deSerialize(&buffer, &deserLen, streamEndianness); + deserSize = maxSize - deserLen; + return result; } /** * Forwards to regular @serialize call with big (network) endianness */ - virtual ReturnValue_t deSerializeBe(const uint8_t *buffer, size_t maxSize) { - return deSerialize(buffer, maxSize, SerializeIF::Endianness::NETWORK); + virtual ReturnValue_t deSerializeBe(const uint8_t *buffer, size_t &deserSize, size_t maxSize) { + return deSerialize(buffer, deserSize, maxSize, SerializeIF::Endianness::NETWORK); } /** * If endianness is not explicitly specified, use machine endianness */ - virtual ReturnValue_t deSerialize(const uint8_t *buffer, size_t maxSize) { - return deSerialize(buffer, maxSize, SerializeIF::Endianness::MACHINE); + virtual ReturnValue_t deSerialize(const uint8_t *buffer, size_t &deserSize, size_t maxSize) { + return deSerialize(buffer, deserSize, maxSize, SerializeIF::Endianness::MACHINE); } }; diff --git a/src/fsfw/tmtcpacket/pus/tc/PusTcCreator.cpp b/src/fsfw/tmtcpacket/pus/tc/PusTcCreator.cpp index f509717b..be472051 100644 --- a/src/fsfw/tmtcpacket/pus/tc/PusTcCreator.cpp +++ b/src/fsfw/tmtcpacket/pus/tc/PusTcCreator.cpp @@ -108,6 +108,6 @@ ReturnValue_t PusTcCreator::serialize(uint8_t **buffer, size_t *size, size_t max return serialize(buffer, size, maxSize, SerializeIF::Endianness::NETWORK); } -ReturnValue_t PusTcCreator::serialize(uint8_t *buffer, size_t maxSize) const { - return SerializeIF::serialize(buffer, maxSize, SerializeIF::Endianness::NETWORK); +ReturnValue_t PusTcCreator::serialize(uint8_t *buffer, size_t &serLen, size_t maxSize) const { + return SerializeIF::serialize(buffer, serLen, maxSize, SerializeIF::Endianness::NETWORK); } diff --git a/src/fsfw/tmtcpacket/pus/tc/PusTcCreator.h b/src/fsfw/tmtcpacket/pus/tc/PusTcCreator.h index b41ad940..63a5d45f 100644 --- a/src/fsfw/tmtcpacket/pus/tc/PusTcCreator.h +++ b/src/fsfw/tmtcpacket/pus/tc/PusTcCreator.h @@ -35,7 +35,7 @@ class PusTcCreator : public PusTcIF, public SerializeIF, public CustomUserDataIF SpacePacketParams &getSpParams(); ReturnValue_t serialize(uint8_t **buffer, size_t *size, size_t maxSize) const override; - ReturnValue_t serialize(uint8_t *buffer, size_t maxSize) const override; + ReturnValue_t serialize(uint8_t *buffer, size_t &serLen, size_t maxSize) const override; [[nodiscard]] size_t getSerializedSize() const override; [[nodiscard]] uint16_t getPacketIdRaw() const override; diff --git a/src/fsfw/tmtcpacket/pus/tm/PusTmCreator.cpp b/src/fsfw/tmtcpacket/pus/tm/PusTmCreator.cpp index 6253bc24..d80182ed 100644 --- a/src/fsfw/tmtcpacket/pus/tm/PusTmCreator.cpp +++ b/src/fsfw/tmtcpacket/pus/tm/PusTmCreator.cpp @@ -94,6 +94,9 @@ ReturnValue_t PusTmCreator::serialize(uint8_t** buffer, size_t* size, size_t max uint16_t crc16 = CRC::crc16ccitt(start, getFullPacketLen() - sizeof(ecss::PusChecksumT)); return SerializeAdapter::serialize(&crc16, buffer, size, maxSize, streamEndianness); } + // Even if no CRC is calculated, account for the space taken by it + *size += 2; + *buffer += 2; return HasReturnvaluesIF::RETURN_OK; } @@ -151,6 +154,6 @@ ReturnValue_t PusTmCreator::serialize(uint8_t** buffer, size_t* size, size_t max return serialize(buffer, size, maxSize, SerializeIF::Endianness::NETWORK); } -ReturnValue_t PusTmCreator::serialize(uint8_t* buffer, size_t maxSize) const { - return SerializeIF::serialize(buffer, maxSize, SerializeIF::Endianness::NETWORK); +ReturnValue_t PusTmCreator::serialize(uint8_t* buffer, size_t& serLen, size_t maxSize) const { + return SerializeIF::serialize(buffer, serLen, maxSize, SerializeIF::Endianness::NETWORK); } \ No newline at end of file diff --git a/src/fsfw/tmtcpacket/pus/tm/PusTmCreator.h b/src/fsfw/tmtcpacket/pus/tm/PusTmCreator.h index c8de3428..a7905858 100644 --- a/src/fsfw/tmtcpacket/pus/tm/PusTmCreator.h +++ b/src/fsfw/tmtcpacket/pus/tm/PusTmCreator.h @@ -74,7 +74,7 @@ class PusTmCreator : public SerializeIF, public PusTmIF, public CustomUserDataIF uint16_t getDestId() override; // Override these to use default big endianness ReturnValue_t serialize(uint8_t** buffer, size_t* size, size_t maxSize) const override; - ReturnValue_t serialize(uint8_t* buffer, size_t maxSize) const override; + ReturnValue_t serialize(uint8_t* buffer, size_t& serLen, size_t maxSize) const override; ReturnValue_t serialize(uint8_t** buffer, size_t* size, size_t maxSize, Endianness streamEndianness) const override; [[nodiscard]] size_t getSerializedSize() const override; diff --git a/unittests/serialize/testSerializeIF.cpp b/unittests/serialize/testSerializeIF.cpp index ecd61357..5200f8b7 100644 --- a/unittests/serialize/testSerializeIF.cpp +++ b/unittests/serialize/testSerializeIF.cpp @@ -24,12 +24,14 @@ TEST_CASE("Serialize IF Serialize", "[serialize-if-ser]") { } SECTION("Little Endian Simple") { - REQUIRE( - simpleSer.SerializeIF::serialize(buf.data(), buf.size(), SerializeIF::Endianness::LITTLE) == - HasReturnvaluesIF::RETURN_OK); + size_t serLen = 0xff; + REQUIRE(simpleSer.SerializeIF::serialize(buf.data(), serLen, buf.size(), + SerializeIF::Endianness::LITTLE) == + HasReturnvaluesIF::RETURN_OK); CHECK(buf[0] == 1); CHECK(buf[1] == 3); CHECK(buf[2] == 2); + CHECK(serLen == 3); } SECTION("Big Endian Normal") { @@ -54,23 +56,25 @@ TEST_CASE("Serialize IF Serialize", "[serialize-if-ser]") { } SECTION("Big Endian Simple") { + size_t serLen = 0xff; SECTION("Explicit") { - REQUIRE( - simpleSer.SerializeIF::serialize(buf.data(), buf.size(), SerializeIF::Endianness::BIG) == - HasReturnvaluesIF::RETURN_OK); + REQUIRE(simpleSer.SerializeIF::serialize(buf.data(), serLen, buf.size(), + SerializeIF::Endianness::BIG) == + HasReturnvaluesIF::RETURN_OK); } SECTION("Network 0") { - REQUIRE(simpleSer.SerializeIF::serialize(buf.data(), buf.size(), + REQUIRE(simpleSer.SerializeIF::serialize(buf.data(), serLen, buf.size(), SerializeIF::Endianness::NETWORK) == HasReturnvaluesIF::RETURN_OK); } SECTION("Network 1") { - REQUIRE(simpleSer.SerializeIF::serializeBe(buf.data(), buf.size()) == + REQUIRE(simpleSer.SerializeIF::serializeBe(buf.data(), serLen, buf.size()) == HasReturnvaluesIF::RETURN_OK); } CHECK(buf[0] == 1); CHECK(buf[1] == 2); CHECK(buf[2] == 3); + CHECK(serLen == 3); } SECTION("Machine Endian Implicit") { @@ -90,7 +94,8 @@ TEST_CASE("Serialize IF Serialize", "[serialize-if-ser]") { } SECTION("Machine Endian Simple Implicit") { - REQUIRE(simpleSer.SerializeIF::serialize(buf.data(), buf.size()) == + size_t serLen = 0xff; + REQUIRE(simpleSer.SerializeIF::serialize(buf.data(), serLen, buf.size()) == HasReturnvaluesIF::RETURN_OK); CHECK(buf[0] == 1); #if BYTE_ORDER_SYSTEM == LITTLE_ENDIAN @@ -100,6 +105,7 @@ TEST_CASE("Serialize IF Serialize", "[serialize-if-ser]") { CHECK(buf[1] == 2); CHECK(buf[2] == 3); #endif + CHECK(serLen == 3); } } @@ -119,10 +125,13 @@ TEST_CASE("SerializeIF Deserialize", "[serialize-if-de]") { } SECTION("Little Endian Simple") { - REQUIRE(simpleSer.SerializeIF::deSerialize(ptr, len, SerializeIF::Endianness::LITTLE) == - HasReturnvaluesIF::RETURN_OK); + size_t deserLen = 0xff; + REQUIRE( + simpleSer.SerializeIF::deSerialize(ptr, deserLen, len, SerializeIF::Endianness::LITTLE) == + HasReturnvaluesIF::RETURN_OK); CHECK(simpleSer.getU8() == 5); CHECK(simpleSer.getU16() == 0x0100); + CHECK(deserLen == 3); } SECTION("Big Endian Normal") { @@ -144,22 +153,24 @@ TEST_CASE("SerializeIF Deserialize", "[serialize-if-de]") { } SECTION("Big Endian Simple") { + size_t deserLen = 0xff; SECTION("Explicit") { - REQUIRE(simpleSer.SerializeIF::deSerialize(buf.data(), buf.size(), + REQUIRE(simpleSer.SerializeIF::deSerialize(buf.data(), deserLen, buf.size(), SerializeIF::Endianness::BIG) == HasReturnvaluesIF::RETURN_OK); } SECTION("Network 0") { - REQUIRE(simpleSer.SerializeIF::deSerialize(buf.data(), buf.size(), + REQUIRE(simpleSer.SerializeIF::deSerialize(buf.data(), deserLen, buf.size(), SerializeIF::Endianness::NETWORK) == HasReturnvaluesIF::RETURN_OK); } SECTION("Network 1") { - REQUIRE(simpleSer.SerializeIF::deSerializeBe(buf.data(), buf.size()) == + REQUIRE(simpleSer.SerializeIF::deSerializeBe(buf.data(), deserLen, buf.size()) == HasReturnvaluesIF::RETURN_OK); } CHECK(simpleSer.getU8() == 5); CHECK(simpleSer.getU16() == 1); + CHECK(deserLen == 3); } SECTION("Machine Endian Implicit") { @@ -176,7 +187,8 @@ TEST_CASE("SerializeIF Deserialize", "[serialize-if-de]") { } SECTION("Machine Endian Simple Implicit") { - REQUIRE(simpleSer.SerializeIF::deSerialize(buf.data(), buf.size()) == + size_t deserLen = 0xff; + REQUIRE(simpleSer.SerializeIF::deSerialize(buf.data(), deserLen, buf.size()) == HasReturnvaluesIF::RETURN_OK); CHECK(simpleSer.getU8() == 5); #if BYTE_ORDER_SYSTEM == LITTLE_ENDIAN @@ -184,5 +196,6 @@ TEST_CASE("SerializeIF Deserialize", "[serialize-if-de]") { #else CHECK(simpleSer.getU16() == 1); #endif + CHECK(deserLen == 3); } } \ No newline at end of file diff --git a/unittests/tmtcpacket/testCcsds.cpp b/unittests/tmtcpacket/testCcsds.cpp index 9be2b8af..095020ae 100644 --- a/unittests/tmtcpacket/testCcsds.cpp +++ b/unittests/tmtcpacket/testCcsds.cpp @@ -42,21 +42,28 @@ TEST_CASE("CCSDS Packet ID", "[ccsds-packet-id]") { packetId.apid = 0x1ff; packetId.secHeaderFlag = false; packetId.packetType = ccsds::PacketType::TM; + size_t serLen = 0; REQUIRE(packetId.raw() == 0x1ff); - REQUIRE(packetId.SerializeIF::serializeBe(buf.data(), buf.size()) == + REQUIRE(packetId.SerializeIF::serializeBe(buf.data(), serLen, buf.size()) == HasReturnvaluesIF::RETURN_OK); CHECK(buf[0] == 0x1); CHECK(buf[1] == 0xff); } SECTION("Invalid Ser") { - REQUIRE(packetId.SerializeIF::serializeBe(buf.data(), 0) == SerializeIF::BUFFER_TOO_SHORT); - REQUIRE(packetId.SerializeIF::serializeBe(buf.data(), 1) == SerializeIF::BUFFER_TOO_SHORT); + size_t serLen = 0; + REQUIRE(packetId.SerializeIF::serializeBe(buf.data(), serLen, 0) == + SerializeIF::BUFFER_TOO_SHORT); + REQUIRE(packetId.SerializeIF::serializeBe(buf.data(), serLen, 1) == + SerializeIF::BUFFER_TOO_SHORT); } SECTION("Invalid Deser") { - REQUIRE(packetId.SerializeIF::deSerialize(buf.data(), 1) == SerializeIF::STREAM_TOO_SHORT); - REQUIRE(packetId.SerializeIF::deSerialize(buf.data(), 0) == SerializeIF::STREAM_TOO_SHORT); + size_t deserLen = 0; + REQUIRE(packetId.SerializeIF::deSerialize(buf.data(), deserLen, 1) == + SerializeIF::STREAM_TOO_SHORT); + REQUIRE(packetId.SerializeIF::deSerialize(buf.data(), deserLen, 0) == + SerializeIF::STREAM_TOO_SHORT); } SECTION("From Raw") { @@ -69,9 +76,11 @@ TEST_CASE("CCSDS Packet ID", "[ccsds-packet-id]") { SECTION("Deserialize") { buf[0] = 0x1a; buf[1] = 0xff; - REQUIRE(packetId.SerializeIF::deSerialize(buf.data(), buf.size()) == + size_t deserLen = 0xff; + REQUIRE(packetId.SerializeIF::deSerialize(buf.data(), deserLen, buf.size()) == HasReturnvaluesIF::RETURN_OK); CHECK(packetId.apid == 0x2ff); + CHECK(deserLen == 2); CHECK(packetId.packetType == ccsds::PacketType::TC); CHECK(packetId.secHeaderFlag == true); } @@ -81,12 +90,15 @@ TEST_CASE("CCSDS Packet Seq Ctrl", "[ccsds-packet-seq-ctrl]") { PacketSeqCtrl psc; std::array buf{}; SECTION("Basic") { + size_t serLen = 0xff; psc.seqFlags = ccsds::SequenceFlags::FIRST_SEGMENT; psc.seqCount = static_cast(std::round(std::pow(2, 14) - 1)); REQUIRE(psc.raw() == 0x7fff); - REQUIRE(psc.SerializeIF::serializeBe(buf.data(), buf.size()) == HasReturnvaluesIF::RETURN_OK); + REQUIRE(psc.SerializeIF::serializeBe(buf.data(), serLen, buf.size()) == + HasReturnvaluesIF::RETURN_OK); CHECK(buf[0] == 0x7f); CHECK(buf[1] == 0xff); + CHECK(serLen == 2); } SECTION("From Raw") { @@ -100,18 +112,23 @@ TEST_CASE("CCSDS Packet Seq Ctrl", "[ccsds-packet-seq-ctrl]") { SECTION("Deserialize") { buf[0] = 0xbf; buf[1] = 0xfe; - REQUIRE(psc.SerializeIF::deSerialize(buf.data(), buf.size()) == HasReturnvaluesIF::RETURN_OK); + size_t deserLen = 0xff; + REQUIRE(psc.SerializeIF::deSerialize(buf.data(), deserLen, buf.size()) == + HasReturnvaluesIF::RETURN_OK); CHECK(psc.seqFlags == ccsds::SequenceFlags::LAST_SEGMENT); + CHECK(deserLen == 2); CHECK(psc.seqCount == static_cast(std::round(std::pow(2, 14) - 2))); } SECTION("Invalid Ser") { - REQUIRE(psc.SerializeIF::serializeBe(buf.data(), 0) == SerializeIF::BUFFER_TOO_SHORT); - REQUIRE(psc.SerializeIF::serializeBe(buf.data(), 1) == SerializeIF::BUFFER_TOO_SHORT); + size_t deserLen = 0; + REQUIRE(psc.SerializeIF::serializeBe(buf.data(), deserLen, 0) == SerializeIF::BUFFER_TOO_SHORT); + REQUIRE(psc.SerializeIF::serializeBe(buf.data(), deserLen, 1) == SerializeIF::BUFFER_TOO_SHORT); } SECTION("Invalid Deser") { - REQUIRE(psc.SerializeIF::deSerialize(buf.data(), 1) == SerializeIF::STREAM_TOO_SHORT); - REQUIRE(psc.SerializeIF::deSerialize(buf.data(), 0) == SerializeIF::STREAM_TOO_SHORT); + size_t deserLen = 0; + REQUIRE(psc.SerializeIF::deSerialize(buf.data(), deserLen, 1) == SerializeIF::STREAM_TOO_SHORT); + REQUIRE(psc.SerializeIF::deSerialize(buf.data(), deserLen, 0) == SerializeIF::STREAM_TOO_SHORT); } } diff --git a/unittests/tmtcpacket/testPusTcCreator.cpp b/unittests/tmtcpacket/testPusTcCreator.cpp index fdda717f..ff9b6399 100644 --- a/unittests/tmtcpacket/testPusTcCreator.cpp +++ b/unittests/tmtcpacket/testPusTcCreator.cpp @@ -94,6 +94,18 @@ TEST_CASE("PUS TC Creator", "[pus-tc-creator]") { REQUIRE(buf[13] == 3); } + SECTION("Test with Application Data Serializable Simple Ser API") { + auto& params = creator.getPusParams(); + auto simpleSer = SimpleSerializable(); + creator.setSerializableUserData(simpleSer); + REQUIRE(creator.getSerializedSize() == 16); + REQUIRE(creator.serialize(dataPtr, serLen, buf.size()) == HasReturnvaluesIF::RETURN_OK); + REQUIRE(serLen == 16); + REQUIRE(buf[11] == 1); + REQUIRE(buf[12] == 2); + REQUIRE(buf[13] == 3); + } + SECTION("Deserialization Fails") { SerializeIF& deser = creator; size_t deserLen = buf.size(); diff --git a/unittests/tmtcservices/testPsb.cpp b/unittests/tmtcservices/testPsb.cpp index e434607f..9404c401 100644 --- a/unittests/tmtcservices/testPsb.cpp +++ b/unittests/tmtcservices/testPsb.cpp @@ -57,7 +57,8 @@ TEST_CASE("Pus Service Base", "[pus-service-base]") { REQUIRE(psb.performServiceCallCnt == 0); uint8_t* dataPtr; REQUIRE(pool.getFreeElement(&storeId, creator.getSerializedSize(), &dataPtr) == retval::OK); - REQUIRE(creator.serializeBe(dataPtr, creator.getSerializedSize()) == retval::OK); + size_t serLen = 0; + REQUIRE(creator.serializeBe(dataPtr, serLen, creator.getSerializedSize()) == retval::OK); tmtcMsg.setStorageId(storeId); msgQueue.addReceivedMessage(tmtcMsg); REQUIRE(psb.performOperation(0) == retval::OK); @@ -77,7 +78,8 @@ TEST_CASE("Pus Service Base", "[pus-service-base]") { SECTION("Send Request with Failed Handling") { uint8_t* dataPtr; REQUIRE(pool.getFreeElement(&storeId, creator.getSerializedSize(), &dataPtr) == retval::OK); - REQUIRE(creator.serializeBe(dataPtr, creator.getSerializedSize()) == retval::OK); + size_t serLen = 0; + REQUIRE(creator.serializeBe(dataPtr, serLen, creator.getSerializedSize()) == retval::OK); tmtcMsg.setStorageId(storeId); msgQueue.addReceivedMessage(tmtcMsg); psb.makeNextHandleReqCallFail(3);