mirror of
https://github.com/esphome/esphome.git
synced 2026-07-10 08:55:36 +00:00
Stop setting USE_SOCKET_SELECT_SUPPORT on fast-select platforms
USE_SOCKET_SELECT_SUPPORT now exclusively means 'host select() fallback'. ESP32/LibreTiny use USE_LWIP_FAST_SELECT instead. This eliminates all 'USE_SOCKET_SELECT_SUPPORT && !USE_LWIP_FAST_SELECT' guards.
This commit is contained in:
@@ -161,16 +161,16 @@ async def to_code(config):
|
||||
cg.add_define("USE_SOCKET_IMPL_LWIP_TCP")
|
||||
elif impl == IMPLEMENTATION_LWIP_SOCKETS:
|
||||
cg.add_define("USE_SOCKET_IMPL_LWIP_SOCKETS")
|
||||
cg.add_define("USE_SOCKET_SELECT_SUPPORT")
|
||||
elif impl == IMPLEMENTATION_BSD_SOCKETS:
|
||||
cg.add_define("USE_SOCKET_IMPL_BSD_SOCKETS")
|
||||
cg.add_define("USE_SOCKET_SELECT_SUPPORT")
|
||||
# ESP32 and LibreTiny both have LwIP >= 2.1.3 with lwip_socket_dbg_get_socket()
|
||||
# and FreeRTOS task notifications — enable fast select to bypass lwip_select().
|
||||
# Only when not using lwip_tcp, which does not provide select() support.
|
||||
if (CORE.is_esp32 or CORE.is_libretiny) and impl != IMPLEMENTATION_LWIP_TCP:
|
||||
cg.add_build_flag("-DUSE_LWIP_FAST_SELECT")
|
||||
elif impl != IMPLEMENTATION_LWIP_TCP:
|
||||
# Host platform: uses select() syscall for socket monitoring
|
||||
cg.add_define("USE_SOCKET_SELECT_SUPPORT")
|
||||
# Platforms with select() but without fast select (host) need a UDP
|
||||
# loopback socket for wake_loop_threadsafe().
|
||||
consume_sockets(1, "socket.wake_loop_threadsafe", SocketType.UDP)({})
|
||||
|
||||
@@ -8,7 +8,7 @@
|
||||
|
||||
namespace esphome::socket {
|
||||
|
||||
#if defined(USE_SOCKET_SELECT_SUPPORT) && !defined(USE_LWIP_FAST_SELECT)
|
||||
#ifdef USE_SOCKET_SELECT_SUPPORT
|
||||
// Shared ready() implementation for fd-based socket implementations (BSD and LWIP sockets).
|
||||
// Checks if the Application's select() loop has marked this fd as ready.
|
||||
bool socket_ready_fd(int fd, bool loop_monitored) { return !loop_monitored || App.is_socket_ready_(fd); }
|
||||
|
||||
@@ -128,13 +128,13 @@ void Application::setup() {
|
||||
clear_setup_priority_overrides();
|
||||
#endif
|
||||
|
||||
#if defined(USE_SOCKET_SELECT_SUPPORT) && defined(USE_LWIP_FAST_SELECT)
|
||||
#ifdef USE_LWIP_FAST_SELECT
|
||||
// Initialize fast select: saves main loop task handle for xTaskNotifyGive wake.
|
||||
// The fast path (rcvevent reads + ulTaskNotifyTake) is used unconditionally
|
||||
// when USE_LWIP_FAST_SELECT is enabled (ESP32 and LibreTiny).
|
||||
esphome_lwip_fast_select_init();
|
||||
#endif
|
||||
#if defined(USE_SOCKET_SELECT_SUPPORT) && !defined(USE_LWIP_FAST_SELECT)
|
||||
#ifdef USE_SOCKET_SELECT_SUPPORT
|
||||
// Set up wake socket for waking main loop from tasks (platforms without fast select only)
|
||||
this->setup_wake_loop_threadsafe_();
|
||||
#endif
|
||||
@@ -547,7 +547,7 @@ void Application::unregister_socket_fd(int fd) {
|
||||
#endif
|
||||
|
||||
// Only the select() fallback path remains in the .cpp — all other paths are inlined in application.h
|
||||
#if defined(USE_SOCKET_SELECT_SUPPORT) && !defined(USE_LWIP_FAST_SELECT)
|
||||
#ifdef USE_SOCKET_SELECT_SUPPORT
|
||||
void Application::yield_with_select_(uint32_t delay_ms) {
|
||||
// Fallback select() path (host platform and any future platforms without fast select).
|
||||
if (!this->socket_fds_.empty()) [[likely]] {
|
||||
@@ -597,7 +597,7 @@ void Application::yield_with_select_(uint32_t delay_ms) {
|
||||
// No sockets registered or select() failed - use regular delay
|
||||
delay(delay_ms);
|
||||
}
|
||||
#endif // defined(USE_SOCKET_SELECT_SUPPORT) && !defined(USE_LWIP_FAST_SELECT)
|
||||
#endif // USE_SOCKET_SELECT_SUPPORT
|
||||
|
||||
// App storage — asm label shares the linker symbol with "extern Application App".
|
||||
// char[] is trivially destructible, so no __cxa_atexit or destructor chain is emitted.
|
||||
@@ -620,7 +620,7 @@ alignas(Application) char app_storage[sizeof(Application)] asm(
|
||||
|
||||
// Host platform wake_loop_threadsafe() and setup — needs wake_socket_fd_
|
||||
// ESP32/LibreTiny/ESP8266/RP2040 implementations are in wake.cpp
|
||||
#if defined(USE_SOCKET_SELECT_SUPPORT) && !defined(USE_LWIP_FAST_SELECT)
|
||||
#ifdef USE_SOCKET_SELECT_SUPPORT
|
||||
|
||||
void Application::setup_wake_loop_threadsafe_() {
|
||||
// Create UDP socket for wake notifications
|
||||
|
||||
+14
-27
@@ -25,20 +25,9 @@
|
||||
#endif
|
||||
|
||||
#ifdef USE_SOCKET_SELECT_SUPPORT
|
||||
#ifdef USE_LWIP_FAST_SELECT
|
||||
#include "esphome/core/lwip_fast_select.h"
|
||||
#ifdef USE_ESP32
|
||||
#include <freertos/FreeRTOS.h>
|
||||
#include <freertos/task.h>
|
||||
#else
|
||||
#include <FreeRTOS.h>
|
||||
#include <task.h>
|
||||
#endif
|
||||
#else
|
||||
#include <sys/select.h>
|
||||
#include <lwip/sockets.h>
|
||||
#endif
|
||||
#endif // USE_SOCKET_SELECT_SUPPORT
|
||||
#ifdef USE_RUNTIME_STATS
|
||||
#include "esphome/components/runtime_stats/runtime_stats.h"
|
||||
#endif
|
||||
@@ -565,7 +554,7 @@ class Application {
|
||||
|
||||
protected:
|
||||
friend Component;
|
||||
#if defined(USE_SOCKET_SELECT_SUPPORT) && !defined(USE_LWIP_FAST_SELECT)
|
||||
#ifdef USE_SOCKET_SELECT_SUPPORT
|
||||
friend bool socket::socket_ready_fd(int fd, bool loop_monitored);
|
||||
#endif
|
||||
#ifdef USE_RUNTIME_STATS
|
||||
@@ -573,11 +562,11 @@ class Application {
|
||||
#endif
|
||||
friend void ::setup();
|
||||
friend void ::original_setup();
|
||||
#if defined(USE_SOCKET_SELECT_SUPPORT) && !defined(USE_LWIP_FAST_SELECT)
|
||||
#ifdef USE_SOCKET_SELECT_SUPPORT
|
||||
friend void wake_loop_threadsafe(); // Host platform accesses wake_socket_fd_
|
||||
#endif
|
||||
|
||||
#if defined(USE_SOCKET_SELECT_SUPPORT) && !defined(USE_LWIP_FAST_SELECT)
|
||||
#ifdef USE_SOCKET_SELECT_SUPPORT
|
||||
bool is_socket_ready_(int fd) const { return FD_ISSET(fd, &this->read_fds_); }
|
||||
#endif
|
||||
|
||||
@@ -622,14 +611,14 @@ class Application {
|
||||
void feed_wdt_arch_();
|
||||
|
||||
/// Perform a delay while also monitoring socket file descriptors for readiness
|
||||
#if defined(USE_SOCKET_SELECT_SUPPORT) && !defined(USE_LWIP_FAST_SELECT)
|
||||
#ifdef USE_SOCKET_SELECT_SUPPORT
|
||||
// select() fallback path is too complex to inline (host platform)
|
||||
void yield_with_select_(uint32_t delay_ms);
|
||||
#else
|
||||
inline void ESPHOME_ALWAYS_INLINE yield_with_select_(uint32_t delay_ms);
|
||||
#endif
|
||||
|
||||
#if defined(USE_SOCKET_SELECT_SUPPORT) && !defined(USE_LWIP_FAST_SELECT)
|
||||
#ifdef USE_SOCKET_SELECT_SUPPORT
|
||||
void setup_wake_loop_threadsafe_(); // Create wake notification socket
|
||||
inline void drain_wake_notifications_(); // Read pending wake notifications in main loop (hot path - inlined)
|
||||
#endif
|
||||
@@ -663,9 +652,7 @@ class Application {
|
||||
std::vector<int> socket_fds_; // Vector of all monitored socket file descriptors
|
||||
#endif
|
||||
#ifdef USE_SOCKET_SELECT_SUPPORT
|
||||
#if !defined(USE_LWIP_FAST_SELECT)
|
||||
int wake_socket_fd_{-1}; // Shared wake notification socket for waking main loop from tasks
|
||||
#endif
|
||||
#endif
|
||||
|
||||
// StringRef members (8 bytes each: pointer + size)
|
||||
@@ -676,7 +663,7 @@ class Application {
|
||||
uint32_t last_loop_{0};
|
||||
uint32_t loop_component_start_time_{0};
|
||||
|
||||
#if defined(USE_SOCKET_SELECT_SUPPORT) && !defined(USE_LWIP_FAST_SELECT)
|
||||
#ifdef USE_SOCKET_SELECT_SUPPORT
|
||||
int max_fd_{-1}; // Highest file descriptor number for select()
|
||||
#endif
|
||||
|
||||
@@ -692,11 +679,11 @@ class Application {
|
||||
bool in_loop_{false};
|
||||
volatile bool has_pending_enable_loop_requests_{false};
|
||||
|
||||
#if defined(USE_SOCKET_SELECT_SUPPORT) && !defined(USE_LWIP_FAST_SELECT)
|
||||
#ifdef USE_SOCKET_SELECT_SUPPORT
|
||||
bool socket_fds_changed_{false}; // Flag to rebuild base_read_fds_ when socket_fds_ changes
|
||||
#endif
|
||||
|
||||
#if defined(USE_SOCKET_SELECT_SUPPORT) && !defined(USE_LWIP_FAST_SELECT)
|
||||
#ifdef USE_SOCKET_SELECT_SUPPORT
|
||||
// Variable-sized members (not needed with fast select — is_socket_ready_ reads rcvevent directly)
|
||||
fd_set read_fds_{}; // Working fd_set: populated by select()
|
||||
fd_set base_read_fds_{}; // Cached fd_set rebuilt only when socket_fds_ changes
|
||||
@@ -789,7 +776,7 @@ class Application {
|
||||
/// Global storage of Application pointer - only one Application can exist.
|
||||
extern Application App; // NOLINT(cppcoreguidelines-avoid-non-const-global-variables)
|
||||
|
||||
#if defined(USE_SOCKET_SELECT_SUPPORT) && !defined(USE_LWIP_FAST_SELECT)
|
||||
#ifdef USE_SOCKET_SELECT_SUPPORT
|
||||
// Inline implementations for hot-path functions
|
||||
// drain_wake_notifications_() is called on every loop iteration
|
||||
|
||||
@@ -811,10 +798,10 @@ inline void Application::drain_wake_notifications_() {
|
||||
}
|
||||
}
|
||||
}
|
||||
#endif // defined(USE_SOCKET_SELECT_SUPPORT) && !defined(USE_LWIP_FAST_SELECT)
|
||||
#endif // USE_SOCKET_SELECT_SUPPORT
|
||||
|
||||
inline void ESPHOME_ALWAYS_INLINE Application::before_loop_tasks_(uint32_t loop_start_time) {
|
||||
#if defined(USE_SOCKET_SELECT_SUPPORT) && !defined(USE_LWIP_FAST_SELECT)
|
||||
#ifdef USE_SOCKET_SELECT_SUPPORT
|
||||
// Drain wake notifications first to clear socket for next wake
|
||||
this->drain_wake_notifications_();
|
||||
#endif
|
||||
@@ -905,9 +892,9 @@ inline void ESPHOME_ALWAYS_INLINE Application::loop() {
|
||||
}
|
||||
|
||||
// Inline yield_with_select_ for all paths except the select() fallback
|
||||
#if !defined(USE_SOCKET_SELECT_SUPPORT) || defined(USE_LWIP_FAST_SELECT)
|
||||
#ifndef USE_SOCKET_SELECT_SUPPORT
|
||||
inline void ESPHOME_ALWAYS_INLINE Application::yield_with_select_(uint32_t delay_ms) {
|
||||
#if defined(USE_SOCKET_SELECT_SUPPORT) && defined(USE_LWIP_FAST_SELECT)
|
||||
#ifdef USE_LWIP_FAST_SELECT
|
||||
// Fast path (ESP32/LibreTiny): reads rcvevent directly from cached lwip_sock pointers.
|
||||
// Safe because this runs on the main loop which owns socket lifetime (create, read, close).
|
||||
if (delay_ms == 0) [[unlikely]] {
|
||||
@@ -934,6 +921,6 @@ inline void ESPHOME_ALWAYS_INLINE Application::yield_with_select_(uint32_t delay
|
||||
esphome::wakeable_delay(delay_ms);
|
||||
#endif
|
||||
}
|
||||
#endif // !defined(USE_SOCKET_SELECT_SUPPORT) || defined(USE_LWIP_FAST_SELECT)
|
||||
#endif // !USE_SOCKET_SELECT_SUPPORT
|
||||
|
||||
} // namespace esphome
|
||||
|
||||
@@ -251,7 +251,6 @@
|
||||
#define USE_SENDSPIN
|
||||
#define USE_SENDSPIN_PORT 8928 // NOLINT
|
||||
#define USE_SOCKET_IMPL_BSD_SOCKETS
|
||||
#define USE_SOCKET_SELECT_SUPPORT
|
||||
#define USE_LWIP_FAST_SELECT
|
||||
|
||||
#define USE_SPEAKER
|
||||
@@ -377,7 +376,6 @@
|
||||
#ifdef USE_LIBRETINY
|
||||
#define USE_CAPTIVE_PORTAL
|
||||
#define USE_SOCKET_IMPL_LWIP_SOCKETS
|
||||
#define USE_SOCKET_SELECT_SUPPORT
|
||||
#define USE_LWIP_FAST_SELECT
|
||||
#define USE_WEBSERVER
|
||||
#define USE_WEBSERVER_AUTH
|
||||
|
||||
Reference in New Issue
Block a user