Port to C027 (using AppShield and Ethernet)

Dependencies:   C12832 EthernetInterface LM75B MMA7660 MQTT mbed-rtos mbed

Fork of IBMIoTClientEthernetExample by IBM Watson IoT

Committer:
icraggs
Date:
Wed Oct 01 13:27:35 2014 +0000
Revision:
8:80d49dd91542
Parent:
6:37b6d0d56190
Remove conditional compilation for IBM IoT settings

Who changed what in which revision?

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