ESPHome 2026.11.0-dev
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 template<typename F> void add_listener(F &&listener) { this->packet_listeners_.add(std::forward<F>(listener)); }
35 void setup() override;
36 void loop() override;
37 void dump_config() override;
39 bool send_packet(const uint8_t *data, size_t size);
40 bool send_packet(const std::vector<uint8_t> &buf) { return this->send_packet(buf.data(), buf.size()); }
41 float get_setup_priority() const override { return setup_priority::AFTER_WIFI; };
42
43 protected:
44 enum PollStep : uint8_t { POLL_JOIN = 1, POLL_BIND = 2, POLL_MCAST_IF = 4 };
45
48#ifdef USE_UDP_IPV6_SEND
49 sockaddr_in6 in6; // sizes the storage for IPv6 destinations
50#endif
51 };
52
54 bool network_poll_();
55 void set_poll_warning_(PollStep step, const LogString *what);
56
57 LazyCallbackManager<void(std::span<const uint8_t>)> packet_listeners_{};
58 std::unique_ptr<socket::UDPSendSocket> broadcast_socket_ = nullptr;
59 std::unique_ptr<socket::UDPSocket> listen_socket_ = nullptr;
60#ifdef USE_UDP_IPV6_SEND
61 std::unique_ptr<socket::UDPSendSocket> send_socket_v6_ = nullptr;
62#endif
64 const char *const *addresses_{nullptr};
65 const char *listen_address_{nullptr};
66#ifdef USE_SOCKET_IPV6
68#endif
69 uint16_t listen_port_{};
70 uint16_t broadcast_port_{};
73 uint8_t poll_pending_{}; // PollStep bits
74 uint8_t poll_failed_{}; // PollStep bits already logged
76#ifdef USE_UDP_IPV6_SEND
77 bool ipv4_send_{true};
78 bool ipv6_send_{};
80#endif
81};
82
83} // namespace esphome::udp
84#endif
Fixed-capacity vector - sized once through init() or try_init(); push_back never reallocates This avo...
Definition helpers.h:624
void set_poll_warning_(PollStep step, const LogString *what)
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_broadcast_port(uint16_t port)
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