Contiki-NG
stimer.c
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2004, 2008, 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>, Nicolas Tsiftes <nvt@sics.se>
32  *
33  */
34 
35 /**
36  * \file
37  * Timer of seconds library implementation.
38  * \author
39  * Adam Dunkels <adam@sics.se>, Nicolas Tsiftes <nvt@sics.se>
40  */
41 
42 /**
43  * \addtogroup stimer
44  * @{
45  */
46 
47 #include "contiki.h"
48 #include "sys/clock.h"
49 #include "sys/stimer.h"
50 
51 #define SCLOCK_GEQ(a, b) ((unsigned long)((a) - (b)) < \
52  ((unsigned long)(~((unsigned long)0)) >> 1))
53 
54 /*---------------------------------------------------------------------------*/
55 /**
56  * Set a timer.
57  *
58  * This function is used to set a timer for a time sometime in the
59  * future. The function stimer_expired() will evaluate to true after
60  * the timer has expired.
61  *
62  * \param t A pointer to the timer
63  * \param interval The interval before the timer expires.
64  *
65  */
66 void
67 stimer_set(struct stimer *t, unsigned long interval)
68 {
69  t->interval = interval;
70  t->start = clock_seconds();
71 }
72 /*---------------------------------------------------------------------------*/
73 /**
74  * Reset the timer with the same interval.
75  *
76  * This function resets the timer with the same interval that was
77  * given to the stimer_set() function. The start point of the interval
78  * is the exact time that the timer last expired. Therefore, this
79  * function will cause the timer to be stable over time, unlike the
80  * stimer_restart() function. If this is executed before the
81  * timer expired, this function has no effect.
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  if(stimer_expired(t)) {
91  t->start += t->interval;
92  }
93 }
94 /*---------------------------------------------------------------------------*/
95 /**
96  * Restart the timer from the current point in time
97  *
98  * This function restarts a timer with the same interval that was
99  * given to the stimer_set() function. The timer will start at the
100  * current time.
101  *
102  * \note A periodic timer will drift if this function is used to reset
103  * it. For preioric timers, use the stimer_reset() function instead.
104  *
105  * \param t A pointer to the timer.
106  *
107  * \sa stimer_reset()
108  */
109 void
111 {
112  t->start = clock_seconds();
113 }
114 /*---------------------------------------------------------------------------*/
115 /**
116  * Check if a timer has expired.
117  *
118  * This function tests if a timer has expired and returns true or
119  * false depending on its status.
120  *
121  * \param t A pointer to the timer
122  *
123  * \return Non-zero if the timer has expired, zero otherwise.
124  *
125  */
126 int
128 {
129  return SCLOCK_GEQ(clock_seconds(), t->start + t->interval);
130 }
131 /*---------------------------------------------------------------------------*/
132 /**
133  * The time until the timer expires
134  *
135  * This function returns the time until the timer expires.
136  *
137  * \param t A pointer to the timer
138  *
139  * \return The time until the timer expires
140  *
141  */
142 unsigned long
144 {
145  return t->start + t->interval - clock_seconds();
146 }
147 /*---------------------------------------------------------------------------*/
148 /**
149  * The time elapsed since the timer started
150  *
151  * This function returns the time elapsed.
152  *
153  * \param t A pointer to the timer
154  *
155  * \return The time elapsed since the last start of the timer
156  *
157  */
158 unsigned long
160 {
161  return clock_seconds() - t->start;
162 }
163 
164 /*---------------------------------------------------------------------------*/
165 
166 /** @} */
unsigned long stimer_remaining(struct stimer *t)
The time until the timer expires.
Definition: stimer.c:143
int stimer_expired(struct stimer *t)
Check if a timer has expired.
Definition: stimer.c:127
void stimer_restart(struct stimer *t)
Restart the timer from the current point in time.
Definition: stimer.c:110
unsigned long clock_seconds(void)
Get the current value of the platform seconds.
Definition: clock.c:130
unsigned long stimer_elapsed(struct stimer *t)
The time elapsed since the timer started.
Definition: stimer.c:159
void stimer_set(struct stimer *t, unsigned long interval)
Set a timer.
Definition: stimer.c:67
void stimer_reset(struct stimer *t)
Reset the timer with the same interval.
Definition: stimer.c:88
A timer.
Definition: stimer.h:81
Second timer library header file.