From e70b6b210e5bfa29c19bc9203c229df5e331b01a Mon Sep 17 00:00:00 2001 From: MichaelDvP Date: Tue, 13 Sep 2022 07:54:36 +0200 Subject: [PATCH] check command `send` for valid data --- src/emsesp.cpp | 5 ----- src/emsesp.h | 1 - src/system.cpp | 3 +-- src/telegram.cpp | 12 ++++++++---- src/telegram.h | 2 +- src/test/test.cpp | 4 ++-- 6 files changed, 12 insertions(+), 15 deletions(-) diff --git a/src/emsesp.cpp b/src/emsesp.cpp index 51e663e55..42d405edc 100644 --- a/src/emsesp.cpp +++ b/src/emsesp.cpp @@ -1275,11 +1275,6 @@ void EMSESP::incoming_telegram(uint8_t * data, const uint8_t length) { } } -// sends raw data of bytes along the Tx line -void EMSESP::send_raw_telegram(const char * data) { - txservice_.send_raw(data); -} - // start all the core services // the services must be loaded in the correct order void EMSESP::start() { diff --git a/src/emsesp.h b/src/emsesp.h index 0835f2034..4a31f7c5d 100644 --- a/src/emsesp.h +++ b/src/emsesp.h @@ -128,7 +128,6 @@ class EMSESP { static void send_write_request(const uint16_t type_id, const uint8_t dest, const uint8_t offset, const uint8_t value); static void send_write_request(const uint16_t type_id, const uint8_t dest, const uint8_t offset, const uint8_t value, const uint16_t validate_typeid); - static void send_raw_telegram(const char * data); static bool device_exists(const uint8_t device_id); static bool cmd_is_readonly(const uint8_t device_type, const char * cmd, const int8_t id); diff --git a/src/system.cpp b/src/system.cpp index 7edefddab..faa66e66e 100644 --- a/src/system.cpp +++ b/src/system.cpp @@ -108,8 +108,7 @@ bool System::command_pin(const char * value, const int8_t id) { // send raw to ems bool System::command_send(const char * value, const int8_t id) { - EMSESP::send_raw_telegram(value); // ignore id - return true; + return EMSESP::txservice_.send_raw(value); // ignore id } // fetch device values diff --git a/src/telegram.cpp b/src/telegram.cpp index 1a06a1ea2..ea16e9e62 100644 --- a/src/telegram.cpp +++ b/src/telegram.cpp @@ -540,9 +540,9 @@ void TxService::read_request(const uint16_t type_id, const uint8_t dest, const u } // Send a raw telegram to the bus, telegram is a text string of hex values -void TxService::send_raw(const char * telegram_data) { +bool TxService::send_raw(const char * telegram_data) { if (telegram_data == nullptr) { - return; + return false; } // since the telegram data is a const, make a copy. add 1 to grab the \0 EOS @@ -562,6 +562,8 @@ void TxService::send_raw(const char * telegram_data) { if ((p = strtok(telegram, " ,"))) { // delimiter strlcpy(value, p, sizeof(value)); data[0] = (uint8_t)strtol(value, 0, 16); + } else { + return false; } // and iterate until end @@ -573,11 +575,13 @@ void TxService::send_raw(const char * telegram_data) { } } - if (count == 0) { - return; // nothing to send + // check valid length and src + if ((count < 4) || ((data[0] & 0x7F) != ems_bus_id())) { + return false; } add(Telegram::Operation::TX_RAW, data, count + 1, 0, true); // add to top/front of Tx queue + return true; } // add last Tx to tx queue and increment count diff --git a/src/telegram.h b/src/telegram.h index c9ac32714..cafa60b42 100644 --- a/src/telegram.h +++ b/src/telegram.h @@ -304,7 +304,7 @@ class TxService : public EMSbus { const bool front = false); void add(const uint8_t operation, const uint8_t * data, const uint8_t length, const uint16_t validateid, const bool front = false); void read_request(const uint16_t type_id, const uint8_t dest, const uint8_t offset = 0, const uint8_t length = 0); - void send_raw(const char * telegram_data); + bool send_raw(const char * telegram_data); void send_poll() const; void retry_tx(const uint8_t operation, const uint8_t * data, const uint8_t length); bool is_last_tx(const uint8_t src, const uint8_t dest) const; diff --git a/src/test/test.cpp b/src/test/test.cpp index 349a95f6f..7652fff36 100644 --- a/src/test/test.cpp +++ b/src/test/test.cpp @@ -1046,7 +1046,7 @@ void Test::run_test(uuid::console::Shell & shell, const std::string & cmd, const EMSESP::show_device_values(shell); shell.invoke_command("call system publish"); - // EMSESP::send_raw_telegram("B0 00 FF 18 02 62 80 00 B8"); + // EMSESP::txservice_.send_raw("B0 00 FF 18 02 62 80 00 B8"); } if (command == "heatpump") { @@ -1071,7 +1071,7 @@ void Test::run_test(uuid::console::Shell & shell, const std::string & cmd, const rx_telegram({0xB0, 00, 0xFF, 0x18, 02, 0x62, 0x80, 00, 0xB8}); - EMSESP::send_raw_telegram("B0 00 FF 18 02 62 80 00 B8"); + EMSESP::txservice_.send_raw("B0 00 FF 18 02 62 80 00 B8"); uart_telegram("30 00 FF 0A 02 6A 04"); // SM100 pump on 1 uart_telegram("30 00 FF 00 02 64 00 00 00 04 00 00 FF 00 00 1E 0B 09 64 00 00 00 00"); // SM100 modulation