123
utility/uip_timer.c@1:01c2344f98a3, 2014-11-20 (annotated)
- Committer:
- hudakz
- Date:
- Thu Nov 20 21:26:54 2014 +0000
- Revision:
- 1:01c2344f98a3
- Parent:
- uitility/uip_timer.c@0:5350a66d5279
rev. 01
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
hudakz | 0:5350a66d5279 | 1 | /** |
hudakz | 0:5350a66d5279 | 2 | * \addtogroup timer |
hudakz | 0:5350a66d5279 | 3 | * @{ |
hudakz | 0:5350a66d5279 | 4 | */ |
hudakz | 0:5350a66d5279 | 5 | /** |
hudakz | 0:5350a66d5279 | 6 | * \file |
hudakz | 0:5350a66d5279 | 7 | * Timer library implementation. |
hudakz | 0:5350a66d5279 | 8 | * \author |
hudakz | 0:5350a66d5279 | 9 | * Adam Dunkels <adam@sics.se> |
hudakz | 0:5350a66d5279 | 10 | */ |
hudakz | 0:5350a66d5279 | 11 | /* |
hudakz | 0:5350a66d5279 | 12 | * Copyright (c) 2004, Swedish Institute of Computer Science. |
hudakz | 0:5350a66d5279 | 13 | * All rights reserved. |
hudakz | 0:5350a66d5279 | 14 | * |
hudakz | 0:5350a66d5279 | 15 | * Redistribution and use in source and binary forms, with or without |
hudakz | 0:5350a66d5279 | 16 | * modification, are permitted provided that the following conditions |
hudakz | 0:5350a66d5279 | 17 | * are met: |
hudakz | 0:5350a66d5279 | 18 | * 1. Redistributions of source code must retain the above copyright |
hudakz | 0:5350a66d5279 | 19 | * notice, this list of conditions and the following disclaimer. |
hudakz | 0:5350a66d5279 | 20 | * 2. Redistributions in binary form must reproduce the above copyright |
hudakz | 0:5350a66d5279 | 21 | * notice, this list of conditions and the following disclaimer in the |
hudakz | 0:5350a66d5279 | 22 | * documentation and/or other materials provided with the distribution. |
hudakz | 0:5350a66d5279 | 23 | * 3. Neither the name of the Institute nor the names of its contributors |
hudakz | 0:5350a66d5279 | 24 | * may be used to endorse or promote products derived from this software |
hudakz | 0:5350a66d5279 | 25 | * without specific prior written permission. |
hudakz | 0:5350a66d5279 | 26 | * |
hudakz | 0:5350a66d5279 | 27 | * THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND |
hudakz | 0:5350a66d5279 | 28 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
hudakz | 0:5350a66d5279 | 29 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
hudakz | 0:5350a66d5279 | 30 | * ARE DISCLAIMED. IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE |
hudakz | 0:5350a66d5279 | 31 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL |
hudakz | 0:5350a66d5279 | 32 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS |
hudakz | 0:5350a66d5279 | 33 | * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) |
hudakz | 0:5350a66d5279 | 34 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT |
hudakz | 0:5350a66d5279 | 35 | * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY |
hudakz | 0:5350a66d5279 | 36 | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF |
hudakz | 0:5350a66d5279 | 37 | * SUCH DAMAGE. |
hudakz | 0:5350a66d5279 | 38 | * |
hudakz | 0:5350a66d5279 | 39 | * This file is part of the uIP TCP/IP stack |
hudakz | 0:5350a66d5279 | 40 | * |
hudakz | 0:5350a66d5279 | 41 | * Author: Adam Dunkels <adam@sics.se> |
hudakz | 0:5350a66d5279 | 42 | * |
hudakz | 0:5350a66d5279 | 43 | * $Id: timer.c,v 1.2 2006/06/12 08:00:30 adam Exp $ |
hudakz | 0:5350a66d5279 | 44 | */ |
hudakz | 0:5350a66d5279 | 45 | #include "uip_clock.h" |
hudakz | 0:5350a66d5279 | 46 | #include "uip_timer.h" |
hudakz | 0:5350a66d5279 | 47 | |
hudakz | 0:5350a66d5279 | 48 | /*---------------------------------------------------------------------------*/ |
hudakz | 0:5350a66d5279 | 49 | |
hudakz | 0:5350a66d5279 | 50 | /** |
hudakz | 0:5350a66d5279 | 51 | * Set a timer. |
hudakz | 0:5350a66d5279 | 52 | * |
hudakz | 0:5350a66d5279 | 53 | * This function is used to set a timer for a time sometime in the |
hudakz | 0:5350a66d5279 | 54 | * future. The function timer_expired() will evaluate to true after |
hudakz | 0:5350a66d5279 | 55 | * the timer has expired. |
hudakz | 0:5350a66d5279 | 56 | * |
hudakz | 0:5350a66d5279 | 57 | * \param t A pointer to the timer |
hudakz | 0:5350a66d5279 | 58 | * \param interval The interval before the timer expires. |
hudakz | 0:5350a66d5279 | 59 | * |
hudakz | 0:5350a66d5279 | 60 | */ |
hudakz | 0:5350a66d5279 | 61 | void uip_timer_set(struct uip_timer* t, clock_time_t interval) { |
hudakz | 0:5350a66d5279 | 62 | t->interval = interval; |
hudakz | 0:5350a66d5279 | 63 | t->start = clock_time(); |
hudakz | 0:5350a66d5279 | 64 | } |
hudakz | 0:5350a66d5279 | 65 | |
hudakz | 0:5350a66d5279 | 66 | /*---------------------------------------------------------------------------*/ |
hudakz | 0:5350a66d5279 | 67 | |
hudakz | 0:5350a66d5279 | 68 | /** |
hudakz | 0:5350a66d5279 | 69 | * Reset the timer with the same interval. |
hudakz | 0:5350a66d5279 | 70 | * |
hudakz | 0:5350a66d5279 | 71 | * This function resets the timer with the same interval that was |
hudakz | 0:5350a66d5279 | 72 | * given to the timer_set() function. The start point of the interval |
hudakz | 0:5350a66d5279 | 73 | * is the exact time that the timer last expired. Therefore, this |
hudakz | 0:5350a66d5279 | 74 | * function will cause the timer to be stable over time, unlike the |
hudakz | 0:5350a66d5279 | 75 | * timer_rester() function. |
hudakz | 0:5350a66d5279 | 76 | * |
hudakz | 0:5350a66d5279 | 77 | * \param t A pointer to the timer. |
hudakz | 0:5350a66d5279 | 78 | * |
hudakz | 0:5350a66d5279 | 79 | * \sa timer_restart() |
hudakz | 0:5350a66d5279 | 80 | */ |
hudakz | 0:5350a66d5279 | 81 | void uip_timer_reset(struct uip_timer* t) { |
hudakz | 0:5350a66d5279 | 82 | t->start += t->interval; |
hudakz | 0:5350a66d5279 | 83 | } |
hudakz | 0:5350a66d5279 | 84 | |
hudakz | 0:5350a66d5279 | 85 | /*---------------------------------------------------------------------------*/ |
hudakz | 0:5350a66d5279 | 86 | |
hudakz | 0:5350a66d5279 | 87 | /** |
hudakz | 0:5350a66d5279 | 88 | * Restart the timer from the current point in time |
hudakz | 0:5350a66d5279 | 89 | * |
hudakz | 0:5350a66d5279 | 90 | * This function restarts a timer with the same interval that was |
hudakz | 0:5350a66d5279 | 91 | * given to the timer_set() function. The timer will start at the |
hudakz | 0:5350a66d5279 | 92 | * current time. |
hudakz | 0:5350a66d5279 | 93 | * |
hudakz | 0:5350a66d5279 | 94 | * \note A periodic timer will drift if this function is used to reset |
hudakz | 0:5350a66d5279 | 95 | * it. For preioric timers, use the timer_reset() function instead. |
hudakz | 0:5350a66d5279 | 96 | * |
hudakz | 0:5350a66d5279 | 97 | * \param t A pointer to the timer. |
hudakz | 0:5350a66d5279 | 98 | * |
hudakz | 0:5350a66d5279 | 99 | * \sa timer_reset() |
hudakz | 0:5350a66d5279 | 100 | */ |
hudakz | 0:5350a66d5279 | 101 | void uip_timer_restart(struct uip_timer* t) { |
hudakz | 0:5350a66d5279 | 102 | t->start = clock_time(); |
hudakz | 0:5350a66d5279 | 103 | } |
hudakz | 0:5350a66d5279 | 104 | |
hudakz | 0:5350a66d5279 | 105 | /*---------------------------------------------------------------------------*/ |
hudakz | 0:5350a66d5279 | 106 | |
hudakz | 0:5350a66d5279 | 107 | /** |
hudakz | 0:5350a66d5279 | 108 | * Check if a timer has expired. |
hudakz | 0:5350a66d5279 | 109 | * |
hudakz | 0:5350a66d5279 | 110 | * This function tests if a timer has expired and returns true or |
hudakz | 0:5350a66d5279 | 111 | * false depending on its status. |
hudakz | 0:5350a66d5279 | 112 | * |
hudakz | 0:5350a66d5279 | 113 | * \param t A pointer to the timer |
hudakz | 0:5350a66d5279 | 114 | * |
hudakz | 0:5350a66d5279 | 115 | * \return Non-zero if the timer has expired, zero otherwise. |
hudakz | 0:5350a66d5279 | 116 | * |
hudakz | 0:5350a66d5279 | 117 | */ |
hudakz | 0:5350a66d5279 | 118 | int uip_timer_expired(struct uip_timer* t) { |
hudakz | 0:5350a66d5279 | 119 | return(clock_time_t) (clock_time() - t->start) >= (clock_time_t) t->interval; |
hudakz | 0:5350a66d5279 | 120 | } |
hudakz | 0:5350a66d5279 | 121 | |
hudakz | 0:5350a66d5279 | 122 | /*---------------------------------------------------------------------------*/ |
hudakz | 0:5350a66d5279 | 123 | /** @} */ |