Skip to content

Commit

Permalink
Switch MTRDeviceControllerFactory to storing MTRDeviceController_Conc…
Browse files Browse the repository at this point in the history
…rete instances.

All the controllers it works with are concrete ones.
  • Loading branch information
bzbarsky-apple committed Sep 20, 2024
1 parent d63051a commit e14ecf7
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 33 deletions.
54 changes: 27 additions & 27 deletions src/darwin/Framework/CHIP/MTRDeviceControllerFactory.mm
Original file line number Diff line number Diff line change
Expand Up @@ -150,9 +150,9 @@ @implementation MTRDeviceControllerFactory {
// must lock.
// D. Locking around reads not from the Matter queue is OK but not required.
os_unfair_lock _controllersLock;
NSMutableArray<MTRDeviceController *> * _controllers;
MTRDeviceController * _controllerBeingStarted;
MTRDeviceController * _controllerBeingShutDown;
NSMutableArray<MTRDeviceController_Concrete *> * _controllers;
MTRDeviceController_Concrete * _controllerBeingStarted;
MTRDeviceController_Concrete * _controllerBeingShutDown;

// Next available fabric index. Only valid when _controllerBeingStarted is
// non-nil, and then it corresponds to the controller being started. This
Expand Down Expand Up @@ -461,12 +461,12 @@ - (void)stopControllerFactory
* The provided controller is expected to have just been allocated and to not be
* initialized yet.
*/
- (MTRDeviceController * _Nullable)_startDeviceController:(MTRDeviceController_Concrete *)controller
startupParams:(id)startupParams
fabricChecker:(MTRDeviceControllerStartupParamsInternal * (^)(FabricTable * fabricTable,
MTRDeviceController * controller,
CHIP_ERROR & fabricError))fabricChecker
error:(NSError * __autoreleasing *)error
- (MTRDeviceController_Concrete * _Nullable)_startDeviceController:(MTRDeviceController_Concrete *)controller
startupParams:(id)startupParams
fabricChecker:(MTRDeviceControllerStartupParamsInternal * (^)(FabricTable * fabricTable,
MTRDeviceController_Concrete * controller,
CHIP_ERROR & fabricError))fabricChecker
error:(NSError * __autoreleasing *)error
{
[self _assertCurrentQueueIsNotMatterQueue];

Expand Down Expand Up @@ -590,7 +590,7 @@ - (MTRDeviceController * _Nullable)_startDeviceController:(MTRDeviceController_C
// Check that we are not trying to start a controller with a uniqueIdentifier that
// matches a running controller.
auto * controllersCopy = [self getRunningControllers];
for (MTRDeviceController * existing in controllersCopy) {
for (MTRDeviceController_Concrete * existing in controllersCopy) {
if (existing != controller && [existing.uniqueIdentifier isEqual:params.uniqueIdentifier]) {
MTR_LOG_ERROR("Already have running controller with uniqueIdentifier %@", existing.uniqueIdentifier);
fabricError = CHIP_ERROR_INVALID_ARGUMENT;
Expand Down Expand Up @@ -661,15 +661,15 @@ - (MTRDeviceController * _Nullable)createControllerOnExistingFabric:(MTRDeviceCo
}

// 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];
auto * existingController = [self _findPendingShutdownControllerWithOperationalCertificate:startupParams.operationalCertificate andRootCertificate:startupParams.rootCertificate];
if (existingController) {
return existingController;
}

return [self _startDeviceController:[MTRDeviceController_Concrete alloc]
startupParams:startupParams
fabricChecker:^MTRDeviceControllerStartupParamsInternal *(
FabricTable * fabricTable, MTRDeviceController * controller, CHIP_ERROR & fabricError) {
FabricTable * fabricTable, MTRDeviceController_Concrete * controller, CHIP_ERROR & fabricError) {
const FabricInfo * fabric = nullptr;
BOOL ok = [self findMatchingFabric:*fabricTable params:startupParams fabric:&fabric];
if (!ok) {
Expand All @@ -686,7 +686,7 @@ - (MTRDeviceController * _Nullable)createControllerOnExistingFabric:(MTRDeviceCo

auto * controllersCopy = [self getRunningControllers];

for (MTRDeviceController * existing in controllersCopy) {
for (MTRDeviceController_Concrete * existing in controllersCopy) {
BOOL isRunning = YES; // assume the worst
if ([existing isRunningOnFabric:fabricTable
fabricIndex:fabric->GetFabricIndex()
Expand Down Expand Up @@ -741,7 +741,7 @@ - (MTRDeviceController * _Nullable)createControllerOnNewFabric:(MTRDeviceControl
return [self _startDeviceController:[MTRDeviceController_Concrete alloc]
startupParams:startupParams
fabricChecker:^MTRDeviceControllerStartupParamsInternal *(
FabricTable * fabricTable, MTRDeviceController * controller, CHIP_ERROR & fabricError) {
FabricTable * fabricTable, MTRDeviceController_Concrete * controller, CHIP_ERROR & fabricError) {
const FabricInfo * fabric = nullptr;
BOOL ok = [self findMatchingFabric:*fabricTable params:startupParams fabric:&fabric];
if (!ok) {
Expand Down Expand Up @@ -969,7 +969,7 @@ - (void)controllerShuttingDown:(MTRDeviceController_Concrete *)controller
[controller deinitFromFactory];
}

- (NSArray<MTRDeviceController *> *)getRunningControllers
- (NSArray<MTRDeviceController_Concrete *> *)getRunningControllers
{
std::lock_guard lock(_controllersLock);
return [_controllers copy];
Expand All @@ -984,11 +984,11 @@ - (nullable MTRDeviceController *)runningControllerForFabricIndex:(FabricIndex)f
auto * controllersCopy = [self getRunningControllers];

os_unfair_lock_lock(&_controllersLock);
MTRDeviceController * controllerBeingStarted = _controllerBeingStarted;
MTRDeviceController * controllerBeingShutDown = _controllerBeingShutDown;
auto * controllerBeingStarted = _controllerBeingStarted;
auto * controllerBeingShutDown = _controllerBeingShutDown;
os_unfair_lock_unlock(&_controllersLock);

for (MTRDeviceController * existing in controllersCopy) {
for (MTRDeviceController_Concrete * existing in controllersCopy) {
if (existing.fabricIndex == fabricIndex) {
return existing;
}
Expand Down Expand Up @@ -1088,7 +1088,7 @@ - (nullable NSNumber *)neededReadPrivilegeForClusterID:(NSNumber *)clusterID att
}
}

for (MTRDeviceController * controller in [self getRunningControllers]) {
for (MTRDeviceController_Concrete * controller in [self getRunningControllers]) {
NSNumber * _Nullable neededPrivilege = [controller neededReadPrivilegeForClusterID:clusterID attributeID:attributeID];
if (neededPrivilege != nil) {
return neededPrivilege;
Expand Down Expand Up @@ -1127,7 +1127,7 @@ - (void)operationalInstanceAdded:(chip::PeerId &)operationalID

auto * controllersCopy = [self getRunningControllers];

for (MTRDeviceController * controller in controllersCopy) {
for (MTRDeviceController_Concrete * controller in controllersCopy) {
auto * compressedFabricId = controller.compressedFabricID;
if (compressedFabricId != nil && compressedFabricId.unsignedLongLongValue == operationalID.GetCompressedFabricId()) {
ChipLogProgress(Controller, "Notifying controller at fabric index %u about new operational node 0x" ChipLogFormatX64,
Expand All @@ -1140,10 +1140,10 @@ - (void)operationalInstanceAdded:(chip::PeerId &)operationalID
}
}

- (nullable MTRDeviceController *)_findPendingShutdownControllerWithOperationalCertificate:(nullable MTRCertificateDERBytes)operationalCertificate andRootCertificate:(nullable MTRCertificateDERBytes)rootCertificate
- (nullable MTRDeviceController_Concrete *)_findPendingShutdownControllerWithOperationalCertificate:(nullable MTRCertificateDERBytes)operationalCertificate andRootCertificate:(nullable MTRCertificateDERBytes)rootCertificate
{
std::lock_guard lock(_controllersLock);
for (MTRDeviceController * controller in _controllers) {
for (MTRDeviceController_Concrete * controller in _controllers) {
// TODO: Once we know our controllers are MTRDeviceController_Concrete, move
// matchesPendingShutdownControllerWithOperationalCertificate and clearPendingShutdown to that
// interface and remove them from base MTRDeviceController_Internal.
Expand All @@ -1156,22 +1156,22 @@ - (nullable MTRDeviceController *)_findPendingShutdownControllerWithOperationalC
return nil;
}

- (nullable MTRDeviceController *)initializeController:(MTRDeviceController_Concrete *)controller
withParameters:(MTRDeviceControllerParameters *)parameters
error:(NSError * __autoreleasing *)error
- (nullable MTRDeviceController_Concrete *)initializeController:(MTRDeviceController_Concrete *)controller
withParameters:(MTRDeviceControllerParameters *)parameters
error:(NSError * __autoreleasing *)error
{
[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 _findPendingShutdownControllerWithOperationalCertificate:parameters.operationalCertificate andRootCertificate:parameters.rootCertificate];
MTRDeviceController_Concrete * existingController = [self _findPendingShutdownControllerWithOperationalCertificate:parameters.operationalCertificate andRootCertificate:parameters.rootCertificate];
if (existingController) {
return existingController;
}

return [self _startDeviceController:controller
startupParams:parameters
fabricChecker:^MTRDeviceControllerStartupParamsInternal *(
FabricTable * fabricTable, MTRDeviceController * controller, CHIP_ERROR & fabricError) {
FabricTable * fabricTable, MTRDeviceController_Concrete * controller, CHIP_ERROR & fabricError) {
auto advertiseOperational = self->_advertiseOperational && parameters.shouldAdvertiseOperational;
auto * params =
[[MTRDeviceControllerStartupParamsInternal alloc] initForNewController:controller
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -92,9 +92,9 @@ MTR_DIRECT_MEMBERS
/**
* Initialize an MTRDeviceController_Concrete with the given parameters.
*/
- (nullable MTRDeviceController *)initializeController:(MTRDeviceController_Concrete *)controller
withParameters:(MTRDeviceControllerParameters *)parameters
error:(NSError * __autoreleasing *)error;
- (nullable MTRDeviceController_Concrete *)initializeController:(MTRDeviceController_Concrete *)controller
withParameters:(MTRDeviceControllerParameters *)parameters
error:(NSError * __autoreleasing *)error;

/**
* Add a server endpoint. This will verify that there is no existing server
Expand Down
6 changes: 3 additions & 3 deletions src/darwin/Framework/CHIP/MTRDeviceController_Concrete.mm
Original file line number Diff line number Diff line change
Expand Up @@ -157,9 +157,9 @@ - (nullable instancetype)initWithParameters:(MTRDeviceControllerAbstractParamete

// Start us up normally. MTRDeviceControllerFactory will auto-start in per-controller-storage mode if necessary.
MTRDeviceControllerFactory * factory = MTRDeviceControllerFactory.sharedInstance;
id controller = [factory initializeController:self
withParameters:controllerParameters
error:error];
auto * controller = [factory initializeController:self
withParameters:controllerParameters
error:error];
return controller;
}

Expand Down

0 comments on commit e14ecf7

Please sign in to comment.