[dataset] choose a preferred channel when forming new dataset (#3910)

This commit changes the `ActiveDataset::CreateNewNetwork()` to select
a random channel from the preferred channel mask if it is not empty,
otherwise a random channel from the supported mask is picked.
This commit is contained in:
Abtin Keshavarzian
2019-06-12 08:45:29 -07:00
committed by Jonathan Hui
parent d614d4a080
commit dbc04a8a05
+17 -3
View File
@@ -317,7 +317,9 @@ exit:
otError ActiveDataset::CreateNewNetwork(otOperationalDataset &aDataset)
{
otError error = OT_ERROR_NONE;
otError error = OT_ERROR_NONE;
Mac::ChannelMask supportedChannels = Get<Mac::Mac>().GetSupportedChannelMask();
Mac::ChannelMask preferredChannels(otPlatRadioGetPreferredChannelMask(&GetInstance()));
memset(&aDataset, 0, sizeof(aDataset));
@@ -331,8 +333,20 @@ otError ActiveDataset::CreateNewNetwork(otOperationalDataset &aDataset)
SuccessOrExit(error = Random::Crypto::FillBuffer(&aDataset.mMeshLocalPrefix.m8[1], OT_MESH_LOCAL_PREFIX_SIZE - 1));
aDataset.mSecurityPolicy.mFlags = Get<KeyManager>().GetSecurityPolicyFlags();
aDataset.mChannelMask = Get<Mac::Mac>().GetSupportedChannelMask().GetMask();
aDataset.mChannel = Get<Mac::Mac>().GetSupportedChannelMask().ChooseRandomChannel();
// If the preferred channel mask is not empty, select a random
// channel from it, otherwise choose one from the supported
// channel mask.
preferredChannels.Intersect(supportedChannels);
if (preferredChannels.IsEmpty())
{
preferredChannels = supportedChannels;
}
aDataset.mChannel = preferredChannels.ChooseRandomChannel();
aDataset.mChannelMask = supportedChannels.GetMask();
do
{