MeterLogger
timers.c
Go to the documentation of this file.
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 
60 #ifdef MEMLEAK_DEBUG
61 static const char mem_debug_file[] ICACHE_RODATA_ATTR = __FILE__;
62 #endif
63 
64 /** The one and only timeout list */
65 static struct sys_timeo *next_timeout = NULL;
66 #if NO_SYS
67 static u32_t timeouts_last_time;
68 #endif /* NO_SYS */
69 
70 #if LWIP_TCP
71 /** global variable that shows if the tcp timer is currently scheduled or not */
72 static int tcpip_tcp_timer_active;
73 
74 /**
75  * Timer callback function that calls tcp_tmr() and reschedules itself.
76  *
77  * @param arg unused argument
78  */
79 static void ICACHE_FLASH_ATTR
80 tcpip_tcp_timer(void *arg)
81 {
82  LWIP_UNUSED_ARG(arg);
83 
84  /* call TCP timer handler */
85  tcp_tmr();
86  /* timer still needed? */
87  if (tcp_active_pcbs || tcp_tw_pcbs) {
88  /* restart timer */
89  sys_timeout(TCP_TMR_INTERVAL, tcpip_tcp_timer, NULL);
90  } else {
91  /* disable timer */
92  tcpip_tcp_timer_active = 0;
93  }
94 }
95 
96 /**
97  * Called from TCP_REG when registering a new PCB:
98  * the reason is to have the TCP timer only running when
99  * there are active (or time-wait) PCBs.
100  */
101 void
102 tcp_timer_needed(void)
103 {
104  /* timer is off but needed again? */
105  if (!tcpip_tcp_timer_active && (tcp_active_pcbs || tcp_tw_pcbs)) {
106  /* enable and start timer */
107  tcpip_tcp_timer_active = 1;
108  sys_timeout(TCP_TMR_INTERVAL, tcpip_tcp_timer, NULL);
109  }
110 }
111 
112 /**
113  * Timer callback function that calls tcp_tmr() and reschedules itself.
114  *
115  * @param arg unused argument
116  */
117 
118 static void ICACHE_FLASH_ATTR
119 tcp_timer_coarse(void *arg)
120 {
121  LWIP_UNUSED_ARG(arg);
122  LWIP_DEBUGF(TIMERS_DEBUG, ("tcpip: tcp_tmr()\n"));
123  tcp_tmr();
124  sys_timeout(TCP_TMR_INTERVAL, tcp_timer_coarse, NULL);
125 }
126 
127 #endif /* LWIP_TCP */
128 
129 #if IP_REASSEMBLY
130 /**
131  * Timer callback function that calls ip_reass_tmr() and reschedules itself.
132  *
133  * @param arg unused argument
134  */
135 static void ICACHE_FLASH_ATTR
136 ip_reass_timer(void *arg)
137 {
138  LWIP_UNUSED_ARG(arg);
139  LWIP_DEBUGF(TIMERS_DEBUG, ("tcpip: ip_reass_tmr()\n"));
140  ip_reass_tmr();
141  sys_timeout(IP_TMR_INTERVAL, ip_reass_timer, NULL);
142 }
143 #endif /* IP_REASSEMBLY */
144 
145 #if LWIP_ARP
146 /**
147  * Timer callback function that calls etharp_tmr() and reschedules itself.
148  *
149  * @param arg unused argument
150  */
151 static void ICACHE_FLASH_ATTR
152 arp_timer(void *arg)
153 {
154  LWIP_UNUSED_ARG(arg);
155  LWIP_DEBUGF(TIMERS_DEBUG, ("tcpip: etharp_tmr()\n"));
156  etharp_tmr();
157  sys_timeout(ARP_TMR_INTERVAL, arp_timer, NULL);
158 }
159 #endif /* LWIP_ARP */
160 
161 #if LWIP_DHCP
162 /**
163  * Timer callback function that calls dhcp_coarse_tmr() and reschedules itself.
164  *
165  * @param arg unused argument
166  */
167 extern void dhcps_coarse_tmr(void);
168 static void
169 dhcp_timer_coarse(void *arg)
170 {
171  LWIP_UNUSED_ARG(arg);
172  LWIP_DEBUGF(TIMERS_DEBUG, ("tcpip: dhcp_coarse_tmr()\n"));
173  dhcp_coarse_tmr();
175  sys_timeout(DHCP_COARSE_TIMER_MSECS, dhcp_timer_coarse, NULL);
176 }
177 
178 /**
179  * Timer callback function that calls dhcp_fine_tmr() and reschedules itself.
180  *
181  * @param arg unused argument
182  */
183 static void
184 dhcp_timer_fine(void *arg)
185 {
186  LWIP_UNUSED_ARG(arg);
187  LWIP_DEBUGF(TIMERS_DEBUG, ("tcpip: dhcp_fine_tmr()\n"));
188  dhcp_fine_tmr();
189  sys_timeout(DHCP_FINE_TIMER_MSECS, dhcp_timer_fine, NULL);
190 }
191 #endif /* LWIP_DHCP */
192 
193 #if LWIP_AUTOIP
194 /**
195  * Timer callback function that calls autoip_tmr() and reschedules itself.
196  *
197  * @param arg unused argument
198  */
199 static void
200 autoip_timer(void *arg)
201 {
202  LWIP_UNUSED_ARG(arg);
203  LWIP_DEBUGF(TIMERS_DEBUG, ("tcpip: autoip_tmr()\n"));
204  autoip_tmr();
205  sys_timeout(AUTOIP_TMR_INTERVAL, autoip_timer, NULL);
206 }
207 #endif /* LWIP_AUTOIP */
208 
209 #if LWIP_IGMP
210 /**
211  * Timer callback function that calls igmp_tmr() and reschedules itself.
212  *
213  * @param arg unused argument
214  */
215 static void
216 igmp_timer(void *arg)
217 {
218  LWIP_UNUSED_ARG(arg);
219  LWIP_DEBUGF(TIMERS_DEBUG, ("tcpip: igmp_tmr()\n"));
220  igmp_tmr();
221  sys_timeout(IGMP_TMR_INTERVAL, igmp_timer, NULL);
222 }
223 #endif /* LWIP_IGMP */
224 
225 #if LWIP_DNS
226 /**
227  * Timer callback function that calls dns_tmr() and reschedules itself.
228  *
229  * @param arg unused argument
230  */
231 static void
232 dns_timer(void *arg)
233 {
234  LWIP_UNUSED_ARG(arg);
235  LWIP_DEBUGF(TIMERS_DEBUG, ("tcpip: dns_tmr()\n"));
236  dns_tmr();
237  sys_timeout(DNS_TMR_INTERVAL, dns_timer, NULL);
238 }
239 #endif /* LWIP_DNS */
240 
241 /** Initialize this module */
242 void sys_timeouts_init(void)
243 {
244 #if IP_REASSEMBLY
245  sys_timeout(IP_TMR_INTERVAL, ip_reass_timer, NULL);
246 #endif /* IP_REASSEMBLY */
247 #if LWIP_ARP
248  sys_timeout(ARP_TMR_INTERVAL, arp_timer, NULL);
249 #endif /* LWIP_ARP */
250 #if LWIP_DHCP
251  DHCP_MAXRTX = 0;
252  sys_timeout(DHCP_COARSE_TIMER_MSECS, dhcp_timer_coarse, NULL);
253  sys_timeout(DHCP_FINE_TIMER_MSECS, dhcp_timer_fine, NULL);
254 #endif /* LWIP_DHCP */
255 #if LWIP_AUTOIP
256  sys_timeout(AUTOIP_TMR_INTERVAL, autoip_timer, NULL);
257 #endif /* LWIP_AUTOIP */
258 #if LWIP_IGMP
259  sys_timeout(IGMP_TMR_INTERVAL, igmp_timer, NULL);
260 #endif /* LWIP_IGMP */
261 #if LWIP_DNS
262  sys_timeout(DNS_TMR_INTERVAL, dns_timer, NULL);
263 #endif /* LWIP_DNS */
264 
265 #if LWIP_TCP
266  sys_timeout(TCP_TMR_INTERVAL, tcpip_tcp_timer, NULL);
267 // sys_timeout(TCP_TMR_INTERVAL, tcp_timer_coarse, NULL);
268 #endif
269 
270 #if NO_SYS
271  /* Initialise timestamp for sys_check_timeouts */
272  timeouts_last_time = NOW();
273 #endif
274 }
275 
276 /**
277  * Create a one-shot timer (aka timeout). Timeouts are processed in the
278  * following cases:
279  * - while waiting for a message using sys_timeouts_mbox_fetch()
280  * - by calling sys_check_timeouts() (NO_SYS==1 only)
281  *
282  * @param msecs time in milliseconds after that the timer should expire
283  * @param handler callback function to call when msecs have elapsed
284  * @param arg argument to pass to the callback function
285  */
286 #if LWIP_DEBUG_TIMERNAMES
287 void
288 sys_timeout_debug(u32_t msecs, sys_timeout_handler handler, void *arg, const char* handler_name)
289 #else /* LWIP_DEBUG_TIMERNAMES */
290 void
291 sys_timeout(u32_t msecs, sys_timeout_handler handler, void *arg)
292 #endif /* LWIP_DEBUG_TIMERNAMES */
293 {
294  struct sys_timeo *timeout, *t;
295 
296  timeout = (struct sys_timeo *)memp_malloc(MEMP_SYS_TIMEOUT);
297  if (timeout == NULL) {
298  LWIP_ASSERT("sys_timeout: timeout != NULL, pool MEMP_SYS_TIMEOUT is empty", timeout != NULL);
299  return;
300  }
301  timeout->next = NULL;
302  timeout->h = handler;
303  timeout->arg = arg;
304  timeout->time = msecs;
305 #if LWIP_DEBUG_TIMERNAMES
306  timeout->handler_name = handler_name;
307  LWIP_DEBUGF(TIMERS_DEBUG, ("sys_timeout: %p msecs=%"U32_F" handler=%s arg=%p\n",
308  (void *)timeout, msecs, handler_name, (void *)arg));
309 #endif /* LWIP_DEBUG_TIMERNAMES */
310 
311  if (next_timeout == NULL) {
312  next_timeout = timeout;
313  return;
314  }
315 
316  if (next_timeout->time > msecs) {
317  next_timeout->time -= msecs;
318  timeout->next = next_timeout;
319  next_timeout = timeout;
320  } else {
321  for(t = next_timeout; t != NULL; t = t->next) {
322  timeout->time -= t->time;
323  if (t->next == NULL || t->next->time > timeout->time) {
324  if (t->next != NULL) {
325  t->next->time -= timeout->time;
326  }
327  timeout->next = t->next;
328  t->next = timeout;
329  break;
330  }
331  }
332  }
333 }
334 
335 /**
336  * Go through timeout list (for this task only) and remove the first matching
337  * entry, even though the timeout has not triggered yet.
338  *
339  * @note This function only works as expected if there is only one timeout
340  * calling 'handler' in the list of timeouts.
341  *
342  * @param handler callback function that would be called by the timeout
343  * @param arg callback argument that would be passed to handler
344 */
345 void
346 sys_untimeout(sys_timeout_handler handler, void *arg)
347 {
348  struct sys_timeo *prev_t, *t;
349 
350  if (next_timeout == NULL) {
351  return;
352  }
353 
354  for (t = next_timeout, prev_t = NULL; t != NULL; prev_t = t, t = t->next) {
355  if ((t->h == handler) && (t->arg == arg)) {
356  /* We have a match */
357  /* Unlink from previous in list */
358  if (prev_t == NULL) {
359  next_timeout = t->next;
360  } else {
361  prev_t->next = t->next;
362  }
363  /* If not the last one, add time of this one back to next */
364  if (t->next != NULL) {
365  t->next->time += t->time;
366  }
367  memp_free(MEMP_SYS_TIMEOUT, t);
368  return;
369  }
370  }
371  return;
372 }
373 
374 #if NO_SYS
375 extern uint8 timer2_ms_flag;
376 /** Handle timeouts for NO_SYS==1 (i.e. without using
377  * tcpip_thread/sys_timeouts_mbox_fetch(). Uses sys_now() to call timeout
378  * handler functions when timeouts expire.
379  *
380  * Must be called periodically from your main loop.
381  */
382 void
383 sys_check_timeouts(void)
384 {
385  struct sys_timeo *tmptimeout;
386  u32_t diff;
387  sys_timeout_handler handler;
388  void *arg;
389  int had_one;
390  u32_t now;
391 
392  now = NOW();
393  if (next_timeout) {
394  /* this cares for wraparounds */
395  if (timer2_ms_flag == 0) {
396  diff = LWIP_U32_DIFF(now, timeouts_last_time)/((APB_CLK_FREQ>>4)/1000);
397  } else {
398  diff = LWIP_U32_DIFF(now, timeouts_last_time)/((APB_CLK_FREQ>>8)/1000);
399  }
400  do
401  {
402  had_one = 0;
403  tmptimeout = next_timeout;
404  if (tmptimeout->time <= diff) {
405  /* timeout has expired */
406  had_one = 1;
407  timeouts_last_time = now;
408  diff -= tmptimeout->time;
409  next_timeout = tmptimeout->next;
410  handler = tmptimeout->h;
411  arg = tmptimeout->arg;
412 #if LWIP_DEBUG_TIMERNAMES
413  if (handler != NULL) {
414  LWIP_DEBUGF(TIMERS_DEBUG, ("sct calling h=%s arg=%p\n",
415  tmptimeout->handler_name, arg));
416  }
417 #endif /* LWIP_DEBUG_TIMERNAMES */
418  memp_free(MEMP_SYS_TIMEOUT, tmptimeout);
419  if (handler != NULL) {
420  handler(arg);
421  }
422  }
423  /* repeat until all expired timers have been called */
424  }while(had_one);
425  }
426 }
427 
428 /** Set back the timestamp of the last call to sys_check_timeouts()
429  * This is necessary if sys_check_timeouts() hasn't been called for a long
430  * time (e.g. while saving energy) to prevent all timer functions of that
431  * period being called.
432  */
433 void
434 sys_restart_timeouts(void)
435 {
436  timeouts_last_time = NOW();
437 }
438 
439 #else /* NO_SYS */
440 
441 /**
442  * Wait (forever) for a message to arrive in an mbox.
443  * While waiting, timeouts are processed.
444  *
445  * @param mbox the mbox to fetch the message from
446  * @param msg the place to store the message
447  */
448 void
449 sys_timeouts_mbox_fetch(sys_mbox_t *mbox, void **msg)
450 {
451  u32_t time_needed;
452  struct sys_timeo *tmptimeout;
453  sys_timeout_handler handler;
454  void *arg;
455 
456  again:
457  if (!next_timeout) {
458  time_needed = sys_arch_mbox_fetch(mbox, msg, 0);
459  } else {
460  if (next_timeout->time > 0) {
461  time_needed = sys_arch_mbox_fetch(mbox, msg, next_timeout->time);
462  } else {
463  time_needed = SYS_ARCH_TIMEOUT;
464  }
465 
466  if (time_needed == SYS_ARCH_TIMEOUT) {
467  /* If time == SYS_ARCH_TIMEOUT, a timeout occured before a message
468  could be fetched. We should now call the timeout handler and
469  deallocate the memory allocated for the timeout. */
470  tmptimeout = next_timeout;
471  next_timeout = tmptimeout->next;
472  handler = tmptimeout->h;
473  arg = tmptimeout->arg;
474 #if LWIP_DEBUG_TIMERNAMES
475  if (handler != NULL) {
476  LWIP_DEBUGF(TIMERS_DEBUG, ("stmf calling h=%s arg=%p\n",
477  tmptimeout->handler_name, arg));
478  }
479 #endif /* LWIP_DEBUG_TIMERNAMES */
480  memp_free(MEMP_SYS_TIMEOUT, tmptimeout);
481  if (handler != NULL) {
482  /* For LWIP_TCPIP_CORE_LOCKING, lock the core before calling the
483  timeout handler function. */
484  LOCK_TCPIP_CORE();
485  handler(arg);
487  }
489 
490  /* We try again to fetch a message from the mbox. */
491  goto again;
492  } else {
493  /* If time != SYS_ARCH_TIMEOUT, a message was received before the timeout
494  occured. The time variable is set to the number of
495  milliseconds we waited for the message. */
496  if (time_needed < next_timeout->time) {
497  next_timeout->time -= time_needed;
498  } else {
499  next_timeout->time = 0;
500  }
501  }
502  }
503 }
504 
505 #endif /* NO_SYS */
506 
507 #else /* LWIP_TIMERS */
508 /* Satisfy the TCP code which calls this function */
509 void
511 {
512 }
513 #endif /* LWIP_TIMERS */
u32_t time
Definition: timers.h:67
void memp_free(memp_t type, void *mem) ICACHE_FLASH_ATTR
Definition: memp.c:438
sys_timeout_handler h
Definition: timers.h:68
void(* sys_timeout_handler)(void *arg)
Definition: timers.h:63
#define LWIP_TCPIP_THREAD_ALIVE()
Definition: tcpip.h:54
#define LOCK_TCPIP_CORE()
Definition: tcpip.h:67
#define NULL
Definition: def.h:47
#define SYS_ARCH_TIMEOUT
Definition: sys.h:74
void tcp_timer_needed(void)
Definition: timers.c:510
u32_t sys_arch_mbox_fetch(sys_mbox_t *mbox, void **msg, u32_t timeout)
const ip_addr_t ip_addr_any ICACHE_RODATA_ATTR
Definition: ip_addr.c:44
#define ICACHE_FLASH_ATTR
Definition: c_types.h:99
void * memp_malloc(memp_t type) ICACHE_FLASH_ATTR
Definition: memp.c:393
#define APB_CLK_FREQ
Definition: eagle_soc.h:77
#define UNLOCK_TCPIP_CORE()
Definition: tcpip.h:68
void sys_timeout(u32_t msecs, sys_timeout_handler handler, void *arg) ICACHE_FLASH_ATTR
void * arg
Definition: timers.h:69
#define U32_F
Definition: cc.h:65
unsigned long u32_t
Definition: cc.h:56
#define LWIP_DEBUGF(debug, message)
Definition: debug.h:94
unsigned char uint8
Definition: c_types.h:45
struct sys_timeo * next
Definition: timers.h:66
#define LWIP_U32_DIFF(a, b)
Definition: def.h:52
void sys_untimeout(sys_timeout_handler handler, void *arg) ICACHE_FLASH_ATTR
void sys_timeouts_init(void) ICACHE_FLASH_ATTR
#define NOW()
Definition: eagle_soc.h:173
void ICACHE_FLASH_ATTR dhcps_coarse_tmr(void)
Definition: dhcpserver.c:1066
#define LWIP_ASSERT(message, assertion)
Definition: debug.h:65
void sys_timeouts_mbox_fetch(sys_mbox_t *mbox, void **msg)
#define DHCP_MAXRTX
Definition: lwipopts.h:682
static sys_mbox_t mbox
Definition: tcpip.c:55
#define TIMERS_DEBUG
Definition: opt.h:1917
#define LWIP_UNUSED_ARG(x)
Definition: arch.h:73