mirror of
https://github.com/espressif/openthread.git
synced 2026-10-01 21:37:39 +00:00
[mac] add Mac::GenerateRandomPanId() (#4283)
This commit adds a helper function `Mac::GenerateRandomPanId()` which generates a random PAN Identifier (excluding the broadcast PAN Id `kPanIdBroadcast`).
This commit is contained in:
committed by
Jonathan Hui
parent
a1cf4397ae
commit
21da048a00
@@ -42,6 +42,18 @@
|
|||||||
namespace ot {
|
namespace ot {
|
||||||
namespace Mac {
|
namespace Mac {
|
||||||
|
|
||||||
|
PanId GenerateRandomPanId(void)
|
||||||
|
{
|
||||||
|
PanId panId;
|
||||||
|
|
||||||
|
do
|
||||||
|
{
|
||||||
|
panId = Random::NonCrypto::GetUint16();
|
||||||
|
} while (panId == kPanIdBroadcast);
|
||||||
|
|
||||||
|
return panId;
|
||||||
|
}
|
||||||
|
|
||||||
void ExtAddress::GenerateRandom(void)
|
void ExtAddress::GenerateRandom(void)
|
||||||
{
|
{
|
||||||
Random::NonCrypto::FillBuffer(m8, sizeof(ExtAddress));
|
Random::NonCrypto::FillBuffer(m8, sizeof(ExtAddress));
|
||||||
|
|||||||
@@ -73,6 +73,14 @@ typedef otPanId PanId;
|
|||||||
*/
|
*/
|
||||||
typedef otShortAddress ShortAddress;
|
typedef otShortAddress ShortAddress;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* This function generates a random IEEE 802.15.4 PAN ID.
|
||||||
|
*
|
||||||
|
* @returns A randomly generated IEEE 802.15.4 PAN ID (excluding `kPanIdBroadcast`).
|
||||||
|
*
|
||||||
|
*/
|
||||||
|
PanId GenerateRandomPanId(void);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* This structure represents an IEEE 802.15.4 Extended Address.
|
* This structure represents an IEEE 802.15.4 Extended Address.
|
||||||
*
|
*
|
||||||
|
|||||||
@@ -345,10 +345,7 @@ otError ActiveDataset::CreateNewNetwork(otOperationalDataset &aDataset)
|
|||||||
aDataset.mChannel = preferredChannels.ChooseRandomChannel();
|
aDataset.mChannel = preferredChannels.ChooseRandomChannel();
|
||||||
aDataset.mChannelMask = supportedChannels.GetMask();
|
aDataset.mChannelMask = supportedChannels.GetMask();
|
||||||
|
|
||||||
do
|
aDataset.mPanId = Mac::GenerateRandomPanId();
|
||||||
{
|
|
||||||
aDataset.mPanId = Random::NonCrypto::GetUint16();
|
|
||||||
} while (aDataset.mPanId == Mac::kPanIdBroadcast);
|
|
||||||
|
|
||||||
snprintf(aDataset.mNetworkName.m8, sizeof(aDataset.mNetworkName), "OpenThread-%04x", aDataset.mPanId);
|
snprintf(aDataset.mNetworkName.m8, sizeof(aDataset.mNetworkName), "OpenThread-%04x", aDataset.mPanId);
|
||||||
|
|
||||||
|
|||||||
@@ -496,14 +496,7 @@ otError MeshForwarder::HandleFrameRequest(Mac::TxFrame &aFrame)
|
|||||||
// value.
|
// value.
|
||||||
if (mSendMessage->GetPanId() == Mac::kPanIdBroadcast && Get<Mac::Mac>().GetPanId() == Mac::kPanIdBroadcast)
|
if (mSendMessage->GetPanId() == Mac::kPanIdBroadcast && Get<Mac::Mac>().GetPanId() == Mac::kPanIdBroadcast)
|
||||||
{
|
{
|
||||||
uint16_t panid;
|
Get<Mac::Mac>().SetPanId(Mac::GenerateRandomPanId());
|
||||||
|
|
||||||
do
|
|
||||||
{
|
|
||||||
panid = Random::NonCrypto::GetUint16();
|
|
||||||
} while (panid == Mac::kPanIdBroadcast);
|
|
||||||
|
|
||||||
Get<Mac::Mac>().SetPanId(panid);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user