Leest de waarde van een sensor binnen een maakt deze beschikbaar via internet

Dependencies:   NTPClient_NetServices mbed

Committer:
hendrikvincent
Date:
Mon Dec 02 09:01:23 2013 +0000
Revision:
0:05ccbd4f84f1
eerste programma;

Who changed what in which revision?

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