Skip to content

Commit

Permalink
[OTA] Make OTARequestor responsible for responding to AnnounceOTAProv…
Browse files Browse the repository at this point in the history
…ider command (#16952)
  • Loading branch information
carol-apple authored and pull[bot] committed Nov 14, 2023
1 parent c33c519 commit 1852777
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 20 deletions.
16 changes: 6 additions & 10 deletions src/app/clusters/ota-requestor/OTARequestor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ using namespace app::Clusters::OtaSoftwareUpdateRequestor::Commands;
using namespace app::Clusters::OtaSoftwareUpdateRequestor::Structs;
using app::DataModel::Nullable;
using bdx::TransferSession;
using Protocols::InteractionModel::Status;

// Global instance of the OTARequestorInterface.
OTARequestorInterface * globalOTARequestorInstance = nullptr;
Expand Down Expand Up @@ -286,20 +287,15 @@ void OTARequestor::Reset()
StoreCurrentUpdateInfo();
}

EmberAfStatus OTARequestor::HandleAnnounceOTAProvider(app::CommandHandler * commandObj,
const app::ConcreteCommandPath & commandPath,
const AnnounceOtaProvider::DecodableType & commandData)
void OTARequestor::HandleAnnounceOTAProvider(app::CommandHandler * commandObj, const app::ConcreteCommandPath & commandPath,
const AnnounceOtaProvider::DecodableType & commandData)
{
VerifyOrReturn(commandObj != nullptr, ChipLogError(SoftwareUpdate, "Invalid commandObj, cannot handle AnnounceOTAProvider"));

auto & announcementReason = commandData.announcementReason;

ChipLogProgress(SoftwareUpdate, "OTA Requestor received AnnounceOTAProvider");

if (commandObj == nullptr || commandObj->GetExchangeContext() == nullptr)
{
ChipLogError(SoftwareUpdate, "Cannot access ExchangeContext for FabricIndex");
return EMBER_ZCL_STATUS_FAILURE;
}

ProviderLocationType providerLocation = { .providerNodeID = commandData.providerNodeId,
.endpoint = commandData.endpoint,
.fabricIndex = commandObj->GetAccessingFabricIndex() };
Expand All @@ -316,7 +312,7 @@ EmberAfStatus OTARequestor::HandleAnnounceOTAProvider(app::CommandHandler * comm

mOtaRequestorDriver->ProcessAnnounceOTAProviders(providerLocation, announcementReason);

return EMBER_ZCL_STATUS_SUCCESS;
commandObj->AddStatus(commandPath, Status::Success);
}

void OTARequestor::ConnectToProvider(OnConnectedAction onConnectedAction)
Expand Down
2 changes: 1 addition & 1 deletion src/app/clusters/ota-requestor/OTARequestor.h
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ class OTARequestor : public OTARequestorInterface, public BDXDownloader::StateDe
//////////// OTARequestorInterface Implementation ///////////////
void Reset(void) override;

EmberAfStatus HandleAnnounceOTAProvider(
void HandleAnnounceOTAProvider(
app::CommandHandler * commandObj, const app::ConcreteCommandPath & commandPath,
const app::Clusters::OtaSoftwareUpdateRequestor::Commands::AnnounceOtaProvider::DecodableType & commandData) override;

Expand Down
7 changes: 5 additions & 2 deletions src/app/clusters/ota-requestor/OTARequestorInterface.h
Original file line number Diff line number Diff line change
Expand Up @@ -156,8 +156,11 @@ class OTARequestorInterface
// Reset any relevant states
virtual void Reset(void) = 0;

// Handler for the AnnounceOTAProvider command
virtual EmberAfStatus HandleAnnounceOTAProvider(
/**
* Called to handle an AnnounceOTAProvider command and is responsible for sending the status. The caller is responsible for
* validating fields in the command.
*/
virtual void HandleAnnounceOTAProvider(
chip::app::CommandHandler * commandObj, const chip::app::ConcreteCommandPath & commandPath,
const chip::app::Clusters::OtaSoftwareUpdateRequestor::Commands::AnnounceOtaProvider::DecodableType & commandData) = 0;

Expand Down
24 changes: 17 additions & 7 deletions src/app/clusters/ota-requestor/ota-requestor-server.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
*/

#include <app/AttributeAccessInterface.h>
#include <app/CommandHandler.h>
#include <app/EventLogging.h>
#include <app/clusters/ota-requestor/OTARequestorInterface.h>
#include <app/clusters/ota-requestor/ota-requestor-server.h>
Expand All @@ -32,9 +33,12 @@ using namespace chip::app::Clusters;
using namespace chip::app::Clusters::OtaSoftwareUpdateRequestor;
using namespace chip::app::Clusters::OtaSoftwareUpdateRequestor::Attributes;
using namespace chip::app::Clusters::OtaSoftwareUpdateRequestor::Structs;
using Protocols::InteractionModel::Status;

namespace {

constexpr size_t kMaxMetadataLen = 512; // The maximum length of Metadata in any OTA Requestor command

class OtaSoftwareUpdateRequestorAttrAccess : public AttributeAccessInterface
{
public:
Expand Down Expand Up @@ -258,19 +262,25 @@ bool emberAfOtaSoftwareUpdateRequestorClusterAnnounceOtaProviderCallback(
chip::app::CommandHandler * commandObj, const chip::app::ConcreteCommandPath & commandPath,
const chip::app::Clusters::OtaSoftwareUpdateRequestor::Commands::AnnounceOtaProvider::DecodableType & commandData)
{
EmberAfStatus status;
chip::OTARequestorInterface * requestor = chip::GetRequestorInstance();
auto & metadataForNode = commandData.metadataForNode;

if (requestor != nullptr)
chip::OTARequestorInterface * requestor = chip::GetRequestorInstance();
if (requestor == nullptr)
{
status = requestor->HandleAnnounceOTAProvider(commandObj, commandPath, commandData);
commandObj->AddStatus(commandPath, Status::UnsupportedCommand);
return true;
}
else

if (metadataForNode.HasValue() && metadataForNode.Value().size() > kMaxMetadataLen)
{
status = EMBER_ZCL_STATUS_FAILURE;
ChipLogError(Zcl, "Metadata size %u exceeds max %u", static_cast<unsigned>(metadataForNode.Value().size()),
static_cast<unsigned>(kMaxMetadataLen));
commandObj->AddStatus(commandPath, Status::InvalidCommand);
return true;
}

emberAfSendImmediateDefaultResponse(status);
requestor->HandleAnnounceOTAProvider(commandObj, commandPath, commandData);

return true;
}

Expand Down

0 comments on commit 1852777

Please sign in to comment.