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 the /status page, the server return a json dictionary where are store
41 * some board status info, like board temperature, up-time, etc.
46 #include "hw/hw_adc.h"
47 #include "hw/hw_sdram.h"
49 #include <cfg/debug.h>
52 #include <cpu/power.h>
54 #include <drv/timer.h>
57 #include <drv/dmac_sam3.h>
60 #include <kern/proc.h>
61 #include <kern/monitor.h>
65 #include <netif/ethernetif.h>
68 #include <lwip/ip_addr.h>
69 #include <lwip/netif.h>
70 #include <lwip/tcpip.h>
71 #include <lwip/dhcp.h>
75 #include <icons/bertos.h>
80 /* Network interface global variables */
81 static struct ip_addr ipaddr, netmask, gw;
82 static struct netif netif;
84 typedef struct BoardStatus
86 char local_ip[sizeof("123.123.123.123")];
87 char last_connected_ip[sizeof("123.123.123.123")];
88 uint16_t internal_temp;
93 static BoardStatus status;
96 static void init(void)
98 /* Enable all the interrupts */
101 /* Initialize debugging module (allow kprintf(), etc.) */
103 /* Initialize system timer */
107 * Kernel initialization: processes (allow to create and dispatch
108 * processes using proc_new()).
113 /* Initialize TCP/IP stack */
114 tcpip_init(NULL, NULL);
116 /* Bring up the network interface */
117 netif_add(&netif, &ipaddr, &netmask, &gw, NULL, ethernetif_init, tcpip_input);
118 netif_set_default(&netif);
119 netif_set_up(&netif);
124 /* Enable the adc to read internal temperature sensor */
128 static NORETURN void status_process(void)
132 status.internal_temp = hw_convertToDegree(adc_read(ADC_TEMPERATURE_CH));
138 #define IP_ADDR_TO_INT_TUPLE(addr) \
139 (int)((addr) >> 0 & 0xff), \
140 (int)((addr) >> 8 & 0xff), \
141 (int)((addr) >> 16 & 0xff), \
142 (int)((addr) >> 24 & 0xff)
145 static uint8_t tx_buf[1024];
147 static int cgi_status(char *revc_buf, struct netconn *client)
151 //Update board status.
152 sprintf(status.last_connected_ip, "%d.%d.%d.%d", IP_ADDR_TO_INT_TUPLE(client->pcb.ip->remote_ip.addr));
153 sprintf(status.local_ip, "%d.%d.%d.%d", IP_ADDR_TO_INT_TUPLE(client->pcb.ip->local_ip.addr));
154 sprintf((char *)tx_buf, "[ %s, %s, %d.%d, %ld, %d ]", status.local_ip, status.last_connected_ip,
155 status.internal_temp / 10, status.internal_temp % 10,
156 status.up_time, status.tot_req);
161 netconn_write(client, tx_buf, strlen((char *)tx_buf), NETCONN_COPY);
165 static int cgi_logo(char *revc_buf, struct netconn *client)
170 netconn_write(client, bertos_logo_jpg, bertos_logo_jpg_len, NETCONN_NOCOPY);
174 static int cgi_temp(char *revc_buf, struct netconn *client)
178 sprintf((char *)tx_buf, "[ %d.%d ]", status.internal_temp / 10, status.internal_temp % 10);
181 netconn_write(client, tx_buf, strlen((char *)tx_buf), NETCONN_COPY);
185 static int cgi_echo(char *revc_buf, struct netconn *client)
188 netconn_write(client, revc_buf, strlen((char *)revc_buf), NETCONN_COPY);
192 static HttpCGI cgi_table[] =
194 { "echo", cgi_echo },
195 { "temp", cgi_temp },
196 { "status", cgi_status },
197 { "bertos_logo_jpg.jpg", cgi_logo },
204 struct netconn *server;
207 /* Hardware initialization */
210 proc_new(status_process, NULL, KERN_MINSTACKSIZE * 2, NULL);
213 while (!netif.ip_addr.addr)
214 timer_delay(DHCP_FINE_TIMER_MSECS);
215 kprintf("dhcp ok: ip = %d.%d.%d.%d\n", IP_ADDR_TO_INT_TUPLE(netif.ip_addr.addr));
217 server = netconn_new(NETCONN_TCP);
218 netconn_bind(server, IP_ADDR_ANY, 80);
219 netconn_listen(server);
223 http_server(server, cgi_table);