mirror of
https://github.com/esphome/esphome.git
synced 2026-11-18 11:06:02 +00:00
Compare commits
31
Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
6c28ef21d9 | ||
|
|
ab6e3d9bf0 | ||
|
|
10ef6e371b | ||
|
|
c84cd04cf7 | ||
|
|
fb6d7bf265 | ||
|
|
3ad8e35be8 | ||
|
|
11f9f20b61 | ||
|
|
b6d0961f25 | ||
|
|
6046d067aa | ||
|
|
690c5f2fb6 | ||
|
|
654954f684 | ||
|
|
e27614fda3 | ||
|
|
8426d04138 | ||
|
|
4a39ca1b71 | ||
|
|
4a218fdbd8 | ||
|
|
d8fc1cd214 | ||
|
|
1e030b7371 | ||
|
|
3b113f2c39 | ||
|
|
73468592a9 | ||
|
|
a1b456150d | ||
|
|
865a883abb | ||
|
|
357a6717c8 | ||
|
|
bcb434e05e | ||
|
|
fb65fcc5f6 | ||
|
|
b5e379a9d4 | ||
|
|
05286dfd5d | ||
|
|
36184f09aa | ||
|
|
449933891e | ||
|
|
a5bfa6dac3 | ||
|
|
5aaa5b0552 | ||
|
|
9ab090a818 |
No files matched your search
@@ -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.0b7
|
||||
|
||||
# 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
|
||||
|
||||
@@ -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);
|
||||
|
||||
@@ -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() {
|
||||
|
||||
@@ -65,13 +65,24 @@ void CST816Touchscreen::update_touches() {
|
||||
this->status_set_warning();
|
||||
return;
|
||||
}
|
||||
uint8_t num_of_touches = data[REG_TOUCH_NUM] & 3;
|
||||
// The low nibble is the touch count. CST816/CST820 are single-touch;
|
||||
// CST826/CST836 can report two points, but this driver only reads the first
|
||||
// point. Some CST8xx controllers report an all-0xFF frame when idle or after
|
||||
// disabling auto-sleep: the count lands above two and the coordinates are
|
||||
// the 0xFFF sentinel, so both guards below reject it.
|
||||
uint8_t num_of_touches = data[REG_TOUCH_NUM] & 0x0F;
|
||||
if (num_of_touches == 0) {
|
||||
return;
|
||||
}
|
||||
if (num_of_touches > 2) {
|
||||
return;
|
||||
}
|
||||
|
||||
uint16_t x = encode_uint16(data[REG_XPOS_HIGH] & 0xF, data[REG_XPOS_LOW]);
|
||||
uint16_t y = encode_uint16(data[REG_YPOS_HIGH] & 0xF, data[REG_YPOS_LOW]);
|
||||
if (x == 0x0FFF || y == 0x0FFF) {
|
||||
return;
|
||||
}
|
||||
ESP_LOGV(TAG, "Read touch %d/%d", x, y);
|
||||
this->add_raw_touch_position_(0, x, y);
|
||||
}
|
||||
|
||||
@@ -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() {
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -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) {
|
||||
@@ -176,23 +184,18 @@ void ArduinoI2CBus::recover_() {
|
||||
return;
|
||||
}
|
||||
|
||||
// From the specification:
|
||||
// "If the data line (SDA) is stuck LOW, send nine clock pulses. The
|
||||
// device that held the bus LOW should release it sometime within
|
||||
// those nine clocks."
|
||||
// We don't really have to detect if SDA is stuck low. We'll simply send
|
||||
// nine clock pulses here, just in case SDA is stuck. Actual checks on
|
||||
// the SDA line status will be done after the clock pulses.
|
||||
|
||||
// Make sure that switching to output mode will make SCL low, just in
|
||||
// case other code has setup the pin for a HIGH signal.
|
||||
digitalWrite(this->scl_pin_, LOW); // NOLINT
|
||||
// Activate input and pull up resistor for the SDA pin, so it can be read during the pulses.
|
||||
pinMode(this->sda_pin_, INPUT_PULLUP); // NOLINT
|
||||
|
||||
delayMicroseconds(half_period_usec);
|
||||
for (auto i = 0; i < 9; i++) {
|
||||
// Release pull up resistor and switch to output to make the signal LOW.
|
||||
pinMode(this->scl_pin_, INPUT); // NOLINT
|
||||
pinMode(this->scl_pin_, OUTPUT); // NOLINT
|
||||
// Spec: "If the data line (SDA) is stuck LOW, send nine clock pulses."
|
||||
// Pulse only while SDA is low: a device receiving a write ACKs every ninth pulse.
|
||||
for (auto i = 0; i < 9 && digitalRead(this->sda_pin_) == LOW; i++) { // NOLINT
|
||||
// Drive LOW. Set the level while the pin is a plain input: on RP2040 a write to an
|
||||
// INPUT_PULLUP pin drives it, which would leave SCL low if the loop never runs.
|
||||
pinMode(this->scl_pin_, INPUT); // NOLINT
|
||||
digitalWrite(this->scl_pin_, LOW); // NOLINT
|
||||
pinMode(this->scl_pin_, OUTPUT); // NOLINT
|
||||
delayMicroseconds(half_period_usec);
|
||||
|
||||
// Release output and activate pull up resistor to make the signal HIGH.
|
||||
@@ -219,14 +222,7 @@ void ArduinoI2CBus::recover_() {
|
||||
}
|
||||
}
|
||||
|
||||
// Activate input and pull resistor for the SDA pin, so we can verify
|
||||
// that SDA is pulled HIGH in the following step.
|
||||
pinMode(this->sda_pin_, INPUT_PULLUP); // NOLINT
|
||||
digitalWrite(this->sda_pin_, LOW); // NOLINT
|
||||
|
||||
// By now, any stuck device ought to have sent all remaining bits of its
|
||||
// transaction, meaning that it should have freed up the SDA line, resulting
|
||||
// in SDA being pulled up.
|
||||
// A device still holding SDA after nine pulses cannot be freed here.
|
||||
if (digitalRead(this->sda_pin_) == LOW) { // NOLINT
|
||||
ESP_LOGE(TAG, "Recovery failed: SDA is held LOW after clock pulse cycle");
|
||||
this->recovery_result_ = RECOVERY_FAILED_SDA_LOW;
|
||||
@@ -238,15 +234,13 @@ void ArduinoI2CBus::recover_() {
|
||||
// a START or repeated START condition such that they all anticipate
|
||||
// the sending of a target address, even if these START conditions are
|
||||
// not positioned according to the proper format."
|
||||
// While the 9 clock pulses from above might have drained all bits of a
|
||||
// single byte within a transaction, a device might have more bytes to
|
||||
// transmit. So here we'll generate a START condition to snap the device
|
||||
// out of this state.
|
||||
// SDA high does not mean the device is idle (it may be mid-byte); the START resets it.
|
||||
// SCL and SDA are already high at this point, so we can generate a START
|
||||
// condition by making the SDA signal LOW.
|
||||
delayMicroseconds(half_period_usec);
|
||||
pinMode(this->sda_pin_, INPUT); // NOLINT
|
||||
pinMode(this->sda_pin_, OUTPUT); // NOLINT
|
||||
pinMode(this->sda_pin_, INPUT); // NOLINT
|
||||
digitalWrite(this->sda_pin_, LOW); // NOLINT
|
||||
pinMode(this->sda_pin_, OUTPUT); // NOLINT
|
||||
|
||||
// From the specification:
|
||||
// "A START condition immediately followed by a STOP condition (void
|
||||
|
||||
@@ -346,14 +346,9 @@ void IDFI2CBus::recover_() {
|
||||
return;
|
||||
}
|
||||
|
||||
// From the specification:
|
||||
// "If the data line (SDA) is stuck LOW, send nine clock pulses. The
|
||||
// device that held the bus LOW should release it sometime within
|
||||
// those nine clocks."
|
||||
// We don't really have to detect if SDA is stuck low. We'll simply send
|
||||
// nine clock pulses here, just in case SDA is stuck. Actual checks on
|
||||
// the SDA line status will be done after the clock pulses.
|
||||
for (auto i = 0; i < 9; i++) {
|
||||
// Spec: "If the data line (SDA) is stuck LOW, send nine clock pulses."
|
||||
// Pulse only while SDA is low: a device receiving a write ACKs every ninth pulse.
|
||||
for (auto i = 0; i < 9 && gpio_get_level(sda_pin) == 0; i++) {
|
||||
gpio_set_level(scl_pin, 0);
|
||||
delayMicroseconds(half_period_usec);
|
||||
gpio_set_level(scl_pin, 1);
|
||||
@@ -378,9 +373,7 @@ void IDFI2CBus::recover_() {
|
||||
}
|
||||
}
|
||||
|
||||
// By now, any stuck device ought to have sent all remaining bits of its
|
||||
// transaction, meaning that it should have freed up the SDA line, resulting
|
||||
// in SDA being pulled up.
|
||||
// A device still holding SDA after nine pulses cannot be freed here.
|
||||
if (gpio_get_level(sda_pin) == 0) {
|
||||
ESP_LOGE(TAG, "Recovery failed: SDA is held LOW after clock pulse cycle");
|
||||
this->recovery_result_ = RECOVERY_FAILED_SDA_LOW;
|
||||
@@ -392,10 +385,7 @@ void IDFI2CBus::recover_() {
|
||||
// a START or repeated START condition such that they all anticipate
|
||||
// the sending of a target address, even if these START conditions are
|
||||
// not positioned according to the proper format."
|
||||
// While the 9 clock pulses from above might have drained all bits of a
|
||||
// single byte within a transaction, a device might have more bytes to
|
||||
// transmit. So here we'll generate a START condition to snap the device
|
||||
// out of this state.
|
||||
// SDA high does not mean the device is idle (it may be mid-byte); the START resets it.
|
||||
// SCL and SDA are already high at this point, so we can generate a START
|
||||
// condition by making the SDA signal LOW.
|
||||
delayMicroseconds(half_period_usec);
|
||||
|
||||
@@ -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().
|
||||
|
||||
@@ -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;
|
||||
}
|
||||
|
||||
@@ -9,7 +9,7 @@ void HOT Logger::write_msg_(const char *msg, uint16_t len) {
|
||||
char buffer[TIMESTAMP_LEN + ESPHOME_LOGGER_TX_BUFFER_SIZE + HEADROOM];
|
||||
|
||||
time_t rawtime;
|
||||
time(&rawtime);
|
||||
::time(&rawtime);
|
||||
struct tm timeinfo;
|
||||
localtime_r(&rawtime, &timeinfo); // Thread-safe version
|
||||
size_t pos = strftime(buffer, TIMESTAMP_LEN + 1, "[%H:%M:%S]", &timeinfo);
|
||||
|
||||
@@ -16,6 +16,7 @@ from esphome.const import (
|
||||
UNIT_CELSIUS,
|
||||
UNIT_MICROTESLA,
|
||||
)
|
||||
from esphome.core import CORE
|
||||
from esphome.types import ConfigType
|
||||
|
||||
CODEOWNERS = ["@functionpointer"]
|
||||
@@ -148,3 +149,5 @@ async def to_code(config: ConfigType) -> None:
|
||||
await cg.add_pin(var.set_drdy_gpio, config.get(CONF_DRDY_PIN))
|
||||
|
||||
cg.add_library("functionpointer/arduino-MLX90393", "1.0.2")
|
||||
if CORE.using_arduino:
|
||||
cg.add_library("Wire", None)
|
||||
@@ -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)
|
||||
|
||||
@@ -0,0 +1,165 @@
|
||||
#include "at_reply.h"
|
||||
|
||||
#include <cctype>
|
||||
#include <cstdlib>
|
||||
#include <cstring>
|
||||
#include <strings.h>
|
||||
|
||||
namespace esphome::modem {
|
||||
|
||||
bool collect_reply(char *buf, size_t size, size_t &len, const char *data, size_t data_len, bool whole) {
|
||||
const size_t capacity = size - 1;
|
||||
if (whole) {
|
||||
len = 0;
|
||||
}
|
||||
bool dropped = false;
|
||||
if (data_len > capacity) {
|
||||
memcpy(buf, data + data_len - capacity, capacity);
|
||||
len = capacity;
|
||||
dropped = true;
|
||||
} else {
|
||||
if (len + data_len > capacity) {
|
||||
const size_t drop = len + data_len - capacity;
|
||||
memmove(buf, buf + drop, len - drop);
|
||||
len -= drop;
|
||||
dropped = true;
|
||||
}
|
||||
memcpy(buf + len, data, data_len);
|
||||
len += data_len;
|
||||
}
|
||||
buf[len] = '\0';
|
||||
return dropped;
|
||||
}
|
||||
|
||||
ReplyEnd reply_end(const char *data, size_t len, bool skip_first) {
|
||||
const char *end = data + len;
|
||||
const char *line = data;
|
||||
if (skip_first) {
|
||||
const auto *newline = static_cast<const char *>(memchr(data, '\n', len));
|
||||
line = newline != nullptr ? newline + 1 : end;
|
||||
}
|
||||
while (line < end) {
|
||||
const auto *newline = static_cast<const char *>(memchr(line, '\n', end - line));
|
||||
if (newline == nullptr) {
|
||||
break; // the line has not ended yet
|
||||
}
|
||||
size_t size = newline - line;
|
||||
if (size != 0 && line[size - 1] == '\r') {
|
||||
size--;
|
||||
}
|
||||
if (size == 2 && memcmp(line, "OK", 2) == 0) {
|
||||
return ReplyEnd::REPLY_END_OK;
|
||||
}
|
||||
// esp_modem drops a CMUX frame without a line end, so the ": <reason>" part may be missing
|
||||
if ((size == 5 && memcmp(line, "ERROR", 5) == 0) || (size >= 10 && memcmp(line, "+CME ERROR", 10) == 0) ||
|
||||
(size >= 10 && memcmp(line, "+CMS ERROR", 10) == 0)) {
|
||||
return ReplyEnd::REPLY_END_ERROR;
|
||||
}
|
||||
line = newline + 1;
|
||||
}
|
||||
return ReplyEnd::REPLY_END_NONE;
|
||||
}
|
||||
|
||||
size_t flatten_reply(char *reply, size_t len) {
|
||||
for (size_t i = 0; i < len; i++) {
|
||||
if (reply[i] == '\r' || reply[i] == '\n') {
|
||||
reply[i] = ' ';
|
||||
}
|
||||
}
|
||||
while (len != 0 && reply[len - 1] == ' ') {
|
||||
len--;
|
||||
}
|
||||
if (len >= 2 && memcmp(reply + len - 2, "OK", 2) == 0 && (len == 2 || reply[len - 3] == ' ')) {
|
||||
len -= 2;
|
||||
while (len != 0 && reply[len - 1] == ' ') {
|
||||
len--;
|
||||
}
|
||||
}
|
||||
size_t start = 0;
|
||||
while (start < len && reply[start] == ' ') {
|
||||
start++;
|
||||
}
|
||||
len -= start;
|
||||
memmove(reply, reply + start, len);
|
||||
reply[len] = '\0';
|
||||
return len;
|
||||
}
|
||||
|
||||
int registration_from_reply(const char *reply, const char *prefix) {
|
||||
const size_t prefix_len = strlen(prefix);
|
||||
for (const char *line = strstr(reply, prefix); line != nullptr; line = strstr(line + 1, prefix)) {
|
||||
const char *start = line + prefix_len;
|
||||
char *end = nullptr;
|
||||
strtol(start, &end, 10);
|
||||
if (end == start || *end != ',') {
|
||||
continue;
|
||||
}
|
||||
start = end + 1;
|
||||
const auto stat = static_cast<int32_t>(strtol(start, &end, 10));
|
||||
if (end != start) {
|
||||
return stat;
|
||||
}
|
||||
}
|
||||
return REGISTRATION_UNKNOWN;
|
||||
}
|
||||
|
||||
int merge_registration(int lte, int legacy) {
|
||||
// A multimode modem can be registered or still searching on one network type while the other refuses it
|
||||
if (is_registered(lte)) {
|
||||
return lte;
|
||||
}
|
||||
if (is_registered(legacy)) {
|
||||
return legacy;
|
||||
}
|
||||
// A definite answer from one query outweighs a timeout of the other
|
||||
if (lte == REGISTRATION_SEARCHING || legacy == REGISTRATION_SEARCHING) {
|
||||
return REGISTRATION_SEARCHING;
|
||||
}
|
||||
if (lte == REGISTRATION_DENIED || legacy == REGISTRATION_DENIED) {
|
||||
return REGISTRATION_DENIED;
|
||||
}
|
||||
if (lte >= 0 || legacy >= 0) {
|
||||
return lte >= 0 ? lte : legacy;
|
||||
}
|
||||
if (lte == REGISTRATION_NO_ANSWER || legacy == REGISTRATION_NO_ANSWER) {
|
||||
return REGISTRATION_NO_ANSWER;
|
||||
}
|
||||
if (lte == REGISTRATION_UNSUPPORTED && legacy == REGISTRATION_UNSUPPORTED) {
|
||||
return REGISTRATION_UNSUPPORTED;
|
||||
}
|
||||
return REGISTRATION_UNKNOWN;
|
||||
}
|
||||
|
||||
bool reply_names_unsupported(const char *reply) {
|
||||
static constexpr char PREFIX[] = "+CME ERROR:";
|
||||
static constexpr char TEXT[] = "operation not supported";
|
||||
const char *code = strstr(reply, PREFIX);
|
||||
if (code == nullptr) {
|
||||
return false;
|
||||
}
|
||||
code += sizeof(PREFIX) - 1;
|
||||
while (*code == ' ') {
|
||||
code++;
|
||||
}
|
||||
return strncasecmp(code, TEXT, sizeof(TEXT) - 1) == 0 || (code[0] == '4' && !isdigit(code[1]));
|
||||
}
|
||||
|
||||
const char *sim_status(char *reply) {
|
||||
static constexpr char PREFIX[] = "+CPIN:";
|
||||
char *status = strstr(reply, PREFIX);
|
||||
if (status == nullptr) {
|
||||
return nullptr;
|
||||
}
|
||||
status += sizeof(PREFIX) - 1;
|
||||
while (*status == ' ') {
|
||||
status++;
|
||||
}
|
||||
// A URC in the same reply follows on its own line, which flatten_reply() turned into two spaces
|
||||
char *end = strstr(status, " ");
|
||||
if (end != nullptr) {
|
||||
*end = '\0';
|
||||
}
|
||||
return status;
|
||||
}
|
||||
|
||||
} // namespace esphome::modem
|
||||
@@ -0,0 +1,67 @@
|
||||
#pragma once
|
||||
|
||||
#include <cstddef>
|
||||
#include <cstdint>
|
||||
|
||||
// Parsers for raw AT replies; free of esp_modem so they build and run in host unit tests
|
||||
namespace esphome::modem {
|
||||
|
||||
enum class ReplyEnd : uint8_t {
|
||||
REPLY_END_NONE, // no complete final result line yet
|
||||
REPLY_END_OK, // an OK line
|
||||
REPLY_END_ERROR, // an ERROR, +CME ERROR: or +CMS ERROR: line
|
||||
};
|
||||
|
||||
// AT+CEREG? and AT+CREG? <stat> values (3GPP TS 27.007)
|
||||
static constexpr int REGISTRATION_NOT_SEARCHING = 0;
|
||||
static constexpr int REGISTRATION_HOME = 1;
|
||||
static constexpr int REGISTRATION_SEARCHING = 2;
|
||||
static constexpr int REGISTRATION_DENIED = 3;
|
||||
static constexpr int REGISTRATION_REPORTED_UNKNOWN = 4;
|
||||
static constexpr int REGISTRATION_ROAMING = 5;
|
||||
static constexpr int REGISTRATION_SMS_ONLY_HOME = 6;
|
||||
static constexpr int REGISTRATION_SMS_ONLY_ROAMING = 7;
|
||||
static constexpr int REGISTRATION_EMERGENCY_ONLY = 8;
|
||||
static constexpr int REGISTRATION_CSFB_NOT_PREFERRED_HOME = 9;
|
||||
static constexpr int REGISTRATION_CSFB_NOT_PREFERRED_ROAMING = 10;
|
||||
// Results beyond the 3GPP values
|
||||
static constexpr int REGISTRATION_UNKNOWN = -1; // no readable answer
|
||||
static constexpr int REGISTRATION_UNSUPPORTED = -2; // the query is not supported
|
||||
static constexpr int REGISTRATION_NO_ANSWER = -3; // the query timed out
|
||||
|
||||
/// Registered on a network, so the data attach can be requested. SMS only and "CSFB not preferred" limit the
|
||||
/// circuit switched side, not the packet data attach; emergency only (8) is not a registration.
|
||||
inline bool is_registered(int registration) {
|
||||
return registration == REGISTRATION_HOME || registration == REGISTRATION_ROAMING ||
|
||||
(registration >= REGISTRATION_SMS_ONLY_HOME && registration <= REGISTRATION_SMS_ONLY_ROAMING) ||
|
||||
(registration >= REGISTRATION_CSFB_NOT_PREFERRED_HOME &&
|
||||
registration <= REGISTRATION_CSFB_NOT_PREFERRED_ROAMING);
|
||||
}
|
||||
|
||||
/// Add data to the reply in buf (size includes the terminator, len is the current length). With whole, data is
|
||||
/// everything received so far (UART) and replaces the reply; otherwise it is one new CMUX frame and is appended. A
|
||||
/// reply longer than the buffer keeps its newest bytes, where the final line is. Returns true when bytes were dropped.
|
||||
bool collect_reply(char *buf, size_t size, size_t &len, const char *data, size_t data_len, bool whole);
|
||||
|
||||
/// How the reply ends. Only a complete final result line counts, so ERROR inside a payload line does not; a +CME ERROR
|
||||
/// line counts even without its reason, which a dropped CMUX frame can take away. With
|
||||
/// skip_first, the first line is a cut-off fragment and is ignored.
|
||||
ReplyEnd reply_end(const char *data, size_t len, bool skip_first);
|
||||
|
||||
/// Puts a raw reply on one line for the log and drops the final OK; returns the new length
|
||||
size_t flatten_reply(char *reply, size_t len);
|
||||
|
||||
/// The <stat> after the first comma of the "<n>,<stat>" line starting with prefix (+CREG: and friends), or
|
||||
/// REGISTRATION_UNKNOWN; a one-field line is an unsolicited notification and is skipped
|
||||
int registration_from_reply(const char *reply, const char *prefix);
|
||||
|
||||
/// The state that tells most from the AT+CEREG? (LTE) and AT+CREG? (2G and 3G) results
|
||||
int merge_registration(int lte, int legacy);
|
||||
|
||||
/// Whether an error reply says the command is not supported (+CME ERROR: 4 or its verbose text)
|
||||
bool reply_names_unsupported(const char *reply);
|
||||
|
||||
/// The SIM status in a flattened AT+CPIN? reply, cut before any following line, or nullptr
|
||||
const char *sim_status(char *reply);
|
||||
|
||||
} // namespace esphome::modem
|
||||
@@ -1,5 +1,6 @@
|
||||
#ifdef USE_ESP32
|
||||
#include "modem_component.h"
|
||||
#include "at_reply.h"
|
||||
#include "soc/soc_caps.h"
|
||||
#include <driver/uart.h>
|
||||
|
||||
@@ -42,6 +43,14 @@ static constexpr uint32_t PROBE_TIMEOUT_MS = 100;
|
||||
// A silent modem is not going to answer the next second, so its probes are spaced out, doubling up to a minute
|
||||
static constexpr uint32_t PROBE_RETRY_DELAY_MS = 5000;
|
||||
static constexpr uint32_t PROBE_RETRY_MAX_MS = 60000;
|
||||
// A modem in command mode answers the ATE0 sent before the mode detection within this time
|
||||
static constexpr uint32_t ECHO_OFF_TIMEOUT_MS = 100;
|
||||
// The +++ escape needs a second of silence before it; esp_modem waits for the answer after it
|
||||
static constexpr uint32_t ESCAPE_GUARD_MS = 1100;
|
||||
// A SIM7670 left in CMUX by an ESP reset ignores every frame until this many flag bytes overflow its receive buffer;
|
||||
// it then reinitialises its AT channel within about 2 s
|
||||
static constexpr size_t CMUX_FLUSH_BYTES = 4096;
|
||||
static constexpr uint32_t CMUX_FLUSH_SETTLE_MS = 3000;
|
||||
// How long the task waits for the main loop to run on_sync before attaching
|
||||
static constexpr uint32_t SYNC_ACK_TIMEOUT_MS = 5000;
|
||||
// esp_modem's command strings and the log lines are built on this stack
|
||||
@@ -56,7 +65,14 @@ 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 uint8_t ATTACH_UNKNOWN_POLLS = 15; // about a minute of polls
|
||||
// AT+CGATT=1: SIMCom documents up to 75 s, but a SIM7080G that is not registered yet held it for 141 s
|
||||
static constexpr uint32_t ATTACH_TIMEOUT_MS = 150000;
|
||||
// SIM status polls that fail before a warning, and between repeats of it
|
||||
static constexpr uint8_t SIM_WARN_AFTER = 10;
|
||||
static constexpr uint8_t SIM_WARN_EVERY = 60;
|
||||
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;
|
||||
@@ -72,8 +88,22 @@ static const LogString *state_to_str(ModemState state) {
|
||||
// Indexed by ModemFault
|
||||
PROGMEM_STRING_TABLE(ModemFaultStrings, "", "Modem not responding", "Configured baud rate failed",
|
||||
"init_at command failed", "SIM not ready", "SIM locked", "SIM PIN not sent; check pin_code",
|
||||
"PIN record not saved", "APN or radio setup failed", "No network attachment", "PPP link failed",
|
||||
"Modem teardown failed");
|
||||
"PIN record not saved", "APN or radio setup failed", "No network attachment",
|
||||
"Network registration denied", "PPP link failed", "Modem teardown failed");
|
||||
|
||||
// Indexed by the AT+CEREG? <stat> value
|
||||
PROGMEM_STRING_TABLE(RegistrationStrings, "not searching", "registered", "searching", "denied", "unknown", "roaming",
|
||||
"registered for SMS only", "roaming for SMS only", "emergency only",
|
||||
"registered, CSFB not preferred", "roaming, CSFB not preferred", "other");
|
||||
static const LogString *registration_to_str(int registration) {
|
||||
if (registration == REGISTRATION_UNSUPPORTED) {
|
||||
return LOG_STR("not supported");
|
||||
}
|
||||
if (registration < 0) {
|
||||
return LOG_STR("not reported");
|
||||
}
|
||||
return RegistrationStrings::get_log_str(static_cast<uint8_t>(registration), RegistrationStrings::LAST_INDEX);
|
||||
}
|
||||
|
||||
// Indexed by esp_modem::command_result
|
||||
PROGMEM_STRING_TABLE(CommandResultStrings, "OK", "FAIL", "TIMEOUT");
|
||||
@@ -587,6 +617,9 @@ ModemState ModemComponent::compute_next_state_() {
|
||||
}
|
||||
|
||||
ModemState ModemComponent::handle_state_enabling_() {
|
||||
// With echo on (SIM7670G after power-up), the modem sends esp_modem's PPP probe back and the detection takes it for
|
||||
// a PPP reply; in PPP or CMUX these bytes are dropped as a bad frame
|
||||
this->at_reply_("ATE0", ECHO_OFF_TIMEOUT_MS);
|
||||
// Find the baud rate the modem answers at and leave it in command mode, synced
|
||||
if (!this->dce_->set_mode(modem_mode::AUTODETECT)) {
|
||||
// A failed detection keeps the previous mode, which would skip the baud rate search below
|
||||
@@ -612,21 +645,18 @@ ModemState ModemComponent::handle_state_enabling_() {
|
||||
exited = false;
|
||||
const int bauds[] = {this->restored_baud_, this->baud_rate_, FALLBACK_BAUD_RATE};
|
||||
bool found = false;
|
||||
bool probed = false;
|
||||
bool rejected = false;
|
||||
for (size_t i = 0; i != std::size(bauds) && !found; i++) {
|
||||
const int baud = bauds[i];
|
||||
if (baud == 0 || baud == detected || std::find(bauds, bauds + i, baud) != bauds + i) {
|
||||
continue;
|
||||
}
|
||||
if (uart_set_baudrate(static_cast<uart_port_t>(this->uart_port_), baud) != ESP_OK) {
|
||||
if (this->set_uart_baud_(baud) != ESP_OK) {
|
||||
ESP_LOGW(TAG, "UART rejected baud rate %d", baud);
|
||||
this->set_fault_(ModemFault::MODEM_FAULT_BAUD);
|
||||
rejected = true;
|
||||
continue;
|
||||
}
|
||||
this->current_baud_rate_ = baud;
|
||||
probed = true;
|
||||
std::string output;
|
||||
found = this->dce_->at("AT", output, PROBE_TIMEOUT_MS) == command_result::OK;
|
||||
if (!found) {
|
||||
@@ -634,11 +664,31 @@ ModemState ModemComponent::handle_state_enabling_() {
|
||||
}
|
||||
}
|
||||
if (!found) {
|
||||
// The detection at the current rate already failed; a UART that refused every other rate set BAUD
|
||||
if (!probed && !rejected) {
|
||||
// A modem left in data mode before PPP started, or stuck in CMUX, answers none of the probes; both recoveries
|
||||
// go out at the rate the detection used
|
||||
if (this->current_baud_rate_ != detected) {
|
||||
const esp_err_t err = this->set_uart_baud_(detected);
|
||||
if (err != ESP_OK) {
|
||||
ESP_LOGW(TAG, "UART rejected baud rate %d (%s); data mode and CMUX recovery skipped", detected,
|
||||
esp_err_to_name(err));
|
||||
this->set_fault_(ModemFault::MODEM_FAULT_BAUD);
|
||||
rejected = true;
|
||||
}
|
||||
}
|
||||
// The escape before the flush is what a stuck SIM7670G responds to; the order is measured, not arbitrary
|
||||
if (this->current_baud_rate_ == detected && (this->escape_data_mode_() || this->flush_cmux_())) {
|
||||
exited = true;
|
||||
break;
|
||||
}
|
||||
// A UART that refused a rate set BAUD
|
||||
if (!rejected) {
|
||||
this->not_responding_(detected);
|
||||
}
|
||||
return this->retry_(ModemState::MODEM_STATE_ENABLING, this->next_probe_delay_());
|
||||
const uint32_t delay_ms = this->next_probe_delay_();
|
||||
if (delay_ms == PROBE_RETRY_MAX_MS) {
|
||||
this->answered_ = false; // silent for minutes, so the wiring hint applies again
|
||||
}
|
||||
return this->retry_(ModemState::MODEM_STATE_ENABLING, delay_ms);
|
||||
}
|
||||
ESP_LOGD(TAG, "Modem responded at baud %d", this->current_baud_rate_);
|
||||
break;
|
||||
@@ -672,33 +722,48 @@ 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);
|
||||
// The modem answers AT, so it is in command mode. A failed data mode exit leaves esp_modem's UART side in data mode,
|
||||
// from where its later switch to CMUX fails at once, so its mode is set to match; this sends nothing to the modem.
|
||||
if (!this->dce_->set_mode(modem_mode::RESUME_COMMAND_MODE)) {
|
||||
ESP_LOGW(TAG, "esp_modem refused command mode; the switch to PPP may fail");
|
||||
}
|
||||
// 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");
|
||||
}
|
||||
// Verbose +CME ERROR text, so a failed command or a missing SIM says why; a modem without it keeps plain ERROR
|
||||
if (this->dce_->config_mobile_termination_error(2) != command_result::OK) {
|
||||
ESP_LOGW(TAG, "Modem refused verbose errors (AT+CMEE=2)");
|
||||
}
|
||||
|
||||
// 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
|
||||
this->answered_ = true;
|
||||
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_);
|
||||
if (this->dce_->set_baud(this->baud_rate_) == command_result::OK) {
|
||||
delay(200); // NOLINT
|
||||
if (uart_set_baudrate(static_cast<uart_port_t>(this->uart_port_), this->baud_rate_) != ESP_OK) {
|
||||
if (this->set_uart_baud_(this->baud_rate_) != ESP_OK) {
|
||||
ESP_LOGE(TAG, "UART rejected baud rate %d", this->baud_rate_);
|
||||
this->set_fault_(ModemFault::MODEM_FAULT_BAUD);
|
||||
return ModemState::MODEM_STATE_DISCONNECTING;
|
||||
}
|
||||
this->current_baud_rate_ = this->baud_rate_;
|
||||
if (this->dce_->sync() != command_result::OK) {
|
||||
ESP_LOGE(TAG, "Failed to sync modem at baud rate %d.", this->baud_rate_);
|
||||
this->set_fault_(ModemFault::MODEM_FAULT_BAUD);
|
||||
@@ -726,6 +791,7 @@ ModemState ModemComponent::handle_state_enabling_() {
|
||||
ESP_LOGW(TAG, "Modem refused hardware flow control; UART overruns are possible");
|
||||
}
|
||||
this->modem_synced_ = true;
|
||||
this->answered_ = true;
|
||||
return ModemState::MODEM_STATE_SYNCED;
|
||||
}
|
||||
|
||||
@@ -741,12 +807,13 @@ ModemState ModemComponent::handle_state_synced_() {
|
||||
this->clear_config_fault_(ModemFault::MODEM_FAULT_INIT_AT);
|
||||
}
|
||||
this->connect_step_ = ConnectStep::CONNECT_STEP_START;
|
||||
this->sim_polls_ = 0;
|
||||
return ModemState::MODEM_STATE_CONNECTING;
|
||||
}
|
||||
|
||||
ModemState ModemComponent::handle_state_connecting_() {
|
||||
if (this->connect_step_ == ConnectStep::CONNECT_STEP_START) {
|
||||
// ENABLING just synced; only a failed attachment read comes back here unsure of the modem
|
||||
// ENABLING just synced; a command that timed out comes back here unsure of the modem
|
||||
if (this->resync_) {
|
||||
this->resync_ = false;
|
||||
if (this->dce_->sync() != command_result::OK) {
|
||||
@@ -754,7 +821,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 +838,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");
|
||||
@@ -786,35 +854,78 @@ ModemState ModemComponent::handle_state_connecting_() {
|
||||
}
|
||||
this->connect_step_ = ConnectStep::CONNECT_STEP_RADIO_ON;
|
||||
this->attach_polls_ = 0;
|
||||
}
|
||||
|
||||
if (this->connect_step_ == ConnectStep::CONNECT_STEP_RADIO_ON) {
|
||||
// A modem still searching for a network refuses this; the attachment poll asks again
|
||||
if (this->dce_->set_network_attachment_state(1) != command_result::OK) {
|
||||
ESP_LOGD(TAG, "Network attachment request refused");
|
||||
}
|
||||
this->connect_step_ = ConnectStep::CONNECT_STEP_ATTACH_REQUESTED;
|
||||
this->idle_polls_ = 0;
|
||||
this->unknown_polls_ = 0;
|
||||
}
|
||||
|
||||
int attached = 0;
|
||||
if (this->dce_->get_network_attachment_state(attached) != command_result::OK) {
|
||||
ESP_LOGW(TAG, "Failed to read the network attachment state");
|
||||
this->set_fault_(ModemFault::MODEM_FAULT_NOT_ATTACHED);
|
||||
this->connect_step_ = ConnectStep::CONNECT_STEP_START; // start over with a sync on the next pass
|
||||
this->resync_ = true;
|
||||
this->request_resync_();
|
||||
return this->retry_(ModemState::MODEM_STATE_CONNECTING);
|
||||
}
|
||||
if (!attached) {
|
||||
const int registration = this->read_registration_();
|
||||
if (registration == REGISTRATION_NO_ANSWER) {
|
||||
// The state is open, so the modem is checked with a sync instead
|
||||
ESP_LOGW(TAG, "No answer to a registration query");
|
||||
this->request_resync_();
|
||||
this->set_fault_(ModemFault::MODEM_FAULT_NOT_ATTACHED);
|
||||
return this->retry_(ModemState::MODEM_STATE_CONNECTING);
|
||||
}
|
||||
// "Not searching" for a few polls in a row means the attachment is off; right after CFUN=1 it is only brief
|
||||
this->idle_polls_ = registration == REGISTRATION_NOT_SEARCHING ? static_cast<uint8_t>(this->idle_polls_ + 1) : 0;
|
||||
const bool idle = this->idle_polls_ >= ATTACH_DIAG_EVERY;
|
||||
// A state that cannot be read or means nothing here holds the request back only for a while
|
||||
const bool unknown = registration == REGISTRATION_UNKNOWN || registration == REGISTRATION_REPORTED_UNKNOWN ||
|
||||
(registration > REGISTRATION_ROAMING && !is_registered(registration));
|
||||
this->unknown_polls_ = unknown ? static_cast<uint8_t>(this->unknown_polls_ + 1) : 0;
|
||||
const bool unknown_too_long = this->unknown_polls_ >= ATTACH_UNKNOWN_POLLS;
|
||||
if (this->connect_step_ == ConnectStep::CONNECT_STEP_RADIO_ON &&
|
||||
(is_registered(registration) || registration == REGISTRATION_UNSUPPORTED || idle || unknown_too_long)) {
|
||||
if (unknown_too_long) {
|
||||
ESP_LOGI(TAG, "Registration state %d (%s) for %u polls; requesting the network attachment anyway", registration,
|
||||
LOG_STR_ARG(registration_to_str(registration)), this->unknown_polls_);
|
||||
this->unknown_polls_ = 0;
|
||||
}
|
||||
// Before registration a modem can hold the AT channel on this for minutes and drop every command meanwhile,
|
||||
// so it waits for one; the fallbacks above send it anyway, and ATTACH_TIMEOUT_MS covers that hold
|
||||
const command_result attach = this->at_reply_("AT+CGATT=1", ATTACH_TIMEOUT_MS);
|
||||
if (attach == command_result::TIMEOUT) {
|
||||
ESP_LOGW(TAG, "No answer to the network attachment request");
|
||||
this->set_fault_(ModemFault::MODEM_FAULT_NOT_ATTACHED);
|
||||
this->request_resync_();
|
||||
return this->retry_(ModemState::MODEM_STATE_CONNECTING);
|
||||
}
|
||||
if (attach != command_result::OK) {
|
||||
ESP_LOGW(TAG, "Network attachment request refused: %s", this->reply_);
|
||||
}
|
||||
this->connect_step_ = ConnectStep::CONNECT_STEP_ATTACH_REQUESTED;
|
||||
return this->retry_(ModemState::MODEM_STATE_CONNECTING);
|
||||
}
|
||||
if (this->attach_polls_++ % ATTACH_DIAG_EVERY == 0) {
|
||||
int rssi = 99, ber = 99, registration = -1, mode = -1; // -1 stays when the query fails
|
||||
this->dce_->get_network_registration_state(registration);
|
||||
int rssi = 99, ber = 99, mode = -1; // -1 stays when the query fails
|
||||
this->dce_->get_signal_quality(rssi, ber);
|
||||
this->dce_->get_network_system_mode(mode);
|
||||
ESP_LOGW(TAG, "Modem not attached yet: registration %d, network type %d, CSQ %d, BER %d", registration, mode,
|
||||
rssi, ber);
|
||||
ESP_LOGW(TAG, "Modem not attached yet: registration %d (%s), network type %d, CSQ %d, BER %d", registration,
|
||||
LOG_STR_ARG(registration_to_str(registration)), mode, rssi, ber);
|
||||
if (unknown) {
|
||||
ESP_LOGW(TAG, "Network attachment request held back while the registration state is unknown (%u of %u polls)",
|
||||
this->unknown_polls_, ATTACH_UNKNOWN_POLLS);
|
||||
}
|
||||
if (registration == REGISTRATION_DENIED) {
|
||||
// The modem's reason tells a rejected APN (ESM failure) from a SIM the network does not serve
|
||||
const command_result reason_read = this->at_reply_("AT+CEER");
|
||||
ESP_LOGW(TAG,
|
||||
"The network refused registration (AT+CEER %s: %s); check the APN and that the SIM's plan covers "
|
||||
"this modem's network types",
|
||||
LOG_STR_ARG(result_to_str(reason_read)), this->reply_);
|
||||
}
|
||||
this->connect_step_ = ConnectStep::CONNECT_STEP_RADIO_ON; // request the attachment again
|
||||
}
|
||||
this->set_fault_(ModemFault::MODEM_FAULT_NOT_ATTACHED);
|
||||
this->set_fault_(registration == REGISTRATION_DENIED ? ModemFault::MODEM_FAULT_REGISTRATION_DENIED
|
||||
: ModemFault::MODEM_FAULT_NOT_ATTACHED);
|
||||
return this->retry_(ModemState::MODEM_STATE_CONNECTING, ATTACH_POLL_MS);
|
||||
}
|
||||
|
||||
@@ -864,11 +975,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_();
|
||||
@@ -916,11 +1028,114 @@ void ModemComponent::transition_to_(ModemState next_state) {
|
||||
}
|
||||
}
|
||||
|
||||
esp_err_t ModemComponent::set_uart_baud_(int baud) {
|
||||
const esp_err_t err = uart_set_baudrate(static_cast<uart_port_t>(this->uart_port_), baud);
|
||||
if (err == ESP_OK) {
|
||||
this->current_baud_rate_ = baud;
|
||||
}
|
||||
return err;
|
||||
}
|
||||
|
||||
bool ModemComponent::escape_data_mode_() {
|
||||
delay(ESCAPE_GUARD_MS); // NOLINT
|
||||
if (this->dce_->set_command_mode() != command_result::OK) {
|
||||
ESP_LOGD(TAG, "No answer after the data mode escape");
|
||||
return false;
|
||||
}
|
||||
ESP_LOGW(TAG, "Modem was left in data mode; escaped to command mode");
|
||||
return true;
|
||||
}
|
||||
|
||||
bool ModemComponent::flush_cmux_() {
|
||||
// With RTS/CTS a silent modem can hold CTS off; the flag bytes would then stay queued and block every later write
|
||||
if (this->rts_pin_ != nullptr) {
|
||||
ESP_LOGW(TAG, "CMUX recovery is not available with RTS/CTS flow control");
|
||||
return false;
|
||||
}
|
||||
static constexpr uint8_t CMUX_FLAG = 0xF9;
|
||||
uint8_t flags[64];
|
||||
memset(flags, CMUX_FLAG, sizeof(flags));
|
||||
const auto port = static_cast<uart_port_t>(this->uart_port_);
|
||||
for (size_t sent = 0; sent < CMUX_FLUSH_BYTES; sent += sizeof(flags)) {
|
||||
if (this->dte_->write(flags, sizeof(flags)) != static_cast<int>(sizeof(flags))) {
|
||||
ESP_LOGW(TAG, "CMUX flush: short write after %u bytes", static_cast<unsigned>(sent));
|
||||
return false;
|
||||
}
|
||||
}
|
||||
// 10 bits per byte, plus a margin
|
||||
const uint32_t send_ms = CMUX_FLUSH_BYTES * 10 * 1000 / this->current_baud_rate_ + 100;
|
||||
if (uart_wait_tx_done(port, pdMS_TO_TICKS(send_ms)) != ESP_OK) {
|
||||
ESP_LOGW(TAG, "CMUX flush: transmit not finished after %u ms", static_cast<unsigned>(send_ms));
|
||||
return false;
|
||||
}
|
||||
delay(CMUX_FLUSH_SETTLE_MS); // NOLINT
|
||||
if (this->dce_->sync() != command_result::OK) {
|
||||
ESP_LOGD(TAG, "No answer after the CMUX flush");
|
||||
return false;
|
||||
}
|
||||
ESP_LOGW(TAG, "Modem was stuck in CMUX; reset it to command mode");
|
||||
return true;
|
||||
}
|
||||
|
||||
// A command timed out: the next pass checks with a sync that the modem still answers
|
||||
void ModemComponent::request_resync_() {
|
||||
this->connect_step_ = ConnectStep::CONNECT_STEP_START;
|
||||
this->resync_ = true;
|
||||
}
|
||||
|
||||
// One registration query: the <stat>, REGISTRATION_UNSUPPORTED, REGISTRATION_NO_ANSWER or REGISTRATION_UNKNOWN
|
||||
int ModemComponent::read_stat_(const char *cmd, const char *prefix) {
|
||||
const command_result result = this->at_reply_(cmd);
|
||||
if (result == command_result::TIMEOUT) {
|
||||
return REGISTRATION_NO_ANSWER;
|
||||
}
|
||||
if (result == command_result::FAIL && reply_names_unsupported(this->reply_)) {
|
||||
return REGISTRATION_UNSUPPORTED;
|
||||
}
|
||||
const int stat = result == command_result::OK ? registration_from_reply(this->reply_, prefix) : REGISTRATION_UNKNOWN;
|
||||
if (stat == REGISTRATION_UNKNOWN) {
|
||||
// An error such as a SIM failure is a warning on the diagnostic polls, so it shows at the default log level
|
||||
if (result == command_result::FAIL && this->attach_polls_ % ATTACH_DIAG_EVERY == 0) {
|
||||
ESP_LOGW(TAG, "%s failed: %s", cmd, this->reply_);
|
||||
} else {
|
||||
ESP_LOGD(TAG, "%s gave no state: %s", cmd, this->reply_);
|
||||
}
|
||||
}
|
||||
return stat;
|
||||
}
|
||||
|
||||
// AT+CEREG covers LTE; on 2G and 3G the network registration is in AT+CREG, and AT+CGATT=1 is answered quickly once
|
||||
// it is set. Returns the <stat> that tells most, or one of the negative REGISTRATION_ values.
|
||||
int ModemComponent::read_registration_() {
|
||||
const int lte = this->read_stat_("AT+CEREG?", "+CEREG:");
|
||||
if (is_registered(lte)) {
|
||||
return lte;
|
||||
}
|
||||
return merge_registration(lte, this->read_stat_("AT+CREG?", "+CREG:"));
|
||||
}
|
||||
|
||||
void ModemComponent::not_responding_(int baud) {
|
||||
ESP_LOGW(TAG, "Modem not responding at %d baud. Check power, PWRKEY and TX/RX wiring.", baud);
|
||||
if (this->answered_) {
|
||||
// It answered before, so the wiring is fine; a long command or a modem restart keeps it silent for minutes
|
||||
ESP_LOGW(TAG, "Modem not responding at %d baud; it may be busy or restarting", baud);
|
||||
} else {
|
||||
ESP_LOGW(TAG, "Modem not responding at %d baud. Check power, PWRKEY and TX/RX wiring.", baud);
|
||||
}
|
||||
this->set_fault_(ModemFault::MODEM_FAULT_NO_RESPONSE);
|
||||
}
|
||||
|
||||
void ModemComponent::sim_not_ready_(const char *reason) {
|
||||
// A missing or broken SIM never gets ready; the status is polled every second, so only some polls warn
|
||||
if (++this->sim_polls_ == SIM_WARN_AFTER + SIM_WARN_EVERY) {
|
||||
this->sim_polls_ = SIM_WARN_AFTER;
|
||||
}
|
||||
if (this->sim_polls_ == SIM_WARN_AFTER) {
|
||||
ESP_LOGW(TAG, "SIM not ready: %s. Check that a working SIM is inserted",
|
||||
reason[0] != '\0' ? reason : LOG_STR_LITERAL("no reply"));
|
||||
}
|
||||
this->set_fault_(ModemFault::MODEM_FAULT_SIM_NOT_READY);
|
||||
}
|
||||
|
||||
#ifdef USE_MODEM_SEND_AT
|
||||
bool ModemComponent::send_at(const std::string &cmd, uint32_t timeout) { return this->send_at(cmd.c_str(), timeout); }
|
||||
|
||||
@@ -947,13 +1162,54 @@ bool ModemComponent::send_at(const char *cmd, uint32_t timeout) {
|
||||
|
||||
#endif
|
||||
|
||||
// at() keeps no text from a failed command, so the raw reply is collected into reply_ to show the +CME ERROR
|
||||
command_result ModemComponent::at_reply_(const char *cmd, uint32_t timeout) {
|
||||
std::string line = cmd; // poll commands fit the short string buffer
|
||||
line += '\r';
|
||||
this->reply_len_ = 0;
|
||||
this->reply_[0] = '\0';
|
||||
#ifdef CONFIG_ESP_MODEM_USE_INFLATABLE_BUFFER_IF_NEEDED
|
||||
// The inflatable buffer gathers CMUX frames too, so every reply comes over whole each time
|
||||
const bool whole = true;
|
||||
#else
|
||||
// esp_modem hands a UART reply over whole each time, and a CMUX reply one frame at a time
|
||||
const modem_mode mode = this->dce_->get_mode();
|
||||
const bool whole = mode != modem_mode::CMUX_MODE && mode != modem_mode::CMUX_MANUAL_MODE &&
|
||||
mode != modem_mode::CMUX_MANUAL_DATA && mode != modem_mode::CMUX_MANUAL_COMMAND;
|
||||
#endif
|
||||
struct {
|
||||
bool whole;
|
||||
bool cut;
|
||||
} state{whole, false};
|
||||
const command_result result = this->dce_->command(
|
||||
line,
|
||||
[this, &state](const uint8_t *data, size_t len) {
|
||||
// OK, or FAIL once an ERROR line has ended (the +CME ERROR reason may still be on its way), else TIMEOUT
|
||||
state.cut |= collect_reply(this->reply_, sizeof(this->reply_), this->reply_len_,
|
||||
reinterpret_cast<const char *>(data), len, state.whole);
|
||||
switch (reply_end(this->reply_, this->reply_len_, state.cut)) {
|
||||
case ReplyEnd::REPLY_END_OK:
|
||||
return command_result::OK;
|
||||
case ReplyEnd::REPLY_END_ERROR:
|
||||
return command_result::FAIL;
|
||||
default:
|
||||
return command_result::TIMEOUT;
|
||||
}
|
||||
},
|
||||
timeout);
|
||||
this->reply_len_ = flatten_reply(this->reply_, this->reply_len_);
|
||||
this->reply_cut_ = state.cut;
|
||||
return result;
|
||||
}
|
||||
|
||||
command_result ModemComponent::at_(const char *cmd, uint32_t timeout) {
|
||||
std::string output;
|
||||
const command_result result = this->dce_->at(cmd, output, timeout);
|
||||
const command_result result = this->at_reply_(cmd, timeout);
|
||||
// A reply longer than the buffer kept only its end
|
||||
const char *cut = this->reply_cut_ ? LOG_STR_LITERAL("...") : "";
|
||||
if (result == command_result::OK) {
|
||||
ESP_LOGI(TAG, "AT '%s' -> OK: %s", cmd, output.c_str());
|
||||
ESP_LOGI(TAG, "AT '%s' -> OK: %s%s", cmd, cut, this->reply_);
|
||||
} else {
|
||||
ESP_LOGW(TAG, "AT '%s' -> %s: %s", cmd, LOG_STR_ARG(result_to_str(result)), output.c_str());
|
||||
ESP_LOGW(TAG, "AT '%s' -> %s: %s%s", cmd, LOG_STR_ARG(result_to_str(result)), cut, this->reply_);
|
||||
}
|
||||
return result;
|
||||
}
|
||||
@@ -966,25 +1222,27 @@ SimResult ModemComponent::prepare_sim_() {
|
||||
return SimResult::SIM_RESULT_RETRY; // the save wakes the task
|
||||
}
|
||||
#endif
|
||||
std::string output;
|
||||
if (this->dce_->at("AT+CPIN?", output, COMMAND_TIMEOUT_MS) != command_result::OK) {
|
||||
// The raw reply keeps the +CME ERROR text a missing SIM gives; status below points into it
|
||||
const command_result result = this->at_reply_("AT+CPIN?");
|
||||
if (result == command_result::TIMEOUT) {
|
||||
// The next pass checks with a sync whether the modem or only the SIM interface is silent
|
||||
ESP_LOGD(TAG, "No answer to the SIM status request");
|
||||
this->sim_not_ready_(LOG_STR_LITERAL("no answer to AT+CPIN?"));
|
||||
this->request_resync_();
|
||||
return SimResult::SIM_RESULT_RETRY;
|
||||
}
|
||||
if (result != command_result::OK) {
|
||||
// A SIM that is still starting answers with an error for a second or so
|
||||
ESP_LOGD(TAG, "SIM status not readable yet: %s", output.c_str());
|
||||
this->set_fault_(ModemFault::MODEM_FAULT_SIM_NOT_READY);
|
||||
ESP_LOGD(TAG, "SIM status not readable yet: %s", this->reply_);
|
||||
this->sim_not_ready_(this->reply_);
|
||||
return SimResult::SIM_RESULT_RETRY;
|
||||
}
|
||||
// Anything else, such as an echoed command, is not a status and must not reach the lock checks below
|
||||
const char *found = strstr(output.c_str(), "+CPIN:");
|
||||
if (found != nullptr) {
|
||||
found += sizeof("+CPIN:") - 1;
|
||||
while (*found == ' ') {
|
||||
found++;
|
||||
}
|
||||
}
|
||||
const char *found = sim_status(this->reply_);
|
||||
const StringRef status = found != nullptr ? StringRef(found) : StringRef();
|
||||
if (status.empty()) {
|
||||
ESP_LOGD(TAG, "SIM status not reported yet: %s", output.c_str());
|
||||
this->set_fault_(ModemFault::MODEM_FAULT_SIM_NOT_READY);
|
||||
ESP_LOGD(TAG, "SIM status not reported yet: %s", this->reply_);
|
||||
this->sim_not_ready_(this->reply_);
|
||||
return SimResult::SIM_RESULT_RETRY;
|
||||
}
|
||||
ESP_LOGD(TAG, "SIM: %s", status.c_str());
|
||||
@@ -1012,8 +1270,8 @@ SimResult ModemComponent::prepare_sim_() {
|
||||
this->set_fault_(ModemFault::MODEM_FAULT_SIM_LOCKED);
|
||||
return SimResult::SIM_RESULT_LOCKED;
|
||||
}
|
||||
ESP_LOGW(TAG, "SIM not ready: %s", status.c_str()); // e.g. NOT READY or NOT INSERTED
|
||||
this->set_fault_(ModemFault::MODEM_FAULT_SIM_NOT_READY);
|
||||
ESP_LOGD(TAG, "SIM not ready: %s", status.c_str()); // e.g. NOT READY or NOT INSERTED
|
||||
this->sim_not_ready_(status.c_str());
|
||||
return SimResult::SIM_RESULT_RETRY;
|
||||
}
|
||||
#ifdef USE_MODEM_PIN_CODE
|
||||
|
||||
@@ -62,6 +62,7 @@ enum class ModemFault : uint8_t {
|
||||
MODEM_FAULT_PIN_RECORD,
|
||||
MODEM_FAULT_SETUP,
|
||||
MODEM_FAULT_NOT_ATTACHED,
|
||||
MODEM_FAULT_REGISTRATION_DENIED,
|
||||
MODEM_FAULT_PPP,
|
||||
MODEM_FAULT_TEARDOWN,
|
||||
};
|
||||
@@ -151,6 +152,7 @@ class ModemComponent : public Component {
|
||||
static constexpr uint32_t RETRY_DELAY_MS = 1000;
|
||||
static constexpr size_t URC_LINE_SIZE = 128;
|
||||
static constexpr size_t AT_COMMAND_SIZE = 128;
|
||||
static constexpr size_t REPLY_SIZE = 256; // a longer reply keeps its newest bytes, so its final line counts
|
||||
#ifdef USE_MODEM_POLL
|
||||
static constexpr uint8_t CSQ_UNKNOWN = 99; // AT+CSQ rssi or ber the modem cannot tell
|
||||
static constexpr uint8_t CSQ_RSSI_MAX = 31;
|
||||
@@ -200,8 +202,20 @@ class ModemComponent : public Component {
|
||||
void hand_over_(uint32_t pending);
|
||||
/// Send an AT command from the task and log the result; blocks for up to the timeout
|
||||
esp_modem::command_result at_(const char *cmd, uint32_t timeout = COMMAND_TIMEOUT_MS);
|
||||
/// Send an AT command; reply_ gets the whole reply on one line, including any +CME ERROR text
|
||||
esp_modem::command_result at_reply_(const char *cmd, uint32_t timeout = COMMAND_TIMEOUT_MS);
|
||||
void publish_status_();
|
||||
/// Send the +++ escape with its guard times; true when the modem answered
|
||||
bool escape_data_mode_();
|
||||
/// Overflow a stuck CMUX receiver with flag bytes; true when the modem answers AT afterwards
|
||||
bool flush_cmux_();
|
||||
void request_resync_();
|
||||
int read_stat_(const char *cmd, const char *prefix);
|
||||
int read_registration_();
|
||||
/// Set the UART rate; current_baud_rate_ follows when the UART takes it
|
||||
esp_err_t set_uart_baud_(int baud);
|
||||
void not_responding_(int baud);
|
||||
void sim_not_ready_(const char *reason);
|
||||
void dump_connect_params_();
|
||||
SimResult prepare_sim_();
|
||||
#ifdef USE_MODEM_PIN_CODE
|
||||
@@ -221,6 +235,9 @@ class ModemComponent : public Component {
|
||||
const char *urc_line_{nullptr}; // the line urc_pending_ hands to loop(), inside urc_reader_
|
||||
#endif
|
||||
std::unique_ptr<esp_modem::DCE> dce_;
|
||||
char reply_[REPLY_SIZE + 1]{}; // task only; the last reply, null-terminated
|
||||
size_t reply_len_{0};
|
||||
bool reply_cut_{false}; // the last reply was longer than reply_ and kept only its end
|
||||
std::shared_ptr<esp_modem::DTE> dte_;
|
||||
esp_netif_t *ppp_netif_{nullptr};
|
||||
ESPPreferenceObject pref_;
|
||||
@@ -291,6 +308,9 @@ class ModemComponent : public Component {
|
||||
uint8_t init_at_count_{0};
|
||||
uint8_t uart_port_{UART_NUM_MAX};
|
||||
uint8_t attach_polls_{0};
|
||||
uint8_t idle_polls_{0}; // polls in a row that reported "not searching"
|
||||
uint8_t unknown_polls_{0}; // polls in a row with no usable registration state
|
||||
uint8_t sim_polls_{0}; // failed SIM status reads in this attempt
|
||||
#ifdef USE_MODEM_POLL
|
||||
uint8_t poll_rssi_{CSQ_UNKNOWN}; // raw AT+CSQ answers
|
||||
uint8_t poll_ber_{CSQ_UNKNOWN};
|
||||
@@ -298,7 +318,9 @@ class ModemComponent : public Component {
|
||||
#endif
|
||||
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 answered_{false}; // the modem synced since boot, so its wiring and power are fine
|
||||
bool resync_{false}; // a command timed out, 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
|
||||
|
||||
@@ -323,12 +323,17 @@ bool MQTTClientComponent::can_proceed() {
|
||||
}
|
||||
|
||||
void MQTTClientComponent::start_dnslookup_() {
|
||||
this->status_set_warning();
|
||||
// No lookup can start before the network is up: there is no DNS server to ask yet.
|
||||
// Stay disconnected, and loop() calls this again.
|
||||
if (!network::is_connected())
|
||||
return;
|
||||
|
||||
for (auto &subscription : this->subscriptions_) {
|
||||
subscription.subscribed = false;
|
||||
subscription.resubscribe_timeout = 0;
|
||||
}
|
||||
|
||||
this->status_set_warning();
|
||||
this->dns_resolve_error_ = false;
|
||||
this->dns_resolved_ = false;
|
||||
ip_addr_t addr;
|
||||
@@ -358,9 +363,13 @@ void MQTTClientComponent::start_dnslookup_() {
|
||||
}
|
||||
default:
|
||||
case ERR_ARG: {
|
||||
// error
|
||||
// The lookup never started, so there is no answer to wait for; loop() tries again in 5 seconds
|
||||
ESP_LOGW(TAG, "Error resolving broker IP address: %d", err);
|
||||
break;
|
||||
this->state_ = MQTT_CLIENT_DISCONNECTED;
|
||||
this->disconnect_reason_ = MQTTClientDisconnectReason::DNS_RESOLVE_ERROR;
|
||||
this->on_disconnect_.call(MQTTClientDisconnectReason::DNS_RESOLVE_ERROR);
|
||||
this->connect_begin_ = millis();
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -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:
|
||||
|
||||
@@ -2,10 +2,6 @@
|
||||
#include "esphome/core/log.h"
|
||||
#include "esphome/core/hal.h"
|
||||
|
||||
#ifdef USE_ESP8266
|
||||
#include <Wire.h>
|
||||
#endif
|
||||
|
||||
namespace esphome::scd30 {
|
||||
|
||||
ESPHOME_LOG_TAG(TAG, "scd30");
|
||||
@@ -27,10 +23,6 @@ static const uint16_t SCD30_CMD_TEMPERATURE_OFFSET = 0x5403;
|
||||
static const uint16_t SCD30_CMD_SOFT_RESET = 0xD304;
|
||||
|
||||
void SCD30Component::setup() {
|
||||
#ifdef USE_ESP8266
|
||||
Wire.setClockStretchLimit(150000);
|
||||
#endif
|
||||
|
||||
/// Firmware version identification
|
||||
uint16_t raw_firmware_version[3];
|
||||
if (!this->get_register(SCD30_CMD_GET_FIRMWARE_VERSION, raw_firmware_version, 3)) {
|
||||
|
||||
@@ -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 {
|
||||
|
||||
@@ -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)
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -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{};
|
||||
|
||||
@@ -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
@@ -4,7 +4,7 @@ from enum import Enum
|
||||
|
||||
from esphome.enum import StrEnum
|
||||
|
||||
__version__ = "2026.11.0b5"
|
||||
__version__ = "2026.11.0b7"
|
||||
|
||||
ALLOWED_NAME_CHARS = "abcdefghijklmnopqrstuvwxyz0123456789-_"
|
||||
VALID_SUBSTITUTIONS_CHARACTERS = (
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -846,7 +846,9 @@ def convert_visual_temperatures(
|
||||
that needs ``full_config`` is left for final validation, which runs this again with it."""
|
||||
if (unit := entity_temperature_unit(config, full_config)) is None:
|
||||
return config
|
||||
visual = config[CONF_VISUAL]
|
||||
# A platform entry that only holds nested entities has no visual section of its own
|
||||
if (visual := config.get(CONF_VISUAL)) is None:
|
||||
return config
|
||||
convert_temperature_keys(visual, (CONF_MIN_TEMPERATURE, CONF_MAX_TEMPERATURE), unit)
|
||||
section = visual if delta_section is None else visual.get(delta_section)
|
||||
if section is not None:
|
||||
|
||||
@@ -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)
|
||||
|
||||
@@ -222,6 +222,20 @@ def test_a_resolved_unit_waits_for_the_full_config() -> None:
|
||||
assert config[CONF_VISUAL][CONF_MIN_TEMPERATURE] == pytest.approx(59.0)
|
||||
|
||||
|
||||
def test_final_validate_skips_a_platform_that_only_holds_nested_climates() -> None:
|
||||
"""A hub platform may put each of its climates under a key of one platform entry."""
|
||||
nested = climate_schema(climate_ns.class_("NestedClimate", Climate))
|
||||
platform_schema = cv.Schema({cv.Optional("zone_1"): nested})
|
||||
config = platform_schema(
|
||||
{"zone_1": {CONF_NAME: "Zone 1", CONF_VISUAL: {CONF_MIN_TEMPERATURE: "50°F"}}}
|
||||
)
|
||||
# The platform entry itself is not a climate and has no visual section
|
||||
assert CONF_VISUAL not in config
|
||||
_final_validate([config])
|
||||
# The nested climate was converted by its own schema
|
||||
assert config["zone_1"][CONF_VISUAL][CONF_MIN_TEMPERATURE] == pytest.approx(10.0)
|
||||
|
||||
|
||||
@pytest.mark.parametrize(
|
||||
("use_fahrenheit", "cn105_lines"),
|
||||
[
|
||||
|
||||
@@ -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
|
||||
@@ -0,0 +1,6 @@
|
||||
from tests.testing_helpers import ComponentManifestOverride
|
||||
|
||||
|
||||
def override_manifest(manifest: ComponentManifestOverride) -> None:
|
||||
# touchscreen.h includes display.h
|
||||
manifest.dependencies = manifest.dependencies + ["display"]
|
||||
@@ -0,0 +1,42 @@
|
||||
#pragma once
|
||||
|
||||
#include <array>
|
||||
|
||||
#include <gtest/gtest.h>
|
||||
|
||||
#include "esphome/components/cst816/touchscreen/cst816_touchscreen.h"
|
||||
|
||||
namespace esphome::cst816::testing {
|
||||
|
||||
static constexpr uint8_t ADDRESS = 0x15;
|
||||
// The driver reads REG_STATUS onwards in one transfer: status, gesture, touch count, then two points.
|
||||
static constexpr size_t FRAME_SIZE = 13;
|
||||
|
||||
/// Serves one fixed status frame.
|
||||
class FrameBus final : public i2c::I2CBus {
|
||||
public:
|
||||
explicit FrameBus(std::array<uint8_t, FRAME_SIZE> frame) : frame_(frame) {}
|
||||
|
||||
i2c::ErrorCode write_readv(uint8_t address, const uint8_t *write_buffer, size_t write_count, uint8_t *read_buffer,
|
||||
size_t read_count) override {
|
||||
EXPECT_EQ(address, ADDRESS);
|
||||
for (size_t i = 0; i < read_count; i++)
|
||||
read_buffer[i] = i < FRAME_SIZE ? this->frame_[i] : 0;
|
||||
return i2c::ERROR_OK;
|
||||
}
|
||||
|
||||
protected:
|
||||
std::array<uint8_t, FRAME_SIZE> frame_;
|
||||
};
|
||||
|
||||
inline std::array<uint8_t, FRAME_SIZE> make_frame(uint8_t touch_num, uint16_t x = 0, uint16_t y = 0) {
|
||||
std::array<uint8_t, FRAME_SIZE> frame{};
|
||||
frame[REG_TOUCH_NUM] = touch_num;
|
||||
frame[REG_XPOS_HIGH] = (x >> 8) & 0x0F;
|
||||
frame[REG_XPOS_LOW] = x & 0xFF;
|
||||
frame[REG_YPOS_HIGH] = (y >> 8) & 0x0F;
|
||||
frame[REG_YPOS_LOW] = y & 0xFF;
|
||||
return frame;
|
||||
}
|
||||
|
||||
} // namespace esphome::cst816::testing
|
||||
@@ -0,0 +1,88 @@
|
||||
#include <gtest/gtest.h>
|
||||
|
||||
#include <memory>
|
||||
|
||||
#include "../common.h"
|
||||
|
||||
namespace esphome::cst816::testing {
|
||||
|
||||
class CST816TouchscreenTest : public ::testing::Test {
|
||||
protected:
|
||||
void feed_(const std::array<uint8_t, FRAME_SIZE> &frame) {
|
||||
this->bus_ = std::make_unique<FrameBus>(frame);
|
||||
this->touchscreen_.set_i2c_bus(this->bus_.get());
|
||||
this->touchscreen_.set_i2c_address(ADDRESS);
|
||||
this->touchscreen_.update_touches();
|
||||
}
|
||||
|
||||
std::unique_ptr<FrameBus> bus_;
|
||||
CST816Touchscreen touchscreen_;
|
||||
};
|
||||
|
||||
// An idle frame reports 0xFF in the touch count byte and 0xFFF coordinates. Masking 0xFF to the
|
||||
// low two bits turns that into three touches at the maximum position, a phantom tap.
|
||||
TEST_F(CST816TouchscreenTest, IdleAllOnesFrameIsIgnored) {
|
||||
this->feed_(make_frame(0xFF, 0x0FFF, 0x0FFF));
|
||||
EXPECT_TRUE(this->touchscreen_.get_touches().empty());
|
||||
}
|
||||
|
||||
TEST_F(CST816TouchscreenTest, ZeroTouchCountIsIgnored) {
|
||||
this->feed_(make_frame(0));
|
||||
EXPECT_TRUE(this->touchscreen_.get_touches().empty());
|
||||
}
|
||||
|
||||
// Three is the boundary the guard rejects: the old mask turned a count of three into three
|
||||
// touches, so this is the value whose behaviour changes.
|
||||
TEST_F(CST816TouchscreenTest, TouchCountThreeIsIgnored) {
|
||||
this->feed_(make_frame(3, 100, 200));
|
||||
EXPECT_TRUE(this->touchscreen_.get_touches().empty());
|
||||
}
|
||||
|
||||
// The count is the whole low nibble, so 0x05 must not be masked down to one touch by a 0x03 mask.
|
||||
TEST_F(CST816TouchscreenTest, TouchCountFiveIsIgnored) {
|
||||
this->feed_(make_frame(0x05, 100, 200));
|
||||
EXPECT_TRUE(this->touchscreen_.get_touches().empty());
|
||||
}
|
||||
|
||||
TEST_F(CST816TouchscreenTest, SingleTouchIsReported) {
|
||||
this->feed_(make_frame(1, 300, 100));
|
||||
auto touches = this->touchscreen_.get_touches();
|
||||
ASSERT_EQ(touches.size(), 1u);
|
||||
EXPECT_EQ(touches[0].x_raw, 300);
|
||||
EXPECT_EQ(touches[0].y_raw, 100);
|
||||
}
|
||||
|
||||
// CST826/CST836 can report two points; the driver only consumes the first one.
|
||||
TEST_F(CST816TouchscreenTest, TwoPointFrameReportsFirstPoint) {
|
||||
this->feed_(make_frame(2, 300, 100));
|
||||
auto touches = this->touchscreen_.get_touches();
|
||||
ASSERT_EQ(touches.size(), 1u);
|
||||
EXPECT_EQ(touches[0].x_raw, 300);
|
||||
EXPECT_EQ(touches[0].y_raw, 100);
|
||||
}
|
||||
|
||||
TEST_F(CST816TouchscreenTest, SentinelPositionIsIgnored) {
|
||||
this->feed_(make_frame(1, 0x0FFF, 100));
|
||||
EXPECT_TRUE(this->touchscreen_.get_touches().empty());
|
||||
}
|
||||
|
||||
// The sentinel is checked on both axes; a valid X with Y=0xFFF is still an idle reading.
|
||||
TEST_F(CST816TouchscreenTest, SentinelYPositionIsIgnored) {
|
||||
this->feed_(make_frame(1, 100, 0x0FFF));
|
||||
EXPECT_TRUE(this->touchscreen_.get_touches().empty());
|
||||
}
|
||||
|
||||
// x_raw_max_/y_raw_max_ are calibration endpoints, not validity limits: the base class applies them
|
||||
// after swapping axes and clamps readings outside them. A touch beyond the calibrated maximum is
|
||||
// still a real touch and must reach the base class.
|
||||
TEST_F(CST816TouchscreenTest, CalibrationBoundsDoNotDropTouches) {
|
||||
this->touchscreen_.set_calibration(0, 240, 0, 320);
|
||||
this->touchscreen_.set_swap_xy(true);
|
||||
this->feed_(make_frame(1, 300, 100));
|
||||
auto touches = this->touchscreen_.get_touches();
|
||||
ASSERT_EQ(touches.size(), 1u);
|
||||
EXPECT_EQ(touches[0].x_raw, 100);
|
||||
EXPECT_EQ(touches[0].y_raw, 300);
|
||||
}
|
||||
|
||||
} // namespace esphome::cst816::testing
|
||||
@@ -0,0 +1,3 @@
|
||||
packages:
|
||||
i2c: !include ../../test_build_components/common/i2c/esp32-ard.yaml
|
||||
mlx90393: !include common.yaml
|
||||
@@ -0,0 +1,11 @@
|
||||
import esphome.config_validation as cv
|
||||
from esphome.const import CONF_USE_ADDRESS
|
||||
from tests.testing_helpers import ComponentManifestOverride
|
||||
|
||||
|
||||
def override_manifest(manifest: ComponentManifestOverride) -> None:
|
||||
# Only the reply parsers are tested; the build still needs the address a modem config provides.
|
||||
manifest.dependencies = []
|
||||
manifest.config_schema = cv.Schema(
|
||||
{cv.Optional(CONF_USE_ADDRESS, default="test.local"): cv.string_strict}
|
||||
)
|
||||
@@ -0,0 +1,211 @@
|
||||
#include <gtest/gtest.h>
|
||||
|
||||
#include <cstring>
|
||||
#include <string>
|
||||
|
||||
#include "esphome/components/modem/at_reply.h"
|
||||
|
||||
namespace esphome::modem::testing {
|
||||
|
||||
// Runs a raw reply through flatten_reply() as at_reply_() does
|
||||
static std::string flat(const char *raw) {
|
||||
char buf[256];
|
||||
const size_t len = strlen(raw);
|
||||
memcpy(buf, raw, len + 1);
|
||||
return std::string(buf, flatten_reply(buf, len));
|
||||
}
|
||||
|
||||
static ReplyEnd end_of(const char *reply, bool skip_first = false) {
|
||||
return reply_end(reply, strlen(reply), skip_first);
|
||||
}
|
||||
|
||||
TEST(ModemReplyEnd, OnlyCompleteFinalLinesCount) {
|
||||
EXPECT_EQ(end_of("\r\n+CSQ: 20,99\r\n\r\nOK\r\n"), ReplyEnd::REPLY_END_OK);
|
||||
EXPECT_EQ(end_of("\r\nERROR\r\n"), ReplyEnd::REPLY_END_ERROR);
|
||||
EXPECT_EQ(end_of("\r\n+CME ERROR: SIM not inserted\r\n"), ReplyEnd::REPLY_END_ERROR);
|
||||
EXPECT_EQ(end_of("\r\n+CMS ERROR: 500\r\n"), ReplyEnd::REPLY_END_ERROR);
|
||||
// ERROR inside a payload line is not a result
|
||||
EXPECT_EQ(end_of("\r\n+CEER: PROTOCOL_ERROR\r\n"), ReplyEnd::REPLY_END_NONE);
|
||||
EXPECT_EQ(end_of("\r\n+CEER: PROTOCOL_ERROR\r\n\r\nOK\r\n"), ReplyEnd::REPLY_END_OK);
|
||||
// A line that has not ended yet
|
||||
EXPECT_EQ(end_of("\r\nOK"), ReplyEnd::REPLY_END_NONE);
|
||||
EXPECT_EQ(end_of("\r\n+CME ERROR: 10"), ReplyEnd::REPLY_END_NONE);
|
||||
}
|
||||
|
||||
TEST(ModemReplyEnd, ErrorWithoutItsReason) {
|
||||
// esp_modem drops the CMUX frame ": SIM not inserted" because it holds no line end
|
||||
char buf[64];
|
||||
size_t len = 0;
|
||||
collect_reply(buf, sizeof(buf), len, "\r\n+CME ERROR", 12, false);
|
||||
EXPECT_EQ(reply_end(buf, len, false), ReplyEnd::REPLY_END_NONE);
|
||||
collect_reply(buf, sizeof(buf), len, "\r\n", 2, false);
|
||||
EXPECT_EQ(reply_end(buf, len, false), ReplyEnd::REPLY_END_ERROR);
|
||||
EXPECT_EQ(end_of("\r\n+CMS ERROR\r\n"), ReplyEnd::REPLY_END_ERROR);
|
||||
}
|
||||
|
||||
TEST(ModemReplyEnd, CutFirstLineIsSkipped) {
|
||||
// The tail of "...OK" from a longer line must not count as a result
|
||||
EXPECT_EQ(end_of("OK\r\nmore text\r\n", true), ReplyEnd::REPLY_END_NONE);
|
||||
EXPECT_EQ(end_of("OK\r\nmore text\r\n", false), ReplyEnd::REPLY_END_OK);
|
||||
EXPECT_EQ(end_of("tail\r\nOK\r\n", true), ReplyEnd::REPLY_END_OK);
|
||||
}
|
||||
|
||||
TEST(ModemCollectReply, UartReplyComesBackWhole) {
|
||||
char buf[9];
|
||||
size_t len = 0;
|
||||
EXPECT_FALSE(collect_reply(buf, sizeof(buf), len, "abc", 3, true));
|
||||
EXPECT_STREQ(buf, "abc");
|
||||
EXPECT_FALSE(collect_reply(buf, sizeof(buf), len, "abcdef", 6, true));
|
||||
EXPECT_STREQ(buf, "abcdef");
|
||||
// Data longer than the buffer keeps its tail
|
||||
EXPECT_TRUE(collect_reply(buf, sizeof(buf), len, "0123456789", 10, true));
|
||||
EXPECT_STREQ(buf, "23456789");
|
||||
}
|
||||
|
||||
TEST(ModemCollectReply, GatheredCmuxFramesAreNotRepeated) {
|
||||
// With esp_modem's inflatable buffer, each CMUX frame comes with the ones before it
|
||||
char buf[64];
|
||||
size_t len = 0;
|
||||
const char *first = "\r\n+CSQ: 20,99\r\n";
|
||||
const char *both = "\r\n+CSQ: 20,99\r\n\r\nOK\r\n";
|
||||
collect_reply(buf, sizeof(buf), len, first, strlen(first), true);
|
||||
collect_reply(buf, sizeof(buf), len, both, strlen(both), true);
|
||||
EXPECT_STREQ(buf, both);
|
||||
EXPECT_EQ(reply_end(buf, len, false), ReplyEnd::REPLY_END_OK);
|
||||
}
|
||||
|
||||
TEST(ModemCollectReply, ExactlyFullIsNotCut) {
|
||||
char buf[9];
|
||||
size_t len = 0;
|
||||
EXPECT_FALSE(collect_reply(buf, sizeof(buf), len, "OK\r\nabcd", 8, true));
|
||||
EXPECT_STREQ(buf, "OK\r\nabcd");
|
||||
len = 0;
|
||||
EXPECT_FALSE(collect_reply(buf, sizeof(buf), len, "OK\r\nabcd", 8, false));
|
||||
EXPECT_EQ(reply_end(buf, len, false), ReplyEnd::REPLY_END_OK);
|
||||
// Filling up by appending is not a cut either, one more byte is
|
||||
len = 0;
|
||||
EXPECT_FALSE(collect_reply(buf, sizeof(buf), len, "OK\r\n", 4, false));
|
||||
EXPECT_FALSE(collect_reply(buf, sizeof(buf), len, "abcd", 4, false));
|
||||
EXPECT_TRUE(collect_reply(buf, sizeof(buf), len, "e", 1, false));
|
||||
EXPECT_STREQ(buf, "K\r\nabcde");
|
||||
}
|
||||
|
||||
TEST(ModemCollectReply, CmuxFramesAreAppended) {
|
||||
char buf[9];
|
||||
size_t len = 0;
|
||||
EXPECT_FALSE(collect_reply(buf, sizeof(buf), len, "abc", 3, false));
|
||||
// A frame that starts like the text so far is still new data
|
||||
EXPECT_FALSE(collect_reply(buf, sizeof(buf), len, "abcd", 4, false));
|
||||
EXPECT_STREQ(buf, "abcabcd");
|
||||
// The oldest bytes go once the buffer is full
|
||||
EXPECT_TRUE(collect_reply(buf, sizeof(buf), len, "XY", 2, false));
|
||||
EXPECT_STREQ(buf, "bcabcdXY");
|
||||
EXPECT_EQ(len, 8u);
|
||||
EXPECT_TRUE(collect_reply(buf, sizeof(buf), len, "0123456789", 10, false));
|
||||
EXPECT_STREQ(buf, "23456789");
|
||||
}
|
||||
|
||||
TEST(ModemFlattenReply, OneLineWithoutFinalOk) {
|
||||
EXPECT_EQ(flat("\r\n+CSQ: 20,99\r\n\r\nOK\r\n"), "+CSQ: 20,99");
|
||||
EXPECT_EQ(flat("\r\nOK\r\n"), "");
|
||||
EXPECT_EQ(flat("\r\n+CME ERROR: SIM not inserted\r\n"), "+CME ERROR: SIM not inserted");
|
||||
EXPECT_EQ(flat("\r\n+COPS: 0,0,\"NOK\"\r\n"), "+COPS: 0,0,\"NOK\"");
|
||||
// A line ending in OK is not the final result
|
||||
EXPECT_EQ(flat("\r\n+CSPN: NOK\r\n"), "+CSPN: NOK");
|
||||
EXPECT_EQ(flat(""), "");
|
||||
}
|
||||
|
||||
TEST(ModemRegistration, QueryAnswerNotNotification) {
|
||||
// A one-field URC before the query answer is skipped
|
||||
EXPECT_EQ(registration_from_reply("+CEREG: 2 +CEREG: 0,1", "+CEREG:"), 1);
|
||||
EXPECT_EQ(registration_from_reply("+CEREG: 2,5,\"1A2B\",\"01A2B3C4\",7", "+CEREG:"), 5);
|
||||
EXPECT_EQ(registration_from_reply("+CREG: 0,3", "+CREG:"), 3);
|
||||
EXPECT_EQ(registration_from_reply("+CEREG: 1", "+CEREG:"), REGISTRATION_UNKNOWN);
|
||||
EXPECT_EQ(registration_from_reply("+CREG: 0,1", "+CEREG:"), REGISTRATION_UNKNOWN);
|
||||
EXPECT_EQ(registration_from_reply("", "+CEREG:"), REGISTRATION_UNKNOWN);
|
||||
}
|
||||
|
||||
TEST(ModemRegistration, OtherRegisteredStates) {
|
||||
EXPECT_TRUE(is_registered(REGISTRATION_HOME));
|
||||
EXPECT_TRUE(is_registered(REGISTRATION_ROAMING));
|
||||
EXPECT_TRUE(is_registered(REGISTRATION_SMS_ONLY_HOME));
|
||||
EXPECT_TRUE(is_registered(REGISTRATION_SMS_ONLY_ROAMING));
|
||||
EXPECT_TRUE(is_registered(REGISTRATION_CSFB_NOT_PREFERRED_HOME));
|
||||
EXPECT_TRUE(is_registered(REGISTRATION_CSFB_NOT_PREFERRED_ROAMING));
|
||||
EXPECT_FALSE(is_registered(REGISTRATION_NOT_SEARCHING));
|
||||
EXPECT_FALSE(is_registered(REGISTRATION_SEARCHING));
|
||||
EXPECT_FALSE(is_registered(REGISTRATION_DENIED));
|
||||
EXPECT_FALSE(is_registered(REGISTRATION_REPORTED_UNKNOWN));
|
||||
EXPECT_FALSE(is_registered(REGISTRATION_EMERGENCY_ONLY));
|
||||
EXPECT_FALSE(is_registered(11));
|
||||
EXPECT_FALSE(is_registered(REGISTRATION_UNKNOWN));
|
||||
}
|
||||
|
||||
TEST(ModemMergeRegistration, RegisteredOnEitherSideWins) {
|
||||
EXPECT_EQ(merge_registration(REGISTRATION_HOME, REGISTRATION_DENIED), REGISTRATION_HOME);
|
||||
EXPECT_EQ(merge_registration(REGISTRATION_DENIED, REGISTRATION_ROAMING), REGISTRATION_ROAMING);
|
||||
EXPECT_EQ(merge_registration(REGISTRATION_NO_ANSWER, REGISTRATION_HOME), REGISTRATION_HOME);
|
||||
EXPECT_EQ(merge_registration(REGISTRATION_UNSUPPORTED, REGISTRATION_ROAMING), REGISTRATION_ROAMING);
|
||||
EXPECT_EQ(merge_registration(REGISTRATION_UNSUPPORTED, REGISTRATION_CSFB_NOT_PREFERRED_ROAMING),
|
||||
REGISTRATION_CSFB_NOT_PREFERRED_ROAMING);
|
||||
EXPECT_EQ(merge_registration(REGISTRATION_SEARCHING, REGISTRATION_CSFB_NOT_PREFERRED_HOME),
|
||||
REGISTRATION_CSFB_NOT_PREFERRED_HOME);
|
||||
EXPECT_EQ(merge_registration(REGISTRATION_SMS_ONLY_HOME, REGISTRATION_DENIED), REGISTRATION_SMS_ONLY_HOME);
|
||||
// Emergency only is no registration, so a searching query still decides
|
||||
EXPECT_EQ(merge_registration(REGISTRATION_EMERGENCY_ONLY, REGISTRATION_SEARCHING), REGISTRATION_SEARCHING);
|
||||
}
|
||||
|
||||
TEST(ModemMergeRegistration, SearchingBeatsDenied) {
|
||||
EXPECT_EQ(merge_registration(REGISTRATION_SEARCHING, REGISTRATION_DENIED), REGISTRATION_SEARCHING);
|
||||
EXPECT_EQ(merge_registration(REGISTRATION_DENIED, REGISTRATION_SEARCHING), REGISTRATION_SEARCHING);
|
||||
}
|
||||
|
||||
TEST(ModemMergeRegistration, DefiniteAnswerBeatsTimeout) {
|
||||
EXPECT_EQ(merge_registration(REGISTRATION_DENIED, REGISTRATION_NO_ANSWER), REGISTRATION_DENIED);
|
||||
EXPECT_EQ(merge_registration(REGISTRATION_NO_ANSWER, REGISTRATION_SEARCHING), REGISTRATION_SEARCHING);
|
||||
EXPECT_EQ(merge_registration(REGISTRATION_NO_ANSWER, REGISTRATION_NO_ANSWER), REGISTRATION_NO_ANSWER);
|
||||
// Any reported <stat> outweighs a timeout, so the fallbacks still count it
|
||||
EXPECT_EQ(merge_registration(REGISTRATION_NOT_SEARCHING, REGISTRATION_NO_ANSWER), REGISTRATION_NOT_SEARCHING);
|
||||
EXPECT_EQ(merge_registration(REGISTRATION_NO_ANSWER, REGISTRATION_NOT_SEARCHING), REGISTRATION_NOT_SEARCHING);
|
||||
EXPECT_EQ(merge_registration(REGISTRATION_REPORTED_UNKNOWN, REGISTRATION_NO_ANSWER), REGISTRATION_REPORTED_UNKNOWN);
|
||||
EXPECT_EQ(merge_registration(REGISTRATION_NO_ANSWER, REGISTRATION_REPORTED_UNKNOWN), REGISTRATION_REPORTED_UNKNOWN);
|
||||
// Only a timeout and no <stat> at all leave the state open
|
||||
EXPECT_EQ(merge_registration(REGISTRATION_UNSUPPORTED, REGISTRATION_NO_ANSWER), REGISTRATION_NO_ANSWER);
|
||||
EXPECT_EQ(merge_registration(REGISTRATION_NO_ANSWER, REGISTRATION_UNKNOWN), REGISTRATION_NO_ANSWER);
|
||||
}
|
||||
|
||||
TEST(ModemMergeRegistration, UnsupportedOnlyWhenBothAre) {
|
||||
EXPECT_EQ(merge_registration(REGISTRATION_UNSUPPORTED, REGISTRATION_UNSUPPORTED), REGISTRATION_UNSUPPORTED);
|
||||
EXPECT_EQ(merge_registration(REGISTRATION_UNSUPPORTED, REGISTRATION_NOT_SEARCHING), REGISTRATION_NOT_SEARCHING);
|
||||
EXPECT_EQ(merge_registration(REGISTRATION_UNKNOWN, REGISTRATION_UNSUPPORTED), REGISTRATION_UNKNOWN);
|
||||
}
|
||||
|
||||
TEST(ModemMergeRegistration, UnknownFallback) {
|
||||
// The LTE value goes first, then the 2G and 3G one, then unknown
|
||||
EXPECT_EQ(merge_registration(REGISTRATION_NOT_SEARCHING, REGISTRATION_REPORTED_UNKNOWN), REGISTRATION_NOT_SEARCHING);
|
||||
EXPECT_EQ(merge_registration(REGISTRATION_UNKNOWN, REGISTRATION_REPORTED_UNKNOWN), REGISTRATION_REPORTED_UNKNOWN);
|
||||
EXPECT_EQ(merge_registration(REGISTRATION_UNKNOWN, REGISTRATION_UNKNOWN), REGISTRATION_UNKNOWN);
|
||||
}
|
||||
|
||||
TEST(ModemUnsupported, OnlyWhenTheErrorSaysSo) {
|
||||
EXPECT_TRUE(reply_names_unsupported("+CME ERROR: operation not supported"));
|
||||
EXPECT_TRUE(reply_names_unsupported("+CME ERROR: Operation Not Supported"));
|
||||
EXPECT_TRUE(reply_names_unsupported("+CME ERROR: 4"));
|
||||
EXPECT_FALSE(reply_names_unsupported("+CME ERROR: 40"));
|
||||
EXPECT_FALSE(reply_names_unsupported("+CME ERROR: SIM busy"));
|
||||
EXPECT_FALSE(reply_names_unsupported("ERROR"));
|
||||
EXPECT_FALSE(reply_names_unsupported(""));
|
||||
}
|
||||
|
||||
TEST(ModemSimStatus, CutBeforeTheNextLine) {
|
||||
char ready[] = "+CPIN: READY";
|
||||
EXPECT_STREQ(sim_status(ready), "READY");
|
||||
char with_urc[] = "+CPIN: SIM PIN +CGEV: ME PDN DEACT 1";
|
||||
EXPECT_STREQ(sim_status(with_urc), "SIM PIN");
|
||||
char echoed[] = "AT+CPIN?";
|
||||
EXPECT_EQ(sim_status(echoed), nullptr);
|
||||
char empty[] = "+CPIN:";
|
||||
EXPECT_STREQ(sim_status(empty), "");
|
||||
}
|
||||
|
||||
} // namespace esphome::modem::testing
|
||||
@@ -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"
|
||||
+37
@@ -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, ¬_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
|
||||
+10
@@ -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"
|
||||
)
|
||||
@@ -1550,6 +1550,15 @@ def test_convert_visual_temperatures(
|
||||
convert_visual_temperatures({"visual": {}}, delta_keys, delta_section)
|
||||
|
||||
|
||||
def test_convert_visual_temperatures_without_a_visual_section() -> None:
|
||||
"""A platform entry that only holds nested entities has no visual section and is left alone."""
|
||||
nested = {"visual": {"min_temperature": (50.0, "°F")}}
|
||||
config = {"zone_1": nested}
|
||||
|
||||
assert convert_visual_temperatures(config, ("step",)) is config
|
||||
assert nested["visual"]["min_temperature"] == (50.0, "°F")
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_setup_entity_integrations_registers_mqtt_and_web_server(
|
||||
monkeypatch: pytest.MonkeyPatch, setup_test_environment: list[str]
|
||||
|
||||
Reference in new issue
Block a user