fix most clang formatting issues

This commit is contained in:
Guido Schreuder 2024-02-19 16:21:20 +01:00
parent e1a8f60ddb
commit 55183c0383
8 changed files with 200 additions and 207 deletions

View file

@ -1,27 +1,11 @@
#include "ebus.h"
#include <utility>
namespace esphome {
namespace ebus {
Ebus::Ebus(ebus_config_t &config) {
this->primary_address_ = config.primary_address;
this->max_tries_ = config.max_tries;
this->max_lock_counter_ = config.max_lock_counter;
}
void Ebus::set_uart_send_function(std::function<void(const char *, int16_t)> uart_send) {
this->uart_send_ = uart_send;
}
void Ebus::set_queue_received_telegram_function(std::function<void(Telegram &telegram)> queue_received_telegram) {
this->queue_received_telegram_ = queue_received_telegram;
}
void Ebus::set_dequeue_command_function(std::function<bool(void *const)> dequeue_command) {
this->dequeue_command_ = dequeue_command;
}
uint8_t Ebus::uart_send_char(uint8_t cr, bool esc, bool run_crc, uint8_t crc_init) {
uint8_t Ebus::uart_send_char_(uint8_t cr, bool esc, bool run_crc, uint8_t crc_init) {
char buffer[2];
uint8_t crc = 0;
uint8_t len = 1;
@ -47,27 +31,27 @@ uint8_t Ebus::uart_send_char(uint8_t cr, bool esc, bool run_crc, uint8_t crc_ini
return Elf::crc8_calc(buffer[1], crc);
}
void Ebus::uart_send_char(uint8_t cr, bool esc) { this->uart_send_char(cr, esc, false, 0); }
void Ebus::uart_send_char_(uint8_t cr, bool esc) { this->uart_send_char_(cr, esc, false, 0); }
void Ebus::uart_send_remaining_request_part(SendCommand &command) {
this->uart_send_char(command.getZZ());
this->uart_send_char(command.getPB());
this->uart_send_char(command.getSB());
this->uart_send_char(command.getNN());
for (int i = 0; i < command.getNN(); i++) {
this->uart_send_char((uint8_t) command.get_request_byte(i));
void Ebus::uart_send_remaining_request_part_(SendCommand &command) {
this->uart_send_char_(command.get_zz());
this->uart_send_char_(command.get_pb());
this->uart_send_char_(command.get_sb());
this->uart_send_char_(command.get_nn());
for (int i = 0; i < command.get_nn(); i++) {
this->uart_send_char_((uint8_t) command.get_request_byte(i));
}
this->uart_send_char(command.get_crc());
this->uart_send_char_(command.get_crc());
}
void Ebus::process_received_char(unsigned char received_byte) {
// keep track of number of character between last 2 SYN chars
// this is needed in case of arbitration
if (received_byte == SYN) {
this->state = this->char_count_since_last_syn_ == 1 ? EbusState::arbitration : EbusState::normal;
this->state_ = this->char_count_since_last_syn_ == 1 ? EbusState::ARBITRATION : EbusState::NORMAL;
this->char_count_since_last_syn_ = 0;
if (this->lock_counter_ > 0 && this->state == EbusState::normal) {
if (this->lock_counter_ > 0 && this->state_ == EbusState::NORMAL) {
this->lock_counter_--;
}
@ -103,7 +87,7 @@ void Ebus::process_received_char(unsigned char received_byte) {
break;
case TelegramState::waitForRequestData:
if (received_byte == SYN) {
if (this->receiving_telegram_.getZZ() == ESC) {
if (this->receiving_telegram_.get_zz() == ESC) {
this->receiving_telegram_.set_state(TelegramState::endArbitration);
} else {
this->receiving_telegram_.set_state(TelegramState::endErrorUnexpectedSyn);
@ -159,22 +143,22 @@ void Ebus::process_received_char(unsigned char received_byte) {
switch (this->active_command_.get_state()) {
case TelegramState::waitForSend:
if (received_byte == SYN && state == EbusState::normal && this->lock_counter_ == 0) {
if (received_byte == SYN && state_ == EbusState::NORMAL && this->lock_counter_ == 0) {
this->active_command_.set_state(TelegramState::waitForArbitration);
this->uart_send_char(this->active_command_.getQQ());
this->uart_send_char_(this->active_command_.get_qq());
}
break;
case TelegramState::waitForArbitration:
if (received_byte == this->active_command_.getQQ()) {
if (received_byte == this->active_command_.get_qq()) {
// we won arbitration
this->uart_send_remaining_request_part(this->active_command_);
this->uart_send_remaining_request_part_(this->active_command_);
if (this->active_command_.is_ack_expected()) {
this->active_command_.set_state(TelegramState::waitForCommandAck);
} else {
this->active_command_.set_state(TelegramState::endCompleted);
this->lock_counter_ = this->max_lock_counter_;
}
} else if (Elf::get_priority_class(received_byte) == Elf::get_priority_class(this->active_command_.getQQ())) {
} else if (Elf::get_priority_class(received_byte) == Elf::get_priority_class(this->active_command_.get_qq())) {
// eligible for round 2
this->active_command_.set_state(TelegramState::waitForArbitration2nd);
} else {
@ -186,10 +170,10 @@ void Ebus::process_received_char(unsigned char received_byte) {
break;
case TelegramState::waitForArbitration2nd:
if (received_byte == SYN) {
this->uart_send_char(this->active_command_.getQQ());
} else if (received_byte == this->active_command_.getQQ()) {
this->uart_send_char_(this->active_command_.get_qq());
} else if (received_byte == this->active_command_.get_qq()) {
// won round 2
this->uart_send_remaining_request_part(this->active_command_);
this->uart_send_remaining_request_part_(this->active_command_);
if (this->active_command_.is_ack_expected()) {
this->active_command_.set_state(TelegramState::waitForCommandAck);
} else {
@ -220,30 +204,30 @@ void Ebus::process_received_char(unsigned char received_byte) {
// responses to our commands are stored in receiving_telegram_
// when response is completed send ACK or NACK when we were the primary
if (this->receiving_telegram_.get_state() == TelegramState::waitForResponseAck &&
this->receiving_telegram_.getQQ() == this->primary_address_) {
this->receiving_telegram_.get_qq() == this->primary_address_) {
if (this->receiving_telegram_.is_response_valid()) {
this->uart_send_char(ACK);
this->uart_send_char(SYN, false);
this->uart_send_char_(ACK);
this->uart_send_char_(SYN, false);
} else {
this->uart_send_char(NACK);
this->uart_send_char_(NACK);
}
}
// Handle our responses
this->handle_response(this->receiving_telegram_);
this->handle_response_(this->receiving_telegram_);
}
void Ebus::add_send_response_handler(std::function<uint8_t(Telegram &, uint8_t *)> send_response_handler) {
void Ebus::add_send_response_handler(const std::function<uint8_t(Telegram &, uint8_t *)>& send_response_handler) {
send_response_handlers_.push_back(send_response_handler);
}
void Ebus::handle_response(Telegram &telegram) {
void Ebus::handle_response_(Telegram &telegram) {
if (telegram.get_state() != TelegramState::waitForRequestAck ||
telegram.getZZ() != Elf::to_secondary(this->primary_address_)) {
telegram.get_zz() != Elf::to_secondary(this->primary_address_)) {
return;
}
if (!telegram.is_request_valid()) {
uart_send_char(NACK);
uart_send_char_(NACK);
return;
}
@ -261,17 +245,17 @@ void Ebus::handle_response(Telegram &telegram) {
// we found no reponse to send
if (len == 0) {
uart_send_char(NACK);
uart_send_char_(NACK);
return;
}
uart_send_char(ACK);
uart_send_char_(ACK);
uint8_t crc = Elf::crc8_calc(len, 0);
uart_send_char(len);
uart_send_char_(len);
for (int i = 0; i < len; i++) {
crc = uart_send_char(buf[i], true, true, crc);
crc = uart_send_char_(buf[i], true, true, crc);
}
uart_send_char(crc);
uart_send_char_(crc);
}
unsigned char Elf::crc8_calc(unsigned char data, unsigned char crc_init) {

View file

@ -1,6 +1,6 @@
#pragma once
#include <stdint.h>
#include <cstdint>
#include <functional>
#include <list>
@ -9,12 +9,6 @@
namespace esphome {
namespace ebus {
typedef struct {
uint8_t primary_address;
uint8_t max_tries;
uint8_t max_lock_counter;
} ebus_config_t;
class Elf {
public:
static unsigned char crc8_calc(unsigned char data, unsigned char crc_init);
@ -28,12 +22,27 @@ class Elf {
class Ebus {
public:
explicit Ebus(ebus_config_t &config);
void set_uart_send_function(std::function<void(const char *, int16_t)> uart_send);
void set_queue_received_telegram_function(std::function<void(Telegram &telegram)> queue_received_telegram);
void set_dequeue_command_function(std::function<bool(void *const)> dequeue_command);
void process_received_char(unsigned char receivedByte);
void add_send_response_handler(std::function<uint8_t(Telegram &, uint8_t *)>);
void set_primary_address(uint8_t primary_address) {
this->primary_address_ = primary_address;
}
void set_max_tries(uint8_t max_tries) {
this->max_tries_ = max_tries;
}
void set_max_lock_counter(uint8_t max_lock_counter) {
this->max_lock_counter_ = max_lock_counter;
}
void set_uart_send_function(std::function<void(const char *, int16_t)> uart_send) {
this->uart_send_ = std::move(uart_send);
}
void set_queue_received_telegram_function(std::function<void(Telegram &telegram)> queue_received_telegram) {
this->queue_received_telegram_ = std::move(queue_received_telegram);
}
void set_dequeue_command_function(const std::function<bool(void *const)>& dequeue_command) {
this->dequeue_command_ = dequeue_command;
}
void process_received_char(unsigned char received_byte);
void add_send_response_handler(const std::function<uint8_t(Telegram &, uint8_t *)>& send_response_handler);
protected:
uint8_t primary_address_;
@ -41,7 +50,7 @@ class Ebus {
uint8_t max_lock_counter_;
uint8_t lock_counter_ = 0;
uint8_t char_count_since_last_syn_ = 0;
EbusState state = EbusState::arbitration;
EbusState state_ = EbusState::ARBITRATION;
Telegram receiving_telegram_;
SendCommand active_command_;
std::list<std::function<uint8_t(Telegram &, uint8_t *)>> send_response_handlers_;
@ -49,10 +58,10 @@ class Ebus {
std::function<void(const char *, int16_t)> uart_send_;
std::function<void(Telegram &)> queue_received_telegram_;
std::function<bool(void *const &)> dequeue_command_;
uint8_t uart_send_char(uint8_t cr, bool esc, bool run_crc, uint8_t crc_init);
void uart_send_char(uint8_t cr, bool esc = true);
void uart_send_remaining_request_part(SendCommand &command);
void handle_response(Telegram &telegram);
uint8_t uart_send_char_(uint8_t cr, bool esc, bool run_crc, uint8_t crc_init);
void uart_send_char_(uint8_t cr, bool esc = true);
void uart_send_remaining_request_part_(SendCommand &command);
void handle_response_(Telegram &telegram);
};
} // namespace ebus

View file

@ -19,10 +19,10 @@ void EbusComponent::dump_config() {
}
void EbusComponent::setup() {
this->setup_queues();
this->setup_ebus();
this->setup_uart();
this->setup_tasks();
this->setup_queues_();
this->setup_ebus_();
this->setup_uart_();
this->setup_tasks_();
}
void EbusComponent::set_primary_address(uint8_t primary_address) { this->primary_address_ = primary_address; }
@ -44,34 +44,32 @@ void EbusComponent::add_sender(EbusSender *sender) {
}
void EbusComponent::add_receiver(EbusReceiver *receiver) { this->receivers_.push_back(receiver); }
void EbusComponent::setup_queues() {
void EbusComponent::setup_queues_() {
this->history_queue_ = xQueueCreate(this->history_queue_size_, sizeof(Telegram));
this->command_queue_ = xQueueCreate(this->command_queue_size_, sizeof(Telegram));
}
void EbusComponent::setup_ebus() {
ebus_config_t ebus_config = ebus_config_t{
.primary_address = this->primary_address_,
.max_tries = this->max_tries_,
.max_lock_counter = this->max_lock_counter_,
};
this->ebus = new Ebus(ebus_config);
void EbusComponent::setup_ebus_() {
this->ebus_ = std::unique_ptr<Ebus>(new Ebus());
this->ebus_->set_primary_address(this->primary_address_);
this->ebus_->set_max_tries(this->max_tries_);
this->ebus_->set_max_lock_counter(this->max_lock_counter_);
this->ebus->set_uart_send_function(
this->ebus_->set_uart_send_function(
[&](const char *buffer, int16_t length) { return uart_write_bytes(this->uart_num_, buffer, length); });
this->ebus->set_queue_received_telegram_function([&](Telegram &telegram) {
BaseType_t xHigherPriorityTaskWoken;
xHigherPriorityTaskWoken = pdFALSE;
xQueueSendToBackFromISR(this->history_queue_, &telegram, &xHigherPriorityTaskWoken);
if (xHigherPriorityTaskWoken) {
this->ebus_->set_queue_received_telegram_function([&](Telegram &telegram) {
BaseType_t x_higher_priority_task_woken;
x_higher_priority_task_woken = pdFALSE;
xQueueSendToBackFromISR(this->history_queue_, &telegram, &x_higher_priority_task_woken);
if (x_higher_priority_task_woken) {
portYIELD_FROM_ISR();
}
});
this->ebus->set_dequeue_command_function([&](void *const command) {
BaseType_t xHigherPriorityTaskWoken = pdFALSE;
if (xQueueReceiveFromISR(this->command_queue_, command, &xHigherPriorityTaskWoken)) {
if (xHigherPriorityTaskWoken) {
this->ebus_->set_dequeue_command_function([&](void *const command) {
BaseType_t x_higher_priority_task_woken = pdFALSE;
if (xQueueReceiveFromISR(this->command_queue_, command, &x_higher_priority_task_woken)) {
if (x_higher_priority_task_woken) {
portYIELD_FROM_ISR();
}
return true;
@ -80,7 +78,7 @@ void EbusComponent::setup_ebus() {
});
}
void EbusComponent::setup_uart() {
void EbusComponent::setup_uart_() {
portMUX_TYPE mux = portMUX_INITIALIZER_UNLOCKED;
portENTER_CRITICAL(&mux);
@ -99,36 +97,36 @@ void EbusComponent::setup_uart() {
ESP_ERROR_CHECK(
uart_set_pin(this->uart_num_, this->uart_tx_pin_, this->uart_rx_pin_, UART_PIN_NO_CHANGE, UART_PIN_NO_CHANGE));
ESP_ERROR_CHECK(uart_driver_install(this->uart_num_, 256, 0, 0, NULL, 0));
ESP_ERROR_CHECK(uart_driver_install(this->uart_num_, 256, 0, 0, nullptr, 0));
portEXIT_CRITICAL(&mux);
}
void EbusComponent::setup_tasks() {
xTaskCreate(&process_received_bytes, "ebus_process_received_bytes", 2048, (void *) this, 10, NULL);
xTaskCreate(&process_received_messages, "ebus_process_received_messages", 2560, (void *) this, 5, NULL);
void EbusComponent::setup_tasks_() {
xTaskCreate(&process_received_bytes, "ebus_process_received_bytes", 2048, (void *) this, 10, nullptr);
xTaskCreate(&process_received_messages, "ebus_process_received_messages", 2560, (void *) this, 5, nullptr);
}
void EbusComponent::process_received_bytes(void *pvParameter) {
EbusComponent *instance = static_cast<EbusComponent *>(pvParameter);
void EbusComponent::process_received_bytes(void *pv_parameter) {
EbusComponent *instance = static_cast<EbusComponent *>(pv_parameter);
while (1) {
uint8_t receivedByte;
int len = uart_read_bytes(instance->uart_num_, &receivedByte, 1, 20 / portTICK_PERIOD_MS);
while (true) {
uint8_t received_byte;
int len = uart_read_bytes(instance->uart_num_, &received_byte, 1, 20 / portTICK_PERIOD_MS);
if (len) {
instance->ebus->process_received_char(receivedByte);
instance->ebus_->process_received_char(received_byte);
taskYIELD();
}
}
}
void EbusComponent::process_received_messages(void *pvParameter) {
EbusComponent *instance = static_cast<EbusComponent *>(pvParameter);
void EbusComponent::process_received_messages(void *pv_parameter) {
EbusComponent *instance = static_cast<EbusComponent *>(pv_parameter);
Telegram telegram;
while (1) {
while (true) {
if (xQueueReceive(instance->history_queue_, &telegram, pdMS_TO_TICKS(1000))) {
instance->handle_message(telegram);
instance->handle_message_(telegram);
// TODO: this comment is kept as reference on how to debug stack overflows. Could be generalized.
// ESP_LOGD(TAG, "Task: %s, Stack Highwater Mark: %d", pcTaskGetTaskName(NULL),
// uxTaskGetStackHighWaterMark(NULL));
@ -137,10 +135,10 @@ void EbusComponent::process_received_messages(void *pvParameter) {
}
}
void EbusComponent::handle_message(Telegram &telegram) {
void EbusComponent::handle_message_(Telegram &telegram) {
if (telegram.get_state() != TelegramState::endCompleted) {
ESP_LOGD(TAG, "Message received with invalid state: %s, QQ:%02X, ZZ:%02X, Command:%02X%02X",
telegram.get_state_string(), telegram.getQQ(), telegram.getZZ(), telegram.getPB(), telegram.getSB());
telegram.get_state_string(), telegram.get_qq(), telegram.get_zz(), telegram.get_pb(), telegram.get_sb());
return;
}

View file

@ -1,5 +1,7 @@
#pragma once
#include <memory>
#include "esphome/core/log.h"
#include "esphome/core/component.h"
@ -33,19 +35,19 @@ class EbusComponent : public PollingComponent {
void dump_config() override;
void setup() override;
void set_primary_address(uint8_t);
void set_max_tries(uint8_t);
void set_max_lock_counter(uint8_t);
void set_uart_num(uint8_t);
void set_uart_tx_pin(uint8_t);
void set_uart_rx_pin(uint8_t);
void set_history_queue_size(uint8_t);
void set_command_queue_size(uint8_t);
void set_primary_address(uint8_t /*primary_address*/);
void set_max_tries(uint8_t /*max_tries*/);
void set_max_lock_counter(uint8_t /*max_lock_counter*/);
void set_uart_num(uint8_t /*uart_num*/);
void set_uart_tx_pin(uint8_t /*uart_tx_pin*/);
void set_uart_rx_pin(uint8_t /*uart_rx_pin*/);
void set_history_queue_size(uint8_t /*history_queue_size*/);
void set_command_queue_size(uint8_t /*command_queue_size*/);
void add_sender(EbusSender *);
void add_receiver(EbusReceiver *);
void add_sender(EbusSender * /*sender*/);
void add_receiver(EbusReceiver * /*receiver*/);
void update();
void update() override;
protected:
uint8_t primary_address_;
@ -63,16 +65,16 @@ class EbusComponent : public PollingComponent {
std::list<EbusSender *> senders_;
std::list<EbusReceiver *> receivers_;
Ebus *ebus;
std::unique_ptr<Ebus> ebus_;
void setup_queues();
void setup_ebus();
void setup_uart();
void setup_tasks();
void setup_queues_();
void setup_ebus_();
void setup_uart_();
void setup_tasks_();
static void process_received_bytes(void *);
static void process_received_messages(void *);
void handle_message(Telegram &);
static void process_received_bytes(void * /*pvParameter*/);
static void process_received_messages(void * /*pvParameter*/);
void handle_message_(Telegram & /*telegram*/);
};
} // namespace ebus

View file

@ -2,7 +2,7 @@
#include "ebus_sensor.h"
// TODO: remove
#define GET_BYTE(CMD, I) ((uint8_t) ((CMD >> 8 * I) & 0XFF))
#define GET_BYTE(CMD, I) ((uint8_t) (((CMD) >> 8 * (I)) & 0XFF))
namespace esphome {
namespace ebus {
@ -62,10 +62,10 @@ float EbusSensor::to_float(Telegram &telegram, uint8_t start, uint8_t length, fl
}
bool EbusSensor::is_mine(Telegram &telegram) {
if (this->source_ != SYN && this->source_ != telegram.getZZ()) {
if (this->source_ != SYN && this->source_ != telegram.get_zz()) {
return false;
}
if (telegram.getCommand() != this->command_) {
if (telegram.get_command() != this->command_) {
return false;
}
for (int i = 0; i < this->payload_.size(); i++) {

View file

@ -12,17 +12,17 @@ class EbusSensor : public EbusReceiver, public EbusSender, public sensor::Sensor
void dump_config() override;
void set_primary_address(uint8_t) override;
void set_source(uint8_t);
void set_destination(uint8_t);
void set_command(uint16_t);
void set_payload(const std::vector<uint8_t> &);
void set_primary_address(uint8_t /*primary_address*/) override;
void set_source(uint8_t /*source*/);
void set_destination(uint8_t /*destination*/);
void set_command(uint16_t /*command*/);
void set_payload(const std::vector<uint8_t> & /*payload*/);
void set_response_read_position(uint8_t);
void set_response_read_bytes(uint8_t);
void set_response_read_divider(float);
void set_response_read_position(uint8_t /*response_position*/);
void set_response_read_bytes(uint8_t /*response_bytes*/);
void set_response_read_divider(float /*response_divider*/);
void process_received(Telegram) override;
void process_received(Telegram /*telegram*/) override;
optional<SendCommand> prepare_command() override;
// TODO: refactor these

View file

@ -5,15 +5,15 @@ namespace ebus {
TelegramBase::TelegramBase() {}
void TelegramBase::set_state(TelegramState new_state) { this->state = new_state; }
void TelegramBase::set_state(TelegramState new_state) { this->state_ = new_state; }
TelegramState TelegramBase::get_state() { return this->state; }
TelegramState TelegramBase::get_state() { return this->state_; }
#define X(name, int) \
case int: \
return "" #name "";
const char *TelegramBase::get_state_string() {
switch ((int8_t) this->state) {
switch ((int8_t) this->state_) {
TELEGRAM_STATE_TABLE
default:
return "[INVALID STATE]";
@ -21,7 +21,7 @@ const char *TelegramBase::get_state_string() {
}
#undef X
void TelegramBase::push_buffer(uint8_t cr, uint8_t *buffer, uint8_t *pos, uint8_t *crc, int max_pos) {
void TelegramBase::push_buffer_(uint8_t cr, uint8_t *buffer, uint8_t *pos, uint8_t *crc, int max_pos) {
if (*pos < max_pos) {
*crc = Elf::crc8_calc(cr, *crc);
}
@ -35,90 +35,90 @@ void TelegramBase::push_buffer(uint8_t cr, uint8_t *buffer, uint8_t *pos, uint8_
}
TelegramType TelegramBase::get_type() {
if (this->getZZ() == ESC) {
return TelegramType::Unknown;
if (this->get_zz() == ESC) {
return TelegramType::UNKNOWN;
}
if (this->getZZ() == BROADCAST_ADDRESS) {
return TelegramType::Broadcast;
if (this->get_zz() == BROADCAST_ADDRESS) {
return TelegramType::BROADCAST;
}
if (Elf::is_primary(this->getZZ())) {
return TelegramType::PrimaryPrimary;
if (Elf::is_primary(this->get_zz())) {
return TelegramType::PRIMARY_PRIMARY;
}
return TelegramType::PrimarySecondary;
return TelegramType::PRIMARY_SECONDARY;
}
int16_t TelegramBase::get_request_byte(uint8_t pos) {
if (pos > this->getNN() || pos >= MAX_DATA_LENGTH) {
if (pos > this->get_nn() || pos >= MAX_DATA_LENGTH) {
return -1;
}
return this->request_buffer[OFFSET_DATA + pos];
return this->request_buffer_[OFFSET_DATA + pos];
}
uint8_t TelegramBase::get_request_crc() { return this->request_buffer[OFFSET_DATA + this->getNN()]; }
uint8_t TelegramBase::get_request_crc() { return this->request_buffer_[OFFSET_DATA + this->get_nn()]; }
void TelegramBase::push_req_data(uint8_t cr) {
this->push_buffer(cr, request_buffer, &request_buffer_pos, &request_rolling_crc, OFFSET_DATA + getNN());
this->push_buffer_(cr, request_buffer_, &request_buffer_pos_, &request_rolling_crc_, OFFSET_DATA + get_nn());
}
bool TelegramBase::is_ack_expected() { return (this->get_type() != TelegramType::Broadcast); }
bool TelegramBase::is_ack_expected() { return (this->get_type() != TelegramType::BROADCAST); }
bool TelegramBase::is_response_expected() { return (this->get_type() == TelegramType::PrimarySecondary); }
bool TelegramBase::is_response_expected() { return (this->get_type() == TelegramType::PRIMARY_SECONDARY); }
bool TelegramBase::is_finished() { return this->state < TelegramState::unknown; }
bool TelegramBase::is_finished() { return this->state_ < TelegramState::unknown; }
Telegram::Telegram() { this->state = TelegramState::waitForSyn; }
Telegram::Telegram() { this->state_ = TelegramState::waitForSyn; }
int16_t Telegram::get_response_byte(uint8_t pos) {
if (pos > this->getResponseNN() || pos >= MAX_DATA_LENGTH) {
if (pos > this->get_response_nn() || pos >= MAX_DATA_LENGTH) {
return INVALID_RESPONSE_BYTE;
}
return this->response_buffer[RESPONSE_OFFSET + pos];
return this->response_buffer_[RESPONSE_OFFSET + pos];
}
uint8_t Telegram::get_response_crc() { return this->response_buffer[RESPONSE_OFFSET + this->getResponseNN()]; }
uint8_t Telegram::get_response_crc() { return this->response_buffer_[RESPONSE_OFFSET + this->get_response_nn()]; }
void Telegram::push_response_data(uint8_t cr) {
this->push_buffer(cr, response_buffer, &response_buffer_pos, &response_rolling_crc,
RESPONSE_OFFSET + getResponseNN());
this->push_buffer_(cr, response_buffer_, &response_buffer_pos_, &response_rolling_crc_,
RESPONSE_OFFSET + get_response_nn());
}
bool Telegram::is_response_complete() {
return (this->state > TelegramState::waitForSyn || this->state == TelegramState::endCompleted) &&
(this->response_buffer_pos > RESPONSE_OFFSET) &&
(this->response_buffer_pos == (RESPONSE_OFFSET + this->getResponseNN() + 1)) && !this->wait_for_escaped_char_;
return (this->state_ > TelegramState::waitForSyn || this->state_ == TelegramState::endCompleted) &&
(this->response_buffer_pos_ > RESPONSE_OFFSET) &&
(this->response_buffer_pos_ == (RESPONSE_OFFSET + this->get_response_nn() + 1)) && !this->wait_for_escaped_char_;
}
bool Telegram::is_response_valid() {
return this->is_response_complete() && this->get_response_crc() == this->response_rolling_crc;
return this->is_response_complete() && this->get_response_crc() == this->response_rolling_crc_;
}
bool Telegram::is_request_complete() {
return (this->state > TelegramState::waitForSyn || this->state == TelegramState::endCompleted) &&
(this->request_buffer_pos > OFFSET_DATA) && (this->request_buffer_pos == (OFFSET_DATA + this->getNN() + 1)) &&
return (this->state_ > TelegramState::waitForSyn || this->state_ == TelegramState::endCompleted) &&
(this->request_buffer_pos_ > OFFSET_DATA) && (this->request_buffer_pos_ == (OFFSET_DATA + this->get_nn() + 1)) &&
!this->wait_for_escaped_char_;
}
bool Telegram::is_request_valid() {
return this->is_request_complete() && this->get_request_crc() == this->request_rolling_crc;
return this->is_request_complete() && this->get_request_crc() == this->request_rolling_crc_;
}
SendCommand::SendCommand() { this->state = TelegramState::endCompleted; }
SendCommand::SendCommand() { this->state_ = TelegramState::endCompleted; }
SendCommand::SendCommand(uint8_t QQ, uint8_t ZZ, uint8_t PB, uint8_t SB, uint8_t NN, uint8_t *data) {
this->state = TelegramState::waitForSend;
this->push_req_data(QQ);
this->push_req_data(ZZ);
this->push_req_data(PB);
this->push_req_data(SB);
this->push_req_data(NN);
for (int i = 0; i < NN; i++) {
SendCommand::SendCommand(uint8_t qq, uint8_t zz, uint8_t pb, uint8_t sb, uint8_t nn, uint8_t *data) {
this->state_ = TelegramState::waitForSend;
this->push_req_data(qq);
this->push_req_data(zz);
this->push_req_data(pb);
this->push_req_data(sb);
this->push_req_data(nn);
for (int i = 0; i < nn; i++) {
this->push_req_data(data[i]);
}
this->push_req_data(this->request_rolling_crc);
this->push_req_data(this->request_rolling_crc_);
}
bool SendCommand::can_retry(int8_t max_tries) { return this->tries_count_++ < max_tries; }
uint8_t SendCommand::get_crc() { return this->request_rolling_crc; }
uint8_t SendCommand::get_crc() { return this->request_rolling_crc_; }
} // namespace ebus
} // namespace esphome

View file

@ -4,15 +4,15 @@ namespace esphome {
namespace ebus {
enum EbusState : int8_t {
normal,
arbitration,
NORMAL,
ARBITRATION,
};
enum TelegramType : int8_t {
Unknown = -1,
Broadcast = 0,
PrimaryPrimary = 1,
PrimarySecondary = 2,
UNKNOWN = -1,
BROADCAST = 0,
PRIMARY_PRIMARY = 1,
PRIMARY_SECONDARY = 2,
};
#define TELEGRAM_STATE_TABLE \
@ -67,13 +67,13 @@ class TelegramBase {
public:
TelegramBase();
uint8_t getQQ() { return this->request_buffer[OFFSET_QQ]; }
uint8_t getZZ() { return this->request_buffer[OFFSET_ZZ]; }
uint8_t getPB() { return this->request_buffer[OFFSET_PB]; }
uint8_t getSB() { return this->request_buffer[OFFSET_SB]; }
uint16_t getCommand() { return ((uint16_t) getPB()) << 8 | getSB(); }
uint8_t getNN() {
uint8_t nn = this->request_buffer[OFFSET_NN];
uint8_t get_qq() { return this->request_buffer_[OFFSET_QQ]; }
uint8_t get_zz() { return this->request_buffer_[OFFSET_ZZ]; }
uint8_t get_pb() { return this->request_buffer_[OFFSET_PB]; }
uint8_t get_sb() { return this->request_buffer_[OFFSET_SB]; }
uint16_t get_command() { return ((uint16_t) get_pb()) << 8 | get_sb(); }
uint8_t get_nn() {
uint8_t nn = this->request_buffer_[OFFSET_NN];
if (nn >= MAX_DATA_LENGTH) {
return 0;
}
@ -93,21 +93,21 @@ class TelegramBase {
bool is_finished();
protected:
TelegramState state;
uint8_t request_buffer[REQUEST_BUFFER_SIZE] = {
TelegramState state_;
uint8_t request_buffer_[REQUEST_BUFFER_SIZE] = {
ESC, ESC}; // initialize QQ and ZZ with ESC char to distinguish from valid primary 0
uint8_t request_buffer_pos = 0;
uint8_t request_rolling_crc = 0;
uint8_t request_buffer_pos_ = 0;
uint8_t request_rolling_crc_ = 0;
bool wait_for_escaped_char_ = false;
void push_buffer(uint8_t cr, uint8_t *buffer, uint8_t *pos, uint8_t *crc, int max_pos);
void push_buffer_(uint8_t cr, uint8_t *buffer, uint8_t *pos, uint8_t *crc, int max_pos);
};
class Telegram : public TelegramBase {
public:
Telegram();
uint8_t getResponseNN() {
uint8_t nn = response_buffer[0];
uint8_t get_response_nn() {
uint8_t nn = response_buffer_[0];
if (nn >= MAX_DATA_LENGTH) {
return 0;
}
@ -124,15 +124,15 @@ class Telegram : public TelegramBase {
bool is_request_valid();
protected:
uint8_t response_buffer[RESPONSE_BUFFER_SIZE] = {0};
uint8_t response_buffer_pos = 0;
uint8_t response_rolling_crc = 0;
uint8_t response_buffer_[RESPONSE_BUFFER_SIZE] = {0};
uint8_t response_buffer_pos_ = 0;
uint8_t response_rolling_crc_ = 0;
};
class SendCommand : public TelegramBase {
public:
SendCommand();
SendCommand(uint8_t QQ, uint8_t ZZ, uint8_t PB, uint8_t SB, uint8_t NN, uint8_t *data);
SendCommand(uint8_t qq, uint8_t zz, uint8_t pb, uint8_t sb, uint8_t nn, uint8_t *data);
bool can_retry(int8_t max_tries);
uint8_t get_crc();