Commit Graph

14398 Commits

Author SHA1 Message Date
J. Nick Koston eb5da72dab [esphome.ota] Revert inline wake hook to a single Component * + extern C call
With the listener filter from the previous commit, the wake hook only fires
on actual OTA connection attempts — no more spurious wakes from API client
data packets or other monitored-socket traffic. That removes the motivation
for inlining the hook at three call sites.

Collapse back to the simpler shape:
- Application gains Component *ota_wake_component_ (one pointer, 4 bytes,
  gated on USE_OTA) and a wake_ota_component_any_context() inline method.
- lwip_fast_select.c reaches the method via an extern-C trampoline
  (esphome_wake_ota_component_any_context) defined in application.cpp.
  One call_n instruction per actual wake, which now happens at most once
  per real OTA upload attempt.
- Raw-TCP (LWIPRawListenImpl::accept_fn_) and host select paths call
  App.wake_ota_component_any_context() directly — both are .cpp files.
- wake.h reverts to its pre-PR state (no C-compatible section, no extern
  pointer globals, no inline OTA hook). All wake-related state still lives
  in Application.

Net effect versus the inline approach:
- RAM: -4 bytes (one pointer vs two)
- Flash: ~-60 bytes (no 3x duplication of the inlined hook body)
- CPU: function-call overhead (~10 cycles) paid only on actual OTA wakes,
  which happen ~0 times/sec in steady state. Inline was premature once
  filtering reduced the fire rate to "rare intentional events."
2026-04-10 17:22:49 -10:00
J. Nick Koston 1490845dcf [esphome.ota] Use existing esphome_lwip_get_sock() instead of adding Socket accessor
get_cached_sock() was a new public method that only OTA's fast-select wake
filter would ever call. Drop it. The existing public C API already covers
this: esphome_lwip_get_sock(fd) looks up a lwip_sock* from a file descriptor
(it's exactly what BSDSocketImpl's own constructor calls to populate
cached_sock_). OTA uses the public get_fd() + esphome_lwip_get_sock() chain
instead — no new Socket accessor, no friend declarations, no layering
concerns. The one-time lookup at setup is negligible.
2026-04-10 17:15:04 -10:00
J. Nick Koston 01beb56899 [esphome.ota] Filter fast-select wake hook to OTA listener netconn only
The inline OTA wake hook was firing on every NETCONN_EVT_RCVPLUS across every
monitored socket (API client data packets, mDNS queries, web server, etc.).
Each false fire paid two volatile stores + memw barriers to mark OTA
pending-enable, only for OTA::loop() to run a wake-up tick and re-disable
itself because there was no actual listener activity.

Add a compare-against-listener filter in esphome_socket_event_callback so the
wake hook only fires when `conn` matches the OTA listen socket's netconn.
Non-match sockets now cost only a pointer load + one branch (~3 instructions)
instead of the full ~10-instruction hook body.

Plumbing:
- lwip_fast_select.[ch]: new s_ota_listener_conn global +
  esphome_fast_select_set_ota_listener_sock() setter, used in the callback.
- BSDSocketImpl / LwIPSocketImpl: new public get_cached_sock() accessor (only
  under USE_LWIP_FAST_SELECT) mirroring the existing get_fd() pattern.
- ESPHomeOTAComponent::setup(): after registering the wake component,
  install the listener filter with this->server_->get_cached_sock().

Raw TCP (ESP8266/RP2040) is unaffected — that path wakes from
LWIPRawListenImpl::accept_fn_, which only fires for the specific listener
pcb it was registered on, so the filtering is implicit there.
2026-04-10 17:14:00 -10:00
J. Nick Koston 5f04cff8bc [esphome.ota] Fold OTA wake hook into wake.h
All wake_* state lives in one place now. wake.h gains a C-compatible section
at the top (the inline esphome_wake_ota_component_any_context() + its two
extern 'volatile bool *' globals) guarded outside any C++ namespace, with
the existing C++ platform wake primitives moved behind an outer
#ifdef __cplusplus. lwip_fast_select.c includes wake.h directly for the
inline; .cpp files continue to see the C++ side as before.

