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 <commits@restyled.io>
diff --git a/src/darwin/Framework/CHIP/MTRDeviceController_XPC.mm b/src/darwin/Framework/CHIP/MTRDeviceController_XPC.mm
index 038be79..ca3100a 100644
--- a/src/darwin/Framework/CHIP/MTRDeviceController_XPC.mm
+++ b/src/darwin/Framework/CHIP/MTRDeviceController_XPC.mm
@@ -40,46 +40,58 @@
 
 @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
+
+MTR_DEVICECONTROLLER_SIMPLE_REMOTE_XPC_COMMAND(updateControllerConfiguration
+                                               : (NSDictionary *) controllerState, updateControllerConfiguration
+                                               : (NSDictionary *) controllerState)
+
+- (void)_updateRegistrationInfo
+{
+    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
 {
-    @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);
-    }
+    [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 _updateRegistrationInfo];
+}
 
-        [[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);
-    }
+- (void)removeDevice:(MTRDevice *)device
+{
+    [super removeDevice:device];
+    [self _updateRegistrationInfo];
 }
 
 #pragma mark - XPC
@@ -246,13 +258,7 @@
         // 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 @@
     [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 b404056..36e2be5 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 <NSObject>
 
@@ -42,6 +46,7 @@
 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;
@@ -66,7 +71,9 @@
 
 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