Update the tcp_server example to use the correct ip input callback.
[bertos.git] / boards / at91sam7x-ek / examples / tcp_server / main.c
1 /**
2  * \file
3  * <!--
4  * This file is part of BeRTOS.
5  *
6  * Bertos is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation; either version 2 of the License, or
9  * (at your option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program; if not, write to the Free Software
18  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
19  *
20  * As a special exception, you may use this file as part of a free software
21  * library without restriction.  Specifically, if other files instantiate
22  * templates or use macros or inline functions from this file, or you compile
23  * this file and link it with other files to produce an executable, this
24  * file does not by itself cause the resulting executable to be covered by
25  * the GNU General Public License.  This exception does not however
26  * invalidate any other reasons why the executable file might be covered by
27  * the GNU General Public License.
28  *
29  * Copyright 2010 Develer S.r.l. (http://www.develer.com/)
30  *
31  * -->
32  *
33  * \author Andrea Righi <arighi@develer.com>
34  *
35  * \brief lwIP TCP/IP echo server listening on port 80.
36  */
37
38 #include "hw/hw_led.h"
39
40 #include <cfg/debug.h>
41
42 #include <cpu/irq.h>
43 #include <cpu/power.h>
44
45 #include <drv/timer.h>
46 #include <drv/ser.h>
47
48 #include <kern/proc.h>
49 #include <kern/monitor.h>
50
51 #include <netif/ethernetif.h>
52
53 #include <lwip/ip.h>
54 #include <lwip/ip_addr.h>
55 #include <lwip/netif.h>
56 #include <lwip/tcpip.h>
57 #include <lwip/dhcp.h>
58
59 static Serial out;
60
61 /* Network interface global variables */
62 static struct ip_addr ipaddr, netmask, gw;
63 static struct netif netif;
64
65 static void init(void)
66 {
67         /* Enable all the interrupts */
68         IRQ_ENABLE;
69
70         /* Initialize debugging module (allow kprintf(), etc.) */
71         kdbg_init();
72         /* Initialize system timer */
73         timer_init();
74         /* Initialize UART0 */
75         ser_init(&out, SER_UART0);
76         /* Configure UART0 to work at 115.200 bps */
77         ser_setbaudrate(&out, 115200);
78         /* Initialize LED driver */
79         LED_INIT();
80
81         /*
82          * Kernel initialization: processes (allow to create and dispatch
83          * processes using proc_new()).
84          */
85         proc_init();
86
87         /* Initialize TCP/IP stack */
88         tcpip_init(NULL, NULL);
89
90         /* Bring up the network interface */
91         netif_add(&netif, &ipaddr, &netmask, &gw, NULL, ethernetif_init, tcpip_input);
92         netif_set_default(&netif);
93         netif_set_up(&netif);
94 }
95
96 static int tot_req;
97
98 static NORETURN void monitor_process(void)
99 {
100         int start = tot_req;
101
102         while (1)
103         {
104                 monitor_report();
105                 kprintf("tot_req=%d [%d reqs/s]\n", tot_req, tot_req - start);
106                 start = tot_req;
107                 timer_delay(1000);
108         }
109 }
110
111 int main(void)
112 {
113         struct netconn *server;
114
115         /* Hardware initialization */
116         init();
117
118         proc_new(monitor_process, NULL, KERN_MINSTACKSIZE * 2, NULL);
119
120         dhcp_start(&netif);
121         /*
122          * Here we wait for an ip address, but it's not strictly
123          * necessary. The address is obtained in background and
124          * as long as we don't use network functions, we could go
125          * on with initialization
126          */
127         while (!netif.ip_addr.addr)
128                 timer_delay(200);
129         kprintf(">>> dhcp ok: ip = ip = %s (kernel %s)\n",
130                 ip_ntoa(&netif.ip_addr.addr),
131                 CONFIG_KERN_PREEMPT ? "preempt" : "coop");
132
133         server = netconn_new(NETCONN_TCP);
134         netconn_bind(server, IP_ADDR_ANY, 80);
135         netconn_listen(server);
136
137         while (1)
138         {
139                 struct netconn *client;
140                 struct netbuf *rx_buf_conn;
141                 char *rx_buf;
142                 u16_t len;
143
144                 client = netconn_accept(server);
145                 if (!client)
146                         continue;
147
148                 tot_req++;
149                 rx_buf_conn = netconn_recv(client);
150                 if (rx_buf_conn)
151                 {
152                         netbuf_data(rx_buf_conn, (void **)&rx_buf, &len);
153                         if (rx_buf)
154                                 netconn_write(client, rx_buf, len, NETCONN_COPY);
155                         netbuf_delete(rx_buf_conn);
156                 }
157                 while (netconn_delete(client) != ERR_OK)
158                         cpu_relax();
159         }
160 }