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