Merge pull request #196 from cskarai/cmd_resp_cb

Web-Server to use CMD_RESP_CB + form submit segmentation
pull/213/head
Thorsten von Eicken 8 years ago committed by GitHub
commit 9515ea538f
  1. 4
      cmd/cmd.h
  2. 1
      cmd/handlers.c
  3. 61
      web-server/web-server.c
  4. 1
      web-server/web-server.h

@ -52,8 +52,8 @@ typedef enum {
CMD_REST_REQUEST, // do REST request CMD_REST_REQUEST, // do REST request
CMD_REST_SETHEADER, // define header CMD_REST_SETHEADER, // define header
CMD_WEB_DATA = 30, // MCU pushes data using this command CMD_WEB_SETUP = 30, // set-up WEB callback
CMD_WEB_REQ_CB, // esp-link WEB callback CMD_WEB_DATA, // WEB data from MCU
CMD_SOCKET_SETUP = 40, // set-up callbacks CMD_SOCKET_SETUP = 40, // set-up callbacks
CMD_SOCKET_SEND, // send data over UDP socket CMD_SOCKET_SEND, // send data over UDP socket

@ -55,6 +55,7 @@ const CmdList commands[] = {
{CMD_REST_REQUEST, "REST_REQ", REST_Request}, {CMD_REST_REQUEST, "REST_REQ", REST_Request},
{CMD_REST_SETHEADER, "REST_SETHDR", REST_SetHeader}, {CMD_REST_SETHEADER, "REST_SETHDR", REST_SetHeader},
#endif #endif
{CMD_WEB_SETUP, "WEB_SETUP", WEB_Setup},
{CMD_WEB_DATA, "WEB_DATA", WEB_Data}, {CMD_WEB_DATA, "WEB_DATA", WEB_Data},
#ifdef SOCKET #ifdef SOCKET
{CMD_SOCKET_SETUP, "SOCKET_SETUP", SOCKET_Setup}, {CMD_SOCKET_SETUP, "SOCKET_SETUP", SOCKET_Setup},

@ -13,8 +13,10 @@
// - handles JSON data coming from the browser // - handles JSON data coming from the browser
// - handles SLIP messages coming from MCU // - handles SLIP messages coming from MCU
#define WEB_CB "webCb" #define MAX_ARGUMENT_BUFFER_SIZE 128
#define MAX_ARGUMENT_BUFFER_SIZE 1024 #define HEADER_SIZE 32
uint32_t web_server_cb = 0;
struct ArgumentBuffer struct ArgumentBuffer
{ {
@ -104,14 +106,14 @@ void ICACHE_FLASH_ATTR WEB_Init()
} }
// initializes the argument buffer // 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->numberOfArgs = 0;
argBuffer->argBufferPtr = 0; argBuffer->argBufferPtr = 0;
} }
// adds an argument to the argument buffer (returns 0 if successful) // 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 ) if( argBuffer->argBufferPtr + argLen + sizeof(int) >= MAX_ARGUMENT_BUFFER_SIZE )
return -1; // buffer overflow 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 // 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; uint16_t r = (uint16_t)reason;
cmdResponseBody(&r, sizeof(uint16_t)); // 1st argument: reason cmdResponseBody(&r, sizeof(uint16_t)); // 1st argument: reason
cmdResponseBody(&connData->conn->proto.tcp->remote_ip, 4); // 2nd argument: IP 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!"); errorResponse(connData, 400, "Slip processing is disabled!");
return HTTPD_CGI_DONE; 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!"); errorResponse(connData, 500, "No MCU callback is registered!");
return HTTPD_CGI_DONE; return HTTPD_CGI_DONE;
@ -226,6 +228,8 @@ static int ICACHE_FLASH_ATTR WEB_handleJSONRequest(HttpdConnData *connData)
} }
int bptr = 0; 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 ) while( bptr < connData->post->len )
{ {
@ -269,11 +273,23 @@ static int ICACHE_FLASH_ATTR WEB_handleJSONRequest(HttpdConnData *connData)
os_strcpy( arg + argPtr, value ); os_strcpy( arg + argPtr, value );
argPtr += valLen; 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) ) if( WEB_addArg(&argBuffer, arg, argPtr) )
{ {
errorResponse(connData, 400, "Post too large!"); errorResponse(connData, 400, "Post too large!");
return HTTPD_CGI_DONE; 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); 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 ) if( reason == SUBMIT )
{ {
@ -376,10 +392,16 @@ static int ICACHE_FLASH_ATTR WEB_handleMCUResponse(HttpdConnData *connData, CmdR
float f; float f;
os_memcpy( &f, value, 4); os_memcpy( &f, value, 4);
char intbuf[20]; // os_sprintf doesn't support %f
os_sprintf(intbuf, "%f", f); int intPart = f;
os_strcpy(jsonBuf + jsonPtr, intbuf); int fracPart = (f - intPart) * 1000; // use 3 digit precision
jsonPtr += os_strlen(intbuf); if( fracPart < 0 ) // for negative numbers
fracPart = -fracPart;
char floatBuf[20];
os_sprintf(floatBuf, "%d.%03d", intPart, fracPart);
os_strcpy(jsonBuf + jsonPtr, floatBuf);
jsonPtr += os_strlen(floatBuf);
} }
break; break;
case WEB_STRING: case WEB_STRING:
@ -432,6 +454,19 @@ int ICACHE_FLASH_ATTR WEB_CgiJsonHook(HttpdConnData *connData)
return HTTPD_CGI_MORE; 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 // this method is called when MCU transmits WEB_DATA command
void ICACHE_FLASH_ATTR WEB_Data(CmdPacket *cmd) void ICACHE_FLASH_ATTR WEB_Data(CmdPacket *cmd)
{ {

@ -31,6 +31,7 @@ void WEB_Init();
char * WEB_UserPages(); char * WEB_UserPages();
int WEB_CgiJsonHook(HttpdConnData *connData); int WEB_CgiJsonHook(HttpdConnData *connData);
void WEB_Setup(CmdPacket *cmd);
void WEB_Data(CmdPacket *cmd); void WEB_Data(CmdPacket *cmd);
#endif /* WEB_SERVER_H */ #endif /* WEB_SERVER_H */

Loading…
Cancel
Save