Skip to content

Commit

Permalink
Fix BLE commissioning with iOS CHIPTool. (#16075)
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
bzbarsky-apple authored and pull[bot] committed Feb 22, 2024
1 parent d8b003b commit 2347960
Show file tree
Hide file tree
Showing 5 changed files with 47 additions and 6 deletions.
2 changes: 2 additions & 0 deletions src/controller/CommissioneeDeviceProxy.h
Original file line number Diff line number Diff line change
Expand Up @@ -226,6 +226,8 @@ class CommissioneeDeviceProxy : public DeviceProxy, public SessionReleaseDelegat
return LoadSecureSessionParametersIfNeeded(loadedSecureSession);
};

Transport::Type GetDeviceTransportType() const { return mDeviceAddress.GetTransportType(); }

private:
enum class ConnectionState
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}
}
}

Expand Down
6 changes: 6 additions & 0 deletions src/darwin/Framework/CHIP/CHIPDeviceController.h
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down
21 changes: 19 additions & 2 deletions src/darwin/Framework/CHIP/CHIPDeviceController.mm
Original file line number Diff line number Diff line change
Expand Up @@ -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];
});
Expand Down Expand Up @@ -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];
});
Expand Down Expand Up @@ -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
6 changes: 6 additions & 0 deletions src/darwin/Framework/CHIP/CHIPDeviceControllerOverXPC.m
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 2347960

Please sign in to comment.