X-Git-Url: https://codewiz.org/gitweb?a=blobdiff_plain;f=boards%2Fsam3x-ek%2Fexamples%2Fsam3x-ek_http_server%2Fmain.c;h=fdf90de5092a44f42e44b871ce25a1f176ca2c96;hb=f1700213ce77cc08371c77ba3da1f6e8cf60e4f3;hp=ddb361de6189c7b0843809c73f249293584e8251;hpb=5726374e3dc5a370ae5ab43c400e43b6ed954edd;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 ddb361de..fdf90de5 100644 --- a/boards/sam3x-ek/examples/sam3x-ek_http_server/main.c +++ b/boards/sam3x-ek/examples/sam3x-ek_http_server/main.c @@ -37,8 +37,7 @@ * * 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. + * Quering from browser some cgi page, the server return a json dictionary. */ @@ -91,7 +90,7 @@ 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; + uint32_t up_time; size_t tot_req; } BoardStatus; @@ -140,6 +139,7 @@ static NORETURN void status_process(void) } } +/* Macro to unpack the ip addres from lwip format */ #define IP_ADDR_TO_INT_TUPLE(addr) \ (int)((addr) >> 0 & 0xff), \ (int)((addr) >> 8 & 0xff), \ @@ -149,6 +149,9 @@ static NORETURN void status_process(void) static uint8_t tx_buf[2048]; +/* + * Return a JSON string of board status. + */ static int cgi_status(struct netconn *client, const char *name, char *revc_buf, size_t revc_len) { (void)revc_buf; @@ -158,7 +161,7 @@ static int cgi_status(struct netconn *client, const char *name, char *revc_buf, //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, + 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); @@ -180,19 +183,77 @@ static int cgi_logo(struct netconn *client, const char *name, char *revc_buf, si return 0; } +/* + * Return the internal micro temperature string. + */ 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); + 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; } + +/* + * Return the board uptime. + */ +static int cgi_uptime(struct netconn *client, const char *name, char *revc_buf, size_t revc_len) +{ + (void)revc_buf; + (void)revc_len; + (void)name; + + + + uint32_t m = status.up_time / 60; + uint32_t h = m / 60; + uint32_t s = status.up_time - (m * 60) - (h * 3600); + + sprintf((char *)tx_buf, "[\"%ldh %ldm %lds\"]", h, m, s); + + http_sendOk(client); + netconn_write(client, tx_buf, strlen((char *)tx_buf), NETCONN_COPY); + return 0; +} + +/* + * Return the VR1 potentiometer voltage. + */ +static int cgi_resistor(struct netconn *client, const char *name, char *revc_buf, size_t revc_len) +{ + (void)revc_buf; + (void)revc_len; + (void)name; + + uint16_t volt = ADC_RANGECONV(adc_read(1), 0, 3300); + sprintf((char *)tx_buf, "[ \"%d.%dV\" ]", volt / 1000, volt % 1000); + + http_sendOk(client); + netconn_write(client, tx_buf, strlen((char *)tx_buf), NETCONN_COPY); + return 0; +} + +/* + * Reply to client the request string. + */ +static int cgi_led(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; +} + +/* + * Reply to client the request string. + */ static int cgi_echo(struct netconn *client, const char *name, char *revc_buf, size_t revc_len) { (void)name; @@ -202,6 +263,16 @@ static int cgi_echo(struct netconn *client, const char *name, char *revc_buf, si return 0; } + + +/* + * Default function that http server call every client request, if it doesn't match a cgi table. + * In this implementation all client request are associate to real file stored on FAT file + * sistem on SD card. If the file there is not on SD card the server reply to client the + * error File not found, and send an harcoded page. In the same way, the server reply + * error page if the SD card is not present. + * + */ static int http_htmPageLoad(struct netconn *client, const char *name, char *revc_buf, size_t revc_len) { (void)revc_buf; @@ -271,13 +342,18 @@ static int http_htmPageLoad(struct netconn *client, const char *name, char *revc return 0; } +/* + * Return to client a string that display the CHIP ID information. + * See datasheet for more detail. + */ 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; - sprintf((char *)tx_buf, "[ %s, %s, %s, %s, %s ]", + sprintf((char *)tx_buf, "{ \"core_name\":\"%s\", \"arch_name\":\"%s\", \"sram_size\":\"%s\",\ + \"flash_size\":\"%s\", \"mem_boot_type\":\"%s\" }", chipid_eproc_name(CHIPID_EPRCOC()), chipid_archnames(CHIPID_ARCH()), chipid_sramsize(CHIPID_SRAMSIZ()), @@ -290,23 +366,18 @@ static int cgi_chipInfo(struct netconn *client, const char *name, char *revc_buf return 0; } -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; - - return -1; -} - +/* + * Static cgi table where we associate callback to page. + */ 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_WORD, "echo", cgi_echo }, + { CGI_MATCH_NAME, "get_temperature", cgi_temp }, + { CGI_MATCH_NAME, "get_uptime", cgi_uptime }, + { CGI_MATCH_NAME, "get_resistor", cgi_resistor }, + { CGI_MATCH_NAME, "set_led", cgi_led }, + { CGI_MATCH_WORD, "status", cgi_status }, + { CGI_MATCH_NAME, "get_chipinfo", cgi_chipInfo }, { CGI_MATCH_NAME, "bertos_logo_jpg", cgi_logo }, { CGI_MATCH_NONE, NULL, NULL } };