ESPHome 2026.4.1
Loading...
Searching...
No Matches
application.cpp
Go to the documentation of this file.
3#include "esphome/core/log.h"
5#include <cstring>
6
7#ifdef USE_ESP8266
8#include <pgmspace.h>
9#endif
10#ifdef USE_ESP32
11#include <esp_chip_info.h>
12#include <esp_ota_ops.h>
13#include <esp_bootloader_desc.h>
14#endif
15#ifdef USE_LWIP_FAST_SELECT
17#endif // USE_LWIP_FAST_SELECT
19#include "esphome/core/hal.h"
20#include <algorithm>
21#include <ranges>
22
23#ifdef USE_STATUS_LED
25#endif
26
27#if (defined(USE_ESP8266) || defined(USE_RP2040)) && defined(USE_SOCKET_IMPL_LWIP_TCP)
29#endif
30
31#ifdef USE_HOST
32#include <cerrno>
33#endif
34
35namespace esphome {
36
37static const char *const TAG = "app";
38
39// Helper function for insertion sort of components by priority
40// Using insertion sort instead of std::stable_sort saves ~1.3KB of flash
41// by avoiding template instantiations (std::rotate, std::stable_sort, lambdas)
42// IMPORTANT: This sort is stable (preserves relative order of equal elements),
43// which is necessary to maintain user-defined component order for same priority
44template<typename Iterator, float (Component::*GetPriority)() const>
45static void insertion_sort_by_priority(Iterator first, Iterator last) {
46 for (auto it = first + 1; it != last; ++it) {
47 auto key = *it;
48 float key_priority = (key->*GetPriority)();
49 auto j = it - 1;
50
51 // Using '<' (not '<=') ensures stability - equal priority components keep their order
52 while (j >= first && ((*j)->*GetPriority)() < key_priority) {
53 *(j + 1) = *j;
54 j--;
55 }
56 *(j + 1) = key;
57 }
58}
59
61 if (has_loop) {
63 }
64 this->components_.push_back(comp);
65}
67 ESP_LOGI(TAG, "Running through setup()");
68 ESP_LOGV(TAG, "Sorting components by setup priority");
69
70 // Sort by setup priority using our helper function
71 insertion_sort_by_priority<decltype(this->components_.begin()), &Component::get_actual_setup_priority>(
72 this->components_.begin(), this->components_.end());
73
74 // Initialize looping_components_ early so enable_pending_loops_() works during setup
76
77 for (uint32_t i = 0; i < this->components_.size(); i++) {
79
80 // Update loop_component_start_time_ before calling each component during setup
82 component->call();
83 this->scheduler.process_to_add();
84 this->feed_wdt();
85 if (component->can_proceed())
86 continue;
87
88 // Force the status LED to blink WARNING while we wait for a slow
89 // component to come up. Cleared after setup() finishes if no real
90 // component has warning set.
92
93 do {
94 uint32_t now = millis();
95
96 // Process pending loop enables to handle GPIO interrupts during setup
97 this->before_loop_tasks_(now);
98
99 for (uint32_t j = 0; j <= i; j++) {
100 // Update loop_component_start_time_ right before calling each component
102 this->components_[j]->call();
103 this->feed_wdt();
104 }
105
106 this->after_loop_tasks_();
107 yield();
108 } while (!component->can_proceed() && !component->is_failed());
109 }
110
111 // Setup is complete. Reconcile STATUS_LED_WARNING: the slow-setup path
112 // above may have forced it on, and any status_clear_warning() calls
113 // from components during setup were intentional no-ops (gated by
114 // APP_STATE_SETUP_COMPLETE). Walk components once here to pick up the
115 // real state. STATUS_LED_ERROR is never artificially forced, so its
116 // clear path always works and needs no reconciliation. Finally, set
117 // APP_STATE_SETUP_COMPLETE so subsequent warning clears go through
118 // the normal walk-and-clear path.
120 this->app_state_ &= ~STATUS_LED_WARNING;
122
123 ESP_LOGI(TAG, "setup() finished successfully!");
124
125#ifdef USE_SETUP_PRIORITY_OVERRIDE
126 // Clear setup priority overrides to free memory
128#endif
129
130#if defined(USE_ESP32) || defined(USE_LIBRETINY)
131 // Save main loop task handle for wake_loop_*() / fast select FreeRTOS notifications.
132 esphome_main_task_handle = xTaskGetCurrentTaskHandle();
133#endif
134#ifdef USE_HOST
135 // Set up wake socket for waking main loop from tasks (platforms without fast select only)
137#endif
138
139 // Ensure all active looping components are in LOOP state.
140 // Components after the last blocking component only got one call() during setup
141 // (CONSTRUCTION→SETUP) and never received the second call() (SETUP→LOOP).
142 // The main loop calls loop() directly, bypassing call()'s state machine.
143 for (uint16_t i = 0; i < this->looping_components_active_end_; i++) {
144 this->looping_components_[i]->set_component_state_(COMPONENT_STATE_LOOP);
145 }
146
147 this->schedule_dump_config();
148}
149
150void Application::process_dump_config_() {
151 if (this->dump_config_at_ == 0) {
152 char build_time_str[Application::BUILD_TIME_STR_SIZE];
153 this->get_build_time_string(build_time_str);
154 ESP_LOGI(TAG, "ESPHome version " ESPHOME_VERSION " compiled on %s", build_time_str);
155#ifdef ESPHOME_PROJECT_NAME
156 ESP_LOGI(TAG, "Project " ESPHOME_PROJECT_NAME " version " ESPHOME_PROJECT_VERSION);
157#endif
158#ifdef USE_ESP32
159 esp_chip_info_t chip_info;
160 esp_chip_info(&chip_info);
161 ESP_LOGI(TAG, "ESP32 Chip: %s rev%d.%d, %d core(s)", ESPHOME_VARIANT, chip_info.revision / 100,
162 chip_info.revision % 100, chip_info.cores);
163#if defined(USE_ESP32_VARIANT_ESP32) && (!defined(USE_ESP32_MIN_CHIP_REVISION_SET) || !defined(USE_ESP32_SRAM1_AS_IRAM))
164 static const char *const ESP32_ADVANCED_PATH = "under esp32 > framework > advanced";
165#endif
166#if defined(USE_ESP32_VARIANT_ESP32) && !defined(USE_ESP32_MIN_CHIP_REVISION_SET)
167 {
168 // Suggest optimization for chips that don't need the PSRAM cache workaround
169 if (chip_info.revision >= 300) {
170#ifdef USE_PSRAM
171 ESP_LOGW(TAG, "Chip rev >= 3.0 detected. Set minimum_chip_revision: \"%d.%d\" %s to save ~10KB IRAM",
172 chip_info.revision / 100, chip_info.revision % 100, ESP32_ADVANCED_PATH);
173#else
174 ESP_LOGW(TAG, "Chip rev >= 3.0 detected. Set minimum_chip_revision: \"%d.%d\" %s to reduce binary size",
175 chip_info.revision / 100, chip_info.revision % 100, ESP32_ADVANCED_PATH);
176#endif
177 }
178 }
179#endif
180 {
181 // esp_bootloader_desc_t is available in ESP-IDF >= 5.2; if readable the bootloader is modern.
182 //
183 // Design decision: We intentionally do NOT mention sram1_as_iram when the bootloader is too old.
184 // Enabling sram1_as_iram with an old bootloader causes a hard brick (device fails to boot,
185 // requires USB reflash to recover). Users don't always read warnings carefully, so we only
186 // suggest the option once we've confirmed the bootloader can handle it. In practice this
187 // means a user with an old bootloader may need to flash twice: once via USB to update the
188 // bootloader (they'll see the suggestion on next boot), then OTA with sram1_as_iram: true.
189 // Two flashes is a better outcome than a bricked device.
190 esp_bootloader_desc_t boot_desc;
191 if (esp_ota_get_bootloader_description(nullptr, &boot_desc) != ESP_OK) {
192#ifdef USE_ESP32_VARIANT_ESP32
193 ESP_LOGW(TAG, "Bootloader too old for OTA rollback and SRAM1 as IRAM (+40KB). "
194 "Flash via USB once to update the bootloader");
195#else
196 ESP_LOGW(TAG, "Bootloader too old for OTA rollback. Flash via USB once to update the bootloader");
197#endif
198 }
199#if defined(USE_ESP32_VARIANT_ESP32) && !defined(USE_ESP32_SRAM1_AS_IRAM)
200 else {
201 ESP_LOGW(TAG, "Bootloader supports SRAM1 as IRAM (+40KB). Set sram1_as_iram: true %s", ESP32_ADVANCED_PATH);
202 }
203#endif
204 }
205#endif // USE_ESP32
206 }
207
208 this->components_[this->dump_config_at_]->call_dump_config_();
209 this->dump_config_at_++;
210}
211
213 // Cold entry: callers without a millis() timestamp in hand. Fetches the
214 // time and takes the same rate-limit path as feed_wdt_with_time().
215 uint32_t now = millis();
216 if (now - this->last_wdt_feed_ > WDT_FEED_INTERVAL_MS) {
217 this->feed_wdt_slow_(now);
218 }
219}
220
222 // Callers (both feed_wdt() and feed_wdt_with_time()) have already
223 // confirmed the WDT_FEED_INTERVAL_MS rate limit was exceeded.
225 this->last_wdt_feed_ = time;
226#ifdef USE_STATUS_LED
227 if (status_led::global_status_led != nullptr) {
229 }
230#endif
231}
232
234 // Walk all components (not just looping ones) so non-looping components'
235 // status bits are respected. Only called from the slow-path clear helpers
236 // (status_clear_warning_slow_path_ / status_clear_error_slow_path_) on an
237 // actual set→clear transition, so walking O(N) here is paid once per
238 // transition — not once per loop iteration.
239 for (auto *component : this->components_) {
240 if ((component->get_component_state() & flag) != 0)
241 return true;
242 }
243 return false;
244}
245
247 ESP_LOGI(TAG, "Forcing a reboot");
248 for (auto &component : std::ranges::reverse_view(this->components_)) {
249 component->on_shutdown();
250 }
251 arch_restart();
252}
254 ESP_LOGI(TAG, "Rebooting safely");
256 teardown_components(TEARDOWN_TIMEOUT_REBOOT_MS);
258 arch_restart();
259}
260
262 for (auto &component : std::ranges::reverse_view(this->components_)) {
263 component->on_safe_shutdown();
264 }
265 for (auto &component : std::ranges::reverse_view(this->components_)) {
266 component->on_shutdown();
267 }
268}
269
271 for (auto &component : std::ranges::reverse_view(this->components_)) {
272 component->on_powerdown();
273 }
274}
275
277 uint32_t start_time = millis();
278
279 // Use a StaticVector instead of std::vector to avoid heap allocation
280 // since we know the actual size at compile time
282
283 // Copy all components in reverse order
284 // Reverse order matches the behavior of run_safe_shutdown_hooks() above and ensures
285 // components are torn down in the opposite order of their setup_priority (which is
286 // used to sort components during Application::setup())
287 size_t num_components = this->components_.size();
288 for (size_t i = 0; i < num_components; ++i) {
289 pending_components[i] = this->components_[num_components - 1 - i];
290 }
291
292 uint32_t now = start_time;
293 size_t pending_count = num_components;
294
295 // Teardown Algorithm
296 // ==================
297 // We iterate through pending components, calling teardown() on each.
298 // Components that return false (need more time) are copied forward
299 // in the array. Components that return true (finished) are skipped.
300 //
301 // The compaction happens in-place during iteration:
302 // - still_pending tracks the write position (where to put next pending component)
303 // - i tracks the read position (which component we're testing)
304 // - When teardown() returns false, we copy component[i] to component[still_pending]
305 // - When teardown() returns true, we just skip it (don't increment still_pending)
306 //
307 // Example with 4 components where B can teardown immediately:
308 //
309 // Start:
310 // pending_components: [A, B, C, D]
311 // pending_count: 4 ^----------^
312 //
313 // Iteration 1:
314 // i=0: A needs more time → keep at pos 0 (no copy needed)
315 // i=1: B finished → skip
316 // i=2: C needs more time → copy to pos 1
317 // i=3: D needs more time → copy to pos 2
318 //
319 // After iteration 1:
320 // pending_components: [A, C, D | D]
321 // pending_count: 3 ^--------^
322 //
323 // Iteration 2:
324 // i=0: A finished → skip
325 // i=1: C needs more time → copy to pos 0
326 // i=2: D finished → skip
327 //
328 // After iteration 2:
329 // pending_components: [C | C, D, D] (positions 1-3 have old values)
330 // pending_count: 1 ^--^
331
332 while (pending_count > 0 && (now - start_time) < timeout_ms) {
333 // Feed watchdog during teardown to prevent triggering
334 this->feed_wdt_with_time(now);
335
336 // Process components and compact the array, keeping only those still pending
337 size_t still_pending = 0;
338 for (size_t i = 0; i < pending_count; ++i) {
339 if (!pending_components[i]->teardown()) {
340 // Component still needs time, copy it forward
341 if (still_pending != i) {
342 pending_components[still_pending] = pending_components[i];
343 }
344 ++still_pending;
345 }
346 // Component finished teardown, skip it (don't increment still_pending)
347 }
348 pending_count = still_pending;
349
350 // Give some time for I/O operations if components are still pending
351 if (pending_count > 0) {
352 this->yield_with_select_(1);
353 }
354
355 // Update time for next iteration
356 now = millis();
357 }
358
359 if (pending_count > 0) {
360 // Note: At this point, connections are either disconnected or in a bad state,
361 // so this warning will only appear via serial rather than being transmitted to clients
362 for (size_t i = 0; i < pending_count; ++i) {
363 ESP_LOGW(TAG, "%s did not complete teardown within %" PRIu32 " ms",
364 LOG_STR_ARG(pending_components[i]->get_component_log_str()), timeout_ms);
365 }
366 }
367}
368
370 for (auto *obj : this->components_) {
371 if (obj->has_overridden_loop() &&
372 ((obj->get_component_state() & COMPONENT_STATE_MASK) == COMPONENT_STATE_LOOP_DONE) == match_loop_done) {
373 this->looping_components_.push_back(obj);
374 }
375 }
376}
377
379 // This method must be reentrant - components can disable themselves during their own loop() call
380 // Linear search to find component in active section
381 // Most configs have 10-30 looping components (30 is on the high end)
382 // O(n) is acceptable here as we optimize for memory, not complexity
383 for (uint16_t i = 0; i < this->looping_components_active_end_; i++) {
384 if (this->looping_components_[i] == component) {
385 // Move last active component to this position
386 this->looping_components_active_end_--;
387 if (i != this->looping_components_active_end_) {
388 std::swap(this->looping_components_[i], this->looping_components_[this->looping_components_active_end_]);
389
390 // If we're currently iterating and just swapped the current position
391 if (this->in_loop_ && i == this->current_loop_index_) {
392 // Decrement so we'll process the swapped component next
393 this->current_loop_index_--;
394 // Update the loop start time to current time so the swapped component
395 // gets correct timing instead of inheriting stale timing.
396 // This prevents integer underflow in timing calculations by ensuring
397 // the swapped component starts with a fresh timing reference, avoiding
398 // errors caused by stale or wrapped timing values.
400 }
401 }
402 return;
403 }
404 }
405}
406
408 // Helper to move component from inactive to active section
409 if (index != this->looping_components_active_end_) {
410 std::swap(this->looping_components_[index], this->looping_components_[this->looping_components_active_end_]);
411 }
413}
414
416 // This method is only called when component state is LOOP_DONE, so we know
417 // the component must be in the inactive section (if it exists in looping_components_)
418 // Only search the inactive portion for better performance
419 // With typical 0-5 inactive components, O(k) is much faster than O(n)
420 const uint16_t size = this->looping_components_.size();
421 for (uint16_t i = this->looping_components_active_end_; i < size; i++) {
422 if (this->looping_components_[i] == component) {
423 // Found in inactive section - move to active
425 return;
426 }
427 }
428 // Component not found in looping_components_ - this is normal for components
429 // that don't have loop() or were not included in the partitioned vector
430}
431
433 // Process components that requested enable_loop from ISR context
434 // Only iterate through inactive looping_components_ (typically 0-5) instead of all components
435 //
436 // Race condition handling:
437 // 1. We check if component is already in LOOP state first - if so, just clear the flag
438 // This handles reentrancy where enable_loop() was called between ISR and processing
439 // 2. We only clear pending_enable_loop_ after checking state, preventing lost requests
440 // 3. If any components aren't in LOOP_DONE state, we set has_pending_enable_loop_requests_
441 // back to true to ensure we check again next iteration
442 // 4. ISRs can safely set flags at any time - worst case is we process them next iteration
443 // 5. The global flag (has_pending_enable_loop_requests_) is cleared before this method,
444 // so any ISR that fires during processing will be caught in the next loop
445 const uint16_t size = this->looping_components_.size();
446 bool has_pending = false;
447
448 for (uint16_t i = this->looping_components_active_end_; i < size; i++) {
450 if (!component->pending_enable_loop_) {
451 continue; // Skip components without pending requests
452 }
453
454 // Check current state
456
457 // If already in LOOP state, nothing to do - clear flag and continue
459 component->pending_enable_loop_ = false;
460 continue;
461 }
462
463 // If not in LOOP_DONE state, can't enable yet - keep flag set
465 has_pending = true; // Keep tracking this component
466 continue; // Keep the flag set - try again next iteration
467 }
468
469 // Clear the pending flag and enable the loop
470 component->pending_enable_loop_ = false;
471 ESP_LOGVV(TAG, "%s loop enabled from ISR", LOG_STR_ARG(component->get_component_log_str()));
472 component->set_component_state_(COMPONENT_STATE_LOOP);
473
474 // Move to active section
476 }
477
478 // If we couldn't process some requests, ensure we check again next iteration
479 if (has_pending) {
481 }
482}
483
484#ifdef USE_LWIP_FAST_SELECT
485bool Application::register_socket(struct lwip_sock *sock) {
486 // It modifies monitored_sockets_ without locking — must only be called from the main loop.
487 if (sock == nullptr)
488 return false;
490 this->monitored_sockets_.push_back(sock);
491 return true;
492}
493
494void Application::unregister_socket(struct lwip_sock *sock) {
495 // It modifies monitored_sockets_ without locking — must only be called from the main loop.
496 for (size_t i = 0; i < this->monitored_sockets_.size(); i++) {
497 if (this->monitored_sockets_[i] != sock)
498 continue;
499
500 // Swap with last element and pop - O(1) removal since order doesn't matter.
501 // No need to unhook the netconn callback — all LwIP sockets share the same
502 // static event_callback, and the socket will be closed by the caller.
503 if (i < this->monitored_sockets_.size() - 1)
504 this->monitored_sockets_[i] = this->monitored_sockets_.back();
505 this->monitored_sockets_.pop_back();
506 return;
507 }
508}
509#elif defined(USE_HOST)
511 // WARNING: This function is NOT thread-safe and must only be called from the main loop
512 // It modifies socket_fds_ and related variables without locking
513 if (fd < 0)
514 return false;
515
516 if (fd >= FD_SETSIZE) {
517 ESP_LOGE(TAG, "fd %d exceeds FD_SETSIZE %d", fd, FD_SETSIZE);
518 return false;
519 }
520
521 this->socket_fds_.push_back(fd);
522 this->socket_fds_changed_ = true;
523 if (fd > this->max_fd_) {
524 this->max_fd_ = fd;
525 }
526
527 return true;
528}
529
531 // WARNING: This function is NOT thread-safe and must only be called from the main loop
532 // It modifies socket_fds_ and related variables without locking
533 if (fd < 0)
534 return;
535
536 for (size_t i = 0; i < this->socket_fds_.size(); i++) {
537 if (this->socket_fds_[i] != fd)
538 continue;
539
540 // Swap with last element and pop - O(1) removal since order doesn't matter.
541 if (i < this->socket_fds_.size() - 1)
542 this->socket_fds_[i] = this->socket_fds_.back();
543 this->socket_fds_.pop_back();
544 this->socket_fds_changed_ = true;
545 // Only recalculate max_fd if we removed the current max
546 if (fd == this->max_fd_) {
547 this->max_fd_ = -1;
548 for (int sock_fd : this->socket_fds_) {
549 if (sock_fd > this->max_fd_)
550 this->max_fd_ = sock_fd;
551 }
552 }
553 return;
554 }
555}
556
557#endif
558
559// Only the select() fallback path remains in the .cpp — all other paths are inlined in application.h
560#ifdef USE_HOST
562 // Fallback select() path (host platform and any future platforms without fast select).
563 if (!this->socket_fds_.empty()) [[likely]] {
564 // Update fd_set if socket list has changed
565 if (this->socket_fds_changed_) [[unlikely]] {
566 FD_ZERO(&this->base_read_fds_);
567 // fd bounds are validated in register_socket_fd()
568 for (int fd : this->socket_fds_) {
569 FD_SET(fd, &this->base_read_fds_);
570 }
571 this->socket_fds_changed_ = false;
572 }
573
574 // Copy base fd_set before each select
575 this->read_fds_ = this->base_read_fds_;
576
577 // Convert delay_ms to timeval
578 struct timeval tv;
579 tv.tv_sec = delay_ms / 1000;
580 tv.tv_usec = (delay_ms - tv.tv_sec * 1000) * 1000;
581
582 // Call select with timeout
583 int ret = ::select(this->max_fd_ + 1, &this->read_fds_, nullptr, nullptr, &tv);
584
585 // Process select() result:
586 // ret > 0: socket(s) have data ready - normal and expected
587 // ret == 0: timeout occurred - normal and expected
588 if (ret >= 0) [[likely]] {
589 // Yield if zero timeout since select(0) only polls without yielding
590 if (delay_ms == 0) [[unlikely]] {
591 yield();
592 }
593 return;
594 }
595 // ret < 0: error (EINTR is normal, anything else is unexpected)
596 const int err = errno;
597 if (err == EINTR) {
598 return;
599 }
600 // select() error - log and fall through to delay()
601 ESP_LOGW(TAG, "select() failed with errno %d", err);
602 }
603 // No sockets registered or select() failed - use regular delay
604 delay(delay_ms);
605}
606#endif // USE_HOST
607
608// App storage — asm label shares the linker symbol with "extern Application App".
609// char[] is trivially destructible, so no __cxa_atexit or destructor chain is emitted.
610// Constructed via placement new in the generated setup().
611#ifndef __GXX_ABI_VERSION
612#error "Application placement new requires Itanium C++ ABI (GCC/Clang)"
613#endif
614static_assert(std::is_default_constructible<Application>::value, "Application must be default-constructible");
615// __USER_LABEL_PREFIX__ is "_" on Mach-O (macOS) and empty on ELF (embedded targets).
616// String literal concatenation produces the correct platform-specific mangled symbol.
617// Two-level macro needed: # stringifies before expansion, so the
618// indirection forces __USER_LABEL_PREFIX__ to expand first.
619#define ESPHOME_STRINGIFY_IMPL_(x) #x
620#define ESPHOME_STRINGIFY_(x) ESPHOME_STRINGIFY_IMPL_(x)
621// NOLINTNEXTLINE(cppcoreguidelines-avoid-non-const-global-variables)
622alignas(Application) char app_storage[sizeof(Application)] asm(
623 ESPHOME_STRINGIFY_(__USER_LABEL_PREFIX__) "_ZN7esphome3AppE");
624#undef ESPHOME_STRINGIFY_
625#undef ESPHOME_STRINGIFY_IMPL_
626
627// Host platform wake_loop_threadsafe() and setup — needs wake_socket_fd_
628// ESP32/LibreTiny/ESP8266/RP2040 implementations are in wake.cpp
629#ifdef USE_HOST
630
632 // Create UDP socket for wake notifications
633 this->wake_socket_fd_ = ::socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP);
634 if (this->wake_socket_fd_ < 0) {
635 ESP_LOGW(TAG, "Wake socket create failed: %d", errno);
636 return;
637 }
638
639 // Bind to loopback with auto-assigned port
640 struct sockaddr_in addr = {};
641 addr.sin_family = AF_INET;
642 addr.sin_addr.s_addr = htonl(INADDR_LOOPBACK);
643 addr.sin_port = 0; // Auto-assign port
644
645 if (::bind(this->wake_socket_fd_, (struct sockaddr *) &addr, sizeof(addr)) < 0) {
646 ESP_LOGW(TAG, "Wake socket bind failed: %d", errno);
647 ::close(this->wake_socket_fd_);
648 this->wake_socket_fd_ = -1;
649 return;
650 }
651
652 // Get the assigned address and connect to it
653 // Connecting a UDP socket allows using send() instead of sendto() for better performance
654 struct sockaddr_in wake_addr;
655 socklen_t len = sizeof(wake_addr);
656 if (::getsockname(this->wake_socket_fd_, (struct sockaddr *) &wake_addr, &len) < 0) {
657 ESP_LOGW(TAG, "Wake socket address failed: %d", errno);
658 ::close(this->wake_socket_fd_);
659 this->wake_socket_fd_ = -1;
660 return;
661 }
662
663 // Connect to self (loopback) - allows using send() instead of sendto()
664 // After connect(), no need to store wake_addr - the socket remembers it
665 if (::connect(this->wake_socket_fd_, (struct sockaddr *) &wake_addr, sizeof(wake_addr)) < 0) {
666 ESP_LOGW(TAG, "Wake socket connect failed: %d", errno);
667 ::close(this->wake_socket_fd_);
668 this->wake_socket_fd_ = -1;
669 return;
670 }
671
672 // Set non-blocking mode
673 int flags = ::fcntl(this->wake_socket_fd_, F_GETFL, 0);
674 ::fcntl(this->wake_socket_fd_, F_SETFL, flags | O_NONBLOCK);
675
676 // Register with application's select() loop
677 if (!this->register_socket_fd(this->wake_socket_fd_)) {
678 ESP_LOGW(TAG, "Wake socket register failed");
679 ::close(this->wake_socket_fd_);
680 this->wake_socket_fd_ = -1;
681 return;
682 }
683}
684
685#endif // USE_HOST
686
687void Application::get_build_time_string(std::span<char, BUILD_TIME_STR_SIZE> buffer) {
688 ESPHOME_strncpy_P(buffer.data(), ESPHOME_BUILD_TIME_STR, buffer.size());
689 buffer[buffer.size() - 1] = '\0';
690}
691
692void Application::get_comment_string(std::span<char, ESPHOME_COMMENT_SIZE_MAX> buffer) {
693 ESPHOME_strncpy_P(buffer.data(), ESPHOME_COMMENT_STR, ESPHOME_COMMENT_SIZE);
694 buffer[ESPHOME_COMMENT_SIZE - 1] = '\0';
695}
696
697uint32_t Application::get_config_hash() { return ESPHOME_CONFIG_HASH; }
698
699uint32_t Application::get_config_version_hash() { return fnv1a_hash_extend(ESPHOME_CONFIG_HASH, ESPHOME_VERSION); }
700
701time_t Application::get_build_time() { return ESPHOME_BUILD_TIME; }
702
703} // namespace esphome
std::vector< struct lwip_sock * > monitored_sockets_
void setup()
Reserve space for components to avoid memory fragmentation.
uint32_t ESPHOME_ALWAYS_INLINE before_loop_tasks_(uint32_t loop_start_time)
void ESPHOME_ALWAYS_INLINE feed_wdt_with_time(uint32_t time)
Feed the task watchdog, hot entry.
uint16_t looping_components_active_end_
void yield_with_select_(uint32_t delay_ms)
Perform a delay while also monitoring socket file descriptors for readiness.
bool register_socket(struct lwip_sock *sock)
Register/unregister a socket to be monitored for read events.
static constexpr size_t BUILD_TIME_STR_SIZE
Size of buffer required for build time string (including null terminator)
uint32_t get_config_hash()
Get the config hash as a 32-bit integer.
std::vector< int > socket_fds_
StaticVector< Component *, ESPHOME_COMPONENT_COUNT > components_
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 enable_component_loop_(Component *component)
uint32_t loop_component_start_time_
void feed_wdt()
Feed the task watchdog.
void disable_component_loop_(Component *component)
void activate_looping_component_(uint16_t index)
static constexpr uint32_t WDT_FEED_INTERVAL_MS
Minimum interval between real arch_feed_wdt() calls.
void ESPHOME_ALWAYS_INLINE after_loop_tasks_()
void teardown_components(uint32_t timeout_ms)
Teardown all components with a timeout.
FixedVector< Component * > looping_components_
void add_looping_components_by_state_(bool match_loop_done)
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.
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...
uint16_t current_loop_index_
time_t get_build_time()
Get the build time as a Unix timestamp.
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.
void calculate_looping_components_()
void register_component_impl_(Component *comp, bool has_loop)
void unregister_socket(struct lwip_sock *sock)
float get_actual_setup_priority() const
virtual bool can_proceed()
uint8_t component_state_
State of this component - each bit has a purpose: Bits 0-2: Component state (0x00=CONSTRUCTION,...
Definition component.h:587
Minimal static vector - saves memory by avoiding std::vector overhead.
Definition helpers.h:210
size_t size() const
Definition helpers.h:270
const Component * component
Definition component.cpp:34
uint16_t flags
bool state
Definition fan.h:2
uint32_t socklen_t
Definition headers.h:99
void esphome_lwip_hook_socket(struct lwip_sock *sock)
Hook a socket's netconn callback to notify the main loop task on receive events.
TaskHandle_t esphome_main_task_handle
Main loop task handle and wake helpers — shared between wake.h (C++) and lwip_fast_select....
Definition main_task.c:4
const char *const TAG
Definition spi.cpp:7
StatusLED * global_status_led
Providing packet encoding functions for exchanging data with a remote host.
Definition a01nyub.cpp:7
constexpr uint8_t COMPONENT_HAS_LOOP
Definition component.h:91
constexpr uint32_t fnv1a_hash_extend(uint32_t hash, const char *str)
Extend a FNV-1a hash with additional string data.
Definition helpers.h:799
constexpr uint8_t COMPONENT_STATE_LOOP
Definition component.h:82
constexpr uint8_t APP_STATE_SETUP_COMPLETE
Definition component.h:96
constexpr uint8_t STATUS_LED_WARNING
Definition component.h:88
constexpr uint8_t COMPONENT_STATE_MASK
Definition component.h:79
std::string size_t len
Definition helpers.h:1045
uint16_t size
Definition helpers.cpp:25
void clear_setup_priority_overrides()
void HOT yield()
Definition core.cpp:25
void HOT arch_feed_wdt()
Definition core.cpp:54
constexpr uint8_t COMPONENT_STATE_LOOP_DONE
Definition component.h:84
void HOT delay(uint32_t ms)
Definition core.cpp:28
uint32_t IRAM_ATTR HOT millis()
Definition core.cpp:26
void arch_restart()
Definition core.cpp:31
static void uint32_t
struct in_addr sin_addr
Definition headers.h:67
sa_family_t sin_family
Definition headers.h:65
in_port_t sin_port
Definition headers.h:66