--- /dev/null
+/**
+ * \file
+ * <!--
+ * This file is part of BeRTOS.
+ *
+ * Bertos is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
+ *
+ * As a special exception, you may use this file as part of a free software
+ * library without restriction. Specifically, if other files instantiate
+ * templates or use macros or inline functions from this file, or you compile
+ * this file and link it with other files to produce an executable, this
+ * file does not by itself cause the resulting executable to be covered by
+ * the GNU General Public License. This exception does not however
+ * invalidate any other reasons why the executable file might be covered by
+ * the GNU General Public License.
+ *
+ * Copyright 2011 Develer S.r.l. (http://www.develer.com/)
+ *
+ * -->
+ *
+ * \brief Configuration file for the HTTP module.
+ *
+ * \author Daniele Basile <asterix@develer.com>
+ */
+
+#ifndef CFG_HTTP_H
+#define CFG_HTTP_H
+
+/**
+ * Module logging level.
+ *
+ * $WIZ$ type = "enum"
+ * $WIZ$ value_list = "log_level"
+ */
+#define HTTP_LOG_LEVEL LOG_LVL_INFO
+
+/**
+ * Module logging format.
+ *
+ * $WIZ$ type = "enum"
+ * $WIZ$ value_list = "log_format"
+ */
+#define HTTP_LOG_FORMAT LOG_FMT_VERBOSE
+
+/**
+ * Default start page to load
+ *
+ * $WIZ$ type = "str"
+ */
+#define HTTP_DEFAULT_PAGE "index.htm"
+
+#endif /* CFG_HTTP_H */
--- /dev/null
+/**
+ * \file
+ * <!--
+ * This file is part of BeRTOS.
+ *
+ * Bertos is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
+ *
+ * As a special exception, you may use this file as part of a free software
+ * library without restriction. Specifically, if other files instantiate
+ * templates or use macros or inline functions from this file, or you compile
+ * this file and link it with other files to produce an executable, this
+ * file does not by itself cause the resulting executable to be covered by
+ * the GNU General Public License. This exception does not however
+ * invalidate any other reasons why the executable file might be covered by
+ * the GNU General Public License.
+ *
+ * Copyright 2011 Develer S.r.l. (http://www.develer.com/)
+ * All Rights Reserved.
+ * -->
+ *
+ * \brief Simple Http server error pages.
+ *
+ * \author Daniele Basile <asteix@develer.com>
+ */
+
+#include "hw_http.h"
+
+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_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>";
+
+const size_t http_file_not_found_len = sizeof(http_file_not_found);
+
+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_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> \
+";
+const size_t http_sd_not_present_len = sizeof(http_sd_not_present);
--- /dev/null
+/**
+ * \file
+ * <!--
+ * This file is part of BeRTOS.
+ *
+ * Bertos is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
+ *
+ * As a special exception, you may use this file as part of a free software
+ * library without restriction. Specifically, if other files instantiate
+ * templates or use macros or inline functions from this file, or you compile
+ * this file and link it with other files to produce an executable, this
+ * file does not by itself cause the resulting executable to be covered by
+ * the GNU General Public License. This exception does not however
+ * invalidate any other reasons why the executable file might be covered by
+ * the GNU General Public License.
+ *
+ * Copyright 2011 Develer S.r.l. (http://www.develer.com/)
+ * All Rights Reserved.
+ * -->
+ *
+ * \brief Simple Http server error pages.
+ *
+ * \author Daniele Basile <asteix@develer.com>
+ */
+
+#ifndef HW_HTTP_H
+#define HW_HTTP_H
+
+#include <cpu/types.h>
+
+extern const char http_file_not_found[];
+extern const size_t http_file_not_found_len;
+
+extern const char http_sd_not_present[];
+extern const size_t http_sd_not_present_len;
+
+#endif /* HW_HTTP_H */
--- /dev/null
+/**
+ * \file
+ * <!--
+ * This file is part of BeRTOS.
+ *
+ * Bertos is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
+ *
+ * As a special exception, you may use this file as part of a free software
+ * library without restriction. Specifically, if other files instantiate
+ * templates or use macros or inline functions from this file, or you compile
+ * this file and link it with other files to produce an executable, this
+ * file does not by itself cause the resulting executable to be covered by
+ * the GNU General Public License. This exception does not however
+ * invalidate any other reasons why the executable file might be covered by
+ * the GNU General Public License.
+ *
+ * Copyright 2011 Develer S.r.l. (http://www.develer.com/)
+ *
+ * -->
+ *
+ * \author Daniele Basile <asterix@develer.com>
+ *
+ * \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 "http.h"
+
+#include "hw/hw_sd.h"
+#include "hw/hw_http.h"
+
+#include "cfg/cfg_http.h"
+
+// Define logging setting (for cfg/log.h module).
+#define LOG_LEVEL HTTP_LOG_LEVEL
+#define LOG_VERBOSITY HTTP_LOG_FORMAT
+#include <cfg/log.h>
+
+#include <drv/sd.h>
+
+#include <fs/fat.h>
+
+#include <stdio.h>
+#include <string.h>
+
+
+static const char http_html_hdr_200[] = "HTTP/1.1 200 OK\r\nContent-type: text/html\r\n\r\n";
+static const char http_html_hdr_404[] = "HTTP/1.1 404 Not Found\r\nContent-type: text/html\r\n\r\n";
+
+void http_sendOk(struct netconn *client)
+{
+ netconn_write(client, http_html_hdr_200, sizeof(http_html_hdr_200) - 1, NETCONN_NOCOPY);
+}
+
+void http_sendFileNotFound(struct netconn *client)
+{
+ netconn_write(client, http_html_hdr_404, sizeof(http_html_hdr_404) - 1, NETCONN_NOCOPY);
+}
+
+
+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++)
+ {
+ if (i > len)
+ break;
+ name[i] = *p;
+ }
+ }
+}
+
+static http_gci_handler_t cgi_search(const char *name, HttpCGI *table)
+{
+ for (int i = 0; table[i].name; i++)
+ {
+ if (!strcmp(table[i].name, name))
+ return table[i].handler;
+ }
+ return NULL;
+}
+
+static uint8_t tx_buf[2048];
+static char file_name[80];
+
+void http_server(struct netconn *server, struct HttpCGI *table)
+{
+ // SD fat filesystem context
+ Sd sd;
+ FATFS fs;
+ FatFile in_file;
+ struct netconn *client;
+ struct netbuf *rx_buf_conn;
+ char *rx_buf;
+ u16_t len;
+ FRESULT result;
+
+ client = netconn_accept(server);
+ if (!client)
+ return;
+
+ rx_buf_conn = netconn_recv(client);
+ if (rx_buf_conn)
+ {
+ netbuf_data(rx_buf_conn, (void **)&rx_buf, &len);
+ if (rx_buf)
+ {
+ memset(file_name, 0, sizeof(file_name));
+ get_fileName(rx_buf, file_name, sizeof(file_name));
+
+ LOG_INFO("Search %s\n", file_name);
+ if (strlen(file_name) == 0)
+ strcpy(file_name, HTTP_DEFAULT_PAGE);
+
+ http_gci_handler_t cgi = cgi_search(file_name, table);
+ if (cgi)
+ {
+ cgi(rx_buf, client);
+ }
+ else if (SD_CARD_PRESENT())
+ {
+ bool sd_ok = sd_init(&sd, NULL, 0);
+ if (sd_ok)
+ {
+ LOG_INFO("Mount FAT filesystem.\n");
+ result = f_mount(0, &fs);
+ if (result != FR_OK)
+ {
+ LOG_ERR("Mounting FAT volumes error[%d]\n", result);
+ sd_ok = false;
+ f_mount(0, NULL);
+ }
+
+ if (sd_ok)
+ {
+ result = fatfile_open(&in_file, file_name, FA_OPEN_EXISTING | FA_READ);
+
+ size_t count = 0;
+ if (result == FR_OK)
+ {
+ LOG_INFO("Opened file '%s' size %ld\n", file_name, in_file.fat_file.fsize);
+
+ http_sendOk(client);
+
+ while (count < in_file.fat_file.fsize)
+ {
+ int len = kfile_read(&in_file.fd, tx_buf, sizeof(tx_buf));
+ netconn_write(client, tx_buf, len, NETCONN_COPY);
+ count += len;
+ }
+
+ kfile_flush(&in_file.fd);
+ kfile_close(&in_file.fd);
+
+ LOG_INFO("Sent: %d\n", count);
+ }
+ else
+ {
+ LOG_ERR("Unable to open file: '%s' error[%d]\n", file_name, result);
+ http_sendFileNotFound(client);
+ netconn_write(client, http_file_not_found, http_file_not_found_len - 1, NETCONN_NOCOPY);
+ }
+ }
+ }
+ f_mount(0, NULL);
+ LOG_INFO("Umount FAT filesystem.\n");
+ }
+ else
+ {
+ http_sendFileNotFound(client);
+ netconn_write(client, http_sd_not_present, http_sd_not_present_len, NETCONN_NOCOPY);
+ }
+ }
+ netconn_close(client);
+ netbuf_delete(rx_buf_conn);
+ }
+ netconn_delete(client);
+}
--- /dev/null
+/**
+ * \file
+ * <!--
+ * This file is part of BeRTOS.
+ *
+ * Bertos is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
+ *
+ * As a special exception, you may use this file as part of a free software
+ * library without restriction. Specifically, if other files instantiate
+ * templates or use macros or inline functions from this file, or you compile
+ * this file and link it with other files to produce an executable, this
+ * file does not by itself cause the resulting executable to be covered by
+ * the GNU General Public License. This exception does not however
+ * invalidate any other reasons why the executable file might be covered by
+ * the GNU General Public License.
+ *
+ * Copyright 2011 Develer S.r.l. (http://www.develer.com/)
+ *
+ * -->
+ *
+ * \brief Simple HTTP server
+ *
+ * \author Daniele Basile <asterix@develer.com>
+ *
+ * $WIZ$ module_name = "http"
+ * $WIZ$ module_configuration = "bertos/cfg/cfg_http.h"
+ * $WIZ$ module_depends = "lwip", "kfile", "sd"
+ * $WIZ$ module_hw = "bertos/hw/hw_http.h", "bertos/hw/hw_http.c"
+ */
+
+#ifndef NET_HTTP_H
+#define NET_HTTP_H
+
+
+#include <netif/ethernetif.h>
+
+#include <lwip/ip.h>
+#include <lwip/ip_addr.h>
+#include <lwip/netif.h>
+#include <lwip/tcpip.h>
+#include <lwip/dhcp.h>
+
+typedef int (*http_gci_handler_t)(char *revc_buf, struct netconn *client);
+
+typedef struct HttpCGI
+{
+ const char *name;
+ http_gci_handler_t handler;
+} HttpCGI;
+
+void http_sendOk(struct netconn *client);
+void http_sendFileNotFound(struct netconn *client);
+void http_server(struct netconn *server, struct HttpCGI *gci);
+
+#endif /* NET_HTTP_H */
--- /dev/null
+/**
+ * \file
+ * <!--
+ * This file is part of BeRTOS.
+ *
+ * Bertos is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
+ *
+ * As a special exception, you may use this file as part of a free software
+ * library without restriction. Specifically, if other files instantiate
+ * templates or use macros or inline functions from this file, or you compile
+ * this file and link it with other files to produce an executable, this
+ * file does not by itself cause the resulting executable to be covered by
+ * the GNU General Public License. This exception does not however
+ * invalidate any other reasons why the executable file might be covered by
+ * the GNU General Public License.
+ *
+ * Copyright 2011 Develer S.r.l. (http://www.develer.com/)
+ *
+ * -->
+ *
+ * \brief Configuration file for the HTTP module.
+ *
+ * \author Daniele Basile <asterix@develer.com>
+ */
+
+#ifndef CFG_HTTP_H
+#define CFG_HTTP_H
+
+/**
+ * Module logging level.
+ *
+ * $WIZ$ type = "enum"
+ * $WIZ$ value_list = "log_level"
+ */
+#define HTTP_LOG_LEVEL LOG_LVL_INFO
+
+/**
+ * Module logging format.
+ *
+ * $WIZ$ type = "enum"
+ * $WIZ$ value_list = "log_format"
+ */
+#define HTTP_LOG_FORMAT LOG_FMT_VERBOSE
+
+/**
+ * Default start page to load
+ *
+ * $WIZ$ type = "str"
+ */
+#define HTTP_DEFAULT_PAGE "index.htm"
+
+#endif /* CFG_HTTP_H */
--- /dev/null
+/**
+ * \file
+ * <!--
+ * This file is part of BeRTOS.
+ *
+ * Bertos is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
+ *
+ * As a special exception, you may use this file as part of a free software
+ * library without restriction. Specifically, if other files instantiate
+ * templates or use macros or inline functions from this file, or you compile
+ * this file and link it with other files to produce an executable, this
+ * file does not by itself cause the resulting executable to be covered by
+ * the GNU General Public License. This exception does not however
+ * invalidate any other reasons why the executable file might be covered by
+ * the GNU General Public License.
+ *
+ * Copyright 2011 Develer S.r.l. (http://www.develer.com/)
+ * All Rights Reserved.
+ * -->
+ *
+ * \brief Simple Http server error pages.
+ *
+ * \author Daniele Basile <asteix@develer.com>
+ */
+
+#include "hw_http.h"
+
+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_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>";
+
+const size_t http_file_not_found_len = sizeof(http_file_not_found);
+
+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_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> \
+";
+const size_t http_sd_not_present_len = sizeof(http_sd_not_present);
--- /dev/null
+/**
+ * \file
+ * <!--
+ * This file is part of BeRTOS.
+ *
+ * Bertos is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
+ *
+ * As a special exception, you may use this file as part of a free software
+ * library without restriction. Specifically, if other files instantiate
+ * templates or use macros or inline functions from this file, or you compile
+ * this file and link it with other files to produce an executable, this
+ * file does not by itself cause the resulting executable to be covered by
+ * the GNU General Public License. This exception does not however
+ * invalidate any other reasons why the executable file might be covered by
+ * the GNU General Public License.
+ *
+ * Copyright 2011 Develer S.r.l. (http://www.develer.com/)
+ * All Rights Reserved.
+ * -->
+ *
+ * \brief Simple Http server error pages.
+ *
+ * \author Daniele Basile <asteix@develer.com>
+ */
+
+#ifndef HW_HTTP_H
+#define HW_HTTP_H
+
+#include <cpu/types.h>
+
+extern const char http_file_not_found[];
+extern const size_t http_file_not_found_len;
+
+extern const char http_sd_not_present[];
+extern const size_t http_sd_not_present_len;
+
+#endif /* HW_HTTP_H */