From e08232f52569cc66f47c8cf8ffad57de0530f22b Mon Sep 17 00:00:00 2001 From: Boris Zbarsky Date: Thu, 19 Sep 2024 15:05:00 -0400 Subject: [PATCH] Mark setupDeviceForNodeID: on MTRDeviceController as abstract. 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. --- .../Framework/CHIP/MTRDeviceController.mm | 38 +------------------ .../CHIP/MTRDeviceController_Concrete.mm | 24 ------------ .../CHIP/MTRDeviceController_Internal.h | 4 ++ 3 files changed, 6 insertions(+), 60 deletions(-) diff --git a/src/darwin/Framework/CHIP/MTRDeviceController.mm b/src/darwin/Framework/CHIP/MTRDeviceController.mm index e39a4d14e75612..5ea62c4b6c34ff 100644 --- a/src/darwin/Framework/CHIP/MTRDeviceController.mm +++ b/src/darwin/Framework/CHIP/MTRDeviceController.mm @@ -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 *)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(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 diff --git a/src/darwin/Framework/CHIP/MTRDeviceController_Concrete.mm b/src/darwin/Framework/CHIP/MTRDeviceController_Concrete.mm index 97f47a50faca06..8ef2ff9903c89c 100644 --- a/src/darwin/Framework/CHIP/MTRDeviceController_Concrete.mm +++ b/src/darwin/Framework/CHIP/MTRDeviceController_Concrete.mm @@ -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 *)unitTestGetDeviceAttributeCounts { diff --git a/src/darwin/Framework/CHIP/MTRDeviceController_Internal.h b/src/darwin/Framework/CHIP/MTRDeviceController_Internal.h index 993298234f33ef..298193023c9edb 100644 --- a/src/darwin/Framework/CHIP/MTRDeviceController_Internal.h +++ b/src/darwin/Framework/CHIP/MTRDeviceController_Internal.h @@ -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 *)prefetchedClusterData; - (void)removeDevice:(MTRDevice *)device;