2022-07-21 19:16:44 +02:00
|
|
|
#include <catch2/catch_test_macros.hpp>
|
|
|
|
|
2022-07-22 17:09:44 +02:00
|
|
|
#include "fsfw/globalfunctions/CRC.h"
|
2022-07-22 16:06:31 +02:00
|
|
|
#include "fsfw/globalfunctions/arrayprinter.h"
|
|
|
|
#include "fsfw/tmtcpacket/pus/tm.h"
|
2024-12-12 16:15:02 +01:00
|
|
|
#include "mock/CdsShortTimestamperMock.h"
|
|
|
|
#include "mock/SimpleSerializable.h"
|
2022-07-22 16:06:31 +02:00
|
|
|
|
|
|
|
TEST_CASE("PUS TM Creator", "[pus-tm-creator]") {
|
|
|
|
auto packetId = PacketId(ccsds::PacketType::TC, true, 0xef);
|
|
|
|
auto spParams =
|
|
|
|
SpacePacketParams(packetId, PacketSeqCtrl(ccsds::SequenceFlags::UNSEGMENTED, 22), 0x00);
|
|
|
|
auto timeStamper = CdsShortTimestamperMock();
|
|
|
|
auto pusTmParams = PusTmParams(17, 2, &timeStamper);
|
|
|
|
timeStamper.valueToStamp = {1, 2, 3, 4, 5, 6, 7};
|
|
|
|
PusTmCreator creator(spParams, pusTmParams);
|
|
|
|
std::array<uint8_t, 32> buf{};
|
|
|
|
uint8_t* dataPtr = buf.data();
|
|
|
|
size_t serLen = 0;
|
|
|
|
|
|
|
|
SECTION("State") {
|
2022-07-28 15:13:27 +02:00
|
|
|
CHECK(creator.isTm());
|
|
|
|
CHECK(creator.hasSecHeader());
|
|
|
|
CHECK(creator.getApid() == 0xef);
|
|
|
|
CHECK(creator.getPusVersion() == 2);
|
|
|
|
CHECK(creator.getScTimeRefStatus() == 0);
|
|
|
|
CHECK(creator.getService() == 17);
|
|
|
|
CHECK(creator.getSubService() == 2);
|
|
|
|
CHECK(creator.getTimestamper() == &timeStamper);
|
|
|
|
CHECK(creator.getSequenceFlags() == ccsds::SequenceFlags::UNSEGMENTED);
|
|
|
|
CHECK(creator.getSequenceCount() == 22);
|
2022-07-22 16:06:31 +02:00
|
|
|
// 6 bytes CCSDS header, 7 bytes secondary header, 7 bytes CDS short timestamp,
|
|
|
|
// 0 bytes application data, 2 bytes CRC
|
2022-07-28 15:13:27 +02:00
|
|
|
CHECK(creator.getFullPacketLen() == 22);
|
2022-07-22 16:06:31 +02:00
|
|
|
// As specified in standard, the data length fields is the total size of the packet without
|
|
|
|
// the primary header minus 1
|
2022-07-28 15:13:27 +02:00
|
|
|
CHECK(creator.getPacketDataLen() == 15);
|
|
|
|
CHECK(timeStamper.getSizeCallCount == 1);
|
2022-07-22 16:06:31 +02:00
|
|
|
}
|
|
|
|
|
2022-07-22 17:09:44 +02:00
|
|
|
SECTION("SP Params") {
|
|
|
|
auto& spParamsRef = creator.getSpParams();
|
|
|
|
REQUIRE(spParamsRef.dataLen == 15);
|
|
|
|
REQUIRE(spParamsRef.packetId.apid == 0xef);
|
|
|
|
}
|
|
|
|
|
2022-07-22 16:06:31 +02:00
|
|
|
SECTION("Serialization") {
|
2022-08-16 01:08:26 +02:00
|
|
|
REQUIRE(creator.SerializeIF::serializeBe(&dataPtr, &serLen, buf.size()) == returnvalue::OK);
|
2022-07-22 16:06:31 +02:00
|
|
|
REQUIRE(buf[0] == 0x08);
|
|
|
|
REQUIRE(buf[1] == 0xef);
|
|
|
|
// Unsegmented is the default
|
|
|
|
REQUIRE(buf[2] == 0xc0);
|
|
|
|
REQUIRE(buf[3] == 22);
|
|
|
|
REQUIRE(buf[4] == 0);
|
|
|
|
REQUIRE(buf[5] == 15);
|
|
|
|
REQUIRE(((buf[6] >> 4) & 0b1111) == ecss::PusVersion::PUS_C);
|
|
|
|
// SC time reference field
|
|
|
|
REQUIRE((buf[6] & 0b1111) == 0);
|
|
|
|
// Service and subservice field
|
|
|
|
REQUIRE(buf[7] == 17);
|
|
|
|
REQUIRE(buf[8] == 2);
|
|
|
|
// Message Sequence Count
|
|
|
|
REQUIRE(((buf[9] << 8) | buf[10]) == 0);
|
|
|
|
// Destination ID
|
|
|
|
REQUIRE(((buf[11] << 8) | buf[12]) == 0);
|
|
|
|
// Custom timestamp
|
2022-07-22 17:09:44 +02:00
|
|
|
for (size_t i = 1; i < 8; i++) {
|
2022-07-22 16:06:31 +02:00
|
|
|
REQUIRE(buf[12 + i] == i);
|
|
|
|
}
|
|
|
|
REQUIRE(serLen == 22);
|
|
|
|
REQUIRE(CRC::crc16ccitt(buf.data(), serLen) == 0);
|
|
|
|
REQUIRE(buf[20] == 0x03);
|
|
|
|
REQUIRE(buf[21] == 0x79);
|
2022-07-22 18:22:35 +02:00
|
|
|
REQUIRE(timeStamper.serializeCallCount == 1);
|
2022-07-22 16:06:31 +02:00
|
|
|
}
|
2022-07-22 16:41:32 +02:00
|
|
|
|
|
|
|
SECTION("Custom Fields") {
|
|
|
|
creator.setApid(0x3ff);
|
|
|
|
SECTION("Using Params") {
|
|
|
|
auto& pusParams = creator.getParams();
|
|
|
|
pusParams.secHeader.destId = 0xfff;
|
|
|
|
pusParams.secHeader.messageTypeCounter = 0x313;
|
|
|
|
}
|
|
|
|
SECTION("Using Setters") {
|
|
|
|
auto& pusParams = creator.getParams();
|
|
|
|
creator.setDestId(0xfff);
|
|
|
|
creator.setMessageTypeCounter(0x313);
|
|
|
|
}
|
|
|
|
REQUIRE(creator.getApid() == 0x3ff);
|
|
|
|
REQUIRE(creator.getDestId() == 0xfff);
|
|
|
|
REQUIRE(creator.getMessageTypeCounter() == 0x313);
|
2022-08-16 01:08:26 +02:00
|
|
|
REQUIRE(creator.serializeBe(&dataPtr, &serLen, buf.size()) == returnvalue::OK);
|
2022-07-22 16:41:32 +02:00
|
|
|
// Message Sequence Count
|
|
|
|
REQUIRE(((buf[9] << 8) | buf[10]) == 0x313);
|
|
|
|
// Destination ID
|
|
|
|
REQUIRE(((buf[11] << 8) | buf[12]) == 0xfff);
|
|
|
|
}
|
|
|
|
|
|
|
|
SECTION("Deserialization fails") {
|
2022-07-27 21:11:12 +02:00
|
|
|
SerializeIF& deser = creator;
|
2022-07-22 16:41:32 +02:00
|
|
|
const uint8_t* roDataPtr = nullptr;
|
2022-07-27 21:11:12 +02:00
|
|
|
REQUIRE(deser.deSerialize(&roDataPtr, &serLen, SerializeIF::Endianness::NETWORK) ==
|
2022-08-16 01:08:26 +02:00
|
|
|
returnvalue::FAILED);
|
2022-07-22 16:41:32 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
SECTION("Serialize with Raw Data") {
|
|
|
|
std::array<uint8_t, 3> data{1, 2, 3};
|
2022-07-22 17:09:44 +02:00
|
|
|
creator.setRawUserData(data.data(), data.size());
|
2022-07-22 16:41:32 +02:00
|
|
|
REQUIRE(creator.getFullPacketLen() == 25);
|
2022-08-16 01:08:26 +02:00
|
|
|
REQUIRE(creator.serializeBe(&dataPtr, &serLen, buf.size()) == returnvalue::OK);
|
2022-07-22 16:41:32 +02:00
|
|
|
REQUIRE(buf[20] == 1);
|
|
|
|
REQUIRE(buf[21] == 2);
|
|
|
|
REQUIRE(buf[22] == 3);
|
|
|
|
}
|
|
|
|
|
|
|
|
SECTION("Serialize with Serializable") {
|
2022-07-22 17:09:44 +02:00
|
|
|
auto simpleSer = SimpleSerializable();
|
2022-07-25 11:26:45 +02:00
|
|
|
creator.setSerializableUserData(simpleSer);
|
2022-07-22 17:09:44 +02:00
|
|
|
REQUIRE(creator.getFullPacketLen() == 25);
|
2022-08-15 19:16:31 +02:00
|
|
|
REQUIRE(creator.serialize(&dataPtr, &serLen, buf.size(), SerializeIF::Endianness::NETWORK) ==
|
2022-08-16 01:08:26 +02:00
|
|
|
returnvalue::OK);
|
2022-07-22 17:09:44 +02:00
|
|
|
REQUIRE(buf[20] == 1);
|
|
|
|
REQUIRE(buf[21] == 2);
|
|
|
|
REQUIRE(buf[22] == 3);
|
|
|
|
}
|
|
|
|
|
|
|
|
SECTION("Empty Ctor") {
|
|
|
|
PusTmCreator creatorFromEmptyCtor;
|
|
|
|
// 6 bytes CCSDS header, 7 bytes secondary header, no timestamp (IF is null),
|
|
|
|
// 0 bytes application data, 2 bytes CRC
|
|
|
|
REQUIRE(creatorFromEmptyCtor.getFullPacketLen() == 15);
|
|
|
|
// As specified in standard, the data length fields is the total size of the packet without
|
|
|
|
// the primary header minus 1
|
|
|
|
REQUIRE(creatorFromEmptyCtor.getPacketDataLen() == 8);
|
2022-07-25 10:50:52 +02:00
|
|
|
creatorFromEmptyCtor.setTimeStamper(timeStamper);
|
2022-07-22 17:09:44 +02:00
|
|
|
REQUIRE(creatorFromEmptyCtor.getFullPacketLen() == 22);
|
|
|
|
REQUIRE(creatorFromEmptyCtor.getPacketDataLen() == 15);
|
|
|
|
}
|
2022-07-22 16:41:32 +02:00
|
|
|
|
2022-07-22 17:09:44 +02:00
|
|
|
SECTION("Invalid Buffer Sizes") {
|
|
|
|
size_t reqSize = creator.getSerializedSize();
|
|
|
|
for (size_t maxSize = 0; maxSize < reqSize; maxSize++) {
|
|
|
|
dataPtr = buf.data();
|
|
|
|
serLen = 0;
|
2022-08-15 19:16:31 +02:00
|
|
|
REQUIRE(creator.serialize(&dataPtr, &serLen, maxSize, SerializeIF::Endianness::NETWORK) ==
|
2022-07-27 21:06:23 +02:00
|
|
|
SerializeIF::BUFFER_TOO_SHORT);
|
2022-07-22 17:09:44 +02:00
|
|
|
}
|
2022-07-22 16:41:32 +02:00
|
|
|
}
|
2022-07-28 13:37:07 +02:00
|
|
|
|
|
|
|
SECTION("No CRC Generation") {
|
|
|
|
creator.disableCrcCalculation();
|
|
|
|
REQUIRE(not creator.crcCalculationEnabled());
|
2022-08-16 01:08:26 +02:00
|
|
|
REQUIRE(creator.serializeBe(dataPtr, serLen, buf.size()) == returnvalue::OK);
|
2022-07-28 13:37:07 +02:00
|
|
|
REQUIRE(serLen == 22);
|
|
|
|
REQUIRE(buf[20] == 0x00);
|
|
|
|
REQUIRE(buf[21] == 0x00);
|
|
|
|
}
|
2022-07-22 16:06:31 +02:00
|
|
|
}
|