4 * This file is part of BeRTOS.
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.
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.
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
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.
29 * Copyright 2010,2011 Develer S.r.l. (http://www.develer.com/)
33 * \author Andrea Righi <arighi@develer.com>
35 * \brief lwIP TCP/IP echo server listening on port 80.
38 #include <cfg/debug.h>
41 #include <cpu/power.h>
43 #include <drv/timer.h>
46 #include <kern/proc.h>
47 #include <kern/monitor.h>
49 #include <netif/ethernetif.h>
52 #include <lwip/ip_addr.h>
53 #include <lwip/netif.h>
54 #include <lwip/tcpip.h>
55 #include <lwip/dhcp.h>
58 /* Network interface global variables */
59 static struct ip_addr ipaddr, netmask, gw;
60 static struct netif netif;
62 static void init(void)
64 /* Enable all the interrupts */
67 /* Initialize debugging module (allow kprintf(), etc.) */
69 /* Initialize system timer */
73 * Kernel initialization: processes (allow to create and dispatch
74 * processes using proc_new()).
78 /* Initialize TCP/IP stack */
79 tcpip_init(NULL, NULL);
81 /* Bring up the network interface */
82 netif_add(&netif, &ipaddr, &netmask, &gw, NULL, ethernetif_init, tcpip_input);
83 netif_set_default(&netif);
89 static NORETURN void monitor_process(void)
96 kprintf("tot_req=%d [%d reqs/s]\n", tot_req, tot_req - start);
104 struct netconn *server;
106 /* Hardware initialization */
109 proc_new(monitor_process, NULL, KERN_MINSTACKSIZE * 2, NULL);
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");
121 server = netconn_new(NETCONN_TCP);
122 netconn_bind(server, IP_ADDR_ANY, 80);
123 netconn_listen(server);
127 struct netconn *client;
128 struct netbuf *rx_buf_conn;
132 client = netconn_accept(server);
137 rx_buf_conn = netconn_recv(client);
140 netbuf_data(rx_buf_conn, (void **)&rx_buf, &len);
142 netconn_write(client, rx_buf, len, NETCONN_COPY);
143 netbuf_delete(rx_buf_conn);
145 while (netconn_delete(client) != ERR_OK)