Skip to content

Commit

Permalink
Mark setupDeviceForNodeID: on MTRDeviceController as abstract.
Browse files Browse the repository at this point in the history
This is overridden by the XPC and Concrete implementations.

Also, there is no need to override deviceForNodeID and removeDevice in
MTRDeviceController_Concrete, so those overrides are removed.
  • Loading branch information
bzbarsky-apple committed Sep 19, 2024
1 parent 12f04dc commit e08232f
Show file tree
Hide file tree
Showing 3 changed files with 6 additions and 60 deletions.
38 changes: 2 additions & 36 deletions src/darwin/Framework/CHIP/MTRDeviceController.mm
Original file line number Diff line number Diff line change
Expand Up @@ -1246,44 +1246,10 @@ - (MTRBaseDevice *)baseDeviceForNodeID:(NSNumber *)nodeID
return [[MTRBaseDevice alloc] initWithNodeID:nodeID controller:self];
}

// If prefetchedClusterData is not provided, load attributes individually from controller data store
- (MTRDevice *)_setupDeviceForNodeID:(NSNumber *)nodeID prefetchedClusterData:(NSDictionary<MTRClusterPath *, MTRDeviceClusterData *> *)prefetchedClusterData
{
os_unfair_lock_assert_owner(self.deviceMapLock);

MTRDevice * deviceToReturn = [[MTRDevice_Concrete alloc] initWithNodeID:nodeID controller:self];
// If we're not running, don't add the device to our map. That would
// create a cycle that nothing would break. Just return the device,
// which will be in exactly the state it would be in if it were created
// while we were running and then we got shut down.
if ([self isRunning]) {
[_nodeIDToDeviceMap setObject:deviceToReturn forKey:nodeID];
}

if (prefetchedClusterData) {
if (prefetchedClusterData.count) {
[deviceToReturn setPersistedClusterData:prefetchedClusterData];
}
} else if (_controllerDataStore) {
// Load persisted cluster data if they exist.
NSDictionary * clusterData = [_controllerDataStore getStoredClusterDataForNodeID:nodeID];
MTR_LOG("%@ Loaded %lu cluster data from storage for %@", self, static_cast<unsigned long>(clusterData.count), deviceToReturn);
if (clusterData.count) {
[deviceToReturn setPersistedClusterData:clusterData];
}
}

// TODO: Figure out how to get the device data as part of our bulk-read bits.
if (_controllerDataStore) {
auto * deviceData = [_controllerDataStore getStoredDeviceDataForNodeID:nodeID];
if (deviceData.count) {
[deviceToReturn setPersistedDeviceData:deviceData];
}
}

[deviceToReturn setStorageBehaviorConfiguration:_storageBehaviorConfiguration];

return deviceToReturn;
MTR_ABSTRACT_METHOD();
return nil;
}

- (MTRDevice *)deviceForNodeID:(NSNumber *)nodeID
Expand Down
24 changes: 0 additions & 24 deletions src/darwin/Framework/CHIP/MTRDeviceController_Concrete.mm
Original file line number Diff line number Diff line change
Expand Up @@ -1223,30 +1223,6 @@ - (MTRDevice *)_setupDeviceForNodeID:(NSNumber *)nodeID prefetchedClusterData:(N
return deviceToReturn;
}

- (MTRDevice *)deviceForNodeID:(NSNumber *)nodeID
{
std::lock_guard lock(*self.deviceMapLock);
MTRDevice * deviceToReturn = [self.nodeIDToDeviceMap objectForKey:nodeID];
if (!deviceToReturn) {
deviceToReturn = [self _setupDeviceForNodeID:nodeID prefetchedClusterData:nil];
}

return deviceToReturn;
}

- (void)removeDevice:(MTRDevice *)device
{
std::lock_guard lock(*self.deviceMapLock);
auto * nodeID = device.nodeID;
MTRDevice * deviceToRemove = [self.nodeIDToDeviceMap objectForKey:nodeID];
if (deviceToRemove == device) {
[deviceToRemove invalidate];
[self.nodeIDToDeviceMap removeObjectForKey:nodeID];
} else {
MTR_LOG_ERROR("%@ Error: Cannot remove device %p with nodeID %llu", self, device, nodeID.unsignedLongLongValue);
}
}

#ifdef DEBUG
- (NSDictionary<NSNumber *, NSNumber *> *)unitTestGetDeviceAttributeCounts
{
Expand Down
4 changes: 4 additions & 0 deletions src/darwin/Framework/CHIP/MTRDeviceController_Internal.h
Original file line number Diff line number Diff line change
Expand Up @@ -298,6 +298,10 @@ NS_ASSUME_NONNULL_BEGIN
#pragma mark - Device-specific data and SDK access
// DeviceController will act as a central repository for this opaque dictionary that MTRDevice manages
- (MTRDevice *)deviceForNodeID:(NSNumber *)nodeID;
/**
* _setupDeviceForNodeID is a hook expected to be implemented by subclasses to
* actually allocate a device object of the right type.
*/
- (MTRDevice *)_setupDeviceForNodeID:(NSNumber *)nodeID prefetchedClusterData:(nullable NSDictionary<MTRClusterPath *, MTRDeviceClusterData *> *)prefetchedClusterData;
- (void)removeDevice:(MTRDevice *)device;

Expand Down

0 comments on commit e08232f

Please sign in to comment.