ESPHome 2026.3.3
Loading...
Searching...
No Matches
esphome::socket Namespace Reference

Data Structures

class  BSDSocketImpl
 
class  LWIPRawCommon
 Non-virtual common base for LWIP raw TCP sockets. More...
 
class  LWIPRawImpl
 Connected socket implementation for LWIP raw TCP. More...
 
class  LWIPRawListenImpl
 Listening socket implementation for LWIP raw TCP. More...
 
class  LwIPSocketImpl
 

Typedefs

using Socket = BSDSocketImpl
 
using ListenSocket = BSDSocketImpl
 

Functions

std::unique_ptr< Socketsocket (int domain, int type, int protocol)
 Create a socket of the given domain, type and protocol.
 
std::unique_ptr< Socketsocket_loop_monitored (int domain, int type, int protocol)
 Create a socket and monitor it for data in the main loop.
 
void socket_delay (uint32_t ms)
 Delay that can be woken early by socket activity.
 
void IRAM_ATTR socket_wake ()
 Signal socket/IO activity and wake the main loop early.
 
std::unique_ptr< ListenSocketsocket_listen (int domain, int type, int protocol)
 Create a listening socket of the given domain, type and protocol.
 
std::unique_ptr< ListenSocketsocket_listen_loop_monitored (int domain, int type, int protocol)
 
bool socket_ready_fd (int fd, bool loop_monitored)
 Shared ready() helper for fd-based socket implementations.
 
size_t format_sockaddr_to (const struct sockaddr *addr_ptr, socklen_t len, std::span< char, SOCKADDR_STR_LEN > buf)
 Format sockaddr into caller-provided buffer, returns length written (excluding null)
 
std::unique_ptr< Socketsocket_ip (int type, int protocol)
 Create a socket in the newest available IP domain (IPv6 or IPv4) of the given type and protocol.
 
std::unique_ptr< ListenSocketsocket_ip_loop_monitored (int type, int protocol)
 
socklen_t set_sockaddr (struct sockaddr *addr, socklen_t addrlen, const char *ip_address, uint16_t port)
 Set a sockaddr to the specified address and port for the IP version used by socket_ip().
 
socklen_t set_sockaddr_any (struct sockaddr *addr, socklen_t addrlen, uint16_t port)
 Set a sockaddr to the any address and specified port for the IP version used by socket_ip().
 
bool socket_ready (struct lwip_sock *cached_sock, bool loop_monitored)
 Shared ready() helper using cached lwip_sock pointer for direct rcvevent read.
 
socklen_t set_sockaddr (struct sockaddr *addr, socklen_t addrlen, const std::string &ip_address, uint16_t port)
 Convenience overload for std::string (backward compatible).
 

Typedef Documentation

◆ ListenSocket

◆ Socket

Definition at line 33 of file socket.h.

Function Documentation

◆ format_sockaddr_to()

size_t esphome::socket::format_sockaddr_to ( const struct sockaddr * addr_ptr,
socklen_t len,
std::span< char, SOCKADDR_STR_LEN > buf )

Format sockaddr into caller-provided buffer, returns length written (excluding null)

Definition at line 53 of file socket.cpp.

◆ set_sockaddr() [1/2]

socklen_t esphome::socket::set_sockaddr ( struct sockaddr * addr,
socklen_t addrlen,
const char * ip_address,
uint16_t port )

Set a sockaddr to the specified address and port for the IP version used by socket_ip().

Parameters
addrDestination sockaddr structure
addrlenSize of the addr buffer
ip_addressNull-terminated IP address string (IPv4 or IPv6)
portPort number in host byte order
Returns
Size of the sockaddr structure used, or 0 on error

Definition at line 104 of file socket.cpp.

◆ set_sockaddr() [2/2]

socklen_t esphome::socket::set_sockaddr ( struct sockaddr * addr,
socklen_t addrlen,
const std::string & ip_address,
uint16_t port )
inline

Convenience overload for std::string (backward compatible).

Definition at line 113 of file socket.h.

◆ set_sockaddr_any()

socklen_t esphome::socket::set_sockaddr_any ( struct sockaddr * addr,
socklen_t addrlen,
uint16_t port )

Set a sockaddr to the any address and specified port for the IP version used by socket_ip().

Definition at line 143 of file socket.cpp.

◆ socket()

std::unique_ptr< Socket > esphome::socket::socket ( int domain,
int type,
int protocol )

Create a socket of the given domain, type and protocol.

Definition at line 90 of file bsd_sockets_impl.cpp.

◆ socket_delay()

void esphome::socket::socket_delay ( uint32_t ms)

Delay that can be woken early by socket activity.

On ESP8266, uses esp_delay() with a callback that checks socket activity. On RP2040, uses __wfe() (Wait For Event) to truly sleep until an interrupt (for example, CYW43 GPIO or a timer alarm) fires and wakes the CPU.

Definition at line 27 of file lwip_raw_tcp_impl.cpp.

◆ socket_ip()

std::unique_ptr< Socket > esphome::socket::socket_ip ( int type,
int protocol )

Create a socket in the newest available IP domain (IPv6 or IPv4) of the given type and protocol.

Definition at line 84 of file socket.cpp.

◆ socket_ip_loop_monitored()

std::unique_ptr< ListenSocket > esphome::socket::socket_ip_loop_monitored ( int type,
int protocol )
inline

Definition at line 95 of file socket.cpp.

◆ socket_listen()

std::unique_ptr< ListenSocket > esphome::socket::socket_listen ( int domain,
int type,
int protocol )
inline

Create a listening socket of the given domain, type and protocol.

Create a listening socket and monitor it for data in the main loop. Create a listening socket in the newest available IP domain and monitor it.

Definition at line 982 of file lwip_raw_tcp_impl.cpp.

◆ socket_listen_loop_monitored()

std::unique_ptr< ListenSocket > esphome::socket::socket_listen_loop_monitored ( int domain,
int type,
int protocol )
inline

Definition at line 997 of file lwip_raw_tcp_impl.cpp.

◆ socket_loop_monitored()

std::unique_ptr< Socket > esphome::socket::socket_loop_monitored ( int domain,
int type,
int protocol )

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. WARNING: These functions are NOT thread-safe. They must only be called from the main loop as they register the socket file descriptor with the global Application instance. NOTE: On ESP platforms, FD_SETSIZE is typically 10, limiting the number of monitored sockets. File descriptors >= FD_SETSIZE will not be monitored and will log an error.

Definition at line 94 of file bsd_sockets_impl.cpp.

◆ socket_ready()

bool esphome::socket::socket_ready ( struct lwip_sock * cached_sock,
bool loop_monitored )
inline

Shared ready() helper using cached lwip_sock pointer for direct rcvevent read.

Definition at line 45 of file socket.h.

◆ socket_ready_fd()

bool esphome::socket::socket_ready_fd ( int fd,
bool loop_monitored )

Shared ready() helper for fd-based socket implementations.

Checks if the Application's select() loop has marked this fd as ready.

Definition at line 14 of file socket.cpp.

◆ socket_wake()

void esphome::socket::socket_wake ( )

Signal socket/IO activity and wake the main loop early.

On ESP8266: sets flag + esp_schedule(). On RP2040: sets flag + __sev() (Send Event) to wake from __wfe(). ISR-safe on both platforms.

Definition at line 43 of file lwip_raw_tcp_impl.cpp.