Skip to content

Commit a92b952

Browse files
Select maxInterval based on device sleep interval in MTRDevice. (#23426)
* Select maxInterval based on device sleep interval in MTRDevice. This lets us avoid hardcoding a value which might not be appropriate to all devices. * Address review comment.
1 parent f88f7bb commit a92b952

File tree

2 files changed

+61
-5
lines changed

2 files changed

+61
-5
lines changed

src/darwin/Framework/CHIP/MTRDevice.mm

+17-5
Original file line numberDiff line numberDiff line change
@@ -174,15 +174,16 @@ + (instancetype)deviceWithNodeID:(NSNumber *)nodeID controller:(MTRDeviceControl
174174
#pragma mark Subscription and delegate handling
175175

176176
// subscription intervals are in seconds
177-
#define MTR_DEVICE_SUBSCRIPTION_MAX_INTERVAL_DEFAULT (3600)
177+
#define MTR_DEVICE_SUBSCRIPTION_MAX_INTERVAL_MIN (2)
178+
#define MTR_DEVICE_SUBSCRIPTION_MAX_INTERVAL_MAX (60)
178179

179180
- (void)setDelegate:(id<MTRDeviceDelegate>)delegate queue:(dispatch_queue_t)queue
180181
{
181182
os_unfair_lock_lock(&self->_lock);
182183

183184
_weakDelegate = [MTRWeakReference weakReferenceWithObject:delegate];
184185
_delegateQueue = queue;
185-
[self subscribeWithMinInterval:0 maxInterval:MTR_DEVICE_SUBSCRIPTION_MAX_INTERVAL_DEFAULT];
186+
[self setupSubscription];
186187

187188
os_unfair_lock_unlock(&self->_lock);
188189
}
@@ -285,7 +286,7 @@ - (void)_handleEventReport:(NSArray<NSDictionary<NSString *, id> *> *)eventRepor
285286
os_unfair_lock_unlock(&self->_lock);
286287
}
287288

288-
- (void)subscribeWithMinInterval:(uint16_t)minInterval maxInterval:(uint16_t)maxInterval
289+
- (void)setupSubscription
289290
{
290291
// for now just subscribe once
291292
if (_subscriptionActive) {
@@ -310,8 +311,19 @@ - (void)subscribeWithMinInterval:(uint16_t)minInterval maxInterval:(uint16_t)max
310311
// We want to get event reports at the minInterval, not the maxInterval.
311312
eventPath->mIsUrgentEvent = true;
312313
ReadPrepareParams readParams(session.Value());
313-
readParams.mMinIntervalFloorSeconds = minInterval;
314-
readParams.mMaxIntervalCeilingSeconds = maxInterval;
314+
315+
readParams.mMinIntervalFloorSeconds = 0;
316+
// Select a max interval based on the device's claimed idle sleep interval.
317+
auto idleSleepInterval = std::chrono::duration_cast<System::Clock::Seconds32>(
318+
session.Value()->GetRemoteMRPConfig().mIdleRetransTimeout);
319+
if (idleSleepInterval.count() < MTR_DEVICE_SUBSCRIPTION_MAX_INTERVAL_MIN) {
320+
idleSleepInterval = System::Clock::Seconds32(MTR_DEVICE_SUBSCRIPTION_MAX_INTERVAL_MIN);
321+
}
322+
if (idleSleepInterval.count() > MTR_DEVICE_SUBSCRIPTION_MAX_INTERVAL_MAX) {
323+
idleSleepInterval = System::Clock::Seconds32(MTR_DEVICE_SUBSCRIPTION_MAX_INTERVAL_MAX);
324+
}
325+
readParams.mMaxIntervalCeilingSeconds = static_cast<uint16_t>(idleSleepInterval.count());
326+
315327
readParams.mpAttributePathParamsList = attributePath.get();
316328
readParams.mAttributePathParamsListSize = 1;
317329
readParams.mpEventPathParamsList = eventPath.get();

src/darwin/Framework/CHIPTests/MTRDeviceTests.m

+44
Original file line numberDiff line numberDiff line change
@@ -122,6 +122,28 @@ - (void)onAddressUpdated:(NSError *)error
122122
}
123123
@end
124124

125+
@interface MTRDeviceTestDelegate : NSObject <MTRDeviceDelegate>
126+
@property (nonatomic) dispatch_block_t onSubscriptionEstablished;
127+
@end
128+
129+
@implementation MTRDeviceTestDelegate
130+
- (void)device:(MTRDevice *)device stateChanged:(MTRDeviceState)state
131+
{
132+
if (state == MTRDeviceStateReachable) {
133+
self.onSubscriptionEstablished();
134+
}
135+
}
136+
137+
- (void)device:(MTRDevice *)device receivedAttributeReport:(NSArray<NSDictionary<NSString *, id> *> *)attributeReport
138+
{
139+
}
140+
141+
- (void)device:(MTRDevice *)device receivedEventReport:(NSArray<NSDictionary<NSString *, id> *> *)eventReport
142+
{
143+
}
144+
145+
@end
146+
125147
@interface MTRDeviceTests : XCTestCase
126148
@end
127149

@@ -1359,6 +1381,28 @@ - (void)test016_FailedSubscribeWithCacheReadDuringFailure
13591381
[self waitForExpectations:@[ errorExpectation ] timeout:60];
13601382
}
13611383

1384+
- (void)test017_TestMTRDeviceBasics
1385+
{
1386+
#if MANUAL_INDIVIDUAL_TEST
1387+
[self initStack];
1388+
[self waitForCommissionee];
1389+
#endif
1390+
1391+
__auto_type * device = [MTRDevice deviceWithNodeID:kDeviceId deviceController:sController];
1392+
dispatch_queue_t queue = dispatch_get_main_queue();
1393+
1394+
XCTestExpectation * subscriptionExpectation = [self expectationWithDescription:@"Subscription has been set up"];
1395+
1396+
__auto_type * delegate = [[MTRDeviceTestDelegate alloc] init];
1397+
delegate.onSubscriptionEstablished = ^() {
1398+
[subscriptionExpectation fulfill];
1399+
};
1400+
1401+
[device setDelegate:delegate queue:queue];
1402+
1403+
[self waitForExpectations:@[ subscriptionExpectation ] timeout:60];
1404+
}
1405+
13621406
- (void)test900_SubscribeAllAttributes
13631407
{
13641408
#if MANUAL_INDIVIDUAL_TEST

0 commit comments

Comments
 (0)