11#include <esp_chip_info.h>
13#ifdef USE_LWIP_FAST_SELECT
16#include <freertos/FreeRTOS.h>
17#include <freertos/task.h>
27#ifdef USE_RUNTIME_STATS
35#if defined(USE_ESP8266) && defined(USE_SOCKET_IMPL_LWIP_TCP)
39#ifdef USE_SOCKET_SELECT_SUPPORT
42#ifdef USE_SOCKET_IMPL_LWIP_SOCKETS
44#include <lwip/sockets.h>
45#elif defined(USE_SOCKET_IMPL_BSD_SOCKETS)
49#include <lwip/sockets.h>
52#include <sys/select.h>
59static const char *
const TAG =
"app";
66template<
typename Iterator,
float (Component::*GetPriority)() const>
67static void insertion_sort_by_priority(Iterator first, Iterator last) {
68 for (
auto it = first + 1; it != last; ++it) {
70 float key_priority = (key->*GetPriority)();
74 while (j >= first && ((*j)->*GetPriority)() < key_priority) {
89 ESP_LOGI(TAG,
"Running through setup()");
90 ESP_LOGV(TAG,
"Sorting components by setup priority");
99 for (uint32_t i = 0; i < this->
components_.size(); i++) {
105 this->scheduler.process_to_add();
110#ifdef USE_LOOP_PRIORITY
123 for (uint32_t j = 0; j <= i; j++) {
127 new_app_state |= this->
components_[j]->get_component_state();
138 ESP_LOGI(TAG,
"setup() finished successfully!");
140#ifdef USE_SETUP_PRIORITY_OVERRIDE
145#if defined(USE_SOCKET_SELECT_SUPPORT) && defined(USE_LWIP_FAST_SELECT)
151#if defined(USE_SOCKET_SELECT_SUPPORT) && defined(USE_WAKE_LOOP_THREADSAFE) && !defined(USE_LWIP_FAST_SELECT)
159 uint8_t new_app_state = 0;
162 uint32_t last_op_end_time =
millis();
178 last_op_end_time = guard.
finish();
188#ifdef USE_RUNTIME_STATS
197 auto elapsed = last_op_end_time - this->
last_loop_;
204 uint32_t next_schedule = this->scheduler.next_schedule_in(last_op_end_time).value_or(delay_time);
207 next_schedule = std::max(next_schedule, delay_time / 2);
208 delay_time = std::min(next_schedule, delay_time);
212 this->last_loop_ = last_op_end_time;
215 this->process_dump_config_();
219void Application::process_dump_config_() {
223 ESP_LOGI(TAG,
"ESPHome version " ESPHOME_VERSION
" compiled on %s", build_time_str);
224#ifdef ESPHOME_PROJECT_NAME
225 ESP_LOGI(TAG,
"Project " ESPHOME_PROJECT_NAME
" version " ESPHOME_PROJECT_VERSION);
228 esp_chip_info_t chip_info;
229 esp_chip_info(&chip_info);
230 ESP_LOGI(TAG,
"ESP32 Chip: %s rev%d.%d, %d core(s)", ESPHOME_VARIANT, chip_info.revision / 100,
231 chip_info.revision % 100, chip_info.cores);
232#if defined(USE_ESP32_VARIANT_ESP32) && !defined(USE_ESP32_MIN_CHIP_REVISION_SET)
234 if (chip_info.revision >= 300) {
236 ESP_LOGW(TAG,
"Set minimum_chip_revision: \"%d.%d\" to save ~10KB IRAM", chip_info.revision / 100,
237 chip_info.revision % 100);
239 ESP_LOGW(TAG,
"Set minimum_chip_revision: \"%d.%d\" to reduce binary size", chip_info.revision / 100,
240 chip_info.revision % 100);
252 static uint32_t last_feed = 0;
254 uint32_t now = time ? time :
millis();
256 if (now - last_feed > 3) {
267 ESP_LOGI(TAG,
"Forcing a reboot");
274 ESP_LOGI(TAG,
"Rebooting safely");
297 uint32_t start_time =
millis();
308 for (
size_t i = 0; i < num_components; ++i) {
309 pending_components[i] = this->
components_[num_components - 1 - i];
312 uint32_t now = start_time;
313 size_t pending_count = num_components;
352 while (pending_count > 0 && (now - start_time) < timeout_ms) {
357 size_t still_pending = 0;
358 for (
size_t i = 0; i < pending_count; ++i) {
359 if (!pending_components[i]->teardown()) {
361 if (still_pending != i) {
362 pending_components[still_pending] = pending_components[i];
368 pending_count = still_pending;
371 if (pending_count > 0) {
379 if (pending_count > 0) {
382 for (
size_t i = 0; i < pending_count; ++i) {
383 ESP_LOGW(TAG,
"%s did not complete teardown within %" PRIu32
" ms",
384 LOG_STR_ARG(pending_components[i]->get_component_log_str()), timeout_ms);
407 if (obj->has_overridden_loop() &&
422 this->looping_components_active_end_--;
423 if (i != this->looping_components_active_end_) {
482 bool has_pending =
false;
507 ESP_LOGVV(TAG,
"%s loop enabled from ISR", LOG_STR_ARG(
component->get_component_log_str()));
521#if defined(USE_SOCKET_SELECT_SUPPORT) && defined(USE_WAKE_LOOP_THREADSAFE) && !defined(USE_LWIP_FAST_SELECT)
527 this->scheduler.call(loop_start_time);
555#ifdef USE_SOCKET_SELECT_SUPPORT
567 if (fd >= FD_SETSIZE) {
568 ESP_LOGE(TAG,
"fd %d exceeds FD_SETSIZE %d", fd, FD_SETSIZE);
574#ifdef USE_LWIP_FAST_SELECT
593 for (
size_t i = 0; i < this->
socket_fds_.size(); i++) {
604#ifndef USE_LWIP_FAST_SELECT
623#if defined(USE_SOCKET_SELECT_SUPPORT) && defined(USE_LWIP_FAST_SELECT)
626 if (delay_ms == 0) [[unlikely]] {
646 ulTaskNotifyTake(pdTRUE, pdMS_TO_TICKS(delay_ms));
648#elif defined(USE_SOCKET_SELECT_SUPPORT)
651 if (!this->socket_fds_.empty()) [[likely]] {
656 for (
int fd : this->socket_fds_) {
667 tv.tv_sec = delay_ms / 1000;
668 tv.tv_usec = (delay_ms - tv.tv_sec * 1000) * 1000;
671#ifdef USE_SOCKET_IMPL_LWIP_SOCKETS
672 int ret = lwip_select(this->
max_fd_ + 1, &this->
read_fds_,
nullptr,
nullptr, &tv);
674 int ret = ::select(this->
max_fd_ + 1, &this->
read_fds_,
nullptr,
nullptr, &tv);
681 if (ret >= 0 || errno == EINTR) [[likely]] {
683 if (delay_ms == 0) [[unlikely]] {
689 ESP_LOGW(TAG,
"select() failed with errno %d", errno);
693#elif defined(USE_ESP8266) && defined(USE_SOCKET_IMPL_LWIP_TCP)
705#ifndef __GXX_ABI_VERSION
706#error "Application placement new requires Itanium C++ ABI (GCC/Clang)"
708static_assert(std::is_default_constructible<Application>::value,
"Application must be default-constructible");
713#define ESPHOME_STRINGIFY_IMPL_(x) #x
714#define ESPHOME_STRINGIFY_(x) ESPHOME_STRINGIFY_IMPL_(x)
717 ESPHOME_STRINGIFY_(__USER_LABEL_PREFIX__)
"_ZN7esphome3AppE");
718#undef ESPHOME_STRINGIFY_
719#undef ESPHOME_STRINGIFY_IMPL_
721#if defined(USE_SOCKET_SELECT_SUPPORT) && defined(USE_WAKE_LOOP_THREADSAFE)
723#ifdef USE_LWIP_FAST_SELECT
734 ESP_LOGW(TAG,
"Wake socket create failed: %d", errno);
741 addr.
sin_addr.s_addr = lwip_htonl(INADDR_LOOPBACK);
745 ESP_LOGW(TAG,
"Wake socket bind failed: %d", errno);
756 ESP_LOGW(TAG,
"Wake socket address failed: %d", errno);
765 ESP_LOGW(TAG,
"Wake socket connect failed: %d", errno);
777 ESP_LOGW(TAG,
"Wake socket register failed");
788 const char dummy = 1;
801 ESPHOME_strncpy_P(buffer.data(), ESPHOME_BUILD_TIME_STR, buffer.size());
802 buffer[buffer.size() - 1] =
'\0';
806 ESPHOME_strncpy_P(buffer.data(), ESPHOME_COMMENT_STR, ESPHOME_COMMENT_SIZE);
807 buffer[ESPHOME_COMMENT_SIZE - 1] =
'\0';
void enable_pending_loops_()
void setup()
Reserve space for components to avoid memory fragmentation.
void wake_loop_threadsafe()
Wake the main event loop from another FreeRTOS task.
uint16_t looping_components_active_end_
void set_current_component(Component *component)
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 drain_wake_notifications_()
void enable_component_loop_(Component *component)
uint32_t loop_component_start_time_
void disable_component_loop_(Component *component)
void activate_looping_component_(uint16_t index)
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 before_loop_tasks_(uint32_t loop_start_time)
void loop()
Make a loop iteration. Call this in your loop() function.
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)
Register/unregister a socket file descriptor to be monitored for read events.
void calculate_looping_components_()
void yield_with_select_(uint32_t delay_ms)
Perform a delay while also monitoring socket file descriptors for readiness.
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()
virtual float get_loop_priority() const
priority of loop().
uint8_t component_state_
State of this component - each bit has a purpose: Bits 0-2: Component state (0x00=CONSTRUCTION,...
static bool is_high_frequency()
Check whether the loop is running continuously.
Minimal static vector - saves memory by avoiding std::vector overhead.
void process_pending_stats(uint32_t current_time)
const Component * component
void esphome_lwip_fast_select_init(void)
Initialize fast select — must be called from the main loop task during setup().
bool esphome_lwip_socket_has_data(int fd)
Check if a LwIP socket has data ready via direct rcvevent read (~215 ns per socket).
void esphome_lwip_hook_socket(int fd)
Hook a socket's netconn callback to notify the main loop task on receive events.
void esphome_lwip_wake_main_loop(void)
Wake the main loop task from another FreeRTOS task — costs <1 us.
void socket_delay(uint32_t ms)
Delay that can be woken early by socket activity.
StatusLED * global_status_led
Providing packet encoding functions for exchanging data with a remote host.
runtime_stats::RuntimeStatsCollector * global_runtime_stats
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()