Skip to content

Commit

Permalink
Push down the deviceController ivar into MTRDevice subclasses.
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
bzbarsky-apple committed Sep 25, 2024
1 parent 67e42c1 commit 9b9613f
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 11 deletions.
12 changes: 7 additions & 5 deletions src/darwin/Framework/CHIP/MTRDevice.mm
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down Expand Up @@ -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<MTRClusterPath *, MTRDeviceClusterData *> * 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];
Expand Down Expand Up @@ -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];
}
Expand Down Expand Up @@ -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];
Expand Down
8 changes: 8 additions & 0 deletions src/darwin/Framework/CHIP/MTRDevice_Concrete.mm
Original file line number Diff line number Diff line change
Expand Up @@ -272,6 +272,8 @@ - (BOOL)unitTestSuppressTimeBasedReachabilityChanges:(MTRDevice *)device;
#endif

@implementation MTRDevice_Concrete {
MTRDeviceController_Concrete * _deviceController;

#ifdef DEBUG
NSUInteger _unitTestAttributesReportedSinceLastCheck;
#endif
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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<unsigned long>(lastSubscriptionAttemptWait), static_cast<unsigned long>(_asyncWorkQueue.itemCount), mostRecentReportTime, reportAge, lastSubscriptionFailureTime, subscriptionFailureAge, _deviceController.uniqueIdentifier];
}

- (nullable MTRDeviceController *)deviceController
{
return _deviceController;
}

- (NSDictionary *)_internalProperties
{
NSMutableDictionary * properties = [NSMutableDictionary dictionary];
Expand Down
4 changes: 0 additions & 4 deletions src/darwin/Framework/CHIP/MTRDevice_Internal.h
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
11 changes: 9 additions & 2 deletions src/darwin/Framework/CHIP/MTRDevice_XPC.mm
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -131,6 +133,11 @@ - (NSString *)description
_deviceController.uniqueIdentifier];
}

- (nullable MTRDeviceController *)deviceController
{
return _deviceController;
}

#pragma mark - Client Callbacks (MTRDeviceDelegate)

// required methods for MTRDeviceDelegates
Expand Down

0 comments on commit 9b9613f

Please sign in to comment.