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.
10 * Copyright (c) 2001-2004 Swedish Institute of Computer Science.
11 * All rights reserved.
13 * Redistribution and use in source and binary forms, with or without modification,
14 * are permitted provided that the following conditions are met:
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.
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
35 * This file is part of the lwIP TCP/IP stack.
37 * Author: Adam Dunkels <adam@sics.se>
44 #include "lwip/timers.h"
45 #include "lwip/tcp_impl.h"
50 #include "lwip/memp.h"
51 #include "lwip/tcpip.h"
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"
60 #include "lwip/ip6_frag.h"
61 #include "lwip/mld6.h"
63 #include "lwip/pbuf.h"
65 /** The one and only timeout list */
66 static struct sys_timeo *next_timeout;
68 static u32_t timeouts_last_time;
72 /** global variable that shows if the tcp timer is currently scheduled or not */
73 static int tcpip_tcp_timer_active;
76 * Timer callback function that calls tcp_tmr() and reschedules itself.
78 * @param arg unused argument
81 tcpip_tcp_timer(void *arg)
85 /* call TCP timer handler */
87 /* timer still needed? */
88 if (tcp_active_pcbs || tcp_tw_pcbs) {
90 sys_timeout(TCP_TMR_INTERVAL, tcpip_tcp_timer, NULL);
93 tcpip_tcp_timer_active = 0;
98 * Called from TCP_REG when registering a new PCB:
99 * the reason is to have the TCP timer only running when
100 * there are active (or time-wait) PCBs.
103 tcp_timer_needed(void)
105 /* timer is off but needed again? */
106 if (!tcpip_tcp_timer_active && (tcp_active_pcbs || tcp_tw_pcbs)) {
107 /* enable and start timer */
108 tcpip_tcp_timer_active = 1;
109 sys_timeout(TCP_TMR_INTERVAL, tcpip_tcp_timer, NULL);
112 #endif /* LWIP_TCP */
116 * Timer callback function that calls ip_reass_tmr() and reschedules itself.
118 * @param arg unused argument
121 ip_reass_timer(void *arg)
123 LWIP_UNUSED_ARG(arg);
124 LWIP_DEBUGF(TIMERS_DEBUG, ("tcpip: ip_reass_tmr()\n"));
126 sys_timeout(IP_TMR_INTERVAL, ip_reass_timer, NULL);
128 #endif /* IP_REASSEMBLY */
132 * Timer callback function that calls etharp_tmr() and reschedules itself.
134 * @param arg unused argument
139 LWIP_UNUSED_ARG(arg);
140 LWIP_DEBUGF(TIMERS_DEBUG, ("tcpip: etharp_tmr()\n"));
142 sys_timeout(ARP_TMR_INTERVAL, arp_timer, NULL);
144 #endif /* LWIP_ARP */
148 * Timer callback function that calls dhcp_coarse_tmr() and reschedules itself.
150 * @param arg unused argument
153 dhcp_timer_coarse(void *arg)
155 LWIP_UNUSED_ARG(arg);
156 LWIP_DEBUGF(TIMERS_DEBUG, ("tcpip: dhcp_coarse_tmr()\n"));
158 sys_timeout(DHCP_COARSE_TIMER_MSECS, dhcp_timer_coarse, NULL);
162 * Timer callback function that calls dhcp_fine_tmr() and reschedules itself.
164 * @param arg unused argument
167 dhcp_timer_fine(void *arg)
169 LWIP_UNUSED_ARG(arg);
170 LWIP_DEBUGF(TIMERS_DEBUG, ("tcpip: dhcp_fine_tmr()\n"));
172 sys_timeout(DHCP_FINE_TIMER_MSECS, dhcp_timer_fine, NULL);
174 #endif /* LWIP_DHCP */
178 * Timer callback function that calls autoip_tmr() and reschedules itself.
180 * @param arg unused argument
183 autoip_timer(void *arg)
185 LWIP_UNUSED_ARG(arg);
186 LWIP_DEBUGF(TIMERS_DEBUG, ("tcpip: autoip_tmr()\n"));
188 sys_timeout(AUTOIP_TMR_INTERVAL, autoip_timer, NULL);
190 #endif /* LWIP_AUTOIP */
194 * Timer callback function that calls igmp_tmr() and reschedules itself.
196 * @param arg unused argument
199 igmp_timer(void *arg)
201 LWIP_UNUSED_ARG(arg);
202 LWIP_DEBUGF(TIMERS_DEBUG, ("tcpip: igmp_tmr()\n"));
204 sys_timeout(IGMP_TMR_INTERVAL, igmp_timer, NULL);
206 #endif /* LWIP_IGMP */
210 * Timer callback function that calls dns_tmr() and reschedules itself.
212 * @param arg unused argument
217 LWIP_UNUSED_ARG(arg);
218 LWIP_DEBUGF(TIMERS_DEBUG, ("tcpip: dns_tmr()\n"));
220 sys_timeout(DNS_TMR_INTERVAL, dns_timer, NULL);
222 #endif /* LWIP_DNS */
226 * Timer callback function that calls nd6_tmr() and reschedules itself.
228 * @param arg unused argument
233 LWIP_UNUSED_ARG(arg);
234 LWIP_DEBUGF(TIMERS_DEBUG, ("tcpip: nd6_tmr()\n"));
236 sys_timeout(ND6_TMR_INTERVAL, nd6_timer, NULL);
241 * Timer callback function that calls ip6_reass_tmr() and reschedules itself.
243 * @param arg unused argument
246 ip6_reass_timer(void *arg)
248 LWIP_UNUSED_ARG(arg);
249 LWIP_DEBUGF(TIMERS_DEBUG, ("tcpip: ip6_reass_tmr()\n"));
251 sys_timeout(IP6_REASS_TMR_INTERVAL, ip6_reass_timer, NULL);
253 #endif /* LWIP_IPV6_REASS */
257 * Timer callback function that calls mld6_tmr() and reschedules itself.
259 * @param arg unused argument
262 mld6_timer(void *arg)
264 LWIP_UNUSED_ARG(arg);
265 LWIP_DEBUGF(TIMERS_DEBUG, ("tcpip: mld6_tmr()\n"));
267 sys_timeout(MLD6_TMR_INTERVAL, mld6_timer, NULL);
269 #endif /* LWIP_IPV6_MLD */
270 #endif /* LWIP_IPV6 */
272 /** Initialize this module */
273 void sys_timeouts_init(void)
276 sys_timeout(IP_TMR_INTERVAL, ip_reass_timer, NULL);
277 #endif /* IP_REASSEMBLY */
279 sys_timeout(ARP_TMR_INTERVAL, arp_timer, NULL);
280 #endif /* LWIP_ARP */
282 sys_timeout(DHCP_COARSE_TIMER_MSECS, dhcp_timer_coarse, NULL);
283 sys_timeout(DHCP_FINE_TIMER_MSECS, dhcp_timer_fine, NULL);
284 #endif /* LWIP_DHCP */
286 sys_timeout(AUTOIP_TMR_INTERVAL, autoip_timer, NULL);
287 #endif /* LWIP_AUTOIP */
289 sys_timeout(IGMP_TMR_INTERVAL, igmp_timer, NULL);
290 #endif /* LWIP_IGMP */
292 sys_timeout(DNS_TMR_INTERVAL, dns_timer, NULL);
293 #endif /* LWIP_DNS */
295 sys_timeout(ND6_TMR_INTERVAL, nd6_timer, NULL);
297 sys_timeout(IP6_REASS_TMR_INTERVAL, ip6_reass_timer, NULL);
298 #endif /* LWIP_IPV6_REASS */
300 sys_timeout(MLD6_TMR_INTERVAL, mld6_timer, NULL);
301 #endif /* LWIP_IPV6_MLD */
302 #endif /* LWIP_IPV6 */
305 /* Initialise timestamp for sys_check_timeouts */
306 timeouts_last_time = sys_now();
311 * Create a one-shot timer (aka timeout). Timeouts are processed in the
313 * - while waiting for a message using sys_timeouts_mbox_fetch()
314 * - by calling sys_check_timeouts() (NO_SYS==1 only)
316 * @param msecs time in milliseconds after that the timer should expire
317 * @param handler callback function to call when msecs have elapsed
318 * @param arg argument to pass to the callback function
320 #if LWIP_DEBUG_TIMERNAMES
322 sys_timeout_debug(u32_t msecs, sys_timeout_handler handler, void *arg, const char* handler_name)
323 #else /* LWIP_DEBUG_TIMERNAMES */
325 sys_timeout(u32_t msecs, sys_timeout_handler handler, void *arg)
326 #endif /* LWIP_DEBUG_TIMERNAMES */
328 struct sys_timeo *timeout, *t;
330 timeout = (struct sys_timeo *)memp_malloc(MEMP_SYS_TIMEOUT);
331 if (timeout == NULL) {
332 LWIP_ASSERT("sys_timeout: timeout != NULL, pool MEMP_SYS_TIMEOUT is empty", timeout != NULL);
335 timeout->next = NULL;
336 timeout->h = handler;
338 timeout->time = msecs;
339 #if LWIP_DEBUG_TIMERNAMES
340 timeout->handler_name = handler_name;
341 LWIP_DEBUGF(TIMERS_DEBUG, ("sys_timeout: %p msecs=%"U32_F" handler=%s arg=%p\n",
342 (void *)timeout, msecs, handler_name, (void *)arg));
343 #endif /* LWIP_DEBUG_TIMERNAMES */
345 if (next_timeout == NULL) {
346 next_timeout = timeout;
350 if (next_timeout->time > msecs) {
351 next_timeout->time -= msecs;
352 timeout->next = next_timeout;
353 next_timeout = timeout;
355 for(t = next_timeout; t != NULL; t = t->next) {
356 timeout->time -= t->time;
357 if (t->next == NULL || t->next->time > timeout->time) {
358 if (t->next != NULL) {
359 t->next->time -= timeout->time;
361 timeout->next = t->next;
370 * Go through timeout list (for this task only) and remove the first matching
371 * entry, even though the timeout has not triggered yet.
373 * @note This function only works as expected if there is only one timeout
374 * calling 'handler' in the list of timeouts.
376 * @param handler callback function that would be called by the timeout
377 * @param arg callback argument that would be passed to handler
380 sys_untimeout(sys_timeout_handler handler, void *arg)
382 struct sys_timeo *prev_t, *t;
384 if (next_timeout == NULL) {
388 for (t = next_timeout, prev_t = NULL; t != NULL; prev_t = t, t = t->next) {
389 if ((t->h == handler) && (t->arg == arg)) {
390 /* We have a match */
391 /* Unlink from previous in list */
392 if (prev_t == NULL) {
393 next_timeout = t->next;
395 prev_t->next = t->next;
397 /* If not the last one, add time of this one back to next */
398 if (t->next != NULL) {
399 t->next->time += t->time;
401 memp_free(MEMP_SYS_TIMEOUT, t);
410 /** Handle timeouts for NO_SYS==1 (i.e. without using
411 * tcpip_thread/sys_timeouts_mbox_fetch(). Uses sys_now() to call timeout
412 * handler functions when timeouts expire.
414 * Must be called periodically from your main loop.
417 sys_check_timeouts(void)
420 struct sys_timeo *tmptimeout;
422 sys_timeout_handler handler;
428 /* this cares for wraparounds */
429 diff = LWIP_U32_DIFF(now, timeouts_last_time);
432 #if PBUF_POOL_FREE_OOSEQ
433 PBUF_CHECK_FREE_OOSEQ();
434 #endif /* PBUF_POOL_FREE_OOSEQ */
436 tmptimeout = next_timeout;
437 if (tmptimeout && (tmptimeout->time <= diff)) {
438 /* timeout has expired */
440 timeouts_last_time = now;
441 diff -= tmptimeout->time;
442 next_timeout = tmptimeout->next;
443 handler = tmptimeout->h;
444 arg = tmptimeout->arg;
445 #if LWIP_DEBUG_TIMERNAMES
446 if (handler != NULL) {
447 LWIP_DEBUGF(TIMERS_DEBUG, ("sct calling h=%s arg=%p\n",
448 tmptimeout->handler_name, arg));
450 #endif /* LWIP_DEBUG_TIMERNAMES */
451 memp_free(MEMP_SYS_TIMEOUT, tmptimeout);
452 if (handler != NULL) {
456 /* repeat until all expired timers have been called */
461 /** Set back the timestamp of the last call to sys_check_timeouts()
462 * This is necessary if sys_check_timeouts() hasn't been called for a long
463 * time (e.g. while saving energy) to prevent all timer functions of that
464 * period being called.
467 sys_restart_timeouts(void)
469 timeouts_last_time = sys_now();
475 * Wait (forever) for a message to arrive in an mbox.
476 * While waiting, timeouts are processed.
478 * @param mbox the mbox to fetch the message from
479 * @param msg the place to store the message
482 sys_timeouts_mbox_fetch(sys_mbox_t *mbox, void **msg)
485 struct sys_timeo *tmptimeout;
486 sys_timeout_handler handler;
491 time_needed = sys_arch_mbox_fetch(mbox, msg, 0);
493 if (next_timeout->time > 0) {
494 time_needed = sys_arch_mbox_fetch(mbox, msg, next_timeout->time);
496 time_needed = SYS_ARCH_TIMEOUT;
499 if (time_needed == SYS_ARCH_TIMEOUT) {
500 /* If time == SYS_ARCH_TIMEOUT, a timeout occured before a message
501 could be fetched. We should now call the timeout handler and
502 deallocate the memory allocated for the timeout. */
503 tmptimeout = next_timeout;
504 next_timeout = tmptimeout->next;
505 handler = tmptimeout->h;
506 arg = tmptimeout->arg;
507 #if LWIP_DEBUG_TIMERNAMES
508 if (handler != NULL) {
509 LWIP_DEBUGF(TIMERS_DEBUG, ("stmf calling h=%s arg=%p\n",
510 tmptimeout->handler_name, arg));
512 #endif /* LWIP_DEBUG_TIMERNAMES */
513 memp_free(MEMP_SYS_TIMEOUT, tmptimeout);
514 if (handler != NULL) {
515 /* For LWIP_TCPIP_CORE_LOCKING, lock the core before calling the
516 timeout handler function. */
521 LWIP_TCPIP_THREAD_ALIVE();
523 /* We try again to fetch a message from the mbox. */
526 /* If time != SYS_ARCH_TIMEOUT, a message was received before the timeout
527 occured. The time variable is set to the number of
528 milliseconds we waited for the message. */
529 if (time_needed < next_timeout->time) {
530 next_timeout->time -= time_needed;
532 next_timeout->time = 0;
540 #else /* LWIP_TIMERS */
541 /* Satisfy the TCP code which calls this function */
543 tcp_timer_needed(void)
546 #endif /* LWIP_TIMERS */