blob: ce8a2ca5d12616d4ba54de9814b30893fb2e02f6 [file] [log] [blame]
Travis Geiselbrecht1d0df692008-09-01 02:26:09 -07001/*
2 * Copyright (c) 2001-2004 Swedish Institute of Computer Science.
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without modification,
6 * are permitted provided that the following conditions are met:
7 *
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.
15 *
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
25 * OF SUCH DAMAGE.
26 *
27 * This file is part of the lwIP TCP/IP stack.
28 *
29 * Author: Adam Dunkels <adam@sics.se>
30 *
31 */
32
33#include "lwip/opt.h"
34
35#include "lwip/sys.h"
36
37#include "lwip/memp.h"
38#include "lwip/pbuf.h"
39
40#include "lwip/ip.h"
41#include "lwip/ip_frag.h"
42#include "lwip/udp.h"
43#include "lwip/tcp.h"
44
45#include "lwip/tcpip.h"
46
47static void (* tcpip_init_done)(void *arg) = NULL;
48static void *tcpip_init_done_arg;
49static sys_mbox_t mbox;
50
51#if LWIP_TCP
52static int tcpip_tcp_timer_active = 0;
53
54static void
55tcpip_tcp_timer(void *arg)
56{
57 (void)arg;
58
59 /* call TCP timer handler */
60 tcp_tmr();
61 /* timer still needed? */
62 if (tcp_active_pcbs || tcp_tw_pcbs) {
63 /* restart timer */
64 sys_timeout(TCP_TMR_INTERVAL, tcpip_tcp_timer, NULL);
65 } else {
66 /* disable timer */
67 tcpip_tcp_timer_active = 0;
68 }
69}
70
71#if !NO_SYS
72void
73tcp_timer_needed(void)
74{
75 /* timer is off but needed again? */
76 if (!tcpip_tcp_timer_active && (tcp_active_pcbs || tcp_tw_pcbs)) {
77 /* enable and start timer */
78 tcpip_tcp_timer_active = 1;
79 sys_timeout(TCP_TMR_INTERVAL, tcpip_tcp_timer, NULL);
80 }
81}
82#endif /* !NO_SYS */
83#endif /* LWIP_TCP */
84
85#if IP_REASSEMBLY
86static void
87ip_timer(void *data)
88{
89 LWIP_DEBUGF(TCPIP_DEBUG, ("tcpip: ip_reass_tmr()\n"));
90 ip_reass_tmr();
91 sys_timeout(1000, ip_timer, NULL);
92}
93#endif
94
95static void
96tcpip_thread(void *arg)
97{
98 struct tcpip_msg *msg;
99
100 (void)arg;
101
102 ip_init();
103#if LWIP_UDP
104 udp_init();
105#endif
106#if LWIP_TCP
107 tcp_init();
108#endif
109#if IP_REASSEMBLY
110 sys_timeout(1000, ip_timer, NULL);
111#endif
112 if (tcpip_init_done != NULL) {
113 tcpip_init_done(tcpip_init_done_arg);
114 }
115
116 while (1) { /* MAIN Loop */
117 sys_mbox_fetch(mbox, (void *)&msg);
118 switch (msg->type) {
119 case TCPIP_MSG_API:
120 LWIP_DEBUGF(TCPIP_DEBUG, ("tcpip_thread: API message %p\n", (void *)msg));
121 api_msg_input(msg->msg.apimsg);
122 break;
123 case TCPIP_MSG_INPUT:
124 LWIP_DEBUGF(TCPIP_DEBUG, ("tcpip_thread: IP packet %p\n", (void *)msg));
125 ip_input(msg->msg.inp.p, msg->msg.inp.netif);
126 break;
127 case TCPIP_MSG_CALLBACK:
128 LWIP_DEBUGF(TCPIP_DEBUG, ("tcpip_thread: CALLBACK %p\n", (void *)msg));
129 msg->msg.cb.f(msg->msg.cb.ctx);
130 break;
131 default:
132 break;
133 }
134 memp_free(MEMP_TCPIP_MSG, msg);
135 }
136}
137
138err_t
139tcpip_input(struct pbuf *p, struct netif *inp)
140{
141 struct tcpip_msg *msg;
142
143 msg = memp_malloc(MEMP_TCPIP_MSG);
144 if (msg == NULL) {
145 pbuf_free(p);
146 return ERR_MEM;
147 }
148
149 msg->type = TCPIP_MSG_INPUT;
150 msg->msg.inp.p = p;
151 msg->msg.inp.netif = inp;
152 sys_mbox_post(mbox, msg);
153 return ERR_OK;
154}
155
156err_t
157tcpip_callback(void (*f)(void *ctx), void *ctx)
158{
159 struct tcpip_msg *msg;
160
161 msg = memp_malloc(MEMP_TCPIP_MSG);
162 if (msg == NULL) {
163 return ERR_MEM;
164 }
165
166 msg->type = TCPIP_MSG_CALLBACK;
167 msg->msg.cb.f = f;
168 msg->msg.cb.ctx = ctx;
169 sys_mbox_post(mbox, msg);
170 return ERR_OK;
171}
172
173void
174tcpip_apimsg(struct api_msg *apimsg)
175{
176 struct tcpip_msg *msg;
177 msg = memp_malloc(MEMP_TCPIP_MSG);
178 if (msg == NULL) {
179 memp_free(MEMP_API_MSG, apimsg);
180 return;
181 }
182 msg->type = TCPIP_MSG_API;
183 msg->msg.apimsg = apimsg;
184 sys_mbox_post(mbox, msg);
185}
186
187void
188tcpip_init(void (* initfunc)(void *), void *arg)
189{
190 tcpip_init_done = initfunc;
191 tcpip_init_done_arg = arg;
192 mbox = sys_mbox_new();
193 sys_thread_new(tcpip_thread, NULL, TCPIP_THREAD_PRIO);
194}
195
196
197
198