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 the AnnounceSender.
32 */
33
34 #include "announce_sender.hpp"
35
36 #include <openthread/platform/radio.h>
37
38 #include "common/code_utils.hpp"
39 #include "common/instance.hpp"
40 #include "common/locator_getters.hpp"
41 #include "common/log.hpp"
42 #include "common/random.hpp"
43 #include "meshcop/meshcop.hpp"
44 #include "meshcop/meshcop_tlvs.hpp"
45 #include "radio/radio.hpp"
46
47 namespace ot {
48
49 RegisterLogModule("AnnounceSender");
50
51 //---------------------------------------------------------------------------------------------------------------------
52 // AnnounceSenderBase
53
AnnounceSenderBase(Instance & aInstance,Timer::Handler aHandler)54 AnnounceSenderBase::AnnounceSenderBase(Instance &aInstance, Timer::Handler aHandler)
55 : InstanceLocator(aInstance)
56 , mPeriod(0)
57 , mJitter(0)
58 , mCount(0)
59 , mChannel(0)
60 , mStartingChannel(kChannelIteratorFirst)
61 , mTimer(aInstance, aHandler)
62 {
63 }
64
SendAnnounce(uint8_t aCount)65 void AnnounceSenderBase::SendAnnounce(uint8_t aCount)
66 {
67 if (IsRunning())
68 {
69 mCount += aCount;
70 ExitNow();
71 }
72
73 VerifyOrExit((mPeriod != 0) && !mChannelMask.IsEmpty());
74
75 SelectStartingChannel();
76
77 mCount = aCount;
78 mChannel = mStartingChannel;
79
80 mTimer.Start(Random::NonCrypto::GetUint32InRange(0, mJitter + 1));
81
82 exit:
83 return;
84 }
85
Stop(void)86 void AnnounceSenderBase::Stop(void)
87 {
88 mTimer.Stop();
89 mCount = 0;
90 }
91
SetChannelMask(Mac::ChannelMask aChannelMask)92 void AnnounceSenderBase::SetChannelMask(Mac::ChannelMask aChannelMask)
93 {
94 mChannelMask = aChannelMask;
95 mChannelMask.Intersect(Get<Mac::Mac>().GetSupportedChannelMask());
96
97 VerifyOrExit(!mChannelMask.IsEmpty(), Stop());
98 SelectStartingChannel();
99
100 exit:
101 return;
102 }
103
SetStartingChannel(uint8_t aStartingChannel)104 void AnnounceSenderBase::SetStartingChannel(uint8_t aStartingChannel)
105 {
106 mStartingChannel = aStartingChannel;
107 SelectStartingChannel();
108 }
109
SelectStartingChannel(void)110 void AnnounceSenderBase::SelectStartingChannel(void)
111 {
112 // If the starting channel is not set or it is not present
113 // in the channel mask, then start from the first channel
114 // in the mask.
115
116 VerifyOrExit(!mChannelMask.IsEmpty());
117 VerifyOrExit((mStartingChannel == kChannelIteratorFirst) || !mChannelMask.ContainsChannel(mStartingChannel));
118
119 mStartingChannel = kChannelIteratorFirst;
120 IgnoreError(mChannelMask.GetNextChannel(mStartingChannel));
121
122 exit:
123 return;
124 }
125
HandleTimer(void)126 void AnnounceSenderBase::HandleTimer(void)
127 {
128 Get<Mle::MleRouter>().SendAnnounce(mChannel);
129
130 // Go to the next channel in the mask. If we have reached the end
131 // of the channel mask, we start over from the first channel in
132 // the mask. Once we get back to `mStartingChannel` we have
133 // finished one full cycle and can decrement `mCount`.
134
135 while (mChannelMask.GetNextChannel(mChannel) != kErrorNone)
136 {
137 mChannel = kChannelIteratorFirst;
138 }
139
140 if ((mChannel == mStartingChannel) && (mCount != 0))
141 {
142 mCount--;
143 VerifyOrExit(mCount != 0);
144 }
145
146 mTimer.Start(Random::NonCrypto::AddJitter(mPeriod, mJitter));
147
148 exit:
149 return;
150 }
151
152 //---------------------------------------------------------------------------------------------------------------------
153 // AnnounceSender
154
155 #if OPENTHREAD_CONFIG_ANNOUNCE_SENDER_ENABLE
156
AnnounceSender(Instance & aInstance)157 AnnounceSender::AnnounceSender(Instance &aInstance)
158 : AnnounceSenderBase(aInstance, AnnounceSender::HandleTimer)
159 , mTrickleTimer(aInstance, AnnounceSender::HandleTrickleTimer)
160 {
161 SetJitter(kMaxJitter);
162 }
163
UpdateOnReceivedAnnounce(void)164 void AnnounceSender::UpdateOnReceivedAnnounce(void)
165 {
166 mTrickleTimer.IndicateConsistent();
167 }
168
Stop(void)169 void AnnounceSender::Stop(void)
170 {
171 AnnounceSenderBase::Stop();
172 mTrickleTimer.Stop();
173 LogInfo("Stopped");
174 }
175
HandleTimer(Timer & aTimer)176 void AnnounceSender::HandleTimer(Timer &aTimer)
177 {
178 aTimer.Get<AnnounceSender>().AnnounceSenderBase::HandleTimer();
179 }
180
HandleTrickleTimer(TrickleTimer & aTimer)181 void AnnounceSender::HandleTrickleTimer(TrickleTimer &aTimer)
182 {
183 aTimer.Get<AnnounceSender>().HandleTrickleTimer();
184 }
185
HandleTrickleTimer(void)186 void AnnounceSender::HandleTrickleTimer(void)
187 {
188 // The trickle timer handler is called when
189 // we do not receive enough Announce messages
190 // within the current interval and therefore
191 // the device itself needs to send Announce.
192 // We then request one more cycle of Announce
193 // message transmissions.
194
195 SendAnnounce(1);
196 LogInfo("Schedule tx for one cycle");
197 }
198
HandleNotifierEvents(Events aEvents)199 void AnnounceSender::HandleNotifierEvents(Events aEvents)
200 {
201 if (aEvents.Contains(kEventThreadRoleChanged))
202 {
203 HandleRoleChanged();
204 }
205
206 if (aEvents.Contains(kEventActiveDatasetChanged))
207 {
208 HandleActiveDatasetChanged();
209 }
210
211 if (aEvents.Contains(kEventThreadChannelChanged))
212 {
213 HandleThreadChannelChanged();
214 }
215 }
216
HandleRoleChanged(void)217 void AnnounceSender::HandleRoleChanged(void)
218 {
219 switch (Get<Mle::Mle>().GetRole())
220 {
221 case Mle::kRoleLeader:
222 case Mle::kRoleRouter:
223 break;
224
225 case Mle::kRoleChild:
226 #if OPENTHREAD_FTD
227 if (Get<Mle::MleRouter>().IsRouterEligible() && Get<Mle::Mle>().IsRxOnWhenIdle())
228 {
229 break;
230 }
231 #endif
232
233 OT_FALL_THROUGH;
234
235 case Mle::kRoleDisabled:
236 case Mle::kRoleDetached:
237 Stop();
238 ExitNow();
239 }
240
241 // Start the trickle timer with same min and max interval as the
242 // desired Announce Tx cycle interval.
243
244 mTrickleTimer.Start(TrickleTimer::kModeTrickle, kInterval, kInterval, kRedundancyConstant);
245 LogInfo("Started");
246
247 exit:
248 return;
249 }
250
HandleActiveDatasetChanged(void)251 void AnnounceSender::HandleActiveDatasetChanged(void)
252 {
253 Mac::ChannelMask channelMask;
254
255 SuccessOrExit(Get<MeshCoP::ActiveDatasetManager>().GetChannelMask(channelMask));
256 VerifyOrExit(!channelMask.IsEmpty());
257
258 VerifyOrExit(channelMask != GetChannelMask());
259
260 SetChannelMask(channelMask);
261 SetPeriod(kTxInterval / channelMask.GetNumberOfChannels());
262 LogInfo("ChannelMask:%s, period:%u", GetChannelMask().ToString().AsCString(), GetPeriod());
263
264 // When channel mask is changed, we also check and update the PAN
265 // channel. This handles the case where `ThreadChannelChanged` event
266 // may be received and processed before `ActiveDatasetChanged`
267 // event.
268
269 HandleThreadChannelChanged();
270
271 exit:
272 return;
273 }
274
HandleThreadChannelChanged(void)275 void AnnounceSender::HandleThreadChannelChanged(void)
276 {
277 SetStartingChannel(Get<Mac::Mac>().GetPanChannel());
278 LogInfo("StartingChannel:%d", GetStartingChannel());
279 }
280
281 #endif // OPENTHREAD_CONFIG_ANNOUNCE_SENDER_ENABLE
282
283 } // namespace ot
284