67
67
using namespace chip ;
68
68
using namespace chip ::Controller;
69
69
70
- static NSString * const kErrorPersistentStorageInit = @" Init failure while creating a persistent storage delegate" ;
71
- static NSString * const kErrorSessionResumptionStorageInit = @" Init failure while creating a session resumption storage delegate" ;
72
- static NSString * const kErrorControllerFactoryInit = @" Init failure while initializing controller factory" ;
73
- static NSString * const kErrorKeystoreInit = @" Init failure while initializing persistent storage keystore" ;
74
- static NSString * const kErrorCertStoreInit = @" Init failure while initializing persistent storage operational certificate store" ;
75
-
76
70
static bool sExitHandlerRegistered = false ;
77
71
static void ShutdownOnExit ()
78
72
{
@@ -228,21 +222,6 @@ - (void)_assertCurrentQueueIsNotMatterQueue
228
222
VerifyOrDie (!DeviceLayer::PlatformMgrImpl ().IsWorkQueueCurrentQueue ());
229
223
}
230
224
231
- - (BOOL )checkIsRunning : (NSError * __autoreleasing *)error
232
- {
233
- [self _assertCurrentQueueIsNotMatterQueue ];
234
-
235
- if ([self isRunning ]) {
236
- return YES ;
237
- }
238
-
239
- if (error != nil ) {
240
- *error = [MTRError errorForCHIPErrorCode: CHIP_ERROR_INCORRECT_STATE];
241
- }
242
-
243
- return NO ;
244
- }
245
-
246
225
- (void )cleanupStartupObjects
247
226
{
248
227
assertChipStackLockedByCurrentThread ();
@@ -293,7 +272,7 @@ - (CHIP_ERROR)_initFabricTable:(FabricTable &)fabricTable
293
272
{
294
273
[self _assertCurrentQueueIsNotMatterQueue ];
295
274
296
- if (!self. isRunning ) {
275
+ if (!_running ) { // Note: reading _running from outside of the Matter work queue
297
276
return nil ;
298
277
}
299
278
@@ -339,40 +318,25 @@ - (BOOL)_startControllerFactory:(MTRDeviceControllerFactoryParams *)startupParam
339
318
{
340
319
[self _assertCurrentQueueIsNotMatterQueue ];
341
320
342
- if ([self isRunning ]) {
343
- MTR_LOG_DEBUG (" Ignoring duplicate call to startup, Matter controller factory already started..." );
344
- return YES ;
345
- }
346
-
347
- __block CHIP_ERROR errorCode = CHIP_NO_ERROR;
321
+ __block CHIP_ERROR err = CHIP_ERROR_INTERNAL;
348
322
dispatch_sync (_chipWorkQueue, ^{
349
- if ([self isRunning ]) {
350
- return ;
323
+ if (_running) {
324
+ // TODO: When treating a duplicate call as success we should validate parameters match
325
+ MTR_LOG_DEBUG (" Ignoring duplicate call to startup, Matter controller factory already started..." );
326
+ ExitNow (err = CHIP_NO_ERROR);
351
327
}
352
328
353
329
StartupMetricsCollection ();
354
330
InitializeServerAccessControl ();
355
331
356
332
if (startupParams.hasStorage ) {
357
- _persistentStorageDelegate = new (std::nothrow) MTRPersistentStorageDelegateBridge (startupParams.storage );
333
+ _persistentStorageDelegate = new MTRPersistentStorageDelegateBridge (startupParams.storage );
358
334
_sessionResumptionStorage = nullptr ;
359
335
_usingPerControllerStorage = NO ;
360
336
} else {
361
- _persistentStorageDelegate = new (std::nothrow) MTRDemuxingStorage (self);
362
- _sessionResumptionStorage = new (std::nothrow) MTRSessionResumptionStorageBridge (self);
337
+ _persistentStorageDelegate = new MTRDemuxingStorage (self);
338
+ _sessionResumptionStorage = new MTRSessionResumptionStorageBridge (self);
363
339
_usingPerControllerStorage = YES ;
364
-
365
- if (_sessionResumptionStorage == nil ) {
366
- MTR_LOG_ERROR (" Error: %@" , kErrorSessionResumptionStorageInit );
367
- errorCode = CHIP_ERROR_NO_MEMORY;
368
- return ;
369
- }
370
- }
371
-
372
- if (_persistentStorageDelegate == nil ) {
373
- MTR_LOG_ERROR (" Error: %@" , kErrorPersistentStorageInit );
374
- errorCode = CHIP_ERROR_NO_MEMORY;
375
- return ;
376
340
}
377
341
378
342
_otaProviderDelegate = startupParams.otaProviderDelegate ;
@@ -383,63 +347,42 @@ - (BOOL)_startControllerFactory:(MTRDeviceControllerFactoryParams *)startupParam
383
347
384
348
// TODO: Allow passing a different keystore implementation via startupParams.
385
349
_keystore = new PersistentStorageOperationalKeystore ();
386
- if (_keystore == nullptr ) {
387
- MTR_LOG_ERROR (" Error: %@" , kErrorKeystoreInit );
388
- errorCode = CHIP_ERROR_NO_MEMORY;
389
- return ;
390
- }
391
-
392
- errorCode = _keystore->Init (_persistentStorageDelegate);
393
- if (errorCode != CHIP_NO_ERROR) {
394
- MTR_LOG_ERROR (" Error: %@" , kErrorKeystoreInit );
395
- return ;
396
- }
350
+ SuccessOrExit (err = _keystore->Init (_persistentStorageDelegate));
397
351
398
352
// TODO Allow passing a different opcert store implementation via startupParams.
399
353
_opCertStore = new Credentials::PersistentStorageOpCertStore ();
400
- if (_opCertStore == nullptr ) {
401
- MTR_LOG_ERROR (" Error: %@" , kErrorCertStoreInit );
402
- errorCode = CHIP_ERROR_NO_MEMORY;
403
- return ;
404
- }
405
-
406
- errorCode = _opCertStore->Init (_persistentStorageDelegate);
407
- if (errorCode != CHIP_NO_ERROR) {
408
- MTR_LOG_ERROR (" Error: %@" , kErrorCertStoreInit );
409
- return ;
410
- }
354
+ SuccessOrExit (err = _opCertStore->Init (_persistentStorageDelegate));
411
355
412
356
_productAttestationAuthorityCertificates = [startupParams.productAttestationAuthorityCertificates copy ];
413
357
_certificationDeclarationCertificates = [startupParams.certificationDeclarationCertificates copy ];
414
358
415
- chip::Controller::FactoryInitParams params;
416
- if (startupParams.port != nil ) {
417
- params.listenPort = [startupParams.port unsignedShortValue ];
418
- }
419
- params.enableServerInteractions = startupParams.shouldStartServer ;
420
-
421
- params.groupDataProvider = &_groupDataProvider;
422
- params.sessionKeystore = &_sessionKeystore;
423
- params.fabricIndependentStorage = _persistentStorageDelegate;
424
- params.operationalKeystore = _keystore;
425
- params.opCertStore = _opCertStore;
426
- params.certificateValidityPolicy = &_certificateValidityPolicy;
427
- params.sessionResumptionStorage = _sessionResumptionStorage;
428
- errorCode = _controllerFactory->Init (params);
429
- if (errorCode != CHIP_NO_ERROR) {
430
- MTR_LOG_ERROR (" Error: %@" , kErrorControllerFactoryInit );
431
- return ;
359
+ {
360
+ chip::Controller::FactoryInitParams params;
361
+ if (startupParams.port != nil ) {
362
+ params.listenPort = [startupParams.port unsignedShortValue ];
363
+ }
364
+ params.enableServerInteractions = startupParams.shouldStartServer ;
365
+
366
+ params.groupDataProvider = &_groupDataProvider;
367
+ params.sessionKeystore = &_sessionKeystore;
368
+ params.fabricIndependentStorage = _persistentStorageDelegate;
369
+ params.operationalKeystore = _keystore;
370
+ params.opCertStore = _opCertStore;
371
+ params.certificateValidityPolicy = &_certificateValidityPolicy;
372
+ params.sessionResumptionStorage = _sessionResumptionStorage;
373
+ SuccessOrExit (err = _controllerFactory->Init (params));
432
374
}
433
375
434
376
// This needs to happen after DeviceControllerFactory::Init,
435
377
// because that creates (lazily, by calling functions with
436
378
// static variables in them) some static-lifetime objects.
437
379
if (!sExitHandlerRegistered ) {
438
- int ret = atexit (ShutdownOnExit);
439
- if (ret != 0 ) {
440
- MTR_LOG_ERROR ( " Error registering exit handler: %d " , ret );
441
- return ;
380
+ if ( atexit (ShutdownOnExit) != 0 ) {
381
+ char error[ 128 ];
382
+ strerror_r (errno, error, sizeof (error) );
383
+ MTR_LOG_ERROR ( " Warning: Failed to register atexit handler: %s " , error) ;
442
384
}
385
+ sExitHandlerRegistered = true ;
443
386
}
444
387
HeapObjectPoolExitHandling::IgnoreLeaksOnExit ();
445
388
@@ -452,47 +395,44 @@ - (BOOL)_startControllerFactory:(MTRDeviceControllerFactoryParams *)startupParam
452
395
_controllerFactory->RetainSystemState ();
453
396
_controllerFactory->ReleaseSystemState ();
454
397
455
- self-> _advertiseOperational = startupParams.shouldStartServer ;
456
- self-> _running = YES ;
457
- }) ;
398
+ _advertiseOperational = startupParams.shouldStartServer ;
399
+ _running = YES ;
400
+ err = CHIP_NO_ERROR ;
458
401
459
- if (![self isRunning ]) {
460
- dispatch_sync (_chipWorkQueue, ^{
402
+ exit :
403
+ if (err != CHIP_NO_ERROR) {
404
+ // Note: Since we have failed no later than _controllerFactory->Init(),
405
+ // there is no need to call _controllerFactory->Shutdown() here.
461
406
[self cleanupStartupObjects ];
462
- });
407
+ }
408
+ });
409
+ if (err != CHIP_NO_ERROR) {
410
+ MTR_LOG_ERROR (" Failed to start Matter controller factory: %" CHIP_ERROR_FORMAT, err.Format ());
463
411
if (error != nil ) {
464
- *error = [MTRError errorForCHIPErrorCode: errorCode ];
412
+ *error = [MTRError errorForCHIPErrorCode: err ];
465
413
}
466
414
return NO ;
467
415
}
468
-
469
416
return YES ;
470
417
}
471
418
472
419
- (void )stopControllerFactory
473
420
{
474
421
[self _assertCurrentQueueIsNotMatterQueue ];
475
422
476
- if (![self isRunning ]) {
477
- return ;
478
- }
479
-
480
423
while ([_controllers count ] != 0 ) {
481
424
[_controllers[0 ] shutdown ];
482
425
}
483
426
484
427
dispatch_sync (_chipWorkQueue, ^{
428
+ VerifyOrReturn (_running);
429
+
485
430
MTR_LOG_INFO (" Shutting down the Matter controller factory" );
486
431
_controllerFactory->Shutdown ();
487
-
488
432
[self cleanupStartupObjects ];
433
+ _running = NO ;
434
+ _advertiseOperational = NO ;
489
435
});
490
-
491
- // NOTE: we do not call cleanupInitObjects because we can be restarted, and
492
- // that does not re-create the objects that we create inside init.
493
- // Maybe we should be creating them in startup?
494
-
495
- _running = NO ;
496
436
}
497
437
498
438
/* *
@@ -540,7 +480,7 @@ - (MTRDeviceController * _Nullable)_startDeviceController:(MTRDeviceController *
540
480
return nil ;
541
481
}
542
482
543
- if (![ self isRunning ] ) {
483
+ if (!_running ) { // Note: reading _running from outside of the Matter work queue
544
484
if (storageDelegate != nil ) {
545
485
MTR_LOG_DEFAULT (" Auto-starting Matter controller factory in per-controller storage mode" );
546
486
auto * params = [[MTRDeviceControllerFactoryParams alloc ] initWithoutStorage ];
@@ -744,13 +684,8 @@ - (MTRDeviceController * _Nullable)createControllerOnExistingFabric:(MTRDeviceCo
744
684
keystore: self ->_keystore
745
685
advertiseOperational: self ->_advertiseOperational
746
686
params: startupParams];
747
- if (params == nil ) {
748
- fabricError = CHIP_ERROR_NO_MEMORY;
749
- } else {
750
- params.productAttestationAuthorityCertificates = self->_productAttestationAuthorityCertificates ;
751
- params.certificationDeclarationCertificates = self->_certificationDeclarationCertificates ;
752
- }
753
-
687
+ params.productAttestationAuthorityCertificates = self->_productAttestationAuthorityCertificates ;
688
+ params.certificationDeclarationCertificates = self->_certificationDeclarationCertificates ;
754
689
return params;
755
690
}
756
691
error: error];
@@ -800,12 +735,8 @@ - (MTRDeviceController * _Nullable)createControllerOnNewFabric:(MTRDeviceControl
800
735
keystore: self ->_keystore
801
736
advertiseOperational: self ->_advertiseOperational
802
737
params: startupParams];
803
- if (params == nil ) {
804
- fabricError = CHIP_ERROR_NO_MEMORY;
805
- } else {
806
- params.productAttestationAuthorityCertificates = self->_productAttestationAuthorityCertificates ;
807
- params.certificationDeclarationCertificates = self->_certificationDeclarationCertificates ;
808
- }
738
+ params.productAttestationAuthorityCertificates = self->_productAttestationAuthorityCertificates ;
739
+ params.certificationDeclarationCertificates = self->_certificationDeclarationCertificates ;
809
740
return params;
810
741
}
811
742
error: error];
@@ -907,12 +838,6 @@ - (MTRDeviceController * _Nullable)maybeInitializeOTAProvider:(MTRDeviceControll
907
838
908
839
- (void )resetOperationalAdvertising
909
840
{
910
- if (![self checkIsRunning: nil ]) {
911
- // No need to reset anything; we are not running, so not
912
- // advertising.
913
- return ;
914
- }
915
-
916
841
if (!_advertiseOperational) {
917
842
// No need to reset anything; we are not advertising the things that
918
843
// would need to get reset.
0 commit comments