Skip to content

Commit

Permalink
[OTA] Implement event support for OTA Requestor cluster
Browse files Browse the repository at this point in the history
  • Loading branch information
carol-apple committed Jan 19, 2022
1 parent fba51b9 commit f258fa9
Show file tree
Hide file tree
Showing 17 changed files with 249 additions and 84 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -1993,7 +1993,7 @@ server cluster OtaSoftwareUpdateRequestor = 42 {
info event DownloadError = 2 {
INT32U softwareVersion = 0;
INT64U bytesDownloaded = 1;
INT8U progressPercent = 2;
nullable INT8U progressPercent = 2;
nullable INT64S platformCode = 3;
}

Expand Down
2 changes: 1 addition & 1 deletion examples/lighting-app/lighting-common/lighting-app.matter
Original file line number Diff line number Diff line change
Expand Up @@ -905,7 +905,7 @@ server cluster OtaSoftwareUpdateRequestor = 42 {
info event DownloadError = 2 {
INT32U softwareVersion = 0;
INT64U bytesDownloaded = 1;
INT8U progressPercent = 2;
nullable INT8U progressPercent = 2;
nullable INT64S platformCode = 3;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -337,7 +337,7 @@ server cluster OtaSoftwareUpdateRequestor = 42 {
info event DownloadError = 2 {
INT32U softwareVersion = 0;
INT64U bytesDownloaded = 1;
INT8U progressPercent = 2;
nullable INT8U progressPercent = 2;
nullable INT64S platformCode = 3;
}

Expand Down
23 changes: 14 additions & 9 deletions src/app/clusters/ota-requestor/BDXDownloader.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@

#include "BDXDownloader.h"

#include <app/data-model/Nullable.h>
#include <lib/core/CHIPError.h>
#include <lib/support/BufferReader.h>
#include <lib/support/BytesToHex.h>
Expand All @@ -28,6 +29,8 @@
#include <transport/raw/MessageHeader.h>

using chip::OTADownloader;
using chip::app::Clusters::OtaSoftwareUpdateRequestor::OTAChangeReasonEnum;
using chip::app::DataModel::Nullable;
using chip::bdx::TransferSession;

namespace chip {
Expand Down Expand Up @@ -68,7 +71,7 @@ CHIP_ERROR BDXDownloader::BeginPrepareDownload()
VerifyOrReturnError(mImageProcessor != nullptr, CHIP_ERROR_INCORRECT_STATE);
ReturnErrorOnFailure(mImageProcessor->PrepareDownload());

SetState(State::kPreparing);
SetState(State::kPreparing, OTAChangeReasonEnum::kSuccess);

return CHIP_NO_ERROR;
}
Expand All @@ -79,7 +82,7 @@ CHIP_ERROR BDXDownloader::OnPreparedForDownload(CHIP_ERROR status)

if (status == CHIP_NO_ERROR)
{
SetState(State::kInProgress);
SetState(State::kInProgress, OTAChangeReasonEnum::kSuccess);

// Must call here because StartTransfer() should have prepared a ReceiveInit message, and now we should send it.
PollTransferSession();
Expand All @@ -88,7 +91,7 @@ CHIP_ERROR BDXDownloader::OnPreparedForDownload(CHIP_ERROR status)
{
ChipLogError(BDX, "failed to prepare download: %" CHIP_ERROR_FORMAT, status.Format());
mBdxTransfer.Reset();
SetState(State::kIdle);
SetState(State::kIdle, OTAChangeReasonEnum::kFailure);
}

return CHIP_NO_ERROR;
Expand All @@ -113,7 +116,7 @@ void BDXDownloader::OnDownloadTimeout()
{
mImageProcessor->Abort();
}
SetState(State::kIdle);
SetState(State::kIdle, OTAChangeReasonEnum::kTimeOut);
}
else
{
Expand All @@ -131,7 +134,7 @@ void BDXDownloader::EndDownload(CHIP_ERROR reason)
{
mImageProcessor->Abort();
}
SetState(State::kIdle);
SetState(State::kIdle, OTAChangeReasonEnum::kSuccess);

// Because AbortTransfer() will generate a StatusReport to send.
PollTransferSession();
Expand Down Expand Up @@ -174,14 +177,16 @@ CHIP_ERROR BDXDownloader::HandleBdxEvent(const chip::bdx::TransferSession::Outpu
if (outEvent.msgTypeData.HasMessageType(chip::bdx::MessageType::BlockAckEOF))
{
// BDX transfer is not complete until BlockAckEOF has been sent
SetState(State::kComplete);
SetState(State::kComplete, OTAChangeReasonEnum::kSuccess);
}
break;
}
case TransferSession::OutputEventType::kBlockReceived: {
chip::ByteSpan blockData(outEvent.blockdata.Data, outEvent.blockdata.Length);
ReturnErrorOnFailure(mImageProcessor->ProcessBlock(blockData));
mStateDelegate->OnUpdateProgressChanged(mImageProcessor->GetPercentComplete());
Nullable<uint8_t> percent;
mImageProcessor->GetPercentComplete(percent);
mStateDelegate->OnUpdateProgressChanged(percent);

// TODO: this will cause problems if Finalize() is not guaranteed to do its work after ProcessBlock().
if (outEvent.blockdata.IsEof)
Expand Down Expand Up @@ -219,13 +224,13 @@ CHIP_ERROR BDXDownloader::HandleBdxEvent(const chip::bdx::TransferSession::Outpu
return CHIP_NO_ERROR;
}

void BDXDownloader::SetState(State state)
void BDXDownloader::SetState(State state, OTAChangeReasonEnum reason)
{
mState = state;

if (mStateDelegate)
{
mStateDelegate->OnDownloadStateChanged(state);
mStateDelegate->OnDownloadStateChanged(state, reason);
}
}

Expand Down
9 changes: 5 additions & 4 deletions src/app/clusters/ota-requestor/BDXDownloader.h
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@

#include "OTADownloader.h"

#include <app-common/zap-generated/cluster-objects.h>
#include <lib/core/CHIPError.h>
#include <protocols/bdx/BdxTransferSession.h>
#include <system/SystemPacketBuffer.h>
Expand All @@ -49,10 +50,10 @@ class BDXDownloader : public chip::OTADownloader
{
public:
// Handle download state change
virtual void OnDownloadStateChanged(State state) = 0;
virtual void OnDownloadStateChanged(State state, app::Clusters::OtaSoftwareUpdateRequestor::OTAChangeReasonEnum reason) = 0;
// Handle update progress change
virtual void OnUpdateProgressChanged(uint8_t percent) = 0;
virtual ~StateDelegate() = default;
virtual void OnUpdateProgressChanged(app::DataModel::Nullable<uint8_t> percent) = 0;
virtual ~StateDelegate() = default;
};

// To be called when there is an incoming message to handle (of any protocol type)
Expand All @@ -77,7 +78,7 @@ class BDXDownloader : public chip::OTADownloader
private:
void PollTransferSession();
CHIP_ERROR HandleBdxEvent(const chip::bdx::TransferSession::OutputEvent & outEvent);
void SetState(State state);
void SetState(State state, app::Clusters::OtaSoftwareUpdateRequestor::OTAChangeReasonEnum reason);

chip::bdx::TransferSession mBdxTransfer;
MessagingDelegate * mMsgDelegate = nullptr;
Expand Down
2 changes: 1 addition & 1 deletion src/app/clusters/ota-requestor/OTADownloader.h
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ class OTADownloader
virtual ~OTADownloader() = default;

protected:
OTAImageProcessorInterface * mImageProcessor;
OTAImageProcessorInterface * mImageProcessor = nullptr;
State mState;
};

Expand Down
Loading

0 comments on commit f258fa9

Please sign in to comment.