2020-10-28 02:20:12 +01:00
|
|
|
#include "TcPacketBase.h"
|
|
|
|
|
2020-08-13 20:53:35 +02:00
|
|
|
#include "../../globalfunctions/CRC.h"
|
2020-10-28 02:20:12 +01:00
|
|
|
#include "../../globalfunctions/arrayprinter.h"
|
2020-08-13 20:53:35 +02:00
|
|
|
#include "../../serviceinterface/ServiceInterfaceStream.h"
|
2016-06-15 23:48:41 +02:00
|
|
|
|
2020-10-28 02:20:12 +01:00
|
|
|
#include <cstring>
|
|
|
|
|
|
|
|
TcPacketBase::TcPacketBase(const uint8_t* setData) :
|
|
|
|
SpacePacketBase(setData) {
|
|
|
|
tcData = reinterpret_cast<TcPacketPointer*>(const_cast<uint8_t*>(setData));
|
2016-06-15 23:48:41 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
TcPacketBase::~TcPacketBase() {
|
|
|
|
//Nothing to do.
|
|
|
|
}
|
|
|
|
|
|
|
|
uint8_t TcPacketBase::getService() {
|
2020-05-19 18:55:13 +02:00
|
|
|
return tcData->dataField.service_type;
|
2016-06-15 23:48:41 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
uint8_t TcPacketBase::getSubService() {
|
2020-05-19 18:55:13 +02:00
|
|
|
return tcData->dataField.service_subtype;
|
2016-06-15 23:48:41 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
uint8_t TcPacketBase::getAcknowledgeFlags() {
|
2020-05-19 18:55:13 +02:00
|
|
|
return tcData->dataField.version_type_ack & 0b00001111;
|
2016-06-15 23:48:41 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
const uint8_t* TcPacketBase::getApplicationData() const {
|
2020-05-19 18:55:13 +02:00
|
|
|
return &tcData->appData;
|
2016-06-15 23:48:41 +02:00
|
|
|
}
|
|
|
|
|
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;
|
2016-06-15 23:48:41 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
uint16_t TcPacketBase::getErrorControl() {
|
2018-07-12 16:29:32 +02:00
|
|
|
uint16_t size = getApplicationDataSize() + CRC_SIZE;
|
2020-05-19 18:55:13 +02:00
|
|
|
uint8_t* p_to_buffer = &tcData->appData;
|
2018-07-12 16:29:32 +02:00
|
|
|
return (p_to_buffer[size - 2] << 8) + p_to_buffer[size - 1];
|
2016-06-15 23:48:41 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void TcPacketBase::setErrorControl() {
|
2018-07-12 16:29:32 +02:00
|
|
|
uint32_t full_size = getFullSize();
|
2020-04-06 13:22:42 +02:00
|
|
|
uint16_t crc = CRC::crc16ccitt(getWholeData(), full_size - CRC_SIZE);
|
2018-07-12 16:29:32 +02:00
|
|
|
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
|
2016-06-15 23:48:41 +02:00
|
|
|
}
|
|
|
|
|
2020-05-15 19:44:14 +02:00
|
|
|
void TcPacketBase::setData(const uint8_t* pData) {
|
|
|
|
SpacePacketBase::setData(pData);
|
|
|
|
tcData = (TcPacketPointer*) pData;
|
|
|
|
}
|
|
|
|
|
2016-06-15 23:48:41 +02:00
|
|
|
uint8_t TcPacketBase::getSecondaryHeaderFlag() {
|
2020-05-19 18:55:13 +02:00
|
|
|
return (tcData->dataField.version_type_ack & 0b10000000) >> 7;
|
2016-06-15 23:48:41 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
uint8_t TcPacketBase::getPusVersionNumber() {
|
2020-05-19 18:55:13 +02:00
|
|
|
return (tcData->dataField.version_type_ack & 0b01110000) >> 4;
|
2016-06-15 23:48:41 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void TcPacketBase::print() {
|
2021-01-03 14:16:52 +01:00
|
|
|
#if FSFW_CPP_OSTREAM_ENABLED == 1
|
2020-10-28 02:20:12 +01:00
|
|
|
sif::debug << "TcPacketBase::print: " << std::endl;
|
2021-01-03 13:58:18 +01:00
|
|
|
#endif
|
2020-10-28 02:20:12 +01:00
|
|
|
arrayprinter::print(getWholeData(), getFullSize());
|
2016-06-15 23:48:41 +02:00
|
|
|
}
|
2018-07-12 16:29:32 +02:00
|
|
|
|
|
|
|
void TcPacketBase::initializeTcPacket(uint16_t apid, uint16_t sequenceCount,
|
2020-10-28 02:20:12 +01:00
|
|
|
uint8_t ack, uint8_t service, uint8_t subservice) {
|
2018-07-12 16:29:32 +02:00
|
|
|
initSpacePacketHeader(true, true, apid, sequenceCount);
|
2020-10-28 02:20:12 +01:00
|
|
|
std::memset(&tcData->dataField, 0, sizeof(tcData->dataField));
|
2020-05-15 19:44:14 +02:00
|
|
|
setPacketDataLength(sizeof(PUSTcDataFieldHeader) + CRC_SIZE - 1);
|
2018-07-12 16:29:32 +02:00
|
|
|
//Data Field Header:
|
2020-10-28 02:20:12 +01:00
|
|
|
//Set CCSDS_secondary_header_flag to 0 and version number to 001
|
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;
|
2018-07-12 16:29:32 +02:00
|
|
|
}
|
2020-05-15 19:44:14 +02:00
|
|
|
|
|
|
|
size_t TcPacketBase::calculateFullPacketLength(size_t appDataLen) {
|
|
|
|
return sizeof(CCSDSPrimaryHeader) + sizeof(PUSTcDataFieldHeader) +
|
|
|
|
appDataLen + TcPacketBase::CRC_SIZE;
|
|
|
|
}
|