Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix pairing via the SetupCodePairer #13677

Merged
merged 7 commits into from
Jan 20, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion src/controller/CHIPDeviceController.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1628,7 +1628,6 @@ void DeviceCommissioner::OnNodeIdResolved(const chip::Dnssd::ResolvedNodeData &

if (mDeviceBeingCommissioned != nullptr && mDeviceBeingCommissioned->GetDeviceId() == nodeData.mPeerId.GetNodeId())
{
RendezvousCleanup(CHIP_NO_ERROR);
bzbarsky-apple marked this conversation as resolved.
Show resolved Hide resolved
// Let's release the device that's being paired, if pairing was successful,
// and the device is available on the operational network.
ReleaseCommissioneeDevice(mDeviceBeingCommissioned);
Expand Down
4 changes: 3 additions & 1 deletion src/controller/SetUpCodePairer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -146,7 +146,9 @@ void SetUpCodePairer::OnDiscoveredDeviceOverBle(BLE_CONNECTION_OBJECT connObj)

Transport::PeerAddress peerAddress = Transport::PeerAddress::BLE();
RendezvousParameters params = RendezvousParameters().SetPeerAddress(peerAddress).SetConnectionObject(connObj);
OnDeviceDiscovered(params);
// We don't have network credentials, so can't do the entire pairing flow. Just establish a PASE session to the
// device and let our consumer deal with the rest.
LogErrorOnFailure(mCommissioner->EstablishPASEConnection(mRemoteId, params.SetSetupPINCode(mSetUpPINCode)));
sagar-apple marked this conversation as resolved.
Show resolved Hide resolved
}

void SetUpCodePairer::OnDiscoveredDeviceOverBleSuccess(void * appState, BLE_CONNECTION_OBJECT connObj)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -628,76 +628,41 @@ - (void)retrieveAndSendWiFiCredentials
}
NSLog(@"New SSID: %@ Password: %@", networkSSID.text, networkPassword.text);

[strongSelf addOrUpdateWiFiNetwork:networkSSID.text password:networkPassword.text];
[strongSelf commissionWithSSID:networkSSID.text password:networkPassword.text];
}
}]];
[self presentViewController:alertController animated:YES completion:nil];
}

- (void)addOrUpdateWiFiNetwork:(NSString *)ssid password:(NSString *)password
- (void)commissionWithSSID:(NSString *)ssid password:(NSString *)password
{
CHIPDevice * chipDevice = CHIPGetDeviceBeingCommissioned();
if (chipDevice) {
self.cluster = [[CHIPNetworkCommissioning alloc] initWithDevice:chipDevice endpoint:0 queue:dispatch_get_main_queue()];
__auto_type * params = [[CHIPNetworkCommissioningClusterAddOrUpdateWiFiNetworkParams alloc] init];
params.ssid = [ssid dataUsingEncoding:NSUTF8StringEncoding];
params.credentials = [password dataUsingEncoding:NSUTF8StringEncoding];
params.breadcrumb = @(0);

__weak typeof(self) weakSelf = self;
[self->_cluster
addOrUpdateWiFiNetworkWithParams:params
completionHandler:^(CHIPNetworkCommissioningClusterNetworkConfigResponseParams * _Nullable response,
NSError * _Nullable error) {
// TODO: addWiFiNetworkWithParams
// returns status in its response,
// not via the NSError!
[weakSelf onAddNetworkResponse:error isWiFi:YES];
}];
} else {
NSLog(@"Status: Failed to find a device being commissioned");
}
}

