Modified version of NetServices. Fixes an issue where connections failed should the HTTP response status line be received in a packet on its own prior to any further headers. Changes are made to the HTTPClient.cpp file's readHeaders method.

Committer:
andrewbonney
Date:
Fri Apr 08 14:39:41 2011 +0000
Revision:
0:ec559500a63f

        

Who changed what in which revision?

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