diff --git a/linux/payload/PlocMpsocHelper.cpp b/linux/payload/PlocMpsocHelper.cpp index e0b832a4..25bfc8d9 100644 --- a/linux/payload/PlocMpsocHelper.cpp +++ b/linux/payload/PlocMpsocHelper.cpp @@ -51,6 +51,16 @@ ReturnValue_t PlocMPSoCHelper::performOperation(uint8_t operationCode) { internalState = InternalState::IDLE; break; } + case InternalState::FLASH_READ: { + result = performFlashRead(); + if (result == returnvalue::OK) { + triggerEvent(MPSOC_FLASH_READ_SUCCESSFUL); + } else { + triggerEvent(MPSOC_FLASH_READ_FAILED); + } + internalState = InternalState::IDLE; + break; + } default: sif::debug << "PlocMPSoCHelper::performOperation: Invalid state" << std::endl; break; @@ -132,29 +142,32 @@ ReturnValue_t PlocMPSoCHelper::performFlashWrite() { size_t bytesRead = 0; while (remainingSize > 0) { if (terminate) { + flashfclose(); return returnvalue::OK; } - if (remainingSize > mpsoc::MAX_DATA_SIZE) { - dataLength = mpsoc::MAX_DATA_SIZE; + if (remainingSize > mpsoc::SP_MAX_DATA_SIZE) { + dataLength = mpsoc::SP_MAX_DATA_SIZE; } else { dataLength = remainingSize; } - if (file.is_open()) { - file.seekg(bytesRead, file.beg); - file.read(reinterpret_cast(fileBuf.data()), dataLength); - bytesRead += dataLength; - remainingSize -= dataLength; - } else { - return FILE_CLOSED_ACCIDENTALLY; + if (file.bad() or not file.is_open()) { + flashfclose(); + return FILE_WRITE_ERROR; } + file.seekg(bytesRead, file.beg); + file.read(reinterpret_cast(fileBuf.data()), dataLength); + bytesRead += dataLength; + remainingSize -= dataLength; (*sequenceCount)++; mpsoc::TcFlashWrite tc(spParams, *sequenceCount); result = tc.buildPacket(fileBuf.data(), dataLength); if (result != returnvalue::OK) { + flashfclose(); return result; } - result = handlePacketTransmission(tc); + result = handlePacketTransmissionNoReply(tc); if (result != returnvalue::OK) { + flashfclose(); return result; } } @@ -166,27 +179,68 @@ ReturnValue_t PlocMPSoCHelper::performFlashWrite() { } ReturnValue_t PlocMPSoCHelper::performFlashRead() { - std::ofstream ofile(flashReadAndWrite.obcFile); + std::error_code e; + std::ofstream ofile(flashReadAndWrite.obcFile, std::ios::trunc | std::ios::binary); if (ofile.bad()) { return returnvalue::FAILED; } ReturnValue_t result = flashfopen(); if (result != returnvalue::OK) { + std::filesystem::remove(flashReadAndWrite.obcFile, e); return result; } + size_t readSoFar = 0; + size_t nextReadSize = mpsoc::MAX_FLASH_READ_DATA_SIZE; + while (readSoFar < flashReadAndWrite.totalReadSize) { + if (terminate) { + std::filesystem::remove(flashReadAndWrite.obcFile, e); + flashfclose(); + return returnvalue::OK; + } + nextReadSize = mpsoc::MAX_FLASH_READ_DATA_SIZE; + if (flashReadAndWrite.totalReadSize - readSoFar < mpsoc::MAX_FLASH_READ_DATA_SIZE) { + nextReadSize = flashReadAndWrite.totalReadSize - readSoFar; + } + if (ofile.bad() or not ofile.is_open()) { + std::filesystem::remove(flashReadAndWrite.obcFile, e); + flashfclose(); + return FILE_READ_ERROR; + } + mpsoc::TcFlashRead flashReadRequest(spParams, *sequenceCount); + result = flashReadRequest.buildPacket(nextReadSize); + if (result != returnvalue::OK) { + std::filesystem::remove(flashReadAndWrite.obcFile, e); + flashfclose(); + return result; + } + result = handlePacketTransmissionFlashRead(flashReadRequest); + if (result != returnvalue::OK) { + std::filesystem::remove(flashReadAndWrite.obcFile, e); + flashfclose(); + return result; + } + result = handleFlashReadReply(ofile, nextReadSize); + if (result != returnvalue::OK) { + std::filesystem::remove(flashReadAndWrite.obcFile, e); + flashfclose(); + return result; + } + (*sequenceCount)++; + readSoFar += nextReadSize; + } return result; } ReturnValue_t PlocMPSoCHelper::flashfopen() { - ReturnValue_t result = returnvalue::OK; spParams.buf = commandBuffer; (*sequenceCount)++; mpsoc::FlashFopen flashFopen(spParams, *sequenceCount); - result = flashFopen.createPacket(flashReadAndWrite.mpsocFile, mpsoc::FlashFopen::APPEND); + ReturnValue_t result = + flashFopen.createPacket(flashReadAndWrite.mpsocFile, mpsoc::FlashFopen::APPEND); if (result != returnvalue::OK) { return result; } - result = handlePacketTransmission(flashFopen); + result = handlePacketTransmissionNoReply(flashFopen); if (result != returnvalue::OK) { return result; } @@ -194,24 +248,18 @@ ReturnValue_t PlocMPSoCHelper::flashfopen() { } ReturnValue_t PlocMPSoCHelper::flashfclose() { - ReturnValue_t result = returnvalue::OK; spParams.buf = commandBuffer; (*sequenceCount)++; mpsoc::FlashFclose flashFclose(spParams, *sequenceCount); - result = flashFclose.createPacket(flashReadAndWrite.mpsocFile); + ReturnValue_t result = flashFclose.createPacket(flashReadAndWrite.mpsocFile); if (result != returnvalue::OK) { return result; } - result = handlePacketTransmission(flashFclose); - if (result != returnvalue::OK) { - return result; - } - return returnvalue::OK; + return handlePacketTransmissionNoReply(flashFclose); } -ReturnValue_t PlocMPSoCHelper::handlePacketTransmission(ploc::SpTcBase& tc) { - ReturnValue_t result = returnvalue::OK; - result = sendCommand(tc); +ReturnValue_t PlocMPSoCHelper::handlePacketTransmissionFlashRead(mpsoc::TcFlashRead& tc) { + ReturnValue_t result = sendCommand(tc); if (result != returnvalue::OK) { return result; } @@ -219,11 +267,24 @@ ReturnValue_t PlocMPSoCHelper::handlePacketTransmission(ploc::SpTcBase& tc) { if (result != returnvalue::OK) { return result; } - result = handleExe(); + result = handleTmReception(ccsds::HEADER_LEN + mpsoc::FLASH_READ_MIN_OVERHEAD + tc.readSize + + mpsoc::CRC_SIZE); if (result != returnvalue::OK) { return result; } - return returnvalue::OK; + return handleExe(); +} + +ReturnValue_t PlocMPSoCHelper::handlePacketTransmissionNoReply(ploc::SpTcBase& tc) { + ReturnValue_t result = sendCommand(tc); + if (result != returnvalue::OK) { + return result; + } + result = handleAck(); + if (result != returnvalue::OK) { + return result; + } + return handleExe(); } ReturnValue_t PlocMPSoCHelper::sendCommand(ploc::SpTcBase& tc) { @@ -323,6 +384,46 @@ ReturnValue_t PlocMPSoCHelper::handleTmReception(size_t remainingBytes) { return result; } +ReturnValue_t PlocMPSoCHelper::handleFlashReadReply(std::ofstream& ofile, size_t expectedReadLen) { + SpTmReader tmPacket(tmBuf.data(), tmBuf.size()); + ReturnValue_t result = checkReceivedTm(tmPacket); + if (result != returnvalue::OK) { + return result; + } + uint16_t apid = tmPacket.getApid(); + if (apid != mpsoc::apid::TM_FLASH_READ_REPORT) { + triggerEvent(MPSOC_FLASH_READ_PACKET_ERROR, FlashReadErrorType::FLASH_READ_APID_ERROR); + sif::warning << "PLOC MPSoC Flash Read: Unexpected APID" << std::endl; + return result; + } + const uint8_t* packetData = tmPacket.getPacketData(); + size_t deserDummy = tmPacket.getPacketDataLen() - mpsoc::CRC_SIZE; + uint32_t receivedReadLen = 0; + std::string receivedShortName = std::string(reinterpret_cast(packetData), 12); + if (receivedShortName != flashReadAndWrite.mpsocFile.substr(0, 12)) { + sif::warning << "PLOC MPSoC Flash Read: Missmatch between request file name and " + "received file name" + << std::endl; + triggerEvent(MPSOC_FLASH_READ_PACKET_ERROR, FlashReadErrorType::FLASH_READ_FILENAME_ERROR); + return returnvalue::FAILED; + } + packetData += 12; + result = SerializeAdapter::deSerialize(&receivedReadLen, &packetData, &deserDummy, + SerializeIF::Endianness::NETWORK); + if (result != returnvalue::OK) { + return result; + } + if (receivedReadLen != expectedReadLen) { + sif::warning << "PLOC MPSoC Flash Read: Missmatch between request read length and " + "received read length" + << std::endl; + triggerEvent(MPSOC_FLASH_READ_PACKET_ERROR, FlashReadErrorType::FLASH_READ_READLEN_ERROR); + return returnvalue::FAILED; + } + ofile.write(reinterpret_cast(packetData), receivedReadLen); + return returnvalue::OK; +} + ReturnValue_t PlocMPSoCHelper::checkReceivedTm(SpTmReader& reader) { ReturnValue_t result = reader.checkSize(); if (result != returnvalue::OK) { diff --git a/linux/payload/PlocMpsocHelper.h b/linux/payload/PlocMpsocHelper.h index 1b74b568..24dd50a1 100644 --- a/linux/payload/PlocMpsocHelper.h +++ b/linux/payload/PlocMpsocHelper.h @@ -29,7 +29,7 @@ class PlocMPSoCHelper : public SystemObject, public ExecutableObjectIF { //! [EXPORT] : [COMMENT] Flash write fails static const Event MPSOC_FLASH_WRITE_FAILED = MAKE_EVENT(0, severity::LOW); //! [EXPORT] : [COMMENT] Flash write successful - static const Event MPSOC_FLASH_WRITE_SUCCESSFUL = MAKE_EVENT(1, severity::LOW); + static const Event MPSOC_FLASH_WRITE_SUCCESSFUL = MAKE_EVENT(1, severity::INFO); //! [EXPORT] : [COMMENT] Communication interface returned failure when trying to send the command //! to the MPSoC //! P1: Return value returned by the communication interface sendMessage function @@ -71,6 +71,15 @@ class PlocMPSoCHelper : public SystemObject, public ExecutableObjectIF { static const Event MPSOC_HELPER_SEQ_CNT_MISMATCH = MAKE_EVENT(11, severity::LOW); static const Event MPSOC_TM_SIZE_ERROR = MAKE_EVENT(12, severity::LOW); static const Event MPSOC_TM_CRC_MISSMATCH = MAKE_EVENT(13, severity::LOW); + static const Event MPSOC_FLASH_READ_PACKET_ERROR = MAKE_EVENT(14, severity::LOW); + static const Event MPSOC_FLASH_READ_FAILED = MAKE_EVENT(15, severity::LOW); + static const Event MPSOC_FLASH_READ_SUCCESSFUL = MAKE_EVENT(16, severity::INFO); + + enum FlashReadErrorType : uint32_t { + FLASH_READ_APID_ERROR = 0, + FLASH_READ_FILENAME_ERROR = 1, + FLASH_READ_READLEN_ERROR = 2 + }; PlocMPSoCHelper(object_id_t objectId); virtual ~PlocMPSoCHelper(); @@ -104,8 +113,10 @@ class PlocMPSoCHelper : public SystemObject, public ExecutableObjectIF { private: static const uint8_t INTERFACE_ID = CLASS_ID::PLOC_MPSOC_HELPER; - //! [EXPORT] : [COMMENT] File accidentally close - static const ReturnValue_t FILE_CLOSED_ACCIDENTALLY = MAKE_RETURN_CODE(0xA0); + //! [EXPORT] : [COMMENT] File error occured for file transfers from OBC to the MPSoC. + static const ReturnValue_t FILE_WRITE_ERROR = MAKE_RETURN_CODE(0xA0); + //! [EXPORT] : [COMMENT] File error occured for file transfers from MPSoC to OBC. + static const ReturnValue_t FILE_READ_ERROR = MAKE_RETURN_CODE(0xA1); // Maximum number of times the communication interface retries polling data from the reply // buffer @@ -117,7 +128,7 @@ class PlocMPSoCHelper : public SystemObject, public ExecutableObjectIF { }; struct FlashRead : public FlashInfo { - size_t readSize = 0; + size_t totalReadSize = 0; }; struct FlashRead flashReadAndWrite; @@ -137,7 +148,7 @@ class PlocMPSoCHelper : public SystemObject, public ExecutableObjectIF { SpacePacketCreator creator; ploc::SpTcParams spParams = ploc::SpTcParams(creator); - std::array fileBuf{}; + std::array fileBuf{}; std::array tmBuf{}; bool terminate = false; @@ -157,7 +168,9 @@ class PlocMPSoCHelper : public SystemObject, public ExecutableObjectIF { ReturnValue_t performFlashRead(); ReturnValue_t flashfopen(); ReturnValue_t flashfclose(); - ReturnValue_t handlePacketTransmission(ploc::SpTcBase& tc); + ReturnValue_t handlePacketTransmissionNoReply(ploc::SpTcBase& tc); + ReturnValue_t handlePacketTransmissionFlashRead(mpsoc::TcFlashRead& tc); + ReturnValue_t handleFlashReadReply(std::ofstream& ofile, size_t expectedReadLen); ReturnValue_t sendCommand(ploc::SpTcBase& tc); ReturnValue_t receive(uint8_t* data, size_t* readBytes, size_t requestBytes); ReturnValue_t handleAck(); diff --git a/linux/payload/plocMpscoDefs.h b/linux/payload/plocMpscoDefs.h index 1ef72bf5..4c98e6e1 100644 --- a/linux/payload/plocMpscoDefs.h +++ b/linux/payload/plocMpscoDefs.h @@ -102,7 +102,8 @@ static const uint16_t TC_DOWNLINK_PWR_ON = 0x113; static const uint16_t TC_MEM_WRITE = 0x114; static const uint16_t TC_MEM_READ = 0x115; static const uint16_t TC_CAM_TAKE_PIC = 0x116; -static const uint16_t TC_FLASHWRITE = 0x117; +static constexpr uint16_t TC_FLASHWRITE = 0x117; +static constexpr uint16_t TC_FLASHREAD = 0x118; static const uint16_t TC_FLASHFOPEN = 0x119; static const uint16_t TC_FLASHFCLOSE = 0x11A; static const uint16_t TC_FLASHDELETE = 0x11C; @@ -115,11 +116,12 @@ static constexpr uint16_t TM_HK_GET_REPORT = 0x408; static const uint16_t TC_DOWNLINK_PWR_OFF = 0x124; static const uint16_t TC_CAM_CMD_SEND = 0x12C; static const uint16_t TC_SIMPLEX_SEND_FILE = 0x130; -static const uint16_t TM_MEMORY_READ_REPORT = 0x404; static const uint16_t ACK_SUCCESS = 0x400; static const uint16_t ACK_FAILURE = 0x401; static const uint16_t EXE_SUCCESS = 0x402; static const uint16_t EXE_FAILURE = 0x403; +static const uint16_t TM_MEMORY_READ_REPORT = 0x404; +static const uint16_t TM_FLASH_READ_REPORT = 0x405; static const uint16_t TM_CAM_CMD_RPT = 0x407; } // namespace apid @@ -153,9 +155,13 @@ static const uint16_t LENGTH_TC_MEM_READ = 8; * at sheet README */ static constexpr size_t SP_MAX_SIZE = 1024; -static const size_t MAX_REPLY_SIZE = SP_MAX_SIZE * 3; -static const size_t MAX_COMMAND_SIZE = SP_MAX_SIZE; -static const size_t MAX_DATA_SIZE = 1016; +static constexpr size_t MAX_REPLY_SIZE = SP_MAX_SIZE * 3; +static constexpr size_t MAX_COMMAND_SIZE = SP_MAX_SIZE; +// 1016 bytes. +static constexpr size_t SP_MAX_DATA_SIZE = SP_MAX_SIZE - ccsds::HEADER_LEN - CRC_SIZE; +static constexpr size_t FLASH_READ_MIN_OVERHEAD = 16; +// 1000 bytes. +static const size_t MAX_FLASH_READ_DATA_SIZE = SP_MAX_DATA_SIZE - FLASH_READ_MIN_OVERHEAD; /** * The replay write sequence command has a maximum delay for the execution report which amounts to @@ -421,8 +427,8 @@ class TcFlashWrite : public ploc::SpTcBase { ReturnValue_t buildPacket(const uint8_t* writeData, uint32_t writeLen_) { ReturnValue_t result = returnvalue::OK; writeLen = writeLen_; - if (writeLen > MAX_DATA_SIZE) { - sif::debug << "FlashWrite::createPacket: Command data too big" << std::endl; + if (writeLen > SP_MAX_DATA_SIZE) { + sif::error << "TcFlashWrite: Command data too big" << std::endl; return returnvalue::FAILED; } spParams.setFullPayloadLen(static_cast(writeLen) + 4 + CRC_SIZE); @@ -438,9 +444,9 @@ class TcFlashWrite : public ploc::SpTcBase { } std::memcpy(payloadStart + sizeof(writeLen), writeData, writeLen); updateSpFields(); - auto res = checkSizeAndSerializeHeader(); - if (res != returnvalue::OK) { - return res; + result = checkSizeAndSerializeHeader(); + if (result != returnvalue::OK) { + return result; } return calcAndSetCrc(); } @@ -449,6 +455,43 @@ class TcFlashWrite : public ploc::SpTcBase { uint32_t writeLen = 0; }; +class TcFlashRead : public ploc::SpTcBase { + public: + TcFlashRead(ploc::SpTcParams params, uint16_t sequenceCount) + : ploc::SpTcBase(params, apid::TC_FLASHWRITE, sequenceCount) {} + + ReturnValue_t buildPacket(uint32_t readLen) { + if (readLen > MAX_FLASH_READ_DATA_SIZE) { + sif::error << "TcFlashRead: Read length " << readLen << " too large" << std::endl; + return returnvalue::FAILED; + } + spParams.setFullPayloadLen(readLen + FLASH_READ_MIN_OVERHEAD + CRC_SIZE); + ReturnValue_t result = checkPayloadLen(); + if (result != returnvalue::OK) { + return result; + } + size_t serializedSize = ccsds::HEADER_LEN; + result = SerializeAdapter::serialize(&readLen, payloadStart, &serializedSize, spParams.maxSize, + SerializeIF::Endianness::NETWORK); + if (result != returnvalue::OK) { + return result; + } + updateSpFields(); + result = checkSizeAndSerializeHeader(); + if (result != returnvalue::OK) { + return result; + } + result = calcAndSetCrc(); + if (result != returnvalue::OK) { + return result; + } + readSize = readLen; + return result; + } + + uint32_t readSize = 0; +}; + /** * @brief Class to help creation of flash delete command. */