Contiki 2.5
stimer.c
Go to the documentation of this file.
1 /**
2  * \addtogroup stimer
3  * @{
4  */
5 
6 /**
7  * \file
8  * Timer of seconds library implementation.
9  * \author
10  * Adam Dunkels <adam@sics.se>, Nicolas Tsiftes <nvt@sics.se>
11  */
12 
13 /*
14  * Copyright (c) 2004, 2008, Swedish Institute of Computer Science.
15  * All rights reserved.
16  *
17  * Redistribution and use in source and binary forms, with or without
18  * modification, are permitted provided that the following conditions
19  * are met:
20  * 1. Redistributions of source code must retain the above copyright
21  * notice, this list of conditions and the following disclaimer.
22  * 2. Redistributions in binary form must reproduce the above copyright
23  * notice, this list of conditions and the following disclaimer in the
24  * documentation and/or other materials provided with the distribution.
25  * 3. Neither the name of the Institute nor the names of its contributors
26  * may be used to endorse or promote products derived from this software
27  * without specific prior written permission.
28  *
29  * THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND
30  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
31  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
32  * ARE DISCLAIMED. IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE
33  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
34  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
35  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
36  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
37  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
38  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
39  * SUCH DAMAGE.
40  *
41  * This file is part of the Contiki operating system.
42  *
43  * Author: Adam Dunkels <adam@sics.se>, Nicolas Tsiftes <nvt@sics.se>
44  *
45  * $Id: stimer.c,v 1.3 2010/03/15 15:53:57 joxe Exp $
46  */
47 
48 #include "contiki-conf.h"
49 #include "sys/clock.h"
50 #include "sys/stimer.h"
51 
52 #define SCLOCK_GEQ(a, b) ((unsigned long)((a) - (b)) < \
53  ((unsigned long)(~((unsigned long)0)) >> 1))
54 
55 /*---------------------------------------------------------------------------*/
56 /**
57  * Set a timer.
58  *
59  * This function is used to set a timer for a time sometime in the
60  * future. The function stimer_expired() will evaluate to true after
61  * the timer has expired.
62  *
63  * \param t A pointer to the timer
64  * \param interval The interval before the timer expires.
65  *
66  */
67 void
68 stimer_set(struct stimer *t, unsigned long interval)
69 {
70  t->interval = interval;
71  t->start = clock_seconds();
72 }
73 /*---------------------------------------------------------------------------*/
74 /**
75  * Reset the timer with the same interval.
76  *
77  * This function resets the timer with the same interval that was
78  * given to the stimer_set() function. The start point of the interval
79  * is the exact time that the timer last expired. Therefore, this
80  * function will cause the timer to be stable over time, unlike the
81  * stimer_restart() function.
82  *
83  * \param t A pointer to the timer.
84  *
85  * \sa stimer_restart()
86  */
87 void
88 stimer_reset(struct stimer *t)
89 {
90  t->start += t->interval;
91 }
92 /*---------------------------------------------------------------------------*/
93 /**
94  * Restart the timer from the current point in time
95  *
96  * This function restarts a timer with the same interval that was
97  * given to the stimer_set() function. The timer will start at the
98  * current time.
99  *
100  * \note A periodic timer will drift if this function is used to reset
101  * it. For preioric timers, use the stimer_reset() function instead.
102  *
103  * \param t A pointer to the timer.
104  *
105  * \sa stimer_reset()
106  */
107 void
109 {
110  t->start = clock_seconds();
111 }
112 /*---------------------------------------------------------------------------*/
113 /**
114  * Check if a timer has expired.
115  *
116  * This function tests if a timer has expired and returns true or
117  * false depending on its status.
118  *
119  * \param t A pointer to the timer
120  *
121  * \return Non-zero if the timer has expired, zero otherwise.
122  *
123  */
124 int
126 {
127  return SCLOCK_GEQ(clock_seconds(), t->start + t->interval);
128 }
129 /*---------------------------------------------------------------------------*/
130 /**
131  * The time until the timer expires
132  *
133  * This function returns the time until the timer expires.
134  *
135  * \param t A pointer to the timer
136  *
137  * \return The time until the timer expires
138  *
139  */
140 unsigned long
142 {
143  return t->start + t->interval - clock_seconds();
144 }
145 /*---------------------------------------------------------------------------*/
146 /**
147  * The time elapsed since the timer started
148  *
149  * This function returns the time elapsed.
150  *
151  * \param t A pointer to the timer
152  *
153  * \return The time elapsed since the last start of the timer
154  *
155  */
156 unsigned long
158 {
159  return clock_seconds() - t->start;
160 }
161 
162 /*---------------------------------------------------------------------------*/
163 
164 /** @} */