Compare commits

..
Author SHA1 Message Date
J. Nick Koston c2aa2c5995 [hardware-test] Ignore the I2C runners' build logs 2026-11-16 22:11:31 -06:00
J. Nick Koston 3040ee9b3a [hardware-test] Clean the copied I2C harness out of the tree when run.sh is interrupted 2026-11-16 21:48:23 -06:00
J. Nick Koston 719e5bf2fa [hardware-test] Stop pdslave tokens() at a byte record cut off by a full log 2026-11-16 21:41:27 -06:00
J. Nick Koston 4af3936ede [hardware-test] Share the wire decoder, malloc counter and log parser in the I2C flows
- wiredecode.hpp holds the token decoder for snf.hpp and swp.hpp; alloc_count.hpp the per
  platform malloc wrap (ESP8266 in IRAM) for both sniffers and the BMI270 fake.
- snlog.py is the one parser for the SN and SW lines; clean.py --nacks replaces nackcheck.py,
  maj.py takes one to three captures and replaces devcmp.py; one buildall.sh.
- host/run.sh generates into a temp dir; the BMI270 fake has its expected hash as a default
  and a public init_bmm(); dead code removed in the comparers and stubs.
- Review fixes: ardcmp.py --reads-by-data compares returned data, runners stop on failed
  flashes and builds and return the test status, the Wire stub defines WIRE_BUFFER_SIZE,
  test_wab recognises both ASan macros.
2026-11-16 21:37:02 -06:00
J. Nick Koston 24d843302e Add I2C wire A/B and the WK2168 and BMI270 fakes to the hardware-test skill 2026-11-16 21:11:56 -06:00
92 changed files with 4326 additions and 1256 deletions

No files matched your search

