timers.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487
  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. * Copyright (c) 2001-2004 Swedish Institute of Computer Science.
  10. * All rights reserved.
  11. *
  12. * Redistribution and use in source and binary forms, with or without modification,
  13. * are permitted provided that the following conditions are met:
  14. *
  15. * 1. Redistributions of source code must retain the above copyright notice,
  16. * this list of conditions and the following disclaimer.
  17. * 2. Redistributions in binary form must reproduce the above copyright notice,
  18. * this list of conditions and the following disclaimer in the documentation
  19. * and/or other materials provided with the distribution.
  20. * 3. The name of the author may not be used to endorse or promote products
  21. * derived from this software without specific prior written permission.
  22. *
  23. * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
  24. * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
  25. * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT
  26. * SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
  27. * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT
  28. * OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
  29. * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
  30. * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
  31. * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY
  32. * OF SUCH DAMAGE.
  33. *
  34. * This file is part of the lwIP TCP/IP stack.
  35. *
  36. * Author: Adam Dunkels <adam@sics.se>
  37. * Simon Goldschmidt
  38. *
  39. */
  40. #include "lwip/opt.h"
  41. #include "lwip/timers.h"
  42. #include "lwip/tcp_impl.h"
  43. #if LWIP_TIMERS
  44. #include "lwip/def.h"
  45. #include "lwip/memp.h"
  46. #include "lwip/tcpip.h"
  47. #include "lwip/ip_frag.h"
  48. #include "netif/etharp.h"
  49. #include "lwip/dhcp.h"
  50. #include "lwip/autoip.h"
  51. #include "lwip/igmp.h"
  52. #include "lwip/dns.h"
  53. #include "lwip/sys.h"
  54. #include "lwip/pbuf.h"
  55. /** The one and only timeout list */
  56. static struct sys_timeo *next_timeout;
  57. #if NO_SYS
  58. static u32_t timeouts_last_time;
  59. #endif /* NO_SYS */
  60. #if LWIP_TCP
  61. /** global variable that shows if the tcp timer is currently scheduled or not */
  62. static int tcpip_tcp_timer_active;
  63. /**
  64. * Timer callback function that calls tcp_tmr() and reschedules itself.
  65. *
  66. * @param arg unused argument
  67. */
  68. static void
  69. tcpip_tcp_timer(void *arg)
  70. {
  71. LWIP_UNUSED_ARG(arg);
  72. /* call TCP timer handler */
  73. tcp_tmr();
  74. /* timer still needed? */
  75. if (tcp_active_pcbs || tcp_tw_pcbs) {
  76. /* restart timer */
  77. sys_timeout(TCP_TMR_INTERVAL, tcpip_tcp_timer, NULL);
  78. } else {
  79. /* disable timer */
  80. tcpip_tcp_timer_active = 0;
  81. }
  82. }
  83. /**
  84. * Called from TCP_REG when registering a new PCB:
  85. * the reason is to have the TCP timer only running when
  86. * there are active (or time-wait) PCBs.
  87. */
  88. void
  89. tcp_timer_needed(void)
  90. {
  91. /* timer is off but needed again? */
  92. if (!tcpip_tcp_timer_active && (tcp_active_pcbs || tcp_tw_pcbs)) {
  93. /* enable and start timer */
  94. tcpip_tcp_timer_active = 1;
  95. sys_timeout(TCP_TMR_INTERVAL, tcpip_tcp_timer, NULL);
  96. }
  97. }
  98. #endif /* LWIP_TCP */
  99. #if IP_REASSEMBLY
  100. /**
  101. * Timer callback function that calls ip_reass_tmr() and reschedules itself.
  102. *
  103. * @param arg unused argument
  104. */
  105. static void
  106. ip_reass_timer(void *arg)
  107. {
  108. LWIP_UNUSED_ARG(arg);
  109. LWIP_DEBUGF(TIMERS_DEBUG, ("tcpip: ip_reass_tmr()\n"));
  110. ip_reass_tmr();
  111. sys_timeout(IP_TMR_INTERVAL, ip_reass_timer, NULL);
  112. }
  113. #endif /* IP_REASSEMBLY */
  114. #if LWIP_ARP
  115. /**
  116. * Timer callback function that calls etharp_tmr() and reschedules itself.
  117. *
  118. * @param arg unused argument
  119. */
  120. static void
  121. arp_timer(void *arg)
  122. {
  123. LWIP_UNUSED_ARG(arg);
  124. LWIP_DEBUGF(TIMERS_DEBUG, ("tcpip: etharp_tmr()\n"));
  125. etharp_tmr();
  126. sys_timeout(ARP_TMR_INTERVAL, arp_timer, NULL);
  127. }
  128. #endif /* LWIP_ARP */
  129. #if LWIP_DHCP
  130. /**
  131. * Timer callback function that calls dhcp_coarse_tmr() and reschedules itself.
  132. *
  133. * @param arg unused argument
  134. */
  135. static void
  136. dhcp_timer_coarse(void *arg)
  137. {
  138. LWIP_UNUSED_ARG(arg);
  139. LWIP_DEBUGF(TIMERS_DEBUG, ("tcpip: dhcp_coarse_tmr()\n"));
  140. dhcp_coarse_tmr();
  141. sys_timeout(DHCP_COARSE_TIMER_MSECS, dhcp_timer_coarse, NULL);
  142. }
  143. /**
  144. * Timer callback function that calls dhcp_fine_tmr() and reschedules itself.
  145. *
  146. * @param arg unused argument
  147. */
  148. static void
  149. dhcp_timer_fine(void *arg)
  150. {
  151. LWIP_UNUSED_ARG(arg);
  152. LWIP_DEBUGF(TIMERS_DEBUG, ("tcpip: dhcp_fine_tmr()\n"));
  153. dhcp_fine_tmr();
  154. sys_timeout(DHCP_FINE_TIMER_MSECS, dhcp_timer_fine, NULL);
  155. }
  156. #endif /* LWIP_DHCP */
  157. #if LWIP_AUTOIP
  158. /**
  159. * Timer callback function that calls autoip_tmr() and reschedules itself.
  160. *
  161. * @param arg unused argument
  162. */
  163. static void
  164. autoip_timer(void *arg)
  165. {
  166. LWIP_UNUSED_ARG(arg);
  167. LWIP_DEBUGF(TIMERS_DEBUG, ("tcpip: autoip_tmr()\n"));
  168. autoip_tmr();
  169. sys_timeout(AUTOIP_TMR_INTERVAL, autoip_timer, NULL);
  170. }
  171. #endif /* LWIP_AUTOIP */
  172. #if LWIP_IGMP
  173. /**
  174. * Timer callback function that calls igmp_tmr() and reschedules itself.
  175. *
  176. * @param arg unused argument
  177. */
  178. static void
  179. igmp_timer(void *arg)
  180. {
  181. LWIP_UNUSED_ARG(arg);
  182. LWIP_DEBUGF(TIMERS_DEBUG, ("tcpip: igmp_tmr()\n"));
  183. igmp_tmr();
  184. sys_timeout(IGMP_TMR_INTERVAL, igmp_timer, NULL);
  185. }
  186. #endif /* LWIP_IGMP */
  187. #if LWIP_DNS
  188. /**
  189. * Timer callback function that calls dns_tmr() and reschedules itself.
  190. *
  191. * @param arg unused argument
  192. */
  193. static void
  194. dns_timer(void *arg)
  195. {
  196. LWIP_UNUSED_ARG(arg);
  197. LWIP_DEBUGF(TIMERS_DEBUG, ("tcpip: dns_tmr()\n"));
  198. dns_tmr();
  199. sys_timeout(DNS_TMR_INTERVAL, dns_timer, NULL);
  200. }
  201. #endif /* LWIP_DNS */
  202. /** Initialize this module */
  203. void sys_timeouts_init(void)
  204. {
  205. #if IP_REASSEMBLY
  206. sys_timeout(IP_TMR_INTERVAL, ip_reass_timer, NULL);
  207. #endif /* IP_REASSEMBLY */
  208. #if LWIP_ARP
  209. sys_timeout(ARP_TMR_INTERVAL, arp_timer, NULL);
  210. #endif /* LWIP_ARP */
  211. #if LWIP_DHCP
  212. sys_timeout(DHCP_COARSE_TIMER_MSECS, dhcp_timer_coarse, NULL);
  213. sys_timeout(DHCP_FINE_TIMER_MSECS, dhcp_timer_fine, NULL);
  214. #endif /* LWIP_DHCP */
  215. #if LWIP_AUTOIP
  216. sys_timeout(AUTOIP_TMR_INTERVAL, autoip_timer, NULL);
  217. #endif /* LWIP_AUTOIP */
  218. #if LWIP_IGMP
  219. sys_timeout(IGMP_TMR_INTERVAL, igmp_timer, NULL);
  220. #endif /* LWIP_IGMP */
  221. #if LWIP_DNS
  222. sys_timeout(DNS_TMR_INTERVAL, dns_timer, NULL);
  223. #endif /* LWIP_DNS */
  224. #if NO_SYS
  225. /* Initialise timestamp for sys_check_timeouts */
  226. timeouts_last_time = sys_now();
  227. #endif
  228. }
  229. /**
  230. * Create a one-shot timer (aka timeout). Timeouts are processed in the
  231. * following cases:
  232. * - while waiting for a message using sys_timeouts_mbox_fetch()
  233. * - by calling sys_check_timeouts() (NO_SYS==1 only)
  234. *
  235. * @param msecs time in milliseconds after that the timer should expire
  236. * @param handler callback function to call when msecs have elapsed
  237. * @param arg argument to pass to the callback function
  238. */
  239. #if LWIP_DEBUG_TIMERNAMES
  240. void
  241. sys_timeout_debug(u32_t msecs, sys_timeout_handler handler, void *arg, const char* handler_name)
  242. #else /* LWIP_DEBUG_TIMERNAMES */
  243. void
  244. sys_timeout(u32_t msecs, sys_timeout_handler handler, void *arg)
  245. #endif /* LWIP_DEBUG_TIMERNAMES */
  246. {
  247. struct sys_timeo *timeout, *t;
  248. timeout = (struct sys_timeo *)memp_malloc(MEMP_SYS_TIMEOUT);
  249. if (timeout == NULL) {
  250. LWIP_ASSERT("sys_timeout: timeout != NULL, pool MEMP_SYS_TIMEOUT is empty", timeout != NULL);
  251. return;
  252. }
  253. timeout->next = NULL;
  254. timeout->h = handler;
  255. timeout->arg = arg;
  256. timeout->time = msecs;
  257. #if LWIP_DEBUG_TIMERNAMES
  258. timeout->handler_name = handler_name;
  259. LWIP_DEBUGF(TIMERS_DEBUG, ("sys_timeout: %p msecs=%"U32_F" handler=%s arg=%p\n",
  260. (void *)timeout, msecs, handler_name, (void *)arg));
  261. #endif /* LWIP_DEBUG_TIMERNAMES */
  262. if (next_timeout == NULL) {
  263. next_timeout = timeout;
  264. return;
  265. }
  266. if (next_timeout->time > msecs) {
  267. next_timeout->time -= msecs;
  268. timeout->next = next_timeout;
  269. next_timeout = timeout;
  270. } else {
  271. for(t = next_timeout; t != NULL; t = t->next) {
  272. timeout->time -= t->time;
  273. if (t->next == NULL || t->next->time > timeout->time) {
  274. if (t->next != NULL) {
  275. t->next->time -= timeout->time;
  276. }
  277. timeout->next = t->next;
  278. t->next = timeout;
  279. break;
  280. }
  281. }
  282. }
  283. }
  284. /**
  285. * Go through timeout list (for this task only) and remove the first matching
  286. * entry, even though the timeout has not triggered yet.
  287. *
  288. * @note This function only works as expected if there is only one timeout
  289. * calling 'handler' in the list of timeouts.
  290. *
  291. * @param handler callback function that would be called by the timeout
  292. * @param arg callback argument that would be passed to handler
  293. */
  294. void
  295. sys_untimeout(sys_timeout_handler handler, void *arg)
  296. {
  297. struct sys_timeo *prev_t, *t;
  298. if (next_timeout == NULL) {
  299. return;
  300. }
  301. for (t = next_timeout, prev_t = NULL; t != NULL; prev_t = t, t = t->next) {
  302. if ((t->h == handler) && (t->arg == arg)) {
  303. /* We have a match */
  304. /* Unlink from previous in list */
  305. if (prev_t == NULL) {
  306. next_timeout = t->next;
  307. } else {
  308. prev_t->next = t->next;
  309. }
  310. /* If not the last one, add time of this one back to next */
  311. if (t->next != NULL) {
  312. t->next->time += t->time;
  313. }
  314. memp_free(MEMP_SYS_TIMEOUT, t);
  315. return;
  316. }
  317. }
  318. return;
  319. }
  320. #if NO_SYS
  321. /** Handle timeouts for NO_SYS==1 (i.e. without using
  322. * tcpip_thread/sys_timeouts_mbox_fetch(). Uses sys_now() to call timeout
  323. * handler functions when timeouts expire.
  324. *
  325. * Must be called periodically from your main loop.
  326. */
  327. void
  328. sys_check_timeouts(void)
  329. {
  330. if (next_timeout) {
  331. struct sys_timeo *tmptimeout;
  332. u32_t diff;
  333. sys_timeout_handler handler;
  334. void *arg;
  335. u8_t had_one;
  336. u32_t now;
  337. now = sys_now();
  338. /* this cares for wraparounds */
  339. diff = now - timeouts_last_time;
  340. do
  341. {
  342. #if PBUF_POOL_FREE_OOSEQ
  343. PBUF_CHECK_FREE_OOSEQ();
  344. #endif /* PBUF_POOL_FREE_OOSEQ */
  345. had_one = 0;
  346. tmptimeout = next_timeout;
  347. if (tmptimeout && (tmptimeout->time <= diff)) {
  348. /* timeout has expired */
  349. had_one = 1;
  350. timeouts_last_time = now;
  351. diff -= tmptimeout->time;
  352. next_timeout = tmptimeout->next;
  353. handler = tmptimeout->h;
  354. arg = tmptimeout->arg;
  355. #if LWIP_DEBUG_TIMERNAMES
  356. if (handler != NULL) {
  357. LWIP_DEBUGF(TIMERS_DEBUG, ("sct calling h=%s arg=%p\n",
  358. tmptimeout->handler_name, arg));
  359. }
  360. #endif /* LWIP_DEBUG_TIMERNAMES */
  361. memp_free(MEMP_SYS_TIMEOUT, tmptimeout);
  362. if (handler != NULL) {
  363. handler(arg);
  364. }
  365. }
  366. /* repeat until all expired timers have been called */
  367. }while(had_one);
  368. }
  369. }
  370. /** Set back the timestamp of the last call to sys_check_timeouts()
  371. * This is necessary if sys_check_timeouts() hasn't been called for a long
  372. * time (e.g. while saving energy) to prevent all timer functions of that
  373. * period being called.
  374. */
  375. void
  376. sys_restart_timeouts(void)
  377. {
  378. timeouts_last_time = sys_now();
  379. }
  380. #else /* NO_SYS */
  381. /**
  382. * Wait (forever) for a message to arrive in an mbox.
  383. * While waiting, timeouts are processed.
  384. *
  385. * @param mbox the mbox to fetch the message from
  386. * @param msg the place to store the message
  387. */
  388. void
  389. sys_timeouts_mbox_fetch(sys_mbox_t *mbox, void **msg)
  390. {
  391. u32_t time_needed;
  392. struct sys_timeo *tmptimeout;
  393. sys_timeout_handler handler;
  394. void *arg;
  395. again:
  396. if (!next_timeout) {
  397. time_needed = sys_arch_mbox_fetch(mbox, msg, 0);
  398. } else {
  399. if (next_timeout->time > 0) {
  400. time_needed = sys_arch_mbox_fetch(mbox, msg, next_timeout->time);
  401. } else {
  402. time_needed = SYS_ARCH_TIMEOUT;
  403. }
  404. if (time_needed == SYS_ARCH_TIMEOUT) {
  405. /* If time == SYS_ARCH_TIMEOUT, a timeout occured before a message
  406. could be fetched. We should now call the timeout handler and
  407. deallocate the memory allocated for the timeout. */
  408. tmptimeout = next_timeout;
  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, ("stmf 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. /* For LWIP_TCPIP_CORE_LOCKING, lock the core before calling the
  421. timeout handler function. */
  422. LOCK_TCPIP_CORE();
  423. handler(arg);
  424. UNLOCK_TCPIP_CORE();
  425. }
  426. LWIP_TCPIP_THREAD_ALIVE();
  427. /* We try again to fetch a message from the mbox. */
  428. goto again;
  429. } else {
  430. /* If time != SYS_ARCH_TIMEOUT, a message was received before the timeout
  431. occured. The time variable is set to the number of
  432. milliseconds we waited for the message. */
  433. if (time_needed < next_timeout->time) {
  434. next_timeout->time -= time_needed;
  435. } else {
  436. next_timeout->time = 0;
  437. }
  438. }
  439. }
  440. }
  441. #endif /* NO_SYS */
  442. #else /* LWIP_TIMERS */
  443. /* Satisfy the TCP code which calls this function */
  444. void
  445. tcp_timer_needed(void)
  446. {
  447. }
  448. #endif /* LWIP_TIMERS */