Skip to content

Commit

Permalink
Fix "ethernet" pairing for chip-tool-darwin. (#18111)
Browse files Browse the repository at this point in the history
Makes the various pairDevice APIs on CHIPDeviceController have
identical behavior in terms of just establishing a PASE session and
waiting for the API consumer to call commissionDevice as desired.

Without this change, both the API implementation and
chip-tool-darwin's pairing delegate would try to commission the same
device, which would fail.
  • Loading branch information
bzbarsky-apple authored May 6, 2022
1 parent 5426850 commit 68518ce
Show file tree
Hide file tree
Showing 6 changed files with 52 additions and 33 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,6 @@
[CurrentCommissioner() pairDevice:mNodeId
address:[NSString stringWithUTF8String:ipAddress]
port:mRemotePort
discriminator:mDiscriminator
setupPINCode:mSetupPINCode
error:error];
}
33 changes: 32 additions & 1 deletion src/darwin/Framework/CHIP/CHIPDeviceController.h
Original file line number Diff line number Diff line change
Expand Up @@ -35,18 +35,49 @@ typedef void (^CHIPDeviceConnectionCallback)(CHIPDevice * _Nullable device, NSEr

@property (readonly, nonatomic) BOOL isRunning;

/**
* Start pairing for a device with the given ID, using the provided setup PIN
* to establish a PASE connection.
*
* The IP and port for the device will be discovered automatically based on the
* provided discriminator.
*
* The pairing process will proceed until a PASE session is established or an
* error occurs, then notify onPairingComplete on the CHIPDevicePairingDelegate
* for this controller. That delegate is expected to call commissionDevice
* after that point if it wants to commission the device.
*/
- (BOOL)pairDevice:(uint64_t)deviceID
discriminator:(uint16_t)discriminator
setupPINCode:(uint32_t)setupPINCode
error:(NSError * __autoreleasing *)error;

/**
* Start pairing for a device with the given ID, using the provided IP address
* and port to connect to the device and the provided setup PIN to establish a
* PASE connection.
*
* The pairing process will proceed until a PASE session is established or an
* error occurs, then notify onPairingComplete on the CHIPDevicePairingDelegate
* for this controller. That delegate is expected to call commissionDevice
* after that point if it wants to commission the device.
*/
- (BOOL)pairDevice:(uint64_t)deviceID
address:(NSString *)address
port:(uint16_t)port
discriminator:(uint16_t)discriminator
setupPINCode:(uint32_t)setupPINCode
error:(NSError * __autoreleasing *)error;

/**
* Start pairing for a device with the given ID and onboarding payload (QR code
* or manual setup code). The payload will be used to discover the device and
* establish a PASE connection.
*
* The pairing process will proceed until a PASE session is established or an
* error occurs, then notify onPairingComplete on the CHIPDevicePairingDelegate
* for this controller. That delegate is expected to call commissionDevice
* after that point if it wants to commission the device.
*/
- (BOOL)pairDevice:(uint64_t)deviceID onboardingPayload:(NSString *)onboardingPayload error:(NSError * __autoreleasing *)error;
- (BOOL)commissionDevice:(uint64_t)deviceId
commissioningParams:(CHIPCommissioningParameters *)commissioningParams
Expand Down
8 changes: 2 additions & 6 deletions src/darwin/Framework/CHIP/CHIPDeviceController.mm
Original file line number Diff line number Diff line change
Expand Up @@ -315,7 +315,6 @@ - (BOOL)pairDevice:(uint64_t)deviceID
- (BOOL)pairDevice:(uint64_t)deviceID
address:(NSString *)address
port:(uint16_t)port
discriminator:(uint16_t)discriminator
setupPINCode:(uint32_t)setupPINCode
error:(NSError * __autoreleasing *)error
{
Expand All @@ -330,13 +329,10 @@ - (BOOL)pairDevice:(uint64_t)deviceID
chip::Inet::IPAddress::FromString([address UTF8String], addr);
chip::Transport::PeerAddress peerAddress = chip::Transport::PeerAddress::UDP(addr, port);

chip::RendezvousParameters params = chip::RendezvousParameters()
.SetSetupPINCode(setupPINCode)
.SetDiscriminator(discriminator)
.SetPeerAddress(peerAddress);
chip::RendezvousParameters params = chip::RendezvousParameters().SetSetupPINCode(setupPINCode).SetPeerAddress(peerAddress);
if ([self isRunning]) {
_operationalCredentialsDelegate->SetDeviceID(deviceID);
errorCode = self.cppCommissioner->PairDevice(deviceID, params);
errorCode = self.cppCommissioner->EstablishPASEConnection(deviceID, params);
}
success = ![self checkForError:errorCode logMsg:kErrorPairDevice error:error];
});
Expand Down
12 changes: 5 additions & 7 deletions src/darwin/Framework/CHIPTests/CHIPClustersTests.m

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

