From a3127d8d768df01bef169e4f81d1aa403c33dc63 Mon Sep 17 00:00:00 2001 From: Carol Yang Date: Thu, 2 Dec 2021 15:43:56 -0800 Subject: [PATCH] [OTA] Use CASESessionManager to establish CASE sessions from OTARequestor class --- .../OTAProviderExample.cpp | 26 +- examples/ota-requestor-app/linux/main.cpp | 61 ++-- src/app/CASESessionManager.h | 3 + .../clusters/ota-requestor/BDXDownloader.cpp | 61 ++++ .../clusters/ota-requestor/BDXDownloader.h | 12 + .../ota-requestor/ClusterInterface.cpp | 2 +- .../clusters/ota-requestor/OTARequestor.cpp | 321 ++++++++---------- src/app/clusters/ota-requestor/OTARequestor.h | 115 ++++--- .../ota-requestor/OTARequestorInterface.h | 4 + src/app/server/Server.h | 22 +- src/lib/core/CHIPError.h | 16 + .../zap-generated/IMClusterCommandHandler.cpp | 4 +- 12 files changed, 382 insertions(+), 265 deletions(-) diff --git a/examples/ota-provider-app/ota-provider-common/OTAProviderExample.cpp b/examples/ota-provider-app/ota-provider-common/OTAProviderExample.cpp index 3cc95d329c7b58..fe53145f19af96 100644 --- a/examples/ota-provider-app/ota-provider-common/OTAProviderExample.cpp +++ b/examples/ota-provider-app/ota-provider-common/OTAProviderExample.cpp @@ -20,17 +20,23 @@ #include #include +#include #include +#include #include #include #include -#include // For chip::kTestDeviceNodeId #include using chip::ByteSpan; using chip::CharSpan; +using chip::FabricIndex; +using chip::FabricInfo; +using chip::MutableCharSpan; +using chip::NodeId; using chip::Optional; +using chip::Server; using chip::Span; using chip::app::Clusters::OTAProviderDelegate; using namespace chip::app::Clusters::OtaSoftwareUpdateProvider::Commands; @@ -57,19 +63,18 @@ void GenerateUpdateToken(uint8_t * buf, size_t bufSize) } } -bool GenerateBdxUri(const Span & fileDesignator, Span outUri, size_t availableSize) +bool GenerateBdxUri(NodeId nodeId, CharSpan fileDesignator, MutableCharSpan outUri) { static constexpr char bdxPrefix[] = "bdx://"; - chip::NodeId nodeId = chip::kTestDeviceNodeId; // TODO: read this dynamically size_t nodeIdHexStrLen = sizeof(nodeId) * 2; size_t expectedLength = strlen(bdxPrefix) + nodeIdHexStrLen + 1 + fileDesignator.size(); - if (expectedLength >= availableSize) + if (expectedLength >= outUri.size()) { return false; } - size_t written = static_cast(snprintf(outUri.data(), availableSize, "%s" ChipLogFormatX64 "/%s", bdxPrefix, + size_t written = static_cast(snprintf(outUri.data(), outUri.size(), "%s" ChipLogFormatX64 "/%s", bdxPrefix, ChipLogValueX64(nodeId), fileDesignator.data())); return expectedLength == written; @@ -115,9 +120,16 @@ EmberAfStatus OTAProviderExample::HandleQueryImage(chip::app::CommandHandler * c if (strlen(mOTAFilePath)) { + // TODO: This uses the current node as the provider to supply the OTA image. This can be configurable such that the provider + // supplying the response is not the provider supplying the OTA image. + FabricIndex fabricIndex = commandObj->GetExchangeContext()->GetSessionHandle().GetFabricIndex(); + FabricInfo * fabricInfo = Server::GetInstance().GetFabricTable().FindFabricWithIndex(fabricIndex); + NodeId nodeId = fabricInfo->GetPeerId().GetNodeId(); + // Only doing BDX transport for now - GenerateBdxUri(Span(mOTAFilePath, strlen(mOTAFilePath)), Span(uriBuf, 0), kUriMaxLen); - ChipLogDetail(SoftwareUpdate, "generated URI: %s", uriBuf); + MutableCharSpan uri(uriBuf, kUriMaxLen); + GenerateBdxUri(nodeId, CharSpan(mOTAFilePath, strlen(mOTAFilePath)), uri); + ChipLogDetail(SoftwareUpdate, "Generated URI: %.*s", static_cast(uri.size()), uri.data()); } // Set Status for the Query Image Response diff --git a/examples/ota-requestor-app/linux/main.cpp b/examples/ota-requestor-app/linux/main.cpp index fc004f357376ed..4f38efd37420a2 100644 --- a/examples/ota-requestor-app/linux/main.cpp +++ b/examples/ota-requestor-app/linux/main.cpp @@ -16,7 +16,8 @@ * limitations under the License. */ -#include +#include +#include #include #include #include @@ -30,30 +31,38 @@ using chip::BDXDownloader; using chip::ByteSpan; +using chip::CASEClientPool; using chip::CharSpan; -using chip::DeviceProxy; using chip::EndpointId; using chip::FabricIndex; +using chip::GetRequestorInstance; using chip::LinuxOTAImageProcessor; using chip::NodeId; using chip::OnDeviceConnected; using chip::OnDeviceConnectionFailure; +using chip::OperationalDeviceProxyPool; +using chip::OTADownloader; using chip::OTAImageProcessorParams; +using chip::OTARequestor; using chip::PeerId; using chip::Server; using chip::VendorId; using chip::Callback::Callback; -using chip::Inet::IPAddress; using chip::System::Layer; using chip::Transport::PeerAddress; using namespace chip::ArgParser; using namespace chip::Messaging; using namespace chip::app::Clusters::OtaSoftwareUpdateProvider::Commands; -OTARequestor requestorCore; -LinuxOTARequestorDriver requestorUser; -BDXDownloader downloader; -LinuxOTAImageProcessor imageProcessor; +constexpr size_t kMaxActiveCaseClients = 2; +constexpr size_t kMaxActiveDevices = 8; + +OTARequestor gRequestorCore; +LinuxOTARequestorDriver gRequestorUser; +BDXDownloader gDownloader; +LinuxOTAImageProcessor gImageProcessor; +CASEClientPool gCASEClientPool; +OperationalDeviceProxyPool gDevicePool; bool HandleOptions(const char * aProgram, OptionSet * aOptions, int aIdentifier, const char * aName, const char * aValue); void OnStartDelayTimerHandler(Layer * systemLayer, void * appState); @@ -62,10 +71,8 @@ constexpr uint16_t kOptionProviderNodeId = 'n'; constexpr uint16_t kOptionProviderFabricIndex = 'f'; constexpr uint16_t kOptionUdpPort = 'u'; constexpr uint16_t kOptionDiscriminator = 'd'; -constexpr uint16_t kOptionIPAddress = 'i'; constexpr uint16_t kOptionDelayQuery = 'q'; -const char * ipAddress = NULL; NodeId providerNodeId = 0x0; FabricIndex providerFabricIndex = 1; uint16_t requestorSecurePort = 0; @@ -77,8 +84,6 @@ OptionDef cmdLineOptionsDef[] = { { "providerFabricIndex", chip::ArgParser::kArgumentRequired, kOptionProviderFabricIndex }, { "udpPort", chip::ArgParser::kArgumentRequired, kOptionUdpPort }, { "discriminator", chip::ArgParser::kArgumentRequired, kOptionDiscriminator }, - // TODO: This can be removed once OperationalDeviceProxy can resolve the IP Address from Node ID - { "ipaddress", chip::ArgParser::kArgumentRequired, kOptionIPAddress }, { "delayQuery", chip::ArgParser::kArgumentRequired, kOptionDelayQuery }, {}, }; @@ -95,8 +100,6 @@ OptionSet cmdLineOptions = { HandleOptions, cmdLineOptionsDef, "PROGRAM OPTIONS" " -d/--discriminator \n" " A 12-bit value used to discern between multiple commissionable CHIP device\n" " advertisements. If none is specified, default value is 3840.\n" - " -i/--ipaddress \n" - " The IP Address of the OTA Provider to connect to. This value must be supplied.\n" " -q/--delayQuery