Skip to content

Commit

Permalink
Clean up MTRDeviceController initWithParameters dispatch. (project-ch…
Browse files Browse the repository at this point in the history
  • Loading branch information
bzbarsky-apple authored Sep 19, 2024
1 parent 9491d8f commit 8318df9
Show file tree
Hide file tree
Showing 5 changed files with 45 additions and 61 deletions.
31 changes: 22 additions & 9 deletions src/darwin/Framework/CHIP/MTRDeviceController.mm
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,10 @@
#import "MTRDeviceControllerLocalTestStorage.h"
#import "MTRDeviceControllerStartupParams.h"
#import "MTRDeviceControllerStartupParams_Internal.h"
#import "MTRDeviceControllerXPCParameters.h"
#import "MTRDeviceController_Concrete.h"
#import "MTRDeviceController_XPC.h"
#import "MTRDeviceController_XPC_Internal.h"
#import "MTRDevice_Concrete.h"
#import "MTRDevice_Internal.h"
#import "MTRError_Internal.h"
Expand Down Expand Up @@ -190,20 +192,31 @@ - (instancetype)initForSubclasses:(BOOL)startSuspended

- (nullable MTRDeviceController *)initWithParameters:(MTRDeviceControllerAbstractParameters *)parameters error:(NSError * __autoreleasing *)error
{
// Dispatch to the right non-abstract implementation.
if ([parameters isKindOfClass:MTRXPCDeviceControllerParameters.class]) {
MTR_LOG("Starting up with XPC Device Controller Parameters: %@", parameters);
return [[MTRDeviceController_XPC alloc] initWithParameters:parameters error:error];
} else 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);

// MTRDeviceControllerFactory will auto-start in per-controller-storage mode if necessary
return [MTRDeviceControllerFactory.sharedInstance initializeController:[MTRDeviceController_Concrete alloc] withParameters:controllerParameters error:error];
if ([parameters isKindOfClass:MTRDeviceControllerMachServiceXPCParameters.class]) {
// TODO: This will need to at least pass in the uniqueIdentifier, no? initWithMachServiceName:options: seems to
// be declared but not actually implemented...
auto * xpcParameters = static_cast<MTRDeviceControllerMachServiceXPCParameters *>(parameters);

MTR_LOG("Starting up with Mach Service XPC Device Controller Parameters: %@", parameters);
return [[MTRDeviceController_XPC alloc] initWithMachServiceName:xpcParameters.machServiceName options:xpcParameters.connectionOptions];
}

if ([parameters isKindOfClass:MTRDeviceControllerParameters.class]) {
MTR_LOG("Starting up with Device Controller Parameters: %@", parameters);
return [[MTRDeviceController_Concrete alloc] initWithParameters:parameters error:error];
}

MTR_LOG_ERROR("Unsupported type of MTRDeviceControllerAbstractParameters: %@", parameters);
if (error) {
*error = [MTRError errorForCHIPErrorCode:CHIP_ERROR_INVALID_ARGUMENT];
}
return nil;
}

- (instancetype)initWithFactory:(MTRDeviceControllerFactory *)factory
Expand Down
6 changes: 3 additions & 3 deletions src/darwin/Framework/CHIP/MTRDeviceControllerXPCParameters.h
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,11 @@

NS_ASSUME_NONNULL_BEGIN

@interface MTRDeviceControllerXPCParameters : MTRDeviceControllerParameters
@end
@interface MTRDeviceControllerMachServiceXPCParameters : MTRDeviceControllerAbstractParameters

@interface MTRDeviceControllerMachServiceXPCParameters : MTRDeviceControllerXPCParameters
- (nullable instancetype)initWithUniqueIdentifier:(NSUUID *)uniqueIdentifier;

@property (atomic, retain) NSUUID * uniqueIdentifier;
@property (atomic, retain) NSString * machServiceName;
@property (atomic, readwrite) NSXPCConnectionOptions connectionOptions;

Expand Down
16 changes: 9 additions & 7 deletions src/darwin/Framework/CHIP/MTRDeviceControllerXPCParameters.mm
Original file line number Diff line number Diff line change
Expand Up @@ -15,17 +15,19 @@
*/

#import "MTRDeviceControllerXPCParameters.h"
#import "MTRDeviceControllerStartupParams_Internal.h"

@implementation MTRDeviceControllerXPCParameters
@implementation MTRDeviceControllerMachServiceXPCParameters

