Skip to content

Commit

Permalink
[Thread] use operational dataset (#5415)
Browse files Browse the repository at this point in the history
  • Loading branch information
bukepo authored Apr 19, 2021
1 parent 994e13c commit 3bb3bb9
Show file tree
Hide file tree
Showing 46 changed files with 1,424 additions and 548 deletions.
56 changes: 16 additions & 40 deletions examples/lighting-app/k32w/main/AppTask.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
#include "OnboardingCodesUtil.h"
#include <platform/CHIPDeviceLayer.h>
#include <platform/internal/DeviceNetworkInfo.h>
#include <support/ThreadOperationalDataset.h>

#include "attribute-storage.h"
#include "gen/attribute-id.h"
Expand Down Expand Up @@ -396,48 +397,23 @@ void AppTask::LightActionEventHandler(AppEvent * aEvent)

void AppTask::ThreadStart()
{
chip::DeviceLayer::Internal::DeviceNetworkInfo networkInfo;

memset(networkInfo.ThreadNetworkName, 0, chip::DeviceLayer::Internal::kMaxThreadNetworkNameLength + 1);
memcpy(networkInfo.ThreadNetworkName, "OpenThread", 10);

networkInfo.ThreadExtendedPANId[0] = 0xde;
networkInfo.ThreadExtendedPANId[1] = 0xad;
networkInfo.ThreadExtendedPANId[2] = 0x00;
networkInfo.ThreadExtendedPANId[3] = 0xbe;
networkInfo.ThreadExtendedPANId[4] = 0xef;
networkInfo.ThreadExtendedPANId[5] = 0x00;
networkInfo.ThreadExtendedPANId[6] = 0xca;
networkInfo.ThreadExtendedPANId[7] = 0xfe;

networkInfo.ThreadMasterKey[0] = 0x00;
networkInfo.ThreadMasterKey[1] = 0x11;
networkInfo.ThreadMasterKey[2] = 0x22;
networkInfo.ThreadMasterKey[3] = 0x33;
networkInfo.ThreadMasterKey[4] = 0x44;
networkInfo.ThreadMasterKey[5] = 0x55;
networkInfo.ThreadMasterKey[6] = 0x66;
networkInfo.ThreadMasterKey[7] = 0x77;
networkInfo.ThreadMasterKey[8] = 0x88;
networkInfo.ThreadMasterKey[9] = 0x99;
networkInfo.ThreadMasterKey[10] = 0xAA;
networkInfo.ThreadMasterKey[11] = 0xBB;
networkInfo.ThreadMasterKey[12] = 0xCC;
networkInfo.ThreadMasterKey[13] = 0xDD;
networkInfo.ThreadMasterKey[14] = 0xEE;
networkInfo.ThreadMasterKey[15] = 0xFF;

networkInfo.ThreadPANId = 0xabcd;
networkInfo.ThreadChannel = 15;

networkInfo.FieldPresent.ThreadExtendedPANId = true;
networkInfo.FieldPresent.ThreadMeshPrefix = false;
networkInfo.FieldPresent.ThreadPSKc = false;
networkInfo.NetworkId = 0;
networkInfo.FieldPresent.NetworkId = true;
chip::Thread::OperationalDataset dataset{};

constexpr uint8_t xpanid[] = { 0xde, 0xad, 0x00, 0xbe, 0xef, 0x00, 0xca, 0xfe };
constexpr uint8_t masterkey[] = {
0x00, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77, 0x88, 0x99, 0xAA, 0xBB, 0xCC, 0xDD, 0xEE, 0xFF
};
constexpr uint16_t panid = 0xabcd;
constexpr uint16_t channel = 15;

dataset.SetNetworkName("OpenThread");
dataset.SetExtendedPanId(xpanid);
dataset.SetMasterKey(masterkey);
dataset.SetPanId(panid);
dataset.SetChannel(channel);

ThreadStackMgr().SetThreadEnabled(false);
ThreadStackMgr().SetThreadProvision(networkInfo);
ThreadStackMgr().SetThreadProvision(dataset.AsByteSpan());
ThreadStackMgr().SetThreadEnabled(true);
}

Expand Down
56 changes: 16 additions & 40 deletions examples/lock-app/k32w/main/AppTask.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
#include "OnboardingCodesUtil.h"
#include <platform/CHIPDeviceLayer.h>
#include <platform/internal/DeviceNetworkInfo.h>
#include <support/ThreadOperationalDataset.h>

#include "attribute-storage.h"
#include "gen/attribute-id.h"
Expand Down Expand Up @@ -400,48 +401,23 @@ void AppTask::LockActionEventHandler(AppEvent * aEvent)

void AppTask::ThreadStart()
{
chip::DeviceLayer::Internal::DeviceNetworkInfo networkInfo;

memset(networkInfo.ThreadNetworkName, 0, chip::DeviceLayer::Internal::kMaxThreadNetworkNameLength + 1);
memcpy(networkInfo.ThreadNetworkName, "OpenThread", 10);

networkInfo.ThreadExtendedPANId[0] = 0xde;
networkInfo.ThreadExtendedPANId[1] = 0xad;
networkInfo.ThreadExtendedPANId[2] = 0x00;
networkInfo.ThreadExtendedPANId[3] = 0xbe;
networkInfo.ThreadExtendedPANId[4] = 0xef;
networkInfo.ThreadExtendedPANId[5] = 0x00;
networkInfo.ThreadExtendedPANId[6] = 0xca;
networkInfo.ThreadExtendedPANId[7] = 0xfe;

networkInfo.ThreadMasterKey[0] = 0x00;
networkInfo.ThreadMasterKey[1] = 0x11;
networkInfo.ThreadMasterKey[2] = 0x22;
networkInfo.ThreadMasterKey[3] = 0x33;
networkInfo.ThreadMasterKey[4] = 0x44;
networkInfo.ThreadMasterKey[5] = 0x55;
networkInfo.ThreadMasterKey[6] = 0x66;
networkInfo.ThreadMasterKey[7] = 0x77;
networkInfo.ThreadMasterKey[8] = 0x88;
networkInfo.ThreadMasterKey[9] = 0x99;
networkInfo.ThreadMasterKey[10] = 0xAA;
networkInfo.ThreadMasterKey[11] = 0xBB;
networkInfo.ThreadMasterKey[12] = 0xCC;
networkInfo.ThreadMasterKey[13] = 0xDD;
networkInfo.ThreadMasterKey[14] = 0xEE;
networkInfo.ThreadMasterKey[15] = 0xFF;

networkInfo.ThreadPANId = 0xabcd;
networkInfo.ThreadChannel = 15;

networkInfo.FieldPresent.ThreadExtendedPANId = true;
networkInfo.FieldPresent.ThreadMeshPrefix = false;
networkInfo.FieldPresent.ThreadPSKc = false;
networkInfo.NetworkId = 0;
networkInfo.FieldPresent.NetworkId = true;
chip::Thread::OperationalDataset dataset{};

constexpr uint8_t xpanid[] = { 0xde, 0xad, 0x00, 0xbe, 0xef, 0x00, 0xca, 0xfe };
constexpr uint8_t masterkey[] = {
0x00, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77, 0x88, 0x99, 0xAA, 0xBB, 0xCC, 0xDD, 0xEE, 0xFF
};
constexpr uint16_t panid = 0xabcd;
constexpr uint16_t channel = 15;

dataset.SetNetworkName("OpenThread");
dataset.SetExtendedPanId(xpanid);
dataset.SetMasterKey(masterkey);
dataset.SetPanId(panid);
dataset.SetChannel(channel);

ThreadStackMgr().SetThreadEnabled(false);
ThreadStackMgr().SetThreadProvision(networkInfo);
ThreadStackMgr().SetThreadProvision(dataset.AsByteSpan());
ThreadStackMgr().SetThreadEnabled(true);
}

Expand Down
33 changes: 16 additions & 17 deletions examples/platform/nrfconnect/util/ThreadUtil.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,32 +20,31 @@

#include <platform/CHIPDeviceLayer.h>
#include <platform/internal/DeviceNetworkInfo.h>
#include <support/ThreadOperationalDataset.h>

#include <zephyr.h>

#include <cstring>

void StartDefaultThreadNetwork(void)
{
chip::DeviceLayer::Internal::DeviceNetworkInfo deviceNetworkInfo;
memset(&deviceNetworkInfo, 0, sizeof(deviceNetworkInfo));

const uint8_t masterKey[chip::DeviceLayer::Internal::kThreadMasterKeyLength] = {
0x00, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77, 0x88, 0x99, 0xaa, 0xbb, 0xcc, 0xdd, 0xee, 0xff
chip::Thread::OperationalDataset dataset{};
uint8_t xpanid[8];
constexpr uint8_t masterkey[] = {
0x00, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77, 0x88, 0x99, 0xAA, 0xBB, 0xCC, 0xDD, 0xEE, 0xFF
};
const uint8_t threadMeshPrefix[chip::DeviceLayer::Internal::kThreadMeshPrefixLength] = { 0xfd, 0x11, 0x11, 0x11,
0x11, 0x22, 0x00, 0x00 };

memcpy(deviceNetworkInfo.ThreadNetworkName, CONFIG_OPENTHREAD_NETWORK_NAME, strlen(CONFIG_OPENTHREAD_NETWORK_NAME));
net_bytes_from_str(deviceNetworkInfo.ThreadExtendedPANId, 8, CONFIG_OPENTHREAD_XPANID);
deviceNetworkInfo.FieldPresent.ThreadExtendedPANId = true;
memcpy(deviceNetworkInfo.ThreadMasterKey, masterKey, sizeof(masterKey));
deviceNetworkInfo.FieldPresent.ThreadMeshPrefix = true;
memcpy(deviceNetworkInfo.ThreadMeshPrefix, threadMeshPrefix, sizeof(threadMeshPrefix));
deviceNetworkInfo.ThreadPANId = CONFIG_OPENTHREAD_PANID;
deviceNetworkInfo.ThreadChannel = CONFIG_OPENTHREAD_CHANNEL;
constexpr uint8_t meshLocalPrefix[] = { 0xfd, 0x11, 0x11, 0x11, 0x11, 0x22, 0x00, 0x00 };

net_bytes_from_str(xpanid, sizeof(xpanid), CONFIG_OPENTHREAD_XPANID);

dataset.SetChannel(CONFIG_OPENTHREAD_CHANNEL);
dataset.SetExtendedPanId(xpanid);
dataset.SetMasterKey(masterkey);
dataset.SetMeshLocalPrefix(meshLocalPrefix);
dataset.SetNetworkName(CONFIG_OPENTHREAD_NETWORK_NAME);
dataset.SetPanId(CONFIG_OPENTHREAD_PANID);

chip::DeviceLayer::ThreadStackMgr().SetThreadEnabled(false);
chip::DeviceLayer::ThreadStackMgr().SetThreadProvision(deviceNetworkInfo);
chip::DeviceLayer::ThreadStackMgr().SetThreadProvision(dataset.AsByteSpan());
chip::DeviceLayer::ThreadStackMgr().SetThreadEnabled(true);
}
35 changes: 15 additions & 20 deletions examples/platform/qpg6100/app/Service.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@
#include <platform/CHIPDeviceLayer.h>
#include <support/CodeUtils.h>
#include <support/ErrorStr.h>
#include <support/ThreadOperationalDataset.h>
#include <system/SystemPacketBuffer.h>
#include <transport/SecureSessionMgr.h>
#include <transport/raw/UDP.h>
Expand Down Expand Up @@ -114,29 +115,23 @@ void PublishService()

void StartDefaultThreadNetwork(void)
{
// Set default thread network Info and enable/start thread

chip::DeviceLayer::Internal::DeviceNetworkInfo deviceNetworkInfo;
memset(&deviceNetworkInfo, 0, sizeof(deviceNetworkInfo));
const char * networkName = "OpenThread";
const uint8_t masterKey[chip::DeviceLayer::Internal::kThreadMasterKeyLength] = {
chip::Thread::OperationalDataset dataset{};
constexpr uint8_t masterkey[] = {
0x00, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77, 0x88, 0x99, 0xaa, 0xbb, 0xcc, 0xdd, 0xee, 0xff
};
const uint8_t threadMeshPrefix[chip::DeviceLayer::Internal::kThreadMeshPrefixLength] = { 0xfd, 0xde, 0xad, 0x00,
0xbe, 0xef, 0x00, 0x00 };

const uint8_t extendedPanId[chip::DeviceLayer::Internal::kThreadExtendedPANIdLength] = { 0xDE, 0xAD, 0x00, 0xBE,
0xEF, 0x00, 0xCA, 0xFE };
memcpy(deviceNetworkInfo.ThreadNetworkName, networkName, strlen(networkName));
memcpy(deviceNetworkInfo.ThreadExtendedPANId, extendedPanId, sizeof(extendedPanId));
deviceNetworkInfo.FieldPresent.ThreadExtendedPANId = true;
memcpy(deviceNetworkInfo.ThreadMasterKey, masterKey, sizeof(masterKey));
deviceNetworkInfo.FieldPresent.ThreadMeshPrefix = true;
memcpy(deviceNetworkInfo.ThreadMeshPrefix, threadMeshPrefix, sizeof(threadMeshPrefix));
deviceNetworkInfo.ThreadPANId = 0x1234;
deviceNetworkInfo.ThreadChannel = 11;
constexpr uint8_t meshLocalPrefix[] = { 0xfd, 0xde, 0xad, 0x00, 0xbe, 0xef, 0x00, 0x00 };
constexpr uint8_t xpanid[] = { 0xDE, 0xAD, 0x00, 0xBE, 0xEF, 0x00, 0xCA, 0xFE };
constexpr uint16_t panid = 0x1234;
constexpr uint16_t channel = 11;

dataset.SetChannel(channel);
dataset.SetExtendedPanId(xpanid);
dataset.SetMasterKey(masterkey);
dataset.SetMeshLocalPrefix(meshLocalPrefix);
dataset.SetNetworkName("OpenThread");
dataset.SetPanId(panid);

chip::DeviceLayer::ThreadStackMgr().SetThreadEnabled(false);
chip::DeviceLayer::ThreadStackMgr().SetThreadProvision(deviceNetworkInfo);
chip::DeviceLayer::ThreadStackMgr().SetThreadProvision(dataset.AsByteSpan());
chip::DeviceLayer::ThreadStackMgr().SetThreadEnabled(true);
}
40 changes: 18 additions & 22 deletions src/app/clusters/network-commissioning/network-commissioning.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
#include <lib/support/CodeUtils.h>
#include <lib/support/SafeInt.h>
#include <lib/support/Span.h>
#include <lib/support/ThreadOperationalDataset.h>
#include <lib/support/logging/CHIPLogging.h>
#include <platform/CHIPDeviceLayer.h>
#include <platform/ConnectivityManager.h>
Expand Down Expand Up @@ -66,12 +67,6 @@ constexpr uint8_t kMaxWiFiSSIDLen = 32;
constexpr uint8_t kMaxWiFiCredentialsLen = 64;
constexpr uint8_t kMaxNetworks = 4;

// The temporary network id which will be used by thread networks in CHIP before we have the API for getting extpanid from dataset
// tlv.
#if CHIP_DEVICE_CONFIG_ENABLE_THREAD
constexpr uint8_t kTemporaryThreadNetworkId[] = { 0x01, 0x23, 0x45, 0x67, 0x89, 0xab, 0xcd, 0xef };
#endif

enum class NetworkType : uint8_t
{
kUndefined = 0,
Expand Down Expand Up @@ -102,7 +97,7 @@ struct NetworkInfo
NetworkType mNetworkType;
union NetworkData
{
ThreadNetworkInfo mThread;
Thread::OperationalDataset mThread;
WiFiNetworkInfo mWiFi;
} mData;
};
Expand All @@ -122,21 +117,23 @@ EmberAfNetworkCommissioningError OnAddThreadNetworkCommandCallbackInternal(app::
{
if (sNetworks[i].mNetworkType == NetworkType::kUndefined)
{
VerifyOrExit(operationalDataset.size() <= sizeof(ThreadNetworkInfo::mDataset),
err = EMBER_ZCL_NETWORK_COMMISSIONING_ERROR_OUT_OF_RANGE);
memcpy(sNetworks[i].mData.mThread.mDataset, operationalDataset.data(), operationalDataset.size());
Thread::OperationalDataset & dataset = sNetworks[i].mData.mThread;
CHIP_ERROR error = dataset.Init(operationalDataset);

using ThreadDatasetLenType = decltype(sNetworks[i].mData.mThread.mDatasetLen);
VerifyOrExit(chip::CanCastTo<ThreadDatasetLenType>(operationalDataset.size()),
err = EMBER_ZCL_NETWORK_COMMISSIONING_ERROR_OUT_OF_RANGE);
sNetworks[i].mData.mThread.mDatasetLen = static_cast<ThreadDatasetLenType>(operationalDataset.size());
if (error != CHIP_NO_ERROR)
{
ChipLogDetail(Zcl, "Failed to parse Thread operational dataset: %s", ErrorStr(error));
err = EMBER_ZCL_NETWORK_COMMISSIONING_ERROR_UNKNOWN_ERROR;
break;
}

uint8_t extendedPanId[Thread::kSizeExtendedPanId];

// A 64bit id for thread networks, currently, we are missing some API for getting the thread network extpanid from the
// dataset tlv.
static_assert(sizeof(kTemporaryThreadNetworkId) <= sizeof(sNetworks[i].mNetworkID),
"TemporaryThreadNetworkId too long");
memcpy(sNetworks[i].mNetworkID, kTemporaryThreadNetworkId, sizeof(kTemporaryThreadNetworkId));
sNetworks[i].mNetworkIDLen = sizeof(kTemporaryThreadNetworkId);
static_assert(sizeof(sNetworks[i].mNetworkID) >= sizeof(extendedPanId),
"Network ID must be larger than Thread extended PAN ID!");
SuccessOrExit(dataset.GetExtendedPanId(extendedPanId));
memcpy(sNetworks[i].mNetworkID, extendedPanId, sizeof(extendedPanId));
sNetworks[i].mNetworkIDLen = sizeof(extendedPanId);

sNetworks[i].mNetworkType = NetworkType::kThread;
sNetworks[i].mEnabled = false;
Expand Down Expand Up @@ -218,8 +215,7 @@ CHIP_ERROR DoEnableNetwork(NetworkInfo * network)
case NetworkType::kThread:
#if CHIP_DEVICE_CONFIG_ENABLE_THREAD
ReturnErrorOnFailure(DeviceLayer::ThreadStackMgr().SetThreadEnabled(false));
ReturnErrorOnFailure(
DeviceLayer::ThreadStackMgr().SetThreadProvision(network->mData.mThread.mDataset, network->mData.mThread.mDatasetLen));
ReturnErrorOnFailure(DeviceLayer::ThreadStackMgr().SetThreadProvision(network->mData.mThread.AsByteSpan()));
ReturnErrorOnFailure(DeviceLayer::ThreadStackMgr().SetThreadEnabled(true));
#else
return CHIP_ERROR_UNSUPPORTED_CHIP_FEATURE;
Expand Down
21 changes: 18 additions & 3 deletions src/controller/java/AndroidDeviceControllerWrapper.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@

#include <memory>

#include <support/ThreadOperationalDataset.h>

using chip::PersistentStorageResultDelegate;
using chip::Controller::DeviceCommissioner;

Expand Down Expand Up @@ -214,16 +216,29 @@ void AndroidDeviceControllerWrapper::SendNetworkCredentials(const char * ssid, c
mCredentialsDelegate->SendNetworkCredentials(ssid, password);
}

void AndroidDeviceControllerWrapper::SendThreadCredentials(const chip::DeviceLayer::Internal::DeviceNetworkInfo & threadData)
void AndroidDeviceControllerWrapper::SendThreadCredentials(chip::ByteSpan threadData)
{
if (mCredentialsDelegate == nullptr)
{
ChipLogError(Controller, "No credential callback available to send Thread credentials.");
return;
}

ChipLogProgress(Controller, "Sending Thread credentials for channel %u, PAN ID %x...", threadData.ThreadChannel,
threadData.ThreadPANId);
chip::Thread::OperationalDataset dataset{};

if (!dataset.Init(threadData))
{
ChipLogError(Controller, "Failed to parse Thread credentials.");
return;
}

uint16_t channel = chip::Thread::kChannel_NotSpecified;
uint16_t panid = chip::Thread::kPANId_NotSpecified;

dataset.GetChannel(channel);
dataset.GetPanId(panid);

ChipLogProgress(Controller, "Sending Thread credentials for channel %u, PAN ID 0x%04x...", channel, panid);
mCredentialsDelegate->SendThreadCredentials(threadData);
}

Expand Down
2 changes: 1 addition & 1 deletion src/controller/java/AndroidDeviceControllerWrapper.h
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ class AndroidDeviceControllerWrapper : public chip::Controller::DevicePairingDel
void SetJavaObjectRef(JavaVM * vm, jobject obj);

void SendNetworkCredentials(const char * ssid, const char * password);
void SendThreadCredentials(const chip::DeviceLayer::Internal::DeviceNetworkInfo & threadData);
void SendThreadCredentials(chip::ByteSpan threadData);

// DevicePairingDelegate implementation
void OnNetworkCredentialsRequested(chip::RendezvousDeviceCredentialsDelegate * callback) override;
Expand Down
Loading

0 comments on commit 3bb3bb9

Please sign in to comment.