Nicer argument handling

pull/193/head
Karai Csaba 9 years ago committed by Thorsten von Eicken
parent 5d4f6c4c3c
commit e697399356
  1. 51
      examples/arduino/libraries/EspLink/WebServer.cpp
  2. 4
      examples/arduino/libraries/EspLink/WebServer.h
  3. 4
      examples/arduino/libraries/EspLink/examples/EspLinkWebApp/LedPage.ino
  4. 1
      examples/arduino/libraries/EspLink/examples/EspLinkWebApp/UserPage.ino
  5. 2
      examples/arduino/libraries/EspLink/examples/EspLinkWebApp/VoltagePage.ino
  6. 8
      web-server/web-server.c

@ -70,21 +70,20 @@ void WebServer::invokeMethod(RequestReason reason, WebMethod * method, CmdReques
} }
} }
return; return;
default: case WS_LOAD:
case WS_REFRESH:
break; 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); method->callback( reason == WS_LOAD ? LOAD : REFRESH, NULL, 0);
if( args_to_send == -1 ) espLink.sendPacketArg(0, NULL);
{
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.sendPacketEnd(); espLink.sendPacketEnd();
} }
@ -130,18 +129,8 @@ void WebServer::handleRequest(CmdRequest *req)
espLink.sendPacketEnd(); 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) void WebServer::setArgString(const char * name, const char * value)
{ {
if( args_to_send <= 0 )
return;
uint8_t nlen = strlen(name); uint8_t nlen = strlen(name);
uint8_t vlen = strlen(value); uint8_t vlen = strlen(value);
char buf[nlen + vlen + 3]; char buf[nlen + vlen + 3];
@ -149,15 +138,10 @@ void WebServer::setArgString(const char * name, const char * value)
strcpy(buf+1, name); strcpy(buf+1, name);
strcpy(buf+2+nlen, value); strcpy(buf+2+nlen, value);
espLink.sendPacketArg(nlen+vlen+2, (uint8_t *)buf); espLink.sendPacketArg(nlen+vlen+2, (uint8_t *)buf);
args_to_send--;
} }
void WebServer::setArgStringP(const char * name, const char * value) void WebServer::setArgStringP(const char * name, const char * value)
{ {
if( args_to_send <= 0 )
return;
uint8_t nlen = strlen(name); uint8_t nlen = strlen(name);
uint8_t vlen = strlen_P(value); uint8_t vlen = strlen_P(value);
char buf[nlen + vlen + 3]; char buf[nlen + vlen + 3];
@ -165,30 +149,20 @@ void WebServer::setArgStringP(const char * name, const char * value)
strcpy(buf+1, name); strcpy(buf+1, name);
strcpy_P(buf+2+nlen, value); strcpy_P(buf+2+nlen, value);
espLink.sendPacketArg(nlen+vlen+2, (uint8_t *)buf); espLink.sendPacketArg(nlen+vlen+2, (uint8_t *)buf);
args_to_send--;
} }
void WebServer::setArgBoolean(const char * name, uint8_t value) void WebServer::setArgBoolean(const char * name, uint8_t value)
{ {
if( args_to_send <= 0 )
return;
uint8_t nlen = strlen(name); uint8_t nlen = strlen(name);
char buf[nlen + 4]; char buf[nlen + 4];
buf[0] = WEB_BOOLEAN; buf[0] = WEB_BOOLEAN;
strcpy(buf+1, name); strcpy(buf+1, name);
buf[2 + nlen] = value; buf[2 + nlen] = value;
espLink.sendPacketArg(nlen+3, (uint8_t *)buf); espLink.sendPacketArg(nlen+3, (uint8_t *)buf);
args_to_send--;
} }
void WebServer::setArgJson(const char * name, const char * value) void WebServer::setArgJson(const char * name, const char * value)
{ {
if( args_to_send <= 0 )
return;
uint8_t nlen = strlen(name); uint8_t nlen = strlen(name);
uint8_t vlen = strlen(value); uint8_t vlen = strlen(value);
char buf[nlen + vlen + 3]; char buf[nlen + vlen + 3];
@ -196,23 +170,16 @@ void WebServer::setArgJson(const char * name, const char * value)
strcpy(buf+1, name); strcpy(buf+1, name);
strcpy(buf+2+nlen, value); strcpy(buf+2+nlen, value);
espLink.sendPacketArg(nlen+vlen+2, (uint8_t *)buf); espLink.sendPacketArg(nlen+vlen+2, (uint8_t *)buf);
args_to_send--;
} }
void WebServer::setArgInt(const char * name, int32_t value) void WebServer::setArgInt(const char * name, int32_t value)
{ {
if( args_to_send <= 0 )
return;
uint8_t nlen = strlen(name); uint8_t nlen = strlen(name);
char buf[nlen + 7]; char buf[nlen + 7];
buf[0] = WEB_INTEGER; buf[0] = WEB_INTEGER;
strcpy(buf+1, name); strcpy(buf+1, name);
memcpy(buf+2+nlen, &value, 4); memcpy(buf+2+nlen, &value, 4);
espLink.sendPacketArg(nlen+6, (uint8_t *)buf); espLink.sendPacketArg(nlen+6, (uint8_t *)buf);
args_to_send--;
} }
int32_t WebServer::getArgInt() int32_t WebServer::getArgInt()

