Skip to content

Commit 85520e6

Browse files
nivi-applewoody-apple
authored andcommitted
Added helpers for removing endpoints, clusters and attributes
1 parent adb54cf commit 85520e6

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
@@ -3063,6 +3063,70 @@ - (void)_removeAttributes:(NSSet *)toBeRemovedAttributes fromCluster:(MTRCluster
30633063
[self.deviceController.controllerDataStore clearStoredClusterDataForNodeID:self.nodeID endpointID:clusterPathToRemoveAttributesFrom.endpoint clusterID:clusterPathToRemoveAttributesFrom.cluster];
30643064
}
30653065

3066+
- (void) _pruneOrphanedEndpoints:(NSDictionary *)previousPartsListValue
3067+
newPartsListValue:(NSDictionary *)newPartsListValue
3068+
{
3069+
// 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.
3070+
// Also remove it from the data store.
3071+
NSMutableSet * toBeRemovedEndpoints = [NSMutableSet setWithArray:[self arrayOfNumbersFromAttributeValue:[self _dataValueWithoutDataVersion:previousPartsListValue]]];
3072+
NSSet * endpointsOnDevice = [NSSet setWithArray:[self arrayOfNumbersFromAttributeValue:newPartsListValue]];
3073+
[toBeRemovedEndpoints minusSet:endpointsOnDevice];
3074+
3075+
for (NSNumber * endpoint in toBeRemovedEndpoints) {
3076+
NSMutableSet<MTRClusterPath *> * clusterPathsToRemove = [[NSMutableSet alloc] init];
3077+
for (MTRClusterPath * path in _persistedClusters) {
3078+
if ([path.endpoint isEqualToNumber:endpoint]) {
3079+
[clusterPathsToRemove addObject:path];
3080+
}
3081+
}
3082+
[self _removeClusters:[clusterPathsToRemove copy]];
3083+
[self.deviceController.controllerDataStore removeEndpointFromEndpointIndex:endpoint forNodeID:self.nodeID];
3084+
}
3085+
}
3086+
3087+
- (void) _pruneOrphanedClusters:(MTRAttributePath *)attributePath
3088+
previousServerListValue:(NSDictionary *)previousServerListValue
3089+
newServerListValue:(NSDictionary *)newServerListValue
3090+
{
3091+
// If the server list changed and clusters were removed, remove the clusters from the _persistedClusters, _persistedClusterData and _clusterDataToPersist for all those clusters.
3092+
// Also remove it from the data store.
3093+
NSMutableSet<NSNumber *> * toBeRemovedClusters = [NSMutableSet setWithArray:[self arrayOfNumbersFromAttributeValue:[self _dataValueWithoutDataVersion:previousServerListValue]]];
3094+
NSSet<NSNumber *> * clustersStillOnEndpoint = [NSSet setWithArray:[self arrayOfNumbersFromAttributeValue:newServerListValue]];
3095+
[toBeRemovedClusters minusSet:clustersStillOnEndpoint];
3096+
3097+
NSMutableSet<MTRClusterPath *> * clusterPathsToRemove = [[NSMutableSet alloc] init];
3098+
for (NSNumber * cluster in toBeRemovedClusters) {
3099+
for (MTRClusterPath * path in _persistedClusters) {
3100+
if ([path.endpoint isEqualToNumber:attributePath.endpoint] && [path.cluster isEqualToNumber:cluster]) {
3101+
[clusterPathsToRemove addObject:path];
3102+
}
3103+
}
3104+
}
3105+
[self _removeClusters:[clusterPathsToRemove copy]];
3106+
}
3107+
3108+
- (void) _pruneOrphanedAttributes:(MTRAttributePath *)attributePath
3109+
newAttributeListValue:(NSDictionary *)newAttributeListValue
3110+
{
3111+
// If the attribute list changed and attributes were removed, remove the attributes from the _clusterDataToPersist for that cluster and endpoint.
3112+
// Clear out the _peristedClusterData and data store cluster data. Update the data storage with updated cluster data.
3113+
NSMutableSet<NSNumber *> * toBeRemovedAttributes = [NSMutableSet setWithArray:[self arrayOfNumbersFromAttributeValue:[self _cachedAttributeValueForPath:attributePath]]];
3114+
NSSet<NSNumber *> * attributesStillInCluster = [NSSet setWithArray:[self arrayOfNumbersFromAttributeValue:newAttributeListValue]];
3115+
3116+
[toBeRemovedAttributes minusSet:attributesStillInCluster];
3117+
MTRClusterPath * clusterPathToRemoveAttributesFrom;
3118+
for (MTRClusterPath * path in _persistedClusters) {
3119+
if ([path.endpoint isEqualToNumber:attributePath.endpoint] && [path.cluster isEqualToNumber:attributePath.cluster]) {
3120+
clusterPathToRemoveAttributesFrom = path;
3121+
break;
3122+
}
3123+
}
3124+
[self _removeAttributes:[toBeRemovedAttributes copy] fromCluster:clusterPathToRemoveAttributesFrom];
3125+
[self.deviceController.controllerDataStore removeAttributes:[toBeRemovedAttributes copy] fromCluster:clusterPathToRemoveAttributesFrom forNodeID:self.nodeID];
3126+
}
3127+
3128+
3129+
30663130
- (void)_pruneOrphanedEndpointsAndClusters:(MTRAttributePath *)attributePath
30673131
previousValue:(NSDictionary *)previousValue
30683132
attributeDataValue:(NSDictionary *)attributeDataValue
@@ -3078,60 +3142,14 @@ - (void)_pruneOrphanedEndpointsAndClusters:(MTRAttributePath *)attributePath
30783142
// If yes, we might need to prune any deleted endpoints, clusters or attributes from the storage and persisted cluster data.
30793143
if (attributePath.cluster.unsignedLongValue == MTRClusterIDTypeDescriptorID) {
30803144
if (attributePath.attribute.unsignedLongValue == MTRAttributeIDTypeClusterDescriptorAttributePartsListID && [attributePath.endpoint isEqualToNumber:rootEndpoint]) {
3081-
3082-
// 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.
3083-
// Also remove it from the data store.
3084-
NSMutableSet * toBeRemovedEndpoints = [NSMutableSet setWithArray:[self arrayOfNumbersFromAttributeValue:previousValue]];
3085-
NSSet * endpointsOnDevice = [NSSet setWithArray:[self arrayOfNumbersFromAttributeValue:attributeDataValue]];
3086-
[toBeRemovedEndpoints minusSet:endpointsOnDevice];
3087-
3088-
for (NSNumber * endpoint in toBeRemovedEndpoints) {
3089-
NSMutableSet<MTRClusterPath *> * clusterPathsToRemove = [[NSMutableSet alloc] init];
3090-
for (MTRClusterPath * path in _persistedClusters) {
3091-
if ([path.endpoint isEqualToNumber:endpoint]) {
3092-
[clusterPathsToRemove addObject:path];
3093-
}
3094-
}
3095-
[self _removeClusters:[clusterPathsToRemove copy]];
3096-
[self.deviceController.controllerDataStore removeEndpointFromEndpointIndex:endpoint forNodeID:self.nodeID];
3097-
}
3145+
[self _pruneOrphanedEndpoints:previousValue newPartsListValue:attributeDataValue];
30983146
} else if (attributePath.attribute.unsignedLongValue == MTRAttributeIDTypeClusterDescriptorAttributeServerListID) {
3099-
3100-
// If the server list changed and clusters were removed, remove the clusters from the _persistedClusters, _persistedClusterData and _clusterDataToPersist for all those clusters.
3101-
// Also remove it from the data store.
3102-
NSMutableSet<NSNumber *> * toBeRemovedClusters = [NSMutableSet setWithArray:[self arrayOfNumbersFromAttributeValue:[self _dataValueWithoutDataVersion:previousValue]]];
3103-
NSSet<NSNumber *> * clustersStillOnEndpoint = [NSSet setWithArray:[self arrayOfNumbersFromAttributeValue:attributeDataValue]];
3104-
[toBeRemovedClusters minusSet:clustersStillOnEndpoint];
3105-
3106-
NSMutableSet<MTRClusterPath *> * clusterPathsToRemove = [[NSMutableSet alloc] init];
3107-
for (NSNumber * cluster in toBeRemovedClusters) {
3108-
for (MTRClusterPath * path in _persistedClusters) {
3109-
if ([path.endpoint isEqualToNumber:attributePath.endpoint] && [path.cluster isEqualToNumber:cluster]) {
3110-
[clusterPathsToRemove addObject:path];
3111-
}
3112-
}
3113-
}
3114-
[self _removeClusters:[clusterPathsToRemove copy]];
3147+
[self _pruneOrphanedClusters:attributePath previousServerListValue:previousValue newServerListValue:attributeDataValue];
31153148
}
31163149
}
31173150

