Skip to content

Commit 023d1e5

Browse files
Don't interrupt a CASE attempt triggered by _deviceMayBeReachable. (#37871)
There's a really good chance this attempt will in fact succeed, so don't touch it. As a followup, we will need to figure out a better state machine here.
1 parent ac1c96a commit 023d1e5

File tree

1 file changed

+11
-2
lines changed

1 file changed

+11
-2
lines changed

src/darwin/Framework/CHIP/MTRDevice_Concrete.mm

+11-2
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,8 @@
5858

5959
static NSString * const sLastInitialSubscribeLatencyKey = @"lastInitialSubscribeLatency";
6060

61+
static NSString * const sDeviceMayBeReachableReason = @"SPI client indicated the device may now be reachable";
62+
6163
// Not static, because these are public API.
6264
NSString * const MTRPreviousDataKey = @"previousData";
6365
NSString * const MTRDataVersionKey = @"dataVersion";
@@ -306,6 +308,7 @@ @interface MTRDevice_Concrete ()
306308
// Actively receiving priming report
307309

308310
@property (nonatomic) MTRInternalDeviceState internalDeviceState;
311+
@property (nonatomic) BOOL doingCASEAttemptForDeviceMayBeReachable;
309312

310313
#define MTRDEVICE_SUBSCRIPTION_ATTEMPT_MIN_WAIT_SECONDS (1)
311314
#define MTRDEVICE_SUBSCRIPTION_ATTEMPT_MAX_WAIT_SECONDS (3600)
@@ -470,6 +473,7 @@ - (instancetype)initWithNodeID:(NSNumber *)nodeID controller:(MTRDeviceControlle
470473
_state = MTRDeviceStateUnknown;
471474
_internalDeviceState = MTRInternalDeviceStateUnsubscribed;
472475
_internalDeviceStateForDescription = MTRInternalDeviceStateUnsubscribed;
476+
_doingCASEAttemptForDeviceMayBeReachable = NO;
473477
if (controller.controllerDataStore) {
474478
_persistedClusterData = [[NSCache alloc] init];
475479
} else {
@@ -1049,7 +1053,7 @@ - (void)_triggerResubscribeWithReason:(NSString *)reason nodeLikelyReachable:(BO
10491053
subscriptionCallback->ResetResubscriptionBackoff();
10501054
}
10511055
readClientToResubscribe->TriggerResubscribeIfScheduled(reason.UTF8String);
1052-
} else if (((_internalDeviceState == MTRInternalDeviceStateSubscribing) || shouldReattemptSubscription) && nodeLikelyReachable) {
1056+
} else if (((_internalDeviceState == MTRInternalDeviceStateSubscribing && !self.doingCASEAttemptForDeviceMayBeReachable) || shouldReattemptSubscription) && nodeLikelyReachable) {
10531057
// If we have reason to suspect that the node is now reachable and we haven't established a
10541058
// CASE session yet, let's consider it to be stalled and invalidate the pairing session.
10551059

@@ -2745,6 +2749,9 @@ - (void)_setupSubscriptionWithReason:(NSString *)reason
27452749
[self _changeInternalState:MTRInternalDeviceStateSubscribing];
27462750

27472751
MTR_LOG("%@ setting up subscription with reason: %@", self, reason);
2752+
if ([reason hasPrefix:sDeviceMayBeReachableReason]) {
2753+
self.doingCASEAttemptForDeviceMayBeReachable = YES;
2754+
}
27482755

27492756
__block bool markUnreachableAfterWait = true;
27502757
#ifdef DEBUG
@@ -2784,6 +2791,8 @@ - (void)_setupSubscriptionWithReason:(NSString *)reason
27842791
mtr_strongify(self);
27852792
VerifyOrReturn(self, MTR_LOG_DEBUG("_setupSubscriptionWithReason directlyGetSessionForNode called back with nil MTRDevice"));
27862793

2794+
self.doingCASEAttemptForDeviceMayBeReachable = NO;
2795+
27872796
if (error != nil) {
27882797
MTR_LOG_ERROR("%@ getSessionForNode error %@", self, error);
27892798
[self->_deviceController asyncDispatchToMatterQueue:^{
@@ -4844,7 +4853,7 @@ - (void)_deviceMayBeReachable
48444853
std::lock_guard lock(self->_lock);
48454854
// Use _ensureSubscriptionForExistingDelegates so that the subscriptions
48464855
// will go through the pool as needed, not necessarily happen immediately.
4847-
[self _ensureSubscriptionForExistingDelegates:@"SPI client indicated the device may now be reachable"];
4856+
[self _ensureSubscriptionForExistingDelegates:sDeviceMayBeReachableReason];
48484857
} errorHandler:nil];
48494858
}
48504859

0 commit comments

Comments
 (0)