trying to fuse header / inc

This commit is contained in:
2021-07-19 16:25:51 +02:00
parent 7849b8e391
commit d47906e833
767 changed files with 117 additions and 135 deletions

View File

@ -0,0 +1,6 @@
target_sources(${LIB_FSFW_NAME} PRIVATE
TcPacketBase.cpp
TcPacketPus.cpp
TcPacketStoredBase.cpp
TcPacketStoredPus.cpp
)

View File

@ -0,0 +1,20 @@
#include "fsfw/tmtcpacket/pus/tc/TcPacketBase.h"
#include "fsfw/globalfunctions/CRC.h"
#include "fsfw/globalfunctions/arrayprinter.h"
#include "fsfw/serviceinterface/ServiceInterface.h"
#include <cstring>
TcPacketBase::TcPacketBase(const uint8_t* setData): SpacePacketBase(setData) {}
TcPacketBase::~TcPacketBase() {}
void TcPacketBase::print() {
#if FSFW_CPP_OSTREAM_ENABLED == 1
sif::info << "TcPacketBase::print:" << std::endl;
#else
sif::printInfo("TcPacketBase::print:\n");
#endif
arrayprinter::print(getWholeData(), getFullSize());
}

View File

@ -0,0 +1,150 @@
#ifndef TMTCPACKET_PUS_TCPACKETBASE_H_
#define TMTCPACKET_PUS_TCPACKETBASE_H_
#include "fsfw/tmtcpacket/SpacePacketBase.h"
#include <cstddef>
/**
* This class is the basic data handler for any ECSS PUS Telecommand packet.
*
* In addition to #SpacePacketBase, the class provides methods to handle
* the standardized entries of the PUS TC Packet Data Field Header.
* It does not contain the packet data itself but a pointer to the
* data must be set on instantiation. An invalid pointer may cause
* damage, as no getter method checks data validity. Anyway, a NULL
* check can be performed by making use of the getWholeData method.
* @ingroup tmtcpackets
*/
class TcPacketBase : public SpacePacketBase {
friend class TcPacketStoredBase;
public:
enum AckField {
//! No acknowledgements are expected.
ACK_NONE = 0b0000,
//! Acknowledgements on acceptance are expected.
ACK_ACCEPTANCE = 0b0001,
//! Acknowledgements on start are expected.
ACK_START = 0b0010,
//! Acknowledgements on step are expected.
ACK_STEP = 0b0100,
//! Acknowledfgement on completion are expected.
ACK_COMPLETION = 0b1000
};
static constexpr uint8_t ACK_ALL = ACK_ACCEPTANCE | ACK_START | ACK_STEP |
ACK_COMPLETION;
/**
* 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 setData The position where the packet data lies.
*/
TcPacketBase( const uint8_t* setData );
/**
* This is the empty default destructor.
*/
virtual ~TcPacketBase();
/**
* This command returns the CCSDS Secondary Header Flag.
* It shall always be zero for PUS Packets. This is the
* highest bit of the first byte of the Data Field Header.
* @return the CCSDS Secondary Header Flag
*/
virtual uint8_t getSecondaryHeaderFlag() const = 0;
/**
* This command returns the TC Packet PUS Version Number.
* The version number of ECSS PUS 2003 is 1.
* It consists of the second to fourth highest bits of the
* first byte.
* @return
*/
virtual uint8_t getPusVersionNumber() const = 0;
/**
* This is a getter for the packet's Ack field, which are the lowest four
* bits of the first byte of the Data Field Header.
*
* It is packed in a uint8_t variable.
* @return The packet's PUS Ack field.
*/
virtual uint8_t getAcknowledgeFlags() const = 0;
/**
* This is a getter for the packet's PUS Service ID, which is the second
* byte of the Data Field Header.
* @return The packet's PUS Service ID.
*/
virtual uint8_t getService() const = 0;
/**
* This is a getter for the packet's PUS Service Subtype, which is the
* third byte of the Data Field Header.
* @return The packet's PUS Service Subtype.
*/
virtual uint8_t getSubService() const = 0;
/**
* The source ID can be used to have an additional identifier, e.g. for different ground
* station.
* @return
*/
virtual uint16_t getSourceId() const = 0;
/**
* This is a getter for a pointer to the packet's Application data.
*
* These are the bytes that follow after the Data Field Header. They form
* the packet's application data.
* @return A pointer to the PUS Application Data.
*/
virtual const uint8_t* getApplicationData() const = 0;
/**
* This method calculates the size of the PUS Application data field.
*
* It takes the information stored in the CCSDS Packet Data Length field
* and subtracts the Data Field Header size and the CRC size.
* @return The size of the PUS Application Data (without Error Control
* field)
*/
virtual uint16_t getApplicationDataSize() const = 0;
/**
* This getter returns the Error Control Field of the packet.
*
* The field is placed after any possible Application Data. If no
* Application Data is present there's still an Error Control field. It is
* supposed to be a 16bit-CRC.
* @return The PUS Error Control
*/
virtual uint16_t getErrorControl() const = 0;
/**
* With this method, the Error Control Field is updated to match the
* current content of the packet.
*/
virtual void setErrorControl() = 0;
/**
* Calculate full packet length from application data length.
* @param appDataLen
* @return
*/
virtual size_t calculateFullPacketLength(size_t appDataLen) const = 0;
/**
* This is a debugging helper method that prints the whole packet content
* to the screen.
*/
void print();
protected:
/**
* 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 Telecommand Packet.
*/
void setData( const uint8_t* pData ) = 0;
};
#endif /* TMTCPACKET_PUS_TCPACKETBASE_H_ */

