2 * Copyright (c) 2001-2004 Swedish Institute of Computer Science.
5 * Redistribution and use in source and binary forms, with or without modification,
6 * are permitted provided that the following conditions are met:
8 * 1. Redistributions of source code must retain the above copyright notice,
9 * this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright notice,
11 * this list of conditions and the following disclaimer in the documentation
12 * and/or other materials provided with the distribution.
13 * 3. The name of the author may not be used to endorse or promote products
14 * derived from this software without specific prior written permission.
16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
17 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
18 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT
19 * SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
20 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT
21 * OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
22 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
23 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
24 * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY
27 * This file is part of the lwIP TCP/IP stack.
29 * Author: Adam Dunkels <adam@sics.se>
37 #include "lwip/memp.h"
38 #include "lwip/pbuf.h"
44 #include "lwip/tcpip.h"
46 static void (* tcpip_init_done)(void *arg) = NULL;
47 static void *tcpip_init_done_arg;
48 static sys_mbox_t mbox;
50 static int tcpip_tcp_timer_active = 0;
55 tcpip_tcp_timer(void *arg)
60 if (tcp_active_pcbs || tcp_tw_pcbs) {
61 sys_timeout(TCP_TMR_INTERVAL, tcpip_tcp_timer, NULL);
63 tcpip_tcp_timer_active = 0;
68 tcp_timer_needed(void)
70 if (!tcpip_tcp_timer_active && (tcp_active_pcbs || tcp_tw_pcbs)) {
71 tcpip_tcp_timer_active = 1;
72 sys_timeout(TCP_TMR_INTERVAL, tcpip_tcp_timer, NULL);
78 tcpip_thread(void *arg)
80 struct tcpip_msg *msg;
91 if (tcpip_init_done != NULL) {
92 tcpip_init_done(tcpip_init_done_arg);
95 while (1) { /* MAIN Loop */
96 sys_mbox_fetch(mbox, (void *)&msg);
99 LWIP_DEBUGF(TCPIP_DEBUG, ("tcpip_thread: API message %p\n", (void *)msg));
100 api_msg_input(msg->msg.apimsg);
102 case TCPIP_MSG_INPUT:
103 LWIP_DEBUGF(TCPIP_DEBUG, ("tcpip_thread: IP packet %p\n", (void *)msg));
104 ip_input(msg->msg.inp.p, msg->msg.inp.netif);
106 case TCPIP_MSG_CALLBACK:
107 LWIP_DEBUGF(TCPIP_DEBUG, ("tcpip_thread: CALLBACK %p\n", (void *)msg));
108 msg->msg.cb.f(msg->msg.cb.ctx);
113 memp_free(MEMP_TCPIP_MSG, msg);
118 tcpip_input(struct pbuf *p, struct netif *inp)
120 struct tcpip_msg *msg;
122 msg = memp_malloc(MEMP_TCPIP_MSG);
128 msg->type = TCPIP_MSG_INPUT;
130 msg->msg.inp.netif = inp;
131 sys_mbox_post(mbox, msg);
136 tcpip_callback(void (*f)(void *ctx), void *ctx)
138 struct tcpip_msg *msg;
140 msg = memp_malloc(MEMP_TCPIP_MSG);
145 msg->type = TCPIP_MSG_CALLBACK;
147 msg->msg.cb.ctx = ctx;
148 sys_mbox_post(mbox, msg);
153 tcpip_apimsg(struct api_msg *apimsg)
155 struct tcpip_msg *msg;
156 msg = memp_malloc(MEMP_TCPIP_MSG);
158 memp_free(MEMP_API_MSG, apimsg);
161 msg->type = TCPIP_MSG_API;
162 msg->msg.apimsg = apimsg;
163 sys_mbox_post(mbox, msg);
167 tcpip_init(void (* initfunc)(void *), void *arg)
169 tcpip_init_done = initfunc;
170 tcpip_init_done_arg = arg;
171 mbox = sys_mbox_new();
172 sys_thread_new(tcpip_thread, NULL, TCPIP_THREAD_PRIO);