diff --git a/CHANGELOG.md b/CHANGELOG.md index 9d786430..03e3189a 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -12,6 +12,10 @@ and this project adheres to [Semantic Versioning](http://semver.org/). ## Added +- Add `util::DataWrapper` class inside the `util` module. This is a tagged union which allows + to specify raw data either as a classic C-style raw pointer and size or as a `SerializeIF` + pointer. + PR: https://egit.irs.uni-stuttgart.de/fsfw/fsfw/pulls/668 - Add new `UnsignedByteField` class PR: https://egit.irs.uni-stuttgart.de/fsfw/fsfw/pulls/660 diff --git a/src/fsfw/pus/Service11TelecommandScheduling.tpp b/src/fsfw/pus/Service11TelecommandScheduling.tpp index 47135155..dfb6ea92 100644 --- a/src/fsfw/pus/Service11TelecommandScheduling.tpp +++ b/src/fsfw/pus/Service11TelecommandScheduling.tpp @@ -581,8 +581,7 @@ inline ReturnValue_t Service11TelecommandScheduling::getMapFilterFr } // additional security check, this should never be true - if ((itBegin != telecommandMap.end() and itEnd != telecommandMap.end()) and - (itBegin->first > itEnd->first)) { + if (itBegin > itEnd) { #if FSFW_CPP_OSTREAM_ENABLED == 1 sif::error << "11::getMapFilterFromData: itBegin > itEnd\n" << std::endl; #else diff --git a/src/fsfw/util/dataWrapper.h b/src/fsfw/util/dataWrapper.h new file mode 100644 index 00000000..440f9501 --- /dev/null +++ b/src/fsfw/util/dataWrapper.h @@ -0,0 +1,60 @@ +#ifndef FSFW_UTIL_DATAWRAPPER_H +#define FSFW_UTIL_DATAWRAPPER_H + +#include +#include +#include + +#include "fsfw/serialize.h" + +namespace util { + +struct RawData { + RawData() = default; + const uint8_t* data = nullptr; + size_t len = 0; +}; + +enum DataTypes { NONE, RAW, SERIALIZABLE }; + +union DataUnion { + RawData raw{}; + SerializeIF* serializable; +}; + +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/util/CMakeLists.txt b/unittests/util/CMakeLists.txt index d4caa4d5..dd9f6dc1 100644 --- a/unittests/util/CMakeLists.txt +++ b/unittests/util/CMakeLists.txt @@ -1,3 +1,4 @@ target_sources(${FSFW_TEST_TGT} PRIVATE testUnsignedByteField.cpp + testDataWrapper.cpp ) diff --git a/unittests/util/testDataWrapper.cpp b/unittests/util/testDataWrapper.cpp new file mode 100644 index 00000000..14ba287a --- /dev/null +++ b/unittests/util/testDataWrapper.cpp @@ -0,0 +1,32 @@ +#include +#include + +#include "fsfw/util/dataWrapper.h" +#include "mocks/SimpleSerializable.h" + +TEST_CASE("Data Wrapper", "[util]") { + util::DataWrapper wrapper; + SECTION("State") { + REQUIRE(wrapper.isNull()); + REQUIRE(wrapper.type == util::DataTypes::NONE); + } + + SECTION("Set Raw Data") { + REQUIRE(wrapper.isNull()); + std::array data = {1, 2, 3, 4}; + wrapper.setRawData({data.data(), data.size()}); + REQUIRE(not wrapper.isNull()); + REQUIRE(wrapper.type == util::DataTypes::RAW); + REQUIRE(wrapper.dataUnion.raw.data == data.data()); + REQUIRE(wrapper.dataUnion.raw.len == data.size()); + } + + SECTION("Simple Serializable") { + REQUIRE(wrapper.isNull()); + SimpleSerializable serializable; + wrapper.setSerializable(serializable); + REQUIRE(not wrapper.isNull()); + REQUIRE(wrapper.type == util::DataTypes::SERIALIZABLE); + REQUIRE(wrapper.dataUnion.serializable == &serializable); + } +} \ No newline at end of file diff --git a/unittests/util/testUnsignedByteField.cpp b/unittests/util/testUnsignedByteField.cpp index df1ff483..58a87a7d 100644 --- a/unittests/util/testUnsignedByteField.cpp +++ b/unittests/util/testUnsignedByteField.cpp @@ -4,7 +4,7 @@ #include "fsfw/util/UnsignedByteField.h" -TEST_CASE("Unsigned Byte Field", "[unsigned-byte-field]") { +TEST_CASE("Unsigned Byte Field", "[util]") { auto testByteField = UnsignedByteField(10); auto u32ByteField = U32ByteField(10); auto u16ByteField = U16ByteField(5);