STM32F769IDiscovery  1.00
uDANTE Audio Networking with STM32F7 DISCO board
lwip_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/lwip_timers.h"
45 #include "lwip/priv/tcp_priv.h"
46 
47 #if LWIP_TIMERS
48 
49 #include "lwip/def.h"
50 #include "lwip/memp.h"
51 #include "lwip/priv/tcpip_priv.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 
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 
73 static int tcpip_tcp_timer_active;
74 
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 
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 LWIP_IPV4
115 #if IP_REASSEMBLY
116 
121 static void
122 ip_reass_timer(void *arg)
123 {
124  LWIP_UNUSED_ARG(arg);
125  LWIP_DEBUGF(TIMERS_DEBUG, ("tcpip: ip_reass_tmr()\n"));
126  ip_reass_tmr();
127  sys_timeout(IP_TMR_INTERVAL, ip_reass_timer, NULL);
128 }
129 #endif /* IP_REASSEMBLY */
130 
131 #if LWIP_ARP
132 
137 static void
138 arp_timer(void *arg)
139 {
140  LWIP_UNUSED_ARG(arg);
141  LWIP_DEBUGF(TIMERS_DEBUG, ("tcpip: etharp_tmr()\n"));
142  etharp_tmr();
143  sys_timeout(ARP_TMR_INTERVAL, arp_timer, NULL);
144 }
145 #endif /* LWIP_ARP */
146 
147 #if LWIP_DHCP
148 
153 static void
154 dhcp_timer_coarse(void *arg)
155 {
156  LWIP_UNUSED_ARG(arg);
157  LWIP_DEBUGF(TIMERS_DEBUG, ("tcpip: dhcp_coarse_tmr()\n"));
158  dhcp_coarse_tmr();
159  sys_timeout(DHCP_COARSE_TIMER_MSECS, dhcp_timer_coarse, NULL);
160 }
161 
167 static void
168 dhcp_timer_fine(void *arg)
169 {
170  LWIP_UNUSED_ARG(arg);
171  LWIP_DEBUGF(TIMERS_DEBUG, ("tcpip: dhcp_fine_tmr()\n"));
172  dhcp_fine_tmr();
173  sys_timeout(DHCP_FINE_TIMER_MSECS, dhcp_timer_fine, NULL);
174 }
175 #endif /* LWIP_DHCP */
176 
177 #if LWIP_AUTOIP
178 
183 static void
184 autoip_timer(void *arg)
185 {
186  LWIP_UNUSED_ARG(arg);
187  LWIP_DEBUGF(TIMERS_DEBUG, ("tcpip: autoip_tmr()\n"));
188  autoip_tmr();
189  sys_timeout(AUTOIP_TMR_INTERVAL, autoip_timer, NULL);
190 }
191 #endif /* LWIP_AUTOIP */
192 
193 #if LWIP_IGMP
194 
199 static void
200 igmp_timer(void *arg)
201 {
202  LWIP_UNUSED_ARG(arg);
203  LWIP_DEBUGF(TIMERS_DEBUG, ("tcpip: igmp_tmr()\n"));
204  igmp_tmr();
205  sys_timeout(IGMP_TMR_INTERVAL, igmp_timer, NULL);
206 }
207 #endif /* LWIP_IGMP */
208 #endif /* LWIP_IPV4 */
209 
210 #if LWIP_DNS
211 
216 static void
217 dns_timer(void *arg)
218 {
219  LWIP_UNUSED_ARG(arg);
220  LWIP_DEBUGF(TIMERS_DEBUG, ("tcpip: dns_tmr()\n"));
221  dns_tmr();
222  sys_timeout(DNS_TMR_INTERVAL, dns_timer, NULL);
223 }
224 #endif /* LWIP_DNS */
225 
226 #if LWIP_IPV6
227 
232 static void
233 nd6_timer(void *arg)
234 {
235  LWIP_UNUSED_ARG(arg);
236  LWIP_DEBUGF(TIMERS_DEBUG, ("tcpip: nd6_tmr()\n"));
237  nd6_tmr();
238  sys_timeout(ND6_TMR_INTERVAL, nd6_timer, NULL);
239 }
240 
241 #if LWIP_IPV6_REASS
242 
247 static void
248 ip6_reass_timer(void *arg)
249 {
250  LWIP_UNUSED_ARG(arg);
251  LWIP_DEBUGF(TIMERS_DEBUG, ("tcpip: ip6_reass_tmr()\n"));
252  ip6_reass_tmr();
253  sys_timeout(IP6_REASS_TMR_INTERVAL, ip6_reass_timer, NULL);
254 }
255 #endif /* LWIP_IPV6_REASS */
256 
257 #if LWIP_IPV6_MLD
258 
263 static void
264 mld6_timer(void *arg)
265 {
266  LWIP_UNUSED_ARG(arg);
267  LWIP_DEBUGF(TIMERS_DEBUG, ("tcpip: mld6_tmr()\n"));
268  mld6_tmr();
269  sys_timeout(MLD6_TMR_INTERVAL, mld6_timer, NULL);
270 }
271 #endif /* LWIP_IPV6_MLD */
272 #endif /* LWIP_IPV6 */
273 
275 void sys_timeouts_init(void)
276 {
277 #if LWIP_IPV4
278 #if IP_REASSEMBLY
279  sys_timeout(IP_TMR_INTERVAL, ip_reass_timer, NULL);
280 #endif /* IP_REASSEMBLY */
281 #if LWIP_ARP
282  sys_timeout(ARP_TMR_INTERVAL, arp_timer, NULL);
283 #endif /* LWIP_ARP */
284 #if LWIP_DHCP
285  sys_timeout(DHCP_COARSE_TIMER_MSECS, dhcp_timer_coarse, NULL);
286  sys_timeout(DHCP_FINE_TIMER_MSECS, dhcp_timer_fine, NULL);
287 #endif /* LWIP_DHCP */
288 #if LWIP_AUTOIP
289  sys_timeout(AUTOIP_TMR_INTERVAL, autoip_timer, NULL);
290 #endif /* LWIP_AUTOIP */
291 #if LWIP_IGMP
292  sys_timeout(IGMP_TMR_INTERVAL, igmp_timer, NULL);
293 #endif /* LWIP_IGMP */
294 #endif /* LWIP_IPV4 */
295 #if LWIP_DNS
296  sys_timeout(DNS_TMR_INTERVAL, dns_timer, NULL);
297 #endif /* LWIP_DNS */
298 #if LWIP_IPV6
299  sys_timeout(ND6_TMR_INTERVAL, nd6_timer, NULL);
300 #if LWIP_IPV6_REASS
301  sys_timeout(IP6_REASS_TMR_INTERVAL, ip6_reass_timer, NULL);
302 #endif /* LWIP_IPV6_REASS */
303 #if LWIP_IPV6_MLD
304  sys_timeout(MLD6_TMR_INTERVAL, mld6_timer, NULL);
305 #endif /* LWIP_IPV6_MLD */
306 #endif /* LWIP_IPV6 */
307 
308 #if NO_SYS
309  /* Initialise timestamp for sys_check_timeouts */
310  timeouts_last_time = sys_now();
311 #endif
312 }
313 
324 #if LWIP_DEBUG_TIMERNAMES
325 void
326 sys_timeout_debug(u32_t msecs, sys_timeout_handler handler, void *arg, const char* handler_name)
327 #else /* LWIP_DEBUG_TIMERNAMES */
328 void
329 sys_timeout(u32_t msecs, sys_timeout_handler handler, void *arg)
330 #endif /* LWIP_DEBUG_TIMERNAMES */
331 {
332  struct sys_timeo *timeout, *t;
333 #if NO_SYS
334  u32_t now, diff;
335 #endif
336 
337  timeout = (struct sys_timeo *)memp_malloc(MEMP_SYS_TIMEOUT);
338  if (timeout == NULL) {
339  LWIP_ASSERT("sys_timeout: timeout != NULL, pool MEMP_SYS_TIMEOUT is empty", timeout != NULL);
340  return;
341  }
342 
343 #if NO_SYS
344  now = sys_now();
345  if (next_timeout == NULL) {
346  diff = 0;
347  timeouts_last_time = now;
348  } else {
349  diff = now - timeouts_last_time;
350  }
351 #endif
352 
353  timeout->next = NULL;
354  timeout->h = handler;
355  timeout->arg = arg;
356 #if NO_SYS
357  timeout->time = msecs + diff;
358 #else
359  timeout->time = msecs;
360 #endif
361 #if LWIP_DEBUG_TIMERNAMES
362  timeout->handler_name = handler_name;
363  LWIP_DEBUGF(TIMERS_DEBUG, ("sys_timeout: %p msecs=%"U32_F" handler=%s arg=%p\n",
364  (void *)timeout, msecs, handler_name, (void *)arg));
365 #endif /* LWIP_DEBUG_TIMERNAMES */
366 
367  if (next_timeout == NULL) {
368  next_timeout = timeout;
369  return;
370  }
371 
372  if (next_timeout->time > msecs) {
373  next_timeout->time -= msecs;
374  timeout->next = next_timeout;
375  next_timeout = timeout;
376  } else {
377  for (t = next_timeout; t != NULL; t = t->next) {
378  timeout->time -= t->time;
379  if (t->next == NULL || t->next->time > timeout->time) {
380  if (t->next != NULL) {
381  t->next->time -= timeout->time;
382  }
383  timeout->next = t->next;
384  t->next = timeout;
385  break;
386  }
387  }
388  }
389 }
390 
399 void
400 sys_untimeout(sys_timeout_handler handler, void *arg)
401 {
402  struct sys_timeo *prev_t, *t;
403 
404  if (next_timeout == NULL) {
405  return;
406  }
407 
408  for (t = next_timeout, prev_t = NULL; t != NULL; prev_t = t, t = t->next) {
409  if ((t->h == handler) && (t->arg == arg)) {
410  /* We have a match */
411  /* Unlink from previous in list */
412  if (prev_t == NULL) {
413  next_timeout = t->next;
414  } else {
415  prev_t->next = t->next;
416  }
417  /* If not the last one, add time of this one back to next */
418  if (t->next != NULL) {
419  t->next->time += t->time;
420  }
421  memp_free(MEMP_SYS_TIMEOUT, t);
422  return;
423  }
424  }
425  return;
426 }
427 
428 #if NO_SYS
429 
436 void
437 sys_check_timeouts(void)
438 {
439  if (next_timeout) {
440  struct sys_timeo *tmptimeout;
441  u32_t diff;
442  sys_timeout_handler handler;
443  void *arg;
444  u8_t had_one;
445  u32_t now;
446 
447  now = sys_now();
448  /* this cares for wraparounds */
449  diff = now - timeouts_last_time;
450  do {
451 #if PBUF_POOL_FREE_OOSEQ
452  PBUF_CHECK_FREE_OOSEQ();
453 #endif /* PBUF_POOL_FREE_OOSEQ */
454  had_one = 0;
455  tmptimeout = next_timeout;
456  if (tmptimeout && (tmptimeout->time <= diff)) {
457  /* timeout has expired */
458  had_one = 1;
459  timeouts_last_time += tmptimeout->time;
460  diff -= tmptimeout->time;
461  next_timeout = tmptimeout->next;
462  handler = tmptimeout->h;
463  arg = tmptimeout->arg;
464 #if LWIP_DEBUG_TIMERNAMES
465  if (handler != NULL) {
466  LWIP_DEBUGF(TIMERS_DEBUG, ("sct calling h=%s arg=%p\n",
467  tmptimeout->handler_name, arg));
468  }
469 #endif /* LWIP_DEBUG_TIMERNAMES */
470  memp_free(MEMP_SYS_TIMEOUT, tmptimeout);
471  if (handler != NULL) {
472  handler(arg);
473  }
474  }
475  /* repeat until all expired timers have been called */
476  } while (had_one);
477  }
478 }
479 
485 void
486 sys_restart_timeouts(void)
487 {
488  timeouts_last_time = sys_now();
489 }
490 
494 u32_t
495 sys_timeouts_sleeptime(void)
496 {
497  u32_t diff;
498  if (next_timeout == NULL) {
499  return 0xffffffff;
500  }
501  diff = sys_now() - timeouts_last_time;
502  if (diff > next_timeout->time) {
503  return 0;
504  } else {
505  return next_timeout->time - diff;
506  }
507 }
508 
509 #else /* NO_SYS */
510 
518 void
519 sys_timeouts_mbox_fetch(sys_mbox_t *mbox, void **msg)
520 {
521  u32_t time_needed;
522  struct sys_timeo *tmptimeout;
523  sys_timeout_handler handler;
524  void *arg;
525 
526  again:
527  if (!next_timeout) {
528  time_needed = sys_arch_mbox_fetch(mbox, msg, 0);
529  } else {
530  if (next_timeout->time > 0) {
531  time_needed = sys_arch_mbox_fetch(mbox, msg, next_timeout->time);
532  } else {
533  time_needed = SYS_ARCH_TIMEOUT;
534  }
535 
536  if (time_needed == SYS_ARCH_TIMEOUT) {
537  /* If time == SYS_ARCH_TIMEOUT, a timeout occurred before a message
538  could be fetched. We should now call the timeout handler and
539  deallocate the memory allocated for the timeout. */
540  tmptimeout = next_timeout;
541  next_timeout = tmptimeout->next;
542  handler = tmptimeout->h;
543  arg = tmptimeout->arg;
544 #if LWIP_DEBUG_TIMERNAMES
545  if (handler != NULL) {
546  LWIP_DEBUGF(TIMERS_DEBUG, ("stmf calling h=%s arg=%p\n",
547  tmptimeout->handler_name, arg));
548  }
549 #endif /* LWIP_DEBUG_TIMERNAMES */
550  memp_free(MEMP_SYS_TIMEOUT, tmptimeout);
551  if (handler != NULL) {
552  /* For LWIP_TCPIP_CORE_LOCKING, lock the core before calling the
553  timeout handler function. */
554  LOCK_TCPIP_CORE();
555  handler(arg);
557  }
559 
560  /* We try again to fetch a message from the mbox. */
561  goto again;
562  } else {
563  /* If time != SYS_ARCH_TIMEOUT, a message was received before the timeout
564  occured. The time variable is set to the number of
565  milliseconds we waited for the message. */
566  if (time_needed < next_timeout->time) {
567  next_timeout->time -= time_needed;
568  } else {
569  next_timeout->time = 0;
570  }
571  }
572  }
573 }
574 
575 #endif /* NO_SYS */
576 
577 #else /* LWIP_TIMERS */
578 /* Satisfy the TCP code which calls this function */
579 void
581 {
582 }
583 #endif /* LWIP_TIMERS */
584 
u32_t time
Definition: lwip_timers.h:69
#define LOCK_TCPIP_CORE()
Definition: tcpip_priv.h:91
void tcp_timer_needed(void)
Definition: lwip_timers.c:580
#define SYS_ARCH_TIMEOUT
Definition: sys.h:82
#define LWIP_TCPIP_THREAD_ALIVE()
Definition: tcpip_priv.h:56
u32_t sys_arch_mbox_fetch(sys_mbox_t *mbox, void **msg, u32_t timeout)
Definition: sys_arch.c:143
osMessageQId sys_mbox_t
Definition: sys_arch.h:46
void memp_free(memp_t type, void *mem)
Definition: memp.c:399
#define NULL
Definition: usbd_def.h:53
#define U32_F
Definition: cc.h:51
unsigned long u32_t
Definition: cc.h:42
sys_timeout_handler h
Definition: lwip_timers.h:70
void sys_timeout(u32_t msecs, sys_timeout_handler handler, void *arg)
struct sys_timeo * next
Definition: lwip_timers.h:68
#define LWIP_ASSERT(message, assertion)
Definition: debug.h:70
#define UNLOCK_TCPIP_CORE()
Definition: tcpip_priv.h:92
void(* sys_timeout_handler)(void *arg)
Definition: lwip_timers.h:65
u32_t sys_now(void)
void * arg
Definition: lwip_timers.h:71
unsigned char u8_t
Definition: cc.h:38
void sys_timeouts_mbox_fetch(sys_mbox_t *mbox, void **msg)
void * memp_malloc(memp_t type)
Definition: memp.c:303
#define LWIP_DEBUGF(debug, message)
Definition: debug.h:113
#define TIMERS_DEBUG
Definition: opt.h:2879
#define LWIP_UNUSED_ARG(x)
Definition: arch.h:89
void sys_timeouts_init(void)
void sys_untimeout(sys_timeout_handler handler, void *arg)