11#include <esp_chip_info.h>
12#include <esp_ota_ops.h>
13#include <esp_bootloader_desc.h>
26static const char *
const TAG =
"app";
30static constexpr uint32_t SCHEDULER_FREELIST_TRIM_DELAY_MS = 10000;
37template<
typename Iterator,
float (Component::*GetPriority)() const>
38static void insertion_sort_by_priority(Iterator first, Iterator last) {
39 for (
auto it = first + 1; it != last; ++it) {
41 float key_priority = (key->*GetPriority)();
45 while (j >= first && ((*j)->*GetPriority)() < key_priority) {
60 ESP_LOGI(TAG,
"Running through setup()");
61 ESP_LOGV(TAG,
"Sorting components by setup priority");
117 ESP_LOGI(TAG,
"setup() finished successfully!");
120 this->
scheduler.set_timeout(
this, SCHEDULER_FREELIST_TRIM_DELAY_MS, [
this]() { this->
scheduler.trim_freelist(); });
122#ifdef USE_SETUP_PRIORITY_OVERRIDE
127#if defined(USE_ESP32) || defined(USE_LIBRETINY)
147void Application::process_dump_config_() {
151 ESP_LOGI(TAG,
"ESPHome version " ESPHOME_VERSION
" compiled on %s", build_time_str);
152#ifdef ESPHOME_PROJECT_NAME
153 ESP_LOGI(TAG,
"Project " ESPHOME_PROJECT_NAME
" version " ESPHOME_PROJECT_VERSION);
156 esp_chip_info_t chip_info;
157 esp_chip_info(&chip_info);
158 ESP_LOGI(TAG,
"ESP32 Chip: %s rev%d.%d, %d core(s)", ESPHOME_VARIANT, chip_info.revision / 100,
159 chip_info.revision % 100, chip_info.cores);
160#if defined(USE_ESP32_VARIANT_ESP32) && (!defined(USE_ESP32_MIN_CHIP_REVISION_SET) || !defined(USE_ESP32_SRAM1_AS_IRAM))
161 static const char *
const ESP32_ADVANCED_PATH =
"under esp32 > framework > advanced";
163#if defined(USE_ESP32_VARIANT_ESP32) && !defined(USE_ESP32_MIN_CHIP_REVISION_SET)
166 if (chip_info.revision >= 300) {
168 ESP_LOGW(TAG,
"Chip rev >= 3.0 detected. Set minimum_chip_revision: \"%d.%d\" %s to save ~10KB IRAM",
169 chip_info.revision / 100, chip_info.revision % 100, ESP32_ADVANCED_PATH);
171 ESP_LOGW(TAG,
"Chip rev >= 3.0 detected. Set minimum_chip_revision: \"%d.%d\" %s to reduce binary size",
172 chip_info.revision / 100, chip_info.revision % 100, ESP32_ADVANCED_PATH);
187 esp_bootloader_desc_t boot_desc;
188 if (esp_ota_get_bootloader_description(
nullptr, &boot_desc) != ESP_OK) {
189#ifdef USE_ESP32_VARIANT_ESP32
190 ESP_LOGW(TAG,
"Bootloader too old for OTA rollback and SRAM1 as IRAM (+40KB). "
191 "Flash via USB once to update the bootloader");
193 ESP_LOGW(TAG,
"Bootloader too old for OTA rollback. Flash via USB once to update the bootloader");
196#if defined(USE_ESP32_VARIANT_ESP32) && !defined(USE_ESP32_SRAM1_AS_IRAM)
198 ESP_LOGW(TAG,
"Bootloader supports SRAM1 as IRAM (+40KB). Set sram1_as_iram: true %s", ESP32_ADVANCED_PATH);
257 if ((
component->get_component_state() & flag) != 0)
264 ESP_LOGI(TAG,
"Forcing a reboot");
271 ESP_LOGI(TAG,
"Rebooting safely");
294 uint32_t start_time = MillisInternal::get();
305 for (
size_t i = 0; i < num_components; ++i) {
306 pending_components[i] = this->
components_[num_components - 1 - i];
310 size_t pending_count = num_components;
349 while (pending_count > 0 && (now - start_time) < timeout_ms) {
354 size_t still_pending = 0;
355 for (
size_t i = 0; i < pending_count; ++i) {
356 if (!pending_components[i]->teardown()) {
358 if (still_pending != i) {
359 pending_components[still_pending] = pending_components[i];
365 pending_count = still_pending;
368 if (pending_count > 0) {
373 now = MillisInternal::get();
376 if (pending_count > 0) {
379 for (
size_t i = 0; i < pending_count; ++i) {
380 ESP_LOGW(TAG,
"%s did not complete teardown within %" PRIu32
" ms",
381 LOG_STR_ARG(pending_components[i]->get_component_log_str()), timeout_ms);
388 if (obj->has_overridden_loop() &&
403 this->looping_components_active_end_--;
404 if (i != this->looping_components_active_end_) {
463 bool has_pending =
false;
488 ESP_LOGVV(TAG,
"%s loop enabled from ISR", LOG_STR_ARG(
component->get_component_log_str()));
504#ifndef __GXX_ABI_VERSION
505#error "Application placement new requires Itanium C++ ABI (GCC/Clang)"
507static_assert(std::is_default_constructible<Application>::value,
"Application must be default-constructible");
512#define ESPHOME_STRINGIFY_IMPL_(x) #x
513#define ESPHOME_STRINGIFY_(x) ESPHOME_STRINGIFY_IMPL_(x)
516 ESPHOME_STRINGIFY_(__USER_LABEL_PREFIX__)
"_ZN7esphome3AppE");
517#undef ESPHOME_STRINGIFY_
518#undef ESPHOME_STRINGIFY_IMPL_
521 ESPHOME_strncpy_P(buffer.data(), ESPHOME_BUILD_TIME_STR, buffer.size());
522 buffer[buffer.size() - 1] =
'\0';
526 ESPHOME_strncpy_P(buffer.data(), ESPHOME_COMMENT_STR, ESPHOME_COMMENT_SIZE);
527 buffer[ESPHOME_COMMENT_SIZE - 1] =
'\0';
void enable_pending_loops_()
uint32_t ESPHOME_ALWAYS_INLINE scheduler_tick_(uint32_t now)
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 service_status_led_slow_(uint32_t time)
Slow path for the status_led dispatch rate limit.
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.
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)
uint32_t last_status_led_service_
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() and updates last_wdt_feed_.
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.
uint32_t get_config_version_hash()
Get the config hash extended with ESPHome version.
void calculate_looping_components_()
void register_component_impl_(Component *comp, bool has_loop)
void run_powerdown_hooks()
float get_actual_setup_priority() const
uint8_t get_component_state() 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
TaskHandle_t esphome_main_task_handle
Main loop task handle and wake helpers — shared between wake.h (C++) and lwip_fast_select....
void ESPHOME_ALWAYS_INLINE wakeable_delay(uint32_t ms)
Host wakeable_delay uses select() over the registered fds — defined in wake_host.cpp.
StatusLED * global_status_led
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 STATUS_LED_MASK
constexpr uint8_t COMPONENT_STATE_LOOP
constexpr uint8_t APP_STATE_SETUP_COMPLETE
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 wake_setup()
One-time setup of the loopback wake socket. Called from Application::setup().