14#ifdef USE_RUNTIME_STATS
22#if defined(USE_ESP8266) && defined(USE_SOCKET_IMPL_LWIP_TCP)
26#ifdef USE_SOCKET_SELECT_SUPPORT
29#ifdef USE_SOCKET_IMPL_LWIP_SOCKETS
31#include <lwip/sockets.h>
32#elif defined(USE_SOCKET_IMPL_BSD_SOCKETS)
36#include <lwip/sockets.h>
39#include <sys/select.h>
46static const char *
const TAG =
"app";
53template<
typename Iterator,
float (Component::*GetPriority)() const>
54static void insertion_sort_by_priority(Iterator first, Iterator last) {
55 for (
auto it = first + 1; it != last; ++it) {
57 float key_priority = (key->*GetPriority)();
61 while (j >= first && ((*j)->*GetPriority)() < key_priority) {
70 if (comp ==
nullptr) {
71 ESP_LOGW(TAG,
"Tried to register null component!");
77 ESP_LOGW(TAG,
"Component %s already registered! (%p)", LOG_STR_ARG(c->get_component_log_str()), c);
81 this->components_.push_back(comp);
84 ESP_LOGI(TAG,
"Running through setup()");
85 ESP_LOGV(TAG,
"Sorting components by setup priority");
94 for (uint32_t i = 0; i < this->
components_.size(); i++) {
100 this->scheduler.process_to_add();
116 for (uint32_t j = 0; j <= i; j++) {
120 new_app_state |= this->
components_[j]->get_component_state();
131 ESP_LOGI(TAG,
"setup() finished successfully!");
136#if defined(USE_SOCKET_SELECT_SUPPORT) && defined(USE_WAKE_LOOP_THREADSAFE)
144 uint8_t new_app_state = 0;
147 uint32_t last_op_end_time =
millis();
163 last_op_end_time = guard.
finish();
173#ifdef USE_RUNTIME_STATS
182 auto elapsed = last_op_end_time - this->
last_loop_;
189 uint32_t next_schedule = this->scheduler.next_schedule_in(last_op_end_time).value_or(delay_time);
192 next_schedule = std::max(next_schedule, delay_time / 2);
193 delay_time = std::min(next_schedule, delay_time);
197 this->last_loop_ = last_op_end_time;
203 ESP_LOGI(TAG,
"ESPHome version " ESPHOME_VERSION
" compiled on %s", build_time_str);
204#ifdef ESPHOME_PROJECT_NAME
205 ESP_LOGI(TAG,
"Project " ESPHOME_PROJECT_NAME
" version " ESPHOME_PROJECT_VERSION);
215 static uint32_t last_feed = 0;
217 uint32_t now = time ? time :
millis();
219 if (now - last_feed > 3) {
230 ESP_LOGI(TAG,
"Forcing a reboot");
237 ESP_LOGI(TAG,
"Rebooting safely");
260 uint32_t start_time =
millis();
271 for (
size_t i = 0; i < num_components; ++i) {
272 pending_components[i] = this->
components_[num_components - 1 - i];
275 uint32_t now = start_time;
276 size_t pending_count = num_components;
315 while (pending_count > 0 && (now - start_time) < timeout_ms) {
320 size_t still_pending = 0;
321 for (
size_t i = 0; i < pending_count; ++i) {
322 if (!pending_components[i]->teardown()) {
324 if (still_pending != i) {
325 pending_components[still_pending] = pending_components[i];
331 pending_count = still_pending;
334 if (pending_count > 0) {
342 if (pending_count > 0) {
345 for (
size_t i = 0; i < pending_count; ++i) {
346 ESP_LOGW(TAG,
"%s did not complete teardown within %" PRIu32
" ms",
347 LOG_STR_ARG(pending_components[i]->get_component_log_str()), timeout_ms);
354 size_t total_looping = 0;
356 if (obj->has_overridden_loop()) {
378 if (obj->has_overridden_loop() &&
393 this->looping_components_active_end_--;
394 if (i != this->looping_components_active_end_) {
453 bool has_pending =
false;
478 ESP_LOGVV(TAG,
"%s loop enabled from ISR", LOG_STR_ARG(
component->get_component_log_str()));
479 component->component_state_ &= ~COMPONENT_STATE_MASK;
493#if defined(USE_SOCKET_SELECT_SUPPORT) && defined(USE_WAKE_LOOP_THREADSAFE)
499 this->scheduler.call(loop_start_time);
527#ifdef USE_SOCKET_SELECT_SUPPORT
539 if (fd >= FD_SETSIZE) {
540 ESP_LOGE(TAG,
"fd %d exceeds FD_SETSIZE %d", fd, FD_SETSIZE);
561 for (
size_t i = 0; i < this->
socket_fds_.size(); i++) {
587 if (fd < 0 || fd >= FD_SETSIZE)
597#ifdef USE_SOCKET_SELECT_SUPPORT
616 tv.tv_sec = delay_ms / 1000;
617 tv.tv_usec = (delay_ms - tv.tv_sec * 1000) * 1000;
620#if defined(USE_SOCKET_IMPL_LWIP_SOCKETS) || (defined(USE_ESP32) && defined(USE_SOCKET_IMPL_BSD_SOCKETS))
621 int ret = lwip_select(this->
max_fd_ + 1, &this->
read_fds_,
nullptr,
nullptr, &tv);
623 int ret = ::select(this->
max_fd_ + 1, &this->
read_fds_,
nullptr,
nullptr, &tv);
630 if (ret < 0 && errno != EINTR) {
632 ESP_LOGW(TAG,
"select() failed with errno %d", errno);
643#elif defined(USE_ESP8266) && defined(USE_SOCKET_IMPL_LWIP_TCP)
654#if defined(USE_SOCKET_SELECT_SUPPORT) && defined(USE_WAKE_LOOP_THREADSAFE)
659 ESP_LOGW(TAG,
"Wake socket create failed: %d", errno);
666 addr.
sin_addr.s_addr = lwip_htonl(INADDR_LOOPBACK);
670 ESP_LOGW(TAG,
"Wake socket bind failed: %d", errno);
681 ESP_LOGW(TAG,
"Wake socket address failed: %d", errno);
690 ESP_LOGW(TAG,
"Wake socket connect failed: %d", errno);
702 ESP_LOGW(TAG,
"Wake socket register failed");
713 const char dummy = 1;
724 ESPHOME_strncpy_P(buffer.data(), ESPHOME_BUILD_TIME_STR, buffer.size());
725 buffer[buffer.size() - 1] =
'\0';
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...
static constexpr size_t BUILD_TIME_STR_SIZE
Size of buffer required for build time string (including null terminator)
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 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 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