Skip to content

Commit

Permalink
[ota-requestor] Add generic OTA requestor driver (#13210)
Browse files Browse the repository at this point in the history
* [ota-requestor] Add generic OTA requestor driver

1. Extend OTARequestorDriver interface so it can be notified
   of various OTA Requestor operations.
2. Use OTARequestorDriver in OTARequestor to report results
   of operations.
3. Implement GenericOTARequestorDriver that provides default
   behavior for handling OTARequestor events. For example,
   it respects DelayedActionTime arguments.
4. Use the driver in existing examples.

* Restyled by whitespace

Co-authored-by: Restyled.io <[email protected]>
  • Loading branch information
Damian-Nordic and restyled-commits authored Dec 28, 2021
1 parent 17520c3 commit eab67b3
Show file tree
Hide file tree
Showing 20 changed files with 423 additions and 349 deletions.
10 changes: 3 additions & 7 deletions examples/lighting-app/nrfconnect/main/AppTask.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@
#if CONFIG_CHIP_OTA_REQUESTOR
#include <app/clusters/ota-requestor/BDXDownloader.h>
#include <app/clusters/ota-requestor/OTARequestor.h>
#include <platform/GenericOTARequestorDriver.h>
#include <platform/nrfconnect/OTAImageProcessorImpl.h>
#endif

Expand Down Expand Up @@ -72,13 +73,7 @@ bool sIsThreadEnabled = false;
bool sHaveBLEConnections = false;

#if CONFIG_CHIP_OTA_REQUESTOR
class DummyOTARequestorDriver : public chip::OTARequestorDriver
{
bool CheckImageDownloadAllowed() override { return true; }
chip::UserConsentAction RequestUserConsent() override { return chip::ImmediateYes; }
void ImageDownloadComplete() override {}
} sOTARequestorDriver;

GenericOTARequestorDriver sOTARequestorDriver;
OTAImageProcessorImpl sOTAImageProcessor;
chip::BDXDownloader sBDXDownloader;
chip::OTARequestor sOTARequestor;
Expand Down Expand Up @@ -145,6 +140,7 @@ void AppTask::InitOTARequestor()
#if CONFIG_CHIP_OTA_REQUESTOR
sOTAImageProcessor.SetOTADownloader(&sBDXDownloader);
sBDXDownloader.SetImageProcessorDelegate(&sOTAImageProcessor);
sOTARequestorDriver.Init(&sOTARequestor, &sOTAImageProcessor);
sOTARequestor.SetOtaRequestorDriver(&sOTARequestorDriver);
sOTARequestor.SetBDXDownloader(&sBDXDownloader);
sOTARequestor.SetServerInstance(&chip::Server::GetInstance());
Expand Down
5 changes: 3 additions & 2 deletions examples/ota-requestor-app/esp32/main/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@
#include <lib/support/ErrorStr.h>

#include "OTAImageProcessorImpl.h"
#include "OTARequestorDriverImpl.h"
#include "platform/GenericOTARequestorDriver.h"
#include "platform/OTARequestorInterface.h"
#include <argtable3/argtable3.h>
#include <esp_console.h>
Expand All @@ -65,7 +65,7 @@ static DeviceCallbacks EchoCallbacks;
CmdArgs applyUpdateCmdArgs;

OTARequestor gRequestorCore;
OTARequestorDriverImpl gRequestorUser;
GenericOTARequestorDriver gRequestorUser;
BDXDownloader gDownloader;
OTAImageProcessorImpl gImageProcessor;
} // namespace
Expand Down Expand Up @@ -149,6 +149,7 @@ extern "C" void app_main()

gImageProcessor.SetOTADownloader(&gDownloader);
gDownloader.SetImageProcessorDelegate(&gImageProcessor);
gRequestorUser.Init(&gRequestorCore, &gImageProcessor);

gRequestorCore.SetBDXDownloader(&gDownloader);
}
5 changes: 3 additions & 2 deletions examples/ota-requestor-app/linux/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,8 @@

#include "app/clusters/ota-requestor/BDXDownloader.h"
#include "app/clusters/ota-requestor/OTARequestor.h"
#include "platform/GenericOTARequestorDriver.h"
#include "platform/Linux/OTAImageProcessorImpl.h"
#include "platform/Linux/OTARequestorDriverImpl.h"

