Fix flash start address.
[bertos.git] / boards / sam3x-ek / examples / sam3x-ek_http_server / main.c
index 9a1282330b4a8c05703a20c5ad53d6d4a11218dc..78cf8d09636ad24fe1eae571ad0418c963c5c949 100644 (file)
  * invalidate any other reasons why the executable file might be covered by
  * the GNU General Public License.
  *
- * Copyright 2010,2011 Develer S.r.l. (http://www.develer.com/)
+ * Copyright 2011 Develer S.r.l. (http://www.develer.com/)
  *
  * -->
  *
  * \author Andrea Righi <arighi@develer.com>
+ * \author Daniele Basile <asterix@develer.com>
  *
- * \brief lwIP TCP/IP echo server listening on port 80.
+ * \brief Simple Http server.
+ *
+ * This simple web server read the site's pages from SD card, and manage
+ * the cases where SD is not present or page not found, using embedded pages.
+ * Quering from browser the /status page, the server return a json dictionary where are store
+ * some board status info, like board temperature, up-time, etc.
  */
 
-#include "bertos.c"
 
 #include "hw/hw_sd.h"
+#include "hw/hw_adc.h"
+#include "hw/hw_sdram.h"
 
 #include <cfg/debug.h>
 
@@ -48,6 +55,7 @@
 #include <drv/ser.h>
 #include <drv/sd.h>
 #include <drv/dmac_sam3.h>
+#include <drv/adc.h>
 
 #include <kern/proc.h>
 #include <kern/monitor.h>
 
 #include <fs/fat.h>
 
+#include <icons/bertos.h>
+
+#include <stdio.h>
 #include <string.h>
 
 /* Network interface global variables */
 static struct ip_addr ipaddr, netmask, gw;
 static struct netif netif;
 
-
 // SD fat filesystem context
 static Sd sd;
 static FATFS fs;
 static FatFile in_file;
 
+typedef struct BoardStatus
+{
+       char local_ip[sizeof("123.123.123.123")];
+       char last_connected_ip[sizeof("123.123.123.123")];
+       uint16_t internal_temp;
+       ticks_t up_time;
+       size_t tot_req;
+} BoardStatus;
+
+static BoardStatus status;
+
 static void init(void)
 {
        /* Enable all the interrupts */
@@ -89,6 +110,7 @@ static void init(void)
         * processes using proc_new()).
         */
        proc_init();
+       sdram_init();
 
        /* Initialize TCP/IP stack */
        tcpip_init(NULL, NULL);
@@ -99,20 +121,19 @@ static void init(void)
        netif_set_up(&netif);
 
        dmac_init();
-}
 
-static int tot_req;
+       adc_init();
+       /* Enable the adc to read internal temperature sensor */
+       hw_enableTempRead();
+}
 
-static NORETURN void monitor_process(void)
+static NORETURN void status_process(void)
 {
-       int start = tot_req;
-
        while (1)
        {
-               //monitor_report();
-               kprintf("tot_req=%d [%d reqs/s]\n", tot_req, tot_req - start);
-               start = tot_req;
-               timer_delay(10000);
+               status.internal_temp = hw_convertToDegree(adc_read(ADC_TEMPERATURE_CH));
+               status.up_time++;
+               timer_delay(1000);
        }
 }
 
