From 218db4cfa8e47f85b8f58cd3cacf30f6d478e8e0 Mon Sep 17 00:00:00 2001 From: Joshua Villasenor Date: Fri, 11 Mar 2022 07:28:28 -0800 Subject: [PATCH] Add BLE and thread pairing capabilities to chip-tool-darwin. --- .../commands/pairing/Commands.h | 30 +++++++++++++-- .../commands/pairing/PairingCommandBridge.h | 36 +++++++++++++++++- .../commands/pairing/PairingCommandBridge.mm | 38 +++++++++++++++++-- .../commands/pairing/PairingDelegateBridge.h | 3 ++ .../commands/pairing/PairingDelegateBridge.mm | 16 +++++++- src/darwin/Framework/CHIP/BUILD.gn | 1 + 6 files changed, 114 insertions(+), 10 deletions(-) diff --git a/examples/chip-tool-darwin/commands/pairing/Commands.h b/examples/chip-tool-darwin/commands/pairing/Commands.h index dcfd6fc789c7cb..820d424e1761b0 100644 --- a/examples/chip-tool-darwin/commands/pairing/Commands.h +++ b/examples/chip-tool-darwin/commands/pairing/Commands.h @@ -23,19 +23,41 @@ class PairQRCode : public PairingCommandBridge { public: - PairQRCode() : PairingCommandBridge("qrcode", PairingMode::QRCode) {} + PairQRCode() : + PairingCommandBridge("qrcode", PairingMode::QRCode, PairingNetworkType::None) + {} }; class PairManualCode : public PairingCommandBridge { public: - PairManualCode() : PairingCommandBridge("manualcode", PairingMode::ManualCode) {} + PairManualCode() : + PairingCommandBridge("manualcode", PairingMode::ManualCode, PairingNetworkType::None) + {} }; class PairWithIPAddress : public PairingCommandBridge { public: - PairWithIPAddress() : PairingCommandBridge("ethernet", PairingMode::Ethernet) {} + PairWithIPAddress() : + PairingCommandBridge("ethernet", PairingMode::Ethernet, PairingNetworkType::Ethernet) + {} +}; + +class PairBleWiFi : public PairingCommandBridge +{ +public: + PairBleWiFi() : + PairingCommandBridge("ble-wifi", PairingMode::Ble, PairingNetworkType::WiFi) + {} +}; + +class PairBleThread : public PairingCommandBridge +{ +public: + PairBleThread() : + PairingCommandBridge("ble-thread", PairingMode::Ble, PairingNetworkType::Thread) + {} }; void registerCommandsPairing(Commands & commands) @@ -46,6 +68,8 @@ void registerCommandsPairing(Commands & commands) make_unique(), make_unique(), make_unique(), + make_unique(), + make_unique(), }; commands.Register(clusterName, clusterCommands); diff --git a/examples/chip-tool-darwin/commands/pairing/PairingCommandBridge.h b/examples/chip-tool-darwin/commands/pairing/PairingCommandBridge.h index 917d7e2bf9521d..dd9704f85a65e2 100644 --- a/examples/chip-tool-darwin/commands/pairing/PairingCommandBridge.h +++ b/examples/chip-tool-darwin/commands/pairing/PairingCommandBridge.h @@ -25,15 +25,37 @@ enum class PairingMode { QRCode, ManualCode, - Ethernet + Ethernet, + Ble, +}; + +enum class PairingNetworkType +{ + None, + WiFi, + Thread, + Ethernet, }; class PairingCommandBridge : public CHIPCommandBridge { public: - PairingCommandBridge(const char * commandName, PairingMode mode) : CHIPCommandBridge(commandName), mPairingMode(mode) + PairingCommandBridge(const char * commandName, PairingMode mode, PairingNetworkType networkType) : CHIPCommandBridge(commandName), mPairingMode(mode), mNetworkType(networkType) { AddArgument("node-id", 0, UINT64_MAX, &mNodeId); + switch (networkType) + { + case PairingNetworkType::None: + case PairingNetworkType::Ethernet: + break; + case PairingNetworkType::WiFi: + AddArgument("ssid", &mSSID); + AddArgument("password", &mPassword); + break; + case PairingNetworkType::Thread: + AddArgument("operationalDataset", &mOperationalDataset); + break; + } switch (mode) { @@ -49,6 +71,10 @@ class PairingCommandBridge : public CHIPCommandBridge AddArgument("device-remote-ip", &ipAddress); AddArgument("device-remote-port", 0, UINT16_MAX, &mRemotePort); break; + case PairingMode::Ble: + AddArgument("setup-pin-code", 0, 134217727, &mSetupPINCode); + AddArgument("discriminator", 0, 4096, &mDiscriminator); + break; } } @@ -58,14 +84,20 @@ class PairingCommandBridge : public CHIPCommandBridge private: void PairWithCode(NSError * __autoreleasing * error); + void PairWithPayload(NSError * __autoreleasing * error); void PairWithIPAddress(NSError * __autoreleasing * error); void SetUpPairingDelegate(); const PairingMode mPairingMode; + const PairingNetworkType mNetworkType; + chip::ByteSpan mOperationalDataset; + chip::ByteSpan mSSID; + chip::ByteSpan mPassword; chip::NodeId mNodeId; uint16_t mRemotePort; uint16_t mDiscriminator; uint32_t mSetupPINCode; char * mOnboardingPayload; char * ipAddress; + }; diff --git a/examples/chip-tool-darwin/commands/pairing/PairingCommandBridge.mm b/examples/chip-tool-darwin/commands/pairing/PairingCommandBridge.mm index 31c2e73fffbfbd..921e461fa9de2b 100644 --- a/examples/chip-tool-darwin/commands/pairing/PairingCommandBridge.mm +++ b/examples/chip-tool-darwin/commands/pairing/PairingCommandBridge.mm @@ -17,6 +17,9 @@ */ #import +#import +#import +#import #include "../common/CHIPCommandBridge.h" #include "PairingCommandBridge.h" @@ -31,9 +34,25 @@ { dispatch_queue_t callbackQueue = dispatch_queue_create("com.chip.pairing", DISPATCH_QUEUE_SERIAL); CHIPToolPairingDelegate * pairing = [[CHIPToolPairingDelegate alloc] init]; + CHIPCommissioningParameters * params = [[CHIPCommissioningParameters alloc] init]; - pairing.deviceID = mNodeId; - pairing.commandBridge = this; + [pairing setDeviceID:mNodeId]; + switch (mNetworkType) { + case PairingNetworkType::None: + case PairingNetworkType::Ethernet: + break; + case PairingNetworkType::WiFi: + [params setWifiSSID:[NSData dataWithBytes:mSSID.data() length:mSSID.size()]]; + [params setWifiCredentials:[NSData dataWithBytes:mPassword.data() length:mPassword.size()]]; + break; + case PairingNetworkType::Thread: + [params setThreadOperationalDataset:[NSData dataWithBytes:mOperationalDataset.data() length:mOperationalDataset.size()]]; + break; + } + + [pairing setCommandBridge:this]; + [pairing setParams:params]; + [pairing setCommissioner:CurrentCommissioner()]; [CurrentCommissioner() setPairingDelegate:pairing queue:callbackQueue]; } @@ -44,17 +63,30 @@ switch (mPairingMode) { case PairingMode::QRCode: case PairingMode::ManualCode: - PairWithCode(&error); + PairWithPayload(&error); break; case PairingMode::Ethernet: PairWithIPAddress(&error); break; + case PairingMode::Ble: + PairWithCode(&error); + break; } return [CHIPError errorToCHIPErrorCode:error]; } void PairingCommandBridge::PairWithCode(NSError * __autoreleasing * error) +{ + SetUpPairingDelegate(); + [CurrentCommissioner() pairDevice:mNodeId + discriminator:mDiscriminator + setupPINCode:mSetupPINCode + error:error]; + +} + +void PairingCommandBridge::PairWithPayload(NSError * __autoreleasing * error) { NSString * payload = [NSString stringWithUTF8String:mOnboardingPayload]; diff --git a/examples/chip-tool-darwin/commands/pairing/PairingDelegateBridge.h b/examples/chip-tool-darwin/commands/pairing/PairingDelegateBridge.h index fbe026e398f15b..641ac77e79501a 100644 --- a/examples/chip-tool-darwin/commands/pairing/PairingDelegateBridge.h +++ b/examples/chip-tool-darwin/commands/pairing/PairingDelegateBridge.h @@ -21,6 +21,9 @@ @interface CHIPToolPairingDelegate : NSObject @property PairingCommandBridge * commandBridge; @property chip::NodeId deviceID; +@property CHIPDeviceController * commissioner; +@property CHIPCommissioningParameters * params; + - (void)onPairingComplete:(NSError *)error; - (void)onPairingDeleted:(NSError *)error; - (void)onCommissioningComplete:(NSError *)error; diff --git a/examples/chip-tool-darwin/commands/pairing/PairingDelegateBridge.mm b/examples/chip-tool-darwin/commands/pairing/PairingDelegateBridge.mm index 4a52f65ce10455..ce2becffc4d33b 100644 --- a/examples/chip-tool-darwin/commands/pairing/PairingDelegateBridge.mm +++ b/examples/chip-tool-darwin/commands/pairing/PairingDelegateBridge.mm @@ -18,6 +18,7 @@ #include "PairingDelegateBridge.h" #import +#import @interface CHIPToolPairingDelegate () @end @@ -41,10 +42,21 @@ - (void)onStatusUpdate:(CHIPPairingStatus)status - (void)onPairingComplete:(NSError *)error { + NSError * __block commissionError; CHIP_ERROR err = [CHIPError errorToCHIPErrorCode:error]; + if (err != CHIP_NO_ERROR) { + _commandBridge->SetCommandExitStatus(err); + return; + } ChipLogProgress(chipTool, "Pairing Complete: %s", chip::ErrorStr(err)); - - _commandBridge->SetCommandExitStatus(err); + [_commissioner commissionDevice:_deviceID + commissioningParams:_params + error:&commissionError]; + err = [CHIPError errorToCHIPErrorCode:commissionError]; + if (err != CHIP_NO_ERROR) { + _commandBridge->SetCommandExitStatus(err); + return; + } } - (void)onPairingDeleted:(NSError *)error diff --git a/src/darwin/Framework/CHIP/BUILD.gn b/src/darwin/Framework/CHIP/BUILD.gn index d823e741f78a6b..f1a5257affde22 100644 --- a/src/darwin/Framework/CHIP/BUILD.gn +++ b/src/darwin/Framework/CHIP/BUILD.gn @@ -30,6 +30,7 @@ static_library("framework") { sources = [ "CHIP.h", "CHIPCluster.mm", + "CHIPCommissioningParameters.m", "CHIPDevice.h", "CHIPDevice.mm", "CHIPDeviceConnectionBridge.mm",