ESPHome 2026.11.0-dev
Loading...
Searching...
No Matches
udp_packet_interface.cpp
Go to the documentation of this file.
2#ifdef USE_NETWORK
4#include "esphome/core/log.h"
5
6namespace esphome::udp {
7
10
11ESPHOME_LOG_TAG(TAG, "udp.packet_interface");
12
14 this->parent_->add_listener([this](std::span<const uint8_t> data) { this->receive_(data); });
15}
16
17void UdpPacketInterface::receive_(std::span<const uint8_t> data) {
18 if (data.size() > this->max_packet_size_) {
19 ESP_LOGV(TAG, "%s: Packet of %zu bytes is larger than %zu, dropped", this->name_, data.size(),
20 this->max_packet_size_);
21 return;
22 }
23 this->on_receive_from_interface_(PacketBuffer(data.data(), data.size()), {});
24}
25
27 size_t size = data.size();
28 if (size > this->max_packet_size_) {
29 ESP_LOGW(TAG, "%s: Packet of %zu bytes is larger than %zu", this->name_, size, this->max_packet_size_);
30 return false;
31 }
33 return false;
34 if (data.is_contiguous()) {
35 auto span = data.as_span();
36 return this->parent_->send_packet(span.data(), span.size());
37 }
38 data.copy_to(this->tx_buffer_.data(), size);
39 return this->parent_->send_packet(this->tx_buffer_.data(), size);
40}
41
42} // namespace esphome::udp
43#endif
A buffer class that efficiently represents a composite buffer with three sections:
bool is_contiguous() const
Check if all the data is held in the pre-buffer, so it can be read as one span.
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.
void add_listener(F &&listener)
bool send_packet(const uint8_t *data, size_t size)
Returns false if the packet could not be sent to every destination.
void receive_(std::span< const uint8_t > data)
const std::span< uint8_t > tx_buffer_
bool send_to_interface(const packet_interface::PacketBuffer &data, const packet_interface::PacketMetaData &meta_data) override
ESPHOME_ALWAYS_INLINE bool is_connected()
Return whether the node is connected to the network (through wifi, eth, ...)
Definition util.h:36
ESPHOME_LOG_TAG(TAG, "udp.packet_interface")
uint16_t size
Definition helpers.cpp:34