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.
42 #include <cfg/debug.h>
45 #include <cpu/power.h>
47 #include <drv/timer.h>
50 #include <drv/dmac_sam3.h>
52 #include <kern/proc.h>
53 #include <kern/monitor.h>
55 #include <netif/ethernetif.h>
58 #include <lwip/ip_addr.h>
59 #include <lwip/netif.h>
60 #include <lwip/tcpip.h>
61 #include <lwip/dhcp.h>
67 /* Network interface global variables */
68 static struct ip_addr ipaddr, netmask, gw;
69 static struct netif netif;
72 // SD fat filesystem context
75 static FatFile in_file;
77 static void init(void)
79 /* Enable all the interrupts */
82 /* Initialize debugging module (allow kprintf(), etc.) */
84 /* Initialize system timer */
88 * Kernel initialization: processes (allow to create and dispatch
89 * processes using proc_new()).
93 /* Initialize TCP/IP stack */
94 tcpip_init(NULL, NULL);
96 /* Bring up the network interface */
97 netif_add(&netif, &ipaddr, &netmask, &gw, NULL, ethernetif_init, tcpip_input);
98 netif_set_default(&netif);
106 static NORETURN void monitor_process(void)
113 kprintf("tot_req=%d [%d reqs/s]\n", tot_req, tot_req - start);
119 static void get_fileName(char *revc_buf, char *name, size_t len)
121 char *p = strstr(revc_buf, "GET");
124 p += sizeof("GET") + 1;
125 for (size_t i = 0; *p != ' '; i++,p++)
134 static const char http_html_hdr_200[] = "HTTP/1.1 200 OK\r\nContent-type: text/html\r\n\r\n";
135 static const char http_html_hdr_404[] = "HTTP/1.1 404 Not Found\r\nContent-type: text/html\r\n\r\n";
137 static const char http_file_not_found[] = "\
138 <!DOCTYPE html PUBLIC \"-//IETF//DTD HTML 2.0//EN\"> \
139 <html><head><meta http-equiv=\"Content-Type\" content=\"text/html; charset=ISO-8859-1\"> \
140 <title>404 Not Found</title></head><body><img src=\"bertos_jpg.jpg\"><h1>404 Not Found</h1>\
141 <p>The requested URL was not found on this server.</p><hr>\
142 <address>BeRTOS simple HTTP server</address></body></html>";
145 static const char http_sd_not_present[] = " \
146 <!DOCTYPE html PUBLIC \"-//IETF//DTD HTML 2.0//EN\"> \
147 <html><head><meta http-equiv=\"Content-Type\" content=\"text/html; charset=ISO-8859-1\"> \
148 <title>BeRTOS simple HTTP Server</title></head><body><img src=\"bertos_jpg.jpg\"> \
149 <h1>BeRTOS simple HTTP Server</h1><p>Simple Http server, the site's pages are stored on SD card, check it if is present.</p><hr>\
150 <a href=\"http://www.bertos.org\">www.BeRTOS.org</a></body></html> \
153 static uint8_t tx_buf[2048];
155 #define IP_ADDR_TO_INT_TUPLE(addr) \
156 (int)((addr) >> 0 & 0xff), \
157 (int)((addr) >> 8 & 0xff), \
158 (int)((addr) >> 16 & 0xff), \
159 (int)((addr) >> 24 & 0xff)
162 static char file_name[80];
165 struct netconn *server;
168 /* Hardware initialization */
171 proc_new(monitor_process, NULL, KERN_MINSTACKSIZE * 2, NULL);
174 while (!netif.ip_addr.addr)
175 timer_delay(DHCP_FINE_TIMER_MSECS);
176 kprintf("dhcp ok: ip = %d.%d.%d.%d\n", IP_ADDR_TO_INT_TUPLE(netif.ip_addr.addr));
178 server = netconn_new(NETCONN_TCP);
179 netconn_bind(server, IP_ADDR_ANY, 80);
180 netconn_listen(server);
185 struct netconn *client;
186 struct netbuf *rx_buf_conn;
190 client = netconn_accept(server);
191 kprintf("remote ip = %d.%d.%d.%d\n", IP_ADDR_TO_INT_TUPLE(client->pcb.ip->remote_ip.addr));
192 kprintf("local ip = %d.%d.%d.%d\n", IP_ADDR_TO_INT_TUPLE(client->pcb.ip->local_ip.addr));
198 rx_buf_conn = netconn_recv(client);
201 netbuf_data(rx_buf_conn, (void **)&rx_buf, &len);
204 memset(file_name, 0, sizeof(file_name));
205 get_fileName(rx_buf, file_name, sizeof(file_name));
207 kprintf("%s\n", file_name);
208 if (strlen(file_name) == 0)
209 strcpy(file_name, "index.htm");
211 if (!strcmp("bertos_jpg.jpg", file_name))
213 netconn_write(client, bertos_jpg, sizeof(bertos_jpg), NETCONN_NOCOPY);
215 else if (SD_CARD_PRESENT())
217 bool sd_ok = sd_init(&sd, NULL, 0);
220 kprintf("Mount FAT filesystem.\n");
221 result = f_mount(0, &fs);
224 kprintf("Mounting FAT volumes error[%d]\n", result);
231 memset(tx_buf, 0, sizeof(tx_buf));
233 result = fatfile_open(&in_file, file_name, FA_OPEN_EXISTING | FA_READ);
238 kprintf("Opened file '%s' size %ld\n", file_name, in_file.fat_file.fsize);
240 netconn_write(client, http_html_hdr_200, sizeof(http_html_hdr_200) - 1, NETCONN_NOCOPY);
242 while (count < in_file.fat_file.fsize)
244 int len = kfile_read(&in_file.fd, tx_buf, sizeof(tx_buf));
245 netconn_write(client, tx_buf, len, NETCONN_COPY);
249 kfile_flush(&in_file.fd);
250 kfile_close(&in_file.fd);
252 kprintf("Sent: %d\n", count);
256 kprintf("Unable to open file: '%s' error[%d]\n", file_name, result);
257 netconn_write(client, http_html_hdr_404, sizeof(http_html_hdr_404) - 1, NETCONN_NOCOPY);
258 netconn_write(client, http_file_not_found, sizeof(http_file_not_found) - 1, NETCONN_NOCOPY);
263 kprintf("Umount FAT filesystem.\n");
267 netconn_write(client, http_html_hdr_404, sizeof(http_html_hdr_404) - 1, NETCONN_NOCOPY);
268 netconn_write(client, http_sd_not_present, sizeof(http_sd_not_present), NETCONN_NOCOPY);
271 netconn_close(client);
272 netbuf_delete(rx_buf_conn);
274 netconn_delete(client);