ESPHome 2026.11.0b6
Loading...
Searching...
No Matches
systa_bus.cpp
Go to the documentation of this file.
1#include "systa_bus.h"
2#include "esphome/core/log.h"
3
4#include <algorithm>
5#include <array>
6
7namespace esphome::systa_bus {
8
9ESPHOME_LOG_TAG(TAG, "systa_bus");
10
11void SystaBus::dump_config() { ESP_LOGCONFIG(TAG, "SystaBus:"); }
12
13static bool checksum(std::span<const uint8_t> data) { return sum_bytes(data.data(), data.size()) == 0; }
14
16 this->read_available([this](uint8_t c) { this->handle_byte_(c); });
17}
18
19void SystaBus::handle_byte_(uint8_t c) {
20 if (this->buffer_.empty()) {
21 if (c == START_BYTE)
22 this->buffer_.push_back(c);
23 return;
24 }
25 this->buffer_.push_back(c);
26 if (this->buffer_.size() == 2) {
27 // The length byte is only trusted for known message types; anything else restarts the search
28 uint16_t message_type = get_message_type(this->buffer_);
29 if (message_type != MESSAGE_TYPE_AQUA_SENSOR_DATA) {
30 ESP_LOGV(TAG, "Unknown message type 0x%04x", message_type);
31 this->buffer_.clear();
32 // A stray start byte followed by a real frame: keep this byte as the new start
33 if (c == START_BYTE)
34 this->buffer_.push_back(c);
35 }
36 return;
37 }
38 if (this->buffer_.size() < this->buffer_[1] + FRAME_OVERHEAD)
39 return;
40 if (!checksum(this->buffer_)) {
41 ESP_LOGW(TAG, "Checksum failed");
42 this->resync_();
43 return;
44 }
45#ifdef SYSTA_BUS_LISTENER_COUNT
46 for (auto *listener : this->listeners_)
47 listener->handle_message(this->buffer_);
48#endif
49 this->buffer_.clear();
50}
51
52// A frame that lost a byte swallows the start of the next one, so the failed bytes are fed back through the
53// parser from the second byte on. They are fewer than a full frame, so this cannot fail the checksum again.
55 std::array<uint8_t, MAX_MESSAGE_SIZE> failed;
56 const size_t count = this->buffer_.size();
57 std::copy(this->buffer_.begin(), this->buffer_.end(), failed.begin());
58 this->buffer_.clear();
59 for (size_t i = 1; i < count; i++)
60 this->handle_byte_(failed[i]);
61}
62
63} // namespace esphome::systa_bus
uint8_t checksum
Definition bl0906.h:3
size_t size() const
Definition helpers.h:373
void push_back(const T &value)
Definition helpers.h:343
bool empty() const
Definition helpers.h:375
void dump_config() override
Definition systa_bus.cpp:11
void handle_byte_(uint8_t c)
Definition systa_bus.cpp:19
StaticVector< uint8_t, MAX_MESSAGE_SIZE > buffer_
Definition systa_bus.h:48
StaticVector< SystaBusListener *, SYSTA_BUS_LISTENER_COUNT > listeners_
Definition systa_bus.h:46
bool read_available(F &&on_byte)
Same as read_available_chunks(), calling on_byte(uint8_t) once per byte.
Definition uart.h:77
unsigned count
Definition gif.cpp:51
uint16_t get_message_type(std::span< const uint8_t > message)
Definition systa_bus.h:24
ESPHOME_LOG_TAG(TAG, "systa_bus.sensor")
constexpr uint8_t sum_bytes(const uint8_t *data, size_t len, uint8_t sum=0)
Add up the bytes of data with size len, truncated to 8 bits: the checksum many serial and IR protocol...
Definition helpers.h:939