From 5acb892fda4e0c95b806231a9fa77d19d4c1d97a Mon Sep 17 00:00:00 2001 From: asterix Date: Mon, 3 Oct 2011 12:59:15 +0000 Subject: [PATCH] Add some cgi functions. git-svn-id: https://src.develer.com/svnoss/bertos/trunk@5132 38d2e660-2303-0410-9eaa-f027e97ec537 --- .../examples/sam3x-ek_http_server/main.c | 79 +++++++++++++++---- 1 file changed, 62 insertions(+), 17 deletions(-) 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 c3a1a4b1..58feea63 100644 --- a/boards/sam3x-ek/examples/sam3x-ek_http_server/main.c +++ b/boards/sam3x-ek/examples/sam3x-ek_http_server/main.c @@ -90,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; @@ -192,13 +192,65 @@ static int cgi_temp(struct netconn *client, const char *name, char *revc_buf, si (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. */ @@ -212,6 +264,7 @@ static int cgi_echo(struct netconn *client, const char *name, char *revc_buf, si } + /* * 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 @@ -312,26 +365,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_WORD, "chipinfo", cgi_chipInfo }, { CGI_MATCH_NAME, "bertos_logo_jpg", cgi_logo }, { CGI_MATCH_NONE, NULL, NULL } }; -- 2.25.1