- (void)addOrUpdateThreadNetwork:(NSData *)threadDataSet
{
CHIPDevice * chipDevice = CHIPGetDeviceBeingCommissioned();
if (chipDevice) {
self.cluster = [[CHIPNetworkCommissioning alloc] initWithDevice:chipDevice endpoint:0 queue:dispatch_get_main_queue()];
__auto_type * params = [[CHIPNetworkCommissioningClusterAddOrUpdateThreadNetworkParams alloc] init];
params.operationalDataset = threadDataSet;
params.breadcrumb = @(0);

__weak typeof(self) weakSelf = self;
[self->_cluster
addOrUpdateThreadNetworkWithParams:params
completionHandler:^(CHIPNetworkCommissioningClusterNetworkConfigResponseParams * _Nullable response,
NSError * _Nullable error) {
// TODO: addThreadNetworkWithParams
// returns status in its response,
// not via the NSError!
[weakSelf onAddNetworkResponse:error isWiFi:NO];
}];
} else {
NSLog(@"Status: Failed to find a device being commissioned");
NSError * error;
CHIPDeviceController * controller = [CHIPDeviceController sharedController];
// create commissioning params in ObjC. Pass those in here with network credentials.
// maybe this just becomes the new norm
CHIPCommissioningParameters * params = [[CHIPCommissioningParameters alloc] init];
sagar-apple marked this conversation as resolved.
Show resolved Hide resolved
params.wifiSSID = [ssid dataUsingEncoding:NSUTF8StringEncoding];
params.wifiCredentials = [password dataUsingEncoding:NSUTF8StringEncoding];

uint64_t deviceId = CHIPGetNextAvailableDeviceID() - 1;

if (![controller commissionDevice:deviceId commissioningParams:params error:&error]) {
NSLog(@"Failed to commission Device %llu, with error %@", deviceId, error);
}
}

- (void)onAddNetworkResponse:(NSError *)error isWiFi:(BOOL)isWiFi
- (void)onAddNetworkResponse:(NSError *)error
{
if (error != nil) {
NSLog(@"Error adding network: %@", error);
return;
}

__auto_type * params = [[CHIPNetworkCommissioningClusterConnectNetworkParams alloc] init];
if (isWiFi) {
NSString * ssid = CHIPGetDomainValueForKey(kCHIPToolDefaultsDomain, kNetworkSSIDDefaultsKey);
params.networkID = [ssid dataUsingEncoding:NSUTF8StringEncoding];
} else {
uint8_t tempThreadNetworkId[] = { 0x01, 0x23, 0x45, 0x67, 0x89, 0xab, 0xcd, 0xef };
params.networkID = [NSData dataWithBytes:tempThreadNetworkId length:sizeof(tempThreadNetworkId)];
}

NSString * ssid = CHIPGetDomainValueForKey(kCHIPToolDefaultsDomain, kNetworkSSIDDefaultsKey);
params.networkID = [ssid dataUsingEncoding:NSUTF8StringEncoding];
params.breadcrumb = @(0);

__weak typeof(self) weakSelf = self;
Expand All @@ -721,7 +686,7 @@ - (void)onConnectNetworkResponse:(NSError *)error
[controller updateDevice:deviceId fabricId:0];
}

- (void)onAddressUpdated:(NSError * _Nullable)error
- (void)onCommissioningComplete:(NSError * _Nullable)error
{
if (error != nil) {
NSLog(@"Error retrieving device informations over Mdns: %@", error);
Expand Down
8 changes: 8 additions & 0 deletions src/darwin/Framework/CHIP.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,9 @@
998F286D26D55E10001846C6 /* CHIPKeypair.h in Headers */ = {isa = PBXBuildFile; fileRef = 998F286C26D55E10001846C6 /* CHIPKeypair.h */; settings = {ATTRIBUTES = (Public, ); }; };
998F286F26D55EC5001846C6 /* CHIPP256KeypairBridge.h in Headers */ = {isa = PBXBuildFile; fileRef = 998F286E26D55EC5001846C6 /* CHIPP256KeypairBridge.h */; };
998F287126D56940001846C6 /* CHIPP256KeypairBridge.mm in Sources */ = {isa = PBXBuildFile; fileRef = 998F287026D56940001846C6 /* CHIPP256KeypairBridge.mm */; };
99AECC802798A57F00B6355B /* CHIPCommissioningParameters.m in Sources */ = {isa = PBXBuildFile; fileRef = 99AECC7F2798A57E00B6355B /* CHIPCommissioningParameters.m */; };
99C65E10267282F1003402F6 /* CHIPControllerTests.m in Sources */ = {isa = PBXBuildFile; fileRef = 99C65E0F267282F1003402F6 /* CHIPControllerTests.m */; };
99D466E12798936D0089A18F /* CHIPCommissioningParameters.h in Headers */ = {isa = PBXBuildFile; fileRef = 99D466E02798936D0089A18F /* CHIPCommissioningParameters.h */; settings = {ATTRIBUTES = (Public, ); }; };
B20252972459E34F00F97062 /* CHIP.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = B202528D2459E34F00F97062 /* CHIP.framework */; };
B289D4212639C0D300D4E314 /* CHIPOnboardingPayloadParser.h in Headers */ = {isa = PBXBuildFile; fileRef = B289D41F2639C0D300D4E314 /* CHIPOnboardingPayloadParser.h */; settings = {ATTRIBUTES = (Public, ); }; };
B289D4222639C0D300D4E314 /* CHIPOnboardingPayloadParser.m in Sources */ = {isa = PBXBuildFile; fileRef = B289D4202639C0D300D4E314 /* CHIPOnboardingPayloadParser.m */; };
Expand Down Expand Up @@ -157,7 +159,9 @@
998F286C26D55E10001846C6 /* CHIPKeypair.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = CHIPKeypair.h; sourceTree = "<group>"; };
998F286E26D55EC5001846C6 /* CHIPP256KeypairBridge.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = CHIPP256KeypairBridge.h; sourceTree = "<group>"; };
998F287026D56940001846C6 /* CHIPP256KeypairBridge.mm */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.cpp.objcpp; path = CHIPP256KeypairBridge.mm; sourceTree = "<group>"; };
99AECC7F2798A57E00B6355B /* CHIPCommissioningParameters.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = CHIPCommissioningParameters.m; sourceTree = "<group>"; };
99C65E0F267282F1003402F6 /* CHIPControllerTests.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = CHIPControllerTests.m; sourceTree = "<group>"; };
99D466E02798936D0089A18F /* CHIPCommissioningParameters.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = CHIPCommissioningParameters.h; sourceTree = "<group>"; };
B202528D2459E34F00F97062 /* CHIP.framework */ = {isa = PBXFileReference; explicitFileType = wrapper.framework; includeInIndex = 0; path = CHIP.framework; sourceTree = BUILT_PRODUCTS_DIR; };
B20252912459E34F00F97062 /* Info.plist */ = {isa = PBXFileReference; lastKnownFileType = text.plist.xml; path = Info.plist; sourceTree = "<group>"; };
B20252962459E34F00F97062 /* CHIPTests.xctest */ = {isa = PBXFileReference; explicitFileType = wrapper.cfbundle; includeInIndex = 0; path = CHIPTests.xctest; sourceTree = BUILT_PRODUCTS_DIR; };
Expand Down Expand Up @@ -285,6 +289,8 @@
997DED172695344800975E97 /* CHIPThreadOperationalDataset.h */,
2C8C8FBD253E0C2100797F05 /* CHIPPersistentStorageDelegateBridge.h */,
2C8C8FBF253E0C2100797F05 /* CHIPPersistentStorageDelegateBridge.mm */,
99D466E02798936D0089A18F /* CHIPCommissioningParameters.h */,
99AECC7F2798A57E00B6355B /* CHIPCommissioningParameters.m */,
2CB7163E252F731E0026E2BB /* CHIPDevicePairingDelegate.h */,
2CB71638252E8A7B0026E2BB /* CHIPDevicePairingDelegateBridge.h */,
2CB71639252E8A7B0026E2BB /* CHIPDevicePairingDelegateBridge.mm */,
Expand Down Expand Up @@ -345,6 +351,7 @@
2CB7163B252E8A7B0026E2BB /* CHIPDevicePairingDelegateBridge.h in Headers */,
1E16A90326B98AF100683C53 /* CHIPTestClustersObjc.h in Headers */,
2C1B027B2641DB4E00780EF1 /* CHIPOperationalCredentialsDelegate.h in Headers */,
99D466E12798936D0089A18F /* CHIPCommissioningParameters.h in Headers */,
B289D4212639C0D300D4E314 /* CHIPOnboardingPayloadParser.h in Headers */,
513DDB862761F69300DAA01A /* CHIPAttributeTLVValueDecoder_Internal.h in Headers */,
2CB7163F252F731E0026E2BB /* CHIPDevicePairingDelegate.h in Headers */,
Expand Down Expand Up @@ -492,6 +499,7 @@
buildActionMask = 2147483647;
files = (
2C8C8FC2253E0C2100797F05 /* CHIPPersistentStorageDelegateBridge.mm in Sources */,
99AECC802798A57F00B6355B /* CHIPCommissioningParameters.m in Sources */,
2CB7163C252E8A7C0026E2BB /* CHIPDevicePairingDelegateBridge.mm in Sources */,
997DED162695343400975E97 /* CHIPThreadOperationalDataset.mm in Sources */,
998F287126D56940001846C6 /* CHIPP256KeypairBridge.mm in Sources */,
Expand Down
1 change: 1 addition & 0 deletions src/darwin/Framework/CHIP/CHIP.h
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
#import <CHIP/CHIPCluster.h>
#import <CHIP/CHIPClustersObjc.h>
#import <CHIP/CHIPCommandPayloadsObjc.h>
#import <CHIP/CHIPCommissioningParameters.h>
#import <CHIP/CHIPDevice.h>
#import <CHIP/CHIPDeviceController.h>
#import <CHIP/CHIPDevicePairingDelegate.h>
Expand Down
51 changes: 51 additions & 0 deletions src/darwin/Framework/CHIP/CHIPCommissioningParameters.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
/**
*
* Copyright (c) 2022 Project CHIP Authors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

#import <Foundation/Foundation.h>

NS_ASSUME_NONNULL_BEGIN

/**
* The class definition for the CHIPCommissioningParameters
*
*/
@interface CHIPCommissioningParameters : NSObject

/**
* The CSRNonce
*/
@property (nonatomic, nullable, copy, readwrite) NSData * CSRNonce;
/**
* The AttestationNonce
*/
@property (nonatomic, nullable, copy, readwrite) NSData * attestationNonce;
/**
* The Wi-Fi SSID
*/
@property (nonatomic, nullable, copy, readwrite) NSData * wifiSSID;
/**
* The Wi-Fi Credentials
*/
@property (nonatomic, nullable, copy, readwrite) NSData * wifiCredentials;
/**
* The Thread operational dataset
*/
@property (nonatomic, nullable, copy, readwrite) NSData * threadOperationalDataset;

@end

NS_ASSUME_NONNULL_END
26 changes: 26 additions & 0 deletions src/darwin/Framework/CHIP/CHIPCommissioningParameters.m
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
/**
*
* Copyright (c) 2022 Project CHIP Authors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

#import "CHIPCommissioningParameters.h"

NS_ASSUME_NONNULL_BEGIN

@implementation CHIPCommissioningParameters : NSObject

sagar-apple marked this conversation as resolved.
Show resolved Hide resolved
@end

NS_ASSUME_NONNULL_END
4 changes: 4 additions & 0 deletions src/darwin/Framework/CHIP/CHIPDeviceController.h
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ NS_ASSUME_NONNULL_BEGIN

typedef void (^CHIPDeviceConnectionCallback)(CHIPDevice * _Nullable device, NSError * _Nullable error);

@class CHIPCommissioningParameters;
@protocol CHIPDevicePairingDelegate;
@protocol CHIPPersistentStorageDelegate;
@protocol CHIPKeypair;
Expand All @@ -49,6 +50,9 @@ typedef void (^CHIPDeviceConnectionCallback)(CHIPDevice * _Nullable device, NSEr
error:(NSError * __autoreleasing *)error;

- (BOOL)pairDevice:(uint64_t)deviceID onboardingPayload:(NSString *)onboardingPayload error:(NSError * __autoreleasing *)error;
- (BOOL)commissionDevice:(uint64_t)deviceId
commissioningParams:(CHIPCommissioningParameters *)commissioningParams
error:(NSError * __autoreleasing *)error;

- (void)setListenPort:(uint16_t)port;
- (BOOL)unpairDevice:(uint64_t)deviceID error:(NSError * __autoreleasing *)error;
Expand Down
42 changes: 42 additions & 0 deletions src/darwin/Framework/CHIP/CHIPDeviceController.mm
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
*/
#import "CHIPDeviceController.h"

#import "CHIPCommissioningParameters.h"
#import "CHIPDevicePairingDelegateBridge.h"
#import "CHIPDevice_Internal.h"
#import "CHIPError_Internal.h"
Expand Down Expand Up @@ -352,6 +353,47 @@ - (BOOL)pairDevice:(uint64_t)deviceID onboardingPayload:(NSString *)onboardingPa
return success;
}

- (BOOL)commissionDevice:(uint64_t)deviceId
commissioningParams:(CHIPCommissioningParameters *)commissioningParams
error:(NSError * __autoreleasing *)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;
}
dispatch_sync(_chipWorkQueue, ^{
if ([self isRunning]) {
chip::Controller::CommissioningParameters params;
if (commissioningParams.CSRNonce) {
params.SetCSRNonce(
chip::ByteSpan((uint8_t *) commissioningParams.CSRNonce.bytes, commissioningParams.CSRNonce.length));
sagar-apple marked this conversation as resolved.
Show resolved Hide resolved
}
if (commissioningParams.attestationNonce) {
params.SetAttestationNonce(chip::ByteSpan(
(uint8_t *) commissioningParams.attestationNonce.bytes, commissioningParams.attestationNonce.length));
}
if (commissioningParams.threadOperationalDataset) {
params.SetThreadOperationalDataset(chip::ByteSpan((uint8_t *) commissioningParams.threadOperationalDataset.bytes,
commissioningParams.threadOperationalDataset.length));
}
if (commissioningParams.wifiSSID && commissioningParams.wifiCredentials) {
chip::ByteSpan ssid((uint8_t *) commissioningParams.wifiSSID.bytes, commissioningParams.wifiSSID.length);
chip::ByteSpan credentials(
(uint8_t *) commissioningParams.wifiCredentials.bytes, commissioningParams.wifiCredentials.length);
chip::Controller::WiFiCredentials wifiCreds(ssid, credentials);
params.SetWiFiCredentials(wifiCreds);
}

_operationalCredentialsDelegate->SetDeviceID(deviceId);
errorCode = self.cppCommissioner->Commission(deviceId, params);
}
success = ![self checkForError:errorCode logMsg:kErrorPairDevice error:error];
});
return success;
}

