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");
123 ESP_LOGI(TAG,
"setup() finished successfully!");
125#ifdef USE_SETUP_PRIORITY_OVERRIDE
130#if defined(USE_ESP32) || defined(USE_LIBRETINY)
150void Application::process_dump_config_() {
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);
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";
166#if defined(USE_ESP32_VARIANT_ESP32) && !defined(USE_ESP32_MIN_CHIP_REVISION_SET)
169 if (chip_info.revision >= 300) {
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);
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);
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");
196 ESP_LOGW(TAG,
"Bootloader too old for OTA rollback. Flash via USB once to update the bootloader");
199#if defined(USE_ESP32_VARIANT_ESP32) && !defined(USE_ESP32_SRAM1_AS_IRAM)
201 ESP_LOGW(TAG,
"Bootloader supports SRAM1 as IRAM (+40KB). Set sram1_as_iram: true %s", ESP32_ADVANCED_PATH);
254 if ((
component->get_component_state() & flag) != 0)
261 ESP_LOGI(TAG,
"Forcing a reboot");
268 ESP_LOGI(TAG,
"Rebooting safely");
302 for (
size_t i = 0; i < num_components; ++i) {
303 pending_components[i] = this->
components_[num_components - 1 - i];
307 size_t pending_count = num_components;
346 while (pending_count > 0 && (now - start_time) < timeout_ms) {
351 size_t still_pending = 0;
352 for (
size_t i = 0; i < pending_count; ++i) {
353 if (!pending_components[i]->teardown()) {
355 if (still_pending != i) {
356 pending_components[still_pending] = pending_components[i];
362 pending_count = still_pending;
365 if (pending_count > 0) {
373 if (pending_count > 0) {
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);
385 if (obj->has_overridden_loop() &&
400 this->looping_components_active_end_--;
401 if (i != this->looping_components_active_end_) {
460 bool has_pending =
false;
485 ESP_LOGVV(TAG,
"%s loop enabled from ISR", LOG_STR_ARG(
component->get_component_log_str()));
498#ifdef USE_LWIP_FAST_SELECT
523#elif defined(USE_HOST)
530 if (fd >= FD_SETSIZE) {
531 ESP_LOGE(TAG,
"fd %d exceeds FD_SETSIZE %d", fd, FD_SETSIZE);
550 for (
size_t i = 0; i < this->
socket_fds_.size(); i++) {
593 tv.tv_sec = delay_ms / 1000;
594 tv.tv_usec = (delay_ms - tv.tv_sec * 1000) * 1000;
597 int ret = ::select(this->
max_fd_ + 1, &this->
read_fds_,
nullptr,
nullptr, &tv);
602 if (ret >= 0) [[likely]] {
604 if (delay_ms == 0) [[unlikely]] {
610 const int err = errno;
615 ESP_LOGW(TAG,
"select() failed with errno %d", err);
625#ifndef __GXX_ABI_VERSION
626#error "Application placement new requires Itanium C++ ABI (GCC/Clang)"
628static_assert(std::is_default_constructible<Application>::value,
"Application must be default-constructible");
633#define ESPHOME_STRINGIFY_IMPL_(x) #x
634#define ESPHOME_STRINGIFY_(x) ESPHOME_STRINGIFY_IMPL_(x)
637 ESPHOME_STRINGIFY_(__USER_LABEL_PREFIX__)
"_ZN7esphome3AppE");
638#undef ESPHOME_STRINGIFY_
639#undef ESPHOME_STRINGIFY_IMPL_
649 ESP_LOGW(TAG,
"Wake socket create failed: %d", errno);
656 addr.
sin_addr.s_addr = htonl(INADDR_LOOPBACK);
660 ESP_LOGW(TAG,
"Wake socket bind failed: %d", errno);
671 ESP_LOGW(TAG,
"Wake socket address failed: %d", errno);
680 ESP_LOGW(TAG,
"Wake socket connect failed: %d", errno);
692 ESP_LOGW(TAG,
"Wake socket register failed");
702 ESPHOME_strncpy_P(buffer.data(), ESPHOME_BUILD_TIME_STR, buffer.size());
703 buffer[buffer.size() - 1] =
'\0';
707 ESPHOME_strncpy_P(buffer.data(), ESPHOME_COMMENT_STR, ESPHOME_COMMENT_SIZE);
708 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.
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_
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 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...
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
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
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 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 HOT delay(uint32_t ms)
uint32_t IRAM_ATTR HOT millis()