Skip to content

Commit c253683

Browse files
committed
Added helpers for removing endpoints, clusters and attributes
1 parent 100dcd3 commit c253683

File tree

3 files changed

+70
-51
lines changed

3 files changed

+70
-51
lines changed

src/darwin/Framework/CHIP/MTRDevice.mm

+67-49
Original file line numberDiff line numberDiff line change
@@ -2773,6 +2773,70 @@ - (void)_removeAttributes:(NSSet *)toBeRemovedAttributes fromCluster:(MTRCluster
27732773
[self.deviceController.controllerDataStore clearStoredClusterDataForNodeID:self.nodeID endpointID:clusterPathToRemoveAttributesFrom.endpoint clusterID:clusterPathToRemoveAttributesFrom.cluster];
27742774
}
27752775

2776+
- (void) _pruneOrphanedEndpoints:(NSDictionary *)previousPartsListValue
2777+
newPartsListValue:(NSDictionary *)newPartsListValue
2778+
{
2779+
// If the parts list changed and one or more endpoints were removed, remove all the clusters in _persistedClusters, _persistedClusterData and _clusterDataToPersist for all those endpoints.
2780+
// Also remove it from the data store.
2781+
NSMutableSet * toBeRemovedEndpoints = [NSMutableSet setWithArray:[self arrayOfNumbersFromAttributeValue:[self _dataValueWithoutDataVersion:previousPartsListValue]]];
2782+
NSSet * endpointsOnDevice = [NSSet setWithArray:[self arrayOfNumbersFromAttributeValue:newPartsListValue]];
2783+
[toBeRemovedEndpoints minusSet:endpointsOnDevice];
2784+
2785+
for (NSNumber * endpoint in toBeRemovedEndpoints) {
2786+
NSMutableSet<MTRClusterPath *> * clusterPathsToRemove = [[NSMutableSet alloc] init];
2787+
for (MTRClusterPath * path in _persistedClusters) {
2788+
if ([path.endpoint isEqualToNumber:endpoint]) {
2789+
[clusterPathsToRemove addObject:path];
2790+
}
2791+
}
2792+
[self _removeClusters:[clusterPathsToRemove copy]];
2793+
[self.deviceController.controllerDataStore removeEndpointFromEndpointIndex:endpoint forNodeID:self.nodeID];
2794+
}
2795+
}
2796+
2797+
- (void) _pruneOrphanedClusters:(MTRAttributePath *)attributePath
2798+
previousServerListValue:(NSDictionary *)previousServerListValue
2799+
newServerListValue:(NSDictionary *)newServerListValue
2800+
{
2801+
// If the server list changed and clusters were removed, remove the clusters from the _persistedClusters, _persistedClusterData and _clusterDataToPersist for all those clusters.
2802+
// Also remove it from the data store.
2803+
NSMutableSet<NSNumber *> * toBeRemovedClusters = [NSMutableSet setWithArray:[self arrayOfNumbersFromAttributeValue:[self _dataValueWithoutDataVersion:previousServerListValue]]];
2804+
NSSet<NSNumber *> * clustersStillOnEndpoint = [NSSet setWithArray:[self arrayOfNumbersFromAttributeValue:newServerListValue]];
2805+
[toBeRemovedClusters minusSet:clustersStillOnEndpoint];
2806+
2807+
NSMutableSet<MTRClusterPath *> * clusterPathsToRemove = [[NSMutableSet alloc] init];
2808+
for (NSNumber * cluster in toBeRemovedClusters) {
2809+
for (MTRClusterPath * path in _persistedClusters) {
2810+
if ([path.endpoint isEqualToNumber:attributePath.endpoint] && [path.cluster isEqualToNumber:cluster]) {
2811+
[clusterPathsToRemove addObject:path];
2812+
}
2813+
}
2814+
}
2815+
[self _removeClusters:[clusterPathsToRemove copy]];
2816+
}
2817+
2818+
- (void) _pruneOrphanedAttributes:(MTRAttributePath *)attributePath
2819+
newAttributeListValue:(NSDictionary *)newAttributeListValue
2820+
{
2821+
// If the attribute list changed and attributes were removed, remove the attributes from the _clusterDataToPersist for that cluster and endpoint.
2822+
// Clear out the _peristedClusterData and data store cluster data. Update the data storage with updated cluster data.
2823+
NSMutableSet<NSNumber *> * toBeRemovedAttributes = [NSMutableSet setWithArray:[self arrayOfNumbersFromAttributeValue:[self _cachedAttributeValueForPath:attributePath]]];
2824+
NSSet<NSNumber *> * attributesStillInCluster = [NSSet setWithArray:[self arrayOfNumbersFromAttributeValue:newAttributeListValue]];
2825+
2826+
[toBeRemovedAttributes minusSet:attributesStillInCluster];
2827+
MTRClusterPath * clusterPathToRemoveAttributesFrom;
2828+
for (MTRClusterPath * path in _persistedClusters) {
2829+
if ([path.endpoint isEqualToNumber:attributePath.endpoint] && [path.cluster isEqualToNumber:attributePath.cluster]) {
2830+
clusterPathToRemoveAttributesFrom = path;
2831+
break;
2832+
}
2833+
}
2834+
[self _removeAttributes:[toBeRemovedAttributes copy] fromCluster:clusterPathToRemoveAttributesFrom];
2835+
[self.deviceController.controllerDataStore removeAttributes:[toBeRemovedAttributes copy] fromCluster:clusterPathToRemoveAttributesFrom forNodeID:self.nodeID];
2836+
}
2837+
2838+
2839+
27762840
- (void)_pruneOrphanedEndpointsAndClusters:(MTRAttributePath *)attributePath
27772841
previousValue:(NSDictionary *)previousValue
27782842
attributeDataValue:(NSDictionary *)attributeDataValue
@@ -2788,60 +2852,14 @@ - (void)_pruneOrphanedEndpointsAndClusters:(MTRAttributePath *)attributePath
27882852
// If yes, we might need to prune any deleted endpoints, clusters or attributes from the storage and persisted cluster data.
27892853
if (attributePath.cluster.unsignedLongValue == MTRClusterIDTypeDescriptorID) {
27902854
if (attributePath.attribute.unsignedLongValue == MTRAttributeIDTypeClusterDescriptorAttributePartsListID && [attributePath.endpoint isEqualToNumber:rootEndpoint]) {
2791-
2792-
// If the parts list changed and one or more endpoints were removed, remove all the clusters in _persistedClusters, _persistedClusterData and _clusterDataToPersist for all those endpoints.
2793-
// Also remove it from the data store.
2794-
NSMutableSet * toBeRemovedEndpoints = [NSMutableSet setWithArray:[self arrayOfNumbersFromAttributeValue:previousValue]];
2795-
NSSet * endpointsOnDevice = [NSSet setWithArray:[self arrayOfNumbersFromAttributeValue:attributeDataValue]];
2796-
[toBeRemovedEndpoints minusSet:endpointsOnDevice];
2797-
2798-
for (NSNumber * endpoint in toBeRemovedEndpoints) {
2799-
NSMutableSet<MTRClusterPath *> * clusterPathsToRemove = [[NSMutableSet alloc] init];
2800-
for (MTRClusterPath * path in _persistedClusters) {
2801-
if ([path.endpoint isEqualToNumber:endpoint]) {
2802-
[clusterPathsToRemove addObject:path];
2803-
}
2804-
}
2805-
[self _removeClusters:[clusterPathsToRemove copy]];
2806-
[self.deviceController.controllerDataStore removeEndpointFromEndpointIndex:endpoint forNodeID:self.nodeID];
2807-
}
2855+
[self _pruneOrphanedEndpoints:previousValue newPartsListValue:attributeDataValue];
28082856
} else if (attributePath.attribute.unsignedLongValue == MTRAttributeIDTypeClusterDescriptorAttributeServerListID) {
2809-
2810-
// If the server list changed and clusters were removed, remove the clusters from the _persistedClusters, _persistedClusterData and _clusterDataToPersist for all those clusters.
2811-
// Also remove it from the data store.
2812-
NSMutableSet<NSNumber *> * toBeRemovedClusters = [NSMutableSet setWithArray:[self arrayOfNumbersFromAttributeValue:[self _dataValueWithoutDataVersion:previousValue]]];
2813-
NSSet<NSNumber *> * clustersStillOnEndpoint = [NSSet setWithArray:[self arrayOfNumbersFromAttributeValue:attributeDataValue]];
2814-
[toBeRemovedClusters minusSet:clustersStillOnEndpoint];
2815-
2816-
NSMutableSet<MTRClusterPath *> * clusterPathsToRemove = [[NSMutableSet alloc] init];
2817-
for (NSNumber * cluster in toBeRemovedClusters) {
2818-
for (MTRClusterPath * path in _persistedClusters) {
2819-
if ([path.endpoint isEqualToNumber:attributePath.endpoint] && [path.cluster isEqualToNumber:cluster]) {
2820-
[clusterPathsToRemove addObject:path];
2821-
}
2822-
}
2823-
}
2824-
[self _removeClusters:[clusterPathsToRemove copy]];
2857+
[self _pruneOrphanedClusters:attributePath previousServerListValue:previousValue newServerListValue:attributeDataValue];
28252858
}
28262859
}
28272860

