Remove sd dependence. Add default callback to match all request that doesn't match...
[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_sd.h"
46 #include "hw/hw_adc.h"
47 #include "hw/hw_sdram.h"
48
49 #include <cfg/debug.h>
50
51 #include <cpu/irq.h>
52 #include <cpu/power.h>
53
54 #include <drv/timer.h>
55 #include <drv/ser.h>
56 #include <drv/sd.h>
57 #include <drv/dmac_sam3.h>
58 #include <drv/adc.h>
59
60 #include <kern/proc.h>
61 #include <kern/monitor.h>
62
63 #include <net/http.h>
64
65 #include <netif/ethernetif.h>
66
67 #include <lwip/ip.h>
68 #include <lwip/ip_addr.h>
69 #include <lwip/netif.h>
70 #include <lwip/tcpip.h>
71 #include <lwip/dhcp.h>
72
73 #include <fs/fat.h>
74
75 #include <icons/bertos.h>
76
77 #include <stdio.h>
78 #include <string.h>
79
80 /* Network interface global variables */
81 static struct ip_addr ipaddr, netmask, gw;
82 static struct netif netif;
83
84 typedef struct BoardStatus
85 {
86         char local_ip[sizeof("123.123.123.123")];
87         char last_connected_ip[sizeof("123.123.123.123")];
88         uint16_t internal_temp;
89         ticks_t up_time;
90         size_t tot_req;
91 } BoardStatus;
92
93 static BoardStatus status;
94
95
96 static void init(void)
97 {
98         /* Enable all the interrupts */
99         IRQ_ENABLE;
100
101         /* Initialize debugging module (allow kprintf(), etc.) */
102         kdbg_init();
103         /* Initialize system timer */
104         timer_init();
105
106         /*
107          * Kernel initialization: processes (allow to create and dispatch
108          * processes using proc_new()).
109          */
110         proc_init();
111         sdram_init();
112
113         /* Initialize TCP/IP stack */
114         tcpip_init(NULL, NULL);
115
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);
120
121         dmac_init();
122
123         adc_init();
124         /* Enable the adc to read internal temperature sensor */
125         hw_enableTempRead();
126 }
127
128 static NORETURN void status_process(void)
129 {
130         while (1)
131         {
132                 status.internal_temp = hw_convertToDegree(adc_read(ADC_TEMPERATURE_CH));
133                 status.up_time++;
134                 timer_delay(1000);
135         }
136 }
137
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)
143
144
145 static uint8_t tx_buf[1024];
146
147 static int cgi_status(char *revc_buf, struct netconn *client)
148 {
149         (void)revc_buf;
150
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);
157
158         status.tot_req++;
159
160         http_sendOk(client);
161         netconn_write(client, tx_buf, strlen((char *)tx_buf), NETCONN_COPY);
162         return 0;
163 }
164
165 static int cgi_logo(char *revc_buf, struct netconn *client)
166 {
167         (void)revc_buf;
168
169         http_sendOk(client);
170         netconn_write(client, bertos_logo_jpg, bertos_logo_jpg_len, NETCONN_NOCOPY);
171         return 0;
172 }
173
174 static int cgi_temp(char *revc_buf, struct netconn *client)
175 {
176         (void)revc_buf;
177
178         sprintf((char *)tx_buf, "[ %d.%d ]", status.internal_temp / 10, status.internal_temp % 10);
179
180         http_sendOk(client);
181         netconn_write(client, tx_buf, strlen((char *)tx_buf), NETCONN_COPY);
182         return 0;
183 }
184
185 static int cgi_echo(char *revc_buf, struct netconn *client)
186 {
187         http_sendOk(client);
188         netconn_write(client, revc_buf, strlen((char *)revc_buf), NETCONN_COPY);
189         return 0;
190 }
191
192 static HttpCGI cgi_table[] =
193 {
194         { "echo",                cgi_echo   },
195         { "temp",                cgi_temp   },
196         { "status",              cgi_status },
197         { "bertos_logo_jpg.jpg", cgi_logo   },
198         { NULL, NULL }
199 };
200
201
202 int main(void)
203 {
204         struct netconn *server;
205
206
207         /* Hardware initialization */
208         init();
209
210         proc_new(status_process, NULL, KERN_MINSTACKSIZE * 2, NULL);
211
212         dhcp_start(&netif);
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));
216
217         server = netconn_new(NETCONN_TCP);
218         netconn_bind(server, IP_ADDR_ANY, 80);
219         netconn_listen(server);
220
221         while (1)
222         {
223                 http_server(server, cgi_table);
224         }
225 }