From 86bbcba37a0fcaa2b01463caf830564463923657 Mon Sep 17 00:00:00 2001 From: Sagar Dhawan Date: Thu, 4 Nov 2021 08:58:24 -0500 Subject: [PATCH] Update the Darwin APIs to use the rendezvous transport agnostic pairing API (#11349) --- src/darwin/CHIPTool/CHIPTool/Info.plist | 2 + .../QRCode/QRCodeViewController.m | 37 +++++++------- .../TemperatureSensorViewController.m | 1 - .../Framework/CHIP/CHIPDeviceController.h | 6 +-- .../Framework/CHIP/CHIPDeviceController.mm | 50 ++++++++++--------- 5 files changed, 47 insertions(+), 49 deletions(-) diff --git a/src/darwin/CHIPTool/CHIPTool/Info.plist b/src/darwin/CHIPTool/CHIPTool/Info.plist index ca2dd2bdea2bb3..76f6de3383393b 100644 --- a/src/darwin/CHIPTool/CHIPTool/Info.plist +++ b/src/darwin/CHIPTool/CHIPTool/Info.plist @@ -27,6 +27,8 @@ NSBonjourServices _matter._tcp + _matterc._udp + _matterd._udp NSCameraUsageDescription Used to scan QR code diff --git a/src/darwin/CHIPTool/CHIPTool/View Controllers/QRCode/QRCodeViewController.m b/src/darwin/CHIPTool/CHIPTool/View Controllers/QRCode/QRCodeViewController.m index 886f46e73b235b..7076e99429e92b 100644 --- a/src/darwin/CHIPTool/CHIPTool/View Controllers/QRCode/QRCodeViewController.m +++ b/src/darwin/CHIPTool/CHIPTool/View Controllers/QRCode/QRCodeViewController.m @@ -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; @@ -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 @@ -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; @@ -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; } } @@ -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); } @@ -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]; } } @@ -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]; }); }); } @@ -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]; } } diff --git a/src/darwin/CHIPTool/CHIPTool/View Controllers/Temperature Sensor/TemperatureSensorViewController.m b/src/darwin/CHIPTool/CHIPTool/View Controllers/Temperature Sensor/TemperatureSensorViewController.m index dd82722484607c..8b016895154492 100644 --- a/src/darwin/CHIPTool/CHIPTool/View Controllers/Temperature Sensor/TemperatureSensorViewController.m +++ b/src/darwin/CHIPTool/CHIPTool/View Controllers/Temperature Sensor/TemperatureSensorViewController.m @@ -207,7 +207,6 @@ - (void)reportFromUserEnteredSettings [cluster subscribeAttributeMeasuredValueWithMinInterval:minIntervalSeconds maxInterval:maxIntervalSeconds - change:deltaInCelsius responseHandler:^(NSError * error, NSDictionary * values) { if (error == nil) return; diff --git a/src/darwin/Framework/CHIP/CHIPDeviceController.h b/src/darwin/Framework/CHIP/CHIPDeviceController.h index a80d0c9b2b0e3e..e9848dada27d5c 100644 --- a/src/darwin/Framework/CHIP/CHIPDeviceController.h +++ b/src/darwin/Framework/CHIP/CHIPDeviceController.h @@ -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 @@ -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; diff --git a/src/darwin/Framework/CHIP/CHIPDeviceController.mm b/src/darwin/Framework/CHIP/CHIPDeviceController.mm index 5abb3edee538f4..873d3511b264ac 100644 --- a/src/darwin/Framework/CHIP/CHIPDeviceController.mm +++ b/src/darwin/Framework/CHIP/CHIPDeviceController.mm @@ -37,6 +37,7 @@ #include #include #include +#include static const char * const CHIP_COMMISSIONER_DEVICE_ID_KEY = "com.zigbee.chip.commissioner.device_id"; @@ -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 () @@ -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; @@ -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]; }); @@ -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