Deletes the ephemeral esphome/core/ota_wake_hook.h — same code, better home.
2026-04-10 17:01:07 -10:00
J. Nick Koston 1fe2588c08 [esphome.ota] Inline fast-select wake hook via ota_wake_hook.h
The extern C shim esphome_wake_ota_component_any_context() was an out-of-line
call from lwip_fast_select.c into application.cpp: save registers, call,
prologue, two stores, epilogue, ret. Per-RCVPLUS, that's ~10-15 Xtensa cycles
of pure call overhead on top of the two volatile bool stores the shim actually
does.

Move the body into a new C-compatible header (esphome/core/ota_wake_hook.h)
as a static inline, backed by two extern C 'volatile bool *' globals that
point at Component::pending_enable_loop_ and
Application::has_pending_enable_loop_requests_. set_ota_wake_component()
captures the addresses once at registration time; the fast-select callback
then inlines a null-check + two volatile stores with zero call overhead.

The main loop sees the same two flags it already checks every iteration
(has_pending_enable_loop_requests_ gates enable_pending_loops_, which
iterates the inactive section looking for components with
pending_enable_loop_ set). Zero new main-loop work — the inline hook writes
exactly the state enable_loop_soon_any_context() would have written.

RAM change: -4 bytes on Application (ota_wake_component_ field removed) plus
+8 bytes in BSS for the two extern pointers. Net +4 bytes RAM.

Raw-TCP and HOST paths switched from App.wake_ota_component_any_context() to
the inline hook too.
2026-04-10 16:56:48 -10:00
J. Nick Koston 0480f43984 [ota] Use ESPHOME_USE_OTA build flag to avoid USE_OTA redefinition warning
Emitting -DUSE_OTA alongside the defines.h #define USE_OTA entry caused
'USE_OTA redefined' warnings in every TU that includes defines.h. Use a
distinct ESPHOME_USE_OTA name for the compiler -D flag — only the .c
files that cannot include defines.h (lwip_fast_select.c) reference the
ESPHOME_-prefixed name, and everyone else continues to use USE_OTA via
defines.h.
2026-04-10 16:30:22 -10:00
J. Nick Koston 4630c7ad94 [ota] Emit USE_OTA as build flag + define so .c files can see it
Root cause of the wake failure: ota/__init__.py only called
cg.add_define("USE_OTA"), which writes to the generated defines.h. That's
invisible to .c translation units that can't include defines.h — notably
lwip_fast_select.c, which can't include defines.h because macros.h →
Arduino.h under Arduino builds would break the C compile.

So the #ifdef USE_OTA gate inside lwip_fast_select.c's RCVPLUS callback
was always false, and esphome_wake_ota_component_any_context() was never
called. The listener callback fired (total rcvplus counter incremented
from the device), but OTA's pending-enable flag was never set, so the
main task woke and ran a loop iteration without touching the (disabled)
OTA component.

Fix: emit both cg.add_define (keeps defines.h in sync for static
analyzers / IDEs) and cg.add_build_flag("-DUSE_OTA") (passes it as a
compiler -D flag, visible to every .c TU). Also reverts all the
diagnostic scaffolding (per-loop-tick logs, debug counters, runtime
function pointer hook) that I added while chasing this.
2026-04-10 16:28:30 -10:00
J. Nick Koston 08c09fbe36 [esphome.ota] TEMP: add shim call counter to pinpoint C->C++ wake break 2026-04-10 16:20:03 -10:00
J. Nick Koston 9c70986909 [esphome.ota] TEMP: log server_->ready() after setup + add global rcvplus counter 2026-04-10 16:13:56 -10:00
J. Nick Koston e820d485e2 [esphome.ota] TEMP: keep loop enabled to trace wake/ready flow 2026-04-10 16:08:00 -10:00
J. Nick Koston f92c745ae9 [esphome.ota] TEMP: debug logging to diagnose wake/accept race 2026-04-10 16:01:07 -10:00
J. Nick Koston 06285dbb75 [esphome.ota] Comment host wake path as currently dead code 2026-04-10 15:43:10 -10:00
J. Nick Koston ae54f3e071 [esphome.ota] Drop explicit disable_loop() from setup, let loop() self-disable 2026-04-10 15:42:32 -10:00
J. Nick Koston ffbd0dcbfc [esphome.ota] Set pending-enable flags before main-loop wake (fix race)
The wake-hook call (esphome_wake_ota_component_any_context / App.wake_ota_component_any_context)
was placed AFTER xTaskNotifyGive()/wake_loop_any_context() in both the fast-select callback and
the raw-TCP accept callback. That opened a race: the main task could wake, run a full iteration
(draining has_pending_enable_loop_requests_), and finish before the flag-set ran — losing the
pending-enable request until the next unrelated socket event happened to re-trigger the path.

