Version of http://mbed.org/cookbook/NetServicesTribute with setting set the same for LPC2368

Dependents:   UDPSocketExample 24LCxx_I2CApp WeatherPlatform_pachube HvZServerLib ... more

Committer:
simon
Date:
Tue Nov 23 14:15:36 2010 +0000
Revision:
0:350011bf8be7
Experimental version for testing UDP

Who changed what in which revision?

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