diff --git a/esphome/components/socket/socket.cpp b/esphome/components/socket/socket.cpp index 6edbcf4e98..188bda0753 100644 --- a/esphome/components/socket/socket.cpp +++ b/esphome/components/socket/socket.cpp @@ -89,56 +89,6 @@ std::unique_ptr socket_ip(int type, int protocol) { #endif /* USE_NETWORK_IPV6 */ } -#ifdef USE_SOCKET_IMPL_LWIP_TCP -// LWIP_TCP has separate Socket/ListenSocket types — needs out-of-line factory. -// BSD and LWIP_SOCKETS define this inline in socket.h. -std::unique_ptr socket_ip_loop_monitored(int type, int protocol) { -#if USE_NETWORK_IPV6 - return socket_listen_loop_monitored(AF_INET6, type, protocol); -#else - return socket_listen_loop_monitored(AF_INET, type, protocol); -#endif /* USE_NETWORK_IPV6 */ -} -#endif - -#ifdef USE_SOCKET_IMPL_LWIP_TCP -// LWIP_TCP has separate UDPRecvSocket type — needs out-of-line factory. -// BSD and LWIP_SOCKETS define this inline in socket.h. -std::unique_ptr socket_ip_udp_recv_loop_monitored(int protocol) { -#if USE_NETWORK_IPV6 - return socket_udp_recv_loop_monitored(AF_INET6, protocol); -#else - return socket_udp_recv_loop_monitored(AF_INET, protocol); -#endif /* USE_NETWORK_IPV6 */ -} -#endif - -#if !defined(USE_SOCKET_IMPL_LWIP_TCP) -// BSD and LWIP_SOCKETS: UDPSocket == UDPRecvSocket == Socket, so these just delegate. -std::unique_ptr socket_udp(int domain, int protocol) { - return esphome::socket::socket(domain, SOCK_DGRAM, protocol); -} -std::unique_ptr socket_udp_recv(int domain, int protocol) { - return esphome::socket::socket(domain, SOCK_DGRAM, protocol); -} -#endif - -std::unique_ptr socket_ip_udp(int protocol) { -#if USE_NETWORK_IPV6 - return socket_udp(AF_INET6, protocol); -#else - return socket_udp(AF_INET, protocol); -#endif -} - -std::unique_ptr socket_ip_udp_recv(int protocol) { -#if USE_NETWORK_IPV6 - return socket_udp_recv(AF_INET6, protocol); -#else - return socket_udp_recv(AF_INET, protocol); -#endif -} - socklen_t set_sockaddr(struct sockaddr *addr, socklen_t addrlen, const char *ip_address, uint16_t port) { #if USE_NETWORK_IPV6 if (strchr(ip_address, ':') != nullptr) { diff --git a/esphome/components/socket/socket.h b/esphome/components/socket/socket.h index d919ad8aa8..e9399d7a67 100644 --- a/esphome/components/socket/socket.h +++ b/esphome/components/socket/socket.h @@ -89,25 +89,49 @@ std::unique_ptr socket(int domain, int type, int protocol); std::unique_ptr socket_ip(int type, int protocol); /// Create a send-only UDP socket of the given domain and protocol. +#ifdef USE_SOCKET_IMPL_LWIP_TCP std::unique_ptr socket_udp(int domain, int protocol); +#else +inline std::unique_ptr socket_udp(int domain, int protocol) { + return esphome::socket::socket(domain, SOCK_DGRAM, protocol); +} +#endif /// Create a send-only UDP socket in the newest available IP domain. -std::unique_ptr socket_ip_udp(int protocol); +inline std::unique_ptr socket_ip_udp(int protocol) { +#if USE_NETWORK_IPV6 + return socket_udp(AF_INET6, protocol); +#else + return socket_udp(AF_INET, protocol); +#endif +} /// Create a UDP socket with receive support of the given domain and protocol. +#ifdef USE_SOCKET_IMPL_LWIP_TCP std::unique_ptr socket_udp_recv(int domain, int protocol); +#else +inline std::unique_ptr socket_udp_recv(int domain, int protocol) { + return esphome::socket::socket(domain, SOCK_DGRAM, protocol); +} +#endif /// Create a UDP socket with receive support in the newest available IP domain. -std::unique_ptr socket_ip_udp_recv(int protocol); +inline std::unique_ptr socket_ip_udp_recv(int protocol) { +#if USE_NETWORK_IPV6 + return socket_udp_recv(AF_INET6, protocol); +#else + return socket_udp_recv(AF_INET, protocol); +#endif +} /// Create a UDP recv socket and monitor it for data in the main loop. /// On LWIP_TCP platforms, wake is built into the recv callback so this just delegates to socket_udp_recv(). /// On BSD/LWIP_SOCKETS platforms, this registers the socket with the Application's select() loop. #ifdef USE_SOCKET_IMPL_LWIP_TCP std::unique_ptr socket_udp_recv_loop_monitored(int domain, int protocol); -std::unique_ptr socket_ip_udp_recv_loop_monitored(int protocol); #else inline std::unique_ptr socket_udp_recv_loop_monitored(int domain, int protocol) { return socket_loop_monitored(domain, SOCK_DGRAM, protocol); } +#endif inline std::unique_ptr socket_ip_udp_recv_loop_monitored(int protocol) { #if USE_NETWORK_IPV6 return socket_udp_recv_loop_monitored(AF_INET6, protocol); @@ -115,7 +139,6 @@ inline std::unique_ptr socket_ip_udp_recv_loop_monitored(int prot return socket_udp_recv_loop_monitored(AF_INET, protocol); #endif } -#endif /// Create a socket and monitor it for data in the main loop. /// Like socket() but also registers the socket with the Application's select() loop. @@ -132,7 +155,6 @@ std::unique_ptr socket_loop_monitored(int domain, int type, int protocol // LWIP_TCP has separate Socket/ListenSocket types — needs distinct factory functions. std::unique_ptr socket_listen(int domain, int type, int protocol); std::unique_ptr socket_listen_loop_monitored(int domain, int type, int protocol); -std::unique_ptr socket_ip_loop_monitored(int type, int protocol); #else // BSD and LWIP_SOCKETS: Socket == ListenSocket, so listen variants just delegate. inline std::unique_ptr socket_listen(int domain, int type, int protocol) { @@ -141,14 +163,14 @@ inline std::unique_ptr socket_listen(int domain, int type, int pro inline std::unique_ptr socket_listen_loop_monitored(int domain, int type, int protocol) { return socket_loop_monitored(domain, type, protocol); } +#endif inline std::unique_ptr socket_ip_loop_monitored(int type, int protocol) { #if USE_NETWORK_IPV6 - return socket_loop_monitored(AF_INET6, type, protocol); + return socket_listen_loop_monitored(AF_INET6, type, protocol); #else - return socket_loop_monitored(AF_INET, type, protocol); + return socket_listen_loop_monitored(AF_INET, type, protocol); #endif } -#endif /// Set a sockaddr to the specified address and port for the IP version used by socket_ip(). /// @param addr Destination sockaddr structure