[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:
Abtin Keshavarzian
2019-10-31 12:58:01 -07:00
committed by Jonathan Hui
parent a1cf4397ae
commit 21da048a00
4 changed files with 22 additions and 12 deletions
+12
View File
@@ -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));
+8
View File
@@ -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.
*
+1 -4
View File
@@ -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);
+1 -8
View File
@@ -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());
}
}