Compare commits

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

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

No files matched your search

+1 -1
View File
@@ -120,7 +120,7 @@ helper; `AGENTS.md` section 4 and the `component-codegen` skill have the detail.
| hand-written `set_x_sensor()` plus `x_sensor_` member | `SUB_SENSOR(x)`, `SUB_BINARY_SENSOR`, `SUB_TEXT_SENSOR`, `SUB_SWITCH`, ... |
| `if CONF_X in config: s = await sensor.new_sensor(config[CONF_X])` plus setter | `sensors = sensor.sub_sensors(config)` then `await sensors(CONF_X, hub.set_x_sensor)` |
| hand-written `{"a": MyEnum.MY_ENUM_A, ...}` dict | `MyEnum.options("a", "b", prefix="MY_ENUM_")` |
| loops on `available()` / `read_byte()` | UART `read_available(on_byte)`, `discard_available()`, `LineReader<N>`, `FrameReader<N, HEADER...>`, `LengthFrameReader<N, FrameFormat<...>...>` |
| loops on `available()` / `read_byte()` | UART `read_available(on_byte)`, `discard_available()`, `LineReader<N>`, `FrameReader<N, HEADER...>` |
| hand-rolled CRC, checksum or bit loop | `crc8`, `crc16`, `xor_bytes`, `sum16_bytes`, `encode_uint16`, ... (`esphome/core/helpers.h`) |
| integer split into buffer bytes by hand (`buf[0] = v >> 8; buf[1] = v;`) | `put_be16`/`24`/`32`, `put_le16`, `put_le32` (`esphome/core/helpers.h`) |
| bare `mark_failed()`, or `ESP_LOGE` then `mark_failed()` | `this->mark_failed(LOG_STR("reason"))` or `LOG_STR(ESP_LOG_MSG_COMM_FAIL)` |
+1 -3
View File
@@ -246,9 +246,7 @@ file does, and it is the authority when they disagree. The most useful starting
`time::decode_bcd_calendar<LAYOUT>` and `encode_bcd_calendar<LAYOUT>` with `EPSON_BCD_CALENDAR`,
`DS_BCD_CALENDAR` or `PCF_BCD_CALENDAR`.
* **Bus helpers:** UART drains with `read_available(on_byte)` and `discard_available()`, text lines with
`LineReader<N>`, fixed-length binary frames with `FrameReader<N, HEADER...>`, frames that give their own length
with `LengthFrameReader<N, FrameFormat<FrameHeader<...>, PayloadLengthU16LE<LengthField{...}>>...>` fed by
`read_frames(reader, handler, now, last_rx)`, which frees frames a bad length holds; I2C has `update_register`,
`LineReader<N>`, fixed-length binary frames with `FrameReader<N, HEADER...>`; I2C has `update_register`,
`update_register16`, `read_byte_reg16` and `write_byte_reg16` for 16-bit register addresses,
`write_registers` (PROGMEM `I2CRegValue` table), `check_chip_id` and `read_bytes_16_le`.
`if (!this->status_comm_result(ok)) return;` sets or clears the warning; an earlier step of a multi-step
@@ -2,6 +2,7 @@
#include <algorithm>
#include <cmath>
#include <cstring>
#include <iterator>
#include "esphome/core/application.h"
@@ -13,6 +14,13 @@ namespace esphome::airton_serial {
ESPHOME_LOG_TAG(TAG, "airton_serial.climate");
static constexpr uint8_t SYNC_BYTE = 0x7A;
static constexpr uint8_t LENGTH_POS = 4;
static constexpr uint8_t OPCODE_POS = 7;
static constexpr uint8_t MIN_FRAME_LEN = OPCODE_POS + 1 + 2; // Up to the opcode, then the CRC
// A partial frame left this long without a new byte is not a frame start.
static constexpr uint32_t FRAME_GAP_MS = 50;
static constexpr uint8_t ADDR_MODULE = 0x21;
static constexpr uint8_t ADDR_DISPLAY = 0xD1;
static constexpr uint8_t ADDR_BOARD = 0xD5;
@@ -113,15 +121,58 @@ void AirtonSerialClimate::setup() {
void AirtonSerialClimate::loop() {
const uint32_t now = App.get_loop_component_start_time();
auto accept = [this](const uint8_t *frame, size_t len) { return this->handle_frame_(frame, len); };
this->read_frames(this->reader_, accept, now, this->last_rx_ms_);
bool received = false;
this->read_available([this, &received](uint8_t byte) {
received = true;
this->handle_byte_(byte);
});
if (received) {
this->last_rx_ms_ = now;
} else if (this->rx_pos_ > 0 && now - this->last_rx_ms_ >= FRAME_GAP_MS) {
// A corrupt length can hold back complete frames behind it; look for them in what is buffered.
while (this->rx_pos_ > 0)
this->drop_first_byte_();
}
}
bool AirtonSerialClimate::handle_frame_(const uint8_t *data, size_t len) {
if (crc16(data, len - 2) != encode_uint16(data[len - 2], data[len - 1])) {
ESP_LOGW(TAG, "Bad CRC on %u-byte frame", static_cast<unsigned>(len));
return false;
void AirtonSerialClimate::handle_byte_(uint8_t byte) {
// Bytes are checked as they arrive, so rx_pos_ stays below the longest frame length.
this->rx_buffer_[this->rx_pos_++] = byte;
this->scan_buffer_();
}
void AirtonSerialClimate::drop_first_byte_() {
this->rx_pos_--;
std::memmove(this->rx_buffer_.data(), this->rx_buffer_.data() + 1, this->rx_pos_);
this->scan_buffer_();
}
void AirtonSerialClimate::scan_buffer_() {
const uint8_t *buf = this->rx_buffer_.data();
while (this->rx_pos_ > 0) {
uint8_t drop = 1;
if (buf[0] == SYNC_BYTE && (this->rx_pos_ < 2 || buf[1] == SYNC_BYTE)) {
if (this->rx_pos_ <= LENGTH_POS)
return;
const uint8_t len = buf[LENGTH_POS];
if (len >= MIN_FRAME_LEN && len <= MAX_FRAME_LEN) {
if (this->rx_pos_ < len)
return;
if (crc16(buf, len - 2) == encode_uint16(buf[len - 2], buf[len - 1])) {
this->handle_frame_(buf, len);
drop = len;
} else {
ESP_LOGW(TAG, "Bad CRC on %u-byte frame", len);
}
}
}
// Not a frame start, or a bad frame: look for the next start after its first byte.
this->rx_pos_ -= drop;
std::memmove(this->rx_buffer_.data(), buf + drop, this->rx_pos_);
}
}
void AirtonSerialClimate::handle_frame_(const uint8_t *data, size_t len) {
const uint8_t opcode = data[OPCODE_POS];
#if ESPHOME_LOG_LEVEL >= ESPHOME_LOG_LEVEL_VERBOSE
if (opcode == OP_A3_STATE || opcode == OP_AC_POLL) {
@@ -130,7 +181,7 @@ bool AirtonSerialClimate::handle_frame_(const uint8_t *data, size_t len) {
}
#endif
if (data[3] != ADDR_MODULE)
return true;
return;
if (data[2] == ADDR_BOARD) {
if (len == 34 && opcode == OP_A3_STATE) {
this->handle_a3_(data);
@@ -140,7 +191,6 @@ bool AirtonSerialClimate::handle_frame_(const uint8_t *data, size_t len) {
} else if (data[2] == ADDR_DISPLAY && len == 13 && opcode == OP_A4_ACK) {
ESP_LOGD(TAG, "A4 acknowledgement from display");
}
return true;
}
void AirtonSerialClimate::handle_a3_(const uint8_t *data) {
@@ -319,14 +369,11 @@ void AirtonSerialClimate::send_a5_() {
void AirtonSerialClimate::send_ab_() {
uint8_t frame[sizeof(AB_FRAME)];
progmem_memcpy(frame, AB_FRAME, sizeof(AB_FRAME));
this->reader_.drop_kept();
this->write_array(frame, sizeof(frame));
}
void AirtonSerialClimate::write_frame_(uint8_t *frame, size_t len) {
put_be16(frame + len - 2, crc16(frame, len - 2));
// Kept bytes of an earlier frame must not merge into the reply
this->reader_.drop_kept();
this->write_array(frame, len);
}
@@ -1,12 +1,12 @@
#pragma once
#include <array>
#include <cstddef>
#include <cstdint>
#include "esphome/components/climate/climate.h"
#include "esphome/components/uart/uart.h"
#include "esphome/core/component.h"
#include "esphome/core/frame_reader.h"
#ifdef USE_SENSOR
#include "esphome/components/sensor/sensor_with_dedup.h"
@@ -24,9 +24,6 @@ static constexpr uint8_t FLAG_HEALTH = 0x10;
static constexpr uint8_t FLAG_SLEEP = 0x02;
static constexpr uint8_t FLAG_ECO = 0x01;
static constexpr uint8_t SYNC_BYTE = 0x7A;
static constexpr uint8_t LENGTH_POS = 4;
static constexpr uint8_t OPCODE_POS = 7;
static constexpr uint8_t MAX_FRAME_LEN = 64;
/// Climate on the Airton serial bus. The ESP takes the place of the original Wi-Fi module (address 0x21):
@@ -64,17 +61,16 @@ class AirtonSerialClimate final : public uart::UARTDevice, public climate::Clima
bool can_send_command_();
void send_command_();
void write_frame_(uint8_t *frame, size_t len);
/// Returns false for a frame that fails the CRC
bool handle_frame_(const uint8_t *data, size_t len);
void handle_byte_(uint8_t byte);
void drop_first_byte_();
void scan_buffer_();
void handle_frame_(const uint8_t *data, size_t len);
void handle_a3_(const uint8_t *data);
void handle_ac_(const uint8_t *data);
void publish_settings_();
// 7A 7A, ..., whole-frame length at 4, opcode, ..., CRC (2); the shortest runs up to the opcode
LengthFrameReader<MAX_FRAME_LEN,
FrameFormat<FrameHeader<SYNC_BYTE, SYNC_BYTE>, FrameLengthU8<LengthField{.offset = LENGTH_POS}>,
FrameMinLength<OPCODE_POS + 3>>>
reader_;
std::array<uint8_t, MAX_FRAME_LEN> rx_buffer_;
uint8_t rx_pos_{0};
uint8_t flags_{FLAG_DISPLAY};
bool state_received_{false};
uint8_t swing_{0}; // Raw swing byte
+88 -41
View File
@@ -5,7 +5,6 @@
#include "esphome/core/log.h"
#include "esphome/core/progmem.h"
#include <array>
#include <cinttypes>
#include <cmath>
@@ -26,6 +25,8 @@ static constexpr uint32_t APC1_COLD_BOOT_INIT_DELAY_MS = 2000;
static constexpr uint32_t APC1_INIT_RETRY_INTERVAL_MS = 10000;
// Active mode streams a frame every second; this long without one means the sensor stopped
static constexpr uint32_t APC1_NO_DATA_TIMEOUT_MS = 15000;
// Drop a partial frame when the bytes stop mid-way
static constexpr uint32_t APC1_RX_IDLE_TIMEOUT_MS = 200;
void APC1Component::setup() {
if (this->set_pin_ != nullptr) {
@@ -98,40 +99,80 @@ void APC1Component::loop() {
}
}
auto handle = [this](const uint8_t *frame, size_t len) { return this->parse_frame_(frame, len); };
this->read_frames(this->reader_, handle, now, this->last_transmission_);
if (this->rx_index_ > 0 && now - this->last_transmission_ > APC1_RX_IDLE_TIMEOUT_MS) {
ESP_LOGV(TAG, "Timed out waiting for data, resetting buffer");
this->rx_index_ = 0;
}
this->read_available([this, now](uint8_t byte) {
this->last_transmission_ = now;
if (this->rx_index_ == 0) {
if (byte == APC1_HEADER_1) {
this->rx_buffer_[this->rx_index_++] = byte;
}
return;
}
if (this->rx_index_ == 1) {
if (byte == APC1_HEADER_2) {
this->rx_buffer_[this->rx_index_++] = byte;
} else if (byte != APC1_HEADER_1) {
this->rx_index_ = 0;
}
return;
}
this->rx_buffer_[this->rx_index_++] = byte;
if (this->rx_index_ < APC1_FRAME_HEADER_SIZE) {
return;
}
// The payload carries at least the checksum and must fit the buffer; checking it before the sum keeps a
// corrupt length from wrapping the total and walking past the buffer
const uint16_t payload_len = encode_uint16(this->rx_buffer_[2], this->rx_buffer_[3]);
if (payload_len < 2 || payload_len > this->rx_buffer_.size() - APC1_FRAME_HEADER_SIZE) {
ESP_LOGW(TAG, "Invalid frame length: %u", payload_len);
this->rx_index_ = 0;
return;
}
const uint8_t total_len = payload_len + APC1_FRAME_HEADER_SIZE;
if (this->rx_index_ == total_len) {
this->parse_frame_(total_len);
this->rx_index_ = 0;
}
});
}
bool APC1Component::parse_frame_(const uint8_t *frame, uint8_t total_len) {
uint16_t expected_checksum = encode_uint16(frame[total_len - 2], frame[total_len - 1]);
uint16_t calculated_checksum = sum16_bytes(frame, total_len - 2);
void APC1Component::parse_frame_(uint16_t total_len) {
uint16_t expected_checksum = encode_uint16(this->rx_buffer_[total_len - 2], this->rx_buffer_[total_len - 1]);
uint16_t calculated_checksum = sum16_bytes(this->rx_buffer_.data(), total_len - 2);
if (expected_checksum != calculated_checksum) {
ESP_LOGW(TAG, "APC1 checksum mismatch: calculated 0x%04X != expected 0x%04X", calculated_checksum,
expected_checksum);
return false;
return;
}
if (total_len == APC1_FRAME_SIZE_MEASUREMENT) {
this->parse_measurement_frame_(frame);
this->parse_measurement_frame_();
} else if (total_len == APC1_FRAME_SIZE_DEVICE_INFO) {
this->parse_device_info_frame_(frame);
this->parse_device_info_frame_();
} else if (total_len == APC1_FRAME_SIZE_COMMAND_RESPONSE) {
ESP_LOGD(TAG, "APC1 command response: cmd=0x%02X, data=0x%02X", frame[4], frame[5]);
ESP_LOGD(TAG, "APC1 command response: cmd=0x%02X, data=0x%02X", this->rx_buffer_[4], this->rx_buffer_[5]);
} else {
ESP_LOGD(TAG, "Received frame of unknown length %u", total_len);
}
return true;
}
// One message per error bit, bit 0 first
PROGMEM_STRING_TABLE(ErrorStrings, "Fan restart error (too many restarts)", "Fan speed low", "Photodiode fault",
"Fan stopped / startup error", "Laser fault", "VOC sensor fault", "RHT sensor fault");
void APC1Component::parse_measurement_frame_(const uint8_t *frame) {
void APC1Component::parse_measurement_frame_() {
const uint32_t now = App.get_loop_component_start_time();
this->last_valid_frame_ = now;
uint8_t error_code = frame[APC1_MEASUREMENT_OFFSET_ERROR_CODE];
uint8_t error_code = this->rx_buffer_[APC1_MEASUREMENT_OFFSET_ERROR_CODE];
if (error_code != this->last_error_code_) {
if (error_code != 0) {
for (uint8_t bit = 0; bit < ErrorStrings::COUNT; bit++) {
@@ -148,7 +189,7 @@ void APC1Component::parse_measurement_frame_(const uint8_t *frame) {
PUBLISH_STATE(this->error_code_sensor_, error_code);
}
uint8_t aqi = frame[APC1_MEASUREMENT_OFFSET_AQI];
uint8_t aqi = this->rx_buffer_[APC1_MEASUREMENT_OFFSET_AQI];
if (aqi == 0) {
if (!this->gas_warming_up_) {
this->gas_warming_up_ = true;
@@ -162,31 +203,38 @@ void APC1Component::parse_measurement_frame_(const uint8_t *frame) {
// Measurement payload data (bytes 4-57): big-endian sensor readings.
// The values below feed the debug summary, so they are always decoded; every
// other reading is only decoded when its sensor is configured.
uint16_t pm_1_0 = encode_value<uint16_t>(frame + 10);
uint16_t pm_2_5 = encode_value<uint16_t>(frame + 12);
uint16_t pm_10_0 = encode_value<uint16_t>(frame + 14);
uint16_t tvoc = encode_value<uint16_t>(frame + 28);
uint16_t eco2 = encode_value<uint16_t>(frame + 30);
float temp_comp = encode_value<int16_t>(frame + 34) / 10.0f;
float hum_comp = encode_value<uint16_t>(frame + 36) / 10.0f;
const auto u16 = [this](uint8_t offset) {
return encode_uint16(this->rx_buffer_[offset], this->rx_buffer_[offset + 1]);
};
const auto u32 = [this](uint8_t offset) {
return encode_uint32(this->rx_buffer_[offset], this->rx_buffer_[offset + 1], this->rx_buffer_[offset + 2],
this->rx_buffer_[offset + 3]);
};
uint16_t pm_1_0 = u16(10);
uint16_t pm_2_5 = u16(12);
uint16_t pm_10_0 = u16(14);
uint16_t tvoc = u16(28);
uint16_t eco2 = u16(30);
float temp_comp = static_cast<int16_t>(u16(34)) / 10.0f;
float hum_comp = u16(36) / 10.0f;
ESP_LOGD(TAG, "APC1: PM1.0: %u, PM2.5: %u, PM10: %u, TVOC: %u, eCO2: %u, AQI: %u, Temp: %.1f°C, Hum: %.1f%%", pm_1_0,
pm_2_5, pm_10_0, tvoc, eco2, aqi, temp_comp, hum_comp);
PUBLISH_STATE(this->pm_1_0_std_sensor_, encode_value<uint16_t>(frame + 4));
PUBLISH_STATE(this->pm_2_5_std_sensor_, encode_value<uint16_t>(frame + 6));
PUBLISH_STATE(this->pm_10_0_std_sensor_, encode_value<uint16_t>(frame + 8));
PUBLISH_STATE(this->pm_1_0_std_sensor_, u16(4));
PUBLISH_STATE(this->pm_2_5_std_sensor_, u16(6));
PUBLISH_STATE(this->pm_10_0_std_sensor_, u16(8));
PUBLISH_STATE(this->pm_1_0_sensor_, pm_1_0);
PUBLISH_STATE(this->pm_2_5_sensor_, pm_2_5);
PUBLISH_STATE(this->pm_10_0_sensor_, pm_10_0);
PUBLISH_STATE(this->pm_0_3um_sensor_, encode_value<uint16_t>(frame + 16));
PUBLISH_STATE(this->pm_0_5um_sensor_, encode_value<uint16_t>(frame + 18));
PUBLISH_STATE(this->pm_1_0um_sensor_, encode_value<uint16_t>(frame + 20));
PUBLISH_STATE(this->pm_2_5um_sensor_, encode_value<uint16_t>(frame + 22));
PUBLISH_STATE(this->pm_5_0um_sensor_, encode_value<uint16_t>(frame + 24));
PUBLISH_STATE(this->pm_10_0um_sensor_, encode_value<uint16_t>(frame + 26));
PUBLISH_STATE(this->pm_0_3um_sensor_, u16(16));
PUBLISH_STATE(this->pm_0_5um_sensor_, u16(18));
PUBLISH_STATE(this->pm_1_0um_sensor_, u16(20));
PUBLISH_STATE(this->pm_2_5um_sensor_, u16(22));
PUBLISH_STATE(this->pm_5_0um_sensor_, u16(24));
PUBLISH_STATE(this->pm_10_0um_sensor_, u16(26));
PUBLISH_STATE(this->tvoc_sensor_, tvoc);
PUBLISH_STATE(this->eco2_sensor_, eco2);
@@ -195,19 +243,20 @@ void APC1Component::parse_measurement_frame_(const uint8_t *frame) {
PUBLISH_STATE(this->temperature_sensor_, temp_comp);
PUBLISH_STATE(this->humidity_sensor_, hum_comp);
PUBLISH_STATE(this->raw_temperature_sensor_, encode_value<int16_t>(frame + 38) / 10.0f);
PUBLISH_STATE(this->raw_humidity_sensor_, encode_value<uint16_t>(frame + 40) / 10.0f);
PUBLISH_STATE(this->raw_temperature_sensor_, static_cast<int16_t>(u16(38)) / 10.0f);
PUBLISH_STATE(this->raw_humidity_sensor_, u16(40) / 10.0f);
PUBLISH_STATE(this->rs0_sensor_, encode_value<uint32_t>(frame + 42));
PUBLISH_STATE(this->rs2_sensor_, encode_value<uint32_t>(frame + 50));
PUBLISH_STATE(this->rs3_sensor_, encode_value<uint32_t>(frame + 54));
PUBLISH_STATE(this->rs0_sensor_, u32(42));
PUBLISH_STATE(this->rs2_sensor_, u32(50));
PUBLISH_STATE(this->rs3_sensor_, u32(54));
}
void APC1Component::parse_device_info_frame_(const uint8_t *frame) {
const uint8_t *serial = frame + APC1_DEVICE_INFO_OFFSET_SERIAL;
uint16_t fw_version = encode_uint16(frame[APC1_DEVICE_INFO_OFFSET_FW], frame[APC1_DEVICE_INFO_OFFSET_FW + 1]);
void APC1Component::parse_device_info_frame_() {
const uint8_t *serial = this->rx_buffer_.data() + APC1_DEVICE_INFO_OFFSET_SERIAL;
uint16_t fw_version =
encode_uint16(this->rx_buffer_[APC1_DEVICE_INFO_OFFSET_FW], this->rx_buffer_[APC1_DEVICE_INFO_OFFSET_FW + 1]);
ESP_LOGI(TAG, "APC1: Module: %.6s, Serial: %08" PRIX32 "%08" PRIX32 ", Firmware: 0x%04X (%u)",
reinterpret_cast<const char *>(frame + APC1_DEVICE_INFO_OFFSET_MODULE),
reinterpret_cast<const char *>(this->rx_buffer_.data() + APC1_DEVICE_INFO_OFFSET_MODULE),
encode_uint32(serial[0], serial[1], serial[2], serial[3]),
encode_uint32(serial[4], serial[5], serial[6], serial[7]), fw_version, fw_version);
}
@@ -246,8 +295,6 @@ void APC1Component::set_measurement_mode() {
}
void APC1Component::send_command_(APC1Command cmd, uint16_t data) {
// Kept bytes of an earlier reply must not merge into this command's
this->reader_.drop_kept();
std::array<uint8_t, APC1_COMMAND_FRAME_SIZE> buf{
APC1_HEADER_1,
APC1_HEADER_2,
+7 -10
View File
@@ -1,11 +1,12 @@
#pragma once
#include "esphome/core/component.h"
#include "esphome/core/frame_reader.h"
#include "esphome/core/hal.h"
#include "esphome/components/sensor/sensor.h"
#include "esphome/components/uart/uart.h"
#include <array>
namespace esphome::apc1 {
static constexpr uint8_t APC1_HEADER_1 = 0x42;
@@ -78,21 +79,17 @@ class APC1Component final : public uart::UARTDevice, public Component {
void initialize_device_();
/// Restarts the no-data watchdog so a mode change gets the same grace period as a cold boot
void arm_watchdog_();
/// Returns false for a frame that fails the checksum
bool parse_frame_(const uint8_t *frame, uint8_t total_len);
void parse_measurement_frame_(const uint8_t *frame);
void parse_device_info_frame_(const uint8_t *frame);
void parse_frame_(uint16_t total_len);
void parse_measurement_frame_();
void parse_device_info_frame_();
GPIOPin *set_pin_{nullptr};
GPIOPin *reset_pin_{nullptr};
uint32_t last_transmission_{0};
uint32_t last_valid_frame_{0};
uint32_t last_init_attempt_{0};
// 42 4D, payload length (2, BE), payload ending in its checksum (2)
LengthFrameReader<APC1_FRAME_SIZE_MEASUREMENT,
FrameFormat<FrameHeader<APC1_HEADER_1, APC1_HEADER_2>, PayloadLengthU16BE<LengthField{.offset = 2}>,
FrameMinLength<APC1_FRAME_HEADER_SIZE + 2>>>
reader_;
std::array<uint8_t, APC1_FRAME_SIZE_MEASUREMENT> rx_buffer_{};
uint8_t rx_index_{0};
uint8_t last_error_code_{0xFF};
bool active_mode_{true};
bool idle_{false};
@@ -1,7 +1,7 @@
import logging
import esphome.codegen as cg
from esphome.components import web_server_base, wifi
from esphome.components import web_server_base, web_server_idf, wifi
from esphome.components.web_server_base import CONF_WEB_SERVER_BASE_ID
from esphome.components.wifi import DOMAIN as WIFI_DOMAIN
import esphome.config_validation as cv
@@ -38,6 +38,13 @@ DOMAIN = "captive_portal"
captive_portal_ns = cg.esphome_ns.namespace("captive_portal")
CaptivePortal = captive_portal_ns.class_("CaptivePortal", cg.Component)
def _require_plain_http(config: ConfigType) -> ConfigType:
# Phones check for a captive portal over plain HTTP.
web_server_idf.require_plain_http(DOMAIN)
return config
CONFIG_SCHEMA = cv.All(
cv.Schema(
{
@@ -58,6 +65,7 @@ CONFIG_SCHEMA = cv.All(
PLATFORM_RTL87XX,
]
),
_require_plain_http,
)
+138 -108
View File
@@ -1,5 +1,6 @@
#include "daly_bms.h"
#include <algorithm>
#include <vector>
#include "esphome/core/application.h"
#include "esphome/core/helpers.h"
#include "esphome/core/log.h"
@@ -8,6 +9,7 @@ namespace esphome::daly_bms {
ESPHOME_LOG_TAG(TAG, "daly_bms");
static const uint8_t DALY_FRAME_SIZE = 13;
static const uint8_t DALY_TEMPERATURE_OFFSET = 40;
static const uint16_t DALY_CURRENT_OFFSET = 30000;
@@ -46,9 +48,14 @@ void DalyBmsComponent::update() {
void DalyBmsComponent::loop() {
const uint32_t now = App.get_loop_component_start_time();
auto handle = [this](const uint8_t *frame, size_t /*len*/) { return this->handle_frame_(frame); };
this->read_frames(this->reader_, handle, now, this->last_transmission_);
if (this->response_complete_() && !this->reader_.receiving() &&
const size_t avail = this->available();
if (this->receiving_ && (now - this->last_transmission_ >= 200)) {
// last transmission too long ago. Reset RX index.
ESP_LOGW(TAG, "Last transmission too long ago. Reset RX index.");
this->data_.clear();
this->receiving_ = false;
}
if (this->response_complete_() && !this->receiving_ && avail == 0 &&
(now - this->last_transmission_ >= DALY_RESPONSE_GAP_MS) && !this->trigger_next_) {
// response complete and nothing more on the line -> trigger next request without waiting for the timeout
this->last_transmission_ = now;
@@ -59,6 +66,25 @@ void DalyBmsComponent::loop() {
this->last_transmission_ = now;
this->trigger_next_ = true;
}
if (avail > 0)
this->last_transmission_ = now;
this->read_available(
[this](uint8_t c) {
if (!this->receiving_) {
if (c != 0xa5)
return;
this->receiving_ = true;
}
this->data_.push_back(c);
if (this->data_.size() == 4)
this->data_count_ = c;
if ((this->data_.size() > 4) and (data_.size() == this->data_count_ + 5)) {
this->decode_data_(this->data_);
this->data_.clear();
this->receiving_ = false;
}
},
avail);
if (!this->trigger_next_)
return;
@@ -79,8 +105,6 @@ void DalyBmsComponent::loop() {
this->last_transmission_ = now;
// the request IDs are consecutive, see DALY_REQUEST_BATTERY_LEVEL
this->last_request_ = DALY_REQUEST_BATTERY_LEVEL + this->next_request_++;
// Kept bytes of an earlier reply must not merge into this request's
this->reader_.drop_kept();
this->request_data_(this->last_request_);
}
@@ -119,122 +143,128 @@ void DalyBmsComponent::request_data_(uint8_t data_id) {
this->flush();
}
bool DalyBmsComponent::handle_frame_(const uint8_t *frame) {
// Only BMS responses (address 0x01) with their 8 data bytes carry data; an echoed request is skipped like noise
if (frame[1] != 0x01 || frame[3] != 8)
return false;
if (sum_bytes(frame, DALY_FRAME_SIZE - 1) != frame[DALY_FRAME_SIZE - 1]) {
ESP_LOGW(TAG, "Checksum-Error on Packet %x", frame[4]);
return false;
}
// only an answer to the last request counts
if (frame[2] == this->last_request_)
this->frames_received_++;
if (frame[2] == DALY_REQUEST_STATUS) {
this->cell_frames_ = (frame[4] + 2) / 3; // three cells per cell voltage frame
this->temperature_frames_ = std::max((frame[5] + 6) / 7, 1);
}
switch (frame[2]) {
void DalyBmsComponent::decode_data_(std::vector<uint8_t> data) {
auto it = data.begin();
while ((it = std::find(it, data.end(), 0xA5)) != data.end()) {
if (data.end() - it >= DALY_FRAME_SIZE && it[1] == 0x01) {
const uint8_t checksum = sum_bytes(&*it, 12);
if (checksum == it[12]) {
// only a valid frame answering the last request counts; an echoed request or a late answer does not
if (it[2] == this->last_request_)
this->frames_received_++;
if (it[2] == DALY_REQUEST_STATUS) {
this->cell_frames_ = (it[4] + 2) / 3; // three cells per cell voltage frame
this->temperature_frames_ = std::max((it[5] + 6) / 7, 1);
}
switch (it[2]) {
#ifdef USE_SENSOR
case DALY_REQUEST_BATTERY_LEVEL:
PUBLISH_STATE(this->voltage_sensor_, (float) encode_uint16(frame[4], frame[5]) / 10);
PUBLISH_STATE(this->current_sensor_, (float) (encode_uint16(frame[8], frame[9]) - DALY_CURRENT_OFFSET) / 10);
PUBLISH_STATE(this->battery_level_sensor_, (float) encode_uint16(frame[10], frame[11]) / 10);
break;
case DALY_REQUEST_BATTERY_LEVEL:
PUBLISH_STATE(this->voltage_sensor_, (float) encode_uint16(it[4], it[5]) / 10);
PUBLISH_STATE(this->current_sensor_, (float) (encode_uint16(it[8], it[9]) - DALY_CURRENT_OFFSET) / 10);
PUBLISH_STATE(this->battery_level_sensor_, (float) encode_uint16(it[10], it[11]) / 10);
break;
case DALY_REQUEST_MIN_MAX_VOLTAGE:
PUBLISH_STATE(this->max_cell_voltage_sensor_, (float) encode_uint16(frame[4], frame[5]) / 1000);
PUBLISH_STATE(this->max_cell_voltage_number_sensor_, frame[6]);
PUBLISH_STATE(this->min_cell_voltage_sensor_, (float) encode_uint16(frame[7], frame[8]) / 1000);
PUBLISH_STATE(this->min_cell_voltage_number_sensor_, frame[9]);
break;
case DALY_REQUEST_MIN_MAX_VOLTAGE:
PUBLISH_STATE(this->max_cell_voltage_sensor_, (float) encode_uint16(it[4], it[5]) / 1000);
PUBLISH_STATE(this->max_cell_voltage_number_sensor_, it[6]);
PUBLISH_STATE(this->min_cell_voltage_sensor_, (float) encode_uint16(it[7], it[8]) / 1000);
PUBLISH_STATE(this->min_cell_voltage_number_sensor_, it[9]);
break;
case DALY_REQUEST_MIN_MAX_TEMPERATURE:
PUBLISH_STATE(this->max_temperature_sensor_, frame[4] - DALY_TEMPERATURE_OFFSET);
PUBLISH_STATE(this->max_temperature_probe_number_sensor_, frame[5]);
PUBLISH_STATE(this->min_temperature_sensor_, frame[6] - DALY_TEMPERATURE_OFFSET);
PUBLISH_STATE(this->min_temperature_probe_number_sensor_, frame[7]);
break;
case DALY_REQUEST_MIN_MAX_TEMPERATURE:
PUBLISH_STATE(this->max_temperature_sensor_, it[4] - DALY_TEMPERATURE_OFFSET);
PUBLISH_STATE(this->max_temperature_probe_number_sensor_, it[5]);
PUBLISH_STATE(this->min_temperature_sensor_, it[6] - DALY_TEMPERATURE_OFFSET);
PUBLISH_STATE(this->min_temperature_probe_number_sensor_, it[7]);
break;
#endif
case DALY_REQUEST_MOS:
case DALY_REQUEST_MOS:
#ifdef USE_TEXT_SENSOR
if (this->status_text_sensor_ != nullptr) {
switch (frame[4]) {
case 0:
this->status_text_sensor_->publish_state("Stationary");
if (this->status_text_sensor_ != nullptr) {
switch (it[4]) {
case 0:
this->status_text_sensor_->publish_state("Stationary");
break;
case 1:
this->status_text_sensor_->publish_state("Charging");
break;
case 2:
this->status_text_sensor_->publish_state("Discharging");
break;
default:
break;
}
}
#endif
#ifdef USE_BINARY_SENSOR
PUBLISH_STATE(this->charging_mos_enabled_binary_sensor_, it[5]);
PUBLISH_STATE(this->discharging_mos_enabled_binary_sensor_, it[6]);
#endif
#ifdef USE_SENSOR
PUBLISH_STATE(this->remaining_capacity_sensor_, (float) encode_uint32(it[8], it[9], it[10], it[11]) / 1000);
#endif
break;
case 1:
this->status_text_sensor_->publish_state("Charging");
#ifdef USE_SENSOR
case DALY_REQUEST_STATUS:
PUBLISH_STATE(this->cells_number_sensor_, it[4]);
break;
case 2:
this->status_text_sensor_->publish_state("Discharging");
case DALY_REQUEST_TEMPERATURE:
if (it[4] == 1) {
PUBLISH_STATE(this->temperature_1_sensor_, it[5] - DALY_TEMPERATURE_OFFSET);
PUBLISH_STATE(this->temperature_2_sensor_, it[6] - DALY_TEMPERATURE_OFFSET);
}
break;
case DALY_REQUEST_CELL_VOLTAGE:
switch (it[4]) {
case 1:
PUBLISH_STATE(this->cell_1_voltage_sensor_, (float) encode_uint16(it[5], it[6]) / 1000);
PUBLISH_STATE(this->cell_2_voltage_sensor_, (float) encode_uint16(it[7], it[8]) / 1000);
PUBLISH_STATE(this->cell_3_voltage_sensor_, (float) encode_uint16(it[9], it[10]) / 1000);
break;
case 2:
PUBLISH_STATE(this->cell_4_voltage_sensor_, (float) encode_uint16(it[5], it[6]) / 1000);
PUBLISH_STATE(this->cell_5_voltage_sensor_, (float) encode_uint16(it[7], it[8]) / 1000);
PUBLISH_STATE(this->cell_6_voltage_sensor_, (float) encode_uint16(it[9], it[10]) / 1000);
break;
case 3:
PUBLISH_STATE(this->cell_7_voltage_sensor_, (float) encode_uint16(it[5], it[6]) / 1000);
PUBLISH_STATE(this->cell_8_voltage_sensor_, (float) encode_uint16(it[7], it[8]) / 1000);
PUBLISH_STATE(this->cell_9_voltage_sensor_, (float) encode_uint16(it[9], it[10]) / 1000);
break;
case 4:
PUBLISH_STATE(this->cell_10_voltage_sensor_, (float) encode_uint16(it[5], it[6]) / 1000);
PUBLISH_STATE(this->cell_11_voltage_sensor_, (float) encode_uint16(it[7], it[8]) / 1000);
PUBLISH_STATE(this->cell_12_voltage_sensor_, (float) encode_uint16(it[9], it[10]) / 1000);
break;
case 5:
PUBLISH_STATE(this->cell_13_voltage_sensor_, (float) encode_uint16(it[5], it[6]) / 1000);
PUBLISH_STATE(this->cell_14_voltage_sensor_, (float) encode_uint16(it[7], it[8]) / 1000);
PUBLISH_STATE(this->cell_15_voltage_sensor_, (float) encode_uint16(it[9], it[10]) / 1000);
break;
case 6:
PUBLISH_STATE(this->cell_16_voltage_sensor_, (float) encode_uint16(it[5], it[6]) / 1000);
PUBLISH_STATE(this->cell_17_voltage_sensor_, (float) encode_uint16(it[7], it[8]) / 1000);
PUBLISH_STATE(this->cell_18_voltage_sensor_, (float) encode_uint16(it[9], it[10]) / 1000);
break;
}
break;
#endif
default:
break;
}
} else {
ESP_LOGW(TAG, "Checksum-Error on Packet %x", it[4]);
}
#endif
#ifdef USE_BINARY_SENSOR
PUBLISH_STATE(this->charging_mos_enabled_binary_sensor_, frame[5]);
PUBLISH_STATE(this->discharging_mos_enabled_binary_sensor_, frame[6]);
#endif
#ifdef USE_SENSOR
PUBLISH_STATE(this->remaining_capacity_sensor_,
(float) encode_uint32(frame[8], frame[9], frame[10], frame[11]) / 1000);
#endif
break;
#ifdef USE_SENSOR
case DALY_REQUEST_STATUS:
PUBLISH_STATE(this->cells_number_sensor_, frame[4]);
break;
case DALY_REQUEST_TEMPERATURE:
if (frame[4] == 1) {
PUBLISH_STATE(this->temperature_1_sensor_, frame[5] - DALY_TEMPERATURE_OFFSET);
PUBLISH_STATE(this->temperature_2_sensor_, frame[6] - DALY_TEMPERATURE_OFFSET);
}
break;
case DALY_REQUEST_CELL_VOLTAGE:
switch (frame[4]) {
case 1:
PUBLISH_STATE(this->cell_1_voltage_sensor_, (float) encode_uint16(frame[5], frame[6]) / 1000);
PUBLISH_STATE(this->cell_2_voltage_sensor_, (float) encode_uint16(frame[7], frame[8]) / 1000);
PUBLISH_STATE(this->cell_3_voltage_sensor_, (float) encode_uint16(frame[9], frame[10]) / 1000);
break;
case 2:
PUBLISH_STATE(this->cell_4_voltage_sensor_, (float) encode_uint16(frame[5], frame[6]) / 1000);
PUBLISH_STATE(this->cell_5_voltage_sensor_, (float) encode_uint16(frame[7], frame[8]) / 1000);
PUBLISH_STATE(this->cell_6_voltage_sensor_, (float) encode_uint16(frame[9], frame[10]) / 1000);
break;
case 3:
PUBLISH_STATE(this->cell_7_voltage_sensor_, (float) encode_uint16(frame[5], frame[6]) / 1000);
PUBLISH_STATE(this->cell_8_voltage_sensor_, (float) encode_uint16(frame[7], frame[8]) / 1000);
PUBLISH_STATE(this->cell_9_voltage_sensor_, (float) encode_uint16(frame[9], frame[10]) / 1000);
break;
case 4:
PUBLISH_STATE(this->cell_10_voltage_sensor_, (float) encode_uint16(frame[5], frame[6]) / 1000);
PUBLISH_STATE(this->cell_11_voltage_sensor_, (float) encode_uint16(frame[7], frame[8]) / 1000);
PUBLISH_STATE(this->cell_12_voltage_sensor_, (float) encode_uint16(frame[9], frame[10]) / 1000);
break;
case 5:
PUBLISH_STATE(this->cell_13_voltage_sensor_, (float) encode_uint16(frame[5], frame[6]) / 1000);
PUBLISH_STATE(this->cell_14_voltage_sensor_, (float) encode_uint16(frame[7], frame[8]) / 1000);
PUBLISH_STATE(this->cell_15_voltage_sensor_, (float) encode_uint16(frame[9], frame[10]) / 1000);
break;
case 6:
PUBLISH_STATE(this->cell_16_voltage_sensor_, (float) encode_uint16(frame[5], frame[6]) / 1000);
PUBLISH_STATE(this->cell_17_voltage_sensor_, (float) encode_uint16(frame[7], frame[8]) / 1000);
PUBLISH_STATE(this->cell_18_voltage_sensor_, (float) encode_uint16(frame[9], frame[10]) / 1000);
break;
}
break;
#endif
default:
break;
std::advance(it, DALY_FRAME_SIZE);
} else {
std::advance(it, 1);
}
}
return true;
}
} // namespace esphome::daly_bms
+6 -7
View File
@@ -2,7 +2,6 @@
#include "esphome/core/component.h"
#include "esphome/core/defines.h"
#include "esphome/core/frame_reader.h"
#ifdef USE_SENSOR
#include "esphome/components/sensor/sensor.h"
#endif
@@ -14,9 +13,9 @@
#endif
#include "esphome/components/uart/uart.h"
namespace esphome::daly_bms {
#include <vector>
static constexpr uint8_t DALY_FRAME_SIZE = 13;
namespace esphome::daly_bms {
class DalyBmsComponent final : public PollingComponent, public uart::UARTDevice {
public:
@@ -77,13 +76,13 @@ class DalyBmsComponent final : public PollingComponent, public uart::UARTDevice
void request_data_(uint8_t data_id);
// all frames of the response to the last request are in
bool response_complete_() const;
/// Returns false for a frame that is not a BMS response or fails the checksum
bool handle_frame_(const uint8_t *frame);
void decode_data_(std::vector<uint8_t> data);
uint8_t addr_;
// A5, address, data ID, length 8, 8 data bytes, sum8; every request and response has 8 data bytes
LengthFrameReader<DALY_FRAME_SIZE, FrameFormat<FrameHeader<0xA5>, FrameFixedLength<DALY_FRAME_SIZE>>> reader_;
std::vector<uint8_t> data_;
bool receiving_{false};
uint8_t data_count_{0};
uint32_t last_transmission_{0};
bool trigger_next_{false};
// update() asked for a request cycle; it starts once no cycle is running. The first one runs at boot.
@@ -0,0 +1,28 @@
#include "epaper_spi_ssd1677_bwr.h"
namespace esphome::epaper_spi {
bool EPaperSSD1677BWR::reset() {
if (!EPaperBase::reset())
return false;
this->command(0x12); // SWRESET; the INITIALISE state waits for busy to clear
return true;
}
void EPaperSSD1677BWR::set_window() {
const uint16_t x_end = this->width_ - 1;
const uint16_t y_first = this->offset_height_ + this->height_ - 1;
const uint16_t y_last = this->offset_height_;
this->cmd_data(0x44, {0x00, 0x00, (uint8_t) x_end, (uint8_t) (x_end >> 8)});
// Y runs down from the last gate, to match data entry mode 0x01
this->cmd_data(0x45, {(uint8_t) y_first, (uint8_t) (y_first >> 8), (uint8_t) y_last, (uint8_t) (y_last >> 8)});
this->cmd_data(0x4E, {0x00, 0x00});
this->cmd_data(0x4F, {(uint8_t) y_first, (uint8_t) (y_first >> 8)});
}
void EPaperSSD1677BWR::refresh_screen(bool partial) {
this->cmd_data(0x22, {0xF7});
this->command(0x20);
}
} // namespace esphome::epaper_spi
@@ -0,0 +1,32 @@
#pragma once
#include "epaper_weact_3c.h"
namespace esphome::epaper_spi {
/**
* SSD1677 3-color (black/white/red) e-paper displays, e.g. the Waveshare 7.5" HD (B).
*
* Drawn and sent as EPaperWeAct3C, except that the SSD1677 addresses RAM X in pixels with two bytes, and the RAM is
* written from the last gate down (data entry mode 0x01), as in Waveshare's reference code. The controller scans
* offset_height + height gates and the panel is on the last height of them, so the RAM window covers only those.
* The window is exactly the panel, so after the first plane the address counter wraps back to the start for the
* second.
*/
class EPaperSSD1677BWR : public EPaperWeAct3C {
public:
EPaperSSD1677BWR(const char *name, uint16_t width, uint16_t height, const uint8_t *init_sequence,
size_t init_sequence_length, uint16_t offset_height)
: EPaperWeAct3C(name, width, height, init_sequence, init_sequence_length), offset_height_(offset_height) {}
protected:
bool reset() override;
void set_window() override;
// The full refresh (0x22 0xF7) powers the panel up itself, as Waveshare's reference code relies on.
void power_on() override {}
void refresh_screen(bool partial) override;
uint16_t offset_height_;
};
} // namespace esphome::epaper_spi
@@ -6,7 +6,7 @@ namespace esphome::epaper_spi {
ESPHOME_LOG_TAG(TAG, "epaper_weact_3c");
void EPaperWeAct3C::set_window_() {
void EPaperWeAct3C::set_window() {
// For full screen refresh, we always start from (0,0)
// The y_low_/y_high_ values track the dirty region for optimization,
// but for display refresh we need to write from the beginning
@@ -39,7 +39,7 @@ bool HOT EPaperWeAct3C::transfer_data() {
// A push only yields after sending some data, so it never stops at exactly 0 or half_buffer.
if (this->current_data_index_ == 0) {
ESP_LOGV(TAG, "transfer_data: sending RED buffer (0x26)");
this->set_window_(); // Reset RAM X/Y counters to start position
this->set_window(); // Reset RAM X/Y counters to start position
this->command(0x26);
this->current_data_index_ = half_buffer;
}
@@ -19,7 +19,7 @@ class EPaperWeAct3C : public EPaperBWR {
: EPaperBWR(name, width, height, init_sequence, init_sequence_length, invert_red, /*red_sets_black=*/true) {}
protected:
void set_window_();
virtual void set_window();
void refresh_screen(bool partial) override;
void power_on() override;
void power_off() override;
@@ -0,0 +1,92 @@
"""Black/White/Red e-paper displays using the SSD1677 controller.
Supported models:
- waveshare-7.5in-hd-b: 880x528 pixels (Waveshare 7.5" HD (B), EPD_7in5b_HD)
The controller scans offset_height + height gates, and the panel is on the last height of them;
offset_height (under dimensions) is a property of the panel.
"""
import esphome.config_validation as cv
from esphome.const import (
CONF_BUSY_PIN,
CONF_DIMENSIONS,
CONF_HEIGHT,
CONF_OFFSET_HEIGHT,
CONF_RESET_PIN,
CONF_WIDTH,
)
from esphome.types import ConfigType
from . import EpaperModel
# The SSD1677 drives this many sources (columns) and gates (rows)
SOURCE_COUNT = 960
GATE_COUNT = 680
class SSD1677BWR(EpaperModel):
"""EpaperModel class for Black/White/Red displays using the SSD1677 controller."""
source_files = (
"epaper_spi_bwr.cpp",
"epaper_weact_3c.cpp",
"epaper_spi_ssd1677_bwr.cpp",
)
def __init__(self, name: str, **defaults: object) -> None:
defaults.setdefault(CONF_OFFSET_HEIGHT, 0)
super().__init__(name, "EPaperSSD1677BWR", **defaults)
def option(
self, name: str, fallback: object = cv.UNDEFINED
) -> cv.Optional | cv.Required:
if name in (CONF_RESET_PIN, CONF_BUSY_PIN):
return cv.Required(name)
return super().option(name, fallback)
def validate_config(self, config: ConfigType) -> ConfigType:
width, height = self.get_dimensions(config)
if width > SOURCE_COUNT:
raise cv.Invalid(
f"The panel width must not exceed {SOURCE_COUNT} sources",
path=[CONF_DIMENSIONS, CONF_WIDTH],
)
# The model's defaults fit, so this fails only with explicit dimensions, which always have a height
if self.get_offset_height(config) + height > GATE_COUNT:
raise cv.Invalid(
f"{CONF_OFFSET_HEIGHT} plus the panel height must not exceed {GATE_COUNT} gates",
path=[CONF_DIMENSIONS, CONF_HEIGHT],
)
return config
def get_constructor_args(self, config: ConfigType) -> tuple:
return (self.get_offset_height(config),)
def get_init_sequence(self, config: ConfigType) -> tuple:
"""Follow Waveshare's EPD_7in5b_HD init, less the steps the driver does itself.
The RAM window is set before each push, the RAM fills (0x46, 0x47) are not needed as both
planes are always written in full, and the full refresh (0x22 0xF7) loads the waveform.
"""
_, height = self.get_dimensions(config)
last_gate = self.get_offset_height(config) + height - 1
return (
(0x0C, 0xAE, 0xC7, 0xC3, 0xC0, 0x40), # Booster soft start
# Driver output control: gate count, scanned bottom to top
(0x01, last_gate & 0xFF, last_gate >> 8, 0x01),
(0x11, 0x01), # Data entry mode: X increments, Y decrements
(0x3C, 0x01), # Border waveform
(0x18, 0x80), # Internal temperature sensor
)
# Model: Waveshare 7.5" HD (B) - 880x528 pixels, SSD1677 controller
# The panel is on the last 528 of the controller's 680 gates. Waveshare's code sets 688 gates (0x2AF),
# more than the controller has, which shows the image a few rows too low.
SSD1677BWR(
"waveshare-7.5in-hd-b",
width=880,
height=528,
offset_height=152,
)
+31 -9
View File
@@ -835,6 +835,22 @@ def is_idf_sdkconfig_option_enabled(name: str) -> bool:
return value is not None and _format_sdkconfig_val(value) == "y"
def task_wdt_timeout_ms() -> int:
"""Return the configured task watchdog timeout; 0 when it is disabled.
For final validation only, as it reads the full config.
"""
return fv.full_config.get()[DOMAIN][CONF_WATCHDOG_TIMEOUT].total_milliseconds
def validate_task_wdt_enabled(config: ConfigType, key: str) -> None:
"""Final-validate that ``key`` is not set while the task watchdog is disabled."""
if key in config and task_wdt_timeout_ms() == 0:
raise cv.Invalid(
f"Cannot be set when {DOMAIN}.{CONF_WATCHDOG_TIMEOUT} is 0s", path=[key]
)
def set_idf_sdkconfig_default(name: str, value: SdkconfigValueType) -> None:
"""Set an sdkconfig option unless it is already set.
@@ -2510,7 +2526,13 @@ CONFIG_SCHEMA = cv.All(
): _validate_toolchain,
cv.Optional(CONF_WATCHDOG_TIMEOUT, default="5s"): cv.All(
cv.positive_time_period_seconds,
cv.Range(min=cv.TimePeriod(seconds=5), max=cv.TimePeriod(seconds=60)),
cv.Any(
cv.Range(max=cv.TimePeriod(seconds=0)),
cv.Range(
min=cv.TimePeriod(seconds=5), max=cv.TimePeriod(seconds=60)
),
msg="must be 0s (disabled) or between 5s and 60s",
),
),
}
),
@@ -3287,14 +3309,14 @@ async def to_code(config):
add_idf_sdkconfig_option("CONFIG_HEAP_PLACE_FUNCTION_INTO_FLASH", True)
# Setup watchdog
add_idf_sdkconfig_option("CONFIG_ESP_TASK_WDT_INIT", True)
add_idf_sdkconfig_option("CONFIG_ESP_TASK_WDT_PANIC", True)
add_idf_sdkconfig_option("CONFIG_ESP_TASK_WDT_CHECK_IDLE_TASK_CPU0", False)
add_idf_sdkconfig_option("CONFIG_ESP_TASK_WDT_CHECK_IDLE_TASK_CPU1", False)
add_idf_sdkconfig_option(
"CONFIG_ESP_TASK_WDT_TIMEOUT_S",
config[CONF_WATCHDOG_TIMEOUT].total_seconds,
)
wdt = config[CONF_WATCHDOG_TIMEOUT].total_seconds
add_idf_sdkconfig_option("CONFIG_ESP_TASK_WDT_INIT", wdt != 0)
if wdt:
cg.add_define("USE_ESP32_TASK_WDT")
add_idf_sdkconfig_option("CONFIG_ESP_TASK_WDT_PANIC", True)
add_idf_sdkconfig_option("CONFIG_ESP_TASK_WDT_CHECK_IDLE_TASK_CPU0", False)
add_idf_sdkconfig_option("CONFIG_ESP_TASK_WDT_CHECK_IDLE_TASK_CPU1", False)
add_idf_sdkconfig_option("CONFIG_ESP_TASK_WDT_TIMEOUT_S", wdt)
# Disable dynamic log level control to save memory
add_idf_sdkconfig_option("CONFIG_LOG_DYNAMIC_LEVEL_CONTROL", False)
+2
View File
@@ -43,7 +43,9 @@ void arch_restart() {
void arch_init() {
// Enable the task watchdog only on the loop task (from which we're currently running)
#ifdef USE_ESP32_TASK_WDT
esp_task_wdt_add(nullptr);
#endif
// Handle OTA rollback: mark partition valid immediately unless USE_OTA_ROLLBACK is enabled,
// in which case safe_mode will mark it valid after confirming successful boot.
+5 -1
View File
@@ -49,7 +49,11 @@ __attribute__((always_inline)) inline uint64_t millis_64() {
// NOLINTNEXTLINE(readability-identifier-naming)
__attribute__((always_inline)) inline void delayMicroseconds(uint32_t us) { delay_microseconds_safe(us); }
__attribute__((always_inline)) inline void arch_feed_wdt() { esp_task_wdt_reset(); }
__attribute__((always_inline)) inline void arch_feed_wdt() {
#ifdef USE_ESP32_TASK_WDT
esp_task_wdt_reset();
#endif
}
__attribute__((always_inline)) inline uint32_t arch_get_cpu_cycle_count() { return esp_cpu_get_cycle_count(); }
void arch_init();
+3 -5
View File
@@ -22,7 +22,6 @@ from esphome.const import (
CONF_DNS2,
CONF_DOMAIN,
CONF_ENABLE_ON_BOOT,
CONF_ETHERNET,
CONF_GATEWAY,
CONF_ID,
CONF_INTERRUPT_PIN,
@@ -397,9 +396,9 @@ def _validate(config: ConfigType) -> ConfigType:
BASE_SCHEMA = cv.Schema(
{
cv.GenerateID(): cv.declare_id(EthernetComponent),
cv.Optional(
CONF_MANUAL_IP, visibility=cv.Visibility.ADVANCED
): MANUAL_IP_SCHEMA,
cv.Optional(CONF_MANUAL_IP, visibility=cv.Visibility.ADVANCED): cv.All(
MANUAL_IP_SCHEMA, require_ipv4(DOMAIN, CONF_MANUAL_IP)
),
cv.Optional(CONF_DOMAIN, default=".local"): cv.domain_name,
cv.Optional(CONF_USE_ADDRESS): cv.string_strict,
cv.Optional(CONF_MAC_ADDRESS): cv.mac_address,
@@ -556,7 +555,6 @@ CONFIG_SCHEMA = cv.All(
upper=True,
),
_validate,
require_ipv4(CONF_ETHERNET),
)
@@ -14,6 +14,9 @@
#if USE_NETWORK_IPV6
#include <esp_netif_net_stack.h>
#include <lwip/netif.h>
#if !USE_NETWORK_IPV4
#include <lwip/dhcp6.h>
#endif
#endif
#ifdef USE_PSRAM
#include <esp_psram.h>
@@ -730,6 +733,7 @@ network::IPAddresses EthernetComponent::get_ip_addresses() {
network::IPAddresses addresses;
if (!this->ethernet_initialized_)
return addresses; // all-zero IPs
#if USE_NETWORK_IPV4
esp_netif_ip_info_t ip;
esp_err_t err = esp_netif_get_ip_info(this->eth_netif_, &ip);
if (err != ESP_OK) {
@@ -739,6 +743,7 @@ network::IPAddresses EthernetComponent::get_ip_addresses() {
} else {
addresses[0] = network::IPAddress(&ip.ip);
}
#endif
#if USE_NETWORK_IPV6
network::fill_ip6_addresses(this->eth_netif_, addresses);
#endif /* USE_NETWORK_IPV6 */
@@ -777,7 +782,7 @@ void EthernetComponent::eth_event_handler(void *arg, esp_event_base_t event_base
#if USE_NETWORK_IPV6
// Start SLAAC on link-up, not after the DHCPv4 lease. This also restores the
// link-local after a link flap, which clears the IPv6 addresses.
if (esp_err_t ll_err = esp_netif_create_ip6_linklocal(global_eth_component->eth_netif_); ll_err != ESP_OK) {
if (esp_err_t ll_err = global_eth_component->ensure_ip6_linklocal_(); ll_err != ESP_OK) {
ESP_LOGW(TAG, "esp_netif_create_ip6_linklocal failed on link-up: %s", esp_err_to_name(ll_err));
}
#endif /* USE_NETWORK_IPV6 */
@@ -816,6 +821,9 @@ void EthernetComponent::got_ip_event_handler(void *arg, esp_event_base_t event_b
void EthernetComponent::got_ip6_event_handler(void *arg, esp_event_base_t event_base, int32_t event_id,
void *event_data) {
ip_event_got_ip6_t *event = (ip_event_got_ip6_t *) event_data;
if (event->esp_netif != global_eth_component->eth_netif_) {
return; // another interface's address (wifi alongside ethernet)
}
ESP_LOGV(TAG, "[Ethernet event] ETH Got IPv6: " IPV6STR, IPV62STR(event->ip6_info.ip));
// Count the addresses on the interface, not the events: recreating the link-local
// after a link flap fires another event for the same address.
@@ -825,8 +833,8 @@ void EthernetComponent::got_ip6_event_handler(void *arg, esp_event_base_t event_
global_eth_component->ip_state_pending_.store(1, std::memory_order_release);
#endif
#if (USE_NETWORK_MIN_IPV6_ADDR_COUNT > 0)
global_eth_component->connected_ =
global_eth_component->got_ipv4_address_ && (global_eth_component->ipv6_count_ >= USE_NETWORK_MIN_IPV6_ADDR_COUNT);
global_eth_component->connected_ = (!USE_NETWORK_IPV4 || global_eth_component->got_ipv4_address_) &&
(global_eth_component->ipv6_count_ >= USE_NETWORK_MIN_IPV6_ADDR_COUNT);
global_eth_component->enable_loop_soon_any_context(); // Enable loop when connection state changes
#else
global_eth_component->connected_ = global_eth_component->got_ipv4_address_;
@@ -840,21 +848,31 @@ void EthernetComponent::got_ip6_event_handler(void *arg, esp_event_base_t event_
// duplicate address detection: recreating it would restart DAD. esp_netif_get_ip6_linklocal()
// only reports a preferred address, so ask lwIP for the slot state instead.
esp_err_t EthernetComponent::ensure_ip6_linklocal_() {
if (auto *netif = static_cast<struct netif *>(esp_netif_get_netif_impl(this->eth_netif_)); netif != nullptr) {
u8_t state;
{
LwIPLock lock;
state = netif_ip6_addr_state(netif, 0);
}
if (ip6_addr_istentative(state) || ip6_addr_isvalid(state)) {
return ESP_OK;
}
auto *netif = static_cast<struct netif *>(esp_netif_get_netif_impl(this->eth_netif_));
u8_t state = IP6_ADDR_INVALID;
if (netif != nullptr) {
LwIPLock lock;
state = netif_ip6_addr_state(netif, 0);
}
esp_err_t err = esp_netif_create_ip6_linklocal(this->eth_netif_);
if (err == ESP_OK) {
if (!ip6_addr_istentative(state) && !ip6_addr_isvalid(state)) {
if (esp_err_t err = esp_netif_create_ip6_linklocal(this->eth_netif_); err != ESP_OK) {
return err;
}
ESP_LOGD(TAG, "IPv6 link-local address created");
}
return err;
#if !USE_NETWORK_IPV4
// Without DHCPv4, DNS servers come from RDNSS or stateless DHCPv6; esp_netif never starts the latter.
// Idempotent in lwIP, so the link-up and connect paths can both call it.
if (netif != nullptr) {
esp_netif_tcpip_exec(
[](void *ctx) -> esp_err_t {
dhcp6_enable_stateless(static_cast<struct netif *>(ctx));
return ESP_OK;
},
netif);
}
#endif
return ESP_OK;
}
#endif /* USE_NETWORK_IPV6 */
@@ -896,6 +914,7 @@ void EthernetComponent::start_connect_() {
ESP_LOGW(TAG, "esp_netif_set_hostname failed: %s", esp_err_to_name(err));
}
#if USE_NETWORK_IPV4
esp_netif_ip_info_t info{};
#ifdef USE_ETHERNET_MANUAL_IP
const ManualIP manual_ip = load_manual_ip();
@@ -946,6 +965,7 @@ void EthernetComponent::start_connect_() {
ESPHL_ERROR_CHECK(err, "DHCPC start error");
}
#endif
#endif // USE_NETWORK_IPV4
#if USE_NETWORK_IPV6
// Attempt to create IPv6 link-local address
// We MUST attempt this here, not just in finish_connect_(), because with
@@ -980,8 +1000,13 @@ void EthernetComponent::dump_connect_params_() {
ESP_LOGCONFIG(TAG, " uninitialized/disabled");
return;
}
#if USE_NETWORK_IPV4
esp_netif_ip_info_t ip;
esp_netif_get_ip_info(this->eth_netif_, &ip);
network::IPAddress ip4(&ip.ip), subnet(&ip.netmask), gateway(&ip.gw);
#else
network::IPAddress ip4, subnet, gateway;
#endif
const ip_addr_t *dns_ip1;
const ip_addr_t *dns_ip2;
{
@@ -1020,8 +1045,7 @@ void EthernetComponent::dump_connect_params_() {
" MAC Address: %s\n"
" Is Full Duplex: %s\n"
" Link Speed: %u",
network::IPAddress(&ip.ip).str_to(ip_buf), App.get_name().c_str(),
network::IPAddress(&ip.netmask).str_to(subnet_buf), network::IPAddress(&ip.gw).str_to(gateway_buf),
ip4.str_to(ip_buf), App.get_name().c_str(), subnet.str_to(subnet_buf), gateway.str_to(gateway_buf),
network::IPAddress(dns_ip1).str_to(dns1_buf), network::IPAddress(dns_ip2).str_to(dns2_buf),
this->get_eth_mac_address_pretty_into_buffer(mac_buf),
YESNO(this->get_duplex_mode() == ETH_DUPLEX_FULL), link_speed);
+6 -6
View File
@@ -17,14 +17,12 @@ from esphome.const import (
CONF_TIMEOUT,
CONF_URL,
CONF_WATCHDOG_TIMEOUT,
PLATFORM_ESP32,
PLATFORM_HOST,
PlatformFramework,
__version__,
)
from esphome.core import CORE, ID, Lambda, TimePeriodMilliseconds
from esphome.cpp_generator import MockObj, TemplateArgsType
import esphome.final_validate as fv
from esphome.helpers import IS_MACOS
from esphome.types import ConfigType
@@ -105,16 +103,18 @@ def default_watchdog_timeout(config: ConfigType) -> None:
The default never goes below the platform task watchdog, so a user who
widened `esp32.watchdog_timeout` keeps that window during requests.
When the task watchdog is disabled, the request watchdog cannot be set.
"""
if not CORE.is_esp32 or CONF_WATCHDOG_TIMEOUT in config:
if not CORE.is_esp32:
return
esp32.validate_task_wdt_enabled(config, CONF_WATCHDOG_TIMEOUT)
platform_ms = esp32.task_wdt_timeout_ms()
if platform_ms == 0 or CONF_WATCHDOG_TIMEOUT in config:
return
derived_ms = (
config[CONF_TIMEOUT].total_milliseconds * WATCHDOG_TIMEOUT_MULTIPLIER
+ WATCHDOG_TIMEOUT_MARGIN_MS
)
platform_ms = fv.full_config.get()[PLATFORM_ESP32][
CONF_WATCHDOG_TIMEOUT
].total_milliseconds
config[CONF_WATCHDOG_TIMEOUT] = TimePeriodMilliseconds(
milliseconds=max(derived_ms, platform_ms)
)
@@ -20,8 +20,6 @@
#include "esp_crt_bundle.h"
#endif
#include "esp_task_wdt.h"
namespace esphome::http_request {
ESPHOME_LOG_TAG(TAG, "http_request");
@@ -468,12 +466,7 @@ void HttpContainerIDF::end() {
this->client_ = nullptr;
}
void HttpContainerIDF::feed_wdt() {
// Tests to see if the executing task has a watchdog timer attached
if (esp_task_wdt_status(nullptr) == ESP_OK) {
App.feed_wdt();
}
}
void HttpContainerIDF::feed_wdt() { App.feed_wdt_if_watched(); }
} // namespace esphome::http_request
+81 -63
View File
@@ -1,7 +1,5 @@
#include "ld2410.h"
#include <algorithm>
#ifdef USE_NUMBER
#include "esphome/components/number/number.h"
#endif
@@ -121,9 +119,13 @@ static constexpr uint8_t UNCONFIRMED_INDEX = static_cast<uint8_t>(BgCorrectionRe
using ld24xx::CMD_FRAME_FOOTER;
using ld24xx::CMD_FRAME_HEADER;
using ld24xx::HEADER_FOOTER_SIZE;
using ld24xx::validate_header_footer;
// Smallest frames whose values can all be read: the last value, data footer and check, then the frame footer
static constexpr uint8_t PERIODIC_FRAME_MIN = DETECT_DISTANCE_HIGH + 3 + HEADER_FOOTER_SIZE;
static constexpr uint8_t ENGINEERING_FRAME_MIN = OUT_PIN_SENSOR + 3 + HEADER_FOOTER_SIZE;
// Data Header & Footer
static constexpr uint8_t DATA_FRAME_HEADER[HEADER_FOOTER_SIZE] = {0xF4, 0xF3, 0xF2, 0xF1};
static constexpr uint8_t DATA_FRAME_FOOTER[HEADER_FOOTER_SIZE] = {0xF8, 0xF7, 0xF6, 0xF5};
void LD2410Component::dump_config() {
char mac_s[MAC_ADDRESS_PRETTY_BUFFER_SIZE];
@@ -229,14 +231,13 @@ void LD2410Component::restart_and_read_all_info() {
}
void LD2410Component::loop() {
auto handler = [this](const uint8_t *frame, size_t len) { return this->handle_frame_(frame, len); };
this->read_frames<MAX_LINE_LENGTH>(this->reader_, handler, App.get_loop_component_start_time(), this->last_rx_);
this->read_available<MAX_LINE_LENGTH>([this](uint8_t b) { this->readline_(b); });
}
void LD2410Component::send_command_(uint8_t command, const uint8_t *command_value, uint8_t command_value_len) {
ESP_LOGV(TAG, "Sending COMMAND %02X", command);
// frame header bytes
this->write_array_progmem(CMD_FRAME_HEADER, sizeof(CMD_FRAME_HEADER));
this->write_array(CMD_FRAME_HEADER, sizeof(CMD_FRAME_HEADER));
// length bytes
uint8_t len = 2;
if (command_value != nullptr) {
@@ -250,27 +251,29 @@ void LD2410Component::send_command_(uint8_t command, const uint8_t *command_valu
this->write_array(command_value, command_value_len);
}
// frame footer bytes
this->write_array_progmem(CMD_FRAME_FOOTER, sizeof(CMD_FRAME_FOOTER));
this->write_array(CMD_FRAME_FOOTER, sizeof(CMD_FRAME_FOOTER));
if (command != CMD_ENABLE_CONF && command != CMD_DISABLE_CONF) {
delay(50); // NOLINT
}
}
bool LD2410Component::handle_periodic_data_(const uint8_t *frame, uint8_t len) {
void LD2410Component::handle_periodic_data_() {
// 4 frame header bytes + 2 length bytes + 1 data end byte + 1 crc byte + 4 frame footer bytes
// data header=0xAA, data footer=0x55, crc=0x00
if (len < 12 || frame[7] != HEADER || frame[len - 6] != FOOTER || frame[len - 5] != CHECK) {
return false;
if (this->buffer_pos_ < 12 || !ld2410::validate_header_footer(DATA_FRAME_HEADER, this->buffer_data_) ||
this->buffer_data_[7] != HEADER || this->buffer_data_[this->buffer_pos_ - 6] != FOOTER ||
this->buffer_data_[this->buffer_pos_ - 5] != CHECK) {
return;
}
/*
Data Type: 7th
0x01: Engineering mode
0x02: Normal mode
*/
bool engineering_mode = frame[DATA_TYPES] == 0x01;
if (len < (engineering_mode ? ENGINEERING_FRAME_MIN : PERIODIC_FRAME_MIN)) {
return false; // too short to hold the values
bool engineering_mode = this->buffer_data_[DATA_TYPES] == 0x01;
if (this->buffer_pos_ < (engineering_mode ? ENGINEERING_FRAME_MIN : PERIODIC_FRAME_MIN)) {
return; // too short to hold the values
}
#ifdef USE_SWITCH
PUBLISH_STATE(this->engineering_mode_switch_, engineering_mode);
@@ -282,7 +285,7 @@ bool LD2410Component::handle_periodic_data_(const uint8_t *frame, uint8_t len) {
0x02 = Still targets
0x03 = Moving+Still targets
*/
const uint8_t target_state = frame[TARGET_STATES];
const uint8_t target_state = this->buffer_data_[TARGET_STATES];
#ifdef USE_LD2410_BG_CORRECTION
// Pass or fail comes only from these frames
if (this->bg_correction_running_) {
@@ -309,13 +312,13 @@ bool LD2410Component::handle_periodic_data_(const uint8_t *frame, uint8_t len) {
*/
#ifdef USE_SENSOR
SAFE_PUBLISH_SENSOR(this->moving_target_distance_sensor_,
encode_uint16(frame[MOVING_TARGET_HIGH], frame[MOVING_TARGET_LOW]));
SAFE_PUBLISH_SENSOR(this->moving_target_energy_sensor_, frame[MOVING_ENERGY]);
encode_uint16(this->buffer_data_[MOVING_TARGET_HIGH], this->buffer_data_[MOVING_TARGET_LOW]));
SAFE_PUBLISH_SENSOR(this->moving_target_energy_sensor_, this->buffer_data_[MOVING_ENERGY]);
SAFE_PUBLISH_SENSOR(this->still_target_distance_sensor_,
encode_uint16(frame[STILL_TARGET_HIGH], frame[STILL_TARGET_LOW]));
SAFE_PUBLISH_SENSOR(this->still_target_energy_sensor_, frame[STILL_ENERGY]);
encode_uint16(this->buffer_data_[STILL_TARGET_HIGH], this->buffer_data_[STILL_TARGET_LOW]));
SAFE_PUBLISH_SENSOR(this->still_target_energy_sensor_, this->buffer_data_[STILL_ENERGY]);
SAFE_PUBLISH_SENSOR(this->detection_distance_sensor_,
encode_uint16(frame[DETECT_DISTANCE_HIGH], frame[DETECT_DISTANCE_LOW]));
encode_uint16(this->buffer_data_[DETECT_DISTANCE_HIGH], this->buffer_data_[DETECT_DISTANCE_LOW]));
if (engineering_mode) {
/*
@@ -324,18 +327,18 @@ bool LD2410Component::handle_periodic_data_(const uint8_t *frame, uint8_t len) {
Moving energy: 20~28th bytes
*/
for (uint8_t i = 0; i < TOTAL_GATES; i++) {
SAFE_PUBLISH_SENSOR(this->gate_move_sensors_[i], frame[MOVING_SENSOR_START + i]);
SAFE_PUBLISH_SENSOR(this->gate_move_sensors_[i], this->buffer_data_[MOVING_SENSOR_START + i]);
}
/*
Still energy: 29~37th bytes
*/
for (uint8_t i = 0; i < TOTAL_GATES; i++) {
SAFE_PUBLISH_SENSOR(this->gate_still_sensors_[i], frame[STILL_SENSOR_START + i]);
SAFE_PUBLISH_SENSOR(this->gate_still_sensors_[i], this->buffer_data_[STILL_SENSOR_START + i]);
}
/*
Light sensor: 38th bytes
*/
SAFE_PUBLISH_SENSOR(this->light_sensor_, frame[LIGHT_SENSOR]);
SAFE_PUBLISH_SENSOR(this->light_sensor_, this->buffer_data_[LIGHT_SENSOR]);
} else {
for (auto &gate_move_sensor : this->gate_move_sensors_) {
SAFE_PUBLISH_SENSOR_UNKNOWN(gate_move_sensor);
@@ -347,9 +350,9 @@ bool LD2410Component::handle_periodic_data_(const uint8_t *frame, uint8_t len) {
}
#endif
#ifdef USE_BINARY_SENSOR
PUBLISH_STATE(this->out_pin_presence_status_binary_sensor_, engineering_mode ? frame[OUT_PIN_SENSOR] == 0x01 : false);
PUBLISH_STATE(this->out_pin_presence_status_binary_sensor_,
engineering_mode ? this->buffer_data_[OUT_PIN_SENSOR] == 0x01 : false);
#endif
return true;
}
#ifdef USE_NUMBER
@@ -360,24 +363,24 @@ void set_number_value(number::Number *n, float value) {
}
#endif
bool LD2410Component::handle_ack_data_(const uint8_t *frame, uint8_t len) {
ESP_LOGV(TAG, "Handling ACK DATA for COMMAND %02X", frame[COMMAND]);
if (frame[COMMAND_STATUS] != 0x01) {
void LD2410Component::handle_ack_data_() {
ESP_LOGV(TAG, "Handling ACK DATA for COMMAND %02X", this->buffer_data_[COMMAND]);
if (this->buffer_data_[COMMAND_STATUS] != 0x01) {
ESP_LOGE(TAG, "Invalid status");
return false;
return;
}
if (frame[8] || frame[9]) {
ESP_LOGW(TAG, "Invalid command: %02X, %02X", frame[8], frame[9]);
if (this->buffer_data_[8] || this->buffer_data_[9]) {
ESP_LOGW(TAG, "Invalid command: %02X, %02X", this->buffer_data_[8], this->buffer_data_[9]);
#ifdef USE_LD2410_BG_CORRECTION
// Only a refused start ends the run; a refused poll waits for the next poll or the deadline
if (frame[COMMAND] == CMD_BG_CORRECTION_START) {
if (this->buffer_data_[COMMAND] == CMD_BG_CORRECTION_START) {
this->finish_background_correction_(BgCorrectionResult::BG_CORRECTION_RESULT_REFUSED);
}
#endif
return true;
return;
}
switch (frame[COMMAND]) {
switch (this->buffer_data_[COMMAND]) {
case CMD_ENABLE_CONF:
ESP_LOGV(TAG, "Enable conf");
break;
@@ -397,10 +400,10 @@ bool LD2410Component::handle_ack_data_(const uint8_t *frame, uint8_t len) {
break;
case CMD_QUERY_VERSION: {
if (len < 12 + sizeof(this->version_) + HEADER_FOOTER_SIZE) {
if (this->buffer_pos_ < 12 + sizeof(this->version_) + HEADER_FOOTER_SIZE) {
break; // too short to hold a version
}
std::memcpy(this->version_, &frame[12], sizeof(this->version_));
std::memcpy(this->version_, &this->buffer_data_[12], sizeof(this->version_));
char version_s[20];
ld24xx::format_version_str(this->version_, version_s);
ESP_LOGV(TAG, "Firmware version: %s", version_s);
@@ -411,12 +414,12 @@ bool LD2410Component::handle_ack_data_(const uint8_t *frame, uint8_t len) {
}
case CMD_QUERY_DISTANCE_RESOLUTION: {
if (len < 11 + HEADER_FOOTER_SIZE) {
if (this->buffer_pos_ < 11 + HEADER_FOOTER_SIZE) {
break; // too short to hold the resolution
}
const auto index = ld24xx::find_index(DISTANCE_RESOLUTIONS, frame[10]);
const auto index = ld24xx::find_index(DISTANCE_RESOLUTIONS, this->buffer_data_[10]);
if (!index) {
ESP_LOGW(TAG, "Unknown distance resolution %u", frame[10]);
ESP_LOGW(TAG, "Unknown distance resolution %u", this->buffer_data_[10]);
break;
}
ESP_LOGV(TAG, "Distance resolution: %s",
@@ -428,12 +431,12 @@ bool LD2410Component::handle_ack_data_(const uint8_t *frame, uint8_t len) {
}
case CMD_QUERY_LIGHT_CONTROL: {
if (len < 13 + HEADER_FOOTER_SIZE) {
if (this->buffer_pos_ < 13 + HEADER_FOOTER_SIZE) {
break; // too short to hold the light settings
}
this->light_function_ = frame[10];
this->light_threshold_ = frame[11];
this->out_pin_level_ = frame[12];
this->light_function_ = this->buffer_data_[10];
this->light_threshold_ = this->buffer_data_[11];
this->out_pin_level_ = this->buffer_data_[12];
ESP_LOGV(TAG, "Light function: %u, threshold: %u, out pin level: %u", this->light_function_,
this->light_threshold_, this->out_pin_level_);
#ifdef USE_SELECT
@@ -446,11 +449,11 @@ bool LD2410Component::handle_ack_data_(const uint8_t *frame, uint8_t len) {
break;
}
case CMD_QUERY_MAC_ADDRESS: {
if (len < 20) {
if (this->buffer_pos_ < 20) {
break; // too short to hold a MAC address
}
this->bluetooth_on_ = ld24xx::store_mac(&frame[10], this->mac_address_);
this->bluetooth_on_ = ld24xx::store_mac(&this->buffer_data_[10], this->mac_address_);
char mac_s[MAC_ADDRESS_PRETTY_BUFFER_SIZE];
const char *mac_str = ld24xx::format_mac_str(this->mac_address_, mac_s);
@@ -474,7 +477,7 @@ bool LD2410Component::handle_ack_data_(const uint8_t *frame, uint8_t len) {
break;
case CMD_BG_CORRECTION_QUERY: {
const uint8_t status = frame[10];
const uint8_t status = this->buffer_data_[10];
ESP_LOGV(TAG, "Background correction status: %u", status);
// The module stops at idle after a fail frame; idle before the run starts is ignored
if (status == BG_CORRECTION_COMPLETED ||
@@ -503,55 +506,70 @@ bool LD2410Component::handle_ack_data_(const uint8_t *frame, uint8_t len) {
break;
case CMD_QUERY: { // Query parameters response
if (len < 38) {
if (this->buffer_pos_ < 38) {
break; // too short to hold the parameters
}
if (frame[10] != HEADER)
return true; // value head=0xAA
if (this->buffer_data_[10] != HEADER)
return; // value head=0xAA
#ifdef USE_NUMBER
/*
Moving distance range: 13th byte
Still distance range: 14th byte
*/
set_number_value(this->max_move_distance_gate_number_, frame[12]);
set_number_value(this->max_still_distance_gate_number_, frame[13]);
set_number_value(this->max_move_distance_gate_number_, this->buffer_data_[12]);
set_number_value(this->max_still_distance_gate_number_, this->buffer_data_[13]);
/*
Moving Sensitivities: 15~23th bytes
*/
for (size_t i = 0; i < TOTAL_GATES; i++) {
set_number_value(this->gate_move_threshold_numbers_[i], frame[14 + i]);
set_number_value(this->gate_move_threshold_numbers_[i], this->buffer_data_[14 + i]);
}
/*
Still Sensitivities: 24~32th bytes
*/
for (size_t i = 0; i < TOTAL_GATES; i++) {
set_number_value(this->gate_still_threshold_numbers_[i], frame[23 + i]);
set_number_value(this->gate_still_threshold_numbers_[i], this->buffer_data_[23 + i]);
}
/*
None Duration: 33~34th bytes
*/
set_number_value(this->timeout_number_, encode_uint16(frame[33], frame[32]));
set_number_value(this->timeout_number_, encode_uint16(this->buffer_data_[33], this->buffer_data_[32]));
#endif
break;
}
default:
break;
}
return true;
}
// Handles the frame when its footer is in place; returns false otherwise
bool LD2410Component::handle_frame_(const uint8_t *frame, uint8_t len) {
if (!ld24xx::has_footer(frame, len)) {
ESP_LOGW(TAG, "Invalid footer");
void LD2410Component::readline_(int readch) {
if (readch < 0) {
return; // No data available
}
// Periodic and command frames both carry their count after the header
ld24xx::feed_frame_byte(
this->buffer_data_, this->buffer_pos_, readch, DATA_FRAME_HEADER, ld24xx::cmd_frame_length,
[this](uint8_t frame_len) { return this->handle_frame_(frame_len); }, []() { ESP_LOGW(TAG, "Invalid length"); });
}
// Dispatches the frame of frame_len bytes at the start of the buffer when its footer is in place
bool LD2410Component::handle_frame_(uint8_t frame_len) {
this->buffer_pos_ = frame_len; // the handlers take the frame length from here
const uint8_t *tail = &this->buffer_data_[frame_len - HEADER_FOOTER_SIZE];
if (this->buffer_data_[0] == DATA_FRAME_HEADER[0]) {
if (!ld2410::validate_header_footer(DATA_FRAME_FOOTER, tail)) {
return false;
}
ESP_LOGV_HEX(TAG, this->buffer_data_, this->buffer_pos_, MAX_LINE_LENGTH, "Handling Periodic Data: %s");
this->handle_periodic_data_();
return true;
}
if (!ld2410::validate_header_footer(CMD_FRAME_FOOTER, tail)) {
return false;
}
if (frame[0] == ld24xx::DataFrameFormat::FIRST) {
ESP_LOGV_HEX(TAG, frame, len, MAX_LINE_LENGTH, "Handling Periodic Data: %s");
return this->handle_periodic_data_(frame, len);
}
ESP_LOGV_HEX(TAG, frame, len, MAX_LINE_LENGTH, "Handling Ack Data: %s");
return this->handle_ack_data_(frame, len);
ESP_LOGV_HEX(TAG, this->buffer_data_, this->buffer_pos_, MAX_LINE_LENGTH, "Handling Ack Data: %s");
this->handle_ack_data_();
return true;
}
void LD2410Component::set_config_mode_(bool enable) {
+6 -6
View File
@@ -24,7 +24,6 @@
#endif
#include "esphome/components/ld24xx/ld24xx.h"
#include "esphome/components/uart/uart.h"
#include "esphome/core/frame_reader.h"
#include "esphome/core/helpers.h"
#include <array>
@@ -146,9 +145,10 @@ class LD2410Component final : public Component, public uart::UARTDevice {
protected:
void send_command_(uint8_t command_str, const uint8_t *command_value, uint8_t command_value_len);
void set_config_mode_(bool enable);
bool handle_periodic_data_(const uint8_t *frame, uint8_t len);
bool handle_ack_data_(const uint8_t *frame, uint8_t len);
bool handle_frame_(const uint8_t *frame, uint8_t len);
void handle_periodic_data_();
void handle_ack_data_();
bool handle_frame_(uint8_t frame_len);
void readline_(int readch);
void query_parameters_();
void get_version_();
void get_mac_();
@@ -161,11 +161,11 @@ class LD2410Component final : public Component, public uart::UARTDevice {
void publish_calibration_result_(BgCorrectionResult result);
#endif
uint32_t last_rx_{0};
uint8_t light_function_ = 0;
uint8_t light_threshold_ = 0;
uint8_t out_pin_level_ = 0;
LengthFrameReader<MAX_LINE_LENGTH, ld24xx::CmdFrameFormat, ld24xx::DataFrameFormat> reader_;
uint8_t buffer_pos_ = 0; // where to resume processing/populating buffer
uint8_t buffer_data_[MAX_LINE_LENGTH];
uint8_t mac_address_[MAC_ADDRESS_SIZE] = {0, 0, 0, 0, 0, 0};
uint8_t version_[6] = {0, 0, 0, 0, 0, 0};
bool bluetooth_on_{false};
+139 -40
View File
@@ -9,12 +9,16 @@ namespace esphome::ld2410s {
ESPHOME_LOG_TAG(TAG, "ld2410s");
using ld24xx::CMD_FRAME_FOOTER;
using ld24xx::CMD_FRAME_HEADER;
static constexpr uint8_t SHORT_DATA_FRAME_FOOTER = 0x62;
using ld24xx::HEADER_FOOTER_SIZE;
// After the header and the two byte length
static constexpr uint8_t LONG_PAYLOAD_POS = ld24xx::PayloadLength::KNOWN_AT;
static constexpr uint8_t SHORT_DATA_FRAME_HEADER = 0x6E;
static constexpr uint8_t SHORT_DATA_FRAME_FOOTER[] PROGMEM = {0x62};
static constexpr uint8_t SHORT_DATA_FRAME_SIZE = 5; // header, state, distance low, distance high, footer
static constexpr uint8_t STD_DATA_FRAME_HEADER[] PROGMEM = {0xF4, 0xF3, 0xF2, 0xF1};
static constexpr uint8_t STD_DATA_FRAME_FOOTER[] PROGMEM = {0xF8, 0xF7, 0xF6, 0xF5};
static constexpr uint8_t CMD_FRAME_HEADER[] PROGMEM = {0xFD, 0xFC, 0xFB, 0xFA};
static constexpr uint8_t CMD_FRAME_FOOTER[] PROGMEM = {0x04, 0x03, 0x02, 0x01};
static constexpr uint8_t LONG_HEADER_SIZE = 4;
static constexpr uint8_t LENGTH_FIELD_SIZE = 2;
static constexpr uint8_t LONG_PAYLOAD_POS = LONG_HEADER_SIZE + LENGTH_FIELD_SIZE;
static constexpr uint16_t CMD_CONFIRMATION = 0x0100; // set in the command word of an acknowledgement
static constexpr uint16_t CONFIG_MODE_START_CMD = 0x00FF;
@@ -94,11 +98,10 @@ void LD2410SComponent::dump_config() {
}
void LD2410SComponent::loop() {
const uint32_t now = App.get_loop_component_start_time();
auto handler = [this](const uint8_t *frame, size_t len) { return this->handle_frame_(frame, len); };
if (!this->read_frames<16>(this->reader_, handler, now, this->last_rx_)) {
if (!this->read_available<16>([this](uint8_t b) { this->receive_byte_(b); })) {
ESP_LOGV(TAG, "UART read failed");
}
const uint32_t now = App.get_loop_component_start_time();
if (this->sequence_ == nullptr) {
if (!this->init_done_) {
this->start_sequence_(INIT_SEQUENCE, INIT_SEQUENCE_LENGTH, now);
@@ -208,7 +211,7 @@ void LD2410SComponent::run_sequence_(uint32_t now) {
void LD2410SComponent::send_command_(uint16_t command) {
uint8_t frame[CMD_FRAME_MAX_SIZE];
progmem_memcpy(frame, CMD_FRAME_HEADER, HEADER_FOOTER_SIZE);
progmem_memcpy(frame, CMD_FRAME_HEADER, LONG_HEADER_SIZE);
uint8_t pos = LONG_PAYLOAD_POS;
put_le16(frame + pos, command);
pos += 2;
@@ -237,41 +240,138 @@ void LD2410SComponent::send_command_(uint16_t command) {
default:
break;
}
put_le16(frame + HEADER_FOOTER_SIZE, pos - LONG_PAYLOAD_POS); // payload length
put_le16(frame + LONG_HEADER_SIZE, pos - LONG_PAYLOAD_POS); // payload length
progmem_memcpy(&frame[pos], CMD_FRAME_FOOTER, sizeof(CMD_FRAME_FOOTER));
pos += sizeof(CMD_FRAME_FOOTER);
this->write_array(frame, pos);
ESP_LOGVV_HEX(TAG, frame, pos, CMD_FRAME_MAX_SIZE, "> %s");
#if ESPHOME_LOG_LEVEL >= ESPHOME_LOG_LEVEL_VERY_VERBOSE
char hex_buf[format_hex_pretty_size(CMD_FRAME_MAX_SIZE)];
ESP_LOGVV(TAG, "> %s", format_hex_pretty_to(hex_buf, frame, pos, ' '));
#endif
}
bool LD2410SComponent::handle_frame_(const uint8_t *frame, uint8_t len) {
const uint8_t type = frame[0];
const bool short_frame = type == ShortDataFrameFormat::FIRST;
if (short_frame ? frame[len - 1] != SHORT_DATA_FRAME_FOOTER : !ld24xx::has_footer(frame, len)) {
ESP_LOGV_HEX(TAG, frame, len, RX_BUFFER_SIZE, "Footer does not match header: %s");
void LD2410SComponent::receive_byte_(uint8_t byte) {
if (this->rx_len_ >= RX_BUFFER_SIZE) {
this->reset_frame_();
}
this->rx_buffer_[this->rx_len_++] = byte;
if (this->frame_type_ == FrameType::NONE) {
if (this->rx_len_ == 1 && byte == SHORT_DATA_FRAME_HEADER) {
this->frame_type_ = FrameType::SHORT_DATA;
this->expected_len_ = SHORT_DATA_FRAME_SIZE;
return;
}
// The two long headers are told apart once all four bytes are in
uint8_t std_header[LONG_HEADER_SIZE];
uint8_t cmd_header[LONG_HEADER_SIZE];
progmem_memcpy(std_header, STD_DATA_FRAME_HEADER, LONG_HEADER_SIZE);
progmem_memcpy(cmd_header, CMD_FRAME_HEADER, LONG_HEADER_SIZE);
const bool std_prefix = memcmp(this->rx_buffer_, std_header, this->rx_len_) == 0;
const bool cmd_prefix = memcmp(this->rx_buffer_, cmd_header, this->rx_len_) == 0;
if (std_prefix || cmd_prefix) {
if (this->rx_len_ == LONG_HEADER_SIZE) {
this->frame_type_ = std_prefix ? FrameType::STD_DATA : FrameType::COMMAND;
}
return;
}
// Not a header: drop what was collected, and try this byte as the start of the next frame
const bool retry = this->rx_len_ > 1;
this->reset_frame_();
if (retry) {
this->receive_byte_(byte);
}
return;
}
if (this->expected_len_ == 0) {
if (this->rx_len_ < LONG_PAYLOAD_POS) {
return;
}
// Standard data and command frames carry a little-endian payload length after the header
const uint16_t payload_len =
encode_uint16(this->rx_buffer_[LONG_HEADER_SIZE + 1], this->rx_buffer_[LONG_HEADER_SIZE]);
if (payload_len > RX_BUFFER_SIZE - LONG_PAYLOAD_POS - sizeof(CMD_FRAME_FOOTER)) {
ESP_LOGV(TAG, "Dropping frame with a %u byte payload, larger than the receive buffer", payload_len);
this->count_dropped_frame_();
this->reset_frame_();
return;
}
this->expected_len_ = LONG_PAYLOAD_POS + payload_len + sizeof(CMD_FRAME_FOOTER);
return;
}
if (this->rx_len_ < this->expected_len_) {
return;
}
if (this->handle_frame_() || this->rescanning_) {
this->reset_frame_();
return;
}
// A truncated long frame swallowed the bytes that followed it; try them again as new frames, once
uint8_t rest[RX_BUFFER_SIZE];
const uint8_t rest_len = this->rx_len_ - 1;
memcpy(rest, &this->rx_buffer_[1], rest_len);
this->reset_frame_();
this->rescanning_ = true;
for (uint8_t i = 0; i < rest_len; i++) {
this->receive_byte_(rest[i]);
}
this->rescanning_ = false;
}
bool LD2410SComponent::handle_frame_() {
const uint8_t *footer_src;
uint8_t footer_size = sizeof(CMD_FRAME_FOOTER);
uint8_t payload_pos = LONG_PAYLOAD_POS;
switch (this->frame_type_) {
case FrameType::SHORT_DATA:
footer_src = SHORT_DATA_FRAME_FOOTER;
footer_size = sizeof(SHORT_DATA_FRAME_FOOTER);
payload_pos = 1;
break;
case FrameType::STD_DATA:
footer_src = STD_DATA_FRAME_FOOTER;
break;
default:
footer_src = CMD_FRAME_FOOTER;
break;
}
uint8_t footer[sizeof(CMD_FRAME_FOOTER)];
progmem_memcpy(footer, footer_src, footer_size);
if (memcmp(&this->rx_buffer_[this->rx_len_ - footer_size], footer, footer_size) != 0) {
#if ESPHOME_LOG_LEVEL >= ESPHOME_LOG_LEVEL_VERBOSE
char hex_buf[format_hex_pretty_size(RX_BUFFER_SIZE)];
ESP_LOGV(TAG, "Footer does not match header: %s",
format_hex_pretty_to(hex_buf, this->rx_buffer_, this->rx_len_, ' '));
#endif
this->count_dropped_frame_();
return false;
}
const uint8_t payload_pos = short_frame ? 1 : LONG_PAYLOAD_POS;
const uint8_t *payload = &frame[payload_pos];
const uint16_t payload_len = len - payload_pos - (short_frame ? 1 : HEADER_FOOTER_SIZE);
if (type == ld24xx::CmdFrameFormat::FIRST) {
return this->handle_command_ack_(payload, payload_len);
const uint8_t *payload = &this->rx_buffer_[payload_pos];
const uint16_t payload_len = this->rx_len_ - payload_pos - footer_size;
switch (this->frame_type_) {
case FrameType::SHORT_DATA:
case FrameType::STD_DATA:
if (!this->handle_data_frame_(payload, payload_len)) {
this->count_dropped_frame_();
break;
}
this->last_frame_at_ = App.get_loop_component_start_time();
this->dropped_frames_ = 0;
break;
default:
this->handle_command_ack_(payload, payload_len);
break;
}
if (!this->handle_data_frame_(payload, payload_len, type == ld24xx::DataFrameFormat::FIRST)) {
this->count_dropped_frame_();
return false;
}
this->last_frame_at_ = App.get_loop_component_start_time();
this->dropped_frames_ = 0;
return true;
}
bool LD2410SComponent::handle_data_frame_(const uint8_t *payload, uint16_t len, bool standard) {
bool LD2410SComponent::handle_data_frame_(const uint8_t *payload, uint16_t len) {
// Short frames carry [state][distance low][distance high]; standard frames put a type byte first, and the
// module only sends them before the init sequence switches it to minimal output
const uint8_t *data = payload;
if (standard) {
if (this->frame_type_ == FrameType::STD_DATA) {
if (len < 4 || payload[0] != DATA_TYPE_STANDARD) {
if (len >= 1) {
ESP_LOGV(TAG, "Ignoring data frame type %02X", payload[0]);
@@ -292,21 +392,21 @@ bool LD2410SComponent::handle_data_frame_(const uint8_t *payload, uint16_t len,
return true;
}
bool LD2410SComponent::handle_command_ack_(const uint8_t *payload, uint16_t len) {
void LD2410SComponent::handle_command_ack_(const uint8_t *payload, uint16_t len) {
if (len < 4) {
ESP_LOGV(TAG, "Acknowledgement too short (%u bytes)", len);
return false;
return;
}
const uint16_t command_word = encode_uint16(payload[1], payload[0]);
const uint16_t ack = encode_uint16(payload[3], payload[2]);
if (!this->awaiting_ack_) {
ESP_LOGV(TAG, "Unexpected acknowledgement %04X", command_word);
return true;
return;
}
const uint16_t command = this->current_command_();
if (command_word != (command | CMD_CONFIRMATION)) {
ESP_LOGV(TAG, "Unexpected acknowledgement %04X", command_word);
return true;
return;
}
#ifdef USE_LD2410S_PARAMETERS
if (ack != 0 && command == PARAMETERS_WRITE_CMD) {
@@ -315,29 +415,29 @@ bool LD2410SComponent::handle_command_ack_(const uint8_t *payload, uint16_t len)
ESP_LOGW(TAG, "Module rejected the parameters, ack %04X", ack);
this->send_command_(CONFIG_MODE_END_CMD);
this->restart_init_(App.get_loop_component_start_time() + COMMAND_GAP_MS);
return true;
return;
}
#endif
if (ack != 0) {
// Leave the command pending so the timeout resends it
ESP_LOGW(TAG, "Module rejected command %04X, ack %04X", command, ack);
return true;
return;
}
// A short answer leaves the read pending the same way, so init cannot finish without the values
#ifdef USE_LD2410S_PARAMETERS
if (command == PARAMETERS_READ_CMD && !this->handle_parameters_(&payload[4], len - 4))
return false;
return;
#endif
#ifdef USE_LD2410S_VERSION
if (command == VERSION_READ_CMD && !this->handle_version_(&payload[4], len - 4))
return false;
return;
#endif
this->awaiting_ack_ = false;
this->step_timeouts_ = 0;
const uint32_t now = App.get_loop_component_start_time();
this->next_send_at_ = now + COMMAND_GAP_MS;
if (++this->step_ < this->sequence_len_) {
return true;
return;
}
const bool was_init = this->sequence_ == INIT_SEQUENCE;
this->sequence_ = nullptr;
@@ -347,7 +447,6 @@ bool LD2410SComponent::handle_command_ack_(const uint8_t *payload, uint16_t len)
this->last_frame_at_ = now;
this->status_clear_warning();
}
return true;
}
#ifdef USE_LD2410S_PARAMETERS
+18 -10
View File
@@ -2,7 +2,6 @@
#include "esphome/core/component.h"
#include "esphome/core/defines.h"
#include "esphome/core/frame_reader.h"
#include "esphome/core/helpers.h"
#include "esphome/components/uart/uart.h"
#ifdef USE_BINARY_SENSOR
@@ -10,8 +9,8 @@
#endif
#ifdef USE_SENSOR
#include "esphome/components/sensor/sensor.h"
#endif
#include "esphome/components/ld24xx/ld24xx.h"
#endif
#ifdef USE_LD2410S_VERSION
#include "esphome/components/text_sensor/text_sensor.h"
#endif
@@ -27,8 +26,6 @@ namespace esphome::ld2410s {
// Largest frame read here is the standard data frame: 4 header + 2 length + payload + 4 footer
static constexpr size_t RX_BUFFER_SIZE = 96;
static_assert(RX_BUFFER_SIZE <= 255, "frame lengths are kept in a byte");
// Minimal output: header, state, distance low, distance high, footer
using ShortDataFrameFormat = FrameFormat<FrameHeader<0x6E>, FrameFixedLength<5>>;
/// The module's common parameters, in the order the parameter read answers them
enum class Parameter : uint8_t {
@@ -81,11 +78,14 @@ class LD2410SComponent final : public Component, public uart::UARTDevice {
#endif
protected:
/// Returns false for bytes that are not a real frame.
bool handle_frame_(const uint8_t *frame, uint8_t len);
enum class FrameType : uint8_t { NONE, SHORT_DATA, STD_DATA, COMMAND };
void receive_byte_(uint8_t byte);
/// Returns false when the footer does not match, so the bytes can be scanned again.
bool handle_frame_();
/// Returns false for a data frame whose state byte is impossible, so it counts as dropped.
bool handle_data_frame_(const uint8_t *payload, uint16_t len, bool standard);
bool handle_command_ack_(const uint8_t *payload, uint16_t len);
bool handle_data_frame_(const uint8_t *payload, uint16_t len);
void handle_command_ack_(const uint8_t *payload, uint16_t len);
#ifdef USE_LD2410S_PARAMETERS
bool handle_parameters_(const uint8_t *values, uint16_t len);
#endif
@@ -97,6 +97,11 @@ class LD2410SComponent final : public Component, public uart::UARTDevice {
this->dropped_frames_++;
}
}
void reset_frame_() {
this->rx_len_ = 0;
this->expected_len_ = 0;
this->frame_type_ = FrameType::NONE;
}
void start_sequence_(const uint16_t *sequence, uint8_t length, uint32_t now);
void restart_init_(uint32_t at);
void run_sequence_(uint32_t now);
@@ -114,16 +119,19 @@ class LD2410SComponent final : public Component, public uart::UARTDevice {
const uint16_t *sequence_{nullptr}; // PROGMEM; null while nothing is being sent
uint32_t next_send_at_{0};
uint32_t last_frame_at_{0};
uint32_t last_rx_{0};
#ifdef USE_LD2410S_PARAMETERS
uint8_t parameters_[PARAMETER_COUNT]{}; // all fit a byte: gates, seconds, tenths of a hertz, speed
#endif
LengthFrameReader<RX_BUFFER_SIZE, ld24xx::CmdFrameFormat, ld24xx::DataFrameFormat, ShortDataFrameFormat> reader_;
uint8_t rx_buffer_[RX_BUFFER_SIZE];
uint8_t rx_len_{0};
uint8_t expected_len_{0};
uint8_t sequence_len_{0};
uint8_t step_{0};
uint8_t step_timeouts_{0};
uint8_t dropped_frames_{0}; // since the last accepted data frame, saturates at 255
FrameType frame_type_{FrameType::NONE};
bool awaiting_ack_{false};
bool rescanning_{false};
bool init_done_{false};
#ifdef USE_LD2410S_PARAMETERS
bool parameters_known_{false}; // the module answered the parameter read at least once
+83 -64
View File
@@ -113,9 +113,13 @@ static constexpr uint8_t STILL_BITMASK = 0x02;
using ld24xx::CMD_FRAME_FOOTER;
using ld24xx::CMD_FRAME_HEADER;
using ld24xx::HEADER_FOOTER_SIZE;
using ld24xx::validate_header_footer;
// Smallest frames whose values can all be read: the last value, data footer and check, then the frame footer
static constexpr uint8_t PERIODIC_FRAME_MIN = STILL_ENERGY + 3 + HEADER_FOOTER_SIZE;
static constexpr uint8_t ENGINEERING_FRAME_MIN = LIGHT_SENSOR + 3 + HEADER_FOOTER_SIZE;
// Data Header & Footer
static constexpr uint8_t DATA_FRAME_HEADER[HEADER_FOOTER_SIZE] = {0xF4, 0xF3, 0xF2, 0xF1};
static constexpr uint8_t DATA_FRAME_FOOTER[HEADER_FOOTER_SIZE] = {0xF8, 0xF7, 0xF6, 0xF5};
static constexpr uint8_t GATE_SENS_ACK_SIZE = ACK_PAYLOAD + TOTAL_GATES + HEADER_FOOTER_SIZE;
void LD2412Component::dump_config() {
@@ -230,14 +234,13 @@ void LD2412Component::restart_and_read_all_info() {
}
void LD2412Component::loop() {
auto handler = [this](const uint8_t *frame, size_t len) { return this->handle_frame_(frame, len); };
this->read_frames<MAX_LINE_LENGTH>(this->reader_, handler, App.get_loop_component_start_time(), this->last_rx_);
this->read_available<MAX_LINE_LENGTH>([this](uint8_t b) { this->readline_(b); });
}
void LD2412Component::send_command_(uint8_t command, const uint8_t *command_value, uint8_t command_value_len) {
ESP_LOGV(TAG, "Sending COMMAND %02X", command);
// frame header bytes
this->write_array_progmem(CMD_FRAME_HEADER, HEADER_FOOTER_SIZE);
this->write_array(CMD_FRAME_HEADER, HEADER_FOOTER_SIZE);
// length bytes
uint8_t len = 2;
if (command_value != nullptr) {
@@ -252,7 +255,7 @@ void LD2412Component::send_command_(uint8_t command, const uint8_t *command_valu
this->write_array(command_value, command_value_len);
}
// frame footer bytes
this->write_array_progmem(CMD_FRAME_FOOTER, HEADER_FOOTER_SIZE);
this->write_array(CMD_FRAME_FOOTER, HEADER_FOOTER_SIZE);
if (command != CMD_ENABLE_CONF && command != CMD_DISABLE_CONF) {
delay(30); // NOLINT
@@ -260,20 +263,21 @@ void LD2412Component::send_command_(uint8_t command, const uint8_t *command_valu
delay(20); // NOLINT
}
bool LD2412Component::handle_periodic_data_(const uint8_t *frame, uint8_t len) {
void LD2412Component::handle_periodic_data_() {
// 4 frame header bytes + 2 length bytes + 1 data end byte + 1 crc byte + 4 frame footer bytes
// data header=0xAA, data footer=0x55, crc=0x00
if (len < 12 || frame[7] != HEADER || frame[len - 6] != FOOTER) {
return false;
if (this->buffer_pos_ < 12 || !ld2412::validate_header_footer(DATA_FRAME_HEADER, this->buffer_data_) ||
this->buffer_data_[7] != HEADER || this->buffer_data_[this->buffer_pos_ - 6] != FOOTER) {
return;
}
/*
Data Type: 7th
0x01: Engineering mode
0x02: Normal mode
*/
bool engineering_mode = frame[DATA_TYPES] == 0x01;
if (len < PERIODIC_FRAME_MIN) {
return false; // too short to hold the values
bool engineering_mode = this->buffer_data_[DATA_TYPES] == 0x01;
if (this->buffer_pos_ < PERIODIC_FRAME_MIN) {
return; // too short to hold the values
}
#ifdef USE_SWITCH
PUBLISH_STATE(this->engineering_mode_switch_, engineering_mode);
@@ -287,7 +291,7 @@ bool LD2412Component::handle_periodic_data_(const uint8_t *frame, uint8_t len) {
0x02 = Still targets
0x03 = Moving+Still targets
*/
char target_state = frame[TARGET_STATES];
char target_state = this->buffer_data_[TARGET_STATES];
PUBLISH_STATE(this->target_binary_sensor_, target_state != 0x00);
PUBLISH_STATE(this->moving_target_binary_sensor_, target_state & MOVE_BITMASK);
PUBLISH_STATE(this->still_target_binary_sensor_, target_state & STILL_BITMASK);
@@ -301,23 +305,24 @@ bool LD2412Component::handle_periodic_data_(const uint8_t *frame, uint8_t len) {
*/
#ifdef USE_SENSOR
SAFE_PUBLISH_SENSOR(this->moving_target_distance_sensor_,
encode_uint16(frame[MOVING_TARGET_HIGH], frame[MOVING_TARGET_LOW]));
SAFE_PUBLISH_SENSOR(this->moving_target_energy_sensor_, frame[MOVING_ENERGY]);
encode_uint16(this->buffer_data_[MOVING_TARGET_HIGH], this->buffer_data_[MOVING_TARGET_LOW]));
SAFE_PUBLISH_SENSOR(this->moving_target_energy_sensor_, this->buffer_data_[MOVING_ENERGY]);
SAFE_PUBLISH_SENSOR(this->still_target_distance_sensor_,
encode_uint16(frame[STILL_TARGET_HIGH], frame[STILL_TARGET_LOW]));
SAFE_PUBLISH_SENSOR(this->still_target_energy_sensor_, frame[STILL_ENERGY]);
encode_uint16(this->buffer_data_[STILL_TARGET_HIGH], this->buffer_data_[STILL_TARGET_LOW]));
SAFE_PUBLISH_SENSOR(this->still_target_energy_sensor_, this->buffer_data_[STILL_ENERGY]);
if (this->detection_distance_sensor_.has_sensor()) {
int new_detect_distance = 0;
if (target_state != 0x00 && (target_state & MOVE_BITMASK)) {
new_detect_distance = encode_uint16(frame[MOVING_TARGET_HIGH], frame[MOVING_TARGET_LOW]);
new_detect_distance =
encode_uint16(this->buffer_data_[MOVING_TARGET_HIGH], this->buffer_data_[MOVING_TARGET_LOW]);
} else if (target_state != 0x00) {
new_detect_distance = encode_uint16(frame[STILL_TARGET_HIGH], frame[STILL_TARGET_LOW]);
new_detect_distance = encode_uint16(this->buffer_data_[STILL_TARGET_HIGH], this->buffer_data_[STILL_TARGET_LOW]);
}
this->detection_distance_sensor_.publish_state_if_not_dup(new_detect_distance);
}
if (engineering_mode) {
if (len < ENGINEERING_FRAME_MIN) {
ESP_LOGW(TAG, "Engineering mode packet too short: %u", len);
if (this->buffer_pos_ < ENGINEERING_FRAME_MIN) {
ESP_LOGW(TAG, "Engineering mode packet too short: %u", this->buffer_pos_);
} else {
/*
Moving distance range: 18th byte
@@ -325,18 +330,18 @@ bool LD2412Component::handle_periodic_data_(const uint8_t *frame, uint8_t len) {
Moving energy: 20~28th bytes
*/
for (uint8_t i = 0; i < TOTAL_GATES; i++) {
SAFE_PUBLISH_SENSOR(this->gate_move_sensors_[i], frame[MOVING_SENSOR_START + i]);
SAFE_PUBLISH_SENSOR(this->gate_move_sensors_[i], this->buffer_data_[MOVING_SENSOR_START + i]);
}
/*
Still energy: 29~37th bytes
*/
for (uint8_t i = 0; i < TOTAL_GATES; i++) {
SAFE_PUBLISH_SENSOR(this->gate_still_sensors_[i], frame[STILL_SENSOR_START + i]);
SAFE_PUBLISH_SENSOR(this->gate_still_sensors_[i], this->buffer_data_[STILL_SENSOR_START + i]);
}
/*
Light sensor value
*/
SAFE_PUBLISH_SENSOR(this->light_sensor_, frame[LIGHT_SENSOR]);
SAFE_PUBLISH_SENSOR(this->light_sensor_, this->buffer_data_[LIGHT_SENSOR]);
}
} else {
for (auto &gate_move_sensor : this->gate_move_sensors_) {
@@ -354,7 +359,6 @@ bool LD2412Component::handle_periodic_data_(const uint8_t *frame, uint8_t len) {
this->query_dynamic_background_correction_();
this->set_config_mode_(false);
}
return true;
}
#ifdef USE_NUMBER
@@ -374,18 +378,18 @@ static constexpr uint32_t GATE_QUERY_TIMEOUT_ID = 0;
static constexpr uint32_t BASIC_CONF_TIMEOUT_ID = 2;
static constexpr uint32_t QUERY_TIMEOUT_MS = 2000;
bool LD2412Component::handle_ack_data_(const uint8_t *frame, uint8_t len) {
ESP_LOGV(TAG, "Handling ACK DATA for COMMAND %02X", frame[COMMAND]);
if (frame[COMMAND_STATUS] != 0x01) {
void LD2412Component::handle_ack_data_() {
ESP_LOGV(TAG, "Handling ACK DATA for COMMAND %02X", this->buffer_data_[COMMAND]);
if (this->buffer_data_[COMMAND_STATUS] != 0x01) {
ESP_LOGW(TAG, "Invalid status");
return false;
return;
}
if (frame[8] || frame[9]) {
ESP_LOGW(TAG, "Invalid command: %02X, %02X", frame[8], frame[9]);
return true;
if (this->buffer_data_[8] || this->buffer_data_[9]) {
ESP_LOGW(TAG, "Invalid command: %02X, %02X", this->buffer_data_[8], this->buffer_data_[9]);
return;
}
switch (frame[COMMAND]) {
switch (this->buffer_data_[COMMAND]) {
case CMD_ENABLE_CONF:
ESP_LOGV(TAG, "Enable conf");
break;
@@ -405,10 +409,10 @@ bool LD2412Component::handle_ack_data_(const uint8_t *frame, uint8_t len) {
break;
case CMD_QUERY_VERSION: {
if (len < 12 + sizeof(this->version_) + HEADER_FOOTER_SIZE) {
if (this->buffer_pos_ < 12 + sizeof(this->version_) + HEADER_FOOTER_SIZE) {
break; // too short to hold a version
}
std::memcpy(this->version_, &frame[12], sizeof(this->version_));
std::memcpy(this->version_, &this->buffer_data_[12], sizeof(this->version_));
char version_s[20];
ld24xx::format_version_str(this->version_, version_s);
ESP_LOGV(TAG, "Firmware version: %s", version_s);
@@ -418,12 +422,12 @@ bool LD2412Component::handle_ack_data_(const uint8_t *frame, uint8_t len) {
break;
}
case CMD_QUERY_DISTANCE_RESOLUTION: {
if (len < 11 + HEADER_FOOTER_SIZE) {
if (this->buffer_pos_ < 11 + HEADER_FOOTER_SIZE) {
break; // too short to hold the resolution
}
const auto index = ld24xx::find_index(DISTANCE_RESOLUTIONS, frame[10]);
const auto index = ld24xx::find_index(DISTANCE_RESOLUTIONS, this->buffer_data_[10]);
if (!index) {
ESP_LOGW(TAG, "Unknown distance resolution %u", frame[10]);
ESP_LOGW(TAG, "Unknown distance resolution %u", this->buffer_data_[10]);
break;
}
ESP_LOGV(TAG, "Distance resolution: %s",
@@ -435,11 +439,11 @@ bool LD2412Component::handle_ack_data_(const uint8_t *frame, uint8_t len) {
}
case CMD_QUERY_LIGHT_CONTROL: {
if (len < 12 + HEADER_FOOTER_SIZE) {
if (this->buffer_pos_ < 12 + HEADER_FOOTER_SIZE) {
break; // too short to hold the light settings
}
this->light_function_ = frame[10];
this->light_threshold_ = frame[11];
this->light_function_ = this->buffer_data_[10];
this->light_threshold_ = this->buffer_data_[11];
ESP_LOGV(TAG, "Light function: %u, threshold: %u", this->light_function_, this->light_threshold_);
#ifdef USE_SELECT
PUBLISH_STATE(this->light_function_select_, static_cast<size_t>(this->light_function_));
@@ -451,11 +455,11 @@ bool LD2412Component::handle_ack_data_(const uint8_t *frame, uint8_t len) {
}
case CMD_QUERY_MAC_ADDRESS: {
if (len < 20) {
if (this->buffer_pos_ < 20) {
break; // too short to hold a MAC address
}
this->bluetooth_on_ = ld24xx::store_mac(&frame[10], this->mac_address_);
this->bluetooth_on_ = ld24xx::store_mac(&this->buffer_data_[10], this->mac_address_);
char mac_s[MAC_ADDRESS_PRETTY_BUFFER_SIZE];
const char *mac_str = ld24xx::format_mac_str(this->mac_address_, mac_s);
@@ -474,11 +478,11 @@ bool LD2412Component::handle_ack_data_(const uint8_t *frame, uint8_t len) {
break;
case CMD_QUERY_DYNAMIC_BACKGROUND_CORRECTION: {
if (len < 11 + HEADER_FOOTER_SIZE) {
if (this->buffer_pos_ < 11 + HEADER_FOOTER_SIZE) {
break; // too short to hold the state
}
ESP_LOGV(TAG, "Handled query dynamic background correction");
bool dynamic_background_correction_active = (frame[10] != 0x00);
bool dynamic_background_correction_active = (this->buffer_data_[10] != 0x00);
#ifdef USE_BINARY_SENSOR
PUBLISH_STATE(this->dynamic_background_correction_status_binary_sensor_, dynamic_background_correction_active);
#endif
@@ -497,17 +501,17 @@ bool LD2412Component::handle_ack_data_(const uint8_t *frame, uint8_t len) {
case CMD_QUERY_MOTION_GATE_SENS:
case CMD_QUERY_STATIC_GATE_SENS: {
#ifdef USE_NUMBER
if (len != GATE_SENS_ACK_SIZE) {
if (this->buffer_pos_ != GATE_SENS_ACK_SIZE) {
// A length this driver does not know
ESP_LOGW(TAG, "Dropping gate threshold answer of %u bytes", len);
return true;
ESP_LOGW(TAG, "Dropping gate threshold answer of %u bytes", this->buffer_pos_);
return;
}
const bool motion = frame[COMMAND] == CMD_QUERY_MOTION_GATE_SENS;
const bool motion = this->buffer_data_[COMMAND] == CMD_QUERY_MOTION_GATE_SENS;
GateThresholds &group = this->gate_thresholds_(motion);
// True only while a held-back write waits for this answer
const bool pending = this->cancel_timeout(GATE_QUERY_TIMEOUT_ID + motion);
for (size_t i = 0; i < TOTAL_GATES; i++) {
group.last_read[i] = frame[ACK_PAYLOAD + i];
group.last_read[i] = this->buffer_data_[ACK_PAYLOAD + i];
// The pending write's user values must survive the answer
if (!pending || !has_value(group.numbers[i])) {
set_number_value(group.numbers[i], group.last_read[i]);
@@ -524,17 +528,17 @@ bool LD2412Component::handle_ack_data_(const uint8_t *frame, uint8_t len) {
}
case CMD_QUERY_BASIC_CONF: { // Query parameters response
if (len < 15 + HEADER_FOOTER_SIZE) {
if (this->buffer_pos_ < 15 + HEADER_FOOTER_SIZE) {
break; // too short to hold the configuration
}
// True only while a held-back write waits for this answer
const bool pending = this->cancel_timeout(BASIC_CONF_TIMEOUT_ID);
this->basic_conf_read_ = true;
// Min gate: 9th byte, max gate: 10th byte, timeout: 11~12th bytes, output pin level: 13th byte
this->min_gate_ = frame[10];
this->max_gate_ = frame[11];
this->timeout_ = encode_uint16(frame[13], frame[12]);
this->out_pin_level_ = frame[14];
this->min_gate_ = this->buffer_data_[10];
this->max_gate_ = this->buffer_data_[11];
this->timeout_ = encode_uint16(this->buffer_data_[13], this->buffer_data_[12]);
this->out_pin_level_ = this->buffer_data_[14];
ESP_LOGV(TAG, "min_distance_gate_number_: %u, max_distance_gate_number_ %u", this->min_gate_, this->max_gate_);
ESP_LOGV(TAG, "timeout_number_: %u", this->timeout_);
// The pending write's user values must survive the answer
@@ -564,21 +568,36 @@ bool LD2412Component::handle_ack_data_(const uint8_t *frame, uint8_t len) {
default:
break;
}
return true;
}
// Handles the frame when its footer is in place; returns false otherwise
bool LD2412Component::handle_frame_(const uint8_t *frame, uint8_t len) {
if (!ld24xx::has_footer(frame, len)) {
ESP_LOGW(TAG, "Invalid footer");
void LD2412Component::readline_(int readch) {
if (readch < 0) {
return; // No data available
}
// Periodic and command frames both carry their count after the header
ld24xx::feed_frame_byte(
this->buffer_data_, this->buffer_pos_, readch, DATA_FRAME_HEADER, ld24xx::cmd_frame_length,
[this](uint8_t frame_len) { return this->handle_frame_(frame_len); }, []() { ESP_LOGW(TAG, "Invalid length"); });
}
// Dispatches the frame of frame_len bytes at the start of the buffer when its footer is in place
bool LD2412Component::handle_frame_(uint8_t frame_len) {
this->buffer_pos_ = frame_len; // the handlers take the frame length from here
const uint8_t *tail = &this->buffer_data_[frame_len - HEADER_FOOTER_SIZE];
if (this->buffer_data_[0] == DATA_FRAME_HEADER[0]) {
if (!ld2412::validate_header_footer(DATA_FRAME_FOOTER, tail)) {
return false;
}
ESP_LOGV_HEX(TAG, this->buffer_data_, this->buffer_pos_, MAX_LINE_LENGTH, "Handling Periodic Data: %s");
this->handle_periodic_data_();
return true;
}
if (!ld2412::validate_header_footer(CMD_FRAME_FOOTER, tail)) {
return false;
}
if (frame[0] == ld24xx::DataFrameFormat::FIRST) {
ESP_LOGV_HEX(TAG, frame, len, MAX_LINE_LENGTH, "Handling Periodic Data: %s");
return this->handle_periodic_data_(frame, len);
}
ESP_LOGV_HEX(TAG, frame, len, MAX_LINE_LENGTH, "Handling Ack Data: %s");
return this->handle_ack_data_(frame, len);
ESP_LOGV_HEX(TAG, this->buffer_data_, this->buffer_pos_, MAX_LINE_LENGTH, "Handling Ack Data: %s");
this->handle_ack_data_();
return true;
}
void LD2412Component::set_config_mode_(bool enable) {
+6 -6
View File
@@ -25,7 +25,6 @@
#include "esphome/components/ld24xx/ld24xx.h"
#include "esphome/components/uart/uart.h"
#include "esphome/core/automation.h"
#include "esphome/core/frame_reader.h"
#include "esphome/core/helpers.h"
#include <array>
@@ -135,10 +134,11 @@ class LD2412Component final : public Component, public uart::UARTDevice {
protected:
void send_command_(uint8_t command_str, const uint8_t *command_value, uint8_t command_value_len);
void set_config_mode_(bool enable);
bool handle_periodic_data_(const uint8_t *frame, uint8_t len);
bool handle_ack_data_(const uint8_t *frame, uint8_t len);
bool handle_frame_(const uint8_t *frame, uint8_t len);
void handle_periodic_data_();
void handle_ack_data_();
bool handle_frame_(uint8_t frame_len);
void basic_conf_timeout_();
void readline_(int readch);
void query_parameters_();
void get_version_();
void get_mac_();
@@ -160,7 +160,6 @@ class LD2412Component final : public Component, public uart::UARTDevice {
void gate_query_timeout_(bool motion);
#endif
uint32_t last_rx_{0};
uint8_t light_function_ = 0;
uint8_t light_threshold_ = 0;
// Basic configuration as last reported by the module; resent for entities that are not configured
@@ -169,7 +168,8 @@ class LD2412Component final : public Component, public uart::UARTDevice {
uint8_t min_gate_ = 1;
uint8_t max_gate_ = TOTAL_GATES;
uint8_t out_pin_level_ = 1;
LengthFrameReader<MAX_LINE_LENGTH, ld24xx::CmdFrameFormat, ld24xx::DataFrameFormat> reader_;
uint8_t buffer_pos_ = 0; // where to resume processing/populating buffer
uint8_t buffer_data_[MAX_LINE_LENGTH];
uint8_t mac_address_[MAC_ADDRESS_SIZE] = {0, 0, 0, 0, 0, 0};
uint8_t version_[6] = {0, 0, 0, 0, 0, 0};
bool bluetooth_on_{false};
+89 -54
View File
@@ -71,10 +71,14 @@ static constexpr uint8_t CMD_SET_ZONE = 0xC2;
using ld24xx::CMD_FRAME_FOOTER;
using ld24xx::CMD_FRAME_HEADER;
using ld24xx::HEADER_FOOTER_SIZE;
static constexpr uint8_t PERIODIC_FRAME_FOOTER[2] = {0x55, 0xCC};
using ld24xx::validate_header_footer;
// Data Header & Footer
static constexpr uint8_t DATA_FRAME_HEADER[HEADER_FOOTER_SIZE] = {0xAA, 0xFF, 0x03, 0x00};
static constexpr uint8_t DATA_FRAME_FOOTER[2] = {0x55, 0xCC};
// Successful zone query ACK: header (4) + length (2) + command/status (2) + result (2) + zone type (2) +
// 3 zones * 8 + footer (4).
static constexpr uint8_t ZONE_ACK_FRAME_SIZE = 40;
static constexpr uint8_t PERIODIC_FRAME_SIZE = 30; // header, three targets of 8 bytes, footer
static inline uint32_t convert_seconds_to_ms(uint16_t value) { return (uint32_t) value * 1000; };
@@ -100,10 +104,22 @@ static inline int16_t decode_speed(uint8_t low_byte, uint8_t high_byte) {
return speed * 10; // mm/s
}
// Length of the frame that opens the buffer: periodic frames are fixed size, command frames carry their
// payload length after the header. readline_ only buffers a valid header, so its first byte names it.
// Returns 0 while the length field has not arrived yet.
static inline uint32_t frame_length(const uint8_t *buffer, uint8_t len) {
if (len < HEADER_FOOTER_SIZE) {
return 0;
}
if (buffer[0] == DATA_FRAME_HEADER[0]) {
return PERIODIC_FRAME_SIZE;
}
return ld24xx::cmd_frame_length(buffer, len);
}
void LD2450Component::setup() {
// The first frame is due link_timeout after the boot restart's grace window
this->last_frame_ms_ = App.get_loop_component_start_time();
this->last_rx_ = this->last_frame_ms_;
this->link_deadline_ms_ = this->last_frame_ms_ + this->link_timeout_ms_;
#ifdef USE_BINARY_SENSOR
if (this->connected_binary_sensor_ != nullptr) {
@@ -225,8 +241,12 @@ void LD2450Component::dump_config() {
}
void LD2450Component::loop() {
auto handler = [this](const uint8_t *frame, size_t len) { return this->handle_frame_(frame, len); };
this->read_frames<MAX_LINE_LENGTH>(this->reader_, handler, App.get_loop_component_start_time(), this->last_rx_);
// Before reading, so a frame in this pass resets it
const size_t count = this->available();
if (count != 0) {
this->bytes_since_frame_ = true;
}
this->read_available<MAX_LINE_LENGTH>([this](uint8_t b) { this->readline_(b); }, count);
this->check_link_();
}
@@ -269,6 +289,7 @@ void LD2450Component::on_valid_frame_() {
this->link_deadline_ms_ = later_ms(window, due);
}
this->last_frame_ms_ = now;
this->bytes_since_frame_ = false;
this->invalid_frames_ = 0;
if (previous != LinkState::LINK_STATE_UP) {
this->link_state_ = LinkState::LINK_STATE_UP;
@@ -302,7 +323,7 @@ void LD2450Component::publish_target_unknown_(uint8_t target) {
// Live values become unknown; settings and diagnostics keep theirs
void LD2450Component::on_link_lost_(uint32_t silent_ms, bool was_up) {
ESP_LOGW(TAG, "Radar link lost: no valid frame for %" PRIu32 " ms (%s, %u invalid frames)", silent_ms,
this->bytes_since_frame_() ? LOG_STR_LITERAL("bytes received") : LOG_STR_LITERAL("no bytes received"),
this->bytes_since_frame_ ? LOG_STR_LITERAL("bytes received") : LOG_STR_LITERAL("no bytes received"),
static_cast<unsigned>(this->invalid_frames_));
this->status_set_warning();
#ifdef USE_BINARY_SENSOR
@@ -426,14 +447,14 @@ bool LD2450Component::get_timeout_status_(uint32_t check_millis) {
}
// Extract, store and publish zone details LD2450 buffer
void LD2450Component::process_zone_(const uint8_t *frame) {
void LD2450Component::process_zone_() {
uint8_t index, start;
for (index = 0; index < MAX_ZONES; index++) {
start = 12 + index * 8;
this->zone_config_[index].x1 = encode_value_le<int16_t>(&frame[start]);
this->zone_config_[index].y1 = encode_value_le<int16_t>(&frame[start + 2]);
this->zone_config_[index].x2 = encode_value_le<int16_t>(&frame[start + 4]);
this->zone_config_[index].y2 = encode_value_le<int16_t>(&frame[start + 6]);
this->zone_config_[index].x1 = encode_value_le<int16_t>(&this->buffer_data_[start]);
this->zone_config_[index].y1 = encode_value_le<int16_t>(&this->buffer_data_[start + 2]);
this->zone_config_[index].x2 = encode_value_le<int16_t>(&this->buffer_data_[start + 4]);
this->zone_config_[index].y2 = encode_value_le<int16_t>(&this->buffer_data_[start + 6]);
#ifdef USE_NUMBER
// only one null check as all coordinates are required for a single zone
if (this->zone_numbers_[index].x1 != nullptr) {
@@ -483,7 +504,7 @@ void LD2450Component::restart_and_read_all_info() {
void LD2450Component::send_command_(uint8_t command, const uint8_t *command_value, uint8_t command_value_len) {
ESP_LOGV(TAG, "Sending COMMAND %02X", command);
// frame header bytes
this->write_array_progmem(CMD_FRAME_HEADER, sizeof(CMD_FRAME_HEADER));
this->write_array(CMD_FRAME_HEADER, sizeof(CMD_FRAME_HEADER));
// length bytes
uint8_t len = 2;
if (command_value != nullptr) {
@@ -497,7 +518,7 @@ void LD2450Component::send_command_(uint8_t command, const uint8_t *command_valu
this->write_array(command_value, command_value_len);
}
// frame footer bytes
this->write_array_progmem(CMD_FRAME_FOOTER, sizeof(CMD_FRAME_FOOTER));
this->write_array(CMD_FRAME_FOOTER, sizeof(CMD_FRAME_FOOTER));
if (command != CMD_ENABLE_CONF && command != CMD_DISABLE_CONF) {
delay(50); // NOLINT
@@ -507,7 +528,7 @@ void LD2450Component::send_command_(uint8_t command, const uint8_t *command_valu
// LD2450 Radar data message:
// [AA FF 03 00] [0E 03 B1 86 10 00 40 01] [00 00 00 00 00 00 00 00] [00 00 00 00 00 00 00 00] [55 CC]
// Header Target 1 Target 2 Target 3 End
void LD2450Component::handle_periodic_data_(const uint8_t *frame) {
void LD2450Component::handle_periodic_data_() {
int16_t target_count = 0;
int16_t still_target_count = 0;
int16_t moving_target_count = 0;
@@ -529,18 +550,18 @@ void LD2450Component::handle_periodic_data_(const uint8_t *frame) {
start = TARGET_X + index * 8;
is_moving = false;
// tx and ty are used for further calculations, so always need to be populated
tx = ld2450::decode_coordinate(frame[start], frame[start + 1]);
tx = ld2450::decode_coordinate(this->buffer_data_[start], this->buffer_data_[start + 1]);
// Y
start = TARGET_Y + index * 8;
ty = ld2450::decode_coordinate(frame[start], frame[start + 1]);
ty = ld2450::decode_coordinate(this->buffer_data_[start], this->buffer_data_[start + 1]);
#ifdef USE_SENSOR
// RESOLUTION
start = TARGET_RESOLUTION + index * 8;
res = encode_value_le<uint16_t>(&frame[start]);
res = encode_value_le<uint16_t>(&this->buffer_data_[start]);
#endif
// SPEED
start = TARGET_SPEED + index * 8;
ts = ld2450::decode_speed(frame[start], frame[start + 1]);
ts = ld2450::decode_speed(this->buffer_data_[start], this->buffer_data_[start + 1]);
if (ts) {
is_moving = true;
moving_target_count++;
@@ -695,18 +716,18 @@ void LD2450Component::handle_periodic_data_(const uint8_t *frame) {
this->data_callback_.call();
}
bool LD2450Component::handle_ack_data_(const uint8_t *frame, uint8_t len) {
ESP_LOGV(TAG, "Handling ACK DATA for COMMAND %02X", frame[COMMAND]);
if (frame[COMMAND_STATUS] != 0x01) {
void LD2450Component::handle_ack_data_() {
ESP_LOGV(TAG, "Handling ACK DATA for COMMAND %02X", this->buffer_data_[COMMAND]);
if (this->buffer_data_[COMMAND_STATUS] != 0x01) {
ESP_LOGE(TAG, "Invalid status");
return false;
return;
}
if (frame[8] || frame[9]) {
ESP_LOGW(TAG, "Invalid command: %02X, %02X", frame[8], frame[9]);
return true;
if (this->buffer_data_[8] || this->buffer_data_[9]) {
ESP_LOGW(TAG, "Invalid command: %02X, %02X", this->buffer_data_[8], this->buffer_data_[9]);
return;
}
switch (frame[COMMAND]) {
switch (this->buffer_data_[COMMAND]) {
case CMD_ENABLE_CONF:
ESP_LOGV(TAG, "Enable conf");
break;
@@ -726,10 +747,10 @@ bool LD2450Component::handle_ack_data_(const uint8_t *frame, uint8_t len) {
break;
case CMD_QUERY_VERSION: {
if (len < 12 + sizeof(this->version_) + HEADER_FOOTER_SIZE) {
if (this->buffer_pos_ < 12 + sizeof(this->version_) + HEADER_FOOTER_SIZE) {
break; // too short to hold a version
}
std::memcpy(this->version_, &frame[12], sizeof(this->version_));
std::memcpy(this->version_, &this->buffer_data_[12], sizeof(this->version_));
char version_s[20];
ld24xx::format_version_str(this->version_, version_s);
ESP_LOGV(TAG, "Firmware version: %s", version_s);
@@ -740,11 +761,11 @@ bool LD2450Component::handle_ack_data_(const uint8_t *frame, uint8_t len) {
}
case CMD_QUERY_MAC_ADDRESS: {
if (len < 20) {
if (this->buffer_pos_ < 20) {
break; // too short to hold a MAC address
}
this->bluetooth_on_ = ld24xx::store_mac(&frame[10], this->mac_address_);
this->bluetooth_on_ = ld24xx::store_mac(&this->buffer_data_[10], this->mac_address_);
char mac_s[MAC_ADDRESS_PRETTY_BUFFER_SIZE];
const char *mac_str = ld24xx::format_mac_str(this->mac_address_, mac_s);
@@ -777,21 +798,21 @@ bool LD2450Component::handle_ack_data_(const uint8_t *frame, uint8_t len) {
break;
case CMD_QUERY_TARGET_MODE:
if (len < 11 + HEADER_FOOTER_SIZE) {
if (this->buffer_pos_ < 11 + HEADER_FOOTER_SIZE) {
break; // too short to hold the mode
}
ESP_LOGV(TAG, "Query target tracking mode");
#ifdef USE_SWITCH
PUBLISH_STATE(this->multi_target_switch_, frame[10] == 0x02);
PUBLISH_STATE(this->multi_target_switch_, this->buffer_data_[10] == 0x02);
#endif
break;
case CMD_QUERY_ZONE:
if (len < ZONE_ACK_FRAME_SIZE) {
if (this->buffer_pos_ < ZONE_ACK_FRAME_SIZE) {
break; // too short to hold the zones
}
ESP_LOGV(TAG, "Query zone conf");
this->zone_type_ = frame[10];
this->zone_type_ = this->buffer_data_[10];
this->publish_zone_type();
#ifdef USE_SELECT
if (this->zone_type_select_ != nullptr) {
@@ -799,16 +820,16 @@ bool LD2450Component::handle_ack_data_(const uint8_t *frame, uint8_t len) {
ESP_LOGV(TAG, "Change zone type to: %.*s", (int) zone.size(), zone.c_str());
}
#endif
if (frame[10] == 0x00) {
if (this->buffer_data_[10] == 0x00) {
ESP_LOGV(TAG, "Zone: Disabled");
}
if (frame[10] == 0x01) {
if (this->buffer_data_[10] == 0x01) {
ESP_LOGV(TAG, "Zone: Area detection");
}
if (frame[10] == 0x02) {
if (this->buffer_data_[10] == 0x02) {
ESP_LOGV(TAG, "Zone: Area filter");
}
this->process_zone_(frame);
this->process_zone_();
break;
case CMD_SET_ZONE:
@@ -819,29 +840,43 @@ bool LD2450Component::handle_ack_data_(const uint8_t *frame, uint8_t len) {
default:
break;
}
return true;
}
// Handles the frame when its footer is in place; returns false otherwise
bool LD2450Component::handle_frame_(const uint8_t *frame, uint8_t len) {
const uint8_t *end = &frame[len];
const bool periodic = frame[0] == PeriodicFrameFormat::FIRST;
if (periodic ? end[-2] != PERIODIC_FRAME_FOOTER[0] || end[-1] != PERIODIC_FRAME_FOOTER[1]
: !ld24xx::has_footer(frame, len)) {
if (this->invalid_frames_ < UINT16_MAX) {
this->invalid_frames_++;
}
ESP_LOGW(TAG, "Invalid footer");
return false;
// Read LD2450 buffer data
void LD2450Component::readline_(int readch) {
if (readch < 0) {
return; // No data available
}
if (periodic) {
ESP_LOGV_HEX(TAG, frame, len, MAX_LINE_LENGTH, "Handling Periodic Data: %s");
ld24xx::feed_frame_byte(
this->buffer_data_, this->buffer_pos_, readch, DATA_FRAME_HEADER, ld2450::frame_length,
[this](uint8_t frame_len) { return this->handle_frame_(frame_len); },
[this]() {
if (this->invalid_frames_ < UINT16_MAX) {
this->invalid_frames_++;
}
ESP_LOGW(TAG, "Invalid header/footer");
});
}
// Dispatches the frame of frame_len bytes at the start of the buffer when its footer is in place
bool LD2450Component::handle_frame_(uint8_t frame_len) {
this->buffer_pos_ = frame_len; // the handlers take the frame length from here
const uint8_t *end = &this->buffer_data_[frame_len];
if (this->buffer_data_[0] == DATA_FRAME_HEADER[0]) {
if (end[-2] != DATA_FRAME_FOOTER[0] || end[-1] != DATA_FRAME_FOOTER[1]) {
return false;
}
ESP_LOGV_HEX(TAG, this->buffer_data_, this->buffer_pos_, MAX_LINE_LENGTH, "Handling Periodic Data: %s");
this->on_valid_frame_();
this->handle_periodic_data_(frame);
this->handle_periodic_data_();
return true;
}
ESP_LOGV_HEX(TAG, frame, len, MAX_LINE_LENGTH, "Handling Ack Data: %s");
return this->handle_ack_data_(frame, len);
if (!ld2450::validate_header_footer(CMD_FRAME_FOOTER, end - HEADER_FOOTER_SIZE)) {
return false;
}
ESP_LOGV_HEX(TAG, this->buffer_data_, this->buffer_pos_, MAX_LINE_LENGTH, "Handling Ack Data: %s");
this->handle_ack_data_();
return true;
}
// Set Config Mode - Pre-requisite sending commands
+9 -13
View File
@@ -30,7 +30,6 @@
#include "esphome/components/ld24xx/ld24xx.h"
#include "esphome/components/uart/uart.h"
#include "esphome/core/frame_reader.h"
#include "esphome/core/helpers.h"
#include "esphome/core/preferences.h"
@@ -47,8 +46,6 @@ static constexpr uint8_t DEFAULT_PRESENCE_TIMEOUT = 5; // Timeout to reset pres
static constexpr uint8_t MAX_LINE_LENGTH = 45;
static constexpr uint8_t MAX_TARGETS = 3; // Max 3 Targets in LD2450
static constexpr uint8_t MAX_ZONES = 3; // Max 3 Zones in LD2450
// Periodic frames: header, three targets of 8 bytes, footer; they carry no length
using PeriodicFrameFormat = FrameFormat<FrameHeader<0xAA, 0xFF, 0x03, 0x00>, FrameFixedLength<30>>;
// Silence after our own commands (restart, Bluetooth toggle, ...) is not a link loss; a restart takes about 2.3 s
static constexpr uint32_t LINK_GRACE_MS = 5000;
@@ -205,10 +202,11 @@ class LD2450Component : public Component, public uart::UARTDevice {
protected:
void send_command_(uint8_t command_str, const uint8_t *command_value, uint8_t command_value_len);
void set_config_mode_(bool enable);
void handle_periodic_data_(const uint8_t *frame);
bool handle_ack_data_(const uint8_t *frame, uint8_t len);
bool handle_frame_(const uint8_t *frame, uint8_t len);
void process_zone_(const uint8_t *frame);
void handle_periodic_data_();
void handle_ack_data_();
bool handle_frame_(uint8_t frame_len);
void process_zone_();
void readline_(int readch);
void get_version_();
void get_mac_();
void query_target_tracking_mode_();
@@ -220,9 +218,6 @@ class LD2450Component : public Component, public uart::UARTDevice {
bool get_timeout_status_(uint32_t check_millis);
void open_link_grace_();
void check_link_();
bool bytes_since_frame_() const {
return static_cast<int32_t>(this->last_rx_ - this->last_frame_ms_) > 0 || this->reader_.length() != 0;
}
void on_valid_frame_();
void on_link_lost_(uint32_t silent_ms, bool was_up);
void on_link_up_(bool restored);
@@ -243,15 +238,16 @@ class LD2450Component : public Component, public uart::UARTDevice {
uint32_t timeout_ = DEFAULT_PRESENCE_TIMEOUT * 1000; // presence_timeout, in ms
uint32_t link_timeout_ms_{0};
uint32_t last_frame_ms_{0}; // last valid periodic frame; until the first one, when setup() ran
uint32_t last_rx_{0}; // last pass that read bytes
uint32_t link_deadline_ms_{0}; // the link is lost when the loop clock reaches this
uint8_t buffer_data_[MAX_LINE_LENGTH];
uint8_t mac_address_[MAC_ADDRESS_SIZE] = {0, 0, 0, 0, 0, 0};
uint8_t version_[6] = {0, 0, 0, 0, 0, 0};
LengthFrameReader<MAX_LINE_LENGTH, ld24xx::CmdFrameFormat, PeriodicFrameFormat> reader_;
uint8_t buffer_pos_ = 0; // where to resume processing/populating buffer
uint8_t zone_type_ = 0;
bool bluetooth_on_{false};
LinkState link_state_{LinkState::LINK_STATE_WAITING};
uint16_t invalid_frames_{0}; // since the last valid frame, saturating
bool bytes_since_frame_{false}; // any bytes since the last valid frame
uint16_t invalid_frames_{0}; // since the last valid frame, saturating
Target target_info_[MAX_TARGETS];
Zone zone_config_[MAX_ZONES];
#ifdef USE_TEXT_SENSOR
+105 -34
View File
@@ -1,33 +1,66 @@
#include "ld2460.h"
#include "esphome/core/application.h"
#include "esphome/core/hal.h"
#include "esphome/core/helpers.h"
#include <algorithm>
#include <cmath>
#include <cstring>
#include <numbers>
namespace esphome::ld2460 {
ESPHOME_LOG_TAG(TAG, "ld2460");
static constexpr uint8_t HEADER_FOOTER_SIZE = 4;
// PROGMEM keeps the patterns out of RAM on the ESP8266; they are copied out for each compare
static constexpr uint8_t CMD_FRAME_HEADER[HEADER_FOOTER_SIZE] PROGMEM = {0xFD, 0xFC, 0xFB, 0xFA};
static constexpr uint8_t CMD_FRAME_FOOTER[HEADER_FOOTER_SIZE] PROGMEM = {0x04, 0x03, 0x02, 0x01};
static constexpr uint8_t DATA_FRAME_HEADER[HEADER_FOOTER_SIZE] PROGMEM = {0xF4, 0xF3, 0xF2, 0xF1};
static constexpr uint8_t DATA_FRAME_FOOTER[HEADER_FOOTER_SIZE] PROGMEM = {0xF8, 0xF7, 0xF6, 0xF5};
static constexpr uint8_t CMD_REPORT_DATA = 0x04;
static constexpr uint8_t CMD_QUERY_INSTALLATION_MODE = 0x0A;
static constexpr uint8_t ACK_FRAME_LENGTH = 12;
static constexpr uint8_t MAX_QUERY_ATTEMPTS = 3;
static constexpr float RADIANS_TO_DEGREES = 180.0f / std::numbers::pi_v<float>;
void LD2460Component::setup() { this->send_query_(); }
// Header, command, length (12, little endian), payload 0x01, footer
static constexpr uint8_t QUERY_INSTALLATION_MODE_FRAME[] PROGMEM = {
0xFD, 0xFC, 0xFB, 0xFA, CMD_QUERY_INSTALLATION_MODE, 0x0C, 0x00, 0x01, 0x04, 0x03, 0x02, 0x01,
};
static bool matches(const uint8_t *bytes, const uint8_t *pattern) {
uint8_t copy[HEADER_FOOTER_SIZE];
progmem_memcpy(copy, pattern, sizeof(copy));
return std::memcmp(bytes, copy, sizeof(copy)) == 0;
}
// Header (4), command (1), length (2, little endian); the length counts the whole frame
static constexpr uint8_t FRAME_PREFIX_SIZE = 7;
static uint16_t frame_length(const uint8_t *frame) { return encode_uint16(frame[6], frame[5]); }
// First offset after 0 whose bytes, as far as they go, match a header; len when there is none (len >= 1)
static uint8_t next_header_start(const uint8_t *buffer, uint8_t len) {
uint8_t data_header[HEADER_FOOTER_SIZE];
uint8_t cmd_header[HEADER_FOOTER_SIZE];
progmem_memcpy(data_header, DATA_FRAME_HEADER, sizeof(data_header));
progmem_memcpy(cmd_header, CMD_FRAME_HEADER, sizeof(cmd_header));
uint8_t start = 1;
for (; start < len; start++) {
const size_t n = std::min<size_t>(len - start, HEADER_FOOTER_SIZE);
if (std::memcmp(&buffer[start], data_header, n) == 0 || std::memcmp(&buffer[start], cmd_header, n) == 0) {
break;
}
}
return start;
}
static constexpr float RADIANS_TO_DEGREES = 180.0f / std::numbers::pi_v<float>;
void LD2460Component::setup() { this->send_query_(); }
void LD2460Component::send_query_() {
this->write_array_progmem(QUERY_INSTALLATION_MODE_FRAME, sizeof(QUERY_INSTALLATION_MODE_FRAME));
uint8_t frame[sizeof(QUERY_INSTALLATION_MODE_FRAME)];
progmem_memcpy(frame, QUERY_INSTALLATION_MODE_FRAME, sizeof(frame));
this->write_array(frame, sizeof(frame));
}
void LD2460Component::dump_config() {
@@ -49,19 +82,19 @@ void LD2460Component::dump_config() {
}
void LD2460Component::loop() {
const uint32_t now = App.get_loop_component_start_time();
auto handler = [this](const uint8_t *frame, size_t len) { return this->handle_frame_(frame, len); };
this->read_frames<MAX_FRAME_LENGTH>(this->reader_, handler, now, this->last_rx_);
this->read_available<MAX_LINE_LENGTH>([this](uint8_t b) { this->readline_(b); });
}
bool LD2460Component::handle_periodic_data_(const uint8_t *data, uint8_t packet_len) {
if (data[4] != CMD_REPORT_DATA) {
void LD2460Component::handle_periodic_data_() {
const uint8_t *data = this->buffer_data_;
if (!this->align_to_header_(DATA_FRAME_HEADER) || data[4] != CMD_REPORT_DATA) {
ESP_LOGW(TAG, "Invalid periodic data header");
return false;
return;
}
if ((packet_len - MIN_FRAME_LENGTH) % 4 != 0) {
const uint8_t packet_len = this->buffer_pos_;
if (packet_len < 11 || (packet_len - 11) % 4 != 0) {
ESP_LOGW(TAG, "Periodic packet length %u is not a whole number of targets", packet_len);
return false;
return;
}
if (this->query_attempts_ != 0) {
@@ -76,7 +109,7 @@ bool LD2460Component::handle_periodic_data_(const uint8_t *data, uint8_t packet_
}
#ifdef USE_SENSOR
uint8_t num_targets = std::min<uint8_t>((packet_len - MIN_FRAME_LENGTH) / 4, MAX_TARGETS);
uint8_t num_targets = std::min<uint8_t>((packet_len - 11) / 4, MAX_TARGETS);
for (uint8_t i = 0; i < MAX_TARGETS; i++) {
TargetSensors &t = this->target_sensors_[i];
if (i >= num_targets) {
@@ -122,36 +155,75 @@ bool LD2460Component::handle_periodic_data_(const uint8_t *data, uint8_t packet_
#endif
this->data_callback_.call();
return true;
}
// Handles the frame when its footer and structure are in place; returns false otherwise
bool LD2460Component::handle_frame_(const uint8_t *frame, uint8_t len) {
if (!ld24xx::has_footer(frame, len)) {
ESP_LOGW(TAG, "Invalid footer");
ESP_LOGV_HEX(TAG, frame, len, MAX_FRAME_LENGTH, "Rejected: %s");
return false;
void LD2460Component::readline_(uint8_t readch) {
if (this->buffer_pos_ >= MAX_LINE_LENGTH) {
// Keep the bytes from the next offset that could start a header, so a frame already partly
// buffered behind stray bytes survives; it is still fully validated once its footer arrives
const uint8_t start = next_header_start(this->buffer_data_, this->buffer_pos_);
this->buffer_pos_ -= start;
std::memmove(this->buffer_data_, &this->buffer_data_[start], this->buffer_pos_);
// Header-shaped noise keeps a whole header; warning only otherwise bounds the warning rate
if (this->buffer_pos_ < HEADER_FOOTER_SIZE) {
ESP_LOGW(TAG, "Max frame length exceeded; ignoring");
} else {
ESP_LOGV(TAG, "Max frame length exceeded; kept %u bytes", this->buffer_pos_);
}
}
ESP_LOGV_HEX(TAG, frame, len, MAX_FRAME_LENGTH, "Frame: %s");
if (frame[0] == DataFrame::FIRST)
return this->handle_periodic_data_(frame, len);
return this->handle_ack_(frame, len);
this->buffer_data_[this->buffer_pos_++] = readch;
if (this->buffer_pos_ < HEADER_FOOTER_SIZE) {
return;
}
const uint8_t *tail = &this->buffer_data_[this->buffer_pos_ - HEADER_FOOTER_SIZE];
bool is_data = readch == progmem_read_byte(&DATA_FRAME_FOOTER[3]) && matches(tail, DATA_FRAME_FOOTER);
if (!is_data && (readch != progmem_read_byte(&CMD_FRAME_FOOTER[3]) || !matches(tail, CMD_FRAME_FOOTER))) {
return;
}
ESP_LOGV_HEX(TAG, this->buffer_data_, this->buffer_pos_, MAX_LINE_LENGTH, "Frame: %s");
if (is_data) {
this->handle_periodic_data_();
} else {
this->handle_ack_();
}
this->buffer_pos_ = 0;
}
bool LD2460Component::handle_ack_(const uint8_t *data, uint8_t len) {
if (len != ACK_FRAME_LENGTH) {
// Once a footer is matched, shift the buffer to the first offset holding a header whose length
// field ends the frame exactly at the footer. Stray bytes, truncated frames and header bytes inside
// a payload are skipped this way. Returns false when no such offset exists.
bool LD2460Component::align_to_header_(const uint8_t *header) {
for (uint8_t i = 0; i + FRAME_PREFIX_SIZE <= this->buffer_pos_; i++) {
const uint8_t *frame = &this->buffer_data_[i];
if (matches(frame, header) && frame_length(frame) == this->buffer_pos_ - i) {
if (i != 0) {
this->buffer_pos_ -= i;
std::memmove(this->buffer_data_, frame, this->buffer_pos_);
}
return true;
}
}
return false;
}
void LD2460Component::handle_ack_() {
const uint8_t *data = this->buffer_data_;
if (!this->align_to_header_(CMD_FRAME_HEADER) || this->buffer_pos_ != ACK_FRAME_LENGTH) {
ESP_LOGW(TAG, "Dropping malformed command frame");
return false;
return;
}
if (data[4] != CMD_QUERY_INSTALLATION_MODE) {
ESP_LOGD(TAG, "Ignoring reply to command 0x%02X", data[4]);
return true;
return;
}
const uint8_t mode = data[7];
if (mode != static_cast<uint8_t>(InstallationMode::INSTALLATION_MODE_SIDE) &&
mode != static_cast<uint8_t>(InstallationMode::INSTALLATION_MODE_TOP)) {
ESP_LOGW(TAG, "Unexpected installation mode %u", mode);
return true;
return;
}
this->installation_mode_ = static_cast<InstallationMode>(mode);
this->query_attempts_ = 0;
@@ -164,7 +236,6 @@ bool LD2460Component::handle_ack_(const uint8_t *data, uint8_t len) {
t.last_x = INT16_MIN;
}
#endif
return true;
}
} // namespace esphome::ld2460
+11 -17
View File
@@ -1,7 +1,6 @@
#pragma once
#include "esphome/core/defines.h"
#include "esphome/core/frame_reader.h"
#include "esphome/components/ld24xx/ld24xx.h"
#include "esphome/components/uart/uart.h"
@@ -9,13 +8,8 @@
namespace esphome::ld2460 {
static constexpr uint8_t MAX_FRAME_LENGTH = 31; // A 5-target data frame
static constexpr uint8_t MAX_LINE_LENGTH = 32; // Longest frame is a 5-target data frame of 31 bytes
static constexpr uint8_t MAX_TARGETS = 5;
// Header (4), command, whole-frame length (2, LE), payload, footer (4)
static constexpr uint8_t MIN_FRAME_LENGTH = 11;
using FrameLength = FrameLengthU16LE<LengthField{.offset = 5}>;
using DataFrame = FrameFormat<ld24xx::DataFrameHeader, FrameLength, FrameMinLength<MIN_FRAME_LENGTH>>;
using CmdFrame = FrameFormat<ld24xx::CmdFrameHeader, FrameLength, FrameMinLength<MIN_FRAME_LENGTH>>;
enum class InstallationMode : uint8_t {
INSTALLATION_MODE_SIDE = 1,
@@ -46,16 +40,10 @@ class LD2460Component : public Component, public uart::UARTDevice {
protected:
void send_query_();
bool handle_periodic_data_(const uint8_t *data, uint8_t packet_len);
bool handle_ack_(const uint8_t *data, uint8_t len);
bool handle_frame_(const uint8_t *frame, uint8_t len);
uint32_t last_rx_{0};
LengthFrameReader<MAX_FRAME_LENGTH, DataFrame, CmdFrame> reader_;
// The angle formula depends on how the module is mounted, so it is read from the module at boot
InstallationMode installation_mode_{InstallationMode::INSTALLATION_MODE_SIDE};
// Queries sent so far; 0 once the module answered. Data frames resend up to MAX_QUERY_ATTEMPTS.
uint8_t query_attempts_{1};
bool align_to_header_(const uint8_t *header);
void handle_periodic_data_();
void handle_ack_();
void readline_(uint8_t readch);
#ifdef USE_SENSOR
// INT16_MIN marks a slot with no published coordinates, so the next report always publishes
@@ -68,6 +56,12 @@ class LD2460Component : public Component, public uart::UARTDevice {
#endif
LazyCallbackManager<void()> data_callback_;
uint8_t buffer_data_[MAX_LINE_LENGTH];
uint8_t buffer_pos_{0};
// The angle formula depends on how the module is mounted, so it is read from the module at boot
InstallationMode installation_mode_{InstallationMode::INSTALLATION_MODE_SIDE};
// Queries sent so far; 0 once the module answered. Data frames resend up to MAX_QUERY_ATTEMPTS.
uint8_t query_attempts_{1};
};
} // namespace esphome::ld2460
+65 -17
View File
@@ -1,12 +1,13 @@
#pragma once
#include "esphome/core/defines.h"
#include "esphome/core/frame_reader.h"
#include "esphome/core/hal.h"
#include "esphome/core/helpers.h"
#include "esphome/core/progmem.h"
#include <algorithm>
#include <cstring>
#include <memory>
#include <span>
#ifdef USE_SENSOR
@@ -78,25 +79,72 @@ inline void format_version_str(const uint8_t *version, std::span<char, 20> buffe
version[5], version[4], version[3], version[2]);
}
// Commands and their answers share this header, a little-endian byte count and this footer; periodic data frames have
// their own header and footer
// Commands and their answers share this header, a little-endian byte count and this footer
static constexpr uint8_t HEADER_FOOTER_SIZE = 4;
static constexpr uint8_t CMD_FRAME_HEADER[HEADER_FOOTER_SIZE] PROGMEM = {0xFD, 0xFC, 0xFB, 0xFA};
static constexpr uint8_t CMD_FRAME_FOOTER[HEADER_FOOTER_SIZE] PROGMEM = {0x04, 0x03, 0x02, 0x01};
static constexpr uint8_t DATA_FRAME_FOOTER[HEADER_FOOTER_SIZE] PROGMEM = {0xF8, 0xF7, 0xF6, 0xF5};
static constexpr uint8_t CMD_FRAME_HEADER[HEADER_FOOTER_SIZE] = {0xFD, 0xFC, 0xFB, 0xFA};
static constexpr uint8_t CMD_FRAME_FOOTER[HEADER_FOOTER_SIZE] = {0x04, 0x03, 0x02, 0x01};
// Header (4), payload length (2, LE), payload, footer (4)
using PayloadLength = PayloadLengthU16LE<LengthField{.offset = HEADER_FOOTER_SIZE, .trailer = HEADER_FOOTER_SIZE}>;
using CmdFrameHeader = FrameHeader<0xFD, 0xFC, 0xFB, 0xFA>;
using DataFrameHeader = FrameHeader<0xF4, 0xF3, 0xF2, 0xF1>;
using CmdFrameFormat = FrameFormat<CmdFrameHeader, PayloadLength>;
using DataFrameFormat = FrameFormat<DataFrameHeader, PayloadLength>;
inline bool validate_header_footer(const uint8_t *header_footer, const uint8_t *buffer) {
return std::memcmp(header_footer, buffer, HEADER_FOOTER_SIZE) == 0;
}
// Whether a command or periodic data frame of at least HEADER_FOOTER_SIZE bytes ends in its footer
inline bool has_footer(const uint8_t *frame, size_t len) {
uint8_t footer[HEADER_FOOTER_SIZE];
progmem_memcpy(footer, frame[0] == DataFrameFormat::FIRST ? DATA_FRAME_FOOTER : CMD_FRAME_FOOTER, sizeof(footer));
return std::memcmp(&frame[len - HEADER_FOOTER_SIZE], footer, sizeof(footer)) == 0;
// Whole command frame length, or 0 until the count arrives; 32 bit so 0xFFFF cannot wrap to 0
inline uint32_t cmd_frame_length(const uint8_t *buffer, uint8_t count) {
if (count < HEADER_FOOTER_SIZE + 2) {
return 0;
}
return HEADER_FOOTER_SIZE + 2 + encode_value_le<uint16_t>(&buffer[HEADER_FOOTER_SIZE]) + HEADER_FOOTER_SIZE;
}
// Drops the first `from` bytes and anything after them that does not begin a header; returns the bytes kept
inline uint8_t drop_to_next_header(uint8_t *buffer, uint8_t count, uint8_t from, const uint8_t *data_header) {
for (; from < count; from++) {
const size_t n = std::min<size_t>(HEADER_FOOTER_SIZE, count - from);
if (std::memcmp(&buffer[from], CMD_FRAME_HEADER, n) == 0 || std::memcmp(&buffer[from], data_header, n) == 0) {
std::memmove(buffer, &buffer[from], count - from);
return count - from;
}
}
return 0;
}
// Feeds one byte into a frame buffer. A frame opens with the command header or `data_header` and ends at
// `frame_length(buffer, count)` bytes (0 while unknown); `handle_frame(len)` returns false for a footer out
// of place. A dropped or oversized frame calls `on_drop()`. The handler may overwrite `pos`; the splitter
// works from the count it saved.
template<size_t N, typename LengthFn, typename HandleFn, typename DropFn>
void feed_frame_byte(uint8_t (&buffer)[N], uint8_t &pos, uint8_t byte, const uint8_t *data_header,
LengthFn frame_length, HandleFn handle_frame, DropFn on_drop) {
// The first byte picks the header; bytes that do not continue it are dropped
if (pos > 0 && pos < HEADER_FOOTER_SIZE) {
const uint8_t *header = buffer[0] == data_header[0] ? data_header : CMD_FRAME_HEADER;
if (byte != header[pos]) {
pos = 0;
}
}
if (pos == 0 && byte != data_header[0] && byte != CMD_FRAME_HEADER[0]) {
return;
}
buffer[pos++] = byte;
// Drain every whole frame left in the buffer
while (true) {
const uint32_t frame_len = frame_length(buffer, pos);
if (frame_len == 0 || (frame_len <= N && pos < frame_len)) {
return;
}
const uint8_t total = pos;
// Keep only what begins a header: after the frame, or after its first byte when it is dropped
uint8_t from = 1;
if (frame_len <= N && handle_frame(static_cast<uint8_t>(frame_len))) {
from = frame_len;
} else {
on_drop();
}
pos = drop_to_next_header(buffer, total, from, data_header);
if (pos == 0) {
return;
}
}
}
} // namespace esphome::ld24xx
+19 -7
View File
@@ -293,9 +293,9 @@ def _ip_request(
return validator
def require_ipv4(name: str) -> Callable[[ConfigType], ConfigType]:
"""Return a validator declaring that component `name` needs IPv4."""
return _ip_request(lambda data: data.require_ipv4.add(name))
def require_ipv4(*path: str) -> Callable[[ConfigType], ConfigType]:
"""Return a validator declaring that the component or option at `path` needs IPv4."""
return _ip_request(lambda data: data.require_ipv4.add(": ".join(path)))
def require_ipv6(name: str) -> Callable[[ConfigType], ConfigType]:
@@ -456,7 +456,10 @@ def _final_validate(config: ConfigType) -> None:
CONF_ENABLE_IPV4,
"IPv4",
data.require_ipv4,
default=not data.request_ipv4_off,
# Wi-Fi and Ethernet keep IPv4 unless the config turns it off explicitly
default=not data.request_ipv4_off
or CONF_WIFI in full_config
or CONF_ETHERNET in full_config,
)
if not config[CONF_ENABLE_IPV4] and not config[CONF_ENABLE_IPV6]:
@@ -662,9 +665,11 @@ async def to_code(config: ConfigType) -> None:
cg.add_define("USE_NETWORK_IPV6", enable_ipv6)
if enable_ipv6:
cg.add_define(
"USE_NETWORK_MIN_IPV6_ADDR_COUNT", config[CONF_MIN_IPV6_ADDR_COUNT]
)
# Without IPv4 an IPv6 address is the connection, so wait for at least one
min_count = config[CONF_MIN_IPV6_ADDR_COUNT]
if not enable_ipv4:
min_count = max(min_count, 1)
cg.add_define("USE_NETWORK_MIN_IPV6_ADDR_COUNT", min_count)
if CORE.is_esp32:
add_idf_sdkconfig_option("CONFIG_LWIP_IPV6", enable_ipv6)
add_idf_sdkconfig_option("CONFIG_LWIP_IPV6_AUTOCONFIG", enable_ipv6)
@@ -681,6 +686,13 @@ async def to_code(config: ConfigType) -> None:
cg.add_define("USE_NETWORK_IPV4", enable_ipv4)
if CORE.is_esp32:
add_idf_sdkconfig_option("CONFIG_LWIP_IPV4", enable_ipv4)
# DNS over RDNSS or stateless DHCPv6 is for IPv6-only Wi-Fi and Ethernet; leave
# other IPv4-less builds (OpenThread) on the sdkconfig they already build with.
if not enable_ipv4 and CORE.has_at_least_one_component(
CONF_WIFI, CONF_ETHERNET
):
add_idf_sdkconfig_option("CONFIG_LWIP_IPV6_RDNSS_MAX_DNS_SERVERS", 2)
add_idf_sdkconfig_option("CONFIG_LWIP_IPV6_DHCP6", True)
# Pvariable creation lives in a separate coroutine at NETWORK_SERVICES so it
# emits after wifi/ethernet at COMMUNICATION. This keeps compile-time config
+16 -5
View File
@@ -3,6 +3,8 @@
#include "esphome/core/defines.h"
#include "esphome/core/helpers.h"
#include "esphome/core/log.h"
#include <utility>
#ifdef USE_NETWORK
namespace esphome::network {
@@ -13,8 +15,17 @@ ESPHOME_LOG_TAG(TAG, "network");
void fill_ip6_addresses(esp_netif_t *netif, IPAddresses &addresses) {
esp_ip6_addr_t ip6s[CONFIG_LWIP_IPV6_NUM_ADDRESSES];
size_t count = esp_netif_get_all_ip6(netif, ip6s);
// Slot 0 holds the IPv4 address
if (count > addresses.size() - 1) {
// Slot 0 holds the IPv4 address; without IPv4 the first IPv6 address takes it, so it reads as the primary one
constexpr size_t first = USE_NETWORK_IPV4 ? 1 : 0;
if constexpr (first == 0) {
// Lead with a routable address; the link-local one comes first from esp-netif and is useless off-link
for (size_t i = 1; i < count; i++) {
if (esp_netif_ip6_get_addr_type(&ip6s[0]) != ESP_IP6_ADDR_IS_LINK_LOCAL)
break;
std::swap(ip6s[0], ip6s[i]);
}
}
if (count > addresses.size() - first) {
// Warn once per interface
static esp_netif_t *warned[3]{};
for (auto &slot : warned) {
@@ -24,14 +35,14 @@ void fill_ip6_addresses(esp_netif_t *netif, IPAddresses &addresses) {
if (slot == nullptr) {
slot = netif;
ESP_LOGW(TAG, "%s has %zu IPv6 addresses, only %zu are reported", esp_netif_get_desc(netif), count,
addresses.size() - 1);
addresses.size() - first);
break;
}
}
count = addresses.size() - 1;
count = addresses.size() - first;
}
for (size_t i = 0; i < count; i++) {
addresses[i + 1] = IPAddress(&ip6s[i]);
addresses[i + first] = IPAddress(&ip6s[i]);
}
}
#endif
+9
View File
@@ -10,6 +10,7 @@ from esphome.const import (
PLATFORM_ESP8266,
)
from esphome.core import CORE, TimePeriod
from esphome.types import ConfigType
from . import Nextion, nextion_ref
from .base_component import (
@@ -147,6 +148,14 @@ CONFIG_SCHEMA = cv.All(
)
def _final_validate(config: ConfigType) -> None:
if CORE.is_esp32:
esp32.validate_task_wdt_enabled(config, CONF_TFT_UPLOAD_WATCHDOG_TIMEOUT)
FINAL_VALIDATE_SCHEMA = _final_validate
automation.register_apply_action(
"display.nextion.set_brightness",
cv.maybe_simple_value(
+3 -3
View File
@@ -98,8 +98,8 @@ def encryption_schema(config: ConfigType | None) -> ConfigType:
return ENCRYPTION_SCHEMA(config)
def _use_managed_components() -> bool:
"""Whether noise-c and libsodium come from the ESP-IDF component registry.
def use_registry_libsodium() -> bool:
"""Whether libsodium, and noise-c with it, come from the ESP-IDF component registry.
Both build as ESP-IDF components, so on ESP32 they skip the PlatformIO library
converter unless arduino-esp32's own libsodium cannot be replaced. Every
@@ -122,7 +122,7 @@ async def to_code(config: ConfigType) -> None:
cg.add_define("USE_NOISE")
# libsodium is declared next to noise-c so the library manager sees both up front
# and nothing else pulls a second copy; the version must match noise-c's own pin
if _use_managed_components():
if use_registry_libsodium():
from esphome.components.esp32 import add_idf_component
add_idf_component(name="esphome/noise-c", ref=NOISE_C_VERSION)
+10 -2
View File
@@ -31,7 +31,12 @@ from esphome.components.esp32 import (
require_vfs_select,
)
from esphome.components.mdns import MDNSComponent, enable_mdns_storage
from esphome.components.network import add_use_address, request_ipv4_off, require_ipv6
from esphome.components.network import (
add_use_address,
request_ipv4_off,
require_ipv4,
require_ipv6,
)
from esphome.components.nrf52.framework import include_west_project
from esphome.components.zephyr import zephyr_add_prj_conf, zephyr_add_prj_conf_min
from esphome.config_helpers import filter_source_files_from_platform
@@ -297,7 +302,10 @@ _BORDER_ROUTER_SCHEMA = cv.Schema({cv.Optional(CONF_RCP): _RCP_SCHEMA})
def _validate_border_router(value: object) -> ConfigType:
return _BORDER_ROUTER_SCHEMA({} if value is None else value)
# ESP-IDF's border router library resolves DNS and runs DHCP over IPv4
return require_ipv4(CONF_OPENTHREAD, CONF_BORDER_ROUTER)(
_BORDER_ROUTER_SCHEMA({} if value is None else value)
)
_CONNECTION_SCHEMA = cv.Schema(
@@ -22,7 +22,6 @@
#include "esp_openthread.h"
#include "esp_openthread_lock.h"
#include "esp_openthread_types.h"
#include "esp_task_wdt.h"
#include "esphome/components/network/util.h"
#include "esphome/core/hal.h"
#include "esphome/core/helpers.h"
@@ -470,11 +469,11 @@ InstanceLock InstanceLock::acquire() {
abort();
}
delay(10);
esp_task_wdt_reset();
arch_feed_wdt();
}
while (!esp_openthread_lock_acquire(100)) {
abort_if_task_exited();
esp_task_wdt_reset();
arch_feed_wdt();
}
// The clean-up may have released the lock to this waiter just before deinit destroys it
if (global_openthread_component->is_task_exited()) {
+16 -11
View File
@@ -19,6 +19,8 @@ static constexpr uint8_t MAX_ATTEMPTS = 4;
static constexpr uint32_t STATUS_REFRESH_MS = 5 * 60 * 1000;
// Until the first full status has arrived, ask again this often.
static constexpr uint32_t STATUS_RETRY_MS = 30 * 1000;
// A frame arrives in one burst; after a pause this long a partly received frame is dropped.
static constexpr uint32_t RX_GAP_MS = 100;
static constexpr float MIN_TEMPERATURE_C = 16.0f;
static constexpr float MAX_TEMPERATURE_C = 31.0f;
@@ -66,17 +68,22 @@ climate::ClimateTraits PioneerWT::traits() {
void PioneerWT::loop() {
const uint32_t now = App.get_loop_component_start_time();
auto handle = [this](const uint8_t *frame, size_t len) {
if (!checksum_valid(frame, len)) {
if (const size_t count = this->available()) {
this->last_rx_ = now;
this->read_available(
[this](uint8_t byte) {
if (const size_t len = this->reader_.feed(byte))
this->handle_frame_(this->reader_.frame(), len);
},
count);
if (this->reader_.take_checksum_error()) {
ESP_LOGD(TAG, "Dropped a frame with a wrong checksum");
return false;
}
this->handle_frame_(frame, len);
return true;
};
this->read_frames(this->reader_, handle, now, this->last_rx_);
// A frame still coming in may be the reply; wait for it before resending
if (!this->queue_.empty() && now - this->last_send_ >= RESEND_INTERVAL_MS && !this->reader_.receiving())
} else if (this->reader_.partial() && now - this->last_rx_ >= RX_GAP_MS) {
// Only on an idle pass: after a slow loop the rest of the frame may already be buffered.
this->reader_.reset();
}
if (!this->queue_.empty() && now - this->last_send_ >= RESEND_INTERVAL_MS)
this->send_pending_();
// The target temperature only arrives in a full status, so it tells whether one has been received.
const uint32_t status_interval = std::isnan(this->target_temperature) ? STATUS_RETRY_MS : STATUS_REFRESH_MS;
@@ -327,8 +334,6 @@ void PioneerWT::write_frame_(uint8_t flags, uint8_t type, uint8_t seq, uint8_t a
uint8_t frame[HEADER_SIZE + 2 + MAX_BODY_SIZE];
const size_t total = build_frame(frame, flags, type, seq, ack, class_1, class_2, body, len);
ESP_LOGVV_HEX(TAG, frame, total, sizeof(frame), "TX %s");
// Kept bytes of an earlier frame must not merge into the reply
this->reader_.drop_kept();
this->write_array(frame, total);
}
@@ -11,6 +11,31 @@ uint16_t frame_crc(const uint8_t *frame, size_t len) {
return crc16be(frame + HEADER_SIZE, len - HEADER_SIZE, head);
}
size_t PacketReader::feed(uint8_t byte) {
if (this->pos_ == 0 && byte != 0xA5)
return 0;
if (this->pos_ == 1 && byte != 0x01) {
this->pos_ = byte == 0xA5 ? 1 : 0;
return 0;
}
this->buf_[this->pos_++] = byte;
if (this->pos_ < 8)
return 0;
const uint8_t len = this->buf_[7];
if (len < HEADER_SIZE) {
this->pos_ = 0;
return 0;
}
if (this->pos_ < len)
return 0;
this->pos_ = 0;
if (frame_crc(this->buf_.data(), len) != encode_uint16(this->buf_[8], this->buf_[9])) {
this->checksum_error_ = true;
return 0;
}
return len;
}
enum class AttrKind : uint8_t {
ATTR_KIND_UNKNOWN,
ATTR_KIND_ONE_BYTE,
@@ -103,19 +128,19 @@ bool AttributeReader::next() {
size_t build_frame(uint8_t *out, uint8_t flags, uint8_t type, uint8_t seq, uint8_t ack, uint8_t class_1,
uint8_t class_2, const uint8_t *body, size_t len) {
const size_t total = HEADER_SIZE + 2 + len;
out[0] = FRAME_START_1;
out[1] = FRAME_START_2;
out[0] = 0xA5;
out[1] = 0x01;
out[2] = flags;
out[3] = type;
out[4] = seq;
out[5] = ack;
out[6] = 0x00;
out[LEN_POS] = total;
out[7] = total;
out[HEADER_SIZE] = class_1;
out[HEADER_SIZE + 1] = class_2;
if (len)
std::memcpy(out + HEADER_SIZE + 2, body, len);
put_be16(out + CRC_POS, frame_crc(out, total));
put_be16(out + 8, frame_crc(out, total));
return total;
}
@@ -1,20 +1,16 @@
#pragma once
#include <array>
#include <cstddef>
#include <cstdint>
#include "esphome/core/frame_reader.h"
#include "esphome/core/helpers.h"
namespace esphome::pioneer_wt {
// Frame layout: A5 01 | flags | type | seq | ack | 00 | len | crc_hi crc_lo | class class | body
static constexpr size_t HEADER_SIZE = 10;
static constexpr uint8_t FRAME_START_1 = 0xA5;
static constexpr uint8_t FRAME_START_2 = 0x01;
// The length byte counts the whole frame.
static constexpr size_t LEN_POS = 7;
static constexpr size_t CRC_POS = 8;
static constexpr size_t MAX_FRAME_SIZE = 255;
static constexpr uint8_t FLAG_NO_ACK = 0x00;
@@ -49,15 +45,28 @@ static constexpr uint8_t SWING_OFF = 0x08;
/// CRC-16/XMODEM over a whole frame, skipping the two checksum bytes at offset 8.
uint16_t frame_crc(const uint8_t *frame, size_t len);
// A5 01, ..., whole-frame length at 7, CRC (2) at 8, body
using PacketReader =
LengthFrameReader<MAX_FRAME_SIZE,
FrameFormat<FrameHeader<FRAME_START_1, FRAME_START_2>,
FrameLengthU8<LengthField{.offset = LEN_POS}>, FrameMinLength<HEADER_SIZE>>>;
/// Collects bytes from the serial line into complete frames with a valid checksum.
class PacketReader final {
public:
/// Feeds one byte. Returns the frame length once a complete, valid frame is in frame().
size_t feed(uint8_t byte);
const uint8_t *frame() const { return this->buf_.data(); }
/// True while part of a frame has been collected.
bool partial() const { return this->pos_ != 0; }
/// Drops a partly collected frame, so the next byte can start a new one.
void reset() { this->pos_ = 0; }
/// True once after a complete frame was dropped for a wrong checksum.
bool take_checksum_error() {
const bool error = this->checksum_error_;
this->checksum_error_ = false;
return error;
}
inline bool checksum_valid(const uint8_t *frame, size_t len) {
return frame_crc(frame, len) == encode_uint16(frame[CRC_POS], frame[CRC_POS + 1]);
}
protected:
std::array<uint8_t, MAX_FRAME_SIZE> buf_{};
uint8_t pos_{0};
bool checksum_error_{false};
};
/// Walks the attribute list in a status body. Stops at the first ID whose size is unknown.
class AttributeReader final {
+88 -44
View File
@@ -7,6 +7,9 @@ namespace esphome::pmsx003 {
ESPHOME_LOG_TAG(TAG, "pmsx003");
static const uint8_t START_CHARACTER_1 = 0x42;
static const uint8_t START_CHARACTER_2 = 0x4D;
static const uint16_t STABILISING_MS = 30000; // time taken for the sensor to become stable after power on in ms
static const uint16_t CMD_MEASUREMENT_MODE_PASSIVE =
@@ -80,9 +83,8 @@ void PMSX003Component::loop() {
// wait for the sensor to be stable
if (now - this->fan_on_time_ < STABILISING_MS)
return;
// consume any command responses that are in the serial buffer, and any partial frame from before the sleep
// consume any command responses that are in the serial buffer
this->discard_available();
this->reader_.reset();
// Trigger a new read
this->send_command_(Command::MANUAL_MEASUREMENT, 0);
this->state_ = State::WAITING;
@@ -93,38 +95,82 @@ void PMSX003Component::loop() {
}
}
auto handle = [this, now](const uint8_t *frame, size_t len) {
if (!this->check_frame_(frame, len))
return false;
if (this->update_interval_ > STABILISING_MS || now - this->last_update_ >= this->update_interval_) {
this->parse_data_(frame);
this->last_update_ = now;
if (now - this->last_transmission_ >= 500) {
// last transmission too long ago. Reset RX index.
this->data_index_ = 0;
}
const size_t avail = this->available();
if (avail == 0)
return;
this->last_transmission_ = now;
this->read_available(
[this, now](uint8_t byte) {
this->data_[this->data_index_] = byte;
auto check = this->check_byte_();
if (!check.has_value()) {
if (this->update_interval_ > STABILISING_MS || now - this->last_update_ >= this->update_interval_) {
this->parse_data_();
this->last_update_ = now;
}
this->data_index_ = 0;
} else if (!*check) {
// wrong data
this->data_index_ = 0;
} else {
// next byte
this->data_index_++;
}
},
avail);
}
optional<bool> PMSX003Component::check_byte_() {
const uint8_t index = this->data_index_;
const uint8_t byte = this->data_[index];
if (index == 0 || index == 1) {
const uint8_t start_char = index == 0 ? START_CHARACTER_1 : START_CHARACTER_2;
if (byte == start_char) {
return true;
}
return true;
};
this->read_frames(this->reader_, handle, now, this->last_transmission_);
}
void warn_too_long(size_t len) {
ESP_LOGW(TAG, "Payload length %u doesn't match. Are you using the correct PMSX003 type?",
static_cast<unsigned>(len - PayloadLength::KNOWN_AT));
}
bool PMSX003Component::check_frame_(const uint8_t *frame, size_t total_size) {
const uint16_t payload_length = total_size - PayloadLength::KNOWN_AT;
if (!this->check_payload_length_(payload_length)) {
ESP_LOGW(TAG, "Payload length %u doesn't match. Are you using the correct PMSX003 type?", payload_length);
ESP_LOGW(TAG, "Start character %u mismatch: 0x%02X != 0x%02X", index + 1, byte, start_char);
return false;
}
if (index == 2) {
return true;
}
const uint16_t payload_length = this->get_16_bit_uint_(2);
if (index == 3) {
if (this->check_payload_length_(payload_length)) {
return true;
} else {
ESP_LOGW(TAG, "Payload length %u doesn't match. Are you using the correct PMSX003 type?", payload_length);
return false;
}
}
// start (16bit) + length (16bit) + DATA (payload_length - 16bit) + checksum (16bit)
const uint16_t total_size = 4 + payload_length;
if (index < total_size - 1) {
return true;
}
// checksum is without checksum bytes
const uint16_t checksum = sum16_bytes(frame, total_size - 2);
const uint16_t check = encode_uint16(frame[total_size - 2], frame[total_size - 1]);
const uint16_t checksum = sum16_bytes(this->data_, total_size - 2);
const uint16_t check = this->get_16_bit_uint_(total_size - 2);
if (checksum != check) {
ESP_LOGW(TAG, "PMSX003 checksum mismatch! 0x%02X != 0x%02X", checksum, check);
return false;
}
return true;
return {};
}
bool PMSX003Component::check_payload_length_(uint16_t payload_length) {
@@ -150,8 +196,6 @@ bool PMSX003Component::check_payload_length_(uint16_t payload_length) {
}
void PMSX003Component::send_command_(Command cmd, uint16_t data) {
// Kept bytes of an earlier reply must not merge into this command's
this->reader_.drop_kept();
uint8_t send_data[7] = {
START_CHARACTER_1, // Start Byte 1
START_CHARACTER_2, // Start Byte 2
@@ -170,11 +214,11 @@ void PMSX003Component::send_command_(Command cmd, uint16_t data) {
}
}
void PMSX003Component::parse_data_(const uint8_t *frame) {
void PMSX003Component::parse_data_() {
// PM mass concentrations
const uint16_t pm_1_0_std_concentration = encode_value<uint16_t>(frame + 4);
const uint16_t pm_2_5_std_concentration = encode_value<uint16_t>(frame + 6);
const uint16_t pm_10_0_std_concentration = encode_value<uint16_t>(frame + 8);
const uint16_t pm_1_0_std_concentration = this->get_16_bit_uint_(4);
const uint16_t pm_2_5_std_concentration = this->get_16_bit_uint_(6);
const uint16_t pm_10_0_std_concentration = this->get_16_bit_uint_(8);
PUBLISH_STATE(this->pm_1_0_std_sensor_, pm_1_0_std_concentration);
PUBLISH_STATE(this->pm_2_5_std_sensor_, pm_2_5_std_concentration);
@@ -190,9 +234,9 @@ void PMSX003Component::parse_data_(const uint8_t *frame) {
const uint8_t pc_offset = is_luftmy ? 10 : 16;
if (!is_luftmy) {
const uint16_t pm_1_0_concentration = encode_value<uint16_t>(frame + 10);
const uint16_t pm_2_5_concentration = encode_value<uint16_t>(frame + 12);
const uint16_t pm_10_0_concentration = encode_value<uint16_t>(frame + 14);
const uint16_t pm_1_0_concentration = this->get_16_bit_uint_(10);
const uint16_t pm_2_5_concentration = this->get_16_bit_uint_(12);
const uint16_t pm_10_0_concentration = this->get_16_bit_uint_(14);
ESP_LOGD(TAG, "Got PM1.0 Concentration: %u µg/m³, PM2.5 Concentration %u µg/m³, PM10.0 Concentration: %u µg/m³",
pm_1_0_concentration, pm_2_5_concentration, pm_10_0_concentration);
@@ -203,10 +247,10 @@ void PMSX003Component::parse_data_(const uint8_t *frame) {
}
// Particle counts at pc_offset: >0.3, >0.5, >1.0, >2.5µm
const uint16_t pm_particles_03um = encode_value<uint16_t>(frame + pc_offset);
const uint16_t pm_particles_05um = encode_value<uint16_t>(frame + pc_offset + 2);
const uint16_t pm_particles_10um = encode_value<uint16_t>(frame + pc_offset + 4);
const uint16_t pm_particles_25um = encode_value<uint16_t>(frame + pc_offset + 6);
const uint16_t pm_particles_03um = this->get_16_bit_uint_(pc_offset);
const uint16_t pm_particles_05um = this->get_16_bit_uint_(pc_offset + 2);
const uint16_t pm_particles_10um = this->get_16_bit_uint_(pc_offset + 4);
const uint16_t pm_particles_25um = this->get_16_bit_uint_(pc_offset + 6);
PUBLISH_STATE(this->pm_particles_03um_sensor_, pm_particles_03um);
PUBLISH_STATE(this->pm_particles_05um_sensor_, pm_particles_05um);
@@ -219,8 +263,8 @@ void PMSX003Component::parse_data_(const uint8_t *frame) {
// PMS5003T uses pc_offset+8 and pc_offset+10 for temperature/humidity, not particle counts
if (this->type_ != Type::PMS5003T) {
const uint16_t pm_particles_50um = encode_value<uint16_t>(frame + pc_offset + 8);
const uint16_t pm_particles_100um = encode_value<uint16_t>(frame + pc_offset + 10);
const uint16_t pm_particles_50um = this->get_16_bit_uint_(pc_offset + 8);
const uint16_t pm_particles_100um = this->get_16_bit_uint_(pc_offset + 10);
ESP_LOGD(TAG, "Got PM5.0 Particles: %u Count/0.1L, PM10.0 Particles: %u Count/0.1L", pm_particles_50um,
pm_particles_100um);
@@ -231,7 +275,7 @@ void PMSX003Component::parse_data_(const uint8_t *frame) {
// Formaldehyde
if (this->type_ == Type::PMS5003S || this->type_ == Type::PMS5003ST) {
const uint16_t formaldehyde = encode_value<uint16_t>(frame + 28);
const uint16_t formaldehyde = this->get_16_bit_uint_(28);
ESP_LOGD(TAG, "Got Formaldehyde: %u µg/m^3", formaldehyde);
@@ -242,8 +286,8 @@ void PMSX003Component::parse_data_(const uint8_t *frame) {
if (this->type_ == Type::PMS5003T || this->type_ == Type::PMS5003ST) {
const uint8_t temperature_offset = (this->type_ == Type::PMS5003T) ? 24 : 30;
const float temperature = encode_value<int16_t>(frame + temperature_offset) / 10.0f;
const float humidity = encode_value<uint16_t>(frame + temperature_offset + 2) / 10.0f;
const float temperature = static_cast<int16_t>(this->get_16_bit_uint_(temperature_offset)) / 10.0f;
const float humidity = this->get_16_bit_uint_(temperature_offset + 2) / 10.0f;
ESP_LOGD(TAG, "Got Temperature: %.1f°C, Humidity: %.1f%%", temperature, humidity);
@@ -254,8 +298,8 @@ void PMSX003Component::parse_data_(const uint8_t *frame) {
// Firmware Version and Error Code
if (this->type_ == Type::PMS1003 || this->type_ == Type::PMS5003ST || this->type_ == Type::PMS9003M) {
const uint8_t firmware_error_code_offset = (this->type_ == Type::PMS5003ST) ? 36 : 28;
const uint8_t firmware_version = frame[firmware_error_code_offset];
const uint8_t error_code = frame[firmware_error_code_offset + 1];
const uint8_t firmware_version = this->data_[firmware_error_code_offset];
const uint8_t error_code = this->data_[firmware_error_code_offset + 1];
ESP_LOGD(TAG, "Got Firmware Version: 0x%02X, Error Code: 0x%02X", firmware_version, error_code);
}
+7 -15
View File
@@ -1,24 +1,12 @@
#pragma once
#include "esphome/core/component.h"
#include "esphome/core/frame_reader.h"
#include "esphome/core/helpers.h"
#include "esphome/components/sensor/sensor.h"
#include "esphome/components/uart/uart.h"
namespace esphome::pmsx003 {
static constexpr uint8_t START_CHARACTER_1 = 0x42;
static constexpr uint8_t START_CHARACTER_2 = 0x4D;
// PMS5003ST: 36 byte payload after the 2 start and 2 length bytes
static constexpr uint8_t MAX_FRAME_SIZE = 40;
// 42 4D, payload length (2, BE), payload ending in its checksum (2)
using PayloadLength = PayloadLengthU16BE<LengthField{.offset = 2}>;
/// The wrong-type warning, for a frame longer than any supported type sends
void warn_too_long(size_t len);
using PmsFrame = FrameFormat<FrameHeader<START_CHARACTER_1, START_CHARACTER_2>,
WarnIfLonger<PayloadLength, MAX_FRAME_SIZE, warn_too_long>>;
enum class Type : uint8_t {
PMS1003 = 0,
PMS3003,
@@ -79,15 +67,19 @@ class PMSX003Component final : public uart::UARTDevice, public Component {
SUB_SENSOR(humidity)
protected:
bool check_frame_(const uint8_t *frame, size_t total_size);
void parse_data_(const uint8_t *frame);
optional<bool> check_byte_();
void parse_data_();
bool check_payload_length_(uint16_t payload_length);
void send_command_(Command cmd, uint16_t data);
uint16_t get_16_bit_uint_(uint8_t start_index) const {
return encode_uint16(this->data_[start_index], this->data_[start_index + 1]);
}
Type type_;
State state_{State::IDLE};
bool initialised_{false};
LengthFrameReader<MAX_FRAME_SIZE, PmsFrame> reader_;
uint8_t data_[64];
uint8_t data_index_{0};
uint32_t fan_on_time_{0};
uint32_t last_update_{0};
uint32_t last_transmission_{0};
+25 -15
View File
@@ -1,5 +1,5 @@
import esphome.codegen as cg
from esphome.components import web_server_base
from esphome.components import web_server_base, web_server_idf
from esphome.components.web_server_base import CONF_WEB_SERVER_BASE_ID
import esphome.config_validation as cv
from esphome.const import CONF_ID, CONF_INCLUDE_INTERNAL, CONF_NAME, CONF_RELABEL
@@ -22,20 +22,30 @@ CUSTOMIZED_ENTITY = cv.All(
cv.has_at_least_one_key(CONF_ID, CONF_NAME),
)
CONFIG_SCHEMA = cv.Schema(
{
cv.GenerateID(): cv.declare_id(PrometheusHandler),
cv.GenerateID(CONF_WEB_SERVER_BASE_ID): cv.use_id(
web_server_base.WebServerBase
),
cv.Optional(CONF_INCLUDE_INTERNAL, default=False): cv.boolean,
cv.Optional(CONF_RELABEL, default={}): cv.Schema(
{
cv.use_id(EntityBase): CUSTOMIZED_ENTITY,
}
),
},
).extend(cv.COMPONENT_SCHEMA)
def _require_plain_http(config: ConfigType) -> ConfigType:
# Prometheus is advertised over mDNS as a plain HTTP service.
web_server_idf.require_plain_http(DOMAIN)
return config
CONFIG_SCHEMA = cv.All(
cv.Schema(
{
cv.GenerateID(): cv.declare_id(PrometheusHandler),
cv.GenerateID(CONF_WEB_SERVER_BASE_ID): cv.use_id(
web_server_base.WebServerBase
),
cv.Optional(CONF_INCLUDE_INTERNAL, default=False): cv.boolean,
cv.Optional(CONF_RELABEL, default={}): cv.Schema(
{
cv.use_id(EntityBase): CUSTOMIZED_ENTITY,
}
),
},
).extend(cv.COMPONENT_SCHEMA),
_require_plain_http,
)
async def to_code(config: ConfigType) -> None:
@@ -8,10 +8,6 @@
#include "esphome/core/helpers.h"
#include "esphome/core/log.h"
#ifdef USE_ESP_IDF
#include "esp_task_wdt.h"
#endif
ESPHOME_LOG_TAG(TAG, "image_decoder.jpeg");
namespace esphome::runtime_image {
@@ -51,18 +47,6 @@ void JpegDecoder::draw_rgb565(int x, int y, int w, int h, const uint8_t *pixels)
}
}
// Some very big images take too long to decode, so feed the watchdog as the decode goes
// to avoid crashing if the executing task has a watchdog enabled.
static void feed_watchdog() {
#ifdef USE_ESP_IDF
if (esp_task_wdt_status(nullptr) == ESP_OK) {
#endif
App.feed_wdt();
#ifdef USE_ESP_IDF
}
#endif
}
/**
* @brief Callback method that will be called by the JPEGDEC engine when a chunk
* of the image is decoded.
@@ -77,7 +61,8 @@ static int draw_callback(JPEGDRAW *jpeg) {
return 0;
}
feed_watchdog();
// Very big images take long to decode, so feed the watchdog as the decode goes
App.feed_wdt_if_watched();
if (decoder->uses_direct_rgb565()) {
decoder->draw_rgb565(jpeg->x, jpeg->y, jpeg->iWidth, jpeg->iHeight,
reinterpret_cast<const uint8_t *>(jpeg->pPixels));
@@ -141,7 +126,7 @@ int HOT JpegDecoder::decode(uint8_t *buffer, size_t size) {
return this->fail_();
int result;
while ((result = this->jpeg_->decodeRows(1)) == JPEG_DECODE_MORE)
feed_watchdog();
App.feed_wdt_if_watched();
if (result != 1)
return this->fail_();
this->decoded_bytes_ = size;
@@ -1,7 +1,6 @@
#include "seeed_mr24hpc1.h"
#include "esphome/core/helpers.h"
#include "esphome/core/application.h"
#include "esphome/core/log.h"
#include "esphome/core/progmem.h"
@@ -83,12 +82,17 @@ void MR24HPC1Component::setup() {
this->poll_time_base_func_check_ = false;
this->check_dev_inf_sign_ = true;
this->sg_start_query_data_ = STANDARD_FUNCTION_QUERY_PRODUCT_MODE;
this->sg_data_len_ = 0;
this->sg_frame_len_ = 0;
this->sg_recv_data_state_ = FRAME_IDLE;
this->s_output_info_switch_flag_ = OUTPUT_SWITCH_INIT;
memset(this->c_product_mode_, 0, PRODUCT_BUF_MAX_SIZE);
memset(this->c_product_id_, 0, PRODUCT_BUF_MAX_SIZE);
memset(this->c_firmware_version_, 0, PRODUCT_BUF_MAX_SIZE);
memset(this->c_hardware_model_, 0, PRODUCT_BUF_MAX_SIZE);
memset(this->sg_frame_prase_buf_, 0, FRAME_BUF_MAX_SIZE);
memset(this->sg_frame_buf_, 0, FRAME_BUF_MAX_SIZE);
// The module locks up when queried during its own boot, so hold all TX for a second
this->set_timeout(STARTUP_DELAY_MS, [this]() {
@@ -105,29 +109,9 @@ void MR24HPC1Component::update_() {
this->poll_time_base_func_check_ = true; // Query the base functionality information at regular intervals
}
// The reader keeps frames at MIN_FRAME_SIZE or more, so the three trailing bytes can always be left out
static uint8_t get_frame_crc_sum(const uint8_t *data, size_t len) { return sum_bytes(data, len - 3); }
// Footer and sum
static bool frame_valid(const uint8_t *data, size_t len) {
if (data[len - 2] != FRAME_TAIL1_VALUE || data[len - 1] != FRAME_TAIL2_VALUE)
return false;
if (data[len - 3] != get_frame_crc_sum(data, len)) {
ESP_LOGD(TAG, "frame check failure!");
return false;
}
return true;
}
// main loop
void MR24HPC1Component::loop() {
auto handler = [this](const uint8_t *frame, size_t len) {
if (!frame_valid(frame, len))
return false;
this->r24_parse_data_frame_(frame, len);
return true;
};
this->read_frames(this->reader_, handler, App.get_loop_component_start_time(), this->last_rx_);
this->read_available([this](uint8_t b) { this->r24_split_data_frame_(b); });
if ((this->s_output_info_switch_flag_ == OUTPUT_SWITCH_OFF) &&
(this->sg_start_query_data_ > CUSTOM_FUNCTION_QUERY_TIME_OF_ENTER_UNMANNED) && (!this->check_dev_inf_sign_)) {
@@ -262,8 +246,109 @@ void MR24HPC1Component::loop() {
}
}
// Frames are at least 10 bytes, so the three trailing bytes can always be left out
static uint8_t get_frame_crc_sum(const uint8_t *data, size_t len) { return sum_bytes(data, len - 3); }
// Check that the check digit is correct
static int get_frame_check_status(const uint8_t *data, size_t len) {
uint8_t crc_sum = get_frame_crc_sum(data, len);
uint8_t verified = data[len - 3];
return (verified == crc_sum) ? 1 : 0;
}
// split data frame
void MR24HPC1Component::r24_split_data_frame_(uint8_t value) {
switch (this->sg_recv_data_state_) {
case FRAME_IDLE: // starting value
if (FRAME_HEADER1_VALUE == value) {
this->sg_recv_data_state_ = FRAME_HEADER2;
}
break;
case FRAME_HEADER2:
if (FRAME_HEADER2_VALUE == value) {
this->sg_frame_buf_[0] = FRAME_HEADER1_VALUE;
this->sg_frame_buf_[1] = FRAME_HEADER2_VALUE;
this->sg_recv_data_state_ = FRAME_CTL_WORD;
} else {
this->sg_recv_data_state_ = FRAME_IDLE;
ESP_LOGD(TAG, "FRAME_IDLE ERROR value:%x", value);
}
break;
case FRAME_CTL_WORD:
this->sg_frame_buf_[2] = value;
this->sg_recv_data_state_ = FRAME_CMD_WORD;
break;
case FRAME_CMD_WORD:
this->sg_frame_buf_[3] = value;
this->sg_recv_data_state_ = FRAME_DATA_LEN_H;
break;
case FRAME_DATA_LEN_H:
if (value == 0) {
this->sg_frame_buf_[4] = value;
this->sg_recv_data_state_ = FRAME_DATA_LEN_L;
} else {
this->sg_recv_data_state_ = FRAME_IDLE;
ESP_LOGD(TAG, "FRAME_DATA_LEN_H ERROR value:%x", value);
}
break;
case FRAME_DATA_LEN_L:
this->sg_data_len_ = value;
if (this->sg_data_len_ == 0 || this->sg_data_len_ > 32) {
ESP_LOGD(TAG, "len=%d, FRAME_DATA_LEN_L ERROR value:%x", this->sg_data_len_, value);
this->sg_data_len_ = 0;
this->sg_recv_data_state_ = FRAME_IDLE;
} else {
this->sg_frame_buf_[5] = value;
this->sg_frame_len_ = 6;
this->sg_recv_data_state_ = FRAME_DATA_BYTES;
}
break;
case FRAME_DATA_BYTES:
this->sg_frame_buf_[this->sg_frame_len_++] = value;
if (--this->sg_data_len_ == 0) {
this->sg_recv_data_state_ = FRAME_DATA_CRC;
}
break;
case FRAME_DATA_CRC:
this->sg_frame_buf_[this->sg_frame_len_++] = value;
this->sg_recv_data_state_ = FRAME_TAIL1;
break;
case FRAME_TAIL1:
if (FRAME_TAIL1_VALUE == value) {
this->sg_recv_data_state_ = FRAME_TAIL2;
} else {
this->sg_recv_data_state_ = FRAME_IDLE;
this->sg_frame_len_ = 0;
this->sg_data_len_ = 0;
ESP_LOGD(TAG, "FRAME_TAIL1 ERROR value:%x", value);
}
break;
case FRAME_TAIL2:
if (FRAME_TAIL2_VALUE == value) {
this->sg_frame_buf_[this->sg_frame_len_++] = FRAME_TAIL1_VALUE;
this->sg_frame_buf_[this->sg_frame_len_++] = FRAME_TAIL2_VALUE;
memcpy(this->sg_frame_prase_buf_, this->sg_frame_buf_, this->sg_frame_len_);
if (get_frame_check_status(this->sg_frame_prase_buf_, this->sg_frame_len_)) {
this->r24_parse_data_frame_(this->sg_frame_prase_buf_, this->sg_frame_len_);
} else {
ESP_LOGD(TAG, "frame check failure!");
}
} else {
ESP_LOGD(TAG, "FRAME_TAIL2 ERROR value:%x", value);
}
memset(this->sg_frame_prase_buf_, 0, FRAME_BUF_MAX_SIZE);
memset(this->sg_frame_buf_, 0, FRAME_BUF_MAX_SIZE);
this->sg_frame_len_ = 0;
this->sg_data_len_ = 0;
this->sg_recv_data_state_ = FRAME_IDLE;
break;
default:
this->sg_recv_data_state_ = FRAME_IDLE;
}
}
// Parses data frames related to product information
void MR24HPC1Component::r24_frame_parse_product_information_(const uint8_t *data) {
void MR24HPC1Component::r24_frame_parse_product_information_(uint8_t *data) {
#ifdef USE_TEXT_SENSOR
uint16_t product_len = encode_uint16(data[FRAME_COMMAND_WORD_INDEX + 1], data[FRAME_COMMAND_WORD_INDEX + 2]);
if (data[FRAME_COMMAND_WORD_INDEX] == COMMAND_PRODUCT_MODE) {
@@ -304,7 +389,7 @@ void MR24HPC1Component::r24_frame_parse_product_information_(const uint8_t *data
}
// Parsing the underlying open parameters
void MR24HPC1Component::r24_frame_parse_open_underlying_information_(const uint8_t *data) {
void MR24HPC1Component::r24_frame_parse_open_underlying_information_(uint8_t *data) {
switch (data[FRAME_COMMAND_WORD_INDEX]) {
case 0x00:
case 0x80:
@@ -397,7 +482,7 @@ void MR24HPC1Component::r24_frame_parse_open_underlying_information_(const uint8
}
}
void MR24HPC1Component::r24_parse_data_frame_(const uint8_t *data, uint8_t len) {
void MR24HPC1Component::r24_parse_data_frame_(uint8_t *data, uint8_t len) {
switch (data[FRAME_CONTROL_WORD_INDEX]) {
case 0x01: {
if (data[FRAME_COMMAND_WORD_INDEX] == 0x02) {
@@ -427,7 +512,7 @@ void MR24HPC1Component::r24_parse_data_frame_(const uint8_t *data, uint8_t len)
}
}
void MR24HPC1Component::r24_frame_parse_work_status_(const uint8_t *data) {
void MR24HPC1Component::r24_frame_parse_work_status_(uint8_t *data) {
switch (data[FRAME_COMMAND_WORD_INDEX]) {
case 0x01:
case 0x81:
@@ -484,7 +569,7 @@ void MR24HPC1Component::r24_frame_parse_work_status_(const uint8_t *data) {
}
}
void MR24HPC1Component::r24_frame_parse_human_information_(const uint8_t *data) {
void MR24HPC1Component::r24_frame_parse_human_information_(uint8_t *data) {
switch (data[FRAME_COMMAND_WORD_INDEX]) {
#ifdef USE_BINARY_SENSOR
case 0x01:
@@ -24,7 +24,6 @@
#endif
#include "esphome/components/uart/uart.h"
#include "esphome/core/automation.h"
#include "esphome/core/frame_reader.h"
#include "esphome/core/helpers.h"
#include "seeed_mr24hpc1_constants.h"
@@ -33,6 +32,19 @@
namespace esphome::seeed_mr24hpc1 {
enum FrameState {
FRAME_IDLE,
FRAME_HEADER2,
FRAME_CTL_WORD,
FRAME_CMD_WORD,
FRAME_DATA_LEN_H,
FRAME_DATA_LEN_L,
FRAME_DATA_BYTES,
FRAME_DATA_CRC,
FRAME_TAIL1,
FRAME_TAIL2,
};
enum PollingState {
STANDARD_FUNCTION_QUERY_PRODUCT_MODE = 0,
STANDARD_FUNCTION_QUERY_PRODUCT_ID,
@@ -122,24 +134,24 @@ class MR24HPC1Component final : public Component,
char c_product_id_[PRODUCT_BUF_MAX_SIZE + 1];
char c_hardware_model_[PRODUCT_BUF_MAX_SIZE + 1];
char c_firmware_version_[PRODUCT_BUF_MAX_SIZE + 1];
uint32_t last_rx_{0};
// 53 59, control, command, payload length (2, BE), payload (at least 1), sum, 54 43
LengthFrameReader<FRAME_BUF_MAX_SIZE, FrameFormat<FrameHeader<FRAME_HEADER1_VALUE, FRAME_HEADER2_VALUE>,
PayloadLengthU16BE<LengthField{.offset = 4, .trailer = 3}>,
FrameMinLength<MIN_FRAME_SIZE>>>
reader_;
int sg_start_query_data_;
uint8_t s_output_info_switch_flag_;
uint8_t sg_recv_data_state_;
uint8_t sg_frame_len_;
uint8_t sg_data_len_;
uint8_t sg_frame_buf_[FRAME_BUF_MAX_SIZE];
uint8_t sg_frame_prase_buf_[FRAME_BUF_MAX_SIZE];
int sg_start_query_data_;
bool check_dev_inf_sign_;
bool poll_time_base_func_check_;
bool module_ready_{false};
void update_();
void r24_parse_data_frame_(const uint8_t *data, uint8_t len);
void r24_frame_parse_open_underlying_information_(const uint8_t *data);
void r24_frame_parse_work_status_(const uint8_t *data);
void r24_frame_parse_product_information_(const uint8_t *data);
void r24_frame_parse_human_information_(const uint8_t *data);
void r24_split_data_frame_(uint8_t value);
void r24_parse_data_frame_(uint8_t *data, uint8_t len);
void r24_frame_parse_open_underlying_information_(uint8_t *data);
void r24_frame_parse_work_status_(uint8_t *data);
void r24_frame_parse_product_information_(uint8_t *data);
void r24_frame_parse_human_information_(uint8_t *data);
void send_query_(const uint8_t *query, size_t string_length);
public:
@@ -4,9 +4,7 @@
namespace esphome::seeed_mr24hpc1 {
// 6 header bytes, up to 32 data bytes, sum and 2 footer bytes
static constexpr uint8_t FRAME_BUF_MAX_SIZE = 41;
static constexpr uint8_t MIN_FRAME_SIZE = 10; // header (2), control, command, length (2), 1 data byte, sum, footer (2)
static const uint8_t FRAME_BUF_MAX_SIZE = 128;
static const uint8_t PRODUCT_BUF_MAX_SIZE = 32;
static const uint8_t FRAME_CONTROL_WORD_INDEX = 2;
@@ -1,6 +1,5 @@
#include "seeed_mr60bha2.h"
#include "esphome/core/helpers.h"
#include "esphome/core/application.h"
#include "esphome/core/log.h"
#include <cinttypes>
@@ -28,6 +27,16 @@ void MR60BHA2Component::dump_config() {
#endif
}
// main loop
void MR60BHA2Component::loop() {
this->read_available([this](uint8_t b) {
this->rx_message_.push_back(b);
if (!this->validate_message_()) {
this->rx_message_.clear();
}
});
}
/**
* @brief Validate the checksum of a byte array.
*
@@ -43,62 +52,75 @@ static bool validate_checksum(const uint8_t *data, size_t len, uint8_t expected_
return static_cast<uint8_t>(~xor_bytes(data, len)) == expected_checksum;
}
static bool is_known_type(uint16_t type) {
return type == BREATH_RATE_TYPE_BUFFER || type == HEART_RATE_TYPE_BUFFER || type == DISTANCE_TYPE_BUFFER ||
type == PEOPLE_EXIST_TYPE_BUFFER || type == PRINT_CLOUD_BUFFER;
}
bool MR60BHA2Component::validate_message_() {
size_t at = this->rx_message_.size() - 1;
auto *data = &this->rx_message_[0];
uint8_t new_byte = data[at];
// An unknown type or a bad header checksum gives no length, so the header is dropped at once
size_t TinyFrameLength::get(const uint8_t *buf) {
if (!is_known_type(encode_uint16(buf[5], buf[6])))
return 0;
constexpr size_t cksum_pos = TinyFrameLength::KNOWN_AT - 1;
if (!validate_checksum(buf, cksum_pos, buf[cksum_pos])) {
ESP_LOGE(TAG, "HEAD_CKSUM_FRAME ERROR: 0x%02x", buf[cksum_pos]);
ESP_LOGV_HEX(TAG, buf, TinyFrameLength::KNOWN_AT, MR60BHA2_MAX_LOG_BYTES, "GET FRAME: %s");
return 0;
if (at == 0) {
return new_byte == FRAME_HEADER_BUFFER;
}
return encode_uint16(buf[3], buf[4]) + FRAME_OVERHEAD;
}
void warn_too_long(size_t len) {
ESP_LOGW(TAG, "Skipped a %u byte frame, longer than the %u byte buffer", static_cast<unsigned>(len),
static_cast<unsigned>(FRAME_MAX_SIZE));
}
if (at <= 2) {
return true;
}
uint16_t frame_id = encode_uint16(data[1], data[2]);
// The reader checks the type and the header; the data checksum is checked here
static bool frame_valid(const uint8_t *frame, size_t len) {
if (!validate_checksum(frame + TinyFrameLength::KNOWN_AT, len - FRAME_OVERHEAD, frame[len - 1])) {
ESP_LOGE(TAG, "DATA_CKSUM_FRAME ERROR: 0x%02x", frame[len - 1]);
ESP_LOGV_HEX(TAG, frame, len - 1, MR60BHA2_MAX_LOG_BYTES, "GET FRAME: %s");
if (at <= 4) {
return true;
}
uint16_t length = encode_uint16(data[3], data[4]);
if (at <= 6) {
return true;
}
uint16_t frame_type = encode_uint16(data[5], data[6]);
if (frame_type != BREATH_RATE_TYPE_BUFFER && frame_type != HEART_RATE_TYPE_BUFFER &&
frame_type != DISTANCE_TYPE_BUFFER && frame_type != PEOPLE_EXIST_TYPE_BUFFER &&
frame_type != PRINT_CLOUD_BUFFER) {
return false;
}
return true;
}
void MR60BHA2Component::loop() {
auto handler = [this](const uint8_t *frame, size_t len) {
if (!frame_valid(frame, len))
uint8_t header_checksum = new_byte;
if (at == 7) {
if (!validate_checksum(data, 7, header_checksum)) {
ESP_LOGE(TAG, "HEAD_CKSUM_FRAME ERROR: 0x%02x", header_checksum);
ESP_LOGV_HEX(TAG, data, 8, MR60BHA2_MAX_LOG_BYTES, "GET FRAME: %s");
return false;
this->handle_frame_(frame, len);
}
return true;
};
this->read_frames(this->reader_, handler, App.get_loop_component_start_time(), this->last_rx_);
}
}
void MR60BHA2Component::handle_frame_(const uint8_t *frame, size_t len) {
const uint16_t frame_id = encode_uint16(frame[1], frame[2]);
const uint16_t frame_type = encode_uint16(frame[5], frame[6]);
const uint8_t *frame_data = frame + TinyFrameLength::KNOWN_AT;
const size_t length = len - FRAME_OVERHEAD;
// Wait until all data is read
if (at - 8 < length) {
return true;
}
uint8_t data_checksum = new_byte;
if (at == 8 + length) {
if (!validate_checksum(data + 8, length, data_checksum)) {
ESP_LOGE(TAG, "DATA_CKSUM_FRAME ERROR: 0x%02x", data_checksum);
ESP_LOGV_HEX(TAG, data, 8 + length, MR60BHA2_MAX_LOG_BYTES, "GET FRAME: %s");
return false;
}
}
const uint8_t *frame_data = data + 8;
#if ESPHOME_LOG_LEVEL >= ESPHOME_LOG_LEVEL_VERBOSE
char hex_buf1[format_hex_pretty_size(MR60BHA2_MAX_LOG_BYTES)];
char hex_buf2[format_hex_pretty_size(MR60BHA2_MAX_LOG_BYTES)];
#endif
ESP_LOGV(TAG, "Received Frame: ID: 0x%04x, Type: 0x%04x, Data: [%s] Raw Data: [%s]", frame_id, frame_type,
format_hex_pretty_to(hex_buf1, sizeof(hex_buf1), frame_data, length),
format_hex_pretty_to(hex_buf2, sizeof(hex_buf2), frame, len));
this->process_frame_(frame_id, frame_type, frame_data, length);
format_hex_pretty_to(hex_buf2, sizeof(hex_buf2), this->rx_message_.data(), this->rx_message_.size()));
this->process_frame_(frame_id, frame_type, data + 8, length);
// Return false to reset rx buffer
return false;
}
void MR60BHA2Component::process_frame_(uint16_t frame_id, uint16_t frame_type, const uint8_t *data, size_t length) {
@@ -9,24 +9,12 @@
#endif
#include "esphome/components/uart/uart.h"
#include "esphome/core/automation.h"
#include "esphome/core/frame_reader.h"
#include "esphome/core/helpers.h"
#include <map>
namespace esphome::seeed_mr60bha2 {
static constexpr uint8_t FRAME_HEADER_BUFFER = 0x01;
/// 01, id (2), data length (2, BE), type (2), header checksum, data, data checksum; 0 for a bad header
struct TinyFrameLength {
static constexpr size_t KNOWN_AT = 8;
static size_t get(const uint8_t *buf);
};
void warn_too_long(size_t len);
// Header, id, length and type bytes, header checksum, then the data and its checksum
static constexpr uint8_t FRAME_OVERHEAD = 9;
// A point cloud frame is 13 bytes plus 16 per target and only its count is read; 128 holds up to 7 targets
static constexpr uint8_t FRAME_MAX_SIZE = 128;
static const uint8_t FRAME_HEADER_BUFFER = 0x01;
static const uint16_t BREATH_RATE_TYPE_BUFFER = 0x0A14;
static const uint16_t PEOPLE_EXIST_TYPE_BUFFER = 0x0F09;
static const uint16_t HEART_RATE_TYPE_BUFFER = 0x0A15;
@@ -51,13 +39,10 @@ class MR60BHA2Component final : public Component,
void loop() override;
protected:
void handle_frame_(const uint8_t *frame, size_t len);
bool validate_message_();
void process_frame_(uint16_t frame_id, uint16_t frame_type, const uint8_t *data, size_t length);
uint32_t last_rx_{0};
LengthFrameReader<FRAME_MAX_SIZE, FrameFormat<FrameHeader<FRAME_HEADER_BUFFER>,
WarnIfLonger<TinyFrameLength, FRAME_MAX_SIZE, warn_too_long>>>
reader_;
std::vector<uint8_t> rx_message_;
};
} // namespace esphome::seeed_mr60bha2
@@ -1,7 +1,6 @@
#include "seeed_mr60fda2.h"
#include "esphome/core/hal.h"
#include "esphome/core/helpers.h"
#include "esphome/core/application.h"
#include "esphome/core/log.h"
#include <cinttypes>
@@ -34,7 +33,22 @@ void MR60FDA2Component::dump_config() {
}
// Initialisation functions
void MR60FDA2Component::setup() { this->get_radar_parameters(); }
void MR60FDA2Component::setup() {
this->current_frame_locate_ = LOCATE_FRAME_HEADER;
this->current_frame_id_ = 0;
this->current_frame_len_ = 0;
this->current_data_frame_len_ = 0;
this->current_frame_type_ = 0;
this->get_radar_parameters();
memset(this->current_frame_buf_, 0, FRAME_BUF_MAX_SIZE);
memset(this->current_data_buf_, 0, DATA_BUF_MAX_SIZE);
}
// main loop
void MR60FDA2Component::loop() {
this->read_available([this](uint8_t b) { this->split_frame_(b); });
}
// The checksum is the inverted XOR of the bytes
static uint8_t calculate_checksum(const uint8_t *data, size_t len) { return ~xor_bytes(data, len); }
@@ -54,45 +68,6 @@ static bool validate_checksum(const uint8_t *data, size_t len, uint8_t expected_
return calculate_checksum(data, len) == expected_checksum;
}
static bool is_known_type(uint16_t type) {
return type == IS_FALL_TYPE_BUFFER || type == PEOPLE_EXIST_TYPE_BUFFER || type == RESULT_INSTALL_HEIGHT ||
type == RESULT_PARAMETERS || type == RESULT_HEIGHT_THRESHOLD || type == RESULT_SENSITIVITY;
}
// An unknown type or a bad header checksum gives no length, so the header is dropped at once
size_t TinyFrameLength::get(const uint8_t *buf) {
const uint16_t type = encode_uint16(buf[5], buf[6]);
const uint16_t data_len = encode_uint16(buf[3], buf[4]);
// A parameter reply is read for 12 data bytes
if (!is_known_type(type) || (type == RESULT_PARAMETERS && data_len < 12))
return 0;
if (!validate_checksum(buf, LEN_TO_HEAD_CKSUM - 1, buf[LEN_TO_HEAD_CKSUM - 1])) {
ESP_LOGD(TAG, "HEAD_CKSUM_FRAME ERROR: 0x%02x", buf[LEN_TO_HEAD_CKSUM - 1]);
ESP_LOGV_HEX(TAG, buf, LEN_TO_HEAD_CKSUM, MR60FDA2_MAX_LOG_BYTES, "CURRENT_FRAME: %s");
return 0;
}
return data_len + LEN_TO_DATA_FRAME;
}
static bool frame_valid(const uint8_t *frame, size_t len) {
if (!validate_checksum(frame + LEN_TO_HEAD_CKSUM, len - LEN_TO_DATA_FRAME, frame[len - 1])) {
ESP_LOGD(TAG, "DATA_CKSUM_FRAME ERROR: 0x%02x", frame[len - 1]);
ESP_LOGV_HEX(TAG, frame, len, MR60FDA2_MAX_LOG_BYTES, "GET CURRENT_FRAME: %s");
return false;
}
return true;
}
void MR60FDA2Component::loop() {
auto handler = [this](const uint8_t *frame, size_t len) {
if (!frame_valid(frame, len))
return false;
this->process_frame_(frame);
return true;
};
this->read_frames(this->reader_, handler, App.get_loop_component_start_time(), this->last_rx_);
}
#ifdef USE_SELECT
static uint8_t find_nearest_index(float value, const float *arr, int size) {
int nearest_index = 0;
@@ -113,43 +88,152 @@ alignas(4) static const float INSTALL_HEIGHT[7] PROGMEM = {2.4f, 2.5f, 2.6f, 2.7
alignas(4) static const float HEIGHT_THRESHOLD[7] PROGMEM = {0.0f, 0.1f, 0.2f, 0.3f, 0.4f, 0.5f, 0.6f};
alignas(4) static const float SENSITIVITY[3] PROGMEM = {3, 15, 30};
void MR60FDA2Component::process_frame_(const uint8_t *frame) {
const uint8_t *data = frame + LEN_TO_HEAD_CKSUM;
switch (encode_uint16(frame[5], frame[6])) {
void MR60FDA2Component::split_frame_(uint8_t buffer) {
switch (this->current_frame_locate_) {
case LOCATE_FRAME_HEADER: // starting buffer
if (buffer == FRAME_HEADER_BUFFER) {
this->current_frame_len_ = 0;
this->current_frame_buf_[this->current_frame_len_++] = buffer;
this->current_frame_locate_++;
}
break;
case LOCATE_ID_FRAME1:
this->current_frame_id_ = buffer << 8;
this->current_frame_buf_[this->current_frame_len_++] = buffer;
this->current_frame_locate_++;
break;
case LOCATE_ID_FRAME2:
this->current_frame_id_ += buffer;
this->current_frame_buf_[this->current_frame_len_++] = buffer;
this->current_frame_locate_++;
break;
case LOCATE_LENGTH_FRAME_H:
this->current_data_frame_len_ = buffer << 8;
if (this->current_data_frame_len_ == 0) {
this->current_frame_buf_[this->current_frame_len_++] = buffer;
this->current_frame_locate_++;
} else {
this->current_frame_locate_ = LOCATE_FRAME_HEADER;
}
break;
case LOCATE_LENGTH_FRAME_L:
this->current_data_frame_len_ += buffer;
if (this->current_data_frame_len_ > DATA_BUF_MAX_SIZE) {
this->current_frame_locate_ = LOCATE_FRAME_HEADER;
} else {
this->current_frame_buf_[this->current_frame_len_++] = buffer;
this->current_frame_locate_++;
}
break;
case LOCATE_TYPE_FRAME1:
this->current_frame_type_ = buffer << 8;
this->current_frame_buf_[this->current_frame_len_++] = buffer;
this->current_frame_locate_++;
break;
case LOCATE_TYPE_FRAME2:
this->current_frame_type_ += buffer;
if ((this->current_frame_type_ == IS_FALL_TYPE_BUFFER) ||
(this->current_frame_type_ == PEOPLE_EXIST_TYPE_BUFFER) ||
(this->current_frame_type_ == RESULT_INSTALL_HEIGHT) || (this->current_frame_type_ == RESULT_PARAMETERS) ||
(this->current_frame_type_ == RESULT_HEIGHT_THRESHOLD) || (this->current_frame_type_ == RESULT_SENSITIVITY)) {
this->current_frame_buf_[this->current_frame_len_++] = buffer;
this->current_frame_locate_++;
} else {
this->current_frame_locate_ = LOCATE_FRAME_HEADER;
}
break;
case LOCATE_HEAD_CKSUM_FRAME:
if (validate_checksum(this->current_frame_buf_, this->current_frame_len_, buffer)) {
this->current_frame_buf_[this->current_frame_len_++] = buffer;
this->current_frame_locate_++;
} else {
ESP_LOGD(TAG, "HEAD_CKSUM_FRAME ERROR: 0x%02x", buffer);
#if ESPHOME_LOG_LEVEL >= ESPHOME_LOG_LEVEL_VERBOSE
char frame_buf[format_hex_pretty_size(MR60FDA2_MAX_LOG_BYTES)];
char byte_buf[format_hex_pretty_size(1)];
#endif
ESP_LOGV(TAG, "CURRENT_FRAME: %s %s",
format_hex_pretty_to(frame_buf, this->current_frame_buf_, this->current_frame_len_),
format_hex_pretty_to(byte_buf, &buffer, 1));
this->current_frame_locate_ = LOCATE_FRAME_HEADER;
}
break;
case LOCATE_DATA_FRAME:
if (this->current_frame_len_ >= FRAME_BUF_MAX_SIZE) {
ESP_LOGD(TAG, "PRACTICE_DATA_FRAME_LEN ERROR: %d", this->current_frame_len_ - LEN_TO_HEAD_CKSUM);
this->current_frame_locate_ = LOCATE_FRAME_HEADER;
break;
}
this->current_data_buf_[this->current_frame_len_ - LEN_TO_DATA_FRAME + 1] = buffer;
this->current_frame_buf_[this->current_frame_len_++] = buffer;
if (this->current_frame_len_ - LEN_TO_HEAD_CKSUM == this->current_data_frame_len_) {
this->current_frame_locate_++;
}
break;
case LOCATE_DATA_CKSUM_FRAME:
if (validate_checksum(this->current_data_buf_, this->current_data_frame_len_, buffer)) {
this->current_frame_buf_[this->current_frame_len_++] = buffer;
this->current_frame_locate_++;
this->process_frame_();
} else {
ESP_LOGD(TAG, "DATA_CKSUM_FRAME ERROR: 0x%02x", buffer);
#if ESPHOME_LOG_LEVEL >= ESPHOME_LOG_LEVEL_VERBOSE
char frame_buf[format_hex_pretty_size(MR60FDA2_MAX_LOG_BYTES)];
char byte_buf[format_hex_pretty_size(1)];
#endif
ESP_LOGV(TAG, "GET CURRENT_FRAME: %s %s",
format_hex_pretty_to(frame_buf, this->current_frame_buf_, this->current_frame_len_),
format_hex_pretty_to(byte_buf, &buffer, 1));
this->current_frame_locate_ = LOCATE_FRAME_HEADER;
}
break;
default:
break;
}
}
void MR60FDA2Component::process_frame_() {
switch (this->current_frame_type_) {
case IS_FALL_TYPE_BUFFER:
#ifdef USE_BINARY_SENSOR
PUBLISH_STATE(this->fall_detected_binary_sensor_, data[0]);
PUBLISH_STATE(this->fall_detected_binary_sensor_, this->current_frame_buf_[LEN_TO_HEAD_CKSUM]);
#endif
this->current_frame_locate_ = LOCATE_FRAME_HEADER;
break;
case PEOPLE_EXIST_TYPE_BUFFER:
#ifdef USE_BINARY_SENSOR
PUBLISH_STATE(this->people_exist_binary_sensor_, data[0]);
PUBLISH_STATE(this->people_exist_binary_sensor_, this->current_frame_buf_[LEN_TO_HEAD_CKSUM]);
#endif
this->current_frame_locate_ = LOCATE_FRAME_HEADER;
break;
case RESULT_INSTALL_HEIGHT:
if (data[0]) {
if (this->current_data_buf_[0]) {
ESP_LOGD(TAG, "Successfully set the mounting height");
} else {
ESP_LOGD(TAG, "Failed to set the mounting height");
}
this->current_frame_locate_ = LOCATE_FRAME_HEADER;
break;
case RESULT_HEIGHT_THRESHOLD:
if (data[0]) {
if (this->current_data_buf_[0]) {
ESP_LOGD(TAG, "Successfully set the height threshold");
} else {
ESP_LOGD(TAG, "Failed to set the height threshold");
}
this->current_frame_locate_ = LOCATE_FRAME_HEADER;
break;
case RESULT_SENSITIVITY:
if (data[0]) {
if (this->current_data_buf_[0]) {
ESP_LOGD(TAG, "Successfully set the sensitivity");
} else {
ESP_LOGD(TAG, "Failed to set the sensitivity");
}
this->current_frame_locate_ = LOCATE_FRAME_HEADER;
break;
case RESULT_PARAMETERS: {
@@ -158,7 +242,8 @@ void MR60FDA2Component::process_frame_(const uint8_t *frame) {
uint32_t current_sensitivity = 0;
#ifdef USE_SELECT
if (this->install_height_select_ != nullptr) {
uint32_t current_install_height_int = encode_uint32(data[3], data[2], data[1], data[0]);
uint32_t current_install_height_int =
encode_uint32(current_data_buf_[3], current_data_buf_[2], current_data_buf_[1], current_data_buf_[0]);
install_height_float = bit_cast<float>(current_install_height_int);
uint32_t select_index = find_nearest_index(install_height_float, INSTALL_HEIGHT, 7);
@@ -166,7 +251,8 @@ void MR60FDA2Component::process_frame_(const uint8_t *frame) {
}
if (this->height_threshold_select_ != nullptr) {
uint32_t current_height_threshold_int = encode_uint32(data[7], data[6], data[5], data[4]);
uint32_t current_height_threshold_int =
encode_uint32(current_data_buf_[7], current_data_buf_[6], current_data_buf_[5], current_data_buf_[4]);
height_threshold_float = bit_cast<float>(current_height_threshold_int);
size_t select_index = find_nearest_index(height_threshold_float, HEIGHT_THRESHOLD, 7);
@@ -174,7 +260,8 @@ void MR60FDA2Component::process_frame_(const uint8_t *frame) {
}
if (this->sensitivity_select_ != nullptr) {
current_sensitivity = encode_uint32(data[11], data[10], data[9], data[8]);
current_sensitivity =
encode_uint32(current_data_buf_[11], current_data_buf_[10], current_data_buf_[9], current_data_buf_[8]);
uint32_t select_index = find_nearest_index(current_sensitivity, SENSITIVITY, 3);
this->sensitivity_select_->publish_state(select_index);
@@ -183,6 +270,7 @@ void MR60FDA2Component::process_frame_(const uint8_t *frame) {
ESP_LOGD(TAG, "Mounting height: %.2f, Height threshold: %.2f, Sensitivity: %" PRIu32, install_height_float,
height_threshold_float, current_sensitivity);
this->current_frame_locate_ = LOCATE_FRAME_HEADER;
break;
}
default:
@@ -15,25 +15,18 @@
#endif
#include "esphome/components/uart/uart.h"
#include "esphome/core/automation.h"
#include "esphome/core/frame_reader.h"
#include "esphome/core/helpers.h"
#include <map>
namespace esphome::seeed_mr60fda2 {
static constexpr uint8_t DATA_BUF_MAX_SIZE = 28;
static constexpr uint8_t LEN_TO_HEAD_CKSUM = 8;
static constexpr uint8_t LEN_TO_DATA_FRAME = 9;
static constexpr uint8_t FRAME_BUF_MAX_SIZE = DATA_BUF_MAX_SIZE + LEN_TO_DATA_FRAME;
static const uint8_t DATA_BUF_MAX_SIZE = 28;
static const uint8_t FRAME_BUF_MAX_SIZE = 37;
static const uint8_t LEN_TO_HEAD_CKSUM = 8;
static const uint8_t LEN_TO_DATA_FRAME = 9;
static constexpr uint8_t FRAME_HEADER_BUFFER = 0x01;
/// 01, id (2), data length (2, BE), type (2), header checksum, data, data checksum; 0 for a bad header
struct TinyFrameLength {
static constexpr size_t KNOWN_AT = LEN_TO_HEAD_CKSUM;
static size_t get(const uint8_t *buf);
};
static const uint8_t FRAME_HEADER_BUFFER = 0x01;
static const uint16_t IS_FALL_TYPE_BUFFER = 0x0E02;
static const uint16_t PEOPLE_EXIST_TYPE_BUFFER = 0x0F09;
static const uint16_t RESULT_INSTALL_HEIGHT = 0x0E04;
@@ -41,6 +34,20 @@ static const uint16_t RESULT_PARAMETERS = 0x0E06;
static const uint16_t RESULT_HEIGHT_THRESHOLD = 0x0E08;
static const uint16_t RESULT_SENSITIVITY = 0x0E0A;
enum FrameLocation {
LOCATE_FRAME_HEADER,
LOCATE_ID_FRAME1,
LOCATE_ID_FRAME2,
LOCATE_LENGTH_FRAME_H,
LOCATE_LENGTH_FRAME_L,
LOCATE_TYPE_FRAME1,
LOCATE_TYPE_FRAME2,
LOCATE_HEAD_CKSUM_FRAME, // Header checksum: [from the first byte to the previous byte of the HEAD_CKSUM bit]
LOCATE_DATA_FRAME,
LOCATE_DATA_CKSUM_FRAME, // Data checksum: [from the first to the previous byte of the DATA_CKSUM bit]
LOCATE_PROCESS_FRAME,
};
class MR60FDA2Component final : public Component,
public uart::UARTDevice { // The class name must be the name defined by text_sensor.py
#ifdef USE_BINARY_SENSOR
@@ -58,12 +65,16 @@ class MR60FDA2Component final : public Component,
#endif
protected:
uint32_t last_rx_{0};
LengthFrameReader<FRAME_BUF_MAX_SIZE, FrameFormat<FrameHeader<FRAME_HEADER_BUFFER>, TinyFrameLength,
FrameMinLength<LEN_TO_DATA_FRAME + 1>>>
reader_;
uint8_t current_frame_locate_;
uint8_t current_frame_buf_[FRAME_BUF_MAX_SIZE];
uint8_t current_data_buf_[DATA_BUF_MAX_SIZE];
uint16_t current_frame_id_;
size_t current_frame_len_;
size_t current_data_frame_len_;
uint16_t current_frame_type_;
void process_frame_(const uint8_t *frame);
void split_frame_(uint8_t buffer);
void process_frame_();
public:
float get_setup_priority() const override { return esphome::setup_priority::LATE; }
+2 -2
View File
@@ -41,8 +41,8 @@ from esphome.types import ConfigType
_LOGGER = logging.getLogger(__name__)
# mdns for autodiscovery
AUTO_LOAD = ["mdns"]
# mdns for autodiscovery; noise declares the noise-c and libsodium that sendspin-cpp uses
AUTO_LOAD = ["mdns", "noise"]
CODEOWNERS = ["@kahrendt"]
DEPENDENCIES = ["network"]
DOMAIN = "sendspin"
@@ -15,17 +15,20 @@
#include <algorithm>
#include <cstring>
#include <memory>
#include <numeric>
namespace {
ESPHOME_LOG_TAG(TAG, "shelly_dimmer");
constexpr uint8_t SHELLY_DIMMER_ACK_TIMEOUT = 200; // ms
// After this long without a byte, a partial frame is searched, so a stray header cannot hide the reply
constexpr uint8_t SHELLY_DIMMER_RX_GAP = 20; // ms
constexpr uint8_t SHELLY_DIMMER_MAX_RETRIES = 3;
constexpr uint16_t SHELLY_DIMMER_MAX_BRIGHTNESS = 1000; // 100%
// Protocol framing.
constexpr uint8_t SHELLY_DIMMER_PROTO_START_BYTE = 0x01;
constexpr uint8_t SHELLY_DIMMER_PROTO_END_BYTE = 0x04;
// Supported commands.
constexpr uint8_t SHELLY_DIMMER_PROTO_CMD_SWITCH = 0x01;
constexpr uint8_t SHELLY_DIMMER_PROTO_CMD_POLL = 0x10;
@@ -55,6 +58,11 @@ template<typename T, size_t N> constexpr size_t size(const T (& /*unused*/)[N])
namespace esphome::shelly_dimmer {
/// Computes a crappy checksum as defined by the Shelly Dimmer protocol.
uint16_t shelly_dimmer_checksum(const uint8_t *buf, int len) {
return std::accumulate<decltype(buf), uint16_t>(buf, buf + len, 0);
}
bool ShellyDimmer::is_running_configured_version() const {
return this->version_major_ == USE_SHD_FIRMWARE_MAJOR_VERSION &&
this->version_minor_ == USE_SHD_FIRMWARE_MINOR_VERSION;
@@ -265,16 +273,13 @@ bool ShellyDimmer::send_command_(uint8_t cmd, const uint8_t *const payload, uint
// Write the frame and wait for acknowledgement.
int retries = SHELLY_DIMMER_MAX_RETRIES;
while (retries--) {
// Kept bytes of an earlier reply must not merge into this one
this->reader_.drop_kept();
this->write_array(frame, frame_len);
this->flush();
ESP_LOGD(TAG, "Command sent, waiting for reply");
const uint32_t tx_time = millis();
uint32_t last_rx = tx_time;
for (uint32_t now = tx_time; now - tx_time < SHELLY_DIMMER_ACK_TIMEOUT; now = millis()) {
if (this->read_frame_(now, last_rx)) {
while (millis() - tx_time < SHELLY_DIMMER_ACK_TIMEOUT) {
if (this->read_frame_()) {
return true;
}
delay(1);
@@ -298,31 +303,83 @@ size_t ShellyDimmer::frame_command_(uint8_t *data, uint8_t cmd, const uint8_t *c
}
// Checksum over the sequence, command, length and payload bytes
p = put_be16(p, sum16_bytes(data + 1, 3 + len));
p = put_be16(p, shelly_dimmer_checksum(data + 1, 3 + len));
*p++ = SHELLY_DIMMER_PROTO_END_BYTE;
return p - data;
}
bool ShellyDimmer::read_frame_(uint32_t now, uint32_t &last_rx) {
bool received = false;
auto handler = [this, &received](const uint8_t *frame, size_t len) {
// The checksum covers seq, cmd, length and payload
if (frame[len - 1] != SHELLY_DIMMER_PROTO_END_BYTE ||
encode_uint16(frame[len - 3], frame[len - 2]) != sum16_bytes(frame + 1, len - 4)) {
return false;
int ShellyDimmer::handle_byte_(uint8_t c) {
const uint8_t pos = this->buffer_pos_;
if (pos == 0) {
// Must be start byte.
return c == SHELLY_DIMMER_PROTO_START_BYTE ? 1 : -1;
} else if (pos < 4) {
// Header.
return 1;
}
// Decode payload length from header.
const uint8_t payload_len = this->buffer_[3];
if ((4 + payload_len + 3) > SHELLY_DIMMER_BUFFER_SIZE) {
return -1;
}
if (pos < 4 + payload_len + 1) {
// Payload.
return 1;
}
if (pos == 4 + payload_len + 1) {
// Verify checksum.
const uint16_t csum = encode_uint16(this->buffer_[pos - 1], c);
const uint16_t csum_verify = shelly_dimmer_checksum(&this->buffer_[1], 3 + payload_len);
if (csum != csum_verify) {
return -1;
}
this->handle_frame_(frame);
received = true;
return true;
};
this->read_frames(this->reader_, handler, now, last_rx, SHELLY_DIMMER_RX_GAP);
return received;
return 1;
}
if (pos == 4 + payload_len + 2) {
// Must be end byte.
return c == SHELLY_DIMMER_PROTO_END_BYTE ? 0 : -1;
}
return -1;
}
bool ShellyDimmer::handle_frame_(const uint8_t *frame) {
const uint8_t seq = frame[1];
const uint8_t cmd = frame[2];
const uint8_t payload_len = frame[3];
bool ShellyDimmer::read_frame_() {
while (this->available()) {
const uint8_t c = this->read();
this->buffer_[this->buffer_pos_] = c;
ESP_LOGV(TAG, "Read byte: 0x%02x (pos %d)", c, this->buffer_pos_);
switch (this->handle_byte_(c)) {
case 0: {
// Frame successfully received.
this->handle_frame_();
this->buffer_pos_ = 0;
return true;
}
case -1: {
// Failure.
this->buffer_pos_ = 0;
break;
}
case 1: {
// Need more data.
this->buffer_pos_++;
break;
}
}
}
return false;
}
bool ShellyDimmer::handle_frame_() {
const uint8_t seq = this->buffer_[1];
const uint8_t cmd = this->buffer_[2];
const uint8_t payload_len = this->buffer_[3];
ESP_LOGD(TAG, "Got frame: 0x%02x", cmd);
@@ -332,7 +389,7 @@ bool ShellyDimmer::handle_frame_(const uint8_t *frame) {
return false;
}
const uint8_t *payload = frame + PayloadLength::KNOWN_AT;
const uint8_t *payload = &this->buffer_[4];
// Handle response.
switch (cmd) {
@@ -8,7 +8,7 @@
#include "esphome/components/sensor/sensor.h"
#include "esphome/components/uart/uart.h"
#include "esphome/core/frame_reader.h"
#include <array>
namespace esphome::shelly_dimmer {
@@ -18,10 +18,7 @@ class ShellyDimmer final : public PollingComponent, public light::LightOutput, p
SUB_SENSOR(current)
private:
// Holds the largest reply, a 24 byte poll
static constexpr uint16_t SHELLY_DIMMER_BUFFER_SIZE = 32;
static constexpr uint8_t SHELLY_DIMMER_PROTO_START_BYTE = 0x01;
static constexpr uint8_t SHELLY_DIMMER_PROTO_END_BYTE = 0x04;
static constexpr uint16_t SHELLY_DIMMER_BUFFER_SIZE = 256;
public:
float get_setup_priority() const override { return setup_priority::LATE; }
@@ -55,11 +52,10 @@ class ShellyDimmer final : public PollingComponent, public light::LightOutput, p
GPIOPin *pin_nrst_;
GPIOPin *pin_boot0_;
// Frame parser state.
uint8_t seq_{0};
// Start byte, seq, cmd, payload length, payload, checksum (2), end byte
using PayloadLength = PayloadLengthU8<LengthField{.offset = 3, .trailer = 3}>;
LengthFrameReader<SHELLY_DIMMER_BUFFER_SIZE, FrameFormat<FrameHeader<SHELLY_DIMMER_PROTO_START_BYTE>, PayloadLength>>
reader_;
std::array<uint8_t, SHELLY_DIMMER_BUFFER_SIZE> buffer_;
uint8_t buffer_pos_{0};
// Firmware version.
uint8_t version_major_;
@@ -96,11 +92,16 @@ class ShellyDimmer final : public PollingComponent, public light::LightOutput, p
/// Frames a given command payload.
size_t frame_command_(uint8_t *data, uint8_t cmd, const uint8_t *payload, size_t len);
/// Handles a single byte as part of a protocol frame.
///
/// Returns -1 on failure, 0 when finished and 1 when more bytes needed.
int handle_byte_(uint8_t c);
/// Reads a response frame.
bool read_frame_(uint32_t now, uint32_t &last_rx);
bool read_frame_();
/// Handles a complete frame.
bool handle_frame_(const uint8_t *frame);
bool handle_frame_();
/// Reset STM32 with the BOOT0 pin set to the given value.
void reset_(bool boot0);
+61 -27
View File
@@ -1,5 +1,4 @@
#include "tuya_uart.h"
#include "esphome/core/application.h"
#include "esphome/core/gpio.h"
#include "esphome/core/helpers.h"
#include "esphome/core/log.h"
@@ -28,7 +27,7 @@ static constexpr uint32_t WIFI_STATUS_INTERVAL_ID = 1;
static constexpr uint32_t DATAPOINT_DUMP_TIMEOUT_ID = 2;
static constexpr uint32_t FORCE_TIME_SYNC_INTERVAL_ID = 3;
static const int COMMAND_DELAY = 10;
static constexpr uint32_t RECEIVE_TIMEOUT = 300;
static const int RECEIVE_TIMEOUT = 300;
static const int MAX_RETRIES = 5;
static constexpr uint32_t FORCE_TIME_SYNC_CHECK_INTERVAL = 1000;
@@ -55,12 +54,7 @@ void TuyaUART::setup() {
}
void TuyaUART::loop() {
auto handler = [this](const uint8_t *frame, size_t len) { return this->handle_frame_(frame, len); };
// Holds commands back for the whole read, the idle search and its log lines included
this->handling_frame_ = true;
this->read_frames(this->reader_, handler, App.get_loop_component_start_time(), this->last_rx_char_timestamp_,
RECEIVE_TIMEOUT);
this->handling_frame_ = false;
this->read_available([this](uint8_t b) { this->handle_char_(b); });
process_command_queue_();
}
@@ -98,30 +92,70 @@ void TuyaUART::dump_mcu_config_() {
#endif
}
void warn_too_long(size_t len) {
ESP_LOGW(TAG, "Skipped a %u byte frame, longer than the %u byte buffer", static_cast<unsigned>(len),
static_cast<unsigned>(MAX_FRAME_SIZE));
}
bool TuyaUART::validate_message_() {
uint32_t at = this->rx_message_.size() - 1;
auto *data = &this->rx_message_[0];
uint8_t new_byte = data[at];
// Byte 0: HEADER1 (always 0x55)
if (at == 0)
return new_byte == 0x55;
// Byte 1: HEADER2 (always 0xAA)
if (at == 1)
return new_byte == 0xAA;
// Byte 2: VERSION
// no validation for the following fields:
uint8_t version = data[2];
if (at == 2)
return true;
// Byte 3: COMMAND
uint8_t command = data[3];
if (at == 3)
return true;
// Byte 4: LENGTH1
// Byte 5: LENGTH2
if (at <= 5) {
// no validation for these fields
return true;
}
uint16_t length = encode_uint16(data[4], data[5]);
// wait until all data is read
if (at - 6 < length)
return true;
// Byte 6+LEN: CHECKSUM - sum of all bytes (including header) modulo 256
uint8_t rx_checksum = new_byte;
const uint8_t calc_checksum = sum_bytes(data, 6 + length);
bool TuyaUART::handle_frame_(const uint8_t *frame, size_t len) {
// Last byte: sum of all bytes before it, header included, modulo 256
const uint8_t rx_checksum = frame[len - 1];
const uint8_t calc_checksum = sum_bytes(frame, len - 1);
if (rx_checksum != calc_checksum) {
ESP_LOGW(TAG, "Tuya Received invalid message checksum %02X!=%02X", rx_checksum, calc_checksum);
return false;
}
const uint8_t version = frame[2];
const uint8_t command = frame[3];
const uint8_t *message_data = frame + TuyaLength::KNOWN_AT;
const size_t length = len - TuyaLength::KNOWN_AT - 1;
// valid message
const uint8_t *message_data = data + 6;
#if ESPHOME_LOG_LEVEL >= ESPHOME_LOG_LEVEL_VERBOSE
char hex_buf[format_hex_pretty_size(MAX_DATAPOINT_LOG_BYTES)];
ESP_LOGV(TAG, "Received Tuya: CMD=0x%02X VERSION=%u DATA=[%s] INIT_STATE=%u", command, version,
format_hex_pretty_to(hex_buf, message_data, length), static_cast<uint8_t>(this->init_state_));
#endif
this->handle_command_(command, version, message_data, length);
return true;
// return false to reset rx buffer
return false;
}
void TuyaUART::handle_char_(uint8_t c) {
this->rx_message_.push_back(c);
if (!this->validate_message_()) {
this->rx_message_.clear();
} else {
this->last_rx_char_timestamp_ = millis();
}
}
void TuyaUART::handle_command_(uint8_t command, uint8_t version, const uint8_t *buffer, size_t len) {
@@ -454,8 +488,11 @@ void TuyaUART::process_command_queue_() {
uint32_t now = millis();
uint32_t delay = now - this->last_command_timestamp_;
// A reply still coming in, or held behind a corrupt length, is waited for
if (this->expected_response_.has_value() && delay > RECEIVE_TIMEOUT && !this->reader_.receiving()) {
if (now - this->last_rx_char_timestamp_ > RECEIVE_TIMEOUT) {
this->rx_message_.clear();
}
if (this->expected_response_.has_value() && delay > RECEIVE_TIMEOUT) {
this->expected_response_.reset();
if (init_state_ != TuyaInitState::INIT_DONE) {
if (++this->init_retries_ >= MAX_RETRIES) {
@@ -470,11 +507,8 @@ void TuyaUART::process_command_queue_() {
}
// Left check of delay since last command in case there's ever a command sent by calling send_raw_command_ directly
// A partial frame kept by an idle flush does not hold commands back; a command clears it, so it cannot merge into
// the reply
if (delay > COMMAND_DELAY && !this->command_queue_.empty() && !this->reader_.receiving() && !this->handling_frame_ &&
if (delay > COMMAND_DELAY && !this->command_queue_.empty() && this->rx_message_.empty() &&
!this->expected_response_.has_value()) {
this->reader_.drop_kept();
this->send_raw_command_(command_queue_.front());
if (!this->expected_response_.has_value())
this->command_queue_.erase(command_queue_.begin());
+3 -15
View File
@@ -6,8 +6,6 @@
#include "tuya.h"
#include "esphome/components/uart/uart.h"
#include "esphome/core/frame_reader.h"
#include "esphome/core/helpers.h"
#ifdef USE_TIME
#include "esphome/components/time/real_time_clock.h"
@@ -16,14 +14,6 @@
namespace esphome::tuya {
// Longest frame accepted, as in Tasmota; keeps the reader's positions 8 bit
static constexpr size_t MAX_FRAME_SIZE = 255;
// 55 AA, version, command, payload length (2, BE), payload, checksum
using TuyaLength = PayloadLengthU16BE<LengthField{.offset = 4, .trailer = 1}>;
void warn_too_long(size_t len);
using TuyaFrame = FrameFormat<FrameHeader<0x55, 0xAA>, WarnIfLonger<TuyaLength, MAX_FRAME_SIZE, warn_too_long>>;
enum class TuyaCommandType : uint8_t {
HEARTBEAT = 0x00,
PRODUCT_QUERY = 0x01,
@@ -80,9 +70,9 @@ class TuyaUART final : public Tuya, public uart::UARTDevice {
protected:
void dump_mcu_config_();
/// Returns false for a frame that fails the checksum
bool handle_frame_(const uint8_t *frame, size_t len);
void handle_char_(uint8_t c);
void handle_datapoints_(const uint8_t *buffer, size_t len);
bool validate_message_();
void handle_command_(uint8_t command, uint8_t version, const uint8_t *buffer, size_t len);
void send_raw_command_(TuyaCommand command);
@@ -117,13 +107,11 @@ class TuyaUART final : public Tuya, public uart::UARTDevice {
uint32_t last_command_timestamp_ = 0;
uint32_t last_rx_char_timestamp_ = 0;
std::unique_ptr<char[]> product_;
std::vector<uint8_t> rx_message_;
std::vector<TuyaCommand> command_queue_;
optional<TuyaCommandType> expected_response_{};
uint8_t wifi_status_ = -1;
bool product_invalid_{false};
// Holds commands back while a received frame is handled
bool handling_frame_{false};
LengthFrameReader<MAX_FRAME_SIZE, TuyaFrame> reader_;
};
} // namespace esphome::tuya
-13
View File
@@ -90,19 +90,6 @@ class UARTDevice {
count);
}
/// Feed the waiting bytes to a LengthFrameReader; on a pass with none, gap_ms after last_rx, flush a stalled frame.
/// Returns false when a UART read failed.
template<size_t N = 64, typename R, typename F>
bool read_frames(R &reader, F &&handle, uint32_t now, uint32_t &last_rx, uint32_t gap_ms = 250) {
if (const size_t count = this->available()) {
last_rx = now;
return this->read_available<N>([&reader, &handle](uint8_t c) { reader.feed(c, handle); }, count);
}
if (reader.length() != 0 && now - last_rx >= gap_ms)
reader.flush(handle);
return true;
}
/// Read and drop every byte waiting in the receive buffer.
void discard_available();
+4 -4
View File
@@ -35,7 +35,7 @@ WatchdogManager::~WatchdogManager() {
void WatchdogManager::set_timeout_(uint32_t timeout_ms) {
ESP_LOGV(TAG, "Adjusting WDT to %" PRIu32 "ms", timeout_ms);
#ifdef USE_ESP32
#ifdef USE_ESP32_TASK_WDT
esp_task_wdt_config_t wdt_config = {
.timeout_ms = timeout_ms,
.idle_core_mask = 0,
@@ -51,7 +51,7 @@ void WatchdogManager::set_timeout_(uint32_t timeout_ms) {
wdt_config.trigger_panic = true;
#endif
esp_task_wdt_reconfigure(&wdt_config);
#endif // USE_ESP32
#endif // USE_ESP32_TASK_WDT
#ifdef USE_RP2
watchdog_enable(timeout_ms, true);
@@ -61,9 +61,9 @@ void WatchdogManager::set_timeout_(uint32_t timeout_ms) {
uint32_t WatchdogManager::get_timeout_() {
uint32_t timeout_ms = 0;
#ifdef USE_ESP32
#ifdef USE_ESP32_TASK_WDT
timeout_ms = (uint32_t) CONFIG_ESP_TASK_WDT_TIMEOUT_S * 1000;
#endif // USE_ESP32
#endif // USE_ESP32_TASK_WDT
#ifdef USE_RP2
timeout_ms = watchdog_get_count() / 1000;
+20 -2
View File
@@ -7,7 +7,7 @@ import re
from typing import Any
import esphome.codegen as cg
from esphome.components import web_server_base
from esphome.components import web_server_base, web_server_idf
from esphome.components.json import enable_arena
from esphome.components.logger import request_log_listener
from esphome.components.web_server_base import (
@@ -234,6 +234,8 @@ def _consume_web_server_sockets(config: ConfigType) -> ConfigType:
# (browser opens connections for page resources, SSE event stream, and POST
# requests for entity control which may linger before closing)
web_server_base.add_httpd_clients(5, DOMAIN)
# Event streams send from the main loop straight to the socket, which would bypass TLS.
web_server_idf.require_plain_http(DOMAIN)
return config
@@ -614,9 +616,25 @@ async def to_code(config: ConfigType) -> None:
def FILTER_SOURCE_FILES() -> list[str]:
"""Filter out web_server_v1.cpp when version is not 1."""
"""Filter out web_server_v1.cpp when version is not 1, the ESP32 event stream elsewhere, and the server on host."""
files_to_filter: list[str] = []
# The host has no web server; it builds only sse_chunk.cpp, for its unit tests
if CORE.is_host:
files_to_filter.extend(
(
"event_source.cpp",
"list_entities.cpp",
"web_server.cpp",
"web_server_v1.cpp",
)
)
return files_to_filter
# The /events implementation is ESP32 only
if not CORE.is_esp32:
files_to_filter.extend(("event_source.cpp", "sse_chunk.cpp"))
# web_server_v1.cpp is only needed when version is 1
config = CORE.config.get(DOMAIN, {})
if config.get(CONF_VERSION, 2) != 1:
@@ -0,0 +1,491 @@
#include "event_source.h"
#if defined(USE_ESP32) && defined(USE_WEBSERVER)
#include <algorithm>
#include <cstring>
#include <iterator>
#include "esphome/core/application.h"
#include "esphome/core/helpers.h"
#include "esphome/core/log.h"
#include "web_server.h"
namespace esphome::web_server {
ESPHOME_LOG_TAG(TAG, "web_server_idf");
// Only prepare_json_() may hold the JSON arena: every other frame in this file is capped below one
// arena. Measured at -Os on GCC 14; newer toolchains stay checked on purpose, older ones skip it.
#if defined(__GNUC__) && !defined(__clang__) && __GNUC__ >= 14 && defined(__OPTIMIZE_SIZE__)
#pragma GCC diagnostic error "-Wstack-usage=2048"
#endif
AsyncEventSource::~AsyncEventSource() {
LockGuard guard{this->pending_mutex_};
for (auto *vec : {&this->sessions_, &this->pending_sessions_}) {
for (auto *ses : *vec) {
delete ses; // NOLINT(cppcoreguidelines-owning-memory)
}
}
}
void AsyncEventSource::handleRequest(AsyncWebServerRequest *request) {
// 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->web_server_);
{
LockGuard guard{this->pending_mutex_};
this->pending_sessions_.push_back(rsp);
this->has_pending_sessions_.store(true, std::memory_order_release);
}
this->web_server_->enable_loop_soon_any_context();
}
// clang-analyzer traces a false-positive leak path from loop() through
// adopt_pending_sessions_main_loop_() into start_session_main_loop_() and
// finally ArduinoJson. Suppress along the entire in-our-code call chain.
// NOLINTBEGIN(clang-analyzer-cplusplus.NewDeleteLeaks)
bool AsyncEventSource::loop() {
// Fast path: one atomic load per tick. Slow path is out-of-line on connect.
if (this->has_pending_sessions_.load(std::memory_order_acquire)) {
this->adopt_pending_sessions_main_loop_();
}
// Clean up dead sessions safely
// This follows the ESP-IDF pattern where free_ctx marks resources as dead
// and the main loop handles the actual cleanup to avoid race conditions
for (size_t i = 0; i < this->sessions_.size();) {
auto *ses = this->sessions_[i];
// If the session has a dead socket (marked by destroy callback)
if (ses->safe_to_delete()) {
// destroy() already logged the close with the fd; don't double-log here.
delete ses; // NOLINT(cppcoreguidelines-owning-memory)
// Remove by swapping with last element (O(1) removal, order doesn't matter for sessions)
this->sessions_[i] = this->sessions_.back();
this->sessions_.pop_back();
} else {
ses->loop();
++i;
}
}
// A session still waiting for httpd to commit its context keeps the loop alive too
return !this->sessions_.empty() || this->has_pending_sessions_.load(std::memory_order_acquire);
}
void AsyncEventSource::adopt_pending_sessions_main_loop_() {
std::vector<AsyncEventSourceResponse *> incoming;
{
LockGuard guard{this->pending_mutex_};
incoming.swap(this->pending_sessions_);
this->has_pending_sessions_.store(false, std::memory_order_relaxed);
}
for (auto *rsp : incoming) {
// Already disconnected? Drop it; skip session start on a dead session.
if (rsp->safe_to_delete()) {
delete rsp; // NOLINT(cppcoreguidelines-owning-memory)
continue;
}
// httpd commits the session context only after the creating handler returns, so stay
// pending until then; httpd_req_cleanup() always commits it or calls destroy()
if (!rsp->is_attached()) {
LockGuard guard{this->pending_mutex_};
this->pending_sessions_.push_back(rsp);
this->has_pending_sessions_.store(true, std::memory_order_release);
continue;
}
this->sessions_.push_back(rsp);
rsp->start_session_main_loop_();
}
}
// NOLINTEND(clang-analyzer-cplusplus.NewDeleteLeaks)
void AsyncEventSource::try_send_nodefer(const char *message, size_t message_len, const char *event, uint32_t id,
uint32_t reconnect) {
for (auto *ses : this->sessions_) {
if (ses->is_connected()) {
ses->try_send_nodefer(message, message_len, event, id, reconnect);
}
}
}
void AsyncEventSource::deferrable_send_state(void *source, bool detail_all, message_generator_t *message_generator) {
// Skip if no connected clients to avoid unnecessary processing
if (this->empty())
return;
for (auto *ses : this->sessions_) {
if (ses->is_connected()) {
ses->deferrable_send_state(source, detail_all, message_generator);
}
}
}
AsyncEventSourceResponse::AsyncEventSourceResponse(AsyncWebServerRequest *request, WebServer *ws)
: web_server_(ws), entities_iterator_(this) {
// Httpd task only. start_session_main_loop_() sends the greeting and starts the iterator.
this->begin(request, SSE_STATUS, SSE_CONTENT_TYPE, SSE_HEADERS, SSE_FIRST_CHUNK);
}
static_assert(web_server_base::stream_head_iov_count(std::size(SSE_HEADERS) + WEB_SERVER_DEFAULT_HEADERS_COUNT) <=
web_server_base::STREAM_HEAD_MAX_IOV,
"the event stream head must go out in one write");
// NOLINTBEGIN(clang-analyzer-cplusplus.NewDeleteLeaks) false positive with ArduinoJson
void AsyncEventSourceResponse::start_session_main_loop_() {
auto *ws = this->web_server_;
// The tcp send buffer is empty on connect. A refusal is a closing session or a failed tail
// allocation; nothing retries the greeting, so close and let the client reconnect.
auto message = ws->get_config_json();
if (!this->try_send_nodefer(message.c_str(), message.size(), "ping", millis(), 30000)) {
ESP_LOGW(TAG, "Config not sent to fd %d", this->log_id());
this->close();
return;
}
#ifdef USE_WEBSERVER_SORTING
for (auto &group : ws->sorting_groups_) {
json::JsonBuilder builder;
JsonObject root = builder.root();
root["name"] = json::linked(group.second.name);
root["sorting_weight"] = group.second.weight;
message = builder.serialize();
// a (very) large number of these should be able to be queued initially without defer
// since the only thing in the send buffer at this point is the initial ping/config.
// A refusal means the socket is full or closing; the remaining groups are not sent. The
// session stays up on purpose: partial grouping beats a reconnect loop on a slow link.
if (!this->try_send_nodefer(message.c_str(), message.size(), "sorting_group")) {
ESP_LOGW(TAG, "Sorting groups not sent to fd %d", this->log_id());
break;
}
}
#endif
this->entities_iterator_.begin(ws->include_internal_);
}
// NOLINTEND(clang-analyzer-cplusplus.NewDeleteLeaks)
// helper for allowing only unique entries in the queue
void AsyncEventSourceResponse::deq_push_back_with_dedup_(void *source, message_generator_t *message_generator) {
DeferredEvent item(source, message_generator);
// Use range-based for loop instead of std::find_if to reduce template instantiation overhead and binary size
for (auto &event : this->deferred_queue_) {
if (event == item) {
return; // Already in queue, no need to update since items are equal
}
}
this->deferred_queue_.push_back(item);
}
void AsyncEventSourceResponse::process_deferred_queue_() {
if (this->is_closing()) {
return;
}
while (!deferred_queue_.empty()) {
DeferredEvent &de = deferred_queue_.front();
if (this->send_json_(de.source_, de.message_generator_)) {
if (this->is_closing() || deferred_queue_.empty()) {
return;
}
// O(n) but memory efficiency is more important than speed here which is why std::vector was chosen
deferred_queue_.erase(deferred_queue_.begin());
} else {
break;
}
}
}
void AsyncEventSourceResponse::release_buffers_() {
std::vector<DeferredEvent>().swap(this->deferred_queue_);
this->tail_.reset();
this->tail_cap_ = 0;
this->tail_len_ = 0;
this->tail_sent_ = 0;
}
void AsyncEventSourceResponse::drain_tail_() {
if (this->sending_ || this->is_closing() || this->tail_len_ == 0) {
return;
}
SendGuard guard{*this};
const size_t remaining = this->tail_len_ - this->tail_sent_;
const ssize_t sent = this->write(this->tail_.get() + this->tail_sent_, remaining);
if (sent <= 0) {
return; // write() owns the stall clock and the close
}
this->no_memory_logged_ = false;
this->tail_sent_ += sent;
if (this->tail_sent_ < this->tail_len_) {
ESP_LOGV(TAG, "Partial send: %zd/%zu bytes (total: %u/%u)", sent, remaining, this->tail_sent_, this->tail_len_);
return;
}
// Fully sent; the storage stays for the next stall
this->tail_len_ = 0;
}
bool AsyncEventSourceResponse::reserve_tail_(size_t len) {
if (this->tail_cap_ >= len) {
return true;
}
if (len > TAIL_MAX_SIZE) {
return false;
}
// Nothing is pending while the tail grows, so free the old block first. PREFER_INTERNAL keeps
// the tail where plain new put it.
this->tail_.reset();
this->tail_ = RAMAllocator<uint8_t>(RAMAllocator<uint8_t>::PREFER_INTERNAL).make_unique_array_for_overwrite(len);
this->tail_cap_ = this->tail_ ? len : 0;
return this->tail_cap_ != 0;
}
bool AsyncEventSourceResponse::stash_chunk_(const char *prefix, size_t prefix_len, const char *message,
size_t message_len, size_t total, size_t sent) {
// A log event of nothing but line breaks is the largest chunk; reserve_tail_() must never
// refuse it for size, only for memory
static_assert(SSE_SEP_LEN * LOG_EVENT_MAX_LEN + PREFIX_BUF_SIZE + SSE_SUFFIX_LEN <= TAIL_MAX_SIZE,
"the log cut in web_server.h must keep a worst case log event inside the tail ceiling");
if (!this->reserve_tail_(total)) {
if (sent != 0) {
// Part of the chunk is on the wire, so the stream is broken and the client has to go
ESP_LOGW(TAG, "Cannot buffer a %zu byte chunk, closing", total);
this->close();
} else {
this->tail_alloc_failed_(total); // nothing on the wire, the caller retries on the stall clock
}
return false;
}
uint8_t *dst = this->tail_.get();
std::memcpy(dst, prefix, prefix_len);
dst += prefix_len;
for_each_chunk_piece(
message, message_len,
[](void *ctx, const char *piece, size_t len) {
auto &out = *static_cast<uint8_t **>(ctx);
std::memcpy(out, piece, len);
out += len;
},
&dst);
this->tail_len_ = total;
this->tail_sent_ = sent;
return true;
}
void AsyncEventSourceResponse::loop() {
AsyncStreamResponse::loop();
// A client that left may wait for httpd's queued work before it can be freed; stop filling it meanwhile
if (this->is_closing() || !this->is_connected()) {
if (this->tail_cap_ != 0 || this->deferred_queue_.capacity() != 0) {
this->release_buffers_();
}
return;
}
drain_tail_();
process_deferred_queue_();
if (this->is_closing())
return;
// One step per loop; refusals retry next pass
this->entities_iterator_.try_advance(1);
}
#if defined(__GNUC__) && !defined(__clang__) && __GNUC__ >= 14 && defined(__OPTIMIZE_SIZE__)
#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wstack-usage=" // the one frame that holds the JSON arena
#endif
AsyncEventSourceResponse::JsonPrepareResult AsyncEventSourceResponse::prepare_json_(void *source,
message_generator_t *generator,
char *buffer, size_t *size_out) {
// Log lines emitted while the generator runs are dropped for this session: on the large path they would fill
// tail_ before it is overwritten, and on the small path they would go out ahead of the event being built.
SendGuard guard{*this};
json::JsonArena<json::JSON_ARENA_SIZE> arena;
json::JsonBuilder builder(&arena);
generator(this->web_server_, source, builder);
*size_out = builder.serialize_to(buffer, JSON_BUF_SIZE);
if (*size_out < JSON_BUF_SIZE) {
return JsonPrepareResult::JSON_PREPARE_RESULT_BUFFER;
}
char prefix[PREFIX_BUF_SIZE];
const size_t prefix_len = build_chunk_prefix(prefix, sizeof(prefix), "state", 0, 0, true);
// Nothing reaches the wire until the complete document and its framing are owned by the caller.
size_t json_len = 0;
size_t cap = std::max<size_t>(JSON_BUF_SIZE * 2, this->tail_cap_);
for (;;) {
if (!this->reserve_tail_(cap)) {
*size_out = cap;
return JsonPrepareResult::JSON_PREPARE_RESULT_NO_MEMORY;
}
const size_t room = cap - prefix_len - SSE_SUFFIX_LEN;
json_len = builder.serialize_to(reinterpret_cast<char *>(this->tail_.get()) + prefix_len, room);
if (json_len < room) {
break;
}
if (cap >= TAIL_MAX_SIZE) {
*size_out = room;
return JsonPrepareResult::JSON_PREPARE_RESULT_TOO_LARGE;
}
cap = std::min<size_t>(cap * 2, TAIL_MAX_SIZE);
}
const size_t total = prefix_len + json_len + SSE_SUFFIX_LEN;
write_chunk_header(prefix, total - CHUNK_HDR_LEN - CHUNK_END_LEN);
uint8_t *dst = this->tail_.get();
std::memcpy(dst, prefix, prefix_len);
std::memcpy(dst + prefix_len + json_len, SSE_SUFFIX, SSE_SUFFIX_LEN);
this->tail_len_ = total;
this->tail_sent_ = 0;
return JsonPrepareResult::JSON_PREPARE_RESULT_TAIL;
}
#if defined(__GNUC__) && !defined(__clang__) && __GNUC__ >= 14 && defined(__OPTIMIZE_SIZE__)
#pragma GCC diagnostic pop
#endif
bool AsyncEventSourceResponse::send_json_(void *source, message_generator_t *generator) {
if (!this->ready_to_send_()) {
return false;
}
char buffer[JSON_BUF_SIZE];
size_t size;
const auto result = this->prepare_json_(source, generator, buffer, &size);
switch (result) {
case JsonPrepareResult::JSON_PREPARE_RESULT_BUFFER:
return this->try_send_nodefer(buffer, size, "state");
case JsonPrepareResult::JSON_PREPARE_RESULT_TAIL:
this->drain_tail_();
return true;
case JsonPrepareResult::JSON_PREPARE_RESULT_NO_MEMORY: {
SendGuard guard{*this};
this->tail_alloc_failed_(size);
return false;
}
case JsonPrepareResult::JSON_PREPARE_RESULT_TOO_LARGE: {
SendGuard guard{*this};
ESP_LOGW(TAG, "State event does not fit %zu bytes, dropped", size);
this->tail_.reset();
this->tail_cap_ = 0;
this->stall_.clear();
this->no_memory_logged_ = false;
return true;
}
}
return false;
}
void AsyncEventSourceResponse::tail_alloc_failed_(size_t cap) {
// Same stall clock as a socket that stops draining, so a session cannot retry forever. A refused write may
// already have started it, so the warning keeps its own flag.
if (!this->no_memory_logged_) {
this->no_memory_logged_ = true;
ESP_LOGW(TAG, "No memory for a %zu byte chunk", cap);
}
const uint32_t now = App.get_loop_component_start_time();
if (!this->stall_.is_running()) {
this->stall_.start(now);
return;
}
if (this->stall_.is_expired(now, SEND_STALL_TIMEOUT_MS)) {
ESP_LOGW(TAG, "Closing EventSource after %" PRIu32 " ms without memory", this->stall_.elapsed(now));
this->close();
}
}
bool AsyncEventSourceResponse::ready_to_send_() {
if (this->sending_ || !this->is_connected() || this->is_closing()) {
return false;
}
drain_tail_();
return !this->is_closing() && this->tail_len_ == 0;
}
bool AsyncEventSourceResponse::try_send_nodefer(const char *message, size_t message_len, const char *event, uint32_t id,
uint32_t reconnect) {
if (!this->ready_to_send_()) {
return false;
}
SendGuard guard{*this};
// Everything after the prefix goes out straight from the caller's buffer
char prefix[PREFIX_BUF_SIZE];
const size_t prefix_len = build_chunk_prefix(prefix, sizeof(prefix), event, id, reconnect, message != nullptr);
if (message == nullptr && prefix_len == CHUNK_HDR_LEN) {
return true; // Match ESPAsyncWebServer: nothing to send
}
if (prefix_len >= PREFIX_BUF_SIZE - 1) {
// The appenders truncate silently, which would put a malformed event on the wire
ESP_LOGW(TAG, "Event name too long, dropped");
return true;
}
// Gather list: the prefix, then the data lines and their separators from the message
struct Gather {
web_server_base::StreamIovec iov[MAX_SEND_IOV]; // left uninitialized on purpose
int iovcnt{1};
size_t total{0};
bool fits{true};
} g;
g.total = prefix_len;
for_each_chunk_piece(
message, message_len,
[](void *ctx, const char *piece, size_t len) {
auto &g = *static_cast<Gather *>(ctx);
g.total += len;
if (len == 0) {
return;
}
if (g.iovcnt == MAX_SEND_IOV) {
g.fits = false;
return;
}
g.iov[g.iovcnt++] = {const_cast<char *>(piece), len};
},
&g);
// The header and the terminator are not part of the chunk length
write_chunk_header(prefix, g.total - CHUNK_HDR_LEN - CHUNK_END_LEN);
g.iov[0] = {prefix, prefix_len};
// A message with more lines than the list holds skips straight to the tail
const ssize_t sent = g.fits ? this->write(g.iov, g.iovcnt) : 0;
if (sent < 0) {
return false;
}
if (sent > 0) {
this->no_memory_logged_ = false;
}
if (static_cast<size_t>(sent) == g.total) {
return true;
}
// The caller's buffers do not outlive this call, so keep the chunk and continue from loop()
return this->stash_chunk_(prefix, prefix_len, message, message_len, g.total, sent);
}
void AsyncEventSourceResponse::deferrable_send_state(void *source, bool detail_all,
message_generator_t *message_generator) {
// Let the detail_all listing finish before bare state events, so the web UI never shows unnamed entries and
// initial connect sends less
if (!detail_all && !this->entities_iterator_.completed())
return;
drain_tail_();
process_deferred_queue_();
if (this->is_closing()) {
return;
}
if (this->tail_len_ != 0 || !deferred_queue_.empty()) {
// outgoing event buffer or deferred queue still not empty which means downstream tcp send buffer full, no point
// trying to send first
deq_push_back_with_dedup_(source, message_generator);
} else {
// A send error closes the session and clears the queue; nothing is queued after that
if (!this->send_json_(source, message_generator) && !this->is_closing()) {
deq_push_back_with_dedup_(source, message_generator);
}
}
}
} // namespace esphome::web_server
#endif // USE_ESP32 && USE_WEBSERVER
@@ -0,0 +1,175 @@
#pragma once
#include "esphome/core/defines.h"
#if defined(USE_ESP32) && defined(USE_WEBSERVER)
#include "esphome/components/json/json_util.h"
#include "esphome/components/web_server_base/stream_response.h"
#include "esphome/core/helpers.h"
#include "esphome/core/string_ref.h"
#include "list_entities.h"
#include "sse_chunk.h"
#include <atomic>
#include <cstdint>
#include <vector>
namespace esphome::web_server {
class WebServer;
class AsyncEventSource;
class AsyncEventSourceResponse;
using message_generator_t = void(WebServer *, void *, json::JsonBuilder &);
/*
This class holds a pointer to the source component that wants to publish a state event, and a pointer to a function
that will lazily generate that event. The two pointers allow dedup in the deferred queue if multiple publishes for
the same component are backed up, and take up only two pointers of memory. The entry in the deferred queue (a
std::vector) is the DeferredEvent instance itself (not a pointer to one elsewhere in heap) so still only two pointers
per entry (and no heap fragmentation). Even 100 backed up events (you'd have to have at least 100 sensors publishing
because of dedup) would take up only 0.8 kB.
*/
struct DeferredEvent {
friend class AsyncEventSourceResponse;
protected:
void *source_;
message_generator_t *message_generator_;
public:
DeferredEvent(void *source, message_generator_t *message_generator)
: source_(source), message_generator_(message_generator) {}
bool operator==(const DeferredEvent &test) const {
return (source_ == test.source_ && message_generator_ == test.message_generator_);
}
};
static_assert(sizeof(DeferredEvent) == sizeof(void *) + sizeof(message_generator_t *),
"DeferredEvent should have no padding");
class AsyncEventSourceResponse : public web_server_base::AsyncStreamResponse {
friend class AsyncEventSource;
public:
bool try_send_nodefer(const char *message, size_t message_len, const char *event = nullptr, uint32_t id = 0,
uint32_t reconnect = 0);
// detail_all events go out while entities are still being listed; state events wait until listing is done
void deferrable_send_state(void *source, bool detail_all, message_generator_t *message_generator);
// The old event name argument would otherwise convert to true
void deferrable_send_state(void *source, const char *event_type, message_generator_t *message_generator) = delete;
void loop();
protected:
AsyncEventSourceResponse(AsyncWebServerRequest *request, WebServer *ws);
// Main-loop only: sends initial ping/config/sorting_groups, starts entity iterator.
void start_session_main_loop_();
void deq_push_back_with_dedup_(void *source, message_generator_t *message_generator);
void process_deferred_queue_();
// A new chunk may go out: not re-entered from a log line, session alive, tail empty
bool ready_to_send_();
// Push what is left of the chunk in tail_ to the socket
void drain_tail_();
// Grow tail_ to hold len bytes, kept at its high-water mark. False on OOM.
bool reserve_tail_(size_t len);
// Keep the whole chunk in tail_ and continue from sent; false when the tail cannot be allocated.
bool stash_chunk_(const char *prefix, size_t prefix_len, const char *message, size_t message_len, size_t total,
size_t sent);
// Send a state event; JSON too large for the stack buffer is serialized into tail_ instead
bool send_json_(void *source, message_generator_t *generator);
enum class JsonPrepareResult : uint8_t {
JSON_PREPARE_RESULT_BUFFER,
JSON_PREPARE_RESULT_TAIL,
JSON_PREPARE_RESULT_NO_MEMORY,
JSON_PREPARE_RESULT_TOO_LARGE,
};
// Keep the arena out of the caller's frame so it is gone before the state send.
// size_out: BUFFER = JSON length, NO_MEMORY = requested tail capacity, TOO_LARGE = available JSON room;
// unused for TAIL.
JsonPrepareResult __attribute__((noinline)) prepare_json_(void *source, message_generator_t *generator, char *buffer,
size_t *size_out);
// Warn once per stall, and close the session once the stall timeout passes with no memory for the tail
void tail_alloc_failed_(size_t cap);
// A log line emitted inside a send re-enters try_send_nodefer on this session; refuse it
struct SendGuard {
AsyncEventSourceResponse &owner;
explicit SendGuard(AsyncEventSourceResponse &owner) : owner(owner) { owner.sending_ = true; }
~SendGuard() { this->owner.sending_ = false; }
};
// Frees the tail and the queue's storage once the session is closing
void release_buffers_();
std::vector<DeferredEvent> deferred_queue_;
WebServer *web_server_;
ListEntitiesIterator entities_iterator_;
// One chunk the socket did not take whole, allocated on the first stall; the only heap use
// on the send path
RAMUniquePtr<uint8_t[]> tail_;
uint16_t tail_cap_{0};
uint16_t tail_len_{0}; // Zero means nothing pending
uint16_t tail_sent_{0};
bool sending_{false};
// The no-memory warning went out during the current stall; cleared on send progress
bool no_memory_logged_{false};
// The longest multi line log message in the tree (a climate dump_config) has 22 lines; a
// longer one goes through the tail
static constexpr size_t MAX_SEND_LINES = 22;
static constexpr size_t MAX_SEND_IOV = 1 + 2 * MAX_SEND_LINES;
// Chunk header, retry/id/event lines and the first "data: "
static constexpr size_t PREFIX_BUF_SIZE = 128;
// Stack buffer for a state event's JSON; a larger document is serialized into the tail
static constexpr size_t JSON_BUF_SIZE = 1024;
// Same ceiling JsonBuilder::serialize() applies (max_heap_size in json_util.cpp); a larger
// document is dropped before anything is on the wire
static constexpr size_t JSON_MAX_SIZE = 5120;
// Most RAM a stalled session keeps: the largest state document plus any accepted framing
static constexpr size_t TAIL_MAX_SIZE = JSON_MAX_SIZE + PREFIX_BUF_SIZE + SSE_SUFFIX_LEN;
};
class AsyncEventSource : public AsyncWebHandler {
friend class AsyncEventSourceResponse;
public:
AsyncEventSource(StringRef url, WebServer *ws) : url_(url), web_server_(ws) {}
~AsyncEventSource() override;
// NOLINTNEXTLINE(readability-identifier-naming)
bool canHandle(AsyncWebServerRequest *request) const override {
if (request->method() != HTTP_GET)
return false;
char url_buf[AsyncWebServerRequest::URL_BUF_SIZE];
return request->url_to(url_buf) == this->url_;
}
// NOLINTNEXTLINE(readability-identifier-naming)
void handleRequest(AsyncWebServerRequest *request) override;
void try_send_nodefer(const char *message, size_t message_len, const char *event = nullptr, uint32_t id = 0,
uint32_t reconnect = 0);
void deferrable_send_state(void *source, bool detail_all, message_generator_t *message_generator);
// The old event name argument would otherwise convert to true
void deferrable_send_state(void *source, const char *event_type, message_generator_t *message_generator) = delete;
/// Returns true if there are sessions remaining (including pending cleanup).
bool loop();
bool empty() { return this->count() == 0; }
size_t count() const { return this->sessions_.size(); }
protected:
// Cold path: move sessions from pending_sessions_ into sessions_ and greet each one.
void __attribute__((noinline, cold)) adopt_pending_sessions_main_loop_();
StringRef url_; // Must outlive this object (string literal)
// Main-loop only. Vector: SSE sessions are 1-5 connections, linear search beats set.
std::vector<AsyncEventSourceResponse *> sessions_;
// Httpd-task intake; guarded by pending_mutex_, gated by has_pending_sessions_.
std::vector<AsyncEventSourceResponse *> pending_sessions_;
Mutex pending_mutex_;
WebServer *web_server_;
std::atomic<bool> has_pending_sessions_{false};
};
} // namespace esphome::web_server
#endif // USE_ESP32 && USE_WEBSERVER
@@ -8,14 +8,8 @@
namespace esphome::web_server {
#ifdef USE_ESP32
ListEntitiesIterator::ListEntitiesIterator(const WebServer *ws, AsyncEventSource *es) : web_server_(ws), events_(es) {}
#elif USE_ARDUINO
ListEntitiesIterator::ListEntitiesIterator(const WebServer *ws, DeferredUpdateEventSource *es)
: web_server_(ws), events_(es) {}
#endif
void ListEntitiesIterator::send_all_(void *source, message_generator_t *generator) {
this->events_->deferrable_send_state(source, true, generator);
this->session_->deferrable_send_state(source, true, generator);
}
#ifdef USE_BINARY_SENSOR
+9 -19
View File
@@ -1,33 +1,28 @@
#pragma once
#include "esphome/core/defines.h"
#ifdef USE_WEBSERVER
#if defined(USE_WEBSERVER) && (defined(USE_ESP32) || defined(USE_ARDUINO))
#include "esphome/core/component.h"
#include "esphome/core/component_iterator.h"
namespace esphome::json {
class JsonBuilder;
} // namespace esphome::json
namespace esphome::web_server_idf {
#ifdef USE_ESP32
class AsyncEventSource;
#endif
} // namespace esphome::web_server_idf
namespace esphome::web_server {
#if !defined(USE_ESP32) && defined(USE_ARDUINO)
// The /events session the iterator lists entities to
#ifdef USE_ESP32
class AsyncEventSourceResponse;
using EventSession = AsyncEventSourceResponse;
#elif defined(USE_ARDUINO)
class DeferredUpdateEventSource;
using EventSession = DeferredUpdateEventSource;
#endif
class WebServer;
class ListEntitiesIterator final : public ComponentIterator {
public:
#ifdef USE_ESP32
ListEntitiesIterator(const WebServer *ws, esphome::web_server_idf::AsyncEventSource *es);
#elif defined(USE_ARDUINO)
ListEntitiesIterator(const WebServer *ws, DeferredUpdateEventSource *es);
#endif
explicit ListEntitiesIterator(EventSession *session) : session_(session) {}
// Entity overrides (generated from entity_types.h).
// Implementations live in list_entities.cpp.
@@ -44,12 +39,7 @@ class ListEntitiesIterator final : public ComponentIterator {
// Same type as message_generator_t, which this header cannot include
void send_all_(void *source, void (*generator)(WebServer *, void *, json::JsonBuilder &));
const WebServer *web_server_;
#ifdef USE_ESP32
esphome::web_server_idf::AsyncEventSource *events_;
#elif USE_ARDUINO
DeferredUpdateEventSource *events_;
#endif
EventSession *session_;
};
} // namespace esphome::web_server
@@ -5,7 +5,7 @@
#include "esphome/core/helpers.h"
namespace esphome::web_server_idf {
namespace esphome::web_server {
void for_each_chunk_piece(const char *message, size_t message_len, ChunkPieceSink sink, void *ctx) {
if (message == nullptr) {
@@ -63,4 +63,4 @@ void write_chunk_header(char *buf, size_t chunk_len) {
buf[9] = '\n';
}
} // namespace esphome::web_server_idf
} // namespace esphome::web_server
@@ -3,7 +3,9 @@
#include <cstddef>
#include <cstdint>
namespace esphome::web_server_idf {
#include "esphome/components/web_server_base/stream_head.h"
namespace esphome::web_server {
// Wire framing of one Server-Sent-Events event inside the chunked /events response:
// "%08x\r\n" + fields + "data: " line ["\r\ndata: " line]... "\r\n" + "\r\n" + "\r\n"
@@ -22,6 +24,17 @@ constexpr size_t SSE_SEP_LEN = sizeof(SSE_SEP) - 1;
constexpr char SSE_SUFFIX[] = "\r\n\r\n\r\n";
constexpr size_t SSE_SUFFIX_LEN = sizeof(SSE_SUFFIX) - 1;
// Status, Content-Type and headers of the /events response head
constexpr char SSE_STATUS[] = "200 OK";
constexpr char SSE_CONTENT_TYPE[] = "text/event-stream";
constexpr web_server_base::HttpHeader SSE_HEADERS[] = {
{"Transfer-Encoding", "chunked"},
{"Cache-Control", "no-cache"},
{"Connection", "keep-alive"},
};
// Hand-framed first chunk holding an empty event (one blank line), as httpd_resp_send_chunk() sent it before
constexpr char SSE_FIRST_CHUNK[] = "2\r\n\r\n\r\n";
// Receives one piece of the chunk; ctx is whatever the caller passed to for_each_chunk_piece()
using ChunkPieceSink = void (*)(void *ctx, const char *piece, size_t len);
@@ -37,4 +50,4 @@ size_t build_chunk_prefix(char *buf, size_t size, const char *event, uint32_t id
// Fills the header placeholder at the start of buf once the chunk length is known
void write_chunk_header(char *buf, size_t chunk_len);
} // namespace esphome::web_server_idf
} // namespace esphome::web_server
+1 -1
View File
@@ -273,7 +273,7 @@ void DeferredUpdateEventSource::process_deferred_queue_() {
deferred_queue_.erase(deferred_queue_.begin());
this->consecutive_send_failures_ = 0; // Reset failure count on successful send
} else {
// NOTE: Similar logic exists in web_server_idf/web_server_idf.cpp in AsyncEventSourceResponse::drain_tail_().
// NOTE: Similar logic exists in web_server_idf/web_server_idf.cpp in AsyncStreamResponse::write().
// The close mechanisms are platform-specific (this path calls close() directly; the IDF path is time-based and
// closes through HTTPD to preserve session ownership), but both drop a client after roughly 20 seconds without
// send progress. Keep that stall policy in sync when changing either side.
+6 -2
View File
@@ -5,7 +5,11 @@
#include "esphome/components/json/json_util.h"
#include "esphome/components/web_server_base/web_server_base.h"
#include "esphome/components/web_server_base/captive_dns.h"
#ifdef USE_WEBSERVER
// The host only builds sse_chunk.cpp for unit tests, but other tests can define USE_WEBSERVER there
#if defined(USE_WEBSERVER) && (defined(USE_ESP32) || defined(USE_ARDUINO))
#ifdef USE_ESP32
#include "event_source.h"
#endif
#include "esphome/core/component.h"
#include "esphome/core/entity_base.h"
#include "esphome/core/entity_includes.h"
@@ -158,7 +162,7 @@ class DeferredUpdateEventSource final : public AsyncEventSource {
public:
DeferredUpdateEventSource(WebServer *ws, const String &url)
: AsyncEventSource(url), entities_iterator_(ListEntitiesIterator(ws, this)), web_server_(ws) {}
: AsyncEventSource(url), entities_iterator_(ListEntitiesIterator(this)), web_server_(ws) {}
void loop();
@@ -105,9 +105,9 @@ async def to_code(config: ConfigType) -> None:
var = cg.new_Pvariable(config[CONF_ID])
cg.add(cg.RawExpression(f"{web_server_base_ns}::global_web_server_base = {var}"))
# Default headers added in init(): web_server_idf's StaticVector, or the library's DefaultHeaders
cg.add_define("WEB_SERVER_DEFAULT_HEADERS_COUNT", 1)
if CORE.is_esp32:
# Count for StaticVector in web_server_idf - matches headers added in init()
cg.add_define("WEB_SERVER_DEFAULT_HEADERS_COUNT", 1)
return
# ESP32 uses IDF web server (early return above), so this is for other Arduino platforms
@@ -0,0 +1,126 @@
#pragma once
#include <cstddef>
#include <cstdint>
#include <cstring>
#include <span>
#include <strings.h>
// Pure helpers of AsyncStreamResponse, kept free of httpd and lwIP so they build on the host.
// The iovec type is a template parameter: lwIP defines its own on the device, POSIX on the host.
namespace esphome::web_server_base {
struct HttpHeader {
const char *name;
const char *value;
};
/// Drops the first sent bytes from a gather list after a write; a short write resumes inside the
/// element it stopped in. count reaches 0 once everything went out.
template<typename Iovec> void advance_iovecs(Iovec *&iov, int &count, size_t sent) {
while (count > 0 && sent >= iov->iov_len) {
sent -= iov->iov_len;
++iov;
--count;
}
if (count > 0) {
iov->iov_base = static_cast<char *>(iov->iov_base) + sent;
iov->iov_len -= sent;
}
}
/// Pieces in a head: 4 for the status line and Content-Type, 4 per header, the blank line and the body
constexpr size_t stream_head_iov_count(size_t header_count) { return 4 + 4 * header_count + 2; }
/// Pieces per batch: a head with up to four headers goes out in one write; 192 bytes on 32-bit
static constexpr int STREAM_HEAD_MAX_IOV = 24;
/// True if headers holds a header named name, compared case insensitively
inline bool has_header(std::span<const HttpHeader> headers, const char *name) {
for (const auto &header : headers) {
if (strcasecmp(header.name, name) == 0) {
return true;
}
}
return false;
}
/// Gathers the head of a stream response: "HTTP/1.1 <status>", Content-Type, headers, the default_headers that
/// headers does not replace, the blank line and body (may be null). send(Iovec *iov, int count) gets each full batch of
/// STREAM_HEAD_MAX_IOV pieces and then the rest; once it returns false the later pieces are skipped. Returns false if
/// any send failed.
template<typename Iovec, typename Send>
bool write_stream_head(Send &&send, const char *status, const char *content_type, std::span<const HttpHeader> headers,
std::span<const HttpHeader> default_headers, const char *body) {
struct Batch {
Send &send;
int count{0};
bool ok{true};
Iovec iov[STREAM_HEAD_MAX_IOV]; // left uninitialized on purpose
void flush() {
if (this->ok && this->count > 0) {
this->ok = this->send(this->iov, this->count);
}
this->count = 0;
}
void add(const char *buf, size_t len) {
if (this->count == STREAM_HEAD_MAX_IOV) {
this->flush();
}
this->iov[this->count++] = {const_cast<char *>(buf), len};
}
void add(const char *str) { this->add(str, strlen(str)); }
void add_header(const HttpHeader &header) {
this->add("\r\n", 2);
this->add(header.name);
this->add(": ", 2);
this->add(header.value);
}
} batch{send};
batch.add("HTTP/1.1 ", 9);
batch.add(status);
batch.add("\r\nContent-Type: ", 16);
batch.add(content_type);
for (const auto &header : headers) {
batch.add_header(header);
}
for (const auto &header : default_headers) {
if (!has_header(headers, header.name)) {
batch.add_header(header);
}
}
batch.add("\r\n\r\n", 4);
if (body != nullptr) {
batch.add(body);
}
batch.flush();
return batch.ok;
}
/// How long sends have made no progress. Zero means not running, so a start at millis() == 0 records 1.
/// The comparison is wraparound safe across a millis() rollover.
class StallClock {
public:
/// Starts the clock; a running clock keeps its start time.
void start(uint32_t now) {
if (this->started_ms_ == 0) {
this->started_ms_ = now != 0 ? now : 1;
}
}
void clear() { this->started_ms_ = 0; }
bool is_running() const { return this->started_ms_ != 0; }
/// True once the clock has run for timeout_ms
bool is_expired(uint32_t now, uint32_t timeout_ms) const {
return this->started_ms_ != 0 && static_cast<int32_t>(now - (this->started_ms_ + timeout_ms)) >= 0;
}
uint32_t elapsed(uint32_t now) const { return now - this->started_ms_; }
protected:
uint32_t started_ms_{0};
};
} // namespace esphome::web_server_base
@@ -0,0 +1,16 @@
#pragma once
#include "esphome/core/defines.h"
// The long lived stream response the /events layer writes to, for the platform's web server
#ifdef USE_ESP32
#include "esphome/components/web_server_idf/web_server_idf.h"
#include <sys/socket.h>
namespace esphome::web_server_base {
using web_server_idf::AsyncStreamResponse;
// One entry of the gather list AsyncStreamResponse::write() takes
using StreamIovec = ::iovec;
} // namespace esphome::web_server_base
#endif
@@ -105,6 +105,9 @@ class WebServerBase final {
return;
}
this->server_ = new AsyncWebServer(this->port_);
#if defined(USE_ESP32) && defined(USE_WEBSERVER_SSL)
this->server_->set_ssl_credentials(this->ssl_certificate_, this->ssl_private_key_);
#endif
// All content is controlled and created by user - so allowing all origins is fine here.
// NOTE: Currently 1 header. If more are added, update in __init__.py:
// cg.add_define("WEB_SERVER_DEFAULT_HEADERS_COUNT", 1)
@@ -158,7 +161,21 @@ class WebServerBase final {
void set_port(uint16_t port) { port_ = port; }
uint16_t get_port() const { return port_; }
#if defined(USE_ESP32) && defined(USE_WEBSERVER_SSL)
/// PEM certificate and key for HTTPS. Not copied: they must stay valid. Set them, like the port, before init().
void set_ssl_credentials(const char *certificate, const char *private_key) {
this->ssl_certificate_ = certificate;
this->ssl_private_key_ = private_key;
if (this->server_ != nullptr)
this->server_->set_ssl_credentials(certificate, private_key); // used at the next init()
}
#endif
protected:
#if defined(USE_ESP32) && defined(USE_WEBSERVER_SSL)
const char *ssl_certificate_{nullptr};
const char *ssl_private_key_{nullptr};
#endif
uint8_t initialized_{0};
uint16_t port_{80}; // Keep in sync with DEFAULT_PORT in web_server/__init__.py
AsyncWebServer *server_{nullptr};
@@ -1,14 +1,19 @@
from dataclasses import dataclass, field
import logging
import esphome.codegen as cg
from esphome.components import socket, web_server_base
from esphome.components.esp32 import (
get_user_lwip_max_sockets,
idf_version,
include_builtin_idf_component,
request_tls,
require_mbedtls_tls_server,
set_idf_sdkconfig_default,
)
from esphome.config_helpers import filter_source_files_from_defines
import esphome.config_validation as cv
from esphome.core import CORE
from esphome.types import ConfigType
_LOGGER = logging.getLogger(__name__)
@@ -23,6 +28,46 @@ DEFAULT_HTTPD_MAX_OPEN_SOCKETS = 7
HTTPD_CONTROL_UDP_SOCKETS = 2
# ESP-IDF's HTTPS server allows this many open connections; each holds a TLS session.
HTTPS_MAX_CONNECTIONS = 4
@dataclass
class WebServerIdfData:
ssl: bool = False # a component serves HTTPS (consume_ssl_sockets() was called)
# Consumers that need plain HTTP (require_plain_http() was called)
plain_http: list[str] = field(default_factory=list)
def _get_data() -> WebServerIdfData:
if DOMAIN not in CORE.data:
CORE.data[DOMAIN] = WebServerIdfData()
return CORE.data[DOMAIN]
def consume_ssl_sockets(config: ConfigType) -> ConfigType:
"""Serve the shared web server over HTTPS.
The one thing a component that supplies its own certificate adds to its
schema; it then calls WebServerBase::set_ssl_credentials() before init().
This compiles HTTPS in and registers the connections it can hold open.
"""
data = _get_data()
# HTTPS exists only on ESP32. The listener is shared, so its clients are registered once.
if CORE.is_esp32 and not data.ssl:
web_server_base.add_httpd_clients(HTTPS_MAX_CONNECTIONS, DOMAIN)
data.ssl = True
return config
def require_plain_http(consumer: str) -> None:
"""Record that consumer needs the shared web server to speak plain HTTP.
Call during config validation; HTTPS is then rejected in final validation.
"""
_get_data().plain_http.append(consumer)
CONFIG_SCHEMA = cv.All(
cv.Schema({}),
cv.only_on_esp32,
@@ -37,6 +82,9 @@ def _httpd_max_open_sockets() -> int:
max_open = max(
DEFAULT_HTTPD_MAX_OPEN_SOCKETS, web_server_base.get_httpd_client_count()
)
# Each TLS session takes memory, so HTTPS keeps ESP-IDF's HTTPS connection limit.
if _get_data().ssl:
max_open = min(max_open, HTTPS_MAX_CONNECTIONS)
# Without a user override esp32 sizes CONFIG_LWIP_MAX_SOCKETS from the socket
# registry, which already holds the clients and the internal sockets.
if (user_max := get_user_lwip_max_sockets()) is None:
@@ -55,9 +103,32 @@ def _httpd_max_open_sockets() -> int:
return max_open
def _final_validate(config: ConfigType) -> None:
# Runs after every CONFIG_SCHEMA, so all require_plain_http() calls are in.
data = _get_data()
if not data.ssl:
return
# tls_handshake_timeout_ms, which keeps a stalled client from holding the server task, is new in 5.5.
if idf_version() < cv.Version(5, 5, 0):
raise cv.Invalid("HTTPS needs ESP-IDF 5.5 or later")
if data.plain_http:
names = ", ".join(data.plain_http)
raise cv.Invalid(f"HTTPS can't be used together with {names} yet")
FINAL_VALIDATE_SCHEMA = _final_validate
async def to_code(config: ConfigType) -> None:
# Runs after config validation, so every add_httpd_clients() call is counted.
cg.add_define("WEB_SERVER_MAX_OPEN_SOCKETS", _httpd_max_open_sockets())
if _get_data().ssl:
cg.add_define("USE_WEBSERVER_SSL")
request_tls()
# ESPHome builds mbedTLS client-only unless a component accepts TLS sessions.
require_mbedtls_tls_server()
include_builtin_idf_component("esp_https_server")
set_idf_sdkconfig_default("CONFIG_ESP_HTTPS_SERVER_ENABLE", True)
# Increase the maximum supported size of headers section in HTTP request packet to be processed by the server
set_idf_sdkconfig_default("CONFIG_HTTPD_MAX_REQ_HDR_LEN", 1024)
# Re-enable ESP-IDF's HTTP server (excluded by default to save compile time).
@@ -17,9 +17,16 @@
#include <freertos/task.h>
#include "utils.h"
#include "sse_chunk.h"
#include "web_server_idf.h"
#ifdef USE_WEBSERVER_SSL
#include <esp_https_server.h>
#include <freertos/semphr.h>
#include <esp_idf_version.h>
#include <mbedtls/pk.h>
#include <mbedtls/x509_crt.h>
#endif
#ifdef USE_WEBSERVER_AUTH_DIGEST
#include <esp_random.h>
#include <esp_rom_md5.h>
@@ -30,17 +37,15 @@
#include "multipart.h" // For parse_multipart_boundary and other utils
#endif
#ifdef USE_WEBSERVER
#include "esphome/components/web_server/web_server.h"
#include "esphome/components/web_server/list_entities.h"
#endif // USE_WEBSERVER
// Include socket headers after Arduino headers to avoid IPADDR_NONE/INADDR_NONE macro conflicts
#include <cerrno>
#include <sys/socket.h>
namespace esphome::web_server_idf {
using web_server_base::advance_iovecs;
using web_server_base::write_stream_head;
// Status strings not provided by esp_http_server.h
#ifndef HTTPD_401
#define HTTPD_401 "401 Unauthorized"
@@ -55,13 +60,10 @@ namespace esphome::web_server_idf {
#define HTTPD_422 "422 Unprocessable Entity"
#endif
#define CRLF_STR "\r\n"
#define CRLF_LEN (sizeof(CRLF_STR) - 1)
ESPHOME_LOG_TAG(TAG, "web_server_idf");
// Only prepare_json_() may hold the JSON arena: every other frame in this file is capped below one
// arena. Measured at -Os on GCC 14; newer toolchains stay checked on purpose, older ones skip it.
// Every frame in this file is capped below one JSON arena.
// Measured at -Os on GCC 14; newer toolchains stay checked on purpose, older ones skip it.
#if defined(__GNUC__) && !defined(__clang__) && __GNUC__ >= 14 && defined(__OPTIMIZE_SIZE__)
#pragma GCC diagnostic error "-Wstack-usage=2048"
#endif
@@ -140,6 +142,12 @@ void AsyncWebServer::safe_close_with_shutdown(httpd_handle_t hd, int sockfd) {
//
// See: https://github.com/esphome/esphome-webserver/issues/163
AsyncStreamResponse::socket_closing(hd, sockfd);
#ifdef USE_WEBSERVER_SSL
// Keeps shutdown_clients_() from acting on a descriptor freed, and its number reused, while it walks the list.
LockGuard lock(static_cast<AsyncWebServer *>(httpd_get_global_user_ctx(hd))->close_lock_);
#endif
// Attempt shutdown - ignore errors as socket may already be closed
shutdown(sockfd, SHUT_RD);
@@ -147,16 +155,144 @@ void AsyncWebServer::safe_close_with_shutdown(httpd_handle_t hd, int sockfd) {
close(sockfd);
}
#ifdef USE_WEBSERVER_SSL
namespace {
// Local port of a socket, or 0 if the descriptor is not an open socket.
uint16_t local_port(int fd) {
struct sockaddr_storage addr{};
socklen_t len = sizeof(addr);
if (getsockname(fd, reinterpret_cast<struct sockaddr *>(&addr), &len) != 0) {
return 0;
}
if (addr.ss_family == AF_INET) {
return ntohs(reinterpret_cast<struct sockaddr_in *>(&addr)->sin_port);
}
#if LWIP_IPV6
if (addr.ss_family == AF_INET6) {
return ntohs(reinterpret_cast<struct sockaddr_in6 *>(&addr)->sin6_port);
}
#endif
return 0;
}
// Stops the server for AsyncWebServer::end() on its own task and signals the caller when done.
// A semaphore, not a task notification: the main loop task already uses notifications to wake itself.
struct StopRequest {
httpd_handle_t server;
bool ssl;
esp_err_t result;
StaticSemaphore_t done_buffer;
SemaphoreHandle_t done;
};
// httpd_ssl_stop() also posts HTTPS_SERVER_EVENT_STOP, to match the start event httpd_ssl_start() posted.
esp_err_t stop_server(httpd_handle_t server, bool ssl) { return ssl ? httpd_ssl_stop(server) : httpd_stop(server); }
void stop_task(void *arg) {
auto *request = static_cast<StopRequest *>(arg);
request->result = stop_server(request->server, request->ssl);
xSemaphoreGive(request->done); // the request is not touched after this
vTaskDelete(nullptr);
}
} // namespace
void AsyncWebServer::context_free(void *ctx) {
// Waits for a running shutdown_clients_() pass, and makes later passes skip the freed server.
auto *server = static_cast<AsyncWebServer *>(ctx);
LockGuard lock(server->close_lock_);
server->running_port_ = 0;
}
void AsyncWebServer::shutdown_clients_() {
// Wakes the server task if it is blocked on a client. A just-closed client may still be listed with its number
// reused elsewhere, so only descriptors still bound to this server's port are shut down.
LockGuard lock(this->close_lock_);
if (this->running_port_ == 0) {
return; // the server is being freed (context_free() ran), so its client list is gone
}
int client_fds[CONFIG_LWIP_MAX_SOCKETS];
size_t client_count = CONFIG_LWIP_MAX_SOCKETS;
if (httpd_get_client_list(this->server_, &client_count, client_fds) != ESP_OK) {
return;
}
for (size_t i = 0; i < client_count; i++) {
if (local_port(client_fds[i]) == this->running_port_) {
shutdown(client_fds[i], SHUT_RDWR);
}
}
}
namespace {
#if ESP_IDF_VERSION < ESP_IDF_VERSION_VAL(6, 0, 0)
int ssl_random(void * /*unused*/, unsigned char *buf, size_t len) {
random_bytes(buf, len);
return 0;
}
#endif
// esp_https_server parses the credentials only when a client connects, so check the pair before starting.
// Lengths include the terminating null, as mbedTLS wants for PEM.
bool ssl_credentials_valid(const uint8_t *cert_pem, size_t cert_len, const uint8_t *key_pem, size_t key_len) {
mbedtls_x509_crt crt;
mbedtls_pk_context key;
mbedtls_x509_crt_init(&crt);
mbedtls_pk_init(&key);
bool valid = mbedtls_x509_crt_parse(&crt, cert_pem, cert_len) == 0;
#if ESP_IDF_VERSION >= ESP_IDF_VERSION_VAL(6, 0, 0)
// mbedTLS 4 (ESP-IDF 6) takes its randomness from PSA Crypto instead of an RNG argument.
valid = valid && mbedtls_pk_parse_key(&key, key_pem, key_len, nullptr, 0) == 0 &&
mbedtls_pk_check_pair(&crt.pk, &key) == 0;
#else
valid = valid && mbedtls_pk_parse_key(&key, key_pem, key_len, nullptr, 0, ssl_random, nullptr) == 0 &&
mbedtls_pk_check_pair(&crt.pk, &key, ssl_random, nullptr) == 0;
#endif
mbedtls_pk_free(&key);
mbedtls_x509_crt_free(&crt);
return valid;
}
} // namespace
#endif
void AsyncWebServer::end() {
if (this->server_) {
#ifdef USE_WEBSERVER_SSL
// A stalled TLS handshake or upload can hold the server task until the socket timeout. Stop on a helper task and
// keep shutting the clients down until it is done: a client accepted before the stop message arrives is listed
// during its handshake, so a later pass reaches it. Without memory for the task, stop directly.
StopRequest request{};
request.server = this->server_;
request.ssl = httpd_get_global_transport_ctx(this->server_) != nullptr; // only HTTPS has one
request.done = xSemaphoreCreateBinaryStatic(&request.done_buffer);
if (xTaskCreate(stop_task, "httpd_stop", 3072, &request, uxTaskPriorityGet(nullptr), nullptr) == pdPASS) {
do {
this->shutdown_clients_();
App.feed_wdt();
} while (xSemaphoreTake(request.done, pdMS_TO_TICKS(100)) != pdTRUE);
} else {
this->shutdown_clients_();
request.result = stop_server(this->server_, request.ssl);
}
const esp_err_t err = request.result;
if (err != ESP_OK) {
// The listener is still running; keep its handle so begin() doesn't start a second one beside it.
ESP_LOGE(TAG, "Web server failed to stop: %s", esp_err_to_name(err));
return;
}
#else
httpd_stop(this->server_);
this->server_ = nullptr;
#endif
this->server_ = nullptr; // stopping also frees an HTTPS server's TLS context
}
}
void AsyncWebServer::begin() {
if (this->server_) {
this->end();
#ifdef USE_WEBSERVER_SSL
if (this->server_) {
return; // the old listener failed to stop
}
#endif
}
// Allow room for SerializationBuffer's 640-byte stack buffer and HTTP request handling.
httpd_config_t config = HTTPD_DEFAULT_CONFIG();
@@ -171,7 +307,50 @@ void AsyncWebServer::begin() {
config.lru_purge_enable = true;
// Use custom close function that shuts down before closing to prevent lwIP race conditions
config.close_fn = AsyncWebServer::safe_close_with_shutdown;
if (httpd_start(&this->server_, &config) == ESP_OK) {
esp_err_t start_err;
#ifdef USE_WEBSERVER_SSL
// Lets close_fn find this server's lock. httpd calls the free function on stop just before freeing the server.
config.global_user_ctx = this;
config.global_user_ctx_free_fn = AsyncWebServer::context_free;
if ((this->ssl_certificate_ == nullptr) != (this->ssl_private_key_ == nullptr)) {
ESP_LOGE(TAG, "HTTPS needs both a certificate and a private key");
return;
}
if (this->ssl_certificate_ != nullptr) {
const auto *cert = reinterpret_cast<const uint8_t *>(this->ssl_certificate_);
const auto *key = reinterpret_cast<const uint8_t *>(this->ssl_private_key_);
const size_t cert_len = strlen(this->ssl_certificate_) + 1;
const size_t key_len = strlen(this->ssl_private_key_) + 1;
if (!ssl_credentials_valid(cert, cert_len, key, key_len)) {
ESP_LOGE(TAG, "HTTPS certificate or key is invalid, or the key does not match the certificate");
return;
}
httpd_ssl_config_t ssl_config = HTTPD_SSL_CONFIG_DEFAULT();
// The TLS handshake needs the larger stack of the HTTPS defaults.
config.stack_size = std::max(config.stack_size, ssl_config.httpd.stack_size);
ssl_config.httpd = config;
ssl_config.port_secure = this->port_;
// A client that connects and never completes the handshake holds the server task. The check runs after each
// 5 s receive timeout, so this ends it at the first one instead of after three (the 10 s default).
ssl_config.tls_handshake_timeout_ms = 4000;
ssl_config.servercert = cert;
ssl_config.servercert_len = cert_len;
ssl_config.prvtkey_pem = key;
ssl_config.prvtkey_len = key_len;
start_err = httpd_ssl_start(&this->server_, &ssl_config);
if (start_err != ESP_OK) {
ESP_LOGE(TAG, "HTTPS server failed to start: %s", esp_err_to_name(start_err));
}
} else {
start_err = httpd_start(&this->server_, &config);
}
#else
start_err = httpd_start(&this->server_, &config);
#endif
if (start_err == ESP_OK) {
#ifdef USE_WEBSERVER_SSL
this->running_port_ = this->port_;
#endif
const httpd_uri_t handler_get = {
.uri = "",
.method = HTTP_GET,
@@ -736,225 +915,93 @@ 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);
req->sess_ctx = this;
req->free_ctx = AsyncEventSourceResponse::destroy;
this->hd_ = req->handle;
this->fd_.store(httpd_req_to_sockfd(req));
// 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);
}
// NOLINTBEGIN(clang-analyzer-cplusplus.NewDeleteLeaks) false positive with ArduinoJson
void AsyncEventSourceResponse::start_session_main_loop_() {
auto *ws = this->web_server_;
// The tcp send buffer is empty on connect. A refusal is a closing session or a failed tail
// allocation; nothing retries the greeting, so close and let the client reconnect.
auto message = ws->get_config_json();
if (!this->try_send_nodefer(message.c_str(), message.size(), "ping", millis(), 30000)) {
ESP_LOGW(TAG, "Config not sent to fd %d", this->fd_.load());
this->request_close_();
#ifdef USE_WEBSERVER_SSL
// Streams send straight to the socket, which would bypass TLS; only an HTTPS server has a global transport context.
// Refused before anything is sent, so the stream stays unattached and safe to delete.
if (httpd_get_global_transport_ctx(req->handle) != nullptr) {
httpd_resp_send_err(req, HTTPD_501_METHOD_NOT_IMPLEMENTED, "Streams are not supported over HTTPS");
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
// 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);
this->entities_iterator_.begin(ws->include_internal_);
req->sess_ctx = this;
req->free_ctx = AsyncStreamResponse::destroy;
this->hd_ = req->handle;
this->fd_.store(fd);
// Use non-blocking send to prevent watchdog timeouts when TCP buffers are full
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);
}
}
// NOLINTEND(clang-analyzer-cplusplus.NewDeleteLeaks)
void AsyncEventSourceResponse::destroy(void *ptr) {
auto *rsp = static_cast<AsyncEventSourceResponse *>(ptr);
// 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)
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;
LockGuard guard{stream_send_mutex};
rsp->socket_closed_ = true;
}
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_() {
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;
void AsyncStreamResponse::close() {
if (!this->loop_flags_.close_requested) {
this->loop_flags_.close_requested = true;
this->next_close_attempt_ms_ = App.get_loop_component_start_time();
}
this->process_close_();
}
void AsyncEventSourceResponse::process_close_() {
if (!this->close_requested_ || this->close_work_queued_.load(std::memory_order_acquire)) {
void AsyncStreamResponse::loop() {
if (this->loop_flags_.close_requested) {
this->process_close_();
} else {
this->refresh_lru_();
}
}
void AsyncStreamResponse::process_close_() {
if (!this->loop_flags_.close_requested || this->close_work_queued_.load(std::memory_order_acquire)) {
return;
}
const int fd = this->fd_.load();
@@ -971,25 +1018,25 @@ 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;
}
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));
this->close_retry_warning_logged_ = true;
if (!this->loop_flags_.close_retry_warning_logged) {
ESP_LOGW(TAG, "Failed to queue stream close (%s); retrying", esp_err_to_name(err));
this->loop_flags_.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 +1045,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 +1060,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 +1073,64 @@ 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->loop_flags_.close_requested) {
return -1;
}
// Nothing to send is not a stall
size_t total = 0;
for (int i = 0; i < iovcnt; i++) {
total += iov[i].iov_len;
}
if (total == 0) {
return 0;
}
// httpd closes the socket before it clears the session, so the fd number may already be a new client's.
// Treated as would-block; the stall timer ends a session that never becomes ours again.
const int fd = this->fd_.load();
if (!this->owns_session_(fd)) {
return 0;
// httpd freed the session, so the stream can never send again
if (fd == 0) {
this->close();
return -1;
}
struct msghdr msg{};
msg.msg_iov = iov;
msg.msg_iovlen = iovcnt;
const ssize_t sent = sendmsg(fd, &msg, MSG_DONTWAIT);
if (sent >= 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;
}
}
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);
this->close();
return -1;
}
ESP_LOGD(TAG, "send error: errno %d", err);
this->request_close_();
return -1;
}
void AsyncEventSourceResponse::drain_tail_() {
if (this->sending_ || this->close_requested_ || this->tail_len_ == 0) {
return;
}
SendGuard guard{*this};
const size_t remaining = this->tail_len_ - this->tail_sent_;
struct iovec iov = {this->tail_.get() + this->tail_sent_, remaining};
const ssize_t sent = this->send_(&iov, 1);
if (sent < 0) {
return;
}
if (sent == 0) {
// Socket buffer full, try again later
// NOTE: Similar logic exists in web_server/web_server.cpp in DeferredUpdateEventSource::process_deferred_queue_().
// The IDF path is intentionally time-based and closes through HTTPD to preserve session ownership.
const uint32_t now = App.get_loop_component_start_time();
if (this->send_failure_started_ms_ == 0) {
this->send_failure_started_ms_ = now != 0 ? now : 1; // Reserve zero for no stall.
}
if (static_cast<int32_t>(now - (this->send_failure_started_ms_ + SEND_STALL_TIMEOUT_MS)) >= 0) {
ESP_LOGW(TAG, "Closing stuck EventSource connection after %" PRIu32 " ms without send progress",
now - this->send_failure_started_ms_);
this->request_close_();
}
return;
}
this->send_failure_started_ms_ = 0;
this->tail_sent_ += sent;
if (this->tail_sent_ < this->tail_len_) {
ESP_LOGV(TAG, "Partial send: %zd/%zu bytes (total: %u/%u)", sent, remaining, this->tail_sent_, this->tail_len_);
return;
}
// Fully sent; the storage stays for the next stall
this->tail_len_ = 0;
}
bool AsyncEventSourceResponse::reserve_tail_(size_t len) {
if (this->tail_cap_ >= len) {
return true;
}
if (len > TAIL_MAX_SIZE) {
return false;
}
// Nothing is pending while the tail grows, so free the old block first. PREFER_INTERNAL keeps
// the tail where plain new put it.
this->tail_.reset();
this->tail_ = RAMAllocator<uint8_t>(RAMAllocator<uint8_t>::PREFER_INTERNAL).make_unique_array_for_overwrite(len);
this->tail_cap_ = this->tail_ ? len : 0;
return this->tail_cap_ != 0;
}
bool AsyncEventSourceResponse::stash_chunk_(const char *prefix, size_t prefix_len, const char *message,
size_t message_len, size_t total, size_t sent) {
// A log event of nothing but line breaks is the largest chunk; reserve_tail_() must never
// refuse it for size, only for memory
static_assert(SSE_SEP_LEN * web_server::LOG_EVENT_MAX_LEN + PREFIX_BUF_SIZE + SSE_SUFFIX_LEN <= TAIL_MAX_SIZE,
"the log cut in web_server.h must keep a worst case log event inside the tail ceiling");
if (!this->reserve_tail_(total)) {
if (sent != 0) {
// Part of the chunk is on the wire, so the stream is broken and the client has to go
ESP_LOGW(TAG, "Cannot buffer a %zu byte chunk, closing", total);
this->request_close_();
} else {
this->tail_alloc_failed_(total); // nothing on the wire, the caller retries on the stall clock
}
return false;
}
uint8_t *dst = this->tail_.get();
std::memcpy(dst, prefix, prefix_len);
dst += prefix_len;
for_each_chunk_piece(
message, message_len,
[](void *ctx, const char *piece, size_t len) {
auto &out = *static_cast<uint8_t **>(ctx);
std::memcpy(out, piece, len);
out += len;
},
&dst);
this->tail_len_ = total;
this->tail_sent_ = sent;
return true;
}
void AsyncEventSourceResponse::loop() {
if (this->close_requested_) {
this->process_close_();
return;
}
this->refresh_lru_();
drain_tail_();
process_deferred_queue_();
if (this->close_requested_)
return;
// One step per loop; refusals retry next pass
this->entities_iterator_.try_advance(1);
}
#if defined(__GNUC__) && !defined(__clang__) && __GNUC__ >= 14 && defined(__OPTIMIZE_SIZE__)
#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wstack-usage=" // the one frame that holds the JSON arena
#endif
AsyncEventSourceResponse::JsonPrepareResult AsyncEventSourceResponse::prepare_json_(void *source,
message_generator_t *generator,
char *buffer, size_t *size_out) {
// Log lines emitted while the generator runs are dropped for this session: on the large path they would fill
// tail_ before it is overwritten, and on the small path they would go out ahead of the event being built.
SendGuard guard{*this};
json::JsonArena<json::JSON_ARENA_SIZE> arena;
json::JsonBuilder builder(&arena);
generator(this->web_server_, source, builder);
*size_out = builder.serialize_to(buffer, JSON_BUF_SIZE);
if (*size_out < JSON_BUF_SIZE) {
return JsonPrepareResult::JSON_PREPARE_RESULT_BUFFER;
}
char prefix[PREFIX_BUF_SIZE];
const size_t prefix_len = build_chunk_prefix(prefix, sizeof(prefix), "state", 0, 0, true);
// Nothing reaches the wire until the complete document and its framing are owned by the caller.
size_t json_len = 0;
size_t cap = std::max<size_t>(JSON_BUF_SIZE * 2, this->tail_cap_);
for (;;) {
if (!this->reserve_tail_(cap)) {
*size_out = cap;
return JsonPrepareResult::JSON_PREPARE_RESULT_NO_MEMORY;
}
const size_t room = cap - prefix_len - SSE_SUFFIX_LEN;
json_len = builder.serialize_to(reinterpret_cast<char *>(this->tail_.get()) + prefix_len, room);
if (json_len < room) {
break;
}
if (cap >= TAIL_MAX_SIZE) {
*size_out = room;
return JsonPrepareResult::JSON_PREPARE_RESULT_TOO_LARGE;
}
cap = std::min<size_t>(cap * 2, TAIL_MAX_SIZE);
}
const size_t total = prefix_len + json_len + SSE_SUFFIX_LEN;
write_chunk_header(prefix, total - CHUNK_HDR_LEN - CHUNK_END_LEN);
uint8_t *dst = this->tail_.get();
std::memcpy(dst, prefix, prefix_len);
std::memcpy(dst + prefix_len + json_len, SSE_SUFFIX, SSE_SUFFIX_LEN);
this->tail_len_ = total;
this->tail_sent_ = 0;
return JsonPrepareResult::JSON_PREPARE_RESULT_TAIL;
}
#if defined(__GNUC__) && !defined(__clang__) && __GNUC__ >= 14 && defined(__OPTIMIZE_SIZE__)
#pragma GCC diagnostic pop
#endif
bool AsyncEventSourceResponse::send_json_(void *source, message_generator_t *generator) {
if (!this->ready_to_send_()) {
return false;
}
char buffer[JSON_BUF_SIZE];
size_t size;
const auto result = this->prepare_json_(source, generator, buffer, &size);
switch (result) {
case JsonPrepareResult::JSON_PREPARE_RESULT_BUFFER:
return this->try_send_nodefer(buffer, size, "state");
case JsonPrepareResult::JSON_PREPARE_RESULT_TAIL:
this->drain_tail_();
return true;
case JsonPrepareResult::JSON_PREPARE_RESULT_NO_MEMORY: {
SendGuard guard{*this};
this->tail_alloc_failed_(size);
return false;
}
case JsonPrepareResult::JSON_PREPARE_RESULT_TOO_LARGE: {
SendGuard guard{*this};
ESP_LOGW(TAG, "State event does not fit %zu bytes, dropped", size);
this->tail_.reset();
this->tail_cap_ = 0;
this->send_failure_started_ms_ = 0;
return true;
}
}
return false;
}
void AsyncEventSourceResponse::tail_alloc_failed_(size_t cap) {
// Same stall clock as a socket that stops draining, so a session cannot retry forever
// Socket buffer full, try again later; a socket that stays full past the stall timeout is closed
const uint32_t now = App.get_loop_component_start_time();
if (this->send_failure_started_ms_ == 0) {
this->send_failure_started_ms_ = now != 0 ? now : 1; // Reserve zero for no stall.
ESP_LOGW(TAG, "No memory for a %zu byte chunk", cap);
return;
}
if (static_cast<int32_t>(now - (this->send_failure_started_ms_ + SEND_STALL_TIMEOUT_MS)) >= 0) {
ESP_LOGW(TAG, "Closing EventSource after %" PRIu32 " ms without memory", now - this->send_failure_started_ms_);
this->request_close_();
this->stall_.start(now);
if (this->stall_.is_expired(now, SEND_STALL_TIMEOUT_MS)) {
ESP_LOGW(TAG, "Closing stuck stream after %" PRIu32 " ms without send progress", this->stall_.elapsed(now));
this->close();
return -1;
}
return 0;
}
bool AsyncEventSourceResponse::ready_to_send_() {
if (this->sending_ || this->fd_.load() == 0 || this->close_requested_) {
return false;
}
drain_tail_();
return !this->close_requested_ && this->tail_len_ == 0;
ssize_t AsyncStreamResponse::write(const void *data, size_t len) {
struct iovec iov = {const_cast<void *>(data), len};
return this->write(&iov, 1);
}
bool AsyncEventSourceResponse::try_send_nodefer(const char *message, size_t message_len, const char *event, uint32_t id,
uint32_t reconnect) {
if (!this->ready_to_send_()) {
return false;
}
SendGuard guard{*this};
// Everything after the prefix goes out straight from the caller's buffer
char prefix[PREFIX_BUF_SIZE];
const size_t prefix_len = build_chunk_prefix(prefix, sizeof(prefix), event, id, reconnect, message != nullptr);
if (message == nullptr && prefix_len == CHUNK_HDR_LEN) {
return true; // Match ESPAsyncWebServer: nothing to send
}
if (prefix_len >= PREFIX_BUF_SIZE - 1) {
// The appenders truncate silently, which would put a malformed event on the wire
ESP_LOGW(TAG, "Event name too long, dropped");
return true;
}
// Gather list: the prefix, then the data lines and their separators from the message
struct Gather {
struct iovec iov[MAX_SEND_IOV]; // left uninitialized on purpose
int iovcnt{1};
size_t total{0};
bool fits{true};
} g;
g.total = prefix_len;
for_each_chunk_piece(
message, message_len,
[](void *ctx, const char *piece, size_t len) {
auto &g = *static_cast<Gather *>(ctx);
g.total += len;
if (len == 0) {
return;
}
if (g.iovcnt == MAX_SEND_IOV) {
g.fits = false;
return;
}
g.iov[g.iovcnt++] = {const_cast<char *>(piece), len};
},
&g);
// The header and the terminator are not part of the chunk length
write_chunk_header(prefix, g.total - CHUNK_HDR_LEN - CHUNK_END_LEN);
g.iov[0] = {prefix, prefix_len};
// A message with more lines than the list holds skips straight to the tail
const ssize_t sent = g.fits ? this->send_(g.iov, g.iovcnt) : 0;
if (sent < 0) {
return false;
}
if (static_cast<size_t>(sent) == g.total) {
this->send_failure_started_ms_ = 0; // progress, whichever stall clock was running
return true;
}
// The caller's buffers do not outlive this call, so keep the chunk and continue from loop()
return this->stash_chunk_(prefix, prefix_len, message, message_len, g.total, sent);
}
void AsyncEventSourceResponse::deferrable_send_state(void *source, bool detail_all,
message_generator_t *message_generator) {
// Let the detail_all listing finish before bare state events, so the web UI never shows unnamed entries and
// initial connect sends less
if (!detail_all && !this->entities_iterator_.completed())
return;
drain_tail_();
process_deferred_queue_();
if (this->close_requested_) {
return;
}
if (this->tail_len_ != 0 || !deferred_queue_.empty()) {
// outgoing event buffer or deferred queue still not empty which means downstream tcp send buffer full, no point
// trying to send first
deq_push_back_with_dedup_(source, message_generator);
} else {
// A send error closes the session and clears the queue; nothing is queued after that
if (!this->send_json_(source, message_generator) && !this->close_requested_) {
deq_push_back_with_dedup_(source, message_generator);
}
}
}
#endif
#ifdef USE_WEBSERVER_OTA
esp_err_t AsyncWebServer::handle_multipart_upload_(httpd_req_t *r, const char *content_type) {
// Parse boundary and create reader
@@ -15,21 +15,14 @@
#include <utility>
#include <vector>
#ifdef USE_WEBSERVER
#include "esphome/components/json/json_util.h"
#include "esphome/components/web_server/list_entities.h"
#include "sse_chunk.h"
#endif
#include "esphome/components/web_server_base/stream_head.h"
struct iovec; // NOLINT(readability-identifier-naming) - forward decl of lwip's gather list entry
namespace esphome {
#ifdef USE_WEBSERVER
namespace web_server {
class WebServer;
}; // namespace web_server
#endif
namespace web_server_idf {
namespace esphome::web_server_idf {
using web_server_base::HttpHeader;
using web_server_base::StallClock;
class AsyncWebParameter {
public:
@@ -191,6 +184,7 @@ class AsyncWebServerRequest {
void onDisconnect(std::function<void()> fn) { this->on_disconnect_ = std::move(fn); }
#endif
/// sess_ctx is reserved for AsyncStreamResponse: the server's close_fn treats any session context as one.
operator httpd_req_t *() const { return this->req_; }
optional<std::string> get_header(const char *name) const;
/// Copy a header value into buf without heap allocation.
@@ -229,6 +223,13 @@ class AsyncWebServer {
void begin();
void end();
#ifdef USE_WEBSERVER_SSL
/// PEM certificate and key, not copied; nullptr for plain HTTP. Takes effect at the next begin().
void set_ssl_credentials(const char *certificate, const char *private_key) {
this->ssl_certificate_ = certificate;
this->ssl_private_key_ = private_key;
}
#endif
// NOLINTNEXTLINE(readability-identifier-naming)
AsyncWebHandler &addHandler(AsyncWebHandler *handler) {
@@ -240,7 +241,17 @@ class AsyncWebServer {
protected:
uint16_t port_{};
#ifdef USE_WEBSERVER_SSL
uint16_t running_port_{}; // port of the running listener, 0 once httpd frees it
#endif
httpd_handle_t server_{};
#ifdef USE_WEBSERVER_SSL
const char *ssl_certificate_{nullptr};
const char *ssl_private_key_{nullptr};
Mutex close_lock_; // held by close_fn while closing, by shutdown_clients_() and by context_free()
void shutdown_clients_();
static void context_free(void *ctx);
#endif
static esp_err_t request_handler(httpd_req_t *r);
static esp_err_t request_post_handler(httpd_req_t *r);
esp_err_t request_handler_(AsyncWebServerRequest *request) const;
@@ -269,204 +280,88 @@ 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. On an HTTPS server
/// it answers 501 instead and the stream never attaches.
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->loop_flags_.close_requested; }
/// The socket fd, for log lines only
int log_id() const { return this->fd_.load(); }
/// The session is gone and no queued httpd work still references this object.
bool safe_to_delete() const {
return this->fd_.load() == 0 && !this->close_work_queued_.load(std::memory_order_acquire) &&
!this->lru_work_queued_.load(std::memory_order_acquire);
}
static constexpr uint32_t SEND_STALL_TIMEOUT_MS = 20000;
protected:
void process_close_();
static void close_session_work(void *arg);
void refresh_lru_();
static void refresh_lru_work(void *arg);
// httpd closes the socket before it clears the session, so this can still match a closed fd; write() also checks
// socket_closed_
bool owns_session_(int fd) const { return fd != 0 && httpd_sess_get_ctx(this->hd_, fd) == this; }
static void destroy(void *p);
AsyncEventSource *server_;
httpd_handle_t hd_{};
std::atomic<int> fd_{};
std::vector<DeferredEvent> deferred_queue_;
esphome::web_server::WebServer *web_server_;
esphome::web_server::ListEntitiesIterator entities_iterator_;
// One chunk the socket did not take whole, allocated on the first stall; the only heap use
// on the send path
RAMUniquePtr<uint8_t[]> tail_;
uint32_t send_failure_started_ms_{0}; // Zero means no send stall in progress.
// Runs while sends make no progress; a running clock keeps its start time
StallClock stall_;
uint32_t next_close_attempt_ms_{0};
uint32_t last_lru_refresh_ms_{0};
uint16_t tail_cap_{0};
uint16_t tail_len_{0}; // Zero means nothing pending
uint16_t tail_sent_{0};
// Set on the main loop before queueing close work, cleared by the HTTPD-task callback when done.
std::atomic<bool> close_work_queued_{false};
std::atomic<bool> lru_work_queued_{false};
// Guarded by the stream send mutex
bool socket_closed_{false};
// Main-loop only; the HTTPD task never reads or writes these flags.
bool close_requested_{false};
bool close_retry_warning_logged_{false};
bool sending_{false};
// The longest multi line log message in the tree (a climate dump_config) has 22 lines; a
// longer one goes through the tail
static constexpr size_t MAX_SEND_LINES = 22;
static constexpr size_t MAX_SEND_IOV = 1 + 2 * MAX_SEND_LINES;
// Chunk header, retry/id/event lines and the first "data: "
static constexpr size_t PREFIX_BUF_SIZE = 128;
// One byte, so the stream's flags share a word on 32-bit targets
struct LoopFlags {
uint8_t close_requested : 1;
uint8_t close_retry_warning_logged : 1;
uint8_t reserved : 6;
} loop_flags_{};
// 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 +379,7 @@ class DefaultHeaders {
void apply_(httpd_req_t *req, uint8_t skip_mask = 0) const;
};
} // namespace web_server_idf
} // namespace esphome
} // namespace esphome::web_server_idf
using namespace esphome::web_server_idf; // NOLINT(google-global-names-in-headers)
+10 -6
View File
@@ -298,11 +298,14 @@ AP_MANUAL_IP_SCHEMA = cv.Schema(
}
)
STA_MANUAL_IP_SCHEMA = AP_MANUAL_IP_SCHEMA.extend(
{
cv.Optional(CONF_DNS1, default="0.0.0.0"): cv.ipv4address,
cv.Optional(CONF_DNS2, default="0.0.0.0"): cv.ipv4address,
}
STA_MANUAL_IP_SCHEMA = cv.All(
AP_MANUAL_IP_SCHEMA.extend(
{
cv.Optional(CONF_DNS1, default="0.0.0.0"): cv.ipv4address,
cv.Optional(CONF_DNS2, default="0.0.0.0"): cv.ipv4address,
}
),
require_ipv4(DOMAIN, CONF_MANUAL_IP),
)
TTLS_PHASE_2 = {
@@ -388,6 +391,7 @@ def wifi_network_ap(value):
if value is None:
value = {}
config = WIFI_NETWORK_AP(value)
require_ipv4(DOMAIN, CONF_AP)(config)
ap_mode = config[CONF_AP_MODE]
if ap_mode == WIFI_AP_MODE_WISP and not config.get(CONF_PASSWORD):
raise cv.Invalid(
@@ -688,6 +692,7 @@ def _lease_cache_schema(value: Any) -> ConfigType:
value = LEASE_CACHE_SCHEMA(value)
if value[CONF_ENABLED]:
cv.only_on_esp32(value)
require_ipv4(DOMAIN, CONF_LEASE_CACHE)(value)
if CONF_STORAGE not in value:
if not preferences.rtc_supported():
raise cv.Invalid(
@@ -780,7 +785,6 @@ CONFIG_SCHEMA = cv.All(
_apply_min_auth_mode_default,
_validate,
_report_provisioning_credentials,
require_ipv4(DOMAIN),
)
+5 -2
View File
@@ -471,6 +471,9 @@ class WiFiPowerSaveListener {
static constexpr uint32_t AP_HANDOVER_TIMEOUT_ID = 0;
static constexpr uint32_t LEASE_RENEW_TIMEOUT_ID = 1;
static constexpr uint32_t AP_DEAUTH_RETRY_TIMEOUT_ID = 2;
#if USE_NETWORK_IPV6 && (USE_NETWORK_MIN_IPV6_ADDR_COUNT > 0)
static constexpr uint32_t IPV6_RECOUNT_TIMEOUT_ID = 3;
#endif
/// This component is responsible for managing the ESP WiFi interface.
class WiFiComponent final : public Component {
@@ -1116,9 +1119,9 @@ class WiFiComponent final : public Component {
// so no happens-before relationship with other memory needs to be established.
std::atomic<uint8_t> roaming_suppression_count_{0};
#endif
#if USE_NETWORK_IPV6
#if USE_NETWORK_IPV6 && (USE_NETWORK_MIN_IPV6_ADDR_COUNT > 0)
uint8_t num_ipv6_addresses_{0};
#endif /* USE_NETWORK_IPV6 */
#endif
bool error_from_callback_{false};
#if defined(USE_ESP32) && !defined(USE_WIFI_MULTI_SSID)
bool scan_driver_filtered_{false};
@@ -45,6 +45,9 @@
#include <esp_netif_net_stack.h> // esp_netif_get_netif_impl()
#include "lwip/netif.h"
#endif
#if !USE_NETWORK_IPV4
#include "lwip/dhcp6.h"
#endif
#ifdef USE_WIFI_LEASE_CACHE
#include <esp_rtc_time.h> // esp_rtc_get_time_us() -- RTC-domain clock for lease age
#include "lwip/dhcp.h"
@@ -653,7 +656,7 @@ bool WiFiComponent::wifi_sta_connect_(const WiFiAP &ap) {
void WiFiComponent::clear_sta_session_() {
s_sta_connected = false;
this->got_ipv4_address_ = false;
#if USE_NETWORK_IPV6
#if USE_NETWORK_IPV6 && (USE_NETWORK_MIN_IPV6_ADDR_COUNT > 0)
this->num_ipv6_addresses_ = 0;
#endif
}
@@ -666,6 +669,11 @@ static void disable_dhcp_sntp() {
sntp_servermode_dhcp(false);
}
#if USE_NETWORK_IPV6 && (USE_NETWORK_MIN_IPV6_ADDR_COUNT > 0)
// How long after a driver roam SLAAC gets to settle before the IPv6 addresses are recounted.
static constexpr uint32_t IPV6_ROAM_SETTLE_MS = 5000;
#endif
#ifdef USE_WIFI_LEASE_CACHE
// Minimum lease time that must remain before a cached lease is applied.
static constexpr uint32_t LEASE_APPLY_MARGIN_SECS = 60;
@@ -731,6 +739,11 @@ bool WiFiComponent::wifi_sta_ip_config_(const optional<ManualIP> &manual_ip) {
return false;
}
#if !USE_NETWORK_IPV4
// No IPv4 stack: addresses come from SLAAC, validation rejects manual_ip
(void) manual_ip;
return true;
#else
esp_netif_dhcp_status_t dhcp_status;
esp_err_t err = esp_netif_dhcpc_get_status(s_sta_netif, &dhcp_status);
if (err != ESP_OK) {
@@ -812,6 +825,7 @@ bool WiFiComponent::wifi_sta_ip_config_(const optional<ManualIP> &manual_ip) {
}
return true;
#endif // USE_NETWORK_IPV4
}
#ifdef USE_WIFI_LEASE_CACHE
@@ -884,6 +898,7 @@ network::IPAddresses WiFiComponent::wifi_sta_ip_addresses() {
if (!this->has_sta())
return {};
network::IPAddresses addresses;
#if USE_NETWORK_IPV4
esp_netif_ip_info_t ip;
esp_err_t err = esp_netif_get_ip_info(s_sta_netif, &ip);
if (err != ESP_OK) {
@@ -893,6 +908,7 @@ network::IPAddresses WiFiComponent::wifi_sta_ip_addresses() {
} else {
addresses[0] = network::IPAddress(&ip.ip);
}
#endif
#if USE_NETWORK_IPV6
network::fill_ip6_addresses(s_sta_netif, addresses);
#endif /* USE_NETWORK_IPV6 */
@@ -1106,7 +1122,12 @@ void WiFiComponent::wifi_process_event_(IDFWiFiEvent *data) {
if (auto *netif = static_cast<struct netif *>(esp_netif_get_netif_impl(s_sta_netif)); netif != nullptr) {
if (esp_err_t err = esp_netif_tcpip_exec(
[](void *ctx) -> esp_err_t {
netif_create_ip6_linklocal_address(static_cast<struct netif *>(ctx), 1);
auto *netif = static_cast<struct netif *>(ctx);
netif_create_ip6_linklocal_address(netif, 1);
#if !USE_NETWORK_IPV4
// Without DHCPv4, DNS servers come from RDNSS or stateless DHCPv6; esp_netif never starts the latter
dhcp6_enable_stateless(netif);
#endif
return ESP_OK;
},
netif);
@@ -1131,6 +1152,26 @@ void WiFiComponent::wifi_process_event_(IDFWiFiEvent *data) {
bssid_t roam_bssid;
std::copy(it.bssid, it.bssid + 6, roam_bssid.begin());
this->handle_driver_roam_(roam_bssid, it.channel);
#if USE_NETWORK_IPV6 && (USE_NETWORK_MIN_IPV6_ADDR_COUNT > 0)
// GOT_IP6 never counts down; once SLAAC settles drop addresses the new AP did not give back,
// but never below the minimum, so a slow SLAAC does not read as a lost connection
this->set_timeout(IPV6_RECOUNT_TIMEOUT_ID, IPV6_ROAM_SETTLE_MS, [this]() {
if (!s_sta_connected)
return; // A disconnect already cleared the count
esp_ip6_addr_t addresses[CONFIG_LWIP_IPV6_NUM_ADDRESSES];
int count;
{
LwIPLock lock;
count = esp_netif_get_all_ip6(s_sta_netif, addresses);
}
if (count < USE_NETWORK_MIN_IPV6_ADDR_COUNT || count == this->num_ipv6_addresses_)
return;
this->num_ipv6_addresses_ = static_cast<uint8_t>(count);
#ifdef USE_WIFI_IP_STATE_LISTENERS
this->notify_ip_state_listeners_();
#endif
});
#endif
}
#ifdef USE_WIFI_CONNECT_STATE_LISTENERS
// Defer listener notification until state machine reaches STA_CONNECTED
@@ -1193,6 +1234,7 @@ void WiFiComponent::wifi_process_event_(IDFWiFiEvent *data) {
(data->event_id == IP_EVENT_ETH_GOT_IP || data->event_id == IP_EVENT_PPP_GOT_IP)) {
this->apply_ap_dns_();
#endif
#if USE_NETWORK_IPV4
} else if (data->event_base == IP_EVENT && data->event_id == IP_EVENT_STA_GOT_IP) {
const auto &it = data->data.ip_got_ip;
esp_netif_ip_info_t current_ip{};
@@ -1212,20 +1254,30 @@ void WiFiComponent::wifi_process_event_(IDFWiFiEvent *data) {
#ifdef USE_WIFI_IP_STATE_LISTENERS
this->notify_ip_state_listeners_();
#endif
#endif // USE_NETWORK_IPV4
#if USE_NETWORK_IPV6
} else if (data->event_base == IP_EVENT && data->event_id == IP_EVENT_GOT_IP6) {
const auto &it = data->data.ip_got_ip6;
if (it.esp_netif != s_sta_netif) {
return; // another interface's address (AP netif, or ethernet alongside wifi)
}
if (!s_sta_connected) {
ESP_LOGV(TAG, "Ignoring IPv6 address event before association");
return;
}
ESP_LOGV(TAG, "IPv6 address=" IPV6STR, IPV62STR(it.ip6_info.ip));
#if USE_NETWORK_MIN_IPV6_ADDR_COUNT > 0
// Count addresses, not events (a roam re-fires the link-local). Never count down while associated:
// a roam briefly clears every address, which must not read as a lost connection.
esp_ip6_addr_t addresses[CONFIG_LWIP_IPV6_NUM_ADDRESSES];
uint8_t count;
{
LwIPLock lock;
this->num_ipv6_addresses_ = esp_netif_get_all_ip6(s_sta_netif, addresses);
count = static_cast<uint8_t>(esp_netif_get_all_ip6(s_sta_netif, addresses));
}
ESP_LOGV(TAG, "IPv6 address=" IPV6STR, IPV62STR(it.ip6_info.ip));
this->num_ipv6_addresses_ = std::max(this->num_ipv6_addresses_, count);
#endif
#ifdef USE_WIFI_IP_STATE_LISTENERS
this->notify_ip_state_listeners_();
#endif
@@ -1412,7 +1464,7 @@ void WiFiComponent::wifi_process_event_(IDFWiFiEvent *data) {
}
WiFiSTAConnectStatus WiFiComponent::wifi_sta_connect_status_() const {
if (s_sta_connected && this->got_ipv4_address_) {
if (s_sta_connected && (!USE_NETWORK_IPV4 || this->got_ipv4_address_)) {
#if USE_NETWORK_IPV6 && (USE_NETWORK_MIN_IPV6_ADDR_COUNT > 0)
if (this->num_ipv6_addresses_ >= USE_NETWORK_MIN_IPV6_ADDR_COUNT) {
return WiFiSTAConnectStatus::CONNECTED;
@@ -1702,6 +1754,7 @@ int32_t WiFiComponent::get_wifi_channel() {
return primary;
}
network::IPAddress WiFiComponent::wifi_subnet_mask_() {
#if USE_NETWORK_IPV4
esp_netif_ip_info_t ip;
esp_err_t err = esp_netif_get_ip_info(s_sta_netif, &ip);
if (err != ESP_OK) {
@@ -1709,8 +1762,12 @@ network::IPAddress WiFiComponent::wifi_subnet_mask_() {
return {};
}
return network::IPAddress(&ip.netmask);
#else
return {};
#endif
}
network::IPAddress WiFiComponent::wifi_gateway_ip_() {
#if USE_NETWORK_IPV4
esp_netif_ip_info_t ip;
esp_err_t err = esp_netif_get_ip_info(s_sta_netif, &ip);
if (err != ESP_OK) {
@@ -1718,6 +1775,9 @@ network::IPAddress WiFiComponent::wifi_gateway_ip_() {
return {};
}
return network::IPAddress(&ip.gw);
#else
return {};
#endif
}
network::IPAddress WiFiComponent::wifi_dns_ip_(int num) {
const ip_addr_t *dns_ip = dns_getserver(num);
+6 -1
View File
@@ -4,7 +4,8 @@ import re
from esphome import automation
import esphome.codegen as cg
from esphome.components import network, time
from esphome.components.esp32 import CORE, add_idf_sdkconfig_option
from esphome.components.esp32 import CORE, add_idf_component, add_idf_sdkconfig_option
from esphome.components.noise import LIBSODIUM_VERSION, use_registry_libsodium
import esphome.config_validation as cv
from esphome.const import (
CONF_ADDRESS,
@@ -153,6 +154,10 @@ async def to_code(config):
# be automatically added to the provided list.
cg.add_build_flag(f"-DCONFIG_WIREGUARD_MAX_SRC_IPS={len(allowed_ips) + 1}")
cg.add_library("esphome/wireguard", "0.4.8")
# Same rule as noise: the wireguard library then uses the registry libsodium
# instead of its own copy
if use_registry_libsodium():
add_idf_component(name="esphome/libsodium", ref=LIBSODIUM_VERSION)
await cg.register_component(var, config)
+19
View File
@@ -33,6 +33,9 @@
#ifdef USE_STATUS_LED
#include "esphome/components/status_led/status_led.h"
#endif
#ifdef USE_ESP32_TASK_WDT
#include "esp_task_wdt.h"
#endif
namespace esphome {
@@ -235,11 +238,27 @@ void Application::process_dump_config_() {
this->dump_config_at_++;
}
#if !defined(USE_ESP32) || defined(USE_ESP32_TASK_WDT) || defined(USE_STATUS_LED)
void Application::feed_wdt() {
// Cold entry: callers without a millis() timestamp in hand. Fetches the
// time and defers to the hot path.
this->feed_wdt_with_time(MillisInternal::get());
}
#endif
void Application::feed_wdt_if_watched() {
#ifdef USE_ESP32_TASK_WDT
if (esp_task_wdt_status(nullptr) != ESP_OK)
return;
#elif defined(USE_ESP32) && defined(USE_STATUS_LED)
// No watchdog, but the loop task keeps the status LED blinking through a long blocking call
if (xTaskGetCurrentTaskHandle() != esphome_main_task_handle)
return;
#elif defined(USE_ESP32)
return;
#endif
this->feed_wdt();
}
void HOT Application::feed_wdt_slow_(uint32_t time) {
// Callers (both feed_wdt() and feed_wdt_with_time()) have already
+18 -5
View File
@@ -208,8 +208,8 @@ class Application {
* Each component can request a high frequency loop execution by using the HighFrequencyLoopRequester
* helper in helpers.h
*
* Sleep per wake is capped at 2 * WDT_FEED_INTERVAL_MS (except host and ESP8266);
* raise the platform watchdog timeout to sleep longer.
* Sleep per wake is capped at 2 * WDT_FEED_INTERVAL_MS (except host, ESP8266, and ESP32
* with watchdog_timeout: 0s); raise the platform watchdog timeout to sleep longer.
*
* @param loop_interval The interval in milliseconds to run the core loop at. Defaults to 16 milliseconds.
*/
@@ -239,6 +239,7 @@ class Application {
// https://github.com/libretiny-eu/framework-beken-bdk/blob/44800e7451ea30fbcbd3bb6e905315de59349fee/beken378/driver/wdt/wdt.c#L75-L87
static constexpr uint32_t WDT_FEED_INTERVAL_MS = 2000;
#elif defined(USE_ESP32)
#ifdef USE_ESP32_TASK_WDT
// Auto-scale to 1/5 of the configured ESP32 task WDT timeout so the safety
// margin stays constant when the user raises esp32.watchdog_timeout (default
// 5 s → 1000 ms feed; 10 s → 2000 ms; 60 s → 12000 ms). The esp32 component
@@ -252,6 +253,10 @@ class Application {
static_assert(CONFIG_ESP_TASK_WDT_TIMEOUT_S >= 5,
"CONFIG_ESP_TASK_WDT_TIMEOUT_S must be at least 5s for a safe WDT feed interval");
static constexpr uint32_t WDT_FEED_INTERVAL_MS = (CONFIG_ESP_TASK_WDT_TIMEOUT_S * 1000U) / 5U;
#else
// No task watchdog to feed; the rate limit never fires.
static constexpr uint32_t WDT_FEED_INTERVAL_MS = std::numeric_limits<uint32_t>::max();
#endif
#elif defined(USE_ESP8266)
// ESP8266 needs a tighter feed cadence than the other targets: the soft WDT
// is ~1.6 s and the HW WDT ~6 s, but a single long iteration (mDNS reply,
@@ -267,7 +272,15 @@ class Application {
/// Feed the task watchdog. Cold entry — callers without a millis()
/// timestamp in hand. Out of line to keep call sites tiny.
#if defined(USE_ESP32) && !defined(USE_ESP32_TASK_WDT) && !defined(USE_STATUS_LED)
// watchdog_timeout: 0s leaves no watchdog to feed, and no status LED needs the call
void feed_wdt() {}
#else
void feed_wdt();
#endif
/// Feed from code that may run off the loop task (a long download or image decode): on ESP32 only a task the
/// watchdog watches feeds it. With watchdog_timeout: 0s only the loop task calls through, to run the status LED.
void feed_wdt_if_watched();
#ifdef USE_STATUS_LED
/// Dispatch interval for the status LED update. Deliberately shorter than
@@ -859,9 +872,9 @@ inline void ESPHOME_ALWAYS_INLINE Application::loop() {
// SDK os_timer_arm() accepts at most 0x68D7A3 ms without system_timer_reinit();
// the SDK feeds both watchdogs while the cont task is suspended, so no WDT cap needed.
static constexpr uint32_t MAX_SLEEP_BASE = 0x68D7A3;
#elif defined(USE_HOST)
// arch_feed_wdt() is a no-op on host and ESPHOME_SUSPEND_LOOP is rejected by
// the config validator, so delay_time is already bounded by loop_interval_.
#elif defined(USE_HOST) || (defined(USE_ESP32) && !defined(USE_ESP32_TASK_WDT))
// No watchdog to feed (host, or ESP32 with watchdog_timeout: 0s), so nothing caps the sleep; with
// suspend_loop an ESP32 may sleep until a wake notification arrives.
static constexpr uint32_t MAX_SLEEP_BASE = std::numeric_limits<uint32_t>::max();
#else
static constexpr uint32_t MAX_SLEEP_BASE = WDT_FEED_INTERVAL_MS * 2;
+32 -10
View File
@@ -45,9 +45,7 @@ from esphome.const import (
CONF_TRIGGER_ID,
CONF_URL,
CONF_VERSION,
CONF_WATCHDOG_TIMEOUT,
KEY_CORE,
PLATFORM_ESP32,
PLATFORM_HOST,
PLATFORM_RP2,
PlatformFramework,
@@ -77,6 +75,7 @@ CONF_RELATION = "relation"
CONF_SBOM = "sbom"
CONF_SUPPLIER = "supplier"
CONF_SUSPEND_LOOP = "suspend_loop"
CONF_FRIENDLY_NAME_SEPARATOR = "friendly_name_separator"
# Application::loop_interval_ when loop_interval is not set. Make sure to align with application.h
DEFAULT_LOOP_INTERVAL = TimePeriodMilliseconds(milliseconds=16)
@@ -165,6 +164,23 @@ def validate_hostname(config):
return config
# DeviceInfoResponse sends the friendly name as a short string (encode_short_string_force in api/proto.h)
FRIENDLY_NAME_WITH_SUFFIX_MAX_LEN = 127
def validate_friendly_name_with_suffix(config: ConfigType) -> ConfigType:
if not config[CONF_NAME_ADD_MAC_SUFFIX] or not config[CONF_FRIENDLY_NAME]:
return config
full = f"{config[CONF_FRIENDLY_NAME]}{config[CONF_FRIENDLY_NAME_SEPARATOR]}{_MAC_SUFFIX_PLACEHOLDER}"
if (length := len(full.encode("utf-8"))) > FRIENDLY_NAME_WITH_SUFFIX_MAX_LEN:
raise cv.Invalid(
f"friendly_name with {CONF_FRIENDLY_NAME_SEPARATOR} and the MAC suffix is {length} "
f"bytes; at most {FRIENDLY_NAME_WITH_SUFFIX_MAX_LEN} fit",
path=[CONF_FRIENDLY_NAME],
)
return config
def validate_ids_and_references(config: ConfigType) -> ConfigType:
"""Validate that there are no hash collisions between IDs and that area_id references are valid.
@@ -227,13 +243,13 @@ def validate_loop_interval(config: ConfigType) -> ConfigType:
# max_loop interval is calculated from WDT_FEED_INTERVAL_MS. Make sure to align with application.h
max_loop = 600 # 2 * 300ms default for other platforms
if CORE.is_esp32:
max_loop = (
2
* fv.full_config.get()[PLATFORM_ESP32][
CONF_WATCHDOG_TIMEOUT
].total_milliseconds
// 5
)
from esphome.components.esp32 import task_wdt_timeout_ms
wdt_ms = task_wdt_timeout_ms()
if wdt_ms == 0:
# Task watchdog disabled: the loop sleep is not capped
return config
max_loop = 2 * wdt_ms // 5
elif CORE.is_bk72xx:
max_loop = 4000 # 10000ms / 5 * 2 default value
if config[CONF_LOOP_INTERVAL].total_milliseconds > max_loop:
@@ -448,6 +464,9 @@ CONFIG_SCHEMA = cv.All(
CONF_LIBRARIES, default=[], visibility=cv.Visibility.YAML_ONLY
): cv.ensure_list(cv.string_strict),
cv.Optional(CONF_NAME_ADD_MAC_SUFFIX, default=False): cv.boolean,
cv.Optional(
CONF_FRIENDLY_NAME_SEPARATOR, default=_APP_FRIENDLY_NAME_MAC_SEP
): cv.All(cv.string_no_slash, cv.ByteLength(max=3)),
cv.Optional(CONF_MERGE_WARNINGS, default=True): cv.boolean,
cv.Optional(
CONF_DEBUG_SCHEDULER, default=False, visibility=cv.Visibility.YAML_ONLY
@@ -500,6 +519,7 @@ CONFIG_SCHEMA = cv.All(
}
),
validate_hostname,
validate_friendly_name_with_suffix,
)
@@ -907,7 +927,9 @@ async def to_code(config: ConfigType) -> None:
name_expr, name_len = _emit_app_name(name, _APP_NAME_BUF_VAR, _APP_NAME_MAC_SEP)
friendly_expr, friendly_len = _emit_app_name(
friendly_name, _APP_FRIENDLY_NAME_BUF_VAR, _APP_FRIENDLY_NAME_MAC_SEP
friendly_name,
_APP_FRIENDLY_NAME_BUF_VAR,
config[CONF_FRIENDLY_NAME_SEPARATOR],
)
if name_add_mac_suffix:
cg.add_define("ESPHOME_NAME_ADD_MAC_SUFFIX")
+2
View File
@@ -538,6 +538,7 @@
#define USE_ESP32_MIN_CHIP_REVISION_SET
#define USE_ESP32_RTC_PREFERENCES
#define USE_ESP32_SRAM1_AS_IRAM
#define USE_ESP32_TASK_WDT
#define USE_ESPNOW
#define USE_ESPNOW_MAX_PAYLOAD_SIZE 1470
@@ -634,6 +635,7 @@
#define USE_WEBSERVER_CSS_INCLUDE
#define USE_WEBSERVER_JS_INCLUDE
#define USE_WEBSERVER_PRIVATE_NETWORK_ACCESS
#define USE_WEBSERVER_SSL
#define USE_WEBSERVER_VERSION 3
#define WEB_SERVER_DEFAULT_HEADERS_COUNT 1
#define WEB_SERVER_MAX_OPEN_SOCKETS 7
+13 -320
View File
@@ -1,30 +1,11 @@
#pragma once
#include <algorithm>
#include <cstddef>
#include <cstdint>
#include <cstring>
#include <type_traits>
// Out of line, one copy: the frame readers' rare paths call each other, and GCC would add specialised clones
#if defined(__clang__)
#define ESPHOME_FRAME_READER_COLD __attribute__((noinline))
#else
#define ESPHOME_FRAME_READER_COLD __attribute__((noinline, noclone))
#endif
namespace esphome {
namespace frame_reader_detail {
// A fold, not an array, so no header table lands in RAM on ESP8266
template<uint8_t... HEADER> constexpr uint8_t header_byte(size_t i) {
size_t j = 0;
uint8_t r = 0;
((r = (j++ == i) ? HEADER : r), ...);
return r;
}
} // namespace frame_reader_detail
/// Collects fixed length binary frames of N bytes that start with the header bytes HEADER...; the binary twin of
/// LineReader. Bytes before a header are skipped; checksums are left to the caller.
template<size_t N, uint8_t... HEADER> class FrameReader {
@@ -37,12 +18,11 @@ template<size_t N, uint8_t... HEADER> class FrameReader {
/// Feed one byte; returns the completed frame, header included, valid until the next call, or nullptr.
const uint8_t *feed(uint8_t c) {
pos_t pos = this->pos_;
if (pos < HEADER_LEN && c != frame_reader_detail::header_byte<HEADER...>(pos)) {
if (pos < HEADER_LEN && c != header_byte(pos)) {
// A later byte of the broken header, or c, may start the real one
if (pos == 0)
return nullptr;
this->buf_[pos] = c;
return this->replay_(pos);
this->replay_(pos);
return nullptr;
}
this->buf_[pos++] = c;
if (pos < N) {
@@ -64,311 +44,24 @@ template<size_t N, uint8_t... HEADER> class FrameReader {
void resync() { this->replay_(N - 1); }
protected:
// Start over and feed buf_[1..last] again; never completes a frame as last < N, so it returns nullptr, which lets
// feed() end in a tail call. Out of line, as feed() and replay_() call each other.
const uint8_t *ESPHOME_FRAME_READER_COLD replay_(size_t last) {
// A fold, not an array, so no header table lands in RAM on ESP8266
static constexpr uint8_t header_byte(size_t i) {
size_t j = 0;
uint8_t r = 0;
((r = (j++ == i) ? HEADER : r), ...);
return r;
}
// Start over and feed buf_[1..last] again; never completes a frame as last < N
void replay_(size_t last) {
this->pos_ = 0;
for (size_t i = 1; i <= last; i++) {
this->feed(this->buf_[i]);
}
return nullptr;
}
uint8_t buf_[N];
pos_t pos_{0};
};
// Length rules: get() gives the whole frame length once KNOWN_AT bytes are in, or 0 to reject the header.
/// offset: where the length field starts, counted from the first header byte; trailer: the bytes after the payload
/// (checksum, footer), for a payload length only.
struct LengthField {
size_t offset;
size_t trailer = 0;
};
/// Whole-frame length: the field counts every byte of the frame, header included.
template<LengthField F> struct FrameLengthU8 {
static_assert(F.trailer == 0, "A whole-frame length has no trailer");
static constexpr size_t KNOWN_AT = F.offset + 1;
static constexpr size_t get(const uint8_t *buf) { return buf[F.offset]; }
};
template<LengthField F> struct FrameLengthU16BE {
static_assert(F.trailer == 0, "A whole-frame length has no trailer");
static constexpr size_t KNOWN_AT = F.offset + 2;
static constexpr size_t get(const uint8_t *buf) { return (size_t(buf[F.offset]) << 8) | buf[F.offset + 1]; }
};
template<LengthField F> struct FrameLengthU16LE {
static_assert(F.trailer == 0, "A whole-frame length has no trailer");
static constexpr size_t KNOWN_AT = F.offset + 2;
static constexpr size_t get(const uint8_t *buf) { return buf[F.offset] | (size_t(buf[F.offset + 1]) << 8); }
};
/// Payload length: the field counts the payload right after it.
template<LengthField F> struct PayloadLengthU8 {
static constexpr size_t KNOWN_AT = F.offset + 1;
static constexpr size_t get(const uint8_t *buf) { return KNOWN_AT + buf[F.offset] + F.trailer; }
};
template<LengthField F> struct PayloadLengthU16BE {
static constexpr size_t KNOWN_AT = F.offset + 2;
static constexpr size_t get(const uint8_t *buf) {
return KNOWN_AT + FrameLengthU16BE<LengthField{.offset = F.offset}>::get(buf) + F.trailer;
}
};
template<LengthField F> struct PayloadLengthU16LE {
static constexpr size_t KNOWN_AT = F.offset + 2;
static constexpr size_t get(const uint8_t *buf) {
return KNOWN_AT + FrameLengthU16LE<LengthField{.offset = F.offset}>::get(buf) + F.trailer;
}
};
/// A frame type that always has LEN bytes.
template<size_t LEN> struct FrameFixedLength {
static_assert(LEN >= 2, "A fixed length frame has at least 2 bytes");
static constexpr size_t KNOWN_AT = 1;
static constexpr size_t get(const uint8_t * /*buf*/) { return LEN; }
};
/// Wraps a length rule: CHECK(buf) runs once AT bytes are in, and a header it fails gives length 0 (rejected).
template<class LENGTH, size_t AT, bool (*CHECK)(const uint8_t *)> struct CheckedLength {
static_assert(AT >= LENGTH::KNOWN_AT, "AT must cover the length field");
static constexpr size_t KNOWN_AT = AT;
static size_t get(const uint8_t *buf) { return CHECK(buf) ? LENGTH::get(buf) : 0; }
};
/// Wraps a length rule: a frame longer than MAX calls WARN(length) and is rejected; a replay can warn again.
template<class LENGTH, size_t MAX, void (*WARN)(size_t)> struct WarnIfLonger {
static constexpr size_t KNOWN_AT = LENGTH::KNOWN_AT;
static size_t get(const uint8_t *buf) {
const size_t len = LENGTH::get(buf);
if (len <= MAX)
return len;
WARN(len);
return 0;
}
};
/// The shortest frame of a FrameFormat, when that is more than its header and length field.
template<size_t LEN> struct FrameMinLength {
static constexpr size_t VALUE = LEN;
};
namespace frame_reader_detail {
// The length of a FrameFixedLength rule, 0 for every other rule
template<class LENGTH> constexpr size_t FIXED_LENGTH = 0;
template<size_t LEN> constexpr size_t FIXED_LENGTH<FrameFixedLength<LEN>> = LEN;
template<class LENGTH, size_t AT, bool (*CHECK)(const uint8_t *)>
constexpr size_t FIXED_LENGTH<CheckedLength<LENGTH, AT, CHECK>> = FIXED_LENGTH<LENGTH>;
template<class LENGTH, size_t MAX, void (*WARN)(size_t)>
constexpr size_t FIXED_LENGTH<WarnIfLonger<LENGTH, MAX, WARN>> = FIXED_LENGTH<LENGTH>;
// The MAX of a WarnIfLonger rule, 0 for every other rule
template<class LENGTH> constexpr size_t WARN_MAX = 0;
template<class LENGTH, size_t MAX, void (*WARN)(size_t)>
constexpr size_t WARN_MAX<WarnIfLonger<LENGTH, MAX, WARN>> = MAX;
template<class LENGTH, size_t AT, bool (*CHECK)(const uint8_t *)>
constexpr size_t WARN_MAX<CheckedLength<LENGTH, AT, CHECK>> = WARN_MAX<LENGTH>;
} // namespace frame_reader_detail
/// The bytes every frame of a FrameFormat starts with.
template<uint8_t... BYTES> struct FrameHeader {
static constexpr size_t LEN = sizeof...(BYTES);
static_assert(LEN >= 1 && LEN <= 8, "A FrameHeader has 1 to 8 bytes");
static constexpr uint8_t byte(size_t i) { return frame_reader_detail::header_byte<BYTES...>(i); }
};
/// One frame type for LengthFrameReader: a FrameHeader, a length rule from above and an optional FrameMinLength.
template<class HEADER, class LENGTH, class MIN = FrameMinLength<0>> struct FrameFormat {
using Length = LENGTH;
static constexpr size_t HEADER_LEN = HEADER::LEN;
static constexpr uint8_t FIRST = HEADER::byte(0);
// Bytes collected when the length is checked
static constexpr size_t MIN_LEN = std::max<size_t>({LENGTH::KNOWN_AT, HEADER_LEN, 2});
// A shorter length is not a frame
static constexpr size_t SHORTEST = std::max(MIN_LEN, MIN::VALUE);
static_assert(LENGTH::KNOWN_AT == 1 || LENGTH::KNOWN_AT > HEADER_LEN, "The length field must follow the header");
static constexpr uint8_t header_byte(size_t i) { return HEADER::byte(i); }
};
/// Collects binary frames that give their own length, of up to N bytes, in one or more FrameFormats; the first byte
/// picks the format, so each format needs a different first header byte. Bytes before a header are skipped. A length
/// that ends the frame before its length field, or is longer than N, is not a frame, and the reader looks for a
/// header after its first byte. Checksums and footers are left to the caller, which passes a handler that returns
/// false for a frame it rejects:
///
/// this->reader_.feed(c, [this](const uint8_t *frame, size_t len) { return this->handle_frame_(frame, len); });
///
/// For frames of one fixed size, FrameReader is smaller.
template<size_t N, class... FORMATS> class LengthFrameReader {
using pos_t = std::conditional_t<(N <= 0xFF), uint8_t, uint16_t>;
static_assert(sizeof...(FORMATS) >= 1, "LengthFrameReader takes at least one FrameFormat");
// flush_() copies a partial frame on the stack
static_assert(N >= 2 && N <= 1024, "LengthFrameReader size out of range");
static constexpr bool distinct_first() {
const uint8_t first[] = {FORMATS::FIRST...};
for (size_t i = 0; i < sizeof...(FORMATS); i++) {
for (size_t j = i + 1; j < sizeof...(FORMATS); j++) {
if (first[i] == first[j])
return false;
}
}
return true;
}
static_assert(distinct_first(), "Each FrameFormat needs a different first header byte");
static_assert(((FORMATS::SHORTEST <= N) && ...), "N must hold the shortest frame of every FrameFormat");
static_assert(((frame_reader_detail::FIXED_LENGTH<typename FORMATS::Length> == 0 ||
(frame_reader_detail::FIXED_LENGTH<typename FORMATS::Length> >= FORMATS::SHORTEST &&
frame_reader_detail::FIXED_LENGTH<typename FORMATS::Length> <= N)) &&
...),
"A fixed length must hold the header and fit in N");
static_assert(((frame_reader_detail::WARN_MAX<typename FORMATS::Length> <= N) && ...),
"A WarnIfLonger MAX above N lets longer frames be skipped without a warning");
public:
/// Feed one byte; calls handle(frame, length) for each frame it completes, header included. A rejected frame
/// (handle returns false) is searched for frames inside it and after it, which go to handle the same way.
/// handle must not feed this reader again: the frame and the bytes held behind it live in its buffer.
template<class F> void __attribute__((always_inline)) feed(uint8_t c, F &&handle) {
// Most bytes are in the middle of a frame whose length is known, and not its last byte
const pos_t pos = this->pos_;
if (pos + 1u < this->end_) {
this->buf_[pos] = c;
this->pos_ = pos + 1;
} else if (const size_t len = this->feed_edge_(c)) {
this->drain_(len, handle);
}
}
/// For a partial frame idle too long: hands over the frames a corrupt length holds back inside it, else keeps it for
/// a slow UART. UARTDevice::read_frames() calls this.
template<class F> void flush(F &&handle) {
// held_end_ marks a partial frame already searched; new bytes move pos_ past it
if (this->pos_ != 0 && this->held_end_ != this->pos_)
this->flush_(handle);
}
/// Number of bytes of the partial frame; 0 after a frame is returned and while looking for a header.
size_t length() const { return this->pos_; }
/// Whether a frame is coming in: a partial frame that an idle flush has not searched and kept.
bool receiving() const { return this->pos_ != 0 && this->held_end_ != this->pos_; }
/// Forget a partial frame an idle flush kept, before a request starts a new exchange; safe inside a frame handler.
void drop_kept() {
if (this->pos_ != 0 && this->held_end_ == this->pos_)
this->reset();
}
/// Forget the partial frame, for a request/response protocol starting a new exchange.
void reset() {
this->pos_ = 0;
this->end_ = 0;
this->held_end_ = 0;
}
protected:
// Hands each frame (len bytes at buf_) to handle, then goes on after a handled frame or from the second byte of a
// rejected one
template<class F> bool ESPHOME_FRAME_READER_COLD drain_(size_t len, F &handle) {
bool handled = false;
while (len != 0) {
const bool ok = handle(this->buf_, len);
handled |= ok;
len = this->replay_(ok ? len : 1, this->held_end_);
}
this->held_end_ = 0;
return handled;
}
// Searches the partial frame for frames; when handle takes none, puts the partial frame back and marks it searched
template<class F> void ESPHOME_FRAME_READER_COLD flush_(F &handle) {
const pos_t pos = this->pos_;
const pos_t end = this->end_;
uint8_t saved[N];
std::memcpy(saved, this->buf_, pos);
while (this->pos_ != 0) {
// A partial frame left behind the handed over ones is searched on the next idle pass
if (this->drain_(this->replay_(1, this->pos_), handle))
return;
}
std::memcpy(this->buf_, saved, pos);
this->pos_ = pos;
this->end_ = end;
this->held_end_ = pos;
}
// Every byte but the middle of a frame; returns the length of a completed frame, or 0
size_t ESPHOME_FRAME_READER_COLD feed_edge_(uint8_t c) {
// A byte that starts or ends a frame clears the searched mark, so it cannot match a later partial frame
this->held_end_ = 0;
const pos_t pos = this->pos_;
if (pos == 0) {
const bool header = ((c == FORMATS::FIRST) || ...);
if (header) {
this->buf_[0] = c;
this->pos_ = 1;
}
return 0;
}
if constexpr (sizeof...(FORMATS) == 1) {
return this->feed_as_<FORMATS...>(pos, c);
} else {
const uint8_t first = this->buf_[0];
size_t len = 0;
(void) ((first == FORMATS::FIRST && ((len = this->feed_as_<FORMATS>(pos, c)), true)) || ...);
return len;
}
}
template<class F> size_t feed_as_(pos_t pos, uint8_t c) {
if (pos < F::HEADER_LEN && c != F::header_byte(pos)) {
// A later byte of the broken header, or c, may start the real one
this->buf_[pos] = c;
return this->replay_(1, pos + 1);
}
this->buf_[pos++] = c;
if (pos == F::MIN_LEN) {
const size_t len = F::Length::get(this->buf_);
if (len < F::SHORTEST || len > N)
return this->replay_(1, pos);
this->end_ = len;
}
if (pos == this->end_) {
this->pos_ = 0;
this->end_ = 0;
this->held_end_ = pos;
return pos;
}
this->pos_ = pos;
return 0;
}
// Start over and feed buf_[from..to) again; writes stay behind the read position. On a completed frame, moves the
// bytes not fed yet right behind it, for drain_(), and returns its length; 0 otherwise.
size_t ESPHOME_FRAME_READER_COLD replay_(size_t from, size_t to) {
this->pos_ = 0;
this->end_ = 0;
for (size_t i = from; i < to; i++) {
const size_t len = this->feed_edge_(this->buf_[i]);
if (len == 0)
continue;
// A nested replay may already have put bytes of its own behind the frame
const size_t held = this->held_end_;
const size_t rest = to - i - 1;
std::memmove(this->buf_ + held, this->buf_ + i + 1, rest);
this->held_end_ = held + rest;
return len;
}
return 0;
}
// Before buf_, so they stay within a short load offset of the reader when N is large
pos_t pos_{0};
// Length of the frame being collected once its length is known; 0 otherwise
pos_t end_{0};
// End of a held frame plus the bytes after it not fed yet; or, after flush() kept a partial frame, the pos_ it
// searched
pos_t held_end_{0};
uint8_t buf_[N];
};
} // namespace esphome
#undef ESPHOME_FRAME_READER_COLD
+26 -26
View File
@@ -71,36 +71,36 @@ def patch_file_downloader() -> None:
def patched_init(self, *args: Any, **kwargs: Any) -> None:
max_retries = 5
for attempt in range(max_retries):
for attempt in range(max_retries - 1):
try:
original_init(self, *args, **kwargs)
return
except (PackageException, OSError) as e:
if attempt < max_retries - 1:
delay = 2 ** (attempt + 1)
_LOGGER.warning(
"Package download failed: %s. "
"Retrying in %d seconds... (attempt %d/%d)",
str(e),
delay,
attempt + 1,
max_retries,
)
# pylint: disable=protected-access,broad-except
try:
if (
hasattr(self, "_http_response")
and self._http_response is not None
):
self._http_response.close()
if hasattr(self, "_http_session"):
self._http_session.close()
except Exception: # noqa: BLE001
pass
# pylint: enable=protected-access,broad-except
time.sleep(delay)
else:
raise
delay = 2 ** (attempt + 1)
_LOGGER.warning(
"Package download failed: %s. "
"Retrying in %d seconds... (attempt %d/%d)",
str(e),
delay,
attempt + 1,
max_retries,
)
# pylint: disable=protected-access,broad-except
try:
if (
hasattr(self, "_http_response")
and self._http_response is not None
):
self._http_response.close()
if hasattr(self, "_http_session"):
self._http_session.close()
except Exception: # noqa: BLE001
pass
# pylint: enable=protected-access,broad-except
time.sleep(delay)
# Final attempt: let any failure propagate unchanged
original_init(self, *args, **kwargs)
patched_init._esphome_patched = True # type: ignore[attr-defined] # pylint: disable=protected-access
FileDownloader.__init__ = patched_init
+1 -1
View File
@@ -1,4 +1,4 @@
pylint==4.1.3
pylint==4.1.4
ruff==0.17.5 # .pre-commit-config.yaml rev synced by script/sync_dependency_versions.py
pyupgrade==3.22.0 # .pre-commit-config.yaml rev synced by script/sync_dependency_versions.py
prek==0.5.10 # .github/workflows/ci.yml reads this pin
+1
View File
@@ -107,6 +107,7 @@ ISOLATED_COMPONENTS = {
"tinyusb_keyboard": "Depends on tinyusb which conflicts with usb_host",
"uart_mux": "Depends on tinyusb which conflicts with usb_host",
"usb_cdc_acm": "Depends on tinyusb which conflicts with usb_host",
"web_server_idf": "Its HTTPS variant (test-ssl) can't be merged with web_server or captive_portal, and grouped runs skip variants",
"brizlabs_led_string": "Takes SPI2_HOST for itself, which the shared spi: test bus also uses",
}
@@ -20,12 +20,6 @@ static constexpr uint8_t FRAME_30[30] = {0xAA, 0xFF, 0x03, 0x00, 0x0E, 0x03, 0xB
static constexpr uint8_t ULTRASONIC_NOISY[] = {0x12, 0xFF, 0x07, 0xA1, 0xA7, 0x34, 0xFF, 0xFF, 0x07, 0xA1, 0xA7,
0x00, 0xFF, 0x07, 0xA1, 0xA7, 0x56, 0xFF, 0x07, 0xA1, 0xA7};
// Pioneer WT shaped frames (A5 01 header, whole-frame length at byte 7) of 12, 20 and 12 bytes among noise
static constexpr uint8_t LENGTH_FRAMES[] = {0x00, 0xA5, 0x01, 0x01, 0x23, 0x00, 0xA5, 0x00, 0x0C, 0x11, 0x22, 0x33,
0x44, 0xA5, 0x01, 0x01, 0x23, 0x00, 0xA5, 0x00, 0x14, 0x11, 0x22, 0x33,
0x44, 0x55, 0x66, 0x77, 0x88, 0x99, 0xAA, 0xBB, 0xCC, 0x7E, 0xA5, 0x01,
0x01, 0x23, 0x00, 0xA5, 0x00, 0x0C, 0x11, 0x22, 0x33, 0x44};
// --- LineReader: one GGA sentence fed byte by byte ---
static void LineReader_NmeaLine(benchmark::State &state) {
@@ -99,32 +93,4 @@ static void FrameReader_UltrasonicNoisy(benchmark::State &state) {
}
BENCHMARK(FrameReader_UltrasonicNoisy);
// --- LengthFrameReader: frames of different lengths among noise ---
// A5 01, whole-frame length at 7
using PioneerFormat = FrameFormat<FrameHeader<0xA5, 0x01>, FrameLengthU8<LengthField{.offset = 7}>>;
static void LengthFrameReader_MixedFrames(benchmark::State &state) {
LengthFrameReader<255, PioneerFormat> reader;
const uint8_t *bytes = LENGTH_FRAMES;
uint32_t frames = 0;
auto handle = [&frames](const uint8_t *frame, size_t /*len*/) {
benchmark::DoNotOptimize(frame);
frames++;
return true;
};
for (auto _ : state) {
benchmark::DoNotOptimize(bytes);
for (int i = 0; i < kInnerIterations; i++) {
for (size_t j = 0; j < sizeof(LENGTH_FRAMES); j++)
reader.feed(bytes[j], handle);
}
benchmark::DoNotOptimize(frames);
}
state.SetItemsProcessed(state.iterations() * kInnerIterations);
}
BENCHMARK(LengthFrameReader_MixedFrames);
} // namespace esphome::benchmarks
@@ -797,6 +797,73 @@ def _gdey029z95_config(**overrides: Any) -> ConfigType:
return config
def test_waveshare_7p5in_hd_b_uses_the_ssd1677_bwr_driver() -> None:
"""The 7.5" HD (B) uses EPaperSSD1677BWR, and its 528 rows are the last of the controller's 680 gates."""
model = MODELS["WAVESHARE-7.5IN-HD-B"]
assert model.class_name == "EPaperSSD1677BWR"
assert model.get_dimensions({}) == (880, 528)
assert model.get_constructor_args({}) == (152,)
assert model.get_init_sequence({}) == (
(0x0C, 0xAE, 0xC7, 0xC3, 0xC0, 0x40),
(0x01, 0xA7, 0x02, 0x01),
(0x11, 0x01),
(0x3C, 0x01),
(0x18, 0x80),
)
@pytest.mark.parametrize(
("dimensions", "error", "error_key"),
[
({"width": 880, "height": 528}, None, None),
(
{"width": 880, "height": 528, "offset_height": 153},
"must not exceed 680 gates",
"height",
),
({"width": 880, "height": 680, "offset_height": 0}, None, None),
(
{"width": 880, "height": 681, "offset_height": 0},
"must not exceed 680 gates",
"height",
),
# offset_height not written: the model's 152 applies, and the error must not point at a missing key
({"width": 880, "height": 529}, "must not exceed 680 gates", "height"),
({"width": 960, "height": 528}, None, None),
({"width": 968, "height": 528}, "must not exceed 960 sources", "width"),
],
)
def test_waveshare_7p5in_hd_b_dimensions_must_fit_the_controller(
set_core_config: SetCoreConfigCallable,
set_component_config: Callable[[str, Any], None],
dimensions: dict[str, int],
error: str | None,
error_key: str | None,
) -> None:
"""The SSD1677 has 960 sources for the width and 680 gates for offset_height plus the height."""
set_core_config(
PlatformFramework.ESP32_IDF,
platform_data={KEY_BOARD: "esp32dev", KEY_VARIANT: VARIANT_ESP32},
)
set_component_config("spi", {"id": "spi_bus", "clk_pin": 18, "mosi_pin": 19})
config = {
"id": "test_display",
"model": "waveshare-7.5in-hd-b",
"dc_pin": 21,
"reset_pin": 22,
"busy_pin": 23,
"dimensions": dimensions,
}
if error is None:
run_schema_validation(config)
else:
with pytest.raises(cv.Invalid, match=error) as exc_info:
run_schema_validation(config)
assert exc_info.value.path[-2:] == ["dimensions", error_key]
def test_gdey029z95_uses_the_good_display_driver() -> None:
"""The GDEY029Z95 uses EPaperGoodDisplay3C with the WeAct init sequence."""
model = MODELS[GDEY029Z95.upper()]
@@ -0,0 +1,8 @@
esphome:
name: test
esp32:
board: esp32dev
framework:
type: esp-idf
watchdog_timeout: 10s
@@ -0,0 +1,7 @@
esphome:
name: test
esp32:
board: esp32dev
framework:
type: esp-idf
@@ -0,0 +1,8 @@
esphome:
name: test
esp32:
board: esp32dev
framework:
type: esp-idf
watchdog_timeout: 0s
+47
View File
@@ -354,6 +354,28 @@ def test_sdkconfig_cpu_frequency_errors(
assert exc_info.value.path == ["framework", "sdkconfig_options"]
@pytest.mark.parametrize("watchdog_timeout", ["0s", "5s", "60s"])
def test_esp32_watchdog_timeout_valid(
set_core_config: SetCoreConfigCallable, watchdog_timeout: str
) -> None:
"""0 disables the task watchdog; otherwise 5 to 60 seconds."""
set_core_config(PlatformFramework.ESP32_IDF)
from esphome.components.esp32 import CONFIG_SCHEMA
CONFIG_SCHEMA({"variant": VARIANT_ESP32, "watchdog_timeout": watchdog_timeout})
@pytest.mark.parametrize("watchdog_timeout", ["1s", "4s", "61s"])
def test_esp32_watchdog_timeout_invalid(
set_core_config: SetCoreConfigCallable, watchdog_timeout: str
) -> None:
set_core_config(PlatformFramework.ESP32_IDF)
from esphome.components.esp32 import CONFIG_SCHEMA
with pytest.raises(cv.Invalid, match="must be 0s"):
CONFIG_SCHEMA({"variant": VARIANT_ESP32, "watchdog_timeout": watchdog_timeout})
@pytest.mark.parametrize(
("config_toolchain", "expected"),
[
@@ -2552,3 +2574,28 @@ def test_require_idf_version_rejects_other_platforms(
set_core_config(PlatformFramework.ESP8266_ARDUINO)
with pytest.raises(cv.Invalid, match="only available on"):
require_idf_version(cv.Version(5, 5, 3))("x")
@pytest.mark.parametrize(
("config_name", "timeout"),
[("watchdog_disabled", None), ("watchdog_default", 5), ("watchdog_custom", 10)],
)
def test_task_watchdog_sdkconfig_and_define(
generate_main: Callable[[str | Path], str],
component_config_path: Callable[[str], Path],
config_name: str,
timeout: int | None,
) -> None:
"""watchdog_timeout: 0s turns the task watchdog off; any other value configures it."""
generate_main(component_config_path(f"{config_name}.yaml"))
sdkconfig = CORE.data[KEY_ESP32][KEY_SDKCONFIG_OPTIONS]
defines = {define.name for define in CORE.defines}
assert sdkconfig["CONFIG_ESP_TASK_WDT_INIT"] is (timeout is not None)
assert ("USE_ESP32_TASK_WDT" in defines) is (timeout is not None)
if timeout is None:
assert "CONFIG_ESP_TASK_WDT_TIMEOUT_S" not in sdkconfig
return
assert sdkconfig["CONFIG_ESP_TASK_WDT_TIMEOUT_S"] == timeout
assert sdkconfig["CONFIG_ESP_TASK_WDT_PANIC"] is True
assert sdkconfig["CONFIG_ESP_TASK_WDT_CHECK_IDLE_TASK_CPU0"] is False
assert sdkconfig["CONFIG_ESP_TASK_WDT_CHECK_IDLE_TASK_CPU1"] is False
@@ -0,0 +1,13 @@
esphome:
name: test
esp32:
board: nodemcu-32s
watchdog_timeout: 0s
wifi:
ssid: test
password: testtest
http_request:
timeout: 10s
@@ -0,0 +1,13 @@
esphome:
name: test
esp32:
board: nodemcu-32s
watchdog_timeout: 0s
wifi:
ssid: test
password: testtest
http_request:
watchdog_timeout: 20s
@@ -33,10 +33,29 @@ def test_esp32_watchdog_timeout(
)
@pytest.mark.parametrize("yaml_file", ["test_esp8266.yaml", "test_rp2040.yaml"])
@pytest.mark.parametrize(
"yaml_file",
[
"test_esp8266.yaml",
"test_rp2040.yaml",
# esp32.watchdog_timeout: 0s disables the task watchdog
"test_esp32_platform_disabled.yaml",
],
)
def test_other_platforms_leave_watchdog_unset(
component_config_path: Callable[[str], Path], yaml_file: str
) -> None:
CORE.config_path = component_config_path(yaml_file)
config = read_config({})
assert CONF_WATCHDOG_TIMEOUT not in config["http_request"]
def test_explicit_watchdog_rejected_when_platform_disabled(
component_config_path: Callable[[str], Path],
capsys: pytest.CaptureFixture[str],
) -> None:
CORE.config_path = component_config_path(
"test_esp32_platform_disabled_explicit.yaml"
)
assert read_config({}) is None
assert "Cannot be set when esp32.watchdog_timeout is 0s" in capsys.readouterr().out
@@ -0,0 +1,20 @@
esphome:
name: test
esp32:
board: esp32dev
framework:
type: esp-idf
network:
enable_ipv6: true
enable_ipv4: false
ethernet:
type: LAN8720
mdc_pin: 23
mdio_pin: 32
clk:
pin: 0
mode: CLK_EXT_IN
phy_addr: 0
@@ -0,0 +1,24 @@
esphome:
name: test
esp32:
board: esp32dev
framework:
type: esp-idf
network:
enable_ipv6: true
enable_ipv4: false
ethernet:
type: LAN8720
mdc_pin: 23
mdio_pin: 32
clk:
pin: 0
mode: CLK_EXT_IN
phy_addr: 0
manual_ip:
static_ip: 192.168.1.10
gateway: 192.168.1.1
subnet: 255.255.255.0
@@ -0,0 +1,15 @@
esphome:
name: test
esp32:
variant: esp32c6
framework:
type: esp-idf
wifi:
ssid: test
password: password1
openthread:
border_router:
tlv: 0e080000000000010000000300001035060004001fffe00208e227ac6a7f24052f0708fdb753eb517cb4d3051062b2442a928d9ea3b947a1618fc4085a030f4f70656e5468726561642d393837330102987304105330d857354330133c05e1fd7ae81a910c0402a0f7f8
@@ -0,0 +1,18 @@
esphome:
name: test
esp32:
variant: esp32c6
framework:
type: esp-idf
ethernet:
type: W5500
clk_pin: 6
mosi_pin: 7
miso_pin: 2
cs_pin: 10
interrupt_pin: 3
openthread:
tlv: 0e080000000000010000000300001035060004001fffe00208e227ac6a7f24052f0708fdb753eb517cb4d3051062b2442a928d9ea3b947a1618fc4085a030f4f70656e5468726561642d393837330102987304105330d857354330133c05e1fd7ae81a910c0402a0f7f8
Loaded 100 of 161 files, more files were not shown because too many files have changed in this diff. Show more