Merge pull request 'New UnsignedByte Field class' (#660) from mueller/new-object-id-class into development
Reviewed-on: fsfw/fsfw#660
This commit is contained in:
commit
adcc375f25
@ -8,6 +8,10 @@ and this project adheres to [Semantic Versioning](http://semver.org/).
|
|||||||
|
|
||||||
# [unreleased]
|
# [unreleased]
|
||||||
|
|
||||||
|
## Added
|
||||||
|
|
||||||
|
- Add new `UnsignedByteField` class
|
||||||
|
|
||||||
# [v5.0.0] 25.07.2022
|
# [v5.0.0] 25.07.2022
|
||||||
|
|
||||||
## Changes
|
## Changes
|
||||||
|
@ -16,7 +16,7 @@
|
|||||||
* This is the typedef for object identifiers.
|
* This is the typedef for object identifiers.
|
||||||
* @ingroup system_objects
|
* @ingroup system_objects
|
||||||
*/
|
*/
|
||||||
typedef uint32_t object_id_t;
|
using object_id_t = uint32_t;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* This interface allows a class to be included in the object manager
|
* This interface allows a class to be included in the object manager
|
||||||
|
52
src/fsfw/util/UnsignedByteField.h
Normal file
52
src/fsfw/util/UnsignedByteField.h
Normal file
@ -0,0 +1,52 @@
|
|||||||
|
#ifndef FSFW_UTIL_UNSIGNEDBYTEFIELD_H
|
||||||
|
#define FSFW_UTIL_UNSIGNEDBYTEFIELD_H
|
||||||
|
|
||||||
|
#include "fsfw/serialize.h"
|
||||||
|
|
||||||
|
template<typename T>
|
||||||
|
class UnsignedByteField: public SerializeIF {
|
||||||
|
public:
|
||||||
|
static_assert(std::is_unsigned<T>::value);
|
||||||
|
|
||||||
|
explicit UnsignedByteField(T value): value(value) {}
|
||||||
|
[[nodiscard]] ReturnValue_t serialize(uint8_t **buffer, size_t *size, size_t maxSize,
|
||||||
|
Endianness streamEndianness) const override {
|
||||||
|
return SerializeAdapter::serialize(&value, buffer, size, maxSize, streamEndianness);
|
||||||
|
}
|
||||||
|
|
||||||
|
ReturnValue_t deSerialize(const uint8_t **buffer, size_t *size,
|
||||||
|
Endianness streamEndianness) override {
|
||||||
|
return SerializeAdapter::deSerialize(&value, buffer, size, streamEndianness);
|
||||||
|
}
|
||||||
|
|
||||||
|
[[nodiscard]] size_t getSerializedSize() const override {
|
||||||
|
return sizeof(T);
|
||||||
|
}
|
||||||
|
|
||||||
|
[[nodiscard]] T getValue() const {
|
||||||
|
return value;
|
||||||
|
}
|
||||||
|
|
||||||
|
void setValue(T value_) {
|
||||||
|
value = value_;
|
||||||
|
}
|
||||||
|
private:
|
||||||
|
T value;
|
||||||
|
};
|
||||||
|
|
||||||
|
class U32ByteField: public UnsignedByteField<uint32_t> {
|
||||||
|
public:
|
||||||
|
explicit U32ByteField(uint32_t value): UnsignedByteField<uint32_t>(value) {}
|
||||||
|
};
|
||||||
|
|
||||||
|
class U16ByteField: public UnsignedByteField<uint16_t> {
|
||||||
|
public:
|
||||||
|
explicit U16ByteField(uint16_t value): UnsignedByteField<uint16_t>(value) {}
|
||||||
|
};
|
||||||
|
|
||||||
|
class U8ByteField: public UnsignedByteField<uint8_t> {
|
||||||
|
public:
|
||||||
|
explicit U8ByteField(uint8_t value): UnsignedByteField<uint8_t>(value) {}
|
||||||
|
};
|
||||||
|
|
||||||
|
#endif // FSFW_UTIL_UNSIGNEDBYTEFIELD_H
|
@ -15,6 +15,7 @@ add_subdirectory(mocks)
|
|||||||
|
|
||||||
add_subdirectory(action)
|
add_subdirectory(action)
|
||||||
add_subdirectory(power)
|
add_subdirectory(power)
|
||||||
|
add_subdirectory(util)
|
||||||
add_subdirectory(container)
|
add_subdirectory(container)
|
||||||
add_subdirectory(osal)
|
add_subdirectory(osal)
|
||||||
add_subdirectory(serialize)
|
add_subdirectory(serialize)
|
||||||
|
3
unittests/util/CMakeLists.txt
Normal file
3
unittests/util/CMakeLists.txt
Normal file
@ -0,0 +1,3 @@
|
|||||||
|
target_sources(${FSFW_TEST_TGT} PRIVATE
|
||||||
|
testUnsignedByteField.cpp
|
||||||
|
)
|
74
unittests/util/testUnsignedByteField.cpp
Normal file
74
unittests/util/testUnsignedByteField.cpp
Normal file
@ -0,0 +1,74 @@
|
|||||||
|
|
||||||
|
#include <catch2/catch_test_macros.hpp>
|
||||||
|
|
||||||
|
#include "fsfw/util/UnsignedByteField.h"
|
||||||
|
|
||||||
|
#include <array>
|
||||||
|
|
||||||
|
TEST_CASE("Unsigned Byte Field", "[unsigned-byte-field]") {
|
||||||
|
auto testByteField = UnsignedByteField<uint32_t>(10);
|
||||||
|
auto u32ByteField = U32ByteField(10);
|
||||||
|
auto u16ByteField = U16ByteField(5);
|
||||||
|
auto u8ByteField = U8ByteField(2);
|
||||||
|
std::array<uint8_t, 16> buf{};
|
||||||
|
size_t serLen = 0;
|
||||||
|
SECTION("State") {
|
||||||
|
CHECK(testByteField.getValue() == 10);
|
||||||
|
CHECK(testByteField.getSerializedSize() == 4);
|
||||||
|
CHECK(u32ByteField.getValue() == 10);
|
||||||
|
CHECK(u32ByteField.getSerializedSize() == 4);
|
||||||
|
CHECK(u16ByteField.getValue() == 5);
|
||||||
|
CHECK(u8ByteField.getValue() == 2);
|
||||||
|
CHECK(u8ByteField.getSerializedSize() == 1);
|
||||||
|
}
|
||||||
|
|
||||||
|
SECTION("Setter") {
|
||||||
|
u32ByteField.setValue(20);
|
||||||
|
REQUIRE(u32ByteField.getValue() == 20);
|
||||||
|
}
|
||||||
|
|
||||||
|
SECTION("Serialize U32") {
|
||||||
|
CHECK(testByteField.serializeBe(buf.data(), serLen, buf.size()) == HasReturnvaluesIF::RETURN_OK);
|
||||||
|
CHECK(serLen == 4);
|
||||||
|
CHECK(buf[0] == 0);
|
||||||
|
CHECK(buf[3] == 10);
|
||||||
|
}
|
||||||
|
|
||||||
|
SECTION("Serialize U32 Concrete") {
|
||||||
|
CHECK(u32ByteField.serializeBe(buf.data(), serLen, buf.size()) == HasReturnvaluesIF::RETURN_OK);
|
||||||
|
CHECK(serLen == 4);
|
||||||
|
CHECK(buf[0] == 0);
|
||||||
|
CHECK(buf[3] == 10);
|
||||||
|
}
|
||||||
|
|
||||||
|
SECTION("Serialize U16 Concrete") {
|
||||||
|
CHECK(u16ByteField.serializeBe(buf.data(), serLen, buf.size()) == HasReturnvaluesIF::RETURN_OK);
|
||||||
|
CHECK(serLen == 2);
|
||||||
|
CHECK(buf[0] == 0);
|
||||||
|
CHECK(buf[1] == 5);
|
||||||
|
}
|
||||||
|
|
||||||
|
SECTION("Serialize U8 Concrete") {
|
||||||
|
CHECK(u8ByteField.serializeBe(buf.data(), serLen, buf.size()) == HasReturnvaluesIF::RETURN_OK);
|
||||||
|
CHECK(serLen == 1);
|
||||||
|
CHECK(buf[0] == 2);
|
||||||
|
}
|
||||||
|
|
||||||
|
SECTION("Deserialize") {
|
||||||
|
buf[0] = 0x50;
|
||||||
|
buf[1] = 0x40;
|
||||||
|
buf[2] = 0x30;
|
||||||
|
buf[3] = 0x20;
|
||||||
|
size_t deserLen = 0;
|
||||||
|
CHECK(testByteField.deSerializeBe(buf.data(), deserLen, buf.size()) == HasReturnvaluesIF::RETURN_OK);
|
||||||
|
CHECK(testByteField.getValue() == 0x50403020);
|
||||||
|
}
|
||||||
|
|
||||||
|
SECTION("Deserialize U16") {
|
||||||
|
buf[0] = 0x50;
|
||||||
|
buf[1] = 0x40;
|
||||||
|
size_t deserLen = 0;
|
||||||
|
CHECK(u16ByteField.deSerializeBe(buf.data(), deserLen, buf.size()) == HasReturnvaluesIF::RETURN_OK);
|
||||||
|
CHECK(u16ByteField.getValue() == 0x5040);
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user