]> rtime.felk.cvut.cz Git - pes-rpp/rpp-lwip.git/blob - src/api/tcpip.c
Surround definition of tcp_timer_needed with #if !NO_SYS #endif. (see lwip-users...
[pes-rpp/rpp-lwip.git] / src / api / tcpip.c
1 /*
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/udp.h"
42 #include "lwip/tcp.h"
43
44 #include "lwip/tcpip.h"
45
46 static void (* tcpip_init_done)(void *arg) = NULL;
47 static void *tcpip_init_done_arg;
48 static sys_mbox_t mbox;
49
50 #if LWIP_TCP
51 static int tcpip_tcp_timer_active = 0;
52
53 static void
54 tcpip_tcp_timer(void *arg)
55 {
56   (void)arg;
57
58   /* call TCP timer handler */
59   tcp_tmr();
60   /* timer still needed? */
61   if (tcp_active_pcbs || tcp_tw_pcbs) {
62     /* restart timer */
63     sys_timeout(TCP_TMR_INTERVAL, tcpip_tcp_timer, NULL);
64   } else {
65     /* disable timer */
66     tcpip_tcp_timer_active = 0;
67   }
68 }
69
70 #if !NO_SYS
71 void
72 tcp_timer_needed(void)
73 {
74   /* timer is off but needed again? */
75   if (!tcpip_tcp_timer_active && (tcp_active_pcbs || tcp_tw_pcbs)) {
76     /* enable and start timer */
77     tcpip_tcp_timer_active = 1;
78     sys_timeout(TCP_TMR_INTERVAL, tcpip_tcp_timer, NULL);
79   }
80 }
81 #endif /* !NO_SYS */
82 #endif /* LWIP_TCP */
83
84 static void
85 tcpip_thread(void *arg)
86 {
87   struct tcpip_msg *msg;
88
89   (void)arg;
90
91   ip_init();
92 #if LWIP_UDP  
93   udp_init();
94 #endif
95 #if LWIP_TCP
96   tcp_init();
97 #endif
98   if (tcpip_init_done != NULL) {
99     tcpip_init_done(tcpip_init_done_arg);
100   }
101
102   while (1) {                          /* MAIN Loop */
103     sys_mbox_fetch(mbox, (void *)&msg);
104     switch (msg->type) {
105     case TCPIP_MSG_API:
106       LWIP_DEBUGF(TCPIP_DEBUG, ("tcpip_thread: API message %p\n", (void *)msg));
107       api_msg_input(msg->msg.apimsg);
108       break;
109     case TCPIP_MSG_INPUT:
110       LWIP_DEBUGF(TCPIP_DEBUG, ("tcpip_thread: IP packet %p\n", (void *)msg));
111       ip_input(msg->msg.inp.p, msg->msg.inp.netif);
112       break;
113     case TCPIP_MSG_CALLBACK:
114       LWIP_DEBUGF(TCPIP_DEBUG, ("tcpip_thread: CALLBACK %p\n", (void *)msg));
115       msg->msg.cb.f(msg->msg.cb.ctx);
116       break;
117     default:
118       break;
119     }
120     memp_free(MEMP_TCPIP_MSG, msg);
121   }
122 }
123
124 err_t
125 tcpip_input(struct pbuf *p, struct netif *inp)
126 {
127   struct tcpip_msg *msg;
128   
129   msg = memp_malloc(MEMP_TCPIP_MSG);
130   if (msg == NULL) {
131     pbuf_free(p);    
132     return ERR_MEM;  
133   }
134   
135   msg->type = TCPIP_MSG_INPUT;
136   msg->msg.inp.p = p;
137   msg->msg.inp.netif = inp;
138   sys_mbox_post(mbox, msg);
139   return ERR_OK;
140 }
141
142 err_t
143 tcpip_callback(void (*f)(void *ctx), void *ctx)
144 {
145   struct tcpip_msg *msg;
146   
147   msg = memp_malloc(MEMP_TCPIP_MSG);
148   if (msg == NULL) {
149     return ERR_MEM;  
150   }
151   
152   msg->type = TCPIP_MSG_CALLBACK;
153   msg->msg.cb.f = f;
154   msg->msg.cb.ctx = ctx;
155   sys_mbox_post(mbox, msg);
156   return ERR_OK;
157 }
158
159 void
160 tcpip_apimsg(struct api_msg *apimsg)
161 {
162   struct tcpip_msg *msg;
163   msg = memp_malloc(MEMP_TCPIP_MSG);
164   if (msg == NULL) {
165     memp_free(MEMP_API_MSG, apimsg);
166     return;
167   }
168   msg->type = TCPIP_MSG_API;
169   msg->msg.apimsg = apimsg;
170   sys_mbox_post(mbox, msg);
171 }
172
173 void
174 tcpip_init(void (* initfunc)(void *), void *arg)
175 {
176   tcpip_init_done = initfunc;
177   tcpip_init_done_arg = arg;
178   mbox = sys_mbox_new();
179   sys_thread_new(tcpip_thread, NULL, TCPIP_THREAD_PRIO);
180 }
181
182
183
184