7#ifdef USE_RUNTIME_STATS
15#if defined(USE_ESP8266) && defined(USE_SOCKET_IMPL_LWIP_TCP)
19#ifdef USE_SOCKET_SELECT_SUPPORT
22#ifdef USE_SOCKET_IMPL_LWIP_SOCKETS
24#include <lwip/sockets.h>
25#elif defined(USE_SOCKET_IMPL_BSD_SOCKETS)
29#include <lwip/sockets.h>
32#include <sys/select.h>
39static const char *
const TAG =
"app";
46template<
typename Iterator,
float (Component::*GetPriority)() const>
47static void insertion_sort_by_priority(Iterator first, Iterator last) {
48 for (
auto it = first + 1; it != last; ++it) {
50 float key_priority = (key->*GetPriority)();
54 while (j >= first && ((*j)->*GetPriority)() < key_priority) {
63 if (comp ==
nullptr) {
64 ESP_LOGW(TAG,
"Tried to register null component!");
70 ESP_LOGW(TAG,
"Component %s already registered! (%p)", LOG_STR_ARG(c->get_component_log_str()), c);
74 this->components_.push_back(comp);
77 ESP_LOGI(TAG,
"Running through setup()");
78 ESP_LOGV(TAG,
"Sorting components by setup priority");
87 for (uint32_t i = 0; i < this->
components_.size(); i++) {
93 this->scheduler.process_to_add();
109 for (uint32_t j = 0; j <= i; j++) {
113 new_app_state |= this->
components_[j]->get_component_state();
124 ESP_LOGI(TAG,
"setup() finished successfully!");
129#if defined(USE_SOCKET_SELECT_SUPPORT) && defined(USE_WAKE_LOOP_THREADSAFE)
137 uint8_t new_app_state = 0;
140 uint32_t last_op_end_time =
millis();
156 last_op_end_time = guard.
finish();
166#ifdef USE_RUNTIME_STATS
175 auto elapsed = last_op_end_time - this->
last_loop_;
182 uint32_t next_schedule = this->scheduler.next_schedule_in(last_op_end_time).value_or(delay_time);
185 next_schedule = std::max(next_schedule, delay_time / 2);
186 delay_time = std::min(next_schedule, delay_time);
190 this->last_loop_ = last_op_end_time;
194 ESP_LOGI(TAG,
"ESPHome version " ESPHOME_VERSION
" compiled on %s", this->
compilation_time_);
195#ifdef ESPHOME_PROJECT_NAME
196 ESP_LOGI(TAG,
"Project " ESPHOME_PROJECT_NAME
" version " ESPHOME_PROJECT_VERSION);
206 static uint32_t last_feed = 0;
208 uint32_t now = time ? time :
millis();
210 if (now - last_feed > 3) {
221 ESP_LOGI(TAG,
"Forcing a reboot");
228 ESP_LOGI(TAG,
"Rebooting safely");
251 uint32_t start_time =
millis();
262 for (
size_t i = 0; i < num_components; ++i) {
263 pending_components[i] = this->
components_[num_components - 1 - i];
266 uint32_t now = start_time;
267 size_t pending_count = num_components;
306 while (pending_count > 0 && (now - start_time) < timeout_ms) {
311 size_t still_pending = 0;
312 for (
size_t i = 0; i < pending_count; ++i) {
313 if (!pending_components[i]->teardown()) {
315 if (still_pending != i) {
316 pending_components[still_pending] = pending_components[i];
322 pending_count = still_pending;
325 if (pending_count > 0) {
333 if (pending_count > 0) {
336 for (
size_t i = 0; i < pending_count; ++i) {
337 ESP_LOGW(TAG,
"%s did not complete teardown within %" PRIu32
" ms",
338 LOG_STR_ARG(pending_components[i]->get_component_log_str()), timeout_ms);
345 size_t total_looping = 0;
347 if (obj->has_overridden_loop()) {
369 if (obj->has_overridden_loop() &&
384 this->looping_components_active_end_--;
385 if (i != this->looping_components_active_end_) {
444 bool has_pending =
false;
469 ESP_LOGVV(TAG,
"%s loop enabled from ISR", LOG_STR_ARG(
component->get_component_log_str()));
470 component->component_state_ &= ~COMPONENT_STATE_MASK;
484#if defined(USE_SOCKET_SELECT_SUPPORT) && defined(USE_WAKE_LOOP_THREADSAFE)
490 this->scheduler.call(loop_start_time);
518#ifdef USE_SOCKET_SELECT_SUPPORT
530 if (fd >= FD_SETSIZE) {
531 ESP_LOGE(TAG,
"fd %d exceeds FD_SETSIZE %d", fd, FD_SETSIZE);
552 for (
size_t i = 0; i < this->
socket_fds_.size(); i++) {
578 if (fd < 0 || fd >= FD_SETSIZE)
588#ifdef USE_SOCKET_SELECT_SUPPORT
607 tv.tv_sec = delay_ms / 1000;
608 tv.tv_usec = (delay_ms - tv.tv_sec * 1000) * 1000;
611#if defined(USE_SOCKET_IMPL_LWIP_SOCKETS) || (defined(USE_ESP32) && defined(USE_SOCKET_IMPL_BSD_SOCKETS))
612 int ret = lwip_select(this->
max_fd_ + 1, &this->
read_fds_,
nullptr,
nullptr, &tv);
614 int ret = ::select(this->
max_fd_ + 1, &this->
read_fds_,
nullptr,
nullptr, &tv);
621 if (ret < 0 && errno != EINTR) {
623 ESP_LOGW(TAG,
"select() failed with errno %d", errno);
634#elif defined(USE_ESP8266) && defined(USE_SOCKET_IMPL_LWIP_TCP)
645#if defined(USE_SOCKET_SELECT_SUPPORT) && defined(USE_WAKE_LOOP_THREADSAFE)
650 ESP_LOGW(TAG,
"Wake socket create failed: %d", errno);
657 addr.
sin_addr.s_addr = lwip_htonl(INADDR_LOOPBACK);
661 ESP_LOGW(TAG,
"Wake socket bind failed: %d", errno);
672 ESP_LOGW(TAG,
"Wake socket address failed: %d", errno);
681 ESP_LOGW(TAG,
"Wake socket connect failed: %d", errno);
693 ESP_LOGW(TAG,
"Wake socket register failed");
704 const char dummy = 1;
void enable_pending_loops_()
void setup()
Set up all the registered components. Call this at the end of your setup() function.
void wake_loop_threadsafe()
Wake the main event loop from a FreeRTOS task Thread-safe, can be called from task context to immedia...
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 setup_wake_loop_threadsafe_()
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_
const char * compilation_time_
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
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 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
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