Contiki 2.5
ctimer.h
Go to the documentation of this file.
1 /**
2  * \addtogroup sys
3  * @{
4  */
5 
6 /**
7  * \defgroup ctimer Callback timer
8  * @{
9  *
10  * The ctimer module provides a timer mechanism that calls a specified
11  * C function when a ctimer expires.
12  *
13  */
14 
15 /*
16  * Copyright (c) 2006, Swedish Institute of Computer Science.
17  * All rights reserved.
18  *
19  * Redistribution and use in source and binary forms, with or without
20  * modification, are permitted provided that the following conditions
21  * are met:
22  * 1. Redistributions of source code must retain the above copyright
23  * notice, this list of conditions and the following disclaimer.
24  * 2. Redistributions in binary form must reproduce the above copyright
25  * notice, this list of conditions and the following disclaimer in the
26  * documentation and/or other materials provided with the distribution.
27  * 3. Neither the name of the Institute nor the names of its contributors
28  * may be used to endorse or promote products derived from this software
29  * without specific prior written permission.
30  *
31  * THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND
32  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
33  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
34  * ARE DISCLAIMED. IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE
35  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
36  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
37  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
38  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
39  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
40  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
41  * SUCH DAMAGE.
42  *
43  * This file is part of the Contiki operating system.
44  *
45  * $Id: ctimer.h,v 1.2 2010/06/14 07:35:53 adamdunkels Exp $
46  */
47 
48 /**
49  * \file
50  * Header file for the callback timer
51  * \author
52  * Adam Dunkels <adam@sics.se>
53  */
54 
55 #ifndef __CTIMER_H__
56 #define __CTIMER_H__
57 
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.
80  *
81  * \sa ctimer_restart()
82  */
83 void ctimer_reset(struct ctimer *c);
84 
85 /**
86  * \brief Restart a callback timer from the current point in time
87  * \param c A pointer to the callback timer.
88  *
89  * This function restarts the callback timer with the same
90  * interval that was given to the ctimer_set()
91  * function. The callback timer will start at the current
92  * time.
93  *
94  * \note A periodic timer will drift if this function is
95  * used to reset it. For periodic timers, use the
96  * ctimer_reset() function instead.
97  *
98  * \sa ctimer_reset()
99  */
100 void ctimer_restart(struct ctimer *c);
101 
102 /**
103  * \brief Set a callback timer.
104  * \param c A pointer to the callback timer.
105  * \param t The interval before the timer expires.
106  * \param f A function to be called when the timer expires.
107  * \param ptr An opaque pointer that will be supplied as an argument to the callback function.
108  *
109  * This function is used to set a callback timer for a time
110  * sometime in the future. When the callback timer expires,
111  * the callback function f will be called with ptr as argument.
112  *
113  */
114 void ctimer_set(struct ctimer *c, clock_time_t t,
115  void (*f)(void *), void *ptr);
116 
117 /**
118  * \brief Stop a pending callback timer.
119  * \param c A pointer to the pending callback timer.
120  *
121  * This function stops a callback timer that has previously
122  * been set with ctimer_set(), ctimer_reset(), or ctimer_restart().
123  * After this function has been called, the callback timer will be
124  * expired and will not call the callback function.
125  *
126  */
127 void ctimer_stop(struct ctimer *c);
128 
129 /**
130  * \brief Check if a callback timer has expired.
131  * \param c A pointer to the callback timer
132  * \return Non-zero if the timer has expired, zero otherwise.
133  *
134  * This function tests if a callback timer has expired and
135  * returns true or false depending on its status.
136  */
137 int ctimer_expired(struct ctimer *c);
138 
139 /**
140  * \brief Initialize the callback timer library.
141  *
142  * This function initializes the callback timer library and
143  * should be called from the system boot up code.
144  */
145 void ctimer_init(void);
146 
147 #endif /* __CTIMER_H__ */
148 /** @} */
149 /** @} */