Skip to content

Commit b37012b

Browse files
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>
1 parent 6252541 commit b37012b

File tree

2 files changed

+46
-33
lines changed

2 files changed

+46
-33
lines changed

src/darwin/Framework/CHIP/MTRDeviceController_XPC.mm

+39-33
Original file line numberDiff line numberDiff line change
@@ -40,46 +40,58 @@ @interface MTRDeviceController_XPC ()
4040

4141
@end
4242

43+
NSString * const MTRDeviceControllerRegistrationControllerContextKey = @"MTRDeviceControllerRegistrationControllerContext";
44+
NSString * const MTRDeviceControllerRegistrationNodeIDsKey = @"MTRDeviceControllerRegistrationNodeIDs";
45+
NSString * const MTRDeviceControllerRegistrationNodeIDKey = @"MTRDeviceControllerRegistrationNodeID";
46+
4347
// #define MTR_HAVE_MACH_SERVICE_NAME_CONSTRUCTOR
4448

4549
@implementation MTRDeviceController_XPC
4650

47-
#pragma mark - Device Node ID Commands
51+
#pragma mark - Node ID Management
4852

49-
- (void)_registerNodeID:(NSNumber *)nodeID
53+
MTR_DEVICECONTROLLER_SIMPLE_REMOTE_XPC_COMMAND(updateControllerConfiguration
54+
: (NSDictionary *) controllerState, updateControllerConfiguration
55+
: (NSDictionary *) controllerState)
56+
57+
- (void)_updateRegistrationInfo
5058
{
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];
5769
}
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];
5879
}
5980

6081
- (void)_unregisterNodeID:(NSNumber *)nodeID
6182
{
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];
6984
}
7085

7186
- (void)_checkinWithContext:(NSDictionary *)context
7287
{
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];
8395
}
8496

8597
#pragma mark - XPC
@@ -246,13 +258,7 @@ - (BOOL)_setupXPCConnection
246258
// FIXME: Trying to kick all the MTRDevices attached to this controller to re-establish connections
247259
// This state needs to be stored properly and re-established at connnection time
248260

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];
256262
self.xpcConnectedOrConnecting = YES;
257263
} else {
258264
MTR_LOG_ERROR("%@ Failed to set up XPC Connection", self);
@@ -340,7 +346,7 @@ - (MTRDevice *)_setupDeviceForNodeID:(NSNumber *)nodeID prefetchedClusterData:(N
340346
[self.nodeIDToDeviceMap setObject:deviceToReturn forKey:nodeID];
341347
MTR_LOG("%s: returning XPC device for node id %@", __PRETTY_FUNCTION__, nodeID);
342348

343-
[self _registerNodeID:nodeID];
349+
[self _updateRegistrationInfo];
344350

345351
return deviceToReturn;
346352
}

src/darwin/Framework/CHIP/XPC Protocol/MTRXPCServerProtocol.h

+7
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,10 @@
1818

1919
NS_ASSUME_NONNULL_BEGIN
2020

21+
MTR_EXTERN NSString * const MTRDeviceControllerRegistrationNodeIDsKey MTR_NEWLY_AVAILABLE;
22+
MTR_EXTERN NSString * const MTRDeviceControllerRegistrationNodeIDKey MTR_NEWLY_AVAILABLE;
23+
MTR_EXTERN NSString * const MTRDeviceControllerRegistrationControllerContextKey MTR_NEWLY_AVAILABLE;
24+
2125
MTR_NEWLY_AVAILABLE
2226
@protocol MTRXPCServerProtocol_MTRDevice <NSObject>
2327

@@ -42,6 +46,7 @@ MTR_NEWLY_AVAILABLE
4246
MTR_NEWLY_AVAILABLE
4347
@protocol MTRXPCServerProtocol_MTRDeviceController <NSObject>
4448

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

6772
MTR_NEWLY_AVAILABLE
6873
@protocol MTRXPCServerProtocol <NSObject, MTRXPCServerProtocol_MTRDevice, MTRXPCServerProtocol_MTRDeviceController>
74+
@optional
6975
- (oneway void)deviceController:(NSUUID *)controller checkInWithContext:(NSDictionary *)context;
76+
- (oneway void)deviceController:(NSUUID *)controller updateControllerConfiguration:(NSDictionary *)controllerState;
7077
@end
7178

7279
NS_ASSUME_NONNULL_END

0 commit comments

Comments
 (0)