replace sstream

This commit is contained in:
proddy
2024-10-21 15:35:13 +02:00
parent 670a2bbb4a
commit 8f35be4edf
4 changed files with 48 additions and 37 deletions

View File

@@ -99,7 +99,7 @@ char * Helpers::ultostr(char * ptr, uint32_t value, const uint8_t base) {
#endif #endif
/** /**
* fast atoi returning a std::string * fast itoa returning a std::string
* http://www.strudel.org.uk/itoa/ * 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 * 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) { char * Helpers::itoa(int32_t value, char * result, const uint8_t base) {
// check that the base if valid // check that the base if valid
@@ -834,14 +835,4 @@ float Helpers::numericoperator2scalefactor(int8_t numeric_operator) {
return -numeric_operator; return -numeric_operator;
} }
// convert the data into a vector of strings
void Helpers::splitArguments(const char * data, std::vector<std::string> & arguments) {
std::stringstream ss(data);
std::string item;
while (std::getline(ss, item, ' ')) {
arguments.push_back(item);
}
}
} // namespace emsesp } // namespace emsesp

View File

@@ -19,8 +19,6 @@
#ifndef EMSESP_HELPERS_H #ifndef EMSESP_HELPERS_H
#define EMSESP_HELPERS_H #define EMSESP_HELPERS_H
#include <sstream>
#include "telegram.h" // for EMS_VALUE_* settings #include "telegram.h" // for EMS_VALUE_* settings
#include "common.h" #include "common.h"
@@ -83,8 +81,6 @@ class Helpers {
static const char * translated_word(const char * const * strings, const bool force_en = false); static const char * translated_word(const char * const * strings, const bool force_en = false);
static void splitArguments(const char * data, std::vector<std::string> & arguments);
#ifdef EMSESP_STANDALONE #ifdef EMSESP_STANDALONE
static char * ultostr(char * ptr, uint32_t value, const uint8_t base); static char * ultostr(char * ptr, uint32_t value, const uint8_t base);
#endif #endif

View File

@@ -2015,34 +2015,52 @@ bool System::uploadFirmwareURL(const char * url) {
return true; // OK return true; // OK
} }
// read command, e.g. read <deviceID> <type ID> [offset] [length] from console // read command, e.g. read <deviceID> <type ID> [offset] [length] from console or API
// or a call system read <deviceID> <type ID> [offset] [length] from the API
bool System::readCommand(const char * data) { bool System::readCommand(const char * data) {
// convert the data into a vector of strings // extract <deviceID> <type ID> [offset] [length] from string
std::vector<std::string> arguments = {}; char * p;
Helpers::splitArguments(data, arguments); 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];
if (num_args > 4 || num_args == 0) { for (uint8_t i = 0; i < strlen(data); i++) {
return false; data_args[i] = data[i];
} }
data_args[strlen(data)] = '\0'; // make sure its terminated
uint8_t device_id = Helpers::hextoint(arguments[0].c_str()); 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)) { if (!EMSESP::valid_device(device_id)) {
LOG_ERROR("Invalid device ID for read command"); LOG_ERROR("Invalid device ID (%d) for read command", device_id);
return false; // invalid device return false; // invalid device
} }
}
uint16_t type_id = Helpers::hextoint(arguments[1].c_str()); // iterate until end
uint8_t length = 0; uint8_t num_args = 0;
uint16_t offset = 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++;
}
}
if (num_args == 4) { if (num_args == 0) {
offset = Helpers::hextoint(arguments[2].c_str()); return false; // invalid number of arguments
length = Helpers::hextoint(arguments[3].c_str());
} else if (num_args == 3) {
offset = Helpers::hextoint(arguments.back().c_str());
} }
EMSESP::send_read_request(type_id, device_id, offset, length, true); EMSESP::send_read_request(type_id, device_id, offset, length, true);

View File

@@ -1027,6 +1027,12 @@ void Test::run_test(uuid::console::Shell & shell, const std::string & cmd, const
request.url("/api"); request.url("/api");
EMSESP::webAPIService.webAPIService(&request, json); EMSESP::webAPIService.webAPIService(&request, json);
char data7[] = "{\"device\":\"system\", \"cmd\":\"read\",\"value\":\"08 221\"}";
deserializeJson(doc, data7);
json = doc.as<JsonVariant>();
request.url("/api");
EMSESP::webAPIService.webAPIService(&request, json);
shell.invoke_command("call system read \"8 2 27 1\""); shell.invoke_command("call system read \"8 2 27 1\"");