3 * Sequential API Main thread module
8 * Copyright (c) 2001-2004 Swedish Institute of Computer Science.
11 * Redistribution and use in source and binary forms, with or without modification,
12 * are permitted provided that the following conditions are met:
14 * 1. Redistributions of source code must retain the above copyright notice,
15 * this list of conditions and the following disclaimer.
16 * 2. Redistributions in binary form must reproduce the above copyright notice,
17 * this list of conditions and the following disclaimer in the documentation
18 * and/or other materials provided with the distribution.
19 * 3. The name of the author may not be used to endorse or promote products
20 * derived from this software without specific prior written permission.
22 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
23 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
24 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT
25 * SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
26 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT
27 * OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
28 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
29 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
30 * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY
33 * This file is part of the lwIP TCP/IP stack.
35 * Author: Adam Dunkels <adam@sics.se>
41 #if !NO_SYS /* don't build if not configured for use in lwipopts.h */
44 #include "lwip/memp.h"
45 #include "lwip/pbuf.h"
46 #include "lwip/ip_frag.h"
48 #include "lwip/autoip.h"
49 #include "lwip/dhcp.h"
50 #include "lwip/igmp.h"
52 #include "lwip/tcpip.h"
53 #include "lwip/init.h"
54 #include "netif/etharp.h"
55 #include "netif/ppp_oe.h"
57 /* global variables */
58 static void (* tcpip_init_done)(void *arg);
59 static void *tcpip_init_done_arg;
60 static sys_mbox_t mbox = SYS_MBOX_NULL;
62 #if LWIP_TCPIP_CORE_LOCKING
63 /** The global semaphore to lock the stack. */
64 sys_sem_t lock_tcpip_core;
65 #endif /* LWIP_TCPIP_CORE_LOCKING */
68 /* global variable that shows if the tcp timer is currently scheduled or not */
69 static int tcpip_tcp_timer_active;
72 * Timer callback function that calls tcp_tmr() and reschedules itself.
74 * @param arg unused argument
77 tcpip_tcp_timer(void *arg)
81 /* call TCP timer handler */
83 /* timer still needed? */
84 if (tcp_active_pcbs || tcp_tw_pcbs) {
86 sys_timeout(TCP_TMR_INTERVAL, tcpip_tcp_timer, NULL);
89 tcpip_tcp_timer_active = 0;
95 * Called from TCP_REG when registering a new PCB:
96 * the reason is to have the TCP timer only running when
97 * there are active (or time-wait) PCBs.
100 tcp_timer_needed(void)
102 /* timer is off but needed again? */
103 if (!tcpip_tcp_timer_active && (tcp_active_pcbs || tcp_tw_pcbs)) {
104 /* enable and start timer */
105 tcpip_tcp_timer_active = 1;
106 sys_timeout(TCP_TMR_INTERVAL, tcpip_tcp_timer, NULL);
110 #endif /* LWIP_TCP */
114 * Timer callback function that calls ip_reass_tmr() and reschedules itself.
116 * @param arg unused argument
119 ip_reass_timer(void *arg)
121 LWIP_UNUSED_ARG(arg);
122 LWIP_DEBUGF(TCPIP_DEBUG, ("tcpip: ip_reass_tmr()\n"));
124 sys_timeout(IP_TMR_INTERVAL, ip_reass_timer, NULL);
126 #endif /* IP_REASSEMBLY */
130 * Timer callback function that calls etharp_tmr() and reschedules itself.
132 * @param arg unused argument
137 LWIP_UNUSED_ARG(arg);
138 LWIP_DEBUGF(TCPIP_DEBUG, ("tcpip: etharp_tmr()\n"));
140 sys_timeout(ARP_TMR_INTERVAL, arp_timer, NULL);
142 #endif /* LWIP_ARP */
146 * Timer callback function that calls dhcp_coarse_tmr() and reschedules itself.
148 * @param arg unused argument
151 dhcp_timer_coarse(void *arg)
153 LWIP_UNUSED_ARG(arg);
154 LWIP_DEBUGF(TCPIP_DEBUG, ("tcpip: dhcp_coarse_tmr()\n"));
156 sys_timeout(DHCP_COARSE_TIMER_MSECS, dhcp_timer_coarse, NULL);
160 * Timer callback function that calls dhcp_fine_tmr() and reschedules itself.
162 * @param arg unused argument
165 dhcp_timer_fine(void *arg)
167 LWIP_UNUSED_ARG(arg);
168 LWIP_DEBUGF(TCPIP_DEBUG, ("tcpip: dhcp_fine_tmr()\n"));
170 sys_timeout(DHCP_FINE_TIMER_MSECS, dhcp_timer_fine, NULL);
172 #endif /* LWIP_DHCP */
176 * Timer callback function that calls autoip_tmr() and reschedules itself.
178 * @param arg unused argument
181 autoip_timer(void *arg)
183 LWIP_UNUSED_ARG(arg);
184 LWIP_DEBUGF(TCPIP_DEBUG, ("tcpip: autoip_tmr()\n"));
186 sys_timeout(AUTOIP_TMR_INTERVAL, autoip_timer, NULL);
188 #endif /* LWIP_AUTOIP */
192 * Timer callback function that calls igmp_tmr() and reschedules itself.
194 * @param arg unused argument
197 igmp_timer(void *arg)
199 LWIP_UNUSED_ARG(arg);
200 LWIP_DEBUGF(TCPIP_DEBUG, ("tcpip: igmp_tmr()\n"));
202 sys_timeout(IGMP_TMR_INTERVAL, igmp_timer, NULL);
204 #endif /* LWIP_IGMP */
208 * Timer callback function that calls dns_tmr() and reschedules itself.
210 * @param arg unused argument
215 LWIP_UNUSED_ARG(arg);
216 LWIP_DEBUGF(TCPIP_DEBUG, ("tcpip: dns_tmr()\n"));
218 sys_timeout(DNS_TMR_INTERVAL, dns_timer, NULL);
220 #endif /* LWIP_DNS */
223 * The main lwIP thread. This thread has exclusive access to lwIP core functions
224 * (unless access to them is not locked). Other threads communicate with this
225 * thread using message boxes.
227 * It also starts all the timers to make sure they are running in the right
230 * @param arg unused argument
233 tcpip_thread(void *arg)
235 struct tcpip_msg *msg;
236 LWIP_UNUSED_ARG(arg);
239 sys_timeout(IP_TMR_INTERVAL, ip_reass_timer, NULL);
240 #endif /* IP_REASSEMBLY */
242 sys_timeout(ARP_TMR_INTERVAL, arp_timer, NULL);
243 #endif /* LWIP_ARP */
245 sys_timeout(DHCP_COARSE_TIMER_MSECS, dhcp_timer_coarse, NULL);
246 sys_timeout(DHCP_FINE_TIMER_MSECS, dhcp_timer_fine, NULL);
247 #endif /* LWIP_DHCP */
249 sys_timeout(AUTOIP_TMR_INTERVAL, autoip_timer, NULL);
250 #endif /* LWIP_AUTOIP */
252 sys_timeout(IGMP_TMR_INTERVAL, igmp_timer, NULL);
253 #endif /* LWIP_IGMP */
255 sys_timeout(DNS_TMR_INTERVAL, dns_timer, NULL);
256 #endif /* LWIP_DNS */
258 if (tcpip_init_done != NULL) {
259 tcpip_init_done(tcpip_init_done_arg);
263 while (1) { /* MAIN Loop */
265 PrintBoth("In tcp_thread: main loop\n");
267 sys_mbox_fetch(mbox, (void *)&msg);
272 LWIP_DEBUGF(TCPIP_DEBUG, ("tcpip_thread: API message %p\n", (void *)msg));
273 msg->msg.apimsg->function(&(msg->msg.apimsg->msg));
275 #endif /* LWIP_NETCONN */
277 case TCPIP_MSG_INPKT:
278 LWIP_DEBUGF(TCPIP_DEBUG, ("tcpip_thread: PACKET %p\n", (void *)msg));
280 if (msg->msg.inp.netif->flags & NETIF_FLAG_ETHARP) {
281 ethernet_input(msg->msg.inp.p, msg->msg.inp.netif);
283 #endif /* LWIP_ARP */
284 { ip_input(msg->msg.inp.p, msg->msg.inp.netif);
286 memp_free(MEMP_TCPIP_MSG_INPKT, msg);
290 case TCPIP_MSG_NETIFAPI:
291 LWIP_DEBUGF(TCPIP_DEBUG, ("tcpip_thread: Netif API message %p\n", (void *)msg));
292 msg->msg.netifapimsg->function(&(msg->msg.netifapimsg->msg));
294 #endif /* LWIP_NETIF_API */
296 case TCPIP_MSG_CALLBACK:
297 LWIP_DEBUGF(TCPIP_DEBUG, ("tcpip_thread: CALLBACK %p\n", (void *)msg));
298 msg->msg.cb.f(msg->msg.cb.ctx);
299 memp_free(MEMP_TCPIP_MSG_API, msg);
302 case TCPIP_MSG_TIMEOUT:
303 LWIP_DEBUGF(TCPIP_DEBUG, ("tcpip_thread: TIMEOUT %p\n", (void *)msg));
305 if(msg->msg.tmo.msecs != 0xffffffff)
306 sys_timeout (msg->msg.tmo.msecs, msg->msg.tmo.h, msg->msg.tmo.arg);
308 sys_untimeout (msg->msg.tmo.h, msg->msg.tmo.arg);
309 memp_free(MEMP_TCPIP_MSG_API, msg);
319 * Pass a received packet to tcpip_thread for input processing
321 * @param p the received packet, p->payload pointing to the Ethernet header or
322 * to an IP header (if netif doesn't got NETIF_FLAG_ETHARP flag)
323 * @param inp the network interface on which the packet was received
326 tcpip_input(struct pbuf *p, struct netif *inp)
328 struct tcpip_msg *msg;
330 if (mbox != SYS_MBOX_NULL) {
331 msg = memp_malloc(MEMP_TCPIP_MSG_INPKT);
336 msg->type = TCPIP_MSG_INPKT;
338 msg->msg.inp.netif = inp;
339 if (sys_mbox_trypost(mbox, msg) != ERR_OK) {
340 memp_free(MEMP_TCPIP_MSG_INPKT, msg);
349 * Call a specific function in the thread context of
350 * tcpip_thread for easy access synchronization.
351 * A function called in that way may access lwIP core code
352 * without fearing concurrent access.
354 * @param f the function to call
355 * @param ctx parameter passed to f
356 * @param block 1 to block until the request is posted, 0 to non-blocking mode
357 * @return ERR_OK if the function was called, another err_t if not
360 tcpip_callback_with_block(void (*f)(void *ctx), void *ctx, u8_t block)
362 struct tcpip_msg *msg;
364 if (mbox != SYS_MBOX_NULL) {
365 msg = memp_malloc(MEMP_TCPIP_MSG_API);
370 msg->type = TCPIP_MSG_CALLBACK;
372 msg->msg.cb.ctx = ctx;
374 sys_mbox_post(mbox, msg);
376 if (sys_mbox_trypost(mbox, msg) != ERR_OK) {
377 memp_free(MEMP_TCPIP_MSG_API, msg);
387 tcpip_timeout(u32_t msecs, sys_timeout_handler h, void *arg)
389 struct tcpip_msg *msg;
391 if (mbox != SYS_MBOX_NULL) {
392 msg = memp_malloc(MEMP_TCPIP_MSG_API);
397 msg->type = TCPIP_MSG_TIMEOUT;
398 msg->msg.tmo.msecs = msecs;
400 msg->msg.tmo.arg = arg;
401 sys_mbox_post(mbox, msg);
409 * Call the lower part of a netconn_* function
410 * This function is then running in the thread context
411 * of tcpip_thread and has exclusive access to lwIP core code.
413 * @param apimsg a struct containing the function to call and its parameters
414 * @return ERR_OK if the function was called, another err_t if not
417 tcpip_apimsg(struct api_msg *apimsg)
419 struct tcpip_msg msg;
421 if (mbox != SYS_MBOX_NULL) {
422 msg.type = TCPIP_MSG_API;
423 msg.msg.apimsg = apimsg;
424 sys_mbox_post(mbox, &msg);
425 sys_arch_sem_wait(apimsg->msg.conn->op_completed, 0);
431 #if LWIP_TCPIP_CORE_LOCKING
433 * Call the lower part of a netconn_* function
434 * This function has exclusive access to lwIP core code by locking it
435 * before the function is called.
437 * @param apimsg a struct containing the function to call and its parameters
438 * @return ERR_OK (only for compatibility fo tcpip_apimsg())
441 tcpip_apimsg_lock(struct api_msg *apimsg)
444 apimsg->function(&(apimsg->msg));
449 #endif /* LWIP_TCPIP_CORE_LOCKING */
450 #endif /* LWIP_NETCONN */
453 #if !LWIP_TCPIP_CORE_LOCKING
455 * Much like tcpip_apimsg, but calls the lower part of a netifapi_*
458 * @param netifapimsg a struct containing the function to call and its parameters
459 * @return error code given back by the function that was called
462 tcpip_netifapi(struct netifapi_msg* netifapimsg)
464 struct tcpip_msg msg;
466 if (mbox != SYS_MBOX_NULL) {
467 netifapimsg->msg.sem = sys_sem_new(0);
468 if (netifapimsg->msg.sem == SYS_SEM_NULL) {
469 netifapimsg->msg.err = ERR_MEM;
470 return netifapimsg->msg.err;
473 msg.type = TCPIP_MSG_NETIFAPI;
474 msg.msg.netifapimsg = netifapimsg;
475 sys_mbox_post(mbox, &msg);
476 sys_sem_wait(netifapimsg->msg.sem);
477 sys_sem_free(netifapimsg->msg.sem);
478 return netifapimsg->msg.err;
482 #else /* !LWIP_TCPIP_CORE_LOCKING */
484 * Call the lower part of a netifapi_* function
485 * This function has exclusive access to lwIP core code by locking it
486 * before the function is called.
488 * @param netifapimsg a struct containing the function to call and its parameters
489 * @return ERR_OK (only for compatibility fo tcpip_netifapi())
492 tcpip_netifapi_lock(struct netifapi_msg* netifapimsg)
495 netifapimsg->function(&(netifapimsg->msg));
497 return netifapimsg->msg.err;
499 #endif /* !LWIP_TCPIP_CORE_LOCKING */
500 #endif /* LWIP_NETIF_API */
503 * Initialize this module:
504 * - initialize all sub modules
505 * - start the tcpip_thread
507 * @param initfunc a function to call when tcpip_thread is running and finished initializing
508 * @param arg argument to pass to initfunc
511 tcpip_init(void (* initfunc)(void *), void *arg)
515 tcpip_init_done = initfunc;
516 tcpip_init_done_arg = arg;
517 mbox = sys_mbox_new(TCPIP_MBOX_SIZE);
518 #if LWIP_TCPIP_CORE_LOCKING
519 lock_tcpip_core = sys_sem_new(1);
520 #endif /* LWIP_TCPIP_CORE_LOCKING */
522 sys_thread_new(TCPIP_THREAD_NAME, tcpip_thread, NULL, TCPIP_THREAD_STACKSIZE, TCPIP_THREAD_PRIO);