Swap the order so the pending-enable flags are set first, then the main task is woken. The
main-loop iteration triggered by the wake is now guaranteed to see the pending request.

Note: host's yield_with_select_ path already sets and consumes the flag on the main thread
with no cross-task wake in between, so it has no race and is unchanged.
2026-04-10 15:41:57 -10:00
J. Nick Koston 903a159344 [esphome.ota] Fix loop() docstring to reflect cleanup no longer disables 2026-04-10 14:52:12 -10:00
J. Nick Koston 92f93e128f [esphome.ota] Drop unnecessary IRAM_ATTR from wake hook
The wake hook is called only from:
  - esphome_socket_event_callback (lwip fast select, LwIP TCP/IP task context)
  - LWIPRawListenImpl::accept_fn_ (raw TCP accept callback)
  - Application::yield_with_select_ (host select fallback, main thread)

None of those contexts require IRAM-resident code. The LwIP fast-select
event callback itself is not IRAM_ATTR; the raw-TCP accept callback runs
from a low-priority user IRQ on RP2040 where IRAM_ATTR is a no-op anyway
(it's an ESP32-specific section attribute for code that must run while
flash cache is disabled). This is not a real ISR path the way
enable_loop_soon_any_context() is (which is called from GPIO ISRs and
genuinely does need IRAM).

Removing IRAM_ATTR frees scarce IRAM on ESP32.
2026-04-10 14:47:52 -10:00
J. Nick Koston af8fd1d060 [esphome.ota] Fix cleanup race, tighten error message and comments, add tests
Addresses copilot review on #15636.

1. Fix cleanup_connection_() race with queued listener events.
   While an OTA session was active, a second incoming connection would
   fire esphome_socket_event_callback → esphome_wake_ota_component_any_context,
   which sets pending_enable_loop_ on the (still-active) OTA component.
   enable_pending_loops_() only scans the inactive section, so that flag
   goes invisible. When cleanup_connection_() then called disable_loop(),
   the component dropped to LOOP_DONE with a stale pending flag and
   nothing to re-trigger the scan — the queued client sat forever until
   some unrelated socket activity woke the main loop.

   Fix: don't call disable_loop() from cleanup_connection_(). loop() has
   the idempotent idle check at its top; one more dispatch after cleanup
   is cheap and guarantees we re-read server_->ready() and either accept
   the queued client or disable cleanly.

2. Tighten the multi-port error message. Merging is fine — the constraint
   is single-port. Reworded: "Only a single port is supported for 'ota'
   'platform: esphome'. Got ports [...]. Consolidate onto a single port;
   configs sharing a port are merged automatically."

3. Comment drift: three call sites and the fast-select extern declaration
   still referred to enable_loop_soon_any_context() and implied the hook
   wakes the main loop. Updated to reflect the current mechanism (sets
   pending-enable flags only; callers have already woken the main loop).
   Also clarified that esphome_wake_ota_component_any_context fires on
   every RCVPLUS event across all monitored sockets, so false wakes are
   expected and OTA::loop() disables itself again when idle.

