1 /*
2 * Copyright (c) 2016, 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 a multiplexed timer service on top of the alarm abstraction.
32 */
33
34 #include "timer.hpp"
35
36 #include "common/code_utils.hpp"
37 #include "common/debug.hpp"
38 #include "common/instance.hpp"
39 #include "common/locator_getters.hpp"
40 #include "common/logging.hpp"
41
42 namespace ot {
43
44 const Timer::Scheduler::AlarmApi TimerMilli::Scheduler::sAlarmMilliApi = {
45 &otPlatAlarmMilliStartAt,
46 &otPlatAlarmMilliStop,
47 &otPlatAlarmMilliGetNow,
48 };
49
DoesFireBefore(const Timer & aSecondTimer,Time aNow) const50 bool Timer::DoesFireBefore(const Timer &aSecondTimer, Time aNow) const
51 {
52 // Indicates whether the fire time of this timer is strictly
53 // before the fire time of a second given timer.
54
55 bool retval;
56 bool isBeforeNow = (GetFireTime() < aNow);
57
58 // Check if one timer is before `now` and the other one is not.
59 if ((aSecondTimer.GetFireTime() < aNow) != isBeforeNow)
60 {
61 // One timer is before `now` and the other one is not, so if
62 // this timer's fire time is before `now` then the second fire
63 // time would be after `now` and this timer would fire before
64 // the second timer.
65
66 retval = isBeforeNow;
67 }
68 else
69 {
70 // Both timers are before `now` or both are after `now`. Either
71 // way the difference is guaranteed to be less than `kMaxDt` so
72 // we can safely compare the fire times directly.
73
74 retval = GetFireTime() < aSecondTimer.GetFireTime();
75 }
76
77 return retval;
78 }
79
Start(uint32_t aDelay)80 void TimerMilli::Start(uint32_t aDelay)
81 {
82 StartAt(GetNow(), aDelay);
83 }
84
StartAt(TimeMilli aStartTime,uint32_t aDelay)85 void TimerMilli::StartAt(TimeMilli aStartTime, uint32_t aDelay)
86 {
87 OT_ASSERT(aDelay <= kMaxDelay);
88 FireAt(aStartTime + aDelay);
89 }
90
FireAt(TimeMilli aFireTime)91 void TimerMilli::FireAt(TimeMilli aFireTime)
92 {
93 mFireTime = aFireTime;
94 Get<Scheduler>().Add(*this);
95 }
96
FireAtIfEarlier(TimeMilli aFireTime)97 void TimerMilli::FireAtIfEarlier(TimeMilli aFireTime)
98 {
99 if (!IsRunning() || (mFireTime > aFireTime))
100 {
101 FireAt(aFireTime);
102 }
103 }
104
Stop(void)105 void TimerMilli::Stop(void)
106 {
107 Get<Scheduler>().Remove(*this);
108 }
109
RemoveAll(Instance & aInstance)110 void TimerMilli::RemoveAll(Instance &aInstance)
111 {
112 aInstance.Get<Scheduler>().RemoveAll();
113 }
114
Add(Timer & aTimer,const AlarmApi & aAlarmApi)115 void Timer::Scheduler::Add(Timer &aTimer, const AlarmApi &aAlarmApi)
116 {
117 Timer *prev = nullptr;
118 Time now(aAlarmApi.AlarmGetNow());
119
120 Remove(aTimer, aAlarmApi);
121
122 for (Timer &cur : mTimerList)
123 {
124 if (aTimer.DoesFireBefore(cur, now))
125 {
126 break;
127 }
128
129 prev = &cur;
130 }
131
132 if (prev == nullptr)
133 {
134 mTimerList.Push(aTimer);
135 SetAlarm(aAlarmApi);
136 }
137 else
138 {
139 mTimerList.PushAfter(aTimer, *prev);
140 }
141 }
142
Remove(Timer & aTimer,const AlarmApi & aAlarmApi)143 void Timer::Scheduler::Remove(Timer &aTimer, const AlarmApi &aAlarmApi)
144 {
145 VerifyOrExit(aTimer.IsRunning());
146
147 if (mTimerList.GetHead() == &aTimer)
148 {
149 mTimerList.Pop();
150 SetAlarm(aAlarmApi);
151 }
152 else
153 {
154 IgnoreError(mTimerList.Remove(aTimer));
155 }
156
157 aTimer.SetNext(&aTimer);
158
159 exit:
160 return;
161 }
162
SetAlarm(const AlarmApi & aAlarmApi)163 void Timer::Scheduler::SetAlarm(const AlarmApi &aAlarmApi)
164 {
165 if (mTimerList.IsEmpty())
166 {
167 aAlarmApi.AlarmStop(&GetInstance());
168 }
169 else
170 {
171 Timer * timer = mTimerList.GetHead();
172 Time now(aAlarmApi.AlarmGetNow());
173 uint32_t remaining;
174
175 remaining = (now < timer->mFireTime) ? (timer->mFireTime - now) : 0;
176
177 aAlarmApi.AlarmStartAt(&GetInstance(), now.GetValue(), remaining);
178 }
179 }
180
ProcessTimers(const AlarmApi & aAlarmApi)181 void Timer::Scheduler::ProcessTimers(const AlarmApi &aAlarmApi)
182 {
183 Timer *timer = mTimerList.GetHead();
184
185 if (timer)
186 {
187 Time now(aAlarmApi.AlarmGetNow());
188
189 if (now >= timer->mFireTime)
190 {
191 Remove(*timer, aAlarmApi); // `Remove()` will `SetAlarm` for next timer if there is any.
192 timer->Fired();
193 ExitNow();
194 }
195 }
196
197 SetAlarm(aAlarmApi);
198
199 exit:
200 return;
201 }
202
RemoveAll(const AlarmApi & aAlarmApi)203 void Timer::Scheduler::RemoveAll(const AlarmApi &aAlarmApi)
204 {
205 Timer *timer;
206
207 while ((timer = mTimerList.Pop()) != nullptr)
208 {
209 timer->SetNext(timer);
210 }
211
212 SetAlarm(aAlarmApi);
213 }
214
otPlatAlarmMilliFired(otInstance * aInstance)215 extern "C" void otPlatAlarmMilliFired(otInstance *aInstance)
216 {
217 Instance *instance = static_cast<Instance *>(aInstance);
218
219 VerifyOrExit(otInstanceIsInitialized(aInstance));
220 instance->Get<TimerMilli::Scheduler>().ProcessTimers();
221
222 exit:
223 return;
224 }
225
226 #if OPENTHREAD_CONFIG_PLATFORM_USEC_TIMER_ENABLE
227 const Timer::Scheduler::AlarmApi TimerMicro::Scheduler::sAlarmMicroApi = {
228 &otPlatAlarmMicroStartAt,
229 &otPlatAlarmMicroStop,
230 &otPlatAlarmMicroGetNow,
231 };
232
Start(uint32_t aDelay)233 void TimerMicro::Start(uint32_t aDelay)
234 {
235 StartAt(GetNow(), aDelay);
236 }
237
StartAt(TimeMicro aStartTime,uint32_t aDelay)238 void TimerMicro::StartAt(TimeMicro aStartTime, uint32_t aDelay)
239 {
240 OT_ASSERT(aDelay <= kMaxDelay);
241 FireAt(aStartTime + aDelay);
242 }
243
FireAt(TimeMicro aFireTime)244 void TimerMicro::FireAt(TimeMicro aFireTime)
245 {
246 mFireTime = aFireTime;
247 Get<Scheduler>().Add(*this);
248 }
249
Stop(void)250 void TimerMicro::Stop(void)
251 {
252 Get<Scheduler>().Remove(*this);
253 }
254
RemoveAll(Instance & aInstance)255 void TimerMicro::RemoveAll(Instance &aInstance)
256 {
257 aInstance.Get<Scheduler>().RemoveAll();
258 }
259
otPlatAlarmMicroFired(otInstance * aInstance)260 extern "C" void otPlatAlarmMicroFired(otInstance *aInstance)
261 {
262 Instance *instance = static_cast<Instance *>(aInstance);
263
264 VerifyOrExit(otInstanceIsInitialized(aInstance));
265 instance->Get<TimerMicro::Scheduler>().ProcessTimers();
266
267 exit:
268 return;
269 }
270 #endif // OPENTHREAD_CONFIG_PLATFORM_USEC_TIMER_ENABLE
271
272 } // namespace ot
273