Skip to content

Commit

Permalink
[dataset] properly configure operational dataset (#1935)
Browse files Browse the repository at this point in the history
  • Loading branch information
jwhui authored Jul 10, 2023
1 parent 2a11b14 commit 3c60d48
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 54 deletions.
68 changes: 21 additions & 47 deletions src/utils/thread_helper.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@

#include <openthread/border_router.h>
#include <openthread/channel_manager.h>
#include <openthread/dataset_ftd.h>
#include <openthread/jam_detection.h>
#include <openthread/joiner.h>
#include <openthread/thread_ftd.h>
Expand Down Expand Up @@ -321,78 +322,51 @@ void ThreadHelper::Attach(const std::string &aNetworkName,
AttachHandler aHandler)

{
otError error = OT_ERROR_NONE;
otExtendedPanId extPanId;
otNetworkKey networkKey;
otPskc pskc;
uint32_t channelMask;
uint8_t channel;
otError error = OT_ERROR_NONE;
otOperationalDataset dataset = {};

VerifyOrExit(aHandler != nullptr, error = OT_ERROR_INVALID_ARGS);
VerifyOrExit(mAttachHandler == nullptr && mJoinerHandler == nullptr, error = OT_ERROR_INVALID_STATE);
VerifyOrExit(aNetworkKey.empty() || aNetworkKey.size() == sizeof(networkKey.m8), error = OT_ERROR_INVALID_ARGS);
VerifyOrExit(aPSKc.empty() || aPSKc.size() == sizeof(pskc.m8), error = OT_ERROR_INVALID_ARGS);
VerifyOrExit(aNetworkKey.empty() || aNetworkKey.size() == sizeof(dataset.mNetworkKey.m8),
error = OT_ERROR_INVALID_ARGS);
VerifyOrExit(aPSKc.empty() || aPSKc.size() == sizeof(dataset.mPskc.m8), error = OT_ERROR_INVALID_ARGS);
VerifyOrExit(aChannelMask != 0, error = OT_ERROR_INVALID_ARGS);

while (aPanId == UINT16_MAX)
{
RandomFill(&aPanId, sizeof(aPanId));
}
SuccessOrExit(error = otDatasetCreateNewNetwork(mInstance, &dataset));

if (aExtPanId != UINT64_MAX)
{
extPanId = ToOtExtendedPanId(aExtPanId);
}
else
{
*reinterpret_cast<uint64_t *>(&extPanId) = UINT64_MAX;

while (*reinterpret_cast<uint64_t *>(&extPanId) == UINT64_MAX)
{
RandomFill(extPanId.m8, sizeof(extPanId.m8));
}
dataset.mExtendedPanId = ToOtExtendedPanId(aExtPanId);
}

if (!aNetworkKey.empty())
{
memcpy(networkKey.m8, &aNetworkKey[0], sizeof(networkKey.m8));
memcpy(dataset.mNetworkKey.m8, &aNetworkKey[0], sizeof(dataset.mNetworkKey.m8));
}
else

if (aPanId != UINT16_MAX)
{
RandomFill(networkKey.m8, sizeof(networkKey.m8));
dataset.mPanId = aPanId;
}

if (!aPSKc.empty())
{
memcpy(pskc.m8, &aPSKc[0], sizeof(pskc.m8));
}
else
{
RandomFill(pskc.m8, sizeof(pskc.m8));
memcpy(dataset.mPskc.m8, &aPSKc[0], sizeof(dataset.mPskc.m8));
}

if (!otIp6IsEnabled(mInstance))
{
SuccessOrExit(error = otIp6SetEnabled(mInstance, true));
}
SuccessOrExit(error = otNetworkNameFromString(&dataset.mNetworkName, aNetworkName.c_str()));

dataset.mChannelMask &= aChannelMask;
VerifyOrExit(dataset.mChannelMask != 0, otbrLogWarning("Invalid channel mask"), error = OT_ERROR_INVALID_ARGS);

SuccessOrExit(error = otThreadSetNetworkName(mInstance, aNetworkName.c_str()));
SuccessOrExit(error = otLinkSetPanId(mInstance, aPanId));
SuccessOrExit(error = otThreadSetExtendedPanId(mInstance, &extPanId));
SuccessOrExit(error = otThreadSetNetworkKey(mInstance, &networkKey));
dataset.mChannel = RandomChannelFromChannelMask(dataset.mChannelMask);

channelMask = otPlatRadioGetPreferredChannelMask(mInstance) & aChannelMask;
SuccessOrExit(error = otDatasetSetActive(mInstance, &dataset));

if (channelMask == 0)
if (!otIp6IsEnabled(mInstance))
{
channelMask = otLinkGetSupportedChannelMask(mInstance) & aChannelMask;
SuccessOrExit(error = otIp6SetEnabled(mInstance, true));
}
VerifyOrExit(channelMask != 0, otbrLogWarning("Invalid channel mask"), error = OT_ERROR_INVALID_ARGS);

channel = RandomChannelFromChannelMask(channelMask);
SuccessOrExit(otLinkSetChannel(mInstance, channel));

SuccessOrExit(error = otThreadSetPskc(mInstance, &pskc));

SuccessOrExit(error = otThreadSetEnabled(mInstance, true));
mAttachDelayMs = 0;
Expand Down
22 changes: 15 additions & 7 deletions tests/dbus/test-client
Original file line number Diff line number Diff line change
Expand Up @@ -221,11 +221,15 @@ main()
# The ot-cli-ftd node is used to test Thread attach.
expect <<EOF &
spawn ot-cli-ftd 3
send "panid 0x7890\r\n"
send "dataset init new\r\n"
expect "Done"
send "networkname Test1\r\n"
send "dataset panid 0x7890\r\n"
expect "Done"
send "channel 15\r\n"
send "dataset networkname Test1\r\n"
expect "Done"
send "dataset channel 15\r\n"
expect "Done"
send "dataset commit active\r\n"
expect "Done"
send "ifconfig up\r\n"
expect "Done"
Expand All @@ -249,13 +253,17 @@ EOF
# The ot-cli-mtd node is used to test the child and neighbor table.
expect <<EOF &
spawn ot-cli-mtd 2
send "panid 0x3456\r\n"
send "dataset clear"
expect "Done"
send "dataset panid 0x3456\r\n"
expect "Done"
send "dataset networkkey 00112233445566778899aabbccddeeff\r\n"
expect "Done"
send "networkkey 00112233445566778899aabbccddeeff\r\n"
send "dataset networkname Test\r\n"
expect "Done"
send "networkname Test\r\n"
send "dataset channel 11\r\n"
expect "Done"
send "channel 11\r\n"
send "dataset commit active\r\n"
expect "Done"
send "ifconfig up\r\n"
expect "Done"
Expand Down
4 changes: 4 additions & 0 deletions tests/rest/test-rest-server
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,10 @@ main()
sleep 1
sudo expect <<EOF &
spawn ${CMAKE_BINARY_DIR}/third_party/openthread/repo/src/posix/ot-ctl
send "dataset init new\r\n"
expect "Done"
send "dataset commit active\r\n"
expect "Done"
send "ifconfig up\r\n"
expect "Done"
send "thread start\r\n"
Expand Down

0 comments on commit 3c60d48

Please sign in to comment.