Skip to content

Commit ef9203d

Browse files
committed
[Darwin] MTRDeviceController should clear attribute cache on pairing start
1 parent 9d2f350 commit ef9203d

File tree

2 files changed

+171
-67
lines changed

2 files changed

+171
-67
lines changed

src/darwin/Framework/CHIP/MTRDeviceController.mm

+35-10
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@
4444
#import "MTRPersistentStorageDelegateBridge.h"
4545
#import "MTRServerEndpoint_Internal.h"
4646
#import "MTRSetupPayload.h"
47+
#import "MTRUnfairLock.h"
4748
#import "NSDataSpanConversion.h"
4849
#import "NSStringSpanConversion.h"
4950
#import <setup_payload/ManualSetupPayloadGenerator.h>
@@ -623,6 +624,12 @@ - (BOOL)setupCommissioningSessionWithPayload:(MTRSetupPayload *)payload
623624
return ![MTRDeviceController checkForError:errorCode logMsg:kErrorPairDevice error:error];
624625
};
625626

627+
// Reset previous information about this new nodeID
628+
os_unfair_lock_lock(&_deviceMapLock);
629+
[self _removeDeviceWithNodeID:newNodeID device:nil];
630+
os_unfair_lock_unlock(&_deviceMapLock);
631+
[_controllerDataStore clearStoredAttributesForNodeID:newNodeID];
632+
626633
return [self syncRunOnWorkQueueWithBoolReturnValue:block error:error];
627634
}
628635

@@ -670,6 +677,12 @@ - (BOOL)setupCommissioningSessionWithDiscoveredDevice:(MTRCommissionableBrowserR
670677
return ![MTRDeviceController checkForError:errorCode logMsg:kErrorPairDevice error:error];
671678
};
672679

680+
// Reset previous information about this new nodeID
681+
os_unfair_lock_lock(&_deviceMapLock);
682+
[self _removeDeviceWithNodeID:newNodeID device:nil];
683+
os_unfair_lock_unlock(&_deviceMapLock);
684+
[_controllerDataStore clearStoredAttributesForNodeID:newNodeID];
685+
673686
return [self syncRunOnWorkQueueWithBoolReturnValue:block error:error];
674687
}
675688

@@ -897,18 +910,30 @@ - (MTRDevice *)deviceForNodeID:(NSNumber *)nodeID
897910
return deviceToReturn;
898911
}
899912

900-
- (void)removeDevice:(MTRDevice *)device
913+
// Remove a device from the device map, optionally with a specific object
914+
- (void)_removeDeviceWithNodeID:(NSNumber *)nodeID device:(MTRDevice * _Nullable)deviceToRemove
901915
{
902-
os_unfair_lock_lock(&_deviceMapLock);
903-
auto * nodeID = device.nodeID;
904-
MTRDevice * deviceToRemove = _nodeIDToDeviceMap[nodeID];
905-
if (deviceToRemove == device) {
906-
[deviceToRemove invalidate];
907-
_nodeIDToDeviceMap[nodeID] = nil;
908-
} else {
909-
MTR_LOG_ERROR("Error: Cannot remove device %p with nodeID %llu", device, nodeID.unsignedLongLongValue);
916+
os_unfair_lock_assert_owner(&_deviceMapLock);
917+
918+
MTRDevice * device = _nodeIDToDeviceMap[nodeID];
919+
if (!device) {
920+
MTR_LOG_INFO("No device to remove with nodeID %llu", nodeID.unsignedLongLongValue);
921+
return;
910922
}
911-
os_unfair_lock_unlock(&_deviceMapLock);
923+
924+
if (deviceToRemove && (device != deviceToRemove)) {
925+
MTR_LOG_ERROR("Error: Cannot remove device %p with nodeID %llu", deviceToRemove, nodeID.unsignedLongLongValue);
926+
return;
927+
}
928+
929+
[deviceToRemove invalidate];
930+
_nodeIDToDeviceMap[nodeID] = nil;
931+
}
932+
933+
- (void)removeDevice:(MTRDevice *)device
934+
{
935+
std::lock_guard lock(_deviceMapLock);
936+
[self _removeDeviceWithNodeID:device.nodeID device:device];
912937
}
913938

914939
- (void)setDeviceControllerDelegate:(id<MTRDeviceControllerDelegate>)delegate queue:(dispatch_queue_t)queue

0 commit comments

Comments
 (0)