From ea550b1656214fa929e2049e520c284408f3c740 Mon Sep 17 00:00:00 2001 From: proddy Date: Sun, 7 Nov 2021 13:50:39 +0100 Subject: [PATCH] read command checks device id - #184 --- src/console.cpp | 12 +++++++++--- src/emsesp.cpp | 12 ++++++++++++ src/emsesp.h | 1 + 3 files changed, 22 insertions(+), 3 deletions(-) diff --git a/src/console.cpp b/src/console.cpp index 54fe8eae3..34e7fcfa8 100644 --- a/src/console.cpp +++ b/src/console.cpp @@ -242,12 +242,18 @@ void EMSESPShell::add_console_commands() { }); commands->add_command(ShellContext::MAIN, - CommandFlags::ADMIN, + CommandFlags::USER, flash_string_vector{F_(read)}, flash_string_vector{F_(deviceid_mandatory), F_(typeid_mandatory), F_(offset_optional)}, [=](Shell & shell __attribute__((unused)), const std::vector & arguments) { - uint8_t device_id = Helpers::hextoint(arguments.front().c_str()); - uint16_t type_id = Helpers::hextoint(arguments[1].c_str()); + uint8_t device_id = Helpers::hextoint(arguments.front().c_str()); + + if (!EMSESP::valid_device(device_id)) { + shell.printfln(F("Invalid device ID")); + return; + } + + uint16_t type_id = Helpers::hextoint(arguments[1].c_str()); if (arguments.size() == 4) { uint16_t offset = Helpers::hextoint(arguments[2].c_str()); uint8_t length = Helpers::hextoint(arguments.back().c_str()); diff --git a/src/emsesp.cpp b/src/emsesp.cpp index 4aea3ec43..101d9b64a 100644 --- a/src/emsesp.cpp +++ b/src/emsesp.cpp @@ -94,6 +94,18 @@ void EMSESP::fetch_device_values(const uint8_t device_id) { } } +// see if the device ID exists +bool EMSESP::valid_device(const uint8_t device_id) { + for (const auto & emsdevice : emsdevices) { + if (emsdevice) { + if (emsdevice->is_device_id(device_id)) { + return true; + } + } + } + return false; // can't find it +} + // for a specific EMS device type go and request data values void EMSESP::fetch_device_values_type(const uint8_t device_type) { for (const auto & emsdevice : emsdevices) { diff --git a/src/emsesp.h b/src/emsesp.h index 69bd363d3..9dcf3cf06 100644 --- a/src/emsesp.h +++ b/src/emsesp.h @@ -212,6 +212,7 @@ class EMSESP { static void fetch_device_values(const uint8_t device_id = 0); static void fetch_device_values_type(const uint8_t device_type); + static bool valid_device(const uint8_t device_id); static bool add_device(const uint8_t device_id, const uint8_t product_id, std::string & version, const uint8_t brand); static void scan_devices();