0.01.00
timer.hpp
Go to the documentation of this file.
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 
34 #ifndef TIMER_HPP_
35 #define TIMER_HPP_
36 
37 #include "openthread-core-config.h"
38 
39 #include <stddef.h>
40 #include "utils/wrap_stdint.h"
41 
42 #include <openthread/types.h>
45 
46 #include "common/context.hpp"
47 #include "common/debug.hpp"
48 #include "common/locator.hpp"
49 #include "common/tasklet.hpp"
50 
51 namespace ot {
52 
53 class TimerMilliScheduler;
54 
69 class Timer: public InstanceLocator, public Context
70 {
71  friend class TimerScheduler;
72 
73 public:
74 
75  enum
76  {
77  kMaxDt = (1UL << 31) - 1, //< Maximum permitted value for parameter `aDt` in `Start` and `StartAt` method.
78  };
79 
86  typedef void (*Handler)(Timer &aTimer);
87 
96  Timer(otInstance &aInstance, Handler aHandler, void *aContext):
97  InstanceLocator(aInstance),
98  Context(aContext),
99  mHandler(aHandler),
100  mFireTime(0),
101  mNext(this) {
102  }
103 
110  uint32_t GetFireTime(void) const { return mFireTime; }
111 
119  bool IsRunning(void) const { return (mNext != this); }
120 
121 protected:
132  bool DoesFireBefore(const Timer &aTimer, uint32_t aNow);
133 
134  void Fired(void) { mHandler(*this); }
135 
136  Handler mHandler;
137  uint32_t mFireTime;
138  Timer *mNext;
139 };
140 
145 class TimerMilli: public Timer
146 {
147 public:
156  TimerMilli(otInstance &aInstance, Handler aHandler, void *aContext):
157  Timer(aInstance, aHandler, aContext) {
158  }
159 
167  void Start(uint32_t aDt) { StartAt(GetNow(), aDt); }
168 
177  void StartAt(uint32_t aT0, uint32_t aDt);
178 
183  void Stop(void);
184 
191  static uint32_t GetNow(void) { return otPlatAlarmMilliGetNow(); }
192 
199  static uint32_t SecToMsec(uint32_t aSeconds) { return aSeconds * 1000u; }
200 
207  static uint32_t MsecToSec(uint32_t aMilliseconds) { return aMilliseconds / 1000u; }
208 
209 private:
216  TimerMilliScheduler &GetTimerMilliScheduler(void) const;
217 };
218 
219 
225 {
226  friend class Timer;
227 
228 protected:
233  struct AlarmApi
234  {
235  void (*AlarmStartAt)(otInstance *aInstance, uint32_t aT0, uint32_t aDt);
236  void (*AlarmStop)(otInstance *aInstance);
237  uint32_t (*AlarmGetNow)(void);
238  };
239 
247  InstanceLocator(aInstance),
248  mHead(NULL) {
249  }
250 
258  void Add(Timer &aTimer, const AlarmApi &aAlarmApi);
259 
267  void Remove(Timer &aTimer, const AlarmApi &aAlarmApi);
268 
275  void ProcessTimers(const AlarmApi &aAlarmApi);
276 
283  void SetAlarm(const AlarmApi &aAlarmApi);
284 
298  static bool IsStrictlyBefore(uint32_t aTimeA, uint32_t aTimeB);
299 
300  Timer *mHead;
301 };
302 
308 {
309 public:
317  TimerScheduler(aInstance) {
318  }
319 
326  void Add(TimerMilli &aTimer) { TimerScheduler::Add(aTimer, sAlarmMilliApi); }
327 
334  void Remove(TimerMilli &aTimer) { TimerScheduler::Remove(aTimer, sAlarmMilliApi); };
335 
340  void ProcessTimers(void) { TimerScheduler::ProcessTimers(sAlarmMilliApi); }
341 
342 private:
343  static const AlarmApi sAlarmMilliApi;
344 };
345 
346 #if OPENTHREAD_CONFIG_ENABLE_PLATFORM_USEC_TIMER
347 class TimerMicroScheduler;
348 
353 class TimerMicro: public Timer
354 {
355 public:
364  TimerMicro(otInstance &aInstance, Handler aHandler, void *aContext):
365  Timer(aInstance, aHandler, aContext) {
366  }
367 
375  void Start(uint32_t aDt) { StartAt(GetNow(), aDt); }
376 
385  void StartAt(uint32_t aT0, uint32_t aDt);
386 
391  void Stop(void);
392 
399  static uint32_t GetNow(void) { return otPlatAlarmMicroGetNow(); }
400 
401 private:
408  TimerMicroScheduler &GetTimerMicroScheduler(void) const;
409 };
410 
415 class TimerMicroScheduler: public TimerScheduler
416 {
417 public:
424  TimerMicroScheduler(otInstance &aInstance):
425  TimerScheduler(aInstance) {
426  }
427 
434  void Add(TimerMicro &aTimer) { TimerScheduler::Add(aTimer, sAlarmMicroApi); }
435 
442  void Remove(TimerMicro &aTimer) { TimerScheduler::Remove(aTimer, sAlarmMicroApi); };
443 
448  void ProcessTimers(void) { TimerScheduler::ProcessTimers(sAlarmMicroApi); }
449 
450 private:
451  static const AlarmApi sAlarmMicroApi;
452 };
453 #endif // OPENTHREAD_CONFIG_ENABLE_PLATFORM_USEC_TIMER
454 
460 } // namespace ot
461 
462 #endif // TIMER_HPP_
This type represents all the static / global variables used by OpenThread allocated in one place...
Definition: openthread-instance.h:59
Definition: cli.cpp:90
void Start(uint32_t aDt)
This method schedules the timer to fire a dt milliseconds from now.
Definition: timer.hpp:167
This file defines the types and structures used in the OpenThread library API.
void Add(TimerMilli &aTimer)
This method adds a timer instance to the timer scheduler.
Definition: timer.hpp:326
void(* Handler)(Timer &aTimer)
This function pointer is called when the timer expires.
Definition: timer.hpp:86
This file includes the platform abstraction for the microsecond alarm service.
This file includes definitions for locator class for OpenThread objects.
static uint32_t SecToMsec(uint32_t aSeconds)
This static method returns the number of milliseconds given seconds.
Definition: timer.hpp:199
TimerMilliScheduler(otInstance &aInstance)
This constructor initializes the object.
Definition: timer.hpp:316
uint32_t otPlatAlarmMicroGetNow(void)
Get the current time.
This file includes definitions for maintaining a pointer to arbitrary context information.
This class implements the base timer scheduler.
Definition: timer.hpp:224
This class implements a timer.
Definition: timer.hpp:69
This file includes the platform abstraction for the millisecond alarm service.
This class implements the millisecond timer.
Definition: timer.hpp:145
Timer(otInstance &aInstance, Handler aHandler, void *aContext)
This constructor creates a timer instance.
Definition: timer.hpp:96
void Remove(Timer &aTimer, const AlarmApi &aAlarmApi)
This method removes a timer instance to the timer scheduler.
Definition: timer.cpp:138
void ProcessTimers(const AlarmApi &aAlarmApi)
This method processes the running timers.
Definition: timer.cpp:180
uint32_t GetFireTime(void) const
This method returns the fire time of the timer.
Definition: timer.hpp:110
static uint32_t MsecToSec(uint32_t aMilliseconds)
This static method returns the number of seconds given milliseconds.
Definition: timer.hpp:207
bool IsRunning(void) const
This method indicates whether or not the timer instance is running.
Definition: timer.hpp:119
This class implements definitions for maintaining a pointer to arbitrary context information.
Definition: context.hpp:61
This file includes definitions for tasklets and the tasklet scheduler.
The Alarm APIs definition.
Definition: timer.hpp:233
This class implements locator for otInstance object.
Definition: locator.hpp:63
TimerScheduler(otInstance &aInstance)
This constructor initializes the object.
Definition: timer.hpp:246
This file includes functions for debugging.
void Remove(TimerMilli &aTimer)
This method removes a timer instance to the timer scheduler.
Definition: timer.hpp:334
void ProcessTimers(void)
This method processes the running timers.
Definition: timer.hpp:340
bool DoesFireBefore(const Timer &aTimer, uint32_t aNow)
This method indicates if the fire time of this timer is strictly before the fire time of a second giv...
Definition: timer.cpp:52
void Add(Timer &aTimer, const AlarmApi &aAlarmApi)
This method adds a timer instance to the timer scheduler.
Definition: timer.cpp:93
static uint32_t GetNow(void)
This static method returns the current time in milliseconds.
Definition: timer.hpp:191
This file includes compile-time configuration constants for OpenThread.
uint32_t otPlatAlarmMilliGetNow(void)
Get the current time.
TimerMilli(otInstance &aInstance, Handler aHandler, void *aContext)
This constructor creates a millisecond timer instance.
Definition: timer.hpp:156
This class implements the millisecond timer scheduler.
Definition: timer.hpp:307