31183151
if (attributePath.attribute.unsignedLongValue == MTRAttributeIDTypeGlobalAttributeAttributeListID) {
3119-
3120-
// If the attribute list changed and attributes were removed, remove the attributes from the _clusterDataToPersist for that cluster and endpoint.
3121-
// Clear out the _peristedClusterData and data store cluster data. Update the data storage with updated cluster data.
3122-
NSMutableSet<NSNumber *> * toBeRemovedAttributes = [NSMutableSet setWithArray:[self arrayOfNumbersFromAttributeValue:[self _cachedAttributeValueForPath:attributePath]]];
3123-
NSSet<NSNumber *> * attributesStillInCluster = [NSSet setWithArray:[self arrayOfNumbersFromAttributeValue:attributeDataValue]];
3124-
3125-
[toBeRemovedAttributes minusSet:attributesStillInCluster];
3126-
MTRClusterPath * clusterPathToRemoveAttributesFrom;
3127-
for (MTRClusterPath * path in _persistedClusters) {
3128-
if ([path.endpoint isEqualToNumber:attributePath.endpoint] && [path.cluster isEqualToNumber:attributePath.cluster]) {
3129-
clusterPathToRemoveAttributesFrom = path;
3130-
break;
3131-
}
3132-
}
3133-
[self _removeAttributes:[toBeRemovedAttributes copy] fromCluster:clusterPathToRemoveAttributesFrom];
3134-
[self.deviceController.controllerDataStore removeAttributes:[toBeRemovedAttributes copy] fromCluster:clusterPathToRemoveAttributesFrom forNodeID:self.nodeID];
3152+
[self _pruneOrphanedAttributes:attributePath newAttributeListValue:attributeDataValue];
31353153
}
31363154
}
31373155

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
@@ -2362,7 +2362,8 @@ - (void)test011_testDataStorageUpdatesWhenRemovingEndpoints
23622362
// 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.
23632363
// That should cause the endpoint and its corresponding clusters to be removed from data storage.
23642364
// 2. The data store is populated with cluster index and cluster data for endpoints 0, 1 and 2 initially.
2365-
// 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.
2365+
// 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
2366+
// for endpoints 0 and 1 but not 2.
23662367
__block NSDictionary * testDataForPartsList;
23672368
__block NSMutableArray * testClusterDataValueForPartsList;
23682369
delegate.onAttributeDataReceived = ^(NSArray<NSDictionary<NSString *, id> *> * attributeReport) {

0 commit comments

Comments
 (0)