X-Git-Url: https://codewiz.org/gitweb?a=blobdiff_plain;f=boards%2Fsam3x-ek%2Fexamples%2Fsam3x-ek_http_server%2Fmain.c;h=ddb361de6189c7b0843809c73f249293584e8251;hb=5726374e3dc5a370ae5ab43c400e43b6ed954edd;hp=9a1282330b4a8c05703a20c5ad53d6d4a11218dc;hpb=27f2847bef705b09dbe026ffce96123b51593077;p=bertos.git diff --git a/boards/sam3x-ek/examples/sam3x-ek_http_server/main.c b/boards/sam3x-ek/examples/sam3x-ek_http_server/main.c index 9a128233..ddb361de 100644 --- a/boards/sam3x-ek/examples/sam3x-ek_http_server/main.c +++ b/boards/sam3x-ek/examples/sam3x-ek_http_server/main.c @@ -26,19 +26,31 @@ * 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 + * \author Daniele Basile * - * \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_http.h" #include "hw/hw_sd.h" +#include "hw/hw_adc.h" +#include "hw/hw_sdram.h" +// Define logging setting (for cfg/log.h module). +#define LOG_LEVEL 3 +#define LOG_VERBOSITY 0 +#include #include #include @@ -48,10 +60,13 @@ #include #include #include +#include #include #include +#include + #include #include @@ -62,17 +77,26 @@ #include +#include + +#include #include /* Network interface global variables */ static struct ip_addr ipaddr, netmask, gw; static struct netif netif; +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; -// SD fat filesystem context -static Sd sd; -static FATFS fs; -static FatFile in_file; static void init(void) { @@ -89,6 +113,7 @@ static void init(void) * processes using proc_new()). */ proc_init(); + sdram_init(); /* Initialize TCP/IP stack */ tcpip_init(NULL, NULL); @@ -99,76 +124,203 @@ 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); } } -static void get_fileName(char *revc_buf, char *name, size_t len) +#define IP_ADDR_TO_INT_TUPLE(addr) \ + (int)((addr) >> 0 & 0xff), \ + (int)((addr) >> 8 & 0xff), \ + (int)((addr) >> 16 & 0xff), \ + (int)((addr) >> 24 & 0xff) + + +static uint8_t tx_buf[2048]; + +static int cgi_status(struct netconn *client, const char *name, char *revc_buf, size_t revc_len) +{ + (void)revc_buf; + (void)revc_len; + (void)name; + + //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)); + 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); + + status.tot_req++; + + http_sendOk(client); + netconn_write(client, tx_buf, strlen((char *)tx_buf), NETCONN_COPY); + return 0; +} + +static int cgi_logo(struct netconn *client, const char *name, char *revc_buf, size_t revc_len) +{ + (void)revc_buf; + (void)revc_len; + (void)name; + + http_sendOk(client); + netconn_write(client, bertos_logo_jpg, bertos_logo_jpg_len, NETCONN_NOCOPY); + return 0; +} + +static int cgi_temp(struct netconn *client, const char *name, char *revc_buf, size_t revc_len) +{ + (void)revc_buf; + (void)revc_len; + (void)name; + + sprintf((char *)tx_buf, "[ %d.%d ]", status.internal_temp / 10, status.internal_temp % 10); + + http_sendOk(client); + netconn_write(client, tx_buf, strlen((char *)tx_buf), NETCONN_COPY); + return 0; +} + +static int cgi_echo(struct netconn *client, const char *name, char *revc_buf, size_t revc_len) +{ + (void)name; + + http_sendOk(client); + netconn_write(client, revc_buf, revc_len, NETCONN_COPY); + return 0; +} + +static int http_htmPageLoad(struct netconn *client, const char *name, char *revc_buf, size_t revc_len) { - char *p = strstr(revc_buf, "GET"); - if (p) + (void)revc_buf; + (void)revc_len; + + if (SD_CARD_PRESENT()) { - p += sizeof("GET") + 1; - for (size_t i = 0; *p != ' '; i++,p++) + + // SD fat filesystem context + Sd sd; + FATFS fs; + FatFile in_file; + FRESULT result; + + bool sd_ok = sd_init(&sd, NULL, 0); + if (sd_ok) { - if (i > len) - break; - name[i] = *p; + 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, name, FA_OPEN_EXISTING | FA_READ); + + size_t count = 0; + if (result == FR_OK) + { + LOG_INFO("Opened file '%s' size %ld\n", 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", 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); } + + return 0; } -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"; +static int cgi_chipInfo(struct netconn *client, const char *name, char *revc_buf, size_t revc_len) +{ + (void)revc_buf; + (void)revc_len; + (void)name; -static const char http_file_not_found[] = "\ - \ - \ -404 Not Found