16 changes: 7 additions & 9 deletions src/darwin/Framework/CHIPTests/CHIPDeviceTests.m
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,6 @@
static const uint16_t kCASESetupTimeoutInSeconds = 30;
static const uint16_t kTimeoutInSeconds = 3;
static const uint64_t kDeviceId = 0x12344321;
static const uint16_t kDiscriminator = 3840;
static const uint32_t kSetupPINCode = 20202021;
static const uint16_t kRemotePort = 5540;
static const uint16_t kLocalPort = 5541;
Expand Down Expand Up @@ -98,8 +97,12 @@ - (id)initWithExpectation:(XCTestExpectation *)expectation
- (void)onPairingComplete:(NSError *)error
{
XCTAssertEqual(error.code, 0);
[_expectation fulfill];
_expectation = nil;

NSError * commissionError = nil;
[sController commissionDevice:kDeviceId commissioningParams:[[CHIPCommissioningParameters alloc] init] error:&commissionError];
XCTAssertNil(commissionError);

// Keep waiting for onCommissioningComplete
}

- (void)onCommissioningComplete:(NSError *)error
Expand Down Expand Up @@ -166,12 +169,7 @@ - (void)initStack
[controller setPairingDelegate:pairing queue:callbackQueue];

NSError * error;
[controller pairDevice:kDeviceId
address:kAddress
port:kRemotePort
discriminator:kDiscriminator
setupPINCode:kSetupPINCode
error:&error];
[controller pairDevice:kDeviceId address:kAddress port:kRemotePort setupPINCode:kSetupPINCode error:&error];
XCTAssertEqual(error.code, 0);

[self waitForExpectationsWithTimeout:kPairingTimeoutInSeconds handler:nil];
Expand Down
15 changes: 6 additions & 9 deletions src/darwin/Framework/CHIPTests/CHIPXPCListenerSampleTests.m
Original file line number Diff line number Diff line change
Expand Up @@ -430,7 +430,6 @@ - (void)readAttributeCacheWithController:(id _Nullable)controller
static const uint16_t kCASESetupTimeoutInSeconds = 30;
static const uint16_t kTimeoutInSeconds = 3;
static const uint64_t kDeviceId = 0x12344321;
static const uint16_t kDiscriminator = 3840;
static const uint32_t kSetupPINCode = 20202021;
static const uint16_t kRemotePort = 5540;
static const uint16_t kLocalPort = 5541;
Expand Down Expand Up @@ -465,8 +464,11 @@ - (id)initWithExpectation:(XCTestExpectation *)expectation
- (void)onPairingComplete:(NSError *)error
{
XCTAssertEqual(error.code, 0);
[_expectation fulfill];
_expectation = nil;
NSError * commissionError = nil;
[sController commissionDevice:kDeviceId commissioningParams:[[CHIPCommissioningParameters alloc] init] error:&commissionError];
XCTAssertNil(commissionError);

// Keep waiting for onCommissioningComplete
}

- (void)onCommissioningComplete:(NSError *)error
Expand Down Expand Up @@ -535,12 +537,7 @@ - (void)initStack
[controller setPairingDelegate:pairing queue:callbackQueue];

NSError * error;
[controller pairDevice:kDeviceId
address:kAddress
port:kRemotePort
discriminator:kDiscriminator
setupPINCode:kSetupPINCode
error:&error];
[controller pairDevice:kDeviceId address:kAddress port:kRemotePort setupPINCode:kSetupPINCode error:&error];
XCTAssertEqual(error.code, 0);

[self waitForExpectationsWithTimeout:kPairingTimeoutInSeconds handler:nil];
Expand Down

0 comments on commit 68518ce

Please sign in to comment.