diff --git a/src/httpserver/http_fns.c b/src/httpserver/http_fns.c index 29e954726..238dd30b7 100644 --- a/src/httpserver/http_fns.c +++ b/src/httpserver/http_fns.c @@ -1340,6 +1340,8 @@ int http_fn_cmd_tool(http_request_t* request) { commandResult_t res; const char *resStr; char tmpA[128]; + char *long_str_alloced = 0; + int commandLen; http_setup(request, httpMimeTypeHTML); http_html_start(request, "Command tool"); @@ -1348,11 +1350,23 @@ int http_fn_cmd_tool(http_request_t* request) { poststr(request, "Please consider using 'Web Application' console with more options and real time log view.
"); poststr(request, "Remember that some commands are added after a restart when a driver is activated...
"); - if (http_getArg(request->url, "cmd", tmpA, sizeof(tmpA))) { + commandLen = http_getArg(request->url, "cmd", tmpA, sizeof(tmpA)); + if (commandLen) { poststr(request, "
"); // all log printfs made by command will be sent also to request LOG_SetCommandHTTPRedirectReply(request); - res = CMD_ExecuteCommand(tmpA, COMMAND_FLAG_SOURCE_CONSOLE); + if (commandLen > (sizeof(tmpA) - 5)) { + commandLen += 8; + long_str_alloced = (char*)malloc(commandLen); + if (long_str_alloced) { + http_getArg(request->url, "cmd", long_str_alloced, commandLen); + CMD_ExecuteCommand(long_str_alloced, COMMAND_FLAG_SOURCE_CONSOLE); + free(long_str_alloced); + } + } + else { + CMD_ExecuteCommand(tmpA, COMMAND_FLAG_SOURCE_CONSOLE); + } LOG_SetCommandHTTPRedirectReply(0); resStr = CMD_GetResultString(res); hprintf255(request, "

%s

", resStr); @@ -2268,11 +2282,25 @@ int http_tasmota_json_status_generic(http_request_t* request) { } int http_fn_cm(http_request_t* request) { char tmpA[128]; + char *long_str_alloced = 0; + int commandLen; http_setup(request, httpMimeTypeJson); // exec command - if (http_getArg(request->url, "cmnd", tmpA, sizeof(tmpA))) { - CMD_ExecuteCommand(tmpA, COMMAND_FLAG_SOURCE_HTTP); + commandLen = http_getArg(request->url, "cmnd", tmpA, sizeof(tmpA)); + if (commandLen) { + if (commandLen > (sizeof(tmpA) - 5)) { + commandLen += 8; + long_str_alloced = (char*)malloc(commandLen); + if (long_str_alloced) { + http_getArg(request->url, "cmnd", long_str_alloced, commandLen); + CMD_ExecuteCommand(long_str_alloced, COMMAND_FLAG_SOURCE_HTTP); + free(long_str_alloced); + } + } + else { + CMD_ExecuteCommand(tmpA, COMMAND_FLAG_SOURCE_HTTP); + } if (!wal_strnicmp(tmpA, "POWER", 5)) { diff --git a/src/httpserver/new_http.c b/src/httpserver/new_http.c index 2040810b2..8b51313f7 100644 --- a/src/httpserver/new_http.c +++ b/src/httpserver/new_http.c @@ -219,9 +219,9 @@ void http_setup(http_request_t* request, const char* type) { poststr(request, "Accept-Ranges: none"); poststr(request, "\r\n"); poststr(request, "Transfer-Encoding: chunked"); +#endif poststr(request, "\r\n"); poststr(request, "Connection: close"); -#endif poststr(request, "\r\n"); // end headers with double CRLF poststr(request, "\r\n"); } @@ -277,11 +277,14 @@ const char* http_checkArg(const char* p, const char* n) { return p; } -void http_copyCarg(const char* atin, char* to, int maxSize) { +int http_copyCarg(const char* atin, char* to, int maxSize) { int a, b; + int realSize; const unsigned char* at = (unsigned char*)atin; - while (*at != 0 && *at != '&' && *at != ' ' && maxSize > 1) { + realSize = 0; + + while (*at != 0 && *at != '&' && *at != ' ') { #if 0 * to = *at; to++; @@ -303,20 +306,36 @@ void http_copyCarg(const char* atin, char* to, int maxSize) { b -= ('A' - 10); else b -= '0'; - *to++ = 16 * a + b; + // can we afford to place this char in the target? + if (maxSize > 1) { + maxSize--; + *to++ = 16 * a + b; + } + realSize++; at += 3; } else if (*at == '+') { - *to++ = ' '; + // can we afford to place this char in the target? + if (maxSize > 1) { + maxSize--; + *to++ = ' '; + } + realSize++; at++; } else { - *to++ = *at++; + // can we afford to place this char in the target? + if (maxSize > 1) { + maxSize--; + *to++ = *at; + } + realSize++; + at++; } - maxSize--; #endif } *to = 0; + return realSize; } int http_getArg(const char* base, const char* name, char* o, int maxSize) { @@ -331,8 +350,7 @@ int http_getArg(const char* base, const char* name, char* o, int maxSize) { const char* at = http_checkArg(base, name); if (at) { at++; - http_copyCarg(at, o, maxSize); - return 1; + return http_copyCarg(at, o, maxSize); } while (*base != '&') { if (*base == 0) { diff --git a/src/selftest/selftest_cmd_channels.c b/src/selftest/selftest_cmd_channels.c index 6f133af1c..104598c0c 100644 --- a/src/selftest/selftest_cmd_channels.c +++ b/src/selftest/selftest_cmd_channels.c @@ -64,6 +64,39 @@ void Test_Commands_Channels() { // this check will fail obviously! //SELFTEST_ASSERT_CHANNEL(5, 666); + + + const char *logCmd = "backlog%20setChannel%200%2012;%20setChannel%201%203;%20setChannel%202%204;%20setChannel%203%2045;%20setChannel%204%205;%20setChannel%205%206;%20setChannel%206%2078;%20setChannel%207%20999;%20setChannel%208%2010;%20setChannel%209%20234;%20setChannel%2010%20567;%20setChannel%2011%20-1;%20setChannel%2012%201212;%20setChannel%2013%20131313"; + + const char *logCmd2 = ";setChannel%2014%205678;%20setChannel%2015%2012345%20;%20setChannel%2016%207890;%20setChannel%2017%209898;%20setChannel%2018%208765%20;%20setChannel%2019%201191;%20setChannel%2020%204%20"; + + char tmp[8192]; + strcpy(tmp, "cm?cmnd="); + strcat(tmp, logCmd); + strcat(tmp, logCmd2); + Test_FakeHTTPClientPacket_GET(tmp); + + SELFTEST_ASSERT_CHANNEL(0, 12); + SELFTEST_ASSERT_CHANNEL(1, 3); + SELFTEST_ASSERT_CHANNEL(2, 4); + SELFTEST_ASSERT_CHANNEL(3, 45); + SELFTEST_ASSERT_CHANNEL(4, 5); + SELFTEST_ASSERT_CHANNEL(5, 6); + SELFTEST_ASSERT_CHANNEL(6, 78); + SELFTEST_ASSERT_CHANNEL(7, 999); + SELFTEST_ASSERT_CHANNEL(8, 10); + SELFTEST_ASSERT_CHANNEL(9, 234); + SELFTEST_ASSERT_CHANNEL(10, 567); + SELFTEST_ASSERT_CHANNEL(11, -1); + SELFTEST_ASSERT_CHANNEL(12, 1212); + SELFTEST_ASSERT_CHANNEL(13, 131313); + SELFTEST_ASSERT_CHANNEL(14, 5678); + SELFTEST_ASSERT_CHANNEL(15, 12345); + SELFTEST_ASSERT_CHANNEL(16, 7890); + SELFTEST_ASSERT_CHANNEL(17, 9898); + SELFTEST_ASSERT_CHANNEL(18, 8765); + SELFTEST_ASSERT_CHANNEL(19, 1191); + SELFTEST_ASSERT_CHANNEL(20, 4); } diff --git a/src/selftest/selftest_http.c b/src/selftest/selftest_http.c index e1567ecb6..fff29a2ad 100644 --- a/src/selftest/selftest_http.c +++ b/src/selftest/selftest_http.c @@ -91,6 +91,13 @@ void Test_FakeHTTPClientPacket_Generic() { } void Test_FakeHTTPClientPacket_GET(const char *tg) { + //char bufferTemp[8192]; + //va_list argList; + + //va_start(argList, tg); + //vsnprintf(bufferTemp, sizeof(bufferTemp), tg, argList); + //va_end(argList); + sprintf(buffer, http_get_template1, tg); Test_FakeHTTPClientPacket_Generic(); } @@ -101,6 +108,13 @@ void Test_FakeHTTPClientPacket_POST(const char *tg, const char *data) { Test_FakeHTTPClientPacket_Generic(); } void Test_FakeHTTPClientPacket_JSON(const char *tg) { + /*char bufferTemp[8192]; + va_list argList; + + va_start(argList, tg); + vsnprintf(bufferTemp, sizeof(bufferTemp), tg, argList); + va_end(argList); +*/ int r; Test_FakeHTTPClientPacket_GET(tg); diff --git a/src/selftest/selftest_local.h b/src/selftest/selftest_local.h index 7080d15b9..d654ff6ff 100644 --- a/src/selftest/selftest_local.h +++ b/src/selftest/selftest_local.h @@ -47,6 +47,7 @@ void Test_DeviceGroups(); void Test_FakeHTTPClientPacket_GET(const char *tg); void Test_FakeHTTPClientPacket_POST(const char *tg, const char *data); +void Test_FakeHTTPClientPacket_JSON(const char *tg); const char *Test_GetLastHTMLReply(); // TODO: move elsewhere? diff --git a/src/win_main.c b/src/win_main.c index daaa4cf41..bb331727a 100644 --- a/src/win_main.c +++ b/src/win_main.c @@ -11,6 +11,7 @@ #include #include "new_common.h" #include "driver\drv_public.h" +#include "cmnds\cmd_public.h" #include "httpserver\new_http.h" #include "new_pins.h" #include @@ -67,7 +68,7 @@ void Sim_RunFrame(int frameTime) { // this time counter is simulated, I need this for unit tests to work g_simulatedTimeNow += frameTime; accum_time += frameTime; - PIN_ticks(0); + QuickTick(0); HTTPServer_RunQuickTick(); if (accum_time > 1000) { accum_time -= 1000; @@ -108,6 +109,7 @@ void SIM_ClearOBK() { release_lfs(); SIM_Hack_ClearSimulatedPinRoles(); CMD_ExecuteCommand("clearAll", 0); + CMD_ExecuteCommand("led_expoMode", 0); Main_Init(); } } @@ -167,10 +169,17 @@ int rtos_get_time() { #include "sim/sim_public.h" int __cdecl main(int argc, char **argv) { - bool bWantsUnitTests = 0; + bool bWantsUnitTests = 1; WSADATA wsaData; int iResult; + int maxTest = 100; + for (int i = 0; i <= maxTest; i++) { + float frac = (float)i / (float)(maxTest); + float in = frac; + float res = LED_BrightnessMapping(255, in); + printf("Brightness %f with color %f gives %f\n", in, 255.0f, res); + } // Initialize Winsock iResult = WSAStartup(MAKEWORD(2,2), &wsaData); if (iResult != 0) {