ESPHome 2026.12.0-dev
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
12 ESP_LOGCONFIG(TAG, "SystaBus:");
13#ifdef USE_SYSTA_BUS_AQUA_PUBLISHER
14 if (this->dump_publisher_ != nullptr) {
15 ESP_LOGCONFIG(TAG, "SystaSolar Aqua:");
16 this->dump_publisher_(TAG);
17 }
18#endif
19}
20
21static bool checksum(std::span<const uint8_t> data) { return sum_bytes(data.data(), data.size()) == 0; }
22
24 this->read_available([this](uint8_t c) { this->handle_byte_(c); });
25}
26
27void SystaBus::handle_byte_(uint8_t c) {
28 if (this->buffer_.empty()) {
29 if (c == START_BYTE)
30 this->buffer_.push_back(c);
31 return;
32 }
33 this->buffer_.push_back(c);
34 if (this->buffer_.size() == 2) {
35 // The length byte is only trusted for known message types; anything else restarts the search
36 uint16_t message_type = get_message_type(this->buffer_);
37 if (message_type != MESSAGE_TYPE_AQUA_SENSOR_DATA) {
38 ESP_LOGV(TAG, "Unknown message type 0x%04x", message_type);
39 this->buffer_.clear();
40 // A stray start byte followed by a real frame: keep this byte as the new start
41 if (c == START_BYTE)
42 this->buffer_.push_back(c);
43 }
44 return;
45 }
46 if (this->buffer_.size() < this->buffer_[1] + FRAME_OVERHEAD)
47 return;
48 if (!checksum(this->buffer_)) {
49 ESP_LOGW(TAG, "Checksum failed");
50 this->resync_();
51 return;
52 }
53#ifdef USE_SYSTA_BUS_AQUA_PUBLISHER
54 if (this->aqua_publisher_ != nullptr && get_message_type(this->buffer_) == MESSAGE_TYPE_AQUA_SENSOR_DATA)
55 this->aqua_publisher_(this->buffer_);
56#endif
57 this->buffer_.clear();
58}
59
60// A frame that lost a byte swallows the start of the next one, so the failed bytes are fed back through the
61// parser from the second byte on. They are fewer than a full frame, so this cannot fail the checksum again.
63 std::array<uint8_t, MAX_MESSAGE_SIZE> failed;
64 const size_t count = this->buffer_.size();
65 std::copy(this->buffer_.begin(), this->buffer_.end(), failed.begin());
66 this->buffer_.clear();
67 for (size_t i = 1; i < count; i++)
68 this->handle_byte_(failed[i]);
69}
70
71} // namespace esphome::systa_bus
uint8_t checksum
Definition bl0906.h:3
size_t size() const
Definition helpers.h:404
void push_back(const T &value)
Definition helpers.h:374
bool empty() const
Definition helpers.h:406
void dump_config() override
Definition systa_bus.cpp:11
void handle_byte_(uint8_t c)
Definition systa_bus.cpp:27
StaticVector< uint8_t, MAX_MESSAGE_SIZE > buffer_
Definition systa_bus.h:45
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")
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:1086