]> git.sur5r.net Git - freertos/blob - Demo/Common/ethernet/lwIP/api/tcpip.c
git-svn-id: https://svn.code.sf.net/p/freertos/code/trunk@82 1d2547de-c912-0410-9cb9...
[freertos] / Demo / Common / ethernet / lwIP / api / tcpip.c
1 /*\r
2  * Copyright (c) 2001-2004 Swedish Institute of Computer Science.\r
3  * All rights reserved. \r
4  * \r
5  * Redistribution and use in source and binary forms, with or without modification, \r
6  * are permitted provided that the following conditions are met:\r
7  *\r
8  * 1. Redistributions of source code must retain the above copyright notice,\r
9  *    this list of conditions and the following disclaimer.\r
10  * 2. Redistributions in binary form must reproduce the above copyright notice,\r
11  *    this list of conditions and the following disclaimer in the documentation\r
12  *    and/or other materials provided with the distribution.\r
13  * 3. The name of the author may not be used to endorse or promote products\r
14  *    derived from this software without specific prior written permission. \r
15  *\r
16  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED \r
17  * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF \r
18  * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT \r
19  * SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, \r
20  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT \r
21  * OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS \r
22  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN \r
23  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING \r
24  * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY \r
25  * OF SUCH DAMAGE.\r
26  *\r
27  * This file is part of the lwIP TCP/IP stack.\r
28  * \r
29  * Author: Adam Dunkels <adam@sics.se>\r
30  *\r
31  */\r
32 \r
33 #include "lwip/opt.h"\r
34 \r
35 #include "lwip/sys.h"\r
36 \r
37 #include "lwip/memp.h"\r
38 #include "lwip/pbuf.h"\r
39 \r
40 #include "lwip/ip.h"\r
41 #include "lwip/ip_frag.h"\r
42 #include "lwip/udp.h"\r
43 #include "lwip/tcp.h"\r
44 \r
45 #include "lwip/tcpip.h"\r
46 \r
47 static void (* tcpip_init_done)(void *arg) = NULL;\r
48 static void *tcpip_init_done_arg;\r
49 static sys_mbox_t mbox;\r
50 \r
51 #if LWIP_TCP\r
52 static int tcpip_tcp_timer_active = 0;\r
53 \r
54 static void\r
55 tcpip_tcp_timer(void *arg)\r
56 {\r
57   (void)arg;\r
58 \r
59   /* call TCP timer handler */\r
60   tcp_tmr();\r
61   /* timer still needed? */\r
62   if (tcp_active_pcbs || tcp_tw_pcbs) {\r
63     /* restart timer */\r
64     sys_timeout(TCP_TMR_INTERVAL, tcpip_tcp_timer, NULL);\r
65   } else {\r
66     /* disable timer */\r
67     tcpip_tcp_timer_active = 0;\r
68   }\r
69 }\r
70 \r
71 #if !NO_SYS\r
72 void\r
73 tcp_timer_needed(void)\r
74 {\r
75   /* timer is off but needed again? */\r
76   if (!tcpip_tcp_timer_active && (tcp_active_pcbs || tcp_tw_pcbs)) {\r
77     /* enable and start timer */\r
78     tcpip_tcp_timer_active = 1;\r
79     sys_timeout(TCP_TMR_INTERVAL, tcpip_tcp_timer, NULL);\r
80   }\r
81 }\r
82 #endif /* !NO_SYS */\r
83 #endif /* LWIP_TCP */\r
84 \r
85 #if IP_REASSEMBLY\r
86 static void\r
87 ip_timer(void *data)\r
88 {\r
89   LWIP_DEBUGF(TCPIP_DEBUG, ("tcpip: ip_reass_tmr()\n"));\r
90   ip_reass_tmr();\r
91   sys_timeout(1000, ip_timer, NULL);\r
92 }\r
93 #endif\r
94 \r
95 static void\r
96 tcpip_thread(void *arg)\r
97 {\r
98   struct tcpip_msg *msg;\r
99 \r
100   (void)arg;\r
101 \r
102   ip_init();\r
103 #if LWIP_UDP  \r
104   udp_init();\r
105 #endif\r
106 #if LWIP_TCP\r
107   tcp_init();\r
108 #endif\r
109 #if IP_REASSEMBLY\r
110   sys_timeout(1000, ip_timer, NULL);\r
111 #endif\r
112   if (tcpip_init_done != NULL) {\r
113     tcpip_init_done(tcpip_init_done_arg);\r
114   }\r
115 \r
116   while (1) {                          /* MAIN Loop */\r
117     sys_mbox_fetch(mbox, (void *)&msg);\r
118     switch (msg->type) {\r
119     case TCPIP_MSG_API:\r
120       LWIP_DEBUGF(TCPIP_DEBUG, ("tcpip_thread: API message %p\n", (void *)msg));\r
121       api_msg_input(msg->msg.apimsg);\r
122       break;\r
123     case TCPIP_MSG_INPUT:\r
124       LWIP_DEBUGF(TCPIP_DEBUG, ("tcpip_thread: IP packet %p\n", (void *)msg));\r
125       ip_input(msg->msg.inp.p, msg->msg.inp.netif);\r
126       break;\r
127     case TCPIP_MSG_CALLBACK:\r
128       LWIP_DEBUGF(TCPIP_DEBUG, ("tcpip_thread: CALLBACK %p\n", (void *)msg));\r
129       msg->msg.cb.f(msg->msg.cb.ctx);\r
130       break;\r
131     default:\r
132       break;\r
133     }\r
134     memp_free(MEMP_TCPIP_MSG, msg);\r
135   }\r
136 }\r
137 \r
138 err_t\r
139 tcpip_input(struct pbuf *p, struct netif *inp)\r
140 {\r
141   struct tcpip_msg *msg;\r
142   \r
143   msg = memp_malloc(MEMP_TCPIP_MSG);\r
144   if (msg == NULL) {\r
145     pbuf_free(p);    \r
146     return ERR_MEM;  \r
147   }\r
148   \r
149   msg->type = TCPIP_MSG_INPUT;\r
150   msg->msg.inp.p = p;\r
151   msg->msg.inp.netif = inp;\r
152   sys_mbox_post(mbox, msg);\r
153   return ERR_OK;\r
154 }\r
155 \r
156 err_t\r
157 tcpip_callback(void (*f)(void *ctx), void *ctx)\r
158 {\r
159   struct tcpip_msg *msg;\r
160   \r
161   msg = memp_malloc(MEMP_TCPIP_MSG);\r
162   if (msg == NULL) {\r
163     return ERR_MEM;  \r
164   }\r
165   \r
166   msg->type = TCPIP_MSG_CALLBACK;\r
167   msg->msg.cb.f = f;\r
168   msg->msg.cb.ctx = ctx;\r
169   sys_mbox_post(mbox, msg);\r
170   return ERR_OK;\r
171 }\r
172 \r
173 void\r
174 tcpip_apimsg(struct api_msg *apimsg)\r
175 {\r
176   struct tcpip_msg *msg;\r
177   msg = memp_malloc(MEMP_TCPIP_MSG);\r
178   if (msg == NULL) {\r
179     memp_free(MEMP_API_MSG, apimsg);\r
180     return;\r
181   }\r
182   msg->type = TCPIP_MSG_API;\r
183   msg->msg.apimsg = apimsg;\r
184   sys_mbox_post(mbox, msg);\r
185 }\r
186 \r
187 void\r
188 tcpip_init(void (* initfunc)(void *), void *arg)\r
189 {\r
190   tcpip_init_done = initfunc;\r
191   tcpip_init_done_arg = arg;\r
192   mbox = sys_mbox_new();\r
193   sys_thread_new(tcpip_thread, NULL, TCPIP_THREAD_PRIO);\r
194 }\r
195 \r
196 \r
197 \r
198 \r