I have a problem getting this to work. Server only recieves half of the data being sent. Whats wrong

Dependencies:   mbed

Committer:
tax
Date:
Tue Mar 29 13:20:15 2011 +0000
Revision:
0:66300c77c6e9

        

Who changed what in which revision?

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