diff --git a/src/darwin/Framework/CHIP/MTRDeviceController.mm b/src/darwin/Framework/CHIP/MTRDeviceController.mm index 4630ff7fe00b4f..650aba4b24a700 100644 --- a/src/darwin/Framework/CHIP/MTRDeviceController.mm +++ b/src/darwin/Framework/CHIP/MTRDeviceController.mm @@ -331,14 +331,14 @@ - (BOOL)isRunning return _cppCommissioner != nullptr; } -- (BOOL)matchesPendingShutdownWithParams:(MTRDeviceControllerParameters *)parameters +- (BOOL)matchesPendingShutdownControllerWithOperationalCertificate:(nullable MTRCertificateDERBytes)operationalCertificate andRootCertificate:(nullable MTRCertificateDERBytes)rootCertificate { - if (!parameters.operationalCertificate || !parameters.rootCertificate) { + if (!operationalCertificate || !rootCertificate) { return FALSE; } - NSNumber * nodeID = [MTRDeviceControllerParameters nodeIDFromNOC:parameters.operationalCertificate]; - NSNumber * fabricID = [MTRDeviceControllerParameters fabricIDFromNOC:parameters.operationalCertificate]; - NSData * publicKey = [MTRDeviceControllerParameters publicKeyFromCertificate:parameters.rootCertificate]; + NSNumber * nodeID = [MTRDeviceControllerParameters nodeIDFromNOC:operationalCertificate]; + NSNumber * fabricID = [MTRDeviceControllerParameters fabricIDFromNOC:operationalCertificate]; + NSData * publicKey = [MTRDeviceControllerParameters publicKeyFromCertificate:rootCertificate]; std::lock_guard lock(_assertionLock); diff --git a/src/darwin/Framework/CHIP/MTRDeviceControllerFactory.mm b/src/darwin/Framework/CHIP/MTRDeviceControllerFactory.mm index bd0b90450b9ac4..00b59f525b0f16 100644 --- a/src/darwin/Framework/CHIP/MTRDeviceControllerFactory.mm +++ b/src/darwin/Framework/CHIP/MTRDeviceControllerFactory.mm @@ -660,6 +660,12 @@ - (MTRDeviceController * _Nullable)createControllerOnExistingFabric:(MTRDeviceCo return nil; } + // If there is a controller already running with matching parameters that is conceptually shut down from the API consumer's viewpoint, re-use it. + MTRDeviceController * existingController = [self _findPendingShutdownControllerWithOperationalCertificate:startupParams.operationalCertificate andRootCertificate:startupParams.rootCertificate]; + if (existingController) { + return existingController; + } + return [self _startDeviceController:[MTRDeviceController alloc] startupParams:startupParams fabricChecker:^MTRDeviceControllerStartupParamsInternal *( @@ -1133,11 +1139,11 @@ - (void)operationalInstanceAdded:(chip::PeerId &)operationalID } } -- (nullable MTRDeviceController *)_findControllerWithPendingShutdownMatchingParams:(MTRDeviceControllerParameters *)parameters +- (nullable MTRDeviceController *)_findPendingShutdownControllerWithOperationalCertificate:(nullable MTRCertificateDERBytes)operationalCertificate andRootCertificate:(nullable MTRCertificateDERBytes)rootCertificate { std::lock_guard lock(_controllersLock); for (MTRDeviceController * controller in _controllers) { - if ([controller matchesPendingShutdownWithParams:parameters]) { + if ([controller matchesPendingShutdownControllerWithOperationalCertificate:operationalCertificate andRootCertificate:rootCertificate]) { MTR_LOG("%@ Found existing controller %@ that is pending shutdown and matching parameters, re-using it", self, controller); [controller clearPendingShutdown]; return controller; @@ -1153,7 +1159,7 @@ - (nullable MTRDeviceController *)initializeController:(MTRDeviceController *)co [self _assertCurrentQueueIsNotMatterQueue]; // If there is a controller already running with matching parameters that is conceptually shut down from the API consumer's viewpoint, re-use it. - MTRDeviceController * existingController = [self _findControllerWithPendingShutdownMatchingParams:parameters]; + MTRDeviceController * existingController = [self _findPendingShutdownControllerWithOperationalCertificate:parameters.operationalCertificate andRootCertificate:parameters.rootCertificate]; if (existingController) { return existingController; } diff --git a/src/darwin/Framework/CHIP/MTRDeviceController_Internal.h b/src/darwin/Framework/CHIP/MTRDeviceController_Internal.h index e625d13a80e77e..f37d6fbf08c6d9 100644 --- a/src/darwin/Framework/CHIP/MTRDeviceController_Internal.h +++ b/src/darwin/Framework/CHIP/MTRDeviceController_Internal.h @@ -322,7 +322,7 @@ NS_ASSUME_NONNULL_BEGIN /** * This method returns TRUE if this controller matches the fabric reference and node ID as listed in the parameters. */ -- (BOOL)matchesPendingShutdownWithParams:(MTRDeviceControllerParameters *)parameters; +- (BOOL)matchesPendingShutdownControllerWithOperationalCertificate:(nullable MTRCertificateDERBytes)operationalCertificate andRootCertificate:(nullable MTRCertificateDERBytes)rootCertificate; /** * Clear any pending shutdown request.