From 661ecda28bba9fd707577df9ba05847cb08f7877 Mon Sep 17 00:00:00 2001 From: Boris Zbarsky Date: Wed, 14 Sep 2022 12:37:13 -0400 Subject: [PATCH] Address API review issues in MTRControllerFactory. This is a re-landing of PR #22606 but modified to preserve the old APIs. * 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. The header changes not accompanied by backwards-compat shims are OK for the following reasons: * The change in MTRBaseDevice_Internal.h is comment-only. * The change in MTRDeviceController.h should be source and binary compatible. * MTRDeviceControllerFactory_Internal.h is not a public header. * In MTRDeviceControllerStartupParams.h the changes with no shims are either comments or changes to MTR_NEWLY_AVAILABLE APIs. * MTRDeviceControllerStartupParams_Internal.h is not a public header. * MTRDeviceController_Internal.h is not a public header. --- .../commands/common/CHIPCommandBridge.mm | 31 +- .../Framework/CHIP/MTRBaseDevice_Internal.h | 3 - .../Framework/CHIP/MTRDeviceController.h | 4 +- .../Framework/CHIP/MTRDeviceController.mm | 8 +- ...Factory.h => MTRDeviceControllerFactory.h} | 63 ++- ...ctory.mm => MTRDeviceControllerFactory.mm} | 171 +++++- ... => MTRDeviceControllerFactory_Internal.h} | 6 +- .../CHIP/MTRDeviceControllerStartupParams.h | 34 +- .../CHIP/MTRDeviceControllerStartupParams.mm | 27 +- ...TRDeviceControllerStartupParams_Internal.h | 16 +- .../CHIP/MTRDeviceController_Internal.h | 22 +- .../CHIP/MTROTAProviderDelegateBridge.mm | 10 +- src/darwin/Framework/CHIP/Matter.h | 2 +- .../Framework/CHIPTests/MTRControllerTests.m | 514 +++++++++--------- .../Framework/CHIPTests/MTRDeviceTests.m | 14 +- .../CHIPTests/MTRXPCListenerSampleTests.m | 18 +- .../Matter.xcodeproj/project.pbxproj | 24 +- 17 files changed, 567 insertions(+), 400 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/src/darwin/Framework/CHIP/MTRBaseDevice_Internal.h b/src/darwin/Framework/CHIP/MTRBaseDevice_Internal.h index 7c3219fca1531b..0a02435ae144bf 100644 --- a/src/darwin/Framework/CHIP/MTRBaseDevice_Internal.h +++ b/src/darwin/Framework/CHIP/MTRBaseDevice_Internal.h @@ -55,9 +55,6 @@ NS_ASSUME_NONNULL_BEGIN */ @property (nonatomic, assign, readonly) chip::NodeId nodeID; -/** - * Controllers are created via the MTRControllerFactory 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 6515c4742f2987..76f51a18b2b9f7 100644 --- a/src/darwin/Framework/CHIP/MTRDeviceController.h +++ b/src/darwin/Framework/CHIP/MTRDeviceController.h @@ -31,7 +31,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 @@ -139,7 +139,7 @@ typedef void (^MTRDeviceConnectionCallback)(MTRBaseDevice * _Nullable device, NS error:(NSError * __autoreleasing *)error; /** - * 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 6ee251571a775d..07e77b8b34398c 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" @@ -87,14 +87,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; @@ -143,7 +143,7 @@ - (void)cleanupAfterStartup } // 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) { 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 efb2edd36670c0..97e7ca28749050 100644 --- a/src/darwin/Framework/CHIP/MTRControllerFactory.h +++ b/src/darwin/Framework/CHIP/MTRDeviceControllerFactory.h @@ -32,7 +32,8 @@ NS_ASSUME_NONNULL_BEGIN @class MTRDeviceController; @class MTRDeviceControllerStartupParams; -@interface MTRControllerFactoryParams : NSObject +MTR_NEWLY_AVAILABLE +@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 @@ -67,44 +68,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 MTR_NEWLY_AVAILABLE; - (instancetype)init NS_UNAVAILABLE; - (instancetype)initWithStorage:(id)storage; @end -@interface MTRControllerFactory : NSObject - -@property (readonly, nonatomic) BOOL isRunning; +@interface MTRDeviceControllerFactory : NSObject -- (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. @@ -115,7 +119,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. @@ -125,13 +130,31 @@ 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 -@interface MTRControllerFactoryParams (Deprecated) +MTR_NEWLY_DEPRECATED("Please use MTRDeviceControllerFactoryParams") +@interface MTRControllerFactoryParams : MTRDeviceControllerFactoryParams @property (nonatomic, strong, readonly) id storageDelegate MTR_NEWLY_DEPRECATED( "Please use the storage property"); +@property (nonatomic, assign) BOOL startServer; +@end + +MTR_NEWLY_DEPRECATED("Please use MTRDeviceControllerFactory") +@interface MTRControllerFactory : NSObject +@property (readonly, nonatomic) BOOL isRunning; ++ (instancetype)sharedInstance; +- (BOOL)startup:(MTRControllerFactoryParams *)startupParams; +- (void)shutdown; +- (MTRDeviceController * _Nullable)startControllerOnExistingFabric:(MTRDeviceControllerStartupParams *)startupParams; +- (MTRDeviceController * _Nullable)startControllerOnNewFabric:(MTRDeviceControllerStartupParams *)startupParams; +- (instancetype)init NS_UNAVAILABLE; ++ (instancetype)new NS_UNAVAILABLE; @end NS_ASSUME_NONNULL_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 0b6926e08ad4b1..9167cd047c1209 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" @@ -58,7 +59,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; @@ -82,15 +83,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; } @@ -101,7 +102,7 @@ - (instancetype)init return nil; } - _isRunning = NO; + _running = NO; _chipWorkQueue = DeviceLayer::PlatformMgrImpl().GetWorkQueue(); _controllerFactory = &DeviceControllerFactory::GetInstance(); [MTRMemory ensureInit]; @@ -133,10 +134,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) { @@ -201,7 +215,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..."); @@ -221,6 +235,7 @@ - (BOOL)startup:(MTRControllerFactoryParams *)startupParams _persistentStorageDelegateBridge = new MTRPersistentStorageDelegateBridge(startupParams.storage); if (_persistentStorageDelegateBridge == nil) { MTR_LOG_ERROR("Error: %@", kErrorPersistentStorageInit); + errorCode = CHIP_ERROR_NO_MEMORY; return; } @@ -271,6 +286,7 @@ - (BOOL)startup:(MTRControllerFactoryParams *)startupParams _otaProviderDelegateBridge = new MTROTAProviderDelegateBridge(startupParams.otaProviderDelegate); if (_otaProviderDelegateBridge == nil) { MTR_LOG_ERROR("Error: %@", kErrorOtaProviderInit); + errorCode = CHIP_ERROR_NO_MEMORY; return; } } @@ -279,6 +295,7 @@ - (BOOL)startup:(MTRControllerFactoryParams *)startupParams _keystore = new PersistentStorageOperationalKeystore(); if (_keystore == nullptr) { MTR_LOG_ERROR("Error: %@", kErrorKeystoreInit); + errorCode = CHIP_ERROR_NO_MEMORY; return; } @@ -292,6 +309,7 @@ - (BOOL)startup:(MTRControllerFactoryParams *)startupParams _opCertStore = new Credentials::PersistentStorageOpCertStore(); if (_opCertStore == nullptr) { MTR_LOG_ERROR("Error: %@", kErrorCertStoreInit); + errorCode = CHIP_ERROR_NO_MEMORY; return; } @@ -307,6 +325,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; @@ -317,6 +336,7 @@ - (BOOL)startup:(MTRControllerFactoryParams *)startupParams _deviceAttestationVerifier = new Credentials::DefaultDACVerifier(trustStore); if (_deviceAttestationVerifier == nullptr) { MTR_LOG_ERROR("Error: %@", kErrorDACVerifierInit); + errorCode = CHIP_ERROR_NO_MEMORY; return; } @@ -324,6 +344,7 @@ - (BOOL)startup:(MTRControllerFactoryParams *)startupParams auto cdTrustStore = _deviceAttestationVerifier->GetCertificationDeclarationTrustStore(); if (cdTrustStore == nullptr) { MTR_LOG_ERROR("Error: %@", kErrorCDCertStoreInit); + errorCode = CHIP_ERROR_INCORRECT_STATE; return; } @@ -340,7 +361,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; } @@ -367,7 +388,7 @@ - (BOOL)startup:(MTRControllerFactoryParams *)startupParams _controllerFactory->RetainSystemState(); _controllerFactory->ReleaseSystemState(); - 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. @@ -375,12 +396,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; @@ -399,12 +423,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; } @@ -413,10 +438,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; @@ -426,11 +455,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; } @@ -439,11 +470,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; } } @@ -452,22 +485,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"); @@ -488,10 +538,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; @@ -501,30 +555,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 @@ -611,7 +684,7 @@ - (MTRDeviceController * _Nullable)maybeInitializeOTAProvider:(MTRDeviceControll @end -@implementation MTRControllerFactory (InternalMethods) +@implementation MTRDeviceControllerFactory (InternalMethods) - (void)controllerShuttingDown:(MTRDeviceController *)controller { @@ -678,7 +751,7 @@ - (MTRPersistentStorageDelegateBridge *)storageDelegateBridge @end -@implementation MTRControllerFactoryParams +@implementation MTRDeviceControllerFactoryParams - (instancetype)initWithStorage:(id)storage { @@ -691,14 +764,58 @@ - (instancetype)initWithStorage:(id)storage _paaCerts = nil; _cdCerts = nil; _port = nil; - _startServer = NO; + _shouldStartServer = NO; return self; } @end -@implementation MTRControllerFactoryParams (Deprecated) +@implementation MTRControllerFactory +- (BOOL)isRunning +{ + return [[MTRDeviceControllerFactory sharedInstance] isRunning]; +} + ++ (instancetype)sharedInstance +{ + // We could try to delegate to MTRDeviceControllerFactory's sharedInstance + // here, but then we would have to add the backwards-compar selectors to + // MTRDeviceControllerFactory, etc. Just forward things along instead. + // This works because we never accept an MTRControllerFactory as an argument + // in any of our public APIs. + static MTRControllerFactory * factory = nil; + static dispatch_once_t onceToken; + dispatch_once(&onceToken, ^{ + // initialize the factory. + factory = [[MTRControllerFactory alloc] init]; + }); + return factory; +} + +- (BOOL)startup:(MTRControllerFactoryParams *)startupParams +{ + return [[MTRDeviceControllerFactory sharedInstance] startControllerFactory:startupParams error:nil]; +} + +- (void)shutdown +{ + return [[MTRDeviceControllerFactory sharedInstance] stopControllerFactory]; +} + +- (MTRDeviceController * _Nullable)startControllerOnExistingFabric:(MTRDeviceControllerStartupParams *)startupParams +{ + return [[MTRDeviceControllerFactory sharedInstance] createControllerOnExistingFabric:startupParams error:nil]; +} + +- (MTRDeviceController * _Nullable)startControllerOnNewFabric:(MTRDeviceControllerStartupParams *)startupParams +{ + return [[MTRDeviceControllerFactory sharedInstance] createControllerOnNewFabric:startupParams error:nil]; +} + +@end + +@implementation MTRControllerFactoryParams - (id)storageDelegate { @@ -708,4 +825,14 @@ @implementation MTRControllerFactoryParams (Deprecated) return static_cast>(self.storage); } +- (BOOL)startServer +{ + return self.shouldStartServer; +} + +- (void)setStartServer:(BOOL)startServer +{ + self.shouldStartServer = startServer; +} + @end 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 c9733f08701bcc..dfafee444916aa 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 MTR_NEWLY_AVAILABLE; -// TODO: Add something here for CATs? - /** * Root certificate, in X.509 DER form, to use. * @@ -198,16 +197,14 @@ NS_ASSUME_NONNULL_BEGIN * * ipk must be 16 bytes in length */ -- (instancetype)initWithSigningKeypair:(id)nocSigner - fabricID:(NSNumber *)fabricID - ipk:(NSData *)ipk MTR_NEWLY_AVAILABLE; +- (instancetype)initWithIPK:(NSData *)ipk fabricID:(NSNumber *)fabricID nocSigner:(id)nocSigner MTR_NEWLY_AVAILABLE; /** * 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 @@ -215,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 MTR_NEWLY_AVAILABLE; @end @@ -231,7 +228,14 @@ NS_ASSUME_NONNULL_BEGIN - (instancetype)initWithSigningKeypair:(id)nocSigner fabricId:(uint64_t)fabricId - ipk:(NSData *)ipk MTR_NEWLY_DEPRECATED("Please use initWithSigningKeypair:fabricID:ipk:"); + ipk:(NSData *)ipk MTR_NEWLY_DEPRECATED("Please use initWithIPK:fabricID:nocSigner:"); +- (instancetype)initWithOperationalKeypair:(id)operationalKeypair + operationalCertificate:(MTRCertificateDERBytes)operationalCertificate + intermediateCertificate:(MTRCertificateDERBytes _Nullable)intermediateCertificate + rootCertificate:(MTRCertificateDERBytes)rootCertificate + ipk:(NSData *)ipk + MTR_NEWLY_DEPRECATED( + "Please use initWithIPK:operationalKeypair:operationalCertificate:intermediateCertificate:rootCertificate:"); @end diff --git a/src/darwin/Framework/CHIP/MTRDeviceControllerStartupParams.mm b/src/darwin/Framework/CHIP/MTRDeviceControllerStartupParams.mm index dd5512929b2aab..c2f0242d6ffc20 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; @@ -152,7 +152,20 @@ - (void)setNodeId:(nullable NSNumber *)nodeId - (instancetype)initWithSigningKeypair:(id)nocSigner fabricId:(uint64_t)fabricId ipk:(NSData *)ipk { - return [self initWithSigningKeypair:nocSigner fabricID:@(fabricId) ipk:ipk]; + return [self initWithIPK:ipk fabricID:@(fabricId) nocSigner:nocSigner]; +} + +- (instancetype)initWithOperationalKeypair:(id)operationalKeypair + operationalCertificate:(MTRCertificateDERBytes)operationalCertificate + intermediateCertificate:(MTRCertificateDERBytes _Nullable)intermediateCertificate + rootCertificate:(MTRCertificateDERBytes)rootCertificate + ipk:(NSData *)ipk +{ + return [self initWithIPK:ipk + operationalKeypair:operationalKeypair + operationalCertificate:operationalCertificate + intermediateCertificate:intermediateCertificate + rootCertificate:rootCertificate]; } @end diff --git a/src/darwin/Framework/CHIP/MTRDeviceControllerStartupParams_Internal.h b/src/darwin/Framework/CHIP/MTRDeviceControllerStartupParams_Internal.h index 9a4d3ead0359a6..79751cc95362e8 100644 --- a/src/darwin/Framework/CHIP/MTRDeviceControllerStartupParams_Internal.h +++ b/src/darwin/Framework/CHIP/MTRDeviceControllerStartupParams_Internal.h @@ -83,12 +83,16 @@ 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; +/** + * Should use initForExistingFabric or initForNewFabric to initialize + * internally. + */ +- (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 bb20c6b397c38a..d787aa02c329b1 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,16 +92,16 @@ 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 MTRControllerFactory has finished shutting down + * 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 MTRControllerFactory should be calling this. + * Only MTRDeviceControllerFactory should be calling this. */ - (void)deinitFromFactory; diff --git a/src/darwin/Framework/CHIP/MTROTAProviderDelegateBridge.mm b/src/darwin/Framework/CHIP/MTROTAProviderDelegateBridge.mm index 8d82a33d6940df..c2ce1b39548b47 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()); @@ -186,7 +186,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()); @@ -236,7 +236,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()); @@ -387,7 +387,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 c42a242c6b1859..dc71d1cb998dd5 100644 --- a/src/darwin/Framework/CHIP/Matter.h +++ b/src/darwin/Framework/CHIP/Matter.h @@ -37,11 +37,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..78169202dd9a94 100644 --- a/src/darwin/Framework/CHIPTests/MTRControllerTests.m +++ b/src/darwin/Framework/CHIPTests/MTRControllerTests.m @@ -37,27 +37,73 @@ @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 = [MTRDeviceControllerFactory sharedInstance]; + XCTAssertNotNil(factory); + + __auto_type * storage = [[MTRTestStorage alloc] init]; + __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] initWithIPK:testKeys.ipk fabricID:@(1) nocSigner:testKeys]; + XCTAssertNotNil(params); + + params.vendorID = @(kTestVendorId); + + MTRDeviceController * controller = [factory createControllerOnNewFabric:params error:nil]; + XCTAssertNotNil(controller); + XCTAssertTrue([controller isRunning]); + + [controller shutdown]; + XCTAssertFalse([controller isRunning]); + + // now try to restart the controller + controller = [factory createControllerOnExistingFabric:params error:nil]; + XCTAssertNotNil(controller); + XCTAssertTrue([controller isRunning]); + + [controller shutdown]; + XCTAssertFalse([controller isRunning]); + + // now try to restart the controller without providing a vendor id. + params.vendorID = nil; + controller = [factory createControllerOnExistingFabric:params error:nil]; + XCTAssertNotNil(controller); + XCTAssertTrue([controller isRunning]); + + [controller shutdown]; + XCTAssertFalse([controller isRunning]); + + [factory stopControllerFactory]; + XCTAssertFalse([factory isRunning]); +} + +- (void)testDeprecatedControllerLifecycle { __auto_type * factory = [MTRControllerFactory sharedInstance]; XCTAssertNotNil(factory); @@ -70,9 +116,7 @@ - (void)testControllerLifecycle __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); @@ -107,145 +151,135 @@ - (void)testControllerLifecycle - (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 +290,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 +321,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 +404,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 +430,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 +446,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 +460,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 +469,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 +495,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 +515,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 +525,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 +551,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 +559,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 +593,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 +601,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 +694,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 +703,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 +738,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 +749,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 +758,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 +801,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 +833,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 +850,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 +863,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 +895,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 +905,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 +915,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 +927,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 +970,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 +980,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 +990,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 +1003,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 +1034,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 +1089,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 +1108,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 +1121,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 +1153,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 +1171,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 +1203,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 +1258,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 +1290,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 +1309,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 +1330,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 +1339,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 c7a7865f22093d..c101fa1195dabe 100644 --- a/src/darwin/Framework/CHIPTests/MTRDeviceTests.m +++ b/src/darwin/Framework/CHIPTests/MTRDeviceTests.m @@ -161,14 +161,14 @@ - (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]; @@ -178,12 +178,10 @@ - (void)initStack // Needs to match what startControllerOnExistingFabric calls elsewhere in // this file do. - __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; @@ -222,7 +220,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 94538f1cd3efca..61026255cb55cc 100644 --- a/src/darwin/Framework/CHIPTests/MTRXPCListenerSampleTests.m +++ b/src/darwin/Framework/CHIPTests/MTRXPCListenerSampleTests.m @@ -514,26 +514,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; @@ -543,7 +544,6 @@ - (void)initStack [controller setPairingDelegate:pairing queue:callbackQueue]; - NSError * error; __auto_type * payload = [MTRSetupPayload setupPayloadWithOnboardingPayload:kOnboardingPayload error:&error]; XCTAssertNotNil(payload); XCTAssertNil(error); @@ -579,7 +579,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 aea7aed36ad78d..c1d91653c4e951 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 */; }; @@ -183,9 +183,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 = ""; }; @@ -423,12 +423,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 */, @@ -499,7 +499,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 */, @@ -510,7 +510,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 */, @@ -691,7 +691,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 */,