ESPHome 2026.1.4
Loading...
Searching...
No Matches
vbus.cpp
Go to the documentation of this file.
1#include "vbus.h"
3#include "esphome/core/log.h"
4#include <cinttypes>
5
6namespace esphome {
7namespace vbus {
8
9static const char *const TAG = "vbus";
10
11// Maximum bytes to log in verbose hex output (16 frames * 4 bytes = 64 bytes typical)
12static constexpr size_t VBUS_MAX_LOG_BYTES = 64;
13
15 ESP_LOGCONFIG(TAG, "VBus:");
17}
18
19static void septet_spread(uint8_t *data, int start, int count, uint8_t septet) {
20 for (int i = 0; i < count; i++, septet >>= 1) {
21 if (septet & 1)
22 data[start + i] |= 0x80;
23 }
24}
25
26static bool checksum(const uint8_t *data, int start, int count) {
27 uint8_t csum = 0x7f;
28 for (int i = 0; i < count; i++)
29 csum = (csum - data[start + i]) & 0x7f;
30 return csum == 0;
31}
32
33void VBus::loop() {
34 if (!available())
35 return;
36
37 while (available()) {
38 uint8_t c;
39 read_byte(&c);
40
41 if (c == 0xaa) {
42 this->state_ = 1;
43 this->buffer_.clear();
44 continue;
45 }
46 if (c & 0x80) {
47 this->state_ = 0;
48 continue;
49 }
50 if (this->state_ == 0)
51 continue;
52
53 if (this->state_ == 1) {
54 this->buffer_.push_back(c);
55 if (this->buffer_.size() == 7) {
56 this->protocol_ = this->buffer_[4];
57 this->source_ = (this->buffer_[3] << 8) + this->buffer_[2];
58 this->dest_ = (this->buffer_[1] << 8) + this->buffer_[0];
59 this->command_ = (this->buffer_[6] << 8) + this->buffer_[5];
60 }
61 if ((this->protocol_ == 0x20) && (this->buffer_.size() == 15)) {
62 this->state_ = 0;
63 if (!checksum(this->buffer_.data(), 0, 15)) {
64 ESP_LOGE(TAG, "P2 checksum failed");
65 continue;
66 }
67 septet_spread(this->buffer_.data(), 7, 6, this->buffer_[13]);
68 uint16_t id = (this->buffer_[8] << 8) + this->buffer_[7];
69 uint32_t value =
70 (this->buffer_[12] << 24) + (this->buffer_[11] << 16) + (this->buffer_[10] << 8) + this->buffer_[9];
71 ESP_LOGV(TAG, "P1 C%04x %04x->%04x: %04x %04" PRIx32 " (%" PRIu32 ")", this->command_, this->source_,
72 this->dest_, id, value, value);
73 } else if ((this->protocol_ == 0x10) && (this->buffer_.size() == 9)) {
74 if (!checksum(this->buffer_.data(), 0, 9)) {
75 ESP_LOGE(TAG, "P1 checksum failed");
76 this->state_ = 0;
77 continue;
78 }
79 this->frames_ = this->buffer_[7];
80 if (this->frames_) {
81 this->state_ = 2;
82 this->cframe_ = 0;
83 this->fbcount_ = 0;
84 this->buffer_.clear();
85 } else {
86 this->state_ = 0;
87 ESP_LOGD(TAG, "P1 empty message");
88 }
89 }
90 continue;
91 }
92
93 if (this->state_ == 2) {
94 this->fbytes_[this->fbcount_++] = c;
95 if (this->fbcount_ < 6)
96 continue;
97 this->fbcount_ = 0;
98 if (!checksum(this->fbytes_, 0, 6)) {
99 ESP_LOGE(TAG, "frame checksum failed");
100 continue;
101 }
102 septet_spread(this->fbytes_, 0, 4, this->fbytes_[4]);
103 for (int i = 0; i < 4; i++)
104 this->buffer_.push_back(this->fbytes_[i]);
105 if (++this->cframe_ < this->frames_)
106 continue;
107#if ESPHOME_LOG_LEVEL >= ESPHOME_LOG_LEVEL_VERBOSE
108 char hex_buf[format_hex_size(VBUS_MAX_LOG_BYTES)];
109#endif
110 ESP_LOGV(TAG, "P2 C%04x %04x->%04x: %s", this->command_, this->source_, this->dest_,
111 format_hex_to(hex_buf, this->buffer_.data(), this->buffer_.size()));
112 for (auto &listener : this->listeners_)
113 listener->on_message(this->command_, this->source_, this->dest_, this->buffer_);
114 this->state_ = 0;
115 continue;
116 }
117 }
118}
119
120void VBusListener::on_message(uint16_t command, uint16_t source, uint16_t dest, std::vector<uint8_t> &message) {
121 if ((this->command_ != 0xffff) && (this->command_ != command))
122 return;
123 if ((this->source_ != 0xffff) && (this->source_ != source))
124 return;
125 if ((this->dest_ != 0xffff) && (this->dest_ != dest))
126 return;
127 this->handle_message(message);
128}
129
130} // namespace vbus
131} // namespace esphome
uint8_t checksum
Definition bl0906.h:3
void check_uart_settings(uint32_t baud_rate, uint8_t stop_bits=1, UARTParityOptions parity=UART_CONFIG_PARITY_NONE, uint8_t data_bits=8)
Check that the configuration of the UART bus matches the provided values and otherwise print a warnin...
Definition uart.cpp:12
bool read_byte(uint8_t *data)
Definition uart.h:34
uint8_t frames_
Definition vbus.h:43
uint16_t dest_
Definition vbus.h:41
uint8_t cframe_
Definition vbus.h:44
uint8_t fbytes_[6]
Definition vbus.h:45
uint16_t command_
Definition vbus.h:42
std::vector< VBusListener * > listeners_
Definition vbus.h:47
std::vector< uint8_t > buffer_
Definition vbus.h:38
void dump_config() override
Definition vbus.cpp:14
uint16_t source_
Definition vbus.h:40
void loop() override
Definition vbus.cpp:33
uint8_t protocol_
Definition vbus.h:39
void on_message(uint16_t command, uint16_t source, uint16_t dest, std::vector< uint8_t > &message)
Definition vbus.cpp:120
virtual void handle_message(std::vector< uint8_t > &message)=0
const char * message
Definition component.cpp:38
const char *const TAG
Definition spi.cpp:7
Providing packet encoding functions for exchanging data with a remote host.
Definition a01nyub.cpp:7
constexpr size_t format_hex_size(size_t byte_count)
Calculate buffer size needed for format_hex_to: "XXXXXXXX...\0" = bytes * 2 + 1.
Definition helpers.h:804
char * format_hex_to(char *buffer, size_t buffer_size, const uint8_t *data, size_t length)
Format byte array as lowercase hex to buffer (base implementation).
Definition helpers.cpp:322