Onenet

Dependents:   K64F_eCompass_OneNET_JW

Committer:
robert_jw
Date:
Mon Jun 20 01:40:20 2016 +0000
Revision:
0:b2805b6888dc
ADS

Who changed what in which revision?

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