fsfw/tmtcpacket/pus/tc/TcPacketPus.cpp

76 lines
2.6 KiB
C++
Raw Normal View History

2021-06-14 10:19:01 +02:00
#include "TcPacketPus.h"
#include "../../../globalfunctions/CRC.h"
2021-06-13 12:34:06 +02:00
2021-06-13 16:29:13 +02:00
#include <cstring>
2021-06-13 12:34:06 +02:00
2021-06-14 10:19:01 +02:00
TcPacketPus::TcPacketPus(const uint8_t *setData): TcPacketBase(setData) {
2021-06-13 16:29:13 +02:00
tcData = reinterpret_cast<TcPacketPointer*>(const_cast<uint8_t*>(setData));
}
2021-06-14 10:19:01 +02:00
uint8_t TcPacketPus::getService() const {
return tcData->dataField.serviceType;
2021-06-13 16:29:13 +02:00
}
2021-06-14 10:19:01 +02:00
uint8_t TcPacketPus::getSubService() {
return tcData->dataField.serviceSubtype;
2021-06-13 16:29:13 +02:00
}
2021-06-14 10:19:01 +02:00
uint8_t TcPacketPus::getAcknowledgeFlags() {
return tcData->dataField.versionTypeAck & 0b00001111;
2021-06-13 16:29:13 +02:00
}
2021-06-14 10:19:01 +02:00
const uint8_t* TcPacketPus::getApplicationData() const {
2021-06-13 16:29:13 +02:00
return &tcData->appData;
}
2021-06-14 10:19:01 +02:00
uint16_t TcPacketPus::getApplicationDataSize() {
2021-06-13 16:29:13 +02:00
return getPacketDataLength() - sizeof(tcData->dataField) - CRC_SIZE + 1;
}
2021-06-14 10:19:01 +02:00
uint16_t TcPacketPus::getErrorControl() {
2021-06-13 16:29:13 +02:00
uint16_t size = getApplicationDataSize() + CRC_SIZE;
uint8_t* p_to_buffer = &tcData->appData;
return (p_to_buffer[size - 2] << 8) + p_to_buffer[size - 1];
}
2021-06-14 10:19:01 +02:00
void TcPacketPus::setErrorControl() {
2021-06-13 16:29:13 +02:00
uint32_t full_size = getFullSize();
uint16_t crc = CRC::crc16ccitt(getWholeData(), full_size - CRC_SIZE);
uint32_t size = getApplicationDataSize();
(&tcData->appData)[size] = (crc & 0XFF00) >> 8; // CRCH
(&tcData->appData)[size + 1] = (crc) & 0X00FF; // CRCL
}
2021-06-14 10:19:01 +02:00
void TcPacketPus::setData(const uint8_t* pData) {
2021-06-13 16:29:13 +02:00
SpacePacketBase::setData(pData);
// This function is const-correct, but it was decided to keep the pointer non-const
// for convenience. Therefore, cast aways constness here and then cast to packet type.
tcData = reinterpret_cast<TcPacketPointer*>(const_cast<uint8_t*>(pData));
}
2021-06-14 10:19:01 +02:00
uint8_t TcPacketPus::getSecondaryHeaderFlag() {
return (tcData->dataField.versionTypeAck & 0b10000000) >> 7;
2021-06-13 16:29:13 +02:00
}
2021-06-14 10:19:01 +02:00
uint8_t TcPacketPus::getPusVersionNumber() {
return (tcData->dataField.versionTypeAck & 0b01110000) >> 4;
2021-06-13 16:29:13 +02:00
}
2021-06-14 10:19:01 +02:00
void TcPacketPus::initializeTcPacket(uint16_t apid, uint16_t sequenceCount,
2021-06-13 16:29:13 +02:00
uint8_t ack, uint8_t service, uint8_t subservice) {
initSpacePacketHeader(true, true, apid, sequenceCount);
std::memset(&tcData->dataField, 0, sizeof(tcData->dataField));
setPacketDataLength(sizeof(PUSTcDataFieldHeader) + CRC_SIZE - 1);
// Data Field Header:
// Set CCSDS_secondary_header_flag to 0 and version number to 001
2021-06-14 10:19:01 +02:00
tcData->dataField.versionTypeAck = 0b00010000;
tcData->dataField.versionTypeAck |= (ack & 0x0F);
tcData->dataField.serviceType = service;
tcData->dataField.serviceSubtype = subservice;
2021-06-13 16:29:13 +02:00
}
2021-06-14 10:19:01 +02:00
size_t TcPacketPus::calculateFullPacketLength(size_t appDataLen) {
2021-06-13 16:29:13 +02:00
return sizeof(CCSDSPrimaryHeader) + sizeof(PUSTcDataFieldHeader) +
appDataLen + TcPacketBase::CRC_SIZE;
}