fsfw/tmtcpacket/pus/TcPacketBase.cpp

94 lines
2.9 KiB
C++
Raw Normal View History

2020-08-13 20:53:35 +02:00
#include "../../globalfunctions/CRC.h"
#include "../../serviceinterface/ServiceInterfaceStream.h"
#include "TcPacketBase.h"
#include <string.h>
TcPacketBase::TcPacketBase(const uint8_t* set_data) :
SpacePacketBase(set_data) {
tcData = (TcPacketPointer*) set_data;
}
TcPacketBase::~TcPacketBase() {
//Nothing to do.
}
uint8_t TcPacketBase::getService() {
2020-05-19 18:55:13 +02:00
return tcData->dataField.service_type;
}
uint8_t TcPacketBase::getSubService() {
2020-05-19 18:55:13 +02:00
return tcData->dataField.service_subtype;
}
uint8_t TcPacketBase::getAcknowledgeFlags() {
2020-05-19 18:55:13 +02:00
return tcData->dataField.version_type_ack & 0b00001111;
}
const uint8_t* TcPacketBase::getApplicationData() const {
2020-05-19 18:55:13 +02:00
return &tcData->appData;
}
2020-05-15 19:56:21 +02:00
uint16_t TcPacketBase::getApplicationDataSize() {
2020-05-19 18:55:13 +02:00
return getPacketDataLength() - sizeof(tcData->dataField) - CRC_SIZE + 1;
}
uint16_t TcPacketBase::getErrorControl() {
uint16_t size = getApplicationDataSize() + CRC_SIZE;
2020-05-19 18:55:13 +02:00
uint8_t* p_to_buffer = &tcData->appData;
return (p_to_buffer[size - 2] << 8) + p_to_buffer[size - 1];
}
void TcPacketBase::setErrorControl() {
uint32_t full_size = getFullSize();
uint16_t crc = CRC::crc16ccitt(getWholeData(), full_size - CRC_SIZE);
uint32_t size = getApplicationDataSize();
2020-05-19 18:55:13 +02:00
(&tcData->appData)[size] = (crc & 0XFF00) >> 8; // CRCH
(&tcData->appData)[size + 1] = (crc) & 0X00FF; // CRCL
}
2020-05-15 19:44:14 +02:00
void TcPacketBase::setData(const uint8_t* pData) {
SpacePacketBase::setData(pData);
tcData = (TcPacketPointer*) pData;
}
2020-05-19 19:05:17 +02:00
void TcPacketBase::setAppData(uint8_t * appData, uint16_t dataLen) {
memcpy(&tcData->appData, appData, dataLen);
2020-05-15 19:44:14 +02:00
SpacePacketBase::setPacketDataLength(dataLen +
2020-05-19 19:05:17 +02:00
sizeof(PUSTcDataFieldHeader) + TcPacketBase::CRC_SIZE - 1);
}
uint8_t TcPacketBase::getSecondaryHeaderFlag() {
2020-05-19 18:55:13 +02:00
return (tcData->dataField.version_type_ack & 0b10000000) >> 7;
}
uint8_t TcPacketBase::getPusVersionNumber() {
2020-05-19 18:55:13 +02:00
return (tcData->dataField.version_type_ack & 0b01110000) >> 4;
}
void TcPacketBase::print() {
uint8_t * wholeData = getWholeData();
sif::debug << "TcPacket contains: " << std::endl;
for (uint8_t count = 0; count < getFullSize(); ++count) {
sif::debug << std::hex << (uint16_t) wholeData[count] << " ";
}
sif::debug << std::dec << std::endl;
}
void TcPacketBase::initializeTcPacket(uint16_t apid, uint16_t sequenceCount,
uint8_t ack, uint8_t service, uint8_t subservice) {
initSpacePacketHeader(true, true, apid, sequenceCount);
2020-05-19 18:55:13 +02:00
memset(&tcData->dataField, 0, sizeof(tcData->dataField));
2020-05-15 19:44:14 +02:00
setPacketDataLength(sizeof(PUSTcDataFieldHeader) + CRC_SIZE - 1);
//Data Field Header:
//Set CCSDS_secondary_header_flag to 0, version number to 001 and ack to 0000
2020-05-19 18:55:13 +02:00
tcData->dataField.version_type_ack = 0b00010000;
tcData->dataField.version_type_ack |= (ack & 0x0F);
tcData->dataField.service_type = service;
tcData->dataField.service_subtype = subservice;
}
2020-05-15 19:44:14 +02:00
size_t TcPacketBase::calculateFullPacketLength(size_t appDataLen) {
return sizeof(CCSDSPrimaryHeader) + sizeof(PUSTcDataFieldHeader) +
appDataLen + TcPacketBase::CRC_SIZE;
}