00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036
00037
00038
00039
00040
00041
00042 #include "lwip/opt.h"
00043
00044 #include "lwip/timers.h"
00045 #include "lwip/tcp_impl.h"
00046
00047 #if LWIP_TIMERS
00048
00049 #include "lwip/def.h"
00050 #include "lwip/memp.h"
00051 #include "lwip/tcpip.h"
00052
00053 #include "lwip/ip_frag.h"
00054 #include "netif/etharp.h"
00055 #include "lwip/dhcp.h"
00056 #include "lwip/autoip.h"
00057 #include "lwip/igmp.h"
00058 #include "lwip/dns.h"
00059 #include "lwip/sys.h"
00060 #include "lwip/pbuf.h"
00061
00062
00063
00064 static struct sys_timeo *next_timeout;
00065 #if NO_SYS
00066 static u32_t timeouts_last_time;
00067 #endif
00068
00069 #if LWIP_TCP
00070
00071 static int tcpip_tcp_timer_active;
00072
00073
00074
00075
00076
00077
00078 static void
00079 tcpip_tcp_timer(void *arg)
00080 {
00081 LWIP_UNUSED_ARG(arg);
00082
00083
00084 tcp_tmr();
00085
00086 if (tcp_active_pcbs || tcp_tw_pcbs) {
00087
00088 sys_timeout(TCP_TMR_INTERVAL, tcpip_tcp_timer, NULL);
00089 } else {
00090
00091 tcpip_tcp_timer_active = 0;
00092 }
00093 }
00094
00095
00096
00097
00098
00099
00100 void
00101 tcp_timer_needed(void)
00102 {
00103
00104 if (!tcpip_tcp_timer_active && (tcp_active_pcbs || tcp_tw_pcbs)) {
00105
00106 tcpip_tcp_timer_active = 1;
00107 sys_timeout(TCP_TMR_INTERVAL, tcpip_tcp_timer, NULL);
00108 }
00109 }
00110 #endif
00111
00112 #if IP_REASSEMBLY
00113
00114
00115
00116
00117
00118 static void
00119 ip_reass_timer(void *arg)
00120 {
00121 LWIP_UNUSED_ARG(arg);
00122 LWIP_DEBUGF(TIMERS_DEBUG, ("tcpip: ip_reass_tmr()\n"));
00123 ip_reass_tmr();
00124 sys_timeout(IP_TMR_INTERVAL, ip_reass_timer, NULL);
00125 }
00126 #endif
00127
00128 #if LWIP_ARP
00129
00130
00131
00132
00133
00134 static void
00135 arp_timer(void *arg)
00136 {
00137 LWIP_UNUSED_ARG(arg);
00138 LWIP_DEBUGF(TIMERS_DEBUG, ("tcpip: etharp_tmr()\n"));
00139 etharp_tmr();
00140 sys_timeout(ARP_TMR_INTERVAL, arp_timer, NULL);
00141 }
00142 #endif
00143
00144 #if LWIP_DHCP
00145
00146
00147
00148
00149
00150 static void
00151 dhcp_timer_coarse(void *arg)
00152 {
00153 LWIP_UNUSED_ARG(arg);
00154 LWIP_DEBUGF(TIMERS_DEBUG, ("tcpip: dhcp_coarse_tmr()\n"));
00155 dhcp_coarse_tmr();
00156 sys_timeout(DHCP_COARSE_TIMER_MSECS, dhcp_timer_coarse, NULL);
00157 }
00158
00159
00160
00161
00162
00163
00164 static void
00165 dhcp_timer_fine(void *arg)
00166 {
00167 LWIP_UNUSED_ARG(arg);
00168 LWIP_DEBUGF(TIMERS_DEBUG, ("tcpip: dhcp_fine_tmr()\n"));
00169 dhcp_fine_tmr();
00170 sys_timeout(DHCP_FINE_TIMER_MSECS, dhcp_timer_fine, NULL);
00171 }
00172 #endif
00173
00174 #if LWIP_AUTOIP
00175
00176
00177
00178
00179
00180 static void
00181 autoip_timer(void *arg)
00182 {
00183 LWIP_UNUSED_ARG(arg);
00184 LWIP_DEBUGF(TIMERS_DEBUG, ("tcpip: autoip_tmr()\n"));
00185 autoip_tmr();
00186 sys_timeout(AUTOIP_TMR_INTERVAL, autoip_timer, NULL);
00187 }
00188 #endif
00189
00190 #if LWIP_IGMP
00191
00192
00193
00194
00195
00196 static void
00197 igmp_timer(void *arg)
00198 {
00199 LWIP_UNUSED_ARG(arg);
00200 LWIP_DEBUGF(TIMERS_DEBUG, ("tcpip: igmp_tmr()\n"));
00201 igmp_tmr();
00202 sys_timeout(IGMP_TMR_INTERVAL, igmp_timer, NULL);
00203 }
00204 #endif
00205
00206 #if LWIP_DNS
00207
00208
00209
00210
00211
00212 static void
00213 dns_timer(void *arg)
00214 {
00215 LWIP_UNUSED_ARG(arg);
00216 LWIP_DEBUGF(TIMERS_DEBUG, ("tcpip: dns_tmr()\n"));
00217 dns_tmr();
00218 sys_timeout(DNS_TMR_INTERVAL, dns_timer, NULL);
00219 }
00220 #endif
00221
00222
00223 void sys_timeouts_init(void)
00224 {
00225 #if IP_REASSEMBLY
00226 sys_timeout(IP_TMR_INTERVAL, ip_reass_timer, NULL);
00227 #endif
00228 #if LWIP_ARP
00229 sys_timeout(ARP_TMR_INTERVAL, arp_timer, NULL);
00230 #endif
00231 #if LWIP_DHCP
00232 sys_timeout(DHCP_COARSE_TIMER_MSECS, dhcp_timer_coarse, NULL);
00233 sys_timeout(DHCP_FINE_TIMER_MSECS, dhcp_timer_fine, NULL);
00234 #endif
00235 #if LWIP_AUTOIP
00236 sys_timeout(AUTOIP_TMR_INTERVAL, autoip_timer, NULL);
00237 #endif
00238 #if LWIP_IGMP
00239 sys_timeout(IGMP_TMR_INTERVAL, igmp_timer, NULL);
00240 #endif
00241 #if LWIP_DNS
00242 sys_timeout(DNS_TMR_INTERVAL, dns_timer, NULL);
00243 #endif
00244
00245 #if NO_SYS
00246
00247 timeouts_last_time = sys_now();
00248 #endif
00249 }
00250
00251
00252
00253
00254
00255
00256
00257
00258
00259
00260
00261 #if LWIP_DEBUG_TIMERNAMES
00262 void
00263 sys_timeout_debug(u32_t msecs, sys_timeout_handler handler, void *arg, const char* handler_name)
00264 #else
00265 void
00266 sys_timeout(u32_t msecs, sys_timeout_handler handler, void *arg)
00267 #endif
00268 {
00269 struct sys_timeo *timeout, *t;
00270
00271 timeout = (struct sys_timeo *)memp_malloc(MEMP_SYS_TIMEOUT);
00272 if (timeout == NULL) {
00273 LWIP_ASSERT("sys_timeout: timeout != NULL, pool MEMP_SYS_TIMEOUT is empty", timeout != NULL);
00274 return;
00275 }
00276 timeout->next = NULL;
00277 timeout->h = handler;
00278 timeout->arg = arg;
00279 timeout->time = msecs;
00280 #if LWIP_DEBUG_TIMERNAMES
00281 timeout->handler_name = handler_name;
00282 LWIP_DEBUGF(TIMERS_DEBUG, ("sys_timeout: %p msecs=%"U32_F" handler=%s arg=%p\n",
00283 (void *)timeout, msecs, handler_name, (void *)arg));
00284 #endif
00285
00286 if (next_timeout == NULL) {
00287 next_timeout = timeout;
00288 return;
00289 }
00290
00291 if (next_timeout->time > msecs) {
00292 next_timeout->time -= msecs;
00293 timeout->next = next_timeout;
00294 next_timeout = timeout;
00295 } else {
00296 for(t = next_timeout; t != NULL; t = t->next) {
00297 timeout->time -= t->time;
00298 if (t->next == NULL || t->next->time > timeout->time) {
00299 if (t->next != NULL) {
00300 t->next->time -= timeout->time;
00301 }
00302 timeout->next = t->next;
00303 t->next = timeout;
00304 break;
00305 }
00306 }
00307 }
00308 }
00309
00310
00311
00312
00313
00314
00315
00316
00317
00318
00319
00320 void
00321 sys_untimeout(sys_timeout_handler handler, void *arg)
00322 {
00323 struct sys_timeo *prev_t, *t;
00324
00325 if (next_timeout == NULL) {
00326 return;
00327 }
00328
00329 for (t = next_timeout, prev_t = NULL; t != NULL; prev_t = t, t = t->next) {
00330 if ((t->h == handler) && (t->arg == arg)) {
00331
00332
00333 if (prev_t == NULL) {
00334 next_timeout = t->next;
00335 } else {
00336 prev_t->next = t->next;
00337 }
00338
00339 if (t->next != NULL) {
00340 t->next->time += t->time;
00341 }
00342 memp_free(MEMP_SYS_TIMEOUT, t);
00343 return;
00344 }
00345 }
00346 return;
00347 }
00348
00349 #if NO_SYS
00350
00351
00352
00353
00354
00355
00356
00357 void
00358 sys_check_timeouts(void)
00359 {
00360 if (next_timeout) {
00361 struct sys_timeo *tmptimeout;
00362 u32_t diff;
00363 sys_timeout_handler handler;
00364 void *arg;
00365 u8_t had_one;
00366 u32_t now;
00367
00368 now = sys_now();
00369
00370 diff = now - timeouts_last_time;
00371 do
00372 {
00373 #if PBUF_POOL_FREE_OOSEQ
00374 PBUF_CHECK_FREE_OOSEQ();
00375 #endif
00376 had_one = 0;
00377 tmptimeout = next_timeout;
00378 if (tmptimeout && (tmptimeout->time <= diff)) {
00379
00380 had_one = 1;
00381 timeouts_last_time = now;
00382 diff -= tmptimeout->time;
00383 next_timeout = tmptimeout->next;
00384 handler = tmptimeout->h;
00385 arg = tmptimeout->arg;
00386 #if LWIP_DEBUG_TIMERNAMES
00387 if (handler != NULL) {
00388 LWIP_DEBUGF(TIMERS_DEBUG, ("sct calling h=%s arg=%p\n",
00389 tmptimeout->handler_name, arg));
00390 }
00391 #endif
00392 memp_free(MEMP_SYS_TIMEOUT, tmptimeout);
00393 if (handler != NULL) {
00394 handler(arg);
00395 }
00396 }
00397
00398 }while(had_one);
00399 }
00400 }
00401
00402
00403
00404
00405
00406
00407 void
00408 sys_restart_timeouts(void)
00409 {
00410 timeouts_last_time = sys_now();
00411 }
00412
00413 #else
00414
00415
00416
00417
00418
00419
00420
00421
00422 void
00423 sys_timeouts_mbox_fetch(sys_mbox_t *mbox, void **msg)
00424 {
00425 u32_t time_needed;
00426 struct sys_timeo *tmptimeout;
00427 sys_timeout_handler handler;
00428 void *arg;
00429
00430 again:
00431 if (!next_timeout) {
00432 time_needed = sys_arch_mbox_fetch(mbox, msg, 0);
00433 } else {
00434 if (next_timeout->time > 0) {
00435 time_needed = sys_arch_mbox_fetch(mbox, msg, next_timeout->time);
00436 } else {
00437 time_needed = SYS_ARCH_TIMEOUT;
00438 }
00439
00440 if (time_needed == SYS_ARCH_TIMEOUT) {
00441
00442
00443
00444 tmptimeout = next_timeout;
00445 next_timeout = tmptimeout->next;
00446 handler = tmptimeout->h;
00447 arg = tmptimeout->arg;
00448 #if LWIP_DEBUG_TIMERNAMES
00449 if (handler != NULL) {
00450 LWIP_DEBUGF(TIMERS_DEBUG, ("stmf calling h=%s arg=%p\n",
00451 tmptimeout->handler_name, arg));
00452 }
00453 #endif
00454 memp_free(MEMP_SYS_TIMEOUT, tmptimeout);
00455 if (handler != NULL) {
00456
00457
00458 LOCK_TCPIP_CORE();
00459 handler(arg);
00460 UNLOCK_TCPIP_CORE();
00461 }
00462 LWIP_TCPIP_THREAD_ALIVE();
00463
00464
00465 goto again;
00466 } else {
00467
00468
00469
00470 if (time_needed < next_timeout->time) {
00471 next_timeout->time -= time_needed;
00472 } else {
00473 next_timeout->time = 0;
00474 }
00475 }
00476 }
00477 }
00478
00479 #endif
00480
00481 #else
00482
00483 void
00484 tcp_timer_needed(void)
00485 {
00486 }
00487 #endif