Dependencies:   FatFileSystem mbed WeatherMeters SDFileSystem

Committer:
dcoban
Date:
Tue Apr 03 18:43:13 2012 +0000
Revision:
0:1a61c61d0845

        

Who changed what in which revision?

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