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"
49 #include "lwip/memp.h"
50 #include "lwip/tcpip.h"
52 #include "lwip/tcp_impl.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"
61 /** The one and only timeout list */
62 static struct sys_timeo
*next_timeout
;
64 static u32_t timeouts_last_time
;
68 /** global variable that shows if the tcp timer is currently scheduled or not */
69 static int tcpip_tcp_timer_active
;
72 * Timer callback function that calls tcp_tmr() and reschedules itself.
74 * @param arg unused argument
77 tcpip_tcp_timer(void *arg
)
81 /* call TCP timer handler */
83 /* timer still needed? */
84 if (tcp_active_pcbs
|| tcp_tw_pcbs
) {
86 sys_timeout(TCP_TMR_INTERVAL
, tcpip_tcp_timer
, NULL
);
89 tcpip_tcp_timer_active
= 0;
94 * Called from TCP_REG when registering a new PCB:
95 * the reason is to have the TCP timer only running when
96 * there are active (or time-wait) PCBs.
99 tcp_timer_needed(void)
101 /* timer is off but needed again? */
102 if (!tcpip_tcp_timer_active
&& (tcp_active_pcbs
|| tcp_tw_pcbs
)) {
103 /* enable and start timer */
104 tcpip_tcp_timer_active
= 1;
105 sys_timeout(TCP_TMR_INTERVAL
, tcpip_tcp_timer
, NULL
);
108 #endif /* LWIP_TCP */
112 * Timer callback function that calls ip_reass_tmr() and reschedules itself.
114 * @param arg unused argument
117 ip_reass_timer(void *arg
)
119 LWIP_UNUSED_ARG(arg
);
120 LWIP_DEBUGF(TIMERS_DEBUG
, ("tcpip: ip_reass_tmr()\n"));
122 sys_timeout(IP_TMR_INTERVAL
, ip_reass_timer
, NULL
);
124 #endif /* IP_REASSEMBLY */
128 * Timer callback function that calls etharp_tmr() and reschedules itself.
130 * @param arg unused argument
135 LWIP_UNUSED_ARG(arg
);
136 LWIP_DEBUGF(TIMERS_DEBUG
, ("tcpip: etharp_tmr()\n"));
138 sys_timeout(ARP_TMR_INTERVAL
, arp_timer
, NULL
);
140 #endif /* LWIP_ARP */
144 * Timer callback function that calls dhcp_coarse_tmr() and reschedules itself.
146 * @param arg unused argument
149 dhcp_timer_coarse(void *arg
)
151 LWIP_UNUSED_ARG(arg
);
152 LWIP_DEBUGF(TIMERS_DEBUG
, ("tcpip: dhcp_coarse_tmr()\n"));
154 sys_timeout(DHCP_COARSE_TIMER_MSECS
, dhcp_timer_coarse
, NULL
);
158 * Timer callback function that calls dhcp_fine_tmr() and reschedules itself.
160 * @param arg unused argument
163 dhcp_timer_fine(void *arg
)
165 LWIP_UNUSED_ARG(arg
);
166 LWIP_DEBUGF(TIMERS_DEBUG
, ("tcpip: dhcp_fine_tmr()\n"));
168 sys_timeout(DHCP_FINE_TIMER_MSECS
, dhcp_timer_fine
, NULL
);
170 #endif /* LWIP_DHCP */
174 * Timer callback function that calls autoip_tmr() and reschedules itself.
176 * @param arg unused argument
179 autoip_timer(void *arg
)
181 LWIP_UNUSED_ARG(arg
);
182 LWIP_DEBUGF(TIMERS_DEBUG
, ("tcpip: autoip_tmr()\n"));
184 sys_timeout(AUTOIP_TMR_INTERVAL
, autoip_timer
, NULL
);
186 #endif /* LWIP_AUTOIP */
190 * Timer callback function that calls igmp_tmr() and reschedules itself.
192 * @param arg unused argument
195 igmp_timer(void *arg
)
197 LWIP_UNUSED_ARG(arg
);
198 LWIP_DEBUGF(TIMERS_DEBUG
, ("tcpip: igmp_tmr()\n"));
200 sys_timeout(IGMP_TMR_INTERVAL
, igmp_timer
, NULL
);
202 #endif /* LWIP_IGMP */
206 * Timer callback function that calls dns_tmr() and reschedules itself.
208 * @param arg unused argument
213 LWIP_UNUSED_ARG(arg
);
214 LWIP_DEBUGF(TIMERS_DEBUG
, ("tcpip: dns_tmr()\n"));
216 sys_timeout(DNS_TMR_INTERVAL
, dns_timer
, NULL
);
218 #endif /* LWIP_DNS */
220 /** Initialize this module */
221 void sys_timeouts_init(void)
224 sys_timeout(IP_TMR_INTERVAL
, ip_reass_timer
, NULL
);
225 #endif /* IP_REASSEMBLY */
227 sys_timeout(ARP_TMR_INTERVAL
, arp_timer
, NULL
);
228 #endif /* LWIP_ARP */
230 sys_timeout(DHCP_COARSE_TIMER_MSECS
, dhcp_timer_coarse
, NULL
);
231 sys_timeout(DHCP_FINE_TIMER_MSECS
, dhcp_timer_fine
, NULL
);
232 #endif /* LWIP_DHCP */
234 sys_timeout(AUTOIP_TMR_INTERVAL
, autoip_timer
, NULL
);
235 #endif /* LWIP_AUTOIP */
237 sys_timeout(IGMP_TMR_INTERVAL
, igmp_timer
, NULL
);
238 #endif /* LWIP_IGMP */
240 sys_timeout(DNS_TMR_INTERVAL
, dns_timer
, NULL
);
241 #endif /* LWIP_DNS */
244 /* Initialise timestamp for sys_check_timeouts */
245 timeouts_last_time
= sys_now();
250 * Create a one-shot timer (aka timeout). Timeouts are processed in the
252 * - while waiting for a message using sys_timeouts_mbox_fetch()
253 * - by calling sys_check_timeouts() (NO_SYS==1 only)
255 * @param msecs time in milliseconds after that the timer should expire
256 * @param handler callback function to call when msecs have elapsed
257 * @param arg argument to pass to the callback function
259 #if LWIP_DEBUG_TIMERNAMES
261 sys_timeout_debug(u32_t msecs
, sys_timeout_handler handler
, void *arg
, const char* handler_name
)
262 #else /* LWIP_DEBUG_TIMERNAMES */
264 sys_timeout(u32_t msecs
, sys_timeout_handler handler
, void *arg
)
265 #endif /* LWIP_DEBUG_TIMERNAMES */
267 struct sys_timeo
*timeout
, *t
;
269 timeout
= (struct sys_timeo
*)memp_malloc(MEMP_SYS_TIMEOUT
);
270 if (timeout
== NULL
) {
271 LWIP_ASSERT("sys_timeout: timeout != NULL, pool MEMP_SYS_TIMEOUT is empty", timeout
!= NULL
);
274 timeout
->next
= NULL
;
275 timeout
->h
= handler
;
277 timeout
->time
= msecs
;
278 #if LWIP_DEBUG_TIMERNAMES
279 timeout
->handler_name
= handler_name
;
280 LWIP_DEBUGF(TIMERS_DEBUG
, ("sys_timeout: %p msecs=%"U32_F
" handler=%s arg=%p\n",
281 (void *)timeout
, msecs
, handler_name
, (void *)arg
));
282 #endif /* LWIP_DEBUG_TIMERNAMES */
284 if (next_timeout
== NULL
) {
285 next_timeout
= timeout
;
289 if (next_timeout
->time
> msecs
) {
290 next_timeout
->time
-= msecs
;
291 timeout
->next
= next_timeout
;
292 next_timeout
= timeout
;
294 for(t
= next_timeout
; t
!= NULL
; t
= t
->next
) {
295 timeout
->time
-= t
->time
;
296 if (t
->next
== NULL
|| t
->next
->time
> timeout
->time
) {
297 if (t
->next
!= NULL
) {
298 t
->next
->time
-= timeout
->time
;
300 timeout
->next
= t
->next
;
309 * Go through timeout list (for this task only) and remove the first matching
310 * entry, even though the timeout has not triggered yet.
312 * @note This function only works as expected if there is only one timeout
313 * calling 'handler' in the list of timeouts.
315 * @param handler callback function that would be called by the timeout
316 * @param arg callback argument that would be passed to handler
319 sys_untimeout(sys_timeout_handler handler
, void *arg
)
321 struct sys_timeo
*prev_t
, *t
;
323 if (next_timeout
== NULL
) {
327 for (t
= next_timeout
, prev_t
= NULL
; t
!= NULL
; prev_t
= t
, t
= t
->next
) {
328 if ((t
->h
== handler
) && (t
->arg
== arg
)) {
329 /* We have a match */
330 /* Unlink from previous in list */
331 if (prev_t
== NULL
) {
332 next_timeout
= t
->next
;
334 prev_t
->next
= t
->next
;
336 /* If not the last one, add time of this one back to next */
337 if (t
->next
!= NULL
) {
338 t
->next
->time
+= t
->time
;
340 memp_free(MEMP_SYS_TIMEOUT
, t
);
349 /** Handle timeouts for NO_SYS==1 (i.e. without using
350 * tcpip_thread/sys_timeouts_mbox_fetch(). Uses sys_now() to call timeout
351 * handler functions when timeouts expire.
353 * Must be called periodically from your main loop.
356 sys_check_timeouts(void)
358 struct sys_timeo
*tmptimeout
;
360 sys_timeout_handler handler
;
367 /* this cares for wraparounds */
368 diff
= LWIP_U32_DIFF(now
, timeouts_last_time
);
372 tmptimeout
= next_timeout
;
373 if (tmptimeout
->time
<= diff
) {
374 /* timeout has expired */
376 timeouts_last_time
= now
;
377 diff
-= tmptimeout
->time
;
378 next_timeout
= tmptimeout
->next
;
379 handler
= tmptimeout
->h
;
380 arg
= tmptimeout
->arg
;
381 #if LWIP_DEBUG_TIMERNAMES
382 if (handler
!= NULL
) {
383 LWIP_DEBUGF(TIMERS_DEBUG
, ("sct calling h=%s arg=%p\n",
384 tmptimeout
->handler_name
, arg
));
386 #endif /* LWIP_DEBUG_TIMERNAMES */
387 memp_free(MEMP_SYS_TIMEOUT
, tmptimeout
);
388 if (handler
!= NULL
) {
392 /* repeat until all expired timers have been called */
397 /** Set back the timestamp of the last call to sys_check_timeouts()
398 * This is necessary if sys_check_timeouts() hasn't been called for a long
399 * time (e.g. while saving energy) to prevent all timer functions of that
400 * period being called.
403 sys_restart_timeouts(void)
405 timeouts_last_time
= sys_now();
411 * Wait (forever) for a message to arrive in an mbox.
412 * While waiting, timeouts are processed.
414 * @param mbox the mbox to fetch the message from
415 * @param msg the place to store the message
418 sys_timeouts_mbox_fetch(sys_mbox_t
*mbox
, void **msg
)
421 struct sys_timeo
*tmptimeout
;
422 sys_timeout_handler handler
;
427 time_needed
= sys_arch_mbox_fetch(mbox
, msg
, 0);
429 if (next_timeout
->time
> 0) {
430 time_needed
= sys_arch_mbox_fetch(mbox
, msg
, next_timeout
->time
);
432 time_needed
= SYS_ARCH_TIMEOUT
;
435 if (time_needed
== SYS_ARCH_TIMEOUT
) {
436 /* If time == SYS_ARCH_TIMEOUT, a timeout occured before a message
437 could be fetched. We should now call the timeout handler and
438 deallocate the memory allocated for the timeout. */
439 tmptimeout
= next_timeout
;
440 next_timeout
= tmptimeout
->next
;
441 handler
= tmptimeout
->h
;
442 arg
= tmptimeout
->arg
;
443 #if LWIP_DEBUG_TIMERNAMES
444 if (handler
!= NULL
) {
445 LWIP_DEBUGF(TIMERS_DEBUG
, ("stmf calling h=%s arg=%p\n",
446 tmptimeout
->handler_name
, arg
));
448 #endif /* LWIP_DEBUG_TIMERNAMES */
449 memp_free(MEMP_SYS_TIMEOUT
, tmptimeout
);
450 if (handler
!= NULL
) {
451 /* For LWIP_TCPIP_CORE_LOCKING, lock the core before calling the
452 timeout handler function. */
457 LWIP_TCPIP_THREAD_ALIVE();
459 /* We try again to fetch a message from the mbox. */
462 /* If time != SYS_ARCH_TIMEOUT, a message was received before the timeout
463 occured. The time variable is set to the number of
464 milliseconds we waited for the message. */
465 if (time_needed
< next_timeout
->time
) {
466 next_timeout
->time
-= time_needed
;
468 next_timeout
->time
= 0;
476 #else /* LWIP_TIMERS */
477 /* Satisfy the TCP code which calls this function */
479 tcp_timer_needed(void)
482 #endif /* LWIP_TIMERS */