From b37012b35ebbff8a02f4cb3a88ef4359b4065d87 Mon Sep 17 00:00:00 2001 From: Justin Wood Date: Wed, 9 Oct 2024 12:21:25 -0700 Subject: [PATCH] Moving our XPC Checkin to be a general register (#35985) * Moving this to a general register * Restyled by whitespace * Restyled by clang-format --------- Co-authored-by: Restyled.io --- .../Framework/CHIP/MTRDeviceController_XPC.mm | 72 ++++++++++--------- .../CHIP/XPC Protocol/MTRXPCServerProtocol.h | 7 ++ 2 files changed, 46 insertions(+), 33 deletions(-) diff --git a/src/darwin/Framework/CHIP/MTRDeviceController_XPC.mm b/src/darwin/Framework/CHIP/MTRDeviceController_XPC.mm index 038be7934c1196..ca3100a1adf3f2 100644 --- a/src/darwin/Framework/CHIP/MTRDeviceController_XPC.mm +++ b/src/darwin/Framework/CHIP/MTRDeviceController_XPC.mm @@ -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 @@ -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); @@ -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; } diff --git a/src/darwin/Framework/CHIP/XPC Protocol/MTRXPCServerProtocol.h b/src/darwin/Framework/CHIP/XPC Protocol/MTRXPCServerProtocol.h index b404056c960a79..36e2be5d428436 100644 --- a/src/darwin/Framework/CHIP/XPC Protocol/MTRXPCServerProtocol.h +++ b/src/darwin/Framework/CHIP/XPC Protocol/MTRXPCServerProtocol.h @@ -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 @@ -42,6 +46,7 @@ MTR_NEWLY_AVAILABLE MTR_NEWLY_AVAILABLE @protocol MTRXPCServerProtocol_MTRDeviceController +@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; @@ -66,7 +71,9 @@ MTR_NEWLY_AVAILABLE MTR_NEWLY_AVAILABLE @protocol MTRXPCServerProtocol +@optional - (oneway void)deviceController:(NSUUID *)controller checkInWithContext:(NSDictionary *)context; +- (oneway void)deviceController:(NSUUID *)controller updateControllerConfiguration:(NSDictionary *)controllerState; @end NS_ASSUME_NONNULL_END