ESPHome 2026.12.0-dev
Loading...
Searching...
No Matches
lzss_compression.cpp
Go to the documentation of this file.
1#include "lzss_compression.h"
2#ifdef USE_PACKET_INTERFACE_LZSS
4#include "esphome/core/log.h"
5
7
8ESPHOME_LOG_TAG(TAG, "packet_interface.lzss");
9
10LzssCompression::LzssCompression(PacketInterface *lower, size_t max_packet_size, const uint8_t *dictionary,
11 size_t dictionary_size, uint8_t check, uint8_t *buffers, uint16_t *chains)
12 : PacketInterface(max_packet_size != 0 ? max_packet_size : packet_size(lower->get_max_packet_size())),
13 check_(check & HEADER_CHECK_MASK),
14 lower_(lower),
15 codec_(dictionary, dictionary_size),
16 tx_input_(buffers),
17 chains_(chains) {
19}
20
22 size_t lower_size = this->lower_->get_max_packet_size();
23 if (!this->setup_check(this->max_packet_size_ != 0 && lower_size > HEADER_SIZE,
24 LOG_STR("max_packet_size too small for the configured layers")))
25 return;
26 if (!this->setup_check(this->tx_input_ != nullptr && this->chains_ != nullptr,
27 LOG_STR("Not enough memory for the packet buffers")))
28 return;
29 this->rx_output_ = this->tx_input_ + this->max_packet_size_;
30 this->tx_output_ = this->rx_output_ + this->max_packet_size_;
32 [this](const PacketBuffer &data, const PacketMetaData &meta_data) { this->receive_(data, meta_data); });
33}
34
36 ESP_LOGCONFIG(TAG,
37 "LZSS Compression on '%s':\n"
38 " Max Packet Size: %zu\n"
39 " Dictionary Size: %zu\n"
40 " Dictionary Check: 0x%02X",
41 this->name_, this->max_packet_size_, this->codec_.dictionary_size(), this->check_);
42}
43
45 return this->send_exclusive_([&] { return this->send_(data, meta_data); });
46}
47
48bool LzssCompression::send_(const PacketBuffer &data, const PacketMetaData &meta_data) {
49 if (this->tx_output_ == nullptr)
50 return false;
51 size_t size = data.size();
52 if (size > this->max_packet_size_) {
53 ESP_LOGW(TAG, "%s: Packet of %zu bytes is larger than %zu", this->name_, size, this->max_packet_size_);
54 return false;
55 }
56 const uint8_t *in = this->tx_input_;
57 if (data.is_contiguous()) {
58 in = data.as_span().data();
59 } else {
60 data.copy_to(this->tx_input_, size);
61 }
62 size_t room = this->lower_->get_max_packet_size() - HEADER_SIZE;
63 // Compressed only if that makes it smaller
64 size_t length = size > 1 ? this->codec_.compress(in, size, this->tx_output_ + HEADER_SIZE,
65 clamp_at_most(room, size - 1), this->chains_)
66 : 0;
67 if (length != 0) {
68 ESP_LOGV(TAG, "%s: Packet of %zu bytes compressed to %zu", this->name_, size, length);
69 this->tx_output_[0] = HEADER_COMPRESSED | this->check_;
70 return this->lower_->send_to_interface(PacketBuffer(this->tx_output_, length + HEADER_SIZE), meta_data);
71 }
72 if (size > room) {
73 ESP_LOGW(TAG, "%s: Packet of %zu bytes does not compress to %zu", this->name_, size, room);
74 return false;
75 }
76 uint8_t header = this->check_;
77 return this->lower_->send_to_interface(PacketBuffer(&header, HEADER_SIZE, &data, nullptr, 0), meta_data);
78}
79
80void LzssCompression::receive_(const PacketBuffer &data, const PacketMetaData &meta_data) {
81 // Lower layers deliver packets in one piece
82 if (!data.is_contiguous()) {
83 ESP_LOGW(TAG, "%s: Packet split across buffers is not supported", this->name_);
84 this->note_received_(false);
85 return;
86 }
87 auto packet = data.as_span();
88 if (packet.size() < HEADER_SIZE || (packet[0] & HEADER_CHECK_MASK) != this->check_) {
89 ESP_LOGV(TAG, "%s: Packet of %zu bytes with another dictionary dropped", this->name_, packet.size());
90 this->note_received_(false);
91 return;
92 }
93 const uint8_t *body = packet.data() + HEADER_SIZE;
94 size_t size = packet.size() - HEADER_SIZE;
95 if ((packet[0] & HEADER_COMPRESSED) == 0) {
96 if (size > this->max_packet_size_) {
97 ESP_LOGV(TAG, "%s: Packet of %zu bytes is larger than %zu", this->name_, size, this->max_packet_size_);
98 this->note_received_(false);
99 return;
100 }
101 this->note_received_(true);
102 this->on_receive_from_interface_(PacketBuffer(body, size), meta_data);
103 return;
104 }
105 size_t length = this->codec_.expand(body, size, this->rx_output_, this->max_packet_size_);
106 if (length == SIZE_MAX) {
107 ESP_LOGV(TAG, "%s: Malformed packet of %zu bytes dropped", this->name_, packet.size());
108 this->note_received_(false);
109 return;
110 }
111 this->note_received_(true);
112 this->on_receive_from_interface_(PacketBuffer(this->rx_output_, length), meta_data);
113}
114
115} // namespace esphome::packet_interface
116#endif
bool ESPHOME_ALWAYS_INLINE setup_check(bool ok)
Marks the component failed with message (default "Communication failed") unless ok; returns ok.
Definition component.h:236
size_t compress(const uint8_t *in, size_t size, uint8_t *out, size_t capacity, uint16_t *chains) const
Compress size bytes into out, which holds capacity bytes, using chains (CHAIN_WORDS words) as working...
Definition lzss.cpp:98
size_t dictionary_size() const
Definition lzss.h:40
size_t expand(const uint8_t *in, size_t size, uint8_t *out, size_t capacity) const
Expand size bytes into out, which holds capacity bytes; returns the length, or SIZE_MAX if the data i...
Definition lzss.cpp:170
static constexpr uint8_t HEADER_COMPRESSED
The bit flagging compressed data, and the dictionary check in the bits below it.
bool send_to_interface(const PacketBuffer &data, const PacketMetaData &meta_data) override
void receive_(const PacketBuffer &data, const PacketMetaData &meta_data)
bool send_(const PacketBuffer &data, const PacketMetaData &meta_data)
LzssCompression(PacketInterface *lower, size_t max_packet_size, const uint8_t *dictionary, size_t dictionary_size, uint8_t check, uint8_t *buffers, uint16_t *chains)
A buffer class that efficiently represents a composite buffer with three sections:
size_t size() const
Get the total size of the buffer.
bool is_contiguous() const
Check if all the data is held in the pre-buffer, so it can be read as one span.
A class that will transport data to and from an abstract channel.
size_t get_max_packet_size() const
The largest packet this layer accepts; fixed when the layer is constructed.
void note_received_(bool understood)
Called by a layer for each received packet it understood, or could not make sense of.
void stack_on_(PacketInterface *lower, Capability capability)
Called by a layer's constructor to record itself as the layer above lower, adding its own capability.
virtual bool send_to_interface(const PacketBuffer &data, const PacketMetaData &meta_data)=0
Method to be implemented by child classes to send data to the interface.
bool send_exclusive_(F &&send)
For a layer that sends from a buffer of its own: runs send, or returns false if a send through this l...
void add_packet_interface_listener(F &&callback)
Add a listener that will be called when data is received.
void on_receive_from_interface_(const PacketBuffer &data, const PacketMetaData &meta_data)
This function should be called by implementing classes with received data to be passed to any listene...
A struct to hold metadata about a packet.
ESPHOME_LOG_TAG(TAG, "packet_interface.addressing")
T clamp_at_most(T value, U max)
Definition helpers.h:2859
uint16_t size
Definition helpers.cpp:44