4. Added tests/component_tests/ota/test_esphome_ota.py covering
   ota_esphome_final_validate: single instance accepted, same-port
   configs merge, different-port configs rejected with cv.Invalid,
   non-esphome platforms unaffected.
2026-04-10 14:29:32 -10:00
J. Nick Koston ae9c5bab80 [esphome.ota] Drop narrative comment from multi-port validator 2026-04-10 14:11:19 -10:00
J. Nick Koston 0f8419f97d [esphome.ota] Reject multi-port esphome OTA, drop redundant wake
Address copilot review on #15636.

1. Enforce single ESPHome OTA instance (BREAKING CHANGE).
   The `ota_esphome_final_validate` hook has always merged multiple
   `ota: - platform: esphome` configs by port so a user config and a
   remote package that both define OTA would merge rather than break.
   That merge behavior is preserved. But if two configs survive on
   *different* ports they produce two independent listening sockets,
   which is not a sane deployment: it creates ambiguity for safe_mode
   coordination and for the socket wake hook added in this PR.
   Raise cv.Invalid when more than one port remains after merging.

2. Drop redundant main-loop wake in Application::wake_ota_component_any_context.
   Every caller (lwip fast-select callback already calls xTaskNotifyGive,
   raw-tcp accept callback already calls wake_loop_any_context(), host
   path is already running post-select in the main loop) has woken the
   main loop by the time we reach this hook. Calling
   enable_loop_soon_any_context() would re-wake it. Application is a
   friend of Component, so set pending_enable_loop_ and
   has_pending_enable_loop_requests_ directly instead.
2026-04-10 14:09:27 -10:00
J. Nick Koston 5b84ad5926 [esphome.ota] Disable loop while idle, wake on listening-socket activity
ESPHomeOTAComponent::loop() previously ran every main-loop tick just to
check `client_ != nullptr || server_->ready()` — a wasted dispatch on
every device, since OTA is idle the vast majority of the time.

OTA now disables its own loop after setup() and after cleanup_connection_().
A single 4-byte Component* slot in Application (only compiled in under
USE_OTA) lets the existing socket-wake paths call
enable_loop_soon_any_context() on the registered OTA component:

  - ESP32 / LibreTiny (lwip fast select): hooked in
    esphome_socket_event_callback on NETCONN_EVT_RCVPLUS.
  - ESP8266 / RP2040 (raw TCP): hooked in LWIPRawListenImpl::accept_fn_
    right after the existing wake_loop_any_context() call.
  - Host (select fallback): called after select() returns ready in
    Application::yield_with_select_.

False wakes (e.g. an API-socket event firing the fast-select callback)
land in ESPHomeOTAComponent::loop(), which re-disables itself immediately
when idle. Net cost is still far below running every tick.

