2020-10-28 02:20:12 +01:00
|
|
|
#include "TmPacketBase.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 "../../objectmanager/ObjectManagerIF.h"
|
|
|
|
#include "../../serviceinterface/ServiceInterfaceStream.h"
|
|
|
|
#include "../../timemanager/CCSDSTime.h"
|
2016-06-15 23:48:41 +02:00
|
|
|
|
2020-10-28 02:20:12 +01:00
|
|
|
#include <cstring>
|
|
|
|
|
|
|
|
TimeStamperIF* TmPacketBase::timeStamper = nullptr;
|
|
|
|
object_id_t TmPacketBase::timeStamperId = 0;
|
|
|
|
|
|
|
|
TmPacketBase::TmPacketBase(uint8_t* setData) :
|
|
|
|
SpacePacketBase(setData) {
|
|
|
|
tmData = reinterpret_cast<TmPacketPointer*>(setData);
|
2016-06-15 23:48:41 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
TmPacketBase::~TmPacketBase() {
|
|
|
|
//Nothing to do.
|
|
|
|
}
|
|
|
|
|
|
|
|
uint8_t TmPacketBase::getService() {
|
2020-10-28 02:20:12 +01:00
|
|
|
return tmData->data_field.service_type;
|
2016-06-15 23:48:41 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
uint8_t TmPacketBase::getSubService() {
|
2020-10-28 02:20:12 +01:00
|
|
|
return tmData->data_field.service_subtype;
|
2016-06-15 23:48:41 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
uint8_t* TmPacketBase::getSourceData() {
|
2020-10-28 02:20:12 +01:00
|
|
|
return &tmData->data;
|
2016-06-15 23:48:41 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
uint16_t TmPacketBase::getSourceDataSize() {
|
2020-10-28 02:20:12 +01:00
|
|
|
return getPacketDataLength() - sizeof(tmData->data_field)
|
2018-07-12 16:29:32 +02:00
|
|
|
- CRC_SIZE + 1;
|
2016-06-15 23:48:41 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
uint16_t TmPacketBase::getErrorControl() {
|
2018-07-12 16:29:32 +02:00
|
|
|
uint32_t size = getSourceDataSize() + CRC_SIZE;
|
2020-10-28 02:20:12 +01:00
|
|
|
uint8_t* p_to_buffer = &tmData->data;
|
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 TmPacketBase::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 = getSourceDataSize();
|
|
|
|
getSourceData()[size] = (crc & 0XFF00) >> 8; // CRCH
|
|
|
|
getSourceData()[size + 1] = (crc) & 0X00FF; // CRCL
|
2016-06-15 23:48:41 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void TmPacketBase::setData(const uint8_t* p_Data) {
|
2018-07-12 16:29:32 +02:00
|
|
|
SpacePacketBase::setData(p_Data);
|
2020-10-28 02:20:12 +01:00
|
|
|
tmData = (TmPacketPointer*) p_Data;
|
2016-06-15 23:48:41 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void TmPacketBase::print() {
|
2020-10-28 02:20:12 +01:00
|
|
|
sif::debug << "TmPacketBase::print: " << std::endl;
|
|
|
|
arrayprinter::print(getWholeData(), getFullSize());
|
2016-06-15 23:48:41 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
bool TmPacketBase::checkAndSetStamper() {
|
|
|
|
if (timeStamper == NULL) {
|
2018-07-12 16:29:32 +02:00
|
|
|
timeStamper = objectManager->get<TimeStamperIF>(timeStamperId);
|
|
|
|
if (timeStamper == NULL) {
|
2020-04-23 19:13:18 +02:00
|
|
|
sif::error << "TmPacketBase::checkAndSetStamper: Stamper not found!"
|
2018-07-12 16:29:32 +02:00
|
|
|
<< std::endl;
|
2016-06-15 23:48:41 +02:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2018-07-12 16:29:32 +02:00
|
|
|
ReturnValue_t TmPacketBase::getPacketTime(timeval* timestamp) const {
|
2020-12-22 15:49:31 +01:00
|
|
|
size_t tempSize = 0;
|
2020-10-28 02:20:12 +01:00
|
|
|
return CCSDSTime::convertFromCcsds(timestamp, tmData->data_field.time,
|
|
|
|
&tempSize, sizeof(tmData->data_field.time));
|
2018-07-12 16:29:32 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
uint8_t* TmPacketBase::getPacketTimeRaw() const{
|
2020-10-28 02:20:12 +01:00
|
|
|
return tmData->data_field.time;
|
2018-07-12 16:29:32 +02:00
|
|
|
|
|
|
|
}
|
|
|
|
|
2020-10-28 02:20:12 +01:00
|
|
|
void TmPacketBase::initializeTmPacket(uint16_t apid, uint8_t service,
|
|
|
|
uint8_t subservice, uint8_t packetSubcounter) {
|
2018-07-12 16:29:32 +02:00
|
|
|
//Set primary header:
|
|
|
|
initSpacePacketHeader(false, true, apid);
|
|
|
|
//Set data Field Header:
|
|
|
|
//First, set to zero.
|
2020-10-28 02:20:12 +01:00
|
|
|
memset(&tmData->data_field, 0, sizeof(tmData->data_field));
|
|
|
|
|
2019-08-28 14:50:24 +02:00
|
|
|
// NOTE: In PUS-C, the PUS Version is 2 and specified for the first 4 bits.
|
2020-10-28 02:20:12 +01:00
|
|
|
// The other 4 bits of the first byte are the spacecraft time reference
|
|
|
|
// status. To change to PUS-C, set 0b00100000.
|
|
|
|
// Set CCSDS_secondary header flag to 0, version number to 001 and ack
|
|
|
|
// to 0000
|
|
|
|
tmData->data_field.version_type_ack = 0b00010000;
|
|
|
|
tmData->data_field.service_type = service;
|
|
|
|
tmData->data_field.service_subtype = subservice;
|
|
|
|
tmData->data_field.subcounter = packetSubcounter;
|
2018-07-12 16:29:32 +02:00
|
|
|
//Timestamp packet
|
|
|
|
if (checkAndSetStamper()) {
|
2020-10-28 02:20:12 +01:00
|
|
|
timeStamper->addTimeStamp(tmData->data_field.time,
|
|
|
|
sizeof(tmData->data_field.time));
|
2018-07-12 16:29:32 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void TmPacketBase::setSourceDataSize(uint16_t size) {
|
|
|
|
setPacketDataLength(size + sizeof(PUSTmDataFieldHeader) + CRC_SIZE - 1);
|
|
|
|
}
|
|
|
|
|
2020-10-28 02:20:12 +01:00
|
|
|
size_t TmPacketBase::getTimestampSize() const {
|
|
|
|
return sizeof(tmData->data_field.time);
|
2018-07-12 16:29:32 +02:00
|
|
|
}
|