28282861
if (attributePath.attribute.unsignedLongValue == MTRAttributeIDTypeGlobalAttributeAttributeListID) {
2829-
2830-
// If the attribute list changed and attributes were removed, remove the attributes from the _clusterDataToPersist for that cluster and endpoint.
2831-
// Clear out the _peristedClusterData and data store cluster data. Update the data storage with updated cluster data.
2832-
NSMutableSet<NSNumber *> * toBeRemovedAttributes = [NSMutableSet setWithArray:[self arrayOfNumbersFromAttributeValue:[self _cachedAttributeValueForPath:attributePath]]];
2833-
NSSet<NSNumber *> * attributesStillInCluster = [NSSet setWithArray:[self arrayOfNumbersFromAttributeValue:attributeDataValue]];
2834-
2835-
[toBeRemovedAttributes minusSet:attributesStillInCluster];
2836-
MTRClusterPath * clusterPathToRemoveAttributesFrom;
2837-
for (MTRClusterPath * path in _persistedClusters) {
2838-
if ([path.endpoint isEqualToNumber:attributePath.endpoint] && [path.cluster isEqualToNumber:attributePath.cluster]) {
2839-
clusterPathToRemoveAttributesFrom = path;
2840-
break;
2841-
}
2842-
}
2843-
[self _removeAttributes:[toBeRemovedAttributes copy] fromCluster:clusterPathToRemoveAttributesFrom];
2844-
[self.deviceController.controllerDataStore removeAttributes:[toBeRemovedAttributes copy] fromCluster:clusterPathToRemoveAttributesFrom forNodeID:self.nodeID];
2862+
[self _pruneOrphanedAttributes:attributePath newAttributeListValue:attributeDataValue];
28452863
}
28462864
}
28472865

