ESPHome 2026.2.1
Loading...
Searching...
No Matches
udp_component.h
Go to the documentation of this file.
1#pragma once
2
4#ifdef USE_NETWORK
7#if defined(USE_SOCKET_IMPL_BSD_SOCKETS) || defined(USE_SOCKET_IMPL_LWIP_SOCKETS)
9#endif
10#ifdef USE_SOCKET_IMPL_LWIP_TCP
11#include <WiFiUdp.h>
12#endif
13#include <array>
14#include <initializer_list>
15#include <span>
16#include <vector>
17
18namespace esphome::udp {
19
20static const size_t MAX_PACKET_SIZE = 508;
21class UDPComponent : public Component {
22 public:
23 void set_addresses(std::initializer_list<const char *> addresses) { this->addresses_ = addresses; }
25 void set_addresses(std::initializer_list<std::string> addresses) = delete;
26 void set_listen_address(const char *listen_addr) { this->listen_address_ = network::IPAddress(listen_addr); }
27 void set_listen_port(uint16_t port) { this->listen_port_ = port; }
28 void set_broadcast_port(uint16_t port) { this->broadcast_port_ = port; }
29 void set_should_broadcast() { this->should_broadcast_ = true; }
30 void set_should_listen() { this->should_listen_ = true; }
31 void add_listener(std::function<void(std::span<const uint8_t>)> &&listener) {
32 this->packet_listeners_.add(std::move(listener));
33 }
34 void setup() override;
35 void loop() override;
36 void dump_config() override;
37 void send_packet(const uint8_t *data, size_t size);
38 void send_packet(const std::vector<uint8_t> &buf) { this->send_packet(buf.data(), buf.size()); }
39 float get_setup_priority() const override { return setup_priority::AFTER_WIFI; };
40
41 protected:
42 uint16_t listen_port_{};
43 uint16_t broadcast_port_{};
46 CallbackManager<void(std::span<const uint8_t>)> packet_listeners_{};
47
48#if defined(USE_SOCKET_IMPL_BSD_SOCKETS) || defined(USE_SOCKET_IMPL_LWIP_SOCKETS)
49 std::unique_ptr<socket::Socket> broadcast_socket_ = nullptr;
50 std::unique_ptr<socket::Socket> listen_socket_ = nullptr;
51 std::vector<struct sockaddr> sockaddrs_{};
52#endif
53#ifdef USE_SOCKET_IMPL_LWIP_TCP
54 std::vector<IPAddress> ipaddrs_{};
55 WiFiUDP udp_client_{};
56#endif
58
60};
61
62} // namespace esphome::udp
63#endif
Fixed-capacity vector - allocates once at runtime, never reallocates This avoids std::vector template...
Definition helpers.h:227
float get_setup_priority() const override
std::unique_ptr< socket::Socket > listen_socket_
void set_addresses(std::initializer_list< const char * > addresses)
std::unique_ptr< socket::Socket > broadcast_socket_
void send_packet(const uint8_t *data, size_t size)
std::vector< IPAddress > ipaddrs_
void set_listen_address(const char *listen_addr)
void set_broadcast_port(uint16_t port)
void set_addresses(std::initializer_list< std::string > addresses)=delete
Prevent accidental use of std::string which would dangle.
CallbackManager< void(std::span< const uint8_t >)> packet_listeners_
optional< network::IPAddress > listen_address_
void set_listen_port(uint16_t port)
void send_packet(const std::vector< uint8_t > &buf)
std::vector< struct sockaddr > sockaddrs_
FixedVector< const char * > addresses_
void add_listener(std::function< void(std::span< const uint8_t >)> &&listener)
const float AFTER_WIFI
For components that should be initialized after WiFi is connected.
Definition component.cpp:91
size_t size
Definition helpers.h:729