ESPHome 2026.11.0-dev
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
17
21bool wake_register_fd(int fd);
22
24void wake_unregister_fd(int fd);
25
27void wake_setup();
28
30
31namespace internal {
34
35// File-scope state owned by wake_host.cpp. Accessed inline by
36// wake_drain_notifications() and wake_fd_ready() so the hot path stays in the header.
37// NOLINTNEXTLINE(cppcoreguidelines-avoid-non-const-global-variables)
38extern int g_wake_socket_fd;
39// NOLINTNEXTLINE(cppcoreguidelines-avoid-non-const-global-variables)
40extern fd_set g_read_fds;
41} // namespace internal
42
43inline bool ESPHOME_ALWAYS_INLINE wake_fd_ready(int fd) { return FD_ISSET(fd, &internal::g_read_fds); }
44
45// Small buffer for draining wake notification bytes (1 byte sent per wake).
46// Sized to drain multiple notifications per recvfrom() without wasting stack.
47inline constexpr size_t WAKE_NOTIFY_DRAIN_BUFFER_SIZE = 16;
48
49inline void ESPHOME_ALWAYS_INLINE wake_drain_notifications() {
50 // Called from main loop to drain any pending wake notifications.
51 // Must check wake_fd_ready() to avoid blocking on empty socket.
54 // Drain all pending notifications with non-blocking reads. Multiple wake events
55 // may have triggered multiple writes, so drain until EWOULDBLOCK. We control
56 // both ends of this loopback socket (always 1 byte per wake), so no error
57 // checking — any error indicates catastrophic system failure.
58 while (::recvfrom(internal::g_wake_socket_fd, buffer, sizeof(buffer), 0, nullptr, nullptr) > 0) {
59 }
60 }
61}
62
63} // namespace esphome
64
65#endif // USE_HOST
uint32_t buffer
Definition gif.cpp:50
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.
void wake_scheduler_threadsafe()
bool ESPHOME_ALWAYS_INLINE wake_fd_ready(int fd)
Definition wake_host.h:43
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:49
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:47
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