From 9b9613f2ffb40aaf8228ef18ac13c0b8e63ddda8 Mon Sep 17 00:00:00 2001 From: Boris Zbarsky Date: Wed, 25 Sep 2024 18:57:53 -0400 Subject: [PATCH] Push down the deviceController ivar into MTRDevice subclasses. This allows them to store the specific controller types they are associated with, which will allow us to move APIs that only make sense for a particular type to that type. --- src/darwin/Framework/CHIP/MTRDevice.mm | 12 +++++++----- src/darwin/Framework/CHIP/MTRDevice_Concrete.mm | 8 ++++++++ src/darwin/Framework/CHIP/MTRDevice_Internal.h | 4 ---- src/darwin/Framework/CHIP/MTRDevice_XPC.mm | 11 +++++++++-- 4 files changed, 24 insertions(+), 11 deletions(-) diff --git a/src/darwin/Framework/CHIP/MTRDevice.mm b/src/darwin/Framework/CHIP/MTRDevice.mm index 552c99c7e873b4..dbc602155f2741 100644 --- a/src/darwin/Framework/CHIP/MTRDevice.mm +++ b/src/darwin/Framework/CHIP/MTRDevice.mm @@ -282,12 +282,14 @@ @implementation MTRDevice { NSNumber * _Nullable _allNetworkFeatures; } +// deviceController getter is implemented by subclasses. +@dynamic deviceController; + - (instancetype)initForSubclassesWithNodeID:(NSNumber *)nodeID controller:(MTRDeviceController *)controller { if (self = [super init]) { _lock = OS_UNFAIR_LOCK_INIT; _delegates = [NSMutableSet set]; - _deviceController = controller; _nodeID = nodeID; _state = MTRDeviceStateUnknown; } @@ -606,7 +608,7 @@ - (void)_persistClusterData // storage implementation, which will try to read them later. Make sure // we snapshot the state here instead of handing out live copies. NSDictionary * clusterData = [self _clusterDataToPersistSnapshot]; - [_deviceController.controllerDataStore storeClusterData:clusterData forNodeID:_nodeID]; + [self.deviceController.controllerDataStore storeClusterData:clusterData forNodeID:_nodeID]; for (MTRClusterPath * clusterPath in _clusterDataToPersist) { [_persistedClusterData setObject:_clusterDataToPersist[clusterPath] forKey:clusterPath]; [_persistedClusters addObject:clusterPath]; @@ -849,7 +851,7 @@ - (void)_reconcilePersistedClustersWithStorage NSMutableSet * clusterPathsToRemove = [NSMutableSet set]; for (MTRClusterPath * clusterPath in _persistedClusters) { - MTRDeviceClusterData * data = [_deviceController.controllerDataStore getStoredClusterDataForNodeID:_nodeID endpointID:clusterPath.endpoint clusterID:clusterPath.cluster]; + MTRDeviceClusterData * data = [self.deviceController.controllerDataStore getStoredClusterDataForNodeID:_nodeID endpointID:clusterPath.endpoint clusterID:clusterPath.cluster]; if (!data) { [clusterPathsToRemove addObject:clusterPath]; } @@ -884,13 +886,13 @@ - (nullable MTRDeviceClusterData *)_clusterDataForPath:(MTRClusterPath *)cluster return nil; } - NSAssert(_deviceController.controllerDataStore != nil, + NSAssert(self.deviceController.controllerDataStore != nil, @"How can _persistedClusters have an entry if we have no persistence?"); NSAssert(_persistedClusterData != nil, @"How can _persistedClusterData not exist if we have persisted clusters?"); // Page in the stored value for the data. - MTRDeviceClusterData * data = [_deviceController.controllerDataStore getStoredClusterDataForNodeID:_nodeID endpointID:clusterPath.endpoint clusterID:clusterPath.cluster]; + MTRDeviceClusterData * data = [self.deviceController.controllerDataStore getStoredClusterDataForNodeID:_nodeID endpointID:clusterPath.endpoint clusterID:clusterPath.cluster]; MTR_LOG("%@ cluster path %@ cache miss - load from storage success %@", self, clusterPath, MTR_YES_NO(data)); if (data != nil) { [_persistedClusterData setObject:data forKey:clusterPath]; diff --git a/src/darwin/Framework/CHIP/MTRDevice_Concrete.mm b/src/darwin/Framework/CHIP/MTRDevice_Concrete.mm index a2dbb2dcbaf35d..e1d25f24b167ed 100644 --- a/src/darwin/Framework/CHIP/MTRDevice_Concrete.mm +++ b/src/darwin/Framework/CHIP/MTRDevice_Concrete.mm @@ -272,6 +272,8 @@ - (BOOL)unitTestSuppressTimeBasedReachabilityChanges:(MTRDevice *)device; #endif @implementation MTRDevice_Concrete { + MTRDeviceController_Concrete * _deviceController; + #ifdef DEBUG NSUInteger _unitTestAttributesReportedSinceLastCheck; #endif @@ -365,6 +367,7 @@ - (instancetype)initWithNodeID:(NSNumber *)nodeID controller:(MTRDeviceControlle { // `super` was NSObject, is now MTRDevice. MTRDevice hides its `init` if (self = [super initForSubclassesWithNodeID:nodeID controller:controller]) { + _deviceController = controller; _timeSyncLock = OS_UNFAIR_LOCK_INIT; _descriptionLock = OS_UNFAIR_LOCK_INIT; _fabricIndex = controller.fabricIndex; @@ -465,6 +468,11 @@ - (NSString *)description stringWithFormat:@"<%@: %p, node: %016llX-%016llX (%llu), VID: %@, PID: %@, WiFi: %@, Thread: %@, state: %@, last subscription attempt wait: %lus, queued work: %lu, last report: %@%@, last subscription failure: %@%@, controller: %@>", NSStringFromClass(self.class), self, _deviceController.compressedFabricID.unsignedLongLongValue, _nodeID.unsignedLongLongValue, _nodeID.unsignedLongLongValue, vid, pid, wifi, thread, InternalDeviceStateString(internalDeviceState), static_cast(lastSubscriptionAttemptWait), static_cast(_asyncWorkQueue.itemCount), mostRecentReportTime, reportAge, lastSubscriptionFailureTime, subscriptionFailureAge, _deviceController.uniqueIdentifier]; } +- (nullable MTRDeviceController *)deviceController +{ + return _deviceController; +} + - (NSDictionary *)_internalProperties { NSMutableDictionary * properties = [NSMutableDictionary dictionary]; diff --git a/src/darwin/Framework/CHIP/MTRDevice_Internal.h b/src/darwin/Framework/CHIP/MTRDevice_Internal.h index 6aea99eeda1ffb..70840224b8868d 100644 --- a/src/darwin/Framework/CHIP/MTRDevice_Internal.h +++ b/src/darwin/Framework/CHIP/MTRDevice_Internal.h @@ -116,10 +116,6 @@ MTR_DIRECT_MEMBERS // Our node ID, with the ivar declared explicitly so it's accessible to // subclasses. NSNumber * _nodeID; - - // Our controller. Declared nullable because our property is, though in - // practice it does not look like we ever set it to nil. - MTRDeviceController * _Nullable _deviceController; } - (instancetype)initForSubclassesWithNodeID:(NSNumber *)nodeID controller:(MTRDeviceController *)controller; diff --git a/src/darwin/Framework/CHIP/MTRDevice_XPC.mm b/src/darwin/Framework/CHIP/MTRDevice_XPC.mm index 015736e179c2c4..2ca00d2c09720a 100644 --- a/src/darwin/Framework/CHIP/MTRDevice_XPC.mm +++ b/src/darwin/Framework/CHIP/MTRDevice_XPC.mm @@ -80,14 +80,16 @@ : [[self deviceController] uniqueIdentifier] nodeID \ : [self nodeID]) -@implementation MTRDevice_XPC +@implementation MTRDevice_XPC { + MTRDeviceController_XPC * _deviceController; +} @synthesize _internalState; - (instancetype)initWithNodeID:(NSNumber *)nodeID controller:(MTRDeviceController_XPC *)controller { if (self = [super initForSubclassesWithNodeID:nodeID controller:controller]) { - // Nothing else to do, all set. + _deviceController = controller; } return self; @@ -131,6 +133,11 @@ - (NSString *)description _deviceController.uniqueIdentifier]; } +- (nullable MTRDeviceController *)deviceController +{ + return _deviceController; +} + #pragma mark - Client Callbacks (MTRDeviceDelegate) // required methods for MTRDeviceDelegates