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 (#11349)
  • Loading branch information
sagar-apple authored and pull[bot] committed Mar 7, 2022
1 parent b6909bd commit 86bbcba
Show file tree
Hide file tree
Showing 5 changed files with 47 additions and 49 deletions.
2 changes: 2 additions & 0 deletions src/darwin/CHIPTool/CHIPTool/Info.plist
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@
<key>NSBonjourServices</key>
<array>
<string>_matter._tcp</string>
<string>_matterc._udp</string>
<string>_matterd._udp</string>
</array>
<key>NSCameraUsageDescription</key>
<string>Used to scan QR code</string>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -460,7 +460,7 @@ - (void)showError:(NSError *)error
});
}

- (void)showPayload:(CHIPSetupPayload *)payload decimalString:(nullable NSString *)decimalString
- (void)showPayload:(CHIPSetupPayload *)payload rawPayload:(NSString *)rawPayload isManualCode:(BOOL)isManualCode
{
[self->_activityIndicator stopAnimating];
self->_activityIndicator.hidden = YES;
Expand All @@ -469,9 +469,9 @@ - (void)showPayload:(CHIPSetupPayload *)payload decimalString:(nullable NSString
self->_setupPayloadView.hidden = NO;
self->_resetButton.hidden = NO;

[self updateUIFields:payload decimalString:decimalString];
[self updateUIFields:payload rawPayload:rawPayload isManualCode:isManualCode];
[self parseOptionalData:payload];
[self handleRendezVous:payload];
[self handleRendezVous:payload rawPayload:rawPayload];
}

- (void)retrieveAndSendWifiCredentials
Expand Down Expand Up @@ -638,11 +638,11 @@ - (void)onAddressUpdated:(NSError *)error
[self setVendorIDOnAccessory];
}

- (void)updateUIFields:(CHIPSetupPayload *)payload decimalString:(nullable NSString *)decimalString
- (void)updateUIFields:(CHIPSetupPayload *)payload rawPayload:(nullable NSString *)rawPayload isManualCode:(BOOL)isManualCode
{
if (decimalString) {
if (isManualCode) {
_manualCodeLabel.hidden = NO;
_manualCodeLabel.text = decimalString;
_manualCodeLabel.text = rawPayload;
_versionLabel.text = NOT_APPLICABLE_STRING;
_rendezVousInformation.text = NOT_APPLICABLE_STRING;
_serialNumber.text = NOT_APPLICABLE_STRING;
Expand Down Expand Up @@ -697,22 +697,20 @@ - (void)parseOptionalData:(CHIPSetupPayload *)payload

// MARK: Rendez Vous

- (void)handleRendezVous:(CHIPSetupPayload *)payload
- (void)handleRendezVous:(CHIPSetupPayload *)payload rawPayload:(NSString *)rawPayload
{
switch (payload.rendezvousInformation) {
case kRendezvousInformationNone:
case kRendezvousInformationOnNetwork:
case kRendezvousInformationBLE:
case kRendezvousInformationAllMask:
NSLog(@"Rendezvous Unknown");
NSLog(@"Rendezvous Default");
[self handleRendezVousDefault:rawPayload];
break;
case kRendezvousInformationSoftAP:
NSLog(@"Rendezvous Wi-Fi");
[self handleRendezVousWiFi:[self getNetworkName:payload.discriminator]];
break;
case kRendezvousInformationNone:
case kRendezvousInformationBLE:
NSLog(@"Rendezvous BLE");
[self handleRendezVousBLE:payload.discriminator.unsignedShortValue setupPINCode:payload.setUpPINCode.unsignedIntValue];
break;
}
}

Expand All @@ -723,11 +721,12 @@ - (NSString *)getNetworkName:(NSNumber *)discriminator
return peripheralFullName;
}

- (void)handleRendezVousBLE:(uint16_t)discriminator setupPINCode:(uint32_t)setupPINCode
- (void)handleRendezVousDefault:(NSString *)payload
{
NSError * error;
uint64_t deviceID = CHIPGetNextAvailableDeviceID();
if ([self.chipController pairDevice:deviceID discriminator:discriminator setupPINCode:setupPINCode csrNonce:nil error:&error]) {

if ([self.chipController pairDevice:deviceID onboardingPayload:payload error:&error]) {
deviceID++;
CHIPSetNextAvailableDeviceID(deviceID);
}
Expand Down Expand Up @@ -780,12 +779,12 @@ - (BOOL)startScanning
return YES;
}

- (void)displayQRCodeInSetupPayloadView:(CHIPSetupPayload *)payload withError:(NSError *)error
- (void)displayQRCodeInSetupPayloadView:(CHIPSetupPayload *)payload rawPayload:(NSString *)rawPayload error:(NSError *)error
{
if (error) {
[self showError:error];
} else {
[self showPayload:payload decimalString:nil];
[self showPayload:payload rawPayload:rawPayload isManualCode:NO];
}
}

Expand All @@ -802,7 +801,7 @@ - (void)scannedQRCode:(NSString *)qrCode
[self postScanningQRCodeState];

dispatch_after(dispatch_time(DISPATCH_TIME_NOW, INDICATOR_DELAY), dispatch_get_main_queue(), ^{
[self displayQRCodeInSetupPayloadView:self->_setupPayload withError:error];
[self displayQRCodeInSetupPayloadView:self->_setupPayload rawPayload:qrCode error:error];
});
});
}
Expand All @@ -829,7 +828,7 @@ - (void)displayManualCodeInSetupPayloadView:(CHIPSetupPayload *)payload
if (error) {
[self showError:error];
} else {
[self showPayload:payload decimalString:decimalString];
[self showPayload:payload rawPayload:decimalString isManualCode:YES];
}
}

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 86bbcba

Please sign in to comment.