61
61
static NSString * const kErrorControllerFactoryInit = @" Init failure while initializing controller factory" ;
62
62
static NSString * const kErrorKeystoreInit = @" Init failure while initializing persistent storage keystore" ;
63
63
static NSString * const kErrorCertStoreInit = @" Init failure while initializing persistent storage operational certificate store" ;
64
- static NSString * const kErrorOtaProviderInit = @" Init failure while creating an OTA provider delegate" ;
65
64
static NSString * const kErrorSessionKeystoreInit = @" Init failure while initializing session keystore" ;
66
65
67
66
static bool sExitHandlerRegistered = false ;
@@ -123,6 +122,9 @@ @interface MTRDeviceControllerFactory ()
123
122
// D. Locking around reads not from the Matter queue is OK but not required.
124
123
@property (nonatomic , readonly ) os_unfair_lock controllersLock;
125
124
125
+ @property (nonatomic , readonly , nullable ) id <MTROTAProviderDelegate> otaProviderDelegate;
126
+ @property (nonatomic , readonly , nullable ) dispatch_queue_t otaProviderDelegateQueue;
127
+
126
128
- (BOOL )findMatchingFabric : (FabricTable &)fabricTable
127
129
params : (MTRDeviceControllerStartupParams *)params
128
130
fabric : (const FabricInfo * _Nullable * _Nonnull)fabric ;
@@ -289,9 +291,15 @@ - (void)cleanupInitObjects
289
291
290
292
- (void )cleanupStartupObjects
291
293
{
292
- if (_otaProviderDelegateBridge) {
293
- delete _otaProviderDelegateBridge;
294
- _otaProviderDelegateBridge = nullptr ;
294
+ // Make sure the deinit order here is the reverse of the init order in
295
+ // startControllerFactory:
296
+ _certificationDeclarationCertificates = nil ;
297
+ _productAttestationAuthorityCertificates = nil ;
298
+
299
+ if (_opCertStore) {
300
+ _opCertStore->Finish ();
301
+ delete _opCertStore;
302
+ _opCertStore = nullptr ;
295
303
}
296
304
297
305
if (_keystore) {
@@ -300,11 +308,12 @@ - (void)cleanupStartupObjects
300
308
_keystore = nullptr ;
301
309
}
302
310
303
- if (_opCertStore) {
304
- _opCertStore->Finish ();
305
- delete _opCertStore;
306
- _opCertStore = nullptr ;
311
+ if (_otaProviderDelegateBridge) {
312
+ delete _otaProviderDelegateBridge;
313
+ _otaProviderDelegateBridge = nullptr ;
307
314
}
315
+ _otaProviderDelegateQueue = nil ;
316
+ _otaProviderDelegate = nil ;
308
317
309
318
if (_sessionResumptionStorage) {
310
319
delete _sessionResumptionStorage;
@@ -412,57 +421,12 @@ - (BOOL)startControllerFactory:(MTRDeviceControllerFactoryParams *)startupParams
412
421
return ;
413
422
}
414
423
415
- if (startupParams.otaProviderDelegate ) {
416
- if (![startupParams.otaProviderDelegate respondsToSelector: @selector (handleQueryImageForNodeID:
417
- controller:params:completion: )]
418
- && ![startupParams.otaProviderDelegate
419
- respondsToSelector: @selector (handleQueryImageForNodeID:controller:params:completionHandler: )]) {
420
- MTR_LOG_ERROR (" Error: MTROTAProviderDelegate does not support handleQueryImageForNodeID" );
421
- errorCode = CHIP_ERROR_INVALID_ARGUMENT;
422
- return ;
423
- }
424
- if (![startupParams.otaProviderDelegate
425
- respondsToSelector: @selector (handleApplyUpdateRequestForNodeID:controller:params:completion: )]
426
- && ![startupParams.otaProviderDelegate
427
- respondsToSelector: @selector (handleApplyUpdateRequestForNodeID:controller:params:completionHandler: )]) {
428
- MTR_LOG_ERROR (" Error: MTROTAProviderDelegate does not support handleApplyUpdateRequestForNodeID" );
429
- errorCode = CHIP_ERROR_INVALID_ARGUMENT;
430
- return ;
431
- }
432
- if (![startupParams.otaProviderDelegate
433
- respondsToSelector: @selector (handleNotifyUpdateAppliedForNodeID:controller:params:completion: )]
434
- && ![startupParams.otaProviderDelegate
435
- respondsToSelector: @selector (handleNotifyUpdateAppliedForNodeID:controller:params:completionHandler: )]) {
436
- MTR_LOG_ERROR (" Error: MTROTAProviderDelegate does not support handleNotifyUpdateAppliedForNodeID" );
437
- errorCode = CHIP_ERROR_INVALID_ARGUMENT;
438
- return ;
439
- }
440
- if (![startupParams.otaProviderDelegate
441
- respondsToSelector: @selector (handleBDXTransferSessionBeginForNodeID:
442
- controller:fileDesignator:offset:completion: )]
443
- && ![startupParams.otaProviderDelegate
444
- respondsToSelector: @selector
445
- (handleBDXTransferSessionBeginForNodeID:controller:fileDesignator:offset:completionHandler: )]) {
446
- MTR_LOG_ERROR (" Error: MTROTAProviderDelegate does not support handleBDXTransferSessionBeginForNodeID" );
447
- errorCode = CHIP_ERROR_INVALID_ARGUMENT;
448
- return ;
449
- }
450
- if (![startupParams.otaProviderDelegate
451
- respondsToSelector: @selector (handleBDXQueryForNodeID:controller:blockSize:blockIndex:bytesToSkip:completion: )]
452
- && ![startupParams.otaProviderDelegate
453
- respondsToSelector: @selector (handleBDXQueryForNodeID:
454
- controller:blockSize:blockIndex:bytesToSkip:completionHandler: )]) {
455
- MTR_LOG_ERROR (" Error: MTROTAProviderDelegate does not support handleBDXQueryForNodeID" );
456
- errorCode = CHIP_ERROR_INVALID_ARGUMENT;
457
- return ;
458
- }
459
- _otaProviderDelegateBridge = new MTROTAProviderDelegateBridge (startupParams.otaProviderDelegate );
460
- if (_otaProviderDelegateBridge == nil ) {
461
- MTR_LOG_ERROR (" Error: %@" , kErrorOtaProviderInit );
462
- errorCode = CHIP_ERROR_NO_MEMORY;
463
- return ;
464
- }
424
+ _otaProviderDelegate = startupParams.otaProviderDelegate ;
425
+ if (_otaProviderDelegate != nil ) {
426
+ _otaProviderDelegateQueue = dispatch_queue_create (
427
+ " org.csa-iot.matter.framework.otaprovider.workqueue" , DISPATCH_QUEUE_SERIAL_WITH_AUTORELEASE_POOL);
465
428
}
429
+ _otaProviderDelegateBridge = new MTROTAProviderDelegateBridge ();
466
430
467
431
// TODO: Allow passing a different keystore implementation via startupParams.
468
432
_keystore = new PersistentStorageOperationalKeystore ();
@@ -594,19 +558,25 @@ - (MTRDeviceController * _Nullable)_startDeviceController:(id)startupParams
594
558
return nil ;
595
559
}
596
560
597
- id <MTRDeviceControllerStorageDelegate> storageDelegate;
598
- dispatch_queue_t storageDelegateQueue;
561
+ id <MTRDeviceControllerStorageDelegate> _Nullable storageDelegate;
562
+ dispatch_queue_t _Nullable storageDelegateQueue;
599
563
NSUUID * uniqueIdentifier;
564
+ id <MTROTAProviderDelegate> _Nullable otaProviderDelegate;
565
+ dispatch_queue_t _Nullable otaProviderDelegateQueue;
600
566
if ([startupParams isKindOfClass: [MTRDeviceControllerStartupParameters class ]]) {
601
567
MTRDeviceControllerStartupParameters * params = startupParams;
602
568
storageDelegate = params.storageDelegate ;
603
569
storageDelegateQueue = params.storageDelegateQueue ;
604
570
uniqueIdentifier = params.uniqueIdentifier ;
571
+ otaProviderDelegate = params.otaProviderDelegate ;
572
+ otaProviderDelegateQueue = params.otaProviderDelegateQueue ;
605
573
} else if ([startupParams isKindOfClass: [MTRDeviceControllerStartupParams class ]]) {
606
574
MTRDeviceControllerStartupParams * params = startupParams;
607
575
storageDelegate = nil ;
608
576
storageDelegateQueue = nil ;
609
577
uniqueIdentifier = params.uniqueIdentifier ;
578
+ otaProviderDelegate = nil ;
579
+ otaProviderDelegateQueue = nil ;
610
580
} else {
611
581
MTR_LOG_ERROR (" Unknown kind of startup params: %@" , startupParams);
612
582
return nil ;
@@ -628,10 +598,19 @@ - (MTRDeviceController * _Nullable)_startDeviceController:(id)startupParams
628
598
return nil ;
629
599
}
630
600
601
+ // Fall back to the factory-wide OTA provider delegate if one is not
602
+ // provided in the startup params.
603
+ if (otaProviderDelegate == nil ) {
604
+ otaProviderDelegate = self.otaProviderDelegate ;
605
+ otaProviderDelegateQueue = self.otaProviderDelegateQueue ;
606
+ }
607
+
631
608
// Create the controller, so we start the event loop, since we plan to do
632
609
// our fabric table operations there.
633
610
auto * controller = [self _createController: storageDelegate
634
611
storageDelegateQueue: storageDelegateQueue
612
+ otaProviderDelegate: otaProviderDelegate
613
+ otaProviderDelegateQueue: otaProviderDelegateQueue
635
614
uniqueIdentifier: uniqueIdentifier];
636
615
if (controller == nil ) {
637
616
if (error != nil ) {
@@ -874,6 +853,8 @@ - (MTRDeviceController * _Nullable)createController:(MTRDeviceControllerStartupP
874
853
875
854
- (MTRDeviceController * _Nullable)_createController : (id <MTRDeviceControllerStorageDelegate> _Nullable)storageDelegate
876
855
storageDelegateQueue : (dispatch_queue_t _Nullable)storageDelegateQueue
856
+ otaProviderDelegate : (id <MTROTAProviderDelegate> _Nullable)otaProviderDelegate
857
+ otaProviderDelegateQueue : (dispatch_queue_t _Nullable)otaProviderDelegateQueue
877
858
uniqueIdentifier : (NSUUID *)uniqueIdentifier
878
859
{
879
860
[self _assertCurrentQueueIsNotMatterQueue ];
@@ -882,6 +863,8 @@ - (MTRDeviceController * _Nullable)_createController:(id<MTRDeviceControllerStor
882
863
queue: _chipWorkQueue
883
864
storageDelegate: storageDelegate
884
865
storageDelegateQueue: storageDelegateQueue
866
+ otaProviderDelegate: otaProviderDelegate
867
+ otaProviderDelegateQueue: otaProviderDelegateQueue
885
868
uniqueIdentifier: uniqueIdentifier];
886
869
if (controller == nil ) {
887
870
MTR_LOG_ERROR (" Failed to init controller" );
0 commit comments