Contiki-NG
ctimer.h
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2006, 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  */
32 
33 /**
34  * \file
35  * Header file for the callback timer
36  * \author
37  * Adam Dunkels <adam@sics.se>
38  */
39 
40 /**
41  * \addtogroup timers
42  * @{
43  */
44 
45 /**
46  * \defgroup ctimer Callback timer
47  * @{
48  *
49  * The ctimer module provides a timer mechanism that calls a specified
50  * C function when a ctimer expires.
51  *
52  */
53 
54 #ifndef CTIMER_H_
55 #define CTIMER_H_
56 
57 #include "contiki.h"
58 #include "sys/etimer.h"
59 
60 struct ctimer {
61  struct ctimer *next;
62  struct etimer etimer;
63  struct process *p;
64  void (*f)(void *);
65  void *ptr;
66 };
67 
68 /**
69  * \brief Reset a callback timer with the same interval as was
70  * previously set.
71  * \param c A pointer to the callback timer.
72  *
73  * This function resets the callback timer with the same
74  * interval that was given to the callback timer with the
75  * ctimer_set() function. The start point of the interval
76  * is the exact time that the callback timer last
77  * expired. Therefore, this function will cause the timer
78  * to be stable over time, unlike the ctimer_restart()
79  * function. If this is executed before the timer expired,
80  * this function has no effect.
81  *
82  * \sa ctimer_restart()
83  */
84 void ctimer_reset(struct ctimer *c);
85 
86 /**
87  * \brief Restart a callback timer from the current point in time
88  * \param c A pointer to the callback timer.
89  *
90  * This function restarts the callback timer with the same
91  * interval that was given to the ctimer_set()
92  * function. The callback timer will start at the current
93  * time.
94  *
95  * \note A periodic timer will drift if this function is
96  * used to reset it. For periodic timers, use the
97  * ctimer_reset() function instead.
98  *
99  * \sa ctimer_reset()
100  */
101 void ctimer_restart(struct ctimer *c);
102 
103 /**
104  * \brief Set a callback timer.
105  * \param c A pointer to the callback timer.
106  * \param t The interval before the timer expires.
107  * \param f A function to be called when the timer expires.
108  * \param ptr An opaque pointer that will be supplied as an argument to the callback function.
109  *
110  * This function is used to set a callback timer for a time
111  * sometime in the future. When the callback timer expires,
112  * the callback function f will be called with ptr as argument.
113  *
114  * This essentially does ctimer_set_process(c, t, f, ptr, PROCESS_CURRENT());
115  *
116  */
117 void ctimer_set(struct ctimer *c, clock_time_t t,
118  void (*f)(void *), void *ptr);
119 
120 /**
121  * \brief Set a callback timer.
122  * \param c A pointer to the callback timer.
123  * \param t The interval before the timer expires.
124  * \param f A function to be called when the timer expires.
125  * \param ptr An opaque pointer that will be supplied as an argument to the callback function.
126  * \param p A pointer to the process the timer belongs to
127  *
128  * This function is used to set a callback timer for a time
129  * sometime in the future. When the callback timer expires,
130  * the callback function f will be called with ptr as argument.
131  *
132  */
133 void ctimer_set_with_process(struct ctimer *c, clock_time_t t,
134  void (*f)(void *), void *ptr, struct process *p);
135 
136 /**
137  * \brief Stop a pending callback timer.
138  * \param c A pointer to the pending callback timer.
139  *
140  * This function stops a callback timer that has previously
141  * been set with ctimer_set(), ctimer_reset(), or ctimer_restart().
142  * After this function has been called, the callback timer will be
143  * expired and will not call the callback function.
144  *
145  */
146 void ctimer_stop(struct ctimer *c);
147 
148 /**
149  * \brief Check if a callback timer has expired.
150  * \param c A pointer to the callback timer
151  * \return Non-zero if the timer has expired, zero otherwise.
152  *
153  * This function tests if a callback timer has expired and
154  * returns true or false depending on its status.
155  */
156 int ctimer_expired(struct ctimer *c);
157 
158 /**
159  * \brief Initialize the callback timer library.
160  *
161  * This function initializes the callback timer library and
162  * should be called from the system boot up code.
163  */
164 void ctimer_init(void);
165 
166 #endif /* CTIMER_H_ */
167 /** @} */
168 /** @} */
int ctimer_expired(struct ctimer *c)
Check if a callback timer has expired.
Definition: ctimer.c:161
void ctimer_stop(struct ctimer *c)
Stop a pending callback timer.
Definition: ctimer.c:149
void ctimer_set_with_process(struct ctimer *c, clock_time_t t, void(*f)(void *), void *ptr, struct process *p)
Set a callback timer.
Definition: ctimer.c:106
void ctimer_init(void)
Initialize the callback timer library.
Definition: ctimer.c:91
void ctimer_reset(struct ctimer *c)
Reset a callback timer with the same interval as was previously set.
Definition: ctimer.c:125
Event timer header file.
void ctimer_set(struct ctimer *c, clock_time_t t, void(*f)(void *), void *ptr)
Set a callback timer.
Definition: ctimer.c:99
void ctimer_restart(struct ctimer *c)
Restart a callback timer from the current point in time.
Definition: ctimer.c:137
A timer.
Definition: etimer.h:75