Contiki-NG
Loading...
Searching...
No Matches
etimer.h
Go to the documentation of this file.
1/*
2 * Copyright (c) 2004, Swedish Institute of Computer Science.
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
7 * are met:
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
13 * 3. Neither the name of the Institute nor the names of its contributors
14 * may be used to endorse or promote products derived from this software
15 * without specific prior written permission.
16 *
17 * THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND
18 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20 * ARE DISCLAIMED. IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE
21 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27 * SUCH DAMAGE.
28 *
29 * This file is part of the Contiki operating system.
30 *
31 * Author: Adam Dunkels <adam@sics.se>
32 *
33 */
34
35/**
36 * \file
37 * Event timer header file.
38 * \author
39 * Adam Dunkels <adam@sics.se>
40 */
41
42/** \addtogroup timers
43 * @{ */
44
45/**
46 * \defgroup etimer Event timers
47 *
48 * Event timers provides a way to generate timed events. An event
49 * timer will post an event to the process that set the timer when the
50 * event timer expires.
51 *
52 * An event timer is declared as a \c struct \c etimer and all access
53 * to the event timer is made by a pointer to the declared event
54 * timer.
55 *
56 * \sa \ref timer "Simple timer library"
57 * \sa \ref clock "Clock library" (used by the timer library)
58 *
59 * It is \e not safe to manipulate event timers within an interrupt context.
60 * @{
61 */
62
63#ifndef ETIMER_H_
64#define ETIMER_H_
65
66#include "contiki.h"
67
68#include <stdbool.h>
69#include <stddef.h>
70
71/**
72 * A timer.
73 *
74 * This structure is used for declaring a timer. The timer must be set
75 * with etimer_set() before it can be used.
76 *
77 * \hideinitializer
78 */
79struct etimer {
80 struct timer timer;
81 struct etimer *next;
82 struct process *p;
83};
84
85/**
86 * \name Functions called from application programs
87 * @{
88 */
89
90/**
91 * \brief Set an event timer.
92 * \param et A pointer to the event timer
93 * \param interval The interval before the timer expires.
94 *
95 * This function is used to set an event timer for a time
96 * sometime in the future. When the event timer expires,
97 * the event PROCESS_EVENT_TIMER will be posted to the
98 * process that called the etimer_set() function.
99 *
100 */
101void etimer_set(struct etimer *et, clock_time_t interval);
102
103/**
104 * \brief Reset an event timer with the same interval as was
105 * previously set.
106 * \param et A pointer to the event timer.
107 *
108 * This function resets the event timer with the same
109 * interval that was given to the event timer with the
110 * etimer_set() function. The start point of the interval
111 * is the exact time that the event timer last
112 * expired. Therefore, this function will cause the timer
113 * to be stable over time, unlike the etimer_restart()
114 * function. If this is executed before the timer expired,
115 * this function has no effect.
116 *
117 * \sa etimer_restart()
118 */
119void etimer_reset(struct etimer *et);
120
121/**
122 * \brief Reset an event timer with a new interval.
123 * \param et A pointer to the event timer.
124 * \param interval The interval before the timer expires.
125 *
126 * This function very similar to etimer_reset. Opposed to
127 * etimer_reset it is possible to change the timout.
128 * This allows accurate, non-periodic timers without drift.
129 *
130 * \sa etimer_reset()
131 */
132void etimer_reset_with_new_interval(struct etimer *et, clock_time_t interval);
133
134/**
135 * \brief Restart an event timer from the current point in time
136 * \param et A pointer to the event timer.
137 *
138 * This function restarts the event timer with the same
139 * interval that was given to the etimer_set()
140 * function. The event timer will start at the current
141 * time.
142 *
143 * \note A periodic timer will drift if this function is
144 * used to reset it. For periodic timers, use the
145 * etimer_reset() function instead.
146 *
147 * \sa etimer_reset()
148 */
149void etimer_restart(struct etimer *et);
150
151/**
152 * \brief Adjust the expiration time for an event timer
153 * \param et A pointer to the event timer.
154 * \param td The time difference to adjust the expiration time with.
155 *
156 * This function is used to adjust the time the event
157 * timer will expire. It can be used to synchronize
158 * periodic timers without the need to restart the timer
159 * or change the timer interval.
160 *
161 * \note This function should only be used for small
162 * adjustments. For large adjustments use etimer_set()
163 * instead.
164 *
165 * \note A periodic timer will drift unless the
166 * etimer_reset() function is used.
167 *
168 * \sa etimer_set()
169 * \sa etimer_reset()
170 */
171void etimer_adjust(struct etimer *et, int td);
172
173/**
174 * \brief Get the expiration time for the event timer.
175 * \param et A pointer to the event timer
176 * \return The expiration time for the event timer.
177 *
178 * This function returns the expiration time for an event timer.
179 */
180clock_time_t etimer_expiration_time(struct etimer *et);
181
182/**
183 * \brief Get the start time for the event timer.
184 * \param et A pointer to the event timer
185 * \return The start time for the event timer.
186 *
187 * This function returns the start time (when the timer
188 * was last set) for an event timer.
189 */
190clock_time_t etimer_start_time(struct etimer *et);
191
192/**
193 * \brief Check if an event timer has expired.
194 * \param et A pointer to the event timer
195 * \return True if the timer has expired.
196 *
197 * This function tests if an event timer has expired and
198 * returns true or false depending on its status.
199 */
200static inline bool
202{
203 return et->p == PROCESS_NONE;
204}
205
206/**
207 * \brief Stop a pending event timer.
208 * \param et A pointer to the pending event timer.
209 *
210 * This function stops an event timer that has previously
211 * been set with etimer_set() or etimer_reset(). After
212 * this function has been called, the event timer will not
213 * emit any event when it expires.
214 *
215 */
216void etimer_stop(struct etimer *et);
217
218/** @} */
219
220/**
221 * \name Functions called from timer interrupts, by the system
222 * @{
223 */
224
225/**
226 * \brief Make the event timer aware that the clock has changed
227 *
228 * This function is used to inform the event timer module
229 * that the system clock has been updated. Typically, this
230 * function would be called from the timer interrupt
231 * handler when the clock has ticked.
232 */
233void etimer_request_poll(void);
234
235/**
236 * \brief Check if there are any non-expired event timers.
237 * \return True if there are active event timers, false if there are
238 * no active timers.
239 *
240 * This function checks if there are any active event
241 * timers that have not expired.
242 */
243int etimer_pending(void);
244
245/**
246 * \brief Get next event timer expiration time.
247 * \return Next expiration time of all pending event timers.
248 * If there are no pending event timers this function
249 * returns 0.
250 *
251 * This functions returns next expiration time of all
252 * pending event timers.
253 */
254clock_time_t etimer_next_expiration_time(void);
255
256
257/** @} */
258
259PROCESS_NAME(etimer_process);
260#endif /* ETIMER_H_ */
261/** @} */
262/** @} */
void etimer_restart(struct etimer *et)
Restart an event timer from the current point in time.
Definition etimer.c:199
clock_time_t etimer_start_time(struct etimer *et)
Get the start time for the event timer.
Definition etimer.c:219
int etimer_pending(void)
Check if there are any non-expired event timers.
Definition etimer.c:225
void etimer_reset_with_new_interval(struct etimer *et, clock_time_t interval)
Reset an event timer with a new interval.
Definition etimer.c:184
void etimer_reset(struct etimer *et)
Reset an event timer with the same interval as was previously set.
Definition etimer.c:192
void etimer_request_poll(void)
Make the event timer aware that the clock has changed.
Definition etimer.c:145
clock_time_t etimer_next_expiration_time(void)
Get next event timer expiration time.
Definition etimer.c:231
void etimer_stop(struct etimer *et)
Stop a pending event timer.
Definition etimer.c:237
static bool etimer_expired(struct etimer *et)
Check if an event timer has expired.
Definition etimer.h:201
void etimer_set(struct etimer *et, clock_time_t interval)
Set an event timer.
Definition etimer.c:177
clock_time_t etimer_expiration_time(struct etimer *et)
Get the expiration time for the event timer.
Definition etimer.c:213
void etimer_adjust(struct etimer *et, int timediff)
Adjust the expiration time for an event timer.
Definition etimer.c:206
#define PROCESS_NAME(name)
Declare the name of a process.
Definition process.h:287
A timer.
Definition etimer.h:79
A timer.
Definition timer.h:84