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"
46 #include "lwip/memp.h"
47 #include "lwip/tcpip.h"
49 #include "lwip/tcp_impl.h"
50 #include "lwip/ip_frag.h"
51 #include "netif/etharp.h"
52 #include "lwip/dhcp.h"
53 #include "lwip/autoip.h"
54 #include "lwip/igmp.h"
58 /** The one and only timeout list */
59 static struct sys_timeo *next_timeout;
61 static u32_t timeouts_last_time;
65 /** global variable that shows if the tcp timer is currently scheduled or not */
66 static int tcpip_tcp_timer_active;
69 * Timer callback function that calls tcp_tmr() and reschedules itself.
71 * @param arg unused argument
74 tcpip_tcp_timer(void *arg)
78 /* call TCP timer handler */
80 /* timer still needed? */
81 if (tcp_active_pcbs || tcp_tw_pcbs) {
83 sys_timeout(TCP_TMR_INTERVAL, tcpip_tcp_timer, NULL);
86 tcpip_tcp_timer_active = 0;
91 * Called from TCP_REG when registering a new PCB:
92 * the reason is to have the TCP timer only running when
93 * there are active (or time-wait) PCBs.
96 tcp_timer_needed(void)
98 /* timer is off but needed again? */
99 if (!tcpip_tcp_timer_active && (tcp_active_pcbs || tcp_tw_pcbs)) {
100 /* enable and start timer */
101 tcpip_tcp_timer_active = 1;
102 sys_timeout(TCP_TMR_INTERVAL, tcpip_tcp_timer, NULL);
105 #endif /* LWIP_TCP */
109 * Timer callback function that calls ip_reass_tmr() and reschedules itself.
111 * @param arg unused argument
114 ip_reass_timer(void *arg)
116 LWIP_UNUSED_ARG(arg);
117 LWIP_DEBUGF(TIMERS_DEBUG, ("tcpip: ip_reass_tmr()\n"));
119 sys_timeout(IP_TMR_INTERVAL, ip_reass_timer, NULL);
121 #endif /* IP_REASSEMBLY */
125 * Timer callback function that calls etharp_tmr() and reschedules itself.
127 * @param arg unused argument
132 LWIP_UNUSED_ARG(arg);
133 LWIP_DEBUGF(TIMERS_DEBUG, ("tcpip: etharp_tmr()\n"));
135 sys_timeout(ARP_TMR_INTERVAL, arp_timer, NULL);
137 #endif /* LWIP_ARP */
141 * Timer callback function that calls dhcp_coarse_tmr() and reschedules itself.
143 * @param arg unused argument
146 dhcp_timer_coarse(void *arg)
148 LWIP_UNUSED_ARG(arg);
149 LWIP_DEBUGF(TIMERS_DEBUG, ("tcpip: dhcp_coarse_tmr()\n"));
151 sys_timeout(DHCP_COARSE_TIMER_MSECS, dhcp_timer_coarse, NULL);
155 * Timer callback function that calls dhcp_fine_tmr() and reschedules itself.
157 * @param arg unused argument
160 dhcp_timer_fine(void *arg)
162 LWIP_UNUSED_ARG(arg);
163 LWIP_DEBUGF(TIMERS_DEBUG, ("tcpip: dhcp_fine_tmr()\n"));
165 sys_timeout(DHCP_FINE_TIMER_MSECS, dhcp_timer_fine, NULL);
167 #endif /* LWIP_DHCP */
171 * Timer callback function that calls autoip_tmr() and reschedules itself.
173 * @param arg unused argument
176 autoip_timer(void *arg)
178 LWIP_UNUSED_ARG(arg);
179 LWIP_DEBUGF(TIMERS_DEBUG, ("tcpip: autoip_tmr()\n"));
181 sys_timeout(AUTOIP_TMR_INTERVAL, autoip_timer, NULL);
183 #endif /* LWIP_AUTOIP */
187 * Timer callback function that calls igmp_tmr() and reschedules itself.
189 * @param arg unused argument
192 igmp_timer(void *arg)
194 LWIP_UNUSED_ARG(arg);
195 LWIP_DEBUGF(TIMERS_DEBUG, ("tcpip: igmp_tmr()\n"));
197 sys_timeout(IGMP_TMR_INTERVAL, igmp_timer, NULL);
199 #endif /* LWIP_IGMP */
203 * Timer callback function that calls dns_tmr() and reschedules itself.
205 * @param arg unused argument
210 LWIP_UNUSED_ARG(arg);
211 LWIP_DEBUGF(TIMERS_DEBUG, ("tcpip: dns_tmr()\n"));
213 sys_timeout(DNS_TMR_INTERVAL, dns_timer, NULL);
215 #endif /* LWIP_DNS */
217 /** Initialize this module */
218 void sys_timeouts_init(void)
221 sys_timeout(IP_TMR_INTERVAL, ip_reass_timer, NULL);
222 #endif /* IP_REASSEMBLY */
224 sys_timeout(ARP_TMR_INTERVAL, arp_timer, NULL);
225 #endif /* LWIP_ARP */
227 sys_timeout(DHCP_COARSE_TIMER_MSECS, dhcp_timer_coarse, NULL);
228 sys_timeout(DHCP_FINE_TIMER_MSECS, dhcp_timer_fine, NULL);
229 #endif /* LWIP_DHCP */
231 sys_timeout(AUTOIP_TMR_INTERVAL, autoip_timer, NULL);
232 #endif /* LWIP_AUTOIP */
234 sys_timeout(IGMP_TMR_INTERVAL, igmp_timer, NULL);
235 #endif /* LWIP_IGMP */
237 sys_timeout(DNS_TMR_INTERVAL, dns_timer, NULL);
238 #endif /* LWIP_DNS */
241 /* Initialise timestamp for sys_check_timeouts */
242 timeouts_last_time = sys_now();
247 * Create a one-shot timer (aka timeout). Timeouts are processed in the
249 * - while waiting for a message using sys_timeouts_mbox_fetch()
250 * - by calling sys_check_timeouts() (NO_SYS==1 only)
252 * @param msecs time in milliseconds after that the timer should expire
253 * @param h callback function to call when msecs have elapsed
254 * @param arg argument to pass to the callback function
256 #if LWIP_DEBUG_TIMERNAMES
258 sys_timeout_debug(u32_t msecs, sys_timeout_handler h, void *arg, const char* handler_name)
259 #else /* LWIP_DEBUG_TIMERNAMES */
261 sys_timeout(u32_t msecs, sys_timeout_handler h, void *arg)
262 #endif /* LWIP_DEBUG_TIMERNAMES */
264 struct sys_timeo *timeout, *t;
266 timeout = (struct sys_timeo *)memp_malloc(MEMP_SYS_TIMEOUT);
267 if (timeout == NULL) {
268 LWIP_ASSERT("sys_timeout: timeout != NULL, pool MEMP_SYS_TIMEOUT is empty", timeout != NULL);
271 timeout->next = NULL;
274 timeout->time = msecs;
275 #if LWIP_DEBUG_TIMERNAMES
276 timeout->handler_name = handler_name;
277 #endif /* LWIP_DEBUG_TIMERNAMES */
279 #if LWIP_DEBUG_TIMERNAMES
280 LWIP_DEBUGF(TIMERS_DEBUG, ("sys_timeout: %p msecs=%"U32_F" h=%p arg=%p name=%s\n",
281 (void *)timeout, msecs, *(void**)&h, (void *)arg, handler_name));
282 #else /* LWIP_DEBUG_TIMERNAMES */
283 LWIP_DEBUGF(TIMERS_DEBUG, ("sys_timeout: %p msecs=%"U32_F" h=%p arg=%p\n",
284 (void *)timeout, msecs, *(void**)&h, (void *)arg));
285 #endif /* LWIP_DEBUG_TIMERNAMES */
287 if (next_timeout == NULL) {
288 next_timeout = timeout;
292 if (next_timeout->time > msecs) {
293 next_timeout->time -= msecs;
294 timeout->next = next_timeout;
295 next_timeout = timeout;
297 for(t = next_timeout; t != NULL; t = t->next) {
298 timeout->time -= t->time;
299 if (t->next == NULL || t->next->time > timeout->time) {
300 if (t->next != NULL) {
301 t->next->time -= timeout->time;
303 timeout->next = t->next;
312 * Go through timeout list (for this task only) and remove the first matching
313 * entry, even though the timeout has not triggered yet.
315 * @note This function only works as expected if there is only one timeout
316 * calling 'h' in the list of timeouts.
318 * @param h callback function that would be called by the timeout
319 * @param arg callback argument that would be passed to h
322 sys_untimeout(sys_timeout_handler h, void *arg)
324 struct sys_timeo *prev_t, *t;
326 if (next_timeout == NULL) {
330 for (t = next_timeout, prev_t = NULL; t != NULL; prev_t = t, t = t->next) {
331 if ((t->h == h) && (t->arg == arg)) {
332 /* We have a match */
333 /* Unlink from previous in list */
334 if (prev_t == NULL) {
335 next_timeout = t->next;
337 prev_t->next = t->next;
339 /* If not the last one, add time of this one back to next */
340 if (t->next != NULL) {
341 t->next->time += t->time;
343 memp_free(MEMP_SYS_TIMEOUT, t);
352 /** Handle timeouts for NO_SYS==1 (i.e. without using
353 * tcpip_thread/sys_timeouts_mbox_fetch(). Uses sys_now() to call timeout
354 * handler functions when timeouts expire.
356 * Must be called periodically from your main loop.
359 sys_check_timeouts(void)
361 struct sys_timeo *tmptimeout;
363 sys_timeout_handler h;
367 #if LWIP_DEBUG_TIMERNAMES
368 const char *handler_name;
369 #endif /* LWIP_DEBUG_TIMERNAMES */
373 /* this cares for wraparounds */
374 diff = LWIP_U32_DIFF(now, timeouts_last_time);
378 tmptimeout = next_timeout;
379 if (tmptimeout->time <= diff) {
380 /* timeout has expired */
382 timeouts_last_time = now;
383 diff -= tmptimeout->time;
384 next_timeout = tmptimeout->next;
386 arg = tmptimeout->arg;
387 #if LWIP_DEBUG_TIMERNAMES
388 handler_name = tmptimeout->handler_name;
389 #endif /* LWIP_DEBUG_TIMERNAMES */
390 memp_free(MEMP_SYS_TIMEOUT, tmptimeout);
392 #if LWIP_DEBUG_TIMERNAMES
393 LWIP_DEBUGF(TIMERS_DEBUG, ("sct calling h=%p(%p) (%s)\n", *(void**)&h, arg, handler_name));
394 #else /* LWIP_DEBUG_TIMERNAMES */
395 LWIP_DEBUGF(TIMERS_DEBUG, ("sct calling h=%p(%p)\n", *(void**)&h, arg));
396 #endif /* LWIP_DEBUG_TIMERNAMES */
400 /* repeat until all expired timers have been called */
405 /** Set back the timestamp of the last call to sys_check_timeouts()
406 * This is necessary if sys_check_timeouts() hasn't been called for a long
407 * time (e.g. while saving energy) to prevent all timer functions of that
408 * period being called.
411 sys_restart_timeouts(void)
413 timeouts_last_time = sys_now();
419 * Wait (forever) for a message to arrive in an mbox.
420 * While waiting, timeouts are processed.
422 * @param mbox the mbox to fetch the message from
423 * @param msg the place to store the message
426 sys_timeouts_mbox_fetch(sys_mbox_t *mbox, void **msg)
429 struct sys_timeo *tmptimeout;
430 sys_timeout_handler h;
432 #if LWIP_DEBUG_TIMERNAMES
433 const char *handler_name;
434 #endif /* LWIP_DEBUG_TIMERNAMES */
438 time_needed = sys_arch_mbox_fetch(mbox, msg, 0);
440 if (next_timeout->time > 0) {
441 time_needed = sys_arch_mbox_fetch(mbox, msg, next_timeout->time);
443 time_needed = SYS_ARCH_TIMEOUT;
446 if (time_needed == SYS_ARCH_TIMEOUT) {
447 /* If time == SYS_ARCH_TIMEOUT, a timeout occured before a message
448 could be fetched. We should now call the timeout handler and
449 deallocate the memory allocated for the timeout. */
450 tmptimeout = next_timeout;
451 next_timeout = tmptimeout->next;
453 arg = tmptimeout->arg;
454 #if LWIP_DEBUG_TIMERNAMES
455 handler_name = tmptimeout->handler_name;
456 #endif /* LWIP_DEBUG_TIMERNAMES */
457 memp_free(MEMP_SYS_TIMEOUT, tmptimeout);
459 #if LWIP_DEBUG_TIMERNAMES
460 LWIP_DEBUGF(TIMERS_DEBUG, ("stmf calling h=%p(%p) (%s)\n", *(void**)&h, arg, handler_name));
461 #else /* LWIP_DEBUG_TIMERNAMES */
462 LWIP_DEBUGF(TIMERS_DEBUG, ("stmf calling h=%p(%p)\n", *(void**)&h, arg));
463 #endif /* LWIP_DEBUG_TIMERNAMES */
464 /* For LWIP_TCPIP_CORE_LOCKING, lock the core before calling the
465 timeout handler function. */
470 LWIP_TCPIP_THREAD_ALIVE();
472 /* We try again to fetch a message from the mbox. */
475 /* If time != SYS_ARCH_TIMEOUT, a message was received before the timeout
476 occured. The time variable is set to the number of
477 milliseconds we waited for the message. */
478 if (time_needed < next_timeout->time) {
479 next_timeout->time -= time_needed;
481 next_timeout->time = 0;