fsfw/unittests/tmtcpacket/testCcsdsCreator.cpp

114 lines
4.1 KiB
C++
Raw Normal View History

2022-07-18 16:07:26 +02:00
#include <array>
#include <catch2/catch_test_macros.hpp>
2022-07-18 16:18:04 +02:00
#include <cmath>
2022-07-18 16:07:26 +02:00
2022-07-20 11:43:16 +02:00
#include "fsfw/tmtcpacket/ccsds/SpacePacketCreator.h"
2022-07-18 16:07:26 +02:00
2022-07-18 16:21:00 +02:00
TEST_CASE("CCSDS Creator", "[ccsds-creator]") {
2022-07-23 11:43:48 +02:00
auto packetId = PacketId(ccsds::PacketType::TC, true, 0x02);
auto psc = PacketSeqCtrl(ccsds::SequenceFlags::FIRST_SEGMENT, 0x34);
auto params = SpacePacketParams(packetId, psc, 0x16);
2022-07-20 11:43:16 +02:00
SpacePacketCreator base = SpacePacketCreator(params);
2022-07-18 16:18:04 +02:00
std::array<uint8_t, 6> buf{};
2022-07-18 16:07:26 +02:00
uint8_t* bufPtr = buf.data();
size_t serLen = 0;
SECTION("Constexpr Helpers") {
2022-09-26 14:15:11 +02:00
REQUIRE(ccsds::getTcSpacePacketIdFromApid(0x22, true) == 0x1822);
REQUIRE(ccsds::getTmSpacePacketIdFromApid(0x22, true) == 0x0822);
2022-07-18 16:07:26 +02:00
2022-09-26 14:15:11 +02:00
REQUIRE(ccsds::getTcSpacePacketIdFromApid(0x22, false) == 0x1022);
REQUIRE(ccsds::getTmSpacePacketIdFromApid(0x22, false) == 0x0022);
REQUIRE(ccsds::getTcSpacePacketIdFromApid(0x7ff, true) == 0x1fff);
REQUIRE(ccsds::getTmSpacePacketIdFromApid(0x7ff, true) == 0xfff);
REQUIRE(ccsds::getTcSpacePacketIdFromApid(0x7ff, false) == 0x17ff);
REQUIRE(ccsds::getTmSpacePacketIdFromApid(0x7ff, false) == 0x7ff);
2022-07-18 16:07:26 +02:00
}
SECTION("Basic Test") {
2022-07-23 11:43:48 +02:00
CHECK(base.isValid());
CHECK(base);
CHECK(base.getApid() == 0x02);
CHECK(base.getSequenceFlags() == ccsds::SequenceFlags::FIRST_SEGMENT);
CHECK(base.getVersion() == 0b000);
CHECK(base.getSequenceCount() == 0x34);
CHECK(base.getPacketDataLen() == 0x16);
CHECK(base.getPacketType() == ccsds::PacketType::TC);
CHECK(base.getPacketIdRaw() == 0x1802);
CHECK(base.getSerializedSize() == 6);
CHECK(base.getPacketSeqCtrl() == psc);
CHECK(base.getPacketId() == packetId);
2022-07-18 16:18:04 +02:00
}
SECTION("Deserialization Fails") {
serLen = 6;
const uint8_t* readOnlyPtr = buf.data();
REQUIRE(base.deSerialize(&readOnlyPtr, &serLen, SerializeIF::Endianness::BIG) ==
2022-08-16 01:08:26 +02:00
returnvalue::FAILED);
2022-07-18 16:07:26 +02:00
}
SECTION("Raw Output") {
2022-08-16 01:08:26 +02:00
REQUIRE(base.serializeBe(&bufPtr, &serLen, buf.size()) == returnvalue::OK);
2022-07-18 16:07:26 +02:00
// TC, and secondary header flag is set -> 0b0001100 -> 0x18
2022-07-23 11:43:48 +02:00
CHECK(buf[0] == 0x18);
2022-07-18 16:07:26 +02:00
// APID 0x02
2022-07-23 11:43:48 +02:00
CHECK(buf[1] == 0x02);
2022-07-18 16:07:26 +02:00
// Sequence count is one byte value, so the only set bit here is the bit
// from the Sequence flag argument, which is the second bit for
// SequenceFlags.FIRST_SEGMENT
2022-07-23 11:43:48 +02:00
CHECK(buf[2] == 0x40);
2022-07-18 16:07:26 +02:00
// Sequence Count specified above
2022-07-23 11:43:48 +02:00
CHECK(buf[3] == 0x34);
2022-07-18 16:07:26 +02:00
// This byte and the next byte should be 22 big endian (packet length)
2022-07-23 11:43:48 +02:00
CHECK(buf[4] == 0x00);
CHECK(buf[5] == 0x16);
2022-07-18 16:07:26 +02:00
}
SECTION("All Ones Output") {
base.setApid(static_cast<int>(std::pow(2, 11)) - 1);
base.setSeqCount(static_cast<int>(std::pow(2, 14)) - 1);
base.setSeqFlags(ccsds::SequenceFlags::UNSEGMENTED);
base.setDataLen(static_cast<int>(std::pow(2, 16)) - 1);
REQUIRE(base.isValid());
2022-08-16 01:08:26 +02:00
REQUIRE(base.serializeBe(&bufPtr, &serLen, buf.size()) == returnvalue::OK);
2022-07-23 11:43:48 +02:00
CHECK(buf[0] == 0x1F);
CHECK(buf[1] == 0xFF);
CHECK(buf[2] == 0xFF);
CHECK(buf[3] == 0xFF);
CHECK(buf[4] == 0xFF);
CHECK(buf[5] == 0xFF);
2022-07-18 16:07:26 +02:00
}
SECTION("Invalid APID") {
2022-07-23 11:43:48 +02:00
SpacePacketCreator creator = SpacePacketCreator(
2022-07-20 11:43:16 +02:00
ccsds::PacketType::TC, true, 0xFFFF, ccsds::SequenceFlags::FIRST_SEGMENT, 0x34, 0x16);
2022-07-23 11:43:48 +02:00
REQUIRE(not creator.isValid());
REQUIRE(not creator);
2022-08-16 01:08:26 +02:00
REQUIRE(creator.serializeBe(&bufPtr, &serLen, buf.size()) == returnvalue::FAILED);
2022-07-18 16:07:26 +02:00
}
SECTION("Invalid Seq Count") {
2022-07-20 11:43:16 +02:00
SpacePacketCreator invalid = SpacePacketCreator(
ccsds::PacketType::TC, true, 0x02, ccsds::SequenceFlags::FIRST_SEGMENT, 0xFFFF, 0x16);
2022-07-18 16:07:26 +02:00
REQUIRE(not invalid.isValid());
2022-08-16 01:08:26 +02:00
REQUIRE(invalid.serializeBe(&bufPtr, &serLen, buf.size()) == returnvalue::FAILED);
2022-07-18 16:07:26 +02:00
}
2022-07-18 16:18:04 +02:00
SECTION("Invalid Buf Size 1") {
serLen = 2;
REQUIRE(base.serializeBe(&bufPtr, &serLen, buf.size()) == SerializeIF::BUFFER_TOO_SHORT);
2022-07-18 16:18:04 +02:00
}
SECTION("Invalid Buf Size 2") {
serLen = 4;
REQUIRE(base.serializeBe(&bufPtr, &serLen, buf.size()) == SerializeIF::BUFFER_TOO_SHORT);
2022-07-18 16:18:04 +02:00
}
SECTION("Invalid Buf Size 3") {
serLen = 6;
REQUIRE(base.serializeBe(&bufPtr, &serLen, buf.size()) == SerializeIF::BUFFER_TOO_SHORT);
2022-07-18 16:18:04 +02:00
}
2022-07-18 16:07:26 +02:00
}