]> rtime.felk.cvut.cz Git - pes-rpp/rpp-lwip.git/blob - src/core/timers.c
5ae6d0c4e17bb7964ad71e1f7b0a7d9faef9eeeb
[pes-rpp/rpp-lwip.git] / 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/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/nd6.h"
60 #include "lwip/ip6_frag.h"
61 #include "lwip/mld6.h"
62 #include "lwip/sys.h"
63 #include "lwip/pbuf.h"
64
65 /** The one and only timeout list */
66 static struct sys_timeo *next_timeout;
67 #if NO_SYS
68 static u32_t timeouts_last_time;
69 #endif /* NO_SYS */
70
71 #if LWIP_TCP
72 /** global variable that shows if the tcp timer is currently scheduled or not */
73 static int tcpip_tcp_timer_active;
74
75 /**
76  * Timer callback function that calls tcp_tmr() and reschedules itself.
77  *
78  * @param arg unused argument
79  */
80 static void
81 tcpip_tcp_timer(void *arg)
82 {
83   LWIP_UNUSED_ARG(arg);
84
85   /* call TCP timer handler */
86   tcp_tmr();
87   /* timer still needed? */
88   if (tcp_active_pcbs || tcp_tw_pcbs) {
89     /* restart timer */
90     sys_timeout(TCP_TMR_INTERVAL, tcpip_tcp_timer, NULL);
91   } else {
92     /* disable timer */
93     tcpip_tcp_timer_active = 0;
94   }
95 }
96
97 /**
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.
101  */
102 void
103 tcp_timer_needed(void)
104 {
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);
110   }
111 }
112 #endif /* LWIP_TCP */
113
114 #if IP_REASSEMBLY
115 /**
116  * Timer callback function that calls ip_reass_tmr() and reschedules itself.
117  *
118  * @param arg unused argument
119  */
120 static void
121 ip_reass_timer(void *arg)
122 {
123   LWIP_UNUSED_ARG(arg);
124   LWIP_DEBUGF(TIMERS_DEBUG, ("tcpip: ip_reass_tmr()\n"));
125   ip_reass_tmr();
126   sys_timeout(IP_TMR_INTERVAL, ip_reass_timer, NULL);
127 }
128 #endif /* IP_REASSEMBLY */
129
130 #if LWIP_ARP
131 /**
132  * Timer callback function that calls etharp_tmr() and reschedules itself.
133  *
134  * @param arg unused argument
135  */
136 static void
137 arp_timer(void *arg)
138 {
139   LWIP_UNUSED_ARG(arg);
140   LWIP_DEBUGF(TIMERS_DEBUG, ("tcpip: etharp_tmr()\n"));
141   etharp_tmr();
142   sys_timeout(ARP_TMR_INTERVAL, arp_timer, NULL);
143 }
144 #endif /* LWIP_ARP */
145
146 #if LWIP_DHCP
147 /**
148  * Timer callback function that calls dhcp_coarse_tmr() and reschedules itself.
149  *
150  * @param arg unused argument
151  */
152 static void
153 dhcp_timer_coarse(void *arg)
154 {
155   LWIP_UNUSED_ARG(arg);
156   LWIP_DEBUGF(TIMERS_DEBUG, ("tcpip: dhcp_coarse_tmr()\n"));
157   dhcp_coarse_tmr();
158   sys_timeout(DHCP_COARSE_TIMER_MSECS, dhcp_timer_coarse, NULL);
159 }
160
161 /**
162  * Timer callback function that calls dhcp_fine_tmr() and reschedules itself.
163  *
164  * @param arg unused argument
165  */
166 static void
167 dhcp_timer_fine(void *arg)
168 {
169   LWIP_UNUSED_ARG(arg);
170   LWIP_DEBUGF(TIMERS_DEBUG, ("tcpip: dhcp_fine_tmr()\n"));
171   dhcp_fine_tmr();
172   sys_timeout(DHCP_FINE_TIMER_MSECS, dhcp_timer_fine, NULL);
173 }
174 #endif /* LWIP_DHCP */
175
176 #if LWIP_AUTOIP
177 /**
178  * Timer callback function that calls autoip_tmr() and reschedules itself.
179  *
180  * @param arg unused argument
181  */
182 static void
183 autoip_timer(void *arg)
184 {
185   LWIP_UNUSED_ARG(arg);
186   LWIP_DEBUGF(TIMERS_DEBUG, ("tcpip: autoip_tmr()\n"));
187   autoip_tmr();
188   sys_timeout(AUTOIP_TMR_INTERVAL, autoip_timer, NULL);
189 }
190 #endif /* LWIP_AUTOIP */
191
192 #if LWIP_IGMP
193 /**
194  * Timer callback function that calls igmp_tmr() and reschedules itself.
195  *
196  * @param arg unused argument
197  */
198 static void
199 igmp_timer(void *arg)
200 {
201   LWIP_UNUSED_ARG(arg);
202   LWIP_DEBUGF(TIMERS_DEBUG, ("tcpip: igmp_tmr()\n"));
203   igmp_tmr();
204   sys_timeout(IGMP_TMR_INTERVAL, igmp_timer, NULL);
205 }
206 #endif /* LWIP_IGMP */
207
208 #if LWIP_DNS
209 /**
210  * Timer callback function that calls dns_tmr() and reschedules itself.
211  *
212  * @param arg unused argument
213  */
214 static void
215 dns_timer(void *arg)
216 {
217   LWIP_UNUSED_ARG(arg);
218   LWIP_DEBUGF(TIMERS_DEBUG, ("tcpip: dns_tmr()\n"));
219   dns_tmr();
220   sys_timeout(DNS_TMR_INTERVAL, dns_timer, NULL);
221 }
222 #endif /* LWIP_DNS */
223
224 #if LWIP_IPV6
225 /**
226  * Timer callback function that calls nd6_tmr() and reschedules itself.
227  *
228  * @param arg unused argument
229  */
230 static void
231 nd6_timer(void *arg)
232 {
233   LWIP_UNUSED_ARG(arg);
234   LWIP_DEBUGF(TIMERS_DEBUG, ("tcpip: nd6_tmr()\n"));
235   nd6_tmr();
236   sys_timeout(ND6_TMR_INTERVAL, nd6_timer, NULL);
237 }
238
239 #if LWIP_IPV6_REASS
240 /**
241  * Timer callback function that calls ip6_reass_tmr() and reschedules itself.
242  *
243  * @param arg unused argument
244  */
245 static void
246 ip6_reass_timer(void *arg)
247 {
248   LWIP_UNUSED_ARG(arg);
249   LWIP_DEBUGF(TIMERS_DEBUG, ("tcpip: ip6_reass_tmr()\n"));
250   ip6_reass_tmr();
251   sys_timeout(IP6_REASS_TMR_INTERVAL, ip6_reass_timer, NULL);
252 }
253 #endif /* LWIP_IPV6_REASS */
254
255 #if LWIP_IPV6_MLD
256 /**
257  * Timer callback function that calls mld6_tmr() and reschedules itself.
258  *
259  * @param arg unused argument
260  */
261 static void
262 mld6_timer(void *arg)
263 {
264   LWIP_UNUSED_ARG(arg);
265   LWIP_DEBUGF(TIMERS_DEBUG, ("tcpip: mld6_tmr()\n"));
266   mld6_tmr();
267   sys_timeout(MLD6_TMR_INTERVAL, mld6_timer, NULL);
268 }
269 #endif /* LWIP_IPV6_MLD */
270 #endif /* LWIP_IPV6 */
271
272 /** Initialize this module */
273 void sys_timeouts_init(void)
274 {
275 #if IP_REASSEMBLY
276   sys_timeout(IP_TMR_INTERVAL, ip_reass_timer, NULL);
277 #endif /* IP_REASSEMBLY */
278 #if LWIP_ARP
279   sys_timeout(ARP_TMR_INTERVAL, arp_timer, NULL);
280 #endif /* LWIP_ARP */
281 #if LWIP_DHCP
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 */
285 #if LWIP_AUTOIP
286   sys_timeout(AUTOIP_TMR_INTERVAL, autoip_timer, NULL);
287 #endif /* LWIP_AUTOIP */
288 #if LWIP_IGMP
289   sys_timeout(IGMP_TMR_INTERVAL, igmp_timer, NULL);
290 #endif /* LWIP_IGMP */
291 #if LWIP_DNS
292   sys_timeout(DNS_TMR_INTERVAL, dns_timer, NULL);
293 #endif /* LWIP_DNS */
294 #if LWIP_IPV6
295   sys_timeout(ND6_TMR_INTERVAL, nd6_timer, NULL);
296 #if LWIP_IPV6_REASS
297   sys_timeout(IP6_REASS_TMR_INTERVAL, ip6_reass_timer, NULL);
298 #endif /* LWIP_IPV6_REASS */
299 #if LWIP_IPV6_MLD
300   sys_timeout(MLD6_TMR_INTERVAL, mld6_timer, NULL);
301 #endif /* LWIP_IPV6_MLD */
302 #endif /* LWIP_IPV6 */
303
304 #if NO_SYS
305   /* Initialise timestamp for sys_check_timeouts */
306   timeouts_last_time = sys_now();
307 #endif
308 }
309
310 /**
311  * Create a one-shot timer (aka timeout). Timeouts are processed in the
312  * following cases:
313  * - while waiting for a message using sys_timeouts_mbox_fetch()
314  * - by calling sys_check_timeouts() (NO_SYS==1 only)
315  *
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
319  */
320 #if LWIP_DEBUG_TIMERNAMES
321 void
322 sys_timeout_debug(u32_t msecs, sys_timeout_handler handler, void *arg, const char* handler_name)
323 #else /* LWIP_DEBUG_TIMERNAMES */
324 void
325 sys_timeout(u32_t msecs, sys_timeout_handler handler, void *arg)
326 #endif /* LWIP_DEBUG_TIMERNAMES */
327 {
328   struct sys_timeo *timeout, *t;
329
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);
333     return;
334   }
335   timeout->next = NULL;
336   timeout->h = handler;
337   timeout->arg = arg;
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 */
344
345   if (next_timeout == NULL) {
346     next_timeout = timeout;
347     return;
348   }
349
350   if (next_timeout->time > msecs) {
351     next_timeout->time -= msecs;
352     timeout->next = next_timeout;
353     next_timeout = timeout;
354   } else {
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;
360         }
361         timeout->next = t->next;
362         t->next = timeout;
363         break;
364       }
365     }
366   }
367 }
368
369 /**
370  * Go through timeout list (for this task only) and remove the first matching
371  * entry, even though the timeout has not triggered yet.
372  *
373  * @note This function only works as expected if there is only one timeout
374  * calling 'handler' in the list of timeouts.
375  *
376  * @param handler callback function that would be called by the timeout
377  * @param arg callback argument that would be passed to handler
378 */
379 void
380 sys_untimeout(sys_timeout_handler handler, void *arg)
381 {
382   struct sys_timeo *prev_t, *t;
383
384   if (next_timeout == NULL) {
385     return;
386   }
387
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;
394       } else {
395         prev_t->next = t->next;
396       }
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;
400       }
401       memp_free(MEMP_SYS_TIMEOUT, t);
402       return;
403     }
404   }
405   return;
406 }
407
408 #if NO_SYS
409
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.
413  *
414  * Must be called periodically from your main loop.
415  */
416 void
417 sys_check_timeouts(void)
418 {
419   if (next_timeout) {
420     struct sys_timeo *tmptimeout;
421     u32_t diff;
422     sys_timeout_handler handler;
423     void *arg;
424     u8_t had_one;
425     u32_t now;
426
427     now = sys_now();
428     /* this cares for wraparounds */
429     diff = LWIP_U32_DIFF(now, timeouts_last_time);
430     do
431     {
432 #if PBUF_POOL_FREE_OOSEQ
433       PBUF_CHECK_FREE_OOSEQ();
434 #endif /* PBUF_POOL_FREE_OOSEQ */
435       had_one = 0;
436       tmptimeout = next_timeout;
437       if (tmptimeout && (tmptimeout->time <= diff)) {
438         /* timeout has expired */
439         had_one = 1;
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));
449         }
450 #endif /* LWIP_DEBUG_TIMERNAMES */
451         memp_free(MEMP_SYS_TIMEOUT, tmptimeout);
452         if (handler != NULL) {
453           handler(arg);
454         }
455       }
456     /* repeat until all expired timers have been called */
457     }while(had_one);
458   }
459 }
460
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.
465  */
466 void
467 sys_restart_timeouts(void)
468 {
469   timeouts_last_time = sys_now();
470 }
471
472 #else /* NO_SYS */
473
474 /**
475  * Wait (forever) for a message to arrive in an mbox.
476  * While waiting, timeouts are processed.
477  *
478  * @param mbox the mbox to fetch the message from
479  * @param msg the place to store the message
480  */
481 void
482 sys_timeouts_mbox_fetch(sys_mbox_t *mbox, void **msg)
483 {
484   u32_t time_needed;
485   struct sys_timeo *tmptimeout;
486   sys_timeout_handler handler;
487   void *arg;
488
489  again:
490   if (!next_timeout) {
491     time_needed = sys_arch_mbox_fetch(mbox, msg, 0);
492   } else {
493     if (next_timeout->time > 0) {
494       time_needed = sys_arch_mbox_fetch(mbox, msg, next_timeout->time);
495     } else {
496       time_needed = SYS_ARCH_TIMEOUT;
497     }
498
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));
511       }
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. */
517         LOCK_TCPIP_CORE();
518         handler(arg);
519         UNLOCK_TCPIP_CORE();
520       }
521       LWIP_TCPIP_THREAD_ALIVE();
522
523       /* We try again to fetch a message from the mbox. */
524       goto again;
525     } else {
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;
531       } else {
532         next_timeout->time = 0;
533       }
534     }
535   }
536 }
537
538 #endif /* NO_SYS */
539
540 #else /* LWIP_TIMERS */
541 /* Satisfy the TCP code which calls this function */
542 void
543 tcp_timer_needed(void)
544 {
545 }
546 #endif /* LWIP_TIMERS */