ESPHome 2026.5.0-dev
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 uint8_t sl_state = sl->get_component_state() & COMPONENT_STATE_MASK;
230 if (sl_state == COMPONENT_STATE_LOOP_DONE) {
231 // status_led only transitions to LOOP_DONE from inside its own loop() (after the
232 // first idle-path dispatch), so its pin is already initialized by pre_setup() and
233 // its setup() has already run. Re-dispatch only if an error or warning bit has been
234 // set since; otherwise skip entirely.
235 if ((this->app_state_ & STATUS_LED_MASK) == 0)
236 return;
237 sl->enable_loop();
238 } else if (sl_state != COMPONENT_STATE_LOOP) {
239 // CONSTRUCTION/SETUP/FAILED: not our job — App::setup() drives the lifecycle.
240 return;
241 }
242 sl->loop();
243 }
244#endif
245}
246
248 // Walk all components (not just looping ones) so non-looping components'
249 // status bits are respected. Only called from the slow-path clear helpers
250 // (status_clear_warning_slow_path_ / status_clear_error_slow_path_) on an
251 // actual set→clear transition, so walking O(N) here is paid once per
252 // transition — not once per loop iteration.
253 for (auto *component : this->components_) {
254 if ((component->get_component_state() & flag) != 0)
255 return true;
256 }
257 return false;
258}
259
261 ESP_LOGI(TAG, "Forcing a reboot");
262 for (auto &component : std::ranges::reverse_view(this->components_)) {
263 component->on_shutdown();
264 }
265 arch_restart();
266}
268 ESP_LOGI(TAG, "Rebooting safely");
270 teardown_components(TEARDOWN_TIMEOUT_REBOOT_MS);
272 arch_restart();
273}
274
276 for (auto &component : std::ranges::reverse_view(this->components_)) {
277 component->on_safe_shutdown();
278 }
279 for (auto &component : std::ranges::reverse_view(this->components_)) {
280 component->on_shutdown();
281 }
282}
283
285 for (auto &component : std::ranges::reverse_view(this->components_)) {
286 component->on_powerdown();
287 }
288}
289
291 uint32_t start_time = millis();
292
293 // Use a StaticVector instead of std::vector to avoid heap allocation
294 // since we know the actual size at compile time
296
297 // Copy all components in reverse order
298 // Reverse order matches the behavior of run_safe_shutdown_hooks() above and ensures
299 // components are torn down in the opposite order of their setup_priority (which is
300 // used to sort components during Application::setup())
301 size_t num_components = this->components_.size();
302 for (size_t i = 0; i < num_components; ++i) {
303 pending_components[i] = this->components_[num_components - 1 - i];
304 }
305
306 uint32_t now = start_time;
307 size_t pending_count = num_components;
308
309 // Teardown Algorithm
310 // ==================
311 // We iterate through pending components, calling teardown() on each.
312 // Components that return false (need more time) are copied forward
313 // in the array. Components that return true (finished) are skipped.
314 //
315 // The compaction happens in-place during iteration:
316 // - still_pending tracks the write position (where to put next pending component)
317 // - i tracks the read position (which component we're testing)
318 // - When teardown() returns false, we copy component[i] to component[still_pending]
319 // - When teardown() returns true, we just skip it (don't increment still_pending)
320 //
321 // Example with 4 components where B can teardown immediately:
322 //
323 // Start:
324 // pending_components: [A, B, C, D]
325 // pending_count: 4 ^----------^
326 //
327 // Iteration 1:
328 // i=0: A needs more time → keep at pos 0 (no copy needed)
329 // i=1: B finished → skip
330 // i=2: C needs more time → copy to pos 1
331 // i=3: D needs more time → copy to pos 2
332 //
333 // After iteration 1:
334 // pending_components: [A, C, D | D]
335 // pending_count: 3 ^--------^
336 //
337 // Iteration 2:
338 // i=0: A finished → skip
339 // i=1: C needs more time → copy to pos 0
340 // i=2: D finished → skip
341 //
342 // After iteration 2:
343 // pending_components: [C | C, D, D] (positions 1-3 have old values)
344 // pending_count: 1 ^--^
345
346 while (pending_count > 0 && (now - start_time) < timeout_ms) {
347 // Feed watchdog during teardown to prevent triggering
348 this->feed_wdt_with_time(now);
349
350 // Process components and compact the array, keeping only those still pending
351 size_t still_pending = 0;
352 for (size_t i = 0; i < pending_count; ++i) {
353 if (!pending_components[i]->teardown()) {
354 // Component still needs time, copy it forward
355 if (still_pending != i) {
356 pending_components[still_pending] = pending_components[i];
357 }
358 ++still_pending;
359 }
360 // Component finished teardown, skip it (don't increment still_pending)
361 }
362 pending_count = still_pending;
363
364 // Give some time for I/O operations if components are still pending
365 if (pending_count > 0) {
366 this->yield_with_select_(1);
367 }
368
369 // Update time for next iteration
370 now = millis();
371 }
372
373 if (pending_count > 0) {
374 // Note: At this point, connections are either disconnected or in a bad state,
375 // so this warning will only appear via serial rather than being transmitted to clients
376 for (size_t i = 0; i < pending_count; ++i) {
377 ESP_LOGW(TAG, "%s did not complete teardown within %" PRIu32 " ms",
378 LOG_STR_ARG(pending_components[i]->get_component_log_str()), timeout_ms);
379 }
380 }
381}
382
384 for (auto *obj : this->components_) {
385 if (obj->has_overridden_loop() &&
386 ((obj->get_component_state() & COMPONENT_STATE_MASK) == COMPONENT_STATE_LOOP_DONE) == match_loop_done) {
387 this->looping_components_.push_back(obj);
388 }
389 }
390}
391
393 // This method must be reentrant - components can disable themselves during their own loop() call
394 // Linear search to find component in active section
395 // Most configs have 10-30 looping components (30 is on the high end)
396 // O(n) is acceptable here as we optimize for memory, not complexity
397 for (uint16_t i = 0; i < this->looping_components_active_end_; i++) {
398 if (this->looping_components_[i] == component) {
399 // Move last active component to this position
400 this->looping_components_active_end_--;
401 if (i != this->looping_components_active_end_) {
402 std::swap(this->looping_components_[i], this->looping_components_[this->looping_components_active_end_]);
403
404 // If we're currently iterating and just swapped the current position
405 if (this->in_loop_ && i == this->current_loop_index_) {
406 // Decrement so we'll process the swapped component next
407 this->current_loop_index_--;
408 // Update the loop start time to current time so the swapped component
409 // gets correct timing instead of inheriting stale timing.
410 // This prevents integer underflow in timing calculations by ensuring
411 // the swapped component starts with a fresh timing reference, avoiding
412 // errors caused by stale or wrapped timing values.
414 }
415 }
416 return;
417 }
418 }
419}
420
422 // Helper to move component from inactive to active section
423 if (index != this->looping_components_active_end_) {
424 std::swap(this->looping_components_[index], this->looping_components_[this->looping_components_active_end_]);
425 }
427}
428
430 // This method is only called when component state is LOOP_DONE, so we know
431 // the component must be in the inactive section (if it exists in looping_components_)
432 // Only search the inactive portion for better performance
433 // With typical 0-5 inactive components, O(k) is much faster than O(n)
434 const uint16_t size = this->looping_components_.size();
435 for (uint16_t i = this->looping_components_active_end_; i < size; i++) {
436 if (this->looping_components_[i] == component) {
437 // Found in inactive section - move to active
439 return;
440 }
441 }
442 // Component not found in looping_components_ - this is normal for components
443 // that don't have loop() or were not included in the partitioned vector
444}
445
447 // Process components that requested enable_loop from ISR context
448 // Only iterate through inactive looping_components_ (typically 0-5) instead of all components
449 //
450 // Race condition handling:
451 // 1. We check if component is already in LOOP state first - if so, just clear the flag
452 // This handles reentrancy where enable_loop() was called between ISR and processing
453 // 2. We only clear pending_enable_loop_ after checking state, preventing lost requests
454 // 3. If any components aren't in LOOP_DONE state, we set has_pending_enable_loop_requests_
455 // back to true to ensure we check again next iteration
456 // 4. ISRs can safely set flags at any time - worst case is we process them next iteration
457 // 5. The global flag (has_pending_enable_loop_requests_) is cleared before this method,
458 // so any ISR that fires during processing will be caught in the next loop
459 const uint16_t size = this->looping_components_.size();
460 bool has_pending = false;
461
462 for (uint16_t i = this->looping_components_active_end_; i < size; i++) {
464 if (!component->pending_enable_loop_) {
465 continue; // Skip components without pending requests
466 }
467
468 // Check current state
470
471 // If already in LOOP state, nothing to do - clear flag and continue
473 component->pending_enable_loop_ = false;
474 continue;
475 }
476
477 // If not in LOOP_DONE state, can't enable yet - keep flag set
479 has_pending = true; // Keep tracking this component
480 continue; // Keep the flag set - try again next iteration
481 }
482
483 // Clear the pending flag and enable the loop
484 component->pending_enable_loop_ = false;
485 ESP_LOGVV(TAG, "%s loop enabled from ISR", LOG_STR_ARG(component->get_component_log_str()));
486 component->set_component_state_(COMPONENT_STATE_LOOP);
487
488 // Move to active section
490 }
491
492 // If we couldn't process some requests, ensure we check again next iteration
493 if (has_pending) {
495 }
496}
497
498#ifdef USE_LWIP_FAST_SELECT
499bool Application::register_socket(struct lwip_sock *sock) {
500 // It modifies monitored_sockets_ without locking — must only be called from the main loop.
501 if (sock == nullptr)
502 return false;
504 this->monitored_sockets_.push_back(sock);
505 return true;
506}
507
508void Application::unregister_socket(struct lwip_sock *sock) {
509 // It modifies monitored_sockets_ without locking — must only be called from the main loop.
510 for (size_t i = 0; i < this->monitored_sockets_.size(); i++) {
511 if (this->monitored_sockets_[i] != sock)
512 continue;
513
514 // Swap with last element and pop - O(1) removal since order doesn't matter.
515 // No need to unhook the netconn callback — all LwIP sockets share the same
516 // static event_callback, and the socket will be closed by the caller.
517 if (i < this->monitored_sockets_.size() - 1)
518 this->monitored_sockets_[i] = this->monitored_sockets_.back();
519 this->monitored_sockets_.pop_back();
520 return;
521 }
522}
523#elif defined(USE_HOST)
525 // WARNING: This function is NOT thread-safe and must only be called from the main loop
526 // It modifies socket_fds_ and related variables without locking
527 if (fd < 0)
528 return false;
529
530 if (fd >= FD_SETSIZE) {
531 ESP_LOGE(TAG, "fd %d exceeds FD_SETSIZE %d", fd, FD_SETSIZE);
532 return false;
533 }
534
535 this->socket_fds_.push_back(fd);
536 this->socket_fds_changed_ = true;
537 if (fd > this->max_fd_) {
538 this->max_fd_ = fd;
539 }
540
541 return true;
542}
543
545 // WARNING: This function is NOT thread-safe and must only be called from the main loop
546 // It modifies socket_fds_ and related variables without locking
547 if (fd < 0)
548 return;
549
550 for (size_t i = 0; i < this->socket_fds_.size(); i++) {
551 if (this->socket_fds_[i] != fd)
552 continue;
553
554 // Swap with last element and pop - O(1) removal since order doesn't matter.
555 if (i < this->socket_fds_.size() - 1)
556 this->socket_fds_[i] = this->socket_fds_.back();
557 this->socket_fds_.pop_back();
558 this->socket_fds_changed_ = true;
559 // Only recalculate max_fd if we removed the current max
560 if (fd == this->max_fd_) {
561 this->max_fd_ = -1;
562 for (int sock_fd : this->socket_fds_) {
563 if (sock_fd > this->max_fd_)
564 this->max_fd_ = sock_fd;
565 }
566 }
567 return;
568 }
569}
570
571#endif
572
573// Only the select() fallback path remains in the .cpp — all other paths are inlined in application.h
574#ifdef USE_HOST
576 // Fallback select() path (host platform and any future platforms without fast select).
577 if (!this->socket_fds_.empty()) [[likely]] {
578 // Update fd_set if socket list has changed
579 if (this->socket_fds_changed_) [[unlikely]] {
580 FD_ZERO(&this->base_read_fds_);
581 // fd bounds are validated in register_socket_fd()
582 for (int fd : this->socket_fds_) {
583 FD_SET(fd, &this->base_read_fds_);
584 }
585 this->socket_fds_changed_ = false;
586 }
587
588 // Copy base fd_set before each select
589 this->read_fds_ = this->base_read_fds_;
590
591 // Convert delay_ms to timeval
592 struct timeval tv;
593 tv.tv_sec = delay_ms / 1000;
594 tv.tv_usec = (delay_ms - tv.tv_sec * 1000) * 1000;
595
596 // Call select with timeout
597 int ret = ::select(this->max_fd_ + 1, &this->read_fds_, nullptr, nullptr, &tv);
598
599 // Process select() result:
600 // ret > 0: socket(s) have data ready - normal and expected
601 // ret == 0: timeout occurred - normal and expected
602 if (ret >= 0) [[likely]] {
603 // Yield if zero timeout since select(0) only polls without yielding
604 if (delay_ms == 0) [[unlikely]] {
605 yield();
606 }
607 return;
608 }
609 // ret < 0: error (EINTR is normal, anything else is unexpected)
610 const int err = errno;
611 if (err == EINTR) {
612 return;
613 }
614 // select() error - log and fall through to delay()
615 ESP_LOGW(TAG, "select() failed with errno %d", err);
616 }
617 // No sockets registered or select() failed - use regular delay
618 delay(delay_ms);
619}
620#endif // USE_HOST
621
622// App storage — asm label shares the linker symbol with "extern Application App".
623// char[] is trivially destructible, so no __cxa_atexit or destructor chain is emitted.
624// Constructed via placement new in the generated setup().
625#ifndef __GXX_ABI_VERSION
626#error "Application placement new requires Itanium C++ ABI (GCC/Clang)"
627#endif
628static_assert(std::is_default_constructible<Application>::value, "Application must be default-constructible");
629// __USER_LABEL_PREFIX__ is "_" on Mach-O (macOS) and empty on ELF (embedded targets).
630// String literal concatenation produces the correct platform-specific mangled symbol.
631// Two-level macro needed: # stringifies before expansion, so the
632// indirection forces __USER_LABEL_PREFIX__ to expand first.
633#define ESPHOME_STRINGIFY_IMPL_(x) #x
634#define ESPHOME_STRINGIFY_(x) ESPHOME_STRINGIFY_IMPL_(x)
635// NOLINTNEXTLINE(cppcoreguidelines-avoid-non-const-global-variables)
636alignas(Application) char app_storage[sizeof(Application)] asm(
637 ESPHOME_STRINGIFY_(__USER_LABEL_PREFIX__) "_ZN7esphome3AppE");
638#undef ESPHOME_STRINGIFY_
639#undef ESPHOME_STRINGIFY_IMPL_
640
641// Host platform wake_loop_threadsafe() and setup — needs wake_socket_fd_
642// ESP32/LibreTiny/ESP8266/RP2040 implementations are in wake.cpp
643#ifdef USE_HOST
644
646 // Create UDP socket for wake notifications
647 this->wake_socket_fd_ = ::socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP);
648 if (this->wake_socket_fd_ < 0) {
649 ESP_LOGW(TAG, "Wake socket create failed: %d", errno);
650 return;
651 }
652
653 // Bind to loopback with auto-assigned port
654 struct sockaddr_in addr = {};
655 addr.sin_family = AF_INET;
656 addr.sin_addr.s_addr = htonl(INADDR_LOOPBACK);
657 addr.sin_port = 0; // Auto-assign port
658
659 if (::bind(this->wake_socket_fd_, (struct sockaddr *) &addr, sizeof(addr)) < 0) {
660 ESP_LOGW(TAG, "Wake socket bind failed: %d", errno);
661 ::close(this->wake_socket_fd_);
662 this->wake_socket_fd_ = -1;
663 return;
664 }
665
666 // Get the assigned address and connect to it
667 // Connecting a UDP socket allows using send() instead of sendto() for better performance
668 struct sockaddr_in wake_addr;
669 socklen_t len = sizeof(wake_addr);
670 if (::getsockname(this->wake_socket_fd_, (struct sockaddr *) &wake_addr, &len) < 0) {
671 ESP_LOGW(TAG, "Wake socket address failed: %d", errno);
672 ::close(this->wake_socket_fd_);
673 this->wake_socket_fd_ = -1;
674 return;
675 }
676
677 // Connect to self (loopback) - allows using send() instead of sendto()
678 // After connect(), no need to store wake_addr - the socket remembers it
679 if (::connect(this->wake_socket_fd_, (struct sockaddr *) &wake_addr, sizeof(wake_addr)) < 0) {
680 ESP_LOGW(TAG, "Wake socket connect failed: %d", errno);
681 ::close(this->wake_socket_fd_);
682 this->wake_socket_fd_ = -1;
683 return;
684 }
685
686 // Set non-blocking mode
687 int flags = ::fcntl(this->wake_socket_fd_, F_GETFL, 0);
688 ::fcntl(this->wake_socket_fd_, F_SETFL, flags | O_NONBLOCK);
689
690 // Register with application's select() loop
691 if (!this->register_socket_fd(this->wake_socket_fd_)) {
692 ESP_LOGW(TAG, "Wake socket register failed");
693 ::close(this->wake_socket_fd_);
694 this->wake_socket_fd_ = -1;
695 return;
696 }
697}
698
699#endif // USE_HOST
700
701void Application::get_build_time_string(std::span<char, BUILD_TIME_STR_SIZE> buffer) {
702 ESPHOME_strncpy_P(buffer.data(), ESPHOME_BUILD_TIME_STR, buffer.size());
703 buffer[buffer.size() - 1] = '\0';
704}
705
706void Application::get_comment_string(std::span<char, ESPHOME_COMMENT_SIZE_MAX> buffer) {
707 ESPHOME_strncpy_P(buffer.data(), ESPHOME_COMMENT_STR, ESPHOME_COMMENT_SIZE);
708 buffer[ESPHOME_COMMENT_SIZE - 1] = '\0';
709}
710
711uint32_t Application::get_config_hash() { return ESPHOME_CONFIG_HASH; }
712
713uint32_t Application::get_config_version_hash() { return fnv1a_hash_extend(ESPHOME_CONFIG_HASH, ESPHOME_VERSION); }
714
715time_t Application::get_build_time() { return ESPHOME_BUILD_TIME; }
716
717} // namespace esphome
std::vector< struct lwip_sock * > monitored_sockets_
void setup()
Reserve space for components to avoid memory fragmentation.
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 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.
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
uint8_t get_component_state() const
Definition component.h:193
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 STATUS_LED_MASK
Definition component.h:86
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