From 702af4b1c88aabf61f075aa36b94a6984b27c64d Mon Sep 17 00:00:00 2001 From: MichaelDvP Date: Tue, 13 Sep 2022 07:17:42 +0200 Subject: [PATCH 1/5] add operation to log messages #594 --- src/emsesp.cpp | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/src/emsesp.cpp b/src/emsesp.cpp index 5d5ced4fc..5e578feed 100644 --- a/src/emsesp.cpp +++ b/src/emsesp.cpp @@ -685,10 +685,13 @@ std::string EMSESP::pretty_telegram(std::shared_ptr telegram) { std::string str; str.reserve(200); if (telegram->operation == Telegram::Operation::RX_READ) { - str = src_name + "(" + Helpers::hextoa(src) + ") <- " + dest_name + "(" + Helpers::hextoa(dest) + "), " + type_name + "(" + str = src_name + "(" + Helpers::hextoa(src) + ") -R-> " + dest_name + "(" + Helpers::hextoa(dest) + "), " + type_name + "(" + Helpers::hextoa(telegram->type_id) + "), length: " + Helpers::hextoa(telegram->message_data[0]); + } else if (telegram->dest == 0) { + str = src_name + "(" + Helpers::hextoa(src) + ") -B-> " + dest_name + "(" + Helpers::hextoa(dest) + "), " + type_name + "(" + + Helpers::hextoa(telegram->type_id) + "), data: " + telegram->to_string_message(); } else { - str = src_name + "(" + Helpers::hextoa(src) + ") -> " + dest_name + "(" + Helpers::hextoa(dest) + "), " + type_name + "(" + str = src_name + "(" + Helpers::hextoa(src) + ") -W-> " + dest_name + "(" + Helpers::hextoa(dest) + "), " + type_name + "(" + Helpers::hextoa(telegram->type_id) + "), data: " + telegram->to_string_message(); } From df8a36c695ede0ed37eb0d5faa6043cce70211b8 Mon Sep 17 00:00:00 2001 From: MichaelDvP Date: Tue, 13 Sep 2022 07:27:41 +0200 Subject: [PATCH 2/5] calculate length of telegram next part --- src/emsesp.cpp | 2 +- src/telegram.cpp | 29 ++++++++++++----------------- src/telegram.h | 2 +- 3 files changed, 14 insertions(+), 19 deletions(-) diff --git a/src/emsesp.cpp b/src/emsesp.cpp index 5e578feed..51e663e55 100644 --- a/src/emsesp.cpp +++ b/src/emsesp.cpp @@ -1216,7 +1216,7 @@ void EMSESP::incoming_telegram(uint8_t * data, const uint8_t length) { tx_successful = true; // if telegram is longer read next part with offset +25 for ems+ or +27 for ems1.0 - if ((length == 32) && (txservice_.read_next_tx(data[3]) == read_id_)) { + if ((length >= 31) && (txservice_.read_next_tx(data[3], length) == read_id_)) { read_next_ = true; } } diff --git a/src/telegram.cpp b/src/telegram.cpp index 5a81864db..1a06a1ea2 100644 --- a/src/telegram.cpp +++ b/src/telegram.cpp @@ -625,24 +625,19 @@ void TxService::retry_tx(const uint8_t operation, const uint8_t * data, const ui } // send a request to read the next block of data from longer telegrams -uint16_t TxService::read_next_tx(uint8_t offset) { - // add to the top/front of the queue - uint8_t message_data[1] = {EMS_MAX_TELEGRAM_LENGTH}; // request all data, 32 bytes - if (telegram_last_->offset != offset) { - return 0; +// send a request to read the next block of data from longer telegrams +uint16_t TxService::read_next_tx(const uint8_t offset, const uint8_t length) { + uint8_t old_length = telegram_last_->type_id > 0xFF ? length - 7 : length - 5; + uint8_t next_length = telegram_last_->type_id > 0xFF ? EMS_MAX_TELEGRAM_MESSAGE_LENGTH - 2 : EMS_MAX_TELEGRAM_MESSAGE_LENGTH; + uint8_t next_offset = telegram_last_->offset + old_length; + uint8_t message_data = (UINT8_MAX - next_offset) >= next_length ? next_length : UINT8_MAX - next_offset; + // check telegram, offset and overflow + // some telegrams only reply with one byte less, but have higher offsets (0x10) + if (old_length >= (next_length - 1) && telegram_last_->offset == offset) { + add(Telegram::Operation::TX_READ, telegram_last_->dest, telegram_last_->type_id, next_offset, &message_data, 1, 0, true); + return telegram_last_->type_id; } - - uint8_t add_offset = 25; // for EMS+ telegram increase offset by 25 - if (telegram_last_->type_id < 0x100) { // but for EMS1.0 by 27 - add_offset = 27; - } - - if (UINT8_MAX - telegram_last_->offset < add_offset) { // stop if new offset would overflow - return 0; - } - - add(Telegram::Operation::TX_READ, telegram_last_->dest, telegram_last_->type_id, telegram_last_->offset + add_offset, message_data, 1, 0, true); - return telegram_last_->type_id; + return 0; } // checks if a telegram is sent to us matches the last Tx request diff --git a/src/telegram.h b/src/telegram.h index 2312b5b3e..c9ac32714 100644 --- a/src/telegram.h +++ b/src/telegram.h @@ -309,7 +309,7 @@ class TxService : public EMSbus { 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; uint16_t post_send_query(); - uint16_t read_next_tx(uint8_t offset); + uint16_t read_next_tx(const uint8_t offset, const uint8_t length); uint8_t retry_count() const { return retry_count_; From e70b6b210e5bfa29c19bc9203c229df5e331b01a Mon Sep 17 00:00:00 2001 From: MichaelDvP Date: Tue, 13 Sep 2022 07:54:36 +0200 Subject: [PATCH 3/5] 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 From 2bc37027ddf20f3107e55aa6fee44b59dd7c51cf Mon Sep 17 00:00:00 2001 From: MichaelDvP Date: Tue, 13 Sep 2022 07:56:07 +0200 Subject: [PATCH 4/5] remove unused/obsolet `command_pin` --- src/system.cpp | 41 ----------------------------------------- src/system.h | 1 - 2 files changed, 42 deletions(-) diff --git a/src/system.cpp b/src/system.cpp index faa66e66e..98c230298 100644 --- a/src/system.cpp +++ b/src/system.cpp @@ -67,45 +67,6 @@ uint8_t System::language_index() { return 0; // EN } -// send on/off to a gpio pin -// value: true = HIGH, false = LOW -bool System::command_pin(const char * value, const int8_t id) { -#ifndef EMSESP_STANDALONE - - if (!is_valid_gpio(id)) { - LOG_INFO(F("Invalid GPIO number")); - return false; - } - - bool v = false; - std::string v1 = {7, '\0'}; - int v2 = 0; - - if (id == 25 && Helpers::value2number(value, v2)) { - if (v2 >= 0 && v2 <= 255) { - dacWrite(id, v2); - return true; - } - } else if (Helpers::value2bool(value, v)) { - pinMode(id, OUTPUT); - digitalWrite(id, v); - // LOG_INFO(F("GPIO %d set to %s"), id, v ? "HIGH" : "LOW"); - return true; - } else if (Helpers::value2string(value, v1)) { - if (v1 == "input" || v1 == "in" || v1 == "-1") { - pinMode(id, INPUT); - v = digitalRead(id); - // LOG_INFO(F("GPIO %d set input, state %s"), id, v ? "HIGH" : "LOW"); - return true; - } - } - - // LOG_INFO(F("GPIO %d: invalid value"), id); -#endif - - return false; -} - // send raw to ems bool System::command_send(const char * value, const int8_t id) { return EMSESP::txservice_.send_raw(value); // ignore id @@ -694,8 +655,6 @@ void System::system_check() { // commands - takes static function pointers void System::commands_init() { - // Command::add(EMSdevice::DeviceType::SYSTEM, F_(pin), System::command_pin, F("set a GPIO on/off"), CommandFlag::ADMIN_ONLY); - // TODO these should be translated too Command::add(EMSdevice::DeviceType::SYSTEM, F_(send), System::command_send, F("send a telegram"), CommandFlag::ADMIN_ONLY); Command::add(EMSdevice::DeviceType::SYSTEM, F_(fetch), System::command_fetch, F("refresh all EMS values"), CommandFlag::ADMIN_ONLY); diff --git a/src/system.h b/src/system.h index f5204585a..c8a90ba24 100644 --- a/src/system.h +++ b/src/system.h @@ -49,7 +49,6 @@ class System { void loop(); // commands - static bool command_pin(const char * value, const int8_t id); static bool command_send(const char * value, const int8_t id); static bool command_publish(const char * value, const int8_t id); static bool command_fetch(const char * value, const int8_t id); From 9d6af82f9c442a9492047e194b102738ee0aa602 Mon Sep 17 00:00:00 2001 From: MichaelDvP Date: Sat, 17 Sep 2022 09:09:10 +0200 Subject: [PATCH 5/5] remove double comment --- src/telegram.cpp | 1 - 1 file changed, 1 deletion(-) diff --git a/src/telegram.cpp b/src/telegram.cpp index ea16e9e62..f96cdf43e 100644 --- a/src/telegram.cpp +++ b/src/telegram.cpp @@ -628,7 +628,6 @@ void TxService::retry_tx(const uint8_t operation, const uint8_t * data, const ui tx_telegrams_.emplace_front(tx_telegram_id_++, std::move(telegram_last_), true, get_post_send_query()); } -// send a request to read the next block of data from longer telegrams // send a request to read the next block of data from longer telegrams uint16_t TxService::read_next_tx(const uint8_t offset, const uint8_t length) { uint8_t old_length = telegram_last_->type_id > 0xFF ? length - 7 : length - 5;