00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036
00037
00038
00039 #include "lwip/opt.h"
00040
00041 #include "lwip/sys.h"
00042
00043 #include "lwip/memp.h"
00044 #include "lwip/pbuf.h"
00045
00046 #include "lwip/ip.h"
00047 #include "lwip/udp.h"
00048 #include "lwip/tcp.h"
00049
00050 #include "lwip/tcpip.h"
00051
00052 static void (* tcpip_init_done)(void *arg) = NULL;
00053 static void *tcpip_init_done_arg;
00054 static sys_mbox_t mbox;
00055 #if LWIP_TCP
00056 static int tcpip_tcp_timer_active = 0;
00057
00058
00059
00060 static void
00061 tcpip_tcp_timer(int signo)
00062 {
00063
00064 tcp_tmr();
00065 if(tcp_active_pcbs || tcp_tw_pcbs) {
00066 sys_timeout(TCP_TMR_INTERVAL, tcpip_tcp_timer, NULL);
00067 } else {
00068 tcpip_tcp_timer_active = 0;
00069 }
00070 }
00071
00072 void
00073 tcp_timer_needed(void)
00074 {
00075 if(!tcpip_tcp_timer_active && (tcp_active_pcbs || tcp_tw_pcbs)) {
00076 tcpip_tcp_timer_active = 1;
00077 sys_timeout(TCP_TMR_INTERVAL, tcpip_tcp_timer, NULL);
00078 }
00079 }
00080 #endif
00081
00082 static void
00083 tcpip_thread(void *arg)
00084 {
00085 struct tcpip_msg *msg;
00086
00087 (void)arg;
00088
00089 ip_init();
00090 #if LWIP_UDP
00091 udp_init();
00092 #endif
00093 #if LWIP_TCP
00094 tcp_init();
00095 sys_timeout(TCP_TMR_INTERVAL, (sys_timeout_handler)tcpip_tcp_timer, NULL);
00096 #endif
00097 if(tcpip_init_done != NULL) {
00098 tcpip_init_done(tcpip_init_done_arg);
00099 }
00100
00101 while(1) {
00102 sys_mbox_fetch(mbox, (void *)&msg);
00103 switch(msg->type) {
00104 case TCPIP_MSG_API:
00105 DEBUGF(TCPIP_DEBUG, ("tcpip_thread: API message %p\n", (void *)msg));
00106 api_msg_input(msg->msg.apimsg);
00107 break;
00108 case TCPIP_MSG_INPUT:
00109 DEBUGF(TCPIP_DEBUG, ("tcpip_thread: IP packet %p\n", (void *)msg));
00110 ip_input(msg->msg.inp.p, msg->msg.inp.netif);
00111 break;
00112 case TCPIP_MSG_CALLBACK:
00113 DEBUGF(TCPIP_DEBUG, ("tcpip_thread: CALLBACK %p\n", (void *)msg));
00114 msg->msg.cb.f(msg->msg.cb.ctx);
00115 break;
00116 default:
00117 break;
00118 }
00119 memp_freep(MEMP_TCPIP_MSG, msg);
00120 }
00121 }
00122
00123 err_t
00124 tcpip_input(struct pbuf *p, struct netif *inp)
00125 {
00126 struct tcpip_msg *msg;
00127
00128 msg = memp_mallocp(MEMP_TCPIP_MSG);
00129 if(msg == NULL) {
00130 pbuf_free(p);
00131 return ERR_MEM;
00132 }
00133
00134 msg->type = TCPIP_MSG_INPUT;
00135 msg->msg.inp.p = p;
00136 msg->msg.inp.netif = inp;
00137 sys_mbox_post(mbox, msg);
00138 return ERR_OK;
00139 }
00140
00141 err_t
00142 tcpip_callback(void (*f)(void *ctx), void *ctx)
00143 {
00144 struct tcpip_msg *msg;
00145
00146 msg = memp_mallocp(MEMP_TCPIP_MSG);
00147 if(msg == NULL) {
00148 return ERR_MEM;
00149 }
00150
00151 msg->type = TCPIP_MSG_CALLBACK;
00152 msg->msg.cb.f = f;
00153 msg->msg.cb.ctx = ctx;
00154 sys_mbox_post(mbox, msg);
00155 return ERR_OK;
00156 }
00157
00158 void
00159 tcpip_apimsg(struct api_msg *apimsg)
00160 {
00161 struct tcpip_msg *msg;
00162 msg = memp_mallocp(MEMP_TCPIP_MSG);
00163 if(msg == NULL) {
00164 memp_free(MEMP_API_MSG, apimsg);
00165 return;
00166 }
00167 msg->type = TCPIP_MSG_API;
00168 msg->msg.apimsg = apimsg;
00169 sys_mbox_post(mbox, msg);
00170 }
00171
00172 void
00173 tcpip_init(void (* initfunc)(void *), void *arg)
00174 {
00175 tcpip_init_done = initfunc;
00176 tcpip_init_done_arg = arg;
00177 mbox = sys_mbox_new();
00178 sys_thread_new((void *)tcpip_thread, NULL, 0);
00179
00180 }
00181
00182
00183
00184