From: asterix Date: Wed, 28 Sep 2011 16:54:21 +0000 (+0000) Subject: Use the http module. X-Git-Url: https://codewiz.org/gitweb?a=commitdiff_plain;h=f3ce42cfd746ed096afcf3d1ee287dfddba3bdef;p=bertos.git Use the http module. git-svn-id: https://src.develer.com/svnoss/bertos/trunk@5118 38d2e660-2303-0410-9eaa-f027e97ec537 --- 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 78cf8d09..d8087c65 100644 --- a/boards/sam3x-ek/examples/sam3x-ek_http_server/main.c +++ b/boards/sam3x-ek/examples/sam3x-ek_http_server/main.c @@ -60,6 +60,8 @@ #include #include +#include + #include #include @@ -79,11 +81,6 @@ 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")]; @@ -95,6 +92,7 @@ typedef struct BoardStatus static BoardStatus status; + static void init(void) { /* Enable all the interrupts */ @@ -137,55 +135,55 @@ static NORETURN void status_process(void) } } -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[1024]; + +static int cgi_status(char *revc_buf, struct netconn *client) { - 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; - } - } -} + (void)revc_buf; -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"; + //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); -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
"; + status.tot_req++; + http_sendOk(client); + netconn_write(client, tx_buf, strlen((char *)tx_buf), NETCONN_COPY); + return 0; +} -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 \ -"; +static int cgi_logo(char *revc_buf, struct netconn *client) +{ + (void)revc_buf; -static uint8_t tx_buf[2048]; + http_sendOk(client); + netconn_write(client, bertos_logo_jpg, bertos_logo_jpg_len, NETCONN_NOCOPY); + return 0; +} -#define IP_ADDR_TO_INT_TUPLE(addr) \ - (int)((addr) >> 0 & 0xff), \ - (int)((addr) >> 8 & 0xff), \ - (int)((addr) >> 16 & 0xff), \ - (int)((addr) >> 24 & 0xff) + +static HttpCGI cgi_table[] = +{ + { "status", cgi_status }, + { "bertos_logo_jpg.jpg", cgi_logo }, + { NULL, NULL } +}; -static char file_name[80]; int main(void) { struct netconn *server; - FRESULT result; + /* Hardware initialization */ init(); @@ -203,104 +201,6 @@ int main(void) while (1) { - struct netconn *client; - struct netbuf *rx_buf_conn; - char *rx_buf; - u16_t len; - - client = netconn_accept(server); - if (!client) - continue; - - //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) - { - netbuf_data(rx_buf_conn, (void **)&rx_buf, &len); - 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_logo_jpg.jpg", file_name)) - { - 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()) - { - 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) - { - 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_server(server, cgi_table); } } diff --git a/boards/sam3x-ek/examples/sam3x-ek_http_server/sam3x-ek_http_server.mk b/boards/sam3x-ek/examples/sam3x-ek_http_server/sam3x-ek_http_server.mk index 2dcfd7b1..2169ad5b 100644 --- a/boards/sam3x-ek/examples/sam3x-ek_http_server/sam3x-ek_http_server.mk +++ b/boards/sam3x-ek/examples/sam3x-ek_http_server/sam3x-ek_http_server.mk @@ -44,8 +44,10 @@ sam3x-ek_http_server_WIZARD_CSRC = \ bertos/mware/hex.c \ bertos/mware/sprintf.c \ bertos/net/lwip.c \ + bertos/net/http.c \ bertos/struct/heap.c \ $(sam3x-ek_http_server_HW_PATH)/hw/hw_eth.c \ + $(sam3x-ek_http_server_HW_PATH)/hw/hw_http.c \ # # Files automatically generated by the wizard. DO NOT EDIT, USE sam3x-ek_http_server_USER_PCSRC INSTEAD!