Compare commits

..
Author SHA1 Message Date
J. Nick Koston 38e4cebcb6 Merge remote-tracking branch 'origin/web-server-sse-listing-per-session' into web-server-event-source-shared
# Conflicts:
#	esphome/components/web_server_idf/web_server_idf.cpp
2026-11-18 15:17:31 -06:00
J. Nick Koston 7bc38c63b3 Merge remote-tracking branch 'origin/web-server-idf-stream-response' into web-server-sse-listing-per-session 2026-11-18 15:16:31 -06:00
J. Nick Koston 8809fccb90 Merge remote-tracking branch 'origin/dev' into web-server-idf-stream-response
# Conflicts:
#	esphome/components/web_server_idf/web_server_idf.cpp
2026-11-18 15:11:42 -06:00
392ed7e7b3 [wifi][ethernet] Support IPv6-only networks with enable_ipv4: false (#19750)
Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
Co-authored-by: J. Nick Koston <nick@home-assistant.io>
Co-authored-by: J. Nick Koston <nick@koston.org>
2026-11-18 14:58:25 -06:00
aa9f13bb4d [web_server_idf] Add HTTPS support for the shared web server (#22185)
Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
Co-authored-by: J. Nick Koston <nick@koston.org>
Co-authored-by: J. Nick Koston <nick@home-assistant.io>
2026-11-18 14:58:13 -06:00
98892432e7 [core][esp32] Allow watchdog_timeout: 0 to disable the ESP32 task watchdog (#16407)
Co-authored-by: Claude Sonnet 4.6 <noreply@anthropic.com>
Co-authored-by: J. Nick Koston <nick@koston.org>
Co-authored-by: J. Nick Koston <nick@home-assistant.io>
2026-11-18 14:56:26 -06:00
02351d0108 [core] Add friendly_name_separator (#22163)
Co-authored-by: J. Nick Koston <nick@home-assistant.io>
Co-authored-by: J. Nick Koston <nick@koston.org>
2026-11-18 14:50:47 -06:00
dependabot[bot] d9cefc9d52 Bump pylint from 4.1.3 to 4.1.4 (#22890)
Signed-off-by: dependabot[bot] <support@github.com>
Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com>
2026-11-18 14:33:13 -06:00
Craig Andrews 155607fed6 [epaper_spi] Add Waveshare 7.5" HD (B) 3-color e-paper (#21492) 2026-11-19 07:08:40 +11:00
J. Nick Koston 9d6b50e947 Merge branch 'web-server-sse-listing-per-session' into web-server-event-source-shared 2026-11-17 09:19:03 -06:00
J. Nick Koston e490785ad3 Merge branch 'web-server-idf-stream-response' into web-server-sse-listing-per-session 2026-11-17 09:19:02 -06:00
J. Nick Koston 7f1ba24ab1 Merge branch 'dev' into web-server-idf-stream-response 2026-11-17 09:18:57 -06:00
J. Nick Koston 205318ee1a Merge remote-tracking branch 'origin/web-server-sse-listing-per-session' into web-server-event-source-shared 2026-11-15 19:04:55 -06:00
J. Nick Koston 48d8df449a Merge remote-tracking branch 'origin/web-server-idf-stream-response' into web-server-sse-listing-per-session 2026-11-15 19:04:52 -06:00
J. Nick Koston 383ac1b41a Merge remote-tracking branch 'origin/dev' into web-server-idf-stream-response 2026-11-15 19:04:42 -06:00
J. Nick Koston 0ac2299482 Merge remote-tracking branch 'origin/web-server-sse-listing-per-session' into HEAD 2026-11-14 08:40:18 -06:00
J. Nick Koston b58038af63 Merge remote-tracking branch 'origin/web-server-idf-stream-response' into HEAD 2026-11-14 08:39:50 -06:00
J. Nick Koston c741de7a47 Merge remote-tracking branch 'origin/dev' into HEAD 2026-11-14 08:38:08 -06:00
J. Nick Koston 8a5e07e29f [web_server] Keep the server out of host builds, which only need sse_chunk.cpp for unit tests
tests/components/web_server now pulls web_server into the --all host
build, where another test defines USE_WEBSERVER.
2026-11-14 00:19:08 -06:00
J. Nick Koston f7b52be670 Merge remote-tracking branch 'origin/web-server-sse-listing-per-session' into web-server-event-source-shared 2026-11-13 23:15:51 -06:00
J. Nick Koston d5317669c9 Merge remote-tracking branch 'origin/web-server-idf-stream-response' into web-server-sse-listing-per-session 2026-11-13 23:15:42 -06:00
J. Nick Koston 5d0bd88bc6 Merge remote-tracking branch 'origin/dev' into web-server-idf-stream-response 2026-11-13 23:13:08 -06:00
pre-commit-ci-lite[bot] b714d8f69b apply automatic formatting fixes 2026-11-14 04:33:03 +00:00
J. Nick Koston ecabdbb24b [web_server] Keep the shared /events layer free of IDF-only types
The gather type comes from stream_response.h, the default header count is
defined on every platform, the session takes a non-const request, and the
fd in log lines goes through log_id(). Also drops a dead forward declaration
and an unused alias, and narrows the source filter.
2026-11-13 22:28:19 -06:00
J. Nick Koston ffbc4148fb [web_server] Move the ESP32 /events layer to web_server/event_source
The event source and its sessions move out of web_server_idf into
web_server/event_source.h/.cpp, sse_chunk moves to web_server and
stream_head.h to web_server_base, so the ESPAsyncWebServer platforms can
share them later. web_server_base/stream_response.h picks the stream
transport. No behaviour change; off ESP32 the build is unchanged.
2026-11-13 22:16:53 -06:00
J. Nick Koston 2169167908 [web_server] Name the iterator's session member session_ 2026-11-13 22:00:50 -06:00
J. Nick Koston 900574c03a [web_server_idf] List entities to the new /events session only
The listing iterator held the whole event source, so each new client's
detail_all listing went to every open session. Hold the session instead,
as the Arduino path already does.
2026-11-13 21:55:42 -06:00
J. Nick Koston 7b1f9c6bf1 Merge remote-tracking branch 'origin/dev' into web-server-idf-stream-response
# Conflicts:
#	.agents/skills/hardware-test/reference/recipes/web_server.md
2026-11-07 22:09:36 -06:00
J. Nick Koston 3587e29725 Merge branch 'dev' into web-server-idf-stream-response 2026-11-04 22:21:12 -06:00
J. Nick Koston d4244c5388 [web_server_idf] Correct the session close order comment and point stall docs at the stream 2026-11-04 21:47:51 -06:00
J. Nick Koston bec53baf1d Merge branch 'dev' into web-server-idf-stream-response 2026-11-04 21:29:11 -06:00
J. Nick Koston 98fd3786dd [web_server_idf] Create the stream send mutex at boot and test that a failed head batch stops the rest 2026-11-04 21:15:36 -06:00
J. Nick Koston 1f531ff022 Merge branch 'dev' into web-server-idf-stream-response 2026-11-04 20:58:44 -06:00
J. Nick Koston 65e7eb6b06 [web_server_idf] Note that sess_ctx is reserved for AsyncStreamResponse 2026-11-04 20:41:33 -06:00
J. Nick Koston 7538864e1e [web_server_idf] Keep a stream from sending on a socket httpd has closed 2026-11-04 20:36:27 -06:00
J. Nick Koston c0f97475d3 [web_server_idf] Document the stream head body as a C string 2026-11-04 20:07:37 -06:00
J. Nick Koston df6fcaa6fe Merge remote-tracking branch 'origin/dev' into web-server-idf-stream-response
# Conflicts:
#	esphome/components/web_server_idf/web_server_idf.cpp
#	esphome/components/web_server_idf/web_server_idf.h
2026-11-03 18:47:25 -06:00
J. Nick Koston eb1b793af9 Merge branch 'dev' into web-server-idf-stream-response 2026-10-30 15:46:21 -10:00
J. Nick Koston b8ce904e0c [web_server_idf] Let a stream response header replace a default header of the same name 2026-10-30 14:24:31 -10:00
J. Nick Koston 6befa73b00 [web_server_idf] Shut the socket down when the stream head is not fully sent 2026-10-30 13:09:28 -10:00
J. Nick Koston 02211e15d8 [web_server_idf] Skip the stall clock for empty writes and clear it on send errors 2026-10-30 12:52:36 -10:00
J. Nick Koston dbbe02f423 [web_server_idf] Include stream_head.h without web_server, for HttpHeader 2026-10-30 12:35:17 -10:00
J. Nick Koston 97a1aa781e [web_server_idf] Simplify the stream response stall clock and head batching
Log the no-memory warning once per stall, free the closing session's buffers once,
add is_stalled() and check that the event stream head fits one write.
2026-10-30 12:22:07 -10:00
J. Nick Koston 0b67a754cd [web_server_idf] Build the stream head tests only for host 2026-10-30 12:15:18 -10:00
J. Nick Koston 320b6a8221 [web_server_idf] Factor the stream response head, iovec advance and stall clock out for host tests
Moves the pure parts of AsyncStreamResponse into stream_head.h (no httpd or
lwIP includes) and adds gtests: the event stream head is compared byte for
byte against what httpd_resp_send_chunk() sent, plus raw heads, batching,
short writes and the stall clock including a millis() rollover.
2026-10-30 12:06:59 -10:00
J. Nick Koston 4e5a5ec32d [web_server_idf] Send the stream response head in one write and tidy the stall clock
Gather the status line, headers and optional body into one iovec list
and send it with a single blocking sendmsg, instead of one httpd_send per
piece. Split the stall clock into start_stall_(), stall_expired_() and
clear_stall_(), and use is_connected() and is_closing() in the event
stream.
2026-10-30 11:40:30 -10:00
J. Nick Koston 09ce8d7d6e [web_server_idf] Add a long-lived streaming response that components can use
Move the session handling of AsyncEventSourceResponse into a new
AsyncStreamResponse base: the response head and session context set up
in the request handler, the non-blocking send override, main loop gather
writes with the send stall clock, the identity-checked close through
httpd_queue_work and the LRU refresh. The event stream now derives from
it, so other components can stream raw or chunked bytes from the main
loop without their own tasks or raw socket calls.

The response head is written directly instead of through
httpd_resp_send_chunk(), so a stream does not have to start with a data
chunk. The event stream still sends the same bytes on the wire.
2026-10-30 11:35:09 -10:00
91 changed files with 3156 additions and 1563 deletions

No files matched your search

+1 -1
View File
@@ -120,7 +120,7 @@ helper; `AGENTS.md` section 4 and the `component-codegen` skill have the detail.
| hand-written `set_x_sensor()` plus `x_sensor_` member | `SUB_SENSOR(x)`, `SUB_BINARY_SENSOR`, `SUB_TEXT_SENSOR`, `SUB_SWITCH`, ... |
| `if CONF_X in config: s = await sensor.new_sensor(config[CONF_X])` plus setter | `sensors = sensor.sub_sensors(config)` then `await sensors(CONF_X, hub.set_x_sensor)` |
| hand-written `{"a": MyEnum.MY_ENUM_A, ...}` dict | `MyEnum.options("a", "b", prefix="MY_ENUM_")` |
| loops on `available()` / `read_byte()` | UART `read_available(on_byte)`, `discard_available()`, `LineReader<N>`, `FrameReader<N, HEADER...>`, `LengthFrameReader<N, FrameFormat<...>...>` |
| loops on `available()` / `read_byte()` | UART `read_available(on_byte)`, `discard_available()`, `LineReader<N>`, `FrameReader<N, HEADER...>` |
| hand-rolled CRC, checksum or bit loop | `crc8`, `crc16`, `xor_bytes`, `sum16_bytes`, `encode_uint16`, ... (`esphome/core/helpers.h`) |
| integer split into buffer bytes by hand (`buf[0] = v >> 8; buf[1] = v;`) | `put_be16`/`24`/`32`, `put_le16`, `put_le32` (`esphome/core/helpers.h`) |
| bare `mark_failed()`, or `ESP_LOGE` then `mark_failed()` | `this->mark_failed(LOG_STR("reason"))` or `LOG_STR(ESP_LOG_MSG_COMM_FAIL)` |
+1 -4
View File
@@ -246,10 +246,7 @@ file does, and it is the authority when they disagree. The most useful starting
`time::decode_bcd_calendar<LAYOUT>` and `encode_bcd_calendar<LAYOUT>` with `EPSON_BCD_CALENDAR`,
`DS_BCD_CALENDAR` or `PCF_BCD_CALENDAR`.
* **Bus helpers:** UART drains with `read_available(on_byte)` and `discard_available()`, text lines with
`LineReader<N>`, fixed-length binary frames with `FrameReader<N, HEADER...>`, frames that give their own length
with `LengthFrameReader<N, FrameFormat<FrameHeader<...>, PayloadLengthU16LE<LengthField{...}>>...>` fed by
`read_frames(reader, handler, now, last_rx)`, which also gives up a frame stalled by a corrupt length;
I2C has `update_register`,
`LineReader<N>`, fixed-length binary frames with `FrameReader<N, HEADER...>`; I2C has `update_register`,
`update_register16`, `read_byte_reg16` and `write_byte_reg16` for 16-bit register addresses,
`write_registers` (PROGMEM `I2CRegValue` table), `check_chip_id` and `read_bytes_16_le`.
`if (!this->status_comm_result(ok)) return;` sets or clears the warning; an earlier step of a multi-step
@@ -1,7 +1,7 @@
import logging
import esphome.codegen as cg
from esphome.components import web_server_base, wifi
from esphome.components import web_server_base, web_server_idf, wifi
from esphome.components.web_server_base import CONF_WEB_SERVER_BASE_ID
from esphome.components.wifi import DOMAIN as WIFI_DOMAIN
import esphome.config_validation as cv
@@ -38,6 +38,13 @@ DOMAIN = "captive_portal"
captive_portal_ns = cg.esphome_ns.namespace("captive_portal")
CaptivePortal = captive_portal_ns.class_("CaptivePortal", cg.Component)
def _require_plain_http(config: ConfigType) -> ConfigType:
# Phones check for a captive portal over plain HTTP.
web_server_idf.require_plain_http(DOMAIN)
return config
CONFIG_SCHEMA = cv.All(
cv.Schema(
{
@@ -58,6 +65,7 @@ CONFIG_SCHEMA = cv.All(
PLATFORM_RTL87XX,
]
),
_require_plain_http,
)
@@ -0,0 +1,28 @@
#include "epaper_spi_ssd1677_bwr.h"
namespace esphome::epaper_spi {
bool EPaperSSD1677BWR::reset() {
if (!EPaperBase::reset())
return false;
this->command(0x12); // SWRESET; the INITIALISE state waits for busy to clear
return true;
}
void EPaperSSD1677BWR::set_window() {
const uint16_t x_end = this->width_ - 1;
const uint16_t y_first = this->offset_height_ + this->height_ - 1;
const uint16_t y_last = this->offset_height_;
this->cmd_data(0x44, {0x00, 0x00, (uint8_t) x_end, (uint8_t) (x_end >> 8)});
// Y runs down from the last gate, to match data entry mode 0x01
this->cmd_data(0x45, {(uint8_t) y_first, (uint8_t) (y_first >> 8), (uint8_t) y_last, (uint8_t) (y_last >> 8)});
this->cmd_data(0x4E, {0x00, 0x00});
this->cmd_data(0x4F, {(uint8_t) y_first, (uint8_t) (y_first >> 8)});
}
void EPaperSSD1677BWR::refresh_screen(bool partial) {
this->cmd_data(0x22, {0xF7});
this->command(0x20);
}
} // namespace esphome::epaper_spi
@@ -0,0 +1,32 @@
#pragma once
#include "epaper_weact_3c.h"
namespace esphome::epaper_spi {
/**
* SSD1677 3-color (black/white/red) e-paper displays, e.g. the Waveshare 7.5" HD (B).
*
* Drawn and sent as EPaperWeAct3C, except that the SSD1677 addresses RAM X in pixels with two bytes, and the RAM is
* written from the last gate down (data entry mode 0x01), as in Waveshare's reference code. The controller scans
* offset_height + height gates and the panel is on the last height of them, so the RAM window covers only those.
* The window is exactly the panel, so after the first plane the address counter wraps back to the start for the
* second.
*/
class EPaperSSD1677BWR : public EPaperWeAct3C {
public:
EPaperSSD1677BWR(const char *name, uint16_t width, uint16_t height, const uint8_t *init_sequence,
size_t init_sequence_length, uint16_t offset_height)
: EPaperWeAct3C(name, width, height, init_sequence, init_sequence_length), offset_height_(offset_height) {}
protected:
bool reset() override;
void set_window() override;
// The full refresh (0x22 0xF7) powers the panel up itself, as Waveshare's reference code relies on.
void power_on() override {}
void refresh_screen(bool partial) override;
uint16_t offset_height_;
};
} // namespace esphome::epaper_spi
@@ -6,7 +6,7 @@ namespace esphome::epaper_spi {
ESPHOME_LOG_TAG(TAG, "epaper_weact_3c");
void EPaperWeAct3C::set_window_() {
void EPaperWeAct3C::set_window() {
// For full screen refresh, we always start from (0,0)
// The y_low_/y_high_ values track the dirty region for optimization,
// but for display refresh we need to write from the beginning
@@ -39,7 +39,7 @@ bool HOT EPaperWeAct3C::transfer_data() {
// A push only yields after sending some data, so it never stops at exactly 0 or half_buffer.
if (this->current_data_index_ == 0) {
ESP_LOGV(TAG, "transfer_data: sending RED buffer (0x26)");
this->set_window_(); // Reset RAM X/Y counters to start position
this->set_window(); // Reset RAM X/Y counters to start position
this->command(0x26);
this->current_data_index_ = half_buffer;
}
@@ -19,7 +19,7 @@ class EPaperWeAct3C : public EPaperBWR {
: EPaperBWR(name, width, height, init_sequence, init_sequence_length, invert_red, /*red_sets_black=*/true) {}
protected:
void set_window_();
virtual void set_window();
void refresh_screen(bool partial) override;
void power_on() override;
void power_off() override;
@@ -0,0 +1,92 @@
"""Black/White/Red e-paper displays using the SSD1677 controller.
Supported models:
- waveshare-7.5in-hd-b: 880x528 pixels (Waveshare 7.5" HD (B), EPD_7in5b_HD)
The controller scans offset_height + height gates, and the panel is on the last height of them;
offset_height (under dimensions) is a property of the panel.
"""
import esphome.config_validation as cv
from esphome.const import (
CONF_BUSY_PIN,
CONF_DIMENSIONS,
CONF_HEIGHT,
CONF_OFFSET_HEIGHT,
CONF_RESET_PIN,
CONF_WIDTH,
)
from esphome.types import ConfigType
from . import EpaperModel
# The SSD1677 drives this many sources (columns) and gates (rows)
SOURCE_COUNT = 960
GATE_COUNT = 680
class SSD1677BWR(EpaperModel):
"""EpaperModel class for Black/White/Red displays using the SSD1677 controller."""
source_files = (
"epaper_spi_bwr.cpp",
"epaper_weact_3c.cpp",
"epaper_spi_ssd1677_bwr.cpp",
)
def __init__(self, name: str, **defaults: object) -> None:
defaults.setdefault(CONF_OFFSET_HEIGHT, 0)
super().__init__(name, "EPaperSSD1677BWR", **defaults)
def option(
self, name: str, fallback: object = cv.UNDEFINED
) -> cv.Optional | cv.Required:
if name in (CONF_RESET_PIN, CONF_BUSY_PIN):
return cv.Required(name)
return super().option(name, fallback)
def validate_config(self, config: ConfigType) -> ConfigType:
width, height = self.get_dimensions(config)
if width > SOURCE_COUNT:
raise cv.Invalid(
f"The panel width must not exceed {SOURCE_COUNT} sources",
path=[CONF_DIMENSIONS, CONF_WIDTH],
)
# The model's defaults fit, so this fails only with explicit dimensions, which always have a height
if self.get_offset_height(config) + height > GATE_COUNT:
raise cv.Invalid(
f"{CONF_OFFSET_HEIGHT} plus the panel height must not exceed {GATE_COUNT} gates",
path=[CONF_DIMENSIONS, CONF_HEIGHT],
)
return config
def get_constructor_args(self, config: ConfigType) -> tuple:
return (self.get_offset_height(config),)
def get_init_sequence(self, config: ConfigType) -> tuple:
"""Follow Waveshare's EPD_7in5b_HD init, less the steps the driver does itself.
The RAM window is set before each push, the RAM fills (0x46, 0x47) are not needed as both
planes are always written in full, and the full refresh (0x22 0xF7) loads the waveform.
"""
_, height = self.get_dimensions(config)
last_gate = self.get_offset_height(config) + height - 1
return (
(0x0C, 0xAE, 0xC7, 0xC3, 0xC0, 0x40), # Booster soft start
# Driver output control: gate count, scanned bottom to top
(0x01, last_gate & 0xFF, last_gate >> 8, 0x01),
(0x11, 0x01), # Data entry mode: X increments, Y decrements
(0x3C, 0x01), # Border waveform
(0x18, 0x80), # Internal temperature sensor
)
# Model: Waveshare 7.5" HD (B) - 880x528 pixels, SSD1677 controller
# The panel is on the last 528 of the controller's 680 gates. Waveshare's code sets 688 gates (0x2AF),
# more than the controller has, which shows the image a few rows too low.
SSD1677BWR(
"waveshare-7.5in-hd-b",
width=880,
height=528,
offset_height=152,
)
+31 -9
View File
@@ -835,6 +835,22 @@ def is_idf_sdkconfig_option_enabled(name: str) -> bool:
return value is not None and _format_sdkconfig_val(value) == "y"
def task_wdt_timeout_ms() -> int:
"""Return the configured task watchdog timeout; 0 when it is disabled.
For final validation only, as it reads the full config.
"""
return fv.full_config.get()[DOMAIN][CONF_WATCHDOG_TIMEOUT].total_milliseconds
def validate_task_wdt_enabled(config: ConfigType, key: str) -> None:
"""Final-validate that ``key`` is not set while the task watchdog is disabled."""
if key in config and task_wdt_timeout_ms() == 0:
raise cv.Invalid(
f"Cannot be set when {DOMAIN}.{CONF_WATCHDOG_TIMEOUT} is 0s", path=[key]
)
def set_idf_sdkconfig_default(name: str, value: SdkconfigValueType) -> None:
"""Set an sdkconfig option unless it is already set.
@@ -2510,7 +2526,13 @@ CONFIG_SCHEMA = cv.All(
): _validate_toolchain,
cv.Optional(CONF_WATCHDOG_TIMEOUT, default="5s"): cv.All(
cv.positive_time_period_seconds,
cv.Range(min=cv.TimePeriod(seconds=5), max=cv.TimePeriod(seconds=60)),
cv.Any(
cv.Range(max=cv.TimePeriod(seconds=0)),
cv.Range(
min=cv.TimePeriod(seconds=5), max=cv.TimePeriod(seconds=60)
),
msg="must be 0s (disabled) or between 5s and 60s",
),
),
}
),
@@ -3287,14 +3309,14 @@ async def to_code(config):
add_idf_sdkconfig_option("CONFIG_HEAP_PLACE_FUNCTION_INTO_FLASH", True)
# Setup watchdog
add_idf_sdkconfig_option("CONFIG_ESP_TASK_WDT_INIT", True)
add_idf_sdkconfig_option("CONFIG_ESP_TASK_WDT_PANIC", True)
add_idf_sdkconfig_option("CONFIG_ESP_TASK_WDT_CHECK_IDLE_TASK_CPU0", False)
add_idf_sdkconfig_option("CONFIG_ESP_TASK_WDT_CHECK_IDLE_TASK_CPU1", False)
add_idf_sdkconfig_option(
"CONFIG_ESP_TASK_WDT_TIMEOUT_S",
config[CONF_WATCHDOG_TIMEOUT].total_seconds,
)
wdt = config[CONF_WATCHDOG_TIMEOUT].total_seconds
add_idf_sdkconfig_option("CONFIG_ESP_TASK_WDT_INIT", wdt != 0)
if wdt:
cg.add_define("USE_ESP32_TASK_WDT")
add_idf_sdkconfig_option("CONFIG_ESP_TASK_WDT_PANIC", True)
add_idf_sdkconfig_option("CONFIG_ESP_TASK_WDT_CHECK_IDLE_TASK_CPU0", False)
add_idf_sdkconfig_option("CONFIG_ESP_TASK_WDT_CHECK_IDLE_TASK_CPU1", False)
add_idf_sdkconfig_option("CONFIG_ESP_TASK_WDT_TIMEOUT_S", wdt)
# Disable dynamic log level control to save memory
add_idf_sdkconfig_option("CONFIG_LOG_DYNAMIC_LEVEL_CONTROL", False)
+2
View File
@@ -43,7 +43,9 @@ void arch_restart() {
void arch_init() {
// Enable the task watchdog only on the loop task (from which we're currently running)
#ifdef USE_ESP32_TASK_WDT
esp_task_wdt_add(nullptr);
#endif
// Handle OTA rollback: mark partition valid immediately unless USE_OTA_ROLLBACK is enabled,
// in which case safe_mode will mark it valid after confirming successful boot.
+5 -1
View File
@@ -49,7 +49,11 @@ __attribute__((always_inline)) inline uint64_t millis_64() {
// NOLINTNEXTLINE(readability-identifier-naming)
__attribute__((always_inline)) inline void delayMicroseconds(uint32_t us) { delay_microseconds_safe(us); }
__attribute__((always_inline)) inline void arch_feed_wdt() { esp_task_wdt_reset(); }
__attribute__((always_inline)) inline void arch_feed_wdt() {
#ifdef USE_ESP32_TASK_WDT
esp_task_wdt_reset();
#endif
}
__attribute__((always_inline)) inline uint32_t arch_get_cpu_cycle_count() { return esp_cpu_get_cycle_count(); }
void arch_init();
+3 -5
View File
@@ -22,7 +22,6 @@ from esphome.const import (
CONF_DNS2,
CONF_DOMAIN,
CONF_ENABLE_ON_BOOT,
CONF_ETHERNET,
CONF_GATEWAY,
CONF_ID,
CONF_INTERRUPT_PIN,
@@ -397,9 +396,9 @@ def _validate(config: ConfigType) -> ConfigType:
BASE_SCHEMA = cv.Schema(
{
cv.GenerateID(): cv.declare_id(EthernetComponent),
cv.Optional(
CONF_MANUAL_IP, visibility=cv.Visibility.ADVANCED
): MANUAL_IP_SCHEMA,
cv.Optional(CONF_MANUAL_IP, visibility=cv.Visibility.ADVANCED): cv.All(
MANUAL_IP_SCHEMA, require_ipv4(DOMAIN, CONF_MANUAL_IP)
),
cv.Optional(CONF_DOMAIN, default=".local"): cv.domain_name,
cv.Optional(CONF_USE_ADDRESS): cv.string_strict,
cv.Optional(CONF_MAC_ADDRESS): cv.mac_address,
@@ -556,7 +555,6 @@ CONFIG_SCHEMA = cv.All(
upper=True,
),
_validate,
require_ipv4(CONF_ETHERNET),
)
@@ -14,6 +14,9 @@
#if USE_NETWORK_IPV6
#include <esp_netif_net_stack.h>
#include <lwip/netif.h>
#if !USE_NETWORK_IPV4
#include <lwip/dhcp6.h>
#endif
#endif
#ifdef USE_PSRAM
#include <esp_psram.h>
@@ -730,6 +733,7 @@ network::IPAddresses EthernetComponent::get_ip_addresses() {
network::IPAddresses addresses;
if (!this->ethernet_initialized_)
return addresses; // all-zero IPs
#if USE_NETWORK_IPV4
esp_netif_ip_info_t ip;
esp_err_t err = esp_netif_get_ip_info(this->eth_netif_, &ip);
if (err != ESP_OK) {
@@ -739,6 +743,7 @@ network::IPAddresses EthernetComponent::get_ip_addresses() {
} else {
addresses[0] = network::IPAddress(&ip.ip);
}
#endif
#if USE_NETWORK_IPV6
network::fill_ip6_addresses(this->eth_netif_, addresses);
#endif /* USE_NETWORK_IPV6 */
@@ -777,7 +782,7 @@ void EthernetComponent::eth_event_handler(void *arg, esp_event_base_t event_base
#if USE_NETWORK_IPV6
// Start SLAAC on link-up, not after the DHCPv4 lease. This also restores the
// link-local after a link flap, which clears the IPv6 addresses.
if (esp_err_t ll_err = esp_netif_create_ip6_linklocal(global_eth_component->eth_netif_); ll_err != ESP_OK) {
if (esp_err_t ll_err = global_eth_component->ensure_ip6_linklocal_(); ll_err != ESP_OK) {
ESP_LOGW(TAG, "esp_netif_create_ip6_linklocal failed on link-up: %s", esp_err_to_name(ll_err));
}
#endif /* USE_NETWORK_IPV6 */
@@ -816,6 +821,9 @@ void EthernetComponent::got_ip_event_handler(void *arg, esp_event_base_t event_b
void EthernetComponent::got_ip6_event_handler(void *arg, esp_event_base_t event_base, int32_t event_id,
void *event_data) {
ip_event_got_ip6_t *event = (ip_event_got_ip6_t *) event_data;
if (event->esp_netif != global_eth_component->eth_netif_) {
return; // another interface's address (wifi alongside ethernet)
}
ESP_LOGV(TAG, "[Ethernet event] ETH Got IPv6: " IPV6STR, IPV62STR(event->ip6_info.ip));
// Count the addresses on the interface, not the events: recreating the link-local
// after a link flap fires another event for the same address.
@@ -825,8 +833,8 @@ void EthernetComponent::got_ip6_event_handler(void *arg, esp_event_base_t event_
global_eth_component->ip_state_pending_.store(1, std::memory_order_release);
#endif
#if (USE_NETWORK_MIN_IPV6_ADDR_COUNT > 0)
global_eth_component->connected_ =
global_eth_component->got_ipv4_address_ && (global_eth_component->ipv6_count_ >= USE_NETWORK_MIN_IPV6_ADDR_COUNT);
global_eth_component->connected_ = (!USE_NETWORK_IPV4 || global_eth_component->got_ipv4_address_) &&
(global_eth_component->ipv6_count_ >= USE_NETWORK_MIN_IPV6_ADDR_COUNT);
global_eth_component->enable_loop_soon_any_context(); // Enable loop when connection state changes
#else
global_eth_component->connected_ = global_eth_component->got_ipv4_address_;
@@ -840,21 +848,31 @@ void EthernetComponent::got_ip6_event_handler(void *arg, esp_event_base_t event_
// duplicate address detection: recreating it would restart DAD. esp_netif_get_ip6_linklocal()
// only reports a preferred address, so ask lwIP for the slot state instead.
esp_err_t EthernetComponent::ensure_ip6_linklocal_() {
if (auto *netif = static_cast<struct netif *>(esp_netif_get_netif_impl(this->eth_netif_)); netif != nullptr) {
u8_t state;
{
LwIPLock lock;
state = netif_ip6_addr_state(netif, 0);
}
if (ip6_addr_istentative(state) || ip6_addr_isvalid(state)) {
return ESP_OK;
}
auto *netif = static_cast<struct netif *>(esp_netif_get_netif_impl(this->eth_netif_));
u8_t state = IP6_ADDR_INVALID;
if (netif != nullptr) {
LwIPLock lock;
state = netif_ip6_addr_state(netif, 0);
}
esp_err_t err = esp_netif_create_ip6_linklocal(this->eth_netif_);
if (err == ESP_OK) {
if (!ip6_addr_istentative(state) && !ip6_addr_isvalid(state)) {
if (esp_err_t err = esp_netif_create_ip6_linklocal(this->eth_netif_); err != ESP_OK) {
return err;
}
ESP_LOGD(TAG, "IPv6 link-local address created");
}
return err;
#if !USE_NETWORK_IPV4
// Without DHCPv4, DNS servers come from RDNSS or stateless DHCPv6; esp_netif never starts the latter.
// Idempotent in lwIP, so the link-up and connect paths can both call it.
if (netif != nullptr) {
esp_netif_tcpip_exec(
[](void *ctx) -> esp_err_t {
dhcp6_enable_stateless(static_cast<struct netif *>(ctx));
return ESP_OK;
},
netif);
}
#endif
return ESP_OK;
}
#endif /* USE_NETWORK_IPV6 */
@@ -896,6 +914,7 @@ void EthernetComponent::start_connect_() {
ESP_LOGW(TAG, "esp_netif_set_hostname failed: %s", esp_err_to_name(err));
}
#if USE_NETWORK_IPV4
esp_netif_ip_info_t info{};
#ifdef USE_ETHERNET_MANUAL_IP
const ManualIP manual_ip = load_manual_ip();
@@ -946,6 +965,7 @@ void EthernetComponent::start_connect_() {
ESPHL_ERROR_CHECK(err, "DHCPC start error");
}
#endif
#endif // USE_NETWORK_IPV4
#if USE_NETWORK_IPV6
// Attempt to create IPv6 link-local address
// We MUST attempt this here, not just in finish_connect_(), because with
@@ -980,8 +1000,13 @@ void EthernetComponent::dump_connect_params_() {
ESP_LOGCONFIG(TAG, " uninitialized/disabled");
return;
}
#if USE_NETWORK_IPV4
esp_netif_ip_info_t ip;
esp_netif_get_ip_info(this->eth_netif_, &ip);
network::IPAddress ip4(&ip.ip), subnet(&ip.netmask), gateway(&ip.gw);
#else
network::IPAddress ip4, subnet, gateway;
#endif
const ip_addr_t *dns_ip1;
const ip_addr_t *dns_ip2;
{
@@ -1020,8 +1045,7 @@ void EthernetComponent::dump_connect_params_() {
" MAC Address: %s\n"
" Is Full Duplex: %s\n"
" Link Speed: %u",
network::IPAddress(&ip.ip).str_to(ip_buf), App.get_name().c_str(),
network::IPAddress(&ip.netmask).str_to(subnet_buf), network::IPAddress(&ip.gw).str_to(gateway_buf),
ip4.str_to(ip_buf), App.get_name().c_str(), subnet.str_to(subnet_buf), gateway.str_to(gateway_buf),
network::IPAddress(dns_ip1).str_to(dns1_buf), network::IPAddress(dns_ip2).str_to(dns2_buf),
this->get_eth_mac_address_pretty_into_buffer(mac_buf),
YESNO(this->get_duplex_mode() == ETH_DUPLEX_FULL), link_speed);
+6 -6
View File
@@ -17,14 +17,12 @@ from esphome.const import (
CONF_TIMEOUT,
CONF_URL,
CONF_WATCHDOG_TIMEOUT,
PLATFORM_ESP32,
PLATFORM_HOST,
PlatformFramework,
__version__,
)
from esphome.core import CORE, ID, Lambda, TimePeriodMilliseconds
from esphome.cpp_generator import MockObj, TemplateArgsType
import esphome.final_validate as fv
from esphome.helpers import IS_MACOS
from esphome.types import ConfigType
@@ -105,16 +103,18 @@ def default_watchdog_timeout(config: ConfigType) -> None:
The default never goes below the platform task watchdog, so a user who
widened `esp32.watchdog_timeout` keeps that window during requests.
When the task watchdog is disabled, the request watchdog cannot be set.
"""
if not CORE.is_esp32 or CONF_WATCHDOG_TIMEOUT in config:
if not CORE.is_esp32:
return
esp32.validate_task_wdt_enabled(config, CONF_WATCHDOG_TIMEOUT)
platform_ms = esp32.task_wdt_timeout_ms()
if platform_ms == 0 or CONF_WATCHDOG_TIMEOUT in config:
return
derived_ms = (
config[CONF_TIMEOUT].total_milliseconds * WATCHDOG_TIMEOUT_MULTIPLIER
+ WATCHDOG_TIMEOUT_MARGIN_MS
)
platform_ms = fv.full_config.get()[PLATFORM_ESP32][
CONF_WATCHDOG_TIMEOUT
].total_milliseconds
config[CONF_WATCHDOG_TIMEOUT] = TimePeriodMilliseconds(
milliseconds=max(derived_ms, platform_ms)
)
@@ -20,8 +20,6 @@
#include "esp_crt_bundle.h"
#endif
#include "esp_task_wdt.h"
namespace esphome::http_request {
ESPHOME_LOG_TAG(TAG, "http_request");
@@ -468,12 +466,7 @@ void HttpContainerIDF::end() {
this->client_ = nullptr;
}
void HttpContainerIDF::feed_wdt() {
// Tests to see if the executing task has a watchdog timer attached
if (esp_task_wdt_status(nullptr) == ESP_OK) {
App.feed_wdt();
}
}
void HttpContainerIDF::feed_wdt() { App.feed_wdt_if_watched(); }
} // namespace esphome::http_request
+19 -7
View File
@@ -293,9 +293,9 @@ def _ip_request(
return validator
def require_ipv4(name: str) -> Callable[[ConfigType], ConfigType]:
"""Return a validator declaring that component `name` needs IPv4."""
return _ip_request(lambda data: data.require_ipv4.add(name))
def require_ipv4(*path: str) -> Callable[[ConfigType], ConfigType]:
"""Return a validator declaring that the component or option at `path` needs IPv4."""
return _ip_request(lambda data: data.require_ipv4.add(": ".join(path)))
def require_ipv6(name: str) -> Callable[[ConfigType], ConfigType]:
@@ -456,7 +456,10 @@ def _final_validate(config: ConfigType) -> None:
CONF_ENABLE_IPV4,
"IPv4",
data.require_ipv4,
default=not data.request_ipv4_off,
# Wi-Fi and Ethernet keep IPv4 unless the config turns it off explicitly
default=not data.request_ipv4_off
or CONF_WIFI in full_config
or CONF_ETHERNET in full_config,
)
if not config[CONF_ENABLE_IPV4] and not config[CONF_ENABLE_IPV6]:
@@ -662,9 +665,11 @@ async def to_code(config: ConfigType) -> None:
cg.add_define("USE_NETWORK_IPV6", enable_ipv6)
if enable_ipv6:
cg.add_define(
"USE_NETWORK_MIN_IPV6_ADDR_COUNT", config[CONF_MIN_IPV6_ADDR_COUNT]
)
# Without IPv4 an IPv6 address is the connection, so wait for at least one
min_count = config[CONF_MIN_IPV6_ADDR_COUNT]
if not enable_ipv4:
min_count = max(min_count, 1)
cg.add_define("USE_NETWORK_MIN_IPV6_ADDR_COUNT", min_count)
if CORE.is_esp32:
add_idf_sdkconfig_option("CONFIG_LWIP_IPV6", enable_ipv6)
add_idf_sdkconfig_option("CONFIG_LWIP_IPV6_AUTOCONFIG", enable_ipv6)
@@ -681,6 +686,13 @@ async def to_code(config: ConfigType) -> None:
cg.add_define("USE_NETWORK_IPV4", enable_ipv4)
if CORE.is_esp32:
add_idf_sdkconfig_option("CONFIG_LWIP_IPV4", enable_ipv4)
# DNS over RDNSS or stateless DHCPv6 is for IPv6-only Wi-Fi and Ethernet; leave
# other IPv4-less builds (OpenThread) on the sdkconfig they already build with.
if not enable_ipv4 and CORE.has_at_least_one_component(
CONF_WIFI, CONF_ETHERNET
):
add_idf_sdkconfig_option("CONFIG_LWIP_IPV6_RDNSS_MAX_DNS_SERVERS", 2)
add_idf_sdkconfig_option("CONFIG_LWIP_IPV6_DHCP6", True)
# Pvariable creation lives in a separate coroutine at NETWORK_SERVICES so it
# emits after wifi/ethernet at COMMUNICATION. This keeps compile-time config
+16 -5
View File
@@ -3,6 +3,8 @@
#include "esphome/core/defines.h"
#include "esphome/core/helpers.h"
#include "esphome/core/log.h"
#include <utility>
#ifdef USE_NETWORK
namespace esphome::network {
@@ -13,8 +15,17 @@ ESPHOME_LOG_TAG(TAG, "network");
void fill_ip6_addresses(esp_netif_t *netif, IPAddresses &addresses) {
esp_ip6_addr_t ip6s[CONFIG_LWIP_IPV6_NUM_ADDRESSES];
size_t count = esp_netif_get_all_ip6(netif, ip6s);
// Slot 0 holds the IPv4 address
if (count > addresses.size() - 1) {
// Slot 0 holds the IPv4 address; without IPv4 the first IPv6 address takes it, so it reads as the primary one
constexpr size_t first = USE_NETWORK_IPV4 ? 1 : 0;
if constexpr (first == 0) {
// Lead with a routable address; the link-local one comes first from esp-netif and is useless off-link
for (size_t i = 1; i < count; i++) {
if (esp_netif_ip6_get_addr_type(&ip6s[0]) != ESP_IP6_ADDR_IS_LINK_LOCAL)
break;
std::swap(ip6s[0], ip6s[i]);
}
}
if (count > addresses.size() - first) {
// Warn once per interface
static esp_netif_t *warned[3]{};
for (auto &slot : warned) {
@@ -24,14 +35,14 @@ void fill_ip6_addresses(esp_netif_t *netif, IPAddresses &addresses) {
if (slot == nullptr) {
slot = netif;
ESP_LOGW(TAG, "%s has %zu IPv6 addresses, only %zu are reported", esp_netif_get_desc(netif), count,
addresses.size() - 1);
addresses.size() - first);
break;
}
}
count = addresses.size() - 1;
count = addresses.size() - first;
}
for (size_t i = 0; i < count; i++) {
addresses[i + 1] = IPAddress(&ip6s[i]);
addresses[i + first] = IPAddress(&ip6s[i]);
}
}
#endif
+9
View File
@@ -10,6 +10,7 @@ from esphome.const import (
PLATFORM_ESP8266,
)
from esphome.core import CORE, TimePeriod
from esphome.types import ConfigType
from . import Nextion, nextion_ref
from .base_component import (
@@ -147,6 +148,14 @@ CONFIG_SCHEMA = cv.All(
)
def _final_validate(config: ConfigType) -> None:
if CORE.is_esp32:
esp32.validate_task_wdt_enabled(config, CONF_TFT_UPLOAD_WATCHDOG_TIMEOUT)
FINAL_VALIDATE_SCHEMA = _final_validate
automation.register_apply_action(
"display.nextion.set_brightness",
cv.maybe_simple_value(
+10 -2
View File
@@ -31,7 +31,12 @@ from esphome.components.esp32 import (
require_vfs_select,
)
from esphome.components.mdns import MDNSComponent, enable_mdns_storage
from esphome.components.network import add_use_address, request_ipv4_off, require_ipv6
from esphome.components.network import (
add_use_address,
request_ipv4_off,
require_ipv4,
require_ipv6,
)
from esphome.components.nrf52.framework import include_west_project
from esphome.components.zephyr import zephyr_add_prj_conf, zephyr_add_prj_conf_min
from esphome.config_helpers import filter_source_files_from_platform
@@ -297,7 +302,10 @@ _BORDER_ROUTER_SCHEMA = cv.Schema({cv.Optional(CONF_RCP): _RCP_SCHEMA})
def _validate_border_router(value: object) -> ConfigType:
return _BORDER_ROUTER_SCHEMA({} if value is None else value)
# ESP-IDF's border router library resolves DNS and runs DHCP over IPv4
return require_ipv4(CONF_OPENTHREAD, CONF_BORDER_ROUTER)(
_BORDER_ROUTER_SCHEMA({} if value is None else value)
)
_CONNECTION_SCHEMA = cv.Schema(
@@ -22,7 +22,6 @@
#include "esp_openthread.h"
#include "esp_openthread_lock.h"
#include "esp_openthread_types.h"
#include "esp_task_wdt.h"
#include "esphome/components/network/util.h"
#include "esphome/core/hal.h"
#include "esphome/core/helpers.h"
@@ -470,11 +469,11 @@ InstanceLock InstanceLock::acquire() {
abort();
}
delay(10);
esp_task_wdt_reset();
arch_feed_wdt();
}
while (!esp_openthread_lock_acquire(100)) {
abort_if_task_exited();
esp_task_wdt_reset();
arch_feed_wdt();
}
// The clean-up may have released the lock to this waiter just before deinit destroys it
if (global_openthread_component->is_task_exited()) {
+25 -15
View File
@@ -1,5 +1,5 @@
import esphome.codegen as cg
from esphome.components import web_server_base
from esphome.components import web_server_base, web_server_idf
from esphome.components.web_server_base import CONF_WEB_SERVER_BASE_ID
import esphome.config_validation as cv
from esphome.const import CONF_ID, CONF_INCLUDE_INTERNAL, CONF_NAME, CONF_RELABEL
@@ -22,20 +22,30 @@ CUSTOMIZED_ENTITY = cv.All(
cv.has_at_least_one_key(CONF_ID, CONF_NAME),
)
CONFIG_SCHEMA = cv.Schema(
{
cv.GenerateID(): cv.declare_id(PrometheusHandler),
cv.GenerateID(CONF_WEB_SERVER_BASE_ID): cv.use_id(
web_server_base.WebServerBase
),
cv.Optional(CONF_INCLUDE_INTERNAL, default=False): cv.boolean,
cv.Optional(CONF_RELABEL, default={}): cv.Schema(
{
cv.use_id(EntityBase): CUSTOMIZED_ENTITY,
}
),
},
).extend(cv.COMPONENT_SCHEMA)
def _require_plain_http(config: ConfigType) -> ConfigType:
# Prometheus is advertised over mDNS as a plain HTTP service.
web_server_idf.require_plain_http(DOMAIN)
return config
CONFIG_SCHEMA = cv.All(
cv.Schema(
{
cv.GenerateID(): cv.declare_id(PrometheusHandler),
cv.GenerateID(CONF_WEB_SERVER_BASE_ID): cv.use_id(
web_server_base.WebServerBase
),
cv.Optional(CONF_INCLUDE_INTERNAL, default=False): cv.boolean,
cv.Optional(CONF_RELABEL, default={}): cv.Schema(
{
cv.use_id(EntityBase): CUSTOMIZED_ENTITY,
}
),
},
).extend(cv.COMPONENT_SCHEMA),
_require_plain_http,
)
async def to_code(config: ConfigType) -> None:
@@ -8,10 +8,6 @@
#include "esphome/core/helpers.h"
#include "esphome/core/log.h"
#ifdef USE_ESP_IDF
#include "esp_task_wdt.h"
#endif
ESPHOME_LOG_TAG(TAG, "image_decoder.jpeg");
namespace esphome::runtime_image {
@@ -51,18 +47,6 @@ void JpegDecoder::draw_rgb565(int x, int y, int w, int h, const uint8_t *pixels)
}
}
// Some very big images take too long to decode, so feed the watchdog as the decode goes
// to avoid crashing if the executing task has a watchdog enabled.
static void feed_watchdog() {
#ifdef USE_ESP_IDF
if (esp_task_wdt_status(nullptr) == ESP_OK) {
#endif
App.feed_wdt();
#ifdef USE_ESP_IDF
}
#endif
}
/**
* @brief Callback method that will be called by the JPEGDEC engine when a chunk
* of the image is decoded.
@@ -77,7 +61,8 @@ static int draw_callback(JPEGDRAW *jpeg) {
return 0;
}
feed_watchdog();
// Very big images take long to decode, so feed the watchdog as the decode goes
App.feed_wdt_if_watched();
if (decoder->uses_direct_rgb565()) {
decoder->draw_rgb565(jpeg->x, jpeg->y, jpeg->iWidth, jpeg->iHeight,
reinterpret_cast<const uint8_t *>(jpeg->pPixels));
@@ -141,7 +126,7 @@ int HOT JpegDecoder::decode(uint8_t *buffer, size_t size) {
return this->fail_();
int result;
while ((result = this->jpeg_->decodeRows(1)) == JPEG_DECODE_MORE)
feed_watchdog();
App.feed_wdt_if_watched();
if (result != 1)
return this->fail_();
this->decoded_bytes_ = size;
-11
View File
@@ -90,17 +90,6 @@ class UARTDevice {
count);
}
/// Feed the waiting bytes to a LengthFrameReader; on a pass with none, gap_ms after last_rx, flush a stalled frame.
template<size_t N = 64, typename R, typename F>
void read_frames(R &reader, F &&handle, uint32_t now, uint32_t &last_rx, uint32_t gap_ms = 250) {
if (const size_t count = this->available()) {
last_rx = now;
this->read_available<N>([&reader, &handle](uint8_t c) { reader.feed(c, handle); }, count);
} else if (reader.length() != 0 && now - last_rx >= gap_ms) {
reader.flush(handle);
}
}
/// Read and drop every byte waiting in the receive buffer.
void discard_available();
+4 -4
View File
@@ -35,7 +35,7 @@ WatchdogManager::~WatchdogManager() {
void WatchdogManager::set_timeout_(uint32_t timeout_ms) {
ESP_LOGV(TAG, "Adjusting WDT to %" PRIu32 "ms", timeout_ms);
#ifdef USE_ESP32
#ifdef USE_ESP32_TASK_WDT
esp_task_wdt_config_t wdt_config = {
.timeout_ms = timeout_ms,
.idle_core_mask = 0,
@@ -51,7 +51,7 @@ void WatchdogManager::set_timeout_(uint32_t timeout_ms) {
wdt_config.trigger_panic = true;
#endif
esp_task_wdt_reconfigure(&wdt_config);
#endif // USE_ESP32
#endif // USE_ESP32_TASK_WDT
#ifdef USE_RP2
watchdog_enable(timeout_ms, true);
@@ -61,9 +61,9 @@ void WatchdogManager::set_timeout_(uint32_t timeout_ms) {
uint32_t WatchdogManager::get_timeout_() {
uint32_t timeout_ms = 0;
#ifdef USE_ESP32
#ifdef USE_ESP32_TASK_WDT
timeout_ms = (uint32_t) CONFIG_ESP_TASK_WDT_TIMEOUT_S * 1000;
#endif // USE_ESP32
#endif // USE_ESP32_TASK_WDT
#ifdef USE_RP2
timeout_ms = watchdog_get_count() / 1000;
+20 -2
View File
@@ -7,7 +7,7 @@ import re
from typing import Any
import esphome.codegen as cg
from esphome.components import web_server_base
from esphome.components import web_server_base, web_server_idf
from esphome.components.json import enable_arena
from esphome.components.logger import request_log_listener
from esphome.components.web_server_base import (
@@ -234,6 +234,8 @@ def _consume_web_server_sockets(config: ConfigType) -> ConfigType:
# (browser opens connections for page resources, SSE event stream, and POST
# requests for entity control which may linger before closing)
web_server_base.add_httpd_clients(5, DOMAIN)
# Event streams send from the main loop straight to the socket, which would bypass TLS.
web_server_idf.require_plain_http(DOMAIN)
return config
@@ -614,9 +616,25 @@ async def to_code(config: ConfigType) -> None:
def FILTER_SOURCE_FILES() -> list[str]:
"""Filter out web_server_v1.cpp when version is not 1."""
"""Filter out web_server_v1.cpp when version is not 1, the ESP32 event stream elsewhere, and the server on host."""
files_to_filter: list[str] = []
# The host has no web server; it builds only sse_chunk.cpp, for its unit tests
if CORE.is_host:
files_to_filter.extend(
(
"event_source.cpp",
"list_entities.cpp",
"web_server.cpp",
"web_server_v1.cpp",
)
)
return files_to_filter
# The /events implementation is ESP32 only
if not CORE.is_esp32:
files_to_filter.extend(("event_source.cpp", "sse_chunk.cpp"))
# web_server_v1.cpp is only needed when version is 1
config = CORE.config.get(DOMAIN, {})
if config.get(CONF_VERSION, 2) != 1:
@@ -0,0 +1,500 @@
#include "event_source.h"
#if defined(USE_ESP32) && defined(USE_WEBSERVER)
#include <algorithm>
#include <cstring>
#include <iterator>
#include "esphome/core/application.h"
#include "esphome/core/helpers.h"
#include "esphome/core/log.h"
#include "web_server.h"
namespace esphome::web_server {
ESPHOME_LOG_TAG(TAG, "web_server_idf");
// Only prepare_json_() may hold the JSON arena: every other frame in this file is capped below one
// arena. Measured at -Os on GCC 14; newer toolchains stay checked on purpose, older ones skip it.
#if defined(__GNUC__) && !defined(__clang__) && __GNUC__ >= 14 && defined(__OPTIMIZE_SIZE__)
#pragma GCC diagnostic error "-Wstack-usage=2048"
#endif
AsyncEventSource::~AsyncEventSource() {
LockGuard guard{this->pending_mutex_};
for (auto *vec : {&this->sessions_, &this->pending_sessions_}) {
for (auto *ses : *vec) {
delete ses; // NOLINT(cppcoreguidelines-owning-memory)
}
}
}
void AsyncEventSource::handleRequest(AsyncWebServerRequest *request) {
#ifdef USE_WEBSERVER_SSL
// Event streams send from the main loop straight to the socket, which would bypass TLS.
// Only an HTTPS server has a global transport context.
httpd_req_t *req = *request;
if (httpd_get_global_transport_ctx(req->handle) != nullptr) {
httpd_resp_send_err(req, HTTPD_501_METHOD_NOT_IMPLEMENTED, "Event streams are not supported over HTTPS");
return;
}
#endif
// Httpd task: set up the live httpd_req_t and park the session; main loop does the rest.
// NOLINTNEXTLINE(cppcoreguidelines-owning-memory,clang-analyzer-cplusplus.NewDeleteLeaks)
auto *rsp = new AsyncEventSourceResponse(request, this, this->web_server_);
{
LockGuard guard{this->pending_mutex_};
this->pending_sessions_.push_back(rsp);
this->has_pending_sessions_.store(true, std::memory_order_release);
}
this->web_server_->enable_loop_soon_any_context();
}
// clang-analyzer traces a false-positive leak path from loop() through
// adopt_pending_sessions_main_loop_() into start_session_main_loop_() and
// finally ArduinoJson. Suppress along the entire in-our-code call chain.
// NOLINTBEGIN(clang-analyzer-cplusplus.NewDeleteLeaks)
bool AsyncEventSource::loop() {
// Fast path: one atomic load per tick. Slow path is out-of-line on connect.
if (this->has_pending_sessions_.load(std::memory_order_acquire)) {
this->adopt_pending_sessions_main_loop_();
}
// Clean up dead sessions safely
// This follows the ESP-IDF pattern where free_ctx marks resources as dead
// and the main loop handles the actual cleanup to avoid race conditions
for (size_t i = 0; i < this->sessions_.size();) {
auto *ses = this->sessions_[i];
// If the session has a dead socket (marked by destroy callback)
if (ses->safe_to_delete()) {
// destroy() already logged the close with the fd; don't double-log here.
delete ses; // NOLINT(cppcoreguidelines-owning-memory)
// Remove by swapping with last element (O(1) removal, order doesn't matter for sessions)
this->sessions_[i] = this->sessions_.back();
this->sessions_.pop_back();
} else {
ses->loop();
++i;
}
}
// A session still waiting for httpd to commit its context keeps the loop alive too
return !this->sessions_.empty() || this->has_pending_sessions_.load(std::memory_order_acquire);
}
void AsyncEventSource::adopt_pending_sessions_main_loop_() {
std::vector<AsyncEventSourceResponse *> incoming;
{
LockGuard guard{this->pending_mutex_};
incoming.swap(this->pending_sessions_);
this->has_pending_sessions_.store(false, std::memory_order_relaxed);
}
for (auto *rsp : incoming) {
// Already disconnected? Drop it; skip session start on a dead session.
if (rsp->safe_to_delete()) {
delete rsp; // NOLINT(cppcoreguidelines-owning-memory)
continue;
}
// httpd commits the session context only after the creating handler returns, so stay
// pending until then; httpd_req_cleanup() always commits it or calls destroy()
if (!rsp->is_attached()) {
LockGuard guard{this->pending_mutex_};
this->pending_sessions_.push_back(rsp);
this->has_pending_sessions_.store(true, std::memory_order_release);
continue;
}
this->sessions_.push_back(rsp);
rsp->start_session_main_loop_();
}
}
// NOLINTEND(clang-analyzer-cplusplus.NewDeleteLeaks)
void AsyncEventSource::try_send_nodefer(const char *message, size_t message_len, const char *event, uint32_t id,
uint32_t reconnect) {
for (auto *ses : this->sessions_) {
if (ses->is_connected()) {
ses->try_send_nodefer(message, message_len, event, id, reconnect);
}
}
}
void AsyncEventSource::deferrable_send_state(void *source, bool detail_all, message_generator_t *message_generator) {
// Skip if no connected clients to avoid unnecessary processing
if (this->empty())
return;
for (auto *ses : this->sessions_) {
if (ses->is_connected()) {
ses->deferrable_send_state(source, detail_all, message_generator);
}
}
}
AsyncEventSourceResponse::AsyncEventSourceResponse(AsyncWebServerRequest *request, AsyncEventSource *server,
WebServer *ws)
: server_(server), web_server_(ws), entities_iterator_(ws, this) {
// Httpd task only. start_session_main_loop_() sends the greeting and starts the iterator.
this->begin(request, SSE_STATUS, SSE_CONTENT_TYPE, SSE_HEADERS, SSE_FIRST_CHUNK);
}
static_assert(web_server_base::stream_head_iov_count(std::size(SSE_HEADERS) + WEB_SERVER_DEFAULT_HEADERS_COUNT) <=
web_server_base::STREAM_HEAD_MAX_IOV,
"the event stream head must go out in one write");
// NOLINTBEGIN(clang-analyzer-cplusplus.NewDeleteLeaks) false positive with ArduinoJson
void AsyncEventSourceResponse::start_session_main_loop_() {
auto *ws = this->web_server_;
// The tcp send buffer is empty on connect. A refusal is a closing session or a failed tail
// allocation; nothing retries the greeting, so close and let the client reconnect.
auto message = ws->get_config_json();
if (!this->try_send_nodefer(message.c_str(), message.size(), "ping", millis(), 30000)) {
ESP_LOGW(TAG, "Config not sent to fd %d", this->log_id());
this->close();
return;
}
#ifdef USE_WEBSERVER_SORTING
for (auto &group : ws->sorting_groups_) {
json::JsonBuilder builder;
JsonObject root = builder.root();
root["name"] = json::linked(group.second.name);
root["sorting_weight"] = group.second.weight;
message = builder.serialize();
// a (very) large number of these should be able to be queued initially without defer
// since the only thing in the send buffer at this point is the initial ping/config.
// A refusal means the socket is full or closing; the remaining groups are not sent. The
// session stays up on purpose: partial grouping beats a reconnect loop on a slow link.
if (!this->try_send_nodefer(message.c_str(), message.size(), "sorting_group")) {
ESP_LOGW(TAG, "Sorting groups not sent to fd %d", this->log_id());
break;
}
}
#endif
this->entities_iterator_.begin(ws->include_internal_);
}
// NOLINTEND(clang-analyzer-cplusplus.NewDeleteLeaks)
// helper for allowing only unique entries in the queue
void AsyncEventSourceResponse::deq_push_back_with_dedup_(void *source, message_generator_t *message_generator) {
DeferredEvent item(source, message_generator);
// Use range-based for loop instead of std::find_if to reduce template instantiation overhead and binary size
for (auto &event : this->deferred_queue_) {
if (event == item) {
return; // Already in queue, no need to update since items are equal
}
}
this->deferred_queue_.push_back(item);
}
void AsyncEventSourceResponse::process_deferred_queue_() {
if (this->is_closing()) {
return;
}
while (!deferred_queue_.empty()) {
DeferredEvent &de = deferred_queue_.front();
if (this->send_json_(de.source_, de.message_generator_)) {
if (this->is_closing() || deferred_queue_.empty()) {
return;
}
// O(n) but memory efficiency is more important than speed here which is why std::vector was chosen
deferred_queue_.erase(deferred_queue_.begin());
} else {
break;
}
}
}
void AsyncEventSourceResponse::release_buffers_() {
std::vector<DeferredEvent>().swap(this->deferred_queue_);
this->tail_.reset();
this->tail_cap_ = 0;
this->tail_len_ = 0;
this->tail_sent_ = 0;
}
void AsyncEventSourceResponse::drain_tail_() {
if (this->sending_ || this->is_closing() || this->tail_len_ == 0) {
return;
}
SendGuard guard{*this};
const size_t remaining = this->tail_len_ - this->tail_sent_;
const ssize_t sent = this->write(this->tail_.get() + this->tail_sent_, remaining);
if (sent <= 0) {
return; // write() owns the stall clock and the close
}
this->no_memory_logged_ = false;
this->tail_sent_ += sent;
if (this->tail_sent_ < this->tail_len_) {
ESP_LOGV(TAG, "Partial send: %zd/%zu bytes (total: %u/%u)", sent, remaining, this->tail_sent_, this->tail_len_);
return;
}
// Fully sent; the storage stays for the next stall
this->tail_len_ = 0;
}
bool AsyncEventSourceResponse::reserve_tail_(size_t len) {
if (this->tail_cap_ >= len) {
return true;
}
if (len > TAIL_MAX_SIZE) {
return false;
}
// Nothing is pending while the tail grows, so free the old block first. PREFER_INTERNAL keeps
// the tail where plain new put it.
this->tail_.reset();
this->tail_ = RAMAllocator<uint8_t>(RAMAllocator<uint8_t>::PREFER_INTERNAL).make_unique_array_for_overwrite(len);
this->tail_cap_ = this->tail_ ? len : 0;
return this->tail_cap_ != 0;
}
bool AsyncEventSourceResponse::stash_chunk_(const char *prefix, size_t prefix_len, const char *message,
size_t message_len, size_t total, size_t sent) {
// A log event of nothing but line breaks is the largest chunk; reserve_tail_() must never
// refuse it for size, only for memory
static_assert(SSE_SEP_LEN * LOG_EVENT_MAX_LEN + PREFIX_BUF_SIZE + SSE_SUFFIX_LEN <= TAIL_MAX_SIZE,
"the log cut in web_server.h must keep a worst case log event inside the tail ceiling");
if (!this->reserve_tail_(total)) {
if (sent != 0) {
// Part of the chunk is on the wire, so the stream is broken and the client has to go
ESP_LOGW(TAG, "Cannot buffer a %zu byte chunk, closing", total);
this->close();
} else {
this->tail_alloc_failed_(total); // nothing on the wire, the caller retries on the stall clock
}
return false;
}
uint8_t *dst = this->tail_.get();
std::memcpy(dst, prefix, prefix_len);
dst += prefix_len;
for_each_chunk_piece(
message, message_len,
[](void *ctx, const char *piece, size_t len) {
auto &out = *static_cast<uint8_t **>(ctx);
std::memcpy(out, piece, len);
out += len;
},
&dst);
this->tail_len_ = total;
this->tail_sent_ = sent;
return true;
}
void AsyncEventSourceResponse::loop() {
AsyncStreamResponse::loop();
if (this->is_closing()) {
if (this->tail_cap_ != 0 || this->deferred_queue_.capacity() != 0) {
this->release_buffers_();
}
return;
}
drain_tail_();
process_deferred_queue_();
if (this->is_closing())
return;
// One step per loop; refusals retry next pass
this->entities_iterator_.try_advance(1);
}
#if defined(__GNUC__) && !defined(__clang__) && __GNUC__ >= 14 && defined(__OPTIMIZE_SIZE__)
#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wstack-usage=" // the one frame that holds the JSON arena
#endif
AsyncEventSourceResponse::JsonPrepareResult AsyncEventSourceResponse::prepare_json_(void *source,
message_generator_t *generator,
char *buffer, size_t *size_out) {
// Log lines emitted while the generator runs are dropped for this session: on the large path they would fill
// tail_ before it is overwritten, and on the small path they would go out ahead of the event being built.
SendGuard guard{*this};
json::JsonArena<json::JSON_ARENA_SIZE> arena;
json::JsonBuilder builder(&arena);
generator(this->web_server_, source, builder);
*size_out = builder.serialize_to(buffer, JSON_BUF_SIZE);
if (*size_out < JSON_BUF_SIZE) {
return JsonPrepareResult::JSON_PREPARE_RESULT_BUFFER;
}
char prefix[PREFIX_BUF_SIZE];
const size_t prefix_len = build_chunk_prefix(prefix, sizeof(prefix), "state", 0, 0, true);
// Nothing reaches the wire until the complete document and its framing are owned by the caller.
size_t json_len = 0;
size_t cap = std::max<size_t>(JSON_BUF_SIZE * 2, this->tail_cap_);
for (;;) {
if (!this->reserve_tail_(cap)) {
*size_out = cap;
return JsonPrepareResult::JSON_PREPARE_RESULT_NO_MEMORY;
}
const size_t room = cap - prefix_len - SSE_SUFFIX_LEN;
json_len = builder.serialize_to(reinterpret_cast<char *>(this->tail_.get()) + prefix_len, room);
if (json_len < room) {
break;
}
if (cap >= TAIL_MAX_SIZE) {
*size_out = room;
return JsonPrepareResult::JSON_PREPARE_RESULT_TOO_LARGE;
}
cap = std::min<size_t>(cap * 2, TAIL_MAX_SIZE);
}
const size_t total = prefix_len + json_len + SSE_SUFFIX_LEN;
write_chunk_header(prefix, total - CHUNK_HDR_LEN - CHUNK_END_LEN);
uint8_t *dst = this->tail_.get();
std::memcpy(dst, prefix, prefix_len);
std::memcpy(dst + prefix_len + json_len, SSE_SUFFIX, SSE_SUFFIX_LEN);
this->tail_len_ = total;
this->tail_sent_ = 0;
return JsonPrepareResult::JSON_PREPARE_RESULT_TAIL;
}
#if defined(__GNUC__) && !defined(__clang__) && __GNUC__ >= 14 && defined(__OPTIMIZE_SIZE__)
#pragma GCC diagnostic pop
#endif
bool AsyncEventSourceResponse::send_json_(void *source, message_generator_t *generator) {
if (!this->ready_to_send_()) {
return false;
}
char buffer[JSON_BUF_SIZE];
size_t size;
const auto result = this->prepare_json_(source, generator, buffer, &size);
switch (result) {
case JsonPrepareResult::JSON_PREPARE_RESULT_BUFFER:
return this->try_send_nodefer(buffer, size, "state");
case JsonPrepareResult::JSON_PREPARE_RESULT_TAIL:
this->drain_tail_();
return true;
case JsonPrepareResult::JSON_PREPARE_RESULT_NO_MEMORY: {
SendGuard guard{*this};
this->tail_alloc_failed_(size);
return false;
}
case JsonPrepareResult::JSON_PREPARE_RESULT_TOO_LARGE: {
SendGuard guard{*this};
ESP_LOGW(TAG, "State event does not fit %zu bytes, dropped", size);
this->tail_.reset();
this->tail_cap_ = 0;
this->stall_.clear();
this->no_memory_logged_ = false;
return true;
}
}
return false;
}
void AsyncEventSourceResponse::tail_alloc_failed_(size_t cap) {
// Same stall clock as a socket that stops draining, so a session cannot retry forever. A refused write may
// already have started it, so the warning keeps its own flag.
if (!this->no_memory_logged_) {
this->no_memory_logged_ = true;
ESP_LOGW(TAG, "No memory for a %zu byte chunk", cap);
}
const uint32_t now = App.get_loop_component_start_time();
if (!this->stall_.is_running()) {
this->stall_.start(now);
return;
}
if (this->stall_.is_expired(now, SEND_STALL_TIMEOUT_MS)) {
ESP_LOGW(TAG, "Closing EventSource after %" PRIu32 " ms without memory", this->stall_.elapsed(now));
this->close();
}
}
bool AsyncEventSourceResponse::ready_to_send_() {
if (this->sending_ || !this->is_connected() || this->is_closing()) {
return false;
}
drain_tail_();
return !this->is_closing() && this->tail_len_ == 0;
}
bool AsyncEventSourceResponse::try_send_nodefer(const char *message, size_t message_len, const char *event, uint32_t id,
uint32_t reconnect) {
if (!this->ready_to_send_()) {
return false;
}
SendGuard guard{*this};
// Everything after the prefix goes out straight from the caller's buffer
char prefix[PREFIX_BUF_SIZE];
const size_t prefix_len = build_chunk_prefix(prefix, sizeof(prefix), event, id, reconnect, message != nullptr);
if (message == nullptr && prefix_len == CHUNK_HDR_LEN) {
return true; // Match ESPAsyncWebServer: nothing to send
}
if (prefix_len >= PREFIX_BUF_SIZE - 1) {
// The appenders truncate silently, which would put a malformed event on the wire
ESP_LOGW(TAG, "Event name too long, dropped");
return true;
}
// Gather list: the prefix, then the data lines and their separators from the message
struct Gather {
web_server_base::StreamIovec iov[MAX_SEND_IOV]; // left uninitialized on purpose
int iovcnt{1};
size_t total{0};
bool fits{true};
} g;
g.total = prefix_len;
for_each_chunk_piece(
message, message_len,
[](void *ctx, const char *piece, size_t len) {
auto &g = *static_cast<Gather *>(ctx);
g.total += len;
if (len == 0) {
return;
}
if (g.iovcnt == MAX_SEND_IOV) {
g.fits = false;
return;
}
g.iov[g.iovcnt++] = {const_cast<char *>(piece), len};
},
&g);
// The header and the terminator are not part of the chunk length
write_chunk_header(prefix, g.total - CHUNK_HDR_LEN - CHUNK_END_LEN);
g.iov[0] = {prefix, prefix_len};
// A message with more lines than the list holds skips straight to the tail
const ssize_t sent = g.fits ? this->write(g.iov, g.iovcnt) : 0;
if (sent < 0) {
return false;
}
if (sent > 0) {
this->no_memory_logged_ = false;
}
if (static_cast<size_t>(sent) == g.total) {
return true;
}
// The caller's buffers do not outlive this call, so keep the chunk and continue from loop()
return this->stash_chunk_(prefix, prefix_len, message, message_len, g.total, sent);
}
void AsyncEventSourceResponse::deferrable_send_state(void *source, bool detail_all,
message_generator_t *message_generator) {
// Let the detail_all listing finish before bare state events, so the web UI never shows unnamed entries and
// initial connect sends less
if (!detail_all && !this->entities_iterator_.completed())
return;
drain_tail_();
process_deferred_queue_();
if (this->is_closing()) {
return;
}
if (this->tail_len_ != 0 || !deferred_queue_.empty()) {
// outgoing event buffer or deferred queue still not empty which means downstream tcp send buffer full, no point
// trying to send first
deq_push_back_with_dedup_(source, message_generator);
} else {
// A send error closes the session and clears the queue; nothing is queued after that
if (!this->send_json_(source, message_generator) && !this->is_closing()) {
deq_push_back_with_dedup_(source, message_generator);
}
}
}
} // namespace esphome::web_server
#endif // USE_ESP32 && USE_WEBSERVER
@@ -0,0 +1,176 @@
#pragma once
#include "esphome/core/defines.h"
#if defined(USE_ESP32) && defined(USE_WEBSERVER)
#include "esphome/components/json/json_util.h"
#include "esphome/components/web_server_base/stream_response.h"
#include "esphome/core/helpers.h"
#include "esphome/core/string_ref.h"
#include "list_entities.h"
#include "sse_chunk.h"
#include <atomic>
#include <cstdint>
#include <vector>
namespace esphome::web_server {
class WebServer;
class AsyncEventSource;
class AsyncEventSourceResponse;
using message_generator_t = void(WebServer *, void *, json::JsonBuilder &);
/*
This class holds a pointer to the source component that wants to publish a state event, and a pointer to a function
that will lazily generate that event. The two pointers allow dedup in the deferred queue if multiple publishes for
the same component are backed up, and take up only two pointers of memory. The entry in the deferred queue (a
std::vector) is the DeferredEvent instance itself (not a pointer to one elsewhere in heap) so still only two pointers
per entry (and no heap fragmentation). Even 100 backed up events (you'd have to have at least 100 sensors publishing
because of dedup) would take up only 0.8 kB.
*/
struct DeferredEvent {
friend class AsyncEventSourceResponse;
protected:
void *source_;
message_generator_t *message_generator_;
public:
DeferredEvent(void *source, message_generator_t *message_generator)
: source_(source), message_generator_(message_generator) {}
bool operator==(const DeferredEvent &test) const {
return (source_ == test.source_ && message_generator_ == test.message_generator_);
}
};
static_assert(sizeof(DeferredEvent) == sizeof(void *) + sizeof(message_generator_t *),
"DeferredEvent should have no padding");
class AsyncEventSourceResponse : public web_server_base::AsyncStreamResponse {
friend class AsyncEventSource;
public:
bool try_send_nodefer(const char *message, size_t message_len, const char *event = nullptr, uint32_t id = 0,
uint32_t reconnect = 0);
// detail_all events go out while entities are still being listed; state events wait until listing is done
void deferrable_send_state(void *source, bool detail_all, message_generator_t *message_generator);
// The old event name argument would otherwise convert to true
void deferrable_send_state(void *source, const char *event_type, message_generator_t *message_generator) = delete;
void loop();
protected:
AsyncEventSourceResponse(AsyncWebServerRequest *request, AsyncEventSource *server, WebServer *ws);
// Main-loop only: sends initial ping/config/sorting_groups, starts entity iterator.
void start_session_main_loop_();
void deq_push_back_with_dedup_(void *source, message_generator_t *message_generator);
void process_deferred_queue_();
// A new chunk may go out: not re-entered from a log line, session alive, tail empty
bool ready_to_send_();
// Push what is left of the chunk in tail_ to the socket
void drain_tail_();
// Grow tail_ to hold len bytes, kept at its high-water mark. False on OOM.
bool reserve_tail_(size_t len);
// Keep the whole chunk in tail_ and continue from sent; false when the tail cannot be allocated.
bool stash_chunk_(const char *prefix, size_t prefix_len, const char *message, size_t message_len, size_t total,
size_t sent);
// Send a state event; JSON too large for the stack buffer is serialized into tail_ instead
bool send_json_(void *source, message_generator_t *generator);
enum class JsonPrepareResult : uint8_t {
JSON_PREPARE_RESULT_BUFFER,
JSON_PREPARE_RESULT_TAIL,
JSON_PREPARE_RESULT_NO_MEMORY,
JSON_PREPARE_RESULT_TOO_LARGE,
};
// Keep the arena out of the caller's frame so it is gone before the state send.
// size_out: BUFFER = JSON length, NO_MEMORY = requested tail capacity, TOO_LARGE = available JSON room;
// unused for TAIL.
JsonPrepareResult __attribute__((noinline)) prepare_json_(void *source, message_generator_t *generator, char *buffer,
size_t *size_out);
// Warn once per stall, and close the session once the stall timeout passes with no memory for the tail
void tail_alloc_failed_(size_t cap);
// A log line emitted inside a send re-enters try_send_nodefer on this session; refuse it
struct SendGuard {
AsyncEventSourceResponse &owner;
explicit SendGuard(AsyncEventSourceResponse &owner) : owner(owner) { owner.sending_ = true; }
~SendGuard() { this->owner.sending_ = false; }
};
// Frees the tail and the queue's storage once the session is closing
void release_buffers_();
AsyncEventSource *server_;
std::vector<DeferredEvent> deferred_queue_;
WebServer *web_server_;
ListEntitiesIterator entities_iterator_;
// One chunk the socket did not take whole, allocated on the first stall; the only heap use
// on the send path
RAMUniquePtr<uint8_t[]> tail_;
uint16_t tail_cap_{0};
uint16_t tail_len_{0}; // Zero means nothing pending
uint16_t tail_sent_{0};
bool sending_{false};
// The no-memory warning went out during the current stall; cleared on send progress
bool no_memory_logged_{false};
// The longest multi line log message in the tree (a climate dump_config) has 22 lines; a
// longer one goes through the tail
static constexpr size_t MAX_SEND_LINES = 22;
static constexpr size_t MAX_SEND_IOV = 1 + 2 * MAX_SEND_LINES;
// Chunk header, retry/id/event lines and the first "data: "
static constexpr size_t PREFIX_BUF_SIZE = 128;
// Stack buffer for a state event's JSON; a larger document is serialized into the tail
static constexpr size_t JSON_BUF_SIZE = 1024;
// Same ceiling JsonBuilder::serialize() applies (max_heap_size in json_util.cpp); a larger
// document is dropped before anything is on the wire
static constexpr size_t JSON_MAX_SIZE = 5120;
// Most RAM a stalled session keeps: the largest state document plus any accepted framing
static constexpr size_t TAIL_MAX_SIZE = JSON_MAX_SIZE + PREFIX_BUF_SIZE + SSE_SUFFIX_LEN;
};
class AsyncEventSource : public AsyncWebHandler {
friend class AsyncEventSourceResponse;
public:
AsyncEventSource(StringRef url, WebServer *ws) : url_(url), web_server_(ws) {}
~AsyncEventSource() override;
// NOLINTNEXTLINE(readability-identifier-naming)
bool canHandle(AsyncWebServerRequest *request) const override {
if (request->method() != HTTP_GET)
return false;
char url_buf[AsyncWebServerRequest::URL_BUF_SIZE];
return request->url_to(url_buf) == this->url_;
}
// NOLINTNEXTLINE(readability-identifier-naming)
void handleRequest(AsyncWebServerRequest *request) override;
void try_send_nodefer(const char *message, size_t message_len, const char *event = nullptr, uint32_t id = 0,
uint32_t reconnect = 0);
void deferrable_send_state(void *source, bool detail_all, message_generator_t *message_generator);
// The old event name argument would otherwise convert to true
void deferrable_send_state(void *source, const char *event_type, message_generator_t *message_generator) = delete;
/// Returns true if there are sessions remaining (including pending cleanup).
bool loop();
bool empty() { return this->count() == 0; }
size_t count() const { return this->sessions_.size(); }
protected:
// Cold path: move sessions from pending_sessions_ into sessions_ and greet each one.
void __attribute__((noinline, cold)) adopt_pending_sessions_main_loop_();
StringRef url_; // Must outlive this object (string literal)
// Main-loop only. Vector: SSE sessions are 1-5 connections, linear search beats set.
std::vector<AsyncEventSourceResponse *> sessions_;
// Httpd-task intake; guarded by pending_mutex_, gated by has_pending_sessions_.
std::vector<AsyncEventSourceResponse *> pending_sessions_;
Mutex pending_mutex_;
WebServer *web_server_;
std::atomic<bool> has_pending_sessions_{false};
};
} // namespace esphome::web_server
#endif // USE_ESP32 && USE_WEBSERVER
@@ -8,14 +8,8 @@
namespace esphome::web_server {
#ifdef USE_ESP32
ListEntitiesIterator::ListEntitiesIterator(const WebServer *ws, AsyncEventSource *es) : web_server_(ws), events_(es) {}
#elif USE_ARDUINO
ListEntitiesIterator::ListEntitiesIterator(const WebServer *ws, DeferredUpdateEventSource *es)
: web_server_(ws), events_(es) {}
#endif
void ListEntitiesIterator::send_all_(void *source, message_generator_t *generator) {
this->events_->deferrable_send_state(source, true, generator);
this->session_->deferrable_send_state(source, true, generator);
}
#ifdef USE_BINARY_SENSOR
+9 -18
View File
@@ -1,33 +1,28 @@
#pragma once
#include "esphome/core/defines.h"
#ifdef USE_WEBSERVER
#if defined(USE_WEBSERVER) && (defined(USE_ESP32) || defined(USE_ARDUINO))
#include "esphome/core/component.h"
#include "esphome/core/component_iterator.h"
namespace esphome::json {
class JsonBuilder;
} // namespace esphome::json
namespace esphome::web_server_idf {
#ifdef USE_ESP32
class AsyncEventSource;
#endif
} // namespace esphome::web_server_idf
namespace esphome::web_server {
#if !defined(USE_ESP32) && defined(USE_ARDUINO)
// The /events session the iterator lists entities to
#ifdef USE_ESP32
class AsyncEventSourceResponse;
using EventSession = AsyncEventSourceResponse;
#elif defined(USE_ARDUINO)
class DeferredUpdateEventSource;
using EventSession = DeferredUpdateEventSource;
#endif
class WebServer;
class ListEntitiesIterator final : public ComponentIterator {
public:
#ifdef USE_ESP32
ListEntitiesIterator(const WebServer *ws, esphome::web_server_idf::AsyncEventSource *es);
#elif defined(USE_ARDUINO)
ListEntitiesIterator(const WebServer *ws, DeferredUpdateEventSource *es);
#endif
ListEntitiesIterator(const WebServer *ws, EventSession *session) : web_server_(ws), session_(session) {}
// Entity overrides (generated from entity_types.h).
// Implementations live in list_entities.cpp.
@@ -45,11 +40,7 @@ class ListEntitiesIterator final : public ComponentIterator {
void send_all_(void *source, void (*generator)(WebServer *, void *, json::JsonBuilder &));
const WebServer *web_server_;
#ifdef USE_ESP32
esphome::web_server_idf::AsyncEventSource *events_;
#elif USE_ARDUINO
DeferredUpdateEventSource *events_;
#endif
EventSession *session_;
};
} // namespace esphome::web_server
@@ -5,7 +5,7 @@
#include "esphome/core/helpers.h"
namespace esphome::web_server_idf {
namespace esphome::web_server {
void for_each_chunk_piece(const char *message, size_t message_len, ChunkPieceSink sink, void *ctx) {
if (message == nullptr) {
@@ -63,4 +63,4 @@ void write_chunk_header(char *buf, size_t chunk_len) {
buf[9] = '\n';
}
} // namespace esphome::web_server_idf
} // namespace esphome::web_server
@@ -3,7 +3,9 @@
#include <cstddef>
#include <cstdint>
namespace esphome::web_server_idf {
#include "esphome/components/web_server_base/stream_head.h"
namespace esphome::web_server {
// Wire framing of one Server-Sent-Events event inside the chunked /events response:
// "%08x\r\n" + fields + "data: " line ["\r\ndata: " line]... "\r\n" + "\r\n" + "\r\n"
@@ -22,6 +24,17 @@ constexpr size_t SSE_SEP_LEN = sizeof(SSE_SEP) - 1;
constexpr char SSE_SUFFIX[] = "\r\n\r\n\r\n";
constexpr size_t SSE_SUFFIX_LEN = sizeof(SSE_SUFFIX) - 1;
// Status, Content-Type and headers of the /events response head
constexpr char SSE_STATUS[] = "200 OK";
constexpr char SSE_CONTENT_TYPE[] = "text/event-stream";
constexpr web_server_base::HttpHeader SSE_HEADERS[] = {
{"Transfer-Encoding", "chunked"},
{"Cache-Control", "no-cache"},
{"Connection", "keep-alive"},
};
// Hand-framed first chunk holding an empty event (one blank line), as httpd_resp_send_chunk() sent it before
constexpr char SSE_FIRST_CHUNK[] = "2\r\n\r\n\r\n";
// Receives one piece of the chunk; ctx is whatever the caller passed to for_each_chunk_piece()
using ChunkPieceSink = void (*)(void *ctx, const char *piece, size_t len);
@@ -37,4 +50,4 @@ size_t build_chunk_prefix(char *buf, size_t size, const char *event, uint32_t id
// Fills the header placeholder at the start of buf once the chunk length is known
void write_chunk_header(char *buf, size_t chunk_len);
} // namespace esphome::web_server_idf
} // namespace esphome::web_server
+1 -1
View File
@@ -273,7 +273,7 @@ void DeferredUpdateEventSource::process_deferred_queue_() {
deferred_queue_.erase(deferred_queue_.begin());
this->consecutive_send_failures_ = 0; // Reset failure count on successful send
} else {
// NOTE: Similar logic exists in web_server_idf/web_server_idf.cpp in AsyncEventSourceResponse::drain_tail_().
// NOTE: Similar logic exists in web_server_idf/web_server_idf.cpp in AsyncStreamResponse::write().
// The close mechanisms are platform-specific (this path calls close() directly; the IDF path is time-based and
// closes through HTTPD to preserve session ownership), but both drop a client after roughly 20 seconds without
// send progress. Keep that stall policy in sync when changing either side.
+5 -1
View File
@@ -5,7 +5,11 @@
#include "esphome/components/json/json_util.h"
#include "esphome/components/web_server_base/web_server_base.h"
#include "esphome/components/web_server_base/captive_dns.h"
#ifdef USE_WEBSERVER
// The host only builds sse_chunk.cpp for unit tests, but other tests can define USE_WEBSERVER there
#if defined(USE_WEBSERVER) && (defined(USE_ESP32) || defined(USE_ARDUINO))
#ifdef USE_ESP32
#include "event_source.h"
#endif
#include "esphome/core/component.h"
#include "esphome/core/entity_base.h"
#include "esphome/core/entity_includes.h"
@@ -105,9 +105,9 @@ async def to_code(config: ConfigType) -> None:
var = cg.new_Pvariable(config[CONF_ID])
cg.add(cg.RawExpression(f"{web_server_base_ns}::global_web_server_base = {var}"))
# Default headers added in init(): web_server_idf's StaticVector, or the library's DefaultHeaders
cg.add_define("WEB_SERVER_DEFAULT_HEADERS_COUNT", 1)
if CORE.is_esp32:
# Count for StaticVector in web_server_idf - matches headers added in init()
cg.add_define("WEB_SERVER_DEFAULT_HEADERS_COUNT", 1)
return
# ESP32 uses IDF web server (early return above), so this is for other Arduino platforms
@@ -0,0 +1,126 @@
#pragma once
#include <cstddef>
#include <cstdint>
#include <cstring>
#include <span>
#include <strings.h>
// Pure helpers of AsyncStreamResponse, kept free of httpd and lwIP so they build on the host.
// The iovec type is a template parameter: lwIP defines its own on the device, POSIX on the host.
namespace esphome::web_server_base {
struct HttpHeader {
const char *name;
const char *value;
};
/// Drops the first sent bytes from a gather list after a write; a short write resumes inside the
/// element it stopped in. count reaches 0 once everything went out.
template<typename Iovec> void advance_iovecs(Iovec *&iov, int &count, size_t sent) {
while (count > 0 && sent >= iov->iov_len) {
sent -= iov->iov_len;
++iov;
--count;
}
if (count > 0) {
iov->iov_base = static_cast<char *>(iov->iov_base) + sent;
iov->iov_len -= sent;
}
}
/// Pieces in a head: 4 for the status line and Content-Type, 4 per header, the blank line and the body
constexpr size_t stream_head_iov_count(size_t header_count) { return 4 + 4 * header_count + 2; }
/// Pieces per batch: a head with up to four headers goes out in one write; 192 bytes on 32-bit
static constexpr int STREAM_HEAD_MAX_IOV = 24;
/// True if headers holds a header named name, compared case insensitively
inline bool has_header(std::span<const HttpHeader> headers, const char *name) {
for (const auto &header : headers) {
if (strcasecmp(header.name, name) == 0) {
return true;
}
}
return false;
}
/// Gathers the head of a stream response: "HTTP/1.1 <status>", Content-Type, headers, the default_headers that
/// headers does not replace, the blank line and body (may be null). send(Iovec *iov, int count) gets each full batch of
/// STREAM_HEAD_MAX_IOV pieces and then the rest; once it returns false the later pieces are skipped. Returns false if
/// any send failed.
template<typename Iovec, typename Send>
bool write_stream_head(Send &&send, const char *status, const char *content_type, std::span<const HttpHeader> headers,
std::span<const HttpHeader> default_headers, const char *body) {
struct Batch {
Send &send;
int count{0};
bool ok{true};
Iovec iov[STREAM_HEAD_MAX_IOV]; // left uninitialized on purpose
void flush() {
if (this->ok && this->count > 0) {
this->ok = this->send(this->iov, this->count);
}
this->count = 0;
}
void add(const char *buf, size_t len) {
if (this->count == STREAM_HEAD_MAX_IOV) {
this->flush();
}
this->iov[this->count++] = {const_cast<char *>(buf), len};
}
void add(const char *str) { this->add(str, strlen(str)); }
void add_header(const HttpHeader &header) {
this->add("\r\n", 2);
this->add(header.name);
this->add(": ", 2);
this->add(header.value);
}
} batch{send};
batch.add("HTTP/1.1 ", 9);
batch.add(status);
batch.add("\r\nContent-Type: ", 16);
batch.add(content_type);
for (const auto &header : headers) {
batch.add_header(header);
}
for (const auto &header : default_headers) {
if (!has_header(headers, header.name)) {
batch.add_header(header);
}
}
batch.add("\r\n\r\n", 4);
if (body != nullptr) {
batch.add(body);
}
batch.flush();
return batch.ok;
}
/// How long sends have made no progress. Zero means not running, so a start at millis() == 0 records 1.
/// The comparison is wraparound safe across a millis() rollover.
class StallClock {
public:
/// Starts the clock; a running clock keeps its start time.
void start(uint32_t now) {
if (this->started_ms_ == 0) {
this->started_ms_ = now != 0 ? now : 1;
}
}
void clear() { this->started_ms_ = 0; }
bool is_running() const { return this->started_ms_ != 0; }
/// True once the clock has run for timeout_ms
bool is_expired(uint32_t now, uint32_t timeout_ms) const {
return this->started_ms_ != 0 && static_cast<int32_t>(now - (this->started_ms_ + timeout_ms)) >= 0;
}
uint32_t elapsed(uint32_t now) const { return now - this->started_ms_; }
protected:
uint32_t started_ms_{0};
};
} // namespace esphome::web_server_base
@@ -0,0 +1,16 @@
#pragma once
#include "esphome/core/defines.h"
// The long lived stream response the /events layer writes to, for the platform's web server
#ifdef USE_ESP32
#include "esphome/components/web_server_idf/web_server_idf.h"
#include <sys/socket.h>
namespace esphome::web_server_base {
using web_server_idf::AsyncStreamResponse;
// One entry of the gather list AsyncStreamResponse::write() takes
using StreamIovec = ::iovec;
} // namespace esphome::web_server_base
#endif
@@ -105,6 +105,9 @@ class WebServerBase final {
return;
}
this->server_ = new AsyncWebServer(this->port_);
#if defined(USE_ESP32) && defined(USE_WEBSERVER_SSL)
this->server_->set_ssl_credentials(this->ssl_certificate_, this->ssl_private_key_);
#endif
// All content is controlled and created by user - so allowing all origins is fine here.
// NOTE: Currently 1 header. If more are added, update in __init__.py:
// cg.add_define("WEB_SERVER_DEFAULT_HEADERS_COUNT", 1)
@@ -158,7 +161,21 @@ class WebServerBase final {
void set_port(uint16_t port) { port_ = port; }
uint16_t get_port() const { return port_; }
#if defined(USE_ESP32) && defined(USE_WEBSERVER_SSL)
/// PEM certificate and key for HTTPS. Not copied: they must stay valid. Set them, like the port, before init().
void set_ssl_credentials(const char *certificate, const char *private_key) {
this->ssl_certificate_ = certificate;
this->ssl_private_key_ = private_key;
if (this->server_ != nullptr)
this->server_->set_ssl_credentials(certificate, private_key); // used at the next init()
}
#endif
protected:
#if defined(USE_ESP32) && defined(USE_WEBSERVER_SSL)
const char *ssl_certificate_{nullptr};
const char *ssl_private_key_{nullptr};
#endif
uint8_t initialized_{0};
uint16_t port_{80}; // Keep in sync with DEFAULT_PORT in web_server/__init__.py
AsyncWebServer *server_{nullptr};
@@ -1,14 +1,19 @@
from dataclasses import dataclass, field
import logging
import esphome.codegen as cg
from esphome.components import socket, web_server_base
from esphome.components.esp32 import (
get_user_lwip_max_sockets,
idf_version,
include_builtin_idf_component,
request_tls,
require_mbedtls_tls_server,
set_idf_sdkconfig_default,
)
from esphome.config_helpers import filter_source_files_from_defines
import esphome.config_validation as cv
from esphome.core import CORE
from esphome.types import ConfigType
_LOGGER = logging.getLogger(__name__)
@@ -23,6 +28,46 @@ DEFAULT_HTTPD_MAX_OPEN_SOCKETS = 7
HTTPD_CONTROL_UDP_SOCKETS = 2
# ESP-IDF's HTTPS server allows this many open connections; each holds a TLS session.
HTTPS_MAX_CONNECTIONS = 4
@dataclass
class WebServerIdfData:
ssl: bool = False # a component serves HTTPS (consume_ssl_sockets() was called)
# Consumers that need plain HTTP (require_plain_http() was called)
plain_http: list[str] = field(default_factory=list)
def _get_data() -> WebServerIdfData:
if DOMAIN not in CORE.data:
CORE.data[DOMAIN] = WebServerIdfData()
return CORE.data[DOMAIN]
def consume_ssl_sockets(config: ConfigType) -> ConfigType:
"""Serve the shared web server over HTTPS.
The one thing a component that supplies its own certificate adds to its
schema; it then calls WebServerBase::set_ssl_credentials() before init().
This compiles HTTPS in and registers the connections it can hold open.
"""
data = _get_data()
# HTTPS exists only on ESP32. The listener is shared, so its clients are registered once.
if CORE.is_esp32 and not data.ssl:
web_server_base.add_httpd_clients(HTTPS_MAX_CONNECTIONS, DOMAIN)
data.ssl = True
return config
def require_plain_http(consumer: str) -> None:
"""Record that consumer needs the shared web server to speak plain HTTP.
Call during config validation; HTTPS is then rejected in final validation.
"""
_get_data().plain_http.append(consumer)
CONFIG_SCHEMA = cv.All(
cv.Schema({}),
cv.only_on_esp32,
@@ -37,6 +82,9 @@ def _httpd_max_open_sockets() -> int:
max_open = max(
DEFAULT_HTTPD_MAX_OPEN_SOCKETS, web_server_base.get_httpd_client_count()
)
# Each TLS session takes memory, so HTTPS keeps ESP-IDF's HTTPS connection limit.
if _get_data().ssl:
max_open = min(max_open, HTTPS_MAX_CONNECTIONS)
# Without a user override esp32 sizes CONFIG_LWIP_MAX_SOCKETS from the socket
# registry, which already holds the clients and the internal sockets.
if (user_max := get_user_lwip_max_sockets()) is None:
@@ -55,9 +103,32 @@ def _httpd_max_open_sockets() -> int:
return max_open
def _final_validate(config: ConfigType) -> None:
# Runs after every CONFIG_SCHEMA, so all require_plain_http() calls are in.
data = _get_data()
if not data.ssl:
return
# tls_handshake_timeout_ms, which keeps a stalled client from holding the server task, is new in 5.5.
if idf_version() < cv.Version(5, 5, 0):
raise cv.Invalid("HTTPS needs ESP-IDF 5.5 or later")
if data.plain_http:
names = ", ".join(data.plain_http)
raise cv.Invalid(f"HTTPS can't be used together with {names} yet")
FINAL_VALIDATE_SCHEMA = _final_validate
async def to_code(config: ConfigType) -> None:
# Runs after config validation, so every add_httpd_clients() call is counted.
cg.add_define("WEB_SERVER_MAX_OPEN_SOCKETS", _httpd_max_open_sockets())
if _get_data().ssl:
cg.add_define("USE_WEBSERVER_SSL")
request_tls()
# ESPHome builds mbedTLS client-only unless a component accepts TLS sessions.
require_mbedtls_tls_server()
include_builtin_idf_component("esp_https_server")
set_idf_sdkconfig_default("CONFIG_ESP_HTTPS_SERVER_ENABLE", True)
# Increase the maximum supported size of headers section in HTTP request packet to be processed by the server
set_idf_sdkconfig_default("CONFIG_HTTPD_MAX_REQ_HDR_LEN", 1024)
# Re-enable ESP-IDF's HTTP server (excluded by default to save compile time).
@@ -17,9 +17,16 @@
#include <freertos/task.h>
#include "utils.h"
#include "sse_chunk.h"
#include "web_server_idf.h"
#ifdef USE_WEBSERVER_SSL
#include <esp_https_server.h>
#include <freertos/semphr.h>
#include <esp_idf_version.h>
#include <mbedtls/pk.h>
#include <mbedtls/x509_crt.h>
#endif
#ifdef USE_WEBSERVER_AUTH_DIGEST
#include <esp_random.h>
#include <esp_rom_md5.h>
@@ -30,17 +37,15 @@
#include "multipart.h" // For parse_multipart_boundary and other utils
#endif
#ifdef USE_WEBSERVER
#include "esphome/components/web_server/web_server.h"
#include "esphome/components/web_server/list_entities.h"
#endif // USE_WEBSERVER
// Include socket headers after Arduino headers to avoid IPADDR_NONE/INADDR_NONE macro conflicts
#include <cerrno>
#include <sys/socket.h>
namespace esphome::web_server_idf {
using web_server_base::advance_iovecs;
using web_server_base::write_stream_head;
// Status strings not provided by esp_http_server.h
#ifndef HTTPD_401
#define HTTPD_401 "401 Unauthorized"
@@ -55,13 +60,10 @@ namespace esphome::web_server_idf {
#define HTTPD_422 "422 Unprocessable Entity"
#endif
#define CRLF_STR "\r\n"
#define CRLF_LEN (sizeof(CRLF_STR) - 1)
ESPHOME_LOG_TAG(TAG, "web_server_idf");
// Only prepare_json_() may hold the JSON arena: every other frame in this file is capped below one
// arena. Measured at -Os on GCC 14; newer toolchains stay checked on purpose, older ones skip it.
// Every frame in this file is capped below one JSON arena.
// Measured at -Os on GCC 14; newer toolchains stay checked on purpose, older ones skip it.
#if defined(__GNUC__) && !defined(__clang__) && __GNUC__ >= 14 && defined(__OPTIMIZE_SIZE__)
#pragma GCC diagnostic error "-Wstack-usage=2048"
#endif
@@ -140,6 +142,12 @@ void AsyncWebServer::safe_close_with_shutdown(httpd_handle_t hd, int sockfd) {
//
// See: https://github.com/esphome/esphome-webserver/issues/163
AsyncStreamResponse::socket_closing(hd, sockfd);
#ifdef USE_WEBSERVER_SSL
// Keeps shutdown_clients_() from acting on a descriptor freed, and its number reused, while it walks the list.
LockGuard lock(static_cast<AsyncWebServer *>(httpd_get_global_user_ctx(hd))->close_lock_);
#endif
// Attempt shutdown - ignore errors as socket may already be closed
shutdown(sockfd, SHUT_RD);
@@ -147,16 +155,144 @@ void AsyncWebServer::safe_close_with_shutdown(httpd_handle_t hd, int sockfd) {
close(sockfd);
}
#ifdef USE_WEBSERVER_SSL
namespace {
// Local port of a socket, or 0 if the descriptor is not an open socket.
uint16_t local_port(int fd) {
struct sockaddr_storage addr{};
socklen_t len = sizeof(addr);
if (getsockname(fd, reinterpret_cast<struct sockaddr *>(&addr), &len) != 0) {
return 0;
}
if (addr.ss_family == AF_INET) {
return ntohs(reinterpret_cast<struct sockaddr_in *>(&addr)->sin_port);
}
#if LWIP_IPV6
if (addr.ss_family == AF_INET6) {
return ntohs(reinterpret_cast<struct sockaddr_in6 *>(&addr)->sin6_port);
}
#endif
return 0;
}
// Stops the server for AsyncWebServer::end() on its own task and signals the caller when done.
// A semaphore, not a task notification: the main loop task already uses notifications to wake itself.
struct StopRequest {
httpd_handle_t server;
bool ssl;
esp_err_t result;
StaticSemaphore_t done_buffer;
SemaphoreHandle_t done;
};
// httpd_ssl_stop() also posts HTTPS_SERVER_EVENT_STOP, to match the start event httpd_ssl_start() posted.
esp_err_t stop_server(httpd_handle_t server, bool ssl) { return ssl ? httpd_ssl_stop(server) : httpd_stop(server); }
void stop_task(void *arg) {
auto *request = static_cast<StopRequest *>(arg);
request->result = stop_server(request->server, request->ssl);
xSemaphoreGive(request->done); // the request is not touched after this
vTaskDelete(nullptr);
}
} // namespace
void AsyncWebServer::context_free(void *ctx) {
// Waits for a running shutdown_clients_() pass, and makes later passes skip the freed server.
auto *server = static_cast<AsyncWebServer *>(ctx);
LockGuard lock(server->close_lock_);
server->running_port_ = 0;
}
void AsyncWebServer::shutdown_clients_() {
// Wakes the server task if it is blocked on a client. A just-closed client may still be listed with its number
// reused elsewhere, so only descriptors still bound to this server's port are shut down.
LockGuard lock(this->close_lock_);
if (this->running_port_ == 0) {
return; // the server is being freed (context_free() ran), so its client list is gone
}
int client_fds[CONFIG_LWIP_MAX_SOCKETS];
size_t client_count = CONFIG_LWIP_MAX_SOCKETS;
if (httpd_get_client_list(this->server_, &client_count, client_fds) != ESP_OK) {
return;
}
for (size_t i = 0; i < client_count; i++) {
if (local_port(client_fds[i]) == this->running_port_) {
shutdown(client_fds[i], SHUT_RDWR);
}
}
}
namespace {
#if ESP_IDF_VERSION < ESP_IDF_VERSION_VAL(6, 0, 0)
int ssl_random(void * /*unused*/, unsigned char *buf, size_t len) {
random_bytes(buf, len);
return 0;
}
#endif
// esp_https_server parses the credentials only when a client connects, so check the pair before starting.
// Lengths include the terminating null, as mbedTLS wants for PEM.
bool ssl_credentials_valid(const uint8_t *cert_pem, size_t cert_len, const uint8_t *key_pem, size_t key_len) {
mbedtls_x509_crt crt;
mbedtls_pk_context key;
mbedtls_x509_crt_init(&crt);
mbedtls_pk_init(&key);
bool valid = mbedtls_x509_crt_parse(&crt, cert_pem, cert_len) == 0;
#if ESP_IDF_VERSION >= ESP_IDF_VERSION_VAL(6, 0, 0)
// mbedTLS 4 (ESP-IDF 6) takes its randomness from PSA Crypto instead of an RNG argument.
valid = valid && mbedtls_pk_parse_key(&key, key_pem, key_len, nullptr, 0) == 0 &&
mbedtls_pk_check_pair(&crt.pk, &key) == 0;
#else
valid = valid && mbedtls_pk_parse_key(&key, key_pem, key_len, nullptr, 0, ssl_random, nullptr) == 0 &&
mbedtls_pk_check_pair(&crt.pk, &key, ssl_random, nullptr) == 0;
#endif
mbedtls_pk_free(&key);
mbedtls_x509_crt_free(&crt);
return valid;
}
} // namespace
#endif
void AsyncWebServer::end() {
if (this->server_) {
#ifdef USE_WEBSERVER_SSL
// A stalled TLS handshake or upload can hold the server task until the socket timeout. Stop on a helper task and
// keep shutting the clients down until it is done: a client accepted before the stop message arrives is listed
// during its handshake, so a later pass reaches it. Without memory for the task, stop directly.
StopRequest request{};
request.server = this->server_;
request.ssl = httpd_get_global_transport_ctx(this->server_) != nullptr; // only HTTPS has one
request.done = xSemaphoreCreateBinaryStatic(&request.done_buffer);
if (xTaskCreate(stop_task, "httpd_stop", 3072, &request, uxTaskPriorityGet(nullptr), nullptr) == pdPASS) {
do {
this->shutdown_clients_();
App.feed_wdt();
} while (xSemaphoreTake(request.done, pdMS_TO_TICKS(100)) != pdTRUE);
} else {
this->shutdown_clients_();
request.result = stop_server(this->server_, request.ssl);
}
const esp_err_t err = request.result;
if (err != ESP_OK) {
// The listener is still running; keep its handle so begin() doesn't start a second one beside it.
ESP_LOGE(TAG, "Web server failed to stop: %s", esp_err_to_name(err));
return;
}
#else
httpd_stop(this->server_);
this->server_ = nullptr;
#endif
this->server_ = nullptr; // stopping also frees an HTTPS server's TLS context
}
}
void AsyncWebServer::begin() {
if (this->server_) {
this->end();
#ifdef USE_WEBSERVER_SSL
if (this->server_) {
return; // the old listener failed to stop
}
#endif
}
// Allow room for SerializationBuffer's 640-byte stack buffer and HTTP request handling.
httpd_config_t config = HTTPD_DEFAULT_CONFIG();
@@ -171,7 +307,50 @@ void AsyncWebServer::begin() {
config.lru_purge_enable = true;
// Use custom close function that shuts down before closing to prevent lwIP race conditions
config.close_fn = AsyncWebServer::safe_close_with_shutdown;
if (httpd_start(&this->server_, &config) == ESP_OK) {
esp_err_t start_err;
#ifdef USE_WEBSERVER_SSL
// Lets close_fn find this server's lock. httpd calls the free function on stop just before freeing the server.
config.global_user_ctx = this;
config.global_user_ctx_free_fn = AsyncWebServer::context_free;
if ((this->ssl_certificate_ == nullptr) != (this->ssl_private_key_ == nullptr)) {
ESP_LOGE(TAG, "HTTPS needs both a certificate and a private key");
return;
}
if (this->ssl_certificate_ != nullptr) {
const auto *cert = reinterpret_cast<const uint8_t *>(this->ssl_certificate_);
const auto *key = reinterpret_cast<const uint8_t *>(this->ssl_private_key_);
const size_t cert_len = strlen(this->ssl_certificate_) + 1;
const size_t key_len = strlen(this->ssl_private_key_) + 1;
if (!ssl_credentials_valid(cert, cert_len, key, key_len)) {
ESP_LOGE(TAG, "HTTPS certificate or key is invalid, or the key does not match the certificate");
return;
}
httpd_ssl_config_t ssl_config = HTTPD_SSL_CONFIG_DEFAULT();
// The TLS handshake needs the larger stack of the HTTPS defaults.
config.stack_size = std::max(config.stack_size, ssl_config.httpd.stack_size);
ssl_config.httpd = config;
ssl_config.port_secure = this->port_;
// A client that connects and never completes the handshake holds the server task. The check runs after each
// 5 s receive timeout, so this ends it at the first one instead of after three (the 10 s default).
ssl_config.tls_handshake_timeout_ms = 4000;
ssl_config.servercert = cert;
ssl_config.servercert_len = cert_len;
ssl_config.prvtkey_pem = key;
ssl_config.prvtkey_len = key_len;
start_err = httpd_ssl_start(&this->server_, &ssl_config);
if (start_err != ESP_OK) {
ESP_LOGE(TAG, "HTTPS server failed to start: %s", esp_err_to_name(start_err));
}
} else {
start_err = httpd_start(&this->server_, &config);
}
#else
start_err = httpd_start(&this->server_, &config);
#endif
if (start_err == ESP_OK) {
#ifdef USE_WEBSERVER_SSL
this->running_port_ = this->port_;
#endif
const httpd_uri_t handler_get = {
.uri = "",
.method = HTTP_GET,
@@ -736,224 +915,84 @@ void AsyncResponseStream::printf(const char *fmt, ...) {
this->print(str);
}
#ifdef USE_WEBSERVER
AsyncEventSource::~AsyncEventSource() {
LockGuard guard{this->pending_mutex_};
for (auto *vec : {&this->sessions_, &this->pending_sessions_}) {
for (auto *ses : *vec) {
delete ses; // NOLINT(cppcoreguidelines-owning-memory)
}
}
}
void AsyncEventSource::handleRequest(AsyncWebServerRequest *request) {
// Httpd task: set up the live httpd_req_t and park the session; main loop does the rest.
// NOLINTNEXTLINE(cppcoreguidelines-owning-memory,clang-analyzer-cplusplus.NewDeleteLeaks)
auto *rsp = new AsyncEventSourceResponse(request, this, this->web_server_);
{
LockGuard guard{this->pending_mutex_};
this->pending_sessions_.push_back(rsp);
this->has_pending_sessions_.store(true, std::memory_order_release);
}
this->web_server_->enable_loop_soon_any_context();
}
// clang-analyzer traces a false-positive leak path from loop() through
// adopt_pending_sessions_main_loop_() into start_session_main_loop_() and
// finally ArduinoJson. Suppress along the entire in-our-code call chain.
// NOLINTBEGIN(clang-analyzer-cplusplus.NewDeleteLeaks)
bool AsyncEventSource::loop() {
// Fast path: one atomic load per tick. Slow path is out-of-line on connect.
if (this->has_pending_sessions_.load(std::memory_order_acquire)) {
this->adopt_pending_sessions_main_loop_();
}
// Clean up dead sessions safely
// This follows the ESP-IDF pattern where free_ctx marks resources as dead
// and the main loop handles the actual cleanup to avoid race conditions
for (size_t i = 0; i < this->sessions_.size();) {
auto *ses = this->sessions_[i];
// If the session has a dead socket (marked by destroy callback)
if (ses->safe_to_delete_()) {
// destroy() already logged the close with the fd; don't double-log here.
delete ses; // NOLINT(cppcoreguidelines-owning-memory)
// Remove by swapping with last element (O(1) removal, order doesn't matter for sessions)
this->sessions_[i] = this->sessions_.back();
this->sessions_.pop_back();
} else {
ses->loop();
++i;
}
}
// A session still waiting for httpd to commit its context keeps the loop alive too
return !this->sessions_.empty() || this->has_pending_sessions_.load(std::memory_order_acquire);
}
void AsyncEventSource::adopt_pending_sessions_main_loop_() {
std::vector<AsyncEventSourceResponse *> incoming;
{
LockGuard guard{this->pending_mutex_};
incoming.swap(this->pending_sessions_);
this->has_pending_sessions_.store(false, std::memory_order_relaxed);
}
for (auto *rsp : incoming) {
// Already disconnected? Drop it; skip session start on a dead session.
if (rsp->safe_to_delete_()) {
delete rsp; // NOLINT(cppcoreguidelines-owning-memory)
continue;
}
// httpd commits the session context only after the creating handler returns, so stay
// pending until then; httpd_req_cleanup() always commits it or calls destroy()
if (httpd_sess_get_ctx(rsp->hd_, rsp->fd_.load()) != rsp) {
LockGuard guard{this->pending_mutex_};
this->pending_sessions_.push_back(rsp);
this->has_pending_sessions_.store(true, std::memory_order_release);
continue;
}
this->sessions_.push_back(rsp);
rsp->start_session_main_loop_();
}
}
// NOLINTEND(clang-analyzer-cplusplus.NewDeleteLeaks)
void AsyncEventSource::try_send_nodefer(const char *message, size_t message_len, const char *event, uint32_t id,
uint32_t reconnect) {
for (auto *ses : this->sessions_) {
if (ses->fd_.load() != 0) { // Skip dead sessions
ses->try_send_nodefer(message, message_len, event, id, reconnect);
}
}
}
void AsyncEventSource::deferrable_send_state(void *source, bool detail_all, message_generator_t *message_generator) {
// Skip if no connected clients to avoid unnecessary processing
if (this->empty())
return;
for (auto *ses : this->sessions_) {
if (ses->fd_.load() != 0) { // Skip dead sessions
ses->deferrable_send_state(source, detail_all, message_generator);
}
}
}
AsyncEventSourceResponse::AsyncEventSourceResponse(const AsyncWebServerRequest *request,
esphome::web_server_idf::AsyncEventSource *server,
esphome::web_server::WebServer *ws)
: server_(server), web_server_(ws), entities_iterator_(ws, server) {
// Httpd task only. start_session_main_loop_() sends the greeting and starts the iterator.
void AsyncStreamResponse::begin(AsyncWebServerRequest *request, const char *status, const char *content_type,
std::span<const HttpHeader> headers, const char *body) {
httpd_req_t *req = *request;
httpd_resp_set_status(req, HTTPD_200);
httpd_resp_set_type(req, "text/event-stream");
httpd_resp_set_hdr(req, "Cache-Control", "no-cache");
httpd_resp_set_hdr(req, "Connection", "keep-alive");
DefaultHeaders::Instance().apply_(req);
httpd_resp_send_chunk(req, CRLF_STR, CRLF_LEN);
// Same head httpd_resp_send_chunk() writes, without its first chunk. Blocking sends: the session's non-blocking
// send override is not installed yet.
const int fd = httpd_req_to_sockfd(req);
const auto &defaults = DefaultHeaders::Instance().headers_;
const bool head_sent = write_stream_head<struct iovec>(
[fd](struct iovec *iov, int count) {
while (count > 0) {
struct msghdr msg{};
msg.msg_iov = iov;
msg.msg_iovlen = count;
const ssize_t sent = sendmsg(fd, &msg, 0);
if (sent <= 0) {
return false;
}
advance_iovecs(iov, count, sent);
}
return true;
},
status, content_type, headers, std::span<const HttpHeader>(defaults.data(), defaults.size()), body);
req->sess_ctx = this;
req->free_ctx = AsyncEventSourceResponse::destroy;
req->free_ctx = AsyncStreamResponse::destroy;
this->hd_ = req->handle;
this->fd_.store(httpd_req_to_sockfd(req));
this->fd_.store(fd);
// Use non-blocking send to prevent watchdog timeouts when TCP buffers are full
httpd_sess_set_send_override(this->hd_, this->fd_.load(), nonblocking_send);
httpd_sess_set_send_override(this->hd_, fd, nonblocking_send);
if (!head_sent) {
// A partly sent head must not be continued by later writes. close() is main loop only, so shut the socket
// down here; the first write() then fails and closes the stream, and httpd frees the session as usual.
shutdown(fd, SHUT_RDWR);
}
}
// NOLINTBEGIN(clang-analyzer-cplusplus.NewDeleteLeaks) false positive with ArduinoJson
void AsyncEventSourceResponse::start_session_main_loop_() {
auto *ws = this->web_server_;
// Held around a stream's ownership check and send, and while its socket is marked closed.
// Created at boot, so no allocation can fail once requests arrive.
static Mutex stream_send_mutex; // NOLINT(cppcoreguidelines-avoid-non-const-global-variables)
// The tcp send buffer is empty on connect. A refusal is a closing session or a failed tail
// allocation; nothing retries the greeting, so close and let the client reconnect.
auto message = ws->get_config_json();
if (!this->try_send_nodefer(message.c_str(), message.size(), "ping", millis(), 30000)) {
ESP_LOGW(TAG, "Config not sent to fd %d", this->fd_.load());
this->request_close_();
void AsyncStreamResponse::socket_closing(httpd_handle_t hd, int fd) {
// Only streams set a session context
auto *rsp = static_cast<AsyncStreamResponse *>(httpd_sess_get_ctx(hd, fd));
if (rsp == nullptr)
return;
}
#ifdef USE_WEBSERVER_SORTING
for (auto &group : ws->sorting_groups_) {
json::JsonBuilder builder;
JsonObject root = builder.root();
root["name"] = json::linked(group.second.name);
root["sorting_weight"] = group.second.weight;
message = builder.serialize();
// a (very) large number of these should be able to be queued initially without defer
// since the only thing in the send buffer at this point is the initial ping/config.
// A refusal means the socket is full or closing; the remaining groups are not sent. The
// session stays up on purpose: partial grouping beats a reconnect loop on a slow link.
if (!this->try_send_nodefer(message.c_str(), message.size(), "sorting_group")) {
ESP_LOGW(TAG, "Sorting groups not sent to fd %d", this->fd_.load());
break;
}
}
#endif
this->entities_iterator_.begin(ws->include_internal_);
LockGuard guard{stream_send_mutex};
rsp->socket_closed_ = true;
}
// NOLINTEND(clang-analyzer-cplusplus.NewDeleteLeaks)
void AsyncEventSourceResponse::destroy(void *ptr) {
auto *rsp = static_cast<AsyncEventSourceResponse *>(ptr);
void AsyncStreamResponse::destroy(void *ptr) {
auto *rsp = static_cast<AsyncStreamResponse *>(ptr);
int fd = rsp->fd_.exchange(0); // Atomically get and clear fd
ESP_LOGD(TAG, "Event source connection closed (fd: %d)", fd);
// Mark as dead - will be cleaned up in the main loop
// Note: We don't delete or remove from set here to avoid race conditions
ESP_LOGD(TAG, "Stream connection closed (fd: %d)", fd);
// Mark as dead - the owner deletes it from the main loop once safe_to_delete().
// httpd will call our custom close_fn (safe_close_with_shutdown) which handles
// shutdown() before close() to prevent lwIP race conditions
}
// helper for allowing only unique entries in the queue
void AsyncEventSourceResponse::deq_push_back_with_dedup_(void *source, message_generator_t *message_generator) {
DeferredEvent item(source, message_generator);
// Use range-based for loop instead of std::find_if to reduce template instantiation overhead and binary size
for (auto &event : this->deferred_queue_) {
if (event == item) {
return; // Already in queue, no need to update since items are equal
}
}
this->deferred_queue_.push_back(item);
}
void AsyncEventSourceResponse::process_deferred_queue_() {
if (this->close_requested_) {
return;
}
while (!deferred_queue_.empty()) {
DeferredEvent &de = deferred_queue_.front();
if (this->send_json_(de.source_, de.message_generator_)) {
if (this->close_requested_ || deferred_queue_.empty()) {
return;
}
// O(n) but memory efficiency is more important than speed here which is why std::vector was chosen
deferred_queue_.erase(deferred_queue_.begin());
} else {
break;
}
}
}
void AsyncEventSourceResponse::request_close_() {
void AsyncStreamResponse::close() {
if (!this->close_requested_) {
this->close_requested_ = true;
this->deferred_queue_.clear();
this->tail_.reset();
this->tail_cap_ = 0;
this->tail_len_ = 0;
this->tail_sent_ = 0;
this->next_close_attempt_ms_ = App.get_loop_component_start_time();
}
this->process_close_();
}
void AsyncEventSourceResponse::process_close_() {
void AsyncStreamResponse::loop() {
if (this->close_requested_) {
this->process_close_();
} else {
this->refresh_lru_();
}
}
void AsyncStreamResponse::process_close_() {
if (!this->close_requested_ || this->close_work_queued_.load(std::memory_order_acquire)) {
return;
}
@@ -971,7 +1010,7 @@ void AsyncEventSourceResponse::process_close_() {
// httpd_sess_trigger_close() queues only a reusable fd/session slot and can
// therefore close a new client if the original peer disconnects meanwhile.
this->close_work_queued_.store(true, std::memory_order_release);
const esp_err_t err = httpd_queue_work(this->hd_, &AsyncEventSourceResponse::close_session_work, this);
const esp_err_t err = httpd_queue_work(this->hd_, &AsyncStreamResponse::close_session_work, this);
this->next_close_attempt_ms_ = now + (err == ESP_OK ? CLOSE_CONFIRM_INTERVAL_MS : CLOSE_RETRY_INTERVAL_MS);
if (err == ESP_OK) {
return;
@@ -979,17 +1018,17 @@ void AsyncEventSourceResponse::process_close_() {
this->close_work_queued_.store(false, std::memory_order_release);
if (!this->close_retry_warning_logged_) {
ESP_LOGW(TAG, "Failed to queue EventSource close (%s); retrying", esp_err_to_name(err));
ESP_LOGW(TAG, "Failed to queue stream close (%s); retrying", esp_err_to_name(err));
this->close_retry_warning_logged_ = true;
}
}
void AsyncEventSourceResponse::refresh_lru_() {
// httpd only advances a session's LRU counter when a request arrives, and an event stream receives one
void AsyncStreamResponse::refresh_lru_() {
// httpd only advances a session's LRU counter when a request arrives, and a stream receives one
// request ever, so the purge that frees a socket for a new client would evict the live streams first.
// The HTTPD task owns the session table, so the refresh runs there.
// A stalled stream is the socket the purge should take, and a zeroed fd has no session left to refresh.
if (this->send_failure_started_ms_ != 0 || this->fd_.load() == 0)
if (this->stall_.is_running() || this->fd_.load() == 0)
return;
const uint32_t now = App.get_loop_component_start_time();
if (now - this->last_lru_refresh_ms_ < LRU_REFRESH_INTERVAL_MS ||
@@ -998,13 +1037,13 @@ void AsyncEventSourceResponse::refresh_lru_() {
}
this->last_lru_refresh_ms_ = now; // a full queue also waits for the next interval
this->lru_work_queued_.store(true, std::memory_order_release);
if (httpd_queue_work(this->hd_, &AsyncEventSourceResponse::refresh_lru_work, this) != ESP_OK) {
if (httpd_queue_work(this->hd_, &AsyncStreamResponse::refresh_lru_work, this) != ESP_OK) {
this->lru_work_queued_.store(false, std::memory_order_release);
}
}
void AsyncEventSourceResponse::refresh_lru_work(void *arg) {
auto *response = static_cast<AsyncEventSourceResponse *>(arg);
void AsyncStreamResponse::refresh_lru_work(void *arg) {
auto *response = static_cast<AsyncStreamResponse *>(arg);
const int fd = response->fd_.load();
if (response->owns_session_(fd)) {
httpd_sess_update_lru_counter(response->hd_, fd);
@@ -1013,8 +1052,8 @@ void AsyncEventSourceResponse::refresh_lru_work(void *arg) {
response->lru_work_queued_.store(false, std::memory_order_release);
}
void AsyncEventSourceResponse::close_session_work(void *arg) {
auto *response = static_cast<AsyncEventSourceResponse *>(arg);
void AsyncStreamResponse::close_session_work(void *arg) {
auto *response = static_cast<AsyncStreamResponse *>(arg);
const int fd = response->fd_.load();
if (response->owns_session_(fd)) {
// The HTTPD task remains the session owner. Shutting the socket down makes
@@ -1026,320 +1065,61 @@ void AsyncEventSourceResponse::close_session_work(void *arg) {
response->close_work_queued_.store(false, std::memory_order_release);
}
ssize_t AsyncEventSourceResponse::send_(struct iovec *iov, int iovcnt) {
// httpd frees a session before closing its socket, so the fd may already be a new client's.
ssize_t AsyncStreamResponse::write(struct iovec *iov, int iovcnt) {
if (this->close_requested_) {
return -1;
}
// Nothing to send is not a stall
size_t total = 0;
for (int i = 0; i < iovcnt; i++) {
total += iov[i].iov_len;
}
if (total == 0) {
return 0;
}
// httpd closes the socket before it clears the session, so the fd number may already be a new client's.
// Treated as would-block; the stall timer ends a session that never becomes ours again.
const int fd = this->fd_.load();
if (!this->owns_session_(fd)) {
return 0;
ssize_t sent = 0;
int err = 0;
{
// Held across the check and the send, so socket_closing() cannot close the socket in between
LockGuard guard{stream_send_mutex};
if (!this->socket_closed_ && this->owns_session_(fd)) {
struct msghdr msg{};
msg.msg_iov = iov;
msg.msg_iovlen = iovcnt;
sent = sendmsg(fd, &msg, MSG_DONTWAIT);
err = errno;
}
}
struct msghdr msg{};
msg.msg_iov = iov;
msg.msg_iovlen = iovcnt;
const ssize_t sent = sendmsg(fd, &msg, MSG_DONTWAIT);
if (sent >= 0) {
if (sent > 0) {
this->stall_.clear();
return sent;
}
const int err = errno;
if (err == EAGAIN || err == EWOULDBLOCK) {
return 0;
if (sent < 0 && err != EAGAIN && err != EWOULDBLOCK) {
ESP_LOGD(TAG, "send error: errno %d", err);
// A hard error is not a timeout, so is_stalled() must not report one
this->stall_.clear();
this->close();
return -1;
}
ESP_LOGD(TAG, "send error: errno %d", err);
this->request_close_();
return -1;
}
void AsyncEventSourceResponse::drain_tail_() {
if (this->sending_ || this->close_requested_ || this->tail_len_ == 0) {
return;
}
SendGuard guard{*this};
const size_t remaining = this->tail_len_ - this->tail_sent_;
struct iovec iov = {this->tail_.get() + this->tail_sent_, remaining};
const ssize_t sent = this->send_(&iov, 1);
if (sent < 0) {
return;
}
if (sent == 0) {
// Socket buffer full, try again later
// NOTE: Similar logic exists in web_server/web_server.cpp in DeferredUpdateEventSource::process_deferred_queue_().
// The IDF path is intentionally time-based and closes through HTTPD to preserve session ownership.
const uint32_t now = App.get_loop_component_start_time();
if (this->send_failure_started_ms_ == 0) {
this->send_failure_started_ms_ = now != 0 ? now : 1; // Reserve zero for no stall.
}
if (static_cast<int32_t>(now - (this->send_failure_started_ms_ + SEND_STALL_TIMEOUT_MS)) >= 0) {
ESP_LOGW(TAG, "Closing stuck EventSource connection after %" PRIu32 " ms without send progress",
now - this->send_failure_started_ms_);
this->request_close_();
}
return;
}
this->send_failure_started_ms_ = 0;
this->tail_sent_ += sent;
if (this->tail_sent_ < this->tail_len_) {
ESP_LOGV(TAG, "Partial send: %zd/%zu bytes (total: %u/%u)", sent, remaining, this->tail_sent_, this->tail_len_);
return;
}
// Fully sent; the storage stays for the next stall
this->tail_len_ = 0;
}
bool AsyncEventSourceResponse::reserve_tail_(size_t len) {
if (this->tail_cap_ >= len) {
return true;
}
if (len > TAIL_MAX_SIZE) {
return false;
}
// Nothing is pending while the tail grows, so free the old block first. PREFER_INTERNAL keeps
// the tail where plain new put it.
this->tail_.reset();
this->tail_ = RAMAllocator<uint8_t>(RAMAllocator<uint8_t>::PREFER_INTERNAL).make_unique_array_for_overwrite(len);
this->tail_cap_ = this->tail_ ? len : 0;
return this->tail_cap_ != 0;
}
bool AsyncEventSourceResponse::stash_chunk_(const char *prefix, size_t prefix_len, const char *message,
size_t message_len, size_t total, size_t sent) {
// A log event of nothing but line breaks is the largest chunk; reserve_tail_() must never
// refuse it for size, only for memory
static_assert(SSE_SEP_LEN * web_server::LOG_EVENT_MAX_LEN + PREFIX_BUF_SIZE + SSE_SUFFIX_LEN <= TAIL_MAX_SIZE,
"the log cut in web_server.h must keep a worst case log event inside the tail ceiling");
if (!this->reserve_tail_(total)) {
if (sent != 0) {
// Part of the chunk is on the wire, so the stream is broken and the client has to go
ESP_LOGW(TAG, "Cannot buffer a %zu byte chunk, closing", total);
this->request_close_();
} else {
this->tail_alloc_failed_(total); // nothing on the wire, the caller retries on the stall clock
}
return false;
}
uint8_t *dst = this->tail_.get();
std::memcpy(dst, prefix, prefix_len);
dst += prefix_len;
for_each_chunk_piece(
message, message_len,
[](void *ctx, const char *piece, size_t len) {
auto &out = *static_cast<uint8_t **>(ctx);
std::memcpy(out, piece, len);
out += len;
},
&dst);
this->tail_len_ = total;
this->tail_sent_ = sent;
return true;
}
void AsyncEventSourceResponse::loop() {
if (this->close_requested_) {
this->process_close_();
return;
}
this->refresh_lru_();
drain_tail_();
process_deferred_queue_();
if (this->close_requested_)
return;
// One step per loop; refusals retry next pass
this->entities_iterator_.try_advance(1);
}
#if defined(__GNUC__) && !defined(__clang__) && __GNUC__ >= 14 && defined(__OPTIMIZE_SIZE__)
#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wstack-usage=" // the one frame that holds the JSON arena
#endif
AsyncEventSourceResponse::JsonPrepareResult AsyncEventSourceResponse::prepare_json_(void *source,
message_generator_t *generator,
char *buffer, size_t *size_out) {
// Log lines emitted while the generator runs are dropped for this session: on the large path they would fill
// tail_ before it is overwritten, and on the small path they would go out ahead of the event being built.
SendGuard guard{*this};
json::JsonArena<json::JSON_ARENA_SIZE> arena;
json::JsonBuilder builder(&arena);
generator(this->web_server_, source, builder);
*size_out = builder.serialize_to(buffer, JSON_BUF_SIZE);
if (*size_out < JSON_BUF_SIZE) {
return JsonPrepareResult::JSON_PREPARE_RESULT_BUFFER;
}
char prefix[PREFIX_BUF_SIZE];
const size_t prefix_len = build_chunk_prefix(prefix, sizeof(prefix), "state", 0, 0, true);
// Nothing reaches the wire until the complete document and its framing are owned by the caller.
size_t json_len = 0;
size_t cap = std::max<size_t>(JSON_BUF_SIZE * 2, this->tail_cap_);
for (;;) {
if (!this->reserve_tail_(cap)) {
*size_out = cap;
return JsonPrepareResult::JSON_PREPARE_RESULT_NO_MEMORY;
}
const size_t room = cap - prefix_len - SSE_SUFFIX_LEN;
json_len = builder.serialize_to(reinterpret_cast<char *>(this->tail_.get()) + prefix_len, room);
if (json_len < room) {
break;
}
if (cap >= TAIL_MAX_SIZE) {
*size_out = room;
return JsonPrepareResult::JSON_PREPARE_RESULT_TOO_LARGE;
}
cap = std::min<size_t>(cap * 2, TAIL_MAX_SIZE);
}
const size_t total = prefix_len + json_len + SSE_SUFFIX_LEN;
write_chunk_header(prefix, total - CHUNK_HDR_LEN - CHUNK_END_LEN);
uint8_t *dst = this->tail_.get();
std::memcpy(dst, prefix, prefix_len);
std::memcpy(dst + prefix_len + json_len, SSE_SUFFIX, SSE_SUFFIX_LEN);
this->tail_len_ = total;
this->tail_sent_ = 0;
return JsonPrepareResult::JSON_PREPARE_RESULT_TAIL;
}
#if defined(__GNUC__) && !defined(__clang__) && __GNUC__ >= 14 && defined(__OPTIMIZE_SIZE__)
#pragma GCC diagnostic pop
#endif
bool AsyncEventSourceResponse::send_json_(void *source, message_generator_t *generator) {
if (!this->ready_to_send_()) {
return false;
}
char buffer[JSON_BUF_SIZE];
size_t size;
const auto result = this->prepare_json_(source, generator, buffer, &size);
switch (result) {
case JsonPrepareResult::JSON_PREPARE_RESULT_BUFFER:
return this->try_send_nodefer(buffer, size, "state");
case JsonPrepareResult::JSON_PREPARE_RESULT_TAIL:
this->drain_tail_();
return true;
case JsonPrepareResult::JSON_PREPARE_RESULT_NO_MEMORY: {
SendGuard guard{*this};
this->tail_alloc_failed_(size);
return false;
}
case JsonPrepareResult::JSON_PREPARE_RESULT_TOO_LARGE: {
SendGuard guard{*this};
ESP_LOGW(TAG, "State event does not fit %zu bytes, dropped", size);
this->tail_.reset();
this->tail_cap_ = 0;
this->send_failure_started_ms_ = 0;
return true;
}
}
return false;
}
void AsyncEventSourceResponse::tail_alloc_failed_(size_t cap) {
// Same stall clock as a socket that stops draining, so a session cannot retry forever
// Socket buffer full, try again later; a socket that stays full past the stall timeout is closed
const uint32_t now = App.get_loop_component_start_time();
if (this->send_failure_started_ms_ == 0) {
this->send_failure_started_ms_ = now != 0 ? now : 1; // Reserve zero for no stall.
ESP_LOGW(TAG, "No memory for a %zu byte chunk", cap);
return;
}
if (static_cast<int32_t>(now - (this->send_failure_started_ms_ + SEND_STALL_TIMEOUT_MS)) >= 0) {
ESP_LOGW(TAG, "Closing EventSource after %" PRIu32 " ms without memory", now - this->send_failure_started_ms_);
this->request_close_();
this->stall_.start(now);
if (this->stall_.is_expired(now, SEND_STALL_TIMEOUT_MS)) {
ESP_LOGW(TAG, "Closing stuck stream after %" PRIu32 " ms without send progress", this->stall_.elapsed(now));
this->close();
return -1;
}
return 0;
}
bool AsyncEventSourceResponse::ready_to_send_() {
if (this->sending_ || this->fd_.load() == 0 || this->close_requested_) {
return false;
}
drain_tail_();
return !this->close_requested_ && this->tail_len_ == 0;
ssize_t AsyncStreamResponse::write(const void *data, size_t len) {
struct iovec iov = {const_cast<void *>(data), len};
return this->write(&iov, 1);
}
bool AsyncEventSourceResponse::try_send_nodefer(const char *message, size_t message_len, const char *event, uint32_t id,
uint32_t reconnect) {
if (!this->ready_to_send_()) {
return false;
}
SendGuard guard{*this};
// Everything after the prefix goes out straight from the caller's buffer
char prefix[PREFIX_BUF_SIZE];
const size_t prefix_len = build_chunk_prefix(prefix, sizeof(prefix), event, id, reconnect, message != nullptr);
if (message == nullptr && prefix_len == CHUNK_HDR_LEN) {
return true; // Match ESPAsyncWebServer: nothing to send
}
if (prefix_len >= PREFIX_BUF_SIZE - 1) {
// The appenders truncate silently, which would put a malformed event on the wire
ESP_LOGW(TAG, "Event name too long, dropped");
return true;
}
// Gather list: the prefix, then the data lines and their separators from the message
struct Gather {
struct iovec iov[MAX_SEND_IOV]; // left uninitialized on purpose
int iovcnt{1};
size_t total{0};
bool fits{true};
} g;
g.total = prefix_len;
for_each_chunk_piece(
message, message_len,
[](void *ctx, const char *piece, size_t len) {
auto &g = *static_cast<Gather *>(ctx);
g.total += len;
if (len == 0) {
return;
}
if (g.iovcnt == MAX_SEND_IOV) {
g.fits = false;
return;
}
g.iov[g.iovcnt++] = {const_cast<char *>(piece), len};
},
&g);
// The header and the terminator are not part of the chunk length
write_chunk_header(prefix, g.total - CHUNK_HDR_LEN - CHUNK_END_LEN);
g.iov[0] = {prefix, prefix_len};
// A message with more lines than the list holds skips straight to the tail
const ssize_t sent = g.fits ? this->send_(g.iov, g.iovcnt) : 0;
if (sent < 0) {
return false;
}
if (static_cast<size_t>(sent) == g.total) {
this->send_failure_started_ms_ = 0; // progress, whichever stall clock was running
return true;
}
// The caller's buffers do not outlive this call, so keep the chunk and continue from loop()
return this->stash_chunk_(prefix, prefix_len, message, message_len, g.total, sent);
}
void AsyncEventSourceResponse::deferrable_send_state(void *source, bool detail_all,
message_generator_t *message_generator) {
// Let the detail_all listing finish before bare state events, so the web UI never shows unnamed entries and
// initial connect sends less
if (!detail_all && !this->entities_iterator_.completed())
return;
drain_tail_();
process_deferred_queue_();
if (this->close_requested_) {
return;
}
if (this->tail_len_ != 0 || !deferred_queue_.empty()) {
// outgoing event buffer or deferred queue still not empty which means downstream tcp send buffer full, no point
// trying to send first
deq_push_back_with_dedup_(source, message_generator);
} else {
// A send error closes the session and clears the queue; nothing is queued after that
if (!this->send_json_(source, message_generator) && !this->close_requested_) {
deq_push_back_with_dedup_(source, message_generator);
}
}
}
#endif
#ifdef USE_WEBSERVER_OTA
esp_err_t AsyncWebServer::handle_multipart_upload_(httpd_req_t *r, const char *content_type) {
// Parse boundary and create reader
@@ -15,21 +15,14 @@
#include <utility>
#include <vector>
#ifdef USE_WEBSERVER
#include "esphome/components/json/json_util.h"
#include "esphome/components/web_server/list_entities.h"
#include "sse_chunk.h"
#endif
#include "esphome/components/web_server_base/stream_head.h"
struct iovec; // NOLINT(readability-identifier-naming) - forward decl of lwip's gather list entry
namespace esphome {
#ifdef USE_WEBSERVER
namespace web_server {
class WebServer;
}; // namespace web_server
#endif
namespace web_server_idf {
namespace esphome::web_server_idf {
using web_server_base::HttpHeader;
using web_server_base::StallClock;
class AsyncWebParameter {
public:
@@ -191,6 +184,7 @@ class AsyncWebServerRequest {
void onDisconnect(std::function<void()> fn) { this->on_disconnect_ = std::move(fn); }
#endif
/// sess_ctx is reserved for AsyncStreamResponse: the server's close_fn treats any session context as one.
operator httpd_req_t *() const { return this->req_; }
optional<std::string> get_header(const char *name) const;
/// Copy a header value into buf without heap allocation.
@@ -229,6 +223,13 @@ class AsyncWebServer {
void begin();
void end();
#ifdef USE_WEBSERVER_SSL
/// PEM certificate and key, not copied; nullptr for plain HTTP. Takes effect at the next begin().
void set_ssl_credentials(const char *certificate, const char *private_key) {
this->ssl_certificate_ = certificate;
this->ssl_private_key_ = private_key;
}
#endif
// NOLINTNEXTLINE(readability-identifier-naming)
AsyncWebHandler &addHandler(AsyncWebHandler *handler) {
@@ -240,7 +241,17 @@ class AsyncWebServer {
protected:
uint16_t port_{};
#ifdef USE_WEBSERVER_SSL
uint16_t running_port_{}; // port of the running listener, 0 once httpd frees it
#endif
httpd_handle_t server_{};
#ifdef USE_WEBSERVER_SSL
const char *ssl_certificate_{nullptr};
const char *ssl_private_key_{nullptr};
Mutex close_lock_; // held by close_fn while closing, by shutdown_clients_() and by context_free()
void shutdown_clients_();
static void context_free(void *ctx);
#endif
static esp_err_t request_handler(httpd_req_t *r);
static esp_err_t request_post_handler(httpd_req_t *r);
esp_err_t request_handler_(AsyncWebServerRequest *request) const;
@@ -269,204 +280,85 @@ class AsyncWebHandler {
virtual bool isRequestHandlerTrivial() const { return true; }
};
#ifdef USE_WEBSERVER
class AsyncEventSource;
class AsyncEventSourceResponse;
using message_generator_t = void(esphome::web_server::WebServer *, void *, json::JsonBuilder &);
/*
This class holds a pointer to the source component that wants to publish a state event, and a pointer to a function
that will lazily generate that event. The two pointers allow dedup in the deferred queue if multiple publishes for
the same component are backed up, and take up only two pointers of memory. The entry in the deferred queue (a
std::vector) is the DeferredEvent instance itself (not a pointer to one elsewhere in heap) so still only two pointers
per entry (and no heap fragmentation). Even 100 backed up events (you'd have to have at least 100 sensors publishing
because of dedup) would take up only 0.8 kB.
*/
struct DeferredEvent {
friend class AsyncEventSourceResponse;
protected:
void *source_;
message_generator_t *message_generator_;
/// A response that keeps its connection open and is written from the main loop, such as an event stream or
/// an audio stream. Use it directly or derive from it. Call loop() and delete the object only through the type
/// it was created as: subclasses hide loop() instead of overriding it, and the destructor is not virtual.
///
/// Lifecycle:
/// 1. Httpd task, inside handleRequest(): create it and call begin(), then hand the pointer to the main loop.
/// 2. Main loop: once is_attached() is true, write() may go through. Call loop() on every pass.
/// 3. Main loop: delete it once safe_to_delete() is true. httpd owns the socket throughout.
class AsyncStreamResponse {
public:
DeferredEvent(void *source, message_generator_t *message_generator)
: source_(source), message_generator_(message_generator) {}
bool operator==(const DeferredEvent &test) const {
return (source_ == test.source_ && message_generator_ == test.message_generator_);
}
};
static_assert(sizeof(DeferredEvent) == sizeof(void *) + sizeof(message_generator_t *),
"DeferredEvent should have no padding");
/// Httpd task, once. Sends the status line (for example HTTPD_200), Content-Type, headers, the default headers
/// and body (a C string or null, sent as is), then makes this object the session context. A chunked response
/// passes {"Transfer-Encoding", "chunked"} as its first header and frames each chunk itself.
void begin(AsyncWebServerRequest *request, const char *status, const char *content_type,
std::span<const HttpHeader> headers, const char *body = nullptr);
class AsyncEventSourceResponse {
friend class AsyncEventSource;
public:
bool try_send_nodefer(const char *message, size_t message_len, const char *event = nullptr, uint32_t id = 0,
uint32_t reconnect = 0);
// detail_all events go out while entities are still being listed; state events wait until listing is done
void deferrable_send_state(void *source, bool detail_all, message_generator_t *message_generator);
// The old event name argument would otherwise convert to true
void deferrable_send_state(void *source, const char *event_type, message_generator_t *message_generator) = delete;
/// Main loop. Non-blocking gather write. Returns the bytes accepted, which may be fewer than offered: the caller
/// keeps the rest and continues from there. 0 means the socket is full or not ours yet; -1 means the stream failed
/// and is closing. Refused writes for SEND_STALL_TIMEOUT_MS in a row also close it.
ssize_t write(struct iovec *iov, int iovcnt);
ssize_t write(const void *data, size_t len);
/// Main loop. Shuts the connection down through httpd; safe to call more than once.
void close();
/// Main loop, every pass. Keeps the session fresh in httpd's LRU and retries a pending close.
void loop();
/// Httpd task, from the server's close_fn: no write() reaches the socket after this returns.
static void socket_closing(httpd_handle_t hd, int fd);
protected:
AsyncEventSourceResponse(const AsyncWebServerRequest *request, esphome::web_server_idf::AsyncEventSource *server,
esphome::web_server::WebServer *ws);
// Main-loop only: sends initial ping/config/sorting_groups, starts entity iterator.
void start_session_main_loop_();
void deq_push_back_with_dedup_(void *source, message_generator_t *message_generator);
void process_deferred_queue_();
// A new chunk may go out: not re-entered from a log line, session alive, tail empty
bool ready_to_send_();
// Non-blocking gather write. Returns bytes written, 0 on would-block or a socket that is not
// ours, -1 after requesting the close on any other error.
ssize_t send_(struct iovec *iov, int iovcnt);
// Push what is left of the chunk in tail_ to the socket; owns the stall timer.
void drain_tail_();
// Grow tail_ to hold len bytes, kept at its high-water mark. False on OOM.
bool reserve_tail_(size_t len);
// Keep the whole chunk in tail_ and continue from sent; false when the tail cannot be allocated.
bool stash_chunk_(const char *prefix, size_t prefix_len, const char *message, size_t message_len, size_t total,
size_t sent);
// Send a state event; JSON too large for the stack buffer is serialized into tail_ instead
bool send_json_(void *source, message_generator_t *generator);
enum class JsonPrepareResult : uint8_t {
JSON_PREPARE_RESULT_BUFFER,
JSON_PREPARE_RESULT_TAIL,
JSON_PREPARE_RESULT_NO_MEMORY,
JSON_PREPARE_RESULT_TOO_LARGE,
};
// Keep the arena out of the caller's frame so it is gone before the state send.
// size_out: BUFFER = JSON length, NO_MEMORY = requested tail capacity, TOO_LARGE = available JSON room;
// unused for TAIL.
JsonPrepareResult __attribute__((noinline)) prepare_json_(void *source, message_generator_t *generator, char *buffer,
size_t *size_out);
// Warn once, and close the session once the stall timeout passes with no memory for the tail
void tail_alloc_failed_(size_t cap);
void request_close_();
// A log line emitted inside a send re-enters try_send_nodefer on this session; refuse it
struct SendGuard {
AsyncEventSourceResponse &owner;
explicit SendGuard(AsyncEventSourceResponse &owner) : owner(owner) { owner.sending_ = true; }
~SendGuard() { this->owner.sending_ = false; }
};
void process_close_();
static void close_session_work(void *arg);
void refresh_lru_();
static void refresh_lru_work(void *arg);
// httpd frees a session before closing its socket, so the fd may already belong to a new client
bool owns_session_(int fd) const { return fd != 0 && httpd_sess_get_ctx(this->hd_, fd) == this; }
// Deletable only after destroy() zeroed fd_ and no queued HTTPD work still references this object.
bool safe_to_delete_() const {
/// httpd holds this response as the session context; begin() has sent the whole head by then.
bool is_attached() const { return this->owns_session_(this->fd_.load()); }
/// The client is still connected; httpd has not freed the session.
bool is_connected() const { return this->fd_.load() != 0; }
bool is_closing() const { return this->close_requested_; }
/// The socket fd, for log lines only
int log_id() const { return this->fd_.load(); }
/// Sends are making no progress; after write() returns -1 this tells a send timeout from a send error.
bool is_stalled() const { return this->stall_.is_running(); }
/// The session is gone and no queued httpd work still references this object.
bool safe_to_delete() const {
return this->fd_.load() == 0 && !this->close_work_queued_.load(std::memory_order_acquire) &&
!this->lru_work_queued_.load(std::memory_order_acquire);
}
static constexpr uint32_t SEND_STALL_TIMEOUT_MS = 20000;
protected:
void process_close_();
static void close_session_work(void *arg);
void refresh_lru_();
static void refresh_lru_work(void *arg);
// httpd closes the socket before it clears the session, so this can still match a closed fd; write() also checks
// socket_closed_
bool owns_session_(int fd) const { return fd != 0 && httpd_sess_get_ctx(this->hd_, fd) == this; }
static void destroy(void *p);
AsyncEventSource *server_;
httpd_handle_t hd_{};
std::atomic<int> fd_{};
std::vector<DeferredEvent> deferred_queue_;
esphome::web_server::WebServer *web_server_;
esphome::web_server::ListEntitiesIterator entities_iterator_;
// One chunk the socket did not take whole, allocated on the first stall; the only heap use
// on the send path
RAMUniquePtr<uint8_t[]> tail_;
uint32_t send_failure_started_ms_{0}; // Zero means no send stall in progress.
// Runs while sends make no progress; a running clock keeps its start time
StallClock stall_;
uint32_t next_close_attempt_ms_{0};
uint32_t last_lru_refresh_ms_{0};
uint16_t tail_cap_{0};
uint16_t tail_len_{0}; // Zero means nothing pending
uint16_t tail_sent_{0};
// Set on the main loop before queueing close work, cleared by the HTTPD-task callback when done.
std::atomic<bool> close_work_queued_{false};
std::atomic<bool> lru_work_queued_{false};
// Guarded by the stream send mutex
bool socket_closed_{false};
// Main-loop only; the HTTPD task never reads or writes these flags.
bool close_requested_{false};
bool close_retry_warning_logged_{false};
bool sending_{false};
// The longest multi line log message in the tree (a climate dump_config) has 22 lines; a
// longer one goes through the tail
static constexpr size_t MAX_SEND_LINES = 22;
static constexpr size_t MAX_SEND_IOV = 1 + 2 * MAX_SEND_LINES;
// Chunk header, retry/id/event lines and the first "data: "
static constexpr size_t PREFIX_BUF_SIZE = 128;
// Stack buffer for a state event's JSON; a larger document is serialized into the tail
static constexpr size_t JSON_BUF_SIZE = 1024;
// Same ceiling JsonBuilder::serialize() applies (max_heap_size in json_util.cpp); a larger
// document is dropped before anything is on the wire
static constexpr size_t JSON_MAX_SIZE = 5120;
// Most RAM a stalled session keeps: the largest state document plus any accepted framing
static constexpr size_t TAIL_MAX_SIZE = JSON_MAX_SIZE + PREFIX_BUF_SIZE + SSE_SUFFIX_LEN;
static constexpr uint32_t SEND_STALL_TIMEOUT_MS = 20000;
static constexpr uint32_t CLOSE_RETRY_INTERVAL_MS = 250;
static constexpr uint32_t CLOSE_CONFIRM_INTERVAL_MS = 1000;
static constexpr uint32_t LRU_REFRESH_INTERVAL_MS = 1000;
};
using AsyncEventSourceClient = AsyncEventSourceResponse;
class AsyncEventSource : public AsyncWebHandler {
friend class AsyncEventSourceResponse;
public:
AsyncEventSource(StringRef url, esphome::web_server::WebServer *ws) : url_(url), web_server_(ws) {}
~AsyncEventSource() override;
// NOLINTNEXTLINE(readability-identifier-naming)
bool canHandle(AsyncWebServerRequest *request) const override {
if (request->method() != HTTP_GET)
return false;
char url_buf[AsyncWebServerRequest::URL_BUF_SIZE];
return request->url_to(url_buf) == this->url_;
}
// NOLINTNEXTLINE(readability-identifier-naming)
void handleRequest(AsyncWebServerRequest *request) override;
void try_send_nodefer(const char *message, size_t message_len, const char *event = nullptr, uint32_t id = 0,
uint32_t reconnect = 0);
void deferrable_send_state(void *source, bool detail_all, message_generator_t *message_generator);
// The old event name argument would otherwise convert to true
void deferrable_send_state(void *source, const char *event_type, message_generator_t *message_generator) = delete;
/// Returns true if there are sessions remaining (including pending cleanup).
bool loop();
bool empty() { return this->count() == 0; }
size_t count() const { return this->sessions_.size(); }
protected:
// Cold path: move sessions from pending_sessions_ into sessions_ and greet each one.
void __attribute__((noinline, cold)) adopt_pending_sessions_main_loop_();
StringRef url_; // Must outlive this object (string literal)
// Main-loop only. Vector: SSE sessions are 1-5 connections, linear search beats set.
std::vector<AsyncEventSourceResponse *> sessions_;
// Httpd-task intake; guarded by pending_mutex_, gated by has_pending_sessions_.
std::vector<AsyncEventSourceResponse *> pending_sessions_;
Mutex pending_mutex_;
esphome::web_server::WebServer *web_server_;
std::atomic<bool> has_pending_sessions_{false};
};
#endif // USE_WEBSERVER
struct HttpHeader {
const char *name;
const char *value;
};
class DefaultHeaders {
friend class AsyncWebServerRequest;
friend class AsyncStreamResponse;
friend class AsyncWebServerResponse;
#ifdef USE_WEBSERVER
friend class AsyncEventSourceResponse;
#endif
static_assert(WEB_SERVER_DEFAULT_HEADERS_COUNT <= 8, "AsyncWebServerResponse::replaced_defaults_ holds 8 bits");
public:
@@ -484,8 +376,7 @@ class DefaultHeaders {
void apply_(httpd_req_t *req, uint8_t skip_mask = 0) const;
};
} // namespace web_server_idf
} // namespace esphome
} // namespace esphome::web_server_idf
using namespace esphome::web_server_idf; // NOLINT(google-global-names-in-headers)
+10 -6
View File
@@ -298,11 +298,14 @@ AP_MANUAL_IP_SCHEMA = cv.Schema(
}
)
STA_MANUAL_IP_SCHEMA = AP_MANUAL_IP_SCHEMA.extend(
{
cv.Optional(CONF_DNS1, default="0.0.0.0"): cv.ipv4address,
cv.Optional(CONF_DNS2, default="0.0.0.0"): cv.ipv4address,
}
STA_MANUAL_IP_SCHEMA = cv.All(
AP_MANUAL_IP_SCHEMA.extend(
{
cv.Optional(CONF_DNS1, default="0.0.0.0"): cv.ipv4address,
cv.Optional(CONF_DNS2, default="0.0.0.0"): cv.ipv4address,
}
),
require_ipv4(DOMAIN, CONF_MANUAL_IP),
)
TTLS_PHASE_2 = {
@@ -388,6 +391,7 @@ def wifi_network_ap(value):
if value is None:
value = {}
config = WIFI_NETWORK_AP(value)
require_ipv4(DOMAIN, CONF_AP)(config)
ap_mode = config[CONF_AP_MODE]
if ap_mode == WIFI_AP_MODE_WISP and not config.get(CONF_PASSWORD):
raise cv.Invalid(
@@ -688,6 +692,7 @@ def _lease_cache_schema(value: Any) -> ConfigType:
value = LEASE_CACHE_SCHEMA(value)
if value[CONF_ENABLED]:
cv.only_on_esp32(value)
require_ipv4(DOMAIN, CONF_LEASE_CACHE)(value)
if CONF_STORAGE not in value:
if not preferences.rtc_supported():
raise cv.Invalid(
@@ -780,7 +785,6 @@ CONFIG_SCHEMA = cv.All(
_apply_min_auth_mode_default,
_validate,
_report_provisioning_credentials,
require_ipv4(DOMAIN),
)
+5 -2
View File
@@ -471,6 +471,9 @@ class WiFiPowerSaveListener {
static constexpr uint32_t AP_HANDOVER_TIMEOUT_ID = 0;
static constexpr uint32_t LEASE_RENEW_TIMEOUT_ID = 1;
static constexpr uint32_t AP_DEAUTH_RETRY_TIMEOUT_ID = 2;
#if USE_NETWORK_IPV6 && (USE_NETWORK_MIN_IPV6_ADDR_COUNT > 0)
static constexpr uint32_t IPV6_RECOUNT_TIMEOUT_ID = 3;
#endif
/// This component is responsible for managing the ESP WiFi interface.
class WiFiComponent final : public Component {
@@ -1116,9 +1119,9 @@ class WiFiComponent final : public Component {
// so no happens-before relationship with other memory needs to be established.
std::atomic<uint8_t> roaming_suppression_count_{0};
#endif
#if USE_NETWORK_IPV6
#if USE_NETWORK_IPV6 && (USE_NETWORK_MIN_IPV6_ADDR_COUNT > 0)
uint8_t num_ipv6_addresses_{0};
#endif /* USE_NETWORK_IPV6 */
#endif
bool error_from_callback_{false};
#if defined(USE_ESP32) && !defined(USE_WIFI_MULTI_SSID)
bool scan_driver_filtered_{false};
@@ -45,6 +45,9 @@
#include <esp_netif_net_stack.h> // esp_netif_get_netif_impl()
#include "lwip/netif.h"
#endif
#if !USE_NETWORK_IPV4
#include "lwip/dhcp6.h"
#endif
#ifdef USE_WIFI_LEASE_CACHE
#include <esp_rtc_time.h> // esp_rtc_get_time_us() -- RTC-domain clock for lease age
#include "lwip/dhcp.h"
@@ -653,7 +656,7 @@ bool WiFiComponent::wifi_sta_connect_(const WiFiAP &ap) {
void WiFiComponent::clear_sta_session_() {
s_sta_connected = false;
this->got_ipv4_address_ = false;
#if USE_NETWORK_IPV6
#if USE_NETWORK_IPV6 && (USE_NETWORK_MIN_IPV6_ADDR_COUNT > 0)
this->num_ipv6_addresses_ = 0;
#endif
}
@@ -666,6 +669,11 @@ static void disable_dhcp_sntp() {
sntp_servermode_dhcp(false);
}
#if USE_NETWORK_IPV6 && (USE_NETWORK_MIN_IPV6_ADDR_COUNT > 0)
// How long after a driver roam SLAAC gets to settle before the IPv6 addresses are recounted.
static constexpr uint32_t IPV6_ROAM_SETTLE_MS = 5000;
#endif
#ifdef USE_WIFI_LEASE_CACHE
// Minimum lease time that must remain before a cached lease is applied.
static constexpr uint32_t LEASE_APPLY_MARGIN_SECS = 60;
@@ -731,6 +739,11 @@ bool WiFiComponent::wifi_sta_ip_config_(const optional<ManualIP> &manual_ip) {
return false;
}
#if !USE_NETWORK_IPV4
// No IPv4 stack: addresses come from SLAAC, validation rejects manual_ip
(void) manual_ip;
return true;
#else
esp_netif_dhcp_status_t dhcp_status;
esp_err_t err = esp_netif_dhcpc_get_status(s_sta_netif, &dhcp_status);
if (err != ESP_OK) {
@@ -812,6 +825,7 @@ bool WiFiComponent::wifi_sta_ip_config_(const optional<ManualIP> &manual_ip) {
}
return true;
#endif // USE_NETWORK_IPV4
}
#ifdef USE_WIFI_LEASE_CACHE
@@ -884,6 +898,7 @@ network::IPAddresses WiFiComponent::wifi_sta_ip_addresses() {
if (!this->has_sta())
return {};
network::IPAddresses addresses;
#if USE_NETWORK_IPV4
esp_netif_ip_info_t ip;
esp_err_t err = esp_netif_get_ip_info(s_sta_netif, &ip);
if (err != ESP_OK) {
@@ -893,6 +908,7 @@ network::IPAddresses WiFiComponent::wifi_sta_ip_addresses() {
} else {
addresses[0] = network::IPAddress(&ip.ip);
}
#endif
#if USE_NETWORK_IPV6
network::fill_ip6_addresses(s_sta_netif, addresses);
#endif /* USE_NETWORK_IPV6 */
@@ -1106,7 +1122,12 @@ void WiFiComponent::wifi_process_event_(IDFWiFiEvent *data) {
if (auto *netif = static_cast<struct netif *>(esp_netif_get_netif_impl(s_sta_netif)); netif != nullptr) {
if (esp_err_t err = esp_netif_tcpip_exec(
[](void *ctx) -> esp_err_t {
netif_create_ip6_linklocal_address(static_cast<struct netif *>(ctx), 1);
auto *netif = static_cast<struct netif *>(ctx);
netif_create_ip6_linklocal_address(netif, 1);
#if !USE_NETWORK_IPV4
// Without DHCPv4, DNS servers come from RDNSS or stateless DHCPv6; esp_netif never starts the latter
dhcp6_enable_stateless(netif);
#endif
return ESP_OK;
},
netif);
@@ -1131,6 +1152,26 @@ void WiFiComponent::wifi_process_event_(IDFWiFiEvent *data) {
bssid_t roam_bssid;
std::copy(it.bssid, it.bssid + 6, roam_bssid.begin());
this->handle_driver_roam_(roam_bssid, it.channel);
#if USE_NETWORK_IPV6 && (USE_NETWORK_MIN_IPV6_ADDR_COUNT > 0)
// GOT_IP6 never counts down; once SLAAC settles drop addresses the new AP did not give back,
// but never below the minimum, so a slow SLAAC does not read as a lost connection
this->set_timeout(IPV6_RECOUNT_TIMEOUT_ID, IPV6_ROAM_SETTLE_MS, [this]() {
if (!s_sta_connected)
return; // A disconnect already cleared the count
esp_ip6_addr_t addresses[CONFIG_LWIP_IPV6_NUM_ADDRESSES];
int count;
{
LwIPLock lock;
count = esp_netif_get_all_ip6(s_sta_netif, addresses);
}
if (count < USE_NETWORK_MIN_IPV6_ADDR_COUNT || count == this->num_ipv6_addresses_)
return;
this->num_ipv6_addresses_ = static_cast<uint8_t>(count);
#ifdef USE_WIFI_IP_STATE_LISTENERS
this->notify_ip_state_listeners_();
#endif
});
#endif
}
#ifdef USE_WIFI_CONNECT_STATE_LISTENERS
// Defer listener notification until state machine reaches STA_CONNECTED
@@ -1193,6 +1234,7 @@ void WiFiComponent::wifi_process_event_(IDFWiFiEvent *data) {
(data->event_id == IP_EVENT_ETH_GOT_IP || data->event_id == IP_EVENT_PPP_GOT_IP)) {
this->apply_ap_dns_();
#endif
#if USE_NETWORK_IPV4
} else if (data->event_base == IP_EVENT && data->event_id == IP_EVENT_STA_GOT_IP) {
const auto &it = data->data.ip_got_ip;
esp_netif_ip_info_t current_ip{};
@@ -1212,20 +1254,30 @@ void WiFiComponent::wifi_process_event_(IDFWiFiEvent *data) {
#ifdef USE_WIFI_IP_STATE_LISTENERS
this->notify_ip_state_listeners_();
#endif
#endif // USE_NETWORK_IPV4
#if USE_NETWORK_IPV6
} else if (data->event_base == IP_EVENT && data->event_id == IP_EVENT_GOT_IP6) {
const auto &it = data->data.ip_got_ip6;
if (it.esp_netif != s_sta_netif) {
return; // another interface's address (AP netif, or ethernet alongside wifi)
}
if (!s_sta_connected) {
ESP_LOGV(TAG, "Ignoring IPv6 address event before association");
return;
}
ESP_LOGV(TAG, "IPv6 address=" IPV6STR, IPV62STR(it.ip6_info.ip));
#if USE_NETWORK_MIN_IPV6_ADDR_COUNT > 0
// Count addresses, not events (a roam re-fires the link-local). Never count down while associated:
// a roam briefly clears every address, which must not read as a lost connection.
esp_ip6_addr_t addresses[CONFIG_LWIP_IPV6_NUM_ADDRESSES];
uint8_t count;
{
LwIPLock lock;
this->num_ipv6_addresses_ = esp_netif_get_all_ip6(s_sta_netif, addresses);
count = static_cast<uint8_t>(esp_netif_get_all_ip6(s_sta_netif, addresses));
}
ESP_LOGV(TAG, "IPv6 address=" IPV6STR, IPV62STR(it.ip6_info.ip));
this->num_ipv6_addresses_ = std::max(this->num_ipv6_addresses_, count);
#endif
#ifdef USE_WIFI_IP_STATE_LISTENERS
this->notify_ip_state_listeners_();
#endif
@@ -1412,7 +1464,7 @@ void WiFiComponent::wifi_process_event_(IDFWiFiEvent *data) {
}
WiFiSTAConnectStatus WiFiComponent::wifi_sta_connect_status_() const {
if (s_sta_connected && this->got_ipv4_address_) {
if (s_sta_connected && (!USE_NETWORK_IPV4 || this->got_ipv4_address_)) {
#if USE_NETWORK_IPV6 && (USE_NETWORK_MIN_IPV6_ADDR_COUNT > 0)
if (this->num_ipv6_addresses_ >= USE_NETWORK_MIN_IPV6_ADDR_COUNT) {
return WiFiSTAConnectStatus::CONNECTED;
@@ -1702,6 +1754,7 @@ int32_t WiFiComponent::get_wifi_channel() {
return primary;
}
network::IPAddress WiFiComponent::wifi_subnet_mask_() {
#if USE_NETWORK_IPV4
esp_netif_ip_info_t ip;
esp_err_t err = esp_netif_get_ip_info(s_sta_netif, &ip);
if (err != ESP_OK) {
@@ -1709,8 +1762,12 @@ network::IPAddress WiFiComponent::wifi_subnet_mask_() {
return {};
}
return network::IPAddress(&ip.netmask);
#else
return {};
#endif
}
network::IPAddress WiFiComponent::wifi_gateway_ip_() {
#if USE_NETWORK_IPV4
esp_netif_ip_info_t ip;
esp_err_t err = esp_netif_get_ip_info(s_sta_netif, &ip);
if (err != ESP_OK) {
@@ -1718,6 +1775,9 @@ network::IPAddress WiFiComponent::wifi_gateway_ip_() {
return {};
}
return network::IPAddress(&ip.gw);
#else
return {};
#endif
}
network::IPAddress WiFiComponent::wifi_dns_ip_(int num) {
const ip_addr_t *dns_ip = dns_getserver(num);
+19
View File
@@ -33,6 +33,9 @@
#ifdef USE_STATUS_LED
#include "esphome/components/status_led/status_led.h"
#endif
#ifdef USE_ESP32_TASK_WDT
#include "esp_task_wdt.h"
#endif
namespace esphome {
@@ -235,11 +238,27 @@ void Application::process_dump_config_() {
this->dump_config_at_++;
}
#if !defined(USE_ESP32) || defined(USE_ESP32_TASK_WDT) || defined(USE_STATUS_LED)
void Application::feed_wdt() {
// Cold entry: callers without a millis() timestamp in hand. Fetches the
// time and defers to the hot path.
this->feed_wdt_with_time(MillisInternal::get());
}
#endif
void Application::feed_wdt_if_watched() {
#ifdef USE_ESP32_TASK_WDT
if (esp_task_wdt_status(nullptr) != ESP_OK)
return;
#elif defined(USE_ESP32) && defined(USE_STATUS_LED)
// No watchdog, but the loop task keeps the status LED blinking through a long blocking call
if (xTaskGetCurrentTaskHandle() != esphome_main_task_handle)
return;
#elif defined(USE_ESP32)
return;
#endif
this->feed_wdt();
}
void HOT Application::feed_wdt_slow_(uint32_t time) {
// Callers (both feed_wdt() and feed_wdt_with_time()) have already
+18 -5
View File
@@ -208,8 +208,8 @@ class Application {
* Each component can request a high frequency loop execution by using the HighFrequencyLoopRequester
* helper in helpers.h
*
* Sleep per wake is capped at 2 * WDT_FEED_INTERVAL_MS (except host and ESP8266);
* raise the platform watchdog timeout to sleep longer.
* Sleep per wake is capped at 2 * WDT_FEED_INTERVAL_MS (except host, ESP8266, and ESP32
* with watchdog_timeout: 0s); raise the platform watchdog timeout to sleep longer.
*
* @param loop_interval The interval in milliseconds to run the core loop at. Defaults to 16 milliseconds.
*/
@@ -239,6 +239,7 @@ class Application {
// https://github.com/libretiny-eu/framework-beken-bdk/blob/44800e7451ea30fbcbd3bb6e905315de59349fee/beken378/driver/wdt/wdt.c#L75-L87
static constexpr uint32_t WDT_FEED_INTERVAL_MS = 2000;
#elif defined(USE_ESP32)
#ifdef USE_ESP32_TASK_WDT
// Auto-scale to 1/5 of the configured ESP32 task WDT timeout so the safety
// margin stays constant when the user raises esp32.watchdog_timeout (default
// 5 s → 1000 ms feed; 10 s → 2000 ms; 60 s → 12000 ms). The esp32 component
@@ -252,6 +253,10 @@ class Application {
static_assert(CONFIG_ESP_TASK_WDT_TIMEOUT_S >= 5,
"CONFIG_ESP_TASK_WDT_TIMEOUT_S must be at least 5s for a safe WDT feed interval");
static constexpr uint32_t WDT_FEED_INTERVAL_MS = (CONFIG_ESP_TASK_WDT_TIMEOUT_S * 1000U) / 5U;
#else
// No task watchdog to feed; the rate limit never fires.
static constexpr uint32_t WDT_FEED_INTERVAL_MS = std::numeric_limits<uint32_t>::max();
#endif
#elif defined(USE_ESP8266)
// ESP8266 needs a tighter feed cadence than the other targets: the soft WDT
// is ~1.6 s and the HW WDT ~6 s, but a single long iteration (mDNS reply,
@@ -267,7 +272,15 @@ class Application {
/// Feed the task watchdog. Cold entry — callers without a millis()
/// timestamp in hand. Out of line to keep call sites tiny.
#if defined(USE_ESP32) && !defined(USE_ESP32_TASK_WDT) && !defined(USE_STATUS_LED)
// watchdog_timeout: 0s leaves no watchdog to feed, and no status LED needs the call
void feed_wdt() {}
#else
void feed_wdt();
#endif
/// Feed from code that may run off the loop task (a long download or image decode): on ESP32 only a task the
/// watchdog watches feeds it. With watchdog_timeout: 0s only the loop task calls through, to run the status LED.
void feed_wdt_if_watched();
#ifdef USE_STATUS_LED
/// Dispatch interval for the status LED update. Deliberately shorter than
@@ -859,9 +872,9 @@ inline void ESPHOME_ALWAYS_INLINE Application::loop() {
// SDK os_timer_arm() accepts at most 0x68D7A3 ms without system_timer_reinit();
// the SDK feeds both watchdogs while the cont task is suspended, so no WDT cap needed.
static constexpr uint32_t MAX_SLEEP_BASE = 0x68D7A3;
#elif defined(USE_HOST)
// arch_feed_wdt() is a no-op on host and ESPHOME_SUSPEND_LOOP is rejected by
// the config validator, so delay_time is already bounded by loop_interval_.
#elif defined(USE_HOST) || (defined(USE_ESP32) && !defined(USE_ESP32_TASK_WDT))
// No watchdog to feed (host, or ESP32 with watchdog_timeout: 0s), so nothing caps the sleep; with
// suspend_loop an ESP32 may sleep until a wake notification arrives.
static constexpr uint32_t MAX_SLEEP_BASE = std::numeric_limits<uint32_t>::max();
#else
static constexpr uint32_t MAX_SLEEP_BASE = WDT_FEED_INTERVAL_MS * 2;
+32 -10
View File
@@ -45,9 +45,7 @@ from esphome.const import (
CONF_TRIGGER_ID,
CONF_URL,
CONF_VERSION,
CONF_WATCHDOG_TIMEOUT,
KEY_CORE,
PLATFORM_ESP32,
PLATFORM_HOST,
PLATFORM_RP2,
PlatformFramework,
@@ -77,6 +75,7 @@ CONF_RELATION = "relation"
CONF_SBOM = "sbom"
CONF_SUPPLIER = "supplier"
CONF_SUSPEND_LOOP = "suspend_loop"
CONF_FRIENDLY_NAME_SEPARATOR = "friendly_name_separator"
# Application::loop_interval_ when loop_interval is not set. Make sure to align with application.h
DEFAULT_LOOP_INTERVAL = TimePeriodMilliseconds(milliseconds=16)
@@ -165,6 +164,23 @@ def validate_hostname(config):
return config
# DeviceInfoResponse sends the friendly name as a short string (encode_short_string_force in api/proto.h)
FRIENDLY_NAME_WITH_SUFFIX_MAX_LEN = 127
def validate_friendly_name_with_suffix(config: ConfigType) -> ConfigType:
if not config[CONF_NAME_ADD_MAC_SUFFIX] or not config[CONF_FRIENDLY_NAME]:
return config
full = f"{config[CONF_FRIENDLY_NAME]}{config[CONF_FRIENDLY_NAME_SEPARATOR]}{_MAC_SUFFIX_PLACEHOLDER}"
if (length := len(full.encode("utf-8"))) > FRIENDLY_NAME_WITH_SUFFIX_MAX_LEN:
raise cv.Invalid(
f"friendly_name with {CONF_FRIENDLY_NAME_SEPARATOR} and the MAC suffix is {length} "
f"bytes; at most {FRIENDLY_NAME_WITH_SUFFIX_MAX_LEN} fit",
path=[CONF_FRIENDLY_NAME],
)
return config
def validate_ids_and_references(config: ConfigType) -> ConfigType:
"""Validate that there are no hash collisions between IDs and that area_id references are valid.
@@ -227,13 +243,13 @@ def validate_loop_interval(config: ConfigType) -> ConfigType:
# max_loop interval is calculated from WDT_FEED_INTERVAL_MS. Make sure to align with application.h
max_loop = 600 # 2 * 300ms default for other platforms
if CORE.is_esp32:
max_loop = (
2
* fv.full_config.get()[PLATFORM_ESP32][
CONF_WATCHDOG_TIMEOUT
].total_milliseconds
// 5
)
from esphome.components.esp32 import task_wdt_timeout_ms
wdt_ms = task_wdt_timeout_ms()
if wdt_ms == 0:
# Task watchdog disabled: the loop sleep is not capped
return config
max_loop = 2 * wdt_ms // 5
elif CORE.is_bk72xx:
max_loop = 4000 # 10000ms / 5 * 2 default value
if config[CONF_LOOP_INTERVAL].total_milliseconds > max_loop:
@@ -448,6 +464,9 @@ CONFIG_SCHEMA = cv.All(
CONF_LIBRARIES, default=[], visibility=cv.Visibility.YAML_ONLY
): cv.ensure_list(cv.string_strict),
cv.Optional(CONF_NAME_ADD_MAC_SUFFIX, default=False): cv.boolean,
cv.Optional(
CONF_FRIENDLY_NAME_SEPARATOR, default=_APP_FRIENDLY_NAME_MAC_SEP
): cv.All(cv.string_no_slash, cv.ByteLength(max=3)),
cv.Optional(CONF_MERGE_WARNINGS, default=True): cv.boolean,
cv.Optional(
CONF_DEBUG_SCHEDULER, default=False, visibility=cv.Visibility.YAML_ONLY
@@ -500,6 +519,7 @@ CONFIG_SCHEMA = cv.All(
}
),
validate_hostname,
validate_friendly_name_with_suffix,
)
@@ -907,7 +927,9 @@ async def to_code(config: ConfigType) -> None:
name_expr, name_len = _emit_app_name(name, _APP_NAME_BUF_VAR, _APP_NAME_MAC_SEP)
friendly_expr, friendly_len = _emit_app_name(
friendly_name, _APP_FRIENDLY_NAME_BUF_VAR, _APP_FRIENDLY_NAME_MAC_SEP
friendly_name,
_APP_FRIENDLY_NAME_BUF_VAR,
config[CONF_FRIENDLY_NAME_SEPARATOR],
)
if name_add_mac_suffix:
cg.add_define("ESPHOME_NAME_ADD_MAC_SUFFIX")
+2
View File
@@ -538,6 +538,7 @@
#define USE_ESP32_MIN_CHIP_REVISION_SET
#define USE_ESP32_RTC_PREFERENCES
#define USE_ESP32_SRAM1_AS_IRAM
#define USE_ESP32_TASK_WDT
#define USE_ESPNOW
#define USE_ESPNOW_MAX_PAYLOAD_SIZE 1470
@@ -634,6 +635,7 @@
#define USE_WEBSERVER_CSS_INCLUDE
#define USE_WEBSERVER_JS_INCLUDE
#define USE_WEBSERVER_PRIVATE_NETWORK_ACCESS
#define USE_WEBSERVER_SSL
#define USE_WEBSERVER_VERSION 3
#define WEB_SERVER_DEFAULT_HEADERS_COUNT 1
#define WEB_SERVER_MAX_OPEN_SOCKETS 7
+13 -287
View File
@@ -1,30 +1,11 @@
#pragma once
#include <algorithm>
#include <cstddef>
#include <cstdint>
#include <cstring>
#include <type_traits>
// Out of line, one copy: the frame readers' rare paths call each other, and GCC would add specialised clones
#if defined(__clang__)
#define ESPHOME_FRAME_READER_COLD __attribute__((noinline))
#else
#define ESPHOME_FRAME_READER_COLD __attribute__((noinline, noclone))
#endif
namespace esphome {
namespace frame_reader_detail {
// A fold, not an array, so no header table lands in RAM on ESP8266
template<uint8_t... HEADER> constexpr uint8_t header_byte(size_t i) {
size_t j = 0;
uint8_t r = 0;
((r = (j++ == i) ? HEADER : r), ...);
return r;
}
} // namespace frame_reader_detail
/// Collects fixed length binary frames of N bytes that start with the header bytes HEADER...; the binary twin of
/// LineReader. Bytes before a header are skipped; checksums are left to the caller.
template<size_t N, uint8_t... HEADER> class FrameReader {
@@ -37,12 +18,11 @@ template<size_t N, uint8_t... HEADER> class FrameReader {
/// Feed one byte; returns the completed frame, header included, valid until the next call, or nullptr.
const uint8_t *feed(uint8_t c) {
pos_t pos = this->pos_;
if (pos < HEADER_LEN && c != frame_reader_detail::header_byte<HEADER...>(pos)) {
if (pos < HEADER_LEN && c != header_byte(pos)) {
// A later byte of the broken header, or c, may start the real one
if (pos == 0)
return nullptr;
this->buf_[pos] = c;
return this->replay_(pos);
this->replay_(pos);
return nullptr;
}
this->buf_[pos++] = c;
if (pos < N) {
@@ -64,278 +44,24 @@ template<size_t N, uint8_t... HEADER> class FrameReader {
void resync() { this->replay_(N - 1); }
protected:
// Start over and feed buf_[1..last] again; never completes a frame as last < N, so it returns nullptr, which lets
// feed() end in a tail call. Out of line, as feed() and replay_() call each other.
const uint8_t *ESPHOME_FRAME_READER_COLD replay_(size_t last) {
// A fold, not an array, so no header table lands in RAM on ESP8266
static constexpr uint8_t header_byte(size_t i) {
size_t j = 0;
uint8_t r = 0;
((r = (j++ == i) ? HEADER : r), ...);
return r;
}
// Start over and feed buf_[1..last] again; never completes a frame as last < N
void replay_(size_t last) {
this->pos_ = 0;
for (size_t i = 1; i <= last; i++) {
this->feed(this->buf_[i]);
}
return nullptr;
}
uint8_t buf_[N];
pos_t pos_{0};
};
// Length rules: get() gives the whole frame length once KNOWN_AT bytes are in, or 0 to reject the header.
/// offset: where the length field starts, counted from the first header byte; trailer: the bytes after the payload
/// (checksum, footer), for a payload length only.
struct LengthField {
size_t offset;
size_t trailer = 0;
};
/// Whole-frame length: the field counts every byte of the frame, header included.
template<LengthField F> struct FrameLengthU8 {
static_assert(F.trailer == 0, "A whole-frame length has no trailer");
static constexpr size_t KNOWN_AT = F.offset + 1;
static constexpr size_t get(const uint8_t *buf) { return buf[F.offset]; }
};
template<LengthField F> struct FrameLengthU16BE {
static_assert(F.trailer == 0, "A whole-frame length has no trailer");
static constexpr size_t KNOWN_AT = F.offset + 2;
static constexpr size_t get(const uint8_t *buf) { return (size_t(buf[F.offset]) << 8) | buf[F.offset + 1]; }
};
template<LengthField F> struct FrameLengthU16LE {
static_assert(F.trailer == 0, "A whole-frame length has no trailer");
static constexpr size_t KNOWN_AT = F.offset + 2;
static constexpr size_t get(const uint8_t *buf) { return buf[F.offset] | (size_t(buf[F.offset + 1]) << 8); }
};
/// Payload length: the field counts the payload right after it.
template<LengthField F> struct PayloadLengthU8 {
static constexpr size_t KNOWN_AT = F.offset + 1;
static constexpr size_t get(const uint8_t *buf) { return KNOWN_AT + buf[F.offset] + F.trailer; }
};
template<LengthField F> struct PayloadLengthU16BE {
static constexpr size_t KNOWN_AT = F.offset + 2;
static constexpr size_t get(const uint8_t *buf) {
return KNOWN_AT + FrameLengthU16BE<LengthField{.offset = F.offset}>::get(buf) + F.trailer;
}
};
template<LengthField F> struct PayloadLengthU16LE {
static constexpr size_t KNOWN_AT = F.offset + 2;
static constexpr size_t get(const uint8_t *buf) {
return KNOWN_AT + FrameLengthU16LE<LengthField{.offset = F.offset}>::get(buf) + F.trailer;
}
};
/// A frame type that always has LEN bytes.
template<size_t LEN> struct FrameFixedLength {
static_assert(LEN >= 2, "A fixed length frame has at least 2 bytes");
static constexpr size_t KNOWN_AT = 1;
static constexpr size_t get(const uint8_t * /*buf*/) { return LEN; }
};
/// Wraps a length rule: CHECK(buf) runs once AT bytes are in, and a header it fails gives length 0 (rejected).
template<class LENGTH, size_t AT, bool (*CHECK)(const uint8_t *)> struct CheckedLength {
static_assert(AT >= LENGTH::KNOWN_AT, "AT must cover the length field");
static constexpr size_t KNOWN_AT = AT;
static size_t get(const uint8_t *buf) { return CHECK(buf) ? LENGTH::get(buf) : 0; }
};
/// The shortest frame of a FrameFormat, when that is more than its header and length field.
template<size_t LEN> struct FrameMinLength {
static constexpr size_t VALUE = LEN;
};
namespace frame_reader_detail {
// The length of a FrameFixedLength rule, 0 for every other rule
template<class LENGTH> constexpr size_t FIXED_LENGTH = 0;
template<size_t LEN> constexpr size_t FIXED_LENGTH<FrameFixedLength<LEN>> = LEN;
template<class LENGTH, size_t AT, bool (*CHECK)(const uint8_t *)>
constexpr size_t FIXED_LENGTH<CheckedLength<LENGTH, AT, CHECK>> = FIXED_LENGTH<LENGTH>;
} // namespace frame_reader_detail
/// The bytes every frame of a FrameFormat starts with.
template<uint8_t... BYTES> struct FrameHeader {
static constexpr size_t LEN = sizeof...(BYTES);
static_assert(LEN >= 1 && LEN <= 8, "A FrameHeader has 1 to 8 bytes");
static constexpr uint8_t byte(size_t i) { return frame_reader_detail::header_byte<BYTES...>(i); }
};
/// One frame type for LengthFrameReader: a FrameHeader, a length rule from above and an optional FrameMinLength.
template<class HEADER, class LENGTH, class MIN = FrameMinLength<0>> struct FrameFormat {
using Length = LENGTH;
static constexpr size_t HEADER_LEN = HEADER::LEN;
static constexpr uint8_t FIRST = HEADER::byte(0);
// Bytes collected when the length is checked
static constexpr size_t MIN_LEN = std::max<size_t>({LENGTH::KNOWN_AT, HEADER_LEN, 2});
// A shorter length is not a frame
static constexpr size_t SHORTEST = std::max(MIN_LEN, MIN::VALUE);
static_assert(LENGTH::KNOWN_AT == 1 || LENGTH::KNOWN_AT > HEADER_LEN, "The length field must follow the header");
static constexpr uint8_t header_byte(size_t i) { return HEADER::byte(i); }
};
/// Collects binary frames that give their own length, of up to N bytes, in one or more FrameFormats; the first byte
/// picks the format, so each format needs a different first header byte. Bytes before a header are skipped. A length
/// that ends the frame before its length field, or is longer than N, is not a frame, and the reader looks for a
/// header after its first byte. Checksums and footers are left to the caller, which passes a handler that returns
/// false for a frame it rejects:
///
/// this->reader_.feed(c, [this](const uint8_t *frame, size_t len) { return this->handle_frame_(frame, len); });
///
/// For frames of one fixed size, FrameReader is smaller.
template<size_t N, class... FORMATS> class LengthFrameReader {
using pos_t = std::conditional_t<(N <= 0xFF), uint8_t, uint16_t>;
static_assert(sizeof...(FORMATS) >= 1, "LengthFrameReader takes at least one FrameFormat");
static_assert(N >= 2 && N <= 1024,
"LengthFrameReader size out of range"); // flush_() copies a partial frame on the stack
static constexpr bool distinct_first() {
const uint8_t first[] = {FORMATS::FIRST...};
for (size_t i = 0; i < sizeof...(FORMATS); i++) {
for (size_t j = i + 1; j < sizeof...(FORMATS); j++) {
if (first[i] == first[j])
return false;
}
}
return true;
}
static_assert(distinct_first(), "Each FrameFormat needs a different first header byte");
static_assert(((FORMATS::SHORTEST <= N) && ...), "N must hold the shortest frame of every FrameFormat");
static_assert(((frame_reader_detail::FIXED_LENGTH<typename FORMATS::Length> == 0 ||
(frame_reader_detail::FIXED_LENGTH<typename FORMATS::Length> >= FORMATS::SHORTEST &&
frame_reader_detail::FIXED_LENGTH<typename FORMATS::Length> <= N)) &&
...),
"A fixed length must hold the header and fit in N");
public:
/// Feed one byte; calls handle(frame, length) for each frame it completes, header included. A rejected frame
/// (handle returns false) is searched for frames inside it and after it, which go to handle the same way.
/// handle must not feed this reader again: the frame and the bytes held behind it live in its buffer.
template<class F> void __attribute__((always_inline)) feed(uint8_t c, F &&handle) {
// Most bytes are in the middle of a frame whose length is known, and not its last byte
const pos_t pos = this->pos_;
if (pos + 1u < this->end_) {
this->buf_[pos] = c;
this->pos_ = pos + 1;
} else if (const size_t len = this->feed_edge_(c)) {
this->drain_(len, handle);
}
}
/// For a partial frame left too long without a byte: when it holds frames that handle takes, which a corrupt length
/// can cause, give it up and pass them on; otherwise keep it, as a slow UART may still deliver the rest. A UART
/// component gets this from UARTDevice::read_frames().
template<class F> void flush(F &&handle) {
// held_end_ marks a partial frame already searched; new bytes move pos_ past it
if (this->pos_ != 0 && this->held_end_ != this->pos_)
this->flush_(handle);
}
/// Number of bytes of the partial frame; 0 after a frame is returned and while looking for a header.
size_t length() const { return this->pos_; }
/// Forget the partial frame, for a request/response protocol starting a new exchange.
void reset() {
this->pos_ = 0;
this->end_ = 0;
this->held_end_ = 0;
}
protected:
// Hands each frame (len bytes at buf_) to handle, then goes on after a handled frame or from the second byte of a
// rejected one
template<class F> bool ESPHOME_FRAME_READER_COLD drain_(size_t len, F &handle) {
bool handled = false;
while (len != 0) {
const bool ok = handle(this->buf_, len);
handled |= ok;
len = this->replay_(ok ? len : 1, this->held_end_);
}
this->held_end_ = 0;
return handled;
}
// Searches the partial frame for frames; when handle takes none, puts the partial frame back and marks it searched
template<class F> void ESPHOME_FRAME_READER_COLD flush_(F &handle) {
const pos_t pos = this->pos_;
const pos_t end = this->end_;
uint8_t saved[N];
std::memcpy(saved, this->buf_, pos);
while (this->pos_ != 0) {
// A partial frame left behind the handed over ones is searched on the next idle pass
if (this->drain_(this->replay_(1, this->pos_), handle))
return;
}
std::memcpy(this->buf_, saved, pos);
this->pos_ = pos;
this->end_ = end;
this->held_end_ = pos;
}
// Every byte but the middle of a frame; returns the length of a completed frame, or 0
size_t ESPHOME_FRAME_READER_COLD feed_edge_(uint8_t c) {
const pos_t pos = this->pos_;
if (pos == 0) {
const bool header = ((c == FORMATS::FIRST) || ...);
if (header) {
this->buf_[0] = c;
this->pos_ = 1;
}
return 0;
}
if constexpr (sizeof...(FORMATS) == 1) {
return this->feed_as_<FORMATS...>(pos, c);
} else {
const uint8_t first = this->buf_[0];
size_t len = 0;
(void) ((first == FORMATS::FIRST && ((len = this->feed_as_<FORMATS>(pos, c)), true)) || ...);
return len;
}
}
template<class F> size_t feed_as_(pos_t pos, uint8_t c) {
if (pos < F::HEADER_LEN && c != F::header_byte(pos)) {
// A later byte of the broken header, or c, may start the real one
this->buf_[pos] = c;
return this->replay_(1, pos + 1);
}
this->buf_[pos++] = c;
if (pos == F::MIN_LEN) {
const size_t len = F::Length::get(this->buf_);
if (len < F::SHORTEST || len > N)
return this->replay_(1, pos);
this->end_ = len;
}
if (pos == this->end_) {
this->pos_ = 0;
this->end_ = 0;
this->held_end_ = pos;
return pos;
}
this->pos_ = pos;
return 0;
}
// Start over and feed buf_[from..to) again; writes stay behind the read position. On a completed frame, moves the
// bytes not fed yet right behind it, for drain_(), and returns its length; 0 otherwise.
size_t ESPHOME_FRAME_READER_COLD replay_(size_t from, size_t to) {
this->pos_ = 0;
this->end_ = 0;
for (size_t i = from; i < to; i++) {
const size_t len = this->feed_edge_(this->buf_[i]);
if (len == 0)
continue;
// A nested replay may already have put bytes of its own behind the frame
const size_t held = this->held_end_;
const size_t rest = to - i - 1;
std::memmove(this->buf_ + held, this->buf_ + i + 1, rest);
this->held_end_ = held + rest;
return len;
}
return 0;
}
// Before buf_, so they stay within a short load offset of the reader when N is large
pos_t pos_{0};
// Length of the frame being collected once its length is known; 0 otherwise
pos_t end_{0};
// End of a held frame plus the bytes after it not fed yet, set when a frame completes; or a searched partial frame
pos_t held_end_{0};
uint8_t buf_[N];
};
} // namespace esphome
#undef ESPHOME_FRAME_READER_COLD
+1 -1
View File
@@ -1,4 +1,4 @@
pylint==4.1.3
pylint==4.1.4
ruff==0.17.5 # .pre-commit-config.yaml rev synced by script/sync_dependency_versions.py
pyupgrade==3.22.0 # .pre-commit-config.yaml rev synced by script/sync_dependency_versions.py
prek==0.5.10 # .github/workflows/ci.yml reads this pin
+1
View File
@@ -107,6 +107,7 @@ ISOLATED_COMPONENTS = {
"tinyusb_keyboard": "Depends on tinyusb which conflicts with usb_host",
"uart_mux": "Depends on tinyusb which conflicts with usb_host",
"usb_cdc_acm": "Depends on tinyusb which conflicts with usb_host",
"web_server_idf": "Its HTTPS variant (test-ssl) can't be merged with web_server or captive_portal, and grouped runs skip variants",
"brizlabs_led_string": "Takes SPI2_HOST for itself, which the shared spi: test bus also uses",
}
@@ -20,12 +20,6 @@ static constexpr uint8_t FRAME_30[30] = {0xAA, 0xFF, 0x03, 0x00, 0x0E, 0x03, 0xB
static constexpr uint8_t ULTRASONIC_NOISY[] = {0x12, 0xFF, 0x07, 0xA1, 0xA7, 0x34, 0xFF, 0xFF, 0x07, 0xA1, 0xA7,
0x00, 0xFF, 0x07, 0xA1, 0xA7, 0x56, 0xFF, 0x07, 0xA1, 0xA7};
// Pioneer WT shaped frames (A5 01 header, whole-frame length at byte 7) of 12, 20 and 12 bytes among noise
static constexpr uint8_t LENGTH_FRAMES[] = {0x00, 0xA5, 0x01, 0x01, 0x23, 0x00, 0xA5, 0x00, 0x0C, 0x11, 0x22, 0x33,
0x44, 0xA5, 0x01, 0x01, 0x23, 0x00, 0xA5, 0x00, 0x14, 0x11, 0x22, 0x33,
0x44, 0x55, 0x66, 0x77, 0x88, 0x99, 0xAA, 0xBB, 0xCC, 0x7E, 0xA5, 0x01,
0x01, 0x23, 0x00, 0xA5, 0x00, 0x0C, 0x11, 0x22, 0x33, 0x44};
// --- LineReader: one GGA sentence fed byte by byte ---
static void LineReader_NmeaLine(benchmark::State &state) {
@@ -99,32 +93,4 @@ static void FrameReader_UltrasonicNoisy(benchmark::State &state) {
}
BENCHMARK(FrameReader_UltrasonicNoisy);
// --- LengthFrameReader: frames of different lengths among noise ---
// A5 01, whole-frame length at 7
using PioneerFormat = FrameFormat<FrameHeader<0xA5, 0x01>, FrameLengthU8<LengthField{.offset = 7}>>;
static void LengthFrameReader_MixedFrames(benchmark::State &state) {
LengthFrameReader<255, PioneerFormat> reader;
const uint8_t *bytes = LENGTH_FRAMES;
uint32_t frames = 0;
auto handle = [&frames](const uint8_t *frame, size_t /*len*/) {
benchmark::DoNotOptimize(frame);
frames++;
return true;
};
for (auto _ : state) {
benchmark::DoNotOptimize(bytes);
for (int i = 0; i < kInnerIterations; i++) {
for (size_t j = 0; j < sizeof(LENGTH_FRAMES); j++)
reader.feed(bytes[j], handle);
}
benchmark::DoNotOptimize(frames);
}
state.SetItemsProcessed(state.iterations() * kInnerIterations);
}
BENCHMARK(LengthFrameReader_MixedFrames);
} // namespace esphome::benchmarks
@@ -797,6 +797,73 @@ def _gdey029z95_config(**overrides: Any) -> ConfigType:
return config
def test_waveshare_7p5in_hd_b_uses_the_ssd1677_bwr_driver() -> None:
"""The 7.5" HD (B) uses EPaperSSD1677BWR, and its 528 rows are the last of the controller's 680 gates."""
model = MODELS["WAVESHARE-7.5IN-HD-B"]
assert model.class_name == "EPaperSSD1677BWR"
assert model.get_dimensions({}) == (880, 528)
assert model.get_constructor_args({}) == (152,)
assert model.get_init_sequence({}) == (
(0x0C, 0xAE, 0xC7, 0xC3, 0xC0, 0x40),
(0x01, 0xA7, 0x02, 0x01),
(0x11, 0x01),
(0x3C, 0x01),
(0x18, 0x80),
)
@pytest.mark.parametrize(
("dimensions", "error", "error_key"),
[
({"width": 880, "height": 528}, None, None),
(
{"width": 880, "height": 528, "offset_height": 153},
"must not exceed 680 gates",
"height",
),
({"width": 880, "height": 680, "offset_height": 0}, None, None),
(
{"width": 880, "height": 681, "offset_height": 0},
"must not exceed 680 gates",
"height",
),
# offset_height not written: the model's 152 applies, and the error must not point at a missing key
({"width": 880, "height": 529}, "must not exceed 680 gates", "height"),
({"width": 960, "height": 528}, None, None),
({"width": 968, "height": 528}, "must not exceed 960 sources", "width"),
],
)
def test_waveshare_7p5in_hd_b_dimensions_must_fit_the_controller(
set_core_config: SetCoreConfigCallable,
set_component_config: Callable[[str, Any], None],
dimensions: dict[str, int],
error: str | None,
error_key: str | None,
) -> None:
"""The SSD1677 has 960 sources for the width and 680 gates for offset_height plus the height."""
set_core_config(
PlatformFramework.ESP32_IDF,
platform_data={KEY_BOARD: "esp32dev", KEY_VARIANT: VARIANT_ESP32},
)
set_component_config("spi", {"id": "spi_bus", "clk_pin": 18, "mosi_pin": 19})
config = {
"id": "test_display",
"model": "waveshare-7.5in-hd-b",
"dc_pin": 21,
"reset_pin": 22,
"busy_pin": 23,
"dimensions": dimensions,
}
if error is None:
run_schema_validation(config)
else:
with pytest.raises(cv.Invalid, match=error) as exc_info:
run_schema_validation(config)
assert exc_info.value.path[-2:] == ["dimensions", error_key]
def test_gdey029z95_uses_the_good_display_driver() -> None:
"""The GDEY029Z95 uses EPaperGoodDisplay3C with the WeAct init sequence."""
model = MODELS[GDEY029Z95.upper()]
@@ -0,0 +1,8 @@
esphome:
name: test
esp32:
board: esp32dev
framework:
type: esp-idf
watchdog_timeout: 10s
@@ -0,0 +1,7 @@
esphome:
name: test
esp32:
board: esp32dev
framework:
type: esp-idf
@@ -0,0 +1,8 @@
esphome:
name: test
esp32:
board: esp32dev
framework:
type: esp-idf
watchdog_timeout: 0s
+47
View File
@@ -354,6 +354,28 @@ def test_sdkconfig_cpu_frequency_errors(
assert exc_info.value.path == ["framework", "sdkconfig_options"]
@pytest.mark.parametrize("watchdog_timeout", ["0s", "5s", "60s"])
def test_esp32_watchdog_timeout_valid(
set_core_config: SetCoreConfigCallable, watchdog_timeout: str
) -> None:
"""0 disables the task watchdog; otherwise 5 to 60 seconds."""
set_core_config(PlatformFramework.ESP32_IDF)
from esphome.components.esp32 import CONFIG_SCHEMA
CONFIG_SCHEMA({"variant": VARIANT_ESP32, "watchdog_timeout": watchdog_timeout})
@pytest.mark.parametrize("watchdog_timeout", ["1s", "4s", "61s"])
def test_esp32_watchdog_timeout_invalid(
set_core_config: SetCoreConfigCallable, watchdog_timeout: str
) -> None:
set_core_config(PlatformFramework.ESP32_IDF)
from esphome.components.esp32 import CONFIG_SCHEMA
with pytest.raises(cv.Invalid, match="must be 0s"):
CONFIG_SCHEMA({"variant": VARIANT_ESP32, "watchdog_timeout": watchdog_timeout})
@pytest.mark.parametrize(
("config_toolchain", "expected"),
[
@@ -2552,3 +2574,28 @@ def test_require_idf_version_rejects_other_platforms(
set_core_config(PlatformFramework.ESP8266_ARDUINO)
with pytest.raises(cv.Invalid, match="only available on"):
require_idf_version(cv.Version(5, 5, 3))("x")
@pytest.mark.parametrize(
("config_name", "timeout"),
[("watchdog_disabled", None), ("watchdog_default", 5), ("watchdog_custom", 10)],
)
def test_task_watchdog_sdkconfig_and_define(
generate_main: Callable[[str | Path], str],
component_config_path: Callable[[str], Path],
config_name: str,
timeout: int | None,
) -> None:
"""watchdog_timeout: 0s turns the task watchdog off; any other value configures it."""
generate_main(component_config_path(f"{config_name}.yaml"))
sdkconfig = CORE.data[KEY_ESP32][KEY_SDKCONFIG_OPTIONS]
defines = {define.name for define in CORE.defines}
assert sdkconfig["CONFIG_ESP_TASK_WDT_INIT"] is (timeout is not None)
assert ("USE_ESP32_TASK_WDT" in defines) is (timeout is not None)
if timeout is None:
assert "CONFIG_ESP_TASK_WDT_TIMEOUT_S" not in sdkconfig
return
assert sdkconfig["CONFIG_ESP_TASK_WDT_TIMEOUT_S"] == timeout
assert sdkconfig["CONFIG_ESP_TASK_WDT_PANIC"] is True
assert sdkconfig["CONFIG_ESP_TASK_WDT_CHECK_IDLE_TASK_CPU0"] is False
assert sdkconfig["CONFIG_ESP_TASK_WDT_CHECK_IDLE_TASK_CPU1"] is False
@@ -0,0 +1,13 @@
esphome:
name: test
esp32:
board: nodemcu-32s
watchdog_timeout: 0s
wifi:
ssid: test
password: testtest
http_request:
timeout: 10s
@@ -0,0 +1,13 @@
esphome:
name: test
esp32:
board: nodemcu-32s
watchdog_timeout: 0s
wifi:
ssid: test
password: testtest
http_request:
watchdog_timeout: 20s
@@ -33,10 +33,29 @@ def test_esp32_watchdog_timeout(
)
@pytest.mark.parametrize("yaml_file", ["test_esp8266.yaml", "test_rp2040.yaml"])
@pytest.mark.parametrize(
"yaml_file",
[
"test_esp8266.yaml",
"test_rp2040.yaml",
# esp32.watchdog_timeout: 0s disables the task watchdog
"test_esp32_platform_disabled.yaml",
],
)
def test_other_platforms_leave_watchdog_unset(
component_config_path: Callable[[str], Path], yaml_file: str
) -> None:
CORE.config_path = component_config_path(yaml_file)
config = read_config({})
assert CONF_WATCHDOG_TIMEOUT not in config["http_request"]
def test_explicit_watchdog_rejected_when_platform_disabled(
component_config_path: Callable[[str], Path],
capsys: pytest.CaptureFixture[str],
) -> None:
CORE.config_path = component_config_path(
"test_esp32_platform_disabled_explicit.yaml"
)
assert read_config({}) is None
assert "Cannot be set when esp32.watchdog_timeout is 0s" in capsys.readouterr().out
@@ -0,0 +1,20 @@
esphome:
name: test
esp32:
board: esp32dev
framework:
type: esp-idf
network:
enable_ipv6: true
enable_ipv4: false
ethernet:
type: LAN8720
mdc_pin: 23
mdio_pin: 32
clk:
pin: 0
mode: CLK_EXT_IN
phy_addr: 0
@@ -0,0 +1,24 @@
esphome:
name: test
esp32:
board: esp32dev
framework:
type: esp-idf
network:
enable_ipv6: true
enable_ipv4: false
ethernet:
type: LAN8720
mdc_pin: 23
mdio_pin: 32
clk:
pin: 0
mode: CLK_EXT_IN
phy_addr: 0
manual_ip:
static_ip: 192.168.1.10
gateway: 192.168.1.1
subnet: 255.255.255.0
@@ -0,0 +1,15 @@
esphome:
name: test
esp32:
variant: esp32c6
framework:
type: esp-idf
wifi:
ssid: test
password: password1
openthread:
border_router:
tlv: 0e080000000000010000000300001035060004001fffe00208e227ac6a7f24052f0708fdb753eb517cb4d3051062b2442a928d9ea3b947a1618fc4085a030f4f70656e5468726561642d393837330102987304105330d857354330133c05e1fd7ae81a910c0402a0f7f8
@@ -0,0 +1,18 @@
esphome:
name: test
esp32:
variant: esp32c6
framework:
type: esp-idf
ethernet:
type: W5500
clk_pin: 6
mosi_pin: 7
miso_pin: 2
cs_pin: 10
interrupt_pin: 3
openthread:
tlv: 0e080000000000010000000300001035060004001fffe00208e227ac6a7f24052f0708fdb753eb517cb4d3051062b2442a928d9ea3b947a1618fc4085a030f4f70656e5468726561642d393837330102987304105330d857354330133c05e1fd7ae81a910c0402a0f7f8
@@ -0,0 +1,17 @@
esphome:
name: test
esp32:
board: esp32dev
framework:
type: esp-idf
network:
enable_ipv6: true
enable_ipv4: false
wifi:
ssid: "test_ssid"
password: "test_password"
ap:
ssid: "fallback"
@@ -0,0 +1,16 @@
esphome:
name: test
esp32:
board: esp32dev
framework:
type: esp-idf
network:
enable_ipv6: true
enable_ipv4: false
min_ipv6_addr_count: 2
wifi:
ssid: "test_ssid"
password: "test_password"
@@ -0,0 +1,16 @@
esphome:
name: test
esp32:
board: esp32dev
framework:
type: esp-idf
network:
enable_ipv6: true
enable_ipv4: false
wifi:
ssid: "test_ssid"
password: "test_password"
lease_cache: true
@@ -0,0 +1,19 @@
esphome:
name: test
esp32:
board: esp32dev
framework:
type: esp-idf
network:
enable_ipv6: true
enable_ipv4: false
wifi:
ssid: "test_ssid"
password: "test_password"
manual_ip:
static_ip: 192.168.1.10
gateway: 192.168.1.1
subnet: 255.255.255.0
+40 -3
View File
@@ -6,6 +6,7 @@ from pathlib import Path
import pytest
from voluptuous import Invalid
from esphome.components.esp32.const import KEY_ESP32, KEY_SDKCONFIG_OPTIONS
from esphome.components.network import (
CONF_ENABLE_IPV4,
_final_validate,
@@ -57,9 +58,13 @@ def test_require_ipv6_and_request_ipv4_off_record_state() -> None:
("ipv6_off_nothing_required.yaml", "true", "false"),
("ipv4_and_ipv6_both_required.yaml", "true", "true"),
("openthread_with_wake_on_lan.yaml", "true", "true"),
("openthread_border_router_wifi.yaml", "true", "true"),
("openthread_with_ethernet.yaml", "true", "true"),
("esp32_arduino_ipv6_no_ipv4_requirement.yaml", "true", "true"),
("host_ipv6_no_ipv4_requirement.yaml", "true", "true"),
("nrf52_network_no_openthread.yaml", "false", "true"),
("wifi_ipv6_only.yaml", "false", "true"),
("ethernet_ipv6_only.yaml", "false", "true"),
],
)
def test_ip_family_defines(
@@ -78,7 +83,10 @@ def test_ip_family_defines(
@pytest.mark.parametrize(
"config_file",
[
"wifi_ipv4_disabled_rejected.yaml",
"wifi_manual_ip_ipv4_disabled_rejected.yaml",
"wifi_ap_ipv4_disabled_rejected.yaml",
"wifi_lease_cache_ipv4_disabled_rejected.yaml",
"ethernet_manual_ip_ipv4_disabled_rejected.yaml",
"wake_on_lan_ipv4_disabled_rejected.yaml",
"udp_ipv4_disabled_rejected.yaml",
"tcp_uart_ipv4_disabled_rejected.yaml",
@@ -102,14 +110,18 @@ def test_require_ipv4_registration_rejects_explicit_disable(
[
("tcp_uart_ipv4_disabled_rejected.yaml", "tcp_uart"),
("uart_tcp_ipv4_disabled_rejected.yaml", "uart_tcp"),
("wifi_manual_ip_ipv4_disabled_rejected.yaml", "wifi: manual_ip"),
("wifi_ap_ipv4_disabled_rejected.yaml", "wifi: ap"),
("wifi_lease_cache_ipv4_disabled_rejected.yaml", "wifi: lease_cache"),
("ethernet_manual_ip_ipv4_disabled_rejected.yaml", "ethernet: manual_ip"),
],
)
def test_tcp_link_users_named_in_ipv4_error(
def test_ipv4_error_names_the_requirer(
component_config_path: Callable[[str], Path],
config_file: str,
component: str,
) -> None:
"""The IPv4 requirement registered by the socket helper names the component."""
"""The IPv4 error names the component or option that needs it."""
CORE.config_path = component_config_path(config_file)
res = load_config({})
assert any(f"IPv4 is required by {component}" in str(err) for err in res.errors)
@@ -193,6 +205,31 @@ def test_no_ip_defines_without_network_component(
assert _define_value("USE_NETWORK_IPV6") is None
@pytest.mark.parametrize(
("config_file", "min_count", "ipv6_dns"),
[
("wifi_ipv6_only.yaml", "1", True),
("ethernet_ipv6_only.yaml", "1", True),
("wifi_ipv6_only_min_2.yaml", "2", True),
("ipv6_only.yaml", "0", False),
("openthread_no_explicit_network.yaml", "1", False),
],
)
def test_ipv6_minimum_and_dns_options(
generate_main: Callable[[str | Path], str],
component_config_path: Callable[[str], Path],
config_file: str,
min_count: str,
ipv6_dns: bool,
) -> None:
"""Without IPv4 a link needs an IPv6 address; IPv6 DNS options are only for IPv6-only Wi-Fi and Ethernet."""
generate_main(component_config_path(config_file))
assert _define_value("USE_NETWORK_MIN_IPV6_ADDR_COUNT") == min_count
options = CORE.data[KEY_ESP32][KEY_SDKCONFIG_OPTIONS]
assert ("CONFIG_LWIP_IPV6_DHCP6" in options) is ipv6_dns
assert ("CONFIG_LWIP_IPV6_RDNSS_MAX_DNS_SERVERS" in options) is ipv6_dns
@pytest.mark.parametrize(
("config_file", "tcp"),
[
@@ -0,0 +1,38 @@
"""Tests for the nextion TFT upload watchdog timeout validation."""
import pytest
from esphome.components.nextion.base_component import CONF_TFT_UPLOAD_WATCHDOG_TIMEOUT
from esphome.components.nextion.display import FINAL_VALIDATE_SCHEMA
import esphome.config_validation as cv
from esphome.const import CONF_WATCHDOG_TIMEOUT, PLATFORM_ESP32, PlatformFramework
from esphome.core import TimePeriod, TimePeriodMilliseconds
from tests.component_tests.types import SetCoreConfigCallable
NEXTION_CONFIG = {CONF_TFT_UPLOAD_WATCHDOG_TIMEOUT: TimePeriodMilliseconds(seconds=30)}
def _esp32(set_core_config: SetCoreConfigCallable, watchdog_seconds: int) -> None:
set_core_config(
PlatformFramework.ESP32_IDF,
full_config={
PLATFORM_ESP32: {
CONF_WATCHDOG_TIMEOUT: TimePeriod(seconds=watchdog_seconds)
}
},
)
def test_tft_upload_watchdog_accepted_when_platform_enabled(
set_core_config: SetCoreConfigCallable,
) -> None:
_esp32(set_core_config, 5)
FINAL_VALIDATE_SCHEMA(NEXTION_CONFIG)
def test_tft_upload_watchdog_rejected_when_platform_disabled(
set_core_config: SetCoreConfigCallable,
) -> None:
_esp32(set_core_config, 0)
with pytest.raises(cv.Invalid, match="Cannot be set when esp32.watchdog_timeout"):
FINAL_VALIDATE_SCHEMA(NEXTION_CONFIG)
@@ -0,0 +1,19 @@
esphome:
name: test
esp32:
board: esp32dev
framework:
type: esp-idf
wifi:
ssid: test
password: testtest
external_components:
- source:
type: local
path: ../../../test_build_components/common/web_server_https/components
components: [https_test]
https_test:
@@ -0,0 +1,19 @@
esphome:
name: test
esp32:
board: esp32dev
framework:
type: arduino
wifi:
ssid: test
password: testtest
external_components:
- source:
type: local
path: ../../../test_build_components/common/web_server_https/components
components: [https_test]
https_test:
@@ -0,0 +1,20 @@
esphome:
name: test
esp32:
board: esp32dev
framework:
type: esp-idf
version: 5.4.2
wifi:
ssid: test
password: testtest
external_components:
- source:
type: local
path: ../../../test_build_components/common/web_server_https/components
components: [https_test]
https_test:
@@ -0,0 +1,21 @@
esphome:
name: test
esp32:
board: esp32dev
framework:
type: esp-idf
wifi:
ssid: test
password: testtest
external_components:
- source:
type: local
path: ../../../test_build_components/common/web_server_https/components
components: [https_test]
https_test:
captive_portal:
@@ -0,0 +1,21 @@
esphome:
name: test
esp32:
board: esp32dev
framework:
type: esp-idf
wifi:
ssid: test
password: testtest
external_components:
- source:
type: local
path: ../../../test_build_components/common/web_server_https/components
components: [https_test]
https_test:
prometheus:
@@ -0,0 +1,21 @@
esphome:
name: test
esp32:
board: esp32dev
framework:
type: esp-idf
wifi:
ssid: test
password: testtest
external_components:
- source:
type: local
path: ../../../test_build_components/common/web_server_https/components
components: [https_test]
https_test:
web_server:
@@ -0,0 +1,124 @@
"""Tests for HTTPS on the ESP32 web server."""
from collections.abc import Callable, Generator
from pathlib import Path
import sys
import pytest
from esphome import loader
from esphome.components.esp32 import KEY_BOARD, KEY_VARIANT, VARIANT_ESP32, idf_version
from esphome.components.esp32.const import (
KEY_ESP32,
KEY_EXCLUDE_COMPONENTS,
KEY_SDKCONFIG_OPTIONS,
)
from esphome.components.web_server_base import get_httpd_client_count
from esphome.components.web_server_idf import HTTPS_MAX_CONNECTIONS, consume_ssl_sockets
from esphome.config import read_config
import esphome.config_validation as cv
from esphome.const import PlatformFramework
from esphome.core import CORE
from tests.component_tests.helpers import get_define_value
from tests.component_tests.types import SetCoreConfigCallable
ESP32_PLATFORM_DATA = {KEY_BOARD: "esp32dev", KEY_VARIANT: VARIANT_ESP32}
@pytest.fixture(autouse=True)
def unload_https_test() -> Generator[None]:
"""Drop the test-only external component so later tests start clean."""
yield
loader._COMPONENT_CACHE.pop("https_test", None)
sys.modules.pop("esphome.components.https_test", None)
loader.clear_component_meta_finders()
def test_consume_ssl_sockets_sets_up_https(
generate_main: Callable[[str | Path], str],
component_config_path: Callable[[str], Path],
) -> None:
"""A component with consume_ssl_sockets() in its schema compiles HTTPS in, with the server side of TLS."""
generate_main(component_config_path("https.yaml"))
sdkconfig = CORE.data[KEY_ESP32][KEY_SDKCONFIG_OPTIONS]
assert "USE_WEBSERVER_SSL" in {define.name for define in CORE.defines}
assert get_define_value("WEB_SERVER_MAX_OPEN_SOCKETS") == str(HTTPS_MAX_CONNECTIONS)
assert get_httpd_client_count() == HTTPS_MAX_CONNECTIONS
assert sdkconfig["CONFIG_ESP_HTTPS_SERVER_ENABLE"]
assert "esp_https_server" not in CORE.data[KEY_ESP32][KEY_EXCLUDE_COMPONENTS]
assert not sdkconfig.get("CONFIG_MBEDTLS_TLS_CLIENT_ONLY")
def test_no_https_without_consume_ssl_sockets(
generate_main: Callable[[str | Path], str],
component_config_path: Callable[[str], Path],
) -> None:
"""Without consume_ssl_sockets() nothing HTTPS is compiled in."""
generate_main(component_config_path("web_server.yaml"))
sdkconfig = CORE.data[KEY_ESP32][KEY_SDKCONFIG_OPTIONS]
assert "USE_WEBSERVER_SSL" not in {define.name for define in CORE.defines}
assert "CONFIG_ESP_HTTPS_SERVER_ENABLE" not in sdkconfig
assert "esp_https_server" in CORE.data[KEY_ESP32][KEY_EXCLUDE_COMPONENTS]
def test_https_sockets_reserved_once(set_core_config: SetCoreConfigCallable) -> None:
"""The listener is shared, so a second component using HTTPS reserves no more connections."""
set_core_config(PlatformFramework.ESP32_IDF, platform_data=ESP32_PLATFORM_DATA)
consume_ssl_sockets({})
consume_ssl_sockets({})
assert get_httpd_client_count() == HTTPS_MAX_CONNECTIONS
def test_https_sockets_not_reserved_off_esp32(
set_core_config: SetCoreConfigCallable,
) -> None:
"""HTTPS exists only on ESP32, so other platforms reserve no connections for it."""
set_core_config(PlatformFramework.RP2040_ARDUINO)
consume_ssl_sockets({})
assert get_httpd_client_count() == 0
@pytest.mark.parametrize(
("config_file", "component"),
[
("https_with_web_server.yaml", "web_server"),
("https_with_captive_portal.yaml", "captive_portal"),
("https_with_prometheus.yaml", "prometheus"),
],
)
def test_ssl_rejects_incompatible_components(
component_config_path: Callable[[str], Path],
capsys: pytest.CaptureFixture[str],
config_file: str,
component: str,
) -> None:
"""HTTPS can't be combined with components that need plain HTTP."""
CORE.config_path = component_config_path(config_file)
assert read_config({}) is None
assert f"can't be used together with {component} yet" in capsys.readouterr().out
def test_ssl_needs_idf_5_5(
component_config_path: Callable[[str], Path],
capsys: pytest.CaptureFixture[str],
) -> None:
"""HTTPS is rejected before ESP-IDF 5.5, which added the TLS handshake timeout."""
CORE.config_path = component_config_path("https_idf_5_4.yaml")
assert read_config({}) is None
assert "HTTPS needs ESP-IDF 5.5 or later" in capsys.readouterr().out
def test_ssl_on_arduino_checks_its_idf_version(
component_config_path: Callable[[str], Path],
) -> None:
"""On Arduino the check uses the ESP-IDF the core is built on, not the Arduino version."""
CORE.config_path = component_config_path("https_arduino.yaml")
assert read_config({}) is not None
assert idf_version() >= cv.Version(5, 5, 0)
-294
View File
@@ -140,298 +140,4 @@ TEST(FrameReaderTest, SixteenBitIndex) {
EXPECT_EQ(frames[0][299], 0x42);
}
// Feeds bytes and collects every frame the reader hands over, rejecting those reject() picks.
template<size_t N, class... F, class R>
static Frames feed_all(LengthFrameReader<N, F...> &reader, const Bytes &input, R reject) {
Frames frames;
auto handle = [&](const uint8_t *frame, size_t len) {
frames.emplace_back(frame, frame + len);
return !reject(frames.back());
};
for (uint8_t c : input)
reader.feed(c, handle);
return frames;
}
template<size_t N, class... F> static Frames feed_all(LengthFrameReader<N, F...> &reader, const Bytes &input) {
return feed_all(reader, input, [](const Bytes & /*frame*/) { return false; });
}
// pioneer_wt: A5 01, one byte at 7 holds the whole length
using OneByte = FrameFormat<FrameHeader<0xA5, 0x01>, FrameLengthU8<LengthField{.offset = 7}>>;
// tuya: 55 AA, version, command, payload length (2, BE), payload, checksum
using TwoByteBE = FrameFormat<FrameHeader<0x55, 0xAA>, PayloadLengthU16BE<LengthField{.offset = 4, .trailer = 1}>>;
// ld24xx command and data frames: header (4), payload length (2, LE), payload, footer (4)
using LdCmd =
FrameFormat<FrameHeader<0xFD, 0xFC, 0xFB, 0xFA>, PayloadLengthU16LE<LengthField{.offset = 4, .trailer = 4}>>;
using LdData =
FrameFormat<FrameHeader<0xF4, 0xF3, 0xF2, 0xF1>, PayloadLengthU16LE<LengthField{.offset = 4, .trailer = 4}>>;
// ld2410s short frame: 6E, always 5 bytes
using LdShort = FrameFormat<FrameHeader<0x6E>, FrameFixedLength<5>>;
// Small format for the recovery tests: A5, one byte at 1 holds the whole length
using Small = FrameFormat<FrameHeader<0xA5>, FrameLengthU8<LengthField{.offset = 1}>>;
static_assert(sizeof(LengthFrameReader<255, OneByte>) == 258);
static_assert(sizeof(LengthFrameReader<300, TwoByteBE>) == 306);
TEST(LengthFrameReaderTest, OneByteLength) {
LengthFrameReader<16, OneByte> reader;
EXPECT_EQ(feed_all(reader, {0x00, 0xA5, 0x01, 0x10, 0x11, 0x12, 0x13, 0x00, 0x09, 0x42}),
(Frames{{0xA5, 0x01, 0x10, 0x11, 0x12, 0x13, 0x00, 0x09, 0x42}}));
}
TEST(LengthFrameReaderTest, TwoByteBigEndianPayloadLength) {
LengthFrameReader<300, TwoByteBE> reader;
// Payload of 0x0102 = 258 bytes: 265 in all, past an 8-bit index
Bytes input{0x55, 0xAA, 0x03, 0x07, 0x01, 0x02};
input.resize(265, 0x33);
Frames frames = feed_all(reader, input);
ASSERT_EQ(frames.size(), 1u);
EXPECT_EQ(frames[0].size(), 265u);
// Too long for N: not a frame
EXPECT_EQ(feed_all(reader, {0x55, 0xAA, 0x03, 0x07, 0x01, 0x30}), Frames{});
EXPECT_EQ(reader.length(), 0u);
}
TEST(LengthFrameReaderTest, LittleEndianLengthsAboveAByte) {
// Whole-frame length 0x0102 = 258, low byte first
LengthFrameReader<300, FrameFormat<FrameHeader<0xA5>, FrameLengthU16LE<LengthField{.offset = 1}>>> whole;
Bytes input{0xA5, 0x02, 0x01};
input.resize(258, 0x33);
Frames frames = feed_all(whole, input);
ASSERT_EQ(frames.size(), 1u);
EXPECT_EQ(frames[0].size(), 258u);
// Payload length 0x0100 = 256 after the field, then a 1 byte trailer: 260 in all
LengthFrameReader<300, FrameFormat<FrameHeader<0xA5>, PayloadLengthU16LE<LengthField{.offset = 1, .trailer = 1}>>>
payload;
input = {0xA5, 0x00, 0x01};
input.resize(260, 0x44);
frames = feed_all(payload, input);
ASSERT_EQ(frames.size(), 1u);
EXPECT_EQ(frames[0].size(), 260u);
}
TEST(LengthFrameReaderTest, TwoFormatsPickedByFirstByte) {
LengthFrameReader<32, LdCmd, LdData> reader;
const Bytes cmd{0xFD, 0xFC, 0xFB, 0xFA, 0x02, 0x00, 0xA0, 0x01, 0x04, 0x03, 0x02, 0x01};
const Bytes data{0xF4, 0xF3, 0xF2, 0xF1, 0x01, 0x00, 0x55, 0xF8, 0xF7, 0xF6, 0xF5};
Bytes input{0x00};
input.insert(input.end(), data.begin(), data.end());
input.insert(input.end(), cmd.begin(), cmd.end());
EXPECT_EQ(feed_all(reader, input), (Frames{data, cmd}));
}
TEST(LengthFrameReaderTest, FixedAndLengthFormatsTogether) {
LengthFrameReader<32, LdShort, LdData> reader;
const Bytes small{0x6E, 0x01, 0x02, 0x03, 0x62};
const Bytes data{0xF4, 0xF3, 0xF2, 0xF1, 0x00, 0x00, 0xF8, 0xF7, 0xF6, 0xF5};
Bytes input = small;
input.insert(input.end(), data.begin(), data.end());
input.insert(input.end(), small.begin(), small.end());
EXPECT_EQ(feed_all(reader, input), (Frames{small, data, small}));
}
TEST(LengthFrameReaderTest, BrokenHeaderRealigns) {
LengthFrameReader<32, LdCmd, LdData> reader;
// FD FC then F4 breaks the command header and starts a data header
const Bytes data{0xF4, 0xF3, 0xF2, 0xF1, 0x00, 0x00, 0xF8, 0xF7, 0xF6, 0xF5};
Bytes input{0xFD, 0xFC};
input.insert(input.end(), data.begin(), data.end());
EXPECT_EQ(feed_all(reader, input), (Frames{data}));
}
TEST(LengthFrameReaderTest, BrokenHeaderHoldsAShorterFrameOfAnotherFormat) {
// FD FC FB then 77 breaks the command header; its replay finds a whole 2 byte FC frame and keeps the 77 behind it
LengthFrameReader<32, LdCmd, FrameFormat<FrameHeader<0xFC>, FrameFixedLength<2>>> reader;
EXPECT_EQ(feed_all(reader, {0xFD, 0xFC, 0xFB, 0x77}), (Frames{{0xFC, 0xFB}}));
EXPECT_EQ(reader.length(), 0u);
// The held 77 was noise, so the next frame is found normally
EXPECT_EQ(feed_all(reader, {0xFC, 0x01}), (Frames{{0xFC, 0x01}}));
}
TEST(LengthFrameReaderTest, RejectedFrameHoldsFramesOfBothFormats) {
LengthFrameReader<32, LdShort, Small> reader;
// A rejected 12 byte A5 frame holds a whole short frame, a whole A5 frame and the start of another
auto reject_12 = [](const Bytes &frame) { return frame.size() == 12; };
EXPECT_EQ(feed_all(reader, {0xA5, 0x0C, 0x6E, 0x01, 0x02, 0x03, 0x62, 0xA5, 0x03, 0x09, 0xA5, 0x05}, reject_12),
(Frames{{0xA5, 0x0C, 0x6E, 0x01, 0x02, 0x03, 0x62, 0xA5, 0x03, 0x09, 0xA5, 0x05},
{0x6E, 0x01, 0x02, 0x03, 0x62},
{0xA5, 0x03, 0x09}}));
EXPECT_EQ(reader.length(), 2u);
EXPECT_EQ(feed_all(reader, {0x11, 0x22, 0x33}), (Frames{{0xA5, 0x05, 0x11, 0x22, 0x33}}));
}
TEST(LengthFrameReaderTest, BadLengthRealignsAfterFirstByte) {
LengthFrameReader<8, Small> reader;
// 0x01 ends the frame before its length byte and 0x09 is longer than 8: neither is a frame.
// The second A5 inside the bad one starts the real frame.
EXPECT_EQ(feed_all(reader, {0xA5, 0x01, 0xA5, 0x09, 0xA5, 0x04, 0x01, 0x02}), (Frames{{0xA5, 0x04, 0x01, 0x02}}));
}
TEST(LengthFrameReaderTest, LengthByteCanStartHeader) {
LengthFrameReader<8, FrameFormat<FrameHeader<0x09>, FrameLengthU8<LengthField{.offset = 1}>>> reader;
// Length 9 is longer than 8, so the length byte becomes the next header
EXPECT_EQ(feed_all(reader, {0x09, 0x09, 0x03, 0x07}), (Frames{{0x09, 0x03, 0x07}}));
}
TEST(LengthFrameReaderTest, LengthTracksPartialFrame) {
LengthFrameReader<8, Small> reader;
EXPECT_EQ(feed_all(reader, {0x01, 0xA5, 0x05}), Frames{});
EXPECT_EQ(reader.length(), 2u);
EXPECT_EQ(feed_all(reader, {0x10, 0x20, 0x30}), (Frames{{0xA5, 0x05, 0x10, 0x20, 0x30}}));
EXPECT_EQ(reader.length(), 0u);
}
TEST(LengthFrameReaderTest, RejectedFrameIsSearchedForAHeader) {
LengthFrameReader<8, Small> reader;
// The A5 taken as a header was payload; the real frame starts at the second A5
auto reject_long = [](const Bytes &frame) { return frame.size() == 5; };
EXPECT_EQ(feed_all(reader, {0xA5, 0x05, 0x00, 0xA5, 0x04}, reject_long), (Frames{{0xA5, 0x05, 0x00, 0xA5, 0x04}}));
EXPECT_EQ(reader.length(), 2u);
EXPECT_EQ(feed_all(reader, {0x10, 0x20}, reject_long), (Frames{{0xA5, 0x04, 0x10, 0x20}}));
}
TEST(LengthFrameReaderTest, RejectionRecoversEveryFrameInside) {
LengthFrameReader<16, Small> reader;
// The 9 byte frame is rejected; the two frames inside it are handed over too, and a partial one is kept
auto reject_9 = [](const Bytes &frame) { return frame.size() == 9; };
EXPECT_EQ(feed_all(reader, {0xA5, 0x09, 0xA5, 0x03, 0x01, 0xA5, 0x02, 0xA5, 0x04}, reject_9),
(Frames{{0xA5, 0x09, 0xA5, 0x03, 0x01, 0xA5, 0x02, 0xA5, 0x04}, {0xA5, 0x03, 0x01}, {0xA5, 0x02}}));
EXPECT_EQ(reader.length(), 2u);
EXPECT_EQ(feed_all(reader, {0x01, 0x02}), (Frames{{0xA5, 0x04, 0x01, 0x02}}));
}
TEST(LengthFrameReaderTest, RecoveredFrameRejectedTooIsSearched) {
LengthFrameReader<16, Small> reader;
// The 9 byte frame holds a 4 byte candidate, which holds a 3 byte one; both outer ones are rejected
auto reject_outer = [](const Bytes &frame) { return frame.size() != 3; };
EXPECT_EQ(
feed_all(reader, {0xA5, 0x09, 0xA5, 0x04, 0xA5, 0x03, 0x01, 0x02, 0x03}, reject_outer),
(Frames{{0xA5, 0x09, 0xA5, 0x04, 0xA5, 0x03, 0x01, 0x02, 0x03}, {0xA5, 0x04, 0xA5, 0x03}, {0xA5, 0x03, 0x01}}));
EXPECT_EQ(reader.length(), 0u);
}
TEST(LengthFrameReaderTest, FlushHandsOverFramesHeldBack) {
LengthFrameReader<16, Small> reader;
Frames seen;
auto handle = [&seen](const uint8_t *frame, size_t len) {
seen.emplace_back(frame, frame + len);
return true;
};
// A corrupt length of 15 holds back two complete frames until the caller gives up on the partial one
for (uint8_t c : {0xA5, 0x0F, 0xA5, 0x04, 0x01, 0x02, 0xA5, 0x03, 0x09})
reader.feed(c, handle);
EXPECT_EQ(seen, Frames{});
EXPECT_EQ(reader.length(), 9u);
reader.flush(handle);
EXPECT_EQ(seen, (Frames{{0xA5, 0x04, 0x01, 0x02}, {0xA5, 0x03, 0x09}}));
EXPECT_EQ(reader.length(), 0u);
reader.flush(handle); // nothing collected: nothing to do
EXPECT_EQ(seen.size(), 2u);
}
TEST(LengthFrameReaderTest, FlushKeepsASlowFrameUntilTheRestArrives) {
LengthFrameReader<16, Small> reader;
Frames seen;
auto handle = [&seen](const uint8_t *frame, size_t len) {
seen.emplace_back(frame, frame + len);
return true;
};
for (uint8_t c : {0xA5, 0x06, 0xA5, 0x03})
reader.feed(c, handle);
reader.flush(handle);
EXPECT_EQ(seen, Frames{});
EXPECT_EQ(reader.length(), 4u);
for (uint8_t c : {0x01, 0x02})
reader.feed(c, handle);
EXPECT_EQ(seen, (Frames{{0xA5, 0x06, 0xA5, 0x03, 0x01, 0x02}}));
}
TEST(LengthFrameReaderTest, FlushKeepsASlowFrameBehindTheFramesItHandsOver) {
LengthFrameReader<16, Small> reader;
Frames seen;
auto handle = [&seen](const uint8_t *frame, size_t len) {
seen.emplace_back(frame, frame + len);
return true;
};
// A corrupt length holds a whole frame, then the start of a slow one
for (uint8_t c : {0xA5, 0x0F, 0xA5, 0x04, 0x01, 0x02, 0xA5, 0x05, 0x11})
reader.feed(c, handle);
reader.flush(handle);
reader.flush(handle);
EXPECT_EQ(seen, (Frames{{0xA5, 0x04, 0x01, 0x02}}));
EXPECT_EQ(reader.length(), 3u);
for (uint8_t c : {0x22, 0x33})
reader.feed(c, handle);
EXPECT_EQ(seen, (Frames{{0xA5, 0x04, 0x01, 0x02}, {0xA5, 0x05, 0x11, 0x22, 0x33}}));
}
TEST(LengthFrameReaderTest, FlushKeepsTheFrameWhenEveryFrameInsideIsRejected) {
LengthFrameReader<16, Small> reader;
size_t calls = 0;
auto reject = [&calls](const uint8_t *, size_t) {
calls++;
return false;
};
for (uint8_t c : {0xA5, 0x0F, 0xA5, 0x03, 0x09})
reader.feed(c, reject);
reader.flush(reject);
EXPECT_EQ(calls, 1u);
EXPECT_EQ(reader.length(), 5u);
// Searched once until a new byte comes
reader.flush(reject);
EXPECT_EQ(calls, 1u);
}
TEST(LengthFrameReaderTest, ResetForgetsThePartialFrame) {
LengthFrameReader<16, Small> reader;
EXPECT_EQ(feed_all(reader, {0xA5, 0x05, 0x11}), Frames{});
reader.reset();
EXPECT_EQ(reader.length(), 0u);
EXPECT_EQ(feed_all(reader, {0xA5, 0x03, 0x22}), (Frames{{0xA5, 0x03, 0x22}}));
}
TEST(LengthFrameReaderTest, IdleAfterHandledFrameFindsNothingInIt) {
LengthFrameReader<16, Small> reader;
Frames seen;
auto handle = [&seen](const uint8_t *frame, size_t len) {
seen.emplace_back(frame, frame + len);
return true;
};
// A handled frame whose payload looks like a frame of its own, then noise
for (uint8_t c : {0xA5, 0x06, 0xA5, 0x03, 0x01, 0x02, 0x00})
reader.feed(c, handle);
reader.flush(handle);
EXPECT_EQ(seen, (Frames{{0xA5, 0x06, 0xA5, 0x03, 0x01, 0x02}}));
}
// A custom rule known at the first byte that reads the buffer builds (only FrameFixedLength is checked up front)
struct FirstByteLength {
static constexpr size_t KNOWN_AT = 1;
static constexpr size_t get(const uint8_t *buf) { return buf[0]; }
};
TEST(LengthFrameReaderTest, CustomRuleKnownAtTheFirstByte) {
LengthFrameReader<8, FrameFormat<FrameHeader<0x03>, FirstByteLength>> reader;
EXPECT_EQ(feed_all(reader, {0x03, 0x01, 0x02, 0x03}), (Frames{{0x03, 0x01, 0x02}}));
}
// A5, payload length at 1, payload, checksum; a frame has at least 2 payload bytes
using MinTwo =
FrameFormat<FrameHeader<0xA5>, PayloadLengthU8<LengthField{.offset = 1, .trailer = 1}>, FrameMinLength<5>>;
TEST(LengthFrameReaderTest, LengthBelowTheMinimumIsRejectedAtTheHeader) {
LengthFrameReader<16, MinTwo> reader;
EXPECT_EQ(feed_all(reader, {0xA5, 0x00, 0xA5, 0x02, 0x10, 0x20, 0x30}), (Frames{{0xA5, 0x02, 0x10, 0x20, 0x30}}));
}
static bool second_byte_not_ff(const uint8_t *buf) { return buf[1] != 0xFF; }
// A5, a byte that must not be FF, payload length at 2, payload
using Checked =
FrameFormat<FrameHeader<0xA5>, CheckedLength<PayloadLengthU8<LengthField{.offset = 2}>, 3, second_byte_not_ff>>;
TEST(LengthFrameReaderTest, CheckedLengthRejectsABadHeader) {
LengthFrameReader<16, Checked> reader;
EXPECT_EQ(feed_all(reader, {0xA5, 0xFF, 0x01, 0xA5, 0x00, 0x01, 0x42}), (Frames{{0xA5, 0x00, 0x01, 0x42}}));
}
} // namespace esphome::core::testing
@@ -0,0 +1,105 @@
#include <gtest/gtest.h>
#include <vector>
#include "../common.h"
#include "esphome/components/epaper_spi/epaper_spi_ssd1677_bwr.h"
namespace esphome::epaper_spi::testing {
namespace {
class TestableSSD1677BWR : public EPaperSSD1677BWR {
public:
TestableSSD1677BWR(uint16_t width, uint16_t height, uint16_t offset_height)
: EPaperSSD1677BWR("test", width, height, nullptr, 0, offset_height) {}
void install(spi::SPIDelegate *delegate) {
this->delegate_ = delegate;
this->set_dc_pin(&this->dc);
ASSERT_TRUE(this->init_buffer_(this->buffer_length_));
}
void draw(int x, int y, Color color) { this->draw_pixel_at(x, y, color); }
/// Both planes of one push; returns how many calls it took.
int run_push() {
int calls = 1;
while (!this->transfer_data())
calls++;
return calls;
}
/// A whole update with the frame already drawn, run through loop() as the main loop would.
void run_update() {
this->set_auto_clear(false);
this->state_ = EPaperState::UPDATE;
const uint32_t deadline = millis() + 2000;
while (this->state_ != EPaperState::IDLE) {
ASSERT_LT(millis(), deadline) << "update did not finish";
this->loop();
}
}
RecordingPin dc;
};
using Bytes = std::vector<uint8_t>;
const Color RED(255, 0, 0);
/// The window is the panel's rows at the end of the gates, written from the last gate down, with X in pixels.
/// One row of 16 pixels: white, black, red, then white. The red plane goes first as 1=red, then the black/white
/// plane as 1=white with red pixels sent as 0.
TEST(EPaperSSD1677BWRTransfer, PixelsReachThePanel) {
TestableSSD1677BWR display(16, 2, 3);
RecordingDelegate bus(&display.dc);
display.install(&bus);
display.fill(Color::WHITE);
display.draw(1, 0, Color::BLACK);
display.draw(2, 0, RED);
EXPECT_EQ(display.run_push(), 1);
EXPECT_EQ(bus.commands, (Bytes{0x44, 0x45, 0x4E, 0x4F, 0x26, 0x24}));
EXPECT_EQ(bus.data[0x44], (Bytes{0x00, 0x00, 0x0F, 0x00}));
EXPECT_EQ(bus.data[0x45], (Bytes{0x04, 0x00, 0x03, 0x00}));
EXPECT_EQ(bus.data[0x4E], (Bytes{0x00, 0x00}));
EXPECT_EQ(bus.data[0x4F], (Bytes{0x04, 0x00}));
EXPECT_EQ(bus.data[0x26], (Bytes{0x20, 0x00, 0x00, 0x00}));
EXPECT_EQ(bus.data[0x24], (Bytes{0x9F, 0xFF, 0xFF, 0xFF}));
}
/// Addresses past 255 use both bytes: the 880x528 panel on the last 528 of 680 gates.
TEST(EPaperSSD1677BWRTransfer, WindowOfTheFullSizePanel) {
TestableSSD1677BWR display(880, 528, 152);
RecordingDelegate bus(&display.dc);
display.install(&bus);
display.run_push();
EXPECT_EQ(bus.data[0x44], (Bytes{0x00, 0x00, 0x6F, 0x03}));
EXPECT_EQ(bus.data[0x45], (Bytes{0xA7, 0x02, 0x98, 0x00}));
EXPECT_EQ(bus.data[0x4F], (Bytes{0xA7, 0x02}));
EXPECT_EQ(bus.data[0x26].size(), 110u * 528u);
EXPECT_EQ(bus.data[0x24].size(), 110u * 528u);
}
/// A whole update starts with a software reset, and sends no separate power-on before the refresh (0x22 0xF7).
/// Power off (0x83) and deep sleep follow.
TEST(EPaperSSD1677BWRTransfer, UpdateSequence) {
TestableSSD1677BWR display(16, 1, 0);
RecordingDelegate bus(&display.dc);
display.install(&bus);
display.fill(RED);
display.run_update();
EXPECT_EQ(bus.commands, (Bytes{0x12, 0x44, 0x45, 0x4E, 0x4F, 0x26, 0x24, 0x22, 0x20, 0x22, 0x20, 0x10}));
EXPECT_EQ(bus.data[0x26], (Bytes{0xFF, 0xFF}));
EXPECT_EQ(bus.data[0x24], (Bytes{0x00, 0x00}));
EXPECT_EQ(bus.data[0x22], (Bytes{0xF7, 0x83}));
EXPECT_EQ(bus.data[0x10], (Bytes{0x01}));
}
} // namespace
} // namespace esphome::epaper_spi::testing
@@ -544,6 +544,27 @@ display:
it.circle(it.get_width() / 2, it.get_height() / 2, 100, Color::BLACK);
it.circle(it.get_width() / 2, it.get_height() / 2, 60, Color(255, 0, 0));
# Waveshare 7.5" HD (B) (880x528, SSD1677 controller, EPD_7in5b_HD)
- platform: epaper_spi
spi_id: spi_bus
model: waveshare-7.5in-hd-b
cs_pin:
allow_other_uses: true
number: GPIO5
dc_pin:
allow_other_uses: true
number: GPIO17
reset_pin:
allow_other_uses: true
number: GPIO16
busy_pin:
allow_other_uses: true
number: GPIO4
lambda: |-
it.filled_rectangle(0, 0, it.get_width(), it.get_height(), Color::WHITE);
it.circle(it.get_width() / 2, it.get_height() / 2, 100, Color::BLACK);
it.circle(it.get_width() / 2, it.get_height() / 2, 60, Color(255, 0, 0));
# Waveshare 7.5" V2 mono (800x480, UC8179 controller, EPD_7in5_V2)
# full_update_every > 1 exercises the fast/partial refresh paths
- platform: epaper_spi
@@ -0,0 +1,15 @@
esp32:
board: esp32dev
framework:
type: esp-idf
watchdog_timeout: 0s
wifi:
ssid: MySSID
password: password1
status_led:
pin: GPIO2
http_request:
verify_ssl: false
@@ -0,0 +1,14 @@
network:
enable_ipv6: true
min_ipv6_addr_count: 2
enable_ipv4: false
ethernet:
type: LAN8720
mdc_pin: 23
mdio_pin: 32
clk:
pin: 0
mode: CLK_EXT_IN
phy_addr: 0
power_pin: 33
@@ -1,7 +1,6 @@
#include <algorithm>
#include "common.h"
#include "esphome/components/uart/uart.h"
#include "esphome/core/frame_reader.h"
namespace esphome::uart::testing {
@@ -180,58 +179,4 @@ TEST(UARTReadAvailableTest, DiscardStopsOnReadFailure) {
EXPECT_EQ(uart.read_sizes.size(), 3u);
}
// AA, length byte, then that many bytes; the handler takes every frame
using GapReader = LengthFrameReader<16, FrameFormat<FrameHeader<0xAA>, PayloadLengthU8<LengthField{.offset = 1}>>>;
TEST(UARTReadFramesTest, FeedsTheReaderAndStampsTheTime) {
::testing::NiceMock<FakeRxUART> uart(std::vector<uint8_t>{0xAA, 0x01, 0x07});
UARTDevice dev(&uart);
GapReader reader;
std::vector<std::vector<uint8_t>> frames;
auto handle = [&frames](const uint8_t *f, size_t len) {
frames.emplace_back(f, f + len);
return true;
};
uint32_t last_rx = 0;
dev.read_frames(reader, handle, 500, last_rx);
EXPECT_EQ(last_rx, 500u);
EXPECT_EQ(frames, (std::vector<std::vector<uint8_t>>{{0xAA, 0x01, 0x07}}));
}
TEST(UARTReadFramesTest, IdlePassAfterTheGapFlushesAStalledFrame) {
// A corrupt length of 9 holds the whole frame AA 01 07 behind it
::testing::NiceMock<FakeRxUART> uart(std::vector<uint8_t>{0xAA, 0x09, 0xAA, 0x01, 0x07});
UARTDevice dev(&uart);
GapReader reader;
std::vector<std::vector<uint8_t>> frames;
auto handle = [&frames](const uint8_t *f, size_t len) {
frames.emplace_back(f, f + len);
return f[1] != 0x09;
};
uint32_t last_rx = 0;
dev.read_frames(reader, handle, 1000, last_rx, 100);
dev.read_frames(reader, handle, 1099, last_rx, 100);
EXPECT_TRUE(frames.empty());
dev.read_frames(reader, handle, 1100, last_rx, 100);
EXPECT_EQ(frames, (std::vector<std::vector<uint8_t>>{{0xAA, 0x01, 0x07}}));
EXPECT_EQ(reader.length(), 0u);
}
TEST(UARTReadFramesTest, BytesAfterASlowLoopAreFedNotFlushed) {
::testing::NiceMock<FakeRxUART> uart(std::vector<uint8_t>{0xAA, 0x02, 0x01});
UARTDevice dev(&uart);
GapReader reader;
std::vector<std::vector<uint8_t>> frames;
auto handle = [&frames](const uint8_t *f, size_t len) {
frames.emplace_back(f, f + len);
return true;
};
uint32_t last_rx = 0;
dev.read_frames(reader, handle, 1000, last_rx, 100);
// The rest of the frame waited out a 500 ms loop
uart.append({0x02});
dev.read_frames(reader, handle, 1500, last_rx, 100);
EXPECT_EQ(frames, (std::vector<std::vector<uint8_t>>{{0xAA, 0x02, 0x01, 0x02}}));
}
} // namespace esphome::uart::testing
@@ -6,9 +6,9 @@
#include <random>
#include <string>
#include "esphome/components/web_server_idf/sse_chunk.h"
#include "esphome/components/web_server/sse_chunk.h"
namespace esphome::web_server_idf::testing {
namespace esphome::web_server::testing {
// The chunk as try_send_nodefer lays it out: header, fields, first "data: ", then the pieces
static std::string build_chunk(const char *message, size_t message_len, const char *event, uint32_t id,
@@ -128,4 +128,4 @@ TEST(SseChunk, MatchesReferenceOnRandomMessages) {
}
}
} // namespace esphome::web_server_idf::testing
} // namespace esphome::web_server::testing
@@ -0,0 +1,300 @@
#ifdef USE_HOST
#include <gtest/gtest.h>
#include <sys/uio.h>
#include <cstdint>
#include <cstdio>
#include <string>
#include <vector>
#include "esphome/components/web_server/sse_chunk.h"
#include "esphome/components/web_server_base/stream_head.h"
namespace esphome::web_server::testing {
using namespace web_server_base; // NOLINT(google-build-using-namespace)
// Collects every batch write_stream_head() sends
struct Capture {
std::string bytes;
std::vector<int> batch_sizes;
int attempts{0};
int fail_after_batches{-1};
bool operator()(struct iovec *iov, int count) {
this->attempts++;
if (static_cast<int>(this->batch_sizes.size()) == this->fail_after_batches) {
return false;
}
this->batch_sizes.push_back(count);
for (int i = 0; i < count; i++) {
this->bytes.append(static_cast<const char *>(iov[i].iov_base), iov[i].iov_len);
}
return true;
}
};
static std::string build_head(Capture &cap, const char *status, const char *content_type,
std::span<const HttpHeader> headers, std::span<const HttpHeader> defaults,
const char *body) {
EXPECT_TRUE(write_stream_head<struct iovec>(cap, status, content_type, headers, defaults, body));
return cap.bytes;
}
// What httpd_resp_send_chunk() put on the wire for its first chunk (esp_http_server/src/httpd_txrx.c in
// ESP-IDF 5.5): the chunked head with Transfer-Encoding, each httpd_resp_set_hdr() header, the blank line,
// then "%lx\r\n", the chunk data and "\r\n".
static std::string reference_send_chunk(const char *status, const char *content_type,
const std::vector<HttpHeader> &resp_hdrs, const char *buf, size_t buf_len) {
char head[256];
std::string out(
head, snprintf(head, sizeof(head), "HTTP/1.1 %s\r\nContent-Type: %s\r\nTransfer-Encoding: chunked\r\n", status,
content_type));
for (const auto &hdr : resp_hdrs) {
out.append(hdr.name).append(": ").append(hdr.value).append("\r\n");
}
out.append("\r\n");
char len_str[10];
out.append(len_str, snprintf(len_str, sizeof(len_str), "%lx\r\n", static_cast<long>(buf_len)));
out.append(buf, buf_len);
out.append("\r\n");
return out;
}
static constexpr HttpHeader DEFAULTS[] = {
{"Access-Control-Allow-Origin", "*"},
{"Access-Control-Allow-Private-Network", "true"},
};
TEST(StreamHead, SseHeadMatchesHttpdRespSendChunk) {
// The old event source set Cache-Control and Connection, then the default headers, and sent "\r\n" as a chunk
const std::string expected = reference_send_chunk(
"200 OK", "text/event-stream",
{{"Cache-Control", "no-cache"}, {"Connection", "keep-alive"}, DEFAULTS[0], DEFAULTS[1]}, "\r\n", 2);
Capture cap;
EXPECT_EQ(build_head(cap, SSE_STATUS, SSE_CONTENT_TYPE, SSE_HEADERS, DEFAULTS, SSE_FIRST_CHUNK), expected);
}
TEST(StreamHead, HeaderReplacesDefaultOfSameName) {
// A default the caller also sets, in any case, goes out once with the caller's value
static constexpr HttpHeader HEADERS[] = {
{"access-control-allow-origin", "http://example.com"},
};
Capture cap;
EXPECT_EQ(build_head(cap, "200 OK", "text/plain", HEADERS, DEFAULTS, nullptr),
"HTTP/1.1 200 OK\r\nContent-Type: text/plain\r\naccess-control-allow-origin: http://example.com"
"\r\nAccess-Control-Allow-Private-Network: true\r\n\r\n");
}
TEST(StreamHead, RawHeadWithCustomHeadersAndNoBody) {
static constexpr HttpHeader HEADERS[] = {
{"Cache-Control", "no-store"},
{"X-Stream", "audio"},
};
Capture cap;
EXPECT_EQ(build_head(cap, "200 OK", "audio/wav", HEADERS, {}, nullptr), "HTTP/1.1 200 OK\r\n"
"Content-Type: audio/wav\r\n"
"Cache-Control: no-store\r\n"
"X-Stream: audio\r\n"
"\r\n");
}
TEST(StreamHead, BareHead) {
Capture cap;
EXPECT_EQ(build_head(cap, "204 No Content", "text/plain", {}, {}, nullptr),
"HTTP/1.1 204 No Content\r\nContent-Type: text/plain\r\n\r\n");
EXPECT_EQ(cap.batch_sizes, std::vector<int>{5});
}
TEST(StreamHead, ManyHeadersSplitIntoBatchesWithSameBytes) {
// 4 + 12 * 4 + 2 = 54 pieces, more than two batches hold
static constexpr HttpHeader HEADERS[] = {
{"X-Header-0", "value-0"}, {"X-Header-1", "value-1"}, {"X-Header-2", "value-2"}, {"X-Header-3", "value-3"},
{"X-Header-4", "value-4"}, {"X-Header-5", "value-5"}, {"X-Header-6", "value-6"}, {"X-Header-7", "value-7"},
{"X-Header-8", "value-8"}, {"X-Header-9", "value-9"},
};
const std::span<const HttpHeader> headers{HEADERS};
Capture batched;
const std::string bytes = build_head(batched, "200 OK", "text/plain", headers, DEFAULTS, "body");
EXPECT_EQ(batched.batch_sizes, (std::vector<int>{STREAM_HEAD_MAX_IOV, STREAM_HEAD_MAX_IOV, 6}));
std::string expected = "HTTP/1.1 200 OK\r\nContent-Type: text/plain";
const auto append_headers = [&expected](std::span<const HttpHeader> list) {
for (const auto &h : list) {
expected.append("\r\n").append(h.name).append(": ").append(h.value);
}
};
append_headers(headers);
append_headers(DEFAULTS);
expected.append("\r\n\r\nbody");
EXPECT_EQ(bytes, expected);
}
TEST(StreamHead, BatchBoundary) {
// 4 + 4 * 4 + 1 + 1 = 22 pieces fit one batch; a fifth header makes 4 + 5 * 4 = 24 before the blank line
static constexpr HttpHeader HEADERS[] = {{"A", "1"}, {"B", "2"}, {"C", "3"}, {"D", "4"}, {"E", "5"}};
Capture four;
build_head(four, "200 OK", "text/plain", std::span(HEADERS, 4), {}, "x");
EXPECT_EQ(four.batch_sizes, std::vector<int>{22});
EXPECT_EQ(stream_head_iov_count(4), 22u);
Capture five;
const std::string bytes = build_head(five, "200 OK", "text/plain", HEADERS, {}, "x");
EXPECT_EQ(five.batch_sizes, (std::vector<int>{STREAM_HEAD_MAX_IOV, 2}));
EXPECT_EQ(bytes, "HTTP/1.1 200 OK\r\nContent-Type: text/plain\r\nA: 1\r\nB: 2\r\nC: 3\r\nD: 4\r\nE: 5\r\n\r\nx");
}
TEST(StreamHead, FailedSendSkipsLaterBatches) {
// Three batches; the second fails, so the third is never sent
std::vector<HttpHeader> headers(12, HttpHeader{"X", "y"});
Capture full;
build_head(full, "200 OK", "text/plain", headers, {}, nullptr);
ASSERT_EQ(full.batch_sizes.size(), 3u);
Capture cap;
cap.fail_after_batches = 1;
EXPECT_FALSE(write_stream_head<struct iovec>(cap, "200 OK", "text/plain", headers, {}, nullptr));
EXPECT_EQ(cap.attempts, 2);
EXPECT_EQ(cap.batch_sizes.size(), 1u);
}
// Three elements of 3, 4 and 5 bytes
struct IovFixture {
char a[4] = "abc";
char b[5] = "defg";
char c[6] = "hijkl";
struct iovec iov[3] = {{a, 3}, {b, 4}, {c, 5}};
struct iovec *cur = iov;
int count = 3;
std::string rest() const {
std::string out;
for (int i = 0; i < this->count; i++) {
out.append(static_cast<const char *>(this->cur[i].iov_base), this->cur[i].iov_len);
}
return out;
}
};
TEST(AdvanceIovecs, ZeroLengthWriteChangesNothing) {
IovFixture f;
advance_iovecs(f.cur, f.count, 0);
EXPECT_EQ(f.cur, f.iov);
EXPECT_EQ(f.count, 3);
EXPECT_EQ(f.rest(), "abcdefghijkl");
}
TEST(AdvanceIovecs, PartialInsideFirstElement) {
IovFixture f;
advance_iovecs(f.cur, f.count, 2);
EXPECT_EQ(f.cur, f.iov);
EXPECT_EQ(f.count, 3);
EXPECT_EQ(f.cur->iov_len, 1u);
EXPECT_EQ(f.rest(), "cdefghijkl");
}
TEST(AdvanceIovecs, ExactlyOnElementBoundary) {
IovFixture f;
advance_iovecs(f.cur, f.count, 3);
EXPECT_EQ(f.cur, &f.iov[1]);
EXPECT_EQ(f.count, 2);
EXPECT_EQ(f.rest(), "defghijkl");
}
TEST(AdvanceIovecs, AcrossElementBoundaries) {
IovFixture f;
advance_iovecs(f.cur, f.count, 9);
EXPECT_EQ(f.cur, &f.iov[2]);
EXPECT_EQ(f.count, 1);
EXPECT_EQ(f.rest(), "jkl");
}
TEST(AdvanceIovecs, RepeatedShortWrites) {
IovFixture f;
std::string sent;
while (f.count > 0) {
const std::string before = f.rest();
advance_iovecs(f.cur, f.count, 2);
sent += before.substr(0, before.size() - f.rest().size());
}
EXPECT_EQ(sent, "abcdefghijkl");
}
TEST(AdvanceIovecs, EverythingWritten) {
IovFixture f;
advance_iovecs(f.cur, f.count, 12);
EXPECT_EQ(f.count, 0);
EXPECT_EQ(f.rest(), "");
}
TEST(AdvanceIovecs, SkipsZeroLengthElements) {
char a[] = "ab";
char c[] = "cd";
struct iovec iov[3] = {{a, 2}, {nullptr, 0}, {c, 2}};
struct iovec *cur = iov;
int count = 3;
advance_iovecs(cur, count, 2);
EXPECT_EQ(cur, &iov[2]);
EXPECT_EQ(count, 1);
}
static constexpr uint32_t TIMEOUT = 20000;
TEST(StallClock, NotStarted) {
StallClock clock;
EXPECT_FALSE(clock.is_running());
EXPECT_FALSE(clock.is_expired(0, TIMEOUT));
EXPECT_FALSE(clock.is_expired(UINT32_MAX, TIMEOUT));
}
TEST(StallClock, ExpiresAtExactlyTheTimeout) {
StallClock clock;
clock.start(1000);
EXPECT_TRUE(clock.is_running());
EXPECT_FALSE(clock.is_expired(1000, TIMEOUT));
EXPECT_FALSE(clock.is_expired(1000 + TIMEOUT - 1, TIMEOUT));
EXPECT_TRUE(clock.is_expired(1000 + TIMEOUT, TIMEOUT));
EXPECT_TRUE(clock.is_expired(1000 + TIMEOUT + 1, TIMEOUT));
EXPECT_EQ(clock.elapsed(1000 + TIMEOUT), TIMEOUT);
}
TEST(StallClock, RunningClockKeepsItsStart) {
StallClock clock;
clock.start(1000);
clock.start(5000);
EXPECT_TRUE(clock.is_expired(1000 + TIMEOUT, TIMEOUT));
}
TEST(StallClock, Cleared) {
StallClock clock;
clock.start(1000);
clock.clear();
EXPECT_FALSE(clock.is_running());
EXPECT_FALSE(clock.is_expired(1000 + TIMEOUT, TIMEOUT));
clock.start(50000);
EXPECT_FALSE(clock.is_expired(50000 + TIMEOUT - 1, TIMEOUT));
EXPECT_TRUE(clock.is_expired(50000 + TIMEOUT, TIMEOUT));
}
TEST(StallClock, StartAtZeroStillRuns) {
StallClock clock;
clock.start(0);
EXPECT_TRUE(clock.is_running());
// Recorded as 1, so it expires one millisecond late
EXPECT_FALSE(clock.is_expired(TIMEOUT, TIMEOUT));
EXPECT_TRUE(clock.is_expired(TIMEOUT + 1, TIMEOUT));
}
TEST(StallClock, AcrossMillisWraparound) {
StallClock clock;
const uint32_t start = UINT32_MAX - 5000;
clock.start(start);
EXPECT_FALSE(clock.is_expired(UINT32_MAX, TIMEOUT));
EXPECT_FALSE(clock.is_expired(start + TIMEOUT - 1, TIMEOUT)); // wrapped to 14998
EXPECT_TRUE(clock.is_expired(start + TIMEOUT, TIMEOUT));
EXPECT_EQ(clock.elapsed(start + TIMEOUT), TIMEOUT);
}
} // namespace esphome::web_server::testing
#endif // USE_HOST
@@ -0,0 +1,10 @@
packages:
common: !include common.yaml
# HTTPS through the developer API, from a test-only component that supplies its own certificate. The source path is
# relative to the build directory (tests/test_build_components/build/).
external_components:
- source: ../common/web_server_https/components
components: [https_test]
https_test:
@@ -0,0 +1,7 @@
wifi:
ssid: MySSID
password: password1
network:
enable_ipv6: true
enable_ipv4: false
@@ -0,0 +1,30 @@
"""Test-only component that serves HTTPS through the web_server_idf developer API."""
import esphome.codegen as cg
from esphome.components import web_server_idf
from esphome.components.web_server_base import CONF_WEB_SERVER_BASE_ID, WebServerBase
import esphome.config_validation as cv
from esphome.const import CONF_ID
from esphome.types import ConfigType
AUTO_LOAD = ["web_server_base"]
https_test_ns = cg.esphome_ns.namespace("https_test")
HttpsTest = https_test_ns.class_("HttpsTest", cg.Component)
CONFIG_SCHEMA = cv.All(
cv.Schema(
{
cv.GenerateID(): cv.declare_id(HttpsTest),
cv.GenerateID(CONF_WEB_SERVER_BASE_ID): cv.use_id(WebServerBase),
}
).extend(cv.COMPONENT_SCHEMA),
cv.only_on_esp32,
web_server_idf.consume_ssl_sockets,
)
async def to_code(config: ConfigType) -> None:
base = await cg.get_variable(config[CONF_WEB_SERVER_BASE_ID])
var = cg.new_Pvariable(config[CONF_ID], base)
await cg.register_component(var, config)
@@ -0,0 +1,44 @@
#pragma once
#include "esphome/components/web_server_base/web_server_base.h"
#include "esphome/core/component.h"
namespace esphome::https_test {
// Self-signed test certificate and key, for this build test only.
static constexpr const char *CERTIFICATE = R"(-----BEGIN CERTIFICATE-----
MIIBkDCCATegAwIBAgIUMMwcTkVDiMSsim4eX/Cdul/ZnsIwCgYIKoZIzj0EAwIw
HTEbMBkGA1UEAwwSZXNwaG9tZS10ZXN0LmxvY2FsMCAXDTI2MTEwMjIzMTYzNFoY
DzIxMjYxMDA5MjMxNjM0WjAdMRswGQYDVQQDDBJlc3Bob21lLXRlc3QubG9jYWww
WTATBgcqhkjOPQIBBggqhkjOPQMBBwNCAASUqX7QDmtpdjZAP4DgMjaN4JIO7mYj
6FGx7tz5MZCKrz/lkTJY9wMYFfQ0+fp6X69lP76nEkbBmgmc2isz4Uo2o1MwUTAd
BgNVHQ4EFgQUzEmb4w1pKH4RGSm+lUH3dJgCHm4wHwYDVR0jBBgwFoAUzEmb4w1p
KH4RGSm+lUH3dJgCHm4wDwYDVR0TAQH/BAUwAwEB/zAKBggqhkjOPQQDAgNHADBE
AiBn6XgNZA8M9FCLsheo80/VePtC6mci/MmzKrsUVHddmwIgUgouxiAVVswudya9
kedDMCS2/klleJS8/Imr9J05eJ4=
-----END CERTIFICATE-----
)";
static constexpr const char *PRIVATE_KEY = R"(-----BEGIN PRIVATE KEY-----
MIGHAgEAMBMGByqGSM49AgEGCCqGSM49AwEHBG0wawIBAQQgOWIIXfeG3RJdg0CW
IRGNs0g7EbJUIzpQtCIUZbJ+jXGhRANCAASUqX7QDmtpdjZAP4DgMjaN4JIO7mYj
6FGx7tz5MZCKrz/lkTJY9wMYFfQ0+fp6X69lP76nEkbBmgmc2isz4Uo2
-----END PRIVATE KEY-----
)";
// Starts the shared web server with HTTPS on 443, the way a component that supplies its own certificate would.
class HttpsTest : public Component {
public:
explicit HttpsTest(web_server_base::WebServerBase *base) : base_(base) {}
void setup() override {
this->base_->set_port(443);
this->base_->set_ssl_credentials(CERTIFICATE, PRIVATE_KEY);
this->base_->init();
}
float get_setup_priority() const override { return setup_priority::WIFI - 1.0f; }
protected:
web_server_base::WebServerBase *base_;
};
} // namespace esphome::https_test
+106
View File
@@ -17,6 +17,7 @@ from esphome.const import (
CONF_BUILD_PATH,
CONF_DEVICES,
CONF_ESPHOME,
CONF_FRIENDLY_NAME,
CONF_INCLUDES,
CONF_INCLUDES_C,
CONF_NAME,
@@ -28,6 +29,7 @@ from esphome.const import (
)
from esphome.core import CORE, ID, KEY_CONTROLLER_REGISTRY_CONTROLLERS, config
from esphome.core.config import (
CONF_FRIENDLY_NAME_SEPARATOR,
CONF_LOOP_INTERVAL,
CONF_RELATION,
CONF_SUSPEND_LOOP,
@@ -42,6 +44,7 @@ from esphome.core.config import (
valid_include,
valid_project_name,
validate_area_config,
validate_friendly_name_with_suffix,
validate_hostname,
)
import esphome.final_validate as fv
@@ -329,6 +332,19 @@ def test_loop_interval_warn_esp32(
)
def test_loop_interval_no_warn_esp32_watchdog_disabled(
yaml_file: Callable[[str], str],
caplog: pytest.LogCaptureFixture,
) -> None:
"""With the task watchdog disabled the loop sleep is not capped."""
result = load_config_from_fixture(
yaml_file, "loop_interval_esp32_watchdog_disabled.yaml", FIXTURES_DIR
)
assert result is not None
assert "maximum sleep" not in caplog.text
@pytest.mark.parametrize(
("esphome_config", "expected_ms"),
[
@@ -1686,6 +1702,68 @@ def test_make_app_name_cpp_mac_suffix_space_sep() -> None:
assert byte_len == len("My Device XXXXXX")
@pytest.mark.asyncio
@pytest.mark.parametrize(
("separator_line", "expected"),
[
("", "my device XXXXXX"),
(' friendly_name_separator: "-"\n', "my device-XXXXXX"),
(' friendly_name_separator: ""\n', "my deviceXXXXXX"),
],
)
async def test_friendly_name_separator(
yaml_file: Callable[[str], Path], separator_line: str, expected: str
) -> None:
"""The friendly name joins the MAC suffix with a space unless told otherwise."""
result = load_config_from_yaml(
yaml_file,
"esphome:\n"
" name: my-device\n"
" friendly_name: my device\n"
" name_add_mac_suffix: true\n" + separator_line + "host:\n",
)
assert result is not None
with patch("esphome.core.config.cg") as mock_cg:
mock_cg.RawStatement.side_effect = lambda *args, **kwargs: MagicMock()
mock_cg.RawExpression.side_effect = lambda *args, **kwargs: MagicMock()
await config.to_code(result[CONF_ESPHOME])
statements = [c.args[0] for c in mock_cg.RawStatement.call_args_list]
assert f'static char esphome_app_friendly_name_buf[] = "{expected}";' in statements
assert 'static char esphome_app_name_buf[] = "my-device-XXXXXX";' in statements
@pytest.mark.asyncio
async def test_friendly_name_separator_without_mac_suffix(
yaml_file: Callable[[str], Path],
) -> None:
"""Without the MAC suffix the separator is ignored, as when a device is adopted."""
result = load_config_from_yaml(
yaml_file,
"esphome:\n"
" name: my-device\n"
" friendly_name: my device\n"
" name_add_mac_suffix: false\n"
' friendly_name_separator: "-"\n'
"host:\n",
)
assert result is not None
assert result[CONF_ESPHOME][CONF_FRIENDLY_NAME_SEPARATOR] == "-"
with patch("esphome.core.config.cg") as mock_cg:
mock_cg.RawStatement.side_effect = lambda *args, **kwargs: MagicMock()
mock_cg.RawExpression.side_effect = lambda *args, **kwargs: MagicMock()
await config.to_code(result[CONF_ESPHOME])
assert not [
c
for c in mock_cg.RawStatement.call_args_list
if "esphome_app_friendly_name_buf" in c.args[0]
]
assert '"my device"' in [c.args[0] for c in mock_cg.RawExpression.call_args_list]
def test_make_app_name_cpp_non_ascii_utf8_length() -> None:
"""Test non-ASCII characters use UTF-8 byte length."""
_, global_decl, byte_len = make_app_name_cpp(
@@ -1966,3 +2044,31 @@ def test_filter_source_files_drops_util_cpp_without_mqtt() -> None:
CORE.defines = {core.Define("USE_API"), core.Define("USE_MQTT")}
assert "util.cpp" not in config.FILTER_SOURCE_FILES()
@pytest.mark.parametrize(
("friendly_name", "separator", "valid"),
[
("a" * 120, " ", True), # 127 bytes, the most an API short string holds
("a" * 120, " - ", False),
("a" * 118, " - ", True),
("\u00e9" * 60, " ", True), # 120 bytes in UTF-8
("\u00e9" * 60, "--", False),
],
)
def test_validate_friendly_name_with_suffix(
friendly_name: str, separator: str, valid: bool
) -> None:
config = {
CONF_NAME_ADD_MAC_SUFFIX: True,
CONF_FRIENDLY_NAME: friendly_name,
CONF_FRIENDLY_NAME_SEPARATOR: separator,
}
if valid:
assert validate_friendly_name_with_suffix(config) == config
else:
with pytest.raises(cv.Invalid, match="at most 127 fit"):
validate_friendly_name_with_suffix(config)
# Without the suffix nothing is appended
config[CONF_NAME_ADD_MAC_SUFFIX] = False
assert validate_friendly_name_with_suffix(config) == config
@@ -0,0 +1,7 @@
esphome:
name: test
loop_interval: 7s
esp32:
board: esp32dev
watchdog_timeout: 0s