From 586905624d14745c4e82e38cca0c1b4de7a15cdc Mon Sep 17 00:00:00 2001 From: Sergei Lissianoi <54454955+selissia@users.noreply.github.com> Date: Wed, 27 Oct 2021 11:54:10 -0400 Subject: [PATCH] Integrate ExampleOTARequestor class with the Linux OTA Requestor app (#11010) * Make a QueryImage command from OTA Requestor to OTA Provider * Make ota-provider-app and ota-requestor-app compilable on Mac * Remove self commissioning code from ota-requestor-app * Add a parameter for IP address for OTA Requestor to use for creating secure session * Add a parameter for fabric index for OTA Requestor to use for creating secure session * Add a parameter for time to wait for OTA Requestor to initiate a QueryImage command from startup * Address code review comments - Avoid multiple calls to Server::GetInstance() - Update parts of README - Add TODO for workarounds to be removed later - Remove usage of __FUNCTION__ * Integrate ExampleOTARequestor class with the Linux OTA Requestor app Add mConnectToProviderCallback function pointer member ExampleOTARequestor that can be set by the Requestor app implementation. If the callback is set then the reception of the Announce OTA Provider message triggers the Requestor to establish a session with the Provider and initiate the BDX download. * Add a log message to HandleAnnounceOTAProvider * Restyled by clang-format * Replace NULL with nullptr Co-authored-by: Carol Yang Co-authored-by: Restyled.io --- examples/ota-requestor-app/linux/main.cpp | 10 +++--- .../ExampleOTARequestor.cpp | 36 ++++++++----------- .../ExampleOTARequestor.h | 8 +++++ 3 files changed, 28 insertions(+), 26 deletions(-) diff --git a/examples/ota-requestor-app/linux/main.cpp b/examples/ota-requestor-app/linux/main.cpp index f0c0e3902b7767..bcec4be7d50356 100644 --- a/examples/ota-requestor-app/linux/main.cpp +++ b/examples/ota-requestor-app/linux/main.cpp @@ -244,7 +244,7 @@ bool HandleOptions(const char * aProgram, OptionSet * aOptions, int aIdentifier, return (retval); } -void SendQueryImageCommand() +void SendQueryImageCommand(chip::NodeId peerNodeId = providerNodeId, chip::FabricIndex peerFabricIndex = providerFabricIndex) { // Explicitly calling UpdateAddress() should not be needed once OperationalDeviceProxy can resolve IP address from node ID and // fabric index @@ -261,9 +261,8 @@ void SendQueryImageCommand() .fabricsTable = &(server->GetFabricTable()), }; - CHIP_ERROR err = CHIP_NO_ERROR; - FabricIndex peerFabricIndex = providerFabricIndex; - gOperationalDeviceProxy.Init(providerNodeId, peerFabricIndex, initParams); + CHIP_ERROR err = CHIP_NO_ERROR; + gOperationalDeviceProxy.Init(peerNodeId, peerFabricIndex, initParams); err = gOperationalDeviceProxy.Connect(&mOnConnectedCallback, &mOnConnectionFailureCallback); if (err != CHIP_NO_ERROR) { @@ -316,6 +315,9 @@ int main(int argc, char * argv[]) // Initialize device attestation config SetDeviceAttestationCredentialsProvider(chip::Credentials::Examples::GetExampleDACProvider()); + // This will allow ExampleOTARequestor to call SendQueryImageCommand + ExampleOTARequestor::GetInstance().SetConnectToProviderCallback(SendQueryImageCommand); + // If a delay is provided, QueryImage after the timer expires if (delayQueryTimeInSec > 0) { diff --git a/examples/ota-requestor-app/ota-requestor-common/ExampleOTARequestor.cpp b/examples/ota-requestor-app/ota-requestor-common/ExampleOTARequestor.cpp index f677afb692482b..6805e2047911e0 100644 --- a/examples/ota-requestor-app/ota-requestor-common/ExampleOTARequestor.cpp +++ b/examples/ota-requestor-app/ota-requestor-common/ExampleOTARequestor.cpp @@ -61,18 +61,18 @@ void ExampleOTARequestor::Init(chip::Controller::ControllerDeviceInitParams conn void ExampleOTARequestor::ConnectToProvider() { - FabricInfo * providerFabric = GetProviderFabricInfo(); - VerifyOrReturn(providerFabric != nullptr, - ChipLogError(SoftwareUpdate, "No Fabric found for index %" PRIu8, mProviderFabricIndex)); - - ChipLogProgress(SoftwareUpdate, - "Once #7976 is fixed, this would attempt to connect to 0x" ChipLogFormatX64 " on FabricIndex 0x%" PRIu8 - " (" ChipLogFormatX64 ")", - ChipLogValueX64(mProviderNodeId), mProviderFabricIndex, ChipLogValueX64(providerFabric->GetFabricId())); - - // TODO: uncomment and fill in after #7976 is fixed - // mProviderDevice.Init(mConnectParams, mProviderNodeId, address, mProviderFabricIndex); - // mProviderDevice.EstablishConnectivity(); + + if (mConnectToProviderCallback != nullptr) + { + ChipLogProgress(SoftwareUpdate, "Attempting to connect to 0x" ChipLogFormatX64 " on FabricIndex 0x%" PRIu8, + ChipLogValueX64(mProviderNodeId), mProviderFabricIndex); + + mConnectToProviderCallback(mProviderNodeId, mProviderFabricIndex); + } + else + { + ChipLogError(SoftwareUpdate, "ConnectToProviderCallback is not set"); + } } EmberAfStatus ExampleOTARequestor::HandleAnnounceOTAProvider( @@ -91,16 +91,8 @@ EmberAfStatus ExampleOTARequestor::HandleAnnounceOTAProvider( mProviderNodeId = providerLocation; mProviderFabricIndex = commandObj->GetExchangeContext()->GetSessionHandle().GetFabricIndex(); - FabricInfo * providerFabric = GetProviderFabricInfo(); - if (providerFabric == nullptr) - { - ChipLogError(SoftwareUpdate, "No Fabric found for index %" PRIu8, mProviderFabricIndex); - return EMBER_ZCL_STATUS_SUCCESS; - } - - ChipLogProgress(SoftwareUpdate, - "Notified of Provider at NodeID: 0x" ChipLogFormatX64 "on FabricIndex 0x%" PRIu8 " (" ChipLogFormatX64 ")", - ChipLogValueX64(mProviderNodeId), mProviderFabricIndex, ChipLogValueX64(providerFabric->GetFabricId())); + ChipLogProgress(SoftwareUpdate, "Notified of Provider at NodeID: 0x" ChipLogFormatX64 " on FabricIndex 0x%" PRIu8, + ChipLogValueX64(mProviderNodeId), mProviderFabricIndex); // If reason is URGENT_UPDATE_AVAILABLE, we start OTA immediately. Otherwise, respect the timer value set in mOtaStartDelayMs. // This is done to exemplify what a real-world OTA Requestor might do while also being configurable enough to use as a test app. diff --git a/examples/ota-requestor-app/ota-requestor-common/ExampleOTARequestor.h b/examples/ota-requestor-app/ota-requestor-common/ExampleOTARequestor.h index 5cec185c847908..4cdcc2eea2297a 100644 --- a/examples/ota-requestor-app/ota-requestor-common/ExampleOTARequestor.h +++ b/examples/ota-requestor-app/ota-requestor-common/ExampleOTARequestor.h @@ -39,6 +39,9 @@ class ExampleOTARequestor chip::app::CommandHandler * commandObj, const chip::app::ConcreteCommandPath & commandPath, const chip::app::Clusters::OtaSoftwareUpdateRequestor::Commands::AnnounceOtaProvider::DecodableType & commandData); + // Setter for mConnectToProviderCallback + void SetConnectToProviderCallback(void (*f)(chip::NodeId, chip::FabricIndex)) { mConnectToProviderCallback = f; } + private: ExampleOTARequestor(); @@ -53,4 +56,9 @@ class ExampleOTARequestor chip::NodeId mProviderNodeId; chip::FabricIndex mProviderFabricIndex; uint32_t mOtaStartDelayMs; + + // TODO: This will be redone once the full Requestor app design is in place + // Pointer to the function that establishes a session with the Provider and initiates + // the BDX download + void (*mConnectToProviderCallback)(chip::NodeId, chip::FabricIndex); };