mirror of
https://github.com/espressif/openthread.git
synced 2026-08-25 19:59:51 +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 Mac {
|
||||
|
||||
PanId GenerateRandomPanId(void)
|
||||
{
|
||||
PanId panId;
|
||||
|
||||
do
|
||||
{
|
||||
panId = Random::NonCrypto::GetUint16();
|
||||
} while (panId == kPanIdBroadcast);
|
||||
|
||||
return panId;
|
||||
}
|
||||
|
||||
void ExtAddress::GenerateRandom(void)
|
||||
{
|
||||
Random::NonCrypto::FillBuffer(m8, sizeof(ExtAddress));
|
||||
|
||||
@@ -73,6 +73,14 @@ typedef otPanId PanId;
|
||||
*/
|
||||
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.
|
||||
*
|
||||
|
||||
@@ -345,10 +345,7 @@ otError ActiveDataset::CreateNewNetwork(otOperationalDataset &aDataset)
|
||||
aDataset.mChannel = preferredChannels.ChooseRandomChannel();
|
||||
aDataset.mChannelMask = supportedChannels.GetMask();
|
||||
|
||||
do
|
||||
{
|
||||
aDataset.mPanId = Random::NonCrypto::GetUint16();
|
||||
} while (aDataset.mPanId == Mac::kPanIdBroadcast);
|
||||
aDataset.mPanId = Mac::GenerateRandomPanId();
|
||||
|
||||
snprintf(aDataset.mNetworkName.m8, sizeof(aDataset.mNetworkName), "OpenThread-%04x", aDataset.mPanId);
|
||||
|
||||
|
||||
@@ -496,14 +496,7 @@ otError MeshForwarder::HandleFrameRequest(Mac::TxFrame &aFrame)
|
||||
// value.
|
||||
if (mSendMessage->GetPanId() == Mac::kPanIdBroadcast && Get<Mac::Mac>().GetPanId() == Mac::kPanIdBroadcast)
|
||||
{
|
||||
uint16_t panid;
|
||||
|
||||
do
|
||||
{
|
||||
panid = Random::NonCrypto::GetUint16();
|
||||
} while (panid == Mac::kPanIdBroadcast);
|
||||
|
||||
Get<Mac::Mac>().SetPanId(panid);
|
||||
Get<Mac::Mac>().SetPanId(Mac::GenerateRandomPanId());
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user