NetServices Stack source

Dependents:   HelloWorld ServoInterfaceBoardExample1 4180_Lab4

Committer:
donatien
Date:
Thu Aug 05 15:01:33 2010 +0000
Revision:
11:da4498f591ee
Parent:
5:dd63a1e02b1b

        

Who changed what in which revision?

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