mirror of
https://github.com/esphome/esphome.git
synced 2026-11-17 18:46:08 +00:00
Compare commits
9
Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
07c5ef5c2c | ||
|
|
75ba3cbbae | ||
|
|
da6686fe83 | ||
|
|
e988dbd872 | ||
|
|
6d5d793986 | ||
|
|
1263a56c52 | ||
|
|
8f07b49fd9 | ||
|
|
75c3e8e00c | ||
|
|
7d697a8a62 |
No files matched your search
@@ -20,7 +20,9 @@ driven, what the logs show, and the same flow on a `dev` build when a quirk coul
|
||||
down. Never run `esphome logs` in a `while true` loop: the loop respawns it and holds the port.
|
||||
2. **Credentials stay local.** Put a `secrets.yaml` next to the scratch configs and reference it with
|
||||
`!secret`. A copy of a device's own config keeps using the secrets file next to that config: two
|
||||
checkouts can hold different networks, and the wrong one leaves the device unreachable after the OTA. Never paste a config with credentials into a PR, never commit scratch configs or logs.
|
||||
checkouts can hold different networks, and the wrong one leaves the device unreachable after the OTA.
|
||||
Generate an `api_key` with `openssl rand -base64 32` when you have none. Never paste a config with
|
||||
credentials into a PR, never commit scratch configs or logs.
|
||||
3. **Build from the tree under test.** `python -m esphome` puts the current directory first on
|
||||
`sys.path`, so run it with the checkout or worktree as the working directory and pass the yaml by
|
||||
its absolute path. A venv `esphome` entry point may import an installed snapshot instead of the
|
||||
@@ -158,7 +160,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`
|
||||
|
||||
|
||||
@@ -87,3 +87,4 @@ One file per component, named after it:
|
||||
- [`improv.md`](improv.md): Improv serial with stray bytes, scan, bad and good passwords from a client that reads frames out of the log stream, Improv over BLE, per board notes
|
||||
- [`arduino_pico.md`](arduino_pico.md): arduino-pico framework bump A/B: W5100S Ethernet soak, Pico W fallback AP client count, Pico W bluetooth_proxy GATT hold, web_server SSE stalls, `/events` churn crashes and never completed connects, W5100S OTA without `interrupt_pin`
|
||||
- [`bluedroid_gattc_queue.md`](bluedroid_gattc_queue.md): queued MTU request and execute write recursion that overflows BTU_TASK, ble_client central against an ESP32-S3 peer
|
||||
- [`tuya.md`](tuya.md): Tuya MCU simulator on a cross-wired second UART (handshake, every datapoint type, command frames logged), hub vs entity order, dump A/B
|
||||
@@ -67,3 +67,26 @@ reference from an ESP32 proxy (unset `API_KEY` for a plaintext API).
|
||||
- On rp2 before #22679, BTstack truncated 32-bit base UUIDs with a nonzero low half to 16 bits (`5ec10001-...` came
|
||||
out as `00000001-...`); a zero low half (5ec10000) came through intact.
|
||||
- Connects through the Pico W proxy fail with 0x3E now and then (see `reference/boards.md`); the scripts retry.
|
||||
|
||||
## Scan complete never arrives (#22770)
|
||||
|
||||
`scripts/ble_scan_complete/`: `scan.yaml` is a proxy config like the report's (ESP32-C6, interval = window = 1100 ms,
|
||||
debug sensors, `api: reboot_timeout: 0s`, a `duration` substitution, 5 s by default; the validator wants at least
|
||||
three intervals, so 2 s is refused). The tracker watchdog is `duration * 2`, so a short duration shortens it too.
|
||||
`scaninst.yaml` adds `scan_inst.hpp` and five `-Wl,--wrap` flags that count one scan's duration timer through
|
||||
Bluedroid: `BTM_BleScan` starts and stops, `btu_start_timer(BTU_TTYPE_BLE_SCAN)`, the timer's alarm firing (a shim on
|
||||
its `osi_alarm_new` callback, BTC task), `btm_ble_timeout` (BTU task) and the INQ_CMPL handed to `btc_transfer_context`
|
||||
(PID 5, act 3; the param is 108 bytes in libbt, so do not compare with the C++ `sizeof`). A 30 s interval logs them as
|
||||
`scaninst`; a lost completion shows where the chain stopped (timer > fire + 1, fire > timeout, timeout > cmpl_post, or
|
||||
cmpl_post above the ESPHome-side `ends / 2`). `ha.py HOST OUT SECONDS [--toggle S --jitter J]` is the Home Assistant
|
||||
stand-in (raw adverts, scanner state, set mode passive once, optional active/passive toggles). `soak.sh TAG HOST
|
||||
SECONDS [ha.py options]` (env PORT, API_KEY or else `apikey.txt`) runs a bounded serial capture plus ha.py.
|
||||
`wait.sh CAPTURE SECONDS [FROM_LINE]` blocks until a timeout, drop or unexpected-state line and prints the counters.
|
||||
- Bluedroid and IDF log lines reach the ESPHome log as tag `esp-idf` at VERBOSE (`[V][esp-idf:000][BTU_TASK]: E ...`),
|
||||
so a user capturing at INFO over the API never sees a Bluedroid error.
|
||||
- A stop from Home Assistant landing just after the timer fired (toggle period 5.03 s with 0.08 s jitter against 5 s
|
||||
scans hits it about once per 20 minutes) gives `Unexpected state: STOPPING on scan complete`, `stop complete -
|
||||
status 1`, `BTM_BleScan scan not active`; the tracker recovers. A stop while STARTING logs `Cannot stop scan:
|
||||
STARTING` and the mode change waits for the next restart.
|
||||
- With about 55 devices around (about 95 adverts/s), no lost completion in about 2800 natural 5 s cycles on 2026.10.2
|
||||
(VERBOSE and INFO builds) plus about 670 toggled stops, and none on dev.
|
||||
@@ -86,3 +86,15 @@ Notes:
|
||||
them from the device's `Got state` log lines.
|
||||
- Turning a script off stops it mid run (the flag stays on). A script that finishes at once reports on, then off, in
|
||||
the same second.
|
||||
|
||||
## Subscription order and dump order (#22725)
|
||||
|
||||
- `hadrive.py` also prints `<phase> SUBORDER NNN <line>` in the order the device sent the subscriptions (the SUB lines
|
||||
are sorted). `grep -v SUBORDER drive.txt` diffs the sets, `grep SUBORDER` the order.
|
||||
- Dump block order per build: `python3 scripts/lognorm.py boot1.txt | grep -a "^\[C\]\[" |
|
||||
sed -E 's/^\[C\]\[([a-z_.0-9]+)\]: (.{0,40}).*/\1 | \2/' | awk -F' [|] ' '{ if ($1!=p) {print $1 " :: " $2; p=$1} }'`
|
||||
prints one line per component run; diff dump text between builds through `lognorm.py` too, which drops the
|
||||
`:NNN` line numbers.
|
||||
- Idle ESP8266 free heap in this config swings by 2 kB between readings; compare the steady reading (the fifth one
|
||||
after boot) per boot, which matched the heap_size change exactly (+168 B for 15 import entities on #22725).
|
||||
- The wizard light (`l_wiz`) needs #21908; a stack based before it needs it dropped on both builds (`NO_LIGHT=1`).
|
||||
@@ -44,3 +44,116 @@ only the last line sees "Failed to upload" first, so rerun before blaming the bu
|
||||
output. `scripts/esp8266_lock_load/ir-one-pin.yaml` does this, and its `run.sh` adds Wi-Fi load (see
|
||||
[one_wire.md](one_wire.md)). Under load, the build that sends the whole frame in one lock hit a Hardware Watchdog in
|
||||
`wDev_ProcessFiq` after 11 minutes.
|
||||
|
||||
## Climate IR loopback over the API (#22704 whirlpool, #22709 climate_ir)
|
||||
|
||||
- Pattern: one image with a sending climate and a receiving climate (`receiver_id`) per platform on one shared pin, an
|
||||
aioesphomeapi script that sends climate calls and reads every climate's state after each step. Several platforms can
|
||||
share one receiver; each rejects the others' frames. Scripts in `scripts/climate_ir_loop/`: `drive.py`
|
||||
(steps `Name:mode=cool,t=24,fan=low,swing=vertical,preset=eco` or `svc:action?flag=1`), `run.sh NAME PORT [STEPS]`
|
||||
with `steps.sh` (prelude and steps), `frames.py NAME` (splits the `dump: raw` lines per step), `compare.py A B
|
||||
[TOL] [--flat]` (per step, or with `--flat` the whole sequence timing by timing), `base.yaml` (network, API and
|
||||
logger), `c-common.yaml` (Samsung, Friedrich, Siemens IRA211 and Mistral senders and receivers), `esp32-ir.yaml`
|
||||
(GPIO27) and `esp8266-ir.yaml` (GPIO12) as one-pin loopback board packages (`pin`, and on the ESP32 `rmt_symbols`
|
||||
and `receive_symbols`, are substitutions; the Pronto configs include them too), `c32pr.yaml` (an example run file)
|
||||
and `whirlpool-inject-common.yaml` (injected frame actions).
|
||||
- ESP32-PICO-D4 kit: GPIO4 does not loop back cleanly (every mark reads 162 µs short, a -162 space leads the frame, so
|
||||
nothing decodes). GPIO27 gives exact timings (572/553/1659 µs). Try another pin before suspecting the code.
|
||||
- RMT ends a frame on any level longer than `idle`, mark or space. A whirlpool 14 byte frame ends after a 7,960 µs gap,
|
||||
so its decoder needs the trailing idle to be within 25% of that, but the 9 ms header mark must stay below idle:
|
||||
`idle: 9500us` works, 8500 cuts the header off.
|
||||
- Restored climate state changes the first frames (power toggles, Samsung power section): start every run with a
|
||||
prelude that puts each sender in a fixed state (every field each sender supports, such as humidity for daikin_arc
|
||||
or a fan mode daikin_brc supports), and compare only the frames after it. ESP8266 restore lives in RTC memory, so a
|
||||
receiver's state can carry over from the previous image.
|
||||
- Inject frames a sender cannot produce (an out of range temperature code, a short frame) from an API action lambda with
|
||||
`item_bytes<BitTimings>` and `call.perform()`.
|
||||
- Assigning frames to steps by host timestamps is fragile on the D1 mini (a log line can land after the next step
|
||||
starts); compare the whole frame sequence in order instead. Two frames sent back to back (siemens_ira211 power plus
|
||||
sync) come out as one dump, and the gap between them varies by up to 1 ms on ESP8266.
|
||||
- Samsung power frames are 347 timings: an ESP32 receiver needs `rmt_symbols: 384` and `receive_symbols: 384`.
|
||||
|
||||
## Protocol A/B with `dump: all` (#22711)
|
||||
|
||||
- One button per `transmit_<protocol>` action, one binary sensor and one `on_<protocol>` trigger per protocol, each
|
||||
logging a marker; count markers and compare the dumper lines of the dev and PR runs in order. All of nec, dooya,
|
||||
nexus, aok and cardin_s438 decode in the ESP32 GPIO27 loopback with the default idle.
|
||||
- Cardin S438 picks a random seed per frame (`random_uint32()`), so its pronto and cardin lines differ between any two
|
||||
runs; compare code and channel only. Each press sends 3 repeats, so its trigger and binary sensor fire 3 times.
|
||||
- Start the dump extraction at the first `MARK tx` of any protocol: the API lists buttons by name, not config order.
|
||||
- A Python-only change can still move generated code: diff `src/main.cpp` of the two builds (field order in action
|
||||
setters, defaults now written explicitly).
|
||||
|
||||
## Large climate frames in the loopback (#22710)
|
||||
|
||||
- At 115200 baud the serial log cannot keep up with long `dump: raw` frames (hitachi_ac344 is 691 timings, daikin
|
||||
sends 3 frames per command). Lines get lost or merged, frames show up under the wrong step, and the script reads the
|
||||
receiver state before it has decoded the frame. Use `logger: baud_rate: 921600`, capture with `--baud 921600`, and
|
||||
wait 4 s per step (`BAUD=921600 STEP_S=4 run.sh ...`). A CH340 D1 mini can garble 921600; 460800 carries the long
|
||||
frames cleanly there.
|
||||
- On the classic ESP32 the receiver has no RX ping-pong, so a whole frame has to fit `rmt_symbols`. With a 64 symbol
|
||||
transmitter, `rmt_symbols: 448` and `receive_symbols: 448` hold hitachi_ac344.
|
||||
- Receivers that keep state the frame does not carry (climate_ir_lg swing is a toggle, zhlt01 keeps its preset on
|
||||
"none") start each run from restored state, so compare them without those fields or erase the preferences first.
|
||||
|
||||
## Whirlpool A/B by hex lines (#22710 retest)
|
||||
|
||||
- whirlpool logs every frame at VERBOSE (`Sending:` and `Received:` hex), so on the D1 mini compare those bytes line by
|
||||
line instead of raw timings. One image per model (DG11J1-3A, HITACHI_HCRA31NEWH, HISENSE_ARG; "Hisense ARG" is a
|
||||
whirlpool model, not its own component). `scripts/climate_ir_loop/whirlpool/`: `cl.yaml` (the model
|
||||
as a substitution, combined with `../base.yaml` and `../esp8266-ir.yaml`), `steps.sh`, and `all.sh PORT BASE_TREE PR_TREE`, which builds and runs base then PR per model so the
|
||||
board ends on a PR build.
|
||||
- The HISENSE_ARG receiver reports fan medium for fan_only while the sender says auto (dev too).
|
||||
|
||||
## Pronto dump A/B with `dump: all` (#22752)
|
||||
|
||||
- Scripts in `scripts/remote_base/pronto/`: `gen.py` writes `gen-common.yaml` (buttons for NEC and raw frames of
|
||||
21/41/43/121/245 timings, a soak button with heap logs, a `pronto:` binary sensor), `esp32-ir.yaml` (GPIO27) and
|
||||
`d1-ir.yaml` (GPIO12) include it with the board packages from `climate_ir_loop/`, `run.sh NAME TREE YAML PORT BAUD
|
||||
ROUNDS` flashes, captures, presses every tx button ROUNDS times and then the soak button with `../press.py`, and
|
||||
counts the markers, triggers and `on_press` lines on serial; `extract.py` keeps the dump lines without timestamps
|
||||
and source line numbers, `consist.py` checks every dump against the `on_pronto` text, `jit.py` counts frames that
|
||||
differ between two runs.
|
||||
- The received frame ends with the idle gap as an extra timing (`0181` for the 10 ms idle), so a `pronto:` binary sensor
|
||||
built from the sent timings needs one more number at the end, or the length check rejects every frame. A 41 timing
|
||||
frame is then exactly 230 characters, one full log chunk: a good boundary case.
|
||||
- ESP32 RMT loopback gives byte-identical Pronto text across builds. The D1 mini edge interrupt moves timings by one
|
||||
Pronto unit between any two runs (about half the frames), so compare D1 runs by dump against trigger consistency.
|
||||
- `dump: all` prints NEC lines between the `on_pronto` trigger output and the Pronto dump; the API binary sensor
|
||||
ON/OFF pair can arrive collapsed, so count `on_press` log lines on serial instead.
|
||||
|
||||
## Protocol decode A/B on the host, climate fuzz, AEHA/Haier/Mirage loopback (#22773)
|
||||
|
||||
- `scripts/remote_base/hostharness/`: a `host:` config (`rbhost.yaml`) with the local `rbtest` component in
|
||||
`scripts/remote_base/ext/` (AUTO_LOAD remote_base, `request_protocol()` for every protocol source) and `rbh.hpp` called from `on_boot`; it encodes, mutates, decodes
|
||||
and dumps, then `exit(0)`. Run the `program` binary and compare stdout through `scripts/lognorm.py`. The Haier
|
||||
and Mirage decoders want one more item after the last mark (the receiver's idle gap): append `-20000` to transmitter
|
||||
timings or every clean frame fails.
|
||||
- AEHA frames longer than 35 bytes decode as their first 35 bytes (the trailer mark equals a bit mark).
|
||||
- `scripts/climate_ir_fuzz/`: copy `climate_fuzz.hpp` plus the matching `*_fuzz.cpp.example` (as `.cpp`) into
|
||||
`tests/components/<climate>/` of each tree (haier_tundra also needs `haier_init.py` as `__init__.py`), run
|
||||
`script/cpp_unit_test.py`, diff the `FUZZ` lines. `run()` appends the receiver's idle gap to every frame, and
|
||||
`aeha_random()` builds the random frames for the AEHA climates. All tests of one run share one main.cpp: use a macro include guard,
|
||||
not `#pragma once`, for a header copied to several test dirs.
|
||||
- Lambda idiom checks: compile `void f(AEHAData x) { ... }` snippets with `-fsyntax-only -I<host build>/src` and
|
||||
`#include "esphome.h"` (format_hex lives in alloc_helpers.h).
|
||||
- Loopback config `ir-common.yaml` (AEHA 2 to 35 bytes, Haier, Mirage from an API button, `../press.py HOST tx_all:14 ...`;
|
||||
the trigger lambdas call `rbh_on_frame()` from `rbh.hpp`, so keep the run file next to it). An ESP32-PICO-D4
|
||||
on GPIO27 never decodes 34 or 35 byte AEHA, even with `rmt_symbols: 384`, `buffer_size: 8kb` (dev too); a D1 mini on
|
||||
GPIO12 decodes all of them.
|
||||
|
||||
## Every dumper over a saved corpus, on the host (#22814)
|
||||
|
||||
`scripts/remote_base/dumpall/`: the same `rbtest` component requests every `_PROTOCOL_STEMS` source; `rbh.hpp` needs a
|
||||
`dumpers.h` that `gen_dumpers.py TREE` writes from the `DECLARE_REMOTE_PROTOCOL(...)` lines of the tree (plus Pronto,
|
||||
RCSwitch, ABBWelcome; Raw only when nothing matched). Encoders that randomise (Cardin S438 picks a random seed) make two builds see
|
||||
different inputs: run once with `RBH_CORPUS=<file>` to write the corpus, then replay that file on both builds.
|
||||
Merge back-to-back marks or spaces before decoding (Drayton never decodes otherwise). Nudging a quarter of all
|
||||
timings to the tolerance edges proves nothing (the frame fails elsewhere): put only the sync mark at lo-1, lo, hi,
|
||||
hi+1 and keep the rest exact; that variant caught a planted `< max` in 1,000 decodes per protocol.
|
||||
`cmp.sh` compares the DEBUG and VERBOSE outputs (`rbhost.yaml` built with `-s level DEBUG` and `-s level VERBOSE`)
|
||||
through `scripts/lognorm.py`.
|
||||
- Loopback with `dump: all` (`ir-common.yaml`, `../press.py HOST tx_all:9 tx_all:9 verbose tx_all:9 debug`): Beo4 never decodes on one pin (needs its carrier) and
|
||||
Drayton's channel/command form has no checksum; compare decoded lines, raw and Pronto timings jitter between runs.
|
||||
- `cc-common.yaml`: an ESP32-C3 with a CC1101 in async OOK at 433.92 MHz, GDO0 into remote_receiver, for the RF
|
||||
dumpers on real air (a quiet band gives only a few Pronto dumps a minute and no protocol hits).
|
||||
@@ -0,0 +1,30 @@
|
||||
# Tuya MCU simulator (#22724)
|
||||
|
||||
No Tuya device needed. `scripts/tuya_sim/`: `tuyasim.hpp` is a Tuya MCU on a second UART of the same ESP32, cross-wired
|
||||
through the GPIO matrix to the hub's UART (hub tx 9 rx 10, MCU tx 10 rx 9 on an ESP32-PICO-D4, `board: pico32`,
|
||||
`allow_other_uses: true` on all four pins, 9600 baud). It answers heartbeat (0x00 first, then 0x01), product query,
|
||||
conf query with an empty payload (so the hub takes the WIFI_STATE path), wifi state and datapoint query (one report with
|
||||
all 14 datapoints), echoes each DATAPOINT_DELIVER back as a report like a real MCU, and logs every hub frame as
|
||||
`SIM RX #n cmd XX: <hex>`. `tuyasim::start()` (the "Sim start" button) plays 18 scripted steps 1.5 s apart: every
|
||||
datapoint type (bool, value incl. negative, string, enum, 1/2/4 byte bitmask, raw), a four datapoint frame, one
|
||||
DATAPOINT_REPORT_SYNC (the hub must ack with 0x23), a repeated value.
|
||||
|
||||
- `tuya.yaml`: hub plus binary_sensor, switch, three sensors (int, enum, bitmask), three text sensors (string, enum,
|
||||
raw), three selects (enum, string, int), water heater (switch/target/current/mode), number and fan. Hub
|
||||
`on_datapoint_update` lambdas log `HUB dpN x=.. <entity>=..` with the entity's state, and entity triggers log
|
||||
`ENT <id> <value>`, so the hub-before-entity order and the old state the hub sees are visible per datapoint.
|
||||
`name: ${name}`, built with `pio_build.sh compile <name>` per tree.
|
||||
- `tuyadrive.py HOST OUT.json` (API_KEY env): subscribes states, presses Sim start, then sends switch, select,
|
||||
water heater (on, target, mode eco/electric, off), number and fan commands 2 s apart; prints per phase counts.
|
||||
- `run.sh NAME TREE` (env PORT, API_KEY): upload, serial capture with a reset (stopped after the drive), drive.
|
||||
- `compare.sh A B`: normalises both logs with `../lognorm.py` and diffs API states, the entity dump block, the 1 s re-dump, the HUB/ENT order and the non-heartbeat
|
||||
frames the simulator received. Heartbeats move with timing, so they are left out.
|
||||
|
||||
Facts from the run:
|
||||
- At init the fan delivers its own state (`1f01000100`) before DP31 is known and the hub logs `Setting unknown
|
||||
datapoint 31`; same on dev.
|
||||
- The API coalesces states published in one loop pass: the four datapoint water heater frame gives one state.
|
||||
- A switch or select command for the value it already has sends no frame.
|
||||
- Water heater target 55 with multiplier 0.5 is sent as 110 (`15 02 0004 0000006e`); eco/electric as enum 0/2 on DP23.
|
||||
- #22724 changed only the dump order (children now print right after the hub's own lines); `setup_priority:` on a
|
||||
tuya sensor is rejected on the PR and accepted on dev.
|
||||
@@ -36,3 +36,22 @@ tracker as scan results: everything after the radio runs as normal.
|
||||
first one it hears.
|
||||
- Each frame arrives about 4 times; the counts matched the injection run exactly apart from one radio loss, so the
|
||||
over the air comparison allows two missing publishes per tag.
|
||||
|
||||
## Every Xiaomi platform at once (#22727)
|
||||
|
||||
- `gen_all.py` writes `frames_all.hpp`, `xball_entities.yaml` and `expected_all.json`: one device per platform
|
||||
(the 20 listener-only platforms, `xiaomi_mue4094rt`, `xiaomi_rtcgq02lm` hub, `xiaomi_body_scale`, ATC, PVVX),
|
||||
10 cycles, every frame sent twice. `mibeacon.py` holds the frame builders and the C++ injector that `gen.py`
|
||||
and `gen_all.py` share. Each entity logs `PUB <dev>.<key> <value>` on publish (binary sensors with
|
||||
`trigger_on_initial_state: true`). Frame shapes that parse: long encrypted (fc 0x58) only up to 24 bytes, so
|
||||
a 7 byte `th` object needs the short 19 byte frame, which only lywsd02mmc, xmwsdj04mmc, mjyd02yla, cgpr1 and
|
||||
rtcgq02lm accept; hhccjcy10 is FD50 9 bytes and miscale v1 181D 10 bytes (no counter, the repeat publishes
|
||||
again); mue4094rt is a plain fc 0x40 frame; body_scale an S200 0x4E16 object in a 24 byte short frame.
|
||||
`xiaomi_mccgq02hl` publishes 9 of 10: the first frame is "closed", which equals its initial off state.
|
||||
- `xball.yaml` is an ESP32-S3 (ESP-IDF, no network, USB_SERIAL_JTAG log) that injects one cycle per second from
|
||||
20 s and logs free heap before and after (`IDLE` lines). `run_all.sh TREE NAME PORT` builds, flashes, captures
|
||||
and counts; `count_all.py cap1 cap2 ...` also says whether the values are identical across builds.
|
||||
- `xbsetup_priority.yaml`: `esphome config` with `setup_priority:` on a moved sensor, a moved binary sensor and
|
||||
mue4094rt (still a component); remove one at a time, since validation stops at the first platform error.
|
||||
- On #22727: 441 publishes on the PR, its stacked base and dev, identical values; dump text identical, the PR prints
|
||||
the 20 listener dumps after `BLE Tracker:`.
|
||||
@@ -37,3 +37,23 @@ npm1300/*-cap.err
|
||||
npm1300/*-run.log
|
||||
rp2_sched_lock/*.txt
|
||||
rp2_sched_lock/*.log
|
||||
climate_ir_loop/*-*.txt
|
||||
climate_ir_loop/*-frames.json
|
||||
climate_ir_loop/whirlpool/w??base.yaml
|
||||
climate_ir_loop/whirlpool/w??pr.yaml
|
||||
tuya_sim/*.txt
|
||||
tuya_sim/*.json
|
||||
remote_base/pronto/*.txt
|
||||
remote_base/pronto/gen-common.yaml
|
||||
remote_base/dumpall/dumpers.h
|
||||
remote_base/dumpall/db.txt
|
||||
remote_base/dumpall/dp.txt
|
||||
remote_base/dumpall/vb.txt
|
||||
remote_base/dumpall/vp.txt
|
||||
xiaomi_ble/*-cap.txt
|
||||
xiaomi_ble/frames_all.hpp
|
||||
xiaomi_ble/xball_entities.yaml
|
||||
xiaomi_ble/expected_all.json
|
||||
xiaomi_ble/run_*.yaml
|
||||
ble_scan_complete/*.txt
|
||||
ble_scan_complete/*.err
|
||||
@@ -0,0 +1,129 @@
|
||||
"""Home Assistant stand-in for a bluetooth_proxy scan soak (esphome#22770).
|
||||
|
||||
usage: ha.py HOST OUT SECONDS [--toggle SECS [--jitter SECS]] [--mode passive|active|none]
|
||||
|
||||
Subscribes to raw advertisements and the scanner state like Home Assistant, sets the scanner mode once after connect
|
||||
(passive by default, as HA does for its proxies) and with --toggle flips active/passive every SECS seconds to stress
|
||||
the stop/restart path. Writes one line per event to OUT: scanner state changes, a per-second advert count, mode
|
||||
requests, connection drops. Reconnects after a drop (device reboot) and keeps going until SECONDS have passed.
|
||||
API_KEY in the env selects Noise.
|
||||
"""
|
||||
|
||||
import argparse
|
||||
import asyncio
|
||||
import pathlib
|
||||
import random
|
||||
import sys
|
||||
import time
|
||||
|
||||
_here = pathlib.Path(__file__).resolve().parent
|
||||
# apiconn.py: copied next to it, or the skill scripts dir
|
||||
sys.path[:0] = [str(_here), str(_here.parent)]
|
||||
|
||||
from apiconn import FATAL, connect, stop_event # noqa: E402
|
||||
|
||||
|
||||
def main() -> None:
|
||||
ap = argparse.ArgumentParser()
|
||||
ap.add_argument("host")
|
||||
ap.add_argument("out")
|
||||
ap.add_argument("seconds", type=float)
|
||||
ap.add_argument("--toggle", type=float, default=0)
|
||||
ap.add_argument(
|
||||
"--jitter",
|
||||
type=float,
|
||||
default=0,
|
||||
help="random +- seconds added to each toggle period",
|
||||
)
|
||||
ap.add_argument("--mode", default="passive", choices=["passive", "active", "none"])
|
||||
a = ap.parse_args()
|
||||
asyncio.run(run(a))
|
||||
|
||||
|
||||
async def run(a: argparse.Namespace) -> None:
|
||||
from aioesphomeapi import BluetoothScannerMode
|
||||
|
||||
end = time.monotonic() + a.seconds
|
||||
f = pathlib.Path(a.out).open("a", encoding="utf-8", buffering=1) # noqa: SIM115
|
||||
|
||||
def w(s: str) -> None:
|
||||
f.write(
|
||||
f"{time.strftime('%H:%M:%S')}.{int(time.time() * 1000) % 1000:03d} {s}\n"
|
||||
)
|
||||
|
||||
session = 0
|
||||
connected = False
|
||||
while time.monotonic() < end:
|
||||
session += 1
|
||||
stopped, on_stop = stop_event()
|
||||
# a try takes up to 11 s (10 s timeout, 1 s pause): bound the tries by the soak time left instead of
|
||||
# cancelling connect(), which could leave a half open client behind
|
||||
tries = max(1, int((end - time.monotonic()) // 11))
|
||||
try:
|
||||
c = await connect(a.host, tries=tries, on_stop=on_stop)
|
||||
except SystemExit as e:
|
||||
w(f"CONNECT FAILED {e}")
|
||||
# a wrong key or a plaintext/Noise mismatch never recovers: stop instead of soaking without a client
|
||||
if isinstance(e.__context__, FATAL):
|
||||
raise
|
||||
await asyncio.sleep(5)
|
||||
continue
|
||||
w(f"CONNECTED session={session}")
|
||||
connected = True
|
||||
adv = [0]
|
||||
macs: set[int] = set()
|
||||
|
||||
def on_raw(resp, adv=adv, macs=macs) -> None:
|
||||
for x in resp.advertisements:
|
||||
adv[0] += 1
|
||||
macs.add(x.address)
|
||||
|
||||
def on_state(st) -> None:
|
||||
w(f"SCANNER state={st.state.name} mode={st.mode.name}")
|
||||
|
||||
unsub_raw = c.subscribe_bluetooth_le_raw_advertisements(on_raw)
|
||||
unsub_state = c.subscribe_bluetooth_scanner_state(on_state)
|
||||
if a.mode != "none":
|
||||
m = (
|
||||
BluetoothScannerMode.PASSIVE
|
||||
if a.mode == "passive"
|
||||
else BluetoothScannerMode.ACTIVE
|
||||
)
|
||||
c.bluetooth_scanner_set_mode(m)
|
||||
w(f"SET_MODE {a.mode}")
|
||||
last_toggle = time.monotonic()
|
||||
period = a.toggle
|
||||
active = a.mode == "active"
|
||||
last_report = time.monotonic()
|
||||
while time.monotonic() < end and not stopped.is_set():
|
||||
await asyncio.sleep(0.05)
|
||||
now = time.monotonic()
|
||||
if a.toggle and now - last_toggle >= period:
|
||||
active = not active
|
||||
c.bluetooth_scanner_set_mode(
|
||||
BluetoothScannerMode.ACTIVE
|
||||
if active
|
||||
else BluetoothScannerMode.PASSIVE
|
||||
)
|
||||
w(f"SET_MODE {'active' if active else 'passive'}")
|
||||
last_toggle = now
|
||||
period = a.toggle + random.uniform(-a.jitter, a.jitter)
|
||||
if now - last_report >= 10:
|
||||
w(f"ADV {adv[0]} in {now - last_report:.1f}s devices={len(macs)}")
|
||||
adv[0] = 0
|
||||
macs.clear()
|
||||
last_report = now
|
||||
if stopped.is_set():
|
||||
w("DISCONNECTED")
|
||||
await asyncio.sleep(2)
|
||||
continue
|
||||
unsub_raw()
|
||||
unsub_state()
|
||||
await c.disconnect()
|
||||
w("DONE")
|
||||
if not connected:
|
||||
sys.exit("no API connection during the whole soak")
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
main()
|
||||
@@ -0,0 +1,78 @@
|
||||
substitutions:
|
||||
name: scan2610
|
||||
duration: 5s
|
||||
|
||||
esphome:
|
||||
name: ${name}
|
||||
|
||||
esp32:
|
||||
board: esp32-c6-devkitc-1
|
||||
variant: esp32c6
|
||||
flash_size: 4MB
|
||||
framework:
|
||||
type: esp-idf
|
||||
|
||||
logger:
|
||||
level: VERBOSE
|
||||
hardware_uart: USB_SERIAL_JTAG
|
||||
logs:
|
||||
esp32_ble_tracker: VERBOSE
|
||||
esp32_ble: DEBUG
|
||||
bluetooth_proxy: DEBUG
|
||||
api: DEBUG
|
||||
api.connection: DEBUG
|
||||
api.service: DEBUG
|
||||
sensor: INFO
|
||||
text_sensor: INFO
|
||||
debug: INFO
|
||||
wifi: DEBUG
|
||||
scheduler: DEBUG
|
||||
component: DEBUG
|
||||
|
||||
ota:
|
||||
- platform: esphome
|
||||
|
||||
wifi:
|
||||
ssid: !secret wifi_ssid
|
||||
password: !secret wifi_password
|
||||
|
||||
esp32_ble_tracker:
|
||||
scan_parameters:
|
||||
interval: 1100ms
|
||||
window: 1100ms
|
||||
active: true
|
||||
duration: ${duration}
|
||||
|
||||
bluetooth_proxy:
|
||||
active: true
|
||||
|
||||
sensor:
|
||||
- platform: wifi_signal
|
||||
name: WiFi signal
|
||||
update_interval: 60s
|
||||
- platform: uptime
|
||||
name: Uptime
|
||||
- platform: debug
|
||||
free:
|
||||
name: Free memory
|
||||
block:
|
||||
name: Largest free block
|
||||
min_free:
|
||||
name: Lowest free memory
|
||||
loop_time:
|
||||
name: Loop time
|
||||
|
||||
debug:
|
||||
update_interval: 60s
|
||||
|
||||
text_sensor:
|
||||
- platform: version
|
||||
name: ESPHome version
|
||||
- platform: debug
|
||||
reset_reason:
|
||||
name: Reset reason
|
||||
|
||||
api:
|
||||
reboot_timeout: 0s
|
||||
encryption:
|
||||
key: !secret api_key
|
||||
@@ -0,0 +1,126 @@
|
||||
#pragma once
|
||||
// esphome#22770 instrumentation: follows one scan's duration timer through Bluedroid.
|
||||
// Link with -Wl,--wrap= for BTM_BleScan, btu_start_timer, btm_ble_timeout, osi_alarm_new, btc_transfer_context.
|
||||
#include <cstdint>
|
||||
#include <esp_gap_ble_api.h>
|
||||
#include <esp_timer.h>
|
||||
#include <freertos/FreeRTOS.h>
|
||||
|
||||
extern "C" {
|
||||
struct scan_inst_t {
|
||||
uint32_t ble_scan_start; // BTM_BleScan(TRUE) calls
|
||||
uint32_t ble_scan_start_bad; // ... that did not return BTM_CMD_STARTED
|
||||
uint32_t ble_scan_stop; // BTM_BleScan(FALSE) calls
|
||||
uint32_t timer_start; // btu_start_timer(BTU_TTYPE_BLE_SCAN)
|
||||
uint32_t alarm_fire; // esp_timer fired for the scan timer entry (btc thread)
|
||||
uint32_t ble_timeout; // btm_ble_timeout for the scan timer entry (btu thread)
|
||||
uint32_t inq_cmpl_post; // INQ_CMPL handed to btc_transfer_context
|
||||
uint32_t inq_cmpl_post_fail; // ... that failed
|
||||
uint8_t pid;
|
||||
void *scan_tle;
|
||||
int64_t last_timer_start_us;
|
||||
int64_t last_fire_us;
|
||||
int64_t last_timeout_us;
|
||||
int64_t last_inq_cmpl_us;
|
||||
};
|
||||
// Written from the Bluedroid and timer tasks, read on the main loop: every access holds the lock, since the 64 bit
|
||||
// timestamps can tear on the 32 bit chips. Read with scan_inst_snapshot() and log the copy.
|
||||
static scan_inst_t g_scan_inst{};
|
||||
static portMUX_TYPE g_scan_inst_mux = portMUX_INITIALIZER_UNLOCKED;
|
||||
|
||||
static scan_inst_t scan_inst_snapshot() {
|
||||
portENTER_CRITICAL(&g_scan_inst_mux);
|
||||
scan_inst_t copy = g_scan_inst;
|
||||
portEXIT_CRITICAL(&g_scan_inst_mux);
|
||||
return copy;
|
||||
}
|
||||
|
||||
typedef void (*scan_inst_cb_t)(void *);
|
||||
// the scan timer entry's original alarm callback (btu_general_alarm_cb)
|
||||
static scan_inst_cb_t s_orig_cb = nullptr;
|
||||
|
||||
static void scan_inst_shim(void *data) {
|
||||
int64_t now = esp_timer_get_time();
|
||||
portENTER_CRITICAL(&g_scan_inst_mux);
|
||||
g_scan_inst.alarm_fire++;
|
||||
g_scan_inst.last_fire_us = now;
|
||||
portEXIT_CRITICAL(&g_scan_inst_mux);
|
||||
s_orig_cb(data);
|
||||
}
|
||||
|
||||
uint8_t __real_BTM_BleScan(uint8_t start, uint32_t duration, void *a, void *b, void *c);
|
||||
uint8_t __wrap_BTM_BleScan(uint8_t start, uint32_t duration, void *a, void *b, void *c) {
|
||||
uint8_t r = __real_BTM_BleScan(start, duration, a, b, c);
|
||||
portENTER_CRITICAL(&g_scan_inst_mux);
|
||||
if (start) {
|
||||
g_scan_inst.ble_scan_start++;
|
||||
if (r != 1 /* BTM_CMD_STARTED */)
|
||||
g_scan_inst.ble_scan_start_bad++;
|
||||
} else {
|
||||
g_scan_inst.ble_scan_stop++;
|
||||
}
|
||||
portEXIT_CRITICAL(&g_scan_inst_mux);
|
||||
return r;
|
||||
}
|
||||
|
||||
void __real_btu_start_timer(void *p_tle, uint16_t type, uint32_t timeout_sec);
|
||||
void __wrap_btu_start_timer(void *p_tle, uint16_t type, uint32_t timeout_sec) {
|
||||
if (type == 109 /* BTU_TTYPE_BLE_SCAN */) {
|
||||
int64_t now = esp_timer_get_time();
|
||||
portENTER_CRITICAL(&g_scan_inst_mux);
|
||||
g_scan_inst.scan_tle = p_tle;
|
||||
g_scan_inst.timer_start++;
|
||||
g_scan_inst.last_timer_start_us = now;
|
||||
portEXIT_CRITICAL(&g_scan_inst_mux);
|
||||
}
|
||||
__real_btu_start_timer(p_tle, type, timeout_sec);
|
||||
}
|
||||
|
||||
void __real_btm_ble_timeout(void *p_tle);
|
||||
void __wrap_btm_ble_timeout(void *p_tle) {
|
||||
int64_t now = esp_timer_get_time();
|
||||
portENTER_CRITICAL(&g_scan_inst_mux);
|
||||
if (p_tle == g_scan_inst.scan_tle) {
|
||||
g_scan_inst.ble_timeout++;
|
||||
g_scan_inst.last_timeout_us = now;
|
||||
}
|
||||
portEXIT_CRITICAL(&g_scan_inst_mux);
|
||||
__real_btm_ble_timeout(p_tle);
|
||||
}
|
||||
|
||||
void *__real_osi_alarm_new(const char *name, scan_inst_cb_t cb, void *data, uint64_t expire);
|
||||
void *__wrap_osi_alarm_new(const char *name, scan_inst_cb_t cb, void *data, uint64_t expire) {
|
||||
// btu_start_timer's wrap records the scan entry before the real one creates its alarm. Shim every alarm made for
|
||||
// it, since Bluedroid may free and recreate it; they all use the same callback (btu_general_alarm_cb).
|
||||
portENTER_CRITICAL(&g_scan_inst_mux);
|
||||
void *scan_tle = g_scan_inst.scan_tle;
|
||||
portEXIT_CRITICAL(&g_scan_inst_mux);
|
||||
if (data != nullptr && data == scan_tle) {
|
||||
s_orig_cb = cb;
|
||||
return __real_osi_alarm_new(name, scan_inst_shim, data, expire);
|
||||
}
|
||||
return __real_osi_alarm_new(name, cb, data, expire);
|
||||
}
|
||||
|
||||
struct scan_inst_msg_t {
|
||||
uint8_t sig, aid, pid, act;
|
||||
};
|
||||
int __real_btc_transfer_context(scan_inst_msg_t *msg, void *arg, int arg_len, void *cp, void *fr);
|
||||
int __wrap_btc_transfer_context(scan_inst_msg_t *msg, void *arg, int arg_len, void *cp, void *fr) {
|
||||
bool cmpl = msg != nullptr && arg != nullptr && msg->sig == 1 && msg->act == ESP_GAP_BLE_SCAN_RESULT_EVT &&
|
||||
arg_len >= 4 && msg->pid == 5 /* BTC_PID_GAP_BLE */ &&
|
||||
static_cast<esp_ble_gap_cb_param_t *>(arg)->scan_rst.search_evt == ESP_GAP_SEARCH_INQ_CMPL_EVT;
|
||||
int r = __real_btc_transfer_context(msg, arg, arg_len, cp, fr);
|
||||
if (cmpl) {
|
||||
int64_t now = esp_timer_get_time();
|
||||
portENTER_CRITICAL(&g_scan_inst_mux);
|
||||
g_scan_inst.pid = msg->pid;
|
||||
g_scan_inst.inq_cmpl_post++;
|
||||
g_scan_inst.last_inq_cmpl_us = now;
|
||||
if (r != 0)
|
||||
g_scan_inst.inq_cmpl_post_fail++;
|
||||
portEXIT_CRITICAL(&g_scan_inst_mux);
|
||||
}
|
||||
return r;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,40 @@
|
||||
# scan.yaml plus the Bluedroid scan timer counters (scan_inst.hpp) logged every 30 s as `scaninst`
|
||||
substitutions:
|
||||
name: scaninst
|
||||
|
||||
packages:
|
||||
base: !include scan.yaml
|
||||
|
||||
esphome:
|
||||
includes:
|
||||
- scan_inst.hpp
|
||||
platformio_options:
|
||||
build_flags:
|
||||
- -Wl,--wrap=BTM_BleScan
|
||||
- -Wl,--wrap=btu_start_timer
|
||||
- -Wl,--wrap=btm_ble_timeout
|
||||
- -Wl,--wrap=osi_alarm_new
|
||||
- -Wl,--wrap=btc_transfer_context
|
||||
|
||||
esp32_ble_tracker:
|
||||
on_scan_end:
|
||||
- lambda: id(scan_ends)++;
|
||||
|
||||
globals:
|
||||
- id: scan_ends
|
||||
type: uint32_t
|
||||
initial_value: '0'
|
||||
|
||||
interval:
|
||||
- interval: 30s
|
||||
then:
|
||||
- lambda: |-
|
||||
const scan_inst_t g = scan_inst_snapshot();
|
||||
int64_t now = esp_timer_get_time();
|
||||
ESP_LOGI("scaninst", "start=%u bad=%u stop=%u timer=%u fire=%u timeout=%u cmpl_post=%u cmpl_fail=%u pid=%u ends=%u "
|
||||
"ago_ms timer=%lld fire=%lld timeout=%lld cmpl=%lld",
|
||||
(unsigned) g.ble_scan_start, (unsigned) g.ble_scan_start_bad, (unsigned) g.ble_scan_stop,
|
||||
(unsigned) g.timer_start, (unsigned) g.alarm_fire, (unsigned) g.ble_timeout,
|
||||
(unsigned) g.inq_cmpl_post, (unsigned) g.inq_cmpl_post_fail, (unsigned) g.pid, (unsigned) id(scan_ends),
|
||||
(now - g.last_timer_start_us) / 1000, (now - g.last_fire_us) / 1000,
|
||||
(now - g.last_timeout_us) / 1000, (now - g.last_inq_cmpl_us) / 1000);
|
||||
@@ -0,0 +1,15 @@
|
||||
#!/bin/bash
|
||||
# usage: soak.sh TAG HOST SECONDS [ha.py options] serial capture plus HA stand-in, both bounded
|
||||
# env: PORT (serial port), PY, API_KEY (falls back to apikey.txt next to the script; unset or - for plaintext)
|
||||
W=$(cd "$(dirname "$0")" && pwd); cd "$W" || exit 1; T=$1; H=$2; S=$3; shift 3
|
||||
PY=${PY:-python3}
|
||||
PORT=${PORT:?serial port}
|
||||
[ -z "${API_KEY+x}" ] && [ -f apikey.txt ] && API_KEY=$(cat apikey.txt)
|
||||
$PY "$(dirname "$W")/serial_capture.py" "$PORT" "$S" "$T.txt" > /dev/null 2>&1 &
|
||||
CAP=$!
|
||||
API_KEY=$API_KEY PYTHONUNBUFFERED=1 $PY ha.py "$H" "$T-ha.txt" "$S" "$@" > "$T-ha.err" 2>&1 &
|
||||
HA=$!
|
||||
wait $CAP; CRC=$?
|
||||
wait $HA; HRC=$?
|
||||
echo "$T done: capture rc=$CRC ha.py rc=$HRC (see $T-ha.err)"
|
||||
[ $CRC -eq 0 ] && [ $HRC -eq 0 ]
|
||||
@@ -0,0 +1,11 @@
|
||||
#!/bin/bash
|
||||
# usage: wait.sh CAPTURE SECONDS [FROM_LINE] block until an anomaly after FROM_LINE or SECONDS pass, then print status
|
||||
C=$1; L=${3:-1}
|
||||
P="Scan timeout exceeded|Scan never terminated|Dropped [0-9]+ BLE|Guru|Backtrace|Unexpected|cmpl_fail=[1-9]|bad=[1-9]"
|
||||
timeout "$2" tail -n +"$L" -F "$C" 2> /dev/null | grep -a -m1 -q -E "$P"
|
||||
tail -n +"$L" "$C" | awk -v p="$P" -v t="$(date +%T)" '
|
||||
$0 ~ p && shown < 5 { print; shown++ }
|
||||
/Starting scan/ { starts++ }
|
||||
/Scan complete, set/ { completes++ }
|
||||
END { printf "%s starts=%d completes=%d\n", t, starts, completes }'
|
||||
grep -a scaninst "$C" | tail -1 | cut -c1-260
|
||||
@@ -0,0 +1,116 @@
|
||||
#ifndef FUZZ22773_CLIMATE_FUZZ_H
|
||||
#define FUZZ22773_CLIMATE_FUZZ_H
|
||||
// Test only (esphome#22773 A/B): feeds a climate's own frames, mutated copies and random frames to on_receive
|
||||
// and prints whether each was accepted and the state that followed.
|
||||
#include <cstdio>
|
||||
#include <random>
|
||||
#include <vector>
|
||||
#include "esphome/components/climate_ir/climate_ir.h"
|
||||
#include "esphome/components/remote_base/aeha_protocol.h"
|
||||
#include "esphome/components/remote_base/remote_base.h"
|
||||
#include "esphome/core/application.h"
|
||||
#include "esphome/core/preferences.h"
|
||||
|
||||
namespace esphome::fuzz22773 {
|
||||
|
||||
class Cap : public remote_base::RemoteTransmitterBase {
|
||||
public:
|
||||
Cap() : RemoteTransmitterBase(nullptr) {}
|
||||
std::vector<remote_base::RawTimings> frames;
|
||||
|
||||
protected:
|
||||
void send_internal(uint32_t, uint32_t) override { this->frames.push_back(this->temp_.get_data()); }
|
||||
};
|
||||
|
||||
template<typename C> void prep(C &c, Cap &tx) {
|
||||
if (global_preferences != nullptr)
|
||||
global_preferences->reset();
|
||||
static char name[] = "fuzz";
|
||||
static char fn[] = "";
|
||||
App.pre_setup(name, sizeof(name) - 1, fn, 0);
|
||||
c.set_transmitter(&tx);
|
||||
static_cast<Component &>(c).setup();
|
||||
}
|
||||
|
||||
template<typename C> void print_state(const char *tag, int i, bool ok, C &c) {
|
||||
printf("FUZZ %s %d ok=%d mode=%d t=%.2f fan=%d swing=%d\n", tag, i, ok ? 1 : 0, (int) c.mode,
|
||||
(double) c.target_temperature, c.fan_mode.has_value() ? (int) *c.fan_mode : -1, (int) c.swing_mode);
|
||||
}
|
||||
|
||||
// A random AEHA frame of len bytes; half start with the climate's header (set_header), 3 in 4 use its address
|
||||
template<typename Header>
|
||||
void aeha_random(std::mt19937 &rng, remote_base::RawTimings &raw, size_t len, uint16_t address, Header set_header) {
|
||||
uint8_t d[35];
|
||||
for (auto &b : d)
|
||||
b = rng();
|
||||
if (rng() % 2)
|
||||
set_header(d);
|
||||
remote_base::RemoteTransmitData tx;
|
||||
remote_base::AEHAProtocol().encode(&tx, (rng() % 4) ? address : uint16_t(rng()), d, len);
|
||||
raw = tx.get_data();
|
||||
}
|
||||
|
||||
// make() returns a fresh climate; random_frame(rng, raw) fills raw with a random frame of the protocol, without the
|
||||
// trailing idle gap
|
||||
template<typename Make, typename Rand> void run(const char *tag, Make make, Rand random_frame) {
|
||||
std::mt19937 rng(22773);
|
||||
// Valid frames: everything the climate itself sends
|
||||
std::vector<remote_base::RawTimings> valid;
|
||||
{
|
||||
Cap tx;
|
||||
auto c = make();
|
||||
prep(*c, tx);
|
||||
for (auto mode : {climate::CLIMATE_MODE_OFF, climate::CLIMATE_MODE_COOL, climate::CLIMATE_MODE_HEAT,
|
||||
climate::CLIMATE_MODE_DRY, climate::CLIMATE_MODE_FAN_ONLY, climate::CLIMATE_MODE_HEAT_COOL})
|
||||
for (float t : {16.0f, 18.0f, 21.0f, 24.0f, 27.0f, 30.0f, 60.0f, 66.0f, 72.0f, 80.0f, 88.0f})
|
||||
for (auto fan : {climate::CLIMATE_FAN_AUTO, climate::CLIMATE_FAN_LOW, climate::CLIMATE_FAN_MEDIUM,
|
||||
climate::CLIMATE_FAN_HIGH}) {
|
||||
auto call = c->make_call();
|
||||
call.set_mode(mode);
|
||||
call.set_target_temperature(t);
|
||||
call.set_fan_mode(fan);
|
||||
call.perform();
|
||||
}
|
||||
valid = tx.frames;
|
||||
for (auto &v : valid)
|
||||
v.push_back(-20000); // the idle gap a receiver records after the last mark
|
||||
}
|
||||
int i = 0;
|
||||
auto feed = [&](const remote_base::RawTimings &raw, const char *kind) {
|
||||
Cap tx;
|
||||
auto c = make();
|
||||
prep(*c, tx);
|
||||
remote_base::RemoteReceiveData data(raw, 25, remote_base::TOLERANCE_MODE_PERCENTAGE);
|
||||
bool ok = static_cast<remote_base::RemoteReceiverListener &>(*c).on_receive(data);
|
||||
printf("%s ", kind);
|
||||
print_state(tag, i++, ok, *c);
|
||||
};
|
||||
for (const auto &v : valid) {
|
||||
feed(v, "valid");
|
||||
for (int m = 0; m < 4; m++) {
|
||||
auto raw = v;
|
||||
if (raw.size() < 8)
|
||||
continue;
|
||||
// a space swapped with another space's length flips a bit (or does nothing when they are equal)
|
||||
size_t a = 3 + rng() % (raw.size() - 3), b = 3 + rng() % (raw.size() - 3);
|
||||
if (raw[a] < 0 && raw[b] < 0)
|
||||
std::swap(raw[a], raw[b]);
|
||||
else
|
||||
raw[a] = raw[a] < 0 ? -int32_t(100 + rng() % 3000) : int32_t(100 + rng() % 3000);
|
||||
feed(raw, "mut");
|
||||
}
|
||||
auto t = v;
|
||||
t.resize(rng() % t.size());
|
||||
feed(t, "trunc");
|
||||
}
|
||||
for (int r = 0; r < 3000; r++) {
|
||||
remote_base::RawTimings raw;
|
||||
random_frame(rng, raw);
|
||||
raw.push_back(-20000);
|
||||
feed(raw, "rand");
|
||||
}
|
||||
fflush(stdout);
|
||||
}
|
||||
|
||||
} // namespace esphome::fuzz22773
|
||||
#endif
|
||||
@@ -0,0 +1,17 @@
|
||||
#include <gtest/gtest.h>
|
||||
#include <memory>
|
||||
#include "climate_fuzz.hpp"
|
||||
#include "esphome/components/friedrich/friedrich.h"
|
||||
namespace esphome::fuzz22773 {
|
||||
TEST(Fuzz22773, Friedrich) {
|
||||
run("friedrich", [] { return std::make_unique<friedrich::FriedrichClimate>(); },
|
||||
[](std::mt19937 &rng, remote_base::RawTimings &raw) {
|
||||
size_t len = (rng() % 3 == 0) ? 2 + rng() % 34 : ((rng() & 1) ? 14 : 5);
|
||||
aeha_random(rng, raw, len, 0x28C6, [](uint8_t *d) {
|
||||
d[0] = 0x00;
|
||||
d[1] = 0x08;
|
||||
d[2] = 0x08;
|
||||
});
|
||||
});
|
||||
}
|
||||
} // namespace esphome::fuzz22773
|
||||
@@ -0,0 +1,19 @@
|
||||
#include <gtest/gtest.h>
|
||||
#include <memory>
|
||||
#include "climate_fuzz.hpp"
|
||||
#include "esphome/components/haier_tundra/haier_tundra.h"
|
||||
#include "esphome/components/remote_base/haier_protocol.h"
|
||||
namespace esphome::fuzz22773 {
|
||||
TEST(Fuzz22773, HaierTundra) {
|
||||
run("haier_tundra", [] { return std::make_unique<haier_tundra::HaierTundra>(haier_tundra::HAIER_HEADER_UNIT_A); },
|
||||
[](std::mt19937 &rng, remote_base::RawTimings &raw) {
|
||||
uint8_t d[16];
|
||||
size_t len = (rng() % 3 == 0) ? 1 + rng() % 16 : 13;
|
||||
for (auto &b : d) b = rng();
|
||||
if (rng() % 2) d[0] = haier_tundra::HAIER_HEADER_UNIT_A;
|
||||
remote_base::RemoteTransmitData tx;
|
||||
remote_base::HaierProtocol().encode(&tx, d, len);
|
||||
raw = tx.get_data();
|
||||
});
|
||||
}
|
||||
} // namespace esphome::fuzz22773
|
||||
@@ -0,0 +1,12 @@
|
||||
from esphome.components import remote_base
|
||||
from esphome.types import ConfigType
|
||||
from tests.testing_helpers import ComponentManifestOverride
|
||||
|
||||
|
||||
def override_manifest(manifest: ComponentManifestOverride) -> None:
|
||||
manifest.auto_load = ["climate_ir", "climate"]
|
||||
|
||||
async def to_code_testing(config: ConfigType) -> None:
|
||||
remote_base.request_protocol("haier")
|
||||
|
||||
manifest.to_code = to_code_testing
|
||||
@@ -0,0 +1,16 @@
|
||||
#include <gtest/gtest.h>
|
||||
#include <memory>
|
||||
#include "climate_fuzz.hpp"
|
||||
#include "esphome/components/mistral_ir/mistral_ir.h"
|
||||
namespace esphome::fuzz22773 {
|
||||
TEST(Fuzz22773, MistralIR) {
|
||||
run("mistral", [] { return std::make_unique<mistral_ir::MistralIR>(); },
|
||||
[](std::mt19937 &rng, remote_base::RawTimings &raw) {
|
||||
size_t len = (rng() % 3 == 0) ? 2 + rng() % 34 : 12;
|
||||
aeha_random(rng, raw, len, mistral_ir::MISTRAL_ADDRESS, [](uint8_t *d) {
|
||||
d[0] = 0x56;
|
||||
d[2] = 0x08;
|
||||
});
|
||||
});
|
||||
}
|
||||
} // namespace esphome::fuzz22773
|
||||
@@ -0,0 +1,28 @@
|
||||
# Network, API and the VERBOSE logger setup shared by the climate loopback configs.
|
||||
esphome:
|
||||
name: ${name}
|
||||
wifi:
|
||||
ssid: !secret wifi_ssid
|
||||
password: !secret wifi_password
|
||||
power_save_mode: none
|
||||
logger:
|
||||
level: VERBOSE
|
||||
logs:
|
||||
wifi: DEBUG
|
||||
api: DEBUG
|
||||
api.service: DEBUG
|
||||
api.connection: DEBUG
|
||||
scheduler: DEBUG
|
||||
component: DEBUG
|
||||
esp32.preferences: DEBUG
|
||||
esp8266.preferences: DEBUG
|
||||
preferences: DEBUG
|
||||
mdns: DEBUG
|
||||
sensor: DEBUG
|
||||
remote_receiver: DEBUG
|
||||
remote_transmitter: DEBUG
|
||||
climate: DEBUG
|
||||
api:
|
||||
reboot_timeout: 0s
|
||||
ota:
|
||||
- platform: esphome
|
||||
@@ -0,0 +1,12 @@
|
||||
# Samsung, Friedrich, Siemens IRA211 and Mistral senders and receivers on one loopback pin.
|
||||
packages:
|
||||
base: !include base.yaml
|
||||
climate:
|
||||
- {platform: samsung, id: sam_tx, name: SAM TX, transmitter_id: irtx}
|
||||
- {platform: samsung, id: sam_rx, name: SAM RX, transmitter_id: irtx, receiver_id: irrx}
|
||||
- {platform: friedrich, id: fr_tx, name: FR TX, transmitter_id: irtx}
|
||||
- {platform: friedrich, id: fr_rx, name: FR RX, transmitter_id: irtx, receiver_id: irrx}
|
||||
- {platform: siemens_ira211, id: si_tx, name: SI TX, transmitter_id: irtx}
|
||||
- {platform: siemens_ira211, id: si_rx, name: SI RX, transmitter_id: irtx, receiver_id: irrx}
|
||||
- {platform: mistral_ir, id: mi_tx, name: MI TX, transmitter_id: irtx}
|
||||
- {platform: mistral_ir, id: mi_rx, name: MI RX, transmitter_id: irtx, receiver_id: irrx}
|
||||
@@ -0,0 +1,5 @@
|
||||
substitutions:
|
||||
name: c32pr
|
||||
packages:
|
||||
common: !include c-common.yaml
|
||||
board: !include esp32-ir.yaml
|
||||
@@ -0,0 +1,98 @@
|
||||
"""compare.py A B [TOL] [--flat]: compare two runs (A-frames.json, B-frames.json from frames.py).
|
||||
|
||||
Per step (default): frames equal within TOL us and RX states equal.
|
||||
--flat: the whole frame sequence in order, timing by timing, ignoring step boundaries (for the D1 mini, where a log
|
||||
line can land in the next step); reports every item over TOL and the largest deviation elsewhere.
|
||||
"""
|
||||
|
||||
import json
|
||||
import pathlib
|
||||
import sys
|
||||
|
||||
args = [a for a in sys.argv[1:] if a != "--flat"]
|
||||
FLAT = "--flat" in sys.argv[1:]
|
||||
TOL = int(args[2]) if len(args) > 2 else 0
|
||||
|
||||
|
||||
def load(name):
|
||||
steps = json.loads(pathlib.Path(f"{name}-frames.json").read_text(encoding="utf-8"))
|
||||
# an empty run must not compare as equal to another empty run
|
||||
if not steps or not any(s["frames"] for s in steps):
|
||||
sys.exit(f"{name}-frames.json has no steps or no frames")
|
||||
return steps
|
||||
|
||||
|
||||
def same_timing(u, v):
|
||||
return (u > 0) == (v > 0) and abs(u - v) <= TOL
|
||||
|
||||
|
||||
def frame_dev(p, q):
|
||||
"""Largest deviation of two frames, or None when they differ in length or a timing is over TOL."""
|
||||
if len(p) != len(q) or not all(
|
||||
same_timing(u, v) for u, v in zip(p, q, strict=False)
|
||||
):
|
||||
return None
|
||||
return max((abs(u - v) for u, v in zip(p, q, strict=False)), default=0)
|
||||
|
||||
|
||||
def flat(a, b):
|
||||
fa = [f for s in a for f in s["frames"]]
|
||||
fb = [f for s in b for f in s["frames"]]
|
||||
mx = bad = items = 0
|
||||
print("frames", len(fa), len(fb))
|
||||
if len(fa) != len(fb):
|
||||
print("DIFF frame counts differ (a lost or extra transmission)")
|
||||
bad += 1
|
||||
for i, (p, q) in enumerate(zip(fa, fb, strict=False)):
|
||||
if len(p) != len(q):
|
||||
print("frame", i, "length", len(p), len(q))
|
||||
bad += 1
|
||||
continue
|
||||
for j, (u, v) in enumerate(zip(p, q, strict=False)):
|
||||
items += 1
|
||||
if same_timing(u, v):
|
||||
mx = max(mx, abs(u - v))
|
||||
else:
|
||||
print("frame", i, "item", j, u, v)
|
||||
bad += 1
|
||||
print(f"items {items} over_tol {bad} max_dev_within_tol {mx}")
|
||||
return bad == 0
|
||||
|
||||
|
||||
def per_step(a, b):
|
||||
if len(a) != len(b):
|
||||
print(f"DIFF step counts {len(a)} vs {len(b)} (an interrupted run?)")
|
||||
sys.exit(1)
|
||||
same_f = same_s = maxdev = 0
|
||||
for x, y in zip(a, b, strict=False):
|
||||
assert x["step"] == y["step"]
|
||||
fa, fb = x["frames"], y["frames"]
|
||||
devs = [frame_dev(p, q) for p, q in zip(fa, fb, strict=False)]
|
||||
ok = len(fa) == len(fb) and None not in devs
|
||||
if ok:
|
||||
maxdev = max([maxdev, *devs])
|
||||
same_f += ok
|
||||
st = x["rx"] == y["rx"]
|
||||
same_s += st
|
||||
if not ok or not st:
|
||||
print(
|
||||
"DIFF",
|
||||
x["step"],
|
||||
"frames",
|
||||
len(fa),
|
||||
len(fb),
|
||||
[len(p) for p in fa],
|
||||
[len(q) for q in fb],
|
||||
)
|
||||
for k in x["rx"]:
|
||||
if x["rx"][k] != y["rx"].get(k):
|
||||
print(" ", k, x["rx"][k], "|", y["rx"].get(k))
|
||||
print(
|
||||
f"steps {len(a)}/{len(b)} frames_equal {same_f} rx_states_equal {same_s} "
|
||||
f"total_frames {sum(len(s['frames']) for s in a)}/{sum(len(s['frames']) for s in b)} max_dev_us {maxdev}"
|
||||
)
|
||||
return same_f == len(a) and same_s == len(a)
|
||||
|
||||
|
||||
if not (flat if FLAT else per_step)(load(args[0]), load(args[1])):
|
||||
sys.exit(1)
|
||||
@@ -0,0 +1,74 @@
|
||||
"""drive.py IP STEP...: STEP is 'Name:mode=cool,t=24,fan=low,swing=vertical' or 'svc:action'; prints climate states after each."""
|
||||
|
||||
import asyncio
|
||||
import os
|
||||
import pathlib
|
||||
import sys
|
||||
import time
|
||||
|
||||
sys.path.insert(0, str(pathlib.Path(__file__).resolve().parents[1]))
|
||||
from aioesphomeapi import (
|
||||
ClimateFanMode,
|
||||
ClimateInfo,
|
||||
ClimateMode,
|
||||
ClimatePreset,
|
||||
ClimateSwingMode,
|
||||
)
|
||||
from apiconn import connect
|
||||
|
||||
MODES = {m.name.lower(): m for m in ClimateMode}
|
||||
FANS = {m.name.lower(): m for m in ClimateFanMode}
|
||||
SWINGS = {m.name.lower(): m for m in ClimateSwingMode}
|
||||
PRESETS = {m.name.lower(): m for m in ClimatePreset}
|
||||
|
||||
|
||||
async def main() -> None:
|
||||
c = await connect(sys.argv[1])
|
||||
ents, svcs = await c.list_entities_services()
|
||||
clim = {e.name: e for e in ents if isinstance(e, ClimateInfo)}
|
||||
byk = {e.key: e.name for e in clim.values()}
|
||||
st = {}
|
||||
c.subscribe_states(
|
||||
lambda s: st.__setitem__(byk[s.key], s) if s.key in byk else None
|
||||
)
|
||||
await asyncio.sleep(2)
|
||||
|
||||
def show(tag):
|
||||
for n in sorted(st):
|
||||
s = st[n]
|
||||
print(
|
||||
f" {tag} {n:11s} mode={s.mode.name} target={s.target_temperature:g} fan={s.fan_mode.name} swing={s.swing_mode.name} preset={s.preset.name}"
|
||||
)
|
||||
|
||||
show("init")
|
||||
for step in sys.argv[2:]:
|
||||
name, _, args = step.partition(":")
|
||||
print(time.strftime("%H:%M:%S"), "STEP", step, flush=True)
|
||||
if name == "svc":
|
||||
sname, _, sargs = args.partition("?")
|
||||
data = {
|
||||
k: v == "1"
|
||||
for k, v in (a.split("=") for a in filter(None, sargs.split("&")))
|
||||
}
|
||||
await c.execute_service(next(s for s in svcs if s.name == sname), data)
|
||||
else:
|
||||
kw = {}
|
||||
for a in filter(None, args.split(",")):
|
||||
k, v = a.split("=")
|
||||
if k == "mode":
|
||||
kw["mode"] = MODES[v]
|
||||
elif k == "t":
|
||||
kw["target_temperature"] = float(v)
|
||||
elif k == "fan":
|
||||
kw["fan_mode"] = FANS[v]
|
||||
elif k == "swing":
|
||||
kw["swing_mode"] = SWINGS[v]
|
||||
elif k == "preset":
|
||||
kw["preset"] = PRESETS[v]
|
||||
c.climate_command(clim[name].key, **kw)
|
||||
await asyncio.sleep(float(os.environ.get("STEP_S", "2.5")))
|
||||
show("after")
|
||||
await c.disconnect()
|
||||
|
||||
|
||||
asyncio.run(main())
|
||||
@@ -0,0 +1,24 @@
|
||||
# One-pin IR loopback on an ESP32 devkit: transmitter and receiver share GPIO27, RMT reads its own output.
|
||||
# Override the substitutions (or `dump:`) from the including config.
|
||||
substitutions:
|
||||
pin: GPIO27
|
||||
rmt_symbols: "384"
|
||||
receive_symbols: "384"
|
||||
esp32:
|
||||
board: esp32dev
|
||||
framework:
|
||||
type: esp-idf
|
||||
remote_transmitter:
|
||||
id: irtx
|
||||
pin:
|
||||
number: ${pin}
|
||||
allow_other_uses: true
|
||||
carrier_duty_percent: 100%
|
||||
remote_receiver:
|
||||
id: irrx
|
||||
pin:
|
||||
number: ${pin}
|
||||
allow_other_uses: true
|
||||
dump: raw
|
||||
rmt_symbols: ${rmt_symbols}
|
||||
receive_symbols: ${receive_symbols}
|
||||
@@ -0,0 +1,23 @@
|
||||
# One-pin IR loopback on a D1 mini: transmitter and receiver share GPIO12. The receiver's setup leaves the shared pin
|
||||
# an input, so an interval drives it as an output again. Override the substitutions (or `dump:`) from the including
|
||||
# config.
|
||||
substitutions:
|
||||
pin: "12"
|
||||
esp8266:
|
||||
board: d1_mini
|
||||
remote_transmitter:
|
||||
id: irtx
|
||||
pin:
|
||||
number: ${pin}
|
||||
allow_other_uses: true
|
||||
carrier_duty_percent: 100%
|
||||
remote_receiver:
|
||||
id: irrx
|
||||
pin:
|
||||
number: ${pin}
|
||||
allow_other_uses: true
|
||||
dump: raw
|
||||
interval:
|
||||
- interval: 250ms
|
||||
then:
|
||||
- lambda: pinMode(${pin}, OUTPUT);
|
||||
@@ -0,0 +1,53 @@
|
||||
"""frames.py NAME: per step, the raw frames and the RX states. Prints JSON to NAME-frames.json."""
|
||||
|
||||
import json
|
||||
import pathlib
|
||||
import re
|
||||
import sys
|
||||
|
||||
n = sys.argv[1]
|
||||
steps = []
|
||||
cur = None
|
||||
with pathlib.Path(f"{n}-drive.txt").open(encoding="utf-8") as fh:
|
||||
for text in fh:
|
||||
m = re.match(r"(\d\d:\d\d:\d\d) STEP (.*)", text)
|
||||
if m:
|
||||
cur = {"t": m[1], "step": m[2], "rx": {}, "tx": {}, "frames": []}
|
||||
steps.append(cur)
|
||||
m = re.match(r"\s+after (\S+ \S+)\s+(.*)", text)
|
||||
if m and cur:
|
||||
(cur["rx"] if m[1].endswith("RX") else cur["tx"])[m[1]] = m[2]
|
||||
frames = []
|
||||
buf = None
|
||||
with pathlib.Path(f"{n}-ser.txt").open(encoding="utf-8", errors="replace") as fh:
|
||||
for text in fh:
|
||||
m = re.match(r"\[(\d\d:\d\d:\d\d)\]\[I\]\[remote\.raw(?::\d+)?\]: ?(.*)", text)
|
||||
if not m:
|
||||
continue
|
||||
ts, body = m[1], m[2]
|
||||
if body.startswith("Received Raw:"):
|
||||
buf = [ts, body[len("Received Raw:") :]]
|
||||
elif buf is not None:
|
||||
buf[1] += body
|
||||
# the raw dumper splits long frames into lines that end with a comma; the last line does not
|
||||
if buf is not None and not body.rstrip().endswith(","):
|
||||
frames.append(
|
||||
(
|
||||
buf[0],
|
||||
[
|
||||
int(x)
|
||||
for x in buf[1].replace(" ", "").strip(",").split(",")
|
||||
if x
|
||||
],
|
||||
)
|
||||
)
|
||||
buf = None
|
||||
for ts, f in frames:
|
||||
owner = None
|
||||
for s in steps:
|
||||
if s["t"] <= ts:
|
||||
owner = s
|
||||
if owner:
|
||||
owner["frames"].append(f)
|
||||
pathlib.Path(f"{n}-frames.json").write_text(json.dumps(steps), encoding="utf-8")
|
||||
print(n, "steps", len(steps), "frames", sum(len(s["frames"]) for s in steps))
|
||||
@@ -0,0 +1,30 @@
|
||||
#!/bin/bash
|
||||
# run.sh NAME PORT [STEPS_FILE]: serial capture with a reset, then the prelude and steps of STEPS_FILE (default
|
||||
# steps.sh here, which sets the PRE and STEPS arrays) through drive.py; writes NAME-ser.txt, NAME-pre.txt and
|
||||
# NAME-drive.txt. env: PY, BAUD (default 115200; use 921600 for long climate frames), STEP_S (default 2.5, the wait
|
||||
# after each step; the prelude always uses 2.5 s since it is not compared)
|
||||
D=$(cd "$(dirname "$0")" && pwd); S=$(dirname "$D"); N=${1:?name}; P=${2:?port}; PY=${PY:-python3}
|
||||
# shellcheck source=/dev/null
|
||||
source "${3:-$D/steps.sh}"
|
||||
cd "$D" || exit 1
|
||||
pkill -f "[s]erial_capture.py $P "; sleep 1
|
||||
# a stale capture would feed the IP lookup below
|
||||
rm -f "$D/$N-ser.txt"
|
||||
nohup $PY "$S/serial_capture.py" "$P" 1000 "$D/$N-ser.txt" --reset --baud "${BAUD:-115200}" > /dev/null 2>&1 &
|
||||
IP=
|
||||
for _ in $(seq 1 60); do
|
||||
IP=$(LC_ALL=C grep -a -o -E "IP Address: [0-9.]+" "$N-ser.txt" 2>/dev/null | tail -1 | cut -d' ' -f3)
|
||||
[ -n "$IP" ] && break; sleep 2
|
||||
done
|
||||
[ -z "$IP" ] && { echo "no IP in $N-ser.txt"; pkill -f "[s]erial_capture.py $P "; exit 1; }
|
||||
sleep 3
|
||||
STEP_S=2.5 $PY drive.py "$IP" "${PRE[@]}" > "$N-pre.txt" 2>&1; PRC=$?
|
||||
STEP_S=${STEP_S:-2.5} $PY drive.py "$IP" "${STEPS[@]}" > "$N-drive.txt" 2>&1; DRC=$?
|
||||
sleep 2; pkill -f "[s]erial_capture.py $P "; sleep 1
|
||||
echo "$N steps=$(grep -c STEP "$N-drive.txt") raw=$(LC_ALL=C grep -a -c 'Received Raw' "$N-ser.txt")" \
|
||||
"boots=$(LC_ALL=C grep -a -c 'Running through setup\|rst cause' "$N-ser.txt")"
|
||||
# a failed drive leaves a partial run: fail so frames.py and compare.py are not run on it
|
||||
if [ $PRC -ne 0 ] || [ $DRC -ne 0 ]; then
|
||||
echo "$N drive failed: prelude rc=$PRC steps rc=$DRC (see $N-pre.txt, $N-drive.txt)"
|
||||
exit 1
|
||||
fi
|
||||
@@ -0,0 +1,14 @@
|
||||
# prelude puts every sender in the same state on both builds; frames before the first MARK step are not compared
|
||||
PRE=("SAM TX:mode=off,t=24,fan=auto,swing=off" "FR TX:mode=off,t=72,fan=auto,swing=off" "SI TX:mode=off,t=21,fan=auto,preset=comfort" "MI TX:mode=off,t=24,fan=auto")
|
||||
STEPS=()
|
||||
for p in "SAM TX" "FR TX" "MI TX"; do
|
||||
for m in heat_cool cool dry fan_only heat off; do STEPS+=("${p}:mode=$m"); done
|
||||
STEPS+=("${p}:mode=cool")
|
||||
if [ "$p" = "FR TX" ]; then fans=(high medium low quiet auto); else fans=(low medium high auto); fi
|
||||
for f in "${fans[@]}"; do STEPS+=("${p}:fan=$f"); done
|
||||
[ "$p" = "SAM TX" ] && for s in vertical horizontal both off; do STEPS+=("${p}:swing=$s"); done
|
||||
STEPS+=("${p}:mode=off")
|
||||
done
|
||||
STEPS+=("SI TX:mode=heat_cool" "SI TX:preset=eco" "SI TX:preset=comfort")
|
||||
for f in low medium high auto; do STEPS+=("SI TX:fan=$f"); done
|
||||
STEPS+=("SI TX:mode=off")
|
||||
@@ -0,0 +1,52 @@
|
||||
# API actions that send whirlpool frames the sender cannot produce; add a board package and whirlpool/cl.yaml.
|
||||
packages:
|
||||
base: !include base.yaml
|
||||
remote_receiver:
|
||||
# a short frame ends in the idle gap, which the decoder takes as the second divider only up to 9,950 us
|
||||
# (7,960 + 25%); the 9 ms header mark must stay below idle
|
||||
idle: 9500us
|
||||
api:
|
||||
actions:
|
||||
# A Hitachi frame the sender cannot produce: temperature nibble 15 (16 + 15 = 31 C, above the 30 C max), cool, fan low
|
||||
- action: inject_t15
|
||||
then:
|
||||
- lambda: |-
|
||||
static constexpr remote_base::BitTimings T{.bit_mark = 572, .one_space = 1659, .zero_space = 553};
|
||||
uint8_t s[21] = {0};
|
||||
s[0] = 0xA9; s[1] = 0x59; s[2] = 0x03; s[3] = 0xF2; s[6] = 0x80; s[10] = 0x20; s[15] = 0x02;
|
||||
s[18] = 0x38; s[19] = 0x08;
|
||||
s[13] = xor_bytes(&s[2], 11);
|
||||
s[20] = xor_bytes(&s[14], 6);
|
||||
ESP_LOGI("w22704", "inject t15");
|
||||
auto call = id(irtx).transmit();
|
||||
auto *d = call.get_data();
|
||||
d->set_carrier_frequency(38000);
|
||||
d->mark(9000); d->space(4494);
|
||||
for (int i = 0; i < 21; i++) {
|
||||
if (i == 6 || i == 14) { d->mark(572); d->space(7960); }
|
||||
d->item_bytes<T>(s + i, 1);
|
||||
}
|
||||
d->mark(572);
|
||||
call.perform();
|
||||
# A 14-byte frame (what some whirlpool remotes send): bytes 0 to 13, the second divider, then silence
|
||||
- action: inject_short
|
||||
variables:
|
||||
hitachi: bool
|
||||
then:
|
||||
- lambda: |-
|
||||
static constexpr remote_base::BitTimings T{.bit_mark = 572, .one_space = 1659, .zero_space = 553};
|
||||
uint8_t s[14] = {0};
|
||||
s[0] = hitachi ? 0xA9 : 0x83; s[1] = hitachi ? 0x59 : 0x06; s[2] = 0x03; s[3] = 0xE2; s[6] = 0x80;
|
||||
if (hitachi) s[10] = 0x20;
|
||||
s[13] = xor_bytes(&s[2], 11);
|
||||
ESP_LOGI("w22704", "inject short hitachi=%d", (int) hitachi);
|
||||
auto call = id(irtx).transmit();
|
||||
auto *d = call.get_data();
|
||||
d->set_carrier_frequency(38000);
|
||||
d->mark(9000); d->space(4494);
|
||||
for (int i = 0; i < 14; i++) {
|
||||
if (i == 6) { d->mark(572); d->space(7960); }
|
||||
d->item_bytes<T>(s + i, 1);
|
||||
}
|
||||
d->mark(572);
|
||||
call.perform();
|
||||
@@ -0,0 +1,29 @@
|
||||
#!/bin/bash
|
||||
# all.sh PORT BASE_TREE PR_TREE: per whirlpool model (DG11J1-3A, HITACHI_HCRA31NEWH, HISENSE_ARG), build ../base.yaml
|
||||
# plus cl.yaml plus the D1 mini one-pin loopback from the base tree, then the PR tree, flash, run the whirlpool steps and split the frames. Base
|
||||
# runs first per model, so the board ends on a PR build. Compare the hex lines with `grep -a "Sending:\|Received:"`.
|
||||
# env: PY, BUILD_LOCK (optional)
|
||||
W=$(cd "$(dirname "$0")" && pwd); L=$(dirname "$W"); S=$(dirname "$L"); P=${1:?port}
|
||||
export PIO_CORE=${PIO_CORE:-$W/pio}
|
||||
FAILED=()
|
||||
for mm in dg:DG11J1-3A hi:HITACHI_HCRA31NEWH hs:HISENSE_ARG; do
|
||||
m=${mm%%:*}
|
||||
for v in base pr; do
|
||||
if [ $v = base ]; then T=${2:?base tree}; else T=${3:?pr tree}; fi
|
||||
N=w$m$v
|
||||
printf 'substitutions:\n name: %s\n model: %s\npackages:\n common: !include ../base.yaml\n model: !include cl.yaml\n board: !include ../esp8266-ir.yaml\n' \
|
||||
"$N" "${mm#*:}" > "$W/$N.yaml"
|
||||
echo "== $N"
|
||||
# a skipped variant must not leave an older run's outputs behind for the comparison
|
||||
rm -f "$L/$N-ser.txt" "$L/$N-pre.txt" "$L/$N-drive.txt" "$L/$N-frames.json"
|
||||
TREE=$T bash "$S/pio_build.sh" compile "$N" "$W/$N.yaml" || { FAILED+=("$N:compile"); continue; }
|
||||
TREE=$T bash "$S/pio_build.sh" upload "$N" "$W/$N.yaml" "$P" || { FAILED+=("$N:upload"); continue; }
|
||||
RPY=${PY:-$T/venv/bin/python}
|
||||
(cd "$L" && STEP_S=4 PY=$RPY bash run.sh "$N" "$P" "$W/steps.sh" && $RPY frames.py "$N") || FAILED+=("$N:run")
|
||||
done
|
||||
done
|
||||
if [ ${#FAILED[@]} -gt 0 ]; then
|
||||
echo "DONE with ${#FAILED[@]} failed variant(s): ${FAILED[*]}"
|
||||
exit 1
|
||||
fi
|
||||
echo DONE
|
||||
@@ -0,0 +1,3 @@
|
||||
climate:
|
||||
- {platform: whirlpool, id: w_tx, name: W TX, model: "${model}", transmitter_id: irtx}
|
||||
- {platform: whirlpool, id: w_rx, name: W RX, model: "${model}", transmitter_id: irtx, receiver_id: irrx}
|
||||
@@ -0,0 +1,6 @@
|
||||
PRE=("W TX:mode=off,t=24,fan=auto,swing=off")
|
||||
STEPS=()
|
||||
for m in heat_cool heat cool dry fan_only off; do STEPS+=("W TX:mode=${m}"); done
|
||||
STEPS+=("W TX:mode=cool")
|
||||
for f in low medium high auto; do STEPS+=("W TX:fan=${f}"); done
|
||||
STEPS+=("W TX:mode=off")
|
||||
@@ -99,6 +99,9 @@ class Client(BaseClient):
|
||||
for line in out["STATE"]:
|
||||
name, _, value = line.partition(" = ")
|
||||
self.last[name] = value
|
||||
# SUB lines are printed sorted below; SUBORDER keeps the order the device sent them
|
||||
for i, line in enumerate(out["SUB"]):
|
||||
print(f" {label} SUBORDER {i:03d} {line}")
|
||||
for kind in ("SUB", "STATE", "ACTION"):
|
||||
for line in sorted(out[kind]) if kind == "SUB" else out[kind]:
|
||||
print(f" {label} {kind} {line}")
|
||||
|
||||
@@ -0,0 +1,22 @@
|
||||
"""lognorm.py [FILE...]: print ESPHome log lines without ANSI colours, the capture's `[hh:mm:ss]` or
|
||||
`[hh:mm:ss.mmm]` timestamp and the `:NNN` source line number of the tag, so two builds' logs can be diffed.
|
||||
|
||||
Reads stdin when no file is given. Scripts can import normalize() instead.
|
||||
"""
|
||||
|
||||
import fileinput
|
||||
import re
|
||||
|
||||
ANSI = re.compile(r"\x1b\[[0-9;]*[A-Za-z]")
|
||||
STAMP = re.compile(r"^\[\d\d:\d\d:\d\d(?:\.\d+)?\]")
|
||||
LINENO = re.compile(r"(\[[A-Z]\]\[[\w.\-]+):\d+\]")
|
||||
|
||||
|
||||
def normalize(line: str) -> str:
|
||||
return LINENO.sub(r"\1]", STAMP.sub("", ANSI.sub("", line)))
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
with fileinput.input(encoding="utf-8", errors="replace") as fh:
|
||||
for raw in fh:
|
||||
print(normalize(raw.rstrip("\n")))
|
||||
@@ -0,0 +1,33 @@
|
||||
esphome:
|
||||
name: ${name}
|
||||
esp32:
|
||||
board: esp32-c3-devkitm-1
|
||||
framework:
|
||||
type: esp-idf
|
||||
logger:
|
||||
level: DEBUG
|
||||
hardware_uart: USB_SERIAL_JTAG
|
||||
wifi:
|
||||
ssid: !secret wifi_ssid
|
||||
password: !secret wifi_password
|
||||
api:
|
||||
reboot_timeout: 0s
|
||||
encryption:
|
||||
key: !secret api_key
|
||||
ota:
|
||||
- platform: esphome
|
||||
spi:
|
||||
clk_pin: GPIO6
|
||||
mosi_pin: GPIO7
|
||||
miso_pin: GPIO10
|
||||
cc1101:
|
||||
id: radio
|
||||
cs_pin: GPIO3
|
||||
frequency: 433.92MHz
|
||||
remote_receiver:
|
||||
pin: GPIO4
|
||||
dump: all
|
||||
interval:
|
||||
- interval: 30s
|
||||
then:
|
||||
- lambda: ESP_LOGI("alive", "up %u heap %u", (unsigned) (millis() / 1000), (unsigned) esp_get_free_heap_size());
|
||||
@@ -0,0 +1,22 @@
|
||||
#!/bin/bash
|
||||
# cmp.sh BASE_DEBUG PR_DEBUG BASE_VERBOSE PR_VERBOSE: compare host harness outputs from the first INPUT line on,
|
||||
# with colours, timestamps and line numbers removed (../../lognorm.py). Writes db/dp/vb/vp.txt to the current directory.
|
||||
PY=${PY:-python3}; LN=$(cd "$(dirname "$0")/../.." && pwd)/lognorm.py
|
||||
TMP=$(mktemp -d); trap 'rm -rf "$TMP"' EXIT
|
||||
[ $# -eq 4 ] || { echo "usage: cmp.sh BASE_DEBUG PR_DEBUG BASE_VERBOSE PR_VERBOSE"; exit 1; }
|
||||
i=0
|
||||
for f in "$1" "$2" "$3" "$4"; do
|
||||
i=$((i + 1))
|
||||
[ -s "$f" ] || { echo "missing or empty: $f"; exit 1; }
|
||||
"$PY" "$LN" "$f" | awk '/^INPUT /{f=1} f' > "$TMP/$i"
|
||||
# no INPUT line: the harness did not run, and two such files would compare as identical
|
||||
[ -s "$TMP/$i" ] || { echo "no INPUT lines in $f"; exit 1; }
|
||||
done
|
||||
NSYM='s/^\[[DV]\]\[remote.beo4\]: Beo4: n_sym/[*][remote.beo4]: Beo4: n_sym/'
|
||||
sed -E "$NSYM" "$TMP/3" > vb.txt
|
||||
sed -E "$NSYM" "$TMP/4" > vp.txt
|
||||
echo "VERBOSE diff (n_sym level mapped): $(diff vb.txt vp.txt | grep -c '^[<>]') of $(wc -l < vp.txt)"
|
||||
grep -v 'Beo4: n_sym' "$TMP/1" > db.txt; cp "$TMP/2" dp.txt
|
||||
echo "DEBUG diff (base n_sym dropped): $(diff db.txt dp.txt | grep -c '^[<>]') of $(wc -l < dp.txt)"
|
||||
echo "n_sym: DEBUG base $(grep -c '\[D\]\[remote.beo4\]: Beo4: n_sym' "$TMP/1") pr $(grep -c 'Beo4: n_sym' dp.txt);" \
|
||||
"VERBOSE base D $(grep -c '\[D\]\[remote.beo4\]: Beo4: n_sym' "$TMP/3") pr V $(grep -c '\[V\]\[remote.beo4\]: Beo4: n_sym' "$TMP/4")"
|
||||
@@ -0,0 +1,44 @@
|
||||
"""gen_dumpers.py TREE [OUT]: write OUT (default dumpers.h here) for rbh.hpp, one dumper per DECLARE_REMOTE_PROTOCOL(...) in TREE's
|
||||
remote_base plus Pronto, RCSwitch and ABBWelcome (Raw is called by rbh.hpp only when nothing matched)."""
|
||||
|
||||
import pathlib
|
||||
import re
|
||||
import sys
|
||||
|
||||
rb = pathlib.Path(sys.argv[1]) / "esphome" / "components" / "remote_base"
|
||||
headers, names = [], []
|
||||
for h in sorted(rb.glob("*_protocol.h")):
|
||||
found = re.findall(
|
||||
r"^DECLARE_REMOTE_PROTOCOL\((\w+)\)",
|
||||
h.read_text(encoding="utf-8"),
|
||||
re.MULTILINE,
|
||||
)
|
||||
if found or h.name in (
|
||||
"pronto_protocol.h",
|
||||
"rc_switch_protocol.h",
|
||||
"abbwelcome_protocol.h",
|
||||
"raw_protocol.h",
|
||||
):
|
||||
headers.append(h.name)
|
||||
names += found
|
||||
names += [n for n in ("Pronto", "RCSwitch", "ABBWelcome") if n not in names]
|
||||
out = ["#pragma once"]
|
||||
out += [f'#include "esphome/components/remote_base/{h}"' for h in headers]
|
||||
out += [
|
||||
"using namespace esphome::remote_base;",
|
||||
"struct RbDumper {",
|
||||
" const char *name;",
|
||||
" RemoteReceiverDumperBase *d;",
|
||||
"};",
|
||||
]
|
||||
out += [f"static {n}Dumper rbd_{n.lower()};" for n in names]
|
||||
out += ["static RbDumper RB_DUMPERS[] = {"]
|
||||
out += [f' {{"{n}", &rbd_{n.lower()}}},' for n in names]
|
||||
out += ["};", ""]
|
||||
out_path = (
|
||||
pathlib.Path(sys.argv[2])
|
||||
if len(sys.argv) > 2
|
||||
else pathlib.Path(__file__).with_name("dumpers.h")
|
||||
)
|
||||
out_path.write_text("\n".join(out), encoding="utf-8")
|
||||
print(len(names), "dumpers")
|
||||
@@ -0,0 +1,99 @@
|
||||
esphome:
|
||||
name: ${name}
|
||||
on_boot:
|
||||
priority: -100
|
||||
then:
|
||||
- logger.set_level: DEBUG
|
||||
logger:
|
||||
level: VERBOSE
|
||||
wifi:
|
||||
ssid: !secret wifi_ssid
|
||||
password: !secret wifi_password
|
||||
power_save_mode: none
|
||||
api:
|
||||
reboot_timeout: 0s
|
||||
encryption:
|
||||
key: !secret api_key
|
||||
ota:
|
||||
- platform: esphome
|
||||
remote_transmitter:
|
||||
id: irtx
|
||||
pin:
|
||||
number: ${pin}
|
||||
allow_other_uses: true
|
||||
carrier_duty_percent: 100%
|
||||
remote_receiver:
|
||||
pin:
|
||||
number: ${pin}
|
||||
allow_other_uses: true
|
||||
dump: all
|
||||
idle: 25ms
|
||||
button:
|
||||
- platform: template
|
||||
name: TX all
|
||||
on_press:
|
||||
- script.execute: tx_all
|
||||
- platform: template
|
||||
name: Verbose
|
||||
on_press:
|
||||
- logger.set_level: VERBOSE
|
||||
- platform: template
|
||||
name: Debug
|
||||
on_press:
|
||||
- logger.set_level: DEBUG
|
||||
script:
|
||||
- id: tx_all
|
||||
mode: single
|
||||
then:
|
||||
- logger.log: "TXMARK aok 1"
|
||||
- lambda: ${prep}
|
||||
- remote_transmitter.transmit_aok:
|
||||
remote_id: 0x123456
|
||||
channels: 0x00A1
|
||||
command: 0x0B
|
||||
- delay: 500ms
|
||||
- logger.log: "TXMARK aok 2"
|
||||
- lambda: ${prep}
|
||||
- remote_transmitter.transmit_aok:
|
||||
remote_id: 0xA5C381
|
||||
channels: 0x1000
|
||||
command: 0x43
|
||||
- delay: 500ms
|
||||
- logger.log: "TXMARK cardin 1"
|
||||
- lambda: ${prep}
|
||||
- remote_transmitter.transmit_cardin_s438:
|
||||
code: "01001001110100"
|
||||
channel: B
|
||||
- delay: 700ms
|
||||
- logger.log: "TXMARK cardin 2"
|
||||
- lambda: ${prep}
|
||||
- remote_transmitter.transmit_cardin_s438:
|
||||
code: "11100011000101"
|
||||
channel: A
|
||||
- delay: 700ms
|
||||
- logger.log: "TXMARK drayton 1"
|
||||
- lambda: ${prep}
|
||||
- remote_transmitter.transmit_drayton:
|
||||
address: 0x1230
|
||||
data: 0x8
|
||||
- delay: 500ms
|
||||
- logger.log: "TXMARK drayton 2"
|
||||
- lambda: ${prep}
|
||||
- remote_transmitter.transmit_drayton:
|
||||
address: 0x1234
|
||||
channel: 0x01
|
||||
command: 0x05
|
||||
- delay: 500ms
|
||||
- logger.log: "TXMARK beo4 1"
|
||||
- lambda: ${prep}
|
||||
- remote_transmitter.transmit_beo4:
|
||||
source: 0x01
|
||||
command: 0x0C
|
||||
- delay: 500ms
|
||||
- logger.log: "TXMARK beo4 2"
|
||||
- lambda: ${prep}
|
||||
- remote_transmitter.transmit_beo4:
|
||||
source: 0x00
|
||||
command: 0x60
|
||||
- delay: 500ms
|
||||
- logger.log: "TXMARK end"
|
||||
@@ -0,0 +1,240 @@
|
||||
#pragma once
|
||||
#include <cerrno>
|
||||
#include <cstdio>
|
||||
#include <cstdlib>
|
||||
#include <random>
|
||||
#include <string>
|
||||
#include <vector>
|
||||
#include "dumpers.h"
|
||||
|
||||
static std::mt19937 rbh_rng(22814);
|
||||
static uint32_t rbh_r() { return rbh_rng(); }
|
||||
static RawDumper rbh_raw;
|
||||
|
||||
static void rbh_dump_all(const RawTimings &raw, uint32_t tol, ToleranceMode mode, const char *what, int i) {
|
||||
printf("INPUT %d %s tol=%u mode=%d n=%u\n", i, what, (unsigned) tol, (int) mode, (unsigned) raw.size());
|
||||
bool any = false;
|
||||
for (auto &nd : RB_DUMPERS) {
|
||||
RemoteReceiveData src(raw, tol, mode);
|
||||
bool r = nd.d->dump(src);
|
||||
if (r) {
|
||||
printf("HIT %s\n", nd.name);
|
||||
any = true;
|
||||
}
|
||||
}
|
||||
if (!any) {
|
||||
RemoteReceiveData src(raw, tol, mode);
|
||||
rbh_raw.dump(src);
|
||||
}
|
||||
RemoteReceiveData s1(raw, tol, mode);
|
||||
auto a = AOKProtocol().decode(s1);
|
||||
if (a)
|
||||
printf("AOK id=%06X ch=%04X cmd=%02X\n", (unsigned) a->remote_id, a->channels, a->command);
|
||||
RemoteReceiveData s2(raw, tol, mode);
|
||||
auto c = CardinS438Protocol().decode(s2);
|
||||
if (c)
|
||||
printf("CARDIN code=%04X ch=%u\n", c->code, c->channel);
|
||||
RemoteReceiveData s3(raw, tol, mode);
|
||||
auto d = DraytonProtocol().decode(s3);
|
||||
if (d)
|
||||
printf("DRAYTON addr=%04X ch=%u cmd=%u data=%u\n", d->address, d->channel, d->command, d->data);
|
||||
RemoteReceiveData s4(raw, tol, mode);
|
||||
auto b = Beo4Protocol().decode(s4);
|
||||
if (b)
|
||||
printf("BEO4 src=%u cmd=%u rep=%u\n", b->source, b->command, b->repeats);
|
||||
}
|
||||
|
||||
struct Mode {
|
||||
uint32_t tol;
|
||||
ToleranceMode mode;
|
||||
};
|
||||
static const Mode RBH_MODES[] = {{25, TOLERANCE_MODE_PERCENTAGE},
|
||||
{10, TOLERANCE_MODE_PERCENTAGE},
|
||||
{150, TOLERANCE_MODE_TIME},
|
||||
{60, TOLERANCE_MODE_TIME}};
|
||||
|
||||
// One mark or space nudged to just inside or just outside the tolerance of its own length
|
||||
static int32_t rbh_edge(int32_t v, const Mode &m) {
|
||||
int32_t n = v < 0 ? -v : v;
|
||||
int32_t lo, hi;
|
||||
if (m.mode == TOLERANCE_MODE_TIME) {
|
||||
lo = n - (int32_t) m.tol;
|
||||
hi = n + (int32_t) m.tol;
|
||||
} else {
|
||||
lo = int32_t(100 - m.tol) * n / 100;
|
||||
hi = int32_t(100 + m.tol) * n / 100;
|
||||
}
|
||||
int32_t pick[] = {lo - 1, lo, hi, hi + 1};
|
||||
int32_t e = pick[rbh_r() % 4];
|
||||
if (e < 1)
|
||||
e = 1;
|
||||
return v < 0 ? -e : e;
|
||||
}
|
||||
|
||||
static RawTimings rbh_frame(int proto) {
|
||||
RemoteTransmitData t;
|
||||
switch (proto) {
|
||||
case 0:
|
||||
AOKProtocol().encode(&t, AOKData{rbh_r() & 0xFFFFFF, (uint16_t) rbh_r(), (uint8_t) rbh_r()});
|
||||
break;
|
||||
case 1: {
|
||||
CardinS438Data c;
|
||||
c.code = rbh_r();
|
||||
c.channel = rbh_r() % 4;
|
||||
c.seed = rbh_r();
|
||||
c.repeats = 1 + rbh_r() % 3;
|
||||
CardinS438Protocol().encode(&t, c);
|
||||
break;
|
||||
}
|
||||
case 2:
|
||||
DraytonProtocol().encode(&t, DraytonData{(uint16_t) rbh_r(), 0, 0, (uint8_t) (rbh_r() & 0x7F)});
|
||||
break;
|
||||
default:
|
||||
Beo4Protocol().encode(&t, Beo4Data{(uint8_t) rbh_r(), (uint8_t) rbh_r(), 0});
|
||||
break;
|
||||
}
|
||||
// A receiver sees back to back marks (or spaces) as one longer timing
|
||||
RawTimings merged;
|
||||
for (int32_t v : t.get_data()) {
|
||||
if (!merged.empty() && (merged.back() > 0) == (v > 0))
|
||||
merged.back() += v;
|
||||
else
|
||||
merged.push_back(v);
|
||||
}
|
||||
return merged;
|
||||
}
|
||||
|
||||
static void rbh_noise(RawTimings &raw, int n) {
|
||||
bool mark = raw.empty() || raw.back() < 0;
|
||||
for (int i = 0; i < n; i++, mark = !mark) {
|
||||
int32_t v = 50 + rbh_r() % 12000;
|
||||
raw.push_back(mark ? v : -v);
|
||||
}
|
||||
}
|
||||
|
||||
static const char *RBH_PROTO[] = {"aok", "cardin", "drayton", "beo4"};
|
||||
|
||||
struct RbhInput {
|
||||
std::string what;
|
||||
int mode;
|
||||
RawTimings raw;
|
||||
};
|
||||
static void rbh_generate(std::vector<RbhInput> &out) {
|
||||
for (int rep = 0; rep < 250; rep++) {
|
||||
for (int proto = 0; proto < 4; proto++) {
|
||||
RawTimings f = rbh_frame(proto);
|
||||
for (int variant = 0; variant < 9; variant++) {
|
||||
RawTimings raw;
|
||||
if (variant == 1 || variant == 4)
|
||||
rbh_noise(raw, 1 + rbh_r() % 30); // leading noise
|
||||
if (!raw.empty() && raw.back() > 0 && f[0] > 0)
|
||||
raw.push_back(-(int32_t) (3000 + rbh_r() % 9000));
|
||||
int copies = (variant == 2 || variant == 4) ? 2 + rbh_r() % 2 : 1; // repeats
|
||||
for (int c = 0; c < copies; c++) {
|
||||
if (c > 0 && raw.back() > 0)
|
||||
raw.push_back(-(int32_t) (5000 + rbh_r() % 8000));
|
||||
raw.insert(raw.end(), f.begin(), f.end());
|
||||
}
|
||||
for (int mi = 0; mi < 4; mi++) {
|
||||
const auto &m = RBH_MODES[mi];
|
||||
RawTimings x = raw;
|
||||
if (variant == 3) { // nudge about a quarter of the timings to the tolerance edges
|
||||
for (auto &v : x)
|
||||
if (rbh_r() % 4 == 0)
|
||||
v = rbh_edge(v, m);
|
||||
} else if (variant >= 5 && proto < 3) { // only the sync mark, at one tolerance edge, the rest exact
|
||||
const int32_t nominal[] = {5000, 6400, 1000};
|
||||
int32_t n = nominal[proto], lo, hi;
|
||||
if (m.mode == TOLERANCE_MODE_TIME) {
|
||||
lo = n - (int32_t) m.tol;
|
||||
hi = n + (int32_t) m.tol;
|
||||
} else {
|
||||
lo = int32_t(100 - m.tol) * n / 100;
|
||||
hi = int32_t(100 + m.tol) * n / 100;
|
||||
}
|
||||
const int32_t edge[] = {lo - 1, lo, hi, hi + 1};
|
||||
for (auto &v : x) {
|
||||
if (v == n) {
|
||||
v = edge[variant - 5];
|
||||
if (proto == 2)
|
||||
break; // Drayton: the first 1000 us mark is the sync
|
||||
}
|
||||
}
|
||||
} else if (variant == 4) { // jitter every timing by up to 30%
|
||||
for (auto &v : x) {
|
||||
int32_t n = v < 0 ? -v : v;
|
||||
n = n + (int32_t) (n * ((int) (rbh_r() % 61) - 30) / 100);
|
||||
if (n < 1)
|
||||
n = 1;
|
||||
v = v < 0 ? -n : n;
|
||||
}
|
||||
}
|
||||
x.push_back(-20000);
|
||||
char what[32];
|
||||
snprintf(what, sizeof(what), "%s/v%d", RBH_PROTO[proto], variant);
|
||||
out.push_back({what, mi, x});
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
for (int r = 0; r < 4000; r++) { // random timing corpus
|
||||
RawTimings raw;
|
||||
rbh_noise(raw, 4 + rbh_r() % 250);
|
||||
raw.push_back(-20000);
|
||||
out.push_back({"random", r % 4, raw});
|
||||
}
|
||||
}
|
||||
|
||||
static void rbh_fail(const char *what, const char *path) {
|
||||
fprintf(stderr, "RBH_CORPUS %s: %s\n", what, path);
|
||||
exit(1);
|
||||
}
|
||||
|
||||
// RBH_CORPUS=<file>: replay the file when it exists, else generate a corpus and write it there. Any read, parse or
|
||||
// write error exits 1, so two builds never compare different inputs.
|
||||
static void rbh_run() {
|
||||
std::vector<RbhInput> in;
|
||||
const char *path = getenv("RBH_CORPUS");
|
||||
errno = 0;
|
||||
FILE *f = path ? fopen(path, "rb") : nullptr;
|
||||
if (path != nullptr && f == nullptr && errno != ENOENT)
|
||||
rbh_fail("cannot open", path);
|
||||
if (f != nullptr) {
|
||||
char what[64];
|
||||
int mode;
|
||||
unsigned n;
|
||||
int got;
|
||||
while ((got = fscanf(f, "%63s %d %u", what, &mode, &n)) == 3) {
|
||||
if (mode < 0 || mode >= (int) (sizeof(RBH_MODES) / sizeof(RBH_MODES[0])))
|
||||
rbh_fail("bad mode in", path);
|
||||
RawTimings raw(n);
|
||||
for (auto &v : raw) {
|
||||
if (fscanf(f, "%d", &v) != 1)
|
||||
rbh_fail("truncated frame in", path);
|
||||
}
|
||||
in.push_back({what, mode, raw});
|
||||
}
|
||||
if (got != EOF || ferror(f) || in.empty())
|
||||
rbh_fail("unreadable or empty", path);
|
||||
fclose(f);
|
||||
} else {
|
||||
rbh_generate(in);
|
||||
if (path != nullptr) {
|
||||
if ((f = fopen(path, "wb")) == nullptr)
|
||||
rbh_fail("cannot create", path);
|
||||
for (auto &x : in) {
|
||||
fprintf(f, "%s %d %u", x.what.c_str(), x.mode, (unsigned) x.raw.size());
|
||||
for (auto v : x.raw)
|
||||
fprintf(f, " %d", v);
|
||||
fprintf(f, "\n");
|
||||
}
|
||||
if (ferror(f) || fclose(f) != 0)
|
||||
rbh_fail("write failed", path);
|
||||
}
|
||||
}
|
||||
int i = 0;
|
||||
for (auto &x : in)
|
||||
rbh_dump_all(x.raw, RBH_MODES[x.mode].tol, RBH_MODES[x.mode].mode, x.what.c_str(), i++);
|
||||
fflush(stdout);
|
||||
exit(0);
|
||||
}
|
||||
@@ -0,0 +1,20 @@
|
||||
# build twice: `-s level DEBUG` and `-s level VERBOSE` (with a different `-s name`), then compare with cmp.sh
|
||||
substitutions:
|
||||
level: DEBUG
|
||||
esphome:
|
||||
name: ${name}
|
||||
includes:
|
||||
- dumpers.h
|
||||
- rbh.hpp
|
||||
on_boot:
|
||||
priority: -100
|
||||
then:
|
||||
- lambda: rbh_run();
|
||||
host:
|
||||
logger:
|
||||
level: ${level}
|
||||
external_components:
|
||||
- source:
|
||||
type: local
|
||||
path: ../ext
|
||||
rbtest:
|
||||
@@ -0,0 +1,13 @@
|
||||
"""Test only: pulls remote_base with every protocol source into a host build (hostharness and dumpall)."""
|
||||
|
||||
from esphome.components import remote_base
|
||||
import esphome.config_validation as cv
|
||||
|
||||
AUTO_LOAD = ["remote_base"]
|
||||
DOMAIN = "rbtest"
|
||||
CONFIG_SCHEMA = cv.Schema({})
|
||||
|
||||
|
||||
async def to_code(config):
|
||||
for stem in remote_base._PROTOCOL_STEMS:
|
||||
remote_base.request_protocol(stem)
|
||||
@@ -0,0 +1,108 @@
|
||||
esphome:
|
||||
name: ${name}
|
||||
includes:
|
||||
- rbh.hpp
|
||||
logger:
|
||||
level: DEBUG
|
||||
wifi:
|
||||
ssid: !secret wifi_ssid
|
||||
password: !secret wifi_password
|
||||
power_save_mode: none
|
||||
api:
|
||||
reboot_timeout: 0s
|
||||
encryption:
|
||||
key: !secret api_key
|
||||
ota:
|
||||
- platform: esphome
|
||||
remote_transmitter:
|
||||
id: irtx
|
||||
pin:
|
||||
number: ${pin}
|
||||
allow_other_uses: true
|
||||
carrier_duty_percent: 100%
|
||||
remote_receiver:
|
||||
pin:
|
||||
number: ${pin}
|
||||
allow_other_uses: true
|
||||
dump: [aeha, haier, mirage]
|
||||
on_aeha:
|
||||
then:
|
||||
- lambda: rbh_on_frame("ON_AEHA", x.data);
|
||||
on_haier:
|
||||
then:
|
||||
- lambda: rbh_on_frame("ON_HAIER", x.data);
|
||||
on_mirage:
|
||||
then:
|
||||
- lambda: rbh_on_frame("ON_MIRAGE", x.data);
|
||||
button:
|
||||
- platform: template
|
||||
name: TX all
|
||||
on_press:
|
||||
- script.execute: tx_all
|
||||
script:
|
||||
- id: tx_all
|
||||
mode: single
|
||||
then:
|
||||
- logger.log: "TXMARK aeha 2"
|
||||
- lambda: ${prep}
|
||||
- remote_transmitter.transmit_aeha:
|
||||
address: 0xE6C2
|
||||
data: [0x79, 0xB7]
|
||||
- delay: 900ms
|
||||
- logger.log: "TXMARK aeha 3"
|
||||
- lambda: ${prep}
|
||||
- remote_transmitter.transmit_aeha:
|
||||
address: 0x7EDA
|
||||
data: [0x61, 0x89, 0x59]
|
||||
- delay: 900ms
|
||||
- logger.log: "TXMARK aeha 8"
|
||||
- lambda: ${prep}
|
||||
- remote_transmitter.transmit_aeha:
|
||||
address: 0x9D2C
|
||||
data: [0xEC, 0xD4, 0x26, 0x32, 0xB9, 0xA1, 0x14, 0xD7]
|
||||
- delay: 900ms
|
||||
- logger.log: "TXMARK aeha 13"
|
||||
- lambda: ${prep}
|
||||
- remote_transmitter.transmit_aeha:
|
||||
address: 0x3E1C
|
||||
data: [0xAE, 0x41, 0x17, 0xB0, 0xDC, 0x2C, 0xF8, 0x86, 0xF4, 0xF3, 0xDC, 0xD0, 0x92]
|
||||
- delay: 900ms
|
||||
- logger.log: "TXMARK aeha 20"
|
||||
- lambda: ${prep}
|
||||
- remote_transmitter.transmit_aeha:
|
||||
address: 0xDD3D
|
||||
data: [0x30, 0x65, 0xC9, 0x64, 0xB7, 0x32, 0xD3, 0xF5, 0x21, 0x6A, 0x62, 0x88, 0xE2, 0x5A, 0x59, 0x35, 0xF4, 0xC1, 0xC2, 0xE4]
|
||||
- delay: 900ms
|
||||
- logger.log: "TXMARK aeha 34"
|
||||
- lambda: ${prep}
|
||||
- remote_transmitter.transmit_aeha:
|
||||
address: 0x9C21
|
||||
data: [0x27, 0x6B, 0x15, 0x4D, 0x81, 0x14, 0x08, 0xCD, 0x36, 0xC1, 0x82, 0x3F, 0x85, 0xFE, 0x9D, 0xDD, 0x5D, 0xF9, 0x11, 0x98, 0x22, 0x88, 0x42, 0xEF, 0x73, 0xFD, 0xD7, 0x3A, 0x96, 0x50, 0xA1, 0x4E, 0xDB, 0x8D]
|
||||
- delay: 900ms
|
||||
- logger.log: "TXMARK aeha 35"
|
||||
- lambda: ${prep}
|
||||
- remote_transmitter.transmit_aeha:
|
||||
address: 0x1050
|
||||
data: [0x8F, 0xCA, 0xE5, 0x0A, 0x97, 0xF9, 0x34, 0xD7, 0x06, 0x64, 0x96, 0x52, 0xBE, 0x49, 0xFB, 0xB7, 0xA8, 0x46, 0xBA, 0x27, 0x8B, 0x9E, 0xA1, 0x8F, 0x6F, 0xAF, 0x97, 0xA2, 0xD1, 0x67, 0x88, 0x72, 0x8F, 0xC1, 0xD0]
|
||||
- delay: 900ms
|
||||
- logger.log: "TXMARK haier 13"
|
||||
- lambda: ${prep}
|
||||
- remote_transmitter.transmit_haier:
|
||||
code: [0x22, 0x48, 0x40, 0x36, 0x31, 0x7A, 0x21, 0x08, 0x57, 0x0C, 0x7B, 0x16, 0x3A]
|
||||
- delay: 600ms
|
||||
- logger.log: "TXMARK haier 8"
|
||||
- lambda: ${prep}
|
||||
- remote_transmitter.transmit_haier:
|
||||
code: [0x0C, 0xA0, 0x1C, 0x67, 0x3D, 0x0D, 0xF7, 0x81]
|
||||
- delay: 600ms
|
||||
- logger.log: "TXMARK mirage 14"
|
||||
- lambda: ${prep}
|
||||
- remote_transmitter.transmit_mirage:
|
||||
code: [0x41, 0x1E, 0xF7, 0xA9, 0x30, 0xCC, 0x0B, 0xA1, 0x47, 0x86, 0xE8, 0xFF, 0x8C, 0xCF]
|
||||
- delay: 600ms
|
||||
- logger.log: "TXMARK mirage 14"
|
||||
- lambda: ${prep}
|
||||
- remote_transmitter.transmit_mirage:
|
||||
code: [0x68, 0x72, 0x38, 0xAB, 0xA8, 0xF9, 0x4F, 0x86, 0x74, 0xA1, 0x39, 0x0A, 0x49, 0xC8]
|
||||
- delay: 600ms
|
||||
- logger.log: "TXMARK end"
|
||||
@@ -0,0 +1,97 @@
|
||||
#pragma once
|
||||
#include <cstdio>
|
||||
#include <cstdlib>
|
||||
#include <random>
|
||||
#include <type_traits>
|
||||
#include <vector>
|
||||
#include "esphome/components/remote_base/aeha_protocol.h"
|
||||
#include "esphome/components/remote_base/haier_protocol.h"
|
||||
#include "esphome/components/remote_base/mirage_protocol.h"
|
||||
#include "esphome/core/alloc_helpers.h"
|
||||
#include "esphome/core/log.h"
|
||||
using namespace esphome::remote_base;
|
||||
|
||||
inline std::mt19937 rbh_rng(22773);
|
||||
inline uint32_t rbh_r() { return rbh_rng(); }
|
||||
|
||||
template<typename D> void rbh_print(const char *proto, int i, const char *kind, const esphome::optional<D> &d) {
|
||||
printf("CASE %s %d %s has=%d", proto, i, kind, d.has_value() ? 1 : 0);
|
||||
if (d.has_value()) {
|
||||
printf(" n=%u data=", (unsigned) d->data.size());
|
||||
for (uint8_t b : d->data)
|
||||
printf("%02X", b);
|
||||
}
|
||||
printf("\n");
|
||||
}
|
||||
|
||||
// kind 0 clean, 1 one timing corrupted, 2 truncated
|
||||
inline RawTimings rbh_mutate(RawTimings raw, int kind) {
|
||||
raw.push_back(-20000); // the idle gap a receiver records after the last mark
|
||||
if (kind == 1 && !raw.empty()) {
|
||||
size_t k = rbh_r() % raw.size();
|
||||
int32_t v = 100 + rbh_r() % 6000;
|
||||
raw[k] = raw[k] < 0 ? -v : v;
|
||||
} else if (kind == 2 && raw.size() > 1) {
|
||||
raw.resize(rbh_r() % raw.size());
|
||||
}
|
||||
return raw;
|
||||
}
|
||||
inline const char *const RBH_KIND[] = {"clean", "corrupt", "trunc"};
|
||||
|
||||
// Encode one frame with enc, mutate it by kind, decode it, print the case and dump what decoded.
|
||||
template<typename P, typename Enc>
|
||||
void rbh_case(P &proto, const char *name, const char *prefix, int i, int kind, Enc enc) {
|
||||
RemoteTransmitData t;
|
||||
enc(&t);
|
||||
RawTimings raw = rbh_mutate(t.get_data(), kind);
|
||||
auto d = proto.decode(RemoteReceiveData(raw, 25, TOLERANCE_MODE_PERCENTAGE));
|
||||
printf("%s", prefix);
|
||||
rbh_print(name, i, RBH_KIND[kind], d);
|
||||
if (!d.has_value())
|
||||
return;
|
||||
if constexpr (std::is_same_v<P, AEHAProtocol>)
|
||||
printf("ADDR %04X\n", d->address);
|
||||
proto.dump(*d);
|
||||
}
|
||||
|
||||
inline void rbh_run() {
|
||||
AEHAProtocol aeha;
|
||||
HaierProtocol haier;
|
||||
MirageProtocol mirage;
|
||||
char prefix[48];
|
||||
int i = 0;
|
||||
for (int rep = 0; rep < 400; rep++) {
|
||||
for (int kind = 0; kind < 3; kind++) {
|
||||
uint8_t buf[48];
|
||||
// AEHA: 1 to 40 bytes (35 is the cap)
|
||||
size_t len = 1 + rbh_r() % 40;
|
||||
for (auto &b : buf)
|
||||
b = rbh_r();
|
||||
uint16_t addr = rbh_r();
|
||||
snprintf(prefix, sizeof(prefix), "AEHA len=%u addr=%04X ", (unsigned) len, addr);
|
||||
rbh_case(aeha, "aeha", prefix, i, kind, [&](RemoteTransmitData *t) { aeha.encode(t, addr, buf, len); });
|
||||
// Haier: 8 and 13 mostly, sometimes 1 to 16
|
||||
size_t hl = (rep % 4 == 3) ? 1 + rbh_r() % 16 : ((rbh_r() & 1) ? 8 : 13);
|
||||
snprintf(prefix, sizeof(prefix), "HAIER len=%u ", (unsigned) hl);
|
||||
rbh_case(haier, "haier", prefix, i, kind, [&](RemoteTransmitData *t) { haier.encode(t, buf, hl); });
|
||||
// Mirage: 14 mostly, sometimes 10 to 18
|
||||
size_t ml = (rep % 4 == 3) ? 10 + rbh_r() % 9 : 14;
|
||||
snprintf(prefix, sizeof(prefix), "MIRAGE len=%u ", (unsigned) ml);
|
||||
rbh_case(mirage, "mirage", prefix, i, kind, [&](RemoteTransmitData *t) { mirage.encode(t, buf, ml); });
|
||||
i++;
|
||||
}
|
||||
}
|
||||
fflush(stdout);
|
||||
exit(0);
|
||||
}
|
||||
|
||||
// Loopback trigger log (ir-common.yaml): also checks that the vector idioms compile against the trigger's data type.
|
||||
template<typename T> void rbh_on_frame(const char *tag, const T &data) {
|
||||
std::vector<uint8_t> v = data;
|
||||
uint32_t s = 0;
|
||||
for (auto b : data)
|
||||
s += b;
|
||||
bool eq = (v == data) && (data == v) && !(data != v);
|
||||
ESP_LOGI("loop", "%s n=%u hex=%s sum=%u eq=%d front=%u", tag, (unsigned) data.size(),
|
||||
esphome::format_hex(data).c_str(), (unsigned) s, eq ? 1 : 0, (unsigned) data.front());
|
||||
}
|
||||
@@ -0,0 +1,16 @@
|
||||
esphome:
|
||||
name: ${name}
|
||||
includes:
|
||||
- rbh.hpp
|
||||
on_boot:
|
||||
priority: -100
|
||||
then:
|
||||
- lambda: rbh_run();
|
||||
host:
|
||||
logger:
|
||||
level: DEBUG
|
||||
external_components:
|
||||
- source:
|
||||
type: local
|
||||
path: ../ext
|
||||
rbtest:
|
||||
@@ -0,0 +1,29 @@
|
||||
"""press.py HOST BUTTON[:SECS]...: press each button (by object id) in order, waiting SECS (default 1) after each.
|
||||
|
||||
hostharness: `press.py HOST tx_all:14 tx_all:14 ...`; dumpall: `press.py HOST tx_all:9 tx_all:9 verbose tx_all:9 debug`;
|
||||
pronto/run.sh: every tx button ROUNDS times, then `soak:90`.
|
||||
API_KEY in the env selects Noise (see apiconn.py).
|
||||
"""
|
||||
|
||||
import asyncio
|
||||
import pathlib
|
||||
import sys
|
||||
|
||||
sys.path.insert(0, str(pathlib.Path(__file__).resolve().parents[1]))
|
||||
from aioesphomeapi import ButtonInfo # noqa: E402
|
||||
from apiconn import connect # noqa: E402
|
||||
|
||||
|
||||
async def main() -> None:
|
||||
c = await connect(sys.argv[1], tries=60)
|
||||
ents, _ = await c.list_entities_services()
|
||||
keys = {e.object_id: e.key for e in ents if isinstance(e, ButtonInfo)}
|
||||
for step in sys.argv[2:]:
|
||||
name, _, secs = step.partition(":")
|
||||
c.button_command(keys[name])
|
||||
await asyncio.sleep(float(secs or 1))
|
||||
await c.disconnect()
|
||||
print("pressed", len(sys.argv) - 2)
|
||||
|
||||
|
||||
asyncio.run(main())
|
||||
@@ -0,0 +1,36 @@
|
||||
"""consist.py EXT: per frame, the trigger text (decode) must equal the dump chunks joined."""
|
||||
|
||||
import sys
|
||||
|
||||
from extract import read_lines, trigger_blocks
|
||||
|
||||
DUMP_START = "remote.pronto: Received Pronto: data="
|
||||
DUMP = "remote.pronto: "
|
||||
|
||||
lines = read_lines(sys.argv[1])
|
||||
ok = bad = 0
|
||||
for n, t, i in trigger_blocks(lines):
|
||||
# dump: all prints NEC lines and the pronto binary sensor can log between the trigger and the Pronto dump
|
||||
while i < len(lines) and (
|
||||
lines[i].startswith("remote.nec") or "BS pronto" in lines[i]
|
||||
):
|
||||
i += 1
|
||||
if i < len(lines) and lines[i] == DUMP_START:
|
||||
i += 1
|
||||
d = ""
|
||||
while (
|
||||
i < len(lines) and lines[i].startswith(DUMP) and "Received" not in lines[i]
|
||||
):
|
||||
d += lines[i][len(DUMP) :]
|
||||
i += 1
|
||||
if d == t and len(t) == n:
|
||||
ok += 1
|
||||
else:
|
||||
bad += 1
|
||||
print("MISMATCH", n, len(t), len(d))
|
||||
else:
|
||||
bad += 1
|
||||
print("MISSING dump after a trigger of", n)
|
||||
print(f"{ok + bad} trigger frames, {ok} consistent, {bad} not")
|
||||
if bad or not ok:
|
||||
sys.exit(1)
|
||||
@@ -0,0 +1,16 @@
|
||||
# D1 mini, one-pin loopback on GPIO12 with every dumper; the board and network come from climate_ir_loop.
|
||||
packages:
|
||||
base: !include ../../climate_ir_loop/base.yaml
|
||||
board: !include ../../climate_ir_loop/esp8266-ir.yaml
|
||||
common: !include gen-common.yaml
|
||||
logger:
|
||||
baud_rate: 460800
|
||||
level: DEBUG
|
||||
remote_receiver:
|
||||
dump: all
|
||||
buffer_size: 3kb
|
||||
on_pronto:
|
||||
- lambda: |-
|
||||
ESP_LOGI("trig", "TRIG len=%u", (unsigned) x.data.size());
|
||||
for (size_t i = 0; i < x.data.size(); i += 200)
|
||||
ESP_LOGI("trig", "TRIG %s", x.data.substr(i, 200).c_str());
|
||||
@@ -0,0 +1,19 @@
|
||||
# ESP32 devkit, one-pin loopback on GPIO27 with every dumper; the board and network come from climate_ir_loop.
|
||||
packages:
|
||||
base: !include ../../climate_ir_loop/base.yaml
|
||||
board: !include
|
||||
file: ../../climate_ir_loop/esp32-ir.yaml
|
||||
vars:
|
||||
rmt_symbols: "256"
|
||||
receive_symbols: "256"
|
||||
common: !include gen-common.yaml
|
||||
logger:
|
||||
baud_rate: 921600
|
||||
level: DEBUG
|
||||
remote_receiver:
|
||||
dump: all
|
||||
on_pronto:
|
||||
- lambda: |-
|
||||
ESP_LOGI("trig", "TRIG len=%u", (unsigned) x.data.size());
|
||||
for (size_t i = 0; i < x.data.size(); i += 200)
|
||||
ESP_LOGI("trig", "TRIG %s", x.data.substr(i, 200).c_str());
|
||||
@@ -0,0 +1,64 @@
|
||||
"""extract.py SERIAL OUT: keep MARK, pronto dump, trigger, binary sensor and heap lines from the first MARK on,
|
||||
with timestamps and source line numbers removed (lognorm.py). Also holds the trigger block parser that consist.py
|
||||
and jit.py share."""
|
||||
|
||||
import pathlib
|
||||
import re
|
||||
import sys
|
||||
|
||||
sys.path.insert(0, str(pathlib.Path(__file__).resolve().parents[2]))
|
||||
from lognorm import normalize # noqa: E402
|
||||
|
||||
TRIG_LEN = "trig: TRIG len="
|
||||
TRIG = "trig: TRIG "
|
||||
|
||||
|
||||
def trigger_blocks(lines):
|
||||
"""Yield (n, text, i) per on_pronto trigger: the length it logged, its chunks joined, and the index of the first
|
||||
line after the block."""
|
||||
i = 0
|
||||
while i < len(lines):
|
||||
if not lines[i].startswith(TRIG_LEN):
|
||||
i += 1
|
||||
continue
|
||||
n = int(lines[i][len(TRIG_LEN) :])
|
||||
i += 1
|
||||
text = ""
|
||||
while (
|
||||
i < len(lines)
|
||||
and lines[i].startswith(TRIG)
|
||||
and not lines[i].startswith(TRIG_LEN)
|
||||
):
|
||||
text += lines[i][len(TRIG) :]
|
||||
i += 1
|
||||
yield n, text, i
|
||||
|
||||
|
||||
def read_lines(path):
|
||||
return pathlib.Path(path).read_text(encoding="utf-8").splitlines()
|
||||
|
||||
|
||||
def main() -> None:
|
||||
out, on = [], False
|
||||
with pathlib.Path(sys.argv[1]).open(encoding="utf-8", errors="replace") as fh:
|
||||
for raw in fh:
|
||||
m = re.match(r"\[(\w)\]\[([\w.]+)\]: ?(.*)$", normalize(raw.rstrip("\n")))
|
||||
if not m:
|
||||
continue
|
||||
_lvl, tag, msg = m.groups()
|
||||
if "MARK tx" in msg:
|
||||
on = True
|
||||
if not on:
|
||||
continue
|
||||
if (
|
||||
tag in ("remote.pronto", "trig", "heap")
|
||||
or "MARK" in msg
|
||||
or "BS pronto" in msg
|
||||
or tag == "remote.nec"
|
||||
):
|
||||
out.append(f"{tag}: {msg}")
|
||||
pathlib.Path(sys.argv[2]).write_text("\n".join(out) + "\n", encoding="utf-8")
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
main()
|
||||
@@ -0,0 +1,86 @@
|
||||
"""gen.py [OUT]: write OUT (default gen-common.yaml next to this script) with raw frames, buttons, on_pronto trigger,
|
||||
pronto binary sensors and heap logging; prints each frame's length and the pronto binary sensor data."""
|
||||
|
||||
import pathlib
|
||||
import sys
|
||||
|
||||
|
||||
def frame(n, seed):
|
||||
out = []
|
||||
for i in range(n):
|
||||
if i % 2 == 0:
|
||||
out.append(300 + (i * 37 + seed * 101) % 900)
|
||||
else:
|
||||
out.append(-(400 + (i * 53 + seed * 59) % 1500))
|
||||
return out
|
||||
|
||||
|
||||
def pronto(t):
|
||||
nums = [0, 4145146 // 38000, (len(t) + 1) // 2, 0]
|
||||
for x in t:
|
||||
d = x - 20 if x > 0 else -x + 20
|
||||
nums.append((d + 13) // 26)
|
||||
return " ".join(f"{v:04X}" for v in nums) + " 0181 "
|
||||
|
||||
|
||||
frames = {
|
||||
"raw21a": frame(21, 1),
|
||||
"raw21b": [x + 260 if x > 0 else x - 260 for x in frame(21, 1)],
|
||||
"raw41": frame(41, 2),
|
||||
"raw43": frame(43, 3),
|
||||
"raw121": frame(121, 4),
|
||||
"raw245": frame(245, 5),
|
||||
}
|
||||
y = []
|
||||
y.append("button:")
|
||||
y.append(
|
||||
" - {platform: template, name: tx nec, on_press: [{logger.log: 'MARK tx nec'}, {remote_transmitter.transmit_nec: {address: 0x1234, command: 0x78}}]}"
|
||||
)
|
||||
for k, v in frames.items():
|
||||
y.append(
|
||||
f" - platform: template\n name: tx {k}\n on_press:\n - logger.log: 'MARK tx {k}'\n - remote_transmitter.transmit_raw:\n code: {v}"
|
||||
)
|
||||
y.append(
|
||||
""" - platform: template
|
||||
name: soak
|
||||
on_press:
|
||||
- lambda: id(heap_log).execute("before");
|
||||
- repeat:
|
||||
count: 100
|
||||
then:
|
||||
- remote_transmitter.transmit_nec: {{address: 0x1234, command: 0x78}}
|
||||
- delay: 150ms
|
||||
- remote_transmitter.transmit_raw:
|
||||
code: {}
|
||||
- delay: 250ms
|
||||
- delay: 1s
|
||||
- lambda: id(heap_log).execute("after");
|
||||
""".format(frames["raw245"])
|
||||
)
|
||||
y.append(f"""script:
|
||||
- id: heap_log
|
||||
parameters: {{tag: string}}
|
||||
then:
|
||||
- lambda: |-
|
||||
#ifdef USE_ESP32
|
||||
ESP_LOGI("heap", "HEAP %s free=%u largest=%u", tag.c_str(), (unsigned) heap_caps_get_free_size(MALLOC_CAP_INTERNAL), (unsigned) heap_caps_get_largest_free_block(MALLOC_CAP_INTERNAL));
|
||||
#else
|
||||
ESP_LOGI("heap", "HEAP %s free=%u largest=%u", tag.c_str(), (unsigned) ESP.getFreeHeap(), (unsigned) ESP.getMaxFreeBlockSize());
|
||||
#endif
|
||||
binary_sensor:
|
||||
- platform: remote_receiver
|
||||
name: pronto a
|
||||
receiver_id: irrx
|
||||
pronto:
|
||||
data: "{pronto(frames["raw21a"]).strip()}"
|
||||
on_press: [{{logger.log: 'BS pronto a ON'}}]
|
||||
""")
|
||||
out = (
|
||||
pathlib.Path(sys.argv[1])
|
||||
if len(sys.argv) > 1
|
||||
else pathlib.Path(__file__).with_name("gen-common.yaml")
|
||||
)
|
||||
out.write_text("\n".join(y), encoding="utf-8")
|
||||
for k, v in frames.items():
|
||||
print(k, len(v), (4 + len(v)) * 5)
|
||||
print("PRONTO_A", pronto(frames["raw21a"]))
|
||||
@@ -0,0 +1,31 @@
|
||||
"""jit.py EXT_A EXT_B: compare the on_pronto trigger frames of two extract.py outputs in order; counts identical
|
||||
frames, frames whose length differs, and the largest difference in Pronto units (timing jitter) between same-length
|
||||
frames, leaving out the header and the trailing idle."""
|
||||
|
||||
import sys
|
||||
|
||||
from extract import read_lines, trigger_blocks
|
||||
|
||||
|
||||
def frames(p):
|
||||
return [
|
||||
[int(x, 16) for x in text.split()]
|
||||
for _n, text, _i in trigger_blocks(read_lines(p))
|
||||
]
|
||||
|
||||
|
||||
a, b = frames(sys.argv[1]), frames(sys.argv[2])
|
||||
same = sum(x == y for x, y in zip(a, b, strict=False))
|
||||
lens = sum(len(x) != len(y) for x, y in zip(a, b, strict=False))
|
||||
md = max(
|
||||
(
|
||||
abs(p - q)
|
||||
for x, y in zip(a, b, strict=False)
|
||||
if len(x) == len(y)
|
||||
for p, q in zip(x[4:-1], y[4:-1], strict=False)
|
||||
),
|
||||
default=0,
|
||||
)
|
||||
print(
|
||||
f"{len(a)}/{len(b)} frames, {same} identical, {lens} length differs, max unit diff {md}"
|
||||
)
|
||||
@@ -0,0 +1,25 @@
|
||||
#!/bin/bash
|
||||
# run.sh NAME TREE YAML PORT BAUD ROUNDS: flash NAME, capture serial, press each tx button ROUNDS times with
|
||||
# ../press.py, then the soak button, and count the markers, triggers and binary sensor presses on serial
|
||||
S=$(cd "$(dirname "$0")" && pwd); K=$(dirname "$(dirname "$S")"); N=$1; T=$2; Y=$3; P=$4; B=$5; R=$6
|
||||
PY=${PY:-$T/venv/bin/python}
|
||||
pkill -f "[s]erial_capture.py $P "
|
||||
TREE=$T PIO_CORE=${PIO_CORE:-$S/pio} bash "$K/pio_build.sh" upload "$N" "$Y" "$P" || exit 1
|
||||
rm -f "$S/$N-serial.txt"
|
||||
# boot and IP lookup (up to 160 s), 7 presses of 1.5 s per round, the 90 s soak, margin; stopped after the drive
|
||||
"$PY" "$K/serial_capture.py" "$P" $((160 + R * 11 + 120)) "$S/$N-serial.txt" --baud "$B" --reset > /dev/null 2>&1 &
|
||||
CAP=$!
|
||||
IP=""; for _ in $(seq 60); do IP=$(grep -a -o 'IP Address: [0-9.]*' "$S/$N-serial.txt" 2>/dev/null | tail -1 | cut -d' ' -f3); [ -n "$IP" ] && break; sleep 2; done
|
||||
[ -n "$IP" ] || IP=$(bash "$K/resolve.sh" "$N" 20) || { echo no ip; kill $CAP; exit 1; }
|
||||
sleep 5
|
||||
PRESSES=()
|
||||
for _ in $(seq "$R"); do
|
||||
for b in tx_nec tx_raw21a tx_raw21b tx_raw41 tx_raw43 tx_raw121 tx_raw245; do PRESSES+=("$b:1.5"); done
|
||||
done
|
||||
"$PY" "$S/../press.py" "$IP" "${PRESSES[@]}" soak:90 > "$S/$N-drive.txt" 2>&1; RC=$?
|
||||
sleep 3; kill $CAP; wait $CAP 2>/dev/null
|
||||
cat "$S/$N-drive.txt"
|
||||
echo "MARK tx $(grep -a -c 'MARK tx' "$S/$N-serial.txt") triggers $(grep -a -c 'TRIG len=' "$S/$N-serial.txt")" \
|
||||
"binary sensor on_press $(grep -a -c 'BS pronto a ON' "$S/$N-serial.txt")"
|
||||
[ $RC -eq 0 ] || echo "press.py failed (rc=$RC)"
|
||||
exit $RC
|
||||
@@ -0,0 +1,29 @@
|
||||
#!/bin/bash
|
||||
# compare.sh A B: diff two run.sh results (NAME prefixes) in this directory: API states, entity dump block, the
|
||||
# re-dump 1 s after init, hub trigger vs entity publish order, and the command frames the simulator received.
|
||||
# Timestamps (with or without milliseconds) and line numbers are removed with ../lognorm.py.
|
||||
# Exits 1 when an input is missing or empty or a part extracts nothing, so two failed runs never compare as identical,
|
||||
# and 2 when a part differs.
|
||||
cd "$(dirname "$0")" || exit 1; A=${1:?name}; B=${2:?name}; PY=${PY:-python3}
|
||||
for f in $A-states.json $A-log.txt $B-states.json $B-log.txt; do
|
||||
[ -s "$f" ] || { echo "missing or empty: $f"; exit 1; }
|
||||
done
|
||||
for n in $A $B; do
|
||||
$PY -c "
|
||||
import json
|
||||
for e in json.load(open('$n-states.json')): print(e['phase'], e['entity'], json.dumps(e['value'], sort_keys=True))" > $n-states.txt ||
|
||||
{ echo "unreadable: $n-states.json"; exit 1; }
|
||||
$PY ../lognorm.py $n-log.txt > $n-norm.txt || exit 1
|
||||
awk '/^\[C\]\[logger\]: Logger:/{on=1} on' $n-norm.txt | grep -av "^\[[DIVW]\]" > $n-dumpfull.txt
|
||||
awk '/Tuya Binary Sensor:/{on=1} on{print} /Current Temperature has datapoint ID 22/{exit}' $n-dumpfull.txt > $n-ent.txt
|
||||
awk '/\[C\]\[tuya\]: Tuya:/{c++} c>=2 && /\[C\]\[tuya\]/' $n-dumpfull.txt > $n-redump.txt
|
||||
grep -a "\[ord\|SIM \(STEP\|TX\|DELIVER\)\|Datapoint [0-9]* update to" $n-norm.txt > $n-ord.txt
|
||||
grep -a "SIM RX" $n-norm.txt | grep -v "cmd 00" | sed -E 's/#[0-9]+ //' > $n-rx.txt
|
||||
done
|
||||
bad=0
|
||||
for k in states ent redump ord rx; do
|
||||
if [ ! -s $A-$k.txt ] || [ ! -s $B-$k.txt ]; then echo "$k: EMPTY in $A or $B"; bad=1
|
||||
elif diff -q $A-$k.txt $B-$k.txt > /dev/null; then echo "$k: identical ($(wc -l < $A-$k.txt) lines)"
|
||||
else echo "$k: DIFFERS"; [ $bad -eq 0 ] && bad=2; fi
|
||||
done
|
||||
exit $bad
|
||||
+21
@@ -0,0 +1,21 @@
|
||||
#!/bin/bash
|
||||
# run.sh NAME TREE: flash NAME (already compiled from TREE with pio_build.sh), capture the serial log with a reset,
|
||||
# and drive the scenario over the API. Outputs NAME-log.txt, NAME-states.json, NAME-drive.txt.
|
||||
# Needs secrets.yaml (wifi_ssid, wifi_password, api_key) next to tuya.yaml.
|
||||
# env: PORT (the board), API_KEY, PY
|
||||
D=$(cd "$(dirname "$0")" && pwd); S=$(dirname "$D"); N=${1:?name}; T=${2:?tree}; P=${PORT:?set PORT}
|
||||
PY=${PY:-$T/venv/bin/python}
|
||||
pkill -f "[s]erial_capture.py $P "
|
||||
# outputs of an earlier run must not pass for this one in compare.sh
|
||||
rm -f "$D/$N-log.txt" "$D/$N-states.json" "$D/$N-drive.txt"
|
||||
TREE=$T PY=$PY "$S/pio_build.sh" upload "$N" "$D/tuya.yaml" "$P" || exit 1
|
||||
# 130 s is an upper bound; the capture is stopped right after the drive
|
||||
$PY "$S/serial_capture.py" "$P" 130 "$D/$N-log.txt" --reset &
|
||||
CAP=$!
|
||||
IP=$("$S/resolve.sh" "$N" 40) || { kill $CAP; exit 1; }
|
||||
PYTHONUNBUFFERED=1 timeout 120 $PY "$D/tuyadrive.py" "$IP" "$D/$N-states.json" > "$D/$N-drive.txt" 2>&1
|
||||
RC=$?
|
||||
echo "drive rc=$RC"
|
||||
sleep 2; kill $CAP; wait $CAP 2>/dev/null
|
||||
grep -a -c "SIM RX" "$D/$N-log.txt"
|
||||
exit $RC
|
||||
@@ -0,0 +1,208 @@
|
||||
substitutions:
|
||||
name: tuyax
|
||||
|
||||
# Tuya hub plus every moved platform, fed by tuyasim.hpp on a cross-wired second UART (esphome#22724).
|
||||
# The run file sets `name` and includes this file as a package.
|
||||
esphome:
|
||||
name: ${name}
|
||||
includes:
|
||||
- tuyasim.hpp
|
||||
on_boot:
|
||||
priority: 800
|
||||
then:
|
||||
- lambda: tuyasim::bus = id(mcu_uart);
|
||||
|
||||
esp32:
|
||||
board: pico32
|
||||
framework:
|
||||
type: esp-idf
|
||||
|
||||
logger:
|
||||
level: DEBUG
|
||||
|
||||
wifi:
|
||||
ssid: !secret wifi_ssid
|
||||
password: !secret wifi_password
|
||||
power_save_mode: none
|
||||
|
||||
api:
|
||||
reboot_timeout: 0s
|
||||
encryption:
|
||||
key: !secret api_key
|
||||
|
||||
uart:
|
||||
- id: hub_uart
|
||||
tx_pin: {number: GPIO9, allow_other_uses: true}
|
||||
rx_pin: {number: GPIO10, allow_other_uses: true}
|
||||
baud_rate: 9600
|
||||
- id: mcu_uart
|
||||
tx_pin: {number: GPIO10, allow_other_uses: true}
|
||||
rx_pin: {number: GPIO9, allow_other_uses: true}
|
||||
baud_rate: 9600
|
||||
|
||||
interval:
|
||||
- interval: 5ms
|
||||
then:
|
||||
- lambda: tuyasim::poll();
|
||||
- interval: 10s
|
||||
then:
|
||||
- lambda: |-
|
||||
ESP_LOGI("heap", "HEAP up=%us free=%u min=%u largest=%u", (unsigned) (millis() / 1000),
|
||||
(unsigned) esp_get_free_heap_size(), (unsigned) esp_get_minimum_free_heap_size(),
|
||||
(unsigned) heap_caps_get_largest_free_block(MALLOC_CAP_8BIT));
|
||||
|
||||
button:
|
||||
- platform: template
|
||||
name: Sim start
|
||||
id: sim_start
|
||||
on_press:
|
||||
- lambda: tuyasim::start();
|
||||
|
||||
tuya:
|
||||
id: hub
|
||||
uart_id: hub_uart
|
||||
on_datapoint_update:
|
||||
- sensor_datapoint: 1
|
||||
datapoint_type: bool
|
||||
then:
|
||||
- lambda: ESP_LOGI("ord", "HUB dp1 x=%d bs1=%d sw1=%d", x, id(bs1).state, id(sw1).state);
|
||||
- sensor_datapoint: 2
|
||||
datapoint_type: int
|
||||
then:
|
||||
- lambda: ESP_LOGI("ord", "HUB dp2 x=%d s_int=%.0f", x, id(s_int).state);
|
||||
- sensor_datapoint: 3
|
||||
datapoint_type: string
|
||||
then:
|
||||
- lambda: ESP_LOGI("ord", "HUB dp3 x=%s ts_str=%s", x.c_str(), id(ts_str).state.c_str());
|
||||
- sensor_datapoint: 4
|
||||
datapoint_type: enum
|
||||
then:
|
||||
- lambda: ESP_LOGI("ord", "HUB dp4 x=%u sel_enum=%s", x, id(sel_enum).current_option().c_str());
|
||||
- sensor_datapoint: 23
|
||||
datapoint_type: enum
|
||||
then:
|
||||
- lambda: ESP_LOGI("ord", "HUB dp23 x=%u wh_mode=%d", x, (int) id(wh).get_mode());
|
||||
|
||||
binary_sensor:
|
||||
- platform: tuya
|
||||
id: bs1
|
||||
name: BS dp1
|
||||
sensor_datapoint: 1
|
||||
trigger_on_initial_state: true
|
||||
on_state:
|
||||
- lambda: ESP_LOGI("ord", "ENT bs1 %d", x);
|
||||
|
||||
switch:
|
||||
- platform: tuya
|
||||
id: sw1
|
||||
name: SW dp1
|
||||
switch_datapoint: 1
|
||||
on_state:
|
||||
- lambda: ESP_LOGI("ord", "ENT sw1 %d", x);
|
||||
|
||||
sensor:
|
||||
- platform: tuya
|
||||
id: s_int
|
||||
name: S int dp2
|
||||
sensor_datapoint: 2
|
||||
on_value:
|
||||
- lambda: ESP_LOGI("ord", "ENT s_int %.0f", x);
|
||||
- platform: tuya
|
||||
id: s_enum
|
||||
name: S enum dp4
|
||||
sensor_datapoint: 4
|
||||
on_value:
|
||||
- lambda: ESP_LOGI("ord", "ENT s_enum %.0f", x);
|
||||
- platform: tuya
|
||||
id: s_bm
|
||||
name: S bitmask dp5
|
||||
sensor_datapoint: 5
|
||||
on_value:
|
||||
- lambda: ESP_LOGI("ord", "ENT s_bm %.0f", x);
|
||||
|
||||
text_sensor:
|
||||
- platform: tuya
|
||||
id: ts_str
|
||||
name: TS string dp3
|
||||
sensor_datapoint: 3
|
||||
on_value:
|
||||
- lambda: ESP_LOGI("ord", "ENT ts_str %s", x.c_str());
|
||||
- platform: tuya
|
||||
id: ts_enum
|
||||
name: TS enum dp4
|
||||
sensor_datapoint: 4
|
||||
on_value:
|
||||
- lambda: ESP_LOGI("ord", "ENT ts_enum %s", x.c_str());
|
||||
- platform: tuya
|
||||
id: ts_raw
|
||||
name: TS raw dp6
|
||||
sensor_datapoint: 6
|
||||
on_value:
|
||||
- lambda: ESP_LOGI("ord", "ENT ts_raw %s", x.c_str());
|
||||
|
||||
select:
|
||||
- platform: tuya
|
||||
id: sel_enum
|
||||
name: Sel enum dp4
|
||||
enum_datapoint: 4
|
||||
options:
|
||||
0: Zero
|
||||
1: One
|
||||
2: Two
|
||||
on_value:
|
||||
- lambda: ESP_LOGI("ord", "ENT sel_enum %s", x.c_str());
|
||||
- platform: tuya
|
||||
id: sel_str
|
||||
name: Sel string dp24
|
||||
string_datapoint: 24
|
||||
options:
|
||||
- auto
|
||||
- manual
|
||||
- eco
|
||||
on_value:
|
||||
- lambda: ESP_LOGI("ord", "ENT sel_str %s", x.c_str());
|
||||
- platform: tuya
|
||||
id: sel_int
|
||||
name: Sel int dp25
|
||||
int_datapoint: 25
|
||||
options:
|
||||
0: Low
|
||||
1: High
|
||||
on_value:
|
||||
- lambda: ESP_LOGI("ord", "ENT sel_int %s", x.c_str());
|
||||
|
||||
water_heater:
|
||||
- platform: tuya
|
||||
id: wh
|
||||
name: WH
|
||||
switch_datapoint: 20
|
||||
target_temperature_datapoint: 21
|
||||
current_temperature_datapoint: 22
|
||||
current_temperature_multiplier: 0.5
|
||||
target_temperature_multiplier: 0.5
|
||||
mode_datapoint: 23
|
||||
eco_value: 0
|
||||
electric_value: 2
|
||||
supported_modes:
|
||||
- "OFF"
|
||||
- ECO
|
||||
- ELECTRIC
|
||||
visual:
|
||||
min_temperature: 30
|
||||
max_temperature: 75
|
||||
target_temperature_step: 1
|
||||
|
||||
number:
|
||||
- platform: tuya
|
||||
id: num
|
||||
name: Num dp30
|
||||
number_datapoint: 30
|
||||
min_value: 0
|
||||
max_value: 10
|
||||
step: 1
|
||||
|
||||
fan:
|
||||
- platform: tuya
|
||||
id: fan1
|
||||
name: Fan dp31
|
||||
switch_datapoint: 31
|
||||
@@ -0,0 +1,116 @@
|
||||
"""Drive the tuyasim scenario over the API and record every entity state (esphome#22724).
|
||||
|
||||
usage: tuyadrive.py HOST OUT.json (API_KEY in the env)
|
||||
Presses "Sim start", waits for the scripted datapoint changes, then sends switch, select, water heater, number and
|
||||
fan commands. OUT.json gets the ordered state list per entity; a summary is printed.
|
||||
"""
|
||||
|
||||
import asyncio
|
||||
from collections import Counter
|
||||
import json
|
||||
import pathlib
|
||||
import sys
|
||||
import time
|
||||
|
||||
sys.path.insert(0, str(pathlib.Path(__file__).resolve().parents[1]))
|
||||
from aioesphomeapi import WaterHeaterMode # noqa: E402
|
||||
from apiconn import connect # noqa: E402
|
||||
|
||||
STEPS_WAIT = 32 # 18 steps 1.5 s apart, plus margin
|
||||
|
||||
|
||||
def val(st):
|
||||
d = {}
|
||||
for k in (
|
||||
"state",
|
||||
"missing_state",
|
||||
"current_temperature",
|
||||
"target_temperature",
|
||||
"mode",
|
||||
"oscillating",
|
||||
"speed_level",
|
||||
):
|
||||
if hasattr(st, k):
|
||||
x = getattr(st, k)
|
||||
d[k] = x.value if hasattr(x, "value") else x
|
||||
return d
|
||||
|
||||
|
||||
async def main(host: str, out: str) -> None:
|
||||
c = await connect(host)
|
||||
ents, _ = await c.list_entities_services()
|
||||
by_key = {e.key: e for e in ents}
|
||||
by_id = {e.object_id: e for e in ents}
|
||||
events = []
|
||||
phase = ["initial"]
|
||||
t0 = time.monotonic()
|
||||
|
||||
def on_state(st):
|
||||
e = by_key.get(st.key)
|
||||
events.append(
|
||||
{
|
||||
"t": round(time.monotonic() - t0, 3),
|
||||
"phase": phase[0],
|
||||
"entity": e.object_id if e else st.key,
|
||||
"value": val(st),
|
||||
}
|
||||
)
|
||||
|
||||
c.subscribe_states(on_state)
|
||||
await asyncio.sleep(3)
|
||||
phase[0] = "scenario"
|
||||
c.button_command(by_id["sim_start"].key)
|
||||
await asyncio.sleep(STEPS_WAIT)
|
||||
|
||||
phase[0] = "commands"
|
||||
k = {n: by_id[n].key for n in by_id}
|
||||
cmds = [
|
||||
("sw on", lambda: c.switch_command(k["sw_dp1"], True)),
|
||||
("sw off", lambda: c.switch_command(k["sw_dp1"], False)),
|
||||
("sel_enum Two", lambda: c.select_command(k["sel_enum_dp4"], "Two")),
|
||||
("sel_str manual", lambda: c.select_command(k["sel_string_dp24"], "manual")),
|
||||
("sel_int High", lambda: c.select_command(k["sel_int_dp25"], "High")),
|
||||
("wh on", lambda: c.water_heater_command(k["wh"], on=True)),
|
||||
(
|
||||
"wh target 55",
|
||||
lambda: c.water_heater_command(k["wh"], target_temperature=55),
|
||||
),
|
||||
(
|
||||
"wh mode eco",
|
||||
lambda: c.water_heater_command(k["wh"], mode=WaterHeaterMode.ECO),
|
||||
),
|
||||
(
|
||||
"wh mode electric",
|
||||
lambda: c.water_heater_command(k["wh"], mode=WaterHeaterMode.ELECTRIC),
|
||||
),
|
||||
("wh off", lambda: c.water_heater_command(k["wh"], on=False)),
|
||||
("num 3", lambda: c.number_command(k["num_dp30"], 3)),
|
||||
("fan on", lambda: c.fan_command(k["fan_dp31"], state=True)),
|
||||
("fan off", lambda: c.fan_command(k["fan_dp31"], state=False)),
|
||||
]
|
||||
for name, fn in cmds:
|
||||
events.append(
|
||||
{
|
||||
"t": round(time.monotonic() - t0, 3),
|
||||
"phase": "cmd",
|
||||
"entity": "CMD",
|
||||
"value": name,
|
||||
}
|
||||
)
|
||||
fn()
|
||||
await asyncio.sleep(2)
|
||||
await asyncio.sleep(3)
|
||||
await c.disconnect()
|
||||
|
||||
with pathlib.Path(out).open("w", encoding="utf-8") as f:
|
||||
json.dump(events, f, indent=1)
|
||||
counts = Counter(
|
||||
(ev["phase"], ev["entity"]) for ev in events if ev["entity"] != "CMD"
|
||||
)
|
||||
for (ph, ent), n in sorted(counts.items()):
|
||||
print(f"{ph:9} {ent:20} {n}")
|
||||
print("TOTAL", sum(counts.values()))
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
asyncio.run(main(sys.argv[1], sys.argv[2]))
|
||||
@@ -0,0 +1,194 @@
|
||||
#pragma once
|
||||
// A Tuya MCU on a second UART, cross-wired to the tuya hub's UART (esphome#22724, 2026-11-14).
|
||||
// Answers the handshake (heartbeat, product, conf query without status pin so the hub sends WIFI_STATE,
|
||||
// wifi state, datapoint query), echoes every DATAPOINT_DELIVER back as a report like a real MCU, and plays a
|
||||
// scripted list of datapoint changes once tuyasim::start() is called. Every frame from the hub is logged as
|
||||
// `SIM RX #n cmd XX: <hex>` so command streams can be diffed between builds.
|
||||
// Call tuyasim::poll() from a short interval; set tuyasim::bus in on_boot.
|
||||
#include <string>
|
||||
#include <vector>
|
||||
#include "esphome/components/uart/uart.h"
|
||||
#include "esphome/core/hal.h"
|
||||
#include "esphome/core/helpers.h"
|
||||
#include "esphome/core/log.h"
|
||||
|
||||
namespace tuyasim {
|
||||
|
||||
inline esphome::uart::UARTComponent *bus = nullptr;
|
||||
inline std::vector<uint8_t> rx;
|
||||
inline uint32_t frames = 0;
|
||||
inline uint32_t delivers = 0;
|
||||
inline bool hb_sent = false;
|
||||
inline int step = -1; // -1: scenario not started
|
||||
inline uint32_t next_step_ms = 0;
|
||||
inline uint32_t step_gap_ms = 1500;
|
||||
|
||||
struct Dp {
|
||||
uint8_t id;
|
||||
uint8_t type; // 0 raw, 1 bool, 2 value, 3 string, 4 enum, 5 bitmask
|
||||
std::vector<uint8_t> data;
|
||||
};
|
||||
|
||||
inline Dp b(uint8_t id, bool v) { return {id, 1, {static_cast<uint8_t>(v)}}; }
|
||||
inline Dp v(uint8_t id, int32_t x) {
|
||||
uint32_t u = static_cast<uint32_t>(x);
|
||||
return {id, 2, {uint8_t(u >> 24), uint8_t(u >> 16), uint8_t(u >> 8), uint8_t(u)}};
|
||||
}
|
||||
inline Dp s(uint8_t id, const char *str) { return {id, 3, std::vector<uint8_t>(str, str + strlen(str))}; }
|
||||
inline Dp e(uint8_t id, uint8_t x) { return {id, 4, {x}}; }
|
||||
inline Dp m(uint8_t id, std::vector<uint8_t> bytes) { return {id, 5, bytes}; }
|
||||
inline Dp r(uint8_t id, std::vector<uint8_t> bytes) { return {id, 0, bytes}; }
|
||||
|
||||
// Current MCU state, reported in full on DATAPOINT_QUERY
|
||||
inline std::vector<Dp> &state() {
|
||||
static std::vector<Dp> st{b(1, false), v(2, 100), s(3, "boot"), e(4, 1), m(5, {0x01}),
|
||||
r(6, {0x01, 0x02}), b(20, false), v(21, 100), v(22, 80), e(23, 2),
|
||||
s(24, "auto"), v(25, 0), v(30, 5), b(31, false)};
|
||||
return st;
|
||||
}
|
||||
|
||||
inline void store(const Dp &dp) {
|
||||
for (auto &d : state()) {
|
||||
if (d.id == dp.id) {
|
||||
d = dp;
|
||||
return;
|
||||
}
|
||||
}
|
||||
state().push_back(dp);
|
||||
}
|
||||
|
||||
inline void send(uint8_t cmd, const std::vector<uint8_t> &payload) {
|
||||
std::vector<uint8_t> out{0x55, 0xAA, 0x03, cmd, uint8_t(payload.size() >> 8), uint8_t(payload.size())};
|
||||
out.insert(out.end(), payload.begin(), payload.end());
|
||||
out.push_back(esphome::sum_bytes(out.data(), out.size()));
|
||||
bus->write_array(out.data(), out.size());
|
||||
}
|
||||
|
||||
inline void report(const std::vector<Dp> &dps, uint8_t cmd = 0x07) {
|
||||
std::vector<uint8_t> p;
|
||||
for (const auto &d : dps) {
|
||||
store(d);
|
||||
p.push_back(d.id);
|
||||
p.push_back(d.type);
|
||||
p.push_back(uint8_t(d.data.size() >> 8));
|
||||
p.push_back(uint8_t(d.data.size()));
|
||||
p.insert(p.end(), d.data.begin(), d.data.end());
|
||||
}
|
||||
char hex[160];
|
||||
esphome::format_hex_to(hex, sizeof(hex), p.data(), p.size() > 70 ? 70 : p.size());
|
||||
ESP_LOGI("sim", "SIM TX cmd %02X: %s", cmd, hex);
|
||||
send(cmd, p);
|
||||
}
|
||||
|
||||
// The scripted datapoint changes, one entry per step
|
||||
inline std::vector<std::vector<Dp>> &script() {
|
||||
static std::vector<std::vector<Dp>> sc{
|
||||
{b(1, true)},
|
||||
{v(2, 1234)},
|
||||
{s(3, "hello")},
|
||||
{e(4, 2)},
|
||||
{m(5, {0x01, 0x05})},
|
||||
{r(6, {0xDE, 0xAD, 0xBE, 0xEF})},
|
||||
{b(20, true), v(21, 120), v(22, 90), e(23, 0)},
|
||||
{b(1, false)}, // sent as DATAPOINT_REPORT_SYNC, the hub must ack with 0x23
|
||||
{v(2, -5)},
|
||||
{s(3, "world 42")},
|
||||
{e(4, 0)},
|
||||
{s(24, "eco"), v(25, 1)},
|
||||
{m(5, {0x80, 0x00, 0x00, 0x01})},
|
||||
{e(23, 2), v(22, 100)},
|
||||
{v(30, 7), b(31, true)},
|
||||
{b(1, true), v(2, 77), s(3, "multi")},
|
||||
{v(2, 77)}, // same value again
|
||||
{b(20, false)},
|
||||
};
|
||||
return sc;
|
||||
}
|
||||
|
||||
inline void start() {
|
||||
step = 0;
|
||||
next_step_ms = esphome::millis();
|
||||
ESP_LOGI("sim", "SIM SCENARIO START (%u steps)", (unsigned) script().size());
|
||||
}
|
||||
|
||||
inline void handle(uint8_t cmd, const uint8_t *d, size_t len) {
|
||||
frames++;
|
||||
char hex[160];
|
||||
esphome::format_hex_to(hex, sizeof(hex), d, len > 70 ? 70 : len);
|
||||
ESP_LOGI("sim", "SIM RX #%u cmd %02X: %s", (unsigned) frames, cmd, hex);
|
||||
switch (cmd) {
|
||||
case 0x00:
|
||||
send(0x00, {static_cast<uint8_t>(hb_sent ? 0x01 : 0x00)});
|
||||
hb_sent = true;
|
||||
break;
|
||||
case 0x01: {
|
||||
const char *p = R"({"p":"tuyasim","v":"1.0.0","m":0})";
|
||||
send(0x01, std::vector<uint8_t>(p, p + strlen(p)));
|
||||
break;
|
||||
}
|
||||
case 0x02:
|
||||
send(0x02, {}); // no status pin: the hub reports wifi state itself
|
||||
break;
|
||||
case 0x03:
|
||||
send(0x03, {});
|
||||
break;
|
||||
case 0x08:
|
||||
report(state());
|
||||
break;
|
||||
case 0x06: {
|
||||
delivers++;
|
||||
// echo each delivered datapoint back as a report, as a real MCU does
|
||||
std::vector<Dp> dps;
|
||||
size_t i = 0;
|
||||
while (i + 4 <= len) {
|
||||
size_t n = (d[i + 2] << 8) | d[i + 3];
|
||||
if (i + 4 + n > len)
|
||||
break;
|
||||
dps.push_back({d[i], d[i + 1], std::vector<uint8_t>(d + i + 4, d + i + 4 + n)});
|
||||
i += 4 + n;
|
||||
}
|
||||
ESP_LOGI("sim", "SIM DELIVER #%u (%u dps)", (unsigned) delivers, (unsigned) dps.size());
|
||||
report(dps);
|
||||
break;
|
||||
}
|
||||
default:
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
inline void poll() {
|
||||
if (bus == nullptr)
|
||||
return;
|
||||
uint8_t c;
|
||||
while (bus->available() && bus->read_byte(&c)) {
|
||||
rx.push_back(c);
|
||||
if (rx.size() == 1 && rx[0] != 0x55) {
|
||||
rx.clear();
|
||||
continue;
|
||||
}
|
||||
if (rx.size() == 2 && rx[1] != 0xAA) {
|
||||
rx.clear();
|
||||
continue;
|
||||
}
|
||||
if (rx.size() < 6)
|
||||
continue;
|
||||
size_t n = (rx[4] << 8) | rx[5];
|
||||
if (rx.size() < 7 + n)
|
||||
continue;
|
||||
if (esphome::sum_bytes(rx.data(), 6 + n) != rx[6 + n])
|
||||
ESP_LOGW("sim", "SIM RX bad checksum");
|
||||
else
|
||||
handle(rx[3], rx.data() + 6, n);
|
||||
rx.clear();
|
||||
}
|
||||
if (step >= 0 && step < (int) script().size() && (int32_t) (esphome::millis() - next_step_ms) >= 0) {
|
||||
ESP_LOGI("sim", "SIM STEP %d", step);
|
||||
report(script()[step], step == 7 ? 0x22 : 0x07);
|
||||
step++;
|
||||
next_step_ms = esphome::millis() + step_gap_ms;
|
||||
if (step == (int) script().size())
|
||||
ESP_LOGI("sim", "SIM SCENARIO DONE");
|
||||
}
|
||||
}
|
||||
|
||||
} // namespace tuyasim
|
||||
@@ -0,0 +1,41 @@
|
||||
"""Count "PUB <dev>.<key> <value>" lines per entity in a capture, compare with expected_all.json and print a values
|
||||
digest per entity so two builds can be compared. Usage: count_all.py CAPTURE [CAPTURE...]"""
|
||||
|
||||
import collections
|
||||
import hashlib
|
||||
import json
|
||||
from pathlib import Path
|
||||
import re
|
||||
import sys
|
||||
|
||||
exp = json.loads(
|
||||
Path(__file__).with_name("expected_all.json").read_text(encoding="utf-8")
|
||||
)
|
||||
PUB = re.compile(r"PUB (\S+) (\S+)")
|
||||
results = {}
|
||||
failed = False
|
||||
for cap in sys.argv[1:]:
|
||||
vals = collections.defaultdict(list)
|
||||
for line in Path(cap).read_text(encoding="utf-8", errors="replace").splitlines():
|
||||
if m := PUB.search(line):
|
||||
vals[m.group(1)].append(m.group(2))
|
||||
results[cap] = vals
|
||||
bad = 0
|
||||
print(f"== {cap}")
|
||||
for ent in sorted(set(exp) | set(vals)):
|
||||
got, want = len(vals.get(ent, [])), exp.get(ent)
|
||||
ok = got == want
|
||||
bad += not ok
|
||||
dig = hashlib.sha1(" ".join(vals.get(ent, [])).encode()).hexdigest()[:8]
|
||||
print(f" {ent:26} want {want!s:>3} got {got:3} {dig} {'ok' if ok else 'DIFF'}")
|
||||
failed |= bad > 0
|
||||
print(
|
||||
f" total publishes {sum(len(v) for v in vals.values())}, entities with a mismatch: {bad}"
|
||||
)
|
||||
if len(results) > 1:
|
||||
caps = list(results)
|
||||
same = all(results[c] == results[caps[0]] for c in caps[1:])
|
||||
print("values identical across captures:", same)
|
||||
failed |= not same
|
||||
if failed:
|
||||
sys.exit(1)
|
||||
@@ -9,7 +9,7 @@ from pathlib import Path
|
||||
import struct
|
||||
import sys
|
||||
|
||||
from cryptography.hazmat.primitives.ciphers.aead import AESCCM
|
||||
from mibeacon import XB_FILL, c_rows, enc, plain, t16, th
|
||||
|
||||
OTA = "--ota" in sys.argv[1:]
|
||||
CYCLES = 40
|
||||
@@ -25,41 +25,24 @@ MIDX = {k: i for i, k in enumerate(MACS)}
|
||||
|
||||
|
||||
def mibeacon_plain(dev, pid, cnt, obj):
|
||||
return (
|
||||
bytes([0x50, 0x20])
|
||||
+ struct.pack("<H", pid)
|
||||
+ bytes([cnt])
|
||||
+ MAC[dev][::-1]
|
||||
+ obj
|
||||
)
|
||||
return plain(MAC[dev], pid, cnt, obj)
|
||||
|
||||
|
||||
def mibeacon_enc(dev, pid, cnt, obj, key, short, corrupt=False):
|
||||
ext = bytes([cnt, 0x00, 0x00])
|
||||
if short:
|
||||
hdr = bytes([0x48, 0x59]) + struct.pack("<H", pid) + bytes([cnt])
|
||||
else:
|
||||
hdr = (
|
||||
bytes([0x58, 0x58]) + struct.pack("<H", pid) + bytes([cnt]) + MAC[dev][::-1]
|
||||
)
|
||||
nonce = MAC[dev][::-1] + hdr[2:5] + ext
|
||||
ct = AESCCM(key, tag_length=4).encrypt(nonce, obj, b"\x11")
|
||||
body, tag = ct[:-4], ct[-4:]
|
||||
if corrupt:
|
||||
tag = bytes([tag[0] ^ 0xFF]) + tag[1:]
|
||||
out = hdr + body + ext + tag
|
||||
out = enc(
|
||||
MAC[dev],
|
||||
pid,
|
||||
cnt,
|
||||
obj,
|
||||
key,
|
||||
short,
|
||||
version=0x59 if short else 0x58,
|
||||
corrupt=corrupt,
|
||||
)
|
||||
assert len(out) == (19 if short else 18 + len(obj)), len(out)
|
||||
return out
|
||||
|
||||
|
||||
def t16(v): # 0x1004 temperature object
|
||||
return bytes([0x04, 0x10, 0x02]) + struct.pack("<h", round(v * 10))
|
||||
|
||||
|
||||
def th(t, h): # 0x100D temperature + humidity
|
||||
return bytes([0x0D, 0x10, 0x04]) + struct.pack("<hh", round(t * 10), round(h * 10))
|
||||
|
||||
|
||||
frames = [] # (dev, kind 0 = FE95 / 1 = 181A, data, tag for the expectation)
|
||||
frames.append(("D", 0, mibeacon_plain("D", 0x0347, 0, t16(11.1)), "D_boot"))
|
||||
steps = [len(frames)]
|
||||
@@ -125,47 +108,19 @@ print("pr ", expected["pr"])
|
||||
Path("expected.json").write_text(json.dumps(expected), encoding="utf-8")
|
||||
|
||||
|
||||
def c_rows(name, rows):
|
||||
out = [f"static const uint8_t {name}[{len(rows)}][{len(rows[0][0])}] = {{"]
|
||||
out += [
|
||||
" {" + ",".join(f"0x{b:02X}" for b in row) + "}," + (f" // {c}" if c else "")
|
||||
for row, c in rows
|
||||
]
|
||||
return "\n".join([*out, "};", ""])
|
||||
|
||||
|
||||
mac_rows = [(MAC[k], "") for k in MACS]
|
||||
frame_rows = [
|
||||
((bytes([MIDX[d], kind, len(data)]) + data).ljust(28, b"\0"), tag)
|
||||
for d, kind, data, tag in frames
|
||||
]
|
||||
|
||||
INJECT = """
|
||||
inline void xb_fill(esphome::esp32_ble::BLEScanResult &r, const uint8_t *mac, bool fe95, const uint8_t *data,
|
||||
uint8_t len) {
|
||||
memcpy(r.bda, mac, 6);
|
||||
r.ble_addr_type = 0;
|
||||
r.rssi = -50;
|
||||
uint8_t n = 0;
|
||||
r.ble_adv[n++] = 2;
|
||||
r.ble_adv[n++] = 0x01;
|
||||
r.ble_adv[n++] = 0x06;
|
||||
r.ble_adv[n++] = len + 3;
|
||||
r.ble_adv[n++] = 0x16;
|
||||
r.ble_adv[n++] = fe95 ? 0x95 : 0x1A;
|
||||
r.ble_adv[n++] = fe95 ? 0xFE : 0x18;
|
||||
memcpy(r.ble_adv + n, data, len);
|
||||
r.adv_data_len = n + len;
|
||||
r.scan_rsp_len = 0;
|
||||
r.search_evt = ESP_GAP_SEARCH_INQ_RES_EVT;
|
||||
}
|
||||
|
||||
INJECT = (
|
||||
XB_FILL
|
||||
+ """
|
||||
inline void xb_inject(size_t first, size_t last) {
|
||||
for (size_t i = first; i < last; i++) {
|
||||
const uint8_t *row = XB_FRAMES[i];
|
||||
esphome::esp32_ble::BLEScanResult r{};
|
||||
xb_fill(r, XB_MACS[row[0]], row[1] == 0, row + 3, row[2]);
|
||||
esphome::esp32_ble_tracker::global_esp32_ble_tracker->gap_scan_event_handler(r);
|
||||
xb_inject_one(XB_MACS[row[0]], row[1] == 0 ? 0xFE95 : 0x181A, row + 3, row[2]);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -180,7 +135,7 @@ inline void xb_hex(const char *label, const std::vector<uint8_t> &v) {
|
||||
// Parse one encrypted frame with the listener directly and show whether it rewrote the shared service data.
|
||||
inline void xb_rewrite_probe(esphome::ble_device_base::ESPBTDeviceListener *listener) {
|
||||
esphome::esp32_ble::BLEScanResult r{};
|
||||
xb_fill(r, XB_MACS[0], true, XB_PROBE, sizeof(XB_PROBE));
|
||||
xb_fill(r, XB_MACS[0], 0xFE95, XB_PROBE, sizeof(XB_PROBE));
|
||||
esphome::ble_device_base::ESPBTDevice device;
|
||||
device.parse_scan_rst(r);
|
||||
xb_hex("before", device.get_service_datas()[0].data);
|
||||
@@ -189,6 +144,7 @@ inline void xb_rewrite_probe(esphome::ble_device_base::ESPBTDeviceListener *list
|
||||
xb_hex("after ", device.get_service_datas()[0].data);
|
||||
}
|
||||
"""
|
||||
)
|
||||
|
||||
ADVERTISE = """
|
||||
// Sends where a GAP call returned an error when it was queued. Failures the controller reports later in the GAP
|
||||
|
||||
@@ -0,0 +1,419 @@
|
||||
"""One device per Xiaomi platform (20 listener-only platforms, the three that stay components, plus ATC/PVVX):
|
||||
writes frames_all.hpp (injected by xball.yaml), xball_entities.yaml (the platform configs, each entity logs
|
||||
"PUB <dev>.<key> <value>" on every publish) and expected_all.json (publishes per entity) for count_all.py.
|
||||
Usage: gen_all.py
|
||||
Every frame is injected twice in a row: MiBeacon devices drop the repeat by frame counter, the FD50/181D/181A
|
||||
parsers without a counter publish it again. Binary sensors only publish on a change (trigger_on_initial_state on)."""
|
||||
|
||||
import json
|
||||
from pathlib import Path
|
||||
import struct
|
||||
|
||||
import mibeacon
|
||||
from mibeacon import XB_FILL, c_rows, o, plain, t16, th
|
||||
|
||||
HERE = (
|
||||
Path(__file__).resolve().parent
|
||||
) # count_all.py and run_all.sh read the outputs here
|
||||
CYCLES = 10
|
||||
KEY = bytes.fromhex("6c0d1a3e5b7f29c4a18e90d2f3b46a57")
|
||||
KEYHEX = KEY.hex()
|
||||
|
||||
|
||||
def mac(i):
|
||||
return bytes.fromhex(f"A4C138001{i:03X}"[:12])
|
||||
|
||||
|
||||
def enc(m, pid, cnt, obj, short=False, fc=None):
|
||||
return mibeacon.enc(m, pid, cnt, obj, KEY, short, fc)
|
||||
|
||||
|
||||
# name: (platform, kind, yaml body, sensors {key: publishes-per-new-frame or "bin"}, frame builder k -> [(uuid, data)])
|
||||
DEVS = {}
|
||||
|
||||
|
||||
def dev(name, platform, body, ents, build, moved=True, hub=None):
|
||||
DEVS[name] = {
|
||||
"platform": platform,
|
||||
"body": body,
|
||||
"ents": ents,
|
||||
"build": build,
|
||||
"moved": moved,
|
||||
"hub": hub,
|
||||
}
|
||||
|
||||
|
||||
FE95, FD50, S181D, S181A, S181B = 0xFE95, 0xFD50, 0x181D, 0x181A, 0x181B
|
||||
# moved platforms (sensor)
|
||||
dev(
|
||||
"cgd1",
|
||||
"xiaomi_cgd1",
|
||||
"bindkey",
|
||||
{"temperature": "s"},
|
||||
lambda m, k: [(FE95, enc(m, 0x0576, k, t16(30 + k / 10)))],
|
||||
)
|
||||
dev(
|
||||
"cgdk2",
|
||||
"xiaomi_cgdk2",
|
||||
"bindkey",
|
||||
{"temperature": "s"},
|
||||
lambda m, k: [(FE95, enc(m, 0x066F, k, t16(31 + k / 10)))],
|
||||
)
|
||||
dev(
|
||||
"cgg1",
|
||||
"xiaomi_cgg1",
|
||||
"",
|
||||
{"temperature": "s", "humidity": "s"},
|
||||
lambda m, k: [(FE95, plain(m, 0x0347, k, th(32 + k / 10, 40 + k)))],
|
||||
)
|
||||
dev(
|
||||
"gcls002",
|
||||
"xiaomi_gcls002",
|
||||
"",
|
||||
{"temperature": "s"},
|
||||
lambda m, k: [(FE95, plain(m, 0x03BC, k, t16(33 + k / 10)))],
|
||||
)
|
||||
dev(
|
||||
"hhccjcy01",
|
||||
"xiaomi_hhccjcy01",
|
||||
"",
|
||||
{"temperature": "s", "moisture": "s"},
|
||||
lambda m, k: [
|
||||
(FE95, plain(m, 0x0098, 2 * k, t16(34 + k / 10))),
|
||||
(FE95, plain(m, 0x0098, 2 * k + 1, o(0x1008, bytes([k + 10])))),
|
||||
],
|
||||
)
|
||||
dev(
|
||||
"hhccjcy10",
|
||||
"xiaomi_hhccjcy10",
|
||||
"",
|
||||
{
|
||||
"temperature": "r",
|
||||
"moisture": "r",
|
||||
"illuminance": "r",
|
||||
"conductivity": "r",
|
||||
"battery_level": "r",
|
||||
},
|
||||
lambda m, k: [
|
||||
(
|
||||
FD50,
|
||||
bytes([k + 20])
|
||||
+ struct.pack(">h", 350 + k)
|
||||
+ (1000 + k).to_bytes(3, "big")
|
||||
+ bytes([90 - k])
|
||||
+ struct.pack(">H", 300 + k),
|
||||
)
|
||||
],
|
||||
)
|
||||
dev(
|
||||
"hhccpot002",
|
||||
"xiaomi_hhccpot002",
|
||||
"",
|
||||
{"moisture": "s"},
|
||||
lambda m, k: [(FE95, plain(m, 0x015D, k, o(0x1008, bytes([k + 30]))))],
|
||||
)
|
||||
dev(
|
||||
"jqjcy01ym",
|
||||
"xiaomi_jqjcy01ym",
|
||||
"",
|
||||
{"formaldehyde": "s"},
|
||||
lambda m, k: [(FE95, plain(m, 0x02DF, k, o(0x1010, struct.pack("<H", k))))],
|
||||
)
|
||||
dev(
|
||||
"lywsd02",
|
||||
"xiaomi_lywsd02",
|
||||
"",
|
||||
{"temperature": "s"},
|
||||
lambda m, k: [(FE95, plain(m, 0x045B, k, t16(36 + k / 10)))],
|
||||
)
|
||||
dev(
|
||||
"lywsd02mmc",
|
||||
"xiaomi_lywsd02mmc",
|
||||
"bindkey",
|
||||
{"temperature": "s", "humidity": "s"},
|
||||
lambda m, k: [(FE95, enc(m, 0x2542, k, th(37 + k / 10, 50 + k), short=True))],
|
||||
)
|
||||
dev(
|
||||
"lywsd03mmc",
|
||||
"xiaomi_lywsd03mmc",
|
||||
"bindkey",
|
||||
{"temperature": "s"},
|
||||
lambda m, k: [(FE95, enc(m, 0x055B, k, t16(38 + k / 10)))],
|
||||
)
|
||||
dev(
|
||||
"lywsdcgq",
|
||||
"xiaomi_lywsdcgq",
|
||||
"",
|
||||
{"temperature": "s", "humidity": "s"},
|
||||
lambda m, k: [(FE95, plain(m, 0x01AA, k, th(39 + k / 10, 60 + k)))],
|
||||
)
|
||||
dev(
|
||||
"mhoc303",
|
||||
"xiaomi_mhoc303",
|
||||
"",
|
||||
{"temperature": "s", "humidity": "s"},
|
||||
lambda m, k: [(FE95, plain(m, 0x06D3, k, th(40 + k / 10, 70 + k)))],
|
||||
)
|
||||
dev(
|
||||
"mhoc401",
|
||||
"xiaomi_mhoc401",
|
||||
"bindkey",
|
||||
{"temperature": "s"},
|
||||
lambda m, k: [(FE95, enc(m, 0x0387, k, t16(41 + k / 10)))],
|
||||
)
|
||||
dev(
|
||||
"miscale",
|
||||
"xiaomi_miscale",
|
||||
"",
|
||||
{"weight": "r"},
|
||||
lambda m, k: [
|
||||
(
|
||||
S181D,
|
||||
bytes([0x22])
|
||||
+ struct.pack("<H", 14000 + 20 * k)
|
||||
+ bytes([0xE8, 0x07, 11, 14, 12, 0, k]),
|
||||
)
|
||||
],
|
||||
)
|
||||
dev(
|
||||
"xmwsdj04mmc",
|
||||
"xiaomi_xmwsdj04mmc",
|
||||
"bindkey",
|
||||
{"temperature": "s", "humidity": "s"},
|
||||
lambda m, k: [(FE95, enc(m, 0x1203, k, th(42 + k / 10, 45 + k), short=True))],
|
||||
)
|
||||
# moved platforms (binary_sensor; the main entity is the platform itself)
|
||||
dev(
|
||||
"cgpr1",
|
||||
"xiaomi_cgpr1",
|
||||
"bindkey",
|
||||
{"": "b", "idle_time": "s"},
|
||||
lambda m, k: [
|
||||
(
|
||||
FE95,
|
||||
enc(
|
||||
m,
|
||||
0x0A83,
|
||||
k,
|
||||
o(0x1017, struct.pack("<I", 0 if k % 2 else 60 * k)),
|
||||
short=True,
|
||||
),
|
||||
)
|
||||
],
|
||||
)
|
||||
dev(
|
||||
"mccgq02hl",
|
||||
"xiaomi_mccgq02hl",
|
||||
"bindkey",
|
||||
{"": "b"},
|
||||
lambda m, k: [(FE95, enc(m, 0x098B, k, o(0x0019, bytes([k % 2]))))],
|
||||
)
|
||||
dev(
|
||||
"mjyd02yla",
|
||||
"xiaomi_mjyd02yla",
|
||||
"bindkey",
|
||||
{"": "z", "light": "b"},
|
||||
lambda m, k: [(FE95, enc(m, 0x07F6, k, o(0x1018, bytes([k % 2]))))],
|
||||
)
|
||||
dev(
|
||||
"wx08zm",
|
||||
"xiaomi_wx08zm",
|
||||
"",
|
||||
{"": "b", "tablet": "s"},
|
||||
lambda m, k: [
|
||||
(
|
||||
FE95,
|
||||
plain(
|
||||
m, 0x040A, k, o(0x1012, bytes([k % 2])) + o(0x1013, bytes([100 - k]))
|
||||
),
|
||||
)
|
||||
],
|
||||
)
|
||||
# still components
|
||||
dev(
|
||||
"mue4094rt",
|
||||
"xiaomi_mue4094rt",
|
||||
"",
|
||||
{"": "m"},
|
||||
lambda m, k: [
|
||||
(FE95, plain(m, 0x03DD, k, o(0x000F, (200 + k).to_bytes(3, "little")), fc=0x40))
|
||||
],
|
||||
moved=False,
|
||||
)
|
||||
dev(
|
||||
"rtcgq02lm",
|
||||
"xiaomi_rtcgq02lm",
|
||||
"bindkey",
|
||||
{"light": "b", "battery_level": "s"},
|
||||
lambda m, k: [
|
||||
(FE95, enc(m, 0x0A8D, 2 * k, o(0x1018, bytes([k % 2])))),
|
||||
(FE95, enc(m, 0x0A8D, 2 * k + 1, o(0x100A, bytes([80 + k])))),
|
||||
],
|
||||
moved=False,
|
||||
hub=True,
|
||||
)
|
||||
|
||||
|
||||
def body_scale(m, k):
|
||||
obj = o(
|
||||
0x4E16,
|
||||
bytes([1])
|
||||
+ struct.pack("<I", 7000 + 10 * k)
|
||||
+ struct.pack("<I", 1700000000 + k),
|
||||
)
|
||||
assert len(obj) == 12
|
||||
return [(FE95, enc(m, 0x1234, k, obj, short=True))]
|
||||
|
||||
|
||||
dev(
|
||||
"body_scale",
|
||||
"xiaomi_body_scale",
|
||||
"bindkey",
|
||||
{"weight": "s", "profile_id": "s"},
|
||||
body_scale,
|
||||
moved=False,
|
||||
)
|
||||
dev(
|
||||
"atc",
|
||||
"atc_mithermometer",
|
||||
"",
|
||||
{"temperature": "s"},
|
||||
lambda m, k: [
|
||||
(
|
||||
S181A,
|
||||
m
|
||||
+ struct.pack(">h", 250 + k)
|
||||
+ bytes([45, 90])
|
||||
+ struct.pack(">H", 2950)
|
||||
+ bytes([k]),
|
||||
)
|
||||
],
|
||||
moved=None,
|
||||
)
|
||||
dev(
|
||||
"pvvx",
|
||||
"pvvx_mithermometer",
|
||||
"",
|
||||
{"temperature": "s"},
|
||||
lambda m, k: [
|
||||
(S181A, m[::-1] + struct.pack("<hHH", 2600 + k, 4600, 3010) + bytes([88, k, 0]))
|
||||
],
|
||||
moved=None,
|
||||
)
|
||||
|
||||
MACS = {n: mac(i + 1) for i, n in enumerate(DEVS)}
|
||||
frames = [] # (dev index, uuid, data, cycle)
|
||||
for k in range(1, CYCLES + 1):
|
||||
for i, (n, d) in enumerate(DEVS.items()):
|
||||
for uuid, data in d["build"](MACS[n], k):
|
||||
assert (
|
||||
len(data) <= 24
|
||||
or uuid != FE95
|
||||
or not (data[0] & 0x08)
|
||||
or n == "mccgq02hl"
|
||||
), (n, len(data))
|
||||
frames.append((i, uuid, data))
|
||||
frames.append((i, uuid, data)) # repeat: dropped by counter-based dedup
|
||||
|
||||
# Expected publishes per entity
|
||||
exp = {}
|
||||
for n, d in DEVS.items():
|
||||
for key, kind in d["ents"].items():
|
||||
ent = f"{n}.{key or 'main'}"
|
||||
if kind == "s":
|
||||
exp[ent] = CYCLES
|
||||
elif kind == "r": # no counter: the repeat publishes again
|
||||
exp[ent] = CYCLES * 2
|
||||
elif (
|
||||
kind == "b"
|
||||
): # alternating value, dedup on change; mccgq02hl starts closed = the initial off state
|
||||
exp[ent] = CYCLES - (n == "mccgq02hl")
|
||||
elif kind == "z": # configured, no frame sets it
|
||||
exp[ent] = 0
|
||||
elif kind == "m": # mue4094rt: motion true once, false 5 s after the last frame
|
||||
exp[ent] = 2
|
||||
HERE.joinpath("expected_all.json").write_text(
|
||||
json.dumps(exp, indent=1, sort_keys=True), encoding="utf-8"
|
||||
)
|
||||
|
||||
frame_rows = [
|
||||
((bytes([i, uuid & 0xFF, uuid >> 8, len(data)]) + data).ljust(28, b"\0"), "")
|
||||
for i, uuid, data in frames
|
||||
]
|
||||
per = len(frames) // CYCLES
|
||||
HDR = '#pragma once\n#include <cstring>\n#include "esphome/components/esp32_ble/ble_scan_result.h"\n'
|
||||
INJECT = (
|
||||
XB_FILL
|
||||
+ """
|
||||
inline void xa_inject(size_t first, size_t last) {
|
||||
for (size_t i = first; i < last && i < XA_COUNT; i++) {
|
||||
const uint8_t *row = XA_FRAMES[i];
|
||||
xb_inject_one(XA_MACS[row[0]], row[1] | row[2] << 8, row + 4, row[3]);
|
||||
}
|
||||
}
|
||||
"""
|
||||
)
|
||||
HERE.joinpath("frames_all.hpp").write_text(
|
||||
HDR
|
||||
+ c_rows("XA_MACS", [(m, n) for n, m in MACS.items()])
|
||||
+ c_rows("XA_FRAMES", frame_rows)
|
||||
+ f"static constexpr size_t XA_COUNT = {len(frames)};\n"
|
||||
+ f"static constexpr size_t XA_PER_CYCLE = {per};\n"
|
||||
+ INJECT,
|
||||
encoding="utf-8",
|
||||
)
|
||||
|
||||
|
||||
def macstr(m):
|
||||
return ":".join(f"{b:02X}" for b in m)
|
||||
|
||||
|
||||
def ent_yaml(ent, kind, ind):
|
||||
"""name plus a publish log at indent ind."""
|
||||
sp = " " * ind
|
||||
if kind in ("b", "m", "z"):
|
||||
return (
|
||||
f"{sp}trigger_on_initial_state: true\n{sp}on_state:\n"
|
||||
f'{sp} - lambda: \'ESP_LOGI("pub", "PUB {ent} %d", x);\'\n'
|
||||
)
|
||||
return f'{sp}on_value:\n{sp} - lambda: \'ESP_LOGI("pub", "PUB {ent} %.3f", x);\'\n'
|
||||
|
||||
|
||||
sensor, binary, top = [], [], []
|
||||
for n, d in DEVS.items():
|
||||
m = macstr(MACS[n])
|
||||
common = f" mac_address: {m}\n" + (
|
||||
f" bindkey: {KEYHEX}\n" if d["body"] == "bindkey" else ""
|
||||
)
|
||||
subs = {key: kind for key, kind in d["ents"].items() if key}
|
||||
if d["hub"]:
|
||||
top.append(f"{d['platform']}:\n - id: hub_{n}\n" + common)
|
||||
for kinds, lst in (({"b"}, binary), ({"s", "r"}, sensor)):
|
||||
blk = f" - platform: {d['platform']}\n id: hub_{n}\n"
|
||||
for key, kind in subs.items():
|
||||
if kind in kinds:
|
||||
blk += f' {key}:\n name: "{n} {key}"\n' + ent_yaml(
|
||||
f"{n}.{key}", kind, 6
|
||||
)
|
||||
lst.append(blk)
|
||||
continue
|
||||
blk = f" - platform: {d['platform']}\n" + common
|
||||
if "" in d["ents"]:
|
||||
blk += f' name: "{n} main"\n' + ent_yaml(f"{n}.main", d["ents"][""], 4)
|
||||
for key, kind in subs.items():
|
||||
blk += f' {key}:\n name: "{n} {key}"\n' + ent_yaml(
|
||||
f"{n}.{key}", kind, 6
|
||||
)
|
||||
(binary if "" in d["ents"] else sensor).append(blk)
|
||||
|
||||
out = (
|
||||
"".join(top)
|
||||
+ "\nsensor:\n"
|
||||
+ "".join(sensor)
|
||||
+ "\nbinary_sensor:\n"
|
||||
+ "".join(binary)
|
||||
)
|
||||
HERE.joinpath("xball_entities.yaml").write_text(out, encoding="utf-8")
|
||||
print(
|
||||
"devices", len(DEVS), "frames", len(frames), "per cycle", per, "entities", len(exp)
|
||||
)
|
||||
@@ -0,0 +1,84 @@
|
||||
"""MiBeacon frame builders and the C++ scan result injector shared by gen.py and gen_all.py."""
|
||||
|
||||
import struct
|
||||
|
||||
from cryptography.hazmat.primitives.ciphers.aead import AESCCM
|
||||
|
||||
|
||||
def plain(mac, pid, cnt, obj, fc=0x50):
|
||||
"""Unencrypted MiBeacon; the MAC is included when fc has bit 0x10."""
|
||||
hdr = bytes([fc, 0x20]) + struct.pack("<H", pid) + bytes([cnt])
|
||||
return hdr + (mac[::-1] if fc & 0x10 else b"") + obj
|
||||
|
||||
|
||||
def enc(mac, pid, cnt, obj, key, short=False, fc=None, version=0x59, corrupt=False):
|
||||
"""Encrypted MiBeacon v4/v5 (AES-CCM, tag 4, AAD 0x11): long with the MAC (fc 0x58), short without (fc 0x48).
|
||||
corrupt flips the first tag byte."""
|
||||
ext = bytes([cnt, 0x00, 0x00])
|
||||
hdr = (
|
||||
bytes([fc or (0x48 if short else 0x58), version])
|
||||
+ struct.pack("<H", pid)
|
||||
+ bytes([cnt])
|
||||
)
|
||||
if not short:
|
||||
hdr += mac[::-1]
|
||||
nonce = mac[::-1] + hdr[2:5] + ext
|
||||
ct = AESCCM(key, tag_length=4).encrypt(nonce, obj, b"\x11")
|
||||
body, tag = ct[:-4], ct[-4:]
|
||||
if corrupt:
|
||||
tag = bytes([tag[0] ^ 0xFF]) + tag[1:]
|
||||
return hdr + body + ext + tag
|
||||
|
||||
|
||||
def o(t, data):
|
||||
"""One MiBeacon object: type, length, data."""
|
||||
return struct.pack("<H", t) + bytes([len(data)]) + data
|
||||
|
||||
|
||||
def t16(v):
|
||||
"""0x1004 temperature object."""
|
||||
return o(0x1004, struct.pack("<h", round(v * 10)))
|
||||
|
||||
|
||||
def th(t, h):
|
||||
"""0x100D temperature and humidity object."""
|
||||
return o(0x100D, struct.pack("<hh", round(t * 10), round(h * 10)))
|
||||
|
||||
|
||||
def c_rows(name, rows):
|
||||
"""A C byte table from (bytes, comment) rows of equal length."""
|
||||
out = [f"static const uint8_t {name}[{len(rows)}][{len(rows[0][0])}] = {{"]
|
||||
out += [
|
||||
" {" + ",".join(f"0x{b:02X}" for b in row) + "}," + (f" // {c}" if c else "")
|
||||
for row, c in rows
|
||||
]
|
||||
return "\n".join([*out, "};", ""])
|
||||
|
||||
|
||||
# Fills a scan result with flags plus one service data field (16 bit uuid) and hands it to the tracker.
|
||||
XB_FILL = """
|
||||
inline void xb_fill(esphome::esp32_ble::BLEScanResult &r, const uint8_t *mac, uint16_t uuid, const uint8_t *data,
|
||||
uint8_t len) {
|
||||
memcpy(r.bda, mac, 6);
|
||||
r.ble_addr_type = 0;
|
||||
r.rssi = -50;
|
||||
uint8_t n = 0;
|
||||
r.ble_adv[n++] = 2;
|
||||
r.ble_adv[n++] = 0x01;
|
||||
r.ble_adv[n++] = 0x06;
|
||||
r.ble_adv[n++] = len + 3;
|
||||
r.ble_adv[n++] = 0x16;
|
||||
r.ble_adv[n++] = uuid & 0xFF;
|
||||
r.ble_adv[n++] = uuid >> 8;
|
||||
memcpy(r.ble_adv + n, data, len);
|
||||
r.adv_data_len = n + len;
|
||||
r.scan_rsp_len = 0;
|
||||
r.search_evt = ESP_GAP_SEARCH_INQ_RES_EVT;
|
||||
}
|
||||
|
||||
inline void xb_inject_one(const uint8_t *mac, uint16_t uuid, const uint8_t *data, uint8_t len) {
|
||||
esphome::esp32_ble::BLEScanResult r{};
|
||||
xb_fill(r, mac, uuid, data, len);
|
||||
esphome::esp32_ble_tracker::global_esp32_ble_tracker->gap_scan_event_handler(r);
|
||||
}
|
||||
"""
|
||||
@@ -0,0 +1,20 @@
|
||||
#!/bin/bash
|
||||
# run_all.sh TREE NAME PORT: make run_NAME.yaml (xball.yaml + xball_entities.yaml), build it from TREE, flash over USB,
|
||||
# capture 75 s from a reset and count the publishes. Run python gen_all.py first.
|
||||
# env: BUILD_LOCK (optional), PY (default TREE's venv python)
|
||||
D=$(cd "$(dirname "$0")" && pwd); T=${1:?tree}; N=${2:?name}; P=${3:?port}
|
||||
export PY=${PY:-$T/venv/bin/python}
|
||||
SC=$(dirname "$D")/serial_capture.py; B=$(dirname "$D")/pio_build.sh
|
||||
{ sed "s/^ name: xball$/ name: $N/" "$D/xball.yaml"; cat "$D/xball_entities.yaml"; } > "$D/run_$N.yaml"
|
||||
TREE=$T bash $B compile "$N" "$D/run_$N.yaml" || exit 1
|
||||
pkill -f "[s]erial_capture.py $P "
|
||||
TREE=$T bash $B upload "$N" "$D/run_$N.yaml" "$P" || exit 1
|
||||
sleep 3
|
||||
$PY "$SC" "$P" 75 "$D/$N-cap.txt" --reset > /dev/null 2>&1
|
||||
COUNT=$($PY "$D/count_all.py" "$D/$N-cap.txt"); RC=$?
|
||||
echo "$COUNT" | tail -2
|
||||
grep -a "IDLE" "$D/$N-cap.txt"
|
||||
CRASH=$(grep -a -c 'Guru\|abort()\|Backtrace' "$D/$N-cap.txt")
|
||||
echo "crash lines: $CRASH"
|
||||
[ "$CRASH" -eq 0 ] || RC=1
|
||||
exit $RC
|
||||
@@ -0,0 +1,45 @@
|
||||
# All Xiaomi BLE platforms on one ESP32-S3 (ESP-IDF), frames from gen_all.py injected into the tracker.
|
||||
# Build: python gen_all.py, then esphome -s name <name> compile xball.yaml. No network: logs on USB_SERIAL_JTAG.
|
||||
substitutions:
|
||||
name: xball
|
||||
|
||||
esphome:
|
||||
name: ${name}
|
||||
includes:
|
||||
- frames_all.hpp
|
||||
|
||||
esp32:
|
||||
board: esp32-s3-devkitc-1
|
||||
framework:
|
||||
type: esp-idf
|
||||
|
||||
logger:
|
||||
level: DEBUG
|
||||
hardware_uart: USB_SERIAL_JTAG
|
||||
|
||||
esp32_ble_tracker:
|
||||
|
||||
globals:
|
||||
- id: xa_cycle
|
||||
type: int
|
||||
initial_value: "0"
|
||||
|
||||
interval:
|
||||
- interval: 1s
|
||||
startup_delay: 20s
|
||||
then:
|
||||
- lambda: |-
|
||||
int c = id(xa_cycle);
|
||||
if (c == 0)
|
||||
ESP_LOGI("heap", "IDLE before inject: free %u internal %u min %u largest %u", (unsigned) esp_get_free_heap_size(),
|
||||
(unsigned) heap_caps_get_free_size(MALLOC_CAP_INTERNAL), (unsigned) esp_get_minimum_free_heap_size(),
|
||||
(unsigned) heap_caps_get_largest_free_block(MALLOC_CAP_INTERNAL));
|
||||
if ((size_t) c * XA_PER_CYCLE < XA_COUNT) {
|
||||
ESP_LOGI("xa", "inject cycle %d", c + 1);
|
||||
xa_inject(c * XA_PER_CYCLE, (c + 1) * XA_PER_CYCLE);
|
||||
} else if ((size_t) c * XA_PER_CYCLE == XA_COUNT + 10 * XA_PER_CYCLE) {
|
||||
ESP_LOGI("xa", "INJECT DONE");
|
||||
ESP_LOGI("heap", "IDLE after inject: free %u internal %u min %u", (unsigned) esp_get_free_heap_size(),
|
||||
(unsigned) heap_caps_get_free_size(MALLOC_CAP_INTERNAL), (unsigned) esp_get_minimum_free_heap_size());
|
||||
}
|
||||
id(xa_cycle)++;
|
||||
@@ -0,0 +1,24 @@
|
||||
esphome:
|
||||
name: xbsp
|
||||
esp32:
|
||||
board: esp32dev
|
||||
framework:
|
||||
type: esp-idf
|
||||
logger:
|
||||
esp32_ble_tracker:
|
||||
sensor:
|
||||
- platform: xiaomi_lywsd03mmc
|
||||
mac_address: A4:C1:38:00:20:01
|
||||
bindkey: 6c0d1a3e5b7f29c4a18e90d2f3b46a57
|
||||
setup_priority: 600
|
||||
temperature:
|
||||
name: t
|
||||
binary_sensor:
|
||||
- platform: xiaomi_wx08zm
|
||||
mac_address: A4:C1:38:00:20:02
|
||||
name: w
|
||||
setup_priority: 600
|
||||
- platform: xiaomi_mue4094rt
|
||||
mac_address: A4:C1:38:00:20:03
|
||||
name: m
|
||||
setup_priority: 600
|
||||
Reference in new issue
Block a user