Skip to content

Commit

Permalink
[Darwin] Make MTRControllerFactory the OTA Provider Delegate (#21224)
Browse files Browse the repository at this point in the history
  • Loading branch information
vivien-apple authored Jul 27, 2022
1 parent 40092db commit 5cd92fb
Show file tree
Hide file tree
Showing 8 changed files with 275 additions and 216 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -38,9 +38,6 @@
ChipLogProgress(chipTool, "Running Command");
ReturnErrorOnFailure(MaybeSetUpStack());
SetIdentity(mCommissionerName.HasValue() ? mCommissionerName.Value() : kIdentityAlpha);
ChipLogDetail(chipTool, "Setting OTA Provider Delegate:");
mOTADelegate.nodeID = [CurrentCommissioner() controllerNodeId];
[CurrentCommissioner() setOTAProviderDelegate:mOTADelegate queue:mOTAProviderCallbackQueue];
ReturnLogErrorOnFailure(RunCommand());
ReturnLogErrorOnFailure(StartWaiting(GetWaitDuration()));

Expand All @@ -67,7 +64,6 @@
CHIPToolKeypair * nocSigner = [[CHIPToolKeypair alloc] init];
storage = [[CHIPToolPersistentStorageDelegate alloc] init];

mOTAProviderCallbackQueue = dispatch_queue_create("com.darwin-framework-tool.command", DISPATCH_QUEUE_SERIAL);
mOTADelegate = [[OTAProviderDelegate alloc] init];

auto factory = [MTRControllerFactory sharedInstance];
Expand All @@ -79,6 +75,7 @@
auto params = [[MTRControllerFactoryParams alloc] initWithStorage:storage];
params.port = @(kListenPort);
params.startServer = YES;
params.otaProviderDelegate = mOTADelegate;

if ([factory startup:params] == NO) {
ChipLogError(chipTool, "Controller factory startup failed");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,6 @@ @implementation OTAProviderDelegate
- (instancetype)init
{
if (self = [super init]) {
_nodeID = @(0);
_selectedCandidate = [[DeviceSoftwareVersionModel alloc] init];
_userConsentState = OTAProviderUserUnknown;
}
Expand Down Expand Up @@ -122,7 +121,7 @@ - (void)handleBDXTransferSessionBegin:(NSString * _Nonnull)fileDesignator
offset:(NSNumber * _Nonnull)offset
completionHandler:(void (^)(NSError * error))completionHandler
{
NSLog(@"BDX TransferSession begin with %@ (offset: %@ )", fileDesignator, offset);
NSLog(@"BDX TransferSession begin with %@ (offset: %@)", fileDesignator, offset);

auto * handle = [NSFileHandle fileHandleForReadingAtPath:fileDesignator];
if (handle == nil) {
Expand Down Expand Up @@ -198,23 +197,30 @@ - (bool)SelectOTACandidate:(NSNumber *)requestorVendorID
rPID:(NSNumber *)requestorProductID
rSV:(NSNumber *)requestorSoftwareVersion
{
auto vendorId = [requestorVendorID unsignedIntValue];
auto productId = [requestorProductID unsignedIntValue];
auto softwareVersion = [requestorSoftwareVersion unsignedLongValue];

bool candidateFound = false;
NSArray * sortedArray = [_candidates sortedArrayUsingSelector:@selector(CompareSoftwareVersions:)];
for (DeviceSoftwareVersionModel * candidate : sortedArray) {
if (candidate.deviceModelData.softwareVersionValid
&& ([requestorSoftwareVersion unsignedLongValue] < [candidate.softwareVersion unsignedLongValue])
&& ([requestorSoftwareVersion unsignedLongValue] >=
[candidate.deviceModelData.minApplicableSoftwareVersion unsignedLongValue])
&& ([requestorSoftwareVersion unsignedLongValue] <=
[candidate.deviceModelData.maxApplicableSoftwareVersion unsignedLongValue])
&& ([requestorVendorID unsignedIntValue] == [candidate.deviceModelData.vendorId unsignedIntValue])
&& ([requestorProductID unsignedIntValue] == [candidate.deviceModelData.productId unsignedIntValue])) {
candidateFound = true;
auto candidateSoftwareVersionValid = candidate.deviceModelData.softwareVersionValid;
auto candidateSoftwareVersion = [candidate.softwareVersion unsignedLongValue];
auto candidateMinApplicableSoftwareVersion = [candidate.deviceModelData.minApplicableSoftwareVersion unsignedLongValue];
auto candidateMaxApplicableSoftwareVersion = [candidate.deviceModelData.maxApplicableSoftwareVersion unsignedLongValue];
auto candidateVendorId = [candidate.deviceModelData.vendorId unsignedIntValue];
auto candidateProductId = [candidate.deviceModelData.productId unsignedIntValue];

if (candidateSoftwareVersionValid && (softwareVersion < candidateSoftwareVersion)
&& (softwareVersion >= candidateMinApplicableSoftwareVersion)
&& (softwareVersion <= candidateMaxApplicableSoftwareVersion) && (vendorId == candidateVendorId)
&& (productId == candidateProductId)) {
_selectedCandidate = candidate;
_selectedCandidate.imageURI = [NSString
stringWithFormat:@"bdx://%016llX/%@", [_nodeID unsignedLongLongValue], _selectedCandidate.deviceModelData.otaURL];
_selectedCandidate.imageURI = candidate.deviceModelData.otaURL;
candidateFound = true;
}
}

return candidateFound;
}

Expand Down
8 changes: 8 additions & 0 deletions src/darwin/Framework/CHIP/MTRControllerFactory.h
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
NS_ASSUME_NONNULL_BEGIN

@protocol MTRPersistentStorageDelegate;
@protocol MTROTAProviderDelegate;
@protocol MTRKeypair;

@class MTRDeviceController;
Expand All @@ -37,6 +38,13 @@ NS_ASSUME_NONNULL_BEGIN
* controllers ends up interacting with.
*/
@property (strong, nonatomic, readonly) id<MTRPersistentStorageDelegate> storageDelegate;

/*
* OTA Provider delegate to be called when an OTA Requestor is requesting a software update.
* Defaults to nil.
*/
@property (strong, nonatomic, nullable) id<MTROTAProviderDelegate> otaProviderDelegate;

/*
* The Product Attestation Authority certificates that are trusted to sign
* device attestation information. Defaults to nil.
Expand Down
26 changes: 26 additions & 0 deletions src/darwin/Framework/CHIP/MTRControllerFactory.mm
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
#import "MTRDeviceController_Internal.h"
#import "MTRLogging.h"
#import "MTRMemory.h"
#import "MTROTAProviderDelegateBridge.h"
#import "MTRP256KeypairBridge.h"
#import "MTRPersistentStorageDelegateBridge.h"
#import "NSDataSpanConversion.h"
Expand Down Expand Up @@ -53,13 +54,15 @@
static NSString * const kErrorControllerFactoryInit = @"Init failure while initializing controller factory";
static NSString * const kErrorKeystoreInit = @"Init failure while initializing persistent storage keystore";
static NSString * const kErrorCertStoreInit = @"Init failure while initializing persistent storage operational certificate store";
static NSString * const kErrorOtaProviderInit = @"Init failure while creating an OTA provider delegate";

@interface MTRControllerFactory ()

@property (atomic, readonly) dispatch_queue_t chipWorkQueue;
@property (readonly) DeviceControllerFactory * controllerFactory;
@property (readonly) MTRPersistentStorageDelegateBridge * persistentStorageDelegateBridge;
@property (readonly) MTRAttestationTrustStoreBridge * attestationTrustStoreBridge;
@property (readonly) MTROTAProviderDelegateBridge * otaProviderDelegateBridge;
// We use TestPersistentStorageDelegate just to get an in-memory store to back
// our group data provider impl. We initialize this store correctly on every
// controller startup, so don't need to actually persist it.
Expand Down Expand Up @@ -171,6 +174,11 @@ - (void)cleanupStartupObjects
_attestationTrustStoreBridge = nullptr;
}

if (_otaProviderDelegateBridge) {
delete _otaProviderDelegateBridge;
_otaProviderDelegateBridge = nullptr;
}

if (_keystore) {
_keystore->Finish();
delete _keystore;
Expand Down Expand Up @@ -211,6 +219,14 @@ - (BOOL)startup:(MTRControllerFactoryParams *)startupParams
return;
}

if (startupParams.otaProviderDelegate) {
_otaProviderDelegateBridge = new MTROTAProviderDelegateBridge(startupParams.otaProviderDelegate);
if (_otaProviderDelegateBridge == nil) {
MTR_LOG_ERROR("Error: %@", kErrorOtaProviderInit);
return;
}
}

// TODO: Allow passing a different keystore implementation via startupParams.
_keystore = new PersistentStorageOperationalKeystore();
if (_keystore == nullptr) {
Expand Down Expand Up @@ -446,6 +462,11 @@ - (MTRDeviceController * _Nullable)createController
// Bringing up the first controller. Start the event loop now. If we
// fail to bring it up, its cleanup will stop the event loop again.
chip::DeviceLayer::PlatformMgrImpl().StartEventLoopTask();

if (_otaProviderDelegateBridge) {
auto systemState = _controllerFactory->GetSystemState();
_otaProviderDelegateBridge->Init(systemState->SystemLayer(), systemState->ExchangeMgr());
}
}

// Add the controller to _controllers now, so if we fail partway through its
Expand Down Expand Up @@ -521,6 +542,10 @@ - (void)controllerShuttingDown:(MTRDeviceController *)controller
[_controllers removeObject:controller];

if ([_controllers count] == 0) {
if (_otaProviderDelegateBridge) {
_otaProviderDelegateBridge->Shutdown();
}

// That was our last controller. Stop the event loop before it
// shuts down, because shutdown of the last controller will tear
// down most of the world.
Expand Down Expand Up @@ -556,6 +581,7 @@ - (instancetype)initWithStorage:(id<MTRPersistentStorageDelegate>)storageDelegat
}

_storageDelegate = storageDelegate;
_otaProviderDelegate = nil;
_paaCerts = nil;
_port = nil;
_startServer = NO;
Expand Down
10 changes: 0 additions & 10 deletions src/darwin/Framework/CHIP/MTRDeviceController.h
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@ typedef void (^MTRDeviceConnectionCallback)(MTRBaseDevice * _Nullable device, NS

@class MTRCommissioningParameters;
@protocol MTRDevicePairingDelegate;
@protocol MTROTAProviderDelegate;

@interface MTRDeviceController : NSObject

Expand Down Expand Up @@ -120,15 +119,6 @@ typedef void (^MTRDeviceConnectionCallback)(MTRBaseDevice * _Nullable device, NS
*/
- (void)setPairingDelegate:(id<MTRDevicePairingDelegate>)delegate queue:(dispatch_queue_t)queue;

/**
* Set the Delegate for the OTA Provider as well as the Queue on which the Delegate callbacks will be triggered
*
* @param[in] delegate The delegate the OTA Provider should use
*
* @param[in] queue The queue on which the callbacks will be delivered
*/
- (void)setOTAProviderDelegate:(id<MTROTAProviderDelegate>)delegate queue:(dispatch_queue_t)queue;

/**
* Shutdown the controller. Calls to shutdown after the first one are NO-OPs.
*/
Expand Down
20 changes: 0 additions & 20 deletions src/darwin/Framework/CHIP/MTRDeviceController.mm
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@
#import "MTRError_Internal.h"
#import "MTRKeypair.h"
#import "MTRLogging.h"
#import "MTROTAProviderDelegateBridge.h"
#import "MTROperationalCredentialsDelegate.h"
#import "MTRP256KeypairBridge.h"
#import "MTRPersistentStorageDelegateBridge.h"
Expand Down Expand Up @@ -57,7 +56,6 @@
static NSString * const kErrorOperationalCredentialsInit = @"Init failure while creating operational credentials delegate";
static NSString * const kErrorOperationalKeypairInit = @"Init failure while creating operational keypair bridge";
static NSString * const kErrorPairingInit = @"Init failure while creating a pairing delegate";
static NSString * const kErrorOtaProviderInit = @"Init failure while creating an OTA provider delegate";
static NSString * const kErrorPairDevice = @"Failure while pairing the device";
static NSString * const kErrorUnpairDevice = @"Failure while unpairing the device";
static NSString * const kErrorStopPairing = @"Failure while trying to stop the pairing process";
Expand All @@ -77,7 +75,6 @@ @interface MTRDeviceController ()

@property (readonly) chip::Controller::DeviceCommissioner * cppCommissioner;
@property (readonly) MTRDevicePairingDelegateBridge * pairingDelegateBridge;
@property (readonly) MTROTAProviderDelegateBridge * otaProviderDelegateBridge;
@property (readonly) MTROperationalCredentialsDelegate * operationalCredentialsDelegate;
@property (readonly) MTRP256KeypairBridge signingKeypairBridge;
@property (readonly) MTRP256KeypairBridge operationalKeypairBridge;
Expand All @@ -98,11 +95,6 @@ - (instancetype)initWithFactory:(MTRControllerFactory *)factory queue:(dispatch_
return nil;
}

_otaProviderDelegateBridge = new MTROTAProviderDelegateBridge();
if ([self checkForInitError:(_otaProviderDelegateBridge != nullptr) logMsg:kErrorOtaProviderInit]) {
return nil;
}

_operationalCredentialsDelegate = new MTROperationalCredentialsDelegate();
if ([self checkForInitError:(_operationalCredentialsDelegate != nullptr) logMsg:kErrorOperationalCredentialsInit]) {
return nil;
Expand Down Expand Up @@ -156,11 +148,6 @@ - (void)cleanup
_operationalCredentialsDelegate = nullptr;
}

if (_otaProviderDelegateBridge) {
delete _otaProviderDelegateBridge;
_otaProviderDelegateBridge = nullptr;
}

if (_pairingDelegateBridge) {
delete _pairingDelegateBridge;
_pairingDelegateBridge = nullptr;
Expand Down Expand Up @@ -629,13 +616,6 @@ - (void)setPairingDelegate:(id<MTRDevicePairingDelegate>)delegate queue:(dispatc
});
}

- (void)setOTAProviderDelegate:(id<MTROTAProviderDelegate>)delegate queue:(dispatch_queue_t)queue;
{
dispatch_async(_chipWorkQueue, ^{
self->_otaProviderDelegateBridge->setDelegate(delegate, queue);
});
}

- (BOOL)checkForInitError:(BOOL)condition logMsg:(NSString *)logMsg
{
if (condition) {
Expand Down
7 changes: 4 additions & 3 deletions src/darwin/Framework/CHIP/MTROTAProviderDelegateBridge.h
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,11 @@ NS_ASSUME_NONNULL_BEGIN
class MTROTAProviderDelegateBridge : public chip::app::Clusters::OTAProviderDelegate
{
public:
MTROTAProviderDelegateBridge();
MTROTAProviderDelegateBridge(id<MTROTAProviderDelegate> delegate);
~MTROTAProviderDelegateBridge();

void setDelegate(id<MTROTAProviderDelegate> delegate, dispatch_queue_t queue);
void Init(chip::System::Layer * systemLayer, chip::Messaging::ExchangeManager * exchangeManager);
void Shutdown();

void HandleQueryImage(
chip::app::CommandHandler * commandObj, const chip::app::ConcreteCommandPath & commandPath,
Expand Down Expand Up @@ -59,7 +60,7 @@ class MTROTAProviderDelegateBridge : public chip::app::Clusters::OTAProviderDele
MTROtaSoftwareUpdateProviderClusterNotifyUpdateAppliedParams * commandParams);

_Nullable id<MTROTAProviderDelegate> mDelegate;
_Nullable dispatch_queue_t mQueue;
dispatch_queue_t mWorkQueue;
};

NS_ASSUME_NONNULL_END
Loading

0 comments on commit 5cd92fb

Please sign in to comment.