Compare commits

..
Author SHA1 Message Date
J. Nick Koston 1edffa1813 Build the fusb302b and shelly_dimmer frames with a pointer instead of an index 2026-11-15 17:36:52 -06:00
Mark Mayo 07bbad4061 [esp32] Import the SBOM writer only when generating the build (#22702) 2026-11-15 16:50:14 -06:00
Mark Mayo 1cd99ce6d6 [core] Import url2pathname only where a file URL is converted (#22703) 2026-11-15 16:50:04 -06:00
Mark Mayo 91db9d86d0 [debug] Import the nrf52 helper only when building for Zephyr (#22701) 2026-11-15 16:42:23 -06:00
Tanguy Pruvot 2f900f23d7 [cst3240] Add the Hynitron CST3240 touchscreen (#22601) 2026-11-15 12:20:54 -06:00
373224c141 [npm1300] Add the Nordic nPM1300 PMIC component (#22661)
Co-authored-by: Claude <noreply@anthropic.com>
Co-authored-by: J. Nick Koston <nick@home-assistant.io>
2026-11-15 12:52:15 +01:00
Hajo Noerenberg 1dff22f578 [cc1101] Add long packet support, plus a lambda packet_length for unknown-length packets (#17578) 2026-11-14 21:43:21 -06:00
Vinicius FortunaandClyde Stubbs c2eb540b37 [sx126x] Refactor/sx126x radio lifecycle (#22197)
Co-authored-by: Clyde Stubbs <2366188+clydebarrow@users.noreply.github.com>
2026-11-15 13:17:23 +11:00
J. Nick Kostonandclydebarrow fad21e3f14 [cstxxx] Share the Hynitron 16-bit register touchscreen code (#22718)
Co-authored-by: clydebarrow <2366188+clydebarrow@users.noreply.github.com>
2026-11-15 02:11:09 +00:00
Rodrigoah 904f0df920 [whirlpool] Add the Hitachi HCRA31NEWH model (#22704) 2026-11-14 14:23:51 -06:00
Chris 7128d22f07 [ethernet] Notify IP state listeners from the main loop (#22657) 2026-11-14 13:22:11 -06:00
157 changed files with 6365 additions and 5337 deletions

No files matched your search

+4 -30
View File
@@ -47,15 +47,6 @@ driven, what the logs show, and the same flow on a `dev` build when a quirk coul
piped, or lines land minutes late. Bound every long command with `timeout` (coreutils) or
`perl -e 'alarm shift; exec @ARGV' <seconds> <command>` on macOS without it. Save an exit code with `rc=$?`
right after the command: in `echo "$(date +%T) rc=$?"` the command substitution runs first and `$?` reads 0.
8. **Give builds a private PlatformIO directory when sessions may use different Pythons.** ESPHome stamps the
PlatformIO core directory with the Python version that last built, and a build from another version wipes and
reinstalls the shared packages under every other build. Set `PLATFORMIO_CORE_DIR=<scratch>/pio` on every
compile and upload (`scripts/pio_build.sh` does). Old releases and framework A/B builds need one too
(see `reference/boards.md`).
9. **One ESP-IDF or LibreTiny build at a time on a shared machine.** Concurrent builds race over tool caches. Wrap
compiles in a lock (`BUILD_LOCK` for `scripts/pio_build.sh`, for example a script that holds a `mkdir` lock).
10. **Check that the stimulus ran.** A load or soak run where most requests failed was not loaded, and a capture
that stopped early covers nothing; check the driver's own counters before reading the device's verdict.
## Workflow
@@ -90,17 +81,7 @@ driven, what the logs show, and the same flow on a `dev` build when a quirk coul
> Automated testing results
```
Then one short sentence saying what was tested (builds and SHAs, board, what was driven), and on the next line a
bold result, one of **Test result: PASS**, **Test result: FAIL**, **Test result: PASS WITH CAVEATS** or
**Test result: UNCLEAR**, followed by one sentence with the key numbers (for example "the device closed none of 32
streams, dev closed 12 of 16"). FAIL means a regression or a fix that does not work. PASS WITH CAVEATS means it works
but something about the change itself needs a look; name the caveat in that sentence. A limit of the test (the
original crash did not reproduce) or a device or network quirk the tooling reported correctly is not a caveat on
the change; say it as a separate note. UNCLEAR means the run could not decide; say what is
missing. A reader must not have to dig through the table or guess. Side findings that do not change the result (a
bug already on `dev`, a one-off oddity) go in the bullets, marked as separate from the result.
Then a results table
Then one short sentence saying what was tested (builds and SHAs, board, what was driven), a results table
(build SHA, board and framework, what was driven, counts, result, plus the `dev` control row), short bullets
for the findings, and the setup and exact flow in a collapsed `<details>` block. Answer any question asked on
the PR directly in the comment. No credentials, SSIDs, IP addresses, port names or other bench details.
@@ -137,13 +118,8 @@ ESP-IDF OTA image: `.esphome/build/<name>/build/firmware.ota.bin`. LibreTiny OTA
and then; retry up to three times.
- `api_button.py HOST KEY list|press NAME [...]`: list a device's template buttons or press them over the API; the
recipes drive their test configs with it.
- `apiconn.py`: `connect(host, tries, port, on_stop)` (API connect and login with retries, `API_KEY` from the env),
`log_text()` (an API log line without ANSI codes), `stop_event()` (an Event set when the connection ends),
`ble_connect()` (wait for an advert, connect through a proxy without a cache like Home Assistant) and
`enable_notify()` (start notify and write the CCCD), imported by the API drivers.
- `pio_build.sh compile|upload NAME YAML [DEVICE] [esphome options]`: build or upload YAML as `-s name NAME` from
`TREE` with a private PlatformIO directory, an import check and an optional `BUILD_LOCK`; logs to `NAME-*.log`.
- `resolve.sh NAME [TRIES]`: the address of `NAME.local` once it resolves and answers a ping.
- `apiconn.py`: `connect(host, tries)` (API connect and login with retries, `API_KEY` from the env) and `log_text()`
(an API log line without ANSI codes), imported by the newer API drivers.
- `wifisave_hammer.sh KIND DUT_PORT DUT_YAML DUT_TREE CLIENT_PORT CLIENT_YAML CLIENT_TREE [SETTLE]`:
the fallback AP plus captive portal hammer end to end (compile, flash, capture, counts) with the
templates below. KIND is `esp`, `pico`, `bw15` or `bk`. It ends with `RESULT PASS` (exit 0),
@@ -158,7 +134,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), `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 runner), `nrf52_tcp_probe/` (TCP and UDP socket probe config, build and log check, API check over Thread), `rs485_frame/` (simulated DLE/STX/ETX bus device, frame and Line 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), `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 runner), `nrf52_tcp_probe/` (TCP and UDP socket probe config, build and log check, API check over Thread), `rs485_frame/` (simulated DLE/STX/ETX bus device, frame and Line truncated
## Templates in `$SKILL/templates`
@@ -167,8 +143,6 @@ capture decoder, RTL8720C ROM dump versus report matcher), `api_outgoing/` (wake
- `client-wifisave.yaml` (package): joins that AP, posts N bogus `/wifisave` requests, then the real
credentials from `!secret`. `example-client.yaml` is the per-run file. `bogus_saves: "1"` gives a
plain provisioning check.
- `diagnostics.yaml` (package): DEBUG logs, esphome OTA, uptime, free heap, reset reason and a 200 ms counter
for soak and load tests.
- Per-topic device configs, each described in `reference/recipes/<component>.md`: `online-image-dut.yaml`,
`usb-cdc-acm-dongle-dut.yaml`, `nrf52-preferences-dut.yaml`, `rtsp-waveshare-s3-eth-dut.yaml`,
`waveshare-s3-eth-camdiag.yaml`, `s31-cse7766-dut.yaml`, `nrf52-coredump-crash-dut.yaml`,
@@ -29,9 +29,6 @@ Ports are not listed on purpose: they follow the USB hub port and change on ever
interrupt handler; `Software Watchdog` with `pm_send_nullfunc` means the loop was busy too long. Code that takes
interrupt locks needs a load run to show either (`scripts/esp8266_lock_load/`, see recipes/one_wire.md).
- `GPI` bits 16 to 31 are strapping bits (mask with 0xFFFF). GPIO16 has no interrupt; read `GP16O` for an output.
- Cover positions and other restored entity state live in RTC memory by default, so they survive `erase_flash`
and a dev build's state carries into the next build's first boot. Set `esp8266: restore_from_flash: true` on A/B
configs that erase between builds.
## ESP32 classic devkit
@@ -53,8 +50,6 @@ Ports are not listed on purpose: they follow the USB hub port and change on ever
- An ESP32 whose bootloader has no app rollback is protected only by `safe_mode` after a bad OTA. Before sending a
change to such a device that nobody can reach by hand, boot the same code on a devkit first and keep a fallback
image of the merge base built.
- On modules where GPIO16 and GPIO17 are taken by the module itself (PSRAM on WROVER modules, for example), a
`uart:` bus on those pins boot-loops with a watchdog reset during setup. Use GPIO21 and GPIO22.
## ESP32-S3, C3, C6, H2 with native USB
@@ -77,25 +72,6 @@ Ports are not listed on purpose: they follow the USB hub port and change on ever
- A board left in the ROM bootloader after an upload (BOOT held, or the USB JTAG reset landing in download mode
again) starts its app without a replug through `esptool --port <port> --after watchdog-reset chip-id`; flash a
minimal Wi-Fi plus API image first when recovering from a crash-looping one.
- On macOS a software reboot of an ESP32-C3 does not drop its USB JTAG serial port: pyserial keeps reading and gets
no error, so a client that watches for a port reset misses it. Count ESPHome's `Running through setup()` line
instead.
- W5500 SPI Ethernet with IPv6 on (seen on an ESP32-S3, #22676): every `safe_reboot()` (OTA, restart button)
aborts while an API client is subscribed to logs. `mdns_free()` leaves the IPv6 group inside `tcpip_api_call`, the
W5500 driver logs "IPv6 multicast is always filtered in by W5500", the logger sends that line to the API inline,
and the nested `LwIPLock` in `setsockopt(TCP_NODELAY)` releases the core lock it did not take, so `udp_recv`
asserts. No crash with IPv6 off, with no log subscriber (a connected client that does not subscribe is fine), or
on RMII Ethernet. Reading the crash record afterwards: `recipes/crash_report.md`.
- `usb_serial_jtag` holds a `NO_LIGHT_SLEEP` power lock by itself, so a light sleep soak on a native USB board logs
to UART0 instead (see `recipes/light_sleep_gpio_wake.md`).
- The Waveshare ESP32-P4-WIFI6 needs `engineering_sample: true`. Its Wi-Fi is an ESP32-C6 over SDIO through
`esp32_hosted`; a test with no network logs over UART0 to the CH343 console.
## ESP32-C2 (ESP8684, 26 MHz crystal)
- Boards with a 26 MHz crystal print the ROM banner (`ESP-ROM:esp8684`) at 74880 baud; at 115200 it is garbage.
- ESP-IDF builds need `esp32: framework: sdkconfig_options: {CONFIG_XTAL_FREQ_26: y}`, or the app console runs at
the wrong baud rate (the upload log warns `Crystal frequency mismatch`).
## RP2040 / RP2350 (Pico W, Pico 2 W)
@@ -123,22 +99,6 @@ Ports are not listed on purpose: they follow the USB hub port and change on ever
each RD_REQ with one byte from a 256-byte register file, so any read length works (used for BME280/BMP280 in
esphome#22236). The ESP32-S3 I2C slave v2 driver is not usable for register emulation: answering from a task or
straight from the interrupt both gave shifted multi-byte reads.
- RP2350 PIO instruction memory survives a reflash (the 1200 baud touch and the UF2 reboot), so a program loaded by
the previous firmware can hide a missing `pio_add_program`. Clear it from an `on_boot` priority 1000 lambda
(`pio1->instr_mem[i] = 0` for i < 32) to start from power-on state; `scripts/pio_probe/pio-common.yaml` does.
- `RP2Preferences::reset()` erases the flash sector but not the RAM copy the preferences load from, so the LibreTiny
`on_boot` wipe-once pattern still boots with the old saved Wi-Fi credentials. Use a fallback AP config with
`networks:` and a fresh `name:` per build instead.
- WIZnet W5100S-EVB-Pico (RP2040, `rp2: board: wiznet_5100s_evb_pico`): W5100S on SPI with CLK 18, MOSI 19, MISO 16,
CS 17, INT 21, RST 20 (`ethernet: type: W5100`). ESPHome firmware gives it no USB CDC port; reach it over Ethernet,
OTA by address.
- Through a Pico W `bluetooth_proxy`, about 1 to 2 in 5 BLE connects fail with 0x3E (`connect err=62` in the
client) on every build; retry.
- arduino-pico framework bumps and the RPAsyncTCP SSE stall: see `recipes/arduino_pico.md`.
- USB CDC sends nothing until the host asserts DTR; a client that opens with DTR low (`improvcli.py` without
`--keep-dtr`) sees silence.
- Powering a breakout from the Pico: use 3V3(OUT), pin 36. Pin 37 is 3V3_EN, which switches the Pico off when pulled
low.
## BW15 kit (RTL8720CF, LibreTiny rtl87xx)
@@ -180,12 +140,6 @@ Ports are not listed on purpose: they follow the USB hub port and change on ever
- Wrong-MD5 OTA test without changing firmware: monkeypatch `espota2.hashlib.md5` so only the image checksum (data
over 64 KB) returns a wrong hexdigest. Give each build its own `name:` and read `device_info().name` after a
restart to see which image booted.
- Logger UART: `DEFAULT` is UART2 (the kit's USB serial). `hardware_uart: UART1` hangs after the boot banner and
never joins Wi-Fi, on dev too. `UART0` moves both ESPHome and `lt_log` output off the kit's port. LibreTiny's own
`LT_I` lines need `framework: loglevel: info` (the default is warn).
- A config with no Wi-Fi network and no `ap:` resets about 10 s into setup when it has `web_server:` (dev too):
`web_server` setup blocks with no interface up, and no crash record is left. VERBOSE logs show the last
`Setup <component>` line.
## BK7238, WB3S, CB3S (BK7231N) (LibreTiny bk72xx, CH340, RTS on CEN)
@@ -311,7 +265,6 @@ Ports are not listed on purpose: they follow the USB hub port and change on ever
partition even when the upgrade state is still `UPG_STATE_DOWNLOAD_ING` (esphome#22111).
- Without serial access to recover, never send a corrupted image; wrong-MD5 and cut-upload tests with valid
images are safe.
- On the WL2H-U `logger: hardware_uart: DEFAULT` resolves to UART1, LibreTiny's log port for that board.
## nRF52840
@@ -341,10 +294,6 @@ Ports are not listed on purpose: they follow the USB hub port and change on ever
the port open, and the logger warns `logger took a long time`. Print one line per `loop()` instead.
- Zephyr settings survive a reflash: key a `restore_value` global by a `run` substitution when a test needs
a never-saved value.
- `2fe3:0100` is Zephyr's default USB VID:PID, so `ports.py` labels any board running a Zephyr ESPHome build as an
nRF52840 Dongle. Tell an ItsyBitsy from a PCA10059 with a 1200 baud touch: the ItsyBitsy shows its `ITSY840BOOT`
UF2 volume, the PCA10059 its Nordic DFU port.
- nRF52 Zephyr builds use newlib; the nRF54LM20 builds use picolibc.
- Thread: nRF52 `openthread:` accepts only `tlv:`; the individual keys (network_key, channel, ...) are rejected at
validation. To list nearby networks with channel and PAN ID, call
`otThreadDiscover(InstanceLock::acquire().get_instance(), 0, 0xFFFF, false, false, cb, nullptr)` from an
@@ -398,16 +347,6 @@ Ports are not listed on purpose: they follow the USB hub port and change on ever
- BDK headers such as `power_save_pub.h` have no `extern "C"`; wrap them when including them from the Arduino
core's C++ files.
## Shared PlatformIO directory
- The stamp file is `.esphome.pio.stamp.json`; a mismatch deletes `packages`, `platforms` and `.cache`.
- Building an old release (2025.7.0, Arduino ESP32) against the shared directory swapped `tool-scons` for an older
version and broke it midway (`No module named 'SCons.SConf'`) for every PlatformIO build until a newer build
reinstalled it. Build old releases with their own `PLATFORMIO_CORE_DIR`.
- Two arduino-pico framework versions share the plain `framework-arduinopico` package directory, so an A/B swaps the
framework on every build; give each side its own `PLATFORMIO_CORE_DIR`.
- The general rule (a private directory whenever sessions may use different Pythons) is ground rule 8 in `SKILL.md`.
## LibreTiny crash and abort() facts (esphome#21596, #22288)
- `abort()` on dev: the BK7238 hangs for good (the main loop stops and the watchdog never fires once Wi-Fi is up;
@@ -487,27 +426,3 @@ Ports are not listed on purpose: they follow the USB hub port and change on ever
- LED0 P1.22, LED1 P1.25; BUTTON0 P1.26, BUTTON1 P1.09, BUTTON2 P1.08, BUTTON3 P0.05 (active low, pull-up). GPIO
P1 registers at 0x500D8200 (IN +0x00C, PIN_CNF +0x080), P0 at 0x5010A200.
- Binary sensor state changes are not logged at DEBUG; log the states or count `on_press` to see button presses.
- FICR INFO.PART reads 0x054BC20B; the build target is `nrf54lm20dk`.
- Zephyr `micros()` on nRF has 30.5 us resolution. Time short blocks with the DWT cycle counter instead; nRF54L idle
clears `DWT_CTRL`, so enable it again before each timed block.
- The nrf52 logger defaults to USB_CDC (the chip's own USB); set `logger: hardware_uart: UART0` to get the log on the
J-Link VCOM port.
- I2C (#22642): TWIM21 only takes pins on P1 or P3; P0 pins are rejected at config. BUTTON1 (P1.09) and BUTTON2
(P1.08) pads work as SDA and SCL. A bus with no pull-ups times out (-116) on every scan address, so setup takes
about 56 s. The JLinkExe `st` command reads VTref to check the I/O voltage.
## Seeed XIAO nRF54LM20A
- CMSIS-DAP probe (`2886:0068`) with one serial port that carries the console (uart20 on P1.11 and P1.10, #22599).
It ships with a Seeed demo.
- Flash with `esphome run` (pyOCD, MCUboot plus the signed app). A plain `esphome upload` flashed only the bare app
on #22599, and the board stayed dark.
- Reset: `<sdk-nrf python> -m pyocd reset -t nrf54lm20a -u <probe id>`.
## I2C and SPI breakouts
- GPIO switches on an I2C expander (XL9535 and others) write `value_or(false)` at setup even with
`restore_mode: DISABLED`, so test pin retention on a pin no switch uses.
- ADS1015: `sample_rate: 8` maps to 128 SPS at `12_BITS`.
- A bus held low (`Performing bus recovery`, no devices found) on a new breakout can be a bad header joint; reflow
it before blaming the driver.
@@ -4,7 +4,7 @@ Each flow names the pull requests it was used on, so their result tables show wh
One file per component, named after it:
- [`api.md`](api.md): noise handshake and OTA encryption on the ESP8266, outgoing connection wake timing on deep sleep and after a Wi-Fi drop, user defined action listing and execution with a raw plaintext client, store_yaml recovery over Noise or plaintext, rapid connect/disconnect stress (`connstress.py`)
- [`api.md`](api.md): noise handshake and OTA encryption on the ESP8266, outgoing connection wake timing on deep sleep and after a Wi-Fi drop, user defined action listing and execution with a raw plaintext client, store_yaml recovery over Noise or plaintext
- [`ble_midi.md`](ble_midi.md): BLE MIDI client against a fake ESP32 instrument, every packet shape and the send actions
- [`captive_portal.md`](captive_portal.md): fallback AP and /wifisave hammer
- [`cse7766.md`](cse7766.md): Sonoff S31 parser check over the API
@@ -37,7 +37,7 @@ One file per component, named after it:
- [`dfrobot_sen0395.md`](dfrobot_sen0395.md): SEN0395 mmWave command flow, module quirks and overlong lines through its error reply
- [`speaker.md`](speaker.md): media player pipeline tasks on the Connect AUX-2 with the `speaker` platform player
- [`web_server.md`](web_server.md): state SSE stress and the HTTPD task stack on web_server_idf, entity icon and device class readback
- [`uart_sim.md`](uart_sim.md): UART sensors simulated on a classic ESP32 with loopback and cross-wired UARTs, a Duco ventilation box model on a second UART
- [`uart_sim.md`](uart_sim.md): UART sensors simulated on a classic ESP32 with loopback and cross-wired UARTs
- [`gpio_expander.md`](gpio_expander.md): interrupt driven expander inputs, loop idle checks, testing a device out of reach
- [`led_strip.md`](led_strip.md): forcing a failed strip allocation, RMT symbols per chip, loop cost per strip
- [`remote_base.md`](remote_base.md): remote_receiver protocol decoding through a one-pin loopback on ESP32 and ESP8266, no IR hardware
@@ -63,11 +63,11 @@ One file per component, named after it:
- [`rs485_frame.md`](rs485_frame.md): DLE/STX/ETX bus on an ESP32-C3 with a cross-wired simulated bus device (frame cases, gate timing)
- [`hx711.md`](hx711.md): fake HX711 driven from the other core, every published value matched to the served conversion
- [`automation_trace.md`](automation_trace.md): automation codegen A/B, a marker trace of chains, conditions, loops and script modes over the API
- [`bluetooth_proxy.md`](bluetooth_proxy.md): held GATT link after a half-open API session, frozen Home Assistant stand-in, out-of-order GATT discovery responses injected on the proxy, GATT UUID forms (16 bit, 32-bit base, 128 bit) compared against an ESP32 proxy
- [`bluetooth_proxy.md`](bluetooth_proxy.md): held GATT link after a half-open API session, frozen Home Assistant stand-in, out-of-order GATT discovery responses injected on the proxy
- [`crash_report.md`](crash_report.md): crash buttons, fetching the previous boot's crash report over the API, per-chip abort facts
- [`https_idf.md`](https_idf.md): web_server_idf HTTPS test component, listener switch, stalled TLS clients, bad key
- [`mdns.md`](mdns.md): legacy unicast mDNS dump, hammer and latency straight to the device
- [`sim_buses.md`](sim_buses.md): PCF8574/HD44780, MAX7219, SPI spy, packet_interface and SN74HC165 simulators, status LED probe, fake Home Assistant, cg.static_buffer probes, a TRF7962A NFC reader chip behind a fake SPI bus
- [`sim_buses.md`](sim_buses.md): PCF8574/HD44780, MAX7219, SPI spy, packet_interface and SN74HC165 simulators, status LED probe, fake Home Assistant, cg.static_buffer probes
- [`sigma_delta.md`](sigma_delta.md): pulse density of the sigma_delta speaker pin read inside the ESP32-P4, rtttl note onset and release
- [`microphone_http_stream.md`](microphone_http_stream.md): raw PCM over HTTP with a counter fake mic or a USB mic, multi-reader client
- [`i2s_audio.md`](i2s_audio.md): I2S microphone fed by an I2S TX channel on the same S3 through three jumpers
@@ -83,6 +83,3 @@ One file per component, named after it:
- [`heap_alloc.md`](heap_alloc.md): operator new counting, and a ring of allocation callers decoded with addr2line to tell the first run after boot from repeats
- [`wled.md`](wled.md): WLED realtime sender on a board's own access point
- [`combination.md`](combination.md): combination sensor NaN behaviour from the last source
- [`improv.md`](improv.md): Improv serial with stray bytes, scan, bad and good passwords from a client that reads frames out of the log stream, Improv over BLE, per board notes
- [`arduino_pico.md`](arduino_pico.md): arduino-pico framework bump A/B: W5100S Ethernet soak, Pico W fallback AP client count, Pico W bluetooth_proxy GATT hold, web_server SSE stalls and `/events` churn crashes
- [`bluedroid_gattc_queue.md`](bluedroid_gattc_queue.md): queued MTU request and execute write recursion that overflows BTU_TASK, ble_client central against an ESP32-S3 peer
@@ -62,19 +62,3 @@ Results: https://github.com/esphome/esphome/pull/16445#issuecomment-6335435397
An API client that connects allocates about 15 times on ESP32: the connection object, then the list entities and
initial state batches growing the `DeferredBatch` vector. A log subscription adds more per line. Connect before a
heap measurement and subscribe to logs only afterwards (`heap_alloc.md`).
## Native API connect/disconnect stress (#14679, #14687, #14706)
`scripts/api/connstress.py HOST [ITERATIONS] [CONCURRENCY]` (aioesphomeapi; `API_KEY` env for a noise key) opens and
closes native API connections as fast as it can, 500 attempts with 4 at a time by default, and ends with
`RESULT PASS` or `RESULT FAIL`. It is the test that found the raw lwIP socket heap corruption, so run it for any change to
the socket layer, the scheduler or locking on RP2040/RP2350 and ESP8266, with a bounded serial capture on the device:
the client only sees resets, the crash is in the log.
- RP2040/RP2350 before #14687: crashed after 3 or 4 iterations (`malloc` from the lwIP accept IRQ while the main loop
was in `malloc`: double free in `~APIFrameHelper`, `_malloc_r` in `APIConnection()`); after: 500/500. #14679 first
added the lwIP locking between the lwIP callbacks and the main loop on RP2040.
- ESP8266 before #14706: crashed within about 50 iterations (`umm_malloc_core` LoadStoreError, use after free of a
closed PCB's callbacks); after: 490/500, the 10 failures are connection resets from overload, which is expected there.
- ESP32 (BSD sockets) passes 500/500 on both and is the control.
- Compare against dev on the same board; count reboots and decode crash addresses as well as the client's failures.
@@ -1,30 +0,0 @@
# arduino-pico framework bumps (RP2040/RP2350)
Scripts: `scripts/rp2_web_ble/` (README there). Used for #22671 (6.1.1 to 6.3.2).
- Build PR and dev with a private `PLATFORMIO_CORE_DIR` (`scripts/pio_build.sh` does; see `reference/boards.md`).
Check the compile log line `framework-arduinopico @ 1.60302.0` per build.
- Wired Ethernet (W5100S EVB): 6.3.2 defines `LWIP_ASSERT_CORE_LOCKED()` as `async_context_lock_check` on the
Ethernet context (`cores/rp2040/lwip_wrap.cpp`), so unlocked lwIP calls panic there. Soak the API (200 ms template
counter, 1 s uptime, logs subscribed), load pages, OTA and reboot; the rp2 crash handler shows a panic as
`Crash (HardFault)` in the debug reset reason on the next boot.
- Fallback AP client count: `ap_clients_` is protected and `WiFiComponent` is final; read it from an
`esphome: includes:` header through an explicitly instantiated member pointer (`appeek.hpp`). Join raises it to 1.
- bluetooth_proxy on the Pico W: `rp2_ble_tracker:` + `bluetooth_proxy: active: true`. Drive with `gatthold.py`
against the ESP32-S3 server in `scripts/ble_notify/server.yaml`. UUID forms and the 0x3E connect failures are in
`bluetooth_proxy.md`. A "Failed to send notify data response" warning at each disconnect is normal.
- web_server SSE on the W5100S (dev 2a6a1a050e4, 6.1.1): every `/events` stream stops after 0.3 to 11 s (alone, with
the API, or with page loads alike), then the EVB closes it with a FIN 5.8 to 6.3 s after its last data segment, so
curl exits 0. The pcap shows every byte ACKed by the host, window 65535, no retransmits, nothing sent in the gap:
the device simply stops writing. The 5 s gap matches RPAsyncTCP `ASYNC_MAX_ACK_TIME` (likely `_pcb_busy` not
cleared, then `_onTimeout` closes). A new `/events`, GET / and the API keep working. Pico W: 60 s steady. On macOS
`tcpdump` runs without sudo once your account is in the bpf group. Tools: `sserun.sh HOST TAG SECS alone|api|pages
N` (pcap, netstat poll, API VERBOSE log, probes) and `pcapsum.py PCAP DEVICE_IP` (per connection last data, FIN/RST,
retx). Confirmed with counters patched into the build's `.piolibdeps` copy of RPAsyncTCP: every stall is one
`_sent()` underflow with `_tx_unacked_len` 0 and the pcb idle after `send()`; ESPAsyncWebServer 3.12.1 does not
help. The esphome-libs RPAsyncTCP fork (`esphome-1.3.2`, 8efb83c) streams 16 x 30 s with no stall. `stallctr.py PCAP
IP APILOG` lines the counters up with each stall.
- `H=<device ip> churn.sh TAG SECS` (4 loops of 0.2 to 1.5 s `/events` connections) crashes the rp2 web_server: dev on
the EVB, the fork build on the EVB and the Pico W. HardFault in `AsyncEventSource::send` from
`deferrable_send_state`, about one reboot every 10 to 20 s. Ten quick reboots put the board in safe mode (ping and
OTA port 2040 only); OTA a build to recover.
@@ -1,47 +0,0 @@
# Bluedroid GATT client command queue
## Queued MTU request recursion overflows BTU_TASK (#17954, #22670)
Bluedroid's `bta_gattc_pop_command_to_send()` runs the head of `p_cmd_list` and removes it only after
`bta_gattc_sm_execute()` returns. When that head is an MTU request (or an execute write) whose send fails at once,
the failure path pops again with the same command still at the head, so it recurses until the BTU task stack
overflows. Every ESP-IDF release so far; PR #22670 fixes it in ESPHome with linker wraps
(`bluedroid_gattc_queue.c`).
Rig, `scripts/bluedroid_mtu_recursion/`:
- Peer: an ESP32-S3 with `btusrv.yaml` (`esp32_ble_server`, one readable and writable characteristic, advertising).
- Central: a classic ESP32 devkit with `btu-common.yaml` plus a per-build file like `btucli-example.yaml` (set
`peer_mac` to the peer's Bluetooth MAC, which is its Wi-Fi MAC + 2, and give each build its own name).
`CONFIG_FREERTOS_WATCHPOINT_END_OF_STACK: y` makes the overflow panic at the write:
`Stack canary watchpoint triggered (BTU_TASK)`.
- Each connection: `on_connect` looks up the characteristic handle right away (a `ble_client` with no child nodes
releases services and cleans the GATT cache in the same SEARCH_CMPL event, so a lookup after a delay finds
nothing), waits 1.5 s, then suspends `BTU_TASK`, calls `esp_ble_gap_disconnect`, queues 4 reads and one MTU
request, waits 30 ms and resumes. The batch is the key: sent one at a time, the BTU task handles each request
before the next arrives and nothing waits in the queue. The reads fail and keep the command slot busy so the MTU
request is queued; when popped, `GATTC_ConfigureMTU` fails with "BT_GATT: connection not established".
`ble_client` reconnects on its own and the next round starts.
- `analyze.py CAPTURE` counts rounds, BTU canary crashes, boots, reconnects with and without a reboot and MTU 517.
- Decode with the IDF `xtensa-esp32-elf-addr2line -pfiaC`; the overflow shows the repeating
`pop_command_to_send (act.c:1739/1746) -> sm_execute -> cfg_mtu (act.c:1042)` frames.
Results on ESP-IDF 5.5.6: dev at 46844d3631 crashed 73 of 73 rounds in 5.5 min (every round, about 4.5 s per
reboot cycle); the PR at ad787163fd ran with no crash and reconnected without a reboot each time. Check the wraps are
linked with `nm firmware.elf | grep __wrap_GATTC_ConfigureMTU` (native ESP-IDF builds only; the define is
`USE_ESP32_BLE_BLUEDROID_GATTC_QUEUE`).
### Execute write variant
`btuxw-example.yaml` sets `exec_write: "true"`, which sends `esp_ble_gattc_execute_write(gattc_if, conn_id, true)`
in place of the MTU request. No prepare write is needed: Bluedroid does not check for prepared writes, and
`GATTC_ExecuteWrite` fails synchronously with the same "connection not established" error. The variant also sets
`CONFIG_BT_LOG_GATT_TRACE_LEVEL_API: y`, so each send logs `BT_GATT: GATTC_ExecuteWrite conn_id=... is_execute=1`,
and those lines are counted per round with awk between the `ROUND n start` lines. In a few rounds (about 3 to 14%)
the controller reports the disconnect before the command is popped. Those rounds log no execute send and never
crash, so count "rounds that reached the send", not all rounds. Most dev backtraces in this variant are cut off in
`vTaskSwitchContext` (the extra trace logging makes task switches more likely at the moment of overflow). Use the
complete ones to decode the `bta_gattc_execute (act.c:1352)` cycle.
Results: dev 46844d3631 had 81 rounds, 70 reached the send, and all 70 crashed. PR e51f0b8321 had 151
rounds, 147 reached the send with exactly one execute line each, and there were 0 crashes and 1 boot.
@@ -55,15 +55,3 @@ real device uses, decode an Android HCI snoop: a Read By Type response lists its
- Give the proxy an `api` encryption key (`API_KEY` for the scripts). A Home Assistant on the same network connected to
a plaintext test proxy and took over its Bluetooth subscription (`Subscription from Home Assistant ... replaces
aioesphomeapi`), dropping the test client's connection mid-run.
## GATT UUID forms through a proxy (#22679)
`scripts/gatt_uuid/`: `server.yaml` (the `ble_notify` server plus a service with 128 bit, 32-bit base and 16 bit
UUIDs on characteristics and descriptors, and 2901 and 2902; set the name with `-s name`), `gattlist.py PROXY PEER
OUT.json` (lists every UUID, reads, notifies 10 s, retries 0x3E), `cmp.py REF.json OTHER.json` (entry by entry diff,
exits 1 on a difference) and `run.sh NAME` (Pico W proxy from `rp2_web_ble/picow.yaml`, list, compare). Take the
reference from an ESP32 proxy (unset `API_KEY` for a plaintext API).
- On rp2 before #22679, BTstack truncated 32-bit base UUIDs with a nonzero low half to 16 bits (`5ec10001-...` came
out as `00000001-...`); a zero low half (5ec10000) came through intact.
- Connects through the Pico W proxy fail with 0x3E now and then (see `reference/boards.md`); the scripts retry.
@@ -24,11 +24,3 @@ put `scripts/` on `PYTHONPATH`):
- Quick crash reboots count toward BK72xx safe mode, so space out back-to-back crash tests on those boards.
- A device that keeps the previous crash replays it to every new log client until the next crash; that is one
record, not two.
## Crash records and log subscribers
The record is replayed and cleared by the first log subscribe (`on_subscribe_logs_request`), so drain it on the
first boot after an OTA, or a stale record from the previous build shows up. Moving `api: port:` keeps Home Assistant
off the board while testing. `scripts/crash_report/quiet_then_subscribe.py HOST PORT OUT N` drains the record, stays
connected without a log subscription for N reboots, then subscribes and records the replayed record; it tells a crash
that needs a log subscriber (like the W5500 IPv6 reboot abort in `reference/boards.md`) from one that does not.
@@ -1,46 +0,0 @@
# Improv serial and BLE (#22597)
`scripts/improv/` drives Improv on a freshly erased board with no saved network:
- `improvcli.py PORT OUT --secrets FILE`: Improv serial client. It shares the console with the logs and parses
frames out of the log stream. The flow: 300 stray bytes with no `I` in them, state, info, network state, scan, a
bad password (expect `unable_to_connect` after about 30 s), the real password, then state and network state
again. It ends with a count of frames, bad frames, port resets, reboots and log lines. Credentials come from the
secrets file and are never printed, and the SSID is masked in scan results. It reopens a native USB port that
re-enumerates. A reboot is counted from each new `Running through setup()` log line after the settle, so a board
behind a USB-UART bridge that reboots without a port reset is caught too.
Every request must get its reply; a missing reply, an unexpected error, a malformed frame, a port reset or a
reboot fails the run. `--skip-bad` leaves out the bad password case, `--keep-dtr` leaves DTR asserted,
`--allow-reset` accepts a reboot.
- `improvble.py NAME SECRETS`: Improv BLE client (bleak). Finds the device by name, sends WIFI_SETTINGS and prints
the state changes and the RPC result URLs. Needs `improv_ble` (formerly `esp32_improv`) with `authorizer: none`
and a `next_url`; the result must carry a URL.
- `common.yaml`: package with Wi-Fi and no network, `web_server`, `improv_serial` with a `next_url` (so the
provisioned reply carries URLs) and an ALIVE line every 5 s. `example-esp32.yaml` uses it on a classic ESP32.
- `run.sh NAME [improvcli args]` (env `TREE`, `PORT`, `SECRETS`, `PY`): compile, erase, flash and run the client.
It erases with esptool, so ESP chips only; on LibreTiny or RP2040 erase and flash with that platform's tools and
run `improvcli.py` directly. Both clients end with PASS or FAIL and exit non-zero on FAIL.
Per board:
- Improv serial uses the logger's UART, so the client must open the port the log is on. On LibreTiny check which
UART `logger: hardware_uart: DEFAULT` resolves to (see the board notes).
- BW15: drop `web_server:` or add an `ap:` (it resets during setup otherwise), and pass `--allow-reset` for the known
reboot after `Disabling AP`; the provisioned reply usually goes out first, not always. See `reference/boards.md`.
- ESP32-C3 on USB_SERIAL_JTAG: passes with and without `--keep-dtr`; its reboots only show in the
`Running through setup()` count (see `reference/boards.md`). The scan returns only the last scan's results, which
are empty in the first seconds after boot; use `--settle 10` or more when the target network should be listed.
- Pico 2 W: run with `--keep-dtr` (see `reference/boards.md`). Its Improv scan lists no networks, on dev and PR alike.
- Erase the flash between builds: a saved network skips the provisioning path entirely. Both clients fail at once
when the board already reports `provisioned`; erase the flash first. A new config name does not help: the
network is saved under a fixed key when the config has no `networks:`. A provisioned board also stops advertising
Improv over BLE, so `improvble.py` reports the device as not found.
- If the real password fails right after the bad password case, rerun once before blaming the change: on the C3 it
failed once (`Auth Leave` while Wi-Fi was still retrying the bad network) and passed on every rerun.
When posting results:
- Scan results list the neighbours' SSIDs; filter `RPC cmd=4 len=[1-9]` lines out of anything you post.
- Mask IP addresses in the result URLs.
- Cover a long password too: provision once against a network whose password is 20 characters or more (the client
prints the length).
@@ -61,10 +61,3 @@ frame of two LEDs set to `Color(0xA5, 0x3C, 0x0F)` reads `3ca50f3ca50f`.
- An RP2350 `panic()` escalates to a HardFault and the rp2 crash handler reboots within about a second, so a panic
during component setup boot-loops before USB CDC enumerates and the board looks dead (no port, no BOOTSEL drive).
Hold BOOTSEL while plugging it in and flash the UF2 drive. To see such a fault, trigger it after boot.
## Counting a PIO strip's edges on its own pin (#16243)
A GPIO interrupt on the strip's own data pin (`gpio_set_irq_enabled_with_callback(pin, GPIO_IRQ_EDGE_RISE, ...)`)
counts every bit the PIO program sends, with no wires or LEDs; a strip that sends nothing shows 0 edges per report.
Log the PIO block's state machine enable mask and program counters next to the count. `pio_probe/pio-common.yaml`
clears PIO1 instruction memory at boot because it survives a reflash (see `reference/boards.md`).
@@ -34,11 +34,3 @@ wave on the same pad no longer reaches the input (the pad reads 0).
- Result on #21074 at 2ef417bd81 with peripherals powered down: the PR caught all 644 real edges (dev 66) but added
4642 calls while the line was high, one per sleep cycle (about 16 ms), with the level read correctly. Peripherals
on: both builds caught every edge. https://github.com/esphome/esphome/pull/21074#issuecomment-6503939850
## Power lock check
`power_management: esp32_power_management` with `profiling: true` and an `interval` lambda that calls
`esp_pm_dump_locks(stdout);` prints the lock and mode tables on the USB console (not through the ESPHome logger).
Compare lock lists between builds: `usb_serial_jtag` holds `NO_LIGHT_SLEEP` by itself. For a soak that the USB console
must not keep awake, set `logger: hardware_uart: UART0`, dump the locks into `open_memstream()` and log the SLEEP,
CPU_MAX and light sleep count lines, read over the API.
@@ -73,16 +73,3 @@ address they do not simulate.
- An `addressable_light` display writes nothing until its light is turned on.
- Before calling a rebased PR's change trivial, diff the PR's own diff (head against its parent) old against new: a
move that looks like a merge only can carry real commits from below.
## TRF7962A fake chip (#22188)
`scripts/trf7962a_sim/`: `trfsim.hpp` replaces the SPI component's bus (`SPIComponent` is final, so `spi_bus_` is
reached through the explicit-instantiation member pointer trick) with a `FakeBus` from `on_boot` priority 800, between
the bus (1000) and the reader's setup (600). The bus hands both of the reader's clients (MODE0 write, MODE1 read)
delegates on one register, FIFO and IRQ model; the IRQ pad is switched to input/output after setup and driven by the
model. `trfpr.yaml` runs the tag scenarios (none, present, slow, flaky, swap, collision, CRC, error flag, bad MSB,
TX only, stuck IRQ) and prints per-phase counters (CS, field timing, TX payload, reads without an address). The
same FakeBus approach works for any SPI chip whose reply depends on the address byte inside one CS window, which the
ESP32 SPI slave cannot fake.
The header targets the SPI API of the #22188 branch (`SPIBus` and `SPIComponent::spi_bus_`); dev has since dropped
`SPIBus`, so on a newer tree the bus hook has to move to the new delegate creation before it builds.
@@ -72,23 +72,3 @@ case, and `T1 X` shows which frames got through.
about 49 s after reset. Only the second boot logs `CASE done`, so a skipped reboot fails the run.
See `rs485_frame.md` for the same cross-wired UART trick on an ESP32-C3.
## Duco ventilation box (#22560)
`scripts/uart_sim/duco/`: `ducosim.hpp` is the box model from `tests/integration/fixtures/uart_mock_duco.yaml` (ack
plus answer, AA 01 stuffing, CRC16) on a second UART of the same board; on an ESP32-C6 the hub used tx 18 rx 19 and
the box tx 19 rx 18 at 57600, wired across. `gen.py NAME pr|dev` writes the config (every text sensor, sensors, mode,
comfort, sntp clock) with an `on_value` log per entity (`STATE id = '...'`, newlines as `\n`); `DUCO_VERBOSE=1` shows
`Found node`, `DUCO_SERIAL_SIZE=18` gives the dev fixture's short serial answer. `run.sh NAME KIND SECONDS` (env
`TREE`, `DUCO_PORT`, `PY`, optional `BUILD_LOCK`), run from a scratch directory with a copy of `ducosim.hpp`,
generates, builds through `scripts/pio_build.sh`, flashes and captures with `scripts/serial_capture.py`; a pr build
ends with `check.py` against `expected.txt` (exit 1 on a mismatch), and the model logs requests it has no answer for.
- Answer one request per poll call. Answering a 26 request burst in one pass blocks about 100 ms and overflows the
hub's 256 byte UART RX buffer, which silently drops the serial and version answers every round.
- Dev's hub frame is 32 bytes: the PR model's 34 byte serial answer is dropped there and duco sets "No answer".
- Error cases 7 (three long, 210 B) and 8 (four short, 148 B) check that the code-only fallback goes by length.
## ESP8266 software serial byte checks
The worst case for an unlock after a trailing run of high bits is a frame of bytes all below 0x80; a mixed frame
hides it.
@@ -59,11 +59,3 @@ valve, update, alarm_control_panel or water_heater entity reboots with `Exceptio
`json::linked(const char *)` (the ROM `strlen` on a flash address), fixed by #21316. The cover in the template keeps
that path covered; before #21316 is merged, apply its one line change to both trees or the web_server comparison
cannot run.
## Basic auth on the async web servers (ESP8266, RP2040, LibreTiny)
`scripts/web_auth/` (see its README) fuzzes Basic auth over raw sockets: functional routes with and without
credentials, wrong credentials, other schemes, a tampered hash, spacing and case, then large headers, many failed
attempts next to an authenticated `/events` stream, and a timing comparison of wrong first and last bytes. It also
has a captive portal client package that probes `/`, `/config.json`, POST with and without credentials and
`/wifisave`. `compare.py` lines a dev run up with a PR run. These scripts were still being extended when copied here.
@@ -21,14 +21,3 @@ uart_button/uartbtn*.yaml
value_range/rng*.yaml
ld2420_sim/ldenergy*.yaml
ld2420_sim/ldsimple*.yaml
apikey.txt
pio/
rp2_web_ble/*.log
rp2_web_ble/*.txt
rp2_web_ble/*.pcap
gatt_uuid/*.json
gatt_uuid/*.txt
web_auth/configs/*.log
web_auth/configs/*.txt
web_auth/configs/*.json
improv/*.log
@@ -1,88 +0,0 @@
"""
Rapid native API connect/disconnect stress test (from esphome#14687 and esphome#14706).
usage: connstress.py HOST [ITERATIONS] [CONCURRENCY] (default 500 iterations, 4 at a time)
env: API_KEY (base64 noise key; unset, empty or - for plaintext), API_PORT (default 6053), API_PASSWORD
Each attempt connects with login, disconnects and counts a failure on any error or a 10 s timeout on either step.
Exits 1 when any attempt failed. Watch the serial log for crashes: the client only sees resets.
"""
import asyncio
import contextlib
import os
import sys
import time
from aioesphomeapi import APIClient
HOST = sys.argv[1] if len(sys.argv) > 1 else sys.exit(__doc__)
PORT = int(os.environ.get("API_PORT", "6053"))
PASSWORD = os.environ.get("API_PASSWORD", "")
NOISE_PSK = (
None if os.environ.get("API_KEY") in (None, "", "-") else os.environ["API_KEY"]
)
async def connect_disconnect(client_id: int, iteration: int) -> tuple[int, bool, str]:
cli = APIClient(HOST, PORT, PASSWORD, noise_psk=NOISE_PSK)
try:
await asyncio.wait_for(cli.connect(login=True), timeout=10)
# a disconnect that hangs on a wedged device is a failure too, not a stuck slot
await asyncio.wait_for(cli.disconnect(), timeout=10)
return iteration, True, ""
except Exception as e: # noqa: BLE001 every failure is a counted result
return (
iteration,
False,
f"client{client_id} iter{iteration}: {type(e).__name__}: {e}",
)
finally:
with contextlib.suppress(Exception):
await asyncio.wait_for(cli.disconnect(force=True), timeout=10)
async def main() -> None:
iterations = int(sys.argv[2]) if len(sys.argv) > 2 else 500
concurrency = int(sys.argv[3]) if len(sys.argv) > 3 else 4
print(f"Iterations: {iterations}, Concurrency: {concurrency}", flush=True)
success = fail = 0
errors: list[str] = []
start = time.monotonic()
sem = asyncio.Semaphore(concurrency)
async def run(client_id: int, iteration: int) -> tuple[int, bool, str]:
async with sem:
return await connect_disconnect(client_id, iteration)
tasks = [asyncio.create_task(run(i % concurrency, i)) for i in range(iterations)]
for coro in asyncio.as_completed(tasks):
_, ok, err = await coro
if ok:
success += 1
else:
fail += 1
errors.append(err)
total = success + fail
if total % 10 == 0 or not ok:
rate = total / max(time.monotonic() - start, 1e-6)
print(
f"[{total}/{iterations}] ok={success} fail={fail} ({rate:.1f}/s)",
flush=True,
)
if err:
print(f" ERROR: {err}", flush=True)
elapsed = time.monotonic() - start
print(f"\nDone in {elapsed:.1f}s")
print(f"Success: {success}, Failed: {fail}, Rate: {iterations / elapsed:.1f}/s")
if errors:
print("\nLast 10 errors:")
for e in errors[-10:]:
print(f" {e}")
print("RESULT", "PASS" if fail == 0 else "FAIL")
sys.exit(1 if fail else 0)
if __name__ == "__main__":
asyncio.run(main())
@@ -4,7 +4,6 @@ Import it with this directory on sys.path (the drivers add their parent director
"""
import asyncio
from collections.abc import Awaitable, Callable
import contextlib
import os
import re
@@ -28,32 +27,19 @@ FATAL = (
)
async def connect(
host: str,
tries: int = 30,
timeout: float = 10,
port: int = 6053,
on_stop: Callable[[bool], Awaitable[None]] | None = None,
) -> APIClient:
async def connect(host: str, tries: int = 30, timeout: float = 10) -> APIClient:
"""Connect and log in, retrying once a second; exits after TRIES failures with the last error.
API_KEY in the env selects Noise; unset, empty or - is plaintext. ON_STOP is called when the connection ends.
API_KEY in the env selects Noise; unset, empty or - is plaintext.
"""
key = os.environ.get("API_KEY")
last: BaseException | None = None
for _ in range(tries):
c = APIClient(
host, port, None, noise_psk=None if key in (None, "", "-") else key
host, 6053, None, noise_psk=None if key in (None, "", "-") else key
)
live = [False]
# a failed attempt's cleanup may call on_stop too; pass on only stops of the returned client
async def stop(expected: bool, live: list[bool] = live) -> None:
if live[0] and on_stop is not None:
await on_stop(expected)
try:
await asyncio.wait_for(c.connect(on_stop=stop, login=True), timeout)
await asyncio.wait_for(c.connect(login=True), timeout)
except FATAL as e:
sys.exit(f"no API connection to {host}: {type(e).__name__}: {e}")
except Exception as e: # noqa: BLE001
@@ -63,7 +49,6 @@ async def connect(
await c.disconnect(force=True)
await asyncio.sleep(1)
else:
live[0] = True
return c
sys.exit(
f"no API connection to {host} after {tries} tries: {type(last).__name__}: {last}"
@@ -73,80 +58,3 @@ async def connect(
def log_text(msg) -> str:
"""The message of a subscribe_logs callback as text, without ANSI colour codes."""
return ANSI.sub("", msg.message.decode(errors="replace"))
def stop_event() -> tuple[asyncio.Event, Callable[[bool], Awaitable[None]]]:
"""An Event and an on_stop callback for connect() that sets it when the connection ends."""
ev = asyncio.Event()
async def on_stop(expected: bool) -> None:
ev.set()
return ev, on_stop
async def ble_connect(
cli: APIClient,
addr: int,
on_lost: Callable[[int], None] | None = None,
advert_timeout: float = 60,
timeout: float = 30,
) -> tuple[bool, int, int, Callable[[], None]]:
"""Wait for an advert from ADDR, then connect through the proxy without a cache (like Home Assistant).
Returns (connected, mtu, error, cancel); a missing advert or a connect timeout gives (False, 0, -1, cancel), so
callers can retry. ON_LOST(error) is called when the link drops later.
"""
seen = asyncio.Event()
unsub = cli.subscribe_bluetooth_le_raw_advertisements(
lambda m: (
seen.set() if any(a.address == addr for a in m.advertisements) else None
)
)
try:
await asyncio.wait_for(seen.wait(), advert_timeout)
except TimeoutError:
return False, 0, -1, lambda: None
finally:
unsub()
first: asyncio.Queue = asyncio.Queue()
pending = True
def on_state(connected: bool, mtu: int, err: int) -> None:
nonlocal pending
if pending:
pending = False
first.put_nowait((connected, mtu, err))
elif not connected and on_lost is not None:
on_lost(err)
cancel = await cli.bluetooth_device_connect(
addr,
on_state,
timeout=timeout,
address_type=0,
feature_flags=0xFF,
has_cache=False,
)
try:
connected, mtu, err = await asyncio.wait_for(first.get(), timeout + 5)
except TimeoutError:
return False, 0, -1, cancel
return connected, mtu, err, cancel
async def enable_notify(
cli: APIClient, addr: int, ch, on_notify: Callable[[int, bytearray], None]
) -> bool:
"""Start notify on characteristic CH and write its first CCCD (2902) like Home Assistant.
Returns False when the characteristic lists no CCCD.
"""
await cli.bluetooth_gatt_start_notify(addr, ch.handle, on_notify)
cccd = next(
(d for d in ch.descriptors if d.uuid.lower().startswith("00002902")), None
)
if cccd is None:
return False
await cli.bluetooth_gatt_write_descriptor(addr, cccd.handle, b"\x01\x00")
return True
@@ -1,57 +0,0 @@
"""Count rounds, BTU_TASK crashes, reconnects without reboot and MTU 517 in a central capture.
analyze.py CAPTURE
A round counts from its "start" line (a crashing round never logs "fired"); rounds that log "not queued" or
"aborted" are taken back out and listed apart. Exits 1 when no round counts, since such a capture tested nothing.
"""
import pathlib
import re
import sys
lines = (
pathlib.Path(sys.argv[1]).read_text(encoding="utf-8", errors="replace").splitlines()
)
rounds = crashes = other_panics = boots = connects = mtu517 = mtu_other = 0
not_queued = aborted = skipped = 0
reconnect_ok = reconnect_reboot = 0
pending = False # a round fired and we wait for the next connect
for ln in lines:
if "Stack canary watchpoint triggered (BTU_TASK)" in ln:
crashes += 1
elif "Guru Meditation" in ln or "abort() was called" in ln:
other_panics += 1
if "rst:0x" in ln:
boots += 1
if pending:
reconnect_reboot += 1
pending = False
if "CLI connected" in ln:
connects += 1
if pending:
reconnect_ok += 1
pending = False
if re.search(r"ROUND \d+ start", ln):
rounds += 1
pending = True
if re.search(r"ROUND \d+ (not queued|aborted)", ln):
rounds -= 1
pending = False
not_queued += "not queued" in ln
aborted += "aborted" in ln
skipped += "ROUND skipped" in ln
if m := re.search(r"cfg_mtu status 0, mtu (\d+)", ln):
if m.group(1) == "517":
mtu517 += 1
else:
mtu_other += 1
print(
f"rounds={rounds} btu_canary_crashes={crashes} other_panics={other_panics} boots={boots}"
)
print(
f"connects={connects} reconnect_without_reboot={reconnect_ok} reconnect_after_reboot={reconnect_reboot}"
)
print(f"mtu517={mtu517} mtu_other={mtu_other}")
print(f"not_queued={not_queued} aborted={aborted} skipped={skipped}")
if rounds <= 0:
sys.exit("FAIL: no round reached the BTU queue")
@@ -1,75 +0,0 @@
# Central for the Bluedroid queued MTU recursion repro (esphome#17954). Every connection: wait 1.5 s, then
# suspend BTU_TASK, local disconnect, 4 reads + 1 MTU request (or execute write with exec_write: "true") in one
# batch, resume. ble_client reconnects.
esp32:
board: esp32dev
framework:
type: esp-idf
sdkconfig_options:
CONFIG_FREERTOS_WATCHPOINT_END_OF_STACK: y
logger:
level: DEBUG
esp32_ble_tracker:
scan_parameters:
active: false
globals:
- {id: rounds, type: int, initial_value: "0"}
- {id: chr_handle, type: uint16_t, initial_value: "0"}
ble_client:
- mac_address: ${peer_mac}
id: srv
on_connect:
- logger.log: {format: "CLI connected", level: INFO}
# Runs inside SEARCH_CMPL, before ble_client releases the GATT cache (it has no other nodes)
- lambda: |-
esp_bt_uuid_t uuid = esp32_ble_tracker::ESPBTUUID::from_raw("5ec10001-0000-1000-8000-00805f9b34fb").get_uuid();
esp_gattc_char_elem_t elem;
uint16_t count = 1;
id(chr_handle) = 0;
if (esp_ble_gattc_get_char_by_uuid(id(srv)->get_gattc_if(), id(srv)->get_conn_id(), 1, 0xFFFF, uuid, &elem,
&count) == ESP_GATT_OK && count > 0)
id(chr_handle) = elem.char_handle;
- delay: 1500ms
- lambda: |-
if (!id(srv)->connected()) return;
esp_gatt_if_t gattc_if = id(srv)->get_gattc_if();
uint16_t conn_id = id(srv)->get_conn_id();
uint16_t handle = id(chr_handle);
if (handle == 0) {
ESP_LOGW("btu", "ROUND skipped, no characteristic");
return;
}
int n = ++id(rounds);
ESP_LOGI("btu", "ROUND %d start conn_id=%u handle=0x%04x", n, conn_id, handle);
TaskHandle_t btu = xTaskGetHandle("BTU_TASK");
if (btu == nullptr) {
// vTaskSuspend(nullptr) would suspend this task forever
ESP_LOGE("btu", "ROUND %d aborted, no BTU_TASK", n);
return;
}
vTaskSuspend(btu);
// a rejected disconnect leaves the link up, so the queued calls never take the failing send path
esp_err_t err = esp_ble_gap_disconnect(id(srv)->get_remote_bda());
for (int i = 0; i < 4; i++) {
esp_err_t e = esp_ble_gattc_read_char(gattc_if, conn_id, handle, ESP_GATT_AUTH_REQ_NONE);
if (e != ESP_OK)
err = e;
}
esp_err_t e = ${exec_write} ? esp_ble_gattc_execute_write(gattc_if, conn_id, true)
: esp_ble_gattc_send_mtu_req(gattc_if, conn_id);
if (e != ESP_OK)
err = e;
delay(30);
vTaskResume(btu);
// a round whose calls were rejected never reached the BTU queue, so it tests nothing
if (err != ESP_OK) {
ESP_LOGW("btu", "ROUND %d not queued: %s", n, esp_err_to_name(err));
} else {
ESP_LOGI("btu", "ROUND %d fired", n);
}
on_disconnect:
- logger.log: {format: "CLI disconnected", level: INFO}
@@ -1,9 +0,0 @@
esphome:
name: btuclibase # one name per build (btuclibase, btuclipr)
substitutions:
peer_mac: "AA:BB:CC:DD:EE:FF" # the peer S3 Bluetooth MAC (Wi-Fi MAC + 2)
exec_write: "false"
packages:
common: !include btu-common.yaml
@@ -1,31 +0,0 @@
# Peer for the Bluedroid queued MTU recursion repro: a GATT server with one readable and writable characteristic
esphome:
name: btusrv
esp32:
variant: esp32s3
board: esp32-s3-devkitc-1
framework:
type: esp-idf
logger:
level: DEBUG
hardware_uart: USB_SERIAL_JTAG
esp32_ble:
name: btusrv
esp32_ble_server:
on_connect:
- logger.log: {format: "SRV connected", level: INFO}
on_disconnect:
- logger.log: {format: "SRV disconnected", level: INFO}
services:
- uuid: 5ec10000-0000-1000-8000-00805f9b34fb
advertise: true
characteristics:
- id: numc
uuid: 5ec10001-0000-1000-8000-00805f9b34fb
read: true
write: true
value: [42]
@@ -1,15 +0,0 @@
esphome:
name: btuxwbase # one name per build (btuxwbase, btuxwpr)
substitutions:
peer_mac: "AA:BB:CC:DD:EE:FF" # the peer S3 Bluetooth MAC (Wi-Fi MAC + 2)
exec_write: "true"
esp32:
framework:
sdkconfig_options:
# GATT API trace prints one "GATTC_ExecuteWrite conn_id=" line per execute send
CONFIG_BT_LOG_GATT_TRACE_LEVEL_API: y
packages:
common: !include btu-common.yaml
@@ -1,102 +0,0 @@
"""quiet_then_subscribe.py HOST PORT OUT QUIET_BOOTS: on the first boot drain any crash record (the first log
subscribe replays and clears it), then stay connected without a log subscription for QUIET_BOOTS reboots, then
subscribe on the next boot and record the log (including a crash record replayed on subscribe) until a line with
REBOOT. The drain connection is dropped and replaced by one without a subscription before boot 1 reboots, so a crash
that needs a log subscriber is not recorded as a quiet one; a reboot during the drain, or a reconnect slow enough to
hide one, fails the run instead of shifting the boot count. Exits 1 when that line never comes or a quiet boot does
not reboot within QUIET_REBOOT_WAIT seconds (default 900). Every lost connection counts as a reboot, so run it on a
stable link.
env: API_KEY (unset for a plaintext API), QUIET_REBOOT_WAIT."""
import asyncio
import contextlib
import os
import pathlib
import sys
import time
from aioesphomeapi import LogLevel
sys.path.insert(0, str(pathlib.Path(__file__).resolve().parents[1]))
from apiconn import connect, log_text, stop_event # noqa: E402
# how long a quiet boot may run before its reboot; QUIET_REBOOT_WAIT in the env overrides it
REBOOT_WAIT = float(os.environ.get("QUIET_REBOOT_WAIT", "900"))
# a reconnect slower than this may have waited out a reboot (connect() retries once a second)
RECONNECT_MAX_S = 5.0
async def main(host: str, port: int, outf: str, quiet: int) -> None:
with pathlib.Path(outf).open("a", encoding="utf-8") as f:
def w(t: str) -> None:
f.write(f"{time.strftime('%H:%M:%S')} {t}\n")
f.flush()
boots = 0
while True:
lost, on_stop = stop_event()
cli = await connect(host, tries=300, timeout=5, port=port, on_stop=on_stop)
boots += 1
info = await cli.device_info()
w(
f"=== CONNECTED boot {boots} name={info.name} quiet={1 < boots <= quiet + 1}"
)
if boots > quiet + 1:
done = asyncio.Event()
def on_log(m, done: asyncio.Event = done) -> None:
t = log_text(m)
w(t)
if "REBOOT" in t:
done.set()
cli.subscribe_logs(
on_log, log_level=LogLevel.LOG_LEVEL_DEBUG, dump_config=False
)
try:
await asyncio.wait_for(done.wait(), 120)
except TimeoutError:
w("=== no REBOOT within 120 s")
with contextlib.suppress(Exception):
await cli.disconnect()
if not done.is_set():
sys.exit("FAIL: no REBOOT line within 120 s of subscribing")
return
if boots == 1:
# drain any crash record left by the reboot that brought this build up
cli.subscribe_logs(
lambda m: w("drain " + log_text(m)),
log_level=LogLevel.LOG_LEVEL_DEBUG,
dump_config=False,
)
await asyncio.sleep(3)
if lost.is_set():
w("=== boot 1 rebooted during the drain")
sys.exit("FAIL: boot 1 rebooted with the drain subscriber attached")
with contextlib.suppress(Exception):
await cli.disconnect()
lost, on_stop = stop_event()
t = time.monotonic()
cli = await connect(
host, tries=300, timeout=5, port=port, on_stop=on_stop
)
if (dt := time.monotonic() - t) > RECONNECT_MAX_S:
w(f"=== boot 1 reconnect took {dt:.1f} s")
sys.exit(
f"FAIL: the reconnect after the drain took {dt:.1f} s, boot 1 may have rebooted"
)
w("=== boot 1 reconnected without a log subscription")
# quiet: wait for the device to drop the connection (its reboot)
try:
await asyncio.wait_for(lost.wait(), REBOOT_WAIT)
except TimeoutError:
w(f"=== boot {boots}: no reboot within {REBOOT_WAIT:.0f} s")
sys.exit(
f"FAIL: boot {boots} did not reboot within {REBOOT_WAIT:.0f} s"
)
w("=== connection lost")
await asyncio.sleep(2)
asyncio.run(main(sys.argv[1], int(sys.argv[2]), sys.argv[3], int(sys.argv[4])))
@@ -1,62 +0,0 @@
"""cmp.py REF.json OTHER.json : list each UUID (service / characteristic / descriptor, in order) where OTHER differs
from REF, whether reads match (static characteristics), whether every notifying characteristic notified and
whether OTHER lost its link before the final disconnect. Exits 1 on any difference."""
import json
import pathlib
import sys
def flat(p):
d = json.loads(pathlib.Path(p).read_text(encoding="utf-8"))
rows = []
for s in d["services"]:
rows.append(("svc", s["uuid"].lower()))
for c in s["characteristics"]:
rows.append(("chr", c["uuid"].lower()))
rows += [("dsc", x.lower()) for x in c["descriptors"]]
return d, rows
rd, ref = flat(sys.argv[1])
od, oth = flat(sys.argv[2])
diff = [(a, b) for a, b in zip(ref, oth, strict=False) if a != b]
print(
f"entries ref={len(ref)} other={len(oth)} differing={len(diff) + abs(len(ref) - len(oth))}"
)
for a, b in diff:
print(f" {a[0]} ref {a[1]} other {b[1]}")
def chars(d: dict) -> list[dict]:
return [c for s in d["services"] for c in s["characteristics"]]
# compare by position: a UUID form bug renames the keys, so values and notify counts are matched by table order
problems = []
rc, oc = chars(rd), chars(od)
for i, (a, b) in enumerate(zip(rc, oc, strict=False)):
if a["props"] != b["props"]:
problems.append(
f"props {i} {b['uuid']} 0x{b['props']:02X} != ref 0x{a['props']:02X}"
)
rv, ov = rd["reads"].get(a["uuid"]), od["reads"].get(b["uuid"])
if str(ov).startswith("FAILED"):
problems.append(f"read {i} {b['uuid']} {ov}")
elif not a["props"] & 0x10 and rv is not None and ov != rv:
problems.append(f"read {i} {b['uuid']} {ov!r} != ref {rv!r}")
if a["props"] & 0x10:
n = od["notify"].get(b["uuid"])
if not isinstance(n, int) or n <= 0:
problems.append(f"notify {i} {b['uuid']} {n}")
# a link that dropped after the first notification still has counts, so the drop itself fails the run
if od.get("lost"):
problems.append(f"link lost before the disconnect: {od['lost']}")
print(f"notify {od['notify']}")
for x in problems:
print(" ", x)
if diff or len(ref) != len(oth) or problems:
sys.exit(
f"FAIL: {len(diff)} differing entries, {len(problems)} read or notify problems"
)
print("PASS")
@@ -1,100 +0,0 @@
"""List every service, characteristic and descriptor UUID a bluetooth_proxy reports for a peer, read each readable
characteristic, enable notify (CCCD write) on each notifying one and count 10 s of notifications, then disconnect.
A link lost before that disconnect is recorded as "lost" in the JSON, which cmp.py fails on.
usage: gattlist.py PROXY PEER_MAC OUT_JSON env: API_KEY (unset for a plaintext API). Retries 0x3E connect failures."""
import asyncio
import json
import pathlib
import sys
sys.path.insert(0, str(pathlib.Path(__file__).resolve().parents[1]))
from apiconn import ble_connect, connect, enable_notify # noqa: E402
async def once(host: str, addr: int) -> dict | None:
cli = await connect(host)
lost: list[int] = []
closing = False
def on_lost(e: int) -> None:
if not closing:
lost.append(e)
print(f"LINK LOST err={e}", flush=True)
c, _mtu, err, cancel = await ble_connect(cli, addr, on_lost)
if not c:
print(f"connect err={err}", flush=True)
cancel()
await cli.disconnect()
return None
svcs = await cli.bluetooth_gatt_get_services(addr)
out = {"services": [], "reads": {}, "notify": {}}
notif: dict[str, int] = {}
for s in svcs.services:
so = {"uuid": s.uuid, "characteristics": []}
for ch in s.characteristics:
so["characteristics"].append(
{
"uuid": ch.uuid,
"props": ch.properties,
"descriptors": [d.uuid for d in ch.descriptors],
}
)
if ch.properties & 0x02:
try:
out["reads"][ch.uuid] = bytes(
await cli.bluetooth_gatt_read(addr, ch.handle)
).hex()
except Exception as e: # noqa: BLE001
out["reads"][ch.uuid] = f"FAILED {type(e).__name__}"
if ch.properties & 0x10:
notif[ch.uuid] = 0
try:
if not await enable_notify(
cli,
addr,
ch,
lambda h, d, u=ch.uuid: (
notif.__setitem__(u, notif[u] + 1)
if isinstance(notif[u], int)
else None
),
):
notif[ch.uuid] = "FAILED no CCCD"
except Exception as e: # noqa: BLE001
notif[ch.uuid] = f"FAILED {type(e).__name__}"
out["services"].append(so)
await asyncio.sleep(10)
out["notify"] = notif
out["lost"] = lost
closing = True
await cli.bluetooth_device_disconnect(addr)
await asyncio.sleep(2)
cancel()
await cli.disconnect()
return out
async def main(host: str, peer: str, path: str) -> None:
addr = int(peer.replace(":", ""), 16)
for _attempt in range(6):
res = await once(host, addr)
if res:
break
await asyncio.sleep(10)
else:
sys.exit("FAIL: no connection")
pathlib.Path(path).write_text(json.dumps(res, indent=1), encoding="utf-8")
for s in res["services"]:
print(f"SVC {s['uuid']}")
for c in s["characteristics"]:
print(
f" CHR {c['uuid']} props=0x{c['props']:02X} read={res['reads'].get(c['uuid'], '-')}"
f" notify={res['notify'].get(c['uuid'], '-')}"
)
for d in c["descriptors"]:
print(f" DSC {d}")
asyncio.run(main(sys.argv[1], sys.argv[2], sys.argv[3]))
@@ -1,14 +0,0 @@
#!/bin/zsh
# run.sh NAME: build ../rp2_web_ble/picow.yaml as NAME, USB flash the Pico W proxy, list the GATT UUID server
# (server.yaml here) through it and compare with the ESP32 reference. Exit 0 only when the lists match.
# env: TREE, PY, PICO_SN, PEER_MAC (the server); esp32ref.json (gattlist.py through an ESP32 proxy) and apikey.txt here
D=${0:A:h}; N=$1; S=$D/..; T=${TREE:?set TREE}; PY=${PY:-$T/venv/bin/python}; C=$S/rp2_web_ble/picow.yaml
$S/pio_build.sh compile $N $C || exit 1
PORT=$($PY $S/ports.py --wait 2e8a --serial ${PICO_SN:?set PICO_SN} --timeout 30) || { echo "no pico"; exit 1; }
$S/pio_build.sh upload $N $C $PORT || exit 1
H=$($S/resolve.sh $N) || exit 1; sleep 5
rm -f $D/$N.json
API_KEY=$(cat $D/apikey.txt) timeout 600 $PY -I $D/gattlist.py $H ${PEER_MAC:?set PEER_MAC} $D/$N.json > $D/$N-list.txt
RC=$?; grep -v "^ *CHR 00002a\|DSC 00002902\|SVC 000018" $D/$N-list.txt
[ $RC = 0 ] && [ -s $D/$N.json ] || { echo "FAIL: gattlist.py exited $RC"; exit 1; }
$PY $D/cmp.py $D/esp32ref.json $D/$N.json
@@ -1,27 +0,0 @@
# GATT server (ESP32-S3) for the UUID form check: the ble_notify server (16 bit and 32-bit base UUIDs, notify) plus a
# 128 bit service whose characteristics and descriptors use 128 bit, 32-bit base (zero and nonzero low half) and
# 16 bit UUIDs, with a 2901 user description. Set the name with -s name.
packages:
notify: !include ../ble_notify/server.yaml
esp32_ble_server:
services:
- uuid: 0f1e2d3c-4b5a-6978-8796-a5b4c3d2e1f0
characteristics:
- uuid: 0f1e2d3c-4b5a-6978-8796-a5b4c3d2e1f1
read: true
value: "c128"
description: "text value"
descriptors:
- uuid: 5ec1ab01-0000-1000-8000-00805f9b34fb
value: "d32"
- uuid: 7d3e0a51-9c1b-4f2e-8a6d-1c2b3a4d5e6f
value: "d128"
- uuid: 5ec20000-0000-1000-8000-00805f9b34fb
read: true
value: "c32zero"
- uuid: 5ec2abcd-0000-1000-8000-00805f9b34fb
read: true
value: "c32"
- uuid: 2a19
read: true
value: [77]
@@ -1,18 +0,0 @@
# Package for Improv tests: Wi-Fi with no network saved, web_server and improv_serial with a next_url, and an ALIVE
# line every 5 s. Substitution: name. Drop web_server on a LibreTiny board with no ap: (it boot-loops, see
# reference/recipes/improv.md).
esphome:
name: ${name}
wifi:
power_save_mode: none
web_server:
improv_serial:
next_url: https://example.com/?device_name={{device_name}}&ip_address={{ip_address}}
interval:
- interval: 5s
then:
- logger.log: "ALIVE"
@@ -1,15 +0,0 @@
# Classic ESP32 devkit with Improv over serial (UART0, shared with the log) and BLE. Copy it per build with its own
# name; run.sh wants the file name to match name.
substitutions:
name: improv32
packages:
common: !include common.yaml
esp32:
board: esp32dev
framework:
type: esp-idf
logger:
hardware_uart: UART0
improv_ble:
authorizer: none
next_url: https://example.com/?device_name={{device_name}}&ip_address={{ip_address}}
@@ -1,96 +0,0 @@
"""Improv over BLE: find the device by name, send WIFI_SETTINGS and print state, errors and the RPC result URLs.
usage: improvble.py NAME SECRETS (needs bleak; the device needs improv_ble authorizer: none and a next_url)"""
import asyncio
import pathlib
import sys
from bleak import BleakClient, BleakScanner
sys.path.insert(0, str(pathlib.Path(__file__).resolve().parent))
from improvcli import load_wifi_secrets, strings # noqa: E402
BASE = "00467768-6228-2272-4663-2774782680"
STATE, ERROR, RPC, RESULT = BASE + "01", BASE + "02", BASE + "03", BASE + "04"
STATES = {1: "awaiting_auth", 2: "authorized", 3: "provisioning", 4: "provisioned"}
def pkt(cmd: int, data: bytes) -> bytes:
b = bytes([cmd, len(data)]) + data
return b + bytes([sum(b) & 0xFF])
async def main(name: str, secrets: str) -> None:
ssid, pw = (x.encode() for x in load_wifi_secrets(secrets))
dev = await BleakScanner.find_device_by_name(name, timeout=30)
if dev is None:
sys.exit(
"FAIL: device not found (a provisioned board stops advertising Improv)"
)
loop = asyncio.get_running_loop()
t0 = loop.time()
done = asyncio.Event()
errors: list = []
states: list[int] = []
async with BleakClient(dev) as c:
def on_state(_, d: bytearray) -> None:
if not d:
errors.append("empty state")
return
states.append(d[0])
print(f"{loop.time() - t0:6.2f} STATE {STATES.get(d[0], d[0])}", flush=True)
def on_error(_, d: bytearray) -> None:
if not d:
errors.append("empty error")
elif d[0]:
errors.append(d[0])
print(f"{loop.time() - t0:6.2f} ERROR {d[0]}", flush=True)
def on_result(_, d: bytearray) -> None:
if len(d) < 3 or d[1] != len(d) - 3 or sum(d[:-1]) & 0xFF != d[-1]:
errors.append("bad result frame")
print(
f"{loop.time() - t0:6.2f} BAD RESULT {bytes(d).hex()}", flush=True
)
return
try:
strs = strings(bytes(d[2:-1]))
except ValueError:
errors.append("string overruns the result")
strs = []
print(f"{loop.time() - t0:6.2f} RESULT cmd={d[0]} {strs}", flush=True)
if (
d[0] == 1
): # the WIFI_SETTINGS reply; the example sets next_url, so it carries a URL
if not any(x.startswith("http") for x in strs):
errors.append("no URL in result")
done.set()
await c.start_notify(STATE, on_state)
await c.start_notify(ERROR, on_error)
await c.start_notify(RESULT, on_result)
s = await c.read_gatt_char(STATE)
print(f"initial state {STATES.get(s[0], s[0]) if s else 'empty'}")
if not s or s[0] == 4:
sys.exit(
"FAIL: board already provisioned or no state; erase the flash first"
)
payload = bytes([len(ssid)]) + ssid + bytes([len(pw)]) + pw
await c.write_gatt_char(RPC, pkt(1, payload), response=True)
try:
await asyncio.wait_for(done.wait(), 45)
except TimeoutError:
print("no result within 45 s")
await asyncio.sleep(1)
last = await c.read_gatt_char(STATE)
states.append(last[0] if last else 0)
if not done.is_set() or errors or states[-1] != 4:
sys.exit(
f"FAIL (errors {errors}, last state {STATES.get(states[-1], states[-1])})"
)
print("PASS")
asyncio.run(main(sys.argv[1], sys.argv[2]))
@@ -1,339 +0,0 @@
"""Improv serial client that reads frames out of the log stream on the same port.
usage: improvcli.py PORT OUT --secrets FILE [--baud N] [--keep-dtr] [--settle S] [--skip-bad] [--allow-reset]
Runs: 300 stray bytes, state, info, network state, scan, provision with a bad password, provision with the real one,
state and network state again. Prints parsed frames; the raw stream overwrites OUT.
Exit 2 when the port does not open or the secrets file lacks a key. Exit 1 when any request goes unanswered, an
unexpected error or a malformed frame arrives, or the port resets or the log shows a reboot (unless --allow-reset).
Credentials come from the secrets file (wifi_ssid, wifi_password) and are never printed; the SSID is masked in scan
results."""
import argparse
import pathlib
import random
import sys
import time
import serial
import yaml
STATES = {
0: "stopped",
1: "awaiting_auth",
2: "authorized",
3: "provisioning",
4: "provisioned",
}
ERRORS = {
0: "none",
1: "invalid_rpc",
2: "unknown_rpc",
3: "unable_to_connect",
4: "not_authorized",
0xFF: "unknown",
}
def frame(cmd: int, payload: bytes = b"") -> bytes:
data = bytes([cmd, len(payload)]) + payload
body = b"IMPROV" + bytes([1, 3, len(data)]) + data
return body + bytes([sum(body) & 0xFF])
BOOT_MARKER = (
b"Running through setup()" # ESPHome logs it once per boot on every platform
)
def rpc_strings(data: bytes) -> bytes:
return data[2 : 2 + data[1]]
def strings(p: bytes) -> list[str]:
"""Split length-prefixed strings; raise ValueError when a prefix runs past the end."""
out = []
while p:
if 1 + p[0] > len(p):
raise ValueError("string overruns the payload")
out.append(p[1 : 1 + p[0]].decode(errors="replace"))
p = p[1 + p[0] :]
return out
def load_wifi_secrets(path: str) -> tuple[str, str]:
"""wifi_ssid and wifi_password from a secrets file; exits 2 (setup error) when it cannot be used."""
try:
with pathlib.Path(path).open(encoding="utf-8") as f:
sec = yaml.safe_load(f)
except (OSError, yaml.YAMLError) as exc:
print(f"cannot read secrets file: {exc}", file=sys.stderr)
sys.exit(2)
try:
raw = (sec["wifi_ssid"], sec["wifi_password"])
except (KeyError, TypeError) as exc:
print(f"secrets file lacks {exc}", file=sys.stderr)
sys.exit(2)
if not all(
isinstance(x, (str, int, float)) and not isinstance(x, bool) for x in raw
):
print("wifi_ssid and wifi_password must be plain values", file=sys.stderr)
sys.exit(2)
return str(raw[0]), str(raw[1]) # YAML reads an unquoted number as int
class Dev:
def __init__(
self, port: str, baud: int, out: str, keep_dtr: bool, ssid: str
) -> None:
self.ser = serial.Serial()
self.ser.port, self.ser.baudrate, self.ser.timeout = port, baud, 0.05
if not keep_dtr:
self.ser.dtr = False
self.ser.rts = False
self.reopen(setup=True)
self.buf = bytearray()
try:
self.log = pathlib.Path(out).open("wb") # noqa: SIM115
except OSError as exc:
print(f"cannot open output file: {exc}", file=sys.stderr)
sys.exit(2)
self.t0 = time.time()
self.events: list[tuple[float, int, bytes, bool]] = []
self.loglines = 0
self.reopens = 0
self.boots = 0
self.tail = b""
self.ssid = ssid
def reopen(self, setup: bool = False) -> None:
# Native USB ports re-enumerate on reset; wait up to 10 s for them.
err: Exception | None = None
for _ in range(40):
try:
if self.ser.is_open:
self.ser.close()
self.ser.open()
return
except (serial.SerialException, OSError) as exc:
err = exc
time.sleep(0.25)
print(
f"port did not {'open' if setup else 'come back'}: {err}", file=sys.stderr
)
sys.exit(2 if setup else 1)
def pump(self, dur: float) -> None:
end = time.time() + dur
while time.time() < end:
try:
d = self.ser.read(4096)
except serial.SerialException:
# A dropped native USB port usually means the board reset; make that visible.
self.reopens += 1
print(
f"{time.time() - self.t0:7.2f} PORT RESET (reopen {self.reopens})",
flush=True,
)
self.reopen()
continue
if d:
seen = self.tail + d
self.boots += seen.count(BOOT_MARKER)
self.tail = seen[-(len(BOOT_MARKER) - 1) :]
self.log.write(d)
self.log.flush()
self.buf += d
self.parse()
def parse(self) -> None:
while True:
i = self.buf.find(b"IMPROV")
if i < 0:
keep = max(0, len(self.buf) - 6)
self.loglines += self.buf[:keep].count(b"\n")
del self.buf[:keep]
return
self.loglines += self.buf[:i].count(b"\n")
del self.buf[:i]
if len(self.buf) < 9:
return
if self.buf[6] != 1: # not a version 1 header: "IMPROV" in log text
del self.buf[:6]
continue
ln = self.buf[8]
if len(self.buf) < 10 + ln:
return
body, ck = bytes(self.buf[: 9 + ln]), self.buf[9 + ln]
del self.buf[: 10 + ln]
typ, data = body[7], body[9:]
ok = (sum(body) & 0xFF) == ck
# A malformed frame is counted as bad, so the predicates never index past it.
short = len(data) < {1: 1, 2: 1, 4: 2}.get(typ, 0)
if typ == 4 and not short:
try:
# ESPHome sends one trailing byte after the strings (an unused checksum slot).
short = data[1] > len(data) - 2
strings(rpc_strings(data))
except ValueError:
short = True
self.events.append((time.time() - self.t0, typ, data, ok and not short))
self.show(typ, data, ok, short)
def show(self, typ: int, data: bytes, ok: bool, short: bool) -> None:
t = f"{time.time() - self.t0:7.2f}"
tag = "" if ok else " BADCHECKSUM"
if short:
print(f"{t} TYPE{typ} {data.hex()} MALFORMED{tag}")
elif typ == 1:
print(f"{t} STATE {STATES.get(data[0], data[0])}{tag}")
elif typ == 2:
print(f"{t} ERROR {ERRORS.get(data[0], data[0])}{tag}")
elif typ == 4:
strs = [
"<ssid>" if x == self.ssid else x for x in strings(rpc_strings(data))
]
print(f"{t} RPC cmd={data[0]} len={data[1]} {strs}{tag}")
else:
print(f"{t} TYPE{typ} {data.hex()}{tag}")
def send(self, cmd: int, payload: bytes = b"", wait: float = 2.0) -> None:
self.ser.write(frame(cmd, payload))
self.ser.flush()
self.pump(wait)
def wait_for(self, pred, timeout: float, start: int) -> tuple | None:
end = time.time() + timeout
while time.time() < end:
self.pump(0.2)
for e in self.events[start:]:
if pred(e):
return e
return None
def ask(
d: Dev, label: str, cmd: int, pred, timeout: float = 1.5, payload: bytes = b""
) -> bool:
"""Send one request and require a reply matching pred; any other nonzero ERROR frame fails it."""
print(f"== {label}")
start = len(d.events) # before sending, so an immediate reply counts
t = time.time()
d.send(cmd, payload, wait=0)
e = d.wait_for(pred, timeout, start)
print(
f"reply after {time.time() - t:.1f}s"
if e
else f"NO reply within {timeout:.0f}s"
)
errs = [
x for x in d.events[start:] if x[1] == 2 and x[3] and x[2][0] and not pred(x)
]
if errs:
print(f"unexpected errors: {[ERRORS.get(x[2][0], x[2][0]) for x in errs]}")
return e is not None and not errs
def rpc(cmd: int):
return lambda e: e[1] == 4 and e[3] and e[2][0] == cmd
def state(e: tuple) -> bool:
return e[1] == 1 and e[3]
def main() -> None:
a = argparse.ArgumentParser()
a.add_argument("port")
a.add_argument("out")
a.add_argument("--baud", type=int, default=115200)
a.add_argument(
"--keep-dtr",
action="store_true",
help="leave DTR asserted (Pico 2 W CDC sends nothing without)",
)
a.add_argument("--secrets", required=True)
a.add_argument("--settle", type=float, default=3)
a.add_argument("--skip-bad", action="store_true")
a.add_argument(
"--allow-reset",
action="store_true",
help="a port reset or a reboot in the log does not fail the run",
)
o = a.parse_args()
ssid, pw = load_wifi_secrets(o.secrets)
s, w = ssid.encode(), pw.encode()
print(f"password length {len(w)} bytes")
d = Dev(o.port, o.baud, o.out, o.keep_dtr, ssid)
d.pump(o.settle)
boots_at_start = d.boots
before = d.loglines
print("== stray bytes")
j = len(d.events)
junk = bytes(
random.choice([b for b in range(256) if b != 0x49]) for _ in range(300)
)
d.ser.write(junk)
d.ser.flush()
d.pump(1.0)
# Stray bytes must be ignored silently; the state reply below shows the parser recovered.
ok = not any(e[1] == 2 and e[3] and e[2][0] for e in d.events[j:])
# A saved network skips provisioning: the board must start unprovisioned.
ok &= ask(d, "state", 2, lambda e: state(e) and e[2][0] != 4)
if any(state(e) and e[2][0] == 4 for e in d.events[j:]):
# The rest of the flow means nothing on a provisioned board; stop before re-provisioning it.
print(
"board is already provisioned: erase the flash first (a new config name keeps the saved network)"
)
print("FAIL")
sys.exit(1)
ok &= ask(d, "info", 3, rpc(3))
ok &= ask(d, "netstate", 7, rpc(7))
# The scan sends one reply per network and ends with an empty one.
ok &= ask(d, "scan", 4, lambda e: rpc(4)(e) and e[2][1] == 0, timeout=10)
seen = any(e[1] == 4 and e[3] and e[2][0] == 4 and s in e[2] for e in d.events)
print(f"target network in scan: {seen}")
creds = bytes([len(s)]) + s
if not o.skip_bad:
bad_pw = b"wrong-password-0123456789"
ok &= ask(
d,
"provision bad password",
1,
lambda e: e[1] == 2 and e[3] and e[2][0] == 3,
timeout=60,
payload=creds + bytes([len(bad_pw)]) + bad_pw,
)
d.pump(2)
# common.yaml sets next_url, so the provisioned reply must carry a URL.
ok &= ask(
d,
"provision good",
1,
lambda e: (
rpc(1)(e) and any(x.startswith("http") for x in strings(rpc_strings(e[2])))
),
timeout=60,
payload=creds + bytes([len(w)]) + w,
)
d.pump(3)
ok &= ask(d, "state is provisioned", 2, lambda e: state(e) and e[2][0] == 4)
ok &= ask(d, "netstate", 7, rpc(7))
d.pump(2)
bad = sum(1 for e in d.events if not e[3])
# Errors outside an ask() window, boot included: only the bad password case may report one.
errors = sum(1 for e in d.events if e[1] == 2 and e[3] and e[2][0])
if errors > (0 if o.skip_bad else 1):
print(f"unexpected error frames: {errors}")
ok = False
reboots = d.boots - boots_at_start
if (d.reopens or reboots) and not o.allow_reset:
ok = False
print(
f"frames={len(d.events)} badframes={bad} reopens={d.reopens} reboots={reboots} loglines={d.loglines} "
f"(before stray {before})"
)
print("PASS" if ok and not bad else "FAIL")
sys.exit(0 if ok and not bad else 1)
if __name__ == "__main__":
main()
@@ -1,23 +0,0 @@
#!/bin/bash
# run.sh NAME [improvcli.py args...]: build NAME.yaml (next to this script, or a path) from TREE, erase the flash so no
# credentials are saved, flash it over serial and run improvcli.py on PORT. Output lands in out-NAME/.
# env: TREE (checkout under test), PORT, PY (checkout python), SECRETS (secrets.yaml with wifi_ssid, wifi_password),
# BUILD_LOCK (optional); builds go through ../pio_build.sh with a private PlatformIO directory
# ESP chips only (esptool erase). On LibreTiny or RP2040 erase and flash with that platform's tools, then run
# improvcli.py directly. Exit status is improvcli.py's: 0 pass, 1 fail, 2 setup failed.
set -o pipefail
D=$(cd "$(dirname "$0")" && pwd); N=${1:?name}; shift
W=${TREE:?set TREE}; PORT=${PORT:?set PORT}; S=${SECRETS:?set SECRETS}; PY=${PY:-$W/venv/bin/python}
Y=$N; [ -f "$Y" ] || Y=$D/$N.yaml; Y=$(cd "$(dirname "$Y")" && pwd)/$(basename "$Y"); N=$(basename "$Y" .yaml)
O=$D/out-$N; mkdir -p "$O"
REV=$(git -C "$W" rev-parse --short=10 HEAD) || { echo "TREE is not a git checkout: $W"; exit 2; }
echo "== $N $REV"
# the erase runs while the firmware compiles
$PY -m esptool --port "$PORT" erase_flash > "$O/erase.log" 2>&1 &
ERASE=$!
"$(dirname "$D")/pio_build.sh" compile "$N" "$Y" || { wait $ERASE; exit 2; }
wait $ERASE || { echo "$N erase failed"; exit 2; }
"$(dirname "$D")/pio_build.sh" upload "$N" "$Y" "$PORT" || exit 2
sleep 1
rm -f "$O/raw.bin"
$PY "$D/improvcli.py" "$PORT" "$O/raw.bin" --secrets "$S" "$@" 2>&1 | tee "$O/improv.txt"
@@ -1,27 +0,0 @@
#!/bin/bash
# pio_build.sh compile|upload NAME YAML [DEVICE] [esphome options...]: esphome compile or upload of YAML with
# `-s name NAME`, built from TREE with a private PlatformIO core directory. Logs to <yaml dir>/NAME-<action>.log and
# prints the RAM and Flash lines (compile) or the upload result. Exits 1 on a failure or when esphome would not import
# from TREE. A config that sets `name: ${name}` builds one copy per NAME, so a PR and a dev build of the same file
# never share a build directory.
# env: TREE (checkout under test), PY (default $TREE/venv/bin/python), PIO_CORE (default <yaml dir>/pio),
# BUILD_LOCK (optional command prefix that serialises builds on a shared machine; compile only)
A=${1:?compile|upload}; N=${2:?name}; Y=${3:?yaml}; shift 3
T=${TREE:?set TREE}; PY=${PY:-$T/venv/bin/python}
Y=$(cd "$(dirname "$Y")" && pwd)/$(basename "$Y"); D=$(dirname "$Y")
export PLATFORMIO_CORE_DIR=${PIO_CORE:-$D/pio} PYTHONPATH=$T PYTHONUNBUFFERED=1
L=$D/$N-$A.log
F=$(cd "$T" && $PY -c 'import esphome, os; print(os.path.dirname(esphome.__file__))')
[ "$F" = "$(cd "$T" && pwd)/esphome" ] || { echo "esphome imports from $F, not $T"; exit 1; }
echo "esphome from $F" > "$L"
case $A in
compile)
(cd "$T" && ${BUILD_LOCK:-} timeout 1500 $PY -m esphome -s name "$N" "$@" compile "$Y") >> "$L" 2>&1
RC=$?; grep -a -E "^(RAM|Flash):" "$L"; [ $RC = 0 ] || { echo "$N compile failed (see $L)"; exit 1; } ;;
upload)
DEV=${1:?device}; shift
(cd "$T" && timeout 600 $PY -m esphome -s name "$N" "$@" upload "$Y" --device "$DEV") >> "$L" 2>&1 ||
{ echo "$N upload failed (see $L)"; tail -3 "$L"; exit 1; }
grep -a -E "OTA successful|Successfully uploaded" "$L" | tail -1 ;;
*) echo "unknown action $A"; exit 1 ;;
esac
@@ -1,16 +1,8 @@
# Shared by pa.yaml and pc.yaml: Pico 2 W, pioscope.hpp, PIO1 memory cleared at boot, an ALIVE line every 2 s and
# the s_ws_p0 strip on pin 2.
# Shared by pa.yaml and pc.yaml: Pico 2 W, pioscope.hpp, an ALIVE line every 2 s and the s_ws_p0 strip on pin 2.
esphome:
name: ${name}
includes:
- pioscope.hpp
on_boot:
# PIO instruction memory survives a reflash; clear PIO1 so a program left by the previous firmware cannot hide a
# missing pio_add_program
- priority: 1000
then:
- lambda: |-
for (int i = 0; i < 32; i++) pio1->instr_mem[i] = 0;
rp2040:
board: rpipico2w
@@ -1,10 +0,0 @@
#!/bin/bash
# resolve.sh NAME [TRIES]: print the address of NAME.local once it resolves and answers a ping, retrying every 3 s
# (default 30 tries). Exits 1 when it never does.
for _ in $(seq 1 "${2:-30}"); do
IP=$(python3 -c 'import socket, sys; print(socket.gethostbyname(sys.argv[1]))' "$1.local" 2>/dev/null) &&
ping -c 1 "$IP" > /dev/null 2>&1 && { echo "$IP"; exit 0; }
sleep 3
done
echo "$1.local did not resolve or answer" >&2
exit 1
@@ -1,25 +0,0 @@
# RP2040/RP2350 web_server, API and BLE proxy A/B (#22671)
Configs take their name from the command line (`-s name ...` through `../pio_build.sh`), so a PR build and a dev build
of the same file never share a build directory: `picow.yaml` (Pico W with an active bluetooth_proxy), `pico2w.yaml`,
`evb.yaml` (W5100S-EVB-Pico on Ethernet), `picow-ap.yaml` (fallback AP with the AP client count from `appeek.hpp`) and
`client.yaml` (ESP32 that provisions it). `rp-common.yaml` adds the encrypted API, web_server and a restart button to
`templates/diagnostics.yaml`. Put `secrets.yaml` and `apikey.txt` (the `api_key` secret) in this directory; both are
git-ignored.
The runners read `TREE` and `PY` from the environment, plus `PICO_SN` (the Pico's USB serial number), `PEER_MAC` (the
`ble_notify/server.yaml` GATT server) and `CLIENT_PORT`/`CLIENT_TREE` where noted. `staflow.sh`, `bleseq.sh`,
`apflow.sh` and `churn.sh` end with PASS or FAIL and exit 1 on FAIL; `sseabc.sh` and `sserun.sh` are diagnostic and
only print counts.
- `staflow.sh NAME CONFIG BLE`: USB flash, API soak with page loads, 30 s SSE, OTA, restart, short soak, and with
BLE=1 a 180 s GATT hold.
- `bleseq.sh NAME`: 180 s GATT hold through the Pico W proxy (`gatthold.py`), then 4 short reconnects.
- `apflow.sh NAME`: fallback AP on the Pico W, the ESP32 client provisions it, the AP client count is logged.
- `sseabc.sh HOST TAG`: SSE alone, with the API soak, and with page loads (state events per 5 s).
- `sserun.sh HOST TAG SECS MODE N`: N SSE streams with a pcap, a netstat poll, the API log (`apilog.py`) and probes
after each; `pcapsum.py PCAP IP` summarizes the pcap and `stallctr.py PCAP IP APILOG` lines the RPAsyncTCP counters
up with each stall.
- `H=<device ip> churn.sh TAG SECS`: `/events` connect and disconnect churn, reboots from the reported uptime, then
the crash report through `../crash_report/peek.py`.
- `soak.py HOST SECONDS LOG`: states, drops, uptime resets, reset reason, crash and assert lines from the API logs.
@@ -1,39 +0,0 @@
#!/bin/zsh
# apflow.sh NAME: Pico W fallback AP (picow-ap.yaml as NAME; use a fresh NAME per build) and an ESP32 client
# (client.yaml) that provisions it through /wifisave. Prints the AP client count changes, the provisioning and crash
# or reboot markers. PASS needs a client on the AP, status 200 for the real save, the DUT on the station network
# with its AP disabled, no crash line and a capture that lasted the run; exits 1 otherwise.
# env: TREE, PY, PICO_SN, CLIENT_PORT, CLIENT_TREE (checkout for the client build)
D=${0:A:h}; N=$1; S=$D/..; T=${TREE:?set TREE}; PY=${PY:-$T/venv/bin/python}
SN=${PICO_SN:?set PICO_SN}; CPORT=${CLIENT_PORT:?set CLIENT_PORT}; CTREE=${CLIENT_TREE:?set CLIENT_TREE}
FAILS=0; fail() { echo "FAIL: $*"; FAILS=$((FAILS + 1)); }
# both builds first, so the DUT capture only has to cover the provisioning
$S/pio_build.sh compile $N $D/picow-ap.yaml || exit 1
TREE=$CTREE $S/pio_build.sh compile $N-client $D/client.yaml || exit 1
PORT=$($PY $S/ports.py --wait 2e8a --serial $SN --timeout 30) || { echo "FAIL: no pico"; exit 1; }
$S/pio_build.sh upload $N $D/picow-ap.yaml $PORT || exit 1
PORT=$($PY $S/ports.py --wait 2e8a --serial $SN --timeout 60) || { echo "FAIL: pico gone after the upload"; exit 1; }
L=$D/$N-serial.log; CL=$D/$N-client.log
$PY $S/serial_capture.py $PORT 600 $L 2> $D/$N-cap.err &
CAP=$!; trap 'kill $CAP $CCAP 2>/dev/null' EXIT
n=0; until grep -a -q "Starting fallback AP\|Captive portal" $L; do sleep 2; n=$((n+2)); [ $n -ge 150 ] && { echo "FAIL: the AP never came up"; exit 1; }; done
echo "AP up after ${n}s"
TREE=$CTREE $S/pio_build.sh upload $N-client $D/client.yaml $CPORT || exit 1
$PY $S/serial_capture.py $CPORT 200 $CL --reset 2> $D/$N-ccap.err &
CCAP=$!
n=0; until grep -a -q "real save status" $CL; do sleep 2; n=$((n+2)); [ $n -ge 180 ] && { echo "FAIL: the client never saved"; exit 1; }; done
echo "client posted after ${n}s; settling 60s"; sleep 60
kill -0 $CAP 2>/dev/null || fail "the DUT capture ended before the run did (see $D/$N-cap.err)"
kill $CAP $CCAP 2>/dev/null; wait 2>/dev/null; trap - EXIT
echo "--- DUT"
grep -a -E "AP clients|Starting fallback AP|Requested WiFi|Connected\$|IP Address|Disabling AP|CRASH|panic|HardFault|Log initialized|port lost" $L | sed -E 's/(IP Address: )[0-9.]+/\1<ip>/' | cut -c1-120 | head -40
echo "--- client"
grep -a -E "connected, ip|save status|posting" $CL | sed -E 's/ip [0-9.]+/ip <ip>/' | head
grep -a -q "AP clients [1-9]" $L || fail "the DUT never counted a client on its AP"
grep -a -q "real save status 200" $CL || fail "the real save did not return 200"
grep -a -q "Disabling AP" $L || fail "the DUT never disabled its AP"
# the fallback AP logs its own 192.168.4.x address too, so only another address shows the station side
grep -a "IP Address" $L | grep -v -q "192\.168\.4\." || fail "the DUT never got a station address"
CRASH=$(grep -a -c -E "CRASH|panic|HardFault" $L); [ "$CRASH" = 0 ] || fail "$CRASH crash lines"
[ $FAILS = 0 ] || exit 1
echo PASS
@@ -1,55 +0,0 @@
"""apilog.py HOST SECONDS OUT: subscribe VERBOSE logs and states for SECONDS with ms timestamps, reconnecting on a drop.
env: API_KEY (unset for a plaintext API). Prints the state and drop counts at the end; exits 1 on a drop or when no
state arrived, so an unexercised API is not read as a working one."""
import asyncio
import contextlib
import pathlib
import sys
import time
from aioesphomeapi import LogLevel
sys.path.insert(0, str(pathlib.Path(__file__).resolve().parents[1]))
from apiconn import connect, log_text, stop_event # noqa: E402
def ts() -> str:
return f"{time.time():.3f}"
async def main(host: str, secs: float, outf: str) -> None:
end = time.monotonic() + secs
states = drops = 0
with pathlib.Path(outf).open("a", encoding="utf-8") as out:
def w(line: str) -> None:
out.write(f"{ts()} {line}\n")
out.flush()
def on_state(_s) -> None:
nonlocal states
states += 1
while time.monotonic() < end:
stopped, on_stop = stop_event()
cli = await connect(host, tries=60, timeout=15, on_stop=on_stop)
w(f"API connected {(await cli.device_info()).name}")
cli.subscribe_states(on_state)
cli.subscribe_logs(
lambda m: w(log_text(m)), log_level=LogLevel.LOG_LEVEL_VERBOSE
)
with contextlib.suppress(TimeoutError):
await asyncio.wait_for(stopped.wait(), max(0.0, end - time.monotonic()))
if stopped.is_set():
drops += 1
w("API DROPPED")
with contextlib.suppress(Exception):
await cli.disconnect()
w(f"API done states={states} drops={drops}")
print(f"api states={states} drops={drops}")
if drops or not states:
sys.exit(1)
asyncio.run(main(sys.argv[1], float(sys.argv[2]), sys.argv[3]))
@@ -1,14 +0,0 @@
#pragma once
// Reads the protected WiFiComponent::ap_clients_ for the test: explicit instantiation skips access checks.
#include "esphome/components/wifi/wifi_component.h"
namespace appeek {
struct Tag {
using type = uint8_t esphome::wifi::WiFiComponent::*;
friend type get(Tag);
};
template<typename T, typename T::type M> struct Rob {
friend typename T::type get(T) { return M; }
};
template struct Rob<Tag, &esphome::wifi::WiFiComponent::ap_clients_>;
inline int ap_clients() { return esphome::wifi::global_wifi_component->*get(Tag{}); }
} // namespace appeek
@@ -1,33 +0,0 @@
#!/bin/zsh
# bleseq.sh NAME: 180 s GATT hold through the Pico W proxy NAME (built by staflow.sh), then 4 short holds 10 s apart,
# with the serial log captured. PASS needs every hold to pass, no crash line, boot or port loss and a capture that
# lasted the run.
# env: TREE, PY, PICO_SN, PEER_MAC (the ble_notify server); apikey.txt here
D=${0:A:h}; N=$1; S=$D/..; T=${TREE:?set TREE}; PY=${PY:-$T/venv/bin/python}; H=$(cat $D/$N-host.txt)
FAILS=0; fail() { echo "FAIL: $*"; FAILS=$((FAILS + 1)); }
export API_KEY=$(cat $D/apikey.txt)
PORT=$($PY $S/ports.py --wait 2e8a --serial ${PICO_SN:?set PICO_SN} --timeout 10) || { echo "FAIL: no pico"; exit 1; }
L=$D/$N-bleseq-serial.log
$PY $S/serial_capture.py $PORT 420 $L 2> $D/$N-bleseq-cap.err &
CAP=$!; trap 'kill $CAP 2>/dev/null' EXIT
sleep 2
timeout 300 $PY -I $D/gatthold.py $H ${PEER_MAC:?set PEER_MAC} 180 > $D/$N-hold.txt 2>&1 || fail "the 180 s hold"
grep -v '^uuids' $D/$N-hold.txt
for i in 1 2 3 4; do
sleep 10
timeout 90 $PY -I $D/gatthold.py $H $PEER_MAC 15 > $D/$N-hold$i.txt 2>&1 || fail "short hold $i"
grep -E "PASS|FAIL|held" $D/$N-hold$i.txt | tr '\n' ' '; echo
done
sleep 3
kill -0 $CAP 2>/dev/null || fail "the capture ended before the run did (see $D/$N-bleseq-cap.err)"
[ "$(grep -a -c -v '^\[[0-9:.]*\]\[capture\]' $L)" -gt 0 ] || fail "no firmware lines in $L"
kill $CAP; wait $CAP 2>/dev/null; trap - EXIT
CRASH=$(grep -a -c -i -E 'CRASH|panic|HardFault|assert' $L)
echo "serial: crash/assert $CRASH boots $(grep -a -c 'ESPHome version' $L) notify-response warnings $(grep -a -c 'Failed to send notify' $L) connects $(grep -a -c 'Connecting v3' $L)"
grep -a -E "Disconnected, reason|Connection failed|status" $L | cut -c1-120 | sort | uniq -c | sort -rn | head -5
[ "$CRASH" = 0 ] || fail "$CRASH crash lines"
# the capture opens on a running proxy, so any boot or USB drop is a reboot the next hold's retry would hide
BOOTS=$(grep -a -c 'ESPHome version' $L); LOSSES=$(grep -a -c 'port lost' $L)
[ "$BOOTS" = 0 ] && [ "$LOSSES" = 0 ] || fail "$BOOTS boots and $LOSSES port losses during the holds"
[ $FAILS = 0 ] || exit 1
echo PASS
@@ -1,24 +0,0 @@
#!/bin/zsh
# H=<device ip> churn.sh TAG SECS: 4 parallel loops of short /events connections (0.2 to 1.5 s) for SECS, logging the
# uptime each stream reports; counts reboots from the uptime going backwards, then reads the crash report over the API
# with crash_report/peek.py. PASS needs streams that got data (at least as many as got nothing), no reboot and no
# replayed crash record; exits 1 otherwise. env: H, TREE or PY, API_KEY (unset for a plaintext API)
D=${0:A:h}; T=$1; S=$2; H=${H:?set H to the device address}; PY=${PY:-${TREE:?set TREE or PY}/venv/bin/python}; cd $D
rm -f $T-churn*.txt
end=$(( $(date +%s) + S ))
for k in 1 2 3 4; do
( while (( $(date +%s) < end )); do m=0.$(( RANDOM % 9 + 2 )); [[ $((RANDOM % 3)) == 0 ]] && m=1.5
r=$(curl -s -N --connect-timeout 1 -m $m http://$H/events 2>/dev/null | grep -a -o '"uptime":[0-9]*' | head -1)
echo "$(date +%s) ${r:-none}"; done > $T-churn$k.txt ) &
done; wait
cat $T-churn*.txt | sort -n > $T-churn.txt
FAILS=0; fail() { echo "FAIL: $*"; FAILS=$((FAILS + 1)); }
read -r OK NONE REBOOTS < <(awk '$2 ~ /uptime/ {split($2,a,":"); b=$1-a[2]; if (pb!="" && b-pb>3) n++; pb=b; c++} $2=="none"{x++} END{print c+0, x+0, n+0}' $T-churn.txt)
echo "streams_with_ping=$OK none=$NONE reboots=$REBOOTS"
[ "$OK" -gt 0 ] && [ "$OK" -ge "$NONE" ] || fail "most streams got nothing (the stimulus did not reach the device)"
[ "$REBOOTS" = 0 ] || fail "$REBOOTS reboots"
PEEK=$($PY -I $D/../crash_report/peek.py $H) || fail "reading the crash report"
echo "crash report: $PEEK"
[[ $PEEK == *"replay lines 0"* ]] || fail "a crash record was replayed"
[ $FAILS = 0 ] || exit 1
echo PASS
@@ -1,12 +0,0 @@
# Classic ESP32 that joins the Pico W's fallback AP and provisions it once through /wifisave (name set with -s name).
substitutions:
ap_ssid: rp2webap
real_ssid: !secret wifi_ssid
real_psk: !secret wifi_password
bogus_saves: "1"
esp32:
board: pico32
framework:
type: esp-idf
packages:
hwtest: !include ../../templates/client-wifisave.yaml
@@ -1,13 +0,0 @@
# WIZnet W5100S-EVB-Pico on wired Ethernet (name set with -s name).
rp2:
board: wiznet_5100s_evb_pico
ethernet:
type: W5100
clk_pin: 18
mosi_pin: 19
miso_pin: 16
cs_pin: 17
interrupt_pin: 21
reset_pin: 20
packages:
common: !include rp-common.yaml
@@ -1,89 +0,0 @@
"""Through a bluetooth_proxy: connect to the ble_notify GATT server (server.yaml), read both characteristics, enable
notify on both (writing the CCCD like Home Assistant), hold for HOLD seconds re-reading every 3 s, then disconnect.
usage: gatthold.py PROXY PEER_MAC HOLD env: API_KEY, ALLOW_UUID_REMAP=1 (accept the pre-#22679 short UUIDs).
Prints counts; exits 1 on a lost link, a failed read or a short UUID reported in place of the real one."""
import asyncio
import os
import pathlib
import sys
import time
sys.path.insert(0, str(pathlib.Path(__file__).resolve().parents[1]))
from apiconn import ble_connect, connect, enable_notify # noqa: E402
NUM = "5ec10001-0000-1000-8000-00805f9b34fb"
TXT = "5ec10002-0000-1000-8000-00805f9b34fb"
async def main(host: str, peer: str, hold: float) -> None:
addr = int(peer.replace(":", ""), 16)
cli = await connect(host)
lost: list[tuple[float, int]] = []
closing = False
def on_lost(err: int) -> None:
if not closing:
lost.append((time.monotonic(), err))
print(f"LINK LOST err={err}", flush=True)
t = time.monotonic()
c, mtu, err, cancel = await ble_connect(cli, addr, on_lost)
if not c:
sys.exit(f"FAIL connect err={err}")
print(f"connected in {time.monotonic() - t:.1f}s mtu={mtu}", flush=True)
svcs = await cli.bluetooth_gatt_get_services(addr)
chars = {ch.uuid.lower(): ch for s in svcs.services for ch in s.characteristics}
print("uuids", sorted(chars), flush=True)
print(f"services {len(svcs.services)} characteristics {len(chars)}", flush=True)
# before #22679 the rp2 proxy reported a 32-bit base UUID as its low 16 bits (5ec10001 -> 00000001)
remapped = 0
for u in (NUM, TXT):
if u not in chars and "0000" + u[4:] in chars:
remapped += 1
print(f"UUID {u[:8]} reported as {'0000' + u[4:8]}", flush=True)
chars[u] = chars["0000" + u[4:]]
got = {NUM: 0, TXT: 0}
reads = fails = empty = 0
samples: list[bytes] = []
for u in (NUM, TXT):
if not await enable_notify(
cli, addr, chars[u], lambda h, d, u=u: got.__setitem__(u, got[u] + 1)
):
sys.exit(f"FAIL: {u[:8]} lists no CCCD")
end = time.monotonic() + hold
while time.monotonic() < end and not lost:
for u in (NUM, TXT):
try:
v = bytes(await cli.bluetooth_gatt_read(addr, chars[u].handle))
reads += 1
empty += not v
if v and len(samples) < 4:
samples.append(v)
except Exception as e: # noqa: BLE001
fails += 1
print(f"read {u[4:8]} FAILED {type(e).__name__}: {e}", flush=True)
# 3 s, so a hold covers several of the server's 10 s empty-value cycles
await asyncio.sleep(3)
print(
f"held {hold:.0f}s: notifications num={got[NUM]} txt={got[TXT]} reads ok={reads} failed={fails} "
f"link losses={len(lost)} empty reads={empty} uuid_remapped={remapped} samples={samples}",
flush=True,
)
closing = True
await cli.bluetooth_device_disconnect(addr)
await asyncio.sleep(2)
cancel()
await cli.disconnect()
# server.yaml empties both values on every 5th 2 s round, so about one read in five is legitimately empty; the
# client cannot tell which round it hit, so only a majority of empty reads counts as a proxy returning no data
if remapped and os.environ.get("ALLOW_UUID_REMAP") != "1":
sys.exit(
f"FAIL: {remapped} UUIDs reported in short form (ALLOW_UUID_REMAP=1 accepts them)"
)
if lost or fails or not got[NUM] or not got[TXT] or empty * 2 > reads:
sys.exit("FAIL")
print("PASS", flush=True)
asyncio.run(main(sys.argv[1], sys.argv[2], float(sys.argv[3])))
@@ -1,112 +0,0 @@
"""pcapsum.py PCAP DEVICE_IP: per TCP connection to port 80, device data segments, last data time and seq, retransmits,
FIN/RST and who sent them, windows, and anything sent by the device after its last data. Uses tcpdump -tt text.
Exits 1 when the capture has no TCP packet. packets() is shared with stallctr.py."""
from collections.abc import Iterator
import re
import subprocess
import sys
RX = re.compile(
r"^(\d+\.\d+) IP (\S+)\.(\d+) > (\S+)\.(\d+): Flags \[([^\]]*)\](?:, seq (\d+)(?::(\d+))?)?(?:, ack (\d+))?, win (\d+).*length (\d+)"
)
def packets(pcap: str, dev: str) -> Iterator[dict]:
"""Each TCP packet in PCAP as a dict; port is the host side's port, from_dev tells the direction."""
r = subprocess.run(
["tcpdump", "-n", "-tt", "-S", "-r", pcap],
capture_output=True,
text=True,
check=False,
)
if r.returncode != 0:
sys.exit(f"tcpdump -r {pcap} failed: {r.stderr.strip()}")
skipped = parsed = 0
for line in r.stdout.splitlines():
m = RX.match(line)
if not m:
skipped += " IP " in line and "Flags [" in line
continue
parsed += 1
t, sip, sp, _dip, dp, fl, s1, s2, ack, win, ln = m.groups()
from_dev = sip == dev
yield {
"t": float(t),
"from_dev": from_dev,
"port": dp if from_dev else sp,
"flags": fl,
"seq": (s1, s2),
"ack": int(ack) if ack else None,
"win": int(win),
"len": int(ln),
"line": line,
}
if skipped:
# on stdout, so a summary built from part of the capture says so
print(f"warning: {skipped} TCP lines did not parse", flush=True)
if skipped and not parsed:
sys.exit("no TCP line parsed; the tcpdump output format differs")
def main(pcap: str, dev: str) -> None:
conns: dict[str, dict] = {}
for k in packets(pcap, dev):
t = k["t"]
c = conns.setdefault(
k["port"],
{
"t0": t,
"data": 0,
"bytes": 0,
"last_data": None,
"last_seq": None,
"seen": set(),
"retx": 0,
"fin": [],
"rst": [],
"cwin": [],
"dwin": [],
"after": [],
"last_ack_from_client": None,
"http": None,
},
)
if "GET /" in k["line"] and not k["from_dev"]:
c["http"] = k["line"].split("HTTP: ")[-1][:20]
if k["from_dev"]:
c["dwin"].append(k["win"])
if k["len"] > 0:
if k["seq"] in c["seen"]:
c["retx"] += 1
c["seen"].add(k["seq"])
c["data"] += 1
c["bytes"] += k["len"]
c["last_data"] = t
c["last_seq"] = int(k["seq"][1])
c["after"] = []
else:
c["after"].append((round(t - c["t0"], 3), k["flags"]))
else:
c["cwin"].append(k["win"])
if k["ack"] is not None:
c["last_ack_from_client"] = k["ack"]
who = "dev" if k["from_dev"] else "host"
if "F" in k["flags"]:
c["fin"].append((who, round(t - c["t0"], 3)))
if "R" in k["flags"]:
c["rst"].append((who, round(t - c["t0"], 3)))
if not conns:
sys.exit(f"no TCP packets in {pcap}")
for p, c in conns.items():
ld = round(c["last_data"] - c["t0"], 3) if c["last_data"] else None
print(
f"port {p} {c['http']}: dev data segs={c['data']} bytes={c['bytes']} last_data=+{ld}s "
f"last_dev_seq_end={c['last_seq']} host_last_ack={c['last_ack_from_client']} retx={c['retx']} "
f"FIN={c['fin']} RST={c['rst']} host_win=[{min(c['cwin'] or [0])},{max(c['cwin'] or [0])}] "
f"dev_win=[{min(c['dwin'] or [0])},{max(c['dwin'] or [0])}] dev_ctrl_after_last_data={c['after'][:4]}"
)
if __name__ == "__main__":
main(sys.argv[1], sys.argv[2])
@@ -1,8 +0,0 @@
# Pico 2 W on the station network (name set with -s name).
rp2:
board: rpipico2w
wifi:
ssid: !secret wifi_ssid
password: !secret wifi_password
packages:
common: !include rp-common.yaml
@@ -1,18 +0,0 @@
# Pico W that falls back to its AP; logs the AP client count when it changes (appeek.hpp). Set a fresh name per build
# with -s name so no saved network carries over.
substitutions:
ap_ssid: rp2webap
rp2:
board: rpipicow
esphome:
includes: [appeek.hpp]
packages:
hwtest: !include ../../templates/dut-fallback-ap.yaml
interval:
- interval: 2s
then:
- lambda: |-
static int last = -1;
int n = appeek::ap_clients();
if (n != last) ESP_LOGI("test", "AP clients %d", n);
last = n;
@@ -1,11 +0,0 @@
# Pico W on the station network with an active bluetooth_proxy (name set with -s name).
rp2:
board: rpipicow
wifi:
ssid: !secret wifi_ssid
password: !secret wifi_password
rp2_ble_tracker:
bluetooth_proxy:
active: true
packages:
common: !include rp-common.yaml
@@ -1,13 +0,0 @@
# Shared by every station and Ethernet build: encrypted API, web_server and a restart button on top of the
# diagnostics package (5 Hz state traffic, uptime, heap, reset reason). Substitution: name (set with -s name).
packages:
diagnostics: !include ../../templates/diagnostics.yaml
api:
encryption:
key: !secret api_key
reboot_timeout: 0s
web_server:
port: 80
button:
- platform: restart
name: Restart
@@ -1,80 +0,0 @@
"""API soak: subscribe states and logs for SECONDS, reconnect on drops, report the state count, uptime resets
(reboots), reset reason and crash or assert lines. usage: soak.py HOST SECONDS LOGFILE env: API_KEY
Exits 1 on a drop, an uptime reset, a crash or assert line, no states at all or no uptime reading."""
import asyncio
import contextlib
import pathlib
import re
import sys
import time
from aioesphomeapi import LogLevel
from aioesphomeapi.model import SensorState, TextSensorState
sys.path.insert(0, str(pathlib.Path(__file__).resolve().parents[1]))
from apiconn import connect, log_text, stop_event # noqa: E402
BAD = re.compile(r"CRASH|panic|assert|HardFault|LWIP|lock not held", re.IGNORECASE)
async def main(host: str, secs: float, logf: str) -> None:
end = time.monotonic() + secs
states = drops = reboots = 0
last_up = None
reasons: set[str] = set()
bad: list[str] = []
with pathlib.Path(logf).open("a", encoding="utf-8") as out:
def w(line: str) -> None:
out.write(f"{time.strftime('%T')} {line}\n")
while time.monotonic() < end:
stopped, on_stop = stop_event()
cli = await connect(host, tries=60, timeout=15, on_stop=on_stop)
info = await cli.device_info()
w(f"connected {info.name} {info.esphome_version} {info.compilation_time}")
ents, _ = await cli.list_entities_services()
names = {e.key: e.object_id for e in ents}
def on_state(s, names: dict[int, str] = names) -> None:
nonlocal states, last_up, reboots
states += 1
n = names.get(s.key, "")
if isinstance(s, SensorState) and n == "uptime" and not s.missing_state:
if last_up is not None and s.state + 5 < last_up:
reboots += 1
w(f"UPTIME RESET {last_up} -> {s.state}")
last_up = s.state
if isinstance(s, TextSensorState) and n == "reset_reason" and s.state:
reasons.add(s.state)
def on_log(m) -> None:
line = log_text(m)
w(line)
if BAD.search(line):
bad.append(line)
cli.subscribe_states(on_state)
cli.subscribe_logs(on_log, log_level=LogLevel.LOG_LEVEL_DEBUG)
with contextlib.suppress(TimeoutError):
await asyncio.wait_for(stopped.wait(), max(0.0, end - time.monotonic()))
if stopped.is_set():
drops += 1
w("DROPPED")
with contextlib.suppress(Exception):
await cli.disconnect()
print(
f"states={states} drops={drops} uptime_resets={reboots} last_uptime={last_up} "
f"reset_reason={sorted(reasons)} bad_lines={len(bad)}"
)
for b in bad[:10]:
print("BAD", b)
if last_up is None:
print("no uptime state seen, so reboots were not checked")
if drops or reboots or bad or not states or last_up is None:
sys.exit("FAIL")
print("PASS")
asyncio.run(main(sys.argv[1], float(sys.argv[2]), sys.argv[3]))
@@ -1,13 +0,0 @@
#!/bin/zsh
# sseabc.sh HOST TAG: 40 s SSE streams with (A) the API soak, (B) page loads every 5 s, (C) nothing else; state events
# per 5 s. Diagnostic: it prints counts (and the soak's PASS or FAIL line) for reading, with no verdict of its own.
# env: TREE or PY, API_KEY (or apikey.txt here)
D=${0:A:h}; H=$1; T=$2; PY=${PY:-${TREE:?set TREE or PY}/venv/bin/python}
[ -n "$API_KEY" ] || export API_KEY=$(cat $D/apikey.txt)
bins() { awk 'NR==1{t0=$1} /event: state/ {c[int(($1-t0)/5)]++} END {for(i=0;i<8;i++) printf "%d ", c[i]; print ""}' $1; }
(curl -s -N -m 40 http://$H/events | $PY -I $D/ts.py > $D/$T-A.txt) & $PY -I $D/soak.py $H 40 $D/$T-A-soak.log | tail -1; wait
echo "A sse+api states $(grep -c 'event: state' $D/$T-A.txt) per5s: $(bins $D/$T-A.txt)"
(curl -s -N -m 40 http://$H/events | $PY -I $D/ts.py > $D/$T-B.txt) & (for i in 1 2 3 4 5 6 7; do curl -s -o /dev/null -m 10 http://$H/; sleep 5; done); wait
echo "B sse+pages states $(grep -c 'event: state' $D/$T-B.txt) per5s: $(bins $D/$T-B.txt)"
curl -s -N -m 40 http://$H/events | $PY -I $D/ts.py > $D/$T-C.txt
echo "C sse alone states $(grep -c 'event: state' $D/$T-C.txt) per5s: $(bins $D/$T-C.txt)"
@@ -1,40 +0,0 @@
#!/bin/zsh
# sserun.sh HOST TAG SECS MODE(alone|api|pages) [N] : N sequential SSE streams (curl -m SECS each) with a pcap, a 1 s
# netstat poll and optional API log stream or page loads; after each stream ends, a fresh /events and GET / probe.
# Exits 1 when the API log stream or the API check after the streams (api, apipages) failed.
# env: TREE or PY, IFACE (default en0), API_KEY or apikey.txt here; tcpdump must be allowed to capture (bpf group on macOS)
setopt null_glob
D=${0:A:h}; H=$1; T=$2; S=$3; M=$4; N=${5:-1}
PY=${PY:-${TREE:?set TREE or PY}/venv/bin/python}; IFACE=${IFACE:-en0} # the host interface that reaches the device
cd $D
rm -f $T-*.txt $T.pcap $T-api*.log
tcpdump -i $IFACE -n -s 200 -w $T.pcap host $H and port 80 2>$T-tcpdump.err & TP=$!
sleep 1; kill -0 $TP 2>/dev/null || { echo "FAIL: tcpdump did not start: $(cat $T-tcpdump.err)"; exit 1; }
(while true; do echo "$(date +%s) $(netstat -anp tcp | grep -F "$H.80 " | tr -s ' ' | tr '\n' '|')"; sleep 1; done) > $T-netstat.txt 2>&1 & NP=$!
sleep 1
TOT=$(( (S+12)*N + 10 ))
[[ $M == api || $M == apipages ]] && { API_KEY=${API_KEY:-$(cat apikey.txt)} $PY -I apilog.py $H $TOT $D/$T-api.log > $T-apisum.txt & AP=$!; sleep 3 }
[[ $M == pages || $M == apipages ]] && { (for i in $(seq 1 $((TOT/5))); do echo "$(date +%s) $(curl -s -o /dev/null -w '%{http_code} %{time_total}' -m 10 http://$H/)"; sleep 5; done) > $T-pages.txt & PP=$!; }
for i in $(seq 1 $N); do
F=$T-sse$i.txt
( curl -s -N -m $S http://$H/events; echo "CURL_EXIT $?" ) | $PY -I ts.py > $F
awk -v i=$i 'NR==1{t0=$1} /event: state/{c++; last=$1} /event: log/{l++} /CURL_EXIT/{e=$3; te=$1} END{printf "stream %d: states=%d last_state=+%.1fs logs=%d curl_exit=%s at +%.1fs\n", i, c, last-t0, l, e, te-t0}' $F | tee -a $T-summary.txt
echo " probe fresh_events(8s)=$(curl -s -N -m 8 http://$H/events | grep -c 'event: state') get_root=$(curl -s -o /dev/null -w '%{http_code}/%{size_download}B/%{time_total}s' -m 10 http://$H/)" | tee -a $T-summary.txt
done
RC=0
# a failed API log run must say so in the summary, or an api run reads like one without API traffic
if [[ -n $AP ]]; then
wait $AP; ARC=$?
if [ $ARC = 0 ]; then cat $T-apisum.txt | tee -a $T-summary.txt
else echo "api log FAILED rc=$ARC" | tee -a $T-summary.txt; RC=1; fi
fi
[[ -n $PP ]] && kill $PP 2>/dev/null
if [[ $M == api || $M == apipages ]]; then
OUT=$(API_KEY=${API_KEY:-$(cat apikey.txt)} $PY -I apilog.py $H 8 $D/$T-apiafter.log); ARC=$?
if [ $ARC = 0 ]; then echo "api-after: $OUT" | tee -a $T-summary.txt
else echo "api-after FAILED rc=$ARC" | tee -a $T-summary.txt; RC=1; fi
fi
# a tcpdump that died mid-run leaves a short pcap that pcapsum.py would read as fewer connections
kill -0 $TP 2>/dev/null || { echo "pcap capture ended early, see $T-tcpdump.err" | tee -a $T-summary.txt; RC=1; }
kill $TP $NP 2>/dev/null; wait 2>/dev/null
exit $RC
@@ -1,52 +0,0 @@
#!/bin/zsh
# staflow.sh NAME CONFIG BLE(0/1): USB flash CONFIG (picow.yaml, pico2w.yaml) as NAME, then over Wi-Fi: API soak with
# page loads, SSE alone, OTA, restart, a short soak and (BLE=1) a GATT hold through the proxy. Serial captured
# throughout. PASS needs both soaks to pass, every page load to return 200, state events on the SSE stream, a reboot
# (lower Uptime over REST) after the OTA and after the restart, the GATT hold (BLE=1), no crash or assert line and a
# capture that lasted the run; exits 1 otherwise.
# env: TREE, PY, PICO_SN (the Pico's USB serial number), PEER_MAC (the ble_notify server, for BLE=1); apikey.txt here
D=${0:A:h}; N=$1; C=$2; BLE=${3:-0}; S=$D/..
T=${TREE:?set TREE}; PY=${PY:-$T/venv/bin/python}; SN=${PICO_SN:?set PICO_SN}
FAILS=0; fail() { echo "FAIL: $*"; FAILS=$((FAILS + 1)); }
export API_KEY=$(cat $D/apikey.txt)
$S/pio_build.sh compile $N $D/$C || exit 1
PORT=$($PY $S/ports.py --wait 2e8a --serial $SN --timeout 30) || { echo "FAIL: no pico"; exit 1; }
$S/pio_build.sh upload $N $D/$C $PORT || exit 1
PORT=$($PY $S/ports.py --wait 2e8a --serial $SN --timeout 60) || { echo "FAIL: pico gone after the upload"; exit 1; }
L=$D/$N-serial.log
$PY $S/serial_capture.py $PORT 1100 $L 2> $D/$N-cap.err &
CAP=$!; trap 'kill $CAP 2>/dev/null' EXIT
H=$($S/resolve.sh $N) || exit 1; echo "$H" > $D/$N-host.txt; sleep 5
(for i in $(seq 1 36); do curl -s -o /dev/null -w "%{http_code}\n" -m 10 http://$H/ ; sleep 5; done > $D/$N-web.txt) &
WEB=$!
$PY -I $D/soak.py $H 180 $D/$N-soak.log || fail "the 180 s API soak"
wait $WEB
echo "web: $(sort $D/$N-web.txt | uniq -c | tr '\n' ' ')"
[ "$(grep -c -v '^200$' $D/$N-web.txt)" = 0 ] && [ -s $D/$N-web.txt ] || fail "page loads other than 200"
timeout 30 curl -s -N http://$H/events > $D/$N-sse.txt
SSE=$(grep -c 'event: state' $D/$N-sse.txt); echo "sse 30s alone: states $SSE pings $(grep -c 'event: ping' $D/$N-sse.txt)"
[ "$SSE" -gt 0 ] || fail "no state events on the SSE stream"
# the Uptime sensor over REST (web_server entity URLs use the entity name); empty when there is no answer
upt() { curl -s -m 5 http://$H/sensor/Uptime | $PY -c 'import json, sys; print(int(json.load(sys.stdin)["value"]))' 2>/dev/null; }
# wait up to 120 s for the device to answer with an uptime lower than U0 (it rebooted)
rebooted() { local u; for _ in $(seq 1 60); do u=$(upt); [ -n "$1" ] && [ -n "$u" ] && [ "$u" -lt "$1" ] && return 0; sleep 2; done; return 1; }
U0=$(upt); [ -n "$U0" ] || fail "no Uptime reading over REST"
$S/pio_build.sh upload $N $D/$C $H || fail "the OTA"
rebooted $U0 || fail "no reboot seen within 120 s after the OTA"
# let the uptime grow past 30 s first, so the first reading after the restart is clearly lower
for _ in $(seq 1 30); do U0=$(upt); [ -n "$U0" ] && [ "$U0" -ge 30 ] && break; sleep 2; done
$PY -I $S/api_button.py $H "$API_KEY" press Restart > /dev/null || fail "pressing Restart"
rebooted $U0 || fail "no reboot seen within 120 s after the restart"
$PY -I $D/soak.py $H 30 $D/$N-after.log || fail "the soak after OTA and restart"
if [ $BLE = 1 ]; then
echo "--- ble"; timeout 400 $PY -I $D/gatthold.py $H ${PEER_MAC:?set PEER_MAC} 180 || fail "the GATT hold"
fi
sleep 3
kill -0 $CAP 2>/dev/null || fail "the capture ended before the run did (see $D/$N-cap.err)"
[ "$(grep -a -c -v '^\[[0-9:.]*\]\[capture\]' $L)" -gt 0 ] || fail "no firmware lines in $L"
kill $CAP; wait $CAP 2>/dev/null; trap - EXIT
CRASH=$(grep -a -c -i -E 'CRASH|panic|HardFault|assert|lock not held' $L)
echo "serial: crash/assert lines $CRASH, boots $(grep -a -c 'Log initialized\|ESPHome version' $L), port losses $(grep -a -c 'port lost' $L), lines $(wc -l < $L)"
[ "$CRASH" = 0 ] || fail "$CRASH crash or assert lines"
[ $FAILS = 0 ] || exit 1
echo PASS
@@ -1,62 +0,0 @@
"""stallctr.py PCAP DEV_IP APILOG: for each /events connection print last data and FIN wall times, and the atcp counter
line just before the last data, just after it (about +1.5 s), and about 7 s later (after the FIN). APILOG comes from
apilog.py with counter log lines tagged [atcp."""
import pathlib
import sys
sys.path.insert(0, str(pathlib.Path(__file__).resolve().parent))
from pcapsum import packets # noqa: E402
def main(pcap: str, dev: str, logf: str) -> None:
conns: dict[str, dict] = {}
for k in packets(pcap, dev):
c = conns.setdefault(
k["port"], {"t0": k["t"], "ev": False, "last": None, "fin": None}
)
if "GET /events" in k["line"]:
c["ev"] = True
if k["from_dev"] and k["len"] > 0:
c["last"] = k["t"]
if k["from_dev"] and "F" in k["flags"] and c["fin"] is None:
c["fin"] = k["t"]
logs = []
for line in pathlib.Path(logf).read_text(encoding="utf-8").splitlines():
if "[atcp" in line:
t, rest = line.split(" ", 1)
logs.append((float(t), rest.strip().split("]: ", 1)[-1]))
def at(t: float) -> tuple[float, str] | None:
best = None
for lt, s in logs:
if lt <= t:
best = (lt, s)
return best
events = sum(1 for c in conns.values() if c["ev"] and c["last"] is not None)
print(f"{events} /events connections with data, {len(logs)} atcp counter lines")
if not events or not logs:
sys.exit(
"FAIL: nothing to line up (no /events connection in the pcap or no [atcp line in the API log)"
)
for p, c in conns.items():
if not c["ev"] or c["last"] is None:
continue
fin = (
f"FIN dev +{c['fin'] - c['last']:.2f}s after last data"
if c["fin"]
else "no dev FIN"
)
print(f"conn {p}: last data at +{c['last'] - c['t0']:.2f}s, {fin}")
for lbl, t in (
("before", c["last"] - 0.01),
("+1.5s", c["last"] + 1.5),
("+7s", c["last"] + 7),
):
if a := at(t):
print(f" {lbl:7} {a[1]}")
if __name__ == "__main__":
main(*sys.argv[1:4])
@@ -1,8 +0,0 @@
"""Prefix each stdin line with a wall clock timestamp (ms)."""
import sys
import time
for line in sys.stdin.buffer:
sys.stdout.write(f"{time.time():.3f} {line.decode(errors='replace')}")
sys.stdout.flush()
@@ -1,59 +0,0 @@
# ESP32 Pico kit: trf7962a driven by a fake chip (trfsim.hpp) behind the SPI delegates; the phase table in trfsim.hpp
# runs from on_boot (name set with -s name). The 1 ms interval tick is the validated timing source; keep it.
esphome:
name: ${name}
includes: [trfsim.hpp]
on_boot:
- priority: 800
then:
- lambda: trfsim::install(id(spibus), 5, 4);
- priority: -100
then:
- lambda: trfsim::late_pin_setup();
- delay: 2s
- script.execute: scenarios
esp32:
board: pico32
framework:
type: esp-idf
logger:
level: DEBUG
spi:
id: spibus
clk_pin: GPIO18
mosi_pin: GPIO23
miso_pin: GPIO19
trf7962a:
id: nfcc
cs_pin: GPIO5
irq_pin: GPIO4
vin: 3.3v
rf_power: half
on_tag:
then:
- lambda: 'ESP_LOGI("ev", "EV on_tag %s type=%s", x.c_str(), tag.get_tag_type().c_str());'
on_tag_removed:
then:
- lambda: 'ESP_LOGI("ev", "EV on_tag_removed %s", x.c_str());'
binary_sensor:
- platform: nfc
nfcc_id: nfcc
name: tagA
uid: E0-04-01-00-00-00-00-01
on_state:
- lambda: 'ESP_LOGI("ev", "EV bs_tagA %s", x ? "ON" : "OFF");'
interval:
- interval: 1ms
then:
- lambda: trfsim::tick();
script:
- id: scenarios
then:
- lambda: |-
ESP_LOGI("ev", "SETUP failed=%d", id(nfcc).is_failed());
trfsim::stats("boot");
- repeat:
count: !lambda return trfsim::NUM_PHASES;
then:
- delay: !lambda return trfsim::start_phase(iteration);
- lambda: 'trfsim::stats(trfsim::PHASES[trfsim::NUM_PHASES - 1].name); ESP_LOGI("ev", "DONE");'
@@ -1,382 +0,0 @@
#pragma once
// Fake TRF7962A behind the SPI delegates: a FakeBus replaces the SPI component's bus before the reader's setup(),
// so both of the driver's SPI clients (MODE0 writes, MODE1 reads) talk to one register/FIFO/IRQ model.
#include <driver/gpio.h>
#include <soc/gpio_reg.h>
#include "esphome/components/spi/spi.h"
#include "esphome/core/hal.h"
#include "esphome/core/log.h"
namespace trfsim {
static constexpr const char *const TAG = "trfsim";
// Scenario of the tag in the field
enum Tag : uint8_t { NONE, TAG_A, TAG_B, SLOW_A, FLAKY_A, COLLIDE, CRC_ERR, ERR_FLAG, STUCK_IRQ, BAD_MSB, TX_ONLY };
static const uint8_t UID_A[8] = {0x01, 0x00, 0x00, 0x00, 0x00, 0x01, 0x04, 0xE0}; // LSB first: E0-04-01-00-00-00-00-01
static const uint8_t UID_B[8] = {0xBC, 0x9A, 0x78, 0x56, 0x34, 0x12, 0x07, 0xE0}; // E0-07-12-34-56-78-9A-BC
struct Chip {
uint8_t reg[32]{};
uint8_t fifo[12]{};
uint8_t fifo_n{0}, fifo_rd{0};
uint8_t irq{0};
// pending events in micros(); 0 = none
uint32_t t_tx_end{0}, t_rx{0}, t_nr{0};
uint8_t rx_irq{0};
uint8_t rx_data[10]{};
bool tx_armed{false};
uint16_t tx_len{0};
uint8_t tx_got{0};
uint8_t tx_buf[8]{};
// frame parser
bool in_frame{false};
bool have_addr{false};
uint8_t addr{0};
bool rd{false}, cont{false};
uint32_t field_on_us{0};
// scenario and stats
Tag tag{NONE};
uint32_t polls{0}, cs_bad{0}, tx_field_off{0}, tx_early{0}, bad_tx{0}, bad_cmd{0}, bad_rd{0}, max_field_ms{0},
flaky_n{0}, fifo_under{0}, bad_wr{0};
int irq_gpio{-1}, cs_gpio{-1};
} C;
inline void soft_init() {
memset(C.reg, 0, sizeof(C.reg));
C.reg[0x01] = 0x21; // ISO control reset value
C.reg[0x07] = 0x0E;
C.reg[0x08] = 0x1F;
C.reg[0x09] = 0x91;
C.reg[0x0D] = 0x3E; // IRQ mask reset value: no-response masked
C.fifo_n = C.fifo_rd = 0;
C.irq = 0;
C.t_tx_end = C.t_rx = C.t_nr = 0;
}
inline void set_irq_pin() {
static int last = -1; // tick() runs every millisecond; write the pad only when the level changes
const int level = C.irq != 0;
if (C.irq_gpio >= 0 && level != last) {
gpio_set_level((gpio_num_t) C.irq_gpio, level);
last = level;
}
}
inline bool due(uint32_t t, uint32_t now) { return t != 0 && (int32_t) (now - t) >= 0; }
inline void tick() {
const uint32_t now = esphome::micros();
if (due(C.t_tx_end, now)) {
C.t_tx_end = 0;
C.irq |= 0x80;
}
if (due(C.t_rx, now)) {
C.t_rx = 0;
C.irq |= C.rx_irq;
if (C.rx_irq & 0x40) {
memcpy(C.fifo, C.rx_data, 10);
C.fifo_n = 10;
C.fifo_rd = 0;
}
}
if (due(C.t_nr, now)) {
C.t_nr = 0;
if (C.reg[0x0D] & 0x01)
C.irq |= 0x01;
}
set_irq_pin();
}
inline void start_tx() {
const uint32_t now = esphome::micros();
C.polls++;
if (!(C.reg[0x00] & 0x20))
C.tx_field_off++;
else if (now - C.field_on_us < 5000)
C.tx_early++; // field on for less than 5 ms
if (!(C.tx_len == 3 && C.tx_buf[0] == 0x26 && C.tx_buf[1] == 0x01 && C.tx_buf[2] == 0x00))
C.bad_tx++;
C.t_tx_end = now + 1500;
const uint8_t *uid = nullptr;
uint8_t flags = 0x00;
uint32_t rx_delay = 4500;
C.rx_irq = 0x40;
switch (C.tag) {
case TAG_A:
uid = UID_A;
break;
case TAG_B:
uid = UID_B;
break;
case SLOW_A:
uid = UID_A;
rx_delay = 22000;
break; // TX IRQ is read alone first
case FLAKY_A:
uid = (++C.flaky_n % 3 == 0) ? nullptr : UID_A;
break; // 2 of every 3 polls answer
case COLLIDE:
uid = UID_A;
C.rx_irq = 0x40 | 0x02;
break;
case CRC_ERR:
uid = UID_A;
C.rx_irq = 0x40 | 0x10;
break;
case ERR_FLAG:
uid = UID_A;
flags = 0x01;
break;
case BAD_MSB:
uid = UID_A;
break;
default:
break;
}
if (C.tag == STUCK_IRQ || C.tag == TX_ONLY) {
// only the TX interrupt; STUCK_IRQ also never clears the IRQ line
return;
}
if (uid == nullptr) {
C.t_nr = C.t_tx_end + (uint32_t) C.reg[0x07] * 38;
return;
}
C.rx_data[0] = flags;
C.rx_data[1] = 0x00;
memcpy(C.rx_data + 2, uid, 8);
if (C.tag == BAD_MSB)
C.rx_data[9] = 0x60;
C.t_rx = C.t_tx_end + rx_delay;
}
inline void command(uint8_t cmd) {
switch (cmd) {
case 0x00:
break;
case 0x03:
soft_init();
break;
case 0x0F:
C.fifo_n = C.fifo_rd = 0;
break;
case 0x11:
// re-arming over a transmit that never got all its bytes hides a driver length bug
if (C.tx_armed && C.tx_got > 0) {
C.bad_tx++;
ESP_LOGW(TAG, "transmit re-armed after %u of %u bytes", C.tx_got, C.tx_len);
}
C.tx_armed = true;
C.tx_got = 0;
C.fifo_n = C.fifo_rd = 0;
break;
default:
C.bad_cmd++;
ESP_LOGW(TAG, "unexpected command 0x%02X", cmd);
break;
}
}
inline void write_reg(uint8_t r, uint8_t v) {
if (r == 0x1F) { // FIFO
if (C.tx_armed && C.tx_got < sizeof(C.tx_buf)) {
C.tx_buf[C.tx_got++] = v;
C.tx_len = ((uint16_t) C.reg[0x1D] << 4) | (C.reg[0x1E] >> 4);
if (C.tx_len == 0 || C.tx_len > sizeof(C.tx_buf)) {
// a length the buffer can never reach would wait forever for start_tx()
C.bad_tx++;
C.tx_armed = false;
ESP_LOGW(TAG, "transmit length %u does not fit the %u byte TX buffer", C.tx_len, (unsigned) sizeof(C.tx_buf));
} else if (C.tx_got == C.tx_len) {
C.tx_armed = false;
start_tx();
}
} else {
// a FIFO write with no transmit armed, or past the 8 byte TX buffer: a malformed transmission
C.bad_tx++;
ESP_LOGW(TAG, "dropped FIFO byte 0x%02X (armed=%d got=%u len=%u)", v, C.tx_armed, C.tx_got, C.tx_len);
}
return;
}
if (r == 0x00) {
const bool was = C.reg[0] & 0x20, on = v & 0x20;
if (on && !was)
C.field_on_us = esphome::micros();
if (!on && was) {
uint32_t ms = (esphome::micros() - C.field_on_us) / 1000;
if (ms > C.max_field_ms)
C.max_field_ms = ms;
}
}
if (r == 0x01) { // writing ISO control reloads the wait time presets
C.reg[0x07] = 0x0E;
C.reg[0x08] = 0x1F;
}
C.reg[r] = v;
}
inline uint8_t read_reg(uint8_t r) {
if (r == 0x1F) {
if (C.fifo_rd < C.fifo_n)
return C.fifo[C.fifo_rd++];
// a read past the loaded bytes gets zeros a driver would take as data
if (C.fifo_under++ < 5)
ESP_LOGW(TAG, "FIFO read past %u loaded bytes", C.fifo_n);
return 0;
}
if (r == 0x1C)
return C.fifo_n > C.fifo_rd ? (uint8_t) (C.fifo_n - C.fifo_rd - 1) : 0;
if (r == 0x0C) {
uint8_t v = C.irq;
if (C.tag != STUCK_IRQ)
C.irq = 0;
return v;
}
return C.reg[r];
}
inline bool cs_low() {
if (C.cs_gpio < 0)
return true;
return !((C.cs_gpio < 32 ? REG_READ(GPIO_OUT_REG) >> C.cs_gpio : REG_READ(GPIO_OUT1_REG) >> (C.cs_gpio - 32)) & 1);
}
class FakeDelegate : public esphome::spi::SPIDelegate {
public:
FakeDelegate(bool reader)
: SPIDelegate(2000000, esphome::spi::BIT_ORDER_MSB_FIRST, reader ? esphome::spi::MODE1 : esphome::spi::MODE0,
nullptr),
reader_(reader) {}
void begin_transaction() override {
tick();
if (!cs_low())
C.cs_bad++;
if (!this->reader_) { // every CS window starts with a write
C.in_frame = true;
C.have_addr = false;
}
}
void end_transaction() override {
if (this->reader_)
C.in_frame = false;
tick();
}
uint8_t transfer(uint8_t d) override {
tick();
if (this->reader_) {
if (!C.have_addr || !C.rd) { // a read clocked without a read address byte first
C.bad_rd++;
return 0xFF;
}
uint8_t v = read_reg(C.addr);
if (C.cont && C.addr != 0x1F && C.addr != 0x0C)
C.addr++;
return v;
}
if (!C.have_addr) {
this->addr_byte_(d);
return 0;
}
if (C.rd) { // a write after a read address byte: the register write is lost
if (C.bad_wr++ < 5)
ESP_LOGW(TAG, "byte 0x%02X written after read address 0x%02X", d, C.addr);
return 0;
}
write_reg(C.addr, d);
if (C.cont) {
if (C.addr != 0x1F)
C.addr++;
} else {
C.have_addr = false; // single write done; the next byte is a new address or command
}
return 0;
}
protected:
void addr_byte_(uint8_t d) {
if (d & 0x80) {
command(d & 0x1F);
return; // the next byte is a new address
}
C.have_addr = true;
C.addr = d & 0x1F;
C.rd = d & 0x40;
C.cont = d & 0x20;
}
bool reader_;
};
class FakeBus : public esphome::spi::SPIBus {
public:
esphome::spi::SPIDelegate *get_delegate(uint32_t data_rate, esphome::spi::SPIBitOrder bit_order,
esphome::spi::SPIMode mode, esphome::GPIOPin *cs_pin, bool release_device,
bool write_only) override {
ESP_LOGI(TAG, "delegate mode %u rate %u", (unsigned) mode, (unsigned) data_rate);
return new FakeDelegate(mode == esphome::spi::MODE1);
}
};
// Reach SPIComponent::spi_bus_ (the class is final)
template<typename T, typename T::type M> struct Rob {
friend typename T::type get(T) { return M; }
};
struct BusTag {
using type = esphome::spi::SPIBus *esphome::spi::SPIComponent::*;
friend type get(BusTag);
};
template struct Rob<BusTag, &esphome::spi::SPIComponent::spi_bus_>;
inline void install(esphome::spi::SPIComponent *spi, int cs_gpio, int irq_gpio) {
spi->*get(BusTag()) = new FakeBus();
C.cs_gpio = cs_gpio;
C.irq_gpio = irq_gpio;
soft_init();
ESP_LOGI(TAG, "fake TRF7962A installed");
}
inline void late_pin_setup() {
// the reader configured the IRQ pad as input; also drive it so the chip model sets its level
gpio_set_direction((gpio_num_t) C.irq_gpio, GPIO_MODE_INPUT_OUTPUT);
set_irq_pin();
}
// Scenario sequence for the scenarios script: each phase sets the tag and lasts MS milliseconds
struct Phase {
const char *name;
Tag tag;
uint16_t ms;
};
static const Phase PHASES[] = {
{"none", NONE, 4000}, {"tagA", TAG_A, 4000}, {"slowA", SLOW_A, 4000}, {"flakyA", FLAKY_A, 6000},
{"none2", NONE, 4000}, {"tagB", TAG_B, 3000}, {"swapA", TAG_A, 3000}, {"collision", COLLIDE, 4000},
{"tagA2", TAG_A, 3000}, {"crc_err", CRC_ERR, 4000}, {"tagA3", TAG_A, 3000}, {"err_flag", ERR_FLAG, 4000},
{"tagA4", TAG_A, 3000}, {"bad_msb", BAD_MSB, 4000}, {"tagA5", TAG_A, 3000}, {"tx_only", TX_ONLY, 4000},
{"tagA6", TAG_A, 3000}, {"stuck_irq", STUCK_IRQ, 4000}, {"end", NONE, 3000},
};
static constexpr int NUM_PHASES = sizeof(PHASES) / sizeof(PHASES[0]);
inline void stats(const char *phase);
// Log the counters of the phase that ended and start phase I; returns its length in ms
inline uint32_t start_phase(int i) {
if (i > 0) {
stats(PHASES[i - 1].name);
if (PHASES[i - 1].tag == STUCK_IRQ)
C.irq = 0;
}
C.tag = PHASES[i].tag;
ESP_LOGI("ev", "PHASE %s", PHASES[i].name);
return PHASES[i].ms;
}
// polls and max_field_ms are for this phase; the error counters are totals since boot.
inline void stats(const char *phase) {
ESP_LOGI(TAG,
"STATS %s phase: polls=%u max_field_ms=%u | since boot: cs_bad=%u tx_field_off=%u tx_early=%u bad_tx=%u "
"bad_cmd=%u bad_rd=%u fifo_under=%u bad_wr=%u | reg01=%02X reg07=%02X reg09=%02X reg0D=%02X reg00=%02X",
phase, (unsigned) C.polls, (unsigned) C.max_field_ms, (unsigned) C.cs_bad, (unsigned) C.tx_field_off,
(unsigned) C.tx_early, (unsigned) C.bad_tx, (unsigned) C.bad_cmd, (unsigned) C.bad_rd,
(unsigned) C.fifo_under, (unsigned) C.bad_wr, C.reg[1], C.reg[7], C.reg[9], C.reg[0x0D], C.reg[0]);
C.polls = 0;
C.max_field_ms = 0;
}
} // namespace trfsim
@@ -1,45 +0,0 @@
"""check.py LOG [EXPECTED]: match the last 'STATE id = ...' line per text sensor in LOG against EXPECTED (expected.txt
from gen.py ... pr: id, tab, value; None means the sensor must never publish). Also checks the node 3 readings the box
model sends (ducosim.hpp node data: 21.5 °C, CO2 682, 54.32 %) and the mode and comfort writes the config drives
(CNT2 is 0x09, 22.3 °C goes out as 223 tenths), and fails on a request the box model flagged (bad CRC or no model
answer). Exits 1 on any mismatch."""
import pathlib
import re
import sys
log = pathlib.Path(sys.argv[1]).read_text(encoding="utf-8", errors="replace")
exp = pathlib.Path(sys.argv[2] if len(sys.argv) > 2 else "expected.txt").read_text(
encoding="utf-8"
)
seen = {m[1]: m[2] for m in re.finditer(r"STATE (\S+) = '(.*)' \(\d+ B\)", log)}
bad = 0
# numeric STATE lines: the last value per sensor id
nums = {
m[1]: float(m[2])
for m in re.finditer(r"STATE (\S+) = (-?[0-9.]+)\s*$", log, re.MULTILINE)
}
for sid, want in (("temperature", 21.5), ("co2", 682.0), ("humidity", 54.32)):
got = nums.get(sid)
ok = got is not None and abs(got - want) < 0.01
bad += not ok
print(f"{'ok ' if ok else 'FAIL'} {sid}: got {got} want {want}")
for line in ("Mode set to 09", "Comfort set to 223"):
ok = line in log
bad += not ok
print(f"{'ok ' if ok else 'FAIL'} box saw {line!r}")
for line in exp.splitlines():
sid, want = line.split("\t", 1)
got = seen.get(sid)
ok = got is None if want == "None" else got == want
bad += not ok
print(f"{'ok ' if ok else 'FAIL'} {sid}: got {got!r} want {want!r}")
flagged = re.findall(
r"RX request (?:with a bad CRC|fn \S+ sub \S+ has no model answer)", log
)
for f in flagged[:5]:
print(f"FAIL box model flagged: {f}")
bad += len(flagged)
if bad:
sys.exit(f"FAIL: {bad} mismatches")
print("PASS")
@@ -1,172 +0,0 @@
#pragma once
// A Duco ventilation box on a second UART, cross-wired to the duco hub's UART.
// Port of the box model in tests/integration/fixtures/uart_mock_duco.yaml (esphome#22560):
// every request gets an acknowledgement (function + 1, empty) and then the answer, byte stuffed, CRC16.
// Call ducosim::poll() from a short interval; set ducosim::bus in on_boot.
#include <string>
#include <vector>
#include "esphome/components/uart/uart.h"
#include "esphome/core/helpers.h"
#include "esphome/core/log.h"
namespace ducosim {
inline esphome::uart::UARTComponent *bus = nullptr;
inline std::vector<uint8_t> rx;
inline uint8_t mode = 0x04;
inline uint8_t comfort = 210;
inline uint32_t requests = 0;
// payload size of the box serial answer: 34 as in the PR fixture, 18 as in the dev fixture
inline size_t box_serial_size = 34;
inline void reply(uint8_t fn, uint8_t message_id, const std::vector<uint8_t> &payload) {
std::vector<uint8_t> body{static_cast<uint8_t>(payload.size() + 2), fn, message_id};
body.insert(body.end(), payload.begin(), payload.end());
const uint16_t crc = esphome::crc16(body.data(), body.size());
body.push_back(crc & 0xFF);
body.push_back(crc >> 8);
std::vector<uint8_t> out{0xAA, 0x55};
for (uint8_t b : body) {
out.push_back(b);
if (b == 0xAA)
out.push_back(0x01);
}
char hex[100];
esphome::format_hex_to(hex, sizeof(hex), out.data(), out.size() > 40 ? 40 : out.size());
ESP_LOGV("box", "TX %s", hex);
bus->write_array(out.data(), out.size());
}
inline void handle(std::vector<uint8_t> frame) {
// frame: length, function, message id, payload, CRC16 (low byte first) over everything before it
const uint16_t crc = esphome::crc16(frame.data(), frame.size() - 2);
if ((crc & 0xFF) != frame[frame.size() - 2] || (crc >> 8) != frame[frame.size() - 1])
ESP_LOGW("box", "RX request with a bad CRC (answered anyway)");
// Requests differ in payload length, and the dispatch below compares bytes past a short payload, so pad
// (no warning: a short frame is normal here; a wrong one ends with "no model answer")
if (frame.size() < 10)
frame.resize(10, 0);
const uint8_t function = frame[1];
const uint8_t message_id = frame[2];
const uint8_t *d = frame.data() + 3;
requests++;
char hex[64];
esphome::format_hex_to(hex, sizeof(hex), frame.data(), frame.size() > 20 ? 20 : frame.size());
ESP_LOGD("box", "RX #%u fn %02X id %02X: %s", (unsigned) requests, function, message_id, hex);
reply(function + 1, message_id, {});
if (function == 0x0c && d[0] == 0x01) {
static constexpr uint8_t TYPES[] = {0, 17, 10, 12};
static constexpr uint8_t NETWORK_TYPES[] = {0, 0, 3, 2};
const uint8_t type = d[1] < sizeof(TYPES) ? TYPES[d[1]] : 0;
const uint8_t network_type = d[1] < sizeof(NETWORK_TYPES) ? NETWORK_TYPES[d[1]] : 0;
reply(0x0e, message_id, {type, network_type, 0, 0, 0, 0, 0});
} else if (function == 0x0c && d[0] == 0x02) {
std::vector<uint8_t> status(20, 0);
status[0] = mode;
status[2] = 45;
status[12] = 0xAA; // 21930 s, sent as AA 01 55: an escaped header inside the data
status[13] = 0x55;
reply(0x0e, message_id, status);
} else if (function == 0x0c && d[0] == 0x04) {
mode = d[2];
ESP_LOGI("box", "Mode set to %02X", mode);
reply(0x0e, message_id, {0x01});
} else if (function == 0x10 && d[3] == 0x49) {
// temperature 21.5, CO2 682 (0x02AA is byte stuffed), humidity 54.32, uptime 2190 min
reply(0x12, message_id, {1, 4, 215, 0, 0xAA, 0x02, 0x38, 0x15, 0x8E, 0x08});
} else if (function == 0x10 && d[3] == 0x1a) {
std::vector<uint8_t> serial{0, 0, 'S', 'N', '1', '2', '3', '4'};
serial.resize(box_serial_size, 0);
reply(0x12, message_id, serial);
} else if (function == 0x10 && d[3] == 0x08) {
if (d[1] == 3) {
reply(0x12, message_id, {1, d[4], 0xBE, 0}); // not printable: no state
} else {
// node 2 gives SN2234 like the fixture; the node digit tells other nodes apart
std::vector<uint8_t> serial{1, d[4], 'S', 'N', '2', static_cast<uint8_t>('0' + d[1]), '3', '4'};
serial.resize(2 + 2 * d[4], 0);
reply(0x12, message_id, serial);
}
} else if (function == 0x10 && d[3] == 0x00) {
reply(0x12, message_id, {1, 4, 1, 0, 2, 4, 3, 0, 0, 0});
} else if (function == 0x30) {
if (d[0] == 3)
reply(0x32, message_id, {2, 0, 31, 19, 1, 0, 0, 99, 99, 99, 0});
else if (d[0] == 4)
reply(0x32, message_id, {0});
else if (d[0] == 5)
reply(0x32, message_id, {2, 0, 31, 19, 1, 0}); // second error missing
else if (d[0] == 6)
reply(0x32, message_id, {4, 0, 3, 1, 4, 0, 0, 4, 1, 4, 0, 0, 6, 1, 4, 0, 0, 16, 1, 4, 0});
else if (d[0] == 7) // three long descriptions, 210 characters: they still fit
reply(0x32, message_id, {3, 0, 16, 1, 4, 0, 0, 29, 1, 4, 0, 0, 30, 1, 4, 0});
else if (d[0] == 8) // four short descriptions fit too: the fallback goes by length, not count
reply(0x32, message_id, {4, 0, 31, 19, 1, 0, 0, 31, 19, 1, 0, 0, 31, 19, 1, 0, 0, 31, 19, 1, 0});
else
reply(0x32, message_id, {});
} else if (function == 0x24 && d[0] == 0x00) {
int16_t value = 0;
if (d[2] == 0x0a && d[1] == 0x12)
value = comfort;
else if (d[1] == 0x00)
value = -35;
else if (d[1] == 0x01)
value = 195;
else if (d[1] == 0x02)
value = 220;
else if (d[1] == 0x03)
value = 80;
else if (d[1] == 0x10)
value = 50;
else if (d[1] == 0x30)
value = 120;
reply(0x26, message_id, {0x01, d[1], d[2], uint8_t(value & 0xFF), uint8_t((value >> 8) & 0xFF), 0, 0});
} else if (function == 0x24 && d[0] == 0x01) {
comfort = d[3];
ESP_LOGI("box", "Comfort set to %u", comfort);
reply(0x26, message_id, {0x01, d[1], d[2], d[3], 0, 0, 0});
} else if (function == 0x24 && d[0] == 0x05) {
ESP_LOGI("box", "Time set to %u", (unsigned) esphome::encode_uint32(d[4], d[3], d[2], d[1]));
reply(0x26, message_id, {0x05});
} else {
ESP_LOGW("box", "RX request fn %02X sub %02X has no model answer, sent only the ack", function, d[0]);
}
}
inline void poll() {
if (bus == nullptr)
return;
uint8_t byte;
while (bus->available() && bus->read_byte(&byte))
rx.push_back(byte);
while (rx.size() >= 3) {
if (rx[0] != 0xAA || rx[1] != 0x55) {
rx.erase(rx.begin());
continue;
}
const size_t total = 2 + rx[2] + 3;
if (rx.size() < total)
return;
std::vector<uint8_t> frame(rx.begin() + 2, rx.begin() + total);
rx.erase(rx.begin(), rx.begin() + total);
// One request per call, like a box answering in turn: a burst of answers written at once blocks this
// loop pass for ~100 ms and overflows the hub's 256 byte UART receive buffer before it can read
handle(frame);
return;
}
}
// Log a published text with its newlines shown as \n, so one state is one line.
inline void show(const char *name, const std::string &text) {
std::string flat;
for (char c : text) {
if (c == '\n')
flat += "\\n";
else
flat += c;
}
ESP_LOGI("hwt", "STATE %s = '%s' (%u B)", name, flat.c_str(), (unsigned) text.size());
}
} // namespace ducosim
@@ -1,226 +0,0 @@
"""Write the ESP32-C6 configs for the duco box model: gen.py NAME pr|dev."""
import os
import pathlib
import sys
name, kind = sys.argv[1], sys.argv[2]
# DUCO_VERBOSE=1 shows the hub's discovery lines (Found node ...) and its sent requests
SERIAL_SIZE = int(os.environ.get("DUCO_SERIAL_SIZE", "34"))
LEVEL = "VERBOSE" if os.environ.get("DUCO_VERBOSE") else "DEBUG"
# (key, id, address, expected) in the PR schema; expected None means no state may be published
PR_TEXT = [
("serial_number", "serial_number_node_1", 1, "SN2134"),
("serial_number", "serial_number_node_2", 2, "SN2234"),
("serial_number", "invalid_serial_number", 3, None),
("version", "version", 4, "1.2.3.4"),
(
"error",
"node_error",
3,
"E31.19.01 Filters need replacement.\\nE99.99.99 Unknown DUCO error\\n",
),
("error", "node_no_error", 2, "No errors"),
("error", "node_zero_errors", 4, "No errors"),
("error", "node_short_errors", 5, None),
(
"error",
"node_many_errors",
6,
"E03.01.04\\nE04.01.04\\nE06.01.04\\nE16.01.04\\n",
),
(
"error",
"node_three_long_errors",
7,
"E16.01.04 Extract air pressure sensor value is unchanged for too long.\\nE29.01.04 Supply air pressure sensor value is unchanged for too long.\\nE30.01.04 SUP temperature sensor value is unchanged for too long.\\n",
),
(
"error",
"node_four_short_errors",
8,
"E31.19.01 Filters need replacement.\\n" * 4,
),
("uptime", "uptime", 2, "1d 12h 30m"),
("node_type", "node_type", 2, "BSRH"),
("node_type", "node_type_node_1", 1, "BOX"),
("node_type", "node_type_node_3", 3, "UCCO2"),
]
SENSORS = [
("co2", 3),
("temperature", 3),
("humidity", 3),
("filter_remaining", None),
("flow_level", None),
("time_remaining", None),
("bypass", None),
("temperature_oda", None),
("temperature_sup", None),
("temperature_eta", None),
("temperature_eha", None),
]
ids = [s for s, _ in SENSORS] + ["serial_number"]
if kind == "pr":
ids += [t[1] for t in PR_TEXT]
ids.append("comfort_temperature")
out = []
w = out.append
w(f"""esphome:
name: {name}
includes:
- ducosim.hpp
on_boot:
priority: -100
then:
- lambda: ducosim::bus = id(box_uart); ducosim::box_serial_size = {SERIAL_SIZE};
- delay: 1s
- logger.log: "BURST 1"
- script.execute: burst
- delay: 8s
- logger.log: "SET mode CNT2"
- select.set:
id: mode
option: CNT2
- delay: 3s
- logger.log: "SET comfort 22.3"
- number.set:
id: comfort_temperature
value: 22.3
- delay: 3s
- logger.log: "PHASE1 DONE"
esp32:
variant: esp32c6
board: esp32-c6-devkitc-1
framework:
type: esp-idf
logger:
hardware_uart: USB_SERIAL_JTAG
level: {LEVEL}
logs:
box: DEBUG
text_sensor: DEBUG
sensor: DEBUG
select: DEBUG
number: DEBUG
preferences: DEBUG
wifi: INFO
wifi_esp32: INFO
component: DEBUG
scheduler: DEBUG
wifi:
ssid: !secret wifi_ssid
password: !secret wifi_password
time:
- platform: sntp
id: sntp_time
# duco hub on one UART, the box model on the other, cross-wired through the GPIO matrix
uart:
- id: duco_uart
tx_pin:
number: GPIO18
allow_other_uses: true
rx_pin:
number: GPIO19
allow_other_uses: true
baud_rate: 57600
- id: box_uart
tx_pin:
number: GPIO19
allow_other_uses: true
rx_pin:
number: GPIO18
allow_other_uses: true
baud_rate: 57600
interval:
- interval: 3ms
then:
- lambda: ducosim::poll();
- interval: 20s
then:
- logger.log: "BURST again"
- script.execute: burst
- interval: 30s
then:
- lambda: 'ESP_LOGI("hwt", "ALIVE requests %u free heap %u", (unsigned) ducosim::requests, (unsigned) esp_get_free_heap_size());'
script:
- id: burst
then:
""")
for i in ids:
w(f" - component.update: {i}\n")
w("""
duco:
uart_id: duco_uart
discovery: {}
time:
time_id: sntp_time
update_interval: 10s
select:
- platform: duco
id: mode
name: Mode
update_interval: 1s
on_value:
- lambda: 'ESP_LOGI("hwt", "STATE mode = %s", x.c_str());'
number:
- platform: duco
id: comfort_temperature
name: Comfort Temperature
update_interval: never
on_value:
- lambda: 'ESP_LOGI("hwt", "STATE comfort_temperature = %.2f", x);'
text_sensor:
- platform: duco
id: serial_number
name: Serial Number
update_interval: never
on_value:
- lambda: 'ducosim::show("serial_number", x);'
""")
if kind == "pr":
w(" - platform: duco\n")
for key in dict.fromkeys(t[0] for t in PR_TEXT):
w(f" {key}:\n")
for k, i, addr, _ in PR_TEXT:
if k != key:
continue
w(f""" - address: {addr}
id: {i}
name: {i.replace("_", " ").title()}
update_interval: never
on_value:
- lambda: 'ducosim::show("{i}", x);'
""")
w("\nsensor:\n - platform: duco\n")
for s, addr in SENSORS:
w(f" {s}:\n")
pre = " "
if addr is not None:
w(f" - address: {addr}\n")
pre = " "
w(f"""{pre}id: {s}
{pre}name: {s.replace("_", " ").title()}
{pre}update_interval: never
{pre}on_value:
{pre} - lambda: 'ESP_LOGI("hwt", "STATE {s} = %.2f", x);'
""")
pathlib.Path(f"{name}.yaml").write_text("".join(out), encoding="utf-8")
if kind == "pr":
with pathlib.Path("expected.txt").open("w", encoding="utf-8") as f:
for _, i, _, e in PR_TEXT:
f.write(f"{i}\t{e}\n")
# the box serial answer in ducosim.hpp: "SN1234" padded with zeros
f.write("serial_number\tSN1234\n")
@@ -1,27 +0,0 @@
#!/bin/bash
# run.sh NAME KIND SECONDS: generate NAME.yaml with gen.py (KIND pr|dev), build it from TREE, flash the board and
# capture its log to NAME-log.txt; exit 1 on a crash line, a second boot or a request the box could not decode, and
# a pr build ends with check.py against expected.txt (exit 1 on a mismatch).
# Run it from a scratch directory that holds a copy of ducosim.hpp and secrets.yaml.
# env: TREE, PY, DUCO_PORT (the board), BUILD_LOCK (optional, see ../../pio_build.sh)
D=$PWD; S=$(cd "$(dirname "$0")/../.." && pwd); N=${1:?name}; K=${2:?pr|dev}; SECS=${3:?seconds}
P=${DUCO_PORT:?set DUCO_PORT}; T=${TREE:?set TREE}; PY=${PY:-$T/venv/bin/python}
[ -f "$D/ducosim.hpp" ] || { echo "copy ducosim.hpp into $D first"; exit 1; }
$PY "$(dirname "$0")/gen.py" "$N" "$K" || exit 1
"$S/pio_build.sh" compile "$N" "$D/$N.yaml" || exit 1
pkill -f "[s]erial_capture.py $P "
"$S/pio_build.sh" upload "$N" "$D/$N.yaml" "$P" || exit 1
$PY "$S/serial_capture.py" "$P" "$SECS" "$D/$N-log.txt" --reset || { echo "FAIL: $N capture failed"; exit 1; }
# the box must have answered: an empty control row from a dead hub or a miswired UART is no control
REQ=$(grep -a -o "ALIVE requests [0-9]*" "$D/$N-log.txt" | tail -1 | grep -o "[0-9]*$")
[ "${REQ:-0}" -gt 0 ] || { echo "FAIL: the box model answered no requests"; exit 1; }
# the capture resets the board once; a crash or another boot after it is a failure on either build
CRASH=$(grep -a -c -E 'Guru Meditation|panic|abort\(|Backtrace' "$D/$N-log.txt")
BOOTS=$(grep -a -c 'Running through setup()' "$D/$N-log.txt")
[ "$CRASH" = 0 ] && [ "$BOOTS" -le 1 ] || { echo "FAIL: crash lines $CRASH, boots $BOOTS"; exit 1; }
# a request the box could not decode means a garbled or misaligned UART, which makes the control row invalid too
BADREQ=$(grep -a -c -E 'RX request (with a bad CRC|fn .* has no model answer)' "$D/$N-log.txt")
[ "$BADREQ" = 0 ] || { echo "FAIL: $BADREQ requests with a bad CRC or no model answer"; exit 1; }
# the dev build is the control and has no expected states; a PR build must match expected.txt
[ "$K" = pr ] || { echo "$N done ($REQ requests, no state verdict for dev)"; exit 0; }
$PY "$(dirname "$0")/check.py" "$D/$N-log.txt" "$D/expected.txt"
@@ -1,26 +0,0 @@
# web_server Basic auth fuzz (async web server: ESP8266, RP2040/RP2350, LibreTiny)
- `authfuzz.py HOST USER PASS OUTPREFIX [--only cases|heavy|timing] [--ota IMAGE]`: raw-socket cases
(functional routes with and without credentials, wrong credentials, schemes, tampered hash, spacing and
case), then `heavy` (8 KB Authorization, 32/64 KB headers, 200 headers, 256 KB unauthenticated /update,
600 failed attempts, 4 bad-credential threads next to an authenticated `/events` stream) and `timing`
(wrong first byte vs wrong last byte, ms to the first 401 byte, Mann-Whitney z). Heap comes from the
debug "Heap free" sensor over REST. `--ota` uploads the image with credentials and waits for the reboot.
It is importable: `configure()` sets the host and credentials for `raw()`, `build()` and `uptime()`.
- `isolate.py HOST USER PASS [ROUNDS] [CASE,...]`: one hostile request at a time with the uptime before and after,
to tie a reboot to one request.
- `compare.py DEV_PREFIX PR_PREFIX` lines the two runs up and prints both raw 401 heads;
it exits 1 when any case differs or exists on one side only.
- `configs/`: `wba-common.yaml` (web_server v3, Basic auth, web OTA, switch and light on top of
`templates/diagnostics.yaml`), `creds-short.yaml` and `creds-long.yaml`, `d1.yaml` (D1 mini on the station
network), `d1-ap.yaml` (captive portal DUT on `templates/dut-fallback-ap.yaml`), `ap-client.yaml` with
`ap-client-common.yaml` (`templates/client-wifisave.yaml` plus probes of `/`, `/config.json` and a POST without
and with credentials while on the AP; `approbe_check.py CLIENT_LOG` fails unless the POST without credentials
got 401, the one with credentials 200 and every probe answered), and `run.sh NAME CONFIG KIND PORT MODE` (build
with the chosen credentials through `../pio_build.sh`, flash, capture, authfuzz, crash counts).
Library facts (ESPAsyncWebServer 3.9.6): each header line is trimmed before parsing, so trailing spaces or a
tab after the hash never reach ESPHome; the value skips one space after the colon only; a NUL byte in the
header aborts the request; the scheme split is at the first space, case-insensitive. Before the Basic auth rework,
`authenticate(hash)` compared the text after the first space whatever the scheme, so `Bearer <hash>`,
`Foo <hash>` and a bare `<hash>` were accepted.
@@ -1,36 +0,0 @@
"""approbe_check.py CLIENT_LOG: verdict for the probes of configs/ap-client-common.yaml in the client's serial log.
Needs at least one round of all four probes and the same number of replies to each (a cut-off round fails); in every
round a POST without credentials must get 401 and one with credentials 200, and the portal root and /config.json must
answer (any status but -1; their codes are printed to compare two builds). Exits 1 when a probe is missing or a status
is wrong.
"""
import pathlib
import re
import sys
RX = re.compile(r"probe (portal_root|config_json|post_noauth|post_auth) status (-?\d+)")
WANT = {"post_noauth": "401", "post_auth": "200"}
seen: dict[str, list[str]] = {
k: [] for k in ("portal_root", "config_json", "post_noauth", "post_auth")
}
for line in (
pathlib.Path(sys.argv[1]).read_text(encoding="utf-8", errors="replace").splitlines()
):
if m := RX.search(line):
seen[m[1]].append(m[2])
fails = []
if len({len(c) for c in seen.values()}) > 1:
fails.append("unequal probe counts, a round did not finish")
for name, codes in seen.items():
print(f"{name}: {codes}")
if not codes:
fails.append(f"{name} never ran")
elif name in WANT and any(c != WANT[name] for c in codes):
fails.append(f"{name} got {codes}, want {WANT[name]}")
elif "-1" in codes:
fails.append(f"{name} got no answer")
print("PASS" if not fails else "FAIL: " + "; ".join(fails))
sys.exit(1 if fails else 0)
@@ -1,750 +0,0 @@
"""Basic auth functional and security cases against an ESPHome web_server (non-ESP32 async server).
usage: authfuzz.py HOST USER PASS OUT_PREFIX [--only a,b] [--brute N] [--timing N] [--conc SECONDS]
[--ota FIRMWARE.bin] [--skip-heavy]
Writes OUT_PREFIX-cases.json (one row per case: status, WWW-Authenticate, raw head, pass/fail) and
OUT_PREFIX-summary.txt. Heap comes from the "Heap free" debug sensor over REST (with credentials).
Entity URLs use entity names: /sensor/Heap%20free, /switch/Test%20switch, /light/Test%20light.
"""
from __future__ import annotations
import argparse
import base64
import contextlib
import json
import pathlib
import random
import re
import socket
import statistics
import string
import sys
import threading
import time
# Set by configure(); isolate.py imports this module and calls it too.
A = argparse.Namespace()
HOST = ""
USER = ""
PASSWORD = ""
HASH = ""
GOOD = ""
OUT = None
ROWS: list[dict] = []
def configure(host: str, user: str, password: str, out: str | None = None) -> None:
global HOST, USER, PASSWORD, HASH, GOOD, OUT # noqa: PLW0603
HOST, USER, PASSWORD = host, user, password
HASH = base64.b64encode(f"{user}:{password}".encode()).decode()
GOOD = f"Basic {HASH}"
if out:
OUT = pathlib.Path(out + "-summary.txt").open("w", encoding="utf-8") # noqa: SIM115
def p(*a: object) -> None:
s = time.strftime("[%H:%M:%S] ") + " ".join(str(x) for x in a)
print(s, flush=True)
if OUT is not None:
OUT.write(s + "\n")
OUT.flush()
def raw(
req: bytes, timeout: float = 8.0, read_body: bool = True, max_bytes: int = 65536
) -> dict:
"""Send raw bytes, return status, headers, body, timings, error."""
t0 = time.monotonic()
r: dict = {
"status": None,
"headers": {},
"head": "",
"body": b"",
"err": None,
"closed": None,
}
try:
s = socket.create_connection((HOST, 80), timeout=timeout)
except OSError as e:
r["err"] = f"connect {type(e).__name__}"
return r
try:
s.settimeout(timeout)
try:
s.sendall(req)
except OSError as e:
r["err"] = f"send {type(e).__name__}"
t1 = time.monotonic()
buf = b""
first = None
while len(buf) < max_bytes:
try:
d = s.recv(4096)
except TimeoutError:
r["err"] = r["err"] or "timeout"
break
except OSError as e:
r["err"] = r["err"] or f"recv {type(e).__name__}"
break
if not d:
r["closed"] = True
break
if first is None:
first = time.monotonic()
buf += d
if b"\r\n\r\n" in buf:
if not read_body:
break
hd, bd = buf.split(b"\r\n\r\n", 1)
m = re.search(rb"(?im)^content-length:\s*(\d+)", hd)
if m and len(bd) >= int(m.group(1)):
r["closed"] = False
break
r["ttfb"] = (first - t1) if first else None
r["total"] = time.monotonic() - t0
if b"\r\n\r\n" in buf:
head, body = buf.split(b"\r\n\r\n", 1)
r["head"] = head.decode("latin1")
r["body"] = body
lines = r["head"].split("\r\n")
with contextlib.suppress(IndexError, ValueError):
r["status"] = int(lines[0].split()[1])
for ln in lines[1:]:
if ":" in ln:
k, v = ln.split(":", 1)
r["headers"].setdefault(k.strip().lower(), []).append(v.strip())
elif buf:
r["err"] = r["err"] or "partial"
r["body"] = buf
finally:
s.close()
return r
def build(
method: str,
path: str,
auth: bytes | str | None,
extra: bytes = b"",
body: bytes = b"",
ctype: str | None = None,
) -> bytes:
h = f"{method} {path} HTTP/1.1\r\nHost: {HOST}\r\nConnection: close\r\n".encode()
if auth is not None:
h += (
b"Authorization: "
+ (auth.encode("latin1") if isinstance(auth, str) else auth)
+ b"\r\n"
)
h += extra
if method == "POST" or body:
if ctype:
h += f"Content-Type: {ctype}\r\n".encode()
h += f"Content-Length: {len(body)}\r\n".encode()
return h + b"\r\n" + body
def heap() -> object:
for _ in range(3):
r = raw(build("GET", "/sensor/Heap%20free", GOOD))
if r["status"] == 200:
try:
return json.loads(r["body"]).get("value")
except ValueError:
pass
time.sleep(1)
return None
def heap_ok(h0: object, h1: object) -> bool:
"""Both heap readings present and the heap after a phase at most --heap-margin bytes lower than before.
Sockets in TIME_WAIT hold heap for a while after a flood, so a small drop right after a phase is normal;
leak.py measures a leak properly.
"""
margin = getattr(A, "heap_margin", 8192)
return (
isinstance(h0, (int, float))
and isinstance(h1, (int, float))
and h0 - h1 <= margin
)
def uptime() -> object:
r = raw(build("GET", "/sensor/Uptime", GOOD))
try:
return json.loads(r["body"]).get("value") if r["status"] == 200 else None
except ValueError:
return None
def case(
name: str, req: bytes, expect: int | str, note: str = "", **kw: object
) -> dict:
u0 = uptime() if expect == "reject" else None
r = raw(req, **kw)
st = r["status"]
down = False
if expect == "reject":
ok = st in (400, 401, 413, 431)
if st is None:
# no status (closed, reset or timeout) is a rejection only when the device stayed up
time.sleep(1)
u1 = uptime()
ok = u0 is not None and u1 is not None and u1 >= u0
down = not ok
note = (note + " " if note else "") + f"no status, uptime {u0} -> {u1}"
else:
ok = st == expect
row = {
"case": name,
"status": st,
"expect": expect,
"pass": ok,
"err": r["err"],
"www": r["headers"].get("www-authenticate"),
"head": r["head"],
"body_len": len(r["body"]),
"body_head": r["body"][:120].decode("latin1"),
"note": note,
"closed": r["closed"],
# only the status of an informational case is not judged; a reboot or no answer still fails it
"info": note.startswith("informational") and not down,
}
ROWS.append(row)
p(
f"{'PASS' if ok else 'INFO' if row['info'] else 'FAIL'} {name}: status {st} expect {expect} www={row['www']} err={r['err']} "
f"body {len(r['body'])}B"
)
return row
def mp_body(data: bytes) -> tuple[bytes, str]:
b = "----hwtestboundary" + "".join(random.choices(string.ascii_letters, k=8))
body = (
(
f'--{b}\r\nContent-Disposition: form-data; name="update"; filename="firmware.bin"\r\n'
"Content-Type: application/octet-stream\r\n\r\n"
).encode()
+ data
+ f"\r\n--{b}--\r\n".encode()
)
return body, f"multipart/form-data; boundary={b}"
def flip(c: str) -> str:
return "B" if c != "B" else "C"
def swapcase_b64(h: str) -> str:
return "".join(c.swapcase() if c.isalpha() else c for c in h)
def run_cases() -> None:
sw, lt = (
"/switch/Test%20switch/toggle",
"/light/Test%20light/turn_on?brightness=100",
)
p("HASH length", len(HASH))
# Functional
nr = case("noauth GET /", build("GET", "/", None), 401)
ok401 = nr["www"] == ['Basic realm="Login Required"']
p(f"{'PASS' if ok401 else 'FAIL'} 401 WWW-Authenticate exact: {nr['www']}")
ROWS.append(
{
"case": "401 WWW-Authenticate exact",
"pass": ok401,
"www": nr["www"],
"status": nr["status"],
"head": nr["head"],
"body_head": nr["body_head"],
}
)
case("good GET /", build("GET", "/", GOOD), 200)
case(
"noauth /events",
build("GET", "/events", None, b"Accept: text/event-stream\r\n"),
401,
)
r = raw(
build("GET", "/events", GOOD, b"Accept: text/event-stream\r\n"),
timeout=3,
max_bytes=4096,
)
ev = r["status"] == 200 and b"event:" in r["body"]
ROWS.append(
{
"case": "good /events (SSE data)",
"status": r["status"],
"pass": ev,
"err": r["err"],
}
)
p(
f"{'PASS' if ev else 'FAIL'} good /events: status {r['status']} event bytes {len(r['body'])}"
)
case("noauth POST switch", build("POST", sw, None), 401)
case("good POST switch", build("POST", sw, GOOD), 200)
case("noauth POST light", build("POST", lt, None), 401)
case("good POST light", build("POST", lt, GOOD), 200)
body, ct = mp_body(b"\x00" * 1024)
case(
"noauth POST /update 1KB",
build("POST", "/update", None, body=body, ctype=ct),
401,
)
case("noauth GET /sensor", build("GET", "/sensor/Fast%20counter", None), 401)
case("good GET /sensor", build("GET", "/sensor/Fast%20counter", GOOD), 200)
# Wrong credentials
wp = base64.b64encode(f"{USER}:{PASSWORD}x".encode()).decode()
wp2 = base64.b64encode(
f"{USER}:{PASSWORD[:-1] + flip(PASSWORD[-1])}".encode()
).decode()
wu = base64.b64encode(f"{USER}x:{PASSWORD}".encode()).decode()
wu2 = base64.b64encode(f"{flip(USER[0]) + USER[1:]}:{PASSWORD}".encode()).decode()
case("wrong password (longer)", build("GET", "/", "Basic " + wp), 401)
case("wrong password (same length)", build("GET", "/", "Basic " + wp2), 401)
case("wrong username (longer)", build("GET", "/", "Basic " + wu), 401)
case("wrong username (same length)", build("GET", "/", "Basic " + wu2), 401)
case("hash minus last char", build("GET", "/", "Basic " + HASH[:-1]), 401)
case("hash plus one char", build("GET", "/", "Basic " + HASH + "A"), 401)
case("hash prefix half", build("GET", "/", "Basic " + HASH[: len(HASH) // 2]), 401)
case("empty Authorization value", build("GET", "/", ""), 401)
# Schemes
case(
"Digest scheme",
build(
"GET",
"/",
f'Digest username="{USER}", realm="Login Required", nonce="0", '
f'uri="/", response="00000000000000000000000000000000"',
),
401,
)
case("Digest scheme with hash", build("GET", "/", "Digest " + HASH), 401)
case("Bearer hash", build("GET", "/", "Bearer " + HASH), 401)
case("Basic alone", build("GET", "/", "Basic"), 401)
case("Basic plus space", build("GET", "/", "Basic "), 401)
case("Basic plus spaces only", build("GET", "/", "Basic "), 401)
case("hash without scheme", build("GET", "/", HASH), 401)
case("Basicx scheme", build("GET", "/", "Basicx " + HASH), 401)
# Tampering
case("hash + trailing garbage", build("GET", "/", GOOD + "garbage"), 401)
case("hash + space + garbage", build("GET", "/", GOOD + " garbage"), 401)
case(
"first char changed",
build("GET", "/", "Basic " + flip(HASH[0]) + HASH[1:]),
401,
)
case(
"last char changed",
build("GET", "/", "Basic " + HASH[:-1] + flip(HASH[-1])),
401,
)
case("swapped case base64", build("GET", "/", "Basic " + swapcase_b64(HASH)), 401)
# Spacing and case
case("lowercase basic", build("GET", "/", "basic " + HASH), 200)
case("uppercase BASIC", build("GET", "/", "BASIC " + HASH), 200)
case("Basic 3 spaces", build("GET", "/", "Basic " + HASH), 401)
case(
"trailing spaces",
build("GET", "/", GOOD + " "),
200,
"library trims the header line",
)
case(
"trailing tab",
build("GET", "/", GOOD + "\t"),
200,
"library trims the header line",
)
case(
"trailing space+tab",
build("GET", "/", GOOD + " \t "),
200,
"library trims the header line",
)
case(
"leading spaces in value",
build("GET", "/", " " + GOOD),
"reject",
"informational: library skips one OWS",
)
case(
"lowercase header name",
build("GET", "/", None, f"authorization: {GOOD}\r\n".encode()),
200,
)
case("Basic TAB hash", build("GET", "/", "Basic\t" + HASH), 401)
case("Basic TAB space hash", build("GET", "/", "Basic \t" + HASH), 401)
case(
"duplicate header bad then good",
build("GET", "/", "Basic " + wp, f"Authorization: {GOOD}\r\n".encode()),
"reject",
"informational",
)
case(
"duplicate header good then bad",
build("GET", "/", GOOD, f"Authorization: Basic {wp}\r\n".encode()),
"reject",
"informational",
)
# Last: a NUL byte in a header takes ESPAsyncWebServer's abort path, which can reboot the board
# (seen on dev and on the PR, ESP8266 and RP2350); the case before it must not be lost to that
case(
"hash NUL garbage",
build("GET", "/", GOOD.encode() + b"\x00garbage"),
"reject",
"informational: a reboot here is the library's NUL abort path",
)
for _ in range(30):
if uptime() is not None:
break
time.sleep(2)
else:
ROWS.append({"case": "device back after NUL case", "pass": False})
p("FAIL device back after NUL case: no answer within 60 s")
def heavy() -> None:
h0, u0 = heap(), uptime()
p(f"heap before oversized {h0} uptime {u0}")
case("8KB Authorization", build("GET", "/", "Basic " + "A" * 8192), "reject")
case(
"8KB Authorization valid prefix",
build("GET", "/", GOOD + " " + "A" * 8192),
"reject",
)
case(
"32KB garbage header",
build("GET", "/", None, b"X-Garbage: " + b"Z" * 32768 + b"\r\n"),
"reject",
)
case(
"64KB header no newline",
b"GET / HTTP/1.1\r\nHost: x\r\nX-G: " + b"Q" * 65536,
"reject",
)
case(
"200 headers",
build(
"GET",
"/",
None,
b"".join(f"X-H{i}: {'v' * 60}\r\n".encode() for i in range(200)),
),
"reject",
)
body, ct = mp_body(random.randbytes(256 * 1024))
case(
"noauth POST /update 256KB",
build("POST", "/update", None, body=body, ctype=ct),
"reject",
timeout=30,
)
body, ct = mp_body(random.randbytes(64 * 1024))
case(
"bad-auth POST /update 64KB",
build(
"POST",
"/update",
"Basic " + HASH[:-1] + flip(HASH[-1]),
body=body,
ctype=ct,
),
"reject",
timeout=30,
)
time.sleep(3)
h1, u1 = heap(), uptime()
p(f"heap after oversized {h1} uptime {u1}")
ROWS.append(
{
"case": "heap oversized",
"before": h0,
"after": h1,
"uptime": [u0, u1],
"pass": heap_ok(h0, h1) and u1 is not None and u0 is not None and u1 > u0,
}
)
# Brute force
time.sleep(2)
h0, u0 = heap(), uptime()
codes: dict = {}
t = time.monotonic()
for i in range(A.brute):
bad = base64.b64encode(
f"{USER}:{''.join(random.choices(string.ascii_letters, k=len(PASSWORD)))}".encode()
).decode()
r = raw(
build("GET", "/" if i % 2 else "/sensor/Fast%20counter", "Basic " + bad),
timeout=5,
read_body=False,
)
k = r["status"] if r["status"] is not None else r["err"]
codes[k] = codes.get(k, 0) + 1
dt = time.monotonic() - t
time.sleep(3)
h1, u1 = heap(), uptime()
bf_ok = (
bool(codes)
and set(codes) <= {401}
and heap_ok(h0, h1)
and u1 is not None
and u0 is not None
and u1 > u0
)
p(
f"{'PASS' if bf_ok else 'FAIL'} brute {A.brute} in {dt:.1f}s ({A.brute / dt:.1f}/s): {codes}; heap {h0}->{h1}"
f" uptime {u0}->{u1}"
)
ROWS.append(
{
"case": f"brute force {A.brute}",
"codes": {str(k): v for k, v in codes.items()},
"secs": dt,
"heap": [h0, h1],
"uptime": [u0, u1],
"pass": bf_ok,
}
)
# Concurrency: bad-cred hammer threads while a valid client streams /events
h0, u0 = heap(), uptime()
stop = time.monotonic() + A.conc
stats = {
"bad": {},
"sse_bytes": 0,
"sse_states": 0,
"sse_maxgap": 0.0,
"sse_errors": 0,
"sse_opens": 0,
"sse_closes": 0,
}
lock = threading.Lock()
def hammer() -> None:
while time.monotonic() < stop:
r = raw(
build("GET", "/", "Basic " + HASH[:-1] + flip(HASH[-1])),
timeout=5,
read_body=False,
)
k = str(r["status"] if r["status"] is not None else r["err"])
with lock:
stats["bad"][k] = stats["bad"].get(k, 0) + 1
def sse() -> None:
last = time.monotonic()
while time.monotonic() < stop:
try:
with socket.create_connection((HOST, 80), timeout=10) as s:
s.sendall(
build("GET", "/events", GOOD, b"Accept: text/event-stream\r\n")
)
stats["sse_opens"] += 1
s.settimeout(1)
while time.monotonic() < stop:
try:
d = s.recv(8192)
except TimeoutError:
continue
if not d:
stats["sse_closes"] += 1
break
now = time.monotonic()
n = d.count(b"event: state")
if n:
stats["sse_maxgap"] = max(stats["sse_maxgap"], now - last)
last = now
stats["sse_states"] += n
stats["sse_bytes"] += len(d)
except OSError as e:
stats["sse_errors"] += 1
p(f"SSE error {type(e).__name__}: {e}")
time.sleep(0.5)
# a stream that stopped early leaves the gap open until the end of the phase
stats["sse_maxgap"] = max(stats["sse_maxgap"], time.monotonic() - last)
ts = [threading.Thread(target=sse)] + [
threading.Thread(target=hammer) for _ in range(A.conc_threads)
]
for th in ts:
th.start()
for th in ts:
th.join()
time.sleep(3)
h1, u1 = heap(), uptime()
# at least one completed wrong-credential request per second, or the phase ran without its load
bad_n = sum(stats["bad"].values())
c_ok = (
bad_n >= A.conc
and set(stats["bad"]) <= {"401"}
and stats["sse_states"] > A.conc * 2
and stats["sse_errors"] == 0
and stats["sse_maxgap"] < 5
and heap_ok(h0, h1)
and u1 is not None
and u0 is not None
and u1 > u0
)
p(
f"{'PASS' if c_ok else 'FAIL'} concurrency {A.conc:.0f}s x{A.conc_threads}: bad {bad_n} {stats['bad']}; "
f"SSE states {stats['sse_states']} ({stats['sse_states'] / A.conc:.1f}/s) opens {stats['sse_opens']} closes "
f"{stats['sse_closes']} max gap {stats['sse_maxgap']:.1f}s; heap {h0}->{h1} uptime {u0}->{u1}"
)
ROWS.append(
{
"case": "concurrency",
**stats,
"heap": [h0, h1],
"uptime": [u0, u1],
"pass": c_ok,
}
)
def mann_whitney(a: list[float], b: list[float]) -> tuple[float, float]:
"""U and two sided normal-approximation z (no tie correction; fine for continuous timings)."""
allv = sorted([(v, 0) for v in a] + [(v, 1) for v in b])
ranks = [0.0] * len(allv)
i = 0
while i < len(allv):
j = i
while j + 1 < len(allv) and allv[j + 1][0] == allv[i][0]:
j += 1
for k in range(i, j + 1):
ranks[k] = (i + j) / 2 + 1
i = j + 1
r1 = sum(r for r, (_, g) in zip(ranks, allv, strict=False) if g == 0)
n1, n2 = len(a), len(b)
u = r1 - n1 * (n1 + 1) / 2
mu, sd = n1 * n2 / 2, (n1 * n2 * (n1 + n2 + 1) / 12) ** 0.5
return u, (u - mu) / sd
def timing() -> None:
first = "Basic " + flip(HASH[0]) + HASH[1:]
last = "Basic " + HASH[:-1] + flip(HASH[-1])
res: dict[str, list[float]] = {"first": [], "last": []}
bad = 0
for i in range(A.timing * 2):
which = "first" if (i % 2 == 0) ^ (random.random() < 0.5) else "last"
r = raw(
build("GET", "/sensor/Fast%20counter", first if which == "first" else last),
timeout=5,
read_body=False,
)
if r["status"] == 401 and r.get("ttfb"):
res[which].append(r["ttfb"] * 1000)
else:
bad += 1
# quantiles needs two samples per side and the z score some more; too few means the phase did not run
if min(len(res["first"]), len(res["last"])) < 10:
p(
f"FAIL timing: too few 401 samples (first {len(res['first'])}, last {len(res['last'])}); non-401 {bad}"
)
ROWS.append(
{
"case": "timing",
"n": {k: len(v) for k, v in res.items()},
"non401": bad,
"pass": False,
}
)
return
out = {}
for k, v in res.items():
out[k] = {
"n": len(v),
"mean_ms": statistics.mean(v),
"median_ms": statistics.median(v),
"p10": statistics.quantiles(v, n=10)[0],
"p90": statistics.quantiles(v, n=10)[-1],
}
u, z = mann_whitney(res["first"], res["last"])
# more than 5% of the requests without a 401 means the sample is not the request under test
ok = abs(z) < 1.96 and bad <= A.timing * 2 // 20
p(
f"{'PASS' if ok else 'FAIL'} timing (ms to first byte of 401): first-byte-wrong {out['first']}; "
f"last-byte-wrong {out['last']}; Mann-Whitney U {u:.0f} z {z:.2f} (|z|<1.96: no difference at 5%); "
f"non-401 {bad}"
)
ROWS.append({"case": "timing", **out, "u": u, "z": z, "non401": bad, "pass": ok})
def ota(path: str) -> None:
data = pathlib.Path(path).read_bytes()
body, ct = mp_body(data)
u0 = uptime()
r = raw(build("POST", "/update", GOOD, body=body, ctype=ct), timeout=120)
p(
f"OTA with credentials: status {r['status']} body {r['body'][:60]!r} err {r['err']} uptime before {u0}"
)
ok = r["status"] == 200
for _ in range(60):
time.sleep(2)
u1 = uptime()
if u1 is not None and u0 is not None and u1 < u0:
p(f"device back after OTA, uptime {u1}")
break
else:
ok = False
p("device not seen rebooted after OTA")
ROWS.append(
{"case": "good POST /update (real OTA)", "status": r["status"], "pass": ok}
)
def main() -> None:
global A # noqa: PLW0603
ap = argparse.ArgumentParser()
ap.add_argument("host")
ap.add_argument("user")
ap.add_argument("password")
ap.add_argument("out")
ap.add_argument("--only", default="")
ap.add_argument("--brute", type=int, default=600)
ap.add_argument("--timing", type=int, default=300)
ap.add_argument("--conc", type=float, default=60)
ap.add_argument("--conc-threads", type=int, default=4)
ap.add_argument("--ota", default="")
ap.add_argument("--skip-heavy", action="store_true")
ap.add_argument(
"--heap-margin",
type=int,
default=8192,
help="largest heap drop a heavy phase may leave",
)
A = ap.parse_args()
only = set(filter(None, A.only.split(",")))
if unknown := only - {"cases", "heavy", "timing"}:
ap.error(f"unknown --only phases {sorted(unknown)}")
configure(A.host, A.user, A.password, A.out)
try:
if not only or "cases" in only:
run_cases()
if (not only and not A.skip_heavy) or "heavy" in only:
heavy()
if (not only and not A.skip_heavy) or "timing" in only:
timing()
if A.ota:
ota(A.ota)
finally:
# keep the rows of the phases that ran even when a later one raised
pathlib.Path(A.out + "-cases.json").write_text(
json.dumps(ROWS, indent=1, default=str), encoding="utf-8"
)
# informational cases show library behaviour that dev and the PR share; they are reported, not judged
fails = [r["case"] for r in ROWS if not r.get("pass") and not r.get("info")]
infos = [r["case"] for r in ROWS if not r.get("pass") and r.get("info")]
if infos:
p(f"informational, not counted: {infos}")
p(f"TOTAL {len(ROWS)} rows, fails {len(fails)}: {fails}")
sys.exit(1 if fails or not ROWS else 0)
if __name__ == "__main__":
main()
@@ -1,36 +0,0 @@
"""Compare two authfuzz.py runs (dev vs PR). usage: compare.py DEV_PREFIX PR_PREFIX
Exits 1 when any case differs or exists on one side only."""
import json
import pathlib
import sys
a = {
r["case"]: r
for r in json.loads(
pathlib.Path(sys.argv[1] + "-cases.json").read_text(encoding="utf-8")
)
}
b = {
r["case"]: r
for r in json.loads(
pathlib.Path(sys.argv[2] + "-cases.json").read_text(encoding="utf-8")
)
}
print(f"{'case':45} {'dev':>12} {'pr':>12}")
diffs = 0
for k in list(dict.fromkeys(list(a) + list(b))):
ra, rb = a.get(k, {}), b.get(k, {})
fa = f"{ra.get('status')}/{'ok' if ra.get('pass') else 'FAIL'}" if ra else "-"
fb = f"{rb.get('status')}/{'ok' if rb.get('pass') else 'FAIL'}" if rb else "-"
# A case on one side only differs too: the missing side is (None, None)
differs = (ra.get("status"), ra.get("pass")) != (rb.get("status"), rb.get("pass"))
diffs += differs
mark = " <== differs" if differs else ""
print(f"{k:45} {fa:>12} {fb:>12}{mark}")
na, nb = a.get("noauth GET /", {}), b.get("noauth GET /", {})
print("\n401 head dev:\n" + na.get("head", "") + "\nbody:", repr(na.get("body_head")))
print("\n401 head pr:\n" + nb.get("head", "") + "\nbody:", repr(nb.get("body_head")))
print(f"\n{diffs} case(s) differ")
sys.exit(1 if diffs else 0)
@@ -1,58 +0,0 @@
# Captive portal client: the client-wifisave package (one bogus save, then the real credentials 20 s later) plus
# probes of portal and web_server routes while it is on the device's AP. Lines: "probe <what> status N",
# "bogus save status N", "real save status N". Judge the probes with ../approbe_check.py CLIENT_LOG.
# Substitutions: name, ap_ssid, web_user, web_pass (creds-*.yaml).
substitutions:
ap_ssid: wbacp
real_ssid: !secret wifi_ssid
real_psk: !secret wifi_password
bogus_saves: "1"
save_interval: 20s
packages:
client: !include ../../../templates/client-wifisave.yaml
wifi:
on_connect:
- if:
condition:
lambda: |-
char b[40];
return strncmp(id(wifi_id).get_ip_addresses()[0].str_to(b), "192.168.4.", 10) == 0;
then:
- script.execute: probe
script:
- id: probe
mode: single
then:
- delay: 5s
- http_request.get:
url: "http://192.168.4.1/"
capture_response: true
on_response:
- lambda: ESP_LOGI("test", "probe portal_root status %d len %d", response->status_code, (int) body.size());
on_error:
- lambda: ESP_LOGW("test", "probe portal_root status -1");
- http_request.get:
url: "http://192.168.4.1/config.json"
capture_response: true
on_response:
- lambda: ESP_LOGI("test", "probe config_json status %d len %d", response->status_code, (int) body.size());
on_error:
- lambda: ESP_LOGW("test", "probe config_json status -1");
- http_request.post:
url: "http://192.168.4.1/switch/Test%20switch/toggle"
capture_response: true
on_response:
- lambda: ESP_LOGI("test", "probe post_noauth status %d", response->status_code);
on_error:
- lambda: ESP_LOGW("test", "probe post_noauth status -1");
- http_request.post:
url: "http://192.168.4.1/switch/Test%20switch/toggle"
request_headers:
Authorization: !lambda |-
static const char CREDS[] = "${web_user}:${web_pass}";
return "Basic " + esphome::base64_encode((const uint8_t *) CREDS, sizeof(CREDS) - 1);
capture_response: true
on_response:
- lambda: ESP_LOGI("test", "probe post_auth status %d", response->status_code);
on_error:
- lambda: ESP_LOGW("test", "probe post_auth status -1");
@@ -1,8 +0,0 @@
# Captive portal client on a classic ESP32 devkit (name set with -s name).
esp32:
board: esp32dev
framework:
type: esp-idf
packages:
creds: !include creds-short.yaml
c: !include ap-client-common.yaml
@@ -1,3 +0,0 @@
substitutions:
web_user: hwtest-long-username-abcdefghij
web_pass: hwtest-long-password-0123456789xyz
@@ -1,3 +0,0 @@
substitutions:
web_user: hw
web_pass: k7q
@@ -1,10 +0,0 @@
# D1 mini that falls back to its AP and serves the captive portal next to the Basic auth web_server (name set with
# -s name; use a fresh name per build so no saved network carries over).
substitutions:
ap_ssid: wbacp
esp8266:
board: d1_mini
packages:
creds: !include creds-short.yaml
hwtest: !include ../../../templates/dut-fallback-ap.yaml
common: !include wba-common.yaml
@@ -1,10 +0,0 @@
# D1 mini on the station network with the Basic auth web_server (name set with -s name).
esp8266:
board: d1_mini
wifi:
ssid: !secret wifi_ssid
password: !secret wifi_password
power_save_mode: none
packages:
creds: !include creds-short.yaml
common: !include wba-common.yaml
@@ -1,55 +0,0 @@
#!/bin/bash
# run.sh NAME CONFIG KIND PORT MODE: build CONFIG (a file in this directory) as NAME from TREE, flash it over serial
# (unless MODE contains noflash), capture the serial log, run authfuzz.py and count crash lines.
# KIND esp|pico|bk|bw; MODE full (cases, heavy, timing and an OTA of its own image) | cases
# Exit 0 only when every authfuzz case passed and the serial log shows no crash; 1 otherwise.
# env: TREE, PY, CREDS (default creds-short.yaml; its web_user and web_pass are built in and used by authfuzz)
D=$(cd "$(dirname "$0")" && pwd); N=${1:?name}; C=${2:?config}; K=${3:?kind}; P=${4:?port}; M=${5:?mode}
T=${TREE:?set TREE}; PY=${PY:-$T/venv/bin/python}; S=$(dirname "$(dirname "$D")")
CR=$D/${CREDS:-creds-short.yaml}
read -r U W < <($PY -c 'import sys, yaml; s = yaml.safe_load(open(sys.argv[1]))["substitutions"]; print(s["web_user"], s["web_pass"])' "$CR") ||
{ echo "cannot read $CR"; exit 1; }
"$S/pio_build.sh" compile "$N" "$D/$C" -s web_user "$U" -s web_pass "$W" || exit 1
B=$D/.esphome/build/$N/.pioenvs/$N
PC=${PIO_CORE:-$D/pio}; LTPY=$PC/penv/.libretiny/bin/python; LTL=$(ls -d "$PC"/platforms/libretiny* 2>/dev/null | head -1)
OPT=""
case $K in
esp|pico) IMG=$B/firmware.bin ;;
bk) IMG=$B/firmware.uf2; OPT="--reset" ;;
bw) IMG=$B/firmware.uf2; OPT="--released" ;;
*) echo "unknown kind $K"; exit 1 ;;
esac
pkill -f "[s]erial_capture.py $P "; sleep 1
if [[ $M != *noflash* ]]; then
case $K in
esp|pico) "$S/pio_build.sh" upload "$N" "$D/$C" "$P" -s web_user "$U" -s web_pass "$W" || exit 1 ;;
bk) FRC=1; for _ in 1 2 3; do timeout 400 "$LTPY" "$S/flash_bk72xx.py" -L "$LTL" flash write -d "$P" "$IMG" > "$D/$N-upload.log" 2>&1 && { FRC=0; break; }; done
[ $FRC = 0 ] || { echo "$N flash failed (see $D/$N-upload.log)"; exit 1; } ;;
bw) timeout 600 "$LTPY" "$S/flash_rtl87xx_kit.py" -L "$LTL" flash write -d "$P" "$IMG" > "$D/$N-upload.log" 2>&1 ||
{ echo "$N flash failed (see $D/$N-upload.log)"; exit 1; } ;;
esac
fi
[ "$K" = pico ] && { sleep 3; for _ in $(seq 1 30); do [ -e "$P" ] && break; sleep 1; done; sleep 2; }
L=$D/$N-serial.log
$PY "$S/serial_capture.py" "$P" 3000 "$L" $OPT 2> "$D/$N-cap.err" &
CAP=$!
trap 'kill $CAP 2>/dev/null' EXIT
HOST=$("$S/resolve.sh" "$N" 60) || exit 1
echo "$HOST" > "$D/$N-host.txt"; sleep 8
case $M in
full*) $PY -I "$S/web_auth/authfuzz.py" "$HOST" "$U" "$W" "$D/$N" --ota "$IMG" ;;
*) $PY -I "$S/web_auth/authfuzz.py" "$HOST" "$U" "$W" "$D/$N" --only cases ;;
esac
AF=$?
sleep 5
# a capture that died early watched nothing, so its empty log must not count as a clean run
CAPOK=1; kill -0 $CAP 2>/dev/null || { CAPOK=0; echo "the capture ended before the run did (see $D/$N-cap.err)"; }
# a live capture that only retried a missing port watched nothing either
[ "$(grep -a -c -v '^\[[0-9:.]*\]\[capture\]' "$L")" -gt 0 ] || { CAPOK=0; echo "no firmware lines in $L"; }
kill $CAP 2>/dev/null; wait $CAP 2>/dev/null; trap - EXIT
CRASH=$(grep -a -c -E 'Panic |Exception \(|Fatal exception|HardFault|Hard Fault|CRASH|abort\(\)|Soft WDT|wdt reset|assert|Backtrace' "$L")
echo "SERIAL crash lines $CRASH," \
"boots $(grep -a -c -E 'Log initialized|Reset reason' "$L"), port losses $(grep -a -c 'port lost' "$L"), lines $(wc -l < "$L")" | tee "$D/$N-serialsum.txt"
grep -a "Heap free" "$L" | sed -n '1p;$p' | tee -a "$D/$N-serialsum.txt"
[ "$AF" = 0 ] && [ "$CRASH" = 0 ] && [ $CAPOK = 1 ] || { echo "FAIL (authfuzz exit $AF, crash lines $CRASH, capture ok $CAPOK)"; exit 1; }
echo PASS
@@ -1,28 +0,0 @@
# web_server v3 with Basic auth and web OTA, a switch and a light on top of the diagnostics package (fast sensor,
# heap, reset reason). Substitutions: name, web_user, web_pass (from creds-*.yaml).
packages:
diagnostics: !include ../../../templates/diagnostics.yaml
ota:
- platform: web_server
web_server:
port: 80
version: 3
auth:
username: ${web_user}
password: ${web_pass}
type: basic
switch:
- platform: template
name: Test switch
optimistic: true
output:
- platform: template
id: light_out
type: float
write_action:
- lambda: ESP_LOGD("test", "light out %.3f", state);
light:
- platform: monochromatic
name: Test light
output: light_out
default_transition_length: 0s
@@ -1,80 +0,0 @@
"""Send one hostile request at a time and check uptime afterwards, to tie a reboot to a request.
usage: isolate.py HOST USER PASS [ROUNDS] [CASE,...] (one line per case: uptime before/after, rebooted yes/no)
Exits 1 when any request was followed by a reboot or a device that stopped answering.
Reuses authfuzz.py's raw socket sender and request builder.
"""
import pathlib
import sys
import time
sys.path.insert(0, str(pathlib.Path(__file__).resolve().parent))
import authfuzz as af # noqa: E402
# the Uptime sensor updates every second; allow that plus request latency before calling a boot time later
BOOT_SLACK_S = 3.0
def boot_time() -> tuple[float | None, float | None]:
"""(uptime, estimated boot time on the local monotonic clock), retried for about 50 s so a rebooting device
can come back; (None, None) when it never answers."""
for _ in range(25):
if (u := af.uptime()) is not None:
return float(u), time.monotonic() - float(u)
time.sleep(2)
return None, None
def main() -> None:
host, user, password = sys.argv[1:4]
rounds = int(sys.argv[4]) if len(sys.argv) > 4 else 1
af.configure(host, user, password)
good = af.GOOD.encode()
def get(extra: bytes = b"", auth: bytes | None = None) -> bytes:
return af.build("GET", "/", auth, extra=extra)
cases = {
"nul_in_auth": get(auth=good + b"\x00garbage"),
"nul_in_other_header": get(b"X-A: a\x00b\r\n"),
"nul_in_request_line": b"GET /\x00 HTTP/1.1\r\nHost: x\r\n\r\n",
"auth_8k": get(auth=b"Basic " + b"A" * 8192),
"garbage_32k": get(b"X-Garbage: " + b"Z" * 32768 + b"\r\n"),
"nonl_64k": b"GET / HTTP/1.1\r\nHost: x\r\nX-G: " + b"Q" * 65536,
"headers_200": get(
b"".join(f"X-H{i}: {'v' * 60}\r\n".encode() for i in range(200))
),
"control": get(auth=good),
}
only = sys.argv[5].split(",") if len(sys.argv) > 5 else list(cases)
u0, b0 = boot_time()
reboots = 0
for r in range(rounds):
for name in only:
t = time.strftime("%H:%M:%S")
res = af.raw(cases[name], timeout=6, read_body=False)
head = res["head"].split("\r\n", 1)[0] or res["err"] or "closed"
time.sleep(4)
u1, b1 = boot_time()
# a later boot time catches a reboot even when the uptime climbed past the previous reading
rb = (
"REBOOT"
if (b0 is None or b1 is None or b1 - b0 > BOOT_SLACK_S)
else "ok"
)
reboots += rb == "REBOOT"
print(
f"[{t}] round {r} {name:22} -> {head[:40]:40} uptime {u0} -> {u1} {rb}",
flush=True,
)
u0, b0 = u1, b1
print(
"PASS" if not reboots else f"FAIL: {reboots} requests followed by a reboot",
flush=True,
)
sys.exit(1 if reboots else 0)
if __name__ == "__main__":
main()
@@ -1,27 +0,0 @@
# Package for soak and load tests: DEBUG logs, esphome OTA, a 1 s uptime, the free heap and reset reason from
# `debug:`, and a 200 ms template counter that keeps state traffic flowing. Substitution: name.
esphome:
name: ${name}
logger:
level: DEBUG
ota:
- platform: esphome
debug:
update_interval: 5s
text_sensor:
- platform: debug
reset_reason:
name: Reset reason
sensor:
- platform: uptime
name: Uptime
update_interval: 1s
- platform: debug
free:
name: Heap free
- platform: template
name: Fast counter
update_interval: 200ms
lambda: |-
static uint32_t n = 0;
return n++;
+4 -1
View File
@@ -146,9 +146,11 @@ esphome/components/cover/* @esphome/core
esphome/components/cs5460a/* @balrog-kun
esphome/components/cse7761/* @berfenger
esphome/components/cst226/* @clydebarrow
esphome/components/cst3240/* @tpruvot
esphome/components/cst328/* @latonita
esphome/components/cst816/* @clydebarrow
esphome/components/cst9220/* @clydebarrow
esphome/components/cstxxx/* @clydebarrow @latonita
esphome/components/ct_clamp/* @jesserockz
esphome/components/current_based/* @djwmarcx
esphome/components/cw2017/* @exciton
@@ -466,6 +468,7 @@ esphome/components/nfc/* @jesserockz @kbx81
esphome/components/noblex/* @AGalfra
esphome/components/noise/* @esphome/core
esphome/components/npi19/* @bakerkj
esphome/components/npm1300/* @tomaszduda23
esphome/components/nrf52/* @tomaszduda23
esphome/components/ns2009/* @tchilov
esphome/components/ns2009/touchscreen/* @tchilov
@@ -741,7 +744,7 @@ esphome/components/web_server_idf/* @dentra
esphome/components/weikai/* @DrCoolZic
esphome/components/weikai_i2c/* @DrCoolZic
esphome/components/weikai_spi/* @DrCoolZic
esphome/components/whirlpool/* @arpena @glmnet
esphome/components/whirlpool/* @arpena @glmnet @Rodrigoah
esphome/components/whynter/* @aeonsablaze
esphome/components/wiegand/* @ssieb
esphome/components/wireguard/* @droscy @lhoracek @thomas0bernard
+42 -7
View File
@@ -1,3 +1,5 @@
from typing import Any
from esphome import automation, pins
from esphome.automation import maybe_simple_id
import esphome.codegen as cg
@@ -13,6 +15,7 @@ from esphome.const import (
CONF_VALUE,
CONF_WAIT_TIME,
)
from esphome.types import ConfigType
CODEOWNERS = ["@lygris", "@gabest11"]
DEPENDENCIES = ["spi"]
@@ -22,6 +25,8 @@ MULTI_CONF = True
ns = cg.esphome_ns.namespace("cc1101")
CC1101Component = ns.class_("CC1101Component", cg.Component, spi.SPIDevice)
CC1101Listener = ns.class_("CC1101Listener")
PACKET_TYPE = cg.std_vector.template(cg.uint8)
RX_FIFO_SIZE = 64
# Config keys
CONF_RX_ATTENUATION = "rx_attenuation"
@@ -241,20 +246,34 @@ CONFIG_MAP = {
cv.Optional(CONF_BS_POST_KI): cv.enum(BS_POST_KI, upper=False),
cv.Optional(CONF_BS_POST_KP): cv.enum(BS_POST_KP, upper=False),
cv.Optional(CONF_PACKET_MODE, default=False): cv.boolean,
cv.Optional(CONF_PACKET_LENGTH): cv.uint8_t,
cv.Optional(CONF_CRC_ENABLE, default=False): cv.boolean,
cv.Optional(CONF_WHITENING, default=False): cv.boolean,
}
def _validate_packet_mode(config):
if config.get(CONF_PACKET_MODE, False):
def _is_long_packet(packet_length: Any) -> bool:
"""A packet_length that does not fit the RX FIFO, or is a lambda."""
return cg.is_template(packet_length) or (packet_length or 0) > RX_FIFO_SIZE
def _validate_packet_mode(config: ConfigType) -> ConfigType:
packet_mode = config.get(CONF_PACKET_MODE, False)
packet_length = config.get(CONF_PACKET_LENGTH)
if packet_mode:
if CONF_GDO0_PIN not in config:
raise cv.Invalid("gdo0_pin is required when packet_mode is enabled")
if CONF_PACKET_LENGTH not in config:
if packet_length is None:
raise cv.Invalid("packet_length is required when packet_mode is enabled")
if config[CONF_PACKET_LENGTH] > 64:
raise cv.Invalid("packet_length must be <= 64 (FIFO size)")
elif cg.is_template(packet_length):
raise cv.Invalid("packet_mode is required when packet_length is a lambda")
if (
packet_mode
and _is_long_packet(packet_length)
and config.get(CONF_CRC_ENABLE, False)
):
raise cv.Invalid(
"crc_enable is not supported with packet_length > 64 or a lambda"
)
return config
@@ -263,6 +282,9 @@ CONFIG_SCHEMA = cv.All(
{
cv.GenerateID(): cv.declare_id(CC1101Component),
cv.Optional(CONF_GDO0_PIN): pins.internal_gpio_input_pin_schema,
cv.Optional(CONF_PACKET_LENGTH): cv.templatable(
cv.int_range(min=0, max=1024)
),
cv.Optional(CONF_ON_PACKET): automation.validate_automation(single=True),
}
)
@@ -282,11 +304,24 @@ async def to_code(config):
cg.add(getattr(var, f"set_{key}")(config[key]))
await cg.add_pin(var.set_gdo0_pin, config.get(CONF_GDO0_PIN))
if (packet_length := config.get(CONF_PACKET_LENGTH)) is not None:
# packet_mode can also be switched on at run time, so this does not check it
if _is_long_packet(packet_length):
cg.add_define("USE_CC1101_LONG_PACKET")
if cg.is_template(packet_length):
lambda_ = await cg.process_lambda(
packet_length,
[(PACKET_TYPE.operator("ref").operator("const"), "x")],
return_type=cg.int32,
)
cg.add(var.set_packet_length_lambda(lambda_))
else:
cg.add(var.set_packet_length(packet_length))
if CONF_ON_PACKET in config:
await automation.build_automation(
var.get_packet_trigger(),
[
(cg.std_vector.template(cg.uint8), "x"),
(PACKET_TYPE, "x"),
(cg.float_, "freq_offset"),
(cg.float_, "rssi"),
(cg.uint8, "lqi"),
+353 -33
View File
@@ -1,8 +1,10 @@
#include "cc1101.h"
#include "cc1101pa.h"
#include "esphome/core/application.h"
#include "esphome/core/helpers.h"
#include "esphome/core/log.h"
#include "esphome/core/progmem.h"
#include <algorithm>
#include <cmath>
#include <cstring>
@@ -14,9 +16,15 @@ namespace esphome::cc1101 {
ESPHOME_LOG_TAG(TAG, "cc1101");
static constexpr size_t TX_FIFO_SIZE = 64;
static constexpr size_t RX_FIFO_SIZE = 64;
// RX FIFO threshold for FIFO_THR = 7 in long packet mode
static constexpr uint8_t RX_FIFO_THRESHOLD = 32;
// The crystal takes about 150 us to start after CSn goes low
static constexpr uint32_t WAKEUP_DELAY_US = 200;
static float freq_offset_hz(uint8_t freqest) { return static_cast<int8_t>(freqest) * (XTAL_FREQUENCY / (1 << 14)); }
static float rssi_dbm(int8_t rssi) { return (rssi * RSSI_STEP) - RSSI_OFFSET; }
// MDMCFG2 MOD_FORMAT values
PROGMEM_STRING_TABLE(ModulationStrings, "2-FSK", "GFSK", "UNUSED", "ASK/OOK", "4-FSK", "UNUSED", "UNUSED", "MSK");
@@ -114,8 +122,6 @@ CC1101Component::CC1101Component() {
memset(this->pa_table_, 0, sizeof(this->pa_table_));
}
void IRAM_ATTR CC1101Component::gpio_intr(CC1101Component *arg) { arg->enable_loop_soon_any_context(); }
void CC1101Component::setup() {
this->spi_setup();
@@ -193,7 +199,220 @@ void CC1101Component::call_listeners_(const std::vector<uint8_t> &packet, float
this->packet_trigger_.trigger(packet, freq_offset, rssi, lqi);
}
float CC1101Component::symbol_rate_() const {
return (((256.0f + this->state_.DRATE_M) * (1 << this->state_.DRATE_E)) / (1 << 28)) * XTAL_FREQUENCY;
}
#ifdef USE_CC1101_LONG_PACKET
bool CC1101Component::is_long_packet_mode_() const {
return this->packet_mode_ && (this->packet_length_ > RX_FIFO_SIZE || this->packet_length_func_ != nullptr);
}
void CC1101Component::arm_length_(uint16_t length) {
// Over 255 bytes or unknown starts in infinite mode (datasheet section 15.2.2). PKTLEN must not be 0, so a
// multiple of 256 stays in infinite mode and ends on the MCU byte count alone.
this->packet_expected_length_ = length;
this->state_.LENGTH_CONFIG = static_cast<uint8_t>(length > 0 && length < 256 ? LengthConfig::LENGTH_CONFIG_FIXED
: LengthConfig::LENGTH_CONFIG_INFINITE);
this->state_.PKTLEN = length % 256 == 0 ? 0xFF : static_cast<uint8_t>(length % 256);
}
void CC1101Component::reset_long_packet_rx_() {
this->packet_.clear();
this->packet_last_byte_ms_ = 0;
this->high_freq_.stop();
this->arm_length_(this->packet_length_func_ != nullptr ? 0 : this->packet_length_);
}
void CC1101Component::restart_long_packet_rx_() {
this->enter_idle_();
this->enter_rx_();
this->disable_loop();
}
void CC1101Component::maybe_switch_long_packet_to_fixed_() {
const uint16_t expected = this->packet_expected_length_;
if (this->state_.LENGTH_CONFIG == static_cast<uint8_t>(LengthConfig::LENGTH_CONFIG_FIXED) || expected == 0 ||
expected % 256 == 0) {
return;
}
const uint16_t received = this->packet_.size();
if (expected - received >= 256) {
return;
}
// PKTLEN keeps the (length % 256) set at the start, as in TI Design Note DN500 section 2.3
this->state_.LENGTH_CONFIG = static_cast<uint8_t>(LengthConfig::LENGTH_CONFIG_FIXED);
this->write_(Register::PKTCTRL0);
ESP_LOGVV(TAG, "Switched long packet RX to fixed mode: received=%u expected=%u pktlen=%u", received, expected,
this->state_.PKTLEN);
}
void CC1101Component::finish_long_packet_() {
this->packet_.resize(this->packet_expected_length_);
this->read_(Register::FREQEST);
this->read_(Register::LQI);
const float freq_offset = freq_offset_hz(this->state_.FREQEST);
const uint8_t lqi = this->state_.LQI & STATUS_LQI_MASK;
// No CRC check: crc_enable is rejected for long packets, CRC_OK is only set after the last payload byte.
// Restart RX before dispatching so a callback may change settings; swapping back keeps the reserved capacity
// unless a callback reserved a larger buffer.
std::vector<uint8_t> packet;
packet.swap(this->packet_);
this->restart_long_packet_rx_();
this->call_listeners_(packet, freq_offset, rssi_dbm(this->long_packet_rssi_), lqi);
if (packet.capacity() > this->packet_.capacity()) {
packet.clear();
this->packet_.swap(packet);
}
}
void CC1101Component::handle_long_packet_(uint8_t rx_bytes) {
// Leave one byte in the FIFO unless it holds the rest of the packet (datasheet section 20)
uint16_t to_read = rx_bytes > 0 ? rx_bytes - 1 : 0;
if (this->packet_expected_length_ > 0) {
const uint16_t remaining = this->packet_expected_length_ - this->packet_.size();
if (remaining <= rx_bytes) {
to_read = remaining;
}
}
if (to_read == 0) {
if (this->packet_.empty()) {
return;
}
// Discard a packet when no byte arrived for the air time of PACKET_IDLE_TIMEOUT_BYTES
const uint32_t timeout_ms =
std::max(static_cast<uint32_t>(std::ceil((8.0f * PACKET_IDLE_TIMEOUT_BYTES * 1000.0f) / this->symbol_rate_())),
PACKET_IDLE_TIMEOUT_MIN_MS);
if (App.get_loop_component_start_time() - this->packet_last_byte_ms_ > timeout_ms) {
ESP_LOGW(TAG, "Packet receive timeout, discarding packet: received=%u expected=%u fifo=%u",
static_cast<uint16_t>(this->packet_.size()), this->packet_expected_length_, rx_bytes);
this->restart_long_packet_rx_();
}
return;
}
const size_t old_size = this->packet_.size();
if (old_size == 0) {
// The RSSI register follows the live signal; sample it while the packet is still on air
this->read_(Register::RSSI);
this->long_packet_rssi_ = this->state_.RSSI;
// The FIFO threshold signal does not fire for the bytes after the last threshold crossing; poll until done
this->high_freq_.start();
}
// Read through an aligned buffer: for an unaligned target the ESP32 SPI driver allocates a bounce buffer
alignas(4) uint8_t staged[RX_FIFO_SIZE];
this->read_(Register::FIFO, staged, to_read);
this->packet_.resize(old_size + to_read);
std::memcpy(this->packet_.data() + old_size, staged, to_read);
this->packet_last_byte_ms_ = App.get_loop_component_start_time();
ESP_LOGVV(TAG, "Drained RX FIFO: read=%u received=%u expected=%u fifo=%u", to_read,
static_cast<uint16_t>(this->packet_.size()), this->packet_expected_length_, rx_bytes);
if (this->packet_expected_length_ == 0 && this->packet_length_func_ != nullptr) {
const int32_t length = this->packet_length_func_(this->packet_);
if (length < 0) {
ESP_LOGVV(TAG, "Packet length lambda discarded packet: received=%u", static_cast<uint16_t>(this->packet_.size()));
this->restart_long_packet_rx_();
return;
}
if (length > MAX_PACKET_LENGTH) {
ESP_LOGW(TAG, "Packet length exceeds maximum, discarding packet: length=%" PRId32 " max=%u", length,
MAX_PACKET_LENGTH);
this->restart_long_packet_rx_();
return;
}
if (length > 0) {
this->arm_length_(static_cast<uint16_t>(length));
// A packet shorter than the first FIFO drain is already complete; finish_long_packet_() truncates it
if (this->packet_expected_length_ <= this->packet_.size()) {
this->finish_long_packet_();
return;
}
this->write_(Register::PKTLEN);
this->write_(Register::PKTCTRL0);
ESP_LOGVV(TAG, "Resolved packet length: received=%u expected=%u pktlen=%u",
static_cast<uint16_t>(this->packet_.size()), this->packet_expected_length_, this->state_.PKTLEN);
}
}
// Only reachable while the length is still unknown; a known length caps every read
if (this->packet_.size() > MAX_PACKET_LENGTH) {
ESP_LOGW(TAG, "Packet length exceeds maximum, discarding packet: length=%u max=%u",
static_cast<uint16_t>(this->packet_.size()), MAX_PACKET_LENGTH);
this->restart_long_packet_rx_();
return;
}
this->maybe_switch_long_packet_to_fixed_();
if (this->packet_expected_length_ > 0 && this->packet_.size() >= this->packet_expected_length_) {
this->finish_long_packet_();
}
}
void CC1101Component::loop_long_packet_() {
if (this->sleeping_ || this->state_.PKT_FORMAT != static_cast<uint8_t>(PacketFormat::PACKET_FORMAT_FIFO) ||
this->gdo0_pin_ == nullptr) {
this->high_freq_.stop();
this->disable_loop();
return;
}
// While GDO0 (RX FIFO threshold) is high, the FIFO holds at least RX_FIFO_THRESHOLD bytes, so drain without
// reading RXBYTES. At high data rates RXBYTES changes between two reads, so waiting for two reads that agree
// and retrying on the next loop pass lets the FIFO overflow. MARCSTATE does not change during RX.
for (uint8_t i = 0; i < RX_FIFO_SIZE / RX_FIFO_THRESHOLD + 2 && this->gdo0_pin_->digital_read(); i++) {
this->read_(Register::MARCSTATE);
if (static_cast<State>(this->state_.MARC_STATE) == State::RXFIFO_OVERFLOW) {
ESP_LOGW(TAG, "RX FIFO overflow, discarding packet: received=%u expected=%u",
static_cast<uint16_t>(this->packet_.size()), this->packet_expected_length_);
this->restart_long_packet_rx_();
return;
}
this->handle_long_packet_(RX_FIFO_THRESHOLD);
if (this->packet_.empty()) {
return; // finished or discarded
}
}
// Below the threshold the next crossing raises GDO0 again, and after the packet the count no longer changes,
// so retrying on the next loop pass is safe here. RXBYTES can be misread while bytes arrive (TI SWRZ020), so
// only trust two consecutive reads that agree.
this->read_(Register::RXBYTES);
uint8_t rx_bytes = this->state_.RXBYTES;
bool stable = false;
for (uint8_t retries = RXBYTES_STABLE_RETRIES; retries > 0 && !stable; retries--) {
const uint8_t previous = rx_bytes;
this->read_(Register::RXBYTES);
rx_bytes = this->state_.RXBYTES;
stable = rx_bytes == previous;
}
if (!stable) {
return;
}
rx_bytes = this->state_.NUM_RXBYTES;
// More than the FIFO holds can only be a misread; the staged drain buffer is FIFO sized
if (this->state_.RXFIFO_OVERFLOW || rx_bytes > RX_FIFO_SIZE) {
ESP_LOGW(TAG, "RX FIFO overflow, discarding packet: received=%u expected=%u fifo=%u",
static_cast<uint16_t>(this->packet_.size()), this->packet_expected_length_, rx_bytes);
this->restart_long_packet_rx_();
return;
}
if (rx_bytes == 0 && this->packet_.empty() && !this->gdo0_pin_->digital_read()) {
this->disable_loop();
return;
}
this->handle_long_packet_(rx_bytes);
}
#endif
void CC1101Component::loop() {
#ifdef USE_CC1101_LONG_PACKET
if (this->is_long_packet_mode_()) {
this->loop_long_packet_();
return;
}
#endif
this->disable_loop();
if (this->sleeping_ || this->state_.PKT_FORMAT != static_cast<uint8_t>(PacketFormat::PACKET_FORMAT_FIFO) ||
this->gdo0_pin_ == nullptr || !this->gdo0_pin_->digital_read()) {
@@ -221,7 +440,7 @@ void CC1101Component::loop() {
payload_length = this->state_.PKTLEN;
expected_rx = payload_length;
}
if (payload_length == 0 || payload_length > 64 || rx_bytes != expected_rx) {
if (payload_length == 0 || payload_length > RX_FIFO_SIZE || rx_bytes != expected_rx) {
ESP_LOGW(TAG, "Invalid packet: rx_bytes %u, payload_length %u", rx_bytes, payload_length);
this->enter_idle_();
this->strobe_(Command::FRX);
@@ -235,8 +454,8 @@ void CC1101Component::loop() {
this->read_(Register::FREQEST);
this->read_(Register::RSSI);
this->read_(Register::LQI);
float freq_offset = static_cast<int8_t>(this->state_.FREQEST) * (XTAL_FREQUENCY / (1 << 14));
float rssi = (this->state_.RSSI * RSSI_STEP) - RSSI_OFFSET;
float freq_offset = freq_offset_hz(this->state_.FREQEST);
float rssi = rssi_dbm(this->state_.RSSI);
bool crc_ok = (this->state_.LQI & STATUS_CRC_OK_MASK) != 0;
uint8_t lqi = this->state_.LQI & STATUS_LQI_MASK;
if (this->state_.CRC_EN == 0 || crc_ok) {
@@ -256,7 +475,7 @@ void CC1101Component::loop() {
void CC1101Component::dump_config() {
int32_t freq = static_cast<int32_t>(this->state_.FREQ2 << 16 | this->state_.FREQ1 << 8 | this->state_.FREQ0) *
XTAL_FREQUENCY / (1 << 16);
float symbol_rate = (((256.0f + this->state_.DRATE_M) * (1 << this->state_.DRATE_E)) / (1 << 28)) * XTAL_FREQUENCY;
float symbol_rate = this->symbol_rate_();
float bw = XTAL_FREQUENCY / (8.0f * (4 + this->state_.CHANBW_M) * (1 << this->state_.CHANBW_E));
ESP_LOGCONFIG(TAG,
"CC1101:\n"
@@ -271,6 +490,20 @@ void CC1101Component::dump_config() {
LOG_STR_ARG(ModulationStrings::get_log_str(this->state_.MOD_FORMAT & 0x07, 0)), symbol_rate, bw,
this->output_power_effective_);
LOG_PIN(" CS Pin: ", this->cs_);
LOG_PIN(" GDO0 Pin: ", this->gdo0_pin_);
if (this->packet_mode_) {
#ifdef USE_CC1101_LONG_PACKET
if (this->packet_length_func_ != nullptr) {
ESP_LOGCONFIG(TAG, " Packet Length: lambda");
} else
#endif
if (this->packet_length_ == 0) {
ESP_LOGCONFIG(TAG, " Packet Length: variable");
} else {
ESP_LOGCONFIG(TAG, " Packet Length: %u", this->packet_length_);
}
}
}
void CC1101Component::begin_tx() {
@@ -316,6 +549,14 @@ void CC1101Component::reset() {
void CC1101Component::set_idle() {
ESP_LOGV(TAG, "Setting IDLE state");
this->enter_idle_();
#ifdef USE_CC1101_LONG_PACKET
if (this->is_long_packet_mode_()) {
// Drop a partial packet so a pending loop pass cannot finish it and re-enter RX
this->strobe_(Command::FRX);
this->reset_long_packet_rx_();
this->disable_loop();
}
#endif
}
void CC1101Component::power_down() {
@@ -393,7 +634,18 @@ void CC1101Component::enter_idle_() {
}
}
bool CC1101Component::enter_rx_() { return this->enter_calibrated_(State::RX, Command::RX); }
bool CC1101Component::enter_rx_() {
#ifdef USE_CC1101_LONG_PACKET
if (this->is_long_packet_mode_()) {
// Every RX entry starts a fresh long packet; callers leave the chip in IDLE, where SFRX is allowed
this->strobe_(Command::FRX);
this->reset_long_packet_rx_();
this->write_(Register::PKTLEN);
this->write_(Register::PKTCTRL0);
}
#endif
return this->enter_calibrated_(State::RX, Command::RX);
}
bool CC1101Component::enter_tx_() { return this->enter_calibrated_(State::TX, Command::TX); }
@@ -456,12 +708,22 @@ void CC1101Component::read_(Register reg, uint8_t *buffer, size_t length) {
CC1101Error CC1101Component::transmit_packet(const uint8_t *data, size_t len) {
if (this->state_.PKT_FORMAT != static_cast<uint8_t>(PacketFormat::PACKET_FORMAT_FIFO)) {
ESP_LOGW(TAG, "Cannot transmit packet: packet mode is not FIFO");
return CC1101Error::PARAMS;
}
#ifdef USE_CC1101_LONG_PACKET
// Long packet TX needs chunked FIFO refills and is not implemented
if (this->is_long_packet_mode_()) {
ESP_LOGW(TAG, "Cannot transmit packet: long packet mode is RX-only");
return CC1101Error::PARAMS;
}
#endif
const bool variable = this->state_.LENGTH_CONFIG == static_cast<uint8_t>(LengthConfig::LENGTH_CONFIG_VARIABLE);
// In variable length mode the FIFO also holds the length byte
if (len + (variable ? 1 : 0) > TX_FIFO_SIZE) {
ESP_LOGE(TAG, "Packet of %u bytes does not fit the TX FIFO", static_cast<unsigned>(len));
ESP_LOGW(TAG,
"Cannot transmit packet: long packet TX is not implemented, payload length %u exceeds TX FIFO limit %u",
static_cast<unsigned>(len), static_cast<unsigned>(TX_FIFO_SIZE));
return CC1101Error::PARAMS;
}
@@ -834,19 +1096,60 @@ void CC1101Component::set_bs_post_kp(BsPostKp value) {
}
}
void CC1101Component::set_packet_mode(bool value) {
this->state_.PKT_FORMAT =
static_cast<uint8_t>(value ? PacketFormat::PACKET_FORMAT_FIFO : PacketFormat::PACKET_FORMAT_ASYNC_SERIAL);
if (value) {
// Configure GDO0 for FIFO status (asserts on RX FIFO threshold or end of packet)
this->state_.GDO0_CFG = 0x01;
// Set max RX FIFO threshold to ensure we only trigger on end-of-packet
this->state_.FIFO_THR = 15;
// Don't append status bytes to FIFO - we read from registers instead
this->state_.APPEND_STATUS = 0;
} else {
// Configure GDO0 for serial data (async serial mode)
void CC1101Component::configure_packet_mode_() {
#ifdef USE_CC1101_LONG_PACKET
// Ends a long reception in progress, also when switching to a short packet mode
this->high_freq_.stop();
this->packet_.clear();
#endif
if (!this->packet_mode_) {
this->state_.PKT_FORMAT = static_cast<uint8_t>(PacketFormat::PACKET_FORMAT_ASYNC_SERIAL);
this->state_.GDO0_CFG = 0x0D;
} else {
this->state_.PKT_FORMAT = static_cast<uint8_t>(PacketFormat::PACKET_FORMAT_FIFO);
this->state_.APPEND_STATUS = 0;
#ifdef USE_CC1101_LONG_PACKET
if (this->is_long_packet_mode_()) {
// Threshold-only signal, reasserts after each partial FIFO drain
this->state_.GDO0_CFG = 0x00;
this->state_.FIFO_THR = 7;
// Reserve once so receiving never allocates; a lambda may read one FIFO drain past MAX_PACKET_LENGTH
this->packet_.reserve(this->packet_length_func_ != nullptr ? MAX_PACKET_LENGTH + RX_FIFO_SIZE
: this->packet_length_);
this->reset_long_packet_rx_();
} else
#endif
{
this->state_.GDO0_CFG = 0x01;
this->state_.FIFO_THR = 15;
if (this->packet_length_ == 0) {
this->state_.LENGTH_CONFIG = static_cast<uint8_t>(LengthConfig::LENGTH_CONFIG_VARIABLE);
} else {
this->state_.LENGTH_CONFIG = static_cast<uint8_t>(LengthConfig::LENGTH_CONFIG_FIXED);
this->state_.PKTLEN = static_cast<uint8_t>(this->packet_length_);
}
}
}
if (this->initialized_) {
this->write_(Register::PKTCTRL0);
this->write_(Register::PKTCTRL1);
this->write_(Register::PKTLEN);
this->write_(Register::IOCFG0);
this->write_(Register::FIFOTHR);
}
}
void CC1101Component::set_packet_mode(bool value) {
this->packet_mode_ = value;
#ifdef USE_CC1101_LONG_PACKET
// Long packet RX must drop the FIFO along with a partial packet, so go through IDLE and restart RX
if (this->initialized_ && this->is_long_packet_mode_()) {
this->reconfigure_packet_length_();
} else
#endif
{
this->configure_packet_mode_();
}
if (this->initialized_) {
if (this->gdo0_pin_ != nullptr) {
@@ -856,24 +1159,41 @@ void CC1101Component::set_packet_mode(bool value) {
this->gdo0_pin_->detach_interrupt();
}
}
this->write_(Register::PKTCTRL0);
this->write_(Register::PKTCTRL1);
this->write_(Register::IOCFG0);
this->write_(Register::FIFOTHR);
}
}
void CC1101Component::set_packet_length(uint8_t value) {
if (value == 0) {
this->state_.LENGTH_CONFIG = static_cast<uint8_t>(LengthConfig::LENGTH_CONFIG_VARIABLE);
} else {
this->state_.LENGTH_CONFIG = static_cast<uint8_t>(LengthConfig::LENGTH_CONFIG_FIXED);
this->state_.PKTLEN = value;
void CC1101Component::set_packet_length(uint16_t value) {
#ifndef USE_CC1101_LONG_PACKET
if (value > RX_FIFO_SIZE) {
ESP_LOGW(TAG, "Packet length %u needs a configured packet_length above %u", value,
static_cast<unsigned>(RX_FIFO_SIZE));
return;
}
if (this->initialized_) {
this->write_(Register::PKTCTRL0);
this->write_(Register::PKTLEN);
#else
this->packet_length_func_ = nullptr;
#endif
this->packet_length_ = value;
this->reconfigure_packet_length_();
}
#ifdef USE_CC1101_LONG_PACKET
void CC1101Component::set_packet_length_lambda(packet_length_func_t func) {
this->packet_length_func_ = func;
this->reconfigure_packet_length_();
}
#endif
void CC1101Component::reconfigure_packet_length_() {
// While asleep the writes wait for wake_up() and the radio stays asleep, like set_frequency()
if (!this->initialized_ || this->sleeping_) {
this->configure_packet_mode_();
return;
}
// Like the other runtime setters: go through IDLE, and flush bytes of a reception in progress
this->enter_idle_();
this->strobe_(Command::FRX);
this->configure_packet_mode_();
this->enter_rx_();
}
void CC1101Component::set_crc_enable(bool value) {
+33 -2
View File
@@ -2,6 +2,7 @@
#include "esphome/core/component.h"
#include "esphome/core/hal.h"
#include "esphome/core/helpers.h"
#include "esphome/components/spi/spi.h"
#include "esphome/core/automation.h"
#include "cc1101defs.h"
@@ -10,6 +11,7 @@
namespace esphome::cc1101 {
enum class CC1101Error { NONE = 0, TIMEOUT, PARAMS, CRC_ERROR, FIFO_OVERFLOW, PLL_LOCK };
using packet_length_func_t = int32_t (*)(const std::vector<uint8_t> &);
class CC1101Listener {
public:
@@ -86,7 +88,10 @@ class CC1101Component final : public Component,
// Packet mode settings
void set_packet_mode(bool value);
void set_packet_length(uint8_t value);
void set_packet_length(uint16_t value);
#ifdef USE_CC1101_LONG_PACKET
void set_packet_length_lambda(packet_length_func_t func);
#endif
void set_crc_enable(bool value);
void set_whitening(bool value);
@@ -101,6 +106,7 @@ class CC1101Component final : public Component,
protected:
uint16_t chip_id_{0};
bool initialized_{false};
bool packet_mode_{false};
bool sleeping_{false};
bool registers_dirty_{false};
@@ -112,13 +118,38 @@ class CC1101Component final : public Component,
// GDO pin for packet reception
InternalGPIOPin *gdo0_pin_{nullptr};
static void gpio_intr(CC1101Component *arg);
// Inline like App.get_loop_component_start_time(): on RP2040 an out-of-line IRAM_ATTR function in the same
// file as that inline one fails with a section type conflict
static void IRAM_ATTR gpio_intr(CC1101Component *arg) { arg->enable_loop_soon_any_context(); }
// Packet handling
void call_listeners_(const std::vector<uint8_t> &packet, float freq_offset, float rssi, uint8_t lqi);
void configure_packet_mode_();
void reconfigure_packet_length_();
float symbol_rate_() const;
#ifdef USE_CC1101_LONG_PACKET
bool is_long_packet_mode_() const;
void arm_length_(uint16_t length);
void reset_long_packet_rx_();
void restart_long_packet_rx_();
void loop_long_packet_();
void handle_long_packet_(uint8_t rx_bytes);
void finish_long_packet_();
void maybe_switch_long_packet_to_fixed_();
#endif
Trigger<std::vector<uint8_t>, float, float, uint8_t> packet_trigger_;
std::vector<uint8_t> packet_;
std::vector<CC1101Listener *> listeners_;
#ifdef USE_CC1101_LONG_PACKET
packet_length_func_t packet_length_func_{nullptr};
uint32_t packet_last_byte_ms_{0};
#endif
uint16_t packet_length_{0};
#ifdef USE_CC1101_LONG_PACKET
uint16_t packet_expected_length_{0};
int8_t long_packet_rssi_{0}; // raw RSSI register
HighFrequencyLoopRequester high_freq_;
#endif
// Low-level Helpers
uint8_t strobe_(Command cmd);
+5
View File
@@ -15,6 +15,11 @@ static constexpr uint8_t PLL_LOCK_RETRIES = 3;
static constexpr uint8_t STATUS_CRC_OK_MASK = 0x80;
static constexpr uint8_t STATUS_LQI_MASK = 0x7F;
static constexpr uint16_t MAX_PACKET_LENGTH = 1024;
static constexpr uint16_t PACKET_IDLE_TIMEOUT_BYTES = 64;
static constexpr uint32_t PACKET_IDLE_TIMEOUT_MIN_MS = 5;
static constexpr uint8_t RXBYTES_STABLE_RETRIES = 4;
static constexpr uint8_t BUS_BURST = 0x40;
static constexpr uint8_t BUS_READ = 0x80;
static constexpr uint8_t BUS_WRITE = 0x00;
@@ -1,36 +1,15 @@
from esphome import pins
import esphome.codegen as cg
from esphome.components import i2c, touchscreen
import esphome.config_validation as cv
from esphome.const import CONF_ID, CONF_INTERRUPT_PIN, CONF_RESET_PIN
from esphome.components import cstxxx
from esphome.types import ConfigType
from .. import cst226_ns
CST226Touchscreen = cst226_ns.class_(
"CST226Touchscreen",
touchscreen.Touchscreen,
i2c.I2CDevice,
)
AUTO_LOAD = ["cstxxx"]
CST226Touchscreen = cst226_ns.class_("CST226Touchscreen", cstxxx.CSTTouchscreen)
CST226ButtonListener = cst226_ns.class_("CST226ButtonListener")
CONFIG_SCHEMA = (
touchscreen.touchscreen_schema("100ms")
.extend(
{
cv.GenerateID(): cv.declare_id(CST226Touchscreen),
cv.Optional(CONF_INTERRUPT_PIN): pins.internal_gpio_input_pin_schema,
cv.Optional(CONF_RESET_PIN): pins.gpio_output_pin_schema,
}
)
.extend(i2c.i2c_device_schema(0x5A))
)
CONFIG_SCHEMA = cstxxx.cst_touchscreen_schema(CST226Touchscreen, 0x5A)
async def to_code(config: ConfigType) -> None:
var = cg.new_Pvariable(config[CONF_ID])
await touchscreen.register_touchscreen(var, config)
await i2c.register_i2c_device(var, config)
await cg.add_pin(var.set_interrupt_pin, config.get(CONF_INTERRUPT_PIN))
await cg.add_pin(var.set_reset_pin, config.get(CONF_RESET_PIN))
await cstxxx.new_cst_touchscreen(config)
@@ -14,6 +14,10 @@ void CST226Touchscreen::setup() {
}
void CST226Touchscreen::update_touches() {
if (!this->setup_complete_) {
this->skip_update_ = true;
return;
}
uint8_t data[28];
if (!this->status_comm_result(this->read_bytes(CST226_REG_STATUS, data, sizeof data))) {
this->skip_update_ = true;
@@ -24,56 +28,39 @@ void CST226Touchscreen::update_touches() {
return;
}
this->update_button_state_(false);
if (data[6] != 0xAB || data[0] == 0xAB || data[5] == 0x80) {
if (data[cstxxx::SYNC_IDX] != cstxxx::SYNC_BYTE || data[0] == cstxxx::SYNC_BYTE ||
data[cstxxx::FINGER_COUNT_IDX] == 0x80) {
this->skip_update_ = true;
return;
}
uint8_t num_of_touches = data[5] & 0x7F;
uint8_t num_of_touches = data[cstxxx::FINGER_COUNT_IDX] & 0x7F;
if (num_of_touches == 0 || num_of_touches > 5) {
this->write_byte(0, 0xAB);
this->write_byte(CST226_REG_STATUS, cstxxx::SYNC_BYTE);
return;
}
size_t index = 0;
for (uint8_t i = 0; i != num_of_touches; i++) {
uint8_t id = data[index] >> 4;
int16_t x = (data[index + 1] << 4) | ((data[index + 3] >> 4) & 0x0F);
int16_t y = (data[index + 2] << 4) | (data[index + 3] & 0x0F);
int16_t z = data[index + 4];
this->add_raw_touch_position_(id, x, y, z);
ESP_LOGV(TAG, "Read touch %d: %d/%d", id, x, y);
index += 5;
if (i == 0)
index += 2;
auto touch = cstxxx::decode_touch(data, i);
this->add_raw_touch_position_(touch.id, touch.x, touch.y, touch.z);
ESP_LOGV(TAG, "Read touch %d: %d/%d", touch.id, touch.x, touch.y);
}
}
bool CST226Touchscreen::read16_(uint16_t addr, uint8_t *data, size_t len) {
if (this->read_register16(addr, data, len) != i2c::ERROR_OK) {
ESP_LOGE(TAG, "Read data from 0x%04X failed", addr);
this->mark_failed();
return false;
}
return true;
}
void CST226Touchscreen::continue_setup_() {
uint8_t buffer[8];
if (this->interrupt_pin_ != nullptr) {
this->interrupt_pin_->setup();
this->attach_interrupt_(this->interrupt_pin_, gpio::INTERRUPT_FALLING_EDGE);
}
if (!this->setup_check(this->write_byte_reg16(0xD1, 0xD1), LOG_STR("Write byte to 0xD1 failed")))
this->setup_interrupt_pin_(gpio::INTERRUPT_FALLING_EDGE);
if (!this->enter_command_mode_())
return;
delay(10);
if (this->read16_(0xD204, buffer, 4)) {
uint16_t chip_id = buffer[2] + (buffer[3] << 8);
uint16_t project_id = buffer[0] + (buffer[1] << 8);
ESP_LOGCONFIG(TAG, "Chip ID %X, project ID %x", chip_id, project_id);
}
if (!this->check_firmware_())
return;
if (!this->setup_check(this->read_words_(cstxxx::REG_CHIP_INFO, this->project_id_, this->chip_id_),
LOG_STR("Failed to read chip ID")))
return;
if (this->x_raw_max_ == 0 || this->y_raw_max_ == 0) {
if (this->read16_(0xD1F8, buffer, 4)) {
this->x_raw_max_ = buffer[0] + (buffer[1] << 8);
this->y_raw_max_ = buffer[2] + (buffer[3] << 8);
uint16_t x_max, y_max;
if (this->read_words_(cstxxx::REG_RESOLUTION, x_max, y_max)) {
this->x_raw_max_ = x_max;
this->y_raw_max_ = y_max;
if (this->swap_x_y_)
std::swap(this->x_raw_max_, this->y_raw_max_);
} else {
@@ -81,8 +68,10 @@ void CST226Touchscreen::continue_setup_() {
this->y_raw_max_ = this->display_->get_native_height();
}
}
this->status_comm_check(this->write_register16(cstxxx::REG_NORMAL_MODE, nullptr, 0) == i2c::ERROR_OK);
this->setup_complete_ = true;
}
void CST226Touchscreen::update_button_state_(bool state) {
if (this->button_touched_ == state)
return;
@@ -96,6 +85,10 @@ void CST226Touchscreen::dump_config() {
LOG_I2C_DEVICE(this);
LOG_PIN(" Interrupt Pin: ", this->interrupt_pin_);
LOG_PIN(" Reset Pin: ", this->reset_pin_);
ESP_LOGCONFIG(TAG,
" Chip ID: 0x%04X, Project ID: 0x%04X\n"
" X/Y raw max: %d/%d",
this->chip_id_, this->project_id_, this->x_raw_max_, this->y_raw_max_);
}
} // namespace esphome::cst226
@@ -1,11 +1,12 @@
#pragma once
#include "esphome/components/i2c/i2c.h"
#include "esphome/components/touchscreen/touchscreen.h"
#include "esphome/components/cstxxx/cstxxx.h"
#include "esphome/core/component.h"
#include "esphome/core/hal.h"
#include "esphome/core/log.h"
#include <vector>
namespace esphome::cst226 {
static const uint8_t CST226_REG_STATUS = 0x00;
@@ -15,28 +16,23 @@ class CST226ButtonListener {
virtual void update_button(bool state) = 0;
};
class CST226Touchscreen final : public touchscreen::Touchscreen, public i2c::I2CDevice {
class CST226Touchscreen final : public cstxxx::CSTTouchscreen {
public:
void setup() override;
void update_touches() override;
void dump_config() override;
void set_interrupt_pin(InternalGPIOPin *pin) { this->interrupt_pin_ = pin; }
void set_reset_pin(GPIOPin *pin) { this->reset_pin_ = pin; }
bool can_proceed() override { return this->setup_complete_ || this->is_failed(); }
void register_button_listener(CST226ButtonListener *listener) { this->button_listeners_.push_back(listener); }
protected:
bool read16_(uint16_t addr, uint8_t *data, size_t len);
void continue_setup_();
void update_button_state_(bool state);
InternalGPIOPin *interrupt_pin_{};
GPIOPin *reset_pin_{};
uint8_t chip_id_{};
bool setup_complete_{};
std::vector<CST226ButtonListener *> button_listeners_;
bool button_touched_{};
uint16_t chip_id_{};
uint16_t project_id_{};
std::vector<CST226ButtonListener *> button_listeners_;
};
} // namespace esphome::cst226
+7
View File
@@ -0,0 +1,7 @@
import esphome.codegen as cg
CODEOWNERS = ["@tpruvot"]
DEPENDENCIES = ["i2c"]
DOMAIN = "cst3240"
cst3240_ns = cg.esphome_ns.namespace("cst3240")
@@ -0,0 +1,14 @@
from esphome.components import cstxxx
from esphome.types import ConfigType
from .. import cst3240_ns
AUTO_LOAD = ["cstxxx"]
CST3240Touchscreen = cst3240_ns.class_("CST3240Touchscreen", cstxxx.CSTTouchscreen)
CONFIG_SCHEMA = cstxxx.cst_touchscreen_schema(CST3240Touchscreen, 0x5A)
async def to_code(config: ConfigType) -> None:
await cstxxx.new_cst_touchscreen(config)
@@ -0,0 +1,112 @@
#include "cst3240_touchscreen.h"
#include "esphome/core/helpers.h"
#include "esphome/core/log.h"
#include <utility>
namespace esphome::cst3240 {
ESPHOME_LOG_TAG(TAG, "cst3240.touchscreen");
void CST3240Touchscreen::setup() {
if (this->reset_pin_ == nullptr) {
this->continue_setup_();
return;
}
pulse_reset(this->reset_pin_, {.high_before_ms = 5, .low_ms = 5});
this->set_timeout(30, [this] { this->continue_setup_(); });
}
void CST3240Touchscreen::continue_setup_() {
if (!this->enter_command_mode_())
return;
if (!this->setup_check(this->read_words_(cstxxx::REG_CHIP_INFO, this->project_id_, this->chip_id_),
LOG_STR("Failed to read chip ID")))
return;
// Only warn: the chip ID has not been confirmed on enough boards to fail setup on it
if (this->chip_id_ != CST3240_CHIP_ID) {
ESP_LOGW(TAG, "Unexpected chip ID 0x%04X", this->chip_id_);
}
this->read_words_(cstxxx::REG_FW_VERSION, this->fw_build_, this->fw_version_);
if (this->x_raw_max_ == this->x_raw_min_ || this->y_raw_max_ == this->y_raw_min_) {
uint16_t x_max, y_max;
if (this->read_words_(cstxxx::REG_RESOLUTION, x_max, y_max)) {
this->x_raw_max_ = x_max;
this->y_raw_max_ = y_max;
if (this->swap_x_y_)
std::swap(this->x_raw_max_, this->y_raw_max_);
} else {
this->x_raw_max_ = this->display_->get_native_width();
this->y_raw_max_ = this->display_->get_native_height();
}
}
this->status_comm_check(this->write_register16(cstxxx::REG_NORMAL_MODE, nullptr, 0) == i2c::ERROR_OK);
this->setup_interrupt_pin_(gpio::INTERRUPT_RISING_EDGE);
this->setup_complete_ = true;
}
void CST3240Touchscreen::update_touches() {
if (!this->setup_complete_) {
this->skip_update_ = true;
return;
}
uint8_t data[CST3240_REPORT_SIZE];
if (!this->status_comm_result(this->read_register16(cstxxx::REG_TOUCH_DATA, data, sizeof(data)) == i2c::ERROR_OK)) {
this->skip_update_ = true;
return;
}
const uint8_t sync = data[cstxxx::SYNC_IDX];
if (sync != cstxxx::SYNC_BYTE) {
this->skip_update_ = true;
if (sync != 0) {
ESP_LOGV(TAG, "Skipping report, sync byte 0x%02X", sync);
this->clear_report_();
}
return;
}
const uint8_t count = data[cstxxx::FINGER_COUNT_IDX] & 0x0F;
if (count <= CST3240_MAX_TOUCHES) {
for (uint8_t i = 0; i < count; i++) {
auto touch = cstxxx::decode_touch(data, i);
// A released finger is still listed once, with both press bits clear
if ((touch.event & TOUCH_EVENT_PRESSED) == 0)
continue;
this->add_raw_touch_position_(touch.id, touch.x, touch.y, touch.z);
}
}
if (this->interrupt_pin_ == nullptr) {
this->clear_report_();
} else if (!this->write_byte_reg16(cstxxx::REG_FINGER_COUNT, 0) ||
!this->write_byte_reg16(cstxxx::REG_TOUCH_DATA, cstxxx::SYNC_BYTE)) {
ESP_LOGW(TAG, "Failed to acknowledge the report");
}
}
void CST3240Touchscreen::clear_report_() {
// Built on the stack: write_register16() heap allocates a frame this long
uint8_t frame[2 + CST3240_REPORT_SIZE]{};
put_be16(frame, cstxxx::REG_TOUCH_DATA);
if (this->write(frame, sizeof(frame)) != i2c::ERROR_OK) {
ESP_LOGW(TAG, "Failed to clear the report");
}
}
void CST3240Touchscreen::dump_config() {
ESP_LOGCONFIG(TAG,
"CST3240 Touchscreen:\n"
" Chip ID: 0x%04X, Project ID: 0x%04X\n"
" FW version: %d.%d.%d\n"
" X/Y resolution: %d/%d",
this->chip_id_, this->project_id_, this->fw_version_ >> 8, this->fw_version_ & 0xFF, this->fw_build_,
this->x_raw_max_, this->y_raw_max_);
LOG_I2C_DEVICE(this);
LOG_PIN(" Interrupt Pin: ", this->interrupt_pin_);
LOG_PIN(" Reset Pin: ", this->reset_pin_);
}
} // namespace esphome::cst3240
@@ -0,0 +1,31 @@
#pragma once
#include "esphome/components/cstxxx/cstxxx.h"
#include "esphome/core/component.h"
#include "esphome/core/hal.h"
namespace esphome::cst3240 {
static constexpr uint16_t CST3240_CHIP_ID = 0x3240;
static constexpr uint8_t CST3240_MAX_TOUCHES = 5;
static constexpr uint8_t CST3240_REPORT_SIZE = cstxxx::report_size(CST3240_MAX_TOUCHES);
static constexpr uint8_t TOUCH_EVENT_PRESSED = 0x06;
class CST3240Touchscreen : public cstxxx::CSTTouchscreen {
public:
void setup() override;
void dump_config() override;
protected:
void update_touches() override;
void continue_setup_();
/// Zeroes the touch report block, which polling relies on to tell new reports from old.
void clear_report_();
uint16_t chip_id_{};
uint16_t project_id_{};
uint16_t fw_version_{};
uint16_t fw_build_{};
};
} // namespace esphome::cst3240
@@ -1,37 +1,15 @@
from esphome import pins
import esphome.codegen as cg
from esphome.components import i2c, touchscreen
import esphome.config_validation as cv
from esphome.const import CONF_ID, CONF_INTERRUPT_PIN, CONF_RESET_PIN
from esphome.components import cstxxx
from esphome.types import ConfigType
from .. import cst328_ns
CST328Touchscreen = cst328_ns.class_(
"CST328Touchscreen",
touchscreen.Touchscreen,
i2c.I2CDevice,
)
AUTO_LOAD = ["cstxxx"]
CST328Touchscreen = cst328_ns.class_("CST328Touchscreen", cstxxx.CSTTouchscreen)
CST328ButtonListener = cst328_ns.class_("CST328ButtonListener")
CONFIG_SCHEMA = (
touchscreen.touchscreen_schema("100ms")
.extend(
{
cv.GenerateID(): cv.declare_id(CST328Touchscreen),
cv.Optional(CONF_INTERRUPT_PIN): pins.internal_gpio_input_pin_schema,
cv.Optional(CONF_RESET_PIN): pins.gpio_output_pin_schema,
}
)
.extend(i2c.i2c_device_schema(0x1A))
)
CONFIG_SCHEMA = cstxxx.cst_touchscreen_schema(CST328Touchscreen, 0x1A)
async def to_code(config: ConfigType) -> None:
var = cg.new_Pvariable(config[CONF_ID])
await touchscreen.register_touchscreen(var, config)
await i2c.register_i2c_device(var, config)
await cg.add_pin(var.set_interrupt_pin, config.get(CONF_INTERRUPT_PIN))
await cg.add_pin(var.set_reset_pin, config.get(CONF_RESET_PIN))
await cstxxx.new_cst_touchscreen(config)
@@ -5,31 +5,8 @@ namespace esphome::cst328 {
ESPHOME_LOG_TAG(TAG, "cst328.touchscreen");
static const uint32_t CST328_BEFORE_RESET_TIMEOUT = 50; // 50 ms from datasheet
static const uint32_t CST328_TRANSITION_TIMEOUT = 300; // 200 ms from datasheet, but typically much less
static const uint16_t CST328_FW_CRC = 0xCACA; // Expected firmware CRC value
static const uint8_t CST328_SYNC_BYTE = 0xAB; // Sync byte used in communication
static const uint8_t ZERO_BYTE = 0;
#define I2C_WARN_ON_ERROR(x, log_tag, format, ...) \
do { \
i2c::ErrorCode err_rc_ = (x); \
if (err_rc_ != i2c::ERROR_OK) { \
ESP_LOGW(log_tag, "%s(%d): [error %d] " format, __FUNCTION__, __LINE__, err_rc_, ##__VA_ARGS__); \
this->status_set_warning(format); \
} \
} while (0)
#define I2C_FAIL_ON_ERROR(x, log_tag, format, ...) \
do { \
i2c::ErrorCode err_rc_ = (x); \
if (err_rc_ != i2c::ERROR_OK) { \
ESP_LOGE(log_tag, "%s(%d): [error %d] " format, __FUNCTION__, __LINE__, err_rc_, ##__VA_ARGS__); \
this->mark_failed(); \
return; \
} \
} while (0)
static constexpr uint32_t CST328_BEFORE_RESET_TIMEOUT = 50; // 50 ms from datasheet
static constexpr uint32_t CST328_TRANSITION_TIMEOUT = 300; // 200 ms from datasheet, but typically much less
void CST328Touchscreen::setup() {
ESP_LOGCONFIG(TAG, "Setting up CST328 Touchscreen...");
@@ -50,50 +27,36 @@ void CST328Touchscreen::reset_device_() {
void CST328Touchscreen::continue_setup_() {
ESP_LOGV(TAG, "Continuing CST328 setup...");
uint8_t data_byte{0};
uint8_t buf[24]{};
I2C_FAIL_ON_ERROR(this->write_register16(CST_WM_DEBUG_INFO, buf, 0), TAG, "Failed to enter debug/info mode");
I2C_FAIL_ON_ERROR(this->read_register16(CST_REG_FW_CRC_AND_BOOT_TIME, buf, 4), TAG,
"Failed to read FW CRC and boot time");
uint16_t fw_crc = buf[2] + (buf[3] << 8);
if (fw_crc != CST328_FW_CRC) {
ESP_LOGE(TAG, "Error: Firmware CRC mismatch, expected 0x%04X but got 0x%04X", CST328_FW_CRC, fw_crc);
this->mark_failed();
if (!this->enter_command_mode_() || !this->check_firmware_())
return;
if (!this->setup_check(this->read_words_(cstxxx::REG_CHIP_INFO, this->project_id_, this->chip_id_),
LOG_STR("Failed to read chip and project ID")))
return;
}
I2C_FAIL_ON_ERROR(this->read_register16(CST_REG_CHIP_TYPE_AND_PROJECT_ID, buf, 4), TAG,
"Failed to read chip and project ID");
this->chip_id_ = buf[2] + (buf[3] << 8);
this->project_id_ = buf[0] + (buf[1] << 8);
ESP_LOGD(TAG, "Chip ID %X, project ID %X", this->chip_id_, this->project_id_);
I2C_FAIL_ON_ERROR(this->read_register16(CST_REG_FW_REVISION, buf, 4), TAG, "Failed to read FW version");
this->fw_ver_major_ = buf[3];
this->fw_ver_minor_ = buf[2];
this->fw_build_ = buf[0] + (buf[1] << 8);
uint16_t fw_version;
if (!this->setup_check(this->read_words_(cstxxx::REG_FW_VERSION, this->fw_build_, fw_version),
LOG_STR("Failed to read FW version")))
return;
this->fw_ver_major_ = fw_version >> 8;
this->fw_ver_minor_ = fw_version & 0xFF;
ESP_LOGV(TAG, "FW version %d.%d.%d", this->fw_ver_major_, this->fw_ver_minor_, this->fw_build_);
if (i2c::ERROR_OK == this->read_register16(CST_REG_X_Y_RESOLUTION, buf, 4)) {
this->x_raw_max_ = buf[0] + (buf[1] << 8);
this->y_raw_max_ = buf[2] + (buf[3] << 8);
uint16_t x_max, y_max;
if (this->read_words_(cstxxx::REG_RESOLUTION, x_max, y_max)) {
this->x_raw_max_ = x_max;
this->y_raw_max_ = y_max;
} else {
this->x_raw_max_ = this->display_->get_native_width();
this->y_raw_max_ = this->display_->get_native_height();
}
I2C_WARN_ON_ERROR(this->write_register16(CST_WM_NORMAL, buf, 0), TAG, "Failed to enter normal mode");
I2C_WARN_ON_ERROR(this->read_register16(CST_REG_TOUCH_INFORMATION, &data_byte, 1), TAG, "Failed to read sync");
I2C_WARN_ON_ERROR(this->write_register16(CST_REG_TOUCH_INFORMATION, &CST328_SYNC_BYTE, 1), TAG,
"Failed to write sync");
uint8_t sync;
bool ok = this->write_register16(cstxxx::REG_NORMAL_MODE, nullptr, 0) == i2c::ERROR_OK;
ok &= this->read_byte_reg16(cstxxx::REG_TOUCH_DATA, &sync);
ok &= this->write_byte_reg16(cstxxx::REG_TOUCH_DATA, cstxxx::SYNC_BYTE);
this->status_comm_check(ok);
if (this->interrupt_pin_ != nullptr) {
this->interrupt_pin_->setup();
this->attach_interrupt_(this->interrupt_pin_, gpio::INTERRUPT_FALLING_EDGE);
}
this->setup_interrupt_pin_(gpio::INTERRUPT_FALLING_EDGE);
this->setup_complete_ = true;
ESP_LOGV(TAG, "CST328 setup complete");
@@ -142,21 +105,15 @@ void CST328Touchscreen::update_touches() {
} else {
this->update_button_state_(true);
uint8_t data_idx = 0;
for (uint8_t i = 0; i < touch_cnt; i++) {
uint8_t id = touch_data[data_idx] >> 4;
int16_t x = (touch_data[data_idx + 1] << 4) | ((touch_data[data_idx + 3] >> 4) & 0x0F);
int16_t y = (touch_data[data_idx + 2] << 4) | (touch_data[data_idx + 3] & 0x0F);
int16_t z = touch_data[data_idx + 4];
this->add_raw_touch_position_(id, x, y, z);
data_idx += (i == 0) ? 7 : 5;
auto touch = cstxxx::decode_touch(touch_data, i);
this->add_raw_touch_position_(touch.id, touch.x, touch.y, touch.z);
}
}
bool cleanup_error = false;
cleanup_error |= !this->write_byte_reg16(CST_REG_TOUCH_FINGER_NUMBER, ZERO_BYTE);
cleanup_error |= !this->write_byte_reg16(CST_REG_TOUCH_INFORMATION, CST328_SYNC_BYTE);
cleanup_error |= !this->write_byte_reg16(cstxxx::REG_FINGER_COUNT, 0);
cleanup_error |= !this->write_byte_reg16(cstxxx::REG_TOUCH_DATA, cstxxx::SYNC_BYTE);
if (cleanup_error) {
ESP_LOGW(TAG, "Failed to clean up touch registers");
@@ -1,61 +1,45 @@
#pragma once
#include "esphome/components/i2c/i2c.h"
#include "esphome/components/touchscreen/touchscreen.h"
#include "esphome/components/cstxxx/cstxxx.h"
#include "esphome/core/component.h"
#include "esphome/core/hal.h"
#include <vector>
namespace esphome::cst328 {
static const uint8_t CST328_TOUCH_MAX_POINTS = 5;
static const uint8_t CST328_TOUCH_DATA_SIZE = CST328_TOUCH_MAX_POINTS * 5 + 2;
static constexpr uint8_t CST328_TOUCH_MAX_POINTS = 5;
static constexpr uint8_t CST328_TOUCH_DATA_SIZE = cstxxx::report_size(CST328_TOUCH_MAX_POINTS);
static const uint16_t CST_REG_TOUCH_INFORMATION = 0xD000;
static const uint16_t CST_REG_TOUCH_FINGER_NUMBER = 0xD005;
static const uint16_t CST_REG_FINGER_COUNT_IDX = CST_REG_TOUCH_FINGER_NUMBER - CST_REG_TOUCH_INFORMATION;
static const uint16_t CST_REG_X_Y_RESOLUTION = 0xD1F8;
static const uint16_t CST_REG_FW_CRC_AND_BOOT_TIME = 0xD1FC;
static const uint16_t CST_REG_CHIP_TYPE_AND_PROJECT_ID = 0xD204;
static const uint16_t CST_REG_FW_REVISION = 0xD208;
static const uint16_t CST_WM_DEBUG_INFO = 0xD101;
static const uint16_t CST_WM_NORMAL = 0xD109;
static constexpr uint16_t CST_REG_TOUCH_INFORMATION = cstxxx::REG_TOUCH_DATA;
static constexpr uint16_t CST_REG_FINGER_COUNT_IDX = cstxxx::FINGER_COUNT_IDX;
static constexpr uint16_t CST_REG_X_Y_RESOLUTION = cstxxx::REG_RESOLUTION;
static constexpr uint16_t CST_REG_FW_CRC_AND_BOOT_TIME = cstxxx::REG_FW_CHECK_CODE;
class CST328ButtonListener {
public:
virtual void update_button(bool state) = 0;
};
class CST328Touchscreen : public touchscreen::Touchscreen, public i2c::I2CDevice {
class CST328Touchscreen : public cstxxx::CSTTouchscreen {
public:
void setup() override;
void register_button_listener(CST328ButtonListener *listener) { this->button_listeners_.push_back(listener); }
void dump_config() override;
void set_interrupt_pin(InternalGPIOPin *pin) { this->interrupt_pin_ = pin; }
void set_reset_pin(GPIOPin *pin) { this->reset_pin_ = pin; }
protected:
void update_touches() override;
void reset_device_();
void continue_setup_();
void update_button_state_(bool state);
InternalGPIOPin *interrupt_pin_{};
GPIOPin *reset_pin_{};
std::vector<CST328ButtonListener *> button_listeners_;
bool button_touched_{};
uint16_t chip_id_{};
uint16_t project_id_{};
uint8_t fw_ver_major_{};
uint8_t fw_ver_minor_{};
std::vector<CST328ButtonListener *> button_listeners_;
uint16_t chip_id_{};
uint16_t project_id_{};
uint16_t fw_build_{};
bool setup_complete_{};
};
} // namespace esphome::cst328
@@ -1,35 +1,14 @@
from esphome import pins
import esphome.codegen as cg
from esphome.components import i2c, touchscreen
import esphome.config_validation as cv
from esphome.const import CONF_ID, CONF_INTERRUPT_PIN, CONF_RESET_PIN
from esphome.components import cstxxx
from esphome.types import ConfigType
from .. import cst9220_ns
CST9220Touchscreen = cst9220_ns.class_(
"CST9220Touchscreen",
touchscreen.Touchscreen,
i2c.I2CDevice,
)
AUTO_LOAD = ["cstxxx"]
CONFIG_SCHEMA = (
touchscreen.touchscreen_schema("100ms")
.extend(
{
cv.GenerateID(): cv.declare_id(CST9220Touchscreen),
cv.Optional(CONF_INTERRUPT_PIN): pins.internal_gpio_input_pin_schema,
cv.Optional(CONF_RESET_PIN): pins.gpio_output_pin_schema,
}
)
.extend(i2c.i2c_device_schema(0x5A))
)
CST9220Touchscreen = cst9220_ns.class_("CST9220Touchscreen", cstxxx.CSTTouchscreen)
CONFIG_SCHEMA = cstxxx.cst_touchscreen_schema(CST9220Touchscreen, 0x5A)
async def to_code(config: ConfigType) -> None:
var = cg.new_Pvariable(config[CONF_ID])
await touchscreen.register_touchscreen(var, config)
await i2c.register_i2c_device(var, config)
await cg.add_pin(var.set_interrupt_pin, config.get(CONF_INTERRUPT_PIN))
await cg.add_pin(var.set_reset_pin, config.get(CONF_RESET_PIN))
await cstxxx.new_cst_touchscreen(config)
@@ -1,7 +1,4 @@
#include "cst9220_touchscreen.h"
#include "esphome/core/helpers.h"
#include <cinttypes>
namespace esphome::cst9220 {
@@ -16,55 +13,29 @@ void CST9220Touchscreen::setup() {
}
void CST9220Touchscreen::continue_setup_() {
uint8_t buffer[4];
this->setup_interrupt_pin_(gpio::INTERRUPT_FALLING_EDGE);
if (this->interrupt_pin_ != nullptr) {
this->interrupt_pin_->setup();
this->attach_interrupt_(this->interrupt_pin_, gpio::INTERRUPT_FALLING_EDGE);
}
// Enter command mode so the configuration registers can be read.
if (this->write_register16(REG_CMD_MODE, buffer, 0) != i2c::ERROR_OK) {
this->status_set_error(LOG_STR("Failed to enter command mode"));
this->mark_failed();
if (!this->enter_command_mode_())
return;
}
delay(10);
// The firmware check code confirms that valid firmware is loaded.
if (this->read_register16(REG_CHECKCODE, buffer, 4) != i2c::ERROR_OK) {
this->status_set_error(LOG_STR("Failed to read check code"));
this->mark_failed();
if (!this->check_firmware_())
return;
}
uint32_t checkcode = encode_uint32(buffer[3], buffer[2], buffer[1], buffer[0]);
if ((checkcode & 0xFFFF0000) != 0xCACA0000) {
ESP_LOGE(TAG, "Invalid firmware check code: 0x%08" PRIX32, checkcode);
this->status_set_error(LOG_STR("Invalid firmware check code"));
this->mark_failed();
return;
}
// Read the panel resolution unless the user supplied calibration values.
if (this->read_register16(REG_RESOLUTION, buffer, 4) == i2c::ERROR_OK) {
uint16_t x_max, y_max;
if (this->read_words_(cstxxx::REG_RESOLUTION, x_max, y_max)) {
if (this->x_raw_max_ == this->x_raw_min_)
this->x_raw_max_ = encode_value_le<uint16_t>(buffer);
this->x_raw_max_ = x_max;
if (this->y_raw_max_ == this->y_raw_min_)
this->y_raw_max_ = encode_value_le<uint16_t>(&buffer[2]);
this->y_raw_max_ = y_max;
}
// Read the chip type and project id and validate the controller.
if (this->read_register16(REG_CHIP_INFO, buffer, 4) != i2c::ERROR_OK) {
this->status_set_error(LOG_STR("Failed to read chip ID"));
this->mark_failed();
if (!this->setup_check(this->read_words_(cstxxx::REG_CHIP_INFO, this->project_id_, this->chip_id_),
LOG_STR("Failed to read chip ID")))
return;
}
this->chip_id_ = encode_value_le<uint16_t>(&buffer[2]);
this->project_id_ = encode_value_le<uint16_t>(buffer);
if (this->chip_id_ != CST9220_CHIP_ID && this->chip_id_ != CST9217_CHIP_ID) {
ESP_LOGE(TAG, "Unknown chip ID: 0x%04X", this->chip_id_);
this->status_set_error(LOG_STR("Unknown chip ID"));
this->mark_failed();
this->mark_failed(LOG_STR("Unknown chip ID"));
return;
}
@@ -84,7 +55,7 @@ void CST9220Touchscreen::update_touches() {
// Only an actual I2C failure should skip the update; a successful read with no
// touches is a real "all fingers lifted" state that must flow through so the
// base class can generate the release event.
if (!this->status_comm_result(this->read_register16(REG_TOUCH_DATA, data, sizeof(data)) == i2c::ERROR_OK)) {
if (!this->status_comm_result(this->read_register16(cstxxx::REG_TOUCH_DATA, data, sizeof(data)) == i2c::ERROR_OK)) {
this->skip_update_ = true;
return;
}
@@ -92,30 +63,23 @@ void CST9220Touchscreen::update_touches() {
// A valid report carries the marker at offset 6. Writing an acknowledge back makes the controller return
// invalid frames during a long press, so none is written. An invalid frame keeps the previous points; while
// a finger is down, read again shortly so a lost release frame does not leave the touch held.
if (data[6] != TOUCH_ACK) {
if (data[cstxxx::SYNC_IDX] != cstxxx::SYNC_BYTE) {
this->skip_update_ = true;
if (!this->touches_.empty())
this->set_timeout(REREAD_TIMEOUT_ID, REREAD_DELAY_MS, [this] { this->store_.touched = true; });
return;
}
uint8_t num_touches = data[5] & 0x7F;
uint8_t num_touches = data[cstxxx::FINGER_COUNT_IDX] & 0x7F;
if (num_touches > CST9220_MAX_TOUCHES)
num_touches = CST9220_MAX_TOUCHES;
for (uint8_t i = 0; i < num_touches; i++) {
// The first point starts at offset 0; subsequent points are offset by the
// two status bytes that follow it.
const uint8_t *p = data + i * 5 + (i == 0 ? 0 : 2);
uint8_t id = p[0] >> 4;
uint8_t event = p[0] & 0x0F;
if (event != TOUCH_EVENT_DOWN)
auto touch = cstxxx::decode_touch(data, i);
if (touch.event != TOUCH_EVENT_DOWN)
continue;
// p[3] is shared: high nibble holds the X LSBs, low nibble the Y LSBs.
uint16_t x = (p[1] << 4) | (p[3] >> 4);
uint16_t y = (p[2] << 4) | (p[3] & 0x0F);
ESP_LOGV(TAG, "Read touch %d: %d/%d", id, x, y);
this->add_raw_touch_position_(id, x, y);
ESP_LOGV(TAG, "Read touch %d: %d/%d", touch.id, touch.x, touch.y);
this->add_raw_touch_position_(touch.id, touch.x, touch.y);
}
}
@@ -1,7 +1,6 @@
#pragma once
#include "esphome/components/i2c/i2c.h"
#include "esphome/components/touchscreen/touchscreen.h"
#include "esphome/components/cstxxx/cstxxx.h"
#include "esphome/core/component.h"
#include "esphome/core/hal.h"
#include "esphome/core/log.h"
@@ -10,41 +9,26 @@ namespace esphome::cst9220 {
ESPHOME_LOG_TAG(TAG, "cst9220.touchscreen");
// The CST92xx family uses 16-bit (big-endian) register addresses.
static const uint16_t REG_TOUCH_DATA = 0xD000; // touch report
static const uint16_t REG_CMD_MODE = 0xD101; // enter command mode
static const uint16_t REG_CHECKCODE = 0xD1FC; // firmware check code
static const uint16_t REG_RESOLUTION = 0xD1F8; // panel resolution
static const uint16_t REG_CHIP_INFO = 0xD204; // chip type + project id
static constexpr uint8_t TOUCH_EVENT_DOWN = 0x06;
static const uint8_t TOUCH_ACK = 0xAB;
static const uint8_t TOUCH_EVENT_DOWN = 0x06;
static constexpr uint16_t CST9220_CHIP_ID = 0x9220;
static constexpr uint16_t CST9217_CHIP_ID = 0x9217;
static const uint16_t CST9220_CHIP_ID = 0x9220;
static const uint16_t CST9217_CHIP_ID = 0x9217;
// Maximum simultaneous touch points reported by the family.
static const uint8_t CST9220_MAX_TOUCHES = 5;
static constexpr uint8_t CST9220_MAX_TOUCHES = 5;
// Report layout: 5 bytes per touch point plus 5 bytes of status/ack overhead.
static const size_t CST9220_DATA_LENGTH = CST9220_MAX_TOUCHES * 5 + 5;
static constexpr size_t CST9220_DATA_LENGTH = CST9220_MAX_TOUCHES * 5 + 5;
class CST9220Touchscreen : public touchscreen::Touchscreen, public i2c::I2CDevice {
class CST9220Touchscreen : public cstxxx::CSTTouchscreen {
public:
void setup() override;
void dump_config() override;
void set_interrupt_pin(InternalGPIOPin *pin) { this->interrupt_pin_ = pin; }
void set_reset_pin(GPIOPin *pin) { this->reset_pin_ = pin; }
protected:
void update_touches() override;
void continue_setup_();
InternalGPIOPin *interrupt_pin_{};
GPIOPin *reset_pin_{};
uint16_t chip_id_{};
uint16_t project_id_{};
bool setup_complete_{};
};
} // namespace esphome::cst9220
+42
View File
@@ -0,0 +1,42 @@
from esphome import pins
import esphome.codegen as cg
from esphome.components import i2c, touchscreen
import esphome.config_validation as cv
from esphome.const import CONF_ID, CONF_INTERRUPT_PIN, CONF_RESET_PIN
from esphome.cpp_generator import MockObj, MockObjClass
from esphome.types import ConfigType
CODEOWNERS = ["@clydebarrow", "@latonita"]
DEPENDENCIES = ["i2c", "touchscreen"]
DOMAIN = "cstxxx"
cstxxx_ns = cg.esphome_ns.namespace("cstxxx")
CSTTouchscreen = cstxxx_ns.class_(
"CSTTouchscreen", touchscreen.Touchscreen, i2c.I2CDevice
)
def cst_touchscreen_schema(touchscreen_class: MockObjClass, address: int) -> cv.Schema:
"""Return the touchscreen schema of a Hynitron driver."""
return (
touchscreen.touchscreen_schema("100ms")
.extend(
{
cv.GenerateID(): cv.declare_id(touchscreen_class),
cv.Optional(CONF_INTERRUPT_PIN): pins.internal_gpio_input_pin_schema,
cv.Optional(CONF_RESET_PIN): pins.gpio_output_pin_schema,
}
)
.extend(i2c.i2c_device_schema(address))
)
async def new_cst_touchscreen(config: ConfigType) -> MockObj:
"""Create a Hynitron touchscreen and set its pins."""
var = cg.new_Pvariable(config[CONF_ID])
await touchscreen.register_touchscreen(var, config)
await i2c.register_i2c_device(var, config)
await cg.add_pin(var.set_interrupt_pin, config.get(CONF_INTERRUPT_PIN))
await cg.add_pin(var.set_reset_pin, config.get(CONF_RESET_PIN))
return var
+13
View File
@@ -0,0 +1,13 @@
#include "cstxxx.h"
#include "esphome/core/log.h"
namespace esphome::cstxxx {
ESPHOME_LOG_TAG(TAG, "cstxxx");
void CSTTouchscreen::fail_check_code_(uint16_t check_code) {
ESP_LOGE(TAG, "Firmware check code 0x%04X", check_code);
this->mark_failed(LOG_STR("Invalid firmware check code"));
}
} // namespace esphome::cstxxx
+92
View File
@@ -0,0 +1,92 @@
#pragma once
#include <cstddef>
#include <cstdint>
#include "esphome/components/i2c/i2c.h"
#include "esphome/components/touchscreen/touchscreen.h"
#include "esphome/core/gpio.h"
#include "esphome/core/hal.h"
#include "esphome/core/helpers.h"
#include "esphome/core/log.h"
namespace esphome::cstxxx {
static constexpr uint16_t REG_TOUCH_DATA = 0xD000;
static constexpr uint16_t REG_FINGER_COUNT = 0xD005;
static constexpr uint16_t REG_COMMAND_MODE = 0xD101;
static constexpr uint16_t REG_NORMAL_MODE = 0xD109;
static constexpr uint16_t REG_RESOLUTION = 0xD1F8;
static constexpr uint16_t REG_FW_CHECK_CODE = 0xD1FC;
static constexpr uint16_t REG_CHIP_INFO = 0xD204;
static constexpr uint16_t REG_FW_VERSION = 0xD208;
static constexpr uint8_t FINGER_COUNT_IDX = REG_FINGER_COUNT - REG_TOUCH_DATA;
static constexpr uint8_t SYNC_IDX = FINGER_COUNT_IDX + 1;
static constexpr uint8_t SYNC_BYTE = 0xAB;
static constexpr uint16_t FW_CHECK_CODE = 0xCACA;
/// Bytes of a touch report holding `points` records; two status bytes follow the first record.
constexpr size_t report_size(uint8_t points) { return points * 5 + 2; }
struct TouchRecord {
uint8_t id;
uint8_t event;
uint16_t x;
uint16_t y;
uint8_t z;
};
/// Decodes record `index` of a touch report.
inline TouchRecord decode_touch(const uint8_t *report, uint8_t index) {
const uint8_t *r = report + index * 5 + (index == 0 ? 0 : 2);
return {static_cast<uint8_t>(r[0] >> 4), static_cast<uint8_t>(r[0] & 0x0F),
static_cast<uint16_t>(r[1] << 4 | r[3] >> 4), static_cast<uint16_t>(r[2] << 4 | (r[3] & 0x0F)), r[4]};
}
class CSTTouchscreen : public touchscreen::Touchscreen, public i2c::I2CDevice {
public:
void set_interrupt_pin(InternalGPIOPin *pin) { this->interrupt_pin_ = pin; }
void set_reset_pin(GPIOPin *pin) { this->reset_pin_ = pin; }
protected:
/// Sets up the interrupt pin, if configured, to fire on `type`.
void setup_interrupt_pin_(gpio::InterruptType type) {
if (this->interrupt_pin_ == nullptr)
return;
this->interrupt_pin_->setup();
this->attach_interrupt_(this->interrupt_pin_, type);
}
/// Reads two little-endian words starting at `reg`.
bool ESPHOME_ALWAYS_INLINE read_words_(uint16_t reg, uint16_t &low, uint16_t &high) {
uint16_t words[2];
if (this->read_register16(reg, reinterpret_cast<uint8_t *>(words), sizeof(words)) != i2c::ERROR_OK)
return false;
low = convert_little_endian(words[0]);
high = convert_little_endian(words[1]);
return true;
}
/// Enters command mode, marking the component failed if the write fails.
bool enter_command_mode_() {
return this->setup_check(this->write_register16(REG_COMMAND_MODE, nullptr, 0) == i2c::ERROR_OK,
LOG_STR("Failed to enter command mode"));
}
/// Checks the firmware check code, marking the component failed if it is unreadable or wrong.
bool check_firmware_() {
uint16_t boot_time, check_code;
if (!this->setup_check(this->read_words_(REG_FW_CHECK_CODE, boot_time, check_code)))
return false;
if (check_code == FW_CHECK_CODE)
return true;
this->fail_check_code_(check_code);
return false;
}
/// Logs a wrong firmware check code and marks the component failed.
void fail_check_code_(uint16_t check_code);
InternalGPIOPin *interrupt_pin_{};
GPIOPin *reset_pin_{};
bool setup_complete_{};
};
} // namespace esphome::cstxxx
+3 -1
View File
@@ -1,6 +1,5 @@
import esphome.codegen as cg
from esphome.components import logger
from esphome.components.nrf52.framework import include_west_project
from esphome.components.zephyr import zephyr_add_prj_conf
from esphome.config_helpers import filter_source_files_from_platform
import esphome.config_validation as cv
@@ -53,6 +52,9 @@ CONFIG_SCHEMA = cv.All(
async def to_code(config: ConfigType) -> None:
if CORE.using_zephyr:
# Imported here so configs for other platforms do not load the nrf52 package
from esphome.components.nrf52.framework import include_west_project
zephyr_add_prj_conf("HWINFO", True)
# gdb thread support
zephyr_add_prj_conf("DEBUG_THREAD_INFO", True)
+3 -1
View File
@@ -56,7 +56,6 @@ from esphome.const import (
from esphome.core import CORE, EsphomeError, HexInt
from esphome.core.config import BOARD_MAX_LENGTH
from esphome.coroutine import CoroPriority, coroutine_with_priority
from esphome.espidf import sbom
from esphome.espidf.component import IDFComponent, generate_idf_components
import esphome.final_validate as fv
from esphome.helpers import copy_file_if_changed, rmtree, write_file_if_changed
@@ -4033,6 +4032,9 @@ def copy_files():
CORE.relative_build_path("version.txt"),
__version__,
)
# Imported here: sbom loads git and urllib helpers that config validation does not need
from esphome.espidf import sbom
sbom.write_manifests()
# Remote extra build files are fetched into the shared download cache in
@@ -7,6 +7,8 @@
#include "esphome/core/automation.h"
#include "esphome/components/network/ip_address.h"
#include <atomic>
#ifdef USE_ETHERNET
#ifdef USE_ESP32
@@ -366,6 +368,9 @@ class EthernetComponent final : public Component {
#ifdef USE_ETHERNET_IP_STATE_LISTENERS
StaticVector<EthernetIPStateListener *, ESPHOME_ETHERNET_IP_STATE_LISTENERS> ip_state_listeners_;
#ifdef USE_ESP32
std::atomic<uint8_t> ip_state_pending_{0};
#endif
#endif
#ifdef USE_ETHERNET_CONNECT_TRIGGER
@@ -129,6 +129,14 @@ void EthernetComponent::log_error_and_mark_failed_(esp_err_t err, const char *me
void EthernetComponent::loop() {
const uint32_t now = App.get_loop_component_start_time();
#ifdef USE_ETHERNET_IP_STATE_LISTENERS
// The IP events arrive on the event task; the listeners publish states, so notify them here
if (this->ip_state_pending_.load(std::memory_order_acquire) != 0) {
this->ip_state_pending_.store(0, std::memory_order_relaxed);
this->notify_ip_state_listeners_();
}
#endif
switch (this->state_) {
case EthernetComponentState::STOPPED:
if (this->started_) {
@@ -770,7 +778,8 @@ void EthernetComponent::eth_event_handler(void *arg, esp_event_base_t event_base
event_name = "ETH connected";
// For static IP configurations, GOT_IP event may not fire, so notify IP listeners here
#if defined(USE_ETHERNET_IP_STATE_LISTENERS) && defined(USE_ETHERNET_MANUAL_IP)
global_eth_component->notify_ip_state_listeners_();
global_eth_component->ip_state_pending_.store(1, std::memory_order_release);
global_eth_component->enable_loop_soon_any_context();
#endif
#if USE_NETWORK_IPV6
// Start SLAAC on link-up, not after the DHCPv4 lease. This also restores the
@@ -798,6 +807,9 @@ void EthernetComponent::got_ip_event_handler(void *arg, esp_event_base_t event_b
const esp_netif_ip_info_t *ip_info = &event->ip_info;
ESP_LOGV(TAG, "[Ethernet event] ETH Got IP " IPSTR, IP2STR(&ip_info->ip));
global_eth_component->got_ipv4_address_ = true;
#ifdef USE_ETHERNET_IP_STATE_LISTENERS
global_eth_component->ip_state_pending_.store(1, std::memory_order_release);
#endif
#if USE_NETWORK_IPV6 && (USE_NETWORK_MIN_IPV6_ADDR_COUNT > 0)
global_eth_component->connected_ = global_eth_component->ipv6_count_ >= USE_NETWORK_MIN_IPV6_ADDR_COUNT;
global_eth_component->enable_loop_soon_any_context(); // Enable loop when connection state changes
@@ -805,9 +817,6 @@ void EthernetComponent::got_ip_event_handler(void *arg, esp_event_base_t event_b
global_eth_component->connected_ = true;
global_eth_component->enable_loop_soon_any_context(); // Enable loop when connection state changes
#endif /* USE_NETWORK_IPV6 */
#ifdef USE_ETHERNET_IP_STATE_LISTENERS
global_eth_component->notify_ip_state_listeners_();
#endif
}
#if USE_NETWORK_IPV6
@@ -819,6 +828,9 @@ void EthernetComponent::got_ip6_event_handler(void *arg, esp_event_base_t event_
// after a link flap fires another event for the same address.
struct esp_ip6_addr if_ip6s[CONFIG_LWIP_IPV6_NUM_ADDRESSES];
global_eth_component->ipv6_count_ = esp_netif_get_all_ip6(global_eth_component->eth_netif_, if_ip6s);
#ifdef USE_ETHERNET_IP_STATE_LISTENERS
global_eth_component->ip_state_pending_.store(1, std::memory_order_release);
#endif
#if (USE_NETWORK_MIN_IPV6_ADDR_COUNT > 0)
global_eth_component->connected_ =
global_eth_component->got_ipv4_address_ && (global_eth_component->ipv6_count_ >= USE_NETWORK_MIN_IPV6_ADDR_COUNT);
@@ -827,9 +839,6 @@ void EthernetComponent::got_ip6_event_handler(void *arg, esp_event_base_t event_
global_eth_component->connected_ = global_eth_component->got_ipv4_address_;
global_eth_component->enable_loop_soon_any_context(); // Enable loop when connection state changes
#endif
#ifdef USE_ETHERNET_IP_STATE_LISTENERS
global_eth_component->notify_ip_state_listeners_();
#endif
}
#endif /* USE_NETWORK_IPV6 */
Loaded 100 of 157 files, more files were not shown because too many files have changed in this diff. Show more