Compare commits
8 Commits
mueller/fs
...
mueller/da
Author | SHA1 | Date | |
---|---|---|---|
20f0707813 | |||
8d1777fa0c | |||
d675a789a2 | |||
192255df1c | |||
bdd79d060d | |||
c756297e5c | |||
0f27c7e7e7 | |||
20d42add03 |
@ -8,6 +8,8 @@ and this project adheres to [Semantic Versioning](http://semver.org/).
|
|||||||
|
|
||||||
# [unreleased]
|
# [unreleased]
|
||||||
|
|
||||||
|
# [v6.0.0]
|
||||||
|
|
||||||
## Changes
|
## Changes
|
||||||
|
|
||||||
- Removed `HasReturnvaluesIF` class in favor of `returnvalue` namespace with `OK` and `FAILED`
|
- Removed `HasReturnvaluesIF` class in favor of `returnvalue` namespace with `OK` and `FAILED`
|
||||||
@ -16,6 +18,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
|
||||||
|
|
||||||
|
76
src/fsfw/util/dataWrapper.h
Normal file
76
src/fsfw/util/dataWrapper.h
Normal file
@ -0,0 +1,76 @@
|
|||||||
|
#ifndef FSFW_UTIL_DATAWRAPPER_H
|
||||||
|
#define FSFW_UTIL_DATAWRAPPER_H
|
||||||
|
|
||||||
|
#include <cstddef>
|
||||||
|
#include <cstdint>
|
||||||
|
#include <utility>
|
||||||
|
|
||||||
|
#include "fsfw/serialize.h"
|
||||||
|
|
||||||
|
namespace util {
|
||||||
|
|
||||||
|
using BufPair = std::pair<const uint8_t*, size_t>;
|
||||||
|
|
||||||
|
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 {
|
||||||
|
|
||||||
|
DataWrapper() = default;
|
||||||
|
|
||||||
|
DataWrapper(const uint8_t* data, size_t size): type(DataTypes::RAW) {
|
||||||
|
setRawData({data, size});
|
||||||
|
}
|
||||||
|
|
||||||
|
explicit DataWrapper(BufPair raw): type(DataTypes::RAW) {
|
||||||
|
setRawData(raw);
|
||||||
|
}
|
||||||
|
|
||||||
|
explicit DataWrapper(SerializeIF& serializable): type(DataTypes::SERIALIZABLE) {
|
||||||
|
setSerializable(serializable);
|
||||||
|
}
|
||||||
|
|
||||||
|
DataTypes type = DataTypes::NONE;
|
||||||
|
DataUnion dataUnion;
|
||||||
|
|
||||||
|
[[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(BufPair 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
|
@ -1,3 +1,4 @@
|
|||||||
target_sources(${FSFW_TEST_TGT} PRIVATE
|
target_sources(${FSFW_TEST_TGT} PRIVATE
|
||||||
testUnsignedByteField.cpp
|
testUnsignedByteField.cpp
|
||||||
|
testDataWrapper.cpp
|
||||||
)
|
)
|
||||||
|
59
unittests/util/testDataWrapper.cpp
Normal file
59
unittests/util/testDataWrapper.cpp
Normal file
@ -0,0 +1,59 @@
|
|||||||
|
#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") {
|
||||||
|
util::DataWrapper* instance = &wrapper;
|
||||||
|
bool deleteInst = false;
|
||||||
|
REQUIRE(wrapper.isNull());
|
||||||
|
std::array<uint8_t, 4> data = {1, 2, 3, 4};
|
||||||
|
SECTION("Setter") {
|
||||||
|
wrapper.setRawData({data.data(), data.size()});
|
||||||
|
}
|
||||||
|
SECTION("Direct Construction Pair") {
|
||||||
|
instance = new util::DataWrapper(util::BufPair(data.data(), data.size()));
|
||||||
|
deleteInst = true;
|
||||||
|
}
|
||||||
|
SECTION("Direct Construction Single Args") {
|
||||||
|
instance = new util::DataWrapper(data.data(), data.size());
|
||||||
|
deleteInst = true;
|
||||||
|
}
|
||||||
|
REQUIRE(not instance->isNull());
|
||||||
|
REQUIRE(instance->type == util::DataTypes::RAW);
|
||||||
|
REQUIRE(instance->dataUnion.raw.data == data.data());
|
||||||
|
REQUIRE(instance->dataUnion.raw.len == data.size());
|
||||||
|
if(deleteInst) {
|
||||||
|
delete instance;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
SECTION("Simple Serializable") {
|
||||||
|
util::DataWrapper* instance = &wrapper;
|
||||||
|
bool deleteInst = false;
|
||||||
|
REQUIRE(instance->isNull());
|
||||||
|
SimpleSerializable serializable;
|
||||||
|
SECTION("Setter") {
|
||||||
|
wrapper.setSerializable(serializable);
|
||||||
|
}
|
||||||
|
SECTION("Direct Construction") {
|
||||||
|
instance = new util::DataWrapper(serializable);
|
||||||
|
deleteInst = true;
|
||||||
|
}
|
||||||
|
|
||||||
|
REQUIRE(not instance->isNull());
|
||||||
|
REQUIRE(instance->type == util::DataTypes::SERIALIZABLE);
|
||||||
|
REQUIRE(instance->dataUnion.serializable == &serializable);
|
||||||
|
if(deleteInst) {
|
||||||
|
delete instance;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
@ -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);
|
||||||
|
Reference in New Issue
Block a user