refactor ctors

This commit is contained in:
Robin Müller 2022-09-05 15:30:53 +02:00
parent d64ad71529
commit f0b7a103d4
No known key found for this signature in database
GPG Key ID: 11D4952C8CCEF814
5 changed files with 39 additions and 28 deletions

View File

@ -125,6 +125,13 @@ void SerialBufferAdapter<count_t>::setBuffer(uint8_t* buffer, count_t bufferLeng
this->bufferLength = bufferLength; this->bufferLength = bufferLength;
} }
template <typename count_t>
void SerialBufferAdapter<count_t>::setConstBuffer(const uint8_t* buf, count_t bufLen) {
this->buffer = nullptr;
this->bufferLength = bufLen;
this->constBuffer = buf;
}
// forward Template declaration for linker // forward Template declaration for linker
template class SerialBufferAdapter<uint8_t>; template class SerialBufferAdapter<uint8_t>;
template class SerialBufferAdapter<uint16_t>; template class SerialBufferAdapter<uint16_t>;

View File

@ -21,6 +21,7 @@
template <typename count_t> template <typename count_t>
class SerialBufferAdapter : public SerializeIF { class SerialBufferAdapter : public SerializeIF {
public: public:
SerialBufferAdapter() = default;
/** /**
* Constructor for constant uint8_t buffer. Length field can be serialized optionally. * Constructor for constant uint8_t buffer. Length field can be serialized optionally.
* Type of length can be supplied as template type. * Type of length can be supplied as template type.
@ -40,12 +41,12 @@ class SerialBufferAdapter : public SerializeIF {
*/ */
SerialBufferAdapter(uint8_t* buffer, count_t bufferLength, bool serializeLength = false); SerialBufferAdapter(uint8_t* buffer, count_t bufferLength, bool serializeLength = false);
virtual ~SerialBufferAdapter(); ~SerialBufferAdapter() override;
virtual ReturnValue_t serialize(uint8_t** buffer, size_t* size, size_t maxSize, ReturnValue_t serialize(uint8_t** buffer, size_t* size, size_t maxSize,
Endianness streamEndianness) const override; Endianness streamEndianness) const override;
virtual size_t getSerializedSize() const override; [[nodiscard]] size_t getSerializedSize() const override;
/** /**
* @brief This function deserializes a buffer into the member buffer. * @brief This function deserializes a buffer into the member buffer.
@ -59,12 +60,13 @@ class SerialBufferAdapter : public SerializeIF {
* @param bigEndian * @param bigEndian
* @return * @return
*/ */
virtual ReturnValue_t deSerialize(const uint8_t** buffer, size_t* size, ReturnValue_t deSerialize(const uint8_t** buffer, size_t* size,
Endianness streamEndianness) override; Endianness streamEndianness) override;
uint8_t* getBuffer(); uint8_t* getBuffer();
const uint8_t* getConstBuffer() const; [[nodiscard]] const uint8_t* getConstBuffer() const;
void setBuffer(uint8_t* buffer, count_t bufferLength); void setBuffer(uint8_t* buffer, count_t bufferLength);
void setConstBuffer(const uint8_t* buf, count_t bufLen);
private: private:
bool serializeLength = false; bool serializeLength = false;

View File

@ -7,6 +7,6 @@ class CustomUserDataIF {
public: public:
virtual ~CustomUserDataIF() = default; virtual ~CustomUserDataIF() = default;
virtual ReturnValue_t setRawUserData(const uint8_t* data, size_t len) = 0; virtual ReturnValue_t setRawUserData(const uint8_t* data, size_t len) = 0;
virtual ReturnValue_t setSerializableUserData(SerializeIF& serializable) = 0; virtual ReturnValue_t setSerializableUserData(const SerializeIF& serializable) = 0;
}; };
#endif // FSFW_TMTCPACKET_CREATORDATAIF_H #endif // FSFW_TMTCPACKET_CREATORDATAIF_H

View File

@ -51,7 +51,7 @@ ReturnValue_t PusTmCreator::serialize(uint8_t** buffer, size_t* size, size_t max
if (result != returnvalue::OK) { if (result != returnvalue::OK) {
return result; return result;
} }
size_t userDataLen = pusParams.dataWrapper.getLength(); size_t userDataLen = pusParams.data->getSerializedSize();
**buffer = **buffer =
((pusParams.secHeader.pusVersion << 4) & 0xF0) | (pusParams.secHeader.scTimeRefStatus & 0x0F); ((pusParams.secHeader.pusVersion << 4) & 0xF0) | (pusParams.secHeader.scTimeRefStatus & 0x0F);
*buffer += 1; *buffer += 1;
@ -77,15 +77,8 @@ ReturnValue_t PusTmCreator::serialize(uint8_t** buffer, size_t* size, size_t max
} }
} }
if (pusParams.dataWrapper.type == util::DataTypes::RAW and if (pusParams.data != nullptr) {
pusParams.dataWrapper.dataUnion.raw.data != nullptr) { result = pusParams.data->serialize(buffer, size, maxSize, streamEndianness);
std::memcpy(*buffer, pusParams.dataWrapper.dataUnion.raw.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 (result != returnvalue::OK) { if (result != returnvalue::OK) {
return result; return result;
} }
@ -111,7 +104,7 @@ TimeWriterIF* PusTmCreator::getTimestamper() const { return pusParams.secHeader.
SpacePacketParams& PusTmCreator::getSpParams() { return spCreator.getParams(); } SpacePacketParams& PusTmCreator::getSpParams() { return spCreator.getParams(); }
void PusTmCreator::updateSpLengthField() { void PusTmCreator::updateSpLengthField() {
size_t headerLen = PusTmIF::MIN_SEC_HEADER_LEN + pusParams.dataWrapper.getLength() + size_t headerLen = PusTmIF::MIN_SEC_HEADER_LEN + pusParams.data->getSerializedSize() +
sizeof(ecss::PusChecksumT) - 1; sizeof(ecss::PusChecksumT) - 1;
if (pusParams.secHeader.timeStamper != nullptr) { if (pusParams.secHeader.timeStamper != nullptr) {
headerLen += pusParams.secHeader.timeStamper->getSerializedSize(); headerLen += pusParams.secHeader.timeStamper->getSerializedSize();
@ -134,12 +127,12 @@ void PusTmCreator::setMessageTypeCounter(uint16_t messageTypeCounter) {
void PusTmCreator::setDestId(uint16_t destId) { pusParams.secHeader.destId = destId; } void PusTmCreator::setDestId(uint16_t destId) { pusParams.secHeader.destId = destId; }
ReturnValue_t PusTmCreator::setRawUserData(const uint8_t* data, size_t len) { ReturnValue_t PusTmCreator::setRawUserData(const uint8_t* data, size_t len) {
pusParams.dataWrapper.setRawData({data, len}); pusParams.adapter.setConstBuffer(data, len);
updateSpLengthField(); updateSpLengthField();
return returnvalue::OK; return returnvalue::OK;
} }
ReturnValue_t PusTmCreator::setSerializableUserData(SerializeIF& serializable) { ReturnValue_t PusTmCreator::setSerializableUserData(const SerializeIF& serializable) {
pusParams.dataWrapper.setSerializable(serializable); pusParams.data = &serializable;
updateSpLengthField(); updateSpLengthField();
return returnvalue::OK; return returnvalue::OK;
} }

View File

@ -22,20 +22,29 @@ struct PusTmSecHeader {
struct PusTmParams { struct PusTmParams {
PusTmParams() = default; PusTmParams() = default;
explicit PusTmParams(PusTmSecHeader secHeader) : secHeader(secHeader){}; explicit PusTmParams(PusTmSecHeader secHeader) : secHeader(secHeader) {};
PusTmParams(PusTmSecHeader secHeader, util::DataWrapper dataWrapper) PusTmParams(PusTmSecHeader secHeader, const SerializeIF& data)
: secHeader(secHeader), dataWrapper(dataWrapper) {} : secHeader(secHeader), data(&data) {}
PusTmParams(PusTmSecHeader secHeader, const uint8_t* data, size_t dataLen)
: secHeader(secHeader), adapter(data, dataLen), data(&adapter) {
}
PusTmParams(uint8_t service, uint8_t subservice, TimeWriterIF* timeStamper) PusTmParams(uint8_t service, uint8_t subservice, TimeWriterIF* timeStamper)
: secHeader(service, subservice, timeStamper) {} : secHeader(service, subservice, timeStamper) {}
PusTmParams(uint8_t service, uint8_t subservice, TimeWriterIF* timeStamper, PusTmParams(uint8_t service, uint8_t subservice, TimeWriterIF* timeStamper,
util::DataWrapper dataWrapper_) const SerializeIF& data_)
: PusTmParams(service, subservice, timeStamper) { : PusTmParams(service, subservice, timeStamper) {
dataWrapper = dataWrapper_; data = &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) {}
PusTmSecHeader secHeader; PusTmSecHeader secHeader;
util::DataWrapper dataWrapper{}; SerialBufferAdapter<uint8_t> adapter;
const SerializeIF* data = nullptr;
}; };
class TimeWriterIF; class TimeWriterIF;
@ -88,7 +97,7 @@ class PusTmCreator : public SerializeIF, public PusTmIF, public CustomUserDataIF
[[nodiscard]] size_t getSerializedSize() const override; [[nodiscard]] size_t getSerializedSize() const override;
[[nodiscard]] TimeWriterIF* getTimestamper() const; [[nodiscard]] TimeWriterIF* getTimestamper() const;
ReturnValue_t setRawUserData(const uint8_t* data, size_t len) 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 (network endian) helpers into scope // Load all big endian (network endian) helpers into scope
using SerializeIF::serializeBe; using SerializeIF::serializeBe;