|
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>
|
@@ -623,6 +624,12 @@ - (BOOL)setupCommissioningSessionWithPayload:(MTRSetupPayload *)payload
|
623 | 624 | return ![MTRDeviceController checkForError:errorCode logMsg:kErrorPairDevice error:error];
|
624 | 625 | };
|
625 | 626 |
|
| 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 | + |
626 | 633 | return [self syncRunOnWorkQueueWithBoolReturnValue:block error:error];
|
627 | 634 | }
|
628 | 635 |
|
@@ -670,6 +677,12 @@ - (BOOL)setupCommissioningSessionWithDiscoveredDevice:(MTRCommissionableBrowserR
|
670 | 677 | return ![MTRDeviceController checkForError:errorCode logMsg:kErrorPairDevice error:error];
|
671 | 678 | };
|
672 | 679 |
|
| 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 | + |
673 | 686 | return [self syncRunOnWorkQueueWithBoolReturnValue:block error:error];
|
674 | 687 | }
|
675 | 688 |
|
@@ -897,18 +910,30 @@ - (MTRDevice *)deviceForNodeID:(NSNumber *)nodeID
|
897 | 910 | return deviceToReturn;
|
898 | 911 | }
|
899 | 912 |
|
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 |
901 | 915 | {
|
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; |
910 | 922 | }
|
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]; |
912 | 937 | }
|
913 | 938 |
|
914 | 939 | - (void)setDeviceControllerDelegate:(id<MTRDeviceControllerDelegate>)delegate queue:(dispatch_queue_t)queue
|
|
0 commit comments