@@ -40,46 +40,58 @@ @interface MTRDeviceController_XPC ()
40
40
41
41
@end
42
42
43
+ NSString * const MTRDeviceControllerRegistrationControllerContextKey = @" MTRDeviceControllerRegistrationControllerContext" ;
44
+ NSString * const MTRDeviceControllerRegistrationNodeIDsKey = @" MTRDeviceControllerRegistrationNodeIDs" ;
45
+ NSString * const MTRDeviceControllerRegistrationNodeIDKey = @" MTRDeviceControllerRegistrationNodeID" ;
46
+
43
47
// #define MTR_HAVE_MACH_SERVICE_NAME_CONSTRUCTOR
44
48
45
49
@implementation MTRDeviceController_XPC
46
50
47
- #pragma mark - Device Node ID Commands
51
+ #pragma mark - Node ID Management
48
52
49
- - (void )_registerNodeID : (NSNumber *)nodeID
53
+ MTR_DEVICECONTROLLER_SIMPLE_REMOTE_XPC_COMMAND (updateControllerConfiguration
54
+ : (NSDictionary *) controllerState, updateControllerConfiguration
55
+ : (NSDictionary *) controllerState)
56
+
57
+ - (void )_updateRegistrationInfo
50
58
{
51
- @try {
52
- [[self .xpcConnection remoteObjectProxyWithErrorHandler: ^(NSError * _Nonnull error) {
53
- MTR_LOG_ERROR (" Register node error: %@ nodeID: %@" , error, nodeID);
54
- }] deviceController: self .uniqueIdentifier registerNodeID: nodeID];
55
- } @catch (NSException * exception ) {
56
- MTR_LOG_ERROR (" Exception registering nodeID: %@" , exception );
59
+ NSMutableDictionary * registrationInfo = [NSMutableDictionary dictionary ];
60
+
61
+ NSMutableDictionary * controllerContext = [NSMutableDictionary dictionary ];
62
+ NSMutableArray * nodeIDs = [NSMutableArray array ];
63
+
64
+ for (NSNumber * nodeID in [self .nodeIDToDeviceMap keyEnumerator ]) {
65
+ NSMutableDictionary * nodeDictionary = [NSMutableDictionary dictionary ];
66
+ MTR_REQUIRED_ATTRIBUTE (MTRDeviceControllerRegistrationNodeIDKey, nodeID, nodeDictionary)
67
+
68
+ [nodeIDs addObject: nodeDictionary];
57
69
}
70
+ MTR_REQUIRED_ATTRIBUTE (MTRDeviceControllerRegistrationNodeIDsKey, nodeIDs, registrationInfo)
71
+ MTR_REQUIRED_ATTRIBUTE (MTRDeviceControllerRegistrationControllerContextKey, controllerContext, registrationInfo)
72
+
73
+ [self updateControllerConfiguration: registrationInfo];
74
+ }
75
+
76
+ - (void )_registerNodeID : (NSNumber *)nodeID
77
+ {
78
+ [self _updateRegistrationInfo ];
58
79
}
59
80
60
81
- (void )_unregisterNodeID : (NSNumber *)nodeID
61
82
{
62
- @try {
63
- [[self .xpcConnection remoteObjectProxyWithErrorHandler: ^(NSError * _Nonnull error) {
64
- MTR_LOG_ERROR (" Unregister node error: %@ nodeID: %@" , error, nodeID);
65
- }] deviceController: self .uniqueIdentifier unregisterNodeID: nodeID];
66
- } @catch (NSException * exception ) {
67
- MTR_LOG_ERROR (" Exception unregistering nodeID: %@" , exception );
68
- }
83
+ [self _updateRegistrationInfo ];
69
84
}
70
85
71
86
- (void )_checkinWithContext : (NSDictionary *)context
72
87
{
73
- @try {
74
- if (!context)
75
- context = [NSDictionary dictionary ];
76
-
77
- [[self .xpcConnection remoteObjectProxyWithErrorHandler: ^(NSError * _Nonnull error) {
78
- MTR_LOG_ERROR (" Checkin error: %@" , error);
79
- }] deviceController: self .uniqueIdentifier checkInWithContext: context];
80
- } @catch (NSException * exception ) {
81
- MTR_LOG_ERROR (" Exception checking in with context: %@" , exception );
82
- }
88
+ [self _updateRegistrationInfo ];
89
+ }
90
+
91
+ - (void )removeDevice : (MTRDevice *)device
92
+ {
93
+ [super removeDevice: device];
94
+ [self _updateRegistrationInfo ];
83
95
}
84
96
85
97
#pragma mark - XPC
@@ -246,13 +258,7 @@ - (BOOL)_setupXPCConnection
246
258
// FIXME: Trying to kick all the MTRDevices attached to this controller to re-establish connections
247
259
// This state needs to be stored properly and re-established at connnection time
248
260
249
- MTR_LOG (" %@ Starting existing NodeID Registration" , self);
250
- for (NSNumber * nodeID in [self .nodeIDToDeviceMap keyEnumerator ]) {
251
- MTR_LOG (" %@ => Registering nodeID: %@" , self, nodeID);
252
- [self _registerNodeID: nodeID];
253
- }
254
-
255
- MTR_LOG (" %@ Done existing NodeID Registration" , self);
261
+ [self _updateRegistrationInfo ];
256
262
self.xpcConnectedOrConnecting = YES ;
257
263
} else {
258
264
MTR_LOG_ERROR (" %@ Failed to set up XPC Connection" , self);
@@ -340,7 +346,7 @@ - (MTRDevice *)_setupDeviceForNodeID:(NSNumber *)nodeID prefetchedClusterData:(N
340
346
[self .nodeIDToDeviceMap setObject: deviceToReturn forKey: nodeID];
341
347
MTR_LOG (" %s: returning XPC device for node id %@" , __PRETTY_FUNCTION__, nodeID);
342
348
343
- [self _registerNodeID: nodeID ];
349
+ [self _updateRegistrationInfo ];
344
350
345
351
return deviceToReturn;
346
352
}
0 commit comments