Skip to content

Commit

Permalink
[ChipTool] Add pairing qrcode/manualcode option (#8168)
Browse files Browse the repository at this point in the history
  • Loading branch information
vivien-apple authored and pull[bot] committed Sep 1, 2021
1 parent 266acac commit 4434640
Show file tree
Hide file tree
Showing 3 changed files with 63 additions and 2 deletions.
17 changes: 15 additions & 2 deletions examples/chip-tool/commands/pairing/Commands.h
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down Expand Up @@ -67,8 +79,9 @@ void registerCommandsPairing(Commands & commands)
const char * clusterName = "Pairing";

commands_list clusterCommands = {
make_unique<Unpair>(), make_unique<PairBypass>(), make_unique<PairBleWiFi>(), make_unique<PairBleThread>(),
make_unique<PairSoftAP>(), make_unique<Ethernet>(), make_unique<PairOnNetwork>(),
make_unique<Unpair>(), make_unique<PairBypass>(), make_unique<PairQRCode>(),
make_unique<PairManualCode>(), make_unique<PairBleWiFi>(), make_unique<PairBleThread>(),
make_unique<PairSoftAP>(), make_unique<Ethernet>(), make_unique<PairOnNetwork>(),
};

commands.Register(clusterName, clusterCommands);
Expand Down
36 changes: 36 additions & 0 deletions examples/chip-tool/commands/pairing/PairingCommand.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,9 @@
#include "platform/PlatformManager.h"
#include <lib/core/CHIPSafeCasts.h>

#include <setup_payload/ManualSetupPayloadParser.h>
#include <setup_payload/QRCodeSetupPayloadParser.h>

using namespace ::chip;

constexpr uint64_t kBreadcrumb = 0;
Expand Down Expand Up @@ -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;
Expand All @@ -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 =
Expand Down
12 changes: 12 additions & 0 deletions examples/chip-tool/commands/pairing/PairingCommand.h
Original file line number Diff line number Diff line change
Expand Up @@ -24,12 +24,15 @@
#include "gen/CHIPClusters.h"

#include <controller/ExampleOperationalCredentialsIssuer.h>
#include <setup_payload/SetupPayload.h>
#include <support/Span.h>

enum class PairingMode
{
None,
Bypass,
QRCode,
ManualCode,
Ble,
SoftAP,
Ethernet,
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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);

Expand All @@ -140,6 +151,7 @@ class PairingCommand : public Command,
chip::ByteSpan mOperationalDataset;
chip::ByteSpan mSSID;
chip::ByteSpan mPassword;
char * mOnboardingPayload;

chip::Callback::Callback<NetworkCommissioningClusterAddThreadNetworkResponseCallback> * mOnAddThreadNetworkCallback = nullptr;
chip::Callback::Callback<NetworkCommissioningClusterAddWiFiNetworkResponseCallback> * mOnAddWiFiNetworkCallback = nullptr;
Expand Down

0 comments on commit 4434640

Please sign in to comment.