Skip to content

Commit dbf7327

Browse files
Prevent setting up a commissioning session on a suspended controller. (project-chip#35602)
* Prevent setting up a commissioning session on a suspended controller. Since commissioning session setup does not go through MTRDevice or MTRBaseDevice this was not blocked. * Update src/darwin/Framework/CHIP/MTRDeviceController_Concrete.mm Co-authored-by: Kiel Oleson <kielo@apple.com> --------- Co-authored-by: Kiel Oleson <kielo@apple.com>
1 parent 4f35706 commit dbf7327

File tree

3 files changed

+29
-2
lines changed

3 files changed

+29
-2
lines changed

src/darwin/Framework/CHIP/MTRDeviceController.mm

+1-1
Original file line numberDiff line numberDiff line change
@@ -366,7 +366,7 @@ - (instancetype)initWithFactory:(MTRDeviceControllerFactory *)factory
366366

367367
- (NSString *)description
368368
{
369-
return [NSString stringWithFormat:@"<%@: %p uuid %@>", NSStringFromClass(self.class), self, self.uniqueIdentifier];
369+
return [NSString stringWithFormat:@"<%@: %p, uuid: %@, suspended: %@>", NSStringFromClass(self.class), self, self.uniqueIdentifier, MTR_YES_NO(self.suspended)];
370370
}
371371

372372
- (BOOL)isRunning

src/darwin/Framework/CHIP/MTRDeviceController_Concrete.mm

+19-1
Original file line numberDiff line numberDiff line change
@@ -345,7 +345,7 @@ - (instancetype)initWithFactory:(MTRDeviceControllerFactory *)factory
345345

346346
- (NSString *)description
347347
{
348-
return [NSString stringWithFormat:@"<%@: %p uuid %@>", NSStringFromClass(self.class), self, self.uniqueIdentifier];
348+
return [NSString stringWithFormat:@"<%@: %p, uuid: %@, suspended: %@>", NSStringFromClass(self.class), self, self.uniqueIdentifier, MTR_YES_NO(self.suspended)];
349349
}
350350

351351
- (BOOL)isRunning
@@ -823,6 +823,15 @@ - (BOOL)setupCommissioningSessionWithPayload:(MTRSetupPayload *)payload
823823
newNodeID:(NSNumber *)newNodeID
824824
error:(NSError * __autoreleasing *)error
825825
{
826+
if (self.suspended) {
827+
MTR_LOG_ERROR("%@ suspended: can't set up commissioning session for device ID 0x%016llX with setup payload %@", self, newNodeID.unsignedLongLongValue, payload);
828+
// TODO: Can we do a better error here?
829+
if (error) {
830+
*error = [MTRError errorForCHIPErrorCode:CHIP_ERROR_INCORRECT_STATE];
831+
}
832+
return NO;
833+
}
834+
826835
MTR_LOG("Setting up commissioning session for device ID 0x%016llX with setup payload %@", newNodeID.unsignedLongLongValue, payload);
827836

828837
[[MTRMetricsCollector sharedInstance] resetMetrics];
@@ -952,6 +961,15 @@ - (BOOL)commissionNodeWithID:(NSNumber *)nodeID
952961
commissioningParams:(MTRCommissioningParameters *)commissioningParams
953962
error:(NSError * __autoreleasing *)error
954963
{
964+
if (self.suspended) {
965+
MTR_LOG_ERROR("%@ suspended: can't commission device ID 0x%016llX with parameters %@", self, nodeID.unsignedLongLongValue, commissioningParams);
966+
// TODO: Can we do a better error here?
967+
if (error) {
968+
*error = [MTRError errorForCHIPErrorCode:CHIP_ERROR_INCORRECT_STATE];
969+
}
970+
return NO;
971+
}
972+
955973
auto block = ^BOOL {
956974
chip::Controller::CommissioningParameters params;
957975
if (commissioningParams.csrNonce) {

src/darwin/Framework/CHIPTests/MTRPerControllerStorageTests.m

+9
Original file line numberDiff line numberDiff line change
@@ -1682,6 +1682,15 @@ - (void)test012_startSuspended
16821682
XCTAssertNotNil(controller);
16831683
XCTAssertTrue(controller.running);
16841684
XCTAssertTrue(controller.suspended);
1685+
1686+
// Test that a suspended controller can't set up a commissioning session.
1687+
__auto_type * payload = [MTRSetupPayload setupPayloadWithOnboardingPayload:kOnboardingPayload error:&error];
1688+
XCTAssertNil(error);
1689+
XCTAssertNotNil(payload);
1690+
1691+
[controller setupCommissioningSessionWithPayload:payload newNodeID:@(17) error:&error];
1692+
XCTAssertNotNil(error);
1693+
16851694
[controller shutdown];
16861695
}
16871696

0 commit comments

Comments
 (0)