Define default callback to read all request from sd. Comply to new http api.
[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 2011 Develer S.r.l. (http://www.develer.com/)
30  *
31  * -->
32  *
33  * \author Andrea Righi <arighi@develer.com>
34  * \author Daniele Basile <asterix@develer.com>
35  *
36  * \brief Simple Http server.
37  *
38  * This simple web server read the site's pages from SD card, and manage
39  * the cases where SD is not present or page not found, using embedded pages.
40  * Quering from browser the /status page, the server return a json dictionary where are store
41  * some board status info, like board temperature, up-time, etc.
42  */
43
44
45 #include "hw/hw_http.h"
46 #include "hw/hw_sd.h"
47 #include "hw/hw_adc.h"
48 #include "hw/hw_sdram.h"
49
50 // Define logging setting (for cfg/log.h module).
51 #define LOG_LEVEL         3
52 #define LOG_VERBOSITY     0
53 #include <cfg/log.h>
54 #include <cfg/debug.h>
55
56 #include <cpu/irq.h>
57 #include <cpu/power.h>
58
59 #include <drv/timer.h>
60 #include <drv/ser.h>
61 #include <drv/sd.h>
62 #include <drv/dmac_sam3.h>
63 #include <drv/adc.h>
64
65 #include <kern/proc.h>
66 #include <kern/monitor.h>
67
68 #include <net/http.h>
69
70 #include <netif/ethernetif.h>
71
72 #include <lwip/ip.h>
73 #include <lwip/ip_addr.h>
74 #include <lwip/netif.h>
75 #include <lwip/tcpip.h>
76 #include <lwip/dhcp.h>
77
78 #include <fs/fat.h>
79
80 #include <icons/bertos.h>
81
82 #include <stdio.h>
83 #include <string.h>
84
85 /* Network interface global variables */
86 static struct ip_addr ipaddr, netmask, gw;
87 static struct netif netif;
88
89 typedef struct BoardStatus
90 {
91         char local_ip[sizeof("123.123.123.123")];
92         char last_connected_ip[sizeof("123.123.123.123")];
93         uint16_t internal_temp;
94         ticks_t up_time;
95         size_t tot_req;
96 } BoardStatus;
97
98 static BoardStatus status;
99
100
101 static void init(void)
102 {
103         /* Enable all the interrupts */
104         IRQ_ENABLE;
105
106         /* Initialize debugging module (allow kprintf(), etc.) */
107         kdbg_init();
108         /* Initialize system timer */
109         timer_init();
110
111         /*
112          * Kernel initialization: processes (allow to create and dispatch
113          * processes using proc_new()).
114          */
115         proc_init();
116         sdram_init();
117
118         /* Initialize TCP/IP stack */
119         tcpip_init(NULL, NULL);
120
121         /* Bring up the network interface */
122         netif_add(&netif, &ipaddr, &netmask, &gw, NULL, ethernetif_init, tcpip_input);
123         netif_set_default(&netif);
124         netif_set_up(&netif);
125
126         dmac_init();
127
128         adc_init();
129         /* Enable the adc to read internal temperature sensor */
130         hw_enableTempRead();
131 }
132
133 static NORETURN void status_process(void)
134 {
135         while (1)
136         {
137                 status.internal_temp = hw_convertToDegree(adc_read(ADC_TEMPERATURE_CH));
138                 status.up_time++;
139                 timer_delay(1000);
140         }
141 }
142
143 #define IP_ADDR_TO_INT_TUPLE(addr) \
144                 (int)((addr) >>  0 & 0xff), \
145                 (int)((addr) >>  8 & 0xff), \
146                 (int)((addr) >> 16 & 0xff), \
147                 (int)((addr) >> 24 & 0xff)
148
149
150 static uint8_t tx_buf[2048];
151
152 static int cgi_status(struct netconn *client, const char *name, char *revc_buf, size_t revc_len)
153 {
154         (void)revc_buf;
155         (void)revc_len;
156         (void)name;
157
158         //Update board status.
159         sprintf(status.last_connected_ip, "%d.%d.%d.%d", IP_ADDR_TO_INT_TUPLE(client->pcb.ip->remote_ip.addr));
160         sprintf(status.local_ip, "%d.%d.%d.%d", IP_ADDR_TO_INT_TUPLE(client->pcb.ip->local_ip.addr));
161         sprintf((char *)tx_buf, "[ %s, %s, %d.%d, %ld, %d ]", status.local_ip, status.last_connected_ip,
162                                                                                                                         status.internal_temp / 10, status.internal_temp % 10,
163                                                                                                                         status.up_time, status.tot_req);
164
165         status.tot_req++;
166
167         http_sendOk(client);
168         netconn_write(client, tx_buf, strlen((char *)tx_buf), NETCONN_COPY);
169         return 0;
170 }
171
172 static int cgi_logo(struct netconn *client, const char *name, char *revc_buf, size_t revc_len)
173 {
174         (void)revc_buf;
175         (void)revc_len;
176         (void)name;
177
178         http_sendOk(client);
179         netconn_write(client, bertos_logo_jpg, bertos_logo_jpg_len, NETCONN_NOCOPY);
180         return 0;
181 }
182
183 static int cgi_temp(struct netconn *client, const char *name, char *revc_buf, size_t revc_len)
184 {
185         (void)revc_buf;
186         (void)revc_len;
187         (void)name;
188
189         sprintf((char *)tx_buf, "[ %d.%d ]", status.internal_temp / 10, status.internal_temp % 10);
190
191         http_sendOk(client);
192         netconn_write(client, tx_buf, strlen((char *)tx_buf), NETCONN_COPY);
193         return 0;
194 }
195
196 static int cgi_echo(struct netconn *client, const char *name, char *revc_buf, size_t revc_len)
197 {
198         (void)name;
199
200         http_sendOk(client);
201         netconn_write(client, revc_buf, revc_len, NETCONN_COPY);
202         return 0;
203 }
204
205 static int http_htmPageLoad(struct netconn *client, const char *name, char *revc_buf, size_t revc_len)
206 {
207         (void)revc_buf;
208         (void)revc_len;
209
210         if (SD_CARD_PRESENT())
211         {
212
213                 // SD fat filesystem context
214                 Sd sd;
215                 FATFS fs;
216                 FatFile in_file;
217                 FRESULT result;
218
219                 bool sd_ok = sd_init(&sd, NULL, 0);
220                 if (sd_ok)
221                 {
222                         LOG_INFO("Mount FAT filesystem.\n");
223                         result = f_mount(0, &fs);
224                         if (result != FR_OK)
225                         {
226                                 LOG_ERR("Mounting FAT volumes error[%d]\n", result);
227                                 sd_ok = false;
228                                 f_mount(0, NULL);
229                         }
230
231                         if (sd_ok)
232                         {
233                                 result = fatfile_open(&in_file, name,  FA_OPEN_EXISTING | FA_READ);
234
235                                 size_t count = 0;
236                                 if (result == FR_OK)
237                                 {
238                                         LOG_INFO("Opened file '%s' size %ld\n", name, in_file.fat_file.fsize);
239
240                                         http_sendOk(client);
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                                         LOG_INFO("Sent: %d\n", count);
253                                 }
254                                 else
255                                 {
256                                         LOG_ERR("Unable to open file: '%s' error[%d]\n",  name, result);
257                                         http_sendFileNotFound(client);
258                                         netconn_write(client, http_file_not_found, http_file_not_found_len - 1, NETCONN_NOCOPY);
259                                 }
260                         }
261                 }
262                 f_mount(0, NULL);
263                 LOG_INFO("Umount FAT filesystem.\n");
264         }
265         else
266         {
267                 http_sendFileNotFound(client);
268                 netconn_write(client, http_sd_not_present, http_sd_not_present_len, NETCONN_NOCOPY);
269         }
270
271         return 0;
272 }
273
274 static int cgi_chipInfo(struct netconn *client, const char *name, char *revc_buf, size_t revc_len)
275 {
276         (void)revc_buf;
277         (void)revc_len;
278         (void)name;
279
280         sprintf((char *)tx_buf, "[ %s, %s, %s, %s, %s ]",
281         chipid_eproc_name(CHIPID_EPRCOC()),
282         chipid_archnames(CHIPID_ARCH()),
283         chipid_sramsize(CHIPID_SRAMSIZ()),
284         chipid_nvpsize(CHIPID_NVPSIZ()),
285         chipid_nvptype(CHIPID_NVTYP()));
286
287         http_sendOk(client);
288         netconn_write(client, tx_buf, strlen((char *)tx_buf), NETCONN_COPY);
289
290         return 0;
291 }
292
293 static int cgi_error(struct netconn *client, const char *name, char *revc_buf, size_t revc_len)
294 {
295         (void)revc_buf;
296         (void)revc_len;
297         (void)name;
298         (void)client;
299
300         return -1;
301 }
302
303 static HttpCGI cgi_table[] =
304 {
305         { CGI_MATCH_NAME, "echo",                cgi_echo          },
306         { CGI_MATCH_NAME, "temp",                cgi_temp          },
307         { CGI_MATCH_NAME, "status",              cgi_status        },
308         { CGI_MATCH_NAME, "chipinfo",            cgi_chipInfo      },
309         { CGI_MATCH_NAME, "error_test",          cgi_error         },
310         { CGI_MATCH_NAME, "bertos_logo_jpg",     cgi_logo          },
311         { CGI_MATCH_NONE,  NULL,                 NULL              }
312 };
313
314
315 int main(void)
316 {
317         struct netconn *server;
318
319         /* Hardware initialization */
320         init();
321         http_init(http_htmPageLoad, cgi_table);
322
323         proc_new(status_process, NULL, KERN_MINSTACKSIZE * 2, NULL);
324
325         dhcp_start(&netif);
326         while (!netif.ip_addr.addr)
327                 timer_delay(DHCP_FINE_TIMER_MSECS);
328         kprintf("dhcp ok: ip = %d.%d.%d.%d\n", IP_ADDR_TO_INT_TUPLE(netif.ip_addr.addr));
329
330         server = netconn_new(NETCONN_TCP);
331         netconn_bind(server, IP_ADDR_ANY, 80);
332         netconn_listen(server);
333
334         while (1)
335         {
336                 http_poll(server);
337         }
338 }