eive-obsw/bsp_q7s/devices/devicedefinitions/PlocMPSoCDefinitions.h

311 lines
11 KiB
C
Raw Normal View History

#ifndef MISSION_DEVICES_DEVICEDEFINITIONS_PLOCMPSOCDEFINITIONS_H_
#define MISSION_DEVICES_DEVICEDEFINITIONS_PLOCMPSOCDEFINITIONS_H_
2021-04-11 12:04:13 +02:00
#include <fsfw/tmtcpacket/SpacePacket.h>
#include <fsfw/globalfunctions/CRC.h>
2021-04-15 13:17:15 +02:00
#include <fsfw/serialize/SerializeAdapter.h>
2022-01-05 11:26:01 +01:00
#include <bsp_q7s/devices/ploc/PlocMPSoCHandler.h>
2021-04-11 12:04:13 +02:00
2022-01-05 11:26:01 +01:00
namespace mpsoc {
2021-04-11 12:04:13 +02:00
2022-01-05 11:26:01 +01:00
static const DeviceCommandId_t NONE = 0;
static const DeviceCommandId_t TC_MEM_WRITE = 1;
static const DeviceCommandId_t TC_MEM_READ = 2;
static const DeviceCommandId_t ACK_REPORT = 3;
static const DeviceCommandId_t EXE_REPORT = 5;
static const DeviceCommandId_t TM_MEMORY_READ_REPORT = 6;
static const DeviceCommandId_t TC_FLASHFOPEN = 7;
2022-01-03 08:01:55 +01:00
static const uint16_t SIZE_ACK_REPORT = 14;
static const uint16_t SIZE_EXE_REPORT = 14;
static const uint16_t SIZE_TM_MEM_READ_REPORT = 18;
/**
* SpacePacket apids of PLOC telecommands and telemetry.
*/
2022-01-05 11:26:01 +01:00
namespace apid {
static const uint16_t TC_MEM_WRITE = 0x114;
static const uint16_t TC_MEM_READ = 0x115;
static const uint16_t TC_FLASHFOPEN = 0x119;
static const uint16_t TM_MEMORY_READ_REPORT = 0x404;
static const uint16_t ACK_SUCCESS = 0x400;
static const uint16_t ACK_FAILURE = 0x401;
static const uint16_t EXE_SUCCESS = 0x402;
static const uint16_t EXE_FAILURE = 0x403;
}
/** Offset from first byte in space packet to first byte of data field */
2022-01-03 08:01:55 +01:00
static const uint8_t DATA_FIELD_OFFSET = 6;
/**
* The size of payload data which will be forwarded to the requesting object. e.g. PUS Service
* 8.
*/
static const uint8_t SIZE_MEM_READ_REPORT_DATA = 10;
2022-01-05 11:26:01 +01:00
static const size_t MAX_FILENAME_SIZE = 256;
2022-01-03 08:01:55 +01:00
/**
* PLOC space packet length for fixed size packets. This is the size of the whole packet data
* field. For the length field in the space packet this size will be substracted by one.
*/
static const uint16_t LENGTH_TC_MEM_WRITE = 12;
static const uint16_t LENGTH_TC_MEM_READ = 8;
static const size_t MAX_REPLY_SIZE = SIZE_TM_MEM_READ_REPORT;
static const size_t MAX_COMMAND_SIZE = 18;
2022-01-05 11:26:01 +01:00
/**
* @breif Abstract base class for TC space packet of MPSoC.
*/
class TcBase : public SpacePacket, public HasReturnvaluesIF {
public:
// Initial length field of space packet. Will always be updated when packet is created.
static const uint16_t INIT_LENGTH = 1;
static const uint8_t INTERFACE_ID = CLASS_ID::MPSOC_CMD;
//! [EXPORT] : [COMMENT] Received command with invalid length
static const ReturnValue_t INVALID_LENGTH = MAKE_RETURN_CODE(0xA0);
/**
* @brief Constructor
*
* @param sequenceCount Sequence count of space packet which will be incremented with each
* sent and received packet.s
*/
TcBase(uint16_t apid, uint16_t sequenceCount) :
SpacePacket(INIT_LENGTH, true, apid, sequenceCount) {
}
/**
* @brief Function to initialitze the space packet
*
* @param commandData Pointer to command specific data
* @param commandDataLen Length of command data
*
* @return RETURN_OK if packet creation was successful, otherwise error return value
*/
ReturnValue_t createPacket(const uint8_t* commandData, size_t commandDataLen) {
ReturnValue_t result = RETURN_OK;
result = initPacket(commandData, commandDataLen);
if (result != RETURN_OK) {
return result;
}
result = addCrc();
if (result != RETURN_OK) {
return result;
}
return result;
}
protected:
/**
* @brief Must be overwritten by the child class to define the command specific parameters
*
* @param commandData Pointer to received command data
* @param commandDataLen Length of received command data
*/
virtual ReturnValue_t initPacket(const uint8_t* commandData, size_t commandDataLen) = 0;
private:
/**
* @brief Calculates and adds the CRC
*/
ReturnValue_t addCrc() {
ReturnValue_t result = RETURN_OK;
size_t serializedSize = 0;
uint32_t full_size = getFullSize();
uint16_t crc = CRC::crc16ccitt(getWholeData(), full_size - CRC_SIZE);
result = SerializeAdapter::serialize<uint16_t>(&crc,
this->localData.byteStream + full_size - CRC_SIZE, &serializedSize, sizeof(crc),
SerializeIF::Endianness::BIG);
if (result != RETURN_OK) {
sif::debug << "TcBase::addCrc: Failed to serialize crc field" << std::endl;
}
return result;
}
};
2022-01-03 08:01:55 +01:00
/**
* @brief This class helps to build the memory read command for the PLOC.
*
* @details The last two bytes of the packet data field contain a CRC calculated over the whole
* space packet. This is the CRC-16-CCITT as specified in
* ECSS-E-ST-70-41C Telemetry and telecommand packet utilization.
*/
class TcMemRead: public SpacePacket {
public:
2021-04-11 12:04:13 +02:00
/**
2022-01-03 08:01:55 +01:00
* @brief Constructor
*
* @param memAddr The memory address to read from.
2021-04-11 12:04:13 +02:00
*/
2022-01-03 08:01:55 +01:00
TcMemRead(const uint32_t memAddr, uint16_t sequenceCount) :
2022-01-05 11:26:01 +01:00
SpacePacket(LENGTH_TC_MEM_READ - 1, true, apid::TC_MEM_READ, sequenceCount) {
2022-01-03 08:01:55 +01:00
fillPacketDataField(&memAddr);
}
2021-04-11 12:04:13 +02:00
2022-01-03 08:01:55 +01:00
private:
2022-01-03 08:01:55 +01:00
static const uint8_t OFFSET_ADDRESS = 0;
static const uint8_t OFFSET_MEM_LEN_FIELD = 4;
static const uint8_t CRC_OFFSET = 6;
const uint16_t MEM_LEN = 1;
2021-04-11 12:04:13 +02:00
/**
2022-01-03 08:01:55 +01:00
* @brief This function builds the packet data field for the mem read command.
*
* @param memAddr Pointer to the memory address to read from.
2021-04-11 12:04:13 +02:00
*/
2022-01-03 08:01:55 +01:00
void fillPacketDataField(const uint32_t* memAddr) {
ReturnValue_t result = HasReturnvaluesIF::RETURN_OK;
size_t serializedSize = 0;
result = SerializeAdapter::serialize<uint32_t>(memAddr,
this->localData.fields.buffer + OFFSET_ADDRESS, &serializedSize, sizeof(*memAddr),
SerializeIF::Endianness::BIG);
if (result != HasReturnvaluesIF::RETURN_OK) {
sif::debug << "TcMemRead::fillPacketDataField: Failed to serialize address field"
<< std::endl;
}
result = SerializeAdapter::serialize<uint16_t>(&MEM_LEN,
this->localData.fields.buffer + OFFSET_MEM_LEN_FIELD, &serializedSize,
sizeof(MEM_LEN), SerializeIF::Endianness::BIG);
if (result != HasReturnvaluesIF::RETURN_OK) {
sif::debug << "TcMemRead::fillPacketDataField: Failed to serialize mem len field"
<< std::endl;
}
// Calculate crc
uint16_t crc = CRC::crc16ccitt(this->localData.byteStream,
sizeof(CCSDSPrimaryHeader) + LENGTH_TC_MEM_READ - CRC_SIZE);
// Add crc to packet data field of space packet */
result = SerializeAdapter::serialize<uint16_t>(&crc,
this->localData.fields.buffer + CRC_OFFSET, &serializedSize, sizeof(crc),
SerializeIF::Endianness::BIG);
if (result != HasReturnvaluesIF::RETURN_OK) {
sif::debug << "TcMemRead::fillPacketDataField: Failed to serialize crc field"
<< std::endl;
}
}
};
/**
* @brief This class helps to generate the space packet to write to a memory address within
* the PLOC.
* @details The last two bytes of the packet data field contain a CRC calculated over the whole
* space packet. This is the CRC-16-CCITT as specified in
* ECSS-E-ST-70-41C Telemetry and telecommand packet utilization.
*/
class TcMemWrite: public SpacePacket {
public:
2021-04-11 12:04:13 +02:00
/**
2022-01-03 08:01:55 +01:00
* @brief Constructor
2021-04-11 12:04:13 +02:00
*
2022-01-03 08:01:55 +01:00
* @param memAddr The PLOC memory address where to write to.
* @param memoryData The data to write to the specified memory address.
* @param sequenceCount The subsequence count. Must be incremented with each new packet.
2021-04-11 12:04:13 +02:00
*/
2022-01-03 08:01:55 +01:00
TcMemWrite(const uint32_t memAddr, const uint32_t memoryData, uint16_t sequenceCount) :
2022-01-05 11:26:01 +01:00
SpacePacket(LENGTH_TC_MEM_WRITE - 1, true, apid::TC_MEM_WRITE, sequenceCount) {
2022-01-03 08:01:55 +01:00
fillPacketDataField(&memAddr, &memoryData);
2021-04-26 11:28:19 +02:00
}
2021-04-11 12:04:13 +02:00
2022-01-03 08:01:55 +01:00
private:
2021-04-11 12:04:13 +02:00
2022-01-05 11:26:01 +01:00
/** Offsets from base address of packet data field */
static const uint8_t OFFSET_MEM_LEN_FIELD = 4;
static const uint8_t OFFSET_MEM_DATA_FIELD = 6;
static const uint8_t CRC_OFFSET = 10;
2021-04-11 12:04:13 +02:00
/**
2022-01-03 08:01:55 +01:00
* @brief This function builds the packet data field for the mem write command.
*
* @param memAddrPtr Pointer to the PLOC memory address where to write to.
* @param memoryDataPtr Pointer to the memoryData to write
2021-04-11 12:04:13 +02:00
*/
2022-01-03 08:01:55 +01:00
void fillPacketDataField(const uint32_t* memAddrPtr, const uint32_t* memoryDataPtr) {
/* Add memAddr to packet data field */
size_t serializedSize = 0;
uint8_t* memoryAddressPos = this->localData.fields.buffer;
SerializeAdapter::serialize<uint32_t>(memAddrPtr, &memoryAddressPos, &serializedSize,
sizeof(*memAddrPtr), SerializeIF::Endianness::BIG);
/* Add memLen to packet data field */
this->localData.fields.buffer[OFFSET_MEM_LEN_FIELD] = 1;
this->localData.fields.buffer[OFFSET_MEM_LEN_FIELD + 1] = 0;
/* Add memData to packet data field */
serializedSize = 0;
uint8_t* memoryDataPos = this->localData.fields.buffer + OFFSET_MEM_DATA_FIELD;
SerializeAdapter::serialize<uint32_t>(memoryDataPtr, &memoryDataPos, &serializedSize,
2021-04-15 13:17:15 +02:00
sizeof(*memoryDataPtr), SerializeIF::Endianness::BIG);
2022-01-03 08:01:55 +01:00
/* Calculate crc */
uint16_t crc = CRC::crc16ccitt(this->localData.byteStream,
sizeof(CCSDSPrimaryHeader) + LENGTH_TC_MEM_WRITE - CRC_SIZE);
2021-04-15 13:17:15 +02:00
2022-01-03 08:01:55 +01:00
serializedSize = 0;
uint8_t* crcPos = this->localData.fields.buffer + CRC_OFFSET;
/* Add crc to packet data field of space packet */
SerializeAdapter::serialize<uint16_t>(&crc, &crcPos, &serializedSize, sizeof(crc),
SerializeIF::Endianness::BIG);
}
2022-01-05 11:26:01 +01:00
};
2021-04-11 12:04:13 +02:00
2022-01-05 11:26:01 +01:00
/**
* @brief Class to generate the flash file open command
*/
class FlashFopen : public TcBase {
public:
/**
* @brief Constructor
*
* @param sequenceCount Packet sequence count which is incremented with each sent and received
* packet.
*/
FlashFopen(uint16_t sequenceCount) :
TcBase(apid::TC_FLASHFOPEN, sequenceCount) {
}
protected:
ReturnValue_t initPacket(const uint8_t* commandData, size_t commandDataLen) {
ReturnValue_t result = RETURN_OK;
result = lengthCheck(commandDataLen);
if (result != RETURN_OK) {
return result;
}
filename = std::string(reinterpret_cast<const char*>(commandData), commandDataLen - 1);
accessMode = *(commandData + commandDataLen);
truePacketLen = filename.size() + sizeof(accessMode) + CRC_SIZE;
this->setPacketDataLength(truePacketLen - 1);
std::memcpy(this->getPacketData(), filename.c_str(),
truePacketLen - CRC_SIZE - sizeof(accessMode));
std::memcpy(this->getPacketData() + truePacketLen - CRC_SIZE, &accessMode,
sizeof(accessMode));
return RETURN_OK;
}
private:
uint8_t accessMode = 0;
std::string filename;
uint16_t truePacketLen = 0;
/**
* @brief Check length of received command
*/
ReturnValue_t lengthCheck(size_t commandDataLen) {
if (commandDataLen > MAX_FILENAME_SIZE + sizeof(accessMode)) {
return INVALID_LENGTH;
}
return RETURN_OK;
}
2022-01-03 08:01:55 +01:00
};
2021-04-11 12:04:13 +02:00
}
#endif /* MISSION_DEVICES_DEVICEDEFINITIONS_PLOCMPSOCDEFINITIONS_H_ */