small refactoring

This commit is contained in:
proddy
2020-09-05 11:51:02 +02:00
parent b13bfd3166
commit c21af043fa
4 changed files with 43 additions and 60 deletions

View File

@@ -713,7 +713,6 @@ void EMSESP::incoming_telegram(uint8_t * data, const uint8_t length) {
if (tx_state != Telegram::Operation::NONE) { if (tx_state != Telegram::Operation::NONE) {
bool tx_successful = false; bool tx_successful = false;
EMSbus::tx_state(Telegram::Operation::NONE); // reset Tx wait state EMSbus::tx_state(Telegram::Operation::NONE); // reset Tx wait state
// txservice_.print_last_tx();
// if we're waiting on a Write operation, we want a single byte 1 or 4 // if we're waiting on a Write operation, we want a single byte 1 or 4
if ((tx_state == Telegram::Operation::TX_WRITE) && (length == 1)) { if ((tx_state == Telegram::Operation::TX_WRITE) && (length == 1)) {
@@ -721,8 +720,7 @@ void EMSESP::incoming_telegram(uint8_t * data, const uint8_t length) {
LOG_DEBUG(F("Last Tx write successful")); LOG_DEBUG(F("Last Tx write successful"));
txservice_.increment_telegram_write_count(); // last tx/write was confirmed ok txservice_.increment_telegram_write_count(); // last tx/write was confirmed ok
txservice_.send_poll(); // close the bus txservice_.send_poll(); // close the bus
publish_id_ = txservice_.get_post_send_query(); publish_id_ = txservice_.post_send_query(); // follow up with any post-read if set
txservice_.post_send_query(); // follow up with any post-read
txservice_.reset_retry_count(); txservice_.reset_retry_count();
tx_successful = true; tx_successful = true;
} else if (first_value == TxService::TX_WRITE_FAIL) { } else if (first_value == TxService::TX_WRITE_FAIL) {

View File

@@ -213,7 +213,7 @@ void RxService::add(uint8_t * data, uint8_t length) {
// if we receive a hc2.. telegram from 0x19.. match it to master_thermostat if master is 0x18 // if we receive a hc2.. telegram from 0x19.. match it to master_thermostat if master is 0x18
src = EMSESP::check_master_device(src, type_id, true); src = EMSESP::check_master_device(src, type_id, true);
// create the telegram // create the telegram
auto telegram = std::make_shared<Telegram>(Telegram::Operation::RX, src, dest, type_id, offset, message_data, message_length); auto telegram = std::make_shared<Telegram>(Telegram::Operation::RX, src, dest, type_id, offset, message_data, message_length);
// check if queue is full, if so remove top item to make space // check if queue is full, if so remove top item to make space
@@ -223,7 +223,6 @@ void RxService::add(uint8_t * data, uint8_t length) {
} }
rx_telegrams_.emplace_back(rx_telegram_id_++, std::move(telegram)); // add to queue rx_telegrams_.emplace_back(rx_telegram_id_++, std::move(telegram)); // add to queue
} }
// //
@@ -579,21 +578,20 @@ bool TxService::is_last_tx(const uint8_t src, const uint8_t dest) const {
} }
// sends a type_id read request to fetch values after a successful Tx write operation // sends a type_id read request to fetch values after a successful Tx write operation
void TxService::post_send_query() { // unless the post_send_query has a type_id of 0
if (telegram_last_post_send_query_) { uint16_t TxService::post_send_query() {
uint8_t dest = (telegram_last_->dest & 0x7F); uint16_t post_typeid = this->get_post_send_query();
uint8_t message_data[1] = {EMS_MAX_TELEGRAM_LENGTH}; // request all data, 32 bytes
add(Telegram::Operation::TX_READ, dest, telegram_last_post_send_query_, 0, message_data, 1, true);
// read_request(telegram_last_post_send_query_, dest, 0); // no offset
LOG_DEBUG(F("Sending post validate read, type ID 0x%02X to dest 0x%02X"), telegram_last_post_send_query_, dest);
}
}
// print out the last Tx that was sent if (post_typeid) {
void TxService::print_last_tx() { uint8_t dest = (this->telegram_last_->dest & 0x7F);
LOG_DEBUG(F("Last Tx %s operation: %s"), uint8_t message_data[1] = {EMS_MAX_TELEGRAM_LENGTH}; // request all data, 32 bytes
(telegram_last_->operation == Telegram::Operation::TX_WRITE) ? F("Write") : F("Read"), this->add(Telegram::Operation::TX_READ, dest, post_typeid, 0, message_data, 1, true);
telegram_last_->to_string().c_str()); // read_request(telegram_last_post_send_query_, dest, 0); // no offset
LOG_DEBUG(F("Sending post validate read, type ID 0x%02X to dest 0x%02X"), post_typeid, dest);
set_post_send_query(0); // reset
}
return post_typeid;
} }
} // namespace emsesp } // namespace emsesp

View File

@@ -182,13 +182,12 @@ class EMSbus {
private: private:
static constexpr uint32_t EMS_BUS_TIMEOUT = 30000; // timeout in ms before recognizing the ems bus is offline (30 seconds) static constexpr uint32_t EMS_BUS_TIMEOUT = 30000; // timeout in ms before recognizing the ems bus is offline (30 seconds)
static uint32_t last_bus_activity_; // timestamp of last time a valid Rx came in
static uint32_t last_bus_activity_; // timestamp of last time a valid Rx came in static bool bus_connected_; // start assuming the bus hasn't been connected
static bool bus_connected_; // start assuming the bus hasn't been connected static uint8_t ems_mask_; // unset=0xFF, buderus=0x00, junkers/ht3=0x80
static uint8_t ems_mask_; // unset=0xFF, buderus=0x00, junkers/ht3=0x80 static uint8_t ems_bus_id_; // the bus id, which configurable and stored in settings
static uint8_t ems_bus_id_; // the bus id, which configurable and stored in settings static uint8_t tx_mode_; // local copy of the tx mode
static uint8_t tx_mode_; // local copy of the tx mode static uint8_t tx_state_; // state of the Tx line (NONE or waiting on a TX_READ or TX_WRITE)
static uint8_t tx_state_; // state of the Tx line (NONE or waiting on a TX_READ or TX_WRITE)
}; };
class RxService : public EMSbus { class RxService : public EMSbus {
@@ -238,41 +237,35 @@ class RxService : public EMSbus {
uint32_t telegram_count_ = 0; // # Rx received uint32_t telegram_count_ = 0; // # Rx received
uint32_t telegram_error_count_ = 0; // # Rx CRC errors uint32_t telegram_error_count_ = 0; // # Rx CRC errors
std::shared_ptr<const Telegram> rx_telegram; // the incoming Rx telegram std::shared_ptr<const Telegram> rx_telegram; // the incoming Rx telegram
std::list<QueuedRxTelegram> rx_telegrams_; // the Rx Queue
std::list<QueuedRxTelegram> rx_telegrams_; // the Rx Queue
}; };
class TxService : public EMSbus { class TxService : public EMSbus {
public: public:
static constexpr size_t MAX_TX_TELEGRAMS = 20; // size of Tx queue static constexpr size_t MAX_TX_TELEGRAMS = 20; // size of Tx queue
static constexpr uint8_t TX_WRITE_FAIL = 4; // EMS return code for fail
static constexpr uint8_t TX_WRITE_FAIL = 4; // EMS return code for fail static constexpr uint8_t TX_WRITE_SUCCESS = 1; // EMS return code for success
static constexpr uint8_t TX_WRITE_SUCCESS = 1; // EMS return code for success
TxService() = default; TxService() = default;
~TxService() = default; ~TxService() = default;
void start(); void start();
void send(); void send();
void add(const uint8_t operation,
void add(const uint8_t operation, const uint8_t dest,
const uint8_t dest, const uint16_t type_id,
const uint16_t type_id, const uint8_t offset,
const uint8_t offset, uint8_t * message_data,
uint8_t * message_data, const uint8_t message_length,
const uint8_t message_length, const bool front = false);
const bool front = false); void add(const uint8_t operation, const uint8_t * data, const uint8_t length, const bool front = false);
void add(const uint8_t operation, const uint8_t * data, const uint8_t length, const bool front = false); void read_request(const uint16_t type_id, const uint8_t dest, const uint8_t offset = 0);
void send_raw(const char * telegram_data);
void read_request(const uint16_t type_id, const uint8_t dest, const uint8_t offset = 0); void send_poll();
void flush_tx_queue();
void send_raw(const char * telegram_data); 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;
void send_poll(); uint16_t post_send_query();
void flush_tx_queue();
void retry_tx(const uint8_t operation, const uint8_t * data, const uint8_t length);
uint8_t retry_count() const { uint8_t retry_count() const {
return retry_count_; return retry_count_;
@@ -282,8 +275,6 @@ class TxService : public EMSbus {
retry_count_ = 0; retry_count_ = 0;
} }
bool is_last_tx(const uint8_t src, const uint8_t dest) const;
void set_post_send_query(uint16_t type_id) { void set_post_send_query(uint16_t type_id) {
telegram_last_post_send_query_ = type_id; telegram_last_post_send_query_ = type_id;
} }
@@ -328,10 +319,6 @@ class TxService : public EMSbus {
telegram_write_count_++; telegram_write_count_++;
} }
void post_send_query();
void print_last_tx();
class QueuedTxTelegram { class QueuedTxTelegram {
public: public:
const uint16_t id_; const uint16_t id_;

View File

@@ -1 +1 @@
#define EMSESP_APP_VERSION "2.0.1b2" #define EMSESP_APP_VERSION "2.0.1b3"