Skip to content

Commit

Permalink
Update the Darwin APIs to use the rendezvous transport agnostic pairi…
Browse files Browse the repository at this point in the history
…ng API
  • Loading branch information
sagar-apple committed Nov 3, 2021
1 parent 36329e1 commit b411c42
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 31 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -727,7 +727,7 @@ - (void)handleRendezVousBLE:(uint16_t)discriminator setupPINCode:(uint32_t)setup
{
NSError * error;
uint64_t deviceID = CHIPGetNextAvailableDeviceID();
if ([self.chipController pairDevice:deviceID discriminator:discriminator setupPINCode:setupPINCode csrNonce:nil error:&error]) {
if ([self.chipController pairDevice:deviceID discriminator:discriminator setupPINCode:setupPINCode error:&error]) {
deviceID++;
CHIPSetNextAvailableDeviceID(deviceID);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -207,7 +207,6 @@ - (void)reportFromUserEnteredSettings
[cluster
subscribeAttributeMeasuredValueWithMinInterval:minIntervalSeconds
maxInterval:maxIntervalSeconds
change:deltaInCelsius
responseHandler:^(NSError * error, NSDictionary * values) {
if (error == nil)
return;
Expand Down
6 changes: 1 addition & 5 deletions src/darwin/Framework/CHIP/CHIPDeviceController.h
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,6 @@ typedef void (^CHIPDeviceConnectionCallback)(CHIPDevice * _Nullable device, NSEr
- (BOOL)pairDevice:(uint64_t)deviceID
discriminator:(uint16_t)discriminator
setupPINCode:(uint32_t)setupPINCode
csrNonce:(nullable NSData *)csrNonce
error:(NSError * __autoreleasing *)error;

- (BOOL)pairDevice:(uint64_t)deviceID
Expand All @@ -49,10 +48,7 @@ typedef void (^CHIPDeviceConnectionCallback)(CHIPDevice * _Nullable device, NSEr
setupPINCode:(uint32_t)setupPINCode
error:(NSError * __autoreleasing *)error;

- (BOOL)pairDevice:(uint64_t)deviceID
onboardingPayload:(NSString *)onboardingPayload
onboardingPayloadType:(CHIPOnboardingPayloadType)onboardingPayloadType
error:(NSError * __autoreleasing *)error;
- (BOOL)pairDevice:(uint64_t)deviceID onboardingPayload:(NSString *)onboardingPayload error:(NSError * __autoreleasing *)error;

- (void)setListenPort:(uint16_t)port;
- (BOOL)unpairDevice:(uint64_t)deviceID error:(NSError * __autoreleasing *)error;
Expand Down
50 changes: 26 additions & 24 deletions src/darwin/Framework/CHIP/CHIPDeviceController.mm
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@
#include <credentials/examples/DeviceAttestationVerifierExample.h>
#include <lib/support/CHIPMem.h>
#include <platform/PlatformManager.h>
#include <setup_payload/ManualSetupPayloadGenerator.h>

static const char * const CHIP_COMMISSIONER_DEVICE_ID_KEY = "com.zigbee.chip.commissioner.device_id";

Expand All @@ -51,6 +52,7 @@
static NSString * const kErrorGetPairedDevice = @"Failure while trying to retrieve a paired device";
static NSString * const kErrorNotRunning = @"Controller is not running. Call startup first.";
static NSString * const kInfoStackShutdown = @"Shutting down the CHIP Stack";
static NSString * const kErrorSetupCodeGen = @"Generating Manual Pairing Code failed";

@interface CHIPDeviceController ()

Expand Down Expand Up @@ -267,7 +269,6 @@ - (NSNumber *)_getControllerNodeId
- (BOOL)pairDevice:(uint64_t)deviceID
discriminator:(uint16_t)discriminator
setupPINCode:(uint32_t)setupPINCode
csrNonce:(nullable NSData *)csrNonce
error:(NSError * __autoreleasing *)error
{
__block CHIP_ERROR errorCode = CHIP_ERROR_INCORRECT_STATE;
Expand All @@ -277,16 +278,19 @@ - (BOOL)pairDevice:(uint64_t)deviceID
return success;
}
dispatch_sync(_chipWorkQueue, ^{
chip::RendezvousParameters params
= chip::RendezvousParameters().SetSetupPINCode(setupPINCode).SetDiscriminator(discriminator);

if (csrNonce != nil) {
params = params.SetCSRNonce(chip::ByteSpan((const uint8_t *) csrNonce.bytes, csrNonce.length));
std::string manualPairingCode;
chip::SetupPayload payload;
payload.discriminator = discriminator;
payload.setUpPINCode = setupPINCode;

errorCode = chip::ManualSetupPayloadGenerator(payload).payloadDecimalStringRepresentation(manualPairingCode);
success = ![self checkForError:errorCode logMsg:kErrorSetupCodeGen error:error];
if (!success) {
return;
}

if ([self isRunning]) {
_operationalCredentialsDelegate->SetDeviceID(deviceID);
errorCode = self.cppCommissioner->PairDevice(deviceID, params);
errorCode = self.cppCommissioner->PairDevice(deviceID, manualPairingCode.c_str());
}
success = ![self checkForError:errorCode logMsg:kErrorPairDevice error:error];
});
Expand Down Expand Up @@ -326,24 +330,22 @@ - (BOOL)pairDevice:(uint64_t)deviceID
return success;
}

- (BOOL)pairDevice:(uint64_t)deviceID
onboardingPayload:(NSString *)onboardingPayload
onboardingPayloadType:(CHIPOnboardingPayloadType)onboardingPayloadType
error:(NSError * __autoreleasing *)error
- (BOOL)pairDevice:(uint64_t)deviceID onboardingPayload:(NSString *)onboardingPayload error:(NSError * __autoreleasing *)error
{
BOOL didSucceed = NO;
CHIPSetupPayload * setupPayload = [CHIPOnboardingPayloadParser setupPayloadForOnboardingPayload:onboardingPayload
ofType:onboardingPayloadType
error:error];
if (setupPayload) {
uint16_t discriminator = setupPayload.discriminator.unsignedShortValue;
uint32_t setupPINCode = setupPayload.setUpPINCode.unsignedIntValue;
_operationalCredentialsDelegate->SetDeviceID(deviceID);
didSucceed = [self pairDevice:deviceID discriminator:discriminator setupPINCode:setupPINCode csrNonce:nil error:error];
} else {
CHIP_LOG_ERROR("Failed to create CHIPSetupPayload for pairing with error %@", *error);
__block CHIP_ERROR errorCode = CHIP_ERROR_INCORRECT_STATE;
__block BOOL success = NO;
if (![self isRunning]) {
success = ![self checkForError:errorCode logMsg:kErrorNotRunning error:error];
return success;
}
return didSucceed;
dispatch_sync(_chipWorkQueue, ^{
if ([self isRunning]) {
_operationalCredentialsDelegate->SetDeviceID(deviceID);
errorCode = self.cppCommissioner->PairDevice(deviceID, [onboardingPayload UTF8String]);
}
success = ![self checkForError:errorCode logMsg:kErrorPairDevice error:error];
});
return success;
}

- (BOOL)unpairDevice:(uint64_t)deviceID error:(NSError * __autoreleasing *)error
Expand Down

0 comments on commit b411c42

Please sign in to comment.