+1 -1
View File
@@ -158,7 +158,7 @@ recorder, dump saver and decoder, cover cycle driver), `online_image/`
(host driver, Zephyr driver probe), `rtsp_server/` (protocol client, raw rate reader, measurement set,
API image grab), `cse7766/` (API observer with ping), `zephyr_coredump/` (reader flow, decode with the
Zephyr tools), `libretiny_prefs/` (rtc preference harness header), `libretiny_crash/` (BK72xx crash probe header,
capture decoder, RTL8720C ROM dump versus report matcher), `api_outgoing/` (wake cycle and Wi-Fi drop timing readers), `ld2450/` (zone number driver, loopback harness driver with number and binary sensor states and TX frame summarizer, frame stream harness with presence hold and link loss drivers, BT MAC driver), `api/` (raw plaintext native API client for user defined actions, store_yaml recovery client over Noise or plaintext), `http_request/` (update manifest server, update state driver, request test server, action case driver, concurrency, reboot, update check and OTA drivers, `ota_auth/` OTA request cases with basic auth), `modem/` (button and sensor driver over the plaintext API, also used for the BLE MIDI boards, AT commands for the UART passthrough), `dfrobot_sen0395/` (API driver and case runner), `homeassistant_light/` (direct bulb driver, held-report case driver), `usb_audio/` (URL player), `speaker_media_player/` (API player driver, pipeline task case runner), `web_server/` (SSE client, request mix, SSE and HTTPD case runners, per-session `/events` listing check with its config, `/events` head, multi-client, stall, Wi-Fi drop and churn modes with their config, head comparer and runner), `mqtt/` (`ha_discovery/`, `disable/`, `retained_burst/` and `reconnect/` runners; broker config with TLS, client-certificate and WebSocket listeners, test CA with server and client certificates, transport case runner, flash-and-drive runner, CLI TLS check without a board, MAC suffix runner with per-variant configs for two boards, `shutdown/` shutdown message runner and matcher), `udp/` (datagram recorder, `udp.write` payload checker and case driver), `entity_icons/` (icon and device class readback over the API, MQTT discovery, web_server and dump_config, with a comparer), `uart_sim/` (API driver and flash, capture and drive runner for the simulated UART sensors), `remote_base/` (loopback runner and API log decoder per sent frame), `xiaomi_ble/` (frame and header generator with the expected counts, injecting, advertising and receiving configs, counter and runner), `ble_terminate/` (proxy and peer configs, connect and disconnect driver, peer disconnect matcher, runner), `i2c_slave/` (D1 mini slave and BK7238 master configs), `wpa3_ap/` (PMF AP with join and leave logging, BK7238 and CB3S DUTs, runner and summary), `wisp_ap/` (WISP upstream with AP buttons, WISP and ALWAYS_ON DUTs, client, runner and per phase summary), `wifi_restart/` (WPA3 mixed WISP upstream, static IP DUTs with syslog, runner and summary), `ota_timing/` (OTA upload timer with deflate, raw and TCP modes, API build check, A/B runner and median summary, `inflate_bench/` on-device inflate benchmark with stream generator, configs and syslog runner), `gpio_hold/` (ISR pin hold self test with ESP32 and S3 configs), `ble_event_inject/` (GAP and GATTC event injector header and proxy package), `light_sleep_wake/` (LEDC wave and edge timer for light sleep wakeup on a C3, external wave generator and per-call edge detail probe for peripheral power-down on the P4), `manual_ip/` (station and AP manual IP configs, DNS probe and runners), `web_handlers/` (late handler, web_server, captive_portal and prometheus variants, web OTA runner), `bk72xx_flash/` (flash readback with retries, per partition diff of two dumps), `ble_server_security/` (secure GATT server and pairing client configs, driver, summary and runner), `prometheus/` (datetime metrics configs and driver), `zephyr_ble_server/` (nRF52 server, looping ESP32 client and UF2 flash and count runner), `ble_adv_triggers/` (advertiser with six raw frames, ESP32 and BK72xx receivers with eleven triggers, runner and checker), `sun/` (longitude sweep package, runner and checker), `sensor_filters/` (every filter shape with a fixed input sequence, per-board packages and runner), `led_driver_sniff/` (SPI slave capture header with selftest, config generator, window comparer and runner), `hub_input_sim/` (keypad, wiegand and rdm6300 simulation on a D1 mini, API log runner), `value_range/` (on_value_range sequence with a restart, runner), `ezo_fake_bus/` (fake EZO I2C bus and step config, runner), `twai_self/` (TWAI self-reception header, on_frame config and counter runner), `ota_automations/` (OTA automation package for both OTA platforms, four case runner, repeated web upload runner and per case summary), `output_button/` (pad edge timer header, button package and runner), `homeassistant_button/` (raw plaintext API client for action calls and wizard inputs, package and runner), `uart_button/` (UART0 loopback package, API driver with TX and RX counts, runner), `interval/` (plain and delayed interval package, timing summary and runner), `smp_udp_ota/` (SMP reset fault injector, OTA runner, per mode result check, MCUboot swap limit, running image check and slot blanking UF2, Thread device config), `ld2420_sim/` (fake LD2420 answering the hub's commands on a UART0 loopback, API state recorder, two-build comparer and runneLine truncated
capture decoder, RTL8720C ROM dump versus report matcher), `api_outgoing/` (wake cycle and Wi-Fi drop timing readers), `ld2450/` (zone number driver, loopback harness driver with number and binary sensor states and TX frame summarizer, frame stream harness with presence hold and link loss drivers, BT MAC driver), `api/` (raw plaintext native API client for user defined actions, store_yaml recovery client over Noise or plaintext), `http_request/` (update manifest server, update state driver, request test server, action case driver, concurrency, reboot, update check and OTA drivers, `ota_auth/` OTA request cases with basic auth), `modem/` (button and sensor driver over the plaintext API, also used for the BLE MIDI boards, AT commands for the UART passthrough), `dfrobot_sen0395/` (API driver and case runner), `homeassistant_light/` (direct bulb driver, held-report case driver), `usb_audio/` (URL player), `speaker_media_player/` (API player driver, pipeline task case runner), `web_server/` (SSE client, request mix, SSE and HTTPD case runners, per-session `/events` listing check with its config, `/events` head, multi-client, stall, Wi-Fi drop and churn modes with their config, head comparer and runner), `mqtt/` (`ha_discovery/`, `disable/`, `retained_burst/` and `reconnect/` runners; broker config with TLS, client-certificate and WebSocket listeners, test CA with server and client certificates, transport case runner, flash-and-drive runner, CLI TLS check without a board, MAC suffix runner with per-variant configs for two boards, `shutdown/` shutdown message runner and matcher), `udp/` (datagram recorder, `udp.write` payload checker and case driver), `entity_icons/` (icon and device class readback over the API, MQTT discovery, web_server and dump_config, with a comparer), `uart_sim/` (API driver and flash, capture and drive runner for the simulated UART sensors), `remote_base/` (loopback runner and API log decoder per sent frame), `xiaomi_ble/` (frame and header generator with the expected counts, injecting, advertising and receiving configs, counter and runner), `ble_terminate/` (proxy and peer configs, connect and disconnect driver, peer disconnect matcher, runner), `i2c_slave/` (D1 mini slave and BK7238 master configs), `wpa3_ap/` (PMF AP with join and leave logging, BK7238 and CB3S DUTs, runner and summary), `wisp_ap/` (WISP upstream with AP buttons, WISP and ALWAYS_ON DUTs, client, runner and per phase summary), `wifi_restart/` (WPA3 mixed WISP upstream, static IP DUTs with syslog, runner and summary), `ota_timing/` (OTA upload timer with deflate, raw and TCP modes, API build check, A/B runner and median summary, `inflate_bench/` on-device inflate benchmark with stream generator, configs and syslog runner), `gpio_hold/` (ISR pin hold self test with ESP32 and S3 configs), `ble_event_inject/` (GAP and GATTC event injector header and proxy package), `light_sleep_wake/` (LEDC wave and edge timer for light sleep wakeup on a C3, external wave generator and per-call edge detail probe for peripheral power-down on the P4), `manual_ip/` (station and AP manual IP configs, DNS probe and runners), `web_handlers/` (late handler, web_server, captive_portal and prometheus variants, web OTA runner), `bk72xx_flash/` (flash readback with retries, per partition diff of two dumps), `ble_server_security/` (secure GATT server and pairing client configs, driver, summary and runner), `prometheus/` (datetime metrics configs and driver), `zephyr_ble_server/` (nRF52 server, looping ESP32 client and UF2 flash and count runner), `ble_adv_triggers/` (advertiser with six raw frames, ESP32 and BK72xx receivers with eleven triggers, runner and checker), `sun/` (longitude sweep package, runner and checker), `sensor_filters/` (every filter shape with a fixed input sequence, per-board packages and runner), `led_driver_sniff/` (SPI slave capture header with selftest, config generator, window comparer and runner), `hub_input_sim/` (keypad, wiegand and rdm6300 simulation on a D1 mini, API log runner), `value_range/` (on_value_range sequence with a restart, runner), `ezo_fake_bus/` (fake EZO I2C bus and step config, runner), `twai_self/` (TWAI self-reception header, on_frame config and counter runner), `ota_automations/` (OTA automation package for both OTA platforms, four case runner, repeated web upload runner and per case summary), `output_button/` (pad edge timer header, button package and runner), `homeassistant_button/` (raw plaintext API client for action calls and wizard inputs, package and runner), `uart_button/` (UART0 loopback package, API driver with TX and RX counts, runner), `interval/` (plain and delayed interval package, timing summary and runner), `smp_udp_ota/` (SMP reset fault injector, OTA runner, per mode result check, MCUboot swap limit, running image check and slot blanking UF2, Thread device config), `ld2420_sim/` (fake LD2420 answering the hub's commands on a UART0 loopback, API state recorder, two-build comparer and runneLine truncated
## Templates in `$SKILL/templates`
@@ -2,13 +2,13 @@
## Fake devices on an in-firmware I2C bus
When there is no chip and no wiring, subclass `i2c::I2CBus` in an include, implement `write_readv()` only, answer
the registers the driver reads and check each write. Point the component at it from an `on_boot` lambda that runs
before the component's `setup()`: `id(x)->set_i2c_bus(&fake::bus)` at priority 950, which is ahead of every I2C
component (hubs set up at IO, 900, sensors at DATA, 600). The configs here that use 700 to 1000 work for their
components; use 950 for a new one. Keep a real `i2c:` block in the config so validation passes. Bulk
loops in a button lambda must call `App.feed_wdt()` and keep their per-iteration logs below INFO: 2000 DEBUG lines at
115200 baud trip the task watchdog, which is a fault of the harness and not of the PR.
When there is no chip and no wiring, subclass `i2c::I2CBus` in an include, implement `write_readv()` only, answer the
registers the driver reads and check each write. Point the component at it from an `on_boot` lambda that runs before the
component's `setup()`: `id(x)->set_i2c_bus(&fake::bus)` at a priority above the swapped component's own setup priority
(a hub at BUS - 0.1 needs 999.95; 950 is ahead of hubs at IO, 900, and sensors at DATA, 600). Keep a real `i2c:` block
in the config so validation passes. Bulk loops in a button lambda must call `App.feed_wdt()` and keep their
per-iteration logs below INFO: 2000 DEBUG lines at 115200 baud trip the task watchdog, which is a fault of the harness
and not of the PR. Sensor publishes log at VERBOSE, so log entity values from a lambda (`on_value` or an interval).
`templates/i2c_fake/` holds the fakes; each `*-common.yaml` or `*-test.yaml` is a classic ESP32 or ESP32-S3 config
that includes its `.hpp` (add `esphome: name:` for the `-common` ones):
@@ -151,3 +151,42 @@ Prefer this to glitching the wire. Holding SDA low by rerouting the pin (`esp_ro
`SIG_GPIO_OUT_IDX` and back to `I2CEXT0_SDA_OUT_IDX`) wedges the ESP-IDF controller, which then needs
`i2c_master_bus_reset()` after each glitch, and glitches timed onto writes left the VL53L0X answering 0x00 until a
power cycle.
## WeiKai WK2168 on I2C (#22759)
`templates/i2c_fake/wkfake.hpp` + `wk-common.yaml`: answers 0x10 to 0x17 (`0 A1 A0 1 0 C1 C0 F`), keeps only A3..A0 of
the register byte and builds the 6 bit address C1C0A3..A0, models the globals (a GRST write resets the channels in its
low nibble and logs `!!!!! GRST WRITE`), per-channel SCR/LCR/FCR/baud/RFCNT/FSR and FIFOs (TX logged, RX injected with
`inject_rx`, dropped when SCR is off), GPDIR/GPDAT with `set_input()`. It classifies each transaction W / P (1 byte
pointer) / R / WR (repeated start); `fail_reads`, `fail_ptr` and `every_n` inject NACKs and the fake counts whether
the next read is a retry of the same register. The driver polls RFCNT and FSR of every channel and GPDAT every loop:
log only writes, changes and a `trace` window, never every read. `wk-map-check.cpp.example` is a host check with
copies of the driver's `i2c_address()` (the same in both trees) and the PR's `i2c_register()`, comparing the address
and register byte each tree sends for every register; paste the current bodies in before running it. On #22759 the
base build's GPIO writes hit GRST and reset UART channel 0.
## BMI270 config upload and BMM150 aux (#22778)
`bmi270fake.hpp` + `bmi270-common.yaml` + `bmi270-motion-aux.yaml`: BMI270 register file with reset values; config
upload port (INIT_ADDR word address, INIT_DATA into 8 KB) where INTERNAL_STATUS reads 0x01 only when every byte was
written once and FNV-1a equals `expected_fnv` (default 0x95BB75B5, the current table, computed on the host from
bmi270_config.h; the host harness takes an override in `BMI_EXPECTED_FNV`), else 0x02. Aux interface: AUX_DEV_ID selects
the BMM150, manual mode turns AUX_WR_ADDR/AUX_RD_ADDR writes into BMM150 writes and 1/2/6/8 byte reads into AUX_X..,
STATUS.aux_busy for two polls, auto mode mirrors the BMM150 data. Fault knobs: `bmm_mode` (absent, wrong chip id),
`busy_stuck`, `stuck_from_aux_op` (busy stuck from the Nth aux transfer: 1 power-on, 2 soft reset, 3 chip id, 4 to 6
trim, 7 normal mode), `nack_at` (main-bus transaction N). `norm_fnv` hashes register-level effects and leaves STATUS
polls out (their count depends on timing). Heap is sampled at INIT_CTRL=0/1 and inside each INIT_DATA burst;
`-DBMI_FAKE_WRAP_MALLOC` with `-Wl,--wrap=malloc` in `platformio_options: build_flags` counts malloc() during the upload
through `alloc_count.hpp` (list it in `includes:` too; the link fails without the wrap, so a clean link proves it
applied).
- Host side: copy `bmi270-host-harness.cpp.example` (as a `.cpp`) and the fake into `tests/components/bmi270/` of both
worktrees, plus a `benchmark.yaml` with `esphome: build_flags: [-DUSE_BMI270_BMM150, -DUSE_LOG_LISTENERS,
-DESPHOME_LOG_MAX_LISTENERS=4]` (the unit test build does not use defines.h; this is how to get a component's
optional code and log capture into it); the base also needs the PR's `__init__.py` manifest override.
`bmi270-setters.py` turns each tree's generated main.cpp (`esphome compile --only-generate`) into the setter list
the harness applies, so codegen changes (setters skipped at defaults) are tested as generated.
`bmi270-host-run.sh.example TREE NAME SETTERS [32]` runs it (32 forces the Arduino burst branch);
`bmi270-host-compare.py BASE.report PR.report` merges base's split INIT_ADDR writes, compares every config, fault and
per-step scenario, maps the NACK sweep through the merged stream and lists every log difference.
`bmm150-sweep.cpp.example` compares a decoder against a namespace-renamed copy of the base header bit for bit.
- A planted one byte burst offset gave INTERNAL_STATUS 0x02 and "Config file upload failed" at both burst sizes.
@@ -14,3 +14,39 @@ On the NiceMCU WB3S V1 board (BK7238), P26 and P24 are on the right-hand header.
Fake devices that need no real chip (in-firmware buses, the Pico 2 W target, the DS2482-800 simulator, NACK
injection) are in [fake_i2c.md](fake_i2c.md).
## I2C wire A/B of a bus change (#22780, write_prefixed)
`scripts/i2c_wire_ab/`.
- Host (`host/run.sh TREE OUT`, then `host/compare.py BASE PR`): copies a harness into the tree's
`tests/components/i2c/` (removed again afterwards) and runs `script/cpp_unit_test.py i2c`. `extract.py` cuts the
Arduino and ESP-IDF `write_readv`/`transfer_` bodies out of the tree and compiles them against the tree's real headers
with stub `Wire.h` (esp8266 core 3.1.2 and arduino-pico 6.3.2 buffer rules, 128 and 256 bytes) and a stub
`driver/i2c_master.h` (IDF 5.5.5 checks: at most SOC_I2C_CMD_REG_NUM jobs, NACK before STOP); the software bus is
compiled whole with `#define InternalGPIOPin wab::FakePin` over a bit-level target. Every bus emits one token stream
(`S 90+ 02+ ... P`, `Sr`, `rA0+`), with mallocs counted through macOS `malloc_logger` (ASan is dropped on macOS).
Stub headers must live outside the test dir (a subfolder there is taken as a platform domain) and reach the build
through a `benchmark.yaml` `esphome: build_flags: [-I...]`; `.h` files in the test dir are included into main.cpp, so
generated bodies are `.inc`. Explicit-instantiation member robbers must be declared at global scope. The sources are
stored as `.cpp.example`, `.h.example` and `.hpp` so the repo's C++ lints skip them; `run.sh` copies them back.
- Device: `dev/snf.hpp` is a GPIO interrupt sniffer on the I2C pads (an ESP32-C2 with an ADS1015: SDA 4, SCL 5,
`dev/c2common.yaml`) plus the same case sweep and a malloc counter; at 10 to 20 kHz on the C2 it still drops a bit in
about 0.5 % of transactions (mostly at the chip's NACK), so capture three runs per build with
`dev/flashrun.sh NAME YAML PORT SECONDS SUFFIX` (suffix empty, 2 and 3 write NAME-ser.txt, -ser2 and -ser3) and
judge with `clean.py` (`--nacks` sorts each DIFF into a chip NACK or not) and `maj.py`, which also takes one or two
captures. On the software bus it is exact. `dev/pdslave.hpp` with `snf.hpp` built with `-DSNF_PDSLAVE`
(`pdcommon.yaml`) turn an ESP32-PICO-D4 into its own target at 0x48 on GPIO32/33 (ACKs every byte, so 300-byte
writes reach the wire): exact on 6 of 6 captures at 25 kHz once one warm-up probe runs before the sweep (the ISR
runs from flash; IRAM_ATTR on header inline functions fails to link with l32r literal errors). `mksw.py` turns a
tree's LibreTiny-only software bus into a header so an ESP32 build can run it on real pins. The ADS1015 NACKs the
third byte after the pointer, so long writes on that chip end there on every build.
- `ard/swp.hpp` is the same sweep for ESP8266 (GPIO interrupt sniffer on GPIO4/5), RP2 (raw IO_IRQ_BANK0 handler) and
BK72xx (no sniffer, so `ard/ardcmp.py --reads-by-data` compares reads by their data), with `ard/common.yaml` plus a
board file (`d1.yaml`, `pico.yaml`, `bk.yaml`). Both sweeps share the decoder in `wiredecode.hpp` and count heap
calls with `templates/i2c_fake/alloc_count.hpp`, which names the link flag for each platform. Each image checks
itself with `ALLOC_PROBE 1`. `buildall.sh YAML...` builds the configs of either folder, each under the `name` of
its substitutions (the file name without one), `x-pr.yaml` from PR_TREE and the rest from BASE_TREE.
- Facts: on a D1 mini without a target, register writes up to 128 bytes total show `S 90- P`, longer ones fail with
rc 6 and nothing on the wire; a write+read whose address is NACKed leaves no STOP (`S 90-`), dev too.
arduino-pico: after a register write over 256 bytes, the next empty write (a probe) sends the stale buffer to the
failed write's address.
@@ -37,3 +37,7 @@ npm1300/*-cap.err
npm1300/*-run.log
rp2_sched_lock/*.txt
rp2_sched_lock/*.log
i2c_wire_ab/*/*-ser*.txt
i2c_wire_ab/*/*-flash.log
i2c_wire_ab/*/*-compile.log
i2c_wire_ab/*/*-upload.log
@@ -0,0 +1,50 @@
"""ardcmp.py BASE_SER PR_SER [--reads-by-data]: per case rc and wire hash must match; alloc summary, rc counts and
wire shapes. --reads-by-data compares read cases by the returned data instead (BK72xx has no sniffer, so its wire
hash is that of an empty stream)."""
import argparse
from collections import Counter
import pathlib
import re
import sys
sys.path.insert(0, str(pathlib.Path(__file__).resolve().parent.parent))
from snlog import first_alloc, is_read, load # noqa: E402
ap = argparse.ArgumentParser(description=__doc__.splitlines()[0])
ap.add_argument("base")
ap.add_argument("pr")
ap.add_argument("--reads-by-data", action="store_true")
args = ap.parse_args()
b, p = load(args.base, "SW"), load(args.pr, "SW")
print("cases", len(b), len(p))
def key(label: str, case: dict) -> tuple:
if args.reads_by_data and is_read(label):
return case["rc"], case["rd"]
return case["rc"], case["fnv"]
diff = [k for k in b if k not in p or key(k, b[k]) != key(k, p[k])]
print("rc/wire (or read data) differences:", len(diff))
for k in diff[:20]:
print(" ", k, "\n base", b[k], "\n pr ", p.get(k))
for name, d in (("base", b), ("pr", p)):
wr = [k for k in d if k.startswith("wreg")]
allocs = {k: v["alloc"] for k, v in d.items()}
first = {s: first_alloc(allocs, s) for s in ("wreg8", "wreg16")}
print(
f"{name}: write_register calls that allocated {sum(allocs[k] > 0 for k in wr)} of {len(wr)}; first "
f"allocating len {first}; other allocs {[k for k in d if not k.startswith('wreg') and allocs[k]]}"
)
print(
" rc counts:",
dict(Counter(v["rc"] for v in d.values())),
"| wire shapes:",
dict(
Counter(
re.sub(r"[0-9A-F]{2}", "xx", v["wire"]) for v in d.values()
).most_common(6)
),
)
@@ -0,0 +1,6 @@
bk72xx:
board: xh-wb3s # set your board
ads1115:
- address: 0x48
id: dev
i2c_id: bus_hw
@@ -0,0 +1,15 @@
#!/bin/bash
# bkrun.sh NAME PORT: flash NAME's UF2 to a BK72xx board (up to 3 tries), then capture 60 s from a reset (log on
# UART1) and print the sweep line count, the ALLOC_PROBE line and the SWDONE line. env: PY, LTPY (LibreTiny python)
D=$(cd "$(dirname "$0")" && pwd); S=$(dirname "$(dirname "$D")"); cd "$D" || exit 1
N=${1:?name}; P=${2:?port}; PY=${PY:-python3}
LTPY=${LTPY:-$D/../pio/penv/.libretiny/bin/python}
[ -x "$LTPY" ] || LTPY=~/.platformio/penv/.libretiny/bin/python
for i in 1 2 3; do
timeout 200 "$LTPY" "$S/flash_bk72xx.py" flash write -d "$P" ".esphome/build/$N/.pioenvs/$N/firmware.uf2" > "$N-flash.log" 2>&1 &&
break
echo "flash try $i failed: $(tail -1 "$N-flash.log")"
[ "$i" = 3 ] && exit 1
done
$PY "$S/serial_capture.py" "$P" 60 "$N-ser.txt" --reset > /dev/null 2>&1
echo "$N $(grep -a -c 'SW ' "$N-ser.txt") $(grep -a -o 'ALLOC_PROBE.*' "$N-ser.txt") $(grep -a -o 'SWDONE.*' "$N-ser.txt")"
@@ -0,0 +1,47 @@
esphome:
name: ${name}
includes:
- ../../../templates/i2c_fake/alloc_count.hpp
- ../wiredecode.hpp
- swp.hpp
platformio_options:
build_flags:
- ${wrapflag}
- ${defs}
on_boot:
- priority: -100
then:
- delay: ${start_delay}
- lambda: |-
swp::bus = id(bus_hw);
swp::install();
id(started) = true;
logger:
level: DEBUG
hardware_uart: ${loguart}
globals:
- id: started
type: bool
initial_value: "false"
- id: wedged
type: bool
initial_value: "false"
i2c:
id: bus_hw
sda: ${sda}
scl: ${scl}
frequency: ${freq}
scan: true
interval:
- interval: 20ms
then:
- lambda: |-
if (!id(started)) return;
if (!swp::step(id(dev)) && !id(wedged) && ${do_wedge}) {
id(wedged) = true;
swp::wedge(id(dev));
}
@@ -0,0 +1,6 @@
esp8266:
board: d1_mini
ads1115:
- address: 0x48
id: dev
i2c_id: bus_hw
@@ -0,0 +1,17 @@
rp2040:
board: rpipico2w
sensor:
- platform: ina226
id: dev
i2c_id: bus_hw
address: 0x40
shunt_resistance: 0.1 ohm
update_interval: 2s
bus_voltage:
name: vbus
on_value:
- lambda: ESP_LOGI("rd", "READ vbus %.4f", x);
shunt_voltage:
name: vshunt
on_value:
- lambda: ESP_LOGI("rd", "READ vshunt %.6f", x);
@@ -0,0 +1,10 @@
#!/bin/bash
# picorun.sh NAME YAML PORT [OUTSUFFIX]: upload from TREE (reboots the Pico), wait for the port, capture 60 s and print
# the sweep line count, the SWDONE line and the count of wedge cases. env: TREE, PY
D=$(cd "$(dirname "$0")" && pwd); S=$(dirname "$(dirname "$D")"); cd "$D" || exit 1
N=${1:?name}; Y=${2:?yaml}; P=${3:?port}; X=${4:-}; PY=${PY:-${TREE:?set TREE}/venv/bin/python}
PIO_CORE=${PIO_CORE:-$D/../pio} bash "$S/pio_build.sh" upload "$N" "$Y" "$P" > /dev/null || exit 1
$PY "$S/ports.py" --wait 2e8a > /dev/null 2>&1
$PY "$S/serial_capture.py" "$P" 60 "$N-ser$X.txt" > /dev/null 2>&1
echo "$N$X $(grep -a -c 'SW ' "$N-ser$X.txt") $(grep -a -o 'SWDONE.*' "$N-ser$X.txt")" \
"$(grep -a -c 'WEDGE.*rc=6 then read rc=6' "$N-ser$X.txt")"
@@ -0,0 +1,235 @@
#pragma once
// esphome#22780 sweep for the Arduino (ESP8266, RP2040) and software (BK72xx) buses: one "SW" line per I2CDevice call
// with rc, mallocs during the call (alloc_count.hpp) and, where a sniffer exists (ESP8266, RP2), the decoded wire
// stream and its hash.
#include <cstring>
#include <functional>
#include <string>
#include "esphome/core/application.h"
#include "esphome/core/hal.h"
#include "esphome/core/helpers.h"
#include "esphome/core/log.h"
#include "esphome/components/i2c/i2c.h"
#include "alloc_count.hpp"
#include "wiredecode.hpp"
#ifdef USE_RP2
#include <hardware/gpio.h>
#include <hardware/irq.h>
#include <hardware/structs/sio.h>
#endif
#ifndef SWP_ADDR
#define SWP_ADDR 0x48
#endif
#ifndef SWP_WREG
#define SWP_WREG 0x02
#endif
#ifndef SWP_RREG
#define SWP_RREG 0x01
#endif
#ifndef SWP_MAXLEN8
#define SWP_MAXLEN8 300
#endif
#ifndef SWP_MAXLEN16
#define SWP_MAXLEN16 300
#endif
#ifndef SWP_SDA
#define SWP_SDA 4
#endif
#ifndef SWP_SCL
#define SWP_SCL 5
#endif
namespace swp {
static const char *const TAG = "swp";
#if defined(USE_ESP8266) || defined(USE_RP2)
#define SWP_SNIFF 1
static constexpr uint32_t N = 12000;
static uint8_t buf[N];
static volatile uint32_t n = 0, ovf = 0;
static volatile uint8_t last = 3;
static volatile bool armed = false;
static inline __attribute__((always_inline)) uint8_t levels() {
#ifdef USE_ESP8266
uint32_t v = GPI;
#else
uint32_t v = sio_hw->gpio_in;
#endif
return ((v >> SWP_SDA) & 1) | (((v >> SWP_SCL) & 1) << 1);
}
#ifdef USE_ESP8266
static void IRAM_ATTR isr() {
#else
static void isr() {
for (int p : {SWP_SDA, SWP_SCL}) {
uint32_t ev = gpio_get_irq_event_mask(p);
if (ev)
gpio_acknowledge_irq(p, ev);
}
#endif
uint8_t s = levels();
if (!armed || s == last)
return;
last = s;
if (n < N)
buf[n++] = s;
else
ovf = ovf + 1;
}
static void install() {
#ifdef USE_ESP8266
attachInterrupt(SWP_SDA, isr, CHANGE);
attachInterrupt(SWP_SCL, isr, CHANGE);
#else
gpio_add_raw_irq_handler_masked((1u << SWP_SDA) | (1u << SWP_SCL), isr);
gpio_set_irq_enabled(SWP_SDA, GPIO_IRQ_EDGE_RISE | GPIO_IRQ_EDGE_FALL, true);
gpio_set_irq_enabled(SWP_SCL, GPIO_IRQ_EDGE_RISE | GPIO_IRQ_EDGE_FALL, true);
irq_set_enabled(IO_IRQ_BANK0, true);
#endif
ESP_LOGI(TAG, "sniffer on SDA %d SCL %d", SWP_SDA, SWP_SCL);
}
#else
#define SWP_SNIFF 0
static void install() {}
#endif
static i2c::I2CBus *bus = nullptr;
static uint8_t data[320];
static uint8_t rdata[40];
static uint32_t idx = 0, nalloc = 0;
static bool done = false, probed = false;
#if SWP_SNIFF
static uint8_t start = 3; // levels when the sniffer was armed
#endif
static void arm() {
#if SWP_SNIFF
n = 0;
ovf = 0;
start = levels();
last = start;
armed = true;
#endif
alloc_count::count = 0;
alloc_count::window = true;
}
static void disarm() {
alloc_count::window = false;
#if SWP_SNIFF
armed = false;
#endif
}
static std::string sniffed(uint32_t &amb) {
#if SWP_SNIFF
return wiredecode::decode(buf, n, start, amb);
#else
return {};
#endif
}
static bool step(i2c::I2CDevice *dev) {
if (done)
return false;
if (!probed) {
// the counter must see a malloc inside the window
alloc_count::count = 0;
alloc_count::window = true;
void *volatile p = malloc(100);
alloc_count::window = false;
free(p);
ESP_LOGI(TAG, "ALLOC_PROBE %u", (unsigned) alloc_count::count);
probed = true;
}
const uint32_t W8 = SWP_MAXLEN8 + 1, W16 = SWP_MAXLEN16 + 1;
std::string label;
uint32_t i = idx++;
for (size_t k = 0; k < sizeof data; k++)
data[k] = static_cast<uint8_t>(0x11 + i + k * 7);
memset(rdata, 0, sizeof rdata);
i2c::ErrorCode rc;
if (i < W8) {
label = "wreg8 len=" + std::to_string(i);
arm();
rc = dev->write_register(SWP_WREG, data, i);
disarm();
} else if (i < W8 + W16) {
size_t len = i - W8;
label = "wreg16 len=" + std::to_string(len);
arm();
rc = dev->write_register16((SWP_WREG << 8) | 0x03, data, len);
disarm();
} else if (i < W8 + W16 + 10) {
static const size_t ns[] = {1, 2, 3, 4, 8};
uint32_t j = i - W8 - W16;
size_t k = ns[j % 5];
bool sixteen = j >= 5;
label = std::string(sixteen ? "rreg16" : "rreg8") + " n=" + std::to_string(k);
arm();
rc = sixteen ? dev->read_register16(SWP_RREG << 8, rdata, k) : dev->read_register(SWP_RREG, rdata, k);
disarm();
} else if (i < W8 + W16 + 13) {
size_t k = i - (W8 + W16 + 10) + 1;
label = "read n=" + std::to_string(k);
arm();
rc = dev->read(rdata, k);
disarm();
} else if (i < W8 + W16 + 16) {
static const uint8_t p1[] = {SWP_RREG};
size_t k = i - (W8 + W16 + 13) + 1;
label = "write1+read n=" + std::to_string(k);
arm();
rc = dev->write_read(p1, 1, rdata, k);
disarm();
} else if (i < W8 + W16 + 16 + 112) {
uint8_t a = 8 + (i - (W8 + W16 + 16));
label = "probe " + std::to_string(a);
arm();
rc = bus->write_readv(a, nullptr, 0, nullptr, 0);
disarm();
} else {
done = true;
ESP_LOGI(TAG, "SWDONE cases=%u alloc_calls=%u", (unsigned) idx - 1, (unsigned) nalloc);
return false;
}
nalloc += alloc_count::count != 0;
uint32_t amb = 0, events = 0, overflow = 0;
std::string got = sniffed(amb);
#if SWP_SNIFF
events = n;
overflow = ovf;
#endif
char rd[3 * 8 + 1] = "";
for (int k = 0; k < 8 && label.rfind("wreg", 0) != 0 && label.rfind("probe", 0) != 0; k++)
snprintf(rd + 3 * k, 4, "%02X ", rdata[k]);
ESP_LOGI(TAG, "SW %s|rc=%d|alloc=%u|ev=%u|amb=%u|ovf=%u|fnv=%08X|%s|rd=%s", label.c_str(), (int) rc,
(unsigned) alloc_count::count, (unsigned) events, (unsigned) amb, (unsigned) overflow,
(unsigned) fnv1a_hash(got), got.substr(0, 90).c_str(), rd);
App.feed_wdt();
return true;
}
// Over-long register write and what the bus does afterwards (on arduino-pico this leaves Wire stuck, dev too).
// Each step is sniffed; the probe step shows whether a stale buffer goes out.
static void wedge_step(const char *what, const std::function<i2c::ErrorCode()> &fn) {
uint32_t amb = 0;
arm();
auto rc = fn();
disarm();
std::string got = sniffed(amb);
ESP_LOGI(TAG, "WEDGE %s rc=%d ev=%u wire=%s", what, (int) rc, (unsigned) got.size(), got.substr(0, 120).c_str());
App.feed_wdt();
}
static void wedge(i2c::I2CDevice *dev) {
static uint8_t r[2];
wedge_step("read_register before", [dev] { return dev->read_register(SWP_RREG, r, 2); });
wedge_step("plain read before", [dev] { return dev->read(r, 1); });
wedge_step("write_register len=300", [dev] { return dev->write_register(SWP_WREG, data, 300); });
wedge_step("read_register after", [dev] { return dev->read_register(SWP_RREG, r, 2); });
wedge_step("plain read after", [dev] { return dev->read(r, 1); });
wedge_step("write_register len=2 after", [dev] { return dev->write_register(SWP_WREG, data, 2); });
wedge_step("probe 0x50 after", [] { return bus->write_readv(0x50, nullptr, 0, nullptr, 0); });
wedge_step("plain read after probe", [dev] { return dev->read(r, 1); });
wedge_step("write_register len=2 after probe", [dev] { return dev->write_register(SWP_WREG, data, 2); });
}
} // namespace swp
@@ -0,0 +1,14 @@
#!/bin/bash
# buildall.sh YAML...: compile each config under the `name:` of its substitutions (the file name when it sets none),
# from the tree named by its suffix (`x-pr.yaml` builds from PR_TREE, any other from BASE_TREE).
# env: PR_TREE, BASE_TREE, PY, BUILD_LOCK (optional), PIO_CORE (default i2c_wire_ab/pio)
# Stops at the first failed build and exits with its status.
set -o pipefail
D=$(cd "$(dirname "$0")" && pwd); S=$(dirname "$D")
for y in "$@"; do
b=$(basename "$y" .yaml)
case ${b##*-} in pr) T=${PR_TREE:?set PR_TREE} ;; *) T=${BASE_TREE:?set BASE_TREE} ;; esac
nm=$(awk '/^substitutions:/{s=1; next} /^[^ #]/{s=0} s && /^ name:/{gsub(/["\x27]/, "", $2); print $2; exit}' "$y")
nm=${nm:-$b}
TREE=$T PIO_CORE=${PIO_CORE:-$D/pio} bash "$S/pio_build.sh" compile "$nm" "$y" | sed "s/^/$nm /" || exit
done
@@ -0,0 +1,83 @@
esphome:
name: ${name}
includes:
- ${swfile}
- ../../../templates/i2c_fake/alloc_count.hpp
- ../wiredecode.hpp
- snf.hpp
platformio_options:
build_flags:
- -Wl,--wrap=malloc
- ${flags}
on_boot:
- priority: 700
then:
- lambda: |-
gpio_reset_pin(GPIO_NUM_6); gpio_set_direction(GPIO_NUM_6, GPIO_MODE_OUTPUT); gpio_set_level(GPIO_NUM_6, 1);
gpio_reset_pin(GPIO_NUM_7); gpio_set_direction(GPIO_NUM_7, GPIO_MODE_OUTPUT); gpio_set_level(GPIO_NUM_7, 0);
#ifdef SNF_SW
snf::bus = snf::make_sw();
id(ads)->set_i2c_bus(snf::bus);
#else
snf::bus = id(bus_hw);
#endif
- priority: -100
then:
- delay: 6s
- lambda: |-
snf::install();
id(started) = true;
esp32:
board: esp32-c2-devkitm-1
framework:
type: esp-idf
sdkconfig_options:
CONFIG_XTAL_FREQ_26: y
logger:
level: DEBUG
baud_rate: 115200
globals:
- id: started
type: bool
initial_value: "false"
i2c:
id: bus_hw
sda: ${hw_sda}
scl: ${hw_scl}
frequency: ${freq}
scan: true
ads1115:
- address: 0x48
id: ads
i2c_id: bus_hw
sensor:
- platform: ads1115
ads1115_id: ads
multiplexer: A2_GND
gain: 6.144
resolution: 12_BITS
name: a2
update_interval: 2s
on_value:
- lambda: ESP_LOGI("rd", "READ a2 %.4f", x);
- platform: ads1115
ads1115_id: ads
multiplexer: A3_GND
gain: 6.144
resolution: 12_BITS
name: a3
update_interval: 2s
on_value:
- lambda: ESP_LOGI("rd", "READ a3 %.4f", x);
interval:
- interval: 20ms
then:
- lambda: |-
if (id(started)) snf::step(id(ads));
@@ -0,0 +1,69 @@
"""clean.py [--nacks] NAME...: per build, cases with at least one capture (NAME-ser.txt, -ser2, -ser3) that is exactly
the expected stream or the expected stream cut by the chip's NACK; also every rc seen per case.
--nacks: per capture, count the DIFF cases that are a chip NACK and list the others."""
import argparse
from collections import Counter, defaultdict
import pathlib
import sys
sys.path.insert(0, str(pathlib.Path(__file__).resolve().parent.parent))
from snlog import CAPTURES, load, nack_cut, runs # noqa: E402
def clean(case: dict) -> bool:
if case["verdict"] == "OK":
return True
return nack_cut(case.get("got", []), case.get("exp", [])) is not None
def nacks(name: str) -> None:
for s in CAPTURES:
path = pathlib.Path(f"{name}-ser{s}.txt")
if not path.exists():
continue
c: Counter[str] = Counter()
other = []
for label, case in load(path).items():
if case["verdict"] == "OK":
continue
got, exp = case.get("got", []), case.get("exp", [])
if (j := nack_cut(got, exp)) is not None:
c[f"chip NACK at token {j}"] += 1
else:
other.append((label, " ".join(got)[:80], " ".join(exp)[:80]))
print(path, dict(c), "other:", len(other))
for o in other[:10]:
print(" ", o)
ap = argparse.ArgumentParser(description=__doc__.splitlines()[0])
ap.add_argument(
"--nacks", action="store_true", help="classify the DIFF cases of each capture"
)
ap.add_argument("names", nargs="+")
args = ap.parse_args()
if args.nacks:
for name in args.names:
nacks(name)
sys.exit()
allrc = defaultdict(set)
for name in args.names:
caps = runs(name)
bad = [k for k in caps[0] if not any(k in r and clean(r[k]) for r in caps)]
for r in caps:
for k, case in r.items():
allrc[k].add(case["rc"])
print(
name,
"captures",
len(caps),
"cases",
len(caps[0]),
"with no clean capture:",
bad,
)
print(
"cases whose rc differs between any captures/builds:",
[k for k, s in allrc.items() if len(s) > 1],
)
@@ -0,0 +1,10 @@
#!/bin/bash
# flashrun.sh NAME YAML PORT SECONDS [OUTSUFFIX]: upload from TREE, then capture from a reset into NAME-serOUTSUFFIX.txt
# and print the sniffed line count and the SNDONE line. clean.py and maj.py read NAME-ser.txt, -ser2 and -ser3.
# env: TREE, PY
D=$(cd "$(dirname "$0")" && pwd); S=$(dirname "$(dirname "$D")"); cd "$D" || exit 1
N=${1:?name}; Y=${2:?yaml}; P=${3:?port}; SEC=${4:?seconds}; X=${5:-}; PY=${PY:-${TREE:?set TREE}/venv/bin/python}
pkill -f "[s]erial_capture.py $P "; sleep 1
PIO_CORE=${PIO_CORE:-$D/../pio} bash "$S/pio_build.sh" upload "$N" "$Y" "$P" || exit 1
$PY "$S/serial_capture.py" "$P" "$SEC" "$N-ser$X.txt" --reset > /dev/null 2>&1
grep -a -c "SN " "$N-ser$X.txt"; grep -a "SNDONE" "$N-ser$X.txt"
@@ -0,0 +1,57 @@
"""maj.py NAME_BASE NAME_PR: per case majority of rc and wire hash (reads: rc and verdict, since the chip's registers
change between runs) over NAME-ser.txt, -ser2 and -ser3 (1 to 3 captures), then base vs PR and the alloc summary."""
import argparse
from collections import Counter
import pathlib
import sys
sys.path.insert(0, str(pathlib.Path(__file__).resolve().parent.parent))
from snlog import first_alloc, is_read, runs # noqa: E402
def majority(name: str) -> tuple[dict, int, int]:
"""Case -> (majority key, votes, max alloc); also the count of unstable cases and of captures."""
caps = runs(name)
out, unstable = {}, 0
for k in caps[0]:
c = Counter(
(r[k]["rc"], r[k]["verdict"] if is_read(k) else r[k]["fnv"])
for r in caps
if k in r
)
v, n = c.most_common(1)[0]
unstable += len(c) > 1
out[k] = (v, n, max(r[k]["alloc"] for r in caps if k in r))
return out, unstable, len(caps)
ap = argparse.ArgumentParser(description=__doc__.splitlines()[0])
ap.add_argument("base")
ap.add_argument("pr")
args = ap.parse_args()
b, ub, nb = majority(args.base)
p, up, np_ = majority(args.pr)
print(
f"cases {len(b)} {len(p)}; captures base {nb}, pr {np_}; cases whose captures disagree: base {ub}, pr {up}"
)
d = [k for k in b if k not in p or b[k][0] != p[k][0]]
print("base vs pr majority differences:", len(d))
for k in d[:30]:
print(" ", k, "base", b[k][0], "pr", p[k][0] if k in p else "missing")
weak = [k for k in b if 2 * b[k][1] <= nb or (k in p and 2 * p[k][1] <= np_)]
print("cases without a majority:", weak)
for name, maj in (("base", b), ("pr", p)):
wr = [k for k in maj if k.startswith("wreg")]
allocs = {k: v[2] for k, v in maj.items()}
print(
name,
"write_register calls that allocated:",
sum(allocs[k] > 0 for k in wr),
"of",
len(wr),
"| first allocating len:",
{s: first_alloc(allocs, s) for s in ("wreg8", "wreg16")},
"| other allocs:",
[k for k in maj if not k.startswith("wreg") and allocs[k]],
)
@@ -0,0 +1,21 @@
"""mksw.py TREE OUT: the tree's software bus (header + source) as one header for an ESP32 build."""
from pathlib import Path
import sys
t = Path(sys.argv[1]) / "esphome/components/i2c"
h = (
(t / "i2c_bus_software.h")
.read_text()
.replace("#pragma once", "")
.replace('#include "i2c_bus.h"', '#include "esphome/components/i2c/i2c_bus.h"')
)
c = (
(t / "i2c_bus_software.cpp")
.read_text()
.replace('#include "i2c_bus_software.h"', "")
)
Path(sys.argv[2]).write_text(
"#pragma once\n#define USE_LIBRETINY\n" + h + "\n" + c + "\n#undef USE_LIBRETINY\n",
encoding="utf-8",
)
@@ -0,0 +1,53 @@
esphome:
name: ${name}
includes:
- pdslave.hpp
- ../../../templates/i2c_fake/alloc_count.hpp
- ../wiredecode.hpp
- snf.hpp
platformio_options:
build_flags:
- -Wl,--wrap=malloc
- -DSNF_PDSLAVE
on_boot:
- priority: 700
then:
- lambda: |-
pds::install();
snf::bus = id(bus_hw);
- priority: -100
then:
- delay: 4s
- lambda: id(started) = true;
esp32:
board: pico32
framework:
type: esp-idf
logger:
level: DEBUG
baud_rate: 115200
globals:
- id: started
type: bool
initial_value: "false"
i2c:
id: bus_hw
sda: GPIO32
scl: GPIO33
frequency: ${freq}
scan: false
ads1115:
- address: 0x48
id: ads
i2c_id: bus_hw
interval:
- interval: 20ms
then:
- lambda: |-
if (id(started)) snf::step(id(ads));
@@ -0,0 +1,187 @@
#pragma once
// Software I2C target at 0x48 on the classic ESP32 master's own pads (GPIO32 SDA, GPIO33 SCL), after the
// `i2c_isr_fault/inasim.hpp` pattern: a GPIO ISR on core 0 spins through each transaction and pulls a line low by
// switching the pad's output mux to GPIO output 0. ACKs every byte written to 0x48, answers reads with A0, A1, ...;
// NACKs other addresses. Every START, byte (with the ACK bit seen on the wire) and STOP goes into a log.
#include <cstdint>
#include <string>
#include "esphome/core/log.h"
#include "driver/gpio.h"
#include "soc/gpio_reg.h"
#include "soc/gpio_sig_map.h"
#include "esp_cpu.h"
#include "freertos/FreeRTOS.h"
#include "freertos/task.h"
namespace pds {
static const char *const TAG = "pds";
constexpr int SDA = 32, SCL = 33;
constexpr uint32_t SDA_BIT = 1u << (SDA - 32), SCL_BIT = 1u << (SCL - 32);
constexpr uint32_t LOW_VAL = SIG_GPIO_OUT_IDX | GPIO_FUNC0_OEN_SEL;
constexpr uint32_t CPU_MHZ = 240;
inline volatile uint32_t &reg(uint32_t a) { return *reinterpret_cast<volatile uint32_t *>(a); }
inline uint32_t out_sel_addr(int pin) { return GPIO_FUNC0_OUT_SEL_CFG_REG + 4 * pin; }
inline uint32_t in() { return reg(GPIO_IN1_REG); }
inline uint32_t rel_sda = 0;
// log codes: 1 START, 2 RESTART, 3 STOP, 4 written byte / 6 read byte (next: value, ack), 5 idle abort
constexpr size_t LOGN = 4096;
inline DRAM_ATTR uint8_t lg[LOGN];
inline volatile uint32_t ln = 0, lovf = 0;
inline void put(uint8_t v) {
if (ln < LOGN)
lg[ln++] = v;
else
lovf = lovf + 1;
}
inline void sda_low(bool low) { reg(out_sel_addr(SDA)) = low ? LOW_VAL : rel_sda; }
inline void run_transaction() {
enum { ADDR, WDATA, RDATA, IGNORE } phase = ADDR;
uint32_t prev = in(), last = esp_cpu_get_cycle_count();
uint8_t byte = 0, cnt = 0, out = 0, rd = 0, pend_kind = 4, pend_val = 0;
bool acked = false;
put(1);
reg(GPIO_OUT1_W1TC_REG) = SDA_BIT;
reg(GPIO_ENABLE1_W1TS_REG) = SDA_BIT;
for (;;) {
uint32_t v = in();
uint32_t now = esp_cpu_get_cycle_count();
if (v == prev) {
if (now - last > 20000u * CPU_MHZ) {
put(5);
break;
}
continue;
}
last = now;
uint32_t ch = v ^ prev;
prev = v;
if (ch & SCL_BIT) {
if (v & SCL_BIT) {
cnt++;
if (cnt <= 8 && phase != RDATA)
byte = (byte << 1) | ((v & SDA_BIT) ? 1 : 0);
else if (cnt == 9) {
// the ACK bit as it is on the wire
bool ack = !(v & SDA_BIT);
put(pend_kind);
put(pend_val);
put(ack ? 1 : 0);
if (phase == RDATA && !ack)
phase = IGNORE;
}
} else {
if (cnt == 8) {
pend_kind = phase == RDATA ? 6 : 4;
pend_val = phase == RDATA ? out : byte;
if (phase == ADDR) {
if ((byte >> 1) == 0x48) {
sda_low(true);
acked = true;
phase = (byte & 1) ? RDATA : WDATA;
rd = 0;
} else {
phase = IGNORE;
}
} else if (phase == WDATA) {
sda_low(true);
acked = true;
} else if (phase == RDATA) {
sda_low(false); // master's ACK slot
}
} else if (cnt == 9) {
if (acked) {
sda_low(false);
acked = false;
}
if (phase == RDATA) {
out = 0xA0 + rd++;
sda_low(!(out & 0x80));
}
cnt = 0;
byte = 0;
} else if (cnt >= 1 && cnt <= 7 && phase == RDATA) {
sda_low(!(out & (0x80 >> cnt)));
}
}
} else if ((ch & SDA_BIT) && (v & SCL_BIT)) {
if (v & SDA_BIT) {
put(3);
break;
}
put(2);
sda_low(false);
phase = ADDR;
cnt = 0;
byte = 0;
acked = false;
}
}
sda_low(false);
}
inline void slave_isr(void *) {
reg(GPIO_STATUS1_W1TC_REG) = SDA_BIT;
uint32_t v = in();
if (!(v & SCL_BIT) || (v & SDA_BIT))
return;
run_transaction();
reg(GPIO_STATUS1_W1TC_REG) = SDA_BIT;
}
inline volatile bool installed = false;
inline void install_task(void *) {
rel_sda = reg(out_sel_addr(SDA));
gpio_install_isr_service(0);
gpio_set_intr_type((gpio_num_t) SDA, GPIO_INTR_NEGEDGE);
esp_err_t e = gpio_isr_handler_add((gpio_num_t) SDA, slave_isr, nullptr);
gpio_intr_enable((gpio_num_t) SDA);
ESP_LOGI(TAG, "slave on core %d handler %d rel_sda 0x%x", xPortGetCoreID(), e, (unsigned) rel_sda);
installed = true;
vTaskDelete(nullptr);
}
inline void install() {
xTaskCreatePinnedToCore(install_task, "pdsinst", 3072, nullptr, 5, nullptr, 0);
while (!installed)
vTaskDelay(1);
}
inline std::string tokens() {
std::string s;
char t[8];
for (uint32_t i = 0; i < ln; i++) {
switch (lg[i]) {
case 1:
s += "S ";
break;
case 2:
s += "Sr ";
break;
case 3:
s += "P ";
break;
case 5:
s += "IDLE ";
break;
case 4:
case 6: {
if (i + 2 >= ln) { // byte record cut off by a full log
s += "? ";
i = ln;
break;
}
uint8_t b = lg[i + 1], a = lg[i + 2];
snprintf(t, sizeof t, lg[i] == 6 ? "r%02X%c " : "%02X%c ", b, a ? '+' : '-');
i += 2;
s += t;
break;
}
default:
s += "? ";
}
}
return s;
}
} // namespace pds
@@ -0,0 +1,245 @@
#pragma once
// esphome#22780 device check: GPIO interrupt sniffer on the I2C pads, a sweep of I2CDevice call shapes, and a
// malloc counter (alloc_count.hpp, -Wl,--wrap=malloc). One "SN" line per case: rc, mallocs, decoded wire stream
// hash, match with the stream the call should produce.
#include <cstring>
#include <string>
#include <driver/gpio.h>
#include <soc/gpio_reg.h>
#include <esp_attr.h>
#include "esphome/core/application.h"
#include "esphome/core/helpers.h"
#include "esphome/core/log.h"
#include "esphome/components/i2c/i2c.h"
#include "alloc_count.hpp"
#include "wiredecode.hpp"
#ifdef SNF_PDSLAVE
// the board is its own target (pdslave.hpp); its token stream replaces the pad sniffer's decode
#include "pdslave.hpp"
#endif
namespace snf {
static const char *const TAG = "snf";
#ifndef SNF_SDA
#define SNF_SDA 4
#endif
#ifndef SNF_SCL
#define SNF_SCL 5
#endif
static constexpr size_t N = 24000;
static DRAM_ATTR uint8_t buf[N];
static volatile DRAM_ATTR uint32_t n = 0;
static volatile DRAM_ATTR uint32_t ovf = 0;
static volatile DRAM_ATTR uint8_t last = 3;
static volatile DRAM_ATTR bool armed = false;
static uint8_t start = 3; // levels when the sniffer was armed
static void IRAM_ATTR isr(void *) {
uint32_t v = REG_READ(GPIO_IN_REG);
uint8_t s = ((v >> SNF_SDA) & 1) | (((v >> SNF_SCL) & 1) << 1);
if (!armed || s == last)
return;
last = s;
if (n < N)
buf[n++] = s;
else
ovf = ovf + 1;
}
static void install() {
gpio_install_isr_service(ESP_INTR_FLAG_IRAM);
for (int p : {SNF_SDA, SNF_SCL}) {
gpio_set_intr_type((gpio_num_t) p, GPIO_INTR_ANYEDGE);
gpio_isr_handler_add((gpio_num_t) p, isr, nullptr);
gpio_intr_enable((gpio_num_t) p);
}
ESP_LOGI(TAG, "sniffer on SDA %d SCL %d", SNF_SDA, SNF_SCL);
}
static i2c::I2CBus *bus = nullptr; // the bus the device uses (hardware or the software copy)
static void wb(std::string &e, uint8_t b) {
char t[8];
snprintf(t, sizeof t, "%02X+ ", b);
e += t;
}
static void rb(std::string &e, const uint8_t *r, size_t k) {
char t[8];
for (size_t i = 0; i < k; i++) {
snprintf(t, sizeof t, "r%02X%c ", r[i], i + 1 < k ? '+' : '-');
e += t;
}
}
static uint8_t data[320];
static uint8_t rdata[40];
static uint32_t idx = 0, nok = 0, ndiff = 0, namb = 0, nalloc = 0;
static bool done = false;
static void arm() {
n = 0;
ovf = 0;
uint32_t v = REG_READ(GPIO_IN_REG);
start = ((v >> SNF_SDA) & 1) | (((v >> SNF_SCL) & 1) << 1);
last = start;
alloc_count::count = 0;
alloc_count::window = true;
#ifdef SNF_PDSLAVE
pds::ln = 0;
pds::lovf = 0;
#endif
armed = true;
}
static void disarm() {
armed = false;
alloc_count::window = false;
}
// Runs one case and returns false when the sweep is over
static bool step(i2c::I2CDevice *dev) {
if (done)
return false;
const uint8_t A = 0x48;
std::string label, exp;
#ifdef SNF_PDSLAVE
if (idx == 0) {
// warm the target ISR (it runs from flash) so the first case is not lost to a cache miss
bus->write_readv(0x48, nullptr, 0, nullptr, 0);
delay(5);
}
#endif
uint32_t i = idx++;
for (size_t k = 0; k < sizeof data; k++)
data[k] = static_cast<uint8_t>(0x11 + i + k * 7);
i2c::ErrorCode rc;
static constexpr uint32_t W8 = 301, W16 = 301; // write_register lengths 0..300
if (i < W8) {
size_t len = i;
label = "wreg8 len=" + std::to_string(len);
arm();
rc = dev->write_register(0x02, data, len);
disarm();
exp = "S ";
wb(exp, A << 1);
wb(exp, 0x02);
for (size_t k = 0; k < len; k++)
wb(exp, data[k]);
exp += "P ";
} else if (i < W8 + W16) {
size_t len = i - W8;
label = "wreg16 len=" + std::to_string(len);
arm();
rc = dev->write_register16(0x0203, data, len);
disarm();
exp = "S ";
wb(exp, A << 1);
wb(exp, 0x02);
wb(exp, 0x03);
for (size_t k = 0; k < len; k++)
wb(exp, data[k]);
exp += "P ";
} else if (i < W8 + W16 + 10) {
static const size_t ns[] = {1, 2, 3, 4, 8};
uint32_t j = i - W8 - W16;
size_t k = ns[j % 5];
bool sixteen = j >= 5;
label = std::string(sixteen ? "rreg16" : "rreg8") + " n=" + std::to_string(k);
memset(rdata, 0, sizeof rdata);
arm();
rc = sixteen ? dev->read_register16(0x0100, rdata, k) : dev->read_register(0x01, rdata, k);
disarm();
exp = "S ";
wb(exp, A << 1);
wb(exp, 0x01);
if (sixteen)
wb(exp, 0x00);
exp += "Sr ";
wb(exp, (A << 1) | 1);
rb(exp, rdata, k);
exp += "P ";
} else if (i < W8 + W16 + 13) {
size_t k = i - (W8 + W16 + 10) + 1;
label = "read n=" + std::to_string(k);
arm();
rc = dev->read(rdata, k);
disarm();
exp = "S ";
wb(exp, (A << 1) | 1);
rb(exp, rdata, k);
exp += "P ";
} else if (i < W8 + W16 + 16) {
static const uint8_t p1[] = {0x02};
size_t k = i - (W8 + W16 + 13) + 1;
label = "write1+read n=" + std::to_string(k);
arm();
rc = dev->write_read(p1, 1, rdata, k);
disarm();
exp = "S ";
wb(exp, A << 1);
wb(exp, 0x02);
exp += "Sr ";
wb(exp, (A << 1) | 1);
rb(exp, rdata, k);
exp += "P ";
} else if (i < W8 + W16 + 16 + 112) {
uint8_t a = 8 + (i - (W8 + W16 + 16));
label = "probe " + std::to_string(a);
arm();
rc = bus->write_readv(a, nullptr, 0, nullptr, 0);
disarm();
exp = "S ";
char t[8];
snprintf(t, sizeof t, "%02X%c ", a << 1, a == A ? '+' : '-');
exp += t;
exp += "P ";
} else {
done = true;
ESP_LOGI(TAG, "SNDONE cases=%u ok=%u diff=%u amb=%u alloc_calls=%u", (unsigned) idx - 1, (unsigned) nok,
(unsigned) ndiff, (unsigned) namb, (unsigned) nalloc);
return false;
}
uint32_t amb = 0;
#ifdef SNF_PDSLAVE
delayMicroseconds(500);
std::string got = pds::tokens();
ovf = pds::lovf;
n = pds::ln;
#else
std::string got = wiredecode::decode(buf, n, start, amb);
#endif
bool ok = got == exp && ovf == 0;
nok += ok;
ndiff += !ok;
namb += amb;
nalloc += alloc_count::count != 0;
ESP_LOGI(TAG, "SN %s|rc=%d|alloc=%u|ev=%u|amb=%u|ovf=%u|fnv=%08X|%s", label.c_str(), (int) rc,
(unsigned) alloc_count::count, (unsigned) n, (unsigned) amb, (unsigned) ovf, (unsigned) fnv1a_hash(got),
ok ? "OK" : "DIFF");
if (!ok) {
ESP_LOGI(TAG, "SNGOT %s", got.substr(0, 200).c_str());
ESP_LOGI(TAG, "SNEXP %s", exp.substr(0, 200).c_str());
}
App.feed_wdt();
return true;
}
#ifdef SNF_SW
static i2c::I2CBus *make_sw() {
auto *sda = new esp32::ESP32InternalGPIOPin();
sda->set_pin((gpio_num_t) SNF_SDA);
sda->set_flags(gpio::FLAG_INPUT | gpio::FLAG_PULLUP);
auto *scl = new esp32::ESP32InternalGPIOPin();
scl->set_pin((gpio_num_t) SNF_SCL);
scl->set_flags(gpio::FLAG_INPUT | gpio::FLAG_PULLUP);
auto *b = new i2c::SoftwareI2CBus();
b->set_sda_pin(sda);
b->set_scl_pin(scl);
b->set_frequency(SNF_SW_FREQ);
b->set_scan(true);
b->set_timeout(1000);
b->setup();
b->dump_config();
return b;
}
#endif
} // namespace snf
@@ -0,0 +1,81 @@
"""compare.py BASE PR: wire and return code per bus and case must match; summarize allocations and IDF job lists."""
from collections import Counter, defaultdict
import pathlib
import sys
sys.path.insert(0, str(pathlib.Path(__file__).resolve().parent.parent))
from snlog import first_alloc # noqa: E402
def load(p):
rec, jobs = {}, {}
with pathlib.Path(p).open(encoding="utf-8") as fh:
for line in fh:
line = line.rstrip("\n")
if line.startswith("ALLOC_PROBE"):
continue
parts = line.split("|")
if parts[0] == "J":
jobs[(parts[1], parts[2])] = parts[3].strip()
continue
bus, case, rc, alloc, wire = parts
rec[(bus, case)] = (rc, int(alloc.split("=")[1]), wire.strip())
return rec, jobs
b, bj = load(sys.argv[1])
p, pj = load(sys.argv[2])
assert b.keys() == p.keys(), "case lists differ"
diff = [k for k in b if b[k][0] != p[k][0] or b[k][2] != p[k][2]]
per_bus = Counter(k[0] for k in b)
print("cases per bus:", dict(per_bus))
print("wire/rc differences:", len(diff))
for k in diff[:40]:
print(
" DIFF",
k,
"\n base",
b[k][0],
b[k][2][:160],
"\n pr ",
p[k][0],
p[k][2][:160],
)
alloc = defaultdict(
lambda: [0, 0, 0, 0]
) # base cases with alloc, pr cases with alloc, base total, pr total
for k in b:
if not k[1].startswith(("wreg8", "wreg16")):
continue
a = alloc[k[0]]
a[0] += b[k][1] > 0
a[1] += p[k][1] > 0
a[2] += b[k][1]
a[3] += p[k][1]
print("write_register/16 calls that allocated (base, pr):")
for bus, a in alloc.items():
print(
f" {bus:8s} base {a[0]} calls ({a[2]} mallocs) pr {a[1]} calls ({a[3]} mallocs)"
)
# first length that allocates, per bus and shape
for bus in per_bus:
ab = {c: r[1] for (x, c), r in b.items() if x == bus}
ap = {c: r[1] for (x, c), r in p.items() if x == bus}
for shape in ("wreg8", "wreg16"):
fb, fp = first_alloc(ab, shape), first_alloc(ap, shape)
print(f" first allocating length {bus} {shape}: base {fb} pr {fp}")
other = [
k
for k in b
if not k[1].startswith(("wreg8", "wreg16", "wedge wreg", "absent wreg"))
and (b[k][1] or p[k][1])
]
print("other calls with allocations:", other[:10])
jd = Counter()
for k in bj:
if bj[k] != pj.get(k):
jd[(bj[k], pj.get(k))] += 1
print("IDF job list changes (base -> pr: count):")
for (x, y), n in sorted(jd.items(), key=lambda t: -t[1])[:20]:
print(f" {x!r} -> {y!r}: {n}")
@@ -0,0 +1,33 @@
"""extract.py TREE OUTDIR: copy the write_readv/transfer_ definitions of the Arduino and ESP-IDF buses out of TREE."""
from pathlib import Path
import re
import shutil
import sys
tree, out = Path(sys.argv[1]), Path(sys.argv[2])
for cls, src, dst in (
("ArduinoI2CBus", "i2c_bus_arduino.cpp", "wab_gen_arduino.inc"),
("IDFI2CBus", "i2c_bus_esp_idf.cpp", "wab_gen_idf.inc"),
):
lines = (tree / "esphome/components/i2c" / src).read_text().splitlines()
pat = re.compile(rf"^ErrorCode {cls}::(write_readv|transfer_)\(")
starts = [i for i, line in enumerate(lines) if pat.match(line)]
assert len(starts) == 1, (src, starts)
s = starts[0]
e = next(i for i in range(s, len(lines)) if lines[i] == "}")
(out / dst).write_text("\n".join(lines[s : e + 1]) + "\n")
print(dst, lines[s].split("(")[0], e - s + 1, "lines")
shutil.copy(
tree / "esphome/components/i2c/i2c_bus_software.cpp", out / "wab_sw_impl.inc"
)
shutil.copy(
tree / "esphome/components/i2c/i2c_bus_software.h", out / "i2c_bus_software.h"
)
h = out / "i2c_bus_software.h"
h.write_text(
h.read_text().replace(
'#include "i2c_bus.h"', '#include "esphome/components/i2c/i2c_bus.h"'
)
)
@@ -0,0 +1,22 @@
#!/bin/bash
# run.sh TREE OUTFILE: copy the harness into TREE's tests/components/i2c, generate the extracted bus bodies, run the
# unit test and remove the harness again. env: PY (default TREE's venv python)
T=$(cd "${1:?tree}" && pwd); O=$(cd "$(dirname "${2:?out}")" && pwd)/$(basename "$2")
H=$(cd "$(dirname "$0")" && pwd); PY=${PY:-$T/venv/bin/python}
D=$T/tests/components/i2c
# The sources are stored as .cpp.example, .h.example and .hpp so the repo's C++ lints skip them; copy them back.
FILES=(wab_common.h wab_ard8266.cpp wab_ardpico.cpp wab_idf.cpp wab_sw.cpp test_wab.cpp benchmark.yaml)
G=$(mktemp -d)
cleanup() { for f in "${FILES[@]}"; do rm -f "$D/$f"; done; rm -rf "$G"; }
trap cleanup EXIT
trap 'exit 130' INT TERM
cp "$H/wab_common.hpp" "$D/wab_common.h"
for f in "${FILES[@]:1:5}"; do cp "$H/$f.example" "$D/$f"; done
mkdir -p "$G/gen" "$G/stubs/driver"
for f in Arduino.h Wire.h driver/i2c_master.h; do cp "$H/stub/$f.example" "$G/stubs/$f"; done
$PY "$H/extract.py" "$T" "$G/gen"
printf "esphome:\n build_flags:\n - -I%s\n - -I%s\n - -I%s\n" "$D" "$G/stubs" "$G/gen" > "$D/benchmark.yaml"
(cd "$T" && WAB_OUT=$O PYTHONPATH=$T $PY script/cpp_unit_test.py i2c) > "${O%.txt}-build.log" 2>&1
rc=$?
echo "rc=$rc"; tail -5 "${O%.txt}-build.log"
exit $rc
@@ -0,0 +1,10 @@
#pragma once
#include <cstdint>
#define INPUT 0x01
#define OUTPUT 0x03
#define INPUT_PULLUP 0x05
#define LOW 0
#define HIGH 1
inline void pinMode(uint8_t, uint8_t) {}
inline void digitalWrite(uint8_t, uint8_t) {}
inline int digitalRead(uint8_t) { return HIGH; }
@@ -0,0 +1,207 @@
#pragma once
// TwoWire models of the cores ESPHome uses, with the cores' own buffer rules. Select with WAB_WIRE_ESP8266 or
// WAB_WIRE_PICO before including. Bytes go on the wire through wab::wire() with ACKs from wab::slave().
#include <cstddef>
#include <cstdint>
#include "wab_common.h"
#if defined(WAB_WIRE_ESP8266)
#define TwoWire TwoWire8266
#define Wire Wire8266
// esp8266 core 3.1.2-esphome.3 libraries/Wire: I2C_BUFFER_LENGTH 128
class TwoWire {
public:
static constexpr size_t LEN = 128;
void begin(int, int) {}
void setClock(uint32_t) {}
void setClockStretchLimit(uint32_t) {}
void beginTransmission(uint8_t address) {
transmitting = 1;
txAddress = address;
txBufferIndex = 0;
txBufferLength = 0;
}
size_t write(uint8_t data) {
if (transmitting) {
if (txBufferLength >= LEN)
return 0; // setWriteError()
txBuffer[txBufferIndex] = data;
++txBufferIndex;
txBufferLength = txBufferIndex;
}
return 1;
}
size_t write(const uint8_t *data, size_t quantity) {
if (transmitting) {
for (size_t i = 0; i < quantity; ++i)
if (!write(data[i]))
return i;
}
return quantity;
}
uint8_t endTransmission(uint8_t sendStop) {
// twi_writeTo: 0 ok, 2 address NACK, 3 data NACK; STOP when sendStop (also after a NACK)
int8_t ret = 0;
auto &w = wab::wire();
auto &s = wab::slave();
w.start();
uint8_t ab = txAddress << 1;
bool ack = s.address(ab);
w.wbyte(ab, ack);
if (!ack) {
ret = 2;
} else {
for (size_t i = 0; i < txBufferLength; i++) {
bool a = s.data(txBuffer[i]);
w.wbyte(txBuffer[i], a);
if (!a) {
ret = 3;
break;
}
}
}
if (sendStop || ret != 0)
w.stop();
txBufferIndex = 0;
txBufferLength = 0;
transmitting = 0;
return ret;
}
size_t requestFrom(uint8_t address, size_t size, bool sendStop) {
if (size > LEN)
size = LEN;
auto &w = wab::wire();
auto &s = wab::slave();
w.start();
uint8_t ab = (address << 1) | 1;
bool ack = s.address(ab);
w.wbyte(ab, ack);
size_t read = 0;
if (ack) {
for (size_t i = 0; i < size; i++) {
rxBuffer[i] = s.next_read();
w.rbyte(rxBuffer[i], i + 1 < size);
}
read = size;
}
if (sendStop || !ack)
w.stop();
rxBufferIndex = 0;
rxBufferLength = read;
return read;
}
int read() { return rxBufferIndex < rxBufferLength ? rxBuffer[rxBufferIndex++] : -1; }
void wab_reset() { transmitting = 0; txBufferLength = txBufferIndex = 0; }
uint8_t transmitting{0};
uint8_t txAddress{0};
size_t txBufferIndex{0}, txBufferLength{0}, rxBufferIndex{0}, rxBufferLength{0};
uint8_t txBuffer[LEN]{}, rxBuffer[LEN]{};
};
extern TwoWire Wire;
#elif defined(WAB_WIRE_PICO)
#define TwoWire TwoWirePico
#define Wire WirePico
#define Wire1 Wire1Pico
// arduino-pico 6.3.2 libraries/Wire: WIRE_BUFFER_SIZE 256, one buffer for TX and RX
#ifndef WIRE_BUFFER_SIZE
#define WIRE_BUFFER_SIZE 256
#endif
class TwoWire {
public:
static constexpr size_t LEN = WIRE_BUFFER_SIZE;
void setSDA(int) {}
void setSCL(int) {}
void begin() {}
void setClock(uint32_t) {}
void setTimeout(uint32_t) {}
void beginTransmission(uint8_t addr) {
if (!_running || _txBegun)
return; // ERROR
_addr = addr;
_buffLen = 0;
_buffOff = 0;
_txBegun = true;
}
size_t write(uint8_t ucData) {
if (!_running)
return 0;
if (!_txBegun || (_buffLen == int(LEN)))
return 0;
_buff[_buffLen++] = ucData;
return 1;
}
size_t write(const uint8_t *data, size_t quantity) {
for (size_t i = 0; i < quantity; ++i)
if (!write(data[i]))
return i;
return quantity;
}
uint8_t endTransmission(bool stopBit) {
if (!_running || !_txBegun)
return 4;
_txBegun = false;
auto &w = wab::wire();
auto &s = wab::slave();
if (!_buffLen) {
// _probe(): its own START, address, STOP
w.start();
uint8_t ab = _addr << 1;
bool ack = s.address(ab);
w.wbyte(ab, ack);
w.stop();
return ack ? 0 : 2;
}
// i2c_write_blocking_until: abort (with STOP) on any NACK, returns PICO_ERROR_GENERIC
w.start();
uint8_t ab = _addr << 1;
bool ok = s.address(ab);
w.wbyte(ab, ok);
for (int i = 0; ok && i < _buffLen; i++) {
ok = s.data(_buff[i]);
w.wbyte(_buff[i], ok);
}
if (stopBit || !ok)
w.stop();
_buffLen = 0;
return ok ? 0 : 4;
}
size_t requestFrom(uint8_t address, size_t quantity, bool stopBit) {
if (!_running || _txBegun || !quantity || (quantity > LEN))
return 0;
auto &w = wab::wire();
auto &s = wab::slave();
w.start();
uint8_t ab = (address << 1) | 1;
bool ack = s.address(ab);
w.wbyte(ab, ack);
_buffOff = 0;
if (!ack) {
w.stop();
_buffLen = 0;
return 0;
}
for (size_t i = 0; i < quantity; i++) {
_buff[i] = s.next_read();
w.rbyte(_buff[i], i + 1 < quantity);
}
if (stopBit)
w.stop();
_buffLen = quantity;
return quantity;
}
int read() { return _buffOff < _buffLen ? _buff[_buffOff++] : -1; }
void wab_reset() { _txBegun = false; _buffLen = _buffOff = 0; }
bool _running{true};
bool _txBegun{false};
uint8_t _addr{0};
int _buffLen{0}, _buffOff{0};
uint8_t _buff[LEN]{};
};
extern TwoWire Wire;
extern TwoWire Wire1;
#else
#error "select a Wire model"
#endif
@@ -0,0 +1,39 @@
#pragma once
// Types of ESP-IDF 5.5.5 esp_driver_i2c used by i2c_bus_esp_idf, with an execute model that puts the jobs on the
// wab wire and applies the driver's own argument checks (command count, NACK before STOP).
#include <cstddef>
#include <cstdint>
typedef int esp_err_t;
#define ESP_OK 0
#define ESP_FAIL -1
#define ESP_ERR_INVALID_ARG 0x102
#define ESP_ERR_INVALID_STATE 0x103
#define ESP_ERR_INVALID_RESPONSE 0x108
#define ESP_ERR_TIMEOUT 0x107
inline const char *esp_err_to_name(esp_err_t) { return "err"; }
typedef int i2c_port_t;
typedef enum { I2C_MASTER_WRITE = 0, I2C_MASTER_READ } i2c_rw_t;
typedef enum { I2C_ACK_VAL = 0, I2C_NACK_VAL = 1 } i2c_ack_value_t;
typedef enum { I2C_MASTER_CMD_START, I2C_MASTER_CMD_WRITE, I2C_MASTER_CMD_READ, I2C_MASTER_CMD_STOP } i2c_master_command_t;
typedef struct i2c_master_bus_t *i2c_master_bus_handle_t;
typedef struct i2c_master_dev_t *i2c_master_dev_handle_t;
typedef struct {
i2c_master_command_t command;
union {
struct {
bool ack_check;
uint8_t *data;
size_t total_bytes;
} write;
struct {
i2c_ack_value_t ack_value;
uint8_t *data;
size_t total_bytes;
} read;
};
} i2c_operation_job_t;
#ifndef SOC_I2C_CMD_REG_NUM
#define SOC_I2C_CMD_REG_NUM 8 // C2, C3, C6, S3, P4; 16 on ESP32 and S2
#endif
esp_err_t i2c_master_execute_defined_operations(i2c_master_dev_handle_t dev, i2c_operation_job_t *ops, size_t n,
int timeout_ms);
@@ -0,0 +1,208 @@
// Drives every I2CDevice call shape through each bus model and writes one line per call to $WAB_OUT.
#include <gtest/gtest.h>
#include <cstdint>
#include <cstdlib>
#include <fstream>
#include <functional>
#include <vector>
#ifndef __has_feature
#define __has_feature(x) 0
#endif
#if defined(__SANITIZE_ADDRESS__) || __has_feature(address_sanitizer)
#define WAB_ASAN 1
extern "C" int __sanitizer_install_malloc_and_free_hooks(void (*malloc_hook)(const volatile void *, size_t),
void (*free_hook)(const volatile void *));
#endif
#include "esphome/components/i2c/i2c.h"
#include "wab_common.h"
esphome::i2c::I2CBus *wab_make_esp8266();
esphome::i2c::I2CBus *wab_make_pico();
esphome::i2c::I2CBus *wab_make_idf();
esphome::i2c::I2CBus *wab_make_sw();
void wab_reset_esp8266();
void wab_reset_pico();
void wab_reset_sw();
namespace wab {
static volatile bool g_window = false;
static volatile size_t g_allocs = 0;
#ifdef WAB_ASAN
static void on_malloc(const volatile void *, size_t) {
if (g_window)
g_allocs = g_allocs + 1;
}
static void on_free(const volatile void *) {}
static bool install() {
__sanitizer_install_malloc_and_free_hooks(on_malloc, on_free);
return true;
}
#else
typedef void(malloc_logger_t)(uint32_t type, uintptr_t arg1, uintptr_t arg2, uintptr_t arg3, uintptr_t result,
uint32_t num_hot_frames_to_skip);
extern "C" malloc_logger_t *malloc_logger;
static void on_log(uint32_t type, uintptr_t, uintptr_t, uintptr_t, uintptr_t, uint32_t) {
// MALLOC_LOG_TYPE_ALLOCATE = 2
if (g_window && (type & 2))
g_allocs = g_allocs + 1;
}
static bool install() {
malloc_logger = on_log;
return true;
}
#endif
static bool g_installed = install();
void alloc_window(bool on) {
if (on)
g_allocs = 0;
g_window = on;
}
size_t alloc_count() { return g_allocs; }
} // namespace wab
namespace esphome::i2c::testing {
// Zephyr, host and TCA9548A keep the default write_prefixed(): this bus only has write_readv(), like them
class DefaultPathBus : public I2CBus {
public:
ErrorCode write_readv(uint8_t address, const uint8_t *wb, size_t wc, uint8_t *rb, size_t rc) override {
auto &w = wab::wire();
auto &s = wab::slave();
if (wc != 0 || rc == 0) {
w.start();
bool ok = s.address(address << 1);
w.wbyte(address << 1, ok);
for (size_t i = 0; ok && i < wc; i++) {
ok = s.data(wb[i]);
w.wbyte(wb[i], ok);
}
if (!ok) {
w.stop();
return ERROR_NOT_ACKNOWLEDGED;
}
if (rc == 0) {
w.stop();
return ERROR_OK;
}
}
w.start();
bool ok = s.address((address << 1) | 1);
w.wbyte((address << 1) | 1, ok);
for (size_t i = 0; ok && i < rc; i++) {
rb[i] = s.next_read();
w.rbyte(rb[i], i + 1 < rc);
}
w.stop();
return ok ? ERROR_OK : ERROR_NOT_ACKNOWLEDGED;
}
};
static std::vector<uint8_t> pattern(size_t len, uint8_t seed) {
std::vector<uint8_t> v(len);
for (size_t i = 0; i < len; i++)
v[i] = static_cast<uint8_t>(seed + i * 7);
return v;
}
TEST(WireAB, Record) {
const char *out = std::getenv("WAB_OUT");
ASSERT_NE(out, nullptr);
std::ofstream f(out);
// Allocation counter sanity: a malloc inside the window must count
wab::alloc_window(true);
void *volatile probe = malloc(100);
wab::alloc_window(false);
free(probe);
f << "ALLOC_PROBE " << wab::alloc_count() << "\n";
ASSERT_EQ(wab::alloc_count(), 1u);
struct Bus {
const char *name;
I2CBus *bus;
std::function<void()> reset;
};
std::vector<Bus> buses = {
{"esp8266", wab_make_esp8266(), wab_reset_esp8266},
{"pico", wab_make_pico(), wab_reset_pico},
{"idf", wab_make_idf(), [] {}},
{"sw", wab_make_sw(), wab_reset_sw},
{"default", new DefaultPathBus(), [] {}},
};
for (auto &b : buses) {
I2CDevice dev;
dev.set_i2c_bus(b.bus);
auto run = [&](const std::string &label, const std::function<ErrorCode()> &fn, bool reset = true) {
if (reset)
b.reset();
wab::wire().clear();
wab::jobs_note().clear();
// the recorder must not allocate inside the window
wab::wire().text.reserve(16384);
wab::jobs_note().reserve(1024);
wab::alloc_window(true);
ErrorCode rc = fn();
wab::alloc_window(false);
f << b.name << "|" << label << "|rc=" << rc << "|alloc=" << wab::alloc_count() << "|" << wab::wire().text
<< "\n";
if (!wab::jobs_note().empty())
f << "J|" << b.name << "|" << label << "|" << wab::jobs_note() << "\n";
};
auto &sl = wab::slave();
sl.ack_limit = SIZE_MAX;
dev.set_i2c_address(0x48);
for (size_t len = 0; len <= 300; len++) {
auto d = pattern(len, 0x11);
run("wreg8 len=" + std::to_string(len), [&] { return dev.write_register(0x02, d.data(), d.size()); });
run("wreg16 len=" + std::to_string(len), [&] { return dev.write_register16(0xBEEF, d.data(), d.size()); });
}
// nullptr data with len 0, as a caller with no payload may pass
run("wreg8 null len=0", [&] { return dev.write_register(0x03, nullptr, 0); });
run("wreg16 null len=0", [&] { return dev.write_register16(0x0304, nullptr, 0); });
// slave NACKs after k data bytes (k counts the register byte)
for (size_t k = 0; k <= 6; k++) {
sl.ack_limit = k;
auto d = pattern(4, 0x40);
run("wreg8 len=4 acklimit=" + std::to_string(k), [&] { return dev.write_register(0x05, d.data(), d.size()); });
run("wreg16 len=4 acklimit=" + std::to_string(k),
[&] { return dev.write_register16(0x0506, d.data(), d.size()); });
}
sl.ack_limit = SIZE_MAX;
// reads, write+read with repeated start, plain writes, probes
for (size_t n : {1, 2, 3, 16, 32}) {
std::vector<uint8_t> r(n);
run("rreg8 n=" + std::to_string(n), [&] { return dev.read_register(0x01, r.data(), n); });
run("rreg16 n=" + std::to_string(n), [&] { return dev.read_register16(0x0102, r.data(), n); });
run("read n=" + std::to_string(n), [&] { return dev.read(r.data(), n); });
auto w3 = pattern(3, 0x70);
run("wr 3+" + std::to_string(n), [&] { return dev.write_read(w3.data(), 3, r.data(), n); });
}
for (size_t len : {0, 1, 5, 127, 128, 129, 255, 256, 257}) {
auto d = pattern(len, 0x22);
run("write len=" + std::to_string(len), [&] { return dev.write(d.data(), d.size()); });
}
// a normal transaction right after an over-long write, without resetting the Wire model in between
for (size_t len : {200, 300}) {
auto d = pattern(len, 0x33);
run("wedge wreg8 len=" + std::to_string(len), [&] { return dev.write_register(0x02, d.data(), d.size()); });
std::vector<uint8_t> r(2);
run("wedge then rreg8 n=2", [&] { return dev.read_register(0x01, r.data(), 2); }, false);
auto s = pattern(2, 0x44);
run("wedge then wreg8 len=2", [&] { return dev.write_register(0x02, s.data(), 2); }, false);
}
// absent device
dev.set_i2c_address(0x50);
for (size_t len : {0, 1, 5}) {
auto d = pattern(len, 0x55);
run("absent wreg8 len=" + std::to_string(len), [&] { return dev.write_register(0x02, d.data(), d.size()); });
run("absent wreg16 len=" + std::to_string(len),
[&] { return dev.write_register16(0x0203, d.data(), d.size()); });
}
// scan probe over the whole range, as i2c_scan_() does
for (uint8_t a = 8; a != 120; a++) {
run("probe " + std::to_string(a), [&] { return b.bus->write_readv(a, nullptr, 0, nullptr, 0); });
}
}
}
} // namespace esphome::i2c::testing
@@ -0,0 +1,34 @@
// Arduino bus body of the tree under test on the esp8266 core Wire model
#include "esphome/core/application.h"
#include "esphome/core/component.h"
#include "esphome/core/hal.h"
#include "esphome/core/helpers.h"
#include "esphome/core/log.h"
#include "esphome/components/i2c/i2c_bus.h"
#define WAB_WIRE_ESP8266
#define USE_ARDUINO
#define USE_ESP8266
#define ArduinoI2CBus ArduinoI2CBus8266
#include "esphome/components/i2c/i2c_bus_arduino.h"
TwoWire Wire;
namespace esphome::i2c {
ESPHOME_LOG_TAG(TAG, "i2c");
static constexpr size_t I2C_MAX_LOG_BYTES = 32;
void ArduinoI2CBus::setup() {}
void ArduinoI2CBus::dump_config() {}
void ArduinoI2CBus::set_pins_and_clock_() {}
#include "wab_gen_arduino.inc"
} // namespace esphome::i2c
#undef USE_ESP8266
#undef USE_ARDUINO
struct A8Init { using type = bool esphome::i2c::ArduinoI2CBus::*; friend type get(A8Init); };
struct A8Wire { using type = TwoWire *esphome::i2c::ArduinoI2CBus::*; friend type get(A8Wire); };
template struct WabRob<A8Init, &esphome::i2c::ArduinoI2CBus::initialized_>;
template struct WabRob<A8Wire, &esphome::i2c::ArduinoI2CBus::wire_>;
esphome::i2c::I2CBus *wab_make_esp8266() {
auto *b = new esphome::i2c::ArduinoI2CBus();
b->*get(A8Init()) = true;
b->*get(A8Wire()) = &Wire;
return b;
}
void wab_reset_esp8266() { Wire.wab_reset(); }
@@ -0,0 +1,35 @@
// Arduino bus body of the tree under test on the arduino-pico Wire model
#include "esphome/core/application.h"
#include "esphome/core/component.h"
#include "esphome/core/hal.h"
#include "esphome/core/helpers.h"
#include "esphome/core/log.h"
#include "esphome/components/i2c/i2c_bus.h"
#define WAB_WIRE_PICO
#define USE_ARDUINO
#define USE_RP2
#define ArduinoI2CBus ArduinoI2CBusPico
#include "esphome/components/i2c/i2c_bus_arduino.h"
TwoWire Wire;
TwoWire Wire1;
namespace esphome::i2c {
ESPHOME_LOG_TAG(TAG, "i2c");
static constexpr size_t I2C_MAX_LOG_BYTES = 32;
void ArduinoI2CBus::setup() {}
void ArduinoI2CBus::dump_config() {}
void ArduinoI2CBus::set_pins_and_clock_() {}
#include "wab_gen_arduino.inc"
} // namespace esphome::i2c
#undef USE_RP2
#undef USE_ARDUINO
struct APInit { using type = bool esphome::i2c::ArduinoI2CBus::*; friend type get(APInit); };
struct APWire { using type = TwoWire *esphome::i2c::ArduinoI2CBus::*; friend type get(APWire); };
template struct WabRob<APInit, &esphome::i2c::ArduinoI2CBus::initialized_>;
template struct WabRob<APWire, &esphome::i2c::ArduinoI2CBus::wire_>;
esphome::i2c::I2CBus *wab_make_pico() {
auto *b = new esphome::i2c::ArduinoI2CBus();
b->*get(APInit()) = true;
b->*get(APWire()) = &Wire;
return b;
}
void wab_reset_pico() { Wire.wab_reset(); }
@@ -0,0 +1,87 @@
#pragma once
// Wire-level A/B harness for esphome#22780: every bus model emits the same token stream.
#include <cstddef>
#include <cstdint>
#include <cstdio>
#include <string>
#include <vector>
namespace wab {
// Token stream of the bus: "S", "Sr", "P", "AB+" (written byte, slave ACK), "AB-" (NACK), "rAB+"/"rAB-" (read byte,
// master ACK/NACK), "ABORT".
struct Wire {
std::string text;
bool open{false};
void start() {
text += open ? "Sr " : "S ";
open = true;
}
void stop() {
text += "P ";
open = false;
}
void wbyte(uint8_t b, bool ack) {
char t[8];
snprintf(t, sizeof t, "%02X%c ", b, ack ? '+' : '-');
text += t;
}
void rbyte(uint8_t b, bool ack) {
char t[8];
snprintf(t, sizeof t, "r%02X%c ", b, ack ? '+' : '-');
text += t;
}
void note(const char *s) {
text += s;
text += ' ';
}
void clear() {
text.clear();
open = false;
}
};
inline Wire &wire() {
static Wire w;
return w;
}
inline std::string &jobs_note() {
static std::string s;
return s;
}
// Slave model: present addresses ACK their address and up to ack_limit data bytes after it.
struct Slave {
uint8_t present[4]{0x48, 0x1A, 0, 0};
size_t ack_limit{SIZE_MAX};
size_t written{0}; // data bytes written since the address byte
size_t read_idx{0};
bool is_present(uint8_t addr7) const {
for (auto p : present)
if (p != 0 && p == addr7)
return true;
return false;
}
// Called for the address byte; returns ACK
bool address(uint8_t byte) {
this->written = 0;
this->read_idx = 0;
return this->is_present(byte >> 1);
}
bool data(uint8_t) { return this->written++ < this->ack_limit; }
uint8_t next_read() { return static_cast<uint8_t>(0xA0 + this->read_idx++); }
};
inline Slave &slave() {
static Slave s;
return s;
}
// Allocation counter (window opened around one bus call)
void alloc_window(bool on);
size_t alloc_count();
} // namespace wab
// Member access to private fields of final classes (explicit instantiation ignores access checks)
template<typename Tag, typename Tag::type M> struct WabRob {
friend typename Tag::type get(Tag) { return M; }
};
@@ -0,0 +1,88 @@
// ESP-IDF bus body of the tree under test on the IDF 5.5.5 job model
#include <string>
#include "esphome/core/application.h"
#include "esphome/core/component.h"
#include "esphome/core/hal.h"
#include "esphome/core/helpers.h"
#include "esphome/core/log.h"
#include "esphome/components/i2c/i2c_bus.h"
#include "wab_common.h"
#define USE_ESP32
#include "esphome/components/i2c/i2c_bus_esp_idf.h"
namespace esphome::i2c {
ESPHOME_LOG_TAG(TAG, "i2c.idf");
static constexpr size_t I2C_MAX_LOG_BYTES = 32;
void IDFI2CBus::setup() {}
void IDFI2CBus::dump_config() {}
#include "wab_gen_idf.inc"
} // namespace esphome::i2c
#undef USE_ESP32
esp_err_t i2c_master_execute_defined_operations(i2c_master_dev_handle_t, i2c_operation_job_t *ops, size_t n, int) {
auto &jn = wab::jobs_note();
jn.clear();
for (size_t i = 0; i < n; i++) {
char t[16];
switch (ops[i].command) {
case I2C_MASTER_CMD_START: jn += "S "; break;
case I2C_MASTER_CMD_STOP: jn += "P "; break;
case I2C_MASTER_CMD_WRITE: snprintf(t, sizeof t, "W%zu ", ops[i].write.total_bytes); jn += t; break;
case I2C_MASTER_CMD_READ: snprintf(t, sizeof t, "R%zu%s ", ops[i].read.total_bytes,
ops[i].read.ack_value == I2C_NACK_VAL ? "n" : "a"); jn += t; break;
}
}
auto &w = wab::wire();
auto &s = wab::slave();
if (n > SOC_I2C_CMD_REG_NUM) {
w.note("INVALID_ARG_TOO_MANY_JOBS");
return ESP_ERR_INVALID_ARG;
}
for (size_t i = 0; i + 1 < n; i++) {
if (ops[i].command == I2C_MASTER_CMD_READ && ops[i + 1].command == I2C_MASTER_CMD_STOP &&
ops[i].read.ack_value != I2C_NACK_VAL) {
w.note("INVALID_ARG_ACK_BEFORE_STOP");
return ESP_ERR_INVALID_ARG;
}
}
bool expect_addr = false;
for (size_t i = 0; i < n; i++) {
auto &op = ops[i];
switch (op.command) {
case I2C_MASTER_CMD_START:
w.start();
expect_addr = true;
break;
case I2C_MASTER_CMD_STOP:
w.stop();
break;
case I2C_MASTER_CMD_WRITE:
for (size_t k = 0; k < op.write.total_bytes; k++) {
uint8_t b = op.write.data[k];
bool ack = expect_addr ? s.address(b) : s.data(b);
expect_addr = false;
w.wbyte(b, ack);
if (!ack && op.write.ack_check) {
w.note("ABORT");
w.stop();
return ESP_ERR_INVALID_STATE;
}
}
break;
case I2C_MASTER_CMD_READ:
for (size_t k = 0; k < op.read.total_bytes; k++) {
op.read.data[k] = s.next_read();
w.rbyte(op.read.data[k], op.read.ack_value == I2C_ACK_VAL);
}
break;
}
}
return ESP_OK;
}
struct IdfInit { using type = bool esphome::i2c::IDFI2CBus::*; friend type get(IdfInit); };
template struct WabRob<IdfInit, &esphome::i2c::IDFI2CBus::initialized_>;
esphome::i2c::I2CBus *wab_make_idf() {
auto *b = new esphome::i2c::IDFI2CBus();
b->*get(IdfInit()) = true;
return b;
}
@@ -0,0 +1,156 @@
// The software bus of the tree under test, whole, on fake open-drain pins with a bit-level slave
#include "esphome/core/application.h"
#include "esphome/core/component.h"
#include "esphome/core/gpio.h"
#include "esphome/core/hal.h"
#include "esphome/core/helpers.h"
#include "esphome/core/log.h"
#include "esphome/components/i2c/i2c_bus.h"
#include "wab_common.h"
namespace wab {
struct BitSlave {
bool m_sda_low{false}, m_scl_low{false}, s_sda_low{false};
bool sda{true}, scl{true};
enum { IDLE, ADDR, WDATA, RDATA, IGNORE } state{IDLE};
enum { BITS, ACK_OUT, ACK_IN } phase{BITS};
int bits{0};
uint8_t shift{0}, cur{0};
bool last_ack{false}, is_read{false}, master_ack{false};
void drive_bit() { this->s_sda_low = !(this->cur & (0x80 >> this->bits)); }
void update() {
auto &w = wire();
auto &sl = slave();
for (int guard = 0; guard < 4; guard++) {
bool nsda = !(this->m_sda_low || this->s_sda_low), nscl = !this->m_scl_low;
if (nsda == this->sda && nscl == this->scl)
return;
bool sda_fell = this->sda && !nsda, sda_rose = !this->sda && nsda;
bool scl_rose = !this->scl && nscl, scl_fell = this->scl && !nscl;
this->sda = nsda;
this->scl = nscl;
if (this->scl && !scl_rose && sda_fell) { // START
w.start();
this->state = ADDR;
this->phase = BITS;
this->bits = 0;
this->shift = 0;
this->s_sda_low = false;
continue;
}
if (this->scl && !scl_rose && sda_rose) { // STOP
if (w.open)
w.stop();
this->state = IDLE;
this->s_sda_low = false;
continue;
}
if (scl_rose) {
if ((this->state == ADDR || this->state == WDATA) && this->phase == BITS) {
this->shift = (this->shift << 1) | (this->sda ? 1 : 0);
this->bits++;
} else if (this->state == RDATA && this->phase == ACK_IN) {
this->master_ack = !this->sda;
w.rbyte(this->cur, this->master_ack);
}
} else if (scl_fell) {
if ((this->state == ADDR || this->state == WDATA) && this->phase == BITS && this->bits == 8) {
if (this->state == ADDR) {
this->last_ack = sl.address(this->shift);
this->is_read = this->shift & 1;
} else {
this->last_ack = sl.data(this->shift);
}
w.wbyte(this->shift, this->last_ack);
this->s_sda_low = this->last_ack;
this->phase = ACK_OUT;
} else if (this->phase == ACK_OUT) {
this->s_sda_low = false;
this->bits = 0;
this->shift = 0;
this->phase = BITS;
if (!this->last_ack) {
this->state = IGNORE;
} else if (this->state == ADDR && this->is_read) {
this->state = RDATA;
this->cur = sl.next_read();
this->drive_bit();
} else {
this->state = WDATA;
}
} else if (this->state == RDATA && this->phase == BITS) {
this->bits++;
if (this->bits == 8) {
this->s_sda_low = false;
this->phase = ACK_IN;
} else {
this->drive_bit();
}
} else if (this->state == RDATA && this->phase == ACK_IN) {
if (this->master_ack) {
this->cur = sl.next_read();
this->bits = 0;
this->phase = BITS;
this->drive_bit();
} else {
this->state = IGNORE;
}
}
}
}
}
};
inline BitSlave &bitslave() {
static BitSlave b;
return b;
}
class FakePin : public esphome::GPIOPin {
public:
explicit FakePin(bool is_sda) : is_sda_(is_sda) {}
void setup() override {}
void pin_mode(esphome::gpio::Flags flags) override {
this->flags_ = flags;
this->apply_();
}
esphome::gpio::Flags get_flags() const override { return this->flags_; }
bool digital_read() override { return this->is_sda_ ? bitslave().sda : bitslave().scl; }
void digital_write(bool value) override {
this->value_ = value;
this->apply_();
}
size_t dump_summary(char *buffer, size_t len) const override { return 0; }
protected:
void apply_() {
bool low = (this->flags_ & esphome::gpio::FLAG_OUTPUT) && !this->value_;
(this->is_sda_ ? bitslave().m_sda_low : bitslave().m_scl_low) = low;
bitslave().update();
}
bool is_sda_;
bool value_{true};
esphome::gpio::Flags flags_{esphome::gpio::FLAG_INPUT};
};
} // namespace wab
#define USE_LIBRETINY
#define InternalGPIOPin wab::FakePin
#include "wab_sw_impl.inc"
#undef InternalGPIOPin
#undef USE_LIBRETINY
esphome::i2c::I2CBus *wab_make_sw() {
static wab::FakePin sda(true), scl(false);
auto *b = new esphome::i2c::SoftwareI2CBus();
b->set_sda_pin(&sda);
b->set_scl_pin(&scl);
b->set_frequency(1000000);
b->set_scan(false);
b->setup();
return b;
}
void wab_reset_sw() {
auto &b = wab::bitslave();
b.state = wab::BitSlave::IDLE;
b.s_sda_low = false;
}
@@ -0,0 +1,74 @@
"""Readers for the sweep lines of dev/snf.hpp (SN) and ard/swp.hpp (SW), shared by the comparers."""
from collections.abc import Mapping
import pathlib
import re
ANSI = re.compile(r"\x1b\[[0-9;]*m")
FIELDS = r"\|rc=(-?\d+)\|alloc=(\d+)\|ev=\d+\|amb=\d+\|ovf=\d+\|fnv=([0-9A-F]+)\|(.*)$"
CAPTURES = ("", "2", "3")
def load(path: str | pathlib.Path, tag: str = "SN") -> dict[str, dict]:
"""Case label -> rc, alloc, fnv and the rest of the line.
SN lines get `verdict` (OK or DIFF) and, on a DIFF, the `got` and `exp` token lists of the SNGOT and SNEXP lines
that follow. SW lines get `wire` (the decoded stream) and `rd` (read data).
"""
line_re = re.compile(rf"{tag} (.+?){FIELDS}")
cases: dict[str, dict] = {}
cur: dict | None = None
text = pathlib.Path(path).read_text(encoding="utf-8", errors="replace")
for line in text.splitlines():
line = ANSI.sub("", line).rstrip()
if m := line_re.search(line):
label, rc, alloc, fnv, rest = m.groups()
cur = cases[label] = {"rc": int(rc), "alloc": int(alloc), "fnv": fnv}
if tag == "SW":
wire, _, rd = rest.partition("|rd=")
cur.update(wire=wire.strip(), rd=rd.strip())
else:
cur["verdict"] = rest
elif cur is not None and (m := re.search(r"SN(GOT|EXP) (.*)$", line)):
cur["got" if m.group(1) == "GOT" else "exp"] = m.group(2).split()
return cases
def is_read(label: str) -> bool:
"""Reads return chip data, so their wire hash changes between runs."""
return label.startswith(("rreg", "read", "write1+read"))
def nack_cut(got: list[str], exp: list[str]) -> int | None:
"""Index of the token the chip NACKed when `got` is `exp` cut there (then STOP), else None.
Only address and write tokens count as a chip NACK; a read token ("r..") NACKed early stays a difference.
"""
j = next(
(j for j, t in enumerate(got) if t.endswith("-") and not t.startswith("r")),
None,
)
if (
j is not None
and got[:j] == exp[:j]
and got[j][:-1] == exp[j][:-1]
and got[j + 1 :] == ["P"]
):
return j
return None
def runs(name: str) -> list[dict[str, dict]]:
"""The captures NAME-ser.txt, NAME-ser2.txt and NAME-ser3.txt that exist (at least one)."""
found = [pathlib.Path(f"{name}-ser{s}.txt") for s in CAPTURES]
loaded = [load(p) for p in found if p.exists()]
if not loaded:
raise SystemExit(f"no capture {name}-ser.txt")
return loaded
def first_alloc(allocs: Mapping[str, int], shape: str, limit: int = 400) -> int | None:
"""First length of `shape` (wreg8, wreg16) whose call allocated; allocs maps case label -> malloc count."""
return next(
(n for n in range(limit) if allocs.get(f"{shape} len={n}", 0) > 0), None
)
@@ -0,0 +1,51 @@
#pragma once
// Wire decoder shared by dev/snf.hpp and ard/swp.hpp. Each sample is a level state: SDA in bit 0, SCL in bit 1.
#include <cstdint>
#include <cstdio>
#include <string>
namespace wiredecode {
// Decodes `count` level states that follow `start` into tokens (`S`, `Sr`, `P`, `90+`, `rA0-`); adds to amb the
// samples where both lines changed at once.
inline std::string decode(const uint8_t *buf, uint32_t count, uint8_t start, uint32_t &amb) {
std::string out;
uint8_t prev = start;
bool open = false, first = false, reading = false;
int bits = -1; // -1: no frame; 0..8 bit index in the current 9-bit unit
uint16_t sh = 0;
char t[8];
for (uint32_t i = 0; i < count; i++) {
uint8_t cur = buf[i];
bool psda = prev & 1, pscl = prev & 2, sda = cur & 1, scl = cur & 2;
if ((psda != sda) && (pscl != scl))
amb++;
if (pscl && scl && psda && !sda) {
out += open ? "Sr " : "S ";
open = true;
bits = 0;
sh = 0;
first = true;
} else if (pscl && scl && !psda && sda) {
out += "P ";
open = false;
bits = -1;
} else if (!pscl && scl && bits >= 0) {
if (bits < 8) {
sh = (sh << 1) | (sda ? 1 : 0);
bits++;
} else {
uint8_t b = sh & 0xFF;
if (first)
reading = b & 1;
snprintf(t, sizeof t, (!first && reading) ? "r%02X%c " : "%02X%c ", b, sda ? '-' : '+');
out += t;
first = false;
bits = 0;
sh = 0;
}
}
prev = cur;
}
return out;
}
} // namespace wiredecode
@@ -0,0 +1,45 @@
#pragma once
// Heap allocation counter for test harnesses: counts while alloc_count::window is set. Link flag per platform:
// ESP32 and ESP8266 -Wl,--wrap=malloc (on ESP8266 the wrapper must be in IRAM: in flash it crash-loops with
// IllegalInst at its first instruction). arduino-pico already wraps malloc and _malloc_r (a second wrap is a duplicate
// symbol), so use -Wl,--wrap=__malloc_lock (malloc and free each count once). LibreTiny already wraps malloc into
// pvPortMalloc, so use -Wl,--wrap=pvPortMalloc.
#include <cstddef>
#include <cstdint>
#include "esphome/core/hal.h"
namespace alloc_count {
inline volatile bool window = false;
inline volatile uint32_t count = 0;
inline void note() {
if (window)
count = count + 1;
}
} // namespace alloc_count
#if defined(USE_RP2)
extern "C" void __real___malloc_lock(struct _reent *r);
extern "C" void __wrap___malloc_lock(struct _reent *r) {
alloc_count::note();
__real___malloc_lock(r);
}
#elif defined(USE_LIBRETINY)
extern "C" void *__real_pvPortMalloc(size_t n);
extern "C" void *__wrap_pvPortMalloc(size_t n) {
alloc_count::note();
return __real_pvPortMalloc(n);
}
#elif defined(USE_ESP8266)
extern "C" void *__real_malloc(size_t n);
extern "C" void *IRAM_ATTR __wrap_malloc(size_t n) {
if (alloc_count::window)
alloc_count::count = alloc_count::count + 1;
return __real_malloc(n);
}
#else
extern "C" void *__real_malloc(size_t n);
extern "C" void *__wrap_malloc(size_t n) {
alloc_count::note();
return __real_malloc(n);
}
#endif
@@ -0,0 +1,69 @@
# Fake BMI270 on an in-firmware I2C bus (bmi270fake.hpp). The real i2c: bus keeps codegen happy; the on_boot
# at 700 runs after the bus and before the bmi270 setup (DATA) and swaps the device onto the fake.
esphome:
name: ${name}
includes:
- alloc_count.hpp
- bmi270fake.hpp
platformio_options:
build_flags:
- -Wl,--wrap=malloc
- -DBMI_FAKE_WRAP_MALLOC
on_boot:
- priority: 700
then:
- lambda: |-
bmi270fake::bus.init_bmm(${fake_aux_addr});
id(imu)->set_i2c_bus(&bmi270fake::bus);
- priority: -100
then:
- lambda: |-
ESP_LOGI("bmi270fake", "BUILD %s imu_failed=%d", "${name}", id(imu)->is_failed());
bmi270fake::bus.report();
logger:
level: DEBUG
wifi:
ssid: !secret wifi_ssid
password: !secret wifi_password
api:
reboot_timeout: 0s
i2c:
id: i2c_bus
sda: ${sda}
scl: ${scl}
scan: false
sensor:
- platform: bmi270
bmi270_id: imu
name: "BMI Temperature"
- platform: motion
type: acceleration_x
name: "BMI Accel X"
accuracy_decimals: 4
- platform: motion
type: acceleration_y
name: "BMI Accel Y"
accuracy_decimals: 4
- platform: motion
type: acceleration_z
name: "BMI Accel Z"
accuracy_decimals: 4
- platform: motion
type: gyroscope_x
name: "BMI Gyro X"
- platform: motion
type: gyroscope_y
name: "BMI Gyro Y"
- platform: motion
type: gyroscope_z
name: "BMI Gyro Z"
interval:
- interval: 60s
then:
- lambda: bmi270fake::bus.report();
@@ -0,0 +1,136 @@
"""bmi270-host-compare.py BASE.report PR.report: compare every scenario of the harness between base and PR.
Streams: base's split INIT_ADDR writes (W 5b xx / W 5c yy) are merged before comparing. NACK scenarios are matched by
the normalized transaction they hit. LOG lines are compared separately and every difference is printed."""
import pathlib
import re
import sys
ANSI = re.compile(r"\x1b\[[0-9;]*m")
def parse(path):
"""Scenario name -> head, LOG, TX and other lines; also the NACK_TOTAL count."""
scen, cur, nack_total = {}, None, 0
with pathlib.Path(path).open(encoding="utf-8", errors="replace") as fh:
for line in fh:
line = ANSI.sub("", line.rstrip("\n"))
if line.startswith("SCENARIO "):
cur = scen.setdefault(
line.split()[1], {"head": "", "log": [], "tx": [], "other": []}
)
elif line.startswith("NACK_TOTAL"):
nack_total = int(line.split()[1])
elif line.startswith(" failed="):
cur["head"] = re.sub(r" setup_tx=\d+", "", line.strip())
elif line.startswith(" LOG "):
cur["log"].append(line[6:])
elif line.startswith(" TX "):
cur["tx"].append(line[5:])
else:
cur["other"].append(
re.sub(r" (aux_w|aux_r|status_reads)=\d+", "", line.strip())
)
return scen, nack_total
def merge(tx):
"""Returns the merged list and, per original index (0 based), the merged index."""
out, idx, i = [], [], 0
while i < len(tx):
a = tx[i]
if (
a.startswith("W 5b ")
and len(a.split()) == 3
and i + 1 < len(tx)
and tx[i + 1].startswith("W 5c ")
and len(tx[i + 1].split()) == 3
):
out.append(a + " " + tx[i + 1].split()[2])
idx += [len(out) - 1, len(out) - 1]
i += 2
continue
out.append(a)
idx.append(len(out) - 1)
i += 1
return out, idx
(b, nack_total), (p, _) = parse(sys.argv[1]), parse(sys.argv[2])
problems = 0
logdiffs = {}
for name in sorted(n for n in p if not n.startswith(("nack_", "step_"))):
sb, sp = b[name], p[name]
same_head = sb["head"] == sp["head"]
same_other = sb["other"] == sp["other"]
line = f"{name:20s} head={'same' if same_head else 'DIFF'} regs/values/upload={'same' if same_other else 'DIFF'}"
if sp["tx"]:
mb, _ = merge(sb["tx"])
line += f" stream base={len(sb['tx'])}->{len(mb)} pr={len(sp['tx'])} {'same' if mb == sp['tx'] else 'DIFF'}"
if mb != sp["tx"]:
problems += 1
if not (same_head and same_other):
problems += 1
line += f"\n base: {sb['head']} {sb['other']}\n pr: {sp['head']} {sp['other']}"
print(line)
if sb["log"] != sp["log"]:
key = (
tuple(x for x in sb["log"] if x not in sp["log"]),
tuple(x for x in sp["log"] if x not in sb["log"]),
)
logdiffs.setdefault(key, []).append(name)
# NACK sweep, matched through the merged aux stream; indexes count main-bus transactions only (the fake numbers those)
main_b = [t for t in b["cfg_aux"]["tx"] if not t.startswith("A ")]
main_p = [t for t in p["cfg_aux"]["tx"] if not t.startswith("A ")]
mmb, midx = merge(main_b)
assert mmb == main_p, "main streams differ"
nack_outcomes = {}
for i in range(nack_total):
k = midx[i]
sb, sp = b[f"nack_{i + 1}"], p[f"nack_{k + 1}"]
hb = (sb["head"], tuple(sb["other"]))
hp = (sp["head"], tuple(sp["other"]))
if hb != hp:
problems += 1
print(
f"NACK base tx {i + 1} / pr tx {k + 1} ({main_p[k][:20]}): DIFF\n base {hb}\n pr {hp}"
)
wb = [x for x in sb["log"] if x[:1] in "EW"]
wp = [x for x in sp["log"] if x[:1] in "EW"]
nack_outcomes.setdefault((sp["head"].split(" status")[0], tuple(wp)), []).append(
k + 1
)
if wb != wp:
key = (tuple(x for x in wb if x not in wp), tuple(x for x in wp if x not in wb))
logdiffs.setdefault(key, []).append(f"nack_pr{k + 1}")
print(
f"NACK scenarios checked: {nack_total} base transactions -> {len(main_p)} pr transactions"
)
print("PR NACK outcome classes (head, E/W log lines): pr transaction numbers")
for (h, w), ks in sorted(nack_outcomes.items(), key=lambda x: x[1][0]):
ks = sorted(set(ks))
print(f" {h} | {' / '.join(w)} | tx {ks[0]}..{ks[-1]} ({len(ks)})")
print("BMM150 step failures (failed/mag_ready, values, W lines): base vs PR")
for name in sorted(n for n in p if n.startswith("step_")):
sb, sp = b[name], p[name]
vb = [x for x in sb["other"] if x.startswith(("REGS", "VALUES"))]
vp = [x for x in sp["other"] if x.startswith(("REGS", "VALUES"))]
if sb["head"] != sp["head"] or vb != vp:
problems += 1
print(
f" {name}: DIFF head/regs/values\n base {sb['head']} {vb}\n pr {sp['head']} {vp}"
)
print(
f" {name:32s} {sp['head'].split(' status')[0]} | mag {[x for x in vp if x.startswith('VALUES')][0].split(' t=')[1] if vp else '-'}"
)
print(f" base W: {[x for x in sb['log'] if x[:1] in 'EW']}")
print(f" pr W: {[x for x in sp['log'] if x[:1] in 'EW']}")
print("LOG differences (only in base | only in PR): scenarios")
for (ob, op), names in logdiffs.items():
names = sorted(set(names))
print(
f" base-only {list(ob)} | pr-only {list(op)} : {len(names)} e.g. {names[:4]}"
)
print("RESULT", "SAME" if problems == 0 else f"DIFFERENT ({problems})")
@@ -0,0 +1,203 @@
#ifdef USE_HOST
// Host harness for esphome#22778. Runs BMI270Component::setup() and update_data() against bmi270fake::Bus for every
// scenario and writes one report to $BMI_OUT: per scenario the outcome (failed, error reason, warning, magnetometer
// ready), the log lines, the final registers, decoded values and the raw transaction stream.
// $BMI_EXPECTED_FNV (hex, optional) overrides the fake's expected config table hash.
// Scenarios: configs from $BMI_SETTERS (lines "name setter value", parsed from each tree's generated main.cpp),
// aux faults, and a NACK at every main-bus transaction of the aux config.
#include <gtest/gtest.h>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <fstream>
#include <map>
#include <memory>
#include <sstream>
#include <string>
#include <vector>
#ifndef USE_BMI270_BMM150
#error "build with -DUSE_BMI270_BMM150 (benchmark.yaml build_flags)"
#endif
#ifndef USE_LOG_LISTENERS
#error "build with -DUSE_LOG_LISTENERS"
#endif
#define protected public // read magnetometer_ready_ and call update_data(); harness only
#include "esphome/components/bmi270/bmi270.h"
#undef protected
#include "esphome/components/bmi270/bmi270_config.h"
#include "esphome/components/logger/logger.h"
#include "bmi270fake.hpp"
namespace esphome::bmi270::testing {
static std::vector<std::string> g_lines;
struct Setter {
std::string fn;
int value;
};
using SetterMap = std::map<std::string, std::vector<Setter>>;
static SetterMap load_setters() {
SetterMap m;
std::ifstream f(std::getenv("BMI_SETTERS"));
std::string name, fn;
int v;
while (f >> name >> fn >> v)
m[name].push_back({fn, v});
return m;
}
static void apply(BMI270Component &imu, const std::vector<Setter> &setters) {
for (auto &s : setters) {
if (s.fn == "set_accel_range")
imu.set_accel_range((BMI270AccelRange) s.value);
else if (s.fn == "set_accel_odr")
imu.set_accel_odr((BMI270AccelODR) s.value);
else if (s.fn == "set_gyro_range")
imu.set_gyro_range((BMI270GyroRange) s.value);
else if (s.fn == "set_gyro_odr")
imu.set_gyro_odr((BMI270GyroODR) s.value);
else if (s.fn == "set_aux_device")
imu.set_aux_device((BMI270AuxDevice) s.value);
else if (s.fn == "set_aux_device_address")
imu.set_aux_device_address(s.value);
else if (s.fn != "none")
ADD_FAILURE() << "unknown setter " << s.fn;
}
}
struct Result {
std::string text;
uint32_t tx;
std::vector<std::string> txlog;
};
// aux_addr: the address the fake BMM150 answers on
static Result run(const std::string &label, const std::vector<Setter> &setters, uint8_t aux_addr, uint8_t bmm_mode,
bool busy_stuck, uint32_t nack_at, bool full, uint8_t stuck_from = 0) {
auto bus = std::make_unique<bmi270fake::Bus>();
if (const char *fnv = std::getenv("BMI_EXPECTED_FNV"))
bus->expected_fnv = std::strtoul(fnv, nullptr, 16);
bus->init_bmm(aux_addr, bmm_mode);
bus->busy_stuck = busy_stuck;
bus->nack_at = nack_at;
bus->stuck_from_aux_op = stuck_from;
BMI270Component imu;
imu.set_i2c_bus(bus.get());
imu.set_i2c_address(0x68);
apply(imu, setters);
float temp = NAN;
bmm150::BMM150Data mag{NAN, NAN, NAN};
imu.add_temperature_listener([&temp](float t) { temp = t; });
imu.add_magnetometer_listener([&mag](const bmm150::BMM150Data &m) { mag = m; });
g_lines.clear();
imu.setup();
const uint32_t setup_tx = bus->transactions;
motion::MotionData d{};
bool sampled = imu.update_data(d);
std::ostringstream o;
char fr[240];
bus->final_regs(fr, sizeof(fr));
o << "SCENARIO " << label << "\n";
o << " failed=" << imu.is_failed() << " warning=" << imu.status_has_warning()
<< " error=" << imu.status_has_error() << " mag_ready=" << imu.magnetometer_ready_ << " setup_tx=" << setup_tx
<< " status=0x" << std::hex << (int) bus->regs[0x21] << std::dec << "\n";
for (auto &l : g_lines)
o << " LOG " << l << "\n";
if (full || !imu.is_failed()) {
o << " REGS " << fr << "\n";
char vals[300];
snprintf(vals, sizeof(vals),
" VALUES sampled=%d ax=%.6f ay=%.6f az=%.6f gx=%.5f gy=%.5f gz=%.5f t=%.4f mx=%.6f my=%.6f mz=%.6f\n",
sampled, d.acceleration[0], d.acceleration[1], d.acceleration[2], d.angular_rate[0], d.angular_rate[1],
d.angular_rate[2], temp, mag.x, mag.y, mag.z);
o << vals;
o << " UPLOAD mem_fnv=" << std::hex << bus->mem_fnv << std::dec << " rewrites=" << bus->rewrites
<< " perr=" << bus->protocol_errors << " data_writes=" << bus->data_writes << " burst=" << bus->burst_min << ".."
<< bus->burst_max << " aux_w=" << bus->aux_writes << " aux_r=" << bus->aux_reads
<< " status_reads=" << bus->status_reads << " norm=" << std::hex << bus->norm_fnv << std::dec << "\n";
}
if (full) {
for (auto &l : bus->log)
o << " TX " << l << "\n";
std::string memp = std::string(std::getenv("BMI_OUT")) + "." + label + ".mem";
FILE *f = std::fopen(memp.c_str(), "wb");
std::fwrite(bus->mem, 1, sizeof(bus->mem), f);
std::fclose(f);
}
return {o.str(), setup_tx, bus->log};
}
TEST(BMI270Harness22778, AllScenarios) {
ASSERT_NE(std::getenv("BMI_OUT"), nullptr);
logger::global_logger->add_log_callback(nullptr, [](void *, uint8_t level, const char *tag, const char *msg,
size_t len) {
std::string s(msg, len);
// drop the colour/header prefix up to the tag's closing bracket
auto p = s.find("]: ");
g_lines.push_back(std::string(1, "NEWICDVV"[level & 7]) + " " + tag + ": " +
(p == std::string::npos ? s : s.substr(p + 3)));
});
SetterMap setters = load_setters();
ASSERT_FALSE(setters.empty());
std::ofstream out(std::string(std::getenv("BMI_OUT")) + ".report");
uint32_t aux_tx = 0;
std::vector<std::string> aux_log;
for (auto &[name, list] : setters) {
uint8_t aux_addr = 0x10;
for (auto &s : list)
if (s.fn == "set_aux_device_address")
aux_addr = s.value;
auto r = run("cfg_" + name, list, aux_addr, 0, false, 0, true);
out << r.text;
if (name == "aux") {
aux_tx = r.tx;
aux_log = r.txlog;
}
}
ASSERT_NE(aux_tx, 0u);
const auto &aux = setters["aux"];
out << run("aux_absent", aux, 0x10, 1, false, 0, false).text;
out << run("aux_wrong_id", aux, 0x10, 2, false, 0, false).text;
out << run("aux_busy_stuck", aux, 0x10, 0, true, 0, false).text;
out << run("aux_wrong_address", aux, 0x11, 0, false, 0, false).text;
// One failure per BMM150 setup step: a NACK on the main-bus write of that step, or the aux busy bit stuck from
// the step's aux transfer on (1 power-on, 2 soft reset, 3 chip ID read, 4-6 trim reads, 7 normal mode)
auto main_index = [&](const std::string &prefix) -> uint32_t {
uint32_t n = 0;
for (auto &l : aux_log) {
if (l.rfind("A ", 0) == 0)
continue;
n++;
if (l.rfind(prefix, 0) == 0 && n > 60) // after the config upload
return n;
}
ADD_FAILURE() << "no transaction " << prefix;
return 0;
};
const std::pair<const char *, const char *> nack_steps[] = {
{"step_aux_if_conf_nack", "W 6b 20"}, {"step_aux_manual_nack", "W 4c 8c"},
{"step_aux_dev_id_nack", "W 4b 20"}, {"step_power_on_data_nack", "W 4f 01"},
{"step_soft_reset_data_nack", "W 4f 83"}, {"step_chip_id_rd_addr_nack", "W 4d 40"},
{"step_trim_rd_addr_nack", "W 4d 5d"}, {"step_normal_mode_data_nack", "W 4f 38"},
{"step_auto_burst_conf_nack", "W 4c 4f"}, {"step_auto_burst_rd_addr_nack", "W 4d 42"},
{"step_auto_burst_pwr_ctrl_nack", "W 7d 0f"},
};
for (auto &[label, prefix] : nack_steps)
out << run(label, aux, 0x10, 0, false, main_index(prefix), false).text;
// the soft reset writes the same AUX_WR_ADDR as power-on; the second "W 4e 4b" is the soft reset one
const char *stuck_labels[] = {"step_power_on_busy", "step_soft_reset_busy", "step_chip_id_read_busy",
"step_trim1_busy", "step_trim2_busy", "step_trim3_busy",
"step_normal_mode_busy"};
for (uint8_t op = 1; op <= 7; op++)
out << run(stuck_labels[op - 1], aux, 0x10, 0, false, 0, false, op).text;
out << run("step_chip_id_wrong", aux, 0x10, 2, false, 0, false).text;
for (uint32_t n = 1; n <= aux_tx; n++)
out << run("nack_" + std::to_string(n), aux, 0x10, 0, false, n, false).text;
out << "NACK_TOTAL " << aux_tx << "\n";
}
} // namespace esphome::bmi270::testing
#endif
@@ -0,0 +1,18 @@
#!/bin/bash
# bmi270-host-run.sh TREE OUTNAME SETTERS [32]: host harness run; with 32, forces the 32 byte burst branch (perl, in
# place; the original is saved first and put back on exit, even after an interrupted run). Writes OUT_DIR/OUTNAME and OUT_DIR/OUTNAME.log.
# env: OUT_DIR (default ./host), PY (default TREE's venv python), PIO_CORE (default ./pio)
T=$(cd "${1:?tree}" && pwd); N=${2:?outname}; ST=$(cd "$(dirname "${3:?setters}")" && pwd)/$(basename "$3")
F=$T/esphome/components/bmi270/bmi270.cpp
O=$(mkdir -p "${OUT_DIR:-host}" && cd "${OUT_DIR:-host}" && pwd); PY=${PY:-$T/venv/bin/python}
PC=$(mkdir -p "${PIO_CORE:-pio}" && cd "${PIO_CORE:-pio}" && pwd)
if [ "$4" = 32 ]; then
cp "$F" "$O/$N.bmi270.cpp.orig"
trap 'cp "$O/$N.bmi270.cpp.orig" "$F"' EXIT
perl -pi -e 's{^#if defined\(USE_ARDUINO\) && !defined\(USE_ESP32\)$}{#if 1 // harness: force 32}' "$F"
grep -q '^#if 1 // harness' "$F" || { echo no-sed; exit 1; }
fi
(cd "$T" && PLATFORMIO_CORE_DIR=$PC PYTHONPATH=$T BMI_OUT=$O/$N BMI_SETTERS=$ST \
timeout 2400 $PY script/cpp_unit_test.py bmi270 > "$O/$N.log" 2>&1); rc=$?; echo "$N rc=$rc"
grep -a -E '^\[ (PASSED|FAILED) |BMM150_SWEEP' "$O/$N.log"
exit $rc
@@ -0,0 +1,20 @@
motion:
- platform: bmi270
id: imu
i2c_id: i2c_bus
update_interval: 5s
aux_device: BMM150
sensor:
- platform: bmi270
bmi270_id: imu
type: magnetic_field_x
name: "BMI Mag X"
- platform: bmi270
bmi270_id: imu
type: magnetic_field_y
name: "BMI Mag Y"
- platform: bmi270
bmi270_id: imu
type: magnetic_field_z
name: "BMI Mag Z"
@@ -0,0 +1,26 @@
"""setters.py TREE OUT NAME=main.cpp...: write 'name setter value' lines for the harness from generated main.cpp."""
import pathlib
import re
import sys
tree, out = sys.argv[1], sys.argv[2]
h = pathlib.Path(f"{tree}/esphome/components/bmi270/bmi270.h").read_text(
encoding="utf-8"
)
enums = {
m[0]: int(m[1], 0)
for m in re.findall(r"(BMI270_[A-Z0-9_]+) = (0x[0-9A-Fa-f]+|\d+)", h)
}
with pathlib.Path(out).open("w", encoding="utf-8") as f:
for arg in sys.argv[3:]:
name, path = arg.split("=")
calls = re.findall(
r"imu->(set_(?:accel|gyro|aux)\w*)\((?:bmi270::)?(\w+)\);",
pathlib.Path(path).read_text(encoding="utf-8"),
)
if not calls:
f.write(f"{name} none 0\n")
f.writelines(
f"{name} {fn} {enums[v] if v in enums else int(v, 0)}\n" for fn, v in calls
)
@@ -0,0 +1,364 @@
#pragma once
// Fake BMI270 (+ BMM150 behind its aux interface) on an in-firmware I2C bus (esphome#22778).
// BMI270: register file with reset values, soft reset, config upload port (INIT_ADDR_0/1 word address, INIT_DATA
// streaming into 8 KB), INTERNAL_STATUS 0x01 only when INIT_CTRL=1 arrives with every config byte written once and
// the memory's FNV-1a equal to `expected_fnv` (default: the current table, computed on the host from
// bmi270_config.h), 0x02 otherwise.
// Aux interface: AUX_DEV_ID selects the BMM150 (addr << 1); in manual mode (AUX_IF_CONF bit 7) a write to
// AUX_WR_ADDR writes AUX_WR_DATA to the BMM150 and a write to AUX_RD_ADDR reads man_rd_burst (1/2/6/8) bytes into
// AUX_X_LSB..; either sets STATUS.aux_busy for `busy_reads` STATUS reads. In auto mode with aux enabled the AUX data
// registers mirror the BMM150 from AUX_RD_ADDR. Modes: bmm_mode 0 ok, 1 absent (aux transfers do nothing),
// 2 wrong chip id; busy_stuck keeps aux_busy set (stuck_from_aux_op: from the Nth aux transfer); nack_at fails main-bus
// transaction N (1 based). `norm_fnv` hashes the register-level effect of the stream (split or merged writes hash the
// same). Heap is sampled at INIT_CTRL=0/1 and inside every INIT_DATA burst; -DBMI_FAKE_WRAP_MALLOC with the link flag
// from alloc_count.hpp counts malloc() during the upload.
#include <cstdint>
#include <cstdio>
#include <cstring>
#include "esphome/components/i2c/i2c_bus.h"
#include "esphome/core/log.h"
#ifdef USE_HOST
#include <string>
#include <vector>
#endif
#ifdef USE_ESP8266
#include <Esp.h>
#elif defined(USE_ESP32)
#include <esp_heap_caps.h>
#endif
#ifdef BMI_FAKE_WRAP_MALLOC
#include "alloc_count.hpp"
#endif
namespace bmi270fake {
static const char *const TAG = "bmi270fake";
static constexpr uint8_t ADDR = 0x68;
static constexpr size_t MEM = 8192;
static constexpr uint8_t R_CHIP_ID = 0x00, R_STATUS = 0x03, R_AUX_X = 0x04, R_DATA_8 = 0x0C, R_INTERNAL_STATUS = 0x21,
R_TEMP_0 = 0x22, R_AUX_DEV_ID = 0x4B, R_AUX_IF_CONF = 0x4C, R_AUX_RD_ADDR = 0x4D,
R_AUX_WR_ADDR = 0x4E, R_AUX_WR_DATA = 0x4F, R_INIT_CTRL = 0x59, R_INIT_ADDR_0 = 0x5B,
R_INIT_ADDR_1 = 0x5C, R_INIT_DATA = 0x5E, R_IF_CONF = 0x6B, R_PWR_CONF = 0x7C,
R_PWR_CTRL = 0x7D, R_CMD = 0x7E;
inline uint32_t heap_free() {
#ifdef USE_ESP8266
return ESP.getFreeHeap();
#elif defined(USE_ESP32)
return heap_caps_get_free_size(MALLOC_CAP_INTERNAL);
#else
return 0;
#endif
}
inline uint32_t heap_block() {
#ifdef USE_ESP8266
return ESP.getMaxFreeBlockSize();
#elif defined(USE_ESP32)
return heap_caps_get_largest_free_block(MALLOC_CAP_INTERNAL);
#else
return 0;
#endif
}
struct Bus final : public esphome::i2c::I2CBus {
uint8_t regs[128]{};
uint8_t mem[MEM]{};
uint8_t written[MEM / 8]{};
uint8_t bmm[128]{}; // BMM150 registers 0x00..0x7F
uint8_t bmm_addr{0x10};
uint8_t bmm_mode{0};
bool busy_stuck{false};
uint8_t stuck_from_aux_op{0}; // aux busy never clears from this aux transfer on (1 based), 0 = off
uint32_t aux_ops{0};
uint8_t busy_reads{2}, busy_left{0};
uint32_t nack_at{0};
uint32_t expected_fnv{0x95BB75B5}; // FNV-1a of the 8192 byte table in bmi270_config.h
uint32_t mem_fnv{0}, norm_fnv{2166136261u};
uint32_t transactions{0}, reads{0}, writes{0}, data_writes{0}, data_bytes{0}, addr_writes{0}, rewrites{0};
uint32_t protocol_errors{0}, loads_ok{0}, loads_bad{0}, resets{0}, aux_writes{0}, aux_reads{0}, status_reads{0};
uint32_t burst_min{UINT32_MAX}, burst_max{0};
uint32_t heap_start{0}, block_start{0}, heap_end{0}, block_end{0}, heap_min_in_burst{UINT32_MAX};
uint32_t bursts_heap_below_start{0};
// accel x/y/z, gyro x/y/z (LE int16 at 0x0C..0x17), temperature raw at 0x22
int16_t sample[6]{4096, -8192, 12288, 1638, -3277, 819};
int16_t temp_raw{1024};
// BMM150 raw data 0x42..0x49 and trim 0x5D..0x74 (datasheet-like values)
uint8_t mag_raw[8]{0x10, 0x12, 0x20, 0xF0, 0x30, 0x08, 0x40, 0x1A};
// x1=-2 y1=3 z4=-10 x2=26 y2=27 z2=760 z1=24786 xyz1=7101 z3=64 xy2=-3 xy1=29
uint8_t trim[24]{0xFE, 0x03, 0x00, 0x00, 0x00, 0xF6, 0xFF, 0x1A, 0x1B, 0x00, 0x00, 0xF8,
0x02, 0xD2, 0x60, 0xBD, 0x1B, 0x40, 0x00, 0xFD, 0x1D, 0x00, 0x00, 0x00};
#ifdef USE_HOST
std::vector<std::string> log; // raw transaction stream, plus "A" lines for BMM150 side effects
#endif
Bus() {
this->reset_();
this->bmm_reset_(true);
}
// Puts the BMM150 on `addr` with fault mode `mode` and powers it off
void init_bmm(uint8_t addr, uint8_t mode = 0) {
this->bmm_addr = addr;
this->bmm_mode = mode;
this->bmm_reset_(true);
}
void reset_() {
memset(this->regs, 0, sizeof(this->regs));
this->regs[R_CHIP_ID] = 0x24;
this->regs[R_PWR_CONF] = 0x03;
this->regs[R_AUX_DEV_ID] = 0x20;
this->regs[R_AUX_IF_CONF] = 0x83;
this->regs[R_AUX_RD_ADDR] = 0x42;
this->regs[R_AUX_WR_ADDR] = 0x4C;
this->regs[R_AUX_WR_DATA] = 0x02;
memset(this->mem, 0, sizeof(this->mem));
memset(this->written, 0, sizeof(this->written));
this->busy_left = 0;
}
void bmm_reset_(bool power_off) {
uint8_t pwr = this->bmm[0x4B] & 0x01;
memset(this->bmm, 0, sizeof(this->bmm));
this->bmm[0x40] = this->bmm_mode == 2 ? 0x33 : 0x32;
this->bmm[0x4B] = power_off ? 0 : pwr;
this->bmm[0x4C] = 0x06; // sleep mode
memcpy(&this->bmm[0x42], this->mag_raw, 8);
memcpy(&this->bmm[0x5D], this->trim, sizeof(this->trim));
}
uint8_t bmm_read_(uint8_t r) {
if (r == 0x40 && !(this->bmm[0x4B] & 0x01))
return 0x00; // chip id reads 0 in suspend
if (r >= 0x42 && r <= 0x49)
return this->mag_raw[r - 0x42];
return this->bmm[r & 0x7F];
}
void mix_(uint32_t v) {
for (int i = 0; i < 4; i++) {
this->norm_fnv ^= (v >> (8 * i)) & 0xFF;
this->norm_fnv *= 16777619u;
}
}
static uint32_t fnv(const uint8_t *d, size_t n) {
uint32_t h = 2166136261u;
for (size_t i = 0; i < n; i++) {
h ^= d[i];
h *= 16777619u;
}
return h;
}
uint32_t covered_() const {
uint32_t n = 0;
for (uint8_t b : this->written)
n += __builtin_popcount(b);
return n;
}
void note_(const char *fmt, unsigned a, unsigned b) {
#ifdef USE_HOST
char s[48];
snprintf(s, sizeof(s), fmt, a, b);
this->log.push_back(s);
#endif
}
bool aux_present_() const {
return this->bmm_mode != 1 && (this->regs[R_IF_CONF] & 0x20) && this->regs[R_AUX_DEV_ID] == (this->bmm_addr << 1);
}
void aux_trigger_() {
this->aux_ops++;
if (this->stuck_from_aux_op && this->aux_ops >= this->stuck_from_aux_op)
this->busy_stuck = true;
this->busy_left = this->busy_reads;
}
void fill_data_() {
for (int i = 0; i < 6; i++) {
this->regs[R_DATA_8 + 2 * i] = this->sample[i] & 0xFF;
this->regs[R_DATA_8 + 2 * i + 1] = (uint16_t) this->sample[i] >> 8;
}
this->regs[R_TEMP_0] = this->temp_raw & 0xFF;
this->regs[R_TEMP_0 + 1] = (uint16_t) this->temp_raw >> 8;
// auto mode: the BMI270 keeps AUX_X.. filled from the BMM150
if (!(this->regs[R_AUX_IF_CONF] & 0x80) && (this->regs[R_PWR_CTRL] & 0x01) && this->aux_present_()) {
for (int i = 0; i < 8; i++)
this->regs[R_AUX_X + i] = this->bmm_read_(this->regs[R_AUX_RD_ADDR] + i);
}
}
void write_reg_(uint8_t reg, uint8_t v) {
this->mix_(0x01000000u | (reg << 8) | v);
if (reg == R_CMD && v == 0xB6) {
this->resets++;
this->reset_();
return;
}
if (reg == R_INIT_ADDR_0 || reg == R_INIT_ADDR_1)
this->addr_writes++;
if (reg == R_INIT_CTRL) {
if (v == 0x00) {
this->heap_start = heap_free();
this->block_start = heap_block();
this->heap_min_in_burst = UINT32_MAX;
this->bursts_heap_below_start = 0;
this->regs[R_INTERNAL_STATUS] = 0x00;
#ifdef BMI_FAKE_WRAP_MALLOC
alloc_count::count = 0;
alloc_count::window = true;
#endif
} else if (v == 0x01) {
#ifdef BMI_FAKE_WRAP_MALLOC
alloc_count::window = false;
#endif
this->heap_end = heap_free();
this->block_end = heap_block();
this->mem_fnv = fnv(this->mem, MEM);
bool ok = this->mem_fnv == this->expected_fnv && this->covered_() == MEM && this->rewrites == 0;
this->regs[R_INTERNAL_STATUS] = ok ? 0x01 : 0x02;
(ok ? this->loads_ok : this->loads_bad)++;
}
}
this->regs[reg & 0x7F] = v;
bool manual = this->regs[R_AUX_IF_CONF] & 0x80;
if (reg == R_AUX_WR_ADDR && manual) {
this->aux_writes++;
this->aux_trigger_();
if (this->aux_present_()) {
uint8_t d = this->regs[R_AUX_WR_DATA];
this->note_("A W %02x %02x", v, d);
this->mix_(0x04000000u | (v << 8) | d);
if (v == 0x4B && (d & 0x82) == 0x82)
this->bmm_reset_(false);
else
this->bmm[v & 0x7F] = d;
}
} else if (reg == R_AUX_RD_ADDR && manual) {
this->aux_reads++;
this->aux_trigger_();
if (this->aux_present_()) {
static const uint8_t LEN[4] = {1, 2, 6, 8};
uint8_t n = LEN[(this->regs[R_AUX_IF_CONF] >> 2) & 3];
this->note_("A R %02x n%u", v, n);
this->mix_(0x05000000u | (v << 8) | n);
for (uint8_t i = 0; i < n; i++)
this->regs[R_AUX_X + i] = this->bmm_read_(v + i);
}
}
}
void init_data_(const uint8_t *d, size_t n) {
this->data_writes++;
this->data_bytes += n;
if (n < this->burst_min)
this->burst_min = n;
if (n > this->burst_max)
this->burst_max = n;
uint32_t h = heap_free();
if (h < this->heap_min_in_burst)
this->heap_min_in_burst = h;
if (h < this->heap_start)
this->bursts_heap_below_start++;
if ((this->regs[R_PWR_CONF] & 0x01) || this->regs[R_INIT_CTRL] != 0x00) {
this->protocol_errors++;
return;
}
size_t off = 2 * ((this->regs[R_INIT_ADDR_0] & 0x0F) | ((size_t) this->regs[R_INIT_ADDR_1] << 4));
for (size_t i = 0; i < n; i++, off++) {
if (off >= MEM) {
this->protocol_errors++;
return;
}
this->mix_(0x02000000u | (off << 8) | d[i]);
if (this->written[off / 8] & (1 << (off % 8)))
this->rewrites++;
this->written[off / 8] |= 1 << (off % 8);
this->mem[off] = d[i];
}
}
esphome::i2c::ErrorCode write_readv(uint8_t address, const uint8_t *data, size_t size, uint8_t *out,
size_t out_size) override {
if (address != ADDR)
return esphome::i2c::ERROR_NOT_ACKNOWLEDGED;
this->transactions++;
#ifdef USE_HOST
{
std::string s = size > 1 ? "W" : "R";
char b[8];
for (size_t i = 0; i < size; i++) {
snprintf(b, sizeof(b), " %02x", data[i]);
s += b;
}
if (out_size) {
snprintf(b, sizeof(b), " r%u", (unsigned) out_size);
s += b;
}
if (this->transactions == this->nack_at)
s += " NACK";
this->log.push_back(s);
}
#endif
if (this->transactions == this->nack_at)
return esphome::i2c::ERROR_NOT_ACKNOWLEDGED;
if (size == 0) {
this->protocol_errors++;
return esphome::i2c::ERROR_INVALID_ARGUMENT;
}
uint8_t reg = data[0] & 0x7F;
if (size > 1) {
this->writes++;
if (reg == R_INIT_DATA) {
this->init_data_(data + 1, size - 1);
} else {
for (size_t i = 1; i < size; i++)
this->write_reg_((reg + i - 1) & 0x7F, data[i]);
}
}
if (out_size) {
this->reads++;
// STATUS polls are timing dependent (busy wait), so they stay out of the hash
if (reg != R_STATUS)
this->mix_(0x03000000u | (reg << 8) | (out_size & 0xFF));
if (reg == R_STATUS) {
this->status_reads++;
this->regs[R_STATUS] = (this->busy_stuck || this->busy_left) ? 0x04 : 0x00;
if (this->busy_left)
this->busy_left--;
}
this->fill_data_();
for (size_t i = 0; i < out_size; i++)
out[i] = this->regs[(reg + i) & 0x7F];
}
return esphome::i2c::ERROR_OK;
}
// Final configuration registers, for comparing builds
void final_regs(char *buf, size_t n) const {
snprintf(buf, n,
"ACC_CONF=%02X ACC_RANGE=%02X GYR_CONF=%02X GYR_RANGE=%02X PWR_CONF=%02X PWR_CTRL=%02X IF_CONF=%02X "
"AUX_DEV_ID=%02X AUX_IF_CONF=%02X AUX_RD_ADDR=%02X BMM_PWR=%02X BMM_OPMODE=%02X",
this->regs[0x40], this->regs[0x41], this->regs[0x42], this->regs[0x43], this->regs[R_PWR_CONF],
this->regs[R_PWR_CTRL], this->regs[R_IF_CONF], this->regs[R_AUX_DEV_ID], this->regs[R_AUX_IF_CONF],
this->regs[R_AUX_RD_ADDR], this->bmm[0x4B], this->bmm[0x4C]);
}
void report() const {
char fr[220];
this->final_regs(fr, sizeof(fr));
ESP_LOGI(TAG,
"UPLOAD status=0x%02X ok=%u bad=%u mem_fnv=%08X expected=%08X covered=%u rewrites=%u perr=%u resets=%u",
this->regs[R_INTERNAL_STATUS], this->loads_ok, this->loads_bad, this->mem_fnv, this->expected_fnv,
this->covered_(), this->rewrites, this->protocol_errors, this->resets);
ESP_LOGI(TAG,
"STREAM tx=%u writes=%u reads=%u data_writes=%u data_bytes=%u burst=%u..%u addr_reg_writes=%u aux_w=%u "
"aux_r=%u norm=%08X",
this->transactions, this->writes, this->reads, this->data_writes, this->data_bytes,
this->burst_min == UINT32_MAX ? 0 : this->burst_min, this->burst_max, this->addr_writes, this->aux_writes,
this->aux_reads, this->norm_fnv);
ESP_LOGI(TAG, "REGS %s", fr);
ESP_LOGI(TAG, "HEAP start=%u/%u end=%u/%u min_in_burst=%u bursts_below_start=%u", this->heap_start,
this->block_start, this->heap_end, this->block_end, this->heap_min_in_burst,
this->bursts_heap_below_start);
#ifdef BMI_FAKE_WRAP_MALLOC
ESP_LOGI(TAG, "MALLOC during upload=%u", (unsigned) alloc_count::count);
#endif
}
};
inline Bus bus;
} // namespace bmi270fake
@@ -0,0 +1,98 @@
#ifdef USE_HOST
// esphome#22778: the PR's bmm150_parse_trim/bmm150_convert against the merge base's copy (bmm150_aux_base.h),
// bit for bit, over exhaustive per-field sweeps with edge trims and random raw/trim pairs.
#include <gtest/gtest.h>
#include <array>
#include <cmath>
#include <cstdint>
#include <vector>
#include <cstring>
#include <random>
#include "esphome/components/bmi270/bmm150_aux.h"
#include "bmm150_aux_base.h"
namespace esphome::bmi270::testing {
static uint32_t bits(float f) {
uint32_t u;
std::memcpy(&u, &f, 4);
return u;
}
struct Counts {
uint64_t checked{0}, mismatched{0}, nan{0}, finite{0};
};
static void check(const uint8_t *trim_regs, const uint8_t *raw, Counts &c) {
const auto tp = bmm150::bmm150_parse_trim(trim_regs);
const auto tb = bmm150_base::bmm150_parse_trim(trim_regs);
static_assert(sizeof(tp) == sizeof(tb));
const auto p = bmm150::bmm150_convert(tp, raw);
const auto b = bmm150_base::bmm150_convert(tb, raw);
c.checked++;
bool same = std::memcmp(&tp, &tb, sizeof(tp)) == 0 && bits(p.x) == bits(b.x) && bits(p.y) == bits(b.y) &&
bits(p.z) == bits(b.z);
if (!same) {
if (c.mismatched < 5)
ADD_FAILURE() << "mismatch raw " << std::hex << (int) raw[0] << " " << (int) raw[1] << " " << (int) raw[2]
<< " " << (int) raw[3] << " " << (int) raw[4] << " " << (int) raw[5] << " " << (int) raw[6]
<< " " << (int) raw[7];
c.mismatched++;
}
(std::isnan(p.x) || std::isnan(p.y) || std::isnan(p.z) ? c.nan : c.finite)++;
}
TEST(BMM150Sweep22778, BaseAndPrDecodeIdentically) {
std::vector<std::array<uint8_t, 24>> trims;
// datasheet-like (same as the fake), all zero, all 0xFF, all 0x80, all 0x7F, alternating, and single zeroed words
std::array<uint8_t, 24> ds{0xFE, 0x03, 0, 0, 0, 0xF6, 0xFF, 0x1A, 0x1B, 0, 0, 0xF8, 0x02, 0xD2, 0x60, 0xBD, 0x1B,
0x40, 0, 0xFD, 0x1D, 0, 0, 0};
trims.push_back(ds);
for (uint8_t fill : {0x00, 0xFF, 0x80, 0x7F, 0x01}) {
std::array<uint8_t, 24> t;
t.fill(fill);
trims.push_back(t);
}
for (int off : {11, 13, 15}) { // z2 = 0, z1 = 0, xyz1 = 0 give NaN paths
auto t = ds;
t[off] = t[off + 1] = 0;
trims.push_back(t);
}
{
auto t = ds;
t[16] = 0xFF; // xyz1 with bit 15 set (masked)
trims.push_back(t);
}
const uint8_t backgrounds[3][8] = {{0x10, 0x12, 0x20, 0xF0, 0x30, 0x08, 0x40, 0x1A},
{0x00, 0x80, 0x00, 0x80, 0x00, 0x80, 0xFC, 0xFF},
{0xF8, 0x7F, 0x07, 0x00, 0x01, 0xC0, 0x00, 0x00}};
Counts c;
for (auto &t : trims)
for (auto &bg : backgrounds)
for (int field = 0; field < 4; field++)
for (uint32_t w = 0; w <= 0xFFFF; w++) {
uint8_t raw[8];
std::memcpy(raw, bg, 8);
raw[2 * field] = w & 0xFF;
raw[2 * field + 1] = w >> 8;
check(t.data(), raw, c);
}
const uint64_t exhaustive = c.checked;
std::mt19937 rng(22778);
for (int i = 0; i < 3000000; i++) {
uint8_t t[24], raw[8];
for (auto &b : t)
b = rng();
for (auto &b : raw)
b = rng();
check(t, raw, c);
}
printf("BMM150_SWEEP exhaustive=%llu random=%llu mismatched=%llu nan_results=%llu finite_results=%llu\n",
(unsigned long long) exhaustive, (unsigned long long) (c.checked - exhaustive),
(unsigned long long) c.mismatched, (unsigned long long) c.nan, (unsigned long long) c.finite);
EXPECT_EQ(c.mismatched, 0u);
}
} // namespace esphome::bmi270::testing
#endif
@@ -0,0 +1,168 @@
esphome:
name: ${name}
includes:
- wkfake.hpp
on_boot:
- priority: 999.95 # before the weikai hub setup (BUS - 0.1)
then:
- lambda: |-
wkfake::bus.trace = 1000;
id(wk)->set_i2c_bus(&wkfake::bus);
- priority: -100
then:
- lambda: wkfake::bus.trace = 0;
- delay: 4s
- script.execute: drive
logger:
level: VERBOSE
logs:
component: DEBUG
scheduler: DEBUG
app: DEBUG
switch: DEBUG
binary_sensor: DEBUG
gpio.binary_sensor: DEBUG
gpio.switch: DEBUG
esp32: DEBUG
i2c:
id: realbus
sda: GPIO21
scl: GPIO22
scan: false
wk2168_i2c:
- id: wk
i2c_id: realbus
address: 0x10
uart:
- id: wk_uart0
channel: 0
baud_rate: 115200
- id: wk_uart1
channel: 1
baud_rate: 9600
switch:
- platform: gpio
id: wk_out0
name: "WK pin 0"
restore_mode: ALWAYS_OFF
pin:
wk2168_i2c: wk
number: 0
mode:
output: true
on_turn_on:
- logger.log: {format: "SWITCH pin0 ON", level: INFO}
on_turn_off:
- logger.log: {format: "SWITCH pin0 OFF", level: INFO}
binary_sensor:
- platform: gpio
id: wk_in1
name: "WK pin 1"
trigger_on_initial_state: true
pin:
wk2168_i2c: wk
number: 1
mode:
input: true
on_state:
- lambda: |-
ESP_LOGI("wktest", "BINARY pin1 = %s", x ? "ON" : "OFF");
button:
- platform: template
id: in_high
name: "Fake input pin1 high"
on_press:
- lambda: wkfake::bus.set_input(1, true);
- platform: template
id: in_low
name: "Fake input pin1 low"
on_press:
- lambda: wkfake::bus.set_input(1, false);
interval:
- interval: 100ms
then:
- lambda: |-
uart::UARTComponent *u[2] = {id(wk_uart0), id(wk_uart1)};
for (int c = 0; c < 2; c++) {
size_t n = u[c]->available();
if (!n) continue;
char buf[64];
if (n > sizeof(buf) - 1) n = sizeof(buf) - 1;
u[c]->read_array((uint8_t *) buf, n);
for (size_t i = 0; i < n; i++) if (buf[i] < 0x20) buf[i] = '.';
buf[n] = 0;
ESP_LOGI("wktest", "UART ch%d got %u bytes '%s'", c, (unsigned) n, buf);
}
script:
- id: drive
then:
- logger.log: {format: "STEP 1 state after setup", level: INFO}
- lambda: wkfake::bus.dump();
- logger.log: {format: "STEP 2 trace one poll pass", level: INFO}
- lambda: wkfake::bus.trace = 14;
- delay: 1s
- logger.log: {format: "STEP 3 GPIO output toggles", level: INFO}
- switch.turn_on: wk_out0
- delay: 1s
- switch.turn_off: wk_out0
- delay: 1s
- switch.turn_on: wk_out0
- delay: 1s
- logger.log: {format: "STEP 4 GPIO input changes", level: INFO}
- button.press: in_high
- delay: 1s
- button.press: in_low
- delay: 1s
- button.press: in_high
- delay: 1s
- logger.log: {format: "STEP 5 UART TX", level: INFO}
- lambda: |-
id(wk_uart0)->write_str("PING0");
id(wk_uart1)->write_str("PING1");
- delay: 1s
- logger.log: {format: "STEP 6 UART RX inject", level: INFO}
- lambda: |-
wkfake::bus.inject_rx(0, "hello0");
wkfake::bus.inject_rx(1, "hello1");
- delay: 1s
- logger.log: {format: "STEP 7 three single read NACKs", level: INFO}
- lambda: wkfake::bus.fail_reads = 1;
- delay: 1s
- lambda: wkfake::bus.fail_reads = 1;
- delay: 1s
- lambda: wkfake::bus.fail_reads = 1;
- delay: 1s
- logger.log: {format: "STEP 8 single NACK on the register pointer write", level: INFO}
- lambda: wkfake::bus.fail_ptr = 1;
- delay: 1s
- logger.log: {format: "STEP 9 double read NACK", level: INFO}
- lambda: wkfake::bus.fail_reads = 2;
- delay: 1s
- logger.log: {format: "STEP 10 every 37th read NACKed for 5 s", level: INFO}
- lambda: |-
wkfake::bus.every_n = 37;
ESP_LOGI("wktest", "EVERYN start injected=%u retries=%u", wkfake::bus.injected, wkfake::bus.retries);
- delay: 5s
- lambda: |-
wkfake::bus.every_n = 0;
ESP_LOGI("wktest", "EVERYN end injected=%u retries=%u no_retry=%u", wkfake::bus.injected, wkfake::bus.retries, wkfake::bus.no_retry);
- delay: 500ms
- logger.log: {format: "STEP 11 GPIO and UART again after faults", level: INFO}
- switch.turn_off: wk_out0
- button.press: in_low
- delay: 1s
- lambda: |-
wkfake::bus.inject_rx(0, "again0");
id(wk_uart1)->write_str("PONG1");
- delay: 1s
- logger.log: {format: "STEP 12 final", level: INFO}
- lambda: wkfake::bus.dump();
- logger.log: {format: "DRIVE DONE", level: INFO}
@@ -0,0 +1,32 @@
#include <cstdint>
#include <cstdio>
enum RegType { REG = 0, FIFO = 1 };
// i2c_address() is the same in both trees; only the PR adds i2c_register() (the base sends register_ unchanged).
inline uint8_t i2c_address(uint8_t base_address, uint8_t channel, RegType fifo) {
return base_address | channel << 1 | fifo << 0;
}
namespace pr {
inline uint8_t i2c_register(uint8_t reg, uint8_t &channel) {
if (reg > 0x0F) {
channel = (reg >> 4) & 0x03;
}
return reg & 0x0F;
}
}
int main() {
int same = 0, diff = 0;
const uint8_t B = 0x10;
for (int ch = 0; ch < 4; ch++)
for (int reg = 0; reg < 0x40; reg++) {
uint8_t ba = i2c_address(B, ch, REG), br = reg;
uint8_t c = ch;
uint8_t pr_reg = pr::i2c_register(reg, c);
uint8_t pa = i2c_address(B, c, REG);
bool eq = ba == pa && br == pr_reg;
if (reg < 0x10) { eq ? same++ : diff++; if (!eq) printf("MISMATCH ch%d reg %02X\n", ch, reg); }
else if (reg == 0x10 || reg == 0x11 || reg == 0x21 || reg == 0x31)
printf("global %02X ch%d: base @%02X r=%02X -> PR @%02X r=%02X\n", reg, ch, ba, br, pa, pr_reg);
}
printf("regs below 0x10, 4 channels: %d same, %d different\n", same, diff);
for (int ch = 0; ch < 4; ch++) printf("FIFO ch%d: @%02X in both\n", ch, i2c_address(B, ch, FIFO));
}
@@ -0,0 +1,373 @@
#pragma once
// Fake WeiKai WK2168 on an in-firmware I2C bus (esphome#22759, 2026-11-15).
// Device address 0 A1 A0 1 0 C1 C0 F (base 0x10 when A1 A0 = 00): eight addresses, F=1 is the FIFO of channel C1C0.
// The register byte carries only A3..A0; the 6 bit register address is C1C0A3A2A1A0 (datasheet 12.4). Global registers
// are 0x00 GENA, 0x01 GRST, 0x02 GMUT, 0x10 GIER, 0x11 GIFR, 0x21 GPDIR, 0x31 GPDAT; A3..A0 >= 3 is a sub-UART register
// of channel C1C0. A register byte with bits above A3..A0 is truncated like the chip does and counted in hi_bits.
// A write to GRST soft-resets the channels in its low nibble (registers and FIFOs to defaults) and is logged loudly.
// Every transaction is classified: W (register write), P (1 byte write: register pointer), R (read only, uses the
// pointer of that address), WR (write then read in one transaction, repeated start). trace > 0 logs the next `trace`
// transactions; writes, faults and changed read values are always logged; the rest is counted in cnt[][][].
// Fault injection: fail_reads NACKs the next read phases (R or WR), fail_ptr the next P transfers, every_n NACKs every
// Nth read phase once. After a NACK, the next read phase on the same address and register counts as a retry.
#include <cstring>
#include "esphome/components/i2c/i2c_bus.h"
#include "esphome/core/log.h"
namespace wkfake {
static const char *const TAG = "wkfake";
using esphome::i2c::ErrorCode;
enum Kind : uint8_t { K_W = 0, K_P = 1, K_R = 2, K_WR = 3 };
static const char *const KIND[] = {"W ", "P ", "R ", "WR"};
struct Fifo {
uint8_t d[256];
uint16_t head{0}, n{0};
void clear() { head = n = 0; }
bool push(uint8_t b) {
if (n == 256)
return false;
d[(head + n++) & 0xFF] = b;
return true;
}
uint8_t pop() {
if (!n)
return 0;
uint8_t b = d[head];
head = (head + 1) & 0xFF;
n--;
return b;
}
};
struct Bus final : public esphome::i2c::I2CBus {
uint8_t base{0x10};
uint8_t glob[64]{};
uint8_t pg0[4][16]{}, pg1[4][16]{}, spage[4]{};
uint8_t ptr[8]{};
uint8_t gpio_in{0};
Fifo rx[4];
// counters
uint32_t cnt[8][64][4]{};
uint32_t n_trans{0}, grst_writes{0}, hi_bits{0}, reserved{0}, tx_bytes[4]{}, tx_dropped{0}, rx_dropped{0};
uint32_t injected{0}, retries{0}, no_retry{0}, gpdir_writes{0}, gpdat_writes{0}, gpdat_reads{0};
uint8_t last_read[8][64];
// controls
uint32_t trace{0}, fail_reads{0}, fail_ptr{0}, every_n{0}, read_phases{0};
bool await_retry{false};
uint8_t await_addr{0}, await_reg{0};
Bus() {
memset(this->last_read, 0xEE, sizeof(this->last_read));
for (int c = 0; c < 4; c++)
this->reset_channel_(c);
}
void reset_channel_(int c) {
memset(this->pg0[c], 0, 16);
memset(this->pg1[c], 0, 16);
this->spage[c] = 0;
this->rx[c].clear();
}
static const char *gname(uint8_t a6) {
switch (a6) {
case 0x00:
return "GENA";
case 0x01:
return "GRST";
case 0x02:
return "GMUT";
case 0x10:
return "GIER";
case 0x11:
return "GIFR";
case 0x21:
return "GPDIR";
case 0x31:
return "GPDAT";
default:
return "RSVD";
}
}
static const char *cname(uint8_t r, bool p1) {
static const char *const P0[] = {"", "", "", "SPAGE", "SCR", "LCR", "FCR", "SIER",
"SIFR", "TFCNT", "RFCNT", "FSR", "LSR", "FDAT", "R0E", "R0F"};
static const char *const P1[] = {"", "", "", "SPAGE", "BRH", "BRL", "BRD", "RFI",
"TFI", "R19", "R1A", "R1B", "R1C", "R1D", "R1E", "R1F"};
return p1 ? P1[r & 15] : P0[r & 15];
}
void name_(uint8_t addr, uint8_t regbyte, char *out, size_t len) {
uint8_t c = (addr >> 1) & 3, r = regbyte & 0x0F;
if (r < 3)
snprintf(out, len, "%s(%02X)", gname((c << 4) | r), (c << 4) | r);
else
snprintf(out, len, "ch%u.%s", c, cname(r, r != 3 && this->spage[c]));
}
uint8_t gpdat_value() const {
uint8_t dir = this->glob[0x21];
return (this->glob[0x31] & dir) | (this->gpio_in & ~dir);
}
void show_gpio(const char *why) {
ESP_LOGI(TAG, "GPIO %s: GPDIR=%02X GPDAT latch=%02X inputs=%02X pins=%02X", why, this->glob[0x21], this->glob[0x31],
this->gpio_in, this->gpdat_value());
}
void reg_write_(uint8_t addr, uint8_t regbyte, uint8_t v) {
uint8_t c = (addr >> 1) & 3, r = regbyte & 0x0F;
if (r < 3) {
uint8_t a6 = (c << 4) | r;
switch (a6) {
case 0x01:
this->grst_writes++;
ESP_LOGW(TAG, "!!!!! GRST WRITE %02X via @%02X reg byte %02X: soft reset of channels mask %X !!!!!", v, addr,
regbyte, v & 0x0F);
for (int i = 0; i < 4; i++)
if (v & (1 << i))
this->reset_channel_(i);
return; // self clearing
case 0x21:
this->gpdir_writes++;
this->glob[a6] = v;
this->show_gpio("dir write");
return;
case 0x31:
this->gpdat_writes++;
this->glob[a6] = v;
this->show_gpio("data write");
return;
case 0x00:
case 0x02:
case 0x10:
case 0x11:
this->glob[a6] = v;
return;
default:
this->reserved++;
ESP_LOGW(TAG, "write to reserved global %02X", a6);
return;
}
}
if (r == 3) {
this->spage[c] = v & 1;
return;
}
if (this->spage[c]) {
this->pg1[c][r] = v;
return;
}
if (r == 6) { // FCR: reset bits clear the FIFOs and do not stay set
if (v & 1)
this->rx[c].clear();
v &= 0x0C;
}
if (r == 0x0D) {
this->tx_bytes[c]++;
return;
}
if (r >= 9 && r <= 0x0C)
return; // read only
this->pg0[c][r] = v;
}
uint8_t reg_read_(uint8_t addr, uint8_t regbyte) {
uint8_t c = (addr >> 1) & 3, r = regbyte & 0x0F;
if (r < 3) {
uint8_t a6 = (c << 4) | r;
if (a6 == 0x01)
return 0;
if (a6 == 0x31) {
this->gpdat_reads++;
return this->gpdat_value();
}
return this->glob[a6];
}
if (r == 3)
return this->spage[c];
if (this->spage[c])
return this->pg1[c][r];
switch (r) {
case 0x09:
return 0; // TFCNT: the fake sends at once
case 0x0A:
return (uint8_t) this->rx[c].n; // RFCNT (256 reads 0)
case 0x0B:
return this->rx[c].n ? (1 << 3) : 0; // FSR: RFDAT
case 0x0C:
return 0; // LSR
case 0x0D:
return this->rx[c].pop(); // FDAT
default:
return this->pg0[c][r];
}
}
bool channel_on_(int c, uint8_t bit) const { return (this->glob[0] & (1 << c)) && (this->pg0[c][4] & bit); }
void inject_rx(int c, const char *s) {
if (!this->channel_on_(c, 1)) {
this->rx_dropped += strlen(s);
ESP_LOGW(TAG, "RX inject ch%d dropped %u bytes: receiver disabled (GENA=%02X SCR=%02X)", c, (unsigned) strlen(s),
this->glob[0], this->pg0[c][4]);
return;
}
for (const char *p = s; *p; p++)
this->rx[c].push((uint8_t) *p);
ESP_LOGI(TAG, "RX inject ch%d '%s' fifo=%u", c, s, this->rx[c].n);
}
void set_input(uint8_t pin, bool v) {
if (v)
this->gpio_in |= 1 << pin;
else
this->gpio_in &= ~(1 << pin);
this->show_gpio("input change");
}
ErrorCode write_readv(uint8_t address, const uint8_t *data, size_t size, uint8_t *out, size_t out_size) override {
if ((address & 0xF8) != this->base)
return esphome::i2c::ERROR_NOT_ACKNOWLEDGED;
if (size == 0 && out_size == 0)
return esphome::i2c::ERROR_OK;
this->n_trans++;
const uint8_t a = address & 7, c = (a >> 1) & 3;
if (a & 1) { // FIFO
if (size) {
char txt[48];
size_t i = 0;
for (; i < size && i < sizeof(txt) - 1; i++)
txt[i] = data[i] >= 0x20 && data[i] < 0x7F ? (char) data[i] : '.';
txt[i] = 0;
if (this->channel_on_(c, 2)) {
this->tx_bytes[c] += size;
ESP_LOGI(TAG, "#%u @%02X FIFO TX ch%u %u bytes '%s'", this->n_trans, address, c, (unsigned) size, txt);
} else {
this->tx_dropped += size;
ESP_LOGW(TAG, "#%u @%02X FIFO TX ch%u %u bytes '%s' DROPPED: transmitter disabled (GENA=%02X SCR=%02X)",
this->n_trans, address, c, (unsigned) size, txt, this->glob[0], this->pg0[c][4]);
}
}
if (out_size) {
for (size_t i = 0; i < out_size; i++)
out[i] = this->rx[c].pop();
ESP_LOGI(TAG, "#%u @%02X FIFO RX ch%u %u bytes, %u left", this->n_trans, address, c, (unsigned) out_size,
this->rx[c].n);
}
return esphome::i2c::ERROR_OK;
}
Kind k;
uint8_t regbyte;
if (out_size == 0)
k = size == 1 ? K_P : K_W, regbyte = data[0];
else if (size == 0)
k = K_R, regbyte = this->ptr[a];
else
k = K_WR, regbyte = data[0];
if (regbyte & 0xF0 && (++this->hi_bits <= 3 || k == K_W || k == K_P)) {
ESP_LOGW(TAG, "#%u @%02X %s reg byte %02X has bits above A3..A0; the chip sees register %X", this->n_trans,
address, KIND[k], regbyte, regbyte & 0x0F);
}
this->cnt[a][regbyte & 0x3F][k]++;
char nm_buf[24];
// the register name is only built for the paths that log
auto nm = [&]() {
this->name_(address, regbyte, nm_buf, sizeof(nm_buf));
return nm_buf;
};
if (k == K_P) {
if (this->fail_ptr) {
this->fail_ptr--;
this->injected++;
this->arm_retry_(address, regbyte);
ESP_LOGW(TAG, "#%u @%02X P r=%02X %s INJECTED NACK", this->n_trans, address, regbyte, nm());
return esphome::i2c::ERROR_NOT_ACKNOWLEDGED;
}
this->ptr[a] = regbyte;
if (this->trace) {
this->trace--;
ESP_LOGI(TAG, "#%u @%02X P r=%02X %s", this->n_trans, address, regbyte, nm());
}
return esphome::i2c::ERROR_OK;
}
if (k == K_W) {
this->ptr[a] = regbyte;
for (size_t i = 1; i < size; i++)
this->reg_write_(address, regbyte, data[i]);
if (this->trace)
this->trace--;
ESP_LOGI(TAG, "#%u @%02X W r=%02X %s = %02X", this->n_trans, address, regbyte, nm(), data[1]);
return esphome::i2c::ERROR_OK;
}
// read phase (R or WR)
this->read_phases++;
if (this->await_retry) {
this->await_retry = false;
if (address == this->await_addr && (regbyte == this->await_reg || k == K_R)) {
this->retries++;
ESP_LOGI(TAG, "#%u @%02X %s r=%02X %s RETRY after injected NACK", this->n_trans, address, KIND[k], regbyte,
nm());
} else {
this->no_retry++;
ESP_LOGW(TAG, "#%u @%02X %s r=%02X %s: no retry of the NACKed read", this->n_trans, address, KIND[k], regbyte,
nm());
}
}
if (k == K_WR)
this->ptr[a] = regbyte;
bool fail = false;
if (this->fail_reads) {
this->fail_reads--;
fail = true;
} else if (this->every_n && this->read_phases % this->every_n == 0) {
fail = true;
}
if (fail) {
this->injected++;
this->arm_retry_(address, regbyte);
memset(out, 0, out_size);
ESP_LOGW(TAG, "#%u @%02X %s r=%02X %s INJECTED NACK", this->n_trans, address, KIND[k], regbyte, nm());
return esphome::i2c::ERROR_NOT_ACKNOWLEDGED;
}
for (size_t i = 0; i < out_size; i++)
out[i] = this->reg_read_(address, regbyte);
uint8_t &last = this->last_read[a][regbyte & 0x3F];
bool changed = last != out[0];
last = out[0];
if (this->trace) {
this->trace--;
ESP_LOGI(TAG, "#%u @%02X %s r=%02X %s -> %02X", this->n_trans, address, KIND[k], regbyte, nm(), out[0]);
} else if (changed) {
ESP_LOGI(TAG, "#%u @%02X %s r=%02X %s -> %02X (changed)", this->n_trans, address, KIND[k], regbyte, nm(), out[0]);
}
return esphome::i2c::ERROR_OK;
}
void arm_retry_(uint8_t addr, uint8_t reg) {
this->await_retry = true;
this->await_addr = addr;
this->await_reg = reg;
}
void dump() {
ESP_LOGI(TAG,
"STATS trans=%u grst_writes=%u hi_bits=%u reserved=%u gpdir_w=%u gpdat_w=%u gpdat_r=%u tx=%u/%u "
"tx_dropped=%u rx_dropped=%u injected=%u retries=%u no_retry=%u",
this->n_trans, this->grst_writes, this->hi_bits, this->reserved, this->gpdir_writes, this->gpdat_writes,
this->gpdat_reads, this->tx_bytes[0], this->tx_bytes[1], this->tx_dropped, this->rx_dropped,
this->injected, this->retries, this->no_retry);
this->show_gpio("now");
for (int c = 0; c < 2; c++)
ESP_LOGI(TAG, "ch%d SCR=%02X LCR=%02X FCR=%02X BR=%02X%02X.%X SPAGE=%u", c, this->pg0[c][4], this->pg0[c][5],
this->pg0[c][6], this->pg1[c][4], this->pg1[c][5], this->pg1[c][6], this->spage[c]);
char nm[24];
for (int a = 0; a < 8; a++)
for (int r = 0; r < 64; r++) {
uint32_t *k = this->cnt[a][r];
if (k[0] | k[1] | k[2] | k[3]) {
this->name_(this->base | a, r, nm, sizeof(nm));
ESP_LOGI(TAG, "MAP @%02X r=%02X %-12s W=%u P=%u R=%u WR=%u", this->base | a, r, nm, k[0], k[1], k[2], k[3]);
}
}
}
};
static Bus bus;
} // namespace wkfake
+1 -3
View File
@@ -605,9 +605,7 @@ async def to_code(config: ConfigType) -> None:
cg.add(var.set_type(conf[CONF_TYPE]))
for conf in config[CONF_SELECTS]:
var = await select.new_select(
conf, options=conf[CONF_OPTIONS], fixed_options=True
)
var = await select.new_select(conf, options=conf[CONF_OPTIONS])
await cg.register_component(var, conf)
for conf in config[CONF_SENSORS]:
+1 -1
View File
@@ -41,5 +41,5 @@ CONFIG_SCHEMA = (
async def to_code(config: ConfigType) -> None:
var = await select.new_select(config, options=DUCO_MODE_OPTIONS, fixed_options=True)
var = await select.new_select(config, options=DUCO_MODE_OPTIONS)
await register_duco_device(var, config, config[CONF_DUCO_ID])
+15 -26
View File
@@ -94,7 +94,6 @@ UART_SELECTION_RP2040 = [USB_CDC, UART0, UART1]
UART_SELECTION_NRF52 = [USB_CDC, UART0]
# Read by device-builder
HARDWARE_UART_TO_UART_SELECTION = {
UART0: logger_ns.UART_SELECTION_UART0,
UART0_SWAP: logger_ns.UART_SELECTION_UART0_SWAP,
@@ -539,17 +538,17 @@ async def to_code(config: ConfigType) -> None:
cg.add(log.set_boot_log_level(tag, LOG_LEVELS[log_level]))
if CORE.is_esp32 or CORE.is_host:
cg.add(log.create_pthread_key())
if (hardware_uart := config.get(CONF_HARDWARE_UART)) is not None:
cg.add_define(f"USE_LOGGER_UART_SELECTION_{hardware_uart}")
cg.add_define(
"ESPHOME_LOGGER_UART_SELECTION",
cg.RawExpression(f"logger::UART_SELECTION_{hardware_uart}"),
)
# ESP8266 without serial logging never opens a port
cg.add_define(
"ESPHOME_LOGGER_UART_NAME",
"NONE" if CORE.is_esp8266 and baud_rate == 0 else hardware_uart,
)
# set_uart_selection() must be called before pre_setup() because
# pre_setup() switches on uart_ to decide which hardware to initialize
# (e.g. UART0 vs USB_SERIAL_JTAG). Without this, uart_ is still the
# default UART_SELECTION_UART0 and the wrong hardware gets initialized.
# uart_ is UART0 in C++ except on LibreTiny where it is DEFAULT; skip the
# setter when the config matches it.
cpp_default_uart = DEFAULT if CORE.is_libretiny else UART0
if (
hardware_uart := config.get(CONF_HARDWARE_UART)
) is not None and hardware_uart != cpp_default_uart:
cg.add(log.set_uart_selection(HARDWARE_UART_TO_UART_SELECTION[hardware_uart]))
# pre_setup() sets global_logger and must run before any other code
# that may call ESP_LOG* (e.g. setup_preferences contains ESP_LOGVV).
cg.add(log.pre_setup())
@@ -646,6 +645,7 @@ async def _late_logger_init(config: ConfigType) -> None:
add_idf_sdkconfig_option("CONFIG_ESP_CONSOLE_USB_CDC", True)
elif config[CONF_HARDWARE_UART] == USB_SERIAL_JTAG:
add_idf_sdkconfig_option("CONFIG_ESP_CONSOLE_USB_SERIAL_JTAG", True)
cg.add_define("USE_LOGGER_UART_SELECTION_USB_SERIAL_JTAG")
# Log V2 formats centrally in esp_log() (~4KB flash, ~180B RAM). 6.1
# adds CONSTRAINED_ENV_SAFE=n, keeping early/DRAM logs on
# esp_rom_printf instead of pulling ~1.2KB esp_rom_vprintf into IRAM.
@@ -663,12 +663,10 @@ async def _late_logger_init(config: ConfigType) -> None:
# --wrap: a strong definition collides; see logger_esp32.cpp
cg.add_build_flag("-Wl,--wrap=esp_log_format")
# Zephyr polls the USB port only when it logs there
if (
CORE.using_zephyr
and has_serial_logging
and config[CONF_HARDWARE_UART] == USB_CDC
if config.get(CONF_HARDWARE_UART) == USB_CDC and (
has_serial_logging or not CORE.is_nrf52
):
cg.add_define("USE_LOGGER_ZEPHYR_CDC_LOOP")
cg.add_define("USE_LOGGER_UART_SELECTION_USB_CDC")
# Define platform support flags for components that need auto-detection
try:
uart_selection(USB_SERIAL_JTAG)
@@ -886,15 +884,6 @@ def request_logger_level_listeners() -> None:
_request_log_listener_slot = cg.slot_counter("ESPHOME_LOG_MAX_LISTENERS")
def request_console() -> None:
"""Compile in Logger::console_read() and console_write(); call from to_code.
On ESP32 a USB_CDC logger only has a console where ESP-IDF sets CONFIG_ESP_CONSOLE_USB_CDC (targets with
USB OTG); elsewhere, such as the C3, C6 or H2, both do nothing. Host and Zephyr have no console API.
"""
cg.add_define("USE_LOGGER_CONSOLE")
def request_log_listener() -> None:
"""Reserve a log listener slot; call once per listener from to_code."""
cg.add_define("USE_LOG_LISTENERS")
+1 -22
View File
@@ -114,26 +114,6 @@ struct LogBuffer {
this->finalize_();
}
#endif
// Writes a message that needs no formatting, truncated and stripped of trailing newlines as format_body() does
void HOT write_unformatted_body(const char *text, size_t len) {
if (!this->full_()) {
const uint16_t limit = this->remaining_() - 1; // vsnprintf() keeps the last byte for the null terminator
const uint16_t copy_len = (len < limit) ? static_cast<uint16_t>(len) : limit;
memcpy(this->current_(), text, copy_len);
this->pos += copy_len;
this->strip_trailing_newlines_();
}
this->finalize_();
}
// write_unformatted_body() for a message in flash
inline void HOT ESPHOME_ALWAYS_INLINE write_unformatted_body_p(ESPHOME_PGM_P text) {
if (!this->full_()) {
char *const start = this->current_();
this->pos = static_cast<uint16_t>(progmem_copy_str(start, start + this->remaining_() - 1, text) - this->data);
this->strip_trailing_newlines_();
}
this->finalize_();
}
static constexpr uint16_t ANSI_COLOR_LEN = 7; // "\033[{bold};3{color}m" written by write_header for level != 0
static constexpr uint16_t ANSI_RESET_LEN = 4; // "\033[0m"
// Largest replay header: color plus "@4294967295ms "
@@ -181,8 +161,7 @@ struct LogBuffer {
this->pos += ANSI_RESET_LEN;
}
}
// Forced inline: an outlined copy slows every formatted line
inline void ESPHOME_ALWAYS_INLINE strip_trailing_newlines_() {
void strip_trailing_newlines_() {
while (this->pos > 0 && this->data[this->pos - 1] == '\n')
this->pos--;
}
+8 -75
View File
@@ -19,15 +19,6 @@ namespace esphome::logger {
ESPHOME_LOG_TAG(TAG, "logger");
#ifdef USE_LOGGER_DEFER_UNDER_LWIP_LOCK
// lwIP code run under the core lock (tcpip_api_call, netif callbacks) can log. A listener that writes to a socket would
// take that lock again and deadlock, so such a line is queued for loop() instead. It can print after later inline
// lines, and is lost if loop() never runs again (safe_reboot()).
static inline bool HOT ESPHOME_ALWAYS_INLINE under_lwip_core_lock() {
return sys_thread_tcpip(LWIP_CORE_LOCK_QUERY_HOLDER) && sys_thread_tcpip(LWIP_CORE_IS_TCPIP_INITIALIZED);
}
#endif
#if defined(USE_ESP32) || defined(USE_HOST) || defined(USE_LIBRETINY) || defined(USE_ZEPHYR)
// Implementation for multi-threaded platforms (ESP32 with FreeRTOS, Host with pthreads, LibreTiny with FreeRTOS,
// Zephyr) Main thread/task always uses direct buffer access for console output and callbacks
@@ -60,7 +51,11 @@ void HOT Logger::log_vprintf_(uint8_t level, const char *tag, int line, const ch
// Pass nullptr for thread_name since we already know this is the main task
if (is_main_task && !this->main_task_recursion_guard_) [[likely]] {
#ifdef USE_LOGGER_DEFER_UNDER_LWIP_LOCK
if (under_lwip_core_lock()) [[unlikely]] {
// lwIP code run under the core lock (tcpip_api_call, netif callbacks) can log. A listener that writes
// to a socket would take that lock again and deadlock, so queue the message for loop() instead. Such lines
// can print after later inline ones, and are lost if loop() never runs again (safe_reboot()).
if (sys_thread_tcpip(LWIP_CORE_LOCK_QUERY_HOLDER) && sys_thread_tcpip(LWIP_CORE_IS_TCPIP_INITIALIZED))
[[unlikely]] {
this->log_vprintf_non_main_thread_(level, tag, line, format, args, nullptr);
return;
}
@@ -164,49 +159,6 @@ void Logger::log_vprintf_(uint8_t level, const char *tag, int line, const __Flas
}
#endif // USE_ESP8266
// Not shared with log_message_to_buffer_and_send_(): sharing it slows formatted lines
inline void HOT ESPHOME_ALWAYS_INLINE Logger::send_unformatted_line_(uint8_t level, const char *tag, LogBuffer &buf) {
#ifdef USE_LOGGER_BOOT_LOG_BUFFER
if (!this->boot_log_done_)
this->boot_log_capture_(level, tag, buf);
#endif
this->notify_listeners_(level, tag, buf);
this->write_log_buffer_to_console_(buf);
}
bool HOT Logger::log_str_(uint8_t level, const char *tag, int line, const char *msg) {
if (level > this->level_for(tag))
return true;
// Check the task first: the guard belongs to the main task and must not be read from other threads
if (!this->is_main_task_())
return false;
if (this->main_task_recursion_guard_)
return true;
#ifdef USE_LOGGER_DEFER_UNDER_LWIP_LOCK
// The formatting path queues it
if (under_lwip_core_lock()) [[unlikely]]
return false;
#endif
RecursionGuard guard(this->main_task_recursion_guard_);
LogBuffer buf{this->tx_buffer_, ESPHOME_LOGGER_TX_BUFFER_SIZE};
buf.write_header(level, tag, line, nullptr);
buf.write_unformatted_body(msg, strlen(msg));
this->send_unformatted_line_(level, tag, buf);
return true;
}
#ifdef USE_ESP8266
void Logger::log_str_(uint8_t level, const char *tag, int line, const __FlashStringHelper *msg) {
if (level > this->level_for(tag) || this->main_task_recursion_guard_)
return;
RecursionGuard guard(this->main_task_recursion_guard_);
LogBuffer buf{this->tx_buffer_, ESPHOME_LOGGER_TX_BUFFER_SIZE};
buf.write_header(level, tag, line, nullptr);
buf.write_unformatted_body_p(reinterpret_cast<PGM_P>(msg));
this->send_unformatted_line_(level, tag, buf);
}
#endif // USE_ESP8266
#if defined(USE_LOGGER_RUNTIME_TAG_LEVELS) || defined(USE_LOGGER_BOOT_LOG_PER_TAG_LEVELS)
// Level for tag from a per-tag map, or fallback when the tag has none; tags may live in flash
static inline uint8_t tag_level(const std::map<const char *, uint8_t, CStrCompare> &levels, const char *tag,
@@ -248,10 +200,10 @@ Logger::Logger(uint32_t baud_rate) : baud_rate_(baud_rate) {
#endif
}
#if defined(USE_ESPHOME_TASK_LOG_BUFFER) || defined(USE_LOGGER_ZEPHYR_CDC_LOOP)
#if defined(USE_ESPHOME_TASK_LOG_BUFFER) || (defined(USE_ZEPHYR) && defined(USE_LOGGER_UART_SELECTION_USB_CDC))
void Logger::loop() {
this->process_messages_();
#ifdef USE_LOGGER_ZEPHYR_CDC_LOOP
#if defined(USE_ZEPHYR) && defined(USE_LOGGER_UART_SELECTION_USB_CDC)
this->cdc_loop_();
#endif
}
@@ -278,7 +230,7 @@ void Logger::process_messages_() {
}
}
// Zephyr with USB CDC keeps the loop running for cdc_loop_()
#ifndef USE_LOGGER_ZEPHYR_CDC_LOOP
#if !(defined(USE_ZEPHYR) && defined(USE_LOGGER_UART_SELECTION_USB_CDC))
else {
// A concurrent enable_loop_soon_any_context() still wakes the next iteration
this->disable_loop();
@@ -371,25 +323,6 @@ static const LogString *get_log_level_str(uint8_t level) {
return LogLevelStrings::get_log_str(level, LogLevelStrings::LAST_INDEX);
}
#ifndef USE_HOST
const LogString *Logger::get_uart_selection_() {
#ifdef USE_LOGGER_UART_SELECTION_DEFAULT
// LibreTiny resolves DEFAULT to the board's port only when serial logging is on
if (this->baud_rate_ > 0) {
switch (this->get_uart()) {
case UART_SELECTION_UART0:
return LOG_STR("UART0");
case UART_SELECTION_UART1:
return LOG_STR("UART1");
default:
return LOG_STR("UART2");
}
}
#endif
return LOG_STR(ESPHOME_LOGGER_UART_NAME);
}
#endif
void Logger::dump_config() {
ESP_LOGCONFIG(TAG,
"Logger:\n"
+11 -57
View File
@@ -124,9 +124,6 @@ enum UARTSelection : uint8_t {
UART_SELECTION_UART0_SWAP,
#endif // USE_ESP8266
};
// The port chosen by hardware_uart, fixed at build time
static constexpr UARTSelection CONFIGURED_UART_SELECTION = ESPHOME_LOGGER_UART_SELECTION;
#endif // USE_ESP32 || USE_ESP8266 || USE_RP2 || USE_LIBRETINY || USE_ZEPHYR
/**
@@ -149,7 +146,7 @@ static constexpr UARTSelection CONFIGURED_UART_SELECTION = ESPHOME_LOGGER_UART_S
class Logger final : public Component {
public:
explicit Logger(uint32_t baud_rate);
#if defined(USE_ESPHOME_TASK_LOG_BUFFER) || defined(USE_LOGGER_ZEPHYR_CDC_LOOP)
#if defined(USE_ESPHOME_TASK_LOG_BUFFER) || (defined(USE_ZEPHYR) && defined(USE_LOGGER_UART_SELECTION_USB_CDC))
void loop() override;
#endif
#if defined(USE_RP2_CRASH_HANDLER) && defined(USE_LOGGER_UART_SELECTION_USB_CDC)
@@ -163,11 +160,11 @@ class Logger final : public Component {
#endif
#ifdef USE_ESP32
/// UART_NUM_0 when logging to USB
constexpr uart_port_t get_uart_num() const {
uart_port_t get_uart_num() const {
#if defined(USE_LOGGER_UART_SELECTION_USB_CDC) || defined(USE_LOGGER_UART_SELECTION_USB_SERIAL_JTAG)
return UART_NUM_0;
#else
return static_cast<uart_port_t>(CONFIGURED_UART_SELECTION);
return static_cast<uart_port_t>(this->uart_);
#endif
}
#endif
@@ -175,35 +172,9 @@ class Logger final : public Component {
void create_pthread_key() { pthread_key_create(&this->log_recursion_key_, nullptr); }
#endif
#if defined(USE_ESP32) || defined(USE_ESP8266) || defined(USE_RP2) || defined(USE_LIBRETINY) || defined(USE_ZEPHYR)
void set_uart_selection(UARTSelection uart_selection) { this->uart_ = uart_selection; }
/// Get the UART used by the logger.
#ifdef USE_LIBRETINY
// DEFAULT resolves to the board's default port
constexpr UARTSelection get_uart() const {
return CONFIGURED_UART_SELECTION == UART_SELECTION_DEFAULT ? static_cast<UARTSelection>(LT_UART_DEFAULT_SERIAL + 1)
: CONFIGURED_UART_SELECTION;
}
#else
constexpr UARTSelection get_uart() const { return CONFIGURED_UART_SELECTION; }
#endif
#endif
#ifdef USE_LOGGER_CONSOLE
// Raw bytes on the console port, without log framing; request with logger.request_console()
// console_write() returns the bytes written: a UART write blocks until all are queued, but a USB write can stop
// short when the host is not reading (USB Serial/JTAG gives up after 20 ms)
#if defined(USE_ARDUINO) && !defined(USE_ESP32)
size_t console_write(const uint8_t *data, size_t len) { return this->hw_serial_->write(data, len); }
bool console_read(uint8_t &byte) {
int c = this->hw_serial_->read();
if (c < 0)
return false;
byte = c;
return true;
}
#elif defined(USE_ESP32)
size_t console_write(const uint8_t *data, size_t len);
/// Non-blocking read of one byte; false if none is waiting.
bool console_read(uint8_t &byte);
#endif
UARTSelection get_uart() const { return this->uart_; }
#endif
/// Set the default log level for this logger.
@@ -252,12 +223,6 @@ class Logger final : public Component {
// NOLINTNEXTLINE(readability-identifier-naming)
void log_vprintf_(uint8_t level, const char *tag, int line, const __FlashStringHelper *format, va_list args);
#endif
// Copies a line with no '%' without formatting; false means the caller must format it
bool log_str_(uint8_t level, const char *tag, int line, const char *msg); // NOLINT
#ifdef USE_ESP8266
// NOLINTNEXTLINE(readability-identifier-naming)
void log_str_(uint8_t level, const char *tag, int line, const __FlashStringHelper *msg);
#endif
protected:
// RAII guard for recursion flags - sets flag on construction, clears on destruction
@@ -280,7 +245,7 @@ class Logger final : public Component {
void log_vprintf_non_main_thread_(uint8_t level, const char *tag, int line, const char *format, va_list args,
const char *thread_name);
#endif
#ifdef USE_LOGGER_ZEPHYR_CDC_LOOP
#if defined(USE_ZEPHYR) && defined(USE_LOGGER_UART_SELECTION_USB_CDC)
void cdc_loop_();
#endif
void process_messages_();
@@ -327,7 +292,7 @@ class Logger final : public Component {
}
// Helper to write log buffer to console if logging is enabled
inline void HOT ESPHOME_ALWAYS_INLINE write_log_buffer_to_console_(LogBuffer &buf) {
inline void HOT write_log_buffer_to_console_(LogBuffer &buf) {
if (this->baud_rate_ > 0)
this->write_to_console_(buf);
}
@@ -356,21 +321,6 @@ class Logger final : public Component {
this->write_log_buffer_to_console_(buf);
}
inline void send_unformatted_line_(uint8_t level, const char *tag, LogBuffer &buf);
// True on the task that runs the main loop, and always on single-task platforms
inline bool HOT is_main_task_() const {
#if defined(USE_ESP32) || defined(USE_LIBRETINY)
return xTaskGetCurrentTaskHandle() == this->main_task_;
#elif defined(USE_ZEPHYR)
return k_current_get() == this->main_task_;
#elif defined(USE_HOST)
return pthread_equal(pthread_self(), this->main_thread_);
#else
return true;
#endif
}
#ifdef USE_ESPHOME_TASK_LOG_BUFFER
// Helper to format a pre-formatted message from the task log buffer and notify listeners
inline void HOT format_buffered_message_and_notify_(uint8_t level, const char *tag, uint16_t line,
@@ -440,6 +390,10 @@ class Logger final : public Component {
bool boot_log_done_{false}; // Set on first replay; stops further capture
#endif
uint8_t current_level_{ESPHOME_LOG_LEVEL_VERY_VERBOSE};
#if defined(USE_ESP32) || defined(USE_ESP8266) || defined(USE_RP2) || defined(USE_LIBRETINY) || defined(USE_ZEPHYR)
// 0 is UART0, or DEFAULT on LibreTiny; must match cpp_default_uart in __init__.py
UARTSelection uart_{};
#endif
bool main_task_recursion_guard_{false}; // Guards tx_buffer_ on the main loop
#ifdef USE_LIBRETINY
bool non_main_task_recursion_guard_{false}; // Shared guard for all non-main tasks on LibreTiny
+38 -36
View File
@@ -17,10 +17,6 @@
#include <driver/usb_serial_jtag_vfs.h>
#endif
#endif
// The logger accepts USB_CDC on targets without USB OTG (C3, C6, H2, ...), where IDF has no CDC console
#if defined(USE_LOGGER_CONSOLE) && defined(USE_LOGGER_UART_SELECTION_USB_CDC) && defined(CONFIG_ESP_CONSOLE_USB_CDC)
#include <esp_private/usb_console.h>
#endif
#if defined(CONFIG_PM_ENABLE) && defined(CONFIG_FREERTOS_USE_TICKLESS_IDLE) && \
(ESP_IDF_VERSION >= ESP_IDF_VERSION_VAL(6, 0, 0))
#include "esp_sleep.h"
@@ -77,7 +73,6 @@ static void init_usb_serial_jtag() {
}
#endif
#if !defined(USE_LOGGER_UART_SELECTION_USB_CDC) && !defined(USE_LOGGER_UART_SELECTION_USB_SERIAL_JTAG)
static void init_uart(uart_port_t uart_num, uint32_t baud_rate, int tx_buffer_size) {
uart_config_t uart_config{};
uart_config.baud_rate = (int) baud_rate;
@@ -102,7 +97,6 @@ static void init_uart(uart_port_t uart_num, uint32_t baud_rate, int tx_buffer_si
esp_sleep_set_console_uart_handling_mode(ESP_SLEEP_ALWAYS_FLUSH_UART);
#endif
}
#endif
// get_uart_num() and pre_setup() cast the selection straight to a port
static_assert(static_cast<int>(UART_SELECTION_UART0) == static_cast<int>(UART_NUM_0) &&
@@ -113,11 +107,26 @@ static_assert(static_cast<int>(UART_SELECTION_UART2) == static_cast<int>(UART_NU
void Logger::pre_setup() {
if (this->baud_rate_ > 0) {
#ifdef USE_LOGGER_UART_SELECTION_USB_SERIAL_JTAG
init_usb_serial_jtag();
#elif !defined(USE_LOGGER_UART_SELECTION_USB_CDC)
init_uart(this->get_uart_num(), this->baud_rate_, ESPHOME_LOGGER_TX_BUFFER_SIZE);
switch (this->uart_) {
case UART_SELECTION_UART0:
case UART_SELECTION_UART1:
#ifdef USE_ESP32_VARIANT_ESP32
case UART_SELECTION_UART2:
#endif
init_uart(static_cast<uart_port_t>(this->uart_), this->baud_rate_, ESPHOME_LOGGER_TX_BUFFER_SIZE);
break;
#ifdef USE_LOGGER_USB_CDC
case UART_SELECTION_USB_CDC:
break;
#endif
#ifdef USE_LOGGER_USB_SERIAL_JTAG
case UART_SELECTION_USB_SERIAL_JTAG:
#ifdef USE_LOGGER_UART_SELECTION_USB_SERIAL_JTAG
init_usb_serial_jtag();
#endif
break;
#endif
}
}
global_logger = this;
@@ -129,36 +138,29 @@ void Logger::pre_setup() {
#endif
}
#ifdef USE_LOGGER_CONSOLE
size_t Logger::console_write(const uint8_t *data, size_t len) {
int written = 0;
#if defined(USE_LOGGER_UART_SELECTION_USB_CDC)
#ifdef CONFIG_ESP_CONSOLE_USB_CDC // keep: not set without USB OTG, see the include above
written = esp_usb_console_write_buf(reinterpret_cast<const char *>(data), len);
const LogString *Logger::get_uart_selection_() {
switch (this->uart_) {
case UART_SELECTION_UART0:
return LOG_STR("UART0");
case UART_SELECTION_UART1:
return LOG_STR("UART1");
#ifdef USE_ESP32_VARIANT_ESP32
case UART_SELECTION_UART2:
return LOG_STR("UART2");
#endif
#elif defined(USE_LOGGER_UART_SELECTION_USB_SERIAL_JTAG)
written = usb_serial_jtag_write_bytes(data, len, pdMS_TO_TICKS(20));
#else
written = uart_write_bytes(this->get_uart_num(), data, len);
#ifdef USE_LOGGER_USB_CDC
case UART_SELECTION_USB_CDC:
return LOG_STR("USB_CDC");
#endif
return written > 0 ? written : 0;
#ifdef USE_LOGGER_USB_SERIAL_JTAG
case UART_SELECTION_USB_SERIAL_JTAG:
return LOG_STR("USB_SERIAL_JTAG");
#endif
default:
return LOG_STR("UNKNOWN");
}
}
bool Logger::console_read(uint8_t &byte) {
#if defined(USE_LOGGER_UART_SELECTION_USB_CDC)
#ifdef CONFIG_ESP_CONSOLE_USB_CDC // keep: not set without USB OTG, see the include above
return esp_usb_console_available_for_read() && esp_usb_console_read_buf(reinterpret_cast<char *>(&byte), 1) == 1;
#else
return false;
#endif
#elif defined(USE_LOGGER_UART_SELECTION_USB_SERIAL_JTAG)
return usb_serial_jtag_read_bytes(&byte, 1, 0) > 0;
#else
return uart_read_bytes(this->get_uart_num(), &byte, 1, 0) > 0;
#endif
}
#endif
#ifdef USE_CRASH_HANDLER
bool crash_log_pending() { return esp32::crash_handler_has_data(); }
void log_crash_to_client() {
+16 -3
View File
@@ -14,9 +14,9 @@ void Logger::pre_setup() {
#if defined(USE_ESP8266_LOGGER_SERIAL)
this->hw_serial_ = &Serial;
Serial.begin(this->baud_rate_);
#ifdef USE_LOGGER_UART_SELECTION_UART0_SWAP
Serial.swap();
#endif
if (this->uart_ == UART_SELECTION_UART0_SWAP) {
Serial.swap();
}
Serial.setDebugOutput(ESPHOME_LOG_LEVEL >= ESPHOME_LOG_LEVEL_VERBOSE);
#elif defined(USE_ESP8266_LOGGER_SERIAL1)
this->hw_serial_ = &Serial1;
@@ -35,6 +35,19 @@ void Logger::pre_setup() {
#endif
}
const LogString *Logger::get_uart_selection_() {
#if defined(USE_ESP8266_LOGGER_SERIAL)
if (this->uart_ == UART_SELECTION_UART0_SWAP) {
return LOG_STR("UART0_SWAP");
}
return LOG_STR("UART0");
#elif defined(USE_ESP8266_LOGGER_SERIAL1)
return LOG_STR("UART1");
#else
return LOG_STR("NONE");
#endif
}
#ifdef USE_CRASH_HANDLER
bool crash_log_pending() { return esp8266::crash_handler_has_data(); }
void log_crash_to_client() { esp8266::crash_handler_log(); }
+51 -20
View File
@@ -12,28 +12,45 @@ ESPHOME_LOG_TAG(TAG, "logger");
void Logger::pre_setup() {
if (this->baud_rate_ > 0) {
#if defined(USE_LOGGER_UART_SELECTION_UART0) && LT_HW_UART0
this->hw_serial_ = &Serial0;
libretiny::ensure_serial_handle(0);
Serial0.begin(this->baud_rate_);
#elif defined(USE_LOGGER_UART_SELECTION_UART1) && LT_HW_UART1
this->hw_serial_ = &Serial1;
libretiny::ensure_serial_handle(1);
Serial1.begin(this->baud_rate_);
#elif defined(USE_LOGGER_UART_SELECTION_UART2) && LT_HW_UART2
this->hw_serial_ = &Serial2;
libretiny::ensure_serial_handle(2);
Serial2.begin(this->baud_rate_);
#else
this->hw_serial_ = &Serial;
Serial.begin(this->baud_rate_);
if (CONFIGURED_UART_SELECTION != UART_SELECTION_DEFAULT) {
ESP_LOGW(TAG, " The chosen logger UART port is not available on this board."
"The default port was used instead.");
}
switch (this->uart_) {
#if LT_HW_UART0
case UART_SELECTION_UART0:
this->hw_serial_ = &Serial0;
libretiny::ensure_serial_handle(0);
Serial0.begin(this->baud_rate_);
break;
#endif
#if LT_HW_UART1
case UART_SELECTION_UART1:
this->hw_serial_ = &Serial1;
libretiny::ensure_serial_handle(1);
Serial1.begin(this->baud_rate_);
break;
#endif
#if LT_HW_UART2
case UART_SELECTION_UART2:
this->hw_serial_ = &Serial2;
libretiny::ensure_serial_handle(2);
Serial2.begin(this->baud_rate_);
break;
#endif
default:
this->hw_serial_ = &Serial;
Serial.begin(this->baud_rate_);
if (this->uart_ != UART_SELECTION_DEFAULT) {
ESP_LOGW(TAG, " The chosen logger UART port is not available on this board."
"The default port was used instead.");
}
break;
}
// change lt_log() port to match default Serial
lt_log_set_port(this->get_uart() - 1);
if (this->uart_ == UART_SELECTION_DEFAULT) {
this->uart_ = (UARTSelection) (LT_UART_DEFAULT_SERIAL + 1);
lt_log_set_port(LT_UART_DEFAULT_SERIAL);
} else {
lt_log_set_port(this->uart_ - 1);
}
}
global_logger = this;
@@ -43,6 +60,20 @@ void Logger::pre_setup() {
#endif
}
const LogString *Logger::get_uart_selection_() {
switch (this->uart_) {
case UART_SELECTION_DEFAULT:
return LOG_STR("DEFAULT");
case UART_SELECTION_UART0:
return LOG_STR("UART0");
case UART_SELECTION_UART1:
return LOG_STR("UART1");
case UART_SELECTION_UART2:
default:
return LOG_STR("UART2");
}
}
#ifdef USE_LIBRETINY_CRASH_HANDLER
bool crash_log_pending() { return libretiny::crash_handler_has_data(); }
void log_crash_to_client() { libretiny::crash_handler_log(); }
+27 -10
View File
@@ -26,16 +26,20 @@ void Logger::setup() {
void Logger::pre_setup() {
if (this->baud_rate_ > 0) {
#if defined(USE_LOGGER_UART_SELECTION_USB_CDC)
this->hw_serial_ = &Serial;
Serial.begin(this->baud_rate_);
#elif defined(USE_LOGGER_UART_SELECTION_UART1)
this->hw_serial_ = &Serial2;
Serial2.begin(this->baud_rate_);
#else
this->hw_serial_ = &Serial1;
Serial1.begin(this->baud_rate_);
#endif
switch (this->uart_) {
case UART_SELECTION_UART0:
this->hw_serial_ = &Serial1;
Serial1.begin(this->baud_rate_);
break;
case UART_SELECTION_UART1:
this->hw_serial_ = &Serial2;
Serial2.begin(this->baud_rate_);
break;
case UART_SELECTION_USB_CDC:
this->hw_serial_ = &Serial;
Serial.begin(this->baud_rate_);
break;
}
}
global_logger = this;
ESP_LOGI(TAG, "Log initialized");
@@ -45,6 +49,19 @@ void Logger::pre_setup() {
#endif
}
const LogString *Logger::get_uart_selection_() {
switch (this->uart_) {
case UART_SELECTION_UART0:
return LOG_STR("UART0");
case UART_SELECTION_UART1:
return LOG_STR("UART1");
case UART_SELECTION_USB_CDC:
return LOG_STR("USB_CDC");
default:
return LOG_STR("UNKNOWN");
}
}
#ifdef USE_CRASH_HANDLER
bool crash_log_pending() { return rp2::crash_handler_has_data(); }
void log_crash_to_client() { rp2::crash_handler_log(); }
+34 -13
View File
@@ -42,7 +42,7 @@ __attribute__((section(".noinit"))) struct {
ESPHOME_LOG_TAG(TAG, "logger");
#ifdef USE_LOGGER_ZEPHYR_CDC_LOOP
#ifdef USE_LOGGER_UART_SELECTION_USB_CDC
void Logger::cdc_loop_() {
if (this->uart_dev_ == nullptr) {
return;
@@ -75,19 +75,25 @@ void Logger::pre_setup() {
#ifdef CONFIG_SERIAL
if (this->baud_rate_ > 0) {
const struct device *uart_dev = nullptr;
#if defined(USE_LOGGER_UART_SELECTION_USB_CDC)
switch (this->uart_) {
case UART_SELECTION_UART0: // NOLINT(bugprone-branch-clone)
uart_dev = DEVICE_DT_GET_OR_NULL(DT_NODELABEL(ESPHOME_LOGGER_UART0_NODE));
break;
case UART_SELECTION_UART1:
uart_dev = DEVICE_DT_GET_OR_NULL(DT_NODELABEL(uart1));
break;
#ifdef USE_LOGGER_USB_CDC
case UART_SELECTION_USB_CDC:
#if defined(CONFIG_USB_DEVICE_STACK) || defined(CONFIG_USB_DEVICE_STACK_NEXT)
uart_dev = DEVICE_DT_GET_OR_NULL(DT_NODELABEL(cdc_acm_uart0));
if (device_is_ready(uart_dev)) {
// Nothing can log this early, and the logger has no output without USB anyway
zephyr::usb_device_enable();
uart_dev = DEVICE_DT_GET_OR_NULL(DT_NODELABEL(cdc_acm_uart0));
if (device_is_ready(uart_dev)) {
// Nothing can log this early, and the logger has no output without USB anyway
zephyr::usb_device_enable();
}
#endif
break;
#endif
}
#endif
#elif defined(USE_LOGGER_UART_SELECTION_UART1)
uart_dev = DEVICE_DT_GET_OR_NULL(DT_NODELABEL(uart1));
#else
uart_dev = DEVICE_DT_GET_OR_NULL(DT_NODELABEL(ESPHOME_LOGGER_UART0_NODE));
#endif
if (device_is_ready(uart_dev)) {
this->uart_dev_ = uart_dev;
#if defined(USE_LOGGER_WAIT_FOR_CDC) && defined(USE_LOGGER_UART_SELECTION_USB_CDC)
@@ -111,7 +117,7 @@ void Logger::pre_setup() {
this->dump_crash_();
#endif
// Nobody reads a USB port this early; cdc_loop_() reports the dump once the port is opened
#if defined(USE_CRASH_HANDLER) && !defined(USE_LOGGER_ZEPHYR_CDC_LOOP)
#if defined(USE_CRASH_HANDLER) && !defined(USE_LOGGER_UART_SELECTION_USB_CDC)
// Without a UART (baud_rate: 0, or one that did not come up) nothing reaches the console
if (this->uart_dev_ != nullptr)
log_crash_to_client();
@@ -133,6 +139,21 @@ void HOT Logger::write_msg_(const char *msg, uint16_t len) {
}
}
const LogString *Logger::get_uart_selection_() {
switch (this->uart_) {
case UART_SELECTION_UART0:
return LOG_STR("UART0");
case UART_SELECTION_UART1:
return LOG_STR("UART1");
#ifdef USE_LOGGER_USB_CDC
case UART_SELECTION_USB_CDC:
return LOG_STR("USB_CDC");
#endif
default:
return LOG_STR("UNKNOWN");
}
}
static constexpr uint8_t REASON_BUF_SIZE = 32;
static const char *reason_to_str(unsigned int reason, char *buf) {
+1 -1
View File
@@ -34,6 +34,6 @@ async def to_code(config: ConfigType) -> None:
levels = list(LOG_LEVELS)
index = levels.index(CORE.data[DOMAIN][CONF_LEVEL])
levels = levels[: index + 1]
var = await select.new_select(config, options=levels, fixed_options=True)
var = await select.new_select(config, options=levels)
await register_parented(var, parent)
await register_component(var, config)
@@ -103,9 +103,7 @@ async def to_code(config: ConfigType) -> None:
)
await cg.register_component(var, config)
await select.register_select(
var, config, options=list(options_map.keys()), fixed_options=True
)
await select.register_select(var, config, options=list(options_map.keys()))
parent = await cg.get_variable(config[CONF_MODBUS_CONTROLLER_ID])
cg.add(parent.add_sensor_item(var))
+23 -32
View File
@@ -56,8 +56,7 @@ static constexpr uint8_t PIN_MAX_UNCONFIRMED_SENDS = 2;
// The attachment is polled every 4 s; the signal and operator queries only every 4th poll
static constexpr uint32_t ATTACH_POLL_MS = 4000;
static constexpr uint8_t ATTACH_DIAG_EVERY = 4;
static constexpr int RADIO_STATE_MINIMUM = 0; // CFUN=0: radio off and the SIM powered down
static constexpr int RADIO_STATE_RF_OFF = 4; // CFUN=4: transmit and receive off, SIM still powered
static constexpr int RADIO_STATE_RF_OFF = 4; // CFUN=4: transmit and receive off, SIM still powered
static constexpr uint8_t UART_EVENT_QUEUE_SIZE = 10;
static constexpr uint32_t BAUD_PREF_HASH = 76007670UL;
static constexpr uint32_t PIN_PREF_HASH = 76007671UL;
@@ -671,28 +670,22 @@ ModemState ModemComponent::handle_state_enabling_() {
this->set_fault_(ModemFault::MODEM_FAULT_TEARDOWN);
return this->retry_(ModemState::MODEM_STATE_ENABLING, this->next_probe_delay_());
}
this->probe_backoff_.store(0, std::memory_order_relaxed);
// esp_modem keeps the last line before OK as the reply, which can be the echoed command
if (this->dce_->set_echo(false) != command_result::OK) {
ESP_LOGW(TAG, "Failed to disable command echo");
}
// The SIM7670G only uses an APN written at CFUN=0 (else the attach fails with an ESM failure); CFUN=0 also drops a
// session left by a warm reboot. A modem that refuses gets the APN after the SIM check instead, as before.
esp_modem::PdpContext pdp(this->apn_);
this->apn_pending_ = this->dce_->set_radio_state(RADIO_STATE_MINIMUM) != command_result::OK ||
this->dce_->set_pdp_context(pdp) != command_result::OK;
if (this->apn_pending_) {
ESP_LOGW(TAG, "Failed to set the APN with the SIM powered down; setting it after the SIM check");
int radio_state = 0;
if (this->dce_->get_radio_state(radio_state) != command_result::OK) {
ESP_LOGW(TAG, "Failed to read the radio state; skipping the radio off step");
} else if (radio_state != RADIO_STATE_RF_OFF) {
// Drops a session left by a warm reboot; unlike CFUN=0, which powers the SIM down on the SIM7600, CFUN=4
// keeps the SIM readable for the PIN and the APN. A modem that refuses is used as it is.
if (this->dce_->set_radio_state(RADIO_STATE_RF_OFF) != command_result::OK) {
ESP_LOGW(TAG, "Failed to turn the radio off");
}
}
// CFUN=4 powers the SIM up with the radio still off; a modem without it needs CFUN=1 to bring the SIM back
if (this->dce_->set_radio_state(RADIO_STATE_RF_OFF) != command_result::OK &&
this->dce_->set_radio_state(1) != command_result::OK) {
ESP_LOGW(TAG, "Failed to power the SIM up");
this->set_fault_(ModemFault::MODEM_FAULT_SETUP);
this->modem_synced_ = true; // it answers, so a disable() still tears it down
return this->retry_(ModemState::MODEM_STATE_ENABLING, this->next_probe_delay_());
}
this->probe_backoff_.store(0, std::memory_order_relaxed);
if (this->baud_rate_ != 0 && this->baud_rate_ != this->current_baud_rate_) {
ESP_LOGD(TAG, "Setting baud rate: %d -> %d", this->current_baud_rate_, this->baud_rate_);
@@ -759,7 +752,7 @@ ModemState ModemComponent::handle_state_connecting_() {
return ModemState::MODEM_STATE_ENABLING;
}
}
// ENABLING powered the SIM up (CFUN=4, or CFUN=1 on a modem without it), so the SIM answers
// ENABLING left the radio at CFUN=4, where the SIM answers but the modem cannot attach yet
switch (this->prepare_sim_()) {
case SimResult::SIM_RESULT_READY:
break;
@@ -776,14 +769,13 @@ ModemState ModemComponent::handle_state_connecting_() {
this->target_state_.store(ModemState::MODEM_STATE_DISABLED);
return ModemState::MODEM_STATE_DISCONNECTING;
}
if (this->apn_pending_) {
esp_modem::PdpContext pdp(this->apn_);
if (this->dce_->set_pdp_context(pdp) != command_result::OK) {
ESP_LOGW(TAG, "Failed to set the APN");
this->set_fault_(ModemFault::MODEM_FAULT_SETUP);
return this->retry_(ModemState::MODEM_STATE_CONNECTING);
}
this->apn_pending_ = false;
// The APN has to be in the PDP context before the radio comes on and the modem attaches; esp_modem only
// sends it when PPP starts
esp_modem::PdpContext pdp(this->apn_);
if (this->dce_->set_pdp_context(pdp) != command_result::OK) {
ESP_LOGW(TAG, "Failed to set the APN");
this->set_fault_(ModemFault::MODEM_FAULT_SETUP);
return this->retry_(ModemState::MODEM_STATE_CONNECTING);
}
if (this->dce_->set_radio_state(1) != command_result::OK) {
ESP_LOGW(TAG, "Failed to turn the radio on");
@@ -870,12 +862,11 @@ ModemState ModemComponent::handle_state_disconnecting_() {
ESP_LOGW(TAG, "Failed to leave data mode");
this->set_teardown_fault_();
}
// Turn the radio off; waiting for the lost IP event could take up to 2 min. The SIM is powered down as well, as
// ENABLING does anyway on a reconnect.
if (this->dce_->set_radio_state(RADIO_STATE_MINIMUM) == command_result::OK) {
// Turn the radio off; waiting for the lost IP event could take up to 2 min. A reconnect keeps the SIM powered
// so it is not unlocked again; only disable() powers it down.
const bool reconnect = this->target_state_.load() == ModemState::MODEM_STATE_CONNECTED;
if (this->dce_->set_radio_state(reconnect ? RADIO_STATE_RF_OFF : 0) == command_result::OK) {
ESP_LOGI(TAG, "Radio off");
} else if (this->dce_->set_radio_state(RADIO_STATE_RF_OFF) == command_result::OK) {
ESP_LOGW(TAG, "Radio off, but the SIM could not be powered down");
} else {
ESP_LOGW(TAG, "Radio off failed");
this->set_teardown_fault_();
@@ -299,7 +299,6 @@ class ModemComponent : public Component {
bool connect_timeout_armed_{false};
bool modem_synced_{false}; // a modem that never answered has nothing to tear down
bool resync_{false}; // the attachment read failed, so CONNECTING syncs again first
bool apn_pending_{false}; // ENABLING could not set the APN at CFUN=0, so CONNECTING sets it after the SIM check
#ifdef USE_MODEM_SEND_AT
char at_command_[AT_COMMAND_SIZE];
#endif
+11 -73
View File
@@ -1,6 +1,3 @@
from dataclasses import dataclass, field
import functools
from esphome import automation
import esphome.codegen as cg
import esphome.config_validation as cv
@@ -29,7 +26,7 @@ from esphome.core.entity_helpers import (
)
from esphome.cpp_generator import MockObj, MockObjClass, TemplateArguments
from esphome.cpp_types import global_ns
from esphome.types import ConfigType, TemplateArgsType
from esphome.types import ConfigType
CODEOWNERS = ["@esphome/core"]
DOMAIN = "select"
@@ -50,25 +47,6 @@ SELECT_OPERATION_OPTIONS = SelectOperation.options(
)
@dataclass
class SelectData:
# Options of each select whose option list never changes at run time, by id
fixed_options: dict[str, list[str]] = field(default_factory=dict)
def _get_data() -> SelectData:
if DOMAIN not in CORE.data:
CORE.data[DOMAIN] = SelectData()
return CORE.data[DOMAIN]
def _record_options(
config: ConfigType, options: list[str], fixed_options: bool
) -> None:
if fixed_options:
_get_data().fixed_options[config[CONF_ID].id] = options
_SELECT_SCHEMA = (
cv.ENTITY_BASE_SCHEMA.extend(entity_helpers.WEBSERVER_SORTING_SCHEMA)
.extend(cv.MQTT_COMMAND_COMPONENT_SCHEMA)
@@ -127,11 +105,7 @@ async def setup_select_core_(var, config, *, options: list[str]):
await entity_helpers.setup_entity_integrations(var, config)
async def register_select(
var, config, *, options: list[str], fixed_options: bool = False
):
"""``fixed_options=True`` when the options never change at run time, so ``select.set`` can use the index."""
_record_options(config, options, fixed_options)
async def register_select(var, config, *, options: list[str]):
if not CORE.has_id(config[CONF_ID]):
var = cg.Pvariable(config[CONF_ID], var)
queue_entity_register("select", config)
@@ -139,9 +113,9 @@ async def register_select(
await setup_select_core_(var, config, options=options)
async def new_select(config, *args, options: list[str], fixed_options: bool = False):
async def new_select(config, *args, options: list[str]):
var = cg.new_Pvariable(config[CONF_ID], *args)
await register_select(var, config, options=options, fixed_options=fixed_options)
await register_select(var, config, options=options)
return var
@@ -157,10 +131,7 @@ def sub_selects(
config: ConfigType, *, parent: MockObj | ID | None = None
) -> SubEntities:
"""Return a SubEntities bound to new_select; ``apply`` statements can use ``entity``, ``index`` and ``option``."""
# Hub selects get their options from code generation only
return SubEntities(
functools.partial(new_select, fixed_options=True), config, parent, APPLY_ENTITY
)
return SubEntities(new_select, config, parent, APPLY_ENTITY)
@coroutine_with_priority(CoroPriority.CORE)
@@ -175,20 +146,13 @@ OPERATION_BASE_SCHEMA = cv.Schema(
)
_SELECT_SET_SCHEMA = OPERATION_BASE_SCHEMA.extend(
{
cv.Required(CONF_OPTION): cv.templatable(cv.string_strict),
}
)
def _option_index(config: ConfigType, option: str) -> str:
return str(_get_data().fixed_options[config[CONF_ID].id].index(option))
_set_option_builder = automation.apply_action_builder(
automation.register_apply_action(
"select.set",
_SELECT_SET_SCHEMA,
OPERATION_BASE_SCHEMA.extend(
{
cv.Required(CONF_OPTION): cv.templatable(cv.string_strict),
}
),
automation.ApplyField(
CONF_OPTION,
"set_option",
@@ -197,32 +161,6 @@ _set_option_builder = automation.apply_action_builder(
),
call="make_call",
)
_set_index_builder = automation.apply_action_builder(
"select.set",
_SELECT_SET_SCHEMA,
automation.ApplyField(CONF_OPTION, "set_index", cg.size_t, const_fn=_option_index),
call="make_call",
)
@automation.register_action(
"select.set", automation.ApplyAction, _SELECT_SET_SCHEMA, synchronous=True
)
async def select_set_to_code(
config: ConfigType,
action_id: ID,
template_arg: TemplateArguments,
args: TemplateArgsType,
) -> MockObj:
# A constant fixed option becomes its index
await cg.get_variable(config[CONF_ID])
option = config[CONF_OPTION]
if not cg.is_template(option) and option in _get_data().fixed_options.get(
config[CONF_ID].id, ()
):
return await _set_index_builder(config, action_id, template_arg, args)
return await _set_option_builder(config, action_id, template_arg, args)
automation.register_apply_action(
"select.set_index",
@@ -113,7 +113,7 @@ async def to_code(config):
milliseconds=SCHEDULER_DONT_RUN
)
await cg.register_component(var, component_config)
await select.register_select(var, config, options=options, fixed_options=True)
await select.register_select(var, config, options=options)
if CONF_LAMBDA in config:
lambda_ = await cg.process_lambda(
@@ -36,8 +36,13 @@ void setup_with_restore(BaseTemplateSelect *sel_comp, ESPPreferenceObject &pref,
}
void update_lambda(BaseTemplateSelect *sel_comp, const optional<std::string> &val) {
if (val.has_value())
if (val.has_value()) {
if (!sel_comp->has_option(*val)) {
ESP_LOGE(TAG, "Lambda returned an invalid option: %s", (*val).c_str());
return;
}
sel_comp->publish_state(*val);
}
}
} // namespace esphome::template_
+1 -1
View File
@@ -115,7 +115,7 @@ async def to_code(config):
options = list(options_map.values())
mappings = list(options_map.keys())
var = await select.new_select(config, options=options, fixed_options=True)
var = await select.new_select(config, options=options)
if mappings:
cg.add(var.set_select_mappings(mappings))
hub = await cg.add_variable(var.set_tuya_parent, config[CONF_TUYA_ID])
+21 -31
View File
@@ -5,57 +5,47 @@
namespace esphome::wifi {
/// Which expected leave a disconnect matched.
enum class ExpectedLeave : uint8_t {
EXPECTED_LEAVE_NONE,
EXPECTED_LEAVE_AP,
EXPECTED_LEAVE_ATTEMPT, ///< An attempt that never associated, so no BSSID
};
// Switching APs calls esp_wifi_disconnect(), which queues a leave event for the old AP, or with no BSSID for an
// attempt that never associated. Remembers it so that event does not end the next attempt. Platform free for tests.
// Switching APs calls esp_wifi_disconnect(), which queues an association-leave event from the old AP. Remembers
// that AP so its leave event does not end the connection attempt started after it. Platform free for host tests.
class ExpectedDisconnect {
public:
void arm(const uint8_t *bssid) { this->arm_(bssid, ExpectedLeave::EXPECTED_LEAVE_AP); }
void clear() { this->expected_ = ExpectedLeave::EXPECTED_LEAVE_NONE; }
void arm(const uint8_t *bssid) {
std::copy_n(bssid, this->bssid_.size(), this->bssid_.begin());
this->armed_ = true;
}
void clear() { this->armed_ = false; }
/// Disconnects and, once that succeeds, expects the leave event. read_bssid(uint8_t *out) runs first, since the
/// record is gone after the disconnect, and writes the AP's BSSID only when it returns true; without an AP the
/// attempt's leave, with no BSSID, is expected. disconnect() returns whether it succeeded, which is the result.
/// Disconnects from the current AP and, once that succeeds, expects its leave event. read_bssid(uint8_t *out)
/// returns whether there was an AP and runs first, since the record is gone after the disconnect; disconnect()
/// returns whether it succeeded, which is also the result. Any earlier expectation is dropped.
template<typename ReadBssid, typename Disconnect>
bool disconnect_and_expect_leave(ReadBssid &&read_bssid, Disconnect &&disconnect) {
this->clear();
this->armed_ = false;
std::array<uint8_t, 6> bssid{};
const bool had_ap = read_bssid(bssid.data());
if (!disconnect())
return false;
this->arm_(bssid.data(), had_ap ? ExpectedLeave::EXPECTED_LEAVE_AP : ExpectedLeave::EXPECTED_LEAVE_ATTEMPT);
if (had_ap)
this->arm(bssid.data());
return true;
}
/// A connected event still queued from the previous AP; it keeps the expectation.
bool is_previous_ap(const uint8_t *bssid) const {
return this->expected_ != ExpectedLeave::EXPECTED_LEAVE_NONE &&
std::equal(this->bssid_.begin(), this->bssid_.end(), bssid);
return this->armed_ && std::equal(this->bssid_.begin(), this->bssid_.end(), bssid);
}
/// Which expected leave a disconnect is. \p is_leave covers an AP's association leave (8) and the station leave
/// of an attempt that never associated (36). Every disconnect ends the expectation, so it matches at most once.
ExpectedLeave consume_disconnect(const uint8_t *bssid, bool is_leave) {
const ExpectedLeave matched =
is_leave && this->is_previous_ap(bssid) ? this->expected_ : ExpectedLeave::EXPECTED_LEAVE_NONE;
this->clear();
return matched;
/// Whether a disconnect is the previous AP's leave. Every disconnect ends the expectation, so it matches at
/// most once; the caller handles roaming disconnects before this.
bool consume_disconnect(const uint8_t *bssid, bool assoc_leave) {
const bool expected = assoc_leave && this->is_previous_ap(bssid);
this->armed_ = false;
return expected;
}
protected:
void arm_(const uint8_t *bssid, ExpectedLeave expected) {
std::copy_n(bssid, this->bssid_.size(), this->bssid_.begin());
this->expected_ = expected;
}
std::array<uint8_t, 6> bssid_{};
ExpectedLeave expected_{ExpectedLeave::EXPECTED_LEAVE_NONE};
bool armed_{false};
};
} // namespace esphome::wifi
@@ -1158,12 +1158,10 @@ void WiFiComponent::wifi_process_event_(IDFWiFiEvent *data) {
ESP_LOGI(TAG, "Disconnected ssid='%.*s' reason='Station Roaming'", it.ssid_len, (const char *) it.ssid);
return;
}
const ExpectedLeave expected = s_expected_disconnect.consume_disconnect(
it.bssid, it.reason == WIFI_REASON_ASSOC_LEAVE || it.reason == WIFI_REASON_STA_LEAVING);
const bool expected_disconnect = expected != ExpectedLeave::EXPECTED_LEAVE_NONE;
const bool expected_disconnect =
s_expected_disconnect.consume_disconnect(it.bssid, it.reason == WIFI_REASON_ASSOC_LEAVE);
if (expected_disconnect) {
ESP_LOGD(TAG, "Expected leave of the previous %s",
expected == ExpectedLeave::EXPECTED_LEAVE_AP ? LOG_STR_LITERAL("AP") : LOG_STR_LITERAL("attempt"));
ESP_LOGD(TAG, "Expected disconnect from previous AP");
} else if (it.reason == WIFI_REASON_NO_AP_FOUND) {
ESP_LOGW(TAG, "Disconnected ssid='%.*s' reason='Probe Request Unsuccessful'", it.ssid_len,
(const char *) it.ssid);
@@ -1183,9 +1181,7 @@ void WiFiComponent::wifi_process_event_(IDFWiFiEvent *data) {
// Refresh is_connected() cache; the old session is no longer connected.
this->update_connected_state_();
#ifdef USE_WIFI_CONNECT_STATE_LISTENERS
// An attempt that never associated ended no session listeners know of
if (expected != ExpectedLeave::EXPECTED_LEAVE_ATTEMPT)
this->notify_disconnect_state_listeners_();
this->notify_disconnect_state_listeners_();
#endif
#if defined(USE_WIFI_AP_WISP) && !defined(USE_WIFI_AP_WISP_STATIC_DNS)
@@ -8,16 +8,18 @@ namespace esphome::zigbee {
ESPHOME_LOG_TAG(TAG, "zigbee.attribute");
void ZigbeeAttribute::set_attr_internal() {
void ZigbeeAttribute::set_attr_() {
if (!this->zb_->is_started()) {
return;
}
if (esp_zigbee_lock_acquire(10 / portTICK_PERIOD_MS)) {
ezb_zcl_status_t state = ezb_zcl_set_attr_value(this->endpoint_id_, this->cluster_id_, this->role_, this->attr_id_,
EZB_ZCL_STD_MANUF_CODE, this->value_p_, false);
// cleared before report_() so it can disable the loop
// when the report has to wait for join
this->set_attr_requested_ = false;
if (this->force_report_) {
this->report_internal(true);
this->report_(true);
}
// Check for error
if (state != EZB_ZCL_STATUS_SUCCESS) {
@@ -28,12 +30,15 @@ void ZigbeeAttribute::set_attr_internal() {
}
}
void ZigbeeAttribute::report_internal(bool has_lock) {
void ZigbeeAttribute::report_(bool has_lock) {
if (!this->report_enabled) {
return;
}
if (!this->zb_->is_joined()) {
this->report_requested_ = true;
if (!this->set_attr_requested_) {
this->disable_loop();
}
return;
}
if (has_lock or esp_zigbee_lock_acquire(10 / portTICK_PERIOD_MS)) {
@@ -56,6 +61,43 @@ void ZigbeeAttribute::report_internal(bool has_lock) {
}
}
void ZigbeeAttribute::set_report(ZigbeeReportT report) {
if (report == ZigbeeReportT::ZIGBEE_REPORT_DEFAULT || report == ZigbeeReportT::ZIGBEE_REPORT_COORDINATOR) {
return;
}
this->report_enabled = true;
if (report == ZigbeeReportT::ZIGBEE_REPORT_FORCE) {
this->force_report_ = true;
}
this->zb_->add_on_join_callback([this](bool) {
if (this->report_requested_) {
this->enable_loop();
}
});
}
void ZigbeeAttribute::report() {
if (!this->report_enabled) {
return;
}
this->report_requested_ = true;
this->enable_loop();
}
void ZigbeeAttribute::loop() {
if (this->set_attr_requested_) {
this->set_attr_();
}
if (this->report_requested_) {
this->report_(false);
}
if (!this->report_requested_ && !this->set_attr_requested_) {
this->disable_loop();
}
}
} // namespace esphome::zigbee
#endif
@@ -4,8 +4,9 @@
#include <limits>
#include <type_traits>
#include "esphome/core/automation.h"
#include "esphome/core/component.h"
#include "esphome/core/defines.h"
#include "zigbee_entity_base_esp32.h"
#ifdef USE_ESP32
#ifdef USE_ZIGBEE
@@ -28,22 +29,34 @@
namespace esphome::zigbee {
class ZigbeeAttribute final : public ZigbeeEntityBase {
enum ZigbeeReportT {
ZIGBEE_REPORT_DEFAULT,
ZIGBEE_REPORT_COORDINATOR,
ZIGBEE_REPORT_ENABLE,
ZIGBEE_REPORT_FORCE,
};
class ZigbeeAttribute final : public Component {
public:
ZigbeeAttribute(ZigbeeComponent *parent, uint8_t endpoint_id, uint16_t cluster_id, uint8_t role, uint16_t attr_id,
uint8_t attr_type, float scale, uint8_t max_size)
: ZigbeeEntityBase(parent, endpoint_id),
: zb_(parent),
endpoint_id_(endpoint_id),
cluster_id_(cluster_id),
role_(role),
attr_id_(attr_id),
attr_type_(attr_type),
max_size_(max_size),
scale_(scale) {}
void loop() override;
template<typename T> void add_attr(T value);
template<typename T> void set_attr(const T &value);
uint8_t attr_type() { return attr_type_; }
void set_report(ZigbeeReportT report);
void report();
template<typename F> void add_on_value_callback(F &&callback) { on_value_callback_.add(std::forward<F>(callback)); }
void on_value(ezb_zcl_attribute_t attribute) override {
void on_value(ezb_zcl_attribute_t attribute) {
if (attribute.data.type == this->attr_type() && attribute.data.value) {
this->on_value_callback_.call(attribute);
}
@@ -62,10 +75,13 @@ class ZigbeeAttribute final : public ZigbeeEntityBase {
#ifdef USE_NUMBER
template<typename T> void connect(number::Number *device);
#endif
bool report_enabled = false;
protected:
void set_attr_internal() override;
void report_internal(bool has_lock) override;
void set_attr_();
void report_(bool has_lock);
ZigbeeComponent *zb_;
uint8_t endpoint_id_;
uint16_t cluster_id_;
uint8_t role_;
uint16_t attr_id_;
@@ -73,6 +89,9 @@ class ZigbeeAttribute final : public ZigbeeEntityBase {
uint8_t max_size_;
float scale_;
void *value_p_{nullptr};
bool set_attr_requested_{false};
bool report_requested_{false};
bool force_report_{false};
LazyCallbackManager<void(ezb_zcl_attribute_t attribute)> on_value_callback_{};
template<typename T> T scale_value_(float value);
template<typename T> T invalid_value_();
@@ -1,51 +0,0 @@
#include "zigbee_entity_base_esp32.h"
#include "esphome/core/log.h"
#include "esphome/core/defines.h"
#ifdef USE_ESP32
#ifdef USE_ZIGBEE
namespace esphome::zigbee {
ESPHOME_LOG_TAG(TAG, "zigbee.entity_base");
void ZigbeeEntityBase::set_report(ZigbeeReportT report) {
if (report == ZigbeeReportT::ZIGBEE_REPORT_DEFAULT || report == ZigbeeReportT::ZIGBEE_REPORT_COORDINATOR) {
return;
}
this->report_enabled = true;
if (report == ZigbeeReportT::ZIGBEE_REPORT_FORCE) {
this->force_report_ = true;
}
this->zb_->add_on_join_callback([this](bool) {
if (this->report_requested_) {
this->enable_loop();
}
});
}
void ZigbeeEntityBase::report() {
if (!this->report_enabled) {
return;
}
this->report_requested_ = true;
this->enable_loop();
}
void ZigbeeEntityBase::loop() {
if (this->set_attr_requested_) {
this->set_attr_internal();
}
if (this->report_requested_ && this->zb_->is_joined()) {
this->report_internal(false);
}
if ((!this->report_requested_ || !this->zb_->is_joined()) && !this->set_attr_requested_) {
this->disable_loop();
}
}
} // namespace esphome::zigbee
#endif
#endif
@@ -1,47 +0,0 @@
#pragma once
#include "esphome/core/automation.h"
#include "esphome/core/component.h"
#include "esphome/core/defines.h"
#ifdef USE_ESP32
#ifdef USE_ZIGBEE
#include "esp_zigbee.h"
#include "zigbee_esp32.h"
namespace esphome::zigbee {
enum ZigbeeReportT {
ZIGBEE_REPORT_DEFAULT,
ZIGBEE_REPORT_COORDINATOR,
ZIGBEE_REPORT_ENABLE,
ZIGBEE_REPORT_FORCE,
};
class ZigbeeEntityBase : public Component, public ZigbeeAttributeInterface {
public:
void loop() override;
void set_report(ZigbeeReportT report);
void report() override;
bool report_enabled = false;
protected:
ZigbeeEntityBase(ZigbeeComponent *parent, uint8_t endpoint_id) : zb_(parent), endpoint_id_(endpoint_id) {}
/// Handle pending attribute writes and clear set_attr_requested_ when complete; leave it set to retry.
virtual void set_attr_internal() = 0;
/// Handle pending reports and clear report_requested_ when complete; leave it set to retry.
/// Acquire/release the Zigbee lock only when has_lock is false; otherwise the caller owns it.
virtual void report_internal(bool has_lock) = 0;
ZigbeeComponent *zb_;
uint8_t endpoint_id_;
bool set_attr_requested_{false};
bool report_requested_{false};
bool force_report_{false};
};
} // namespace esphome::zigbee
#endif
#endif
+8 -12
View File
@@ -31,11 +31,7 @@ static constexpr uint8_t MAX_ZB_QUEUE_SIZE = 32;
uint8_t *get_zcl_string(const char *str, uint8_t max_size, bool use_max_size = false);
class ZigbeeAttributeInterface {
public:
virtual void on_value(ezb_zcl_attribute_t attribute) = 0;
virtual void report() = 0;
};
class ZigbeeAttribute;
class ZigbeeComponent final : public Component {
public:
@@ -52,8 +48,8 @@ class ZigbeeComponent final : public Component {
bool register_device();
template<typename T>
void add_attr(ZigbeeAttributeInterface *attr, uint8_t endpoint_id, uint16_t cluster_id, uint8_t role,
uint16_t attr_id, uint8_t max_size, T value);
void add_attr(ZigbeeAttribute *attr, uint8_t endpoint_id, uint16_t cluster_id, uint8_t role, uint16_t attr_id,
uint8_t max_size, T value);
template<typename T>
void add_attr(uint8_t endpoint_id, uint16_t cluster_id, uint8_t role, uint16_t attr_id, uint8_t max_size, T value);
@@ -98,13 +94,13 @@ class ZigbeeComponent final : public Component {
void end_parent_link_failures_(uint8_t seq);
void reset_parent_link_failures_();
template<typename T>
void add_attr_(ZigbeeAttributeInterface *attr, uint8_t endpoint_id, uint16_t cluster_id, uint8_t role,
uint16_t attr_id, T *value_p);
void add_attr_(ZigbeeAttribute *attr, uint8_t endpoint_id, uint16_t cluster_id, uint8_t role, uint16_t attr_id,
T *value_p);
void handle_attribute_(ezb_zcl_message_info_t info, ezb_zcl_attribute_t attribute);
// attributes_ will be used during operation in zigbee callbacks to update the attribute values and trigger
// automations
// key tuple could be replaced by single 64 (48) bit int with bit fields for endpoint, cluster, role and attr_id
std::map<std::tuple<uint8_t, uint16_t, uint8_t, uint16_t>, ZigbeeAttributeInterface *> attributes_;
std::map<std::tuple<uint8_t, uint16_t, uint8_t, uint16_t>, ZigbeeAttribute *> attributes_;
ezb_af_device_desc_t dev_desc_ = ezb_af_create_device_desc();
CallbackManager<void(bool)> join_cb_{};
template<typename... Args> friend void enqueue_zb_event(Args... args);
@@ -137,7 +133,7 @@ void ZigbeeComponent::add_attr(uint8_t endpoint_id, uint16_t cluster_id, uint8_t
}
template<typename T>
void ZigbeeComponent::add_attr(ZigbeeAttributeInterface *attr, uint8_t endpoint_id, uint16_t cluster_id, uint8_t role,
void ZigbeeComponent::add_attr(ZigbeeAttribute *attr, uint8_t endpoint_id, uint16_t cluster_id, uint8_t role,
uint16_t attr_id, uint8_t max_size, T value) {
// The size byte of the zcl_str must be set to the maximum value,
// even though the initial string may be shorter.
@@ -155,7 +151,7 @@ void ZigbeeComponent::add_attr(ZigbeeAttributeInterface *attr, uint8_t endpoint_
}
template<typename T>
void ZigbeeComponent::add_attr_(ZigbeeAttributeInterface *attr, uint8_t endpoint_id, uint16_t cluster_id, uint8_t role,
void ZigbeeComponent::add_attr_(ZigbeeAttribute *attr, uint8_t endpoint_id, uint16_t cluster_id, uint8_t role,
uint16_t attr_id, T *value_p) {
ezb_af_ep_desc_t ep_desc = ezb_af_device_get_endpoint_desc(this->dev_desc_, endpoint_id);
if (ep_desc == NULL) {
+6 -5
View File
@@ -1497,11 +1497,12 @@ class BindKeyValidator(SensitiveValidator):
if any(len(part) != 2 for part in parts):
raise Invalid(f"{self._name} must be format XX")
for part in parts:
# int() also accepts surrounding whitespace and a +/- sign, which
# would silently validate a key different from the one typed
if not re.fullmatch(r"[0-9A-Fa-f]{2}", part):
raise Invalid(f"{self._name} must be hex values from 00 to FF")
parts_int.append(int(part, 16))
try:
parts_int.append(int(part, 16))
except ValueError:
raise Invalid(
f"{self._name} must be hex values from 00 to FF"
) from None
return "".join(f"{part:02X}" for part in parts_int)
+3 -25
View File
@@ -153,7 +153,6 @@
#define USE_LINE_READER
#define USE_LOCK
#define USE_LOGGER
#define USE_LOGGER_CONSOLE
#define USE_LOGGER_LEVEL_LISTENERS
#define USE_LOGGER_RUNTIME_TAG_LEVELS
#define USE_LVGL
@@ -768,26 +767,16 @@
#if defined(USE_ESP32_VARIANT_ESP32S2)
#define USE_LOGGER_USB_CDC
#define USE_LOGGER_UART_SELECTION_USB_CDC
#define ESPHOME_LOGGER_UART_NAME "USB_CDC"
#define ESPHOME_LOGGER_UART_SELECTION logger::UART_SELECTION_USB_CDC
#elif defined(USE_ESP32_VARIANT_ESP32H21)
#define USE_LOGGER_USB_SERIAL_JTAG
#define USE_LOGGER_UART_SELECTION_USB_SERIAL_JTAG
#define ESPHOME_LOGGER_UART_NAME "USB_SERIAL_JTAG"
#define ESPHOME_LOGGER_UART_SELECTION logger::UART_SELECTION_USB_SERIAL_JTAG
#elif defined(USE_ESP32_VARIANT_ESP32C3) || defined(USE_ESP32_VARIANT_ESP32C5) || \
defined(USE_ESP32_VARIANT_ESP32C6) || defined(USE_ESP32_VARIANT_ESP32C61) || defined(USE_ESP32_VARIANT_ESP32H2) || \
defined(USE_ESP32_VARIANT_ESP32H4) || defined(USE_ESP32_VARIANT_ESP32P4) || defined(USE_ESP32_VARIANT_ESP32S3) || \
defined(USE_ESP32_VARIANT_ESP32S31)
defined(USE_ESP32_VARIANT_ESP32H21) || defined(USE_ESP32_VARIANT_ESP32H4) || defined(USE_ESP32_VARIANT_ESP32P4) || \
defined(USE_ESP32_VARIANT_ESP32S3) || defined(USE_ESP32_VARIANT_ESP32S31)
#define USE_LOGGER_USB_CDC
#define USE_LOGGER_UART_SELECTION_USB_CDC
#define USE_LOGGER_USB_SERIAL_JTAG
#define USE_LOGGER_UART_SELECTION_USB_SERIAL_JTAG
#define ESPHOME_LOGGER_UART_NAME "USB_SERIAL_JTAG"
#define ESPHOME_LOGGER_UART_SELECTION logger::UART_SELECTION_USB_SERIAL_JTAG
#else
#define USE_LOGGER_UART_SELECTION_UART0
#define ESPHOME_LOGGER_UART_NAME "UART0"
#define ESPHOME_LOGGER_UART_SELECTION logger::UART_SELECTION_UART0
#endif
#endif
@@ -801,9 +790,6 @@
#define USE_WIFI_SCAN_RESULTS_LOCK
#define USE_ESP8266_LOGGER_SERIAL
#define USE_ESP8266_LOGGER_SERIAL1
#define USE_LOGGER_UART_SELECTION_UART0
#define ESPHOME_LOGGER_UART_NAME "UART0"
#define ESPHOME_LOGGER_UART_SELECTION logger::UART_SELECTION_UART0
#define USE_ESP8266_PREFERENCES_FLASH
#define ESP8266_PREFERENCES_FLASH_WORDS 128
#define ESP8266_PREFERENCES_RTC_EBOOT_WORDS 32
@@ -849,8 +835,6 @@
#define USE_I2C
#define USE_LOGGER_USB_CDC
#define USE_LOGGER_UART_SELECTION_USB_CDC
#define ESPHOME_LOGGER_UART_NAME "USB_CDC"
#define ESPHOME_LOGGER_UART_SELECTION logger::UART_SELECTION_USB_CDC
#define USE_SOCKET_IMPL_LWIP_TCP
#define USE_RP2040_BLE
#define USE_RP2_BLE_TRACKER
@@ -881,9 +865,6 @@
#if defined(USE_BK72XX) || defined(USE_RTL87XX) || defined(USE_LN882X)
#define USE_LIBRETINY_CRASH_HANDLER
#endif
#define USE_LOGGER_UART_SELECTION_DEFAULT
#define ESPHOME_LOGGER_UART_NAME "DEFAULT"
#define ESPHOME_LOGGER_UART_SELECTION logger::UART_SELECTION_DEFAULT
#ifdef USE_BK72XX
#define USE_REMOTE_RECEIVER_TIMER_SAMPLING
#endif
@@ -938,11 +919,8 @@
#define ESPHOME_TASK_LOG_BUFFER_SIZE 768
#define USE_LOGGER_EARLY_MESSAGE
#define USE_LOGGER_UART_SELECTION_USB_CDC
#define ESPHOME_LOGGER_UART_NAME "USB_CDC"
#define ESPHOME_LOGGER_UART_SELECTION logger::UART_SELECTION_USB_CDC
#define USE_LOGGER_USB_CDC
#define USE_LOGGER_WAIT_FOR_CDC
#define USE_LOGGER_ZEPHYR_CDC_LOOP
#define USE_NRF52_DFU
#define USE_NRF52_REG0_VOUT 5
#define USE_NRF52_UICR_ERASE
-36
View File
@@ -1472,42 +1472,6 @@ __attribute__((format(printf, 4, 5))) inline size_t buf_append_printf(char *buf,
}
#endif
/// Copy a PROGMEM string to `dst`, stopping at its null terminator or at `end`, and return the new end of `dst`.
/// No terminator is written. Flash only allows aligned 32-bit loads on ESP8266, so `src` is read a whole word at a
/// time, including the bytes after its null in the last word; `src` must be readable up to the end of that word.
inline char *ESPHOME_ALWAYS_INLINE progmem_copy_str(char *dst, const char *end, const char *src) {
const uint32_t offset = reinterpret_cast<uintptr_t>(src) & 3;
const uint32_t *word = reinterpret_cast<const uint32_t *>(src - offset);
uint32_t bytes = *word++;
// Enters the loop at the first byte of src; after that each word runs its four steps without another dispatch, and
// one step per byte keeps each a single extract
switch (offset) {
for (;;) {
bytes = *word++;
[[fallthrough]];
case 0:
if (dst >= end || static_cast<char>(bytes) == '\0')
return dst;
*dst++ = static_cast<char>(bytes);
[[fallthrough]];
case 1:
if (dst >= end || static_cast<char>(bytes >> 8) == '\0')
return dst;
*dst++ = static_cast<char>(bytes >> 8);
[[fallthrough]];
case 2:
if (dst >= end || static_cast<char>(bytes >> 16) == '\0')
return dst;
*dst++ = static_cast<char>(bytes >> 16);
[[fallthrough]];
default:
if (dst >= end || static_cast<char>(bytes >> 24) == '\0')
return dst;
*dst++ = static_cast<char>(bytes >> 24);
}
}
}
#ifdef USE_ESP8266
/// Safely append a PROGMEM string to buffer, returning new position (capped at size).
/// ESP8266 internal implementation — prefer the `buf_append_str` macro which wraps
-28
View File
@@ -48,34 +48,6 @@ void HOT esp_log_printf_(int level, const char *tag, int line, const __FlashStri
}
#endif
void HOT esp_log_str_(int level, const char *tag, int line, const char *format) { // NOLINT
#ifdef USE_LOGGER
#ifdef ESPHOME_DEBUG
// Before Logger::pre_setup(), the formatting path below reports the early call
if (logger::global_logger != nullptr)
#endif
{
if (logger::global_logger->log_str_(static_cast<uint8_t>(level), tag, line, format))
return;
}
// Formats the line log_str_() could not copy; it has no '%', so it is its own format
#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wformat-security"
#pragma GCC diagnostic ignored "-Wformat-nonliteral"
esp_log_printf_(level, tag, line, format);
#pragma GCC diagnostic pop
#endif
}
#ifdef USE_ESP8266
void HOT esp_log_str_(int level, const char *tag, int line, const __FlashStringHelper *format) {
#ifdef USE_LOGGER
ESPHOME_DEBUG_ASSERT(logger::global_logger != nullptr);
logger::global_logger->log_str_(static_cast<uint8_t>(level), tag, line, format);
#endif
}
#endif
void HOT esp_log_vprintf_(int level, const char *tag, int line, const char *format, va_list args) { // NOLINT
#ifdef USE_LOGGER
#ifdef ESPHOME_DEBUG
+14 -57
View File
@@ -67,14 +67,6 @@ void esp_log_printf_(int level, const char *tag, int line, const char *format, .
// NOLINTNEXTLINE(readability-identifier-naming)
void esp_log_printf_(int level, const char *tag, int line, const __FlashStringHelper *format, ...);
#endif
// esp_log_printf_ for a line with no arguments and no '%': it is copied without formatting
void esp_log_str_(int level, const char *tag, int line, const char *format); // NOLINT
#ifdef USE_ESP8266
// NOLINTNEXTLINE(readability-identifier-naming)
void esp_log_str_(int level, const char *tag, int line, const __FlashStringHelper *format);
#endif
// Never defined or called: the ESP_LOGx macros name it in dead code so a line without arguments has its format checked
int esp_log_format_check_(const char *format, ...) __attribute__((format(printf, 1, 2))); // NOLINT
void esp_log_vprintf_(int level, const char *tag, int line, const char *format, va_list args); // NOLINT
#if defined(USE_ESP32)
int esp_idf_log_vprintf_(const char *format, va_list args); // NOLINT
@@ -86,50 +78,9 @@ int esp_idf_log_vprintf_(const char *format, va_list args); // NOLINT
#define ESPHOME_LOG_FORMAT(format) format
#endif
#define ESPHOME_LOG_ARGS_(level, tag, format, ...) \
::esphome::esp_log_printf_(level, tag, __LINE__, ESPHOME_LOG_FORMAT(format), __VA_ARGS__)
// The format check is a statement-level if (false), not sizeof: GCC 16 skips format checks in unevaluated or folded
// expressions. __builtin_strchr folds on a literal, so only a line with '%' (such as "100%%") is formatted.
#ifdef USE_ESP8266
// F() only takes a literal, so using format more than once cannot repeat a side effect
#define ESPHOME_LOG_NO_ARGS_(level, tag, format) \
({ \
if (false) \
::esphome::esp_log_format_check_(format); \
if (__builtin_strchr(format, '%') != nullptr) \
::esphome::esp_log_printf_(level, tag, __LINE__, ESPHOME_LOG_FORMAT(format)); \
else \
::esphome::esp_log_str_(level, tag, __LINE__, ESPHOME_LOG_FORMAT(format)); \
})
#else
#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wformat-security"
#pragma GCC diagnostic ignored "-Wformat-nonliteral"
// A function argument, so format is evaluated once and a temporary such as std::string().c_str() outlives the call
// NOLINTNEXTLINE(readability-identifier-naming)
inline void __attribute__((always_inline)) esp_log_no_args_(int level, const char *tag, int line, const char *format) {
if (__builtin_strchr(format, '%') != nullptr) {
esp_log_printf_(level, tag, line, format);
} else {
esp_log_str_(level, tag, line, format);
}
}
#pragma GCC diagnostic pop
#define ESPHOME_LOG_NO_ARGS_(level, tag, format) \
({ \
if (false) \
::esphome::esp_log_format_check_(format); \
::esphome::esp_log_no_args_(level, tag, __LINE__, format); \
})
#endif
// ESPHOME_LOG_NO_ARGS_ when there are no arguments after the format, ESPHOME_LOG_ARGS_ otherwise
#define ESPHOME_LOG_PICK_(skip, name, ...) name
#define ESPHOME_LOG_(level, tag, format, ...) \
ESPHOME_LOG_PICK_(__VA_OPT__(, ) ESPHOME_LOG_ARGS_, ESPHOME_LOG_NO_ARGS_)(level, tag, \
format __VA_OPT__(, ) __VA_ARGS__)
#if ESPHOME_LOG_LEVEL >= ESPHOME_LOG_LEVEL_VERY_VERBOSE
#define esph_log_vv(tag, format, ...) ESPHOME_LOG_(ESPHOME_LOG_LEVEL_VERY_VERBOSE, tag, format, __VA_ARGS__)
#define esph_log_vv(tag, format, ...) \
::esphome::esp_log_printf_(ESPHOME_LOG_LEVEL_VERY_VERBOSE, tag, __LINE__, ESPHOME_LOG_FORMAT(format), ##__VA_ARGS__)
#define ESPHOME_LOG_HAS_VERY_VERBOSE
#else
@@ -137,7 +88,8 @@ inline void __attribute__((always_inline)) esp_log_no_args_(int level, const cha
#endif
#if ESPHOME_LOG_LEVEL >= ESPHOME_LOG_LEVEL_VERBOSE
#define esph_log_v(tag, format, ...) ESPHOME_LOG_(ESPHOME_LOG_LEVEL_VERBOSE, tag, format, __VA_ARGS__)
#define esph_log_v(tag, format, ...) \
::esphome::esp_log_printf_(ESPHOME_LOG_LEVEL_VERBOSE, tag, __LINE__, ESPHOME_LOG_FORMAT(format), ##__VA_ARGS__)
#define ESPHOME_LOG_HAS_VERBOSE
#else
@@ -145,8 +97,10 @@ inline void __attribute__((always_inline)) esp_log_no_args_(int level, const cha
#endif
#if ESPHOME_LOG_LEVEL >= ESPHOME_LOG_LEVEL_DEBUG
#define esph_log_d(tag, format, ...) ESPHOME_LOG_(ESPHOME_LOG_LEVEL_DEBUG, tag, format, __VA_ARGS__)
#define esph_log_config(tag, format, ...) ESPHOME_LOG_(ESPHOME_LOG_LEVEL_CONFIG, tag, format, __VA_ARGS__)
#define esph_log_d(tag, format, ...) \
::esphome::esp_log_printf_(ESPHOME_LOG_LEVEL_DEBUG, tag, __LINE__, ESPHOME_LOG_FORMAT(format), ##__VA_ARGS__)
#define esph_log_config(tag, format, ...) \
::esphome::esp_log_printf_(ESPHOME_LOG_LEVEL_CONFIG, tag, __LINE__, ESPHOME_LOG_FORMAT(format), ##__VA_ARGS__)
#define ESPHOME_LOG_HAS_DEBUG
#define ESPHOME_LOG_HAS_CONFIG
@@ -156,7 +110,8 @@ inline void __attribute__((always_inline)) esp_log_no_args_(int level, const cha
#endif
#if ESPHOME_LOG_LEVEL >= ESPHOME_LOG_LEVEL_INFO
#define esph_log_i(tag, format, ...) ESPHOME_LOG_(ESPHOME_LOG_LEVEL_INFO, tag, format, __VA_ARGS__)
#define esph_log_i(tag, format, ...) \
::esphome::esp_log_printf_(ESPHOME_LOG_LEVEL_INFO, tag, __LINE__, ESPHOME_LOG_FORMAT(format), ##__VA_ARGS__)
#define ESPHOME_LOG_HAS_INFO
#else
@@ -164,7 +119,8 @@ inline void __attribute__((always_inline)) esp_log_no_args_(int level, const cha
#endif
#if ESPHOME_LOG_LEVEL >= ESPHOME_LOG_LEVEL_WARN
#define esph_log_w(tag, format, ...) ESPHOME_LOG_(ESPHOME_LOG_LEVEL_WARN, tag, format, __VA_ARGS__)
#define esph_log_w(tag, format, ...) \
::esphome::esp_log_printf_(ESPHOME_LOG_LEVEL_WARN, tag, __LINE__, ESPHOME_LOG_FORMAT(format), ##__VA_ARGS__)
#define ESPHOME_LOG_HAS_WARN
#else
@@ -172,7 +128,8 @@ inline void __attribute__((always_inline)) esp_log_no_args_(int level, const cha
#endif
#if ESPHOME_LOG_LEVEL >= ESPHOME_LOG_LEVEL_ERROR
#define esph_log_e(tag, format, ...) ESPHOME_LOG_(ESPHOME_LOG_LEVEL_ERROR, tag, format, __VA_ARGS__)
#define esph_log_e(tag, format, ...) \
::esphome::esp_log_printf_(ESPHOME_LOG_LEVEL_ERROR, tag, __LINE__, ESPHOME_LOG_FORMAT(format), ##__VA_ARGS__)
#define ESPHOME_LOG_HAS_ERROR
#else
+44 -53
View File
@@ -68,6 +68,38 @@ def test_logger_pre_setup_before_other_components(generate_main):
)
def test_default_uart_selection_is_not_emitted(generate_main):
"""UART0 is the C++ initializer on ESP8266, so the setter is skipped."""
main_cpp = generate_main("tests/component_tests/logger/test_logger.yaml")
assert "set_uart_selection(" not in main_cpp
def test_custom_uart_selection_is_emitted(generate_main):
"""A non default UART still reaches the setter before pre_setup()."""
main_cpp = generate_main("tests/component_tests/logger/test_logger_uart1.yaml")
assert "set_uart_selection(logger::UART_SELECTION_UART1);" in main_cpp
def test_libretiny_default_uart_selection_is_not_emitted(generate_main):
"""DEFAULT is the C++ initializer on LibreTiny, so the setter is skipped."""
main_cpp = generate_main(
"tests/component_tests/logger/test_logger_libretiny_default.yaml"
)
assert "set_uart_selection(" not in main_cpp
def test_libretiny_uart0_is_emitted(generate_main):
"""UART0 is not the LibreTiny initializer, so it must still be set."""
main_cpp = generate_main(
"tests/component_tests/logger/test_logger_libretiny_uart0.yaml"
)
assert "set_uart_selection(logger::UART_SELECTION_UART0);" in main_cpp
def test_ram_log_strings_is_deprecated(
generate_main: Callable[[str | Path], str],
caplog: pytest.LogCaptureFixture,
@@ -136,76 +168,35 @@ def test_log_v2_only_on_native_toolchain(
@pytest.mark.parametrize(
("config", "port", "zephyr_cdc_loop"),
("config", "expected"),
[
pytest.param("test_logger.yaml", "UART0", False, id="esp8266_default"),
pytest.param("test_logger_uart1.yaml", "UART1", False, id="esp8266_uart1"),
pytest.param(
"test_logger_uart0_swap.yaml", "UART0_SWAP", False, id="esp8266_uart0_swap"
),
pytest.param(
"test_logger_libretiny_default.yaml",
"DEFAULT",
False,
id="libretiny_default",
),
pytest.param(
"test_logger_libretiny_uart0.yaml", "UART0", False, id="libretiny_uart0"
),
pytest.param(
"test_logger_rp2_usb_cdc.yaml", "USB_CDC", False, id="rp2_usb_cdc"
),
pytest.param("test_logger_rp2_uart0.yaml", "UART0", False, id="rp2_uart0"),
pytest.param(
"test_logger_esp32_usb_cdc.yaml", "USB_CDC", False, id="esp32_usb_cdc"
),
pytest.param("test_logger_rp2_usb_cdc.yaml", {"USB_CDC"}, id="rp2_usb_cdc"),
pytest.param("test_logger_rp2_uart0.yaml", set(), id="rp2_uart0"),
pytest.param("test_logger_esp32_usb_cdc.yaml", {"USB_CDC"}, id="esp32_usb_cdc"),
pytest.param(
"test_logger_esp32_usb_serial_jtag.yaml",
"USB_SERIAL_JTAG",
False,
{"USB_SERIAL_JTAG"},
id="esp32_usb_serial_jtag",
),
pytest.param(
"test_logger_nrf52_usb_cdc.yaml", "USB_CDC", True, id="nrf52_usb_cdc"
),
pytest.param("test_logger_nrf52_usb_cdc.yaml", {"USB_CDC"}, id="nrf52_usb_cdc"),
# Zephyr polls the USB port only when it logs there
pytest.param(
"test_logger_nrf52_usb_cdc_no_serial.yaml",
"USB_CDC",
False,
id="nrf52_no_serial",
"test_logger_nrf52_usb_cdc_no_serial.yaml", set(), id="nrf52_no_serial"
),
],
)
def test_uart_selection_defines(
generate_main: Callable[[str | Path], str],
config: str,
port: str,
zephyr_cdc_loop: bool,
expected: set[str],
) -> None:
"""The chosen port is fixed at build time: one flag, its enum value and its dump_config name."""
"""USB ports get a selection define, which gates their port code and crash replay."""
generate_main(f"tests/component_tests/logger/{config}")
defines = {d.name: d.value for d in CORE.defines}
prefix = "USE_LOGGER_UART_SELECTION_"
assert {d.removeprefix(prefix) for d in defines if d.startswith(prefix)} == {port}
assert (
str(defines["ESPHOME_LOGGER_UART_SELECTION"])
== f"logger::UART_SELECTION_{port}"
)
assert str(defines["ESPHOME_LOGGER_UART_NAME"]) == f'"{port}"'
assert ("USE_LOGGER_ZEPHYR_CDC_LOOP" in defines) is zephyr_cdc_loop
def test_esp8266_without_serial_dumps_no_port(
generate_main: Callable[[str | Path], str],
) -> None:
"""ESP8266 without serial logging keeps UART0 selected but never opens it."""
generate_main("tests/component_tests/logger/test_logger_no_serial.yaml")
defines = {d.name: d.value for d in CORE.defines}
assert "USE_LOGGER_UART_SELECTION_UART0" in defines
assert str(defines["ESPHOME_LOGGER_UART_NAME"]) == '"NONE"'
assert {
d.name.removeprefix(prefix) for d in CORE.defines if d.name.startswith(prefix)
} == expected
def test_log_listener_slots_count_every_listener(
@@ -1,9 +0,0 @@
---
esphome:
name: test
esp8266:
board: d1_mini_lite
logger:
baud_rate: 0
@@ -1,9 +0,0 @@
---
esphome:
name: test
esp8266:
board: d1_mini_lite
logger:
hardware_uart: UART0_SWAP
@@ -1,39 +0,0 @@
esphome:
name: test
esp8266:
board: esp01_1m
button:
# Declared before the selects, so the action is generated before them
- platform: template
id: early
name: Early
on_press:
- select.set:
id: fan
option: high
- select.set:
id: fan
option: turbo
- select.set:
id: fan
option: !lambda return "low";
- select.set:
id: fan_copy
option: medium
select:
- platform: template
id: fan
name: Fan
optimistic: true
options: [low, medium, high]
on_value:
- select.set:
id: fan
option: medium
- platform: copy
id: fan_copy
name: Fan Copy
source_id: fan
@@ -1,35 +0,0 @@
---
esphome:
name: toshiba-test
esp32:
board: esp32dev
remote_transmitter:
pin: GPIO4
carrier_duty_percent: 50%
non_blocking: true
climate:
- platform: toshiba
id: ras
name: RAS
model: RAS-25J2KVSG-ND
- platform: toshiba
id: generic
name: Generic
select:
- platform: toshiba
toshiba_id: ras
power_selection:
id: power
name: Power selection
button:
- platform: template
name: Set
on_press:
- select.set:
id: power
option: Silent 1
@@ -1,47 +0,0 @@
esphome:
name: test
esp32:
board: esp32dev
framework:
type: esp-idf
spi:
- id: spi_bus
clk_pin: GPIO18
mosi_pin: GPIO23
display:
- platform: mipi_spi
spi_id: spi_bus
model: st7789v
id: tft_display
dimensions:
width: 240
height: 320
cs_pin: GPIO22
dc_pin: GPIO21
auto_clear_enabled: false
invert_colors: false
update_interval: never
lvgl:
displays: tft_display
widgets:
- dropdown:
id: dropdown_widget
options: [low, high]
select:
- platform: lvgl
id: lv_fan
name: LVGL Fan
widget: dropdown_widget
button:
- platform: template
name: Set
on_press:
- select.set:
id: lv_fan
option: high
@@ -1,43 +0,0 @@
"""A constant select.set option of a select with a fixed option list becomes its index."""
from collections.abc import Callable
from pathlib import Path
def _call(select_id: str, statement: str) -> str:
return f"auto apply_call = ::{select_id}->make_call();\n apply_call.{statement};"
def test_select_set_constant_uses_index(
generate_main: Callable[[str | Path], str],
component_config_path: Callable[[str], Path],
) -> None:
main_cpp = generate_main(component_config_path("select_set.yaml"))
# Generated before the select, and from the select's own on_value
assert _call("fan", "set_index(2)") in main_cpp
assert _call("fan", "set_index(1)") in main_cpp
assert 'set_option("high"' not in main_cpp
# Not an option: the lookup by text still logs it at run time
assert _call("fan", 'set_option("turbo", 5)') in main_cpp
# A lambda's value is only known at run time
assert _call("fan", 'set_option("low")') in main_cpp
# A copy select gets its options at run time
assert _call("fan_copy", 'set_option("medium", 6)') in main_cpp
def test_select_set_lvgl_keeps_lookup_by_text(
generate_main: Callable[[str | Path], str],
component_config_path: Callable[[str], Path],
) -> None:
"""Lvgl dropdown options can be replaced at run time, so the index is not fixed."""
main_cpp = generate_main(component_config_path("select_set_lvgl.yaml"))
assert _call("lv_fan", 'set_option("high", 4)') in main_cpp
def test_select_set_hub_select_uses_index(
generate_main: Callable[[str | Path], str],
component_config_path: Callable[[str], Path],
) -> None:
main_cpp = generate_main(component_config_path("select_set_hub.yaml"))
assert _call("power", "set_index(3)") in main_cpp
-23
View File
@@ -1,5 +1,4 @@
#include <gtest/gtest.h>
#include <algorithm>
#include <cmath>
#include <cstring>
@@ -272,28 +271,6 @@ TEST(BufAppendStrP, AppendsAndTruncates) {
EXPECT_EQ(pos, 7u);
}
// --- progmem_copy_str() ---
TEST(ProgmemCopyStr, CopiesUpToTheNullOrTheEndAtEveryAlignment) {
static constexpr char TEXT[] = "0123456789abcdefghij";
constexpr size_t len = sizeof(TEXT) - 1;
for (size_t offset = 0; offset < 4; offset++) {
// Reads whole aligned words, so the source is a zero padded copy
alignas(4) char src[32] = {};
memcpy(src + offset, TEXT, sizeof(TEXT));
for (size_t room = 0; room <= len + 2; room++) {
SCOPED_TRACE(::testing::Message() << "offset " << offset << " room " << room);
char dst[32];
memset(dst, '#', sizeof(dst));
const char *const end = progmem_copy_str(dst, dst + room, src + offset);
const size_t copied = std::min(room, len);
EXPECT_EQ(end, dst + copied);
EXPECT_EQ(memcmp(dst, TEXT, copied), 0);
EXPECT_EQ(dst[copied], '#'); // No terminator written
}
}
}
// --- base64 encode/decode ---
static const char BASE64_ALPHABET[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
@@ -1,140 +0,0 @@
#include <gtest/gtest.h>
#include <cstdarg>
#include <cstring>
#include <string>
#include <thread>
#include <vector>
#include "esphome/components/logger/log_buffer.h"
#include "esphome/components/logger/logger.h"
#include "esphome/core/log.h"
namespace esphome::logger::testing {
ESPHOME_LOG_TAG(PLAIN_TAG, "plain.test");
static void format_into(LogBuffer &buf, const char *format, ...) {
va_list args;
va_start(args, format);
buf.format_body(format, args);
va_end(args);
}
TEST(UnformattedLogTest, BodyMatchesFormattedBodyAtEverySize) {
const std::string long_text(100, 'y');
const char *texts[] = {"", "short", "ends with newlines\n\n", "\n", "a\nb", long_text.c_str()};
constexpr uint16_t max_size = 48;
for (uint16_t size = 1; size <= max_size; size++) {
for (uint16_t start = 0; start <= size; start++) {
for (const char *text : texts) {
SCOPED_TRACE(::testing::Message() << "size " << size << " start " << start << " text '" << text << "'");
char formatted[max_size], unformatted[max_size], flash[max_size];
memset(formatted, '#', sizeof(formatted));
memset(unformatted, '#', sizeof(unformatted));
memset(flash, '#', sizeof(flash));
LogBuffer a{formatted, size, start};
LogBuffer b{unformatted, size, start};
LogBuffer c{flash, size, start};
format_into(a, "%s", text);
b.write_unformatted_body(text, strlen(text));
// The flash copy reads whole aligned words, so give it a zero padded copy
alignas(4) char padded[128] = {};
char *const in_flash = padded + (start & 3);
memcpy(in_flash, text, strlen(text) + 1);
c.write_unformatted_body_p(in_flash);
ASSERT_EQ(a.pos, b.pos);
ASSERT_EQ(a.pos, c.pos);
// Up to and including the null terminator; vsnprintf() may leave bytes after it that nothing reads
const size_t used = (a.pos < size) ? a.pos + 1 : size;
ASSERT_EQ(memcmp(formatted, unformatted, used), 0);
ASSERT_EQ(memcmp(formatted, flash, used), 0);
}
}
}
}
struct PlainLine {
uint8_t level;
std::string message;
};
static void capture_plain(void *instance, uint8_t level, const char *tag, const char *message, size_t len) {
static_cast<std::vector<PlainLine> *>(instance)->push_back({level, std::string(message, len)});
}
class UnformattedLoggerTest : public ::testing::Test {
protected:
void SetUp() override {
this->previous_ = global_logger;
this->logger_.pre_setup();
this->logger_.set_log_level(ESPHOME_LOG_LEVEL_DEBUG);
this->logger_.add_log_callback(&this->lines_, capture_plain);
}
void TearDown() override { global_logger = this->previous_; }
Logger *previous_{nullptr};
Logger logger_{0};
std::vector<PlainLine> lines_;
};
TEST_F(UnformattedLoggerTest, LineWithoutArgumentsMatchesFormattedLine) {
esp_log_str_(ESPHOME_LOG_LEVEL_INFO, PLAIN_TAG, 7, "no arguments here\n");
esp_log_printf_(ESPHOME_LOG_LEVEL_INFO, PLAIN_TAG, 7, "%s", "no arguments here\n");
ASSERT_EQ(this->lines_.size(), 2u);
EXPECT_EQ(this->lines_[0].level, ESPHOME_LOG_LEVEL_INFO);
EXPECT_EQ(this->lines_[0].message, this->lines_[1].message);
EXPECT_NE(this->lines_[0].message.find("[I][plain.test:007]: no arguments here\033[0m"), std::string::npos)
<< this->lines_[0].message;
}
TEST_F(UnformattedLoggerTest, PercentSignIsStillFormatted) {
ESP_LOGW(PLAIN_TAG, "100%% done");
ASSERT_EQ(this->lines_.size(), 1u);
EXPECT_NE(this->lines_[0].message.find("]: 100% done"), std::string::npos) << this->lines_[0].message;
}
TEST_F(UnformattedLoggerTest, FormatExpressionIsEvaluatedOnce) {
const char *const messages[] = {"first", "second"};
int index = 0;
#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wformat-security"
ESP_LOGW(PLAIN_TAG, messages[index++]);
#pragma GCC diagnostic pop
EXPECT_EQ(index, 1);
ASSERT_EQ(this->lines_.size(), 1u);
EXPECT_NE(this->lines_[0].message.find("]: first"), std::string::npos) << this->lines_[0].message;
}
TEST_F(UnformattedLoggerTest, TemporaryFormatOutlivesTheCall) {
#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wformat-security"
ESP_LOGW(PLAIN_TAG, std::string(80, 'x').c_str());
#pragma GCC diagnostic pop
ASSERT_EQ(this->lines_.size(), 1u);
EXPECT_NE(this->lines_[0].message.find("]: " + std::string(80, 'x')), std::string::npos) << this->lines_[0].message;
}
TEST_F(UnformattedLoggerTest, LevelIsStillFiltered) {
this->logger_.set_log_level(ESPHOME_LOG_LEVEL_WARN);
ESP_LOGI(PLAIN_TAG, "hidden");
ESP_LOGW(PLAIN_TAG, "shown");
ASSERT_EQ(this->lines_.size(), 1u);
EXPECT_EQ(this->lines_[0].level, ESPHOME_LOG_LEVEL_WARN);
}
#ifdef USE_HOST
TEST_F(UnformattedLoggerTest, WorkerThreadLineGoesThroughTaskBuffer) {
this->logger_.create_pthread_key();
std::thread worker([]() { esp_log_str_(ESPHOME_LOG_LEVEL_INFO, PLAIN_TAG, 7, "from a worker"); });
worker.join();
EXPECT_TRUE(this->lines_.empty());
#ifdef USE_ESPHOME_TASK_LOG_BUFFER
this->logger_.loop();
ASSERT_EQ(this->lines_.size(), 1u);
EXPECT_NE(this->lines_[0].message.find("from a worker"), std::string::npos) << this->lines_[0].message;
#endif
}
#endif // USE_HOST
} // namespace esphome::logger::testing
@@ -10,7 +10,6 @@ namespace {
constexpr uint8_t OLD_AP[6] = {0x02, 0x00, 0x00, 0x00, 0x00, 0x01};
constexpr uint8_t NEW_AP[6] = {0x02, 0x00, 0x00, 0x00, 0x00, 0x02};
constexpr uint8_t NO_AP[6] = {};
ExpectedDisconnect armed() {
ExpectedDisconnect expected;
@@ -21,20 +20,20 @@ ExpectedDisconnect armed() {
TEST(ExpectedDisconnectTest, PreviousApLeaveIsExpectedOnce) {
auto expected = armed();
EXPECT_EQ(expected.consume_disconnect(OLD_AP, true), ExpectedLeave::EXPECTED_LEAVE_AP);
EXPECT_TRUE(expected.consume_disconnect(OLD_AP, true));
EXPECT_FALSE(expected.is_previous_ap(OLD_AP));
// A second leave from the same AP is a real disconnect
EXPECT_EQ(expected.consume_disconnect(OLD_AP, true), ExpectedLeave::EXPECTED_LEAVE_NONE);
EXPECT_FALSE(expected.consume_disconnect(OLD_AP, true));
}
TEST(ExpectedDisconnectTest, OtherDisconnectsAreRealAndEndTheExpectation) {
auto other_ap = armed();
EXPECT_EQ(other_ap.consume_disconnect(NEW_AP, true), ExpectedLeave::EXPECTED_LEAVE_NONE);
EXPECT_EQ(other_ap.consume_disconnect(OLD_AP, true), ExpectedLeave::EXPECTED_LEAVE_NONE);
EXPECT_FALSE(other_ap.consume_disconnect(NEW_AP, true));
EXPECT_FALSE(other_ap.consume_disconnect(OLD_AP, true));
auto other_reason = armed();
EXPECT_EQ(other_reason.consume_disconnect(OLD_AP, false), ExpectedLeave::EXPECTED_LEAVE_NONE);
EXPECT_EQ(other_reason.consume_disconnect(OLD_AP, true), ExpectedLeave::EXPECTED_LEAVE_NONE);
EXPECT_FALSE(other_reason.consume_disconnect(OLD_AP, false));
EXPECT_FALSE(other_reason.consume_disconnect(OLD_AP, true));
}
TEST(ExpectedDisconnectTest, QueuedConnectionFromPreviousApKeepsTheExpectation) {
@@ -42,17 +41,17 @@ TEST(ExpectedDisconnectTest, QueuedConnectionFromPreviousApKeepsTheExpectation)
EXPECT_TRUE(expected.is_previous_ap(OLD_AP));
EXPECT_FALSE(expected.is_previous_ap(NEW_AP));
EXPECT_EQ(expected.consume_disconnect(OLD_AP, true), ExpectedLeave::EXPECTED_LEAVE_AP);
EXPECT_TRUE(expected.consume_disconnect(OLD_AP, true));
}
TEST(ExpectedDisconnectTest, NothingIsExpectedUnlessArmed) {
ExpectedDisconnect expected;
EXPECT_FALSE(expected.is_previous_ap(OLD_AP));
EXPECT_EQ(expected.consume_disconnect(OLD_AP, true), ExpectedLeave::EXPECTED_LEAVE_NONE);
EXPECT_FALSE(expected.consume_disconnect(OLD_AP, true));
auto cleared = armed();
cleared.clear();
EXPECT_EQ(cleared.consume_disconnect(OLD_AP, true), ExpectedLeave::EXPECTED_LEAVE_NONE);
EXPECT_FALSE(cleared.consume_disconnect(OLD_AP, true));
}
// Stands in for esp_wifi_sta_get_ap_info() and esp_wifi_disconnect(): the AP record is gone after disconnecting
@@ -83,7 +82,16 @@ TEST(ExpectedDisconnectTest, DisconnectExpectsTheLeaveOfTheApItLeft) {
Driver driver;
EXPECT_TRUE(disconnect(expected, driver));
EXPECT_EQ(expected.consume_disconnect(OLD_AP, true), ExpectedLeave::EXPECTED_LEAVE_AP);
EXPECT_TRUE(expected.consume_disconnect(OLD_AP, true));
}
TEST(ExpectedDisconnectTest, NoApMeansNoExpectation) {
ExpectedDisconnect expected;
Driver driver;
driver.has_ap = false;
EXPECT_TRUE(disconnect(expected, driver));
EXPECT_FALSE(expected.consume_disconnect(OLD_AP, true));
}
TEST(ExpectedDisconnectTest, FailedDisconnectExpectsNothing) {
@@ -98,29 +106,5 @@ TEST(ExpectedDisconnectTest, FailedDisconnectExpectsNothing) {
EXPECT_FALSE(expected.is_previous_ap(NEW_AP));
}
TEST(ExpectedDisconnectTest, DisconnectWithoutApExpectsTheAttemptsLeave) {
ExpectedDisconnect expected;
Driver driver;
driver.has_ap = false;
EXPECT_TRUE(disconnect(expected, driver));
// A real AP never matches the attempt's zero BSSID
EXPECT_FALSE(expected.is_previous_ap(NEW_AP));
// The driver reports the attempt as left, with no BSSID, after the next one starts
EXPECT_EQ(expected.consume_disconnect(NO_AP, true), ExpectedLeave::EXPECTED_LEAVE_ATTEMPT);
EXPECT_EQ(expected.consume_disconnect(NO_AP, true), ExpectedLeave::EXPECTED_LEAVE_NONE);
}
TEST(ExpectedDisconnectTest, AttemptLeaveExpectationEndsOnARealFailure) {
ExpectedDisconnect expected;
Driver driver;
driver.has_ap = false;
EXPECT_TRUE(disconnect(expected, driver));
// A probe failure of the new attempt is real, and nothing stays expected after it
EXPECT_EQ(expected.consume_disconnect(NO_AP, false), ExpectedLeave::EXPECTED_LEAVE_NONE);
EXPECT_EQ(expected.consume_disconnect(NO_AP, true), ExpectedLeave::EXPECTED_LEAVE_NONE);
}
} // namespace
} // namespace esphome::wifi::testing
+1
View File
@@ -17,6 +17,7 @@ void setup() {
App.pre_setup(name, sizeof(name) - 1, friendly_name, sizeof(friendly_name) - 1);
auto *log = new logger::Logger(115200); // NOLINT
log->pre_setup();
log->set_uart_selection(logger::UART_SELECTION_UART0);
App.register_component_(log);
auto *wifi = new wifi::WiFiComponent(); // NOLINT
@@ -1,62 +0,0 @@
esphome:
name: select-set-constant
host:
api:
logger:
globals:
- id: next_option
type: std::string
initial_value: '"low"'
select:
- platform: template
name: "Fan"
id: fan
optimistic: true
options: [low, medium, high]
initial_option: low
- platform: template
name: "Picked"
id: picked
options: [low, medium, high]
update_interval: never
lambda: return id(next_option);
set_action:
- lambda: id(next_option) = std::string(x.c_str(), x.size());
button:
- platform: template
name: "Set High"
on_press:
- select.set:
id: fan
option: high
- platform: template
name: "Set Medium"
on_press:
- select.set:
id: fan
option: medium
- platform: template
name: "Set Invalid"
on_press:
- select.set:
id: fan
option: turbo
- platform: template
name: "Pick High"
on_press:
- lambda: id(next_option) = "high";
- component.update: picked
- platform: template
name: "Pick Invalid"
on_press:
- lambda: id(next_option) = "turbo";
- component.update: picked
- platform: template
name: "Pick Medium"
on_press:
- lambda: id(next_option) = "medium";
- component.update: picked
@@ -1,50 +0,0 @@
"""select.set with a constant option and a template select lambda, both set by index."""
from __future__ import annotations
from aioesphomeapi import SelectInfo, SelectState
import pytest
from .log_utils import LineWaiter
from .state_utils import press_and_wait, require_entity, subscribe_after_initial_states
from .types import APIClientConnectedFactory, RunCompiledFunction
from .wait_utils import wait_or_fail
@pytest.mark.asyncio
async def test_select_set_constant(
yaml_config: str,
run_compiled: RunCompiledFunction,
api_client_connected: APIClientConnectedFactory,
) -> None:
waiter_log = LineWaiter()
invalid_lambda = waiter_log.expect("'Picked': Invalid option turbo")
# An unknown constant keeps the lookup by text and its warning, never a compiled index
invalid_constant = waiter_log.expect("'Fan' - No option set")
out_of_bounds = waiter_log.expect("out of bounds")
async with (
run_compiled(yaml_config, line_callback=waiter_log.callback),
api_client_connected() as client,
):
entities, _ = await client.list_entities_services()
fan = require_entity(entities, "fan", SelectInfo, exact=True)
picked = require_entity(entities, "picked", SelectInfo, exact=True)
sub = await subscribe_after_initial_states(client, entities)
async def press(button: str, key: int) -> str:
state = await press_and_wait(
client, entities, sub.waiter, button, SelectState, key=key
)
return state.state
assert await press("set_high", fan.key) == "high"
# An option the select does not have changes nothing; the next press is the next state
client.button_command(require_entity(entities, "set_invalid").key)
assert await press("set_medium", fan.key) == "medium"
await wait_or_fail(invalid_constant, 5.0, "invalid constant was not logged")
assert not out_of_bounds.done()
assert await press("pick_high", picked.key) == "high"
client.button_command(require_entity(entities, "pick_invalid").key)
await wait_or_fail(invalid_lambda, 5.0, "invalid lambda option was not logged")
assert await press("pick_medium", picked.key) == "medium"
@@ -326,21 +326,6 @@ def test_bind_key__rejects_non_hex_pair_length() -> None:
cv.bind_key("0123456789ABCDEF0123456789ABCDE")
@pytest.mark.parametrize(
"value",
(
" A A A A A A A A A A A A A A A A",
"0123456789ABCDEF0123456789ABCD-B",
"+E123456789ABCDEF0123456789ABCDE",
),
)
def test_bind_key__rejects_sign_padded_pairs(value: str) -> None:
# int(part, 16) accepts surrounding whitespace and a +/- sign, which would
# silently validate a key different from the one the user typed.
with pytest.raises(Invalid, match="hex values"):
cv.bind_key(value)
def test_bind_key__direct_call_with_name_validates_with_that_name() -> None:
# Passing both a value and a name validates immediately using the custom
# name for error wording, and still tags the result sensitive.