ESPHome 2026.3.0-dev
Loading...
Searching...
No Matches
socket.h
Go to the documentation of this file.
1#pragma once
2#include <memory>
3#include <span>
4#include <string>
5
7#include "headers.h"
8
9#if defined(USE_SOCKET_IMPL_LWIP_TCP) || defined(USE_SOCKET_IMPL_LWIP_SOCKETS) || defined(USE_SOCKET_IMPL_BSD_SOCKETS)
10
11// Include only the active implementation's header.
12// SOCKADDR_STR_LEN is defined in headers.h.
13#ifdef USE_SOCKET_IMPL_BSD_SOCKETS
14#include "bsd_sockets_impl.h"
15#elif defined(USE_SOCKET_IMPL_LWIP_SOCKETS)
16#include "lwip_sockets_impl.h"
17#elif defined(USE_SOCKET_IMPL_LWIP_TCP)
18#include "lwip_raw_tcp_impl.h"
19#endif
20
21namespace esphome::socket {
22
23// Type aliases — only one implementation is active per build.
24// Socket is the concrete type for connected sockets.
25// ListenSocket is the concrete type for listening/server sockets.
26// On BSD and LWIP_SOCKETS, both aliases resolve to the same type.
27// On LWIP_TCP, they are different types (no virtual dispatch between them).
28#ifdef USE_SOCKET_IMPL_BSD_SOCKETS
31#elif defined(USE_SOCKET_IMPL_LWIP_SOCKETS)
34#elif defined(USE_SOCKET_IMPL_LWIP_TCP)
35using Socket = LWIPRawImpl;
37#endif
38
39#ifdef USE_SOCKET_SELECT_SUPPORT
42bool socket_ready_fd(int fd, bool loop_monitored);
43#endif
44
46std::unique_ptr<Socket> socket(int domain, int type, int protocol);
48std::unique_ptr<Socket> socket_ip(int type, int protocol);
49
56std::unique_ptr<Socket> socket_loop_monitored(int domain, int type, int protocol);
57
59std::unique_ptr<ListenSocket> socket_listen(int domain, int type, int protocol);
61std::unique_ptr<ListenSocket> socket_listen_loop_monitored(int domain, int type, int protocol);
63std::unique_ptr<ListenSocket> socket_ip_loop_monitored(int type, int protocol);
64
71socklen_t set_sockaddr(struct sockaddr *addr, socklen_t addrlen, const char *ip_address, uint16_t port);
72
74inline socklen_t set_sockaddr(struct sockaddr *addr, socklen_t addrlen, const std::string &ip_address, uint16_t port) {
75 return set_sockaddr(addr, addrlen, ip_address.c_str(), port);
76}
77
79socklen_t set_sockaddr_any(struct sockaddr *addr, socklen_t addrlen, uint16_t port);
80
82size_t format_sockaddr_to(const struct sockaddr *addr_ptr, socklen_t len, std::span<char, SOCKADDR_STR_LEN> buf);
83
84#if defined(USE_ESP8266) && defined(USE_SOCKET_IMPL_LWIP_TCP)
87void socket_delay(uint32_t ms);
88
91void socket_wake(); // NOLINT(readability-redundant-declaration)
92#endif
93
94} // namespace esphome::socket
95#endif
Connected socket implementation for LWIP raw TCP.
Listening socket implementation for LWIP raw TCP.
uint16_t type
uint32_t socklen_t
Definition headers.h:97
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().
Definition socket.cpp:100
std::unique_ptr< 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 socket.cpp:84
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)
Definition socket.cpp:53
std::unique_ptr< ListenSocket > socket_listen(int domain, int type, int protocol)
Create a listening socket of the given domain, type and protocol.
std::unique_ptr< ListenSocket > socket_listen_loop_monitored(int domain, int type, int protocol)
Create a listening socket and monitor it for data in the main loop.
void IRAM_ATTR socket_wake()
Signal socket/IO activity and wake the main loop from esp_delay() early.
std::unique_ptr< Socket > socket(int domain, int type, int protocol)
Create a socket of the given domain, type and protocol.
bool socket_ready_fd(int fd, bool loop_monitored)
Shared ready() helper for fd-based socket implementations.
Definition socket.cpp:14
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().
Definition socket.cpp:139
std::unique_ptr< Socket > socket_loop_monitored(int domain, int type, int protocol)
Create a socket and monitor it for data in the main loop.
std::unique_ptr< ListenSocket > socket_ip_loop_monitored(int type, int protocol)
Create a listening socket in the newest available IP domain and monitor it.
Definition socket.cpp:92
void socket_delay(uint32_t ms)
Delay that can be woken early by socket activity.
std::string size_t len
Definition helpers.h:817