Skip to content

Commit

Permalink
Mark setupDeviceForNodeID: on MTRDeviceController as abstract. (proje…
Browse files Browse the repository at this point in the history
…ct-chip#35679)

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 authored and yyzhong-g committed Dec 11, 2024
1 parent d1c3a82 commit b0c489a
Show file tree
Hide file tree
Showing 4 changed files with 7 additions and 61 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 @@ -780,44 +780,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
2 changes: 1 addition & 1 deletion src/darwin/Framework/CHIP/MTRDeviceControllerParameters.h
Original file line number Diff line number Diff line change
Expand Up @@ -166,7 +166,7 @@ MTR_NEWLY_AVAILABLE
/**
* A controller created from this way will connect to a remote instance of an MTRDeviceController loaded in an XPC Service
*
* @param xpcConnectionBlock The XPC Connection block that will return an NSXPCConnection to the indended listener.
* @param xpcConnectionBlock The XPC Connection block that will return an NSXPCConnection to the intended listener.
*
* @param uniqueIdentifier The unique id to assign to the controller.
*
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 @@ -1193,30 +1193,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 @@ -268,6 +268,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 b0c489a

Please sign in to comment.