Merge remote-tracking branch 'upstream/mueller/data-wrapper' into mueller/data-wrapper-update

This commit is contained in:
Robin Müller 2022-08-30 14:52:09 +02:00
commit d4ed528426
No known key found for this signature in database
GPG Key ID: 11D4952C8CCEF814
6 changed files with 99 additions and 3 deletions

View File

@ -12,6 +12,10 @@ and this project adheres to [Semantic Versioning](http://semver.org/).
## Added ## 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 - Add new `UnsignedByteField` class
PR: https://egit.irs.uni-stuttgart.de/fsfw/fsfw/pulls/660 PR: https://egit.irs.uni-stuttgart.de/fsfw/fsfw/pulls/660

View File

@ -581,8 +581,7 @@ inline ReturnValue_t Service11TelecommandScheduling<MAX_NUM_TCS>::getMapFilterFr
} }
// additional security check, this should never be true // additional security check, this should never be true
if ((itBegin != telecommandMap.end() and itEnd != telecommandMap.end()) and if (itBegin > itEnd) {
(itBegin->first > itEnd->first)) {
#if FSFW_CPP_OSTREAM_ENABLED == 1 #if FSFW_CPP_OSTREAM_ENABLED == 1
sif::error << "11::getMapFilterFromData: itBegin > itEnd\n" << std::endl; sif::error << "11::getMapFilterFromData: itBegin > itEnd\n" << std::endl;
#else #else

View File

@ -0,0 +1,60 @@
#ifndef FSFW_UTIL_DATAWRAPPER_H
#define FSFW_UTIL_DATAWRAPPER_H
#include <cstddef>
#include <cstdint>
#include <utility>
#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<const uint8_t*, size_t>;
[[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

View File

@ -1,3 +1,4 @@
target_sources(${FSFW_TEST_TGT} PRIVATE target_sources(${FSFW_TEST_TGT} PRIVATE
testUnsignedByteField.cpp testUnsignedByteField.cpp
testDataWrapper.cpp
) )

View File

@ -0,0 +1,32 @@
#include <array>
#include <catch2/catch_test_macros.hpp>
#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<uint8_t, 4> 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);
}
}

View File

@ -4,7 +4,7 @@
#include "fsfw/util/UnsignedByteField.h" #include "fsfw/util/UnsignedByteField.h"
TEST_CASE("Unsigned Byte Field", "[unsigned-byte-field]") { TEST_CASE("Unsigned Byte Field", "[util]") {
auto testByteField = UnsignedByteField<uint32_t>(10); auto testByteField = UnsignedByteField<uint32_t>(10);
auto u32ByteField = U32ByteField(10); auto u32ByteField = U32ByteField(10);
auto u16ByteField = U16ByteField(5); auto u16ByteField = U16ByteField(5);