@@ -121,6 +142,7 @@ static void get_fileName(char *revc_buf, char *name, size_t len)
        char *p = strstr(revc_buf, "GET");
        if (p)
        {
+               //skip the "/" in get string request
                p += sizeof("GET") + 1;
                for (size_t i = 0; *p != ' '; i++,p++)
                {
@@ -137,7 +159,7 @@ static const char http_html_hdr_404[] = "HTTP/1.1 404 Not Found\r\nContent-type:
 static const char http_file_not_found[] = "\
 <!DOCTYPE html PUBLIC \"-//IETF//DTD HTML 2.0//EN\"> \
 <html><head><meta http-equiv=\"Content-Type\" content=\"text/html; charset=ISO-8859-1\"> \
-<title>404 Not Found</title></head><body><img src=\"bertos_jpg.jpg\"><h1>404 Not Found</h1>\
+<title>404 Not Found</title></head><body><img src=\"bertos_logo_jpg.jpg\"><h1>404 Not Found</h1>\
 <p>The requested URL was not found on this server.</p><hr>\
 <address>BeRTOS simple HTTP server</address></body></html>";
 
@@ -145,7 +167,7 @@ static const char http_file_not_found[] = "\
 static const char http_sd_not_present[] = " \
 <!DOCTYPE html PUBLIC \"-//IETF//DTD HTML 2.0//EN\">  \
 <html><head><meta http-equiv=\"Content-Type\" content=\"text/html; charset=ISO-8859-1\">  \
-<title>BeRTOS simple HTTP Server</title></head><body><img src=\"bertos_jpg.jpg\"> \
+<title>BeRTOS simple HTTP Server</title></head><body><img src=\"bertos_logo_jpg.jpg\"> \
 <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>\
 <a href=\"http://www.bertos.org\">www.BeRTOS.org</a></body></html> \
 ";
@@ -168,7 +190,7 @@ int main(void)
        /* Hardware initialization */
        init();
 
-       proc_new(monitor_process, NULL, KERN_MINSTACKSIZE * 2, NULL);
+       proc_new(status_process, NULL, KERN_MINSTACKSIZE * 2, NULL);
 
        dhcp_start(&netif);
        while (!netif.ip_addr.addr)
@@ -181,20 +203,20 @@ int main(void)
 
        while (1)
        {
-
                struct netconn *client;
                struct netbuf *rx_buf_conn;
                char *rx_buf;
                u16_t len;
 
                client = netconn_accept(server);
-               kprintf("remote ip = %d.%d.%d.%d\n", IP_ADDR_TO_INT_TUPLE(client->pcb.ip->remote_ip.addr));
-               kprintf("local ip = %d.%d.%d.%d\n", IP_ADDR_TO_INT_TUPLE(client->pcb.ip->local_ip.addr));
-
                if (!client)
                        continue;
 
-               tot_req++;
+               //Update board status.
+               sprintf(status.last_connected_ip, "%d.%d.%d.%d", IP_ADDR_TO_INT_TUPLE(client->pcb.ip->remote_ip.addr));
+               sprintf(status.local_ip, "%d.%d.%d.%d", IP_ADDR_TO_INT_TUPLE(client->pcb.ip->local_ip.addr));
+               status.tot_req++;
+
                rx_buf_conn = netconn_recv(client);
                if (rx_buf_conn)
                {
@@ -202,15 +224,25 @@ int main(void)
                        if (rx_buf)
                        {
                                memset(file_name, 0, sizeof(file_name));
+                               memset(tx_buf, 0, sizeof(tx_buf));
+
                                get_fileName(rx_buf, file_name, sizeof(file_name));
 
                                kprintf("%s\n", file_name);
                                if (strlen(file_name) == 0)
                                        strcpy(file_name, "index.htm");
 
-                               if (!strcmp("bertos_jpg.jpg", file_name))
+                               if (!strcmp("bertos_logo_jpg.jpg", file_name))
                                {
-                                       netconn_write(client, bertos_jpg, sizeof(bertos_jpg), NETCONN_NOCOPY);
+                                       netconn_write(client, bertos_logo_jpg, bertos_logo_jpg_len, NETCONN_NOCOPY);
+                               }
+                               else if (!strcmp("status", file_name))
+                               {
+                                       sprintf((char *)tx_buf, "[ %s, %s, %d.%d, %ld, %d ]", status.local_ip, status.last_connected_ip,
+                                                                                                                               status.internal_temp / 10, status.internal_temp % 10,
+                                                                                                                               status.up_time, status.tot_req);
+
+                                       netconn_write(client, tx_buf, strlen((char *)tx_buf), NETCONN_COPY);
                                }
                                else if (SD_CARD_PRESENT())
                                {
@@ -228,8 +260,6 @@ int main(void)
 
                                                if (sd_ok)
                                                {
-                                                       memset(tx_buf, 0, sizeof(tx_buf));
-
                                                        result = fatfile_open(&in_file, file_name,  FA_OPEN_EXISTING | FA_READ);
 
                                                        size_t count = 0;