From 75dc7a405d566ba99cb742eb967f6261b2e0c440 Mon Sep 17 00:00:00 2001 From: Robin Mueller Date: Mon, 5 Sep 2022 16:02:32 +0200 Subject: [PATCH] del data wrapper --- src/fsfw/serialize/SerialBufferAdapter.cpp | 2 +- src/fsfw/serialize/SerialBufferAdapter.h | 4 +- src/fsfw/tmtcpacket/pus/tc/PusTcCreator.cpp | 28 ++++----- src/fsfw/tmtcpacket/pus/tc/PusTcCreator.h | 13 +++- src/fsfw/tmtcpacket/pus/tm/PusTmCreator.cpp | 20 ++++--- src/fsfw/tmtcpacket/pus/tm/PusTmCreator.h | 20 +++---- src/fsfw/util/dataWrapper.h | 60 ------------------- unittests/tmtcpacket/testPusTcCreator.cpp | 2 +- .../tmtcservices/testStoreAndSendHelper.cpp | 16 ++--- 9 files changed, 52 insertions(+), 113 deletions(-) delete mode 100644 src/fsfw/util/dataWrapper.h diff --git a/src/fsfw/serialize/SerialBufferAdapter.cpp b/src/fsfw/serialize/SerialBufferAdapter.cpp index 0893dbde..0293191e 100644 --- a/src/fsfw/serialize/SerialBufferAdapter.cpp +++ b/src/fsfw/serialize/SerialBufferAdapter.cpp @@ -21,7 +21,7 @@ SerialBufferAdapter::SerialBufferAdapter(uint8_t* buffer, count_t buffe bufferLength(bufferLength) {} template -SerialBufferAdapter::~SerialBufferAdapter() {} +SerialBufferAdapter::~SerialBufferAdapter() = default; template ReturnValue_t SerialBufferAdapter::serialize(uint8_t** buffer, size_t* size, diff --git a/src/fsfw/serialize/SerialBufferAdapter.h b/src/fsfw/serialize/SerialBufferAdapter.h index e53a12bc..395f93d2 100644 --- a/src/fsfw/serialize/SerialBufferAdapter.h +++ b/src/fsfw/serialize/SerialBufferAdapter.h @@ -44,7 +44,7 @@ class SerialBufferAdapter : public SerializeIF { ~SerialBufferAdapter() override; ReturnValue_t serialize(uint8_t** buffer, size_t* size, size_t maxSize, - Endianness streamEndianness) const override; + Endianness streamEndianness) const override; [[nodiscard]] size_t getSerializedSize() const override; @@ -61,7 +61,7 @@ class SerialBufferAdapter : public SerializeIF { * @return */ ReturnValue_t deSerialize(const uint8_t** buffer, size_t* size, - Endianness streamEndianness) override; + Endianness streamEndianness) override; uint8_t* getBuffer(); [[nodiscard]] const uint8_t* getConstBuffer() const; diff --git a/src/fsfw/tmtcpacket/pus/tc/PusTcCreator.cpp b/src/fsfw/tmtcpacket/pus/tc/PusTcCreator.cpp index 14a440a0..34d8396d 100644 --- a/src/fsfw/tmtcpacket/pus/tc/PusTcCreator.cpp +++ b/src/fsfw/tmtcpacket/pus/tc/PusTcCreator.cpp @@ -14,7 +14,6 @@ PusTcCreator::PusTcCreator(SpacePacketParams spParams, PusTcParams pusParams) ReturnValue_t PusTcCreator::serialize(uint8_t **buffer, size_t *size, size_t maxSize, SerializeIF::Endianness streamEndianness) const { const uint8_t *start = *buffer; - size_t userDataLen = pusParams.dataWrapper.getLength(); if (*size + getSerializedSize() > maxSize) { return SerializeIF::BUFFER_TOO_SHORT; } @@ -37,17 +36,8 @@ ReturnValue_t PusTcCreator::serialize(uint8_t **buffer, size_t *size, size_t max if (result != returnvalue::OK) { return result; } - if (pusParams.dataWrapper.type == util::DataTypes::RAW) { - const uint8_t *data = pusParams.dataWrapper.dataUnion.raw.data; - if (data != nullptr and userDataLen > 0) { - std::memcpy(*buffer, data, userDataLen); - *buffer += userDataLen; - *size += userDataLen; - } - } else if (pusParams.dataWrapper.type == util::DataTypes::SERIALIZABLE and - pusParams.dataWrapper.dataUnion.serializable != nullptr) { - result = pusParams.dataWrapper.dataUnion.serializable->serialize(buffer, size, maxSize, - streamEndianness); + if (pusParams.appData != nullptr) { + result = pusParams.appData->serialize(buffer, size, maxSize, streamEndianness); if (result != returnvalue::OK) { return result; } @@ -58,8 +48,11 @@ ReturnValue_t PusTcCreator::serialize(uint8_t **buffer, size_t *size, size_t max } void PusTcCreator::updateSpLengthField() { - spCreator.setDataLen(ecss::PusTcDataFieldHeader::MIN_SIZE + pusParams.dataWrapper.getLength() + - 1); + size_t len = ecss::PusTcDataFieldHeader::MIN_SIZE + 1; + if (pusParams.appData != nullptr) { + len += pusParams.appData->getSerializedSize(); + } + spCreator.setDataLen(len); } size_t PusTcCreator::getSerializedSize() const { return spCreator.getFullPacketLen(); } @@ -91,14 +84,15 @@ SpacePacketParams &PusTcCreator::getSpParams() { return spCreator.getParams(); } ReturnValue_t PusTcCreator::setRawUserData(const uint8_t *data, size_t len) { // TODO: Check length field? - pusParams.dataWrapper.setRawData({data, len}); + pusParams.bufAdapter.setConstBuffer(data, len); + pusParams.appData = &pusParams.bufAdapter; updateSpLengthField(); return returnvalue::OK; } -ReturnValue_t PusTcCreator::setSerializableUserData(SerializeIF &serializable) { +ReturnValue_t PusTcCreator::setSerializableUserData(const SerializeIF &serializable) { // TODO: Check length field? - pusParams.dataWrapper.setSerializable(serializable); + pusParams.appData = &serializable; updateSpLengthField(); return returnvalue::OK; } diff --git a/src/fsfw/tmtcpacket/pus/tc/PusTcCreator.h b/src/fsfw/tmtcpacket/pus/tc/PusTcCreator.h index 4b0d81c8..add8a379 100644 --- a/src/fsfw/tmtcpacket/pus/tc/PusTcCreator.h +++ b/src/fsfw/tmtcpacket/pus/tc/PusTcCreator.h @@ -7,16 +7,23 @@ #include "fsfw/tmtcpacket/pus/CustomUserDataIF.h" #include "fsfw/tmtcpacket/pus/defs.h" #include "fsfw/tmtcpacket/pus/tc/PusTcIF.h" -#include "fsfw/util/dataWrapper.h" struct PusTcParams { PusTcParams(uint8_t service_, uint8_t subservice_) : service(service_), subservice(subservice_) {} + void setRawAppData(const uint8_t *data, size_t len) { + bufAdapter.setConstBuffer(data, len); + appData = &bufAdapter; + } + + void setSerializableAppData(const SerializeIF &serializable) { appData = &serializable; } + uint8_t service; uint8_t subservice; uint8_t ackFlags = ecss::ACK_ALL; uint16_t sourceId = 0; - util::DataWrapper dataWrapper{}; + SerialBufferAdapter bufAdapter; + const SerializeIF *appData = nullptr; uint8_t pusVersion = ecss::PusVersion::PUS_C; }; @@ -52,7 +59,7 @@ class PusTcCreator : public PusTcIF, public SerializeIF, public CustomUserDataIF [[nodiscard]] uint8_t getSubService() const override; [[nodiscard]] uint16_t getSourceId() const override; ReturnValue_t setRawUserData(const uint8_t *data, size_t len) override; - ReturnValue_t setSerializableUserData(SerializeIF &serializable) override; + ReturnValue_t setSerializableUserData(const SerializeIF &serializable) override; // Load all big endian helpers into the class namespace using SerializeIF::serializeBe; diff --git a/src/fsfw/tmtcpacket/pus/tm/PusTmCreator.cpp b/src/fsfw/tmtcpacket/pus/tm/PusTmCreator.cpp index 1288b6d8..41512c07 100644 --- a/src/fsfw/tmtcpacket/pus/tm/PusTmCreator.cpp +++ b/src/fsfw/tmtcpacket/pus/tm/PusTmCreator.cpp @@ -51,7 +51,6 @@ ReturnValue_t PusTmCreator::serialize(uint8_t** buffer, size_t* size, size_t max if (result != returnvalue::OK) { return result; } - size_t userDataLen = pusParams.data->getSerializedSize(); **buffer = ((pusParams.secHeader.pusVersion << 4) & 0xF0) | (pusParams.secHeader.scTimeRefStatus & 0x0F); *buffer += 1; @@ -77,8 +76,8 @@ ReturnValue_t PusTmCreator::serialize(uint8_t** buffer, size_t* size, size_t max } } - if (pusParams.data != nullptr) { - result = pusParams.data->serialize(buffer, size, maxSize, streamEndianness); + if (pusParams.sourceData != nullptr) { + result = pusParams.sourceData->serialize(buffer, size, maxSize, streamEndianness); if (result != returnvalue::OK) { return result; } @@ -104,8 +103,10 @@ TimeWriterIF* PusTmCreator::getTimestamper() const { return pusParams.secHeader. SpacePacketParams& PusTmCreator::getSpParams() { return spCreator.getParams(); } void PusTmCreator::updateSpLengthField() { - size_t headerLen = PusTmIF::MIN_SEC_HEADER_LEN + pusParams.data->getSerializedSize() + - sizeof(ecss::PusChecksumT) - 1; + size_t headerLen = PusTmIF::MIN_SEC_HEADER_LEN + sizeof(ecss::PusChecksumT) - 1; + if (pusParams.sourceData != nullptr) { + headerLen += pusParams.sourceData->getSerializedSize(); + } if (pusParams.secHeader.timeStamper != nullptr) { headerLen += pusParams.secHeader.timeStamper->getSerializedSize(); } @@ -127,12 +128,17 @@ void PusTmCreator::setMessageTypeCounter(uint16_t messageTypeCounter) { void PusTmCreator::setDestId(uint16_t destId) { pusParams.secHeader.destId = destId; } ReturnValue_t PusTmCreator::setRawUserData(const uint8_t* data, size_t len) { - pusParams.adapter.setConstBuffer(data, len); + if (data == nullptr or len == 0) { + pusParams.sourceData = nullptr; + } else { + pusParams.adapter.setConstBuffer(data, len); + pusParams.sourceData = &pusParams.adapter; + } updateSpLengthField(); return returnvalue::OK; } ReturnValue_t PusTmCreator::setSerializableUserData(const SerializeIF& serializable) { - pusParams.data = &serializable; + pusParams.sourceData = &serializable; updateSpLengthField(); return returnvalue::OK; } diff --git a/src/fsfw/tmtcpacket/pus/tm/PusTmCreator.h b/src/fsfw/tmtcpacket/pus/tm/PusTmCreator.h index c108aa73..caf6f219 100644 --- a/src/fsfw/tmtcpacket/pus/tm/PusTmCreator.h +++ b/src/fsfw/tmtcpacket/pus/tm/PusTmCreator.h @@ -4,7 +4,6 @@ #include "PusTmIF.h" #include "fsfw/tmtcpacket/ccsds/SpacePacketCreator.h" #include "fsfw/tmtcpacket/pus/CustomUserDataIF.h" -#include "fsfw/util/dataWrapper.h" struct PusTmSecHeader { PusTmSecHeader() = default; @@ -22,29 +21,26 @@ struct PusTmSecHeader { struct PusTmParams { PusTmParams() = default; - explicit PusTmParams(PusTmSecHeader secHeader) : secHeader(secHeader) {}; + explicit PusTmParams(PusTmSecHeader secHeader) : secHeader(secHeader){}; PusTmParams(PusTmSecHeader secHeader, const SerializeIF& data) - : secHeader(secHeader), data(&data) {} + : secHeader(secHeader), sourceData(&data) {} PusTmParams(PusTmSecHeader secHeader, const uint8_t* data, size_t dataLen) - : secHeader(secHeader), adapter(data, dataLen), data(&adapter) { - } + : secHeader(secHeader), adapter(data, dataLen), sourceData(&adapter) {} PusTmParams(uint8_t service, uint8_t subservice, TimeWriterIF* timeStamper) : secHeader(service, subservice, timeStamper) {} PusTmParams(uint8_t service, uint8_t subservice, TimeWriterIF* timeStamper, const SerializeIF& data_) : PusTmParams(service, subservice, timeStamper) { - data = &data_; + sourceData = &data_; } - PusTmParams(uint8_t service, uint8_t subservice, TimeWriterIF* timeStamper, - const uint8_t* data, size_t dataLen) - : secHeader(service, subservice, timeStamper), - adapter(data, dataLen), - data(&adapter) {} + PusTmParams(uint8_t service, uint8_t subservice, TimeWriterIF* timeStamper, const uint8_t* data, + size_t dataLen) + : secHeader(service, subservice, timeStamper), adapter(data, dataLen), sourceData(&adapter) {} PusTmSecHeader secHeader; SerialBufferAdapter adapter; - const SerializeIF* data = nullptr; + const SerializeIF* sourceData = nullptr; }; class TimeWriterIF; diff --git a/src/fsfw/util/dataWrapper.h b/src/fsfw/util/dataWrapper.h deleted file mode 100644 index 74a9ff5f..00000000 --- a/src/fsfw/util/dataWrapper.h +++ /dev/null @@ -1,60 +0,0 @@ -#ifndef FSFW_UTIL_DATAWRAPPER_H -#define FSFW_UTIL_DATAWRAPPER_H - -#include -#include -#include - -#include "fsfw/serialize.h" - -namespace util { - -struct RawData { - const uint8_t* data = nullptr; - size_t len = 0; -}; - -enum DataTypes { NONE, RAW, SERIALIZABLE }; - -union DataUnion { - RawData raw; - SerializeIF* serializable = nullptr; -}; - -struct DataWrapper { - DataTypes type = DataTypes::NONE; - DataUnion dataUnion; - using BufPairT = std::pair; - - [[nodiscard]] size_t getLength() const { - if (type == DataTypes::RAW) { - return dataUnion.raw.len; - } else if (type == DataTypes::SERIALIZABLE and dataUnion.serializable != nullptr) { - return dataUnion.serializable->getSerializedSize(); - } - return 0; - } - - [[nodiscard]] bool isNull() const { - if ((type == DataTypes::NONE) or (type == DataTypes::RAW and dataUnion.raw.data == nullptr) or - (type == DataTypes::SERIALIZABLE and dataUnion.serializable == nullptr)) { - return true; - } - return false; - } - - void setRawData(BufPairT bufPair) { - type = DataTypes::RAW; - dataUnion.raw.data = bufPair.first; - dataUnion.raw.len = bufPair.second; - } - - void setSerializable(SerializeIF& serializable) { - type = DataTypes::SERIALIZABLE; - dataUnion.serializable = &serializable; - } -}; - -} // namespace util - -#endif // FSFW_UTIL_DATAWRAPPER_H diff --git a/unittests/tmtcpacket/testPusTcCreator.cpp b/unittests/tmtcpacket/testPusTcCreator.cpp index b18c4717..58f8cbf9 100644 --- a/unittests/tmtcpacket/testPusTcCreator.cpp +++ b/unittests/tmtcpacket/testPusTcCreator.cpp @@ -71,7 +71,7 @@ TEST_CASE("PUS TC Creator", "[pus-tc-creator]") { SECTION("Test with Application Data Raw") { auto& params = creator.getPusParams(); std::array data{1, 2, 3}; - params.dataWrapper.setRawData({data.data(), data.size()}); + params.setRawAppData(data.data(), data.size()); // To get correct size information, the SP length field needs to be updated automatically REQUIRE(creator.getSerializedSize() == 13); creator.updateSpLengthField(); diff --git a/unittests/tmtcservices/testStoreAndSendHelper.cpp b/unittests/tmtcservices/testStoreAndSendHelper.cpp index 2731b28f..46418dfe 100644 --- a/unittests/tmtcservices/testStoreAndSendHelper.cpp +++ b/unittests/tmtcservices/testStoreAndSendHelper.cpp @@ -44,9 +44,7 @@ TEST_CASE("TM Store And Send Helper", "[tm-store-send-helper]") { REQUIRE(creator.getSubService() == 2); REQUIRE(creator.getService() == 17); auto& params = creator.getParams(); - REQUIRE(params.dataWrapper.type == util::DataTypes::RAW); - REQUIRE(params.dataWrapper.dataUnion.raw.data == nullptr); - REQUIRE(params.dataWrapper.dataUnion.raw.len == 0); + REQUIRE(params.sourceData == nullptr); REQUIRE(tmHelper.sendCounter == 0); REQUIRE(tmHelper.storeAndSendTmPacket() == returnvalue::OK); REQUIRE(tmHelper.sendCounter == 1); @@ -65,9 +63,9 @@ TEST_CASE("TM Store And Send Helper", "[tm-store-send-helper]") { REQUIRE(tmHelper.prepareTmPacket(2, data.data(), data.size()) == returnvalue::OK); auto& creator = storeHelper.getCreatorRef(); auto& params = creator.getParams(); - REQUIRE(params.dataWrapper.type == util::DataTypes::RAW); - REQUIRE(params.dataWrapper.dataUnion.raw.data == data.data()); - REQUIRE(params.dataWrapper.dataUnion.raw.len == data.size()); + REQUIRE(params.sourceData != nullptr); + REQUIRE(params.sourceData->getSerializedSize() == data.size()); + REQUIRE(params.adapter.getConstBuffer() == data.data()); } SECTION("Serializable Helper") { @@ -75,8 +73,7 @@ TEST_CASE("TM Store And Send Helper", "[tm-store-send-helper]") { REQUIRE(tmHelper.prepareTmPacket(2, simpleSer) == returnvalue::OK); auto& creator = storeHelper.getCreatorRef(); auto& params = creator.getParams(); - REQUIRE(params.dataWrapper.type == util::DataTypes::SERIALIZABLE); - REQUIRE(params.dataWrapper.dataUnion.serializable == &simpleSer); + REQUIRE(params.sourceData == &simpleSer); } SECTION("Object ID prefix Helper") { @@ -86,8 +83,7 @@ TEST_CASE("TM Store And Send Helper", "[tm-store-send-helper]") { REQUIRE(tmHelper.prepareTmPacket(2, dataWithObjId) == returnvalue::OK); auto& creator = storeHelper.getCreatorRef(); auto& params = creator.getParams(); - REQUIRE(params.dataWrapper.type == util::DataTypes::SERIALIZABLE); - REQUIRE(params.dataWrapper.dataUnion.serializable == &dataWithObjId); + REQUIRE(params.sourceData == &dataWithObjId); } // TODO: Error handling