Simple SerializeIF adaptions

- Returns serialized or deserialized size
This commit is contained in:
Robin Müller 2022-07-27 21:29:05 +02:00
parent 5bb7023ff3
commit b485afea57
No known key found for this signature in database
GPG Key ID: 71B58F8A3CDFA9AC
9 changed files with 101 additions and 47 deletions

View File

@ -125,22 +125,26 @@ class SerializeIF {
* @param streamEndianness
* @return
*/
[[nodiscard]] virtual ReturnValue_t serialize(uint8_t *buffer, size_t maxSize,
[[nodiscard]] virtual ReturnValue_t serialize(uint8_t *buffer, size_t &serLen, size_t maxSize,
Endianness streamEndianness) const {
size_t tmpSize = 0;
return serialize(&buffer, &tmpSize, maxSize, streamEndianness);
ReturnValue_t result = serialize(&buffer, &tmpSize, maxSize, streamEndianness);
serLen = tmpSize;
return result;
}
/**
* Forwards to regular @serialize call with big (network) endianness
*/
[[nodiscard]] virtual ReturnValue_t serializeBe(uint8_t *buffer, size_t maxSize) const {
return serialize(buffer, maxSize, SerializeIF::Endianness::NETWORK);
[[nodiscard]] virtual ReturnValue_t serializeBe(uint8_t *buffer, size_t &serLen,
size_t maxSize) const {
return serialize(buffer, serLen, maxSize, SerializeIF::Endianness::NETWORK);
}
/**
* If endianness is not explicitly specified, use machine endianness
*/
[[nodiscard]] virtual ReturnValue_t serialize(uint8_t *buffer, size_t maxSize) const {
return serialize(buffer, maxSize, SerializeIF::Endianness::MACHINE);
[[nodiscard]] virtual ReturnValue_t serialize(uint8_t *buffer, size_t &serLen,
size_t maxSize) const {
return serialize(buffer, serLen, maxSize, SerializeIF::Endianness::MACHINE);
}
/**
@ -151,21 +155,24 @@ class SerializeIF {
* @param streamEndianness
* @return
*/
virtual ReturnValue_t deSerialize(const uint8_t *buffer, size_t maxSize,
virtual ReturnValue_t deSerialize(const uint8_t *buffer, size_t &deserSize, size_t maxSize,
Endianness streamEndianness) {
return deSerialize(&buffer, &maxSize, streamEndianness);
size_t deserLen = maxSize;
ReturnValue_t result = deSerialize(&buffer, &deserLen, streamEndianness);
deserSize = maxSize - deserLen;
return result;
}
/**
* Forwards to regular @serialize call with big (network) endianness
*/
virtual ReturnValue_t deSerializeBe(const uint8_t *buffer, size_t maxSize) {
return deSerialize(buffer, maxSize, SerializeIF::Endianness::NETWORK);
virtual ReturnValue_t deSerializeBe(const uint8_t *buffer, size_t &deserSize, size_t maxSize) {
return deSerialize(buffer, deserSize, maxSize, SerializeIF::Endianness::NETWORK);
}
/**
* If endianness is not explicitly specified, use machine endianness
*/
virtual ReturnValue_t deSerialize(const uint8_t *buffer, size_t maxSize) {
return deSerialize(buffer, maxSize, SerializeIF::Endianness::MACHINE);
virtual ReturnValue_t deSerialize(const uint8_t *buffer, size_t &deserSize, size_t maxSize) {
return deSerialize(buffer, deserSize, maxSize, SerializeIF::Endianness::MACHINE);
}
};

View File

@ -108,6 +108,6 @@ ReturnValue_t PusTcCreator::serialize(uint8_t **buffer, size_t *size, size_t max
return serialize(buffer, size, maxSize, SerializeIF::Endianness::NETWORK);
}
ReturnValue_t PusTcCreator::serialize(uint8_t *buffer, size_t maxSize) const {
return SerializeIF::serialize(buffer, maxSize, SerializeIF::Endianness::NETWORK);
ReturnValue_t PusTcCreator::serialize(uint8_t *buffer, size_t &serLen, size_t maxSize) const {
return SerializeIF::serialize(buffer, serLen, maxSize, SerializeIF::Endianness::NETWORK);
}

View File

@ -35,7 +35,7 @@ class PusTcCreator : public PusTcIF, public SerializeIF, public CustomUserDataIF
SpacePacketParams &getSpParams();
ReturnValue_t serialize(uint8_t **buffer, size_t *size, size_t maxSize) const override;
ReturnValue_t serialize(uint8_t *buffer, size_t maxSize) const override;
ReturnValue_t serialize(uint8_t *buffer, size_t &serLen, size_t maxSize) const override;
[[nodiscard]] size_t getSerializedSize() const override;
[[nodiscard]] uint16_t getPacketIdRaw() const override;

View File

@ -94,6 +94,9 @@ ReturnValue_t PusTmCreator::serialize(uint8_t** buffer, size_t* size, size_t max
uint16_t crc16 = CRC::crc16ccitt(start, getFullPacketLen() - sizeof(ecss::PusChecksumT));
return SerializeAdapter::serialize(&crc16, buffer, size, maxSize, streamEndianness);
}
// Even if no CRC is calculated, account for the space taken by it
*size += 2;
*buffer += 2;
return HasReturnvaluesIF::RETURN_OK;
}
@ -151,6 +154,6 @@ ReturnValue_t PusTmCreator::serialize(uint8_t** buffer, size_t* size, size_t max
return serialize(buffer, size, maxSize, SerializeIF::Endianness::NETWORK);
}
ReturnValue_t PusTmCreator::serialize(uint8_t* buffer, size_t maxSize) const {
return SerializeIF::serialize(buffer, maxSize, SerializeIF::Endianness::NETWORK);
ReturnValue_t PusTmCreator::serialize(uint8_t* buffer, size_t& serLen, size_t maxSize) const {
return SerializeIF::serialize(buffer, serLen, maxSize, SerializeIF::Endianness::NETWORK);
}

View File

@ -74,7 +74,7 @@ class PusTmCreator : public SerializeIF, public PusTmIF, public CustomUserDataIF
uint16_t getDestId() override;
// Override these to use default big endianness
ReturnValue_t serialize(uint8_t** buffer, size_t* size, size_t maxSize) const override;
ReturnValue_t serialize(uint8_t* buffer, size_t maxSize) const override;
ReturnValue_t serialize(uint8_t* buffer, size_t& serLen, size_t maxSize) const override;
ReturnValue_t serialize(uint8_t** buffer, size_t* size, size_t maxSize,
Endianness streamEndianness) const override;
[[nodiscard]] size_t getSerializedSize() const override;

View File

@ -24,12 +24,14 @@ TEST_CASE("Serialize IF Serialize", "[serialize-if-ser]") {
}
SECTION("Little Endian Simple") {
REQUIRE(
simpleSer.SerializeIF::serialize(buf.data(), buf.size(), SerializeIF::Endianness::LITTLE) ==
HasReturnvaluesIF::RETURN_OK);
size_t serLen = 0xff;
REQUIRE(simpleSer.SerializeIF::serialize(buf.data(), serLen, buf.size(),
SerializeIF::Endianness::LITTLE) ==
HasReturnvaluesIF::RETURN_OK);
CHECK(buf[0] == 1);
CHECK(buf[1] == 3);
CHECK(buf[2] == 2);
CHECK(serLen == 3);
}
SECTION("Big Endian Normal") {
@ -54,23 +56,25 @@ TEST_CASE("Serialize IF Serialize", "[serialize-if-ser]") {
}
SECTION("Big Endian Simple") {
size_t serLen = 0xff;
SECTION("Explicit") {
REQUIRE(
simpleSer.SerializeIF::serialize(buf.data(), buf.size(), SerializeIF::Endianness::BIG) ==
HasReturnvaluesIF::RETURN_OK);
REQUIRE(simpleSer.SerializeIF::serialize(buf.data(), serLen, buf.size(),
SerializeIF::Endianness::BIG) ==
HasReturnvaluesIF::RETURN_OK);
}
SECTION("Network 0") {
REQUIRE(simpleSer.SerializeIF::serialize(buf.data(), buf.size(),
REQUIRE(simpleSer.SerializeIF::serialize(buf.data(), serLen, buf.size(),
SerializeIF::Endianness::NETWORK) ==
HasReturnvaluesIF::RETURN_OK);
}
SECTION("Network 1") {
REQUIRE(simpleSer.SerializeIF::serializeBe(buf.data(), buf.size()) ==
REQUIRE(simpleSer.SerializeIF::serializeBe(buf.data(), serLen, buf.size()) ==
HasReturnvaluesIF::RETURN_OK);
}
CHECK(buf[0] == 1);
CHECK(buf[1] == 2);
CHECK(buf[2] == 3);
CHECK(serLen == 3);
}
SECTION("Machine Endian Implicit") {
@ -90,7 +94,8 @@ TEST_CASE("Serialize IF Serialize", "[serialize-if-ser]") {
}
SECTION("Machine Endian Simple Implicit") {
REQUIRE(simpleSer.SerializeIF::serialize(buf.data(), buf.size()) ==
size_t serLen = 0xff;
REQUIRE(simpleSer.SerializeIF::serialize(buf.data(), serLen, buf.size()) ==
HasReturnvaluesIF::RETURN_OK);
CHECK(buf[0] == 1);
#if BYTE_ORDER_SYSTEM == LITTLE_ENDIAN
@ -100,6 +105,7 @@ TEST_CASE("Serialize IF Serialize", "[serialize-if-ser]") {
CHECK(buf[1] == 2);
CHECK(buf[2] == 3);
#endif
CHECK(serLen == 3);
}
}
@ -119,10 +125,13 @@ TEST_CASE("SerializeIF Deserialize", "[serialize-if-de]") {
}
SECTION("Little Endian Simple") {
REQUIRE(simpleSer.SerializeIF::deSerialize(ptr, len, SerializeIF::Endianness::LITTLE) ==
HasReturnvaluesIF::RETURN_OK);
size_t deserLen = 0xff;
REQUIRE(
simpleSer.SerializeIF::deSerialize(ptr, deserLen, len, SerializeIF::Endianness::LITTLE) ==
HasReturnvaluesIF::RETURN_OK);
CHECK(simpleSer.getU8() == 5);
CHECK(simpleSer.getU16() == 0x0100);
CHECK(deserLen == 3);
}
SECTION("Big Endian Normal") {
@ -144,22 +153,24 @@ TEST_CASE("SerializeIF Deserialize", "[serialize-if-de]") {
}
SECTION("Big Endian Simple") {
size_t deserLen = 0xff;
SECTION("Explicit") {
REQUIRE(simpleSer.SerializeIF::deSerialize(buf.data(), buf.size(),
REQUIRE(simpleSer.SerializeIF::deSerialize(buf.data(), deserLen, buf.size(),
SerializeIF::Endianness::BIG) ==
HasReturnvaluesIF::RETURN_OK);
}
SECTION("Network 0") {
REQUIRE(simpleSer.SerializeIF::deSerialize(buf.data(), buf.size(),
REQUIRE(simpleSer.SerializeIF::deSerialize(buf.data(), deserLen, buf.size(),
SerializeIF::Endianness::NETWORK) ==
HasReturnvaluesIF::RETURN_OK);
}
SECTION("Network 1") {
REQUIRE(simpleSer.SerializeIF::deSerializeBe(buf.data(), buf.size()) ==
REQUIRE(simpleSer.SerializeIF::deSerializeBe(buf.data(), deserLen, buf.size()) ==
HasReturnvaluesIF::RETURN_OK);
}
CHECK(simpleSer.getU8() == 5);
CHECK(simpleSer.getU16() == 1);
CHECK(deserLen == 3);
}
SECTION("Machine Endian Implicit") {
@ -176,7 +187,8 @@ TEST_CASE("SerializeIF Deserialize", "[serialize-if-de]") {
}
SECTION("Machine Endian Simple Implicit") {
REQUIRE(simpleSer.SerializeIF::deSerialize(buf.data(), buf.size()) ==
size_t deserLen = 0xff;
REQUIRE(simpleSer.SerializeIF::deSerialize(buf.data(), deserLen, buf.size()) ==
HasReturnvaluesIF::RETURN_OK);
CHECK(simpleSer.getU8() == 5);
#if BYTE_ORDER_SYSTEM == LITTLE_ENDIAN
@ -184,5 +196,6 @@ TEST_CASE("SerializeIF Deserialize", "[serialize-if-de]") {
#else
CHECK(simpleSer.getU16() == 1);
#endif
CHECK(deserLen == 3);
}
}

View File

@ -42,21 +42,28 @@ TEST_CASE("CCSDS Packet ID", "[ccsds-packet-id]") {
packetId.apid = 0x1ff;
packetId.secHeaderFlag = false;
packetId.packetType = ccsds::PacketType::TM;
size_t serLen = 0;
REQUIRE(packetId.raw() == 0x1ff);
REQUIRE(packetId.SerializeIF::serializeBe(buf.data(), buf.size()) ==
REQUIRE(packetId.SerializeIF::serializeBe(buf.data(), serLen, buf.size()) ==
HasReturnvaluesIF::RETURN_OK);
CHECK(buf[0] == 0x1);
CHECK(buf[1] == 0xff);
}
SECTION("Invalid Ser") {
REQUIRE(packetId.SerializeIF::serializeBe(buf.data(), 0) == SerializeIF::BUFFER_TOO_SHORT);
REQUIRE(packetId.SerializeIF::serializeBe(buf.data(), 1) == SerializeIF::BUFFER_TOO_SHORT);
size_t serLen = 0;
REQUIRE(packetId.SerializeIF::serializeBe(buf.data(), serLen, 0) ==
SerializeIF::BUFFER_TOO_SHORT);
REQUIRE(packetId.SerializeIF::serializeBe(buf.data(), serLen, 1) ==
SerializeIF::BUFFER_TOO_SHORT);
}
SECTION("Invalid Deser") {
REQUIRE(packetId.SerializeIF::deSerialize(buf.data(), 1) == SerializeIF::STREAM_TOO_SHORT);
REQUIRE(packetId.SerializeIF::deSerialize(buf.data(), 0) == SerializeIF::STREAM_TOO_SHORT);
size_t deserLen = 0;
REQUIRE(packetId.SerializeIF::deSerialize(buf.data(), deserLen, 1) ==
SerializeIF::STREAM_TOO_SHORT);
REQUIRE(packetId.SerializeIF::deSerialize(buf.data(), deserLen, 0) ==
SerializeIF::STREAM_TOO_SHORT);
}
SECTION("From Raw") {
@ -69,9 +76,11 @@ TEST_CASE("CCSDS Packet ID", "[ccsds-packet-id]") {
SECTION("Deserialize") {
buf[0] = 0x1a;
buf[1] = 0xff;
REQUIRE(packetId.SerializeIF::deSerialize(buf.data(), buf.size()) ==
size_t deserLen = 0xff;
REQUIRE(packetId.SerializeIF::deSerialize(buf.data(), deserLen, buf.size()) ==
HasReturnvaluesIF::RETURN_OK);
CHECK(packetId.apid == 0x2ff);
CHECK(deserLen == 2);
CHECK(packetId.packetType == ccsds::PacketType::TC);
CHECK(packetId.secHeaderFlag == true);
}
@ -81,12 +90,15 @@ TEST_CASE("CCSDS Packet Seq Ctrl", "[ccsds-packet-seq-ctrl]") {
PacketSeqCtrl psc;
std::array<uint8_t, 3> buf{};
SECTION("Basic") {
size_t serLen = 0xff;
psc.seqFlags = ccsds::SequenceFlags::FIRST_SEGMENT;
psc.seqCount = static_cast<uint16_t>(std::round(std::pow(2, 14) - 1));
REQUIRE(psc.raw() == 0x7fff);
REQUIRE(psc.SerializeIF::serializeBe(buf.data(), buf.size()) == HasReturnvaluesIF::RETURN_OK);
REQUIRE(psc.SerializeIF::serializeBe(buf.data(), serLen, buf.size()) ==
HasReturnvaluesIF::RETURN_OK);
CHECK(buf[0] == 0x7f);
CHECK(buf[1] == 0xff);
CHECK(serLen == 2);
}
SECTION("From Raw") {
@ -100,18 +112,23 @@ TEST_CASE("CCSDS Packet Seq Ctrl", "[ccsds-packet-seq-ctrl]") {
SECTION("Deserialize") {
buf[0] = 0xbf;
buf[1] = 0xfe;
REQUIRE(psc.SerializeIF::deSerialize(buf.data(), buf.size()) == HasReturnvaluesIF::RETURN_OK);
size_t deserLen = 0xff;
REQUIRE(psc.SerializeIF::deSerialize(buf.data(), deserLen, buf.size()) ==
HasReturnvaluesIF::RETURN_OK);
CHECK(psc.seqFlags == ccsds::SequenceFlags::LAST_SEGMENT);
CHECK(deserLen == 2);
CHECK(psc.seqCount == static_cast<uint16_t>(std::round(std::pow(2, 14) - 2)));
}
SECTION("Invalid Ser") {
REQUIRE(psc.SerializeIF::serializeBe(buf.data(), 0) == SerializeIF::BUFFER_TOO_SHORT);
REQUIRE(psc.SerializeIF::serializeBe(buf.data(), 1) == SerializeIF::BUFFER_TOO_SHORT);
size_t deserLen = 0;
REQUIRE(psc.SerializeIF::serializeBe(buf.data(), deserLen, 0) == SerializeIF::BUFFER_TOO_SHORT);
REQUIRE(psc.SerializeIF::serializeBe(buf.data(), deserLen, 1) == SerializeIF::BUFFER_TOO_SHORT);
}
SECTION("Invalid Deser") {
REQUIRE(psc.SerializeIF::deSerialize(buf.data(), 1) == SerializeIF::STREAM_TOO_SHORT);
REQUIRE(psc.SerializeIF::deSerialize(buf.data(), 0) == SerializeIF::STREAM_TOO_SHORT);
size_t deserLen = 0;
REQUIRE(psc.SerializeIF::deSerialize(buf.data(), deserLen, 1) == SerializeIF::STREAM_TOO_SHORT);
REQUIRE(psc.SerializeIF::deSerialize(buf.data(), deserLen, 0) == SerializeIF::STREAM_TOO_SHORT);
}
}

View File

@ -94,6 +94,18 @@ TEST_CASE("PUS TC Creator", "[pus-tc-creator]") {
REQUIRE(buf[13] == 3);
}
SECTION("Test with Application Data Serializable Simple Ser API") {
auto& params = creator.getPusParams();
auto simpleSer = SimpleSerializable();
creator.setSerializableUserData(simpleSer);
REQUIRE(creator.getSerializedSize() == 16);
REQUIRE(creator.serialize(dataPtr, serLen, buf.size()) == HasReturnvaluesIF::RETURN_OK);
REQUIRE(serLen == 16);
REQUIRE(buf[11] == 1);
REQUIRE(buf[12] == 2);
REQUIRE(buf[13] == 3);
}
SECTION("Deserialization Fails") {
SerializeIF& deser = creator;
size_t deserLen = buf.size();

View File

@ -57,7 +57,8 @@ TEST_CASE("Pus Service Base", "[pus-service-base]") {
REQUIRE(psb.performServiceCallCnt == 0);
uint8_t* dataPtr;
REQUIRE(pool.getFreeElement(&storeId, creator.getSerializedSize(), &dataPtr) == retval::OK);
REQUIRE(creator.serializeBe(dataPtr, creator.getSerializedSize()) == retval::OK);
size_t serLen = 0;
REQUIRE(creator.serializeBe(dataPtr, serLen, creator.getSerializedSize()) == retval::OK);
tmtcMsg.setStorageId(storeId);
msgQueue.addReceivedMessage(tmtcMsg);
REQUIRE(psb.performOperation(0) == retval::OK);
@ -77,7 +78,8 @@ TEST_CASE("Pus Service Base", "[pus-service-base]") {
SECTION("Send Request with Failed Handling") {
uint8_t* dataPtr;
REQUIRE(pool.getFreeElement(&storeId, creator.getSerializedSize(), &dataPtr) == retval::OK);
REQUIRE(creator.serializeBe(dataPtr, creator.getSerializedSize()) == retval::OK);
size_t serLen = 0;
REQUIRE(creator.serializeBe(dataPtr, serLen, creator.getSerializedSize()) == retval::OK);
tmtcMsg.setStorageId(storeId);
msgQueue.addReceivedMessage(tmtcMsg);
psb.makeNextHandleReqCallFail(3);