Add simple http server demo example.
[bertos.git] / boards / sam3x-ek / examples / sam3x-ek_http_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 "bertos.c"
39
40 #include "hw/hw_sd.h"
41
42 #include <cfg/debug.h>
43
44 #include <cpu/irq.h>
45 #include <cpu/power.h>
46
47 #include <drv/timer.h>
48 #include <drv/ser.h>
49 #include <drv/sd.h>
50 #include <drv/dmac_sam3.h>
51
52 #include <kern/proc.h>
53 #include <kern/monitor.h>
54
55 #include <netif/ethernetif.h>
56
57 #include <lwip/ip.h>
58 #include <lwip/ip_addr.h>
59 #include <lwip/netif.h>
60 #include <lwip/tcpip.h>
61 #include <lwip/dhcp.h>
62
63 #include <fs/fat.h>
64
65 #include <string.h>
66
67 /* Network interface global variables */
68 static struct ip_addr ipaddr, netmask, gw;
69 static struct netif netif;
70
71
72 // SD fat filesystem context
73 static Sd sd;
74 static FATFS fs;
75 static FatFile in_file;
76
77 static void init(void)
78 {
79         /* Enable all the interrupts */
80         IRQ_ENABLE;
81
82         /* Initialize debugging module (allow kprintf(), etc.) */
83         kdbg_init();
84         /* Initialize system timer */
85         timer_init();
86
87         /*
88          * Kernel initialization: processes (allow to create and dispatch
89          * processes using proc_new()).
90          */
91         proc_init();
92
93         /* Initialize TCP/IP stack */
94         tcpip_init(NULL, NULL);
95
96         /* Bring up the network interface */
97         netif_add(&netif, &ipaddr, &netmask, &gw, NULL, ethernetif_init, tcpip_input);
98         netif_set_default(&netif);
99         netif_set_up(&netif);
100
101         dmac_init();
102 }
103
104 static int tot_req;
105
106 static NORETURN void monitor_process(void)
107 {
108         int start = tot_req;
109
110         while (1)
111         {
112                 //monitor_report();
113                 kprintf("tot_req=%d [%d reqs/s]\n", tot_req, tot_req - start);
114                 start = tot_req;
115                 timer_delay(10000);
116         }
117 }
118
119 static void get_fileName(char *revc_buf, char *name, size_t len)
120 {
121         char *p = strstr(revc_buf, "GET");
122         if (p)
123         {
124                 p += sizeof("GET") + 1;
125                 for (size_t i = 0; *p != ' '; i++,p++)
126                 {
127                         if (i > len)
128                                 break;
129                         name[i] = *p;
130                 }
131         }
132 }
133
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";
136
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>";
143
144
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> \
151 ";
152
153 static uint8_t tx_buf[2048];
154
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)
160
161
162 static char file_name[80];
163 int main(void)
164 {
165         struct netconn *server;
166         FRESULT result;
167
168         /* Hardware initialization */
169         init();
170
171         proc_new(monitor_process, NULL, KERN_MINSTACKSIZE * 2, NULL);
172
173         dhcp_start(&netif);
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));
177
178         server = netconn_new(NETCONN_TCP);
179         netconn_bind(server, IP_ADDR_ANY, 80);
180         netconn_listen(server);
181
182         while (1)
183         {
184
185                 struct netconn *client;
186                 struct netbuf *rx_buf_conn;
187                 char *rx_buf;
188                 u16_t len;
189
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));
193
194                 if (!client)
195                         continue;
196
197                 tot_req++;
198                 rx_buf_conn = netconn_recv(client);
199                 if (rx_buf_conn)
200                 {
201                         netbuf_data(rx_buf_conn, (void **)&rx_buf, &len);
202                         if (rx_buf)
203                         {
204                                 memset(file_name, 0, sizeof(file_name));
205                                 get_fileName(rx_buf, file_name, sizeof(file_name));
206
207                                 kprintf("%s\n", file_name);
208                                 if (strlen(file_name) == 0)
209                                         strcpy(file_name, "index.htm");
210
211                                 if (!strcmp("bertos_jpg.jpg", file_name))
212                                 {
213                                         netconn_write(client, bertos_jpg, sizeof(bertos_jpg), NETCONN_NOCOPY);
214                                 }
215                                 else if (SD_CARD_PRESENT())
216                                 {
217                                         bool sd_ok = sd_init(&sd, NULL, 0);
218                                         if (sd_ok)
219                                         {
220                                                 kprintf("Mount FAT filesystem.\n");
221                                                 result = f_mount(0, &fs);
222                                                 if (result != FR_OK)
223                                                 {
224                                                         kprintf("Mounting FAT volumes error[%d]\n", result);
225                                                         sd_ok = false;
226                                                         f_mount(0, NULL);
227                                                 }
228
229                                                 if (sd_ok)
230                                                 {
231                                                         memset(tx_buf, 0, sizeof(tx_buf));
232
233                                                         result = fatfile_open(&in_file, file_name,  FA_OPEN_EXISTING | FA_READ);
234
235                                                         size_t count = 0;
236                                                         if (result == FR_OK)
237                                                         {
238                                                                 kprintf("Opened file '%s' size %ld\n", file_name, in_file.fat_file.fsize);
239
240                                                                 netconn_write(client, http_html_hdr_200, sizeof(http_html_hdr_200) - 1, NETCONN_NOCOPY);
241
242                                                                 while (count < in_file.fat_file.fsize)
243                                                                 {
244                                                                         int len = kfile_read(&in_file.fd, tx_buf, sizeof(tx_buf));
245                                                                         netconn_write(client, tx_buf, len, NETCONN_COPY);
246                                                                         count += len;
247                                                                 }
248
249                                                                 kfile_flush(&in_file.fd);
250                                                                 kfile_close(&in_file.fd);
251
252                                                                 kprintf("Sent: %d\n", count);
253                                                         }
254                                                         else
255                                                         {
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);
259                                                         }
260                                                 }
261                                         }
262                                         f_mount(0, NULL);
263                                         kprintf("Umount FAT filesystem.\n");
264                                 }
265                                 else
266                                 {
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);
269                                 }
270                         }
271                         netconn_close(client);
272                         netbuf_delete(rx_buf_conn);
273                 }
274                 netconn_delete(client);
275         }
276 }