Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Darwin: Refactor stack startup logic when creating a controller #32845

Merged
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 3 additions & 13 deletions src/darwin/Framework/CHIP/MTRDeviceController.mm
Original file line number Diff line number Diff line change
Expand Up @@ -138,25 +138,15 @@ - (nullable instancetype)initWithParameters:(MTRDeviceControllerAbstractParamete
{
if (![parameters isKindOfClass:MTRDeviceControllerParameters.class]) {
MTR_LOG_ERROR("Unsupported type of MTRDeviceControllerAbstractParameters: %@", parameters);

if (error) {
*error = [MTRError errorForCHIPErrorCode:CHIP_ERROR_INVALID_ARGUMENT];
}
return nil;
}
auto * controllerParameters = static_cast<MTRDeviceControllerParameters *>(parameters);

__auto_type * factory = [MTRDeviceControllerFactory sharedInstance];
if (!factory.isRunning) {
auto * params = [[MTRDeviceControllerFactoryParams alloc] initWithoutStorage];

if (![factory startControllerFactory:params error:error]) {
return nil;
}
}

auto * parametersForFactory = static_cast<MTRDeviceControllerParameters *>(parameters);

return [factory initializeController:self withParameters:parametersForFactory error:error];
// MTRDeviceControllerFactory will auto-start in per-controller-storage mode if necessary
return [MTRDeviceControllerFactory.sharedInstance initializeController:self withParameters:controllerParameters error:error];
}

- (instancetype)initWithFactory:(MTRDeviceControllerFactory *)factory
Expand Down
39 changes: 31 additions & 8 deletions src/darwin/Framework/CHIP/MTRDeviceControllerFactory.mm
Original file line number Diff line number Diff line change
Expand Up @@ -418,7 +418,14 @@ - (CHIP_ERROR)_initFabricTable:(FabricTable &)fabricTable
return [NSArray arrayWithArray:fabricList];
}

- (BOOL)startControllerFactory:(MTRDeviceControllerFactoryParams *)startupParams error:(NSError * __autoreleasing *)error;
- (BOOL)startControllerFactory:(MTRDeviceControllerFactoryParams *)startupParams error:(NSError * __autoreleasing *)error
{
return [self _startControllerFactory:startupParams startingController:NO error:error];
}

- (BOOL)_startControllerFactory:(MTRDeviceControllerFactoryParams *)startupParams
startingController:(BOOL)startingController
error:(NSError * __autoreleasing *)error
{
[self _assertCurrentQueueIsNotMatterQueue];

Expand Down Expand Up @@ -536,8 +543,10 @@ - (BOOL)startControllerFactory:(MTRDeviceControllerFactoryParams *)startupParams
// state is brought up live on factory init, and not when it comes time
// to actually start a controller, and does not actually clean itself up
// until its refcount (which starts as 0) goes to 0.
_controllerFactory->RetainSystemState();
_controllerFactory->ReleaseSystemState();
if (!startingController) {
_controllerFactory->RetainSystemState();
_controllerFactory->ReleaseSystemState();
ksperling-apple marked this conversation as resolved.
Show resolved Hide resolved
}

self->_advertiseOperational = startupParams.shouldStartServer;
self->_running = YES;
Expand Down Expand Up @@ -598,11 +607,6 @@ - (MTRDeviceController * _Nullable)_startDeviceController:(MTRDeviceController *
{
[self _assertCurrentQueueIsNotMatterQueue];

if (![self checkIsRunning:error]) {
MTR_LOG_ERROR("Trying to start controller while Matter controller factory is not running");
return nil;
}

id<MTRDeviceControllerStorageDelegate> _Nullable storageDelegate;
dispatch_queue_t _Nullable storageDelegateQueue;
NSUUID * uniqueIdentifier;
Expand All @@ -624,9 +628,28 @@ - (MTRDeviceController * _Nullable)_startDeviceController:(MTRDeviceController *
otaProviderDelegateQueue = nil;
} else {
MTR_LOG_ERROR("Unknown kind of startup params: %@", startupParams);
if (error != nil) {
*error = [MTRError errorForCHIPErrorCode:CHIP_ERROR_INVALID_ARGUMENT];
}
return nil;
}

if (![self isRunning]) {
if (storageDelegate != nil) {
MTR_LOG_DEFAULT("Auto-starting Matter controller factory in per-controller storage mode");
auto * params = [[MTRDeviceControllerFactoryParams alloc] initWithoutStorage];
if (![self _startControllerFactory:params startingController:YES error:error]) {
return nil;
}
} else {
MTR_LOG_ERROR("Trying to start controller while Matter controller factory is not running");
if (error != nil) {
*error = [MTRError errorForCHIPErrorCode:CHIP_ERROR_INCORRECT_STATE];
}
return nil;
}
}

if (_usingPerControllerStorage && storageDelegate == nil) {
MTR_LOG_ERROR("Must have a controller storage delegate when we do not have storage for the controller factory");
if (error != nil) {
Expand Down
Loading