17
17
#import " MTRDeviceControllerFactory.h"
18
18
#import " MTRDeviceControllerFactory_Internal.h"
19
19
20
+ #import < Matter/MTRDefines.h>
21
+
22
+ #if MTR_PER_CONTROLLER_STORAGE_ENABLED
23
+ #import < Matter/MTRDeviceControllerParameters.h>
24
+ #else
25
+ #import " MTRDeviceControllerParameters_Wrapper.h"
26
+ #endif // MTR_PER_CONTROLLER_STORAGE_ENABLED
27
+
20
28
#import " MTRCertificates.h"
21
29
#import " MTRControllerAccessControl.h"
22
30
#import " MTRDemuxingStorage.h"
34
42
#import " MTRPersistentStorageDelegateBridge.h"
35
43
#import " MTRSessionResumptionStorageBridge.h"
36
44
#import " NSDataSpanConversion.h"
37
- #if !MTR_PER_CONTROLLER_STORAGE_ENABLED
38
- #import " MTRDeviceControllerStartupParameters_Wrapper.h"
39
- #endif // MTR_PER_CONTROLLER_STORAGE_ENABLED
40
45
41
46
#import < os/lock.h>
42
47
@@ -547,8 +552,12 @@ - (void)stopControllerFactory
547
552
* The fabricChecker block will run on the Matter queue, and is expected to
548
553
* return nil if pre-startup fabric table checks fail, and set fabricError to
549
554
* the right error value in that situation.
555
+ *
556
+ * The provided controller is expected to have just been allocated and to not be
557
+ * initialized yet.
550
558
*/
551
- - (MTRDeviceController * _Nullable)_startDeviceController : (id )startupParams
559
+ - (MTRDeviceController * _Nullable)_startDeviceController : (MTRDeviceController *)controller
560
+ startupParams : (id )startupParams
552
561
fabricChecker : (MTRDeviceControllerStartupParamsInternal * (^)(FabricTable * fabricTable,
553
562
MTRDeviceController * controller,
554
563
CHIP_ERROR & fabricError))fabricChecker
@@ -566,8 +575,8 @@ - (MTRDeviceController * _Nullable)_startDeviceController:(id)startupParams
566
575
NSUUID * uniqueIdentifier;
567
576
id <MTROTAProviderDelegate> _Nullable otaProviderDelegate;
568
577
dispatch_queue_t _Nullable otaProviderDelegateQueue;
569
- if ([startupParams isKindOfClass: [MTRDeviceControllerStartupParameters class ]]) {
570
- MTRDeviceControllerStartupParameters * params = startupParams;
578
+ if ([startupParams isKindOfClass: [MTRDeviceControllerParameters class ]]) {
579
+ MTRDeviceControllerParameters * params = startupParams;
571
580
storageDelegate = params.storageDelegate ;
572
581
storageDelegateQueue = params.storageDelegateQueue ;
573
582
uniqueIdentifier = params.uniqueIdentifier ;
@@ -608,20 +617,35 @@ - (MTRDeviceController * _Nullable)_startDeviceController:(id)startupParams
608
617
otaProviderDelegateQueue = self.otaProviderDelegateQueue ;
609
618
}
610
619
611
- // Create the controller, so we start the event loop, since we plan to do
612
- // our fabric table operations there.
613
- auto * controller = [ self _createController : storageDelegate
614
- storageDelegateQueue: storageDelegateQueue
615
- otaProviderDelegate: otaProviderDelegate
616
- otaProviderDelegateQueue: otaProviderDelegateQueue
617
- uniqueIdentifier: uniqueIdentifier];
620
+ controller = [ controller initWithFactory: self
621
+ queue: _chipWorkQueue
622
+ storageDelegate : storageDelegate
623
+ storageDelegateQueue: storageDelegateQueue
624
+ otaProviderDelegate: otaProviderDelegate
625
+ otaProviderDelegateQueue: otaProviderDelegateQueue
626
+ uniqueIdentifier: uniqueIdentifier];
618
627
if (controller == nil ) {
619
628
if (error != nil ) {
620
- *error = [MTRError errorForCHIPErrorCode: CHIP_ERROR_NO_MEMORY ];
629
+ *error = [MTRError errorForCHIPErrorCode: CHIP_ERROR_INVALID_ARGUMENT ];
621
630
}
622
631
return nil ;
623
632
}
624
633
634
+ if ([_controllers count ] == 0 ) {
635
+ // Bringing up the first controller. Start the event loop now. If we
636
+ // fail to bring it up, its cleanup will stop the event loop again.
637
+ chip::DeviceLayer::PlatformMgrImpl ().StartEventLoopTask ();
638
+ dispatch_sync (_chipWorkQueue, ^{
639
+ self->_operationalBrowser = new MTROperationalBrowser (self, self->_chipWorkQueue );
640
+ });
641
+ }
642
+
643
+ // Add the controller to _controllers now, so if we fail partway through its
644
+ // startup we will still do the right cleanups.
645
+ os_unfair_lock_lock (&_controllersLock);
646
+ [_controllers addObject: controller];
647
+ os_unfair_lock_unlock (&_controllersLock);
648
+
625
649
__block MTRDeviceControllerStartupParamsInternal * params = nil ;
626
650
__block CHIP_ERROR fabricError = CHIP_NO_ERROR;
627
651
@@ -716,7 +740,8 @@ - (MTRDeviceController * _Nullable)createControllerOnExistingFabric:(MTRDeviceCo
716
740
return nil ;
717
741
}
718
742
719
- return [self _startDeviceController: startupParams
743
+ return [self _startDeviceController: [MTRDeviceController alloc ]
744
+ startupParams: startupParams
720
745
fabricChecker: ^MTRDeviceControllerStartupParamsInternal *(
721
746
FabricTable * fabricTable, MTRDeviceController * controller, CHIP_ERROR & fabricError) {
722
747
const FabricInfo * fabric = nullptr ;
@@ -792,7 +817,8 @@ - (MTRDeviceController * _Nullable)createControllerOnNewFabric:(MTRDeviceControl
792
817
return nil ;
793
818
}
794
819
795
- return [self _startDeviceController: startupParams
820
+ return [self _startDeviceController: [MTRDeviceController alloc ]
821
+ startupParams: startupParams
796
822
fabricChecker: ^MTRDeviceControllerStartupParamsInternal *(
797
823
FabricTable * fabricTable, MTRDeviceController * controller, CHIP_ERROR & fabricError) {
798
824
const FabricInfo * fabric = nullptr ;
@@ -825,73 +851,6 @@ - (MTRDeviceController * _Nullable)createControllerOnNewFabric:(MTRDeviceControl
825
851
error: error];
826
852
}
827
853
828
- - (MTRDeviceController * _Nullable)createController : (MTRDeviceControllerStartupParameters *)startupParameters
829
- error : (NSError * __autoreleasing *)error
830
- {
831
- [self _assertCurrentQueueIsNotMatterQueue ];
832
-
833
- return [self _startDeviceController: startupParameters
834
- fabricChecker: ^MTRDeviceControllerStartupParamsInternal *(
835
- FabricTable * fabricTable, MTRDeviceController * controller, CHIP_ERROR & fabricError) {
836
- auto advertiseOperational = self.advertiseOperational && startupParameters.shouldAdvertiseOperational ;
837
- auto * params =
838
- [[MTRDeviceControllerStartupParamsInternal alloc ] initForNewController: controller
839
- fabricTable: fabricTable
840
- keystore: self ->_keystore
841
- advertiseOperational: advertiseOperational
842
- params: startupParameters
843
- error: fabricError];
844
- if (params != nil ) {
845
- if (params.productAttestationAuthorityCertificates == nil ) {
846
- params.productAttestationAuthorityCertificates = self.productAttestationAuthorityCertificates ;
847
- }
848
- if (params.certificationDeclarationCertificates == nil ) {
849
- params.certificationDeclarationCertificates = self.certificationDeclarationCertificates ;
850
- }
851
- }
852
- return params;
853
- }
854
- error: error];
855
- }
856
-
857
- - (MTRDeviceController * _Nullable)_createController : (id <MTRDeviceControllerStorageDelegate> _Nullable)storageDelegate
858
- storageDelegateQueue : (dispatch_queue_t _Nullable)storageDelegateQueue
859
- otaProviderDelegate : (id <MTROTAProviderDelegate> _Nullable)otaProviderDelegate
860
- otaProviderDelegateQueue : (dispatch_queue_t _Nullable)otaProviderDelegateQueue
861
- uniqueIdentifier : (NSUUID *)uniqueIdentifier
862
- {
863
- [self _assertCurrentQueueIsNotMatterQueue ];
864
-
865
- MTRDeviceController * controller = [[MTRDeviceController alloc ] initWithFactory: self
866
- queue: _chipWorkQueue
867
- storageDelegate: storageDelegate
868
- storageDelegateQueue: storageDelegateQueue
869
- otaProviderDelegate: otaProviderDelegate
870
- otaProviderDelegateQueue: otaProviderDelegateQueue
871
- uniqueIdentifier: uniqueIdentifier];
872
- if (controller == nil ) {
873
- MTR_LOG_ERROR (" Failed to init controller" );
874
- return nil ;
875
- }
876
-
877
- if ([_controllers count ] == 0 ) {
878
- // Bringing up the first controller. Start the event loop now. If we
879
- // fail to bring it up, its cleanup will stop the event loop again.
880
- chip::DeviceLayer::PlatformMgrImpl ().StartEventLoopTask ();
881
- dispatch_sync (_chipWorkQueue, ^{
882
- self->_operationalBrowser = new MTROperationalBrowser (self, self->_chipWorkQueue );
883
- });
884
- }
885
-
886
- // Add the controller to _controllers now, so if we fail partway through its
887
- // startup we will still do the right cleanups.
888
- os_unfair_lock_lock (&_controllersLock);
889
- [_controllers addObject: controller];
890
- os_unfair_lock_unlock (&_controllersLock);
891
-
892
- return controller;
893
- }
894
-
895
854
// Finds a fabric that matches the given params, if one exists.
896
855
//
897
856
// Returns NO on failure, YES on success. If YES is returned, the
@@ -1126,6 +1085,37 @@ - (void)operationalInstanceAdded:(chip::PeerId &)operationalID
1126
1085
}
1127
1086
}
1128
1087
1088
+ - (MTRDeviceController * _Nullable)initializeController : (MTRDeviceController *)controller
1089
+ withParameters : (MTRDeviceControllerParameters *)parameters
1090
+ error : (NSError * __autoreleasing *)error
1091
+ {
1092
+ [self _assertCurrentQueueIsNotMatterQueue ];
1093
+
1094
+ return [self _startDeviceController: controller
1095
+ startupParams: parameters
1096
+ fabricChecker: ^MTRDeviceControllerStartupParamsInternal *(
1097
+ FabricTable * fabricTable, MTRDeviceController * controller, CHIP_ERROR & fabricError) {
1098
+ auto advertiseOperational = self.advertiseOperational && parameters.shouldAdvertiseOperational ;
1099
+ auto * params =
1100
+ [[MTRDeviceControllerStartupParamsInternal alloc ] initForNewController: controller
1101
+ fabricTable: fabricTable
1102
+ keystore: self ->_keystore
1103
+ advertiseOperational: advertiseOperational
1104
+ params: parameters
1105
+ error: fabricError];
1106
+ if (params != nil ) {
1107
+ if (params.productAttestationAuthorityCertificates == nil ) {
1108
+ params.productAttestationAuthorityCertificates = self.productAttestationAuthorityCertificates ;
1109
+ }
1110
+ if (params.certificationDeclarationCertificates == nil ) {
1111
+ params.certificationDeclarationCertificates = self.certificationDeclarationCertificates ;
1112
+ }
1113
+ }
1114
+ return params;
1115
+ }
1116
+ error: error];
1117
+ }
1118
+
1129
1119
- (PersistentStorageDelegate *)storageDelegate
1130
1120
{
1131
1121
return _persistentStorageDelegate;
@@ -1176,7 +1166,7 @@ - (instancetype)initWithStorage:(id<MTRStorage>)storage
1176
1166
return self;
1177
1167
}
1178
1168
1179
- - (instancetype )init
1169
+ - (instancetype )initWithoutStorage
1180
1170
{
1181
1171
if (!(self = [super init ])) {
1182
1172
return nil ;
@@ -1191,7 +1181,7 @@ - (instancetype)init
1191
1181
_productAttestationAuthorityCertificates = nil ;
1192
1182
_certificationDeclarationCertificates = nil ;
1193
1183
_port = nil ;
1194
- _shouldStartServer = NO ;
1184
+ _shouldStartServer = YES ;
1195
1185
1196
1186
return self;
1197
1187
}
0 commit comments