Skip to content

Commit

Permalink
[nrfconnect] Fix OTAImageProcessorImpl (#13190)
Browse files Browse the repository at this point in the history
Use asynchronous API to report result of the download
initialization or a block write back to the BDXDownloader.
  • Loading branch information
Damian-Nordic authored and pull[bot] committed Feb 28, 2022
1 parent 44fd091 commit 2407073
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 1 deletion.
1 change: 1 addition & 0 deletions examples/lighting-app/nrfconnect/main/AppTask.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
22 changes: 21 additions & 1 deletion src/platform/nrfconnect/OTAImageProcessorImpl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,9 @@

#include "OTAImageProcessorImpl.h"

#include <app/clusters/ota-requestor/OTADownloader.h>
#include <lib/support/CodeUtils.h>
#include <platform/CHIPDeviceLayer.h>
#include <system/SystemError.h>

#include <dfu/dfu_target.h>
Expand All @@ -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()));
Expand All @@ -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
Expand Down
8 changes: 8 additions & 0 deletions src/platform/nrfconnect/OTAImageProcessorImpl.h
Original file line number Diff line number Diff line change
Expand Up @@ -21,20 +21,28 @@
#include <platform/OTAImageProcessor.h>

namespace chip {

class OTADownloader;

namespace DeviceLayer {

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;
CHIP_ERROR Apply() override;
CHIP_ERROR ProcessBlock(ByteSpan & block) override;

private:
CHIP_ERROR PrepareDownloadImpl();

OTADownloader * mDownloader = nullptr;
uint8_t mBuffer[kBufferSize];
};

Expand Down

0 comments on commit 2407073

Please sign in to comment.