delta param

Dependents:   EthernetInterface

Fork of lwip by mbed official

Committer:
klauss
Date:
Thu Jan 08 23:42:30 2015 +0000
Revision:
29:da6f39d13880
Parent:
0:51ac1d130fd4
wip

Who changed what in which revision?

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