From 8f35be4edf3a3fbe789ad10a647969718e05180a Mon Sep 17 00:00:00 2001 From: proddy Date: Mon, 21 Oct 2024 15:35:13 +0200 Subject: [PATCH] replace sstream --- src/helpers.cpp | 15 +++--------- src/helpers.h | 4 ---- src/system.cpp | 60 ++++++++++++++++++++++++++++++----------------- src/test/test.cpp | 6 +++++ 4 files changed, 48 insertions(+), 37 deletions(-) diff --git a/src/helpers.cpp b/src/helpers.cpp index 2ae936012..dcef19c3b 100644 --- a/src/helpers.cpp +++ b/src/helpers.cpp @@ -99,7 +99,7 @@ char * Helpers::ultostr(char * ptr, uint32_t value, const uint8_t base) { #endif /** - * fast atoi returning a std::string + * fast itoa returning a std::string * http://www.strudel.org.uk/itoa/ * */ @@ -122,8 +122,9 @@ std::string Helpers::itoa(int16_t value) { } /* - * fast itoa and optimized for ESP32 + * fast itoa * written by Lukás Chmela, Released under GPLv3. http://www.strudel.org.uk/itoa/ version 0.4 + * optimized for ESP32 */ char * Helpers::itoa(int32_t value, char * result, const uint8_t base) { // check that the base if valid @@ -834,14 +835,4 @@ float Helpers::numericoperator2scalefactor(int8_t numeric_operator) { return -numeric_operator; } -// convert the data into a vector of strings -void Helpers::splitArguments(const char * data, std::vector & arguments) { - std::stringstream ss(data); - std::string item; - - while (std::getline(ss, item, ' ')) { - arguments.push_back(item); - } -} - } // namespace emsesp diff --git a/src/helpers.h b/src/helpers.h index d7a9a6039..bf2f84246 100644 --- a/src/helpers.h +++ b/src/helpers.h @@ -19,8 +19,6 @@ #ifndef EMSESP_HELPERS_H #define EMSESP_HELPERS_H -#include - #include "telegram.h" // for EMS_VALUE_* settings #include "common.h" @@ -83,8 +81,6 @@ class Helpers { static const char * translated_word(const char * const * strings, const bool force_en = false); - static void splitArguments(const char * data, std::vector & arguments); - #ifdef EMSESP_STANDALONE static char * ultostr(char * ptr, uint32_t value, const uint8_t base); #endif diff --git a/src/system.cpp b/src/system.cpp index 01d84c887..21bd58322 100644 --- a/src/system.cpp +++ b/src/system.cpp @@ -2015,34 +2015,52 @@ bool System::uploadFirmwareURL(const char * url) { return true; // OK } -// read command, e.g. read [offset] [length] from console -// or a call system read [offset] [length] from the API +// read command, e.g. read [offset] [length] from console or API bool System::readCommand(const char * data) { - // convert the data into a vector of strings - std::vector arguments = {}; - Helpers::splitArguments(data, arguments); + // extract [offset] [length] from string + char * p; + char value[10] = {0}; // null just in case - auto num_args = arguments.size(); + // make a copy so we can iterate + char data_args[EMS_MAX_TELEGRAM_LENGTH]; + for (uint8_t i = 0; i < strlen(data); i++) { + data_args[i] = data[i]; + } + data_args[strlen(data)] = '\0'; // make sure its terminated - if (num_args > 4 || num_args == 0) { - return false; + uint8_t device_id = 0; // is in hex + uint16_t type_id = 0; // is in hex + uint8_t length = 0; + uint8_t offset = 0; + + // first check deviceID + if ((p = strtok(data_args, " ,"))) { // delimiter comma or space + strlcpy(value, p, 10); // get string + device_id = (uint8_t)Helpers::hextoint(value); // convert hex to int + if (!EMSESP::valid_device(device_id)) { + LOG_ERROR("Invalid device ID (%d) for read command", device_id); + return false; // invalid device + } } - uint8_t device_id = Helpers::hextoint(arguments[0].c_str()); - if (!EMSESP::valid_device(device_id)) { - LOG_ERROR("Invalid device ID for read command"); - return false; // invalid device + // iterate until end + uint8_t num_args = 0; + while (p != 0) { + if ((p = strtok(nullptr, " ,"))) { // delimiter comma or space + strlcpy(value, p, 10); // get string + if (num_args == 0) { + type_id = (uint16_t)Helpers::hextoint(value); // convert hex to int + } else if (num_args == 1) { + offset = Helpers::atoint(value); // decimal + } else if (num_args == 2) { + length = Helpers::atoint(value); // decimal + } + num_args++; + } } - uint16_t type_id = Helpers::hextoint(arguments[1].c_str()); - uint8_t length = 0; - uint16_t offset = 0; - - if (num_args == 4) { - offset = Helpers::hextoint(arguments[2].c_str()); - length = Helpers::hextoint(arguments[3].c_str()); - } else if (num_args == 3) { - offset = Helpers::hextoint(arguments.back().c_str()); + if (num_args == 0) { + return false; // invalid number of arguments } EMSESP::send_read_request(type_id, device_id, offset, length, true); diff --git a/src/test/test.cpp b/src/test/test.cpp index b17a696f1..8768b0952 100644 --- a/src/test/test.cpp +++ b/src/test/test.cpp @@ -1027,6 +1027,12 @@ void Test::run_test(uuid::console::Shell & shell, const std::string & cmd, const request.url("/api"); EMSESP::webAPIService.webAPIService(&request, json); + char data7[] = "{\"device\":\"system\", \"cmd\":\"read\",\"value\":\"08 221\"}"; + deserializeJson(doc, data7); + json = doc.as(); + request.url("/api"); + EMSESP::webAPIService.webAPIService(&request, json); + shell.invoke_command("call system read \"8 2 27 1\"");