]> git.karo-electronics.de Git - karo-tx-redboot.git/blob - packages/net/lwip_tcpip/v2_0/tests/tcpecho.c
be9bb6fed33f62277032afcb29c422acfd30c1c9
[karo-tx-redboot.git] / packages / net / lwip_tcpip / v2_0 / tests / tcpecho.c
1 /*
2  * Copyright (c) 2001, Swedish Institute of Computer Science.
3  * All rights reserved. 
4  *
5  * Redistribution and use in source and binary forms, with or without 
6  * modification, are permitted provided that the following conditions 
7  * are met: 
8  * 1. Redistributions of source code must retain the above copyright 
9  *    notice, this list of conditions and the following disclaimer. 
10  * 2. Redistributions in binary form must reproduce the above copyright 
11  *    notice, this list of conditions and the following disclaimer in the 
12  *    documentation and/or other materials provided with the distribution. 
13  * 3. Neither the name of the Institute nor the names of its contributors 
14  *    may be used to endorse or promote products derived from this software 
15  *    without specific prior written permission. 
16  *
17  * THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND 
18  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 
19  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 
20  * ARE DISCLAIMED.  IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE 
21  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 
22  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 
23  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 
24  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 
25  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 
26  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 
27  * SUCH DAMAGE. 
28  *
29  * This file is part of the lwIP TCP/IP stack.
30  * 
31  * Author: Adam Dunkels <adam@sics.se>
32  *
33  */
34
35 #include "lwip/sys.h"
36 #include "lwip/api.h"
37
38 static void 
39 tcpecho_thread(void *arg)
40 {
41   struct netconn *conn, *newconn;
42   err_t err;
43
44   /* Create a new connection identifier. */
45   conn = netconn_new(NETCONN_TCP);
46
47   /* Bind connection to well known port number 7. */
48   netconn_bind(conn, NULL, 7);
49
50   /* Tell connection to go into listening mode. */
51   netconn_listen(conn);
52
53   while(1) {
54
55     /* Grab new connection. */
56     newconn = netconn_accept(conn);
57     /* Process the new connection. */
58     if(newconn != NULL) {
59       struct netbuf *buf;
60       void *data;
61       u16_t len;
62       
63       while((buf = netconn_recv(newconn)) != NULL) {
64         do {
65           netbuf_data(buf, &data, &len);
66           err = netconn_write(newconn, data, len, NETCONN_COPY);
67           if(err != ERR_OK) {
68           }
69         } while(netbuf_next(buf) >= 0);
70         netbuf_delete(buf);     
71       }
72       /* Close connection and discard connection identifier. */
73       netconn_delete(newconn);
74     }
75   }
76 }
77
78 void
79 tmain(cyg_addrword_t p)
80 {
81   lwip_init();  
82   sys_thread_new(tcpecho_thread, (void*)"tcpecho",7);  
83 }
84
85 #define STACK_SIZE 0x1000
86 static char stack[STACK_SIZE];
87 static cyg_thread thread_data;
88 static cyg_handle_t thread_handle;
89
90 void
91 cyg_user_start(void)
92 {
93     // Create a main thread, so we can run the scheduler and have time 'pass'
94     cyg_thread_create(10,                // Priority - just a number
95                       tmain,          // entry
96                       0,                 // entry parameter
97                       "TCP echo test",        // Name
98                       &stack[0],         // Stack
99                       STACK_SIZE,        // Size
100                       &thread_handle,    // Handle
101                       &thread_data       // Thread data structure
102             );
103     cyg_thread_resume(thread_handle);  // Start it
104 }
105