ESPHome 2026.12.0-dev
Loading...
Searching...
No Matches
basic_fragmentation.cpp
Go to the documentation of this file.
2#ifdef USE_PACKET_INTERFACE_BASIC_FRAGMENTATION
3#include <algorithm>
4#include <cstring>
6#include "esphome/core/log.h"
7
9
10ESPHOME_LOG_TAG(TAG, "packet_interface.fragmentation");
11
12static constexpr uint8_t LAST_FRAGMENT = 0x80;
13static constexpr uint8_t INDEX_MASK = 0x7F;
14
15BasicFragmentation::BasicFragmentation(PacketInterface *lower, size_t max_packet_size, uint8_t *buffer)
16 : PacketInterface(max_packet_size), lower_(lower), tx_buffer_(buffer) {
18}
19
21 size_t fragment_size = this->fragment_size_();
22 if (fragment_size == 0)
23 return SIZE_MAX;
24 return std::max<size_t>(1, (size + fragment_size - 1) / fragment_size);
25}
26
28 if (this->fragment_size_() == 0) {
29 this->mark_failed(LOG_STR("max_packet_size too small for the configured layers"));
30 return;
31 }
33 this->mark_failed(LOG_STR("max_packet_size needs more than 128 fragments"));
34 return;
35 }
36 // Checked now, while the layer below holds nothing; a failed layer below reports its own failure instead
37 this->queue_too_short_ =
38 !this->lower_->is_failed() && this->fragments_for_(this->max_packet_size_) > this->lower_->get_send_capacity();
39 if (!this->setup_check(this->tx_buffer_ != nullptr, LOG_STR("Not enough memory for the packet buffers")))
40 return;
41 this->rx_buffer_ = this->tx_buffer_ + this->fragment_size_();
42 // Numbering from a random start makes it unlikely that a packet joins fragments from before a restart
43 this->tx_number_ = random_uint32();
45 [this](const PacketBuffer &data, const PacketMetaData &meta_data) { this->receive_(data, meta_data); });
46}
47
49 // A link with no room for fragments fails setup, which core reports below
50 size_t fragments = this->fragment_size_() == 0 ? 0 : this->fragments_for_(this->max_packet_size_);
51 ESP_LOGCONFIG(TAG,
52 "Basic Fragmentation on '%s':\n"
53 " Max Packet Size: %zu\n"
54 " Fragment Size: %zu\n"
55 " Fragments in the Largest Packet: %zu",
56 this->name_, this->max_packet_size_, this->fragment_size_(), fragments);
57 if (this->queue_too_short_) {
58 ESP_LOGW(TAG, " The layer below has room for too few fragments for the largest packets, which cannot be sent");
59 }
60}
61
63 return this->send_exclusive_([&] { return this->send_(data, meta_data); });
64}
65
66bool BasicFragmentation::send_(const PacketBuffer &data, const PacketMetaData &meta_data) {
67 // Set only once setup() succeeds
68 if (this->rx_buffer_ == nullptr)
69 return false;
70 size_t size = data.size();
71 if (size > this->max_packet_size_) {
72 ESP_LOGW(TAG, "%s: Packet of %zu bytes is larger than %zu", this->name_, size, this->max_packet_size_);
73 return false;
74 }
75 size_t fragments = this->fragments_for_(size);
76 if (fragments > this->lower_->get_send_capacity()) {
77 ESP_LOGV(TAG, "%s: No room for %zu fragments", this->name_, fragments);
78 return false;
79 }
80 uint8_t header[HEADER_SIZE] = {this->tx_number_++, 0};
81 if (fragments == 1) {
82 header[1] = LAST_FRAGMENT;
83 return this->lower_->send_to_interface(PacketBuffer(header, HEADER_SIZE, &data, nullptr, 0), meta_data);
84 }
85 size_t fragment_size = this->fragment_size_();
86 for (size_t index = 0; index != fragments; index++) {
87 size_t length = data.copy_to(this->tx_buffer_, fragment_size, index * fragment_size);
88 header[1] = index | (index + 1 == fragments ? LAST_FRAGMENT : 0);
89 PacketBuffer body(this->tx_buffer_, length);
90 if (!this->lower_->send_to_interface(PacketBuffer(header, HEADER_SIZE, &body, nullptr, 0), meta_data)) {
91 if (index == 0) {
92 ESP_LOGV(TAG, "%s: Fragment 0 of %zu not accepted", this->name_, fragments);
93 } else {
94 // The peer drops the part already sent when the next packet starts
95 ESP_LOGW(TAG, "%s: Packet cut short, fragment %zu of %zu not accepted", this->name_, index, fragments);
96 }
97 return false;
98 }
99 }
100 return true;
101}
102
103void BasicFragmentation::receive_(const PacketBuffer &data, const PacketMetaData &meta_data) {
104 // Lower layers deliver packets in one piece; a packet in one fragment is passed up without copying
105 if (!data.is_contiguous()) {
106 ESP_LOGW(TAG, "%s: Packet split across buffers is not supported", this->name_);
107 return;
108 }
109 auto fragment = data.as_span();
110 if (fragment.size() < HEADER_SIZE) {
111 ESP_LOGV(TAG, "%s: Fragment of %zu bytes is shorter than its header, dropped", this->name_, fragment.size());
112 return;
113 }
114 uint8_t number = fragment[0];
115 uint8_t index = fragment[1] & INDEX_MASK;
116 bool last = (fragment[1] & LAST_FRAGMENT) != 0;
117 const uint8_t *payload = fragment.data() + HEADER_SIZE;
118 size_t length = fragment.size() - HEADER_SIZE;
119 if (index == 0 && last) {
120 // A packet in one fragment leaves a packet being joined from another device intact
121 if (this->rx_joining_ && meta_data.source_address == this->rx_source_) {
122 ESP_LOGV(TAG, "%s: Incomplete packet %u dropped", this->name_, this->rx_number_);
123 this->rx_joining_ = false;
124 }
125 if (length > this->max_packet_size_) {
126 ESP_LOGV(TAG, "%s: Packet %u is larger than %zu, dropped", this->name_, number, this->max_packet_size_);
127 return;
128 }
129 this->on_receive_from_interface_(PacketBuffer(payload, length), meta_data);
130 return;
131 }
132 if (index == 0) {
133 if (this->rx_joining_) {
134 ESP_LOGV(TAG, "%s: Incomplete packet %u dropped", this->name_, this->rx_number_);
135 }
136 this->rx_joining_ = true;
137 this->rx_number_ = number;
138 this->rx_source_ = meta_data.source_address;
139 this->rx_next_index_ = 0;
140 this->rx_length_ = 0;
141 } else if (this->rx_joining_ && meta_data.source_address != this->rx_source_) {
142 // Only this fragment is lost, so of two devices sending at once, the one that started last still gets through
143 ESP_LOGV(TAG, "%s: Fragment %u from 0x%04X dropped", this->name_, index, meta_data.source_address);
144 return;
145 } else if (!this->rx_joining_ || number != this->rx_number_ || index != this->rx_next_index_) {
146 ESP_LOGV(TAG, "%s: Fragment %u of packet %u dropped", this->name_, index, number);
147 this->rx_joining_ = false;
148 return;
149 }
150 if (this->rx_length_ + length > this->max_packet_size_) {
151 ESP_LOGV(TAG, "%s: Packet %u is larger than %zu, dropped", this->name_, number, this->max_packet_size_);
152 this->rx_joining_ = false;
153 return;
154 }
155 memcpy(this->rx_buffer_ + this->rx_length_, payload, length);
156 this->rx_length_ = static_cast<uint16_t>(this->rx_length_ + length);
157 this->rx_next_index_++;
158 if (last) {
159 this->rx_joining_ = false;
160 this->on_receive_from_interface_(PacketBuffer(this->rx_buffer_, this->rx_length_), meta_data);
161 }
162}
163
164} // namespace esphome::packet_interface
165#endif
void mark_failed()
Mark this component as failed.
bool is_failed() const
Definition component.h:287
bool ESPHOME_ALWAYS_INLINE setup_check(bool ok)
Marks the component failed with message (default "Communication failed") unless ok; returns ok.
Definition component.h:235
void receive_(const PacketBuffer &data, const PacketMetaData &meta_data)
bool send_to_interface(const PacketBuffer &data, const PacketMetaData &meta_data) override
static constexpr size_t MAX_FRAGMENTS
The index has 7 bits.
BasicFragmentation(PacketInterface *lower, size_t max_packet_size, uint8_t *buffer)
static constexpr size_t HEADER_SIZE
Packet number, then index and last flag.
bool send_(const PacketBuffer &data, const PacketMetaData &meta_data)
A buffer class that efficiently represents a composite buffer with three sections:
size_t size() const
Get the total size of the buffer.
A class that will transport data to and from an abstract channel.
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.
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...
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.
uint32_t buffer
Definition gif.cpp:50
ESPHOME_LOG_TAG(TAG, "packet_interface.addressing")
uint16_t size
Definition helpers.cpp:44
uint32_t random_uint32()
Return a random 32-bit unsigned integer.
Definition helpers.cpp:10