sam3x tcp_server: use the correct input function in netif
[bertos.git] / boards / sam3x-ek / examples / sam3x-ek_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,2011 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 <cfg/debug.h>
39
40 #include <cpu/irq.h>
41 #include <cpu/power.h>
42
43 #include <drv/timer.h>
44 #include <drv/ser.h>
45
46 #include <kern/proc.h>
47 #include <kern/monitor.h>
48
49 #include <netif/ethernetif.h>
50
51 #include <lwip/ip.h>
52 #include <lwip/ip_addr.h>
53 #include <lwip/netif.h>
54 #include <lwip/tcpip.h>
55 #include <lwip/dhcp.h>
56
57
58 /* Network interface global variables */
59 static struct ip_addr ipaddr, netmask, gw;
60 static struct netif netif;
61
62 static void init(void)
63 {
64         /* Enable all the interrupts */
65         IRQ_ENABLE;
66
67         /* Initialize debugging module (allow kprintf(), etc.) */
68         kdbg_init();
69         /* Initialize system timer */
70         timer_init();
71
72         /*
73          * Kernel initialization: processes (allow to create and dispatch
74          * processes using proc_new()).
75          */
76         proc_init();
77
78         /* Initialize TCP/IP stack */
79         tcpip_init(NULL, NULL);
80
81         /* Bring up the network interface */
82         netif_add(&netif, &ipaddr, &netmask, &gw, NULL, ethernetif_init, tcpip_input);
83         netif_set_default(&netif);
84         netif_set_up(&netif);
85 }
86
87 static int tot_req;
88
89 static NORETURN void monitor_process(void)
90 {
91         int start = tot_req;
92
93         while (1)
94         {
95                 monitor_report();
96                 kprintf("tot_req=%d [%d reqs/s]\n", tot_req, tot_req - start);
97                 start = tot_req;
98                 timer_delay(10000);
99         }
100 }
101
102 int main(void)
103 {
104         struct netconn *server;
105
106         /* Hardware initialization */
107         init();
108
109         proc_new(monitor_process, NULL, KERN_MINSTACKSIZE * 2, NULL);
110
111         dhcp_start(&netif);
112         while (!netif.ip_addr.addr)
113                 timer_delay(DHCP_FINE_TIMER_MSECS);
114         kprintf(">>> dhcp ok: ip = %d.%d.%d.%d (kernel %s)\n",
115                 (int)(netif.ip_addr.addr >>  0 & 0xff),
116                 (int)(netif.ip_addr.addr >>  8 & 0xff),
117                 (int)(netif.ip_addr.addr >> 16 & 0xff),
118                 (int)(netif.ip_addr.addr >> 24 & 0xff),
119                 CONFIG_KERN_PREEMPT ? "preempt" : "coop");
120
121         server = netconn_new(NETCONN_TCP);
122         netconn_bind(server, IP_ADDR_ANY, 80);
123         netconn_listen(server);
124
125         while (1)
126         {
127                 struct netconn *client;
128                 struct netbuf *rx_buf_conn;
129                 char *rx_buf;
130                 u16_t len;
131
132                 client = netconn_accept(server);
133                 if (!client)
134                         continue;
135
136                 tot_req++;
137                 rx_buf_conn = netconn_recv(client);
138                 if (rx_buf_conn)
139                 {
140                         netbuf_data(rx_buf_conn, (void **)&rx_buf, &len);
141                         if (rx_buf)
142                                 netconn_write(client, rx_buf, len, NETCONN_COPY);
143                         netbuf_delete(rx_buf_conn);
144                 }
145                 while (netconn_delete(client) != ERR_OK)
146                         cpu_relax();
147         }
148 }