- (BOOL)unpairDevice:(uint64_t)deviceID error:(NSError * __autoreleasing *)error
{
__block CHIP_ERROR errorCode = CHIP_ERROR_INCORRECT_STATE;
Expand Down
8 changes: 4 additions & 4 deletions src/darwin/Framework/CHIP/CHIPDevicePairingDelegate.h
Original file line number Diff line number Diff line change
Expand Up @@ -45,16 +45,16 @@ typedef NS_ENUM(NSUInteger, CHIPPairingStatus) {
- (void)onPairingComplete:(nullable NSError *)error;

/**
* Notify the delegate when pairing is deleted
* Notify the delegate when commissioning is completed
*
*/
- (void)onPairingDeleted:(nullable NSError *)error;
- (void)onCommissioningComplete:(nullable NSError *)error;

/**
* Notify the delegate when address is updated
* Notify the delegate when pairing is deleted
*
*/
- (void)onAddressUpdated:(nullable NSError *)error;
- (void)onPairingDeleted:(nullable NSError *)error;

@end

Expand Down
2 changes: 2 additions & 0 deletions src/darwin/Framework/CHIP/CHIPDevicePairingDelegateBridge.h
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,8 @@ class CHIPDevicePairingDelegateBridge : public chip::Controller::DevicePairingDe

void OnPairingDeleted(CHIP_ERROR error) override;

void OnCommissioningComplete(chip::NodeId deviceId, CHIP_ERROR error) override;

void OnAddressUpdateComplete(chip::NodeId nodeId, CHIP_ERROR error) override;

private:
Expand Down
14 changes: 10 additions & 4 deletions src/darwin/Framework/CHIP/CHIPDevicePairingDelegateBridge.mm
Original file line number Diff line number Diff line change
Expand Up @@ -95,17 +95,23 @@
}
}

