ESPHome 2026.5.0b1
Loading...
Searching...
No Matches
wake_host.h
Go to the documentation of this file.
1#pragma once
2
4
5#ifdef USE_HOST
6
7#include "esphome/core/hal.h"
8
9#include <sys/select.h>
10#include <sys/socket.h>
11
12namespace esphome {
13
16
20bool wake_register_fd(int fd);
21
23void wake_unregister_fd(int fd);
24
26void wake_setup();
27
29
30namespace internal {
33
34// File-scope state owned by wake_host.cpp. Accessed inline by
35// wake_drain_notifications() and wake_fd_ready() so the hot path stays in the header.
36// NOLINTNEXTLINE(cppcoreguidelines-avoid-non-const-global-variables)
37extern int g_wake_socket_fd;
38// NOLINTNEXTLINE(cppcoreguidelines-avoid-non-const-global-variables)
39extern fd_set g_read_fds;
40} // namespace internal
41
42inline bool ESPHOME_ALWAYS_INLINE wake_fd_ready(int fd) { return FD_ISSET(fd, &internal::g_read_fds); }
43
44// Small buffer for draining wake notification bytes (1 byte sent per wake).
45// Sized to drain multiple notifications per recvfrom() without wasting stack.
46inline constexpr size_t WAKE_NOTIFY_DRAIN_BUFFER_SIZE = 16;
47
48inline void ESPHOME_ALWAYS_INLINE wake_drain_notifications() {
49 // Called from main loop to drain any pending wake notifications.
50 // Must check wake_fd_ready() to avoid blocking on empty socket.
53 // Drain all pending notifications with non-blocking reads. Multiple wake events
54 // may have triggered multiple writes, so drain until EWOULDBLOCK. We control
55 // both ends of this loopback socket (always 1 byte per wake), so no error
56 // checking — any error indicates catastrophic system failure.
57 while (::recvfrom(internal::g_wake_socket_fd, buffer, sizeof(buffer), 0, nullptr, nullptr) > 0) {
58 }
59 }
60}
61
62} // namespace esphome
63
64#endif // USE_HOST
void ESPHOME_ALWAYS_INLINE wakeable_delay(uint32_t ms)
Host wakeable_delay uses select() over the registered fds — defined in wake_host.cpp.
void wake_loop_threadsafe()
Non-ISR: always inline.
bool ESPHOME_ALWAYS_INLINE wake_fd_ready(int fd)
Definition wake_host.h:42
bool wake_register_fd(int fd)
Register a socket file descriptor with the host select() loop.
Definition wake_host.cpp:46
void ESPHOME_ALWAYS_INLINE wake_drain_notifications()
Definition wake_host.h:48
void wake_unregister_fd(int fd)
Unregister a socket file descriptor. Not thread-safe — main loop only.
Definition wake_host.cpp:65
constexpr size_t WAKE_NOTIFY_DRAIN_BUFFER_SIZE
Definition wake_host.h:46
void wake_setup()
One-time setup of the loopback wake socket. Called from Application::setup().
void IRAM_ATTR wake_loop_any_context()
IRAM_ATTR entry point for ISR callers — defined in wake_esp8266.cpp.
static void uint32_t