ESPHome 2026.12.0-dev
Loading...
Searching...
No Matches
lzss_compression.h
Go to the documentation of this file.
1#pragma once
2
4#ifdef USE_PACKET_INTERFACE_LZSS
5
7#include "packet_interface.h"
8
10
21class LzssCompression final : public PacketInterface {
22 public:
24 static constexpr uint8_t HEADER_COMPRESSED = 0x80;
25 static constexpr uint8_t HEADER_CHECK_MASK = 0x7F;
26 static constexpr size_t HEADER_SIZE = 1;
28 static constexpr size_t packet_size(size_t lower_size) { return remaining_packet_size(lower_size, HEADER_SIZE); }
29 static constexpr size_t CHAIN_WORDS = lzss::Lzss::CHAIN_WORDS;
31 static constexpr size_t buffer_size(size_t lower_size, size_t max_packet_size) {
32 return 2 * (max_packet_size != 0 ? max_packet_size : packet_size(lower_size)) + lower_size;
33 }
34
42 LzssCompression(PacketInterface *lower, size_t max_packet_size, const uint8_t *dictionary, size_t dictionary_size,
43 uint8_t check, uint8_t *buffers, uint16_t *chains);
44
45 void setup() override;
46 void dump_config() override;
47 bool send_to_interface(const PacketBuffer &data, const PacketMetaData &meta_data) override;
48 // One compressed packet per packet, so a fragmentation layer above sees the room below
49 size_t get_send_capacity() const override { return this->lower_->get_send_capacity(); }
50
51 protected:
52 void receive_(const PacketBuffer &data, const PacketMetaData &meta_data);
53 bool send_(const PacketBuffer &data, const PacketMetaData &meta_data);
54
55 // First, so it fills the spare byte at the end of PacketInterface
56 const uint8_t check_;
59 uint8_t *tx_input_;
60 uint8_t *tx_output_{nullptr}; // set once setup() succeeds
61 uint8_t *rx_output_{nullptr};
62 uint16_t *chains_;
63};
64
65} // namespace esphome::packet_interface
66#endif
LZSS compression of a block of data on its own, with a preset dictionary: text that both ends hold,...
Definition lzss.h:21
static constexpr size_t CHAIN_WORDS
The working space compress() needs: the latest position for each hash, then the previous one for each...
Definition lzss.h:34
A compression layer: each packet is compressed on its own with LZSS, so a lost packet costs nothing m...
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
static constexpr size_t packet_size(size_t lower_size)
The largest packet the layer takes by default, over a layer below whose largest packet is lower_size.
void receive_(const PacketBuffer &data, const PacketMetaData &meta_data)
static constexpr size_t buffer_size(size_t lower_size, size_t max_packet_size)
The packet to compress, the compressed packet, and an expanded packet.
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:
A class that will transport data to and from an abstract channel.
virtual size_t get_send_capacity() const
How many packets send_to_interface() would accept now, so a layer above can tell before sending a ser...
A struct to hold metadata about a packet.
constexpr size_t remaining_packet_size(size_t size, size_t used)
The room left for a packet of size bytes after used bytes, or 0 if it is too small.