From f906605097465daa0f3d300de5bfa7ade1a11384 Mon Sep 17 00:00:00 2001 From: Robin Mueller Date: Mon, 12 Apr 2021 23:55:33 +0200 Subject: [PATCH] addes pus packet c implementation --- timemanager/TimeStamperIF.h | 5 +- tmtcpacket/pus/TmPacketBase.cpp | 9 +- tmtcpacket/pus/TmPacketBase.h | 5 +- tmtcpacket/pus/TmPacketPusA.cpp | 24 +---- tmtcpacket/pus/TmPacketPusA.h | 11 +-- tmtcpacket/pus/TmPacketPusC.cpp | 86 +++++++++++++++++ tmtcpacket/pus/TmPacketPusC.h | 127 ++++++++++++++++++++++++++ tmtcpacket/pus/TmPacketStoredPusC.cpp | 2 + tmtcpacket/pus/TmPacketStoredPusC.h | 8 ++ 9 files changed, 237 insertions(+), 40 deletions(-) create mode 100644 tmtcpacket/pus/TmPacketStoredPusC.cpp create mode 100644 tmtcpacket/pus/TmPacketStoredPusC.h diff --git a/timemanager/TimeStamperIF.h b/timemanager/TimeStamperIF.h index 57b7f0149..1c4ada604 100644 --- a/timemanager/TimeStamperIF.h +++ b/timemanager/TimeStamperIF.h @@ -1,6 +1,7 @@ #ifndef FSFW_TIMEMANAGER_TIMESTAMPERIF_H_ #define FSFW_TIMEMANAGER_TIMESTAMPERIF_H_ +#include #include "../returnvalues/HasReturnvaluesIF.h" /** @@ -16,8 +17,8 @@ public: //! This is a mission-specific constant and determines the total //! size reserved for timestamps. - //! TODO: Default define in FSFWConfig ? - static const uint8_t MISSION_TIMESTAMP_SIZE = 8; + static const uint8_t MISSION_TIMESTAMP_SIZE = fsfwconfig::FSFW_MISSION_TIMESTAMP_SIZE; + virtual ReturnValue_t addTimeStamp(uint8_t* buffer, const uint8_t maxSize) = 0; virtual ~TimeStamperIF() {} diff --git a/tmtcpacket/pus/TmPacketBase.cpp b/tmtcpacket/pus/TmPacketBase.cpp index 2dfef258c..25193c928 100644 --- a/tmtcpacket/pus/TmPacketBase.cpp +++ b/tmtcpacket/pus/TmPacketBase.cpp @@ -38,7 +38,11 @@ void TmPacketBase::setErrorControl() { getSourceData()[size + 1] = (crc) & 0X00FF; // CRCL } - +ReturnValue_t TmPacketBase::getPacketTime(timeval* timestamp) const { + size_t tempSize = 0; + return CCSDSTime::convertFromCcsds(timestamp, getPacketTimeRaw(), + &tempSize, getTimestampSize()); +} void TmPacketBase::print() { #if FSFW_CPP_OSTREAM_ENABLED == 1 @@ -52,8 +56,7 @@ bool TmPacketBase::checkAndSetStamper() { timeStamper = objectManager->get(timeStamperId); if (timeStamper == NULL) { #if FSFW_CPP_OSTREAM_ENABLED == 1 - sif::Warning << "TmPacketBase::checkAndSetStamper: Stamper not found!" - << std::endl; + sif::warning << "TmPacketBase::checkAndSetStamper: Stamper not found!" << std::endl; #else sif::printWarning("TmPacketBase::checkAndSetStamper: Stamper not found!\n"); #endif diff --git a/tmtcpacket/pus/TmPacketBase.h b/tmtcpacket/pus/TmPacketBase.h index 09a6ca547..6925e99b9 100644 --- a/tmtcpacket/pus/TmPacketBase.h +++ b/tmtcpacket/pus/TmPacketBase.h @@ -29,8 +29,7 @@ public: //! Maximum size of a TM Packet in this mission. //! TODO: Make this dependant on a config variable. static const uint32_t MISSION_TM_PACKET_MAX_SIZE = 2048; - //! First byte of secondary header for PUS-A packets. - //! TODO: Maybe also support PUS-C via config? + //! First four bits of first byte of secondary header static const uint8_t VERSION_NUMBER_BYTE = 0b00010000; /** @@ -87,7 +86,7 @@ public: * timeval format. * @return Converted timestamp of packet. */ - virtual ReturnValue_t getPacketTime(timeval* timestamp) const = 0; + virtual ReturnValue_t getPacketTime(timeval* timestamp) const; /** * Returns a raw pointer to the beginning of the time field. * @return Raw pointer to time field. diff --git a/tmtcpacket/pus/TmPacketPusA.cpp b/tmtcpacket/pus/TmPacketPusA.cpp index 841f7e402..d96f6aa79 100644 --- a/tmtcpacket/pus/TmPacketPusA.cpp +++ b/tmtcpacket/pus/TmPacketPusA.cpp @@ -49,27 +49,7 @@ uint16_t TmPacketPusA::getDataFieldSize() { return sizeof(PUSTmDataFieldHeaderPusA); } -bool TmPacketPusA::checkAndSetStamper() { - if (timeStamper == NULL) { - timeStamper = objectManager->get(timeStamperId); - if (timeStamper == NULL) { -#if FSFW_CPP_OSTREAM_ENABLED == 1 - sif::error << "TmPacketPusA::checkAndSetStamper: Stamper not found!" - << std::endl; -#endif - return false; - } - } - return true; -} - -ReturnValue_t TmPacketPusA::getPacketTime(timeval* timestamp) const { - size_t tempSize = 0; - return CCSDSTime::convertFromCcsds(timestamp, tmData->data_field.time, - &tempSize, sizeof(tmData->data_field.time)); -} - -uint8_t* TmPacketPusA::getPacketTimeRaw() const{ +uint8_t* TmPacketPusA::getPacketTimeRaw() const { return tmData->data_field.time; } @@ -92,7 +72,7 @@ void TmPacketPusA::initializeTmPacket(uint16_t apid, uint8_t service, tmData->data_field.service_subtype = subservice; tmData->data_field.subcounter = packetSubcounter; //Timestamp packet - if (checkAndSetStamper()) { + if (TmPacketBase::checkAndSetStamper()) { timeStamper->addTimeStamp(tmData->data_field.time, sizeof(tmData->data_field.time)); } diff --git a/tmtcpacket/pus/TmPacketPusA.h b/tmtcpacket/pus/TmPacketPusA.h index 21bdfd959..dd9a5d090 100644 --- a/tmtcpacket/pus/TmPacketPusA.h +++ b/tmtcpacket/pus/TmPacketPusA.h @@ -53,9 +53,6 @@ public: //! Maximum size of a TM Packet in this mission. //! TODO: Make this dependant on a config variable. static const uint32_t MISSION_TM_PACKET_MAX_SIZE = 2048; - //! First byte of secondary header for PUS-A packets. - //! TODO: Maybe also support PUS-C via config? - static const uint8_t VERSION_NUMBER_BYTE_PUS_A = 0b00010000; /** * This is the default constructor. @@ -76,19 +73,13 @@ public: uint16_t getSourceDataSize() override; uint16_t getDataFieldSize() override; - /** - * Interprets the "time"-field in the secondary header and returns it in - * timeval format. - * @return Converted timestamp of packet. - */ - ReturnValue_t getPacketTime(timeval* timestamp) const override; /** * Returns a raw pointer to the beginning of the time field. * @return Raw pointer to time field. */ uint8_t* getPacketTimeRaw() const override; - size_t getTimestampSize() const override; + size_t getPacketMinimumSize() const override; protected: diff --git a/tmtcpacket/pus/TmPacketPusC.cpp b/tmtcpacket/pus/TmPacketPusC.cpp index 99f508255..e6aa50dda 100644 --- a/tmtcpacket/pus/TmPacketPusC.cpp +++ b/tmtcpacket/pus/TmPacketPusC.cpp @@ -1,2 +1,88 @@ #include "TmPacketPusC.h" +#include "TmPacketBase.h" +#include "../../globalfunctions/CRC.h" +#include "../../globalfunctions/arrayprinter.h" +#include "../../objectmanager/ObjectManagerIF.h" +#include "../../serviceinterface/ServiceInterfaceStream.h" +#include "../../timemanager/CCSDSTime.h" + +#include + + +TmPacketPusC::TmPacketPusC(uint8_t* setData) : TmPacketBase(setData) { + tmData = reinterpret_cast(setData); +} + +TmPacketPusC::~TmPacketPusC() { + //Nothing to do. +} + +uint8_t TmPacketPusC::getService() { + return tmData->dataField.serviceType; +} + +uint8_t TmPacketPusC::getSubService() { + return tmData->dataField.serviceSubtype; +} + +uint8_t* TmPacketPusC::getSourceData() { + return &tmData->data; +} + +uint16_t TmPacketPusC::getSourceDataSize() { + return getPacketDataLength() - sizeof(tmData->dataField) - CRC_SIZE + 1; +} + +void TmPacketPusC::setData(const uint8_t* p_Data) { + SpacePacketBase::setData(p_Data); + tmData = (TmPacketPointerPusC*) p_Data; +} + + +size_t TmPacketPusC::getPacketMinimumSize() const { + return TM_PACKET_MIN_SIZE; +} + +uint16_t TmPacketPusC::getDataFieldSize() { + return sizeof(PUSTmDataFieldHeaderPusC); +} + +uint8_t* TmPacketPusC::getPacketTimeRaw() const{ + return tmData->dataField.time; + +} + +void TmPacketPusC::initializeTmPacket(uint16_t apid, uint8_t service, + uint8_t subservice, uint16_t packetSubcounter, uint16_t destinationId = 0, + uint8_t timeRefField = 0) { + //Set primary header: + initSpacePacketHeader(false, true, apid); + //Set data Field Header: + //First, set to zero. + memset(&tmData->dataField, 0, sizeof(tmData->dataField)); + + // NOTE: In PUS-C, the PUS Version is 2 and specified for the first 4 bits. + // 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->dataField.versionTimeReferenceField = VERSION_NUMBER_BYTE | timeRefField; + tmData->dataField.serviceType = service; + tmData->dataField.serviceSubtype = subservice; + tmData->dataField.subcounter = packetSubcounter; + tmData->dataField.destinationId = destinationId; + //Timestamp packet + if (checkAndSetStamper()) { + timeStamper->addTimeStamp(tmData->dataField.time, + sizeof(tmData->dataField.time)); + } +} + +void TmPacketPusC::setSourceDataSize(uint16_t size) { + setPacketDataLength(size + sizeof(PUSTmDataFieldHeaderPusC) + CRC_SIZE - 1); +} + +size_t TmPacketPusC::getTimestampSize() const { + return sizeof(tmData->dataField.time); +} diff --git a/tmtcpacket/pus/TmPacketPusC.h b/tmtcpacket/pus/TmPacketPusC.h index fec435c49..975d134f7 100644 --- a/tmtcpacket/pus/TmPacketPusC.h +++ b/tmtcpacket/pus/TmPacketPusC.h @@ -1,8 +1,135 @@ #ifndef FSFW_TMTCPACKET_PUS_TMPACKETPUSC_H_ #define FSFW_TMTCPACKET_PUS_TMPACKETPUSC_H_ +#include "TmPacketBase.h" +#include "../SpacePacketBase.h" +#include "../../timemanager/TimeStamperIF.h" +#include "../../timemanager/Clock.h" +#include "../../objectmanager/SystemObjectIF.h" +namespace Factory{ +void setStaticFrameworkObjectIds(); +} +/** + * This struct defines a byte-wise structured PUS TM Data Field Header. + * Any optional fields in the header must be added or removed here. + * Currently, no Destination field is present, but an eigth-byte representation + * for a time tag. + * @ingroup tmtcpackets + */ +struct PUSTmDataFieldHeaderPusC { + uint8_t versionTimeReferenceField; + uint8_t serviceType; + uint8_t serviceSubtype; + uint16_t subcounter; + uint16_t destinationId; + uint8_t time[TimeStamperIF::MISSION_TIMESTAMP_SIZE]; +}; +/** + * This struct defines the data structure of a PUS Telecommand Packet when + * accessed via a pointer. + * @ingroup tmtcpackets + */ +struct TmPacketPointerPusC { + CCSDSPrimaryHeader primary; + PUSTmDataFieldHeaderPusC dataField; + uint8_t data; +}; + +/** + * PUS A packet implementation + * @ingroup tmtcpackets + */ +class TmPacketPusC: public TmPacketBase { + friend void (Factory::setStaticFrameworkObjectIds)(); +public: + /** + * This constant defines the minimum size of a valid PUS Telemetry Packet. + */ + static const uint32_t TM_PACKET_MIN_SIZE = (sizeof(CCSDSPrimaryHeader) + + sizeof(PUSTmDataFieldHeaderPusC) + 2); + //! Maximum size of a TM Packet in this mission. + //! TODO: Make this dependant on a config variable. + static const uint32_t MISSION_TM_PACKET_MAX_SIZE = 2048; + + /** + * This is the default constructor. + * It sets its internal data pointer to the address passed and also + * forwards the data pointer to the parent SpacePacketBase class. + * @param set_address The position where the packet data lies. + */ + TmPacketPusC( uint8_t* setData ); + /** + * This is the empty default destructor. + */ + virtual ~TmPacketPusC(); + + /* TmPacketBase implementations */ + uint8_t getService() override; + uint8_t getSubService() override; + uint8_t* getSourceData() override; + uint16_t getSourceDataSize() override; + uint16_t getDataFieldSize() override; + + /** + * Interprets the "time"-field in the secondary header and returns it in + * timeval format. + * @return Converted timestamp of packet. + */ + ReturnValue_t getPacketTime(timeval* timestamp) const override; + /** + * Returns a raw pointer to the beginning of the time field. + * @return Raw pointer to time field. + */ + uint8_t* getPacketTimeRaw() const override; + + size_t getTimestampSize() const override; + size_t getPacketMinimumSize() const override; + +protected: + /** + * A pointer to a structure which defines the data structure of + * the packet's data. + * + * To be hardware-safe, all elements are of byte size. + */ + TmPacketPointerPusC* tmData; + + /** + * Initializes the Tm Packet header. + * Does set the timestamp (to now), but not the error control field. + * @param apid APID used. + * @param service PUS Service + * @param subservice PUS Subservice + * @param packetSubcounter Additional subcounter used. + */ + void initializeTmPacket(uint16_t apid, uint8_t service, uint8_t subservice, + uint16_t packetSubcounter, uint16_t destinationId, uint8_t timeRefField); + + /** + * With this method, the packet data pointer can be redirected to another + * location. + * + * This call overwrites the parent's setData method to set both its + * @c tc_data pointer and the parent's @c data pointer. + * + * @param p_data A pointer to another PUS Telemetry Packet. + */ + void setData( const uint8_t* pData ); + + /** + * In case data was filled manually (almost never the case). + * @param size Size of source data (without CRC and data filed header!). + */ + void setSourceDataSize(uint16_t size); + + /** + * Checks if a time stamper is available and tries to set it if not. + * @return Returns false if setting failed. + */ + bool checkAndSetStamper(); +}; #endif /* FSFW_TMTCPACKET_PUS_TMPACKETPUSC_H_ */ diff --git a/tmtcpacket/pus/TmPacketStoredPusC.cpp b/tmtcpacket/pus/TmPacketStoredPusC.cpp new file mode 100644 index 000000000..bb27b80f3 --- /dev/null +++ b/tmtcpacket/pus/TmPacketStoredPusC.cpp @@ -0,0 +1,2 @@ +#include "TmPacketStoredPusC.h" + diff --git a/tmtcpacket/pus/TmPacketStoredPusC.h b/tmtcpacket/pus/TmPacketStoredPusC.h new file mode 100644 index 000000000..53b39414c --- /dev/null +++ b/tmtcpacket/pus/TmPacketStoredPusC.h @@ -0,0 +1,8 @@ +#ifndef FSFW_TMTCPACKET_PUS_TMPACKETSTOREDPUSC_H_ +#define FSFW_TMTCPACKET_PUS_TMPACKETSTOREDPUSC_H_ + + + + + +#endif /* FSFW_TMTCPACKET_PUS_TMPACKETSTOREDPUSC_H_ */