@ -52,8 +52,6 @@ class WebServer
uint8_t remote_ip[4]; uint8_t remote_ip[4];
uint16_t remote_port; uint16_t remote_port;
int16_t args_to_send;
char * value_ptr; char * value_ptr;
uint32_t last_connect_ts; uint32_t last_connect_ts;
@ -73,7 +71,6 @@ class WebServer
uint8_t * getRemoteIp() { return remote_ip; } uint8_t * getRemoteIp() { return remote_ip; }
uint16_t getRemotePort() { return remote_port; } uint16_t getRemotePort() { return remote_port; }
void setArgNum(uint8_t num);
void setArgInt(const char * name, int32_t value); void setArgInt(const char * name, int32_t value);
void setArgJson(const char * name, const char * value); void setArgJson(const char * name, const char * value);
void setArgString(const char * name, const char * value); void setArgString(const char * name, const char * value);
@ -86,4 +83,3 @@ class WebServer
}; };
#endif /* WEB_SERVER_H */ #endif /* WEB_SERVER_H */

@ -154,7 +154,6 @@ void ledHtmlCallback(WebServerCommand command, char * data, int dataLen)
} }
break; break;
case LOAD: case LOAD:
webServer.setArgNum(4);
webServer.setArgInt("frequency", frequency); webServer.setArgInt("frequency", frequency);
switch(pattern) switch(pattern)
@ -171,9 +170,6 @@ void ledHtmlCallback(WebServerCommand command, char * data, int dataLen)
} }
case REFRESH: case REFRESH:
{ {
if( command == REFRESH )
webServer.setArgNum(2);
if( blinking ) if( blinking )
webServer.setArgStringP("text", PSTR("LED is blinking")); webServer.setArgStringP("text", PSTR("LED is blinking"));
else else

@ -67,7 +67,6 @@ void userHtmlCallback(WebServerCommand command, char * data, int dataLen)
case LOAD: case LOAD:
{ {
char buf[MAX_STR_LEN]; char buf[MAX_STR_LEN];
webServer.setArgNum(5);
userReadStr( buf, POS_FIRST_NAME ); userReadStr( buf, POS_FIRST_NAME );
webServer.setArgString("first_name", buf); webServer.setArgString("first_name", buf);
userReadStr( buf, POS_LAST_NAME ); userReadStr( buf, POS_LAST_NAME );

@ -87,8 +87,6 @@ void voltageHtmlCallback(WebServerCommand command, char * data, int dataLen)
case LOAD: case LOAD:
case REFRESH: case REFRESH:
{ {
webServer.setArgNum(2);
char buf[20]; char buf[20];
uint8_t int_part = measured_voltage / 256; uint8_t int_part = measured_voltage / 256;
uint8_t float_part = ((measured_voltage & 255) * 100) / 256; uint8_t float_part = ((measured_voltage & 255) * 100) / 256;

@ -263,9 +263,6 @@ int ICACHE_FLASH_ATTR WEB_CgiJsonHook(HttpdConnData *connData)
int c = 2; int c = 2;
while( c++ < cmdGetArgc(req) ) while( c++ < cmdGetArgc(req) )
{ {
if( c > 3 ) // skip the first argument
jsonBuf[jsonPtr++] = ',';
int len = cmdArgLen(req); int len = cmdArgLen(req);
char buf[len+1]; char buf[len+1];
buf[len] = 0; buf[len] = 0;
@ -273,7 +270,10 @@ int ICACHE_FLASH_ATTR WEB_CgiJsonHook(HttpdConnData *connData)
cmdPopArg(req, buf, len); cmdPopArg(req, buf, len);
if(len == 0) if(len == 0)
continue; break; // last argument
if( c > 3 ) // skip the first argument
jsonBuf[jsonPtr++] = ',';
if( jsonPtr + 20 + len > sizeof(jsonBuf) ) if( jsonPtr + 20 + len > sizeof(jsonBuf) )
{ {

Loading…
Cancel
Save