fsfw/src/fsfw/tmtcpacket/SpacePacketBase.cpp

89 lines
3.4 KiB
C++
Raw Normal View History

2021-07-13 20:22:54 +02:00
#include "fsfw/tmtcpacket/SpacePacketBase.h"
2020-10-28 02:20:12 +01:00
#include <cstring>
2022-02-02 10:29:30 +01:00
#include "fsfw/serviceinterface/ServiceInterface.h"
SpacePacketBase::SpacePacketBase(const uint8_t* setAddress) {
2022-02-02 10:29:30 +01:00
this->data = reinterpret_cast<SpacePacketPointer*>(const_cast<uint8_t*>(setAddress));
}
2022-07-18 10:20:26 +02:00
SpacePacketBase::~SpacePacketBase() = default;
2022-02-02 10:29:30 +01:00
ReturnValue_t SpacePacketBase::initSpacePacketHeader(bool isTelecommand, bool hasSecondaryHeader,
uint16_t apid, uint16_t sequenceCount) {
if (data == nullptr) {
#if FSFW_VERBOSE_LEVEL >= 1
#if FSFW_CPP_OSTREAM_ENABLED == 1
2022-02-02 10:29:30 +01:00
sif::warning << "SpacePacketBase::initSpacePacketHeader: Data pointer is invalid" << std::endl;
#else
2022-02-02 10:29:30 +01:00
sif::printWarning("SpacePacketBase::initSpacePacketHeader: Data pointer is invalid!\n");
#endif
#endif
2022-02-02 10:29:30 +01:00
return HasReturnvaluesIF::RETURN_FAILED;
}
// reset header to zero:
memset(data, 0, sizeof(this->data->header));
// Set TC/TM bit.
2022-07-18 10:20:26 +02:00
data->header.packetIdHAndVersion = ((isTelecommand ? 1 : 0)) << 4;
2022-02-02 10:29:30 +01:00
// Set secondaryHeader bit
2022-07-18 10:20:26 +02:00
data->header.packetIdHAndVersion |= ((hasSecondaryHeader ? 1 : 0)) << 3;
this->setApid(apid);
2022-02-02 10:29:30 +01:00
// Always initialize as standalone packets.
2022-07-18 10:20:26 +02:00
data->header.packetSeqCtrlH = 0b11000000;
2022-02-02 10:29:30 +01:00
setPacketSequenceCount(sequenceCount);
return HasReturnvaluesIF::RETURN_OK;
}
2022-07-18 10:20:26 +02:00
inline uint16_t SpacePacketBase::getPacketId() const {
return ((this->data->header.packetIdHAndVersion) << 8) + this->data->header.packetIdL;
}
2022-07-18 10:20:26 +02:00
void SpacePacketBase::setApid(uint16_t setAPID) {
2022-02-02 10:29:30 +01:00
// Use first three bits of new APID, but keep rest of packet id as it was (see specification).
2022-07-18 10:20:26 +02:00
this->data->header.packetIdHAndVersion =
(this->data->header.packetIdHAndVersion & 0b11111000) | ((setAPID & 0x0700) >> 8);
this->data->header.packetIdL = (setAPID & 0x00FF);
}
2022-02-02 10:29:30 +01:00
void SpacePacketBase::setSequenceFlags(uint8_t sequenceflags) {
2022-07-18 10:20:26 +02:00
this->data->header.packetSeqCtrlH &= 0x3F;
this->data->header.packetSeqCtrlH |= sequenceflags << 6;
}
2022-02-02 10:29:30 +01:00
void SpacePacketBase::setPacketSequenceCount(uint16_t new_count) {
2022-07-18 10:20:26 +02:00
this->data->header.packetSeqCtrlH = (this->data->header.packetSeqCtrlH & 0b11000000) |
(((new_count % ccsds::LIMIT_SEQUENCE_COUNT) & 0x3F00) >> 8);
this->data->header.packetSeqCtrlL = ((new_count % ccsds::LIMIT_SEQUENCE_COUNT) & 0x00FF);
}
2022-02-02 10:29:30 +01:00
void SpacePacketBase::setPacketDataLength(uint16_t new_length) {
2022-07-18 10:20:26 +02:00
this->data->header.packetLenH = ((new_length & 0xFF00) >> 8);
this->data->header.packetLenL = (new_length & 0x00FF);
}
2020-05-15 19:51:39 +02:00
size_t SpacePacketBase::getFullSize() {
2022-02-02 10:29:30 +01:00
// +1 is done because size in packet data length field is: size of data field -1
2022-07-18 10:20:26 +02:00
return this->getPacketDataLen() + sizeof(this->data->header) + 1;
}
2022-07-18 10:20:26 +02:00
uint8_t* SpacePacketBase::getWholeData() { return reinterpret_cast<uint8_t*>(this->data); }
2022-02-02 10:29:30 +01:00
uint8_t* SpacePacketBase::getPacketData() { return &(data->packet_data); }
2022-02-02 10:29:30 +01:00
ReturnValue_t SpacePacketBase::setData(uint8_t* pData, size_t maxSize, void* args) {
if (maxSize < 6) {
return HasReturnvaluesIF::RETURN_FAILED;
}
this->data = reinterpret_cast<SpacePacketPointer*>(const_cast<uint8_t*>(pData));
return HasReturnvaluesIF::RETURN_OK;
}
2022-07-18 10:20:26 +02:00
uint16_t SpacePacketBase::getPacketSeqCtrl() const {
return ((this->data->header.packetSeqCtrlH & 0b00111111) << 8) +
this->data->header.packetSeqCtrlL;
}
uint16_t SpacePacketBase::getPacketDataLen() const {
return ((this->data->header.packetLenH) << 8) + this->data->header.packetLenL;
}