Skip to content

Commit

Permalink
[Darwin] Find cached controllers when creating existing controller on…
Browse files Browse the repository at this point in the history
… fabric as well (#35205)

- Missed another call site in #35148
  • Loading branch information
anush-apple authored Aug 26, 2024
1 parent 7ddba36 commit aa508e6
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 9 deletions.
10 changes: 5 additions & 5 deletions src/darwin/Framework/CHIP/MTRDeviceController.mm
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand Down
12 changes: 9 additions & 3 deletions src/darwin/Framework/CHIP/MTRDeviceControllerFactory.mm
Original file line number Diff line number Diff line change
Expand Up @@ -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 *(
Expand Down Expand Up @@ -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;
Expand All @@ -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;
}
Expand Down
2 changes: 1 addition & 1 deletion src/darwin/Framework/CHIP/MTRDeviceController_Internal.h
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down

0 comments on commit aa508e6

Please sign in to comment.