ESPHome 2026.5.0b1
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 <algorithm>
5#include <cinttypes>
6
7namespace esphome::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 = encode_uint32(this->buffer_[12], this->buffer_[11], this->buffer_[10], this->buffer_[9]);
70 ESP_LOGV(TAG, "P1 C%04x %04x->%04x: %04x %04" PRIx32 " (%" PRIu32 ")", this->command_, this->source_,
71 this->dest_, id, value, value);
72 } else if ((this->protocol_ == 0x10) && (this->buffer_.size() == 9)) {
73 if (!checksum(this->buffer_.data(), 0, 9)) {
74 ESP_LOGE(TAG, "P1 checksum failed");
75 this->state_ = 0;
76 continue;
77 }
78 this->frames_ = this->buffer_[7];
79 if (this->frames_) {
80 this->state_ = 2;
81 this->cframe_ = 0;
82 this->fbcount_ = 0;
83 this->buffer_.clear();
84 } else {
85 this->state_ = 0;
86 ESP_LOGD(TAG, "P1 empty message");
87 }
88 } else if (this->buffer_.size() > 15) {
89 ESP_LOGW(TAG, "Unknown protocol 0x%02x, discarding", this->protocol_);
90 this->state_ = 0;
91 }
92 continue;
93 }
94
95 if (this->state_ == 2) {
96 this->fbytes_[this->fbcount_++] = c;
97 if (this->fbcount_ < 6)
98 continue;
99 this->fbcount_ = 0;
100 if (!checksum(this->fbytes_, 0, 6)) {
101 ESP_LOGE(TAG, "frame checksum failed");
102 continue;
103 }
104 septet_spread(this->fbytes_, 0, 4, this->fbytes_[4]);
105 for (int i = 0; i < 4; i++)
106 this->buffer_.push_back(this->fbytes_[i]);
107 if (++this->cframe_ < this->frames_)
108 continue;
109#if ESPHOME_LOG_LEVEL >= ESPHOME_LOG_LEVEL_VERBOSE
110 char hex_buf[format_hex_size(VBUS_MAX_LOG_BYTES)];
111 size_t log_bytes = std::min(this->buffer_.size(), static_cast<size_t>(VBUS_MAX_LOG_BYTES));
112#endif
113 ESP_LOGV(TAG, "P2 C%04x %04x->%04x: %s", this->command_, this->source_, this->dest_,
114 format_hex_to(hex_buf, this->buffer_.data(), log_bytes));
115 for (auto &listener : this->listeners_)
116 listener->on_message(this->command_, this->source_, this->dest_, this->buffer_);
117 this->state_ = 0;
118 continue;
119 }
120 }
121}
122
123void VBusListener::on_message(uint16_t command, uint16_t source, uint16_t dest, std::vector<uint8_t> &message) {
124 if ((this->command_ != 0xffff) && (this->command_ != command))
125 return;
126 if ((this->source_ != 0xffff) && (this->source_ != source))
127 return;
128 if ((this->dest_ != 0xffff) && (this->dest_ != dest))
129 return;
130 this->handle_message(message);
131}
132
133} // namespace esphome::vbus
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:16
bool read_byte(uint8_t *data)
Definition uart.h:34
uint8_t frames_
Definition vbus.h:42
uint16_t dest_
Definition vbus.h:40
uint8_t cframe_
Definition vbus.h:43
uint8_t fbytes_[6]
Definition vbus.h:44
uint16_t command_
Definition vbus.h:41
std::vector< VBusListener * > listeners_
Definition vbus.h:46
std::vector< uint8_t > buffer_
Definition vbus.h:37
void dump_config() override
Definition vbus.cpp:14
uint16_t source_
Definition vbus.h:39
void loop() override
Definition vbus.cpp:33
uint8_t protocol_
Definition vbus.h:38
void on_message(uint16_t command, uint16_t source, uint16_t dest, std::vector< uint8_t > &message)
Definition vbus.cpp:123
virtual void handle_message(std::vector< uint8_t > &message)=0
const char * message
Definition component.cpp:35
const char *const TAG
Definition spi.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:1360
constexpr uint32_t encode_uint32(uint8_t byte1, uint8_t byte2, uint8_t byte3, uint8_t byte4)
Encode a 32-bit value given four bytes in most to least significant byte order.
Definition helpers.h:867
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:335
static void uint32_t