11static const char *
const TAG =
"udp";
14#if defined(USE_SOCKET_IMPL_BSD_SOCKETS) || defined(USE_SOCKET_IMPL_LWIP_SOCKETS)
29 auto err = this->
broadcast_socket_->setsockopt(SOL_SOCKET, SO_REUSEADDR, &enable,
sizeof(
int));
34 err = this->
broadcast_socket_->setsockopt(SOL_SOCKET, SO_BROADCAST, &enable,
sizeof(
int));
50 ESP_LOGE(TAG,
"Unable to set nonblocking: errno %d", errno);
56 err = this->
listen_socket_->setsockopt(SOL_SOCKET, SO_REUSEADDR, &enable,
sizeof(enable));
64 server.
sin_addr.s_addr = ESPHOME_INADDR_ANY;
68 struct ip_mreq imreq = {};
69 imreq.imr_interface.s_addr = ESPHOME_INADDR_ANY;
70 inet_aton(this->
listen_address_.value().str().c_str(), &imreq.imr_multiaddr);
71 server.
sin_addr.s_addr = imreq.imr_multiaddr.s_addr;
72 ESP_LOGD(TAG,
"Join multicast %s", this->
listen_address_.value().str().c_str());
73 err = this->
listen_socket_->setsockopt(IPPROTO_IP, IP_ADD_MEMBERSHIP, &imreq,
sizeof(imreq));
75 ESP_LOGE(TAG,
"Failed to set IP_ADD_MEMBERSHIP. Error %d", errno);
84 ESP_LOGE(TAG,
"Socket unable to bind: errno %d", errno);
91#ifdef USE_SOCKET_IMPL_LWIP_TCP
93 for (
const auto &
address : this->addresses_) {
94 auto ipaddr = IPAddress();
95 ipaddr.fromString(
address.c_str());
104 auto buf = std::vector<uint8_t>(MAX_PACKET_SIZE);
107#if defined(USE_SOCKET_IMPL_BSD_SOCKETS) || defined(USE_SOCKET_IMPL_LWIP_SOCKETS)
110#ifdef USE_SOCKET_IMPL_LWIP_TCP
118 ESP_LOGV(TAG,
"Received packet of length %zu",
len);
125 ESP_LOGCONFIG(TAG,
"UDP:");
126 ESP_LOGCONFIG(TAG,
" Listen Port: %u", this->
listen_port_);
129 ESP_LOGCONFIG(TAG,
" Address: %s",
address.c_str());
131 ESP_LOGCONFIG(TAG,
" Listen address: %s", this->
listen_address_.value().str().c_str());
134 ESP_LOGCONFIG(TAG,
" Listening: %s", YESNO(this->
should_listen_));
138#if defined(USE_SOCKET_IMPL_BSD_SOCKETS) || defined(USE_SOCKET_IMPL_LWIP_SOCKETS)
140 auto result = this->
broadcast_socket_->sendto(data, size, 0, &saddr,
sizeof(saddr));
142 ESP_LOGW(TAG,
"sendto() error %d", errno);
145#ifdef USE_SOCKET_IMPL_LWIP_TCP
146 auto iface = IPAddress(0, 0, 0, 0);
147 for (
const auto &saddr : this->
ipaddrs_) {
148 if (this->
udp_client_.beginPacketMulticast(saddr, this->broadcast_port_, iface, 128) != 0) {
152 ESP_LOGW(TAG,
"udp.write() error");
virtual void mark_failed()
Mark this component as failed.
void status_set_warning(const char *message="unspecified")
void status_set_error(const char *message="unspecified")
std::vector< std::string > addresses_
CallbackManager< void(std::vector< uint8_t > &)> packet_listeners_
std::unique_ptr< socket::Socket > listen_socket_
std::unique_ptr< socket::Socket > broadcast_socket_
void send_packet(const uint8_t *data, size_t size)
std::vector< IPAddress > ipaddrs_
void dump_config() override
optional< network::IPAddress > listen_address_
std::vector< struct sockaddr > sockaddrs_
std::unique_ptr< Socket > socket(int domain, int type, int protocol)
Create a socket of the given domain, type and protocol.
socklen_t set_sockaddr(struct sockaddr *addr, socklen_t addrlen, const std::string &ip_address, uint16_t port)
Set a sockaddr to the specified address and port for the IP version used by socket_ip().
Providing packet encoding functions for exchanging data with a remote host.