View File

@ -0,0 +1,98 @@
#include "fsfw/tmtcpacket/pus/tc/TcPacketPus.h"
#include "fsfw/globalfunctions/CRC.h"
#include <cstring>
TcPacketPus::TcPacketPus(const uint8_t *setData): TcPacketBase(setData) {
tcData = reinterpret_cast<TcPacketPointer*>(const_cast<uint8_t*>(setData));
}
void TcPacketPus::initializeTcPacket(uint16_t apid, uint16_t sequenceCount,
uint8_t ack, uint8_t service, uint8_t subservice, uint16_t sourceId) {
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
tcData->dataField.versionTypeAck = 0b00010000;
tcData->dataField.versionTypeAck |= (ack & 0x0F);
tcData->dataField.serviceType = service;
tcData->dataField.serviceSubtype = subservice;
#if FSFW_USE_PUS_C_TELECOMMANDS == 1
tcData->dataField.sourceIdH = (sourceId >> 8) | 0xff;
tcData->dataField.sourceIdL = sourceId & 0xff;
#else
tcData->dataField.sourceId = sourceId;
#endif
}
uint8_t TcPacketPus::getService() const {
return tcData->dataField.serviceType;
}
uint8_t TcPacketPus::getSubService() const {
return tcData->dataField.serviceSubtype;
}
uint8_t TcPacketPus::getAcknowledgeFlags() const {
return tcData->dataField.versionTypeAck & 0b00001111;
}
const uint8_t* TcPacketPus::getApplicationData() const {
return &tcData->appData;
}
uint16_t TcPacketPus::getApplicationDataSize() const {
return getPacketDataLength() - sizeof(tcData->dataField) - CRC_SIZE + 1;
}
uint16_t TcPacketPus::getErrorControl() const {
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];
}
void TcPacketPus::setErrorControl() {
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
}
void TcPacketPus::setData(const uint8_t* pData) {
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));
}
uint8_t TcPacketPus::getSecondaryHeaderFlag() const {
#if FSFW_USE_PUS_C_TELECOMMANDS == 1
// Does not exist for PUS C
return 0;
#else
return (tcData->dataField.versionTypeAck & 0b10000000) >> 7;
#endif
}
uint8_t TcPacketPus::getPusVersionNumber() const {
#if FSFW_USE_PUS_C_TELECOMMANDS == 1
return (tcData->dataField.versionTypeAck & 0b11110000) >> 4;
#else
return (tcData->dataField.versionTypeAck & 0b01110000) >> 4;
#endif
}
uint16_t TcPacketPus::getSourceId() const {
#if FSFW_USE_PUS_C_TELECOMMANDS == 1
return (tcData->dataField.sourceIdH << 8) | tcData->dataField.sourceIdL;
#else
return tcData->dataField.sourceId;
#endif
}
size_t TcPacketPus::calculateFullPacketLength(size_t appDataLen) const {
return sizeof(CCSDSPrimaryHeader) + sizeof(PUSTcDataFieldHeader) +
appDataLen + TcPacketBase::CRC_SIZE;
}

View File

