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