From 13f34ce27223cf87a486a5d34dae2b665edf1854 Mon Sep 17 00:00:00 2001 From: Boris Zbarsky Date: Tue, 13 Sep 2022 15:48:47 -0400 Subject: [PATCH] Address API review issues in MTRControllerFactory. * Rename to MTRDeviceControllerFactory. * Change the startup params startServer to shouldStartServer. * Change the startup params init signatures to be more aligned. * Change isRunning to running. * Rename startup to startControllerFactory and add NSError outparam. * Rename shutdown to stopControllerFactory * Rename startControllerOnExistingFabric to createControllerOnExistingFabric and add NSError outparam. * Rename startControllerOnNewFabric to createControllerOnNewFabric and add NSError outparam. * Fix a leak when we failed to start a controller because it wanted a fabric that does not exist, or wanted a new fabric and a matching one existed. This used to not show up in LSan before, maybe because we did not have an autoreleasepool in place. Fixes https://github.com/project-chip/connectedhomeip/issues/22594 Addresses part of https://github.com/project-chip/connectedhomeip/issues/22420 --- .../commands/common/CHIPCommandBridge.mm | 31 +- examples/darwin-framework-tool/main.mm | 20 +- .../Framework Helpers/DefaultsUtils.m | 16 +- .../Framework/CHIP/MTRBaseDevice_Internal.h | 2 +- .../Framework/CHIP/MTRDeviceController.h | 4 +- .../Framework/CHIP/MTRDeviceController.mm | 14 +- ...Factory.h => MTRDeviceControllerFactory.h} | 46 +- ...ctory.mm => MTRDeviceControllerFactory.mm} | 120 ++++- ... => MTRDeviceControllerFactory_Internal.h} | 6 +- .../CHIP/MTRDeviceControllerStartupParams.h | 23 +- .../CHIP/MTRDeviceControllerStartupParams.mm | 12 +- ...TRDeviceControllerStartupParams_Internal.h | 12 +- .../CHIP/MTRDeviceController_Internal.h | 27 +- .../CHIP/MTROTAProviderDelegateBridge.mm | 10 +- src/darwin/Framework/CHIP/Matter.h | 2 +- .../Framework/CHIPTests/MTRControllerTests.m | 482 ++++++++---------- .../Framework/CHIPTests/MTRDeviceTests.m | 14 +- .../CHIPTests/MTRXPCListenerSampleTests.m | 18 +- .../Matter.xcodeproj/project.pbxproj | 24 +- 19 files changed, 467 insertions(+), 416 deletions(-) rename src/darwin/Framework/CHIP/{MTRControllerFactory.h => MTRDeviceControllerFactory.h} (66%) rename src/darwin/Framework/CHIP/{MTRControllerFactory.mm => MTRDeviceControllerFactory.mm} (84%) rename src/darwin/Framework/CHIP/{MTRControllerFactory_Internal.h => MTRDeviceControllerFactory_Internal.h} (89%) diff --git a/examples/darwin-framework-tool/commands/common/CHIPCommandBridge.mm b/examples/darwin-framework-tool/commands/common/CHIPCommandBridge.mm index 1ff4a427eeb61e..e886eed2c56b0c 100644 --- a/examples/darwin-framework-tool/commands/common/CHIPCommandBridge.mm +++ b/examples/darwin-framework-tool/commands/common/CHIPCommandBridge.mm @@ -111,15 +111,15 @@ mOTADelegate = [[OTAProviderDelegate alloc] init]; - auto factory = [MTRControllerFactory sharedInstance]; + auto factory = [MTRDeviceControllerFactory sharedInstance]; if (factory == nil) { ChipLogError(chipTool, "Controller factory is nil"); return CHIP_ERROR_INTERNAL; } - auto params = [[MTRControllerFactoryParams alloc] initWithStorage:storage]; + auto params = [[MTRDeviceControllerFactoryParams alloc] initWithStorage:storage]; params.port = @(kListenPort); - params.startServer = YES; + params.shouldStartServer = YES; params.otaProviderDelegate = mOTADelegate; NSArray * paaCertResults; ReturnLogErrorOnFailure(GetPAACertsFromFolder(&paaCertResults)); @@ -127,9 +127,10 @@ params.paaCerts = paaCertResults; } - if ([factory startup:params] == NO) { + NSError * error; + if ([factory startControllerFactory:params error:&error] == NO) { ChipLogError(chipTool, "Controller factory startup failed"); - return CHIP_ERROR_INTERNAL; + return MTRErrorToCHIPErrorCode(error); } ReturnLogErrorOnFailure([gNocSigner createOrLoadKeys:storage]); @@ -138,21 +139,19 @@ constexpr const char * identities[] = { kIdentityAlpha, kIdentityBeta, kIdentityGamma }; for (size_t i = 0; i < ArraySize(identities); ++i) { - auto controllerParams = [[MTRDeviceControllerStartupParams alloc] initWithSigningKeypair:gNocSigner - fabricID:@(i + 1) - ipk:ipk]; + auto controllerParams = [[MTRDeviceControllerStartupParams alloc] initWithIPK:ipk fabricID:@(i + 1) nocSigner:gNocSigner]; // We're not sure whether we're creating a new fabric or using an // existing one, so just try both. - auto controller = [factory startControllerOnExistingFabric:controllerParams]; + auto controller = [factory createControllerOnExistingFabric:controllerParams error:&error]; if (controller == nil) { // Maybe we didn't have this fabric yet. controllerParams.vendorID = @(chip::VendorId::TestVendor1); - controller = [factory startControllerOnNewFabric:controllerParams]; + controller = [factory createControllerOnNewFabric:controllerParams error:&error]; } if (controller == nil) { ChipLogError(chipTool, "Controller startup failure."); - return CHIP_ERROR_INTERNAL; + return MTRErrorToCHIPErrorCode(error); } mControllers[identities[i]] = controller; @@ -195,16 +194,14 @@ { StopCommissioners(); - auto factory = [MTRControllerFactory sharedInstance]; + auto factory = [MTRDeviceControllerFactory sharedInstance]; NSData * ipk = [gNocSigner getIPK]; constexpr const char * identities[] = { kIdentityAlpha, kIdentityBeta, kIdentityGamma }; for (size_t i = 0; i < ArraySize(identities); ++i) { - auto controllerParams = [[MTRDeviceControllerStartupParams alloc] initWithSigningKeypair:gNocSigner - fabricID:@(i + 1) - ipk:ipk]; + auto controllerParams = [[MTRDeviceControllerStartupParams alloc] initWithIPK:ipk fabricID:@(i + 1) nocSigner:gNocSigner]; - auto controller = [factory startControllerOnExistingFabric:controllerParams]; + auto controller = [factory createControllerOnExistingFabric:controllerParams error:nil]; mControllers[identities[i]] = controller; } } @@ -216,7 +213,7 @@ mControllers.clear(); mCurrentController = nil; - [[MTRControllerFactory sharedInstance] shutdown]; + [[MTRDeviceControllerFactory sharedInstance] stopControllerFactory]; } CHIP_ERROR CHIPCommandBridge::StartWaiting(chip::System::Clock::Timeout duration) diff --git a/examples/darwin-framework-tool/main.mm b/examples/darwin-framework-tool/main.mm index 09885d08eb3178..f0aeab45f78342 100644 --- a/examples/darwin-framework-tool/main.mm +++ b/examples/darwin-framework-tool/main.mm @@ -30,13 +30,15 @@ int main(int argc, const char * argv[]) { - Commands commands; - registerCommandsPairing(commands); - registerCommandsInteractive(commands); - registerCommandsPayload(commands); - registerClusterOtaSoftwareUpdateProviderInteractive(commands); - registerCommandsStorage(commands); - registerCommandsTests(commands); - registerClusters(commands); - return commands.Run(argc, (char **) argv); + @autoreleasepool { + Commands commands; + registerCommandsPairing(commands); + registerCommandsInteractive(commands); + registerCommandsPayload(commands); + registerClusterOtaSoftwareUpdateProviderInteractive(commands); + registerCommandsStorage(commands); + registerCommandsTests(commands); + registerClusters(commands); + return commands.Run(argc, (char **) argv); + } } diff --git a/src/darwin/CHIPTool/CHIPTool/Framework Helpers/DefaultsUtils.m b/src/darwin/CHIPTool/CHIPTool/Framework Helpers/DefaultsUtils.m index 18cb9544c10932..18511c34a76976 100644 --- a/src/darwin/CHIPTool/CHIPTool/Framework Helpers/DefaultsUtils.m +++ b/src/darwin/CHIPTool/CHIPTool/Framework Helpers/DefaultsUtils.m @@ -76,9 +76,9 @@ void MTRSetNextAvailableDeviceID(uint64_t id) static dispatch_once_t onceToken; dispatch_once(&onceToken, ^{ CHIPToolPersistentStorageDelegate * storage = [[CHIPToolPersistentStorageDelegate alloc] init]; - __auto_type * factory = [MTRControllerFactory sharedInstance]; - __auto_type * factoryParams = [[MTRControllerFactoryParams alloc] initWithStorage:storage]; - if (![factory startup:factoryParams]) { + __auto_type * factory = [MTRDeviceControllerFactory sharedInstance]; + __auto_type * factoryParams = [[MTRDeviceControllerFactoryParams alloc] initWithStorage:storage]; + if (![factory startControllerFactory:factoryParams error:nil]) { return; } @@ -87,14 +87,14 @@ void MTRSetNextAvailableDeviceID(uint64_t id) return; } - __auto_type * params = [[MTRDeviceControllerStartupParams alloc] initWithSigningKeypair:keys fabricID:@(1) ipk:keys.ipk]; + __auto_type * params = [[MTRDeviceControllerStartupParams alloc] initWithIPK:keys.ipk fabricID:@(1) nocSigner:keys]; params.vendorID = @(kTestVendorId); // We're not sure whether we have a fabric configured already; try as if // we did, and if not fall back to creating a new one. - sController = [factory startControllerOnExistingFabric:params]; + sController = [factory createControllerOnExistingFabric:params error:nil]; if (sController == nil) { - sController = [factory startControllerOnNewFabric:params]; + sController = [factory createControllerOnNewFabric:params error:nil]; } }); @@ -113,9 +113,9 @@ void MTRSetNextAvailableDeviceID(uint64_t id) [controller shutdown]; NSLog(@"Starting up the stack"); - __auto_type * params = [[MTRDeviceControllerStartupParams alloc] initWithSigningKeypair:keys fabricID:@(1) ipk:keys.ipk]; + __auto_type * params = [[MTRDeviceControllerStartupParams alloc] initWithIPK:keys.ipk fabricID:@(1) nocSigner:keys]; - sController = [[MTRControllerFactory sharedInstance] startControllerOnExistingFabric:params]; + sController = [[MTRDeviceControllerFactory sharedInstance] createControllerOnExistingFabric:params error:nil]; return sController; } diff --git a/src/darwin/Framework/CHIP/MTRBaseDevice_Internal.h b/src/darwin/Framework/CHIP/MTRBaseDevice_Internal.h index f7451454a561db..1b902565727e0b 100644 --- a/src/darwin/Framework/CHIP/MTRBaseDevice_Internal.h +++ b/src/darwin/Framework/CHIP/MTRBaseDevice_Internal.h @@ -54,7 +54,7 @@ NS_ASSUME_NONNULL_BEGIN @property (nonatomic, assign, readonly) chip::NodeId nodeID; /** - * Controllers are created via the MTRControllerFactory object. + * Controllers are created via the MTRDeviceControllerFactory object. */ - (instancetype)init NS_UNAVAILABLE; + (instancetype)new NS_UNAVAILABLE; diff --git a/src/darwin/Framework/CHIP/MTRDeviceController.h b/src/darwin/Framework/CHIP/MTRDeviceController.h index 87dea50b3d1476..605fd385d461b3 100644 --- a/src/darwin/Framework/CHIP/MTRDeviceController.h +++ b/src/darwin/Framework/CHIP/MTRDeviceController.h @@ -30,7 +30,7 @@ typedef void (^MTRDeviceConnectionCallback)(MTRBaseDevice * _Nullable device, NS @interface MTRDeviceController : NSObject -@property (readonly, nonatomic) BOOL isRunning; +@property (readonly, nonatomic, getter=isRunning) BOOL running; /** * Return the Node ID assigned to the controller. Will return nil if the @@ -96,7 +96,7 @@ typedef void (^MTRDeviceConnectionCallback)(MTRBaseDevice * _Nullable device, NS - (BOOL)getBaseDevice:(uint64_t)deviceID queue:(dispatch_queue_t)queue completion:(MTRDeviceConnectionCallback)completion; /** - * Controllers are created via the MTRControllerFactory object. + * Controllers are created via the MTRDeviceControllerFactory object. */ - (instancetype)init NS_UNAVAILABLE; + (instancetype)new NS_UNAVAILABLE; diff --git a/src/darwin/Framework/CHIP/MTRDeviceController.mm b/src/darwin/Framework/CHIP/MTRDeviceController.mm index 04c8441fd7b91a..965c5f3ddcd2cf 100644 --- a/src/darwin/Framework/CHIP/MTRDeviceController.mm +++ b/src/darwin/Framework/CHIP/MTRDeviceController.mm @@ -20,7 +20,7 @@ #import "MTRBaseDevice_Internal.h" #import "MTRCommissioningParameters.h" -#import "MTRControllerFactory_Internal.h" +#import "MTRDeviceControllerFactory_Internal.h" #import "MTRDeviceControllerStartupParams.h" #import "MTRDeviceControllerStartupParams_Internal.h" #import "MTRDevicePairingDelegateBridge.h" @@ -86,14 +86,14 @@ @interface MTRDeviceController () @property (readonly) MTRP256KeypairBridge signingKeypairBridge; @property (readonly) MTRP256KeypairBridge operationalKeypairBridge; @property (readonly) MTRDeviceAttestationDelegateBridge * deviceAttestationDelegateBridge; -@property (readonly) MTRControllerFactory * factory; +@property (readonly) MTRDeviceControllerFactory * factory; @property (readonly) NSMutableDictionary * nodeIDToDeviceMap; @property (readonly) os_unfair_lock deviceMapLock; // protects nodeIDToDeviceMap @end @implementation MTRDeviceController -- (instancetype)initWithFactory:(MTRControllerFactory *)factory queue:(dispatch_queue_t)queue +- (instancetype)initWithFactory:(MTRDeviceControllerFactory *)factory queue:(dispatch_queue_t)queue { if (self = [super init]) { _chipWorkQueue = queue; @@ -139,11 +139,10 @@ - (void)shutdown - (void)cleanupAfterStartup { [_factory controllerShuttingDown:self]; - [self cleanup]; } // Part of cleanupAfterStartup that has to interact with the Matter work queue -// in a very specific way that only MTRControllerFactory knows about. +// in a very specific way that only MTRDeviceControllerFactory knows about. - (void)shutDownCppController { if (_cppCommissioner) { @@ -160,6 +159,11 @@ - (void)shutDownCppController } } +- (void)deinitFromFactory +{ + [self cleanup]; +} + // Clean up any members we might have allocated. - (void)cleanup { diff --git a/src/darwin/Framework/CHIP/MTRControllerFactory.h b/src/darwin/Framework/CHIP/MTRDeviceControllerFactory.h similarity index 66% rename from src/darwin/Framework/CHIP/MTRControllerFactory.h rename to src/darwin/Framework/CHIP/MTRDeviceControllerFactory.h index 3a9a3d609955f2..b8131e2e6fe09c 100644 --- a/src/darwin/Framework/CHIP/MTRControllerFactory.h +++ b/src/darwin/Framework/CHIP/MTRDeviceControllerFactory.h @@ -31,7 +31,7 @@ NS_ASSUME_NONNULL_BEGIN @class MTRDeviceController; @class MTRDeviceControllerStartupParams; -@interface MTRControllerFactoryParams : NSObject +@interface MTRDeviceControllerFactoryParams : NSObject /* * Storage delegate must be provided for correct functioning of Matter * controllers. It is used to store persistent information for the fabrics the @@ -66,44 +66,47 @@ NS_ASSUME_NONNULL_BEGIN * Whether to run a server capable of accepting incoming CASE * connections. Defaults to NO. */ -@property (nonatomic, assign) BOOL startServer; +@property (nonatomic, assign) BOOL shouldStartServer; - (instancetype)init NS_UNAVAILABLE; - (instancetype)initWithStorage:(id)storage; @end -@interface MTRControllerFactory : NSObject +@interface MTRDeviceControllerFactory : NSObject -@property (readonly, nonatomic) BOOL isRunning; - -- (instancetype)init NS_UNAVAILABLE; -+ (instancetype)new NS_UNAVAILABLE; +/** + * If true, the factory is in a state where it can create controllers: + * startControllerFactory has been called, but stopControllerFactory has not been called + * since then. + */ +@property (readonly, nonatomic, getter=isRunning) BOOL running; /** - * Return the single MTRControllerFactory we support existing. It starts off + * Return the single MTRDeviceControllerFactory we support existing. It starts off * in a "not started" state. */ + (instancetype)sharedInstance; /** - * Start the controller factory. Repeated calls to startup without calls to - * shutdown in between are NO-OPs. Use the isRunning property to check whether - * the controller factory needs to be started up. + * Start the controller factory. Repeated calls to startControllerFactory + * without calls to stopControllerFactory in between are NO-OPs. Use the + * isRunning property to check whether the controller factory needs to be + * started up. * * @param[in] startupParams data needed to start up the controller factory. * * @return Whether startup succeded. */ -- (BOOL)startup:(MTRControllerFactoryParams *)startupParams; +- (BOOL)startControllerFactory:(MTRDeviceControllerFactoryParams *)startupParams error:(NSError * __autoreleasing *)error; /** - * Shut down the controller factory. This will shut down any outstanding - * controllers as part of the factory shutdown. + * Stop the controller factory. This will shut down any outstanding + * controllers as part of the factory stopping. * - * Repeated calls to shutdown without calls to startup in between are - * NO-OPs. + * Repeated calls to stopControllerFactory without calls to + * startControllerFactory in between are NO-OPs. */ -- (void)shutdown; +- (void)stopControllerFactory; /** * Create a MTRDeviceController on an existing fabric. Returns nil on failure. @@ -114,7 +117,8 @@ NS_ASSUME_NONNULL_BEGIN * The fabric is identified by the root public key and fabric id in * the startupParams. */ -- (MTRDeviceController * _Nullable)startControllerOnExistingFabric:(MTRDeviceControllerStartupParams *)startupParams; +- (MTRDeviceController * _Nullable)createControllerOnExistingFabric:(MTRDeviceControllerStartupParams *)startupParams + error:(NSError * __autoreleasing *)error; /** * Create a MTRDeviceController on a new fabric. Returns nil on failure. @@ -124,7 +128,11 @@ NS_ASSUME_NONNULL_BEGIN * The fabric is identified by the root public key and fabric id in * the startupParams. */ -- (MTRDeviceController * _Nullable)startControllerOnNewFabric:(MTRDeviceControllerStartupParams *)startupParams; +- (MTRDeviceController * _Nullable)createControllerOnNewFabric:(MTRDeviceControllerStartupParams *)startupParams + error:(NSError * __autoreleasing *)error; + +- (instancetype)init NS_UNAVAILABLE; ++ (instancetype)new NS_UNAVAILABLE; @end diff --git a/src/darwin/Framework/CHIP/MTRControllerFactory.mm b/src/darwin/Framework/CHIP/MTRDeviceControllerFactory.mm similarity index 84% rename from src/darwin/Framework/CHIP/MTRControllerFactory.mm rename to src/darwin/Framework/CHIP/MTRDeviceControllerFactory.mm index 307e2601b931fb..7ee9fb783b4a38 100644 --- a/src/darwin/Framework/CHIP/MTRControllerFactory.mm +++ b/src/darwin/Framework/CHIP/MTRDeviceControllerFactory.mm @@ -14,8 +14,8 @@ * limitations under the License. */ -#import "MTRControllerFactory.h" -#import "MTRControllerFactory_Internal.h" +#import "MTRDeviceControllerFactory.h" +#import "MTRDeviceControllerFactory_Internal.h" #import "MTRAttestationTrustStoreBridge.h" #import "MTRCertificates.h" @@ -24,6 +24,7 @@ #import "MTRDeviceControllerStartupParams.h" #import "MTRDeviceControllerStartupParams_Internal.h" #import "MTRDeviceController_Internal.h" +#import "MTRError_Internal.h" #import "MTRLogging.h" #import "MTRMemory.h" #import "MTROTAProviderDelegateBridge.h" @@ -57,7 +58,7 @@ static NSString * const kErrorCDCertStoreInit = @"Init failure while initializing Certificate Declaration Signing Keys store"; static NSString * const kErrorOtaProviderInit = @"Init failure while creating an OTA provider delegate"; -@interface MTRControllerFactory () +@interface MTRDeviceControllerFactory () @property (atomic, readonly) dispatch_queue_t chipWorkQueue; @property (readonly) DeviceControllerFactory * controllerFactory; @@ -81,15 +82,15 @@ - (BOOL)findMatchingFabric:(FabricTable &)fabricTable - (MTRDeviceController * _Nullable)maybeInitializeOTAProvider:(MTRDeviceController * _Nonnull)controller; @end -@implementation MTRControllerFactory +@implementation MTRDeviceControllerFactory + (instancetype)sharedInstance { - static MTRControllerFactory * factory = nil; + static MTRDeviceControllerFactory * factory = nil; static dispatch_once_t onceToken; dispatch_once(&onceToken, ^{ // initialize the factory. - factory = [[MTRControllerFactory alloc] init]; + factory = [[MTRDeviceControllerFactory alloc] init]; }); return factory; } @@ -100,7 +101,7 @@ - (instancetype)init return nil; } - _isRunning = NO; + _running = NO; _chipWorkQueue = DeviceLayer::PlatformMgrImpl().GetWorkQueue(); _controllerFactory = &DeviceControllerFactory::GetInstance(); [MTRMemory ensureInit]; @@ -132,10 +133,23 @@ - (instancetype)init - (void)dealloc { - [self shutdown]; + [self stopControllerFactory]; [self cleanupInitObjects]; } +- (BOOL)checkIsRunning:(NSError * __autoreleasing *)error +{ + if ([self isRunning]) { + return YES; + } + + if (error != nil) { + *error = [MTRError errorForCHIPErrorCode:CHIP_ERROR_INCORRECT_STATE]; + } + + return NO; +} + - (BOOL)checkForInitError:(BOOL)condition logMsg:(NSString *)logMsg { if (condition) { @@ -200,7 +214,7 @@ - (void)cleanupStartupObjects } } -- (BOOL)startup:(MTRControllerFactoryParams *)startupParams +- (BOOL)startControllerFactory:(MTRDeviceControllerFactoryParams *)startupParams error:(NSError * __autoreleasing *)error; { if ([self isRunning]) { MTR_LOG_DEBUG("Ignoring duplicate call to startup, Matter controller factory already started..."); @@ -209,6 +223,7 @@ - (BOOL)startup:(MTRControllerFactoryParams *)startupParams DeviceLayer::PlatformMgrImpl().StartEventLoopTask(); + __block CHIP_ERROR errorCode = CHIP_NO_ERROR; dispatch_sync(_chipWorkQueue, ^{ if ([self isRunning]) { return; @@ -219,6 +234,7 @@ - (BOOL)startup:(MTRControllerFactoryParams *)startupParams _persistentStorageDelegateBridge = new MTRPersistentStorageDelegateBridge(startupParams.storage); if (_persistentStorageDelegateBridge == nil) { MTR_LOG_ERROR("Error: %@", kErrorPersistentStorageInit); + errorCode = CHIP_ERROR_NO_MEMORY; return; } @@ -226,6 +242,7 @@ - (BOOL)startup:(MTRControllerFactoryParams *)startupParams _otaProviderDelegateBridge = new MTROTAProviderDelegateBridge(startupParams.otaProviderDelegate); if (_otaProviderDelegateBridge == nil) { MTR_LOG_ERROR("Error: %@", kErrorOtaProviderInit); + errorCode = CHIP_ERROR_NO_MEMORY; return; } } @@ -234,10 +251,11 @@ - (BOOL)startup:(MTRControllerFactoryParams *)startupParams _keystore = new PersistentStorageOperationalKeystore(); if (_keystore == nullptr) { MTR_LOG_ERROR("Error: %@", kErrorKeystoreInit); + errorCode = CHIP_ERROR_NO_MEMORY; return; } - CHIP_ERROR errorCode = _keystore->Init(_persistentStorageDelegateBridge); + errorCode = _keystore->Init(_persistentStorageDelegateBridge); if (errorCode != CHIP_NO_ERROR) { MTR_LOG_ERROR("Error: %@", kErrorKeystoreInit); return; @@ -247,6 +265,7 @@ - (BOOL)startup:(MTRControllerFactoryParams *)startupParams _opCertStore = new Credentials::PersistentStorageOpCertStore(); if (_opCertStore == nullptr) { MTR_LOG_ERROR("Error: %@", kErrorCertStoreInit); + errorCode = CHIP_ERROR_NO_MEMORY; return; } @@ -262,6 +281,7 @@ - (BOOL)startup:(MTRControllerFactoryParams *)startupParams _attestationTrustStoreBridge = new MTRAttestationTrustStoreBridge(startupParams.paaCerts); if (_attestationTrustStoreBridge == nullptr) { MTR_LOG_ERROR("Error: %@", kErrorAttestationTrustStoreInit); + errorCode = CHIP_ERROR_NO_MEMORY; return; } trustStore = _attestationTrustStoreBridge; @@ -272,6 +292,7 @@ - (BOOL)startup:(MTRControllerFactoryParams *)startupParams _deviceAttestationVerifier = new Credentials::DefaultDACVerifier(trustStore); if (_deviceAttestationVerifier == nullptr) { MTR_LOG_ERROR("Error: %@", kErrorDACVerifierInit); + errorCode = CHIP_ERROR_NO_MEMORY; return; } @@ -279,6 +300,7 @@ - (BOOL)startup:(MTRControllerFactoryParams *)startupParams auto cdTrustStore = _deviceAttestationVerifier->GetCertificationDeclarationTrustStore(); if (cdTrustStore == nullptr) { MTR_LOG_ERROR("Error: %@", kErrorCDCertStoreInit); + errorCode = CHIP_ERROR_INCORRECT_STATE; return; } @@ -295,7 +317,7 @@ - (BOOL)startup:(MTRControllerFactoryParams *)startupParams if (startupParams.port != nil) { params.listenPort = [startupParams.port unsignedShortValue]; } - if (startupParams.startServer == YES) { + if (startupParams.shouldStartServer == YES) { params.enableServerInteractions = true; } @@ -309,7 +331,7 @@ - (BOOL)startup:(MTRControllerFactoryParams *)startupParams return; } - self->_isRunning = YES; + self->_running = YES; }); // Make sure to stop the event loop again before returning, so we are not running it while we don't have any controllers. @@ -317,12 +339,15 @@ - (BOOL)startup:(MTRControllerFactoryParams *)startupParams if (![self isRunning]) { [self cleanupStartupObjects]; + if (error != nil) { + *error = [MTRError errorForCHIPErrorCode:errorCode]; + } } return [self isRunning]; } -- (void)shutdown +- (void)stopControllerFactory { if (![self isRunning]) { return; @@ -341,12 +366,13 @@ - (void)shutdown // that does not re-create the objects that we create inside init. // Maybe we should be creating them in startup? - _isRunning = NO; + _running = NO; } -- (MTRDeviceController * _Nullable)startControllerOnExistingFabric:(MTRDeviceControllerStartupParams *)startupParams +- (MTRDeviceController * _Nullable)createControllerOnExistingFabric:(MTRDeviceControllerStartupParams *)startupParams + error:(NSError * __autoreleasing *)error { - if (![self isRunning]) { + if (![self checkIsRunning:error]) { MTR_LOG_ERROR("Trying to start controller while Matter controller factory is not running"); return nil; } @@ -355,10 +381,14 @@ - (MTRDeviceController * _Nullable)startControllerOnExistingFabric:(MTRDeviceCon // our fabric table operations there. auto * controller = [self createController]; if (controller == nil) { + if (error != nil) { + *error = [MTRError errorForCHIPErrorCode:CHIP_ERROR_NO_MEMORY]; + } return nil; } __block MTRDeviceControllerStartupParamsInternal * params = nil; + __block CHIP_ERROR fabricError = CHIP_NO_ERROR; // We want the block to end up with just a pointer to the fabric table, // since we know our on-stack instance will outlive the block. FabricTable fabricTableInstance; @@ -368,11 +398,13 @@ - (MTRDeviceController * _Nullable)startControllerOnExistingFabric:(MTRDeviceCon BOOL ok = [self findMatchingFabric:*fabricTable params:startupParams fabric:&fabric]; if (!ok) { MTR_LOG_ERROR("Can't start on existing fabric: fabric matching failed"); + fabricError = CHIP_ERROR_INTERNAL; return; } if (fabric == nullptr) { MTR_LOG_ERROR("Can't start on existing fabric: fabric not found"); + fabricError = CHIP_ERROR_NOT_FOUND; return; } @@ -381,11 +413,13 @@ - (MTRDeviceController * _Nullable)startControllerOnExistingFabric:(MTRDeviceCon if ([existing isRunningOnFabric:fabricTable fabricIndex:fabric->GetFabricIndex() isRunning:&isRunning] != CHIP_NO_ERROR) { MTR_LOG_ERROR("Can't tell what fabric a controller is running on. Not safe to start."); + fabricError = CHIP_ERROR_INTERNAL; return; } if (isRunning) { MTR_LOG_ERROR("Can't start on existing fabric: another controller is running on it"); + fabricError = CHIP_ERROR_INCORRECT_STATE; return; } } @@ -394,22 +428,39 @@ - (MTRDeviceController * _Nullable)startControllerOnExistingFabric:(MTRDeviceCon fabricIndex:fabric->GetFabricIndex() keystore:_keystore params:startupParams]; + if (params == nil) { + fabricError = CHIP_ERROR_NO_MEMORY; + } }); if (params == nil) { [self controllerShuttingDown:controller]; + if (error != nil) { + *error = [MTRError errorForCHIPErrorCode:fabricError]; + } return nil; } BOOL ok = [controller startup:params]; if (ok == NO) { + // TODO: get error from controller's startup. + if (error != nil) { + *error = [MTRError errorForCHIPErrorCode:CHIP_ERROR_INTERNAL]; + } return nil; } - return [self maybeInitializeOTAProvider:controller]; + controller = [self maybeInitializeOTAProvider:controller]; + if (controller == nil) { + if (error != nil) { + *error = [MTRError errorForCHIPErrorCode:CHIP_ERROR_INTERNAL]; + } + } + return controller; } -- (MTRDeviceController * _Nullable)startControllerOnNewFabric:(MTRDeviceControllerStartupParams *)startupParams +- (MTRDeviceController * _Nullable)createControllerOnNewFabric:(MTRDeviceControllerStartupParams *)startupParams + error:(NSError * __autoreleasing *)error { if (![self isRunning]) { MTR_LOG_ERROR("Trying to start controller while Matter controller factory is not running"); @@ -430,10 +481,14 @@ - (MTRDeviceController * _Nullable)startControllerOnNewFabric:(MTRDeviceControll // our fabric table operations there. auto * controller = [self createController]; if (controller == nil) { + if (error != nil) { + *error = [MTRError errorForCHIPErrorCode:CHIP_ERROR_NO_MEMORY]; + } return nil; } __block MTRDeviceControllerStartupParamsInternal * params = nil; + __block CHIP_ERROR fabricError = CHIP_NO_ERROR; // We want the block to end up with just a pointer to the fabric table, // since we know our on-stack instance will outlive the block. FabricTable fabricTableInstance; @@ -443,30 +498,49 @@ - (MTRDeviceController * _Nullable)startControllerOnNewFabric:(MTRDeviceControll BOOL ok = [self findMatchingFabric:*fabricTable params:startupParams fabric:&fabric]; if (!ok) { MTR_LOG_ERROR("Can't start on new fabric: fabric matching failed"); + fabricError = CHIP_ERROR_INTERNAL; return; } if (fabric != nullptr) { MTR_LOG_ERROR("Can't start on new fabric that matches existing fabric"); + fabricError = CHIP_ERROR_INCORRECT_STATE; return; } params = [[MTRDeviceControllerStartupParamsInternal alloc] initForNewFabric:fabricTable keystore:_keystore params:startupParams]; + if (params == nil) { + fabricError = CHIP_ERROR_NO_MEMORY; + } }); if (params == nil) { [self controllerShuttingDown:controller]; + if (error != nil) { + *error = [MTRError errorForCHIPErrorCode:fabricError]; + } return nil; } BOOL ok = [controller startup:params]; if (ok == NO) { + // TODO: get error from controller's startup. + if (error != nil) { + *error = [MTRError errorForCHIPErrorCode:CHIP_ERROR_INTERNAL]; + } return nil; } - return [self maybeInitializeOTAProvider:controller]; + // TODO: Need better error propagation. + controller = [self maybeInitializeOTAProvider:controller]; + if (controller == nil) { + if (error != nil) { + *error = [MTRError errorForCHIPErrorCode:CHIP_ERROR_INTERNAL]; + } + } + return controller; } - (MTRDeviceController * _Nullable)createController @@ -553,7 +627,7 @@ - (MTRDeviceController * _Nullable)maybeInitializeOTAProvider:(MTRDeviceControll @end -@implementation MTRControllerFactory (InternalMethods) +@implementation MTRDeviceControllerFactory (InternalMethods) - (void)controllerShuttingDown:(MTRDeviceController *)controller { @@ -593,6 +667,8 @@ - (void)controllerShuttingDown:(MTRDeviceController *)controller [controller shutDownCppController]; }); } + + [controller deinitFromFactory]; } - (nullable MTRDeviceController *)runningControllerForFabricIndex:(chip::FabricIndex)fabricIndex @@ -618,7 +694,7 @@ - (MTRPersistentStorageDelegateBridge *)storageDelegateBridge @end -@implementation MTRControllerFactoryParams +@implementation MTRDeviceControllerFactoryParams - (instancetype)initWithStorage:(id)storage { @@ -631,7 +707,7 @@ - (instancetype)initWithStorage:(id)storage _paaCerts = nil; _cdCerts = nil; _port = nil; - _startServer = NO; + _shouldStartServer = NO; return self; } diff --git a/src/darwin/Framework/CHIP/MTRControllerFactory_Internal.h b/src/darwin/Framework/CHIP/MTRDeviceControllerFactory_Internal.h similarity index 89% rename from src/darwin/Framework/CHIP/MTRControllerFactory_Internal.h rename to src/darwin/Framework/CHIP/MTRDeviceControllerFactory_Internal.h index e976aea01609af..a92d00b1c1e8b3 100644 --- a/src/darwin/Framework/CHIP/MTRControllerFactory_Internal.h +++ b/src/darwin/Framework/CHIP/MTRDeviceControllerFactory_Internal.h @@ -15,13 +15,13 @@ */ /** - * Parts of MTRControllerFactory that are not part of the framework API. + * Parts of MTRDeviceControllerFactory that are not part of the framework API. * Mostly for use from MTRDeviceController. */ #import -#import "MTRControllerFactory.h" +#import "MTRDeviceControllerFactory.h" #include @@ -38,7 +38,7 @@ namespace Credentials { NS_ASSUME_NONNULL_BEGIN -@interface MTRControllerFactory (InternalMethods) +@interface MTRDeviceControllerFactory (InternalMethods) - (void)controllerShuttingDown:(MTRDeviceController *)controller; diff --git a/src/darwin/Framework/CHIP/MTRDeviceControllerStartupParams.h b/src/darwin/Framework/CHIP/MTRDeviceControllerStartupParams.h index 9f180edf34404e..5fd662143a5749 100644 --- a/src/darwin/Framework/CHIP/MTRDeviceControllerStartupParams.h +++ b/src/darwin/Framework/CHIP/MTRDeviceControllerStartupParams.h @@ -28,9 +28,10 @@ NS_ASSUME_NONNULL_BEGIN * if not using an intermediate CA, the intermediate CA's keypair otherwise. * * Allowed to be nil if this controller will not be issuing operational - * certificates. In that case, the MTRDeviceControllerStartupParams object - * must be initialized using initWithOperationalKeypair (to provide the - * operational credentials for the controller itself). + * certificates. In that case, the MTRDeviceControllerStartupParams object must + * be initialized using + * initWithIPK:operationalKeypair:operationalCertificate:intermediateCertificate:rootCertificate: + * (to provide the operational credentials for the controller itself). */ @property (nonatomic, copy, readonly, nullable) id nocSigner; /** @@ -99,8 +100,6 @@ NS_ASSUME_NONNULL_BEGIN */ @property (nonatomic, copy, nullable) NSNumber * nodeID; -// TODO: Add something here for CATs? - /** * Root certificate, in X.509 DER form, to use. * @@ -198,14 +197,14 @@ NS_ASSUME_NONNULL_BEGIN * * ipk must be 16 bytes in length */ -- (instancetype)initWithSigningKeypair:(id)nocSigner fabricID:(NSNumber *)fabricID ipk:(NSData *)ipk; +- (instancetype)initWithIPK:(NSData *)ipk fabricID:(NSNumber *)fabricID nocSigner:(id)nocSigner; /** * Prepare to initialize a controller with a complete operational certificate * chain. This initialization method should be used when none of the * certificate-signing private keys are available locally. * - * The fabric id and node if to use will be derived from the provided + * The fabric id and node id to use will be derived from the provided * operationalCertificate. * * intermediateCertificate may be nil if operationalCertificate is signed by @@ -213,11 +212,11 @@ NS_ASSUME_NONNULL_BEGIN * * ipk must be 16 bytes in length. */ -- (instancetype)initWithOperationalKeypair:(id)operationalKeypair - operationalCertificate:(MTRCertificateDERBytes *)operationalCertificate - intermediateCertificate:(MTRCertificateDERBytes * _Nullable)intermediateCertificate - rootCertificate:(MTRCertificateDERBytes *)rootCertificate - ipk:(NSData *)ipk; +- (instancetype)initWithIPK:(NSData *)ipk + operationalKeypair:(id)operationalKeypair + operationalCertificate:(MTRCertificateDERBytes *)operationalCertificate + intermediateCertificate:(MTRCertificateDERBytes * _Nullable)intermediateCertificate + rootCertificate:(MTRCertificateDERBytes *)rootCertificate; @end diff --git a/src/darwin/Framework/CHIP/MTRDeviceControllerStartupParams.mm b/src/darwin/Framework/CHIP/MTRDeviceControllerStartupParams.mm index a32f5de46d64e3..22680ecf6d7797 100644 --- a/src/darwin/Framework/CHIP/MTRDeviceControllerStartupParams.mm +++ b/src/darwin/Framework/CHIP/MTRDeviceControllerStartupParams.mm @@ -30,7 +30,7 @@ @implementation MTRDeviceControllerStartupParams -- (instancetype)initWithSigningKeypair:(id)nocSigner fabricID:(NSNumber *)fabricID ipk:(NSData *)ipk +- (instancetype)initWithIPK:(NSData *)ipk fabricID:(NSNumber *)fabricID nocSigner:(id)nocSigner { if (!(self = [super init])) { return nil; @@ -48,11 +48,11 @@ - (instancetype)initWithSigningKeypair:(id)nocSigner fabricID:(NSNum return self; } -- (instancetype)initWithOperationalKeypair:(id)operationalKeypair - operationalCertificate:(MTRCertificateDERBytes *)operationalCertificate - intermediateCertificate:(MTRCertificateDERBytes * _Nullable)intermediateCertificate - rootCertificate:(MTRCertificateDERBytes *)rootCertificate - ipk:(NSData *)ipk +- (instancetype)initWithIPK:(NSData *)ipk + operationalKeypair:(id)operationalKeypair + operationalCertificate:(MTRCertificateDERBytes *)operationalCertificate + intermediateCertificate:(MTRCertificateDERBytes * _Nullable)intermediateCertificate + rootCertificate:(MTRCertificateDERBytes *)rootCertificate { if (!(self = [super init])) { return nil; diff --git a/src/darwin/Framework/CHIP/MTRDeviceControllerStartupParams_Internal.h b/src/darwin/Framework/CHIP/MTRDeviceControllerStartupParams_Internal.h index a448dc0901ab5a..5094ee0290b132 100644 --- a/src/darwin/Framework/CHIP/MTRDeviceControllerStartupParams_Internal.h +++ b/src/darwin/Framework/CHIP/MTRDeviceControllerStartupParams_Internal.h @@ -83,12 +83,12 @@ NS_ASSUME_NONNULL_BEGIN keystore:(chip::Crypto::OperationalKeystore *)keystore params:(MTRDeviceControllerStartupParams *)params; -- (instancetype)initWithSigningKeypair:(id)nocSigner fabricID:(NSNumber *)fabricID ipk:(NSData *)ipk NS_UNAVAILABLE; -- (instancetype)initWithOperationalKeypair:(id)operationalKeypair - operationalCertificate:(MTRCertificateDERBytes *)operationalCertificate - intermediateCertificate:(MTRCertificateDERBytes * _Nullable)intermediateCertificate - rootCertificate:(MTRCertificateDERBytes *)rootCertificate - ipk:(NSData *)ipk NS_UNAVAILABLE; +- (instancetype)initWithIPK:(NSData *)ipk fabricID:(NSNumber *)fabricID nocSigner:(id)nocSigner NS_UNAVAILABLE; +- (instancetype)initWithIPK:(NSData *)ipk + operationalKeypair:(id)operationalKeypair + operationalCertificate:(MTRCertificateDERBytes *)operationalCertificate + intermediateCertificate:(MTRCertificateDERBytes * _Nullable)intermediateCertificate + rootCertificate:(MTRCertificateDERBytes *)rootCertificate NS_UNAVAILABLE; @end NS_ASSUME_NONNULL_END diff --git a/src/darwin/Framework/CHIP/MTRDeviceController_Internal.h b/src/darwin/Framework/CHIP/MTRDeviceController_Internal.h index b6c4d01fa57b20..f61a1b81a4eb53 100644 --- a/src/darwin/Framework/CHIP/MTRDeviceController_Internal.h +++ b/src/darwin/Framework/CHIP/MTRDeviceController_Internal.h @@ -16,7 +16,7 @@ /** * Parts of MTRDeviceController that are not part of the framework API. Mostly - * for use from MTRControllerFactory. + * for use from MTRDeviceControllerFactory. */ #import @@ -31,7 +31,7 @@ #import "MTRDeviceController.h" @class MTRDeviceControllerStartupParamsInternal; -@class MTRControllerFactory; +@class MTRDeviceControllerFactory; @class MTRDevice; namespace chip { @@ -46,17 +46,17 @@ NS_ASSUME_NONNULL_BEGIN @interface MTRDeviceController (InternalMethods) -#pragma mark - MTRControllerFactory methods +#pragma mark - MTRDeviceControllerFactory methods /** * Start a new controller. Returns whether startup succeeded. If this fails, * it guarantees that it has called controllerShuttingDown on the - * MTRControllerFactory. + * MTRDeviceControllerFactory. * * The return value will always match [controller isRunning] for this * controller. * - * Only MTRControllerFactory should be calling this. + * Only MTRDeviceControllerFactory should be calling this. */ - (BOOL)startup:(MTRDeviceControllerStartupParamsInternal *)startupParams; @@ -69,9 +69,9 @@ NS_ASSUME_NONNULL_BEGIN /** * Init a newly created controller. * - * Only MTRControllerFactory should be calling this. + * Only MTRDeviceControllerFactory should be calling this. */ -- (instancetype)initWithFactory:(MTRControllerFactory *)factory queue:(dispatch_queue_t)queue; +- (instancetype)initWithFactory:(MTRDeviceControllerFactory *)factory queue:(dispatch_queue_t)queue; /** * Check whether this controller is running on the given fabric, as represented @@ -82,7 +82,7 @@ NS_ASSUME_NONNULL_BEGIN * Might return failure, in which case we don't know whether it's running on the * given fabric. Otherwise it will set *isRunning to the right boolean value. * - * Only MTRControllerFactory should be calling this. + * Only MTRDeviceControllerFactory should be calling this. */ - (CHIP_ERROR)isRunningOnFabric:(chip::FabricTable *)fabricTable fabricIndex:(chip::FabricIndex)fabricIndex @@ -92,10 +92,19 @@ NS_ASSUME_NONNULL_BEGIN * Shut down the underlying C++ controller. Must be called on the Matter work * queue or after the Matter work queue has been shut down. * - * Only MTRControllerFactory should be calling this. + * Only MTRDeviceControllerFactory should be calling this. */ - (void)shutDownCppController; +/** + * Notification that the MTRDeviceControllerFactory has finished shutting down + * this controller and will not be touching it anymore. This is guaranteed to + * be called after initWithFactory succeeds. + * + * Only MTRDeviceControllerFactory should be calling this. + */ +- (void)deinitFromFactory; + /** * Ensure we have a CASE session to the given node ID and then call the provided * connection callback. This may be called on any queue (including the Matter diff --git a/src/darwin/Framework/CHIP/MTROTAProviderDelegateBridge.mm b/src/darwin/Framework/CHIP/MTROTAProviderDelegateBridge.mm index 2b2234cc011ce4..6c7e668d3ed213 100644 --- a/src/darwin/Framework/CHIP/MTROTAProviderDelegateBridge.mm +++ b/src/darwin/Framework/CHIP/MTROTAProviderDelegateBridge.mm @@ -16,7 +16,7 @@ */ #import "MTROTAProviderDelegateBridge.h" -#import "MTRControllerFactory_Internal.h" +#import "MTRDeviceControllerFactory_Internal.h" #import "NSDataSpanConversion.h" #import "NSStringSpanConversion.h" @@ -149,7 +149,7 @@ CHIP_ERROR OnTransferSessionBegin(TransferSession::OutputEvent & event) }); }; - auto * controller = [[MTRControllerFactory sharedInstance] runningControllerForFabricIndex:mFabricIndex.Value()]; + auto * controller = [[MTRDeviceControllerFactory sharedInstance] runningControllerForFabricIndex:mFabricIndex.Value()]; VerifyOrReturnError(controller != nil, CHIP_ERROR_INCORRECT_STATE); auto nodeId = @(mNodeId.Value()); @@ -177,7 +177,7 @@ CHIP_ERROR OnTransferSessionEnd(TransferSession::OutputEvent & event) error = CHIP_ERROR_INTERNAL; } - auto * controller = [[MTRControllerFactory sharedInstance] runningControllerForFabricIndex:mFabricIndex.Value()]; + auto * controller = [[MTRDeviceControllerFactory sharedInstance] runningControllerForFabricIndex:mFabricIndex.Value()]; VerifyOrReturnError(controller != nil, CHIP_ERROR_INCORRECT_STATE); auto nodeId = @(mNodeId.Value()); @@ -227,7 +227,7 @@ CHIP_ERROR OnBlockQuery(TransferSession::OutputEvent & event) // TODO Handle MaxLength - auto * controller = [[MTRControllerFactory sharedInstance] runningControllerForFabricIndex:mFabricIndex.Value()]; + auto * controller = [[MTRDeviceControllerFactory sharedInstance] runningControllerForFabricIndex:mFabricIndex.Value()]; VerifyOrReturnError(controller != nil, CHIP_ERROR_INCORRECT_STATE); auto nodeId = @(mNodeId.Value()); @@ -368,7 +368,7 @@ bool GetPeerNodeInfo(CommandHandler * commandHandler, const ConcreteCommandPath } auto * controller = - [[MTRControllerFactory sharedInstance] runningControllerForFabricIndex:commandHandler->GetAccessingFabricIndex()]; + [[MTRDeviceControllerFactory sharedInstance] runningControllerForFabricIndex:commandHandler->GetAccessingFabricIndex()]; if (controller == nil) { commandHandler->AddStatus(commandPath, Status::Failure); return false; diff --git a/src/darwin/Framework/CHIP/Matter.h b/src/darwin/Framework/CHIP/Matter.h index 2f35ef004b5cf9..81c89158317551 100644 --- a/src/darwin/Framework/CHIP/Matter.h +++ b/src/darwin/Framework/CHIP/Matter.h @@ -29,11 +29,11 @@ #import #import #import -#import #import #import #import #import +#import #import #import #import diff --git a/src/darwin/Framework/CHIPTests/MTRControllerTests.m b/src/darwin/Framework/CHIPTests/MTRControllerTests.m index 9a0a551ab34eb5..6f6984403b0429 100644 --- a/src/darwin/Framework/CHIPTests/MTRControllerTests.m +++ b/src/darwin/Framework/CHIPTests/MTRControllerTests.m @@ -37,47 +37,45 @@ @implementation MTRControllerTests - (void)testFactoryLifecycle { - __auto_type * factory = [MTRControllerFactory sharedInstance]; + __auto_type * factory = [MTRDeviceControllerFactory sharedInstance]; XCTAssertNotNil(factory); XCTAssertFalse([factory isRunning]); __auto_type * storage = [[MTRTestStorage alloc] init]; - __auto_type * factoryParams = [[MTRControllerFactoryParams alloc] initWithStorage:storage]; - XCTAssertTrue([factory startup:factoryParams]); + __auto_type * factoryParams = [[MTRDeviceControllerFactoryParams alloc] initWithStorage:storage]; + XCTAssertTrue([factory startControllerFactory:factoryParams error:nil]); XCTAssertTrue([factory isRunning]); - [factory shutdown]; + [factory stopControllerFactory]; XCTAssertFalse([factory isRunning]); // Now try to restart the factory. - XCTAssertTrue([factory startup:factoryParams]); + XCTAssertTrue([factory startControllerFactory:factoryParams error:nil]); XCTAssertTrue([factory isRunning]); - [factory shutdown]; + [factory stopControllerFactory]; XCTAssertFalse([factory isRunning]); } - (void)testControllerLifecycle { - __auto_type * factory = [MTRControllerFactory sharedInstance]; + __auto_type * factory = [MTRDeviceControllerFactory sharedInstance]; XCTAssertNotNil(factory); __auto_type * storage = [[MTRTestStorage alloc] init]; - __auto_type * factoryParams = [[MTRControllerFactoryParams alloc] initWithStorage:storage]; - XCTAssertTrue([factory startup:factoryParams]); + __auto_type * factoryParams = [[MTRDeviceControllerFactoryParams alloc] initWithStorage:storage]; + XCTAssertTrue([factory startControllerFactory:factoryParams error:nil]); XCTAssertTrue([factory isRunning]); __auto_type * testKeys = [[MTRTestKeys alloc] init]; XCTAssertNotNil(testKeys); - __auto_type * params = [[MTRDeviceControllerStartupParams alloc] initWithSigningKeypair:testKeys - fabricID:@(1) - ipk:testKeys.ipk]; + __auto_type * params = [[MTRDeviceControllerStartupParams alloc] initWithIPK:testKeys.ipk fabricID:@(1) nocSigner:testKeys]; XCTAssertNotNil(params); params.vendorID = @(kTestVendorId); - MTRDeviceController * controller = [factory startControllerOnNewFabric:params]; + MTRDeviceController * controller = [factory createControllerOnNewFabric:params error:nil]; XCTAssertNotNil(controller); XCTAssertTrue([controller isRunning]); @@ -85,7 +83,7 @@ - (void)testControllerLifecycle XCTAssertFalse([controller isRunning]); // now try to restart the controller - controller = [factory startControllerOnExistingFabric:params]; + controller = [factory createControllerOnExistingFabric:params error:nil]; XCTAssertNotNil(controller); XCTAssertTrue([controller isRunning]); @@ -94,158 +92,148 @@ - (void)testControllerLifecycle // now try to restart the controller without providing a vendor id. params.vendorID = nil; - controller = [factory startControllerOnExistingFabric:params]; + controller = [factory createControllerOnExistingFabric:params error:nil]; XCTAssertNotNil(controller); XCTAssertTrue([controller isRunning]); [controller shutdown]; XCTAssertFalse([controller isRunning]); - [factory shutdown]; + [factory stopControllerFactory]; XCTAssertFalse([factory isRunning]); } - (void)testFactoryShutdownShutsDownController { - __auto_type * factory = [MTRControllerFactory sharedInstance]; + __auto_type * factory = [MTRDeviceControllerFactory sharedInstance]; XCTAssertNotNil(factory); __auto_type * storage = [[MTRTestStorage alloc] init]; - __auto_type * factoryParams = [[MTRControllerFactoryParams alloc] initWithStorage:storage]; - XCTAssertTrue([factory startup:factoryParams]); + __auto_type * factoryParams = [[MTRDeviceControllerFactoryParams alloc] initWithStorage:storage]; + XCTAssertTrue([factory startControllerFactory:factoryParams error:nil]); XCTAssertTrue([factory isRunning]); __auto_type * testKeys = [[MTRTestKeys alloc] init]; XCTAssertNotNil(testKeys); - __auto_type * params = [[MTRDeviceControllerStartupParams alloc] initWithSigningKeypair:testKeys - fabricID:@(1) - ipk:testKeys.ipk]; + __auto_type * params = [[MTRDeviceControllerStartupParams alloc] initWithIPK:testKeys.ipk fabricID:@(1) nocSigner:testKeys]; XCTAssertNotNil(params); params.vendorID = @(kTestVendorId); - MTRDeviceController * controller = [factory startControllerOnNewFabric:params]; + MTRDeviceController * controller = [factory createControllerOnNewFabric:params error:nil]; XCTAssertNotNil(controller); XCTAssertTrue([controller isRunning]); - [factory shutdown]; + [factory stopControllerFactory]; XCTAssertFalse([factory isRunning]); XCTAssertFalse([controller isRunning]); } - (void)testControllerMultipleShutdown { - __auto_type * factory = [MTRControllerFactory sharedInstance]; + __auto_type * factory = [MTRDeviceControllerFactory sharedInstance]; XCTAssertNotNil(factory); __auto_type * storage = [[MTRTestStorage alloc] init]; - __auto_type * factoryParams = [[MTRControllerFactoryParams alloc] initWithStorage:storage]; - XCTAssertTrue([factory startup:factoryParams]); + __auto_type * factoryParams = [[MTRDeviceControllerFactoryParams alloc] initWithStorage:storage]; + XCTAssertTrue([factory startControllerFactory:factoryParams error:nil]); XCTAssertTrue([factory isRunning]); __auto_type * testKeys = [[MTRTestKeys alloc] init]; XCTAssertNotNil(testKeys); - __auto_type * params = [[MTRDeviceControllerStartupParams alloc] initWithSigningKeypair:testKeys - fabricID:@(1) - ipk:testKeys.ipk]; + __auto_type * params = [[MTRDeviceControllerStartupParams alloc] initWithIPK:testKeys.ipk fabricID:@(1) nocSigner:testKeys]; XCTAssertNotNil(params); params.vendorID = @(kTestVendorId); - MTRDeviceController * controller = [factory startControllerOnNewFabric:params]; + MTRDeviceController * controller = [factory createControllerOnNewFabric:params error:nil]; XCTAssertTrue([controller isRunning]); for (int i = 0; i < 5; i++) { [controller shutdown]; XCTAssertFalse([controller isRunning]); } - [factory shutdown]; + [factory stopControllerFactory]; XCTAssertFalse([factory isRunning]); } - (void)testControllerWithOTAProviderDelegate { - __auto_type * factory = [MTRControllerFactory sharedInstance]; + __auto_type * factory = [MTRDeviceControllerFactory sharedInstance]; XCTAssertNotNil(factory); __auto_type * otaProvider = [[MTRTestOTAProvider alloc] init]; __auto_type * storage = [[MTRTestStorage alloc] init]; - __auto_type * factoryParams = [[MTRControllerFactoryParams alloc] initWithStorage:storage]; + __auto_type * factoryParams = [[MTRDeviceControllerFactoryParams alloc] initWithStorage:storage]; factoryParams.otaProviderDelegate = otaProvider; - XCTAssertTrue([factory startup:factoryParams]); + XCTAssertTrue([factory startControllerFactory:factoryParams error:nil]); XCTAssertTrue([factory isRunning]); __auto_type * testKeys = [[MTRTestKeys alloc] init]; XCTAssertNotNil(testKeys); - __auto_type * params = [[MTRDeviceControllerStartupParams alloc] initWithSigningKeypair:testKeys - fabricID:@(1) - ipk:testKeys.ipk]; + __auto_type * params = [[MTRDeviceControllerStartupParams alloc] initWithIPK:testKeys.ipk fabricID:@(1) nocSigner:testKeys]; XCTAssertNotNil(params); params.vendorID = @(kTestVendorId); - MTRDeviceController * controller = [factory startControllerOnNewFabric:params]; + MTRDeviceController * controller = [factory createControllerOnNewFabric:params error:nil]; XCTAssertTrue([controller isRunning]); [controller shutdown]; // OTA Provider depends on the system state maintained by CHIPDeviceControllerFactory that is destroyed when // the controller count goes down to 0. Make sure that a new controller can still be started successfully onto the // same fabric. - MTRDeviceController * controller2 = [factory startControllerOnExistingFabric:params]; + MTRDeviceController * controller2 = [factory createControllerOnExistingFabric:params error:nil]; XCTAssertTrue([controller2 isRunning]); [controller2 shutdown]; // Check that a new controller can be started on a different fabric too. - __auto_type * params2 = [[MTRDeviceControllerStartupParams alloc] initWithSigningKeypair:testKeys - fabricID:@(2) - ipk:testKeys.ipk]; + __auto_type * params2 = [[MTRDeviceControllerStartupParams alloc] initWithIPK:testKeys.ipk fabricID:@(2) nocSigner:testKeys]; XCTAssertNotNil(params2); params2.vendorID = @(kTestVendorId); - MTRDeviceController * controller3 = [factory startControllerOnNewFabric:params2]; + MTRDeviceController * controller3 = [factory createControllerOnNewFabric:params2 error:nil]; XCTAssertTrue([controller3 isRunning]); [controller3 shutdown]; // Stop the factory, start it up again and create a controller to ensure that no dead state from the previous // ota provider delegate is staying around. - [factory shutdown]; + [factory stopControllerFactory]; XCTAssertFalse([factory isRunning]); - XCTAssertTrue([factory startup:factoryParams]); + XCTAssertTrue([factory startControllerFactory:factoryParams error:nil]); XCTAssertTrue([factory isRunning]); - MTRDeviceController * controller4 = [factory startControllerOnExistingFabric:params2]; + MTRDeviceController * controller4 = [factory createControllerOnExistingFabric:params2 error:nil]; XCTAssertTrue([controller4 isRunning]); [controller4 shutdown]; - [factory shutdown]; + [factory stopControllerFactory]; XCTAssertFalse([factory isRunning]); } - (void)testControllerInvalidAccess { - __auto_type * factory = [MTRControllerFactory sharedInstance]; + __auto_type * factory = [MTRDeviceControllerFactory sharedInstance]; XCTAssertNotNil(factory); __auto_type * storage = [[MTRTestStorage alloc] init]; - __auto_type * factoryParams = [[MTRControllerFactoryParams alloc] initWithStorage:storage]; - XCTAssertTrue([factory startup:factoryParams]); + __auto_type * factoryParams = [[MTRDeviceControllerFactoryParams alloc] initWithStorage:storage]; + XCTAssertTrue([factory startControllerFactory:factoryParams error:nil]); XCTAssertTrue([factory isRunning]); __auto_type * testKeys = [[MTRTestKeys alloc] init]; XCTAssertNotNil(testKeys); - __auto_type * params = [[MTRDeviceControllerStartupParams alloc] initWithSigningKeypair:testKeys - fabricID:@(1) - ipk:testKeys.ipk]; + __auto_type * params = [[MTRDeviceControllerStartupParams alloc] initWithIPK:testKeys.ipk fabricID:@(1) nocSigner:testKeys]; XCTAssertNotNil(params); params.vendorID = @(kTestVendorId); - MTRDeviceController * controller = [factory startControllerOnNewFabric:params]; + MTRDeviceController * controller = [factory createControllerOnNewFabric:params error:nil]; XCTAssertTrue([controller isRunning]); [controller shutdown]; @@ -256,31 +244,29 @@ - (void)testControllerInvalidAccess XCTAssertEqual(error.code, MTRErrorCodeInvalidState); }]); - [factory shutdown]; + [factory stopControllerFactory]; XCTAssertFalse([factory isRunning]); } - (void)testControllerNewFabricMatchesOldFabric { - __auto_type * factory = [MTRControllerFactory sharedInstance]; + __auto_type * factory = [MTRDeviceControllerFactory sharedInstance]; XCTAssertNotNil(factory); __auto_type * storage = [[MTRTestStorage alloc] init]; - __auto_type * factoryParams = [[MTRControllerFactoryParams alloc] initWithStorage:storage]; - XCTAssertTrue([factory startup:factoryParams]); + __auto_type * factoryParams = [[MTRDeviceControllerFactoryParams alloc] initWithStorage:storage]; + XCTAssertTrue([factory startControllerFactory:factoryParams error:nil]); XCTAssertTrue([factory isRunning]); __auto_type * testKeys = [[MTRTestKeys alloc] init]; XCTAssertNotNil(testKeys); - __auto_type * params = [[MTRDeviceControllerStartupParams alloc] initWithSigningKeypair:testKeys - fabricID:@(1) - ipk:testKeys.ipk]; + __auto_type * params = [[MTRDeviceControllerStartupParams alloc] initWithIPK:testKeys.ipk fabricID:@(1) nocSigner:testKeys]; XCTAssertNotNil(params); params.vendorID = @(kTestVendorId); - MTRDeviceController * controller = [factory startControllerOnNewFabric:params]; + MTRDeviceController * controller = [factory createControllerOnNewFabric:params error:nil]; XCTAssertNotNil(controller); XCTAssertTrue([controller isRunning]); @@ -289,88 +275,82 @@ - (void)testControllerNewFabricMatchesOldFabric // now try to start a new controller on a new fabric but using the // same params; this should fail. - XCTAssertNil([factory startControllerOnNewFabric:params]); + XCTAssertNil([factory createControllerOnNewFabric:params error:nil]); XCTAssertFalse([controller isRunning]); - [factory shutdown]; + [factory stopControllerFactory]; XCTAssertFalse([factory isRunning]); } - (void)testControllerExistingFabricMatchesRunningController { - __auto_type * factory = [MTRControllerFactory sharedInstance]; + __auto_type * factory = [MTRDeviceControllerFactory sharedInstance]; XCTAssertNotNil(factory); __auto_type * storage = [[MTRTestStorage alloc] init]; - __auto_type * factoryParams = [[MTRControllerFactoryParams alloc] initWithStorage:storage]; - XCTAssertTrue([factory startup:factoryParams]); + __auto_type * factoryParams = [[MTRDeviceControllerFactoryParams alloc] initWithStorage:storage]; + XCTAssertTrue([factory startControllerFactory:factoryParams error:nil]); XCTAssertTrue([factory isRunning]); __auto_type * testKeys = [[MTRTestKeys alloc] init]; XCTAssertNotNil(testKeys); - __auto_type * params = [[MTRDeviceControllerStartupParams alloc] initWithSigningKeypair:testKeys - fabricID:@(1) - ipk:testKeys.ipk]; + __auto_type * params = [[MTRDeviceControllerStartupParams alloc] initWithIPK:testKeys.ipk fabricID:@(1) nocSigner:testKeys]; XCTAssertNotNil(params); params.vendorID = @(kTestVendorId); - MTRDeviceController * controller = [factory startControllerOnNewFabric:params]; + MTRDeviceController * controller = [factory createControllerOnNewFabric:params error:nil]; XCTAssertNotNil(controller); XCTAssertTrue([controller isRunning]); // Now try to start a new controller on the same fabric. This should fail. - XCTAssertNil([factory startControllerOnExistingFabric:params]); + XCTAssertNil([factory createControllerOnExistingFabric:params error:nil]); XCTAssertTrue([controller isRunning]); [controller shutdown]; XCTAssertFalse([controller isRunning]); - [factory shutdown]; + [factory stopControllerFactory]; XCTAssertFalse([factory isRunning]); } - (void)testControllerStartControllersOnTwoFabricIds { - __auto_type * factory = [MTRControllerFactory sharedInstance]; + __auto_type * factory = [MTRDeviceControllerFactory sharedInstance]; XCTAssertNotNil(factory); __auto_type * storage = [[MTRTestStorage alloc] init]; - __auto_type * factoryParams = [[MTRControllerFactoryParams alloc] initWithStorage:storage]; - XCTAssertTrue([factory startup:factoryParams]); + __auto_type * factoryParams = [[MTRDeviceControllerFactoryParams alloc] initWithStorage:storage]; + XCTAssertTrue([factory startControllerFactory:factoryParams error:nil]); XCTAssertTrue([factory isRunning]); __auto_type * testKeys = [[MTRTestKeys alloc] init]; XCTAssertNotNil(testKeys); - __auto_type * params1 = [[MTRDeviceControllerStartupParams alloc] initWithSigningKeypair:testKeys - fabricID:@(1) - ipk:testKeys.ipk]; + __auto_type * params1 = [[MTRDeviceControllerStartupParams alloc] initWithIPK:testKeys.ipk fabricID:@(1) nocSigner:testKeys]; XCTAssertNotNil(params1); params1.vendorID = @(kTestVendorId); - MTRDeviceController * controller1 = [factory startControllerOnNewFabric:params1]; + MTRDeviceController * controller1 = [factory createControllerOnNewFabric:params1 error:nil]; XCTAssertNotNil(controller1); XCTAssertTrue([controller1 isRunning]); // Now try to start a new controller with the same root but a // different fabric id. - __auto_type * params2 = [[MTRDeviceControllerStartupParams alloc] initWithSigningKeypair:testKeys - fabricID:@(2) - ipk:testKeys.ipk]; + __auto_type * params2 = [[MTRDeviceControllerStartupParams alloc] initWithIPK:testKeys.ipk fabricID:@(2) nocSigner:testKeys]; XCTAssertNotNil(params2); params2.vendorID = @(kTestVendorId); - MTRDeviceController * controller2 = [factory startControllerOnNewFabric:params2]; + MTRDeviceController * controller2 = [factory createControllerOnNewFabric:params2 error:nil]; XCTAssertNotNil(controller2); XCTAssertTrue([controller2 isRunning]); - XCTAssertNil([factory startControllerOnExistingFabric:params2]); + XCTAssertNil([factory createControllerOnExistingFabric:params2 error:nil]); [controller1 shutdown]; XCTAssertFalse([controller1 isRunning]); @@ -378,18 +358,18 @@ - (void)testControllerStartControllersOnTwoFabricIds [controller2 shutdown]; XCTAssertFalse([controller2 isRunning]); - [factory shutdown]; + [factory stopControllerFactory]; XCTAssertFalse([factory isRunning]); } - (void)testControllerStartControllerSameFabricWrongSubject { - __auto_type * factory = [MTRControllerFactory sharedInstance]; + __auto_type * factory = [MTRDeviceControllerFactory sharedInstance]; XCTAssertNotNil(factory); __auto_type * storage = [[MTRTestStorage alloc] init]; - __auto_type * factoryParams = [[MTRControllerFactoryParams alloc] initWithStorage:storage]; - XCTAssertTrue([factory startup:factoryParams]); + __auto_type * factoryParams = [[MTRDeviceControllerFactoryParams alloc] initWithStorage:storage]; + XCTAssertTrue([factory startControllerFactory:factoryParams error:nil]); XCTAssertTrue([factory isRunning]); __auto_type * testKeys = [[MTRTestKeys alloc] init]; @@ -404,15 +384,13 @@ - (void)testControllerStartControllerSameFabricWrongSubject __auto_type * root3 = [MTRCertificates createRootCertificate:testKeys issuerID:@2 fabricID:@1 error:nil]; XCTAssertNotNil(root3); - __auto_type * params = [[MTRDeviceControllerStartupParams alloc] initWithSigningKeypair:testKeys - fabricID:@(1) - ipk:testKeys.ipk]; + __auto_type * params = [[MTRDeviceControllerStartupParams alloc] initWithIPK:testKeys.ipk fabricID:@(1) nocSigner:testKeys]; XCTAssertNotNil(params); params.vendorID = @(kTestVendorId); params.rootCertificate = root1; - MTRDeviceController * controller = [factory startControllerOnNewFabric:params]; + MTRDeviceController * controller = [factory createControllerOnNewFabric:params error:nil]; XCTAssertNotNil(controller); XCTAssertTrue([controller isRunning]); @@ -422,7 +400,7 @@ - (void)testControllerStartControllerSameFabricWrongSubject // Now try to start a new controller on the same fabric with what should be // a compatible root certificate. params.rootCertificate = root2; - controller = [factory startControllerOnExistingFabric:params]; + controller = [factory createControllerOnExistingFabric:params error:nil]; XCTAssertNotNil(controller); XCTAssertTrue([controller isRunning]); @@ -436,7 +414,7 @@ - (void)testControllerStartControllerSameFabricWrongSubject // reasons, including our existing operational certificate not matching this // root. params.rootCertificate = root3; - controller = [factory startControllerOnExistingFabric:params]; + controller = [factory createControllerOnExistingFabric:params error:nil]; XCTAssertNil(controller); // Now try to start a new controller on the same fabric but with a root @@ -445,21 +423,21 @@ - (void)testControllerStartControllerSameFabricWrongSubject // the fabric would change if we allowed this. params.rootCertificate = root3; params.nodeID = nodeId; - controller = [factory startControllerOnExistingFabric:params]; + controller = [factory createControllerOnExistingFabric:params error:nil]; XCTAssertNil(controller); - [factory shutdown]; + [factory stopControllerFactory]; XCTAssertFalse([factory isRunning]); } - (void)testControllerFabricIdRootCertMismatch { - __auto_type * factory = [MTRControllerFactory sharedInstance]; + __auto_type * factory = [MTRDeviceControllerFactory sharedInstance]; XCTAssertNotNil(factory); __auto_type * storage = [[MTRTestStorage alloc] init]; - __auto_type * factoryParams = [[MTRControllerFactoryParams alloc] initWithStorage:storage]; - XCTAssertTrue([factory startup:factoryParams]); + __auto_type * factoryParams = [[MTRDeviceControllerFactoryParams alloc] initWithStorage:storage]; + XCTAssertTrue([factory startControllerFactory:factoryParams error:nil]); XCTAssertTrue([factory isRunning]); __auto_type * testKeys = [[MTRTestKeys alloc] init]; @@ -471,21 +449,19 @@ - (void)testControllerFabricIdRootCertMismatch __auto_type * root2 = [MTRCertificates createRootCertificate:testKeys issuerID:@1 fabricID:@2 error:nil]; XCTAssertNotNil(root2); - __auto_type * params = [[MTRDeviceControllerStartupParams alloc] initWithSigningKeypair:testKeys - fabricID:@(1) - ipk:testKeys.ipk]; + __auto_type * params = [[MTRDeviceControllerStartupParams alloc] initWithIPK:testKeys.ipk fabricID:@(1) nocSigner:testKeys]; XCTAssertNotNil(params); params.vendorID = @(kTestVendorId); // Try to start controller when fabric id in root cert subject does not match provided fabric id. params.rootCertificate = root2; - MTRDeviceController * controller = [factory startControllerOnNewFabric:params]; + MTRDeviceController * controller = [factory createControllerOnNewFabric:params error:nil]; XCTAssertNil(controller); // Start controller when the fabric ids do match. params.rootCertificate = root1; - controller = [factory startControllerOnNewFabric:params]; + controller = [factory createControllerOnNewFabric:params error:nil]; XCTAssertNotNil(controller); XCTAssertTrue([controller isRunning]); @@ -493,7 +469,7 @@ - (void)testControllerFabricIdRootCertMismatch XCTAssertFalse([controller isRunning]); // Re-start controller on the new fabric. - controller = [factory startControllerOnExistingFabric:params]; + controller = [factory createControllerOnExistingFabric:params error:nil]; XCTAssertNotNil(controller); XCTAssertTrue([controller isRunning]); @@ -503,21 +479,21 @@ - (void)testControllerFabricIdRootCertMismatch // Now try to restart controller on the fabric, but with the wrong fabric id // in the root cert. params.rootCertificate = root2; - controller = [factory startControllerOnExistingFabric:params]; + controller = [factory createControllerOnExistingFabric:params error:nil]; XCTAssertNil(controller); - [factory shutdown]; + [factory stopControllerFactory]; XCTAssertFalse([factory isRunning]); } - (void)testControllerSignerDoesNotMatchRoot { - __auto_type * factory = [MTRControllerFactory sharedInstance]; + __auto_type * factory = [MTRDeviceControllerFactory sharedInstance]; XCTAssertNotNil(factory); __auto_type * storage = [[MTRTestStorage alloc] init]; - __auto_type * factoryParams = [[MTRControllerFactoryParams alloc] initWithStorage:storage]; - XCTAssertTrue([factory startup:factoryParams]); + __auto_type * factoryParams = [[MTRDeviceControllerFactoryParams alloc] initWithStorage:storage]; + XCTAssertTrue([factory startControllerFactory:factoryParams error:nil]); XCTAssertTrue([factory isRunning]); __auto_type * rootKeys = [[MTRTestKeys alloc] init]; @@ -529,9 +505,7 @@ - (void)testControllerSignerDoesNotMatchRoot __auto_type * root = [MTRCertificates createRootCertificate:rootKeys issuerID:nil fabricID:nil error:nil]; XCTAssertNotNil(root); - __auto_type * params = [[MTRDeviceControllerStartupParams alloc] initWithSigningKeypair:signerKeys - fabricID:@(1) - ipk:rootKeys.ipk]; + __auto_type * params = [[MTRDeviceControllerStartupParams alloc] initWithIPK:rootKeys.ipk fabricID:@(1) nocSigner:signerKeys]; XCTAssertNotNil(params); params.vendorID = @(kTestVendorId); @@ -539,21 +513,21 @@ - (void)testControllerSignerDoesNotMatchRoot // Try to start controller when there is no ICA and root cert does not match signing key. params.rootCertificate = root; - MTRDeviceController * controller = [factory startControllerOnNewFabric:params]; + MTRDeviceController * controller = [factory createControllerOnNewFabric:params error:nil]; XCTAssertNil(controller); - [factory shutdown]; + [factory stopControllerFactory]; XCTAssertFalse([factory isRunning]); } - (void)testControllerSignerKeyWithIntermediate { - __auto_type * factory = [MTRControllerFactory sharedInstance]; + __auto_type * factory = [MTRDeviceControllerFactory sharedInstance]; XCTAssertNotNil(factory); __auto_type * storage = [[MTRTestStorage alloc] init]; - __auto_type * factoryParams = [[MTRControllerFactoryParams alloc] initWithStorage:storage]; - XCTAssertTrue([factory startup:factoryParams]); + __auto_type * factoryParams = [[MTRDeviceControllerFactoryParams alloc] initWithStorage:storage]; + XCTAssertTrue([factory startControllerFactory:factoryParams error:nil]); XCTAssertTrue([factory isRunning]); __auto_type * rootKeys = [[MTRTestKeys alloc] init]; @@ -573,9 +547,7 @@ - (void)testControllerSignerKeyWithIntermediate error:nil]; XCTAssertNotNil(intermediate); - __auto_type * params = [[MTRDeviceControllerStartupParams alloc] initWithSigningKeypair:rootKeys - fabricID:@(1) - ipk:rootKeys.ipk]; + __auto_type * params = [[MTRDeviceControllerStartupParams alloc] initWithIPK:rootKeys.ipk fabricID:@(1) nocSigner:rootKeys]; XCTAssertNotNil(params); params.vendorID = @(kTestVendorId); @@ -583,97 +555,91 @@ - (void)testControllerSignerKeyWithIntermediate // Try to start controller when there is an ICA and the ICA cert does not match signing key. params.rootCertificate = root; params.intermediateCertificate = intermediate; - MTRDeviceController * controller = [factory startControllerOnNewFabric:params]; + MTRDeviceController * controller = [factory createControllerOnNewFabric:params error:nil]; XCTAssertNil(controller); // Now start controller with the signing key matching the intermediate cert. - params = [[MTRDeviceControllerStartupParams alloc] initWithSigningKeypair:intermediateKeys fabricID:@(1) ipk:rootKeys.ipk]; + params = [[MTRDeviceControllerStartupParams alloc] initWithIPK:rootKeys.ipk fabricID:@(1) nocSigner:intermediateKeys]; params.vendorID = @(kTestVendorId); params.rootCertificate = root; params.intermediateCertificate = intermediate; - controller = [factory startControllerOnNewFabric:params]; + controller = [factory createControllerOnNewFabric:params error:nil]; XCTAssertNotNil(controller); XCTAssertTrue([controller isRunning]); [controller shutdown]; XCTAssertFalse([controller isRunning]); - [factory shutdown]; + [factory stopControllerFactory]; XCTAssertFalse([factory isRunning]); } - (void)testControllerStartupParamsInvalidFabric { - __auto_type * factory = [MTRControllerFactory sharedInstance]; + __auto_type * factory = [MTRDeviceControllerFactory sharedInstance]; XCTAssertNotNil(factory); __auto_type * storage = [[MTRTestStorage alloc] init]; - __auto_type * factoryParams = [[MTRControllerFactoryParams alloc] initWithStorage:storage]; - XCTAssertTrue([factory startup:factoryParams]); + __auto_type * factoryParams = [[MTRDeviceControllerFactoryParams alloc] initWithStorage:storage]; + XCTAssertTrue([factory startControllerFactory:factoryParams error:nil]); XCTAssertTrue([factory isRunning]); __auto_type * rootKeys = [[MTRTestKeys alloc] init]; XCTAssertNotNil(rootKeys); // Invalid fabric ID. - __auto_type * params = [[MTRDeviceControllerStartupParams alloc] initWithSigningKeypair:rootKeys - fabricID:@(0) - ipk:rootKeys.ipk]; + __auto_type * params = [[MTRDeviceControllerStartupParams alloc] initWithIPK:rootKeys.ipk fabricID:@(0) nocSigner:rootKeys]; XCTAssertNil(params); - [factory shutdown]; + [factory stopControllerFactory]; XCTAssertFalse([factory isRunning]); } - (void)testControllerStartupParamsInvalidVendor { - __auto_type * factory = [MTRControllerFactory sharedInstance]; + __auto_type * factory = [MTRDeviceControllerFactory sharedInstance]; XCTAssertNotNil(factory); __auto_type * storage = [[MTRTestStorage alloc] init]; - __auto_type * factoryParams = [[MTRControllerFactoryParams alloc] initWithStorage:storage]; - XCTAssertTrue([factory startup:factoryParams]); + __auto_type * factoryParams = [[MTRDeviceControllerFactoryParams alloc] initWithStorage:storage]; + XCTAssertTrue([factory startControllerFactory:factoryParams error:nil]); XCTAssertTrue([factory isRunning]); __auto_type * rootKeys = [[MTRTestKeys alloc] init]; XCTAssertNotNil(rootKeys); - __auto_type * params = [[MTRDeviceControllerStartupParams alloc] initWithSigningKeypair:rootKeys - fabricID:@(1) - ipk:rootKeys.ipk]; + __auto_type * params = [[MTRDeviceControllerStartupParams alloc] initWithIPK:rootKeys.ipk fabricID:@(1) nocSigner:rootKeys]; XCTAssertNotNil(params); // Invalid vendor ID ("standard"). params.vendorID = @(0); - MTRDeviceController * controller = [factory startControllerOnNewFabric:params]; + MTRDeviceController * controller = [factory createControllerOnNewFabric:params error:nil]; XCTAssertNil(controller); - [factory shutdown]; + [factory stopControllerFactory]; XCTAssertFalse([factory isRunning]); } - (void)testControllerStartupNodeIdPreserved { - __auto_type * factory = [MTRControllerFactory sharedInstance]; + __auto_type * factory = [MTRDeviceControllerFactory sharedInstance]; XCTAssertNotNil(factory); __auto_type * storage = [[MTRTestStorage alloc] init]; - __auto_type * factoryParams = [[MTRControllerFactoryParams alloc] initWithStorage:storage]; - XCTAssertTrue([factory startup:factoryParams]); + __auto_type * factoryParams = [[MTRDeviceControllerFactoryParams alloc] initWithStorage:storage]; + XCTAssertTrue([factory startControllerFactory:factoryParams error:nil]); XCTAssertTrue([factory isRunning]); __auto_type * rootKeys = [[MTRTestKeys alloc] init]; XCTAssertNotNil(rootKeys); - __auto_type * params = [[MTRDeviceControllerStartupParams alloc] initWithSigningKeypair:rootKeys - fabricID:@(1) - ipk:rootKeys.ipk]; + __auto_type * params = [[MTRDeviceControllerStartupParams alloc] initWithIPK:rootKeys.ipk fabricID:@(1) nocSigner:rootKeys]; XCTAssertNotNil(params); params.vendorID = @(kTestVendorId); - MTRDeviceController * controller = [factory startControllerOnNewFabric:params]; + MTRDeviceController * controller = [factory createControllerOnNewFabric:params error:nil]; XCTAssertNotNil(controller); XCTAssertTrue([controller isRunning]); @@ -682,7 +648,7 @@ - (void)testControllerStartupNodeIdPreserved [controller shutdown]; XCTAssertFalse([controller isRunning]); - controller = [factory startControllerOnExistingFabric:params]; + controller = [factory createControllerOnExistingFabric:params error:nil]; XCTAssertNotNil(controller); XCTAssertTrue([controller isRunning]); @@ -691,33 +657,31 @@ - (void)testControllerStartupNodeIdPreserved [controller shutdown]; XCTAssertFalse([controller isRunning]); - [factory shutdown]; + [factory stopControllerFactory]; XCTAssertFalse([factory isRunning]); } - (void)testControllerStartupNodeIdUsed { - __auto_type * factory = [MTRControllerFactory sharedInstance]; + __auto_type * factory = [MTRDeviceControllerFactory sharedInstance]; XCTAssertNotNil(factory); __auto_type * storage = [[MTRTestStorage alloc] init]; - __auto_type * factoryParams = [[MTRControllerFactoryParams alloc] initWithStorage:storage]; - XCTAssertTrue([factory startup:factoryParams]); + __auto_type * factoryParams = [[MTRDeviceControllerFactoryParams alloc] initWithStorage:storage]; + XCTAssertTrue([factory startControllerFactory:factoryParams error:nil]); XCTAssertTrue([factory isRunning]); __auto_type * rootKeys = [[MTRTestKeys alloc] init]; XCTAssertNotNil(rootKeys); - __auto_type * params = [[MTRDeviceControllerStartupParams alloc] initWithSigningKeypair:rootKeys - fabricID:@(1) - ipk:rootKeys.ipk]; + __auto_type * params = [[MTRDeviceControllerStartupParams alloc] initWithIPK:rootKeys.ipk fabricID:@(1) nocSigner:rootKeys]; XCTAssertNotNil(params); params.vendorID = @(kTestVendorId); // Bring up with node id 17. params.nodeID = @17; - MTRDeviceController * controller = [factory startControllerOnNewFabric:params]; + MTRDeviceController * controller = [factory createControllerOnNewFabric:params error:nil]; XCTAssertNotNil(controller); XCTAssertTrue([controller isRunning]); @@ -728,7 +692,7 @@ - (void)testControllerStartupNodeIdUsed // Bring up with a different node id (18). params.nodeID = @18; - controller = [factory startControllerOnExistingFabric:params]; + controller = [factory createControllerOnExistingFabric:params error:nil]; XCTAssertNotNil(controller); XCTAssertTrue([controller isRunning]); @@ -739,7 +703,7 @@ - (void)testControllerStartupNodeIdUsed // Verify the new node id has been stored. params.nodeID = nil; - controller = [factory startControllerOnExistingFabric:params]; + controller = [factory createControllerOnExistingFabric:params error:nil]; XCTAssertNotNil(controller); XCTAssertTrue([controller isRunning]); @@ -748,43 +712,41 @@ - (void)testControllerStartupNodeIdUsed [controller shutdown]; XCTAssertFalse([controller isRunning]); - [factory shutdown]; + [factory stopControllerFactory]; XCTAssertFalse([factory isRunning]); } - (void)testControllerStartupNodeIdValidation { - __auto_type * factory = [MTRControllerFactory sharedInstance]; + __auto_type * factory = [MTRDeviceControllerFactory sharedInstance]; XCTAssertNotNil(factory); __auto_type * storage = [[MTRTestStorage alloc] init]; - __auto_type * factoryParams = [[MTRControllerFactoryParams alloc] initWithStorage:storage]; - XCTAssertTrue([factory startup:factoryParams]); + __auto_type * factoryParams = [[MTRDeviceControllerFactoryParams alloc] initWithStorage:storage]; + XCTAssertTrue([factory startControllerFactory:factoryParams error:nil]); XCTAssertTrue([factory isRunning]); __auto_type * rootKeys = [[MTRTestKeys alloc] init]; XCTAssertNotNil(rootKeys); - __auto_type * params = [[MTRDeviceControllerStartupParams alloc] initWithSigningKeypair:rootKeys - fabricID:@(1) - ipk:rootKeys.ipk]; + __auto_type * params = [[MTRDeviceControllerStartupParams alloc] initWithIPK:rootKeys.ipk fabricID:@(1) nocSigner:rootKeys]; XCTAssertNotNil(params); params.vendorID = @(kTestVendorId); // Try to bring up with node id 0. params.nodeID = @0; - MTRDeviceController * controller = [factory startControllerOnNewFabric:params]; + MTRDeviceController * controller = [factory createControllerOnNewFabric:params error:nil]; XCTAssertNil(controller); // Try to bring up with node id that is outside of the operational range. params.nodeID = @(0xFFFFFFFF00000000ULL); - controller = [factory startControllerOnNewFabric:params]; + controller = [factory createControllerOnNewFabric:params error:nil]; XCTAssertNil(controller); // Verify that we can indeed bring up a controller for this fabric, with a valid node id. params.nodeID = @17; - controller = [factory startControllerOnNewFabric:params]; + controller = [factory createControllerOnNewFabric:params error:nil]; XCTAssertNotNil(controller); XCTAssertTrue([controller isRunning]); @@ -793,19 +755,19 @@ - (void)testControllerStartupNodeIdValidation [controller shutdown]; XCTAssertFalse([controller isRunning]); - [factory shutdown]; + [factory stopControllerFactory]; XCTAssertFalse([factory isRunning]); } - (void)testControllerRotateToICA { // Tests that we can switch a fabric from not using an ICA to using an ICA. - __auto_type * factory = [MTRControllerFactory sharedInstance]; + __auto_type * factory = [MTRDeviceControllerFactory sharedInstance]; XCTAssertNotNil(factory); __auto_type * storage = [[MTRTestStorage alloc] init]; - __auto_type * factoryParams = [[MTRControllerFactoryParams alloc] initWithStorage:storage]; - XCTAssertTrue([factory startup:factoryParams]); + __auto_type * factoryParams = [[MTRDeviceControllerFactoryParams alloc] initWithStorage:storage]; + XCTAssertTrue([factory startControllerFactory:factoryParams error:nil]); XCTAssertTrue([factory isRunning]); __auto_type * rootKeys = [[MTRTestKeys alloc] init]; @@ -825,16 +787,14 @@ - (void)testControllerRotateToICA error:nil]; XCTAssertNotNil(intermediate); - __auto_type * params = [[MTRDeviceControllerStartupParams alloc] initWithSigningKeypair:rootKeys - fabricID:@(1) - ipk:rootKeys.ipk]; + __auto_type * params = [[MTRDeviceControllerStartupParams alloc] initWithIPK:rootKeys.ipk fabricID:@(1) nocSigner:rootKeys]; XCTAssertNotNil(params); params.vendorID = @(kTestVendorId); // Create a new fabric without the ICA. params.rootCertificate = root; - MTRDeviceController * controller = [factory startControllerOnNewFabric:params]; + MTRDeviceController * controller = [factory createControllerOnNewFabric:params error:nil]; XCTAssertNotNil(controller); XCTAssertTrue([controller isRunning]); @@ -844,11 +804,11 @@ - (void)testControllerRotateToICA XCTAssertFalse([controller isRunning]); // Now start controller on the same fabric but using the ICA. - params = [[MTRDeviceControllerStartupParams alloc] initWithSigningKeypair:intermediateKeys fabricID:@(1) ipk:rootKeys.ipk]; + params = [[MTRDeviceControllerStartupParams alloc] initWithIPK:rootKeys.ipk fabricID:@(1) nocSigner:intermediateKeys]; params.vendorID = @(kTestVendorId); params.rootCertificate = root; params.intermediateCertificate = intermediate; - controller = [factory startControllerOnExistingFabric:params]; + controller = [factory createControllerOnExistingFabric:params error:nil]; XCTAssertNotNil(controller); XCTAssertTrue([controller isRunning]); @@ -857,19 +817,19 @@ - (void)testControllerRotateToICA [controller shutdown]; XCTAssertFalse([controller isRunning]); - [factory shutdown]; + [factory stopControllerFactory]; XCTAssertFalse([factory isRunning]); } - (void)testControllerRotateFromICA { // Tests that we can switch a fabric from using an ICA to not using an ICA. - __auto_type * factory = [MTRControllerFactory sharedInstance]; + __auto_type * factory = [MTRDeviceControllerFactory sharedInstance]; XCTAssertNotNil(factory); __auto_type * storage = [[MTRTestStorage alloc] init]; - __auto_type * factoryParams = [[MTRControllerFactoryParams alloc] initWithStorage:storage]; - XCTAssertTrue([factory startup:factoryParams]); + __auto_type * factoryParams = [[MTRDeviceControllerFactoryParams alloc] initWithStorage:storage]; + XCTAssertTrue([factory startControllerFactory:factoryParams error:nil]); XCTAssertTrue([factory isRunning]); __auto_type * rootKeys = [[MTRTestKeys alloc] init]; @@ -889,9 +849,9 @@ - (void)testControllerRotateFromICA error:nil]; XCTAssertNotNil(intermediate); - __auto_type * params = [[MTRDeviceControllerStartupParams alloc] initWithSigningKeypair:intermediateKeys - fabricID:@(1) - ipk:rootKeys.ipk]; + __auto_type * params = [[MTRDeviceControllerStartupParams alloc] initWithIPK:rootKeys.ipk + fabricID:@(1) + nocSigner:intermediateKeys]; XCTAssertNotNil(params); params.vendorID = @(kTestVendorId); @@ -899,7 +859,7 @@ - (void)testControllerRotateFromICA // Create a new fabric without the ICA. params.rootCertificate = root; params.intermediateCertificate = intermediate; - MTRDeviceController * controller = [factory startControllerOnNewFabric:params]; + MTRDeviceController * controller = [factory createControllerOnNewFabric:params error:nil]; XCTAssertNotNil(controller); XCTAssertTrue([controller isRunning]); @@ -909,10 +869,10 @@ - (void)testControllerRotateFromICA XCTAssertFalse([controller isRunning]); // Now start controller on the same fabric but without using the ICA. - params = [[MTRDeviceControllerStartupParams alloc] initWithSigningKeypair:rootKeys fabricID:@(1) ipk:rootKeys.ipk]; + params = [[MTRDeviceControllerStartupParams alloc] initWithIPK:rootKeys.ipk fabricID:@(1) nocSigner:rootKeys]; params.vendorID = @(kTestVendorId); params.rootCertificate = root; - controller = [factory startControllerOnExistingFabric:params]; + controller = [factory createControllerOnExistingFabric:params error:nil]; XCTAssertNotNil(controller); XCTAssertTrue([controller isRunning]); @@ -921,19 +881,19 @@ - (void)testControllerRotateFromICA [controller shutdown]; XCTAssertFalse([controller isRunning]); - [factory shutdown]; + [factory stopControllerFactory]; XCTAssertFalse([factory isRunning]); } - (void)testControllerRotateICA { // Tests that we can change the ICA being used for a fabric. - __auto_type * factory = [MTRControllerFactory sharedInstance]; + __auto_type * factory = [MTRDeviceControllerFactory sharedInstance]; XCTAssertNotNil(factory); __auto_type * storage = [[MTRTestStorage alloc] init]; - __auto_type * factoryParams = [[MTRControllerFactoryParams alloc] initWithStorage:storage]; - XCTAssertTrue([factory startup:factoryParams]); + __auto_type * factoryParams = [[MTRDeviceControllerFactoryParams alloc] initWithStorage:storage]; + XCTAssertTrue([factory startControllerFactory:factoryParams error:nil]); XCTAssertTrue([factory isRunning]); __auto_type * rootKeys = [[MTRTestKeys alloc] init]; @@ -964,9 +924,9 @@ - (void)testControllerRotateICA error:nil]; XCTAssertNotNil(intermediate2); - __auto_type * params = [[MTRDeviceControllerStartupParams alloc] initWithSigningKeypair:intermediateKeys1 - fabricID:@(1) - ipk:rootKeys.ipk]; + __auto_type * params = [[MTRDeviceControllerStartupParams alloc] initWithIPK:rootKeys.ipk + fabricID:@(1) + nocSigner:intermediateKeys1]; XCTAssertNotNil(params); params.vendorID = @(kTestVendorId); @@ -974,7 +934,7 @@ - (void)testControllerRotateICA // Create a new fabric without the first ICA. params.rootCertificate = root; params.intermediateCertificate = intermediate1; - MTRDeviceController * controller = [factory startControllerOnNewFabric:params]; + MTRDeviceController * controller = [factory createControllerOnNewFabric:params error:nil]; XCTAssertNotNil(controller); XCTAssertTrue([controller isRunning]); @@ -984,11 +944,11 @@ - (void)testControllerRotateICA XCTAssertFalse([controller isRunning]); // Now start controller on the same fabric but using the second ICA. - params = [[MTRDeviceControllerStartupParams alloc] initWithSigningKeypair:intermediateKeys2 fabricID:@(1) ipk:rootKeys.ipk]; + params = [[MTRDeviceControllerStartupParams alloc] initWithIPK:rootKeys.ipk fabricID:@(1) nocSigner:intermediateKeys2]; params.vendorID = @(kTestVendorId); params.rootCertificate = root; params.intermediateCertificate = intermediate2; - controller = [factory startControllerOnExistingFabric:params]; + controller = [factory createControllerOnExistingFabric:params error:nil]; XCTAssertNotNil(controller); XCTAssertTrue([controller isRunning]); @@ -997,18 +957,18 @@ - (void)testControllerRotateICA [controller shutdown]; XCTAssertFalse([controller isRunning]); - [factory shutdown]; + [factory stopControllerFactory]; XCTAssertFalse([factory isRunning]); } - (void)testControllerICAWithoutRoot { - __auto_type * factory = [MTRControllerFactory sharedInstance]; + __auto_type * factory = [MTRDeviceControllerFactory sharedInstance]; XCTAssertNotNil(factory); __auto_type * storage = [[MTRTestStorage alloc] init]; - __auto_type * factoryParams = [[MTRControllerFactoryParams alloc] initWithStorage:storage]; - XCTAssertTrue([factory startup:factoryParams]); + __auto_type * factoryParams = [[MTRDeviceControllerFactoryParams alloc] initWithStorage:storage]; + XCTAssertTrue([factory startControllerFactory:factoryParams error:nil]); XCTAssertTrue([factory isRunning]); __auto_type * rootKeys = [[MTRTestKeys alloc] init]; @@ -1028,30 +988,30 @@ - (void)testControllerICAWithoutRoot error:nil]; XCTAssertNotNil(intermediate); - __auto_type * params = [[MTRDeviceControllerStartupParams alloc] initWithSigningKeypair:intermediateKeys - fabricID:@(1) - ipk:rootKeys.ipk]; + __auto_type * params = [[MTRDeviceControllerStartupParams alloc] initWithIPK:rootKeys.ipk + fabricID:@(1) + nocSigner:intermediateKeys]; XCTAssertNotNil(params); params.vendorID = @(kTestVendorId); // Pass in an intermediate but no root. Should fail. params.intermediateCertificate = intermediate; - MTRDeviceController * controller = [factory startControllerOnNewFabric:params]; + MTRDeviceController * controller = [factory createControllerOnNewFabric:params error:nil]; XCTAssertNil(controller); - [factory shutdown]; + [factory stopControllerFactory]; XCTAssertFalse([factory isRunning]); } - (void)testControllerProvideFullCertChain { - __auto_type * factory = [MTRControllerFactory sharedInstance]; + __auto_type * factory = [MTRDeviceControllerFactory sharedInstance]; XCTAssertNotNil(factory); __auto_type * storage = [[MTRTestStorage alloc] init]; - __auto_type * factoryParams = [[MTRControllerFactoryParams alloc] initWithStorage:storage]; - XCTAssertTrue([factory startup:factoryParams]); + __auto_type * factoryParams = [[MTRDeviceControllerFactoryParams alloc] initWithStorage:storage]; + XCTAssertTrue([factory startControllerFactory:factoryParams error:nil]); XCTAssertTrue([factory isRunning]); __auto_type * rootKeys = [[MTRTestKeys alloc] init]; @@ -1083,16 +1043,16 @@ - (void)testControllerProvideFullCertChain error:nil]; XCTAssertNotNil(operational); - __auto_type * params = [[MTRDeviceControllerStartupParams alloc] initWithOperationalKeypair:operationalKeys - operationalCertificate:operational - intermediateCertificate:intermediate - rootCertificate:root - ipk:rootKeys.ipk]; + __auto_type * params = [[MTRDeviceControllerStartupParams alloc] initWithIPK:rootKeys.ipk + operationalKeypair:operationalKeys + operationalCertificate:operational + intermediateCertificate:intermediate + rootCertificate:root]; XCTAssertNotNil(params); params.vendorID = @(kTestVendorId); - MTRDeviceController * controller = [factory startControllerOnNewFabric:params]; + MTRDeviceController * controller = [factory createControllerOnNewFabric:params error:nil]; XCTAssertNotNil(controller); XCTAssertTrue([controller isRunning]); @@ -1102,11 +1062,11 @@ - (void)testControllerProvideFullCertChain XCTAssertFalse([controller isRunning]); // Trying to bring up another new fabric with the same root and NOC should fail. - controller = [factory startControllerOnNewFabric:params]; + controller = [factory createControllerOnNewFabric:params error:nil]; XCTAssertNil(controller); // Trying to bring up the same fabric should succeed. - controller = [factory startControllerOnExistingFabric:params]; + controller = [factory createControllerOnExistingFabric:params error:nil]; XCTAssertNotNil(controller); XCTAssertTrue([controller isRunning]); @@ -1115,18 +1075,18 @@ - (void)testControllerProvideFullCertChain [controller shutdown]; XCTAssertFalse([controller isRunning]); - [factory shutdown]; + [factory stopControllerFactory]; XCTAssertFalse([factory isRunning]); } - (void)testControllerProvideCertChainNoICA { - __auto_type * factory = [MTRControllerFactory sharedInstance]; + __auto_type * factory = [MTRDeviceControllerFactory sharedInstance]; XCTAssertNotNil(factory); __auto_type * storage = [[MTRTestStorage alloc] init]; - __auto_type * factoryParams = [[MTRControllerFactoryParams alloc] initWithStorage:storage]; - XCTAssertTrue([factory startup:factoryParams]); + __auto_type * factoryParams = [[MTRDeviceControllerFactoryParams alloc] initWithStorage:storage]; + XCTAssertTrue([factory startControllerFactory:factoryParams error:nil]); XCTAssertTrue([factory isRunning]); __auto_type * rootKeys = [[MTRTestKeys alloc] init]; @@ -1147,16 +1107,16 @@ - (void)testControllerProvideCertChainNoICA error:nil]; XCTAssertNotNil(operational); - __auto_type * params = [[MTRDeviceControllerStartupParams alloc] initWithOperationalKeypair:operationalKeys - operationalCertificate:operational - intermediateCertificate:nil - rootCertificate:root - ipk:rootKeys.ipk]; + __auto_type * params = [[MTRDeviceControllerStartupParams alloc] initWithIPK:rootKeys.ipk + operationalKeypair:operationalKeys + operationalCertificate:operational + intermediateCertificate:nil + rootCertificate:root]; XCTAssertNotNil(params); params.vendorID = @(kTestVendorId); - MTRDeviceController * controller = [factory startControllerOnNewFabric:params]; + MTRDeviceController * controller = [factory createControllerOnNewFabric:params error:nil]; XCTAssertNotNil(controller); XCTAssertTrue([controller isRunning]); @@ -1165,18 +1125,18 @@ - (void)testControllerProvideCertChainNoICA [controller shutdown]; XCTAssertFalse([controller isRunning]); - [factory shutdown]; + [factory stopControllerFactory]; XCTAssertFalse([factory isRunning]); } - (void)testControllerCertChainFabricMismatchRoot { - __auto_type * factory = [MTRControllerFactory sharedInstance]; + __auto_type * factory = [MTRDeviceControllerFactory sharedInstance]; XCTAssertNotNil(factory); __auto_type * storage = [[MTRTestStorage alloc] init]; - __auto_type * factoryParams = [[MTRControllerFactoryParams alloc] initWithStorage:storage]; - XCTAssertTrue([factory startup:factoryParams]); + __auto_type * factoryParams = [[MTRDeviceControllerFactoryParams alloc] initWithStorage:storage]; + XCTAssertTrue([factory startControllerFactory:factoryParams error:nil]); XCTAssertTrue([factory isRunning]); __auto_type * rootKeys = [[MTRTestKeys alloc] init]; @@ -1197,30 +1157,30 @@ - (void)testControllerCertChainFabricMismatchRoot error:nil]; XCTAssertNotNil(operational); - __auto_type * params = [[MTRDeviceControllerStartupParams alloc] initWithOperationalKeypair:operationalKeys - operationalCertificate:operational - intermediateCertificate:nil - rootCertificate:root - ipk:rootKeys.ipk]; + __auto_type * params = [[MTRDeviceControllerStartupParams alloc] initWithIPK:rootKeys.ipk + operationalKeypair:operationalKeys + operationalCertificate:operational + intermediateCertificate:nil + rootCertificate:root]; XCTAssertNotNil(params); params.vendorID = @(kTestVendorId); - MTRDeviceController * controller = [factory startControllerOnNewFabric:params]; + MTRDeviceController * controller = [factory createControllerOnNewFabric:params error:nil]; XCTAssertNil(controller); - [factory shutdown]; + [factory stopControllerFactory]; XCTAssertFalse([factory isRunning]); } - (void)testControllerCertChainFabricMismatchIntermediate { - __auto_type * factory = [MTRControllerFactory sharedInstance]; + __auto_type * factory = [MTRDeviceControllerFactory sharedInstance]; XCTAssertNotNil(factory); __auto_type * storage = [[MTRTestStorage alloc] init]; - __auto_type * factoryParams = [[MTRControllerFactoryParams alloc] initWithStorage:storage]; - XCTAssertTrue([factory startup:factoryParams]); + __auto_type * factoryParams = [[MTRDeviceControllerFactoryParams alloc] initWithStorage:storage]; + XCTAssertTrue([factory startControllerFactory:factoryParams error:nil]); XCTAssertTrue([factory isRunning]); __auto_type * rootKeys = [[MTRTestKeys alloc] init]; @@ -1252,30 +1212,30 @@ - (void)testControllerCertChainFabricMismatchIntermediate error:nil]; XCTAssertNotNil(operational); - __auto_type * params = [[MTRDeviceControllerStartupParams alloc] initWithOperationalKeypair:operationalKeys - operationalCertificate:operational - intermediateCertificate:intermediate - rootCertificate:root - ipk:rootKeys.ipk]; + __auto_type * params = [[MTRDeviceControllerStartupParams alloc] initWithIPK:rootKeys.ipk + operationalKeypair:operationalKeys + operationalCertificate:operational + intermediateCertificate:intermediate + rootCertificate:root]; XCTAssertNotNil(params); params.vendorID = @(kTestVendorId); - MTRDeviceController * controller = [factory startControllerOnNewFabric:params]; + MTRDeviceController * controller = [factory createControllerOnNewFabric:params error:nil]; XCTAssertNil(controller); - [factory shutdown]; + [factory stopControllerFactory]; XCTAssertFalse([factory isRunning]); } - (void)testControllerExternallyProvidedOperationalKey { - __auto_type * factory = [MTRControllerFactory sharedInstance]; + __auto_type * factory = [MTRDeviceControllerFactory sharedInstance]; XCTAssertNotNil(factory); __auto_type * storage = [[MTRTestStorage alloc] init]; - __auto_type * factoryParams = [[MTRControllerFactoryParams alloc] initWithStorage:storage]; - XCTAssertTrue([factory startup:factoryParams]); + __auto_type * factoryParams = [[MTRDeviceControllerFactoryParams alloc] initWithStorage:storage]; + XCTAssertTrue([factory startControllerFactory:factoryParams error:nil]); XCTAssertTrue([factory isRunning]); __auto_type * rootKeys = [[MTRTestKeys alloc] init]; @@ -1284,15 +1244,13 @@ - (void)testControllerExternallyProvidedOperationalKey __auto_type * operationalKeys = [[MTRTestKeys alloc] init]; XCTAssertNotNil(operationalKeys); - __auto_type * params = [[MTRDeviceControllerStartupParams alloc] initWithSigningKeypair:rootKeys - fabricID:@(1) - ipk:rootKeys.ipk]; + __auto_type * params = [[MTRDeviceControllerStartupParams alloc] initWithIPK:rootKeys.ipk fabricID:@(1) nocSigner:rootKeys]; XCTAssertNotNil(params); params.vendorID = @(kTestVendorId); params.operationalKeypair = operationalKeys; - MTRDeviceController * controller = [factory startControllerOnNewFabric:params]; + MTRDeviceController * controller = [factory createControllerOnNewFabric:params error:nil]; XCTAssertNotNil(controller); XCTAssertTrue([controller isRunning]); @@ -1305,13 +1263,13 @@ - (void)testControllerExternallyProvidedOperationalKey // keypair should now fail, because we won't know what operational keys to // use. params.operationalKeypair = nil; - controller = [factory startControllerOnExistingFabric:params]; + controller = [factory createControllerOnExistingFabric:params error:nil]; XCTAssertNil(controller); // But bringing up the controller with provided operational keys should // work, and have the same node id. params.operationalKeypair = operationalKeys; - controller = [factory startControllerOnExistingFabric:params]; + controller = [factory createControllerOnExistingFabric:params error:nil]; XCTAssertNotNil(controller); XCTAssertTrue([controller isRunning]); @@ -1326,7 +1284,7 @@ - (void)testControllerExternallyProvidedOperationalKey XCTAssertNotNil(newOperationalKeys); params.operationalKeypair = newOperationalKeys; - controller = [factory startControllerOnExistingFabric:params]; + controller = [factory createControllerOnExistingFabric:params error:nil]; XCTAssertNotNil(controller); XCTAssertTrue([controller isRunning]); @@ -1335,7 +1293,7 @@ - (void)testControllerExternallyProvidedOperationalKey [controller shutdown]; XCTAssertFalse([controller isRunning]); - [factory shutdown]; + [factory stopControllerFactory]; XCTAssertFalse([factory isRunning]); } diff --git a/src/darwin/Framework/CHIPTests/MTRDeviceTests.m b/src/darwin/Framework/CHIPTests/MTRDeviceTests.m index 0ad1dad79bf52e..abbb56442912e3 100644 --- a/src/darwin/Framework/CHIPTests/MTRDeviceTests.m +++ b/src/darwin/Framework/CHIPTests/MTRDeviceTests.m @@ -144,25 +144,23 @@ - (void)initStack { XCTestExpectation * expectation = [self expectationWithDescription:@"Pairing Complete"]; - __auto_type * factory = [MTRControllerFactory sharedInstance]; + __auto_type * factory = [MTRDeviceControllerFactory sharedInstance]; XCTAssertNotNil(factory); __auto_type * storage = [[MTRTestStorage alloc] init]; - __auto_type * factoryParams = [[MTRControllerFactoryParams alloc] initWithStorage:storage]; + __auto_type * factoryParams = [[MTRDeviceControllerFactoryParams alloc] initWithStorage:storage]; factoryParams.port = @(kLocalPort); - BOOL ok = [factory startup:factoryParams]; + BOOL ok = [factory startControllerFactory:factoryParams error:nil]; XCTAssertTrue(ok); __auto_type * testKeys = [[MTRTestKeys alloc] init]; XCTAssertNotNil(testKeys); - __auto_type * params = [[MTRDeviceControllerStartupParams alloc] initWithSigningKeypair:testKeys - fabricID:@(1) - ipk:testKeys.ipk]; + __auto_type * params = [[MTRDeviceControllerStartupParams alloc] initWithIPK:testKeys.ipk fabricID:@(1) nocSigner:testKeys]; params.vendorID = @(kTestVendorId); - MTRDeviceController * controller = [factory startControllerOnNewFabric:params]; + MTRDeviceController * controller = [factory createControllerOnNewFabric:params error:nil]; XCTAssertNotNil(controller); sController = controller; @@ -197,7 +195,7 @@ - (void)shutdownStack [controller shutdown]; XCTAssertFalse([controller isRunning]); - [[MTRControllerFactory sharedInstance] shutdown]; + [[MTRDeviceControllerFactory sharedInstance] stopControllerFactory]; } - (void)waitForCommissionee diff --git a/src/darwin/Framework/CHIPTests/MTRXPCListenerSampleTests.m b/src/darwin/Framework/CHIPTests/MTRXPCListenerSampleTests.m index fdd29512cb6b8d..593035a6830b18 100644 --- a/src/darwin/Framework/CHIPTests/MTRXPCListenerSampleTests.m +++ b/src/darwin/Framework/CHIPTests/MTRXPCListenerSampleTests.m @@ -450,26 +450,27 @@ - (void)initStack { XCTestExpectation * expectation = [self expectationWithDescription:@"Pairing Complete"]; - __auto_type * factory = [MTRControllerFactory sharedInstance]; + __auto_type * factory = [MTRDeviceControllerFactory sharedInstance]; XCTAssertNotNil(factory); __auto_type * storage = [[MTRTestStorage alloc] init]; - __auto_type * factoryParams = [[MTRControllerFactoryParams alloc] initWithStorage:storage]; + __auto_type * factoryParams = [[MTRDeviceControllerFactoryParams alloc] initWithStorage:storage]; factoryParams.port = @(kLocalPort); - BOOL ok = [factory startup:factoryParams]; + NSError * error; + BOOL ok = [factory startControllerFactory:factoryParams error:&error]; XCTAssertTrue(ok); + XCTAssertNil(error); __auto_type * testKeys = [[MTRTestKeys alloc] init]; XCTAssertNotNil(testKeys); - __auto_type * params = [[MTRDeviceControllerStartupParams alloc] initWithSigningKeypair:testKeys - fabricID:@(1) - ipk:testKeys.ipk]; + __auto_type * params = [[MTRDeviceControllerStartupParams alloc] initWithIPK:testKeys.ipk fabricID:@(1) nocSigner:testKeys]; params.vendorID = @(kTestVendorId); - MTRDeviceController * controller = [factory startControllerOnNewFabric:params]; + MTRDeviceController * controller = [factory createControllerOnNewFabric:params error:&error]; XCTAssertNotNil(controller); + XCTAssertNil(error); sController = controller; @@ -479,7 +480,6 @@ - (void)initStack [controller setPairingDelegate:pairing queue:callbackQueue]; - NSError * error; [controller pairDevice:kDeviceId address:kAddress port:kRemotePort setupPINCode:kSetupPINCode error:&error]; XCTAssertEqual(error.code, 0); @@ -510,7 +510,7 @@ - (void)shutdownStack [controller shutdown]; XCTAssertFalse([controller isRunning]); - [[MTRControllerFactory sharedInstance] shutdown]; + [[MTRDeviceControllerFactory sharedInstance] stopControllerFactory]; mDeviceController = nil; } diff --git a/src/darwin/Framework/Matter.xcodeproj/project.pbxproj b/src/darwin/Framework/Matter.xcodeproj/project.pbxproj index 0b8828141a3ab5..8b44d5f39639dc 100644 --- a/src/darwin/Framework/Matter.xcodeproj/project.pbxproj +++ b/src/darwin/Framework/Matter.xcodeproj/project.pbxproj @@ -43,9 +43,9 @@ 511913FC28C100EF009235E9 /* MTRBaseSubscriptionCallback.h in Headers */ = {isa = PBXBuildFile; fileRef = 511913FA28C100EF009235E9 /* MTRBaseSubscriptionCallback.h */; }; 5129BCFD26A9EE3300122DDF /* MTRError.h in Headers */ = {isa = PBXBuildFile; fileRef = 5129BCFC26A9EE3300122DDF /* MTRError.h */; settings = {ATTRIBUTES = (Public, ); }; }; 5136661328067D550025EDAE /* MTRDeviceController_Internal.h in Headers */ = {isa = PBXBuildFile; fileRef = 5136660F28067D540025EDAE /* MTRDeviceController_Internal.h */; }; - 5136661428067D550025EDAE /* MTRControllerFactory.mm in Sources */ = {isa = PBXBuildFile; fileRef = 5136661028067D540025EDAE /* MTRControllerFactory.mm */; }; - 5136661528067D550025EDAE /* MTRControllerFactory_Internal.h in Headers */ = {isa = PBXBuildFile; fileRef = 5136661128067D540025EDAE /* MTRControllerFactory_Internal.h */; }; - 5136661628067D550025EDAE /* MTRControllerFactory.h in Headers */ = {isa = PBXBuildFile; fileRef = 5136661228067D550025EDAE /* MTRControllerFactory.h */; settings = {ATTRIBUTES = (Public, ); }; }; + 5136661428067D550025EDAE /* MTRDeviceControllerFactory.mm in Sources */ = {isa = PBXBuildFile; fileRef = 5136661028067D540025EDAE /* MTRDeviceControllerFactory.mm */; }; + 5136661528067D550025EDAE /* MTRDeviceControllerFactory_Internal.h in Headers */ = {isa = PBXBuildFile; fileRef = 5136661128067D540025EDAE /* MTRDeviceControllerFactory_Internal.h */; }; + 5136661628067D550025EDAE /* MTRDeviceControllerFactory.h in Headers */ = {isa = PBXBuildFile; fileRef = 5136661228067D550025EDAE /* MTRDeviceControllerFactory.h */; settings = {ATTRIBUTES = (Public, ); }; }; 513DDB862761F69300DAA01A /* MTRAttributeTLVValueDecoder_Internal.h in Headers */ = {isa = PBXBuildFile; fileRef = 513DDB852761F69300DAA01A /* MTRAttributeTLVValueDecoder_Internal.h */; }; 513DDB8A2761F6F900DAA01A /* MTRAttributeTLVValueDecoder.mm in Sources */ = {isa = PBXBuildFile; fileRef = 513DDB892761F6F900DAA01A /* MTRAttributeTLVValueDecoder.mm */; }; 51431AF927D2973E008A7943 /* MTRIMDispatch.mm in Sources */ = {isa = PBXBuildFile; fileRef = 51431AF827D2973E008A7943 /* MTRIMDispatch.mm */; }; @@ -182,9 +182,9 @@ 511913FA28C100EF009235E9 /* MTRBaseSubscriptionCallback.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = MTRBaseSubscriptionCallback.h; sourceTree = ""; }; 5129BCFC26A9EE3300122DDF /* MTRError.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = MTRError.h; sourceTree = ""; }; 5136660F28067D540025EDAE /* MTRDeviceController_Internal.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = MTRDeviceController_Internal.h; sourceTree = ""; }; - 5136661028067D540025EDAE /* MTRControllerFactory.mm */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.objcpp; path = MTRControllerFactory.mm; sourceTree = ""; }; - 5136661128067D540025EDAE /* MTRControllerFactory_Internal.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = MTRControllerFactory_Internal.h; sourceTree = ""; }; - 5136661228067D550025EDAE /* MTRControllerFactory.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = MTRControllerFactory.h; sourceTree = ""; }; + 5136661028067D540025EDAE /* MTRDeviceControllerFactory.mm */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.objcpp; path = MTRDeviceControllerFactory.mm; sourceTree = ""; }; + 5136661128067D540025EDAE /* MTRDeviceControllerFactory_Internal.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = MTRDeviceControllerFactory_Internal.h; sourceTree = ""; }; + 5136661228067D550025EDAE /* MTRDeviceControllerFactory.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = MTRDeviceControllerFactory.h; sourceTree = ""; }; 513DDB852761F69300DAA01A /* MTRAttributeTLVValueDecoder_Internal.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = MTRAttributeTLVValueDecoder_Internal.h; sourceTree = ""; }; 513DDB892761F6F900DAA01A /* MTRAttributeTLVValueDecoder.mm */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.objcpp; name = MTRAttributeTLVValueDecoder.mm; path = "zap-generated/MTRAttributeTLVValueDecoder.mm"; sourceTree = ""; }; 51431AF827D2973E008A7943 /* MTRIMDispatch.mm */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.objcpp; path = MTRIMDispatch.mm; sourceTree = ""; }; @@ -421,12 +421,12 @@ 991DC0822475F45400C13860 /* MTRDeviceController.h */, 991DC0872475F47D00C13860 /* MTRDeviceController.mm */, 5136660F28067D540025EDAE /* MTRDeviceController_Internal.h */, - 5136661128067D540025EDAE /* MTRControllerFactory_Internal.h */, + 5136661128067D540025EDAE /* MTRDeviceControllerFactory_Internal.h */, 51E51FBD282AD37A00FC978D /* MTRDeviceControllerStartupParams_Internal.h */, 51E51FBC282AD37A00FC978D /* MTRDeviceControllerStartupParams.h */, 51E51FBE282AD37A00FC978D /* MTRDeviceControllerStartupParams.mm */, - 5136661228067D550025EDAE /* MTRControllerFactory.h */, - 5136661028067D540025EDAE /* MTRControllerFactory.mm */, + 5136661228067D550025EDAE /* MTRDeviceControllerFactory.h */, + 5136661028067D540025EDAE /* MTRDeviceControllerFactory.mm */, 5A7947E227C0101200434CF2 /* MTRDeviceController+XPC.h */, 517BF3EE282B62B800A8B7DB /* MTRCertificates.h */, 517BF3EF282B62B800A8B7DB /* MTRCertificates.mm */, @@ -496,7 +496,7 @@ files = ( 517BF3F0282B62B800A8B7DB /* MTRCertificates.h in Headers */, 51E51FBF282AD37A00FC978D /* MTRDeviceControllerStartupParams.h in Headers */, - 5136661628067D550025EDAE /* MTRControllerFactory.h in Headers */, + 5136661628067D550025EDAE /* MTRDeviceControllerFactory.h in Headers */, 7596A84B287636C1004DAE0E /* MTRDevice_Internal.h in Headers */, 5A6FEC9927B5C88900F25F42 /* MTRDeviceOverXPC.h in Headers */, 51B22C222740CB1D008D5055 /* MTRCommandPayloadsObjc.h in Headers */, @@ -507,7 +507,7 @@ 2C1B027B2641DB4E00780EF1 /* MTROperationalCredentialsDelegate.h in Headers */, 7596A85728788557004DAE0E /* MTRClusters.h in Headers */, 99D466E12798936D0089A18F /* MTRCommissioningParameters.h in Headers */, - 5136661528067D550025EDAE /* MTRControllerFactory_Internal.h in Headers */, + 5136661528067D550025EDAE /* MTRDeviceControllerFactory_Internal.h in Headers */, 515C1C70284F9FFB00A48F0C /* MTRMemory.h in Headers */, 7534F12928BFF20300390851 /* MTRDeviceAttestationDelegate_Internal.h in Headers */, D4772A46285AE98400383630 /* MTRClusterConstants.h in Headers */, @@ -687,7 +687,7 @@ 515C1C6F284F9FFB00A48F0C /* MTRMemory.mm in Sources */, 27A53C1827FBC6920053F131 /* MTRAttestationTrustStoreBridge.mm in Sources */, 998F287126D56940001846C6 /* MTRP256KeypairBridge.mm in Sources */, - 5136661428067D550025EDAE /* MTRControllerFactory.mm in Sources */, + 5136661428067D550025EDAE /* MTRDeviceControllerFactory.mm in Sources */, 51B22C2A2740CB47008D5055 /* MTRCommandPayloadsObjc.mm in Sources */, AF5F90FF2878D351005503FA /* MTROTAProviderDelegateBridge.mm in Sources */, 7534F12828BFF20300390851 /* MTRDeviceAttestationDelegate.mm in Sources */,