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>
static void insertion_sort_by_setup_priority(Iterator first, Iterator last) {
43 for (
auto it = first + 1; it != last; ++it) {
45 float key_priority = key->get_actual_setup_priority();
49 while (j >= first && (*j)->get_actual_setup_priority() < key_priority) {
60template<
typename Iterator>
static void insertion_sort_by_loop_priority(Iterator first, Iterator last) {
61 for (
auto it = first + 1; it != last; ++it) {
63 float key_priority = key->get_loop_priority();
67 while (j >= first && (*j)->get_loop_priority() < key_priority) {
76 if (comp ==
nullptr) {
77 ESP_LOGW(TAG,
"Tried to register null component!");
83 ESP_LOGW(TAG,
"Component %s already registered! (%p)", c->get_component_source(), c);
87 this->components_.push_back(comp);
90 ESP_LOGI(TAG,
"Running through setup()");
91 ESP_LOGV(TAG,
"Sorting components by setup priority");
94 insertion_sort_by_setup_priority(this->
components_.begin(), this->components_.end());
99 for (uint32_t i = 0; i < this->
components_.size(); i++) {
105 this->scheduler.process_to_add();
111 insertion_sort_by_loop_priority(this->
components_.begin(), this->components_.begin() + i + 1);
120 for (uint32_t j = 0; j <= i; j++) {
124 new_app_state |= this->
components_[j]->get_component_state();
135 ESP_LOGI(TAG,
"setup() finished successfully!");
143 uint8_t new_app_state = 0;
146 uint32_t last_op_end_time =
millis();
162 last_op_end_time = guard.finish();
172#ifdef USE_RUNTIME_STATS
181 auto elapsed = last_op_end_time - this->
last_loop_;
188 uint32_t next_schedule = this->scheduler.next_schedule_in(last_op_end_time).value_or(delay_time);
191 next_schedule = std::max(next_schedule, delay_time / 2);
192 delay_time = std::min(next_schedule, delay_time);
196 this->last_loop_ = last_op_end_time;
200 ESP_LOGI(TAG,
"ESPHome version " ESPHOME_VERSION
" compiled on %s", this->
compilation_time_);
201#ifdef ESPHOME_PROJECT_NAME
202 ESP_LOGI(TAG,
"Project " ESPHOME_PROJECT_NAME
" version " ESPHOME_PROJECT_VERSION);
212 static uint32_t last_feed = 0;
214 uint32_t now = time ? time :
millis();
216 if (now - last_feed > 3) {
227 ESP_LOGI(TAG,
"Forcing a reboot");
228 for (
auto &component : std::ranges::reverse_view(this->
components_)) {
229 component->on_shutdown();
234 ESP_LOGI(TAG,
"Rebooting safely");
242 for (
auto &component : std::ranges::reverse_view(this->
components_)) {
243 component->on_safe_shutdown();
245 for (
auto &component : std::ranges::reverse_view(this->
components_)) {
246 component->on_shutdown();
251 for (
auto &component : std::ranges::reverse_view(this->
components_)) {
252 component->on_powerdown();
257 uint32_t start_time =
millis();
263 std::vector<Component *> pending_components(this->
components_.rbegin(), this->components_.rend());
265 uint32_t now = start_time;
266 while (!pending_components.empty() && (now - start_time) < timeout_ms) {
271 for (
auto it = pending_components.begin(); it != pending_components.end();) {
272 if ((*it)->teardown()) {
274 it = pending_components.erase(it);
282 if (!pending_components.empty()) {
290 if (!pending_components.empty()) {
293 for (
auto *component : pending_components) {
294 ESP_LOGW(TAG,
"%s did not complete teardown within %" PRIu32
" ms", component->get_component_source(),
302 size_t total_looping = 0;
304 if (obj->has_overridden_loop()) {
315 for (
auto *obj : this->components_) {
316 if (obj->has_overridden_loop() &&
326 for (
auto *obj : this->components_) {
327 if (obj->has_overridden_loop() &&
342 this->looping_components_active_end_--;
343 if (i != this->looping_components_active_end_) {
402 bool has_pending =
false;
443 this->scheduler.call(loop_start_time);
471#ifdef USE_SOCKET_SELECT_SUPPORT
478 if (fd >= FD_SETSIZE) {
479 ESP_LOGE(TAG,
"Cannot monitor socket fd %d: exceeds FD_SETSIZE (%d)", fd, FD_SETSIZE);
480 ESP_LOGE(TAG,
"Socket will not be monitored for data - may cause performance issues!");
500 for (
size_t i = 0; i < this->
socket_fds_.size(); i++) {
526 if (fd < 0 || fd >= FD_SETSIZE)
536#ifdef USE_SOCKET_SELECT_SUPPORT
542 if (fd >= 0 && fd < FD_SETSIZE) {
554 tv.tv_sec = delay_ms / 1000;
555 tv.tv_usec = (delay_ms - tv.tv_sec * 1000) * 1000;
558#if defined(USE_SOCKET_IMPL_LWIP_SOCKETS) || (defined(USE_ESP32) && defined(USE_SOCKET_IMPL_BSD_SOCKETS))
559 int ret = lwip_select(this->
max_fd_ + 1, &this->
read_fds_,
nullptr,
nullptr, &tv);
561 int ret = ::select(this->
max_fd_ + 1, &this->
read_fds_,
nullptr,
nullptr, &tv);
568 if (ret < 0 && errno != EINTR) {
570 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.
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)
uint8_t get_component_state() const
volatile bool pending_enable_loop_
ISR-safe flag for enable_loop_soon_any_context.
virtual bool can_proceed()
const char * get_component_source() const
Get the integration where this component was declared as a string.
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.
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