del data wrapper

This commit is contained in:
Robin Müller 2022-09-05 16:02:32 +02:00
parent f0b7a103d4
commit 75dc7a405d
No known key found for this signature in database
GPG Key ID: 11D4952C8CCEF814
9 changed files with 52 additions and 113 deletions

View File

@ -21,7 +21,7 @@ SerialBufferAdapter<count_t>::SerialBufferAdapter(uint8_t* buffer, count_t buffe
bufferLength(bufferLength) {}
template <typename count_t>
SerialBufferAdapter<count_t>::~SerialBufferAdapter() {}
SerialBufferAdapter<count_t>::~SerialBufferAdapter() = default;
template <typename count_t>
ReturnValue_t SerialBufferAdapter<count_t>::serialize(uint8_t** buffer, size_t* size,

View File

@ -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;

View File

@ -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;
}

View File

@ -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<uint8_t> 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;

View File

@ -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;
}

View File

@ -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<uint8_t> adapter;
const SerializeIF* data = nullptr;
const SerializeIF* sourceData = nullptr;
};
class TimeWriterIF;

View File

@ -1,60 +0,0 @@
#ifndef FSFW_UTIL_DATAWRAPPER_H
#define FSFW_UTIL_DATAWRAPPER_H
#include <cstddef>
#include <cstdint>
#include <utility>
#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<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

@ -71,7 +71,7 @@ TEST_CASE("PUS TC Creator", "[pus-tc-creator]") {
SECTION("Test with Application Data Raw") {
auto& params = creator.getPusParams();
std::array<uint8_t, 3> 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();

View File

@ -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