Skip to content

Commit

Permalink
[Darwin] MTRDeviceController should clear attribute cache on pairing …
Browse files Browse the repository at this point in the history
…start
  • Loading branch information
jtung-apple committed Mar 12, 2024
1 parent 9d2f350 commit 51a600f
Show file tree
Hide file tree
Showing 3 changed files with 214 additions and 80 deletions.
47 changes: 37 additions & 10 deletions src/darwin/Framework/CHIP/MTRDeviceController.mm
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@
#import "MTRPersistentStorageDelegateBridge.h"
#import "MTRServerEndpoint_Internal.h"
#import "MTRSetupPayload.h"
#import "MTRUnfairLock.h"
#import "NSDataSpanConversion.h"
#import "NSStringSpanConversion.h"
#import <setup_payload/ManualSetupPayloadGenerator.h>
Expand Down Expand Up @@ -607,6 +608,13 @@ - (BOOL)setupCommissioningSessionWithPayload:(MTRSetupPayload *)payload
error:(NSError * __autoreleasing *)error
{
auto block = ^BOOL {
// First reset previous information about this new nodeID
os_unfair_lock_lock(&_deviceMapLock);
[self _removeDeviceWithNodeID:newNodeID device:nil];
os_unfair_lock_unlock(&_deviceMapLock);
[_controllerDataStore clearResumptionInfoForNodeID:newNodeID];
[_controllerDataStore clearStoredAttributesForNodeID:newNodeID];

// Try to get a QR code if possible (because it has a better
// discriminator, etc), then fall back to manual code if that fails.
NSString * pairingCode = [payload qrCodeString:nil];
Expand All @@ -632,6 +640,13 @@ - (BOOL)setupCommissioningSessionWithDiscoveredDevice:(MTRCommissionableBrowserR
error:(NSError * __autoreleasing *)error
{
auto block = ^BOOL {
// First reset previous information about this new nodeID
os_unfair_lock_lock(&_deviceMapLock);
[self _removeDeviceWithNodeID:newNodeID device:nil];
os_unfair_lock_unlock(&_deviceMapLock);
[_controllerDataStore clearResumptionInfoForNodeID:newNodeID];
[_controllerDataStore clearStoredAttributesForNodeID:newNodeID];

chip::NodeId nodeId = [newNodeID unsignedLongLongValue];
self->_operationalCredentialsDelegate->SetDeviceID(nodeId);

Expand Down Expand Up @@ -897,18 +912,30 @@ - (MTRDevice *)deviceForNodeID:(NSNumber *)nodeID
return deviceToReturn;
}

- (void)removeDevice:(MTRDevice *)device
// Remove a device from the device map, optionally with a specific object
- (void)_removeDeviceWithNodeID:(NSNumber *)nodeID device:(MTRDevice * _Nullable)deviceToRemove
{
os_unfair_lock_lock(&_deviceMapLock);
auto * nodeID = device.nodeID;
MTRDevice * deviceToRemove = _nodeIDToDeviceMap[nodeID];
if (deviceToRemove == device) {
[deviceToRemove invalidate];
_nodeIDToDeviceMap[nodeID] = nil;
} else {
MTR_LOG_ERROR("Error: Cannot remove device %p with nodeID %llu", device, nodeID.unsignedLongLongValue);
os_unfair_lock_assert_owner(&_deviceMapLock);

MTRDevice * device = _nodeIDToDeviceMap[nodeID];
if (!device) {
MTR_LOG_INFO("No device to remove with nodeID %llu", nodeID.unsignedLongLongValue);
return;
}
os_unfair_lock_unlock(&_deviceMapLock);

if (deviceToRemove && (device != deviceToRemove)) {
MTR_LOG_ERROR("Error: Cannot remove device %p with nodeID %llu", deviceToRemove, nodeID.unsignedLongLongValue);
return;
}

[deviceToRemove invalidate];
_nodeIDToDeviceMap[nodeID] = nil;
}

- (void)removeDevice:(MTRDevice *)device
{
std::lock_guard lock(_deviceMapLock);
[self _removeDeviceWithNodeID:device.nodeID device:device];
}

- (void)setDeviceControllerDelegate:(id<MTRDeviceControllerDelegate>)delegate queue:(dispatch_queue_t)queue
Expand Down
1 change: 1 addition & 0 deletions src/darwin/Framework/CHIP/MTRDeviceControllerDataStore.h
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ NS_ASSUME_NONNULL_BEGIN
- (nullable MTRCASESessionResumptionInfo *)findResumptionInfoByNodeID:(NSNumber *)nodeID;
- (nullable MTRCASESessionResumptionInfo *)findResumptionInfoByResumptionID:(NSData *)resumptionID;
- (void)storeResumptionInfo:(MTRCASESessionResumptionInfo *)resumptionInfo;
- (void)clearResumptionInfoForNodeID:(NSNumber *)nodeID;
- (void)clearAllResumptionInfo;

/**
Expand Down
Loading

0 comments on commit 51a600f

Please sign in to comment.