stimer.c

Go to the documentation of this file.
00001 /**
00002  * \addtogroup stimer
00003  * @{
00004  */
00005 
00006 /**
00007  * \file
00008  * Timer of seconds library implementation.
00009  * \author
00010  * Adam Dunkels <adam@sics.se>, Nicolas Tsiftes <nvt@sics.se>
00011  */
00012 
00013 /*
00014  * Copyright (c) 2004, 2008, Swedish Institute of Computer Science.
00015  * All rights reserved.
00016  *
00017  * Redistribution and use in source and binary forms, with or without
00018  * modification, are permitted provided that the following conditions
00019  * are met:
00020  * 1. Redistributions of source code must retain the above copyright
00021  *    notice, this list of conditions and the following disclaimer.
00022  * 2. Redistributions in binary form must reproduce the above copyright
00023  *    notice, this list of conditions and the following disclaimer in the
00024  *    documentation and/or other materials provided with the distribution.
00025  * 3. Neither the name of the Institute nor the names of its contributors
00026  *    may be used to endorse or promote products derived from this software
00027  *    without specific prior written permission.
00028  *
00029  * THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND
00030  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
00031  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
00032  * ARE DISCLAIMED.  IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE
00033  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
00034  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
00035  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
00036  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
00037  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
00038  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
00039  * SUCH DAMAGE.
00040  *
00041  * This file is part of the Contiki operating system.
00042  *
00043  * Author: Adam Dunkels <adam@sics.se>, Nicolas Tsiftes <nvt@sics.se>
00044  *
00045  * $Id: stimer.c,v 1.3 2010/03/15 15:53:57 joxe Exp $
00046  */
00047 
00048 #include "contiki-conf.h"
00049 #include "sys/clock.h"
00050 #include "sys/stimer.h"
00051 
00052 #define SCLOCK_GEQ(a, b)        ((unsigned long)((a) - (b)) < \
00053                                 ((unsigned long)(~((unsigned long)0)) >> 1))
00054 
00055 /*---------------------------------------------------------------------------*/
00056 /**
00057  * Set a timer.
00058  *
00059  * This function is used to set a timer for a time sometime in the
00060  * future. The function stimer_expired() will evaluate to true after
00061  * the timer has expired.
00062  *
00063  * \param t A pointer to the timer
00064  * \param interval The interval before the timer expires.
00065  *
00066  */
00067 void
00068 stimer_set(struct stimer *t, unsigned long interval)
00069 {
00070   t->interval = interval;
00071   t->start = clock_seconds();
00072 }
00073 /*---------------------------------------------------------------------------*/
00074 /**
00075  * Reset the timer with the same interval.
00076  *
00077  * This function resets the timer with the same interval that was
00078  * given to the stimer_set() function. The start point of the interval
00079  * is the exact time that the timer last expired. Therefore, this
00080  * function will cause the timer to be stable over time, unlike the
00081  * stimer_restart() function.
00082  *
00083  * \param t A pointer to the timer.
00084  *
00085  * \sa stimer_restart()
00086  */
00087 void
00088 stimer_reset(struct stimer *t)
00089 {
00090   t->start += t->interval;
00091 }
00092 /*---------------------------------------------------------------------------*/
00093 /**
00094  * Restart the timer from the current point in time
00095  *
00096  * This function restarts a timer with the same interval that was
00097  * given to the stimer_set() function. The timer will start at the
00098  * current time.
00099  *
00100  * \note A periodic timer will drift if this function is used to reset
00101  * it. For preioric timers, use the stimer_reset() function instead.
00102  *
00103  * \param t A pointer to the timer.
00104  *
00105  * \sa stimer_reset()
00106  */
00107 void
00108 stimer_restart(struct stimer *t)
00109 {
00110   t->start = clock_seconds();
00111 }
00112 /*---------------------------------------------------------------------------*/
00113 /**
00114  * Check if a timer has expired.
00115  *
00116  * This function tests if a timer has expired and returns true or
00117  * false depending on its status.
00118  *
00119  * \param t A pointer to the timer
00120  *
00121  * \return Non-zero if the timer has expired, zero otherwise.
00122  *
00123  */
00124 int
00125 stimer_expired(struct stimer *t)
00126 {
00127   return SCLOCK_GEQ(clock_seconds(), t->start + t->interval);
00128 }
00129 /*---------------------------------------------------------------------------*/
00130 /**
00131  * The time until the timer expires
00132  *
00133  * This function returns the time until the timer expires.
00134  *
00135  * \param t A pointer to the timer
00136  *
00137  * \return The time until the timer expires
00138  *
00139  */
00140 unsigned long
00141 stimer_remaining(struct stimer *t)
00142 {
00143   return t->start + t->interval - clock_seconds();
00144 }
00145 /*---------------------------------------------------------------------------*/
00146 /**
00147  * The time elapsed since the timer started
00148  *
00149  * This function returns the time elapsed.
00150  *
00151  * \param t A pointer to the timer
00152  *
00153  * \return The time elapsed since the last start of the timer
00154  *
00155  */
00156 unsigned long
00157 stimer_elapsed(struct stimer *t)
00158 {
00159   return clock_seconds() - t->start;
00160 }
00161 
00162 /*---------------------------------------------------------------------------*/
00163 
00164 /** @} */

Generated on Mon Apr 11 14:23:33 2011 for Contiki 2.5 by  doxygen 1.6.1