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/locator_getters.hpp"
40 #include "common/log.hpp"
41 #include "common/random.hpp"
42 #include "instance/instance.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) { mTrickleTimer.IndicateConsistent(); }
165
Stop(void)166 void AnnounceSender::Stop(void)
167 {
168 AnnounceSenderBase::Stop();
169 mTrickleTimer.Stop();
170 LogInfo("Stopped");
171 }
172
HandleTimer(Timer & aTimer)173 void AnnounceSender::HandleTimer(Timer &aTimer) { aTimer.Get<AnnounceSender>().AnnounceSenderBase::HandleTimer(); }
174
HandleTrickleTimer(TrickleTimer & aTimer)175 void AnnounceSender::HandleTrickleTimer(TrickleTimer &aTimer) { aTimer.Get<AnnounceSender>().HandleTrickleTimer(); }
176
HandleTrickleTimer(void)177 void AnnounceSender::HandleTrickleTimer(void)
178 {
179 // The trickle timer handler is called when
180 // we do not receive enough Announce messages
181 // within the current interval and therefore
182 // the device itself needs to send Announce.
183 // We then request one more cycle of Announce
184 // message transmissions.
185
186 SendAnnounce(1);
187 LogInfo("Schedule tx for one cycle");
188 }
189
HandleNotifierEvents(Events aEvents)190 void AnnounceSender::HandleNotifierEvents(Events aEvents)
191 {
192 if (aEvents.Contains(kEventThreadRoleChanged))
193 {
194 HandleRoleChanged();
195 }
196
197 if (aEvents.Contains(kEventActiveDatasetChanged))
198 {
199 HandleActiveDatasetChanged();
200 }
201
202 if (aEvents.Contains(kEventThreadChannelChanged))
203 {
204 HandleThreadChannelChanged();
205 }
206 }
207
HandleRoleChanged(void)208 void AnnounceSender::HandleRoleChanged(void)
209 {
210 switch (Get<Mle::Mle>().GetRole())
211 {
212 case Mle::kRoleLeader:
213 case Mle::kRoleRouter:
214 break;
215
216 case Mle::kRoleChild:
217 #if OPENTHREAD_FTD
218 if (Get<Mle::MleRouter>().IsRouterEligible() && Get<Mle::Mle>().IsRxOnWhenIdle())
219 {
220 break;
221 }
222 #endif
223
224 OT_FALL_THROUGH;
225
226 case Mle::kRoleDisabled:
227 case Mle::kRoleDetached:
228 Stop();
229 ExitNow();
230 }
231
232 // Start the trickle timer with same min and max interval as the
233 // desired Announce Tx cycle interval.
234
235 mTrickleTimer.Start(TrickleTimer::kModeTrickle, kInterval, kInterval, kRedundancyConstant);
236 LogInfo("Started");
237
238 exit:
239 return;
240 }
241
HandleActiveDatasetChanged(void)242 void AnnounceSender::HandleActiveDatasetChanged(void)
243 {
244 Mac::ChannelMask channelMask;
245
246 SuccessOrExit(Get<MeshCoP::ActiveDatasetManager>().GetChannelMask(channelMask));
247 VerifyOrExit(!channelMask.IsEmpty());
248
249 VerifyOrExit(channelMask != GetChannelMask());
250
251 SetChannelMask(channelMask);
252 SetPeriod(kTxInterval / channelMask.GetNumberOfChannels());
253 LogInfo("ChannelMask:%s, period:%lu", GetChannelMask().ToString().AsCString(), ToUlong(GetPeriod()));
254
255 // When channel mask is changed, we also check and update the PAN
256 // channel. This handles the case where `ThreadChannelChanged` event
257 // may be received and processed before `ActiveDatasetChanged`
258 // event.
259
260 HandleThreadChannelChanged();
261
262 exit:
263 return;
264 }
265
HandleThreadChannelChanged(void)266 void AnnounceSender::HandleThreadChannelChanged(void)
267 {
268 SetStartingChannel(Get<Mac::Mac>().GetPanChannel());
269 LogInfo("StartingChannel:%d", GetStartingChannel());
270 }
271
272 #endif // OPENTHREAD_CONFIG_ANNOUNCE_SENDER_ENABLE
273
274 } // namespace ot
275