#ifndef LINUX_PAYLOAD_FRESHSUPVHANDLER_H_ #define LINUX_PAYLOAD_FRESHSUPVHANDLER_H_ #include #include #include #include "PlocSupvUartMan.h" #include "fsfw/devicehandlers/FreshDeviceHandlerBase.h" #include "fsfw/power/definitions.h" #include "fsfw_hal/linux/gpio/Gpio.h" #include "plocSupvDefs.h" using supv::TcBase; static constexpr bool DEBUG_PLOC_SUPV = true; static constexpr bool REDUCE_NORMAL_MODE_PRINTOUT = false; class FreshSupvHandler : public FreshDeviceHandlerBase { public: enum OpCode { DEFAULT_OPERATION = 0, PARSE_TM = 1 }; FreshSupvHandler(DhbConfig cfg, CookieIF* comCookie, Gpio uartIsolatorSwitch, PowerSwitchIF& switchIF, power::Switch_t powerSwitch); /** * Periodic helper executed function, implemented by child class. */ void performDeviceOperation(uint8_t opCode) override; /** * Implemented by child class. Handle all command messages which are * not health, mode, action or housekeeping messages. * @param message * @return */ ReturnValue_t handleCommandMessage(CommandMessage* message) override; ReturnValue_t initialize() override; private: // HK manager abstract functions. LocalPoolDataSetBase* getDataSetHandle(sid_t sid) override; ReturnValue_t initializeLocalDataPool(localpool::DataPool& localDataPoolMap, LocalDataPoolManager& poolManager) override; // Mode abstract functions ReturnValue_t checkModeCommand(Mode_t mode, Submode_t submode, uint32_t* msToReachTheMode) override; // Action override. Forward to user. ReturnValue_t executeAction(ActionId_t actionId, MessageQueueId_t commandedBy, const uint8_t* data, size_t size) override; /** * @overload * @param submode */ void startTransition(Mode_t newMode, Submode_t submode) override; void handleTransitionToOn(); void handleTransitionToOff(); private: static constexpr bool SET_TIME_DURING_BOOT = true; static const uint8_t SIZE_NULL_TERMINATOR = 1; enum class StartupState : uint8_t { IDLE, POWER_SWITCHING, BOOTING, SET_TIME, WAIT_FOR_TIME_REPLY, TIME_WAS_SET, ON }; StartupState startupState = StartupState::IDLE; MessageQueueIF* eventQueue = nullptr; supv::TmBase tmReader; enum class ShutdownState : uint8_t { IDLE, POWER_SWITCHING }; ShutdownState shutdownState = ShutdownState::IDLE; PlocSupvUartManager* uartManager; CookieIF* comCookie; PowerSwitchIF& switchIF; power::Switch_t switchId; Gpio uartIsolatorSwitch; supv::HkSet hkSet; supv::BootStatusReport bootStatusReport; supv::LatchupStatusReport latchupStatusReport; supv::CountersReport countersReport; supv::AdcReport adcReport; bool transitionActive = false; Mode_t targetMode = HasModesIF::MODE_INVALID; Submode_t targetSubmode = 0; Countdown switchTimeout = Countdown(2000); // Vorago nees some time to boot properly Countdown bootTimeout = Countdown(supv::BOOT_TIMEOUT_MS); PoolEntry adcRawEntry = PoolEntry(16); PoolEntry adcEngEntry = PoolEntry(16); PoolEntry latchupCounters = PoolEntry(7); PoolEntry fmcStateEntry = PoolEntry(1); PoolEntry bootStateEntry = PoolEntry(1); PoolEntry bootCyclesEntry = PoolEntry(1); PoolEntry tempSupEntry = PoolEntry(1); pwrctrl::EnablePl enablePl = pwrctrl::EnablePl(objects::POWER_CONTROLLER); struct ActiveCmdInfo { ActiveCmdInfo(DeviceCommandId_t commandId, uint32_t cmdCountdownMs) : commandId(commandId), cmdCountdown(cmdCountdownMs) {} DeviceCommandId_t commandId = DeviceHandlerIF::NO_COMMAND_ID; bool isPending = false; bool ackRecv = false; bool ackExeRecv = false; bool replyPacketExpected = false; bool replyPacketReceived = false; MessageQueueId_t commandedBy = MessageQueueIF::NO_QUEUE; bool requiresActionReply = false; Countdown cmdCountdown; }; uint32_t buildActiveCmdKey(uint16_t moduleApid, uint8_t serviceId); // Map for Action commands. For normal commands, a separate static structure will be used. std::map activeActionCmds; std::array commandBuffer{}; SpacePacketCreator creator; supv::TcParams spParams = supv::TcParams(creator); ReturnValue_t parseTmPackets(); ReturnValue_t sendCommand(DeviceCommandId_t commandId, TcBase& tc, bool replyPacketExpected, uint32_t cmdCountdownMs = 1000); ReturnValue_t sendEmptyCmd(DeviceCommandId_t commandId, uint16_t apid, uint8_t serviceId, bool replyPacketExpected); ReturnValue_t prepareSelBootImageCmd(const uint8_t* commandData); ReturnValue_t prepareSetTimeRefCmd(); ReturnValue_t prepareSetBootTimeoutCmd(const uint8_t* commandData, size_t cmdDataLen); ReturnValue_t prepareRestartTriesCmd(const uint8_t* commandData, size_t cmdDataLen); ReturnValue_t prepareDisableHk(); ReturnValue_t prepareLatchupConfigCmd(const uint8_t* commandData, DeviceCommandId_t deviceCommand, size_t cmdDataLen); ReturnValue_t prepareSetAlertLimitCmd(const uint8_t* commandData, size_t cmdDataLen); ReturnValue_t prepareFactoryResetCmd(const uint8_t* commandData, size_t len); ReturnValue_t prepareSetShutdownTimeoutCmd(const uint8_t* commandData, size_t cmdDataLen); ReturnValue_t prepareSetGpioCmd(const uint8_t* commandData, size_t commandDataLen); ReturnValue_t prepareReadGpioCmd(const uint8_t* commandData, size_t commandDataLen); ReturnValue_t prepareSetAdcEnabledChannelsCmd(const uint8_t* commandData); ReturnValue_t prepareSetAdcWindowAndStrideCmd(const uint8_t* commandData); ReturnValue_t prepareSetAdcThresholdCmd(const uint8_t* commandData); ReturnValue_t prepareWipeMramCmd(const uint8_t* commandData, size_t cmdDataLen); ReturnValue_t extractUpdateCommand(const uint8_t* commandData, size_t size, supv::UpdateParams& params); ReturnValue_t extractBaseParams(const uint8_t** commandData, size_t& remSize, supv::UpdateParams& params); void handleEvent(EventMessage* eventMessage); void handleBadApidServiceCombination(Event event, unsigned int apid, unsigned int serviceId); ReturnValue_t eventSubscription(); void handlePacketPrint(); bool isCommandAlreadyActive(ActionId_t actionId) const; ReturnValue_t handleAckReport(const uint8_t* data); void printAckFailureInfo(uint16_t statusCode, DeviceCommandId_t commandId); ReturnValue_t handleExecutionReport(const uint8_t* data); ReturnValue_t handleExecutionSuccessReport(ActiveCmdInfo& info, supv::ExecutionReport& report); void handleExecutionFailureReport(ActiveCmdInfo& info, supv::ExecutionReport& report); ReturnValue_t handleHkReport(const uint8_t* data); ReturnValue_t verifyPacket(const uint8_t* start, size_t foundLen); void confirmReplyPacketReceived(supv::Apid apid, uint8_t serviceId); void performCommandCompletionHandling(supv::Apid apid, uint8_t serviceId, ActiveCmdInfo& info); ReturnValue_t handleBootStatusReport(const uint8_t* data); ReturnValue_t genericHandleTm(const char* contextString, const uint8_t* data, LocalPoolDataSetBase& set, supv::Apid apid, uint8_t serviceId); ReturnValue_t handleLatchupStatusReport(const uint8_t* data); }; #endif /* LINUX_PAYLOAD_FRESHSUPVHANDLER_H_ */