From e697399356fc668ad2ce560dd6e0217b2efd6f27 Mon Sep 17 00:00:00 2001 From: Karai Csaba Date: Sat, 21 May 2016 07:08:28 +0200 Subject: [PATCH] Nicer argument handling --- .../arduino/libraries/EspLink/WebServer.cpp | 51 ++++--------------- .../arduino/libraries/EspLink/WebServer.h | 4 -- .../examples/EspLinkWebApp/LedPage.ino | 4 -- .../examples/EspLinkWebApp/UserPage.ino | 1 - .../examples/EspLinkWebApp/VoltagePage.ino | 2 - web-server/web-server.c | 8 +-- 6 files changed, 13 insertions(+), 57 deletions(-) diff --git a/examples/arduino/libraries/EspLink/WebServer.cpp b/examples/arduino/libraries/EspLink/WebServer.cpp index 6faa822..eea3ec6 100644 --- a/examples/arduino/libraries/EspLink/WebServer.cpp +++ b/examples/arduino/libraries/EspLink/WebServer.cpp @@ -70,21 +70,20 @@ void WebServer::invokeMethod(RequestReason reason, WebMethod * method, CmdReques } } return; - default: + case WS_LOAD: + case WS_REFRESH: break; + default: + return; } - args_to_send = -1; + espLink.sendPacketStart(CMD_WEB_JSON_DATA, 100, 255); + espLink.sendPacketArg(4, remote_ip); + espLink.sendPacketArg(2, (uint8_t *)&remote_port); + method->callback( reason == WS_LOAD ? LOAD : REFRESH, NULL, 0); - if( args_to_send == -1 ) - { - espLink.sendPacketStart(CMD_WEB_JSON_DATA, 100, 2); - espLink.sendPacketArg(4, remote_ip); - espLink.sendPacketArg(2, (uint8_t *)&remote_port); - } - while( args_to_send-- > 0 ) - espLink.sendPacketArg(0, NULL); + espLink.sendPacketArg(0, NULL); espLink.sendPacketEnd(); } @@ -130,18 +129,8 @@ void WebServer::handleRequest(CmdRequest *req) espLink.sendPacketEnd(); } -void WebServer::setArgNum(uint8_t num) -{ - espLink.sendPacketStart(CMD_WEB_JSON_DATA, 100, 2 + (args_to_send = num)); - espLink.sendPacketArg(4, remote_ip); - espLink.sendPacketArg(2, (uint8_t *)&remote_port); -} - void WebServer::setArgString(const char * name, const char * value) { - if( args_to_send <= 0 ) - return; - uint8_t nlen = strlen(name); uint8_t vlen = strlen(value); char buf[nlen + vlen + 3]; @@ -149,15 +138,10 @@ void WebServer::setArgString(const char * name, const char * value) strcpy(buf+1, name); strcpy(buf+2+nlen, value); espLink.sendPacketArg(nlen+vlen+2, (uint8_t *)buf); - - args_to_send--; } void WebServer::setArgStringP(const char * name, const char * value) { - if( args_to_send <= 0 ) - return; - uint8_t nlen = strlen(name); uint8_t vlen = strlen_P(value); char buf[nlen + vlen + 3]; @@ -165,30 +149,20 @@ void WebServer::setArgStringP(const char * name, const char * value) strcpy(buf+1, name); strcpy_P(buf+2+nlen, value); espLink.sendPacketArg(nlen+vlen+2, (uint8_t *)buf); - - args_to_send--; } void WebServer::setArgBoolean(const char * name, uint8_t value) { - if( args_to_send <= 0 ) - return; - uint8_t nlen = strlen(name); char buf[nlen + 4]; buf[0] = WEB_BOOLEAN; strcpy(buf+1, name); buf[2 + nlen] = value; espLink.sendPacketArg(nlen+3, (uint8_t *)buf); - - args_to_send--; } void WebServer::setArgJson(const char * name, const char * value) { - if( args_to_send <= 0 ) - return; - uint8_t nlen = strlen(name); uint8_t vlen = strlen(value); char buf[nlen + vlen + 3]; @@ -196,23 +170,16 @@ void WebServer::setArgJson(const char * name, const char * value) strcpy(buf+1, name); strcpy(buf+2+nlen, value); espLink.sendPacketArg(nlen+vlen+2, (uint8_t *)buf); - - args_to_send--; } void WebServer::setArgInt(const char * name, int32_t value) { - if( args_to_send <= 0 ) - return; - uint8_t nlen = strlen(name); char buf[nlen + 7]; buf[0] = WEB_INTEGER; strcpy(buf+1, name); memcpy(buf+2+nlen, &value, 4); espLink.sendPacketArg(nlen+6, (uint8_t *)buf); - - args_to_send--; } int32_t WebServer::getArgInt() diff --git a/examples/arduino/libraries/EspLink/WebServer.h b/examples/arduino/libraries/EspLink/WebServer.h index 0a3606d..a589f3d 100644 --- a/examples/arduino/libraries/EspLink/WebServer.h +++ b/examples/arduino/libraries/EspLink/WebServer.h @@ -52,8 +52,6 @@ class WebServer uint8_t remote_ip[4]; uint16_t remote_port; - int16_t args_to_send; - char * value_ptr; uint32_t last_connect_ts; @@ -73,7 +71,6 @@ class WebServer uint8_t * getRemoteIp() { return remote_ip; } uint16_t getRemotePort() { return remote_port; } - void setArgNum(uint8_t num); void setArgInt(const char * name, int32_t value); void setArgJson(const char * name, const char * value); void setArgString(const char * name, const char * value); @@ -86,4 +83,3 @@ class WebServer }; #endif /* WEB_SERVER_H */ - diff --git a/examples/arduino/libraries/EspLink/examples/EspLinkWebApp/LedPage.ino b/examples/arduino/libraries/EspLink/examples/EspLinkWebApp/LedPage.ino index 768520b..494df83 100644 --- a/examples/arduino/libraries/EspLink/examples/EspLinkWebApp/LedPage.ino +++ b/examples/arduino/libraries/EspLink/examples/EspLinkWebApp/LedPage.ino @@ -154,7 +154,6 @@ void ledHtmlCallback(WebServerCommand command, char * data, int dataLen) } break; case LOAD: - webServer.setArgNum(4); webServer.setArgInt("frequency", frequency); switch(pattern) @@ -171,9 +170,6 @@ void ledHtmlCallback(WebServerCommand command, char * data, int dataLen) } case REFRESH: { - if( command == REFRESH ) - webServer.setArgNum(2); - if( blinking ) webServer.setArgStringP("text", PSTR("LED is blinking")); else diff --git a/examples/arduino/libraries/EspLink/examples/EspLinkWebApp/UserPage.ino b/examples/arduino/libraries/EspLink/examples/EspLinkWebApp/UserPage.ino index 0d7dfa4..e6581a5 100644 --- a/examples/arduino/libraries/EspLink/examples/EspLinkWebApp/UserPage.ino +++ b/examples/arduino/libraries/EspLink/examples/EspLinkWebApp/UserPage.ino @@ -67,7 +67,6 @@ void userHtmlCallback(WebServerCommand command, char * data, int dataLen) case LOAD: { char buf[MAX_STR_LEN]; - webServer.setArgNum(5); userReadStr( buf, POS_FIRST_NAME ); webServer.setArgString("first_name", buf); userReadStr( buf, POS_LAST_NAME ); diff --git a/examples/arduino/libraries/EspLink/examples/EspLinkWebApp/VoltagePage.ino b/examples/arduino/libraries/EspLink/examples/EspLinkWebApp/VoltagePage.ino index 68f9aa3..4080f75 100644 --- a/examples/arduino/libraries/EspLink/examples/EspLinkWebApp/VoltagePage.ino +++ b/examples/arduino/libraries/EspLink/examples/EspLinkWebApp/VoltagePage.ino @@ -87,8 +87,6 @@ void voltageHtmlCallback(WebServerCommand command, char * data, int dataLen) case LOAD: case REFRESH: { - webServer.setArgNum(2); - char buf[20]; uint8_t int_part = measured_voltage / 256; uint8_t float_part = ((measured_voltage & 255) * 100) / 256; diff --git a/web-server/web-server.c b/web-server/web-server.c index 85bef1f..2d536b4 100644 --- a/web-server/web-server.c +++ b/web-server/web-server.c @@ -263,9 +263,6 @@ int ICACHE_FLASH_ATTR WEB_CgiJsonHook(HttpdConnData *connData) int c = 2; while( c++ < cmdGetArgc(req) ) { - if( c > 3 ) // skip the first argument - jsonBuf[jsonPtr++] = ','; - int len = cmdArgLen(req); char buf[len+1]; buf[len] = 0; @@ -273,7 +270,10 @@ int ICACHE_FLASH_ATTR WEB_CgiJsonHook(HttpdConnData *connData) cmdPopArg(req, buf, len); if(len == 0) - continue; + break; // last argument + + if( c > 3 ) // skip the first argument + jsonBuf[jsonPtr++] = ','; if( jsonPtr + 20 + len > sizeof(jsonBuf) ) {