+ (BOOL)supportsSecureCoding
- (nullable instancetype)initWithUniqueIdentifier:(NSUUID *)uniqueIdentifier
{
return YES;
}
if (!(self = [super _initInternal])) {
return nil;
}

@end

@implementation MTRDeviceControllerMachServiceXPCParameters
_uniqueIdentifier = uniqueIdentifier;
return self;
}

+ (BOOL)supportsSecureCoding
{
Expand Down
52 changes: 11 additions & 41 deletions src/darwin/Framework/CHIP/MTRDeviceController_Concrete.mm
Original file line number Diff line number Diff line change
Expand Up @@ -31,10 +31,7 @@
#import "MTRDeviceControllerLocalTestStorage.h"
#import "MTRDeviceControllerStartupParams.h"
#import "MTRDeviceControllerStartupParams_Internal.h"
#import "MTRDeviceControllerXPCParameters.h"
#import "MTRDeviceController_Concrete.h"
#import "MTRDeviceController_XPC.h"
#import "MTRDeviceController_XPC_Internal.h"
#import "MTRDevice_Concrete.h"
#import "MTRDevice_Internal.h"
#import "MTRError_Internal.h"
Expand Down Expand Up @@ -148,49 +145,22 @@ @implementation MTRDeviceController_Concrete {
- (nullable instancetype)initWithParameters:(MTRDeviceControllerAbstractParameters *)parameters
error:(NSError * __autoreleasing *)error
{
/// IF YOU ARE ALARMED BY TYPES: You are right to be alarmed, but do not panic.
/// _ORDER MATTERS HERE:_ XPC parameters are a subclass of `MTRDeviceControllerParameters`
/// because of the enormous overlap of params.
if ([parameters isKindOfClass:MTRDeviceControllerXPCParameters.class]) {
if ([parameters isKindOfClass:MTRDeviceControllerMachServiceXPCParameters.class]) {
MTRDeviceControllerMachServiceXPCParameters * xpcParameters = (MTRDeviceControllerMachServiceXPCParameters *) parameters;
MTR_LOG_DEBUG("%s: got XPC parameters, getting XPC device controller", __PRETTY_FUNCTION__);

NSString * machServiceName = xpcParameters.machServiceName;
MTR_LOG_DEBUG("%s: machServiceName %@", __PRETTY_FUNCTION__, machServiceName);

MTRDeviceController * xpcDeviceController = [[MTRDeviceController_XPC alloc] initWithMachServiceName:machServiceName options:xpcParameters.connectionOptions];

/// Being of sound mind, I willfully and voluntarily make this static cast.
return static_cast<MTRDeviceController_Concrete *>(xpcDeviceController);
} else {
MTR_LOG_ERROR("%s: unrecognized XPC parameters class %@", __PRETTY_FUNCTION__, NSStringFromClass(parameters.class));

// TODO: there's probably a more appropriate error here.
if (error) {
*error = [MTRError errorForCHIPErrorCode:CHIP_ERROR_NOT_IMPLEMENTED];
}

return nil;
}
} else if ([parameters isKindOfClass:MTRDeviceControllerParameters.class]) {
MTR_LOG_DEBUG("%s: got standard parameters, getting standard device controller from factory", __PRETTY_FUNCTION__);
auto * controllerParameters = static_cast<MTRDeviceControllerParameters *>(parameters);

// 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];
return controller;
} else {
// way out of our league
MTR_LOG_ERROR("Unsupported type of MTRDeviceControllerAbstractParameters: %@", parameters);
if (![parameters isKindOfClass:MTRDeviceControllerParameters.class]) {
MTR_LOG_ERROR("Expected MTRDeviceControllerParameters but got: %@", parameters);
if (error) {
*error = [MTRError errorForCHIPErrorCode:CHIP_ERROR_INVALID_ARGUMENT];
}
return nil;
}

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

// 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];
return controller;
}

- (instancetype)initWithFactory:(MTRDeviceControllerFactory *)factory
Expand Down
1 change: 0 additions & 1 deletion src/darwin/Framework/CHIP/MTRDevice_XPC.mm
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,6 @@
#import "MTRDeviceControllerLocalTestStorage.h"
#import "MTRDeviceControllerStartupParams.h"
#import "MTRDeviceControllerStartupParams_Internal.h"
#import "MTRDeviceControllerXPCParameters.h"
#import "MTRDeviceController_Concrete.h"
#import "MTRDeviceController_XPC.h"
#import "MTRDevice_Concrete.h"
Expand Down

0 comments on commit 8318df9

Please sign in to comment.