From 33ccbeeef11553e454b506185dfcecee7d5dc761 Mon Sep 17 00:00:00 2001 From: "J. Nick Koston" Date: Tue, 27 May 2025 15:04:21 -0500 Subject: [PATCH] fixes --- esphome/components/socket/socket.cpp | 2 +- esphome/core/application.cpp | 6 +++--- esphome/core/application.h | 6 +++--- esphome/core/defines.h | 5 +++++ 4 files changed, 12 insertions(+), 7 deletions(-) diff --git a/esphome/components/socket/socket.cpp b/esphome/components/socket/socket.cpp index fe65f35167..704f326080 100644 --- a/esphome/components/socket/socket.cpp +++ b/esphome/components/socket/socket.cpp @@ -17,7 +17,7 @@ bool Socket::ready() const { return true; } -#if (defined(USE_SOCKET_IMPL_LWIP_SOCKETS) || defined(USE_SOCKET_IMPL_BSD_SOCKETS)) && defined(FD_SETSIZE) +#ifdef USE_SOCKET_SELECT_SUPPORT // For loop-monitored sockets, check with the Application's select() results int fd = this->get_fd(); if (fd < 0) { diff --git a/esphome/core/application.cpp b/esphome/core/application.cpp index b9ab02a0e1..818cb25f8c 100644 --- a/esphome/core/application.cpp +++ b/esphome/core/application.cpp @@ -7,7 +7,7 @@ #include "esphome/components/status_led/status_led.h" #endif -#if (defined(USE_SOCKET_IMPL_LWIP_SOCKETS) || defined(USE_SOCKET_IMPL_BSD_SOCKETS)) && defined(FD_SETSIZE) +#ifdef USE_SOCKET_SELECT_SUPPORT #include #ifdef USE_ESP32 // ESP32 with BSD sockets actually uses lwIP underneath @@ -129,7 +129,7 @@ void Application::loop() { next_schedule = std::max(next_schedule, delay_time / 2); delay_time = std::min(next_schedule, delay_time); -#if (defined(USE_SOCKET_IMPL_LWIP_SOCKETS) || defined(USE_SOCKET_IMPL_BSD_SOCKETS)) && defined(FD_SETSIZE) +#ifdef USE_SOCKET_SELECT_SUPPORT if (!this->socket_fds_.empty()) { // Use select() with timeout when we have sockets to monitor @@ -247,7 +247,7 @@ void Application::calculate_looping_components_() { } } -#if (defined(USE_SOCKET_IMPL_LWIP_SOCKETS) || defined(USE_SOCKET_IMPL_BSD_SOCKETS)) && defined(FD_SETSIZE) +#ifdef USE_SOCKET_SELECT_SUPPORT bool Application::register_socket_fd(int fd) { // WARNING: This function is NOT thread-safe and must only be called from the main loop // It modifies socket_fds_ and related variables without locking diff --git a/esphome/core/application.h b/esphome/core/application.h index 4ae58d04f0..2db5b8d288 100644 --- a/esphome/core/application.h +++ b/esphome/core/application.h @@ -469,7 +469,7 @@ class Application { Scheduler scheduler; /// Register/unregister a socket file descriptor to be monitored for read events. -#if (defined(USE_SOCKET_IMPL_LWIP_SOCKETS) || defined(USE_SOCKET_IMPL_BSD_SOCKETS)) && defined(FD_SETSIZE) +#ifdef USE_SOCKET_SELECT_SUPPORT /// These functions update the fd_set used by select() in the main loop. /// WARNING: These functions are NOT thread-safe. They must only be called from the main loop. /// NOTE: File descriptors >= FD_SETSIZE (typically 10 on ESP) will be rejected with an error. @@ -564,7 +564,7 @@ class Application { const char *compilation_time_{nullptr}; bool name_add_mac_suffix_; uint32_t last_loop_{0}; -#if (defined(USE_SOCKET_IMPL_LWIP_SOCKETS) || defined(USE_SOCKET_IMPL_BSD_SOCKETS)) && defined(FD_SETSIZE) +#ifdef USE_SOCKET_SELECT_SUPPORT uint32_t loop_interval_{21}; // Increased interval when select() is available for better efficiency #else uint32_t loop_interval_{16}; // Standard interval for platforms without select() @@ -574,7 +574,7 @@ class Application { Component *current_component_{nullptr}; uint32_t loop_component_start_time_{0}; -#if (defined(USE_SOCKET_IMPL_LWIP_SOCKETS) || defined(USE_SOCKET_IMPL_BSD_SOCKETS)) && defined(FD_SETSIZE) +#ifdef USE_SOCKET_SELECT_SUPPORT // Socket select management std::set socket_fds_; // Set of all monitored socket file descriptors bool socket_fds_changed_{false}; // Flag to rebuild base_read_fds_ when socket_fds_ changes diff --git a/esphome/core/defines.h b/esphome/core/defines.h index 455b404e32..94e8159d1c 100644 --- a/esphome/core/defines.h +++ b/esphome/core/defines.h @@ -7,6 +7,11 @@ #include "esphome/core/macros.h" +// Define a shorter macro for socket select support +#if (defined(USE_SOCKET_IMPL_LWIP_SOCKETS) || defined(USE_SOCKET_IMPL_BSD_SOCKETS)) && defined(FD_SETSIZE) +#define USE_SOCKET_SELECT_SUPPORT +#endif + // Informative flags #define ESPHOME_BOARD "dummy_board" #define ESPHOME_PROJECT_NAME "dummy project"