]> rtime.felk.cvut.cz Git - l4.git/blob - l4/pkg/ankh/lib/lwip/contrib/src/core/timers.c
0dba927246b57c820d35c2b0eea12810f1d97fd6
[l4.git] / l4 / pkg / ankh / lib / lwip / contrib / src / core / timers.c
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/def.h"
46 #include "lwip/memp.h"
47 #include "lwip/tcpip.h"
48
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"
55 #include "lwip/dns.h"
56
57
58 /** The one and only timeout list */
59 static struct sys_timeo *next_timeout;
60 #if NO_SYS
61 static u32_t timeouts_last_time;
62 #endif /* NO_SYS */
63
64 #if LWIP_TCP
65 /** global variable that shows if the tcp timer is currently scheduled or not */
66 static int tcpip_tcp_timer_active;
67
68 /**
69  * Timer callback function that calls tcp_tmr() and reschedules itself.
70  *
71  * @param arg unused argument
72  */
73 static void
74 tcpip_tcp_timer(void *arg)
75 {
76   LWIP_UNUSED_ARG(arg);
77
78   /* call TCP timer handler */
79   tcp_tmr();
80   /* timer still needed? */
81   if (tcp_active_pcbs || tcp_tw_pcbs) {
82     /* restart timer */
83     sys_timeout(TCP_TMR_INTERVAL, tcpip_tcp_timer, NULL);
84   } else {
85     /* disable timer */
86     tcpip_tcp_timer_active = 0;
87   }
88 }
89
90 /**
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.
94  */
95 void
96 tcp_timer_needed(void)
97 {
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);
103   }
104 }
105 #endif /* LWIP_TCP */
106
107 #if IP_REASSEMBLY
108 /**
109  * Timer callback function that calls ip_reass_tmr() and reschedules itself.
110  *
111  * @param arg unused argument
112  */
113 static void
114 ip_reass_timer(void *arg)
115 {
116   LWIP_UNUSED_ARG(arg);
117   LWIP_DEBUGF(TIMERS_DEBUG, ("tcpip: ip_reass_tmr()\n"));
118   ip_reass_tmr();
119   sys_timeout(IP_TMR_INTERVAL, ip_reass_timer, NULL);
120 }
121 #endif /* IP_REASSEMBLY */
122
123 #if LWIP_ARP
124 /**
125  * Timer callback function that calls etharp_tmr() and reschedules itself.
126  *
127  * @param arg unused argument
128  */
129 static void
130 arp_timer(void *arg)
131 {
132   LWIP_UNUSED_ARG(arg);
133   LWIP_DEBUGF(TIMERS_DEBUG, ("tcpip: etharp_tmr()\n"));
134   etharp_tmr();
135   sys_timeout(ARP_TMR_INTERVAL, arp_timer, NULL);
136 }
137 #endif /* LWIP_ARP */
138
139 #if LWIP_DHCP
140 /**
141  * Timer callback function that calls dhcp_coarse_tmr() and reschedules itself.
142  *
143  * @param arg unused argument
144  */
145 static void
146 dhcp_timer_coarse(void *arg)
147 {
148   LWIP_UNUSED_ARG(arg);
149   LWIP_DEBUGF(TIMERS_DEBUG, ("tcpip: dhcp_coarse_tmr()\n"));
150   dhcp_coarse_tmr();
151   sys_timeout(DHCP_COARSE_TIMER_MSECS, dhcp_timer_coarse, NULL);
152 }
153
154 /**
155  * Timer callback function that calls dhcp_fine_tmr() and reschedules itself.
156  *
157  * @param arg unused argument
158  */
159 static void
160 dhcp_timer_fine(void *arg)
161 {
162   LWIP_UNUSED_ARG(arg);
163   LWIP_DEBUGF(TIMERS_DEBUG, ("tcpip: dhcp_fine_tmr()\n"));
164   dhcp_fine_tmr();
165   sys_timeout(DHCP_FINE_TIMER_MSECS, dhcp_timer_fine, NULL);
166 }
167 #endif /* LWIP_DHCP */
168
169 #if LWIP_AUTOIP
170 /**
171  * Timer callback function that calls autoip_tmr() and reschedules itself.
172  *
173  * @param arg unused argument
174  */
175 static void
176 autoip_timer(void *arg)
177 {
178   LWIP_UNUSED_ARG(arg);
179   LWIP_DEBUGF(TIMERS_DEBUG, ("tcpip: autoip_tmr()\n"));
180   autoip_tmr();
181   sys_timeout(AUTOIP_TMR_INTERVAL, autoip_timer, NULL);
182 }
183 #endif /* LWIP_AUTOIP */
184
185 #if LWIP_IGMP
186 /**
187  * Timer callback function that calls igmp_tmr() and reschedules itself.
188  *
189  * @param arg unused argument
190  */
191 static void
192 igmp_timer(void *arg)
193 {
194   LWIP_UNUSED_ARG(arg);
195   LWIP_DEBUGF(TIMERS_DEBUG, ("tcpip: igmp_tmr()\n"));
196   igmp_tmr();
197   sys_timeout(IGMP_TMR_INTERVAL, igmp_timer, NULL);
198 }
199 #endif /* LWIP_IGMP */
200
201 #if LWIP_DNS
202 /**
203  * Timer callback function that calls dns_tmr() and reschedules itself.
204  *
205  * @param arg unused argument
206  */
207 static void
208 dns_timer(void *arg)
209 {
210   LWIP_UNUSED_ARG(arg);
211   LWIP_DEBUGF(TIMERS_DEBUG, ("tcpip: dns_tmr()\n"));
212   dns_tmr();
213   sys_timeout(DNS_TMR_INTERVAL, dns_timer, NULL);
214 }
215 #endif /* LWIP_DNS */
216
217 /** Initialize this module */
218 void sys_timeouts_init(void)
219 {
220 #if IP_REASSEMBLY
221   sys_timeout(IP_TMR_INTERVAL, ip_reass_timer, NULL);
222 #endif /* IP_REASSEMBLY */
223 #if LWIP_ARP
224   sys_timeout(ARP_TMR_INTERVAL, arp_timer, NULL);
225 #endif /* LWIP_ARP */
226 #if LWIP_DHCP
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 */
230 #if LWIP_AUTOIP
231   sys_timeout(AUTOIP_TMR_INTERVAL, autoip_timer, NULL);
232 #endif /* LWIP_AUTOIP */
233 #if LWIP_IGMP
234   sys_timeout(IGMP_TMR_INTERVAL, igmp_timer, NULL);
235 #endif /* LWIP_IGMP */
236 #if LWIP_DNS
237   sys_timeout(DNS_TMR_INTERVAL, dns_timer, NULL);
238 #endif /* LWIP_DNS */
239
240 #if NO_SYS
241   /* Initialise timestamp for sys_check_timeouts */
242   timeouts_last_time = sys_now();
243 #endif
244 }
245
246 /**
247  * Create a one-shot timer (aka timeout). Timeouts are processed in the
248  * following cases:
249  * - while waiting for a message using sys_timeouts_mbox_fetch()
250  * - by calling sys_check_timeouts() (NO_SYS==1 only)
251  *
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
255  */
256 #if LWIP_DEBUG_TIMERNAMES
257 void
258 sys_timeout_debug(u32_t msecs, sys_timeout_handler h, void *arg, const char* handler_name)
259 #else /* LWIP_DEBUG_TIMERNAMES */
260 void
261 sys_timeout(u32_t msecs, sys_timeout_handler h, void *arg)
262 #endif /* LWIP_DEBUG_TIMERNAMES */
263 {
264   struct sys_timeo *timeout, *t;
265
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);
269     return;
270   }
271   timeout->next = NULL;
272   timeout->h = h;
273   timeout->arg = arg;
274   timeout->time = msecs;
275 #if LWIP_DEBUG_TIMERNAMES
276   timeout->handler_name = handler_name;
277 #endif /* LWIP_DEBUG_TIMERNAMES */
278
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 */
286
287   if (next_timeout == NULL) {
288     next_timeout = timeout;
289     return;
290   }
291
292   if (next_timeout->time > msecs) {
293     next_timeout->time -= msecs;
294     timeout->next = next_timeout;
295     next_timeout = timeout;
296   } else {
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;
302         }
303         timeout->next = t->next;
304         t->next = timeout;
305         break;
306       }
307     }
308   }
309 }
310
311 /**
312  * Go through timeout list (for this task only) and remove the first matching
313  * entry, even though the timeout has not triggered yet.
314  *
315  * @note This function only works as expected if there is only one timeout
316  * calling 'h' in the list of timeouts.
317  *
318  * @param h callback function that would be called by the timeout
319  * @param arg callback argument that would be passed to h
320 */
321 void
322 sys_untimeout(sys_timeout_handler h, void *arg)
323 {
324   struct sys_timeo *prev_t, *t;
325
326   if (next_timeout == NULL) {
327     return;
328   }
329
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;
336       } else {
337         prev_t->next = t->next;
338       }
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;
342       }
343       memp_free(MEMP_SYS_TIMEOUT, t);
344       return;
345     }
346   }
347   return;
348 }
349
350 #if NO_SYS
351
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.
355  *
356  * Must be called periodically from your main loop.
357  */
358 void
359 sys_check_timeouts(void)
360 {
361   struct sys_timeo *tmptimeout;
362   u32_t diff;
363   sys_timeout_handler h;
364   void *arg;
365   int had_one;
366   u32_t now;
367 #if LWIP_DEBUG_TIMERNAMES
368   const char *handler_name;
369 #endif /* LWIP_DEBUG_TIMERNAMES */
370
371   now = sys_now();
372   if (next_timeout) {
373     /* this cares for wraparounds */
374     diff = LWIP_U32_DIFF(now, timeouts_last_time);
375     do
376     {
377       had_one = 0;
378       tmptimeout = next_timeout;
379       if (tmptimeout->time <= diff) {
380         /* timeout has expired */
381         had_one = 1;
382         timeouts_last_time = now;
383         diff -= tmptimeout->time;
384         next_timeout = tmptimeout->next;
385         h   = tmptimeout->h;
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);
391         if (h != NULL) {
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 */
397           h(arg);
398         }
399       }
400     /* repeat until all expired timers have been called */
401     }while(had_one);
402   }
403 }
404
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.
409  */
410 void
411 sys_restart_timeouts(void)
412 {
413   timeouts_last_time = sys_now();
414 }
415
416 #else /* NO_SYS */
417
418 /**
419  * Wait (forever) for a message to arrive in an mbox.
420  * While waiting, timeouts are processed.
421  *
422  * @param mbox the mbox to fetch the message from
423  * @param msg the place to store the message
424  */
425 void
426 sys_timeouts_mbox_fetch(sys_mbox_t *mbox, void **msg)
427 {
428   u32_t time_needed;
429   struct sys_timeo *tmptimeout;
430   sys_timeout_handler h;
431   void *arg;
432 #if LWIP_DEBUG_TIMERNAMES
433   const char *handler_name;
434 #endif /* LWIP_DEBUG_TIMERNAMES */
435
436  again:
437   if (!next_timeout) {
438     time_needed = sys_arch_mbox_fetch(mbox, msg, 0);
439   } else {
440     if (next_timeout->time > 0) {
441       time_needed = sys_arch_mbox_fetch(mbox, msg, next_timeout->time);
442     } else {
443       time_needed = SYS_ARCH_TIMEOUT;
444     }
445
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;
452       h   = tmptimeout->h;
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);
458       if (h != NULL) {
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. */
466         LOCK_TCPIP_CORE();
467         h(arg);
468         UNLOCK_TCPIP_CORE();
469       }
470       LWIP_TCPIP_THREAD_ALIVE();
471
472       /* We try again to fetch a message from the mbox. */
473       goto again;
474     } else {
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;
480       } else {
481         next_timeout->time = 0;
482       }
483     }
484   }
485 }
486
487 #endif /* NO_SYS */