From 23479601ba12995cc313326021eebc4099e8b354 Mon Sep 17 00:00:00 2001 From: Boris Zbarsky Date: Thu, 10 Mar 2022 18:17:56 -0500 Subject: [PATCH] Fix BLE commissioning with iOS CHIPTool. (#16075) Two changes: 1. Fix BLE commissioning by explicitly using EstablishPASESession so we get a chance to provide network credentials after that's done. 2. Fix the CHIPTool UI for the on-network case to not prompt for network credentials after the PASE session is established by exposing whether the device being commissioned is connected over BLE or not. We need a better plan for how to handle that last bit, but that will probably be part of the general PairingDelegate redesign. --- src/controller/CommissioneeDeviceProxy.h | 2 ++ .../QRCode/QRCodeViewController.m | 18 ++++++++++++---- .../Framework/CHIP/CHIPDeviceController.h | 6 ++++++ .../Framework/CHIP/CHIPDeviceController.mm | 21 +++++++++++++++++-- .../CHIP/CHIPDeviceControllerOverXPC.m | 6 ++++++ 5 files changed, 47 insertions(+), 6 deletions(-) diff --git a/src/controller/CommissioneeDeviceProxy.h b/src/controller/CommissioneeDeviceProxy.h index 14208d1f49da0e..a40a38eff91733 100644 --- a/src/controller/CommissioneeDeviceProxy.h +++ b/src/controller/CommissioneeDeviceProxy.h @@ -226,6 +226,8 @@ class CommissioneeDeviceProxy : public DeviceProxy, public SessionReleaseDelegat return LoadSecureSessionParametersIfNeeded(loadedSecureSession); }; + Transport::Type GetDeviceTransportType() const { return mDeviceAddress.GetTransportType(); } + private: enum class ConnectionState { diff --git a/src/darwin/CHIPTool/CHIPTool/View Controllers/QRCode/QRCodeViewController.m b/src/darwin/CHIPTool/CHIPTool/View Controllers/QRCode/QRCodeViewController.m index aa6b3abd4de11f..ad87d2d4ce8fbd 100644 --- a/src/darwin/CHIPTool/CHIPTool/View Controllers/QRCode/QRCodeViewController.m +++ b/src/darwin/CHIPTool/CHIPTool/View Controllers/QRCode/QRCodeViewController.m @@ -479,10 +479,20 @@ - (void)onPairingComplete:(NSError * _Nullable)error if (error != nil) { NSLog(@"Got pairing error back %@", error); } else { - dispatch_async(dispatch_get_main_queue(), ^{ - [self->_deviceList refreshDeviceList]; - [self retrieveAndSendWiFiCredentials]; - }); + CHIPDeviceController * controller = [CHIPDeviceController sharedController]; + uint64_t deviceId = CHIPGetLastPairedDeviceId(); + if ([controller deviceBeingCommissionedOverBLE:deviceId]) { + dispatch_async(dispatch_get_main_queue(), ^{ + [self->_deviceList refreshDeviceList]; + [self retrieveAndSendWiFiCredentials]; + }); + } else { + CHIPCommissioningParameters * params = [[CHIPCommissioningParameters alloc] init]; + NSError * error; + if (![controller commissionDevice:deviceId commissioningParams:params error:&error]) { + NSLog(@"Failed to commission Device %llu, with error %@", deviceId, error); + } + } } } diff --git a/src/darwin/Framework/CHIP/CHIPDeviceController.h b/src/darwin/Framework/CHIP/CHIPDeviceController.h index b91c5d430cf1cf..110af1bd3c497b 100644 --- a/src/darwin/Framework/CHIP/CHIPDeviceController.h +++ b/src/darwin/Framework/CHIP/CHIPDeviceController.h @@ -70,6 +70,12 @@ typedef void (^CHIPDeviceConnectionCallback)(CHIPDevice * _Nullable device, NSEr setupPIN:(NSUInteger)setupPIN error:(NSError * __autoreleasing *)error; +/** + * Temporary until PairingDelegate is fixed to clearly communicate this + * information to consumers. + */ +- (BOOL)deviceBeingCommissionedOverBLE:(uint64_t)deviceId; + - (instancetype)init NS_UNAVAILABLE; + (instancetype)new NS_UNAVAILABLE; diff --git a/src/darwin/Framework/CHIP/CHIPDeviceController.mm b/src/darwin/Framework/CHIP/CHIPDeviceController.mm index 14c8479245556a..8d4e54ae952f97 100644 --- a/src/darwin/Framework/CHIP/CHIPDeviceController.mm +++ b/src/darwin/Framework/CHIP/CHIPDeviceController.mm @@ -299,7 +299,7 @@ - (BOOL)pairDevice:(uint64_t)deviceID } if ([self isRunning]) { _operationalCredentialsDelegate->SetDeviceID(deviceID); - errorCode = self.cppCommissioner->PairDevice(deviceID, manualPairingCode.c_str()); + errorCode = self.cppCommissioner->EstablishPASEConnection(deviceID, manualPairingCode.c_str()); } success = ![self checkForError:errorCode logMsg:kErrorPairDevice error:error]; }); @@ -350,7 +350,7 @@ - (BOOL)pairDevice:(uint64_t)deviceID onboardingPayload:(NSString *)onboardingPa dispatch_sync(_chipWorkQueue, ^{ if ([self isRunning]) { _operationalCredentialsDelegate->SetDeviceID(deviceID); - errorCode = self.cppCommissioner->PairDevice(deviceID, [onboardingPayload UTF8String]); + errorCode = self.cppCommissioner->EstablishPASEConnection(deviceID, [onboardingPayload UTF8String]); } success = ![self checkForError:errorCode logMsg:kErrorPairDevice error:error]; }); @@ -641,4 +641,21 @@ - (void)dealloc { } +- (BOOL)deviceBeingCommissionedOverBLE:(uint64_t)deviceId +{ + CHIP_ERROR errorCode = CHIP_ERROR_INCORRECT_STATE; + if (![self isRunning]) { + [self checkForError:errorCode logMsg:kErrorNotRunning error:nil]; + return NO; + } + + chip::CommissioneeDeviceProxy * deviceProxy; + errorCode = self->_cppCommissioner->GetDeviceBeingCommissioned(deviceId, &deviceProxy); + if (errorCode != CHIP_NO_ERROR) { + return NO; + } + + return deviceProxy->GetDeviceTransportType() == chip::Transport::Type::kBle; +} + @end diff --git a/src/darwin/Framework/CHIP/CHIPDeviceControllerOverXPC.m b/src/darwin/Framework/CHIP/CHIPDeviceControllerOverXPC.m index 7831d0c7313e2a..3c4ca4d586e704 100644 --- a/src/darwin/Framework/CHIP/CHIPDeviceControllerOverXPC.m +++ b/src/darwin/Framework/CHIP/CHIPDeviceControllerOverXPC.m @@ -103,6 +103,12 @@ - (nullable CHIPDevice *)getDeviceBeingCommissioned:(uint64_t)deviceId error:(NS return nil; } +- (BOOL)deviceBeingCommissionedOverBLE:(uint64_t)deviceId +{ + CHIP_LOG_ERROR("CHIPDevice doesn't support deviceBeingCommissionedOverBLE over XPC"); + return NO; +} + - (BOOL)getConnectedDevice:(uint64_t)deviceID queue:(dispatch_queue_t)queue completionHandler:(CHIPDeviceConnectionCallback)completionHandler