diff --git a/examples/chip-tool-darwin/commands/pairing/Commands.h b/examples/chip-tool-darwin/commands/pairing/Commands.h index dcfd6fc789c7cb..4e88b1ea3a502a 100644 --- a/examples/chip-tool-darwin/commands/pairing/Commands.h +++ b/examples/chip-tool-darwin/commands/pairing/Commands.h @@ -23,19 +23,31 @@ 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) @@ -43,9 +55,8 @@ void registerCommandsPairing(Commands & commands) const char * clusterName = "Pairing"; commands_list clusterCommands = { - make_unique(), - make_unique(), - make_unique(), + 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..92dcff9b3cf072 100644 --- a/examples/chip-tool-darwin/commands/pairing/PairingCommandBridge.h +++ b/examples/chip-tool-darwin/commands/pairing/PairingCommandBridge.h @@ -25,15 +25,38 @@ 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 +72,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,10 +85,15 @@ 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; diff --git a/examples/chip-tool-darwin/commands/pairing/PairingCommandBridge.mm b/examples/chip-tool-darwin/commands/pairing/PairingCommandBridge.mm index 31c2e73fffbfbd..3b90ece6bbdf72 100644 --- a/examples/chip-tool-darwin/commands/pairing/PairingCommandBridge.mm +++ b/examples/chip-tool-darwin/commands/pairing/PairingCommandBridge.mm @@ -16,7 +16,10 @@ * */ +#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,26 @@ 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..b35b22737a3b20 100644 --- a/examples/chip-tool-darwin/commands/pairing/PairingDelegateBridge.mm +++ b/examples/chip-tool-darwin/commands/pairing/PairingDelegateBridge.mm @@ -17,6 +17,7 @@ */ #include "PairingDelegateBridge.h" +#import #import @interface CHIPToolPairingDelegate () @@ -41,10 +42,19 @@ - (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 198fdf371da18a..31ef2e0d94ecaa 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", "CHIPControllerAccessControl.h", "CHIPControllerAccessControl.mm", "CHIPDevice.h",