source: UsbWattMeter/trunk/lwip-1.4.1/src/core/timers.c@ 167

Last change on this file since 167 was 167, checked in by coas-nagasima, 6 years ago

MIMEにSJISを設定

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id
  • Property svn:mime-type set to text/x-csrc; charset=SHIFT_JIS
File size: 12.9 KB
Line 
1/**
2 * @file
3 * Stack-internal timers implementation.
4 * This file includes timer callbacks for stack-internal timers as well as
5 * functions to set up or stop timers and check for expired timers.
6 *
7 */
8
9/*
10 * Copyright (c) 2001-2004 Swedish Institute of Computer Science.
11 * All rights reserved.
12 *
13 * Redistribution and use in source and binary forms, with or without modification,
14 * are permitted provided that the following conditions are met:
15 *
16 * 1. Redistributions of source code must retain the above copyright notice,
17 * this list of conditions and the following disclaimer.
18 * 2. Redistributions in binary form must reproduce the above copyright notice,
19 * this list of conditions and the following disclaimer in the documentation
20 * and/or other materials provided with the distribution.
21 * 3. The name of the author may not be used to endorse or promote products
22 * derived from this software without specific prior written permission.
23 *
24 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
25 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
26 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT
27 * SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
28 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT
29 * OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
30 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
31 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
32 * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY
33 * OF SUCH DAMAGE.
34 *
35 * This file is part of the lwIP TCP/IP stack.
36 *
37 * Author: Adam Dunkels <adam@sics.se>
38 * Simon Goldschmidt
39 *
40 */
41
42#include "lwip/opt.h"
43
44#include "lwip/timers.h"
45#include "lwip/tcp_impl.h"
46
47#if LWIP_TIMERS
48
49#include "lwip/def.h"
50#include "lwip/memp.h"
51#include "lwip/tcpip.h"
52
53#include "lwip/ip_frag.h"
54#include "netif/etharp.h"
55#include "lwip/dhcp.h"
56#include "lwip/autoip.h"
57#include "lwip/igmp.h"
58#include "lwip/dns.h"
59#include "lwip/sys.h"
60#include "lwip/pbuf.h"
61
62
63/** The one and only timeout list */
64static struct sys_timeo *next_timeout;
65#if NO_SYS
66static u32_t timeouts_last_time;
67#endif /* NO_SYS */
68
69#if LWIP_TCP
70/** global variable that shows if the tcp timer is currently scheduled or not */
71static int tcpip_tcp_timer_active;
72
73/**
74 * Timer callback function that calls tcp_tmr() and reschedules itself.
75 *
76 * @param arg unused argument
77 */
78static void
79tcpip_tcp_timer(void *arg)
80{
81 LWIP_UNUSED_ARG(arg);
82
83 /* call TCP timer handler */
84 tcp_tmr();
85 /* timer still needed? */
86 if (tcp_active_pcbs || tcp_tw_pcbs) {
87 /* restart timer */
88 sys_timeout(TCP_TMR_INTERVAL, tcpip_tcp_timer, NULL);
89 } else {
90 /* disable timer */
91 tcpip_tcp_timer_active = 0;
92 }
93}
94
95/**
96 * Called from TCP_REG when registering a new PCB:
97 * the reason is to have the TCP timer only running when
98 * there are active (or time-wait) PCBs.
99 */
100void
101tcp_timer_needed(void)
102{
103 /* timer is off but needed again? */
104 if (!tcpip_tcp_timer_active && (tcp_active_pcbs || tcp_tw_pcbs)) {
105 /* enable and start timer */
106 tcpip_tcp_timer_active = 1;
107 sys_timeout(TCP_TMR_INTERVAL, tcpip_tcp_timer, NULL);
108 }
109}
110#endif /* LWIP_TCP */
111
112#if IP_REASSEMBLY
113/**
114 * Timer callback function that calls ip_reass_tmr() and reschedules itself.
115 *
116 * @param arg unused argument
117 */
118static void
119ip_reass_timer(void *arg)
120{
121 LWIP_UNUSED_ARG(arg);
122 LWIP_DEBUGF(TIMERS_DEBUG, ("tcpip: ip_reass_tmr()\n"));
123 ip_reass_tmr();
124 sys_timeout(IP_TMR_INTERVAL, ip_reass_timer, NULL);
125}
126#endif /* IP_REASSEMBLY */
127
128#if LWIP_ARP
129/**
130 * Timer callback function that calls etharp_tmr() and reschedules itself.
131 *
132 * @param arg unused argument
133 */
134static void
135arp_timer(void *arg)
136{
137 LWIP_UNUSED_ARG(arg);
138 LWIP_DEBUGF(TIMERS_DEBUG, ("tcpip: etharp_tmr()\n"));
139 etharp_tmr();
140 sys_timeout(ARP_TMR_INTERVAL, arp_timer, NULL);
141}
142#endif /* LWIP_ARP */
143
144#if LWIP_DHCP
145/**
146 * Timer callback function that calls dhcp_coarse_tmr() and reschedules itself.
147 *
148 * @param arg unused argument
149 */
150static void
151dhcp_timer_coarse(void *arg)
152{
153 LWIP_UNUSED_ARG(arg);
154 LWIP_DEBUGF(TIMERS_DEBUG, ("tcpip: dhcp_coarse_tmr()\n"));
155 dhcp_coarse_tmr();
156 sys_timeout(DHCP_COARSE_TIMER_MSECS, dhcp_timer_coarse, NULL);
157}
158
159/**
160 * Timer callback function that calls dhcp_fine_tmr() and reschedules itself.
161 *
162 * @param arg unused argument
163 */
164static void
165dhcp_timer_fine(void *arg)
166{
167 LWIP_UNUSED_ARG(arg);
168 LWIP_DEBUGF(TIMERS_DEBUG, ("tcpip: dhcp_fine_tmr()\n"));
169 dhcp_fine_tmr();
170 sys_timeout(DHCP_FINE_TIMER_MSECS, dhcp_timer_fine, NULL);
171}
172#endif /* LWIP_DHCP */
173
174#if LWIP_AUTOIP
175/**
176 * Timer callback function that calls autoip_tmr() and reschedules itself.
177 *
178 * @param arg unused argument
179 */
180static void
181autoip_timer(void *arg)
182{
183 LWIP_UNUSED_ARG(arg);
184 LWIP_DEBUGF(TIMERS_DEBUG, ("tcpip: autoip_tmr()\n"));
185 autoip_tmr();
186 sys_timeout(AUTOIP_TMR_INTERVAL, autoip_timer, NULL);
187}
188#endif /* LWIP_AUTOIP */
189
190#if LWIP_IGMP
191/**
192 * Timer callback function that calls igmp_tmr() and reschedules itself.
193 *
194 * @param arg unused argument
195 */
196static void
197igmp_timer(void *arg)
198{
199 LWIP_UNUSED_ARG(arg);
200 LWIP_DEBUGF(TIMERS_DEBUG, ("tcpip: igmp_tmr()\n"));
201 igmp_tmr();
202 sys_timeout(IGMP_TMR_INTERVAL, igmp_timer, NULL);
203}
204#endif /* LWIP_IGMP */
205
206#if LWIP_DNS
207/**
208 * Timer callback function that calls dns_tmr() and reschedules itself.
209 *
210 * @param arg unused argument
211 */
212static void
213dns_timer(void *arg)
214{
215 LWIP_UNUSED_ARG(arg);
216 LWIP_DEBUGF(TIMERS_DEBUG, ("tcpip: dns_tmr()\n"));
217 dns_tmr();
218 sys_timeout(DNS_TMR_INTERVAL, dns_timer, NULL);
219}
220#endif /* LWIP_DNS */
221
222/** Initialize this module */
223void sys_timeouts_init(void)
224{
225#if IP_REASSEMBLY
226 sys_timeout(IP_TMR_INTERVAL, ip_reass_timer, NULL);
227#endif /* IP_REASSEMBLY */
228#if LWIP_ARP
229 sys_timeout(ARP_TMR_INTERVAL, arp_timer, NULL);
230#endif /* LWIP_ARP */
231#if LWIP_DHCP
232 sys_timeout(DHCP_COARSE_TIMER_MSECS, dhcp_timer_coarse, NULL);
233 sys_timeout(DHCP_FINE_TIMER_MSECS, dhcp_timer_fine, NULL);
234#endif /* LWIP_DHCP */
235#if LWIP_AUTOIP
236 sys_timeout(AUTOIP_TMR_INTERVAL, autoip_timer, NULL);
237#endif /* LWIP_AUTOIP */
238#if LWIP_IGMP
239 sys_timeout(IGMP_TMR_INTERVAL, igmp_timer, NULL);
240#endif /* LWIP_IGMP */
241#if LWIP_DNS
242 sys_timeout(DNS_TMR_INTERVAL, dns_timer, NULL);
243#endif /* LWIP_DNS */
244
245#if NO_SYS
246 /* Initialise timestamp for sys_check_timeouts */
247 timeouts_last_time = sys_now();
248#endif
249}
250
251/**
252 * Create a one-shot timer (aka timeout). Timeouts are processed in the
253 * following cases:
254 * - while waiting for a message using sys_timeouts_mbox_fetch()
255 * - by calling sys_check_timeouts() (NO_SYS==1 only)
256 *
257 * @param msecs time in milliseconds after that the timer should expire
258 * @param handler callback function to call when msecs have elapsed
259 * @param arg argument to pass to the callback function
260 */
261#if LWIP_DEBUG_TIMERNAMES
262void
263sys_timeout_debug(u32_t msecs, sys_timeout_handler handler, void *arg, const char* handler_name)
264#else /* LWIP_DEBUG_TIMERNAMES */
265void
266sys_timeout(u32_t msecs, sys_timeout_handler handler, void *arg)
267#endif /* LWIP_DEBUG_TIMERNAMES */
268{
269 struct sys_timeo *timeout, *t;
270
271 timeout = (struct sys_timeo *)memp_malloc(MEMP_SYS_TIMEOUT);
272 if (timeout == NULL) {
273 LWIP_ASSERT("sys_timeout: timeout != NULL, pool MEMP_SYS_TIMEOUT is empty", timeout != NULL);
274 return;
275 }
276 timeout->next = NULL;
277 timeout->h = handler;
278 timeout->arg = arg;
279 timeout->time = msecs;
280#if LWIP_DEBUG_TIMERNAMES
281 timeout->handler_name = handler_name;
282 LWIP_DEBUGF(TIMERS_DEBUG, ("sys_timeout: %p msecs=%"U32_F" handler=%s arg=%p\n",
283 (void *)timeout, msecs, handler_name, (void *)arg));
284#endif /* LWIP_DEBUG_TIMERNAMES */
285
286 if (next_timeout == NULL) {
287 next_timeout = timeout;
288 return;
289 }
290
291 if (next_timeout->time > msecs) {
292 next_timeout->time -= msecs;
293 timeout->next = next_timeout;
294 next_timeout = timeout;
295 } else {
296 for(t = next_timeout; t != NULL; t = t->next) {
297 timeout->time -= t->time;
298 if (t->next == NULL || t->next->time > timeout->time) {
299 if (t->next != NULL) {
300 t->next->time -= timeout->time;
301 }
302 timeout->next = t->next;
303 t->next = timeout;
304 break;
305 }
306 }
307 }
308}
309
310/**
311 * Go through timeout list (for this task only) and remove the first matching
312 * entry, even though the timeout has not triggered yet.
313 *
314 * @note This function only works as expected if there is only one timeout
315 * calling 'handler' in the list of timeouts.
316 *
317 * @param handler callback function that would be called by the timeout
318 * @param arg callback argument that would be passed to handler
319*/
320void
321sys_untimeout(sys_timeout_handler handler, void *arg)
322{
323 struct sys_timeo *prev_t, *t;
324
325 if (next_timeout == NULL) {
326 return;
327 }
328
329 for (t = next_timeout, prev_t = NULL; t != NULL; prev_t = t, t = t->next) {
330 if ((t->h == handler) && (t->arg == arg)) {
331 /* We have a match */
332 /* Unlink from previous in list */
333 if (prev_t == NULL) {
334 next_timeout = t->next;
335 } else {
336 prev_t->next = t->next;
337 }
338 /* If not the last one, add time of this one back to next */
339 if (t->next != NULL) {
340 t->next->time += t->time;
341 }
342 memp_free(MEMP_SYS_TIMEOUT, t);
343 return;
344 }
345 }
346 return;
347}
348
349#if NO_SYS
350
351/** Handle timeouts for NO_SYS==1 (i.e. without using
352 * tcpip_thread/sys_timeouts_mbox_fetch(). Uses sys_now() to call timeout
353 * handler functions when timeouts expire.
354 *
355 * Must be called periodically from your main loop.
356 */
357void
358sys_check_timeouts(void)
359{
360 if (next_timeout) {
361 struct sys_timeo *tmptimeout;
362 u32_t diff;
363 sys_timeout_handler handler;
364 void *arg;
365 u8_t had_one;
366 u32_t now;
367
368 now = sys_now();
369 /* this cares for wraparounds */
370 diff = now - timeouts_last_time;
371 do
372 {
373#if PBUF_POOL_FREE_OOSEQ
374 PBUF_CHECK_FREE_OOSEQ();
375#endif /* PBUF_POOL_FREE_OOSEQ */
376 had_one = 0;
377 tmptimeout = next_timeout;
378 if (tmptimeout && (tmptimeout->time <= diff)) {
379 /* timeout has expired */
380 had_one = 1;
381 timeouts_last_time = now;
382 diff -= tmptimeout->time;
383 next_timeout = tmptimeout->next;
384 handler = tmptimeout->h;
385 arg = tmptimeout->arg;
386#if LWIP_DEBUG_TIMERNAMES
387 if (handler != NULL) {
388 LWIP_DEBUGF(TIMERS_DEBUG, ("sct calling h=%s arg=%p\n",
389 tmptimeout->handler_name, arg));
390 }
391#endif /* LWIP_DEBUG_TIMERNAMES */
392 memp_free(MEMP_SYS_TIMEOUT, tmptimeout);
393 if (handler != NULL) {
394 handler(arg);
395 }
396 }
397 /* repeat until all expired timers have been called */
398 }while(had_one);
399 }
400}
401
402/** Set back the timestamp of the last call to sys_check_timeouts()
403 * This is necessary if sys_check_timeouts() hasn't been called for a long
404 * time (e.g. while saving energy) to prevent all timer functions of that
405 * period being called.
406 */
407void
408sys_restart_timeouts(void)
409{
410 timeouts_last_time = sys_now();
411}
412
413#else /* NO_SYS */
414
415/**
416 * Wait (forever) for a message to arrive in an mbox.
417 * While waiting, timeouts are processed.
418 *
419 * @param mbox the mbox to fetch the message from
420 * @param msg the place to store the message
421 */
422void
423sys_timeouts_mbox_fetch(sys_mbox_t *mbox, void **msg)
424{
425 u32_t time_needed;
426 struct sys_timeo *tmptimeout;
427 sys_timeout_handler handler;
428 void *arg;
429
430 again:
431 if (!next_timeout) {
432 time_needed = sys_arch_mbox_fetch(mbox, msg, 0);
433 } else {
434 if (next_timeout->time > 0) {
435 time_needed = sys_arch_mbox_fetch(mbox, msg, next_timeout->time);
436 } else {
437 time_needed = SYS_ARCH_TIMEOUT;
438 }
439
440 if (time_needed == SYS_ARCH_TIMEOUT) {
441 /* If time == SYS_ARCH_TIMEOUT, a timeout occured before a message
442 could be fetched. We should now call the timeout handler and
443 deallocate the memory allocated for the timeout. */
444 tmptimeout = next_timeout;
445 next_timeout = tmptimeout->next;
446 handler = tmptimeout->h;
447 arg = tmptimeout->arg;
448#if LWIP_DEBUG_TIMERNAMES
449 if (handler != NULL) {
450 LWIP_DEBUGF(TIMERS_DEBUG, ("stmf calling h=%s arg=%p\n",
451 tmptimeout->handler_name, arg));
452 }
453#endif /* LWIP_DEBUG_TIMERNAMES */
454 memp_free(MEMP_SYS_TIMEOUT, tmptimeout);
455 if (handler != NULL) {
456 /* For LWIP_TCPIP_CORE_LOCKING, lock the core before calling the
457 timeout handler function. */
458 LOCK_TCPIP_CORE();
459 handler(arg);
460 UNLOCK_TCPIP_CORE();
461 }
462 LWIP_TCPIP_THREAD_ALIVE();
463
464 /* We try again to fetch a message from the mbox. */
465 goto again;
466 } else {
467 /* If time != SYS_ARCH_TIMEOUT, a message was received before the timeout
468 occured. The time variable is set to the number of
469 milliseconds we waited for the message. */
470 if (time_needed < next_timeout->time) {
471 next_timeout->time -= time_needed;
472 } else {
473 next_timeout->time = 0;
474 }
475 }
476 }
477}
478
479#endif /* NO_SYS */
480
481#else /* LWIP_TIMERS */
482/* Satisfy the TCP code which calls this function */
483void
484tcp_timer_needed(void)
485{
486}
487#endif /* LWIP_TIMERS */
Note: See TracBrowser for help on using the repository browser.