]> rtime.felk.cvut.cz Git - pes-rpp/rpp-lwip.git/blob - src/api/tcpip.c
37fb99dd6a9ac3538fd0c9d0b9fcdc558130e016
[pes-rpp/rpp-lwip.git] / src / api / tcpip.c
1 /*
2  * Copyright (c) 2001-2003 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/debug.h"
34
35 #include "lwip/opt.h"
36
37 #include "lwip/sys.h"
38
39 #include "lwip/memp.h"
40 #include "lwip/pbuf.h"
41
42 #include "lwip/ip.h"
43 #include "lwip/udp.h"
44 #include "lwip/tcp.h"
45
46 #include "lwip/tcpip.h"
47
48 static void (* tcpip_init_done)(void *arg) = NULL;
49 static void *tcpip_init_done_arg;
50 static sys_mbox_t mbox;
51
52 static int tcpip_tcp_timer_active = 0;
53
54
55 /*-----------------------------------------------------------------------------------*/
56 static void
57 tcpip_tcp_timer(void *arg)
58 {
59   (void)arg;
60
61   tcp_tmr();
62   if(tcp_active_pcbs || tcp_tw_pcbs) {
63         sys_timeout(TCP_TMR_INTERVAL, (sys_timeout_handler)tcpip_tcp_timer, NULL);
64   } else {
65         tcpip_tcp_timer_active = 0;
66   }
67 }
68
69 void
70 tcp_timer_needed(void)
71 {
72   if(!tcpip_tcp_timer_active && (tcp_active_pcbs || tcp_tw_pcbs)) {
73         tcpip_tcp_timer_active = 1;
74         sys_timeout(TCP_TMR_INTERVAL, (sys_timeout_handler)tcpip_tcp_timer, NULL);
75   }
76 }
77 /*-----------------------------------------------------------------------------------*/
78 static void
79 tcpip_thread(void *arg)
80 {
81   struct tcpip_msg *msg;
82
83   ip_init();
84   udp_init();
85   tcp_init();
86
87   if(tcpip_init_done != NULL) {
88     tcpip_init_done(tcpip_init_done_arg);
89   }
90
91   while(1) {                          /* MAIN Loop */
92     sys_mbox_fetch(mbox, (void *)&msg);
93     switch(msg->type) {
94     case TCPIP_MSG_API:
95       DEBUGF(TCPIP_DEBUG, ("tcpip_thread: API message %p\n", (void *)msg));
96       api_msg_input(msg->msg.apimsg);
97       break;
98     case TCPIP_MSG_INPUT:
99       DEBUGF(TCPIP_DEBUG, ("tcpip_thread: IP packet %p\n", (void *)msg));
100       ip_input(msg->msg.inp.p, msg->msg.inp.netif);
101       break;
102     case TCPIP_MSG_LINK:
103       DEBUGF(TCPIP_DEBUG, ("tcpip_thread: LINK packet %p\n", (void *)msg));
104       msg->msg.inp.netif->input(msg->msg.inp.p, msg->msg.inp.netif);
105       break;
106     default:
107       break;
108     }
109     memp_freep(MEMP_TCPIP_MSG, msg);
110   }
111 }
112 /*-----------------------------------------------------------------------------------*/
113 err_t
114 tcpip_input(struct pbuf *p, struct netif *inp)
115 {
116   struct tcpip_msg *msg;
117   
118   msg = memp_mallocp(MEMP_TCPIP_MSG);
119   if(msg == NULL) {
120     pbuf_free(p);    
121     return ERR_MEM;  
122   }
123   
124   msg->type = TCPIP_MSG_INPUT;
125   msg->msg.inp.p = p;
126   msg->msg.inp.netif = inp;
127   sys_mbox_post(mbox, msg);
128   return ERR_OK;
129 }
130 /*-----------------------------------------------------------------------------------*/
131 err_t
132 tcpip_link_input(struct pbuf *p, struct netif *inp)
133 {
134   struct tcpip_msg *msg;
135   
136   msg = memp_mallocp(MEMP_TCPIP_MSG);
137   if(msg == NULL) {
138     pbuf_free(p);    
139     return ERR_MEM;  
140   }
141   
142   msg->type = TCPIP_MSG_LINK;
143   msg->msg.inp.p = p;
144   msg->msg.inp.netif = inp;
145   sys_mbox_post(mbox, msg);
146   return ERR_OK;
147 }
148 /*-----------------------------------------------------------------------------------*/
149 void
150 tcpip_apimsg(struct api_msg *apimsg)
151 {
152   struct tcpip_msg *msg;
153   msg = memp_mallocp(MEMP_TCPIP_MSG);
154   if(msg == NULL) {
155     memp_free(MEMP_API_MSG, apimsg);
156     return;
157   }
158   msg->type = TCPIP_MSG_API;
159   msg->msg.apimsg = apimsg;
160   sys_mbox_post(mbox, msg);
161 }
162 /*-----------------------------------------------------------------------------------*/
163 void
164 tcpip_init(void (* initfunc)(void *), void *arg)
165 {
166   tcpip_init_done = initfunc;
167   tcpip_init_done_arg = arg;
168   mbox = sys_mbox_new();
169   sys_thread_new(tcpip_thread, NULL);
170 }
171 /*-----------------------------------------------------------------------------------*/
172
173
174