eive-obsw/linux/devices/ploc/PlocSupervisorHandler.cpp
Robin Mueller ec880d4232
All checks were successful
EIVE/eive-obsw/pipeline/pr-develop This commit looks good
EIVE/eive-obsw/pipeline/head This commit looks good
apply auto-formatter
2022-04-04 13:40:45 +02:00

1519 lines
56 KiB
C++

#include "PlocSupervisorHandler.h"
#include <fsfw/datapool/PoolReadGuard.h>
#include <fsfw/globalfunctions/CRC.h>
#include <fsfw/timemanager/Clock.h>
#include <filesystem>
#include <fstream>
#include <sstream>
#include <string>
#include "OBSWConfig.h"
PlocSupervisorHandler::PlocSupervisorHandler(object_id_t objectId, object_id_t uartComIFid,
CookieIF* comCookie, Gpio uartIsolatorSwitch,
power::Switch_t powerSwitch)
: DeviceHandlerBase(objectId, uartComIFid, comCookie),
uartIsolatorSwitch(uartIsolatorSwitch),
hkset(this),
bootStatusReport(this),
latchupStatusReport(this),
powerSwitch(powerSwitch) {
if (comCookie == NULL) {
sif::error << "PlocSupervisorHandler: Invalid com cookie" << std::endl;
}
}
PlocSupervisorHandler::~PlocSupervisorHandler() {}
ReturnValue_t PlocSupervisorHandler::initialize() {
ReturnValue_t result = RETURN_OK;
result = DeviceHandlerBase::initialize();
if (result != RETURN_OK) {
return result;
}
uartComIf = dynamic_cast<UartComIF*>(communicationInterface);
if (uartComIf == nullptr) {
sif::warning << "PlocSupervisorHandler::initialize: Invalid uart com if" << std::endl;
return ObjectManagerIF::CHILD_INIT_FAILED;
}
#ifdef TE0720_1CFA
sdcMan = SdCardManager::instance();
#endif /* BOARD_TE0720 == 0 */
return result;
}
void PlocSupervisorHandler::doStartUp() {
setMode(_MODE_TO_ON);
uartIsolatorSwitch.pullHigh();
}
void PlocSupervisorHandler::doShutDown() {
setMode(_MODE_POWER_DOWN);
uartIsolatorSwitch.pullLow();
}
ReturnValue_t PlocSupervisorHandler::buildNormalDeviceCommand(DeviceCommandId_t* id) {
return NOTHING_TO_SEND;
}
ReturnValue_t PlocSupervisorHandler::buildTransitionDeviceCommand(DeviceCommandId_t* id) {
return NOTHING_TO_SEND;
}
ReturnValue_t PlocSupervisorHandler::buildCommandFromCommand(DeviceCommandId_t deviceCommand,
const uint8_t* commandData,
size_t commandDataLen) {
ReturnValue_t result = RETURN_FAILED;
switch (deviceCommand) {
case (supv::GET_HK_REPORT): {
prepareEmptyCmd(supv::APID_GET_HK_REPORT);
result = RETURN_OK;
break;
}
case (supv::RESTART_MPSOC): {
prepareEmptyCmd(supv::APID_RESTART_MPSOC);
result = RETURN_OK;
break;
}
case (supv::START_MPSOC): {
prepareEmptyCmd(supv::APID_START_MPSOC);
result = RETURN_OK;
break;
}
case (supv::SHUTDOWN_MPSOC): {
prepareEmptyCmd(supv::APID_SHUTWOWN_MPSOC);
result = RETURN_OK;
break;
}
case (supv::SEL_MPSOC_BOOT_IMAGE): {
prepareSelBootImageCmd(commandData);
result = RETURN_OK;
break;
}
case (supv::RESET_MPSOC): {
prepareEmptyCmd(supv::APID_RESET_MPSOC);
result = RETURN_OK;
break;
}
case (supv::SET_TIME_REF): {
result = prepareSetTimeRefCmd();
break;
}
case (supv::SET_BOOT_TIMEOUT): {
prepareSetBootTimeoutCmd(commandData);
result = RETURN_OK;
break;
}
case (supv::SET_MAX_RESTART_TRIES): {
prepareRestartTriesCmd(commandData);
result = RETURN_OK;
break;
}
case (supv::DISABLE_PERIOIC_HK_TRANSMISSION): {
prepareDisableHk();
result = RETURN_OK;
break;
}
case (supv::GET_BOOT_STATUS_REPORT): {
prepareEmptyCmd(supv::APID_GET_BOOT_STATUS_RPT);
result = RETURN_OK;
break;
}
case (supv::WATCHDOGS_ENABLE): {
prepareWatchdogsEnableCmd(commandData);
result = RETURN_OK;
break;
}
case (supv::WATCHDOGS_CONFIG_TIMEOUT): {
result = prepareWatchdogsConfigTimeoutCmd(commandData);
break;
}
case (supv::ENABLE_LATCHUP_ALERT): {
result = prepareLatchupConfigCmd(commandData, deviceCommand);
break;
}
case (supv::DISABLE_LATCHUP_ALERT): {
result = prepareLatchupConfigCmd(commandData, deviceCommand);
break;
}
case (supv::AUTO_CALIBRATE_ALERT): {
result = prepareAutoCalibrateAlertCmd(commandData);
break;
}
case (supv::SET_ALERT_LIMIT): {
result = prepareSetAlertLimitCmd(commandData);
break;
}
case (supv::SET_ALERT_IRQ_FILTER): {
result = prepareSetAlertIrqFilterCmd(commandData);
break;
}
case (supv::SET_ADC_SWEEP_PERIOD): {
result = prepareSetAdcSweetPeriodCmd(commandData);
break;
}
case (supv::SET_ADC_ENABLED_CHANNELS): {
prepareSetAdcEnabledChannelsCmd(commandData);
result = RETURN_OK;
break;
}
case (supv::SET_ADC_WINDOW_AND_STRIDE): {
prepareSetAdcWindowAndStrideCmd(commandData);
result = RETURN_OK;
break;
}
case (supv::SET_ADC_THRESHOLD): {
prepareSetAdcThresholdCmd(commandData);
result = RETURN_OK;
break;
}
case (supv::GET_LATCHUP_STATUS_REPORT): {
prepareEmptyCmd(supv::APID_GET_LATCHUP_STATUS_REPORT);
result = RETURN_OK;
break;
}
case (supv::COPY_ADC_DATA_TO_MRAM): {
prepareEmptyCmd(supv::APID_COPY_ADC_DATA_TO_MRAM);
result = RETURN_OK;
break;
}
case (supv::ENABLE_NVMS): {
prepareEnableNvmsCmd(commandData);
result = RETURN_OK;
break;
}
case (supv::SELECT_NVM): {
prepareSelectNvmCmd(commandData);
result = RETURN_OK;
break;
}
case (supv::RUN_AUTO_EM_TESTS): {
result = prepareRunAutoEmTest(commandData);
break;
}
case (supv::WIPE_MRAM): {
result = prepareWipeMramCmd(commandData);
break;
}
case (supv::FIRST_MRAM_DUMP):
case (supv::CONSECUTIVE_MRAM_DUMP):
result = prepareDumpMramCmd(commandData);
break;
case (supv::PRINT_CPU_STATS): {
preparePrintCpuStatsCmd(commandData);
result = RETURN_OK;
break;
}
case (supv::SET_DBG_VERBOSITY): {
prepareSetDbgVerbosityCmd(commandData);
result = RETURN_OK;
break;
}
case (supv::CAN_LOOPBACK_TEST): {
prepareEmptyCmd(supv::APID_CAN_LOOPBACK_TEST);
result = RETURN_OK;
break;
}
case (supv::SET_GPIO): {
prepareSetGpioCmd(commandData);
result = RETURN_OK;
break;
}
case (supv::READ_GPIO): {
prepareReadGpioCmd(commandData);
result = RETURN_OK;
break;
}
case (supv::RESTART_SUPERVISOR): {
prepareEmptyCmd(supv::APID_RESTART_SUPERVISOR);
result = RETURN_OK;
break;
}
case (supv::FACTORY_RESET_CLEAR_ALL): {
supv::FactoryReset packet(supv::FactoryReset::Op::CLEAR_ALL);
packetToOutBuffer(packet.getWholeData(), packet.getFullSize());
result = RETURN_OK;
break;
}
case (supv::FACTORY_RESET_CLEAR_MIRROR): {
supv::FactoryReset packet(supv::FactoryReset::Op::MIRROR_ENTRIES);
packetToOutBuffer(packet.getWholeData(), packet.getFullSize());
result = RETURN_OK;
break;
}
case (supv::FACTORY_RESET_CLEAR_CIRCULAR): {
supv::FactoryReset packet(supv::FactoryReset::Op::CIRCULAR_ENTRIES);
packetToOutBuffer(packet.getWholeData(), packet.getFullSize());
result = RETURN_OK;
break;
}
case (supv::UPDATE_AVAILABLE):
case (supv::UPDATE_IMAGE_DATA):
case (supv::UPDATE_VERIFY):
// Simply forward data from PLOC Updater to supervisor
std::memcpy(commandBuffer, commandData, commandDataLen);
rawPacket = commandBuffer;
rawPacketLen = commandDataLen;
nextReplyId = supv::ACK_REPORT;
result = RETURN_OK;
break;
default:
sif::debug << "PlocSupervisorHandler::buildCommandFromCommand: Command not implemented"
<< std::endl;
result = DeviceHandlerIF::COMMAND_NOT_IMPLEMENTED;
break;
}
if (result == RETURN_OK) {
/**
* Flushing the receive buffer to make sure there are no data left from a faulty reply.
*/
uartComIf->flushUartRxBuffer(comCookie);
}
return result;
}
void PlocSupervisorHandler::fillCommandAndReplyMap() {
this->insertInCommandMap(supv::GET_HK_REPORT);
this->insertInCommandMap(supv::RESTART_MPSOC);
this->insertInCommandMap(supv::START_MPSOC);
this->insertInCommandMap(supv::SHUTDOWN_MPSOC);
this->insertInCommandMap(supv::SEL_MPSOC_BOOT_IMAGE);
this->insertInCommandMap(supv::SET_BOOT_TIMEOUT);
this->insertInCommandMap(supv::SET_MAX_RESTART_TRIES);
this->insertInCommandMap(supv::RESET_MPSOC);
this->insertInCommandMap(supv::SET_TIME_REF);
this->insertInCommandMap(supv::DISABLE_PERIOIC_HK_TRANSMISSION);
this->insertInCommandMap(supv::GET_BOOT_STATUS_REPORT);
this->insertInCommandMap(supv::UPDATE_AVAILABLE);
this->insertInCommandMap(supv::UPDATE_VERIFY);
this->insertInCommandMap(supv::UPDATE_IMAGE_DATA);
this->insertInCommandMap(supv::WATCHDOGS_ENABLE);
this->insertInCommandMap(supv::WATCHDOGS_CONFIG_TIMEOUT);
this->insertInCommandMap(supv::ENABLE_LATCHUP_ALERT);
this->insertInCommandMap(supv::DISABLE_LATCHUP_ALERT);
this->insertInCommandMap(supv::AUTO_CALIBRATE_ALERT);
this->insertInCommandMap(supv::SET_ALERT_LIMIT);
this->insertInCommandMap(supv::SET_ALERT_IRQ_FILTER);
this->insertInCommandMap(supv::SET_ADC_SWEEP_PERIOD);
this->insertInCommandMap(supv::SET_ADC_ENABLED_CHANNELS);
this->insertInCommandMap(supv::SET_ADC_WINDOW_AND_STRIDE);
this->insertInCommandMap(supv::SET_ADC_THRESHOLD);
this->insertInCommandMap(supv::GET_LATCHUP_STATUS_REPORT);
this->insertInCommandMap(supv::COPY_ADC_DATA_TO_MRAM);
this->insertInCommandMap(supv::ENABLE_NVMS);
this->insertInCommandMap(supv::SELECT_NVM);
this->insertInCommandMap(supv::RUN_AUTO_EM_TESTS);
this->insertInCommandMap(supv::WIPE_MRAM);
this->insertInCommandMap(supv::PRINT_CPU_STATS);
this->insertInCommandMap(supv::SET_DBG_VERBOSITY);
this->insertInCommandMap(supv::SET_GPIO);
this->insertInCommandMap(supv::READ_GPIO);
this->insertInCommandMap(supv::RESTART_SUPERVISOR);
this->insertInCommandMap(supv::FACTORY_RESET_CLEAR_ALL);
this->insertInCommandMap(supv::FACTORY_RESET_CLEAR_MIRROR);
this->insertInCommandMap(supv::FACTORY_RESET_CLEAR_CIRCULAR);
this->insertInCommandMap(supv::CAN_LOOPBACK_TEST);
this->insertInCommandAndReplyMap(supv::FIRST_MRAM_DUMP, 3);
this->insertInCommandAndReplyMap(supv::CONSECUTIVE_MRAM_DUMP, 3);
this->insertInReplyMap(supv::ACK_REPORT, 3, nullptr, supv::SIZE_ACK_REPORT);
this->insertInReplyMap(supv::EXE_REPORT, 3, nullptr, supv::SIZE_EXE_REPORT);
this->insertInReplyMap(supv::HK_REPORT, 3, &hkset, supv::SIZE_HK_REPORT);
this->insertInReplyMap(supv::BOOT_STATUS_REPORT, 3, &bootStatusReport,
supv::SIZE_BOOT_STATUS_REPORT);
this->insertInReplyMap(supv::LATCHUP_REPORT, 3, &latchupStatusReport,
supv::SIZE_LATCHUP_STATUS_REPORT);
}
ReturnValue_t PlocSupervisorHandler::scanForReply(const uint8_t* start, size_t remainingSize,
DeviceCommandId_t* foundId, size_t* foundLen) {
if (nextReplyId == supv::FIRST_MRAM_DUMP) {
*foundId = supv::FIRST_MRAM_DUMP;
return parseMramPackets(start, remainingSize, foundLen);
} else if (nextReplyId == supv::CONSECUTIVE_MRAM_DUMP) {
*foundId = supv::CONSECUTIVE_MRAM_DUMP;
return parseMramPackets(start, remainingSize, foundLen);
}
ReturnValue_t result = RETURN_OK;
uint16_t apid = (*(start) << 8 | *(start + 1)) & APID_MASK;
switch (apid) {
case (supv::APID_ACK_SUCCESS):
*foundLen = supv::SIZE_ACK_REPORT;
*foundId = supv::ACK_REPORT;
break;
case (supv::APID_ACK_FAILURE):
*foundLen = supv::SIZE_ACK_REPORT;
*foundId = supv::ACK_REPORT;
break;
case (supv::APID_HK_REPORT):
*foundLen = supv::SIZE_HK_REPORT;
*foundId = supv::HK_REPORT;
break;
case (supv::APID_BOOT_STATUS_REPORT):
*foundLen = supv::SIZE_BOOT_STATUS_REPORT;
*foundId = supv::BOOT_STATUS_REPORT;
break;
case (supv::APID_LATCHUP_STATUS_REPORT):
*foundLen = supv::SIZE_LATCHUP_STATUS_REPORT;
*foundId = supv::LATCHUP_REPORT;
break;
case (supv::APID_EXE_SUCCESS):
*foundLen = supv::SIZE_EXE_REPORT;
*foundId = supv::EXE_REPORT;
break;
case (supv::APID_EXE_FAILURE):
*foundLen = supv::SIZE_EXE_REPORT;
*foundId = supv::EXE_REPORT;
break;
default: {
sif::debug << "PlocSupervisorHandler::scanForReply: Reply has invalid apid" << std::endl;
*foundLen = remainingSize;
return INVALID_APID;
}
}
return result;
}
ReturnValue_t PlocSupervisorHandler::getSwitches(const uint8_t** switches,
uint8_t* numberOfSwitches) {
if (powerSwitch == power::NO_SWITCH) {
return DeviceHandlerBase::NO_SWITCH;
}
*numberOfSwitches = 1;
*switches = &powerSwitch;
return RETURN_OK;
}
ReturnValue_t PlocSupervisorHandler::interpretDeviceReply(DeviceCommandId_t id,
const uint8_t* packet) {
ReturnValue_t result = RETURN_OK;
switch (id) {
case supv::ACK_REPORT: {
result = handleAckReport(packet);
break;
}
case (supv::HK_REPORT): {
result = handleHkReport(packet);
break;
}
case (supv::BOOT_STATUS_REPORT): {
result = handleBootStatusReport(packet);
break;
}
case (supv::LATCHUP_REPORT): {
result = handleLatchupStatusReport(packet);
break;
}
case (supv::FIRST_MRAM_DUMP):
case (supv::CONSECUTIVE_MRAM_DUMP):
result = handleMramDumpPacket(id);
break;
case (supv::EXE_REPORT): {
result = handleExecutionReport(packet);
break;
}
default: {
sif::debug << "PlocSupervisorHandler::interpretDeviceReply: Unknown device reply id"
<< std::endl;
return DeviceHandlerIF::UNKNOWN_DEVICE_REPLY;
}
}
return result;
}
void PlocSupervisorHandler::setNormalDatapoolEntriesInvalid() {}
uint32_t PlocSupervisorHandler::getTransitionDelayMs(Mode_t modeFrom, Mode_t modeTo) { return 500; }
ReturnValue_t PlocSupervisorHandler::initializeLocalDataPool(localpool::DataPool& localDataPoolMap,
LocalDataPoolManager& poolManager) {
localDataPoolMap.emplace(supv::NUM_TMS, new PoolEntry<uint32_t>({0}));
localDataPoolMap.emplace(supv::TEMP_PS, new PoolEntry<uint32_t>({0}));
localDataPoolMap.emplace(supv::TEMP_PL, new PoolEntry<uint32_t>({0}));
localDataPoolMap.emplace(supv::SOC_STATE, new PoolEntry<uint32_t>({0}));
localDataPoolMap.emplace(supv::NVM0_1_STATE, new PoolEntry<uint8_t>({0}));
localDataPoolMap.emplace(supv::NVM3_STATE, new PoolEntry<uint8_t>({0}));
localDataPoolMap.emplace(supv::MISSION_IO_STATE, new PoolEntry<uint8_t>({0}));
localDataPoolMap.emplace(supv::FMC_STATE, new PoolEntry<uint32_t>({0}));
localDataPoolMap.emplace(supv::NUM_TCS, new PoolEntry<uint32_t>({0}));
localDataPoolMap.emplace(supv::UPTIME, new PoolEntry<uint32_t>({0}));
localDataPoolMap.emplace(supv::CPULOAD, new PoolEntry<uint32_t>({0}));
localDataPoolMap.emplace(supv::AVAILABLEHEAP, new PoolEntry<uint32_t>({0}));
localDataPoolMap.emplace(supv::BOOT_SIGNAL, new PoolEntry<uint8_t>({0}));
localDataPoolMap.emplace(supv::RESET_COUNTER, new PoolEntry<uint8_t>({0}));
localDataPoolMap.emplace(supv::BOOT_AFTER_MS, new PoolEntry<uint32_t>({0}));
localDataPoolMap.emplace(supv::BOOT_TIMEOUT_MS, new PoolEntry<uint32_t>({0}));
localDataPoolMap.emplace(supv::ACTIVE_NVM, new PoolEntry<uint8_t>({0}));
localDataPoolMap.emplace(supv::BP0_STATE, new PoolEntry<uint8_t>({0}));
localDataPoolMap.emplace(supv::BP1_STATE, new PoolEntry<uint8_t>({0}));
localDataPoolMap.emplace(supv::BP2_STATE, new PoolEntry<uint8_t>({0}));
localDataPoolMap.emplace(supv::LATCHUP_ID, new PoolEntry<uint8_t>({0}));
localDataPoolMap.emplace(supv::CNT0, new PoolEntry<uint16_t>({0}));
localDataPoolMap.emplace(supv::CNT1, new PoolEntry<uint16_t>({0}));
localDataPoolMap.emplace(supv::CNT2, new PoolEntry<uint16_t>({0}));
localDataPoolMap.emplace(supv::CNT3, new PoolEntry<uint16_t>({0}));
localDataPoolMap.emplace(supv::CNT4, new PoolEntry<uint16_t>({0}));
localDataPoolMap.emplace(supv::CNT5, new PoolEntry<uint16_t>({0}));
localDataPoolMap.emplace(supv::CNT6, new PoolEntry<uint16_t>({0}));
localDataPoolMap.emplace(supv::LATCHUP_RPT_TIME_SEC, new PoolEntry<uint32_t>({0}));
localDataPoolMap.emplace(supv::LATCHUP_RPT_TIME_MIN, new PoolEntry<uint32_t>({0}));
localDataPoolMap.emplace(supv::LATCHUP_RPT_TIME_HOUR, new PoolEntry<uint32_t>({0}));
localDataPoolMap.emplace(supv::LATCHUP_RPT_TIME_DAY, new PoolEntry<uint32_t>({0}));
localDataPoolMap.emplace(supv::LATCHUP_RPT_TIME_MON, new PoolEntry<uint32_t>({0}));
localDataPoolMap.emplace(supv::LATCHUP_RPT_TIME_YEAR, new PoolEntry<uint32_t>({0}));
localDataPoolMap.emplace(supv::LATCHUP_RPT_TIME_MSEC, new PoolEntry<uint32_t>({0}));
localDataPoolMap.emplace(supv::LATCHUP_RPT_TIME_IS_SET, new PoolEntry<uint32_t>({0}));
return HasReturnvaluesIF::RETURN_OK;
}
ReturnValue_t PlocSupervisorHandler::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 supv::GET_HK_REPORT: {
enabledReplies = 3;
result =
DeviceHandlerBase::enableReplyInReplyMap(command, enabledReplies, true, supv::HK_REPORT);
if (result != RETURN_OK) {
sif::debug << "PlocSupervisorHandler::enableReplyInReplyMap: Reply with id "
<< supv::HK_REPORT << " not in replyMap" << std::endl;
}
break;
}
case supv::GET_BOOT_STATUS_REPORT: {
enabledReplies = 3;
result = DeviceHandlerBase::enableReplyInReplyMap(command, enabledReplies, true,
supv::BOOT_STATUS_REPORT);
if (result != RETURN_OK) {
sif::debug << "PlocSupervisorHandler::enableReplyInReplyMap: Reply with id "
<< supv::BOOT_STATUS_REPORT << " not in replyMap" << std::endl;
}
break;
}
case supv::GET_LATCHUP_STATUS_REPORT: {
enabledReplies = 3;
result = DeviceHandlerBase::enableReplyInReplyMap(command, enabledReplies, true,
supv::LATCHUP_REPORT);
if (result != RETURN_OK) {
sif::debug << "PlocSupervisorHandler::enableReplyInReplyMap: Reply with id "
<< supv::LATCHUP_REPORT << " not in replyMap" << std::endl;
}
break;
}
case supv::FIRST_MRAM_DUMP: {
enabledReplies = 2; // expected replies will be increased in handleMramDumpPacket
result = DeviceHandlerBase::enableReplyInReplyMap(command, enabledReplies, true,
supv::FIRST_MRAM_DUMP);
if (result != RETURN_OK) {
sif::debug << "PlocSupervisorHandler::enableReplyInReplyMap: Reply with id "
<< supv::FIRST_MRAM_DUMP << " not in replyMap" << std::endl;
}
break;
}
case supv::CONSECUTIVE_MRAM_DUMP: {
enabledReplies = 2; // expected replies will be increased in handleMramDumpPacket
result = DeviceHandlerBase::enableReplyInReplyMap(command, enabledReplies, true,
supv::CONSECUTIVE_MRAM_DUMP);
if (result != RETURN_OK) {
sif::debug << "PlocSupervisorHandler::enableReplyInReplyMap: Reply with id "
<< supv::CONSECUTIVE_MRAM_DUMP << " not in replyMap" << std::endl;
}
break;
}
case supv::RESTART_MPSOC:
case supv::START_MPSOC:
case supv::SHUTDOWN_MPSOC:
case supv::SEL_MPSOC_BOOT_IMAGE:
case supv::SET_BOOT_TIMEOUT:
case supv::SET_MAX_RESTART_TRIES:
case supv::RESET_MPSOC:
case supv::SET_TIME_REF:
case supv::UPDATE_AVAILABLE:
case supv::UPDATE_IMAGE_DATA:
case supv::UPDATE_VERIFY:
case supv::WATCHDOGS_ENABLE:
case supv::WATCHDOGS_CONFIG_TIMEOUT:
case supv::ENABLE_LATCHUP_ALERT:
case supv::DISABLE_LATCHUP_ALERT:
case supv::AUTO_CALIBRATE_ALERT:
case supv::SET_ALERT_LIMIT:
case supv::SET_ALERT_IRQ_FILTER:
case supv::SET_ADC_SWEEP_PERIOD:
case supv::SET_ADC_ENABLED_CHANNELS:
case supv::SET_ADC_WINDOW_AND_STRIDE:
case supv::SET_ADC_THRESHOLD:
case supv::COPY_ADC_DATA_TO_MRAM:
case supv::ENABLE_NVMS:
case supv::SELECT_NVM:
case supv::RUN_AUTO_EM_TESTS:
case supv::WIPE_MRAM:
case supv::SET_DBG_VERBOSITY:
case supv::CAN_LOOPBACK_TEST:
case supv::PRINT_CPU_STATS:
case supv::SET_GPIO:
case supv::READ_GPIO:
case supv::RESTART_SUPERVISOR:
case supv::FACTORY_RESET_CLEAR_ALL:
case supv::FACTORY_RESET_CLEAR_MIRROR:
case supv::FACTORY_RESET_CLEAR_CIRCULAR:
case supv::REQUEST_LOGGING_DATA:
case supv::DISABLE_PERIOIC_HK_TRANSMISSION:
enabledReplies = 2;
break;
default:
sif::debug << "PlocSupervisorHandler::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, supv::ACK_REPORT);
if (result != RETURN_OK) {
sif::debug << "PlocSupervisorHandler::enableReplyInReplyMap: Reply with id " << supv::ACK_REPORT
<< " not in replyMap" << std::endl;
}
result =
DeviceHandlerBase::enableReplyInReplyMap(command, enabledReplies, true, supv::EXE_REPORT);
if (result != RETURN_OK) {
sif::debug << "PlocSupervisorHandler::enableReplyInReplyMap: Reply with id " << supv::EXE_REPORT
<< " not in replyMap" << std::endl;
}
return RETURN_OK;
}
ReturnValue_t PlocSupervisorHandler::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 PlocSupervisorHandler::handleAckReport(const uint8_t* data) {
ReturnValue_t result = RETURN_OK;
result = verifyPacket(data, supv::SIZE_ACK_REPORT);
if (result == CRC_FAILURE) {
sif::error << "PlocSupervisorHandler::handleAckReport: CRC failure" << std::endl;
nextReplyId = supv::NONE;
replyRawReplyIfnotWiretapped(data, supv::SIZE_ACK_REPORT);
triggerEvent(SUPV_CRC_FAILURE_EVENT);
sendFailureReport(supv::ACK_REPORT, CRC_FAILURE);
disableAllReplies();
return RETURN_OK;
}
uint16_t apid = (*(data) << 8 | *(data + 1)) & APID_MASK;
switch (apid) {
case supv::APID_ACK_FAILURE: {
// TODO: Interpretation of status field in acknowledgment report
sif::debug << "PlocSupervisorHandler::handleAckReport: Received Ack failure report"
<< std::endl;
DeviceCommandId_t commandId = getPendingCommand();
if (commandId != DeviceHandlerIF::NO_COMMAND_ID) {
triggerEvent(SUPV_ACK_FAILURE, commandId);
}
sendFailureReport(supv::ACK_REPORT, RECEIVED_ACK_FAILURE);
disableAllReplies();
nextReplyId = supv::NONE;
result = IGNORE_REPLY_DATA;
break;
}
case supv::APID_ACK_SUCCESS: {
setNextReplyId();
break;
}
default: {
sif::debug << "PlocSupervisorHandler::handleAckReport: Invalid APID in Ack report"
<< std::endl;
result = RETURN_FAILED;
break;
}
}
return result;
}
ReturnValue_t PlocSupervisorHandler::handleExecutionReport(const uint8_t* data) {
ReturnValue_t result = RETURN_OK;
result = verifyPacket(data, supv::SIZE_EXE_REPORT);
if (result == CRC_FAILURE) {
sif::error << "PlocSupervisorHandler::handleExecutionReport: CRC failure" << std::endl;
nextReplyId = supv::NONE;
return result;
}
uint16_t apid = (*(data) << 8 | *(data + 1)) & APID_MASK;
switch (apid) {
case (supv::APID_EXE_SUCCESS): {
break;
}
case (supv::APID_EXE_FAILURE): {
// TODO: Interpretation of status field in execution report
sif::error
<< "PlocSupervisorHandler::handleExecutionReport: Received execution failure report"
<< std::endl;
DeviceCommandId_t commandId = getPendingCommand();
if (commandId != DeviceHandlerIF::NO_COMMAND_ID) {
triggerEvent(SUPV_EXE_FAILURE, commandId);
} else {
sif::debug << "PlocSupervisorHandler::handleExecutionReport: Unknown command id"
<< std::endl;
}
sendFailureReport(supv::EXE_REPORT, RECEIVED_EXE_FAILURE);
disableExeReportReply();
result = IGNORE_REPLY_DATA;
break;
}
default: {
sif::error << "PlocSupervisorHandler::handleExecutionReport: Unknown APID" << std::endl;
result = RETURN_FAILED;
break;
}
}
nextReplyId = supv::NONE;
return result;
}
ReturnValue_t PlocSupervisorHandler::handleHkReport(const uint8_t* data) {
ReturnValue_t result = RETURN_OK;
result = verifyPacket(data, supv::SIZE_HK_REPORT);
if (result == CRC_FAILURE) {
sif::error << "PlocSupervisorHandler::handleHkReport: Hk report has invalid crc" << std::endl;
}
uint16_t offset = supv::DATA_FIELD_OFFSET;
hkset.tempPs = *(data + offset) << 24 | *(data + offset + 1) << 16 | *(data + offset + 2) << 8 |
*(data + offset + 3);
offset += 4;
hkset.tempPl = *(data + offset) << 24 | *(data + offset + 1) << 16 | *(data + offset + 2) << 8 |
*(data + offset + 3);
offset += 4;
hkset.tempSup = *(data + offset) << 24 | *(data + offset + 1) << 16 | *(data + offset + 2) << 8 |
*(data + offset + 3);
offset += 4;
hkset.uptime = *(data + offset) << 24 | *(data + offset + 1) << 16 | *(data + offset + 2) << 8 |
*(data + offset + 3);
offset += 4;
hkset.cpuLoad = *(data + offset) << 24 | *(data + offset + 1) << 16 | *(data + offset + 2) << 8 |
*(data + offset + 3);
offset += 4;
hkset.availableHeap = *(data + offset) << 24 | *(data + offset + 1) << 16 |
*(data + offset + 2) << 8 | *(data + offset + 3);
offset += 4;
hkset.numTcs = *(data + offset) << 24 | *(data + offset + 1) << 16 | *(data + offset + 2) << 8 |
*(data + offset + 3);
offset += 4;
hkset.numTms = *(data + offset) << 24 | *(data + offset + 1) << 16 | *(data + offset + 2) << 8 |
*(data + offset + 3);
offset += 4;
hkset.socState = *(data + offset) << 24 | *(data + offset + 1) << 16 | *(data + offset + 2) << 8 |
*(data + offset + 3);
offset += 4;
hkset.nvm0_1_state = *(data + offset);
offset += 1;
hkset.nvm3_state = *(data + offset);
offset += 1;
hkset.missionIoState = *(data + offset);
offset += 1;
hkset.fmcState = *(data + offset);
offset += 1;
nextReplyId = supv::EXE_REPORT;
#if OBSW_VERBOSE_LEVEL >= 1 && OBSW_DEBUG_PLOC_SUPERVISOR == 1
sif::info << "PlocSupervisorHandler::handleHkReport: temp_ps: " << hkset.tempPs << std::endl;
sif::info << "PlocSupervisorHandler::handleHkReport: temp_pl: " << hkset.tempPl << std::endl;
sif::info << "PlocSupervisorHandler::handleHkReport: temp_sup: " << hkset.tempSup << std::endl;
sif::info << "PlocSupervisorHandler::handleHkReport: uptime: " << hkset.uptime << std::endl;
sif::info << "PlocSupervisorHandler::handleHkReport: cpu_load: " << hkset.cpuLoad << std::endl;
sif::info << "PlocSupervisorHandler::handleHkReport: available_heap: " << hkset.availableHeap
<< std::endl;
sif::info << "PlocSupervisorHandler::handleHkReport: num_tcs: " << hkset.numTcs << std::endl;
sif::info << "PlocSupervisorHandler::handleHkReport: num_tms: " << hkset.numTms << std::endl;
sif::info << "PlocSupervisorHandler::handleHkReport: soc_state: " << hkset.socState << std::endl;
sif::info << "PlocSupervisorHandler::handleHkReport: nvm0_1_state: "
<< static_cast<unsigned int>(hkset.nvm0_1_state.value) << std::endl;
sif::info << "PlocSupervisorHandler::handleHkReport: nvm3_state: "
<< static_cast<unsigned int>(hkset.nvm3_state.value) << std::endl;
sif::info << "PlocSupervisorHandler::handleHkReport: missoin_io_state: "
<< static_cast<unsigned int>(hkset.missionIoState.value) << std::endl;
sif::info << "PlocSupervisorHandler::handleHkReport: fmc_state: "
<< static_cast<unsigned int>(hkset.fmcState.value) << std::endl;
#endif
return result;
}
ReturnValue_t PlocSupervisorHandler::handleBootStatusReport(const uint8_t* data) {
ReturnValue_t result = RETURN_OK;
result = verifyPacket(data, supv::SIZE_BOOT_STATUS_REPORT);
if (result == CRC_FAILURE) {
sif::error << "PlocSupervisorHandler::handleBootStatusReport: Boot status report has invalid"
" crc"
<< std::endl;
return result;
}
uint16_t offset = supv::DATA_FIELD_OFFSET;
bootStatusReport.bootSignal = *(data + offset);
offset += 1;
bootStatusReport.resetCounter = *(data + offset);
offset += 1;
bootStatusReport.bootAfterMs = *(data + offset) << 24 | *(data + offset + 1) << 16 |
*(data + offset + 2) << 8 | *(data + offset + 3);
offset += 4;
bootStatusReport.bootTimeoutMs = *(data + offset) << 24 | *(data + offset + 1) << 16 |
*(data + offset + 2) << 8 | *(data + offset + 3);
offset += 4;
bootStatusReport.activeNvm = *(data + offset);
offset += 1;
bootStatusReport.bp0State = *(data + offset);
offset += 1;
bootStatusReport.bp1State = *(data + offset);
offset += 1;
bootStatusReport.bp2State = *(data + offset);
nextReplyId = supv::EXE_REPORT;
#if OBSW_VERBOSE_LEVEL >= 1 && OBSW_DEBUG_PLOC_SUPERVISOR == 1
sif::info << "PlocSupervisorHandler::handleBootStatusReport: Boot signal: "
<< static_cast<unsigned int>(bootStatusReport.bootSignal.value) << std::endl;
sif::info << "PlocSupervisorHandler::handleBootStatusReport: Reset counter: "
<< static_cast<unsigned int>(bootStatusReport.resetCounter.value) << std::endl;
sif::info << "PlocSupervisorHandler::handleBootStatusReport: BootAfterMs: "
<< bootStatusReport.bootAfterMs << " ms" << std::endl;
sif::info << "PlocSupervisorHandler::handleBootStatusReport: BootTimeoutMs: "
<< bootStatusReport.bootTimeoutMs << " ms" << std::endl;
sif::info << "PlocSupervisorHandler::handleBootStatusReport: Active NVM: "
<< static_cast<unsigned int>(bootStatusReport.activeNvm.value) << std::endl;
sif::info << "PlocSupervisorHandler::handleBootStatusReport: BP0: "
<< static_cast<unsigned int>(bootStatusReport.bp0State.value) << std::endl;
sif::info << "PlocSupervisorHandler::handleBootStatusReport: BP1: "
<< static_cast<unsigned int>(bootStatusReport.bp1State.value) << std::endl;
sif::info << "PlocSupervisorHandler::handleBootStatusReport: BP2: "
<< static_cast<unsigned int>(bootStatusReport.bp2State.value) << std::endl;
#endif
return result;
}
ReturnValue_t PlocSupervisorHandler::handleLatchupStatusReport(const uint8_t* data) {
ReturnValue_t result = RETURN_OK;
result = verifyPacket(data, supv::SIZE_LATCHUP_STATUS_REPORT);
if (result == CRC_FAILURE) {
sif::error << "PlocSupervisorHandler::handleLatchupStatusReport: Latchup status report has "
<< "invalid crc" << std::endl;
return result;
}
uint16_t offset = supv::DATA_FIELD_OFFSET;
latchupStatusReport.id = *(data + offset);
offset += 1;
latchupStatusReport.cnt0 = *(data + offset) << 8 | *(data + offset + 1);
offset += 2;
latchupStatusReport.cnt1 = *(data + offset) << 8 | *(data + offset + 1);
offset += 2;
latchupStatusReport.cnt2 = *(data + offset) << 8 | *(data + offset + 1);
offset += 2;
latchupStatusReport.cnt3 = *(data + offset) << 8 | *(data + offset + 1);
offset += 2;
latchupStatusReport.cnt4 = *(data + offset) << 8 | *(data + offset + 1);
offset += 2;
latchupStatusReport.cnt5 = *(data + offset) << 8 | *(data + offset + 1);
offset += 2;
latchupStatusReport.cnt6 = *(data + offset) << 8 | *(data + offset + 1);
offset += 2;
latchupStatusReport.timeSec = *(data + offset) << 24 | *(data + offset + 1) << 16 |
*(data + offset + 2) << 8 | *(data + offset + 3);
offset += 4;
latchupStatusReport.timeMin = *(data + offset) << 24 | *(data + offset + 1) << 16 |
*(data + offset + 2) << 8 | *(data + offset + 3);
offset += 4;
latchupStatusReport.timeHour = *(data + offset) << 24 | *(data + offset + 1) << 16 |
*(data + offset + 2) << 8 | *(data + offset + 3);
offset += 4;
latchupStatusReport.timeDay = *(data + offset) << 24 | *(data + offset + 1) << 16 |
*(data + offset + 2) << 8 | *(data + offset + 3);
offset += 4;
latchupStatusReport.timeMon = *(data + offset) << 24 | *(data + offset + 1) << 16 |
*(data + offset + 2) << 8 | *(data + offset + 3);
offset += 4;
latchupStatusReport.timeYear = *(data + offset) << 24 | *(data + offset + 1) << 16 |
*(data + offset + 2) << 8 | *(data + offset + 3);
offset += 4;
latchupStatusReport.timeMsec = *(data + offset) << 24 | *(data + offset + 1) << 16 |
*(data + offset + 2) << 8 | *(data + offset + 3);
offset += 4;
latchupStatusReport.isSet = *(data + offset) << 24 | *(data + offset + 1) << 16 |
*(data + offset + 2) << 8 | *(data + offset + 3);
offset += 4;
nextReplyId = supv::EXE_REPORT;
#if OBSW_VERBOSE_LEVEL >= 1 && OBSW_DEBUG_PLOC_SUPERVISOR == 1
sif::info << "PlocSupervisorHandler::handleLatchupStatusReport: Latchup ID: "
<< static_cast<unsigned int>(latchupStatusReport.id.value) << std::endl;
sif::info << "PlocSupervisorHandler::handleLatchupStatusReport: CNT0: "
<< latchupStatusReport.cnt0 << std::endl;
sif::info << "PlocSupervisorHandler::handleLatchupStatusReport: CNT1: "
<< latchupStatusReport.cnt1 << std::endl;
sif::info << "PlocSupervisorHandler::handleLatchupStatusReport: CNT2: "
<< latchupStatusReport.cnt2 << std::endl;
sif::info << "PlocSupervisorHandler::handleLatchupStatusReport: CNT3: "
<< latchupStatusReport.cnt3 << std::endl;
sif::info << "PlocSupervisorHandler::handleLatchupStatusReport: CNT4: "
<< latchupStatusReport.cnt4 << std::endl;
sif::info << "PlocSupervisorHandler::handleLatchupStatusReport: CNT5: "
<< latchupStatusReport.cnt5 << std::endl;
sif::info << "PlocSupervisorHandler::handleLatchupStatusReport: CNT6: "
<< latchupStatusReport.cnt6 << std::endl;
sif::info << "PlocSupervisorHandler::handleLatchupStatusReport: Sec: "
<< latchupStatusReport.timeSec << std::endl;
sif::info << "PlocSupervisorHandler::handleLatchupStatusReport: Min: "
<< latchupStatusReport.timeMin << std::endl;
sif::info << "PlocSupervisorHandler::handleLatchupStatusReport: Hour: "
<< latchupStatusReport.timeHour << std::endl;
sif::info << "PlocSupervisorHandler::handleLatchupStatusReport: Day: "
<< latchupStatusReport.timeDay << std::endl;
sif::info << "PlocSupervisorHandler::handleLatchupStatusReport: Mon: "
<< latchupStatusReport.timeMon << std::endl;
sif::info << "PlocSupervisorHandler::handleLatchupStatusReport: Year: "
<< latchupStatusReport.timeYear << std::endl;
sif::info << "PlocSupervisorHandler::handleLatchupStatusReport: Msec: "
<< latchupStatusReport.timeMsec << std::endl;
sif::info << "PlocSupervisorHandler::handleLatchupStatusReport: isSet: 0x" << std::hex
<< latchupStatusReport.timeMsec << std::dec << std::endl;
#endif
return result;
}
void PlocSupervisorHandler::setNextReplyId() {
switch (getPendingCommand()) {
case supv::GET_HK_REPORT:
nextReplyId = supv::HK_REPORT;
break;
case supv::GET_BOOT_STATUS_REPORT:
nextReplyId = supv::BOOT_STATUS_REPORT;
break;
case supv::GET_LATCHUP_STATUS_REPORT:
nextReplyId = supv::LATCHUP_REPORT;
break;
case supv::FIRST_MRAM_DUMP:
nextReplyId = supv::FIRST_MRAM_DUMP;
break;
case supv::CONSECUTIVE_MRAM_DUMP:
nextReplyId = supv::CONSECUTIVE_MRAM_DUMP;
break;
default:
/* If no telemetry is expected the next reply is always the execution report */
nextReplyId = supv::EXE_REPORT;
break;
}
}
size_t PlocSupervisorHandler::getNextReplyLength(DeviceCommandId_t commandId) {
size_t replyLen = 0;
if (nextReplyId == supv::NONE) {
return replyLen;
}
if (nextReplyId == supv::FIRST_MRAM_DUMP || nextReplyId == supv::CONSECUTIVE_MRAM_DUMP) {
/**
* Try to read 20 MRAM packets. If reply is larger, the packets will be read with the
* next doSendRead call. The command will be as long active as the packet with the sequence
* count indicating the last packet has not been received.
*/
replyLen = supv::MAX_PACKET_SIZE * 20;
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 << "PlocSupervisorHandler::getNextReplyLength: No entry for reply with reply id "
<< std::hex << nextReplyId << " in deviceReplyMap" << std::endl;
}
return replyLen;
}
void PlocSupervisorHandler::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 << "PlocSupervisorHandler::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 << "PlocSupervisorHandler::handleDeviceTM: Failed to report data" << std::endl;
}
}
void PlocSupervisorHandler::prepareEmptyCmd(uint16_t apid) {
supv::EmptyPacket packet(apid);
packetToOutBuffer(packet.getWholeData(), packet.getFullSize());
}
void PlocSupervisorHandler::prepareSelBootImageCmd(const uint8_t* commandData) {
supv::MPSoCBootSelect packet(*commandData, *(commandData + 1), *(commandData + 2),
*(commandData + 3));
packetToOutBuffer(packet.getWholeData(), packet.getFullSize());
}
ReturnValue_t PlocSupervisorHandler::prepareSetTimeRefCmd() {
Clock::TimeOfDay_t time;
ReturnValue_t result = Clock::getDateAndTime(&time);
if (result != RETURN_OK) {
sif::warning << "PlocSupervisorHandler::prepareSetTimeRefCmd: Failed to get current time"
<< std::endl;
return GET_TIME_FAILURE;
}
supv::SetTimeRef packet(&time);
packetToOutBuffer(packet.getWholeData(), packet.getFullSize());
return RETURN_OK;
}
void PlocSupervisorHandler::prepareDisableHk() {
supv::DisablePeriodicHkTransmission packet;
packetToOutBuffer(packet.getWholeData(), packet.getFullSize());
}
void PlocSupervisorHandler::prepareSetBootTimeoutCmd(const uint8_t* commandData) {
uint32_t timeout = *(commandData) << 24 | *(commandData + 1) << 16 | *(commandData + 2) << 8 |
*(commandData + 3);
supv::SetBootTimeout packet(timeout);
packetToOutBuffer(packet.getWholeData(), packet.getFullSize());
}
void PlocSupervisorHandler::prepareRestartTriesCmd(const uint8_t* commandData) {
uint8_t restartTries = *(commandData);
supv::SetRestartTries packet(restartTries);
packetToOutBuffer(packet.getWholeData(), packet.getFullSize());
}
void PlocSupervisorHandler::prepareWatchdogsEnableCmd(const uint8_t* commandData) {
uint8_t offset = 0;
uint8_t watchdogPs = *(commandData + offset);
offset += 1;
uint8_t watchdogPl = *(commandData + offset);
offset += 1;
uint8_t watchdogInt = *(commandData + offset);
supv::WatchdogsEnable packet(watchdogPs, watchdogPl, watchdogInt);
packetToOutBuffer(packet.getWholeData(), packet.getFullSize());
}
ReturnValue_t PlocSupervisorHandler::prepareWatchdogsConfigTimeoutCmd(const uint8_t* commandData) {
uint8_t offset = 0;
uint8_t watchdog = *(commandData + offset);
offset += 1;
if (watchdog > 2) {
return INVALID_WATCHDOG;
}
uint32_t timeout = *(commandData + offset) << 24 | *(commandData + offset + 1) << 16 |
*(commandData + offset + 2) << 8 | *(commandData + offset + 3);
if (timeout < 1000 || timeout > 360000) {
return INVALID_WATCHDOG_TIMEOUT;
}
supv::WatchdogsConfigTimeout packet(watchdog, timeout);
packetToOutBuffer(packet.getWholeData(), packet.getFullSize());
return RETURN_OK;
}
ReturnValue_t PlocSupervisorHandler::prepareLatchupConfigCmd(const uint8_t* commandData,
DeviceCommandId_t deviceCommand) {
ReturnValue_t result = RETURN_OK;
uint8_t latchupId = *commandData;
if (latchupId > 6) {
return INVALID_LATCHUP_ID;
}
switch (deviceCommand) {
case (supv::ENABLE_LATCHUP_ALERT): {
supv::LatchupAlert packet(true, latchupId);
packetToOutBuffer(packet.getWholeData(), packet.getFullSize());
break;
}
case (supv::DISABLE_LATCHUP_ALERT): {
supv::LatchupAlert packet(false, latchupId);
packetToOutBuffer(packet.getWholeData(), packet.getFullSize());
break;
}
default: {
sif::debug << "PlocSupervisorHandler::prepareLatchupConfigCmd: Invalid command id"
<< std::endl;
result = RETURN_FAILED;
break;
}
}
return result;
}
ReturnValue_t PlocSupervisorHandler::prepareAutoCalibrateAlertCmd(const uint8_t* commandData) {
uint8_t offset = 0;
uint8_t latchupId = *commandData;
offset += 1;
uint32_t mg = *(commandData + offset) << 24 | *(commandData + offset + 1) << 16 |
*(commandData + offset + 2) << 8 | *(commandData + offset + 3);
if (latchupId > 6) {
return INVALID_LATCHUP_ID;
}
supv::AutoCalibrateAlert packet(latchupId, mg);
packetToOutBuffer(packet.getWholeData(), packet.getFullSize());
return RETURN_OK;
}
ReturnValue_t PlocSupervisorHandler::prepareSetAlertIrqFilterCmd(const uint8_t* commandData) {
uint8_t latchupId = *commandData;
uint8_t tp = *(commandData + 1);
uint8_t div = *(commandData + 2);
if (latchupId > 6) {
return INVALID_LATCHUP_ID;
}
supv::SetAlertIrqFilter packet(latchupId, tp, div);
packetToOutBuffer(packet.getWholeData(), packet.getFullSize());
return RETURN_OK;
}
ReturnValue_t PlocSupervisorHandler::prepareSetAlertLimitCmd(const uint8_t* commandData) {
uint8_t offset = 0;
uint8_t latchupId = *commandData;
offset += 1;
uint32_t dutycycle = *(commandData + offset) << 24 | *(commandData + offset + 1) << 16 |
*(commandData + offset + 2) << 8 | *(commandData + offset + 3);
if (latchupId > 6) {
return INVALID_LATCHUP_ID;
}
supv::SetAlertlimit packet(latchupId, dutycycle);
packetToOutBuffer(packet.getWholeData(), packet.getFullSize());
return RETURN_OK;
}
ReturnValue_t PlocSupervisorHandler::prepareSetAdcSweetPeriodCmd(const uint8_t* commandData) {
uint32_t sweepPeriod = *(commandData) << 24 | *(commandData + 1) << 16 | *(commandData + 2) << 8 |
*(commandData + 3);
if (sweepPeriod < 21) {
return SWEEP_PERIOD_TOO_SMALL;
}
supv::SetAdcSweepPeriod packet(sweepPeriod);
packetToOutBuffer(packet.getWholeData(), packet.getFullSize());
return RETURN_OK;
}
void PlocSupervisorHandler::prepareSetAdcEnabledChannelsCmd(const uint8_t* commandData) {
uint16_t ch = *(commandData) << 8 | *(commandData + 1);
supv::SetAdcEnabledChannels packet(ch);
packetToOutBuffer(packet.getWholeData(), packet.getFullSize());
}
void PlocSupervisorHandler::prepareSetAdcWindowAndStrideCmd(const uint8_t* commandData) {
uint8_t offset = 0;
uint16_t windowSize = *(commandData + offset) << 8 | *(commandData + offset + 1);
offset += 2;
uint16_t stridingStepSize = *(commandData + offset) << 8 | *(commandData + offset + 1);
supv::SetAdcWindowAndStride packet(windowSize, stridingStepSize);
packetToOutBuffer(packet.getWholeData(), packet.getFullSize());
}
void PlocSupervisorHandler::prepareSetAdcThresholdCmd(const uint8_t* commandData) {
uint32_t threshold = *(commandData) << 24 | *(commandData + 1) << 16 | *(commandData + 2) << 8 |
*(commandData + 3);
supv::SetAdcThreshold packet(threshold);
packetToOutBuffer(packet.getWholeData(), packet.getFullSize());
}
void PlocSupervisorHandler::prepareEnableNvmsCmd(const uint8_t* commandData) {
uint8_t n01 = *commandData;
uint8_t n3 = *(commandData + 1);
supv::EnableNvms packet(n01, n3);
packetToOutBuffer(packet.getWholeData(), packet.getFullSize());
}
void PlocSupervisorHandler::prepareSelectNvmCmd(const uint8_t* commandData) {
uint8_t mem = *commandData;
supv::SelectNvm packet(mem);
packetToOutBuffer(packet.getWholeData(), packet.getFullSize());
}
ReturnValue_t PlocSupervisorHandler::prepareRunAutoEmTest(const uint8_t* commandData) {
uint8_t test = *commandData;
if (test != 1 && test != 2) {
return INVALID_TEST_PARAM;
}
supv::RunAutoEmTests packet(test);
packetToOutBuffer(packet.getWholeData(), packet.getFullSize());
return RETURN_OK;
}
ReturnValue_t PlocSupervisorHandler::prepareWipeMramCmd(const uint8_t* commandData) {
uint32_t start = 0;
uint32_t stop = 0;
size_t size = sizeof(start) + sizeof(stop);
SerializeAdapter::deSerialize(&start, &commandData, &size, SerializeIF::Endianness::BIG);
SerializeAdapter::deSerialize(&stop, &commandData, &size, SerializeIF::Endianness::BIG);
if ((stop - start) <= 0) {
return INVALID_MRAM_ADDRESSES;
}
supv::MramCmd packet(start, stop, supv::MramCmd::MramAction::WIPE);
packetToOutBuffer(packet.getWholeData(), packet.getFullSize());
return RETURN_OK;
}
ReturnValue_t PlocSupervisorHandler::prepareDumpMramCmd(const uint8_t* commandData) {
uint32_t start = 0;
uint32_t stop = 0;
size_t size = sizeof(start) + sizeof(stop);
SerializeAdapter::deSerialize(&start, &commandData, &size, SerializeIF::Endianness::BIG);
SerializeAdapter::deSerialize(&stop, &commandData, &size, SerializeIF::Endianness::BIG);
supv::MramCmd packet(start, stop, supv::MramCmd::MramAction::DUMP);
packetToOutBuffer(packet.getWholeData(), packet.getFullSize());
if ((stop - start) <= 0) {
return INVALID_MRAM_ADDRESSES;
}
expectedMramDumpPackets = (stop - start) / supv::MAX_DATA_CAPACITY;
if ((stop - start) % supv::MAX_DATA_CAPACITY) {
expectedMramDumpPackets++;
}
receivedMramDumpPackets = 0;
return RETURN_OK;
}
void PlocSupervisorHandler::preparePrintCpuStatsCmd(const uint8_t* commandData) {
uint8_t en = *commandData;
supv::PrintCpuStats packet(en);
packetToOutBuffer(packet.getWholeData(), packet.getFullSize());
}
void PlocSupervisorHandler::prepareSetDbgVerbosityCmd(const uint8_t* commandData) {
uint8_t vb = *commandData;
supv::SetDbgVerbosity packet(vb);
packetToOutBuffer(packet.getWholeData(), packet.getFullSize());
}
void PlocSupervisorHandler::prepareSetGpioCmd(const uint8_t* commandData) {
uint8_t port = *commandData;
uint8_t pin = *(commandData + 1);
uint8_t val = *(commandData + 2);
supv::SetGpio packet(port, pin, val);
packetToOutBuffer(packet.getWholeData(), packet.getFullSize());
}
void PlocSupervisorHandler::prepareReadGpioCmd(const uint8_t* commandData) {
uint8_t port = *commandData;
uint8_t pin = *(commandData + 1);
supv::ReadGpio packet(port, pin);
packetToOutBuffer(packet.getWholeData(), packet.getFullSize());
}
void PlocSupervisorHandler::packetToOutBuffer(uint8_t* packetData, size_t fullSize) {
memcpy(commandBuffer, packetData, fullSize);
rawPacket = commandBuffer;
rawPacketLen = fullSize;
nextReplyId = supv::ACK_REPORT;
}
void PlocSupervisorHandler::disableAllReplies() {
DeviceReplyMap::iterator iter;
/* Disable ack reply */
iter = deviceReplyMap.find(supv::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 supv::GET_HK_REPORT: {
iter = deviceReplyMap.find(supv::GET_HK_REPORT);
info = &(iter->second);
info->delayCycles = 0;
info->command = deviceCommandMap.end();
break;
}
default: {
break;
}
}
/* We must always disable the execution report reply here */
disableExeReportReply();
}
void PlocSupervisorHandler::sendFailureReport(DeviceCommandId_t replyId, ReturnValue_t status) {
DeviceReplyIter iter = deviceReplyMap.find(replyId);
if (iter == deviceReplyMap.end()) {
sif::debug << "PlocSupervisorHandler::sendFailureReport: Reply not in reply map" << std::endl;
return;
}
DeviceCommandInfo* info = &(iter->second.command->second);
if (info == nullptr) {
sif::debug << "PlocSupervisorHandler::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 PlocSupervisorHandler::disableExeReportReply() {
DeviceReplyIter iter = deviceReplyMap.find(supv::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 = 1;
}
ReturnValue_t PlocSupervisorHandler::parseMramPackets(const uint8_t* packet, size_t remainingSize,
size_t* foundLen) {
ReturnValue_t result = IGNORE_FULL_PACKET;
uint16_t packetLen = 0;
*foundLen = 0;
for (size_t idx = 0; idx < remainingSize; idx++) {
std::memcpy(spacePacketBuffer + bufferTop, packet + idx, 1);
bufferTop += 1;
*foundLen += 1;
if (bufferTop >= supv::SPACE_PACKET_HEADER_LENGTH) {
packetLen = readSpacePacketLength(spacePacketBuffer);
}
if (bufferTop == supv::SPACE_PACKET_HEADER_LENGTH + packetLen + 1) {
packetInBuffer = true;
bufferTop = 0;
return checkMramPacketApid();
}
if (bufferTop == supv::MAX_PACKET_SIZE) {
*foundLen = remainingSize;
disableAllReplies();
bufferTop = 0;
return MRAM_PACKET_PARSING_FAILURE;
}
}
return result;
}
ReturnValue_t PlocSupervisorHandler::handleMramDumpPacket(DeviceCommandId_t id) {
ReturnValue_t result = RETURN_FAILED;
// Prepare packet for downlink
if (packetInBuffer) {
uint16_t packetLen = readSpacePacketLength(spacePacketBuffer);
result = verifyPacket(spacePacketBuffer, supv::SPACE_PACKET_HEADER_LENGTH + packetLen + 1);
if (result != RETURN_OK) {
sif::warning << "PlocSupervisorHandler::handleMramDumpPacket: CRC failure" << std::endl;
return result;
}
handleMramDumpFile(id);
if (downlinkMramDump == true) {
handleDeviceTM(spacePacketBuffer + supv::SPACE_PACKET_HEADER_LENGTH, packetLen - 1, id);
}
packetInBuffer = false;
receivedMramDumpPackets++;
if (expectedMramDumpPackets == receivedMramDumpPackets) {
nextReplyId = supv::EXE_REPORT;
}
increaseExpectedMramReplies(id);
return RETURN_OK;
}
return result;
}
void PlocSupervisorHandler::increaseExpectedMramReplies(DeviceCommandId_t id) {
DeviceReplyMap::iterator mramDumpIter = deviceReplyMap.find(id);
DeviceReplyMap::iterator exeReportIter = deviceReplyMap.find(supv::EXE_REPORT);
if (mramDumpIter == deviceReplyMap.end()) {
sif::debug << "PlocSupervisorHandler::increaseExpectedMramReplies: Dump MRAM reply not "
<< "in reply map" << std::endl;
return;
}
if (exeReportIter == deviceReplyMap.end()) {
sif::debug << "PlocSupervisorHandler::increaseExpectedMramReplies: Execution report not "
<< "in reply map" << std::endl;
return;
}
DeviceReplyInfo* mramReplyInfo = &(mramDumpIter->second);
if (mramReplyInfo == nullptr) {
sif::debug << "PlocSupervisorHandler::increaseExpectedReplies: MRAM reply info nullptr"
<< std::endl;
return;
}
DeviceReplyInfo* exeReplyInfo = &(exeReportIter->second);
if (exeReplyInfo == nullptr) {
sif::debug << "PlocSupervisorHandler::increaseExpectedReplies: Execution reply info"
<< " nullptr" << std::endl;
return;
}
DeviceCommandInfo* info = &(mramReplyInfo->command->second);
if (info == nullptr) {
sif::debug << "PlocSupervisorHandler::increaseExpectedReplies: Command info nullptr"
<< std::endl;
return;
}
uint8_t sequenceFlags = spacePacketBuffer[2] >> 6;
if (sequenceFlags != static_cast<uint8_t>(supv::SequenceFlags::LAST_PKT) &&
(sequenceFlags != static_cast<uint8_t>(supv::SequenceFlags::STANDALONE_PKT))) {
// Command expects at least one MRAM packet more and the execution report
info->expectedReplies = 2;
// Wait maximum of 2 cycles for next MRAM packet
mramReplyInfo->delayCycles = 2;
// Also adapting delay cycles for execution report
exeReplyInfo->delayCycles = 3;
} else {
// Command expects the execution report
info->expectedReplies = 1;
mramReplyInfo->delayCycles = 0;
}
return;
}
ReturnValue_t PlocSupervisorHandler::checkMramPacketApid() {
uint16_t apid = (spacePacketBuffer[0] << 8 | spacePacketBuffer[1]) & supv::APID_MASK;
if (apid != supv::APID_MRAM_DUMP_TM) {
return NO_MRAM_PACKET;
}
return APERIODIC_REPLY;
}
ReturnValue_t PlocSupervisorHandler::handleMramDumpFile(DeviceCommandId_t id) {
ReturnValue_t result = RETURN_OK;
uint16_t packetLen = readSpacePacketLength(spacePacketBuffer);
uint8_t sequenceFlags = readSequenceFlags(spacePacketBuffer);
if (id == supv::FIRST_MRAM_DUMP) {
if (sequenceFlags == static_cast<uint8_t>(supv::SequenceFlags::FIRST_PKT) ||
(sequenceFlags == static_cast<uint8_t>(supv::SequenceFlags::STANDALONE_PKT))) {
result = createMramDumpFile();
if (result != RETURN_OK) {
return result;
}
}
}
if (not std::filesystem::exists(activeMramFile)) {
sif::warning << "PlocSupervisorHandler::handleMramDumpFile: MRAM file does not exist"
<< std::endl;
return MRAM_FILE_NOT_EXISTS;
}
std::ofstream file(activeMramFile, std::ios_base::app | std::ios_base::out);
file.write(reinterpret_cast<const char*>(spacePacketBuffer + supv::SPACE_PACKET_HEADER_LENGTH),
packetLen - 1);
file.close();
return RETURN_OK;
}
uint16_t PlocSupervisorHandler::readSpacePacketLength(uint8_t* spacePacket) {
return spacePacket[4] << 8 | spacePacket[5];
}
uint8_t PlocSupervisorHandler::readSequenceFlags(uint8_t* spacePacket) {
return spacePacketBuffer[2] >> 6;
}
ReturnValue_t PlocSupervisorHandler::createMramDumpFile() {
ReturnValue_t result = RETURN_OK;
std::string timeStamp;
result = getTimeStampString(timeStamp);
if (result != RETURN_OK) {
return result;
}
std::string filename = "mram-dump--" + timeStamp + ".bin";
#ifdef TE0720_1CFA
std::string currentMountPrefix = sdcMan->getCurrentMountPrefix();
#else
std::string currentMountPrefix("/mnt/sd0");
#endif /* BOARD_TE0720 == 0 */
// Check if path to PLOC directory exists
if (not std::filesystem::exists(std::string(currentMountPrefix + "/" + plocFilePath))) {
sif::warning << "PlocSupervisorHandler::createMramDumpFile: Ploc path does not exist"
<< std::endl;
return PATH_DOES_NOT_EXIST;
}
activeMramFile = currentMountPrefix + "/" + plocFilePath + "/" + filename;
// Create new file
std::ofstream file(activeMramFile, std::ios_base::out);
file.close();
return RETURN_OK;
}
ReturnValue_t PlocSupervisorHandler::getTimeStampString(std::string& timeStamp) {
Clock::TimeOfDay_t time;
ReturnValue_t result = Clock::getDateAndTime(&time);
if (result != RETURN_OK) {
sif::warning << "PlocSupervisorHandler::createMramDumpFile: Failed to get current time"
<< std::endl;
return GET_TIME_FAILURE;
}
timeStamp = std::to_string(time.year) + "-" + std::to_string(time.month) + "-" +
std::to_string(time.day) + "--" + std::to_string(time.hour) + "-" +
std::to_string(time.minute) + "-" + std::to_string(time.second);
return RETURN_OK;
}