Skip to content

Commit

Permalink
Address various review comments for Darwin OTA delegate bridge. (#20888)
Browse files Browse the repository at this point in the history
* Address various review comments for Darwin OTA delegate bridge.

Fixes #20763

* Address review comments.
  • Loading branch information
bzbarsky-apple authored and pull[bot] committed Sep 21, 2023
1 parent 65f5dc5 commit 1026796
Show file tree
Hide file tree
Showing 3 changed files with 94 additions and 95 deletions.
2 changes: 1 addition & 1 deletion src/darwin/Framework/CHIP/MTROTAProviderDelegate.h
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ NS_ASSUME_NONNULL_BEGIN
* All delegate methods will be called on the supplied Delegate Queue.
*/
@protocol MTROTAProviderDelegate <NSObject>
@optional
@required
/**
* Notify the delegate when query image command is received
*
Expand Down
10 changes: 5 additions & 5 deletions src/darwin/Framework/CHIP/MTROTAProviderDelegateBridge.h
Original file line number Diff line number Diff line change
Expand Up @@ -42,19 +42,19 @@ class MTROTAProviderDelegateBridge : public chip::app::Clusters::OTAProviderDele
const chip::app::Clusters::OtaSoftwareUpdateProvider::Commands::NotifyUpdateApplied::DecodableType & commandData) override;

private:
void ConvertToQueryImageParams(
static CHIP_ERROR ConvertToQueryImageParams(
const chip::app::Clusters::OtaSoftwareUpdateProvider::Commands::QueryImage::DecodableType & commandData,
MTROtaSoftwareUpdateProviderClusterQueryImageParams * commandParams);
void ConvertFromQueryImageResponseParms(
static void ConvertFromQueryImageResponseParms(
const MTROtaSoftwareUpdateProviderClusterQueryImageResponseParams * responseParams,
chip::app::Clusters::OtaSoftwareUpdateProvider::Commands::QueryImageResponse::Type & response);
void ConvertToApplyUpdateRequestParams(
static void ConvertToApplyUpdateRequestParams(
const chip::app::Clusters::OtaSoftwareUpdateProvider::Commands::ApplyUpdateRequest::DecodableType & commandData,
MTROtaSoftwareUpdateProviderClusterApplyUpdateRequestParams * commandParams);
void ConvertFromApplyUpdateRequestResponseParms(
static void ConvertFromApplyUpdateRequestResponseParms(
const MTROtaSoftwareUpdateProviderClusterApplyUpdateResponseParams * responseParams,
chip::app::Clusters::OtaSoftwareUpdateProvider::Commands::ApplyUpdateResponse::Type & response);
void ConvertToNotifyUpdateAppliedParams(
static void ConvertToNotifyUpdateAppliedParams(
const chip::app::Clusters::OtaSoftwareUpdateProvider::Commands::NotifyUpdateApplied::DecodableType & commandData,
MTROtaSoftwareUpdateProviderClusterNotifyUpdateAppliedParams * commandParams);

Expand Down
177 changes: 88 additions & 89 deletions src/darwin/Framework/CHIP/MTROTAProviderDelegateBridge.mm
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,12 @@
*/

#import "MTROTAProviderDelegateBridge.h"
#import "NSDataSpanConversion.h"

#include <app/clusters/ota-provider/ota-provider.h>
#include <lib/support/TypeTraits.h>
#include <platform/PlatformManager.h>
#include <protocols/interaction_model/Constants.h>

static NSInteger const kOtaProviderEndpoint = 0;

Expand All @@ -41,34 +44,36 @@
const chip::app::ConcreteCommandPath & commandPath,
const chip::app::Clusters::OtaSoftwareUpdateProvider::Commands::QueryImage::DecodableType & commandData)
{
// Make sure to hold on to the command handler and command path to be used in the completion block
__block chip::app::CommandHandler::Handle handle(commandObj);
__block chip::app::ConcreteCommandPath cachedCommandPath(
commandPath.mEndpointId, commandPath.mClusterId, commandPath.mCommandId);

id<MTROTAProviderDelegate> strongDelegate = mDelegate;
if ([strongDelegate respondsToSelector:@selector(handleQueryImage:completionHandler:)]) {
if (strongDelegate && mQueue) {
auto * commandParams = [[MTROtaSoftwareUpdateProviderClusterQueryImageParams alloc] init];
ConvertToQueryImageParams(commandData, commandParams);

dispatch_async(mQueue, ^{
[strongDelegate handleQueryImage:commandParams
completionHandler:^(MTROtaSoftwareUpdateProviderClusterQueryImageResponseParams * _Nullable data,
NSError * _Nullable error) {
dispatch_async(chip::DeviceLayer::PlatformMgrImpl().GetWorkQueue(), ^{
chip::app::Clusters::OtaSoftwareUpdateProvider::Commands::QueryImageResponse::Type response;
ConvertFromQueryImageResponseParms(data, response);

chip::app::CommandHandler * handler = handle.Get();
if (handler) {
handler->AddResponse(cachedCommandPath, response);
handle.Release();
}
});
}];
});
if (strongDelegate && mQueue) {
auto * commandParams = [[MTROtaSoftwareUpdateProviderClusterQueryImageParams alloc] init];
CHIP_ERROR err = ConvertToQueryImageParams(commandData, commandParams);
if (err != CHIP_NO_ERROR) {
commandObj->AddStatus(commandPath, chip::Protocols::InteractionModel::Status::InvalidCommand);
return;
}

// Make sure to hold on to the command handler and command path to be used in the completion block
__block chip::app::CommandHandler::Handle handle(commandObj);
__block chip::app::ConcreteCommandPath cachedCommandPath(
commandPath.mEndpointId, commandPath.mClusterId, commandPath.mCommandId);

dispatch_async(mQueue, ^{
[strongDelegate handleQueryImage:commandParams
completionHandler:^(MTROtaSoftwareUpdateProviderClusterQueryImageResponseParams * _Nullable data,
NSError * _Nullable error) {
dispatch_async(chip::DeviceLayer::PlatformMgrImpl().GetWorkQueue(), ^{
chip::app::Clusters::OtaSoftwareUpdateProvider::Commands::QueryImageResponse::Type response;
ConvertFromQueryImageResponseParms(data, response);

chip::app::CommandHandler * handler = handle.Get();
if (handler) {
handler->AddResponse(cachedCommandPath, response);
handle.Release();
}
});
}];
});
}
}

Expand All @@ -82,29 +87,27 @@
commandPath.mEndpointId, commandPath.mClusterId, commandPath.mCommandId);

id<MTROTAProviderDelegate> strongDelegate = mDelegate;
if ([strongDelegate respondsToSelector:@selector(handleApplyUpdateRequest:completionHandler:)]) {
if (strongDelegate && mQueue) {
auto * commandParams = [[MTROtaSoftwareUpdateProviderClusterApplyUpdateRequestParams alloc] init];
ConvertToApplyUpdateRequestParams(commandData, commandParams);

dispatch_async(mQueue, ^{
[strongDelegate
handleApplyUpdateRequest:commandParams
completionHandler:^(MTROtaSoftwareUpdateProviderClusterApplyUpdateResponseParams * _Nullable data,
NSError * _Nullable error) {
dispatch_async(chip::DeviceLayer::PlatformMgrImpl().GetWorkQueue(), ^{
chip::app::Clusters::OtaSoftwareUpdateProvider::Commands::ApplyUpdateResponse::Type response;
ConvertFromApplyUpdateRequestResponseParms(data, response);

chip::app::CommandHandler * handler = handle.Get();
if (handler) {
handler->AddResponse(cachedCommandPath, response);
handle.Release();
}
});
}];
});
}
if (strongDelegate && mQueue) {
auto * commandParams = [[MTROtaSoftwareUpdateProviderClusterApplyUpdateRequestParams alloc] init];
ConvertToApplyUpdateRequestParams(commandData, commandParams);

dispatch_async(mQueue, ^{
[strongDelegate
handleApplyUpdateRequest:commandParams
completionHandler:^(MTROtaSoftwareUpdateProviderClusterApplyUpdateResponseParams * _Nullable data,
NSError * _Nullable error) {
dispatch_async(chip::DeviceLayer::PlatformMgrImpl().GetWorkQueue(), ^{
chip::app::Clusters::OtaSoftwareUpdateProvider::Commands::ApplyUpdateResponse::Type response;
ConvertFromApplyUpdateRequestResponseParms(data, response);

chip::app::CommandHandler * handler = handle.Get();
if (handler) {
handler->AddResponse(cachedCommandPath, response);
handle.Release();
}
});
}];
});
}
}

Expand All @@ -118,45 +121,44 @@
commandPath.mEndpointId, commandPath.mClusterId, commandPath.mCommandId);

