NetTribute library with debug turned on in FShandler Donatien Garner -> Segundo Equipo -> this version

Committer:
hexley
Date:
Fri Nov 19 01:54:45 2010 +0000
Revision:
0:281d6ff68967

        

Who changed what in which revision?

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