extended serialize if further

This commit is contained in:
Robin Müller 2022-07-23 10:24:56 +02:00
parent 9ccd9fd775
commit 55a238d553
No known key found for this signature in database
GPG Key ID: 71B58F8A3CDFA9AC
6 changed files with 46 additions and 15 deletions

View File

@ -62,6 +62,20 @@ class SerializeIF {
virtual ReturnValue_t serialize(uint8_t **buffer, size_t *size, size_t maxSize, virtual ReturnValue_t serialize(uint8_t **buffer, size_t *size, size_t maxSize,
Endianness streamEndianness) const = 0; Endianness streamEndianness) const = 0;
/**
* Forwards to regular @serialize call with network endianness
*/
virtual ReturnValue_t serializeNe(uint8_t** buffer, size_t* size, size_t maxSize) {
return serialize(buffer, size, maxSize, SerializeIF::Endianness::NETWORK);
}
/**
* If endianness is not explicitly specified, use machine endianness
*/
virtual ReturnValue_t serialize(uint8_t **buffer, size_t *size, size_t maxSize) {
return serialize(buffer, size, maxSize, SerializeIF::Endianness::MACHINE);
}
/** /**
* Gets the size of a object if it would be serialized in a buffer * Gets the size of a object if it would be serialized in a buffer
* @return Size of serialized object * @return Size of serialized object
@ -90,6 +104,18 @@ class SerializeIF {
*/ */
virtual ReturnValue_t deSerialize(const uint8_t **buffer, size_t *size, virtual ReturnValue_t deSerialize(const uint8_t **buffer, size_t *size,
Endianness streamEndianness) = 0; Endianness streamEndianness) = 0;
/**
* Forwards to regular @deSerialize call with network endianness
*/
virtual ReturnValue_t deSerializeNe(const uint8_t** buffer, size_t* size, size_t maxSize) {
return deSerialize(buffer, size, SerializeIF::Endianness::NETWORK);
}
/**
* If endianness is not explicitly specified, use machine endianness
*/
virtual ReturnValue_t deSerialize(const uint8_t** buffer, size_t *size, size_t maxSize) {
return deSerialize(buffer, size, SerializeIF::Endianness::MACHINE);
}
/** /**
* Helper method which can be used if serialization should be performed without any additional * Helper method which can be used if serialization should be performed without any additional

View File

@ -48,9 +48,6 @@ ReturnValue_t SpacePacketCreator::deSerialize(const uint8_t **buffer, size_t *si
} }
bool SpacePacketCreator::isValid() const { return valid; } bool SpacePacketCreator::isValid() const { return valid; }
ReturnValue_t SpacePacketCreator::serialize(uint8_t **buffer, size_t *size, size_t maxSize) const {
return serialize(buffer, size, maxSize, SerializeIF::Endianness::NETWORK);
}
void SpacePacketCreator::setApid(uint16_t apid) { void SpacePacketCreator::setApid(uint16_t apid) {
if (apid < ccsds::LIMIT_APID) { if (apid < ccsds::LIMIT_APID) {
params.packetId.apid = apid; params.packetId.apid = apid;
@ -83,3 +80,4 @@ SpacePacketParams &SpacePacketCreator::getParams() { return params; }
void SpacePacketCreator::setPacketType(ccsds::PacketType type) { void SpacePacketCreator::setPacketType(ccsds::PacketType type) {
params.packetId.packetType = type; params.packetId.packetType = type;
} }
bool SpacePacketCreator::operator==(const SpacePacketCreator &other) const { return false; }

View File

@ -24,6 +24,9 @@ class SpacePacketCreator : public SpacePacketIF, public SerializeIF {
public: public:
SpacePacketCreator() = default; SpacePacketCreator() = default;
explicit SpacePacketCreator(SpacePacketParams params); explicit SpacePacketCreator(SpacePacketParams params);
bool operator==(const SpacePacketCreator &other) const;
SpacePacketCreator(ccsds::PacketType packetType, bool secHeaderFlag, uint16_t apid, SpacePacketCreator(ccsds::PacketType packetType, bool secHeaderFlag, uint16_t apid,
ccsds::SequenceFlags seqFlags, uint16_t seqCount, uint16_t dataLen, ccsds::SequenceFlags seqFlags, uint16_t seqCount, uint16_t dataLen,
uint8_t version = 0); uint8_t version = 0);
@ -41,7 +44,6 @@ class SpacePacketCreator : public SpacePacketIF, public SerializeIF {
void setSeqFlags(ccsds::SequenceFlags flags); void setSeqFlags(ccsds::SequenceFlags flags);
void setDataLen(uint16_t dataLen); void setDataLen(uint16_t dataLen);
ReturnValue_t serialize(uint8_t **buffer, size_t *size, size_t maxSize) const;
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;

View File

@ -43,7 +43,7 @@ ReturnValue_t PusTmCreator::serialize(uint8_t** buffer, size_t* size, size_t max
if (*size + getSerializedSize() > maxSize) { if (*size + getSerializedSize() > maxSize) {
return SerializeIF::BUFFER_TOO_SHORT; return SerializeIF::BUFFER_TOO_SHORT;
} }
ReturnValue_t result = spCreator.serialize(buffer, size, maxSize); ReturnValue_t result = spCreator.serialize(buffer, size, maxSize, streamEndianness);
if (result != HasReturnvaluesIF::RETURN_OK) { if (result != HasReturnvaluesIF::RETURN_OK) {
return result; return result;
} }

View File

@ -36,18 +36,23 @@ TEST_CASE("CCSDS Low Level", "[ccsds-ll]") {
TEST_CASE("CCSDS Packet ID", "[ccsds-packet-id]") { TEST_CASE("CCSDS Packet ID", "[ccsds-packet-id]") {
PacketId packetId; PacketId packetId;
std::array<uint8_t, 3> buf{}; std::array<uint8_t, 3> buf{};
size_t serLen = 0;
uint8_t* ptr = buf.data();
SECTION("Basic") { SECTION("Basic") {
packetId.apid = 0x1ff; packetId.apid = 0x1ff;
packetId.secHeaderFlag = false; packetId.secHeaderFlag = false;
packetId.packetType = ccsds::PacketType::TM; packetId.packetType = ccsds::PacketType::TM;
REQUIRE(packetId.raw() == 0x1ff); REQUIRE(packetId.raw() == 0x1ff);
REQUIRE(packetId.serialize(buf.data(), buf.size(), SerializeIF::Endianness::NETWORK) == REQUIRE(packetId.SerializeIF::serialize(buf.data(), buf.size(), SerializeIF::Endianness::NETWORK) ==
HasReturnvaluesIF::RETURN_OK); HasReturnvaluesIF::RETURN_OK);
REQUIRE(buf[0] == 0x1); REQUIRE(buf[0] == 0x1);
REQUIRE(buf[1] == 0xff); REQUIRE(buf[1] == 0xff);
} }
SECTION("From Raw") {
auto newId = PacketId(ccsds::PacketType::TC, true, 0x2ff);
uint16_t rawId = newId.raw();
REQUIRE(rawId == 0x1aff);
REQUIRE(PacketId::fromRaw(rawId) == newId);
}
} }
TEST_CASE("CCSDS Packet Seq Ctrl", "[ccsds-packet-seq-ctrl]") {} TEST_CASE("CCSDS Packet Seq Ctrl", "[ccsds-packet-seq-ctrl]") {}

View File

@ -40,7 +40,7 @@ TEST_CASE("CCSDS Creator", "[ccsds-creator]") {
} }
SECTION("Raw Output") { SECTION("Raw Output") {
REQUIRE(base.serialize(&bufPtr, &serLen, buf.size()) == HasReturnvaluesIF::RETURN_OK); REQUIRE(base.serializeNe(&bufPtr, &serLen, buf.size()) == HasReturnvaluesIF::RETURN_OK);
// TC, and secondary header flag is set -> 0b0001100 -> 0x18 // TC, and secondary header flag is set -> 0b0001100 -> 0x18
REQUIRE(buf[0] == 0x18); REQUIRE(buf[0] == 0x18);
// APID 0x02 // APID 0x02
@ -62,7 +62,7 @@ TEST_CASE("CCSDS Creator", "[ccsds-creator]") {
base.setSeqFlags(ccsds::SequenceFlags::UNSEGMENTED); base.setSeqFlags(ccsds::SequenceFlags::UNSEGMENTED);
base.setDataLen(static_cast<int>(std::pow(2, 16)) - 1); base.setDataLen(static_cast<int>(std::pow(2, 16)) - 1);
REQUIRE(base.isValid()); REQUIRE(base.isValid());
REQUIRE(base.serialize(&bufPtr, &serLen, buf.size()) == HasReturnvaluesIF::RETURN_OK); REQUIRE(base.serializeNe(&bufPtr, &serLen, buf.size()) == HasReturnvaluesIF::RETURN_OK);
REQUIRE(buf[0] == 0x1F); REQUIRE(buf[0] == 0x1F);
REQUIRE(buf[1] == 0xFF); REQUIRE(buf[1] == 0xFF);
REQUIRE(buf[2] == 0xFF); REQUIRE(buf[2] == 0xFF);
@ -75,28 +75,28 @@ TEST_CASE("CCSDS Creator", "[ccsds-creator]") {
SpacePacketCreator invalid = SpacePacketCreator( SpacePacketCreator invalid = SpacePacketCreator(
ccsds::PacketType::TC, true, 0xFFFF, ccsds::SequenceFlags::FIRST_SEGMENT, 0x34, 0x16); ccsds::PacketType::TC, true, 0xFFFF, ccsds::SequenceFlags::FIRST_SEGMENT, 0x34, 0x16);
REQUIRE(not invalid.isValid()); REQUIRE(not invalid.isValid());
REQUIRE(invalid.serialize(&bufPtr, &serLen, buf.size()) == HasReturnvaluesIF::RETURN_FAILED); REQUIRE(invalid.serializeNe(&bufPtr, &serLen, buf.size()) == HasReturnvaluesIF::RETURN_FAILED);
} }
SECTION("Invalid Seq Count") { SECTION("Invalid Seq Count") {
SpacePacketCreator invalid = SpacePacketCreator( SpacePacketCreator invalid = SpacePacketCreator(
ccsds::PacketType::TC, true, 0x02, ccsds::SequenceFlags::FIRST_SEGMENT, 0xFFFF, 0x16); ccsds::PacketType::TC, true, 0x02, ccsds::SequenceFlags::FIRST_SEGMENT, 0xFFFF, 0x16);
REQUIRE(not invalid.isValid()); REQUIRE(not invalid.isValid());
REQUIRE(invalid.serialize(&bufPtr, &serLen, buf.size()) == HasReturnvaluesIF::RETURN_FAILED); REQUIRE(invalid.serializeNe(&bufPtr, &serLen, buf.size()) == HasReturnvaluesIF::RETURN_FAILED);
} }
SECTION("Invalid Buf Size 1") { SECTION("Invalid Buf Size 1") {
serLen = 2; serLen = 2;
REQUIRE(base.serialize(&bufPtr, &serLen, buf.size()) == SerializeIF::BUFFER_TOO_SHORT); REQUIRE(base.serializeNe(&bufPtr, &serLen, buf.size()) == SerializeIF::BUFFER_TOO_SHORT);
} }
SECTION("Invalid Buf Size 2") { SECTION("Invalid Buf Size 2") {
serLen = 4; serLen = 4;
REQUIRE(base.serialize(&bufPtr, &serLen, buf.size()) == SerializeIF::BUFFER_TOO_SHORT); REQUIRE(base.serializeNe(&bufPtr, &serLen, buf.size()) == SerializeIF::BUFFER_TOO_SHORT);
} }
SECTION("Invalid Buf Size 3") { SECTION("Invalid Buf Size 3") {
serLen = 6; serLen = 6;
REQUIRE(base.serialize(&bufPtr, &serLen, buf.size()) == SerializeIF::BUFFER_TOO_SHORT); REQUIRE(base.serializeNe(&bufPtr, &serLen, buf.size()) == SerializeIF::BUFFER_TOO_SHORT);
} }
} }