From 491931b7d22be9da54971d2cf913fd3c514b3458 Mon Sep 17 00:00:00 2001 From: cskarai Date: Fri, 7 Oct 2016 06:54:37 +0200 Subject: [PATCH] Web-Server to use CMD_RESP_CB --- cmd/cmd.h | 4 ++-- cmd/handlers.c | 1 + web-server/web-server.c | 47 +++++++++++++++++++++++++++++++++-------- web-server/web-server.h | 1 + 4 files changed, 42 insertions(+), 11 deletions(-) diff --git a/cmd/cmd.h b/cmd/cmd.h index 2cd6cec..3b4e8ab 100644 --- a/cmd/cmd.h +++ b/cmd/cmd.h @@ -52,8 +52,8 @@ typedef enum { CMD_REST_REQUEST, // do REST request CMD_REST_SETHEADER, // define header - CMD_WEB_DATA = 30, // MCU pushes data using this command - CMD_WEB_REQ_CB, // esp-link WEB callback + CMD_WEB_SETUP = 30, // set-up WEB callback + CMD_WEB_DATA, // WEB data from MCU CMD_SOCKET_SETUP = 40, // set-up callbacks CMD_SOCKET_SEND, // send data over UDP socket diff --git a/cmd/handlers.c b/cmd/handlers.c index 3697490..c191091 100644 --- a/cmd/handlers.c +++ b/cmd/handlers.c @@ -55,6 +55,7 @@ const CmdList commands[] = { {CMD_REST_REQUEST, "REST_REQ", REST_Request}, {CMD_REST_SETHEADER, "REST_SETHDR", REST_SetHeader}, #endif + {CMD_WEB_SETUP, "WEB_SETUP", WEB_Setup}, {CMD_WEB_DATA, "WEB_DATA", WEB_Data}, #ifdef SOCKET {CMD_SOCKET_SETUP, "SOCKET_SETUP", SOCKET_Setup}, diff --git a/web-server/web-server.c b/web-server/web-server.c index 0d323a2..bc8ffed 100644 --- a/web-server/web-server.c +++ b/web-server/web-server.c @@ -13,8 +13,10 @@ // - handles JSON data coming from the browser // - handles SLIP messages coming from MCU -#define WEB_CB "webCb" -#define MAX_ARGUMENT_BUFFER_SIZE 1024 +#define MAX_ARGUMENT_BUFFER_SIZE 128 +#define HEADER_SIZE 32 + +uint32_t web_server_cb = 0; struct ArgumentBuffer { @@ -104,14 +106,14 @@ void ICACHE_FLASH_ATTR WEB_Init() } // initializes the argument buffer -static void WEB_argInit(struct ArgumentBuffer * argBuffer) +static void ICACHE_FLASH_ATTR WEB_argInit(struct ArgumentBuffer * argBuffer) { argBuffer->numberOfArgs = 0; argBuffer->argBufferPtr = 0; } // adds an argument to the argument buffer (returns 0 if successful) -static int WEB_addArg(struct ArgumentBuffer * argBuffer, char * arg, int argLen ) +static int ICACHE_FLASH_ATTR WEB_addArg(struct ArgumentBuffer * argBuffer, char * arg, int argLen ) { if( argBuffer->argBufferPtr + argLen + sizeof(int) >= MAX_ARGUMENT_BUFFER_SIZE ) return -1; // buffer overflow @@ -129,9 +131,9 @@ static int WEB_addArg(struct ArgumentBuffer * argBuffer, char * arg, int argLen } // creates and sends a SLIP message from the argument buffer -static void WEB_sendArgBuffer(struct ArgumentBuffer * argBuffer, HttpdConnData *connData, int id, RequestReason reason) +static void ICACHE_FLASH_ATTR WEB_sendArgBuffer(struct ArgumentBuffer * argBuffer, HttpdConnData *connData, RequestReason reason) { - cmdResponseStart(CMD_WEB_REQ_CB, id, 4 + argBuffer->numberOfArgs); + cmdResponseStart(CMD_RESP_CB, web_server_cb, 4 + argBuffer->numberOfArgs); uint16_t r = (uint16_t)reason; cmdResponseBody(&r, sizeof(uint16_t)); // 1st argument: reason cmdResponseBody(&connData->conn->proto.tcp->remote_ip, 4); // 2nd argument: IP @@ -161,8 +163,8 @@ static int ICACHE_FLASH_ATTR WEB_handleJSONRequest(HttpdConnData *connData) errorResponse(connData, 400, "Slip processing is disabled!"); return HTTPD_CGI_DONE; } - CmdCallback* cb = cmdGetCbByName( WEB_CB ); - if( cb == NULL ) + + if( web_server_cb == 0 ) { errorResponse(connData, 500, "No MCU callback is registered!"); return HTTPD_CGI_DONE; @@ -226,6 +228,8 @@ static int ICACHE_FLASH_ATTR WEB_handleJSONRequest(HttpdConnData *connData) } int bptr = 0; + int sent_args = 0; + int max_buf_size = MAX_ARGUMENT_BUFFER_SIZE - HEADER_SIZE - os_strlen(connData->url); while( bptr < connData->post->len ) { @@ -269,11 +273,23 @@ static int ICACHE_FLASH_ATTR WEB_handleJSONRequest(HttpdConnData *connData) os_strcpy( arg + argPtr, value ); argPtr += valLen; + if( sent_args != 0 ) + { + if( argBuffer.argBufferPtr + argPtr >= max_buf_size ) + { + WEB_addArg(&argBuffer, NULL, 0); // there's enough room in the buffer for termination block + WEB_sendArgBuffer(&argBuffer, connData, reason ); + WEB_argInit( &argBuffer ); + sent_args = 0; + } + } + if( WEB_addArg(&argBuffer, arg, argPtr) ) { errorResponse(connData, 400, "Post too large!"); return HTTPD_CGI_DONE; } + sent_args++; } } } @@ -292,7 +308,7 @@ static int ICACHE_FLASH_ATTR WEB_handleJSONRequest(HttpdConnData *connData) os_printf("Web callback to MCU: %s\n", reasonBuf); - WEB_sendArgBuffer(&argBuffer, connData, (uint32_t)cb->callback, reason ); + WEB_sendArgBuffer(&argBuffer, connData, reason ); if( reason == SUBMIT ) { @@ -432,6 +448,19 @@ int ICACHE_FLASH_ATTR WEB_CgiJsonHook(HttpdConnData *connData) return HTTPD_CGI_MORE; } +// configuring the callback +void ICACHE_FLASH_ATTR WEB_Setup(CmdPacket *cmd) +{ + CmdRequest req; + cmdRequest(&req, cmd); + + if (cmdGetArgc(&req) < 1) return; + + cmdPopArg(&req, &web_server_cb, 4); // pop the callback + + os_printf("Web-server connected, cb=0x%x\n", web_server_cb); +} + // this method is called when MCU transmits WEB_DATA command void ICACHE_FLASH_ATTR WEB_Data(CmdPacket *cmd) { diff --git a/web-server/web-server.h b/web-server/web-server.h index 0827229..c6609a6 100644 --- a/web-server/web-server.h +++ b/web-server/web-server.h @@ -31,6 +31,7 @@ void WEB_Init(); char * WEB_UserPages(); int WEB_CgiJsonHook(HttpdConnData *connData); +void WEB_Setup(CmdPacket *cmd); void WEB_Data(CmdPacket *cmd); #endif /* WEB_SERVER_H */