2#ifdef USE_PACKET_INTERFACE_BASIC_FRAGMENTATION
12static constexpr uint8_t LAST_FRAGMENT = 0x80;
13static constexpr uint8_t INDEX_MASK = 0x7F;
22 if (fragment_size == 0)
24 return std::max<size_t>(1, (
size + fragment_size - 1) / fragment_size);
29 this->
mark_failed(LOG_STR(
"max_packet_size too small for the configured layers"));
33 this->
mark_failed(LOG_STR(
"max_packet_size needs more than 128 fragments"));
39 if (!this->
setup_check(this->
tx_buffer_ !=
nullptr, LOG_STR(
"Not enough memory for the packet buffers")))
52 "Basic Fragmentation on '%s':\n"
53 " Max Packet Size: %zu\n"
54 " Fragment Size: %zu\n"
55 " Fragments in the Largest Packet: %zu",
58 ESP_LOGW(TAG,
" The layer below has room for too few fragments for the largest packets, which cannot be sent");
72 ESP_LOGW(TAG,
"%s: Packet of %zu bytes is larger than %zu", this->
name_, size, this->
max_packet_size_);
77 ESP_LOGV(TAG,
"%s: No room for %zu fragments", this->
name_, fragments);
82 header[1] = LAST_FRAGMENT;
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);
92 ESP_LOGV(TAG,
"%s: Fragment 0 of %zu not accepted", this->
name_, fragments);
95 ESP_LOGW(TAG,
"%s: Packet cut short, fragment %zu of %zu not accepted", this->
name_, index, fragments);
105 if (!data.is_contiguous()) {
106 ESP_LOGW(TAG,
"%s: Packet split across buffers is not supported", this->
name_);
109 auto fragment = data.as_span();
111 ESP_LOGV(TAG,
"%s: Fragment of %zu bytes is shorter than its header, dropped", this->
name_, fragment.size());
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;
119 if (index == 0 && last) {
122 ESP_LOGV(TAG,
"%s: Incomplete packet %u dropped", this->
name_, this->
rx_number_);
126 ESP_LOGV(TAG,
"%s: Packet %u is larger than %zu, dropped", this->
name_, number, this->
max_packet_size_);
134 ESP_LOGV(TAG,
"%s: Incomplete packet %u dropped", this->
name_, this->
rx_number_);
143 ESP_LOGV(TAG,
"%s: Fragment %u from 0x%04X dropped", this->
name_, index, meta_data.
source_address);
146 ESP_LOGV(TAG,
"%s: Fragment %u of packet %u dropped", this->
name_, index, number);
151 ESP_LOGV(TAG,
"%s: Packet %u is larger than %zu, dropped", this->
name_, number, this->
max_packet_size_);
void mark_failed()
Mark this component as failed.
bool ESPHOME_ALWAYS_INLINE setup_check(bool ok)
Marks the component failed with message (default "Communication failed") unless ok; returns ok.
void receive_(const PacketBuffer &data, const PacketMetaData &meta_data)
size_t fragments_for_(size_t size) const
void dump_config() override
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.
size_t fragment_size_() const
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.
const size_t max_packet_size_
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...
ESPHOME_LOG_TAG(TAG, "packet_interface.addressing")
@ CAPABILITY_FRAGMENTATION
uint32_t random_uint32()
Return a random 32-bit unsigned integer.