Stripped down version of Segundos NetService library (http://mbed.org/users/segundo/libraries/NetServices ). I have removed all NetServices, and all functions which had been disabled. Use this version when you need only pure TCP or UDP functions - this library compiles faster.

Dependencies:   lwip lwip-sys

Dependents:   christmasLights device_server pop3demo device_server_udp ... more

Committer:
hlipka
Date:
Mon Jan 10 21:03:11 2011 +0000
Revision:
0:8b387bed54c2
initial version

Who changed what in which revision?

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