404 Not Found

\ -

The requested URL was not found on this server.


\ -
BeRTOS simple HTTP server
"; + sprintf((char *)tx_buf, "[ %s, %s, %s, %s, %s ]", + chipid_eproc_name(CHIPID_EPRCOC()), + chipid_archnames(CHIPID_ARCH()), + chipid_sramsize(CHIPID_SRAMSIZ()), + chipid_nvpsize(CHIPID_NVPSIZ()), + chipid_nvptype(CHIPID_NVTYP())); + http_sendOk(client); + netconn_write(client, tx_buf, strlen((char *)tx_buf), NETCONN_COPY); -static const char http_sd_not_present[] = " \ - \ - \ -BeRTOS simple HTTP Server \ -

BeRTOS simple HTTP Server

Simple Http server, the site's pages are stored on SD card, check it if is present.


\ -www.BeRTOS.org \ -"; + return 0; +} -static uint8_t tx_buf[2048]; +static int cgi_error(struct netconn *client, const char *name, char *revc_buf, size_t revc_len) +{ + (void)revc_buf; + (void)revc_len; + (void)name; + (void)client; -#define IP_ADDR_TO_INT_TUPLE(addr) \ - (int)((addr) >> 0 & 0xff), \ - (int)((addr) >> 8 & 0xff), \ - (int)((addr) >> 16 & 0xff), \ - (int)((addr) >> 24 & 0xff) + return -1; +} + +static HttpCGI cgi_table[] = +{ + { CGI_MATCH_NAME, "echo", cgi_echo }, + { CGI_MATCH_NAME, "temp", cgi_temp }, + { CGI_MATCH_NAME, "status", cgi_status }, + { CGI_MATCH_NAME, "chipinfo", cgi_chipInfo }, + { CGI_MATCH_NAME, "error_test", cgi_error }, + { CGI_MATCH_NAME, "bertos_logo_jpg", cgi_logo }, + { CGI_MATCH_NONE, NULL, NULL } +}; -static char file_name[80]; int main(void) { struct netconn *server; - FRESULT result; /* Hardware initialization */ init(); + http_init(http_htmPageLoad, cgi_table); - 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,96 +333,6 @@ 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++; - 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)); - - kprintf("%s\n", file_name); - if (strlen(file_name) == 0) - strcpy(file_name, "index.htm"); - - if (!strcmp("bertos_jpg.jpg", file_name)) - { - netconn_write(client, bertos_jpg, sizeof(bertos_jpg), NETCONN_NOCOPY); - } - else if (SD_CARD_PRESENT()) - { - bool sd_ok = sd_init(&sd, NULL, 0); - if (sd_ok) - { - kprintf("Mount FAT filesystem.\n"); - result = f_mount(0, &fs); - if (result != FR_OK) - { - kprintf("Mounting FAT volumes error[%d]\n", result); - sd_ok = false; - f_mount(0, NULL); - } - - 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; - if (result == FR_OK) - { - kprintf("Opened file '%s' size %ld\n", file_name, in_file.fat_file.fsize); - - netconn_write(client, http_html_hdr_200, sizeof(http_html_hdr_200) - 1, NETCONN_NOCOPY); - - 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); - - kprintf("Sent: %d\n", count); - } - else - { - kprintf("Unable to open file: '%s' error[%d]\n", file_name, result); - netconn_write(client, http_html_hdr_404, sizeof(http_html_hdr_404) - 1, NETCONN_NOCOPY); - netconn_write(client, http_file_not_found, sizeof(http_file_not_found) - 1, NETCONN_NOCOPY); - } - } - } - f_mount(0, NULL); - kprintf("Umount FAT filesystem.\n"); - } - else - { - netconn_write(client, http_html_hdr_404, sizeof(http_html_hdr_404) - 1, NETCONN_NOCOPY); - netconn_write(client, http_sd_not_present, sizeof(http_sd_not_present), NETCONN_NOCOPY); - } - } - netconn_close(client); - netbuf_delete(rx_buf_conn); - } - netconn_delete(client); + http_poll(server); } }