@ -0,0 +1,90 @@
#ifndef FSFW_TMTCPACKET_PUS_TCPACKETPUSA_H_
#define FSFW_TMTCPACKET_PUS_TCPACKETPUSA_H_
#include "../../../FSFW.h"
#include "../../ccsds_header.h"
#include "TcPacketBase.h"
#include <cstdint>
/**
* This struct defines a byte-wise structured PUS TC A Data Field Header.
* Any optional fields in the header must be added or removed here.
* Currently, the Source Id field is present with one byte.
* @ingroup tmtcpackets
*/
struct PUSTcDataFieldHeader {
uint8_t versionTypeAck;
uint8_t serviceType;
uint8_t serviceSubtype;
#if FSFW_USE_PUS_C_TELECOMMANDS == 1
uint8_t sourceIdH;
uint8_t sourceIdL;
#else
uint8_t sourceId;
#endif
};
/**
* This struct defines the data structure of a PUS Telecommand A packet when
* accessed via a pointer.
* @ingroup tmtcpackets
*/
struct TcPacketPointer {
CCSDSPrimaryHeader primary;
PUSTcDataFieldHeader dataField;
uint8_t appData;
};
class TcPacketPus: public TcPacketBase {
public:
static const uint16_t TC_PACKET_MIN_SIZE = (sizeof(CCSDSPrimaryHeader) +
sizeof(PUSTcDataFieldHeader) + 2);
/**
* Initialize a PUS A telecommand packet which already exists. You can also
* create an empty (invalid) object by passing nullptr as the data pointer
* @param setData
*/
TcPacketPus(const uint8_t* setData);
// Base class overrides
uint8_t getSecondaryHeaderFlag() const override;
uint8_t getPusVersionNumber() const override;
uint8_t getAcknowledgeFlags() const override;
uint8_t getService() const override;
uint8_t getSubService() const override;
uint16_t getSourceId() const override;
const uint8_t* getApplicationData() const override;
uint16_t getApplicationDataSize() const override;
uint16_t getErrorControl() const override;
void setErrorControl() override;
size_t calculateFullPacketLength(size_t appDataLen) const override;
protected:
void setData(const uint8_t* pData) override;
/**
* Initializes the Tc Packet header.
* @param apid APID used.
* @param sequenceCount Sequence Count in the primary header.
* @param ack Which acknowledeges are expected from the receiver.
* @param service PUS Service
* @param subservice PUS Subservice
*/
void initializeTcPacket(uint16_t apid, uint16_t sequenceCount, uint8_t ack,
uint8_t service, uint8_t subservice, uint16_t sourceId = 0);
/**
* 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.
*/
TcPacketPointer* tcData = nullptr;
};
#endif /* FSFW_TMTCPACKET_PUS_TCPACKETPUSA_H_ */

View File

@ -0,0 +1,72 @@
#include "fsfw/tmtcpacket/pus/tc/TcPacketStoredBase.h"
#include "fsfw/objectmanager/ObjectManager.h"
#include "fsfw/serviceinterface/ServiceInterface.h"
#include "fsfw/objectmanager/frameworkObjects.h"
#include <cstring>
StorageManagerIF* TcPacketStoredBase::store = nullptr;
TcPacketStoredBase::TcPacketStoredBase() {
this->storeAddress.raw = StorageManagerIF::INVALID_ADDRESS;
this->checkAndSetStore();
}
TcPacketStoredBase::~TcPacketStoredBase() {
}
ReturnValue_t TcPacketStoredBase::getData(const uint8_t ** dataPtr,
size_t* dataSize) {
auto result = this->store->getData(storeAddress, dataPtr, dataSize);
if(result != HasReturnvaluesIF::RETURN_OK) {
#if FSFW_CPP_OSTREAM_ENABLED == 1
sif::warning << "TcPacketStoredBase: Could not get data!" << std::endl;
#else
sif::printWarning("TcPacketStoredBase: Could not get data!\n");
#endif
}
return result;
}
bool TcPacketStoredBase::checkAndSetStore() {
if (this->store == nullptr) {
this->store = ObjectManager::instance()->get<StorageManagerIF>(objects::TC_STORE);
if (this->store == nullptr) {
#if FSFW_CPP_OSTREAM_ENABLED == 1
sif::error << "TcPacketStoredBase::TcPacketStoredBase: TC Store not found!"
<< std::endl;
#endif
return false;
}
}
return true;
}
void TcPacketStoredBase::setStoreAddress(store_address_t setAddress) {
this->storeAddress = setAddress;
const uint8_t* tempData = nullptr;
size_t tempSize;
ReturnValue_t status = StorageManagerIF::RETURN_FAILED;
if (this->checkAndSetStore()) {
status = this->store->getData(this->storeAddress, &tempData, &tempSize);
}
TcPacketBase* tcPacketBase = this->getPacketBase();
if(tcPacketBase == nullptr) {
return;
}
if (status == StorageManagerIF::RETURN_OK) {
tcPacketBase->setData(tempData);
}
else {
tcPacketBase->setData(nullptr);
this->storeAddress.raw = StorageManagerIF::INVALID_ADDRESS;
}
}
store_address_t TcPacketStoredBase::getStoreAddress() {
return this->storeAddress;
}