id<MTROTAProviderDelegate> strongDelegate = mDelegate;
if ([strongDelegate respondsToSelector:@selector(handleNotifyUpdateApplied:completionHandler:)]) {
if (strongDelegate && mQueue) {
auto * commandParams = [[MTROtaSoftwareUpdateProviderClusterNotifyUpdateAppliedParams alloc] init];
ConvertToNotifyUpdateAppliedParams(commandData, commandParams);

dispatch_async(mQueue, ^{
[strongDelegate
handleNotifyUpdateApplied:commandParams
completionHandler:^(NSError * _Nullable error) {
dispatch_async(chip::DeviceLayer::PlatformMgrImpl().GetWorkQueue(), ^{
chip::app::CommandHandler * handler = handle.Get();
if (handler) {
handler->AddStatus(cachedCommandPath, chip::Protocols::InteractionModel::Status::Success);
handle.Release();
}
});
}];
});
}
if (strongDelegate && mQueue) {
auto * commandParams = [[MTROtaSoftwareUpdateProviderClusterNotifyUpdateAppliedParams alloc] init];
ConvertToNotifyUpdateAppliedParams(commandData, commandParams);

dispatch_async(mQueue, ^{
[strongDelegate
handleNotifyUpdateApplied:commandParams
completionHandler:^(NSError * _Nullable error) {
dispatch_async(chip::DeviceLayer::PlatformMgrImpl().GetWorkQueue(), ^{
chip::app::CommandHandler * handler = handle.Get();
if (handler) {
handler->AddStatus(cachedCommandPath, chip::Protocols::InteractionModel::Status::Success);
handle.Release();
}
});
}];
});
}
}

