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