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..f6567256dded04 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 setMParams:params]; + [pairing setMCommissioner: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..76d9cf966ba60e 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 * mCommissioner; +@property CHIPCommissioningParameters * mParams; + - (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..6adf7684bdc58d 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); + [_mCommissioner commissionDevice:_deviceID + commissioningParams:_mParams + 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 540b36129ff1ed..7d8d34e6e5271e 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",