498 lines
16 KiB
C++
498 lines
16 KiB
C++
#include "PlocHandler.h"
|
|
#include "OBSWConfig.h"
|
|
|
|
#include <fsfw/globalfunctions/CRC.h>
|
|
#include <fsfw/datapool/PoolReadGuard.h>
|
|
|
|
PlocHandler::PlocHandler(object_id_t objectId, object_id_t comIF, CookieIF * comCookie) :
|
|
DeviceHandlerBase(objectId, comIF, comCookie) {
|
|
if (comCookie == NULL) {
|
|
sif::error << "PlocHandler: Invalid com cookie" << std::endl;
|
|
}
|
|
}
|
|
|
|
PlocHandler::~PlocHandler() {
|
|
}
|
|
|
|
|
|
void PlocHandler::doStartUp(){
|
|
if(mode == _MODE_START_UP){
|
|
setMode(MODE_ON);
|
|
}
|
|
}
|
|
|
|
void PlocHandler::doShutDown(){
|
|
|
|
}
|
|
|
|
ReturnValue_t PlocHandler::buildNormalDeviceCommand(
|
|
DeviceCommandId_t * id) {
|
|
return RETURN_OK;
|
|
}
|
|
|
|
ReturnValue_t PlocHandler::buildTransitionDeviceCommand(
|
|
DeviceCommandId_t * id){
|
|
return HasReturnvaluesIF::RETURN_OK;
|
|
}
|
|
|
|
ReturnValue_t PlocHandler::buildCommandFromCommand(
|
|
DeviceCommandId_t deviceCommand, const uint8_t * commandData,
|
|
size_t commandDataLen) {
|
|
switch(deviceCommand) {
|
|
case(PLOC::TC_MEM_WRITE): {
|
|
return prepareTcMemWriteCommand(commandData, commandDataLen);
|
|
}
|
|
case(PLOC::TC_MEM_READ): {
|
|
return prepareTcMemReadCommand(commandData, commandDataLen);
|
|
}
|
|
default:
|
|
sif::debug << "PlocHandler::buildCommandFromCommand: Command not implemented" << std::endl;
|
|
return DeviceHandlerIF::COMMAND_NOT_IMPLEMENTED;
|
|
}
|
|
return HasReturnvaluesIF::RETURN_FAILED;
|
|
}
|
|
|
|
void PlocHandler::fillCommandAndReplyMap() {
|
|
this->insertInCommandMap(PLOC::TC_MEM_WRITE);
|
|
this->insertInCommandMap(PLOC::TC_MEM_READ);
|
|
this->insertInReplyMap(PLOC::ACK_REPORT, 1, nullptr, PLOC::SIZE_ACK_REPORT);
|
|
this->insertInReplyMap(PLOC::EXE_REPORT, 3, nullptr, PLOC::SIZE_EXE_REPORT);
|
|
this->insertInReplyMap(PLOC::TM_MEMORY_READ_REPORT, 2, nullptr, PLOC::SIZE_TM_MEM_READ_REPORT);
|
|
}
|
|
|
|
ReturnValue_t PlocHandler::scanForReply(const uint8_t *start,
|
|
size_t remainingSize, DeviceCommandId_t *foundId, size_t *foundLen) {
|
|
|
|
ReturnValue_t result = RETURN_OK;
|
|
|
|
uint16_t apid = (*(start) << 8 | *(start + 1)) & APID_MASK;
|
|
|
|
switch(apid) {
|
|
case(PLOC::APID_ACK_SUCCESS):
|
|
*foundLen = PLOC::SIZE_ACK_REPORT;
|
|
*foundId = PLOC::ACK_REPORT;
|
|
break;
|
|
case(PLOC::APID_ACK_FAILURE):
|
|
*foundLen = PLOC::SIZE_ACK_REPORT;
|
|
*foundId = PLOC::ACK_REPORT;
|
|
break;
|
|
case(PLOC::APID_TM_MEMORY_READ_REPORT):
|
|
*foundLen = PLOC::SIZE_TM_MEM_READ_REPORT;
|
|
*foundId = PLOC::TM_MEMORY_READ_REPORT;
|
|
break;
|
|
case(PLOC::APID_EXE_SUCCESS):
|
|
*foundLen = PLOC::SIZE_EXE_REPORT;
|
|
*foundId = PLOC::EXE_REPORT;
|
|
break;
|
|
case(PLOC::APID_EXE_FAILURE):
|
|
*foundLen = PLOC::SIZE_EXE_REPORT;
|
|
*foundId = PLOC::EXE_REPORT;
|
|
break;
|
|
default: {
|
|
sif::debug << "PlocHandler::scanForReply: Reply has invalid apid" << std::endl;
|
|
*foundLen = remainingSize;
|
|
return INVALID_APID;
|
|
}
|
|
}
|
|
|
|
/**
|
|
* This should normally never fail. However, this function is also responsible for incrementing
|
|
* the packet sequence count why it is called here.
|
|
*/
|
|
result = checkPacketSequenceCount(start);
|
|
|
|
return result;
|
|
}
|
|
|
|
ReturnValue_t PlocHandler::interpretDeviceReply(DeviceCommandId_t id,
|
|
const uint8_t *packet) {
|
|
|
|
ReturnValue_t result = RETURN_OK;
|
|
|
|
switch (id) {
|
|
case PLOC::ACK_REPORT: {
|
|
result = handleAckReport(packet);
|
|
break;
|
|
}
|
|
case (PLOC::TM_MEMORY_READ_REPORT): {
|
|
result = handleMemoryReadReport(packet);
|
|
break;
|
|
}
|
|
case (PLOC::EXE_REPORT): {
|
|
result = handleExecutionReport(packet);
|
|
break;
|
|
}
|
|
default: {
|
|
sif::debug << "PlocHandler::interpretDeviceReply: Unknown device reply id" << std::endl;
|
|
return DeviceHandlerIF::UNKNOWN_DEVICE_REPLY;
|
|
}
|
|
}
|
|
|
|
return result;
|
|
}
|
|
|
|
void PlocHandler::setNormalDatapoolEntriesInvalid(){
|
|
|
|
}
|
|
|
|
uint32_t PlocHandler::getTransitionDelayMs(Mode_t modeFrom, Mode_t modeTo){
|
|
return 500;
|
|
}
|
|
|
|
ReturnValue_t PlocHandler::initializeLocalDataPool(localpool::DataPool& localDataPoolMap,
|
|
LocalDataPoolManager& poolManager) {
|
|
|
|
return HasReturnvaluesIF::RETURN_OK;
|
|
}
|
|
|
|
void PlocHandler::setModeNormal() {
|
|
mode = MODE_NORMAL;
|
|
}
|
|
|
|
ReturnValue_t PlocHandler::prepareTcMemWriteCommand(const uint8_t * commandData,
|
|
size_t commandDataLen) {
|
|
const uint32_t memoryAddress = *(commandData) << 24 | *(commandData + 1) << 16
|
|
| *(commandData + 2) << 8 | *(commandData + 3);
|
|
const uint32_t memoryData = *(commandData + 4) << 24 | *(commandData + 5) << 16
|
|
| *(commandData + 6) << 8 | *(commandData + 7);
|
|
packetSequenceCount = (packetSequenceCount + 1) & PACKET_SEQUENCE_COUNT_MASK;
|
|
PLOC::TcMemWrite tcMemWrite(memoryAddress, memoryData, packetSequenceCount);
|
|
if (tcMemWrite.getFullSize() > PLOC::MAX_COMMAND_SIZE) {
|
|
sif::debug << "PlocHandler::prepareTcMemWriteCommand: Command too big" << std::endl;
|
|
return RETURN_FAILED;
|
|
}
|
|
memcpy(commandBuffer, tcMemWrite.getWholeData(), tcMemWrite.getFullSize());
|
|
rawPacket = commandBuffer;
|
|
rawPacketLen = tcMemWrite.getFullSize();
|
|
nextReplyId = PLOC::ACK_REPORT;
|
|
return RETURN_OK;
|
|
}
|
|
|
|
ReturnValue_t PlocHandler::prepareTcMemReadCommand(const uint8_t * commandData,
|
|
size_t commandDataLen) {
|
|
const uint32_t memoryAddress = *(commandData) << 24 | *(commandData + 1) << 16
|
|
| *(commandData + 2) << 8 | *(commandData + 3);
|
|
packetSequenceCount = (packetSequenceCount + 1) & PACKET_SEQUENCE_COUNT_MASK;
|
|
PLOC::TcMemRead tcMemRead(memoryAddress, packetSequenceCount);
|
|
if (tcMemRead.getFullSize() > PLOC::MAX_COMMAND_SIZE) {
|
|
sif::debug << "PlocHandler::prepareTcMemReadCommand: Command too big" << std::endl;
|
|
return RETURN_FAILED;
|
|
}
|
|
memcpy(commandBuffer, tcMemRead.getWholeData(), tcMemRead.getFullSize());
|
|
rawPacket = commandBuffer;
|
|
rawPacketLen = tcMemRead.getFullSize();
|
|
nextReplyId = PLOC::ACK_REPORT;
|
|
|
|
return RETURN_OK;
|
|
}
|
|
|
|
ReturnValue_t PlocHandler::verifyPacket(const uint8_t* start, size_t foundLen) {
|
|
|
|
uint16_t receivedCrc = *(start + foundLen - 2) << 8 | *(start + foundLen - 1);
|
|
|
|
uint16_t recalculatedCrc = CRC::crc16ccitt(start, foundLen - 2);
|
|
|
|
if (receivedCrc != recalculatedCrc) {
|
|
return CRC_FAILURE;
|
|
}
|
|
|
|
return RETURN_OK;
|
|
}
|
|
|
|
ReturnValue_t PlocHandler::handleAckReport(const uint8_t* data) {
|
|
|
|
ReturnValue_t result = RETURN_OK;
|
|
|
|
result = verifyPacket(data, PLOC::SIZE_ACK_REPORT);
|
|
if(result == CRC_FAILURE) {
|
|
sif::error << "PlocHandler::handleAckReport: CRC failure" << std::endl;
|
|
nextReplyId = PLOC::NONE;
|
|
replyRawReplyIfnotWiretapped(data, PLOC::SIZE_ACK_REPORT);
|
|
triggerEvent(CRC_FAILURE_EVENT);
|
|
sendFailureReport(PLOC::ACK_REPORT, CRC_FAILURE);
|
|
disableAllReplies();
|
|
return IGNORE_REPLY_DATA;
|
|
}
|
|
|
|
uint16_t apid = (*(data) << 8 | *(data + 1)) & APID_MASK;
|
|
|
|
switch(apid) {
|
|
case PLOC::APID_ACK_FAILURE: {
|
|
//TODO: Interpretation of status field in acknowledgment report
|
|
sif::debug << "PlocHandler::handleAckReport: Received Ack failure report" << std::endl;
|
|
DeviceCommandId_t commandId = getPendingCommand();
|
|
if (commandId != DeviceHandlerIF::NO_COMMAND_ID) {
|
|
triggerEvent(ACK_FAILURE, commandId);
|
|
}
|
|
sendFailureReport(PLOC::ACK_REPORT, RECEIVED_ACK_FAILURE);
|
|
disableAllReplies();
|
|
nextReplyId = PLOC::NONE;
|
|
result = IGNORE_REPLY_DATA;
|
|
break;
|
|
}
|
|
case PLOC::APID_ACK_SUCCESS: {
|
|
setNextReplyId();
|
|
break;
|
|
}
|
|
default: {
|
|
sif::debug << "PlocHandler::handleAckReport: Invalid APID in Ack report" << std::endl;
|
|
result = RETURN_FAILED;
|
|
break;
|
|
}
|
|
}
|
|
|
|
return result;
|
|
}
|
|
|
|
ReturnValue_t PlocHandler::handleExecutionReport(const uint8_t* data) {
|
|
|
|
ReturnValue_t result = RETURN_OK;
|
|
|
|
result = verifyPacket(data, PLOC::SIZE_EXE_REPORT);
|
|
if(result == CRC_FAILURE) {
|
|
sif::error << "PlocHandler::handleExecutionReport: CRC failure" << std::endl;
|
|
nextReplyId = PLOC::NONE;
|
|
return result;
|
|
}
|
|
|
|
uint16_t apid = (*(data) << 8 | *(data + 1)) & APID_MASK;
|
|
|
|
switch (apid) {
|
|
case (PLOC::APID_EXE_SUCCESS): {
|
|
break;
|
|
}
|
|
case (PLOC::APID_EXE_FAILURE): {
|
|
//TODO: Interpretation of status field in execution report
|
|
sif::error << "PlocHandler::handleExecutionReport: Received execution failure report"
|
|
<< std::endl;
|
|
DeviceCommandId_t commandId = getPendingCommand();
|
|
if (commandId != DeviceHandlerIF::NO_COMMAND_ID) {
|
|
triggerEvent(EXE_FAILURE, commandId);
|
|
}
|
|
else {
|
|
sif::debug << "PlocHandler::handleExecutionReport: Unknown command id" << std::endl;
|
|
}
|
|
sendFailureReport(PLOC::EXE_REPORT, RECEIVED_EXE_FAILURE);
|
|
disableExeReportReply();
|
|
result = IGNORE_REPLY_DATA;
|
|
break;
|
|
}
|
|
default: {
|
|
sif::error << "PlocHandler::handleExecutionReport: Unknown APID" << std::endl;
|
|
result = RETURN_FAILED;
|
|
break;
|
|
}
|
|
}
|
|
|
|
nextReplyId = PLOC::NONE;
|
|
|
|
return result;
|
|
}
|
|
|
|
ReturnValue_t PlocHandler::handleMemoryReadReport(const uint8_t* data) {
|
|
|
|
ReturnValue_t result = RETURN_OK;
|
|
|
|
result = verifyPacket(data, PLOC::SIZE_TM_MEM_READ_REPORT);
|
|
|
|
if(result == CRC_FAILURE) {
|
|
sif::error << "PlocHandler::handleMemoryReadReport: Memory read report has invalid crc"
|
|
<< std::endl;
|
|
}
|
|
/** Send data to commanding queue */
|
|
handleDeviceTM(data + PLOC::DATA_FIELD_OFFSET, PLOC::SIZE_MEM_READ_REPORT_DATA,
|
|
PLOC::TM_MEMORY_READ_REPORT);
|
|
|
|
nextReplyId = PLOC::EXE_REPORT;
|
|
|
|
return result;
|
|
}
|
|
|
|
ReturnValue_t PlocHandler::enableReplyInReplyMap(DeviceCommandMap::iterator command,
|
|
uint8_t expectedReplies, bool useAlternateId,
|
|
DeviceCommandId_t alternateReplyID) {
|
|
|
|
ReturnValue_t result = RETURN_OK;
|
|
|
|
uint8_t enabledReplies = 0;
|
|
|
|
switch (command->first) {
|
|
case PLOC::TC_MEM_WRITE:
|
|
enabledReplies = 2;
|
|
break;
|
|
case PLOC::TC_MEM_READ: {
|
|
enabledReplies = 3;
|
|
result = DeviceHandlerBase::enableReplyInReplyMap(command, enabledReplies, true,
|
|
PLOC::TM_MEMORY_READ_REPORT);
|
|
if (result != RETURN_OK) {
|
|
sif::debug << "PlocHandler::enableReplyInReplyMap: Reply with id "
|
|
<< PLOC::TM_MEMORY_READ_REPORT << " not in replyMap" << std::endl;
|
|
}
|
|
break;
|
|
}
|
|
default:
|
|
sif::debug << "PlocHandler::enableReplyInReplyMap: Unknown command id" << std::endl;
|
|
break;
|
|
}
|
|
|
|
/**
|
|
* Every command causes at least one acknowledgment and one execution report. Therefore both
|
|
* replies will be enabled here.
|
|
*/
|
|
result = DeviceHandlerBase::enableReplyInReplyMap(command,
|
|
enabledReplies, true, PLOC::ACK_REPORT);
|
|
if (result != RETURN_OK) {
|
|
sif::debug << "PlocHandler::enableReplyInReplyMap: Reply with id " << PLOC::ACK_REPORT
|
|
<< " not in replyMap" << std::endl;
|
|
}
|
|
|
|
result = DeviceHandlerBase::enableReplyInReplyMap(command,
|
|
enabledReplies, true, PLOC::EXE_REPORT);
|
|
if (result != RETURN_OK) {
|
|
sif::debug << "PlocHandler::enableReplyInReplyMap: Reply with id " << PLOC::EXE_REPORT
|
|
<< " not in replyMap" << std::endl;
|
|
}
|
|
|
|
return RETURN_OK;
|
|
}
|
|
|
|
void PlocHandler::setNextReplyId() {
|
|
switch(getPendingCommand()) {
|
|
case PLOC::TC_MEM_READ:
|
|
nextReplyId = PLOC::TM_MEMORY_READ_REPORT;
|
|
break;
|
|
default:
|
|
/* If no telemetry is expected the next reply is always the execution report */
|
|
nextReplyId = PLOC::EXE_REPORT;
|
|
break;
|
|
}
|
|
}
|
|
size_t PlocHandler::getNextReplyLength(DeviceCommandId_t commandId){
|
|
|
|
size_t replyLen = 0;
|
|
|
|
if (nextReplyId == PLOC::NONE) {
|
|
return replyLen;
|
|
}
|
|
|
|
DeviceReplyIter iter = deviceReplyMap.find(nextReplyId);
|
|
if (iter != deviceReplyMap.end()) {
|
|
if (iter->second.delayCycles == 0) {
|
|
/* Reply inactive */
|
|
return replyLen;
|
|
}
|
|
replyLen = iter->second.replyLen;
|
|
}
|
|
else {
|
|
sif::debug << "PlocHandler::getNextReplyLength: No entry for reply with reply id "
|
|
<< std::hex << nextReplyId << " in deviceReplyMap" << std::endl;
|
|
}
|
|
|
|
return replyLen;
|
|
}
|
|
|
|
void PlocHandler::handleDeviceTM(const uint8_t* data, size_t dataSize, DeviceCommandId_t replyId) {
|
|
|
|
ReturnValue_t result = RETURN_OK;
|
|
|
|
if (wiretappingMode == RAW) {
|
|
/* Data already sent in doGetRead() */
|
|
return;
|
|
}
|
|
|
|
DeviceReplyMap::iterator iter = deviceReplyMap.find(replyId);
|
|
if (iter == deviceReplyMap.end()) {
|
|
sif::debug << "PlocHandler::handleDeviceTM: Unknown reply id" << std::endl;
|
|
return;
|
|
}
|
|
MessageQueueId_t queueId = iter->second.command->second.sendReplyTo;
|
|
|
|
if (queueId == NO_COMMANDER) {
|
|
return;
|
|
}
|
|
|
|
result = actionHelper.reportData(queueId, replyId, data, dataSize);
|
|
if (result != RETURN_OK) {
|
|
sif::debug << "PlocHandler::handleDeviceTM: Failed to report data" << std::endl;
|
|
}
|
|
}
|
|
|
|
void PlocHandler::disableAllReplies() {
|
|
|
|
DeviceReplyMap::iterator iter;
|
|
|
|
/* Disable ack reply */
|
|
iter = deviceReplyMap.find(PLOC::ACK_REPORT);
|
|
DeviceReplyInfo *info = &(iter->second);
|
|
info->delayCycles = 0;
|
|
info->command = deviceCommandMap.end();
|
|
|
|
DeviceCommandId_t commandId = getPendingCommand();
|
|
|
|
/* If the command expects a telemetry packet the appropriate tm reply will be disabled here */
|
|
switch (commandId) {
|
|
case PLOC::TC_MEM_WRITE:
|
|
break;
|
|
case PLOC::TC_MEM_READ: {
|
|
iter = deviceReplyMap.find(PLOC::TM_MEMORY_READ_REPORT);
|
|
info = &(iter->second);
|
|
info->delayCycles = 0;
|
|
info->command = deviceCommandMap.end();
|
|
break;
|
|
}
|
|
default: {
|
|
sif::debug << "PlocHandler::disableAllReplies: Unknown command id" << commandId
|
|
<< std::endl;
|
|
break;
|
|
}
|
|
}
|
|
|
|
/* We must always disable the execution report reply here */
|
|
disableExeReportReply();
|
|
}
|
|
|
|
void PlocHandler::sendFailureReport(DeviceCommandId_t replyId, ReturnValue_t status) {
|
|
|
|
DeviceReplyIter iter = deviceReplyMap.find(replyId);
|
|
|
|
if (iter == deviceReplyMap.end()) {
|
|
sif::debug << "PlocHandler::sendFailureReport: Reply not in reply map" << std::endl;
|
|
return;
|
|
}
|
|
|
|
DeviceCommandInfo* info = &(iter->second.command->second);
|
|
|
|
if (info == nullptr) {
|
|
sif::debug << "PlocHandler::sendFailureReport: Reply has no active command" << std::endl;
|
|
return;
|
|
}
|
|
|
|
if (info->sendReplyTo != NO_COMMANDER) {
|
|
actionHelper.finish(false, info->sendReplyTo, iter->first, status);
|
|
}
|
|
info->isExecuting = false;
|
|
}
|
|
|
|
void PlocHandler::disableExeReportReply() {
|
|
DeviceReplyIter iter = deviceReplyMap.find(PLOC::EXE_REPORT);
|
|
DeviceReplyInfo *info = &(iter->second);
|
|
info->delayCycles = 0;
|
|
info->command = deviceCommandMap.end();
|
|
/* Expected replies is set to one here. The value will set to 0 in replyToReply() */
|
|
info->command->second.expectedReplies = 0;
|
|
}
|
|
|
|
ReturnValue_t PlocHandler::checkPacketSequenceCount(const uint8_t* data) {
|
|
uint16_t receivedSequenceCount = (*(data + 2) << 8 | *(data + 3)) & PACKET_SEQUENCE_COUNT_MASK;
|
|
uint16_t expectedPacketSequenceCount = ((packetSequenceCount + 1) & PACKET_SEQUENCE_COUNT_MASK);
|
|
if (receivedSequenceCount != expectedPacketSequenceCount) {
|
|
sif::debug
|
|
<< "PlocHandler::checkPacketSequenceCount: Packet sequence count mismatch. "
|
|
<< std::endl;
|
|
sif::debug << "Received sequence count: " << receivedSequenceCount << ". OBSW sequence "
|
|
<< "count: " << expectedPacketSequenceCount << std::endl;
|
|
}
|
|
packetSequenceCount = receivedSequenceCount;
|
|
return RETURN_OK;
|
|
}
|