Skip to content

Commit

Permalink
chip-tool: do not hardcode Thread network id (#8245)
Browse files Browse the repository at this point in the history
* chip-tool: do not hardcode Thread network id

* Apply review suggestions

* fix merge conflict
  • Loading branch information
bluebin14 authored Jul 9, 2021
1 parent dce4293 commit 8ccbbb5
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 4 deletions.
32 changes: 28 additions & 4 deletions examples/chip-tool/commands/pairing/PairingCommand.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,8 @@

using namespace ::chip;

constexpr uint64_t kBreadcrumb = 0;
constexpr uint32_t kTimeoutMs = 6000;
constexpr uint8_t kTemporaryThreadNetworkId[] = { 0x01, 0x23, 0x45, 0x67, 0x89, 0xab, 0xcd, 0xef };
constexpr uint64_t kBreadcrumb = 0;
constexpr uint32_t kTimeoutMs = 6000;

CHIP_ERROR PairingCommand::Run()
{
Expand Down Expand Up @@ -258,6 +257,26 @@ CHIP_ERROR PairingCommand::AddWiFiNetwork()
return mCluster.AddWiFiNetwork(successCallback, failureCallback, mSSID, mPassword, kBreadcrumb, kTimeoutMs);
}

chip::ByteSpan PairingCommand::GetThreadNetworkId()
{
// For Thread devices the networkId is the extendedPanId and it is
// part of the dataset defined by OpenThread

Thread::OperationalDataset dataset;

if (dataset.Init(mOperationalDataset) != CHIP_NO_ERROR)
{
return ByteSpan();
}

if (dataset.GetExtendedPanId(mExtendedPanId) != CHIP_NO_ERROR)
{
return ByteSpan();
}

return ByteSpan(mExtendedPanId);
}

CHIP_ERROR PairingCommand::EnableNetwork()
{
Callback::Cancelable * successCallback = mOnEnableNetworkCallback->Cancel();
Expand All @@ -270,7 +289,12 @@ CHIP_ERROR PairingCommand::EnableNetwork()
}
else
{
networkId = ByteSpan(kTemporaryThreadNetworkId, sizeof(kTemporaryThreadNetworkId));
networkId = GetThreadNetworkId();
}

if (networkId.empty())
{
return CHIP_ERROR_INVALID_ARGUMENT;
}

return mCluster.EnableNetwork(successCallback, failureCallback, networkId, kBreadcrumb, kTimeoutMs);
Expand Down
4 changes: 4 additions & 0 deletions examples/chip-tool/commands/pairing/PairingCommand.h
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
#include "gen/CHIPClusters.h"

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

Expand Down Expand Up @@ -140,6 +141,8 @@ class PairingCommand : public Command,
CHIP_ERROR EnableNetwork();
CHIP_ERROR UpdateNetworkAddress();

chip::ByteSpan GetThreadNetworkId();

const PairingMode mPairingMode;
const PairingNetworkType mNetworkType;
Command::AddressWithInterface mRemoteAddr;
Expand All @@ -149,6 +152,7 @@ class PairingCommand : public Command,
uint16_t mDiscriminator;
uint32_t mSetupPINCode;
chip::ByteSpan mOperationalDataset;
uint8_t mExtendedPanId[chip::Thread::kSizeExtendedPanId];
chip::ByteSpan mSSID;
chip::ByteSpan mPassword;
char * mOnboardingPayload;
Expand Down

0 comments on commit 8ccbbb5

Please sign in to comment.