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 2011 Develer S.r.l. (http://www.develer.com/)
33 * \author Andrea Righi <arighi@develer.com>
34 * \author Daniele Basile <asterix@develer.com>
36 * \brief Simple Http server.
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 some cgi page, the server return a json dictionary.
44 #include "hw/hw_http.h"
46 #include "hw/hw_adc.h"
47 #include "hw/hw_sdram.h"
49 // Define logging setting (for cfg/log.h module).
51 #define LOG_VERBOSITY 0
53 #include <cfg/debug.h>
56 #include <cpu/power.h>
58 #include <drv/timer.h>
61 #include <drv/dmac_sam3.h>
64 #include <kern/proc.h>
65 #include <kern/monitor.h>
69 #include <netif/ethernetif.h>
72 #include <lwip/ip_addr.h>
73 #include <lwip/netif.h>
74 #include <lwip/tcpip.h>
75 #include <lwip/dhcp.h>
79 #include <icons/bertos.h>
84 /* Network interface global variables */
85 static struct ip_addr ipaddr, netmask, gw;
86 static struct netif netif;
88 typedef struct BoardStatus
90 char local_ip[sizeof("123.123.123.123")];
91 char last_connected_ip[sizeof("123.123.123.123")];
92 uint16_t internal_temp;
97 static BoardStatus status;
100 static void init(void)
102 /* Enable all the interrupts */
105 /* Initialize debugging module (allow kprintf(), etc.) */
107 /* Initialize system timer */
111 * Kernel initialization: processes (allow to create and dispatch
112 * processes using proc_new()).
117 /* Initialize TCP/IP stack */
118 tcpip_init(NULL, NULL);
120 /* Bring up the network interface */
121 netif_add(&netif, &ipaddr, &netmask, &gw, NULL, ethernetif_init, tcpip_input);
122 netif_set_default(&netif);
123 netif_set_up(&netif);
128 /* Enable the adc to read internal temperature sensor */
132 static NORETURN void status_process(void)
136 status.internal_temp = hw_convertToDegree(adc_read(ADC_TEMPERATURE_CH));
142 /* Macro to unpack the ip addres from lwip format */
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)
150 static uint8_t tx_buf[2048];
153 * Return a JSON string of board status.
155 static int cgi_status(struct netconn *client, const char *name, char *revc_buf, size_t revc_len)
161 //Update board status.
162 sprintf(status.last_connected_ip, "%d.%d.%d.%d", IP_ADDR_TO_INT_TUPLE(client->pcb.ip->remote_ip.addr));
163 sprintf(status.local_ip, "%d.%d.%d.%d", IP_ADDR_TO_INT_TUPLE(client->pcb.ip->local_ip.addr));
164 sprintf((char *)tx_buf, "[ %s, %s, %d.%d, %ld, %d ]", status.local_ip, status.last_connected_ip,
165 status.internal_temp / 10, status.internal_temp % 10,
166 status.up_time, status.tot_req);
171 netconn_write(client, tx_buf, strlen((char *)tx_buf), NETCONN_COPY);
175 static int cgi_logo(struct netconn *client, const char *name, char *revc_buf, size_t revc_len)
182 netconn_write(client, bertos_logo_jpg, bertos_logo_jpg_len, NETCONN_NOCOPY);
187 * Return the internal micro temperature string.
189 static int cgi_temp(struct netconn *client, const char *name, char *revc_buf, size_t revc_len)
195 sprintf((char *)tx_buf, "[ %d.%d ]", status.internal_temp / 10, status.internal_temp % 10);
198 netconn_write(client, tx_buf, strlen((char *)tx_buf), NETCONN_COPY);
203 * Reply to client the request string.
205 static int cgi_echo(struct netconn *client, const char *name, char *revc_buf, size_t revc_len)
210 netconn_write(client, revc_buf, revc_len, NETCONN_COPY);
216 * Default function that http server call every client request, if it doesn't match a cgi table.
217 * In this implementation all client request are associate to real file stored on FAT file
218 * sistem on SD card. If the file there is not on SD card the server reply to client the
219 * error File not found, and send an harcoded page. In the same way, the server reply
220 * error page if the SD card is not present.
223 static int http_htmPageLoad(struct netconn *client, const char *name, char *revc_buf, size_t revc_len)
228 if (SD_CARD_PRESENT())
231 // SD fat filesystem context
237 bool sd_ok = sd_init(&sd, NULL, 0);
240 LOG_INFO("Mount FAT filesystem.\n");
241 result = f_mount(0, &fs);
244 LOG_ERR("Mounting FAT volumes error[%d]\n", result);
251 result = fatfile_open(&in_file, name, FA_OPEN_EXISTING | FA_READ);
256 LOG_INFO("Opened file '%s' size %ld\n", name, in_file.fat_file.fsize);
260 while (count < in_file.fat_file.fsize)
262 int len = kfile_read(&in_file.fd, tx_buf, sizeof(tx_buf));
263 netconn_write(client, tx_buf, len, NETCONN_COPY);
267 kfile_flush(&in_file.fd);
268 kfile_close(&in_file.fd);
270 LOG_INFO("Sent: %d\n", count);
274 LOG_ERR("Unable to open file: '%s' error[%d]\n", name, result);
275 http_sendFileNotFound(client);
276 netconn_write(client, http_file_not_found, http_file_not_found_len - 1, NETCONN_NOCOPY);
281 LOG_INFO("Umount FAT filesystem.\n");
285 http_sendFileNotFound(client);
286 netconn_write(client, http_sd_not_present, http_sd_not_present_len, NETCONN_NOCOPY);
293 * Return to client a string that display the CHIP ID information.
294 * See datasheet for more detail.
296 static int cgi_chipInfo(struct netconn *client, const char *name, char *revc_buf, size_t revc_len)
302 sprintf((char *)tx_buf, "[ %s, %s, %s, %s, %s ]",
303 chipid_eproc_name(CHIPID_EPRCOC()),
304 chipid_archnames(CHIPID_ARCH()),
305 chipid_sramsize(CHIPID_SRAMSIZ()),
306 chipid_nvpsize(CHIPID_NVPSIZ()),
307 chipid_nvptype(CHIPID_NVTYP()));
310 netconn_write(client, tx_buf, strlen((char *)tx_buf), NETCONN_COPY);
315 static int cgi_error(struct netconn *client, const char *name, char *revc_buf, size_t revc_len)
326 * Static cgi table where we associate callback to page.
328 static HttpCGI cgi_table[] =
330 { CGI_MATCH_NAME, "echo", cgi_echo },
331 { CGI_MATCH_NAME, "temp", cgi_temp },
332 { CGI_MATCH_NAME, "status", cgi_status },
333 { CGI_MATCH_NAME, "chipinfo", cgi_chipInfo },
334 { CGI_MATCH_NAME, "error_test", cgi_error },
335 { CGI_MATCH_NAME, "bertos_logo_jpg", cgi_logo },
336 { CGI_MATCH_NONE, NULL, NULL }
342 struct netconn *server;
344 /* Hardware initialization */
346 http_init(http_htmPageLoad, cgi_table);
348 proc_new(status_process, NULL, KERN_MINSTACKSIZE * 2, NULL);
351 while (!netif.ip_addr.addr)
352 timer_delay(DHCP_FINE_TIMER_MSECS);
353 kprintf("dhcp ok: ip = %d.%d.%d.%d\n", IP_ADDR_TO_INT_TUPLE(netif.ip_addr.addr));
355 server = netconn_new(NETCONN_TCP);
356 netconn_bind(server, IP_ADDR_ANY, 80);
357 netconn_listen(server);