From 98fcf06e31c66f5c6ce482394283d659d20912db Mon Sep 17 00:00:00 2001 From: Robin Mueller Date: Fri, 18 Nov 2022 17:25:52 +0100 Subject: [PATCH] various improvements and bugfixes --- .../PlocSupervisorDefinitions.h | 2 +- linux/devices/ploc/PlocSupervisorHandler.cpp | 2 +- linux/devices/ploc/PlocSupvUartMan.cpp | 67 ++++++++----------- linux/devices/ploc/PlocSupvUartMan.h | 8 +-- linux/scheduling.cpp | 4 ++ mission/devices/devicedefinitions/SpBase.h | 2 + 6 files changed, 39 insertions(+), 46 deletions(-) diff --git a/linux/devices/devicedefinitions/PlocSupervisorDefinitions.h b/linux/devices/devicedefinitions/PlocSupervisorDefinitions.h index ac89c8d4..18e92ccb 100644 --- a/linux/devices/devicedefinitions/PlocSupervisorDefinitions.h +++ b/linux/devices/devicedefinitions/PlocSupervisorDefinitions.h @@ -501,7 +501,7 @@ class TcBase : public ploc::SpTcBase { uint16_t getModuleApid() const { return getApid() & APID_MODULE_MASK; } - uint8_t getServiceId() const { return payloadStart[supv::PAYLOAD_OFFSET]; } + uint8_t getServiceId() const { return getPacketData()[TIMESTAMP_LEN]; } static size_t fullSpDataLenFromPayloadLen(size_t payloadLen) { return SECONDARY_HEADER_LEN + payloadLen + CRC_LEN; diff --git a/linux/devices/ploc/PlocSupervisorHandler.cpp b/linux/devices/ploc/PlocSupervisorHandler.cpp index 3293c652..bd027e37 100644 --- a/linux/devices/ploc/PlocSupervisorHandler.cpp +++ b/linux/devices/ploc/PlocSupervisorHandler.cpp @@ -138,12 +138,12 @@ void PlocSupervisorHandler::doStartUp() { if (setTimeDuringStartup) { if (startupState == StartupState::OFF) { bootTimeout.resetTimer(); - uartManager.start(); startupState = StartupState::BOOTING; } if (startupState == StartupState::BOOTING) { if (bootTimeout.hasTimedOut()) { uartIsolatorSwitch.pullHigh(); + uartManager.start(); startupState = StartupState::SET_TIME; } } diff --git a/linux/devices/ploc/PlocSupvUartMan.cpp b/linux/devices/ploc/PlocSupvUartMan.cpp index 8bc2019d..0db0feef 100644 --- a/linux/devices/ploc/PlocSupvUartMan.cpp +++ b/linux/devices/ploc/PlocSupvUartMan.cpp @@ -101,12 +101,11 @@ ReturnValue_t PlocSupvUartManager::performOperation(uint8_t operationCode) { lock->unlockMutex(); semaphore->acquire(); while (true) { - // sif::debug << "SUPV UART MAN: Running.." << std::endl; - putTaskToSleep = handleUartReception(); if (putTaskToSleep) { performUartShutdown(); break; } + handleUartReception(); lock->lockMutex(); InternalState currentState = state; lock->unlockMutex(); @@ -126,44 +125,39 @@ ReturnValue_t PlocSupvUartManager::performOperation(uint8_t operationCode) { break; } } - if (putTaskToSleep) { - performUartShutdown(); - break; - } } } } -bool PlocSupvUartManager::handleUartReception() { +ReturnValue_t PlocSupvUartManager::handleUartReception() { ReturnValue_t result = OK; + ReturnValue_t status = OK; ssize_t bytesRead = read(serialPort, reinterpret_cast(recBuf.data()), static_cast(recBuf.size())); if (bytesRead == 0) { - { - MutexGuard mg(lock); - if (state == InternalState::GO_TO_SLEEP) { - return true; - } - } while (result != NO_PACKET_FOUND) { result = tryHdlcParsing(); + if (result != NO_PACKET_FOUND and result != OK) { + status = result; + } } } else if (bytesRead < 0) { sif::warning << "PlocSupvHelper::performOperation: read call failed with error [" << errno << ", " << strerror(errno) << "]" << std::endl; - return true; + return FAILED; } else if (bytesRead >= static_cast(recBuf.size())) { sif::error << "PlocSupvHelper::performOperation: Receive buffer too small for " << bytesRead << " bytes" << std::endl; + return FAILED; } else if (bytesRead > 0) { if (debugMode) { sif::info << "Received " << bytesRead << " bytes from the PLOC Supervisor:" << std::endl; arrayprinter::print(recBuf.data(), bytesRead); } recRingBuf.writeData(recBuf.data(), bytesRead); - tryHdlcParsing(); + status = tryHdlcParsing(); } - return false; + return status; } ReturnValue_t PlocSupvUartManager::startUpdate(std::string file, uint8_t memoryId, @@ -571,23 +565,20 @@ ReturnValue_t PlocSupvUartManager::handlePacketTransmissionNoReply( bool ackReceived = false; bool packetWasHandled = false; while (true) { - do { - result = tryHdlcParsing(); - } while (result != NO_PACKET_FOUND); + handleUartReception(); if (not decodedQueue.empty()) { size_t packetLen = 0; decodedQueue.retrieve(&packetLen); - decodedRingBuf.readData(decodedBuf.data(), packetLen); + decodedRingBuf.readData(decodedBuf.data(), packetLen, true); tmReader.setData(decodedBuf.data(), packetLen); result = checkReceivedTm(); if (result != returnvalue::OK) { continue; } if (tmReader.getModuleApid() == Apid::TMTC_MAN) { - uint8_t serviceId = tmReader.getServiceId(); int retval = 0; if (not ackReceived) { - retval = handleAckReception(packet, serviceId, packetLen); + retval = handleAckReception(packet, packetLen); if (retval == 1) { sif::debug << "ACK received" << std::endl; ackReceived = true; @@ -596,7 +587,7 @@ ReturnValue_t PlocSupvUartManager::handlePacketTransmissionNoReply( return returnvalue::FAILED; } } else { - retval = handleExeAckReception(packet, serviceId, packetLen); + retval = handleExeAckReception(packet, packetLen); if (retval == 1) { sif::debug << "EXE ACK received" << std::endl; break; @@ -619,7 +610,8 @@ ReturnValue_t PlocSupvUartManager::handlePacketTransmissionNoReply( return returnvalue::OK; } -int PlocSupvUartManager::handleAckReception(supv::TcBase& tc, uint8_t serviceId, size_t packetLen) { +int PlocSupvUartManager::handleAckReception(supv::TcBase& tc, size_t packetLen) { + uint8_t serviceId = tmReader.getServiceId(); if (serviceId == static_cast(supv::tm::TmtcId::ACK) or serviceId == static_cast(supv::tm::TmtcId::NAK)) { AcknowledgmentReport ackReport(tmReader); @@ -650,8 +642,8 @@ int PlocSupvUartManager::handleAckReception(supv::TcBase& tc, uint8_t serviceId, return 0; } -int PlocSupvUartManager::handleExeAckReception(supv::TcBase& tc, uint8_t serviceId, - size_t packetLen) { +int PlocSupvUartManager::handleExeAckReception(supv::TcBase& tc, size_t packetLen) { + uint8_t serviceId = tmReader.getServiceId(); if (serviceId == static_cast(supv::tm::TmtcId::EXEC_ACK) or serviceId == static_cast(supv::tm::TmtcId::EXEC_NAK)) { ExecutionReport exeReport(tmReader); @@ -660,7 +652,7 @@ int PlocSupvUartManager::handleExeAckReception(supv::TcBase& tc, uint8_t service triggerEvent(EXE_RECEPTION_FAILURE); return -1; } - if (exeReport.getRefModuleApid() == tc.getApid() and + if (exeReport.getRefModuleApid() == tc.getModuleApid() and exeReport.getRefServiceId() == tc.getServiceId()) { if (serviceId == static_cast(supv::tm::TmtcId::EXEC_ACK)) { return 1; @@ -688,8 +680,7 @@ ReturnValue_t PlocSupvUartManager::checkReceivedTm() { triggerEvent(SUPV_REPLY_SIZE_MISSMATCH, rememberApid); return result; } - result = tmReader.checkCrc(); - if (result != returnvalue::OK) { + if (not tmReader.verifyCrc()) { triggerEvent(SUPV_REPLY_CRC_MISSMATCH, rememberApid); return result; } @@ -745,12 +736,12 @@ ReturnValue_t PlocSupvUartManager::calcImageCrc() { ReturnValue_t PlocSupvUartManager::handleCheckMemoryCommand() { ReturnValue_t result = returnvalue::OK; resetSpParams(); - supv::CheckMemory packet(spParams); - result = packet.buildPacket(update.memoryId, update.startAddress, update.fullFileSize); + supv::CheckMemory tcPacket(spParams); + result = tcPacket.buildPacket(update.memoryId, update.startAddress, update.fullFileSize); if (result != returnvalue::OK) { return result; } - result = encodeAndSendPacket(packet.getFullPacket(), packet.getFullPacketLen()); + result = encodeAndSendPacket(tcPacket.getFullPacket(), tcPacket.getFullPacketLen()); if (result != returnvalue::OK) { return result; } @@ -760,14 +751,11 @@ ReturnValue_t PlocSupvUartManager::handleCheckMemoryCommand() { bool packetWasHandled = false; bool exeReceived = false; while (true) { - do { - result = tryHdlcParsing(); - - } while (result != NO_PACKET_FOUND); + handleUartReception(); if (not decodedQueue.empty()) { size_t packetLen = 0; decodedQueue.retrieve(&packetLen); - decodedRingBuf.readData(decodedBuf.data(), packetLen); + decodedRingBuf.readData(decodedBuf.data(), packetLen, true); tmReader.setData(decodedBuf.data(), packetLen); result = checkReceivedTm(); if (result != returnvalue::OK) { @@ -775,10 +763,9 @@ ReturnValue_t PlocSupvUartManager::handleCheckMemoryCommand() { } packetWasHandled = false; if (tmReader.getModuleApid() == Apid::TMTC_MAN) { - uint8_t serviceId = tmReader.getServiceId(); int retval = 0; if (not ackReceived) { - retval = handleAckReception(packet, serviceId, packetLen); + retval = handleAckReception(tcPacket, packetLen); if (retval == 1) { packetWasHandled = true; ackReceived = true; @@ -786,7 +773,7 @@ ReturnValue_t PlocSupvUartManager::handleCheckMemoryCommand() { return returnvalue::FAILED; } } else { - retval = handleExeAckReception(packet, serviceId, packetLen); + retval = handleExeAckReception(tcPacket, packetLen); if (retval == 1) { packetWasHandled = true; exeReceived = true; diff --git a/linux/devices/ploc/PlocSupvUartMan.h b/linux/devices/ploc/PlocSupvUartMan.h index ce24e721..9d9e3738 100644 --- a/linux/devices/ploc/PlocSupvUartMan.h +++ b/linux/devices/ploc/PlocSupvUartMan.h @@ -252,14 +252,14 @@ class PlocSupvUartManager : public DeviceCommunicationIF, std::array tmBuf{}; - bool debugMode = false; + bool debugMode = true; bool timestamping = true; // Remembers APID to know at which command a procedure failed uint16_t rememberApid = 0; ReturnValue_t handleRunningLongerRequest(); - bool handleUartReception(); + ReturnValue_t handleUartReception(); void addHdlcFraming(const uint8_t* src, size_t slen, uint8_t* dst, size_t* dlen, size_t maxDest); int removeHdlcFramingWithCrcCheck(const uint8_t* src, size_t slen, uint8_t* dst, size_t* dlen); @@ -275,8 +275,8 @@ class PlocSupvUartManager : public DeviceCommunicationIF, // ReturnValue_t performEventBufferRequest(); ReturnValue_t handlePacketTransmissionNoReply(supv::TcBase& packet, uint32_t timeoutExecutionReport = 60000); - int handleAckReception(supv::TcBase& tc, uint8_t serviceId, size_t packetLen); - int handleExeAckReception(supv::TcBase& tc, uint8_t serviceId, size_t packetLen); + int handleAckReception(supv::TcBase& tc, size_t packetLen); + int handleExeAckReception(supv::TcBase& tc, size_t packetLen); /** * @brief Handles reading of TM packets from the communication interface diff --git a/linux/scheduling.cpp b/linux/scheduling.cpp index 1300266f..75706185 100644 --- a/linux/scheduling.cpp +++ b/linux/scheduling.cpp @@ -54,6 +54,8 @@ void scheduling::addMpsocSupvHandlers(PeriodicTaskIF* plTask) { plTask->addComponent(objects::PLOC_SUPERVISOR_HANDLER, DeviceHandlerIF::GET_WRITE); plTask->addComponent(objects::PLOC_SUPERVISOR_HANDLER, DeviceHandlerIF::SEND_READ); plTask->addComponent(objects::PLOC_SUPERVISOR_HANDLER, DeviceHandlerIF::GET_READ); + plTask->addComponent(objects::PLOC_SUPERVISOR_HANDLER, DeviceHandlerIF::SEND_READ); + plTask->addComponent(objects::PLOC_SUPERVISOR_HANDLER, DeviceHandlerIF::GET_READ); #endif #if OBSW_ADD_PLOC_MPSOC == 1 @@ -62,5 +64,7 @@ void scheduling::addMpsocSupvHandlers(PeriodicTaskIF* plTask) { plTask->addComponent(objects::PLOC_MPSOC_HANDLER, DeviceHandlerIF::GET_WRITE); plTask->addComponent(objects::PLOC_MPSOC_HANDLER, DeviceHandlerIF::SEND_READ); plTask->addComponent(objects::PLOC_MPSOC_HANDLER, DeviceHandlerIF::GET_READ); + plTask->addComponent(objects::PLOC_MPSOC_HANDLER, DeviceHandlerIF::SEND_READ); + plTask->addComponent(objects::PLOC_MPSOC_HANDLER, DeviceHandlerIF::GET_READ); #endif } diff --git a/mission/devices/devicedefinitions/SpBase.h b/mission/devices/devicedefinitions/SpBase.h index 22c138b5..11d67a59 100644 --- a/mission/devices/devicedefinitions/SpBase.h +++ b/mission/devices/devicedefinitions/SpBase.h @@ -46,6 +46,8 @@ class SpTcBase { void updateLenFromParams() { spParams.creator.setDataLenField(spParams.fullPayloadLen - 1); } const uint8_t* getFullPacket() const { return spParams.buf; } + const uint8_t* getPacketData() const { return spParams.buf + ccsds::HEADER_LEN; } + size_t getFullPacketLen() const { return spParams.creator.getFullPacketLen(); } uint16_t getApid() const { return spParams.creator.getApid(); }