diff --git a/linux/devices/ploc/PlocSupervisorHandler.cpp b/linux/devices/ploc/PlocSupervisorHandler.cpp index 3d00d440..1ad77883 100644 --- a/linux/devices/ploc/PlocSupervisorHandler.cpp +++ b/linux/devices/ploc/PlocSupervisorHandler.cpp @@ -114,14 +114,13 @@ ReturnValue_t PlocSupervisorHandler::executeAction(ActionId_t actionId, if (size > config::MAX_PATH_SIZE + config::MAX_FILENAME_SIZE) { return SupvReturnValuesIF::FILENAME_TOO_LONG; } - std::string file = ""; - uint8_t memoryId = 0; - uint32_t startAddress = 0; - result = extractUpdateCommand(data, size, &file, &memoryId, &startAddress); + UpdateParams params; + result = extractUpdateCommand(data, size, params); if (result != RETURN_OK) { return result; } - result = supvHelper->startUpdate(file, memoryId, startAddress); + result = supvHelper->performUpdate(params.file, params.memId, params.startAddr, + params.bytesWritten, params.seqCount); if (result != RETURN_OK) { return result; } @@ -1974,24 +1973,23 @@ ReturnValue_t PlocSupervisorHandler::getTimeStampString(std::string& timeStamp) } ReturnValue_t PlocSupervisorHandler::extractUpdateCommand(const uint8_t* commandData, size_t size, - std::string* file, uint8_t* memoryId, - uint32_t* startAddress) { + UpdateParams& params) { ReturnValue_t result = RETURN_OK; - if (size > (config::MAX_FILENAME_SIZE + config::MAX_PATH_SIZE + sizeof(*memoryId)) + - sizeof(*startAddress)) { + if (size > (config::MAX_FILENAME_SIZE + config::MAX_PATH_SIZE + sizeof(params.memId)) + + sizeof(params.startAddr)) { sif::warning << "PlocSupervisorHandler::extractUpdateCommand: Data size too big" << std::endl; return SupvReturnValuesIF::INVALID_LENGTH; } - *file = std::string(reinterpret_cast(commandData)); - if (file->size() > (config::MAX_FILENAME_SIZE + config::MAX_PATH_SIZE)) { + params.file = std::string(reinterpret_cast(commandData)); + if (params.file.size() > (config::MAX_FILENAME_SIZE + config::MAX_PATH_SIZE)) { sif::warning << "PlocSupervisorHandler::extractUpdateCommand: Filename too long" << std::endl; return SupvReturnValuesIF::FILENAME_TOO_LONG; } - *memoryId = *(commandData + file->size() + SIZE_NULL_TERMINATOR); + params.memId = *(commandData + params.file.size() + SIZE_NULL_TERMINATOR); const uint8_t* startAddressPtr = - commandData + file->size() + SIZE_NULL_TERMINATOR + sizeof(*memoryId); - size_t remainingSize = 4; - result = SerializeAdapter::deSerialize(startAddress, startAddressPtr, &remainingSize, + commandData + params.file.size() + SIZE_NULL_TERMINATOR + sizeof(params.memId); + size_t remainingSize = 10; + result = SerializeAdapter::deSerialize(¶ms.startAddr, &startAddressPtr, &remainingSize, SerializeIF::Endianness::BIG); if (result != RETURN_OK) { sif::warning @@ -1999,6 +1997,22 @@ ReturnValue_t PlocSupervisorHandler::extractUpdateCommand(const uint8_t* command << std::endl; return result; } + result = SerializeAdapter::deSerialize(¶ms.bytesWritten, &startAddressPtr, &remainingSize, + SerializeIF::Endianness::BIG); + if (result != RETURN_OK) { + sif::warning << "PlocSupervisorHandler::extractUpdateCommand: Failed to deserialize bytes " + "already written" + << std::endl; + return result; + } + result = SerializeAdapter::deSerialize(¶ms.seqCount, &startAddressPtr, &remainingSize, + SerializeIF::Endianness::BIG); + if (result != RETURN_OK) { + sif::warning + << "PlocSupervisorHandler::extractUpdateCommand: Failed to deserialize start sequence count" + << std::endl; + return result; + } return RETURN_OK; } diff --git a/linux/devices/ploc/PlocSupervisorHandler.h b/linux/devices/ploc/PlocSupervisorHandler.h index ebd059f9..7f0b2574 100644 --- a/linux/devices/ploc/PlocSupervisorHandler.h +++ b/linux/devices/ploc/PlocSupervisorHandler.h @@ -367,8 +367,15 @@ class PlocSupervisorHandler : public DeviceHandlerBase { ReturnValue_t prepareSetShutdownTimeoutCmd(const uint8_t* commandData); - ReturnValue_t extractUpdateCommand(const uint8_t* commandData, size_t size, std::string* file, - uint8_t* memoryId, uint32_t* startAddress); + struct UpdateParams { + std::string file; + uint8_t memId; + uint32_t startAddr; + uint32_t bytesWritten; + uint16_t seqCount; + }; + + ReturnValue_t extractUpdateCommand(const uint8_t* commandData, size_t size, UpdateParams& params); ReturnValue_t eventSubscription(); ReturnValue_t handleExecutionSuccessReport(const uint8_t* data); diff --git a/linux/devices/ploc/PlocSupvHelper.cpp b/linux/devices/ploc/PlocSupvHelper.cpp index c5876793..c2a46c68 100644 --- a/linux/devices/ploc/PlocSupvHelper.cpp +++ b/linux/devices/ploc/PlocSupvHelper.cpp @@ -99,8 +99,14 @@ ReturnValue_t PlocSupvHelper::setComIF(UartComIF* uartComIF_) { void PlocSupvHelper::setComCookie(CookieIF* comCookie_) { comCookie = comCookie_; } -ReturnValue_t PlocSupvHelper::startUpdate(std::string file, uint8_t memoryId, uint32_t startAddress, - size_t startBytesWritten, uint16_t initSeqCount) { +ReturnValue_t PlocSupvHelper::startUpdate(std::string file, uint8_t memoryId, + uint32_t startAddress) { + return performUpdate(file, memoryId, startAddress, 0, 1); +} + +ReturnValue_t PlocSupvHelper::performUpdate(std::string file, uint8_t memoryId, + uint32_t startAddress, size_t startBytesWritten, + uint16_t initSeqCount) { ReturnValue_t result = RETURN_OK; #ifdef XIPHOS_Q7S result = FilesystemHelper::checkPath(file); @@ -127,7 +133,6 @@ ReturnValue_t PlocSupvHelper::startUpdate(std::string file, uint8_t memoryId, ui update.memoryId = memoryId; update.startAddress = startAddress; update.progressPercent = 0; - update.remainingSize = update.length; update.bytesWritten = startBytesWritten; update.packetNum = 1; update.sequenceCount = initSeqCount; @@ -214,16 +219,19 @@ ReturnValue_t PlocSupvHelper::writeUpdatePackets() { std::ifstream file(update.file, std::ifstream::binary); uint16_t dataLength = 0; ccsds::SequenceFlags seqFlags; - while (update.remainingSize > 0) { + while (update.bytesWritten < update.length) { if (terminate) { terminate = false; triggerEvent(TERMINATED_UPDATE_PROCEDURE); return PROCESS_TERMINATED; } - if (update.remainingSize > supv::WriteMemory::CHUNK_MAX) { + size_t remainingSize = update.length - update.bytesWritten; + bool lastSegment = false; + if (remainingSize > supv::WriteMemory::CHUNK_MAX) { dataLength = supv::WriteMemory::CHUNK_MAX; } else { - dataLength = static_cast(update.remainingSize); + lastSegment = true; + dataLength = static_cast(remainingSize); } if (file.is_open()) { file.seekg(update.bytesWritten, std::ios::beg); @@ -239,26 +247,14 @@ ReturnValue_t PlocSupvHelper::writeUpdatePackets() { } if (update.bytesWritten == 0) { seqFlags = ccsds::SequenceFlags::FIRST_SEGMENT; - } else if (update.remainingSize == 0) { + } else if (lastSegment) { seqFlags = ccsds::SequenceFlags::LAST_SEGMENT; } else { seqFlags = ccsds::SequenceFlags::CONTINUATION; } resetSpParams(); - supv::WriteMemory packet(spParams); - result = packet.buildPacket(seqFlags, update.sequenceCount, update.memoryId, - update.startAddress + update.bytesWritten, dataLength, tempData); - if (result != RETURN_OK) { - triggerEvent(WRITE_MEMORY_FAILED, update.packetNum); - return result; - } - result = handlePacketTransmission(packet); - if (result != RETURN_OK) { - triggerEvent(WRITE_MEMORY_FAILED, update.packetNum); - return result; - } - uint8_t progPercent = - static_cast(std::floor(static_cast(update.bytesWritten) / update.length)); + float progress = static_cast(update.bytesWritten) / update.length; + uint8_t progPercent = std::floor(progress * 100); if (progPercent > update.progressPercent) { update.progressPercent = progPercent; if (progPercent % 5 == 0) { @@ -266,8 +262,19 @@ ReturnValue_t PlocSupvHelper::writeUpdatePackets() { triggerEvent(SUPV_UPDATE_PROGRESS, update.bytesWritten, update.sequenceCount); } } + supv::WriteMemory packet(spParams); + result = packet.buildPacket(seqFlags, update.sequenceCount, update.memoryId, + update.startAddress + update.bytesWritten, dataLength, tempData); + if (result != RETURN_OK) { + triggerEvent(WRITE_MEMORY_FAILED, update.bytesWritten, update.sequenceCount); + return result; + } + result = handlePacketTransmission(packet); + if (result != RETURN_OK) { + triggerEvent(WRITE_MEMORY_FAILED, update.bytesWritten, update.sequenceCount); + return result; + } update.sequenceCount++; - update.remainingSize -= dataLength; update.packetNum += 1; update.bytesWritten += dataLength; diff --git a/linux/devices/ploc/PlocSupvHelper.h b/linux/devices/ploc/PlocSupvHelper.h index 0e53a9b6..d3bfc49c 100644 --- a/linux/devices/ploc/PlocSupvHelper.h +++ b/linux/devices/ploc/PlocSupvHelper.h @@ -83,8 +83,8 @@ class PlocSupvHelper : public SystemObject, public ExecutableObjectIF, public Ha //! P1: Return value //! P2: Apid of command for which the reception of the execution report failed static const Event EXE_RECEPTION_FAILURE = MAKE_EVENT(18, severity::LOW); - //! [EXPORT] : [COMMENT] Update procedure failed when sending packet with number P1 - //! P1: Packet number for which the memory write command fails + //! [EXPORT] : [COMMENT] Update procedure failed when sending packet. + //! P1: Bytes written, P2: Sequence Count static const Event WRITE_MEMORY_FAILED = MAKE_EVENT(19, severity::LOW); static const Event SUPV_REPLY_SIZE_MISSMATCH = MAKE_EVENT(20, severity::LOW); static const Event SUPV_REPLY_CRC_MISSMATCH = MAKE_EVENT(21, severity::LOW); @@ -111,8 +111,9 @@ class PlocSupvHelper : public SystemObject, public ExecutableObjectIF, public Ha * * @return RETURN_OK if successful, otherwise error return value */ - ReturnValue_t startUpdate(std::string file, uint8_t memoryId, uint32_t startAddress, - size_t startBytesWritten = 0, uint16_t initSeqCount = 1); + ReturnValue_t performUpdate(std::string file, uint8_t memoryId, uint32_t startAddress, + size_t startBytesWritten, uint16_t initSeqCount); + ReturnValue_t startUpdate(std::string file, uint8_t memoryId, uint32_t startAddress); /** * @brief This initiate the continuation of a failed update. @@ -158,7 +159,7 @@ class PlocSupvHelper : public SystemObject, public ExecutableObjectIF, public Ha // Size of update uint32_t length; uint32_t crc; - size_t remainingSize; + // size_t remainingSize; size_t bytesWritten; uint32_t packetNum; uint16_t sequenceCount;