uc-sdk
 All Classes Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
timers.c
Go to the documentation of this file.
1 
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 
60 
62 static struct sys_timeo *next_timeout;
63 #if NO_SYS
64 static u32_t timeouts_last_time;
65 #endif /* NO_SYS */
66 
67 #if LWIP_TCP
68 
69 static int tcpip_tcp_timer_active;
70 
76 static void
77 tcpip_tcp_timer(void *arg)
78 {
79  LWIP_UNUSED_ARG(arg);
80 
81  /* call TCP timer handler */
82  tcp_tmr();
83  /* timer still needed? */
84  if (tcp_active_pcbs || tcp_tw_pcbs) {
85  /* restart timer */
86  sys_timeout(TCP_TMR_INTERVAL, tcpip_tcp_timer, NULL);
87  } else {
88  /* disable timer */
89  tcpip_tcp_timer_active = 0;
90  }
91 }
92 
98 void
99 tcp_timer_needed(void)
100 {
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);
106  }
107 }
108 #endif /* LWIP_TCP */
109 
110 #if IP_REASSEMBLY
111 
116 static void
117 ip_reass_timer(void *arg)
118 {
119  LWIP_UNUSED_ARG(arg);
120  LWIP_DEBUGF(TIMERS_DEBUG, ("tcpip: ip_reass_tmr()\n"));
121  ip_reass_tmr();
122  sys_timeout(IP_TMR_INTERVAL, ip_reass_timer, NULL);
123 }
124 #endif /* IP_REASSEMBLY */
125 
126 #if LWIP_ARP
127 
132 static void
133 arp_timer(void *arg)
134 {
135  LWIP_UNUSED_ARG(arg);
136  LWIP_DEBUGF(TIMERS_DEBUG, ("tcpip: etharp_tmr()\n"));
137  etharp_tmr();
138  sys_timeout(ARP_TMR_INTERVAL, arp_timer, NULL);
139 }
140 #endif /* LWIP_ARP */
141 
142 #if LWIP_DHCP
143 
148 static void
149 dhcp_timer_coarse(void *arg)
150 {
151  LWIP_UNUSED_ARG(arg);
152  LWIP_DEBUGF(TIMERS_DEBUG, ("tcpip: dhcp_coarse_tmr()\n"));
153  dhcp_coarse_tmr();
154  sys_timeout(DHCP_COARSE_TIMER_MSECS, dhcp_timer_coarse, NULL);
155 }
156 
162 static void
163 dhcp_timer_fine(void *arg)
164 {
165  LWIP_UNUSED_ARG(arg);
166  LWIP_DEBUGF(TIMERS_DEBUG, ("tcpip: dhcp_fine_tmr()\n"));
167  dhcp_fine_tmr();
168  sys_timeout(DHCP_FINE_TIMER_MSECS, dhcp_timer_fine, NULL);
169 }
170 #endif /* LWIP_DHCP */
171 
172 #if LWIP_AUTOIP
173 
178 static void
179 autoip_timer(void *arg)
180 {
181  LWIP_UNUSED_ARG(arg);
182  LWIP_DEBUGF(TIMERS_DEBUG, ("tcpip: autoip_tmr()\n"));
183  autoip_tmr();
184  sys_timeout(AUTOIP_TMR_INTERVAL, autoip_timer, NULL);
185 }
186 #endif /* LWIP_AUTOIP */
187 
188 #if LWIP_IGMP
189 
194 static void
195 igmp_timer(void *arg)
196 {
197  LWIP_UNUSED_ARG(arg);
198  LWIP_DEBUGF(TIMERS_DEBUG, ("tcpip: igmp_tmr()\n"));
199  igmp_tmr();
200  sys_timeout(IGMP_TMR_INTERVAL, igmp_timer, NULL);
201 }
202 #endif /* LWIP_IGMP */
203 
204 #if LWIP_DNS
205 
210 static void
211 dns_timer(void *arg)
212 {
213  LWIP_UNUSED_ARG(arg);
214  LWIP_DEBUGF(TIMERS_DEBUG, ("tcpip: dns_tmr()\n"));
215  dns_tmr();
216  sys_timeout(DNS_TMR_INTERVAL, dns_timer, NULL);
217 }
218 #endif /* LWIP_DNS */
219 
221 void sys_timeouts_init(void)
222 {
223 #if IP_REASSEMBLY
224  sys_timeout(IP_TMR_INTERVAL, ip_reass_timer, NULL);
225 #endif /* IP_REASSEMBLY */
226 #if LWIP_ARP
227  sys_timeout(ARP_TMR_INTERVAL, arp_timer, NULL);
228 #endif /* LWIP_ARP */
229 #if LWIP_DHCP
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 */
233 #if LWIP_AUTOIP
234  sys_timeout(AUTOIP_TMR_INTERVAL, autoip_timer, NULL);
235 #endif /* LWIP_AUTOIP */
236 #if LWIP_IGMP
237  sys_timeout(IGMP_TMR_INTERVAL, igmp_timer, NULL);
238 #endif /* LWIP_IGMP */
239 #if LWIP_DNS
240  sys_timeout(DNS_TMR_INTERVAL, dns_timer, NULL);
241 #endif /* LWIP_DNS */
242 
243 #if NO_SYS
244  /* Initialise timestamp for sys_check_timeouts */
245  timeouts_last_time = sys_now();
246 #endif
247 }
248 
259 #if LWIP_DEBUG_TIMERNAMES
260 void
261 sys_timeout_debug(u32_t msecs, sys_timeout_handler handler, void *arg, const char* handler_name)
262 #else /* LWIP_DEBUG_TIMERNAMES */
263 void
264 sys_timeout(u32_t msecs, sys_timeout_handler handler, void *arg)
265 #endif /* LWIP_DEBUG_TIMERNAMES */
266 {
267  struct sys_timeo *timeout, *t;
268 
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);
272  return;
273  }
274  timeout->next = NULL;
275  timeout->h = handler;
276  timeout->arg = arg;
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 */
283 
284  if (next_timeout == NULL) {
285  next_timeout = timeout;
286  return;
287  }
288 
289  if (next_timeout->time > msecs) {
290  next_timeout->time -= msecs;
291  timeout->next = next_timeout;
292  next_timeout = timeout;
293  } else {
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;
299  }
300  timeout->next = t->next;
301  t->next = timeout;
302  break;
303  }
304  }
305  }
306 }
307 
318 void
319 sys_untimeout(sys_timeout_handler handler, void *arg)
320 {
321  struct sys_timeo *prev_t, *t;
322 
323  if (next_timeout == NULL) {
324  return;
325  }
326 
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;
333  } else {
334  prev_t->next = t->next;
335  }
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;
339  }
340  memp_free(MEMP_SYS_TIMEOUT, t);
341  return;
342  }
343  }
344  return;
345 }
346 
347 #if NO_SYS
348 
355 void
356 sys_check_timeouts(void)
357 {
358  struct sys_timeo *tmptimeout;
359  u32_t diff;
360  sys_timeout_handler handler;
361  void *arg;
362  int had_one;
363  u32_t now;
364 
365  now = sys_now();
366  if (next_timeout) {
367  /* this cares for wraparounds */
368  diff = LWIP_U32_DIFF(now, timeouts_last_time);
369  do
370  {
371  had_one = 0;
372  tmptimeout = next_timeout;
373  if (tmptimeout->time <= diff) {
374  /* timeout has expired */
375  had_one = 1;
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));
385  }
386 #endif /* LWIP_DEBUG_TIMERNAMES */
387  memp_free(MEMP_SYS_TIMEOUT, tmptimeout);
388  if (handler != NULL) {
389  handler(arg);
390  }
391  }
392  /* repeat until all expired timers have been called */
393  }while(had_one);
394  }
395 }
396 
402 void
403 sys_restart_timeouts(void)
404 {
405  timeouts_last_time = sys_now();
406 }
407 
408 #else /* NO_SYS */
409 
417 void
418 sys_timeouts_mbox_fetch(sys_mbox_t *mbox, void **msg)
419 {
420  u32_t time_needed;
421  struct sys_timeo *tmptimeout;
422  sys_timeout_handler handler;
423  void *arg;
424 
425  again:
426  if (!next_timeout) {
427  time_needed = sys_arch_mbox_fetch(mbox, msg, 0);
428  } else {
429  if (next_timeout->time > 0) {
430  time_needed = sys_arch_mbox_fetch(mbox, msg, next_timeout->time);
431  } else {
432  time_needed = SYS_ARCH_TIMEOUT;
433  }
434 
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));
447  }
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. */
453  LOCK_TCPIP_CORE();
454  handler(arg);
456  }
458 
459  /* We try again to fetch a message from the mbox. */
460  goto again;
461  } else {
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;
467  } else {
468  next_timeout->time = 0;
469  }
470  }
471  }
472 }
473 
474 #endif /* NO_SYS */
475 
476 #else /* LWIP_TIMERS */
477 /* Satisfy the TCP code which calls this function */
478 void
480 {
481 }
482 #endif /* LWIP_TIMERS */