fix for corrupted graphics when manipulating config files
[open-ps2-loader.git] / modules / network / SMSTCPIP / tcpip.c
blob587ecadc1d9311607088a3047cf6d88887abb7b3
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:
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.
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.
27 * This file is part of the lwIP TCP/IP stack.
29 * Author: Adam Dunkels <adam@sics.se>
33 #include "lwip/opt.h"
35 #include "lwip/sys.h"
37 #include "lwip/memp.h"
38 #include "lwip/pbuf.h"
40 #include "lwip/ip.h"
41 #include "lwip/udp.h"
42 #include "lwip/tcp.h"
44 #include "lwip/tcpip.h"
46 static void (* tcpip_init_done)(void *arg) = NULL;
47 static void *tcpip_init_done_arg;
48 sys_mbox_t g_TCPIPMBox;
50 static void tcpip_thread ( void* arg ) {
52 struct tcpip_msg* msg;
54 ip_init ();
55 #if LWIP_UDP
56 udp_init ();
57 #endif /* LWIP_UDP */
58 #if LWIP_TCP
59 tcp_init ();
60 #endif /* LWIP_TCP */
61 tcpip_init_done ( tcpip_init_done_arg );
63 while ( 1 ) {
65 sys_mbox_fetch ( g_TCPIPMBox, ( void* )&msg );
67 switch ( msg -> type ) {
69 case TCPIP_MSG_API:
70 api_msg_input ( msg -> msg.apimsg );
71 break;
73 case TCPIP_MSG_INPUT:
74 ip_input ( msg -> msg.inp.p, msg -> msg.inp.netif );
75 break;
77 case TCPIP_MSG_CALLBACK:
78 msg -> msg.cb.f ( msg -> msg.cb.ctx );
79 break;
81 } /* end switch */
83 memp_free ( MEMP_TCPIP_MSG, msg );
85 } /* end while */
87 } /* end tcpip_thread */
89 err_t tcpip_input ( struct pbuf* p, struct netif* inp ) {
91 struct tcpip_msg *msg = memp_malloc ( MEMP_TCPIP_MSG );
93 if ( msg ) {
95 msg -> type = TCPIP_MSG_INPUT;
96 msg -> msg.inp.p = p;
97 msg -> msg.inp.netif = inp;
98 sys_mbox_post ( g_TCPIPMBox, msg );
100 return ERR_OK;
102 } else {
104 pbuf_free ( p );
105 return ERR_MEM;
107 } /* end else */
109 } /* end tcpip_input */
111 void tcpip_apimsg ( struct api_msg* apimsg ) {
113 struct tcpip_msg* msg = memp_malloc ( MEMP_TCPIP_MSG );
115 if ( msg ) {
117 msg -> type = TCPIP_MSG_API;
118 msg -> msg.apimsg = apimsg;
119 sys_mbox_post ( g_TCPIPMBox, msg );
121 } else memp_free ( MEMP_API_MSG, apimsg );
123 } /* end tcpip_apimsg */
125 void tcpip_init ( void ( *initfunc ) ( void* ), void* arg ) {
127 tcpip_init_done = initfunc;
128 tcpip_init_done_arg = arg;
130 g_TCPIPMBox = sys_mbox_new ();
132 sys_thread_new ( tcpip_thread, NULL, TCPIP_THREAD_PRIO );
134 } /* end tcpip_init */