void CHIPDevicePairingDelegateBridge::OnAddressUpdateComplete(chip::NodeId nodeId, CHIP_ERROR error)
void CHIPDevicePairingDelegateBridge::OnCommissioningComplete(chip::NodeId nodeId, CHIP_ERROR error)
{
NSLog(@"OnAddressUpdateComplete. Status %s", chip::ErrorStr(error));
NSLog(@"DevicePairingDelegate Commissioning complete. NodeId %llu Status %s", nodeId, chip::ErrorStr(error));

id<CHIPDevicePairingDelegate> strongDelegate = mDelegate;
if ([strongDelegate respondsToSelector:@selector(onAddressUpdated:)]) {
if ([strongDelegate respondsToSelector:@selector(onCommissioningComplete:)]) {
if (strongDelegate && mQueue) {
dispatch_async(mQueue, ^{
NSError * nsError = [CHIPError errorForCHIPErrorCode:error];
[strongDelegate onAddressUpdated:nsError];
[strongDelegate onCommissioningComplete:nsError];
});
}
}
}

void CHIPDevicePairingDelegateBridge::OnAddressUpdateComplete(chip::NodeId nodeId, CHIP_ERROR error)
{
// Todo, is there any benefit of exposing this anymore?
NSLog(@"OnAddressUpdateComplete. Status %s", chip::ErrorStr(error));
}
Loading