diff --git a/src/emsesp.cpp b/src/emsesp.cpp index d7a278e55..ca2418e37 100644 --- a/src/emsesp.cpp +++ b/src/emsesp.cpp @@ -60,7 +60,7 @@ Shower EMSESP::shower_; // Shower logic // static/common variables uint8_t EMSESP::actual_master_thermostat_ = EMSESP_DEFAULT_MASTER_THERMOSTAT; // which thermostat leads when multiple found -uint16_t EMSESP::watch_id_ = WATCH_NONE; // for when log is TRACE. 0 means no trace set +uint16_t EMSESP::watch_id_ = WATCH_ID_NONE; // for when log is TRACE. 0 means no trace set uint8_t EMSESP::watch_ = 0; // trace off bool EMSESP::tap_water_active_ = false; // for when Boiler states we having running warm water. used in Shower() bool EMSESP::ems_read_only_; @@ -398,8 +398,8 @@ void EMSESP::process_version(std::shared_ptr telegram) { // returns false if there are none found bool EMSESP::process_telegram(std::shared_ptr telegram) { // if watching... - if (watch() == 1) { - if ((watch_id_ == WATCH_NONE) || (telegram->src == watch_id_) || (telegram->dest == watch_id_) || (telegram->type_id == watch_id_)) { + if (watch() == WATCH_ON) { + if ((watch_id_ == WATCH_ID_NONE) || (telegram->src == watch_id_) || (telegram->dest == watch_id_) || (telegram->type_id == watch_id_)) { LOG_NOTICE(pretty_telegram(telegram).c_str()); } } @@ -570,6 +570,7 @@ void EMSESP::send_write_request(const uint16_t type_id, // this is main entry point when data is received on the Rx line, via emsuart library // we check if its a complete telegram or just a single byte (which could be a poll or a return status) +// the CRC check is not done here, only when it's added to the Rx queue with add() void EMSESP::incoming_telegram(uint8_t * data, const uint8_t length) { static uint32_t tx_time_ = 0; // check first for echo @@ -641,6 +642,7 @@ void EMSESP::incoming_telegram(uint8_t * data, const uint8_t length) { // check if there is a message for the roomcontroller Roomctrl::check((data[1] ^ 0x80 ^ rxservice_.ems_mask()), data); // add to RxQueue, what ever it is. + // in add() the CRC will be checked rxservice_.add(data, length); } } @@ -828,11 +830,11 @@ void EMSESP::console_commands(Shell & shell, unsigned int context) { [](Shell & shell, const std::vector & arguments) { // get raw/pretty if (arguments[0] == read_flash_string(F_(raw))) { - emsesp::EMSESP::watch(2); // raw + emsesp::EMSESP::watch(WATCH_RAW); // raw } else if (arguments[0] == read_flash_string(F_(on))) { - emsesp::EMSESP::watch(1); // on + emsesp::EMSESP::watch(WATCH_ON); // on } else if (arguments[0] == read_flash_string(F_(off))) { - emsesp::EMSESP::watch(0); // off + emsesp::EMSESP::watch(WATCH_OFF); // off } else { shell.printfln(F_(invalid_watch)); return; @@ -843,13 +845,13 @@ void EMSESP::console_commands(Shell & shell, unsigned int context) { // get the watch_id if its set watch_id = Helpers::hextoint(arguments[1].c_str()); } else { - watch_id = WATCH_NONE; + watch_id = WATCH_ID_NONE; } emsesp::EMSESP::watch_id(watch_id); uint8_t watch = emsesp::EMSESP::watch(); - if (watch == 0) { + if (watch == WATCH_OFF) { shell.printfln(F("Watch is off")); return; } @@ -859,14 +861,14 @@ void EMSESP::console_commands(Shell & shell, unsigned int context) { shell.log_level(Level::NOTICE); } - if (watch == 1) { + if (watch == WATCH_ON) { shell.printfln(F("Watching incoming telegrams, displayed in decoded format")); } else { - shell.printfln(F("Watching incoming telegrams, displayed as raw bytes")); + shell.printfln(F("Watching incoming telegrams, displayed as raw bytes")); // WATCH_RAW } watch_id = emsesp::EMSESP::watch_id(); - if (watch_id != WATCH_NONE) { + if (watch_id != WATCH_ID_NONE) { shell.printfln(F("Filtering only telegrams that match a device ID or telegram type of 0x%02X"), watch_id); } }); diff --git a/src/emsesp.h b/src/emsesp.h index a2c862df1..3cb31d51f 100644 --- a/src/emsesp.h +++ b/src/emsesp.h @@ -46,7 +46,7 @@ #include "devices/boiler.h" -#define WATCH_NONE 0 // no watch id set +#define WATCH_ID_NONE 0 // no watch id set namespace emsesp { @@ -109,8 +109,9 @@ class EMSESP { watch_ = watch; // 0=off, 1=on, 2=raw } + enum Watch : uint8_t { WATCH_OFF, WATCH_ON, WATCH_RAW }; static uint8_t watch() { - return watch_; // 0=off, 1=on, 2=raw + return watch_; } static bool tap_water_active() { diff --git a/src/telegram.cpp b/src/telegram.cpp index 0b305903d..2f44a8bfa 100644 --- a/src/telegram.cpp +++ b/src/telegram.cpp @@ -246,8 +246,8 @@ void RxService::add(uint8_t * data, uint8_t length) { // validate the CRC uint8_t crc = calculate_crc(data, length - 1); - if ((data[length - 1] != crc) && (EMSESP::watch() != 0)) { - LOG_ERROR(F("Rx: %s %s(BAD, CRC %02X != %02X)%s"), Helpers::data_to_hex(data, length).c_str(), COLOR_RED, data[length - 1], crc, COLOR_RESET); + if ((data[length - 1] != crc) && (EMSESP::watch() != EMSESP::Watch::WATCH_OFF)) { + LOG_ERROR(F("Rx: %s %s(CRC %02X != %02X)%s"), Helpers::data_to_hex(data, length).c_str(), COLOR_RED, data[length - 1], crc, COLOR_RESET); increment_telegram_error_count(); return; } @@ -296,9 +296,9 @@ void RxService::add(uint8_t * data, uint8_t length) { } // if we're watching and "raw" print out actual telegram as bytes to the console - if (EMSESP::watch() == 2) { + if (EMSESP::watch() == EMSESP::Watch::WATCH_RAW) { uint16_t trace_watch_id = EMSESP::watch_id(); - if ((trace_watch_id == WATCH_NONE) || (src == trace_watch_id) || (dest == trace_watch_id) || (type_id == trace_watch_id)) { + if ((trace_watch_id == WATCH_ID_NONE) || (src == trace_watch_id) || (dest == trace_watch_id) || (type_id == trace_watch_id)) { LOG_NOTICE(F("Rx: %s"), Helpers::data_to_hex(data, length).c_str()); } } @@ -465,7 +465,7 @@ void TxService::send_telegram(const QueuedTxTelegram & tx_telegram) { #ifdef EMSESP_DEBUG // if watching in 'raw' mode - if (EMSESP::watch() == 2) { + if (EMSESP::watch() == EMSESP::Watch::WATCH_RAW) { LOG_NOTICE(F("[DEBUG] Tx: %s"), Helpers::data_to_hex(telegram_raw, length).c_str()); } #endif @@ -624,26 +624,24 @@ void TxService::send_raw(const char * telegram_data) { // add last Tx to tx queue and increment count // returns retry count, or 0 if all done void TxService::retry_tx(const uint8_t operation, const uint8_t * data, const uint8_t length) { - retry_count_++; // have we reached the limit? if so, reset count and give up - if (retry_count_ > MAXIMUM_TX_RETRIES) { + if (++retry_count_ > MAXIMUM_TX_RETRIES) { reset_retry_count(); // give up increment_telegram_fail_count(); // another Tx fail LOG_ERROR(F("Last Tx %s operation failed after %d retries. Ignoring request."), (operation == Telegram::Operation::TX_WRITE) ? F("Write") : F("Read"), MAXIMUM_TX_RETRIES); - } else { - LOG_DEBUG(F("[DEBUG] Last Tx %s operation failed. Retry #%d. sent message data: %s, received: %s"), - (operation == Telegram::Operation::TX_WRITE) ? F("Write") : F("Read"), - retry_count_, - telegram_last_->to_string().c_str(), - Helpers::data_to_hex(data, length).c_str()); - return; } + LOG_DEBUG(F("[DEBUG] Last Tx %s operation failed. Retry #%d. sent message data: %s, received: %s"), + (operation == Telegram::Operation::TX_WRITE) ? F("Write") : F("Read"), + retry_count_, + telegram_last_->to_string().c_str(), + Helpers::data_to_hex(data, length).c_str()); + // add to the top of the queue if (tx_telegrams_.size() >= MAX_TX_TELEGRAMS) { tx_telegrams_.pop_back();