Example self-announcing webserver which controls a servo through a smallHTML userinterface.

Dependencies:   mbed

Committer:
dirkx
Date:
Sat Aug 14 15:56:01 2010 +0000
Revision:
0:a259777c45a3

        

Who changed what in which revision?

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