1 /*
2 * Copyright (c) 2016-2018, The OpenThread Authors.
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions are met:
7 * 1. Redistributions of source code must retain the above copyright
8 * notice, this list of conditions and the following disclaimer.
9 * 2. Redistributions in binary form must reproduce the above copyright
10 * notice, this list of conditions and the following disclaimer in the
11 * documentation and/or other materials provided with the distribution.
12 * 3. Neither the name of the copyright holder nor the
13 * names of its contributors may be used to endorse or promote products
14 * derived from this software without specific prior written permission.
15 *
16 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
17 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
20 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
21 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
22 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
23 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
24 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
25 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
26 * POSSIBILITY OF SUCH DAMAGE.
27 */
28
29 /**
30 * @file
31 * This file implements MAC Channel Mask.
32 */
33
34 #include "channel_mask.hpp"
35
36 #include "common/code_utils.hpp"
37 #include "common/random.hpp"
38
39 namespace ot {
40 namespace Mac {
41
GetNumberOfChannels(void) const42 uint8_t ChannelMask::GetNumberOfChannels(void) const
43 {
44 uint8_t num = 0;
45 uint8_t channel = kChannelIteratorFirst;
46
47 while (GetNextChannel(channel) == kErrorNone)
48 {
49 num++;
50 }
51
52 return num;
53 }
54
GetNextChannel(uint8_t & aChannel) const55 Error ChannelMask::GetNextChannel(uint8_t &aChannel) const
56 {
57 Error error = kErrorNotFound;
58
59 if (aChannel == kChannelIteratorFirst)
60 {
61 aChannel = (Radio::kChannelMin - 1);
62 }
63
64 for (aChannel++; aChannel <= Radio::kChannelMax; aChannel++)
65 {
66 if (ContainsChannel(aChannel))
67 {
68 ExitNow(error = kErrorNone);
69 }
70 }
71
72 exit:
73 return error;
74 }
75
ChooseRandomChannel(void) const76 uint8_t ChannelMask::ChooseRandomChannel(void) const
77 {
78 uint8_t channel = kChannelIteratorFirst;
79 uint8_t randomIndex;
80
81 VerifyOrExit(!IsEmpty());
82
83 randomIndex = Random::NonCrypto::GetUint8InRange(0, GetNumberOfChannels());
84
85 SuccessOrExit(GetNextChannel(channel));
86
87 while (randomIndex-- != 0)
88 {
89 SuccessOrExit(GetNextChannel(channel));
90 }
91
92 exit:
93 return channel;
94 }
95
ToString(void) const96 ChannelMask::InfoString ChannelMask::ToString(void) const
97 {
98 InfoString string;
99 uint8_t channel = kChannelIteratorFirst;
100 bool addComma = false;
101 Error error;
102
103 string.Append("{");
104
105 error = GetNextChannel(channel);
106
107 while (error == kErrorNone)
108 {
109 uint8_t rangeStart = channel;
110 uint8_t rangeEnd = channel;
111
112 while ((error = GetNextChannel(channel)) == kErrorNone)
113 {
114 if (channel != rangeEnd + 1)
115 {
116 break;
117 }
118
119 rangeEnd = channel;
120 }
121
122 string.Append("%s%d", addComma ? ", " : " ", rangeStart);
123 addComma = true;
124
125 if (rangeStart < rangeEnd)
126 {
127 string.Append("%s%d", rangeEnd == rangeStart + 1 ? ", " : "-", rangeEnd);
128 }
129 }
130
131 string.Append(" }");
132
133 return string;
134 }
135
136 } // namespace Mac
137 } // namespace ot
138