ESPHome 2026.11.0b6
Loading...
Searching...
No Matches
udp_component.h
Go to the documentation of this file.
1#pragma once
2
4#ifdef USE_NETWORK
7#include <array>
8#include <initializer_list>
9#include <span>
10#include <vector>
11
12namespace esphome::udp {
13
14static const size_t MAX_PACKET_SIZE = 508;
15class UDPComponent final : public Component {
16 public:
18 void set_addresses(const char *const *addresses) { this->addresses_ = addresses; }
20 void set_listen_address(const char *listen_addr) { this->listen_address_ = listen_addr; }
21 void set_listen_port(uint16_t port) { this->listen_port_ = port; }
22 void set_broadcast_port(uint16_t port) { this->broadcast_port_ = port; }
23 void set_should_broadcast() { this->should_broadcast_ = true; }
24 void set_should_listen() { this->should_listen_ = true; }
25#ifdef USE_UDP_IPV6_SEND
27 void set_ipv6_send(bool multicast) {
28 this->ipv6_send_ = true;
29 this->ipv6_multicast_ = multicast;
30 }
32 void set_ipv4_send(bool ipv4_send) { this->ipv4_send_ = ipv4_send; }
33#endif
34#ifdef USE_UDP_OMR_SOURCE
38#endif
39 template<typename F> void add_listener(F &&listener) { this->packet_listeners_.add(std::forward<F>(listener)); }
40 void setup() override;
41 void loop() override;
42 void dump_config() override;
44 bool send_packet(const uint8_t *data, size_t size);
45 bool send_packet(const std::vector<uint8_t> &buf) { return this->send_packet(buf.data(), buf.size()); }
46 float get_setup_priority() const override { return setup_priority::AFTER_WIFI; };
47
48 protected:
49 enum PollStep : uint8_t {
54 POLL_SEND_HELD = 16, // only in poll_failed_: the held IPv6 sends were logged
55 };
56
59#ifdef USE_UDP_IPV6_SEND
60 sockaddr_in6 in6; // sizes the storage for IPv6 destinations
61#endif
62 };
63
65 bool network_poll_();
67 void set_poll_warning_(PollStep step, const LogString *what, int err);
68
69 LazyCallbackManager<void(std::span<const uint8_t>)> packet_listeners_{};
70 std::unique_ptr<socket::UDPSendSocket> broadcast_socket_ = nullptr;
71 std::unique_ptr<socket::UDPSocket> listen_socket_ = nullptr;
72#ifdef USE_UDP_IPV6_SEND
73 std::unique_ptr<socket::UDPSendSocket> send_socket_v6_ = nullptr;
74#endif
76 const char *const *addresses_{nullptr};
77 const char *listen_address_{nullptr};
78#ifdef USE_SOCKET_IPV6
80#endif
81 uint16_t listen_port_{};
82 uint16_t broadcast_port_{};
85 uint8_t poll_pending_{}; // PollStep bits
86 uint8_t poll_failed_{}; // PollStep bits already logged
88#ifdef USE_UDP_IPV6_SEND
89 bool ipv4_send_{true};
90 bool ipv6_send_{};
92#endif
93};
94
95} // namespace esphome::udp
96#endif
Fixed-capacity vector - sized once through init() or try_init(); push_back never reallocates This avo...
Definition helpers.h:669
void set_ipv4_send(bool ipv4_send)
An instance whose destinations are all IPv6 needs no IPv4 send socket.
float get_setup_priority() const override
std::unique_ptr< socket::UDPSendSocket > send_socket_v6_
FixedVector< SockaddrEntry > sockaddrs_
LazyCallbackManager< void(std::span< const uint8_t >)> packet_listeners_
void set_ipv6_send(bool multicast)
This instance has IPv6 destinations; multicast ones also need an egress interface.
bool network_poll_()
One pass of the network poll; true once nothing is left to do.
void set_listen_address(const char *listen_addr)
Multicast group literal; must outlive the component.
std::unique_ptr< socket::UDPSendSocket > broadcast_socket_
void set_poll_warning_(PollStep step, const LogString *what, int err)
err is the errno of the failed call, or 0 when no call failed.
void set_broadcast_port(uint16_t port)
void set_omr_source()
A destination is off the Thread mesh, so the IPv6 send socket must be bound to the OMR address first.
void add_listener(F &&listener)
const char *const * addresses_
std::unique_ptr< socket::UDPSocket > listen_socket_
void set_addresses(const char *const *addresses)
nullptr-terminated table of addresses; must outlive the component.
void set_listen_port(uint16_t port)
bool send_packet(const std::vector< uint8_t > &buf)
bool send_packet(const uint8_t *data, size_t size)
Returns false if the packet could not be sent to every destination.
constexpr float AFTER_WIFI
For components that should be initialized after WiFi is connected.
Definition component.h:55
uint16_t size
Definition helpers.cpp:34
static void uint32_t
sockaddr_in6 in6
sockaddr sa