11#include <esp_chip_info.h>
12#include <esp_ota_ops.h>
13#include <esp_bootloader_desc.h>
15#ifdef USE_LWIP_FAST_SELECT
27#if (defined(USE_ESP8266) || defined(USE_RP2040)) && defined(USE_SOCKET_IMPL_LWIP_TCP)
37static const char *
const TAG =
"app";
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) {
48 float key_priority = (key->*GetPriority)();
52 while (j >= first && ((*j)->*GetPriority)() < key_priority) {
67 ESP_LOGI(TAG,
"Running through setup()");
68 ESP_LOGV(TAG,
"Sorting components by setup priority");
83 this->scheduler.process_to_add();
99 new_app_state |= this->
components_[j]->get_component_state();
110 ESP_LOGI(TAG,
"setup() finished successfully!");
112#ifdef USE_SETUP_PRIORITY_OVERRIDE
117#if defined(USE_ESP32) || defined(USE_LIBRETINY)
137void Application::process_dump_config_() {
141 ESP_LOGI(TAG,
"ESPHome version " ESPHOME_VERSION
" compiled on %s", build_time_str);
142#ifdef ESPHOME_PROJECT_NAME
143 ESP_LOGI(TAG,
"Project " ESPHOME_PROJECT_NAME
" version " ESPHOME_PROJECT_VERSION);
146 esp_chip_info_t chip_info;
147 esp_chip_info(&chip_info);
148 ESP_LOGI(TAG,
"ESP32 Chip: %s rev%d.%d, %d core(s)", ESPHOME_VARIANT, chip_info.revision / 100,
149 chip_info.revision % 100, chip_info.cores);
150#if defined(USE_ESP32_VARIANT_ESP32) && (!defined(USE_ESP32_MIN_CHIP_REVISION_SET) || !defined(USE_ESP32_SRAM1_AS_IRAM))
151 static const char *
const ESP32_ADVANCED_PATH =
"under esp32 > framework > advanced";
153#if defined(USE_ESP32_VARIANT_ESP32) && !defined(USE_ESP32_MIN_CHIP_REVISION_SET)
156 if (chip_info.revision >= 300) {
158 ESP_LOGW(TAG,
"Chip rev >= 3.0 detected. Set minimum_chip_revision: \"%d.%d\" %s to save ~10KB IRAM",
159 chip_info.revision / 100, chip_info.revision % 100, ESP32_ADVANCED_PATH);
161 ESP_LOGW(TAG,
"Chip rev >= 3.0 detected. Set minimum_chip_revision: \"%d.%d\" %s to reduce binary size",
162 chip_info.revision / 100, chip_info.revision % 100, ESP32_ADVANCED_PATH);
177 esp_bootloader_desc_t boot_desc;
178 if (esp_ota_get_bootloader_description(
nullptr, &boot_desc) != ESP_OK) {
179#ifdef USE_ESP32_VARIANT_ESP32
180 ESP_LOGW(TAG,
"Bootloader too old for OTA rollback and SRAM1 as IRAM (+40KB). "
181 "Flash via USB once to update the bootloader");
183 ESP_LOGW(TAG,
"Bootloader too old for OTA rollback. Flash via USB once to update the bootloader");
186#if defined(USE_ESP32_VARIANT_ESP32) && !defined(USE_ESP32_SRAM1_AS_IRAM)
188 ESP_LOGW(TAG,
"Bootloader supports SRAM1 as IRAM (+40KB). Set sram1_as_iram: true %s", ESP32_ADVANCED_PATH);
204 if (now - last_feed > 3) {
215 ESP_LOGI(TAG,
"Forcing a reboot");
222 ESP_LOGI(TAG,
"Rebooting safely");
256 for (
size_t i = 0; i < num_components; ++i) {
257 pending_components[i] = this->
components_[num_components - 1 - i];
261 size_t pending_count = num_components;
300 while (pending_count > 0 && (now - start_time) < timeout_ms) {
305 size_t still_pending = 0;
306 for (
size_t i = 0; i < pending_count; ++i) {
307 if (!pending_components[i]->teardown()) {
309 if (still_pending != i) {
310 pending_components[still_pending] = pending_components[i];
316 pending_count = still_pending;
319 if (pending_count > 0) {
327 if (pending_count > 0) {
330 for (
size_t i = 0; i < pending_count; ++i) {
331 ESP_LOGW(TAG,
"%s did not complete teardown within %" PRIu32
" ms",
332 LOG_STR_ARG(pending_components[i]->get_component_log_str()), timeout_ms);
339 if (obj->has_overridden_loop() &&
354 this->looping_components_active_end_--;
355 if (i != this->looping_components_active_end_) {
414 bool has_pending =
false;
439 ESP_LOGVV(TAG,
"%s loop enabled from ISR", LOG_STR_ARG(
component->get_component_log_str()));
452#ifdef USE_LWIP_FAST_SELECT
477#elif defined(USE_HOST)
484 if (fd >= FD_SETSIZE) {
485 ESP_LOGE(TAG,
"fd %d exceeds FD_SETSIZE %d", fd, FD_SETSIZE);
504 for (
size_t i = 0; i < this->
socket_fds_.size(); i++) {
547 tv.tv_sec = delay_ms / 1000;
548 tv.tv_usec = (delay_ms - tv.tv_sec * 1000) * 1000;
551 int ret = ::select(this->
max_fd_ + 1, &this->
read_fds_,
nullptr,
nullptr, &tv);
556 if (ret >= 0) [[likely]] {
558 if (delay_ms == 0) [[unlikely]] {
564 const int err = errno;
569 ESP_LOGW(TAG,
"select() failed with errno %d", err);
579#ifndef __GXX_ABI_VERSION
580#error "Application placement new requires Itanium C++ ABI (GCC/Clang)"
582static_assert(std::is_default_constructible<Application>::value,
"Application must be default-constructible");
587#define ESPHOME_STRINGIFY_IMPL_(x) #x
588#define ESPHOME_STRINGIFY_(x) ESPHOME_STRINGIFY_IMPL_(x)
591 ESPHOME_STRINGIFY_(__USER_LABEL_PREFIX__)
"_ZN7esphome3AppE");
592#undef ESPHOME_STRINGIFY_
593#undef ESPHOME_STRINGIFY_IMPL_
603 ESP_LOGW(TAG,
"Wake socket create failed: %d", errno);
610 addr.
sin_addr.s_addr = htonl(INADDR_LOOPBACK);
614 ESP_LOGW(TAG,
"Wake socket bind failed: %d", errno);
625 ESP_LOGW(TAG,
"Wake socket address failed: %d", errno);
634 ESP_LOGW(TAG,
"Wake socket connect failed: %d", errno);
646 ESP_LOGW(TAG,
"Wake socket register failed");
656 ESPHOME_strncpy_P(buffer.data(), ESPHOME_BUILD_TIME_STR, buffer.size());
657 buffer[buffer.size() - 1] =
'\0';
661 ESPHOME_strncpy_P(buffer.data(), ESPHOME_COMMENT_STR, ESPHOME_COMMENT_SIZE);
662 buffer[ESPHOME_COMMENT_SIZE - 1] =
'\0';
void enable_pending_loops_()
std::vector< struct lwip_sock * > monitored_sockets_
void setup()
Reserve space for components to avoid memory fragmentation.
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_
void setup_wake_loop_threadsafe_()
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 feed_wdt(uint32_t time=0)
void enable_component_loop_(Component *component)
uint32_t loop_component_start_time_
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)
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 schedule_dump_config()
void run_safe_shutdown_hooks()
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 run_powerdown_hooks()
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,...
Minimal static vector - saves memory by avoiding std::vector overhead.
const Component * component
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....
StatusLED * global_status_led
Providing packet encoding functions for exchanging data with a remote host.
constexpr uint8_t COMPONENT_HAS_LOOP
constexpr uint32_t fnv1a_hash_extend(uint32_t hash, const char *str)
Extend a FNV-1a hash with additional string data.
constexpr uint8_t COMPONENT_STATE_LOOP
constexpr uint8_t STATUS_LED_WARNING
constexpr uint8_t COMPONENT_STATE_MASK
void clear_setup_priority_overrides()
constexpr uint8_t COMPONENT_STATE_LOOP_DONE
void HOT delay(uint32_t ms)
uint32_t IRAM_ATTR HOT millis()