Contiki-NG
Loading...
Searching...
No Matches
timer.c
Go to the documentation of this file.
1/*
2 * Copyright (c) 2004, 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>
32 *
33 */
34
35/**
36 * \file
37 * Timer library implementation.
38 * \author
39 * Adam Dunkels <adam@sics.se>
40 */
41
42/**
43 * \addtogroup timer
44 * @{
45 */
46
47#include "contiki.h"
48#include "sys/clock.h"
49#include "sys/timer.h"
50
51/*---------------------------------------------------------------------------*/
52/**
53 * Set a timer.
54 *
55 * This function is used to set a timer for a time sometime in the
56 * future. The function timer_expired() will evaluate to true after
57 * the timer has expired.
58 *
59 * \param t A pointer to the timer
60 * \param interval The interval before the timer expires.
61 *
62 */
63void
64timer_set(struct timer *t, clock_time_t interval)
65{
66 t->interval = interval;
67 t->start = clock_time();
68}
69/*---------------------------------------------------------------------------*/
70/**
71 * Reset the timer with the same interval.
72 *
73 * This function resets the timer with the same interval that was
74 * given to the timer_set() function. The start point of the interval
75 * is the exact time that the timer last expired. Therefore, this
76 * function will cause the timer to be stable over time, unlike the
77 * timer_restart() function. If this is executed before the
78 * timer expired, this function has no effect.
79 *
80 * \param t A pointer to the timer.
81 * \sa timer_restart()
82 */
83void
85{
86 if(timer_expired(t)) {
87 t->start += t->interval;
88 }
89}
90/*---------------------------------------------------------------------------*/
91/**
92 * Restart the timer from the current point in time
93 *
94 * This function restarts a timer with the same interval that was
95 * given to the timer_set() function. The timer will start at the
96 * current time.
97 *
98 * \note A periodic timer will drift if this function is used to reset
99 * it. For preioric timers, use the timer_reset() function instead.
100 *
101 * \param t A pointer to the timer.
102 *
103 * \sa timer_reset()
104 */
105void
107{
108 t->start = clock_time();
109}
110/*---------------------------------------------------------------------------*/
111/**
112 * Check if a timer has expired.
113 *
114 * This function tests if a timer has expired and returns true or
115 * false depending on its status.
116 *
117 * \param t A pointer to the timer
118 *
119 * \return True if the timer has expired.
120 *
121 */
122bool
124{
125 /* Note: Can not return diff >= t->interval so we add 1 to diff and return
126 t->interval < diff - required to avoid an internal error in mspgcc. */
127 clock_time_t diff = (clock_time() - t->start) + 1;
128 return t->interval < diff;
129
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 */
142clock_time_t
144{
145 return t->start + t->interval - clock_time();
146}
147/*---------------------------------------------------------------------------*/
148
149/** @} */
clock_time_t clock_time(void)
Get the current clock time.
Definition clock.c:118
void timer_set(struct timer *t, clock_time_t interval)
Set a timer.
Definition timer.c:64
clock_time_t timer_remaining(struct timer *t)
The time until the timer expires.
Definition timer.c:143
void timer_restart(struct timer *t)
Restart the timer from the current point in time.
Definition timer.c:106
bool timer_expired(struct timer *t)
Check if a timer has expired.
Definition timer.c:123
void timer_reset(struct timer *t)
Reset the timer with the same interval.
Definition timer.c:84
A timer.
Definition timer.h:84
Timer library header file.