mirror of
https://github.com/esphome/esphome.git
synced 2026-11-17 18:46:08 +00:00
Compare commits
72
Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
9d6b50e947 | ||
|
|
e490785ad3 | ||
|
|
7f1ba24ab1 | ||
|
|
84dea8078b | ||
|
|
dbf6af2fd7 | ||
|
|
bc6fb7082f | ||
|
|
cfa27cea58 | ||
|
|
5c061f9e1e | ||
|
|
34b24d0cf5 | ||
|
|
e27614fda3 | ||
|
|
8426d04138 | ||
|
|
641c8bedc9 | ||
|
|
4a39ca1b71 | ||
|
|
4a218fdbd8 | ||
|
|
d8fc1cd214 | ||
|
|
1e030b7371 | ||
|
|
3b113f2c39 | ||
|
|
73468592a9 | ||
|
|
a1b456150d | ||
|
|
865a883abb | ||
|
|
357a6717c8 | ||
|
|
bcb434e05e | ||
|
|
fb65fcc5f6 | ||
|
|
b5e379a9d4 | ||
|
|
05286dfd5d | ||
|
|
36184f09aa | ||
|
|
449933891e | ||
|
|
a5bfa6dac3 | ||
|
|
5aaa5b0552 | ||
|
|
9ab090a818 | ||
|
|
02d0bb56a6 | ||
|
|
e12784543d | ||
|
|
bc3e9c88f4 | ||
|
|
7e3c2ebc10 | ||
|
|
3f6c0f35fe | ||
|
|
200bf69035 | ||
|
|
0054041d6a | ||
|
|
205318ee1a | ||
|
|
48d8df449a | ||
|
|
383ac1b41a | ||
|
|
0ac2299482 | ||
|
|
b58038af63 | ||
|
|
c741de7a47 | ||
|
|
8a5e07e29f | ||
|
|
f7b52be670 | ||
|
|
d5317669c9 | ||
|
|
5d0bd88bc6 | ||
|
|
b714d8f69b | ||
|
|
ecabdbb24b | ||
|
|
ffbc4148fb | ||
|
|
2169167908 | ||
|
|
900574c03a | ||
|
|
7b1f9c6bf1 | ||
|
|
3587e29725 | ||
|
|
d4244c5388 | ||
|
|
bec53baf1d | ||
|
|
98fd3786dd | ||
|
|
1f531ff022 | ||
|
|
65e7eb6b06 | ||
|
|
7538864e1e | ||
|
|
c0f97475d3 | ||
|
|
df6fcaa6fe | ||
|
|
eb1b793af9 | ||
|
|
b8ce904e0c | ||
|
|
6befa73b00 | ||
|
|
02211e15d8 | ||
|
|
dbbe02f423 | ||
|
|
97a1aa781e | ||
|
|
0b67a754cd | ||
|
|
320b6a8221 | ||
|
|
4e5a5ec32d | ||
|
|
09ce8d7d6e |
No files matched your search
@@ -158,7 +158,7 @@ recorder, dump saver and decoder, cover cycle driver), `online_image/`
|
||||
(host driver, Zephyr driver probe), `rtsp_server/` (protocol client, raw rate reader, measurement set,
|
||||
API image grab), `cse7766/` (API observer with ping), `zephyr_coredump/` (reader flow, decode with the
|
||||
Zephyr tools), `libretiny_prefs/` (rtc preference harness header), `libretiny_crash/` (BK72xx crash probe header,
|
||||
capture decoder, RTL8720C ROM dump versus report matcher), `api_outgoing/` (wake cycle and Wi-Fi drop timing readers), `ld2450/` (zone number driver, loopback harness driver with number and binary sensor states and TX frame summarizer, frame stream harness with presence hold and link loss drivers, BT MAC driver), `api/` (raw plaintext native API client for user defined actions, store_yaml recovery client over Noise or plaintext), `http_request/` (update manifest server, update state driver, request test server, action case driver, concurrency, reboot, update check and OTA drivers, `ota_auth/` OTA request cases with basic auth), `modem/` (button and sensor driver over the plaintext API, also used for the BLE MIDI boards, AT commands for the UART passthrough), `dfrobot_sen0395/` (API driver and case runner), `homeassistant_light/` (direct bulb driver, held-report case driver), `usb_audio/` (URL player), `speaker_media_player/` (API player driver, pipeline task case runner), `web_server/` (SSE client, request mix, SSE and HTTPD case runners, per-session `/events` listing check with its config, `/events` head, multi-client, stall, Wi-Fi drop and churn modes with their config, head comparer and runner), `mqtt/` (`ha_discovery/`, `disable/`, `retained_burst/` and `reconnect/` runners; broker config with TLS, client-certificate and WebSocket listeners, test CA with server and client certificates, transport case runner, flash-and-drive runner, CLI TLS check without a board, MAC suffix runner with per-variant configs for two boards, `shutdown/` shutdown message runner and matcher), `udp/` (datagram recorder, `udp.write` payload checker and case driver), `entity_icons/` (icon and device class readback over the API, MQTT discovery, web_server and dump_config, with a comparer), `uart_sim/` (API driver and flash, capture and drive runner for the simulated UART sensors), `remote_base/` (loopback runner and API log decoder per sent frame), `xiaomi_ble/` (frame and header generator with the expected counts, injecting, advertising and receiving configs, counter and runner), `ble_terminate/` (proxy and peer configs, connect and disconnect driver, peer disconnect matcher, runner), `i2c_slave/` (D1 mini slave and BK7238 master configs), `wpa3_ap/` (PMF AP with join and leave logging, BK7238 and CB3S DUTs, runner and summary), `wisp_ap/` (WISP upstream with AP buttons, WISP and ALWAYS_ON DUTs, client, runner and per phase summary), `wifi_restart/` (WPA3 mixed WISP upstream, static IP DUTs with syslog, runner and summary), `ota_timing/` (OTA upload timer with deflate, raw and TCP modes, API build check, A/B runner and median summary, `inflate_bench/` on-device inflate benchmark with stream generator, configs and syslog runner), `gpio_hold/` (ISR pin hold self test with ESP32 and S3 configs), `ble_event_inject/` (GAP and GATTC event injector header and proxy package), `light_sleep_wake/` (LEDC wave and edge timer for light sleep wakeup on a C3, external wave generator and per-call edge detail probe for peripheral power-down on the P4), `manual_ip/` (station and AP manual IP configs, DNS probe and runners), `web_handlers/` (late handler, web_server, captive_portal and prometheus variants, web OTA runner), `bk72xx_flash/` (flash readback with retries, per partition diff of two dumps), `ble_server_security/` (secure GATT server and pairing client configs, driver, summary and runner), `prometheus/` (datetime metrics configs and driver), `zephyr_ble_server/` (nRF52 server, looping ESP32 client and UF2 flash and count runner), `ble_adv_triggers/` (advertiser with six raw frames, ESP32 and BK72xx receivers with eleven triggers, runner and checker), `sun/` (longitude sweep package, runner and checker), `sensor_filters/` (every filter shape with a fixed input sequence, per-board packages and runner), `led_driver_sniff/` (SPI slave capture header with selftest, config generator, window comparer and runner), `hub_input_sim/` (keypad, wiegand and rdm6300 simulation on a D1 mini, API log runner), `value_range/` (on_value_range sequence with a restart, runner), `ezo_fake_bus/` (fake EZO I2C bus and step config, runner), `twai_self/` (TWAI self-reception header, on_frame config and counter runner), `ota_automations/` (OTA automation package for both OTA platforms, four case runner, repeated web upload runner and per case summary), `output_button/` (pad edge timer header, button package and runner), `homeassistant_button/` (raw plaintext API client for action calls and wizard inputs, package and runner), `uart_button/` (UART0 loopback package, API driver with TX and RX counts, runner), `interval/` (plain and delayed interval package, timing summary and runner), `smp_udp_ota/` (SMP reset fault injector, OTA runner, per mode result check, MCUboot swap limit, running image check and slot blanking UF2, Thread device config), `ld2420_sim/` (fake LD2420 answering the hub's commands on a UART0 loopback, API state recorder, two-build comparer and runneLine truncated
|
||||
capture decoder, RTL8720C ROM dump versus report matcher), `api_outgoing/` (wake cycle and Wi-Fi drop timing readers), `ld2450/` (zone number driver, loopback harness driver with number and binary sensor states and TX frame summarizer, frame stream harness with presence hold and link loss drivers, BT MAC driver), `api/` (raw plaintext native API client for user defined actions, store_yaml recovery client over Noise or plaintext), `http_request/` (update manifest server, update state driver, request test server, action case driver, concurrency, reboot, update check and OTA drivers, `ota_auth/` OTA request cases with basic auth), `modem/` (button and sensor driver over the plaintext API, also used for the BLE MIDI boards, AT commands for the UART passthrough), `dfrobot_sen0395/` (API driver and case runner), `homeassistant_light/` (direct bulb driver, held-report case driver), `usb_audio/` (URL player), `speaker_media_player/` (API player driver, pipeline task case runner), `web_server/` (SSE client, request mix, SSE and HTTPD case runners, per-session `/events` listing check with its config, `/events` head, multi-client, stall, Wi-Fi drop and churn modes with their config, head comparer and runner), `mqtt/` (`ha_discovery/`, `disable/`, `retained_burst/` and `reconnect/` runners; broker config with TLS, client-certificate and WebSocket listeners, test CA with server and client certificates, transport case runner, flash-and-drive runner, CLI TLS check without a board, MAC suffix runner with per-variant configs for two boards, `shutdown/` shutdown message runner and matcher), `udp/` (datagram recorder, `udp.write` payload checker and case driver), `entity_icons/` (icon and device class readback over the API, MQTT discovery, web_server and dump_config, with a comparer), `uart_sim/` (API driver and flash, capture and drive runner for the simulated UART sensors), `remote_base/` (loopback runner and API log decoder per sent frame), `xiaomi_ble/` (frame and header generator with the expected counts, injecting, advertising and receiving configs, counter and runner), `ble_terminate/` (proxy and peer configs, connect and disconnect driver, peer disconnect matcher, runner), `i2c_slave/` (D1 mini slave and BK7238 master configs), `wpa3_ap/` (PMF AP with join and leave logging, BK7238 and CB3S DUTs, runner and summary), `wisp_ap/` (WISP upstream with AP buttons, WISP and ALWAYS_ON DUTs, client, runner and per phase summary), `wifi_restart/` (WPA3 mixed WISP upstream, static IP DUTs with syslog, runner and summary), `ota_timing/` (OTA upload timer with deflate, raw and TCP modes, API build check, A/B runner and median summary, `inflate_bench/` on-device inflate benchmark with stream generator, configs and syslog runner), `gpio_hold/` (ISR pin hold self test with ESP32 and S3 configs), `ble_event_inject/` (GAP and GATTC event injector header and proxy package), `light_sleep_wake/` (LEDC wave and edge timer for light sleep wakeup on a C3, external wave generator and per-call edge detail probe for peripheral power-down on the P4), `manual_ip/` (station and AP manual IP configs, DNS probe and runners), `web_handlers/` (late handler, web_server, captive_portal and prometheus variants, web OTA runner), `bk72xx_flash/` (flash readback with retries, per partition diff of two dumps), `ble_server_security/` (secure GATT server and pairing client configs, driver, summary and runner), `prometheus/` (datetime metrics configs and driver), `zephyr_ble_server/` (nRF52 server, looping ESP32 client and UF2 flash and count runner), `ble_adv_triggers/` (advertiser with six raw frames, ESP32 and BK72xx receivers with eleven triggers, runner and checker), `sun/` (longitude sweep package, runner and checker), `sensor_filters/` (every filter shape with a fixed input sequence, per-board packages and runner), `led_driver_sniff/` (SPI slave capture header with selftest, config generator, window comparer and runner), `hub_input_sim/` (keypad, wiegand and rdm6300 simulation on a D1 mini, API log runner), `value_range/` (on_value_range sequence with a restart, runner), `ezo_fake_bus/` (fake EZO I2C bus and step config, runner), `twai_self/` (TWAI self-reception header, on_frame config and counter runner), `ota_automations/` (OTA automation package for both OTA platforms, four case runner, repeated web upload runner and per case summary), `output_button/` (pad edge timer header, button package and runner), `homeassistant_button/` (raw plaintext API client for action calls and wizard inputs, package and runner), `uart_button/` (UART0 loopback package, API driver with TX and RX counts, runner), `interval/` (plain and delayed interval package, timing summary and runner), `smp_udp_ota/` (SMP reset fault injector, OTA runner, per mode result check, MCUboot swap limit, running image check and slot blanking UF2, Thread device config), `ld2420_sim/` (fake LD2420 answering the hub's commands on a UART0 loopback, API state recorder, two-build comparer and runneLine truncated
|
||||
|
||||
## Templates in `$SKILL/templates`
|
||||
|
||||
|
||||
@@ -2,13 +2,13 @@
|
||||
|
||||
## Fake devices on an in-firmware I2C bus
|
||||
|
||||
When there is no chip and no wiring, subclass `i2c::I2CBus` in an include, implement `write_readv()` only, answer the
|
||||
registers the driver reads and check each write. Point the component at it from an `on_boot` lambda that runs before the
|
||||
component's `setup()`: `id(x)->set_i2c_bus(&fake::bus)` at a priority above the swapped component's own setup priority
|
||||
(a hub at BUS - 0.1 needs 999.95; 950 is ahead of hubs at IO, 900, and sensors at DATA, 600). Keep a real `i2c:` block
|
||||
in the config so validation passes. Bulk loops in a button lambda must call `App.feed_wdt()` and keep their
|
||||
per-iteration logs below INFO: 2000 DEBUG lines at 115200 baud trip the task watchdog, which is a fault of the harness
|
||||
and not of the PR. Sensor publishes log at VERBOSE, so log entity values from a lambda (`on_value` or an interval).
|
||||
When there is no chip and no wiring, subclass `i2c::I2CBus` in an include, implement `write_readv()` only, answer
|
||||
the registers the driver reads and check each write. Point the component at it from an `on_boot` lambda that runs
|
||||
before the component's `setup()`: `id(x)->set_i2c_bus(&fake::bus)` at priority 950, which is ahead of every I2C
|
||||
component (hubs set up at IO, 900, sensors at DATA, 600). The configs here that use 700 to 1000 work for their
|
||||
components; use 950 for a new one. Keep a real `i2c:` block in the config so validation passes. Bulk
|
||||
loops in a button lambda must call `App.feed_wdt()` and keep their per-iteration logs below INFO: 2000 DEBUG lines at
|
||||
115200 baud trip the task watchdog, which is a fault of the harness and not of the PR.
|
||||
|
||||
`templates/i2c_fake/` holds the fakes; each `*-common.yaml` or `*-test.yaml` is a classic ESP32 or ESP32-S3 config
|
||||
that includes its `.hpp` (add `esphome: name:` for the `-common` ones):
|
||||
@@ -151,42 +151,3 @@ Prefer this to glitching the wire. Holding SDA low by rerouting the pin (`esp_ro
|
||||
`SIG_GPIO_OUT_IDX` and back to `I2CEXT0_SDA_OUT_IDX`) wedges the ESP-IDF controller, which then needs
|
||||
`i2c_master_bus_reset()` after each glitch, and glitches timed onto writes left the VL53L0X answering 0x00 until a
|
||||
power cycle.
|
||||
|
||||
## WeiKai WK2168 on I2C (#22759)
|
||||
|
||||
`templates/i2c_fake/wkfake.hpp` + `wk-common.yaml`: answers 0x10 to 0x17 (`0 A1 A0 1 0 C1 C0 F`), keeps only A3..A0 of
|
||||
the register byte and builds the 6 bit address C1C0A3..A0, models the globals (a GRST write resets the channels in its
|
||||
low nibble and logs `!!!!! GRST WRITE`), per-channel SCR/LCR/FCR/baud/RFCNT/FSR and FIFOs (TX logged, RX injected with
|
||||
`inject_rx`, dropped when SCR is off), GPDIR/GPDAT with `set_input()`. It classifies each transaction W / P (1 byte
|
||||
pointer) / R / WR (repeated start); `fail_reads`, `fail_ptr` and `every_n` inject NACKs and the fake counts whether
|
||||
the next read is a retry of the same register. The driver polls RFCNT and FSR of every channel and GPDAT every loop:
|
||||
log only writes, changes and a `trace` window, never every read. `wk-map-check.cpp.example` is a host check with
|
||||
copies of the driver's `i2c_address()` (the same in both trees) and the PR's `i2c_register()`, comparing the address
|
||||
and register byte each tree sends for every register; paste the current bodies in before running it. On #22759 the
|
||||
base build's GPIO writes hit GRST and reset UART channel 0.
|
||||
|
||||
## BMI270 config upload and BMM150 aux (#22778)
|
||||
|
||||
`bmi270fake.hpp` + `bmi270-common.yaml` + `bmi270-motion-aux.yaml`: BMI270 register file with reset values; config
|
||||
upload port (INIT_ADDR word address, INIT_DATA into 8 KB) where INTERNAL_STATUS reads 0x01 only when every byte was
|
||||
written once and FNV-1a equals `expected_fnv` (default 0x95BB75B5, the current table, computed on the host from
|
||||
bmi270_config.h; the host harness takes an override in `BMI_EXPECTED_FNV`), else 0x02. Aux interface: AUX_DEV_ID selects
|
||||
the BMM150, manual mode turns AUX_WR_ADDR/AUX_RD_ADDR writes into BMM150 writes and 1/2/6/8 byte reads into AUX_X..,
|
||||
STATUS.aux_busy for two polls, auto mode mirrors the BMM150 data. Fault knobs: `bmm_mode` (absent, wrong chip id),
|
||||
`busy_stuck`, `stuck_from_aux_op` (busy stuck from the Nth aux transfer: 1 power-on, 2 soft reset, 3 chip id, 4 to 6
|
||||
trim, 7 normal mode), `nack_at` (main-bus transaction N). `norm_fnv` hashes register-level effects and leaves STATUS
|
||||
polls out (their count depends on timing). Heap is sampled at INIT_CTRL=0/1 and inside each INIT_DATA burst;
|
||||
`-DBMI_FAKE_WRAP_MALLOC` with `-Wl,--wrap=malloc` in `platformio_options: build_flags` counts malloc() during the upload
|
||||
through `alloc_count.hpp` (list it in `includes:` too; the link fails without the wrap, so a clean link proves it
|
||||
applied).
|
||||
- Host side: copy `bmi270-host-harness.cpp.example` (as a `.cpp`) and the fake into `tests/components/bmi270/` of both
|
||||
worktrees, plus a `benchmark.yaml` with `esphome: build_flags: [-DUSE_BMI270_BMM150, -DUSE_LOG_LISTENERS,
|
||||
-DESPHOME_LOG_MAX_LISTENERS=4]` (the unit test build does not use defines.h; this is how to get a component's
|
||||
optional code and log capture into it); the base also needs the PR's `__init__.py` manifest override.
|
||||
`bmi270-setters.py` turns each tree's generated main.cpp (`esphome compile --only-generate`) into the setter list
|
||||
the harness applies, so codegen changes (setters skipped at defaults) are tested as generated.
|
||||
`bmi270-host-run.sh.example TREE NAME SETTERS [32]` runs it (32 forces the Arduino burst branch);
|
||||
`bmi270-host-compare.py BASE.report PR.report` merges base's split INIT_ADDR writes, compares every config, fault and
|
||||
per-step scenario, maps the NACK sweep through the merged stream and lists every log difference.
|
||||
`bmm150-sweep.cpp.example` compares a decoder against a namespace-renamed copy of the base header bit for bit.
|
||||
- A planted one byte burst offset gave INTERNAL_STATUS 0x02 and "Config file upload failed" at both burst sizes.
|
||||
@@ -14,39 +14,3 @@ On the NiceMCU WB3S V1 board (BK7238), P26 and P24 are on the right-hand header.
|
||||
|
||||
Fake devices that need no real chip (in-firmware buses, the Pico 2 W target, the DS2482-800 simulator, NACK
|
||||
injection) are in [fake_i2c.md](fake_i2c.md).
|
||||
|
||||
## I2C wire A/B of a bus change (#22780, write_prefixed)
|
||||
|
||||
`scripts/i2c_wire_ab/`.
|
||||
- Host (`host/run.sh TREE OUT`, then `host/compare.py BASE PR`): copies a harness into the tree's
|
||||
`tests/components/i2c/` (removed again afterwards) and runs `script/cpp_unit_test.py i2c`. `extract.py` cuts the
|
||||
Arduino and ESP-IDF `write_readv`/`transfer_` bodies out of the tree and compiles them against the tree's real headers
|
||||
with stub `Wire.h` (esp8266 core 3.1.2 and arduino-pico 6.3.2 buffer rules, 128 and 256 bytes) and a stub
|
||||
`driver/i2c_master.h` (IDF 5.5.5 checks: at most SOC_I2C_CMD_REG_NUM jobs, NACK before STOP); the software bus is
|
||||
compiled whole with `#define InternalGPIOPin wab::FakePin` over a bit-level target. Every bus emits one token stream
|
||||
(`S 90+ 02+ ... P`, `Sr`, `rA0+`), with mallocs counted through macOS `malloc_logger` (ASan is dropped on macOS).
|
||||
Stub headers must live outside the test dir (a subfolder there is taken as a platform domain) and reach the build
|
||||
through a `benchmark.yaml` `esphome: build_flags: [-I...]`; `.h` files in the test dir are included into main.cpp, so
|
||||
generated bodies are `.inc`. Explicit-instantiation member robbers must be declared at global scope. The sources are
|
||||
stored as `.cpp.example`, `.h.example` and `.hpp` so the repo's C++ lints skip them; `run.sh` copies them back.
|
||||
- Device: `dev/snf.hpp` is a GPIO interrupt sniffer on the I2C pads (an ESP32-C2 with an ADS1015: SDA 4, SCL 5,
|
||||
`dev/c2common.yaml`) plus the same case sweep and a malloc counter; at 10 to 20 kHz on the C2 it still drops a bit in
|
||||
about 0.5 % of transactions (mostly at the chip's NACK), so capture three runs per build with
|
||||
`dev/flashrun.sh NAME YAML PORT SECONDS SUFFIX` (suffix empty, 2 and 3 write NAME-ser.txt, -ser2 and -ser3) and
|
||||
judge with `clean.py` (`--nacks` sorts each DIFF into a chip NACK or not) and `maj.py`, which also takes one or two
|
||||
captures. On the software bus it is exact. `dev/pdslave.hpp` with `snf.hpp` built with `-DSNF_PDSLAVE`
|
||||
(`pdcommon.yaml`) turn an ESP32-PICO-D4 into its own target at 0x48 on GPIO32/33 (ACKs every byte, so 300-byte
|
||||
writes reach the wire): exact on 6 of 6 captures at 25 kHz once one warm-up probe runs before the sweep (the ISR
|
||||
runs from flash; IRAM_ATTR on header inline functions fails to link with l32r literal errors). `mksw.py` turns a
|
||||
tree's LibreTiny-only software bus into a header so an ESP32 build can run it on real pins. The ADS1015 NACKs the
|
||||
third byte after the pointer, so long writes on that chip end there on every build.
|
||||
- `ard/swp.hpp` is the same sweep for ESP8266 (GPIO interrupt sniffer on GPIO4/5), RP2 (raw IO_IRQ_BANK0 handler) and
|
||||
BK72xx (no sniffer, so `ard/ardcmp.py --reads-by-data` compares reads by their data), with `ard/common.yaml` plus a
|
||||
board file (`d1.yaml`, `pico.yaml`, `bk.yaml`). Both sweeps share the decoder in `wiredecode.hpp` and count heap
|
||||
calls with `templates/i2c_fake/alloc_count.hpp`, which names the link flag for each platform. Each image checks
|
||||
itself with `ALLOC_PROBE 1`. `buildall.sh YAML...` builds the configs of either folder, each under the `name` of
|
||||
its substitutions (the file name without one), `x-pr.yaml` from PR_TREE and the rest from BASE_TREE.
|
||||
- Facts: on a D1 mini without a target, register writes up to 128 bytes total show `S 90- P`, longer ones fail with
|
||||
rc 6 and nothing on the wire; a write+read whose address is NACKed leaves no STOP (`S 90-`), dev too.
|
||||
arduino-pico: after a register write over 256 bytes, the next empty write (a probe) sends the stale buffer to the
|
||||
failed write's address.
|
||||
@@ -37,7 +37,3 @@ npm1300/*-cap.err
|
||||
npm1300/*-run.log
|
||||
rp2_sched_lock/*.txt
|
||||
rp2_sched_lock/*.log
|
||||
i2c_wire_ab/*/*-ser*.txt
|
||||
i2c_wire_ab/*/*-flash.log
|
||||
i2c_wire_ab/*/*-compile.log
|
||||
i2c_wire_ab/*/*-upload.log
|
||||
@@ -1,50 +0,0 @@
|
||||
"""ardcmp.py BASE_SER PR_SER [--reads-by-data]: per case rc and wire hash must match; alloc summary, rc counts and
|
||||
wire shapes. --reads-by-data compares read cases by the returned data instead (BK72xx has no sniffer, so its wire
|
||||
hash is that of an empty stream)."""
|
||||
|
||||
import argparse
|
||||
from collections import Counter
|
||||
import pathlib
|
||||
import re
|
||||
import sys
|
||||
|
||||
sys.path.insert(0, str(pathlib.Path(__file__).resolve().parent.parent))
|
||||
from snlog import first_alloc, is_read, load # noqa: E402
|
||||
|
||||
ap = argparse.ArgumentParser(description=__doc__.splitlines()[0])
|
||||
ap.add_argument("base")
|
||||
ap.add_argument("pr")
|
||||
ap.add_argument("--reads-by-data", action="store_true")
|
||||
args = ap.parse_args()
|
||||
b, p = load(args.base, "SW"), load(args.pr, "SW")
|
||||
print("cases", len(b), len(p))
|
||||
|
||||
|
||||
def key(label: str, case: dict) -> tuple:
|
||||
if args.reads_by_data and is_read(label):
|
||||
return case["rc"], case["rd"]
|
||||
return case["rc"], case["fnv"]
|
||||
|
||||
|
||||
diff = [k for k in b if k not in p or key(k, b[k]) != key(k, p[k])]
|
||||
print("rc/wire (or read data) differences:", len(diff))
|
||||
for k in diff[:20]:
|
||||
print(" ", k, "\n base", b[k], "\n pr ", p.get(k))
|
||||
for name, d in (("base", b), ("pr", p)):
|
||||
wr = [k for k in d if k.startswith("wreg")]
|
||||
allocs = {k: v["alloc"] for k, v in d.items()}
|
||||
first = {s: first_alloc(allocs, s) for s in ("wreg8", "wreg16")}
|
||||
print(
|
||||
f"{name}: write_register calls that allocated {sum(allocs[k] > 0 for k in wr)} of {len(wr)}; first "
|
||||
f"allocating len {first}; other allocs {[k for k in d if not k.startswith('wreg') and allocs[k]]}"
|
||||
)
|
||||
print(
|
||||
" rc counts:",
|
||||
dict(Counter(v["rc"] for v in d.values())),
|
||||
"| wire shapes:",
|
||||
dict(
|
||||
Counter(
|
||||
re.sub(r"[0-9A-F]{2}", "xx", v["wire"]) for v in d.values()
|
||||
).most_common(6)
|
||||
),
|
||||
)
|
||||
@@ -1,6 +0,0 @@
|
||||
bk72xx:
|
||||
board: xh-wb3s # set your board
|
||||
ads1115:
|
||||
- address: 0x48
|
||||
id: dev
|
||||
i2c_id: bus_hw
|
||||
@@ -1,15 +0,0 @@
|
||||
#!/bin/bash
|
||||
# bkrun.sh NAME PORT: flash NAME's UF2 to a BK72xx board (up to 3 tries), then capture 60 s from a reset (log on
|
||||
# UART1) and print the sweep line count, the ALLOC_PROBE line and the SWDONE line. env: PY, LTPY (LibreTiny python)
|
||||
D=$(cd "$(dirname "$0")" && pwd); S=$(dirname "$(dirname "$D")"); cd "$D" || exit 1
|
||||
N=${1:?name}; P=${2:?port}; PY=${PY:-python3}
|
||||
LTPY=${LTPY:-$D/../pio/penv/.libretiny/bin/python}
|
||||
[ -x "$LTPY" ] || LTPY=~/.platformio/penv/.libretiny/bin/python
|
||||
for i in 1 2 3; do
|
||||
timeout 200 "$LTPY" "$S/flash_bk72xx.py" flash write -d "$P" ".esphome/build/$N/.pioenvs/$N/firmware.uf2" > "$N-flash.log" 2>&1 &&
|
||||
break
|
||||
echo "flash try $i failed: $(tail -1 "$N-flash.log")"
|
||||
[ "$i" = 3 ] && exit 1
|
||||
done
|
||||
$PY "$S/serial_capture.py" "$P" 60 "$N-ser.txt" --reset > /dev/null 2>&1
|
||||
echo "$N $(grep -a -c 'SW ' "$N-ser.txt") $(grep -a -o 'ALLOC_PROBE.*' "$N-ser.txt") $(grep -a -o 'SWDONE.*' "$N-ser.txt")"
|
||||
@@ -1,47 +0,0 @@
|
||||
esphome:
|
||||
name: ${name}
|
||||
includes:
|
||||
- ../../../templates/i2c_fake/alloc_count.hpp
|
||||
- ../wiredecode.hpp
|
||||
- swp.hpp
|
||||
platformio_options:
|
||||
build_flags:
|
||||
- ${wrapflag}
|
||||
- ${defs}
|
||||
on_boot:
|
||||
- priority: -100
|
||||
then:
|
||||
- delay: ${start_delay}
|
||||
- lambda: |-
|
||||
swp::bus = id(bus_hw);
|
||||
swp::install();
|
||||
id(started) = true;
|
||||
|
||||
logger:
|
||||
level: DEBUG
|
||||
hardware_uart: ${loguart}
|
||||
|
||||
globals:
|
||||
- id: started
|
||||
type: bool
|
||||
initial_value: "false"
|
||||
- id: wedged
|
||||
type: bool
|
||||
initial_value: "false"
|
||||
|
||||
i2c:
|
||||
id: bus_hw
|
||||
sda: ${sda}
|
||||
scl: ${scl}
|
||||
frequency: ${freq}
|
||||
scan: true
|
||||
|
||||
interval:
|
||||
- interval: 20ms
|
||||
then:
|
||||
- lambda: |-
|
||||
if (!id(started)) return;
|
||||
if (!swp::step(id(dev)) && !id(wedged) && ${do_wedge}) {
|
||||
id(wedged) = true;
|
||||
swp::wedge(id(dev));
|
||||
}
|
||||
@@ -1,6 +0,0 @@
|
||||
esp8266:
|
||||
board: d1_mini
|
||||
ads1115:
|
||||
- address: 0x48
|
||||
id: dev
|
||||
i2c_id: bus_hw
|
||||
@@ -1,17 +0,0 @@
|
||||
rp2040:
|
||||
board: rpipico2w
|
||||
sensor:
|
||||
- platform: ina226
|
||||
id: dev
|
||||
i2c_id: bus_hw
|
||||
address: 0x40
|
||||
shunt_resistance: 0.1 ohm
|
||||
update_interval: 2s
|
||||
bus_voltage:
|
||||
name: vbus
|
||||
on_value:
|
||||
- lambda: ESP_LOGI("rd", "READ vbus %.4f", x);
|
||||
shunt_voltage:
|
||||
name: vshunt
|
||||
on_value:
|
||||
- lambda: ESP_LOGI("rd", "READ vshunt %.6f", x);
|
||||
@@ -1,10 +0,0 @@
|
||||
#!/bin/bash
|
||||
# picorun.sh NAME YAML PORT [OUTSUFFIX]: upload from TREE (reboots the Pico), wait for the port, capture 60 s and print
|
||||
# the sweep line count, the SWDONE line and the count of wedge cases. env: TREE, PY
|
||||
D=$(cd "$(dirname "$0")" && pwd); S=$(dirname "$(dirname "$D")"); cd "$D" || exit 1
|
||||
N=${1:?name}; Y=${2:?yaml}; P=${3:?port}; X=${4:-}; PY=${PY:-${TREE:?set TREE}/venv/bin/python}
|
||||
PIO_CORE=${PIO_CORE:-$D/../pio} bash "$S/pio_build.sh" upload "$N" "$Y" "$P" > /dev/null || exit 1
|
||||
$PY "$S/ports.py" --wait 2e8a > /dev/null 2>&1
|
||||
$PY "$S/serial_capture.py" "$P" 60 "$N-ser$X.txt" > /dev/null 2>&1
|
||||
echo "$N$X $(grep -a -c 'SW ' "$N-ser$X.txt") $(grep -a -o 'SWDONE.*' "$N-ser$X.txt")" \
|
||||
"$(grep -a -c 'WEDGE.*rc=6 then read rc=6' "$N-ser$X.txt")"
|
||||
@@ -1,235 +0,0 @@
|
||||
#pragma once
|
||||
// esphome#22780 sweep for the Arduino (ESP8266, RP2040) and software (BK72xx) buses: one "SW" line per I2CDevice call
|
||||
// with rc, mallocs during the call (alloc_count.hpp) and, where a sniffer exists (ESP8266, RP2), the decoded wire
|
||||
// stream and its hash.
|
||||
#include <cstring>
|
||||
#include <functional>
|
||||
#include <string>
|
||||
#include "esphome/core/application.h"
|
||||
#include "esphome/core/hal.h"
|
||||
#include "esphome/core/helpers.h"
|
||||
#include "esphome/core/log.h"
|
||||
#include "esphome/components/i2c/i2c.h"
|
||||
#include "alloc_count.hpp"
|
||||
#include "wiredecode.hpp"
|
||||
#ifdef USE_RP2
|
||||
#include <hardware/gpio.h>
|
||||
#include <hardware/irq.h>
|
||||
#include <hardware/structs/sio.h>
|
||||
#endif
|
||||
|
||||
#ifndef SWP_ADDR
|
||||
#define SWP_ADDR 0x48
|
||||
#endif
|
||||
#ifndef SWP_WREG
|
||||
#define SWP_WREG 0x02
|
||||
#endif
|
||||
#ifndef SWP_RREG
|
||||
#define SWP_RREG 0x01
|
||||
#endif
|
||||
#ifndef SWP_MAXLEN8
|
||||
#define SWP_MAXLEN8 300
|
||||
#endif
|
||||
#ifndef SWP_MAXLEN16
|
||||
#define SWP_MAXLEN16 300
|
||||
#endif
|
||||
#ifndef SWP_SDA
|
||||
#define SWP_SDA 4
|
||||
#endif
|
||||
#ifndef SWP_SCL
|
||||
#define SWP_SCL 5
|
||||
#endif
|
||||
|
||||
namespace swp {
|
||||
static const char *const TAG = "swp";
|
||||
|
||||
#if defined(USE_ESP8266) || defined(USE_RP2)
|
||||
#define SWP_SNIFF 1
|
||||
static constexpr uint32_t N = 12000;
|
||||
static uint8_t buf[N];
|
||||
static volatile uint32_t n = 0, ovf = 0;
|
||||
static volatile uint8_t last = 3;
|
||||
static volatile bool armed = false;
|
||||
static inline __attribute__((always_inline)) uint8_t levels() {
|
||||
#ifdef USE_ESP8266
|
||||
uint32_t v = GPI;
|
||||
#else
|
||||
uint32_t v = sio_hw->gpio_in;
|
||||
#endif
|
||||
return ((v >> SWP_SDA) & 1) | (((v >> SWP_SCL) & 1) << 1);
|
||||
}
|
||||
#ifdef USE_ESP8266
|
||||
static void IRAM_ATTR isr() {
|
||||
#else
|
||||
static void isr() {
|
||||
for (int p : {SWP_SDA, SWP_SCL}) {
|
||||
uint32_t ev = gpio_get_irq_event_mask(p);
|
||||
if (ev)
|
||||
gpio_acknowledge_irq(p, ev);
|
||||
}
|
||||
#endif
|
||||
uint8_t s = levels();
|
||||
if (!armed || s == last)
|
||||
return;
|
||||
last = s;
|
||||
if (n < N)
|
||||
buf[n++] = s;
|
||||
else
|
||||
ovf = ovf + 1;
|
||||
}
|
||||
static void install() {
|
||||
#ifdef USE_ESP8266
|
||||
attachInterrupt(SWP_SDA, isr, CHANGE);
|
||||
attachInterrupt(SWP_SCL, isr, CHANGE);
|
||||
#else
|
||||
gpio_add_raw_irq_handler_masked((1u << SWP_SDA) | (1u << SWP_SCL), isr);
|
||||
gpio_set_irq_enabled(SWP_SDA, GPIO_IRQ_EDGE_RISE | GPIO_IRQ_EDGE_FALL, true);
|
||||
gpio_set_irq_enabled(SWP_SCL, GPIO_IRQ_EDGE_RISE | GPIO_IRQ_EDGE_FALL, true);
|
||||
irq_set_enabled(IO_IRQ_BANK0, true);
|
||||
#endif
|
||||
ESP_LOGI(TAG, "sniffer on SDA %d SCL %d", SWP_SDA, SWP_SCL);
|
||||
}
|
||||
#else
|
||||
#define SWP_SNIFF 0
|
||||
static void install() {}
|
||||
#endif
|
||||
|
||||
static i2c::I2CBus *bus = nullptr;
|
||||
static uint8_t data[320];
|
||||
static uint8_t rdata[40];
|
||||
static uint32_t idx = 0, nalloc = 0;
|
||||
static bool done = false, probed = false;
|
||||
#if SWP_SNIFF
|
||||
static uint8_t start = 3; // levels when the sniffer was armed
|
||||
#endif
|
||||
|
||||
static void arm() {
|
||||
#if SWP_SNIFF
|
||||
n = 0;
|
||||
ovf = 0;
|
||||
start = levels();
|
||||
last = start;
|
||||
armed = true;
|
||||
#endif
|
||||
alloc_count::count = 0;
|
||||
alloc_count::window = true;
|
||||
}
|
||||
static void disarm() {
|
||||
alloc_count::window = false;
|
||||
#if SWP_SNIFF
|
||||
armed = false;
|
||||
#endif
|
||||
}
|
||||
static std::string sniffed(uint32_t &amb) {
|
||||
#if SWP_SNIFF
|
||||
return wiredecode::decode(buf, n, start, amb);
|
||||
#else
|
||||
return {};
|
||||
#endif
|
||||
}
|
||||
|
||||
static bool step(i2c::I2CDevice *dev) {
|
||||
if (done)
|
||||
return false;
|
||||
if (!probed) {
|
||||
// the counter must see a malloc inside the window
|
||||
alloc_count::count = 0;
|
||||
alloc_count::window = true;
|
||||
void *volatile p = malloc(100);
|
||||
alloc_count::window = false;
|
||||
free(p);
|
||||
ESP_LOGI(TAG, "ALLOC_PROBE %u", (unsigned) alloc_count::count);
|
||||
probed = true;
|
||||
}
|
||||
const uint32_t W8 = SWP_MAXLEN8 + 1, W16 = SWP_MAXLEN16 + 1;
|
||||
std::string label;
|
||||
uint32_t i = idx++;
|
||||
for (size_t k = 0; k < sizeof data; k++)
|
||||
data[k] = static_cast<uint8_t>(0x11 + i + k * 7);
|
||||
memset(rdata, 0, sizeof rdata);
|
||||
i2c::ErrorCode rc;
|
||||
if (i < W8) {
|
||||
label = "wreg8 len=" + std::to_string(i);
|
||||
arm();
|
||||
rc = dev->write_register(SWP_WREG, data, i);
|
||||
disarm();
|
||||
} else if (i < W8 + W16) {
|
||||
size_t len = i - W8;
|
||||
label = "wreg16 len=" + std::to_string(len);
|
||||
arm();
|
||||
rc = dev->write_register16((SWP_WREG << 8) | 0x03, data, len);
|
||||
disarm();
|
||||
} else if (i < W8 + W16 + 10) {
|
||||
static const size_t ns[] = {1, 2, 3, 4, 8};
|
||||
uint32_t j = i - W8 - W16;
|
||||
size_t k = ns[j % 5];
|
||||
bool sixteen = j >= 5;
|
||||
label = std::string(sixteen ? "rreg16" : "rreg8") + " n=" + std::to_string(k);
|
||||
arm();
|
||||
rc = sixteen ? dev->read_register16(SWP_RREG << 8, rdata, k) : dev->read_register(SWP_RREG, rdata, k);
|
||||
disarm();
|
||||
} else if (i < W8 + W16 + 13) {
|
||||
size_t k = i - (W8 + W16 + 10) + 1;
|
||||
label = "read n=" + std::to_string(k);
|
||||
arm();
|
||||
rc = dev->read(rdata, k);
|
||||
disarm();
|
||||
} else if (i < W8 + W16 + 16) {
|
||||
static const uint8_t p1[] = {SWP_RREG};
|
||||
size_t k = i - (W8 + W16 + 13) + 1;
|
||||
label = "write1+read n=" + std::to_string(k);
|
||||
arm();
|
||||
rc = dev->write_read(p1, 1, rdata, k);
|
||||
disarm();
|
||||
} else if (i < W8 + W16 + 16 + 112) {
|
||||
uint8_t a = 8 + (i - (W8 + W16 + 16));
|
||||
label = "probe " + std::to_string(a);
|
||||
arm();
|
||||
rc = bus->write_readv(a, nullptr, 0, nullptr, 0);
|
||||
disarm();
|
||||
} else {
|
||||
done = true;
|
||||
ESP_LOGI(TAG, "SWDONE cases=%u alloc_calls=%u", (unsigned) idx - 1, (unsigned) nalloc);
|
||||
return false;
|
||||
}
|
||||
nalloc += alloc_count::count != 0;
|
||||
uint32_t amb = 0, events = 0, overflow = 0;
|
||||
std::string got = sniffed(amb);
|
||||
#if SWP_SNIFF
|
||||
events = n;
|
||||
overflow = ovf;
|
||||
#endif
|
||||
char rd[3 * 8 + 1] = "";
|
||||
for (int k = 0; k < 8 && label.rfind("wreg", 0) != 0 && label.rfind("probe", 0) != 0; k++)
|
||||
snprintf(rd + 3 * k, 4, "%02X ", rdata[k]);
|
||||
ESP_LOGI(TAG, "SW %s|rc=%d|alloc=%u|ev=%u|amb=%u|ovf=%u|fnv=%08X|%s|rd=%s", label.c_str(), (int) rc,
|
||||
(unsigned) alloc_count::count, (unsigned) events, (unsigned) amb, (unsigned) overflow,
|
||||
(unsigned) fnv1a_hash(got), got.substr(0, 90).c_str(), rd);
|
||||
App.feed_wdt();
|
||||
return true;
|
||||
}
|
||||
|
||||
// Over-long register write and what the bus does afterwards (on arduino-pico this leaves Wire stuck, dev too).
|
||||
// Each step is sniffed; the probe step shows whether a stale buffer goes out.
|
||||
static void wedge_step(const char *what, const std::function<i2c::ErrorCode()> &fn) {
|
||||
uint32_t amb = 0;
|
||||
arm();
|
||||
auto rc = fn();
|
||||
disarm();
|
||||
std::string got = sniffed(amb);
|
||||
ESP_LOGI(TAG, "WEDGE %s rc=%d ev=%u wire=%s", what, (int) rc, (unsigned) got.size(), got.substr(0, 120).c_str());
|
||||
App.feed_wdt();
|
||||
}
|
||||
static void wedge(i2c::I2CDevice *dev) {
|
||||
static uint8_t r[2];
|
||||
wedge_step("read_register before", [dev] { return dev->read_register(SWP_RREG, r, 2); });
|
||||
wedge_step("plain read before", [dev] { return dev->read(r, 1); });
|
||||
wedge_step("write_register len=300", [dev] { return dev->write_register(SWP_WREG, data, 300); });
|
||||
wedge_step("read_register after", [dev] { return dev->read_register(SWP_RREG, r, 2); });
|
||||
wedge_step("plain read after", [dev] { return dev->read(r, 1); });
|
||||
wedge_step("write_register len=2 after", [dev] { return dev->write_register(SWP_WREG, data, 2); });
|
||||
wedge_step("probe 0x50 after", [] { return bus->write_readv(0x50, nullptr, 0, nullptr, 0); });
|
||||
wedge_step("plain read after probe", [dev] { return dev->read(r, 1); });
|
||||
wedge_step("write_register len=2 after probe", [dev] { return dev->write_register(SWP_WREG, data, 2); });
|
||||
}
|
||||
} // namespace swp
|
||||
@@ -1,14 +0,0 @@
|
||||
#!/bin/bash
|
||||
# buildall.sh YAML...: compile each config under the `name:` of its substitutions (the file name when it sets none),
|
||||
# from the tree named by its suffix (`x-pr.yaml` builds from PR_TREE, any other from BASE_TREE).
|
||||
# env: PR_TREE, BASE_TREE, PY, BUILD_LOCK (optional), PIO_CORE (default i2c_wire_ab/pio)
|
||||
# Stops at the first failed build and exits with its status.
|
||||
set -o pipefail
|
||||
D=$(cd "$(dirname "$0")" && pwd); S=$(dirname "$D")
|
||||
for y in "$@"; do
|
||||
b=$(basename "$y" .yaml)
|
||||
case ${b##*-} in pr) T=${PR_TREE:?set PR_TREE} ;; *) T=${BASE_TREE:?set BASE_TREE} ;; esac
|
||||
nm=$(awk '/^substitutions:/{s=1; next} /^[^ #]/{s=0} s && /^ name:/{gsub(/["\x27]/, "", $2); print $2; exit}' "$y")
|
||||
nm=${nm:-$b}
|
||||
TREE=$T PIO_CORE=${PIO_CORE:-$D/pio} bash "$S/pio_build.sh" compile "$nm" "$y" | sed "s/^/$nm /" || exit
|
||||
done
|
||||
@@ -1,83 +0,0 @@
|
||||
esphome:
|
||||
name: ${name}
|
||||
includes:
|
||||
- ${swfile}
|
||||
- ../../../templates/i2c_fake/alloc_count.hpp
|
||||
- ../wiredecode.hpp
|
||||
- snf.hpp
|
||||
platformio_options:
|
||||
build_flags:
|
||||
- -Wl,--wrap=malloc
|
||||
- ${flags}
|
||||
on_boot:
|
||||
- priority: 700
|
||||
then:
|
||||
- lambda: |-
|
||||
gpio_reset_pin(GPIO_NUM_6); gpio_set_direction(GPIO_NUM_6, GPIO_MODE_OUTPUT); gpio_set_level(GPIO_NUM_6, 1);
|
||||
gpio_reset_pin(GPIO_NUM_7); gpio_set_direction(GPIO_NUM_7, GPIO_MODE_OUTPUT); gpio_set_level(GPIO_NUM_7, 0);
|
||||
#ifdef SNF_SW
|
||||
snf::bus = snf::make_sw();
|
||||
id(ads)->set_i2c_bus(snf::bus);
|
||||
#else
|
||||
snf::bus = id(bus_hw);
|
||||
#endif
|
||||
- priority: -100
|
||||
then:
|
||||
- delay: 6s
|
||||
- lambda: |-
|
||||
snf::install();
|
||||
id(started) = true;
|
||||
|
||||
esp32:
|
||||
board: esp32-c2-devkitm-1
|
||||
framework:
|
||||
type: esp-idf
|
||||
sdkconfig_options:
|
||||
CONFIG_XTAL_FREQ_26: y
|
||||
|
||||
logger:
|
||||
level: DEBUG
|
||||
baud_rate: 115200
|
||||
|
||||
globals:
|
||||
- id: started
|
||||
type: bool
|
||||
initial_value: "false"
|
||||
|
||||
i2c:
|
||||
id: bus_hw
|
||||
sda: ${hw_sda}
|
||||
scl: ${hw_scl}
|
||||
frequency: ${freq}
|
||||
scan: true
|
||||
|
||||
ads1115:
|
||||
- address: 0x48
|
||||
id: ads
|
||||
i2c_id: bus_hw
|
||||
|
||||
sensor:
|
||||
- platform: ads1115
|
||||
ads1115_id: ads
|
||||
multiplexer: A2_GND
|
||||
gain: 6.144
|
||||
resolution: 12_BITS
|
||||
name: a2
|
||||
update_interval: 2s
|
||||
on_value:
|
||||
- lambda: ESP_LOGI("rd", "READ a2 %.4f", x);
|
||||
- platform: ads1115
|
||||
ads1115_id: ads
|
||||
multiplexer: A3_GND
|
||||
gain: 6.144
|
||||
resolution: 12_BITS
|
||||
name: a3
|
||||
update_interval: 2s
|
||||
on_value:
|
||||
- lambda: ESP_LOGI("rd", "READ a3 %.4f", x);
|
||||
|
||||
interval:
|
||||
- interval: 20ms
|
||||
then:
|
||||
- lambda: |-
|
||||
if (id(started)) snf::step(id(ads));
|
||||
@@ -1,69 +0,0 @@
|
||||
"""clean.py [--nacks] NAME...: per build, cases with at least one capture (NAME-ser.txt, -ser2, -ser3) that is exactly
|
||||
the expected stream or the expected stream cut by the chip's NACK; also every rc seen per case.
|
||||
--nacks: per capture, count the DIFF cases that are a chip NACK and list the others."""
|
||||
|
||||
import argparse
|
||||
from collections import Counter, defaultdict
|
||||
import pathlib
|
||||
import sys
|
||||
|
||||
sys.path.insert(0, str(pathlib.Path(__file__).resolve().parent.parent))
|
||||
from snlog import CAPTURES, load, nack_cut, runs # noqa: E402
|
||||
|
||||
|
||||
def clean(case: dict) -> bool:
|
||||
if case["verdict"] == "OK":
|
||||
return True
|
||||
return nack_cut(case.get("got", []), case.get("exp", [])) is not None
|
||||
|
||||
|
||||
def nacks(name: str) -> None:
|
||||
for s in CAPTURES:
|
||||
path = pathlib.Path(f"{name}-ser{s}.txt")
|
||||
if not path.exists():
|
||||
continue
|
||||
c: Counter[str] = Counter()
|
||||
other = []
|
||||
for label, case in load(path).items():
|
||||
if case["verdict"] == "OK":
|
||||
continue
|
||||
got, exp = case.get("got", []), case.get("exp", [])
|
||||
if (j := nack_cut(got, exp)) is not None:
|
||||
c[f"chip NACK at token {j}"] += 1
|
||||
else:
|
||||
other.append((label, " ".join(got)[:80], " ".join(exp)[:80]))
|
||||
print(path, dict(c), "other:", len(other))
|
||||
for o in other[:10]:
|
||||
print(" ", o)
|
||||
|
||||
|
||||
ap = argparse.ArgumentParser(description=__doc__.splitlines()[0])
|
||||
ap.add_argument(
|
||||
"--nacks", action="store_true", help="classify the DIFF cases of each capture"
|
||||
)
|
||||
ap.add_argument("names", nargs="+")
|
||||
args = ap.parse_args()
|
||||
if args.nacks:
|
||||
for name in args.names:
|
||||
nacks(name)
|
||||
sys.exit()
|
||||
allrc = defaultdict(set)
|
||||
for name in args.names:
|
||||
caps = runs(name)
|
||||
bad = [k for k in caps[0] if not any(k in r and clean(r[k]) for r in caps)]
|
||||
for r in caps:
|
||||
for k, case in r.items():
|
||||
allrc[k].add(case["rc"])
|
||||
print(
|
||||
name,
|
||||
"captures",
|
||||
len(caps),
|
||||
"cases",
|
||||
len(caps[0]),
|
||||
"with no clean capture:",
|
||||
bad,
|
||||
)
|
||||
print(
|
||||
"cases whose rc differs between any captures/builds:",
|
||||
[k for k, s in allrc.items() if len(s) > 1],
|
||||
)
|
||||
@@ -1,10 +0,0 @@
|
||||
#!/bin/bash
|
||||
# flashrun.sh NAME YAML PORT SECONDS [OUTSUFFIX]: upload from TREE, then capture from a reset into NAME-serOUTSUFFIX.txt
|
||||
# and print the sniffed line count and the SNDONE line. clean.py and maj.py read NAME-ser.txt, -ser2 and -ser3.
|
||||
# env: TREE, PY
|
||||
D=$(cd "$(dirname "$0")" && pwd); S=$(dirname "$(dirname "$D")"); cd "$D" || exit 1
|
||||
N=${1:?name}; Y=${2:?yaml}; P=${3:?port}; SEC=${4:?seconds}; X=${5:-}; PY=${PY:-${TREE:?set TREE}/venv/bin/python}
|
||||
pkill -f "[s]erial_capture.py $P "; sleep 1
|
||||
PIO_CORE=${PIO_CORE:-$D/../pio} bash "$S/pio_build.sh" upload "$N" "$Y" "$P" || exit 1
|
||||
$PY "$S/serial_capture.py" "$P" "$SEC" "$N-ser$X.txt" --reset > /dev/null 2>&1
|
||||
grep -a -c "SN " "$N-ser$X.txt"; grep -a "SNDONE" "$N-ser$X.txt"
|
||||
@@ -1,57 +0,0 @@
|
||||
"""maj.py NAME_BASE NAME_PR: per case majority of rc and wire hash (reads: rc and verdict, since the chip's registers
|
||||
change between runs) over NAME-ser.txt, -ser2 and -ser3 (1 to 3 captures), then base vs PR and the alloc summary."""
|
||||
|
||||
import argparse
|
||||
from collections import Counter
|
||||
import pathlib
|
||||
import sys
|
||||
|
||||
sys.path.insert(0, str(pathlib.Path(__file__).resolve().parent.parent))
|
||||
from snlog import first_alloc, is_read, runs # noqa: E402
|
||||
|
||||
|
||||
def majority(name: str) -> tuple[dict, int, int]:
|
||||
"""Case -> (majority key, votes, max alloc); also the count of unstable cases and of captures."""
|
||||
caps = runs(name)
|
||||
out, unstable = {}, 0
|
||||
for k in caps[0]:
|
||||
c = Counter(
|
||||
(r[k]["rc"], r[k]["verdict"] if is_read(k) else r[k]["fnv"])
|
||||
for r in caps
|
||||
if k in r
|
||||
)
|
||||
v, n = c.most_common(1)[0]
|
||||
unstable += len(c) > 1
|
||||
out[k] = (v, n, max(r[k]["alloc"] for r in caps if k in r))
|
||||
return out, unstable, len(caps)
|
||||
|
||||
|
||||
ap = argparse.ArgumentParser(description=__doc__.splitlines()[0])
|
||||
ap.add_argument("base")
|
||||
ap.add_argument("pr")
|
||||
args = ap.parse_args()
|
||||
b, ub, nb = majority(args.base)
|
||||
p, up, np_ = majority(args.pr)
|
||||
print(
|
||||
f"cases {len(b)} {len(p)}; captures base {nb}, pr {np_}; cases whose captures disagree: base {ub}, pr {up}"
|
||||
)
|
||||
d = [k for k in b if k not in p or b[k][0] != p[k][0]]
|
||||
print("base vs pr majority differences:", len(d))
|
||||
for k in d[:30]:
|
||||
print(" ", k, "base", b[k][0], "pr", p[k][0] if k in p else "missing")
|
||||
weak = [k for k in b if 2 * b[k][1] <= nb or (k in p and 2 * p[k][1] <= np_)]
|
||||
print("cases without a majority:", weak)
|
||||
for name, maj in (("base", b), ("pr", p)):
|
||||
wr = [k for k in maj if k.startswith("wreg")]
|
||||
allocs = {k: v[2] for k, v in maj.items()}
|
||||
print(
|
||||
name,
|
||||
"write_register calls that allocated:",
|
||||
sum(allocs[k] > 0 for k in wr),
|
||||
"of",
|
||||
len(wr),
|
||||
"| first allocating len:",
|
||||
{s: first_alloc(allocs, s) for s in ("wreg8", "wreg16")},
|
||||
"| other allocs:",
|
||||
[k for k in maj if not k.startswith("wreg") and allocs[k]],
|
||||
)
|
||||
@@ -1,21 +0,0 @@
|
||||
"""mksw.py TREE OUT: the tree's software bus (header + source) as one header for an ESP32 build."""
|
||||
|
||||
from pathlib import Path
|
||||
import sys
|
||||
|
||||
t = Path(sys.argv[1]) / "esphome/components/i2c"
|
||||
h = (
|
||||
(t / "i2c_bus_software.h")
|
||||
.read_text()
|
||||
.replace("#pragma once", "")
|
||||
.replace('#include "i2c_bus.h"', '#include "esphome/components/i2c/i2c_bus.h"')
|
||||
)
|
||||
c = (
|
||||
(t / "i2c_bus_software.cpp")
|
||||
.read_text()
|
||||
.replace('#include "i2c_bus_software.h"', "")
|
||||
)
|
||||
Path(sys.argv[2]).write_text(
|
||||
"#pragma once\n#define USE_LIBRETINY\n" + h + "\n" + c + "\n#undef USE_LIBRETINY\n",
|
||||
encoding="utf-8",
|
||||
)
|
||||
@@ -1,53 +0,0 @@
|
||||
esphome:
|
||||
name: ${name}
|
||||
includes:
|
||||
- pdslave.hpp
|
||||
- ../../../templates/i2c_fake/alloc_count.hpp
|
||||
- ../wiredecode.hpp
|
||||
- snf.hpp
|
||||
platformio_options:
|
||||
build_flags:
|
||||
- -Wl,--wrap=malloc
|
||||
- -DSNF_PDSLAVE
|
||||
on_boot:
|
||||
- priority: 700
|
||||
then:
|
||||
- lambda: |-
|
||||
pds::install();
|
||||
snf::bus = id(bus_hw);
|
||||
- priority: -100
|
||||
then:
|
||||
- delay: 4s
|
||||
- lambda: id(started) = true;
|
||||
|
||||
esp32:
|
||||
board: pico32
|
||||
framework:
|
||||
type: esp-idf
|
||||
|
||||
logger:
|
||||
level: DEBUG
|
||||
baud_rate: 115200
|
||||
|
||||
globals:
|
||||
- id: started
|
||||
type: bool
|
||||
initial_value: "false"
|
||||
|
||||
i2c:
|
||||
id: bus_hw
|
||||
sda: GPIO32
|
||||
scl: GPIO33
|
||||
frequency: ${freq}
|
||||
scan: false
|
||||
|
||||
ads1115:
|
||||
- address: 0x48
|
||||
id: ads
|
||||
i2c_id: bus_hw
|
||||
|
||||
interval:
|
||||
- interval: 20ms
|
||||
then:
|
||||
- lambda: |-
|
||||
if (id(started)) snf::step(id(ads));
|
||||
@@ -1,187 +0,0 @@
|
||||
#pragma once
|
||||
// Software I2C target at 0x48 on the classic ESP32 master's own pads (GPIO32 SDA, GPIO33 SCL), after the
|
||||
// `i2c_isr_fault/inasim.hpp` pattern: a GPIO ISR on core 0 spins through each transaction and pulls a line low by
|
||||
// switching the pad's output mux to GPIO output 0. ACKs every byte written to 0x48, answers reads with A0, A1, ...;
|
||||
// NACKs other addresses. Every START, byte (with the ACK bit seen on the wire) and STOP goes into a log.
|
||||
#include <cstdint>
|
||||
#include <string>
|
||||
#include "esphome/core/log.h"
|
||||
#include "driver/gpio.h"
|
||||
#include "soc/gpio_reg.h"
|
||||
#include "soc/gpio_sig_map.h"
|
||||
#include "esp_cpu.h"
|
||||
#include "freertos/FreeRTOS.h"
|
||||
#include "freertos/task.h"
|
||||
|
||||
namespace pds {
|
||||
static const char *const TAG = "pds";
|
||||
constexpr int SDA = 32, SCL = 33;
|
||||
constexpr uint32_t SDA_BIT = 1u << (SDA - 32), SCL_BIT = 1u << (SCL - 32);
|
||||
constexpr uint32_t LOW_VAL = SIG_GPIO_OUT_IDX | GPIO_FUNC0_OEN_SEL;
|
||||
constexpr uint32_t CPU_MHZ = 240;
|
||||
inline volatile uint32_t ®(uint32_t a) { return *reinterpret_cast<volatile uint32_t *>(a); }
|
||||
inline uint32_t out_sel_addr(int pin) { return GPIO_FUNC0_OUT_SEL_CFG_REG + 4 * pin; }
|
||||
inline uint32_t in() { return reg(GPIO_IN1_REG); }
|
||||
inline uint32_t rel_sda = 0;
|
||||
|
||||
// log codes: 1 START, 2 RESTART, 3 STOP, 4 written byte / 6 read byte (next: value, ack), 5 idle abort
|
||||
constexpr size_t LOGN = 4096;
|
||||
inline DRAM_ATTR uint8_t lg[LOGN];
|
||||
inline volatile uint32_t ln = 0, lovf = 0;
|
||||
inline void put(uint8_t v) {
|
||||
if (ln < LOGN)
|
||||
lg[ln++] = v;
|
||||
else
|
||||
lovf = lovf + 1;
|
||||
}
|
||||
|
||||
inline void sda_low(bool low) { reg(out_sel_addr(SDA)) = low ? LOW_VAL : rel_sda; }
|
||||
|
||||
inline void run_transaction() {
|
||||
enum { ADDR, WDATA, RDATA, IGNORE } phase = ADDR;
|
||||
uint32_t prev = in(), last = esp_cpu_get_cycle_count();
|
||||
uint8_t byte = 0, cnt = 0, out = 0, rd = 0, pend_kind = 4, pend_val = 0;
|
||||
bool acked = false;
|
||||
put(1);
|
||||
reg(GPIO_OUT1_W1TC_REG) = SDA_BIT;
|
||||
reg(GPIO_ENABLE1_W1TS_REG) = SDA_BIT;
|
||||
for (;;) {
|
||||
uint32_t v = in();
|
||||
uint32_t now = esp_cpu_get_cycle_count();
|
||||
if (v == prev) {
|
||||
if (now - last > 20000u * CPU_MHZ) {
|
||||
put(5);
|
||||
break;
|
||||
}
|
||||
continue;
|
||||
}
|
||||
last = now;
|
||||
uint32_t ch = v ^ prev;
|
||||
prev = v;
|
||||
if (ch & SCL_BIT) {
|
||||
if (v & SCL_BIT) {
|
||||
cnt++;
|
||||
if (cnt <= 8 && phase != RDATA)
|
||||
byte = (byte << 1) | ((v & SDA_BIT) ? 1 : 0);
|
||||
else if (cnt == 9) {
|
||||
// the ACK bit as it is on the wire
|
||||
bool ack = !(v & SDA_BIT);
|
||||
put(pend_kind);
|
||||
put(pend_val);
|
||||
put(ack ? 1 : 0);
|
||||
if (phase == RDATA && !ack)
|
||||
phase = IGNORE;
|
||||
}
|
||||
} else {
|
||||
if (cnt == 8) {
|
||||
pend_kind = phase == RDATA ? 6 : 4;
|
||||
pend_val = phase == RDATA ? out : byte;
|
||||
if (phase == ADDR) {
|
||||
if ((byte >> 1) == 0x48) {
|
||||
sda_low(true);
|
||||
acked = true;
|
||||
phase = (byte & 1) ? RDATA : WDATA;
|
||||
rd = 0;
|
||||
} else {
|
||||
phase = IGNORE;
|
||||
}
|
||||
} else if (phase == WDATA) {
|
||||
sda_low(true);
|
||||
acked = true;
|
||||
} else if (phase == RDATA) {
|
||||
sda_low(false); // master's ACK slot
|
||||
}
|
||||
} else if (cnt == 9) {
|
||||
if (acked) {
|
||||
sda_low(false);
|
||||
acked = false;
|
||||
}
|
||||
if (phase == RDATA) {
|
||||
out = 0xA0 + rd++;
|
||||
sda_low(!(out & 0x80));
|
||||
}
|
||||
cnt = 0;
|
||||
byte = 0;
|
||||
} else if (cnt >= 1 && cnt <= 7 && phase == RDATA) {
|
||||
sda_low(!(out & (0x80 >> cnt)));
|
||||
}
|
||||
}
|
||||
} else if ((ch & SDA_BIT) && (v & SCL_BIT)) {
|
||||
if (v & SDA_BIT) {
|
||||
put(3);
|
||||
break;
|
||||
}
|
||||
put(2);
|
||||
sda_low(false);
|
||||
phase = ADDR;
|
||||
cnt = 0;
|
||||
byte = 0;
|
||||
acked = false;
|
||||
}
|
||||
}
|
||||
sda_low(false);
|
||||
}
|
||||
|
||||
inline void slave_isr(void *) {
|
||||
reg(GPIO_STATUS1_W1TC_REG) = SDA_BIT;
|
||||
uint32_t v = in();
|
||||
if (!(v & SCL_BIT) || (v & SDA_BIT))
|
||||
return;
|
||||
run_transaction();
|
||||
reg(GPIO_STATUS1_W1TC_REG) = SDA_BIT;
|
||||
}
|
||||
|
||||
inline volatile bool installed = false;
|
||||
inline void install_task(void *) {
|
||||
rel_sda = reg(out_sel_addr(SDA));
|
||||
gpio_install_isr_service(0);
|
||||
gpio_set_intr_type((gpio_num_t) SDA, GPIO_INTR_NEGEDGE);
|
||||
esp_err_t e = gpio_isr_handler_add((gpio_num_t) SDA, slave_isr, nullptr);
|
||||
gpio_intr_enable((gpio_num_t) SDA);
|
||||
ESP_LOGI(TAG, "slave on core %d handler %d rel_sda 0x%x", xPortGetCoreID(), e, (unsigned) rel_sda);
|
||||
installed = true;
|
||||
vTaskDelete(nullptr);
|
||||
}
|
||||
inline void install() {
|
||||
xTaskCreatePinnedToCore(install_task, "pdsinst", 3072, nullptr, 5, nullptr, 0);
|
||||
while (!installed)
|
||||
vTaskDelay(1);
|
||||
}
|
||||
|
||||
inline std::string tokens() {
|
||||
std::string s;
|
||||
char t[8];
|
||||
for (uint32_t i = 0; i < ln; i++) {
|
||||
switch (lg[i]) {
|
||||
case 1:
|
||||
s += "S ";
|
||||
break;
|
||||
case 2:
|
||||
s += "Sr ";
|
||||
break;
|
||||
case 3:
|
||||
s += "P ";
|
||||
break;
|
||||
case 5:
|
||||
s += "IDLE ";
|
||||
break;
|
||||
case 4:
|
||||
case 6: {
|
||||
if (i + 2 >= ln) { // byte record cut off by a full log
|
||||
s += "? ";
|
||||
i = ln;
|
||||
break;
|
||||
}
|
||||
uint8_t b = lg[i + 1], a = lg[i + 2];
|
||||
snprintf(t, sizeof t, lg[i] == 6 ? "r%02X%c " : "%02X%c ", b, a ? '+' : '-');
|
||||
i += 2;
|
||||
s += t;
|
||||
break;
|
||||
}
|
||||
default:
|
||||
s += "? ";
|
||||
}
|
||||
}
|
||||
return s;
|
||||
}
|
||||
} // namespace pds
|
||||
@@ -1,245 +0,0 @@
|
||||
#pragma once
|
||||
// esphome#22780 device check: GPIO interrupt sniffer on the I2C pads, a sweep of I2CDevice call shapes, and a
|
||||
// malloc counter (alloc_count.hpp, -Wl,--wrap=malloc). One "SN" line per case: rc, mallocs, decoded wire stream
|
||||
// hash, match with the stream the call should produce.
|
||||
#include <cstring>
|
||||
#include <string>
|
||||
#include <driver/gpio.h>
|
||||
#include <soc/gpio_reg.h>
|
||||
#include <esp_attr.h>
|
||||
#include "esphome/core/application.h"
|
||||
#include "esphome/core/helpers.h"
|
||||
#include "esphome/core/log.h"
|
||||
#include "esphome/components/i2c/i2c.h"
|
||||
#include "alloc_count.hpp"
|
||||
#include "wiredecode.hpp"
|
||||
#ifdef SNF_PDSLAVE
|
||||
// the board is its own target (pdslave.hpp); its token stream replaces the pad sniffer's decode
|
||||
#include "pdslave.hpp"
|
||||
#endif
|
||||
|
||||
namespace snf {
|
||||
static const char *const TAG = "snf";
|
||||
#ifndef SNF_SDA
|
||||
#define SNF_SDA 4
|
||||
#endif
|
||||
#ifndef SNF_SCL
|
||||
#define SNF_SCL 5
|
||||
#endif
|
||||
static constexpr size_t N = 24000;
|
||||
static DRAM_ATTR uint8_t buf[N];
|
||||
static volatile DRAM_ATTR uint32_t n = 0;
|
||||
static volatile DRAM_ATTR uint32_t ovf = 0;
|
||||
static volatile DRAM_ATTR uint8_t last = 3;
|
||||
static volatile DRAM_ATTR bool armed = false;
|
||||
static uint8_t start = 3; // levels when the sniffer was armed
|
||||
|
||||
static void IRAM_ATTR isr(void *) {
|
||||
uint32_t v = REG_READ(GPIO_IN_REG);
|
||||
uint8_t s = ((v >> SNF_SDA) & 1) | (((v >> SNF_SCL) & 1) << 1);
|
||||
if (!armed || s == last)
|
||||
return;
|
||||
last = s;
|
||||
if (n < N)
|
||||
buf[n++] = s;
|
||||
else
|
||||
ovf = ovf + 1;
|
||||
}
|
||||
|
||||
static void install() {
|
||||
gpio_install_isr_service(ESP_INTR_FLAG_IRAM);
|
||||
for (int p : {SNF_SDA, SNF_SCL}) {
|
||||
gpio_set_intr_type((gpio_num_t) p, GPIO_INTR_ANYEDGE);
|
||||
gpio_isr_handler_add((gpio_num_t) p, isr, nullptr);
|
||||
gpio_intr_enable((gpio_num_t) p);
|
||||
}
|
||||
ESP_LOGI(TAG, "sniffer on SDA %d SCL %d", SNF_SDA, SNF_SCL);
|
||||
}
|
||||
|
||||
static i2c::I2CBus *bus = nullptr; // the bus the device uses (hardware or the software copy)
|
||||
|
||||
static void wb(std::string &e, uint8_t b) {
|
||||
char t[8];
|
||||
snprintf(t, sizeof t, "%02X+ ", b);
|
||||
e += t;
|
||||
}
|
||||
static void rb(std::string &e, const uint8_t *r, size_t k) {
|
||||
char t[8];
|
||||
for (size_t i = 0; i < k; i++) {
|
||||
snprintf(t, sizeof t, "r%02X%c ", r[i], i + 1 < k ? '+' : '-');
|
||||
e += t;
|
||||
}
|
||||
}
|
||||
|
||||
static uint8_t data[320];
|
||||
static uint8_t rdata[40];
|
||||
static uint32_t idx = 0, nok = 0, ndiff = 0, namb = 0, nalloc = 0;
|
||||
static bool done = false;
|
||||
|
||||
static void arm() {
|
||||
n = 0;
|
||||
ovf = 0;
|
||||
uint32_t v = REG_READ(GPIO_IN_REG);
|
||||
start = ((v >> SNF_SDA) & 1) | (((v >> SNF_SCL) & 1) << 1);
|
||||
last = start;
|
||||
alloc_count::count = 0;
|
||||
alloc_count::window = true;
|
||||
#ifdef SNF_PDSLAVE
|
||||
pds::ln = 0;
|
||||
pds::lovf = 0;
|
||||
#endif
|
||||
armed = true;
|
||||
}
|
||||
static void disarm() {
|
||||
armed = false;
|
||||
alloc_count::window = false;
|
||||
}
|
||||
|
||||
// Runs one case and returns false when the sweep is over
|
||||
static bool step(i2c::I2CDevice *dev) {
|
||||
if (done)
|
||||
return false;
|
||||
const uint8_t A = 0x48;
|
||||
std::string label, exp;
|
||||
#ifdef SNF_PDSLAVE
|
||||
if (idx == 0) {
|
||||
// warm the target ISR (it runs from flash) so the first case is not lost to a cache miss
|
||||
bus->write_readv(0x48, nullptr, 0, nullptr, 0);
|
||||
delay(5);
|
||||
}
|
||||
#endif
|
||||
uint32_t i = idx++;
|
||||
for (size_t k = 0; k < sizeof data; k++)
|
||||
data[k] = static_cast<uint8_t>(0x11 + i + k * 7);
|
||||
i2c::ErrorCode rc;
|
||||
static constexpr uint32_t W8 = 301, W16 = 301; // write_register lengths 0..300
|
||||
if (i < W8) {
|
||||
size_t len = i;
|
||||
label = "wreg8 len=" + std::to_string(len);
|
||||
arm();
|
||||
rc = dev->write_register(0x02, data, len);
|
||||
disarm();
|
||||
exp = "S ";
|
||||
wb(exp, A << 1);
|
||||
wb(exp, 0x02);
|
||||
for (size_t k = 0; k < len; k++)
|
||||
wb(exp, data[k]);
|
||||
exp += "P ";
|
||||
} else if (i < W8 + W16) {
|
||||
size_t len = i - W8;
|
||||
label = "wreg16 len=" + std::to_string(len);
|
||||
arm();
|
||||
rc = dev->write_register16(0x0203, data, len);
|
||||
disarm();
|
||||
exp = "S ";
|
||||
wb(exp, A << 1);
|
||||
wb(exp, 0x02);
|
||||
wb(exp, 0x03);
|
||||
for (size_t k = 0; k < len; k++)
|
||||
wb(exp, data[k]);
|
||||
exp += "P ";
|
||||
} else if (i < W8 + W16 + 10) {
|
||||
static const size_t ns[] = {1, 2, 3, 4, 8};
|
||||
uint32_t j = i - W8 - W16;
|
||||
size_t k = ns[j % 5];
|
||||
bool sixteen = j >= 5;
|
||||
label = std::string(sixteen ? "rreg16" : "rreg8") + " n=" + std::to_string(k);
|
||||
memset(rdata, 0, sizeof rdata);
|
||||
arm();
|
||||
rc = sixteen ? dev->read_register16(0x0100, rdata, k) : dev->read_register(0x01, rdata, k);
|
||||
disarm();
|
||||
exp = "S ";
|
||||
wb(exp, A << 1);
|
||||
wb(exp, 0x01);
|
||||
if (sixteen)
|
||||
wb(exp, 0x00);
|
||||
exp += "Sr ";
|
||||
wb(exp, (A << 1) | 1);
|
||||
rb(exp, rdata, k);
|
||||
exp += "P ";
|
||||
} else if (i < W8 + W16 + 13) {
|
||||
size_t k = i - (W8 + W16 + 10) + 1;
|
||||
label = "read n=" + std::to_string(k);
|
||||
arm();
|
||||
rc = dev->read(rdata, k);
|
||||
disarm();
|
||||
exp = "S ";
|
||||
wb(exp, (A << 1) | 1);
|
||||
rb(exp, rdata, k);
|
||||
exp += "P ";
|
||||
} else if (i < W8 + W16 + 16) {
|
||||
static const uint8_t p1[] = {0x02};
|
||||
size_t k = i - (W8 + W16 + 13) + 1;
|
||||
label = "write1+read n=" + std::to_string(k);
|
||||
arm();
|
||||
rc = dev->write_read(p1, 1, rdata, k);
|
||||
disarm();
|
||||
exp = "S ";
|
||||
wb(exp, A << 1);
|
||||
wb(exp, 0x02);
|
||||
exp += "Sr ";
|
||||
wb(exp, (A << 1) | 1);
|
||||
rb(exp, rdata, k);
|
||||
exp += "P ";
|
||||
} else if (i < W8 + W16 + 16 + 112) {
|
||||
uint8_t a = 8 + (i - (W8 + W16 + 16));
|
||||
label = "probe " + std::to_string(a);
|
||||
arm();
|
||||
rc = bus->write_readv(a, nullptr, 0, nullptr, 0);
|
||||
disarm();
|
||||
exp = "S ";
|
||||
char t[8];
|
||||
snprintf(t, sizeof t, "%02X%c ", a << 1, a == A ? '+' : '-');
|
||||
exp += t;
|
||||
exp += "P ";
|
||||
} else {
|
||||
done = true;
|
||||
ESP_LOGI(TAG, "SNDONE cases=%u ok=%u diff=%u amb=%u alloc_calls=%u", (unsigned) idx - 1, (unsigned) nok,
|
||||
(unsigned) ndiff, (unsigned) namb, (unsigned) nalloc);
|
||||
return false;
|
||||
}
|
||||
uint32_t amb = 0;
|
||||
#ifdef SNF_PDSLAVE
|
||||
delayMicroseconds(500);
|
||||
std::string got = pds::tokens();
|
||||
ovf = pds::lovf;
|
||||
n = pds::ln;
|
||||
#else
|
||||
std::string got = wiredecode::decode(buf, n, start, amb);
|
||||
#endif
|
||||
bool ok = got == exp && ovf == 0;
|
||||
nok += ok;
|
||||
ndiff += !ok;
|
||||
namb += amb;
|
||||
nalloc += alloc_count::count != 0;
|
||||
ESP_LOGI(TAG, "SN %s|rc=%d|alloc=%u|ev=%u|amb=%u|ovf=%u|fnv=%08X|%s", label.c_str(), (int) rc,
|
||||
(unsigned) alloc_count::count, (unsigned) n, (unsigned) amb, (unsigned) ovf, (unsigned) fnv1a_hash(got),
|
||||
ok ? "OK" : "DIFF");
|
||||
if (!ok) {
|
||||
ESP_LOGI(TAG, "SNGOT %s", got.substr(0, 200).c_str());
|
||||
ESP_LOGI(TAG, "SNEXP %s", exp.substr(0, 200).c_str());
|
||||
}
|
||||
App.feed_wdt();
|
||||
return true;
|
||||
}
|
||||
|
||||
#ifdef SNF_SW
|
||||
static i2c::I2CBus *make_sw() {
|
||||
auto *sda = new esp32::ESP32InternalGPIOPin();
|
||||
sda->set_pin((gpio_num_t) SNF_SDA);
|
||||
sda->set_flags(gpio::FLAG_INPUT | gpio::FLAG_PULLUP);
|
||||
auto *scl = new esp32::ESP32InternalGPIOPin();
|
||||
scl->set_pin((gpio_num_t) SNF_SCL);
|
||||
scl->set_flags(gpio::FLAG_INPUT | gpio::FLAG_PULLUP);
|
||||
auto *b = new i2c::SoftwareI2CBus();
|
||||
b->set_sda_pin(sda);
|
||||
b->set_scl_pin(scl);
|
||||
b->set_frequency(SNF_SW_FREQ);
|
||||
b->set_scan(true);
|
||||
b->set_timeout(1000);
|
||||
b->setup();
|
||||
b->dump_config();
|
||||
return b;
|
||||
}
|
||||
#endif
|
||||
} // namespace snf
|
||||
@@ -1,81 +0,0 @@
|
||||
"""compare.py BASE PR: wire and return code per bus and case must match; summarize allocations and IDF job lists."""
|
||||
|
||||
from collections import Counter, defaultdict
|
||||
import pathlib
|
||||
import sys
|
||||
|
||||
sys.path.insert(0, str(pathlib.Path(__file__).resolve().parent.parent))
|
||||
from snlog import first_alloc # noqa: E402
|
||||
|
||||
|
||||
def load(p):
|
||||
rec, jobs = {}, {}
|
||||
with pathlib.Path(p).open(encoding="utf-8") as fh:
|
||||
for line in fh:
|
||||
line = line.rstrip("\n")
|
||||
if line.startswith("ALLOC_PROBE"):
|
||||
continue
|
||||
parts = line.split("|")
|
||||
if parts[0] == "J":
|
||||
jobs[(parts[1], parts[2])] = parts[3].strip()
|
||||
continue
|
||||
bus, case, rc, alloc, wire = parts
|
||||
rec[(bus, case)] = (rc, int(alloc.split("=")[1]), wire.strip())
|
||||
return rec, jobs
|
||||
|
||||
|
||||
b, bj = load(sys.argv[1])
|
||||
p, pj = load(sys.argv[2])
|
||||
assert b.keys() == p.keys(), "case lists differ"
|
||||
diff = [k for k in b if b[k][0] != p[k][0] or b[k][2] != p[k][2]]
|
||||
per_bus = Counter(k[0] for k in b)
|
||||
print("cases per bus:", dict(per_bus))
|
||||
print("wire/rc differences:", len(diff))
|
||||
for k in diff[:40]:
|
||||
print(
|
||||
" DIFF",
|
||||
k,
|
||||
"\n base",
|
||||
b[k][0],
|
||||
b[k][2][:160],
|
||||
"\n pr ",
|
||||
p[k][0],
|
||||
p[k][2][:160],
|
||||
)
|
||||
alloc = defaultdict(
|
||||
lambda: [0, 0, 0, 0]
|
||||
) # base cases with alloc, pr cases with alloc, base total, pr total
|
||||
for k in b:
|
||||
if not k[1].startswith(("wreg8", "wreg16")):
|
||||
continue
|
||||
a = alloc[k[0]]
|
||||
a[0] += b[k][1] > 0
|
||||
a[1] += p[k][1] > 0
|
||||
a[2] += b[k][1]
|
||||
a[3] += p[k][1]
|
||||
print("write_register/16 calls that allocated (base, pr):")
|
||||
for bus, a in alloc.items():
|
||||
print(
|
||||
f" {bus:8s} base {a[0]} calls ({a[2]} mallocs) pr {a[1]} calls ({a[3]} mallocs)"
|
||||
)
|
||||
# first length that allocates, per bus and shape
|
||||
for bus in per_bus:
|
||||
ab = {c: r[1] for (x, c), r in b.items() if x == bus}
|
||||
ap = {c: r[1] for (x, c), r in p.items() if x == bus}
|
||||
for shape in ("wreg8", "wreg16"):
|
||||
fb, fp = first_alloc(ab, shape), first_alloc(ap, shape)
|
||||
print(f" first allocating length {bus} {shape}: base {fb} pr {fp}")
|
||||
other = [
|
||||
k
|
||||
for k in b
|
||||
if not k[1].startswith(("wreg8", "wreg16", "wedge wreg", "absent wreg"))
|
||||
and (b[k][1] or p[k][1])
|
||||
]
|
||||
print("other calls with allocations:", other[:10])
|
||||
jd = Counter()
|
||||
for k in bj:
|
||||
if bj[k] != pj.get(k):
|
||||
jd[(bj[k], pj.get(k))] += 1
|
||||
print("IDF job list changes (base -> pr: count):")
|
||||
for (x, y), n in sorted(jd.items(), key=lambda t: -t[1])[:20]:
|
||||
print(f" {x!r} -> {y!r}: {n}")
|
||||
@@ -1,33 +0,0 @@
|
||||
"""extract.py TREE OUTDIR: copy the write_readv/transfer_ definitions of the Arduino and ESP-IDF buses out of TREE."""
|
||||
|
||||
from pathlib import Path
|
||||
import re
|
||||
import shutil
|
||||
import sys
|
||||
|
||||
tree, out = Path(sys.argv[1]), Path(sys.argv[2])
|
||||
for cls, src, dst in (
|
||||
("ArduinoI2CBus", "i2c_bus_arduino.cpp", "wab_gen_arduino.inc"),
|
||||
("IDFI2CBus", "i2c_bus_esp_idf.cpp", "wab_gen_idf.inc"),
|
||||
):
|
||||
lines = (tree / "esphome/components/i2c" / src).read_text().splitlines()
|
||||
pat = re.compile(rf"^ErrorCode {cls}::(write_readv|transfer_)\(")
|
||||
starts = [i for i, line in enumerate(lines) if pat.match(line)]
|
||||
assert len(starts) == 1, (src, starts)
|
||||
s = starts[0]
|
||||
e = next(i for i in range(s, len(lines)) if lines[i] == "}")
|
||||
(out / dst).write_text("\n".join(lines[s : e + 1]) + "\n")
|
||||
print(dst, lines[s].split("(")[0], e - s + 1, "lines")
|
||||
|
||||
shutil.copy(
|
||||
tree / "esphome/components/i2c/i2c_bus_software.cpp", out / "wab_sw_impl.inc"
|
||||
)
|
||||
shutil.copy(
|
||||
tree / "esphome/components/i2c/i2c_bus_software.h", out / "i2c_bus_software.h"
|
||||
)
|
||||
h = out / "i2c_bus_software.h"
|
||||
h.write_text(
|
||||
h.read_text().replace(
|
||||
'#include "i2c_bus.h"', '#include "esphome/components/i2c/i2c_bus.h"'
|
||||
)
|
||||
)
|
||||
@@ -1,22 +0,0 @@
|
||||
#!/bin/bash
|
||||
# run.sh TREE OUTFILE: copy the harness into TREE's tests/components/i2c, generate the extracted bus bodies, run the
|
||||
# unit test and remove the harness again. env: PY (default TREE's venv python)
|
||||
T=$(cd "${1:?tree}" && pwd); O=$(cd "$(dirname "${2:?out}")" && pwd)/$(basename "$2")
|
||||
H=$(cd "$(dirname "$0")" && pwd); PY=${PY:-$T/venv/bin/python}
|
||||
D=$T/tests/components/i2c
|
||||
# The sources are stored as .cpp.example, .h.example and .hpp so the repo's C++ lints skip them; copy them back.
|
||||
FILES=(wab_common.h wab_ard8266.cpp wab_ardpico.cpp wab_idf.cpp wab_sw.cpp test_wab.cpp benchmark.yaml)
|
||||
G=$(mktemp -d)
|
||||
cleanup() { for f in "${FILES[@]}"; do rm -f "$D/$f"; done; rm -rf "$G"; }
|
||||
trap cleanup EXIT
|
||||
trap 'exit 130' INT TERM
|
||||
cp "$H/wab_common.hpp" "$D/wab_common.h"
|
||||
for f in "${FILES[@]:1:5}"; do cp "$H/$f.example" "$D/$f"; done
|
||||
mkdir -p "$G/gen" "$G/stubs/driver"
|
||||
for f in Arduino.h Wire.h driver/i2c_master.h; do cp "$H/stub/$f.example" "$G/stubs/$f"; done
|
||||
$PY "$H/extract.py" "$T" "$G/gen"
|
||||
printf "esphome:\n build_flags:\n - -I%s\n - -I%s\n - -I%s\n" "$D" "$G/stubs" "$G/gen" > "$D/benchmark.yaml"
|
||||
(cd "$T" && WAB_OUT=$O PYTHONPATH=$T $PY script/cpp_unit_test.py i2c) > "${O%.txt}-build.log" 2>&1
|
||||
rc=$?
|
||||
echo "rc=$rc"; tail -5 "${O%.txt}-build.log"
|
||||
exit $rc
|
||||
@@ -1,10 +0,0 @@
|
||||
#pragma once
|
||||
#include <cstdint>
|
||||
#define INPUT 0x01
|
||||
#define OUTPUT 0x03
|
||||
#define INPUT_PULLUP 0x05
|
||||
#define LOW 0
|
||||
#define HIGH 1
|
||||
inline void pinMode(uint8_t, uint8_t) {}
|
||||
inline void digitalWrite(uint8_t, uint8_t) {}
|
||||
inline int digitalRead(uint8_t) { return HIGH; }
|
||||
@@ -1,207 +0,0 @@
|
||||
#pragma once
|
||||
// TwoWire models of the cores ESPHome uses, with the cores' own buffer rules. Select with WAB_WIRE_ESP8266 or
|
||||
// WAB_WIRE_PICO before including. Bytes go on the wire through wab::wire() with ACKs from wab::slave().
|
||||
#include <cstddef>
|
||||
#include <cstdint>
|
||||
#include "wab_common.h"
|
||||
|
||||
#if defined(WAB_WIRE_ESP8266)
|
||||
#define TwoWire TwoWire8266
|
||||
#define Wire Wire8266
|
||||
// esp8266 core 3.1.2-esphome.3 libraries/Wire: I2C_BUFFER_LENGTH 128
|
||||
class TwoWire {
|
||||
public:
|
||||
static constexpr size_t LEN = 128;
|
||||
void begin(int, int) {}
|
||||
void setClock(uint32_t) {}
|
||||
void setClockStretchLimit(uint32_t) {}
|
||||
void beginTransmission(uint8_t address) {
|
||||
transmitting = 1;
|
||||
txAddress = address;
|
||||
txBufferIndex = 0;
|
||||
txBufferLength = 0;
|
||||
}
|
||||
size_t write(uint8_t data) {
|
||||
if (transmitting) {
|
||||
if (txBufferLength >= LEN)
|
||||
return 0; // setWriteError()
|
||||
txBuffer[txBufferIndex] = data;
|
||||
++txBufferIndex;
|
||||
txBufferLength = txBufferIndex;
|
||||
}
|
||||
return 1;
|
||||
}
|
||||
size_t write(const uint8_t *data, size_t quantity) {
|
||||
if (transmitting) {
|
||||
for (size_t i = 0; i < quantity; ++i)
|
||||
if (!write(data[i]))
|
||||
return i;
|
||||
}
|
||||
return quantity;
|
||||
}
|
||||
uint8_t endTransmission(uint8_t sendStop) {
|
||||
// twi_writeTo: 0 ok, 2 address NACK, 3 data NACK; STOP when sendStop (also after a NACK)
|
||||
int8_t ret = 0;
|
||||
auto &w = wab::wire();
|
||||
auto &s = wab::slave();
|
||||
w.start();
|
||||
uint8_t ab = txAddress << 1;
|
||||
bool ack = s.address(ab);
|
||||
w.wbyte(ab, ack);
|
||||
if (!ack) {
|
||||
ret = 2;
|
||||
} else {
|
||||
for (size_t i = 0; i < txBufferLength; i++) {
|
||||
bool a = s.data(txBuffer[i]);
|
||||
w.wbyte(txBuffer[i], a);
|
||||
if (!a) {
|
||||
ret = 3;
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
if (sendStop || ret != 0)
|
||||
w.stop();
|
||||
txBufferIndex = 0;
|
||||
txBufferLength = 0;
|
||||
transmitting = 0;
|
||||
return ret;
|
||||
}
|
||||
size_t requestFrom(uint8_t address, size_t size, bool sendStop) {
|
||||
if (size > LEN)
|
||||
size = LEN;
|
||||
auto &w = wab::wire();
|
||||
auto &s = wab::slave();
|
||||
w.start();
|
||||
uint8_t ab = (address << 1) | 1;
|
||||
bool ack = s.address(ab);
|
||||
w.wbyte(ab, ack);
|
||||
size_t read = 0;
|
||||
if (ack) {
|
||||
for (size_t i = 0; i < size; i++) {
|
||||
rxBuffer[i] = s.next_read();
|
||||
w.rbyte(rxBuffer[i], i + 1 < size);
|
||||
}
|
||||
read = size;
|
||||
}
|
||||
if (sendStop || !ack)
|
||||
w.stop();
|
||||
rxBufferIndex = 0;
|
||||
rxBufferLength = read;
|
||||
return read;
|
||||
}
|
||||
int read() { return rxBufferIndex < rxBufferLength ? rxBuffer[rxBufferIndex++] : -1; }
|
||||
void wab_reset() { transmitting = 0; txBufferLength = txBufferIndex = 0; }
|
||||
|
||||
uint8_t transmitting{0};
|
||||
uint8_t txAddress{0};
|
||||
size_t txBufferIndex{0}, txBufferLength{0}, rxBufferIndex{0}, rxBufferLength{0};
|
||||
uint8_t txBuffer[LEN]{}, rxBuffer[LEN]{};
|
||||
};
|
||||
extern TwoWire Wire;
|
||||
|
||||
#elif defined(WAB_WIRE_PICO)
|
||||
#define TwoWire TwoWirePico
|
||||
#define Wire WirePico
|
||||
#define Wire1 Wire1Pico
|
||||
// arduino-pico 6.3.2 libraries/Wire: WIRE_BUFFER_SIZE 256, one buffer for TX and RX
|
||||
#ifndef WIRE_BUFFER_SIZE
|
||||
#define WIRE_BUFFER_SIZE 256
|
||||
#endif
|
||||
class TwoWire {
|
||||
public:
|
||||
static constexpr size_t LEN = WIRE_BUFFER_SIZE;
|
||||
void setSDA(int) {}
|
||||
void setSCL(int) {}
|
||||
void begin() {}
|
||||
void setClock(uint32_t) {}
|
||||
void setTimeout(uint32_t) {}
|
||||
void beginTransmission(uint8_t addr) {
|
||||
if (!_running || _txBegun)
|
||||
return; // ERROR
|
||||
_addr = addr;
|
||||
_buffLen = 0;
|
||||
_buffOff = 0;
|
||||
_txBegun = true;
|
||||
}
|
||||
size_t write(uint8_t ucData) {
|
||||
if (!_running)
|
||||
return 0;
|
||||
if (!_txBegun || (_buffLen == int(LEN)))
|
||||
return 0;
|
||||
_buff[_buffLen++] = ucData;
|
||||
return 1;
|
||||
}
|
||||
size_t write(const uint8_t *data, size_t quantity) {
|
||||
for (size_t i = 0; i < quantity; ++i)
|
||||
if (!write(data[i]))
|
||||
return i;
|
||||
return quantity;
|
||||
}
|
||||
uint8_t endTransmission(bool stopBit) {
|
||||
if (!_running || !_txBegun)
|
||||
return 4;
|
||||
_txBegun = false;
|
||||
auto &w = wab::wire();
|
||||
auto &s = wab::slave();
|
||||
if (!_buffLen) {
|
||||
// _probe(): its own START, address, STOP
|
||||
w.start();
|
||||
uint8_t ab = _addr << 1;
|
||||
bool ack = s.address(ab);
|
||||
w.wbyte(ab, ack);
|
||||
w.stop();
|
||||
return ack ? 0 : 2;
|
||||
}
|
||||
// i2c_write_blocking_until: abort (with STOP) on any NACK, returns PICO_ERROR_GENERIC
|
||||
w.start();
|
||||
uint8_t ab = _addr << 1;
|
||||
bool ok = s.address(ab);
|
||||
w.wbyte(ab, ok);
|
||||
for (int i = 0; ok && i < _buffLen; i++) {
|
||||
ok = s.data(_buff[i]);
|
||||
w.wbyte(_buff[i], ok);
|
||||
}
|
||||
if (stopBit || !ok)
|
||||
w.stop();
|
||||
_buffLen = 0;
|
||||
return ok ? 0 : 4;
|
||||
}
|
||||
size_t requestFrom(uint8_t address, size_t quantity, bool stopBit) {
|
||||
if (!_running || _txBegun || !quantity || (quantity > LEN))
|
||||
return 0;
|
||||
auto &w = wab::wire();
|
||||
auto &s = wab::slave();
|
||||
w.start();
|
||||
uint8_t ab = (address << 1) | 1;
|
||||
bool ack = s.address(ab);
|
||||
w.wbyte(ab, ack);
|
||||
_buffOff = 0;
|
||||
if (!ack) {
|
||||
w.stop();
|
||||
_buffLen = 0;
|
||||
return 0;
|
||||
}
|
||||
for (size_t i = 0; i < quantity; i++) {
|
||||
_buff[i] = s.next_read();
|
||||
w.rbyte(_buff[i], i + 1 < quantity);
|
||||
}
|
||||
if (stopBit)
|
||||
w.stop();
|
||||
_buffLen = quantity;
|
||||
return quantity;
|
||||
}
|
||||
int read() { return _buffOff < _buffLen ? _buff[_buffOff++] : -1; }
|
||||
void wab_reset() { _txBegun = false; _buffLen = _buffOff = 0; }
|
||||
|
||||
bool _running{true};
|
||||
bool _txBegun{false};
|
||||
uint8_t _addr{0};
|
||||
int _buffLen{0}, _buffOff{0};
|
||||
uint8_t _buff[LEN]{};
|
||||
};
|
||||
extern TwoWire Wire;
|
||||
extern TwoWire Wire1;
|
||||
#else
|
||||
#error "select a Wire model"
|
||||
#endif
|
||||
-39
@@ -1,39 +0,0 @@
|
||||
#pragma once
|
||||
// Types of ESP-IDF 5.5.5 esp_driver_i2c used by i2c_bus_esp_idf, with an execute model that puts the jobs on the
|
||||
// wab wire and applies the driver's own argument checks (command count, NACK before STOP).
|
||||
#include <cstddef>
|
||||
#include <cstdint>
|
||||
typedef int esp_err_t;
|
||||
#define ESP_OK 0
|
||||
#define ESP_FAIL -1
|
||||
#define ESP_ERR_INVALID_ARG 0x102
|
||||
#define ESP_ERR_INVALID_STATE 0x103
|
||||
#define ESP_ERR_INVALID_RESPONSE 0x108
|
||||
#define ESP_ERR_TIMEOUT 0x107
|
||||
inline const char *esp_err_to_name(esp_err_t) { return "err"; }
|
||||
typedef int i2c_port_t;
|
||||
typedef enum { I2C_MASTER_WRITE = 0, I2C_MASTER_READ } i2c_rw_t;
|
||||
typedef enum { I2C_ACK_VAL = 0, I2C_NACK_VAL = 1 } i2c_ack_value_t;
|
||||
typedef enum { I2C_MASTER_CMD_START, I2C_MASTER_CMD_WRITE, I2C_MASTER_CMD_READ, I2C_MASTER_CMD_STOP } i2c_master_command_t;
|
||||
typedef struct i2c_master_bus_t *i2c_master_bus_handle_t;
|
||||
typedef struct i2c_master_dev_t *i2c_master_dev_handle_t;
|
||||
typedef struct {
|
||||
i2c_master_command_t command;
|
||||
union {
|
||||
struct {
|
||||
bool ack_check;
|
||||
uint8_t *data;
|
||||
size_t total_bytes;
|
||||
} write;
|
||||
struct {
|
||||
i2c_ack_value_t ack_value;
|
||||
uint8_t *data;
|
||||
size_t total_bytes;
|
||||
} read;
|
||||
};
|
||||
} i2c_operation_job_t;
|
||||
#ifndef SOC_I2C_CMD_REG_NUM
|
||||
#define SOC_I2C_CMD_REG_NUM 8 // C2, C3, C6, S3, P4; 16 on ESP32 and S2
|
||||
#endif
|
||||
esp_err_t i2c_master_execute_defined_operations(i2c_master_dev_handle_t dev, i2c_operation_job_t *ops, size_t n,
|
||||
int timeout_ms);
|
||||
@@ -1,208 +0,0 @@
|
||||
// Drives every I2CDevice call shape through each bus model and writes one line per call to $WAB_OUT.
|
||||
#include <gtest/gtest.h>
|
||||
|
||||
#include <cstdint>
|
||||
#include <cstdlib>
|
||||
#include <fstream>
|
||||
#include <functional>
|
||||
#include <vector>
|
||||
#ifndef __has_feature
|
||||
#define __has_feature(x) 0
|
||||
#endif
|
||||
#if defined(__SANITIZE_ADDRESS__) || __has_feature(address_sanitizer)
|
||||
#define WAB_ASAN 1
|
||||
extern "C" int __sanitizer_install_malloc_and_free_hooks(void (*malloc_hook)(const volatile void *, size_t),
|
||||
void (*free_hook)(const volatile void *));
|
||||
#endif
|
||||
#include "esphome/components/i2c/i2c.h"
|
||||
#include "wab_common.h"
|
||||
|
||||
esphome::i2c::I2CBus *wab_make_esp8266();
|
||||
esphome::i2c::I2CBus *wab_make_pico();
|
||||
esphome::i2c::I2CBus *wab_make_idf();
|
||||
esphome::i2c::I2CBus *wab_make_sw();
|
||||
void wab_reset_esp8266();
|
||||
void wab_reset_pico();
|
||||
void wab_reset_sw();
|
||||
|
||||
namespace wab {
|
||||
static volatile bool g_window = false;
|
||||
static volatile size_t g_allocs = 0;
|
||||
#ifdef WAB_ASAN
|
||||
static void on_malloc(const volatile void *, size_t) {
|
||||
if (g_window)
|
||||
g_allocs = g_allocs + 1;
|
||||
}
|
||||
static void on_free(const volatile void *) {}
|
||||
static bool install() {
|
||||
__sanitizer_install_malloc_and_free_hooks(on_malloc, on_free);
|
||||
return true;
|
||||
}
|
||||
#else
|
||||
typedef void(malloc_logger_t)(uint32_t type, uintptr_t arg1, uintptr_t arg2, uintptr_t arg3, uintptr_t result,
|
||||
uint32_t num_hot_frames_to_skip);
|
||||
extern "C" malloc_logger_t *malloc_logger;
|
||||
static void on_log(uint32_t type, uintptr_t, uintptr_t, uintptr_t, uintptr_t, uint32_t) {
|
||||
// MALLOC_LOG_TYPE_ALLOCATE = 2
|
||||
if (g_window && (type & 2))
|
||||
g_allocs = g_allocs + 1;
|
||||
}
|
||||
static bool install() {
|
||||
malloc_logger = on_log;
|
||||
return true;
|
||||
}
|
||||
#endif
|
||||
static bool g_installed = install();
|
||||
void alloc_window(bool on) {
|
||||
if (on)
|
||||
g_allocs = 0;
|
||||
g_window = on;
|
||||
}
|
||||
size_t alloc_count() { return g_allocs; }
|
||||
} // namespace wab
|
||||
|
||||
namespace esphome::i2c::testing {
|
||||
|
||||
// Zephyr, host and TCA9548A keep the default write_prefixed(): this bus only has write_readv(), like them
|
||||
class DefaultPathBus : public I2CBus {
|
||||
public:
|
||||
ErrorCode write_readv(uint8_t address, const uint8_t *wb, size_t wc, uint8_t *rb, size_t rc) override {
|
||||
auto &w = wab::wire();
|
||||
auto &s = wab::slave();
|
||||
if (wc != 0 || rc == 0) {
|
||||
w.start();
|
||||
bool ok = s.address(address << 1);
|
||||
w.wbyte(address << 1, ok);
|
||||
for (size_t i = 0; ok && i < wc; i++) {
|
||||
ok = s.data(wb[i]);
|
||||
w.wbyte(wb[i], ok);
|
||||
}
|
||||
if (!ok) {
|
||||
w.stop();
|
||||
return ERROR_NOT_ACKNOWLEDGED;
|
||||
}
|
||||
if (rc == 0) {
|
||||
w.stop();
|
||||
return ERROR_OK;
|
||||
}
|
||||
}
|
||||
w.start();
|
||||
bool ok = s.address((address << 1) | 1);
|
||||
w.wbyte((address << 1) | 1, ok);
|
||||
for (size_t i = 0; ok && i < rc; i++) {
|
||||
rb[i] = s.next_read();
|
||||
w.rbyte(rb[i], i + 1 < rc);
|
||||
}
|
||||
w.stop();
|
||||
return ok ? ERROR_OK : ERROR_NOT_ACKNOWLEDGED;
|
||||
}
|
||||
};
|
||||
|
||||
static std::vector<uint8_t> pattern(size_t len, uint8_t seed) {
|
||||
std::vector<uint8_t> v(len);
|
||||
for (size_t i = 0; i < len; i++)
|
||||
v[i] = static_cast<uint8_t>(seed + i * 7);
|
||||
return v;
|
||||
}
|
||||
|
||||
TEST(WireAB, Record) {
|
||||
const char *out = std::getenv("WAB_OUT");
|
||||
ASSERT_NE(out, nullptr);
|
||||
std::ofstream f(out);
|
||||
// Allocation counter sanity: a malloc inside the window must count
|
||||
wab::alloc_window(true);
|
||||
void *volatile probe = malloc(100);
|
||||
wab::alloc_window(false);
|
||||
free(probe);
|
||||
f << "ALLOC_PROBE " << wab::alloc_count() << "\n";
|
||||
ASSERT_EQ(wab::alloc_count(), 1u);
|
||||
|
||||
struct Bus {
|
||||
const char *name;
|
||||
I2CBus *bus;
|
||||
std::function<void()> reset;
|
||||
};
|
||||
std::vector<Bus> buses = {
|
||||
{"esp8266", wab_make_esp8266(), wab_reset_esp8266},
|
||||
{"pico", wab_make_pico(), wab_reset_pico},
|
||||
{"idf", wab_make_idf(), [] {}},
|
||||
{"sw", wab_make_sw(), wab_reset_sw},
|
||||
{"default", new DefaultPathBus(), [] {}},
|
||||
};
|
||||
for (auto &b : buses) {
|
||||
I2CDevice dev;
|
||||
dev.set_i2c_bus(b.bus);
|
||||
auto run = [&](const std::string &label, const std::function<ErrorCode()> &fn, bool reset = true) {
|
||||
if (reset)
|
||||
b.reset();
|
||||
wab::wire().clear();
|
||||
wab::jobs_note().clear();
|
||||
// the recorder must not allocate inside the window
|
||||
wab::wire().text.reserve(16384);
|
||||
wab::jobs_note().reserve(1024);
|
||||
wab::alloc_window(true);
|
||||
ErrorCode rc = fn();
|
||||
wab::alloc_window(false);
|
||||
f << b.name << "|" << label << "|rc=" << rc << "|alloc=" << wab::alloc_count() << "|" << wab::wire().text
|
||||
<< "\n";
|
||||
if (!wab::jobs_note().empty())
|
||||
f << "J|" << b.name << "|" << label << "|" << wab::jobs_note() << "\n";
|
||||
};
|
||||
auto &sl = wab::slave();
|
||||
sl.ack_limit = SIZE_MAX;
|
||||
dev.set_i2c_address(0x48);
|
||||
for (size_t len = 0; len <= 300; len++) {
|
||||
auto d = pattern(len, 0x11);
|
||||
run("wreg8 len=" + std::to_string(len), [&] { return dev.write_register(0x02, d.data(), d.size()); });
|
||||
run("wreg16 len=" + std::to_string(len), [&] { return dev.write_register16(0xBEEF, d.data(), d.size()); });
|
||||
}
|
||||
// nullptr data with len 0, as a caller with no payload may pass
|
||||
run("wreg8 null len=0", [&] { return dev.write_register(0x03, nullptr, 0); });
|
||||
run("wreg16 null len=0", [&] { return dev.write_register16(0x0304, nullptr, 0); });
|
||||
// slave NACKs after k data bytes (k counts the register byte)
|
||||
for (size_t k = 0; k <= 6; k++) {
|
||||
sl.ack_limit = k;
|
||||
auto d = pattern(4, 0x40);
|
||||
run("wreg8 len=4 acklimit=" + std::to_string(k), [&] { return dev.write_register(0x05, d.data(), d.size()); });
|
||||
run("wreg16 len=4 acklimit=" + std::to_string(k),
|
||||
[&] { return dev.write_register16(0x0506, d.data(), d.size()); });
|
||||
}
|
||||
sl.ack_limit = SIZE_MAX;
|
||||
// reads, write+read with repeated start, plain writes, probes
|
||||
for (size_t n : {1, 2, 3, 16, 32}) {
|
||||
std::vector<uint8_t> r(n);
|
||||
run("rreg8 n=" + std::to_string(n), [&] { return dev.read_register(0x01, r.data(), n); });
|
||||
run("rreg16 n=" + std::to_string(n), [&] { return dev.read_register16(0x0102, r.data(), n); });
|
||||
run("read n=" + std::to_string(n), [&] { return dev.read(r.data(), n); });
|
||||
auto w3 = pattern(3, 0x70);
|
||||
run("wr 3+" + std::to_string(n), [&] { return dev.write_read(w3.data(), 3, r.data(), n); });
|
||||
}
|
||||
for (size_t len : {0, 1, 5, 127, 128, 129, 255, 256, 257}) {
|
||||
auto d = pattern(len, 0x22);
|
||||
run("write len=" + std::to_string(len), [&] { return dev.write(d.data(), d.size()); });
|
||||
}
|
||||
// a normal transaction right after an over-long write, without resetting the Wire model in between
|
||||
for (size_t len : {200, 300}) {
|
||||
auto d = pattern(len, 0x33);
|
||||
run("wedge wreg8 len=" + std::to_string(len), [&] { return dev.write_register(0x02, d.data(), d.size()); });
|
||||
std::vector<uint8_t> r(2);
|
||||
run("wedge then rreg8 n=2", [&] { return dev.read_register(0x01, r.data(), 2); }, false);
|
||||
auto s = pattern(2, 0x44);
|
||||
run("wedge then wreg8 len=2", [&] { return dev.write_register(0x02, s.data(), 2); }, false);
|
||||
}
|
||||
// absent device
|
||||
dev.set_i2c_address(0x50);
|
||||
for (size_t len : {0, 1, 5}) {
|
||||
auto d = pattern(len, 0x55);
|
||||
run("absent wreg8 len=" + std::to_string(len), [&] { return dev.write_register(0x02, d.data(), d.size()); });
|
||||
run("absent wreg16 len=" + std::to_string(len),
|
||||
[&] { return dev.write_register16(0x0203, d.data(), d.size()); });
|
||||
}
|
||||
// scan probe over the whole range, as i2c_scan_() does
|
||||
for (uint8_t a = 8; a != 120; a++) {
|
||||
run("probe " + std::to_string(a), [&] { return b.bus->write_readv(a, nullptr, 0, nullptr, 0); });
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
} // namespace esphome::i2c::testing
|
||||
@@ -1,34 +0,0 @@
|
||||
// Arduino bus body of the tree under test on the esp8266 core Wire model
|
||||
#include "esphome/core/application.h"
|
||||
#include "esphome/core/component.h"
|
||||
#include "esphome/core/hal.h"
|
||||
#include "esphome/core/helpers.h"
|
||||
#include "esphome/core/log.h"
|
||||
#include "esphome/components/i2c/i2c_bus.h"
|
||||
#define WAB_WIRE_ESP8266
|
||||
#define USE_ARDUINO
|
||||
#define USE_ESP8266
|
||||
#define ArduinoI2CBus ArduinoI2CBus8266
|
||||
#include "esphome/components/i2c/i2c_bus_arduino.h"
|
||||
TwoWire Wire;
|
||||
namespace esphome::i2c {
|
||||
ESPHOME_LOG_TAG(TAG, "i2c");
|
||||
static constexpr size_t I2C_MAX_LOG_BYTES = 32;
|
||||
void ArduinoI2CBus::setup() {}
|
||||
void ArduinoI2CBus::dump_config() {}
|
||||
void ArduinoI2CBus::set_pins_and_clock_() {}
|
||||
#include "wab_gen_arduino.inc"
|
||||
} // namespace esphome::i2c
|
||||
#undef USE_ESP8266
|
||||
#undef USE_ARDUINO
|
||||
struct A8Init { using type = bool esphome::i2c::ArduinoI2CBus::*; friend type get(A8Init); };
|
||||
struct A8Wire { using type = TwoWire *esphome::i2c::ArduinoI2CBus::*; friend type get(A8Wire); };
|
||||
template struct WabRob<A8Init, &esphome::i2c::ArduinoI2CBus::initialized_>;
|
||||
template struct WabRob<A8Wire, &esphome::i2c::ArduinoI2CBus::wire_>;
|
||||
esphome::i2c::I2CBus *wab_make_esp8266() {
|
||||
auto *b = new esphome::i2c::ArduinoI2CBus();
|
||||
b->*get(A8Init()) = true;
|
||||
b->*get(A8Wire()) = &Wire;
|
||||
return b;
|
||||
}
|
||||
void wab_reset_esp8266() { Wire.wab_reset(); }
|
||||
@@ -1,35 +0,0 @@
|
||||
// Arduino bus body of the tree under test on the arduino-pico Wire model
|
||||
#include "esphome/core/application.h"
|
||||
#include "esphome/core/component.h"
|
||||
#include "esphome/core/hal.h"
|
||||
#include "esphome/core/helpers.h"
|
||||
#include "esphome/core/log.h"
|
||||
#include "esphome/components/i2c/i2c_bus.h"
|
||||
#define WAB_WIRE_PICO
|
||||
#define USE_ARDUINO
|
||||
#define USE_RP2
|
||||
#define ArduinoI2CBus ArduinoI2CBusPico
|
||||
#include "esphome/components/i2c/i2c_bus_arduino.h"
|
||||
TwoWire Wire;
|
||||
TwoWire Wire1;
|
||||
namespace esphome::i2c {
|
||||
ESPHOME_LOG_TAG(TAG, "i2c");
|
||||
static constexpr size_t I2C_MAX_LOG_BYTES = 32;
|
||||
void ArduinoI2CBus::setup() {}
|
||||
void ArduinoI2CBus::dump_config() {}
|
||||
void ArduinoI2CBus::set_pins_and_clock_() {}
|
||||
#include "wab_gen_arduino.inc"
|
||||
} // namespace esphome::i2c
|
||||
#undef USE_RP2
|
||||
#undef USE_ARDUINO
|
||||
struct APInit { using type = bool esphome::i2c::ArduinoI2CBus::*; friend type get(APInit); };
|
||||
struct APWire { using type = TwoWire *esphome::i2c::ArduinoI2CBus::*; friend type get(APWire); };
|
||||
template struct WabRob<APInit, &esphome::i2c::ArduinoI2CBus::initialized_>;
|
||||
template struct WabRob<APWire, &esphome::i2c::ArduinoI2CBus::wire_>;
|
||||
esphome::i2c::I2CBus *wab_make_pico() {
|
||||
auto *b = new esphome::i2c::ArduinoI2CBus();
|
||||
b->*get(APInit()) = true;
|
||||
b->*get(APWire()) = &Wire;
|
||||
return b;
|
||||
}
|
||||
void wab_reset_pico() { Wire.wab_reset(); }
|
||||
@@ -1,87 +0,0 @@
|
||||
#pragma once
|
||||
// Wire-level A/B harness for esphome#22780: every bus model emits the same token stream.
|
||||
#include <cstddef>
|
||||
#include <cstdint>
|
||||
#include <cstdio>
|
||||
#include <string>
|
||||
#include <vector>
|
||||
|
||||
namespace wab {
|
||||
|
||||
// Token stream of the bus: "S", "Sr", "P", "AB+" (written byte, slave ACK), "AB-" (NACK), "rAB+"/"rAB-" (read byte,
|
||||
// master ACK/NACK), "ABORT".
|
||||
struct Wire {
|
||||
std::string text;
|
||||
bool open{false};
|
||||
void start() {
|
||||
text += open ? "Sr " : "S ";
|
||||
open = true;
|
||||
}
|
||||
void stop() {
|
||||
text += "P ";
|
||||
open = false;
|
||||
}
|
||||
void wbyte(uint8_t b, bool ack) {
|
||||
char t[8];
|
||||
snprintf(t, sizeof t, "%02X%c ", b, ack ? '+' : '-');
|
||||
text += t;
|
||||
}
|
||||
void rbyte(uint8_t b, bool ack) {
|
||||
char t[8];
|
||||
snprintf(t, sizeof t, "r%02X%c ", b, ack ? '+' : '-');
|
||||
text += t;
|
||||
}
|
||||
void note(const char *s) {
|
||||
text += s;
|
||||
text += ' ';
|
||||
}
|
||||
void clear() {
|
||||
text.clear();
|
||||
open = false;
|
||||
}
|
||||
};
|
||||
inline Wire &wire() {
|
||||
static Wire w;
|
||||
return w;
|
||||
}
|
||||
inline std::string &jobs_note() {
|
||||
static std::string s;
|
||||
return s;
|
||||
}
|
||||
|
||||
// Slave model: present addresses ACK their address and up to ack_limit data bytes after it.
|
||||
struct Slave {
|
||||
uint8_t present[4]{0x48, 0x1A, 0, 0};
|
||||
size_t ack_limit{SIZE_MAX};
|
||||
size_t written{0}; // data bytes written since the address byte
|
||||
size_t read_idx{0};
|
||||
bool is_present(uint8_t addr7) const {
|
||||
for (auto p : present)
|
||||
if (p != 0 && p == addr7)
|
||||
return true;
|
||||
return false;
|
||||
}
|
||||
// Called for the address byte; returns ACK
|
||||
bool address(uint8_t byte) {
|
||||
this->written = 0;
|
||||
this->read_idx = 0;
|
||||
return this->is_present(byte >> 1);
|
||||
}
|
||||
bool data(uint8_t) { return this->written++ < this->ack_limit; }
|
||||
uint8_t next_read() { return static_cast<uint8_t>(0xA0 + this->read_idx++); }
|
||||
};
|
||||
inline Slave &slave() {
|
||||
static Slave s;
|
||||
return s;
|
||||
}
|
||||
|
||||
// Allocation counter (window opened around one bus call)
|
||||
void alloc_window(bool on);
|
||||
size_t alloc_count();
|
||||
|
||||
} // namespace wab
|
||||
|
||||
// Member access to private fields of final classes (explicit instantiation ignores access checks)
|
||||
template<typename Tag, typename Tag::type M> struct WabRob {
|
||||
friend typename Tag::type get(Tag) { return M; }
|
||||
};
|
||||
@@ -1,88 +0,0 @@
|
||||
// ESP-IDF bus body of the tree under test on the IDF 5.5.5 job model
|
||||
#include <string>
|
||||
#include "esphome/core/application.h"
|
||||
#include "esphome/core/component.h"
|
||||
#include "esphome/core/hal.h"
|
||||
#include "esphome/core/helpers.h"
|
||||
#include "esphome/core/log.h"
|
||||
#include "esphome/components/i2c/i2c_bus.h"
|
||||
#include "wab_common.h"
|
||||
#define USE_ESP32
|
||||
#include "esphome/components/i2c/i2c_bus_esp_idf.h"
|
||||
namespace esphome::i2c {
|
||||
ESPHOME_LOG_TAG(TAG, "i2c.idf");
|
||||
static constexpr size_t I2C_MAX_LOG_BYTES = 32;
|
||||
void IDFI2CBus::setup() {}
|
||||
void IDFI2CBus::dump_config() {}
|
||||
#include "wab_gen_idf.inc"
|
||||
} // namespace esphome::i2c
|
||||
#undef USE_ESP32
|
||||
|
||||
esp_err_t i2c_master_execute_defined_operations(i2c_master_dev_handle_t, i2c_operation_job_t *ops, size_t n, int) {
|
||||
auto &jn = wab::jobs_note();
|
||||
jn.clear();
|
||||
for (size_t i = 0; i < n; i++) {
|
||||
char t[16];
|
||||
switch (ops[i].command) {
|
||||
case I2C_MASTER_CMD_START: jn += "S "; break;
|
||||
case I2C_MASTER_CMD_STOP: jn += "P "; break;
|
||||
case I2C_MASTER_CMD_WRITE: snprintf(t, sizeof t, "W%zu ", ops[i].write.total_bytes); jn += t; break;
|
||||
case I2C_MASTER_CMD_READ: snprintf(t, sizeof t, "R%zu%s ", ops[i].read.total_bytes,
|
||||
ops[i].read.ack_value == I2C_NACK_VAL ? "n" : "a"); jn += t; break;
|
||||
}
|
||||
}
|
||||
auto &w = wab::wire();
|
||||
auto &s = wab::slave();
|
||||
if (n > SOC_I2C_CMD_REG_NUM) {
|
||||
w.note("INVALID_ARG_TOO_MANY_JOBS");
|
||||
return ESP_ERR_INVALID_ARG;
|
||||
}
|
||||
for (size_t i = 0; i + 1 < n; i++) {
|
||||
if (ops[i].command == I2C_MASTER_CMD_READ && ops[i + 1].command == I2C_MASTER_CMD_STOP &&
|
||||
ops[i].read.ack_value != I2C_NACK_VAL) {
|
||||
w.note("INVALID_ARG_ACK_BEFORE_STOP");
|
||||
return ESP_ERR_INVALID_ARG;
|
||||
}
|
||||
}
|
||||
bool expect_addr = false;
|
||||
for (size_t i = 0; i < n; i++) {
|
||||
auto &op = ops[i];
|
||||
switch (op.command) {
|
||||
case I2C_MASTER_CMD_START:
|
||||
w.start();
|
||||
expect_addr = true;
|
||||
break;
|
||||
case I2C_MASTER_CMD_STOP:
|
||||
w.stop();
|
||||
break;
|
||||
case I2C_MASTER_CMD_WRITE:
|
||||
for (size_t k = 0; k < op.write.total_bytes; k++) {
|
||||
uint8_t b = op.write.data[k];
|
||||
bool ack = expect_addr ? s.address(b) : s.data(b);
|
||||
expect_addr = false;
|
||||
w.wbyte(b, ack);
|
||||
if (!ack && op.write.ack_check) {
|
||||
w.note("ABORT");
|
||||
w.stop();
|
||||
return ESP_ERR_INVALID_STATE;
|
||||
}
|
||||
}
|
||||
break;
|
||||
case I2C_MASTER_CMD_READ:
|
||||
for (size_t k = 0; k < op.read.total_bytes; k++) {
|
||||
op.read.data[k] = s.next_read();
|
||||
w.rbyte(op.read.data[k], op.read.ack_value == I2C_ACK_VAL);
|
||||
}
|
||||
break;
|
||||
}
|
||||
}
|
||||
return ESP_OK;
|
||||
}
|
||||
|
||||
struct IdfInit { using type = bool esphome::i2c::IDFI2CBus::*; friend type get(IdfInit); };
|
||||
template struct WabRob<IdfInit, &esphome::i2c::IDFI2CBus::initialized_>;
|
||||
esphome::i2c::I2CBus *wab_make_idf() {
|
||||
auto *b = new esphome::i2c::IDFI2CBus();
|
||||
b->*get(IdfInit()) = true;
|
||||
return b;
|
||||
}
|
||||
@@ -1,156 +0,0 @@
|
||||
// The software bus of the tree under test, whole, on fake open-drain pins with a bit-level slave
|
||||
#include "esphome/core/application.h"
|
||||
#include "esphome/core/component.h"
|
||||
#include "esphome/core/gpio.h"
|
||||
#include "esphome/core/hal.h"
|
||||
#include "esphome/core/helpers.h"
|
||||
#include "esphome/core/log.h"
|
||||
#include "esphome/components/i2c/i2c_bus.h"
|
||||
#include "wab_common.h"
|
||||
|
||||
namespace wab {
|
||||
struct BitSlave {
|
||||
bool m_sda_low{false}, m_scl_low{false}, s_sda_low{false};
|
||||
bool sda{true}, scl{true};
|
||||
enum { IDLE, ADDR, WDATA, RDATA, IGNORE } state{IDLE};
|
||||
enum { BITS, ACK_OUT, ACK_IN } phase{BITS};
|
||||
int bits{0};
|
||||
uint8_t shift{0}, cur{0};
|
||||
bool last_ack{false}, is_read{false}, master_ack{false};
|
||||
void drive_bit() { this->s_sda_low = !(this->cur & (0x80 >> this->bits)); }
|
||||
void update() {
|
||||
auto &w = wire();
|
||||
auto &sl = slave();
|
||||
for (int guard = 0; guard < 4; guard++) {
|
||||
bool nsda = !(this->m_sda_low || this->s_sda_low), nscl = !this->m_scl_low;
|
||||
if (nsda == this->sda && nscl == this->scl)
|
||||
return;
|
||||
bool sda_fell = this->sda && !nsda, sda_rose = !this->sda && nsda;
|
||||
bool scl_rose = !this->scl && nscl, scl_fell = this->scl && !nscl;
|
||||
this->sda = nsda;
|
||||
this->scl = nscl;
|
||||
if (this->scl && !scl_rose && sda_fell) { // START
|
||||
w.start();
|
||||
this->state = ADDR;
|
||||
this->phase = BITS;
|
||||
this->bits = 0;
|
||||
this->shift = 0;
|
||||
this->s_sda_low = false;
|
||||
continue;
|
||||
}
|
||||
if (this->scl && !scl_rose && sda_rose) { // STOP
|
||||
if (w.open)
|
||||
w.stop();
|
||||
this->state = IDLE;
|
||||
this->s_sda_low = false;
|
||||
continue;
|
||||
}
|
||||
if (scl_rose) {
|
||||
if ((this->state == ADDR || this->state == WDATA) && this->phase == BITS) {
|
||||
this->shift = (this->shift << 1) | (this->sda ? 1 : 0);
|
||||
this->bits++;
|
||||
} else if (this->state == RDATA && this->phase == ACK_IN) {
|
||||
this->master_ack = !this->sda;
|
||||
w.rbyte(this->cur, this->master_ack);
|
||||
}
|
||||
} else if (scl_fell) {
|
||||
if ((this->state == ADDR || this->state == WDATA) && this->phase == BITS && this->bits == 8) {
|
||||
if (this->state == ADDR) {
|
||||
this->last_ack = sl.address(this->shift);
|
||||
this->is_read = this->shift & 1;
|
||||
} else {
|
||||
this->last_ack = sl.data(this->shift);
|
||||
}
|
||||
w.wbyte(this->shift, this->last_ack);
|
||||
this->s_sda_low = this->last_ack;
|
||||
this->phase = ACK_OUT;
|
||||
} else if (this->phase == ACK_OUT) {
|
||||
this->s_sda_low = false;
|
||||
this->bits = 0;
|
||||
this->shift = 0;
|
||||
this->phase = BITS;
|
||||
if (!this->last_ack) {
|
||||
this->state = IGNORE;
|
||||
} else if (this->state == ADDR && this->is_read) {
|
||||
this->state = RDATA;
|
||||
this->cur = sl.next_read();
|
||||
this->drive_bit();
|
||||
} else {
|
||||
this->state = WDATA;
|
||||
}
|
||||
} else if (this->state == RDATA && this->phase == BITS) {
|
||||
this->bits++;
|
||||
if (this->bits == 8) {
|
||||
this->s_sda_low = false;
|
||||
this->phase = ACK_IN;
|
||||
} else {
|
||||
this->drive_bit();
|
||||
}
|
||||
} else if (this->state == RDATA && this->phase == ACK_IN) {
|
||||
if (this->master_ack) {
|
||||
this->cur = sl.next_read();
|
||||
this->bits = 0;
|
||||
this->phase = BITS;
|
||||
this->drive_bit();
|
||||
} else {
|
||||
this->state = IGNORE;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
};
|
||||
inline BitSlave &bitslave() {
|
||||
static BitSlave b;
|
||||
return b;
|
||||
}
|
||||
|
||||
class FakePin : public esphome::GPIOPin {
|
||||
public:
|
||||
explicit FakePin(bool is_sda) : is_sda_(is_sda) {}
|
||||
void setup() override {}
|
||||
void pin_mode(esphome::gpio::Flags flags) override {
|
||||
this->flags_ = flags;
|
||||
this->apply_();
|
||||
}
|
||||
esphome::gpio::Flags get_flags() const override { return this->flags_; }
|
||||
bool digital_read() override { return this->is_sda_ ? bitslave().sda : bitslave().scl; }
|
||||
void digital_write(bool value) override {
|
||||
this->value_ = value;
|
||||
this->apply_();
|
||||
}
|
||||
size_t dump_summary(char *buffer, size_t len) const override { return 0; }
|
||||
|
||||
protected:
|
||||
void apply_() {
|
||||
bool low = (this->flags_ & esphome::gpio::FLAG_OUTPUT) && !this->value_;
|
||||
(this->is_sda_ ? bitslave().m_sda_low : bitslave().m_scl_low) = low;
|
||||
bitslave().update();
|
||||
}
|
||||
bool is_sda_;
|
||||
bool value_{true};
|
||||
esphome::gpio::Flags flags_{esphome::gpio::FLAG_INPUT};
|
||||
};
|
||||
} // namespace wab
|
||||
|
||||
#define USE_LIBRETINY
|
||||
#define InternalGPIOPin wab::FakePin
|
||||
#include "wab_sw_impl.inc"
|
||||
#undef InternalGPIOPin
|
||||
#undef USE_LIBRETINY
|
||||
|
||||
esphome::i2c::I2CBus *wab_make_sw() {
|
||||
static wab::FakePin sda(true), scl(false);
|
||||
auto *b = new esphome::i2c::SoftwareI2CBus();
|
||||
b->set_sda_pin(&sda);
|
||||
b->set_scl_pin(&scl);
|
||||
b->set_frequency(1000000);
|
||||
b->set_scan(false);
|
||||
b->setup();
|
||||
return b;
|
||||
}
|
||||
void wab_reset_sw() {
|
||||
auto &b = wab::bitslave();
|
||||
b.state = wab::BitSlave::IDLE;
|
||||
b.s_sda_low = false;
|
||||
}
|
||||
@@ -1,74 +0,0 @@
|
||||
"""Readers for the sweep lines of dev/snf.hpp (SN) and ard/swp.hpp (SW), shared by the comparers."""
|
||||
|
||||
from collections.abc import Mapping
|
||||
import pathlib
|
||||
import re
|
||||
|
||||
ANSI = re.compile(r"\x1b\[[0-9;]*m")
|
||||
FIELDS = r"\|rc=(-?\d+)\|alloc=(\d+)\|ev=\d+\|amb=\d+\|ovf=\d+\|fnv=([0-9A-F]+)\|(.*)$"
|
||||
CAPTURES = ("", "2", "3")
|
||||
|
||||
|
||||
def load(path: str | pathlib.Path, tag: str = "SN") -> dict[str, dict]:
|
||||
"""Case label -> rc, alloc, fnv and the rest of the line.
|
||||
|
||||
SN lines get `verdict` (OK or DIFF) and, on a DIFF, the `got` and `exp` token lists of the SNGOT and SNEXP lines
|
||||
that follow. SW lines get `wire` (the decoded stream) and `rd` (read data).
|
||||
"""
|
||||
line_re = re.compile(rf"{tag} (.+?){FIELDS}")
|
||||
cases: dict[str, dict] = {}
|
||||
cur: dict | None = None
|
||||
text = pathlib.Path(path).read_text(encoding="utf-8", errors="replace")
|
||||
for line in text.splitlines():
|
||||
line = ANSI.sub("", line).rstrip()
|
||||
if m := line_re.search(line):
|
||||
label, rc, alloc, fnv, rest = m.groups()
|
||||
cur = cases[label] = {"rc": int(rc), "alloc": int(alloc), "fnv": fnv}
|
||||
if tag == "SW":
|
||||
wire, _, rd = rest.partition("|rd=")
|
||||
cur.update(wire=wire.strip(), rd=rd.strip())
|
||||
else:
|
||||
cur["verdict"] = rest
|
||||
elif cur is not None and (m := re.search(r"SN(GOT|EXP) (.*)$", line)):
|
||||
cur["got" if m.group(1) == "GOT" else "exp"] = m.group(2).split()
|
||||
return cases
|
||||
|
||||
|
||||
def is_read(label: str) -> bool:
|
||||
"""Reads return chip data, so their wire hash changes between runs."""
|
||||
return label.startswith(("rreg", "read", "write1+read"))
|
||||
|
||||
|
||||
def nack_cut(got: list[str], exp: list[str]) -> int | None:
|
||||
"""Index of the token the chip NACKed when `got` is `exp` cut there (then STOP), else None.
|
||||
|
||||
Only address and write tokens count as a chip NACK; a read token ("r..") NACKed early stays a difference.
|
||||
"""
|
||||
j = next(
|
||||
(j for j, t in enumerate(got) if t.endswith("-") and not t.startswith("r")),
|
||||
None,
|
||||
)
|
||||
if (
|
||||
j is not None
|
||||
and got[:j] == exp[:j]
|
||||
and got[j][:-1] == exp[j][:-1]
|
||||
and got[j + 1 :] == ["P"]
|
||||
):
|
||||
return j
|
||||
return None
|
||||
|
||||
|
||||
def runs(name: str) -> list[dict[str, dict]]:
|
||||
"""The captures NAME-ser.txt, NAME-ser2.txt and NAME-ser3.txt that exist (at least one)."""
|
||||
found = [pathlib.Path(f"{name}-ser{s}.txt") for s in CAPTURES]
|
||||
loaded = [load(p) for p in found if p.exists()]
|
||||
if not loaded:
|
||||
raise SystemExit(f"no capture {name}-ser.txt")
|
||||
return loaded
|
||||
|
||||
|
||||
def first_alloc(allocs: Mapping[str, int], shape: str, limit: int = 400) -> int | None:
|
||||
"""First length of `shape` (wreg8, wreg16) whose call allocated; allocs maps case label -> malloc count."""
|
||||
return next(
|
||||
(n for n in range(limit) if allocs.get(f"{shape} len={n}", 0) > 0), None
|
||||
)
|
||||
@@ -1,51 +0,0 @@
|
||||
#pragma once
|
||||
// Wire decoder shared by dev/snf.hpp and ard/swp.hpp. Each sample is a level state: SDA in bit 0, SCL in bit 1.
|
||||
#include <cstdint>
|
||||
#include <cstdio>
|
||||
#include <string>
|
||||
|
||||
namespace wiredecode {
|
||||
// Decodes `count` level states that follow `start` into tokens (`S`, `Sr`, `P`, `90+`, `rA0-`); adds to amb the
|
||||
// samples where both lines changed at once.
|
||||
inline std::string decode(const uint8_t *buf, uint32_t count, uint8_t start, uint32_t &amb) {
|
||||
std::string out;
|
||||
uint8_t prev = start;
|
||||
bool open = false, first = false, reading = false;
|
||||
int bits = -1; // -1: no frame; 0..8 bit index in the current 9-bit unit
|
||||
uint16_t sh = 0;
|
||||
char t[8];
|
||||
for (uint32_t i = 0; i < count; i++) {
|
||||
uint8_t cur = buf[i];
|
||||
bool psda = prev & 1, pscl = prev & 2, sda = cur & 1, scl = cur & 2;
|
||||
if ((psda != sda) && (pscl != scl))
|
||||
amb++;
|
||||
if (pscl && scl && psda && !sda) {
|
||||
out += open ? "Sr " : "S ";
|
||||
open = true;
|
||||
bits = 0;
|
||||
sh = 0;
|
||||
first = true;
|
||||
} else if (pscl && scl && !psda && sda) {
|
||||
out += "P ";
|
||||
open = false;
|
||||
bits = -1;
|
||||
} else if (!pscl && scl && bits >= 0) {
|
||||
if (bits < 8) {
|
||||
sh = (sh << 1) | (sda ? 1 : 0);
|
||||
bits++;
|
||||
} else {
|
||||
uint8_t b = sh & 0xFF;
|
||||
if (first)
|
||||
reading = b & 1;
|
||||
snprintf(t, sizeof t, (!first && reading) ? "r%02X%c " : "%02X%c ", b, sda ? '-' : '+');
|
||||
out += t;
|
||||
first = false;
|
||||
bits = 0;
|
||||
sh = 0;
|
||||
}
|
||||
}
|
||||
prev = cur;
|
||||
}
|
||||
return out;
|
||||
}
|
||||
} // namespace wiredecode
|
||||
@@ -1,45 +0,0 @@
|
||||
#pragma once
|
||||
// Heap allocation counter for test harnesses: counts while alloc_count::window is set. Link flag per platform:
|
||||
// ESP32 and ESP8266 -Wl,--wrap=malloc (on ESP8266 the wrapper must be in IRAM: in flash it crash-loops with
|
||||
// IllegalInst at its first instruction). arduino-pico already wraps malloc and _malloc_r (a second wrap is a duplicate
|
||||
// symbol), so use -Wl,--wrap=__malloc_lock (malloc and free each count once). LibreTiny already wraps malloc into
|
||||
// pvPortMalloc, so use -Wl,--wrap=pvPortMalloc.
|
||||
#include <cstddef>
|
||||
#include <cstdint>
|
||||
#include "esphome/core/hal.h"
|
||||
|
||||
namespace alloc_count {
|
||||
inline volatile bool window = false;
|
||||
inline volatile uint32_t count = 0;
|
||||
inline void note() {
|
||||
if (window)
|
||||
count = count + 1;
|
||||
}
|
||||
} // namespace alloc_count
|
||||
|
||||
#if defined(USE_RP2)
|
||||
extern "C" void __real___malloc_lock(struct _reent *r);
|
||||
extern "C" void __wrap___malloc_lock(struct _reent *r) {
|
||||
alloc_count::note();
|
||||
__real___malloc_lock(r);
|
||||
}
|
||||
#elif defined(USE_LIBRETINY)
|
||||
extern "C" void *__real_pvPortMalloc(size_t n);
|
||||
extern "C" void *__wrap_pvPortMalloc(size_t n) {
|
||||
alloc_count::note();
|
||||
return __real_pvPortMalloc(n);
|
||||
}
|
||||
#elif defined(USE_ESP8266)
|
||||
extern "C" void *__real_malloc(size_t n);
|
||||
extern "C" void *IRAM_ATTR __wrap_malloc(size_t n) {
|
||||
if (alloc_count::window)
|
||||
alloc_count::count = alloc_count::count + 1;
|
||||
return __real_malloc(n);
|
||||
}
|
||||
#else
|
||||
extern "C" void *__real_malloc(size_t n);
|
||||
extern "C" void *__wrap_malloc(size_t n) {
|
||||
alloc_count::note();
|
||||
return __real_malloc(n);
|
||||
}
|
||||
#endif
|
||||
@@ -1,69 +0,0 @@
|
||||
# Fake BMI270 on an in-firmware I2C bus (bmi270fake.hpp). The real i2c: bus keeps codegen happy; the on_boot
|
||||
# at 700 runs after the bus and before the bmi270 setup (DATA) and swaps the device onto the fake.
|
||||
esphome:
|
||||
name: ${name}
|
||||
includes:
|
||||
- alloc_count.hpp
|
||||
- bmi270fake.hpp
|
||||
platformio_options:
|
||||
build_flags:
|
||||
- -Wl,--wrap=malloc
|
||||
- -DBMI_FAKE_WRAP_MALLOC
|
||||
on_boot:
|
||||
- priority: 700
|
||||
then:
|
||||
- lambda: |-
|
||||
bmi270fake::bus.init_bmm(${fake_aux_addr});
|
||||
id(imu)->set_i2c_bus(&bmi270fake::bus);
|
||||
- priority: -100
|
||||
then:
|
||||
- lambda: |-
|
||||
ESP_LOGI("bmi270fake", "BUILD %s imu_failed=%d", "${name}", id(imu)->is_failed());
|
||||
bmi270fake::bus.report();
|
||||
|
||||
logger:
|
||||
level: DEBUG
|
||||
|
||||
wifi:
|
||||
ssid: !secret wifi_ssid
|
||||
password: !secret wifi_password
|
||||
|
||||
api:
|
||||
reboot_timeout: 0s
|
||||
|
||||
i2c:
|
||||
id: i2c_bus
|
||||
sda: ${sda}
|
||||
scl: ${scl}
|
||||
scan: false
|
||||
|
||||
sensor:
|
||||
- platform: bmi270
|
||||
bmi270_id: imu
|
||||
name: "BMI Temperature"
|
||||
- platform: motion
|
||||
type: acceleration_x
|
||||
name: "BMI Accel X"
|
||||
accuracy_decimals: 4
|
||||
- platform: motion
|
||||
type: acceleration_y
|
||||
name: "BMI Accel Y"
|
||||
accuracy_decimals: 4
|
||||
- platform: motion
|
||||
type: acceleration_z
|
||||
name: "BMI Accel Z"
|
||||
accuracy_decimals: 4
|
||||
- platform: motion
|
||||
type: gyroscope_x
|
||||
name: "BMI Gyro X"
|
||||
- platform: motion
|
||||
type: gyroscope_y
|
||||
name: "BMI Gyro Y"
|
||||
- platform: motion
|
||||
type: gyroscope_z
|
||||
name: "BMI Gyro Z"
|
||||
|
||||
interval:
|
||||
- interval: 60s
|
||||
then:
|
||||
- lambda: bmi270fake::bus.report();
|
||||
@@ -1,136 +0,0 @@
|
||||
"""bmi270-host-compare.py BASE.report PR.report: compare every scenario of the harness between base and PR.
|
||||
|
||||
Streams: base's split INIT_ADDR writes (W 5b xx / W 5c yy) are merged before comparing. NACK scenarios are matched by
|
||||
the normalized transaction they hit. LOG lines are compared separately and every difference is printed."""
|
||||
|
||||
import pathlib
|
||||
import re
|
||||
import sys
|
||||
|
||||
ANSI = re.compile(r"\x1b\[[0-9;]*m")
|
||||
|
||||
|
||||
def parse(path):
|
||||
"""Scenario name -> head, LOG, TX and other lines; also the NACK_TOTAL count."""
|
||||
scen, cur, nack_total = {}, None, 0
|
||||
with pathlib.Path(path).open(encoding="utf-8", errors="replace") as fh:
|
||||
for line in fh:
|
||||
line = ANSI.sub("", line.rstrip("\n"))
|
||||
if line.startswith("SCENARIO "):
|
||||
cur = scen.setdefault(
|
||||
line.split()[1], {"head": "", "log": [], "tx": [], "other": []}
|
||||
)
|
||||
elif line.startswith("NACK_TOTAL"):
|
||||
nack_total = int(line.split()[1])
|
||||
elif line.startswith(" failed="):
|
||||
cur["head"] = re.sub(r" setup_tx=\d+", "", line.strip())
|
||||
elif line.startswith(" LOG "):
|
||||
cur["log"].append(line[6:])
|
||||
elif line.startswith(" TX "):
|
||||
cur["tx"].append(line[5:])
|
||||
else:
|
||||
cur["other"].append(
|
||||
re.sub(r" (aux_w|aux_r|status_reads)=\d+", "", line.strip())
|
||||
)
|
||||
return scen, nack_total
|
||||
|
||||
|
||||
def merge(tx):
|
||||
"""Returns the merged list and, per original index (0 based), the merged index."""
|
||||
out, idx, i = [], [], 0
|
||||
while i < len(tx):
|
||||
a = tx[i]
|
||||
if (
|
||||
a.startswith("W 5b ")
|
||||
and len(a.split()) == 3
|
||||
and i + 1 < len(tx)
|
||||
and tx[i + 1].startswith("W 5c ")
|
||||
and len(tx[i + 1].split()) == 3
|
||||
):
|
||||
out.append(a + " " + tx[i + 1].split()[2])
|
||||
idx += [len(out) - 1, len(out) - 1]
|
||||
i += 2
|
||||
continue
|
||||
out.append(a)
|
||||
idx.append(len(out) - 1)
|
||||
i += 1
|
||||
return out, idx
|
||||
|
||||
|
||||
(b, nack_total), (p, _) = parse(sys.argv[1]), parse(sys.argv[2])
|
||||
problems = 0
|
||||
logdiffs = {}
|
||||
for name in sorted(n for n in p if not n.startswith(("nack_", "step_"))):
|
||||
sb, sp = b[name], p[name]
|
||||
same_head = sb["head"] == sp["head"]
|
||||
same_other = sb["other"] == sp["other"]
|
||||
line = f"{name:20s} head={'same' if same_head else 'DIFF'} regs/values/upload={'same' if same_other else 'DIFF'}"
|
||||
if sp["tx"]:
|
||||
mb, _ = merge(sb["tx"])
|
||||
line += f" stream base={len(sb['tx'])}->{len(mb)} pr={len(sp['tx'])} {'same' if mb == sp['tx'] else 'DIFF'}"
|
||||
if mb != sp["tx"]:
|
||||
problems += 1
|
||||
if not (same_head and same_other):
|
||||
problems += 1
|
||||
line += f"\n base: {sb['head']} {sb['other']}\n pr: {sp['head']} {sp['other']}"
|
||||
print(line)
|
||||
if sb["log"] != sp["log"]:
|
||||
key = (
|
||||
tuple(x for x in sb["log"] if x not in sp["log"]),
|
||||
tuple(x for x in sp["log"] if x not in sb["log"]),
|
||||
)
|
||||
logdiffs.setdefault(key, []).append(name)
|
||||
|
||||
# NACK sweep, matched through the merged aux stream; indexes count main-bus transactions only (the fake numbers those)
|
||||
main_b = [t for t in b["cfg_aux"]["tx"] if not t.startswith("A ")]
|
||||
main_p = [t for t in p["cfg_aux"]["tx"] if not t.startswith("A ")]
|
||||
mmb, midx = merge(main_b)
|
||||
assert mmb == main_p, "main streams differ"
|
||||
nack_outcomes = {}
|
||||
for i in range(nack_total):
|
||||
k = midx[i]
|
||||
sb, sp = b[f"nack_{i + 1}"], p[f"nack_{k + 1}"]
|
||||
hb = (sb["head"], tuple(sb["other"]))
|
||||
hp = (sp["head"], tuple(sp["other"]))
|
||||
if hb != hp:
|
||||
problems += 1
|
||||
print(
|
||||
f"NACK base tx {i + 1} / pr tx {k + 1} ({main_p[k][:20]}): DIFF\n base {hb}\n pr {hp}"
|
||||
)
|
||||
wb = [x for x in sb["log"] if x[:1] in "EW"]
|
||||
wp = [x for x in sp["log"] if x[:1] in "EW"]
|
||||
nack_outcomes.setdefault((sp["head"].split(" status")[0], tuple(wp)), []).append(
|
||||
k + 1
|
||||
)
|
||||
if wb != wp:
|
||||
key = (tuple(x for x in wb if x not in wp), tuple(x for x in wp if x not in wb))
|
||||
logdiffs.setdefault(key, []).append(f"nack_pr{k + 1}")
|
||||
print(
|
||||
f"NACK scenarios checked: {nack_total} base transactions -> {len(main_p)} pr transactions"
|
||||
)
|
||||
print("PR NACK outcome classes (head, E/W log lines): pr transaction numbers")
|
||||
for (h, w), ks in sorted(nack_outcomes.items(), key=lambda x: x[1][0]):
|
||||
ks = sorted(set(ks))
|
||||
print(f" {h} | {' / '.join(w)} | tx {ks[0]}..{ks[-1]} ({len(ks)})")
|
||||
print("BMM150 step failures (failed/mag_ready, values, W lines): base vs PR")
|
||||
for name in sorted(n for n in p if n.startswith("step_")):
|
||||
sb, sp = b[name], p[name]
|
||||
vb = [x for x in sb["other"] if x.startswith(("REGS", "VALUES"))]
|
||||
vp = [x for x in sp["other"] if x.startswith(("REGS", "VALUES"))]
|
||||
if sb["head"] != sp["head"] or vb != vp:
|
||||
problems += 1
|
||||
print(
|
||||
f" {name}: DIFF head/regs/values\n base {sb['head']} {vb}\n pr {sp['head']} {vp}"
|
||||
)
|
||||
print(
|
||||
f" {name:32s} {sp['head'].split(' status')[0]} | mag {[x for x in vp if x.startswith('VALUES')][0].split(' t=')[1] if vp else '-'}"
|
||||
)
|
||||
print(f" base W: {[x for x in sb['log'] if x[:1] in 'EW']}")
|
||||
print(f" pr W: {[x for x in sp['log'] if x[:1] in 'EW']}")
|
||||
print("LOG differences (only in base | only in PR): scenarios")
|
||||
for (ob, op), names in logdiffs.items():
|
||||
names = sorted(set(names))
|
||||
print(
|
||||
f" base-only {list(ob)} | pr-only {list(op)} : {len(names)} e.g. {names[:4]}"
|
||||
)
|
||||
print("RESULT", "SAME" if problems == 0 else f"DIFFERENT ({problems})")
|
||||
@@ -1,203 +0,0 @@
|
||||
#ifdef USE_HOST
|
||||
// Host harness for esphome#22778. Runs BMI270Component::setup() and update_data() against bmi270fake::Bus for every
|
||||
// scenario and writes one report to $BMI_OUT: per scenario the outcome (failed, error reason, warning, magnetometer
|
||||
// ready), the log lines, the final registers, decoded values and the raw transaction stream.
|
||||
// $BMI_EXPECTED_FNV (hex, optional) overrides the fake's expected config table hash.
|
||||
// Scenarios: configs from $BMI_SETTERS (lines "name setter value", parsed from each tree's generated main.cpp),
|
||||
// aux faults, and a NACK at every main-bus transaction of the aux config.
|
||||
#include <gtest/gtest.h>
|
||||
|
||||
#include <cmath>
|
||||
#include <cstdio>
|
||||
#include <cstdlib>
|
||||
#include <fstream>
|
||||
#include <map>
|
||||
#include <memory>
|
||||
#include <sstream>
|
||||
#include <string>
|
||||
#include <vector>
|
||||
#ifndef USE_BMI270_BMM150
|
||||
#error "build with -DUSE_BMI270_BMM150 (benchmark.yaml build_flags)"
|
||||
#endif
|
||||
#ifndef USE_LOG_LISTENERS
|
||||
#error "build with -DUSE_LOG_LISTENERS"
|
||||
#endif
|
||||
#define protected public // read magnetometer_ready_ and call update_data(); harness only
|
||||
#include "esphome/components/bmi270/bmi270.h"
|
||||
#undef protected
|
||||
#include "esphome/components/bmi270/bmi270_config.h"
|
||||
#include "esphome/components/logger/logger.h"
|
||||
#include "bmi270fake.hpp"
|
||||
|
||||
namespace esphome::bmi270::testing {
|
||||
|
||||
static std::vector<std::string> g_lines;
|
||||
|
||||
struct Setter {
|
||||
std::string fn;
|
||||
int value;
|
||||
};
|
||||
using SetterMap = std::map<std::string, std::vector<Setter>>;
|
||||
|
||||
static SetterMap load_setters() {
|
||||
SetterMap m;
|
||||
std::ifstream f(std::getenv("BMI_SETTERS"));
|
||||
std::string name, fn;
|
||||
int v;
|
||||
while (f >> name >> fn >> v)
|
||||
m[name].push_back({fn, v});
|
||||
return m;
|
||||
}
|
||||
|
||||
static void apply(BMI270Component &imu, const std::vector<Setter> &setters) {
|
||||
for (auto &s : setters) {
|
||||
if (s.fn == "set_accel_range")
|
||||
imu.set_accel_range((BMI270AccelRange) s.value);
|
||||
else if (s.fn == "set_accel_odr")
|
||||
imu.set_accel_odr((BMI270AccelODR) s.value);
|
||||
else if (s.fn == "set_gyro_range")
|
||||
imu.set_gyro_range((BMI270GyroRange) s.value);
|
||||
else if (s.fn == "set_gyro_odr")
|
||||
imu.set_gyro_odr((BMI270GyroODR) s.value);
|
||||
else if (s.fn == "set_aux_device")
|
||||
imu.set_aux_device((BMI270AuxDevice) s.value);
|
||||
else if (s.fn == "set_aux_device_address")
|
||||
imu.set_aux_device_address(s.value);
|
||||
else if (s.fn != "none")
|
||||
ADD_FAILURE() << "unknown setter " << s.fn;
|
||||
}
|
||||
}
|
||||
|
||||
struct Result {
|
||||
std::string text;
|
||||
uint32_t tx;
|
||||
std::vector<std::string> txlog;
|
||||
};
|
||||
|
||||
// aux_addr: the address the fake BMM150 answers on
|
||||
static Result run(const std::string &label, const std::vector<Setter> &setters, uint8_t aux_addr, uint8_t bmm_mode,
|
||||
bool busy_stuck, uint32_t nack_at, bool full, uint8_t stuck_from = 0) {
|
||||
auto bus = std::make_unique<bmi270fake::Bus>();
|
||||
if (const char *fnv = std::getenv("BMI_EXPECTED_FNV"))
|
||||
bus->expected_fnv = std::strtoul(fnv, nullptr, 16);
|
||||
bus->init_bmm(aux_addr, bmm_mode);
|
||||
bus->busy_stuck = busy_stuck;
|
||||
bus->nack_at = nack_at;
|
||||
bus->stuck_from_aux_op = stuck_from;
|
||||
BMI270Component imu;
|
||||
imu.set_i2c_bus(bus.get());
|
||||
imu.set_i2c_address(0x68);
|
||||
apply(imu, setters);
|
||||
float temp = NAN;
|
||||
bmm150::BMM150Data mag{NAN, NAN, NAN};
|
||||
imu.add_temperature_listener([&temp](float t) { temp = t; });
|
||||
imu.add_magnetometer_listener([&mag](const bmm150::BMM150Data &m) { mag = m; });
|
||||
g_lines.clear();
|
||||
imu.setup();
|
||||
const uint32_t setup_tx = bus->transactions;
|
||||
motion::MotionData d{};
|
||||
bool sampled = imu.update_data(d);
|
||||
std::ostringstream o;
|
||||
char fr[240];
|
||||
bus->final_regs(fr, sizeof(fr));
|
||||
o << "SCENARIO " << label << "\n";
|
||||
o << " failed=" << imu.is_failed() << " warning=" << imu.status_has_warning()
|
||||
<< " error=" << imu.status_has_error() << " mag_ready=" << imu.magnetometer_ready_ << " setup_tx=" << setup_tx
|
||||
<< " status=0x" << std::hex << (int) bus->regs[0x21] << std::dec << "\n";
|
||||
for (auto &l : g_lines)
|
||||
o << " LOG " << l << "\n";
|
||||
if (full || !imu.is_failed()) {
|
||||
o << " REGS " << fr << "\n";
|
||||
char vals[300];
|
||||
snprintf(vals, sizeof(vals),
|
||||
" VALUES sampled=%d ax=%.6f ay=%.6f az=%.6f gx=%.5f gy=%.5f gz=%.5f t=%.4f mx=%.6f my=%.6f mz=%.6f\n",
|
||||
sampled, d.acceleration[0], d.acceleration[1], d.acceleration[2], d.angular_rate[0], d.angular_rate[1],
|
||||
d.angular_rate[2], temp, mag.x, mag.y, mag.z);
|
||||
o << vals;
|
||||
o << " UPLOAD mem_fnv=" << std::hex << bus->mem_fnv << std::dec << " rewrites=" << bus->rewrites
|
||||
<< " perr=" << bus->protocol_errors << " data_writes=" << bus->data_writes << " burst=" << bus->burst_min << ".."
|
||||
<< bus->burst_max << " aux_w=" << bus->aux_writes << " aux_r=" << bus->aux_reads
|
||||
<< " status_reads=" << bus->status_reads << " norm=" << std::hex << bus->norm_fnv << std::dec << "\n";
|
||||
}
|
||||
if (full) {
|
||||
for (auto &l : bus->log)
|
||||
o << " TX " << l << "\n";
|
||||
std::string memp = std::string(std::getenv("BMI_OUT")) + "." + label + ".mem";
|
||||
FILE *f = std::fopen(memp.c_str(), "wb");
|
||||
std::fwrite(bus->mem, 1, sizeof(bus->mem), f);
|
||||
std::fclose(f);
|
||||
}
|
||||
return {o.str(), setup_tx, bus->log};
|
||||
}
|
||||
|
||||
TEST(BMI270Harness22778, AllScenarios) {
|
||||
ASSERT_NE(std::getenv("BMI_OUT"), nullptr);
|
||||
logger::global_logger->add_log_callback(nullptr, [](void *, uint8_t level, const char *tag, const char *msg,
|
||||
size_t len) {
|
||||
std::string s(msg, len);
|
||||
// drop the colour/header prefix up to the tag's closing bracket
|
||||
auto p = s.find("]: ");
|
||||
g_lines.push_back(std::string(1, "NEWICDVV"[level & 7]) + " " + tag + ": " +
|
||||
(p == std::string::npos ? s : s.substr(p + 3)));
|
||||
});
|
||||
SetterMap setters = load_setters();
|
||||
ASSERT_FALSE(setters.empty());
|
||||
std::ofstream out(std::string(std::getenv("BMI_OUT")) + ".report");
|
||||
uint32_t aux_tx = 0;
|
||||
std::vector<std::string> aux_log;
|
||||
for (auto &[name, list] : setters) {
|
||||
uint8_t aux_addr = 0x10;
|
||||
for (auto &s : list)
|
||||
if (s.fn == "set_aux_device_address")
|
||||
aux_addr = s.value;
|
||||
auto r = run("cfg_" + name, list, aux_addr, 0, false, 0, true);
|
||||
out << r.text;
|
||||
if (name == "aux") {
|
||||
aux_tx = r.tx;
|
||||
aux_log = r.txlog;
|
||||
}
|
||||
}
|
||||
ASSERT_NE(aux_tx, 0u);
|
||||
const auto &aux = setters["aux"];
|
||||
out << run("aux_absent", aux, 0x10, 1, false, 0, false).text;
|
||||
out << run("aux_wrong_id", aux, 0x10, 2, false, 0, false).text;
|
||||
out << run("aux_busy_stuck", aux, 0x10, 0, true, 0, false).text;
|
||||
out << run("aux_wrong_address", aux, 0x11, 0, false, 0, false).text;
|
||||
// One failure per BMM150 setup step: a NACK on the main-bus write of that step, or the aux busy bit stuck from
|
||||
// the step's aux transfer on (1 power-on, 2 soft reset, 3 chip ID read, 4-6 trim reads, 7 normal mode)
|
||||
auto main_index = [&](const std::string &prefix) -> uint32_t {
|
||||
uint32_t n = 0;
|
||||
for (auto &l : aux_log) {
|
||||
if (l.rfind("A ", 0) == 0)
|
||||
continue;
|
||||
n++;
|
||||
if (l.rfind(prefix, 0) == 0 && n > 60) // after the config upload
|
||||
return n;
|
||||
}
|
||||
ADD_FAILURE() << "no transaction " << prefix;
|
||||
return 0;
|
||||
};
|
||||
const std::pair<const char *, const char *> nack_steps[] = {
|
||||
{"step_aux_if_conf_nack", "W 6b 20"}, {"step_aux_manual_nack", "W 4c 8c"},
|
||||
{"step_aux_dev_id_nack", "W 4b 20"}, {"step_power_on_data_nack", "W 4f 01"},
|
||||
{"step_soft_reset_data_nack", "W 4f 83"}, {"step_chip_id_rd_addr_nack", "W 4d 40"},
|
||||
{"step_trim_rd_addr_nack", "W 4d 5d"}, {"step_normal_mode_data_nack", "W 4f 38"},
|
||||
{"step_auto_burst_conf_nack", "W 4c 4f"}, {"step_auto_burst_rd_addr_nack", "W 4d 42"},
|
||||
{"step_auto_burst_pwr_ctrl_nack", "W 7d 0f"},
|
||||
};
|
||||
for (auto &[label, prefix] : nack_steps)
|
||||
out << run(label, aux, 0x10, 0, false, main_index(prefix), false).text;
|
||||
// the soft reset writes the same AUX_WR_ADDR as power-on; the second "W 4e 4b" is the soft reset one
|
||||
const char *stuck_labels[] = {"step_power_on_busy", "step_soft_reset_busy", "step_chip_id_read_busy",
|
||||
"step_trim1_busy", "step_trim2_busy", "step_trim3_busy",
|
||||
"step_normal_mode_busy"};
|
||||
for (uint8_t op = 1; op <= 7; op++)
|
||||
out << run(stuck_labels[op - 1], aux, 0x10, 0, false, 0, false, op).text;
|
||||
out << run("step_chip_id_wrong", aux, 0x10, 2, false, 0, false).text;
|
||||
for (uint32_t n = 1; n <= aux_tx; n++)
|
||||
out << run("nack_" + std::to_string(n), aux, 0x10, 0, false, n, false).text;
|
||||
out << "NACK_TOTAL " << aux_tx << "\n";
|
||||
}
|
||||
|
||||
} // namespace esphome::bmi270::testing
|
||||
#endif
|
||||
@@ -1,18 +0,0 @@
|
||||
#!/bin/bash
|
||||
# bmi270-host-run.sh TREE OUTNAME SETTERS [32]: host harness run; with 32, forces the 32 byte burst branch (perl, in
|
||||
# place; the original is saved first and put back on exit, even after an interrupted run). Writes OUT_DIR/OUTNAME and OUT_DIR/OUTNAME.log.
|
||||
# env: OUT_DIR (default ./host), PY (default TREE's venv python), PIO_CORE (default ./pio)
|
||||
T=$(cd "${1:?tree}" && pwd); N=${2:?outname}; ST=$(cd "$(dirname "${3:?setters}")" && pwd)/$(basename "$3")
|
||||
F=$T/esphome/components/bmi270/bmi270.cpp
|
||||
O=$(mkdir -p "${OUT_DIR:-host}" && cd "${OUT_DIR:-host}" && pwd); PY=${PY:-$T/venv/bin/python}
|
||||
PC=$(mkdir -p "${PIO_CORE:-pio}" && cd "${PIO_CORE:-pio}" && pwd)
|
||||
if [ "$4" = 32 ]; then
|
||||
cp "$F" "$O/$N.bmi270.cpp.orig"
|
||||
trap 'cp "$O/$N.bmi270.cpp.orig" "$F"' EXIT
|
||||
perl -pi -e 's{^#if defined\(USE_ARDUINO\) && !defined\(USE_ESP32\)$}{#if 1 // harness: force 32}' "$F"
|
||||
grep -q '^#if 1 // harness' "$F" || { echo no-sed; exit 1; }
|
||||
fi
|
||||
(cd "$T" && PLATFORMIO_CORE_DIR=$PC PYTHONPATH=$T BMI_OUT=$O/$N BMI_SETTERS=$ST \
|
||||
timeout 2400 $PY script/cpp_unit_test.py bmi270 > "$O/$N.log" 2>&1); rc=$?; echo "$N rc=$rc"
|
||||
grep -a -E '^\[ (PASSED|FAILED) |BMM150_SWEEP' "$O/$N.log"
|
||||
exit $rc
|
||||
@@ -1,20 +0,0 @@
|
||||
motion:
|
||||
- platform: bmi270
|
||||
id: imu
|
||||
i2c_id: i2c_bus
|
||||
update_interval: 5s
|
||||
aux_device: BMM150
|
||||
|
||||
sensor:
|
||||
- platform: bmi270
|
||||
bmi270_id: imu
|
||||
type: magnetic_field_x
|
||||
name: "BMI Mag X"
|
||||
- platform: bmi270
|
||||
bmi270_id: imu
|
||||
type: magnetic_field_y
|
||||
name: "BMI Mag Y"
|
||||
- platform: bmi270
|
||||
bmi270_id: imu
|
||||
type: magnetic_field_z
|
||||
name: "BMI Mag Z"
|
||||
@@ -1,26 +0,0 @@
|
||||
"""setters.py TREE OUT NAME=main.cpp...: write 'name setter value' lines for the harness from generated main.cpp."""
|
||||
|
||||
import pathlib
|
||||
import re
|
||||
import sys
|
||||
|
||||
tree, out = sys.argv[1], sys.argv[2]
|
||||
h = pathlib.Path(f"{tree}/esphome/components/bmi270/bmi270.h").read_text(
|
||||
encoding="utf-8"
|
||||
)
|
||||
enums = {
|
||||
m[0]: int(m[1], 0)
|
||||
for m in re.findall(r"(BMI270_[A-Z0-9_]+) = (0x[0-9A-Fa-f]+|\d+)", h)
|
||||
}
|
||||
with pathlib.Path(out).open("w", encoding="utf-8") as f:
|
||||
for arg in sys.argv[3:]:
|
||||
name, path = arg.split("=")
|
||||
calls = re.findall(
|
||||
r"imu->(set_(?:accel|gyro|aux)\w*)\((?:bmi270::)?(\w+)\);",
|
||||
pathlib.Path(path).read_text(encoding="utf-8"),
|
||||
)
|
||||
if not calls:
|
||||
f.write(f"{name} none 0\n")
|
||||
f.writelines(
|
||||
f"{name} {fn} {enums[v] if v in enums else int(v, 0)}\n" for fn, v in calls
|
||||
)
|
||||
@@ -1,364 +0,0 @@
|
||||
#pragma once
|
||||
// Fake BMI270 (+ BMM150 behind its aux interface) on an in-firmware I2C bus (esphome#22778).
|
||||
// BMI270: register file with reset values, soft reset, config upload port (INIT_ADDR_0/1 word address, INIT_DATA
|
||||
// streaming into 8 KB), INTERNAL_STATUS 0x01 only when INIT_CTRL=1 arrives with every config byte written once and
|
||||
// the memory's FNV-1a equal to `expected_fnv` (default: the current table, computed on the host from
|
||||
// bmi270_config.h), 0x02 otherwise.
|
||||
// Aux interface: AUX_DEV_ID selects the BMM150 (addr << 1); in manual mode (AUX_IF_CONF bit 7) a write to
|
||||
// AUX_WR_ADDR writes AUX_WR_DATA to the BMM150 and a write to AUX_RD_ADDR reads man_rd_burst (1/2/6/8) bytes into
|
||||
// AUX_X_LSB..; either sets STATUS.aux_busy for `busy_reads` STATUS reads. In auto mode with aux enabled the AUX data
|
||||
// registers mirror the BMM150 from AUX_RD_ADDR. Modes: bmm_mode 0 ok, 1 absent (aux transfers do nothing),
|
||||
// 2 wrong chip id; busy_stuck keeps aux_busy set (stuck_from_aux_op: from the Nth aux transfer); nack_at fails main-bus
|
||||
// transaction N (1 based). `norm_fnv` hashes the register-level effect of the stream (split or merged writes hash the
|
||||
// same). Heap is sampled at INIT_CTRL=0/1 and inside every INIT_DATA burst; -DBMI_FAKE_WRAP_MALLOC with the link flag
|
||||
// from alloc_count.hpp counts malloc() during the upload.
|
||||
#include <cstdint>
|
||||
#include <cstdio>
|
||||
#include <cstring>
|
||||
#include "esphome/components/i2c/i2c_bus.h"
|
||||
#include "esphome/core/log.h"
|
||||
#ifdef USE_HOST
|
||||
#include <string>
|
||||
#include <vector>
|
||||
#endif
|
||||
#ifdef USE_ESP8266
|
||||
#include <Esp.h>
|
||||
#elif defined(USE_ESP32)
|
||||
#include <esp_heap_caps.h>
|
||||
#endif
|
||||
|
||||
#ifdef BMI_FAKE_WRAP_MALLOC
|
||||
#include "alloc_count.hpp"
|
||||
#endif
|
||||
|
||||
namespace bmi270fake {
|
||||
static const char *const TAG = "bmi270fake";
|
||||
static constexpr uint8_t ADDR = 0x68;
|
||||
static constexpr size_t MEM = 8192;
|
||||
static constexpr uint8_t R_CHIP_ID = 0x00, R_STATUS = 0x03, R_AUX_X = 0x04, R_DATA_8 = 0x0C, R_INTERNAL_STATUS = 0x21,
|
||||
R_TEMP_0 = 0x22, R_AUX_DEV_ID = 0x4B, R_AUX_IF_CONF = 0x4C, R_AUX_RD_ADDR = 0x4D,
|
||||
R_AUX_WR_ADDR = 0x4E, R_AUX_WR_DATA = 0x4F, R_INIT_CTRL = 0x59, R_INIT_ADDR_0 = 0x5B,
|
||||
R_INIT_ADDR_1 = 0x5C, R_INIT_DATA = 0x5E, R_IF_CONF = 0x6B, R_PWR_CONF = 0x7C,
|
||||
R_PWR_CTRL = 0x7D, R_CMD = 0x7E;
|
||||
|
||||
inline uint32_t heap_free() {
|
||||
#ifdef USE_ESP8266
|
||||
return ESP.getFreeHeap();
|
||||
#elif defined(USE_ESP32)
|
||||
return heap_caps_get_free_size(MALLOC_CAP_INTERNAL);
|
||||
#else
|
||||
return 0;
|
||||
#endif
|
||||
}
|
||||
inline uint32_t heap_block() {
|
||||
#ifdef USE_ESP8266
|
||||
return ESP.getMaxFreeBlockSize();
|
||||
#elif defined(USE_ESP32)
|
||||
return heap_caps_get_largest_free_block(MALLOC_CAP_INTERNAL);
|
||||
#else
|
||||
return 0;
|
||||
#endif
|
||||
}
|
||||
|
||||
struct Bus final : public esphome::i2c::I2CBus {
|
||||
uint8_t regs[128]{};
|
||||
uint8_t mem[MEM]{};
|
||||
uint8_t written[MEM / 8]{};
|
||||
uint8_t bmm[128]{}; // BMM150 registers 0x00..0x7F
|
||||
uint8_t bmm_addr{0x10};
|
||||
uint8_t bmm_mode{0};
|
||||
bool busy_stuck{false};
|
||||
uint8_t stuck_from_aux_op{0}; // aux busy never clears from this aux transfer on (1 based), 0 = off
|
||||
uint32_t aux_ops{0};
|
||||
uint8_t busy_reads{2}, busy_left{0};
|
||||
uint32_t nack_at{0};
|
||||
uint32_t expected_fnv{0x95BB75B5}; // FNV-1a of the 8192 byte table in bmi270_config.h
|
||||
uint32_t mem_fnv{0}, norm_fnv{2166136261u};
|
||||
uint32_t transactions{0}, reads{0}, writes{0}, data_writes{0}, data_bytes{0}, addr_writes{0}, rewrites{0};
|
||||
uint32_t protocol_errors{0}, loads_ok{0}, loads_bad{0}, resets{0}, aux_writes{0}, aux_reads{0}, status_reads{0};
|
||||
uint32_t burst_min{UINT32_MAX}, burst_max{0};
|
||||
uint32_t heap_start{0}, block_start{0}, heap_end{0}, block_end{0}, heap_min_in_burst{UINT32_MAX};
|
||||
uint32_t bursts_heap_below_start{0};
|
||||
// accel x/y/z, gyro x/y/z (LE int16 at 0x0C..0x17), temperature raw at 0x22
|
||||
int16_t sample[6]{4096, -8192, 12288, 1638, -3277, 819};
|
||||
int16_t temp_raw{1024};
|
||||
// BMM150 raw data 0x42..0x49 and trim 0x5D..0x74 (datasheet-like values)
|
||||
uint8_t mag_raw[8]{0x10, 0x12, 0x20, 0xF0, 0x30, 0x08, 0x40, 0x1A};
|
||||
// x1=-2 y1=3 z4=-10 x2=26 y2=27 z2=760 z1=24786 xyz1=7101 z3=64 xy2=-3 xy1=29
|
||||
uint8_t trim[24]{0xFE, 0x03, 0x00, 0x00, 0x00, 0xF6, 0xFF, 0x1A, 0x1B, 0x00, 0x00, 0xF8,
|
||||
0x02, 0xD2, 0x60, 0xBD, 0x1B, 0x40, 0x00, 0xFD, 0x1D, 0x00, 0x00, 0x00};
|
||||
#ifdef USE_HOST
|
||||
std::vector<std::string> log; // raw transaction stream, plus "A" lines for BMM150 side effects
|
||||
#endif
|
||||
|
||||
Bus() {
|
||||
this->reset_();
|
||||
this->bmm_reset_(true);
|
||||
}
|
||||
|
||||
// Puts the BMM150 on `addr` with fault mode `mode` and powers it off
|
||||
void init_bmm(uint8_t addr, uint8_t mode = 0) {
|
||||
this->bmm_addr = addr;
|
||||
this->bmm_mode = mode;
|
||||
this->bmm_reset_(true);
|
||||
}
|
||||
|
||||
void reset_() {
|
||||
memset(this->regs, 0, sizeof(this->regs));
|
||||
this->regs[R_CHIP_ID] = 0x24;
|
||||
this->regs[R_PWR_CONF] = 0x03;
|
||||
this->regs[R_AUX_DEV_ID] = 0x20;
|
||||
this->regs[R_AUX_IF_CONF] = 0x83;
|
||||
this->regs[R_AUX_RD_ADDR] = 0x42;
|
||||
this->regs[R_AUX_WR_ADDR] = 0x4C;
|
||||
this->regs[R_AUX_WR_DATA] = 0x02;
|
||||
memset(this->mem, 0, sizeof(this->mem));
|
||||
memset(this->written, 0, sizeof(this->written));
|
||||
this->busy_left = 0;
|
||||
}
|
||||
void bmm_reset_(bool power_off) {
|
||||
uint8_t pwr = this->bmm[0x4B] & 0x01;
|
||||
memset(this->bmm, 0, sizeof(this->bmm));
|
||||
this->bmm[0x40] = this->bmm_mode == 2 ? 0x33 : 0x32;
|
||||
this->bmm[0x4B] = power_off ? 0 : pwr;
|
||||
this->bmm[0x4C] = 0x06; // sleep mode
|
||||
memcpy(&this->bmm[0x42], this->mag_raw, 8);
|
||||
memcpy(&this->bmm[0x5D], this->trim, sizeof(this->trim));
|
||||
}
|
||||
uint8_t bmm_read_(uint8_t r) {
|
||||
if (r == 0x40 && !(this->bmm[0x4B] & 0x01))
|
||||
return 0x00; // chip id reads 0 in suspend
|
||||
if (r >= 0x42 && r <= 0x49)
|
||||
return this->mag_raw[r - 0x42];
|
||||
return this->bmm[r & 0x7F];
|
||||
}
|
||||
void mix_(uint32_t v) {
|
||||
for (int i = 0; i < 4; i++) {
|
||||
this->norm_fnv ^= (v >> (8 * i)) & 0xFF;
|
||||
this->norm_fnv *= 16777619u;
|
||||
}
|
||||
}
|
||||
static uint32_t fnv(const uint8_t *d, size_t n) {
|
||||
uint32_t h = 2166136261u;
|
||||
for (size_t i = 0; i < n; i++) {
|
||||
h ^= d[i];
|
||||
h *= 16777619u;
|
||||
}
|
||||
return h;
|
||||
}
|
||||
uint32_t covered_() const {
|
||||
uint32_t n = 0;
|
||||
for (uint8_t b : this->written)
|
||||
n += __builtin_popcount(b);
|
||||
return n;
|
||||
}
|
||||
void note_(const char *fmt, unsigned a, unsigned b) {
|
||||
#ifdef USE_HOST
|
||||
char s[48];
|
||||
snprintf(s, sizeof(s), fmt, a, b);
|
||||
this->log.push_back(s);
|
||||
#endif
|
||||
}
|
||||
bool aux_present_() const {
|
||||
return this->bmm_mode != 1 && (this->regs[R_IF_CONF] & 0x20) && this->regs[R_AUX_DEV_ID] == (this->bmm_addr << 1);
|
||||
}
|
||||
void aux_trigger_() {
|
||||
this->aux_ops++;
|
||||
if (this->stuck_from_aux_op && this->aux_ops >= this->stuck_from_aux_op)
|
||||
this->busy_stuck = true;
|
||||
this->busy_left = this->busy_reads;
|
||||
}
|
||||
void fill_data_() {
|
||||
for (int i = 0; i < 6; i++) {
|
||||
this->regs[R_DATA_8 + 2 * i] = this->sample[i] & 0xFF;
|
||||
this->regs[R_DATA_8 + 2 * i + 1] = (uint16_t) this->sample[i] >> 8;
|
||||
}
|
||||
this->regs[R_TEMP_0] = this->temp_raw & 0xFF;
|
||||
this->regs[R_TEMP_0 + 1] = (uint16_t) this->temp_raw >> 8;
|
||||
// auto mode: the BMI270 keeps AUX_X.. filled from the BMM150
|
||||
if (!(this->regs[R_AUX_IF_CONF] & 0x80) && (this->regs[R_PWR_CTRL] & 0x01) && this->aux_present_()) {
|
||||
for (int i = 0; i < 8; i++)
|
||||
this->regs[R_AUX_X + i] = this->bmm_read_(this->regs[R_AUX_RD_ADDR] + i);
|
||||
}
|
||||
}
|
||||
|
||||
void write_reg_(uint8_t reg, uint8_t v) {
|
||||
this->mix_(0x01000000u | (reg << 8) | v);
|
||||
if (reg == R_CMD && v == 0xB6) {
|
||||
this->resets++;
|
||||
this->reset_();
|
||||
return;
|
||||
}
|
||||
if (reg == R_INIT_ADDR_0 || reg == R_INIT_ADDR_1)
|
||||
this->addr_writes++;
|
||||
if (reg == R_INIT_CTRL) {
|
||||
if (v == 0x00) {
|
||||
this->heap_start = heap_free();
|
||||
this->block_start = heap_block();
|
||||
this->heap_min_in_burst = UINT32_MAX;
|
||||
this->bursts_heap_below_start = 0;
|
||||
this->regs[R_INTERNAL_STATUS] = 0x00;
|
||||
#ifdef BMI_FAKE_WRAP_MALLOC
|
||||
alloc_count::count = 0;
|
||||
alloc_count::window = true;
|
||||
#endif
|
||||
} else if (v == 0x01) {
|
||||
#ifdef BMI_FAKE_WRAP_MALLOC
|
||||
alloc_count::window = false;
|
||||
#endif
|
||||
this->heap_end = heap_free();
|
||||
this->block_end = heap_block();
|
||||
this->mem_fnv = fnv(this->mem, MEM);
|
||||
bool ok = this->mem_fnv == this->expected_fnv && this->covered_() == MEM && this->rewrites == 0;
|
||||
this->regs[R_INTERNAL_STATUS] = ok ? 0x01 : 0x02;
|
||||
(ok ? this->loads_ok : this->loads_bad)++;
|
||||
}
|
||||
}
|
||||
this->regs[reg & 0x7F] = v;
|
||||
bool manual = this->regs[R_AUX_IF_CONF] & 0x80;
|
||||
if (reg == R_AUX_WR_ADDR && manual) {
|
||||
this->aux_writes++;
|
||||
this->aux_trigger_();
|
||||
if (this->aux_present_()) {
|
||||
uint8_t d = this->regs[R_AUX_WR_DATA];
|
||||
this->note_("A W %02x %02x", v, d);
|
||||
this->mix_(0x04000000u | (v << 8) | d);
|
||||
if (v == 0x4B && (d & 0x82) == 0x82)
|
||||
this->bmm_reset_(false);
|
||||
else
|
||||
this->bmm[v & 0x7F] = d;
|
||||
}
|
||||
} else if (reg == R_AUX_RD_ADDR && manual) {
|
||||
this->aux_reads++;
|
||||
this->aux_trigger_();
|
||||
if (this->aux_present_()) {
|
||||
static const uint8_t LEN[4] = {1, 2, 6, 8};
|
||||
uint8_t n = LEN[(this->regs[R_AUX_IF_CONF] >> 2) & 3];
|
||||
this->note_("A R %02x n%u", v, n);
|
||||
this->mix_(0x05000000u | (v << 8) | n);
|
||||
for (uint8_t i = 0; i < n; i++)
|
||||
this->regs[R_AUX_X + i] = this->bmm_read_(v + i);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void init_data_(const uint8_t *d, size_t n) {
|
||||
this->data_writes++;
|
||||
this->data_bytes += n;
|
||||
if (n < this->burst_min)
|
||||
this->burst_min = n;
|
||||
if (n > this->burst_max)
|
||||
this->burst_max = n;
|
||||
uint32_t h = heap_free();
|
||||
if (h < this->heap_min_in_burst)
|
||||
this->heap_min_in_burst = h;
|
||||
if (h < this->heap_start)
|
||||
this->bursts_heap_below_start++;
|
||||
if ((this->regs[R_PWR_CONF] & 0x01) || this->regs[R_INIT_CTRL] != 0x00) {
|
||||
this->protocol_errors++;
|
||||
return;
|
||||
}
|
||||
size_t off = 2 * ((this->regs[R_INIT_ADDR_0] & 0x0F) | ((size_t) this->regs[R_INIT_ADDR_1] << 4));
|
||||
for (size_t i = 0; i < n; i++, off++) {
|
||||
if (off >= MEM) {
|
||||
this->protocol_errors++;
|
||||
return;
|
||||
}
|
||||
this->mix_(0x02000000u | (off << 8) | d[i]);
|
||||
if (this->written[off / 8] & (1 << (off % 8)))
|
||||
this->rewrites++;
|
||||
this->written[off / 8] |= 1 << (off % 8);
|
||||
this->mem[off] = d[i];
|
||||
}
|
||||
}
|
||||
|
||||
esphome::i2c::ErrorCode write_readv(uint8_t address, const uint8_t *data, size_t size, uint8_t *out,
|
||||
size_t out_size) override {
|
||||
if (address != ADDR)
|
||||
return esphome::i2c::ERROR_NOT_ACKNOWLEDGED;
|
||||
this->transactions++;
|
||||
#ifdef USE_HOST
|
||||
{
|
||||
std::string s = size > 1 ? "W" : "R";
|
||||
char b[8];
|
||||
for (size_t i = 0; i < size; i++) {
|
||||
snprintf(b, sizeof(b), " %02x", data[i]);
|
||||
s += b;
|
||||
}
|
||||
if (out_size) {
|
||||
snprintf(b, sizeof(b), " r%u", (unsigned) out_size);
|
||||
s += b;
|
||||
}
|
||||
if (this->transactions == this->nack_at)
|
||||
s += " NACK";
|
||||
this->log.push_back(s);
|
||||
}
|
||||
#endif
|
||||
if (this->transactions == this->nack_at)
|
||||
return esphome::i2c::ERROR_NOT_ACKNOWLEDGED;
|
||||
if (size == 0) {
|
||||
this->protocol_errors++;
|
||||
return esphome::i2c::ERROR_INVALID_ARGUMENT;
|
||||
}
|
||||
uint8_t reg = data[0] & 0x7F;
|
||||
if (size > 1) {
|
||||
this->writes++;
|
||||
if (reg == R_INIT_DATA) {
|
||||
this->init_data_(data + 1, size - 1);
|
||||
} else {
|
||||
for (size_t i = 1; i < size; i++)
|
||||
this->write_reg_((reg + i - 1) & 0x7F, data[i]);
|
||||
}
|
||||
}
|
||||
if (out_size) {
|
||||
this->reads++;
|
||||
// STATUS polls are timing dependent (busy wait), so they stay out of the hash
|
||||
if (reg != R_STATUS)
|
||||
this->mix_(0x03000000u | (reg << 8) | (out_size & 0xFF));
|
||||
if (reg == R_STATUS) {
|
||||
this->status_reads++;
|
||||
this->regs[R_STATUS] = (this->busy_stuck || this->busy_left) ? 0x04 : 0x00;
|
||||
if (this->busy_left)
|
||||
this->busy_left--;
|
||||
}
|
||||
this->fill_data_();
|
||||
for (size_t i = 0; i < out_size; i++)
|
||||
out[i] = this->regs[(reg + i) & 0x7F];
|
||||
}
|
||||
return esphome::i2c::ERROR_OK;
|
||||
}
|
||||
|
||||
// Final configuration registers, for comparing builds
|
||||
void final_regs(char *buf, size_t n) const {
|
||||
snprintf(buf, n,
|
||||
"ACC_CONF=%02X ACC_RANGE=%02X GYR_CONF=%02X GYR_RANGE=%02X PWR_CONF=%02X PWR_CTRL=%02X IF_CONF=%02X "
|
||||
"AUX_DEV_ID=%02X AUX_IF_CONF=%02X AUX_RD_ADDR=%02X BMM_PWR=%02X BMM_OPMODE=%02X",
|
||||
this->regs[0x40], this->regs[0x41], this->regs[0x42], this->regs[0x43], this->regs[R_PWR_CONF],
|
||||
this->regs[R_PWR_CTRL], this->regs[R_IF_CONF], this->regs[R_AUX_DEV_ID], this->regs[R_AUX_IF_CONF],
|
||||
this->regs[R_AUX_RD_ADDR], this->bmm[0x4B], this->bmm[0x4C]);
|
||||
}
|
||||
|
||||
void report() const {
|
||||
char fr[220];
|
||||
this->final_regs(fr, sizeof(fr));
|
||||
ESP_LOGI(TAG,
|
||||
"UPLOAD status=0x%02X ok=%u bad=%u mem_fnv=%08X expected=%08X covered=%u rewrites=%u perr=%u resets=%u",
|
||||
this->regs[R_INTERNAL_STATUS], this->loads_ok, this->loads_bad, this->mem_fnv, this->expected_fnv,
|
||||
this->covered_(), this->rewrites, this->protocol_errors, this->resets);
|
||||
ESP_LOGI(TAG,
|
||||
"STREAM tx=%u writes=%u reads=%u data_writes=%u data_bytes=%u burst=%u..%u addr_reg_writes=%u aux_w=%u "
|
||||
"aux_r=%u norm=%08X",
|
||||
this->transactions, this->writes, this->reads, this->data_writes, this->data_bytes,
|
||||
this->burst_min == UINT32_MAX ? 0 : this->burst_min, this->burst_max, this->addr_writes, this->aux_writes,
|
||||
this->aux_reads, this->norm_fnv);
|
||||
ESP_LOGI(TAG, "REGS %s", fr);
|
||||
ESP_LOGI(TAG, "HEAP start=%u/%u end=%u/%u min_in_burst=%u bursts_below_start=%u", this->heap_start,
|
||||
this->block_start, this->heap_end, this->block_end, this->heap_min_in_burst,
|
||||
this->bursts_heap_below_start);
|
||||
#ifdef BMI_FAKE_WRAP_MALLOC
|
||||
ESP_LOGI(TAG, "MALLOC during upload=%u", (unsigned) alloc_count::count);
|
||||
#endif
|
||||
}
|
||||
};
|
||||
inline Bus bus;
|
||||
} // namespace bmi270fake
|
||||
@@ -1,98 +0,0 @@
|
||||
#ifdef USE_HOST
|
||||
// esphome#22778: the PR's bmm150_parse_trim/bmm150_convert against the merge base's copy (bmm150_aux_base.h),
|
||||
// bit for bit, over exhaustive per-field sweeps with edge trims and random raw/trim pairs.
|
||||
#include <gtest/gtest.h>
|
||||
|
||||
#include <array>
|
||||
#include <cmath>
|
||||
#include <cstdint>
|
||||
#include <vector>
|
||||
#include <cstring>
|
||||
#include <random>
|
||||
#include "esphome/components/bmi270/bmm150_aux.h"
|
||||
#include "bmm150_aux_base.h"
|
||||
|
||||
namespace esphome::bmi270::testing {
|
||||
|
||||
static uint32_t bits(float f) {
|
||||
uint32_t u;
|
||||
std::memcpy(&u, &f, 4);
|
||||
return u;
|
||||
}
|
||||
|
||||
struct Counts {
|
||||
uint64_t checked{0}, mismatched{0}, nan{0}, finite{0};
|
||||
};
|
||||
|
||||
static void check(const uint8_t *trim_regs, const uint8_t *raw, Counts &c) {
|
||||
const auto tp = bmm150::bmm150_parse_trim(trim_regs);
|
||||
const auto tb = bmm150_base::bmm150_parse_trim(trim_regs);
|
||||
static_assert(sizeof(tp) == sizeof(tb));
|
||||
const auto p = bmm150::bmm150_convert(tp, raw);
|
||||
const auto b = bmm150_base::bmm150_convert(tb, raw);
|
||||
c.checked++;
|
||||
bool same = std::memcmp(&tp, &tb, sizeof(tp)) == 0 && bits(p.x) == bits(b.x) && bits(p.y) == bits(b.y) &&
|
||||
bits(p.z) == bits(b.z);
|
||||
if (!same) {
|
||||
if (c.mismatched < 5)
|
||||
ADD_FAILURE() << "mismatch raw " << std::hex << (int) raw[0] << " " << (int) raw[1] << " " << (int) raw[2]
|
||||
<< " " << (int) raw[3] << " " << (int) raw[4] << " " << (int) raw[5] << " " << (int) raw[6]
|
||||
<< " " << (int) raw[7];
|
||||
c.mismatched++;
|
||||
}
|
||||
(std::isnan(p.x) || std::isnan(p.y) || std::isnan(p.z) ? c.nan : c.finite)++;
|
||||
}
|
||||
|
||||
TEST(BMM150Sweep22778, BaseAndPrDecodeIdentically) {
|
||||
std::vector<std::array<uint8_t, 24>> trims;
|
||||
// datasheet-like (same as the fake), all zero, all 0xFF, all 0x80, all 0x7F, alternating, and single zeroed words
|
||||
std::array<uint8_t, 24> ds{0xFE, 0x03, 0, 0, 0, 0xF6, 0xFF, 0x1A, 0x1B, 0, 0, 0xF8, 0x02, 0xD2, 0x60, 0xBD, 0x1B,
|
||||
0x40, 0, 0xFD, 0x1D, 0, 0, 0};
|
||||
trims.push_back(ds);
|
||||
for (uint8_t fill : {0x00, 0xFF, 0x80, 0x7F, 0x01}) {
|
||||
std::array<uint8_t, 24> t;
|
||||
t.fill(fill);
|
||||
trims.push_back(t);
|
||||
}
|
||||
for (int off : {11, 13, 15}) { // z2 = 0, z1 = 0, xyz1 = 0 give NaN paths
|
||||
auto t = ds;
|
||||
t[off] = t[off + 1] = 0;
|
||||
trims.push_back(t);
|
||||
}
|
||||
{
|
||||
auto t = ds;
|
||||
t[16] = 0xFF; // xyz1 with bit 15 set (masked)
|
||||
trims.push_back(t);
|
||||
}
|
||||
const uint8_t backgrounds[3][8] = {{0x10, 0x12, 0x20, 0xF0, 0x30, 0x08, 0x40, 0x1A},
|
||||
{0x00, 0x80, 0x00, 0x80, 0x00, 0x80, 0xFC, 0xFF},
|
||||
{0xF8, 0x7F, 0x07, 0x00, 0x01, 0xC0, 0x00, 0x00}};
|
||||
Counts c;
|
||||
for (auto &t : trims)
|
||||
for (auto &bg : backgrounds)
|
||||
for (int field = 0; field < 4; field++)
|
||||
for (uint32_t w = 0; w <= 0xFFFF; w++) {
|
||||
uint8_t raw[8];
|
||||
std::memcpy(raw, bg, 8);
|
||||
raw[2 * field] = w & 0xFF;
|
||||
raw[2 * field + 1] = w >> 8;
|
||||
check(t.data(), raw, c);
|
||||
}
|
||||
const uint64_t exhaustive = c.checked;
|
||||
std::mt19937 rng(22778);
|
||||
for (int i = 0; i < 3000000; i++) {
|
||||
uint8_t t[24], raw[8];
|
||||
for (auto &b : t)
|
||||
b = rng();
|
||||
for (auto &b : raw)
|
||||
b = rng();
|
||||
check(t, raw, c);
|
||||
}
|
||||
printf("BMM150_SWEEP exhaustive=%llu random=%llu mismatched=%llu nan_results=%llu finite_results=%llu\n",
|
||||
(unsigned long long) exhaustive, (unsigned long long) (c.checked - exhaustive),
|
||||
(unsigned long long) c.mismatched, (unsigned long long) c.nan, (unsigned long long) c.finite);
|
||||
EXPECT_EQ(c.mismatched, 0u);
|
||||
}
|
||||
|
||||
} // namespace esphome::bmi270::testing
|
||||
#endif
|
||||
@@ -1,168 +0,0 @@
|
||||
esphome:
|
||||
name: ${name}
|
||||
includes:
|
||||
- wkfake.hpp
|
||||
on_boot:
|
||||
- priority: 999.95 # before the weikai hub setup (BUS - 0.1)
|
||||
then:
|
||||
- lambda: |-
|
||||
wkfake::bus.trace = 1000;
|
||||
id(wk)->set_i2c_bus(&wkfake::bus);
|
||||
- priority: -100
|
||||
then:
|
||||
- lambda: wkfake::bus.trace = 0;
|
||||
- delay: 4s
|
||||
- script.execute: drive
|
||||
|
||||
logger:
|
||||
level: VERBOSE
|
||||
logs:
|
||||
component: DEBUG
|
||||
scheduler: DEBUG
|
||||
app: DEBUG
|
||||
switch: DEBUG
|
||||
binary_sensor: DEBUG
|
||||
gpio.binary_sensor: DEBUG
|
||||
gpio.switch: DEBUG
|
||||
esp32: DEBUG
|
||||
|
||||
i2c:
|
||||
id: realbus
|
||||
sda: GPIO21
|
||||
scl: GPIO22
|
||||
scan: false
|
||||
|
||||
wk2168_i2c:
|
||||
- id: wk
|
||||
i2c_id: realbus
|
||||
address: 0x10
|
||||
uart:
|
||||
- id: wk_uart0
|
||||
channel: 0
|
||||
baud_rate: 115200
|
||||
- id: wk_uart1
|
||||
channel: 1
|
||||
baud_rate: 9600
|
||||
|
||||
switch:
|
||||
- platform: gpio
|
||||
id: wk_out0
|
||||
name: "WK pin 0"
|
||||
restore_mode: ALWAYS_OFF
|
||||
pin:
|
||||
wk2168_i2c: wk
|
||||
number: 0
|
||||
mode:
|
||||
output: true
|
||||
on_turn_on:
|
||||
- logger.log: {format: "SWITCH pin0 ON", level: INFO}
|
||||
on_turn_off:
|
||||
- logger.log: {format: "SWITCH pin0 OFF", level: INFO}
|
||||
|
||||
binary_sensor:
|
||||
- platform: gpio
|
||||
id: wk_in1
|
||||
name: "WK pin 1"
|
||||
trigger_on_initial_state: true
|
||||
pin:
|
||||
wk2168_i2c: wk
|
||||
number: 1
|
||||
mode:
|
||||
input: true
|
||||
on_state:
|
||||
- lambda: |-
|
||||
ESP_LOGI("wktest", "BINARY pin1 = %s", x ? "ON" : "OFF");
|
||||
|
||||
button:
|
||||
- platform: template
|
||||
id: in_high
|
||||
name: "Fake input pin1 high"
|
||||
on_press:
|
||||
- lambda: wkfake::bus.set_input(1, true);
|
||||
- platform: template
|
||||
id: in_low
|
||||
name: "Fake input pin1 low"
|
||||
on_press:
|
||||
- lambda: wkfake::bus.set_input(1, false);
|
||||
|
||||
interval:
|
||||
- interval: 100ms
|
||||
then:
|
||||
- lambda: |-
|
||||
uart::UARTComponent *u[2] = {id(wk_uart0), id(wk_uart1)};
|
||||
for (int c = 0; c < 2; c++) {
|
||||
size_t n = u[c]->available();
|
||||
if (!n) continue;
|
||||
char buf[64];
|
||||
if (n > sizeof(buf) - 1) n = sizeof(buf) - 1;
|
||||
u[c]->read_array((uint8_t *) buf, n);
|
||||
for (size_t i = 0; i < n; i++) if (buf[i] < 0x20) buf[i] = '.';
|
||||
buf[n] = 0;
|
||||
ESP_LOGI("wktest", "UART ch%d got %u bytes '%s'", c, (unsigned) n, buf);
|
||||
}
|
||||
|
||||
script:
|
||||
- id: drive
|
||||
then:
|
||||
- logger.log: {format: "STEP 1 state after setup", level: INFO}
|
||||
- lambda: wkfake::bus.dump();
|
||||
- logger.log: {format: "STEP 2 trace one poll pass", level: INFO}
|
||||
- lambda: wkfake::bus.trace = 14;
|
||||
- delay: 1s
|
||||
- logger.log: {format: "STEP 3 GPIO output toggles", level: INFO}
|
||||
- switch.turn_on: wk_out0
|
||||
- delay: 1s
|
||||
- switch.turn_off: wk_out0
|
||||
- delay: 1s
|
||||
- switch.turn_on: wk_out0
|
||||
- delay: 1s
|
||||
- logger.log: {format: "STEP 4 GPIO input changes", level: INFO}
|
||||
- button.press: in_high
|
||||
- delay: 1s
|
||||
- button.press: in_low
|
||||
- delay: 1s
|
||||
- button.press: in_high
|
||||
- delay: 1s
|
||||
- logger.log: {format: "STEP 5 UART TX", level: INFO}
|
||||
- lambda: |-
|
||||
id(wk_uart0)->write_str("PING0");
|
||||
id(wk_uart1)->write_str("PING1");
|
||||
- delay: 1s
|
||||
- logger.log: {format: "STEP 6 UART RX inject", level: INFO}
|
||||
- lambda: |-
|
||||
wkfake::bus.inject_rx(0, "hello0");
|
||||
wkfake::bus.inject_rx(1, "hello1");
|
||||
- delay: 1s
|
||||
- logger.log: {format: "STEP 7 three single read NACKs", level: INFO}
|
||||
- lambda: wkfake::bus.fail_reads = 1;
|
||||
- delay: 1s
|
||||
- lambda: wkfake::bus.fail_reads = 1;
|
||||
- delay: 1s
|
||||
- lambda: wkfake::bus.fail_reads = 1;
|
||||
- delay: 1s
|
||||
- logger.log: {format: "STEP 8 single NACK on the register pointer write", level: INFO}
|
||||
- lambda: wkfake::bus.fail_ptr = 1;
|
||||
- delay: 1s
|
||||
- logger.log: {format: "STEP 9 double read NACK", level: INFO}
|
||||
- lambda: wkfake::bus.fail_reads = 2;
|
||||
- delay: 1s
|
||||
- logger.log: {format: "STEP 10 every 37th read NACKed for 5 s", level: INFO}
|
||||
- lambda: |-
|
||||
wkfake::bus.every_n = 37;
|
||||
ESP_LOGI("wktest", "EVERYN start injected=%u retries=%u", wkfake::bus.injected, wkfake::bus.retries);
|
||||
- delay: 5s
|
||||
- lambda: |-
|
||||
wkfake::bus.every_n = 0;
|
||||
ESP_LOGI("wktest", "EVERYN end injected=%u retries=%u no_retry=%u", wkfake::bus.injected, wkfake::bus.retries, wkfake::bus.no_retry);
|
||||
- delay: 500ms
|
||||
- logger.log: {format: "STEP 11 GPIO and UART again after faults", level: INFO}
|
||||
- switch.turn_off: wk_out0
|
||||
- button.press: in_low
|
||||
- delay: 1s
|
||||
- lambda: |-
|
||||
wkfake::bus.inject_rx(0, "again0");
|
||||
id(wk_uart1)->write_str("PONG1");
|
||||
- delay: 1s
|
||||
- logger.log: {format: "STEP 12 final", level: INFO}
|
||||
- lambda: wkfake::bus.dump();
|
||||
- logger.log: {format: "DRIVE DONE", level: INFO}
|
||||
@@ -1,32 +0,0 @@
|
||||
#include <cstdint>
|
||||
#include <cstdio>
|
||||
enum RegType { REG = 0, FIFO = 1 };
|
||||
// i2c_address() is the same in both trees; only the PR adds i2c_register() (the base sends register_ unchanged).
|
||||
inline uint8_t i2c_address(uint8_t base_address, uint8_t channel, RegType fifo) {
|
||||
return base_address | channel << 1 | fifo << 0;
|
||||
}
|
||||
namespace pr {
|
||||
inline uint8_t i2c_register(uint8_t reg, uint8_t &channel) {
|
||||
if (reg > 0x0F) {
|
||||
channel = (reg >> 4) & 0x03;
|
||||
}
|
||||
return reg & 0x0F;
|
||||
}
|
||||
}
|
||||
int main() {
|
||||
int same = 0, diff = 0;
|
||||
const uint8_t B = 0x10;
|
||||
for (int ch = 0; ch < 4; ch++)
|
||||
for (int reg = 0; reg < 0x40; reg++) {
|
||||
uint8_t ba = i2c_address(B, ch, REG), br = reg;
|
||||
uint8_t c = ch;
|
||||
uint8_t pr_reg = pr::i2c_register(reg, c);
|
||||
uint8_t pa = i2c_address(B, c, REG);
|
||||
bool eq = ba == pa && br == pr_reg;
|
||||
if (reg < 0x10) { eq ? same++ : diff++; if (!eq) printf("MISMATCH ch%d reg %02X\n", ch, reg); }
|
||||
else if (reg == 0x10 || reg == 0x11 || reg == 0x21 || reg == 0x31)
|
||||
printf("global %02X ch%d: base @%02X r=%02X -> PR @%02X r=%02X\n", reg, ch, ba, br, pa, pr_reg);
|
||||
}
|
||||
printf("regs below 0x10, 4 channels: %d same, %d different\n", same, diff);
|
||||
for (int ch = 0; ch < 4; ch++) printf("FIFO ch%d: @%02X in both\n", ch, i2c_address(B, ch, FIFO));
|
||||
}
|
||||
@@ -1,373 +0,0 @@
|
||||
#pragma once
|
||||
// Fake WeiKai WK2168 on an in-firmware I2C bus (esphome#22759, 2026-11-15).
|
||||
// Device address 0 A1 A0 1 0 C1 C0 F (base 0x10 when A1 A0 = 00): eight addresses, F=1 is the FIFO of channel C1C0.
|
||||
// The register byte carries only A3..A0; the 6 bit register address is C1C0A3A2A1A0 (datasheet 12.4). Global registers
|
||||
// are 0x00 GENA, 0x01 GRST, 0x02 GMUT, 0x10 GIER, 0x11 GIFR, 0x21 GPDIR, 0x31 GPDAT; A3..A0 >= 3 is a sub-UART register
|
||||
// of channel C1C0. A register byte with bits above A3..A0 is truncated like the chip does and counted in hi_bits.
|
||||
// A write to GRST soft-resets the channels in its low nibble (registers and FIFOs to defaults) and is logged loudly.
|
||||
// Every transaction is classified: W (register write), P (1 byte write: register pointer), R (read only, uses the
|
||||
// pointer of that address), WR (write then read in one transaction, repeated start). trace > 0 logs the next `trace`
|
||||
// transactions; writes, faults and changed read values are always logged; the rest is counted in cnt[][][].
|
||||
// Fault injection: fail_reads NACKs the next read phases (R or WR), fail_ptr the next P transfers, every_n NACKs every
|
||||
// Nth read phase once. After a NACK, the next read phase on the same address and register counts as a retry.
|
||||
#include <cstring>
|
||||
#include "esphome/components/i2c/i2c_bus.h"
|
||||
#include "esphome/core/log.h"
|
||||
|
||||
namespace wkfake {
|
||||
static const char *const TAG = "wkfake";
|
||||
using esphome::i2c::ErrorCode;
|
||||
|
||||
enum Kind : uint8_t { K_W = 0, K_P = 1, K_R = 2, K_WR = 3 };
|
||||
static const char *const KIND[] = {"W ", "P ", "R ", "WR"};
|
||||
|
||||
struct Fifo {
|
||||
uint8_t d[256];
|
||||
uint16_t head{0}, n{0};
|
||||
void clear() { head = n = 0; }
|
||||
bool push(uint8_t b) {
|
||||
if (n == 256)
|
||||
return false;
|
||||
d[(head + n++) & 0xFF] = b;
|
||||
return true;
|
||||
}
|
||||
uint8_t pop() {
|
||||
if (!n)
|
||||
return 0;
|
||||
uint8_t b = d[head];
|
||||
head = (head + 1) & 0xFF;
|
||||
n--;
|
||||
return b;
|
||||
}
|
||||
};
|
||||
|
||||
struct Bus final : public esphome::i2c::I2CBus {
|
||||
uint8_t base{0x10};
|
||||
uint8_t glob[64]{};
|
||||
uint8_t pg0[4][16]{}, pg1[4][16]{}, spage[4]{};
|
||||
uint8_t ptr[8]{};
|
||||
uint8_t gpio_in{0};
|
||||
Fifo rx[4];
|
||||
// counters
|
||||
uint32_t cnt[8][64][4]{};
|
||||
uint32_t n_trans{0}, grst_writes{0}, hi_bits{0}, reserved{0}, tx_bytes[4]{}, tx_dropped{0}, rx_dropped{0};
|
||||
uint32_t injected{0}, retries{0}, no_retry{0}, gpdir_writes{0}, gpdat_writes{0}, gpdat_reads{0};
|
||||
uint8_t last_read[8][64];
|
||||
// controls
|
||||
uint32_t trace{0}, fail_reads{0}, fail_ptr{0}, every_n{0}, read_phases{0};
|
||||
bool await_retry{false};
|
||||
uint8_t await_addr{0}, await_reg{0};
|
||||
|
||||
Bus() {
|
||||
memset(this->last_read, 0xEE, sizeof(this->last_read));
|
||||
for (int c = 0; c < 4; c++)
|
||||
this->reset_channel_(c);
|
||||
}
|
||||
void reset_channel_(int c) {
|
||||
memset(this->pg0[c], 0, 16);
|
||||
memset(this->pg1[c], 0, 16);
|
||||
this->spage[c] = 0;
|
||||
this->rx[c].clear();
|
||||
}
|
||||
static const char *gname(uint8_t a6) {
|
||||
switch (a6) {
|
||||
case 0x00:
|
||||
return "GENA";
|
||||
case 0x01:
|
||||
return "GRST";
|
||||
case 0x02:
|
||||
return "GMUT";
|
||||
case 0x10:
|
||||
return "GIER";
|
||||
case 0x11:
|
||||
return "GIFR";
|
||||
case 0x21:
|
||||
return "GPDIR";
|
||||
case 0x31:
|
||||
return "GPDAT";
|
||||
default:
|
||||
return "RSVD";
|
||||
}
|
||||
}
|
||||
static const char *cname(uint8_t r, bool p1) {
|
||||
static const char *const P0[] = {"", "", "", "SPAGE", "SCR", "LCR", "FCR", "SIER",
|
||||
"SIFR", "TFCNT", "RFCNT", "FSR", "LSR", "FDAT", "R0E", "R0F"};
|
||||
static const char *const P1[] = {"", "", "", "SPAGE", "BRH", "BRL", "BRD", "RFI",
|
||||
"TFI", "R19", "R1A", "R1B", "R1C", "R1D", "R1E", "R1F"};
|
||||
return p1 ? P1[r & 15] : P0[r & 15];
|
||||
}
|
||||
void name_(uint8_t addr, uint8_t regbyte, char *out, size_t len) {
|
||||
uint8_t c = (addr >> 1) & 3, r = regbyte & 0x0F;
|
||||
if (r < 3)
|
||||
snprintf(out, len, "%s(%02X)", gname((c << 4) | r), (c << 4) | r);
|
||||
else
|
||||
snprintf(out, len, "ch%u.%s", c, cname(r, r != 3 && this->spage[c]));
|
||||
}
|
||||
uint8_t gpdat_value() const {
|
||||
uint8_t dir = this->glob[0x21];
|
||||
return (this->glob[0x31] & dir) | (this->gpio_in & ~dir);
|
||||
}
|
||||
void show_gpio(const char *why) {
|
||||
ESP_LOGI(TAG, "GPIO %s: GPDIR=%02X GPDAT latch=%02X inputs=%02X pins=%02X", why, this->glob[0x21], this->glob[0x31],
|
||||
this->gpio_in, this->gpdat_value());
|
||||
}
|
||||
|
||||
void reg_write_(uint8_t addr, uint8_t regbyte, uint8_t v) {
|
||||
uint8_t c = (addr >> 1) & 3, r = regbyte & 0x0F;
|
||||
if (r < 3) {
|
||||
uint8_t a6 = (c << 4) | r;
|
||||
switch (a6) {
|
||||
case 0x01:
|
||||
this->grst_writes++;
|
||||
ESP_LOGW(TAG, "!!!!! GRST WRITE %02X via @%02X reg byte %02X: soft reset of channels mask %X !!!!!", v, addr,
|
||||
regbyte, v & 0x0F);
|
||||
for (int i = 0; i < 4; i++)
|
||||
if (v & (1 << i))
|
||||
this->reset_channel_(i);
|
||||
return; // self clearing
|
||||
case 0x21:
|
||||
this->gpdir_writes++;
|
||||
this->glob[a6] = v;
|
||||
this->show_gpio("dir write");
|
||||
return;
|
||||
case 0x31:
|
||||
this->gpdat_writes++;
|
||||
this->glob[a6] = v;
|
||||
this->show_gpio("data write");
|
||||
return;
|
||||
case 0x00:
|
||||
case 0x02:
|
||||
case 0x10:
|
||||
case 0x11:
|
||||
this->glob[a6] = v;
|
||||
return;
|
||||
default:
|
||||
this->reserved++;
|
||||
ESP_LOGW(TAG, "write to reserved global %02X", a6);
|
||||
return;
|
||||
}
|
||||
}
|
||||
if (r == 3) {
|
||||
this->spage[c] = v & 1;
|
||||
return;
|
||||
}
|
||||
if (this->spage[c]) {
|
||||
this->pg1[c][r] = v;
|
||||
return;
|
||||
}
|
||||
if (r == 6) { // FCR: reset bits clear the FIFOs and do not stay set
|
||||
if (v & 1)
|
||||
this->rx[c].clear();
|
||||
v &= 0x0C;
|
||||
}
|
||||
if (r == 0x0D) {
|
||||
this->tx_bytes[c]++;
|
||||
return;
|
||||
}
|
||||
if (r >= 9 && r <= 0x0C)
|
||||
return; // read only
|
||||
this->pg0[c][r] = v;
|
||||
}
|
||||
|
||||
uint8_t reg_read_(uint8_t addr, uint8_t regbyte) {
|
||||
uint8_t c = (addr >> 1) & 3, r = regbyte & 0x0F;
|
||||
if (r < 3) {
|
||||
uint8_t a6 = (c << 4) | r;
|
||||
if (a6 == 0x01)
|
||||
return 0;
|
||||
if (a6 == 0x31) {
|
||||
this->gpdat_reads++;
|
||||
return this->gpdat_value();
|
||||
}
|
||||
return this->glob[a6];
|
||||
}
|
||||
if (r == 3)
|
||||
return this->spage[c];
|
||||
if (this->spage[c])
|
||||
return this->pg1[c][r];
|
||||
switch (r) {
|
||||
case 0x09:
|
||||
return 0; // TFCNT: the fake sends at once
|
||||
case 0x0A:
|
||||
return (uint8_t) this->rx[c].n; // RFCNT (256 reads 0)
|
||||
case 0x0B:
|
||||
return this->rx[c].n ? (1 << 3) : 0; // FSR: RFDAT
|
||||
case 0x0C:
|
||||
return 0; // LSR
|
||||
case 0x0D:
|
||||
return this->rx[c].pop(); // FDAT
|
||||
default:
|
||||
return this->pg0[c][r];
|
||||
}
|
||||
}
|
||||
|
||||
bool channel_on_(int c, uint8_t bit) const { return (this->glob[0] & (1 << c)) && (this->pg0[c][4] & bit); }
|
||||
|
||||
void inject_rx(int c, const char *s) {
|
||||
if (!this->channel_on_(c, 1)) {
|
||||
this->rx_dropped += strlen(s);
|
||||
ESP_LOGW(TAG, "RX inject ch%d dropped %u bytes: receiver disabled (GENA=%02X SCR=%02X)", c, (unsigned) strlen(s),
|
||||
this->glob[0], this->pg0[c][4]);
|
||||
return;
|
||||
}
|
||||
for (const char *p = s; *p; p++)
|
||||
this->rx[c].push((uint8_t) *p);
|
||||
ESP_LOGI(TAG, "RX inject ch%d '%s' fifo=%u", c, s, this->rx[c].n);
|
||||
}
|
||||
|
||||
void set_input(uint8_t pin, bool v) {
|
||||
if (v)
|
||||
this->gpio_in |= 1 << pin;
|
||||
else
|
||||
this->gpio_in &= ~(1 << pin);
|
||||
this->show_gpio("input change");
|
||||
}
|
||||
|
||||
ErrorCode write_readv(uint8_t address, const uint8_t *data, size_t size, uint8_t *out, size_t out_size) override {
|
||||
if ((address & 0xF8) != this->base)
|
||||
return esphome::i2c::ERROR_NOT_ACKNOWLEDGED;
|
||||
if (size == 0 && out_size == 0)
|
||||
return esphome::i2c::ERROR_OK;
|
||||
this->n_trans++;
|
||||
const uint8_t a = address & 7, c = (a >> 1) & 3;
|
||||
if (a & 1) { // FIFO
|
||||
if (size) {
|
||||
char txt[48];
|
||||
size_t i = 0;
|
||||
for (; i < size && i < sizeof(txt) - 1; i++)
|
||||
txt[i] = data[i] >= 0x20 && data[i] < 0x7F ? (char) data[i] : '.';
|
||||
txt[i] = 0;
|
||||
if (this->channel_on_(c, 2)) {
|
||||
this->tx_bytes[c] += size;
|
||||
ESP_LOGI(TAG, "#%u @%02X FIFO TX ch%u %u bytes '%s'", this->n_trans, address, c, (unsigned) size, txt);
|
||||
} else {
|
||||
this->tx_dropped += size;
|
||||
ESP_LOGW(TAG, "#%u @%02X FIFO TX ch%u %u bytes '%s' DROPPED: transmitter disabled (GENA=%02X SCR=%02X)",
|
||||
this->n_trans, address, c, (unsigned) size, txt, this->glob[0], this->pg0[c][4]);
|
||||
}
|
||||
}
|
||||
if (out_size) {
|
||||
for (size_t i = 0; i < out_size; i++)
|
||||
out[i] = this->rx[c].pop();
|
||||
ESP_LOGI(TAG, "#%u @%02X FIFO RX ch%u %u bytes, %u left", this->n_trans, address, c, (unsigned) out_size,
|
||||
this->rx[c].n);
|
||||
}
|
||||
return esphome::i2c::ERROR_OK;
|
||||
}
|
||||
Kind k;
|
||||
uint8_t regbyte;
|
||||
if (out_size == 0)
|
||||
k = size == 1 ? K_P : K_W, regbyte = data[0];
|
||||
else if (size == 0)
|
||||
k = K_R, regbyte = this->ptr[a];
|
||||
else
|
||||
k = K_WR, regbyte = data[0];
|
||||
if (regbyte & 0xF0 && (++this->hi_bits <= 3 || k == K_W || k == K_P)) {
|
||||
ESP_LOGW(TAG, "#%u @%02X %s reg byte %02X has bits above A3..A0; the chip sees register %X", this->n_trans,
|
||||
address, KIND[k], regbyte, regbyte & 0x0F);
|
||||
}
|
||||
this->cnt[a][regbyte & 0x3F][k]++;
|
||||
char nm_buf[24];
|
||||
// the register name is only built for the paths that log
|
||||
auto nm = [&]() {
|
||||
this->name_(address, regbyte, nm_buf, sizeof(nm_buf));
|
||||
return nm_buf;
|
||||
};
|
||||
if (k == K_P) {
|
||||
if (this->fail_ptr) {
|
||||
this->fail_ptr--;
|
||||
this->injected++;
|
||||
this->arm_retry_(address, regbyte);
|
||||
ESP_LOGW(TAG, "#%u @%02X P r=%02X %s INJECTED NACK", this->n_trans, address, regbyte, nm());
|
||||
return esphome::i2c::ERROR_NOT_ACKNOWLEDGED;
|
||||
}
|
||||
this->ptr[a] = regbyte;
|
||||
if (this->trace) {
|
||||
this->trace--;
|
||||
ESP_LOGI(TAG, "#%u @%02X P r=%02X %s", this->n_trans, address, regbyte, nm());
|
||||
}
|
||||
return esphome::i2c::ERROR_OK;
|
||||
}
|
||||
if (k == K_W) {
|
||||
this->ptr[a] = regbyte;
|
||||
for (size_t i = 1; i < size; i++)
|
||||
this->reg_write_(address, regbyte, data[i]);
|
||||
if (this->trace)
|
||||
this->trace--;
|
||||
ESP_LOGI(TAG, "#%u @%02X W r=%02X %s = %02X", this->n_trans, address, regbyte, nm(), data[1]);
|
||||
return esphome::i2c::ERROR_OK;
|
||||
}
|
||||
// read phase (R or WR)
|
||||
this->read_phases++;
|
||||
if (this->await_retry) {
|
||||
this->await_retry = false;
|
||||
if (address == this->await_addr && (regbyte == this->await_reg || k == K_R)) {
|
||||
this->retries++;
|
||||
ESP_LOGI(TAG, "#%u @%02X %s r=%02X %s RETRY after injected NACK", this->n_trans, address, KIND[k], regbyte,
|
||||
nm());
|
||||
} else {
|
||||
this->no_retry++;
|
||||
ESP_LOGW(TAG, "#%u @%02X %s r=%02X %s: no retry of the NACKed read", this->n_trans, address, KIND[k], regbyte,
|
||||
nm());
|
||||
}
|
||||
}
|
||||
if (k == K_WR)
|
||||
this->ptr[a] = regbyte;
|
||||
bool fail = false;
|
||||
if (this->fail_reads) {
|
||||
this->fail_reads--;
|
||||
fail = true;
|
||||
} else if (this->every_n && this->read_phases % this->every_n == 0) {
|
||||
fail = true;
|
||||
}
|
||||
if (fail) {
|
||||
this->injected++;
|
||||
this->arm_retry_(address, regbyte);
|
||||
memset(out, 0, out_size);
|
||||
ESP_LOGW(TAG, "#%u @%02X %s r=%02X %s INJECTED NACK", this->n_trans, address, KIND[k], regbyte, nm());
|
||||
return esphome::i2c::ERROR_NOT_ACKNOWLEDGED;
|
||||
}
|
||||
for (size_t i = 0; i < out_size; i++)
|
||||
out[i] = this->reg_read_(address, regbyte);
|
||||
uint8_t &last = this->last_read[a][regbyte & 0x3F];
|
||||
bool changed = last != out[0];
|
||||
last = out[0];
|
||||
if (this->trace) {
|
||||
this->trace--;
|
||||
ESP_LOGI(TAG, "#%u @%02X %s r=%02X %s -> %02X", this->n_trans, address, KIND[k], regbyte, nm(), out[0]);
|
||||
} else if (changed) {
|
||||
ESP_LOGI(TAG, "#%u @%02X %s r=%02X %s -> %02X (changed)", this->n_trans, address, KIND[k], regbyte, nm(), out[0]);
|
||||
}
|
||||
return esphome::i2c::ERROR_OK;
|
||||
}
|
||||
void arm_retry_(uint8_t addr, uint8_t reg) {
|
||||
this->await_retry = true;
|
||||
this->await_addr = addr;
|
||||
this->await_reg = reg;
|
||||
}
|
||||
|
||||
void dump() {
|
||||
ESP_LOGI(TAG,
|
||||
"STATS trans=%u grst_writes=%u hi_bits=%u reserved=%u gpdir_w=%u gpdat_w=%u gpdat_r=%u tx=%u/%u "
|
||||
"tx_dropped=%u rx_dropped=%u injected=%u retries=%u no_retry=%u",
|
||||
this->n_trans, this->grst_writes, this->hi_bits, this->reserved, this->gpdir_writes, this->gpdat_writes,
|
||||
this->gpdat_reads, this->tx_bytes[0], this->tx_bytes[1], this->tx_dropped, this->rx_dropped,
|
||||
this->injected, this->retries, this->no_retry);
|
||||
this->show_gpio("now");
|
||||
for (int c = 0; c < 2; c++)
|
||||
ESP_LOGI(TAG, "ch%d SCR=%02X LCR=%02X FCR=%02X BR=%02X%02X.%X SPAGE=%u", c, this->pg0[c][4], this->pg0[c][5],
|
||||
this->pg0[c][6], this->pg1[c][4], this->pg1[c][5], this->pg1[c][6], this->spage[c]);
|
||||
char nm[24];
|
||||
for (int a = 0; a < 8; a++)
|
||||
for (int r = 0; r < 64; r++) {
|
||||
uint32_t *k = this->cnt[a][r];
|
||||
if (k[0] | k[1] | k[2] | k[3]) {
|
||||
this->name_(this->base | a, r, nm, sizeof(nm));
|
||||
ESP_LOGI(TAG, "MAP @%02X r=%02X %-12s W=%u P=%u R=%u WR=%u", this->base | a, r, nm, k[0], k[1], k[2], k[3]);
|
||||
}
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
static Bus bus;
|
||||
} // namespace wkfake
|
||||
@@ -515,18 +515,12 @@ void CC1101Component::dump_config() {
|
||||
}
|
||||
|
||||
void CC1101Component::begin_tx() {
|
||||
this->wake_up();
|
||||
// Read the mode before the PKTCTRL0 write below overwrites it in state_
|
||||
const bool packet_mode = this->state_.PKT_FORMAT == static_cast<uint8_t>(PacketFormat::PACKET_FORMAT_FIFO);
|
||||
// Ensure Packet Format is 3 (Async Serial)
|
||||
this->write_(Register::PKTCTRL0, 0x32);
|
||||
if (this->state_.PKT_FORMAT == static_cast<uint8_t>(PacketFormat::PACKET_FORMAT_FIFO)) {
|
||||
ESP_LOGW(TAG, "begin_tx is for async serial mode; use send_packet in packet mode");
|
||||
return;
|
||||
}
|
||||
ESP_LOGV(TAG, "Beginning TX sequence");
|
||||
if (this->gdo0_pin_ != nullptr) {
|
||||
// setup() only attaches the interrupt in packet mode. In async serial mode the pin may be
|
||||
// shared with remote_receiver, and detaching here would remove the receiver's interrupt.
|
||||
if (packet_mode) {
|
||||
this->gdo0_pin_->detach_interrupt();
|
||||
}
|
||||
this->gdo0_pin_->pin_mode(gpio::FLAG_OUTPUT);
|
||||
}
|
||||
// Transition through IDLE to bypass CCA (Clear Channel Assessment) which can
|
||||
|
||||
@@ -63,13 +63,25 @@ void CST816Touchscreen::update_touches() {
|
||||
uint8_t data[13];
|
||||
if (!this->status_comm_result(this->read_bytes(REG_STATUS, data, sizeof data)))
|
||||
return;
|
||||
uint8_t num_of_touches = data[REG_TOUCH_NUM] & 3;
|
||||
|
||||
// The low nibble is the touch count. CST816/CST820 are single-touch;
|
||||
// CST826/CST836 can report two points, but this driver only reads the first
|
||||
// point. Some CST8xx controllers report an all-0xFF frame when idle or after
|
||||
// disabling auto-sleep: the count lands above two and the coordinates are
|
||||
// the 0xFFF sentinel, so both guards below reject it.
|
||||
uint8_t num_of_touches = low_nibble(data[REG_TOUCH_NUM]);
|
||||
if (num_of_touches == 0) {
|
||||
return;
|
||||
}
|
||||
if (num_of_touches > 2) {
|
||||
return;
|
||||
}
|
||||
|
||||
uint16_t x = encode_uint16(low_nibble(data[REG_XPOS_HIGH]), data[REG_XPOS_LOW]);
|
||||
uint16_t y = encode_uint16(low_nibble(data[REG_YPOS_HIGH]), data[REG_YPOS_LOW]);
|
||||
if (x == 0x0FFF || y == 0x0FFF) {
|
||||
return;
|
||||
}
|
||||
ESP_LOGV(TAG, "Read touch %d/%d", x, y);
|
||||
this->add_raw_touch_position_(0, x, y);
|
||||
}
|
||||
|
||||
@@ -605,7 +605,9 @@ async def to_code(config: ConfigType) -> None:
|
||||
cg.add(var.set_type(conf[CONF_TYPE]))
|
||||
|
||||
for conf in config[CONF_SELECTS]:
|
||||
var = await select.new_select(conf, options=conf[CONF_OPTIONS])
|
||||
var = await select.new_select(
|
||||
conf, options=conf[CONF_OPTIONS], fixed_options=True
|
||||
)
|
||||
await cg.register_component(var, conf)
|
||||
|
||||
for conf in config[CONF_SENSORS]:
|
||||
|
||||
@@ -41,5 +41,5 @@ CONFIG_SCHEMA = (
|
||||
|
||||
|
||||
async def to_code(config: ConfigType) -> None:
|
||||
var = await select.new_select(config, options=DUCO_MODE_OPTIONS)
|
||||
var = await select.new_select(config, options=DUCO_MODE_OPTIONS, fixed_options=True)
|
||||
await register_duco_device(var, config, config[CONF_DUCO_ID])
|
||||
@@ -114,6 +114,26 @@ struct LogBuffer {
|
||||
this->finalize_();
|
||||
}
|
||||
#endif
|
||||
// Writes a message that needs no formatting, truncated and stripped of trailing newlines as format_body() does
|
||||
void HOT write_unformatted_body(const char *text, size_t len) {
|
||||
if (!this->full_()) {
|
||||
const uint16_t limit = this->remaining_() - 1; // vsnprintf() keeps the last byte for the null terminator
|
||||
const uint16_t copy_len = (len < limit) ? static_cast<uint16_t>(len) : limit;
|
||||
memcpy(this->current_(), text, copy_len);
|
||||
this->pos += copy_len;
|
||||
this->strip_trailing_newlines_();
|
||||
}
|
||||
this->finalize_();
|
||||
}
|
||||
// write_unformatted_body() for a message in flash
|
||||
inline void HOT ESPHOME_ALWAYS_INLINE write_unformatted_body_p(ESPHOME_PGM_P text) {
|
||||
if (!this->full_()) {
|
||||
char *const start = this->current_();
|
||||
this->pos = static_cast<uint16_t>(progmem_copy_str(start, start + this->remaining_() - 1, text) - this->data);
|
||||
this->strip_trailing_newlines_();
|
||||
}
|
||||
this->finalize_();
|
||||
}
|
||||
static constexpr uint16_t ANSI_COLOR_LEN = 7; // "\033[{bold};3{color}m" written by write_header for level != 0
|
||||
static constexpr uint16_t ANSI_RESET_LEN = 4; // "\033[0m"
|
||||
// Largest replay header: color plus "@4294967295ms "
|
||||
@@ -161,7 +181,8 @@ struct LogBuffer {
|
||||
this->pos += ANSI_RESET_LEN;
|
||||
}
|
||||
}
|
||||
void strip_trailing_newlines_() {
|
||||
// Forced inline: an outlined copy slows every formatted line
|
||||
inline void ESPHOME_ALWAYS_INLINE strip_trailing_newlines_() {
|
||||
while (this->pos > 0 && this->data[this->pos - 1] == '\n')
|
||||
this->pos--;
|
||||
}
|
||||
|
||||
@@ -19,6 +19,15 @@ namespace esphome::logger {
|
||||
|
||||
ESPHOME_LOG_TAG(TAG, "logger");
|
||||
|
||||
#ifdef USE_LOGGER_DEFER_UNDER_LWIP_LOCK
|
||||
// lwIP code run under the core lock (tcpip_api_call, netif callbacks) can log. A listener that writes to a socket would
|
||||
// take that lock again and deadlock, so such a line is queued for loop() instead. It can print after later inline
|
||||
// lines, and is lost if loop() never runs again (safe_reboot()).
|
||||
static inline bool HOT ESPHOME_ALWAYS_INLINE under_lwip_core_lock() {
|
||||
return sys_thread_tcpip(LWIP_CORE_LOCK_QUERY_HOLDER) && sys_thread_tcpip(LWIP_CORE_IS_TCPIP_INITIALIZED);
|
||||
}
|
||||
#endif
|
||||
|
||||
#if defined(USE_ESP32) || defined(USE_HOST) || defined(USE_LIBRETINY) || defined(USE_ZEPHYR)
|
||||
// Implementation for multi-threaded platforms (ESP32 with FreeRTOS, Host with pthreads, LibreTiny with FreeRTOS,
|
||||
// Zephyr) Main thread/task always uses direct buffer access for console output and callbacks
|
||||
@@ -51,11 +60,7 @@ void HOT Logger::log_vprintf_(uint8_t level, const char *tag, int line, const ch
|
||||
// Pass nullptr for thread_name since we already know this is the main task
|
||||
if (is_main_task && !this->main_task_recursion_guard_) [[likely]] {
|
||||
#ifdef USE_LOGGER_DEFER_UNDER_LWIP_LOCK
|
||||
// lwIP code run under the core lock (tcpip_api_call, netif callbacks) can log. A listener that writes
|
||||
// to a socket would take that lock again and deadlock, so queue the message for loop() instead. Such lines
|
||||
// can print after later inline ones, and are lost if loop() never runs again (safe_reboot()).
|
||||
if (sys_thread_tcpip(LWIP_CORE_LOCK_QUERY_HOLDER) && sys_thread_tcpip(LWIP_CORE_IS_TCPIP_INITIALIZED))
|
||||
[[unlikely]] {
|
||||
if (under_lwip_core_lock()) [[unlikely]] {
|
||||
this->log_vprintf_non_main_thread_(level, tag, line, format, args, nullptr);
|
||||
return;
|
||||
}
|
||||
@@ -159,6 +164,49 @@ void Logger::log_vprintf_(uint8_t level, const char *tag, int line, const __Flas
|
||||
}
|
||||
#endif // USE_ESP8266
|
||||
|
||||
// Not shared with log_message_to_buffer_and_send_(): sharing it slows formatted lines
|
||||
inline void HOT ESPHOME_ALWAYS_INLINE Logger::send_unformatted_line_(uint8_t level, const char *tag, LogBuffer &buf) {
|
||||
#ifdef USE_LOGGER_BOOT_LOG_BUFFER
|
||||
if (!this->boot_log_done_)
|
||||
this->boot_log_capture_(level, tag, buf);
|
||||
#endif
|
||||
this->notify_listeners_(level, tag, buf);
|
||||
this->write_log_buffer_to_console_(buf);
|
||||
}
|
||||
|
||||
bool HOT Logger::log_str_(uint8_t level, const char *tag, int line, const char *msg) {
|
||||
if (level > this->level_for(tag))
|
||||
return true;
|
||||
// Check the task first: the guard belongs to the main task and must not be read from other threads
|
||||
if (!this->is_main_task_())
|
||||
return false;
|
||||
if (this->main_task_recursion_guard_)
|
||||
return true;
|
||||
#ifdef USE_LOGGER_DEFER_UNDER_LWIP_LOCK
|
||||
// The formatting path queues it
|
||||
if (under_lwip_core_lock()) [[unlikely]]
|
||||
return false;
|
||||
#endif
|
||||
RecursionGuard guard(this->main_task_recursion_guard_);
|
||||
LogBuffer buf{this->tx_buffer_, ESPHOME_LOGGER_TX_BUFFER_SIZE};
|
||||
buf.write_header(level, tag, line, nullptr);
|
||||
buf.write_unformatted_body(msg, strlen(msg));
|
||||
this->send_unformatted_line_(level, tag, buf);
|
||||
return true;
|
||||
}
|
||||
|
||||
#ifdef USE_ESP8266
|
||||
void Logger::log_str_(uint8_t level, const char *tag, int line, const __FlashStringHelper *msg) {
|
||||
if (level > this->level_for(tag) || this->main_task_recursion_guard_)
|
||||
return;
|
||||
RecursionGuard guard(this->main_task_recursion_guard_);
|
||||
LogBuffer buf{this->tx_buffer_, ESPHOME_LOGGER_TX_BUFFER_SIZE};
|
||||
buf.write_header(level, tag, line, nullptr);
|
||||
buf.write_unformatted_body_p(reinterpret_cast<PGM_P>(msg));
|
||||
this->send_unformatted_line_(level, tag, buf);
|
||||
}
|
||||
#endif // USE_ESP8266
|
||||
|
||||
#if defined(USE_LOGGER_RUNTIME_TAG_LEVELS) || defined(USE_LOGGER_BOOT_LOG_PER_TAG_LEVELS)
|
||||
// Level for tag from a per-tag map, or fallback when the tag has none; tags may live in flash
|
||||
static inline uint8_t tag_level(const std::map<const char *, uint8_t, CStrCompare> &levels, const char *tag,
|
||||
|
||||
@@ -223,6 +223,12 @@ class Logger final : public Component {
|
||||
// NOLINTNEXTLINE(readability-identifier-naming)
|
||||
void log_vprintf_(uint8_t level, const char *tag, int line, const __FlashStringHelper *format, va_list args);
|
||||
#endif
|
||||
// Copies a line with no '%' without formatting; false means the caller must format it
|
||||
bool log_str_(uint8_t level, const char *tag, int line, const char *msg); // NOLINT
|
||||
#ifdef USE_ESP8266
|
||||
// NOLINTNEXTLINE(readability-identifier-naming)
|
||||
void log_str_(uint8_t level, const char *tag, int line, const __FlashStringHelper *msg);
|
||||
#endif
|
||||
|
||||
protected:
|
||||
// RAII guard for recursion flags - sets flag on construction, clears on destruction
|
||||
@@ -292,7 +298,7 @@ class Logger final : public Component {
|
||||
}
|
||||
|
||||
// Helper to write log buffer to console if logging is enabled
|
||||
inline void HOT write_log_buffer_to_console_(LogBuffer &buf) {
|
||||
inline void HOT ESPHOME_ALWAYS_INLINE write_log_buffer_to_console_(LogBuffer &buf) {
|
||||
if (this->baud_rate_ > 0)
|
||||
this->write_to_console_(buf);
|
||||
}
|
||||
@@ -321,6 +327,21 @@ class Logger final : public Component {
|
||||
this->write_log_buffer_to_console_(buf);
|
||||
}
|
||||
|
||||
inline void send_unformatted_line_(uint8_t level, const char *tag, LogBuffer &buf);
|
||||
|
||||
// True on the task that runs the main loop, and always on single-task platforms
|
||||
inline bool HOT is_main_task_() const {
|
||||
#if defined(USE_ESP32) || defined(USE_LIBRETINY)
|
||||
return xTaskGetCurrentTaskHandle() == this->main_task_;
|
||||
#elif defined(USE_ZEPHYR)
|
||||
return k_current_get() == this->main_task_;
|
||||
#elif defined(USE_HOST)
|
||||
return pthread_equal(pthread_self(), this->main_thread_);
|
||||
#else
|
||||
return true;
|
||||
#endif
|
||||
}
|
||||
|
||||
#ifdef USE_ESPHOME_TASK_LOG_BUFFER
|
||||
// Helper to format a pre-formatted message from the task log buffer and notify listeners
|
||||
inline void HOT format_buffered_message_and_notify_(uint8_t level, const char *tag, uint16_t line,
|
||||
|
||||
@@ -9,7 +9,7 @@ void HOT Logger::write_msg_(const char *msg, uint16_t len) {
|
||||
char buffer[TIMESTAMP_LEN + ESPHOME_LOGGER_TX_BUFFER_SIZE + HEADROOM];
|
||||
|
||||
time_t rawtime;
|
||||
time(&rawtime);
|
||||
::time(&rawtime);
|
||||
struct tm timeinfo;
|
||||
localtime_r(&rawtime, &timeinfo); // Thread-safe version
|
||||
size_t pos = strftime(buffer, TIMESTAMP_LEN + 1, "[%H:%M:%S]", &timeinfo);
|
||||
|
||||
@@ -34,6 +34,6 @@ async def to_code(config: ConfigType) -> None:
|
||||
levels = list(LOG_LEVELS)
|
||||
index = levels.index(CORE.data[DOMAIN][CONF_LEVEL])
|
||||
levels = levels[: index + 1]
|
||||
var = await select.new_select(config, options=levels)
|
||||
var = await select.new_select(config, options=levels, fixed_options=True)
|
||||
await register_parented(var, parent)
|
||||
await register_component(var, config)
|
||||
@@ -103,7 +103,9 @@ async def to_code(config: ConfigType) -> None:
|
||||
)
|
||||
|
||||
await cg.register_component(var, config)
|
||||
await select.register_select(var, config, options=list(options_map.keys()))
|
||||
await select.register_select(
|
||||
var, config, options=list(options_map.keys()), fixed_options=True
|
||||
)
|
||||
|
||||
parent = await cg.get_variable(config[CONF_MODBUS_CONTROLLER_ID])
|
||||
cg.add(parent.add_sensor_item(var))
|
||||
|
||||
@@ -56,7 +56,8 @@ static constexpr uint8_t PIN_MAX_UNCONFIRMED_SENDS = 2;
|
||||
// The attachment is polled every 4 s; the signal and operator queries only every 4th poll
|
||||
static constexpr uint32_t ATTACH_POLL_MS = 4000;
|
||||
static constexpr uint8_t ATTACH_DIAG_EVERY = 4;
|
||||
static constexpr int RADIO_STATE_RF_OFF = 4; // CFUN=4: transmit and receive off, SIM still powered
|
||||
static constexpr int RADIO_STATE_MINIMUM = 0; // CFUN=0: radio off and the SIM powered down
|
||||
static constexpr int RADIO_STATE_RF_OFF = 4; // CFUN=4: transmit and receive off, SIM still powered
|
||||
static constexpr uint8_t UART_EVENT_QUEUE_SIZE = 10;
|
||||
static constexpr uint32_t BAUD_PREF_HASH = 76007670UL;
|
||||
static constexpr uint32_t PIN_PREF_HASH = 76007671UL;
|
||||
@@ -670,22 +671,28 @@ ModemState ModemComponent::handle_state_enabling_() {
|
||||
this->set_fault_(ModemFault::MODEM_FAULT_TEARDOWN);
|
||||
return this->retry_(ModemState::MODEM_STATE_ENABLING, this->next_probe_delay_());
|
||||
}
|
||||
this->probe_backoff_.store(0, std::memory_order_relaxed);
|
||||
// esp_modem keeps the last line before OK as the reply, which can be the echoed command
|
||||
if (this->dce_->set_echo(false) != command_result::OK) {
|
||||
ESP_LOGW(TAG, "Failed to disable command echo");
|
||||
}
|
||||
|
||||
int radio_state = 0;
|
||||
if (this->dce_->get_radio_state(radio_state) != command_result::OK) {
|
||||
ESP_LOGW(TAG, "Failed to read the radio state; skipping the radio off step");
|
||||
} else if (radio_state != RADIO_STATE_RF_OFF) {
|
||||
// Drops a session left by a warm reboot; unlike CFUN=0, which powers the SIM down on the SIM7600, CFUN=4
|
||||
// keeps the SIM readable for the PIN and the APN. A modem that refuses is used as it is.
|
||||
if (this->dce_->set_radio_state(RADIO_STATE_RF_OFF) != command_result::OK) {
|
||||
ESP_LOGW(TAG, "Failed to turn the radio off");
|
||||
}
|
||||
// The SIM7670G only uses an APN written at CFUN=0 (else the attach fails with an ESM failure); CFUN=0 also drops a
|
||||
// session left by a warm reboot. A modem that refuses gets the APN after the SIM check instead, as before.
|
||||
esp_modem::PdpContext pdp(this->apn_);
|
||||
this->apn_pending_ = this->dce_->set_radio_state(RADIO_STATE_MINIMUM) != command_result::OK ||
|
||||
this->dce_->set_pdp_context(pdp) != command_result::OK;
|
||||
if (this->apn_pending_) {
|
||||
ESP_LOGW(TAG, "Failed to set the APN with the SIM powered down; setting it after the SIM check");
|
||||
}
|
||||
// CFUN=4 powers the SIM up with the radio still off; a modem without it needs CFUN=1 to bring the SIM back
|
||||
if (this->dce_->set_radio_state(RADIO_STATE_RF_OFF) != command_result::OK &&
|
||||
this->dce_->set_radio_state(1) != command_result::OK) {
|
||||
ESP_LOGW(TAG, "Failed to power the SIM up");
|
||||
this->set_fault_(ModemFault::MODEM_FAULT_SETUP);
|
||||
this->modem_synced_ = true; // it answers, so a disable() still tears it down
|
||||
return this->retry_(ModemState::MODEM_STATE_ENABLING, this->next_probe_delay_());
|
||||
}
|
||||
this->probe_backoff_.store(0, std::memory_order_relaxed);
|
||||
|
||||
if (this->baud_rate_ != 0 && this->baud_rate_ != this->current_baud_rate_) {
|
||||
ESP_LOGD(TAG, "Setting baud rate: %d -> %d", this->current_baud_rate_, this->baud_rate_);
|
||||
@@ -752,7 +759,7 @@ ModemState ModemComponent::handle_state_connecting_() {
|
||||
return ModemState::MODEM_STATE_ENABLING;
|
||||
}
|
||||
}
|
||||
// ENABLING left the radio at CFUN=4, where the SIM answers but the modem cannot attach yet
|
||||
// ENABLING powered the SIM up (CFUN=4, or CFUN=1 on a modem without it), so the SIM answers
|
||||
switch (this->prepare_sim_()) {
|
||||
case SimResult::SIM_RESULT_READY:
|
||||
break;
|
||||
@@ -769,13 +776,14 @@ ModemState ModemComponent::handle_state_connecting_() {
|
||||
this->target_state_.store(ModemState::MODEM_STATE_DISABLED);
|
||||
return ModemState::MODEM_STATE_DISCONNECTING;
|
||||
}
|
||||
// The APN has to be in the PDP context before the radio comes on and the modem attaches; esp_modem only
|
||||
// sends it when PPP starts
|
||||
esp_modem::PdpContext pdp(this->apn_);
|
||||
if (this->dce_->set_pdp_context(pdp) != command_result::OK) {
|
||||
ESP_LOGW(TAG, "Failed to set the APN");
|
||||
this->set_fault_(ModemFault::MODEM_FAULT_SETUP);
|
||||
return this->retry_(ModemState::MODEM_STATE_CONNECTING);
|
||||
if (this->apn_pending_) {
|
||||
esp_modem::PdpContext pdp(this->apn_);
|
||||
if (this->dce_->set_pdp_context(pdp) != command_result::OK) {
|
||||
ESP_LOGW(TAG, "Failed to set the APN");
|
||||
this->set_fault_(ModemFault::MODEM_FAULT_SETUP);
|
||||
return this->retry_(ModemState::MODEM_STATE_CONNECTING);
|
||||
}
|
||||
this->apn_pending_ = false;
|
||||
}
|
||||
if (this->dce_->set_radio_state(1) != command_result::OK) {
|
||||
ESP_LOGW(TAG, "Failed to turn the radio on");
|
||||
@@ -862,11 +870,12 @@ ModemState ModemComponent::handle_state_disconnecting_() {
|
||||
ESP_LOGW(TAG, "Failed to leave data mode");
|
||||
this->set_teardown_fault_();
|
||||
}
|
||||
// Turn the radio off; waiting for the lost IP event could take up to 2 min. A reconnect keeps the SIM powered
|
||||
// so it is not unlocked again; only disable() powers it down.
|
||||
const bool reconnect = this->target_state_.load() == ModemState::MODEM_STATE_CONNECTED;
|
||||
if (this->dce_->set_radio_state(reconnect ? RADIO_STATE_RF_OFF : 0) == command_result::OK) {
|
||||
// Turn the radio off; waiting for the lost IP event could take up to 2 min. The SIM is powered down as well, as
|
||||
// ENABLING does anyway on a reconnect.
|
||||
if (this->dce_->set_radio_state(RADIO_STATE_MINIMUM) == command_result::OK) {
|
||||
ESP_LOGI(TAG, "Radio off");
|
||||
} else if (this->dce_->set_radio_state(RADIO_STATE_RF_OFF) == command_result::OK) {
|
||||
ESP_LOGW(TAG, "Radio off, but the SIM could not be powered down");
|
||||
} else {
|
||||
ESP_LOGW(TAG, "Radio off failed");
|
||||
this->set_teardown_fault_();
|
||||
|
||||
@@ -299,6 +299,7 @@ class ModemComponent : public Component {
|
||||
bool connect_timeout_armed_{false};
|
||||
bool modem_synced_{false}; // a modem that never answered has nothing to tear down
|
||||
bool resync_{false}; // the attachment read failed, so CONNECTING syncs again first
|
||||
bool apn_pending_{false}; // ENABLING could not set the APN at CFUN=0, so CONNECTING sets it after the SIM check
|
||||
#ifdef USE_MODEM_SEND_AT
|
||||
char at_command_[AT_COMMAND_SIZE];
|
||||
#endif
|
||||
|
||||
@@ -1,4 +1,5 @@
|
||||
from collections.abc import Callable
|
||||
import logging
|
||||
|
||||
from esphome import automation
|
||||
import esphome.codegen as cg
|
||||
@@ -8,6 +9,8 @@ import esphome.config_validation as cv
|
||||
from esphome.const import CONF_DURATION, CONF_ID, CONF_MODE, CONF_VOLTAGE
|
||||
from esphome.types import ConfigType
|
||||
|
||||
_LOGGER = logging.getLogger(__name__)
|
||||
|
||||
CODEOWNERS = ["@tomaszduda23"]
|
||||
DEPENDENCIES = ["i2c"]
|
||||
DOMAIN = "npm1300"
|
||||
@@ -42,20 +45,18 @@ NPM1300_CHILD_SCHEMA = cv.Schema(
|
||||
{cv.GenerateID(CONF_NPM1300_ID): cv.use_id(NPM1300Component)}
|
||||
)
|
||||
|
||||
# Status registers read on each update: VBUSINSTATUS, BCHGCHARGESTATUS and BCHGERRREASON
|
||||
NPM1300Status = npm1300_ns.struct("NPM1300Status")
|
||||
# The status registers read on each update
|
||||
STATUS_PUBLISHER = cg.HubFunction(
|
||||
"status",
|
||||
cg.void,
|
||||
[
|
||||
(cg.uint8, "vbus_status"),
|
||||
(cg.uint8, "charge_status"),
|
||||
(cg.uint8, "error_reason"),
|
||||
],
|
||||
[(NPM1300Status.operator("const").operator("ref"), "status")],
|
||||
define="USE_NPM1300_STATUS_PUBLISHER",
|
||||
)
|
||||
VBUS_STATUS = STATUS_PUBLISHER.param("vbus_status")
|
||||
CHARGE_STATUS = STATUS_PUBLISHER.param("charge_status")
|
||||
ERROR_REASON = STATUS_PUBLISHER.param("error_reason")
|
||||
STATUS = STATUS_PUBLISHER.param("status")
|
||||
VBUS_STATUS = STATUS.vbus
|
||||
CHARGE_STATUS = STATUS.charge
|
||||
ERROR_REASON = STATUS.error_reason
|
||||
# The ADC results and the amperes per code of the battery current
|
||||
DATA_PUBLISHER = cg.HubFunction(
|
||||
"data",
|
||||
@@ -77,7 +78,10 @@ OUTPUTS = NPM1300Output.options(
|
||||
# Written for a field the YAML leaves out
|
||||
NPM1300_UNSET = 0xFF
|
||||
# ADCNTCRSEL codes, by kiloohms
|
||||
THERMISTOR_NONE = "none"
|
||||
THERMISTOR_RESISTANCES = {10: 1, 47: 2, 100: 3}
|
||||
# B25/50 of the thermistors the product specification lists for each resistance, by kiloohms
|
||||
THERMISTOR_BETAS = {10: 3380, 47: 4050, 100: 4250}
|
||||
LDO_MODES = {"load_switch": 0, "ldo": 1}
|
||||
TERMINATION_CURRENTS = {"10%": 0, "20%": 1}
|
||||
# BCHGVTRICKLESEL, by millivolts
|
||||
@@ -101,14 +105,13 @@ def _one_of_units(
|
||||
validator: Callable[[object], float],
|
||||
allowed: list[int],
|
||||
unit: str,
|
||||
scale: float = 1000,
|
||||
) -> Callable[[object], float]:
|
||||
"""Validate a quantity with `validator` and accept only the listed values, given in units * `scale`."""
|
||||
"""Validate a quantity with `validator` and accept only the listed values, given in thousandths of `unit`."""
|
||||
|
||||
def validate(value: object) -> float:
|
||||
value = validator(value)
|
||||
if round(value * scale) not in allowed:
|
||||
options = ", ".join(f"{v / scale:g}{unit}" for v in allowed)
|
||||
if _millis(value) not in allowed:
|
||||
options = ", ".join(f"{v / 1000:g}{unit}" for v in allowed)
|
||||
raise cv.Invalid(f"Must be one of {options}")
|
||||
return value
|
||||
|
||||
@@ -135,6 +138,23 @@ def _stepped(
|
||||
return validate
|
||||
|
||||
|
||||
def _thermistor_resistance(value: object) -> float | str:
|
||||
if isinstance(value, str) and value.lower() == THERMISTOR_NONE:
|
||||
return THERMISTOR_NONE
|
||||
value = cv.resistance(value)
|
||||
if round(value) not in (kilohms * 1000 for kilohms in THERMISTOR_RESISTANCES):
|
||||
options = ", ".join(f"{kilohms}kOhm" for kilohms in THERMISTOR_RESISTANCES)
|
||||
raise cv.Invalid(f"Must be one of {options} or {THERMISTOR_NONE}")
|
||||
return value
|
||||
|
||||
|
||||
def thermistor_beta(config: ConfigType) -> int:
|
||||
"""The B constant of the hub's thermistor: the configured one, or the one listed for its resistance."""
|
||||
if (beta := config.get(CONF_THERMISTOR_BETA)) is not None:
|
||||
return beta
|
||||
return THERMISTOR_BETAS[round(config[CONF_THERMISTOR_RESISTANCE] / 1000)]
|
||||
|
||||
|
||||
OUTPUT_VOLTAGE = _stepped(cv.voltage, 1000, 3300, 100, "V")
|
||||
VBUS_CURRENT_LIMIT = _stepped(cv.current, 100, 1500, 100, "A")
|
||||
|
||||
@@ -150,15 +170,58 @@ LDO_SCHEMA = BUCK_SCHEMA.extend(
|
||||
}
|
||||
)
|
||||
|
||||
CHARGER_SCHEMA = cv.Schema(
|
||||
WARM_TERMINATION_VOLTAGE_DEFAULT = 3.6
|
||||
|
||||
|
||||
def _warm_termination_default(config: ConfigType) -> ConfigType:
|
||||
"""Keep the default warm termination voltage at or below the termination voltage.
|
||||
|
||||
Warn when the default stays below a higher termination voltage.
|
||||
"""
|
||||
if (
|
||||
not isinstance(config, dict)
|
||||
or CONF_WARM_TERMINATION_VOLTAGE in config
|
||||
or CONF_TERMINATION_VOLTAGE not in config
|
||||
):
|
||||
return config
|
||||
try:
|
||||
termination = cv.voltage(config[CONF_TERMINATION_VOLTAGE])
|
||||
except cv.Invalid:
|
||||
# The schema reports it
|
||||
return config
|
||||
if termination < WARM_TERMINATION_VOLTAGE_DEFAULT - 0.001:
|
||||
# A warm battery is never charged higher than a cool one
|
||||
return {
|
||||
**config,
|
||||
CONF_WARM_TERMINATION_VOLTAGE: config[CONF_TERMINATION_VOLTAGE],
|
||||
}
|
||||
if termination > WARM_TERMINATION_VOLTAGE_DEFAULT + 0.001:
|
||||
_LOGGER.warning(
|
||||
"npm1300 '%s' is not set, so a warm battery (45 °C to 60 °C by default) stops "
|
||||
"charging at %.2fV instead of the %.2fV of '%s'. Set '%s' to the value your "
|
||||
"battery allows when warm",
|
||||
CONF_WARM_TERMINATION_VOLTAGE,
|
||||
WARM_TERMINATION_VOLTAGE_DEFAULT,
|
||||
termination,
|
||||
CONF_TERMINATION_VOLTAGE,
|
||||
CONF_WARM_TERMINATION_VOLTAGE,
|
||||
)
|
||||
return config
|
||||
|
||||
|
||||
_CHARGER_SETTINGS_SCHEMA = cv.Schema(
|
||||
{
|
||||
cv.Required(CONF_CHARGE_CURRENT): _stepped(cv.current, 32, 800, 2, "A"),
|
||||
cv.Optional(CONF_TERMINATION_VOLTAGE, default="4.2V"): _one_of_units(
|
||||
cv.voltage, TERMINATION_VOLTAGES, "V"
|
||||
# Defaults are the reset values of the product specification
|
||||
cv.Optional(CONF_CHARGE_CURRENT, default="32mA"): _stepped(
|
||||
cv.current, 32, 800, 2, "A"
|
||||
),
|
||||
cv.Optional(CONF_WARM_TERMINATION_VOLTAGE): _one_of_units(
|
||||
cv.Optional(CONF_TERMINATION_VOLTAGE, default="3.6V"): _one_of_units(
|
||||
cv.voltage, TERMINATION_VOLTAGES, "V"
|
||||
),
|
||||
cv.Optional(
|
||||
CONF_WARM_TERMINATION_VOLTAGE,
|
||||
default=f"{WARM_TERMINATION_VOLTAGE_DEFAULT}V",
|
||||
): _one_of_units(cv.voltage, TERMINATION_VOLTAGES, "V"),
|
||||
cv.Optional(CONF_TERMINATION_CURRENT, default="10%"): cv.enum(
|
||||
TERMINATION_CURRENTS
|
||||
),
|
||||
@@ -169,22 +232,37 @@ CHARGER_SCHEMA = cv.Schema(
|
||||
cv.current, list(DISCHARGE_CURRENT_LIMITS), "A"
|
||||
),
|
||||
cv.Optional(CONF_RECHARGE, default=True): cv.boolean,
|
||||
cv.Optional(CONF_ENABLED, default=True): cv.boolean,
|
||||
cv.Optional(CONF_ENABLED, default=False): cv.boolean,
|
||||
}
|
||||
)
|
||||
CHARGER_SCHEMA = cv.All(_warm_termination_default, _CHARGER_SETTINGS_SCHEMA)
|
||||
|
||||
|
||||
CONFIG_SCHEMA = (
|
||||
def _check_thermistor(config: ConfigType) -> ConfigType:
|
||||
"""A wrong thermistor shifts the battery temperature the charger sees, so charging needs one chosen."""
|
||||
resistance = config.get(CONF_THERMISTOR_RESISTANCE)
|
||||
if CONF_CHARGER in config and resistance is None:
|
||||
raise cv.Invalid(
|
||||
f"'{CONF_THERMISTOR_RESISTANCE}' is required with '{CONF_CHARGER}': set the nominal "
|
||||
f"resistance of the battery thermistor, or '{THERMISTOR_NONE}'",
|
||||
path=[CONF_THERMISTOR_RESISTANCE],
|
||||
)
|
||||
if CONF_THERMISTOR_BETA in config and resistance in (None, THERMISTOR_NONE):
|
||||
raise cv.Invalid(
|
||||
f"'{CONF_THERMISTOR_BETA}' needs '{CONF_THERMISTOR_RESISTANCE}' set to the "
|
||||
"resistance of the thermistor",
|
||||
path=[CONF_THERMISTOR_BETA],
|
||||
)
|
||||
return config
|
||||
|
||||
|
||||
CONFIG_SCHEMA = cv.All(
|
||||
cv.Schema(
|
||||
{
|
||||
cv.GenerateID(): cv.declare_id(NPM1300Component),
|
||||
cv.Optional(CONF_VBUS_CURRENT_LIMIT): VBUS_CURRENT_LIMIT,
|
||||
cv.Optional(CONF_THERMISTOR_RESISTANCE): _one_of_units(
|
||||
cv.resistance, list(THERMISTOR_RESISTANCES), "kOhm", scale=0.001
|
||||
),
|
||||
cv.Optional(CONF_THERMISTOR_BETA, default=3380): cv.int_range(
|
||||
min=1, max=65535
|
||||
),
|
||||
cv.Optional(CONF_THERMISTOR_RESISTANCE): _thermistor_resistance,
|
||||
cv.Optional(CONF_THERMISTOR_BETA): cv.int_range(min=1, max=65535),
|
||||
cv.Optional(CONF_CHARGER): CHARGER_SCHEMA,
|
||||
cv.Optional(CONF_BUCK1): BUCK_SCHEMA,
|
||||
cv.Optional(CONF_BUCK2): BUCK_SCHEMA,
|
||||
@@ -193,7 +271,8 @@ CONFIG_SCHEMA = (
|
||||
}
|
||||
)
|
||||
.extend(cv.polling_component_schema("60s"))
|
||||
.extend(i2c.i2c_device_schema(0x6B))
|
||||
.extend(i2c.i2c_device_schema(0x6B)),
|
||||
_check_thermistor,
|
||||
)
|
||||
|
||||
|
||||
@@ -208,7 +287,7 @@ def _vbus_current_limit_code(amps: float) -> int:
|
||||
|
||||
def _charger_config(config: ConfigType) -> cg.StructInitializer:
|
||||
termination = _termination_voltage_code(config[CONF_TERMINATION_VOLTAGE])
|
||||
warm = config.get(CONF_WARM_TERMINATION_VOLTAGE)
|
||||
warm = _termination_voltage_code(config[CONF_WARM_TERMINATION_VOLTAGE])
|
||||
return cg.StructInitializer(
|
||||
NPM1300ChargerConfig,
|
||||
("current", _millis(config[CONF_CHARGE_CURRENT]) // 2),
|
||||
@@ -217,10 +296,7 @@ def _charger_config(config: ConfigType) -> cg.StructInitializer:
|
||||
DISCHARGE_CURRENT_LIMITS[_millis(config[CONF_DISCHARGE_CURRENT_LIMIT])],
|
||||
),
|
||||
("termination_voltage", termination),
|
||||
(
|
||||
"warm_termination_voltage",
|
||||
termination if warm is None else _termination_voltage_code(warm),
|
||||
),
|
||||
("warm_termination_voltage", warm),
|
||||
("trickle_voltage", TRICKLE_VOLTAGES[_millis(config[CONF_TRICKLE_VOLTAGE])]),
|
||||
("termination_current", TERMINATION_CURRENTS[config[CONF_TERMINATION_CURRENT]]),
|
||||
("recharge", config[CONF_RECHARGE]),
|
||||
@@ -247,6 +323,13 @@ def _output_config(config: ConfigType | None) -> cg.StructInitializer:
|
||||
def _config(config: ConfigType) -> cg.StructInitializer:
|
||||
charger = config.get(CONF_CHARGER)
|
||||
resistance = config.get(CONF_THERMISTOR_RESISTANCE)
|
||||
if resistance is None:
|
||||
thermistor, beta = NPM1300_UNSET, 0
|
||||
elif resistance == THERMISTOR_NONE:
|
||||
thermistor, beta = 0, 0
|
||||
else:
|
||||
thermistor = THERMISTOR_RESISTANCES[round(resistance / 1000)]
|
||||
beta = thermistor_beta(config)
|
||||
return cg.StructInitializer(
|
||||
NPM1300Config,
|
||||
(
|
||||
@@ -254,13 +337,8 @@ def _config(config: ConfigType) -> cg.StructInitializer:
|
||||
cg.ArrayInitializer(*(_output_config(config.get(key)) for key in OUTPUTS)),
|
||||
),
|
||||
("charger", None if charger is None else _charger_config(charger)),
|
||||
("thermistor_beta", config[CONF_THERMISTOR_BETA]),
|
||||
(
|
||||
"thermistor",
|
||||
NPM1300_UNSET
|
||||
if resistance is None
|
||||
else THERMISTOR_RESISTANCES[round(resistance / 1000)],
|
||||
),
|
||||
("thermistor_beta", beta),
|
||||
("thermistor", thermistor),
|
||||
("charger_configured", charger is not None),
|
||||
)
|
||||
|
||||
|
||||
@@ -29,6 +29,7 @@ static constexpr uint16_t REG_BCHG_VTERM = 0x030C;
|
||||
static constexpr uint16_t REG_BCHG_VTERM_R = 0x030D;
|
||||
static constexpr uint16_t REG_BCHG_VTRICKLE_SEL = 0x030E;
|
||||
static constexpr uint16_t REG_BCHG_ITERM_SEL = 0x030F;
|
||||
static constexpr uint16_t REG_BCHG_NTC_STATUS = 0x0332;
|
||||
static constexpr uint16_t REG_BCHG_CHARGE_STATUS = 0x0334;
|
||||
static constexpr uint16_t REG_BCHG_ERR_REASON = 0x0336;
|
||||
static constexpr uint16_t REG_BUCK1_ENA_SET = 0x0400;
|
||||
@@ -53,6 +54,9 @@ static constexpr uint16_t REG_SHIP_TASK_SHIP_MODE = 0x0B02;
|
||||
|
||||
// VBUSINSTATUS
|
||||
static constexpr uint8_t VBUS_PRESENT = 0x01;
|
||||
// NTCSTATUS
|
||||
static constexpr uint8_t NTC_STATUS_COLD = 0x01;
|
||||
static constexpr uint8_t NTC_STATUS_HOT = 0x08;
|
||||
// BCHGCHARGESTATUS; bit 0 (battery detected) is reserved since C00 silicon
|
||||
static constexpr uint8_t CHARGE_STATUS_COMPLETED = 0x02;
|
||||
static constexpr uint8_t CHARGE_STATUS_TRICKLE = 0x04;
|
||||
@@ -107,26 +111,36 @@ float ntc_temperature(uint16_t code, float inv_beta) {
|
||||
#endif
|
||||
|
||||
#ifdef USE_TEXT_SENSOR
|
||||
PROGMEM_STRING_TABLE(ChargerStatusStrings, "Error", "Paused, Die Too Hot", "Charge Complete", "Trickle Charge",
|
||||
"Constant Current", "Constant Voltage", "Discharging", "Not Charging");
|
||||
// BCHGCHARGESTATUS bits in order of priority; bit i is reported as string i + 1
|
||||
PROGMEM_STRING_TABLE(ChargerStatusStrings, "Discharging", "Error", "Disabled", "Paused, PMIC Too Hot",
|
||||
"Charge Complete", "Trickle Charge", "Constant Current", "Constant Voltage",
|
||||
"Paused, Battery Too Cold", "Paused, Battery Too Hot", "Not Charging");
|
||||
static constexpr uint8_t CHARGER_STATUS_FIRST_BIT = 3;
|
||||
// BCHGCHARGESTATUS bits in order of priority, reported from string CHARGER_STATUS_FIRST_BIT on
|
||||
static const uint8_t CHARGER_STATUS_BITS[] PROGMEM = {CHARGE_STATUS_DIE_TEMP_HIGH, CHARGE_STATUS_COMPLETED,
|
||||
CHARGE_STATUS_TRICKLE, CHARGE_STATUS_CONSTANT_CURRENT,
|
||||
CHARGE_STATUS_CONSTANT_VOLTAGE};
|
||||
|
||||
static uint8_t charger_status_index(uint8_t vbus_status, uint8_t charge_status, uint8_t error_reason) {
|
||||
if (error_reason != 0)
|
||||
static uint8_t charger_status_index(const NPM1300Status &status) {
|
||||
if ((status.vbus & VBUS_PRESENT) == 0)
|
||||
return 0;
|
||||
if (status.error_reason != 0)
|
||||
return 1;
|
||||
if ((status.charger_enable & 1) == 0)
|
||||
return 2;
|
||||
for (size_t i = 0; i < sizeof(CHARGER_STATUS_BITS); i++) {
|
||||
if ((charge_status & progmem_read_byte(&CHARGER_STATUS_BITS[i])) != 0)
|
||||
return i + 1;
|
||||
if ((status.charge & progmem_read_byte(&CHARGER_STATUS_BITS[i])) != 0)
|
||||
return CHARGER_STATUS_FIRST_BIT + i;
|
||||
}
|
||||
return (vbus_status & VBUS_PRESENT) == 0 ? 6 : 7;
|
||||
// The charger stops without an error outside the cold and hot limits of the battery temperature
|
||||
if ((status.ntc & NTC_STATUS_COLD) != 0)
|
||||
return 8;
|
||||
if ((status.ntc & NTC_STATUS_HOT) != 0)
|
||||
return 9;
|
||||
return 10;
|
||||
}
|
||||
|
||||
void publish_charger_status(text_sensor::TextSensor *sensor, uint8_t &last, uint8_t vbus_status, uint8_t charge_status,
|
||||
uint8_t error_reason) {
|
||||
const uint8_t index = charger_status_index(vbus_status, charge_status, error_reason);
|
||||
void publish_charger_status(text_sensor::TextSensor *sensor, uint8_t &last, const NPM1300Status &status) {
|
||||
const uint8_t index = charger_status_index(status);
|
||||
if (index == last)
|
||||
return;
|
||||
last = index;
|
||||
@@ -198,7 +212,7 @@ bool NPM1300Component::configure_charger_(const NPM1300Config &config) {
|
||||
split_9bit(charger.discharge_limit, discharge);
|
||||
uint8_t disable = charger.recharge ? 0 : CHARGER_DISABLE_RECHARGE;
|
||||
// Without a thermistor the charger reports an NTC error and does not charge
|
||||
if (config.thermistor == NPM1300_UNSET)
|
||||
if (config.thermistor == 0 || config.thermistor == NPM1300_UNSET)
|
||||
disable |= CHARGER_DISABLE_NTC;
|
||||
const uint8_t enable = (CHARGER_DISABLE_RECHARGE | CHARGER_DISABLE_NTC) & ~disable;
|
||||
if (this->write_register16(REG_BCHG_ISET_MSB, current, sizeof(current)) != i2c::ERROR_OK ||
|
||||
@@ -292,33 +306,40 @@ void NPM1300Component::hibernate(uint32_t duration_ms) {
|
||||
}
|
||||
|
||||
void NPM1300Component::update() {
|
||||
uint8_t vbus_status;
|
||||
uint8_t charge_status;
|
||||
uint8_t error_reason;
|
||||
if (!this->status_comm_check(this->read_byte_reg16(REG_VBUSIN_STATUS, &vbus_status) &&
|
||||
this->read_byte_reg16(REG_BCHG_CHARGE_STATUS, &charge_status) &&
|
||||
this->read_byte_reg16(REG_BCHG_ERR_REASON, &error_reason)))
|
||||
NPM1300Status status;
|
||||
// NTCSTATUS, DIETEMPSTATUS and BCHGCHARGESTATUS
|
||||
uint8_t charger_status[3];
|
||||
if (!this->status_comm_check(this->read_byte_reg16(REG_VBUSIN_STATUS, &status.vbus) &&
|
||||
this->read_byte_reg16(REG_BCHG_ENABLE_SET, &status.charger_enable) &&
|
||||
this->read_register16(REG_BCHG_NTC_STATUS, charger_status, sizeof(charger_status)) ==
|
||||
i2c::ERROR_OK &&
|
||||
this->read_byte_reg16(REG_BCHG_ERR_REASON, &status.error_reason)))
|
||||
return;
|
||||
status.ntc = charger_status[0];
|
||||
status.charge = charger_status[2];
|
||||
// Without a thermistor the NTC pin is grounded and can read as a hot battery
|
||||
if (progmem_read_byte(&this->config_->thermistor) == 0)
|
||||
status.ntc = 0;
|
||||
|
||||
// The chip goes back to its default VBUS current limit whenever VBUS is removed
|
||||
const bool vbus_present = (vbus_status & VBUS_PRESENT) != 0;
|
||||
const bool vbus_present = (status.vbus & VBUS_PRESENT) != 0;
|
||||
if (vbus_present && !this->vbus_present_ && this->vbus_current_limit_ != NPM1300_UNSET &&
|
||||
!this->status_comm_check(this->apply_vbus_current_limit_()))
|
||||
return;
|
||||
this->vbus_present_ = vbus_present;
|
||||
|
||||
if (error_reason != this->last_error_reason_) {
|
||||
this->last_error_reason_ = error_reason;
|
||||
if (error_reason != 0) {
|
||||
ESP_LOGW(TAG, "Charger error, BCHGERRREASON 0x%02X", error_reason);
|
||||
if (status.error_reason != this->last_error_reason_) {
|
||||
this->last_error_reason_ = status.error_reason;
|
||||
if (status.error_reason != 0) {
|
||||
ESP_LOGW(TAG, "Charger error, BCHGERRREASON 0x%02X", status.error_reason);
|
||||
}
|
||||
}
|
||||
#ifdef USE_NPM1300_STATUS_PUBLISHER
|
||||
if (this->status_publisher_ != nullptr)
|
||||
this->status_publisher_(vbus_status, charge_status, error_reason);
|
||||
this->status_publisher_(status);
|
||||
#endif
|
||||
#ifdef USE_SWITCH
|
||||
if (!this->status_comm_check(this->publish_switches_()))
|
||||
if (!this->status_comm_check(this->publish_switches_(status)))
|
||||
return;
|
||||
#endif
|
||||
|
||||
@@ -348,16 +369,10 @@ void NPM1300Component::update() {
|
||||
}
|
||||
|
||||
#ifdef USE_SWITCH
|
||||
bool NPM1300Component::publish_switches_() {
|
||||
bool NPM1300Component::publish_switches_(const NPM1300Status &status) {
|
||||
PUBLISH_STATE(this->charger_switch_, (status.charger_enable & 1) != 0);
|
||||
uint8_t value;
|
||||
bool ok = true;
|
||||
if (this->charger_switch_ != nullptr) {
|
||||
if (this->read_byte_reg16(REG_BCHG_ENABLE_SET, &value)) {
|
||||
this->charger_switch_->publish_state((value & 1) != 0);
|
||||
} else {
|
||||
ok = false;
|
||||
}
|
||||
}
|
||||
switch_::Switch *const bucks[2] = {this->buck1_switch_, this->buck2_switch_};
|
||||
if (bucks[0] != nullptr || bucks[1] != nullptr) {
|
||||
if (this->read_byte_reg16(REG_BUCK_STATUS, &value)) {
|
||||
@@ -461,7 +476,9 @@ void NPM1300Component::dump_config() {
|
||||
ESP_LOGCONFIG(TAG, " Enabled at boot: %s", YESNO(output.enabled != 0));
|
||||
}
|
||||
}
|
||||
if (config.thermistor != NPM1300_UNSET) {
|
||||
if (config.thermistor == 0) {
|
||||
ESP_LOGCONFIG(TAG, " Thermistor: none");
|
||||
} else if (config.thermistor != NPM1300_UNSET) {
|
||||
ESP_LOGCONFIG(TAG, " Thermistor B constant: %u", config.thermistor_beta);
|
||||
}
|
||||
#ifdef USE_SENSOR
|
||||
|
||||
@@ -54,10 +54,19 @@ struct NPM1300Config {
|
||||
NPM1300OutputConfig outputs[NPM1300_OUTPUT_COUNT];
|
||||
NPM1300ChargerConfig charger; // only applied when charger_configured
|
||||
uint16_t thermistor_beta;
|
||||
uint8_t thermistor; // ADCNTCRSEL code: 1 for 10 kOhm, 2 for 47 kOhm, 3 for 100 kOhm, or NPM1300_UNSET
|
||||
uint8_t thermistor; // ADCNTCRSEL code: 0 for none, 1 for 10 kOhm, 2 for 47 kOhm, 3 for 100 kOhm, or NPM1300_UNSET
|
||||
bool charger_configured;
|
||||
};
|
||||
|
||||
/// The status registers read on every update
|
||||
struct NPM1300Status {
|
||||
uint8_t vbus; // VBUSINSTATUS
|
||||
uint8_t charger_enable; // BCHGENABLESET
|
||||
uint8_t ntc; // NTCSTATUS
|
||||
uint8_t charge; // BCHGCHARGESTATUS
|
||||
uint8_t error_reason; // BCHGERRREASON
|
||||
};
|
||||
|
||||
#ifdef USE_NPM1300_DATA_PUBLISHER
|
||||
/// The 10-bit ADC result whose high 8 bits are adc[msb] and low 2 bits are at `shift` in adc[lsbs]
|
||||
inline uint16_t adc_code(const uint8_t *adc, uint8_t msb, uint8_t lsbs, uint8_t shift) {
|
||||
@@ -68,8 +77,7 @@ float ntc_temperature(uint16_t code, float inv_beta);
|
||||
#endif
|
||||
#ifdef USE_TEXT_SENSOR
|
||||
/// Publish the charger status text, only when it differs from `last`
|
||||
void publish_charger_status(text_sensor::TextSensor *sensor, uint8_t &last, uint8_t vbus_status, uint8_t charge_status,
|
||||
uint8_t error_reason);
|
||||
void publish_charger_status(text_sensor::TextSensor *sensor, uint8_t &last, const NPM1300Status &status);
|
||||
#endif
|
||||
|
||||
class NPM1300Component : public PollingComponent, public i2c::I2CDevice {
|
||||
@@ -95,7 +103,7 @@ class NPM1300Component : public PollingComponent, public i2c::I2CDevice {
|
||||
float get_setup_priority() const override { return setup_priority::BUS - 1.0f; }
|
||||
|
||||
#ifdef USE_NPM1300_STATUS_PUBLISHER
|
||||
HUB_PUBLISHER(status, uint8_t, uint8_t, uint8_t)
|
||||
HUB_PUBLISHER(status, const NPM1300Status &)
|
||||
#endif
|
||||
#ifdef USE_NPM1300_DATA_PUBLISHER
|
||||
HUB_PUBLISHER(data, const uint8_t *, float)
|
||||
@@ -128,7 +136,7 @@ class NPM1300Component : public PollingComponent, public i2c::I2CDevice {
|
||||
#endif
|
||||
#ifdef USE_SWITCH
|
||||
/// Returns false when a status read failed
|
||||
bool publish_switches_();
|
||||
bool publish_switches_(const NPM1300Status &status);
|
||||
#endif
|
||||
|
||||
const NPM1300Config *config_;
|
||||
|
||||
@@ -21,7 +21,6 @@ from esphome.types import ConfigType
|
||||
from . import (
|
||||
ADC,
|
||||
CONF_NPM1300_ID,
|
||||
CONF_THERMISTOR_BETA,
|
||||
CONF_THERMISTOR_RESISTANCE,
|
||||
CONF_VBUS_CURRENT_LIMIT,
|
||||
DATA_PUBLISHER,
|
||||
@@ -30,7 +29,9 @@ from . import (
|
||||
DUMP_TAG,
|
||||
IBAT_SCALE,
|
||||
NPM1300_CHILD_SCHEMA,
|
||||
THERMISTOR_NONE,
|
||||
npm1300_ns,
|
||||
thermistor_beta,
|
||||
)
|
||||
|
||||
DEPENDENCIES = ["npm1300"]
|
||||
@@ -109,10 +110,10 @@ def _final_validate(config: ConfigType) -> None:
|
||||
if CONF_BATTERY_TEMPERATURE not in config:
|
||||
return
|
||||
hub = fv.get_declaring_config(config[CONF_NPM1300_ID])
|
||||
if CONF_THERMISTOR_RESISTANCE not in hub:
|
||||
if hub.get(CONF_THERMISTOR_RESISTANCE, THERMISTOR_NONE) == THERMISTOR_NONE:
|
||||
raise cv.Invalid(
|
||||
f"'{CONF_BATTERY_TEMPERATURE}' needs '{CONF_THERMISTOR_RESISTANCE}' "
|
||||
"set on the npm1300 component",
|
||||
f"'{CONF_BATTERY_TEMPERATURE}' needs '{CONF_THERMISTOR_RESISTANCE}' set to the "
|
||||
"nominal resistance of the battery thermistor on the npm1300 component",
|
||||
path=[CONF_BATTERY_TEMPERATURE],
|
||||
)
|
||||
|
||||
@@ -145,7 +146,7 @@ async def to_code(config: ConfigType) -> None:
|
||||
if (var := await sensors(CONF_BATTERY_TEMPERATURE, None)) is not None:
|
||||
hub_config = next(c for c in CORE.config[DOMAIN] if c[CONF_ID] == hub_id)
|
||||
state = npm1300_ns.ntc_temperature(
|
||||
_adc(2, 5, 2), 1.0 / hub_config[CONF_THERMISTOR_BETA]
|
||||
_adc(2, 5, 2), 1.0 / thermistor_beta(hub_config)
|
||||
)
|
||||
_add_adc_sensor(hub, var, state, "Battery Temperature")
|
||||
await sensors(CONF_VBUS_CURRENT_LIMIT, hub.set_vbus_current_limit_sensor)
|
||||
@@ -15,6 +15,7 @@ from . import (
|
||||
CONF_THERMISTOR_RESISTANCE,
|
||||
NPM1300_CHILD_SCHEMA,
|
||||
OUTPUTS,
|
||||
THERMISTOR_NONE,
|
||||
)
|
||||
|
||||
DEPENDENCIES = ["npm1300"]
|
||||
@@ -44,11 +45,18 @@ def _final_validate(config: ConfigType) -> None:
|
||||
if CONF_CHARGER not in config:
|
||||
return
|
||||
hub = fv.get_declaring_config(config[CONF_NPM1300_ID])
|
||||
# Only the charger block turns off the NTC check, without which the charger never starts
|
||||
if CONF_CHARGER not in hub and CONF_THERMISTOR_RESISTANCE not in hub:
|
||||
# A wrong thermistor shifts the battery temperature the charger sees
|
||||
if CONF_THERMISTOR_RESISTANCE not in hub:
|
||||
raise cv.Invalid(
|
||||
f"The '{CONF_CHARGER}' switch needs a '{CONF_CHARGER}' block or "
|
||||
f"'{CONF_THERMISTOR_RESISTANCE}' on the npm1300 component",
|
||||
f"The '{CONF_CHARGER}' switch needs '{CONF_THERMISTOR_RESISTANCE}' on the npm1300 "
|
||||
f"component: the nominal resistance of the battery thermistor, or '{THERMISTOR_NONE}'",
|
||||
path=[CONF_CHARGER],
|
||||
)
|
||||
# Only the charger block turns off the NTC check, without which the charger never starts
|
||||
if CONF_CHARGER not in hub and hub[CONF_THERMISTOR_RESISTANCE] == THERMISTOR_NONE:
|
||||
raise cv.Invalid(
|
||||
f"The '{CONF_CHARGER}' switch needs a '{CONF_CHARGER}' block when "
|
||||
f"'{CONF_THERMISTOR_RESISTANCE}' is '{THERMISTOR_NONE}'",
|
||||
path=[CONF_CHARGER],
|
||||
)
|
||||
|
||||
|
||||
@@ -11,14 +11,12 @@ from esphome.cpp_generator import (
|
||||
from esphome.types import ConfigType
|
||||
|
||||
from . import (
|
||||
CHARGE_STATUS,
|
||||
CONF_NPM1300_ID,
|
||||
DUMP,
|
||||
DUMP_TAG,
|
||||
ERROR_REASON,
|
||||
NPM1300_CHILD_SCHEMA,
|
||||
STATUS,
|
||||
STATUS_PUBLISHER,
|
||||
VBUS_STATUS,
|
||||
npm1300_ns,
|
||||
)
|
||||
|
||||
@@ -48,9 +46,7 @@ async def to_code(config: ConfigType) -> None:
|
||||
)
|
||||
STATUS_PUBLISHER.add(
|
||||
hub,
|
||||
npm1300_ns.publish_charger_status(
|
||||
parent_ref(var), last, VBUS_STATUS, CHARGE_STATUS, ERROR_REASON
|
||||
),
|
||||
npm1300_ns.publish_charger_status(parent_ref(var), last, STATUS),
|
||||
)
|
||||
DUMP.add(
|
||||
hub,
|
||||
|
||||
@@ -1,3 +1,6 @@
|
||||
from dataclasses import dataclass, field
|
||||
import functools
|
||||
|
||||
from esphome import automation
|
||||
import esphome.codegen as cg
|
||||
import esphome.config_validation as cv
|
||||
@@ -26,7 +29,7 @@ from esphome.core.entity_helpers import (
|
||||
)
|
||||
from esphome.cpp_generator import MockObj, MockObjClass, TemplateArguments
|
||||
from esphome.cpp_types import global_ns
|
||||
from esphome.types import ConfigType
|
||||
from esphome.types import ConfigType, TemplateArgsType
|
||||
|
||||
CODEOWNERS = ["@esphome/core"]
|
||||
DOMAIN = "select"
|
||||
@@ -47,6 +50,25 @@ SELECT_OPERATION_OPTIONS = SelectOperation.options(
|
||||
)
|
||||
|
||||
|
||||
@dataclass
|
||||
class SelectData:
|
||||
# Options of each select whose option list never changes at run time, by id
|
||||
fixed_options: dict[str, list[str]] = field(default_factory=dict)
|
||||
|
||||
|
||||
def _get_data() -> SelectData:
|
||||
if DOMAIN not in CORE.data:
|
||||
CORE.data[DOMAIN] = SelectData()
|
||||
return CORE.data[DOMAIN]
|
||||
|
||||
|
||||
def _record_options(
|
||||
config: ConfigType, options: list[str], fixed_options: bool
|
||||
) -> None:
|
||||
if fixed_options:
|
||||
_get_data().fixed_options[config[CONF_ID].id] = options
|
||||
|
||||
|
||||
_SELECT_SCHEMA = (
|
||||
cv.ENTITY_BASE_SCHEMA.extend(entity_helpers.WEBSERVER_SORTING_SCHEMA)
|
||||
.extend(cv.MQTT_COMMAND_COMPONENT_SCHEMA)
|
||||
@@ -105,7 +127,11 @@ async def setup_select_core_(var, config, *, options: list[str]):
|
||||
await entity_helpers.setup_entity_integrations(var, config)
|
||||
|
||||
|
||||
async def register_select(var, config, *, options: list[str]):
|
||||
async def register_select(
|
||||
var, config, *, options: list[str], fixed_options: bool = False
|
||||
):
|
||||
"""``fixed_options=True`` when the options never change at run time, so ``select.set`` can use the index."""
|
||||
_record_options(config, options, fixed_options)
|
||||
if not CORE.has_id(config[CONF_ID]):
|
||||
var = cg.Pvariable(config[CONF_ID], var)
|
||||
queue_entity_register("select", config)
|
||||
@@ -113,9 +139,9 @@ async def register_select(var, config, *, options: list[str]):
|
||||
await setup_select_core_(var, config, options=options)
|
||||
|
||||
|
||||
async def new_select(config, *args, options: list[str]):
|
||||
async def new_select(config, *args, options: list[str], fixed_options: bool = False):
|
||||
var = cg.new_Pvariable(config[CONF_ID], *args)
|
||||
await register_select(var, config, options=options)
|
||||
await register_select(var, config, options=options, fixed_options=fixed_options)
|
||||
return var
|
||||
|
||||
|
||||
@@ -131,7 +157,10 @@ def sub_selects(
|
||||
config: ConfigType, *, parent: MockObj | ID | None = None
|
||||
) -> SubEntities:
|
||||
"""Return a SubEntities bound to new_select; ``apply`` statements can use ``entity``, ``index`` and ``option``."""
|
||||
return SubEntities(new_select, config, parent, APPLY_ENTITY)
|
||||
# Hub selects get their options from code generation only
|
||||
return SubEntities(
|
||||
functools.partial(new_select, fixed_options=True), config, parent, APPLY_ENTITY
|
||||
)
|
||||
|
||||
|
||||
@coroutine_with_priority(CoroPriority.CORE)
|
||||
@@ -146,13 +175,20 @@ OPERATION_BASE_SCHEMA = cv.Schema(
|
||||
)
|
||||
|
||||
|
||||
automation.register_apply_action(
|
||||
_SELECT_SET_SCHEMA = OPERATION_BASE_SCHEMA.extend(
|
||||
{
|
||||
cv.Required(CONF_OPTION): cv.templatable(cv.string_strict),
|
||||
}
|
||||
)
|
||||
|
||||
|
||||
def _option_index(config: ConfigType, option: str) -> str:
|
||||
return str(_get_data().fixed_options[config[CONF_ID].id].index(option))
|
||||
|
||||
|
||||
_set_option_builder = automation.apply_action_builder(
|
||||
"select.set",
|
||||
OPERATION_BASE_SCHEMA.extend(
|
||||
{
|
||||
cv.Required(CONF_OPTION): cv.templatable(cv.string_strict),
|
||||
}
|
||||
),
|
||||
_SELECT_SET_SCHEMA,
|
||||
automation.ApplyField(
|
||||
CONF_OPTION,
|
||||
"set_option",
|
||||
@@ -161,6 +197,32 @@ automation.register_apply_action(
|
||||
),
|
||||
call="make_call",
|
||||
)
|
||||
_set_index_builder = automation.apply_action_builder(
|
||||
"select.set",
|
||||
_SELECT_SET_SCHEMA,
|
||||
automation.ApplyField(CONF_OPTION, "set_index", cg.size_t, const_fn=_option_index),
|
||||
call="make_call",
|
||||
)
|
||||
|
||||
|
||||
@automation.register_action(
|
||||
"select.set", automation.ApplyAction, _SELECT_SET_SCHEMA, synchronous=True
|
||||
)
|
||||
async def select_set_to_code(
|
||||
config: ConfigType,
|
||||
action_id: ID,
|
||||
template_arg: TemplateArguments,
|
||||
args: TemplateArgsType,
|
||||
) -> MockObj:
|
||||
# A constant fixed option becomes its index
|
||||
await cg.get_variable(config[CONF_ID])
|
||||
option = config[CONF_OPTION]
|
||||
if not cg.is_template(option) and option in _get_data().fixed_options.get(
|
||||
config[CONF_ID].id, ()
|
||||
):
|
||||
return await _set_index_builder(config, action_id, template_arg, args)
|
||||
return await _set_option_builder(config, action_id, template_arg, args)
|
||||
|
||||
|
||||
automation.register_apply_action(
|
||||
"select.set_index",
|
||||
|
||||
@@ -113,7 +113,7 @@ async def to_code(config):
|
||||
milliseconds=SCHEDULER_DONT_RUN
|
||||
)
|
||||
await cg.register_component(var, component_config)
|
||||
await select.register_select(var, config, options=options)
|
||||
await select.register_select(var, config, options=options, fixed_options=True)
|
||||
|
||||
if CONF_LAMBDA in config:
|
||||
lambda_ = await cg.process_lambda(
|
||||
|
||||
@@ -36,13 +36,8 @@ void setup_with_restore(BaseTemplateSelect *sel_comp, ESPPreferenceObject &pref,
|
||||
}
|
||||
|
||||
void update_lambda(BaseTemplateSelect *sel_comp, const optional<std::string> &val) {
|
||||
if (val.has_value()) {
|
||||
if (!sel_comp->has_option(*val)) {
|
||||
ESP_LOGE(TAG, "Lambda returned an invalid option: %s", (*val).c_str());
|
||||
return;
|
||||
}
|
||||
if (val.has_value())
|
||||
sel_comp->publish_state(*val);
|
||||
}
|
||||
}
|
||||
|
||||
} // namespace esphome::template_
|
||||
@@ -115,7 +115,7 @@ async def to_code(config):
|
||||
options = list(options_map.values())
|
||||
mappings = list(options_map.keys())
|
||||
|
||||
var = await select.new_select(config, options=options)
|
||||
var = await select.new_select(config, options=options, fixed_options=True)
|
||||
if mappings:
|
||||
cg.add(var.set_select_mappings(mappings))
|
||||
hub = await cg.add_variable(var.set_tuya_parent, config[CONF_TUYA_ID])
|
||||
|
||||
@@ -614,9 +614,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, this->web_server_);
|
||||
{
|
||||
LockGuard guard{this->pending_mutex_};
|
||||
this->pending_sessions_.push_back(rsp);
|
||||
this->has_pending_sessions_.store(true, std::memory_order_release);
|
||||
}
|
||||
this->web_server_->enable_loop_soon_any_context();
|
||||
}
|
||||
|
||||
// clang-analyzer traces a false-positive leak path from loop() through
|
||||
// adopt_pending_sessions_main_loop_() into start_session_main_loop_() and
|
||||
// finally ArduinoJson. Suppress along the entire in-our-code call chain.
|
||||
// NOLINTBEGIN(clang-analyzer-cplusplus.NewDeleteLeaks)
|
||||
bool AsyncEventSource::loop() {
|
||||
// Fast path: one atomic load per tick. Slow path is out-of-line on connect.
|
||||
if (this->has_pending_sessions_.load(std::memory_order_acquire)) {
|
||||
this->adopt_pending_sessions_main_loop_();
|
||||
}
|
||||
|
||||
// Clean up dead sessions safely
|
||||
// This follows the ESP-IDF pattern where free_ctx marks resources as dead
|
||||
// and the main loop handles the actual cleanup to avoid race conditions
|
||||
for (size_t i = 0; i < this->sessions_.size();) {
|
||||
auto *ses = this->sessions_[i];
|
||||
// If the session has a dead socket (marked by destroy callback)
|
||||
if (ses->safe_to_delete()) {
|
||||
// destroy() already logged the close with the fd; don't double-log here.
|
||||
delete ses; // NOLINT(cppcoreguidelines-owning-memory)
|
||||
// Remove by swapping with last element (O(1) removal, order doesn't matter for sessions)
|
||||
this->sessions_[i] = this->sessions_.back();
|
||||
this->sessions_.pop_back();
|
||||
} else {
|
||||
ses->loop();
|
||||
++i;
|
||||
}
|
||||
}
|
||||
// A session still waiting for httpd to commit its context keeps the loop alive too
|
||||
return !this->sessions_.empty() || this->has_pending_sessions_.load(std::memory_order_acquire);
|
||||
}
|
||||
|
||||
void AsyncEventSource::adopt_pending_sessions_main_loop_() {
|
||||
std::vector<AsyncEventSourceResponse *> incoming;
|
||||
{
|
||||
LockGuard guard{this->pending_mutex_};
|
||||
incoming.swap(this->pending_sessions_);
|
||||
this->has_pending_sessions_.store(false, std::memory_order_relaxed);
|
||||
}
|
||||
for (auto *rsp : incoming) {
|
||||
// Already disconnected? Drop it; skip session start on a dead session.
|
||||
if (rsp->safe_to_delete()) {
|
||||
delete rsp; // NOLINT(cppcoreguidelines-owning-memory)
|
||||
continue;
|
||||
}
|
||||
// httpd commits the session context only after the creating handler returns, so stay
|
||||
// pending until then; httpd_req_cleanup() always commits it or calls destroy()
|
||||
if (!rsp->is_attached()) {
|
||||
LockGuard guard{this->pending_mutex_};
|
||||
this->pending_sessions_.push_back(rsp);
|
||||
this->has_pending_sessions_.store(true, std::memory_order_release);
|
||||
continue;
|
||||
}
|
||||
this->sessions_.push_back(rsp);
|
||||
rsp->start_session_main_loop_();
|
||||
}
|
||||
}
|
||||
// NOLINTEND(clang-analyzer-cplusplus.NewDeleteLeaks)
|
||||
|
||||
void AsyncEventSource::try_send_nodefer(const char *message, size_t message_len, const char *event, uint32_t id,
|
||||
uint32_t reconnect) {
|
||||
for (auto *ses : this->sessions_) {
|
||||
if (ses->is_connected()) {
|
||||
ses->try_send_nodefer(message, message_len, event, id, reconnect);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void AsyncEventSource::deferrable_send_state(void *source, bool detail_all, message_generator_t *message_generator) {
|
||||
// Skip if no connected clients to avoid unnecessary processing
|
||||
if (this->empty())
|
||||
return;
|
||||
for (auto *ses : this->sessions_) {
|
||||
if (ses->is_connected()) {
|
||||
ses->deferrable_send_state(source, detail_all, message_generator);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
AsyncEventSourceResponse::AsyncEventSourceResponse(AsyncWebServerRequest *request, AsyncEventSource *server,
|
||||
WebServer *ws)
|
||||
: server_(server), web_server_(ws), entities_iterator_(ws, this) {
|
||||
// Httpd task only. start_session_main_loop_() sends the greeting and starts the iterator.
|
||||
this->begin(request, SSE_STATUS, SSE_CONTENT_TYPE, SSE_HEADERS, SSE_FIRST_CHUNK);
|
||||
}
|
||||
static_assert(web_server_base::stream_head_iov_count(std::size(SSE_HEADERS) + WEB_SERVER_DEFAULT_HEADERS_COUNT) <=
|
||||
web_server_base::STREAM_HEAD_MAX_IOV,
|
||||
"the event stream head must go out in one write");
|
||||
|
||||
// NOLINTBEGIN(clang-analyzer-cplusplus.NewDeleteLeaks) false positive with ArduinoJson
|
||||
void AsyncEventSourceResponse::start_session_main_loop_() {
|
||||
auto *ws = this->web_server_;
|
||||
|
||||
// The tcp send buffer is empty on connect. A refusal is a closing session or a failed tail
|
||||
// allocation; nothing retries the greeting, so close and let the client reconnect.
|
||||
auto message = ws->get_config_json();
|
||||
if (!this->try_send_nodefer(message.c_str(), message.size(), "ping", millis(), 30000)) {
|
||||
ESP_LOGW(TAG, "Config not sent to fd %d", this->log_id());
|
||||
this->close();
|
||||
return;
|
||||
}
|
||||
|
||||
#ifdef USE_WEBSERVER_SORTING
|
||||
for (auto &group : ws->sorting_groups_) {
|
||||
json::JsonBuilder builder;
|
||||
JsonObject root = builder.root();
|
||||
root["name"] = json::linked(group.second.name);
|
||||
root["sorting_weight"] = group.second.weight;
|
||||
message = builder.serialize();
|
||||
|
||||
// a (very) large number of these should be able to be queued initially without defer
|
||||
// since the only thing in the send buffer at this point is the initial ping/config.
|
||||
// A refusal means the socket is full or closing; the remaining groups are not sent. The
|
||||
// session stays up on purpose: partial grouping beats a reconnect loop on a slow link.
|
||||
if (!this->try_send_nodefer(message.c_str(), message.size(), "sorting_group")) {
|
||||
ESP_LOGW(TAG, "Sorting groups not sent to fd %d", this->log_id());
|
||||
break;
|
||||
}
|
||||
}
|
||||
#endif
|
||||
|
||||
this->entities_iterator_.begin(ws->include_internal_);
|
||||
}
|
||||
// NOLINTEND(clang-analyzer-cplusplus.NewDeleteLeaks)
|
||||
|
||||
// helper for allowing only unique entries in the queue
|
||||
void AsyncEventSourceResponse::deq_push_back_with_dedup_(void *source, message_generator_t *message_generator) {
|
||||
DeferredEvent item(source, message_generator);
|
||||
|
||||
// Use range-based for loop instead of std::find_if to reduce template instantiation overhead and binary size
|
||||
for (auto &event : this->deferred_queue_) {
|
||||
if (event == item) {
|
||||
return; // Already in queue, no need to update since items are equal
|
||||
}
|
||||
}
|
||||
this->deferred_queue_.push_back(item);
|
||||
}
|
||||
|
||||
void AsyncEventSourceResponse::process_deferred_queue_() {
|
||||
if (this->is_closing()) {
|
||||
return;
|
||||
}
|
||||
while (!deferred_queue_.empty()) {
|
||||
DeferredEvent &de = deferred_queue_.front();
|
||||
if (this->send_json_(de.source_, de.message_generator_)) {
|
||||
if (this->is_closing() || deferred_queue_.empty()) {
|
||||
return;
|
||||
}
|
||||
// O(n) but memory efficiency is more important than speed here which is why std::vector was chosen
|
||||
deferred_queue_.erase(deferred_queue_.begin());
|
||||
} else {
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void AsyncEventSourceResponse::release_buffers_() {
|
||||
std::vector<DeferredEvent>().swap(this->deferred_queue_);
|
||||
this->tail_.reset();
|
||||
this->tail_cap_ = 0;
|
||||
this->tail_len_ = 0;
|
||||
this->tail_sent_ = 0;
|
||||
}
|
||||
|
||||
void AsyncEventSourceResponse::drain_tail_() {
|
||||
if (this->sending_ || this->is_closing() || this->tail_len_ == 0) {
|
||||
return;
|
||||
}
|
||||
SendGuard guard{*this};
|
||||
|
||||
const size_t remaining = this->tail_len_ - this->tail_sent_;
|
||||
const ssize_t sent = this->write(this->tail_.get() + this->tail_sent_, remaining);
|
||||
if (sent <= 0) {
|
||||
return; // write() owns the stall clock and the close
|
||||
}
|
||||
this->no_memory_logged_ = false;
|
||||
this->tail_sent_ += sent;
|
||||
if (this->tail_sent_ < this->tail_len_) {
|
||||
ESP_LOGV(TAG, "Partial send: %zd/%zu bytes (total: %u/%u)", sent, remaining, this->tail_sent_, this->tail_len_);
|
||||
return;
|
||||
}
|
||||
// Fully sent; the storage stays for the next stall
|
||||
this->tail_len_ = 0;
|
||||
}
|
||||
|
||||
bool AsyncEventSourceResponse::reserve_tail_(size_t len) {
|
||||
if (this->tail_cap_ >= len) {
|
||||
return true;
|
||||
}
|
||||
if (len > TAIL_MAX_SIZE) {
|
||||
return false;
|
||||
}
|
||||
// Nothing is pending while the tail grows, so free the old block first. PREFER_INTERNAL keeps
|
||||
// the tail where plain new put it.
|
||||
this->tail_.reset();
|
||||
this->tail_ = RAMAllocator<uint8_t>(RAMAllocator<uint8_t>::PREFER_INTERNAL).make_unique_array_for_overwrite(len);
|
||||
this->tail_cap_ = this->tail_ ? len : 0;
|
||||
return this->tail_cap_ != 0;
|
||||
}
|
||||
|
||||
bool AsyncEventSourceResponse::stash_chunk_(const char *prefix, size_t prefix_len, const char *message,
|
||||
size_t message_len, size_t total, size_t sent) {
|
||||
// A log event of nothing but line breaks is the largest chunk; reserve_tail_() must never
|
||||
// refuse it for size, only for memory
|
||||
static_assert(SSE_SEP_LEN * LOG_EVENT_MAX_LEN + PREFIX_BUF_SIZE + SSE_SUFFIX_LEN <= TAIL_MAX_SIZE,
|
||||
"the log cut in web_server.h must keep a worst case log event inside the tail ceiling");
|
||||
if (!this->reserve_tail_(total)) {
|
||||
if (sent != 0) {
|
||||
// Part of the chunk is on the wire, so the stream is broken and the client has to go
|
||||
ESP_LOGW(TAG, "Cannot buffer a %zu byte chunk, closing", total);
|
||||
this->close();
|
||||
} else {
|
||||
this->tail_alloc_failed_(total); // nothing on the wire, the caller retries on the stall clock
|
||||
}
|
||||
return false;
|
||||
}
|
||||
uint8_t *dst = this->tail_.get();
|
||||
std::memcpy(dst, prefix, prefix_len);
|
||||
dst += prefix_len;
|
||||
for_each_chunk_piece(
|
||||
message, message_len,
|
||||
[](void *ctx, const char *piece, size_t len) {
|
||||
auto &out = *static_cast<uint8_t **>(ctx);
|
||||
std::memcpy(out, piece, len);
|
||||
out += len;
|
||||
},
|
||||
&dst);
|
||||
this->tail_len_ = total;
|
||||
this->tail_sent_ = sent;
|
||||
return true;
|
||||
}
|
||||
|
||||
void AsyncEventSourceResponse::loop() {
|
||||
AsyncStreamResponse::loop();
|
||||
if (this->is_closing()) {
|
||||
if (this->tail_cap_ != 0 || this->deferred_queue_.capacity() != 0) {
|
||||
this->release_buffers_();
|
||||
}
|
||||
return;
|
||||
}
|
||||
drain_tail_();
|
||||
process_deferred_queue_();
|
||||
if (this->is_closing())
|
||||
return;
|
||||
// One step per loop; refusals retry next pass
|
||||
this->entities_iterator_.try_advance(1);
|
||||
}
|
||||
|
||||
#if defined(__GNUC__) && !defined(__clang__) && __GNUC__ >= 14 && defined(__OPTIMIZE_SIZE__)
|
||||
#pragma GCC diagnostic push
|
||||
#pragma GCC diagnostic ignored "-Wstack-usage=" // the one frame that holds the JSON arena
|
||||
#endif
|
||||
AsyncEventSourceResponse::JsonPrepareResult AsyncEventSourceResponse::prepare_json_(void *source,
|
||||
message_generator_t *generator,
|
||||
char *buffer, size_t *size_out) {
|
||||
// Log lines emitted while the generator runs are dropped for this session: on the large path they would fill
|
||||
// tail_ before it is overwritten, and on the small path they would go out ahead of the event being built.
|
||||
SendGuard guard{*this};
|
||||
json::JsonArena<json::JSON_ARENA_SIZE> arena;
|
||||
json::JsonBuilder builder(&arena);
|
||||
generator(this->web_server_, source, builder);
|
||||
*size_out = builder.serialize_to(buffer, JSON_BUF_SIZE);
|
||||
if (*size_out < JSON_BUF_SIZE) {
|
||||
return JsonPrepareResult::JSON_PREPARE_RESULT_BUFFER;
|
||||
}
|
||||
|
||||
char prefix[PREFIX_BUF_SIZE];
|
||||
const size_t prefix_len = build_chunk_prefix(prefix, sizeof(prefix), "state", 0, 0, true);
|
||||
|
||||
// Nothing reaches the wire until the complete document and its framing are owned by the caller.
|
||||
size_t json_len = 0;
|
||||
size_t cap = std::max<size_t>(JSON_BUF_SIZE * 2, this->tail_cap_);
|
||||
for (;;) {
|
||||
if (!this->reserve_tail_(cap)) {
|
||||
*size_out = cap;
|
||||
return JsonPrepareResult::JSON_PREPARE_RESULT_NO_MEMORY;
|
||||
}
|
||||
const size_t room = cap - prefix_len - SSE_SUFFIX_LEN;
|
||||
json_len = builder.serialize_to(reinterpret_cast<char *>(this->tail_.get()) + prefix_len, room);
|
||||
if (json_len < room) {
|
||||
break;
|
||||
}
|
||||
if (cap >= TAIL_MAX_SIZE) {
|
||||
*size_out = room;
|
||||
return JsonPrepareResult::JSON_PREPARE_RESULT_TOO_LARGE;
|
||||
}
|
||||
cap = std::min<size_t>(cap * 2, TAIL_MAX_SIZE);
|
||||
}
|
||||
|
||||
const size_t total = prefix_len + json_len + SSE_SUFFIX_LEN;
|
||||
write_chunk_header(prefix, total - CHUNK_HDR_LEN - CHUNK_END_LEN);
|
||||
uint8_t *dst = this->tail_.get();
|
||||
std::memcpy(dst, prefix, prefix_len);
|
||||
std::memcpy(dst + prefix_len + json_len, SSE_SUFFIX, SSE_SUFFIX_LEN);
|
||||
this->tail_len_ = total;
|
||||
this->tail_sent_ = 0;
|
||||
return JsonPrepareResult::JSON_PREPARE_RESULT_TAIL;
|
||||
}
|
||||
#if defined(__GNUC__) && !defined(__clang__) && __GNUC__ >= 14 && defined(__OPTIMIZE_SIZE__)
|
||||
#pragma GCC diagnostic pop
|
||||
#endif
|
||||
|
||||
bool AsyncEventSourceResponse::send_json_(void *source, message_generator_t *generator) {
|
||||
if (!this->ready_to_send_()) {
|
||||
return false;
|
||||
}
|
||||
char buffer[JSON_BUF_SIZE];
|
||||
size_t size;
|
||||
const auto result = this->prepare_json_(source, generator, buffer, &size);
|
||||
switch (result) {
|
||||
case JsonPrepareResult::JSON_PREPARE_RESULT_BUFFER:
|
||||
return this->try_send_nodefer(buffer, size, "state");
|
||||
case JsonPrepareResult::JSON_PREPARE_RESULT_TAIL:
|
||||
this->drain_tail_();
|
||||
return true;
|
||||
case JsonPrepareResult::JSON_PREPARE_RESULT_NO_MEMORY: {
|
||||
SendGuard guard{*this};
|
||||
this->tail_alloc_failed_(size);
|
||||
return false;
|
||||
}
|
||||
case JsonPrepareResult::JSON_PREPARE_RESULT_TOO_LARGE: {
|
||||
SendGuard guard{*this};
|
||||
ESP_LOGW(TAG, "State event does not fit %zu bytes, dropped", size);
|
||||
this->tail_.reset();
|
||||
this->tail_cap_ = 0;
|
||||
this->stall_.clear();
|
||||
this->no_memory_logged_ = false;
|
||||
return true;
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
void AsyncEventSourceResponse::tail_alloc_failed_(size_t cap) {
|
||||
// Same stall clock as a socket that stops draining, so a session cannot retry forever. A refused write may
|
||||
// already have started it, so the warning keeps its own flag.
|
||||
if (!this->no_memory_logged_) {
|
||||
this->no_memory_logged_ = true;
|
||||
ESP_LOGW(TAG, "No memory for a %zu byte chunk", cap);
|
||||
}
|
||||
const uint32_t now = App.get_loop_component_start_time();
|
||||
if (!this->stall_.is_running()) {
|
||||
this->stall_.start(now);
|
||||
return;
|
||||
}
|
||||
if (this->stall_.is_expired(now, SEND_STALL_TIMEOUT_MS)) {
|
||||
ESP_LOGW(TAG, "Closing EventSource after %" PRIu32 " ms without memory", this->stall_.elapsed(now));
|
||||
this->close();
|
||||
}
|
||||
}
|
||||
|
||||
bool AsyncEventSourceResponse::ready_to_send_() {
|
||||
if (this->sending_ || !this->is_connected() || this->is_closing()) {
|
||||
return false;
|
||||
}
|
||||
drain_tail_();
|
||||
return !this->is_closing() && this->tail_len_ == 0;
|
||||
}
|
||||
|
||||
bool AsyncEventSourceResponse::try_send_nodefer(const char *message, size_t message_len, const char *event, uint32_t id,
|
||||
uint32_t reconnect) {
|
||||
if (!this->ready_to_send_()) {
|
||||
return false;
|
||||
}
|
||||
SendGuard guard{*this};
|
||||
|
||||
// Everything after the prefix goes out straight from the caller's buffer
|
||||
char prefix[PREFIX_BUF_SIZE];
|
||||
const size_t prefix_len = build_chunk_prefix(prefix, sizeof(prefix), event, id, reconnect, message != nullptr);
|
||||
if (message == nullptr && prefix_len == CHUNK_HDR_LEN) {
|
||||
return true; // Match ESPAsyncWebServer: nothing to send
|
||||
}
|
||||
if (prefix_len >= PREFIX_BUF_SIZE - 1) {
|
||||
// The appenders truncate silently, which would put a malformed event on the wire
|
||||
ESP_LOGW(TAG, "Event name too long, dropped");
|
||||
return true;
|
||||
}
|
||||
|
||||
// Gather list: the prefix, then the data lines and their separators from the message
|
||||
struct Gather {
|
||||
web_server_base::StreamIovec iov[MAX_SEND_IOV]; // left uninitialized on purpose
|
||||
int iovcnt{1};
|
||||
size_t total{0};
|
||||
bool fits{true};
|
||||
} g;
|
||||
g.total = prefix_len;
|
||||
for_each_chunk_piece(
|
||||
message, message_len,
|
||||
[](void *ctx, const char *piece, size_t len) {
|
||||
auto &g = *static_cast<Gather *>(ctx);
|
||||
g.total += len;
|
||||
if (len == 0) {
|
||||
return;
|
||||
}
|
||||
if (g.iovcnt == MAX_SEND_IOV) {
|
||||
g.fits = false;
|
||||
return;
|
||||
}
|
||||
g.iov[g.iovcnt++] = {const_cast<char *>(piece), len};
|
||||
},
|
||||
&g);
|
||||
// The header and the terminator are not part of the chunk length
|
||||
write_chunk_header(prefix, g.total - CHUNK_HDR_LEN - CHUNK_END_LEN);
|
||||
g.iov[0] = {prefix, prefix_len};
|
||||
|
||||
// A message with more lines than the list holds skips straight to the tail
|
||||
const ssize_t sent = g.fits ? this->write(g.iov, g.iovcnt) : 0;
|
||||
if (sent < 0) {
|
||||
return false;
|
||||
}
|
||||
if (sent > 0) {
|
||||
this->no_memory_logged_ = false;
|
||||
}
|
||||
if (static_cast<size_t>(sent) == g.total) {
|
||||
return true;
|
||||
}
|
||||
// The caller's buffers do not outlive this call, so keep the chunk and continue from loop()
|
||||
return this->stash_chunk_(prefix, prefix_len, message, message_len, g.total, sent);
|
||||
}
|
||||
|
||||
void AsyncEventSourceResponse::deferrable_send_state(void *source, bool detail_all,
|
||||
message_generator_t *message_generator) {
|
||||
// Let the detail_all listing finish before bare state events, so the web UI never shows unnamed entries and
|
||||
// initial connect sends less
|
||||
if (!detail_all && !this->entities_iterator_.completed())
|
||||
return;
|
||||
|
||||
drain_tail_();
|
||||
process_deferred_queue_();
|
||||
|
||||
if (this->is_closing()) {
|
||||
return;
|
||||
}
|
||||
|
||||
if (this->tail_len_ != 0 || !deferred_queue_.empty()) {
|
||||
// outgoing event buffer or deferred queue still not empty which means downstream tcp send buffer full, no point
|
||||
// trying to send first
|
||||
deq_push_back_with_dedup_(source, message_generator);
|
||||
} else {
|
||||
// A send error closes the session and clears the queue; nothing is queued after that
|
||||
if (!this->send_json_(source, message_generator) && !this->is_closing()) {
|
||||
deq_push_back_with_dedup_(source, message_generator);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
} // namespace esphome::web_server
|
||||
|
||||
#endif // USE_ESP32 && USE_WEBSERVER
|
||||
@@ -0,0 +1,176 @@
|
||||
#pragma once
|
||||
|
||||
#include "esphome/core/defines.h"
|
||||
#if defined(USE_ESP32) && defined(USE_WEBSERVER)
|
||||
|
||||
#include "esphome/components/json/json_util.h"
|
||||
#include "esphome/components/web_server_base/stream_response.h"
|
||||
#include "esphome/core/helpers.h"
|
||||
#include "esphome/core/string_ref.h"
|
||||
#include "list_entities.h"
|
||||
#include "sse_chunk.h"
|
||||
|
||||
#include <atomic>
|
||||
#include <cstdint>
|
||||
#include <vector>
|
||||
|
||||
namespace esphome::web_server {
|
||||
|
||||
class WebServer;
|
||||
class AsyncEventSource;
|
||||
class AsyncEventSourceResponse;
|
||||
|
||||
using message_generator_t = void(WebServer *, void *, json::JsonBuilder &);
|
||||
|
||||
/*
|
||||
This class holds a pointer to the source component that wants to publish a state event, and a pointer to a function
|
||||
that will lazily generate that event. The two pointers allow dedup in the deferred queue if multiple publishes for
|
||||
the same component are backed up, and take up only two pointers of memory. The entry in the deferred queue (a
|
||||
std::vector) is the DeferredEvent instance itself (not a pointer to one elsewhere in heap) so still only two pointers
|
||||
per entry (and no heap fragmentation). Even 100 backed up events (you'd have to have at least 100 sensors publishing
|
||||
because of dedup) would take up only 0.8 kB.
|
||||
*/
|
||||
struct DeferredEvent {
|
||||
friend class AsyncEventSourceResponse;
|
||||
|
||||
protected:
|
||||
void *source_;
|
||||
message_generator_t *message_generator_;
|
||||
|
||||
public:
|
||||
DeferredEvent(void *source, message_generator_t *message_generator)
|
||||
: source_(source), message_generator_(message_generator) {}
|
||||
bool operator==(const DeferredEvent &test) const {
|
||||
return (source_ == test.source_ && message_generator_ == test.message_generator_);
|
||||
}
|
||||
};
|
||||
static_assert(sizeof(DeferredEvent) == sizeof(void *) + sizeof(message_generator_t *),
|
||||
"DeferredEvent should have no padding");
|
||||
|
||||
class AsyncEventSourceResponse : public web_server_base::AsyncStreamResponse {
|
||||
friend class AsyncEventSource;
|
||||
|
||||
public:
|
||||
bool try_send_nodefer(const char *message, size_t message_len, const char *event = nullptr, uint32_t id = 0,
|
||||
uint32_t reconnect = 0);
|
||||
// detail_all events go out while entities are still being listed; state events wait until listing is done
|
||||
void deferrable_send_state(void *source, bool detail_all, message_generator_t *message_generator);
|
||||
// The old event name argument would otherwise convert to true
|
||||
void deferrable_send_state(void *source, const char *event_type, message_generator_t *message_generator) = delete;
|
||||
void loop();
|
||||
|
||||
protected:
|
||||
AsyncEventSourceResponse(AsyncWebServerRequest *request, AsyncEventSource *server, WebServer *ws);
|
||||
|
||||
// Main-loop only: sends initial ping/config/sorting_groups, starts entity iterator.
|
||||
void start_session_main_loop_();
|
||||
|
||||
void deq_push_back_with_dedup_(void *source, message_generator_t *message_generator);
|
||||
void process_deferred_queue_();
|
||||
// A new chunk may go out: not re-entered from a log line, session alive, tail empty
|
||||
bool ready_to_send_();
|
||||
// Push what is left of the chunk in tail_ to the socket
|
||||
void drain_tail_();
|
||||
// Grow tail_ to hold len bytes, kept at its high-water mark. False on OOM.
|
||||
bool reserve_tail_(size_t len);
|
||||
// Keep the whole chunk in tail_ and continue from sent; false when the tail cannot be allocated.
|
||||
bool stash_chunk_(const char *prefix, size_t prefix_len, const char *message, size_t message_len, size_t total,
|
||||
size_t sent);
|
||||
// Send a state event; JSON too large for the stack buffer is serialized into tail_ instead
|
||||
bool send_json_(void *source, message_generator_t *generator);
|
||||
enum class JsonPrepareResult : uint8_t {
|
||||
JSON_PREPARE_RESULT_BUFFER,
|
||||
JSON_PREPARE_RESULT_TAIL,
|
||||
JSON_PREPARE_RESULT_NO_MEMORY,
|
||||
JSON_PREPARE_RESULT_TOO_LARGE,
|
||||
};
|
||||
// Keep the arena out of the caller's frame so it is gone before the state send.
|
||||
// size_out: BUFFER = JSON length, NO_MEMORY = requested tail capacity, TOO_LARGE = available JSON room;
|
||||
// unused for TAIL.
|
||||
JsonPrepareResult __attribute__((noinline)) prepare_json_(void *source, message_generator_t *generator, char *buffer,
|
||||
size_t *size_out);
|
||||
// Warn once per stall, and close the session once the stall timeout passes with no memory for the tail
|
||||
void tail_alloc_failed_(size_t cap);
|
||||
|
||||
// A log line emitted inside a send re-enters try_send_nodefer on this session; refuse it
|
||||
struct SendGuard {
|
||||
AsyncEventSourceResponse &owner;
|
||||
explicit SendGuard(AsyncEventSourceResponse &owner) : owner(owner) { owner.sending_ = true; }
|
||||
~SendGuard() { this->owner.sending_ = false; }
|
||||
};
|
||||
// Frees the tail and the queue's storage once the session is closing
|
||||
void release_buffers_();
|
||||
|
||||
AsyncEventSource *server_;
|
||||
std::vector<DeferredEvent> deferred_queue_;
|
||||
WebServer *web_server_;
|
||||
ListEntitiesIterator entities_iterator_;
|
||||
// One chunk the socket did not take whole, allocated on the first stall; the only heap use
|
||||
// on the send path
|
||||
RAMUniquePtr<uint8_t[]> tail_;
|
||||
uint16_t tail_cap_{0};
|
||||
uint16_t tail_len_{0}; // Zero means nothing pending
|
||||
uint16_t tail_sent_{0};
|
||||
bool sending_{false};
|
||||
// The no-memory warning went out during the current stall; cleared on send progress
|
||||
bool no_memory_logged_{false};
|
||||
// The longest multi line log message in the tree (a climate dump_config) has 22 lines; a
|
||||
// longer one goes through the tail
|
||||
static constexpr size_t MAX_SEND_LINES = 22;
|
||||
static constexpr size_t MAX_SEND_IOV = 1 + 2 * MAX_SEND_LINES;
|
||||
// Chunk header, retry/id/event lines and the first "data: "
|
||||
static constexpr size_t PREFIX_BUF_SIZE = 128;
|
||||
|
||||
// Stack buffer for a state event's JSON; a larger document is serialized into the tail
|
||||
static constexpr size_t JSON_BUF_SIZE = 1024;
|
||||
// Same ceiling JsonBuilder::serialize() applies (max_heap_size in json_util.cpp); a larger
|
||||
// document is dropped before anything is on the wire
|
||||
static constexpr size_t JSON_MAX_SIZE = 5120;
|
||||
// Most RAM a stalled session keeps: the largest state document plus any accepted framing
|
||||
static constexpr size_t TAIL_MAX_SIZE = JSON_MAX_SIZE + PREFIX_BUF_SIZE + SSE_SUFFIX_LEN;
|
||||
};
|
||||
|
||||
class AsyncEventSource : public AsyncWebHandler {
|
||||
friend class AsyncEventSourceResponse;
|
||||
|
||||
public:
|
||||
AsyncEventSource(StringRef url, WebServer *ws) : url_(url), web_server_(ws) {}
|
||||
~AsyncEventSource() override;
|
||||
|
||||
// NOLINTNEXTLINE(readability-identifier-naming)
|
||||
bool canHandle(AsyncWebServerRequest *request) const override {
|
||||
if (request->method() != HTTP_GET)
|
||||
return false;
|
||||
char url_buf[AsyncWebServerRequest::URL_BUF_SIZE];
|
||||
return request->url_to(url_buf) == this->url_;
|
||||
}
|
||||
// NOLINTNEXTLINE(readability-identifier-naming)
|
||||
void handleRequest(AsyncWebServerRequest *request) override;
|
||||
void try_send_nodefer(const char *message, size_t message_len, const char *event = nullptr, uint32_t id = 0,
|
||||
uint32_t reconnect = 0);
|
||||
void deferrable_send_state(void *source, bool detail_all, message_generator_t *message_generator);
|
||||
// The old event name argument would otherwise convert to true
|
||||
void deferrable_send_state(void *source, const char *event_type, message_generator_t *message_generator) = delete;
|
||||
/// Returns true if there are sessions remaining (including pending cleanup).
|
||||
bool loop();
|
||||
bool empty() { return this->count() == 0; }
|
||||
|
||||
size_t count() const { return this->sessions_.size(); }
|
||||
|
||||
protected:
|
||||
// Cold path: move sessions from pending_sessions_ into sessions_ and greet each one.
|
||||
void __attribute__((noinline, cold)) adopt_pending_sessions_main_loop_();
|
||||
|
||||
StringRef url_; // Must outlive this object (string literal)
|
||||
// Main-loop only. Vector: SSE sessions are 1-5 connections, linear search beats set.
|
||||
std::vector<AsyncEventSourceResponse *> sessions_;
|
||||
// Httpd-task intake; guarded by pending_mutex_, gated by has_pending_sessions_.
|
||||
std::vector<AsyncEventSourceResponse *> pending_sessions_;
|
||||
Mutex pending_mutex_;
|
||||
WebServer *web_server_;
|
||||
std::atomic<bool> has_pending_sessions_{false};
|
||||
};
|
||||
|
||||
} // namespace esphome::web_server
|
||||
|
||||
#endif // USE_ESP32 && USE_WEBSERVER
|
||||
@@ -8,14 +8,8 @@
|
||||
|
||||
namespace esphome::web_server {
|
||||
|
||||
#ifdef USE_ESP32
|
||||
ListEntitiesIterator::ListEntitiesIterator(const WebServer *ws, AsyncEventSource *es) : web_server_(ws), events_(es) {}
|
||||
#elif USE_ARDUINO
|
||||
ListEntitiesIterator::ListEntitiesIterator(const WebServer *ws, DeferredUpdateEventSource *es)
|
||||
: web_server_(ws), events_(es) {}
|
||||
#endif
|
||||
void ListEntitiesIterator::send_all_(void *source, message_generator_t *generator) {
|
||||
this->events_->deferrable_send_state(source, true, generator);
|
||||
this->session_->deferrable_send_state(source, true, generator);
|
||||
}
|
||||
|
||||
#ifdef USE_BINARY_SENSOR
|
||||
|
||||
@@ -1,33 +1,28 @@
|
||||
#pragma once
|
||||
|
||||
#include "esphome/core/defines.h"
|
||||
#ifdef USE_WEBSERVER
|
||||
#if defined(USE_WEBSERVER) && (defined(USE_ESP32) || defined(USE_ARDUINO))
|
||||
#include "esphome/core/component.h"
|
||||
#include "esphome/core/component_iterator.h"
|
||||
namespace esphome::json {
|
||||
class JsonBuilder;
|
||||
} // namespace esphome::json
|
||||
|
||||
namespace esphome::web_server_idf {
|
||||
#ifdef USE_ESP32
|
||||
class AsyncEventSource;
|
||||
#endif
|
||||
} // namespace esphome::web_server_idf
|
||||
|
||||
namespace esphome::web_server {
|
||||
|
||||
#if !defined(USE_ESP32) && defined(USE_ARDUINO)
|
||||
// The /events session the iterator lists entities to
|
||||
#ifdef USE_ESP32
|
||||
class AsyncEventSourceResponse;
|
||||
using EventSession = AsyncEventSourceResponse;
|
||||
#elif defined(USE_ARDUINO)
|
||||
class DeferredUpdateEventSource;
|
||||
using EventSession = DeferredUpdateEventSource;
|
||||
#endif
|
||||
class WebServer;
|
||||
|
||||
class ListEntitiesIterator final : public ComponentIterator {
|
||||
public:
|
||||
#ifdef USE_ESP32
|
||||
ListEntitiesIterator(const WebServer *ws, esphome::web_server_idf::AsyncEventSource *es);
|
||||
#elif defined(USE_ARDUINO)
|
||||
ListEntitiesIterator(const WebServer *ws, DeferredUpdateEventSource *es);
|
||||
#endif
|
||||
ListEntitiesIterator(const WebServer *ws, EventSession *session) : web_server_(ws), session_(session) {}
|
||||
|
||||
// Entity overrides (generated from entity_types.h).
|
||||
// Implementations live in list_entities.cpp.
|
||||
@@ -45,11 +40,7 @@ class ListEntitiesIterator final : public ComponentIterator {
|
||||
void send_all_(void *source, void (*generator)(WebServer *, void *, json::JsonBuilder &));
|
||||
|
||||
const WebServer *web_server_;
|
||||
#ifdef USE_ESP32
|
||||
esphome::web_server_idf::AsyncEventSource *events_;
|
||||
#elif USE_ARDUINO
|
||||
DeferredUpdateEventSource *events_;
|
||||
#endif
|
||||
EventSession *session_;
|
||||
};
|
||||
|
||||
} // namespace esphome::web_server
|
||||
|
||||
+2
-2
@@ -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
|
||||
+15
-2
@@ -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
|
||||
@@ -273,7 +273,7 @@ void DeferredUpdateEventSource::process_deferred_queue_() {
|
||||
deferred_queue_.erase(deferred_queue_.begin());
|
||||
this->consecutive_send_failures_ = 0; // Reset failure count on successful send
|
||||
} else {
|
||||
// NOTE: Similar logic exists in web_server_idf/web_server_idf.cpp in AsyncEventSourceResponse::drain_tail_().
|
||||
// NOTE: Similar logic exists in web_server_idf/web_server_idf.cpp in AsyncStreamResponse::write().
|
||||
// The close mechanisms are platform-specific (this path calls close() directly; the IDF path is time-based and
|
||||
// closes through HTTPD to preserve session ownership), but both drop a client after roughly 20 seconds without
|
||||
// send progress. Keep that stall policy in sync when changing either side.
|
||||
|
||||
@@ -5,7 +5,11 @@
|
||||
#include "esphome/components/json/json_util.h"
|
||||
#include "esphome/components/web_server_base/web_server_base.h"
|
||||
#include "esphome/components/web_server_base/captive_dns.h"
|
||||
#ifdef USE_WEBSERVER
|
||||
// The host only builds sse_chunk.cpp for unit tests, but other tests can define USE_WEBSERVER there
|
||||
#if defined(USE_WEBSERVER) && (defined(USE_ESP32) || defined(USE_ARDUINO))
|
||||
#ifdef USE_ESP32
|
||||
#include "event_source.h"
|
||||
#endif
|
||||
#include "esphome/core/component.h"
|
||||
#include "esphome/core/entity_base.h"
|
||||
#include "esphome/core/entity_includes.h"
|
||||
|
||||
@@ -105,9 +105,9 @@ async def to_code(config: ConfigType) -> None:
|
||||
var = cg.new_Pvariable(config[CONF_ID])
|
||||
cg.add(cg.RawExpression(f"{web_server_base_ns}::global_web_server_base = {var}"))
|
||||
|
||||
# Default headers added in init(): web_server_idf's StaticVector, or the library's DefaultHeaders
|
||||
cg.add_define("WEB_SERVER_DEFAULT_HEADERS_COUNT", 1)
|
||||
if CORE.is_esp32:
|
||||
# Count for StaticVector in web_server_idf - matches headers added in init()
|
||||
cg.add_define("WEB_SERVER_DEFAULT_HEADERS_COUNT", 1)
|
||||
return
|
||||
|
||||
# ESP32 uses IDF web server (early return above), so this is for other Arduino platforms
|
||||
|
||||
@@ -0,0 +1,126 @@
|
||||
#pragma once
|
||||
|
||||
#include <cstddef>
|
||||
#include <cstdint>
|
||||
#include <cstring>
|
||||
#include <span>
|
||||
|
||||
#include <strings.h>
|
||||
|
||||
// Pure helpers of AsyncStreamResponse, kept free of httpd and lwIP so they build on the host.
|
||||
// The iovec type is a template parameter: lwIP defines its own on the device, POSIX on the host.
|
||||
|
||||
namespace esphome::web_server_base {
|
||||
|
||||
struct HttpHeader {
|
||||
const char *name;
|
||||
const char *value;
|
||||
};
|
||||
|
||||
/// Drops the first sent bytes from a gather list after a write; a short write resumes inside the
|
||||
/// element it stopped in. count reaches 0 once everything went out.
|
||||
template<typename Iovec> void advance_iovecs(Iovec *&iov, int &count, size_t sent) {
|
||||
while (count > 0 && sent >= iov->iov_len) {
|
||||
sent -= iov->iov_len;
|
||||
++iov;
|
||||
--count;
|
||||
}
|
||||
if (count > 0) {
|
||||
iov->iov_base = static_cast<char *>(iov->iov_base) + sent;
|
||||
iov->iov_len -= sent;
|
||||
}
|
||||
}
|
||||
|
||||
/// Pieces in a head: 4 for the status line and Content-Type, 4 per header, the blank line and the body
|
||||
constexpr size_t stream_head_iov_count(size_t header_count) { return 4 + 4 * header_count + 2; }
|
||||
|
||||
/// Pieces per batch: a head with up to four headers goes out in one write; 192 bytes on 32-bit
|
||||
static constexpr int STREAM_HEAD_MAX_IOV = 24;
|
||||
|
||||
/// True if headers holds a header named name, compared case insensitively
|
||||
inline bool has_header(std::span<const HttpHeader> headers, const char *name) {
|
||||
for (const auto &header : headers) {
|
||||
if (strcasecmp(header.name, name) == 0) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
/// Gathers the head of a stream response: "HTTP/1.1 <status>", Content-Type, headers, the default_headers that
|
||||
/// headers does not replace, the blank line and body (may be null). send(Iovec *iov, int count) gets each full batch of
|
||||
/// STREAM_HEAD_MAX_IOV pieces and then the rest; once it returns false the later pieces are skipped. Returns false if
|
||||
/// any send failed.
|
||||
template<typename Iovec, typename Send>
|
||||
bool write_stream_head(Send &&send, const char *status, const char *content_type, std::span<const HttpHeader> headers,
|
||||
std::span<const HttpHeader> default_headers, const char *body) {
|
||||
struct Batch {
|
||||
Send &send;
|
||||
int count{0};
|
||||
bool ok{true};
|
||||
Iovec iov[STREAM_HEAD_MAX_IOV]; // left uninitialized on purpose
|
||||
|
||||
void flush() {
|
||||
if (this->ok && this->count > 0) {
|
||||
this->ok = this->send(this->iov, this->count);
|
||||
}
|
||||
this->count = 0;
|
||||
}
|
||||
void add(const char *buf, size_t len) {
|
||||
if (this->count == STREAM_HEAD_MAX_IOV) {
|
||||
this->flush();
|
||||
}
|
||||
this->iov[this->count++] = {const_cast<char *>(buf), len};
|
||||
}
|
||||
void add(const char *str) { this->add(str, strlen(str)); }
|
||||
void add_header(const HttpHeader &header) {
|
||||
this->add("\r\n", 2);
|
||||
this->add(header.name);
|
||||
this->add(": ", 2);
|
||||
this->add(header.value);
|
||||
}
|
||||
} batch{send};
|
||||
|
||||
batch.add("HTTP/1.1 ", 9);
|
||||
batch.add(status);
|
||||
batch.add("\r\nContent-Type: ", 16);
|
||||
batch.add(content_type);
|
||||
for (const auto &header : headers) {
|
||||
batch.add_header(header);
|
||||
}
|
||||
for (const auto &header : default_headers) {
|
||||
if (!has_header(headers, header.name)) {
|
||||
batch.add_header(header);
|
||||
}
|
||||
}
|
||||
batch.add("\r\n\r\n", 4);
|
||||
if (body != nullptr) {
|
||||
batch.add(body);
|
||||
}
|
||||
batch.flush();
|
||||
return batch.ok;
|
||||
}
|
||||
|
||||
/// How long sends have made no progress. Zero means not running, so a start at millis() == 0 records 1.
|
||||
/// The comparison is wraparound safe across a millis() rollover.
|
||||
class StallClock {
|
||||
public:
|
||||
/// Starts the clock; a running clock keeps its start time.
|
||||
void start(uint32_t now) {
|
||||
if (this->started_ms_ == 0) {
|
||||
this->started_ms_ = now != 0 ? now : 1;
|
||||
}
|
||||
}
|
||||
void clear() { this->started_ms_ = 0; }
|
||||
bool is_running() const { return this->started_ms_ != 0; }
|
||||
/// True once the clock has run for timeout_ms
|
||||
bool is_expired(uint32_t now, uint32_t timeout_ms) const {
|
||||
return this->started_ms_ != 0 && static_cast<int32_t>(now - (this->started_ms_ + timeout_ms)) >= 0;
|
||||
}
|
||||
uint32_t elapsed(uint32_t now) const { return now - this->started_ms_; }
|
||||
|
||||
protected:
|
||||
uint32_t started_ms_{0};
|
||||
};
|
||||
|
||||
} // namespace esphome::web_server_base
|
||||
@@ -0,0 +1,16 @@
|
||||
#pragma once
|
||||
|
||||
#include "esphome/core/defines.h"
|
||||
|
||||
// The long lived stream response the /events layer writes to, for the platform's web server
|
||||
#ifdef USE_ESP32
|
||||
#include "esphome/components/web_server_idf/web_server_idf.h"
|
||||
|
||||
#include <sys/socket.h>
|
||||
|
||||
namespace esphome::web_server_base {
|
||||
using web_server_idf::AsyncStreamResponse;
|
||||
// One entry of the gather list AsyncStreamResponse::write() takes
|
||||
using StreamIovec = ::iovec;
|
||||
} // namespace esphome::web_server_base
|
||||
#endif
|
||||
@@ -17,7 +17,6 @@
|
||||
#include <freertos/task.h>
|
||||
|
||||
#include "utils.h"
|
||||
#include "sse_chunk.h"
|
||||
#include "web_server_idf.h"
|
||||
|
||||
#ifdef USE_WEBSERVER_AUTH_DIGEST
|
||||
@@ -30,17 +29,15 @@
|
||||
#include "multipart.h" // For parse_multipart_boundary and other utils
|
||||
#endif
|
||||
|
||||
#ifdef USE_WEBSERVER
|
||||
#include "esphome/components/web_server/web_server.h"
|
||||
#include "esphome/components/web_server/list_entities.h"
|
||||
#endif // USE_WEBSERVER
|
||||
|
||||
// Include socket headers after Arduino headers to avoid IPADDR_NONE/INADDR_NONE macro conflicts
|
||||
#include <cerrno>
|
||||
#include <sys/socket.h>
|
||||
|
||||
namespace esphome::web_server_idf {
|
||||
|
||||
using web_server_base::advance_iovecs;
|
||||
using web_server_base::write_stream_head;
|
||||
|
||||
// Status strings not provided by esp_http_server.h
|
||||
#ifndef HTTPD_401
|
||||
#define HTTPD_401 "401 Unauthorized"
|
||||
@@ -55,13 +52,10 @@ namespace esphome::web_server_idf {
|
||||
#define HTTPD_422 "422 Unprocessable Entity"
|
||||
#endif
|
||||
|
||||
#define CRLF_STR "\r\n"
|
||||
#define CRLF_LEN (sizeof(CRLF_STR) - 1)
|
||||
|
||||
ESPHOME_LOG_TAG(TAG, "web_server_idf");
|
||||
|
||||
// Only prepare_json_() may hold the JSON arena: every other frame in this file is capped below one
|
||||
// arena. Measured at -Os on GCC 14; newer toolchains stay checked on purpose, older ones skip it.
|
||||
// Every frame in this file is capped below one JSON arena.
|
||||
// Measured at -Os on GCC 14; newer toolchains stay checked on purpose, older ones skip it.
|
||||
#if defined(__GNUC__) && !defined(__clang__) && __GNUC__ >= 14 && defined(__OPTIMIZE_SIZE__)
|
||||
#pragma GCC diagnostic error "-Wstack-usage=2048"
|
||||
#endif
|
||||
@@ -140,6 +134,8 @@ void AsyncWebServer::safe_close_with_shutdown(httpd_handle_t hd, int sockfd) {
|
||||
//
|
||||
// See: https://github.com/esphome/esphome-webserver/issues/163
|
||||
|
||||
AsyncStreamResponse::socket_closing(hd, sockfd);
|
||||
|
||||
// Attempt shutdown - ignore errors as socket may already be closed
|
||||
shutdown(sockfd, SHUT_RD);
|
||||
|
||||
@@ -736,224 +732,84 @@ void AsyncResponseStream::printf(const char *fmt, ...) {
|
||||
this->print(str);
|
||||
}
|
||||
|
||||
#ifdef USE_WEBSERVER
|
||||
AsyncEventSource::~AsyncEventSource() {
|
||||
LockGuard guard{this->pending_mutex_};
|
||||
for (auto *vec : {&this->sessions_, &this->pending_sessions_}) {
|
||||
for (auto *ses : *vec) {
|
||||
delete ses; // NOLINT(cppcoreguidelines-owning-memory)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void AsyncEventSource::handleRequest(AsyncWebServerRequest *request) {
|
||||
// Httpd task: set up the live httpd_req_t and park the session; main loop does the rest.
|
||||
// NOLINTNEXTLINE(cppcoreguidelines-owning-memory,clang-analyzer-cplusplus.NewDeleteLeaks)
|
||||
auto *rsp = new AsyncEventSourceResponse(request, this, this->web_server_);
|
||||
{
|
||||
LockGuard guard{this->pending_mutex_};
|
||||
this->pending_sessions_.push_back(rsp);
|
||||
this->has_pending_sessions_.store(true, std::memory_order_release);
|
||||
}
|
||||
this->web_server_->enable_loop_soon_any_context();
|
||||
}
|
||||
|
||||
// clang-analyzer traces a false-positive leak path from loop() through
|
||||
// adopt_pending_sessions_main_loop_() into start_session_main_loop_() and
|
||||
// finally ArduinoJson. Suppress along the entire in-our-code call chain.
|
||||
// NOLINTBEGIN(clang-analyzer-cplusplus.NewDeleteLeaks)
|
||||
bool AsyncEventSource::loop() {
|
||||
// Fast path: one atomic load per tick. Slow path is out-of-line on connect.
|
||||
if (this->has_pending_sessions_.load(std::memory_order_acquire)) {
|
||||
this->adopt_pending_sessions_main_loop_();
|
||||
}
|
||||
|
||||
// Clean up dead sessions safely
|
||||
// This follows the ESP-IDF pattern where free_ctx marks resources as dead
|
||||
// and the main loop handles the actual cleanup to avoid race conditions
|
||||
for (size_t i = 0; i < this->sessions_.size();) {
|
||||
auto *ses = this->sessions_[i];
|
||||
// If the session has a dead socket (marked by destroy callback)
|
||||
if (ses->safe_to_delete_()) {
|
||||
// destroy() already logged the close with the fd; don't double-log here.
|
||||
delete ses; // NOLINT(cppcoreguidelines-owning-memory)
|
||||
// Remove by swapping with last element (O(1) removal, order doesn't matter for sessions)
|
||||
this->sessions_[i] = this->sessions_.back();
|
||||
this->sessions_.pop_back();
|
||||
} else {
|
||||
ses->loop();
|
||||
++i;
|
||||
}
|
||||
}
|
||||
// A session still waiting for httpd to commit its context keeps the loop alive too
|
||||
return !this->sessions_.empty() || this->has_pending_sessions_.load(std::memory_order_acquire);
|
||||
}
|
||||
|
||||
void AsyncEventSource::adopt_pending_sessions_main_loop_() {
|
||||
std::vector<AsyncEventSourceResponse *> incoming;
|
||||
{
|
||||
LockGuard guard{this->pending_mutex_};
|
||||
incoming.swap(this->pending_sessions_);
|
||||
this->has_pending_sessions_.store(false, std::memory_order_relaxed);
|
||||
}
|
||||
for (auto *rsp : incoming) {
|
||||
// Already disconnected? Drop it; skip session start on a dead session.
|
||||
if (rsp->safe_to_delete_()) {
|
||||
delete rsp; // NOLINT(cppcoreguidelines-owning-memory)
|
||||
continue;
|
||||
}
|
||||
// httpd commits the session context only after the creating handler returns, so stay
|
||||
// pending until then; httpd_req_cleanup() always commits it or calls destroy()
|
||||
if (httpd_sess_get_ctx(rsp->hd_, rsp->fd_.load()) != rsp) {
|
||||
LockGuard guard{this->pending_mutex_};
|
||||
this->pending_sessions_.push_back(rsp);
|
||||
this->has_pending_sessions_.store(true, std::memory_order_release);
|
||||
continue;
|
||||
}
|
||||
this->sessions_.push_back(rsp);
|
||||
rsp->start_session_main_loop_();
|
||||
}
|
||||
}
|
||||
// NOLINTEND(clang-analyzer-cplusplus.NewDeleteLeaks)
|
||||
|
||||
void AsyncEventSource::try_send_nodefer(const char *message, size_t message_len, const char *event, uint32_t id,
|
||||
uint32_t reconnect) {
|
||||
for (auto *ses : this->sessions_) {
|
||||
if (ses->fd_.load() != 0) { // Skip dead sessions
|
||||
ses->try_send_nodefer(message, message_len, event, id, reconnect);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void AsyncEventSource::deferrable_send_state(void *source, bool detail_all, message_generator_t *message_generator) {
|
||||
// Skip if no connected clients to avoid unnecessary processing
|
||||
if (this->empty())
|
||||
return;
|
||||
for (auto *ses : this->sessions_) {
|
||||
if (ses->fd_.load() != 0) { // Skip dead sessions
|
||||
ses->deferrable_send_state(source, detail_all, message_generator);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
AsyncEventSourceResponse::AsyncEventSourceResponse(const AsyncWebServerRequest *request,
|
||||
esphome::web_server_idf::AsyncEventSource *server,
|
||||
esphome::web_server::WebServer *ws)
|
||||
: server_(server), web_server_(ws), entities_iterator_(ws, server) {
|
||||
// Httpd task only. start_session_main_loop_() sends the greeting and starts the iterator.
|
||||
void AsyncStreamResponse::begin(AsyncWebServerRequest *request, const char *status, const char *content_type,
|
||||
std::span<const HttpHeader> headers, const char *body) {
|
||||
httpd_req_t *req = *request;
|
||||
|
||||
httpd_resp_set_status(req, HTTPD_200);
|
||||
httpd_resp_set_type(req, "text/event-stream");
|
||||
httpd_resp_set_hdr(req, "Cache-Control", "no-cache");
|
||||
httpd_resp_set_hdr(req, "Connection", "keep-alive");
|
||||
|
||||
DefaultHeaders::Instance().apply_(req);
|
||||
|
||||
httpd_resp_send_chunk(req, CRLF_STR, CRLF_LEN);
|
||||
// Same head httpd_resp_send_chunk() writes, without its first chunk. Blocking sends: the session's non-blocking
|
||||
// send override is not installed yet.
|
||||
const int fd = httpd_req_to_sockfd(req);
|
||||
const auto &defaults = DefaultHeaders::Instance().headers_;
|
||||
const bool head_sent = write_stream_head<struct iovec>(
|
||||
[fd](struct iovec *iov, int count) {
|
||||
while (count > 0) {
|
||||
struct msghdr msg{};
|
||||
msg.msg_iov = iov;
|
||||
msg.msg_iovlen = count;
|
||||
const ssize_t sent = sendmsg(fd, &msg, 0);
|
||||
if (sent <= 0) {
|
||||
return false;
|
||||
}
|
||||
advance_iovecs(iov, count, sent);
|
||||
}
|
||||
return true;
|
||||
},
|
||||
status, content_type, headers, std::span<const HttpHeader>(defaults.data(), defaults.size()), body);
|
||||
|
||||
req->sess_ctx = this;
|
||||
req->free_ctx = AsyncEventSourceResponse::destroy;
|
||||
req->free_ctx = AsyncStreamResponse::destroy;
|
||||
|
||||
this->hd_ = req->handle;
|
||||
this->fd_.store(httpd_req_to_sockfd(req));
|
||||
this->fd_.store(fd);
|
||||
|
||||
// Use non-blocking send to prevent watchdog timeouts when TCP buffers are full
|
||||
httpd_sess_set_send_override(this->hd_, this->fd_.load(), nonblocking_send);
|
||||
httpd_sess_set_send_override(this->hd_, fd, nonblocking_send);
|
||||
|
||||
if (!head_sent) {
|
||||
// A partly sent head must not be continued by later writes. close() is main loop only, so shut the socket
|
||||
// down here; the first write() then fails and closes the stream, and httpd frees the session as usual.
|
||||
shutdown(fd, SHUT_RDWR);
|
||||
}
|
||||
}
|
||||
|
||||
// NOLINTBEGIN(clang-analyzer-cplusplus.NewDeleteLeaks) false positive with ArduinoJson
|
||||
void AsyncEventSourceResponse::start_session_main_loop_() {
|
||||
auto *ws = this->web_server_;
|
||||
// Held around a stream's ownership check and send, and while its socket is marked closed.
|
||||
// Created at boot, so no allocation can fail once requests arrive.
|
||||
static Mutex stream_send_mutex; // NOLINT(cppcoreguidelines-avoid-non-const-global-variables)
|
||||
|
||||
// The tcp send buffer is empty on connect. A refusal is a closing session or a failed tail
|
||||
// allocation; nothing retries the greeting, so close and let the client reconnect.
|
||||
auto message = ws->get_config_json();
|
||||
if (!this->try_send_nodefer(message.c_str(), message.size(), "ping", millis(), 30000)) {
|
||||
ESP_LOGW(TAG, "Config not sent to fd %d", this->fd_.load());
|
||||
this->request_close_();
|
||||
void AsyncStreamResponse::socket_closing(httpd_handle_t hd, int fd) {
|
||||
// Only streams set a session context
|
||||
auto *rsp = static_cast<AsyncStreamResponse *>(httpd_sess_get_ctx(hd, fd));
|
||||
if (rsp == nullptr)
|
||||
return;
|
||||
}
|
||||
|
||||
#ifdef USE_WEBSERVER_SORTING
|
||||
for (auto &group : ws->sorting_groups_) {
|
||||
json::JsonBuilder builder;
|
||||
JsonObject root = builder.root();
|
||||
root["name"] = json::linked(group.second.name);
|
||||
root["sorting_weight"] = group.second.weight;
|
||||
message = builder.serialize();
|
||||
|
||||
// a (very) large number of these should be able to be queued initially without defer
|
||||
// since the only thing in the send buffer at this point is the initial ping/config.
|
||||
// A refusal means the socket is full or closing; the remaining groups are not sent. The
|
||||
// session stays up on purpose: partial grouping beats a reconnect loop on a slow link.
|
||||
if (!this->try_send_nodefer(message.c_str(), message.size(), "sorting_group")) {
|
||||
ESP_LOGW(TAG, "Sorting groups not sent to fd %d", this->fd_.load());
|
||||
break;
|
||||
}
|
||||
}
|
||||
#endif
|
||||
|
||||
this->entities_iterator_.begin(ws->include_internal_);
|
||||
LockGuard guard{stream_send_mutex};
|
||||
rsp->socket_closed_ = true;
|
||||
}
|
||||
// NOLINTEND(clang-analyzer-cplusplus.NewDeleteLeaks)
|
||||
|
||||
void AsyncEventSourceResponse::destroy(void *ptr) {
|
||||
auto *rsp = static_cast<AsyncEventSourceResponse *>(ptr);
|
||||
void AsyncStreamResponse::destroy(void *ptr) {
|
||||
auto *rsp = static_cast<AsyncStreamResponse *>(ptr);
|
||||
int fd = rsp->fd_.exchange(0); // Atomically get and clear fd
|
||||
ESP_LOGD(TAG, "Event source connection closed (fd: %d)", fd);
|
||||
// Mark as dead - will be cleaned up in the main loop
|
||||
// Note: We don't delete or remove from set here to avoid race conditions
|
||||
ESP_LOGD(TAG, "Stream connection closed (fd: %d)", fd);
|
||||
// Mark as dead - the owner deletes it from the main loop once safe_to_delete().
|
||||
// httpd will call our custom close_fn (safe_close_with_shutdown) which handles
|
||||
// shutdown() before close() to prevent lwIP race conditions
|
||||
}
|
||||
|
||||
// helper for allowing only unique entries in the queue
|
||||
void AsyncEventSourceResponse::deq_push_back_with_dedup_(void *source, message_generator_t *message_generator) {
|
||||
DeferredEvent item(source, message_generator);
|
||||
|
||||
// Use range-based for loop instead of std::find_if to reduce template instantiation overhead and binary size
|
||||
for (auto &event : this->deferred_queue_) {
|
||||
if (event == item) {
|
||||
return; // Already in queue, no need to update since items are equal
|
||||
}
|
||||
}
|
||||
this->deferred_queue_.push_back(item);
|
||||
}
|
||||
|
||||
void AsyncEventSourceResponse::process_deferred_queue_() {
|
||||
if (this->close_requested_) {
|
||||
return;
|
||||
}
|
||||
while (!deferred_queue_.empty()) {
|
||||
DeferredEvent &de = deferred_queue_.front();
|
||||
if (this->send_json_(de.source_, de.message_generator_)) {
|
||||
if (this->close_requested_ || deferred_queue_.empty()) {
|
||||
return;
|
||||
}
|
||||
// O(n) but memory efficiency is more important than speed here which is why std::vector was chosen
|
||||
deferred_queue_.erase(deferred_queue_.begin());
|
||||
} else {
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void AsyncEventSourceResponse::request_close_() {
|
||||
void AsyncStreamResponse::close() {
|
||||
if (!this->close_requested_) {
|
||||
this->close_requested_ = true;
|
||||
this->deferred_queue_.clear();
|
||||
this->tail_.reset();
|
||||
this->tail_cap_ = 0;
|
||||
this->tail_len_ = 0;
|
||||
this->tail_sent_ = 0;
|
||||
this->next_close_attempt_ms_ = App.get_loop_component_start_time();
|
||||
}
|
||||
|
||||
this->process_close_();
|
||||
}
|
||||
|
||||
void AsyncEventSourceResponse::process_close_() {
|
||||
void AsyncStreamResponse::loop() {
|
||||
if (this->close_requested_) {
|
||||
this->process_close_();
|
||||
} else {
|
||||
this->refresh_lru_();
|
||||
}
|
||||
}
|
||||
|
||||
void AsyncStreamResponse::process_close_() {
|
||||
if (!this->close_requested_ || this->close_work_queued_.load(std::memory_order_acquire)) {
|
||||
return;
|
||||
}
|
||||
@@ -971,7 +827,7 @@ void AsyncEventSourceResponse::process_close_() {
|
||||
// httpd_sess_trigger_close() queues only a reusable fd/session slot and can
|
||||
// therefore close a new client if the original peer disconnects meanwhile.
|
||||
this->close_work_queued_.store(true, std::memory_order_release);
|
||||
const esp_err_t err = httpd_queue_work(this->hd_, &AsyncEventSourceResponse::close_session_work, this);
|
||||
const esp_err_t err = httpd_queue_work(this->hd_, &AsyncStreamResponse::close_session_work, this);
|
||||
this->next_close_attempt_ms_ = now + (err == ESP_OK ? CLOSE_CONFIRM_INTERVAL_MS : CLOSE_RETRY_INTERVAL_MS);
|
||||
if (err == ESP_OK) {
|
||||
return;
|
||||
@@ -979,17 +835,17 @@ void AsyncEventSourceResponse::process_close_() {
|
||||
|
||||
this->close_work_queued_.store(false, std::memory_order_release);
|
||||
if (!this->close_retry_warning_logged_) {
|
||||
ESP_LOGW(TAG, "Failed to queue EventSource close (%s); retrying", esp_err_to_name(err));
|
||||
ESP_LOGW(TAG, "Failed to queue stream close (%s); retrying", esp_err_to_name(err));
|
||||
this->close_retry_warning_logged_ = true;
|
||||
}
|
||||
}
|
||||
|
||||
void AsyncEventSourceResponse::refresh_lru_() {
|
||||
// httpd only advances a session's LRU counter when a request arrives, and an event stream receives one
|
||||
void AsyncStreamResponse::refresh_lru_() {
|
||||
// httpd only advances a session's LRU counter when a request arrives, and a stream receives one
|
||||
// request ever, so the purge that frees a socket for a new client would evict the live streams first.
|
||||
// The HTTPD task owns the session table, so the refresh runs there.
|
||||
// A stalled stream is the socket the purge should take, and a zeroed fd has no session left to refresh.
|
||||
if (this->send_failure_started_ms_ != 0 || this->fd_.load() == 0)
|
||||
if (this->stall_.is_running() || this->fd_.load() == 0)
|
||||
return;
|
||||
const uint32_t now = App.get_loop_component_start_time();
|
||||
if (now - this->last_lru_refresh_ms_ < LRU_REFRESH_INTERVAL_MS ||
|
||||
@@ -998,13 +854,13 @@ void AsyncEventSourceResponse::refresh_lru_() {
|
||||
}
|
||||
this->last_lru_refresh_ms_ = now; // a full queue also waits for the next interval
|
||||
this->lru_work_queued_.store(true, std::memory_order_release);
|
||||
if (httpd_queue_work(this->hd_, &AsyncEventSourceResponse::refresh_lru_work, this) != ESP_OK) {
|
||||
if (httpd_queue_work(this->hd_, &AsyncStreamResponse::refresh_lru_work, this) != ESP_OK) {
|
||||
this->lru_work_queued_.store(false, std::memory_order_release);
|
||||
}
|
||||
}
|
||||
|
||||
void AsyncEventSourceResponse::refresh_lru_work(void *arg) {
|
||||
auto *response = static_cast<AsyncEventSourceResponse *>(arg);
|
||||
void AsyncStreamResponse::refresh_lru_work(void *arg) {
|
||||
auto *response = static_cast<AsyncStreamResponse *>(arg);
|
||||
const int fd = response->fd_.load();
|
||||
if (response->owns_session_(fd)) {
|
||||
httpd_sess_update_lru_counter(response->hd_, fd);
|
||||
@@ -1013,8 +869,8 @@ void AsyncEventSourceResponse::refresh_lru_work(void *arg) {
|
||||
response->lru_work_queued_.store(false, std::memory_order_release);
|
||||
}
|
||||
|
||||
void AsyncEventSourceResponse::close_session_work(void *arg) {
|
||||
auto *response = static_cast<AsyncEventSourceResponse *>(arg);
|
||||
void AsyncStreamResponse::close_session_work(void *arg) {
|
||||
auto *response = static_cast<AsyncStreamResponse *>(arg);
|
||||
const int fd = response->fd_.load();
|
||||
if (response->owns_session_(fd)) {
|
||||
// The HTTPD task remains the session owner. Shutting the socket down makes
|
||||
@@ -1026,320 +882,61 @@ void AsyncEventSourceResponse::close_session_work(void *arg) {
|
||||
response->close_work_queued_.store(false, std::memory_order_release);
|
||||
}
|
||||
|
||||
ssize_t AsyncEventSourceResponse::send_(struct iovec *iov, int iovcnt) {
|
||||
// httpd frees a session before closing its socket, so the fd may already be a new client's.
|
||||
ssize_t AsyncStreamResponse::write(struct iovec *iov, int iovcnt) {
|
||||
if (this->close_requested_) {
|
||||
return -1;
|
||||
}
|
||||
// Nothing to send is not a stall
|
||||
size_t total = 0;
|
||||
for (int i = 0; i < iovcnt; i++) {
|
||||
total += iov[i].iov_len;
|
||||
}
|
||||
if (total == 0) {
|
||||
return 0;
|
||||
}
|
||||
// httpd closes the socket before it clears the session, so the fd number may already be a new client's.
|
||||
// Treated as would-block; the stall timer ends a session that never becomes ours again.
|
||||
const int fd = this->fd_.load();
|
||||
if (!this->owns_session_(fd)) {
|
||||
return 0;
|
||||
ssize_t sent = 0;
|
||||
int err = 0;
|
||||
{
|
||||
// Held across the check and the send, so socket_closing() cannot close the socket in between
|
||||
LockGuard guard{stream_send_mutex};
|
||||
if (!this->socket_closed_ && this->owns_session_(fd)) {
|
||||
struct msghdr msg{};
|
||||
msg.msg_iov = iov;
|
||||
msg.msg_iovlen = iovcnt;
|
||||
sent = sendmsg(fd, &msg, MSG_DONTWAIT);
|
||||
err = errno;
|
||||
}
|
||||
}
|
||||
struct msghdr msg{};
|
||||
msg.msg_iov = iov;
|
||||
msg.msg_iovlen = iovcnt;
|
||||
const ssize_t sent = sendmsg(fd, &msg, MSG_DONTWAIT);
|
||||
if (sent >= 0) {
|
||||
if (sent > 0) {
|
||||
this->stall_.clear();
|
||||
return sent;
|
||||
}
|
||||
const int err = errno;
|
||||
if (err == EAGAIN || err == EWOULDBLOCK) {
|
||||
return 0;
|
||||
if (sent < 0 && err != EAGAIN && err != EWOULDBLOCK) {
|
||||
ESP_LOGD(TAG, "send error: errno %d", err);
|
||||
// A hard error is not a timeout, so is_stalled() must not report one
|
||||
this->stall_.clear();
|
||||
this->close();
|
||||
return -1;
|
||||
}
|
||||
ESP_LOGD(TAG, "send error: errno %d", err);
|
||||
this->request_close_();
|
||||
return -1;
|
||||
}
|
||||
|
||||
void AsyncEventSourceResponse::drain_tail_() {
|
||||
if (this->sending_ || this->close_requested_ || this->tail_len_ == 0) {
|
||||
return;
|
||||
}
|
||||
SendGuard guard{*this};
|
||||
|
||||
const size_t remaining = this->tail_len_ - this->tail_sent_;
|
||||
struct iovec iov = {this->tail_.get() + this->tail_sent_, remaining};
|
||||
const ssize_t sent = this->send_(&iov, 1);
|
||||
if (sent < 0) {
|
||||
return;
|
||||
}
|
||||
if (sent == 0) {
|
||||
// Socket buffer full, try again later
|
||||
// NOTE: Similar logic exists in web_server/web_server.cpp in DeferredUpdateEventSource::process_deferred_queue_().
|
||||
// The IDF path is intentionally time-based and closes through HTTPD to preserve session ownership.
|
||||
const uint32_t now = App.get_loop_component_start_time();
|
||||
if (this->send_failure_started_ms_ == 0) {
|
||||
this->send_failure_started_ms_ = now != 0 ? now : 1; // Reserve zero for no stall.
|
||||
}
|
||||
if (static_cast<int32_t>(now - (this->send_failure_started_ms_ + SEND_STALL_TIMEOUT_MS)) >= 0) {
|
||||
ESP_LOGW(TAG, "Closing stuck EventSource connection after %" PRIu32 " ms without send progress",
|
||||
now - this->send_failure_started_ms_);
|
||||
this->request_close_();
|
||||
}
|
||||
return;
|
||||
}
|
||||
|
||||
this->send_failure_started_ms_ = 0;
|
||||
this->tail_sent_ += sent;
|
||||
if (this->tail_sent_ < this->tail_len_) {
|
||||
ESP_LOGV(TAG, "Partial send: %zd/%zu bytes (total: %u/%u)", sent, remaining, this->tail_sent_, this->tail_len_);
|
||||
return;
|
||||
}
|
||||
// Fully sent; the storage stays for the next stall
|
||||
this->tail_len_ = 0;
|
||||
}
|
||||
|
||||
bool AsyncEventSourceResponse::reserve_tail_(size_t len) {
|
||||
if (this->tail_cap_ >= len) {
|
||||
return true;
|
||||
}
|
||||
if (len > TAIL_MAX_SIZE) {
|
||||
return false;
|
||||
}
|
||||
// Nothing is pending while the tail grows, so free the old block first. PREFER_INTERNAL keeps
|
||||
// the tail where plain new put it.
|
||||
this->tail_.reset();
|
||||
this->tail_ = RAMAllocator<uint8_t>(RAMAllocator<uint8_t>::PREFER_INTERNAL).make_unique_array_for_overwrite(len);
|
||||
this->tail_cap_ = this->tail_ ? len : 0;
|
||||
return this->tail_cap_ != 0;
|
||||
}
|
||||
|
||||
bool AsyncEventSourceResponse::stash_chunk_(const char *prefix, size_t prefix_len, const char *message,
|
||||
size_t message_len, size_t total, size_t sent) {
|
||||
// A log event of nothing but line breaks is the largest chunk; reserve_tail_() must never
|
||||
// refuse it for size, only for memory
|
||||
static_assert(SSE_SEP_LEN * web_server::LOG_EVENT_MAX_LEN + PREFIX_BUF_SIZE + SSE_SUFFIX_LEN <= TAIL_MAX_SIZE,
|
||||
"the log cut in web_server.h must keep a worst case log event inside the tail ceiling");
|
||||
if (!this->reserve_tail_(total)) {
|
||||
if (sent != 0) {
|
||||
// Part of the chunk is on the wire, so the stream is broken and the client has to go
|
||||
ESP_LOGW(TAG, "Cannot buffer a %zu byte chunk, closing", total);
|
||||
this->request_close_();
|
||||
} else {
|
||||
this->tail_alloc_failed_(total); // nothing on the wire, the caller retries on the stall clock
|
||||
}
|
||||
return false;
|
||||
}
|
||||
uint8_t *dst = this->tail_.get();
|
||||
std::memcpy(dst, prefix, prefix_len);
|
||||
dst += prefix_len;
|
||||
for_each_chunk_piece(
|
||||
message, message_len,
|
||||
[](void *ctx, const char *piece, size_t len) {
|
||||
auto &out = *static_cast<uint8_t **>(ctx);
|
||||
std::memcpy(out, piece, len);
|
||||
out += len;
|
||||
},
|
||||
&dst);
|
||||
this->tail_len_ = total;
|
||||
this->tail_sent_ = sent;
|
||||
return true;
|
||||
}
|
||||
|
||||
void AsyncEventSourceResponse::loop() {
|
||||
if (this->close_requested_) {
|
||||
this->process_close_();
|
||||
return;
|
||||
}
|
||||
this->refresh_lru_();
|
||||
drain_tail_();
|
||||
process_deferred_queue_();
|
||||
if (this->close_requested_)
|
||||
return;
|
||||
// One step per loop; refusals retry next pass
|
||||
this->entities_iterator_.try_advance(1);
|
||||
}
|
||||
|
||||
#if defined(__GNUC__) && !defined(__clang__) && __GNUC__ >= 14 && defined(__OPTIMIZE_SIZE__)
|
||||
#pragma GCC diagnostic push
|
||||
#pragma GCC diagnostic ignored "-Wstack-usage=" // the one frame that holds the JSON arena
|
||||
#endif
|
||||
AsyncEventSourceResponse::JsonPrepareResult AsyncEventSourceResponse::prepare_json_(void *source,
|
||||
message_generator_t *generator,
|
||||
char *buffer, size_t *size_out) {
|
||||
// Log lines emitted while the generator runs are dropped for this session: on the large path they would fill
|
||||
// tail_ before it is overwritten, and on the small path they would go out ahead of the event being built.
|
||||
SendGuard guard{*this};
|
||||
json::JsonArena<json::JSON_ARENA_SIZE> arena;
|
||||
json::JsonBuilder builder(&arena);
|
||||
generator(this->web_server_, source, builder);
|
||||
*size_out = builder.serialize_to(buffer, JSON_BUF_SIZE);
|
||||
if (*size_out < JSON_BUF_SIZE) {
|
||||
return JsonPrepareResult::JSON_PREPARE_RESULT_BUFFER;
|
||||
}
|
||||
|
||||
char prefix[PREFIX_BUF_SIZE];
|
||||
const size_t prefix_len = build_chunk_prefix(prefix, sizeof(prefix), "state", 0, 0, true);
|
||||
|
||||
// Nothing reaches the wire until the complete document and its framing are owned by the caller.
|
||||
size_t json_len = 0;
|
||||
size_t cap = std::max<size_t>(JSON_BUF_SIZE * 2, this->tail_cap_);
|
||||
for (;;) {
|
||||
if (!this->reserve_tail_(cap)) {
|
||||
*size_out = cap;
|
||||
return JsonPrepareResult::JSON_PREPARE_RESULT_NO_MEMORY;
|
||||
}
|
||||
const size_t room = cap - prefix_len - SSE_SUFFIX_LEN;
|
||||
json_len = builder.serialize_to(reinterpret_cast<char *>(this->tail_.get()) + prefix_len, room);
|
||||
if (json_len < room) {
|
||||
break;
|
||||
}
|
||||
if (cap >= TAIL_MAX_SIZE) {
|
||||
*size_out = room;
|
||||
return JsonPrepareResult::JSON_PREPARE_RESULT_TOO_LARGE;
|
||||
}
|
||||
cap = std::min<size_t>(cap * 2, TAIL_MAX_SIZE);
|
||||
}
|
||||
|
||||
const size_t total = prefix_len + json_len + SSE_SUFFIX_LEN;
|
||||
write_chunk_header(prefix, total - CHUNK_HDR_LEN - CHUNK_END_LEN);
|
||||
uint8_t *dst = this->tail_.get();
|
||||
std::memcpy(dst, prefix, prefix_len);
|
||||
std::memcpy(dst + prefix_len + json_len, SSE_SUFFIX, SSE_SUFFIX_LEN);
|
||||
this->tail_len_ = total;
|
||||
this->tail_sent_ = 0;
|
||||
return JsonPrepareResult::JSON_PREPARE_RESULT_TAIL;
|
||||
}
|
||||
#if defined(__GNUC__) && !defined(__clang__) && __GNUC__ >= 14 && defined(__OPTIMIZE_SIZE__)
|
||||
#pragma GCC diagnostic pop
|
||||
#endif
|
||||
|
||||
bool AsyncEventSourceResponse::send_json_(void *source, message_generator_t *generator) {
|
||||
if (!this->ready_to_send_()) {
|
||||
return false;
|
||||
}
|
||||
char buffer[JSON_BUF_SIZE];
|
||||
size_t size;
|
||||
const auto result = this->prepare_json_(source, generator, buffer, &size);
|
||||
switch (result) {
|
||||
case JsonPrepareResult::JSON_PREPARE_RESULT_BUFFER:
|
||||
return this->try_send_nodefer(buffer, size, "state");
|
||||
case JsonPrepareResult::JSON_PREPARE_RESULT_TAIL:
|
||||
this->drain_tail_();
|
||||
return true;
|
||||
case JsonPrepareResult::JSON_PREPARE_RESULT_NO_MEMORY: {
|
||||
SendGuard guard{*this};
|
||||
this->tail_alloc_failed_(size);
|
||||
return false;
|
||||
}
|
||||
case JsonPrepareResult::JSON_PREPARE_RESULT_TOO_LARGE: {
|
||||
SendGuard guard{*this};
|
||||
ESP_LOGW(TAG, "State event does not fit %zu bytes, dropped", size);
|
||||
this->tail_.reset();
|
||||
this->tail_cap_ = 0;
|
||||
this->send_failure_started_ms_ = 0;
|
||||
return true;
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
void AsyncEventSourceResponse::tail_alloc_failed_(size_t cap) {
|
||||
// Same stall clock as a socket that stops draining, so a session cannot retry forever
|
||||
// Socket buffer full, try again later; a socket that stays full past the stall timeout is closed
|
||||
const uint32_t now = App.get_loop_component_start_time();
|
||||
if (this->send_failure_started_ms_ == 0) {
|
||||
this->send_failure_started_ms_ = now != 0 ? now : 1; // Reserve zero for no stall.
|
||||
ESP_LOGW(TAG, "No memory for a %zu byte chunk", cap);
|
||||
return;
|
||||
}
|
||||
if (static_cast<int32_t>(now - (this->send_failure_started_ms_ + SEND_STALL_TIMEOUT_MS)) >= 0) {
|
||||
ESP_LOGW(TAG, "Closing EventSource after %" PRIu32 " ms without memory", now - this->send_failure_started_ms_);
|
||||
this->request_close_();
|
||||
this->stall_.start(now);
|
||||
if (this->stall_.is_expired(now, SEND_STALL_TIMEOUT_MS)) {
|
||||
ESP_LOGW(TAG, "Closing stuck stream after %" PRIu32 " ms without send progress", this->stall_.elapsed(now));
|
||||
this->close();
|
||||
return -1;
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
bool AsyncEventSourceResponse::ready_to_send_() {
|
||||
if (this->sending_ || this->fd_.load() == 0 || this->close_requested_) {
|
||||
return false;
|
||||
}
|
||||
drain_tail_();
|
||||
return !this->close_requested_ && this->tail_len_ == 0;
|
||||
ssize_t AsyncStreamResponse::write(const void *data, size_t len) {
|
||||
struct iovec iov = {const_cast<void *>(data), len};
|
||||
return this->write(&iov, 1);
|
||||
}
|
||||
|
||||
bool AsyncEventSourceResponse::try_send_nodefer(const char *message, size_t message_len, const char *event, uint32_t id,
|
||||
uint32_t reconnect) {
|
||||
if (!this->ready_to_send_()) {
|
||||
return false;
|
||||
}
|
||||
SendGuard guard{*this};
|
||||
|
||||
// Everything after the prefix goes out straight from the caller's buffer
|
||||
char prefix[PREFIX_BUF_SIZE];
|
||||
const size_t prefix_len = build_chunk_prefix(prefix, sizeof(prefix), event, id, reconnect, message != nullptr);
|
||||
if (message == nullptr && prefix_len == CHUNK_HDR_LEN) {
|
||||
return true; // Match ESPAsyncWebServer: nothing to send
|
||||
}
|
||||
if (prefix_len >= PREFIX_BUF_SIZE - 1) {
|
||||
// The appenders truncate silently, which would put a malformed event on the wire
|
||||
ESP_LOGW(TAG, "Event name too long, dropped");
|
||||
return true;
|
||||
}
|
||||
|
||||
// Gather list: the prefix, then the data lines and their separators from the message
|
||||
struct Gather {
|
||||
struct iovec iov[MAX_SEND_IOV]; // left uninitialized on purpose
|
||||
int iovcnt{1};
|
||||
size_t total{0};
|
||||
bool fits{true};
|
||||
} g;
|
||||
g.total = prefix_len;
|
||||
for_each_chunk_piece(
|
||||
message, message_len,
|
||||
[](void *ctx, const char *piece, size_t len) {
|
||||
auto &g = *static_cast<Gather *>(ctx);
|
||||
g.total += len;
|
||||
if (len == 0) {
|
||||
return;
|
||||
}
|
||||
if (g.iovcnt == MAX_SEND_IOV) {
|
||||
g.fits = false;
|
||||
return;
|
||||
}
|
||||
g.iov[g.iovcnt++] = {const_cast<char *>(piece), len};
|
||||
},
|
||||
&g);
|
||||
// The header and the terminator are not part of the chunk length
|
||||
write_chunk_header(prefix, g.total - CHUNK_HDR_LEN - CHUNK_END_LEN);
|
||||
g.iov[0] = {prefix, prefix_len};
|
||||
|
||||
// A message with more lines than the list holds skips straight to the tail
|
||||
const ssize_t sent = g.fits ? this->send_(g.iov, g.iovcnt) : 0;
|
||||
if (sent < 0) {
|
||||
return false;
|
||||
}
|
||||
if (static_cast<size_t>(sent) == g.total) {
|
||||
this->send_failure_started_ms_ = 0; // progress, whichever stall clock was running
|
||||
return true;
|
||||
}
|
||||
// The caller's buffers do not outlive this call, so keep the chunk and continue from loop()
|
||||
return this->stash_chunk_(prefix, prefix_len, message, message_len, g.total, sent);
|
||||
}
|
||||
|
||||
void AsyncEventSourceResponse::deferrable_send_state(void *source, bool detail_all,
|
||||
message_generator_t *message_generator) {
|
||||
// Let the detail_all listing finish before bare state events, so the web UI never shows unnamed entries and
|
||||
// initial connect sends less
|
||||
if (!detail_all && !this->entities_iterator_.completed())
|
||||
return;
|
||||
|
||||
drain_tail_();
|
||||
process_deferred_queue_();
|
||||
|
||||
if (this->close_requested_) {
|
||||
return;
|
||||
}
|
||||
|
||||
if (this->tail_len_ != 0 || !deferred_queue_.empty()) {
|
||||
// outgoing event buffer or deferred queue still not empty which means downstream tcp send buffer full, no point
|
||||
// trying to send first
|
||||
deq_push_back_with_dedup_(source, message_generator);
|
||||
} else {
|
||||
// A send error closes the session and clears the queue; nothing is queued after that
|
||||
if (!this->send_json_(source, message_generator) && !this->close_requested_) {
|
||||
deq_push_back_with_dedup_(source, message_generator);
|
||||
}
|
||||
}
|
||||
}
|
||||
#endif
|
||||
|
||||
#ifdef USE_WEBSERVER_OTA
|
||||
esp_err_t AsyncWebServer::handle_multipart_upload_(httpd_req_t *r, const char *content_type) {
|
||||
// Parse boundary and create reader
|
||||
|
||||
@@ -15,21 +15,14 @@
|
||||
#include <utility>
|
||||
#include <vector>
|
||||
|
||||
#ifdef USE_WEBSERVER
|
||||
#include "esphome/components/json/json_util.h"
|
||||
#include "esphome/components/web_server/list_entities.h"
|
||||
#include "sse_chunk.h"
|
||||
#endif
|
||||
#include "esphome/components/web_server_base/stream_head.h"
|
||||
|
||||
struct iovec; // NOLINT(readability-identifier-naming) - forward decl of lwip's gather list entry
|
||||
|
||||
namespace esphome {
|
||||
#ifdef USE_WEBSERVER
|
||||
namespace web_server {
|
||||
class WebServer;
|
||||
}; // namespace web_server
|
||||
#endif
|
||||
namespace web_server_idf {
|
||||
namespace esphome::web_server_idf {
|
||||
|
||||
using web_server_base::HttpHeader;
|
||||
using web_server_base::StallClock;
|
||||
|
||||
class AsyncWebParameter {
|
||||
public:
|
||||
@@ -191,6 +184,7 @@ class AsyncWebServerRequest {
|
||||
void onDisconnect(std::function<void()> fn) { this->on_disconnect_ = std::move(fn); }
|
||||
#endif
|
||||
|
||||
/// sess_ctx is reserved for AsyncStreamResponse: the server's close_fn treats any session context as one.
|
||||
operator httpd_req_t *() const { return this->req_; }
|
||||
optional<std::string> get_header(const char *name) const;
|
||||
/// Copy a header value into buf without heap allocation.
|
||||
@@ -269,204 +263,85 @@ class AsyncWebHandler {
|
||||
virtual bool isRequestHandlerTrivial() const { return true; }
|
||||
};
|
||||
|
||||
#ifdef USE_WEBSERVER
|
||||
class AsyncEventSource;
|
||||
class AsyncEventSourceResponse;
|
||||
|
||||
using message_generator_t = void(esphome::web_server::WebServer *, void *, json::JsonBuilder &);
|
||||
|
||||
/*
|
||||
This class holds a pointer to the source component that wants to publish a state event, and a pointer to a function
|
||||
that will lazily generate that event. The two pointers allow dedup in the deferred queue if multiple publishes for
|
||||
the same component are backed up, and take up only two pointers of memory. The entry in the deferred queue (a
|
||||
std::vector) is the DeferredEvent instance itself (not a pointer to one elsewhere in heap) so still only two pointers
|
||||
per entry (and no heap fragmentation). Even 100 backed up events (you'd have to have at least 100 sensors publishing
|
||||
because of dedup) would take up only 0.8 kB.
|
||||
*/
|
||||
struct DeferredEvent {
|
||||
friend class AsyncEventSourceResponse;
|
||||
|
||||
protected:
|
||||
void *source_;
|
||||
message_generator_t *message_generator_;
|
||||
|
||||
/// A response that keeps its connection open and is written from the main loop, such as an event stream or
|
||||
/// an audio stream. Use it directly or derive from it. Call loop() and delete the object only through the type
|
||||
/// it was created as: subclasses hide loop() instead of overriding it, and the destructor is not virtual.
|
||||
///
|
||||
/// Lifecycle:
|
||||
/// 1. Httpd task, inside handleRequest(): create it and call begin(), then hand the pointer to the main loop.
|
||||
/// 2. Main loop: once is_attached() is true, write() may go through. Call loop() on every pass.
|
||||
/// 3. Main loop: delete it once safe_to_delete() is true. httpd owns the socket throughout.
|
||||
class AsyncStreamResponse {
|
||||
public:
|
||||
DeferredEvent(void *source, message_generator_t *message_generator)
|
||||
: source_(source), message_generator_(message_generator) {}
|
||||
bool operator==(const DeferredEvent &test) const {
|
||||
return (source_ == test.source_ && message_generator_ == test.message_generator_);
|
||||
}
|
||||
};
|
||||
static_assert(sizeof(DeferredEvent) == sizeof(void *) + sizeof(message_generator_t *),
|
||||
"DeferredEvent should have no padding");
|
||||
/// Httpd task, once. Sends the status line (for example HTTPD_200), Content-Type, headers, the default headers
|
||||
/// and body (a C string or null, sent as is), then makes this object the session context. A chunked response
|
||||
/// passes {"Transfer-Encoding", "chunked"} as its first header and frames each chunk itself.
|
||||
void begin(AsyncWebServerRequest *request, const char *status, const char *content_type,
|
||||
std::span<const HttpHeader> headers, const char *body = nullptr);
|
||||
|
||||
class AsyncEventSourceResponse {
|
||||
friend class AsyncEventSource;
|
||||
|
||||
public:
|
||||
bool try_send_nodefer(const char *message, size_t message_len, const char *event = nullptr, uint32_t id = 0,
|
||||
uint32_t reconnect = 0);
|
||||
// detail_all events go out while entities are still being listed; state events wait until listing is done
|
||||
void deferrable_send_state(void *source, bool detail_all, message_generator_t *message_generator);
|
||||
// The old event name argument would otherwise convert to true
|
||||
void deferrable_send_state(void *source, const char *event_type, message_generator_t *message_generator) = delete;
|
||||
/// Main loop. Non-blocking gather write. Returns the bytes accepted, which may be fewer than offered: the caller
|
||||
/// keeps the rest and continues from there. 0 means the socket is full or not ours yet; -1 means the stream failed
|
||||
/// and is closing. Refused writes for SEND_STALL_TIMEOUT_MS in a row also close it.
|
||||
ssize_t write(struct iovec *iov, int iovcnt);
|
||||
ssize_t write(const void *data, size_t len);
|
||||
/// Main loop. Shuts the connection down through httpd; safe to call more than once.
|
||||
void close();
|
||||
/// Main loop, every pass. Keeps the session fresh in httpd's LRU and retries a pending close.
|
||||
void loop();
|
||||
/// Httpd task, from the server's close_fn: no write() reaches the socket after this returns.
|
||||
static void socket_closing(httpd_handle_t hd, int fd);
|
||||
|
||||
protected:
|
||||
AsyncEventSourceResponse(const AsyncWebServerRequest *request, esphome::web_server_idf::AsyncEventSource *server,
|
||||
esphome::web_server::WebServer *ws);
|
||||
|
||||
// Main-loop only: sends initial ping/config/sorting_groups, starts entity iterator.
|
||||
void start_session_main_loop_();
|
||||
|
||||
void deq_push_back_with_dedup_(void *source, message_generator_t *message_generator);
|
||||
void process_deferred_queue_();
|
||||
// A new chunk may go out: not re-entered from a log line, session alive, tail empty
|
||||
bool ready_to_send_();
|
||||
// Non-blocking gather write. Returns bytes written, 0 on would-block or a socket that is not
|
||||
// ours, -1 after requesting the close on any other error.
|
||||
ssize_t send_(struct iovec *iov, int iovcnt);
|
||||
// Push what is left of the chunk in tail_ to the socket; owns the stall timer.
|
||||
void drain_tail_();
|
||||
// Grow tail_ to hold len bytes, kept at its high-water mark. False on OOM.
|
||||
bool reserve_tail_(size_t len);
|
||||
// Keep the whole chunk in tail_ and continue from sent; false when the tail cannot be allocated.
|
||||
bool stash_chunk_(const char *prefix, size_t prefix_len, const char *message, size_t message_len, size_t total,
|
||||
size_t sent);
|
||||
// Send a state event; JSON too large for the stack buffer is serialized into tail_ instead
|
||||
bool send_json_(void *source, message_generator_t *generator);
|
||||
enum class JsonPrepareResult : uint8_t {
|
||||
JSON_PREPARE_RESULT_BUFFER,
|
||||
JSON_PREPARE_RESULT_TAIL,
|
||||
JSON_PREPARE_RESULT_NO_MEMORY,
|
||||
JSON_PREPARE_RESULT_TOO_LARGE,
|
||||
};
|
||||
// Keep the arena out of the caller's frame so it is gone before the state send.
|
||||
// size_out: BUFFER = JSON length, NO_MEMORY = requested tail capacity, TOO_LARGE = available JSON room;
|
||||
// unused for TAIL.
|
||||
JsonPrepareResult __attribute__((noinline)) prepare_json_(void *source, message_generator_t *generator, char *buffer,
|
||||
size_t *size_out);
|
||||
// Warn once, and close the session once the stall timeout passes with no memory for the tail
|
||||
void tail_alloc_failed_(size_t cap);
|
||||
void request_close_();
|
||||
|
||||
// A log line emitted inside a send re-enters try_send_nodefer on this session; refuse it
|
||||
struct SendGuard {
|
||||
AsyncEventSourceResponse &owner;
|
||||
explicit SendGuard(AsyncEventSourceResponse &owner) : owner(owner) { owner.sending_ = true; }
|
||||
~SendGuard() { this->owner.sending_ = false; }
|
||||
};
|
||||
void process_close_();
|
||||
static void close_session_work(void *arg);
|
||||
void refresh_lru_();
|
||||
static void refresh_lru_work(void *arg);
|
||||
// httpd frees a session before closing its socket, so the fd may already belong to a new client
|
||||
bool owns_session_(int fd) const { return fd != 0 && httpd_sess_get_ctx(this->hd_, fd) == this; }
|
||||
|
||||
// Deletable only after destroy() zeroed fd_ and no queued HTTPD work still references this object.
|
||||
bool safe_to_delete_() const {
|
||||
/// httpd holds this response as the session context; begin() has sent the whole head by then.
|
||||
bool is_attached() const { return this->owns_session_(this->fd_.load()); }
|
||||
/// The client is still connected; httpd has not freed the session.
|
||||
bool is_connected() const { return this->fd_.load() != 0; }
|
||||
bool is_closing() const { return this->close_requested_; }
|
||||
/// The socket fd, for log lines only
|
||||
int log_id() const { return this->fd_.load(); }
|
||||
/// Sends are making no progress; after write() returns -1 this tells a send timeout from a send error.
|
||||
bool is_stalled() const { return this->stall_.is_running(); }
|
||||
/// The session is gone and no queued httpd work still references this object.
|
||||
bool safe_to_delete() const {
|
||||
return this->fd_.load() == 0 && !this->close_work_queued_.load(std::memory_order_acquire) &&
|
||||
!this->lru_work_queued_.load(std::memory_order_acquire);
|
||||
}
|
||||
|
||||
static constexpr uint32_t SEND_STALL_TIMEOUT_MS = 20000;
|
||||
|
||||
protected:
|
||||
void process_close_();
|
||||
static void close_session_work(void *arg);
|
||||
void refresh_lru_();
|
||||
static void refresh_lru_work(void *arg);
|
||||
// httpd closes the socket before it clears the session, so this can still match a closed fd; write() also checks
|
||||
// socket_closed_
|
||||
bool owns_session_(int fd) const { return fd != 0 && httpd_sess_get_ctx(this->hd_, fd) == this; }
|
||||
static void destroy(void *p);
|
||||
AsyncEventSource *server_;
|
||||
|
||||
httpd_handle_t hd_{};
|
||||
std::atomic<int> fd_{};
|
||||
std::vector<DeferredEvent> deferred_queue_;
|
||||
esphome::web_server::WebServer *web_server_;
|
||||
esphome::web_server::ListEntitiesIterator entities_iterator_;
|
||||
// One chunk the socket did not take whole, allocated on the first stall; the only heap use
|
||||
// on the send path
|
||||
RAMUniquePtr<uint8_t[]> tail_;
|
||||
uint32_t send_failure_started_ms_{0}; // Zero means no send stall in progress.
|
||||
// Runs while sends make no progress; a running clock keeps its start time
|
||||
StallClock stall_;
|
||||
uint32_t next_close_attempt_ms_{0};
|
||||
uint32_t last_lru_refresh_ms_{0};
|
||||
uint16_t tail_cap_{0};
|
||||
uint16_t tail_len_{0}; // Zero means nothing pending
|
||||
uint16_t tail_sent_{0};
|
||||
// Set on the main loop before queueing close work, cleared by the HTTPD-task callback when done.
|
||||
std::atomic<bool> close_work_queued_{false};
|
||||
std::atomic<bool> lru_work_queued_{false};
|
||||
// Guarded by the stream send mutex
|
||||
bool socket_closed_{false};
|
||||
// Main-loop only; the HTTPD task never reads or writes these flags.
|
||||
bool close_requested_{false};
|
||||
bool close_retry_warning_logged_{false};
|
||||
bool sending_{false};
|
||||
// The longest multi line log message in the tree (a climate dump_config) has 22 lines; a
|
||||
// longer one goes through the tail
|
||||
static constexpr size_t MAX_SEND_LINES = 22;
|
||||
static constexpr size_t MAX_SEND_IOV = 1 + 2 * MAX_SEND_LINES;
|
||||
// Chunk header, retry/id/event lines and the first "data: "
|
||||
static constexpr size_t PREFIX_BUF_SIZE = 128;
|
||||
|
||||
// Stack buffer for a state event's JSON; a larger document is serialized into the tail
|
||||
static constexpr size_t JSON_BUF_SIZE = 1024;
|
||||
// Same ceiling JsonBuilder::serialize() applies (max_heap_size in json_util.cpp); a larger
|
||||
// document is dropped before anything is on the wire
|
||||
static constexpr size_t JSON_MAX_SIZE = 5120;
|
||||
// Most RAM a stalled session keeps: the largest state document plus any accepted framing
|
||||
static constexpr size_t TAIL_MAX_SIZE = JSON_MAX_SIZE + PREFIX_BUF_SIZE + SSE_SUFFIX_LEN;
|
||||
static constexpr uint32_t SEND_STALL_TIMEOUT_MS = 20000;
|
||||
static constexpr uint32_t CLOSE_RETRY_INTERVAL_MS = 250;
|
||||
static constexpr uint32_t CLOSE_CONFIRM_INTERVAL_MS = 1000;
|
||||
static constexpr uint32_t LRU_REFRESH_INTERVAL_MS = 1000;
|
||||
};
|
||||
|
||||
using AsyncEventSourceClient = AsyncEventSourceResponse;
|
||||
|
||||
class AsyncEventSource : public AsyncWebHandler {
|
||||
friend class AsyncEventSourceResponse;
|
||||
|
||||
public:
|
||||
AsyncEventSource(StringRef url, esphome::web_server::WebServer *ws) : url_(url), web_server_(ws) {}
|
||||
~AsyncEventSource() override;
|
||||
|
||||
// NOLINTNEXTLINE(readability-identifier-naming)
|
||||
bool canHandle(AsyncWebServerRequest *request) const override {
|
||||
if (request->method() != HTTP_GET)
|
||||
return false;
|
||||
char url_buf[AsyncWebServerRequest::URL_BUF_SIZE];
|
||||
return request->url_to(url_buf) == this->url_;
|
||||
}
|
||||
// NOLINTNEXTLINE(readability-identifier-naming)
|
||||
void handleRequest(AsyncWebServerRequest *request) override;
|
||||
void try_send_nodefer(const char *message, size_t message_len, const char *event = nullptr, uint32_t id = 0,
|
||||
uint32_t reconnect = 0);
|
||||
void deferrable_send_state(void *source, bool detail_all, message_generator_t *message_generator);
|
||||
// The old event name argument would otherwise convert to true
|
||||
void deferrable_send_state(void *source, const char *event_type, message_generator_t *message_generator) = delete;
|
||||
/// Returns true if there are sessions remaining (including pending cleanup).
|
||||
bool loop();
|
||||
bool empty() { return this->count() == 0; }
|
||||
|
||||
size_t count() const { return this->sessions_.size(); }
|
||||
|
||||
protected:
|
||||
// Cold path: move sessions from pending_sessions_ into sessions_ and greet each one.
|
||||
void __attribute__((noinline, cold)) adopt_pending_sessions_main_loop_();
|
||||
|
||||
StringRef url_; // Must outlive this object (string literal)
|
||||
// Main-loop only. Vector: SSE sessions are 1-5 connections, linear search beats set.
|
||||
std::vector<AsyncEventSourceResponse *> sessions_;
|
||||
// Httpd-task intake; guarded by pending_mutex_, gated by has_pending_sessions_.
|
||||
std::vector<AsyncEventSourceResponse *> pending_sessions_;
|
||||
Mutex pending_mutex_;
|
||||
esphome::web_server::WebServer *web_server_;
|
||||
std::atomic<bool> has_pending_sessions_{false};
|
||||
};
|
||||
#endif // USE_WEBSERVER
|
||||
|
||||
struct HttpHeader {
|
||||
const char *name;
|
||||
const char *value;
|
||||
};
|
||||
|
||||
class DefaultHeaders {
|
||||
friend class AsyncWebServerRequest;
|
||||
friend class AsyncStreamResponse;
|
||||
friend class AsyncWebServerResponse;
|
||||
#ifdef USE_WEBSERVER
|
||||
friend class AsyncEventSourceResponse;
|
||||
#endif
|
||||
static_assert(WEB_SERVER_DEFAULT_HEADERS_COUNT <= 8, "AsyncWebServerResponse::replaced_defaults_ holds 8 bits");
|
||||
|
||||
public:
|
||||
@@ -484,8 +359,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)
|
||||
|
||||
|
||||
@@ -5,47 +5,57 @@
|
||||
|
||||
namespace esphome::wifi {
|
||||
|
||||
// Switching APs calls esp_wifi_disconnect(), which queues an association-leave event from the old AP. Remembers
|
||||
// that AP so its leave event does not end the connection attempt started after it. Platform free for host tests.
|
||||
/// Which expected leave a disconnect matched.
|
||||
enum class ExpectedLeave : uint8_t {
|
||||
EXPECTED_LEAVE_NONE,
|
||||
EXPECTED_LEAVE_AP,
|
||||
EXPECTED_LEAVE_ATTEMPT, ///< An attempt that never associated, so no BSSID
|
||||
};
|
||||
|
||||
// Switching APs calls esp_wifi_disconnect(), which queues a leave event for the old AP, or with no BSSID for an
|
||||
// attempt that never associated. Remembers it so that event does not end the next attempt. Platform free for tests.
|
||||
class ExpectedDisconnect {
|
||||
public:
|
||||
void arm(const uint8_t *bssid) {
|
||||
std::copy_n(bssid, this->bssid_.size(), this->bssid_.begin());
|
||||
this->armed_ = true;
|
||||
}
|
||||
void clear() { this->armed_ = false; }
|
||||
void arm(const uint8_t *bssid) { this->arm_(bssid, ExpectedLeave::EXPECTED_LEAVE_AP); }
|
||||
void clear() { this->expected_ = ExpectedLeave::EXPECTED_LEAVE_NONE; }
|
||||
|
||||
/// Disconnects from the current AP and, once that succeeds, expects its leave event. read_bssid(uint8_t *out)
|
||||
/// returns whether there was an AP and runs first, since the record is gone after the disconnect; disconnect()
|
||||
/// returns whether it succeeded, which is also the result. Any earlier expectation is dropped.
|
||||
/// Disconnects and, once that succeeds, expects the leave event. read_bssid(uint8_t *out) runs first, since the
|
||||
/// record is gone after the disconnect, and writes the AP's BSSID only when it returns true; without an AP the
|
||||
/// attempt's leave, with no BSSID, is expected. disconnect() returns whether it succeeded, which is the result.
|
||||
template<typename ReadBssid, typename Disconnect>
|
||||
bool disconnect_and_expect_leave(ReadBssid &&read_bssid, Disconnect &&disconnect) {
|
||||
this->armed_ = false;
|
||||
this->clear();
|
||||
std::array<uint8_t, 6> bssid{};
|
||||
const bool had_ap = read_bssid(bssid.data());
|
||||
if (!disconnect())
|
||||
return false;
|
||||
if (had_ap)
|
||||
this->arm(bssid.data());
|
||||
this->arm_(bssid.data(), had_ap ? ExpectedLeave::EXPECTED_LEAVE_AP : ExpectedLeave::EXPECTED_LEAVE_ATTEMPT);
|
||||
return true;
|
||||
}
|
||||
|
||||
/// A connected event still queued from the previous AP; it keeps the expectation.
|
||||
bool is_previous_ap(const uint8_t *bssid) const {
|
||||
return this->armed_ && std::equal(this->bssid_.begin(), this->bssid_.end(), bssid);
|
||||
return this->expected_ != ExpectedLeave::EXPECTED_LEAVE_NONE &&
|
||||
std::equal(this->bssid_.begin(), this->bssid_.end(), bssid);
|
||||
}
|
||||
|
||||
/// Whether a disconnect is the previous AP's leave. Every disconnect ends the expectation, so it matches at
|
||||
/// most once; the caller handles roaming disconnects before this.
|
||||
bool consume_disconnect(const uint8_t *bssid, bool assoc_leave) {
|
||||
const bool expected = assoc_leave && this->is_previous_ap(bssid);
|
||||
this->armed_ = false;
|
||||
return expected;
|
||||
/// Which expected leave a disconnect is. \p is_leave covers an AP's association leave (8) and the station leave
|
||||
/// of an attempt that never associated (36). Every disconnect ends the expectation, so it matches at most once.
|
||||
ExpectedLeave consume_disconnect(const uint8_t *bssid, bool is_leave) {
|
||||
const ExpectedLeave matched =
|
||||
is_leave && this->is_previous_ap(bssid) ? this->expected_ : ExpectedLeave::EXPECTED_LEAVE_NONE;
|
||||
this->clear();
|
||||
return matched;
|
||||
}
|
||||
|
||||
protected:
|
||||
void arm_(const uint8_t *bssid, ExpectedLeave expected) {
|
||||
std::copy_n(bssid, this->bssid_.size(), this->bssid_.begin());
|
||||
this->expected_ = expected;
|
||||
}
|
||||
|
||||
std::array<uint8_t, 6> bssid_{};
|
||||
bool armed_{false};
|
||||
ExpectedLeave expected_{ExpectedLeave::EXPECTED_LEAVE_NONE};
|
||||
};
|
||||
|
||||
} // namespace esphome::wifi
|
||||
@@ -1158,10 +1158,12 @@ void WiFiComponent::wifi_process_event_(IDFWiFiEvent *data) {
|
||||
ESP_LOGI(TAG, "Disconnected ssid='%.*s' reason='Station Roaming'", it.ssid_len, (const char *) it.ssid);
|
||||
return;
|
||||
}
|
||||
const bool expected_disconnect =
|
||||
s_expected_disconnect.consume_disconnect(it.bssid, it.reason == WIFI_REASON_ASSOC_LEAVE);
|
||||
const ExpectedLeave expected = s_expected_disconnect.consume_disconnect(
|
||||
it.bssid, it.reason == WIFI_REASON_ASSOC_LEAVE || it.reason == WIFI_REASON_STA_LEAVING);
|
||||
const bool expected_disconnect = expected != ExpectedLeave::EXPECTED_LEAVE_NONE;
|
||||
if (expected_disconnect) {
|
||||
ESP_LOGD(TAG, "Expected disconnect from previous AP");
|
||||
ESP_LOGD(TAG, "Expected leave of the previous %s",
|
||||
expected == ExpectedLeave::EXPECTED_LEAVE_AP ? LOG_STR_LITERAL("AP") : LOG_STR_LITERAL("attempt"));
|
||||
} else if (it.reason == WIFI_REASON_NO_AP_FOUND) {
|
||||
ESP_LOGW(TAG, "Disconnected ssid='%.*s' reason='Probe Request Unsuccessful'", it.ssid_len,
|
||||
(const char *) it.ssid);
|
||||
@@ -1181,7 +1183,9 @@ void WiFiComponent::wifi_process_event_(IDFWiFiEvent *data) {
|
||||
// Refresh is_connected() cache; the old session is no longer connected.
|
||||
this->update_connected_state_();
|
||||
#ifdef USE_WIFI_CONNECT_STATE_LISTENERS
|
||||
this->notify_disconnect_state_listeners_();
|
||||
// An attempt that never associated ended no session listeners know of
|
||||
if (expected != ExpectedLeave::EXPECTED_LEAVE_ATTEMPT)
|
||||
this->notify_disconnect_state_listeners_();
|
||||
#endif
|
||||
|
||||
#if defined(USE_WIFI_AP_WISP) && !defined(USE_WIFI_AP_WISP_STATIC_DNS)
|
||||
|
||||
@@ -8,18 +8,16 @@ namespace esphome::zigbee {
|
||||
|
||||
ESPHOME_LOG_TAG(TAG, "zigbee.attribute");
|
||||
|
||||
void ZigbeeAttribute::set_attr_() {
|
||||
void ZigbeeAttribute::set_attr_internal() {
|
||||
if (!this->zb_->is_started()) {
|
||||
return;
|
||||
}
|
||||
if (esp_zigbee_lock_acquire(10 / portTICK_PERIOD_MS)) {
|
||||
ezb_zcl_status_t state = ezb_zcl_set_attr_value(this->endpoint_id_, this->cluster_id_, this->role_, this->attr_id_,
|
||||
EZB_ZCL_STD_MANUF_CODE, this->value_p_, false);
|
||||
// cleared before report_() so it can disable the loop
|
||||
// when the report has to wait for join
|
||||
this->set_attr_requested_ = false;
|
||||
if (this->force_report_) {
|
||||
this->report_(true);
|
||||
this->report_internal(true);
|
||||
}
|
||||
// Check for error
|
||||
if (state != EZB_ZCL_STATUS_SUCCESS) {
|
||||
@@ -30,15 +28,12 @@ void ZigbeeAttribute::set_attr_() {
|
||||
}
|
||||
}
|
||||
|
||||
void ZigbeeAttribute::report_(bool has_lock) {
|
||||
void ZigbeeAttribute::report_internal(bool has_lock) {
|
||||
if (!this->report_enabled) {
|
||||
return;
|
||||
}
|
||||
if (!this->zb_->is_joined()) {
|
||||
this->report_requested_ = true;
|
||||
if (!this->set_attr_requested_) {
|
||||
this->disable_loop();
|
||||
}
|
||||
return;
|
||||
}
|
||||
if (has_lock or esp_zigbee_lock_acquire(10 / portTICK_PERIOD_MS)) {
|
||||
@@ -61,43 +56,6 @@ void ZigbeeAttribute::report_(bool has_lock) {
|
||||
}
|
||||
}
|
||||
|
||||
void ZigbeeAttribute::set_report(ZigbeeReportT report) {
|
||||
if (report == ZigbeeReportT::ZIGBEE_REPORT_DEFAULT || report == ZigbeeReportT::ZIGBEE_REPORT_COORDINATOR) {
|
||||
return;
|
||||
}
|
||||
this->report_enabled = true;
|
||||
if (report == ZigbeeReportT::ZIGBEE_REPORT_FORCE) {
|
||||
this->force_report_ = true;
|
||||
}
|
||||
this->zb_->add_on_join_callback([this](bool) {
|
||||
if (this->report_requested_) {
|
||||
this->enable_loop();
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
void ZigbeeAttribute::report() {
|
||||
if (!this->report_enabled) {
|
||||
return;
|
||||
}
|
||||
this->report_requested_ = true;
|
||||
this->enable_loop();
|
||||
}
|
||||
|
||||
void ZigbeeAttribute::loop() {
|
||||
if (this->set_attr_requested_) {
|
||||
this->set_attr_();
|
||||
}
|
||||
|
||||
if (this->report_requested_) {
|
||||
this->report_(false);
|
||||
}
|
||||
|
||||
if (!this->report_requested_ && !this->set_attr_requested_) {
|
||||
this->disable_loop();
|
||||
}
|
||||
}
|
||||
|
||||
} // namespace esphome::zigbee
|
||||
|
||||
#endif
|
||||
|
||||
@@ -4,9 +4,8 @@
|
||||
#include <limits>
|
||||
#include <type_traits>
|
||||
|
||||
#include "esphome/core/automation.h"
|
||||
#include "esphome/core/component.h"
|
||||
#include "esphome/core/defines.h"
|
||||
#include "zigbee_entity_base_esp32.h"
|
||||
|
||||
#ifdef USE_ESP32
|
||||
#ifdef USE_ZIGBEE
|
||||
@@ -29,34 +28,22 @@
|
||||
|
||||
namespace esphome::zigbee {
|
||||
|
||||
enum ZigbeeReportT {
|
||||
ZIGBEE_REPORT_DEFAULT,
|
||||
ZIGBEE_REPORT_COORDINATOR,
|
||||
ZIGBEE_REPORT_ENABLE,
|
||||
ZIGBEE_REPORT_FORCE,
|
||||
};
|
||||
|
||||
class ZigbeeAttribute final : public Component {
|
||||
class ZigbeeAttribute final : public ZigbeeEntityBase {
|
||||
public:
|
||||
ZigbeeAttribute(ZigbeeComponent *parent, uint8_t endpoint_id, uint16_t cluster_id, uint8_t role, uint16_t attr_id,
|
||||
uint8_t attr_type, float scale, uint8_t max_size)
|
||||
: zb_(parent),
|
||||
endpoint_id_(endpoint_id),
|
||||
: ZigbeeEntityBase(parent, endpoint_id),
|
||||
cluster_id_(cluster_id),
|
||||
role_(role),
|
||||
attr_id_(attr_id),
|
||||
attr_type_(attr_type),
|
||||
max_size_(max_size),
|
||||
scale_(scale) {}
|
||||
void loop() override;
|
||||
template<typename T> void add_attr(T value);
|
||||
template<typename T> void set_attr(const T &value);
|
||||
uint8_t attr_type() { return attr_type_; }
|
||||
void set_report(ZigbeeReportT report);
|
||||
void report();
|
||||
|
||||
template<typename F> void add_on_value_callback(F &&callback) { on_value_callback_.add(std::forward<F>(callback)); }
|
||||
void on_value(ezb_zcl_attribute_t attribute) {
|
||||
void on_value(ezb_zcl_attribute_t attribute) override {
|
||||
if (attribute.data.type == this->attr_type() && attribute.data.value) {
|
||||
this->on_value_callback_.call(attribute);
|
||||
}
|
||||
@@ -75,13 +62,10 @@ class ZigbeeAttribute final : public Component {
|
||||
#ifdef USE_NUMBER
|
||||
template<typename T> void connect(number::Number *device);
|
||||
#endif
|
||||
bool report_enabled = false;
|
||||
|
||||
protected:
|
||||
void set_attr_();
|
||||
void report_(bool has_lock);
|
||||
ZigbeeComponent *zb_;
|
||||
uint8_t endpoint_id_;
|
||||
void set_attr_internal() override;
|
||||
void report_internal(bool has_lock) override;
|
||||
uint16_t cluster_id_;
|
||||
uint8_t role_;
|
||||
uint16_t attr_id_;
|
||||
@@ -89,9 +73,6 @@ class ZigbeeAttribute final : public Component {
|
||||
uint8_t max_size_;
|
||||
float scale_;
|
||||
void *value_p_{nullptr};
|
||||
bool set_attr_requested_{false};
|
||||
bool report_requested_{false};
|
||||
bool force_report_{false};
|
||||
LazyCallbackManager<void(ezb_zcl_attribute_t attribute)> on_value_callback_{};
|
||||
template<typename T> T scale_value_(float value);
|
||||
template<typename T> T invalid_value_();
|
||||
|
||||
@@ -0,0 +1,51 @@
|
||||
#include "zigbee_entity_base_esp32.h"
|
||||
#include "esphome/core/log.h"
|
||||
#include "esphome/core/defines.h"
|
||||
#ifdef USE_ESP32
|
||||
#ifdef USE_ZIGBEE
|
||||
|
||||
namespace esphome::zigbee {
|
||||
|
||||
ESPHOME_LOG_TAG(TAG, "zigbee.entity_base");
|
||||
|
||||
void ZigbeeEntityBase::set_report(ZigbeeReportT report) {
|
||||
if (report == ZigbeeReportT::ZIGBEE_REPORT_DEFAULT || report == ZigbeeReportT::ZIGBEE_REPORT_COORDINATOR) {
|
||||
return;
|
||||
}
|
||||
this->report_enabled = true;
|
||||
if (report == ZigbeeReportT::ZIGBEE_REPORT_FORCE) {
|
||||
this->force_report_ = true;
|
||||
}
|
||||
this->zb_->add_on_join_callback([this](bool) {
|
||||
if (this->report_requested_) {
|
||||
this->enable_loop();
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
void ZigbeeEntityBase::report() {
|
||||
if (!this->report_enabled) {
|
||||
return;
|
||||
}
|
||||
this->report_requested_ = true;
|
||||
this->enable_loop();
|
||||
}
|
||||
|
||||
void ZigbeeEntityBase::loop() {
|
||||
if (this->set_attr_requested_) {
|
||||
this->set_attr_internal();
|
||||
}
|
||||
|
||||
if (this->report_requested_ && this->zb_->is_joined()) {
|
||||
this->report_internal(false);
|
||||
}
|
||||
|
||||
if ((!this->report_requested_ || !this->zb_->is_joined()) && !this->set_attr_requested_) {
|
||||
this->disable_loop();
|
||||
}
|
||||
}
|
||||
|
||||
} // namespace esphome::zigbee
|
||||
|
||||
#endif
|
||||
#endif
|
||||
@@ -0,0 +1,47 @@
|
||||
#pragma once
|
||||
|
||||
#include "esphome/core/automation.h"
|
||||
#include "esphome/core/component.h"
|
||||
#include "esphome/core/defines.h"
|
||||
|
||||
#ifdef USE_ESP32
|
||||
#ifdef USE_ZIGBEE
|
||||
|
||||
#include "esp_zigbee.h"
|
||||
#include "zigbee_esp32.h"
|
||||
|
||||
namespace esphome::zigbee {
|
||||
|
||||
enum ZigbeeReportT {
|
||||
ZIGBEE_REPORT_DEFAULT,
|
||||
ZIGBEE_REPORT_COORDINATOR,
|
||||
ZIGBEE_REPORT_ENABLE,
|
||||
ZIGBEE_REPORT_FORCE,
|
||||
};
|
||||
|
||||
class ZigbeeEntityBase : public Component, public ZigbeeAttributeInterface {
|
||||
public:
|
||||
void loop() override;
|
||||
void set_report(ZigbeeReportT report);
|
||||
void report() override;
|
||||
|
||||
bool report_enabled = false;
|
||||
|
||||
protected:
|
||||
ZigbeeEntityBase(ZigbeeComponent *parent, uint8_t endpoint_id) : zb_(parent), endpoint_id_(endpoint_id) {}
|
||||
/// Handle pending attribute writes and clear set_attr_requested_ when complete; leave it set to retry.
|
||||
virtual void set_attr_internal() = 0;
|
||||
/// Handle pending reports and clear report_requested_ when complete; leave it set to retry.
|
||||
/// Acquire/release the Zigbee lock only when has_lock is false; otherwise the caller owns it.
|
||||
virtual void report_internal(bool has_lock) = 0;
|
||||
ZigbeeComponent *zb_;
|
||||
uint8_t endpoint_id_;
|
||||
bool set_attr_requested_{false};
|
||||
bool report_requested_{false};
|
||||
bool force_report_{false};
|
||||
};
|
||||
|
||||
} // namespace esphome::zigbee
|
||||
|
||||
#endif
|
||||
#endif
|
||||
@@ -31,7 +31,11 @@ static constexpr uint8_t MAX_ZB_QUEUE_SIZE = 32;
|
||||
|
||||
uint8_t *get_zcl_string(const char *str, uint8_t max_size, bool use_max_size = false);
|
||||
|
||||
class ZigbeeAttribute;
|
||||
class ZigbeeAttributeInterface {
|
||||
public:
|
||||
virtual void on_value(ezb_zcl_attribute_t attribute) = 0;
|
||||
virtual void report() = 0;
|
||||
};
|
||||
|
||||
class ZigbeeComponent final : public Component {
|
||||
public:
|
||||
@@ -48,8 +52,8 @@ class ZigbeeComponent final : public Component {
|
||||
bool register_device();
|
||||
|
||||
template<typename T>
|
||||
void add_attr(ZigbeeAttribute *attr, uint8_t endpoint_id, uint16_t cluster_id, uint8_t role, uint16_t attr_id,
|
||||
uint8_t max_size, T value);
|
||||
void add_attr(ZigbeeAttributeInterface *attr, uint8_t endpoint_id, uint16_t cluster_id, uint8_t role,
|
||||
uint16_t attr_id, uint8_t max_size, T value);
|
||||
|
||||
template<typename T>
|
||||
void add_attr(uint8_t endpoint_id, uint16_t cluster_id, uint8_t role, uint16_t attr_id, uint8_t max_size, T value);
|
||||
@@ -94,13 +98,13 @@ class ZigbeeComponent final : public Component {
|
||||
void end_parent_link_failures_(uint8_t seq);
|
||||
void reset_parent_link_failures_();
|
||||
template<typename T>
|
||||
void add_attr_(ZigbeeAttribute *attr, uint8_t endpoint_id, uint16_t cluster_id, uint8_t role, uint16_t attr_id,
|
||||
T *value_p);
|
||||
void add_attr_(ZigbeeAttributeInterface *attr, uint8_t endpoint_id, uint16_t cluster_id, uint8_t role,
|
||||
uint16_t attr_id, T *value_p);
|
||||
void handle_attribute_(ezb_zcl_message_info_t info, ezb_zcl_attribute_t attribute);
|
||||
// attributes_ will be used during operation in zigbee callbacks to update the attribute values and trigger
|
||||
// automations
|
||||
// key tuple could be replaced by single 64 (48) bit int with bit fields for endpoint, cluster, role and attr_id
|
||||
std::map<std::tuple<uint8_t, uint16_t, uint8_t, uint16_t>, ZigbeeAttribute *> attributes_;
|
||||
std::map<std::tuple<uint8_t, uint16_t, uint8_t, uint16_t>, ZigbeeAttributeInterface *> attributes_;
|
||||
ezb_af_device_desc_t dev_desc_ = ezb_af_create_device_desc();
|
||||
CallbackManager<void(bool)> join_cb_{};
|
||||
template<typename... Args> friend void enqueue_zb_event(Args... args);
|
||||
@@ -133,7 +137,7 @@ void ZigbeeComponent::add_attr(uint8_t endpoint_id, uint16_t cluster_id, uint8_t
|
||||
}
|
||||
|
||||
template<typename T>
|
||||
void ZigbeeComponent::add_attr(ZigbeeAttribute *attr, uint8_t endpoint_id, uint16_t cluster_id, uint8_t role,
|
||||
void ZigbeeComponent::add_attr(ZigbeeAttributeInterface *attr, uint8_t endpoint_id, uint16_t cluster_id, uint8_t role,
|
||||
uint16_t attr_id, uint8_t max_size, T value) {
|
||||
// The size byte of the zcl_str must be set to the maximum value,
|
||||
// even though the initial string may be shorter.
|
||||
@@ -151,7 +155,7 @@ void ZigbeeComponent::add_attr(ZigbeeAttribute *attr, uint8_t endpoint_id, uint1
|
||||
}
|
||||
|
||||
template<typename T>
|
||||
void ZigbeeComponent::add_attr_(ZigbeeAttribute *attr, uint8_t endpoint_id, uint16_t cluster_id, uint8_t role,
|
||||
void ZigbeeComponent::add_attr_(ZigbeeAttributeInterface *attr, uint8_t endpoint_id, uint16_t cluster_id, uint8_t role,
|
||||
uint16_t attr_id, T *value_p) {
|
||||
ezb_af_ep_desc_t ep_desc = ezb_af_device_get_endpoint_desc(this->dev_desc_, endpoint_id);
|
||||
if (ep_desc == NULL) {
|
||||
|
||||
@@ -1497,12 +1497,11 @@ class BindKeyValidator(SensitiveValidator):
|
||||
if any(len(part) != 2 for part in parts):
|
||||
raise Invalid(f"{self._name} must be format XX")
|
||||
for part in parts:
|
||||
try:
|
||||
parts_int.append(int(part, 16))
|
||||
except ValueError:
|
||||
raise Invalid(
|
||||
f"{self._name} must be hex values from 00 to FF"
|
||||
) from None
|
||||
# int() also accepts surrounding whitespace and a +/- sign, which
|
||||
# would silently validate a key different from the one typed
|
||||
if not re.fullmatch(r"[0-9A-Fa-f]{2}", part):
|
||||
raise Invalid(f"{self._name} must be hex values from 00 to FF")
|
||||
parts_int.append(int(part, 16))
|
||||
|
||||
return "".join(f"{part:02X}" for part in parts_int)
|
||||
|
||||
|
||||
@@ -846,7 +846,9 @@ def convert_visual_temperatures(
|
||||
that needs ``full_config`` is left for final validation, which runs this again with it."""
|
||||
if (unit := entity_temperature_unit(config, full_config)) is None:
|
||||
return config
|
||||
visual = config[CONF_VISUAL]
|
||||
# A platform entry that only holds nested entities has no visual section of its own
|
||||
if (visual := config.get(CONF_VISUAL)) is None:
|
||||
return config
|
||||
convert_temperature_keys(visual, (CONF_MIN_TEMPERATURE, CONF_MAX_TEMPERATURE), unit)
|
||||
section = visual if delta_section is None else visual.get(delta_section)
|
||||
if section is not None:
|
||||
|
||||
@@ -1472,6 +1472,42 @@ __attribute__((format(printf, 4, 5))) inline size_t buf_append_printf(char *buf,
|
||||
}
|
||||
#endif
|
||||
|
||||
/// Copy a PROGMEM string to `dst`, stopping at its null terminator or at `end`, and return the new end of `dst`.
|
||||
/// No terminator is written. Flash only allows aligned 32-bit loads on ESP8266, so `src` is read a whole word at a
|
||||
/// time, including the bytes after its null in the last word; `src` must be readable up to the end of that word.
|
||||
inline char *ESPHOME_ALWAYS_INLINE progmem_copy_str(char *dst, const char *end, const char *src) {
|
||||
const uint32_t offset = reinterpret_cast<uintptr_t>(src) & 3;
|
||||
const uint32_t *word = reinterpret_cast<const uint32_t *>(src - offset);
|
||||
uint32_t bytes = *word++;
|
||||
// Enters the loop at the first byte of src; after that each word runs its four steps without another dispatch, and
|
||||
// one step per byte keeps each a single extract
|
||||
switch (offset) {
|
||||
for (;;) {
|
||||
bytes = *word++;
|
||||
[[fallthrough]];
|
||||
case 0:
|
||||
if (dst >= end || static_cast<char>(bytes) == '\0')
|
||||
return dst;
|
||||
*dst++ = static_cast<char>(bytes);
|
||||
[[fallthrough]];
|
||||
case 1:
|
||||
if (dst >= end || static_cast<char>(bytes >> 8) == '\0')
|
||||
return dst;
|
||||
*dst++ = static_cast<char>(bytes >> 8);
|
||||
[[fallthrough]];
|
||||
case 2:
|
||||
if (dst >= end || static_cast<char>(bytes >> 16) == '\0')
|
||||
return dst;
|
||||
*dst++ = static_cast<char>(bytes >> 16);
|
||||
[[fallthrough]];
|
||||
default:
|
||||
if (dst >= end || static_cast<char>(bytes >> 24) == '\0')
|
||||
return dst;
|
||||
*dst++ = static_cast<char>(bytes >> 24);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#ifdef USE_ESP8266
|
||||
/// Safely append a PROGMEM string to buffer, returning new position (capped at size).
|
||||
/// ESP8266 internal implementation — prefer the `buf_append_str` macro which wraps
|
||||
|
||||
@@ -48,6 +48,34 @@ void HOT esp_log_printf_(int level, const char *tag, int line, const __FlashStri
|
||||
}
|
||||
#endif
|
||||
|
||||
void HOT esp_log_str_(int level, const char *tag, int line, const char *format) { // NOLINT
|
||||
#ifdef USE_LOGGER
|
||||
#ifdef ESPHOME_DEBUG
|
||||
// Before Logger::pre_setup(), the formatting path below reports the early call
|
||||
if (logger::global_logger != nullptr)
|
||||
#endif
|
||||
{
|
||||
if (logger::global_logger->log_str_(static_cast<uint8_t>(level), tag, line, format))
|
||||
return;
|
||||
}
|
||||
// Formats the line log_str_() could not copy; it has no '%', so it is its own format
|
||||
#pragma GCC diagnostic push
|
||||
#pragma GCC diagnostic ignored "-Wformat-security"
|
||||
#pragma GCC diagnostic ignored "-Wformat-nonliteral"
|
||||
esp_log_printf_(level, tag, line, format);
|
||||
#pragma GCC diagnostic pop
|
||||
#endif
|
||||
}
|
||||
|
||||
#ifdef USE_ESP8266
|
||||
void HOT esp_log_str_(int level, const char *tag, int line, const __FlashStringHelper *format) {
|
||||
#ifdef USE_LOGGER
|
||||
ESPHOME_DEBUG_ASSERT(logger::global_logger != nullptr);
|
||||
logger::global_logger->log_str_(static_cast<uint8_t>(level), tag, line, format);
|
||||
#endif
|
||||
}
|
||||
#endif
|
||||
|
||||
void HOT esp_log_vprintf_(int level, const char *tag, int line, const char *format, va_list args) { // NOLINT
|
||||
#ifdef USE_LOGGER
|
||||
#ifdef ESPHOME_DEBUG
|
||||
|
||||
+57
-14
@@ -67,6 +67,14 @@ void esp_log_printf_(int level, const char *tag, int line, const char *format, .
|
||||
// NOLINTNEXTLINE(readability-identifier-naming)
|
||||
void esp_log_printf_(int level, const char *tag, int line, const __FlashStringHelper *format, ...);
|
||||
#endif
|
||||
// esp_log_printf_ for a line with no arguments and no '%': it is copied without formatting
|
||||
void esp_log_str_(int level, const char *tag, int line, const char *format); // NOLINT
|
||||
#ifdef USE_ESP8266
|
||||
// NOLINTNEXTLINE(readability-identifier-naming)
|
||||
void esp_log_str_(int level, const char *tag, int line, const __FlashStringHelper *format);
|
||||
#endif
|
||||
// Never defined or called: the ESP_LOGx macros name it in dead code so a line without arguments has its format checked
|
||||
int esp_log_format_check_(const char *format, ...) __attribute__((format(printf, 1, 2))); // NOLINT
|
||||
void esp_log_vprintf_(int level, const char *tag, int line, const char *format, va_list args); // NOLINT
|
||||
#if defined(USE_ESP32)
|
||||
int esp_idf_log_vprintf_(const char *format, va_list args); // NOLINT
|
||||
@@ -78,9 +86,50 @@ int esp_idf_log_vprintf_(const char *format, va_list args); // NOLINT
|
||||
#define ESPHOME_LOG_FORMAT(format) format
|
||||
#endif
|
||||
|
||||
#define ESPHOME_LOG_ARGS_(level, tag, format, ...) \
|
||||
::esphome::esp_log_printf_(level, tag, __LINE__, ESPHOME_LOG_FORMAT(format), __VA_ARGS__)
|
||||
// The format check is a statement-level if (false), not sizeof: GCC 16 skips format checks in unevaluated or folded
|
||||
// expressions. __builtin_strchr folds on a literal, so only a line with '%' (such as "100%%") is formatted.
|
||||
#ifdef USE_ESP8266
|
||||
// F() only takes a literal, so using format more than once cannot repeat a side effect
|
||||
#define ESPHOME_LOG_NO_ARGS_(level, tag, format) \
|
||||
({ \
|
||||
if (false) \
|
||||
::esphome::esp_log_format_check_(format); \
|
||||
if (__builtin_strchr(format, '%') != nullptr) \
|
||||
::esphome::esp_log_printf_(level, tag, __LINE__, ESPHOME_LOG_FORMAT(format)); \
|
||||
else \
|
||||
::esphome::esp_log_str_(level, tag, __LINE__, ESPHOME_LOG_FORMAT(format)); \
|
||||
})
|
||||
#else
|
||||
#pragma GCC diagnostic push
|
||||
#pragma GCC diagnostic ignored "-Wformat-security"
|
||||
#pragma GCC diagnostic ignored "-Wformat-nonliteral"
|
||||
// A function argument, so format is evaluated once and a temporary such as std::string().c_str() outlives the call
|
||||
// NOLINTNEXTLINE(readability-identifier-naming)
|
||||
inline void __attribute__((always_inline)) esp_log_no_args_(int level, const char *tag, int line, const char *format) {
|
||||
if (__builtin_strchr(format, '%') != nullptr) {
|
||||
esp_log_printf_(level, tag, line, format);
|
||||
} else {
|
||||
esp_log_str_(level, tag, line, format);
|
||||
}
|
||||
}
|
||||
#pragma GCC diagnostic pop
|
||||
#define ESPHOME_LOG_NO_ARGS_(level, tag, format) \
|
||||
({ \
|
||||
if (false) \
|
||||
::esphome::esp_log_format_check_(format); \
|
||||
::esphome::esp_log_no_args_(level, tag, __LINE__, format); \
|
||||
})
|
||||
#endif
|
||||
// ESPHOME_LOG_NO_ARGS_ when there are no arguments after the format, ESPHOME_LOG_ARGS_ otherwise
|
||||
#define ESPHOME_LOG_PICK_(skip, name, ...) name
|
||||
#define ESPHOME_LOG_(level, tag, format, ...) \
|
||||
ESPHOME_LOG_PICK_(__VA_OPT__(, ) ESPHOME_LOG_ARGS_, ESPHOME_LOG_NO_ARGS_)(level, tag, \
|
||||
format __VA_OPT__(, ) __VA_ARGS__)
|
||||
|
||||
#if ESPHOME_LOG_LEVEL >= ESPHOME_LOG_LEVEL_VERY_VERBOSE
|
||||
#define esph_log_vv(tag, format, ...) \
|
||||
::esphome::esp_log_printf_(ESPHOME_LOG_LEVEL_VERY_VERBOSE, tag, __LINE__, ESPHOME_LOG_FORMAT(format), ##__VA_ARGS__)
|
||||
#define esph_log_vv(tag, format, ...) ESPHOME_LOG_(ESPHOME_LOG_LEVEL_VERY_VERBOSE, tag, format, __VA_ARGS__)
|
||||
|
||||
#define ESPHOME_LOG_HAS_VERY_VERBOSE
|
||||
#else
|
||||
@@ -88,8 +137,7 @@ int esp_idf_log_vprintf_(const char *format, va_list args); // NOLINT
|
||||
#endif
|
||||
|
||||
#if ESPHOME_LOG_LEVEL >= ESPHOME_LOG_LEVEL_VERBOSE
|
||||
#define esph_log_v(tag, format, ...) \
|
||||
::esphome::esp_log_printf_(ESPHOME_LOG_LEVEL_VERBOSE, tag, __LINE__, ESPHOME_LOG_FORMAT(format), ##__VA_ARGS__)
|
||||
#define esph_log_v(tag, format, ...) ESPHOME_LOG_(ESPHOME_LOG_LEVEL_VERBOSE, tag, format, __VA_ARGS__)
|
||||
|
||||
#define ESPHOME_LOG_HAS_VERBOSE
|
||||
#else
|
||||
@@ -97,10 +145,8 @@ int esp_idf_log_vprintf_(const char *format, va_list args); // NOLINT
|
||||
#endif
|
||||
|
||||
#if ESPHOME_LOG_LEVEL >= ESPHOME_LOG_LEVEL_DEBUG
|
||||
#define esph_log_d(tag, format, ...) \
|
||||
::esphome::esp_log_printf_(ESPHOME_LOG_LEVEL_DEBUG, tag, __LINE__, ESPHOME_LOG_FORMAT(format), ##__VA_ARGS__)
|
||||
#define esph_log_config(tag, format, ...) \
|
||||
::esphome::esp_log_printf_(ESPHOME_LOG_LEVEL_CONFIG, tag, __LINE__, ESPHOME_LOG_FORMAT(format), ##__VA_ARGS__)
|
||||
#define esph_log_d(tag, format, ...) ESPHOME_LOG_(ESPHOME_LOG_LEVEL_DEBUG, tag, format, __VA_ARGS__)
|
||||
#define esph_log_config(tag, format, ...) ESPHOME_LOG_(ESPHOME_LOG_LEVEL_CONFIG, tag, format, __VA_ARGS__)
|
||||
|
||||
#define ESPHOME_LOG_HAS_DEBUG
|
||||
#define ESPHOME_LOG_HAS_CONFIG
|
||||
@@ -110,8 +156,7 @@ int esp_idf_log_vprintf_(const char *format, va_list args); // NOLINT
|
||||
#endif
|
||||
|
||||
#if ESPHOME_LOG_LEVEL >= ESPHOME_LOG_LEVEL_INFO
|
||||
#define esph_log_i(tag, format, ...) \
|
||||
::esphome::esp_log_printf_(ESPHOME_LOG_LEVEL_INFO, tag, __LINE__, ESPHOME_LOG_FORMAT(format), ##__VA_ARGS__)
|
||||
#define esph_log_i(tag, format, ...) ESPHOME_LOG_(ESPHOME_LOG_LEVEL_INFO, tag, format, __VA_ARGS__)
|
||||
|
||||
#define ESPHOME_LOG_HAS_INFO
|
||||
#else
|
||||
@@ -119,8 +164,7 @@ int esp_idf_log_vprintf_(const char *format, va_list args); // NOLINT
|
||||
#endif
|
||||
|
||||
#if ESPHOME_LOG_LEVEL >= ESPHOME_LOG_LEVEL_WARN
|
||||
#define esph_log_w(tag, format, ...) \
|
||||
::esphome::esp_log_printf_(ESPHOME_LOG_LEVEL_WARN, tag, __LINE__, ESPHOME_LOG_FORMAT(format), ##__VA_ARGS__)
|
||||
#define esph_log_w(tag, format, ...) ESPHOME_LOG_(ESPHOME_LOG_LEVEL_WARN, tag, format, __VA_ARGS__)
|
||||
|
||||
#define ESPHOME_LOG_HAS_WARN
|
||||
#else
|
||||
@@ -128,8 +172,7 @@ int esp_idf_log_vprintf_(const char *format, va_list args); // NOLINT
|
||||
#endif
|
||||
|
||||
#if ESPHOME_LOG_LEVEL >= ESPHOME_LOG_LEVEL_ERROR
|
||||
#define esph_log_e(tag, format, ...) \
|
||||
::esphome::esp_log_printf_(ESPHOME_LOG_LEVEL_ERROR, tag, __LINE__, ESPHOME_LOG_FORMAT(format), ##__VA_ARGS__)
|
||||
#define esph_log_e(tag, format, ...) ESPHOME_LOG_(ESPHOME_LOG_LEVEL_ERROR, tag, format, __VA_ARGS__)
|
||||
|
||||
#define ESPHOME_LOG_HAS_ERROR
|
||||
#else
|
||||
|
||||
@@ -222,6 +222,20 @@ def test_a_resolved_unit_waits_for_the_full_config() -> None:
|
||||
assert config[CONF_VISUAL][CONF_MIN_TEMPERATURE] == pytest.approx(59.0)
|
||||
|
||||
|
||||
def test_final_validate_skips_a_platform_that_only_holds_nested_climates() -> None:
|
||||
"""A hub platform may put each of its climates under a key of one platform entry."""
|
||||
nested = climate_schema(climate_ns.class_("NestedClimate", Climate))
|
||||
platform_schema = cv.Schema({cv.Optional("zone_1"): nested})
|
||||
config = platform_schema(
|
||||
{"zone_1": {CONF_NAME: "Zone 1", CONF_VISUAL: {CONF_MIN_TEMPERATURE: "50°F"}}}
|
||||
)
|
||||
# The platform entry itself is not a climate and has no visual section
|
||||
assert CONF_VISUAL not in config
|
||||
_final_validate([config])
|
||||
# The nested climate was converted by its own schema
|
||||
assert config["zone_1"][CONF_VISUAL][CONF_MIN_TEMPERATURE] == pytest.approx(10.0)
|
||||
|
||||
|
||||
@pytest.mark.parametrize(
|
||||
("use_fahrenheit", "cn105_lines"),
|
||||
[
|
||||
|
||||
@@ -8,11 +8,16 @@ from esphome.components.npm1300 import (
|
||||
CHARGER_SCHEMA,
|
||||
NPM1300_UNSET,
|
||||
OUTPUT_VOLTAGE,
|
||||
THERMISTOR_NONE,
|
||||
VBUS_CURRENT_LIMIT,
|
||||
_charger_config,
|
||||
_check_thermistor,
|
||||
_config,
|
||||
_output_config,
|
||||
_termination_voltage_code,
|
||||
_thermistor_resistance,
|
||||
_vbus_current_limit_code,
|
||||
thermistor_beta,
|
||||
)
|
||||
from esphome.const import CONF_MODE, CONF_VOLTAGE
|
||||
from esphome.cpp_generator import StructInitializer
|
||||
@@ -54,10 +59,35 @@ def test_termination_voltage_gap_rejected(voltage: str) -> None:
|
||||
CHARGER_SCHEMA({"charge_current": "100mA", "termination_voltage": voltage})
|
||||
|
||||
|
||||
def test_warm_termination_defaults_to_termination() -> None:
|
||||
def test_warm_termination_defaults_to_reset_value() -> None:
|
||||
fields = _charger({"charge_current": "100mA", "termination_voltage": "4.1V"})
|
||||
assert fields["termination_voltage"] == "6"
|
||||
assert fields["warm_termination_voltage"] == "6"
|
||||
assert fields["warm_termination_voltage"] == "2"
|
||||
|
||||
|
||||
@pytest.mark.parametrize(
|
||||
("config", "warns"),
|
||||
[
|
||||
({"termination_voltage": "4.2V"}, True),
|
||||
({"termination_voltage": "3.5V"}, False),
|
||||
({"termination_voltage": "3.6V"}, False),
|
||||
({}, False),
|
||||
({"termination_voltage": "4.2V", "warm_termination_voltage": "4.0V"}, False),
|
||||
],
|
||||
)
|
||||
def test_warm_termination_default_warning(
|
||||
config: ConfigType, warns: bool, caplog: pytest.LogCaptureFixture
|
||||
) -> None:
|
||||
CHARGER_SCHEMA(config)
|
||||
assert ("warm_termination_voltage" in caplog.text) is warns
|
||||
|
||||
|
||||
@pytest.mark.parametrize(("termination", "code"), [("3.5V", "0"), ("3.55V", "1")])
|
||||
def test_warm_termination_default_not_above_termination(
|
||||
termination: str, code: str
|
||||
) -> None:
|
||||
fields = _charger({"termination_voltage": termination})
|
||||
assert fields["warm_termination_voltage"] == code
|
||||
|
||||
|
||||
def test_warm_termination_code() -> None:
|
||||
@@ -112,3 +142,66 @@ def test_vbus_current_limit_code(limit: str, code: int) -> None:
|
||||
def test_vbus_current_limit_rejected(limit: str) -> None:
|
||||
with pytest.raises(cv.Invalid):
|
||||
VBUS_CURRENT_LIMIT(limit)
|
||||
|
||||
|
||||
def test_charger_needs_thermistor() -> None:
|
||||
with pytest.raises(cv.Invalid, match="thermistor_resistance"):
|
||||
_check_thermistor({"charger": {}})
|
||||
config = {"charger": {}, "thermistor_resistance": THERMISTOR_NONE}
|
||||
assert _check_thermistor(config) is config
|
||||
|
||||
|
||||
@pytest.mark.parametrize("resistance", [None, THERMISTOR_NONE])
|
||||
def test_thermistor_beta_needs_resistance(resistance: str | None) -> None:
|
||||
config = {"thermistor_beta": 3380}
|
||||
if resistance is not None:
|
||||
config["thermistor_resistance"] = resistance
|
||||
with pytest.raises(cv.Invalid, match="thermistor_beta"):
|
||||
_check_thermistor(config)
|
||||
|
||||
|
||||
@pytest.mark.parametrize(
|
||||
("resistance", "beta"), [(10000.0, 3380), (47000.0, 4050), (100000.0, 4250)]
|
||||
)
|
||||
def test_thermistor_beta_default(resistance: float, beta: int) -> None:
|
||||
assert thermistor_beta({"thermistor_resistance": resistance}) == beta
|
||||
|
||||
|
||||
def test_thermistor_beta_configured() -> None:
|
||||
config = {"thermistor_resistance": 47000.0, "thermistor_beta": 3950}
|
||||
assert thermistor_beta(config) == 3950
|
||||
|
||||
|
||||
@pytest.mark.parametrize(
|
||||
("resistance", "code", "beta"),
|
||||
[
|
||||
(None, str(NPM1300_UNSET), "0"),
|
||||
(THERMISTOR_NONE, "0", "0"),
|
||||
(100000.0, "3", "4250"),
|
||||
],
|
||||
)
|
||||
def test_config_thermistor(
|
||||
resistance: str | float | None, code: str, beta: str
|
||||
) -> None:
|
||||
config = {} if resistance is None else {"thermistor_resistance": resistance}
|
||||
fields = _fields(_config(config))
|
||||
assert fields["thermistor"] == code
|
||||
assert fields["thermistor_beta"] == beta
|
||||
|
||||
|
||||
@pytest.mark.parametrize(
|
||||
("value", "result"),
|
||||
[
|
||||
("10kOhm", 10000.0),
|
||||
("0.047MOhm", 47000.0),
|
||||
("100000", 100000.0),
|
||||
("None", "none"),
|
||||
],
|
||||
)
|
||||
def test_thermistor_resistance(value: str, result: float | str) -> None:
|
||||
assert _thermistor_resistance(value) == result
|
||||
|
||||
|
||||
def test_thermistor_resistance_rejected() -> None:
|
||||
with pytest.raises(cv.Invalid, match="10kOhm, 47kOhm, 100kOhm or none"):
|
||||
_thermistor_resistance("22kOhm")
|
||||
@@ -0,0 +1,39 @@
|
||||
esphome:
|
||||
name: test
|
||||
|
||||
esp8266:
|
||||
board: esp01_1m
|
||||
|
||||
button:
|
||||
# Declared before the selects, so the action is generated before them
|
||||
- platform: template
|
||||
id: early
|
||||
name: Early
|
||||
on_press:
|
||||
- select.set:
|
||||
id: fan
|
||||
option: high
|
||||
- select.set:
|
||||
id: fan
|
||||
option: turbo
|
||||
- select.set:
|
||||
id: fan
|
||||
option: !lambda return "low";
|
||||
- select.set:
|
||||
id: fan_copy
|
||||
option: medium
|
||||
|
||||
select:
|
||||
- platform: template
|
||||
id: fan
|
||||
name: Fan
|
||||
optimistic: true
|
||||
options: [low, medium, high]
|
||||
on_value:
|
||||
- select.set:
|
||||
id: fan
|
||||
option: medium
|
||||
- platform: copy
|
||||
id: fan_copy
|
||||
name: Fan Copy
|
||||
source_id: fan
|
||||
@@ -0,0 +1,35 @@
|
||||
---
|
||||
esphome:
|
||||
name: toshiba-test
|
||||
|
||||
esp32:
|
||||
board: esp32dev
|
||||
|
||||
remote_transmitter:
|
||||
pin: GPIO4
|
||||
carrier_duty_percent: 50%
|
||||
non_blocking: true
|
||||
|
||||
climate:
|
||||
- platform: toshiba
|
||||
id: ras
|
||||
name: RAS
|
||||
model: RAS-25J2KVSG-ND
|
||||
- platform: toshiba
|
||||
id: generic
|
||||
name: Generic
|
||||
|
||||
select:
|
||||
- platform: toshiba
|
||||
toshiba_id: ras
|
||||
power_selection:
|
||||
id: power
|
||||
name: Power selection
|
||||
|
||||
button:
|
||||
- platform: template
|
||||
name: Set
|
||||
on_press:
|
||||
- select.set:
|
||||
id: power
|
||||
option: Silent 1
|
||||
@@ -0,0 +1,47 @@
|
||||
esphome:
|
||||
name: test
|
||||
|
||||
esp32:
|
||||
board: esp32dev
|
||||
framework:
|
||||
type: esp-idf
|
||||
|
||||
spi:
|
||||
- id: spi_bus
|
||||
clk_pin: GPIO18
|
||||
mosi_pin: GPIO23
|
||||
|
||||
display:
|
||||
- platform: mipi_spi
|
||||
spi_id: spi_bus
|
||||
model: st7789v
|
||||
id: tft_display
|
||||
dimensions:
|
||||
width: 240
|
||||
height: 320
|
||||
cs_pin: GPIO22
|
||||
dc_pin: GPIO21
|
||||
auto_clear_enabled: false
|
||||
invert_colors: false
|
||||
update_interval: never
|
||||
|
||||
lvgl:
|
||||
displays: tft_display
|
||||
widgets:
|
||||
- dropdown:
|
||||
id: dropdown_widget
|
||||
options: [low, high]
|
||||
|
||||
select:
|
||||
- platform: lvgl
|
||||
id: lv_fan
|
||||
name: LVGL Fan
|
||||
widget: dropdown_widget
|
||||
|
||||
button:
|
||||
- platform: template
|
||||
name: Set
|
||||
on_press:
|
||||
- select.set:
|
||||
id: lv_fan
|
||||
option: high
|
||||
Loaded 100 of 115 files, more files were not shown because too many files have changed in this diff.
Show more
Reference in new issue
Block a user