From 5275b586c1de5a7914fc77065e79eea38191af54 Mon Sep 17 00:00:00 2001 From: Justin Wood Date: Mon, 28 Oct 2024 16:43:24 -0700 Subject: [PATCH 01/13] Updating this --- .../Framework/CHIP/MTRDeviceController.mm | 2 +- .../Framework/CHIP/MTRDeviceController_XPC.mm | 69 +++++++- .../Framework/CHIP/MTRDevice_Concrete.mm | 66 ++++++-- .../Framework/CHIP/MTRDevice_Internal.h | 6 +- src/darwin/Framework/CHIP/MTRDevice_XPC.mm | 158 +++++++++--------- .../CHIP/XPC Protocol/MTRXPCClientProtocol.h | 2 + .../CHIP/XPC Protocol/MTRXPCServerProtocol.h | 9 +- .../Matter.xcodeproj/project.pbxproj | 8 +- 8 files changed, 213 insertions(+), 107 deletions(-) diff --git a/src/darwin/Framework/CHIP/MTRDeviceController.mm b/src/darwin/Framework/CHIP/MTRDeviceController.mm index 5695ef23c7a9e7..8b8295d94450c2 100644 --- a/src/darwin/Framework/CHIP/MTRDeviceController.mm +++ b/src/darwin/Framework/CHIP/MTRDeviceController.mm @@ -714,7 +714,7 @@ - (NSUInteger)_iterateDelegateInfoWithBlock:(void (^_Nullable)(MTRDeviceControll } } -- (void)_callDelegatesWithBlock:(void (^_Nullable)(id delegate))block logString:(const char *)logString; +- (void)_callDelegatesWithBlock:(void (^_Nullable)(id delegate))block logString:(const char *)logString { NSUInteger delegatesCalled = [self _iterateDelegateInfoWithBlock:^(MTRDeviceControllerDelegateInfo * delegateInfo) { id strongDelegate = delegateInfo.delegate; diff --git a/src/darwin/Framework/CHIP/MTRDeviceController_XPC.mm b/src/darwin/Framework/CHIP/MTRDeviceController_XPC.mm index 7e88870aa7b570..676af86ef79a40 100644 --- a/src/darwin/Framework/CHIP/MTRDeviceController_XPC.mm +++ b/src/darwin/Framework/CHIP/MTRDeviceController_XPC.mm @@ -43,6 +43,9 @@ @interface MTRDeviceController_XPC () NSString * const MTRDeviceControllerRegistrationControllerContextKey = @"MTRDeviceControllerRegistrationControllerContext"; NSString * const MTRDeviceControllerRegistrationNodeIDsKey = @"MTRDeviceControllerRegistrationNodeIDs"; NSString * const MTRDeviceControllerRegistrationNodeIDKey = @"MTRDeviceControllerRegistrationNodeID"; +NSString * const MTRDeviceControllerRegistrationControllerNodeIDKey = @"MTRDeviceControllerRegistrationControllerNodeID"; +NSString * const MTRDeviceControllerRegistrationControllerIsRunningKey = @"MTRDeviceControllerRegistrationControllerIsRunning"; +NSString * const MTRDeviceControllerRegistrationDeviceInternalStateKey = @"MTRDeviceControllerRegistrationDeviceInternalState"; // #define MTR_HAVE_MACH_SERVICE_NAME_CONSTRUCTOR @@ -90,6 +93,8 @@ - (void)removeDevice:(MTRDevice *)device } #pragma mark - XPC +@synthesize controllerNodeID = _controllerNodeID; + + (NSMutableSet *)_allowedClasses { static NSArray * sBaseAllowedClasses = @[ @@ -167,6 +172,13 @@ - (NSXPCInterface *)_interfaceForClientProtocol argumentIndex:1 ofReply:NO]; + allowedClasses = [MTRDeviceController_XPC _allowedClasses]; + + [interface setClasses:allowedClasses + forSelector:@selector(controller:controllerConfigurationUpdated:) + argumentIndex:1 + ofReply:NO]; + return interface; } @@ -346,9 +358,6 @@ - (MTRDevice *)_setupDeviceForNodeID:(NSNumber *)nodeID prefetchedClusterData:(N #pragma mark - XPC Action Overrides -MTR_DEVICECONTROLLER_SIMPLE_REMOTE_XPC_GETTER(isRunning, BOOL, NO, getIsRunningWithReply) -MTR_DEVICECONTROLLER_SIMPLE_REMOTE_XPC_GETTER(controllerNodeID, NSNumber *, nil, controllerNodeIDWithReply) - // Not Supported via XPC // - (oneway void)deviceController:(NSUUID *)controller setupCommissioningSessionWithPayload:(MTRSetupPayload *)payload newNodeID:(NSNumber *)newNodeID withReply:(void(^)(BOOL success, NSError * _Nullable error))reply; // - (oneway void)deviceController:(NSUUID *)controller setupCommissioningSessionWithDiscoveredDevice:(MTRCommissionableBrowserResult *)discoveredDevice payload:(MTRSetupPayload *)payload newNodeID:(NSNumber *)newNodeID withReply:(void(^)(BOOL success, NSError * _Nullable error))reply; @@ -424,6 +433,60 @@ - (oneway void)device:(NSNumber *)nodeID internalStateUpdated:(NSDictionary *)di #pragma mark - MTRDeviceController Protocol Client +- (oneway void)controller:(NSUUID *)controller controllerConfigurationUpdated:(NSDictionary *)configuration +{ + // Reuse the same format as config dictionary, and add values for internal states + // @{ + // MTRDeviceControllerRegistrationControllerContextKey: @{ + // MTRDeviceControllerRegistrationControllerNodeIDKey: controllerNodeID + // } + // MTRDeviceControllerRegistrationNodeIDsKey: @[ + // @{ + // MTRDeviceControllerRegistrationNodeIDKey: nodeID, + // MTRDeviceControllerRegistrationDeviceInternalStateKey: deviceInternalStateDictionary + // } + // ] + // } + + NSDictionary * controllerContext = MTR_SAFE_CAST(configuration[MTRDeviceControllerRegistrationControllerContextKey], NSDictionary); + NSNumber * controllerNodeID = MTR_SAFE_CAST(controllerContext[MTRDeviceControllerRegistrationControllerNodeIDKey], NSNumber); + if (controllerNodeID && controllerNodeID) { + _controllerNodeID = controllerContext[MTRDeviceControllerRegistrationControllerNodeIDKey]; + } + + NSArray * deviceInfoList = MTR_SAFE_CAST(configuration[MTRDeviceControllerRegistrationNodeIDsKey], NSArray); + + MTR_LOG("Received controllerConfigurationUpdated: controllerNode ID %@ deviceInfoList %@", self.controllerNodeID, deviceInfoList); + + for (NSDictionary * deviceInfo in deviceInfoList) { + if (!MTR_SAFE_CAST(deviceInfo, NSDictionary)) { + MTR_LOG_ERROR(" - Missing or malformed device Info"); + } + + NSNumber * nodeID = MTR_SAFE_CAST(deviceInfo[MTRDeviceControllerRegistrationNodeIDKey], NSNumber); + if (!nodeID) { + MTR_LOG_ERROR(" - Missing or malformed nodeID"); + continue; + } + + NSDictionary * deviceInternalState = MTR_SAFE_CAST(deviceInfo[MTRDeviceControllerRegistrationDeviceInternalStateKey], NSDictionary); + if (!deviceInternalState) { + MTR_LOG_ERROR(" - Missing or malformed deviceInternalState"); + continue; + } + + auto * device = static_cast([self deviceForNodeID:nodeID]); + [device device:nodeID internalStateUpdated:deviceInternalState]; + } +} + +// TODO: Create DeviceControllerRunningState that is a tri-state with "unknown", "not running", and "running" states +- (BOOL)isRunning +{ + // For XPC controller, always return yes + return YES; +} + // Not Supported via XPC //- (oneway void)controller:(NSUUID *)controller statusUpdate:(MTRCommissioningStatus)status { // } diff --git a/src/darwin/Framework/CHIP/MTRDevice_Concrete.mm b/src/darwin/Framework/CHIP/MTRDevice_Concrete.mm index 12b6250e0bcadb..c4511aed9016bc 100644 --- a/src/darwin/Framework/CHIP/MTRDevice_Concrete.mm +++ b/src/darwin/Framework/CHIP/MTRDevice_Concrete.mm @@ -356,6 +356,14 @@ @implementation MTRDevice_Concrete { NSDate * _Nullable _mostRecentReportTimeForDescription; // Copy of _lastSubscriptionFailureTime that is safe to use in description. NSDate * _Nullable _lastSubscriptionFailureTimeForDescription; + // Copy of _state that is safe to use in description. + MTRDeviceState _deviceStateForDescription; + // Copy of _deviceCachePrimed that is safe to use in description. + BOOL _deviceCachePrimedForDescription; + // Copy of _estimatedStartTime that is safe to use in description. + NSDate * _Nullable _estimatedStartTimeForDescription; + // Copy of _estimatedSubscriptionLatency that is safe to use in description. + NSNumber * _Nullable _estimatedSubscriptionLatencyForDescription; } // synthesize superclass property readwrite accessors @@ -480,11 +488,16 @@ - (NSDictionary *)_internalProperties MTR_OPTIONAL_ATTRIBUTE(kMTRDeviceInternalPropertyKeyVendorID, _vid, properties); MTR_OPTIONAL_ATTRIBUTE(kMTRDeviceInternalPropertyKeyProductID, _pid, properties); MTR_OPTIONAL_ATTRIBUTE(kMTRDeviceInternalPropertyNetworkFeatures, _allNetworkFeatures, properties); - MTR_OPTIONAL_ATTRIBUTE(kMTRDeviceInternalPropertyDeviceState, [NSNumber numberWithUnsignedInteger:_internalDeviceStateForDescription], properties); + MTR_OPTIONAL_ATTRIBUTE(kMTRDeviceInternalPropertyDeviceInternalState, [NSNumber numberWithUnsignedInteger:_internalDeviceStateForDescription], properties); MTR_OPTIONAL_ATTRIBUTE(kMTRDeviceInternalPropertyLastSubscriptionAttemptWait, [NSNumber numberWithUnsignedInt:_lastSubscriptionAttemptWaitForDescription], properties); MTR_OPTIONAL_ATTRIBUTE(kMTRDeviceInternalPropertyMostRecentReportTime, _mostRecentReportTimeForDescription, properties); MTR_OPTIONAL_ATTRIBUTE(kMTRDeviceInternalPropertyLastSubscriptionFailureTime, _lastSubscriptionFailureTimeForDescription, properties); + MTR_OPTIONAL_ATTRIBUTE(kMTRDeviceInternalPropertyDeviceState, [NSNumber numberWithUnsignedInteger:_deviceStateForDescription], properties); + MTR_OPTIONAL_ATTRIBUTE(kMTRDeviceInternalPropertyDeviceCachePrimed, @(_deviceCachePrimedForDescription), properties); + MTR_OPTIONAL_ATTRIBUTE(kMTRDeviceInternalPropertyEstimatedStartTime, _estimatedStartTimeForDescription, properties); + MTR_OPTIONAL_ATTRIBUTE(kMTRDeviceInternalPropertyEstimatedSubscriptionLatency, _estimatedSubscriptionLatencyForDescription, properties); + return properties; } @@ -985,11 +998,15 @@ - (void)_changeState:(MTRDeviceState)state os_unfair_lock_assert_owner(&self->_lock); MTRDeviceState lastState = _state; _state = state; + { + std::lock_guard lock(_descriptionLock); + _deviceStateForDescription = _state; + } if (lastState != state) { if (state != MTRDeviceStateReachable) { MTR_LOG("%@ reachability state change %lu => %lu, set estimated start time to nil", self, static_cast(lastState), static_cast(state)); - _estimatedStartTime = nil; + [self _updateEstimatedStartTime:nil]; _estimatedStartTimeFromGeneralDiagnosticsUpTime = nil; } else { MTR_LOG( @@ -1046,6 +1063,15 @@ - (MTRInternalDeviceState)_getInternalState } #endif +- (void)_updateEstimatedSubscriptionLatency:(NSNumber *)estimatedSubscriptionLatency +{ + os_unfair_lock_assert_owner(&_lock); + _estimatedSubscriptionLatency = estimatedSubscriptionLatency; + + std::lock_guard lock(_descriptionLock); + _estimatedSubscriptionLatencyForDescription = estimatedSubscriptionLatency; +} + // First Time Sync happens 2 minutes after reachability (this can be changed in the future) #define MTR_DEVICE_TIME_UPDATE_INITIAL_WAIT_TIME_SEC (60 * 2) - (void)_handleSubscriptionEstablished @@ -1077,10 +1103,10 @@ - (void)_handleSubscriptionEstablished // way around. NSTimeInterval subscriptionLatency = -[initialSubscribeStart timeIntervalSinceNow]; if (_estimatedSubscriptionLatency == nil) { - _estimatedSubscriptionLatency = @(subscriptionLatency); + [self _updateEstimatedSubscriptionLatency:@(subscriptionLatency)]; } else { NSTimeInterval newSubscriptionLatencyEstimate = MTRDEVICE_SUBSCRIPTION_LATENCY_NEW_VALUE_WEIGHT * subscriptionLatency + (1 - MTRDEVICE_SUBSCRIPTION_LATENCY_NEW_VALUE_WEIGHT) * _estimatedSubscriptionLatency.doubleValue; - _estimatedSubscriptionLatency = @(newSubscriptionLatencyEstimate); + [self _updateEstimatedSubscriptionLatency:@(newSubscriptionLatencyEstimate)]; } [self _storePersistedDeviceData]; } @@ -1767,6 +1793,15 @@ - (void)setStorageBehaviorConfiguration:(MTRDeviceStorageBehaviorConfiguration * [self _resetStorageBehaviorState]; } +- (void)_updateDeviceCachePrimed:(BOOL)deviceCachePrimed +{ + os_unfair_lock_assert_owner(&_lock); + _deviceCachePrimed = deviceCachePrimed; + + std::lock_guard lock(_descriptionLock); + _deviceCachePrimedForDescription = deviceCachePrimed; +} + - (void)_handleReportEnd { MTR_LOG("%@ handling report end", self); @@ -1799,7 +1834,7 @@ - (void)_handleReportEnd if (!_deviceCachePrimed) { // This is the end of the priming sequence of data reports, so we have // all the data for the device now. - _deviceCachePrimed = YES; + [self _updateDeviceCachePrimed:YES]; [self _callDelegateDeviceCachePrimed]; [self _notifyDelegateOfPrivateInternalPropertiesChanges]; } @@ -1998,6 +2033,15 @@ - (BOOL)_interestedPaths:(NSArray * _Nullable)interestedPaths includesEventPath: return filteredEvents; } +- (void)_updateEstimatedStartTime:(NSDate *)estimatedStartTime +{ + os_unfair_lock_assert_owner(&_lock); + _estimatedStartTime = estimatedStartTime; + + std::lock_guard lock(_descriptionLock); + _estimatedStartTimeForDescription = _estimatedStartTime; +} + - (void)_handleEventReport:(NSArray *> *)eventReport { std::lock_guard lock(_lock); @@ -2043,11 +2087,11 @@ - (void)_handleEventReport:(NSArray *> *)eventRepor // If UpTime was received, make use of it as mark of system start time MTR_LOG("%@ StartUp event: set estimated start time forward to %@", self, _estimatedStartTimeFromGeneralDiagnosticsUpTime); - _estimatedStartTime = _estimatedStartTimeFromGeneralDiagnosticsUpTime; + [self _updateEstimatedStartTime:_estimatedStartTimeFromGeneralDiagnosticsUpTime]; } else { // If UpTime was not received, reset estimated start time in case of reboot MTR_LOG("%@ StartUp event: set estimated start time to nil", self); - _estimatedStartTime = nil; + [self _updateEstimatedStartTime:nil]; } } @@ -2067,7 +2111,7 @@ - (void)_handleEventReport:(NSArray *> *)eventRepor NSTimeInterval eventTimeValue = eventTimeValueNumber.doubleValue; NSDate * potentialSystemStartTime = [NSDate dateWithTimeIntervalSinceNow:-eventTimeValue]; if (!_estimatedStartTime || ([potentialSystemStartTime compare:_estimatedStartTime] == NSOrderedAscending)) { - _estimatedStartTime = potentialSystemStartTime; + [self _updateEstimatedStartTime:potentialSystemStartTime]; } } @@ -3644,7 +3688,7 @@ - (NSArray *)_getAttributesToReportWithReportedValues:(NSArray %@", self, upTime, oldSystemStartTime, potentialSystemStartTime); - _estimatedStartTime = potentialSystemStartTime; + [self _updateEstimatedStartTime:potentialSystemStartTime]; } // Save estimate in the subscription resumption case, for when StartUp event uses it @@ -3736,7 +3780,7 @@ - (void)setPersistedClusterData:(NSDictionary *)data diff --git a/src/darwin/Framework/CHIP/MTRDevice_Internal.h b/src/darwin/Framework/CHIP/MTRDevice_Internal.h index 35cc25e26949da..f162e43e4d683d 100644 --- a/src/darwin/Framework/CHIP/MTRDevice_Internal.h +++ b/src/darwin/Framework/CHIP/MTRDevice_Internal.h @@ -187,9 +187,13 @@ static NSString * const kTestStorageUserDefaultEnabledKey = @"enableTestStorage" static NSString * const kMTRDeviceInternalPropertyKeyVendorID = @"MTRDeviceInternalStateKeyVendorID"; static NSString * const kMTRDeviceInternalPropertyKeyProductID = @"MTRDeviceInternalStateKeyProductID"; static NSString * const kMTRDeviceInternalPropertyNetworkFeatures = @"MTRDeviceInternalPropertyNetworkFeatures"; -static NSString * const kMTRDeviceInternalPropertyDeviceState = @"MTRDeviceInternalPropertyDeviceState"; +static NSString * const kMTRDeviceInternalPropertyDeviceInternalState = @"MTRDeviceInternalPropertyDeviceInternalState"; static NSString * const kMTRDeviceInternalPropertyLastSubscriptionAttemptWait = @"kMTRDeviceInternalPropertyLastSubscriptionAttemptWait"; static NSString * const kMTRDeviceInternalPropertyMostRecentReportTime = @"MTRDeviceInternalPropertyMostRecentReportTime"; static NSString * const kMTRDeviceInternalPropertyLastSubscriptionFailureTime = @"MTRDeviceInternalPropertyLastSubscriptionFailureTime"; +static NSString * const kMTRDeviceInternalPropertyDeviceState = @"MTRDeviceInternalPropertyDeviceState"; +static NSString * const kMTRDeviceInternalPropertyDeviceCachePrimed = @"MTRDeviceInternalPropertyDeviceCachePrimed"; +static NSString * const kMTRDeviceInternalPropertyEstimatedStartTime = @"MTRDeviceInternalPropertyEstimatedStartTime"; +static NSString * const kMTRDeviceInternalPropertyEstimatedSubscriptionLatency = @"MTRDeviceInternalPropertyEstimatedSubscriptionLatency"; NS_ASSUME_NONNULL_END diff --git a/src/darwin/Framework/CHIP/MTRDevice_XPC.mm b/src/darwin/Framework/CHIP/MTRDevice_XPC.mm index 8a5f745a1dfc70..665523e43ed6b5 100644 --- a/src/darwin/Framework/CHIP/MTRDevice_XPC.mm +++ b/src/darwin/Framework/CHIP/MTRDevice_XPC.mm @@ -56,6 +56,7 @@ #import "MTRSetupPayload.h" #import "MTRTimeUtils.h" #import "MTRUnfairLock.h" +#import "MTRUtilities.h" #import "NSDataSpanConversion.h" #import "NSStringSpanConversion.h" @@ -65,20 +66,14 @@ #import -#define MTR_DEVICE_SIMPLE_REMOTE_XPC_GETTER(NAME, TYPE, DEFAULT_VALUE, GETTER_NAME) \ - MTR_SIMPLE_REMOTE_XPC_GETTER([(MTRDeviceController_XPC *) [self deviceController] xpcConnection], NAME, TYPE, DEFAULT_VALUE, GETTER_NAME, deviceController \ - : [[self deviceController] uniqueIdentifier] nodeID \ - : [self nodeID]) +#define MTR_DEVICE_SIMPLE_REMOTE_XPC_GETTER(NAME, TYPE, DEFAULT_VALUE, GETTER_NAME) \ + MTR_SIMPLE_REMOTE_XPC_GETTER([(MTRDeviceController_XPC *) [self deviceController] xpcConnection], NAME, TYPE, DEFAULT_VALUE, GETTER_NAME, deviceController : [[self deviceController] uniqueIdentifier] nodeID : [self nodeID]) -#define MTR_DEVICE_COMPLEX_REMOTE_XPC_GETTER(SIGNATURE, TYPE, DEFAULT_VALUE, ADDITIONAL_ARGUMENTS) \ - MTR_COMPLEX_REMOTE_XPC_GETTER([(MTRDeviceController_XPC *) [self deviceController] xpcConnection], SIGNATURE, TYPE, DEFAULT_VALUE, ADDITIONAL_ARGUMENTS, deviceController \ - : [[self deviceController] uniqueIdentifier] nodeID \ - : [self nodeID]) +#define MTR_DEVICE_COMPLEX_REMOTE_XPC_GETTER(SIGNATURE, TYPE, DEFAULT_VALUE, ADDITIONAL_ARGUMENTS) \ + MTR_COMPLEX_REMOTE_XPC_GETTER([(MTRDeviceController_XPC *) [self deviceController] xpcConnection], SIGNATURE, TYPE, DEFAULT_VALUE, ADDITIONAL_ARGUMENTS, deviceController : [[self deviceController] uniqueIdentifier] nodeID : [self nodeID]) -#define MTR_DEVICE_SIMPLE_REMOTE_XPC_COMMAND(METHOD_SIGNATURE, ADDITIONAL_ARGUMENTS) \ - MTR_SIMPLE_REMOTE_XPC_COMMAND([(MTRDeviceController_XPC *) [self deviceController] xpcConnection], METHOD_SIGNATURE, ADDITIONAL_ARGUMENTS, deviceController \ - : [[self deviceController] uniqueIdentifier] nodeID \ - : [self nodeID]) +#define MTR_DEVICE_SIMPLE_REMOTE_XPC_COMMAND(METHOD_SIGNATURE, ADDITIONAL_ARGUMENTS) \ + MTR_SIMPLE_REMOTE_XPC_COMMAND([(MTRDeviceController_XPC *) [self deviceController] xpcConnection], METHOD_SIGNATURE, ADDITIONAL_ARGUMENTS, deviceController : [[self deviceController] uniqueIdentifier] nodeID : [self nodeID]) @implementation MTRDevice_XPC @@ -113,32 +108,31 @@ - (NSString *)description } // TODO: Add these to the description - // MTR_OPTIONAL_ATTRIBUTE(kMTRDeviceInternalPropertyDeviceState, _internalDeviceStateForDescription, properties); // MTR_OPTIONAL_ATTRIBUTE(kMTRDeviceInternalPropertyLastSubscriptionAttemptWait, _lastSubscriptionAttemptWaitForDescription, properties); // MTR_OPTIONAL_ATTRIBUTE(kMTRDeviceInternalPropertyMostRecentReportTime, _mostRecentReportTimeForDescription, properties); // MTR_OPTIONAL_ATTRIBUTE(kMTRDeviceInternalPropertyLastSubscriptionFailureTime, _lastSubscriptionFailureTimeForDescription, properties); - return [NSString - stringWithFormat:@"<%@: %p, node: %016llX-%016llX (%llu), VID: %@, PID: %@, WiFi: %@, Thread: %@, controller: %@>", + return [NSString stringWithFormat:@"<%@: %p, node: %016llX-%016llX (%llu), VID: %@, PID: %@, WiFi: %@, Thread: %@, controller: %@ state: %ld>", NSStringFromClass(self.class), self, _deviceController.compressedFabricID.unsignedLongLongValue, _nodeID.unsignedLongLongValue, _nodeID.unsignedLongLongValue, - [self._internalState objectForKey:kMTRDeviceInternalPropertyKeyVendorID], - [self._internalState objectForKey:kMTRDeviceInternalPropertyKeyProductID], + [self vendorID], + [self productID], wifi, thread, - _deviceController.uniqueIdentifier]; + _deviceController.uniqueIdentifier, + [self state]]; } - (nullable NSNumber *)vendorID { - return [[self._internalState objectForKey:kMTRDeviceInternalPropertyKeyVendorID] copy]; + return [self._internalState objectForKey:kMTRDeviceInternalPropertyKeyVendorID]; } - (nullable NSNumber *)productID { - return [[self._internalState objectForKey:kMTRDeviceInternalPropertyKeyProductID] copy]; + return [self._internalState objectForKey:kMTRDeviceInternalPropertyKeyProductID]; } #pragma mark - Client Callbacks (MTRDeviceDelegate) @@ -146,19 +140,12 @@ - (nullable NSNumber *)productID // required methods for MTRDeviceDelegates - (oneway void)device:(NSNumber *)nodeID stateChanged:(MTRDeviceState)state { - if (!MTR_SAFE_CAST(nodeID, NSNumber)) { - MTR_LOG_ERROR("%@ invalid device:stateChanged: nodeID: %@", self, nodeID); - return; - } - - MTR_LOG("%s", __PRETTY_FUNCTION__); - [self _lockAndCallDelegatesWithBlock:^(id delegate) { - [delegate device:self stateChanged:state]; - }]; + // Not needed, since internal will get this } - (oneway void)device:(NSNumber *)nodeID receivedAttributeReport:(NSArray *)attributeReport { + MTR_LOG("%@ %s", self, __PRETTY_FUNCTION__); if (!MTR_SAFE_CAST(nodeID, NSNumber)) { MTR_LOG_ERROR("%@ invalid device:receivedAttributeReport: nodeID: %@", self, nodeID); return; @@ -169,7 +156,6 @@ - (oneway void)device:(NSNumber *)nodeID receivedAttributeReport:(NSArray delegate) { [delegate device:self receivedAttributeReport:attributeReport]; }]; @@ -177,6 +163,7 @@ - (oneway void)device:(NSNumber *)nodeID receivedAttributeReport:(NSArray *)eventReport { + MTR_LOG("%@ %s", self, __PRETTY_FUNCTION__); if (!MTR_SAFE_CAST(nodeID, NSNumber)) { MTR_LOG_ERROR("%@ invalid device:receivedEventReport: nodeID: %@", self, nodeID); return; @@ -187,7 +174,6 @@ - (oneway void)device:(NSNumber *)nodeID receivedEventReport:(NSArray delegate) { [delegate device:self receivedEventReport:eventReport]; }]; @@ -196,12 +182,12 @@ - (oneway void)device:(NSNumber *)nodeID receivedEventReport:(NSArray delegate) { if ([delegate respondsToSelector:@selector(deviceBecameActive:)]) { [delegate deviceBecameActive:self]; @@ -211,20 +197,12 @@ - (oneway void)deviceBecameActive:(NSNumber *)nodeID - (oneway void)deviceCachePrimed:(NSNumber *)nodeID { - if (!MTR_SAFE_CAST(nodeID, NSNumber)) { - MTR_LOG_ERROR("%@ invalid deviceCachePrimed: nodeID: %@", self, nodeID); - return; - } - - [self _lockAndCallDelegatesWithBlock:^(id delegate) { - if ([delegate respondsToSelector:@selector(deviceCachePrimed:)]) { - [delegate deviceCachePrimed:self]; - } - }]; + // Not needed since this is a state udpate now } - (oneway void)deviceConfigurationChanged:(NSNumber *)nodeID { + MTR_LOG("%@ %s", self, __PRETTY_FUNCTION__); if (!MTR_SAFE_CAST(nodeID, NSNumber)) { MTR_LOG_ERROR("%@ invalid deviceConfigurationChanged: nodeID: %@", self, nodeID); return; @@ -264,6 +242,7 @@ - (BOOL)_internalState:(NSDictionary *)dictionary hasValidValuesForKeys:(const N - (oneway void)device:(NSNumber *)nodeID internalStateUpdated:(NSDictionary *)dictionary { + MTR_LOG("%@ %s", self, __PRETTY_FUNCTION__); if (!MTR_SAFE_CAST(nodeID, NSNumber)) { MTR_LOG_ERROR("%@ invalid device:internalStateUpdated: nodeID: %@", self, nodeID); return; @@ -274,58 +253,73 @@ - (oneway void)device:(NSNumber *)nodeID internalStateUpdated:(NSDictionary *)di return; } + NSNumber * oldStateNumber = MTR_SAFE_CAST(self._internalState[kMTRDeviceInternalPropertyDeviceState], NSNumber); + NSNumber * newStateNumber = MTR_SAFE_CAST(dictionary[kMTRDeviceInternalPropertyDeviceState], NSNumber); + VerifyOrReturn([self _internalState:dictionary hasValidValuesForKeys:requiredInternalStateKeys valueRequired:YES]); VerifyOrReturn([self _internalState:dictionary hasValidValuesForKeys:optionalInternalStateKeys valueRequired:NO]); + // Save old state for comparison later + NSNumber * oldStateNumber = self._internalState[kMTRDeviceInternalPropertyDeviceState]; + NSNumber * newStateNumber = dictionary[kMTRDeviceInternalPropertyDeviceState]; [self _setInternalState:dictionary]; - MTR_LOG("%@ internal state updated", self); + + if (!MTREqualObjects(oldStateNumber, newStateNumber)) { + MTRDeviceState state = self.state; + [self _lockAndCallDelegatesWithBlock:^(id delegate) { + [delegate device:self stateChanged:state]; + }]; + } + + NSNumber * oldPrimedState = MTR_SAFE_CAST(self._internalState[kMTRDeviceInternalPropertyDeviceCachePrimed], NSNumber); + NSNumber * newPrimedState = MTR_SAFE_CAST(dictionary[kMTRDeviceInternalPropertyDeviceCachePrimed], NSNumber); + + if (!MTREqualObjects(oldPrimedState, newPrimedState)) { + [self _lockAndCallDelegatesWithBlock:^(id delegate) { + if ([delegate respondsToSelector:@selector(deviceCachePrimed:)]) { + [delegate deviceCachePrimed:self]; + } + }]; + } } -#pragma mark - Remote Commands +- (MTRDeviceState)state +{ + NSNumber * stateNumber = MTR_SAFE_CAST(self._internalState[kMTRDeviceInternalPropertyDeviceState], NSNumber); + return stateNumber ? static_cast(stateNumber.unsignedIntegerValue) : MTRDeviceStateUnknown; +} + +- (BOOL)deviceCachePrimed +{ + NSNumber * deviceCachePrimedNumber = MTR_SAFE_CAST(self._internalState[kMTRDeviceInternalPropertyDeviceCachePrimed], NSNumber); + return deviceCachePrimedNumber.boolValue; +} + +- (nullable NSDate *)estimatedStartTime +{ + return MTR_SAFE_CAST(self._internalState[kMTRDeviceInternalPropertyEstimatedStartTime], NSDate); +} -// TODO: Figure out how to validate the return values for the various -// MTR_DEVICE_*_XPC macros below. +- (nullable NSNumber *)estimatedSubscriptionLatency +{ + return MTR_SAFE_CAST(self._internalState[kMTRDeviceInternalPropertyEstimatedSubscriptionLatency], NSNumber); +} -MTR_DEVICE_SIMPLE_REMOTE_XPC_GETTER(state, MTRDeviceState, MTRDeviceStateUnknown, getStateWithReply) -MTR_DEVICE_SIMPLE_REMOTE_XPC_GETTER(deviceCachePrimed, BOOL, NO, getDeviceCachePrimedWithReply) -MTR_DEVICE_SIMPLE_REMOTE_XPC_GETTER(estimatedStartTime, NSDate * _Nullable, nil, getEstimatedStartTimeWithReply) -MTR_DEVICE_SIMPLE_REMOTE_XPC_GETTER(estimatedSubscriptionLatency, NSNumber * _Nullable, nil, getEstimatedSubscriptionLatencyWithReply) +#pragma mark - Remote Commands typedef NSDictionary * _Nullable ReadAttributeResponseType; -MTR_DEVICE_COMPLEX_REMOTE_XPC_GETTER(readAttributeWithEndpointID - : (NSNumber *) endpointID clusterID - : (NSNumber *) clusterID attributeID - : (NSNumber *) attributeID params - : (MTRReadParams * _Nullable) params, - ReadAttributeResponseType, - nil, - readAttributeWithEndpointID - : endpointID clusterID - : clusterID attributeID - : attributeID params - : params withReply) - -MTR_DEVICE_SIMPLE_REMOTE_XPC_COMMAND(writeAttributeWithEndpointID - : (NSNumber *) endpointID clusterID - : (NSNumber *) clusterID attributeID - : (NSNumber *) attributeID value - : (id) value expectedValueInterval - : (NSNumber *) expectedValueInterval timedWriteTimeout - : (NSNumber * _Nullable) timeout, writeAttributeWithEndpointID - : endpointID clusterID - : clusterID attributeID - : attributeID value - : value expectedValueInterval - : expectedValueInterval timedWriteTimeout - : timeout) +MTR_DEVICE_COMPLEX_REMOTE_XPC_GETTER(readAttributeWithEndpointID : (NSNumber *) endpointID clusterID : (NSNumber *) clusterID attributeID : (NSNumber *) attributeID params : (MTRReadParams * _Nullable) params, + ReadAttributeResponseType, + nil, + readAttributeWithEndpointID : endpointID clusterID : clusterID attributeID : attributeID params : params withReply) + +MTR_DEVICE_SIMPLE_REMOTE_XPC_COMMAND(writeAttributeWithEndpointID : (NSNumber *) endpointID clusterID : (NSNumber *) clusterID attributeID : (NSNumber *) attributeID value : (id) value expectedValueInterval : (NSNumber *) expectedValueInterval timedWriteTimeout : (NSNumber * _Nullable) timeout, writeAttributeWithEndpointID : endpointID clusterID : clusterID attributeID : attributeID value : value expectedValueInterval : expectedValueInterval timedWriteTimeout : timeout) typedef NSArray *> * ReadAttributePathsResponseType; -MTR_DEVICE_COMPLEX_REMOTE_XPC_GETTER(readAttributePaths - : (NSArray *) attributePaths, - ReadAttributePathsResponseType, - [NSArray array], // Default return value - readAttributePaths - : attributePaths withReply) +MTR_DEVICE_COMPLEX_REMOTE_XPC_GETTER(readAttributePaths : (NSArray *) attributePaths, + ReadAttributePathsResponseType, + [NSArray array], // Default return value + readAttributePaths : attributePaths withReply) - (void)_invokeCommandWithEndpointID:(NSNumber *)endpointID clusterID:(NSNumber *)clusterID diff --git a/src/darwin/Framework/CHIP/XPC Protocol/MTRXPCClientProtocol.h b/src/darwin/Framework/CHIP/XPC Protocol/MTRXPCClientProtocol.h index cfa59db8a73303..bd9048e4595f81 100644 --- a/src/darwin/Framework/CHIP/XPC Protocol/MTRXPCClientProtocol.h +++ b/src/darwin/Framework/CHIP/XPC Protocol/MTRXPCClientProtocol.h @@ -37,6 +37,8 @@ MTR_NEWLY_AVAILABLE //- (oneway void)controller:(NSUUID *)controller commissioningSessionEstablishmentDone:(NSError * _Nullable)error; //- (oneway void)controller:(NSUUID *)controller commissioningComplete:(NSError * _Nullable)error nodeID:(NSNumber * _Nullable)nodeID metrics:(MTRMetrics * _Nullable)metrics; //- (oneway void)controller:(NSUUID *)controller readCommissioningInfo:(MTRProductIdentity *)info; +@optional +- (oneway void)controller:(NSUUID *)controller controllerConfigurationUpdated:(NSDictionary *)configuration; @end MTR_NEWLY_AVAILABLE diff --git a/src/darwin/Framework/CHIP/XPC Protocol/MTRXPCServerProtocol.h b/src/darwin/Framework/CHIP/XPC Protocol/MTRXPCServerProtocol.h index 9ecec60ae52a41..1a468a133264bb 100644 --- a/src/darwin/Framework/CHIP/XPC Protocol/MTRXPCServerProtocol.h +++ b/src/darwin/Framework/CHIP/XPC Protocol/MTRXPCServerProtocol.h @@ -21,6 +21,9 @@ 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_EXTERN NSString * const MTRDeviceControllerRegistrationControllerNodeIDKey MTR_NEWLY_AVAILABLE; +MTR_EXTERN NSString * const MTRDeviceControllerRegistrationControllerIsRunningKey MTR_NEWLY_AVAILABLE; +MTR_EXTERN NSString * const MTRDeviceControllerRegistrationDeviceInternalStateKey MTR_NEWLY_AVAILABLE; MTR_NEWLY_AVAILABLE @protocol MTRXPCServerProtocol_MTRDevice @@ -54,10 +57,6 @@ 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; - // - (oneway void)deviceController:(NSUUID *)controller setupCommissioningSessionWithPayload:(MTRSetupPayload *)payload newNodeID:(NSNumber *)newNodeID withReply:(void (^)(BOOL success, NSError * _Nullable error))reply; // - (oneway void)deviceController:(NSUUID *)controller setupCommissioningSessionWithDiscoveredDevice:(MTRCommissionableBrowserResult *)discoveredDevice payload:(MTRSetupPayload *)payload newNodeID:(NSNumber *)newNodeID withReply:(void (^)(BOOL success, NSError * _Nullable error))reply; // - (oneway void)deviceController:(NSUUID *)controller commissionNodeWithID:(NSNumber *)nodeID commissioningParams:(MTRCommissioningParameters *)commissioningParams withReply:(void (^)(BOOL success, NSError * _Nullable error))reply; @@ -73,6 +72,7 @@ MTR_NEWLY_AVAILABLE - (oneway void)deviceController:(NSUUID *)controller registerNodeID:(NSNumber *)nodeID; - (oneway void)deviceController:(NSUUID *)controller unregisterNodeID:(NSNumber *)nodeID; +- (oneway void)deviceController:(NSUUID *)controller updateControllerConfiguration:(NSDictionary *)controllerState; @end @@ -80,7 +80,6 @@ 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 diff --git a/src/darwin/Framework/Matter.xcodeproj/project.pbxproj b/src/darwin/Framework/Matter.xcodeproj/project.pbxproj index 673f1cacaabc10..a21e90d726ec0b 100644 --- a/src/darwin/Framework/Matter.xcodeproj/project.pbxproj +++ b/src/darwin/Framework/Matter.xcodeproj/project.pbxproj @@ -1322,8 +1322,6 @@ isa = PBXGroup; children = ( D444F9A12C6E8058007761E5 /* XPC Protocol */, - 9B231B032C62EF650030EB37 /* MTRDeviceController_Concrete.mm */, - 9B0484F42C701154006C2D5F /* MTRDeviceController_Concrete.h */, 88E07D602B9A89A4005FD53E /* MTRMetricKeys.h */, 88FA798B2B7B257100CD4B6F /* MTRMetricsCollector.h */, 88FA798C2B7B257100CD4B6F /* MTRMetricsCollector.mm */, @@ -1404,11 +1402,13 @@ 75B3269B2BCDB9D600E17C4E /* MTRDeviceConnectivityMonitor.h */, 75B3269D2BCDB9EA00E17C4E /* MTRDeviceConnectivityMonitor.mm */, 991DC0822475F45400C13860 /* MTRDeviceController.h */, + 5136660F28067D540025EDAE /* MTRDeviceController_Internal.h */, + 991DC0872475F47D00C13860 /* MTRDeviceController.mm */, + 9B0484F42C701154006C2D5F /* MTRDeviceController_Concrete.h */, + 9B231B032C62EF650030EB37 /* MTRDeviceController_Concrete.mm */, 9BFE5D4E2C6D3075007D4319 /* MTRDeviceController_XPC.h */, 9B5CCB582C6E6FD3009DD99B /* MTRDeviceController_XPC_Internal.h */, 9BFE5D4F2C6D3075007D4319 /* MTRDeviceController_XPC.mm */, - 5136660F28067D540025EDAE /* MTRDeviceController_Internal.h */, - 991DC0872475F47D00C13860 /* MTRDeviceController.mm */, 5A7947E227C0101200434CF2 /* MTRDeviceController+XPC.h */, 5A7947E327C0129500434CF2 /* MTRDeviceController+XPC.mm */, 2CB7163E252F731E0026E2BB /* MTRDeviceControllerDelegate.h */, From 8d5dea870fa00b57aeb22d2c60c2649b2729c3c4 Mon Sep 17 00:00:00 2001 From: "Restyled.io" Date: Mon, 28 Oct 2024 23:44:13 +0000 Subject: [PATCH 02/13] Restyled by clang-format --- src/darwin/Framework/CHIP/MTRDevice_XPC.mm | 80 +++++++++++++++------- 1 file changed, 54 insertions(+), 26 deletions(-) diff --git a/src/darwin/Framework/CHIP/MTRDevice_XPC.mm b/src/darwin/Framework/CHIP/MTRDevice_XPC.mm index 665523e43ed6b5..86f5486a669a22 100644 --- a/src/darwin/Framework/CHIP/MTRDevice_XPC.mm +++ b/src/darwin/Framework/CHIP/MTRDevice_XPC.mm @@ -66,14 +66,20 @@ #import -#define MTR_DEVICE_SIMPLE_REMOTE_XPC_GETTER(NAME, TYPE, DEFAULT_VALUE, GETTER_NAME) \ - MTR_SIMPLE_REMOTE_XPC_GETTER([(MTRDeviceController_XPC *) [self deviceController] xpcConnection], NAME, TYPE, DEFAULT_VALUE, GETTER_NAME, deviceController : [[self deviceController] uniqueIdentifier] nodeID : [self nodeID]) +#define MTR_DEVICE_SIMPLE_REMOTE_XPC_GETTER(NAME, TYPE, DEFAULT_VALUE, GETTER_NAME) \ + MTR_SIMPLE_REMOTE_XPC_GETTER([(MTRDeviceController_XPC *) [self deviceController] xpcConnection], NAME, TYPE, DEFAULT_VALUE, GETTER_NAME, deviceController \ + : [[self deviceController] uniqueIdentifier] nodeID \ + : [self nodeID]) -#define MTR_DEVICE_COMPLEX_REMOTE_XPC_GETTER(SIGNATURE, TYPE, DEFAULT_VALUE, ADDITIONAL_ARGUMENTS) \ - MTR_COMPLEX_REMOTE_XPC_GETTER([(MTRDeviceController_XPC *) [self deviceController] xpcConnection], SIGNATURE, TYPE, DEFAULT_VALUE, ADDITIONAL_ARGUMENTS, deviceController : [[self deviceController] uniqueIdentifier] nodeID : [self nodeID]) +#define MTR_DEVICE_COMPLEX_REMOTE_XPC_GETTER(SIGNATURE, TYPE, DEFAULT_VALUE, ADDITIONAL_ARGUMENTS) \ + MTR_COMPLEX_REMOTE_XPC_GETTER([(MTRDeviceController_XPC *) [self deviceController] xpcConnection], SIGNATURE, TYPE, DEFAULT_VALUE, ADDITIONAL_ARGUMENTS, deviceController \ + : [[self deviceController] uniqueIdentifier] nodeID \ + : [self nodeID]) -#define MTR_DEVICE_SIMPLE_REMOTE_XPC_COMMAND(METHOD_SIGNATURE, ADDITIONAL_ARGUMENTS) \ - MTR_SIMPLE_REMOTE_XPC_COMMAND([(MTRDeviceController_XPC *) [self deviceController] xpcConnection], METHOD_SIGNATURE, ADDITIONAL_ARGUMENTS, deviceController : [[self deviceController] uniqueIdentifier] nodeID : [self nodeID]) +#define MTR_DEVICE_SIMPLE_REMOTE_XPC_COMMAND(METHOD_SIGNATURE, ADDITIONAL_ARGUMENTS) \ + MTR_SIMPLE_REMOTE_XPC_COMMAND([(MTRDeviceController_XPC *) [self deviceController] xpcConnection], METHOD_SIGNATURE, ADDITIONAL_ARGUMENTS, deviceController \ + : [[self deviceController] uniqueIdentifier] nodeID \ + : [self nodeID]) @implementation MTRDevice_XPC @@ -113,16 +119,16 @@ - (NSString *)description // MTR_OPTIONAL_ATTRIBUTE(kMTRDeviceInternalPropertyLastSubscriptionFailureTime, _lastSubscriptionFailureTimeForDescription, properties); return [NSString stringWithFormat:@"<%@: %p, node: %016llX-%016llX (%llu), VID: %@, PID: %@, WiFi: %@, Thread: %@, controller: %@ state: %ld>", - NSStringFromClass(self.class), self, - _deviceController.compressedFabricID.unsignedLongLongValue, - _nodeID.unsignedLongLongValue, - _nodeID.unsignedLongLongValue, - [self vendorID], - [self productID], - wifi, - thread, - _deviceController.uniqueIdentifier, - [self state]]; + NSStringFromClass(self.class), self, + _deviceController.compressedFabricID.unsignedLongLongValue, + _nodeID.unsignedLongLongValue, + _nodeID.unsignedLongLongValue, + [self vendorID], + [self productID], + wifi, + thread, + _deviceController.uniqueIdentifier, + [self state]]; } - (nullable NSNumber *)vendorID @@ -308,18 +314,40 @@ - (nullable NSNumber *)estimatedSubscriptionLatency #pragma mark - Remote Commands typedef NSDictionary * _Nullable ReadAttributeResponseType; -MTR_DEVICE_COMPLEX_REMOTE_XPC_GETTER(readAttributeWithEndpointID : (NSNumber *) endpointID clusterID : (NSNumber *) clusterID attributeID : (NSNumber *) attributeID params : (MTRReadParams * _Nullable) params, - ReadAttributeResponseType, - nil, - readAttributeWithEndpointID : endpointID clusterID : clusterID attributeID : attributeID params : params withReply) - -MTR_DEVICE_SIMPLE_REMOTE_XPC_COMMAND(writeAttributeWithEndpointID : (NSNumber *) endpointID clusterID : (NSNumber *) clusterID attributeID : (NSNumber *) attributeID value : (id) value expectedValueInterval : (NSNumber *) expectedValueInterval timedWriteTimeout : (NSNumber * _Nullable) timeout, writeAttributeWithEndpointID : endpointID clusterID : clusterID attributeID : attributeID value : value expectedValueInterval : expectedValueInterval timedWriteTimeout : timeout) +MTR_DEVICE_COMPLEX_REMOTE_XPC_GETTER(readAttributeWithEndpointID + : (NSNumber *) endpointID clusterID + : (NSNumber *) clusterID attributeID + : (NSNumber *) attributeID params + : (MTRReadParams * _Nullable) params, + ReadAttributeResponseType, + nil, + readAttributeWithEndpointID + : endpointID clusterID + : clusterID attributeID + : attributeID params + : params withReply) + +MTR_DEVICE_SIMPLE_REMOTE_XPC_COMMAND(writeAttributeWithEndpointID + : (NSNumber *) endpointID clusterID + : (NSNumber *) clusterID attributeID + : (NSNumber *) attributeID value + : (id) value expectedValueInterval + : (NSNumber *) expectedValueInterval timedWriteTimeout + : (NSNumber * _Nullable) timeout, writeAttributeWithEndpointID + : endpointID clusterID + : clusterID attributeID + : attributeID value + : value expectedValueInterval + : expectedValueInterval timedWriteTimeout + : timeout) typedef NSArray *> * ReadAttributePathsResponseType; -MTR_DEVICE_COMPLEX_REMOTE_XPC_GETTER(readAttributePaths : (NSArray *) attributePaths, - ReadAttributePathsResponseType, - [NSArray array], // Default return value - readAttributePaths : attributePaths withReply) +MTR_DEVICE_COMPLEX_REMOTE_XPC_GETTER(readAttributePaths + : (NSArray *) attributePaths, + ReadAttributePathsResponseType, + [NSArray array], // Default return value + readAttributePaths + : attributePaths withReply) - (void)_invokeCommandWithEndpointID:(NSNumber *)endpointID clusterID:(NSNumber *)clusterID From 90341dc5611e5c4bb65c82324ce7251810e62ff0 Mon Sep 17 00:00:00 2001 From: Justin Wood Date: Mon, 28 Oct 2024 16:47:52 -0700 Subject: [PATCH 03/13] Removing bad rebase --- src/darwin/Framework/CHIP/MTRDevice_XPC.mm | 3 --- 1 file changed, 3 deletions(-) diff --git a/src/darwin/Framework/CHIP/MTRDevice_XPC.mm b/src/darwin/Framework/CHIP/MTRDevice_XPC.mm index 86f5486a669a22..74eaf468d73789 100644 --- a/src/darwin/Framework/CHIP/MTRDevice_XPC.mm +++ b/src/darwin/Framework/CHIP/MTRDevice_XPC.mm @@ -265,9 +265,6 @@ - (oneway void)device:(NSNumber *)nodeID internalStateUpdated:(NSDictionary *)di VerifyOrReturn([self _internalState:dictionary hasValidValuesForKeys:requiredInternalStateKeys valueRequired:YES]); VerifyOrReturn([self _internalState:dictionary hasValidValuesForKeys:optionalInternalStateKeys valueRequired:NO]); - // Save old state for comparison later - NSNumber * oldStateNumber = self._internalState[kMTRDeviceInternalPropertyDeviceState]; - NSNumber * newStateNumber = dictionary[kMTRDeviceInternalPropertyDeviceState]; [self _setInternalState:dictionary]; if (!MTREqualObjects(oldStateNumber, newStateNumber)) { From f11293c20f5b87764f4af2d595e136882c4e386e Mon Sep 17 00:00:00 2001 From: Justin Wood Date: Mon, 28 Oct 2024 16:51:55 -0700 Subject: [PATCH 04/13] Update src/darwin/Framework/CHIP/MTRDevice_XPC.mm --- src/darwin/Framework/CHIP/MTRDevice_XPC.mm | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/darwin/Framework/CHIP/MTRDevice_XPC.mm b/src/darwin/Framework/CHIP/MTRDevice_XPC.mm index 74eaf468d73789..95d873c70e6c4e 100644 --- a/src/darwin/Framework/CHIP/MTRDevice_XPC.mm +++ b/src/darwin/Framework/CHIP/MTRDevice_XPC.mm @@ -203,7 +203,7 @@ - (oneway void)deviceBecameActive:(NSNumber *)nodeID - (oneway void)deviceCachePrimed:(NSNumber *)nodeID { - // Not needed since this is a state udpate now + // Not needed since this is a state update now } - (oneway void)deviceConfigurationChanged:(NSNumber *)nodeID From f75f3234cd5feceefeeef8305d23397a612bd78e Mon Sep 17 00:00:00 2001 From: Justin Wood Date: Mon, 28 Oct 2024 17:08:01 -0700 Subject: [PATCH 05/13] Fixing format --- src/darwin/Framework/CHIP/MTRDevice_XPC.mm | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/darwin/Framework/CHIP/MTRDevice_XPC.mm b/src/darwin/Framework/CHIP/MTRDevice_XPC.mm index 95d873c70e6c4e..4c50587b594be4 100644 --- a/src/darwin/Framework/CHIP/MTRDevice_XPC.mm +++ b/src/darwin/Framework/CHIP/MTRDevice_XPC.mm @@ -118,7 +118,7 @@ - (NSString *)description // MTR_OPTIONAL_ATTRIBUTE(kMTRDeviceInternalPropertyMostRecentReportTime, _mostRecentReportTimeForDescription, properties); // MTR_OPTIONAL_ATTRIBUTE(kMTRDeviceInternalPropertyLastSubscriptionFailureTime, _lastSubscriptionFailureTimeForDescription, properties); - return [NSString stringWithFormat:@"<%@: %p, node: %016llX-%016llX (%llu), VID: %@, PID: %@, WiFi: %@, Thread: %@, controller: %@ state: %ld>", + return [NSString stringWithFormat:@"<%@: %p, node: %016llX-%016llX (%llu), VID: %@, PID: %@, WiFi: %@, Thread: %@, controller: %@ state: %lu>", NSStringFromClass(self.class), self, _deviceController.compressedFabricID.unsignedLongLongValue, _nodeID.unsignedLongLongValue, @@ -128,7 +128,7 @@ - (NSString *)description wifi, thread, _deviceController.uniqueIdentifier, - [self state]]; + self.state]; } - (nullable NSNumber *)vendorID From be00a391fb739e0f6b842816159ac22481caa301 Mon Sep 17 00:00:00 2001 From: Justin Wood Date: Mon, 28 Oct 2024 17:26:00 -0700 Subject: [PATCH 06/13] Fixing format --- src/darwin/Framework/CHIP/MTRDevice_XPC.mm | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/darwin/Framework/CHIP/MTRDevice_XPC.mm b/src/darwin/Framework/CHIP/MTRDevice_XPC.mm index 4c50587b594be4..47579d31eee285 100644 --- a/src/darwin/Framework/CHIP/MTRDevice_XPC.mm +++ b/src/darwin/Framework/CHIP/MTRDevice_XPC.mm @@ -115,7 +115,7 @@ - (NSString *)description // TODO: Add these to the description // MTR_OPTIONAL_ATTRIBUTE(kMTRDeviceInternalPropertyLastSubscriptionAttemptWait, _lastSubscriptionAttemptWaitForDescription, properties); - // MTR_OPTIONAL_ATTRIBUTE(kMTRDeviceInternalPropertyMostRecentReportTime, _mostRecentReportTimeForDescription, properties); +// MTR_OPTIONAL_ATTRIBUTE(kMTRDeviceInternalPropertyMostRecentReportTime, _mostRecentReportTimeForDescription, properties); // MTR_OPTIONAL_ATTRIBUTE(kMTRDeviceInternalPropertyLastSubscriptionFailureTime, _lastSubscriptionFailureTimeForDescription, properties); return [NSString stringWithFormat:@"<%@: %p, node: %016llX-%016llX (%llu), VID: %@, PID: %@, WiFi: %@, Thread: %@, controller: %@ state: %lu>", @@ -128,7 +128,7 @@ - (NSString *)description wifi, thread, _deviceController.uniqueIdentifier, - self.state]; + (unsigned long)self.state]; } - (nullable NSNumber *)vendorID From 472c18cc2ced6dbdacb0bbe52bb2efe961ab0624 Mon Sep 17 00:00:00 2001 From: "Restyled.io" Date: Tue, 29 Oct 2024 00:26:42 +0000 Subject: [PATCH 07/13] Restyled by clang-format --- src/darwin/Framework/CHIP/MTRDevice_XPC.mm | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/darwin/Framework/CHIP/MTRDevice_XPC.mm b/src/darwin/Framework/CHIP/MTRDevice_XPC.mm index 47579d31eee285..7dfdf2e97a2ff3 100644 --- a/src/darwin/Framework/CHIP/MTRDevice_XPC.mm +++ b/src/darwin/Framework/CHIP/MTRDevice_XPC.mm @@ -115,7 +115,7 @@ - (NSString *)description // TODO: Add these to the description // MTR_OPTIONAL_ATTRIBUTE(kMTRDeviceInternalPropertyLastSubscriptionAttemptWait, _lastSubscriptionAttemptWaitForDescription, properties); -// MTR_OPTIONAL_ATTRIBUTE(kMTRDeviceInternalPropertyMostRecentReportTime, _mostRecentReportTimeForDescription, properties); + // MTR_OPTIONAL_ATTRIBUTE(kMTRDeviceInternalPropertyMostRecentReportTime, _mostRecentReportTimeForDescription, properties); // MTR_OPTIONAL_ATTRIBUTE(kMTRDeviceInternalPropertyLastSubscriptionFailureTime, _lastSubscriptionFailureTimeForDescription, properties); return [NSString stringWithFormat:@"<%@: %p, node: %016llX-%016llX (%llu), VID: %@, PID: %@, WiFi: %@, Thread: %@, controller: %@ state: %lu>", @@ -128,7 +128,7 @@ - (NSString *)description wifi, thread, _deviceController.uniqueIdentifier, - (unsigned long)self.state]; + (unsigned long) self.state]; } - (nullable NSNumber *)vendorID From 7f32eb816e1bdadf5d86d6e0fa06b79151051741 Mon Sep 17 00:00:00 2001 From: Justin Wood Date: Mon, 28 Oct 2024 22:32:01 -0700 Subject: [PATCH 08/13] Fixing --- .../Framework/CHIP/MTRDeviceController_XPC.mm | 61 +++++++++++++++++++ .../Framework/CHIP/MTRDevice_Concrete.mm | 34 ++++++++++- 2 files changed, 94 insertions(+), 1 deletion(-) diff --git a/src/darwin/Framework/CHIP/MTRDeviceController_XPC.mm b/src/darwin/Framework/CHIP/MTRDeviceController_XPC.mm index 676af86ef79a40..f33d1f1fc5a575 100644 --- a/src/darwin/Framework/CHIP/MTRDeviceController_XPC.mm +++ b/src/darwin/Framework/CHIP/MTRDeviceController_XPC.mm @@ -174,6 +174,13 @@ - (NSXPCInterface *)_interfaceForClientProtocol allowedClasses = [MTRDeviceController_XPC _allowedClasses]; + [interface setClasses:allowedClasses + forSelector:@selector(controller:controllerConfigurationUpdated:) + argumentIndex:1 + ofReply:NO]; + + allowedClasses = [MTRDeviceController_XPC _allowedClasses]; + [interface setClasses:allowedClasses forSelector:@selector(controller:controllerConfigurationUpdated:) argumentIndex:1 @@ -487,6 +494,60 @@ - (BOOL)isRunning return YES; } +- (oneway void)controller:(NSUUID *)controller controllerConfigurationUpdated:(NSDictionary *)configuration +{ + // Reuse the same format as config dictionary, and add values for internal states + // @{ + // MTRDeviceControllerRegistrationControllerContextKey: @{ + // MTRDeviceControllerRegistrationControllerNodeIDKey: controllerNodeID + // } + // MTRDeviceControllerRegistrationNodeIDsKey: @[ + // @{ + // MTRDeviceControllerRegistrationNodeIDKey: nodeID, + // MTRDeviceControllerRegistrationDeviceInternalStateKey: deviceInternalStateDictionary + // } + // ] + // } + + NSDictionary * controllerContext = MTR_SAFE_CAST(configuration[MTRDeviceControllerRegistrationControllerContextKey], NSDictionary); + NSNumber * controllerNodeID = MTR_SAFE_CAST(controllerContext[MTRDeviceControllerRegistrationControllerNodeIDKey], NSNumber); + if (controllerNodeID && controllerNodeID) { + _controllerNodeID = controllerContext[MTRDeviceControllerRegistrationControllerNodeIDKey]; + } + + NSArray * deviceInfoList = MTR_SAFE_CAST(configuration[MTRDeviceControllerRegistrationNodeIDsKey], NSArray); + + MTR_LOG("Received controllerConfigurationUpdated: controllerNode ID %@ deviceInfoList %@", self.controllerNodeID, deviceInfoList); + + for (NSDictionary * deviceInfo in deviceInfoList) { + if (!MTR_SAFE_CAST(deviceInfo, NSDictionary)) { + MTR_LOG_ERROR(" - Missing or malformed device Info"); + } + + NSNumber * nodeID = MTR_SAFE_CAST(deviceInfo[MTRDeviceControllerRegistrationNodeIDKey], NSNumber); + if (!nodeID) { + MTR_LOG_ERROR(" - Missing or malformed nodeID"); + continue; + } + + NSDictionary * deviceInternalState = MTR_SAFE_CAST(deviceInfo[MTRDeviceControllerRegistrationDeviceInternalStateKey], NSDictionary); + if (!deviceInternalState) { + MTR_LOG_ERROR(" - Missing or malformed deviceInternalState"); + continue; + } + + auto * device = static_cast([self deviceForNodeID:nodeID]); + [device device:nodeID internalStateUpdated:deviceInternalState]; + } +} + +// TODO: Create DeviceControllerRunningState that is a tri-state with "unknown", "not running", and "running" states +- (BOOL)isRunning +{ + // For XPC controller, always return yes + return YES; +} + // Not Supported via XPC //- (oneway void)controller:(NSUUID *)controller statusUpdate:(MTRCommissioningStatus)status { // } diff --git a/src/darwin/Framework/CHIP/MTRDevice_Concrete.mm b/src/darwin/Framework/CHIP/MTRDevice_Concrete.mm index c4511aed9016bc..16c68dd671a8be 100644 --- a/src/darwin/Framework/CHIP/MTRDevice_Concrete.mm +++ b/src/darwin/Framework/CHIP/MTRDevice_Concrete.mm @@ -981,6 +981,7 @@ - (void)_callDelegateDeviceCachePrimed [delegate deviceCachePrimed:self]; } }]; + [self _notifyDelegateOfPrivateInternalPropertiesChanges]; } // assume lock is held @@ -1015,6 +1016,7 @@ - (void)_changeState:(MTRDeviceState)state [self _callDelegatesWithBlock:^(id delegate) { [delegate device:self stateChanged:state]; }]; + [self _notifyDelegateOfPrivateInternalPropertiesChanges]; } else { MTR_LOG( "%@ Not reporting reachability state change, since no change in state %lu => %lu", self, static_cast(lastState), static_cast(state)); @@ -1072,6 +1074,15 @@ - (void)_updateEstimatedSubscriptionLatency:(NSNumber *)estimatedSubscriptionLat _estimatedSubscriptionLatencyForDescription = estimatedSubscriptionLatency; } +- (void)_updateEstimatedSubscriptionLatency:(NSNumber *)estimatedSubscriptionLatency +{ + os_unfair_lock_assert_owner(&_lock); + _estimatedSubscriptionLatency = estimatedSubscriptionLatency; + + std::lock_guard lock(_descriptionLock); + _estimatedSubscriptionLatencyForDescription = estimatedSubscriptionLatency; +} + // First Time Sync happens 2 minutes after reachability (this can be changed in the future) #define MTR_DEVICE_TIME_UPDATE_INITIAL_WAIT_TIME_SEC (60 * 2) - (void)_handleSubscriptionEstablished @@ -1464,6 +1475,7 @@ - (void)_handleUnsolicitedMessageFromPublisher [delegate deviceBecameActive:self]; } }]; + [self _notifyDelegateOfPrivateInternalPropertiesChanges]; // in case this is called during exponential back off of subscription // reestablishment, this starts the attempt right away @@ -1802,6 +1814,15 @@ - (void)_updateDeviceCachePrimed:(BOOL)deviceCachePrimed _deviceCachePrimedForDescription = deviceCachePrimed; } +- (void)_updateDeviceCachePrimed:(BOOL)deviceCachePrimed +{ + os_unfair_lock_assert_owner(&_lock); + _deviceCachePrimed = deviceCachePrimed; + + std::lock_guard lock(_descriptionLock); + _deviceCachePrimedForDescription = deviceCachePrimed; +} + - (void)_handleReportEnd { MTR_LOG("%@ handling report end", self); @@ -2042,6 +2063,15 @@ - (void)_updateEstimatedStartTime:(NSDate *)estimatedStartTime _estimatedStartTimeForDescription = _estimatedStartTime; } +- (void)_updateEstimatedStartTime:(NSDate *)estimatedStartTime +{ + os_unfair_lock_assert_owner(&_lock); + _estimatedStartTime = estimatedStartTime; + + std::lock_guard lock(_descriptionLock); + _estimatedStartTimeForDescription = _estimatedStartTime; +} + - (void)_handleEventReport:(NSArray *> *)eventReport { std::lock_guard lock(_lock); @@ -2467,7 +2497,9 @@ - (void)_setupSubscriptionWithReason:(NSString *)reason mtr_strongify(self); VerifyOrReturn(self); - [self _markDeviceAsUnreachableIfNeverSubscribed]; + if ( !HaveSubscriptionEstablishedRightNow(self->_internalDeviceState) ) + [self _markDeviceAsUnreachableIfNeverSubscribed]; + }); } From 07deb34672c3ff2aa2bbeea7c851488cbada2f38 Mon Sep 17 00:00:00 2001 From: "Restyled.io" Date: Tue, 29 Oct 2024 05:33:07 +0000 Subject: [PATCH 09/13] Restyled by clang-format --- src/darwin/Framework/CHIP/MTRDevice_Concrete.mm | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/src/darwin/Framework/CHIP/MTRDevice_Concrete.mm b/src/darwin/Framework/CHIP/MTRDevice_Concrete.mm index 16c68dd671a8be..ed7c2cbda847bb 100644 --- a/src/darwin/Framework/CHIP/MTRDevice_Concrete.mm +++ b/src/darwin/Framework/CHIP/MTRDevice_Concrete.mm @@ -2497,9 +2497,8 @@ - (void)_setupSubscriptionWithReason:(NSString *)reason mtr_strongify(self); VerifyOrReturn(self); - if ( !HaveSubscriptionEstablishedRightNow(self->_internalDeviceState) ) + if (!HaveSubscriptionEstablishedRightNow(self->_internalDeviceState)) [self _markDeviceAsUnreachableIfNeverSubscribed]; - }); } From d7ca8f043228d091c0575d8097e0a3aff6bc58b5 Mon Sep 17 00:00:00 2001 From: Justin Wood Date: Mon, 28 Oct 2024 23:06:23 -0700 Subject: [PATCH 10/13] fixing bad merge --- .../Framework/CHIP/MTRDeviceController_XPC.mm | 54 ------------------- .../Framework/CHIP/MTRDevice_Concrete.mm | 27 ---------- 2 files changed, 81 deletions(-) diff --git a/src/darwin/Framework/CHIP/MTRDeviceController_XPC.mm b/src/darwin/Framework/CHIP/MTRDeviceController_XPC.mm index f33d1f1fc5a575..09b20337e280c8 100644 --- a/src/darwin/Framework/CHIP/MTRDeviceController_XPC.mm +++ b/src/darwin/Framework/CHIP/MTRDeviceController_XPC.mm @@ -468,62 +468,9 @@ - (oneway void)controller:(NSUUID *)controller controllerConfigurationUpdated:(N for (NSDictionary * deviceInfo in deviceInfoList) { if (!MTR_SAFE_CAST(deviceInfo, NSDictionary)) { MTR_LOG_ERROR(" - Missing or malformed device Info"); - } - - NSNumber * nodeID = MTR_SAFE_CAST(deviceInfo[MTRDeviceControllerRegistrationNodeIDKey], NSNumber); - if (!nodeID) { - MTR_LOG_ERROR(" - Missing or malformed nodeID"); continue; } - NSDictionary * deviceInternalState = MTR_SAFE_CAST(deviceInfo[MTRDeviceControllerRegistrationDeviceInternalStateKey], NSDictionary); - if (!deviceInternalState) { - MTR_LOG_ERROR(" - Missing or malformed deviceInternalState"); - continue; - } - - auto * device = static_cast([self deviceForNodeID:nodeID]); - [device device:nodeID internalStateUpdated:deviceInternalState]; - } -} - -// TODO: Create DeviceControllerRunningState that is a tri-state with "unknown", "not running", and "running" states -- (BOOL)isRunning -{ - // For XPC controller, always return yes - return YES; -} - -- (oneway void)controller:(NSUUID *)controller controllerConfigurationUpdated:(NSDictionary *)configuration -{ - // Reuse the same format as config dictionary, and add values for internal states - // @{ - // MTRDeviceControllerRegistrationControllerContextKey: @{ - // MTRDeviceControllerRegistrationControllerNodeIDKey: controllerNodeID - // } - // MTRDeviceControllerRegistrationNodeIDsKey: @[ - // @{ - // MTRDeviceControllerRegistrationNodeIDKey: nodeID, - // MTRDeviceControllerRegistrationDeviceInternalStateKey: deviceInternalStateDictionary - // } - // ] - // } - - NSDictionary * controllerContext = MTR_SAFE_CAST(configuration[MTRDeviceControllerRegistrationControllerContextKey], NSDictionary); - NSNumber * controllerNodeID = MTR_SAFE_CAST(controllerContext[MTRDeviceControllerRegistrationControllerNodeIDKey], NSNumber); - if (controllerNodeID && controllerNodeID) { - _controllerNodeID = controllerContext[MTRDeviceControllerRegistrationControllerNodeIDKey]; - } - - NSArray * deviceInfoList = MTR_SAFE_CAST(configuration[MTRDeviceControllerRegistrationNodeIDsKey], NSArray); - - MTR_LOG("Received controllerConfigurationUpdated: controllerNode ID %@ deviceInfoList %@", self.controllerNodeID, deviceInfoList); - - for (NSDictionary * deviceInfo in deviceInfoList) { - if (!MTR_SAFE_CAST(deviceInfo, NSDictionary)) { - MTR_LOG_ERROR(" - Missing or malformed device Info"); - } - NSNumber * nodeID = MTR_SAFE_CAST(deviceInfo[MTRDeviceControllerRegistrationNodeIDKey], NSNumber); if (!nodeID) { MTR_LOG_ERROR(" - Missing or malformed nodeID"); @@ -541,7 +488,6 @@ - (oneway void)controller:(NSUUID *)controller controllerConfigurationUpdated:(N } } -// TODO: Create DeviceControllerRunningState that is a tri-state with "unknown", "not running", and "running" states - (BOOL)isRunning { // For XPC controller, always return yes diff --git a/src/darwin/Framework/CHIP/MTRDevice_Concrete.mm b/src/darwin/Framework/CHIP/MTRDevice_Concrete.mm index ed7c2cbda847bb..01d345582d9fae 100644 --- a/src/darwin/Framework/CHIP/MTRDevice_Concrete.mm +++ b/src/darwin/Framework/CHIP/MTRDevice_Concrete.mm @@ -1074,15 +1074,6 @@ - (void)_updateEstimatedSubscriptionLatency:(NSNumber *)estimatedSubscriptionLat _estimatedSubscriptionLatencyForDescription = estimatedSubscriptionLatency; } -- (void)_updateEstimatedSubscriptionLatency:(NSNumber *)estimatedSubscriptionLatency -{ - os_unfair_lock_assert_owner(&_lock); - _estimatedSubscriptionLatency = estimatedSubscriptionLatency; - - std::lock_guard lock(_descriptionLock); - _estimatedSubscriptionLatencyForDescription = estimatedSubscriptionLatency; -} - // First Time Sync happens 2 minutes after reachability (this can be changed in the future) #define MTR_DEVICE_TIME_UPDATE_INITIAL_WAIT_TIME_SEC (60 * 2) - (void)_handleSubscriptionEstablished @@ -1814,15 +1805,6 @@ - (void)_updateDeviceCachePrimed:(BOOL)deviceCachePrimed _deviceCachePrimedForDescription = deviceCachePrimed; } -- (void)_updateDeviceCachePrimed:(BOOL)deviceCachePrimed -{ - os_unfair_lock_assert_owner(&_lock); - _deviceCachePrimed = deviceCachePrimed; - - std::lock_guard lock(_descriptionLock); - _deviceCachePrimedForDescription = deviceCachePrimed; -} - - (void)_handleReportEnd { MTR_LOG("%@ handling report end", self); @@ -2063,15 +2045,6 @@ - (void)_updateEstimatedStartTime:(NSDate *)estimatedStartTime _estimatedStartTimeForDescription = _estimatedStartTime; } -- (void)_updateEstimatedStartTime:(NSDate *)estimatedStartTime -{ - os_unfair_lock_assert_owner(&_lock); - _estimatedStartTime = estimatedStartTime; - - std::lock_guard lock(_descriptionLock); - _estimatedStartTimeForDescription = _estimatedStartTime; -} - - (void)_handleEventReport:(NSArray *> *)eventReport { std::lock_guard lock(_lock); From fb4cf85ea26e0989af4b2ed020624eedb5b0e730 Mon Sep 17 00:00:00 2001 From: Justin Wood Date: Mon, 28 Oct 2024 23:07:30 -0700 Subject: [PATCH 11/13] fixing bad merge --- src/darwin/Framework/CHIP/MTRDeviceController_XPC.mm | 7 ------- 1 file changed, 7 deletions(-) diff --git a/src/darwin/Framework/CHIP/MTRDeviceController_XPC.mm b/src/darwin/Framework/CHIP/MTRDeviceController_XPC.mm index 09b20337e280c8..2d1a4e2440da41 100644 --- a/src/darwin/Framework/CHIP/MTRDeviceController_XPC.mm +++ b/src/darwin/Framework/CHIP/MTRDeviceController_XPC.mm @@ -174,13 +174,6 @@ - (NSXPCInterface *)_interfaceForClientProtocol allowedClasses = [MTRDeviceController_XPC _allowedClasses]; - [interface setClasses:allowedClasses - forSelector:@selector(controller:controllerConfigurationUpdated:) - argumentIndex:1 - ofReply:NO]; - - allowedClasses = [MTRDeviceController_XPC _allowedClasses]; - [interface setClasses:allowedClasses forSelector:@selector(controller:controllerConfigurationUpdated:) argumentIndex:1 From 99e156af3cb7c8d8f9b79f92a557c587445310ab Mon Sep 17 00:00:00 2001 From: Boris Zbarsky Date: Tue, 29 Oct 2024 11:08:58 -0400 Subject: [PATCH 12/13] Apply suggestions from code review --- src/darwin/Framework/CHIP/MTRDeviceController_XPC.mm | 2 +- src/darwin/Framework/CHIP/MTRDevice_XPC.mm | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/src/darwin/Framework/CHIP/MTRDeviceController_XPC.mm b/src/darwin/Framework/CHIP/MTRDeviceController_XPC.mm index 2d1a4e2440da41..d737e4d95d9856 100644 --- a/src/darwin/Framework/CHIP/MTRDeviceController_XPC.mm +++ b/src/darwin/Framework/CHIP/MTRDeviceController_XPC.mm @@ -450,7 +450,7 @@ - (oneway void)controller:(NSUUID *)controller controllerConfigurationUpdated:(N NSDictionary * controllerContext = MTR_SAFE_CAST(configuration[MTRDeviceControllerRegistrationControllerContextKey], NSDictionary); NSNumber * controllerNodeID = MTR_SAFE_CAST(controllerContext[MTRDeviceControllerRegistrationControllerNodeIDKey], NSNumber); - if (controllerNodeID && controllerNodeID) { + if (controllerContext && controllerNodeID) { _controllerNodeID = controllerContext[MTRDeviceControllerRegistrationControllerNodeIDKey]; } diff --git a/src/darwin/Framework/CHIP/MTRDevice_XPC.mm b/src/darwin/Framework/CHIP/MTRDevice_XPC.mm index 7dfdf2e97a2ff3..ec156cdcc5c0b3 100644 --- a/src/darwin/Framework/CHIP/MTRDevice_XPC.mm +++ b/src/darwin/Framework/CHIP/MTRDevice_XPC.mm @@ -133,12 +133,12 @@ - (NSString *)description - (nullable NSNumber *)vendorID { - return [self._internalState objectForKey:kMTRDeviceInternalPropertyKeyVendorID]; + return [[self._internalState objectForKey:kMTRDeviceInternalPropertyKeyVendorID] copy]; } - (nullable NSNumber *)productID { - return [self._internalState objectForKey:kMTRDeviceInternalPropertyKeyProductID]; + return [[self._internalState objectForKey:kMTRDeviceInternalPropertyKeyProductID] copy]; } #pragma mark - Client Callbacks (MTRDeviceDelegate) From 8cb98cfb81200cbec9a4cc4142c413c6b72c16d0 Mon Sep 17 00:00:00 2001 From: Boris Zbarsky Date: Tue, 29 Oct 2024 11:21:55 -0400 Subject: [PATCH 13/13] Remove unnecessary MTRDevice_Concrete changes. --- .../Framework/CHIP/MTRDevice_Concrete.mm | 92 ++++++------------- 1 file changed, 30 insertions(+), 62 deletions(-) diff --git a/src/darwin/Framework/CHIP/MTRDevice_Concrete.mm b/src/darwin/Framework/CHIP/MTRDevice_Concrete.mm index 01d345582d9fae..a7b5d255f8772d 100644 --- a/src/darwin/Framework/CHIP/MTRDevice_Concrete.mm +++ b/src/darwin/Framework/CHIP/MTRDevice_Concrete.mm @@ -356,14 +356,6 @@ @implementation MTRDevice_Concrete { NSDate * _Nullable _mostRecentReportTimeForDescription; // Copy of _lastSubscriptionFailureTime that is safe to use in description. NSDate * _Nullable _lastSubscriptionFailureTimeForDescription; - // Copy of _state that is safe to use in description. - MTRDeviceState _deviceStateForDescription; - // Copy of _deviceCachePrimed that is safe to use in description. - BOOL _deviceCachePrimedForDescription; - // Copy of _estimatedStartTime that is safe to use in description. - NSDate * _Nullable _estimatedStartTimeForDescription; - // Copy of _estimatedSubscriptionLatency that is safe to use in description. - NSNumber * _Nullable _estimatedSubscriptionLatencyForDescription; } // synthesize superclass property readwrite accessors @@ -483,20 +475,26 @@ - (NSString *)description - (NSDictionary *)_internalProperties { NSMutableDictionary * properties = [NSMutableDictionary dictionary]; - std::lock_guard lock(_descriptionLock); + { + std::lock_guard lock(_descriptionLock); + + MTR_OPTIONAL_ATTRIBUTE(kMTRDeviceInternalPropertyKeyVendorID, _vid, properties); + MTR_OPTIONAL_ATTRIBUTE(kMTRDeviceInternalPropertyKeyProductID, _pid, properties); + MTR_OPTIONAL_ATTRIBUTE(kMTRDeviceInternalPropertyNetworkFeatures, _allNetworkFeatures, properties); + MTR_OPTIONAL_ATTRIBUTE(kMTRDeviceInternalPropertyMostRecentReportTime, _mostRecentReportTimeForDescription, properties); + } - MTR_OPTIONAL_ATTRIBUTE(kMTRDeviceInternalPropertyKeyVendorID, _vid, properties); - MTR_OPTIONAL_ATTRIBUTE(kMTRDeviceInternalPropertyKeyProductID, _pid, properties); - MTR_OPTIONAL_ATTRIBUTE(kMTRDeviceInternalPropertyNetworkFeatures, _allNetworkFeatures, properties); - MTR_OPTIONAL_ATTRIBUTE(kMTRDeviceInternalPropertyDeviceInternalState, [NSNumber numberWithUnsignedInteger:_internalDeviceStateForDescription], properties); - MTR_OPTIONAL_ATTRIBUTE(kMTRDeviceInternalPropertyLastSubscriptionAttemptWait, [NSNumber numberWithUnsignedInt:_lastSubscriptionAttemptWaitForDescription], properties); - MTR_OPTIONAL_ATTRIBUTE(kMTRDeviceInternalPropertyMostRecentReportTime, _mostRecentReportTimeForDescription, properties); - MTR_OPTIONAL_ATTRIBUTE(kMTRDeviceInternalPropertyLastSubscriptionFailureTime, _lastSubscriptionFailureTimeForDescription, properties); + { + std::lock_guard lock(_lock); + MTR_OPTIONAL_ATTRIBUTE(kMTRDeviceInternalPropertyDeviceInternalState, [NSNumber numberWithUnsignedInteger:_internalDeviceState], properties); + MTR_OPTIONAL_ATTRIBUTE(kMTRDeviceInternalPropertyLastSubscriptionAttemptWait, [NSNumber numberWithUnsignedInt:_lastSubscriptionAttemptWait], properties); + MTR_OPTIONAL_ATTRIBUTE(kMTRDeviceInternalPropertyLastSubscriptionFailureTime, _lastSubscriptionFailureTime, properties); - MTR_OPTIONAL_ATTRIBUTE(kMTRDeviceInternalPropertyDeviceState, [NSNumber numberWithUnsignedInteger:_deviceStateForDescription], properties); - MTR_OPTIONAL_ATTRIBUTE(kMTRDeviceInternalPropertyDeviceCachePrimed, @(_deviceCachePrimedForDescription), properties); - MTR_OPTIONAL_ATTRIBUTE(kMTRDeviceInternalPropertyEstimatedStartTime, _estimatedStartTimeForDescription, properties); - MTR_OPTIONAL_ATTRIBUTE(kMTRDeviceInternalPropertyEstimatedSubscriptionLatency, _estimatedSubscriptionLatencyForDescription, properties); + MTR_OPTIONAL_ATTRIBUTE(kMTRDeviceInternalPropertyDeviceState, @(_state), properties); + MTR_OPTIONAL_ATTRIBUTE(kMTRDeviceInternalPropertyDeviceCachePrimed, @(_deviceCachePrimed), properties); + MTR_OPTIONAL_ATTRIBUTE(kMTRDeviceInternalPropertyEstimatedStartTime, _estimatedStartTime, properties); + MTR_OPTIONAL_ATTRIBUTE(kMTRDeviceInternalPropertyEstimatedSubscriptionLatency, _estimatedSubscriptionLatency, properties); + } return properties; } @@ -999,15 +997,11 @@ - (void)_changeState:(MTRDeviceState)state os_unfair_lock_assert_owner(&self->_lock); MTRDeviceState lastState = _state; _state = state; - { - std::lock_guard lock(_descriptionLock); - _deviceStateForDescription = _state; - } if (lastState != state) { if (state != MTRDeviceStateReachable) { MTR_LOG("%@ reachability state change %lu => %lu, set estimated start time to nil", self, static_cast(lastState), static_cast(state)); - [self _updateEstimatedStartTime:nil]; + _estimatedStartTime = nil; _estimatedStartTimeFromGeneralDiagnosticsUpTime = nil; } else { MTR_LOG( @@ -1065,15 +1059,6 @@ - (MTRInternalDeviceState)_getInternalState } #endif -- (void)_updateEstimatedSubscriptionLatency:(NSNumber *)estimatedSubscriptionLatency -{ - os_unfair_lock_assert_owner(&_lock); - _estimatedSubscriptionLatency = estimatedSubscriptionLatency; - - std::lock_guard lock(_descriptionLock); - _estimatedSubscriptionLatencyForDescription = estimatedSubscriptionLatency; -} - // First Time Sync happens 2 minutes after reachability (this can be changed in the future) #define MTR_DEVICE_TIME_UPDATE_INITIAL_WAIT_TIME_SEC (60 * 2) - (void)_handleSubscriptionEstablished @@ -1105,10 +1090,10 @@ - (void)_handleSubscriptionEstablished // way around. NSTimeInterval subscriptionLatency = -[initialSubscribeStart timeIntervalSinceNow]; if (_estimatedSubscriptionLatency == nil) { - [self _updateEstimatedSubscriptionLatency:@(subscriptionLatency)]; + _estimatedSubscriptionLatency = @(subscriptionLatency); } else { NSTimeInterval newSubscriptionLatencyEstimate = MTRDEVICE_SUBSCRIPTION_LATENCY_NEW_VALUE_WEIGHT * subscriptionLatency + (1 - MTRDEVICE_SUBSCRIPTION_LATENCY_NEW_VALUE_WEIGHT) * _estimatedSubscriptionLatency.doubleValue; - [self _updateEstimatedSubscriptionLatency:@(newSubscriptionLatencyEstimate)]; + _estimatedSubscriptionLatency = @(newSubscriptionLatencyEstimate); } [self _storePersistedDeviceData]; } @@ -1796,15 +1781,6 @@ - (void)setStorageBehaviorConfiguration:(MTRDeviceStorageBehaviorConfiguration * [self _resetStorageBehaviorState]; } -- (void)_updateDeviceCachePrimed:(BOOL)deviceCachePrimed -{ - os_unfair_lock_assert_owner(&_lock); - _deviceCachePrimed = deviceCachePrimed; - - std::lock_guard lock(_descriptionLock); - _deviceCachePrimedForDescription = deviceCachePrimed; -} - - (void)_handleReportEnd { MTR_LOG("%@ handling report end", self); @@ -1837,7 +1813,7 @@ - (void)_handleReportEnd if (!_deviceCachePrimed) { // This is the end of the priming sequence of data reports, so we have // all the data for the device now. - [self _updateDeviceCachePrimed:YES]; + _deviceCachePrimed = YES; [self _callDelegateDeviceCachePrimed]; [self _notifyDelegateOfPrivateInternalPropertiesChanges]; } @@ -2036,15 +2012,6 @@ - (BOOL)_interestedPaths:(NSArray * _Nullable)interestedPaths includesEventPath: return filteredEvents; } -- (void)_updateEstimatedStartTime:(NSDate *)estimatedStartTime -{ - os_unfair_lock_assert_owner(&_lock); - _estimatedStartTime = estimatedStartTime; - - std::lock_guard lock(_descriptionLock); - _estimatedStartTimeForDescription = _estimatedStartTime; -} - - (void)_handleEventReport:(NSArray *> *)eventReport { std::lock_guard lock(_lock); @@ -2090,11 +2057,11 @@ - (void)_handleEventReport:(NSArray *> *)eventRepor // If UpTime was received, make use of it as mark of system start time MTR_LOG("%@ StartUp event: set estimated start time forward to %@", self, _estimatedStartTimeFromGeneralDiagnosticsUpTime); - [self _updateEstimatedStartTime:_estimatedStartTimeFromGeneralDiagnosticsUpTime]; + _estimatedStartTime = _estimatedStartTimeFromGeneralDiagnosticsUpTime; } else { // If UpTime was not received, reset estimated start time in case of reboot MTR_LOG("%@ StartUp event: set estimated start time to nil", self); - [self _updateEstimatedStartTime:nil]; + _estimatedStartTime = nil; } } @@ -2114,7 +2081,7 @@ - (void)_handleEventReport:(NSArray *> *)eventRepor NSTimeInterval eventTimeValue = eventTimeValueNumber.doubleValue; NSDate * potentialSystemStartTime = [NSDate dateWithTimeIntervalSinceNow:-eventTimeValue]; if (!_estimatedStartTime || ([potentialSystemStartTime compare:_estimatedStartTime] == NSOrderedAscending)) { - [self _updateEstimatedStartTime:potentialSystemStartTime]; + _estimatedStartTime = potentialSystemStartTime; } } @@ -2470,8 +2437,9 @@ - (void)_setupSubscriptionWithReason:(NSString *)reason mtr_strongify(self); VerifyOrReturn(self); - if (!HaveSubscriptionEstablishedRightNow(self->_internalDeviceState)) + if (!HaveSubscriptionEstablishedRightNow(self->_internalDeviceState)) { [self _markDeviceAsUnreachableIfNeverSubscribed]; + } }); } @@ -3692,7 +3660,7 @@ - (NSArray *)_getAttributesToReportWithReportedValues:(NSArray %@", self, upTime, oldSystemStartTime, potentialSystemStartTime); - [self _updateEstimatedStartTime:potentialSystemStartTime]; + _estimatedStartTime = potentialSystemStartTime; } // Save estimate in the subscription resumption case, for when StartUp event uses it @@ -3784,7 +3752,7 @@ - (void)setPersistedClusterData:(NSDictionary *)data