Skip to content

Commit c1ba1fd

Browse files
committed
More clean up
1 parent 24a4581 commit c1ba1fd

File tree

3 files changed

+34
-45
lines changed

3 files changed

+34
-45
lines changed

src/darwin/Framework/CHIP/MTRDevice.mm

+7-15
Original file line numberDiff line numberDiff line change
@@ -3033,6 +3033,7 @@ - (BOOL)_attributeAffectsDeviceConfiguration:(MTRAttributePath *)attributePath
30333033
}
30343034

30353035
- (void)_removeClusters:(NSSet<MTRClusterPath *> *)clusterPathsToRemove
3036+
doRemoveFromDataStore:(BOOL)doRemoveFromDataStore
30363037
{
30373038
os_unfair_lock_assert_owner(&self->_lock);
30383039

@@ -3041,19 +3042,10 @@ - (void)_removeClusters:(NSSet<MTRClusterPath *> *)clusterPathsToRemove
30413042
for (MTRClusterPath * path in clusterPathsToRemove) {
30423043
[_persistedClusterData removeObjectForKey:path];
30433044
[_clusterDataToPersist removeObjectForKey:path];
3044-
[self.deviceController.controllerDataStore clearStoredClusterDataForNodeID:self.nodeID endpointID:path.endpoint clusterID:path.cluster];
3045-
}
3046-
}
3047-
3048-
- (void)_removeClustersFromCache:(NSSet<MTRClusterPath *> *)clusterPathsToRemove
3049-
{
3050-
os_unfair_lock_assert_owner(&self->_lock);
3051-
3052-
[_persistedClusters minusSet:clusterPathsToRemove];
3053-
3054-
for (MTRClusterPath * path in clusterPathsToRemove) {
3055-
[_persistedClusterData removeObjectForKey:path];
3056-
[_clusterDataToPersist removeObjectForKey:path];
3045+
if (doRemoveFromDataStore)
3046+
{
3047+
[self.deviceController.controllerDataStore clearStoredClusterDataForNodeID:self.nodeID endpointID:path.endpoint clusterID:path.cluster];
3048+
}
30573049
}
30583050
}
30593051

@@ -3086,7 +3078,7 @@ - (void)_pruneEndpointsIn:(MTRDeviceDataValueDictionary)previousPartsListValue
30863078
[clusterPathsToRemove addObject:path];
30873079
}
30883080
}
3089-
[self _removeClustersFromCache:clusterPathsToRemove];
3081+
[self _removeClusters:clusterPathsToRemove doRemoveFromDataStore:NO];
30903082
[self.deviceController.controllerDataStore clearStoredClusterDataForNodeID:self.nodeID endpointID:endpoint];
30913083
}
30923084
}
@@ -3109,7 +3101,7 @@ - (void)_pruneClustersIn:(MTRDeviceDataValueDictionary)previousServerListValue
31093101
}
31103102
}
31113103
}
3112-
[self _removeClusters:clusterPathsToRemove];
3104+
[self _removeClusters:clusterPathsToRemove doRemoveFromDataStore:YES];
31133105
}
31143106

31153107
- (void)_pruneAttributesIn:(MTRDeviceDataValueDictionary)previousAttributeListValue

src/darwin/Framework/CHIP/MTRDeviceControllerDataStore.mm

+23-23
Original file line numberDiff line numberDiff line change
@@ -716,54 +716,54 @@ - (void)clearStoredClusterDataForNodeID:(NSNumber *)nodeID
716716
});
717717
}
718718

