diff --git a/examples/lighting-app/nrfconnect/main/AppTask.cpp b/examples/lighting-app/nrfconnect/main/AppTask.cpp index 8c4b1391f88dbf..f8d9f6a3b53cb9 100644 --- a/examples/lighting-app/nrfconnect/main/AppTask.cpp +++ b/examples/lighting-app/nrfconnect/main/AppTask.cpp @@ -143,6 +143,7 @@ int AppTask::Init() void AppTask::InitOTARequestor() { #if CONFIG_CHIP_OTA_REQUESTOR + sOTAImageProcessor.SetOTADownloader(&sBDXDownloader); sBDXDownloader.SetImageProcessorDelegate(&sOTAImageProcessor); sOTARequestor.SetOtaRequestorDriver(&sOTARequestorDriver); sOTARequestor.SetBDXDownloader(&sBDXDownloader); diff --git a/src/platform/nrfconnect/OTAImageProcessorImpl.cpp b/src/platform/nrfconnect/OTAImageProcessorImpl.cpp index a01417e956e247..c31f321a70872b 100644 --- a/src/platform/nrfconnect/OTAImageProcessorImpl.cpp +++ b/src/platform/nrfconnect/OTAImageProcessorImpl.cpp @@ -17,7 +17,9 @@ #include "OTAImageProcessorImpl.h" +#include #include +#include #include #include @@ -27,6 +29,13 @@ namespace chip { namespace DeviceLayer { CHIP_ERROR OTAImageProcessorImpl::PrepareDownload() +{ + VerifyOrReturnError(mDownloader != nullptr, CHIP_ERROR_INCORRECT_STATE); + + return DeviceLayer::SystemLayer().ScheduleLambda([this] { mDownloader->OnPreparedForDownload(PrepareDownloadImpl()); }); +} + +CHIP_ERROR OTAImageProcessorImpl::PrepareDownloadImpl() { ReturnErrorOnFailure(System::MapErrorZephyr(dfu_target_mcuboot_set_buf(mBuffer, sizeof(mBuffer)))); ReturnErrorOnFailure(System::MapErrorZephyr(dfu_target_reset())); @@ -50,7 +59,18 @@ CHIP_ERROR OTAImageProcessorImpl::Apply() CHIP_ERROR OTAImageProcessorImpl::ProcessBlock(ByteSpan & block) { - return System::MapErrorZephyr(dfu_target_write(block.data(), block.size())); + VerifyOrReturnError(mDownloader != nullptr, CHIP_ERROR_INCORRECT_STATE); + + // DFU target library buffers data internally, so do not clone the block data. + CHIP_ERROR error = System::MapErrorZephyr(dfu_target_write(block.data(), block.size())); + + // Report the result back to the downloader asynchronously. + return DeviceLayer::SystemLayer().ScheduleLambda([this, error] { + if (error == CHIP_NO_ERROR) + mDownloader->FetchNextData(); + else + mDownloader->EndDownload(error); + }); } } // namespace DeviceLayer diff --git a/src/platform/nrfconnect/OTAImageProcessorImpl.h b/src/platform/nrfconnect/OTAImageProcessorImpl.h index ef510f06f3c219..a9db9201ec6565 100644 --- a/src/platform/nrfconnect/OTAImageProcessorImpl.h +++ b/src/platform/nrfconnect/OTAImageProcessorImpl.h @@ -21,6 +21,9 @@ #include namespace chip { + +class OTADownloader; + namespace DeviceLayer { class OTAImageProcessorImpl : public OTAImageProcessorInterface @@ -28,6 +31,8 @@ class OTAImageProcessorImpl : public OTAImageProcessorInterface public: static constexpr size_t kBufferSize = CONFIG_CHIP_OTA_REQUESTOR_BUFFER_SIZE; + void SetOTADownloader(OTADownloader * downloader) { mDownloader = downloader; }; + CHIP_ERROR PrepareDownload() override; CHIP_ERROR Finalize() override; CHIP_ERROR Abort() override; @@ -35,6 +40,9 @@ class OTAImageProcessorImpl : public OTAImageProcessorInterface CHIP_ERROR ProcessBlock(ByteSpan & block) override; private: + CHIP_ERROR PrepareDownloadImpl(); + + OTADownloader * mDownloader = nullptr; uint8_t mBuffer[kBufferSize]; };