void MTROTAProviderDelegateBridge::ConvertToQueryImageParams(
CHIP_ERROR MTROTAProviderDelegateBridge::ConvertToQueryImageParams(
const chip::app::Clusters::OtaSoftwareUpdateProvider::Commands::QueryImage::DecodableType & commandData,
MTROtaSoftwareUpdateProviderClusterQueryImageParams * commandParams)
{
commandParams.vendorId = [NSNumber numberWithInt:commandData.vendorId];
commandParams.productId = [NSNumber numberWithInt:commandData.productId];
commandParams.softwareVersion = [NSNumber numberWithInt:commandData.softwareVersion];
commandParams.vendorId = [NSNumber numberWithUnsignedShort:commandData.vendorId];
commandParams.productId = [NSNumber numberWithUnsignedShort:commandData.productId];
commandParams.softwareVersion = [NSNumber numberWithUnsignedLong:commandData.softwareVersion];
auto iterator = commandData.protocolsSupported.begin();
NSMutableArray * protocolsSupported = [[NSMutableArray alloc] init];
while (iterator.Next()) {
chip::app::Clusters::OtaSoftwareUpdateProvider::OTADownloadProtocol protocol = iterator.GetValue();
[protocolsSupported addObject:[NSNumber numberWithInt:static_cast<int>(protocol)]];
[protocolsSupported addObject:[NSNumber numberWithInt:chip::to_underlying(protocol)]];
}
commandParams.protocolsSupported = [protocolsSupported copy];
ReturnErrorOnFailure(iterator.GetStatus());
commandParams.protocolsSupported = protocolsSupported;

if (commandData.hardwareVersion.HasValue()) {
commandParams.hardwareVersion = [NSNumber numberWithInt:commandData.hardwareVersion.Value()];
commandParams.hardwareVersion = [NSNumber numberWithUnsignedShort:commandData.hardwareVersion.Value()];
}

if (commandData.location.HasValue()) {
Expand All @@ -170,9 +172,9 @@
}

if (commandData.metadataForProvider.HasValue()) {
commandParams.metadataForProvider = [NSData dataWithBytes:commandData.metadataForProvider.Value().data()
length:commandData.metadataForProvider.Value().size()];
commandParams.metadataForProvider = AsData(commandData.metadataForProvider.Value());
}
return CHIP_NO_ERROR;
}

void MTROTAProviderDelegateBridge::ConvertFromQueryImageResponseParms(
Expand All @@ -182,15 +184,15 @@
response.status = static_cast<chip::app::Clusters::OtaSoftwareUpdateProvider::OTAQueryStatus>([responseParams.status intValue]);

if (responseParams.delayedActionTime) {
response.delayedActionTime.SetValue([responseParams.delayedActionTime intValue]);
response.delayedActionTime.SetValue([responseParams.delayedActionTime unsignedIntValue]);
}

if (responseParams.imageURI) {
response.imageURI.SetValue(chip::CharSpan([responseParams.imageURI UTF8String], responseParams.imageURI.length));
}

if (responseParams.softwareVersion) {
response.softwareVersion.SetValue([responseParams.softwareVersion intValue]);
response.softwareVersion.SetValue([responseParams.softwareVersion unsignedIntValue]);
}

if (responseParams.softwareVersionString) {
Expand All @@ -199,27 +201,24 @@
}

if (responseParams.updateToken) {
UInt8 * updateTokenBytes = (UInt8 *) responseParams.updateToken.bytes;
response.updateToken.SetValue(chip::ByteSpan(updateTokenBytes, responseParams.updateToken.length));
response.updateToken.SetValue(AsByteSpan(responseParams.updateToken));
}

if (responseParams.userConsentNeeded) {
response.userConsentNeeded.SetValue([responseParams.userConsentNeeded boolValue]);
}

if (responseParams.metadataForRequestor) {
UInt8 * metadataForRequestorBytes = (UInt8 *) responseParams.metadataForRequestor.bytes;
response.metadataForRequestor.SetValue(
chip::ByteSpan(metadataForRequestorBytes, responseParams.metadataForRequestor.length));
response.metadataForRequestor.SetValue(AsByteSpan(responseParams.metadataForRequestor));
}
}

void MTROTAProviderDelegateBridge::ConvertToApplyUpdateRequestParams(
const chip::app::Clusters::OtaSoftwareUpdateProvider::Commands::ApplyUpdateRequest::DecodableType & commandData,
MTROtaSoftwareUpdateProviderClusterApplyUpdateRequestParams * commandParams)
{
commandParams.updateToken = [NSData dataWithBytes:commandData.updateToken.data() length:commandData.updateToken.size()];
commandParams.newVersion = [NSNumber numberWithInt:commandData.newVersion];
commandParams.updateToken = AsData(commandData.updateToken);
commandParams.newVersion = [NSNumber numberWithUnsignedLong:commandData.newVersion];
}

void MTROTAProviderDelegateBridge::ConvertFromApplyUpdateRequestResponseParms(
Expand All @@ -228,13 +227,13 @@
{
response.action
= static_cast<chip::app::Clusters::OtaSoftwareUpdateProvider::OTAApplyUpdateAction>([responseParams.action intValue]);
response.delayedActionTime = [responseParams.delayedActionTime intValue];
response.delayedActionTime = [responseParams.delayedActionTime unsignedIntValue];
}

void MTROTAProviderDelegateBridge::ConvertToNotifyUpdateAppliedParams(
const chip::app::Clusters::OtaSoftwareUpdateProvider::Commands::NotifyUpdateApplied::DecodableType & commandData,
MTROtaSoftwareUpdateProviderClusterNotifyUpdateAppliedParams * commandParams)
{
commandParams.updateToken = [NSData dataWithBytes:commandData.updateToken.data() length:commandData.updateToken.size()];
commandParams.softwareVersion = [NSNumber numberWithInt:commandData.softwareVersion];
commandParams.updateToken = AsData(commandData.updateToken);
commandParams.softwareVersion = [NSNumber numberWithUnsignedLong:commandData.softwareVersion];
}

0 comments on commit 1026796

Please sign in to comment.