Skip to content

Commit

Permalink
Rebase and use updateControllerConfiguration, add asynchronous client…
Browse files Browse the repository at this point in the history
… protocol
  • Loading branch information
jtung-apple committed Oct 16, 2024
1 parent 5bc90f3 commit 8234e8c
Show file tree
Hide file tree
Showing 9 changed files with 97 additions and 51 deletions.
2 changes: 1 addition & 1 deletion src/darwin/Framework/CHIP/MTRDeviceController.mm
Original file line number Diff line number Diff line change
Expand Up @@ -714,7 +714,7 @@ - (NSUInteger)_iterateDelegateInfoWithBlock:(void (^_Nullable)(MTRDeviceControll
}
}

- (void)_callDelegatesWithBlock:(void (^_Nullable)(id<MTRDeviceControllerDelegate> delegate))block logString:(const char *)logString;
- (void)_callDelegatesWithBlock:(void (^_Nullable)(id<MTRDeviceControllerDelegate> delegate))block logString:(const char *)logString
{
NSUInteger delegatesCalled = [self _iterateDelegateInfoWithBlock:^(MTRDeviceControllerDelegateInfo * delegateInfo) {
id<MTRDeviceControllerDelegate> strongDelegate = delegateInfo.delegate;
Expand Down
7 changes: 7 additions & 0 deletions src/darwin/Framework/CHIP/MTRDeviceControllerDelegate.h
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,13 @@ MTR_AVAILABLE(ios(16.4), macos(13.3), watchos(9.4), tvos(16.4))
*/
- (void)controller:(MTRDeviceController *)controller
suspendedChangedTo:(BOOL)suspended MTR_NEWLY_AVAILABLE;

/**
* Notify the delegate when the isRunning state changed of the controller, after this happens
* the controller will be in the specified state.
*/
- (void)controller:(MTRDeviceController *)controller
isRunningChangedTo:(BOOL)isRunning MTR_NEWLY_AVAILABLE;
@end

typedef NS_ENUM(NSUInteger, MTRPairingStatus) {
Expand Down
12 changes: 12 additions & 0 deletions src/darwin/Framework/CHIP/MTRDeviceController_Concrete.mm
Original file line number Diff line number Diff line change
Expand Up @@ -488,6 +488,12 @@ - (void)shutDownCppController
if (_operationalCredentialsDelegate != nil) {
_operationalCredentialsDelegate->SetDeviceCommissioner(nullptr);
}

[self _callDelegatesWithBlock:^(id<MTRDeviceControllerDelegate> delegate) {
if ([delegate respondsToSelector:@selector(controller:isRunningChangedTo:)]) {
[delegate controller:self isRunningChangedTo:NO];
}
} logString:__PRETTY_FUNCTION__];
}

if (!self.suspended) {
Expand Down Expand Up @@ -747,6 +753,12 @@ - (BOOL)startup:(MTRDeviceControllerStartupParamsInternal *)startupParams
self->_deviceControllerDelegateBridge->setDelegate(self, selfDelegate, _chipWorkQueue);

MTR_LOG("%@ startup succeeded for nodeID 0x%016llX", self, self->_cppCommissioner->GetNodeId());

[self _callDelegatesWithBlock:^(id<MTRDeviceControllerDelegate> delegate) {
if ([delegate respondsToSelector:@selector(controller:isRunningChangedTo:)]) {
[delegate controller:self isRunningChangedTo:YES];
}
} logString:__PRETTY_FUNCTION__];
});

if (commissionerInitialized == NO) {
Expand Down
2 changes: 2 additions & 0 deletions src/darwin/Framework/CHIP/MTRDeviceController_Internal.h
Original file line number Diff line number Diff line change
Expand Up @@ -182,6 +182,8 @@ NS_ASSUME_NONNULL_BEGIN
- (MTRDevice *)_setupDeviceForNodeID:(NSNumber *)nodeID prefetchedClusterData:(nullable NSDictionary<MTRClusterPath *, MTRDeviceClusterData *> *)prefetchedClusterData;
- (void)removeDevice:(MTRDevice *)device;

- (void)_callDelegatesWithBlock:(void (^_Nullable)(id<MTRDeviceControllerDelegate> delegate))block logString:(const char *)logString;

@end

/**
Expand Down
3 changes: 2 additions & 1 deletion src/darwin/Framework/CHIP/MTRDeviceController_XPC.h
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,8 @@ MTR_TESTABLE
- (id)initWithUniqueIdentifier:(NSUUID *)UUID machServiceName:(NSString *)machServiceName options:(NSXPCConnectionOptions)options
#endif

@property(nullable, atomic, retain, readwrite)NSXPCConnection * xpcConnection;
@property(nullable, atomic, retain, readwrite) NSXPCConnection * xpcConnection;
@property(atomic, readonly) BOOL isRunning;

@end

Expand Down
104 changes: 65 additions & 39 deletions src/darwin/Framework/CHIP/MTRDeviceController_XPC.mm
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,9 @@ @interface MTRDeviceController_XPC ()
NSString * const MTRDeviceControllerRegistrationControllerContextKey = @"MTRDeviceControllerRegistrationControllerContext";
NSString * const MTRDeviceControllerRegistrationNodeIDsKey = @"MTRDeviceControllerRegistrationNodeIDs";
NSString * const MTRDeviceControllerRegistrationNodeIDKey = @"MTRDeviceControllerRegistrationNodeID";
NSString * const MTRDeviceControllerRegistrationControllerNodeIDKey = @"MTRDeviceControllerRegistrationControllerNodeID";
NSString * const MTRDeviceControllerRegistrationControllerIsRunningKey = @"MTRDeviceControllerRegistrationControllerIsRunning";
NSString * const MTRDeviceControllerRegistrationDeviceInternalStateKey = @"MTRDeviceControllerRegistrationDeviceInternalState";

// #define MTR_HAVE_MACH_SERVICE_NAME_CONSTRUCTOR

Expand Down Expand Up @@ -70,23 +73,6 @@ - (void)_updateRegistrationInfo
MTR_REQUIRED_ATTRIBUTE(MTRDeviceControllerRegistrationNodeIDsKey, nodeIDs, registrationInfo)
MTR_REQUIRED_ATTRIBUTE(MTRDeviceControllerRegistrationControllerContextKey, controllerContext, registrationInfo)

MTR_LOG("%@ Starting existing NodeID Registration", self);
for (NSNumber * nodeID in [self.nodeIDToDeviceMap keyEnumerator]) {
MTR_LOG("%@ => Registering nodeID: %@", self, nodeID);
mtr_weakify(self);

[[self.xpcConnection synchronousRemoteObjectProxyWithErrorHandler:^(NSError * _Nonnull error) {
mtr_strongify(self);
MTR_LOG_ERROR("%@ Registration error for device nodeID: %@ : %@", self, nodeID, error);
}] deviceController:self.uniqueIdentifier registerNodeID:nodeID reply:^(NSDictionary * _Nonnull internalState) {
mtr_strongify(self);
MTRDevice_XPC * device = (MTRDevice_XPC *) [self deviceForNodeID:nodeID];
[device device:nodeID internalStateUpdated:internalState];
}];
}

MTR_LOG("%@ Done existing NodeID Registration", self);

[self updateControllerConfiguration:registrationInfo];
}

Expand All @@ -108,6 +94,7 @@ - (void)removeDevice:(MTRDevice *)device

#pragma mark - XPC
@synthesize controllerNodeID = _controllerNodeID;
@synthesize isRunning = _isRunning;

+ (NSMutableSet *)_allowedClasses
{
Expand Down Expand Up @@ -160,20 +147,6 @@ - (NSXPCInterface *)_interfaceForServerProtocol
argumentIndex:0
ofReply:YES];

// registerNodeID: returns dictionary containing standard nsstring / nsnumber / nsdate objects.
allowedClasses = [MTRDeviceController_XPC _allowedClasses];
[interface setClasses:allowedClasses
forSelector:@selector(deviceController:registerNodeID:reply:)
argumentIndex:0
ofReply:YES];

// checkInWithContext: returns dictionary containing standard nsstring / nsnumber objects.
allowedClasses = [MTRDeviceController_XPC _allowedClasses];
[interface setClasses:allowedClasses
forSelector:@selector(deviceController:checkInWithContext:reply:)
argumentIndex:0
ofReply:YES];

return interface;
}

Expand All @@ -200,6 +173,13 @@ - (NSXPCInterface *)_interfaceForClientProtocol
argumentIndex:1
ofReply:NO];

allowedClasses = [MTRDeviceController_XPC _allowedClasses];

[interface setClasses:allowedClasses
forSelector:@selector(controller:controllerConfigurationUpdated:)
argumentIndex:1
ofReply:NO];

return interface;
}

Expand Down Expand Up @@ -281,13 +261,6 @@ - (BOOL)_setupXPCConnection
MTR_LOG("%@ Activating new XPC connection", self);
[self.xpcConnection activate];

[[self.xpcConnection synchronousRemoteObjectProxyWithErrorHandler:^(NSError * _Nonnull error) {
MTR_LOG_ERROR("Checkin error: %@", error);
}] deviceController:self.uniqueIdentifier checkInWithContext:@{} reply:^(NSDictionary * _Nonnull controllerInfo) {
// Get the controller nodeID
;
}];

// FIXME: Trying to kick all the MTRDevices attached to this controller to re-establish connections
// This state needs to be stored properly and re-established at connnection time

Expand Down Expand Up @@ -386,7 +359,7 @@ - (MTRDevice *)_setupDeviceForNodeID:(NSNumber *)nodeID prefetchedClusterData:(N

#pragma mark - XPC Action Overrides

MTR_DEVICECONTROLLER_SIMPLE_REMOTE_XPC_GETTER(isRunning, BOOL, NO, getIsRunningWithReply)
//MTR_DEVICECONTROLLER_SIMPLE_REMOTE_XPC_GETTER(isRunning, BOOL, NO, getIsRunningWithReply)
//MTR_DEVICECONTROLLER_SIMPLE_REMOTE_XPC_GETTER(controllerNodeID, NSNumber *, nil, controllerNodeIDWithReply)

// Not Supported via XPC
Expand Down Expand Up @@ -464,6 +437,59 @@ - (oneway void)device:(NSNumber *)nodeID internalStateUpdated:(NSDictionary *)di

#pragma mark - MTRDeviceController Protocol Client

- (oneway void)controller:(NSUUID *)controller controllerConfigurationUpdated:(NSDictionary *)configuration
{
// Reuse the same format as config dictionary, and add values for internal states
// @{
// MTRDeviceControllerRegistrationControllerContextKey: @{
// MTRDeviceControllerRegistrationControllerNodeIDKey: controllerNodeID
// }
// MTRDeviceControllerRegistrationNodeIDsKey: @[
// @{
// MTRDeviceControllerRegistrationNodeIDKey: nodeID,
// MTRDeviceControllerRegistrationDeviceInternalStateKey: deviceInternalStateDictionary
// }
// ]
// }

NSDictionary *controllerContext = configuration[MTRDeviceControllerRegistrationControllerContextKey];
NSNumber *controllerNodeID = controllerContext[MTRDeviceControllerRegistrationControllerNodeIDKey];
if (controllerNodeID && [controllerNodeID isKindOfClass:[NSNumber class]]) {
_controllerNodeID = controllerContext[MTRDeviceControllerRegistrationControllerNodeIDKey];
}
NSNumber *isRunning = controllerContext[MTRDeviceControllerRegistrationControllerIsRunningKey];
if (isRunning && [isRunning isKindOfClass:[NSNumber class]]) {
_isRunning = isRunning.boolValue;
}

NSArray *deviceInfoList = configuration[MTRDeviceControllerRegistrationNodeIDsKey];

MTR_LOG("Received controllerConfigurationUpdated: controllerNode ID %@ deviceInfoList %@", self.controllerNodeID, deviceInfoList);

for (NSDictionary *deviceInfo in deviceInfoList) {
if (![deviceInfo isKindOfClass:[NSDictionary class]]) {
MTR_LOG_ERROR(" - deviceInfo %@ not NSDictionary class %@", deviceInfo, NSStringFromClass([deviceInfo class]));
continue;
}
NSNumber *nodeID = deviceInfo[MTRDeviceControllerRegistrationNodeIDKey];
if (nodeID && ![nodeID isKindOfClass:[NSNumber class]]) {
MTR_LOG_ERROR(" - deviceInfo %@ nodeID not NSNumber class %@", deviceInfo, NSStringFromClass([nodeID class]));
continue;
}
NSDictionary *deviceInternalState = deviceInfo[MTRDeviceControllerRegistrationDeviceInternalStateKey];
if (deviceInternalState && ![deviceInternalState isKindOfClass:[NSDictionary class]]) {
MTR_LOG_ERROR(" - deviceInfo %@ deviceInternalState not NSNumber class %@", deviceInfo, NSStringFromClass([deviceInternalState class]));
continue;
}
if (!nodeID || !deviceInternalState) {
MTR_LOG(" - deviceInfo %@ missing elements: nodeID %@ internalState %@", deviceInfo, nodeID, deviceInternalState);
continue;
}
MTRDevice_XPC * device = (MTRDevice_XPC *) [self deviceForNodeID:nodeID];
[device device:nodeID internalStateUpdated:deviceInternalState];
}
}

// Not Supported via XPC
//- (oneway void)controller:(NSUUID *)controller statusUpdate:(MTRCommissioningStatus)status {
// }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ MTR_NEWLY_AVAILABLE

MTR_NEWLY_AVAILABLE
@protocol MTRXPCClientProtocol <NSObject, MTRXPCClientProtocol_MTRDevice, MTRXPCClientProtocol_MTRDeviceController>
- (oneway void)controller:(NSUUID *)controller controllerConfigurationUpdated:(NSDictionary *)configuration;
@end

NS_ASSUME_NONNULL_END
9 changes: 3 additions & 6 deletions src/darwin/Framework/CHIP/XPC Protocol/MTRXPCServerProtocol.h
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,9 @@ NS_ASSUME_NONNULL_BEGIN
MTR_EXTERN NSString * const MTRDeviceControllerRegistrationNodeIDsKey MTR_NEWLY_AVAILABLE;
MTR_EXTERN NSString * const MTRDeviceControllerRegistrationNodeIDKey MTR_NEWLY_AVAILABLE;
MTR_EXTERN NSString * const MTRDeviceControllerRegistrationControllerContextKey MTR_NEWLY_AVAILABLE;
MTR_EXTERN NSString * const MTRDeviceControllerRegistrationControllerNodeIDKey MTR_NEWLY_AVAILABLE;
MTR_EXTERN NSString * const MTRDeviceControllerRegistrationControllerIsRunningKey MTR_NEWLY_AVAILABLE;
MTR_EXTERN NSString * const MTRDeviceControllerRegistrationDeviceInternalStateKey MTR_NEWLY_AVAILABLE;

MTR_NEWLY_AVAILABLE
@protocol MTRXPCServerProtocol_MTRDevice <NSObject>
Expand Down Expand Up @@ -73,19 +76,13 @@ MTR_NEWLY_AVAILABLE

- (oneway void)deviceController:(NSUUID *)controller registerNodeID:(NSNumber *)nodeID;
- (oneway void)deviceController:(NSUUID *)controller unregisterNodeID:(NSNumber *)nodeID;

// Register Node and get initial internal state
- (oneway void)deviceController:(NSUUID *)controller registerNodeID:(NSNumber *)nodeID reply:(void (^)(NSDictionary * internalState))reply;
@end

MTR_NEWLY_AVAILABLE
@protocol MTRXPCServerProtocol <NSObject, MTRXPCServerProtocol_MTRDevice, MTRXPCServerProtocol_MTRDeviceController>
@optional
- (oneway void)deviceController:(NSUUID *)controller checkInWithContext:(NSDictionary *)context;
- (oneway void)deviceController:(NSUUID *)controller updateControllerConfiguration:(NSDictionary *)controllerState;

// Check-in and get initial state
- (oneway void)deviceController:(NSUUID *)controller checkInWithContext:(NSDictionary *)context reply:(void (^)(NSDictionary * controllerInfo))reply;
@end

NS_ASSUME_NONNULL_END
8 changes: 4 additions & 4 deletions src/darwin/Framework/Matter.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -1308,8 +1308,6 @@
isa = PBXGroup;
children = (
D444F9A12C6E8058007761E5 /* XPC Protocol */,
9B231B032C62EF650030EB37 /* MTRDeviceController_Concrete.mm */,
9B0484F42C701154006C2D5F /* MTRDeviceController_Concrete.h */,
88E07D602B9A89A4005FD53E /* MTRMetricKeys.h */,
88FA798B2B7B257100CD4B6F /* MTRMetricsCollector.h */,
88FA798C2B7B257100CD4B6F /* MTRMetricsCollector.mm */,
Expand Down Expand Up @@ -1388,11 +1386,13 @@
75B3269B2BCDB9D600E17C4E /* MTRDeviceConnectivityMonitor.h */,
75B3269D2BCDB9EA00E17C4E /* MTRDeviceConnectivityMonitor.mm */,
991DC0822475F45400C13860 /* MTRDeviceController.h */,
5136660F28067D540025EDAE /* MTRDeviceController_Internal.h */,
991DC0872475F47D00C13860 /* MTRDeviceController.mm */,
9B0484F42C701154006C2D5F /* MTRDeviceController_Concrete.h */,
9B231B032C62EF650030EB37 /* MTRDeviceController_Concrete.mm */,
9BFE5D4E2C6D3075007D4319 /* MTRDeviceController_XPC.h */,
9B5CCB582C6E6FD3009DD99B /* MTRDeviceController_XPC_Internal.h */,
9BFE5D4F2C6D3075007D4319 /* MTRDeviceController_XPC.mm */,
5136660F28067D540025EDAE /* MTRDeviceController_Internal.h */,
991DC0872475F47D00C13860 /* MTRDeviceController.mm */,
5A7947E227C0101200434CF2 /* MTRDeviceController+XPC.h */,
5A7947E327C0129500434CF2 /* MTRDeviceController+XPC.mm */,
2CB7163E252F731E0026E2BB /* MTRDeviceControllerDelegate.h */,
Expand Down

0 comments on commit 8234e8c

Please sign in to comment.