719-
- (void)clearStoredClusterDataForNodeID:(NSNumber *)nodeID endpointID:(NSNumber *)endpointID
719+
- (void)clearStoredClusterDataForNodeID:(NSNumber *)nodeID endpointID:(NSNumber *)endpointID clusterID:(NSNumber *)clusterID
720720
{
721721
dispatch_async(_storageDelegateQueue, ^{
722-
BOOL success = [self _removeEndpointFromEndpointIndex:endpointID forNodeID:nodeID];
723-
if (!success) {
724-
MTR_LOG_ERROR("removeEndpointFromEndpointIndex for endpointID %u failed for node 0x%016llX", endpointID.unsignedShortValue, nodeID.unsignedLongLongValue);
725-
}
726-
727722
NSArray<NSNumber *> * clusterIndex = [self _fetchClusterIndexForNodeID:nodeID endpointID:endpointID];
723+
NSMutableArray<NSNumber *> * clusterIndexCopy = [clusterIndex mutableCopy];
724+
[clusterIndexCopy removeObject:clusterID];
728725

729-
for (NSNumber * cluster in clusterIndex) {
730-
success = [self _deleteClusterDataForNodeID:nodeID endpointID:endpointID clusterID:cluster];
726+
BOOL success;
727+
if (clusterIndexCopy.count != clusterIndex.count) {
728+
success = [self _storeClusterIndex:clusterIndexCopy forNodeID:nodeID endpointID:endpointID];
731729
if (!success) {
732-
MTR_LOG_ERROR("Delete failed for clusterData for node 0x%016llX endpoint %u cluster 0x%08lX", nodeID.unsignedLongLongValue, endpointID.unsignedShortValue, cluster.unsignedLongValue);
730+
MTR_LOG_ERROR("clearStoredClusterDataForNodeID: _storeClusterIndex failed for node 0x%016llX endpoint %u", nodeID.unsignedLongLongValue, endpointID.unsignedShortValue);
733731
}
734732
}
735733

736-
success = [self _deleteClusterIndexForNodeID:nodeID endpointID:endpointID];
734+
success = [self _deleteClusterDataForNodeID:nodeID endpointID:endpointID clusterID:clusterID];
737735
if (!success) {
738-
MTR_LOG_ERROR("Delete failed for clusterIndex for node 0x%016llX endpoint %u", nodeID.unsignedLongLongValue, endpointID.unsignedShortValue);
736+
MTR_LOG_ERROR("clearStoredClusterDataForNodeID: _deleteClusterDataForNodeID failed for node 0x%016llX endpoint %u cluster 0x%08lX", nodeID.unsignedLongLongValue, endpointID.unsignedShortValue, clusterID.unsignedLongValue);
737+
return;
739738
}
740739

741-
MTR_LOG("clearStoredClusterDataForNodeID: Deleted endpoint %u for node 0x%016llX successfully", endpointID.unsignedShortValue, nodeID.unsignedLongLongValue);
740+
MTR_LOG("clearStoredClusterDataForNodeID: Deleted endpoint %u cluster 0x%08lX for node 0x%016llX successfully", endpointID.unsignedShortValue, clusterID.unsignedLongValue, nodeID.unsignedLongLongValue);
742741
});
743742
}
744743

745-
- (void)clearStoredClusterDataForNodeID:(NSNumber *)nodeID endpointID:(NSNumber *)endpointID clusterID:(NSNumber *)clusterID
744+
- (void)clearStoredClusterDataForNodeID:(NSNumber *)nodeID endpointID:(NSNumber *)endpointID
746745
{
747746
dispatch_async(_storageDelegateQueue, ^{
747+
BOOL success = [self _removeEndpointFromEndpointIndex:endpointID forNodeID:nodeID];
748+
if (!success) {
749+
MTR_LOG_ERROR("removeEndpointFromEndpointIndex for endpointID %u failed for node 0x%016llX", endpointID.unsignedShortValue, nodeID.unsignedLongLongValue);
750+
}
751+
748752
NSArray<NSNumber *> * clusterIndex = [self _fetchClusterIndexForNodeID:nodeID endpointID:endpointID];
749-
NSMutableArray<NSNumber *> * clusterIndexCopy = [clusterIndex mutableCopy];
750-
[clusterIndexCopy removeObject:clusterID];
751753

752-
BOOL success;
753-
if (clusterIndexCopy.count != clusterIndex.count) {
754-
success = [self _storeClusterIndex:clusterIndexCopy forNodeID:nodeID endpointID:endpointID];
754+
for (NSNumber * cluster in clusterIndex) {
755+
success = [self _deleteClusterDataForNodeID:nodeID endpointID:endpointID clusterID:cluster];
755756
if (!success) {
756-
MTR_LOG_ERROR("clearStoredClusterDataForNodeID: _storeClusterIndex failed for node 0x%016llX endpoint %u", nodeID.unsignedLongLongValue, endpointID.unsignedShortValue);
757+
MTR_LOG_ERROR("Delete failed for clusterData for node 0x%016llX endpoint %u cluster 0x%08lX", nodeID.unsignedLongLongValue, endpointID.unsignedShortValue, cluster.unsignedLongValue);
757758
}
758759
}
759760

760-
success = [self _deleteClusterDataForNodeID:nodeID endpointID:endpointID clusterID:clusterID];
761+
success = [self _deleteClusterIndexForNodeID:nodeID endpointID:endpointID];
761762
if (!success) {
762-
MTR_LOG_ERROR("clearStoredClusterDataForNodeID: _deleteClusterDataForNodeID failed for node 0x%016llX endpoint %u cluster 0x%08lX", nodeID.unsignedLongLongValue, endpointID.unsignedShortValue, clusterID.unsignedLongValue);
763-
return;
763+
MTR_LOG_ERROR("Delete failed for clusterIndex for node 0x%016llX endpoint %u", nodeID.unsignedLongLongValue, endpointID.unsignedShortValue);
764764
}
765765

766-
MTR_LOG("clearStoredClusterDataForNodeID: Deleted endpoint %u cluster 0x%08lX for node 0x%016llX successfully", endpointID.unsignedShortValue, clusterID.unsignedLongValue, nodeID.unsignedLongLongValue);
766+
MTR_LOG("clearStoredClusterDataForNodeID: Deleted endpoint %u for node 0x%016llX successfully", endpointID.unsignedShortValue, nodeID.unsignedLongLongValue);
767767
});
768768
}
769769

src/darwin/Framework/CHIPTests/MTRPerControllerStorageTests.m

+4-7
Original file line numberDiff line numberDiff line change
@@ -2157,9 +2157,6 @@ - (void)testSetMRPParametersWithRunningController
21572157
MTRSetMessageReliabilityParameters(nil, nil, nil, nil);
21582158
}
21592159

2160-
static NSString * const kLocalTestUserDefaultDomain = @"org.csa-iot.matter.darwintest";
2161-
static NSString * const kLocalTestUserDefaultSubscriptionPoolSizeOverrideKey = @"subscriptionPoolSizeOverride";
2162-
21632160
// TODO: This might also want to go in a separate test file, with some shared setup for commissioning devices per test
21642161
- (void)doTestSubscriptionPoolWithSize:(NSInteger)subscriptionPoolSize deviceOnboardingPayloads:(NSDictionary<NSNumber *, NSString *> *)deviceOnboardingPayloads
21652162
{
@@ -2339,7 +2336,7 @@ - (MTRDevice *)getMTRDevice:(NSNumber *)deviceID
23392336
return device;
23402337
}
23412338

2342-
- (NSMutableArray<NSNumber *> *)getEndpointArrayFromPartsList:(NSDictionary *)partsList forDevice:(MTRDevice *)device
2339+
- (NSMutableArray<NSNumber *> *)getEndpointArrayFromPartsList:(MTRDeviceDataValueDictionary)partsList forDevice:(MTRDevice *)device
23432340
{
23442341
// Initialize the endpoint array with endpoint 0.
23452342
NSMutableArray<NSNumber *> * endpoints = [NSMutableArray arrayWithObject:@0];
@@ -2368,7 +2365,7 @@ - (void)testDataStorageUpdatesWhenRemovingEndpoints
23682365
// 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
23692366
// for endpoints 0 and 1 but not 2.
23702367
__block MTRDeviceDataValueDictionary testDataForPartsList;
2371-
__block NSMutableArray<NSNumber *> * testClusterDataValueForPartsList;
2368+
__block id testClusterDataValueForPartsList;
23722369
delegate.onAttributeDataReceived = ^(NSArray<NSDictionary<NSString *, id> *> * attributeReport) {
23732370
XCTAssertGreaterThan(attributeReport.count, 0);
23742371

@@ -2523,7 +2520,7 @@ - (void)testDataStorageUpdatesWhenRemovingClusters
25232520
// 2. The data store is populated with MTRClusterIDTypeIdentifyID in the cluster index and cluster data for endpoint 1 initially.
25242521
// 3. After the fake attribute report is injected with deleted cluster ID - MTRClusterIDTypeIdentifyID, make sure the data store is still populated with cluster index and
25252522
// cluster data for all other clusters at endpoint 1 but not the deleted cluster.
2526-
__block NSMutableArray<NSNumber *> * testClusterDataValue;
2523+
__block id testClusterDataValue;
25272524
delegate.onAttributeDataReceived = ^(NSArray<NSDictionary<NSString *, id> *> * attributeReport) {
25282525
XCTAssertGreaterThan(attributeReport.count, 0);
25292526

@@ -2642,7 +2639,7 @@ - (void)testDataStorageUpdatesWhenRemovingAttributes
26422639
__block NSNumber * dataVersionForIdentify;
26432640
__block NSNumber * testEndpoint = @(1);
26442641
__block NSNumber * toBeDeletedAttribute = @(1);
2645-
__block NSMutableArray<NSNumber *> * testClusterDataValue;
2642+
__block id testClusterDataValue;
26462643

26472644
// This test will do the following -
26482645
// 1. Get the data version and attribute value of the attribute list for endpoint 1 to inject a fake report with attribute 1 removed from MTRClusterIDTypeIdentifyID.

0 commit comments

Comments
 (0)