using chip::BDXDownloader;
using chip::ByteSpan;
Expand All @@ -52,7 +52,7 @@ using namespace chip::Messaging;
using namespace chip::app::Clusters::OtaSoftwareUpdateProvider::Commands;

OTARequestor gRequestorCore;
OTARequestorDriverImpl gRequestorUser;
DeviceLayer::GenericOTARequestorDriver gRequestorUser;
BDXDownloader gDownloader;
OTAImageProcessorImpl gImageProcessor;

Expand Down Expand Up @@ -201,6 +201,7 @@ int main(int argc, char * argv[])

// Connect the Requestor and Requestor Driver objects
gRequestorCore.SetOtaRequestorDriver(&gRequestorUser);
gRequestorUser.Init(&gRequestorCore, &gImageProcessor);

// WARNING: this is probably not realistic to know such details of the image or to even have an OTADownloader instantiated at
// the beginning of program execution. We're using hardcoded values here for now since this is a reference application.
Expand Down
22 changes: 16 additions & 6 deletions src/app/clusters/ota-requestor/BDXDownloader.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ CHIP_ERROR BDXDownloader::BeginPrepareDownload()
VerifyOrReturnError(mImageProcessor != nullptr, CHIP_ERROR_INCORRECT_STATE);
ReturnErrorOnFailure(mImageProcessor->PrepareDownload());

mState = State::kPreparing;
SetState(State::kPreparing);

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

if (status == CHIP_NO_ERROR)
{
mState = State::kInProgress;
SetState(State::kInProgress);

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

return CHIP_NO_ERROR;
Expand All @@ -110,7 +110,7 @@ void BDXDownloader::OnDownloadTimeout()
{
mImageProcessor->Abort();
}
mState = State::kIdle;
SetState(State::kIdle);
}
else
{
Expand All @@ -128,7 +128,7 @@ void BDXDownloader::EndDownload(CHIP_ERROR reason)
{
mImageProcessor->Abort();
}
mState = State::kIdle;
SetState(State::kIdle);

// Because AbortTransfer() will generate a StatusReport to send.
PollTransferSession();
Expand Down Expand Up @@ -171,7 +171,7 @@ 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
mState = State::kComplete;
SetState(State::kComplete);

// TODO: how/when to reset the BDXDownloader to be ready to handle another download
}
Expand Down Expand Up @@ -217,4 +217,14 @@ CHIP_ERROR BDXDownloader::HandleBdxEvent(const chip::bdx::TransferSession::Outpu
return CHIP_NO_ERROR;
}

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

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

} // namespace chip
12 changes: 10 additions & 2 deletions src/app/clusters/ota-requestor/BDXDownloader.h
Original file line number Diff line number Diff line change
Expand Up @@ -45,12 +45,18 @@ class BDXDownloader : public chip::OTADownloader
virtual ~MessagingDelegate() {}
};

BDXDownloader() : chip::OTADownloader() {}
class StateDelegate
{
public:
virtual void OnDownloadStateChanged(State state) = 0;
virtual ~StateDelegate() = default;
};

// To be called when there is an incoming message to handle (of any protocol type)
void OnMessageReceived(const chip::PayloadHeader & payloadHeader, chip::System::PacketBufferHandle msg);

void SetMessageDelegate(MessagingDelegate * delegate) { mMsgDelegate = delegate; }
void SetStateDelegate(StateDelegate * delegate) { mStateDelegate = delegate; }

// Initialize a BDX transfer session but will not proceed until OnPreparedForDownload() is called.
CHIP_ERROR SetBDXParams(const chip::bdx::TransferSession::TransferInitData & bdxInitData);
Expand All @@ -68,9 +74,11 @@ class BDXDownloader : public chip::OTADownloader
private:
void PollTransferSession();
CHIP_ERROR HandleBdxEvent(const chip::bdx::TransferSession::OutputEvent & outEvent);
void SetState(State state);

chip::bdx::TransferSession mBdxTransfer;
MessagingDelegate * mMsgDelegate;
MessagingDelegate * mMsgDelegate = nullptr;
StateDelegate * mStateDelegate = nullptr;
};

} // namespace chip
Loading

0 comments on commit eab67b3

Please sign in to comment.