Deprecated fork of old network stack source from github. Please use official library instead: https://mbed.org/users/mbed_official/code/EthernetInterface/

Committer:
AdamGreen
Date:
Sat Oct 26 08:51:36 2013 +0000
Revision:
1:eadc868c2acf
Parent:
0:3b00827bb0b7
Fix TCP checksum bug and stranded large TCP segments.

Who changed what in which revision?

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