From 4434640c4090d7215abee869bd0d07db7d6bd49c Mon Sep 17 00:00:00 2001 From: Vivien Nicolas Date: Thu, 8 Jul 2021 15:22:10 +0200 Subject: [PATCH] [ChipTool] Add pairing qrcode/manualcode option (#8168) --- .../chip-tool/commands/pairing/Commands.h | 17 +++++++-- .../commands/pairing/PairingCommand.cpp | 36 +++++++++++++++++++ .../commands/pairing/PairingCommand.h | 12 +++++++ 3 files changed, 63 insertions(+), 2 deletions(-) diff --git a/examples/chip-tool/commands/pairing/Commands.h b/examples/chip-tool/commands/pairing/Commands.h index 70008a924a0a8c..ac30f08598d330 100644 --- a/examples/chip-tool/commands/pairing/Commands.h +++ b/examples/chip-tool/commands/pairing/Commands.h @@ -32,6 +32,18 @@ class PairBypass : public PairingCommand PairBypass() : PairingCommand("bypass", PairingMode::Bypass, PairingNetworkType::None) {} }; +class PairQRCode : public PairingCommand +{ +public: + PairQRCode() : PairingCommand("qrcode", PairingMode::QRCode, PairingNetworkType::None) {} +}; + +class PairManualCode : public PairingCommand +{ +public: + PairManualCode() : PairingCommand("manualcode", PairingMode::ManualCode, PairingNetworkType::None) {} +}; + class PairOnNetwork : public PairingCommand { public: @@ -67,8 +79,9 @@ 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(), 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/commands/pairing/PairingCommand.cpp b/examples/chip-tool/commands/pairing/PairingCommand.cpp index f21554299fcd45..ec9772c1bce42e 100644 --- a/examples/chip-tool/commands/pairing/PairingCommand.cpp +++ b/examples/chip-tool/commands/pairing/PairingCommand.cpp @@ -20,6 +20,9 @@ #include "platform/PlatformManager.h" #include +#include +#include + using namespace ::chip; constexpr uint64_t kBreadcrumb = 0; @@ -56,6 +59,12 @@ CHIP_ERROR PairingCommand::RunInternal(NodeId remoteId) case PairingMode::Bypass: err = PairWithoutSecurity(remoteId, PeerAddress::UDP(mRemoteAddr.address, mRemotePort)); break; + case PairingMode::QRCode: + err = PairWithQRCode(remoteId); + break; + case PairingMode::ManualCode: + err = PairWithManualCode(remoteId); + break; case PairingMode::Ble: err = Pair(remoteId, PeerAddress::BLE()); break; @@ -71,6 +80,33 @@ CHIP_ERROR PairingCommand::RunInternal(NodeId remoteId) return err; } +CHIP_ERROR PairingCommand::PairWithQRCode(NodeId remoteId) +{ + SetupPayload payload; + ReturnErrorOnFailure(QRCodeSetupPayloadParser(mOnboardingPayload).populatePayload(payload)); + return PairWithCode(remoteId, payload); +} + +CHIP_ERROR PairingCommand::PairWithManualCode(NodeId remoteId) +{ + SetupPayload payload; + ReturnErrorOnFailure(ManualSetupPayloadParser(mOnboardingPayload).populatePayload(payload)); + return PairWithCode(remoteId, payload); +} + +CHIP_ERROR PairingCommand::PairWithCode(NodeId remoteId, SetupPayload payload) +{ + chip::RendezvousInformationFlags rendezvousInformation = payload.rendezvousInformation; + ReturnErrorCodeIf(rendezvousInformation != RendezvousInformationFlag::kBLE, CHIP_ERROR_INVALID_ARGUMENT); + + RendezvousParameters params = RendezvousParameters() + .SetSetupPINCode(payload.setUpPINCode) + .SetDiscriminator(payload.discriminator) + .SetPeerAddress(PeerAddress::BLE()); + + return GetExecContext()->commissioner->PairDevice(remoteId, params); +} + CHIP_ERROR PairingCommand::Pair(NodeId remoteId, PeerAddress address) { RendezvousParameters params = diff --git a/examples/chip-tool/commands/pairing/PairingCommand.h b/examples/chip-tool/commands/pairing/PairingCommand.h index 092fbfd806b4de..ac2353f041f280 100644 --- a/examples/chip-tool/commands/pairing/PairingCommand.h +++ b/examples/chip-tool/commands/pairing/PairingCommand.h @@ -24,12 +24,15 @@ #include "gen/CHIPClusters.h" #include +#include #include enum class PairingMode { None, Bypass, + QRCode, + ManualCode, Ble, SoftAP, Ethernet, @@ -74,6 +77,11 @@ class PairingCommand : public Command, AddArgument("device-remote-ip", &mRemoteAddr); AddArgument("device-remote-port", 0, UINT16_MAX, &mRemotePort); break; + case PairingMode::QRCode: + case PairingMode::ManualCode: + AddArgument("fabric-id", 0, UINT64_MAX, &mFabricId); + AddArgument("payload", &mOnboardingPayload); + break; case PairingMode::Ble: AddArgument("fabric-id", 0, UINT64_MAX, &mFabricId); AddArgument("setup-pin-code", 0, 134217727, &mSetupPINCode); @@ -118,6 +126,9 @@ class PairingCommand : public Command, private: CHIP_ERROR RunInternal(NodeId remoteId); CHIP_ERROR Pair(NodeId remoteId, PeerAddress address); + CHIP_ERROR PairWithQRCode(NodeId remoteId); + CHIP_ERROR PairWithManualCode(NodeId remoteId); + CHIP_ERROR PairWithCode(NodeId remoteId, chip::SetupPayload payload); CHIP_ERROR PairWithoutSecurity(NodeId remoteId, PeerAddress address); CHIP_ERROR Unpair(NodeId remoteId); @@ -140,6 +151,7 @@ class PairingCommand : public Command, chip::ByteSpan mOperationalDataset; chip::ByteSpan mSSID; chip::ByteSpan mPassword; + char * mOnboardingPayload; chip::Callback::Callback * mOnAddThreadNetworkCallback = nullptr; chip::Callback::Callback * mOnAddWiFiNetworkCallback = nullptr;