Files
esphome/esphome/components/socket/bsd_sockets_impl.cpp
T
J. Nick Koston ec833ce2d9 [core] Move host socket-select wake mechanism into wake.h/wake.cpp
The host platform's wake plumbing was split between wake.cpp (which had
wake_loop_threadsafe()) and application.{h,cpp} (which owned the fd
registry, wake socket lifecycle, select() loop, and readiness query).
The split required application.h to friend wake_loop_threadsafe() and
expose the wake socket fd, and forced a host-only out-of-line override
of yield_with_select_() that was equivalent to wakeable_delay() on every
other platform.

Consolidate the host wake mechanism in wake.h/wake.cpp:

- New host-only API: wake_register_fd, wake_unregister_fd, wake_fd_ready,
  wake_setup, wake_drain_notifications.
- Host implementation of internal::wakeable_delay now performs the
  select() over registered fds, mirroring the role of xTaskNotifyTake on
  ESP32 and esp_delay on ESP8266.
- Application drops the host-specific socket_fds_, max_fd_,
  wake_socket_fd_, socket_fds_changed_, read_fds_, base_read_fds_
  members, the host include block (sys/select.h, sys/socket.h, etc.),
  and the friend declarations for wake_loop_threadsafe() and
  socket::socket_ready_fd().
- yield_with_select_() collapses to a single inline definition for all
  platforms that delegates to wakeable_delay().
- Socket factories (bsd_sockets_impl.cpp, lwip_sockets_impl.cpp) and
  socket::socket_ready_fd() now call the wake.h API directly instead of
  going through App.
2026-04-23 00:33:04 +02:00

100 lines
2.9 KiB
C++

#include "esphome/core/defines.h"
#include "esphome/core/helpers.h"
#include "socket.h"
#ifdef USE_SOCKET_IMPL_BSD_SOCKETS
#include <cstring>
#include "esphome/core/application.h"
#ifdef USE_HOST
#include "esphome/core/wake.h"
#endif
namespace esphome::socket {
BSDSocketImpl::BSDSocketImpl(int fd, bool monitor_loop) {
this->fd_ = fd;
if (!monitor_loop || this->fd_ < 0)
return;
#ifdef USE_LWIP_FAST_SELECT
this->cached_sock_ = hook_fd_for_fast_select(this->fd_);
#else
this->loop_monitored_ = wake_register_fd(this->fd_);
#endif
}
BSDSocketImpl::~BSDSocketImpl() { this->close(); }
int BSDSocketImpl::close() {
if (this->fd_ < 0) {
// Already closed, or never opened.
return 0;
}
#ifdef USE_LWIP_FAST_SELECT
// Null the cached lwip_sock pointer before closing. The underlying lwip slot can be
// recycled for a new connection as soon as ::close() returns, so anything that might
// dereference cached_sock_ post-close (e.g. setsockopt(TCP_NODELAY)) would otherwise
// touch an unrelated socket's pcb. No per-socket callback unhook is needed —
// all LwIP sockets share the same static event_callback.
this->cached_sock_ = nullptr;
#else
if (this->loop_monitored_) {
wake_unregister_fd(this->fd_);
}
#endif
int ret = ::close(this->fd_);
this->fd_ = -1; // Sentinel for "closed" — prevents double-close and makes use-after-close visible.
return ret;
}
int BSDSocketImpl::setblocking(bool blocking) {
int fl = ::fcntl(this->fd_, F_GETFL, 0);
if (blocking) {
fl &= ~O_NONBLOCK;
} else {
fl |= O_NONBLOCK;
}
::fcntl(this->fd_, F_SETFL, fl);
return 0;
}
size_t BSDSocketImpl::getpeername_to(std::span<char, SOCKADDR_STR_LEN> buf) {
struct sockaddr_storage storage;
socklen_t len = sizeof(storage);
if (this->getpeername(reinterpret_cast<struct sockaddr *>(&storage), &len) != 0) {
buf[0] = '\0';
return 0;
}
return format_sockaddr_to(reinterpret_cast<struct sockaddr *>(&storage), len, buf);
}
size_t BSDSocketImpl::getsockname_to(std::span<char, SOCKADDR_STR_LEN> buf) {
struct sockaddr_storage storage;
socklen_t len = sizeof(storage);
if (this->getsockname(reinterpret_cast<struct sockaddr *>(&storage), &len) != 0) {
buf[0] = '\0';
return 0;
}
return format_sockaddr_to(reinterpret_cast<struct sockaddr *>(&storage), len, buf);
}
// Helper to create a socket with optional monitoring
static std::unique_ptr<BSDSocketImpl> create_socket(int domain, int type, int protocol, bool loop_monitored = false) {
int ret = ::socket(domain, type, protocol);
if (ret == -1)
return nullptr;
return make_unique<BSDSocketImpl>(ret, loop_monitored);
}
std::unique_ptr<Socket> socket(int domain, int type, int protocol) {
return create_socket(domain, type, protocol, false);
}
std::unique_ptr<Socket> socket_loop_monitored(int domain, int type, int protocol) {
return create_socket(domain, type, protocol, true);
}
} // namespace esphome::socket
#endif // USE_SOCKET_IMPL_BSD_SOCKETS