ESPHome 2026.10.0-dev
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
14void VBus::dump_config() { ESP_LOGCONFIG(TAG, "VBus:"); }
15
16static void septet_spread(uint8_t *data, int start, int count, uint8_t septet) {
17 for (int i = 0; i < count; i++, septet >>= 1) {
18 if (septet & 1)
19 data[start + i] |= 0x80;
20 }
21}
22
23static bool checksum(const uint8_t *data, int start, int count) {
24 uint8_t csum = 0x7f;
25 for (int i = 0; i < count; i++)
26 csum = (csum - data[start + i]) & 0x7f;
27 return csum == 0;
28}
29
30void VBus::loop() {
31 if (!available())
32 return;
33
34 while (available()) {
35 uint8_t c;
36 read_byte(&c);
37
38 if (c == 0xaa) {
39 this->state_ = 1;
40 this->buffer_.clear();
41 continue;
42 }
43 if (c & 0x80) {
44 this->state_ = 0;
45 continue;
46 }
47 if (this->state_ == 0)
48 continue;
49
50 if (this->state_ == 1) {
51 this->buffer_.push_back(c);
52 if (this->buffer_.size() == 7) {
53 this->protocol_ = this->buffer_[4];
54 this->source_ = (this->buffer_[3] << 8) + this->buffer_[2];
55 this->dest_ = (this->buffer_[1] << 8) + this->buffer_[0];
56 this->command_ = (this->buffer_[6] << 8) + this->buffer_[5];
57 }
58 if ((this->protocol_ == 0x20) && (this->buffer_.size() == 15)) {
59 this->state_ = 0;
60 if (!checksum(this->buffer_.data(), 0, 15)) {
61 ESP_LOGE(TAG, "P2 checksum failed");
62 continue;
63 }
64 septet_spread(this->buffer_.data(), 7, 6, this->buffer_[13]);
65 uint16_t id = (this->buffer_[8] << 8) + this->buffer_[7];
66 uint32_t value = encode_uint32(this->buffer_[12], this->buffer_[11], this->buffer_[10], this->buffer_[9]);
67 ESP_LOGV(TAG, "P1 C%04x %04x->%04x: %04x %04" PRIx32 " (%" PRIu32 ")", this->command_, this->source_,
68 this->dest_, id, value, value);
69 } else if ((this->protocol_ == 0x10) && (this->buffer_.size() == 9)) {
70 if (!checksum(this->buffer_.data(), 0, 9)) {
71 ESP_LOGE(TAG, "P1 checksum failed");
72 this->state_ = 0;
73 continue;
74 }
75 this->frames_ = this->buffer_[7];
76 if (this->frames_) {
77 this->state_ = 2;
78 this->cframe_ = 0;
79 this->fbcount_ = 0;
80 this->buffer_.clear();
81 } else {
82 this->state_ = 0;
83 ESP_LOGD(TAG, "P1 empty message");
84 }
85 } else if (this->buffer_.size() > 15) {
86 ESP_LOGW(TAG, "Unknown protocol 0x%02x, discarding", this->protocol_);
87 this->state_ = 0;
88 }
89 continue;
90 }
91
92 if (this->state_ == 2) {
93 this->fbytes_[this->fbcount_++] = c;
94 if (this->fbcount_ < 6)
95 continue;
96 this->fbcount_ = 0;
97 if (!checksum(this->fbytes_, 0, 6)) {
98 ESP_LOGE(TAG, "frame checksum failed");
99 continue;
100 }
101 septet_spread(this->fbytes_, 0, 4, this->fbytes_[4]);
102 for (int i = 0; i < 4; i++)
103 this->buffer_.push_back(this->fbytes_[i]);
104 if (++this->cframe_ < this->frames_)
105 continue;
106#if ESPHOME_LOG_LEVEL >= ESPHOME_LOG_LEVEL_VERBOSE
107 char hex_buf[format_hex_size(VBUS_MAX_LOG_BYTES)];
108 size_t log_bytes = std::min(this->buffer_.size(), static_cast<size_t>(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(), log_bytes));
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 esphome::vbus
uint8_t checksum
Definition bl0906.h:3
bool read_byte(uint8_t *data)
Definition uart.h:35
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:30
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:120
virtual void handle_message(std::vector< uint8_t > &message)=0
const LogString * 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:1412
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:892
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:353
static void uint32_t