7#ifdef USE_RUNTIME_STATS
15#ifdef USE_SOCKET_SELECT_SUPPORT
18#ifdef USE_SOCKET_IMPL_LWIP_SOCKETS
20#include <lwip/sockets.h>
21#elif defined(USE_SOCKET_IMPL_BSD_SOCKETS)
25#include <lwip/sockets.h>
28#include <sys/select.h>
35static const char *
const TAG =
"app";
42template<
typename Iterator,
float (Component::*GetPriority)() const>
43static void insertion_sort_by_priority(Iterator first, Iterator last) {
44 for (
auto it = first + 1; it != last; ++it) {
46 float key_priority = (key->*GetPriority)();
50 while (j >= first && ((*j)->*GetPriority)() < key_priority) {
59 if (comp ==
nullptr) {
60 ESP_LOGW(TAG,
"Tried to register null component!");
66 ESP_LOGW(TAG,
"Component %s already registered! (%p)", LOG_STR_ARG(c->get_component_log_str()), c);
70 this->components_.push_back(comp);
73 ESP_LOGI(TAG,
"Running through setup()");
74 ESP_LOGV(TAG,
"Sorting components by setup priority");
83 for (uint32_t i = 0; i < this->
components_.size(); i++) {
89 this->scheduler.process_to_add();
105 for (uint32_t j = 0; j <= i; j++) {
109 new_app_state |= this->
components_[j]->get_component_state();
120 ESP_LOGI(TAG,
"setup() finished successfully!");
128 uint8_t new_app_state = 0;
131 uint32_t last_op_end_time =
millis();
147 last_op_end_time = guard.finish();
157#ifdef USE_RUNTIME_STATS
166 auto elapsed = last_op_end_time - this->
last_loop_;
173 uint32_t next_schedule = this->scheduler.next_schedule_in(last_op_end_time).value_or(delay_time);
176 next_schedule = std::max(next_schedule, delay_time / 2);
177 delay_time = std::min(next_schedule, delay_time);
181 this->last_loop_ = last_op_end_time;
185 ESP_LOGI(TAG,
"ESPHome version " ESPHOME_VERSION
" compiled on %s", this->
compilation_time_);
186#ifdef ESPHOME_PROJECT_NAME
187 ESP_LOGI(TAG,
"Project " ESPHOME_PROJECT_NAME
" version " ESPHOME_PROJECT_VERSION);
197 static uint32_t last_feed = 0;
199 uint32_t now = time ? time :
millis();
201 if (now - last_feed > 3) {
212 ESP_LOGI(TAG,
"Forcing a reboot");
213 for (
auto &component : std::ranges::reverse_view(this->
components_)) {
214 component->on_shutdown();
219 ESP_LOGI(TAG,
"Rebooting safely");
227 for (
auto &component : std::ranges::reverse_view(this->
components_)) {
228 component->on_safe_shutdown();
230 for (
auto &component : std::ranges::reverse_view(this->
components_)) {
231 component->on_shutdown();
236 for (
auto &component : std::ranges::reverse_view(this->
components_)) {
237 component->on_powerdown();
242 uint32_t start_time =
millis();
253 for (
size_t i = 0; i < num_components; ++i) {
254 pending_components[i] = this->
components_[num_components - 1 - i];
257 uint32_t now = start_time;
258 size_t pending_count = num_components;
297 while (pending_count > 0 && (now - start_time) < timeout_ms) {
302 size_t still_pending = 0;
303 for (
size_t i = 0; i < pending_count; ++i) {
304 if (!pending_components[i]->teardown()) {
306 if (still_pending != i) {
307 pending_components[still_pending] = pending_components[i];
313 pending_count = still_pending;
316 if (pending_count > 0) {
324 if (pending_count > 0) {
327 for (
size_t i = 0; i < pending_count; ++i) {
328 ESP_LOGW(TAG,
"%s did not complete teardown within %" PRIu32
" ms",
329 LOG_STR_ARG(pending_components[i]->get_component_log_str()), timeout_ms);
336 size_t total_looping = 0;
338 if (obj->has_overridden_loop()) {
360 if (obj->has_overridden_loop() &&
375 this->looping_components_active_end_--;
376 if (i != this->looping_components_active_end_) {
435 bool has_pending =
false;
476 this->scheduler.call(loop_start_time);
504#ifdef USE_SOCKET_SELECT_SUPPORT
516 if (fd >= FD_SETSIZE) {
517 ESP_LOGE(TAG,
"fd %d exceeds FD_SETSIZE %d", fd, FD_SETSIZE);
538 for (
size_t i = 0; i < this->
socket_fds_.size(); i++) {
564 if (fd < 0 || fd >= FD_SETSIZE)
574#ifdef USE_SOCKET_SELECT_SUPPORT
580 if (fd >= 0 && fd < FD_SETSIZE) {
592 tv.tv_sec = delay_ms / 1000;
593 tv.tv_usec = (delay_ms - tv.tv_sec * 1000) * 1000;
596#if defined(USE_SOCKET_IMPL_LWIP_SOCKETS) || (defined(USE_ESP32) && defined(USE_SOCKET_IMPL_BSD_SOCKETS))
597 int ret = lwip_select(this->
max_fd_ + 1, &this->
read_fds_,
nullptr,
nullptr, &tv);
599 int ret = ::select(this->
max_fd_ + 1, &this->
read_fds_,
nullptr,
nullptr, &tv);
606 if (ret < 0 && errno != EINTR) {
608 ESP_LOGW(TAG,
"select() failed with errno %d", errno);
void enable_pending_loops_()
void setup()
Set up all the registered components. Call this at the end of your setup() function.
uint16_t looping_components_active_end_
void set_current_component(Component *component)
bool is_socket_ready(int fd) const
Check if there's data available on a socket without blocking This function is thread-safe for reading...
std::vector< int > socket_fds_
StaticVector< Component *, ESPHOME_COMPONENT_COUNT > components_
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.
void add_looping_components_by_state_(bool match_loop_done)
volatile bool has_pending_enable_loop_requests_
const char * compilation_time_
std::vector< Component * > looping_components_
void schedule_dump_config()
void run_safe_shutdown_hooks()
uint16_t current_loop_index_
void feed_wdt(uint32_t time=0)
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)
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 run_powerdown_hooks()
void register_component_(Component *comp)
float get_actual_setup_priority() const
const LogString * get_component_log_str() const
Get the integration where this component was declared as a LogString for logging.
uint8_t get_component_state() const
volatile bool pending_enable_loop_
ISR-safe flag for enable_loop_soon_any_context.
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)
StatusLED * global_status_led
Providing packet encoding functions for exchanging data with a remote host.
runtime_stats::RuntimeStatsCollector * global_runtime_stats
const uint8_t COMPONENT_STATE_MASK
const uint8_t COMPONENT_STATE_LOOP
void clear_setup_priority_overrides()
void IRAM_ATTR HOT yield()
void IRAM_ATTR HOT arch_feed_wdt()
const uint8_t STATUS_LED_WARNING
void IRAM_ATTR HOT delay(uint32_t ms)
uint32_t IRAM_ATTR HOT millis()
Application App
Global storage of Application pointer - only one Application can exist.
const uint8_t COMPONENT_STATE_LOOP_DONE