This is deliberately an OTA-only hook: OTA is the only component that
benefits, and a single global slot avoids adding per-socket Component*
storage, wake-callback lists, or any new API churn to the socket layer.
2026-04-10 13:24:32 -10:00
J. Nick Koston 40081e5ae7 [rp2040] Fix W5500 Ethernet pbuf corruption by mirroring LWIPMutex semantics (#15624)
Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
2026-04-10 13:13:05 -10:00
Jonathan Swoboda a7c5b0ab46 [sx127x][cc1101][sx126x] Use GPIO interrupt to wake loop (#15627) 2026-04-10 16:26:09 -04:00
dependabot[bot] e1a813e11f Bump peter-evans/create-pull-request from 8.1.0 to 8.1.1 (#15630)
Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com>
2026-04-10 10:21:01 -10:00
dependabot[bot] 1dfeef0265 Bump actions/github-script from 8.0.0 to 9.0.0 (#15632)
Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com>
2026-04-10 10:20:43 -10:00
dependabot[bot] 395610c117 Bump docker/build-push-action from 7.0.0 to 7.1.0 in /.github/actions/build-image (#15633)
Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com>
2026-04-10 10:20:17 -10:00
dependabot[bot] ae96f82b82 Bump actions/upload-artifact from 7.0.0 to 7.0.1 (#15631)
Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com>
2026-04-10 10:20:04 -10:00
dependabot[bot] 2c610abcd0 Bump resvg-py from 0.2.6 to 0.3.0 (#15629)
Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com>
2026-04-10 10:19:52 -10:00
Kevin Ahrendt d3591c8d9e [micro_wake_word] Pin esp-nn version (#15628) 2026-04-10 15:21:26 -04:00
J. Nick Koston ec420d5792 [api] Add (inline_encode) proto option for sub-message inlining (#15599) 2026-04-10 15:33:56 +12:00
J. Nick Koston 17209df7b5 [mcp23016] Add interrupt pin support (#15616) 2026-04-10 15:29:52 +12:00
J. Nick Koston 9cf9b02ba2 [pca6416a] Add interrupt pin support (#15614) 2026-04-10 15:29:26 +12:00
J. Nick Koston c90fa2378a [tca9555] Add interrupt pin support (#15613) 2026-04-10 15:29:00 +12:00
Jesse Hills c04dfa922e [hbridge] Move light pin switching to loop (#15615) 2026-04-10 14:02:49 +12:00
Jesse Hills 668007707d [CI] Add org fork detection warning to auto-label PR workflow (#15588) 2026-04-10 12:13:22 +12:00
dependabot[bot] ab71f5276f Bump ruff from 0.15.9 to 0.15.10 (#15609)
Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com>
Co-authored-by: J. Nick Koston <nick@home-assistant.io>
2026-04-09 19:36:25 +00:00
Jonathan Swoboda d062f62656 [sx127x][cc1101] Disable loop when packet mode is inactive (#15606) 2026-04-09 15:00:52 -04:00
J. Nick Koston 03db32d045 [core] Add CodSpeed benchmarks for hot helper functions (#15593) 2026-04-09 07:48:32 -10:00
J. Nick Koston 8f6d489a9a [ci] Use --base-only for memory impact builds (#15598) 2026-04-09 11:48:33 -04:00
J. Nick Koston dd07fba943 [socket] Document ready() contract: callers must drain or track (#15590) 2026-04-09 11:48:18 -04:00
J. Nick Koston 6f5d642a31 [gdk101] Increase reset retries for slow-booting sensor MCU (#15584) 2026-04-09 11:48:10 -04:00
dependabot[bot] 2721f08bcc Bump aioesphomeapi from 44.12.0 to 44.13.1 (#15600)
Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com>
2026-04-09 10:03:58 +00:00
J. Nick Koston eafc5df3f2 [safe_mode] Combine related OTA rollback log messages (#15592) 2026-04-09 05:30:39 +00:00
J. Nick Koston 46d0c29be5 [safe_mode] Use loop component start time instead of millis() (#15591) 2026-04-09 05:20:32 +00:00
J. Nick Koston abdbbf4dd2 [api] Fix ListEntitiesRequest not read due to LWIP rcvevent tracking (#15589) 2026-04-09 02:14:01 +00:00
Jesse Hills 4dc0599a7d Merge branch 'beta' into dev 2026-04-09 13:41:27 +12:00
Jesse Hills ded0936b2a Merge pull request #15587 from esphome/bump-2026.4.0b1
2026.4.0b1
2026.4.0b1
2026-04-09 13:40:37 +12:00
Jesse Hills 52c35ec09c Bump version to 2026.5.0-dev 2026-04-09 11:28:48 +12:00
J. Nick Koston 76490e45bc [ci] Fix status-check-labels workflow flooding CI queue (#15585) 2026-04-08 13:08:29 -10:00
Angel Nunez Mencias 0a8130858c [ade7953_spi] Fix SPI mode on esp-idf (#14824)
Co-authored-by: Copilot Autofix powered by AI <175728472+Copilot@users.noreply.github.com>
Co-authored-by: J. Nick Koston <nick@koston.org>
Co-authored-by: J. Nick Koston <nick@home-assistant.io>
2026-04-08 22:57:53 +00:00
Jesse Hills ff5ba99d16 Bump version to 2026.4.0b1 2026-04-09 10:39:13 +12:00