#include "SusHandler.h" #include #include #include "fsfw/datapool/PoolReadGuard.h" SusHandler::SusHandler(object_id_t objectId, uint8_t susIdx, object_id_t deviceCommunication, CookieIF *comCookie) : DeviceHandlerBase(objectId, deviceCommunication, comCookie), dataset(this), susIdx(susIdx) {} SusHandler::~SusHandler() = default; void SusHandler::doStartUp() { if (internalState != InternalState::STARTUP) { commandExecuted = false; updatePeriodicReply(true, REPLY); internalState = InternalState::STARTUP; } if (internalState == InternalState::STARTUP) { if (commandExecuted) { setMode(MODE_ON); internalState = InternalState::NONE; commandExecuted = false; } } } void SusHandler::doShutDown() { if (internalState != InternalState::SHUTDOWN) { PoolReadGuard pg(&dataset); dataset.tempC = thermal::INVALID_TEMPERATURE; dataset.setValidity(false, true); internalState = InternalState::SHUTDOWN; commandExecuted = false; } if (internalState == InternalState::SHUTDOWN and commandExecuted) { updatePeriodicReply(false, REPLY); commandExecuted = false; internalState = InternalState::NONE; setMode(MODE_OFF); } } ReturnValue_t SusHandler::buildTransitionDeviceCommand(DeviceCommandId_t *id) { if (internalState == InternalState::STARTUP) { *id = REQUEST; return prepareRequest(acs::SimpleSensorMode::NORMAL); } else if (internalState == InternalState::SHUTDOWN) { *id = REQUEST; return prepareRequest(acs::SimpleSensorMode::OFF); } return NOTHING_TO_SEND; } ReturnValue_t SusHandler::buildNormalDeviceCommand(DeviceCommandId_t *id) { *id = REQUEST; return prepareRequest(acs::SimpleSensorMode::NORMAL); } ReturnValue_t SusHandler::buildCommandFromCommand(DeviceCommandId_t deviceCommand, const uint8_t *commandData, size_t commandDataLen) { return NOTHING_TO_SEND; } ReturnValue_t SusHandler::scanForReply(const uint8_t *start, size_t len, DeviceCommandId_t *foundId, size_t *foundLen) { if (getMode() == _MODE_WAIT_OFF or getMode() == _MODE_WAIT_ON) { return IGNORE_FULL_PACKET; } if (len != sizeof(acs::SusReply)) { *foundLen = len; return returnvalue::FAILED; } *foundId = REPLY; *foundLen = len; if (internalState == InternalState::SHUTDOWN) { commandExecuted = true; } return returnvalue::OK; } ReturnValue_t SusHandler::interpretDeviceReply(DeviceCommandId_t id, const uint8_t *packet) { const auto *reply = reinterpret_cast(packet); if (reply->dataWasSet) { if (internalState == InternalState::STARTUP) { commandExecuted = true; } PoolReadGuard pg(&dataset); // In a previous stricter FDIR variant, this was considered faulty communication and was already // handled in the communication interface. However, the SUS devices probably glitch in orbit, // so the FDIR was relaxed. The fault case check previously used now only leads to the dataset // being marked invalid, shifting more responsibility of determining and setting SUS devices // faulty to the operator. // UPDATE: Step1: First determine how often and whether this happens at all if (reply->tempRaw == 0xfff) { // Prevent spam if a device is glitchy for prolonged periods by only triggering with a // maximum interval. if (faultyDataEventCd.hasTimedOut()) { triggerEvent(TEMPERATURE_IS_ALL_ONES); faultyDataEventCd.resetTimer(); } // dataset.setValidity(false, true); // return returnvalue::OK; } dataset.setValidity(true, true); dataset.tempC = max1227::getTemperature(reply->tempRaw); std::memcpy(dataset.channels.value, reply->channelsRaw, sizeof(reply->channelsRaw)); } return returnvalue::OK; } void SusHandler::fillCommandAndReplyMap() { insertInCommandMap(REQUEST); insertInReplyMap(REPLY, 5, nullptr, 0, true); } void SusHandler::setToGoToNormalMode(bool enable) { this->goToNormalMode = enable; } uint32_t SusHandler::getTransitionDelayMs(Mode_t from, Mode_t to) { return transitionDelay; } void SusHandler::modeChanged(void) { internalState = InternalState::NONE; } ReturnValue_t SusHandler::initializeLocalDataPool(localpool::DataPool &localDataPoolMap, LocalDataPoolManager &poolManager) { localDataPoolMap.emplace(susMax1227::TEMPERATURE_C, &tempC); localDataPoolMap.emplace(susMax1227::CHANNEL_VEC, &channelVec); poolManager.subscribeForDiagPeriodicPacket( subdp::DiagnosticsHkPeriodicParams(dataset.getSid(), false, 5.0)); return returnvalue::OK; } ReturnValue_t SusHandler::prepareRequest(acs::SimpleSensorMode mode) { request.mode = mode; rawPacket = reinterpret_cast(&request); rawPacketLen = sizeof(acs::SusRequest); return returnvalue::OK; } LocalPoolDataSetBase *SusHandler::getDataSetHandle(sid_t sid) { if (sid == dataset.getSid()) { return &dataset; } return nullptr; }