Main Page | Alphabetical List | Data Structures | File List | Data Fields | Globals | Related Pages

tcpip.c

Go to the documentation of this file.
00001 /*
00002  * Copyright (c) 2001-2003 Swedish Institute of Computer Science.
00003  * All rights reserved. 
00004  * 
00005  * Redistribution and use in source and binary forms, with or without modification, 
00006  * are permitted provided that the following conditions are met:
00007  *
00008  * 1. Redistributions of source code must retain the above copyright notice,
00009  *    this list of conditions and the following disclaimer.
00010  * 2. Redistributions in binary form must reproduce the above copyright notice,
00011  *    this list of conditions and the following disclaimer in the documentation
00012  *    and/or other materials provided with the distribution.
00013  * 3. The name of the author may not be used to endorse or promote products
00014  *    derived from this software without specific prior written permission. 
00015  *
00016  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED 
00017  * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF 
00018  * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT 
00019  * SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, 
00020  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT 
00021  * OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 
00022  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 
00023  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING 
00024  * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY 
00025  * OF SUCH DAMAGE.
00026  *
00027  * This file is part of the lwIP TCP/IP stack.
00028  * 
00029  * Author: Adam Dunkels <adam@sics.se>
00030  *
00031  * CHANGELOG: this file has been modified by Sergio Perez Alcañiz <serpeal@disca.upv.es> 
00032  *            Departamento de Informática de Sistemas y Computadores          
00033  *            Universidad Politécnica de Valencia                             
00034  *            Valencia (Spain)    
00035  *            Date: April 2003                                          
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 /* LWIP_TCP */
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) {                          /* MAIN Loop */
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 

Generated on Wed Jan 14 12:58:57 2004 for RTL-lwIP-0.4 by doxygen 1.3.4