src/darwin/Framework/CHIP/MTRDeviceControllerDataStore.mm

+1-1
Original file line numberDiff line numberDiff line change
@@ -736,7 +736,7 @@ - (void)clearStoredClusterDataForNodeID:(NSNumber *)nodeID endpointID:(NSNumber
736736
MTR_LOG_ERROR("clearStoredClusterDataForNodeID: _storeClusterIndex failed for node 0x%016llX endpoint %u", nodeID.unsignedLongLongValue, endpointID.unsignedShortValue);
737737
}
738738
}
739-
MTR_LOG_INFO("clearStoredClusterDataForNodeID: Deleted endpoint %u cluster 0x%08lX for node 0x%016llX successfully", endpointID.unsignedShortValue, clusterID.unsignedLongValue, nodeID.unsignedLongLongValue);
739+
MTR_LOG("clearStoredClusterDataForNodeID: Deleted endpoint %u cluster 0x%08lX for node 0x%016llX successfully", endpointID.unsignedShortValue, clusterID.unsignedLongValue, nodeID.unsignedLongLongValue);
740740
});
741741
}
742742

src/darwin/Framework/CHIPTests/MTRPerControllerStorageTests.m

+2-1
Original file line numberDiff line numberDiff line change
@@ -2242,7 +2242,8 @@ - (void)test011_testDataStorageUpdatesWhenRemovingEndpoints
22422242
// 1. Get the data version and attribute value of the parts list for endpoint 0 to inject a fake report. The attribute report will delete endpoint 2.
22432243
// That should cause the endpoint and its corresponding clusters to be removed from data storage.
22442244
// 2. The data store is populated with cluster index and cluster data for endpoints 0, 1 and 2 initially.
2245-
// 3. After the fake attribute report is injected with deleted endpoint 2, make sure the data store is still populated with cluster index and cluster data for endpoints 0 and 1 but not 2.
2245+
// 3. After the fake attribute report is injected with deleted endpoint 2, make sure the data store is still populated with cluster index and cluster data
2246+
// for endpoints 0 and 1 but not 2.
22462247
__block NSDictionary * testDataForPartsList;
22472248
__block NSMutableArray * testClusterDataValueForPartsList;
22482249
delegate.onAttributeDataReceived = ^(NSArray<NSDictionary<NSString *, id> *> * attributeReport) {

0 commit comments

Comments
 (0)