27#ifdef USE_LWIP_FAST_SELECT
31#include <sys/select.h>
32#include <sys/socket.h>
35#include <netinet/in.h>
38#ifdef USE_RUNTIME_STATS
51#ifdef USE_RUNTIME_STATS
53class RuntimeStatsCollector;
69 : std::bool_constant<!std::is_same_v<decltype(&T::loop), decltype(&Component::loop)>> {};
75static constexpr uint32_t TEARDOWN_TIMEOUT_REBOOT_MS = 1000;
79#ifdef ESPHOME_NAME_ADD_MAC_SUFFIX
82 void pre_setup(
char *name,
size_t name_len,
char *friendly_name,
size_t friendly_name_len) {
86 constexpr size_t mac_address_len = 13;
88 constexpr size_t mac_address_suffix_len = 6;
89 char mac_addr[mac_address_len];
93 memcpy(name + name_len - mac_address_suffix_len, mac_addr + mac_address_suffix_len, mac_address_suffix_len);
94 if (friendly_name_len > 0) {
95 memcpy(friendly_name + friendly_name_len - mac_address_suffix_len, mac_addr + mac_address_suffix_len,
96 mac_address_suffix_len);
104 void pre_setup(
const char *name,
size_t name_len,
const char *friendly_name,
size_t friendly_name_len) {
124#define ENTITY_TYPE_(type, singular, plural, count, upper) \
125 void register_##singular(type *obj) { this->plural##_.push_back(obj); }
126#define ENTITY_CONTROLLER_TYPE_(type, singular, plural, count, upper, callback) \
127 ENTITY_TYPE_(type, singular, plural, count, upper)
130#undef ENTITY_CONTROLLER_TYPE_
133#ifdef USE_SERIAL_PROXY
146 inline void ESPHOME_ALWAYS_INLINE
loop();
158 if (!this->
areas_.empty() && this->areas_[0] !=
nullptr) {
159 return this->
areas_[0]->get_name();
175 return std::string(buffer);
198 ESPDEPRECATED(
"Use get_build_time_string() instead. Removed in 2026.7.0",
"2026.1.0")
199 std::
string get_compilation_time() {
202 return std::string(buf);
225 this->
loop_interval_ = std::min(loop_interval,
static_cast<uint32_t>(std::numeric_limits<uint16_t>::max()));
280#define GET_ENTITY_METHOD(entity_type, entity_name, entities_member) \
281 entity_type *get_##entity_name##_by_key(uint32_t key, uint32_t device_id, bool include_internal = false) { \
282 for (auto *obj : this->entities_member##_) { \
283 if (obj->get_object_id_hash() == key && obj->get_device_id() == device_id && \
284 (include_internal || !obj->is_internal())) \
291#define GET_ENTITY_METHOD(entity_type, entity_name, entities_member) \
292 entity_type *get_##entity_name##_by_key(uint32_t key, bool include_internal = false) { \
293 for (auto *obj : this->entities_member##_) { \
294 if (obj->get_object_id_hash() == key && (include_internal || !obj->is_internal())) \
305#define ENTITY_TYPE_(type, singular, plural, count, upper) \
306 auto &get_##plural() const { return this->plural##_; } \
307 GET_ENTITY_METHOD(type, singular, plural)
308#define ENTITY_CONTROLLER_TYPE_(type, singular, plural, count, upper, callback) \
309 ENTITY_TYPE_(type, singular, plural, count, upper)
312#undef ENTITY_CONTROLLER_TYPE_
315#ifdef USE_SERIAL_PROXY
323#ifdef USE_LWIP_FAST_SELECT
328#elif defined(USE_HOST)
340#if defined(USE_ESP32) || defined(USE_LIBRETINY)
353#ifdef USE_RUNTIME_STATS
356 friend void ::setup();
357 friend void ::original_setup();
451#ifdef USE_LWIP_FAST_SELECT
453#elif defined(USE_HOST)
506#define ENTITY_TYPE_(type, singular, plural, count, upper) StaticVector<type *, count> plural##_{};
507#define ENTITY_CONTROLLER_TYPE_(type, singular, plural, count, upper, callback) \
508 ENTITY_TYPE_(type, singular, plural, count, upper)
511#undef ENTITY_CONTROLLER_TYPE_
514#ifdef USE_SERIAL_PROXY
520extern Application
App;
528static constexpr size_t WAKE_NOTIFY_DRAIN_BUFFER_SIZE = 16;
534 char buffer[WAKE_NOTIFY_DRAIN_BUFFER_SIZE];
539 while (::recvfrom(this->
wake_socket_fd_, buffer,
sizeof(buffer), 0,
nullptr,
nullptr) > 0) {
557 this->scheduler.call(loop_start_time);
578#ifdef USE_RUNTIME_STATS
593#ifdef USE_RUNTIME_STATS
610 last_op_end_time = guard.finish();
615#ifdef USE_RUNTIME_STATS
620#ifdef USE_RUNTIME_STATS
627 uint32_t loop_before_wall_us = loop_before_end_us - loop_active_start_us;
628 uint32_t loop_before_overhead_us = loop_before_wall_us > loop_before_scheduled_us
629 ? loop_before_wall_us -
static_cast<uint32_t>(loop_before_scheduled_us)
632 loop_now_us - loop_tail_start_us);
639 auto elapsed = last_op_end_time - this->
last_loop_;
642 uint32_t next_schedule = this->scheduler.next_schedule_in(last_op_end_time).value_or(delay_time);
645 next_schedule = std::max(next_schedule, delay_time / 2);
646 delay_time = std::min(next_schedule, delay_time);
649 this->last_loop_ = last_op_end_time;
652 this->process_dump_config_();
659#ifdef USE_LWIP_FAST_SELECT
662 if (delay_ms == 0) [[unlikely]] {
672 if (esphome_lwip_socket_has_data(sock)) {
void enable_pending_loops_()
std::vector< struct lwip_sock * > monitored_sockets_
void setup()
Reserve space for components to avoid memory fragmentation.
uint32_t get_loop_interval() const
const StringRef & get_name() const
Get the name of this Application set by pre_setup().
void wake_loop_threadsafe()
Wake the main event loop from another thread or callback.
void ESPHOME_ALWAYS_INLINE feed_wdt_with_time(uint32_t time)
Feed the task watchdog, hot entry.
uint16_t looping_components_active_end_
void pre_setup(char *name, size_t name_len, char *friendly_name, size_t friendly_name_len)
Pre-setup with MAC suffix: overwrites placeholder in mutable static buffers with actual MAC.
void set_current_component(Component *component)
Component * get_current_component()
void yield_with_select_(uint32_t delay_ms)
Perform a delay while also monitoring socket file descriptors for readiness.
void register_serial_proxy(serial_proxy::SerialProxy *proxy)
bool register_socket(struct lwip_sock *sock)
Register/unregister a socket to be monitored for read events.
static void IRAM_ATTR wake_loop_any_context()
Wake from any context (ISR, thread, callback).
static constexpr size_t BUILD_TIME_STR_SIZE
Size of buffer required for build time string (including null terminator)
void __attribute__((noinline)) process_dump_config_()
Process dump_config output one component per loop iteration.
StaticVector< Area *, ESPHOME_AREA_COUNT > areas_
std::string get_comment()
Get the comment of this Application as a string.
uint32_t get_config_hash()
Get the config hash as a 32-bit integer.
std::vector< int > socket_fds_
const StringRef & get_friendly_name() const
Get the friendly name of this Application set by pre_setup().
void pre_setup(const char *name, size_t name_len, const char *friendly_name, size_t friendly_name_len)
Pre-setup without MAC suffix: StringRef points directly at const string literals in flash.
void set_loop_interval(uint32_t loop_interval)
Set the target interval with which to run the loop() calls.
StaticVector< Component *, ESPHOME_COMPONENT_COUNT > components_
void setup_wake_loop_threadsafe_()
bool any_component_has_status_flag_(uint8_t flag) const
Walk all registered components looking for any whose component_state_ has the given flag set.
void get_build_time_string(std::span< char, BUILD_TIME_STR_SIZE > buffer)
Copy the build time string into the provided buffer Buffer must be BUILD_TIME_STR_SIZE bytes (compile...
void register_area(Area *area)
Component * current_component_
void drain_wake_notifications_()
void enable_component_loop_(Component *component)
uint32_t loop_component_start_time_
void feed_wdt()
Feed the task watchdog.
void disable_component_loop_(Component *component)
const char * get_area() const
Get the area of this Application set by pre_setup().
bool is_name_add_mac_suffix_enabled() const
void ESPHOME_ALWAYS_INLINE before_loop_tasks_(uint32_t loop_start_time)
void activate_looping_component_(uint16_t index)
static constexpr uint32_t WDT_FEED_INTERVAL_MS
Minimum interval between real arch_feed_wdt() calls.
const auto & get_devices()
ESPDEPRECATED("Use get_build_time_string() instead. Removed in 2026.7.0", "2026.1.0") std
Get the build time as a string (deprecated, use get_build_time_string() instead)
void ESPHOME_ALWAYS_INLINE after_loop_tasks_()
void teardown_components(uint32_t timeout_ms)
Teardown all components with a timeout.
static constexpr size_t ESPHOME_COMMENT_SIZE_MAX
Maximum size of the comment buffer (including null terminator)
FixedVector< Component * > looping_components_
auto & get_serial_proxies() const
void add_looping_components_by_state_(bool match_loop_done)
bool name_add_mac_suffix_
volatile bool has_pending_enable_loop_requests_
void get_comment_string(std::span< char, ESPHOME_COMMENT_SIZE_MAX > buffer)
Copy the comment string into the provided buffer.
static void IRAM_ATTR wake_loop_isrsafe(BaseType_t *px)
Wake from ISR (ESP32 and LibreTiny).
void feed_wdt_slow_(uint32_t time)
Slow path for feed_wdt(): actually calls arch_feed_wdt(), updates last_wdt_feed_, and re-dispatches t...
void schedule_dump_config()
void run_safe_shutdown_hooks()
uint16_t current_loop_index_
bool is_socket_ready_(int fd) const
void ESPHOME_ALWAYS_INLINE loop()
Make a loop iteration. Call this in your loop() function.
time_t get_build_time()
Get the build time as a Unix timestamp.
StaticVector< serial_proxy::SerialProxy *, SERIAL_PROXY_COUNT > serial_proxies_
bool is_setup_complete() const
True once Application::setup() has finished walking all components and finalized the initial status f...
void register_component_(T *comp)
Register a component, detecting loop() override at compile time.
void register_device(Device *device)
void unregister_socket_fd(int fd)
uint32_t get_config_version_hash()
Get the config hash extended with ESPHome version.
bool register_socket_fd(int fd)
Fallback select() path: monitors file descriptors.
StaticVector< Device *, ESPHOME_DEVICE_COUNT > devices_
void calculate_looping_components_()
uint32_t IRAM_ATTR HOT get_loop_component_start_time() const
Get the cached time in milliseconds from when the current component started its loop execution.
void register_component_impl_(Component *comp, bool has_loop)
uint8_t get_app_state() const
Return the public app state status bits (STATUS_LED_* only).
void run_powerdown_hooks()
void unregister_socket(struct lwip_sock *sock)
Fixed-capacity vector - allocates once at runtime, never reallocates This avoids std::vector template...
static bool is_high_frequency()
Check whether the loop is running continuously.
Minimal static vector - saves memory by avoiding std::vector overhead.
StringRef is a reference to a string owned by something else.
void process_pending_stats(uint32_t current_time)
void record_loop_active(uint32_t active_us, uint32_t before_us, uint32_t tail_us)
void set_instance_index(uint32_t index)
Set the instance index (called by Application::register_serial_proxy)
const Component * component
void wakeable_delay(uint32_t ms)
bool socket_ready_fd(int fd, bool loop_monitored)
Shared ready() helper for fd-based socket implementations.
Providing packet encoding functions for exchanging data with a remote host.
runtime_stats::RuntimeStatsCollector * global_runtime_stats
void wake_loop_threadsafe()
Non-ISR: always inline.
constexpr uint8_t APP_STATE_SETUP_COMPLETE
uint32_t IRAM_ATTR HOT micros()
void get_mac_address_into_buffer(std::span< char, MAC_ADDRESS_BUFFER_SIZE > buf)
Get the device MAC address into the given buffer, in lowercase hex notation.
uint32_t IRAM_ATTR HOT millis()
Application App
Global storage of Application pointer - only one Application can exist.
void IRAM_ATTR wake_loop_any_context()
IRAM_ATTR entry point for ISR callers — defined in wake.cpp.
void IRAM_ATTR wake_loop_isrsafe(BaseType_t *px_higher_priority_task_woken)
IRAM_ATTR entry points — defined in wake.cpp.
static uint64_t global_recorded_us
SFINAE helper: detects whether T overrides Component::loop().
Platform-specific main loop wake primitives.