EthernetNetIf Compatibility.

Dependents:   XBeeWiFi_SPI_example

Fork of NetServicesSource by Donatien Garnier

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