mirror of
https://github.com/esphome/esphome.git
synced 2026-11-17 18:46:08 +00:00
Compare commits
117
Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
d721dd9a62 | ||
|
|
7d7f732592 | ||
|
|
9d6b50e947 | ||
|
|
e490785ad3 | ||
|
|
7f1ba24ab1 | ||
|
|
84dea8078b | ||
|
|
dbf6af2fd7 | ||
|
|
bc6fb7082f | ||
|
|
cfa27cea58 | ||
|
|
5c061f9e1e | ||
|
|
34b24d0cf5 | ||
|
|
e27614fda3 | ||
|
|
8426d04138 | ||
|
|
641c8bedc9 | ||
|
|
4a39ca1b71 | ||
|
|
4a218fdbd8 | ||
|
|
d8fc1cd214 | ||
|
|
1e030b7371 | ||
|
|
3b113f2c39 | ||
|
|
73468592a9 | ||
|
|
a1b456150d | ||
|
|
865a883abb | ||
|
|
357a6717c8 | ||
|
|
bcb434e05e | ||
|
|
fb65fcc5f6 | ||
|
|
b5e379a9d4 | ||
|
|
05286dfd5d | ||
|
|
36184f09aa | ||
|
|
449933891e | ||
|
|
a5bfa6dac3 | ||
|
|
5aaa5b0552 | ||
|
|
9ab090a818 | ||
|
|
02d0bb56a6 | ||
|
|
5412b46618 | ||
|
|
205318ee1a | ||
|
|
48d8df449a | ||
|
|
383ac1b41a | ||
|
|
f31d7660c9 | ||
|
|
a9e3b2a801 | ||
|
|
f5c0923544 | ||
|
|
0ac2299482 | ||
|
|
b58038af63 | ||
|
|
c741de7a47 | ||
|
|
d08139a3c4 | ||
|
|
5626360c37 | ||
|
|
fe6449edca | ||
|
|
1de99dd7b1 | ||
|
|
011c9aa307 | ||
|
|
9cc04c8c90 | ||
|
|
05d3ade5b9 | ||
|
|
291035c80d | ||
|
|
87dca59bee | ||
|
|
07928a96be | ||
|
|
a960d9f161 | ||
|
|
1b2629fb21 | ||
|
|
d0af43c479 | ||
|
|
5733449170 | ||
|
|
a5d9d66f5d | ||
|
|
f2b3f876bb | ||
|
|
1f9532a176 | ||
|
|
5be927619c | ||
|
|
c1ee74d20a | ||
|
|
1fc034970d | ||
|
|
4ac5b55611 | ||
|
|
a1ac45ae28 | ||
|
|
1c6593bd05 | ||
|
|
25182e09fd | ||
|
|
8a5e07e29f | ||
|
|
de94563809 | ||
|
|
b1c0639d58 | ||
|
|
03ca176563 | ||
|
|
b34ad3d556 | ||
|
|
f7b52be670 | ||
|
|
d5317669c9 | ||
|
|
5d0bd88bc6 | ||
|
|
1019e37daa | ||
|
|
c157ca8d0d | ||
|
|
a987ec87bd | ||
|
|
cbb214b7ea | ||
|
|
51be31c0f8 | ||
|
|
9958c3ad5f | ||
|
|
b714d8f69b | ||
|
|
ecabdbb24b | ||
|
|
ffbc4148fb | ||
|
|
2169167908 | ||
|
|
900574c03a | ||
|
|
25ba0e10a7 | ||
|
|
2aad97b6bf | ||
|
|
8031fd0b98 | ||
|
|
5c410105e6 | ||
|
|
516feefad1 | ||
|
|
5380e0ae9f | ||
|
|
1f28686746 | ||
|
|
daebdabf09 | ||
|
|
08921be52d | ||
|
|
5297065113 | ||
|
|
b78f8e55aa | ||
|
|
7b1f9c6bf1 | ||
|
|
3587e29725 | ||
|
|
d4244c5388 | ||
|
|
bec53baf1d | ||
|
|
98fd3786dd | ||
|
|
1f531ff022 | ||
|
|
65e7eb6b06 | ||
|
|
7538864e1e | ||
|
|
c0f97475d3 | ||
|
|
df6fcaa6fe | ||
|
|
eb1b793af9 | ||
|
|
b8ce904e0c | ||
|
|
6befa73b00 | ||
|
|
02211e15d8 | ||
|
|
dbbe02f423 | ||
|
|
97a1aa781e | ||
|
|
0b67a754cd | ||
|
|
320b6a8221 | ||
|
|
4e5a5ec32d | ||
|
|
09ce8d7d6e |
No files matched your search
@@ -515,18 +515,12 @@ void CC1101Component::dump_config() {
|
||||
}
|
||||
|
||||
void CC1101Component::begin_tx() {
|
||||
this->wake_up();
|
||||
// Read the mode before the PKTCTRL0 write below overwrites it in state_
|
||||
const bool packet_mode = this->state_.PKT_FORMAT == static_cast<uint8_t>(PacketFormat::PACKET_FORMAT_FIFO);
|
||||
// Ensure Packet Format is 3 (Async Serial)
|
||||
this->write_(Register::PKTCTRL0, 0x32);
|
||||
if (this->state_.PKT_FORMAT == static_cast<uint8_t>(PacketFormat::PACKET_FORMAT_FIFO)) {
|
||||
ESP_LOGW(TAG, "begin_tx is for async serial mode; use send_packet in packet mode");
|
||||
return;
|
||||
}
|
||||
ESP_LOGV(TAG, "Beginning TX sequence");
|
||||
if (this->gdo0_pin_ != nullptr) {
|
||||
// setup() only attaches the interrupt in packet mode. In async serial mode the pin may be
|
||||
// shared with remote_receiver, and detaching here would remove the receiver's interrupt.
|
||||
if (packet_mode) {
|
||||
this->gdo0_pin_->detach_interrupt();
|
||||
}
|
||||
this->gdo0_pin_->pin_mode(gpio::FLAG_OUTPUT);
|
||||
}
|
||||
// Transition through IDLE to bypass CCA (Clear Channel Assessment) which can
|
||||
|
||||
@@ -63,13 +63,25 @@ void CST816Touchscreen::update_touches() {
|
||||
uint8_t data[13];
|
||||
if (!this->status_comm_result(this->read_bytes(REG_STATUS, data, sizeof data)))
|
||||
return;
|
||||
uint8_t num_of_touches = data[REG_TOUCH_NUM] & 3;
|
||||
|
||||
// The low nibble is the touch count. CST816/CST820 are single-touch;
|
||||
// CST826/CST836 can report two points, but this driver only reads the first
|
||||
// point. Some CST8xx controllers report an all-0xFF frame when idle or after
|
||||
// disabling auto-sleep: the count lands above two and the coordinates are
|
||||
// the 0xFFF sentinel, so both guards below reject it.
|
||||
uint8_t num_of_touches = low_nibble(data[REG_TOUCH_NUM]);
|
||||
if (num_of_touches == 0) {
|
||||
return;
|
||||
}
|
||||
if (num_of_touches > 2) {
|
||||
return;
|
||||
}
|
||||
|
||||
uint16_t x = encode_uint16(low_nibble(data[REG_XPOS_HIGH]), data[REG_XPOS_LOW]);
|
||||
uint16_t y = encode_uint16(low_nibble(data[REG_YPOS_HIGH]), data[REG_YPOS_LOW]);
|
||||
if (x == 0x0FFF || y == 0x0FFF) {
|
||||
return;
|
||||
}
|
||||
ESP_LOGV(TAG, "Read touch %d/%d", x, y);
|
||||
this->add_raw_touch_position_(0, x, y);
|
||||
}
|
||||
|
||||
@@ -94,7 +94,6 @@ UART_SELECTION_RP2040 = [USB_CDC, UART0, UART1]
|
||||
|
||||
UART_SELECTION_NRF52 = [USB_CDC, UART0]
|
||||
|
||||
# Read by device-builder
|
||||
HARDWARE_UART_TO_UART_SELECTION = {
|
||||
UART0: logger_ns.UART_SELECTION_UART0,
|
||||
UART0_SWAP: logger_ns.UART_SELECTION_UART0_SWAP,
|
||||
@@ -539,17 +538,17 @@ async def to_code(config: ConfigType) -> None:
|
||||
cg.add(log.set_boot_log_level(tag, LOG_LEVELS[log_level]))
|
||||
if CORE.is_esp32 or CORE.is_host:
|
||||
cg.add(log.create_pthread_key())
|
||||
if (hardware_uart := config.get(CONF_HARDWARE_UART)) is not None:
|
||||
cg.add_define(f"USE_LOGGER_UART_SELECTION_{hardware_uart}")
|
||||
cg.add_define(
|
||||
"ESPHOME_LOGGER_UART_SELECTION",
|
||||
cg.RawExpression(f"logger::UART_SELECTION_{hardware_uart}"),
|
||||
)
|
||||
# ESP8266 without serial logging never opens a port
|
||||
cg.add_define(
|
||||
"ESPHOME_LOGGER_UART_NAME",
|
||||
"NONE" if CORE.is_esp8266 and baud_rate == 0 else hardware_uart,
|
||||
)
|
||||
# set_uart_selection() must be called before pre_setup() because
|
||||
# pre_setup() switches on uart_ to decide which hardware to initialize
|
||||
# (e.g. UART0 vs USB_SERIAL_JTAG). Without this, uart_ is still the
|
||||
# default UART_SELECTION_UART0 and the wrong hardware gets initialized.
|
||||
# uart_ is UART0 in C++ except on LibreTiny where it is DEFAULT; skip the
|
||||
# setter when the config matches it.
|
||||
cpp_default_uart = DEFAULT if CORE.is_libretiny else UART0
|
||||
if (
|
||||
hardware_uart := config.get(CONF_HARDWARE_UART)
|
||||
) is not None and hardware_uart != cpp_default_uart:
|
||||
cg.add(log.set_uart_selection(HARDWARE_UART_TO_UART_SELECTION[hardware_uart]))
|
||||
# pre_setup() sets global_logger and must run before any other code
|
||||
# that may call ESP_LOG* (e.g. setup_preferences contains ESP_LOGVV).
|
||||
cg.add(log.pre_setup())
|
||||
@@ -646,6 +645,7 @@ async def _late_logger_init(config: ConfigType) -> None:
|
||||
add_idf_sdkconfig_option("CONFIG_ESP_CONSOLE_USB_CDC", True)
|
||||
elif config[CONF_HARDWARE_UART] == USB_SERIAL_JTAG:
|
||||
add_idf_sdkconfig_option("CONFIG_ESP_CONSOLE_USB_SERIAL_JTAG", True)
|
||||
cg.add_define("USE_LOGGER_UART_SELECTION_USB_SERIAL_JTAG")
|
||||
# Log V2 formats centrally in esp_log() (~4KB flash, ~180B RAM). 6.1
|
||||
# adds CONSTRAINED_ENV_SAFE=n, keeping early/DRAM logs on
|
||||
# esp_rom_printf instead of pulling ~1.2KB esp_rom_vprintf into IRAM.
|
||||
@@ -663,12 +663,10 @@ async def _late_logger_init(config: ConfigType) -> None:
|
||||
# --wrap: a strong definition collides; see logger_esp32.cpp
|
||||
cg.add_build_flag("-Wl,--wrap=esp_log_format")
|
||||
# Zephyr polls the USB port only when it logs there
|
||||
if (
|
||||
CORE.using_zephyr
|
||||
and has_serial_logging
|
||||
and config[CONF_HARDWARE_UART] == USB_CDC
|
||||
if config.get(CONF_HARDWARE_UART) == USB_CDC and (
|
||||
has_serial_logging or not CORE.is_nrf52
|
||||
):
|
||||
cg.add_define("USE_LOGGER_ZEPHYR_CDC_LOOP")
|
||||
cg.add_define("USE_LOGGER_UART_SELECTION_USB_CDC")
|
||||
# Define platform support flags for components that need auto-detection
|
||||
try:
|
||||
uart_selection(USB_SERIAL_JTAG)
|
||||
@@ -886,15 +884,6 @@ def request_logger_level_listeners() -> None:
|
||||
_request_log_listener_slot = cg.slot_counter("ESPHOME_LOG_MAX_LISTENERS")
|
||||
|
||||
|
||||
def request_console() -> None:
|
||||
"""Compile in Logger::console_read() and console_write(); call from to_code.
|
||||
|
||||
On ESP32 a USB_CDC logger only has a console where ESP-IDF sets CONFIG_ESP_CONSOLE_USB_CDC (targets with
|
||||
USB OTG); elsewhere, such as the C3, C6 or H2, both do nothing. Host and Zephyr have no console API.
|
||||
"""
|
||||
cg.add_define("USE_LOGGER_CONSOLE")
|
||||
|
||||
|
||||
def request_log_listener() -> None:
|
||||
"""Reserve a log listener slot; call once per listener from to_code."""
|
||||
cg.add_define("USE_LOG_LISTENERS")
|
||||
|
||||
@@ -248,10 +248,10 @@ Logger::Logger(uint32_t baud_rate) : baud_rate_(baud_rate) {
|
||||
#endif
|
||||
}
|
||||
|
||||
#if defined(USE_ESPHOME_TASK_LOG_BUFFER) || defined(USE_LOGGER_ZEPHYR_CDC_LOOP)
|
||||
#if defined(USE_ESPHOME_TASK_LOG_BUFFER) || (defined(USE_ZEPHYR) && defined(USE_LOGGER_UART_SELECTION_USB_CDC))
|
||||
void Logger::loop() {
|
||||
this->process_messages_();
|
||||
#ifdef USE_LOGGER_ZEPHYR_CDC_LOOP
|
||||
#if defined(USE_ZEPHYR) && defined(USE_LOGGER_UART_SELECTION_USB_CDC)
|
||||
this->cdc_loop_();
|
||||
#endif
|
||||
}
|
||||
@@ -278,7 +278,7 @@ void Logger::process_messages_() {
|
||||
}
|
||||
}
|
||||
// Zephyr with USB CDC keeps the loop running for cdc_loop_()
|
||||
#ifndef USE_LOGGER_ZEPHYR_CDC_LOOP
|
||||
#if !(defined(USE_ZEPHYR) && defined(USE_LOGGER_UART_SELECTION_USB_CDC))
|
||||
else {
|
||||
// A concurrent enable_loop_soon_any_context() still wakes the next iteration
|
||||
this->disable_loop();
|
||||
@@ -371,25 +371,6 @@ static const LogString *get_log_level_str(uint8_t level) {
|
||||
return LogLevelStrings::get_log_str(level, LogLevelStrings::LAST_INDEX);
|
||||
}
|
||||
|
||||
#ifndef USE_HOST
|
||||
const LogString *Logger::get_uart_selection_() {
|
||||
#ifdef USE_LOGGER_UART_SELECTION_DEFAULT
|
||||
// LibreTiny resolves DEFAULT to the board's port only when serial logging is on
|
||||
if (this->baud_rate_ > 0) {
|
||||
switch (this->get_uart()) {
|
||||
case UART_SELECTION_UART0:
|
||||
return LOG_STR("UART0");
|
||||
case UART_SELECTION_UART1:
|
||||
return LOG_STR("UART1");
|
||||
default:
|
||||
return LOG_STR("UART2");
|
||||
}
|
||||
}
|
||||
#endif
|
||||
return LOG_STR(ESPHOME_LOGGER_UART_NAME);
|
||||
}
|
||||
#endif
|
||||
|
||||
void Logger::dump_config() {
|
||||
ESP_LOGCONFIG(TAG,
|
||||
"Logger:\n"
|
||||
|
||||
@@ -124,9 +124,6 @@ enum UARTSelection : uint8_t {
|
||||
UART_SELECTION_UART0_SWAP,
|
||||
#endif // USE_ESP8266
|
||||
};
|
||||
|
||||
// The port chosen by hardware_uart, fixed at build time
|
||||
static constexpr UARTSelection CONFIGURED_UART_SELECTION = ESPHOME_LOGGER_UART_SELECTION;
|
||||
#endif // USE_ESP32 || USE_ESP8266 || USE_RP2 || USE_LIBRETINY || USE_ZEPHYR
|
||||
|
||||
/**
|
||||
@@ -149,7 +146,7 @@ static constexpr UARTSelection CONFIGURED_UART_SELECTION = ESPHOME_LOGGER_UART_S
|
||||
class Logger final : public Component {
|
||||
public:
|
||||
explicit Logger(uint32_t baud_rate);
|
||||
#if defined(USE_ESPHOME_TASK_LOG_BUFFER) || defined(USE_LOGGER_ZEPHYR_CDC_LOOP)
|
||||
#if defined(USE_ESPHOME_TASK_LOG_BUFFER) || (defined(USE_ZEPHYR) && defined(USE_LOGGER_UART_SELECTION_USB_CDC))
|
||||
void loop() override;
|
||||
#endif
|
||||
#if defined(USE_RP2_CRASH_HANDLER) && defined(USE_LOGGER_UART_SELECTION_USB_CDC)
|
||||
@@ -163,11 +160,11 @@ class Logger final : public Component {
|
||||
#endif
|
||||
#ifdef USE_ESP32
|
||||
/// UART_NUM_0 when logging to USB
|
||||
constexpr uart_port_t get_uart_num() const {
|
||||
uart_port_t get_uart_num() const {
|
||||
#if defined(USE_LOGGER_UART_SELECTION_USB_CDC) || defined(USE_LOGGER_UART_SELECTION_USB_SERIAL_JTAG)
|
||||
return UART_NUM_0;
|
||||
#else
|
||||
return static_cast<uart_port_t>(CONFIGURED_UART_SELECTION);
|
||||
return static_cast<uart_port_t>(this->uart_);
|
||||
#endif
|
||||
}
|
||||
#endif
|
||||
@@ -175,35 +172,9 @@ class Logger final : public Component {
|
||||
void create_pthread_key() { pthread_key_create(&this->log_recursion_key_, nullptr); }
|
||||
#endif
|
||||
#if defined(USE_ESP32) || defined(USE_ESP8266) || defined(USE_RP2) || defined(USE_LIBRETINY) || defined(USE_ZEPHYR)
|
||||
void set_uart_selection(UARTSelection uart_selection) { this->uart_ = uart_selection; }
|
||||
/// Get the UART used by the logger.
|
||||
#ifdef USE_LIBRETINY
|
||||
// DEFAULT resolves to the board's default port
|
||||
constexpr UARTSelection get_uart() const {
|
||||
return CONFIGURED_UART_SELECTION == UART_SELECTION_DEFAULT ? static_cast<UARTSelection>(LT_UART_DEFAULT_SERIAL + 1)
|
||||
: CONFIGURED_UART_SELECTION;
|
||||
}
|
||||
#else
|
||||
constexpr UARTSelection get_uart() const { return CONFIGURED_UART_SELECTION; }
|
||||
#endif
|
||||
#endif
|
||||
#ifdef USE_LOGGER_CONSOLE
|
||||
// Raw bytes on the console port, without log framing; request with logger.request_console()
|
||||
// console_write() returns the bytes written: a UART write blocks until all are queued, but a USB write can stop
|
||||
// short when the host is not reading (USB Serial/JTAG gives up after 20 ms)
|
||||
#if defined(USE_ARDUINO) && !defined(USE_ESP32)
|
||||
size_t console_write(const uint8_t *data, size_t len) { return this->hw_serial_->write(data, len); }
|
||||
bool console_read(uint8_t &byte) {
|
||||
int c = this->hw_serial_->read();
|
||||
if (c < 0)
|
||||
return false;
|
||||
byte = c;
|
||||
return true;
|
||||
}
|
||||
#elif defined(USE_ESP32)
|
||||
size_t console_write(const uint8_t *data, size_t len);
|
||||
/// Non-blocking read of one byte; false if none is waiting.
|
||||
bool console_read(uint8_t &byte);
|
||||
#endif
|
||||
UARTSelection get_uart() const { return this->uart_; }
|
||||
#endif
|
||||
|
||||
/// Set the default log level for this logger.
|
||||
@@ -280,7 +251,7 @@ class Logger final : public Component {
|
||||
void log_vprintf_non_main_thread_(uint8_t level, const char *tag, int line, const char *format, va_list args,
|
||||
const char *thread_name);
|
||||
#endif
|
||||
#ifdef USE_LOGGER_ZEPHYR_CDC_LOOP
|
||||
#if defined(USE_ZEPHYR) && defined(USE_LOGGER_UART_SELECTION_USB_CDC)
|
||||
void cdc_loop_();
|
||||
#endif
|
||||
void process_messages_();
|
||||
@@ -440,6 +411,10 @@ class Logger final : public Component {
|
||||
bool boot_log_done_{false}; // Set on first replay; stops further capture
|
||||
#endif
|
||||
uint8_t current_level_{ESPHOME_LOG_LEVEL_VERY_VERBOSE};
|
||||
#if defined(USE_ESP32) || defined(USE_ESP8266) || defined(USE_RP2) || defined(USE_LIBRETINY) || defined(USE_ZEPHYR)
|
||||
// 0 is UART0, or DEFAULT on LibreTiny; must match cpp_default_uart in __init__.py
|
||||
UARTSelection uart_{};
|
||||
#endif
|
||||
bool main_task_recursion_guard_{false}; // Guards tx_buffer_ on the main loop
|
||||
#ifdef USE_LIBRETINY
|
||||
bool non_main_task_recursion_guard_{false}; // Shared guard for all non-main tasks on LibreTiny
|
||||
|
||||
@@ -17,10 +17,6 @@
|
||||
#include <driver/usb_serial_jtag_vfs.h>
|
||||
#endif
|
||||
#endif
|
||||
// The logger accepts USB_CDC on targets without USB OTG (C3, C6, H2, ...), where IDF has no CDC console
|
||||
#if defined(USE_LOGGER_CONSOLE) && defined(USE_LOGGER_UART_SELECTION_USB_CDC) && defined(CONFIG_ESP_CONSOLE_USB_CDC)
|
||||
#include <esp_private/usb_console.h>
|
||||
#endif
|
||||
#if defined(CONFIG_PM_ENABLE) && defined(CONFIG_FREERTOS_USE_TICKLESS_IDLE) && \
|
||||
(ESP_IDF_VERSION >= ESP_IDF_VERSION_VAL(6, 0, 0))
|
||||
#include "esp_sleep.h"
|
||||
@@ -77,7 +73,6 @@ static void init_usb_serial_jtag() {
|
||||
}
|
||||
#endif
|
||||
|
||||
#if !defined(USE_LOGGER_UART_SELECTION_USB_CDC) && !defined(USE_LOGGER_UART_SELECTION_USB_SERIAL_JTAG)
|
||||
static void init_uart(uart_port_t uart_num, uint32_t baud_rate, int tx_buffer_size) {
|
||||
uart_config_t uart_config{};
|
||||
uart_config.baud_rate = (int) baud_rate;
|
||||
@@ -102,7 +97,6 @@ static void init_uart(uart_port_t uart_num, uint32_t baud_rate, int tx_buffer_si
|
||||
esp_sleep_set_console_uart_handling_mode(ESP_SLEEP_ALWAYS_FLUSH_UART);
|
||||
#endif
|
||||
}
|
||||
#endif
|
||||
|
||||
// get_uart_num() and pre_setup() cast the selection straight to a port
|
||||
static_assert(static_cast<int>(UART_SELECTION_UART0) == static_cast<int>(UART_NUM_0) &&
|
||||
@@ -113,11 +107,26 @@ static_assert(static_cast<int>(UART_SELECTION_UART2) == static_cast<int>(UART_NU
|
||||
|
||||
void Logger::pre_setup() {
|
||||
if (this->baud_rate_ > 0) {
|
||||
#ifdef USE_LOGGER_UART_SELECTION_USB_SERIAL_JTAG
|
||||
init_usb_serial_jtag();
|
||||
#elif !defined(USE_LOGGER_UART_SELECTION_USB_CDC)
|
||||
init_uart(this->get_uart_num(), this->baud_rate_, ESPHOME_LOGGER_TX_BUFFER_SIZE);
|
||||
switch (this->uart_) {
|
||||
case UART_SELECTION_UART0:
|
||||
case UART_SELECTION_UART1:
|
||||
#ifdef USE_ESP32_VARIANT_ESP32
|
||||
case UART_SELECTION_UART2:
|
||||
#endif
|
||||
init_uart(static_cast<uart_port_t>(this->uart_), this->baud_rate_, ESPHOME_LOGGER_TX_BUFFER_SIZE);
|
||||
break;
|
||||
#ifdef USE_LOGGER_USB_CDC
|
||||
case UART_SELECTION_USB_CDC:
|
||||
break;
|
||||
#endif
|
||||
#ifdef USE_LOGGER_USB_SERIAL_JTAG
|
||||
case UART_SELECTION_USB_SERIAL_JTAG:
|
||||
#ifdef USE_LOGGER_UART_SELECTION_USB_SERIAL_JTAG
|
||||
init_usb_serial_jtag();
|
||||
#endif
|
||||
break;
|
||||
#endif
|
||||
}
|
||||
}
|
||||
|
||||
global_logger = this;
|
||||
@@ -129,36 +138,29 @@ void Logger::pre_setup() {
|
||||
#endif
|
||||
}
|
||||
|
||||
#ifdef USE_LOGGER_CONSOLE
|
||||
size_t Logger::console_write(const uint8_t *data, size_t len) {
|
||||
int written = 0;
|
||||
#if defined(USE_LOGGER_UART_SELECTION_USB_CDC)
|
||||
#ifdef CONFIG_ESP_CONSOLE_USB_CDC // keep: not set without USB OTG, see the include above
|
||||
written = esp_usb_console_write_buf(reinterpret_cast<const char *>(data), len);
|
||||
const LogString *Logger::get_uart_selection_() {
|
||||
switch (this->uart_) {
|
||||
case UART_SELECTION_UART0:
|
||||
return LOG_STR("UART0");
|
||||
case UART_SELECTION_UART1:
|
||||
return LOG_STR("UART1");
|
||||
#ifdef USE_ESP32_VARIANT_ESP32
|
||||
case UART_SELECTION_UART2:
|
||||
return LOG_STR("UART2");
|
||||
#endif
|
||||
#elif defined(USE_LOGGER_UART_SELECTION_USB_SERIAL_JTAG)
|
||||
written = usb_serial_jtag_write_bytes(data, len, pdMS_TO_TICKS(20));
|
||||
#else
|
||||
written = uart_write_bytes(this->get_uart_num(), data, len);
|
||||
#ifdef USE_LOGGER_USB_CDC
|
||||
case UART_SELECTION_USB_CDC:
|
||||
return LOG_STR("USB_CDC");
|
||||
#endif
|
||||
return written > 0 ? written : 0;
|
||||
#ifdef USE_LOGGER_USB_SERIAL_JTAG
|
||||
case UART_SELECTION_USB_SERIAL_JTAG:
|
||||
return LOG_STR("USB_SERIAL_JTAG");
|
||||
#endif
|
||||
default:
|
||||
return LOG_STR("UNKNOWN");
|
||||
}
|
||||
}
|
||||
|
||||
bool Logger::console_read(uint8_t &byte) {
|
||||
#if defined(USE_LOGGER_UART_SELECTION_USB_CDC)
|
||||
#ifdef CONFIG_ESP_CONSOLE_USB_CDC // keep: not set without USB OTG, see the include above
|
||||
return esp_usb_console_available_for_read() && esp_usb_console_read_buf(reinterpret_cast<char *>(&byte), 1) == 1;
|
||||
#else
|
||||
return false;
|
||||
#endif
|
||||
#elif defined(USE_LOGGER_UART_SELECTION_USB_SERIAL_JTAG)
|
||||
return usb_serial_jtag_read_bytes(&byte, 1, 0) > 0;
|
||||
#else
|
||||
return uart_read_bytes(this->get_uart_num(), &byte, 1, 0) > 0;
|
||||
#endif
|
||||
}
|
||||
#endif
|
||||
|
||||
#ifdef USE_CRASH_HANDLER
|
||||
bool crash_log_pending() { return esp32::crash_handler_has_data(); }
|
||||
void log_crash_to_client() {
|
||||
|
||||
@@ -14,9 +14,9 @@ void Logger::pre_setup() {
|
||||
#if defined(USE_ESP8266_LOGGER_SERIAL)
|
||||
this->hw_serial_ = &Serial;
|
||||
Serial.begin(this->baud_rate_);
|
||||
#ifdef USE_LOGGER_UART_SELECTION_UART0_SWAP
|
||||
Serial.swap();
|
||||
#endif
|
||||
if (this->uart_ == UART_SELECTION_UART0_SWAP) {
|
||||
Serial.swap();
|
||||
}
|
||||
Serial.setDebugOutput(ESPHOME_LOG_LEVEL >= ESPHOME_LOG_LEVEL_VERBOSE);
|
||||
#elif defined(USE_ESP8266_LOGGER_SERIAL1)
|
||||
this->hw_serial_ = &Serial1;
|
||||
@@ -35,6 +35,19 @@ void Logger::pre_setup() {
|
||||
#endif
|
||||
}
|
||||
|
||||
const LogString *Logger::get_uart_selection_() {
|
||||
#if defined(USE_ESP8266_LOGGER_SERIAL)
|
||||
if (this->uart_ == UART_SELECTION_UART0_SWAP) {
|
||||
return LOG_STR("UART0_SWAP");
|
||||
}
|
||||
return LOG_STR("UART0");
|
||||
#elif defined(USE_ESP8266_LOGGER_SERIAL1)
|
||||
return LOG_STR("UART1");
|
||||
#else
|
||||
return LOG_STR("NONE");
|
||||
#endif
|
||||
}
|
||||
|
||||
#ifdef USE_CRASH_HANDLER
|
||||
bool crash_log_pending() { return esp8266::crash_handler_has_data(); }
|
||||
void log_crash_to_client() { esp8266::crash_handler_log(); }
|
||||
|
||||
@@ -9,7 +9,7 @@ void HOT Logger::write_msg_(const char *msg, uint16_t len) {
|
||||
char buffer[TIMESTAMP_LEN + ESPHOME_LOGGER_TX_BUFFER_SIZE + HEADROOM];
|
||||
|
||||
time_t rawtime;
|
||||
time(&rawtime);
|
||||
::time(&rawtime);
|
||||
struct tm timeinfo;
|
||||
localtime_r(&rawtime, &timeinfo); // Thread-safe version
|
||||
size_t pos = strftime(buffer, TIMESTAMP_LEN + 1, "[%H:%M:%S]", &timeinfo);
|
||||
|
||||
@@ -12,28 +12,45 @@ ESPHOME_LOG_TAG(TAG, "logger");
|
||||
|
||||
void Logger::pre_setup() {
|
||||
if (this->baud_rate_ > 0) {
|
||||
#if defined(USE_LOGGER_UART_SELECTION_UART0) && LT_HW_UART0
|
||||
this->hw_serial_ = &Serial0;
|
||||
libretiny::ensure_serial_handle(0);
|
||||
Serial0.begin(this->baud_rate_);
|
||||
#elif defined(USE_LOGGER_UART_SELECTION_UART1) && LT_HW_UART1
|
||||
this->hw_serial_ = &Serial1;
|
||||
libretiny::ensure_serial_handle(1);
|
||||
Serial1.begin(this->baud_rate_);
|
||||
#elif defined(USE_LOGGER_UART_SELECTION_UART2) && LT_HW_UART2
|
||||
this->hw_serial_ = &Serial2;
|
||||
libretiny::ensure_serial_handle(2);
|
||||
Serial2.begin(this->baud_rate_);
|
||||
#else
|
||||
this->hw_serial_ = &Serial;
|
||||
Serial.begin(this->baud_rate_);
|
||||
if (CONFIGURED_UART_SELECTION != UART_SELECTION_DEFAULT) {
|
||||
ESP_LOGW(TAG, " The chosen logger UART port is not available on this board."
|
||||
"The default port was used instead.");
|
||||
}
|
||||
switch (this->uart_) {
|
||||
#if LT_HW_UART0
|
||||
case UART_SELECTION_UART0:
|
||||
this->hw_serial_ = &Serial0;
|
||||
libretiny::ensure_serial_handle(0);
|
||||
Serial0.begin(this->baud_rate_);
|
||||
break;
|
||||
#endif
|
||||
#if LT_HW_UART1
|
||||
case UART_SELECTION_UART1:
|
||||
this->hw_serial_ = &Serial1;
|
||||
libretiny::ensure_serial_handle(1);
|
||||
Serial1.begin(this->baud_rate_);
|
||||
break;
|
||||
#endif
|
||||
#if LT_HW_UART2
|
||||
case UART_SELECTION_UART2:
|
||||
this->hw_serial_ = &Serial2;
|
||||
libretiny::ensure_serial_handle(2);
|
||||
Serial2.begin(this->baud_rate_);
|
||||
break;
|
||||
#endif
|
||||
default:
|
||||
this->hw_serial_ = &Serial;
|
||||
Serial.begin(this->baud_rate_);
|
||||
if (this->uart_ != UART_SELECTION_DEFAULT) {
|
||||
ESP_LOGW(TAG, " The chosen logger UART port is not available on this board."
|
||||
"The default port was used instead.");
|
||||
}
|
||||
break;
|
||||
}
|
||||
|
||||
// change lt_log() port to match default Serial
|
||||
lt_log_set_port(this->get_uart() - 1);
|
||||
if (this->uart_ == UART_SELECTION_DEFAULT) {
|
||||
this->uart_ = (UARTSelection) (LT_UART_DEFAULT_SERIAL + 1);
|
||||
lt_log_set_port(LT_UART_DEFAULT_SERIAL);
|
||||
} else {
|
||||
lt_log_set_port(this->uart_ - 1);
|
||||
}
|
||||
}
|
||||
|
||||
global_logger = this;
|
||||
@@ -43,6 +60,20 @@ void Logger::pre_setup() {
|
||||
#endif
|
||||
}
|
||||
|
||||
const LogString *Logger::get_uart_selection_() {
|
||||
switch (this->uart_) {
|
||||
case UART_SELECTION_DEFAULT:
|
||||
return LOG_STR("DEFAULT");
|
||||
case UART_SELECTION_UART0:
|
||||
return LOG_STR("UART0");
|
||||
case UART_SELECTION_UART1:
|
||||
return LOG_STR("UART1");
|
||||
case UART_SELECTION_UART2:
|
||||
default:
|
||||
return LOG_STR("UART2");
|
||||
}
|
||||
}
|
||||
|
||||
#ifdef USE_LIBRETINY_CRASH_HANDLER
|
||||
bool crash_log_pending() { return libretiny::crash_handler_has_data(); }
|
||||
void log_crash_to_client() { libretiny::crash_handler_log(); }
|
||||
|
||||
@@ -26,16 +26,20 @@ void Logger::setup() {
|
||||
|
||||
void Logger::pre_setup() {
|
||||
if (this->baud_rate_ > 0) {
|
||||
#if defined(USE_LOGGER_UART_SELECTION_USB_CDC)
|
||||
this->hw_serial_ = &Serial;
|
||||
Serial.begin(this->baud_rate_);
|
||||
#elif defined(USE_LOGGER_UART_SELECTION_UART1)
|
||||
this->hw_serial_ = &Serial2;
|
||||
Serial2.begin(this->baud_rate_);
|
||||
#else
|
||||
this->hw_serial_ = &Serial1;
|
||||
Serial1.begin(this->baud_rate_);
|
||||
#endif
|
||||
switch (this->uart_) {
|
||||
case UART_SELECTION_UART0:
|
||||
this->hw_serial_ = &Serial1;
|
||||
Serial1.begin(this->baud_rate_);
|
||||
break;
|
||||
case UART_SELECTION_UART1:
|
||||
this->hw_serial_ = &Serial2;
|
||||
Serial2.begin(this->baud_rate_);
|
||||
break;
|
||||
case UART_SELECTION_USB_CDC:
|
||||
this->hw_serial_ = &Serial;
|
||||
Serial.begin(this->baud_rate_);
|
||||
break;
|
||||
}
|
||||
}
|
||||
global_logger = this;
|
||||
ESP_LOGI(TAG, "Log initialized");
|
||||
@@ -45,6 +49,19 @@ void Logger::pre_setup() {
|
||||
#endif
|
||||
}
|
||||
|
||||
const LogString *Logger::get_uart_selection_() {
|
||||
switch (this->uart_) {
|
||||
case UART_SELECTION_UART0:
|
||||
return LOG_STR("UART0");
|
||||
case UART_SELECTION_UART1:
|
||||
return LOG_STR("UART1");
|
||||
case UART_SELECTION_USB_CDC:
|
||||
return LOG_STR("USB_CDC");
|
||||
default:
|
||||
return LOG_STR("UNKNOWN");
|
||||
}
|
||||
}
|
||||
|
||||
#ifdef USE_CRASH_HANDLER
|
||||
bool crash_log_pending() { return rp2::crash_handler_has_data(); }
|
||||
void log_crash_to_client() { rp2::crash_handler_log(); }
|
||||
|
||||
@@ -42,7 +42,7 @@ __attribute__((section(".noinit"))) struct {
|
||||
|
||||
ESPHOME_LOG_TAG(TAG, "logger");
|
||||
|
||||
#ifdef USE_LOGGER_ZEPHYR_CDC_LOOP
|
||||
#ifdef USE_LOGGER_UART_SELECTION_USB_CDC
|
||||
void Logger::cdc_loop_() {
|
||||
if (this->uart_dev_ == nullptr) {
|
||||
return;
|
||||
@@ -75,19 +75,25 @@ void Logger::pre_setup() {
|
||||
#ifdef CONFIG_SERIAL
|
||||
if (this->baud_rate_ > 0) {
|
||||
const struct device *uart_dev = nullptr;
|
||||
#if defined(USE_LOGGER_UART_SELECTION_USB_CDC)
|
||||
switch (this->uart_) {
|
||||
case UART_SELECTION_UART0: // NOLINT(bugprone-branch-clone)
|
||||
uart_dev = DEVICE_DT_GET_OR_NULL(DT_NODELABEL(ESPHOME_LOGGER_UART0_NODE));
|
||||
break;
|
||||
case UART_SELECTION_UART1:
|
||||
uart_dev = DEVICE_DT_GET_OR_NULL(DT_NODELABEL(uart1));
|
||||
break;
|
||||
#ifdef USE_LOGGER_USB_CDC
|
||||
case UART_SELECTION_USB_CDC:
|
||||
#if defined(CONFIG_USB_DEVICE_STACK) || defined(CONFIG_USB_DEVICE_STACK_NEXT)
|
||||
uart_dev = DEVICE_DT_GET_OR_NULL(DT_NODELABEL(cdc_acm_uart0));
|
||||
if (device_is_ready(uart_dev)) {
|
||||
// Nothing can log this early, and the logger has no output without USB anyway
|
||||
zephyr::usb_device_enable();
|
||||
uart_dev = DEVICE_DT_GET_OR_NULL(DT_NODELABEL(cdc_acm_uart0));
|
||||
if (device_is_ready(uart_dev)) {
|
||||
// Nothing can log this early, and the logger has no output without USB anyway
|
||||
zephyr::usb_device_enable();
|
||||
}
|
||||
#endif
|
||||
break;
|
||||
#endif
|
||||
}
|
||||
#endif
|
||||
#elif defined(USE_LOGGER_UART_SELECTION_UART1)
|
||||
uart_dev = DEVICE_DT_GET_OR_NULL(DT_NODELABEL(uart1));
|
||||
#else
|
||||
uart_dev = DEVICE_DT_GET_OR_NULL(DT_NODELABEL(ESPHOME_LOGGER_UART0_NODE));
|
||||
#endif
|
||||
if (device_is_ready(uart_dev)) {
|
||||
this->uart_dev_ = uart_dev;
|
||||
#if defined(USE_LOGGER_WAIT_FOR_CDC) && defined(USE_LOGGER_UART_SELECTION_USB_CDC)
|
||||
@@ -111,7 +117,7 @@ void Logger::pre_setup() {
|
||||
this->dump_crash_();
|
||||
#endif
|
||||
// Nobody reads a USB port this early; cdc_loop_() reports the dump once the port is opened
|
||||
#if defined(USE_CRASH_HANDLER) && !defined(USE_LOGGER_ZEPHYR_CDC_LOOP)
|
||||
#if defined(USE_CRASH_HANDLER) && !defined(USE_LOGGER_UART_SELECTION_USB_CDC)
|
||||
// Without a UART (baud_rate: 0, or one that did not come up) nothing reaches the console
|
||||
if (this->uart_dev_ != nullptr)
|
||||
log_crash_to_client();
|
||||
@@ -133,6 +139,21 @@ void HOT Logger::write_msg_(const char *msg, uint16_t len) {
|
||||
}
|
||||
}
|
||||
|
||||
const LogString *Logger::get_uart_selection_() {
|
||||
switch (this->uart_) {
|
||||
case UART_SELECTION_UART0:
|
||||
return LOG_STR("UART0");
|
||||
case UART_SELECTION_UART1:
|
||||
return LOG_STR("UART1");
|
||||
#ifdef USE_LOGGER_USB_CDC
|
||||
case UART_SELECTION_USB_CDC:
|
||||
return LOG_STR("USB_CDC");
|
||||
#endif
|
||||
default:
|
||||
return LOG_STR("UNKNOWN");
|
||||
}
|
||||
}
|
||||
|
||||
static constexpr uint8_t REASON_BUF_SIZE = 32;
|
||||
|
||||
static const char *reason_to_str(unsigned int reason, char *buf) {
|
||||
|
||||
@@ -1,4 +1,5 @@
|
||||
from collections.abc import Callable
|
||||
import logging
|
||||
|
||||
from esphome import automation
|
||||
import esphome.codegen as cg
|
||||
@@ -8,6 +9,8 @@ import esphome.config_validation as cv
|
||||
from esphome.const import CONF_DURATION, CONF_ID, CONF_MODE, CONF_VOLTAGE
|
||||
from esphome.types import ConfigType
|
||||
|
||||
_LOGGER = logging.getLogger(__name__)
|
||||
|
||||
CODEOWNERS = ["@tomaszduda23"]
|
||||
DEPENDENCIES = ["i2c"]
|
||||
DOMAIN = "npm1300"
|
||||
@@ -42,20 +45,18 @@ NPM1300_CHILD_SCHEMA = cv.Schema(
|
||||
{cv.GenerateID(CONF_NPM1300_ID): cv.use_id(NPM1300Component)}
|
||||
)
|
||||
|
||||
# Status registers read on each update: VBUSINSTATUS, BCHGCHARGESTATUS and BCHGERRREASON
|
||||
NPM1300Status = npm1300_ns.struct("NPM1300Status")
|
||||
# The status registers read on each update
|
||||
STATUS_PUBLISHER = cg.HubFunction(
|
||||
"status",
|
||||
cg.void,
|
||||
[
|
||||
(cg.uint8, "vbus_status"),
|
||||
(cg.uint8, "charge_status"),
|
||||
(cg.uint8, "error_reason"),
|
||||
],
|
||||
[(NPM1300Status.operator("const").operator("ref"), "status")],
|
||||
define="USE_NPM1300_STATUS_PUBLISHER",
|
||||
)
|
||||
VBUS_STATUS = STATUS_PUBLISHER.param("vbus_status")
|
||||
CHARGE_STATUS = STATUS_PUBLISHER.param("charge_status")
|
||||
ERROR_REASON = STATUS_PUBLISHER.param("error_reason")
|
||||
STATUS = STATUS_PUBLISHER.param("status")
|
||||
VBUS_STATUS = STATUS.vbus
|
||||
CHARGE_STATUS = STATUS.charge
|
||||
ERROR_REASON = STATUS.error_reason
|
||||
# The ADC results and the amperes per code of the battery current
|
||||
DATA_PUBLISHER = cg.HubFunction(
|
||||
"data",
|
||||
@@ -77,7 +78,10 @@ OUTPUTS = NPM1300Output.options(
|
||||
# Written for a field the YAML leaves out
|
||||
NPM1300_UNSET = 0xFF
|
||||
# ADCNTCRSEL codes, by kiloohms
|
||||
THERMISTOR_NONE = "none"
|
||||
THERMISTOR_RESISTANCES = {10: 1, 47: 2, 100: 3}
|
||||
# B25/50 of the thermistors the product specification lists for each resistance, by kiloohms
|
||||
THERMISTOR_BETAS = {10: 3380, 47: 4050, 100: 4250}
|
||||
LDO_MODES = {"load_switch": 0, "ldo": 1}
|
||||
TERMINATION_CURRENTS = {"10%": 0, "20%": 1}
|
||||
# BCHGVTRICKLESEL, by millivolts
|
||||
@@ -101,14 +105,13 @@ def _one_of_units(
|
||||
validator: Callable[[object], float],
|
||||
allowed: list[int],
|
||||
unit: str,
|
||||
scale: float = 1000,
|
||||
) -> Callable[[object], float]:
|
||||
"""Validate a quantity with `validator` and accept only the listed values, given in units * `scale`."""
|
||||
"""Validate a quantity with `validator` and accept only the listed values, given in thousandths of `unit`."""
|
||||
|
||||
def validate(value: object) -> float:
|
||||
value = validator(value)
|
||||
if round(value * scale) not in allowed:
|
||||
options = ", ".join(f"{v / scale:g}{unit}" for v in allowed)
|
||||
if _millis(value) not in allowed:
|
||||
options = ", ".join(f"{v / 1000:g}{unit}" for v in allowed)
|
||||
raise cv.Invalid(f"Must be one of {options}")
|
||||
return value
|
||||
|
||||
@@ -135,6 +138,23 @@ def _stepped(
|
||||
return validate
|
||||
|
||||
|
||||
def _thermistor_resistance(value: object) -> float | str:
|
||||
if isinstance(value, str) and value.lower() == THERMISTOR_NONE:
|
||||
return THERMISTOR_NONE
|
||||
value = cv.resistance(value)
|
||||
if round(value) not in (kilohms * 1000 for kilohms in THERMISTOR_RESISTANCES):
|
||||
options = ", ".join(f"{kilohms}kOhm" for kilohms in THERMISTOR_RESISTANCES)
|
||||
raise cv.Invalid(f"Must be one of {options} or {THERMISTOR_NONE}")
|
||||
return value
|
||||
|
||||
|
||||
def thermistor_beta(config: ConfigType) -> int:
|
||||
"""The B constant of the hub's thermistor: the configured one, or the one listed for its resistance."""
|
||||
if (beta := config.get(CONF_THERMISTOR_BETA)) is not None:
|
||||
return beta
|
||||
return THERMISTOR_BETAS[round(config[CONF_THERMISTOR_RESISTANCE] / 1000)]
|
||||
|
||||
|
||||
OUTPUT_VOLTAGE = _stepped(cv.voltage, 1000, 3300, 100, "V")
|
||||
VBUS_CURRENT_LIMIT = _stepped(cv.current, 100, 1500, 100, "A")
|
||||
|
||||
@@ -150,15 +170,58 @@ LDO_SCHEMA = BUCK_SCHEMA.extend(
|
||||
}
|
||||
)
|
||||
|
||||
CHARGER_SCHEMA = cv.Schema(
|
||||
WARM_TERMINATION_VOLTAGE_DEFAULT = 3.6
|
||||
|
||||
|
||||
def _warm_termination_default(config: ConfigType) -> ConfigType:
|
||||
"""Keep the default warm termination voltage at or below the termination voltage.
|
||||
|
||||
Warn when the default stays below a higher termination voltage.
|
||||
"""
|
||||
if (
|
||||
not isinstance(config, dict)
|
||||
or CONF_WARM_TERMINATION_VOLTAGE in config
|
||||
or CONF_TERMINATION_VOLTAGE not in config
|
||||
):
|
||||
return config
|
||||
try:
|
||||
termination = cv.voltage(config[CONF_TERMINATION_VOLTAGE])
|
||||
except cv.Invalid:
|
||||
# The schema reports it
|
||||
return config
|
||||
if termination < WARM_TERMINATION_VOLTAGE_DEFAULT - 0.001:
|
||||
# A warm battery is never charged higher than a cool one
|
||||
return {
|
||||
**config,
|
||||
CONF_WARM_TERMINATION_VOLTAGE: config[CONF_TERMINATION_VOLTAGE],
|
||||
}
|
||||
if termination > WARM_TERMINATION_VOLTAGE_DEFAULT + 0.001:
|
||||
_LOGGER.warning(
|
||||
"npm1300 '%s' is not set, so a warm battery (45 °C to 60 °C by default) stops "
|
||||
"charging at %.2fV instead of the %.2fV of '%s'. Set '%s' to the value your "
|
||||
"battery allows when warm",
|
||||
CONF_WARM_TERMINATION_VOLTAGE,
|
||||
WARM_TERMINATION_VOLTAGE_DEFAULT,
|
||||
termination,
|
||||
CONF_TERMINATION_VOLTAGE,
|
||||
CONF_WARM_TERMINATION_VOLTAGE,
|
||||
)
|
||||
return config
|
||||
|
||||
|
||||
_CHARGER_SETTINGS_SCHEMA = cv.Schema(
|
||||
{
|
||||
cv.Required(CONF_CHARGE_CURRENT): _stepped(cv.current, 32, 800, 2, "A"),
|
||||
cv.Optional(CONF_TERMINATION_VOLTAGE, default="4.2V"): _one_of_units(
|
||||
cv.voltage, TERMINATION_VOLTAGES, "V"
|
||||
# Defaults are the reset values of the product specification
|
||||
cv.Optional(CONF_CHARGE_CURRENT, default="32mA"): _stepped(
|
||||
cv.current, 32, 800, 2, "A"
|
||||
),
|
||||
cv.Optional(CONF_WARM_TERMINATION_VOLTAGE): _one_of_units(
|
||||
cv.Optional(CONF_TERMINATION_VOLTAGE, default="3.6V"): _one_of_units(
|
||||
cv.voltage, TERMINATION_VOLTAGES, "V"
|
||||
),
|
||||
cv.Optional(
|
||||
CONF_WARM_TERMINATION_VOLTAGE,
|
||||
default=f"{WARM_TERMINATION_VOLTAGE_DEFAULT}V",
|
||||
): _one_of_units(cv.voltage, TERMINATION_VOLTAGES, "V"),
|
||||
cv.Optional(CONF_TERMINATION_CURRENT, default="10%"): cv.enum(
|
||||
TERMINATION_CURRENTS
|
||||
),
|
||||
@@ -169,22 +232,37 @@ CHARGER_SCHEMA = cv.Schema(
|
||||
cv.current, list(DISCHARGE_CURRENT_LIMITS), "A"
|
||||
),
|
||||
cv.Optional(CONF_RECHARGE, default=True): cv.boolean,
|
||||
cv.Optional(CONF_ENABLED, default=True): cv.boolean,
|
||||
cv.Optional(CONF_ENABLED, default=False): cv.boolean,
|
||||
}
|
||||
)
|
||||
CHARGER_SCHEMA = cv.All(_warm_termination_default, _CHARGER_SETTINGS_SCHEMA)
|
||||
|
||||
|
||||
CONFIG_SCHEMA = (
|
||||
def _check_thermistor(config: ConfigType) -> ConfigType:
|
||||
"""A wrong thermistor shifts the battery temperature the charger sees, so charging needs one chosen."""
|
||||
resistance = config.get(CONF_THERMISTOR_RESISTANCE)
|
||||
if CONF_CHARGER in config and resistance is None:
|
||||
raise cv.Invalid(
|
||||
f"'{CONF_THERMISTOR_RESISTANCE}' is required with '{CONF_CHARGER}': set the nominal "
|
||||
f"resistance of the battery thermistor, or '{THERMISTOR_NONE}'",
|
||||
path=[CONF_THERMISTOR_RESISTANCE],
|
||||
)
|
||||
if CONF_THERMISTOR_BETA in config and resistance in (None, THERMISTOR_NONE):
|
||||
raise cv.Invalid(
|
||||
f"'{CONF_THERMISTOR_BETA}' needs '{CONF_THERMISTOR_RESISTANCE}' set to the "
|
||||
"resistance of the thermistor",
|
||||
path=[CONF_THERMISTOR_BETA],
|
||||
)
|
||||
return config
|
||||
|
||||
|
||||
CONFIG_SCHEMA = cv.All(
|
||||
cv.Schema(
|
||||
{
|
||||
cv.GenerateID(): cv.declare_id(NPM1300Component),
|
||||
cv.Optional(CONF_VBUS_CURRENT_LIMIT): VBUS_CURRENT_LIMIT,
|
||||
cv.Optional(CONF_THERMISTOR_RESISTANCE): _one_of_units(
|
||||
cv.resistance, list(THERMISTOR_RESISTANCES), "kOhm", scale=0.001
|
||||
),
|
||||
cv.Optional(CONF_THERMISTOR_BETA, default=3380): cv.int_range(
|
||||
min=1, max=65535
|
||||
),
|
||||
cv.Optional(CONF_THERMISTOR_RESISTANCE): _thermistor_resistance,
|
||||
cv.Optional(CONF_THERMISTOR_BETA): cv.int_range(min=1, max=65535),
|
||||
cv.Optional(CONF_CHARGER): CHARGER_SCHEMA,
|
||||
cv.Optional(CONF_BUCK1): BUCK_SCHEMA,
|
||||
cv.Optional(CONF_BUCK2): BUCK_SCHEMA,
|
||||
@@ -193,7 +271,8 @@ CONFIG_SCHEMA = (
|
||||
}
|
||||
)
|
||||
.extend(cv.polling_component_schema("60s"))
|
||||
.extend(i2c.i2c_device_schema(0x6B))
|
||||
.extend(i2c.i2c_device_schema(0x6B)),
|
||||
_check_thermistor,
|
||||
)
|
||||
|
||||
|
||||
@@ -208,7 +287,7 @@ def _vbus_current_limit_code(amps: float) -> int:
|
||||
|
||||
def _charger_config(config: ConfigType) -> cg.StructInitializer:
|
||||
termination = _termination_voltage_code(config[CONF_TERMINATION_VOLTAGE])
|
||||
warm = config.get(CONF_WARM_TERMINATION_VOLTAGE)
|
||||
warm = _termination_voltage_code(config[CONF_WARM_TERMINATION_VOLTAGE])
|
||||
return cg.StructInitializer(
|
||||
NPM1300ChargerConfig,
|
||||
("current", _millis(config[CONF_CHARGE_CURRENT]) // 2),
|
||||
@@ -217,10 +296,7 @@ def _charger_config(config: ConfigType) -> cg.StructInitializer:
|
||||
DISCHARGE_CURRENT_LIMITS[_millis(config[CONF_DISCHARGE_CURRENT_LIMIT])],
|
||||
),
|
||||
("termination_voltage", termination),
|
||||
(
|
||||
"warm_termination_voltage",
|
||||
termination if warm is None else _termination_voltage_code(warm),
|
||||
),
|
||||
("warm_termination_voltage", warm),
|
||||
("trickle_voltage", TRICKLE_VOLTAGES[_millis(config[CONF_TRICKLE_VOLTAGE])]),
|
||||
("termination_current", TERMINATION_CURRENTS[config[CONF_TERMINATION_CURRENT]]),
|
||||
("recharge", config[CONF_RECHARGE]),
|
||||
@@ -247,6 +323,13 @@ def _output_config(config: ConfigType | None) -> cg.StructInitializer:
|
||||
def _config(config: ConfigType) -> cg.StructInitializer:
|
||||
charger = config.get(CONF_CHARGER)
|
||||
resistance = config.get(CONF_THERMISTOR_RESISTANCE)
|
||||
if resistance is None:
|
||||
thermistor, beta = NPM1300_UNSET, 0
|
||||
elif resistance == THERMISTOR_NONE:
|
||||
thermistor, beta = 0, 0
|
||||
else:
|
||||
thermistor = THERMISTOR_RESISTANCES[round(resistance / 1000)]
|
||||
beta = thermistor_beta(config)
|
||||
return cg.StructInitializer(
|
||||
NPM1300Config,
|
||||
(
|
||||
@@ -254,13 +337,8 @@ def _config(config: ConfigType) -> cg.StructInitializer:
|
||||
cg.ArrayInitializer(*(_output_config(config.get(key)) for key in OUTPUTS)),
|
||||
),
|
||||
("charger", None if charger is None else _charger_config(charger)),
|
||||
("thermistor_beta", config[CONF_THERMISTOR_BETA]),
|
||||
(
|
||||
"thermistor",
|
||||
NPM1300_UNSET
|
||||
if resistance is None
|
||||
else THERMISTOR_RESISTANCES[round(resistance / 1000)],
|
||||
),
|
||||
("thermistor_beta", beta),
|
||||
("thermistor", thermistor),
|
||||
("charger_configured", charger is not None),
|
||||
)
|
||||
|
||||
|
||||
@@ -29,6 +29,7 @@ static constexpr uint16_t REG_BCHG_VTERM = 0x030C;
|
||||
static constexpr uint16_t REG_BCHG_VTERM_R = 0x030D;
|
||||
static constexpr uint16_t REG_BCHG_VTRICKLE_SEL = 0x030E;
|
||||
static constexpr uint16_t REG_BCHG_ITERM_SEL = 0x030F;
|
||||
static constexpr uint16_t REG_BCHG_NTC_STATUS = 0x0332;
|
||||
static constexpr uint16_t REG_BCHG_CHARGE_STATUS = 0x0334;
|
||||
static constexpr uint16_t REG_BCHG_ERR_REASON = 0x0336;
|
||||
static constexpr uint16_t REG_BUCK1_ENA_SET = 0x0400;
|
||||
@@ -53,6 +54,9 @@ static constexpr uint16_t REG_SHIP_TASK_SHIP_MODE = 0x0B02;
|
||||
|
||||
// VBUSINSTATUS
|
||||
static constexpr uint8_t VBUS_PRESENT = 0x01;
|
||||
// NTCSTATUS
|
||||
static constexpr uint8_t NTC_STATUS_COLD = 0x01;
|
||||
static constexpr uint8_t NTC_STATUS_HOT = 0x08;
|
||||
// BCHGCHARGESTATUS; bit 0 (battery detected) is reserved since C00 silicon
|
||||
static constexpr uint8_t CHARGE_STATUS_COMPLETED = 0x02;
|
||||
static constexpr uint8_t CHARGE_STATUS_TRICKLE = 0x04;
|
||||
@@ -107,26 +111,36 @@ float ntc_temperature(uint16_t code, float inv_beta) {
|
||||
#endif
|
||||
|
||||
#ifdef USE_TEXT_SENSOR
|
||||
PROGMEM_STRING_TABLE(ChargerStatusStrings, "Error", "Paused, Die Too Hot", "Charge Complete", "Trickle Charge",
|
||||
"Constant Current", "Constant Voltage", "Discharging", "Not Charging");
|
||||
// BCHGCHARGESTATUS bits in order of priority; bit i is reported as string i + 1
|
||||
PROGMEM_STRING_TABLE(ChargerStatusStrings, "Discharging", "Error", "Disabled", "Paused, PMIC Too Hot",
|
||||
"Charge Complete", "Trickle Charge", "Constant Current", "Constant Voltage",
|
||||
"Paused, Battery Too Cold", "Paused, Battery Too Hot", "Not Charging");
|
||||
static constexpr uint8_t CHARGER_STATUS_FIRST_BIT = 3;
|
||||
// BCHGCHARGESTATUS bits in order of priority, reported from string CHARGER_STATUS_FIRST_BIT on
|
||||
static const uint8_t CHARGER_STATUS_BITS[] PROGMEM = {CHARGE_STATUS_DIE_TEMP_HIGH, CHARGE_STATUS_COMPLETED,
|
||||
CHARGE_STATUS_TRICKLE, CHARGE_STATUS_CONSTANT_CURRENT,
|
||||
CHARGE_STATUS_CONSTANT_VOLTAGE};
|
||||
|
||||
static uint8_t charger_status_index(uint8_t vbus_status, uint8_t charge_status, uint8_t error_reason) {
|
||||
if (error_reason != 0)
|
||||
static uint8_t charger_status_index(const NPM1300Status &status) {
|
||||
if ((status.vbus & VBUS_PRESENT) == 0)
|
||||
return 0;
|
||||
if (status.error_reason != 0)
|
||||
return 1;
|
||||
if ((status.charger_enable & 1) == 0)
|
||||
return 2;
|
||||
for (size_t i = 0; i < sizeof(CHARGER_STATUS_BITS); i++) {
|
||||
if ((charge_status & progmem_read_byte(&CHARGER_STATUS_BITS[i])) != 0)
|
||||
return i + 1;
|
||||
if ((status.charge & progmem_read_byte(&CHARGER_STATUS_BITS[i])) != 0)
|
||||
return CHARGER_STATUS_FIRST_BIT + i;
|
||||
}
|
||||
return (vbus_status & VBUS_PRESENT) == 0 ? 6 : 7;
|
||||
// The charger stops without an error outside the cold and hot limits of the battery temperature
|
||||
if ((status.ntc & NTC_STATUS_COLD) != 0)
|
||||
return 8;
|
||||
if ((status.ntc & NTC_STATUS_HOT) != 0)
|
||||
return 9;
|
||||
return 10;
|
||||
}
|
||||
|
||||
void publish_charger_status(text_sensor::TextSensor *sensor, uint8_t &last, uint8_t vbus_status, uint8_t charge_status,
|
||||
uint8_t error_reason) {
|
||||
const uint8_t index = charger_status_index(vbus_status, charge_status, error_reason);
|
||||
void publish_charger_status(text_sensor::TextSensor *sensor, uint8_t &last, const NPM1300Status &status) {
|
||||
const uint8_t index = charger_status_index(status);
|
||||
if (index == last)
|
||||
return;
|
||||
last = index;
|
||||
@@ -198,7 +212,7 @@ bool NPM1300Component::configure_charger_(const NPM1300Config &config) {
|
||||
split_9bit(charger.discharge_limit, discharge);
|
||||
uint8_t disable = charger.recharge ? 0 : CHARGER_DISABLE_RECHARGE;
|
||||
// Without a thermistor the charger reports an NTC error and does not charge
|
||||
if (config.thermistor == NPM1300_UNSET)
|
||||
if (config.thermistor == 0 || config.thermistor == NPM1300_UNSET)
|
||||
disable |= CHARGER_DISABLE_NTC;
|
||||
const uint8_t enable = (CHARGER_DISABLE_RECHARGE | CHARGER_DISABLE_NTC) & ~disable;
|
||||
if (this->write_register16(REG_BCHG_ISET_MSB, current, sizeof(current)) != i2c::ERROR_OK ||
|
||||
@@ -292,33 +306,40 @@ void NPM1300Component::hibernate(uint32_t duration_ms) {
|
||||
}
|
||||
|
||||
void NPM1300Component::update() {
|
||||
uint8_t vbus_status;
|
||||
uint8_t charge_status;
|
||||
uint8_t error_reason;
|
||||
if (!this->status_comm_check(this->read_byte_reg16(REG_VBUSIN_STATUS, &vbus_status) &&
|
||||
this->read_byte_reg16(REG_BCHG_CHARGE_STATUS, &charge_status) &&
|
||||
this->read_byte_reg16(REG_BCHG_ERR_REASON, &error_reason)))
|
||||
NPM1300Status status;
|
||||
// NTCSTATUS, DIETEMPSTATUS and BCHGCHARGESTATUS
|
||||
uint8_t charger_status[3];
|
||||
if (!this->status_comm_check(this->read_byte_reg16(REG_VBUSIN_STATUS, &status.vbus) &&
|
||||
this->read_byte_reg16(REG_BCHG_ENABLE_SET, &status.charger_enable) &&
|
||||
this->read_register16(REG_BCHG_NTC_STATUS, charger_status, sizeof(charger_status)) ==
|
||||
i2c::ERROR_OK &&
|
||||
this->read_byte_reg16(REG_BCHG_ERR_REASON, &status.error_reason)))
|
||||
return;
|
||||
status.ntc = charger_status[0];
|
||||
status.charge = charger_status[2];
|
||||
// Without a thermistor the NTC pin is grounded and can read as a hot battery
|
||||
if (progmem_read_byte(&this->config_->thermistor) == 0)
|
||||
status.ntc = 0;
|
||||
|
||||
// The chip goes back to its default VBUS current limit whenever VBUS is removed
|
||||
const bool vbus_present = (vbus_status & VBUS_PRESENT) != 0;
|
||||
const bool vbus_present = (status.vbus & VBUS_PRESENT) != 0;
|
||||
if (vbus_present && !this->vbus_present_ && this->vbus_current_limit_ != NPM1300_UNSET &&
|
||||
!this->status_comm_check(this->apply_vbus_current_limit_()))
|
||||
return;
|
||||
this->vbus_present_ = vbus_present;
|
||||
|
||||
if (error_reason != this->last_error_reason_) {
|
||||
this->last_error_reason_ = error_reason;
|
||||
if (error_reason != 0) {
|
||||
ESP_LOGW(TAG, "Charger error, BCHGERRREASON 0x%02X", error_reason);
|
||||
if (status.error_reason != this->last_error_reason_) {
|
||||
this->last_error_reason_ = status.error_reason;
|
||||
if (status.error_reason != 0) {
|
||||
ESP_LOGW(TAG, "Charger error, BCHGERRREASON 0x%02X", status.error_reason);
|
||||
}
|
||||
}
|
||||
#ifdef USE_NPM1300_STATUS_PUBLISHER
|
||||
if (this->status_publisher_ != nullptr)
|
||||
this->status_publisher_(vbus_status, charge_status, error_reason);
|
||||
this->status_publisher_(status);
|
||||
#endif
|
||||
#ifdef USE_SWITCH
|
||||
if (!this->status_comm_check(this->publish_switches_()))
|
||||
if (!this->status_comm_check(this->publish_switches_(status)))
|
||||
return;
|
||||
#endif
|
||||
|
||||
@@ -348,16 +369,10 @@ void NPM1300Component::update() {
|
||||
}
|
||||
|
||||
#ifdef USE_SWITCH
|
||||
bool NPM1300Component::publish_switches_() {
|
||||
bool NPM1300Component::publish_switches_(const NPM1300Status &status) {
|
||||
PUBLISH_STATE(this->charger_switch_, (status.charger_enable & 1) != 0);
|
||||
uint8_t value;
|
||||
bool ok = true;
|
||||
if (this->charger_switch_ != nullptr) {
|
||||
if (this->read_byte_reg16(REG_BCHG_ENABLE_SET, &value)) {
|
||||
this->charger_switch_->publish_state((value & 1) != 0);
|
||||
} else {
|
||||
ok = false;
|
||||
}
|
||||
}
|
||||
switch_::Switch *const bucks[2] = {this->buck1_switch_, this->buck2_switch_};
|
||||
if (bucks[0] != nullptr || bucks[1] != nullptr) {
|
||||
if (this->read_byte_reg16(REG_BUCK_STATUS, &value)) {
|
||||
@@ -461,7 +476,9 @@ void NPM1300Component::dump_config() {
|
||||
ESP_LOGCONFIG(TAG, " Enabled at boot: %s", YESNO(output.enabled != 0));
|
||||
}
|
||||
}
|
||||
if (config.thermistor != NPM1300_UNSET) {
|
||||
if (config.thermistor == 0) {
|
||||
ESP_LOGCONFIG(TAG, " Thermistor: none");
|
||||
} else if (config.thermistor != NPM1300_UNSET) {
|
||||
ESP_LOGCONFIG(TAG, " Thermistor B constant: %u", config.thermistor_beta);
|
||||
}
|
||||
#ifdef USE_SENSOR
|
||||
|
||||
@@ -54,10 +54,19 @@ struct NPM1300Config {
|
||||
NPM1300OutputConfig outputs[NPM1300_OUTPUT_COUNT];
|
||||
NPM1300ChargerConfig charger; // only applied when charger_configured
|
||||
uint16_t thermistor_beta;
|
||||
uint8_t thermistor; // ADCNTCRSEL code: 1 for 10 kOhm, 2 for 47 kOhm, 3 for 100 kOhm, or NPM1300_UNSET
|
||||
uint8_t thermistor; // ADCNTCRSEL code: 0 for none, 1 for 10 kOhm, 2 for 47 kOhm, 3 for 100 kOhm, or NPM1300_UNSET
|
||||
bool charger_configured;
|
||||
};
|
||||
|
||||
/// The status registers read on every update
|
||||
struct NPM1300Status {
|
||||
uint8_t vbus; // VBUSINSTATUS
|
||||
uint8_t charger_enable; // BCHGENABLESET
|
||||
uint8_t ntc; // NTCSTATUS
|
||||
uint8_t charge; // BCHGCHARGESTATUS
|
||||
uint8_t error_reason; // BCHGERRREASON
|
||||
};
|
||||
|
||||
#ifdef USE_NPM1300_DATA_PUBLISHER
|
||||
/// The 10-bit ADC result whose high 8 bits are adc[msb] and low 2 bits are at `shift` in adc[lsbs]
|
||||
inline uint16_t adc_code(const uint8_t *adc, uint8_t msb, uint8_t lsbs, uint8_t shift) {
|
||||
@@ -68,8 +77,7 @@ float ntc_temperature(uint16_t code, float inv_beta);
|
||||
#endif
|
||||
#ifdef USE_TEXT_SENSOR
|
||||
/// Publish the charger status text, only when it differs from `last`
|
||||
void publish_charger_status(text_sensor::TextSensor *sensor, uint8_t &last, uint8_t vbus_status, uint8_t charge_status,
|
||||
uint8_t error_reason);
|
||||
void publish_charger_status(text_sensor::TextSensor *sensor, uint8_t &last, const NPM1300Status &status);
|
||||
#endif
|
||||
|
||||
class NPM1300Component : public PollingComponent, public i2c::I2CDevice {
|
||||
@@ -95,7 +103,7 @@ class NPM1300Component : public PollingComponent, public i2c::I2CDevice {
|
||||
float get_setup_priority() const override { return setup_priority::BUS - 1.0f; }
|
||||
|
||||
#ifdef USE_NPM1300_STATUS_PUBLISHER
|
||||
HUB_PUBLISHER(status, uint8_t, uint8_t, uint8_t)
|
||||
HUB_PUBLISHER(status, const NPM1300Status &)
|
||||
#endif
|
||||
#ifdef USE_NPM1300_DATA_PUBLISHER
|
||||
HUB_PUBLISHER(data, const uint8_t *, float)
|
||||
@@ -128,7 +136,7 @@ class NPM1300Component : public PollingComponent, public i2c::I2CDevice {
|
||||
#endif
|
||||
#ifdef USE_SWITCH
|
||||
/// Returns false when a status read failed
|
||||
bool publish_switches_();
|
||||
bool publish_switches_(const NPM1300Status &status);
|
||||
#endif
|
||||
|
||||
const NPM1300Config *config_;
|
||||
|
||||
@@ -21,7 +21,6 @@ from esphome.types import ConfigType
|
||||
from . import (
|
||||
ADC,
|
||||
CONF_NPM1300_ID,
|
||||
CONF_THERMISTOR_BETA,
|
||||
CONF_THERMISTOR_RESISTANCE,
|
||||
CONF_VBUS_CURRENT_LIMIT,
|
||||
DATA_PUBLISHER,
|
||||
@@ -30,7 +29,9 @@ from . import (
|
||||
DUMP_TAG,
|
||||
IBAT_SCALE,
|
||||
NPM1300_CHILD_SCHEMA,
|
||||
THERMISTOR_NONE,
|
||||
npm1300_ns,
|
||||
thermistor_beta,
|
||||
)
|
||||
|
||||
DEPENDENCIES = ["npm1300"]
|
||||
@@ -109,10 +110,10 @@ def _final_validate(config: ConfigType) -> None:
|
||||
if CONF_BATTERY_TEMPERATURE not in config:
|
||||
return
|
||||
hub = fv.get_declaring_config(config[CONF_NPM1300_ID])
|
||||
if CONF_THERMISTOR_RESISTANCE not in hub:
|
||||
if hub.get(CONF_THERMISTOR_RESISTANCE, THERMISTOR_NONE) == THERMISTOR_NONE:
|
||||
raise cv.Invalid(
|
||||
f"'{CONF_BATTERY_TEMPERATURE}' needs '{CONF_THERMISTOR_RESISTANCE}' "
|
||||
"set on the npm1300 component",
|
||||
f"'{CONF_BATTERY_TEMPERATURE}' needs '{CONF_THERMISTOR_RESISTANCE}' set to the "
|
||||
"nominal resistance of the battery thermistor on the npm1300 component",
|
||||
path=[CONF_BATTERY_TEMPERATURE],
|
||||
)
|
||||
|
||||
@@ -145,7 +146,7 @@ async def to_code(config: ConfigType) -> None:
|
||||
if (var := await sensors(CONF_BATTERY_TEMPERATURE, None)) is not None:
|
||||
hub_config = next(c for c in CORE.config[DOMAIN] if c[CONF_ID] == hub_id)
|
||||
state = npm1300_ns.ntc_temperature(
|
||||
_adc(2, 5, 2), 1.0 / hub_config[CONF_THERMISTOR_BETA]
|
||||
_adc(2, 5, 2), 1.0 / thermistor_beta(hub_config)
|
||||
)
|
||||
_add_adc_sensor(hub, var, state, "Battery Temperature")
|
||||
await sensors(CONF_VBUS_CURRENT_LIMIT, hub.set_vbus_current_limit_sensor)
|
||||
@@ -15,6 +15,7 @@ from . import (
|
||||
CONF_THERMISTOR_RESISTANCE,
|
||||
NPM1300_CHILD_SCHEMA,
|
||||
OUTPUTS,
|
||||
THERMISTOR_NONE,
|
||||
)
|
||||
|
||||
DEPENDENCIES = ["npm1300"]
|
||||
@@ -44,11 +45,18 @@ def _final_validate(config: ConfigType) -> None:
|
||||
if CONF_CHARGER not in config:
|
||||
return
|
||||
hub = fv.get_declaring_config(config[CONF_NPM1300_ID])
|
||||
# Only the charger block turns off the NTC check, without which the charger never starts
|
||||
if CONF_CHARGER not in hub and CONF_THERMISTOR_RESISTANCE not in hub:
|
||||
# A wrong thermistor shifts the battery temperature the charger sees
|
||||
if CONF_THERMISTOR_RESISTANCE not in hub:
|
||||
raise cv.Invalid(
|
||||
f"The '{CONF_CHARGER}' switch needs a '{CONF_CHARGER}' block or "
|
||||
f"'{CONF_THERMISTOR_RESISTANCE}' on the npm1300 component",
|
||||
f"The '{CONF_CHARGER}' switch needs '{CONF_THERMISTOR_RESISTANCE}' on the npm1300 "
|
||||
f"component: the nominal resistance of the battery thermistor, or '{THERMISTOR_NONE}'",
|
||||
path=[CONF_CHARGER],
|
||||
)
|
||||
# Only the charger block turns off the NTC check, without which the charger never starts
|
||||
if CONF_CHARGER not in hub and hub[CONF_THERMISTOR_RESISTANCE] == THERMISTOR_NONE:
|
||||
raise cv.Invalid(
|
||||
f"The '{CONF_CHARGER}' switch needs a '{CONF_CHARGER}' block when "
|
||||
f"'{CONF_THERMISTOR_RESISTANCE}' is '{THERMISTOR_NONE}'",
|
||||
path=[CONF_CHARGER],
|
||||
)
|
||||
|
||||
|
||||
@@ -11,14 +11,12 @@ from esphome.cpp_generator import (
|
||||
from esphome.types import ConfigType
|
||||
|
||||
from . import (
|
||||
CHARGE_STATUS,
|
||||
CONF_NPM1300_ID,
|
||||
DUMP,
|
||||
DUMP_TAG,
|
||||
ERROR_REASON,
|
||||
NPM1300_CHILD_SCHEMA,
|
||||
STATUS,
|
||||
STATUS_PUBLISHER,
|
||||
VBUS_STATUS,
|
||||
npm1300_ns,
|
||||
)
|
||||
|
||||
@@ -48,9 +46,7 @@ async def to_code(config: ConfigType) -> None:
|
||||
)
|
||||
STATUS_PUBLISHER.add(
|
||||
hub,
|
||||
npm1300_ns.publish_charger_status(
|
||||
parent_ref(var), last, VBUS_STATUS, CHARGE_STATUS, ERROR_REASON
|
||||
),
|
||||
npm1300_ns.publish_charger_status(parent_ref(var), last, STATUS),
|
||||
)
|
||||
DUMP.add(
|
||||
hub,
|
||||
|
||||
@@ -526,8 +526,9 @@ async def to_code(config: ConfigType) -> None:
|
||||
cg.add(paren.set_port(port))
|
||||
cg.add_define("USE_WEBSERVER")
|
||||
cg.add_define("USE_WEBSERVER_PORT", port)
|
||||
if CORE.is_esp32:
|
||||
# The ESP-IDF event source builds state documents in a stack arena
|
||||
if CORE.is_esp32 or CORE.is_libretiny:
|
||||
# The event source builds state documents in a 2 KB stack arena; LibreTiny's main task has 8 KB,
|
||||
# ESP8266 and RP2040 only 4 KB
|
||||
enable_arena()
|
||||
cg.add_define("USE_WEBSERVER_VERSION", version)
|
||||
if version >= 2:
|
||||
@@ -614,9 +615,21 @@ 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, 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
|
||||
|
||||
# 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,528 @@
|
||||
#include "event_source.h"
|
||||
#if defined(USE_WEBSERVER) && (defined(USE_ESP32) || defined(USE_ARDUINO))
|
||||
|
||||
#include <algorithm>
|
||||
#include <cstring>
|
||||
#include <iterator>
|
||||
|
||||
#include "esphome/core/application.h"
|
||||
#include "esphome/core/hal.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");
|
||||
|
||||
// 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() {
|
||||
web_server_base::StreamIntakeGuard guard{this->pending_mutex_};
|
||||
for (auto *ses : {this->sessions_, this->pending_sessions_.load(std::memory_order_relaxed)}) {
|
||||
while (ses != nullptr) {
|
||||
auto *next = ses->next_;
|
||||
delete ses; // NOLINT(cppcoreguidelines-owning-memory)
|
||||
ses = next;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void AsyncEventSource::handle_request(AsyncWebServerRequest *request, WebServer *ws) {
|
||||
// Network context: start the stream and park the session; the main loop does the rest.
|
||||
#ifdef USE_ESP32
|
||||
// NOLINTNEXTLINE(cppcoreguidelines-owning-memory,clang-analyzer-cplusplus.NewDeleteLeaks)
|
||||
auto *rsp = new AsyncEventSourceResponse(ws);
|
||||
rsp->begin(request, SSE_STATUS, SSE_CONTENT_TYPE, SSE_HEADERS, SSE_FIRST_CHUNK);
|
||||
this->push_pending_(rsp);
|
||||
#else
|
||||
// The session is created only once the head is queued and the connection is ours
|
||||
web_server_base::AsyncStreamResponse::accept(
|
||||
request, SSE_STATUS, SSE_CONTENT_TYPE, SSE_HEADERS, SSE_FIRST_CHUNK,
|
||||
{&AsyncEventSource::create_session, &AsyncEventSource::publish_session, this, ws});
|
||||
#endif
|
||||
}
|
||||
|
||||
void AsyncEventSource::push_pending_(AsyncEventSourceResponse *rsp) {
|
||||
{
|
||||
web_server_base::StreamIntakeGuard guard{this->pending_mutex_};
|
||||
rsp->next_ = this->pending_sessions_.load(std::memory_order_relaxed);
|
||||
this->pending_sessions_.store(rsp, std::memory_order_release);
|
||||
}
|
||||
rsp->web_server_->enable_loop_soon_any_context();
|
||||
}
|
||||
|
||||
#ifndef USE_ESP32
|
||||
web_server_base::AsyncStreamResponse *AsyncEventSource::create_session(void *ws) {
|
||||
// NOLINTNEXTLINE(cppcoreguidelines-owning-memory,clang-analyzer-cplusplus.NewDeleteLeaks)
|
||||
return new AsyncEventSourceResponse(static_cast<WebServer *>(ws));
|
||||
}
|
||||
|
||||
void AsyncEventSource::publish_session(void *source, web_server_base::AsyncStreamResponse *stream) {
|
||||
static_cast<AsyncEventSource *>(source)->push_pending_(static_cast<AsyncEventSourceResponse *>(stream));
|
||||
}
|
||||
#endif
|
||||
|
||||
// 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->pending_sessions_.load(std::memory_order_acquire) != nullptr) {
|
||||
this->adopt_pending_sessions_main_loop_();
|
||||
}
|
||||
|
||||
// The network side only marks a session dead; the main loop frees it, so nothing races the free
|
||||
for (AsyncEventSourceResponse **link = &this->sessions_; *link != nullptr;) {
|
||||
auto *ses = *link;
|
||||
if (ses->safe_to_delete()) {
|
||||
*link = ses->next_;
|
||||
delete ses; // NOLINT(cppcoreguidelines-owning-memory)
|
||||
} else {
|
||||
ses->loop();
|
||||
link = &ses->next_;
|
||||
}
|
||||
}
|
||||
// A session still waiting for its connection keeps the loop alive too
|
||||
return this->sessions_ != nullptr || this->pending_sessions_.load(std::memory_order_acquire) != nullptr;
|
||||
}
|
||||
|
||||
void AsyncEventSource::adopt_pending_sessions_main_loop_() {
|
||||
AsyncEventSourceResponse *incoming;
|
||||
{
|
||||
web_server_base::StreamIntakeGuard guard{this->pending_mutex_};
|
||||
// Plain load and store under the guard: RP2040 has no atomic read-modify-write
|
||||
incoming = this->pending_sessions_.load(std::memory_order_acquire);
|
||||
this->pending_sessions_.store(nullptr, std::memory_order_relaxed);
|
||||
}
|
||||
while (incoming != nullptr) {
|
||||
auto *rsp = incoming;
|
||||
incoming = rsp->next_;
|
||||
// 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 a stream after the creating handler returns (or destroys it), so it stays pending until then;
|
||||
// on ESPAsyncWebServer a session is published already attached
|
||||
if (!rsp->is_attached()) {
|
||||
web_server_base::StreamIntakeGuard guard{this->pending_mutex_};
|
||||
rsp->next_ = this->pending_sessions_.load(std::memory_order_relaxed);
|
||||
this->pending_sessions_.store(rsp, std::memory_order_release);
|
||||
continue;
|
||||
}
|
||||
rsp->next_ = this->sessions_;
|
||||
this->sessions_ = 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_; ses != nullptr; ses = ses->next_) {
|
||||
if (ses->is_connected()) {
|
||||
ses->try_send_nodefer(message, message_len, event, id, reconnect);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void AsyncEventSource::deferrable_send_state(void *source, message_generator_t *message_generator) {
|
||||
for (auto *ses = this->sessions_; ses != nullptr; ses = ses->next_) {
|
||||
if (ses->is_connected()) {
|
||||
ses->deferrable_send_state(source, false, message_generator);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Network context only. start_session_main_loop_() sends the greeting and starts the iterator.
|
||||
AsyncEventSourceResponse::AsyncEventSourceResponse(WebServer *ws) : web_server_(ws), entities_iterator_(ws, this) {}
|
||||
static_assert(web_server_base::stream_head_iov_count(SSE_HEADERS_COUNT + 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)) {
|
||||
// A stream already closing (its head did not go out whole) is not a failed greeting
|
||||
if (!this->is_closing()) {
|
||||
ESP_LOGW(TAG, "Config not sent to stream %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();
|
||||
|
||||
// Groups the socket does not take yet queue in the tail behind the ping. A refusal means the
|
||||
// tail is full or the stream is 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")) {
|
||||
// A stream closing mid-greeting is not a failed send
|
||||
if (!this->is_closing()) {
|
||||
ESP_LOGW(TAG, "Sorting groups not sent to stream %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;
|
||||
}
|
||||
copy_chunk(this->tail_.get(), prefix, prefix_len, message, message_len);
|
||||
this->tail_len_ = total;
|
||||
this->tail_sent_ = sent;
|
||||
return true;
|
||||
}
|
||||
|
||||
bool AsyncEventSourceResponse::append_chunk_(const char *prefix, size_t prefix_len, const char *message,
|
||||
size_t message_len, size_t total) {
|
||||
const size_t cap = tail_queue_cap(this->tail_len_ - this->tail_sent_ + total, this->tail_cap_, TAIL_QUEUE_SIZE);
|
||||
if (cap == 0) {
|
||||
return false;
|
||||
}
|
||||
uint8_t *src = this->tail_.get();
|
||||
RAMUniquePtr<uint8_t[]> grown;
|
||||
if (cap != this->tail_cap_) {
|
||||
// Kept once grown, so a burst does not allocate per event
|
||||
grown = RAMAllocator<uint8_t>(RAMAllocator<uint8_t>::PREFER_INTERNAL).make_unique_array_for_overwrite(cap);
|
||||
if (!grown) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
uint8_t *dst = grown ? grown.get() : src;
|
||||
this->tail_len_ = queue_chunk(dst, src, this->tail_len_, this->tail_sent_, prefix, prefix_len, message, message_len);
|
||||
this->tail_sent_ = 0;
|
||||
if (grown) {
|
||||
this->tail_ = std::move(grown);
|
||||
this->tail_cap_ = cap;
|
||||
}
|
||||
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};
|
||||
#ifdef USE_JSON_ARENA
|
||||
json::JsonArena<json::JSON_ARENA_SIZE> arena;
|
||||
json::JsonBuilder builder(&arena);
|
||||
#else
|
||||
json::JsonBuilder builder;
|
||||
#endif
|
||||
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);
|
||||
progmem_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->drain_if_idle_() || this->tail_len_ != 0) {
|
||||
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);
|
||||
}
|
||||
this->stall_or_close_(LOG_STR("without memory"));
|
||||
}
|
||||
|
||||
bool AsyncEventSourceResponse::drain_if_idle_() {
|
||||
if (this->sending_ || !this->is_connected() || this->is_closing()) {
|
||||
return false;
|
||||
}
|
||||
drain_tail_();
|
||||
return !this->is_closing();
|
||||
}
|
||||
|
||||
bool AsyncEventSourceResponse::try_send_nodefer(const char *message, size_t message_len, const char *event, uint32_t id,
|
||||
uint32_t reconnect) {
|
||||
if (!this->drain_if_idle_()) {
|
||||
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};
|
||||
|
||||
if (this->tail_len_ != 0) {
|
||||
// Behind an unsent chunk (on ESP8266, one waiting for its ACK): queue it rather than drop it
|
||||
return this->append_chunk_(prefix, prefix_len, message, message_len, g.total);
|
||||
}
|
||||
// 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_WEBSERVER && (USE_ESP32 || USE_ARDUINO)
|
||||
@@ -0,0 +1,178 @@
|
||||
#pragma once
|
||||
|
||||
#include "esphome/core/defines.h"
|
||||
#if defined(USE_WEBSERVER) && (defined(USE_ESP32) || defined(USE_ARDUINO))
|
||||
|
||||
#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:
|
||||
explicit AsyncEventSourceResponse(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_();
|
||||
// Not re-entered from a log line and the session is alive after pushing the tail
|
||||
bool drain_if_idle_();
|
||||
// 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);
|
||||
// Queue a whole chunk behind the unsent part of the tail; false when it does not fit TAIL_QUEUE_SIZE or OOM
|
||||
bool append_chunk_(const char *prefix, size_t prefix_len, const char *message, size_t message_len, size_t total);
|
||||
// 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_();
|
||||
|
||||
std::vector<DeferredEvent> deferred_queue_;
|
||||
WebServer *web_server_;
|
||||
// Next session in AsyncEventSource's sessions or pending list
|
||||
AsyncEventSourceResponse *next_{nullptr};
|
||||
ListEntitiesIterator entities_iterator_;
|
||||
// Chunks the socket did not take yet, 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};
|
||||
#ifdef USE_ESP8266
|
||||
// The 4 KB loop stack: smaller send frames, anything larger goes through the tail
|
||||
static constexpr size_t MAX_SEND_LINES = 8;
|
||||
static constexpr size_t JSON_BUF_SIZE = 512;
|
||||
#else
|
||||
// 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;
|
||||
// Stack buffer for a state event's JSON; a larger document is serialized into the tail
|
||||
static constexpr size_t JSON_BUF_SIZE = 1024;
|
||||
#endif
|
||||
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;
|
||||
// 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 an unsent chunk and the events queued behind it may hold: about one TCP segment
|
||||
static constexpr size_t TAIL_QUEUE_SIZE = 1460;
|
||||
// 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;
|
||||
};
|
||||
|
||||
// Not an AsyncWebHandler: WebServer routes /events here, which keeps the handler base and its guard out of RAM
|
||||
class AsyncEventSource {
|
||||
public:
|
||||
AsyncEventSource() = default;
|
||||
~AsyncEventSource();
|
||||
|
||||
void handle_request(AsyncWebServerRequest *request, WebServer *ws);
|
||||
void try_send_nodefer(const char *message, size_t message_len, const char *event = nullptr, uint32_t id = 0,
|
||||
uint32_t reconnect = 0);
|
||||
// State events to every session; each session's own listing sends the detail_all events
|
||||
void deferrable_send_state(void *source, message_generator_t *message_generator);
|
||||
/// Returns true if there are sessions remaining (including pending cleanup).
|
||||
bool loop();
|
||||
bool empty() const { return this->sessions_ == nullptr; }
|
||||
|
||||
protected:
|
||||
// Cold path: move sessions from the pending list into sessions_ and greet each one.
|
||||
void __attribute__((noinline, cold)) adopt_pending_sessions_main_loop_();
|
||||
// Network context: queue a new session for the main loop
|
||||
void push_pending_(AsyncEventSourceResponse *rsp);
|
||||
#ifndef USE_ESP32
|
||||
// web_server_base::StreamAcceptor callbacks, network context
|
||||
static web_server_base::AsyncStreamResponse *create_session(void *ws);
|
||||
static void publish_session(void *source, web_server_base::AsyncStreamResponse *stream);
|
||||
#endif
|
||||
|
||||
// Main-loop only; intrusive list through AsyncEventSourceResponse::next_
|
||||
AsyncEventSourceResponse *sessions_{nullptr};
|
||||
// Network-context intake, guarded by pending_mutex_; non-null means work to adopt
|
||||
std::atomic<AsyncEventSourceResponse *> pending_sessions_{nullptr};
|
||||
[[no_unique_address]] Mutex pending_mutex_;
|
||||
};
|
||||
|
||||
} // namespace esphome::web_server
|
||||
|
||||
#endif // USE_WEBSERVER && (USE_ESP32 || USE_ARDUINO)
|
||||
@@ -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
|
||||
|
||||
@@ -1,33 +1,22 @@
|
||||
#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)
|
||||
class DeferredUpdateEventSource;
|
||||
#endif
|
||||
// The /events session the iterator lists entities to
|
||||
class AsyncEventSourceResponse;
|
||||
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, AsyncEventSourceResponse *session) : web_server_(ws), session_(session) {}
|
||||
|
||||
// Entity overrides (generated from entity_types.h).
|
||||
// Implementations live in list_entities.cpp.
|
||||
@@ -45,11 +34,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
|
||||
AsyncEventSourceResponse *session_;
|
||||
};
|
||||
|
||||
} // namespace esphome::web_server
|
||||
|
||||
@@ -0,0 +1,119 @@
|
||||
#include "sse_chunk.h"
|
||||
|
||||
#include <cinttypes>
|
||||
#include <cstring>
|
||||
|
||||
#include "esphome/core/hal.h"
|
||||
#include "esphome/core/helpers.h"
|
||||
|
||||
namespace esphome::web_server {
|
||||
|
||||
const char CHUNK_END[] PROGMEM = "\r\n";
|
||||
const char SSE_SEP[] PROGMEM = "\r\ndata: ";
|
||||
const char SSE_SUFFIX[] PROGMEM = "\r\n\r\n\r\n";
|
||||
const char SSE_STATUS[] PROGMEM = "200 OK";
|
||||
const char SSE_CONTENT_TYPE[] PROGMEM = "text/event-stream";
|
||||
const char SSE_FIRST_CHUNK[] PROGMEM = "2\r\n\r\n\r\n";
|
||||
static const char SSE_TRANSFER_ENCODING[] PROGMEM = "Transfer-Encoding";
|
||||
static const char SSE_CHUNKED[] PROGMEM = "chunked";
|
||||
static const char SSE_CACHE_CONTROL[] PROGMEM = "Cache-Control";
|
||||
static const char SSE_NO_CACHE[] PROGMEM = "no-cache";
|
||||
static const char SSE_CONNECTION[] PROGMEM = "Connection";
|
||||
static const char SSE_KEEP_ALIVE[] PROGMEM = "keep-alive";
|
||||
// Word aligned pointers, which ESP8266 reads straight from flash
|
||||
const web_server_base::HttpHeader SSE_HEADERS[SSE_HEADERS_COUNT] PROGMEM = {
|
||||
{SSE_TRANSFER_ENCODING, SSE_CHUNKED},
|
||||
{SSE_CACHE_CONTROL, SSE_NO_CACHE},
|
||||
{SSE_CONNECTION, SSE_KEEP_ALIVE},
|
||||
};
|
||||
|
||||
void for_each_chunk_piece(const char *message, size_t message_len, ChunkPieceSink sink, void *ctx) {
|
||||
if (message == nullptr) {
|
||||
sink(ctx, CHUNK_END, CHUNK_END_LEN);
|
||||
return;
|
||||
}
|
||||
const char *pos = message;
|
||||
const char *end = message + message_len;
|
||||
for (;;) {
|
||||
const size_t remaining = end - pos;
|
||||
const auto *n = static_cast<const char *>(memchr(pos, '\n', remaining));
|
||||
// Only a \r before the next \n can end this line, so the search stops there instead of
|
||||
// rescanning the rest of the message for every line
|
||||
const auto *r = static_cast<const char *>(memchr(pos, '\r', n != nullptr ? n - pos : remaining));
|
||||
if (n == nullptr && r == nullptr) {
|
||||
sink(ctx, pos, remaining);
|
||||
break;
|
||||
}
|
||||
const char *brk = (r != nullptr && (n == nullptr || r < n)) ? r : n;
|
||||
sink(ctx, pos, brk - pos);
|
||||
pos = brk + ((brk == r && brk + 1 == n) ? 2 : 1);
|
||||
if (pos >= end) {
|
||||
break;
|
||||
}
|
||||
sink(ctx, SSE_SEP, SSE_SEP_LEN);
|
||||
}
|
||||
sink(ctx, SSE_SUFFIX, SSE_SUFFIX_LEN);
|
||||
}
|
||||
|
||||
size_t build_chunk_prefix(char *buf, size_t size, const char *event, uint32_t id, uint32_t reconnect, bool with_data) {
|
||||
size_t len = CHUNK_HDR_LEN;
|
||||
if (reconnect) {
|
||||
len = buf_append_printf(buf, size, len, "retry: %" PRIu32 "\r\n", reconnect);
|
||||
}
|
||||
if (id) {
|
||||
len = buf_append_printf(buf, size, len, "id: %" PRIu32 "\r\n", id);
|
||||
}
|
||||
if (event && *event) {
|
||||
// event is not a literal, so it cannot go through buf_append_str (a PSTR() wrapper on ESP8266)
|
||||
len = buf_append_printf(buf, size, len, "event: %s\r\n", event);
|
||||
}
|
||||
if (with_data) {
|
||||
len = buf_append_str(buf, size, len, "data: ");
|
||||
}
|
||||
return len;
|
||||
}
|
||||
|
||||
void write_chunk_header(char *buf, size_t chunk_len) {
|
||||
// Eight lowercase hex digits; the temp keeps the terminator format_hex_to writes out of buf
|
||||
char digits[9];
|
||||
format_hex_to(digits, static_cast<uint32_t>(chunk_len));
|
||||
std::memcpy(buf, digits, 8);
|
||||
buf[8] = '\r';
|
||||
buf[9] = '\n';
|
||||
}
|
||||
|
||||
size_t copy_chunk(uint8_t *dst, const char *prefix, size_t prefix_len, const char *message, size_t message_len) {
|
||||
uint8_t *const start = dst;
|
||||
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);
|
||||
progmem_memcpy(out, piece, len);
|
||||
out += len;
|
||||
},
|
||||
&dst);
|
||||
return dst - start;
|
||||
}
|
||||
|
||||
size_t tail_queue_cap(size_t need, size_t cap, size_t max) {
|
||||
if (need > max) {
|
||||
return 0;
|
||||
}
|
||||
if (need <= cap) {
|
||||
return cap;
|
||||
}
|
||||
return need <= max / 2 ? max / 2 : max;
|
||||
}
|
||||
|
||||
size_t queue_chunk(uint8_t *dst, const uint8_t *src, size_t len, size_t sent, const char *prefix, size_t prefix_len,
|
||||
const char *message, size_t message_len) {
|
||||
const size_t pending = len - sent;
|
||||
if (dst != src || sent != 0) {
|
||||
std::memmove(dst, src + sent, pending);
|
||||
}
|
||||
return pending + copy_chunk(dst + pending, prefix, prefix_len, message, message_len);
|
||||
}
|
||||
|
||||
} // namespace esphome::web_server
|
||||
+30
-8
@@ -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"
|
||||
@@ -13,14 +15,23 @@ namespace esphome::web_server_idf {
|
||||
// HTTP chunk header "%08x\r\n"
|
||||
constexpr size_t CHUNK_HDR_LEN = 10;
|
||||
// Chunk terminator, also the only piece of a null message (no data line, no blank line)
|
||||
constexpr char CHUNK_END[] = "\r\n";
|
||||
constexpr size_t CHUNK_END_LEN = sizeof(CHUNK_END) - 1;
|
||||
extern const char CHUNK_END[];
|
||||
constexpr size_t CHUNK_END_LEN = 2;
|
||||
// Between two data lines: the end of one and the prefix of the next
|
||||
constexpr char SSE_SEP[] = "\r\ndata: ";
|
||||
constexpr size_t SSE_SEP_LEN = sizeof(SSE_SEP) - 1;
|
||||
extern const char SSE_SEP[];
|
||||
constexpr size_t SSE_SEP_LEN = 8;
|
||||
// End of the last data line, the blank line ending the event, and the chunk terminator
|
||||
constexpr char SSE_SUFFIX[] = "\r\n\r\n\r\n";
|
||||
constexpr size_t SSE_SUFFIX_LEN = sizeof(SSE_SUFFIX) - 1;
|
||||
extern const char SSE_SUFFIX[];
|
||||
constexpr size_t SSE_SUFFIX_LEN = 6;
|
||||
|
||||
// Status, Content-Type and headers of the /events response head
|
||||
// In flash on ESP8266; the stream helpers read them with the _P functions
|
||||
extern const char SSE_STATUS[];
|
||||
extern const char SSE_CONTENT_TYPE[];
|
||||
constexpr size_t SSE_HEADERS_COUNT = 3;
|
||||
extern const web_server_base::HttpHeader SSE_HEADERS[SSE_HEADERS_COUNT];
|
||||
// Hand-framed first chunk holding an empty event (one blank line), as httpd_resp_send_chunk() sent it before
|
||||
extern const char SSE_FIRST_CHUNK[];
|
||||
|
||||
// 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 +48,15 @@ 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
|
||||
// Copies the prefix and the message's pieces to dst; returns the bytes written
|
||||
size_t copy_chunk(uint8_t *dst, const char *prefix, size_t prefix_len, const char *message, size_t message_len);
|
||||
|
||||
// Tail capacity for need bytes: cap when they fit, else max / 2, then max; 0 when need > max
|
||||
size_t tail_queue_cap(size_t need, size_t cap, size_t max);
|
||||
|
||||
// Moves the unsent bytes [sent, len) of src to the front of dst (dst may be src) and copies the chunk after them.
|
||||
// Returns the new length.
|
||||
size_t queue_chunk(uint8_t *dst, const uint8_t *src, size_t len, size_t sent, const char *prefix, size_t prefix_len,
|
||||
const char *message, size_t message_len);
|
||||
|
||||
} // namespace esphome::web_server
|
||||
@@ -8,14 +8,11 @@
|
||||
#include "esphome/core/application.h"
|
||||
#include "esphome/core/defines.h"
|
||||
#include "esphome/core/entity_base.h"
|
||||
#include "esphome/core/hal.h"
|
||||
#include "esphome/core/helpers.h"
|
||||
#include "esphome/core/log.h"
|
||||
#include "esphome/core/util.h"
|
||||
|
||||
#if !defined(USE_ESP32) && defined(USE_ARDUINO)
|
||||
#include "StreamString.h"
|
||||
#endif
|
||||
|
||||
#include <cstdlib>
|
||||
|
||||
#ifdef USE_LIGHT
|
||||
@@ -219,197 +216,6 @@ EntityMatchResult UrlMatch::match_entity(EntityBase *entity) const {
|
||||
return result;
|
||||
}
|
||||
|
||||
#if !defined(USE_ESP32) && defined(USE_ARDUINO)
|
||||
// BEGIN ESPAsyncWebServer 3.12.1 workaround: remove once a release containing upstream commit
|
||||
// 880c97068abb0f0e63f803cd3c0cac84fa5cd7bb is out and we bump to it.
|
||||
// 3.12.1 creates the SSE client before the head is ACKed and credits the head's ACK to its first queued messages,
|
||||
// dropping or truncating them. Create the client once the head is ACKed instead, as 3.9.6 did.
|
||||
class HeadAckedEventSourceResponse final : public AsyncEventSourceResponse {
|
||||
public:
|
||||
HeadAckedEventSourceResponse(DeferredUpdateEventSourceList *list, WebServer *ws)
|
||||
: AsyncEventSourceResponse(nullptr), list_(list), ws_(ws) {}
|
||||
void _respond(AsyncWebServerRequest *request) override {
|
||||
String head;
|
||||
this->_assembleHead(head, request->version());
|
||||
request->client()->write(head.c_str(), this->_headLength);
|
||||
}
|
||||
// Every ACK until clientRelease() is the head's, and clientRelease() unhooks this. _state must stay
|
||||
// RESPONSE_SETUP: past RESPONSE_WAIT_ACK the request would close its released (null) client after this returns.
|
||||
size_t _ack(AsyncWebServerRequest *request, size_t len, uint32_t time) override {
|
||||
this->_ackedLength += len;
|
||||
if (this->_ackedLength >= this->_headLength)
|
||||
this->list_->add_client_(this->ws_, request->clientRelease());
|
||||
return 0;
|
||||
}
|
||||
|
||||
private:
|
||||
DeferredUpdateEventSourceList *list_;
|
||||
WebServer *ws_;
|
||||
};
|
||||
// END ESPAsyncWebServer 3.12.1 workaround
|
||||
|
||||
// helper for allowing only unique entries in the queue
|
||||
void __attribute__((flatten)) DeferredUpdateEventSource::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 DeferredUpdateEventSource::process_deferred_queue_() {
|
||||
while (!deferred_queue_.empty()) {
|
||||
DeferredEvent &de = deferred_queue_.front();
|
||||
json::JsonBuilder builder;
|
||||
de.message_generator_(web_server_, de.source_, builder);
|
||||
auto message = builder.serialize();
|
||||
if (this->send(message.c_str(), "state") != DISCARDED) {
|
||||
// O(n) but memory efficiency is more important than speed here which is why std::vector was chosen
|
||||
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_().
|
||||
// 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.
|
||||
this->consecutive_send_failures_++;
|
||||
if (this->consecutive_send_failures_ >= MAX_CONSECUTIVE_SEND_FAILURES) {
|
||||
// Too many failures, connection is likely dead
|
||||
ESP_LOGW(TAG, "Closing stuck EventSource connection after %" PRIu16 " failed sends",
|
||||
this->consecutive_send_failures_);
|
||||
this->close();
|
||||
this->deferred_queue_.clear();
|
||||
}
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void DeferredUpdateEventSource::loop() {
|
||||
process_deferred_queue_();
|
||||
// One step per loop; refusals retry next pass
|
||||
this->entities_iterator_.try_advance(1);
|
||||
}
|
||||
|
||||
void DeferredUpdateEventSource::deferrable_send_state(void *source, bool detail_all,
|
||||
message_generator_t *message_generator) {
|
||||
// Skip if no connected clients to avoid unnecessary deferred queue processing
|
||||
if (this->count() == 0)
|
||||
return;
|
||||
|
||||
// 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;
|
||||
|
||||
if (!deferred_queue_.empty())
|
||||
process_deferred_queue_();
|
||||
if (!deferred_queue_.empty()) {
|
||||
// deferred queue still not empty which means downstream event queue full, no point trying to send first
|
||||
deq_push_back_with_dedup_(source, message_generator);
|
||||
} else {
|
||||
json::JsonBuilder builder;
|
||||
message_generator(web_server_, source, builder);
|
||||
auto message = builder.serialize();
|
||||
if (this->send(message.c_str(), "state") == DISCARDED) {
|
||||
deq_push_back_with_dedup_(source, message_generator);
|
||||
} else {
|
||||
this->consecutive_send_failures_ = 0; // Reset failure count on successful send
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// used for logs plus the initial ping/config
|
||||
void DeferredUpdateEventSource::try_send_nodefer(const char *message, size_t message_len, const char *event,
|
||||
uint32_t id, uint32_t reconnect) {
|
||||
// ESPAsyncWebServer's send() only accepts null-terminated strings
|
||||
(void) message_len;
|
||||
this->send(message, event, id, reconnect);
|
||||
}
|
||||
|
||||
bool DeferredUpdateEventSourceList::loop() {
|
||||
for (DeferredUpdateEventSource *dues : *this) {
|
||||
dues->loop();
|
||||
}
|
||||
return !this->empty();
|
||||
}
|
||||
|
||||
void DeferredUpdateEventSourceList::deferrable_send_state(void *source, bool detail_all,
|
||||
message_generator_t *message_generator) {
|
||||
// Skip if no event sources (no connected clients) to avoid unnecessary iteration
|
||||
if (this->empty())
|
||||
return;
|
||||
for (DeferredUpdateEventSource *dues : *this) {
|
||||
dues->deferrable_send_state(source, detail_all, message_generator);
|
||||
}
|
||||
}
|
||||
|
||||
void DeferredUpdateEventSourceList::try_send_nodefer(const char *message, size_t message_len, const char *event,
|
||||
uint32_t id, uint32_t reconnect) {
|
||||
for (DeferredUpdateEventSource *dues : *this) {
|
||||
dues->try_send_nodefer(message, message_len, event, id, reconnect);
|
||||
}
|
||||
}
|
||||
|
||||
// BEGIN ESPAsyncWebServer 3.12.1 workaround (see HeadAckedEventSourceResponse): once removed, add_new_client creates
|
||||
// the source as add_client_ does and ends with es->handleRequest(request); add_client_ goes away.
|
||||
void DeferredUpdateEventSourceList::add_new_client(WebServer *ws, AsyncWebServerRequest *request) {
|
||||
request->send(new HeadAckedEventSourceResponse(this, ws)); // NOLINT
|
||||
}
|
||||
|
||||
void DeferredUpdateEventSourceList::add_client_(WebServer *ws, AsyncClient *client) {
|
||||
DeferredUpdateEventSource *es = new DeferredUpdateEventSource(ws, ESPHOME_F("/events"));
|
||||
this->push_back(es);
|
||||
|
||||
es->onConnect([this, es](AsyncEventSourceClient *client) { this->on_client_connect_(es); });
|
||||
|
||||
es->onDisconnect([this, es](AsyncEventSourceClient *client) { this->on_client_disconnect_(es); });
|
||||
|
||||
new AsyncEventSourceClient(client, es); // NOLINT: owned by es
|
||||
ws->enable_loop_soon_any_context();
|
||||
}
|
||||
// END ESPAsyncWebServer 3.12.1 workaround
|
||||
|
||||
void DeferredUpdateEventSourceList::on_client_connect_(DeferredUpdateEventSource *source) {
|
||||
WebServer *ws = source->web_server_;
|
||||
ws->defer([ws, source]() {
|
||||
// Configure reconnect timeout and send config
|
||||
// this should always go through since the AsyncEventSourceClient event queue is empty on connect
|
||||
auto message = ws->get_config_json();
|
||||
source->try_send_nodefer(message.c_str(), message.size(), "ping", millis(), 30000);
|
||||
|
||||
#ifdef USE_WEBSERVER_SORTING
|
||||
for (auto &group : ws->sorting_groups_) {
|
||||
json::JsonBuilder builder;
|
||||
JsonObject root = builder.root();
|
||||
root[ESPHOME_F("name")] = linked(group.second.name);
|
||||
root[ESPHOME_F("sorting_weight")] = group.second.weight;
|
||||
auto group_msg = builder.serialize();
|
||||
|
||||
// up to 31 groups should be able to be queued initially without defer
|
||||
source->try_send_nodefer(group_msg.c_str(), group_msg.size(), "sorting_group");
|
||||
}
|
||||
#endif
|
||||
|
||||
source->entities_iterator_.begin(ws->include_internal_);
|
||||
});
|
||||
}
|
||||
|
||||
void DeferredUpdateEventSourceList::on_client_disconnect_(DeferredUpdateEventSource *source) {
|
||||
source->web_server_->defer([this, source]() {
|
||||
// This method was called via WebServer->defer() and is no longer executing in the
|
||||
// context of the network callback. The object is now dead and can be safely deleted.
|
||||
this->remove(source);
|
||||
delete source; // NOLINT
|
||||
});
|
||||
}
|
||||
#endif
|
||||
|
||||
#ifdef USE_WEBSERVER_CAPTIVE
|
||||
WebServer *global_web_server = nullptr; // NOLINT(cppcoreguidelines-avoid-non-const-global-variables)
|
||||
#endif
|
||||
@@ -455,9 +261,6 @@ void WebServer::setup() {
|
||||
}
|
||||
#endif
|
||||
|
||||
#ifdef USE_ESP32
|
||||
this->base_->add_handler(&this->events_);
|
||||
#endif
|
||||
this->base_->add_handler(this);
|
||||
#ifdef USE_WEBSERVER_CAPTIVE
|
||||
// Not-found fallback (outside the auth middleware): the OS captive portal probes hit
|
||||
@@ -532,6 +335,9 @@ void WebServer::handle_not_found_(AsyncWebServerRequest *request) {
|
||||
void WebServer::on_log(uint8_t level, const char *tag, const char *message, size_t message_len) {
|
||||
(void) level;
|
||||
(void) tag;
|
||||
// Sessions are main-loop state; RP2 logs from its network IRQ arrive here directly, so drop those
|
||||
if (!is_main_loop_thread())
|
||||
return;
|
||||
this->events_.try_send_nodefer(message, std::min(message_len, LOG_EVENT_MAX_LEN), "log", millis());
|
||||
}
|
||||
#endif
|
||||
@@ -693,7 +499,7 @@ static void set_json_icon_state_value(JsonObject root, EntityBase *obj, ProgmemS
|
||||
|
||||
void WebServer::send_state_event_(void *source, const EntityBase *entity, message_generator_t *generator) {
|
||||
if (!entity->is_internal() || this->include_internal_)
|
||||
this->events_.deferrable_send_state(source, false, generator);
|
||||
this->events_.deferrable_send_state(source, generator);
|
||||
}
|
||||
|
||||
#ifdef USE_SENSOR
|
||||
@@ -2278,10 +2084,8 @@ bool WebServer::canHandle(AsyncWebServerRequest *request) const {
|
||||
// Static URL checks - use ESPHOME_F to keep strings in flash on ESP8266
|
||||
if (url == ESPHOME_F("/"))
|
||||
return true;
|
||||
#if !defined(USE_ESP32) && defined(USE_ARDUINO)
|
||||
if (url == ESPHOME_F("/events"))
|
||||
if (method == HTTP_GET && url == ESPHOME_F("/events"))
|
||||
return true;
|
||||
#endif
|
||||
#ifdef USE_WEBSERVER_CSS_INCLUDE
|
||||
if (url == ESPHOME_F("/0.css"))
|
||||
return true;
|
||||
@@ -2424,20 +2228,16 @@ void WebServer::handleRequest(AsyncWebServerRequest *request) {
|
||||
this->handle_index_request(request);
|
||||
return;
|
||||
}
|
||||
|
||||
#ifdef USE_WEBSERVER_PRIVATE_NETWORK_ACCESS
|
||||
if (request->method() == HTTP_OPTIONS && request->hasHeader(ESPHOME_F("Access-Control-Request-Private-Network"))) {
|
||||
this->handle_pna_cors_request(request);
|
||||
return;
|
||||
}
|
||||
#endif
|
||||
|
||||
#if !defined(USE_ESP32) && defined(USE_ARDUINO)
|
||||
if (url == ESPHOME_F("/events")) {
|
||||
this->events_.add_new_client(this, request);
|
||||
this->events_.handle_request(request, this);
|
||||
return;
|
||||
}
|
||||
#endif
|
||||
|
||||
#ifdef USE_WEBSERVER_CSS_INCLUDE
|
||||
if (url == ESPHOME_F("/0.css")) {
|
||||
|
||||
@@ -5,7 +5,9 @@
|
||||
#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))
|
||||
#include "event_source.h"
|
||||
#include "esphome/core/component.h"
|
||||
#include "esphome/core/entity_base.h"
|
||||
#include "esphome/core/entity_includes.h"
|
||||
@@ -15,7 +17,6 @@
|
||||
#endif
|
||||
|
||||
#include <functional>
|
||||
#include <list>
|
||||
#include <map>
|
||||
#include <string>
|
||||
#include <utility>
|
||||
@@ -98,100 +99,6 @@ struct SortingGroup {
|
||||
|
||||
enum JsonDetail { DETAIL_ALL, DETAIL_STATE };
|
||||
|
||||
/*
|
||||
In order to defer updates in arduino mode, we need to create one AsyncEventSource per incoming request to /events.
|
||||
This is because only minimal changes were made to the ESPAsyncWebServer lib_dep, it was undesirable to put deferred
|
||||
update logic into that library. We need one deferred queue per connection so instead of one AsyncEventSource with
|
||||
multiple clients, we have multiple event sources with one client each. This is slightly awkward which is why it's
|
||||
implemented in a more straightforward way for ESP-IDF. Arduino platform will eventually go away and this workaround
|
||||
can be forgotten.
|
||||
*/
|
||||
#if !defined(USE_ESP32) && defined(USE_ARDUINO)
|
||||
using message_generator_t = void(WebServer *, void *, json::JsonBuilder &);
|
||||
|
||||
class DeferredUpdateEventSourceList;
|
||||
class DeferredUpdateEventSource final : public AsyncEventSource {
|
||||
friend class DeferredUpdateEventSourceList;
|
||||
|
||||
/*
|
||||
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 DeferredUpdateEventSource;
|
||||
|
||||
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");
|
||||
|
||||
protected:
|
||||
// surface a couple methods from the base class
|
||||
using AsyncEventSource::handleRequest;
|
||||
using AsyncEventSource::send;
|
||||
|
||||
ListEntitiesIterator entities_iterator_;
|
||||
// vector is used very specifically for its zero memory overhead even though items are popped from the front (memory
|
||||
// footprint is more important than speed here)
|
||||
std::vector<DeferredEvent> deferred_queue_;
|
||||
WebServer *web_server_;
|
||||
uint16_t consecutive_send_failures_{0};
|
||||
static constexpr uint16_t MAX_CONSECUTIVE_SEND_FAILURES = 2500; // ~20 seconds at 125Hz loop rate
|
||||
|
||||
// helper for allowing only unique entries in the queue
|
||||
void deq_push_back_with_dedup_(void *source, message_generator_t *message_generator);
|
||||
|
||||
void process_deferred_queue_();
|
||||
|
||||
public:
|
||||
DeferredUpdateEventSource(WebServer *ws, const String &url)
|
||||
: AsyncEventSource(url), entities_iterator_(ListEntitiesIterator(ws, this)), web_server_(ws) {}
|
||||
|
||||
void loop();
|
||||
|
||||
// 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 try_send_nodefer(const char *message, size_t message_len, const char *event = nullptr, uint32_t id = 0,
|
||||
uint32_t reconnect = 0);
|
||||
};
|
||||
|
||||
class DeferredUpdateEventSourceList final : public std::list<DeferredUpdateEventSource *> {
|
||||
friend class HeadAckedEventSourceResponse; // ESPAsyncWebServer 3.12.1 workaround, remove with it
|
||||
|
||||
protected:
|
||||
void on_client_connect_(DeferredUpdateEventSource *source);
|
||||
void on_client_disconnect_(DeferredUpdateEventSource *source);
|
||||
void add_client_(WebServer *ws, AsyncClient *client); // ESPAsyncWebServer 3.12.1 workaround, remove with it
|
||||
|
||||
public:
|
||||
/// Returns true if there are event sources remaining (including pending cleanup).
|
||||
bool loop();
|
||||
|
||||
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 try_send_nodefer(const char *message, size_t message_len, const char *event = nullptr, uint32_t id = 0,
|
||||
uint32_t reconnect = 0);
|
||||
|
||||
void add_new_client(WebServer *ws, AsyncWebServerRequest *request);
|
||||
};
|
||||
#endif
|
||||
|
||||
/** This class allows users to create a web server with their ESP nodes.
|
||||
*
|
||||
* Behind the scenes it's using AsyncWebServer to set up the server. It exposes 3 things:
|
||||
@@ -202,10 +109,6 @@ class DeferredUpdateEventSourceList final : public std::list<DeferredUpdateEvent
|
||||
* can be found under https://esphome.io/web-api/.
|
||||
*/
|
||||
class WebServer final : public Component, public AsyncWebHandler {
|
||||
#if !defined(USE_ESP32) && defined(USE_ARDUINO)
|
||||
friend class DeferredUpdateEventSourceList;
|
||||
#endif
|
||||
|
||||
public:
|
||||
WebServer(web_server_base::WebServerBase *base);
|
||||
|
||||
@@ -610,11 +513,7 @@ class WebServer final : public Component, public AsyncWebHandler {
|
||||
}
|
||||
|
||||
web_server_base::WebServerBase *base_;
|
||||
#ifdef USE_ESP32
|
||||
AsyncEventSource events_{StringRef::from_lit("/events"), this};
|
||||
#elif USE_ARDUINO
|
||||
DeferredUpdateEventSourceList events_;
|
||||
#endif
|
||||
AsyncEventSource events_;
|
||||
#ifdef USE_WEBSERVER_CAPTIVE
|
||||
void handle_not_found_(AsyncWebServerRequest *request);
|
||||
web_server_base::CaptiveDNS dns_;
|
||||
|
||||
@@ -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,246 @@
|
||||
#include "async_stream_response.h"
|
||||
#if !defined(USE_ESP32) && defined(USE_ARDUINO)
|
||||
|
||||
#include <cinttypes>
|
||||
|
||||
#include "esphome/core/application.h"
|
||||
#include "esphome/core/helpers.h"
|
||||
#include "esphome/core/log.h"
|
||||
|
||||
#ifndef USE_LIBRETINY
|
||||
#include <lwip/tcp.h>
|
||||
#endif
|
||||
|
||||
namespace esphome::web_server_base {
|
||||
|
||||
ESPHOME_LOG_TAG(TAG, "web_server_base");
|
||||
|
||||
static_assert(STREAM_ADD_FLAG_COPY == ASYNC_WRITE_FLAG_COPY && STREAM_ADD_FLAG_MORE == ASYNC_WRITE_FLAG_MORE,
|
||||
"add_iovecs() flags must match the TCP library");
|
||||
|
||||
// Bytes coalesced into one AsyncClient::add(); on the loop stack, so kept small
|
||||
static constexpr size_t ADD_SCRATCH_SIZE = 256;
|
||||
|
||||
// Closes now and runs the disconnect callback. AsyncTCP (LibreTiny) deprecates close(bool); ESPAsyncTCP and
|
||||
// RPAsyncTCP need it, as their close() waits for the next poll.
|
||||
static void close_now(AsyncClient *client) {
|
||||
#ifdef USE_LIBRETINY
|
||||
client->close();
|
||||
#else
|
||||
client->close(true);
|
||||
#endif
|
||||
}
|
||||
|
||||
#ifndef USE_LIBRETINY
|
||||
// Reaches the library's protected pcb
|
||||
struct AsyncClientPcb : AsyncClient {
|
||||
static tcp_pcb *of(AsyncClient *client) { return client->*(&AsyncClientPcb::_pcb); }
|
||||
};
|
||||
#endif
|
||||
|
||||
// Sends a RST and frees the pcb now. A graceful close queues its FIN behind data a stalled reader never takes, so
|
||||
// it would hold the pcb until the peer leaves. False on ESP8266/RP2, where the caller marks the stream disconnected;
|
||||
// LibreTiny's callback runs here or later from a queued error event that may still name the client, so never mark it.
|
||||
static bool abort_now(AsyncClient *client) {
|
||||
#ifdef USE_LIBRETINY
|
||||
// AsyncTCP resets the pcb callbacks in the tcpip thread, aborts, then runs the disconnect callback here
|
||||
client->abort();
|
||||
return true;
|
||||
#else
|
||||
// ESPAsyncTCP and RPAsyncTCP leave the pcb's error callback set, and RPAsyncTCP's would use the pcb lwIP freed
|
||||
tcp_pcb *pcb = AsyncClientPcb::of(client);
|
||||
if (pcb != nullptr)
|
||||
tcp_err(pcb, nullptr);
|
||||
client->abort();
|
||||
return false;
|
||||
#endif
|
||||
}
|
||||
|
||||
// Writes the head, then creates the stream with the connection. The library runs _respond() in the network context
|
||||
// right after the handler returns; clientRelease() then unhooks the request, which frees itself and this response.
|
||||
// A request that ends before _respond() never creates a stream, so nothing is left to free.
|
||||
class StreamHandoffResponse final : public AsyncWebServerResponse {
|
||||
public:
|
||||
StreamHandoffResponse(const char *status, const char *content_type, std::span<const HttpHeader> headers,
|
||||
const char *body, const StreamAcceptor &acceptor)
|
||||
: status_(status), content_type_(content_type), headers_(headers), body_(body), acceptor_(acceptor) {}
|
||||
|
||||
void _respond(AsyncWebServerRequest *request) override {
|
||||
AsyncClient *client = request->client();
|
||||
// The library's default headers only reach its own responses, so this raw head writes them itself
|
||||
HttpHeader defaults[WEB_SERVER_DEFAULT_HEADERS_COUNT];
|
||||
size_t default_count = 0;
|
||||
for (const auto &header : DefaultHeaders::Instance()) {
|
||||
if (default_count == WEB_SERVER_DEFAULT_HEADERS_COUNT) {
|
||||
break;
|
||||
}
|
||||
defaults[default_count++] = {header.name().c_str(), header.value().c_str()};
|
||||
}
|
||||
char scratch[ADD_SCRATCH_SIZE];
|
||||
const bool head_sent = write_stream_head<StreamIovec>(
|
||||
[client, &scratch](StreamIovec *iov, int count) {
|
||||
return add_iovecs(*client, iov, count, scratch, sizeof(scratch)) == iovecs_total(iov, count);
|
||||
},
|
||||
this->status_, this->content_type_, this->headers_, std::span<const HttpHeader>(defaults, default_count),
|
||||
this->body_);
|
||||
AsyncStreamResponse *stream = this->acceptor_.create(this->acceptor_.ctx);
|
||||
stream->attach_(request->clientRelease(), head_sent);
|
||||
this->acceptor_.publish(this->acceptor_.owner, stream);
|
||||
}
|
||||
bool _sourceValid() const override { return true; }
|
||||
|
||||
protected:
|
||||
const char *status_;
|
||||
const char *content_type_;
|
||||
std::span<const HttpHeader> headers_;
|
||||
const char *body_;
|
||||
StreamAcceptor acceptor_;
|
||||
};
|
||||
|
||||
void AsyncStreamResponse::accept(AsyncWebServerRequest *request, const char *status, const char *content_type,
|
||||
std::span<const HttpHeader> headers, const char *body,
|
||||
const StreamAcceptor &acceptor) {
|
||||
// NOLINTNEXTLINE(cppcoreguidelines-owning-memory) the request owns and frees its response
|
||||
request->send(new StreamHandoffResponse(status, content_type, headers, body, acceptor));
|
||||
}
|
||||
|
||||
void AsyncStreamResponse::attach_(AsyncClient *client, bool head_sent) {
|
||||
client->setRxTimeout(0);
|
||||
// Backstop for data left unacked once writes stop; each send restarts it
|
||||
client->setAckTimeout(SEND_STALL_TIMEOUT_MS);
|
||||
client->setNoDelay(true);
|
||||
// clientRelease() already cleared the request's data, ack, poll and error handlers
|
||||
// Network context: store a flag and nothing else; the main loop frees the client. On LibreTiny async_tcp runs above
|
||||
// the loop task and touches nothing after this callback, so the loop cannot free the client under it.
|
||||
client->onDisconnect(
|
||||
[](void *arg, AsyncClient *) {
|
||||
static_cast<AsyncStreamResponse *>(arg)->disconnected_.store(true, std::memory_order_release);
|
||||
},
|
||||
this);
|
||||
client->onTimeout(
|
||||
[](void *arg, AsyncClient *, uint32_t) {
|
||||
static_cast<AsyncStreamResponse *>(arg)->ack_timed_out_.store(true, std::memory_order_release);
|
||||
},
|
||||
this);
|
||||
this->client_ = client;
|
||||
// A partly queued head must not be continued by later writes. Closing here, inside the client's receive callback,
|
||||
// could free the pcb the library still uses after it, so the main loop closes it once the callback has returned.
|
||||
this->close_requested_ = !head_sent;
|
||||
}
|
||||
|
||||
AsyncStreamResponse::~AsyncStreamResponse() {
|
||||
AsyncClient *client = this->client_;
|
||||
if (client == nullptr) {
|
||||
return;
|
||||
}
|
||||
// Held so the RP2 network IRQ cannot run while the client goes away
|
||||
LwIPLock lock;
|
||||
client->onDisconnect(nullptr, nullptr);
|
||||
client->onTimeout(nullptr, nullptr);
|
||||
delete client; // NOLINT(cppcoreguidelines-owning-memory)
|
||||
}
|
||||
|
||||
ssize_t AsyncStreamResponse::write(StreamIovec *iov, int iovcnt) {
|
||||
if (this->close_requested_) {
|
||||
return -1;
|
||||
}
|
||||
// Nothing to send is not a stall
|
||||
if (iovecs_total(iov, iovcnt) == 0) {
|
||||
return 0;
|
||||
}
|
||||
if (!this->is_connected()) {
|
||||
this->close();
|
||||
return -1;
|
||||
}
|
||||
size_t sent = 0;
|
||||
const uint32_t now = App.get_loop_component_start_time();
|
||||
if (!this->stall_.is_running() || now - this->last_try_ms_ >= STALL_RETRY_MS) {
|
||||
this->last_try_ms_ = now;
|
||||
// RP2: the network IRQ may free the pcb between the check and the use, so hold it out across both
|
||||
LwIPLock lock;
|
||||
#ifdef USE_ESP8266
|
||||
// Every segment holds a full MSS pbuf on the heap until acked (LWIP_NETIF_TX_SINGLE_PBUF), so write only once
|
||||
// the last send is acked, like ESPAsyncWebServer. Not a stall: the library's ACK timeout covers a silent peer.
|
||||
if (this->is_connected() && !this->client_->canSend())
|
||||
return 0;
|
||||
#endif
|
||||
if (this->is_connected()) {
|
||||
// Only an ACK grows the send buffer's free space (lwIP snd_buf). ESPAsyncTCP and RPAsyncTCP call onAck
|
||||
// only once everything is acked, so it cannot measure this.
|
||||
if (this->client_->space() > this->space_after_write_)
|
||||
this->last_ack_ms_ = now;
|
||||
char scratch[ADD_SCRATCH_SIZE];
|
||||
sent = add_iovecs(*this->client_, iov, iovcnt, scratch, sizeof(scratch));
|
||||
if (sent > 0)
|
||||
this->space_after_write_ = this->client_->space();
|
||||
}
|
||||
}
|
||||
if (sent > 0) {
|
||||
this->stall_.clear();
|
||||
// lwIP keeps queueing into a large buffer while the window is shut, so the buffer may never fill
|
||||
if (now - this->last_ack_ms_ >= SEND_STALL_TIMEOUT_MS) {
|
||||
ESP_LOGW(TAG, "Closing stuck stream after %" PRIu32 " ms without an ACK", now - this->last_ack_ms_);
|
||||
this->abort_();
|
||||
return -1;
|
||||
}
|
||||
return static_cast<ssize_t>(sent);
|
||||
}
|
||||
// Send buffer full, try again later; a buffer that stays full past the stall timeout is closed
|
||||
return this->stall_or_close_(LOG_STR("without send progress")) ? -1 : 0;
|
||||
}
|
||||
|
||||
ssize_t AsyncStreamResponse::write(const void *data, size_t len) {
|
||||
StreamIovec iov = {const_cast<void *>(data), len};
|
||||
return this->write(&iov, 1);
|
||||
}
|
||||
|
||||
bool AsyncStreamResponse::stall_or_close_(const LogString *reason) {
|
||||
const uint32_t now = App.get_loop_component_start_time();
|
||||
if (!this->stall_.start_or_expired(now, SEND_STALL_TIMEOUT_MS)) {
|
||||
return false;
|
||||
}
|
||||
ESP_LOGW(TAG, "Closing stuck stream after %" PRIu32 " ms %s", this->stall_.elapsed(now), LOG_STR_ARG(reason));
|
||||
this->abort_();
|
||||
return true;
|
||||
}
|
||||
|
||||
void AsyncStreamResponse::abort_() {
|
||||
this->close_requested_ = true;
|
||||
this->close_client_(true);
|
||||
}
|
||||
|
||||
void AsyncStreamResponse::close() {
|
||||
this->close_requested_ = true;
|
||||
this->close_client_();
|
||||
}
|
||||
|
||||
void AsyncStreamResponse::close_client_(bool abort) {
|
||||
if (this->close_sent_ || !this->is_connected()) {
|
||||
return;
|
||||
}
|
||||
this->close_sent_ = true;
|
||||
LwIPLock lock;
|
||||
if (!this->is_connected()) {
|
||||
return;
|
||||
}
|
||||
if (!abort) {
|
||||
close_now(this->client_);
|
||||
} else if (!abort_now(this->client_)) {
|
||||
this->disconnected_.store(true, std::memory_order_release);
|
||||
}
|
||||
}
|
||||
|
||||
void AsyncStreamResponse::loop() {
|
||||
if (this->close_requested_) {
|
||||
this->close_client_();
|
||||
return;
|
||||
}
|
||||
if (this->ack_timed_out_.load(std::memory_order_acquire)) {
|
||||
ESP_LOGW(TAG, "Closing stream after %" PRIu32 " ms without an ACK", SEND_STALL_TIMEOUT_MS);
|
||||
this->abort_();
|
||||
}
|
||||
}
|
||||
|
||||
} // namespace esphome::web_server_base
|
||||
|
||||
#endif // !USE_ESP32 && USE_ARDUINO
|
||||
@@ -0,0 +1,108 @@
|
||||
#pragma once
|
||||
|
||||
#include "esphome/core/defines.h"
|
||||
#if !defined(USE_ESP32) && defined(USE_ARDUINO)
|
||||
|
||||
#include <ESPAsyncWebServer.h>
|
||||
|
||||
#include <atomic>
|
||||
#include <cstddef>
|
||||
#include <cstdint>
|
||||
#include <span>
|
||||
#include <sys/types.h>
|
||||
|
||||
#include "esphome/core/log.h"
|
||||
#include "stream_head.h"
|
||||
|
||||
namespace esphome::web_server_base {
|
||||
|
||||
/// One entry of the gather list AsyncStreamResponse::write() takes; the TCP libraries take plain buffers
|
||||
struct StreamIovec {
|
||||
void *iov_base;
|
||||
size_t iov_len;
|
||||
};
|
||||
|
||||
class AsyncStreamResponse;
|
||||
|
||||
/// Who makes and takes the stream accept() creates: create(ctx) allocates it, publish(owner, stream) hands the
|
||||
/// attached stream to the main loop. Both run in the network context.
|
||||
struct StreamAcceptor {
|
||||
AsyncStreamResponse *(*create)(void *ctx);
|
||||
void (*publish)(void *owner, AsyncStreamResponse *stream);
|
||||
void *owner;
|
||||
void *ctx;
|
||||
};
|
||||
|
||||
/// A long lived response the main loop writes to without blocking, on ESPAsyncWebServer. It exists only once the
|
||||
/// head is queued and the connection taken from the request; the network callbacks only set flags, and the main
|
||||
/// loop owns and frees the AsyncClient. Writes like web_server_idf's AsyncStreamResponse.
|
||||
class AsyncStreamResponse {
|
||||
public:
|
||||
AsyncStreamResponse() = default;
|
||||
AsyncStreamResponse(const AsyncStreamResponse &) = delete;
|
||||
AsyncStreamResponse &operator=(const AsyncStreamResponse &) = delete;
|
||||
/// Main loop, once safe_to_delete(): frees the connection
|
||||
~AsyncStreamResponse();
|
||||
|
||||
/// Network context, from a request handler. Queues the status line, Content-Type, headers, the default headers and
|
||||
/// body (a C string or null), then has the acceptor create the stream, attach the connection and publish it. A
|
||||
/// chunked response passes {"Transfer-Encoding", "chunked"} as its first header and frames each chunk itself.
|
||||
static void accept(AsyncWebServerRequest *request, const char *status, const char *content_type,
|
||||
std::span<const HttpHeader> headers, const char *body, const StreamAcceptor &acceptor);
|
||||
|
||||
/// 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 send buffer is full, the last send is not acked yet (ESP8266
|
||||
/// only) or the connection is not ours yet; -1 means the stream is closing. SEND_STALL_TIMEOUT_MS of refused writes,
|
||||
/// or of writes with no ACK in between, also aborts it (RST).
|
||||
ssize_t write(StreamIovec *iov, int iovcnt);
|
||||
ssize_t write(const void *data, size_t len);
|
||||
/// Main loop. Closes the connection; safe to call more than once.
|
||||
void close();
|
||||
/// Main loop, every pass. Aborts (RST) a stream whose last send went unacknowledged for SEND_STALL_TIMEOUT_MS.
|
||||
void loop();
|
||||
|
||||
/// Always true: a stream is published only once it has its connection.
|
||||
bool is_attached() const { return true; }
|
||||
/// The client is still connected.
|
||||
bool is_connected() const { return !this->disconnected_.load(std::memory_order_acquire); }
|
||||
bool is_closing() const { return this->close_requested_; }
|
||||
/// An id for log lines only
|
||||
int log_id() const { return static_cast<int>(reinterpret_cast<uintptr_t>(this) & 0xFFFF); }
|
||||
/// The connection is gone; no network callback still references this object.
|
||||
bool safe_to_delete() const { return this->disconnected_.load(std::memory_order_acquire); }
|
||||
|
||||
static constexpr uint32_t SEND_STALL_TIMEOUT_MS = 20000;
|
||||
// While stalled, retry at most this often: on RP2 each try releases the lwIP lock, which postpones a polled
|
||||
// Ethernet chip's 20 ms receive poll
|
||||
static constexpr uint32_t STALL_RETRY_MS = 100;
|
||||
|
||||
protected:
|
||||
friend class StreamHandoffResponse;
|
||||
|
||||
// Network context, before publishing: take the connection; a head not queued whole closes it
|
||||
void attach_(AsyncClient *client, bool head_sent);
|
||||
// Starts the stall clock; once it has run SEND_STALL_TIMEOUT_MS, logs reason and aborts. True when it did.
|
||||
bool stall_or_close_(const LogString *reason);
|
||||
// Graceful FIN, or with abort a RST that frees the pcb now (a stuck stream's FIN would queue behind its data)
|
||||
void close_client_(bool abort = false);
|
||||
void abort_();
|
||||
|
||||
// Set before publishing; the main loop owns it and frees it once disconnected_
|
||||
AsyncClient *client_{nullptr};
|
||||
// Runs while sends make no progress
|
||||
StallClock stall_;
|
||||
uint32_t last_try_ms_{0};
|
||||
// Send buffer free after the last write that queued bytes, and when an ACK was last seen; main loop only
|
||||
size_t space_after_write_{0};
|
||||
uint32_t last_ack_ms_{0};
|
||||
// Set from the network callbacks with plain stores (no read-modify-write: RP2040 has none)
|
||||
std::atomic<bool> disconnected_{false};
|
||||
std::atomic<bool> ack_timed_out_{false};
|
||||
// Main loop only
|
||||
bool close_requested_{false};
|
||||
bool close_sent_{false};
|
||||
};
|
||||
|
||||
} // namespace esphome::web_server_base
|
||||
|
||||
#endif // !USE_ESP32 && USE_ARDUINO
|
||||
@@ -0,0 +1,199 @@
|
||||
#pragma once
|
||||
|
||||
#include <algorithm>
|
||||
#include <cstddef>
|
||||
#include <cstdint>
|
||||
#include <cstring>
|
||||
#include <span>
|
||||
|
||||
#include <strings.h>
|
||||
|
||||
#include "esphome/core/defines.h"
|
||||
#ifdef USE_ESP8266
|
||||
#include <pgmspace.h>
|
||||
#endif
|
||||
|
||||
// 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 {
|
||||
|
||||
// The stream helpers take strings and pieces that may sit in flash on ESP8266: only word reads
|
||||
// work there, so copies and string scans go through the _P functions (which also read RAM)
|
||||
#ifdef USE_ESP8266
|
||||
#define STREAM_PSTR(s) PSTR(s)
|
||||
inline size_t stream_strlen(const char *s) { return strlen_P(s); }
|
||||
inline int stream_strcasecmp(const char *ram, const char *any) { return strcasecmp_P(ram, any); }
|
||||
inline void stream_memcpy(void *dst, const void *src, size_t len) { memcpy_P(dst, src, len); }
|
||||
#else
|
||||
#define STREAM_PSTR(s) (s)
|
||||
inline size_t stream_strlen(const char *s) { return strlen(s); }
|
||||
inline int stream_strcasecmp(const char *ram, const char *any) { return strcasecmp(ram, any); }
|
||||
inline void stream_memcpy(void *dst, const void *src, size_t len) { std::memcpy(dst, src, len); }
|
||||
#endif
|
||||
|
||||
struct HttpHeader {
|
||||
const char *name;
|
||||
const char *value;
|
||||
};
|
||||
|
||||
/// Bytes in a gather list
|
||||
template<typename Iovec> size_t iovecs_total(const Iovec *iov, int count) {
|
||||
size_t total = 0;
|
||||
for (int i = 0; i < count; i++) {
|
||||
total += iov[i].iov_len;
|
||||
}
|
||||
return total;
|
||||
}
|
||||
|
||||
/// 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 (stream_strcasecmp(name, header.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, stream_strlen(str)); }
|
||||
void add_header(const HttpHeader &header) {
|
||||
this->add(STREAM_PSTR("\r\n"), 2);
|
||||
this->add(header.name);
|
||||
this->add(STREAM_PSTR(": "), 2);
|
||||
this->add(header.value);
|
||||
}
|
||||
} batch{send};
|
||||
|
||||
batch.add(STREAM_PSTR("HTTP/1.1 "), 9);
|
||||
batch.add(status);
|
||||
batch.add(STREAM_PSTR("\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(STREAM_PSTR("\r\n\r\n"), 4);
|
||||
if (body != nullptr) {
|
||||
batch.add(body);
|
||||
}
|
||||
batch.flush();
|
||||
return batch.ok;
|
||||
}
|
||||
|
||||
/// AsyncClient::add() flags; the three TCP libraries use the same values (checked where they are included)
|
||||
static constexpr uint8_t STREAM_ADD_FLAG_COPY = 0x01;
|
||||
static constexpr uint8_t STREAM_ADD_FLAG_MORE = 0x02;
|
||||
|
||||
/// Queues a gather list on an AsyncClient-like client through scratch, so each add() carries up to scratch_size bytes
|
||||
/// (on LibreTiny every add() is a round trip into the tcpip thread). Stops at the first add() that takes less than
|
||||
/// offered, calls send() once if anything was taken, and returns the bytes taken.
|
||||
template<typename Client, typename Iovec>
|
||||
size_t add_iovecs(Client &client, const Iovec *iov, int count, char *scratch, size_t scratch_size) {
|
||||
size_t taken = 0;
|
||||
size_t fill = 0;
|
||||
int i = 0;
|
||||
size_t offset = 0; // bytes of iov[i] already copied
|
||||
while (i < count) {
|
||||
const size_t piece = std::min(iov[i].iov_len - offset, scratch_size - fill);
|
||||
stream_memcpy(scratch + fill, static_cast<const char *>(iov[i].iov_base) + offset, piece);
|
||||
fill += piece;
|
||||
offset += piece;
|
||||
if (offset == iov[i].iov_len) {
|
||||
++i;
|
||||
offset = 0;
|
||||
}
|
||||
if (fill < scratch_size && i < count) {
|
||||
continue;
|
||||
}
|
||||
const uint8_t flags = STREAM_ADD_FLAG_COPY | (i < count ? STREAM_ADD_FLAG_MORE : 0);
|
||||
const size_t added = client.add(scratch, fill, flags);
|
||||
taken += added;
|
||||
if (added < fill) {
|
||||
break;
|
||||
}
|
||||
fill = 0;
|
||||
}
|
||||
if (taken != 0) {
|
||||
client.send();
|
||||
}
|
||||
return taken;
|
||||
}
|
||||
|
||||
/// 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_; }
|
||||
/// Starts the clock if needed; true once it has run for timeout_ms
|
||||
bool start_or_expired(uint32_t now, uint32_t timeout_ms) {
|
||||
this->start(now);
|
||||
return this->is_expired(now, timeout_ms);
|
||||
}
|
||||
|
||||
protected:
|
||||
uint32_t started_ms_{0};
|
||||
};
|
||||
|
||||
} // namespace esphome::web_server_base
|
||||
@@ -0,0 +1,36 @@
|
||||
#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 "esphome/core/helpers.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;
|
||||
// Guards the hand-off of new streams from the server task to the main loop
|
||||
using StreamIntakeGuard = LockGuard;
|
||||
} // namespace esphome::web_server_base
|
||||
#elif defined(USE_ARDUINO)
|
||||
// ESP8266, RP2040 and LibreTiny: ESPAsyncWebServer, with the connection taken from the request
|
||||
#include "async_stream_response.h"
|
||||
#include "esphome/core/helpers.h"
|
||||
|
||||
namespace esphome::web_server_base {
|
||||
// Guards the hand-off of new streams from the network context to the main loop: the lwIP lock keeps RP2's network
|
||||
// IRQ out, the mutex LibreTiny's async_tcp task; each is a no-op where it is not needed (both on ESP8266)
|
||||
class StreamIntakeGuard {
|
||||
public:
|
||||
explicit StreamIntakeGuard(Mutex &mutex) : guard_(mutex) {}
|
||||
|
||||
protected:
|
||||
LwIPLock lock_;
|
||||
LockGuard guard_;
|
||||
};
|
||||
} // namespace esphome::web_server_base
|
||||
#endif
|
||||
@@ -1,66 +0,0 @@
|
||||
#include "sse_chunk.h"
|
||||
|
||||
#include <cinttypes>
|
||||
#include <cstring>
|
||||
|
||||
#include "esphome/core/helpers.h"
|
||||
|
||||
namespace esphome::web_server_idf {
|
||||
|
||||
void for_each_chunk_piece(const char *message, size_t message_len, ChunkPieceSink sink, void *ctx) {
|
||||
if (message == nullptr) {
|
||||
sink(ctx, CHUNK_END, CHUNK_END_LEN);
|
||||
return;
|
||||
}
|
||||
const char *pos = message;
|
||||
const char *end = message + message_len;
|
||||
for (;;) {
|
||||
const size_t remaining = end - pos;
|
||||
const auto *n = static_cast<const char *>(memchr(pos, '\n', remaining));
|
||||
// Only a \r before the next \n can end this line, so the search stops there instead of
|
||||
// rescanning the rest of the message for every line
|
||||
const auto *r = static_cast<const char *>(memchr(pos, '\r', n != nullptr ? n - pos : remaining));
|
||||
if (n == nullptr && r == nullptr) {
|
||||
sink(ctx, pos, remaining);
|
||||
break;
|
||||
}
|
||||
const char *brk = (r != nullptr && (n == nullptr || r < n)) ? r : n;
|
||||
sink(ctx, pos, brk - pos);
|
||||
pos = brk + ((brk == r && brk + 1 == n) ? 2 : 1);
|
||||
if (pos >= end) {
|
||||
break;
|
||||
}
|
||||
sink(ctx, SSE_SEP, SSE_SEP_LEN);
|
||||
}
|
||||
sink(ctx, SSE_SUFFIX, SSE_SUFFIX_LEN);
|
||||
}
|
||||
|
||||
size_t build_chunk_prefix(char *buf, size_t size, const char *event, uint32_t id, uint32_t reconnect, bool with_data) {
|
||||
size_t len = CHUNK_HDR_LEN;
|
||||
if (reconnect) {
|
||||
len = buf_append_printf(buf, size, len, "retry: %" PRIu32 "\r\n", reconnect);
|
||||
}
|
||||
if (id) {
|
||||
len = buf_append_printf(buf, size, len, "id: %" PRIu32 "\r\n", id);
|
||||
}
|
||||
if (event && *event) {
|
||||
len = buf_append_str(buf, size, len, "event: ");
|
||||
len = buf_append_str(buf, size, len, event);
|
||||
len = buf_append_str(buf, size, len, "\r\n");
|
||||
}
|
||||
if (with_data) {
|
||||
len = buf_append_str(buf, size, len, "data: ");
|
||||
}
|
||||
return len;
|
||||
}
|
||||
|
||||
void write_chunk_header(char *buf, size_t chunk_len) {
|
||||
// Eight lowercase hex digits; the temp keeps the terminator format_hex_to writes out of buf
|
||||
char digits[9];
|
||||
format_hex_to(digits, static_cast<uint32_t>(chunk_len));
|
||||
std::memcpy(buf, digits, 8);
|
||||
buf[8] = '\r';
|
||||
buf[9] = '\n';
|
||||
}
|
||||
|
||||
} // namespace esphome::web_server_idf
|
||||
@@ -17,7 +17,6 @@
|
||||
#include <freertos/task.h>
|
||||
|
||||
#include "utils.h"
|
||||
#include "sse_chunk.h"
|
||||
#include "web_server_idf.h"
|
||||
|
||||
#ifdef USE_WEBSERVER_AUTH_DIGEST
|
||||
@@ -30,17 +29,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 +52,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 +134,8 @@ 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);
|
||||
|
||||
// Attempt shutdown - ignore errors as socket may already be closed
|
||||
shutdown(sockfd, SHUT_RD);
|
||||
|
||||
@@ -736,224 +732,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 +827,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 +835,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 +854,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 +869,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 +882,60 @@ 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
|
||||
if (web_server_base::iovecs_total(iov, iovcnt) == 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;
|
||||
// Socket buffer full, try again later; a socket that stays full past the stall timeout is closed
|
||||
return this->stall_or_close_(LOG_STR("without send progress")) ? -1 : 0;
|
||||
}
|
||||
|
||||
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) {
|
||||
bool AsyncStreamResponse::stall_or_close_(const LogString *reason) {
|
||||
const uint32_t now = App.get_loop_component_start_time();
|
||||
if (!this->stall_.start_or_expired(now, SEND_STALL_TIMEOUT_MS)) {
|
||||
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;
|
||||
ESP_LOGW(TAG, "Closing stuck stream after %" PRIu32 " ms %s", this->stall_.elapsed(now), LOG_STR_ARG(reason));
|
||||
this->close();
|
||||
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);
|
||||
ssize_t AsyncStreamResponse::write(const void *data, size_t len) {
|
||||
struct iovec iov = {const_cast<void *>(data), len};
|
||||
return this->write(&iov, 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
|
||||
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_();
|
||||
}
|
||||
}
|
||||
|
||||
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;
|
||||
}
|
||||
|
||||
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
|
||||
|
||||
@@ -3,6 +3,7 @@
|
||||
|
||||
#include "esphome/core/defines.h"
|
||||
#include "esphome/core/helpers.h"
|
||||
#include "esphome/core/log.h"
|
||||
#include "esphome/core/string_ref.h"
|
||||
#include <esp_http_server.h>
|
||||
|
||||
@@ -15,21 +16,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 +185,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.
|
||||
@@ -269,204 +264,87 @@ 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_;
|
||||
// Starts the stall clock; once it has run SEND_STALL_TIMEOUT_MS, logs reason and closes. True when it closed.
|
||||
bool stall_or_close_(const LogString *reason);
|
||||
|
||||
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 +362,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)
|
||||
|
||||
|
||||
+6
-25
@@ -153,7 +153,6 @@
|
||||
#define USE_LINE_READER
|
||||
#define USE_LOCK
|
||||
#define USE_LOGGER
|
||||
#define USE_LOGGER_CONSOLE
|
||||
#define USE_LOGGER_LEVEL_LISTENERS
|
||||
#define USE_LOGGER_RUNTIME_TAG_LEVELS
|
||||
#define USE_LVGL
|
||||
@@ -768,26 +767,16 @@
|
||||
#if defined(USE_ESP32_VARIANT_ESP32S2)
|
||||
#define USE_LOGGER_USB_CDC
|
||||
#define USE_LOGGER_UART_SELECTION_USB_CDC
|
||||
#define ESPHOME_LOGGER_UART_NAME "USB_CDC"
|
||||
#define ESPHOME_LOGGER_UART_SELECTION logger::UART_SELECTION_USB_CDC
|
||||
#elif defined(USE_ESP32_VARIANT_ESP32H21)
|
||||
#define USE_LOGGER_USB_SERIAL_JTAG
|
||||
#define USE_LOGGER_UART_SELECTION_USB_SERIAL_JTAG
|
||||
#define ESPHOME_LOGGER_UART_NAME "USB_SERIAL_JTAG"
|
||||
#define ESPHOME_LOGGER_UART_SELECTION logger::UART_SELECTION_USB_SERIAL_JTAG
|
||||
#elif defined(USE_ESP32_VARIANT_ESP32C3) || defined(USE_ESP32_VARIANT_ESP32C5) || \
|
||||
defined(USE_ESP32_VARIANT_ESP32C6) || defined(USE_ESP32_VARIANT_ESP32C61) || defined(USE_ESP32_VARIANT_ESP32H2) || \
|
||||
defined(USE_ESP32_VARIANT_ESP32H4) || defined(USE_ESP32_VARIANT_ESP32P4) || defined(USE_ESP32_VARIANT_ESP32S3) || \
|
||||
defined(USE_ESP32_VARIANT_ESP32S31)
|
||||
defined(USE_ESP32_VARIANT_ESP32H21) || defined(USE_ESP32_VARIANT_ESP32H4) || defined(USE_ESP32_VARIANT_ESP32P4) || \
|
||||
defined(USE_ESP32_VARIANT_ESP32S3) || defined(USE_ESP32_VARIANT_ESP32S31)
|
||||
#define USE_LOGGER_USB_CDC
|
||||
#define USE_LOGGER_UART_SELECTION_USB_CDC
|
||||
#define USE_LOGGER_USB_SERIAL_JTAG
|
||||
#define USE_LOGGER_UART_SELECTION_USB_SERIAL_JTAG
|
||||
#define ESPHOME_LOGGER_UART_NAME "USB_SERIAL_JTAG"
|
||||
#define ESPHOME_LOGGER_UART_SELECTION logger::UART_SELECTION_USB_SERIAL_JTAG
|
||||
#else
|
||||
#define USE_LOGGER_UART_SELECTION_UART0
|
||||
#define ESPHOME_LOGGER_UART_NAME "UART0"
|
||||
#define ESPHOME_LOGGER_UART_SELECTION logger::UART_SELECTION_UART0
|
||||
#endif
|
||||
#endif
|
||||
|
||||
@@ -801,9 +790,6 @@
|
||||
#define USE_WIFI_SCAN_RESULTS_LOCK
|
||||
#define USE_ESP8266_LOGGER_SERIAL
|
||||
#define USE_ESP8266_LOGGER_SERIAL1
|
||||
#define USE_LOGGER_UART_SELECTION_UART0
|
||||
#define ESPHOME_LOGGER_UART_NAME "UART0"
|
||||
#define ESPHOME_LOGGER_UART_SELECTION logger::UART_SELECTION_UART0
|
||||
#define USE_ESP8266_PREFERENCES_FLASH
|
||||
#define ESP8266_PREFERENCES_FLASH_WORDS 128
|
||||
#define ESP8266_PREFERENCES_RTC_EBOOT_WORDS 32
|
||||
@@ -837,6 +823,7 @@
|
||||
#define USE_WEBSERVER_LOCAL
|
||||
#define USE_WEBSERVER_OTA
|
||||
#define USE_WEBSERVER_OTA_DISABLED
|
||||
#define WEB_SERVER_DEFAULT_HEADERS_COUNT 1
|
||||
#endif
|
||||
|
||||
// USE_RP2 is the canonical platform define for the RP2 chip family. The
|
||||
@@ -849,8 +836,6 @@
|
||||
#define USE_I2C
|
||||
#define USE_LOGGER_USB_CDC
|
||||
#define USE_LOGGER_UART_SELECTION_USB_CDC
|
||||
#define ESPHOME_LOGGER_UART_NAME "USB_CDC"
|
||||
#define ESPHOME_LOGGER_UART_SELECTION logger::UART_SELECTION_USB_CDC
|
||||
#define USE_SOCKET_IMPL_LWIP_TCP
|
||||
#define USE_RP2040_BLE
|
||||
#define USE_RP2_BLE_TRACKER
|
||||
@@ -875,15 +860,13 @@
|
||||
#define ESPHOME_WIFI_IP_STATE_LISTENERS 2
|
||||
#define USE_ETHERNET_IP_STATE_LISTENERS
|
||||
#define ESPHOME_ETHERNET_IP_STATE_LISTENERS 2
|
||||
#define WEB_SERVER_DEFAULT_HEADERS_COUNT 1
|
||||
#endif
|
||||
|
||||
#ifdef USE_LIBRETINY
|
||||
#if defined(USE_BK72XX) || defined(USE_RTL87XX) || defined(USE_LN882X)
|
||||
#define USE_LIBRETINY_CRASH_HANDLER
|
||||
#endif
|
||||
#define USE_LOGGER_UART_SELECTION_DEFAULT
|
||||
#define ESPHOME_LOGGER_UART_NAME "DEFAULT"
|
||||
#define ESPHOME_LOGGER_UART_SELECTION logger::UART_SELECTION_DEFAULT
|
||||
#ifdef USE_BK72XX
|
||||
#define USE_REMOTE_RECEIVER_TIMER_SAMPLING
|
||||
#endif
|
||||
@@ -914,6 +897,7 @@
|
||||
#define USE_WEBSERVER_AUTH_DIGEST
|
||||
#define USE_WEBSERVER_CAPTIVE
|
||||
#define USE_WEBSERVER_PORT 80 // NOLINT
|
||||
#define WEB_SERVER_DEFAULT_HEADERS_COUNT 1
|
||||
#define USE_ESPHOME_TASK_LOG_BUFFER
|
||||
#define ESPHOME_TASK_LOG_BUFFER_SIZE 768
|
||||
#endif
|
||||
@@ -938,11 +922,8 @@
|
||||
#define ESPHOME_TASK_LOG_BUFFER_SIZE 768
|
||||
#define USE_LOGGER_EARLY_MESSAGE
|
||||
#define USE_LOGGER_UART_SELECTION_USB_CDC
|
||||
#define ESPHOME_LOGGER_UART_NAME "USB_CDC"
|
||||
#define ESPHOME_LOGGER_UART_SELECTION logger::UART_SELECTION_USB_CDC
|
||||
#define USE_LOGGER_USB_CDC
|
||||
#define USE_LOGGER_WAIT_FOR_CDC
|
||||
#define USE_LOGGER_ZEPHYR_CDC_LOOP
|
||||
#define USE_NRF52_DFU
|
||||
#define USE_NRF52_REG0_VOUT 5
|
||||
#define USE_NRF52_UICR_ERASE
|
||||
|
||||
@@ -846,7 +846,9 @@ def convert_visual_temperatures(
|
||||
that needs ``full_config`` is left for final validation, which runs this again with it."""
|
||||
if (unit := entity_temperature_unit(config, full_config)) is None:
|
||||
return config
|
||||
visual = config[CONF_VISUAL]
|
||||
# A platform entry that only holds nested entities has no visual section of its own
|
||||
if (visual := config.get(CONF_VISUAL)) is None:
|
||||
return config
|
||||
convert_temperature_keys(visual, (CONF_MIN_TEMPERATURE, CONF_MAX_TEMPERATURE), unit)
|
||||
section = visual if delta_section is None else visual.get(delta_section)
|
||||
if section is not None:
|
||||
|
||||
@@ -222,6 +222,20 @@ def test_a_resolved_unit_waits_for_the_full_config() -> None:
|
||||
assert config[CONF_VISUAL][CONF_MIN_TEMPERATURE] == pytest.approx(59.0)
|
||||
|
||||
|
||||
def test_final_validate_skips_a_platform_that_only_holds_nested_climates() -> None:
|
||||
"""A hub platform may put each of its climates under a key of one platform entry."""
|
||||
nested = climate_schema(climate_ns.class_("NestedClimate", Climate))
|
||||
platform_schema = cv.Schema({cv.Optional("zone_1"): nested})
|
||||
config = platform_schema(
|
||||
{"zone_1": {CONF_NAME: "Zone 1", CONF_VISUAL: {CONF_MIN_TEMPERATURE: "50°F"}}}
|
||||
)
|
||||
# The platform entry itself is not a climate and has no visual section
|
||||
assert CONF_VISUAL not in config
|
||||
_final_validate([config])
|
||||
# The nested climate was converted by its own schema
|
||||
assert config["zone_1"][CONF_VISUAL][CONF_MIN_TEMPERATURE] == pytest.approx(10.0)
|
||||
|
||||
|
||||
@pytest.mark.parametrize(
|
||||
("use_fahrenheit", "cn105_lines"),
|
||||
[
|
||||
|
||||
@@ -68,6 +68,38 @@ def test_logger_pre_setup_before_other_components(generate_main):
|
||||
)
|
||||
|
||||
|
||||
def test_default_uart_selection_is_not_emitted(generate_main):
|
||||
"""UART0 is the C++ initializer on ESP8266, so the setter is skipped."""
|
||||
main_cpp = generate_main("tests/component_tests/logger/test_logger.yaml")
|
||||
|
||||
assert "set_uart_selection(" not in main_cpp
|
||||
|
||||
|
||||
def test_custom_uart_selection_is_emitted(generate_main):
|
||||
"""A non default UART still reaches the setter before pre_setup()."""
|
||||
main_cpp = generate_main("tests/component_tests/logger/test_logger_uart1.yaml")
|
||||
|
||||
assert "set_uart_selection(logger::UART_SELECTION_UART1);" in main_cpp
|
||||
|
||||
|
||||
def test_libretiny_default_uart_selection_is_not_emitted(generate_main):
|
||||
"""DEFAULT is the C++ initializer on LibreTiny, so the setter is skipped."""
|
||||
main_cpp = generate_main(
|
||||
"tests/component_tests/logger/test_logger_libretiny_default.yaml"
|
||||
)
|
||||
|
||||
assert "set_uart_selection(" not in main_cpp
|
||||
|
||||
|
||||
def test_libretiny_uart0_is_emitted(generate_main):
|
||||
"""UART0 is not the LibreTiny initializer, so it must still be set."""
|
||||
main_cpp = generate_main(
|
||||
"tests/component_tests/logger/test_logger_libretiny_uart0.yaml"
|
||||
)
|
||||
|
||||
assert "set_uart_selection(logger::UART_SELECTION_UART0);" in main_cpp
|
||||
|
||||
|
||||
def test_ram_log_strings_is_deprecated(
|
||||
generate_main: Callable[[str | Path], str],
|
||||
caplog: pytest.LogCaptureFixture,
|
||||
@@ -136,76 +168,35 @@ def test_log_v2_only_on_native_toolchain(
|
||||
|
||||
|
||||
@pytest.mark.parametrize(
|
||||
("config", "port", "zephyr_cdc_loop"),
|
||||
("config", "expected"),
|
||||
[
|
||||
pytest.param("test_logger.yaml", "UART0", False, id="esp8266_default"),
|
||||
pytest.param("test_logger_uart1.yaml", "UART1", False, id="esp8266_uart1"),
|
||||
pytest.param(
|
||||
"test_logger_uart0_swap.yaml", "UART0_SWAP", False, id="esp8266_uart0_swap"
|
||||
),
|
||||
pytest.param(
|
||||
"test_logger_libretiny_default.yaml",
|
||||
"DEFAULT",
|
||||
False,
|
||||
id="libretiny_default",
|
||||
),
|
||||
pytest.param(
|
||||
"test_logger_libretiny_uart0.yaml", "UART0", False, id="libretiny_uart0"
|
||||
),
|
||||
pytest.param(
|
||||
"test_logger_rp2_usb_cdc.yaml", "USB_CDC", False, id="rp2_usb_cdc"
|
||||
),
|
||||
pytest.param("test_logger_rp2_uart0.yaml", "UART0", False, id="rp2_uart0"),
|
||||
pytest.param(
|
||||
"test_logger_esp32_usb_cdc.yaml", "USB_CDC", False, id="esp32_usb_cdc"
|
||||
),
|
||||
pytest.param("test_logger_rp2_usb_cdc.yaml", {"USB_CDC"}, id="rp2_usb_cdc"),
|
||||
pytest.param("test_logger_rp2_uart0.yaml", set(), id="rp2_uart0"),
|
||||
pytest.param("test_logger_esp32_usb_cdc.yaml", {"USB_CDC"}, id="esp32_usb_cdc"),
|
||||
pytest.param(
|
||||
"test_logger_esp32_usb_serial_jtag.yaml",
|
||||
"USB_SERIAL_JTAG",
|
||||
False,
|
||||
{"USB_SERIAL_JTAG"},
|
||||
id="esp32_usb_serial_jtag",
|
||||
),
|
||||
pytest.param(
|
||||
"test_logger_nrf52_usb_cdc.yaml", "USB_CDC", True, id="nrf52_usb_cdc"
|
||||
),
|
||||
pytest.param("test_logger_nrf52_usb_cdc.yaml", {"USB_CDC"}, id="nrf52_usb_cdc"),
|
||||
# Zephyr polls the USB port only when it logs there
|
||||
pytest.param(
|
||||
"test_logger_nrf52_usb_cdc_no_serial.yaml",
|
||||
"USB_CDC",
|
||||
False,
|
||||
id="nrf52_no_serial",
|
||||
"test_logger_nrf52_usb_cdc_no_serial.yaml", set(), id="nrf52_no_serial"
|
||||
),
|
||||
],
|
||||
)
|
||||
def test_uart_selection_defines(
|
||||
generate_main: Callable[[str | Path], str],
|
||||
config: str,
|
||||
port: str,
|
||||
zephyr_cdc_loop: bool,
|
||||
expected: set[str],
|
||||
) -> None:
|
||||
"""The chosen port is fixed at build time: one flag, its enum value and its dump_config name."""
|
||||
"""USB ports get a selection define, which gates their port code and crash replay."""
|
||||
generate_main(f"tests/component_tests/logger/{config}")
|
||||
|
||||
defines = {d.name: d.value for d in CORE.defines}
|
||||
prefix = "USE_LOGGER_UART_SELECTION_"
|
||||
assert {d.removeprefix(prefix) for d in defines if d.startswith(prefix)} == {port}
|
||||
assert (
|
||||
str(defines["ESPHOME_LOGGER_UART_SELECTION"])
|
||||
== f"logger::UART_SELECTION_{port}"
|
||||
)
|
||||
assert str(defines["ESPHOME_LOGGER_UART_NAME"]) == f'"{port}"'
|
||||
assert ("USE_LOGGER_ZEPHYR_CDC_LOOP" in defines) is zephyr_cdc_loop
|
||||
|
||||
|
||||
def test_esp8266_without_serial_dumps_no_port(
|
||||
generate_main: Callable[[str | Path], str],
|
||||
) -> None:
|
||||
"""ESP8266 without serial logging keeps UART0 selected but never opens it."""
|
||||
generate_main("tests/component_tests/logger/test_logger_no_serial.yaml")
|
||||
|
||||
defines = {d.name: d.value for d in CORE.defines}
|
||||
assert "USE_LOGGER_UART_SELECTION_UART0" in defines
|
||||
assert str(defines["ESPHOME_LOGGER_UART_NAME"]) == '"NONE"'
|
||||
assert {
|
||||
d.name.removeprefix(prefix) for d in CORE.defines if d.name.startswith(prefix)
|
||||
} == expected
|
||||
|
||||
|
||||
def test_log_listener_slots_count_every_listener(
|
||||
|
||||
@@ -1,9 +0,0 @@
|
||||
---
|
||||
esphome:
|
||||
name: test
|
||||
|
||||
esp8266:
|
||||
board: d1_mini_lite
|
||||
|
||||
logger:
|
||||
baud_rate: 0
|
||||
@@ -1,9 +0,0 @@
|
||||
---
|
||||
esphome:
|
||||
name: test
|
||||
|
||||
esp8266:
|
||||
board: d1_mini_lite
|
||||
|
||||
logger:
|
||||
hardware_uart: UART0_SWAP
|
||||
@@ -8,11 +8,16 @@ from esphome.components.npm1300 import (
|
||||
CHARGER_SCHEMA,
|
||||
NPM1300_UNSET,
|
||||
OUTPUT_VOLTAGE,
|
||||
THERMISTOR_NONE,
|
||||
VBUS_CURRENT_LIMIT,
|
||||
_charger_config,
|
||||
_check_thermistor,
|
||||
_config,
|
||||
_output_config,
|
||||
_termination_voltage_code,
|
||||
_thermistor_resistance,
|
||||
_vbus_current_limit_code,
|
||||
thermistor_beta,
|
||||
)
|
||||
from esphome.const import CONF_MODE, CONF_VOLTAGE
|
||||
from esphome.cpp_generator import StructInitializer
|
||||
@@ -54,10 +59,35 @@ def test_termination_voltage_gap_rejected(voltage: str) -> None:
|
||||
CHARGER_SCHEMA({"charge_current": "100mA", "termination_voltage": voltage})
|
||||
|
||||
|
||||
def test_warm_termination_defaults_to_termination() -> None:
|
||||
def test_warm_termination_defaults_to_reset_value() -> None:
|
||||
fields = _charger({"charge_current": "100mA", "termination_voltage": "4.1V"})
|
||||
assert fields["termination_voltage"] == "6"
|
||||
assert fields["warm_termination_voltage"] == "6"
|
||||
assert fields["warm_termination_voltage"] == "2"
|
||||
|
||||
|
||||
@pytest.mark.parametrize(
|
||||
("config", "warns"),
|
||||
[
|
||||
({"termination_voltage": "4.2V"}, True),
|
||||
({"termination_voltage": "3.5V"}, False),
|
||||
({"termination_voltage": "3.6V"}, False),
|
||||
({}, False),
|
||||
({"termination_voltage": "4.2V", "warm_termination_voltage": "4.0V"}, False),
|
||||
],
|
||||
)
|
||||
def test_warm_termination_default_warning(
|
||||
config: ConfigType, warns: bool, caplog: pytest.LogCaptureFixture
|
||||
) -> None:
|
||||
CHARGER_SCHEMA(config)
|
||||
assert ("warm_termination_voltage" in caplog.text) is warns
|
||||
|
||||
|
||||
@pytest.mark.parametrize(("termination", "code"), [("3.5V", "0"), ("3.55V", "1")])
|
||||
def test_warm_termination_default_not_above_termination(
|
||||
termination: str, code: str
|
||||
) -> None:
|
||||
fields = _charger({"termination_voltage": termination})
|
||||
assert fields["warm_termination_voltage"] == code
|
||||
|
||||
|
||||
def test_warm_termination_code() -> None:
|
||||
@@ -112,3 +142,66 @@ def test_vbus_current_limit_code(limit: str, code: int) -> None:
|
||||
def test_vbus_current_limit_rejected(limit: str) -> None:
|
||||
with pytest.raises(cv.Invalid):
|
||||
VBUS_CURRENT_LIMIT(limit)
|
||||
|
||||
|
||||
def test_charger_needs_thermistor() -> None:
|
||||
with pytest.raises(cv.Invalid, match="thermistor_resistance"):
|
||||
_check_thermistor({"charger": {}})
|
||||
config = {"charger": {}, "thermistor_resistance": THERMISTOR_NONE}
|
||||
assert _check_thermistor(config) is config
|
||||
|
||||
|
||||
@pytest.mark.parametrize("resistance", [None, THERMISTOR_NONE])
|
||||
def test_thermistor_beta_needs_resistance(resistance: str | None) -> None:
|
||||
config = {"thermistor_beta": 3380}
|
||||
if resistance is not None:
|
||||
config["thermistor_resistance"] = resistance
|
||||
with pytest.raises(cv.Invalid, match="thermistor_beta"):
|
||||
_check_thermistor(config)
|
||||
|
||||
|
||||
@pytest.mark.parametrize(
|
||||
("resistance", "beta"), [(10000.0, 3380), (47000.0, 4050), (100000.0, 4250)]
|
||||
)
|
||||
def test_thermistor_beta_default(resistance: float, beta: int) -> None:
|
||||
assert thermistor_beta({"thermistor_resistance": resistance}) == beta
|
||||
|
||||
|
||||
def test_thermistor_beta_configured() -> None:
|
||||
config = {"thermistor_resistance": 47000.0, "thermistor_beta": 3950}
|
||||
assert thermistor_beta(config) == 3950
|
||||
|
||||
|
||||
@pytest.mark.parametrize(
|
||||
("resistance", "code", "beta"),
|
||||
[
|
||||
(None, str(NPM1300_UNSET), "0"),
|
||||
(THERMISTOR_NONE, "0", "0"),
|
||||
(100000.0, "3", "4250"),
|
||||
],
|
||||
)
|
||||
def test_config_thermistor(
|
||||
resistance: str | float | None, code: str, beta: str
|
||||
) -> None:
|
||||
config = {} if resistance is None else {"thermistor_resistance": resistance}
|
||||
fields = _fields(_config(config))
|
||||
assert fields["thermistor"] == code
|
||||
assert fields["thermistor_beta"] == beta
|
||||
|
||||
|
||||
@pytest.mark.parametrize(
|
||||
("value", "result"),
|
||||
[
|
||||
("10kOhm", 10000.0),
|
||||
("0.047MOhm", 47000.0),
|
||||
("100000", 100000.0),
|
||||
("None", "none"),
|
||||
],
|
||||
)
|
||||
def test_thermistor_resistance(value: str, result: float | str) -> None:
|
||||
assert _thermistor_resistance(value) == result
|
||||
|
||||
|
||||
def test_thermistor_resistance_rejected() -> None:
|
||||
with pytest.raises(cv.Invalid, match="10kOhm, 47kOhm, 100kOhm or none"):
|
||||
_thermistor_resistance("22kOhm")
|
||||
@@ -0,0 +1,6 @@
|
||||
from tests.testing_helpers import ComponentManifestOverride
|
||||
|
||||
|
||||
def override_manifest(manifest: ComponentManifestOverride) -> None:
|
||||
# touchscreen.h includes display.h
|
||||
manifest.dependencies = manifest.dependencies + ["display"]
|
||||
@@ -0,0 +1,42 @@
|
||||
#pragma once
|
||||
|
||||
#include <array>
|
||||
|
||||
#include <gtest/gtest.h>
|
||||
|
||||
#include "esphome/components/cst816/touchscreen/cst816_touchscreen.h"
|
||||
|
||||
namespace esphome::cst816::testing {
|
||||
|
||||
static constexpr uint8_t ADDRESS = 0x15;
|
||||
// The driver reads REG_STATUS onwards in one transfer: status, gesture, touch count, then two points.
|
||||
static constexpr size_t FRAME_SIZE = 13;
|
||||
|
||||
/// Serves one fixed status frame.
|
||||
class FrameBus final : public i2c::I2CBus {
|
||||
public:
|
||||
explicit FrameBus(std::array<uint8_t, FRAME_SIZE> frame) : frame_(frame) {}
|
||||
|
||||
i2c::ErrorCode write_readv(uint8_t address, const uint8_t *write_buffer, size_t write_count, uint8_t *read_buffer,
|
||||
size_t read_count) override {
|
||||
EXPECT_EQ(address, ADDRESS);
|
||||
for (size_t i = 0; i < read_count; i++)
|
||||
read_buffer[i] = i < FRAME_SIZE ? this->frame_[i] : 0;
|
||||
return i2c::ERROR_OK;
|
||||
}
|
||||
|
||||
protected:
|
||||
std::array<uint8_t, FRAME_SIZE> frame_;
|
||||
};
|
||||
|
||||
inline std::array<uint8_t, FRAME_SIZE> make_frame(uint8_t touch_num, uint16_t x = 0, uint16_t y = 0) {
|
||||
std::array<uint8_t, FRAME_SIZE> frame{};
|
||||
frame[REG_TOUCH_NUM] = touch_num;
|
||||
frame[REG_XPOS_HIGH] = (x >> 8) & 0x0F;
|
||||
frame[REG_XPOS_LOW] = x & 0xFF;
|
||||
frame[REG_YPOS_HIGH] = (y >> 8) & 0x0F;
|
||||
frame[REG_YPOS_LOW] = y & 0xFF;
|
||||
return frame;
|
||||
}
|
||||
|
||||
} // namespace esphome::cst816::testing
|
||||
@@ -0,0 +1,88 @@
|
||||
#include <gtest/gtest.h>
|
||||
|
||||
#include <memory>
|
||||
|
||||
#include "../common.h"
|
||||
|
||||
namespace esphome::cst816::testing {
|
||||
|
||||
class CST816TouchscreenTest : public ::testing::Test {
|
||||
protected:
|
||||
void feed_(const std::array<uint8_t, FRAME_SIZE> &frame) {
|
||||
this->bus_ = std::make_unique<FrameBus>(frame);
|
||||
this->touchscreen_.set_i2c_bus(this->bus_.get());
|
||||
this->touchscreen_.set_i2c_address(ADDRESS);
|
||||
this->touchscreen_.update_touches();
|
||||
}
|
||||
|
||||
std::unique_ptr<FrameBus> bus_;
|
||||
CST816Touchscreen touchscreen_;
|
||||
};
|
||||
|
||||
// An idle frame reports 0xFF in the touch count byte and 0xFFF coordinates. Masking 0xFF to the
|
||||
// low two bits turns that into three touches at the maximum position, a phantom tap.
|
||||
TEST_F(CST816TouchscreenTest, IdleAllOnesFrameIsIgnored) {
|
||||
this->feed_(make_frame(0xFF, 0x0FFF, 0x0FFF));
|
||||
EXPECT_TRUE(this->touchscreen_.get_touches().empty());
|
||||
}
|
||||
|
||||
TEST_F(CST816TouchscreenTest, ZeroTouchCountIsIgnored) {
|
||||
this->feed_(make_frame(0));
|
||||
EXPECT_TRUE(this->touchscreen_.get_touches().empty());
|
||||
}
|
||||
|
||||
// Three is the boundary the guard rejects: the old mask turned a count of three into three
|
||||
// touches, so this is the value whose behaviour changes.
|
||||
TEST_F(CST816TouchscreenTest, TouchCountThreeIsIgnored) {
|
||||
this->feed_(make_frame(3, 100, 200));
|
||||
EXPECT_TRUE(this->touchscreen_.get_touches().empty());
|
||||
}
|
||||
|
||||
// The count is the whole low nibble, so 0x05 must not be masked down to one touch by a 0x03 mask.
|
||||
TEST_F(CST816TouchscreenTest, TouchCountFiveIsIgnored) {
|
||||
this->feed_(make_frame(0x05, 100, 200));
|
||||
EXPECT_TRUE(this->touchscreen_.get_touches().empty());
|
||||
}
|
||||
|
||||
TEST_F(CST816TouchscreenTest, SingleTouchIsReported) {
|
||||
this->feed_(make_frame(1, 300, 100));
|
||||
auto touches = this->touchscreen_.get_touches();
|
||||
ASSERT_EQ(touches.size(), 1u);
|
||||
EXPECT_EQ(touches[0].x_raw, 300);
|
||||
EXPECT_EQ(touches[0].y_raw, 100);
|
||||
}
|
||||
|
||||
// CST826/CST836 can report two points; the driver only consumes the first one.
|
||||
TEST_F(CST816TouchscreenTest, TwoPointFrameReportsFirstPoint) {
|
||||
this->feed_(make_frame(2, 300, 100));
|
||||
auto touches = this->touchscreen_.get_touches();
|
||||
ASSERT_EQ(touches.size(), 1u);
|
||||
EXPECT_EQ(touches[0].x_raw, 300);
|
||||
EXPECT_EQ(touches[0].y_raw, 100);
|
||||
}
|
||||
|
||||
TEST_F(CST816TouchscreenTest, SentinelPositionIsIgnored) {
|
||||
this->feed_(make_frame(1, 0x0FFF, 100));
|
||||
EXPECT_TRUE(this->touchscreen_.get_touches().empty());
|
||||
}
|
||||
|
||||
// The sentinel is checked on both axes; a valid X with Y=0xFFF is still an idle reading.
|
||||
TEST_F(CST816TouchscreenTest, SentinelYPositionIsIgnored) {
|
||||
this->feed_(make_frame(1, 100, 0x0FFF));
|
||||
EXPECT_TRUE(this->touchscreen_.get_touches().empty());
|
||||
}
|
||||
|
||||
// x_raw_max_/y_raw_max_ are calibration endpoints, not validity limits: the base class applies them
|
||||
// after swapping axes and clamps readings outside them. A touch beyond the calibrated maximum is
|
||||
// still a real touch and must reach the base class.
|
||||
TEST_F(CST816TouchscreenTest, CalibrationBoundsDoNotDropTouches) {
|
||||
this->touchscreen_.set_calibration(0, 240, 0, 320);
|
||||
this->touchscreen_.set_swap_xy(true);
|
||||
this->feed_(make_frame(1, 300, 100));
|
||||
auto touches = this->touchscreen_.get_touches();
|
||||
ASSERT_EQ(touches.size(), 1u);
|
||||
EXPECT_EQ(touches[0].x_raw, 100);
|
||||
EXPECT_EQ(touches[0].y_raw, 300);
|
||||
}
|
||||
|
||||
} // namespace esphome::cst816::testing
|
||||
@@ -1,29 +1,38 @@
|
||||
npm1300:
|
||||
id: pmic
|
||||
i2c_id: i2c_bus
|
||||
vbus_current_limit: 500mA
|
||||
thermistor_resistance: 10kOhm
|
||||
thermistor_beta: 3380
|
||||
charger:
|
||||
charge_current: 150mA
|
||||
termination_voltage: 4.2V
|
||||
warm_termination_voltage: 4.0V
|
||||
termination_current: 20%
|
||||
trickle_voltage: 2.5V
|
||||
discharge_current_limit: 1000mA
|
||||
recharge: true
|
||||
buck2:
|
||||
voltage: 3.3V
|
||||
ldo1:
|
||||
voltage: 3.3V
|
||||
mode: ldo
|
||||
enabled: true
|
||||
ldo2:
|
||||
mode: load_switch
|
||||
enabled: false
|
||||
- id: pmic
|
||||
i2c_id: i2c_bus
|
||||
vbus_current_limit: 500mA
|
||||
thermistor_resistance: 10kOhm
|
||||
thermistor_beta: 3380
|
||||
charger:
|
||||
charge_current: 150mA
|
||||
termination_voltage: 4.2V
|
||||
warm_termination_voltage: 4.0V
|
||||
termination_current: 20%
|
||||
trickle_voltage: 2.5V
|
||||
discharge_current_limit: 1000mA
|
||||
recharge: true
|
||||
buck2:
|
||||
voltage: 3.3V
|
||||
ldo1:
|
||||
voltage: 3.3V
|
||||
mode: ldo
|
||||
enabled: true
|
||||
ldo2:
|
||||
mode: load_switch
|
||||
enabled: false
|
||||
# A battery without a thermistor
|
||||
- id: pmic_no_ntc
|
||||
i2c_id: i2c_bus
|
||||
address: 0x6C
|
||||
thermistor_resistance: none
|
||||
charger:
|
||||
termination_voltage: 3.5V
|
||||
enabled: true
|
||||
|
||||
sensor:
|
||||
- platform: npm1300
|
||||
npm1300_id: pmic
|
||||
battery_voltage:
|
||||
name: Battery Voltage
|
||||
battery_current:
|
||||
@@ -41,6 +50,7 @@ sensor:
|
||||
|
||||
binary_sensor:
|
||||
- platform: npm1300
|
||||
npm1300_id: pmic
|
||||
vbus_present:
|
||||
name: VBUS Present
|
||||
vbus_current_limit_active:
|
||||
@@ -54,11 +64,13 @@ binary_sensor:
|
||||
|
||||
text_sensor:
|
||||
- platform: npm1300
|
||||
npm1300_id: pmic
|
||||
charger_status:
|
||||
name: Charger Status
|
||||
|
||||
switch:
|
||||
- platform: npm1300
|
||||
npm1300_id: pmic
|
||||
charger:
|
||||
name: Charger
|
||||
buck1:
|
||||
@@ -67,6 +79,10 @@ switch:
|
||||
name: LDO1
|
||||
ldo2:
|
||||
name: LDO2
|
||||
- platform: npm1300
|
||||
npm1300_id: pmic_no_ntc
|
||||
charger:
|
||||
name: Charger Without Thermistor
|
||||
|
||||
button:
|
||||
- platform: template
|
||||
|
||||
@@ -0,0 +1,2 @@
|
||||
packages:
|
||||
web_server: !include common_v3.yaml
|
||||
@@ -0,0 +1,2 @@
|
||||
packages:
|
||||
web_server: !include common_v3.yaml
|
||||
@@ -0,0 +1,2 @@
|
||||
packages:
|
||||
web_server: !include common_v3.yaml
|
||||
@@ -0,0 +1,105 @@
|
||||
#ifdef USE_HOST
|
||||
#include <gtest/gtest.h>
|
||||
|
||||
#include <sys/uio.h>
|
||||
|
||||
#include <cstdint>
|
||||
#include <string>
|
||||
#include <vector>
|
||||
|
||||
#include "esphome/components/web_server_base/stream_head.h"
|
||||
|
||||
namespace esphome::web_server::testing {
|
||||
|
||||
using namespace web_server_base; // NOLINT(google-build-using-namespace)
|
||||
|
||||
// Records each add() and send() like an AsyncClient whose send buffer holds space bytes
|
||||
struct FakeClient {
|
||||
size_t space{SIZE_MAX};
|
||||
std::string bytes;
|
||||
std::vector<uint8_t> flags;
|
||||
int sends{0};
|
||||
|
||||
size_t add(const char *data, size_t len, uint8_t apiflags) {
|
||||
this->flags.push_back(apiflags);
|
||||
const size_t taken = std::min(len, this->space);
|
||||
this->bytes.append(data, taken);
|
||||
this->space -= taken;
|
||||
return taken;
|
||||
}
|
||||
bool send() {
|
||||
this->sends++;
|
||||
return true;
|
||||
}
|
||||
};
|
||||
|
||||
static std::vector<struct iovec> pieces(std::vector<std::string> &parts) {
|
||||
std::vector<struct iovec> iov;
|
||||
for (auto &part : parts) {
|
||||
iov.push_back({part.data(), part.size()});
|
||||
}
|
||||
return iov;
|
||||
}
|
||||
|
||||
TEST(AddIovecs, CoalescesPiecesIntoOneAdd) {
|
||||
std::vector<std::string> parts{"abc", "de", "fghij"};
|
||||
auto iov = pieces(parts);
|
||||
FakeClient client;
|
||||
char scratch[64];
|
||||
EXPECT_EQ(add_iovecs(client, iov.data(), static_cast<int>(iov.size()), scratch, sizeof(scratch)), 10u);
|
||||
EXPECT_EQ(client.bytes, "abcdefghij");
|
||||
ASSERT_EQ(client.flags.size(), 1u);
|
||||
EXPECT_EQ(client.flags[0], STREAM_ADD_FLAG_COPY);
|
||||
EXPECT_EQ(client.sends, 1);
|
||||
}
|
||||
|
||||
TEST(AddIovecs, SplitsAtScratchSizeWithMoreOnAllButLast) {
|
||||
std::vector<std::string> parts{"0123456789", "abcdefghij", "XYZ"};
|
||||
auto iov = pieces(parts);
|
||||
FakeClient client;
|
||||
char scratch[8];
|
||||
EXPECT_EQ(add_iovecs(client, iov.data(), static_cast<int>(iov.size()), scratch, sizeof(scratch)), 23u);
|
||||
EXPECT_EQ(client.bytes, "0123456789abcdefghijXYZ");
|
||||
ASSERT_EQ(client.flags.size(), 3u);
|
||||
EXPECT_EQ(client.flags[0], STREAM_ADD_FLAG_COPY | STREAM_ADD_FLAG_MORE);
|
||||
EXPECT_EQ(client.flags[1], STREAM_ADD_FLAG_COPY | STREAM_ADD_FLAG_MORE);
|
||||
EXPECT_EQ(client.flags[2], STREAM_ADD_FLAG_COPY);
|
||||
EXPECT_EQ(client.sends, 1);
|
||||
}
|
||||
|
||||
TEST(AddIovecs, StopsAtPartialAccept) {
|
||||
std::vector<std::string> parts{"0123456789", "abcdefghij"};
|
||||
auto iov = pieces(parts);
|
||||
FakeClient client;
|
||||
client.space = 12;
|
||||
char scratch[8];
|
||||
EXPECT_EQ(add_iovecs(client, iov.data(), static_cast<int>(iov.size()), scratch, sizeof(scratch)), 12u);
|
||||
EXPECT_EQ(client.bytes, "0123456789ab");
|
||||
EXPECT_EQ(client.flags.size(), 2u); // nothing offered after the short add()
|
||||
EXPECT_EQ(client.sends, 1);
|
||||
}
|
||||
|
||||
TEST(AddIovecs, ZeroSpaceSendsNothing) {
|
||||
std::vector<std::string> parts{"abc"};
|
||||
auto iov = pieces(parts);
|
||||
FakeClient client;
|
||||
client.space = 0;
|
||||
char scratch[8];
|
||||
EXPECT_EQ(add_iovecs(client, iov.data(), static_cast<int>(iov.size()), scratch, sizeof(scratch)), 0u);
|
||||
EXPECT_EQ(client.flags.size(), 1u);
|
||||
EXPECT_EQ(client.sends, 0);
|
||||
}
|
||||
|
||||
TEST(AddIovecs, EmptyPiecesAreSkipped) {
|
||||
std::vector<std::string> parts{"", "ab", "", "c"};
|
||||
auto iov = pieces(parts);
|
||||
FakeClient client;
|
||||
char scratch[8];
|
||||
EXPECT_EQ(add_iovecs(client, iov.data(), static_cast<int>(iov.size()), scratch, sizeof(scratch)), 3u);
|
||||
EXPECT_EQ(client.bytes, "abc");
|
||||
ASSERT_EQ(client.flags.size(), 1u);
|
||||
EXPECT_EQ(client.flags[0], STREAM_ADD_FLAG_COPY);
|
||||
}
|
||||
|
||||
} // namespace esphome::web_server::testing
|
||||
#endif // USE_HOST
|
||||
+77
-3
@@ -1,14 +1,15 @@
|
||||
#include <gtest/gtest.h>
|
||||
|
||||
#include <algorithm>
|
||||
#include <cstdint>
|
||||
#include <cstdio>
|
||||
#include <cstring>
|
||||
#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 +129,77 @@ TEST(SseChunk, MatchesReferenceOnRandomMessages) {
|
||||
}
|
||||
}
|
||||
|
||||
} // namespace esphome::web_server_idf::testing
|
||||
// Queues one event behind the unsent bytes of src as append_chunk_() does; returns the new length
|
||||
static size_t queue_event(uint8_t *dst, const uint8_t *src, size_t len, size_t sent, const std::string &message,
|
||||
const char *event) {
|
||||
char prefix[128];
|
||||
const size_t prefix_len = build_chunk_prefix(prefix, sizeof(prefix), event, 0, 0, true);
|
||||
size_t total = prefix_len;
|
||||
for_each_chunk_piece(
|
||||
message.data(), message.size(), [](void *ctx, const char *, size_t n) { *static_cast<size_t *>(ctx) += n; },
|
||||
&total);
|
||||
write_chunk_header(prefix, total - CHUNK_HDR_LEN - CHUNK_END_LEN);
|
||||
return queue_chunk(dst, src, len, sent, prefix, prefix_len, message.data(), message.size());
|
||||
}
|
||||
|
||||
static std::string as_string(const uint8_t *buf, size_t len) { return {reinterpret_cast<const char *>(buf), len}; }
|
||||
|
||||
TEST(SseChunk, CopyChunkMatchesBuiltChunk) {
|
||||
const std::string want = build_chunk("a\nb", 3, "log", 0, 0);
|
||||
char prefix[128];
|
||||
const size_t prefix_len = build_chunk_prefix(prefix, sizeof(prefix), "log", 0, 0, true);
|
||||
std::memcpy(prefix, want.data(), CHUNK_HDR_LEN); // the header build_chunk() filled in
|
||||
uint8_t buf[128];
|
||||
EXPECT_EQ(copy_chunk(buf, prefix, prefix_len, "a\nb", 3), want.size());
|
||||
EXPECT_EQ(as_string(buf, want.size()), want);
|
||||
}
|
||||
|
||||
TEST(SseChunk, QueueIntoEmptyBuffer) {
|
||||
const std::string want = build_chunk("a\nb", 3, "log", 0, 0);
|
||||
uint8_t buf[128];
|
||||
EXPECT_EQ(queue_event(buf, buf, 0, 0, "a\nb", "log"), want.size());
|
||||
EXPECT_EQ(as_string(buf, want.size()), want);
|
||||
}
|
||||
|
||||
TEST(SseChunk, QueueBehindPartlySentChunkCompactsInPlace) {
|
||||
const std::string first = build_chunk(R"({"name":"ping"})", 15, "ping", 0, 0);
|
||||
const std::string second = build_chunk(R"({"name":"group"})", 16, "sorting_group", 0, 0);
|
||||
uint8_t buf[256];
|
||||
std::copy(first.begin(), first.end(), buf);
|
||||
const size_t sent = 7; // part of the first chunk is already on the wire
|
||||
const size_t len = queue_event(buf, buf, first.size(), sent, R"({"name":"group"})", "sorting_group");
|
||||
EXPECT_EQ(as_string(buf, len), first.substr(sent) + second);
|
||||
}
|
||||
|
||||
TEST(SseChunk, QueueIntoGrownBufferKeepsOrder) {
|
||||
const std::string first = build_chunk("first", 5, "log", 0, 0);
|
||||
uint8_t old_buf[64];
|
||||
std::copy(first.begin(), first.end(), old_buf);
|
||||
uint8_t grown[256];
|
||||
size_t len = queue_event(grown, old_buf, first.size(), 3, "second", "log");
|
||||
len = queue_event(grown, grown, len, 0, "third\nline", "log");
|
||||
EXPECT_EQ(as_string(grown, len),
|
||||
first.substr(3) + build_chunk("second", 6, "log", 0, 0) + build_chunk("third\nline", 10, "log", 0, 0));
|
||||
}
|
||||
|
||||
TEST(SseChunk, QueueAfterFirstChunkFullySent) {
|
||||
const std::string first = build_chunk("x", 1, "log", 0, 0);
|
||||
uint8_t buf[128];
|
||||
std::copy(first.begin(), first.end(), buf);
|
||||
const size_t len = queue_event(buf, buf, first.size(), first.size(), "y", "log");
|
||||
EXPECT_EQ(as_string(buf, len), build_chunk("y", 1, "log", 0, 0));
|
||||
}
|
||||
|
||||
TEST(SseChunk, TailQueueCap) {
|
||||
constexpr size_t max = 1460;
|
||||
EXPECT_EQ(tail_queue_cap(max + 1, 5000, max), 0u); // refused even when a large state grew the tail
|
||||
EXPECT_EQ(tail_queue_cap(100, 300, max), 300u); // fits in place
|
||||
EXPECT_EQ(tail_queue_cap(300, 300, max), 300u);
|
||||
EXPECT_EQ(tail_queue_cap(301, 300, max), max / 2); // first growth: half
|
||||
EXPECT_EQ(tail_queue_cap(max / 2, 0, max), max / 2);
|
||||
EXPECT_EQ(tail_queue_cap(max / 2 + 1, max / 2, max), max); // then all of it
|
||||
EXPECT_EQ(tail_queue_cap(max, max / 2, max), max);
|
||||
EXPECT_EQ(tail_queue_cap(max, 2048, max), 2048u); // a larger tail is used as is
|
||||
}
|
||||
|
||||
} // namespace esphome::web_server::testing
|
||||
@@ -0,0 +1,316 @@
|
||||
#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);
|
||||
}
|
||||
|
||||
TEST(StallClock, StartOrExpiredStartsOnceAndExpiresAtTheTimeout) {
|
||||
StallClock clock;
|
||||
EXPECT_FALSE(clock.start_or_expired(1000, TIMEOUT)); // first call starts the clock
|
||||
EXPECT_TRUE(clock.is_running());
|
||||
EXPECT_FALSE(clock.start_or_expired(1000 + TIMEOUT - 1, TIMEOUT)); // a later call keeps the start
|
||||
EXPECT_TRUE(clock.start_or_expired(1000 + TIMEOUT, TIMEOUT));
|
||||
EXPECT_EQ(clock.elapsed(1000 + TIMEOUT), TIMEOUT);
|
||||
}
|
||||
|
||||
TEST(IovecsTotal, SumsEveryElementIncludingEmptyOnes) {
|
||||
char a[3], b[5];
|
||||
struct iovec iov[] = {{a, sizeof(a)}, {nullptr, 0}, {b, sizeof(b)}};
|
||||
EXPECT_EQ(iovecs_total(iov, 3), 8U);
|
||||
EXPECT_EQ(iovecs_total(iov, 0), 0U);
|
||||
}
|
||||
|
||||
} // namespace esphome::web_server::testing
|
||||
|
||||
#endif // USE_HOST
|
||||
@@ -17,6 +17,7 @@ void setup() {
|
||||
App.pre_setup(name, sizeof(name) - 1, friendly_name, sizeof(friendly_name) - 1);
|
||||
auto *log = new logger::Logger(115200); // NOLINT
|
||||
log->pre_setup();
|
||||
log->set_uart_selection(logger::UART_SELECTION_UART0);
|
||||
App.register_component_(log);
|
||||
|
||||
auto *wifi = new wifi::WiFiComponent(); // NOLINT
|
||||
|
||||
@@ -1550,6 +1550,15 @@ def test_convert_visual_temperatures(
|
||||
convert_visual_temperatures({"visual": {}}, delta_keys, delta_section)
|
||||
|
||||
|
||||
def test_convert_visual_temperatures_without_a_visual_section() -> None:
|
||||
"""A platform entry that only holds nested entities has no visual section and is left alone."""
|
||||
nested = {"visual": {"min_temperature": (50.0, "°F")}}
|
||||
config = {"zone_1": nested}
|
||||
|
||||
assert convert_visual_temperatures(config, ("step",)) is config
|
||||
assert nested["visual"]["min_temperature"] == (50.0, "°F")
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_setup_entity_integrations_registers_mqtt_and_web_server(
|
||||
monkeypatch: pytest.MonkeyPatch, setup_test_environment: list[str]
|
||||
|
||||
@@ -1792,7 +1792,13 @@ def test_ccache_env_opt_in_without_binary(
|
||||
# Explicit IDF_CCACHE_ENABLE=1 forces it on; without a usable binary
|
||||
# IDF's CMake silently skips ccache, so this branch must say so out loud.
|
||||
p1, p2, p3 = _ccache_patches(tmp_path, None, tmp_path / "build")
|
||||
with patch.dict("os.environ", {"IDF_CCACHE_ENABLE": "1"}, clear=True), p1, p2, p3:
|
||||
with (
|
||||
patch.dict("os.environ", {"IDF_CCACHE_ENABLE": "1"}, clear=True),
|
||||
patch("esphome.espidf.framework.shutil.which", return_value=None),
|
||||
p1,
|
||||
p2,
|
||||
p3,
|
||||
):
|
||||
env = _ccache_env()
|
||||
assert env["IDF_CCACHE_ENABLE"] == "1"
|
||||
assert env["CCACHE_DIR"] == str(tmp_path / "tools" / "ccache")
|
||||
|
||||
Reference in new issue
Block a user