Skip to content

Commit

Permalink
Moving our XPC Checkin to be a general register (#35985)
Browse files Browse the repository at this point in the history
* Moving this to a general register

* Restyled by whitespace

* Restyled by clang-format

---------

Co-authored-by: Restyled.io <[email protected]>
  • Loading branch information
woody-apple and restyled-commits authored Oct 9, 2024
1 parent 6252541 commit b37012b
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 33 deletions.
72 changes: 39 additions & 33 deletions src/darwin/Framework/CHIP/MTRDeviceController_XPC.mm
Original file line number Diff line number Diff line change
Expand Up @@ -40,46 +40,58 @@ @interface MTRDeviceController_XPC ()

@end

NSString * const MTRDeviceControllerRegistrationControllerContextKey = @"MTRDeviceControllerRegistrationControllerContext";
NSString * const MTRDeviceControllerRegistrationNodeIDsKey = @"MTRDeviceControllerRegistrationNodeIDs";
NSString * const MTRDeviceControllerRegistrationNodeIDKey = @"MTRDeviceControllerRegistrationNodeID";

// #define MTR_HAVE_MACH_SERVICE_NAME_CONSTRUCTOR

@implementation MTRDeviceController_XPC

#pragma mark - Device Node ID Commands
#pragma mark - Node ID Management

- (void)_registerNodeID:(NSNumber *)nodeID
MTR_DEVICECONTROLLER_SIMPLE_REMOTE_XPC_COMMAND(updateControllerConfiguration
: (NSDictionary *) controllerState, updateControllerConfiguration
: (NSDictionary *) controllerState)

- (void)_updateRegistrationInfo
{
@try {
[[self.xpcConnection remoteObjectProxyWithErrorHandler:^(NSError * _Nonnull error) {
MTR_LOG_ERROR("Register node error: %@ nodeID: %@", error, nodeID);
}] deviceController:self.uniqueIdentifier registerNodeID:nodeID];
} @catch (NSException * exception) {
MTR_LOG_ERROR("Exception registering nodeID: %@", exception);
NSMutableDictionary * registrationInfo = [NSMutableDictionary dictionary];

NSMutableDictionary * controllerContext = [NSMutableDictionary dictionary];
NSMutableArray * nodeIDs = [NSMutableArray array];

for (NSNumber * nodeID in [self.nodeIDToDeviceMap keyEnumerator]) {
NSMutableDictionary * nodeDictionary = [NSMutableDictionary dictionary];
MTR_REQUIRED_ATTRIBUTE(MTRDeviceControllerRegistrationNodeIDKey, nodeID, nodeDictionary)

[nodeIDs addObject:nodeDictionary];
}
MTR_REQUIRED_ATTRIBUTE(MTRDeviceControllerRegistrationNodeIDsKey, nodeIDs, registrationInfo)
MTR_REQUIRED_ATTRIBUTE(MTRDeviceControllerRegistrationControllerContextKey, controllerContext, registrationInfo)

[self updateControllerConfiguration:registrationInfo];
}

- (void)_registerNodeID:(NSNumber *)nodeID
{
[self _updateRegistrationInfo];
}

- (void)_unregisterNodeID:(NSNumber *)nodeID
{
@try {
[[self.xpcConnection remoteObjectProxyWithErrorHandler:^(NSError * _Nonnull error) {
MTR_LOG_ERROR("Unregister node error: %@ nodeID: %@", error, nodeID);
}] deviceController:self.uniqueIdentifier unregisterNodeID:nodeID];
} @catch (NSException * exception) {
MTR_LOG_ERROR("Exception unregistering nodeID: %@", exception);
}
[self _updateRegistrationInfo];
}

- (void)_checkinWithContext:(NSDictionary *)context
{
@try {
if (!context)
context = [NSDictionary dictionary];

[[self.xpcConnection remoteObjectProxyWithErrorHandler:^(NSError * _Nonnull error) {
MTR_LOG_ERROR("Checkin error: %@", error);
}] deviceController:self.uniqueIdentifier checkInWithContext:context];
} @catch (NSException * exception) {
MTR_LOG_ERROR("Exception checking in with context: %@", exception);
}
[self _updateRegistrationInfo];
}

- (void)removeDevice:(MTRDevice *)device
{
[super removeDevice:device];
[self _updateRegistrationInfo];
}

#pragma mark - XPC
Expand Down Expand Up @@ -246,13 +258,7 @@ - (BOOL)_setupXPCConnection
// 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

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

MTR_LOG("%@ Done existing NodeID Registration", self);
[self _updateRegistrationInfo];
self.xpcConnectedOrConnecting = YES;
} else {
MTR_LOG_ERROR("%@ Failed to set up XPC Connection", self);
Expand Down Expand Up @@ -340,7 +346,7 @@ - (MTRDevice *)_setupDeviceForNodeID:(NSNumber *)nodeID prefetchedClusterData:(N
[self.nodeIDToDeviceMap setObject:deviceToReturn forKey:nodeID];
MTR_LOG("%s: returning XPC device for node id %@", __PRETTY_FUNCTION__, nodeID);

[self _registerNodeID:nodeID];
[self _updateRegistrationInfo];

return deviceToReturn;
}
Expand Down
7 changes: 7 additions & 0 deletions src/darwin/Framework/CHIP/XPC Protocol/MTRXPCServerProtocol.h
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,10 @@

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_NEWLY_AVAILABLE
@protocol MTRXPCServerProtocol_MTRDevice <NSObject>

Expand All @@ -42,6 +46,7 @@ MTR_NEWLY_AVAILABLE
MTR_NEWLY_AVAILABLE
@protocol MTRXPCServerProtocol_MTRDeviceController <NSObject>

@optional
- (oneway void)deviceController:(NSUUID *)controller getIsRunningWithReply:(void (^)(BOOL response))reply;
- (oneway void)deviceController:(NSUUID *)controller getUniqueIdentifierWithReply:(void (^)(NSUUID *))reply;
- (oneway void)deviceController:(NSUUID *)controller controllerNodeIDWithReply:(void (^)(NSNumber * nodeID))reply;
Expand All @@ -66,7 +71,9 @@ MTR_NEWLY_AVAILABLE

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;
@end

NS_ASSUME_NONNULL_END

0 comments on commit b37012b

Please sign in to comment.