Compare commits

...
Author SHA1 Message Date
Jesse Hills e27614fda3 Merge pull request #22833 from esphome/bump-2026.11.0b6
2026.11.0b6
2026-11-17 22:24:55 +13:00
J. Nick Koston 8426d04138 [api] Make the event order backpressure test wait until the pipe is full (#22112) 2026-11-17 21:09:01 +13:00
Jesse Hills 4a39ca1b71 Fix 2026-11-17 20:54:40 +13:00
Jesse Hills 4a218fdbd8 Remove extra test 2026-11-17 20:47:40 +13:00
Jesse Hills d8fc1cd214 Bump version to 2026.11.0b6 2026-11-17 18:54:23 +13:00
J. Nick Koston 1e030b7371 [modem] Set the APN while the SIM is powered down (#22817) 2026-11-17 18:54:20 +13:00
J. Nick Koston 3b113f2c39 [wifi] Join the next network after a failed hidden network probe on ESP32 (#22764) 2026-11-17 18:54:20 +13:00
J. Nick Koston 73468592a9 [remote_base] Fix the Haier and Mirage remote_receiver binary sensors failing to compile 2026-11-17 18:54:06 +13:00
Jonathan Swoboda a1b456150d [esp32][libretiny] Make delay() wait at least the requested time (#22822) 2026-11-17 18:53:11 +13:00
J. Nick Koston 865a883abb [remote_base] Fix the canalsatld binary sensor and trigger build 2026-11-17 18:53:11 +13:00
J. Nick Koston 357a6717c8 [core] Release only the lwIP core lock a LwIPLock took (#22683) 2026-11-17 18:53:11 +13:00
J. Nick Koston bcb434e05e [esp32_ble] Stop Bluedroid re-running a failed queued MTU or execute write request (#22670) 2026-11-17 18:53:11 +13:00
J. Nick Koston fb65fcc5f6 [esp32_power_management] Clamp the tick step after light sleep so it cannot trip vTaskStepTick (#22637) 2026-11-17 18:53:11 +13:00
J. Nick Koston b5e379a9d4 [uart] Stop ESP8266 software serial TX from tripping the Wi-Fi watchdog at 57600 baud (#22641) 2026-11-17 18:53:11 +13:00
J. Nick Kostonandpre-commit-ci-lite[bot] 05286dfd5d [api] Send queued entity states before DisconnectRequest on shutdown (#22625)
Co-authored-by: pre-commit-ci-lite[bot] <117423508+pre-commit-ci-lite[bot]@users.noreply.github.com>
2026-11-17 18:53:11 +13:00
J. Nick Koston 36184f09aa [zephyr] Count micros() in microseconds and busy-wait in delayMicroseconds() (#22612) 2026-11-17 18:53:11 +13:00
Adam Matthews 449933891e [wifi] Clear the error flag when the adapter restart is skipped (#22797) 2026-11-17 18:53:11 +13:00
Jonathan Swoboda a5bfa6dac3 [esp32_ble] Apply the Bluedroid workarounds to the ESP-IDF 6.1 releases that need them (#22806) 2026-11-17 18:53:11 +13:00
Jonathan Swoboda 5aaa5b0552 [esp32_ble] Replace Bluedroid's sscanf callers on ESP-IDF 6 too (#22805) 2026-11-17 18:53:11 +13:00
J. Nick Koston 9ab090a818 [i2c] Refuse RP2040 writes larger than the Wire buffer instead of leaving the bus stuck (#22791) 2026-11-17 18:53:10 +13:00
50 changed files with 1195 additions and 155 deletions

No files matched your search

+1 -1
View File
@@ -48,7 +48,7 @@ PROJECT_NAME = ESPHome
# could be handy for archiving the generated documentation or if some version
# control system is used.
PROJECT_NUMBER = 2026.11.0b5
PROJECT_NUMBER = 2026.11.0b6
# Using the PROJECT_BRIEF tag one can provide an optional one line description
# for a project that appears at the top of each page and should give viewer a
+2 -1
View File
@@ -784,8 +784,9 @@ class APIConnection final : public APIServerConnectionBase {
}
// Helper function to schedule a high priority message at the front of the batch
// Out-of-line: callers (on_shutdown, check_keepalive_) are cold paths
// Out-of-line: callers (send_shutdown_disconnect_, check_keepalive_) are cold paths
bool schedule_message_front_(EntityBase *entity, uint16_t message_type, uint8_t estimated_size);
void send_shutdown_disconnect_();
// Helper function to log client messages with name and peername
void log_client_(int level, const LogString *message);
+28 -9
View File
@@ -3,6 +3,7 @@
#include <cerrno>
#include <cinttypes>
#include "api_connection.h"
#include "api_connection_buffer.h"
#include "esphome/components/network/util.h"
#include "esphome/core/application.h"
#include "esphome/core/defines.h"
@@ -794,6 +795,31 @@ void APIServer::on_camera_image(const std::shared_ptr<camera::CameraImage> &imag
}
#endif
// Kept out of api_connection.cpp so GCC inlines its send path as before
void APIConnection::send_shutdown_disconnect_() {
#ifndef USE_DEEP_SLEEP
// Queued states first: Home Assistant mishandles states after DisconnectRequest (#22610).
// Not with deep sleep: its entities stay available, and the disconnect must not wait on a slow link
this->flags_.state_subscription = false;
this->destroy_active_iterator_();
// process_batch_ first flushes any overflow without blocking; stop once the queue stops shrinking
for (size_t left = this->deferred_batch_.size(); left != 0;) {
this->process_batch_();
const size_t now_left = this->deferred_batch_.size();
if (now_left >= left)
break;
left = now_left;
}
#endif
DisconnectRequest req;
if (!this->send_message(req)) {
// tx buffer full: first in the queue, so a slow link still delivers it before the teardown timeout.
// Due at once: teardown does not advance the cached loop time the batch_delay gate reads
this->schedule_message_front_(nullptr, DisconnectRequest::MESSAGE_TYPE, DisconnectRequest::ESTIMATED_SIZE);
this->make_batch_due_now_();
}
}
void APIServer::on_shutdown() {
this->shutting_down_ = true;
@@ -806,15 +832,8 @@ void APIServer::on_shutdown() {
// Change batch delay to 5ms for quick flushing during shutdown
this->batch_delay_ = 5;
// Send disconnect requests to all connected clients
for (auto &c : this->active_clients()) {
DisconnectRequest req;
if (!c->send_message(req)) {
// If we can't send the disconnect request directly (tx_buffer full),
// schedule it at the front of the batch so it will be sent with priority
c->schedule_message_front_(nullptr, DisconnectRequest::MESSAGE_TYPE, DisconnectRequest::ESTIMATED_SIZE);
}
}
for (auto &c : this->active_clients())
c->send_shutdown_disconnect_();
}
bool APIServer::teardown() {
+4 -1
View File
@@ -37,7 +37,10 @@ __attribute__((always_inline)) inline bool is_main_loop_thread() {
return xTaskGetCurrentTaskHandle() == esphome_main_task_handle;
}
__attribute__((always_inline)) inline void yield() { vPortYield(); }
__attribute__((always_inline)) inline void delay(uint32_t ms) { vTaskDelay(ms / portTICK_PERIOD_MS); }
// vTaskDelay() counts the partial tick, so one more tick makes the wait at least ms
__attribute__((always_inline)) inline void delay(uint32_t ms) {
vTaskDelay(ms == 0 ? 0 : (ms + portTICK_PERIOD_MS - 1) / portTICK_PERIOD_MS + 1);
}
__attribute__((always_inline)) inline uint32_t micros() { return static_cast<uint32_t>(esp_timer_get_time()); }
uint32_t millis();
__attribute__((always_inline)) inline uint64_t millis_64() {
+4 -20
View File
@@ -33,34 +33,18 @@ IRAM_ATTR InterruptLock::~InterruptLock() { portENABLE_INTERRUPTS(); }
LwIPLock::LwIPLock() {
#ifdef CONFIG_LWIP_TCPIP_CORE_LOCKING
// When CONFIG_LWIP_TCPIP_CORE_LOCKING is enabled, lwIP uses a global mutex to protect
// its internal state. Any thread can take this lock to safely access lwIP APIs.
//
// sys_thread_tcpip(LWIP_CORE_LOCK_QUERY_HOLDER) returns true if the current thread
// already holds the lwIP core lock. This prevents recursive locking attempts and
// allows nested LwIPLock instances to work correctly.
//
// If we don't already hold the lock, acquire it. This will block until the lock
// is available if another thread currently holds it.
// A thread that already holds the core lock (lwIP took it for a tcpip_api_call or netif callback, or an
// outer LwIPLock did) must not take it again, and must not release it when this nested guard ends.
if (!sys_thread_tcpip(LWIP_CORE_LOCK_QUERY_HOLDER)) {
LOCK_TCPIP_CORE();
this->locked_ = true;
}
#endif
}
LwIPLock::~LwIPLock() {
#ifdef CONFIG_LWIP_TCPIP_CORE_LOCKING
// Only release the lwIP core lock if this thread currently holds it.
//
// sys_thread_tcpip(LWIP_CORE_LOCK_QUERY_HOLDER) queries lwIP's internal lock
// ownership tracking. It returns true only if the current thread is registered
// as the lock holder.
//
// This check is essential because:
// 1. We may not have acquired the lock in the constructor (if we already held it)
// 2. The lock might have been released by other means between constructor and destructor
// 3. Calling UNLOCK_TCPIP_CORE() without holding the lock causes undefined behavior
if (sys_thread_tcpip(LWIP_CORE_LOCK_QUERY_HOLDER)) {
if (this->locked_) {
UNLOCK_TCPIP_CORE();
}
#endif
+42 -13
View File
@@ -567,30 +567,46 @@ async def to_code(config: ConfigType) -> None:
request_bluetooth()
# Replace Bluedroid's only two sscanf callers so newlib's scanf engine is not
# linked; see bluedroid_hex_wraps.cpp. IDF 6.0+ uses picolibc, not newlib.
if (
not CORE.using_arduino
and idf_version() < cv.Version(6, 0, 0)
and get_esp32_variant() not in ROM_SSCANF_VARIANTS
# Replace Bluedroid's only two sscanf callers so the C library's scanf engine is
# not linked; see bluedroid_hex_wraps.cpp. The ROM only provides sscanf for newlib,
# which IDF 6.0 replaced with picolibc.
if not CORE.using_arduino and (
idf_version() >= cv.Version(6, 0, 0)
or get_esp32_variant() not in ROM_SSCANF_VARIANTS
):
cg.add_define("USE_ESP32_BLE_BLUEDROID_HEX_WRAPS")
_wrap_bluedroid_symbols("btc_config_get_bin", "string_to_bdaddr")
# The headers only feed bluedroid_hex_wraps.cpp's ESP-IDF drift checks
add_src_cmake(bluedroid_headers_cmake("bluedroid_hex_wraps.cpp"))
# ESP-IDF 5.5.6 drops bond keys on re-encryption (see bluedroid_bond_keys.c). Needs the
# native toolchain for Bluedroid's private headers; Windows response files lose them.
# ESP-IDF 5.5.6 and 6.1.1 drop bond keys on re-encryption (see bluedroid_bond_keys.c). Needs
# the native toolchain for Bluedroid's private headers; Windows response files lose them.
if (
CORE.using_toolchain_esp_idf
and not IS_WINDOWS
and cv.Version(5, 5, 6) <= idf_version() < cv.Version(6, 0, 0)
and _drops_bond_keys(idf_version())
):
cg.add_define("USE_ESP32_BLE_BLUEDROID_BOND_KEYS")
_wrap_bluedroid_symbols("smp_check_auth_req", "btm_sec_clear_ble_keys")
add_src_cmake(bluedroid_headers_cmake("bluedroid_bond_keys.c"))
# ESP-IDF 5.5.6 and 6.0.3 end GATT discovery on a response listing handles out of order
# Bluedroid runs a queued MTU or execute write request that fails at once again until the BTU task stack
# overflows (see bluedroid_gattc_queue.c); every ESP-IDF release so far. Private headers, as above.
if (
CORE.using_toolchain_esp_idf
and not IS_WINDOWS
and _has_gatt_client(CORE.config)
):
cg.add_define("USE_ESP32_BLE_BLUEDROID_GATTC_QUEUE")
_wrap_bluedroid_symbols(
"bta_gattc_cfg_mtu",
"bta_gattc_execute",
"GATTC_ConfigureMTU",
"GATTC_ExecuteWrite",
)
add_src_cmake(bluedroid_headers_cmake("bluedroid_gattc_queue.c"))
# ESP-IDF 5.5.6, 6.0.3 and 6.1.1 end GATT discovery on a response listing handles out of order
# (see bluedroid_gattc_order.cpp). It needs no Bluedroid headers, so any toolchain works.
if _has_gatt_client(CORE.config) and _has_strict_handle_order(idf_version()):
cg.add_define("USE_ESP32_BLE_GATTC_HANDLE_ORDER")
@@ -667,11 +683,23 @@ def _has_gatt_client(config: ConfigType) -> bool:
return "esp32_ble_tracker" in config or "esp32_ble_client" in config
def _has_strict_handle_order(version: cv.Version) -> bool:
"""Whether this ESP-IDF rejects discovery responses whose handles are out of order."""
def _drops_bond_keys(version: cv.Version) -> bool:
"""Whether this ESP-IDF clears a bonded device's keys when it re-encrypts."""
return cv.Version(5, 5, 6) <= version < cv.Version(
6, 0, 0
) or version >= cv.Version(6, 0, 3)
) or version >= cv.Version(6, 1, 1)
def _has_strict_handle_order(version: cv.Version) -> bool:
"""Whether this ESP-IDF rejects discovery responses whose handles are out of order.
ESP-IDF 6.1.0 was released before the check reached the 6.1 line.
"""
return (
cv.Version(5, 5, 6) <= version < cv.Version(6, 0, 0)
or cv.Version(6, 0, 3) <= version < cv.Version(6, 1, 0)
or version >= cv.Version(6, 1, 1)
)
# ble_advertising.cpp is fully #ifdef'd on USE_ESP32_BLE_ADVERTISING, set
@@ -681,6 +709,7 @@ FILTER_SOURCE_FILES = filter_source_files_from_defines(
"ble_advertising.cpp": "USE_ESP32_BLE_ADVERTISING",
"bluedroid_hex_wraps.cpp": "USE_ESP32_BLE_BLUEDROID_HEX_WRAPS",
"bluedroid_bond_keys.c": "USE_ESP32_BLE_BLUEDROID_BOND_KEYS",
"bluedroid_gattc_queue.c": "USE_ESP32_BLE_BLUEDROID_GATTC_QUEUE",
"bluedroid_gattc_order.cpp": "USE_ESP32_BLE_GATTC_HANDLE_ORDER",
}
)
@@ -1,4 +1,4 @@
// Linker wraps that keep a bonded device's keys when it re-encrypts without pairing again (ESP-IDF 5.5.6).
// Linker wraps that keep a bonded device's keys when it re-encrypts without pairing again (ESP-IDF 5.5.6, 6.1.1).
// Same guard as espressif/esp-idf#19322; drop once ESPHome uses an ESP-IDF release that contains it.
// C, because Bluedroid's btm_ble_int.h does not close its extern "C" block.
@@ -1,5 +1,5 @@
// Linker wrap that puts a GATT discovery response back in handle order before Bluedroid parses it.
// ESP-IDF 5.5.6 and 6.0.3 end the whole discovery when a peer lists handles out of order (esphome#22518), which
// ESP-IDF 5.5.6, 6.0.3 and 6.1.1 end the whole discovery when a peer lists handles out of order (esphome#22518), which
// earlier releases, Android and BlueZ accept.
#include "esphome/core/defines.h"
@@ -0,0 +1,67 @@
// Linker wraps that stop Bluedroid running a failed queued MTU or execute write request again until the BTU task
// stack overflows (esphome#17954). The same fix as removing the command from p_cmd_list in bta_gattc_act.c before
// the failure path pops the queue; drop once an ESP-IDF release has it.
// Compiled only with Bluedroid's private headers (bluedroid_headers_cmake); static analysis skips it
#ifdef USE_ESP32_BLE_BLUEDROID_HEADERS
#include <stddef.h>
#include "bta_gattc_int.h"
#include "osi/list.h"
// The command bta_gattc_cfg_mtu() or bta_gattc_execute() is running, NULL outside them
static tBTA_GATTC_CLCB *running_clcb = NULL;
static tBTA_GATTC_DATA *running_cmd = NULL;
void __real_bta_gattc_cfg_mtu(tBTA_GATTC_CLCB *p_clcb, tBTA_GATTC_DATA *p_data);
void __real_bta_gattc_execute(tBTA_GATTC_CLCB *p_clcb, tBTA_GATTC_DATA *p_data);
tGATT_STATUS __real_GATTC_ConfigureMTU(UINT16 conn_id);
tGATT_STATUS __real_GATTC_ExecuteWrite(UINT16 conn_id, BOOLEAN is_execute);
// Runs on the Bluetooth host task; the failure path pops the queue again, so this nests
static void run_command(void (*real)(tBTA_GATTC_CLCB *, tBTA_GATTC_DATA *), tBTA_GATTC_CLCB *p_clcb,
tBTA_GATTC_DATA *p_data) {
tBTA_GATTC_CLCB *const outer_clcb = running_clcb;
tBTA_GATTC_DATA *const outer_cmd = running_cmd;
running_clcb = p_clcb;
running_cmd = p_data;
real(p_clcb, p_data);
running_clcb = outer_clcb;
running_cmd = outer_cmd;
}
// The send failed, so the caller clears p_q_cmd and pops the queue. A command popped from p_cmd_list is still its
// head and would run again; remove (and free) it first. A command not from the queue is not in it: a no-op.
static void drop_failed_command(void) {
if (running_clcb != NULL && running_clcb->p_cmd_list != NULL && running_clcb->p_q_cmd == running_cmd) {
list_remove(running_clcb->p_cmd_list, running_cmd);
}
}
void __wrap_bta_gattc_cfg_mtu(tBTA_GATTC_CLCB *p_clcb, tBTA_GATTC_DATA *p_data) {
run_command(__real_bta_gattc_cfg_mtu, p_clcb, p_data);
}
void __wrap_bta_gattc_execute(tBTA_GATTC_CLCB *p_clcb, tBTA_GATTC_DATA *p_data) {
run_command(__real_bta_gattc_execute, p_clcb, p_data);
}
// The failure checks are the ones bta_gattc_cfg_mtu() and bta_gattc_execute() make
tGATT_STATUS __wrap_GATTC_ConfigureMTU(UINT16 conn_id) {
const tGATT_STATUS status = __real_GATTC_ConfigureMTU(conn_id);
if (status != GATT_SUCCESS && status != GATT_CMD_STARTED) {
drop_failed_command();
}
return status;
}
tGATT_STATUS __wrap_GATTC_ExecuteWrite(UINT16 conn_id, BOOLEAN is_execute) {
const tGATT_STATUS status = __real_GATTC_ExecuteWrite(conn_id, is_execute);
if (status != BTA_GATT_OK) {
drop_failed_command();
}
return status;
}
#endif // USE_ESP32_BLE_BLUEDROID_HEADERS
@@ -1,4 +1,5 @@
// Linker wraps for the only two sscanf callers in Bluedroid, so newlib's scanf engine (~16 KB) is not linked.
// Linker wraps for the only two sscanf callers in Bluedroid, so the C library's scanf engine is not linked
// (~16 KB with newlib, ~4 KB with picolibc).
// Same parsing as espressif/esp-idf#19235; drop once ESPHome uses an ESP-IDF release that contains it.
#include "esphome/core/defines.h"
@@ -18,10 +18,12 @@ from esphome.components.esp32 import (
VARIANT_ESP32S31,
add_idf_sdkconfig_option,
get_esp32_variant,
idf_version,
)
from esphome.components.openthread.const import CONF_POLL_PERIOD
from esphome.components.psram import DOMAIN as PSRAM_DOMAIN
from esphome.components.zigbee import DOMAIN as ZIGBEE_DOMAIN
from esphome.config_helpers import filter_source_files_from_defines
import esphome.config_validation as cv
from esphome.const import CONF_ID, CONF_OPENTHREAD
from esphome.core import CORE
@@ -32,6 +34,10 @@ from . import DOMAIN
_LOGGER = logging.getLogger(__name__)
FILTER_SOURCE_FILES = filter_source_files_from_defines(
{"tick_overflow_guard.cpp": "USE_ESP32_PM_TICK_OVERFLOW_GUARD"}
)
CODEOWNERS = ["@rwrozelle"]
DEPENDENCIES = ["esp32"]
@@ -187,6 +193,15 @@ async def to_code(config: ConfigType) -> None:
add_idf_sdkconfig_option(
"CONFIG_FREERTOS_IDLE_TIME_BEFORE_SLEEP", config[CONF_IDLE_TIME_BEFORE_SLEEP]
)
# Stop vTaskStepTick asserting on a tick overshoot after light sleep; built into
# IDF 6.0.1, backported by tick_overflow_guard.cpp before that.
if idf_version() >= cv.Version(6, 0, 1):
add_idf_sdkconfig_option("CONFIG_PM_LIGHTSLEEP_TICK_OVERFLOW_PROTECTION", True)
else:
cg.add_define("USE_ESP32_PM_TICK_OVERFLOW_GUARD")
for symbol in ("vApplicationSleep", "vTaskStepTick"):
cg.add_build_flag(f"-Wl,--wrap={symbol}")
cg.add_build_flag(f"-Wl,--undefined=__wrap_{symbol}")
if config[CONF_POWER_DOWN_PERIPHERALS]:
add_idf_sdkconfig_option("CONFIG_PM_POWER_DOWN_PERIPHERAL_IN_LIGHT_SLEEP", True)
if config[CONF_POWER_DOWN_FLASH]:
@@ -0,0 +1,42 @@
#include "esphome/core/defines.h"
#ifdef USE_ESP32_PM_TICK_OVERFLOW_GUARD
#include <freertos/FreeRTOS.h>
#include <freertos/task.h>
namespace esphome::esp32_power_management {}
// IDF drops tickless idle on chips without light sleep support (ESP32-H4 and -H21 on 5.5), and then
// vApplicationSleep() does not exist
#if CONFIG_FREERTOS_USE_TICKLESS_IDLE
// Backport of IDF 6.0.1 CONFIG_PM_LIGHTSLEEP_TICK_OVERFLOW_PROTECTION: clamp a small tick overshoot
// after light sleep so vTaskStepTick() does not assert.
// NOLINTBEGIN(bugprone-reserved-identifier,cert-dcl37-c,cert-dcl51-cpp,readability-identifier-naming,cppcoreguidelines-avoid-non-const-global-variables)
extern "C" {
void __real_vApplicationSleep(TickType_t expected_idle_time);
void __real_vTaskStepTick(TickType_t ticks_to_jump);
// Same default as IDF's CONFIG_PM_LIGHTSLEEP_TICK_OVERFLOW_TOLERANCE; a larger overshoot still asserts
static constexpr TickType_t TICK_OVERFLOW_TOLERANCE = 2;
static TickType_t expected_idle_time[portNUM_PROCESSORS];
void __wrap_vApplicationSleep(TickType_t expected) {
expected_idle_time[xPortGetCoreID()] = expected;
__real_vApplicationSleep(expected);
}
// Only pm_impl.c calls vTaskStepTick(), from inside vApplicationSleep() on the same core
void __wrap_vTaskStepTick(TickType_t ticks) {
const TickType_t expected = expected_idle_time[xPortGetCoreID()];
if (ticks > expected && ticks <= expected + TICK_OVERFLOW_TOLERANCE)
ticks = expected;
__real_vTaskStepTick(ticks);
}
} // extern "C"
// NOLINTEND(bugprone-reserved-identifier,cert-dcl37-c,cert-dcl51-cpp,readability-identifier-naming,cppcoreguidelines-avoid-non-const-global-variables)
#endif // CONFIG_FREERTOS_USE_TICKLESS_IDLE
#endif // USE_ESP32_PM_TICK_OVERFLOW_GUARD
@@ -102,6 +102,14 @@ ErrorCode ArduinoI2CBus::write_readv(uint8_t address, const uint8_t *write_buffe
uint8_t status = 0;
if (write_count != 0 || read_count == 0) {
#ifdef USE_RP2
// arduino-pico leaves a transmission open when a write overflows its buffer, and every later
// beginTransmission() is then ignored, so refuse the write before starting it
if (write_count > WIRE_BUFFER_SIZE) {
ESP_LOGVV(TAG, "TX of %zu bytes exceeds the %u byte Wire buffer", write_count, WIRE_BUFFER_SIZE);
return ERROR_TOO_LARGE;
}
#endif
this->wire_->beginTransmission(address);
size_t ret = this->wire_->write(write_buffer, write_count);
if (ret != write_count) {
+8
View File
@@ -90,7 +90,15 @@ __attribute__((always_inline)) inline bool is_main_loop_thread() {
return xTaskGetCurrentTaskHandle() == esphome_main_task_handle;
}
__attribute__((always_inline)) inline void yield() { ::yield(); }
#if defined(USE_LIBRETINY_VARIANT_RTL8710B)
// Already sleeps to a deadline (rtl8710b_micros.c)
__attribute__((always_inline)) inline void delay(uint32_t ms) { ::delay(ms); }
#else
// vTaskDelay() counts the partial tick, so one more tick makes the wait at least ms
__attribute__((always_inline)) inline void delay(uint32_t ms) {
vTaskDelay(ms == 0 ? 0 : (ms + portTICK_PERIOD_MS - 1) / portTICK_PERIOD_MS + 1);
}
#endif
__attribute__((always_inline)) inline uint32_t micros() { return static_cast<uint32_t>(::micros()); }
// Per-variant millis() fast path — matches MillisInternal::get().
+18
View File
@@ -7,6 +7,14 @@
#include "esphome/core/log.h"
#include "esphome/core/progmem.h"
#if defined(USE_ESP32) && defined(USE_NETWORK)
#include <sdkconfig.h>
#if defined(CONFIG_LWIP_TCPIP_CORE_LOCKING) && defined(USE_ESPHOME_TASK_LOG_BUFFER)
#include "lwip/sys.h"
#define USE_LOGGER_DEFER_UNDER_LWIP_LOCK
#endif
#endif
namespace esphome::logger {
ESPHOME_LOG_TAG(TAG, "logger");
@@ -42,6 +50,16 @@ void HOT Logger::log_vprintf_(uint8_t level, const char *tag, int line, const ch
// Fast path: main thread, no recursion (99.9% of all logs)
// Pass nullptr for thread_name since we already know this is the main task
if (is_main_task && !this->main_task_recursion_guard_) [[likely]] {
#ifdef USE_LOGGER_DEFER_UNDER_LWIP_LOCK
// lwIP code run under the core lock (tcpip_api_call, netif callbacks) can log. A listener that writes
// to a socket would take that lock again and deadlock, so queue the message for loop() instead. Such lines
// can print after later inline ones, and are lost if loop() never runs again (safe_reboot()).
if (sys_thread_tcpip(LWIP_CORE_LOCK_QUERY_HOLDER) && sys_thread_tcpip(LWIP_CORE_IS_TCPIP_INITIALIZED))
[[unlikely]] {
this->log_vprintf_non_main_thread_(level, tag, line, format, args, nullptr);
return;
}
#endif
this->log_message_to_buffer_and_send_(this->main_task_recursion_guard_, level, tag, line, format, args, nullptr);
return;
}
+4 -4
View File
@@ -806,10 +806,10 @@ void ModbusServerHub::process_modbus_client_frame_(uint8_t address, uint8_t func
bool Modbus::send_frame_(const ModbusFrame &frame) {
int32_t tx_delay_remaining = this->tx_delay_remaining();
if (tx_delay_remaining > 0) {
// Yield the whole-ms part: delay() never blocks past the request on FreeRTOS, and only slightly
// over elsewhere, which just lengthens the gap. The recompute below makes the remainder exact.
if (tx_delay_remaining >= (int32_t) US_PER_MS) {
delay(tx_delay_remaining / US_PER_MS);
// Yield all but the last whole ms: delay() can block up to a tick past the request, and a tick
// longer than 1 ms just lengthens the gap. The recompute below makes the remainder exact.
if (tx_delay_remaining >= (int32_t) (2 * US_PER_MS)) {
delay(tx_delay_remaining / US_PER_MS - 1);
tx_delay_remaining = this->tx_delay_remaining();
}
if (tx_delay_remaining > 0)
+32 -23
View File
@@ -56,7 +56,8 @@ static constexpr uint8_t PIN_MAX_UNCONFIRMED_SENDS = 2;
// The attachment is polled every 4 s; the signal and operator queries only every 4th poll
static constexpr uint32_t ATTACH_POLL_MS = 4000;
static constexpr uint8_t ATTACH_DIAG_EVERY = 4;
static constexpr int RADIO_STATE_RF_OFF = 4; // CFUN=4: transmit and receive off, SIM still powered
static constexpr int RADIO_STATE_MINIMUM = 0; // CFUN=0: radio off and the SIM powered down
static constexpr int RADIO_STATE_RF_OFF = 4; // CFUN=4: transmit and receive off, SIM still powered
static constexpr uint8_t UART_EVENT_QUEUE_SIZE = 10;
static constexpr uint32_t BAUD_PREF_HASH = 76007670UL;
static constexpr uint32_t PIN_PREF_HASH = 76007671UL;
@@ -672,22 +673,28 @@ ModemState ModemComponent::handle_state_enabling_() {
this->set_fault_(ModemFault::MODEM_FAULT_TEARDOWN);
return this->retry_(ModemState::MODEM_STATE_ENABLING, this->next_probe_delay_());
}
this->probe_backoff_.store(0, std::memory_order_relaxed);
// esp_modem keeps the last line before OK as the reply, which can be the echoed command
if (this->dce_->set_echo(false) != command_result::OK) {
ESP_LOGW(TAG, "Failed to disable command echo");
}
int radio_state = 0;
if (this->dce_->get_radio_state(radio_state) != command_result::OK) {
ESP_LOGW(TAG, "Failed to read the radio state; skipping the radio off step");
} else if (radio_state != RADIO_STATE_RF_OFF) {
// Drops a session left by a warm reboot; unlike CFUN=0, which powers the SIM down on the SIM7600, CFUN=4
// keeps the SIM readable for the PIN and the APN. A modem that refuses is used as it is.
if (this->dce_->set_radio_state(RADIO_STATE_RF_OFF) != command_result::OK) {
ESP_LOGW(TAG, "Failed to turn the radio off");
}
// The SIM7670G only uses an APN written at CFUN=0 (else the attach fails with an ESM failure); CFUN=0 also drops a
// session left by a warm reboot. A modem that refuses gets the APN after the SIM check instead, as before.
esp_modem::PdpContext pdp(this->apn_);
this->apn_pending_ = this->dce_->set_radio_state(RADIO_STATE_MINIMUM) != command_result::OK ||
this->dce_->set_pdp_context(pdp) != command_result::OK;
if (this->apn_pending_) {
ESP_LOGW(TAG, "Failed to set the APN with the SIM powered down; setting it after the SIM check");
}
// CFUN=4 powers the SIM up with the radio still off; a modem without it needs CFUN=1 to bring the SIM back
if (this->dce_->set_radio_state(RADIO_STATE_RF_OFF) != command_result::OK &&
this->dce_->set_radio_state(1) != command_result::OK) {
ESP_LOGW(TAG, "Failed to power the SIM up");
this->set_fault_(ModemFault::MODEM_FAULT_SETUP);
this->modem_synced_ = true; // it answers, so a disable() still tears it down
return this->retry_(ModemState::MODEM_STATE_ENABLING, this->next_probe_delay_());
}
this->probe_backoff_.store(0, std::memory_order_relaxed);
if (this->baud_rate_ != 0 && this->baud_rate_ != this->current_baud_rate_) {
ESP_LOGD(TAG, "Setting baud rate: %d -> %d", this->current_baud_rate_, this->baud_rate_);
@@ -754,7 +761,7 @@ ModemState ModemComponent::handle_state_connecting_() {
return ModemState::MODEM_STATE_ENABLING;
}
}
// ENABLING left the radio at CFUN=4, where the SIM answers but the modem cannot attach yet
// ENABLING powered the SIM up (CFUN=4, or CFUN=1 on a modem without it), so the SIM answers
switch (this->prepare_sim_()) {
case SimResult::SIM_RESULT_READY:
break;
@@ -771,13 +778,14 @@ ModemState ModemComponent::handle_state_connecting_() {
this->target_state_.store(ModemState::MODEM_STATE_DISABLED);
return ModemState::MODEM_STATE_DISCONNECTING;
}
// The APN has to be in the PDP context before the radio comes on and the modem attaches; esp_modem only
// sends it when PPP starts
esp_modem::PdpContext pdp(this->apn_);
if (this->dce_->set_pdp_context(pdp) != command_result::OK) {
ESP_LOGW(TAG, "Failed to set the APN");
this->set_fault_(ModemFault::MODEM_FAULT_SETUP);
return this->retry_(ModemState::MODEM_STATE_CONNECTING);
if (this->apn_pending_) {
esp_modem::PdpContext pdp(this->apn_);
if (this->dce_->set_pdp_context(pdp) != command_result::OK) {
ESP_LOGW(TAG, "Failed to set the APN");
this->set_fault_(ModemFault::MODEM_FAULT_SETUP);
return this->retry_(ModemState::MODEM_STATE_CONNECTING);
}
this->apn_pending_ = false;
}
if (this->dce_->set_radio_state(1) != command_result::OK) {
ESP_LOGW(TAG, "Failed to turn the radio on");
@@ -864,11 +872,12 @@ ModemState ModemComponent::handle_state_disconnecting_() {
ESP_LOGW(TAG, "Failed to leave data mode");
this->set_teardown_fault_();
}
// Turn the radio off; waiting for the lost IP event could take up to 2 min. A reconnect keeps the SIM powered
// so it is not unlocked again; only disable() powers it down.
const bool reconnect = this->target_state_.load() == ModemState::MODEM_STATE_CONNECTED;
if (this->dce_->set_radio_state(reconnect ? RADIO_STATE_RF_OFF : 0) == command_result::OK) {
// Turn the radio off; waiting for the lost IP event could take up to 2 min. The SIM is powered down as well, as
// ENABLING does anyway on a reconnect.
if (this->dce_->set_radio_state(RADIO_STATE_MINIMUM) == command_result::OK) {
ESP_LOGI(TAG, "Radio off");
} else if (this->dce_->set_radio_state(RADIO_STATE_RF_OFF) == command_result::OK) {
ESP_LOGW(TAG, "Radio off, but the SIM could not be powered down");
} else {
ESP_LOGW(TAG, "Radio off failed");
this->set_teardown_fault_();
@@ -299,6 +299,7 @@ class ModemComponent : public Component {
bool connect_timeout_armed_{false};
bool modem_synced_{false}; // a modem that never answered has nothing to tear down
bool resync_{false}; // the attachment read failed, so CONNECTING syncs again first
bool apn_pending_{false}; // ENABLING could not set the APN at CFUN=0, so CONNECTING sets it after the SIM check
#ifdef USE_MODEM_SEND_AT
char at_command_[AT_COMMAND_SIZE];
#endif
+2 -2
View File
@@ -2712,7 +2712,7 @@ HAIER_SCHEMA = cv.Schema(
@register_binary_sensor("haier", HaierBinarySensor, HAIER_SCHEMA)
def haier_binary_sensor(var, config):
cg.add(var.set_code(config[CONF_CODE]))
cg.add(var.set_data(cg.StructInitializer(HaierData, ("data", config[CONF_CODE]))))
@register_trigger("haier", HaierTrigger, HaierData)
@@ -2831,7 +2831,7 @@ MIRAGE_SCHEMA = cv.Schema(
@register_binary_sensor("mirage", MirageBinarySensor, MIRAGE_SCHEMA)
def mirage_binary_sensor(var, config):
cg.add(var.set_code(config[CONF_CODE]))
cg.add(var.set_data(cg.StructInitializer(MirageData, ("data", config[CONF_CODE]))))
@register_trigger("mirage", MirageTrigger, MirageData)
@@ -15,7 +15,7 @@ struct CanalSatData {
}
};
struct CanalSatLDData : public CanalSatData {};
using CanalSatLDData = CanalSatData;
class CanalSatBaseProtocol : public RemoteProtocol<CanalSatData> {
public:
@@ -0,0 +1,39 @@
#pragma once
#include <cstdint>
#include "esphome/core/helpers.h"
namespace esphome::uart {
/// One software serial TX byte as line levels, one bit per slot.
struct SoftwareSerialTxFrame {
/// The start bit at slot 0, then the frame, then a stop bit that ends the last run
uint32_t line;
/// Slot of the last 0; from the edge after it the line stays high through the stop bits
uint32_t high_from;
/// Slots right after an edge into a run of equal bits lasting at least `min_run_cycles`, where a pending
/// interrupt only delays bits that repeat the level
uint32_t unlock_at;
};
/// `frame` holds `num_bits` data and parity bits, LSB first. `unlock_at` stays 0 unless `find_unlocks`.
ESPHOME_ALWAYS_INLINE inline SoftwareSerialTxFrame software_serial_tx_frame(uint32_t frame, uint32_t num_bits,
uint32_t bit_time, uint32_t min_run_cycles,
bool find_unlocks) {
SoftwareSerialTxFrame tx;
tx.line = frame << 1 | (1u << (num_bits + 1));
// Slot 0 is the start bit, always 0, so the argument is never 0
tx.high_from = 31 - __builtin_clz(~tx.line & ((2u << num_bits) - 1));
tx.unlock_at = 0;
if (find_unlocks) {
const uint32_t edges = tx.line ^ (tx.line << 1 | 1); // the idle line before the start bit is high
for (uint32_t pos = 0; pos <= tx.high_from; pos++) {
if (((edges >> pos) & 1) && (__builtin_ctz(edges >> (pos + 1)) + 1) * bit_time >= min_run_cycles)
tx.unlock_at |= 1u << pos;
}
}
return tx;
}
} // namespace esphome::uart
@@ -26,6 +26,9 @@ static constexpr uint32_t SW_SERIAL_TX_BIT_LOCK_MAX_BAUD = 38400;
// Edge decoder time base: 1/16 us from the 1 MHz system timer, which keeps its rate during a
// CpuFrequencyBoost; the cycle counter does not.
static constexpr uint32_t RX_TICKS_PER_SECOND = 16000000;
// A byte lock lets pending interrupts run right after an edge when the next edge is at least this far away, so their
// delay lands inside a run of equal bits. Locks longer than about 6 bits at 57600 trip the Wi-Fi watchdog.
static constexpr uint32_t TX_UNLOCK_MIN_RUN_CYCLES = F_CPU / 1000000 * 30;
// Longest per-bit TX interrupt lock; the Wi-Fi interrupt handler asserts (hardware WDT) when held off about 1 ms
static constexpr uint32_t MAX_TX_LOCK_CYCLES = F_CPU / 1000000 * 200;
__attribute__((always_inline)) static inline uint32_t rx_now() { return micros() << 4; }
@@ -395,6 +398,19 @@ void IRAM_ATTR HOT ESP8266SoftwareSerial::write_byte(uint8_t data) {
// Transmit runs from the main loop and never overlaps a CpuFrequencyBoost
const uint32_t bit_time = this->bit_time_;
// Data bits LSB first, then the parity bit
uint32_t frame = data & ((1u << this->data_bits_) - 1);
uint32_t num_bits = this->data_bits_;
if (need_parity_bit) {
parity_bit ^= __builtin_parity(frame);
frame |= uint32_t(parity_bit) << num_bits++;
}
// Decided before the start bit so the timed loop only tests a bit
const SoftwareSerialTxFrame tx = software_serial_tx_frame(frame, num_bits, bit_time, TX_UNLOCK_MIN_RUN_CYCLES,
this->lock_frame_ && blocking_rx_count == 0);
const uint32_t line = tx.line;
const uint32_t high_from = tx.high_from;
const uint32_t unlock_at = tx.unlock_at;
std::optional<InterruptLock> frame_lock;
if (this->locks_whole_frame())
frame_lock.emplace();
@@ -405,19 +421,13 @@ void IRAM_ATTR HOT ESP8266SoftwareSerial::write_byte(uint8_t data) {
this->tx_pin_.digital_write(false);
start = arch_get_cpu_cycle_count();
}
// Data bits LSB first, then the parity bit
uint32_t frame = data & ((1u << this->data_bits_) - 1);
uint32_t num_bits = this->data_bits_;
if (need_parity_bit) {
parity_bit ^= __builtin_parity(frame);
frame |= uint32_t(parity_bit) << num_bits++;
}
// From the edge after the last 0 the line stays high through the stop bits
const uint32_t zeros = ~frame & ((1u << num_bits) - 1);
const uint32_t high_from = zeros == 0 ? 0 : 32 - __builtin_clz(zeros);
uint32_t wait = bit_time;
for (uint32_t i = 0; i < high_from; i++)
this->write_bit_((frame >> i) & 1, &wait, start, bit_time);
for (uint32_t pos = 0; pos <= high_from; pos++) {
if (pos != 0)
this->write_bit_((line >> pos) & 1, &wait, start, bit_time);
if ((unlock_at >> pos) & 1)
frame_lock.emplace(); // drops the held lock first, so pending interrupts run
}
this->write_bit_(true, &wait, start, bit_time);
// Hold that high run with interrupts on: one that runs past it only delays the next start bit, and back-to-back
// byte locks with no gap between them trip the Wi-Fi watchdog
@@ -8,6 +8,7 @@
#include "esphome/core/hal.h"
#include "esphome/core/log.h"
#include "software_serial_rx_decoder.h"
#include "software_serial_tx_frame.h"
#include "uart_component.h"
namespace esphome::uart {
+31 -21
View File
@@ -5,47 +5,57 @@
namespace esphome::wifi {
// Switching APs calls esp_wifi_disconnect(), which queues an association-leave event from the old AP. Remembers
// that AP so its leave event does not end the connection attempt started after it. Platform free for host tests.
/// Which expected leave a disconnect matched.
enum class ExpectedLeave : uint8_t {
EXPECTED_LEAVE_NONE,
EXPECTED_LEAVE_AP,
EXPECTED_LEAVE_ATTEMPT, ///< An attempt that never associated, so no BSSID
};
// Switching APs calls esp_wifi_disconnect(), which queues a leave event for the old AP, or with no BSSID for an
// attempt that never associated. Remembers it so that event does not end the next attempt. Platform free for tests.
class ExpectedDisconnect {
public:
void arm(const uint8_t *bssid) {
std::copy_n(bssid, this->bssid_.size(), this->bssid_.begin());
this->armed_ = true;
}
void clear() { this->armed_ = false; }
void arm(const uint8_t *bssid) { this->arm_(bssid, ExpectedLeave::EXPECTED_LEAVE_AP); }
void clear() { this->expected_ = ExpectedLeave::EXPECTED_LEAVE_NONE; }
/// Disconnects from the current AP and, once that succeeds, expects its leave event. read_bssid(uint8_t *out)
/// returns whether there was an AP and runs first, since the record is gone after the disconnect; disconnect()
/// returns whether it succeeded, which is also the result. Any earlier expectation is dropped.
/// Disconnects and, once that succeeds, expects the leave event. read_bssid(uint8_t *out) runs first, since the
/// record is gone after the disconnect, and writes the AP's BSSID only when it returns true; without an AP the
/// attempt's leave, with no BSSID, is expected. disconnect() returns whether it succeeded, which is the result.
template<typename ReadBssid, typename Disconnect>
bool disconnect_and_expect_leave(ReadBssid &&read_bssid, Disconnect &&disconnect) {
this->armed_ = false;
this->clear();
std::array<uint8_t, 6> bssid{};
const bool had_ap = read_bssid(bssid.data());
if (!disconnect())
return false;
if (had_ap)
this->arm(bssid.data());
this->arm_(bssid.data(), had_ap ? ExpectedLeave::EXPECTED_LEAVE_AP : ExpectedLeave::EXPECTED_LEAVE_ATTEMPT);
return true;
}
/// A connected event still queued from the previous AP; it keeps the expectation.
bool is_previous_ap(const uint8_t *bssid) const {
return this->armed_ && std::equal(this->bssid_.begin(), this->bssid_.end(), bssid);
return this->expected_ != ExpectedLeave::EXPECTED_LEAVE_NONE &&
std::equal(this->bssid_.begin(), this->bssid_.end(), bssid);
}
/// Whether a disconnect is the previous AP's leave. Every disconnect ends the expectation, so it matches at
/// most once; the caller handles roaming disconnects before this.
bool consume_disconnect(const uint8_t *bssid, bool assoc_leave) {
const bool expected = assoc_leave && this->is_previous_ap(bssid);
this->armed_ = false;
return expected;
/// Which expected leave a disconnect is. \p is_leave covers an AP's association leave (8) and the station leave
/// of an attempt that never associated (36). Every disconnect ends the expectation, so it matches at most once.
ExpectedLeave consume_disconnect(const uint8_t *bssid, bool is_leave) {
const ExpectedLeave matched =
is_leave && this->is_previous_ap(bssid) ? this->expected_ : ExpectedLeave::EXPECTED_LEAVE_NONE;
this->clear();
return matched;
}
protected:
void arm_(const uint8_t *bssid, ExpectedLeave expected) {
std::copy_n(bssid, this->bssid_.size(), this->bssid_.begin());
this->expected_ = expected;
}
std::array<uint8_t, 6> bssid_{};
bool armed_{false};
ExpectedLeave expected_{ExpectedLeave::EXPECTED_LEAVE_NONE};
};
} // namespace esphome::wifi
@@ -2174,6 +2174,10 @@ bool WiFiComponent::transition_to_phase_(WiFiRetryPhase new_phase) {
// Even when skipping full restart, disconnect to clear driver state
// Without this, platforms like LibreTiny may think we're still connecting
this->wifi_disconnect_();
// And clear the error flag, as restart_adapter() does: COOLDOWN leads to
// check_connecting_finished() without start_connecting(), so a stale
// error would fail a connection the driver completes meanwhile.
this->error_from_callback_ = false;
}
// Clear scan flag - we're starting a new retry cycle
// This is critical for DPP/captive portal/improv flow: when determine_next_phase_()
@@ -1158,10 +1158,12 @@ void WiFiComponent::wifi_process_event_(IDFWiFiEvent *data) {
ESP_LOGI(TAG, "Disconnected ssid='%.*s' reason='Station Roaming'", it.ssid_len, (const char *) it.ssid);
return;
}
const bool expected_disconnect =
s_expected_disconnect.consume_disconnect(it.bssid, it.reason == WIFI_REASON_ASSOC_LEAVE);
const ExpectedLeave expected = s_expected_disconnect.consume_disconnect(
it.bssid, it.reason == WIFI_REASON_ASSOC_LEAVE || it.reason == WIFI_REASON_STA_LEAVING);
const bool expected_disconnect = expected != ExpectedLeave::EXPECTED_LEAVE_NONE;
if (expected_disconnect) {
ESP_LOGD(TAG, "Expected disconnect from previous AP");
ESP_LOGD(TAG, "Expected leave of the previous %s",
expected == ExpectedLeave::EXPECTED_LEAVE_AP ? LOG_STR_LITERAL("AP") : LOG_STR_LITERAL("attempt"));
} else if (it.reason == WIFI_REASON_NO_AP_FOUND) {
ESP_LOGW(TAG, "Disconnected ssid='%.*s' reason='Probe Request Unsuccessful'", it.ssid_len,
(const char *) it.ssid);
@@ -1181,7 +1183,9 @@ void WiFiComponent::wifi_process_event_(IDFWiFiEvent *data) {
// Refresh is_connected() cache; the old session is no longer connected.
this->update_connected_state_();
#ifdef USE_WIFI_CONNECT_STATE_LISTENERS
this->notify_disconnect_state_listeners_();
// An attempt that never associated ended no session listeners know of
if (expected != ExpectedLeave::EXPECTED_LEAVE_ATTEMPT)
this->notify_disconnect_state_listeners_();
#endif
#if defined(USE_WIFI_AP_WISP) && !defined(USE_WIFI_AP_WISP_STATIC_DNS)
+6 -1
View File
@@ -20,7 +20,12 @@ void Mutex::lock() { k_mutex_lock(static_cast<k_mutex *>(this->handle_), K_FOREV
bool Mutex::try_lock() { return k_mutex_lock(static_cast<k_mutex *>(this->handle_), K_NO_WAIT) == 0; }
void Mutex::unlock() { k_mutex_unlock(static_cast<k_mutex *>(this->handle_)); }
IRAM_ATTR InterruptLock::InterruptLock() { state_ = irq_lock(); }
IRAM_ATTR InterruptLock::InterruptLock() {
#ifdef USE_ZEPHYR_DWT_MICROS
micros(); // Unlocked, so it catches up any sleep before timestamps are taken under the lock
#endif
state_ = irq_lock();
}
IRAM_ATTR InterruptLock::~InterruptLock() { irq_unlock(state_); }
// Zephyr LwIPLock is defined inline as a no-op in helpers.h
+58 -1
View File
@@ -6,6 +6,10 @@
#include <zephyr/drivers/watchdog.h>
#include <zephyr/sys/reboot.h>
#ifdef USE_ZEPHYR_DWT_MICROS
#include <cmsis_core.h>
#endif
// Empty zephyr namespace block to satisfy ci-custom's lint_namespace check.
// HAL functions live in namespace esphome (root) — they are not part of the
// zephyr component's API.
@@ -18,12 +22,65 @@ static int wdt_channel_id = -1; // NOLINT(cppcoreguidelines-avoid-non-const-glo
static const device *const WDT = DEVICE_DT_GET(DT_ALIAS(watchdog0));
#endif
// yield(), delay(), micros(), millis(), millis_64(), delayMicroseconds(),
// yield(), delay(), millis(), millis_64(), delayMicroseconds(), micros() except the cycle-counter one,
// arch_get_cpu_cycle_count(), arch_get_cpu_freq_hz() inlined in
// components/zephyr/hal.h.
#ifdef USE_ZEPHYR_DWT_MICROS
// The kernel clock counts in ticks of about 30 us, and the CPU cycle counter stops while the CPU sleeps.
// micros() adds the cycles counted since the last call. While the CPU stays busy the result and the kernel clock
// each trail the true time by less than a tick, so they differ by less than a tick either way. A sleep shorter
// than a tick can go unnoticed, so the result may trail the true time by up to two ticks until the next resync.
// The CPU cannot sleep while interrupts are locked, so a lag there comes from before the lock. It and any drift
// are left for the next unlocked call, which keeps differences inside an InterruptLock exact; InterruptLock makes that
// call just before it locks, so timestamps taken under the lock line up with later unlocked ones. A counter that has
// not moved since the last call is not running, and then the kernel clock is used even under a lock.
// One struct so the code loads a single base address.
static struct {
uint32_t value;
uint32_t last_cycles; // Moves on by whole microseconds only, so the remainder carries over.
} micros_state; // NOLINT(cppcoreguidelines-avoid-non-const-global-variables)
static constexpr uint32_t CYCLES_PER_US = 64; // nRF52 cores always run at 64 MHz.
// Drift between unlocked calls stays far below this; a larger lead is a lag of over 2^31 us that wrapped. A lag
// within this of 2^32 us still looks like a lead and holds micros() for at most a second; that is accepted.
static constexpr int32_t MAX_LEAD_US = 1000000;
uint32_t micros() {
const unsigned int key = irq_lock();
const uint32_t cycles = DWT->CYCCNT;
const uint32_t elapsed_us = (cycles - micros_state.last_cycles) / CYCLES_PER_US;
micros_state.last_cycles += elapsed_us * CYCLES_PER_US;
uint32_t value = micros_state.value + elapsed_us;
const uint32_t kernel_us = k_ticks_to_us_floor32(k_uptime_ticks());
// A tick, plus 1 us because kernel_us is rounded down.
const int32_t tick_us = k_ticks_to_us_ceil32(1) + 1;
const int32_t ahead = static_cast<int32_t>(value - kernel_us);
const bool unlocked = arch_irq_unlocked(key);
if ((ahead < -tick_us || ahead > MAX_LEAD_US) && (unlocked || elapsed_us == 0)) {
// The CPU slept, so the cycle counter fell behind.
value = kernel_us;
micros_state.last_cycles = cycles;
} else if (ahead >= tick_us && ahead <= MAX_LEAD_US && unlocked) {
// The CPU clock can run fast against the RTC; cap the drift at one tick, but never below a result already
// returned under a lock.
const uint32_t cap = kernel_us + tick_us - 1;
value = static_cast<int32_t>(cap - micros_state.value) > 0 ? cap : micros_state.value;
}
micros_state.value = value;
irq_unlock(key);
return value;
}
#endif
void arch_init() {
main_loop_thread() = k_current_get();
#ifdef USE_ZEPHYR_DWT_MICROS
CoreDebug->DEMCR |= CoreDebug_DEMCR_TRCENA_Msk;
DWT->CTRL |= DWT_CTRL_CYCCNTENA_Msk;
#endif
#ifdef CONFIG_WATCHDOG
if (device_is_ready(WDT)) {
static wdt_timeout_cfg wdt_config{};
+9 -1
View File
@@ -27,12 +27,20 @@ __attribute__((always_inline)) inline bool is_main_loop_thread() { return k_curr
__attribute__((always_inline)) inline void yield() { ::k_yield(); }
__attribute__((always_inline)) inline void delay(uint32_t ms) { ::k_msleep(ms); }
#if CONFIG_SYS_CLOCK_HW_CYCLES_PER_SEC == 1000000
// The system timer counts microseconds (the GRTC on nRF54L) and wraps at 2^32 like micros().
__attribute__((always_inline)) inline uint32_t micros() { return k_cycle_get_32(); }
#elif defined(CONFIG_SOC_SERIES_NRF52X) || defined(CONFIG_SOC_SERIES_NRF52) // the name changed in sdk-nrf 3
#define USE_ZEPHYR_DWT_MICROS
uint32_t micros(); // CPU cycles kept close to the kernel clock, see hal.cpp
#else
__attribute__((always_inline)) inline uint32_t micros() { return k_ticks_to_us_floor32(k_uptime_ticks()); }
#endif
__attribute__((always_inline)) inline uint64_t millis_64() { return static_cast<uint64_t>(k_uptime_get()); }
__attribute__((always_inline)) inline uint32_t millis() { return static_cast<uint32_t>(millis_64()); }
// NOLINTNEXTLINE(readability-identifier-naming)
__attribute__((always_inline)) inline void delayMicroseconds(uint32_t us) { ::k_usleep(us); }
__attribute__((always_inline)) inline void delayMicroseconds(uint32_t us) { ::k_busy_wait(us); }
__attribute__((always_inline)) inline uint32_t arch_get_cpu_cycle_count() { return k_cycle_get_32(); }
__attribute__((always_inline)) inline uint32_t arch_get_cpu_freq_hz() { return sys_clock_hw_cycles_per_sec(); }
+1 -1
View File
@@ -4,7 +4,7 @@ from enum import Enum
from esphome.enum import StrEnum
__version__ = "2026.11.0b5"
__version__ = "2026.11.0b6"
ALLOWED_NAME_CHARS = "abcdefghijklmnopqrstuvwxyz0123456789-_"
VALID_SUBSTITUTIONS_CHARACTERS = (
+2
View File
@@ -481,6 +481,7 @@
#ifdef USE_ESP32
#define USE_ESP32_CRASH_HANDLER
#define USE_ESP32_VASPRINTF_STUB
#define USE_ESP32_PM_TICK_OVERFLOW_GUARD
#define USE_MQTT_IDF_ENQUEUE
#define USE_MQTT_TLS
#define USE_MQTT_CERT_BUNDLE
@@ -508,6 +509,7 @@
#define USE_WIFI_SCAN_RESULTS_LOCK
#define USE_ESP32_BLE
#define USE_ESP32_BLE_BLUEDROID_BOND_KEYS
#define USE_ESP32_BLE_BLUEDROID_GATTC_QUEUE
#define USE_ESP32_BLE_BLUEDROID_HEX_WRAPS
#define USE_ESP32_BLE_GATTC_HANDLE_ORDER
#define USE_ESP32_BLE_MAX_CONNECTIONS 3
+5
View File
@@ -2282,6 +2282,11 @@ class LwIPLock {
// Platforms with potential lwIP core locking — out-of-line implementations in helpers.cpp
LwIPLock();
~LwIPLock();
#ifdef USE_ESP32
private:
// Only the instance that took the core lock releases it; unused without CONFIG_LWIP_TCPIP_CORE_LOCKING
[[maybe_unused]] bool locked_{false};
#endif
#else
// No lwIP core locking — inline no-ops (empty bodies instead of = default
// to prevent clang-tidy unused-variable warnings at call sites)
@@ -7,8 +7,9 @@ from pathlib import Path
import pytest
from esphome import config_validation as cv
from esphome.components.esp32.const import KEY_ESP32, KEY_SRC_CMAKE
from esphome.components.esp32_ble import bluedroid_headers_cmake
from esphome.components.esp32_ble import _drops_bond_keys, bluedroid_headers_cmake
from esphome.core import CORE
_WRAP_FLAGS = {
@@ -19,12 +20,31 @@ _WRAP_FLAGS = {
}
@pytest.mark.parametrize(
("version", "expected"),
[
("5.5.5", False),
("5.5.6", True),
("5.5.7", True),
("6.0.0", False),
("6.0.3", False),
("6.1.0", False),
("6.1.1", True),
("6.2.0", True),
],
)
def test_drops_bond_keys_versions(version: str, expected: bool) -> None:
"""ESP-IDF added the bond clearing in 5.5.6 and 6.1.1."""
assert _drops_bond_keys(cv.Version.parse(version)) is expected
@pytest.mark.parametrize(
("framework", "windows", "expected"),
[
pytest.param("{type: esp-idf, version: 5.5.6}", False, True, id="idf_5_5_6"),
pytest.param("{type: esp-idf, version: 5.5.5}", False, False, id="idf_5_5_5"),
pytest.param("{type: esp-idf, version: 6.0.1}", False, False, id="idf_6"),
pytest.param("{type: esp-idf, version: 6.1.1}", False, True, id="idf_6_1_1"),
pytest.param("{type: esp-idf, version: 5.5.6}", True, False, id="windows"),
# Arduino 3.3.12 is built on ESP-IDF 5.5.6, 3.3.11 on 5.5.5
pytest.param(
@@ -48,7 +68,7 @@ _WRAP_FLAGS = {
),
],
)
def test_wraps_only_on_idf_5_5_6_native_builds(
def test_wraps_only_on_affected_idf_native_builds(
generate_main: Callable[[str | Path], str],
tmp_path: Path,
monkeypatch: pytest.MonkeyPatch,
@@ -39,11 +39,14 @@ _CLIENTS = {
("6.0.0", False),
("6.0.2", False),
("6.0.3", True),
("6.1.0", True),
("6.0.4", True),
("6.1.0", False),
("6.1.1", True),
("6.2.0", True),
],
)
def test_strict_handle_order_versions(version: str, expected: bool) -> None:
"""ESP-IDF added the check in 5.5.6 and 6.0.3."""
"""ESP-IDF added the check in 5.5.6, 6.0.3 and 6.1.1."""
assert _has_strict_handle_order(cv.Version.parse(version)) is expected
@@ -0,0 +1,75 @@
"""Tests for the Bluedroid wraps that drop a failed queued GATT client command."""
from __future__ import annotations
from collections.abc import Callable
from pathlib import Path
import pytest
from esphome.components.esp32.const import KEY_ESP32, KEY_SRC_CMAKE
from esphome.components.esp32_ble import bluedroid_headers_cmake
from esphome.core import CORE
_WRAPPED = (
"bta_gattc_cfg_mtu",
"bta_gattc_execute",
"GATTC_ConfigureMTU",
"GATTC_ExecuteWrite",
)
_WRAP_FLAGS = {f"-Wl,--wrap={s}" for s in _WRAPPED} | {
f"-Wl,--undefined=__wrap_{s}" for s in _WRAPPED
}
_TRACKER = "\nesp32_ble_tracker: {}\n"
@pytest.mark.parametrize(
("framework", "components", "windows", "expected"),
[
pytest.param(
"{type: esp-idf, version: 5.5.6}", _TRACKER, False, True, id="idf_5_5_6"
),
pytest.param(
"{type: esp-idf, version: 6.0.1}", _TRACKER, False, True, id="idf_6"
),
pytest.param(
"{type: arduino, version: 3.3.12}", _TRACKER, False, True, id="arduino"
),
# Without the tracker or esp32_ble_client there is no GATT client
pytest.param(
"{type: esp-idf, version: 5.5.6}", "", False, False, id="no_gatt_client"
),
pytest.param(
"{type: esp-idf, version: 5.5.6}", _TRACKER, True, False, id="windows"
),
# PlatformIO builds cannot reach the private headers
pytest.param(
"{type: esp-idf, version: 5.5.6}\n toolchain: platformio",
_TRACKER,
False,
False,
id="platformio",
),
],
)
def test_wraps_on_native_builds_with_a_gatt_client(
generate_main: Callable[[str | Path], str],
tmp_path: Path,
monkeypatch: pytest.MonkeyPatch,
framework: str,
components: str,
windows: bool,
expected: bool,
) -> None:
monkeypatch.setattr("esphome.components.esp32_ble.IS_WINDOWS", windows)
config = tmp_path / "test.yaml"
config.write_text(
"esphome:\n name: test\n\nesp32:\n variant: esp32\n"
f" framework: {framework}\n\nesp32_ble: {{}}\n{components}"
)
generate_main(config)
assert CORE.build_flags & _WRAP_FLAGS == (_WRAP_FLAGS if expected else set())
defines = {define.name for define in CORE.defines}
assert ("USE_ESP32_BLE_BLUEDROID_GATTC_QUEUE" in defines) is expected
headers = bluedroid_headers_cmake("bluedroid_gattc_queue.c")
assert (headers in CORE.data[KEY_ESP32][KEY_SRC_CMAKE]) is expected
@@ -28,19 +28,22 @@ _WRAP_FLAGS = {
pytest.param("esp32c2", "{type: esp-idf}", True, id="c2"),
pytest.param("esp32c6", "{type: esp-idf}", False, id="c6_rom_sscanf"),
pytest.param(
"esp32", "{type: esp-idf, version: 6.0.1}", False, id="idf6_picolibc"
"esp32", "{type: esp-idf, version: 6.0.1}", True, id="idf6_picolibc"
),
pytest.param(
"esp32c6", "{type: esp-idf, version: 6.0.1}", True, id="c6_idf6_picolibc"
),
pytest.param("esp32", "{type: arduino}", False, id="arduino"),
],
)
def test_wraps_only_on_newlib_idf_without_rom_sscanf(
def test_wraps_only_on_idf_without_rom_sscanf(
generate_main: Callable[[str | Path], str],
tmp_path: Path,
variant: str,
framework: str,
expected: bool,
) -> None:
"""The wraps are emitted only where Bluedroid's sscanf calls link newlib's engine."""
"""The wraps are emitted only where Bluedroid's sscanf calls link a scanf engine."""
config = tmp_path / "test.yaml"
config.write_text(
f"esphome:\n name: test\n\nesp32:\n variant: {variant}\n"
@@ -9,6 +9,7 @@ import pytest
from esphome import config_validation as cv
from esphome.components.const import CONF_DEVICE_TYPE
from esphome.components.esp32 import (
KEY_IDF_VERSION,
KEY_VARIANT,
VARIANT_ESP32,
VARIANT_ESP32C6,
@@ -33,6 +34,7 @@ from esphome.types import ConfigType
from tests.component_tests.types import SetCoreConfigCallable
LIGHT_SLEEP = {CONF_ENABLE_LIGHT_SLEEP: True}
IDF_VERSION = cv.Version(5, 5, 5)
def _set_variant(
@@ -41,11 +43,12 @@ def _set_variant(
/,
*,
full_config: ConfigType | None = None,
idf_version: cv.Version = IDF_VERSION,
) -> None:
set_core_config(
PlatformFramework.ESP32_IDF,
core_data={KEY_FRAMEWORK_VERSION: cv.Version(5, 5, 5)},
platform_data={KEY_VARIANT: variant},
core_data={KEY_FRAMEWORK_VERSION: idf_version},
platform_data={KEY_VARIANT: variant, KEY_IDF_VERSION: idf_version},
full_config=full_config,
)
@@ -360,3 +363,32 @@ async def test_to_code_sdkconfig(
CORE.config = core_config
await to_code(CONFIG_SCHEMA({CONF_ENABLE_LIGHT_SLEEP: light_sleep}))
assert sdkconfig_calls.get(key, NOT_SET) is expected
@pytest.mark.asyncio
@pytest.mark.parametrize(
("idf_version", "light_sleep", "kconfig", "guard"),
[
pytest.param(cv.Version(5, 5, 6), True, NOT_SET, True, id="idf55_wrap"),
pytest.param(cv.Version(6, 0, 0), True, NOT_SET, True, id="idf600_wrap"),
pytest.param(cv.Version(6, 0, 1), True, True, False, id="idf601_kconfig"),
pytest.param(cv.Version(5, 5, 6), False, NOT_SET, False, id="light_sleep_off"),
],
)
async def test_tick_overflow_guard(
set_core_config: SetCoreConfigCallable,
sdkconfig_calls: dict[str, object],
idf_version: cv.Version,
light_sleep: bool,
kconfig: object,
guard: bool,
) -> None:
"""IDF 6.0.1 has the overflow protection built in; older releases get the --wrap backport."""
_set_variant(set_core_config, VARIANT_ESP32H2, idf_version=idf_version)
CORE.config = {}
await to_code(CONFIG_SCHEMA({CONF_ENABLE_LIGHT_SLEEP: light_sleep}))
key = "CONFIG_PM_LIGHTSLEEP_TICK_OVERFLOW_PROTECTION"
assert sdkconfig_calls.get(key, NOT_SET) is kconfig
defines = {define.name for define in CORE.defines}
assert ("USE_ESP32_PM_TICK_OVERFLOW_GUARD" in defines) is guard
assert ("-Wl,--wrap=vTaskStepTick" in CORE.build_flags) is guard
@@ -28,11 +28,11 @@ on_canalsat:
- logger.log:
format: "on_canalsat: %u %u"
args: ["x.address", "x.command"]
# on_canalsatld:
# then:
# - logger.log:
# format: "on_canalsatld: %u %u"
# args: ["x.address", "x.command"]
on_canalsatld:
then:
- logger.log:
format: "on_canalsatld: %u %u"
args: ["x.address", "x.command"]
on_coolix:
then:
- logger.log:
@@ -10,6 +10,19 @@ remote_receiver:
<<: !include common-actions.yaml
binary_sensor:
- platform: remote_receiver
name: CanalSatLD Remote Input
canalsatld:
device: 0x7F
command: 0x12
- platform: remote_receiver
name: Haier Remote Input
haier:
code: [0xA6, 0x12, 0x00, 0x00, 0x40, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x05]
- platform: remote_receiver
name: Mirage Remote Input
mirage:
code: [0x56, 0x75, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x12]
- platform: remote_receiver
name: Panasonic Remote Input
panasonic:
@@ -5,6 +5,14 @@ remote_receiver:
<<: !include common-actions.yaml
binary_sensor:
- platform: remote_receiver
name: Haier Remote Input
haier:
code: [0xA6, 0x12, 0x00, 0x00, 0x40, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x05]
- platform: remote_receiver
name: Mirage Remote Input
mirage:
code: [0x56, 0x75, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x12]
- platform: remote_receiver
name: Panasonic Remote Input
panasonic:
@@ -0,0 +1,81 @@
#include <gtest/gtest.h>
#include <cstdint>
#include "esphome/components/uart/software_serial_tx_frame.h"
namespace esphome::uart::testing {
namespace {
constexpr uint32_t US_CYCLES_80MHZ = 80;
constexpr uint32_t MIN_RUN_80MHZ = US_CYCLES_80MHZ * 30;
// Line level of a slot by reading the bits one at a time: start 0, frame LSB first, then high
bool level_at(uint32_t frame, uint32_t num_bits, uint32_t slot) {
if (slot == 0)
return false;
if (slot <= num_bits)
return (frame >> (slot - 1)) & 1;
return true;
}
} // namespace
// Every set slot opens the lock right after an edge into a run that lasts long enough, every such slot up to
// high_from is set, and high_from is the slot of the last 0
TEST(SoftwareSerialTxFrameTest, UnlockSlotsMatchBitByBitReference) {
for (uint32_t cpu_mhz : {80u, 160u}) {
const uint32_t min_run = cpu_mhz * 30;
for (uint32_t baud : {57600u, 76800u, 115200u, 230400u}) {
const uint32_t bit_time = cpu_mhz * 1000000 / baud;
for (uint32_t num_bits = 5; num_bits <= 9; num_bits++) {
for (uint32_t frame = 0; frame < (1u << num_bits); frame++) {
const SoftwareSerialTxFrame tx = software_serial_tx_frame(frame, num_bits, bit_time, min_run, true);
uint32_t last_zero = 0;
for (uint32_t slot = 1; slot <= num_bits; slot++) {
if (!level_at(frame, num_bits, slot))
last_zero = slot;
}
ASSERT_EQ(tx.high_from, last_zero) << "frame " << frame << " bits " << num_bits;
for (uint32_t slot = 0; slot <= num_bits + 1; slot++)
ASSERT_EQ(((tx.line >> slot) & 1) != 0, level_at(frame, num_bits, slot));
for (uint32_t slot = 0; slot <= tx.high_from; slot++) {
const bool level = level_at(frame, num_bits, slot);
const bool edge = slot == 0 || level != level_at(frame, num_bits, slot - 1);
uint32_t run = 1;
while (level_at(frame, num_bits, slot + run) == level)
run++;
const bool expected = edge && run * bit_time >= min_run;
ASSERT_EQ(((tx.unlock_at >> slot) & 1) != 0, expected)
<< "frame " << frame << " bits " << num_bits << " baud " << baud << " slot " << slot;
}
ASSERT_EQ(tx.unlock_at >> (tx.high_from + 1), 0u);
}
}
}
}
}
TEST(SoftwareSerialTxFrameTest, NoUnlocksWhenNotRequested) {
const SoftwareSerialTxFrame tx =
software_serial_tx_frame(0x00, 8, US_CYCLES_80MHZ * 1000000 / 57600, MIN_RUN_80MHZ, false);
EXPECT_EQ(tx.unlock_at, 0u);
EXPECT_EQ(tx.high_from, 8u);
}
// At 57600 a run of two bits is long enough; 0x55 alternates every bit, so its lock never opens
TEST(SoftwareSerialTxFrameTest, Examples57600) {
const uint32_t bit_time = US_CYCLES_80MHZ * 1000000 / 57600;
EXPECT_EQ(software_serial_tx_frame(0x55, 8, bit_time, MIN_RUN_80MHZ, true).unlock_at, 0u);
// 0x00: the start bit begins a run of nine 0s
EXPECT_EQ(software_serial_tx_frame(0x00, 8, bit_time, MIN_RUN_80MHZ, true).unlock_at, 0b1u);
// 0xFF: only the start bit is sent before the trailing high run, and it is a run of one
const SoftwareSerialTxFrame ff = software_serial_tx_frame(0xFF, 8, bit_time, MIN_RUN_80MHZ, true);
EXPECT_EQ(ff.high_from, 0u);
EXPECT_EQ(ff.unlock_at, 0u);
// 'A' (0x41): slots 0 1 0 0 0 0 0 1 0, so a run of five 0s starts at slot 2
EXPECT_EQ(software_serial_tx_frame(0x41, 8, bit_time, MIN_RUN_80MHZ, true).unlock_at, 1u << 2);
}
} // namespace esphome::uart::testing
@@ -10,6 +10,7 @@ namespace {
constexpr uint8_t OLD_AP[6] = {0x02, 0x00, 0x00, 0x00, 0x00, 0x01};
constexpr uint8_t NEW_AP[6] = {0x02, 0x00, 0x00, 0x00, 0x00, 0x02};
constexpr uint8_t NO_AP[6] = {};
ExpectedDisconnect armed() {
ExpectedDisconnect expected;
@@ -20,20 +21,20 @@ ExpectedDisconnect armed() {
TEST(ExpectedDisconnectTest, PreviousApLeaveIsExpectedOnce) {
auto expected = armed();
EXPECT_TRUE(expected.consume_disconnect(OLD_AP, true));
EXPECT_EQ(expected.consume_disconnect(OLD_AP, true), ExpectedLeave::EXPECTED_LEAVE_AP);
EXPECT_FALSE(expected.is_previous_ap(OLD_AP));
// A second leave from the same AP is a real disconnect
EXPECT_FALSE(expected.consume_disconnect(OLD_AP, true));
EXPECT_EQ(expected.consume_disconnect(OLD_AP, true), ExpectedLeave::EXPECTED_LEAVE_NONE);
}
TEST(ExpectedDisconnectTest, OtherDisconnectsAreRealAndEndTheExpectation) {
auto other_ap = armed();
EXPECT_FALSE(other_ap.consume_disconnect(NEW_AP, true));
EXPECT_FALSE(other_ap.consume_disconnect(OLD_AP, true));
EXPECT_EQ(other_ap.consume_disconnect(NEW_AP, true), ExpectedLeave::EXPECTED_LEAVE_NONE);
EXPECT_EQ(other_ap.consume_disconnect(OLD_AP, true), ExpectedLeave::EXPECTED_LEAVE_NONE);
auto other_reason = armed();
EXPECT_FALSE(other_reason.consume_disconnect(OLD_AP, false));
EXPECT_FALSE(other_reason.consume_disconnect(OLD_AP, true));
EXPECT_EQ(other_reason.consume_disconnect(OLD_AP, false), ExpectedLeave::EXPECTED_LEAVE_NONE);
EXPECT_EQ(other_reason.consume_disconnect(OLD_AP, true), ExpectedLeave::EXPECTED_LEAVE_NONE);
}
TEST(ExpectedDisconnectTest, QueuedConnectionFromPreviousApKeepsTheExpectation) {
@@ -41,17 +42,17 @@ TEST(ExpectedDisconnectTest, QueuedConnectionFromPreviousApKeepsTheExpectation)
EXPECT_TRUE(expected.is_previous_ap(OLD_AP));
EXPECT_FALSE(expected.is_previous_ap(NEW_AP));
EXPECT_TRUE(expected.consume_disconnect(OLD_AP, true));
EXPECT_EQ(expected.consume_disconnect(OLD_AP, true), ExpectedLeave::EXPECTED_LEAVE_AP);
}
TEST(ExpectedDisconnectTest, NothingIsExpectedUnlessArmed) {
ExpectedDisconnect expected;
EXPECT_FALSE(expected.is_previous_ap(OLD_AP));
EXPECT_FALSE(expected.consume_disconnect(OLD_AP, true));
EXPECT_EQ(expected.consume_disconnect(OLD_AP, true), ExpectedLeave::EXPECTED_LEAVE_NONE);
auto cleared = armed();
cleared.clear();
EXPECT_FALSE(cleared.consume_disconnect(OLD_AP, true));
EXPECT_EQ(cleared.consume_disconnect(OLD_AP, true), ExpectedLeave::EXPECTED_LEAVE_NONE);
}
// Stands in for esp_wifi_sta_get_ap_info() and esp_wifi_disconnect(): the AP record is gone after disconnecting
@@ -82,16 +83,7 @@ TEST(ExpectedDisconnectTest, DisconnectExpectsTheLeaveOfTheApItLeft) {
Driver driver;
EXPECT_TRUE(disconnect(expected, driver));
EXPECT_TRUE(expected.consume_disconnect(OLD_AP, true));
}
TEST(ExpectedDisconnectTest, NoApMeansNoExpectation) {
ExpectedDisconnect expected;
Driver driver;
driver.has_ap = false;
EXPECT_TRUE(disconnect(expected, driver));
EXPECT_FALSE(expected.consume_disconnect(OLD_AP, true));
EXPECT_EQ(expected.consume_disconnect(OLD_AP, true), ExpectedLeave::EXPECTED_LEAVE_AP);
}
TEST(ExpectedDisconnectTest, FailedDisconnectExpectsNothing) {
@@ -106,5 +98,29 @@ TEST(ExpectedDisconnectTest, FailedDisconnectExpectsNothing) {
EXPECT_FALSE(expected.is_previous_ap(NEW_AP));
}
TEST(ExpectedDisconnectTest, DisconnectWithoutApExpectsTheAttemptsLeave) {
ExpectedDisconnect expected;
Driver driver;
driver.has_ap = false;
EXPECT_TRUE(disconnect(expected, driver));
// A real AP never matches the attempt's zero BSSID
EXPECT_FALSE(expected.is_previous_ap(NEW_AP));
// The driver reports the attempt as left, with no BSSID, after the next one starts
EXPECT_EQ(expected.consume_disconnect(NO_AP, true), ExpectedLeave::EXPECTED_LEAVE_ATTEMPT);
EXPECT_EQ(expected.consume_disconnect(NO_AP, true), ExpectedLeave::EXPECTED_LEAVE_NONE);
}
TEST(ExpectedDisconnectTest, AttemptLeaveExpectationEndsOnARealFailure) {
ExpectedDisconnect expected;
Driver driver;
driver.has_ap = false;
EXPECT_TRUE(disconnect(expected, driver));
// A probe failure of the new attempt is real, and nothing stays expected after it
EXPECT_EQ(expected.consume_disconnect(NO_AP, false), ExpectedLeave::EXPECTED_LEAVE_NONE);
EXPECT_EQ(expected.consume_disconnect(NO_AP, true), ExpectedLeave::EXPECTED_LEAVE_NONE);
}
} // namespace
} // namespace esphome::wifi::testing
@@ -11,11 +11,27 @@ api:
- action: fire_first_then_second
then:
- lambda: |-
// The client is not reading: about 110 KB of log lines fill the TCP pipe, then the overflow buffer
// The client is not reading: log lines fill the TCP pipe, then the overflow buffer. Until the
// client's receive window is closed, a late acknowledgement can still free send buffer space
// and let the overflow buffer drain, so keep going until it holds data with the window closed
auto *client = sndbuf_pin::api_log_client();
const std::string padding(250, 'x');
for (int i = 0; i < 400; i++) {
ESP_LOGD("fill", "fill line %s", padding.c_str());
const uint32_t start = millis();
bool full = false;
while (client != nullptr && millis() - start < 5000) {
const bool window_closed = sndbuf_pin::api_peer_window_closed();
if (client->try_to_clear_buffer(false)) {
ESP_LOGD("fill", "fill line %s", padding.c_str());
} else if (window_closed) {
full = true;
break;
} else {
delay(1);
}
}
// The api drops this line while the pipe is blocked; the test reads it from the console
if (!full)
ESP_LOGE("fill", "fill timed out");
// The test checks that this line was dropped, which means the overflow buffer still holds data
ESP_LOGD("fill", "last fill line");
# So this event waits in the batch
@@ -0,0 +1,65 @@
esphome:
name: api-shutdown-bp-test
host:
api:
# One queued message, so DisconnectRequest cannot join the overflow buffer once it holds data
max_send_queue: 1
actions:
- action: fill_then_restart
then:
- lambda: &fill_pipe |-
// The client is not reading: log lines fill the TCP pipe, then the overflow buffer. Keep going
// until the overflow buffer holds data with the client's window closed, so nothing drains it
auto *client = sndbuf_pin::api_log_client();
const std::string padding(250, 'x');
const uint32_t start = millis();
bool full = false;
while (client != nullptr && millis() - start < 5000) {
const bool window_closed = sndbuf_pin::api_peer_window_closed();
if (client->try_to_clear_buffer(false)) {
ESP_LOGD("fill", "fill line %s", padding.c_str());
} else if (window_closed) {
full = true;
break;
} else {
delay(1);
}
}
if (!full)
ESP_LOGE("fill", "fill timed out");
# The test reads again once this line is on the console
- logger.log: fill done
# DisconnectRequest cannot be sent now, so it waits in an otherwise empty batch
- button.press: restart_button
- action: fill_publish_then_restart
then:
- lambda: *fill_pipe
# Queued behind the blocked socket when the shutdown starts
- lambda: id(queued_sensor).publish_state(42);
- logger.log: fill done
- button.press: restart_button
button:
- platform: restart
id: restart_button
name: "Restart"
sensor:
- platform: template
id: queued_sensor
name: "Queued sensor"
external_components:
- source:
type: local
path: EXTERNAL_COMPONENT_PATH
components: [sndbuf_pin_component]
# Pins the device's socket send buffers for deterministic TCP backpressure
sndbuf_pin_component:
buffer_size: SERVER_SNDBUF
logger:
level: DEBUG
@@ -0,0 +1,114 @@
esphome:
name: api-shutdown-order
host:
logger:
api:
switch:
- platform: template
id: test_switch
name: "Test switch"
optimistic: true
button:
- platform: restart
id: restart_button
name: "Restart"
- platform: template
name: "Toggle then restart"
on_press:
- switch.toggle: test_switch
- button.press: restart_button
- platform: template
name: "Publish then restart"
on_press:
- lambda: |-
for (auto *sensor : App.get_sensors())
sensor->publish_state(1);
- button.press: restart_button
sensor:
# More states than one batch packet holds (MAX_MESSAGES_PER_BATCH is 34)
- platform: template
name: "Burst 0"
- platform: template
name: "Burst 1"
- platform: template
name: "Burst 2"
- platform: template
name: "Burst 3"
- platform: template
name: "Burst 4"
- platform: template
name: "Burst 5"
- platform: template
name: "Burst 6"
- platform: template
name: "Burst 7"
- platform: template
name: "Burst 8"
- platform: template
name: "Burst 9"
- platform: template
name: "Burst 10"
- platform: template
name: "Burst 11"
- platform: template
name: "Burst 12"
- platform: template
name: "Burst 13"
- platform: template
name: "Burst 14"
- platform: template
name: "Burst 15"
- platform: template
name: "Burst 16"
- platform: template
name: "Burst 17"
- platform: template
name: "Burst 18"
- platform: template
name: "Burst 19"
- platform: template
name: "Burst 20"
- platform: template
name: "Burst 21"
- platform: template
name: "Burst 22"
- platform: template
name: "Burst 23"
- platform: template
name: "Burst 24"
- platform: template
name: "Burst 25"
- platform: template
name: "Burst 26"
- platform: template
name: "Burst 27"
- platform: template
name: "Burst 28"
- platform: template
name: "Burst 29"
- platform: template
name: "Burst 30"
- platform: template
name: "Burst 31"
- platform: template
name: "Burst 32"
- platform: template
name: "Burst 33"
- platform: template
name: "Burst 34"
- platform: template
name: "Burst 35"
- platform: template
name: "Burst 36"
- platform: template
name: "Burst 37"
- platform: template
name: "Burst 38"
- platform: template
name: "Burst 39"
@@ -7,6 +7,12 @@
#include "esphome/components/api/api_server.h"
#include "esphome/core/log.h"
// Host only: clang-tidy also checks this file for ESP32, which has no Linux socket headers
#if defined(USE_HOST) && defined(__linux__)
#include <linux/sockios.h>
#include <sys/ioctl.h>
#endif
namespace esphome::sndbuf_pin {
static const char *const TAG = "sndbuf_pin";
@@ -52,4 +58,35 @@ void SndbufPinComponent::setup() {
}
}
api::APIConnection *api_log_client() {
for (const auto &conn : api::global_api_server->active_clients()) {
if (conn->get_log_subscription_level() > 0)
return conn.get();
}
return nullptr;
}
bool api_peer_window_closed() {
#if defined(USE_HOST) && defined(__linux__)
for (int fd = FIRST_USER_FD; fd < MAX_FD_SCAN; fd++) {
struct sockaddr_in addr{};
socklen_t addr_len = sizeof(addr);
if (::getsockname(fd, reinterpret_cast<struct sockaddr *>(&addr), &addr_len) != 0 ||
ntohs(addr.sin_port) != api::global_api_server->get_port())
continue;
// Skips the listener: only the accepted client socket has a peer
addr_len = sizeof(addr);
if (::getpeername(fd, reinterpret_cast<struct sockaddr *>(&addr), &addr_len) != 0)
continue;
int queued = 0;
int not_sent = 0;
if (::ioctl(fd, SIOCOUTQ, &queued) != 0 || ::ioctl(fd, SIOCOUTQNSD, &not_sent) != 0)
return false;
return not_sent > 0 && queued == not_sent;
}
#endif
// Unknown elsewhere: a fill loop waiting on this times out and says so
return false;
}
} // namespace esphome::sndbuf_pin
@@ -1,5 +1,6 @@
#pragma once
#include "esphome/components/api/api_connection.h"
#include "esphome/core/component.h"
namespace esphome::sndbuf_pin {
@@ -18,4 +19,13 @@ class SndbufPinComponent : public Component {
int buffer_size_;
};
/// The api client subscribed to logs, or nullptr. A fixture lambda cannot name the api namespace:
/// the test harness rewrites every "api" key followed by a colon in the YAML.
api::APIConnection *api_log_client();
/// True once the api client's receive window is closed: everything sent is acknowledged and data still
/// waits to be sent, so no acknowledgement can free send buffer space until the client reads again.
/// Linux host only; always false elsewhere.
bool api_peer_window_closed();
} // namespace esphome::sndbuf_pin
@@ -72,6 +72,9 @@ async def test_api_event_order_backpressure(
# The client stops reading here, so the device console tells when "first" fired
await client.send_message(api_pb2.ExecuteServiceRequest(key=action_key))
await waiter.wait_for("first fired", timeout=10)
assert not any("fill timed out" in line for line in waiter.lines), (
"the client's receive window never closed while the overflow buffer held data"
)
# The client reads again while the action still runs.
# On a timeout the asserts below show which event is missing
@@ -0,0 +1,112 @@
"""A client whose send path is full at shutdown still gets DisconnectRequest.
The disconnect then waits in an otherwise empty deferred batch. Teardown does not
advance the cached loop time that the batch_delay gate reads, so unless the batch
is made due at once it is never sent before the reboot.
"""
from __future__ import annotations
import asyncio
import sys
from aioesphomeapi import api_pb2
import pytest
from .log_utils import LineWaiter
from .raw_api_client import (
MESSAGE_TYPE_OF,
RECV_BUFFER_SIZE,
SNDBUF_PIN_LOG,
RawApiClient,
pin_server_sndbuf,
)
from .types import RunCompiledFunction
DISCONNECT_REQUEST = MESSAGE_TYPE_OF[api_pb2.DisconnectRequest]
SERVICES_RESPONSE = MESSAGE_TYPE_OF[api_pb2.ListEntitiesServicesResponse]
LIST_DONE_RESPONSE = MESSAGE_TYPE_OF[api_pb2.ListEntitiesDoneResponse]
SENSOR_STATE = MESSAGE_TYPE_OF[api_pb2.SensorStateResponse]
LINUX_ONLY = pytest.mark.skipif(
sys.platform != "linux",
reason="other kernels grow loopback buffers past the pinned size, so the pipe never fills",
)
async def _read_until_disconnect_or_closed(client: RawApiClient) -> None:
while not client.frame_counts[DISCONNECT_REQUEST]:
try:
await client._recv_once()
except (AssertionError, ConnectionError):
return
async def _fill_then_restart(
yaml_config: str,
run_compiled: RunCompiledFunction,
port: int,
action: str,
subscribe_states: bool,
) -> RawApiClient:
"""Run ``action`` with the client not reading, then read again during teardown."""
waiter = LineWaiter()
async with run_compiled(
pin_server_sndbuf(yaml_config), line_callback=waiter.callback
):
await waiter.wait_for(SNDBUF_PIN_LOG, timeout=10)
async with RawApiClient(port, recv_buffer_size=RECV_BUFFER_SIZE) as client:
await client.connect(client_info="shutdown-backpressure-client")
await client.send_message(api_pb2.ListEntitiesRequest())
await client.read_until_frame(LIST_DONE_RESPONSE)
services = [
api_pb2.ListEntitiesServicesResponse.FromString(payload)
for payload in client.frames(SERVICES_RESPONSE)
]
(action_key,) = (svc.key for svc in services if svc.name == action)
if subscribe_states:
await client.send_message(api_pb2.SubscribeStatesRequest())
# Take the initial state so nothing is queued before the fill
await client.read_until_frame(SENSOR_STATE)
await client.send_message(
api_pb2.SubscribeLogsRequest(level=api_pb2.LOG_LEVEL_DEBUG)
)
# The client stops reading here; the device console tells when the pipe is full
await client.send_message(api_pb2.ExecuteServiceRequest(key=action_key))
await waiter.wait_for("fill done", timeout=10)
# Logged just before the shutdown hooks; reading earlier would let the direct send succeed
await waiter.wait_for("Rebooting safely", timeout=10)
assert not any("fill timed out" in line for line in waiter.lines), (
"the client's receive window never closed while the overflow buffer held data"
)
# Read again during the 1 s teardown; the disconnect must arrive before the reboot
await asyncio.wait_for(_read_until_disconnect_or_closed(client), 10)
return client
@LINUX_ONLY
@pytest.mark.asyncio
@pytest.mark.parametrize(
("action", "subscribe_states"),
[
("fill_then_restart", False),
# A state queued behind the blocked socket when the shutdown starts
("fill_publish_then_restart", True),
],
)
async def test_api_shutdown_disconnect_backpressure(
yaml_config: str,
run_compiled: RunCompiledFunction,
unused_tcp_port: int,
action: str,
subscribe_states: bool,
) -> None:
client = await _fill_then_restart(
yaml_config, run_compiled, unused_tcp_port, action, subscribe_states
)
assert client.frame_counts[DISCONNECT_REQUEST] == 1, (
"the backed up client never got DisconnectRequest"
)
@@ -0,0 +1,82 @@
"""States published right before a shutdown reach the client before DisconnectRequest."""
from __future__ import annotations
from aioesphomeapi import (
ButtonInfo,
EntityState,
SensorInfo,
SensorState,
SwitchInfo,
SwitchState,
)
import pytest
from .state_utils import require_entity, subscribe_after_initial_states
from .types import APIClientConnectedWithDisconnectFactory, RunCompiledFunction
from .wait_utils import wait_or_fail
# Sensors in the fixture: more than MAX_MESSAGES_PER_BATCH (34) states
BURST_SENSORS = 40
@pytest.mark.asyncio
@pytest.mark.shared_yaml("api_shutdown_sends_states_first")
async def test_api_shutdown_sends_states_first(
yaml_config: str,
run_compiled: RunCompiledFunction,
api_client_connected_with_disconnect: APIClientConnectedWithDisconnectFactory,
) -> None:
async with (
run_compiled(yaml_config),
api_client_connected_with_disconnect() as (client, disconnect_event),
):
entities, _ = await client.list_entities_services()
switch = require_entity(entities, "test_switch", SwitchInfo)
button = require_entity(entities, "toggle_then_restart", ButtonInfo)
states: list[EntityState] = []
sub = await subscribe_after_initial_states(client, entities, states.append)
assert sub.initial_states[switch.key].state is False
client.button_command(button.key)
await wait_or_fail(
disconnect_event, msg="device never disconnected", states=states
)
assert [s.state for s in states if isinstance(s, SwitchState)] == [True], (
"the toggled state must arrive before the device disconnects"
)
@pytest.mark.asyncio
@pytest.mark.shared_yaml("api_shutdown_sends_states_first")
async def test_api_shutdown_sends_burst_before_disconnect(
yaml_config: str,
run_compiled: RunCompiledFunction,
api_client_connected_with_disconnect: APIClientConnectedWithDisconnectFactory,
) -> None:
"""More states than one batch packet holds all arrive before the disconnect."""
async with (
run_compiled(yaml_config),
api_client_connected_with_disconnect() as (client, disconnect_event),
):
entities, _ = await client.list_entities_services()
button = require_entity(entities, "publish_then_restart", ButtonInfo)
burst_keys = {e.key for e in entities if isinstance(e, SensorInfo)}
assert len(burst_keys) == BURST_SENSORS
states: list[EntityState] = []
await subscribe_after_initial_states(client, entities, states.append)
client.button_command(button.key)
await wait_or_fail(
disconnect_event, msg="device never disconnected", states=states
)
received = {
s.key for s in states if isinstance(s, SensorState) and s.key in burst_keys
}
assert received == burst_keys, (
f"{len(burst_keys - received)} of {BURST_SENSORS} states missed the disconnect"
)