Web server based weather station using Sparkfun Weather Meters.

Dependencies:   FatFileSystem mbed WeatherMeters SDFileSystem

Committer:
AdamGreen
Date:
Thu Feb 23 21:38:39 2012 +0000
Revision:
0:616601bde9fb

        

Who changed what in which revision?

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