View File

@ -0,0 +1,90 @@
#ifndef TMTCPACKET_PUS_TCPACKETSTORED_H_
#define TMTCPACKET_PUS_TCPACKETSTORED_H_
#include "TcPacketStoredIF.h"
#include "../../../storagemanager/StorageManagerIF.h"
/**
* This class generates a ECSS PUS Telecommand packet within a given
* intermediate storage.
* As most packets are passed between tasks with the help of a storage
* anyway, it seems logical to create a Packet-In-Storage access class
* which saves the user almost all storage handling operation.
* Packets can both be newly created with the class and be "linked" to
* packets in a store with the help of a storeAddress.
* @ingroup tmtcpackets
*/
class TcPacketStoredBase: public TcPacketStoredIF {
public:
/**
* This is a default constructor which does not set the data pointer to initialize
* with an empty cached store address
*/
TcPacketStoredBase();
/**
* Constructor to set to an existing store address.
* @param setAddress
*/
TcPacketStoredBase(store_address_t setAddress);
/**
* Another constructor to create a TcPacket from a raw packet stream.
* Takes the data and adds it unchecked to the TcStore.
* @param data Pointer to the complete TC Space Packet.
* @param Size size of the packet.
*/
TcPacketStoredBase(const uint8_t* data, uint32_t size);
virtual~ TcPacketStoredBase();
/**
* Getter function for the raw data.
* @param dataPtr [out] Pointer to the data pointer to set
* @param dataSize [out] Address of size to set.
* @return -@c RETURN_OK if data was retrieved successfully.
*/
ReturnValue_t getData(const uint8_t ** dataPtr, size_t* dataSize) override;
void setStoreAddress(store_address_t setAddress) override;
store_address_t getStoreAddress() override;
/**
* With this call, the packet is deleted.
* It removes itself from the store and sets its data pointer to NULL.
* @return returncode from deleting the data.
*/
virtual ReturnValue_t deletePacket() = 0;
/**
* This method performs a size check.
* It reads the stored size and compares it with the size entered in the
* packet header. This class is the optimal place for such a check as it
* has access to both the header data and the store.
* @return true if size is correct, false if packet is not registered in
* store or size is incorrect.
*/
virtual bool isSizeCorrect() = 0;
protected:
/**
* This is a pointer to the store all instances of the class use.
* If the store is not yet set (i.e. @c store is NULL), every constructor
* call tries to set it and throws an error message in case of failures.
* The default store is objects::TC_STORE.
*/
static StorageManagerIF* store;
/**
* The address where the packet data of the object instance is stored.
*/
store_address_t storeAddress;
/**
* A helper method to check if a store is assigned to the class.
* If not, the method tries to retrieve the store from the global
* ObjectManager.
* @return @li @c true if the store is linked or could be created.
* @li @c false otherwise.
*/
bool checkAndSetStore();
};
#endif /* TMTCPACKET_PUS_TcPacketStoredBase_H_ */

View File

@ -0,0 +1,38 @@
#ifndef FSFW_TMTCPACKET_PUS_TCPACKETSTOREDIF_H_
#define FSFW_TMTCPACKET_PUS_TCPACKETSTOREDIF_H_
#include "TcPacketBase.h"
#include "../../../storagemanager/storeAddress.h"
#include "../../../returnvalues/HasReturnvaluesIF.h"
class TcPacketStoredIF {
public:
virtual~TcPacketStoredIF() {};
/**
* With this call, the stored packet can be set to another packet in a store. This is useful
* if the packet is a class member and used for more than one packet.
* @param setAddress The new packet id to link to.
*/
virtual void setStoreAddress(store_address_t setAddress) = 0;
virtual store_address_t getStoreAddress() = 0;
/**
* Getter function for the raw data.
* @param dataPtr [out] Pointer to the data pointer to set
* @param dataSize [out] Address of size to set.
* @return -@c RETURN_OK if data was retrieved successfully.
*/
virtual ReturnValue_t getData(const uint8_t ** dataPtr, size_t* dataSize) = 0;
/**
* Get packet base pointer which can be used to get access to PUS packet fields
* @return
*/
virtual TcPacketBase* getPacketBase() = 0;
};
#endif /* FSFW_TMTCPACKET_PUS_TCPACKETSTOREDIF_H_ */

View File

