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 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 "hw/hw_led.h"
40 #include <cfg/debug.h>
43 #include <cpu/power.h>
45 #include <drv/timer.h>
48 #include <kern/proc.h>
49 #include <kern/monitor.h>
51 #include <netif/ethernetif.h>
54 #include <lwip/ip_addr.h>
55 #include <lwip/netif.h>
56 #include <lwip/tcpip.h>
57 #include <lwip/dhcp.h>
61 /* Network interface global variables */
62 static struct ip_addr ipaddr, netmask, gw;
63 static struct netif netif;
65 static void init(void)
67 /* Enable all the interrupts */
70 /* Initialize debugging module (allow kprintf(), etc.) */
72 /* Initialize system timer */
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 */
82 * Kernel initialization: processes (allow to create and dispatch
83 * processes using proc_new()).
87 /* Initialize TCP/IP stack */
88 tcpip_init(NULL, NULL);
90 /* Bring up the network interface */
91 netif_add(&netif, &ipaddr, &netmask, &gw, NULL, ethernetif_init, ip_input);
92 netif_set_default(&netif);
98 static NORETURN void monitor_process(void)
105 kprintf("tot_req=%d [%d reqs/s]\n", tot_req, tot_req - start);
113 struct netconn *server;
115 /* Hardware initialization */
118 proc_new(monitor_process, NULL, KERN_MINSTACKSIZE * 2, NULL);
121 while (!netif.ip_addr.addr)
124 timer_delay(DHCP_FINE_TIMER_MSECS);
126 kprintf(">>> dhcp ok: ip = %#x (kernel %s)\n",
127 (unsigned int)netif.ip_addr.addr,
128 CONFIG_KERN_PREEMPT ? "preempt" : "coop");
130 server = netconn_new(NETCONN_TCP);
131 netconn_bind(server, IP_ADDR_ANY, 80);
132 netconn_listen(server);
136 struct netconn *client;
137 struct netbuf *rx_buf_conn;
141 client = netconn_accept(server);
146 rx_buf_conn = netconn_recv(client);
149 netbuf_data(rx_buf_conn, (void **)&rx_buf, &len);
151 netconn_write(client, rx_buf, len, NETCONN_COPY);
152 netbuf_delete(rx_buf_conn);
154 while (netconn_delete(client) != ERR_OK)