@@ -417,7 +417,14 @@ - (CHIP_ERROR)_initFabricTable:(FabricTable &)fabricTable
417
417
return [NSArray arrayWithArray: fabricList];
418
418
}
419
419
420
- - (BOOL )startControllerFactory : (MTRDeviceControllerFactoryParams *)startupParams error : (NSError * __autoreleasing *)error ;
420
+ - (BOOL )startControllerFactory : (MTRDeviceControllerFactoryParams *)startupParams error : (NSError * __autoreleasing *)error
421
+ {
422
+ return [self _startControllerFactory: startupParams startingController: NO error: error];
423
+ }
424
+
425
+ - (BOOL )_startControllerFactory : (MTRDeviceControllerFactoryParams *)startupParams
426
+ startingController : (BOOL )startingController
427
+ error : (NSError * __autoreleasing *)error
421
428
{
422
429
[self _assertCurrentQueueIsNotMatterQueue ];
423
430
@@ -535,6 +542,7 @@ - (BOOL)startControllerFactory:(MTRDeviceControllerFactoryParams *)startupParams
535
542
// state is brought up live on factory init, and not when it comes time
536
543
// to actually start a controller, and does not actually clean itself up
537
544
// until its refcount (which starts as 0) goes to 0.
545
+ // TODO: Don't cause a stack shutdown and restart if startingController
538
546
_controllerFactory->RetainSystemState ();
539
547
_controllerFactory->ReleaseSystemState ();
540
548
@@ -597,11 +605,6 @@ - (MTRDeviceController * _Nullable)_startDeviceController:(MTRDeviceController *
597
605
{
598
606
[self _assertCurrentQueueIsNotMatterQueue ];
599
607
600
- if (![self checkIsRunning: error]) {
601
- MTR_LOG_ERROR (" Trying to start controller while Matter controller factory is not running" );
602
- return nil ;
603
- }
604
-
605
608
id <MTRDeviceControllerStorageDelegate> _Nullable storageDelegate;
606
609
dispatch_queue_t _Nullable storageDelegateQueue;
607
610
NSUUID * uniqueIdentifier;
@@ -623,9 +626,28 @@ - (MTRDeviceController * _Nullable)_startDeviceController:(MTRDeviceController *
623
626
otaProviderDelegateQueue = nil ;
624
627
} else {
625
628
MTR_LOG_ERROR (" Unknown kind of startup params: %@" , startupParams);
629
+ if (error != nil ) {
630
+ *error = [MTRError errorForCHIPErrorCode: CHIP_ERROR_INVALID_ARGUMENT];
631
+ }
626
632
return nil ;
627
633
}
628
634
635
+ if (![self isRunning ]) {
636
+ if (storageDelegate != nil ) {
637
+ MTR_LOG_DEFAULT (" Auto-starting Matter controller factory in per-controller storage mode" );
638
+ auto * params = [[MTRDeviceControllerFactoryParams alloc ] initWithoutStorage ];
639
+ if (![self _startControllerFactory: params startingController: YES error: error]) {
640
+ return nil ;
641
+ }
642
+ } else {
643
+ MTR_LOG_ERROR (" Trying to start controller while Matter controller factory is not running" );
644
+ if (error != nil ) {
645
+ *error = [MTRError errorForCHIPErrorCode: CHIP_ERROR_INCORRECT_STATE];
646
+ }
647
+ return nil ;
648
+ }
649
+ }
650
+
629
651
if (_usingPerControllerStorage && storageDelegate == nil ) {
630
652
MTR_LOG_ERROR (" Must have a controller storage delegate when we do not have storage for the controller factory" );
631
653
if (error != nil ) {
0 commit comments