@ -0,0 +1,79 @@
#include "fsfw/tmtcpacket/pus/tc/TcPacketStoredPus.h"
#include "fsfw/serviceinterface/ServiceInterface.h"
#include <cstring>
TcPacketStoredPus::TcPacketStoredPus(uint16_t apid, uint8_t service,
uint8_t subservice, uint8_t sequenceCount, const uint8_t* data,
size_t size, uint8_t ack) :
TcPacketPus(nullptr) {
this->storeAddress.raw = StorageManagerIF::INVALID_ADDRESS;
if (not this->checkAndSetStore()) {
return;
}
uint8_t* pData = nullptr;
ReturnValue_t returnValue = this->store->getFreeElement(&this->storeAddress,
(TC_PACKET_MIN_SIZE + size), &pData);
if (returnValue != this->store->RETURN_OK) {
#if FSFW_CPP_OSTREAM_ENABLED == 1
sif::warning << "TcPacketStoredBase: Could not get free element from store!"
<< std::endl;
#endif
return;
}
this->setData(pData);
initializeTcPacket(apid, sequenceCount, ack, service, subservice);
std::memcpy(&tcData->appData, data, size);
this->setPacketDataLength(
size + sizeof(PUSTcDataFieldHeader) + CRC_SIZE - 1);
this->setErrorControl();
}
TcPacketStoredPus::TcPacketStoredPus(): TcPacketStoredBase(), TcPacketPus(nullptr) {
}
TcPacketStoredPus::TcPacketStoredPus(store_address_t setAddress): TcPacketPus(nullptr) {
TcPacketStoredBase::setStoreAddress(setAddress);
}
TcPacketStoredPus::TcPacketStoredPus(const uint8_t* data, size_t size): TcPacketPus(data) {
if (this->getFullSize() != size) {
return;
}
if (this->checkAndSetStore()) {
ReturnValue_t status = store->addData(&storeAddress, data, size);
if (status != HasReturnvaluesIF::RETURN_OK) {
this->setData(nullptr);
}
const uint8_t* storePtr = nullptr;
// Repoint base data pointer to the data in the store.
store->getData(storeAddress, &storePtr, &size);
this->setData(storePtr);
}
}
ReturnValue_t TcPacketStoredPus::deletePacket() {
ReturnValue_t result = this->store->deleteData(this->storeAddress);
this->storeAddress.raw = StorageManagerIF::INVALID_ADDRESS;
this->setData(nullptr);
return result;
}
TcPacketBase* TcPacketStoredPus::getPacketBase() {
return this;
}
bool TcPacketStoredPus::isSizeCorrect() {
const uint8_t* temp_data = nullptr;
size_t temp_size;
ReturnValue_t status = this->store->getData(this->storeAddress, &temp_data,
&temp_size);
if (status == StorageManagerIF::RETURN_OK) {
if (this->getFullSize() == temp_size) {
return true;
}
}
return false;
}

View File

@ -0,0 +1,53 @@
#ifndef FSFW_TMTCPACKET_PUS_TCPACKETSTOREDPUSA_H_
#define FSFW_TMTCPACKET_PUS_TCPACKETSTOREDPUSA_H_
#include "TcPacketStoredBase.h"
#include "TcPacketPus.h"
class TcPacketStoredPus:
public TcPacketStoredBase,
public TcPacketPus {
public:
/**
* With this constructor, new space is allocated in the packet store and
* a new PUS Telecommand Packet is created there.
* Packet Application Data passed in data is copied into the packet.
* @param apid Sets the packet's APID field.
* @param service Sets the packet's Service ID field.
* This specifies the destination service.
* @param subservice Sets the packet's Service Subtype field.
* This specifies the destination sub-service.
* @param sequence_count Sets the packet's Source Sequence Count field.
* @param data The data to be copied to the Application Data Field.
* @param size The amount of data to be copied.
* @param ack Set's the packet's Ack field, which specifies
* number of verification packets returned
* for this command.
*/
TcPacketStoredPus(uint16_t apid, uint8_t service, uint8_t subservice,
uint8_t sequence_count = 0, const uint8_t* data = nullptr,
size_t size = 0, uint8_t ack = TcPacketBase::ACK_ALL);
/**
* Create stored packet with existing data.
* @param data
* @param size
*/
TcPacketStoredPus(const uint8_t* data, size_t size);
/**
* Create stored packet from existing packet in store
* @param setAddress
*/
TcPacketStoredPus(store_address_t setAddress);
TcPacketStoredPus();
ReturnValue_t deletePacket() override;
TcPacketBase* getPacketBase() override;
private:
bool isSizeCorrect() override;
};
#endif /* FSFW_TMTCPACKET_PUS_TCPACKETSTOREDPUSA_H_ */