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