44
44
#import " MTRPersistentStorageDelegateBridge.h"
45
45
#import " MTRServerEndpoint_Internal.h"
46
46
#import " MTRSetupPayload.h"
47
+ #import " MTRUnfairLock.h"
47
48
#import " NSDataSpanConversion.h"
48
49
#import " NSStringSpanConversion.h"
49
50
#import < setup_payload/ManualSetupPayloadGenerator.h>
@@ -607,6 +608,13 @@ - (BOOL)setupCommissioningSessionWithPayload:(MTRSetupPayload *)payload
607
608
error : (NSError * __autoreleasing *)error
608
609
{
609
610
auto block = ^BOOL {
611
+ // First reset previous information about this new nodeID
612
+ os_unfair_lock_lock (&_deviceMapLock);
613
+ [self _removeDeviceWithNodeID: newNodeID device: nil ];
614
+ os_unfair_lock_unlock (&_deviceMapLock);
615
+ [_controllerDataStore clearResumptionInfoForNodeID: newNodeID];
616
+ [_controllerDataStore clearStoredAttributesForNodeID: newNodeID];
617
+
610
618
// Try to get a QR code if possible (because it has a better
611
619
// discriminator, etc), then fall back to manual code if that fails.
612
620
NSString * pairingCode = [payload qrCodeString: nil ];
@@ -632,6 +640,13 @@ - (BOOL)setupCommissioningSessionWithDiscoveredDevice:(MTRCommissionableBrowserR
632
640
error : (NSError * __autoreleasing *)error
633
641
{
634
642
auto block = ^BOOL {
643
+ // First reset previous information about this new nodeID
644
+ os_unfair_lock_lock (&_deviceMapLock);
645
+ [self _removeDeviceWithNodeID: newNodeID device: nil ];
646
+ os_unfair_lock_unlock (&_deviceMapLock);
647
+ [_controllerDataStore clearResumptionInfoForNodeID: newNodeID];
648
+ [_controllerDataStore clearStoredAttributesForNodeID: newNodeID];
649
+
635
650
chip::NodeId nodeId = [newNodeID unsignedLongLongValue ];
636
651
self->_operationalCredentialsDelegate ->SetDeviceID (nodeId);
637
652
@@ -897,18 +912,30 @@ - (MTRDevice *)deviceForNodeID:(NSNumber *)nodeID
897
912
return deviceToReturn;
898
913
}
899
914
900
- - (void )removeDevice : (MTRDevice *)device
915
+ // Remove a device from the device map, optionally with a specific object
916
+ - (void )_removeDeviceWithNodeID : (NSNumber *)nodeID device : (MTRDevice * _Nullable)deviceToRemove
901
917
{
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 );
918
+ os_unfair_lock_assert_owner (&_deviceMapLock);
919
+
920
+ MTRDevice * device = _nodeIDToDeviceMap[nodeID];
921
+ if (!device) {
922
+ MTR_LOG_INFO (" No device to remove with nodeID %llu" , nodeID.unsignedLongLongValue );
923
+ return ;
910
924
}
911
- os_unfair_lock_unlock (&_deviceMapLock);
925
+
926
+ if (deviceToRemove && (device != deviceToRemove)) {
927
+ MTR_LOG_ERROR (" Error: Cannot remove device %p with nodeID %llu" , deviceToRemove, nodeID.unsignedLongLongValue );
928
+ return ;
929
+ }
930
+
931
+ [deviceToRemove invalidate ];
932
+ _nodeIDToDeviceMap[nodeID] = nil ;
933
+ }
934
+
935
+ - (void )removeDevice : (MTRDevice *)device
936
+ {
937
+ std::lock_guard lock (_deviceMapLock);
938
+ [self _removeDeviceWithNodeID: device.nodeID device: device];
912
939
}
913
940
914
941
- (void )setDeviceControllerDelegate : (id <MTRDeviceControllerDelegate>)delegate queue : (dispatch_queue_t )queue
0 commit comments