Library for Bert van Dam's book "ARM MICROCONTROLLERS" For all chapters with internet.

Dependencies:   mbed

Committer:
ICTFBI
Date:
Fri Oct 16 14:28:26 2015 +0000
Revision:
0:4edb816d21e1
Pre-update 16-10-15

Who changed what in which revision?

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