Compare commits

..
Author SHA1 Message Date
J. Nick Koston e5c151db47 [hardware-test] Capture the board log at DEBUG in apiwatch.py so the CONN and ETRX markers arrive 2026-11-17 03:31:08 -06:00
J. Nick Koston 18646fbd23 [hardware-test] Check the board's own log in run_pair.sh, captured over the API in ESP32 mode 2026-11-17 02:59:51 -06:00
J. Nick Koston b8e91aa192 [hardware-test] Give udpraw.py probes their own magic and tighten encheck, ccdrive and tcp_peer checks on saved logs 2026-11-17 02:28:46 -06:00
J. Nick Koston 7d7bb65bd2 [hardware-test] Check every capture, job and log in the radio and link runners and put crashes in the exit status 2026-11-17 01:56:22 -06:00
J. Nick Koston be34455f20 [hardware-test] Fail ccdrive.py cases whose sender reported errors or never confirmed the send 2026-11-17 01:22:09 -06:00
J. Nick Koston 01ed7b02f6 [hardware-test] Count listen before talk misses in sxlink and fail ccdrive.py cases below 80 % good packets 2026-11-17 00:50:54 -06:00
J. Nick Koston 9d073ca7f4 [hardware-test] Fail an accepted sxlink raw send unless the receiver got exactly that packet 2026-11-17 00:18:58 -06:00
J. Nick Koston 8696497f7a [hardware-test] Count only an accepted then closed connection as a tcp rejection
The tcp link's allowed_ips check accepts the connection, logs "Rejected <address>" and drops
the socket, so a rejection is a connect that succeeds and then hits EOF or a reset before any
data. A refused connect means nothing is listening, so it now fails.
2026-11-16 23:45:55 -06:00
J. Nick Koston decbcb7972 [hardware-test] Pass the tcp_peer close checks only on a real remote EOF or reset 2026-11-16 23:15:13 -06:00
J. Nick Koston f56691d789 [hardware-test] Make the refused-source tcp step fail when the peer keeps the connection 2026-11-16 23:13:49 -06:00
J. Nick Koston c4038cd487 [hardware-test] Exit non-zero from tcp_peer.py, ccdrive.py and compare_spi.py on failures, and count sim failures in DONE 2026-11-16 22:45:57 -06:00
J. Nick Koston f5507f602b [hardware-test] Fail the radio and link runners on missing config lines, background job failures and FIFO overruns 2026-11-16 22:15:14 -06:00
J. Nick Koston 80a4a2a254 [hardware-test] Fail pairrun.sh on a dead capture and compare_spi.py on incomplete cases 2026-11-16 21:48:15 -06:00
J. Nick Koston 6fb227799e [hardware-test] Fail the radio and link runners on missing input, refused actions and lost packets 2026-11-16 21:46:53 -06:00
J. Nick Koston b8bc47c74f [hardware-test] Share the SX127x pin layer and simplify the radio and link runners
- spibits.hpp holds the fake SPI pins and the chip interface for sxsim, sxair and sxrec;
  the register models stay separate. Same STATE, TRACE and REC output on an ESP32.
- rec.yaml with two wrappers, compare_spi.py reads device logs directly, one radio template
  in gen_pair.py, dead code removed from sxair and sxlink.
- entest.hpp covers the UDP streams; pairrun.sh and run_pair.sh replace the per-flow runners;
  validation cases write into out-valid/ with ok_ and bad_ names.
- cc1101: flash by USB serial number (ports.py has no station MAC), apiinfo.py replaces
  devinfo.py, ccdrive.py uses log_text and documents every mode.
2026-11-16 21:38:43 -06:00
J. Nick Koston 17b0ea8e9b [hardware-test] Keep the radio and link flows here; the bus, IR and sensor, and network flows move to their own PRs 2026-11-16 21:10:13 -06:00
J. Nick Koston e9ce3ce0e6 [hardware-test] Share helpers in the sync 18 flows and fix the first review round
- The API drivers connect through apiconn.connect() with API_KEY from apikey.txt, like the
  existing drivers.
- One rbtest component and one press.py for the remote_base host harness and dumper corpus,
  one rbhost.yaml with a log level substitution, one whirlpool climate file per model.
- snf.hpp covers the in-firmware target with -DSNF_PDSLAVE, so snfpd.hpp is gone.
- scaninst.yaml includes scan.yaml; the 60 flooded sensors of the API overflow test come from
  gen_sensors.py and one loop.
- nPM1300 charger facts moved from boards.md to the npm1300 recipe.
- Review fixes: climate frame comparers fail on unequal step or frame counts, the NACK
  classifiers keep master NACKs on reads as differences, consist.py counts a missing dump,
  host runners resolve relative paths, the BMI270 runner restores the saved driver from a trap,
  otarun.sh sends the UF2 on LibreTiny, capture stops match the whole port name.
2026-11-16 21:08:08 -06:00
J. Nick Koston fcc177acbf [hardware-test] Add simulated radios and buses, link layer, climate IR, Wi-Fi outage and DNS loss flows
New flows from recent bench runs: the SX127x and SX126x simulators, I2C wire A/B, climate IR
loopback and decoder fuzz, Pronto and dumper A/B, ESP-NOW, UDP and TCP packet_interface links,
the Tuya MCU and serial_proxy simulators, CC1101 long packets, the router outage and Disassociation
sniffer rigs, Bluedroid scan instrumentation, the API overflow counters, the ESPAsyncWebServer bump
flows, the network.priority DNS loss harness, the WK2168 and BMI270 fakes, and new board facts.
heap_churn.py now reads each 401 to EOF so the device closes first.
2026-11-16 18:19:00 -06:00
319 changed files with 8519 additions and 4967 deletions

No files matched your search

+1 -1
View File
@@ -158,7 +158,7 @@ recorder, dump saver and decoder, cover cycle driver), `online_image/`
(host driver, Zephyr driver probe), `rtsp_server/` (protocol client, raw rate reader, measurement set,
API image grab), `cse7766/` (API observer with ping), `zephyr_coredump/` (reader flow, decode with the
Zephyr tools), `libretiny_prefs/` (rtc preference harness header), `libretiny_crash/` (BK72xx crash probe header,
capture decoder, RTL8720C ROM dump versus report matcher), `api_outgoing/` (wake cycle and Wi-Fi drop timing readers), `ld2450/` (zone number driver, loopback harness driver with number and binary sensor states and TX frame summarizer, frame stream harness with presence hold and link loss drivers, BT MAC driver), `api/` (raw plaintext native API client for user defined actions, store_yaml recovery client over Noise or plaintext), `http_request/` (update manifest server, update state driver, request test server, action case driver, concurrency, reboot, update check and OTA drivers, `ota_auth/` OTA request cases with basic auth), `modem/` (button and sensor driver over the plaintext API, also used for the BLE MIDI boards, AT commands for the UART passthrough), `dfrobot_sen0395/` (API driver and case runner), `homeassistant_light/` (direct bulb driver, held-report case driver), `usb_audio/` (URL player), `speaker_media_player/` (API player driver, pipeline task case runner), `web_server/` (SSE client, request mix, SSE and HTTPD case runners, per-session `/events` listing check with its config, `/events` head, multi-client, stall, Wi-Fi drop and churn modes with their config, head comparer and runner), `mqtt/` (`ha_discovery/`, `disable/`, `retained_burst/` and `reconnect/` runners; broker config with TLS, client-certificate and WebSocket listeners, test CA with server and client certificates, transport case runner, flash-and-drive runner, CLI TLS check without a board, MAC suffix runner with per-variant configs for two boards, `shutdown/` shutdown message runner and matcher), `udp/` (datagram recorder, `udp.write` payload checker and case driver), `entity_icons/` (icon and device class readback over the API, MQTT discovery, web_server and dump_config, with a comparer), `uart_sim/` (API driver and flash, capture and drive runner for the simulated UART sensors), `remote_base/` (loopback runner and API log decoder per sent frame), `xiaomi_ble/` (frame and header generator with the expected counts, injecting, advertising and receiving configs, counter and runner), `ble_terminate/` (proxy and peer configs, connect and disconnect driver, peer disconnect matcher, runner), `i2c_slave/` (D1 mini slave and BK7238 master configs), `wpa3_ap/` (PMF AP with join and leave logging, BK7238 and CB3S DUTs, runner and summary), `wisp_ap/` (WISP upstream with AP buttons, WISP and ALWAYS_ON DUTs, client, runner and per phase summary), `wifi_restart/` (WPA3 mixed WISP upstream, static IP DUTs with syslog, runner and summary), `ota_timing/` (OTA upload timer with deflate, raw and TCP modes, API build check, A/B runner and median summary, `inflate_bench/` on-device inflate benchmark with stream generator, configs and syslog runner), `gpio_hold/` (ISR pin hold self test with ESP32 and S3 configs), `ble_event_inject/` (GAP and GATTC event injector header and proxy package), `light_sleep_wake/` (LEDC wave and edge timer for light sleep wakeup on a C3, external wave generator and per-call edge detail probe for peripheral power-down on the P4), `manual_ip/` (station and AP manual IP configs, DNS probe and runners), `web_handlers/` (late handler, web_server, captive_portal and prometheus variants, web OTA runner), `bk72xx_flash/` (flash readback with retries, per partition diff of two dumps), `ble_server_security/` (secure GATT server and pairing client configs, driver, summary and runner), `prometheus/` (datetime metrics configs and driver), `zephyr_ble_server/` (nRF52 server, looping ESP32 client and UF2 flash and count runner), `ble_adv_triggers/` (advertiser with six raw frames, ESP32 and BK72xx receivers with eleven triggers, runner and checker), `sun/` (longitude sweep package, runner and checker), `sensor_filters/` (every filter shape with a fixed input sequence, per-board packages and runner), `led_driver_sniff/` (SPI slave capture header with selftest, config generator, window comparer and runner), `hub_input_sim/` (keypad, wiegand and rdm6300 simulation on a D1 mini, API log runner), `value_range/` (on_value_range sequence with a restart, runner), `ezo_fake_bus/` (fake EZO I2C bus and step config, runner), `twai_self/` (TWAI self-reception header, on_frame config and counter runner), `ota_automations/` (OTA automation package for both OTA platforms, four case runner, repeated web upload runner and per case summary), `output_button/` (pad edge timer header, button package and runner), `homeassistant_button/` (raw plaintext API client for action calls and wizard inputs, package and runner), `uart_button/` (UART0 loopback package, API driver with TX and RX counts, runner), `interval/` (plain and delayed interval package, timing summary and runner), `smp_udp_ota/` (SMP reset fault injector, OTA runner, per mode result check, MCUboot swap limit, running image check and slot blanking UF2, Thread device config), `ld2420_sim/` (fake LD2420 answering the hub's commands on a UART0 loopback, API state recorder, two-build comparer and runneLine truncated
capture decoder, RTL8720C ROM dump versus report matcher), `api_outgoing/` (wake cycle and Wi-Fi drop timing readers), `ld2450/` (zone number driver, loopback harness driver with number and binary sensor states and TX frame summarizer, frame stream harness with presence hold and link loss drivers, BT MAC driver), `api/` (raw plaintext native API client for user defined actions, store_yaml recovery client over Noise or plaintext), `http_request/` (update manifest server, update state driver, request test server, action case driver, concurrency, reboot, update check and OTA drivers, `ota_auth/` OTA request cases with basic auth), `modem/` (button and sensor driver over the plaintext API, also used for the BLE MIDI boards, AT commands for the UART passthrough), `dfrobot_sen0395/` (API driver and case runner), `homeassistant_light/` (direct bulb driver, held-report case driver), `usb_audio/` (URL player), `speaker_media_player/` (API player driver, pipeline task case runner), `web_server/` (SSE client, request mix, SSE and HTTPD case runners, per-session `/events` listing check with its config, `/events` head, multi-client, stall, Wi-Fi drop and churn modes with their config, head comparer and runner), `mqtt/` (`ha_discovery/`, `disable/`, `retained_burst/` and `reconnect/` runners; broker config with TLS, client-certificate and WebSocket listeners, test CA with server and client certificates, transport case runner, flash-and-drive runner, CLI TLS check without a board, MAC suffix runner with per-variant configs for two boards, `shutdown/` shutdown message runner and matcher), `udp/` (datagram recorder, `udp.write` payload checker and case driver), `entity_icons/` (icon and device class readback over the API, MQTT discovery, web_server and dump_config, with a comparer), `uart_sim/` (API driver and flash, capture and drive runner for the simulated UART sensors), `remote_base/` (loopback runner and API log decoder per sent frame), `xiaomi_ble/` (frame and header generator with the expected counts, injecting, advertising and receiving configs, counter and runner), `ble_terminate/` (proxy and peer configs, connect and disconnect driver, peer disconnect matcher, runner), `i2c_slave/` (D1 mini slave and BK7238 master configs), `wpa3_ap/` (PMF AP with join and leave logging, BK7238 and CB3S DUTs, runner and summary), `wisp_ap/` (WISP upstream with AP buttons, WISP and ALWAYS_ON DUTs, client, runner and per phase summary), `wifi_restart/` (WPA3 mixed WISP upstream, static IP DUTs with syslog, runner and summary), `ota_timing/` (OTA upload timer with deflate, raw and TCP modes, API build check, A/B runner and median summary, `inflate_bench/` on-device inflate benchmark with stream generator, configs and syslog runner), `gpio_hold/` (ISR pin hold self test with ESP32 and S3 configs), `ble_event_inject/` (GAP and GATTC event injector header and proxy package), `light_sleep_wake/` (LEDC wave and edge timer for light sleep wakeup on a C3, external wave generator and per-call edge detail probe for peripheral power-down on the P4), `manual_ip/` (station and AP manual IP configs, DNS probe and runners), `web_handlers/` (late handler, web_server, captive_portal and prometheus variants, web OTA runner), `bk72xx_flash/` (flash readback with retries, per partition diff of two dumps), `ble_server_security/` (secure GATT server and pairing client configs, driver, summary and runner), `prometheus/` (datetime metrics configs and driver), `zephyr_ble_server/` (nRF52 server, looping ESP32 client and UF2 flash and count runner), `ble_adv_triggers/` (advertiser with six raw frames, ESP32 and BK72xx receivers with eleven triggers, runner and checker), `sun/` (longitude sweep package, runner and checker), `sensor_filters/` (every filter shape with a fixed input sequence, per-board packages and runner), `led_driver_sniff/` (SPI slave capture header with selftest, config generator, window comparer and runner), `hub_input_sim/` (keypad, wiegand and rdm6300 simulation on a D1 mini, API log runner), `value_range/` (on_value_range sequence with a restart, runner), `ezo_fake_bus/` (fake EZO I2C bus and step config, runner), `twai_self/` (TWAI self-reception header, on_frame config and counter runner), `ota_automations/` (OTA automation package for both OTA platforms, four case runner, repeated web upload runner and per case summary), `output_button/` (pad edge timer header, button package and runner), `homeassistant_button/` (raw plaintext API client for action calls and wizard inputs, package and runner), `uart_button/` (UART0 loopback package, API driver with TX and RX counts, runner), `interval/` (plain and delayed interval package, timing summary and runner), `smp_udp_ota/` (SMP reset fault injector, OTA runner, per mode result check, MCUboot swap limit, running image check and slot blanking UF2, Thread device config), `ld2420_sim/` (fake LD2420 answering the hub's commands on a UART0 loopback, API state recorder, two-build comparer and runneLine truncated
## Templates in `$SKILL/templates`
@@ -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
- [`tcp_link.md`](tcp_link.md): tcp packet_interface link, host to host pair on one computer, raw framing peer, entity_transport both ways
@@ -15,3 +15,39 @@ ends with `DONE fails=N`, and T6 prints whether each setter woke a sleeping chip
and the chip itself change them.
- Results: on 7e6b90d08b, `set_frequency`, `set_channel` and `set_modulation_type` woke a sleeping chip and left it
in RX. 9e65d0c327 fixed that; every other check passed on both, and 200 sleep and wake cycles took about 1 s.
## Long packets and lambda length, two radios (#17578)
`scripts/cc1101_long/`: `cccommon.yaml` (package, API actions `cfg`, `send`, `rate`, `pdown`, `wup`, `setlen`,
`pmode`, `idle_rx`, `tx_rx`, `hf` and more; logs every packet as `RX len= b0= seq= ok= rssi= lqi= fo=`), `cclong.hpp`
(raw register helpers and `long_tx()`, which streams packets longer than the TX FIFO per datasheet 15.2.2, since the
component only sends up to 64 bytes), example board files `c3-long.yaml` / `s3-long.yaml` / `c3-lambda.yaml` (plen 32 =
plain, 1024 = long, lambda `x[0] + 1` with 0xEE meaning -1), `ccdrive.py TXHOST RXHOST MODE OUT TXLABEL RXLABEL`
(modes `short`, `long`, `r2long`, `r2mid`, `lambda`, `r2lambda`, `fast`, plus the dev bug repros `crc`, `rxtx`,
`txback`, `txrx` and `txset` (begin_tx then begin_rx, with setters between them, #22636) and `nocrc64` (100 packets of
64 bytes with and without CRC, #22635)), `flashmac.sh NAME BOARD` (BOARD is a port or the USB serial number from
`ports.py`; flashes, then waits for the API with `ota_timing/apiinfo.py`), `buildall.sh NAME...`, `runall.sh A B`
(both roles, boards as for flashmac.sh). Needs `secrets.yaml` with `api_key` and an `apikey.txt` file with the same key next
to the configs.
- Two boards with an E07-M1101D each, 433.92 MHz GFSK 4800. Ports change after a replug; flash by USB serial number.
- Both radios on the PR: one plain, one long and one lambda build per board; the sender runs the long build (raw TX
does not care). A full both-roles run is about 45 min including builds.
- Order matters: `tx_rx` (begin_tx then begin_rx) kills reception until reflash or `set_packet_mode(true)` on dev
(#22636), so run `long` last on a build and keep it out of `lambda`; `r2lambda` sets a fixed length, which drops
the lambda, so run it after `lambda`. Fixed 64 with CRC is 0/20 on dev too (#22635).
- `ccdrive.py` waits for the RX and TX `CFG` log line before sending; without that a slow API reply leaves the
receiver on the previous length and shows "wrong length" packets.
A result line is marked INVALID when an action failed or a `CFG`, `RATE` or `SENT` line never came since the
previous result, and shows the sender's `txerr` from its `SENT` line, so send failures are not read as lost packets.
A case is MISSED, and ccdrive.py exits non-zero, when the sender reports send errors (or no SENT line), when fewer
than 80 % of its packets arrive good, or when any arrives for the cases that expect none (asleep, lambda discard);
the dev bug repros, 64 bytes with CRC (#22635) and the receiver after begin_tx plus begin_rx (#22636) are reported
but not judged.
- High rate (`fast`): the `rate` action sets modulation, symbol rate, deviation, filter and sync at run time on both
radios, and `long_tx` polls at 20 to 50 us. Use sync 30/32: with 16/16 at 100 kBaud noise syncs about once a second
and collides with real packets. At 100 kBaud a stable RXBYTES read alone overflowed at the first drain
(`received=0 expected=300 fifo=65`, 10/20 at 300 B on the C3) and the GDO0 drain got 20/20; 38.4k showed no
difference. The C3 shows the overflow more than the S3.
- A strong RSSI (about -29 dBm) with ok=0 is a real corruption; ok=0 at -90 dBm, LQI 127 is a noise sync.
- An API drop mid-run can be a Wi-Fi roam scan, not a restart (check serial); rerun.
@@ -5,3 +5,48 @@
The NONOS SDK only delivers broadcast ESP-NOW frames while the station is associated or a soft-AP is
up; a hidden, open, client-less soft-AP (60 s beacon, DHCP stopped) makes broadcasts arrive. In AP-only
mode the device transmits from the soft-AP MAC.
## ESP-NOW and UDP packet_interface links (#21120)
Files in `scripts/espnow_link/` (a D1 mini and a classic ESP32; copy each `*.example.yaml`, set `name`, the peer's name
and its MAC or address):
- `enlink.yaml` + `en32.example.yaml`/`en66.example.yaml`: ESP-NOW without Wi-Fi on a fixed channel. Links `p2p`
(peer_address, xxtea, hdlc, session, fragmentation), `bc` (broadcast, addressing with a reliable rolling code port
`bc_rel` and a broadcast port `bc_all`), `raw` (size sweep, sent size in the header) and `raw_small` (100 bytes,
oversize warning). `entity_transport` on three stacks (ETRX and SESS log lines) and checked 12/200/600 byte streams
on the reliable tops. `chaos: "true"` makes the board hop channel for 3 s and later 15 s (`espnow.set_channel`), a
clean way to lose frames without touching the other board.
- `entest.hpp`: `entest::Stream` checked stream (seq, sent size, pattern; counts ok/bad/gap/dup/trunc/foreign, `STAT`
line). A stream on the top of a stack that also carries `entity_transport` starts with 0x1E (version 1, unknown
type), so entity_transport drops it silently. The UDP only streams `P2PF` and `P2PT` are there too.
- `udplink.yaml` + `ud32.example.yaml`: six udp components (unicast peer, subnet broadcast, multicast with
`listen_address`, peer plus test host, `point_to_point: false`, `point_to_point: true` on the broadcast). Multicast
needs `listen_address: <group>` on the udp component or nothing is received. `udpraw.py SECS HOST...` sends a
datagram per second to ports 21121/21125/21126 of each board from the computer and counts what the boards send to it
on 21124 (exits 1 when none arrived). Its datagrams start with 0xA5 0x5D, no stream's magic, so the links that
accept third-party traffic count them as foreign. The `tick` and `stream_interval` substitutions set the load.
- `pairrun.sh PREFIX E32V D1V [SECS] [RUN]`: flashes `<PREFIX>32<E32V>.yaml` on the ESP32 and `<PREFIX>66<D1V>.yaml`
on the D1 mini (build letter p from `PR_TREE`, any other from `BASE_TREE`), captures both from a reset and prints a
summary per board for the prefix. It exits 1 when a capture fails, or a board logs no STAT line, reboots during the
capture or logs a crash.
- `urlink.yaml` (prefix `ur`): minimal unicast A/B (one raw link plus `on_receive`), PR against base.
- `rglink.yaml` (prefix `rg`): existing users (packet_transport over UDP multicast and ESP-NOW, udp.write,
espnow send and broadcast actions and triggers); remote entities for both peer names so one image serves four
pairings.
- `encheck.py LOG...`: per link counts, gaps and bad float values from ETRX lines, last STAT per stream, warning
histogram; exits 1 on a reboot or crash during a capture, a value never sent, a stream with bad payloads, an ETRX
line it cannot parse, no STAT line or no counter value on any link.
`validation_cases.py`: writes 41 ok_/bad_ configs into `out-valid/`; run `esphome config` on each.
Facts:
- ESP-NOW v2 frames above 250 bytes from an ESP32 (IDF 5.5) arrive on an ESP8266 cut to 250 bytes, not dropped.
- On the ESP8266 a peer link hears the peer's broadcasts (no destination reported), so with a chatty broadcaster it
logs "10 packets in a row could not be understood".
- The espnow send pool fills under a few hundred packets a minute; each refused send logs `[E] Failed to allocate
send packet from pool`.
- On a busy Wi-Fi network about half of the broadcasts between a D1 mini and an ESP32 can be lost, the D1 logs
`sendto() error 5` (lwIP ERR_IF) under load, and reliable UDP links on the broadcast address flap on dev too;
unicast is about 95 to 100 %. Keep UDP loads light (counter tick 5 s, streams every 2 s) or the test measures the
Wi-Fi. With Wi-Fi joined, `espnow.send` from the D1 to the ESP32 fails almost always (dev too).
- The subnet broadcast address guess assumes .255; on a wider subnet (a /21, say) check it really ends in .255.
@@ -86,3 +86,85 @@ same FakeBus approach works for any SPI chip whose reply depends on the address
ESP32 SPI slave cannot fake.
The header targets the SPI API of the #22188 branch (`SPIBus` and `SPIComponent::spi_bus_`); dev has since dropped
`SPIBus`, so on a newer tree the bus hook has to move to the new delegate creation before it builds.
## SX127x fake chip (#22733)
`scripts/sx127x_sim/`: `sxsim.hpp` is a fake SX1276, `sxrun.hpp` the scenario runner, `sxsim.yaml` the ESP32-PICO-D4
config (no Wi-Fi; `sxpr.yaml` and `sxbase.yaml` are two-line wrappers setting `substitutions: flags: -DSXSIM_PR` or
`-DSXSIM_BASE` and `packages: sim: !include sxsim.yaml`, since `-s flags -D...` is parsed as an option).
- Hook, no private access needed on the current SPI API (`SPIBus` is gone; `register_device()` builds the delegate in
the client's `spi_setup()`): the bus is `interface: software`, and at on_boot 800 `set_clk/set_mosi/set_miso` on the
bus and `set_cs_pin` on the radio swap in fake `GPIOPin`s, so the real `SPIDelegateBitBash` clocks every bit into
the model (the MISO read drives the bit engine, as the PR's own gtest helper does). Works on any driver revision.
The fake pins live in `spibits.hpp` (`PinSet` bound to a `SpiChip` with per byte reply and receive hooks, a lock
hook and an `after_end()` that runs outside the lock), shared by `sxsim.hpp`, `sxair.hpp` and `sxrec.hpp` and
buildable on the host; ESPHome copies only the files in `includes:`, so every config lists it.
`InternalGPIOPin` is a final platform class alias, so rst and DIO0 cannot be faked on old drivers; rst is a real
pad and a RegVersion read resets the model instead. DIO0 is GPIO4 with pulldown, switched to INPUT_OUTPUT at on_boot
399 and driven by the model, so the rising edge interrupt really fires.
- Send completion runs from a one-shot `esp_timer` (`M.tx_ms`), because a blocking `transmit_packet()` spins on the
DIO0 pad with no SPI traffic and no loop; model state is shared under a `portMUX`.
- Faults: `M.fail_mode_writes = N` (RegOpMode writes ignored, mode reads back old), `M.tx_never`,
`sxsim::arm_rx_on_standby({...})` (a packet completes just as a send writes standby), `M.version`, CRC error inject.
The chip ignores mode requests other than standby/sleep while a send is still going out (counter `ign_tx`).
- The FSK FIFO is one 64 byte queue for TX and RX, cleared on sleep and on entering RX, so a send that writes over a
waiting FSK packet transmits the received bytes (dev did that in #22733's test). LoRa TX leaves the chip in standby
with TX_DONE set; FSK stays in TX with PacketSent.
- `sxrun.hpp` boot cases use `RTC_NOINIT` plus `esp_reset_reason() == ESP_RST_SW` (a flash or EN reset starts at case
0): 0 LoRa and FSK sequence (traces of one blocking send per modulation as `TRACE` lines, receive, rx before send, raw
set_mode_tx, async OK/BUSY/INVALID/timeout/cancel, actions during a send, runtime mode failure with a 6 s watch),
1 blocking send that never completes, 2 wrong version, 3 mode failure at setup, 4 `ALLDONE`. One 75 s capture with
`--reset` covers all of it. `STATE` lines print the model counters.
- On #22733, dev lost a packet waiting at send start (LoRa) or sent it as its own (FSK), delivered a stale packet after
raw TX mode (LoRa) or two empty ones (FSK), and was marked failed after a blocking send timed out because RX is
refused while the chip is still in TX. The PR passed every case.
## Two radios and a simulated air (#21110)
`scripts/sx127x_sim/sxair.hpp` (models and air), `sxlink.hpp` (runner), `gen_pair.py OUTDIR` (writes `lpair` two SX127x
LoRa, `fpair` two SX127x FSK, `xpair` SX127x plus SX126x LoRa, each with a `<name>_glue.h`, and copies the shared
headers next to them). One ESP32-PICO-D4
firmware, two software SPI buses; at on_boot 1100 (before the buses' setup at 1000) the bus pins, CS, reset and SX126x
busy are swapped for fake pins (needs the `GPIOPin *` reset setter from #22733), so the only pads used are the interrupt
lines GPIO4 (A) and GPIO22 (B), switched to input/output at on_boot 300. Shared real pin numbers need
`allow_other_uses`.
- SX126x fake: command parser on CS rise (SetPacketType, SetModulationParams, SetPacketParams, SetDioIrqParams,
ClrIrqStatus, WriteBuffer, SetTx/SetRx/SetStandby/SetSleep), replies per byte position for ReadRegister (version
string at 0x0320), ReadBuffer, GetIrqStatus, GetRxBufferStatus, GetPacketStatus; BUSY is a fake pin that is always
low; IRQ bits latch only when in the IRQ mask, DIO1 = irq & dio1 mask.
- Air: airtime from the sender's own registers and commands (Semtech LoRa formula; FSK bytes at the bitrate), delivery
to every receiver that stayed in RX, RSSI and SNR per receiver, collisions give a CRC error, knobs `AIR.drop_next`,
`AIR.corrupt_next`, `jam(radio, ms or UINT32_MAX)`, `tx_never`. A receiver shows a signal while a packet is on air
(SX127x LoRa ModemStat, FSK IrqFlags1 sync match held until read, SX126x preamble/sync/header IRQs).
- Gotcha: both radios share one main loop, so while one radio's blocking send runs the other cannot read its FIFO
and the next packet overwrites the unread one (first runs lost 12 of 53 and no HDLC link came up). The model
holds a packet that lands while the previous one is unread and hands it over from the 1 ms air tick once the
driver has read the previous one and 3 ms passed with no SPI on that radio (draining at the flag clear corrupts
the read in progress). A held packet must clear the receiver's signal, or listen before talk sees a phantom busy
radio. Counters `held` say how often it happened; say in results that this is a simulator artifact.
- Raw links share the radios with the layered links, so a raw send can be refused by listen before talk while the
layered links talk; retry. Raw test packets start with 0xEE and carry a length-derived pattern.
- `tx_over_signal` counts sends started while the sender showed a signal: 0 outside the hold cap phase means listen
before talk never sent over a packet.
- The run ends with `DONE fails=N`: values that never arrived, refused sends of valid sizes, accepted oversize sends, a
refused peer send in the listen before talk phase, held sends never released, no release in the hold cap phase and
raw packets with a bad pattern or that did not arrive exactly once at their size. `AIR` lines also count FSK FIFO
writes dropped when full and reads when empty (`fifo_over`, `fifo_under`).
## SX127x SPI byte stream A/B (#22776)
- Host: copy `scripts/sx127x_sim/sx127x_spi_record_test.cpp.example` as a `.cpp` into `tests/components/sx127x/` of
each tree and run `SX127X_RECORD=<file> script/cpp_unit_test.py sx127x` (the env var reaches the test binary). It
sweeps FSK and LoRa frequency, bitrate, deviation and preamble edges through setup, configure() and 1 byte and
largest sends against a register file with burst auto-increment (FIFO address fixed), and writes CASE/T/REGS/FIFO
records. Case labels must be unique (an index is prefixed) or the compare silently drops cases. `compare_spi.py BASE
PR` checks register files and FIFO exactly and compares the transactions flattened into per-register operations, so
only grouping may differ.
- Device: `sxrec.hpp` with `recsw.yaml` (software bus, fake pins at on_boot 1100) or `rechw.yaml` (ESP-IDF hardware bus
`interface: spi2`), two short wrappers setting the substitutions of `rec.yaml`: a tee delegate robbed into `SPIClient::delegate_` at on_boot 800 sends every byte through the
real SPIDelegateHw and feeds the model; the radio's own spi_setup() then logs "Device already registered" once and
keeps the tee. SPI3 as a slave sniffer (CLK 18, MOSI 23, CS 5 routed in, nominal CS GPIO14 high) captures each chip
select window up to 64 bytes and compares it with what the driver sent ("WIRE ok/differ" line). Records print as
"REC ..." lines; compare_spi.py reads the two device logs directly (it cuts each line at "REC " and joins the
"REC +" continuation lines) and fails a log whose WIRE line counts differing or uncaptured windows. The host
harness keeps its own register file and pins, so the copy step stays one file.
@@ -0,0 +1,52 @@
# tcp packet_interface link (#21119)
The `tcp` link carries `packet_interface` packets over one TCP connection: 2 byte little endian length, then the
packet; a zero length packet is a keep-alive. Most of it can be tested with no board: the component works on `host:`,
so two host builds on one computer talk over 127.0.0.1, and a raw Python peer covers what ESPHome cannot do on purpose.
Files in `scripts/tcp_link/`:
- `tcplink.yaml` (package): per device an `et_srv` and `et_cli` link carrying `entity_transport` both ways over xxtea
plus fragmentation (nine 191 char broadcast ids so the dictionary is larger than one 1022 byte TCP frame), a
`raw_srv`/`raw_cli` pair with no layers running a size sweep up to 1022 with checked payloads and sequence numbers
(`STAT` line every 10 s: tx, refused, rx, bad, gap), a `probe_srv` (with `allowed_ips`) and a `probe_cli` for the
Python peer, `CONN <link> 0/1` from every `connected` sensor, `ETRX` lines for remote states, and a 1023 byte send
20 s after boot (must be refused). API on `${api_port}` with an encryption key from secrets.
- `host-peer.example.yaml`, `esp32-peer.example.yaml`: the per device substitutions (ports, peer host, allow list).
The ESP32 one adds `deny_srv` with an allow list that excludes the test computer, for the refused connect.
- `tcp_peer.py client HOST PORT | server PORT`: raw peer. `--silent-after S` stops sending (receive timeout and
reconnect), `--oversize-after S N` sends a header of N bytes (must close), `--send 1,1022` sends checked sizes,
`--bind ADDR` picks the source address, `--hold S` closes after S. Prints keep-alive gap min/max per connection.
- `apiwatch.py HOST PORT SECS [LOGFILE]` (`API_KEY` from `apikey.txt`): state changes with timestamps from connect;
shows the `connected` sensors read False (not unknown) at boot, and writes the device's log lines at DEBUG and above
(the level of the `CONN` and `ETRX` markers) to LOGFILE. The API coalesces a fast off/on, so judge edges from the
`CONN` log lines.
- `run_pair.sh host|esp32 [RUN]`: the timeline (boot states, SIGSTOP/SIGCONT of the host peer, kill -9 and restart,
silent and oversize raw servers, one client at a time, a refused client, oversize from a client) against host build
`tcph1` (peer `tcph2`, refused client from the LAN address; `LAN` overrides the macOS `ipconfig` guess) or the ESP32
`tcpe1` (`NAME` overrides it, found with `resolve.sh`; peer `tcph3`, refused client on `deny_srv`). Build the host
configs here first. Ports default to the example configs (`API_PORT`, `PROBE_PORT`, `PROBE_CLI_PORT`, `DENY_PORT`).
Logs: `<RUN>-dut.txt` (the host build's output, or the board's log over the API in esp32 mode), `-peer-a.txt` and
`-peer-b.txt` (before and after the kill), `-api.txt`, the raw peers and `-events.txt`, without ANSI colours. It
exits 1 when a job ends with the wrong status (`apiwatch.py` fails on a dropped API connection, `tcp_peer.py` on no
connection or a missed close), any device log (the device under test's included) shows bad raw packets or an
accepted 1023 byte send, or a size the raw clients sent never reached the device's `probe_srv` with its sum.
- `validation_cases.py`: writes one config per validation rule into `out-valid/`, named ok_ or bad_ by the expected
outcome; run `esphome config` on each from the tree under test.
Facts:
- Source address trick: a server with `allowed_ips: 127.0.0.0/8` accepts `127.0.0.1` and rejects a connect to the
computer's own LAN address with `--bind <LAN address>` (`Rejected <address>` in the log). No second
machine needed.
- A server accepts one client; the kernel completes a second client's handshake (backlog 1), the second gets nothing
until the first closes, then it is accepted and its queued packets are delivered.
- SIGSTOP of a host peer: the other side closes after `timeout`, the client reconnects after `reconnect_interval` and
connects to the stopped process's backlog, then times out again. kill -9 closes at once (EOF/RST).
- One packet accepted by the socket just before a drop can be lost (the raw sweep shows one sequence gap per
direction per drop); the session goes down and `entity_transport` resends everything on reconnect, so states are
not lost.
- Host builds bind every interface; give each host build its own API port and link ports (6053 may be taken).
- ESP32 pair (`run_pair.sh esp32`): the host peer reaches the board by its mDNS name; set `HOST_LAN_IP` in the ESP32 example to the computer's address.
Over Wi-Fi with `power_save_mode: none`, the raw peer saw keep-alive gaps of 1.65 to 2.26 s. A hardware reset (`esptool --after hard_reset chip_id` with the capture stopped) sends no FIN, so the host closes
on its own receive timeout and then reconnects.
@@ -37,3 +37,8 @@ npm1300/*-cap.err
npm1300/*-run.log
rp2_sched_lock/*.txt
rp2_sched_lock/*.log
cc1101_long/*.log
espnow_link/*.log
espnow_link/*.err
tcp_link/*.txt
sx127x_sim/*pair/
@@ -0,0 +1,13 @@
#!/bin/bash
# buildall.sh NAME...: compile each NAME.yaml in this directory from TREE and show whether the long packet define and
# the GDO0 drain made it into the build. env: TREE, PY, BUILD_LOCK (optional)
D=$(cd "$(dirname "$0")" && pwd); S=$(dirname "$D")
export PIO_CORE=${PIO_CORE:-$D/pio}
FAILED=0
for n in "$@"; do
echo "== $n $(git -C "${TREE:?set TREE}" rev-parse --short=10 HEAD)"
bash "$S/pio_build.sh" compile "$n" "$D/$n.yaml" || { echo "BUILD FAILED $n"; FAILED=$((FAILED + 1)); continue; }
echo "long define: $(grep -c USE_CC1101_LONG_PACKET "$D/.esphome/build/$n/src/esphome/core/defines.h")" \
"gdo0 drain: $(grep -c RX_FIFO_THRESHOLD "$D/.esphome/build/$n/src/esphome/components/cc1101/cc1101.cpp")"
done
[ "$FAILED" = 0 ] || { echo "$FAILED builds failed"; exit 1; }
@@ -0,0 +1,20 @@
# Example wiring as in c3-long.yaml; packet length from a lambda, 0xEE in the first byte means -1 (drop).
substitutions:
name: cc-c3-lambda
clk: GPIO6
mosi: GPIO7
miso: GPIO10
cs: GPIO3
gdo0: GPIO4
plen: "1"
packages:
c: !include cccommon.yaml
esp32:
variant: esp32c3
framework:
type: esp-idf
cc1101:
packet_length: !lambda |-
if (x.size() < 1) return 0;
if (x[0] == 0xEE) return -1;
return x[0] + 1;
@@ -0,0 +1,16 @@
# Example wiring for an E07-M1101D (CC1101) module; change the pins to match your board. plen 1024 = long packets,
# 32 = plain fixed length (copy the file and set plen and name for a plain build).
substitutions:
name: cc-c3-long
clk: GPIO6
mosi: GPIO7
miso: GPIO10
cs: GPIO3
gdo0: GPIO4
plen: "1024"
packages:
c: !include cccommon.yaml
esp32:
variant: esp32c3
framework:
type: esp-idf
@@ -0,0 +1,135 @@
esphome:
name: ${name}
includes: [cclong.hpp]
logger:
hardware_uart: USB_SERIAL_JTAG
wifi:
ssid: !secret wifi_ssid
password: !secret wifi_password
power_save_mode: none
spi:
clk_pin: ${clk}
mosi_pin: ${mosi}
miso_pin: ${miso}
globals:
- {id: seq, type: uint8_t, initial_value: "0"}
- {id: txerr, type: uint32_t, initial_value: "0"}
- {id: txpoll, type: uint32_t, initial_value: "300"}
api:
reboot_timeout: 0s
encryption:
key: !secret api_key
actions:
- action: cfg
variables: {len: int, crc: bool}
then:
- lambda: |-
auto *c = id(radio);
c->set_idle();
c->set_crc_enable(crc);
c->set_packet_length(len);
c->begin_rx();
ESP_LOGI("cct", "CFG len=%d crc=%d PKTLEN=%u PKTCTRL0=0x%02X MARC=0x%02X", len, (int) crc, ccl::rd(c, 0x06), ccl::rd(c, 0x08), ccl::rdst(c, 0x35) & 0x1F);
- action: send
variables: {count: int, len: int, b0: int, gap_ms: int}
then:
- lambda: 'id(txerr) = 0;'
- repeat:
count: !lambda return count;
then:
- lambda: |-
std::vector<uint8_t> b;
ccl::fill(b, len, b0, id(seq)++);
if (len <= 64) {
if (id(radio).transmit_packet(b) != cc1101::CC1101Error::NONE) id(txerr)++;
} else if (ccl::long_tx(id(radio), b.data(), b.size(), id(txpoll)) != 0) {
id(txerr)++;
}
- delay: !lambda return gap_ms;
- lambda: 'ESP_LOGI("cct", "SENT count=%d len=%d b0=%d errors=%u", count, len, b0, (unsigned) id(txerr));'
- action: rate
variables: {sr: float, mod: int, dev: float, bw: float, poll: int, sync: int}
then:
- lambda: |-
auto *c = id(radio);
c->set_idle();
c->set_modulation_type(static_cast<cc1101::Modulation>(mod));
c->set_symbol_rate(sr);
c->set_fsk_deviation(dev);
c->set_filter_bandwidth(bw);
c->set_sync_mode(static_cast<cc1101::SyncMode>(sync));
c->begin_rx();
id(txpoll) = poll;
ESP_LOGI("cct", "RATE sync=%d sr=%.0f mod=%d dev=%.0f bw=%.0f MDMCFG4=0x%02X MDMCFG3=0x%02X MDMCFG2=0x%02X DEVIATN=0x%02X MARC=0x%02X", sync, sr, mod, dev, bw, ccl::rd(c, 0x10), ccl::rd(c, 0x11), ccl::rd(c, 0x12), ccl::rd(c, 0x15), ccl::rdst(c, 0x35) & 0x1F);
- action: pdown
then:
- lambda: 'id(radio).power_down(); ESP_LOGI("cct", "POWER_DOWN");'
- action: wup
then:
- lambda: 'id(radio).wake_up(); id(radio).begin_rx(); ESP_LOGI("cct", "WAKE_UP MARC=0x%02X", ccl::rdst(id(radio), 0x35) & 0x1F);'
- action: setlen
variables: {len: int}
then:
- lambda: 'id(radio).set_packet_length(len); ESP_LOGI("cct", "SETLEN %d PKTLEN=%u PKTCTRL0=0x%02X", len, ccl::rd(id(radio), 0x06), ccl::rd(id(radio), 0x08));'
- action: pmode
variables: {enable: bool}
then:
- lambda: 'id(radio).set_packet_mode(enable); ESP_LOGI("cct", "PMODE %d PKTCTRL0=0x%02X", (int) enable, ccl::rd(id(radio), 0x08));'
- action: whiten
variables: {enable: bool}
then:
- lambda: 'id(radio).set_idle(); id(radio).set_whitening(enable); id(radio).begin_rx(); ESP_LOGI("cct", "WHITEN %d PKTCTRL0=0x%02X", (int) enable, ccl::rd(id(radio), 0x08));'
- action: tx_idle_rx
then:
- lambda: 'id(radio).begin_tx(); delay(5); id(radio).set_idle(); id(radio).begin_rx(); ESP_LOGI("cct", "TX_IDLE_RX MARC=0x%02X", ccl::rdst(id(radio), 0x35) & 0x1F);'
- action: tx_reset
then:
- lambda: 'id(radio).begin_tx(); delay(5); id(radio).reset(); ESP_LOGI("cct", "TX_RESET MARC=0x%02X", ccl::rdst(id(radio), 0x35) & 0x1F);'
- action: tx_setters
then:
- lambda: |-
auto *c = id(radio);
c->begin_tx();
delay(5);
const uint8_t before = ccl::rd(c, 0x08);
c->set_crc_enable(false);
c->set_whitening(false);
c->set_packet_length(32);
const uint8_t after = ccl::rd(c, 0x08);
ESP_LOGI("cct", "TX_SETTERS PKTCTRL0 before=0x%02X after=0x%02X MARC=0x%02X", before, after, ccl::rdst(c, 0x35) & 0x1F);
c->begin_rx();
ESP_LOGI("cct", "TX_SETTERS begin_rx PKTCTRL0=0x%02X MARC=0x%02X", ccl::rd(c, 0x08), ccl::rdst(c, 0x35) & 0x1F);
- action: tx_only
then:
- lambda: 'id(radio).begin_tx(); delay(5); ESP_LOGI("cct", "TX_ONLY MARC=0x%02X", ccl::rdst(id(radio), 0x35) & 0x1F);'
- action: hf
then:
- lambda: 'ESP_LOGI("cct", "HF %d", (int) esphome::HighFrequencyLoopRequester::is_high_frequency());'
- action: idle_rx
then:
- lambda: |-
id(radio).set_idle();
id(radio).begin_rx();
ESP_LOGI("cct", "IDLE_RX MARC=0x%02X", ccl::rdst(id(radio), 0x35) & 0x1F);
- action: tx_rx
then:
- lambda: |-
id(radio).begin_tx();
delay(5);
id(radio).begin_rx();
ESP_LOGI("cct", "TX_RX MARC=0x%02X", ccl::rdst(id(radio), 0x35) & 0x1F);
cc1101:
id: radio
cs_pin: ${cs}
gdo0_pin: ${gdo0}
frequency: 433.92MHz
modulation_type: GFSK
symbol_rate: 4800
packet_mode: true
packet_length: ${plen}
sync_mode: "16/16"
output_power: 0
on_packet:
then:
- lambda: |-
ESP_LOGI("cct", "RX len=%u b0=%u seq=%u ok=%d rssi=%.1f lqi=%u fo=%.1f", (unsigned) x.size(), x.empty() ? 0 : x[0], x.size() > 1 ? x[1] : 0, (int) ccl::check(x), rssi, lqi, freq_offset);
@@ -0,0 +1,501 @@
"""ccdrive.py TXHOST RXHOST MODE OUT [TXLABEL RXLABEL]: drives both radios over API actions and counts the packets
each receiver logs. Writes all log lines to OUT and prints one summary line per case.
MODE: short, long, r2long, r2mid, lambda, r2lambda, fast (fast38, fast100 for one rate), and the dev bug repros crc,
rxtx, txback, txrx, txset and nocrc64 (see recipes/cc1101.md).
Exits 1 on a failed action, a missing CFG/RATE/SENT line or a lost API connection (INVALID), a MISSED case, or error
lines from the radios outside the repros."""
import asyncio
import pathlib
import re
import statistics
import sys
import time
from aioesphomeapi import LogLevel
sys.path.insert(0, str(pathlib.Path(__file__).resolve().parents[1]))
from apiconn import connect, log_text # noqa: E402
TXH, RXH, MODE, OUT = sys.argv[1:5]
TXN, RXN = (sys.argv[5:7] + ["TX", "RX"][len(sys.argv[5:7]) :])[:2]
out = pathlib.Path(OUT).open("w", encoding="utf-8", buffering=1) # noqa: SIM115
RX_RE = re.compile(
r"RX len=(\d+) b0=(\d+) seq=(\d+) ok=(\d) rssi=(-?[\d.]+) lqi=(\d+) fo=(-?[\d.]+)"
)
class Dev:
def __init__(self, host, tag):
self.host, self.tag, self.rx, self.lines = host, tag, [], []
self.failed = (
0 # actions that could not be sent and CFG/RATE lines that never came
)
async def start(self):
self.cli = await connect(self.host, on_stop=self.on_stop)
_, svcs = await self.cli.list_entities_services()
self.svc = {s.name: s for s in svcs}
self.cli.subscribe_logs(self.on_log, log_level=LogLevel.LOG_LEVEL_INFO)
async def on_stop(self, expected: bool) -> None:
if (
not expected
): # a reboot or Wi-Fi drop: the rest of the run is not trustworthy
self.failed += 1
print(f" INVALID: API connection to {self.tag} lost", flush=True)
def on_log(self, m):
line = log_text(m)
out.write(f"{time.strftime('%H:%M:%S')} {self.tag} {line}\n")
if r := RX_RE.search(line):
self.rx.append(tuple(float(v) for v in r.groups()))
if "cct" in line or "cc1101" in line:
self.lines.append(line)
async def act(self, name, **kw):
try:
await self.cli.execute_service(self.svc[name], kw)
except Exception as e: # noqa: BLE001
self.failed += 1
print(
f" ACTION FAILED {self.tag} {name}: {type(e).__name__}: {e}",
flush=True,
)
await asyncio.sleep(0.3)
async def wait_mark(dev, mark, before, timeout=8.0):
t = time.monotonic()
while (
sum(1 for line in dev.lines if mark in line) <= before
and time.monotonic() - t < timeout
):
await asyncio.sleep(0.1)
if sum(1 for line in dev.lines if mark in line) <= before:
dev.failed += 1
print(f" INVALID: {dev.tag} never logged {mark.strip()}", flush=True)
return False
return True
REPORTED = 0 # failures already shown on a result line
# The dev bug repros measure a known failure, so their counts are reported but not judged
REPROS = {"crc", "rxtx", "txback", "txrx", "txset", "nocrc64"}
MISSED: list[str] = [] # cases whose good count missed what they expect
def verdict(tx, rx):
"""INVALID when an action or an awaited log line failed since the last result line."""
global REPORTED
total = tx.failed + rx.failed
bad, REPORTED = total > REPORTED, total
return "INVALID " if bad else ""
async def configure(dev, length, crc=False):
n = sum(1 for line in dev.lines if "CFG " in line)
await dev.act("cfg", len=length, crc=crc)
await wait_mark(dev, "CFG ", n)
def sent_count(dev):
return sum(1 for line in dev.lines if "SENT " in line)
async def tx_errors(tx, before):
"""The errors= of the sender's SENT line for the last send, or ? when it never came."""
if not await wait_mark(tx, "SENT ", before):
return "?"
m = re.search(
r"SENT .* errors=(\d+)", [line for line in tx.lines if "SENT " in line][-1]
)
return m[1] if m else "?"
async def case(
tx,
rx,
label,
count,
length,
b0,
gap_ms,
rx_len=None,
tx_len=None,
crc=False,
wait=None,
want=None,
):
"""WANT: "most" (80 % good), "none" (no packet at all) or "any" (not judged); judged cases need txerr=0."""
want = want or ("any" if MODE in REPROS else "most")
nr, nt = (
sum(1 for line in rx.lines if "CFG " in line),
sum(1 for line in tx.lines if "CFG " in line),
)
if rx_len != -1:
await rx.act("cfg", len=rx_len if rx_len is not None else length, crc=crc)
if tx_len != -1:
await tx.act(
"cfg", len=tx_len if tx_len is not None else min(length, 64), crc=crc
)
if rx_len != -1:
await wait_mark(rx, "CFG ", nr)
if tx_len != -1:
await wait_mark(tx, "CFG ", nt)
await asyncio.sleep(0.5)
rx.rx.clear()
ns = sent_count(tx)
await tx.act("send", count=count, len=length, b0=b0, gap_ms=gap_ms)
await asyncio.sleep(
wait if wait is not None else count * (gap_ms / 1000 + length / 600 + 0.15) + 2
)
txerr = await tx_errors(tx, ns)
got = [p for p in rx.rx if int(p[1]) == (b0 & 0xFF)]
good = [p for p in got if int(p[0]) == length and p[3] == 1]
other = len(rx.rx) - len(got)
def stat(i):
return f"{statistics.mean(p[i] for p in good):.1f}" if good else "-"
lens = sorted({int(p[0]) for p in got})
# a judged case also needs every packet to have gone out: "nothing arrived" proves nothing otherwise
missed = want != "any" and (
txerr != "0"
or (want == "most" and len(good) * 5 < count * 4)
or (want == "none" and len(got) > 0)
)
if missed:
MISSED.append(label)
print(
f"{label:34} {verdict(tx, rx)}{'MISSED ' if missed else ''}sent={count} txerr={txerr} good={len(good)} wrong={len(got) - len(good)} lens={lens} other={other} "
f"rssi={stat(4)} lqi={stat(5)} fo={stat(6)}",
flush=True,
)
return len(good)
async def main():
tx, rx = Dev(TXH, "TX"), Dev(RXH, "RX")
await asyncio.gather(tx.start(), rx.start())
await asyncio.sleep(1)
if MODE.startswith("fast"):
rates = {
"fast38": (38400, 1, 20000, 135000, 50),
"fast100": (100000, 0, 50000, 325000, 20),
}
if MODE not in (*rates, "fast"):
sys.exit(f"unknown mode {MODE}")
for key in [MODE] if MODE in rates else list(rates):
sr, mod, dev, bw, poll = rates[key]
for d in (tx, rx):
n = sum(1 for line in d.lines if "RATE " in line)
await d.act("rate", sr=sr, mod=mod, dev=dev, bw=bw, poll=poll, sync=3)
await wait_mark(d, "RATE ", n)
await asyncio.sleep(0.5)
print(
"RATE",
[line.split("RATE ")[1] for line in rx.lines if "RATE " in line][-1:],
flush=True,
)
k = sr / 4800
await case(tx, rx, f"{key} len=32", 20, 32, 70, 100)
await case(
tx,
rx,
f"{key} len=300",
20,
300,
71,
100,
wait=20 * (0.1 + 0.3 + 300 / 600 / k) + 3,
)
await case(
tx,
rx,
f"{key} len=1024",
10,
1024,
72,
100,
wait=10 * (0.1 + 0.4 + 1024 / 600 / k) + 3,
)
await case(tx, rx, f"{key} burst 300 x5", 5, 300, 73, 0, wait=8)
ov_before = len([line for line in rx.lines if "overflow" in line])
print(f"{key} RX overflow warnings so far: {ov_before}", flush=True)
for d in (tx, rx):
await d.act("rate", sr=4800, mod=1, dev=47607, bw=203000, poll=300, sync=2)
elif MODE == "short":
for direction, a, b in ((f"{TXN}->{RXN}", tx, rx), (f"{RXN}->{TXN}", rx, tx)):
for length in (32, 64):
for crc in (False, True):
await case(
a,
b,
f"{direction} len={length} crc={int(crc)}",
20,
length,
1,
150,
crc=crc,
want="any"
if length == 64 and crc
else None, # 0/20 on dev too, #22635
)
elif MODE == "long":
await case(tx, rx, f"{TXN}->{RXN} len=32 (runtime, long build)", 10, 32, 1, 200)
for length in (100, 255, 300, 1024):
await case(tx, rx, f"{TXN}->{RXN} long len={length}", 10, length, 2, 300)
await rx.act("idle_rx")
await case(tx, rx, "after set_idle+begin_rx len=100", 5, 100, 3, 300, wait=None)
await rx.act("tx_rx")
await case(
tx, rx, "after begin_tx+begin_rx len=300", 5, 300, 4, 300, want="any"
) # dead reception on dev, #22636
elif MODE == "r2long":
await case(tx, rx, "burst 300 x5, no gap", 5, 300, 10, 0)
await case(tx, rx, "burst 65 x10, no gap", 10, 65, 11, 0)
await case(tx, rx, "burst 1024 x3, no gap", 3, 1024, 12, 0)
# power down in the middle of a run of 300-byte packets, wake 1 s later
await configure(rx, 300)
await configure(tx, 64)
rx.rx.clear()
ns = sent_count(tx)
await tx.act("send", count=10, len=300, b0=13, gap_ms=300)
await asyncio.sleep(1.6)
await rx.act("pdown")
await asyncio.sleep(1.0)
await rx.act("wup")
await asyncio.sleep(10 * 0.85 + 2)
got = [p for p in rx.rx if int(p[1]) == 13]
good = sum(1 for p in got if int(p[0]) == 300 and p[3] == 1)
txerr = await tx_errors(tx, ns)
print(
f"{'power_down mid-run, wake 1 s later':34} {verdict(tx, rx)}sent=10 txerr={txerr} good={good} "
f"wrong={len(got) - good}",
flush=True,
)
await case(tx, rx, "after power_down/wake_up 300", 5, 300, 14, 300, rx_len=-1)
await rx.act("pdown")
await asyncio.sleep(0.5)
await rx.act("wup")
await case(tx, rx, "power_down/wake_up between, 1024", 3, 1024, 15, 300)
elif MODE == "r2mid":
await configure(rx, 1024)
await configure(tx, 64)
await asyncio.sleep(0.5)
await tx.act("send", count=1, len=1024, b0=40, gap_ms=0)
await asyncio.sleep(0.6)
await rx.act("hf")
await rx.act("setlen", len=32)
await asyncio.sleep(0.6)
await rx.act("hf")
await asyncio.sleep(1.5)
await case(
tx, rx, "after setlen 32 mid-packet, len 32", 5, 32, 41, 200, rx_len=-1
)
await configure(rx, 1024)
await tx.act("send", count=1, len=1024, b0=42, gap_ms=0)
await asyncio.sleep(0.6)
await rx.act("hf")
await rx.act("pmode", enable=False)
await asyncio.sleep(0.6)
await rx.act("hf")
await rx.act("pmode", enable=True)
await asyncio.sleep(1.5)
await case(
tx, rx, "after packet_mode off/on mid-packet, len 100", 5, 100, 43, 300
)
await rx.act("pdown")
await rx.act("setlen", len=300)
await case(
tx,
rx,
"asleep after setlen 300 (expect 0)",
3,
300,
44,
300,
rx_len=-1,
wait=5,
want="none",
)
await rx.act("wup")
await case(tx, rx, "after wake_up, len 300", 5, 300, 45, 300, rx_len=-1)
hf = [line for line in rx.lines if "HF " in line]
print("HF lines:", [line.split("HF ")[1][:2] for line in hf], flush=True)
elif MODE == "txset":
await case(tx, rx, "32 before", 10, 32, 60, 150)
await rx.act("tx_setters")
await case(tx, rx, "begin_tx, setters, begin_rx", 20, 32, 61, 150, rx_len=-1)
print(
"lines:",
[
line.split("TX_SETTERS ")[1][:60]
for line in rx.lines
if "TX_SETTERS" in line
],
flush=True,
)
elif MODE == "txback":
await case(tx, rx, f"{TXN}->{RXN} before", 10, 32, 50, 150)
await case(
rx,
tx,
f"{RXN} send (plain) -> {TXN}",
10,
32,
51,
150,
rx_len=-1,
tx_len=-1,
)
await case(
tx, rx, f"{TXN}->{RXN} after plain sends", 20, 32, 52, 150, rx_len=-1
)
await rx.act("tx_only")
await case(
rx,
tx,
f"begin_tx, then {RXN} send -> {TXN}",
10,
32,
53,
150,
rx_len=-1,
tx_len=-1,
)
await case(tx, rx, f"{TXN}->{RXN} after that", 20, 32, 54, 150, rx_len=-1)
elif MODE == "nocrc64":
await case(tx, rx, "64 no CRC x100", 100, 64, 55, 150)
await case(tx, rx, "64 CRC x100", 100, 64, 56, 150, crc=True)
elif MODE == "r2lambda":
await case(tx, rx, "lambda 40 before", 5, 40, 39, 200, rx_len=-1, tx_len=40)
await rx.act("setlen", len=100)
await case(
tx, rx, "runtime setlen 100 (clears lambda)", 5, 100, 16, 300, rx_len=-1
)
await rx.act("setlen", len=32)
await case(tx, rx, "runtime setlen 32", 5, 32, 17, 200, rx_len=-1, tx_len=32)
await rx.act("pmode", enable=False)
await rx.act("pmode", enable=True)
await case(
tx,
rx,
"after packet_mode off/on, len 32",
5,
32,
18,
200,
rx_len=-1,
tx_len=32,
)
await rx.act("setlen", len=300)
await case(tx, rx, "runtime setlen 300", 5, 300, 19, 300, rx_len=-1)
elif MODE == "crc":
await case(tx, rx, "64 CRC", 20, 64, 20, 150, crc=True)
await case(tx, rx, "32 CRC", 20, 32, 21, 150, crc=True)
await case(tx, rx, "64 no CRC", 20, 64, 22, 150)
await tx.act("whiten", enable=True)
await case(
tx, rx, "64 CRC, TX whitening only (bad)", 5, 64, 23, 200, crc=True, wait=4
)
await tx.act("whiten", enable=False)
await case(tx, rx, "64 CRC after the bad ones", 10, 64, 24, 150, crc=True)
elif MODE == "rxtx":
await case(tx, rx, "32 before", 10, 32, 30, 150)
await rx.act("tx_rx")
await case(tx, rx, "begin_tx + begin_rx", 20, 32, 31, 150, rx_len=-1)
await rx.act("tx_idle_rx")
await case(tx, rx, "begin_tx + set_idle + begin_rx", 20, 32, 32, 150, rx_len=-1)
await rx.act("tx_reset")
await case(tx, rx, "begin_tx + reset", 20, 32, 33, 150, rx_len=-1)
await rx.act("tx_only")
await case(
rx,
tx,
f"begin_tx, then {RXN} send -> {TXN}",
10,
32,
34,
150,
rx_len=-1,
tx_len=-1,
)
await case(tx, rx, f"{TXN}->{RXN} after that", 20, 32, 35, 150, rx_len=-1)
elif MODE == "txrx":
await case(tx, rx, "len=32 before", 5, 32, 6, 200)
await rx.act("tx_rx")
await case(tx, rx, "len=32 after begin_tx+begin_rx", 5, 32, 7, 200, rx_len=-1)
await rx.act("idle_rx")
await case(tx, rx, "len=32 after set_idle+begin_rx", 5, 32, 8, 200, rx_len=-1)
await case(tx, rx, "len=32 after cfg", 5, 32, 9, 200)
elif MODE == "lambda":
for length in (10, 40, 64):
await case(
tx,
rx,
f"{TXN}->{RXN} lambda len={length}",
10,
length,
length - 1,
200,
rx_len=-1,
tx_len=length,
)
await case(
tx,
rx,
f"{TXN}->{RXN} lambda discard (b0=0xEE)",
3,
40,
0xEE,
200,
rx_len=-1,
tx_len=40,
want="none",
)
await case(
tx,
rx,
f"{TXN}->{RXN} lambda after discard len=40",
5,
40,
39,
200,
rx_len=-1,
tx_len=40,
)
await rx.act("idle_rx")
await case(
tx,
rx,
"lambda after set_idle+begin_rx len=40",
5,
40,
39,
200,
rx_len=-1,
tx_len=40,
)
else:
sys.exit(f"unknown mode {MODE}")
errors = 0
for d in (tx, rx):
warn = [line for line in d.lines if "[W]" in line or "[E]" in line]
print(f"{d.tag} warnings/errors: {len(warn)}", *warn[:10], sep="\n ")
errors += sum("[E]" in line for line in warn)
await d.cli.disconnect()
if errors and MODE not in REPROS:
sys.exit(f"FAIL: {errors} error lines from the radios")
if tx.failed or rx.failed:
sys.exit(
f"INVALID: {tx.failed + rx.failed} failed actions or missing CFG/RATE lines"
)
if MISSED:
sys.exit(f"MISSED: {len(MISSED)} cases below their expected count: {MISSED}")
asyncio.run(main())
@@ -0,0 +1,106 @@
// Raw CC1101 helpers for the #17578 test: register readback and a TX that streams packets longer than the TX FIFO.
#pragma once
#include "esphome/components/cc1101/cc1101.h"
#include "esphome/core/application.h"
#include <algorithm>
namespace ccl {
using esphome::cc1101::CC1101Component;
inline uint8_t rd(CC1101Component *c, uint8_t a) {
c->enable();
c->write_byte(a | 0x80);
uint8_t v = c->read_byte();
c->disable();
return v;
}
inline uint8_t rdst(CC1101Component *c, uint8_t a) { return rd(c, a | 0x40); }
inline void wr(CC1101Component *c, uint8_t a, uint8_t v) {
c->enable();
c->write_byte(a);
c->write_byte(v);
c->disable();
}
inline void strobe(CC1101Component *c, uint8_t s) {
c->enable();
c->write_byte(s);
c->disable();
}
inline void fifo(CC1101Component *c, const uint8_t *d, size_t n) {
c->enable();
c->write_byte(0x7F);
for (size_t i = 0; i < n; i++)
c->write_byte(d[i]);
c->disable();
}
// Datasheet 15.2.2: infinite mode with PKTLEN = n mod 256, fixed mode once fewer than 256 bytes remain.
// Returns 0 on success, 1 on an early underflow, 2 on a timeout, 3 on an underflow after the last byte in fixed length
// mode (the switch from infinite mode came too late). A length that is a multiple of 256 stays in infinite mode and
// ends on the underflow after its last byte.
inline int long_tx(CC1101Component *c, const uint8_t *d, size_t n, uint32_t poll_us = 300) {
c->set_idle();
const uint8_t p0 = rd(c, 0x08), plen = rd(c, 0x06);
strobe(c, 0x3B); // SFTX
bool inf = n > 255;
wr(c, 0x06, n & 0xFF);
wr(c, 0x08, (p0 & ~3) | (inf ? 2 : 0));
size_t w = std::min<size_t>(n, 64);
fifo(c, d, w);
strobe(c, 0x35); // STX
int err = 0;
const uint32_t t0 = millis();
while (true) {
uint8_t tb = rdst(c, 0x3A);
if (tb & 0x80) {
if (w < n)
err = 1;
else if (!inf)
err = 3;
break;
}
tb &= 0x7F;
const size_t sent = w - tb;
if (inf && (n & 0xFF) != 0 && n - sent < 256) {
wr(c, 0x08, p0 & ~3);
inf = false;
}
if (w < n && tb < 48) {
const size_t k = std::min<size_t>(n - w, 63 - tb);
fifo(c, d + w, k);
w += k;
}
const uint8_t marc = rdst(c, 0x35) & 0x1F;
if (w == n && tb == 0 && marc != 0x13 && marc != 0x14) // not TX or TX_END
break;
if (millis() - t0 > 4000) {
err = 2;
break;
}
esphome::App.feed_wdt();
delayMicroseconds(poll_us);
}
delay(5);
c->set_idle();
strobe(c, 0x3B);
wr(c, 0x06, plen);
wr(c, 0x08, p0);
c->begin_rx();
return err;
}
inline void fill(std::vector<uint8_t> &b, size_t len, int b0, uint8_t seq) {
b.resize(len);
for (size_t i = 0; i < len; i++)
b[i] = (uint8_t) (seq * 31 + i * 7);
b[0] = (uint8_t) b0;
if (len > 1)
b[1] = seq;
}
inline bool check(const std::vector<uint8_t> &x) {
if (x.size() < 2)
return true;
const uint8_t seq = x[1];
for (size_t i = 2; i < x.size(); i++)
if (x[i] != (uint8_t) (seq * 31 + i * 7))
return false;
return true;
}
} // namespace ccl
@@ -0,0 +1,16 @@
#!/bin/bash
# flashmac.sh NAME BOARD: upload NAME.yaml from TREE to BOARD (a port such as /dev/cu.usbserial-0001, or the board's
# USB serial number, `ports.py` lists it, so a port that moved after a replug is still found), then wait until
# NAME.local answers on the API with that name. Needs apikey.txt (the api_key secret) next to the configs.
# env: TREE, PY
D=$(cd "$(dirname "$0")" && pwd); S=$(dirname "$D"); N=${1:?name}; BOARD=${2:?port or USB serial number}
T=${TREE:?set TREE}; PY=${PY:-$T/venv/bin/python}; cd "$D" || exit 1
export API_KEY=${API_KEY:-$(cat apikey.txt)}
case $BOARD in
/dev/*) P=$BOARD ;;
*) P=$($PY "$S/ports.py" --serial "$BOARD" --timeout 10) || { echo "no port for $BOARD"; exit 1; } ;;
esac
echo "flash $N to $BOARD on $P"
PIO_CORE=${PIO_CORE:-$D/pio} bash "$S/pio_build.sh" upload "$N" "$D/$N.yaml" "$P" || exit 1
IP=$(bash "$S/resolve.sh" "$N" 40) || { echo "$N did not come up"; exit 1; }
$PY "$S/ota_timing/apiinfo.py" "$IP" | grep " $N " || { echo "$N did not answer on the API"; exit 1; }
+28
View File
@@ -0,0 +1,28 @@
#!/bin/bash
# runall.sh A B: both roles; A and B are the boards' ports or USB serial numbers (see flashmac.sh). Phase A: board A
# receives (long build, then the lambda build) while board B sends from its long build; phase B swaps them.
# Configs: cc-a-long.yaml, cc-a-lambda.yaml, cc-b-long.yaml, cc-b-lambda.yaml (copies of the c3/s3 examples with
# `name:` set to the file name). Order matters, see recipes/cc1101.md.
# env: TREE, PY
D=$(cd "$(dirname "$0")" && pwd); cd "$D" || exit 1; A=${1:?board a}; B=${2:?board b}
PY=${PY:-${TREE:?set TREE}/venv/bin/python}
export API_KEY=${API_KEY:-$(cat apikey.txt)}
FAILED=0
drive() { # TXNAME RXNAME MODE TXLABEL RXLABEL
echo "--- $3 TX=$1 RX=$2"
timeout 1200 $PY ccdrive.py "$1.local" "$2.local" "$3" "$2-$3.log" "$4" "$5" 2>&1 | grep -v "^\s*$"
local rc=${PIPESTATUS[0]}
[ "$rc" = 0 ] || { echo "MODE FAILED $3 TX=$1 RX=$2 exit=$rc (124 is the timeout)"; FAILED=$((FAILED + 1)); }
}
fl() { ./flashmac.sh "$1" "$2" || { echo "FLASH FAIL $1"; exit 1; }; }
phase() { # TX RX TXBOARD RXBOARD TXLABEL RXLABEL: RX receives (long build, then the lambda build), TX sends
fl "cc-$1-long" "$3"
fl "cc-$2-long" "$4"; for m in fast r2long r2mid long; do drive "cc-$1-long" "cc-$2-long" $m "$5" "$6"; done
fl "cc-$2-lambda" "$4"; for m in lambda r2lambda; do drive "cc-$1-long" "cc-$2-lambda" $m "$5" "$6"; done
}
echo "===== Phase A: A receives, B sends"
phase b a "$B" "$A" B A
echo "===== Phase B: B receives, A sends"
phase a b "$A" "$B" A B
echo "RUNDONE failed_modes=$FAILED"
[ "$FAILED" = 0 ]
@@ -0,0 +1,16 @@
# Example wiring for an E07-M1101D (CC1101) module; change the pins to match your board. plen 1024 = long packets,
# 32 = plain fixed length (copy the file and set plen and name for a plain build).
substitutions:
name: cc-s3-long
clk: GPIO12
mosi: GPIO11
miso: GPIO13
cs: GPIO10
gdo0: GPIO9
plen: "1024"
packages:
c: !include cccommon.yaml
esp32:
board: esp32-s3-devkitc-1
framework:
type: esp-idf
@@ -0,0 +1,17 @@
substitutions:
name: en32p
peer: en66p
peer_mac: AA:BB:CC:00:00:66 # the D1 mini peer's station MAC
my_addr: "0x0032"
peer_addr: "0x0066"
raw_max: "1470"
payload_max: "1470"
sizes: "16, 100, 249, 250, 251, 600, 1470"
long_id: "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"
chaos: "true"
esp32:
board: esp32dev
framework:
type: esp-idf
packages:
link: !include enlink.yaml
@@ -0,0 +1,15 @@
substitutions:
name: en66p
peer: en32p
peer_mac: AA:BB:CC:00:00:32 # the ESP32 peer's station MAC
my_addr: "0x0066"
peer_addr: "0x0032"
raw_max: "250"
payload_max: "250"
sizes: "16, 100, 249, 250"
long_id: "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"
chaos: "false"
esp8266:
board: d1_mini
packages:
link: !include enlink.yaml
@@ -0,0 +1,117 @@
"""encheck.py LOG [LOG...]: summarise an entity_transport link test capture.
Reads ETRX <link> <field>=<value>, SESS <link> 0/1, STAT, CHAOS and warning lines; ETRX lines it cannot parse are
counted. Per link: cnt updates, gaps in the received count (a gap is a step other than +1), values never sent (flt not
equal to the sender's formula), binary transitions, long sensor updates, session edges. Prints one block per log.
Exits 1 on a reboot or crash during a capture (more than one boot), a value never sent, a checked stream with bad
payloads, an ETRX line it cannot parse (other than the cut last line), no STAT line, or no counter value on any link.
"""
import collections
import pathlib
import re
import struct
import sys
def f32(v):
return struct.unpack("<f", struct.pack("<f", v))[0]
def expect(t): # 12345.678f + t * 0.001f - (t % 7) * 1000.5f in single precision
return f32(f32(f32(12345.678) + f32(f32(t) * f32(0.001))) - f32((t % 7) * 1000.5))
VALID = {f"{expect(t):.3f}" for t in range(20000)}
TS = re.compile(r"^\[(\d\d):(\d\d):(\d\d)")
def secs(line):
m = TS.match(line)
return int(m[1]) * 3600 + int(m[2]) * 60 + int(m[3]) if m else None
CRASH = re.compile(r"Guru Meditation|Exception \(\d+\)|abort\(\)|panic|Backtrace:")
failures = []
for path in sys.argv[1:]:
crashes = 0
links = collections.defaultdict(
lambda: {"cnt": [], "flt": [], "bin": [], "long": [], "sess": []}
)
warns = collections.Counter()
stats, chaos, boots, unparsed = [], [], 0, 0
with pathlib.Path(path).open(encoding="utf-8", errors="replace") as fh:
for line in fh:
t = secs(line)
if "ETRX" in line:
m = re.search(r"ETRX (\S+) (\w+)=(\S+)", line)
if m:
links[m[1]][m[2]].append((t, m[3]))
elif "[partial]" not in line: # the capture cuts a line at its end
unparsed += 1
elif "SESS" in line:
m = re.search(r"SESS (\S+) (\d)", line)
if m:
links[m[1]]["sess"].append((t, m[2]))
elif "STAT" in line:
stats.append(line.strip())
elif "CHAOS" in line:
chaos.append((line[1:9], line.strip().split("CHAOS")[1]))
if "Running through setup" in line:
boots += 1
if CRASH.search(line):
crashes += 1
if re.search(r"\]\[[WE]\]", line):
msg = re.sub(r"^\[[^\]]*\]", "", line.strip())
msg = re.sub(r"\d+", "N", msg)
warns[msg] += 1
print(f"== {path} boots={boots} crashes={crashes} unparsed_etrx={unparsed}")
if boots > 1 or crashes:
failures.append(f"{path}: {boots} boots, {crashes} crash lines")
if unparsed:
failures.append(f"{path}: {unparsed} ETRX lines that could not be parsed")
if not stats:
failures.append(f"{path}: no STAT line")
if not any(d["cnt"] for d in links.values()):
failures.append(f"{path}: no link received a counter value")
for c in chaos:
print(" chaos", c)
for name, d in sorted(links.items()):
tc = [(t, int(float(v))) for t, v in d["cnt"] if v != "nan"]
cnts = [c for _, c in tc]
gaps = [
(tc[i][0], cnts[i - 1], cnts[i])
for i in range(1, len(cnts))
if cnts[i] not in (cnts[i - 1], cnts[i - 1] + 1)
]
bad_flt = [v for _, v in d["flt"] if v not in VALID and v != "nan"]
bins = [v for _, v in d["bin"]]
trans = sum(1 for i in range(1, len(bins)) if bins[i] != bins[i - 1])
print(
f" {name}: cnt updates={len(cnts)} first={cnts[:1]} last={cnts[-1:]} gaps={len(gaps)} "
f"flt={len(d['flt'])} bad_flt={len(bad_flt)} bin_trans={trans} long={len(d['long'])} "
f"sess={[s for _, s in d['sess']]}"
)
for g in gaps[:12]:
at = (
"?"
if g[0] is None
else f"{g[0] // 3600:02d}:{g[0] // 60 % 60:02d}:{g[0] % 60:02d}"
)
print(f" gap at {at}: {g[1]} -> {g[2]}")
if bad_flt:
print(" bad flt", bad_flt[:5])
failures.append(f"{path}: {name} received {len(bad_flt)} values never sent")
last = {}
for s in stats:
body = s[s.find("STAT") :]
last[body.split()[1]] = body
for body in last.values():
print(" " + body)
if (m := re.search(r" bad=(\d+)", body)) and int(m[1]):
failures.append(f"{path}: {body.split()[1]} bad={m[1]}")
for w, n in warns.most_common(15):
print(f" W x{n}: {w[:170]}")
for f in failures:
print("FAIL", f)
sys.exit(1 if failures else 0)
@@ -0,0 +1,239 @@
# ESP-NOW packet_interface link test (esphome#21120), shared part. No Wi-Fi; espnow on a fixed channel.
# Links per device:
# p2p : peer_address = the other board, xxtea + hdlc reliability + session + fragmentation, entity_transport et_p2p
# bc : broadcast, addressing (address ${my_addr}) with two ports:
# bc_rel : destination ${peer_addr}, xxtea with rolling code + reliability + session + fragmentation (et_bcrel)
# bc_all : destination broadcast, no layers (et_bcall, small dictionary)
# raw : broadcast, no layers, max_packet_size ${raw_max}; size sweep with magic, seq and checked payload
# substitutions: name, peer (peer's name), peer_mac, my_addr, peer_addr, raw_max, payload_max, long_id
esphome:
name: ${name}
includes:
- entest.hpp
on_boot:
- priority: 600
then:
- lambda: |-
entest::RAW.listen(id(raw));
entest::P2P.listen(id(p2p)->top());
entest::BCR.listen(id(bc_rel)->top());
- priority: -100
then:
- delay: 20s
- lambda: |-
bool r = entest::RAW.send(id(raw), ${raw_max} + 1);
ESP_LOGI("raw", "OVERSEND %u byte send returned %d", (unsigned) (${raw_max} + 1), r);
- if:
condition:
lambda: return ${chaos};
then:
- delay: 60s
- logger.log: "CHAOS short start"
- espnow.set_channel: 6
- delay: 3s
- espnow.set_channel: 1
- logger.log: "CHAOS short end"
- delay: 80s
- logger.log: "CHAOS long start"
- espnow.set_channel: 6
- delay: 15s
- espnow.set_channel: 1
- logger.log: "CHAOS long end"
logger:
level: DEBUG
baud_rate: 115200
espnow:
id: espnow_c
channel: 1
max_payload_size: ${payload_max}
globals:
- {id: counter, type: int, initial_value: "0"}
packet_interface:
- platform: espnow
id: p2p
peer_address: ${peer_mac}
encryption: p2p key 21120
reliability:
timeout: 300ms
max_retries: 20
max_queue_length: 6
session:
keep_alive: 2s
timeout: 8s
binary_sensor:
id: p2p_sess
on_state:
- logger.log: {format: "SESS p2p %d", args: ["x ? 1 : 0"]}
fragmentation:
max_packet_size: 1024
- platform: espnow
id: bc
addressing:
address: ${my_addr}
peers:
- id: bc_rel
destination: ${peer_addr}
encryption:
key: bc key 21120
rolling_code_enable: true
reliability:
timeout: 300ms
max_retries: 20
max_queue_length: 6
session:
keep_alive: 2s
timeout: 8s
binary_sensor:
id: bcrel_sess
on_state:
- logger.log: {format: "SESS bc_rel %d", args: ["x ? 1 : 0"]}
fragmentation:
max_packet_size: 1024
- id: bc_all
destination: broadcast
- platform: espnow
id: raw
max_packet_size: ${raw_max}
# hears every broadcast; larger ones must be dropped with a warning at most once a minute
- platform: espnow
id: raw_small
max_packet_size: 100
interval:
- interval: 1s
then:
- lambda: id(counter)++;
- interval: 500ms
then:
- lambda: |-
static const uint16_t SIZES[] = {${sizes}};
static const uint16_t RSIZES[] = {12, 200, 600};
entest::RAW.send(id(raw), SIZES[entest::RAW.idx++ % (sizeof(SIZES) / sizeof(SIZES[0]))]);
entest::P2P.send(id(p2p)->top(), RSIZES[entest::P2P.idx++ % 3]);
entest::BCR.send(id(bc_rel)->top(), RSIZES[entest::BCR.idx++ % 3]);
- interval: 10s
then:
- lambda: |-
entest::RAW.log();
entest::P2P.log();
entest::BCR.log();
ESP_LOGI("stat", "STAT cnt=%d sess=%d/%d", id(counter), id(p2p_sess).state, id(bcrel_sess).state);
sensor:
- platform: template
id: cnt
lambda: return id(counter);
update_interval: 1s
- platform: template
id: flt
lambda: |-
int t = id(counter);
return 12345.678f + t * 0.001f - (t % 7) * 1000.5f;
update_interval: 1s
accuracy_decimals: 3
- platform: template
id: longa
lambda: return id(counter) * 0.5f;
update_interval: 5s
# p2p
- platform: entity_transport
id: rx1
entity_transport_id: et_p2p
provider: ${peer}
remote_id: cnt
on_value: [logger.log: {format: "ETRX p2p cnt=%.0f", args: [x]}]
- platform: entity_transport
id: rx2
entity_transport_id: et_p2p
provider: ${peer}
remote_id: flt
on_value: [logger.log: {format: "ETRX p2p flt=%.3f", args: [x]}]
- platform: entity_transport
id: rx3
entity_transport_id: et_p2p
provider: ${peer}
remote_id: ${long_id}8
on_value: [logger.log: {format: "ETRX p2p long=%.1f", args: [x]}]
# bc_rel
- platform: entity_transport
id: rx4
entity_transport_id: et_bcrel
provider: ${peer}
remote_id: cnt
on_value: [logger.log: {format: "ETRX bcrel cnt=%.0f", args: [x]}]
- platform: entity_transport
id: rx5
entity_transport_id: et_bcrel
provider: ${peer}
remote_id: flt
on_value: [logger.log: {format: "ETRX bcrel flt=%.3f", args: [x]}]
- platform: entity_transport
id: rx6
entity_transport_id: et_bcrel
provider: ${peer}
remote_id: ${long_id}8
on_value: [logger.log: {format: "ETRX bcrel long=%.1f", args: [x]}]
# bc_all
- platform: entity_transport
id: rx7
entity_transport_id: et_bcall
provider: ${peer}
remote_id: cnt
on_value: [logger.log: {format: "ETRX bcall cnt=%.0f", args: [x]}]
- platform: entity_transport
id: rx8
entity_transport_id: et_bcall
provider: ${peer}
remote_id: flt
on_value: [logger.log: {format: "ETRX bcall flt=%.3f", args: [x]}]
binary_sensor:
- platform: template
id: bin
lambda: return (id(counter) / 3) % 2;
- platform: entity_transport
id: rx9
entity_transport_id: et_p2p
provider: ${peer}
remote_id: bin
on_state: [logger.log: {format: "ETRX p2p bin=%d", args: ["x ? 1 : 0"]}]
- platform: entity_transport
id: rx10
entity_transport_id: et_bcrel
provider: ${peer}
remote_id: bin
on_state: [logger.log: {format: "ETRX bcrel bin=%d", args: ["x ? 1 : 0"]}]
- platform: entity_transport
id: rx11
entity_transport_id: et_bcall
provider: ${peer}
remote_id: bin
on_state: [logger.log: {format: "ETRX bcall bin=%d", args: ["x ? 1 : 0"]}]
entity_transport:
- id: et_p2p
packet_interface: p2p
sensors: &big_sensors
- cnt
- flt
- {id: longa, broadcast_id: "${long_id}1"}
- {id: longa, broadcast_id: "${long_id}2"}
- {id: longa, broadcast_id: "${long_id}3"}
- {id: longa, broadcast_id: "${long_id}4"}
- {id: longa, broadcast_id: "${long_id}5"}
- {id: longa, broadcast_id: "${long_id}6"}
- {id: longa, broadcast_id: "${long_id}7"}
- {id: longa, broadcast_id: "${long_id}8"}
binary_sensors: [bin]
- id: et_bcrel
packet_interface: bc_rel
sensors: *big_sensors
binary_sensors: [bin]
- id: et_bcall
packet_interface: bc_all
sensors: [cnt, flt]
binary_sensors: [bin]
@@ -0,0 +1,99 @@
#pragma once
// Checked packet streams for the packet_interface link tests (esphome#21120).
// Packet: magic (2) | seq (2, LE) | sent size (2, LE) | bytes (seq + i). A reliable stream starts with 0x1E so
// entity_transport on the same stack drops it as an unknown packet type.
#include <cstdint>
#include <cstring>
#include "esphome/core/log.h"
#include "esphome/components/packet_interface/packet_interface.h"
namespace entest {
using esphome::packet_interface::PacketBuffer;
using esphome::packet_interface::PacketInterface;
using esphome::packet_interface::PacketMetaData;
struct Stream {
const char *name;
uint8_t m0, m1;
uint32_t tx{0}, ref{0}, rx{0}, ok{0}, bad{0}, gap{0}, dup{0}, trunc{0}, foreign{0}, big{0}, maxsz{0};
int32_t last{-1};
uint16_t seq{0};
uint32_t idx{0};
Stream(const char *n, uint8_t a, uint8_t b) : name(n), m0(a), m1(b) {}
void listen(PacketInterface *link) {
link->add_packet_interface_listener([this](const PacketBuffer &d, const PacketMetaData &) { this->on_rx(d); });
}
void on_rx(const PacketBuffer &d) {
static uint8_t buf[1600];
size_t n = d.size();
if (n > sizeof(buf))
n = sizeof(buf);
d.copy_to(buf, n);
this->rx++;
if (n < 6 || buf[0] != this->m0 || buf[1] != this->m1) {
this->foreign++;
return;
}
uint16_t seq = buf[2] | (buf[3] << 8);
uint16_t sent = buf[4] | (buf[5] << 8);
if (n != sent) {
this->trunc++;
ESP_LOGW("entest", "%s size %u received as %u (seq %u)", this->name, sent, (unsigned) n, seq);
}
for (size_t i = 6; i < n; i++)
if (buf[i] != (uint8_t) (seq + i)) {
this->bad++;
ESP_LOGW("entest", "%s bad payload seq=%u size=%u", this->name, seq, (unsigned) n);
return;
}
this->ok++;
if (n > 250)
this->big++;
if (n > this->maxsz)
this->maxsz = n;
if (this->last >= 0) {
if (seq == (uint16_t) this->last) {
this->dup++;
return;
}
if (seq != (uint16_t) (this->last + 1)) {
this->gap++;
ESP_LOGI("entest", "%s seq gap %d -> %u", this->name, (int) this->last, seq);
}
}
this->last = seq;
}
bool send(PacketInterface *link, uint16_t size) {
static uint8_t buf[1600];
buf[0] = this->m0;
buf[1] = this->m1;
buf[2] = this->seq;
buf[3] = this->seq >> 8;
buf[4] = size;
buf[5] = size >> 8;
for (size_t i = 6; i < size; i++)
buf[i] = (uint8_t) (this->seq + i);
if (link->send_to_interface(PacketBuffer(buf, size), {})) {
this->seq++;
this->tx++;
return true;
}
this->ref++;
return false;
}
void log() {
ESP_LOGI("stat", "STAT %s tx=%u ref=%u rx=%u ok=%u bad=%u gap=%u dup=%u trunc=%u big=%u maxsz=%u foreign=%u",
this->name, this->tx, this->ref, this->rx, this->ok, this->bad, this->gap, this->dup, this->trunc,
this->big, this->maxsz, this->foreign);
}
};
inline Stream RAW("raw", 0xA5, 0x5A);
inline Stream P2P("p2p_seq", 0x1E, 0x5A);
inline Stream BCR("bcrel_seq", 0x1E, 0x5B);
// UDP only: the point_to_point false and true links
inline Stream P2PF("p2pf_raw", 0xA5, 0x5B);
inline Stream P2PT("p2pt_raw", 0xA5, 0x5C);
} // namespace entest
@@ -0,0 +1,40 @@
#!/bin/bash
# pairrun.sh PREFIX E32V D1V [SECS] [RUN]: flash <PREFIX>32<E32V>.yaml (ESP32) and <PREFIX>66<D1V>.yaml (D1 mini), each
# from the tree of its build letter (p = PR_TREE, anything else = BASE_TREE), capture both boards SECS seconds (default
# 150) from a reset into <PREFIX>-<E32V><D1V>[-RUN]-<board>.log and print a summary per board. Exits 1 when a capture
# fails, a board logs no STAT line, reboots during the capture or logs a crash.
# PREFIX ur (urlink.yaml): last p2pf STAT and on_receive lines, sendto error count. PREFIX rg (rglink.yaml): last
# STAT rg line, packet_transport status, warning and error count. Make the files from ur32.example.yaml or
# rg32.example.yaml (the 66 file is the same with esp8266 and the peer swapped).
# env: PR_TREE, BASE_TREE, PORT_D1, PORT_E32, PY
D=$(cd "$(dirname "$0")" && pwd); S=$(dirname "$D"); cd "$D" || exit 1
X=${1:?prefix ur or rg}; E=${2:?esp32 build}; B=${3:?d1 build}; SECS=${4:-150}; L=$X-$E$B${5:+-$5}
tree() { if [ "$1" = p ]; then echo "${PR_TREE:?set PR_TREE}"; else echo "${BASE_TREE:?set BASE_TREE}"; fi; }
P1=${PORT_D1:?set PORT_D1}; P2=${PORT_E32:?set PORT_E32}; PY=${PY:-$(tree p)/venv/bin/python}
TREE=$(tree "$B") bash "$S/pio_build.sh" upload "${X}66$B" "${X}66$B.yaml" "$P1" || exit 1
TREE=$(tree "$E") bash "$S/pio_build.sh" upload "${X}32$E" "${X}32$E.yaml" "$P2" || exit 1
$PY "$S/serial_capture.py" "$P1" "$SECS" "$L-d1.log" --reset > "$L-d1.err" 2>&1 &
C1=$!
$PY "$S/serial_capture.py" "$P2" "$SECS" "$L-e32.log" --reset > "$L-e32.err" 2>&1 &
C2=$!
OK=1
wait $C1 || { echo "capture failed on the D1 mini:"; cat "$L-d1.err"; OK=0; }
wait $C2 || { echo "capture failed on the ESP32:"; cat "$L-e32.err"; OK=0; }
[ $OK = 1 ] || exit 1
MISSING=0
CRASH_RE="Guru Meditation|Exception \\([0-9]+\\)|abort\\(\\)|panic|Backtrace:"
for b in e32 d1; do
F=$L-$b.log
if [ "$X" = rg ]; then STAT='STAT rg'; else STAT='STAT p2pf'; fi
grep -a -q "$STAT" "$F" || { echo "e32=$E d1=$B $b: NO STAT line in $F"; MISSING=1; continue; }
BOOTS=$(grep -a -c "Running through setup" "$F"); CRASH=$(grep -a -c -E "$CRASH_RE" "$F")
[ "$BOOTS" -le 1 ] && [ "$CRASH" = 0 ] || { echo "e32=$E d1=$B $b: $BOOTS boots, $CRASH crash lines"; MISSING=1; }
if [ "$X" = rg ]; then
echo "e32=$E d1=$B $b: $(grep -a 'STAT rg' "$F" | tail -1 | sed 's/.*STAT rg //')" \
"status=$(grep -a PTSTATUS "$F" | awk '{print $NF}' | tr -d '\n') W=$(grep -a -c '\[W\]\|\[E\]' "$F")"
else
echo "e32=$E d1=$B $b: $(grep -a 'STAT p2pf' "$F" | tail -1 | sed 's/.*STAT //')" \
"$(grep -a 'on_receive' "$F" | tail -1 | sed 's/.*STAT //') senderr=$(grep -a -c 'sendto' "$F")"
fi
done
exit $MISSING
@@ -0,0 +1,15 @@
substitutions:
name: rg32p
peer_p: rg66p
peer_b: rg66b
peer_ip: 192.0.2.66 # the D1 mini peer's address
peer_mac: AA:BB:CC:00:00:66 # the D1 mini peer's station MAC
esp32:
board: esp32dev
framework:
type: esp-idf
packages:
link: !include rglink.yaml
espnow:
on_broadcast:
- lambda: id(e_bcb)++;
@@ -0,0 +1,129 @@
# Regression image for existing users (esphome#21120): packet_transport over UDP (multicast, xxtea, rolling code,
# ping-pong), a plain udp listener with on_receive and udp.write to the peer, plain espnow send/broadcast actions with
# on_receive/on_broadcast/on_sent/on_error, and packet_transport over espnow. Remote entities are declared for both
# the base and the PR name of the peer, so one image serves all four pairings.
# substitutions: name, peer_p, peer_b, peer_ip, peer_mac
esphome:
name: ${name}
logger:
level: DEBUG
wifi:
ssid: !secret wifi_ssid
password: !secret wifi_password
power_save_mode: none
globals:
- {id: counter, type: uint32_t, initial_value: "0"}
- {id: u_rx, type: uint32_t, initial_value: "0"}
- {id: u_bad, type: uint32_t, initial_value: "0"}
- {id: u_gap, type: uint32_t, initial_value: "0"}
- {id: u_last, type: int32_t, initial_value: "-1"}
- {id: e_rx, type: uint32_t, initial_value: "0"}
- {id: e_brx, type: uint32_t, initial_value: "0"}
- {id: e_gap, type: uint32_t, initial_value: "0"}
- {id: e_last, type: int32_t, initial_value: "-1"}
- {id: e_bcb, type: uint32_t, initial_value: "0"}
- {id: e_unk, type: uint32_t, initial_value: "0"}
- {id: e_sent, type: uint32_t, initial_value: "0"}
- {id: e_err, type: uint32_t, initial_value: "0"}
- {id: pt_u, type: uint32_t, initial_value: "0"}
- {id: pt_e, type: uint32_t, initial_value: "0"}
udp:
- id: udp_pt
port: 21130
listen_address: 239.0.60.53
addresses: ["239.0.60.53"]
- id: udp_plain
port: 21131
addresses: ["${peer_ip}"]
on_receive:
- lambda: |-
id(u_rx)++;
if (data.size() != 8 || data[0] != 'U') { id(u_bad)++; return; }
int32_t seq = data[1] | (data[2] << 8) | (data[3] << 16);
if (id(u_last) >= 0 && seq != id(u_last) + 1) id(u_gap)++;
id(u_last) = seq;
espnow:
auto_add_peer: false
peers: ["${peer_mac}"]
on_receive:
- address: ${peer_mac}
then:
- lambda: |-
if (size >= 4 && data[0] == 'S') {
id(e_rx)++;
int32_t seq = data[1] | (data[2] << 8) | (data[3] << 16);
if (id(e_last) >= 0 && seq != id(e_last) + 1) id(e_gap)++;
id(e_last) = seq;
} else if (size >= 4 && data[0] == 'B') {
id(e_brx)++;
}
on_unknown_peer:
- lambda: id(e_unk)++;
packet_transport:
- platform: udp
id: pt_udp
udp_id: udp_pt
update_interval: 5s
encryption: rg udp key
rolling_code_enable: true
ping_pong_enable: true
sensors: [cnt]
binary_sensors: [bin]
providers:
- {name: "${peer_p}", encryption: rg udp key}
- {name: "${peer_b}", encryption: rg udp key}
- platform: espnow
id: pt_espnow
peer_address: FF:FF:FF:FF:FF:FF
update_interval: 5s
encryption: {key: rg espnow key}
sensors: [cnt]
binary_sensors: [bin]
providers:
- {name: "${peer_p}", encryption: {key: rg espnow key}}
- {name: "${peer_b}", encryption: {key: rg espnow key}}
interval:
- interval: 1s
then:
- lambda: id(counter)++;
- espnow.send:
address: ${peer_mac}
data: !lambda |-
uint32_t c = id(counter);
return std::vector<uint8_t>{'S', (uint8_t) c, (uint8_t) (c >> 8), (uint8_t) (c >> 16)};
wait_for_sent: true
continue_on_error: true
on_sent: [lambda: id(e_sent)++;]
on_error: [lambda: id(e_err)++;]
- espnow.broadcast:
data: !lambda |-
uint32_t c = id(counter);
return std::vector<uint8_t>{'B', (uint8_t) c, (uint8_t) (c >> 8), (uint8_t) (c >> 16)};
- udp.write:
id: udp_plain
data: !lambda |-
uint32_t c = id(counter);
return std::vector<uint8_t>{'U', (uint8_t) c, (uint8_t) (c >> 8), (uint8_t) (c >> 16), 1, 2, 3, 4};
- interval: 10s
then:
- lambda: |-
ESP_LOGI("stat", "STAT rg cnt=%u udp rx=%u bad=%u gap=%u | espnow rx=%u gap=%u brx=%u onbcast=%u unknown=%u sent=%u err=%u | pt udp=%u espnow=%u",
id(counter), id(u_rx), id(u_bad), id(u_gap), id(e_rx), id(e_gap), id(e_brx), id(e_bcb), id(e_unk),
id(e_sent), id(e_err), id(pt_u), id(pt_e));
sensor:
- platform: template
id: cnt
lambda: return id(counter);
update_interval: 1s
- {platform: packet_transport, id: rpu_p, transport_id: pt_udp, provider: "${peer_p}", remote_id: cnt, on_value: [lambda: 'id(pt_u)++; ESP_LOGD("main", "PTRX udp cnt=%.0f", x);']}
- {platform: packet_transport, id: rpu_b, transport_id: pt_udp, provider: "${peer_b}", remote_id: cnt, on_value: [lambda: 'id(pt_u)++; ESP_LOGD("main", "PTRX udp cnt=%.0f", x);']}
- {platform: packet_transport, id: rpe_p, transport_id: pt_espnow, provider: "${peer_p}", remote_id: cnt, on_value: [lambda: 'id(pt_e)++; ESP_LOGD("main", "PTRX espnow cnt=%.0f", x);']}
- {platform: packet_transport, id: rpe_b, transport_id: pt_espnow, provider: "${peer_b}", remote_id: cnt, on_value: [lambda: 'id(pt_e)++; ESP_LOGD("main", "PTRX espnow cnt=%.0f", x);']}
binary_sensor:
- platform: template
id: bin
lambda: return (id(counter) / 3) % 2;
- {platform: packet_transport, id: rbu_p, transport_id: pt_udp, provider: "${peer_p}", remote_id: bin}
- {platform: packet_transport, id: rbu_b, transport_id: pt_udp, provider: "${peer_b}", remote_id: bin}
- {platform: packet_transport, id: stu_p, transport_id: pt_udp, provider: "${peer_p}", type: status, on_state: [logger.log: {format: "PTSTATUS udp %d", args: ["x ? 1 : 0"]}]}
- {platform: packet_transport, id: stu_b, transport_id: pt_udp, provider: "${peer_b}", type: status, on_state: [logger.log: {format: "PTSTATUS udp %d", args: ["x ? 1 : 0"]}]}
@@ -0,0 +1,18 @@
substitutions:
name: ud32p
peer: ud66p
peer_ip: 192.0.2.66 # the D1 mini peer's address
host_ip: 192.0.2.10 # the computer running udpraw.py
bcast_ip: 192.0.2.255 # the subnet broadcast address
my_addr: "0x0032"
peer_addr: "0x0066"
stream_interval: 2s
rel_timeout: 1s
tick: 5s
long_id: "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"
esp32:
board: esp32dev
framework:
type: esp-idf
packages:
link: !include udplink.yaml
@@ -0,0 +1,287 @@
# UDP packet_interface point to point test (esphome#21120), shared part. Joins Wi-Fi; logs on serial.
# u1 p2p : one unicast address (the peer) -> point to point; xxtea + reliability + session + fragmentation
# u2 bc : the subnet broadcast address (ends in .255) -> addressing; bc_rel (rolling code) and bc_all ports
# u3 mc : multicast -> addressing, destination broadcast
# u4 multi: the peer and the test host -> several addresses -> addressing, destination broadcast
# u5 p2pf : the peer, point_to_point: false -> accepts every sender, raw stream
# u6 p2pt : the subnet broadcast address, point_to_point: true -> only from that address, so the peer is dropped
# substitutions: name, peer, peer_ip, host_ip, bcast_ip, my_addr, peer_addr, long_id
esphome:
name: ${name}
includes:
- entest.hpp
on_boot:
- priority: 600
then:
- lambda: |-
entest::P2P.listen(id(p2p)->top());
entest::BCR.listen(id(bc_rel)->top());
entest::P2PF.listen(id(p2pf));
entest::P2PT.listen(id(p2pt));
logger:
level: DEBUG
baud_rate: 115200
wifi:
ssid: !secret wifi_ssid
password: !secret wifi_password
power_save_mode: none
on_connect:
- lambda: |-
char b[esphome::network::IP_ADDRESS_BUFFER_SIZE];
ESP_LOGI("main", "WIFI IP %s", esphome::network::get_ip_addresses()[0].str_to(b));
globals:
- {id: counter, type: int, initial_value: "0"}
udp:
- {id: u1, port: 21121, addresses: ["${peer_ip}"]}
- {id: u2, port: 21122, addresses: ["${bcast_ip}"]}
- {id: u3, port: 21123, addresses: ["239.0.60.53"], listen_address: 239.0.60.53}
- {id: u4, port: 21124, addresses: ["${peer_ip}", "${host_ip}"]}
- {id: u5, port: 21125, addresses: ["${peer_ip}"]}
- {id: u6, port: 21126, addresses: ["${bcast_ip}"]}
packet_interface:
- platform: udp
id: p2p
udp_id: u1
encryption: p2p key 21120
reliability:
max_queue_length: 6
timeout: ${rel_timeout}
session:
keep_alive: 2s
timeout: 8s
binary_sensor:
id: p2p_sess
on_state: [logger.log: {format: "SESS p2p %d", args: ["x ? 1 : 0"]}]
fragmentation:
max_packet_size: 1024
- platform: udp
id: bc
udp_id: u2
addressing:
address: ${my_addr}
peers:
- id: bc_rel
destination: ${peer_addr}
encryption:
key: bc key 21120
rolling_code_enable: true
reliability:
max_queue_length: 6
timeout: ${rel_timeout}
session:
keep_alive: 2s
timeout: 8s
binary_sensor:
id: bcrel_sess
on_state: [logger.log: {format: "SESS bc_rel %d", args: ["x ? 1 : 0"]}]
fragmentation:
max_packet_size: 1024
- id: bc_all
destination: broadcast
- platform: udp
id: mc
udp_id: u3
addressing:
address: ${my_addr}
destination: broadcast
- platform: udp
id: multi
udp_id: u4
addressing:
address: ${my_addr}
destination: broadcast
- platform: udp
id: p2pf
udp_id: u5
point_to_point: false
- platform: udp
id: p2pt
udp_id: u6
point_to_point: true
interval:
- interval: ${tick}
then:
- lambda: id(counter)++;
- interval: ${stream_interval}
then:
- lambda: |-
static const uint16_t RSIZES[] = {12, 200, 600};
entest::P2P.send(id(p2p)->top(), RSIZES[entest::P2P.idx++ % 3]);
entest::BCR.send(id(bc_rel)->top(), RSIZES[entest::BCR.idx++ % 3]);
entest::P2PF.send(id(p2pf), 100);
entest::P2PT.send(id(p2pt), 100);
- interval: 10s
then:
- lambda: |-
entest::P2P.log();
entest::BCR.log();
entest::P2PF.log();
entest::P2PT.log();
ESP_LOGI("stat", "STAT cnt=%d sess=%d/%d", id(counter), id(p2p_sess).state, id(bcrel_sess).state);
sensor:
- platform: template
id: cnt
lambda: return id(counter);
update_interval: 1s
- platform: template
id: flt
lambda: |-
int t = id(counter);
return 12345.678f + t * 0.001f - (t % 7) * 1000.5f;
update_interval: 1s
accuracy_decimals: 3
- platform: template
id: longa
lambda: return id(counter) * 0.5f;
update_interval: 5s
# p2p
- platform: entity_transport
id: rx1
entity_transport_id: et_p2p
provider: ${peer}
remote_id: cnt
on_value: [logger.log: {format: "ETRX p2p cnt=%.0f", args: [x]}]
- platform: entity_transport
id: rx2
entity_transport_id: et_p2p
provider: ${peer}
remote_id: flt
on_value: [logger.log: {format: "ETRX p2p flt=%.3f", args: [x]}]
- platform: entity_transport
id: rx3
entity_transport_id: et_p2p
provider: ${peer}
remote_id: ${long_id}8
on_value: [logger.log: {format: "ETRX p2p long=%.1f", args: [x]}]
# bc_rel
- platform: entity_transport
id: rx4
entity_transport_id: et_bcrel
provider: ${peer}
remote_id: cnt
on_value: [logger.log: {format: "ETRX bcrel cnt=%.0f", args: [x]}]
- platform: entity_transport
id: rx5
entity_transport_id: et_bcrel
provider: ${peer}
remote_id: flt
on_value: [logger.log: {format: "ETRX bcrel flt=%.3f", args: [x]}]
- platform: entity_transport
id: rx6
entity_transport_id: et_bcrel
provider: ${peer}
remote_id: ${long_id}8
on_value: [logger.log: {format: "ETRX bcrel long=%.1f", args: [x]}]
# bc_all
- platform: entity_transport
id: rx7
entity_transport_id: et_bcall
provider: ${peer}
remote_id: cnt
on_value: [logger.log: {format: "ETRX bcall cnt=%.0f", args: [x]}]
- platform: entity_transport
id: rx8
entity_transport_id: et_bcall
provider: ${peer}
remote_id: flt
on_value: [logger.log: {format: "ETRX bcall flt=%.3f", args: [x]}]
- platform: entity_transport
id: rx_mc_cnt
entity_transport_id: et_mc
provider: ${peer}
remote_id: cnt
on_value: [logger.log: {format: "ETRX mc cnt=%.0f", args: [x]}]
- platform: entity_transport
id: rx_mc_flt
entity_transport_id: et_mc
provider: ${peer}
remote_id: flt
on_value: [logger.log: {format: "ETRX mc flt=%.3f", args: [x]}]
- platform: entity_transport
id: rx_multi_cnt
entity_transport_id: et_multi
provider: ${peer}
remote_id: cnt
on_value: [logger.log: {format: "ETRX multi cnt=%.0f", args: [x]}]
- platform: entity_transport
id: rx_multi_flt
entity_transport_id: et_multi
provider: ${peer}
remote_id: flt
on_value: [logger.log: {format: "ETRX multi flt=%.3f", args: [x]}]
binary_sensor:
- platform: template
id: bin
lambda: return (id(counter) / 3) % 2;
- platform: entity_transport
id: rx9
entity_transport_id: et_p2p
provider: ${peer}
remote_id: bin
on_state: [logger.log: {format: "ETRX p2p bin=%d", args: ["x ? 1 : 0"]}]
- platform: entity_transport
id: rx10
entity_transport_id: et_bcrel
provider: ${peer}
remote_id: bin
on_state: [logger.log: {format: "ETRX bcrel bin=%d", args: ["x ? 1 : 0"]}]
- platform: entity_transport
id: rx11
entity_transport_id: et_bcall
provider: ${peer}
remote_id: bin
on_state: [logger.log: {format: "ETRX bcall bin=%d", args: ["x ? 1 : 0"]}]
- platform: entity_transport
id: rx_mc_bin
entity_transport_id: et_mc
provider: ${peer}
remote_id: bin
on_state: [logger.log: {format: "ETRX mc bin=%d", args: ["x ? 1 : 0"]}]
- platform: entity_transport
id: rx_multi_bin
entity_transport_id: et_multi
provider: ${peer}
remote_id: bin
on_state: [logger.log: {format: "ETRX multi bin=%d", args: ["x ? 1 : 0"]}]
entity_transport:
- id: et_p2p
packet_interface: p2p
sensors: &big_sensors
- cnt
- flt
- {id: longa, broadcast_id: "${long_id}1"}
- {id: longa, broadcast_id: "${long_id}2"}
- {id: longa, broadcast_id: "${long_id}3"}
- {id: longa, broadcast_id: "${long_id}4"}
- {id: longa, broadcast_id: "${long_id}5"}
- {id: longa, broadcast_id: "${long_id}6"}
- {id: longa, broadcast_id: "${long_id}7"}
- {id: longa, broadcast_id: "${long_id}8"}
binary_sensors: [bin]
- id: et_bcrel
packet_interface: bc_rel
sensors: *big_sensors
binary_sensors: [bin]
- id: et_bcall
packet_interface: bc_all
sensors: [cnt, flt]
binary_sensors: [bin]
- id: et_mc
packet_interface: mc
sensors: [cnt, flt]
binary_sensors: [bin]
- id: et_multi
packet_interface: multi
sensors: [cnt, flt]
binary_sensors: [bin]
@@ -0,0 +1,31 @@
"""udpraw.py SECONDS HOST [HOST...]: every second send a 16 byte datagram from this host to ports 21121, 21125 and
21126 of each HOST, and count datagrams arriving on port 21124 (the boards' several-address link). Exits 1 when none
arrived."""
import socket
import sys
import time
# Not the magic of any entest stream, so a link that accepts third-party datagrams counts these as foreign
MAGIC = b"\xa5\x5d"
secs, hosts = float(sys.argv[1]), sys.argv[2:]
tx = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
rx = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
rx.bind(("0.0.0.0", 21124))
rx.settimeout(0.05)
end, nxt, sent, got, src = time.time() + secs, 0, 0, 0, {}
while time.time() < end:
if time.time() >= nxt:
for h in hosts:
for p in (21121, 21125, 21126):
tx.sendto(MAGIC + bytes(14), (h, p))
sent += 1
nxt = time.time() + 1
try:
d, a = rx.recvfrom(2048)
got += 1
src[a[0]] = src.get(a[0], 0) + 1
except TimeoutError:
pass
print(f"sent={sent} received_on_21124={got} by_source={src}")
sys.exit(0 if got else "FAIL nothing arrived on port 21124")
@@ -0,0 +1,10 @@
substitutions:
name: ur32p
peer_ip: 192.0.2.66 # the D1 mini peer's address
stream_interval: 500ms
esp32:
board: esp32dev
framework:
type: esp-idf
packages:
link: !include urlink.yaml
@@ -0,0 +1,36 @@
# Minimal UDP A/B: one udp component sending to the peer only, a raw packet_interface link (point to point on the
# PR, accept-all on the base) with a checked stream, and a plain udp on_receive counter.
esphome:
name: ${name}
includes: [entest.hpp]
on_boot:
- priority: 600
then:
- lambda: entest::P2PF.listen(id(lnk));
logger:
level: DEBUG
wifi:
ssid: !secret wifi_ssid
password: !secret wifi_password
power_save_mode: none
globals:
- {id: onrx, type: uint32_t, initial_value: "0"}
udp:
- id: u5
port: 21125
addresses: ["${peer_ip}"]
on_receive:
- lambda: id(onrx)++;
packet_interface:
- platform: udp
id: lnk
udp_id: u5
interval:
- interval: ${stream_interval}
then:
- lambda: entest::P2PF.send(id(lnk), 100);
- interval: 10s
then:
- lambda: |-
entest::P2PF.log();
ESP_LOGI("stat", "STAT on_receive=%u", id(onrx));
@@ -0,0 +1,139 @@
"""Writes one config per validation case into out-valid/; expected outcome in the file name prefix (ok_ / bad_).
Run `esphome config` on each from the tree under test.
"""
import pathlib
import textwrap
OUT = pathlib.Path(__file__).parent / "out-valid"
E32 = "esp32:\n board: esp32dev\n framework:\n type: esp-idf\n"
E66 = "esp8266:\n board: d1_mini\n"
WIFI = "wifi:\n ssid: x\n password: yyyyyyyy\n"
def write(name: str, plat: str, middle: str, link: str) -> None:
node = name.replace("_", "-")[:31]
(OUT / f"{name}.yaml").write_text(
f"esphome:\n name: {node}\n{plat}{middle}packet_interface:\n"
+ textwrap.indent(textwrap.dedent(link), " ").replace(" ", " - ", 1),
encoding="utf-8",
)
def en(name: str, link: str, plat: str = E32, extra: str = "") -> None:
write(
name,
plat,
f"espnow:\n channel: 1\n{extra}",
"platform: espnow\nid: l\n" + link,
)
def udp(name: str, addrs: str, link: str, plat: str = E32) -> None:
write(
name,
plat,
f"{WIFI}udp:\n - id: u\n addresses: {addrs}\n",
"platform: udp\nid: l\nudp_id: u\n" + link,
)
OUT.mkdir(exist_ok=True)
ADDR = "addressing:\n address: 1\n destination: 2\n"
ADDR_BC = "addressing:\n address: 1\n destination: broadcast\n"
# ESP-NOW
en("bad_en_peer_addressing", "peer_address: 12:22:33:44:55:66\n" + ADDR)
en(
"ok_en_peer_rel_sess",
"peer_address: 12:22:33:44:55:66\nreliability:\nsession:\nencryption:\n key: k\n rolling_code_enable: true\n",
)
en("ok_en_bc_addressing", ADDR)
en("bad_en_bc_reliability", "reliability:\n")
en("bad_en_bc_session", "session:\n")
en("bad_en_bc_rolling", "encryption:\n key: k\n rolling_code_enable: true\n")
en("ok_en_bc_plain_encryption", "encryption: k\n")
en(
"ok_en_bc_addr_rel_sess_rolling",
ADDR
+ "reliability:\nsession:\nencryption:\n key: k\n rolling_code_enable: true\n",
)
en("bad_en_bc_addr_bcdest_reliability", ADDR_BC + "reliability:\n")
en("ok_en_size_250", "max_packet_size: 250\n")
en("bad_en_size_251_payload_250", "max_packet_size: 251\n")
en(
"ok_en_size_1470_payload_1470",
"max_packet_size: 1470\n",
extra=" max_payload_size: 1470\n",
)
en(
"bad_en_size_1471_payload_1470",
"max_packet_size: 1471\n",
extra=" max_payload_size: 1470\n",
)
en(
"bad_en_size_1000_payload_800",
"max_packet_size: 1000\n",
extra=" max_payload_size: 800\n",
)
en(
"bad_en8266_payload_300",
"max_packet_size: 300\n",
plat=E66,
extra=" max_payload_size: 300\n",
)
en("ok_en8266_peer_rel", "peer_address: 12:22:33:44:55:66\nreliability:\n", plat=E66)
# UDP
udp("bad_udp_p2p_addressing", '["192.0.2.60"]', ADDR)
udp(
"ok_udp_p2p_rel_sess_rolling",
'["192.0.2.60"]',
"reliability:\nsession:\nencryption:\n key: k\n rolling_code_enable: true\n",
)
udp("ok_udp_255_addressing", '["192.0.2.255"]', ADDR)
udp("bad_udp_255_reliability", '["192.0.2.255"]', "reliability:\n")
udp("bad_udp_255_session", '["192.0.2.255"]', "session:\n")
udp(
"bad_udp_255_rolling",
'["192.0.2.255"]',
"encryption:\n key: k\n rolling_code_enable: true\n",
)
udp("ok_udp_255_addr_rel", '["192.0.2.255"]', ADDR + "reliability:\nsession:\n")
udp("bad_udp_mcast_reliability", '["239.0.60.53"]', "reliability:\n")
udp("ok_udp_mcast_addressing", '["239.0.60.53"]', ADDR_BC)
udp("bad_udp_two_session", '["192.0.2.60", "192.0.2.61"]', "session:\n")
udp("ok_udp_two_addressing", '["192.0.2.60", "192.0.2.61"]', ADDR)
udp("ok_udp_default_bcast_addressing", '["255.255.255.255"]', ADDR_BC)
udp("bad_udp_default_bcast_reliability", '["255.255.255.255"]', "reliability:\n")
udp(
"bad_udp_p2p_true_255_addressing",
'["192.0.2.255"]',
"point_to_point: true\n" + ADDR,
)
udp(
"ok_udp_p2p_true_255_reliability",
'["192.0.2.255"]',
"point_to_point: true\nreliability:\n",
)
udp(
"ok_udp_p2p_false_unicast_addressing",
'["192.0.2.60"]',
"point_to_point: false\n" + ADDR,
)
udp(
"bad_udp_p2p_false_unicast_reliability",
'["192.0.2.60"]',
"point_to_point: false\nreliability:\n",
)
udp("bad_udp_p2p_true_mcast", '["239.0.60.53"]', "point_to_point: true\n")
udp("bad_udp_p2p_true_two", '["192.0.2.60", "192.0.2.61"]', "point_to_point: true\n")
udp("ok_udp_p2p_8266", '["192.0.2.60"]', "reliability:\nsession:\n", plat=E66)
# peer address and all-ones broadcast cases
en("bad_en_peer_group_mac", "peer_address: 01:22:33:44:55:66\n")
en("bad_en_peer_zero_mac", "peer_address: 00:00:00:00:00:00\n")
en(
"ok_en_peer_explicit_broadcast_addressing",
"peer_address: FF:FF:FF:FF:FF:FF\n" + ADDR,
)
udp("bad_udp_allones_p2p_true", '["255.255.255.255"]', "point_to_point: true\n")
udp("ok_udp_allones_addressing", '["255.255.255.255"]', ADDR)
@@ -0,0 +1,140 @@
"""compare_spi.py BASE PR: compare two SX127x SPI record files (sx127x_spi_record_test.cpp or the device sim).
Each file is a host record file or a device serial log with sxrec.hpp's "REC ..." lines (the log prefix is cut at
"REC " and "REC +" continuation lines are joined). A hardware bus log fails when its WIRE line counts differing or
uncaptured chip select windows.
Register files and FIFO bytes must match exactly. Each transaction is flattened into per-register operations with
burst auto-increment (the FIFO address 0x00 does not move); the flattened streams must match, so the raw streams can
differ only in how the operations are grouped into chip select windows.
"""
from collections import Counter
import pathlib
import re
import sys
ANSI = re.compile(r"\x1b\[[0-9;]*m")
def lines(path: str) -> list[str]:
"""The record lines of a host record file, or only the "REC" lines of a device log."""
plain: list[str] = []
rec: list[str] = []
with pathlib.Path(path).open(encoding="utf-8", errors="replace") as fh:
for raw in fh:
line = ANSI.sub("", raw).strip()
if (i := line.find("REC ")) < 0:
plain.append(line)
elif (body := line[i + 4 :]).startswith("+"):
if rec:
rec[-1] += body[1:]
else:
rec.append(body)
return rec or plain
def wire_check(path: str) -> None:
"""A hardware bus log (rechw) must show every chip select window captured and equal to what the driver sent."""
text = pathlib.Path(path).read_text(encoding="utf-8", errors="replace")
for m in re.finditer(
r"WIRE ok=(\d+) differ=(\d+) over64=\d+ no_capture=(\d+)", text
):
if int(m[2]) or int(m[3]):
sys.exit(f"{path}: wire check failed: {m[0]}")
def parse(path: str) -> dict[str, dict]:
cases: dict[str, dict] = {}
cur = None
for line in lines(path):
if line.startswith("CASE "):
if line[5:] in cases:
sys.exit(f"{path}: case {line[5:]!r} appears twice")
cur = {"T": [], "REGS": None, "FIFO": None, "END": False}
cases[line[5:]] = cur
elif cur is None:
continue
elif line.startswith("T"):
cur["T"].append([int(x, 16) for x in line[1:].split()])
elif line.startswith("REGS"):
cur["REGS"] = line[4:].strip()
elif line.startswith("FIFO"):
cur["FIFO"] = line[4:].strip()
elif line == "END":
cur["END"] = True
if not cases:
sys.exit(f"{path}: no CASE records")
if partial := [
n
for n, c in cases.items()
if c["REGS"] is None or c["FIFO"] is None or not c["END"]
]:
sys.exit(
f"{path}: {len(partial)} cases without REGS, FIFO or END, first {partial[0]!r}"
)
return cases
def flatten(transactions: list[list[int]]) -> list[tuple]:
ops = []
for t in transactions:
if not t:
continue
addr, write = t[0] & 0x7F, bool(t[0] & 0x80)
for value in t[1:]:
ops.append(("W" if write else "R", addr, value if write else None))
if addr != 0:
addr = (addr + 1) & 0x7F
return ops
def burst_shape(transactions: list[list[int]]) -> Counter:
"""Writes of more than one byte to normal registers, by start address and length."""
return Counter(
(t[0] & 0x7F, len(t) - 1)
for t in transactions
if t and t[0] & 0x80 and t[0] & 0x7F != 0 and len(t) > 2
)
def main() -> None:
for path in sys.argv[1:3]:
wire_check(path)
base, pr = parse(sys.argv[1]), parse(sys.argv[2])
if base.keys() != pr.keys():
print("only in base:", sorted(base.keys() - pr.keys()))
print("only in pr:", sorted(pr.keys() - base.keys()))
sys.exit("different case lists")
bad = 0
raw_same = 0
tb = tp = 0
shapes: Counter = Counter()
fifo_tx = Counter()
for name in base:
b, p = base[name], pr[name]
if b["REGS"] != p["REGS"] or b["FIFO"] != p["FIFO"]:
bad += 1
print("REGISTER/FIFO MISMATCH", name)
if flatten(b["T"]) != flatten(p["T"]):
bad += 1
print("FLATTENED MISMATCH", name)
raw_same += b["T"] == p["T"]
tb += len(b["T"])
tp += len(p["T"])
shapes += burst_shape(p["T"])
fifo_tx[
(
sum(1 for t in b["T"] if t and t[0] == 0x80),
sum(1 for t in p["T"] if t and t[0] == 0x80),
)
] += 1
print(f"cases={len(base)} mismatches={bad} raw_identical={raw_same}")
print(f"transactions base={tb} pr={tp}")
print("PR multi-byte register bursts (start, bytes): count", dict(shapes))
print("FIFO write transactions per case (base, pr): count", dict(fifo_tx))
sys.exit(1 if bad else 0)
if __name__ == "__main__":
main()
@@ -0,0 +1,269 @@
"""Write the two-radio air test configs for esphome#21110: gen_pair.py OUTDIR.
lpair: two SX127x in LoRa; fpair: two SX127x in FSK (63 byte packets); xpair: SX127x (A) and SX126x (B) in LoRa.
Each has raw links, reliability links and session plus rolling code links, with entity_transport both ways, run by
sxlink.hpp from a 20 ms interval. Only the interrupt pads (GPIO4 for A, GPIO22 for B) are driven; the SPI, CS, reset and
busy pins are swapped for fake pins at on_boot 1100 and never set up. The shared headers are copied into OUTDIR.
"""
from pathlib import Path
import shutil
import sys
HEADERS = ("spibits.hpp", "sxair.hpp", "sxlink.hpp")
SX127X_LORA = """\
frequency: 917MHz
modulation: LORA
bandwidth: 125_0kHz
spreading_factor: 7
coding_rate: CR_4_5
preamble_size: 8
crc_enable: true
rx_start: true"""
SX127X_FSK = """\
frequency: 917MHz
modulation: FSK
bitrate: 9600
deviation: 5kHz
bandwidth: 25_0kHz
bitsync: true
packet_mode: true
sync_value: [0x33, 0x33]
preamble_size: 4
preamble_detect: 2
crc_enable: true
rx_start: true"""
SX126X_LORA = """\
hw_version: sx1262
rf_switch: true
frequency: 917MHz
modulation: LORA
bandwidth: 125_0kHz
spreading_factor: 7
coding_rate: CR_4_5
preamble_size: 8
crc_enable: true
rx_start: true"""
def pin(num: int, indent: int = 4) -> str:
pad = " " * indent
return f"\n{pad}number: GPIO{num}\n{pad}allow_other_uses: true"
def radio(rid: str, bus: str, dio: int, body: str, sx126x: bool) -> str:
"""One radio entry: SX126x radios get a busy pin and DIO1, SX127x radios DIO0."""
busy = " busy_pin: GPIO36\n" if sx126x else ""
return f"""\
- id: {rid}
spi_id: {bus}
cs_pin:{pin(5, indent=6)}
rst_pin:{pin(21, indent=6)}
{busy} dio{1 if sx126x else 0}_pin:
number: GPIO{dio}
mode:
input: true
pulldown: true
{chr(10).join(" " + line for line in body.splitlines())}
on_packet:
then:
- lambda: 'sxlink::DRV[{0 if rid == "radio_a" else 1}]++;'
"""
def link(lid: str, plat: str, key: str, radio: str, size: int, layers: str = "") -> str:
out = f" - platform: {plat}\n id: {lid}\n {key}: {radio}\n max_packet_size: {size}\n"
return out + layers
def layers(addr: int, dest: int, session: bool) -> str:
out = f"""\
addressing:
address: {addr}
destination: {dest}
"""
if session:
out += """\
encryption:
key: Johnny Cash
rolling_code_enable: true
"""
out += """\
reliability:
ack_delay: 100ms
"""
if session:
out += f"""\
session:
binary_sensor:
name: session {addr}
"""
return out
def config(name: str) -> tuple[str, str]:
fsk = name.startswith("fpair")
mixed = name.startswith("xpair")
size = 63 if fsk else 255
body_a = SX127X_FSK if fsk else SX127X_LORA
radios = "sx127x:\n" + radio("radio_a", "bus_a", 4, body_a, False)
if mixed:
radios += "sx126x:\n" + radio("radio_b", "bus_b", 22, SX126X_LORA, True)
b_ns, b_class = "sx126x", "SX126x"
b_install = 'install_sx126x(id(bus_b), id(radio_b), &sxpair::MB, &sxpair::PB, &sxpair::RSTB, "B", 22)'
else:
radios += radio("radio_b", "bus_b", 22, body_a, False)
b_ns, b_class = "sx127x", "SX127x"
b_install = (
'install_sx127x(id(bus_b), id(radio_b), &sxpair::MB, &sxpair::PB, "B", 22)'
)
b_plat, b_key = b_ns, f"{b_ns}_id"
b_model = f"sxair::{b_class.capitalize()}"
b_err = f"{b_ns}::{b_class}Error::{b_class.upper()}_ERROR_NONE"
b_listener = f"{b_ns}::{b_class}TransmitListener"
b_errtype = f"{b_ns}::{b_class}Error"
hdr = f'#include "esphome/components/{b_ns}/{b_ns}.h"' if mixed else ""
links = "packet_interface:\n"
links += link("raw_a", "sx127x", "sx127x_id", "radio_a", size)
links += link("raw_b", b_plat, b_key, "radio_b", size)
links += link("rel_a", "sx127x", "sx127x_id", "radio_a", size, layers(1, 2, False))
links += link("rel_b", b_plat, b_key, "radio_b", size, layers(2, 1, False))
links += link("ses_a", "sx127x", "sx127x_id", "radio_a", size, layers(3, 4, True))
links += link("ses_b", b_plat, b_key, "radio_b", size, layers(4, 3, True))
glue = f"""\
#pragma once
#include "sxlink.hpp"
#include "esphome/components/sx127x/sx127x.h"
{hdr}
namespace sxpair {{
using namespace esphome;
static sxair::Sx127x MA;
static {b_model} MB;
static sxair::PinSet PA, PB;
static sxair::Sx126xRst RSTB;
struct TxL : {b_listener} {{
void on_transmit_complete({b_errtype} r) override {{ ESP_LOGI("ev", "EV peer async done result=%d", (int) r); }}
}} TL;
static {b_ns}::{b_class} *RB = nullptr;
inline bool b_async(const uint8_t *d, size_t n) {{ return RB->start_transmit_async(&TL, d, n) == {b_err}; }}
}} // namespace sxpair
"""
flags = "\n platformio_options:\n build_flags: -DSXL_FSK" if fsk else ""
yaml = f"""\
# esphome#21110 air test ({name}), generated by gen_pair.py
esphome:
name: {name}
includes: [spibits.hpp, sxair.hpp, sxlink.hpp, {name}_glue.h]{flags}
on_boot:
- priority: 1100
then:
- lambda: |-
sxair::install_sx127x(id(bus_a), id(radio_a), &sxpair::MA, &sxpair::PA, "A", 4);
sxair::{b_install};
sxpair::RB = id(radio_b);
auto &c = sxlink::C;
c.raw_a = id(raw_a); c.raw_b = id(raw_b);
c.links[0] = id(raw_a); c.links[1] = id(raw_b); c.links[2] = id(rel_a);
c.links[3] = id(rel_b); c.links[4] = id(ses_a); c.links[5] = id(ses_b); c.nlinks = 6;
c.ma = &sxpair::MA; c.mb = &sxpair::MB;
c.sa = id(sens_a); c.sb = id(sens_b); c.sa2 = id(sens_a2); c.sb2 = id(sens_b2);
c.ra_on_b = id(rem_a_on_b); c.rb_on_a = id(rem_b_on_a);
c.ra2_on_b = id(rem_a2_on_b); c.rb2_on_a = id(rem_b2_on_a);
c.b_async = sxpair::b_async;
sxlink::RXA.attach(id(raw_a));
sxlink::RXB.attach(id(raw_b));
sxlink::next_ms = 8000;
- priority: 300
then:
- lambda: sxair::late_pins();
esp32:
board: pico32
framework:
type: esp-idf
logger:
level: VERBOSE
tx_buffer_size: 1024
logs:
component: DEBUG
scheduler: DEBUG
sensor: DEBUG
spi_device: DEBUG
sx127x: DEBUG
sx126x: DEBUG
preferences: DEBUG
spi:
- id: bus_a
interface: software
clk_pin:{pin(18, indent=6)}
mosi_pin:{pin(23, indent=6)}
miso_pin:{pin(19, indent=6)}
- id: bus_b
interface: software
clk_pin:{pin(18, indent=6)}
mosi_pin:{pin(23, indent=6)}
miso_pin:{pin(19, indent=6)}
{radios}{links}
entity_transport:
- id: et_a
packet_interface: rel_a
sensors: [sens_a]
- id: et_b
packet_interface: rel_b
sensors: [sens_b]
- id: et_a2
packet_interface: ses_a
sensors: [sens_a2]
- id: et_b2
packet_interface: ses_b
sensors: [sens_b2]
sensor:
"""
for sid in ("sens_a", "sens_b", "sens_a2", "sens_b2"):
yaml += f"""\
- platform: template
id: {sid}
update_interval: never
"""
for rid, et, remote in (
("rem_a_on_b", "et_b", "sens_a"),
("rem_b_on_a", "et_a", "sens_b"),
("rem_a2_on_b", "et_b2", "sens_a2"),
("rem_b2_on_a", "et_a2", "sens_b2"),
):
yaml += f"""\
- platform: entity_transport
id: {rid}
entity_transport_id: {et}
provider: {name}
remote_id: {remote}
on_value:
- lambda: 'ESP_LOGI("ev", "EV remote {rid} = %.1f", x);'
"""
yaml += """\
interval:
- interval: 20ms
then:
- lambda: sxlink::run();
"""
return yaml, glue
def main() -> None:
out = Path(sys.argv[1])
out.mkdir(parents=True, exist_ok=True)
# ESPHome resolves includes from the config's directory, so the shared headers go next to the configs
here = Path(__file__).resolve().parent
if out.resolve() != here:
for header in HEADERS:
shutil.copyfile(here / header, out / header)
for name in ("lpair", "fpair", "xpair"):
yaml, glue = config(name)
(out / f"{name}.yaml").write_text(yaml)
(out / f"{name}_glue.h").write_text(glue)
if __name__ == "__main__":
main()
@@ -0,0 +1,46 @@
# esphome#22776 SPI recorder (sxrec.hpp) on the ESP32-PICO-D4. Build rechw.yaml (ESP-IDF hardware bus, tee and SPI3
# sniffer) or recsw.yaml (software bus with fake pins) with -s name <name>; they set the substitutions and include this.
esphome:
name: ${name}
includes: [spibits.hpp, sxrec.hpp]
platformio_options:
build_flags: ${flag}
on_boot:
- priority: ${prio}
then:
- lambda: sxrec::${install}(id(bus), id(radio), 4);
- priority: 300
then:
- lambda: sxrec::late_pins();
- priority: -100
then:
- delay: 3s
- lambda: sxrec::sweep(id(radio));
esp32:
board: pico32
framework:
type: esp-idf
logger:
level: DEBUG
tx_buffer_size: 1024
spi:
id: bus
interface: ${interface}
clk_pin: GPIO18
mosi_pin: GPIO23
miso_pin: GPIO19
sx127x:
id: radio
cs_pin: GPIO5
rst_pin: GPIO21
dio0_pin:
number: GPIO4
mode:
input: true
pulldown: true
frequency: 433920000
modulation: LORA
bandwidth: 125_0kHz
preamble_size: 8
crc_enable: true
rx_start: true
@@ -0,0 +1,7 @@
substitutions:
flag: -DSXREC_HW
prio: "800"
install: install_hw
interface: spi2
packages:
rec: !include rec.yaml
@@ -0,0 +1,7 @@
substitutions:
flag: -DSXREC_SW
prio: "1100"
install: install_sw
interface: software
packages:
rec: !include rec.yaml
@@ -0,0 +1,91 @@
#pragma once
// Fake SPI pins for a software SPI bus: SPIDelegateBitBash clocks every bit through MosiPin/MisoPin, and CsPin frames
// the chip select windows, so a chip model (SpiChip) sees each byte and answers per byte position. No ESP-IDF
// includes, so it also builds on the host. lock()/unlock() guard the model hooks (a portMUX on the ESP32); after_end()
// runs after the unlock at the end of a window, for work that must not run inside a critical section (esp_timer).
#include <cstdint>
#include "esphome/core/gpio_pin.h"
namespace spibits {
using namespace esphome;
class NopPin : public GPIOPin {
public:
void setup() override {}
void pin_mode(gpio::Flags) override {}
gpio::Flags get_flags() const override { return gpio::FLAG_NONE; }
bool digital_read() override { return false; }
void digital_write(bool) override {}
};
// One chip select window: bytes in, a reply per byte position
struct SpiChip {
bool sel{false}, mosi{false};
uint16_t pos{0};
uint8_t bit{0}, in{0}, out{0};
virtual ~SpiChip() = default;
virtual uint8_t reply_locked(uint16_t pos) = 0;
virtual void byte_locked(uint16_t pos, uint8_t value) = 0;
virtual void begin_locked() {}
virtual void end_locked() = 0;
virtual void after_end() {}
virtual void lock() {}
virtual void unlock() {}
};
class MosiPin : public NopPin {
public:
SpiChip *c{nullptr};
void digital_write(bool v) override { c->mosi = v; }
};
class CsPin : public NopPin {
public:
SpiChip *c{nullptr};
void digital_write(bool high) override {
c->lock();
const bool ended = high && c->sel;
if (!high) {
c->sel = true;
c->pos = c->bit = c->in = 0;
c->begin_locked();
} else if (c->sel) {
c->sel = false;
c->end_locked();
}
c->unlock();
if (ended)
c->after_end();
}
};
class MisoPin : public NopPin {
public:
SpiChip *c{nullptr};
bool digital_read() override {
if (c->bit == 0) {
c->lock();
c->out = c->reply_locked(c->pos);
c->unlock();
}
const bool b = (c->out >> (7 - c->bit)) & 1;
c->in = (c->in << 1) | (c->mosi ? 1 : 0);
if (++c->bit == 8) {
c->lock();
c->byte_locked(c->pos, c->in);
c->unlock();
c->bit = 0;
c->in = 0;
c->pos++;
}
return b;
}
};
struct PinSet {
NopPin clk;
MosiPin mosi;
MisoPin miso;
CsPin cs;
NopPin rst;
NopPin busy;
void bind(SpiChip *c) { mosi.c = miso.c = cs.c = c; }
};
} // namespace spibits
@@ -0,0 +1,218 @@
// Host harness for SX127x SPI A/B checks (esphome#22776): drop into tests/components/sx127x/ of each tree and run
// script/cpp_unit_test.py sx127x with SX127X_RECORD=<file>. For a sweep of FSK and LoRa settings it records every SPI
// transaction (setup, configure() with other settings, a transmit) and the final register file of a fake chip with
// burst auto-increment (FIFO address does not increment). compare_spi.py diffs two record files.
#include <gtest/gtest.h>
#include <cstdio>
#include <cstdlib>
#include <string>
#include <vector>
#include "esphome/components/sx127x/sx127x.h"
#include "esphome/core/application.h"
namespace esphome::sx127x::testing {
class RecPin : public GPIOPin {
public:
void setup() override {}
void pin_mode(gpio::Flags) override {}
gpio::Flags get_flags() const override { return gpio::FLAG_NONE; }
size_t dump_summary(char *b, size_t n) const override { return snprintf(b, n, "rec"); }
bool digital_read() override { return this->on_read ? this->on_read() : this->level; }
void digital_write(bool v) override {
this->level = v;
if (this->on_write)
this->on_write(v);
}
bool level{false};
std::function<bool()> on_read;
std::function<void(bool)> on_write;
};
// Register file with burst auto-increment; records each chip select window as the bytes sent (and read)
class RecChip {
public:
RecChip() {
this->regs.fill(0);
this->regs[REG_OP_MODE] = MODE_STDBY;
cs.level = true;
cs.on_write = [this](bool high) {
if (!high) {
this->cur.clear();
this->pos = this->bit = this->in = 0;
} else {
this->log.push_back(this->cur);
}
};
miso.on_read = [this]() {
if (this->bit == 0)
this->out = this->reply_();
const bool b = (this->out >> (7 - this->bit)) & 1;
this->in = (this->in << 1) | mosi.level;
if (++this->bit == 8) {
this->byte_(this->in);
this->bit = 0;
this->in = 0;
this->pos++;
}
return b;
};
// The send completes at once
dio0.on_read = [this]() { return (this->regs[REG_OP_MODE] & MODE_MASK) == MODE_TX; };
spi.set_clk(&clk);
spi.set_mosi(&mosi);
spi.set_miso(&miso);
spi.call();
radio.set_spi_parent(&spi);
radio.set_cs_pin(&cs);
radio.set_dio0_pin(&dio0);
radio.set_rst_pin(&rst);
}
~RecChip() {
App.scheduler.cancel_interval(&this->radio, uint32_t{0});
this->radio.spi_teardown();
}
SX127x radio;
spi::SPIComponent spi;
RecPin cs, dio0, rst, clk, mosi, miso;
std::array<uint8_t, 128> regs{};
std::vector<uint8_t> fifo;
std::vector<std::vector<uint8_t>> log; // MOSI bytes of each transaction, with the read replies after a '|'
std::vector<uint8_t> cur;
uint8_t addr{0}, pos{0}, bit{0}, in{0}, out{0};
protected:
uint8_t reply_() {
if (this->pos == 0 || (this->addr & 0x80))
return 0;
const uint8_t a = this->addr & 0x7F;
if (a == REG_VERSION)
return 0x12;
if (a == REG_IMAGE_CAL)
return this->regs[a] & ~IMAGE_CAL_RUNNING;
return this->regs[a];
}
void byte_(uint8_t v) {
this->cur.push_back(v);
if (this->pos == 0) {
this->addr = v;
return;
}
if (!(this->addr & 0x80))
return;
const uint8_t a = this->addr & 0x7F;
if (a == REG_FIFO) {
this->fifo.push_back(v);
return; // the FIFO address does not move
}
this->regs[a] = v;
this->addr = 0x80 | ((a + 1) & 0x7F); // auto-increment
}
};
// The record file from SX127X_RECORD; nullptr when it is unset or cannot be opened (the test then fails)
static FILE *out_file() {
static FILE *f = nullptr;
if (f == nullptr) {
const char *path = std::getenv("SX127X_RECORD");
if (path != nullptr)
f = fopen(path, "w");
}
return f;
}
static void dump(const char *label, RecChip &c) {
static int index = 0;
FILE *f = out_file();
fprintf(f, "CASE %d %s\n", index++, label);
for (auto &t : c.log) {
fprintf(f, "T");
for (auto b : t)
fprintf(f, " %02X", b);
fprintf(f, "\n");
}
fprintf(f, "REGS");
for (auto r : c.regs)
fprintf(f, " %02X", r);
fprintf(f, "\nFIFO");
for (auto b : c.fifo)
fprintf(f, " %02X", b);
fprintf(f, "\nEND\n");
fflush(f);
}
static void settings(SX127x &r, bool lora, uint32_t freq, uint32_t bitrate, uint32_t dev, uint16_t pre) {
r.set_modulation(lora ? MOD_LORA : MOD_FSK);
r.set_frequency(freq);
r.set_bandwidth(lora ? SX127X_BW_125_0 : SX127X_BW_50_0);
r.set_afc_bandwidth(SX127X_BW_50_0);
r.set_bitrate(bitrate);
r.set_deviation(dev);
r.set_preamble_size(pre);
r.set_packet_mode(true);
r.set_payload_length(0);
r.set_rx_start(true);
r.set_pa_pin(PA_PIN_BOOST);
r.set_pa_power(17);
r.set_pa_ramp(PA_RAMP_40);
r.set_spreading_factor(7);
r.set_coding_rate(CODING_RATE_4_5);
r.set_crc_enable(true);
r.set_rx_floor(-94);
r.set_sync_value(lora ? std::vector<uint8_t>{0x12} : std::vector<uint8_t>{0x33, 0x33});
}
TEST(SX127xSpiRecord, Sweep) {
ASSERT_NE(out_file(), nullptr) << "set SX127X_RECORD to a writable record file";
const uint32_t freqs[] = {137000000, 169400000, 433920000, 525000000, 779000000, 868100000, 915000000, 1020000000};
const uint32_t rates[] = {500, 1200, 4800, 9600, 38400, 250000, 300000};
const uint32_t devs[] = {0, 600, 5000, 25000, 100000};
const uint16_t fsk_pre[] = {0, 1, 2, 255, 256, 4660, 65535};
const uint16_t lora_pre[] = {0, 5, 6, 8, 255, 256, 4660, 65535};
char label[96];
int n = 0;
for (bool lora : {false, true}) {
const uint16_t *pres = lora ? lora_pre : fsk_pre;
const size_t npre = lora ? sizeof(lora_pre) / 2 : sizeof(fsk_pre) / 2;
for (size_t fi = 0; fi < sizeof(freqs) / 4; fi++) {
for (size_t pi = 0; pi < npre; pi++) {
// bitrate and deviation only matter without LoRa; step through them with the other indexes
const uint32_t br = rates[(fi + pi) % (sizeof(rates) / 4)];
const uint32_t dv = devs[(fi * 3 + pi) % (sizeof(devs) / 4)];
RecChip c;
settings(c.radio, lora, freqs[fi], br, dv, pres[pi]);
c.radio.call(); // setup()
ASSERT_FALSE(c.radio.is_failed());
snprintf(label, sizeof(label), "%s setup f=%u br=%u dev=%u pre=%u", lora ? "lora" : "fsk", freqs[fi], br, dv,
pres[pi]);
dump(label, c);
// configure() with the next frequency, rate, deviation and preamble
c.log.clear();
const uint32_t f2 = freqs[(fi + 1) % (sizeof(freqs) / 4)];
const uint32_t br2 = rates[(fi + pi + 3) % (sizeof(rates) / 4)];
const uint32_t dv2 = devs[(fi + pi + 2) % (sizeof(devs) / 4)];
const uint16_t pre2 = pres[(pi + 1) % npre];
settings(c.radio, lora, f2, br2, dv2, pre2);
c.radio.configure();
snprintf(label, sizeof(label), "%s configure f=%u br=%u dev=%u pre=%u", lora ? "lora" : "fsk", f2, br2, dv2,
pre2);
dump(label, c);
// a send of the largest packet and of one byte
for (size_t len : {(size_t) 1, (size_t) (lora ? 255 : 63)}) {
c.log.clear();
c.fifo.clear();
std::vector<uint8_t> pkt(len);
for (size_t i = 0; i < len; i++)
pkt[i] = (uint8_t) (i * 13 + len);
EXPECT_EQ(c.radio.transmit_packet(pkt), SX127xError::SX127X_ERROR_NONE);
snprintf(label, sizeof(label), "%s send len=%u after f=%u", lora ? "lora" : "fsk", (unsigned) len, f2);
dump(label, c);
}
n++;
}
}
}
printf("recorded %d setups\n", n);
}
} // namespace esphome::sx127x::testing
@@ -0,0 +1,826 @@
#pragma once
// Several fake radios in one ESP32 firmware, joined by a simulated air (esphome#21110). Each radio has its own software
// SPI bus whose CLK/MOSI/MISO and the radio's CS (and reset, busy) are fake GPIOPins swapped in at on_boot priority
// 1100, before the bus's setup, so no SPI pad is ever driven. Only the interrupt pad (SX127x DIO0, SX126x DIO1) is
// real: it is switched to input/output after the radio's setup and driven by the model, so the driver's rising edge
// interrupt fires.
//
// Air: a packet one radio sends lands, after its airtime (worked out from the sender's registers or commands), in every
// other radio that was receiving in the same modulation for the whole airtime, with the receiver's rssi/snr knobs.
// While it is on air each receiver shows a signal: SX127x LoRa RegModemStat (signal detected, synchronized, header
// valid), SX127x FSK RegIrqFlags1 SyncAddressMatch (kept until the packet is read), SX126x IRQ preamble/sync/header
// valid (when in the IRQ mask). Two overlapping senders collide: receivers get a CRC error. Knobs: drop_next (packets
// nobody hears), corrupt_next (CRC error at the receiver), per radio jam (a signal with no packet, for ms or forever),
// tx_never. Counters: tx_over_signal counts sends started while that radio showed a signal (what listen before talk
// prevents).
#include <driver/gpio.h>
#include <esp_random.h>
#include <esp_timer.h>
#include <freertos/FreeRTOS.h>
#include <cmath>
#include "esphome/components/spi/spi.h"
#include "esphome/core/hal.h"
#include "esphome/core/log.h"
#include "spibits.hpp"
namespace sxair {
using namespace esphome;
static const char *const TAG = "sxair";
static portMUX_TYPE MUX = portMUX_INITIALIZER_UNLOCKED;
inline uint32_t now_us() { return (uint32_t) esp_timer_get_time(); }
struct Radio;
struct Air {
Radio *r[4]{};
int n{0};
uint32_t drop_next{0}, corrupt_next{0};
uint32_t dropped{0}, corrupted{0}, collisions{0};
esp_timer_handle_t timer{nullptr};
void begin_tx_locked(Radio *x, uint16_t len);
void end_tx_locked(Radio *x);
void abort_tx_locked(Radio *x);
void left_rx_locked(Radio *y);
} AIR;
inline uint32_t lora_airtime_us(uint8_t sf, uint32_t bw_hz, uint8_t cr, uint16_t preamble, bool crc, bool implicit,
bool ldro, uint16_t len) {
const double ts = (double) (1u << sf) / bw_hz;
const int de = ldro ? 1 : 0;
double num = 8.0 * len - 4.0 * sf + 28 + (crc ? 16 : 0) - (implicit ? 20 : 0);
double sym = 8 + std::max(std::ceil(num / (4.0 * (sf - 2 * de))) * (cr + 4), 0.0);
return (uint32_t) ((preamble + 4.25 + sym) * ts * 1e6);
}
struct Radio {
const char *name{"?"};
int dio_gpio{-1};
bool drive{false}, dio{false};
// sending
bool on_air{false};
uint32_t air_end_us{0};
bool tx_never{false};
// receiving
int incoming{-1}; // index of the sender being heard
bool collided{false}, left_mid{false};
bool jam{false};
uint32_t jam_until_us{0}; // 0: until cleared
float rssi{-80.0f}, snr{7.25f};
int index{0};
// counters
uint32_t tx{0}, rx_ok{0}, rx_crc{0}, rx_collision{0}, rx_left{0}, rx_too_long{0}, tx_over_signal{0}, tx_aborted{0};
uint16_t last_tx_len{0}, last_rx_len{0};
uint32_t fifo_over{0}, fifo_under{0}; // FSK FIFO writes dropped when full, reads when empty
// Both radios share one main loop, so a packet that lands while the previous one is still unread (the loop was busy
// sending on the other radio) is held here and handed over once the driver has read the previous one. A real peer
// reads its own radio in time; this keeps the shared loop from losing packets that two devices would not.
static constexpr int QN = 8;
uint8_t q[QN][256];
uint16_t qlen[QN]{};
bool qcrc[QN]{};
int qhead{0}, qcount{0};
uint32_t queued{0}, queue_full{0};
uint32_t last_spi_us{0}; // a held packet is handed over only after the driver has finished its reads
uint32_t last_tx_air_us{0}, last_tx_end_ms{0};
// the last send started while this radio showed a signal: when, and how long the signal had been showing
uint32_t over_ms{0}, over_signal_age_us{0}, signal_since_us{0};
int over_from{-1};
bool over_jam{false};
virtual bool listening() = 0;
virtual bool lora() = 0;
virtual uint32_t airtime_us(uint16_t len) = 0;
virtual bool crc_on() = 0;
virtual void show_signal_locked(bool on) = 0;
virtual bool signal_visible() = 0;
virtual void tx_done_locked() = 0;
virtual void update_dio_locked() = 0;
virtual bool unread_locked() = 0;
virtual void put_locked(const uint8_t *data, uint16_t len, bool crc_error) = 0;
void deliver_locked(const uint8_t *data, uint16_t len, bool crc_error) {
if (this->unread_locked() || this->qcount > 0) {
if (this->qcount == QN) {
this->queue_full++;
return;
}
int i = (this->qhead + this->qcount) % QN;
memcpy(this->q[i], data, len);
this->qlen[i] = len;
this->qcrc[i] = crc_error;
this->qcount++;
this->queued++;
this->show_signal_locked(this->jam); // the packet is over; it waits in the hold queue
return;
}
this->put_locked(data, len, crc_error);
}
// After every SPI transaction: hand over a held packet once the previous one is read
void drain_locked() {
if (this->qcount == 0 || this->unread_locked() || !this->listening())
return;
int i = this->qhead;
this->qhead = (this->qhead + 1) % QN;
this->qcount--;
this->put_locked(this->q[i], this->qlen[i], this->qcrc[i]);
}
void refresh_signal_locked() {
const bool on = this->jam || this->incoming >= 0;
if (on && !this->signal_visible())
this->signal_since_us = now_us();
this->show_signal_locked(on);
}
void set_dio_locked(bool level) {
this->dio = level;
if (this->drive)
gpio_set_level((gpio_num_t) this->dio_gpio, level);
}
};
inline void Air::begin_tx_locked(Radio *x, uint16_t len) {
x->tx++;
x->last_tx_len = len;
if (x->signal_visible()) {
x->tx_over_signal++;
x->over_ms = millis();
x->over_signal_age_us = now_us() - x->signal_since_us;
x->over_from = x->incoming;
x->over_jam = x->jam;
}
if (x->incoming >= 0)
x->left_mid = true; // it stops hearing the packet it was receiving
x->on_air = true;
x->last_tx_air_us = x->airtime_us(len);
x->air_end_us = now_us() + (x->tx_never ? 0x7FFFFFFF : x->last_tx_air_us);
bool heard = true;
if (this->drop_next > 0) {
this->drop_next--;
this->dropped++;
heard = false;
}
for (int i = 0; i < this->n; i++) {
Radio *y = this->r[i];
if (y == x)
continue;
if (y->incoming >= 0) {
y->collided = true; // two senders at once
this->collisions++;
continue;
}
if (!heard || y->on_air || !y->listening() || y->lora() != x->lora())
continue;
y->incoming = x->index;
y->collided = y->left_mid = false;
y->refresh_signal_locked();
}
}
inline void Air::end_tx_locked(Radio *x) {
x->on_air = false;
x->last_tx_end_ms = millis();
x->tx_done_locked();
}
inline void Air::abort_tx_locked(Radio *x) {
if (!x->on_air)
return;
x->on_air = false;
x->tx_aborted++;
for (int i = 0; i < this->n; i++) {
Radio *y = this->r[i];
if (y->incoming == x->index) {
y->incoming = -1;
y->refresh_signal_locked();
}
}
}
inline void Air::left_rx_locked(Radio *y) {
if (y->incoming >= 0)
y->left_mid = true;
}
// The sender's bytes, kept per sender for delivery at the end of the airtime
static uint8_t TX_PAYLOAD[4][256];
inline void air_tick(void *) {
portENTER_CRITICAL(&MUX);
const uint32_t t = now_us();
for (int i = 0; i < AIR.n; i++) {
Radio *x = AIR.r[i];
if (x->jam && x->jam_until_us != 0 && (int32_t) (t - x->jam_until_us) >= 0) {
x->jam = false;
x->refresh_signal_locked();
}
if ((int32_t) (t - x->last_spi_us) > 3000)
x->drain_locked();
if (!x->on_air || (int32_t) (t - x->air_end_us) < 0)
continue;
AIR.end_tx_locked(x);
for (int j = 0; j < AIR.n; j++) {
Radio *y = AIR.r[j];
if (y->incoming != x->index)
continue;
y->incoming = -1;
if (y->collided) {
y->rx_collision++;
y->collided = false;
y->refresh_signal_locked();
if (y->listening())
y->deliver_locked(TX_PAYLOAD[i], x->last_tx_len, true);
} else if (y->left_mid || !y->listening()) {
y->rx_left++;
y->left_mid = false;
y->refresh_signal_locked();
} else {
bool bad = false;
if (AIR.corrupt_next > 0) {
AIR.corrupt_next--;
AIR.corrupted++;
bad = true;
}
y->deliver_locked(TX_PAYLOAD[i], x->last_tx_len, bad);
}
}
}
portEXIT_CRITICAL(&MUX);
}
inline void start_tx_locked(Radio *x, const uint8_t *data, uint16_t len) {
memcpy(TX_PAYLOAD[x->index], data, len);
AIR.begin_tx_locked(x, len);
}
using spibits::NopPin;
using spibits::PinSet;
// A model behind fake SPI pins, its hooks run under the air's lock
struct AirChip : spibits::SpiChip {
void lock() override { portENTER_CRITICAL(&MUX); }
void unlock() override { portEXIT_CRITICAL(&MUX); }
};
// ---------------------------------------------------------------- SX127x
struct Sx127x : Radio, AirChip {
uint8_t fsk[128]{}, lr[128]{};
uint8_t opmode{0x09};
uint8_t lfifo[256]{};
uint8_t ffifo[64]{};
uint8_t fhead{0}, fcount{0};
uint8_t irq{0};
bool payload_ready{false}, packet_sent{false}, tx_active{false}, sync_hold{false}, signal{false};
uint8_t rx_len{0}, raw_rssi{0};
int8_t raw_snr{0};
uint8_t addr{0};
bool is_lora_mode() const { return (opmode & 0x80) != 0; }
uint8_t mode() const { return opmode & 7; }
static bool common(uint8_t a) { return a == 0x01 || (a >= 0x06 && a <= 0x0C) || a >= 0x40; }
uint8_t *bank(uint8_t a) { return (common(a) || !is_lora_mode()) ? &fsk[a] : &lr[a]; }
bool listening() override { return mode() == 5; }
bool lora() override { return is_lora_mode(); }
bool crc_on() override { return is_lora_mode() ? (lr[0x1E] & 0x04) != 0 : (fsk[0x30] & 0x10) != 0; }
uint32_t freq_hz() { return (uint32_t) ((((uint64_t) fsk[6] << 16) | (fsk[7] << 8) | fsk[8]) * 32000000ULL >> 19); }
uint32_t airtime_us(uint16_t len) override {
if (is_lora_mode()) {
static const uint32_t BW[10] = {7800, 10400, 15600, 20800, 31250, 41700, 62500, 125000, 250000, 500000};
uint8_t bwc = lr[0x1D] >> 4;
return lora_airtime_us(lr[0x1E] >> 4, BW[bwc < 10 ? bwc : 7], (lr[0x1D] >> 1) & 7, (lr[0x20] << 8) | lr[0x21],
crc_on(), lr[0x1D] & 1, (lr[0x26] >> 3) & 1, len);
}
uint32_t br = 32000000u / std::max<uint32_t>(1, (fsk[2] << 8) | fsk[3]);
uint32_t bytes = ((fsk[0x25] << 8) | fsk[0x26]) + ((fsk[0x27] & 0x10) ? (fsk[0x27] & 7) + 1 : 0) +
((fsk[0x30] & 0x80) ? 1 : 0) + len + (crc_on() ? 2 : 0);
return (uint32_t) ((uint64_t) bytes * 8 * 1000000 / br);
}
void show_signal_locked(bool on) override {
signal = on;
if (!on && !payload_ready)
sync_hold = false;
}
bool signal_visible() override { return signal || (!is_lora_mode() && sync_hold); }
void update_dio_locked() override {
bool lvl;
if (is_lora_mode()) {
const uint8_t map = fsk[0x40] & 0xC0;
lvl = map == 0x40 ? (irq & 0x08) != 0 : map == 0x00 ? (irq & 0x40) != 0 : false;
} else {
lvl = mode() == 3 ? packet_sent : payload_ready;
}
set_dio_locked(lvl);
}
void ffifo_push(uint8_t v) {
if (fcount == 64) {
fifo_over++;
return;
}
ffifo[(fhead + fcount) % 64] = v;
fcount++;
}
uint8_t ffifo_pop() {
if (fcount == 0) {
fifo_under++;
return 0;
}
uint8_t v = ffifo[fhead];
fhead = (fhead + 1) % 64;
fcount--;
return v;
}
void ffifo_clear() {
fhead = fcount = 0;
payload_ready = false;
sync_hold = false;
}
bool unread_locked() override { return is_lora_mode() ? (irq & 0x40) != 0 : payload_ready; }
void put_locked(const uint8_t *data, uint16_t len, bool crc_error) override {
signal = jam;
if (is_lora_mode()) {
const uint8_t base = lr[0x0F];
for (uint16_t i = 0; i < len; i++)
lfifo[(uint8_t) (base + i)] = data[i];
lr[0x10] = base;
rx_len = len;
const int off = freq_hz() > 700000000 ? 157 : 164;
raw_rssi = (uint8_t) std::lround(rssi + off);
raw_snr = (int8_t) std::lround(snr * 4);
irq |= 0x40 | 0x10 | (crc_error ? 0x20 : 0);
if (crc_error)
rx_crc++;
else
rx_ok++;
last_rx_len = len;
} else {
const bool variable = fsk[0x30] & 0x80;
if (crc_error && crc_on()) {
rx_crc++; // the chip drops it and restarts RX
sync_hold = false;
} else if (variable && len > fsk[0x32]) {
rx_too_long++;
sync_hold = false;
} else {
ffifo_clear();
if (variable)
ffifo_push(len);
for (uint16_t i = 0; i < len; i++)
ffifo_push(crc_error ? data[i] ^ 0x5A : data[i]);
payload_ready = true;
sync_hold = true;
rx_ok++;
last_rx_len = len;
}
}
update_dio_locked();
}
void tx_done_locked() override {
if (!tx_active || mode() != 3)
return;
tx_active = false;
if (is_lora_mode()) {
irq |= 0x08;
opmode = (opmode & ~7) | 1;
} else {
packet_sent = true;
}
update_dio_locked();
}
void reset_locked() {
AIR.abort_tx_locked(this);
memset(fsk, 0, sizeof(fsk));
memset(lr, 0, sizeof(lr));
opmode = 0x09;
irq = 0;
fhead = fcount = 0;
payload_ready = packet_sent = tx_active = sync_hold = false;
}
void start_tx_locked_() {
uint8_t buf[256];
uint16_t n = 0;
if (is_lora_mode()) {
const uint8_t base = lr[0x0E];
const uint8_t len = lr[0x22];
for (uint16_t i = 0; i < len; i++)
buf[n++] = lfifo[(uint8_t) (base + i)];
} else {
uint8_t len = (fsk[0x30] & 0x80) ? ffifo_pop() : fsk[0x32];
for (uint16_t i = 0; i < len; i++)
buf[n++] = ffifo_pop();
if (fcount == 0)
payload_ready = false;
}
tx_active = true;
start_tx_locked(this, buf, n);
}
void write_mode_locked(uint8_t v) {
const uint8_t prev = mode(), nm = v & 7;
if (prev == 3 && tx_active && nm != 1 && nm != 0 && nm != 3)
return; // still sending: only standby or sleep stop it
const uint8_t lrbit = prev == 0 ? (v & 0x80) : (opmode & 0x80);
opmode = lrbit | (v & 0x78) | nm;
if (prev == 5 && nm != 5)
AIR.left_rx_locked(this);
if (prev == 3 && nm != 3) {
if (tx_active) {
tx_active = false;
AIR.abort_tx_locked(this);
}
packet_sent = false;
}
if (nm == 0 || (nm == 5 && prev != 5 && !is_lora_mode()))
ffifo_clear();
if (nm == 3 && prev != 3)
start_tx_locked_();
update_dio_locked();
}
uint8_t read_locked(uint8_t a) {
if (a == 0x00) {
if (is_lora_mode())
return lfifo[lr[0x0D]++];
uint8_t v = ffifo_pop();
if (fcount == 0) {
payload_ready = false;
sync_hold = false;
}
return v;
}
if (a == 0x01)
return opmode;
if (a == 0x42) {
reset_locked();
return 0x12;
}
if (is_lora_mode()) {
switch (a) {
case 0x12:
return irq;
case 0x13:
return rx_len;
case 0x18:
return signal ? 0x0B : 0x00; // signal detected, synchronized, header valid
case 0x19:
return (uint8_t) raw_snr;
case 0x1A:
return raw_rssi;
default:
break;
}
} else {
if (a == 0x3E)
return 0x80 | ((signal || sync_hold) ? 0x03 : 0x00);
if (a == 0x3F)
return (payload_ready ? 0x04 : 0) | (packet_sent ? 0x08 : 0) | (fcount == 0 ? 0x40 : 0);
if (a == 0x3B)
return fsk[0x3B] & ~0x20;
}
return *bank(a);
}
void write_locked(uint8_t a, uint8_t v) {
if (a == 0x00) {
if (is_lora_mode())
lfifo[lr[0x0D]++] = v;
else
ffifo_push(v);
return;
}
if (a == 0x01) {
write_mode_locked(v);
return;
}
if (is_lora_mode() && a == 0x12) {
irq &= ~v;
return;
}
*bank(a) = v;
}
uint8_t reply_locked(uint16_t p) override {
if (p == 0 || (addr & 0x80))
return 0;
return read_locked(addr & 0x7F);
}
void byte_locked(uint16_t p, uint8_t v) override {
if (p == 0)
addr = v;
else if (addr & 0x80)
write_locked(addr & 0x7F, v);
}
void end_locked() override {
last_spi_us = now_us();
update_dio_locked();
}
};
// ---------------------------------------------------------------- SX126x
struct Sx126x : Radio, AirChip {
uint8_t buf[256]{};
uint8_t cmd[264]{};
uint16_t ncmd{0};
uint8_t pkt_type{0}, mode{1}; // 0 sleep, 1 standby, 3 tx, 4 rx
uint16_t irq{0}, irq_mask{0}, dio1_mask{0};
uint8_t sf{7}, bw{4}, cr{1}, ldro{0};
uint16_t preamble{8};
bool implicit{false}, crc{false}, fsk_var{true};
uint8_t plen{0}, sync_bits{0};
uint32_t bitrate{4800};
uint8_t rx_len{0};
bool signal{false};
uint8_t pkt_rssi{0};
int8_t pkt_snr{0};
bool listening() override { return mode == 4; }
bool lora() override { return pkt_type == 1; }
bool crc_on() override { return crc; }
uint32_t airtime_us(uint16_t len) override {
if (pkt_type == 1) {
uint32_t hz;
switch (bw) {
case 0x00:
hz = 7810;
break;
case 0x08:
hz = 10420;
break;
case 0x01:
hz = 15630;
break;
case 0x09:
hz = 20830;
break;
case 0x02:
hz = 31250;
break;
case 0x0A:
hz = 41670;
break;
case 0x03:
hz = 62500;
break;
case 0x05:
hz = 250000;
break;
case 0x06:
hz = 500000;
break;
default:
hz = 125000;
break;
}
return lora_airtime_us(sf, hz, cr, preamble, crc, implicit, ldro, len);
}
uint32_t bytes = preamble / 8 + sync_bits / 8 + (fsk_var ? 1 : 0) + len + (crc ? 2 : 0);
return (uint32_t) ((uint64_t) bytes * 8 * 1000000 / std::max<uint32_t>(1, bitrate));
}
void raise_locked(uint16_t bits) { irq |= bits & irq_mask; }
void show_signal_locked(bool on) override {
signal = on;
if (on)
raise_locked(0x0004 | 0x0008 | 0x0010); // preamble, sync word, header valid
else
irq &= ~(0x0004 | 0x0008 | 0x0010);
update_dio_locked();
}
bool signal_visible() override { return signal; }
void update_dio_locked() override { set_dio_locked((irq & dio1_mask) != 0); }
bool unread_locked() override { return (irq & 0x0002) != 0; }
void put_locked(const uint8_t *data, uint16_t len, bool crc_error) override {
signal = jam;
memcpy(buf, data, len);
rx_len = len;
pkt_rssi = (uint8_t) std::lround(-rssi * 2);
pkt_snr = (int8_t) std::lround(snr * 4);
raise_locked(0x0002 | (crc_error ? 0x0040 : 0));
if (crc_error)
rx_crc++;
else
rx_ok++;
last_rx_len = len;
update_dio_locked();
}
void tx_done_locked() override {
if (mode != 3)
return;
mode = 1;
raise_locked(0x0001);
update_dio_locked();
}
void reset_locked() {
AIR.abort_tx_locked(this);
mode = 1;
irq = irq_mask = dio1_mask = 0;
pkt_type = 0;
update_dio_locked();
}
void set_mode_locked(uint8_t m) {
if (mode == 4 && m != 4)
AIR.left_rx_locked(this);
if (mode == 3 && m != 3)
AIR.abort_tx_locked(this);
mode = m;
}
uint8_t reply_locked(uint16_t p) override {
if (p == 0)
return 0x22;
const uint8_t op = cmd[0];
switch (op) {
case 0x1D: { // ReadRegister: op, addr hi, addr lo, status, data
if (p < 4)
return 0x22;
const uint16_t reg = ((cmd[1] << 8) | cmd[2]) + (p - 4);
static const char VERSION[] = "SX1261 V2D 2D02";
if (reg >= 0x0320 && reg < 0x0320 + 16)
return (uint8_t) VERSION[reg - 0x0320];
return 0;
}
case 0x1E: // ReadBuffer: op, offset, status, data
return p < 3 ? 0x22 : buf[(uint8_t) (cmd[1] + p - 3)];
case 0x12:
return p == 2 ? irq >> 8 : p == 3 ? irq & 0xFF : 0x22;
case 0x13:
return p == 2 ? rx_len : p == 3 ? 0 : 0x22;
case 0x14:
if (pkt_type == 1)
return p == 2 ? pkt_rssi : p == 3 ? (uint8_t) pkt_snr : p == 4 ? pkt_rssi : 0x22;
return p == 2 ? 0 : (p == 3 || p == 4) ? pkt_rssi : 0x22;
default:
return 0x22;
}
}
void byte_locked(uint16_t p, uint8_t v) override {
if (p < sizeof(cmd))
cmd[p] = v;
ncmd = p + 1;
}
void begin_locked() override { ncmd = 0; }
void end_locked() override {
const uint16_t n = ncmd;
switch (cmd[0]) {
case 0x80:
set_mode_locked(1);
break;
case 0x84:
set_mode_locked(0);
break;
case 0x82:
set_mode_locked(4);
break;
case 0x83:
if (mode != 3) {
if (mode == 4)
AIR.left_rx_locked(this);
mode = 3;
start_tx_locked(this, buf, plen);
}
break;
case 0x8A:
pkt_type = cmd[1];
break;
case 0x8B:
if (pkt_type == 1) {
sf = cmd[1];
bw = cmd[2];
cr = cmd[3];
ldro = cmd[4];
} else {
uint32_t raw = (cmd[1] << 16) | (cmd[2] << 8) | cmd[3];
bitrate = raw ? (uint32_t) (32ULL * 32000000ULL / raw) : 4800;
}
break;
case 0x8C:
preamble = (cmd[1] << 8) | cmd[2];
if (pkt_type == 1) {
implicit = cmd[3];
plen = cmd[4];
crc = cmd[5];
} else {
sync_bits = cmd[4];
fsk_var = cmd[6];
plen = cmd[7];
crc = cmd[8] != 0x01;
}
break;
case 0x08:
irq_mask = (cmd[1] << 8) | cmd[2];
dio1_mask = (cmd[3] << 8) | cmd[4];
break;
case 0x02:
irq &= ~((cmd[1] << 8) | cmd[2]);
break;
case 0x0E:
for (uint16_t i = 2; i < n; i++)
buf[(uint8_t) (cmd[1] + i - 2)] = cmd[i];
break;
default:
break;
}
last_spi_us = now_us();
update_dio_locked();
}
};
class Sx126xRst : public NopPin {
public:
Sx126x *m{nullptr};
void digital_write(bool v) override {
if (!v) {
portENTER_CRITICAL(&MUX);
m->reset_locked();
portEXIT_CRITICAL(&MUX);
}
}
};
// ---------------------------------------------------------------- install helpers
inline void start_air() {
if (AIR.timer != nullptr)
return;
esp_timer_create_args_t args{};
args.callback = air_tick;
args.name = "sxair";
ESP_ERROR_CHECK(esp_timer_create(&args, &AIR.timer));
ESP_ERROR_CHECK(esp_timer_start_periodic(AIR.timer, 1000));
}
inline void add_radio(Radio *r, const char *name, int dio_gpio) {
r->name = name;
r->dio_gpio = dio_gpio;
r->index = AIR.n;
AIR.r[AIR.n++] = r;
}
// on_boot priority 1100: before the SPI buses' setup
template<typename R>
inline void install_sx127x(spi::SPIComponent *bus, R *radio, Sx127x *m, PinSet *p, const char *name, int dio_gpio) {
p->bind(m);
bus->set_clk(&p->clk);
bus->set_mosi(&p->mosi);
bus->set_miso(&p->miso);
radio->set_cs_pin(&p->cs);
radio->set_rst_pin(&p->rst);
add_radio(m, name, dio_gpio);
start_air();
}
template<typename R>
inline void install_sx126x(spi::SPIComponent *bus, R *radio, Sx126x *m, PinSet *p, Sx126xRst *rst, const char *name,
int dio_gpio) {
p->bind(m);
rst->m = m;
bus->set_clk(&p->clk);
bus->set_mosi(&p->mosi);
bus->set_miso(&p->miso);
radio->set_cs_pin(&p->cs);
radio->set_rst_pin(rst);
radio->set_busy_pin(&p->busy);
add_radio(m, name, dio_gpio);
start_air();
}
// After the radios' setup: the interrupt pads become outputs the models drive
inline void late_pins() {
for (int i = 0; i < AIR.n; i++) {
gpio_set_direction((gpio_num_t) AIR.r[i]->dio_gpio, GPIO_MODE_INPUT_OUTPUT);
portENTER_CRITICAL(&MUX);
AIR.r[i]->drive = true;
AIR.r[i]->update_dio_locked();
portEXIT_CRITICAL(&MUX);
}
}
inline void jam(Radio *r, uint32_t ms) {
portENTER_CRITICAL(&MUX);
r->jam = ms != 0;
r->jam_until_us = (ms == 0 || ms == UINT32_MAX) ? 0 : now_us() + ms * 1000;
r->refresh_signal_locked();
portEXIT_CRITICAL(&MUX);
}
inline void unjam(Radio *r) {
portENTER_CRITICAL(&MUX);
r->jam = false;
r->refresh_signal_locked();
portEXIT_CRITICAL(&MUX);
}
inline void log_radio(const char *label, Radio *r) {
ESP_LOGI(
TAG,
"AIR %s %s tx=%u last_tx_len=%u last_air=%uus rx_ok=%u rx_crc=%u rx_coll=%u rx_left=%u rx_too_long=%u "
"tx_over_signal=%u tx_aborted=%u on_air=%d listening=%d signal=%d dio=%d held=%u held_full=%u | dropped=%u "
"corrupted=%u collisions=%u | last over-signal send at %ums, signal %uus old, from %d, jam %d | fifo_over=%u "
"fifo_under=%u",
label, r->name, (unsigned) r->tx, r->last_tx_len, (unsigned) r->last_tx_air_us, (unsigned) r->rx_ok,
(unsigned) r->rx_crc, (unsigned) r->rx_collision, (unsigned) r->rx_left, (unsigned) r->rx_too_long,
(unsigned) r->tx_over_signal, (unsigned) r->tx_aborted, r->on_air, r->listening(), r->signal_visible(), r->dio,
(unsigned) r->queued, (unsigned) r->queue_full, (unsigned) AIR.dropped, (unsigned) AIR.corrupted,
(unsigned) AIR.collisions, (unsigned) r->over_ms, (unsigned) r->over_signal_age_us, r->over_from, r->over_jam,
(unsigned) r->fifo_over, (unsigned) r->fifo_under);
}
} // namespace sxair
@@ -0,0 +1,4 @@
substitutions:
flags: -DSXSIM_BASE
packages:
sim: !include sxsim.yaml
@@ -0,0 +1,360 @@
#pragma once
// Scenario runner for two radios joined by sxair.hpp, with packet_interface radio links on them (esphome#21110):
// raw links (no layers) for sizes, metadata and listen before talk, reliability links and session plus rolling code
// links carrying entity_transport sensors both ways. Called every 20 ms; logs "RES" lines and "DONE fails=N".
// Define SXL_FSK for a FSK pair (63 byte packets).
#include <cmath>
#include "sxair.hpp"
#include "esphome/components/packet_interface/packet_interface.h"
#include "esphome/components/sensor/sensor.h"
namespace sxlink {
using namespace esphome;
using packet_interface::PacketBuffer;
using packet_interface::PacketInterface;
using packet_interface::PacketMetaData;
static const char *const TAG = "ev";
#ifdef SXL_FSK
static constexpr uint16_t MAXP = 63;
static const uint16_t SIZES[] = {1, 2, 16, 32, 62, 63, 64};
#else
static constexpr uint16_t MAXP = 255;
static const uint16_t SIZES[] = {1, 2, 16, 64, 128, 200, 254, 255, 256};
#endif
static constexpr int NSIZES = sizeof(SIZES) / sizeof(SIZES[0]);
inline void fill(uint8_t *d, uint16_t len) {
d[0] = 0xEE;
for (uint16_t i = 1; i < len; i++)
d[i] = (uint8_t) (i * 7 + len);
}
inline bool check(const uint8_t *d, size_t len) {
if (len == 0 || d[0] != 0xEE)
return false;
for (size_t i = 1; i < len; i++)
if (d[i] != (uint8_t) (i * 7 + len))
return false;
return true;
}
struct RawRx {
const char *name;
uint32_t n{0}, bad{0};
uint16_t last_len{0};
float rssi{NAN}, snr{NAN};
void attach(PacketInterface *link) {
link->add_packet_interface_listener([this](const PacketBuffer &data, const PacketMetaData &meta) {
uint8_t buf[256];
size_t len = data.size();
if (len == 0 || len > sizeof(buf)) {
this->bad++;
ESP_LOGW(TAG, "EV raw %s unexpected len=%u", this->name, (unsigned) len);
return;
}
data.copy_to(buf, len);
if (buf[0] != 0xEE)
return; // traffic of the other links on the same radio
if (!check(buf, len)) {
this->bad++;
return;
}
this->n++;
this->last_len = len;
this->rssi = meta.rssi;
this->snr = meta.snr;
ESP_LOGI(TAG, "EV raw %s len=%u rssi=%.1f snr=%.2f", this->name, (unsigned) len, meta.rssi, meta.snr);
});
}
};
static RawRx RXA{"on_a"}, RXB{"on_b"};
static uint32_t DRV[2]{}; // packets each radio driver delivered to on_packet
inline bool raw_send(PacketInterface *link, uint16_t len) {
static uint8_t buf[260];
fill(buf, len);
return link->send_to_interface(PacketBuffer(buf, len), {});
}
struct Ctx {
PacketInterface *raw_a, *raw_b, *links[6];
int nlinks;
sxair::Radio *ma, *mb;
sensor::Sensor *sa, *sb, *sa2, *sb2; // local sensors
sensor::Sensor *ra_on_b, *rb_on_a, *ra2_on_b, *rb2_on_a; // remote copies
// start an async send of len raw bytes on radio B
bool (*b_async)(const uint8_t *, size_t);
} C;
static int step = 0, sub = 0;
static uint32_t next_ms = 0, t0 = 0, cnt = 0, cnt2 = 0, first_ok = 0, over0 = 0;
static float want = NAN;
static uint32_t raw_base = 0; // the receiver's raw count before the last accepted raw send
static bool raw_sent = false;
static uint32_t fails = 0; // values that never arrived, wrong send results, held sends never released
static sensor::Sensor *watch_s = nullptr;
static const char *watch_name = "";
inline void drv_report(const char *label) {
ESP_LOGI(TAG, "RES driver %s delivered A=%u B=%u (air delivered A=%u B=%u)", label, (unsigned) DRV[0],
(unsigned) DRV[1], (unsigned) C.ma->rx_ok, (unsigned) C.mb->rx_ok);
}
inline void idle_report(const char *label) {
drv_report(label);
char s[80];
size_t o = 0;
for (int i = 0; i < C.nlinks; i++)
o += snprintf(s + o, sizeof(s) - o, "%s=%d ", C.links[i]->get_name(), C.links[i]->is_idle());
ESP_LOGI(TAG, "RES idle %s %s", label, s);
}
// Waits for a remote sensor to show the value; true when done (arrived or 15 s)
inline bool wait_value() {
if (watch_s->has_state() && watch_s->state == want) {
ESP_LOGI(TAG, "RES %s arrived value=%.0f after=%ums", watch_name, want, (unsigned) (millis() - t0));
return true;
}
if (millis() - t0 > 15000) {
fails++;
ESP_LOGI(TAG, "RES %s NOT arrived (state=%.1f) after 15 s", watch_name, watch_s->state);
return true;
}
return false;
}
inline void publish_and_watch(sensor::Sensor *local, float v, sensor::Sensor *remote, const char *name) {
want = v;
watch_s = remote;
watch_name = name;
t0 = millis();
local->publish_state(v);
}
#define WAIT(ms) next_ms = millis() + (ms)
#define NEXT() (step++, sub = 0)
inline void run() {
if ((int32_t) (millis() - next_ms) < 0)
return;
switch (step) {
case 0:
sxair::log_radio("boot", C.ma);
sxair::log_radio("boot", C.mb);
idle_report("boot");
ESP_LOGI(TAG, "PHASE entities");
publish_and_watch(C.sa, 11, C.ra_on_b, "rel A->B");
NEXT();
return;
case 1:
if (!wait_value())
return;
publish_and_watch(C.sb, 22, C.rb_on_a, "rel B->A");
NEXT();
return;
case 2:
if (!wait_value())
return;
publish_and_watch(C.sa2, 33, C.ra2_on_b, "session A->B");
NEXT();
return;
case 3:
if (!wait_value())
return;
publish_and_watch(C.sb2, 44, C.rb2_on_a, "session B->A");
NEXT();
return;
case 4:
if (!wait_value())
return;
ESP_LOGI(TAG, "PHASE raw_sizes");
NEXT();
WAIT(1500);
return;
case 5: { // one size per pass, A to B
if (sub > 0) {
const uint16_t prev = SIZES[sub - 1];
ESP_LOGI(TAG, "RES size %u A->B rx_on_b=%u last_len=%u rssi=%.1f snr=%.2f bad=%u air_us=%u", prev,
(unsigned) RXB.n, RXB.last_len, RXB.rssi, RXB.snr, (unsigned) RXB.bad,
(unsigned) C.ma->last_tx_air_us);
if (raw_sent && (RXB.n != raw_base + 1 || RXB.last_len != prev))
fails++; // an accepted packet that did not arrive once and whole
}
if (sub == NSIZES) {
NEXT();
return;
}
const uint16_t len = SIZES[sub];
if (cnt == 0)
t0 = millis();
const bool ok = raw_send(C.raw_a, len);
if (!ok && len <= MAXP && millis() - t0 < 3000) {
cnt++; // refused while the other links' traffic is on air; try again
WAIT(30);
return;
}
if (ok != (len <= MAXP))
fails++; // a valid size refused, or an oversize one accepted
raw_sent = ok;
raw_base = RXB.n;
ESP_LOGI(TAG, "RES send size %u -> %s after %u refusals", len, ok ? "sent" : "refused", (unsigned) cnt);
cnt = 0;
sub++;
WAIT(900);
return;
}
case 6: {
const bool ok = raw_send(C.raw_b, MAXP);
if (!ok && cnt++ < 100) {
WAIT(30);
return;
}
cnt = 0;
if (!ok)
fails++;
raw_sent = ok;
raw_base = RXA.n;
ESP_LOGI(TAG, "RES send size %u B->A -> %s", MAXP, ok ? "sent" : "refused");
NEXT();
WAIT(900);
return;
}
case 7:
ESP_LOGI(TAG, "RES size %u B->A rx_on_a=%u last_len=%u rssi=%.1f snr=%.2f bad=%u", MAXP, (unsigned) RXA.n,
RXA.last_len, RXA.rssi, RXA.snr, (unsigned) RXA.bad);
if (raw_sent && (RXA.n != raw_base + 1 || RXA.last_len != MAXP))
fails++;
idle_report("after_sizes");
ESP_LOGI(TAG, "PHASE lbt_peer_sending");
over0 = C.ma->tx_over_signal;
{
static uint8_t big[256];
fill(big, MAXP);
const bool ok = C.b_async(big, MAXP);
if (!ok)
fails++; // nothing on air, so the listen before talk check below proves nothing
ESP_LOGI(TAG, "RES peer async %u bytes -> %d airtime=%uus", MAXP, ok, (unsigned) C.mb->last_tx_air_us);
}
t0 = millis();
cnt = cnt2 = 0;
NEXT();
return;
case 8: { // try from A every pass until accepted
const bool idle_before = C.raw_a->is_idle();
const bool ok = raw_send(C.raw_a, 10);
if (!ok) {
cnt++;
if (!idle_before || !C.raw_a->is_idle())
cnt2++; // the link's loop ran while the send was held
if (millis() - t0 > 6000) {
fails++;
ESP_LOGI(TAG, "RES lbt never released, refused=%u", (unsigned) cnt);
NEXT();
}
return;
}
const uint32_t end = C.mb->last_tx_end_ms;
if (cnt == 0 || C.ma->tx_over_signal != over0)
fails++; // sent at once over the peer's packet, or over a signal: listen before talk did not hold it
ESP_LOGI(TAG,
"RES lbt refused=%u loop_active_while_held=%u accepted after=%ums, peer packet ended at %dms, "
"a_received_peer=%u tx_over_signal_delta=%u",
(unsigned) cnt, (unsigned) cnt2, (unsigned) (millis() - t0), (int) (end - t0), (unsigned) C.ma->rx_ok,
(unsigned) (C.ma->tx_over_signal - over0));
NEXT();
WAIT(400);
return;
}
case 9:
idle_report("after_lbt");
ESP_LOGI(TAG, "PHASE hold_cap");
sxair::jam(C.ma, UINT32_MAX);
t0 = millis();
cnt = cnt2 = first_ok = 0;
NEXT();
return;
case 10: { // a radio busy for ever: sends are held for max_busy, then go
const bool ok = raw_send(C.raw_a, 8);
const uint32_t el = millis() - t0;
if (ok) {
cnt2++;
ESP_LOGI(TAG, "RES hold release %u at %ums (refused before it %u) loop_idle=%d", (unsigned) cnt2, (unsigned) el,
(unsigned) cnt, C.raw_a->is_idle());
cnt = 0;
} else {
cnt++;
}
if (el > 5000) {
if (cnt2 == 0)
fails++;
ESP_LOGI(TAG, "RES hold end releases=%u loop_idle_while_jammed=%d", (unsigned) cnt2, C.raw_a->is_idle());
sxair::unjam(C.ma);
NEXT();
WAIT(300);
} else {
WAIT(50);
}
return;
}
case 11:
idle_report("after_unjam");
ESP_LOGI(TAG, "PHASE reliability_after_refused");
over0 = C.ma->tx_over_signal;
sxair::jam(C.ma, 800);
publish_and_watch(C.sa, 55, C.ra_on_b, "rel A->B while A busy 800ms");
NEXT();
return;
case 12:
if (!wait_value())
return;
if (C.ma->tx_over_signal != over0)
fails++; // outside the hold cap phase no send may start over a signal
ESP_LOGI(TAG, "RES tx_over_signal_delta=%u", (unsigned) (C.ma->tx_over_signal - over0));
ESP_LOGI(TAG, "PHASE reliability_lost");
cnt = C.ma->tx;
sxair::AIR.drop_next = 1;
publish_and_watch(C.sa, 66, C.ra_on_b, "rel A->B first packet lost");
NEXT();
return;
case 13:
if (!wait_value())
return;
ESP_LOGI(TAG, "RES lost: A sent %u packets for it, dropped=%u", (unsigned) (C.ma->tx - cnt),
(unsigned) sxair::AIR.dropped);
cnt = C.mb->tx;
sxair::AIR.corrupt_next = 1;
publish_and_watch(C.sb, 77, C.rb_on_a, "rel B->A first packet corrupted");
NEXT();
return;
case 14:
if (!wait_value())
return;
ESP_LOGI(TAG, "RES corrupt: B sent %u packets for it, corrupted=%u a_rx_crc=%u", (unsigned) (C.mb->tx - cnt),
(unsigned) sxair::AIR.corrupted, (unsigned) C.ma->rx_crc);
sxair::AIR.drop_next = 2;
publish_and_watch(C.sa2, 88, C.ra2_on_b, "session A->B two packets lost");
NEXT();
return;
case 15:
if (!wait_value())
return;
publish_and_watch(C.sb2, 99, C.rb2_on_a, "session B->A");
NEXT();
return;
case 16:
if (!wait_value())
return;
NEXT();
WAIT(3000);
return;
case 17:
sxair::log_radio("end", C.ma);
sxair::log_radio("end", C.mb);
idle_report("end");
// raw packets with a bad pattern count too
ESP_LOGI(TAG, "DONE fails=%u", (unsigned) (fails + RXA.bad + RXB.bad));
NEXT();
return;
default:
return;
}
}
} // namespace sxlink
@@ -0,0 +1,4 @@
substitutions:
flags: -DSXSIM_PR
packages:
sim: !include sxsim.yaml
@@ -0,0 +1,306 @@
#pragma once
// SX127x SPI recorder on an ESP32 (esphome#22776). The same register file as the host harness (burst auto-increment,
// FIFO address fixed, instant mode changes, version 0x12), fed either by
// - SXREC_SW: a software SPI bus whose pins and the radio's CS are fake (swap at on_boot 1100), or
// - SXREC_HW: the real ESP-IDF hardware bus. A tee delegate (installed at on_boot 800, before the radio's setup)
// sends every byte on the wire through the real SPIDelegateHw and gives the model the same bytes; the model
// answers reads. SPI3 as a slave sniffer (CLK, MOSI and the radio's CS pad routed in through the GPIO matrix,
// nominal CS GPIO14 held high) captures each chip select window from the wire, up to 64 bytes, and the wire bytes
// are compared with what the driver sent.
// DIO0 (a real pad, input/output after setup) is high while the model is in TX, so a send completes at once.
// Records are printed as "REC CASE/T/REGS/FIFO/END" lines; compare_spi.py reads the device logs directly.
#include <driver/gpio.h>
#include <vector>
#include "esphome/components/spi/spi.h"
#include "esphome/components/sx127x/sx127x.h"
#include "esphome/core/application.h"
#include "esphome/core/hal.h"
#include "esphome/core/log.h"
#include "spibits.hpp"
#ifdef SXREC_HW
#include "driver/spi_slave.h"
#include "esp_rom_gpio.h"
#include "soc/spi_periph.h"
#endif
namespace sxrec {
using namespace esphome;
static const char *const TAG = "rec";
struct Chip : spibits::SpiChip {
uint8_t regs[128]{};
std::vector<uint8_t> fifo;
std::vector<uint8_t> cur;
std::vector<std::vector<uint8_t>> log;
uint8_t addr{0};
int dio{-1};
bool drive{false};
Chip() { regs[0x01] = 0x01; }
uint8_t reply_locked(uint16_t p) override {
if (p == 0 || (addr & 0x80))
return 0;
const uint8_t a = addr & 0x7F;
if (a == 0x42)
return 0x12;
if (a == 0x3B)
return regs[a] & ~0x20;
return regs[a];
}
void byte_locked(uint16_t p, uint8_t v) override {
cur.push_back(v);
if (p == 0) {
addr = v;
return;
}
if (!(addr & 0x80))
return;
const uint8_t a = addr & 0x7F;
if (a == 0) {
fifo.push_back(v);
return;
}
regs[a] = v;
addr = 0x80 | ((a + 1) & 0x7F);
}
void begin_locked() override { cur.clear(); }
void end_locked() override {
log.push_back(cur);
if (drive)
gpio_set_level((gpio_num_t) dio, (regs[0x01] & 7) == 3);
}
// The hardware bus tee feeds whole bytes, without the fake pins
void start() {
pos = 0;
begin_locked();
}
uint8_t exchange(uint8_t v) {
const uint8_t r = reply_locked(pos);
byte_locked(pos++, v);
return r;
}
} CHIP;
// ---------------------------------------------------------------- software bus
static spibits::PinSet PINS;
inline void install_sw(spi::SPIComponent *bus, sx127x::SX127x *radio, int dio) {
PINS.bind(&CHIP);
bus->set_clk(&PINS.clk);
bus->set_mosi(&PINS.mosi);
bus->set_miso(&PINS.miso);
radio->set_cs_pin(&PINS.cs);
CHIP.dio = dio;
ESP_LOGI(TAG, "software bus recorder installed");
}
// ---------------------------------------------------------------- hardware bus
#ifdef SXREC_HW
template<typename T, typename T::type M> struct Rob {
friend typename T::type get(T) { return M; }
};
struct DelegateTag {
using type = spi::SPIDelegate *spi::SPIClient::*;
friend type get(DelegateTag);
};
template struct Rob<DelegateTag, &spi::SPIClient::delegate_>;
struct CsTag {
using type = GPIOPin *spi::SPIClient::*;
friend type get(CsTag);
};
template struct Rob<CsTag, &spi::SPIClient::cs_>;
static constexpr int SCS = 14; // nominal slave CS, a spare pad held high
static WORD_ALIGNED_ATTR uint8_t s_rx[64];
static WORD_ALIGNED_ATTR uint8_t s_tx[64];
static int CLK_PAD = 18, MOSI_PAD = 23, CS_PAD = 5;
static uint32_t WIRE_OK = 0, WIRE_BAD = 0, WIRE_LONG = 0, WIRE_FAIL = 0;
static uint32_t IDF_ARRAY_CALLS = 0, IDF_BYTE_CALLS = 0;
inline void slave_setup() {
spi_bus_config_t b{};
b.mosi_io_num = -1;
b.miso_io_num = -1;
b.sclk_io_num = -1;
b.quadwp_io_num = -1;
b.quadhd_io_num = -1;
b.max_transfer_sz = 64;
spi_slave_interface_config_t s{};
s.mode = 0;
s.spics_io_num = SCS;
s.queue_size = 1;
ESP_ERROR_CHECK(spi_slave_initialize(SPI3_HOST, &b, &s, SPI_DMA_DISABLED));
gpio_set_direction((gpio_num_t) SCS, GPIO_MODE_INPUT_OUTPUT);
gpio_set_level((gpio_num_t) SCS, 1);
gpio_input_enable((gpio_num_t) CLK_PAD);
gpio_input_enable((gpio_num_t) MOSI_PAD);
gpio_input_enable((gpio_num_t) CS_PAD);
esp_rom_gpio_connect_in_signal(CLK_PAD, spi_periph_signal[SPI3_HOST].spiclk_in, false);
esp_rom_gpio_connect_in_signal(MOSI_PAD, spi_periph_signal[SPI3_HOST].spid_in, false);
}
class Tee : public spi::SPIDelegate {
public:
explicit Tee(spi::SPIDelegate *hw) : hw_(hw) {}
void begin_transaction() override {
memset(s_rx, 0, sizeof(s_rx));
static spi_slave_transaction_t t;
t = {};
t.length = 64 * 8;
t.tx_buffer = s_tx;
t.rx_buffer = s_rx;
this->queued_ = spi_slave_queue_trans(SPI3_HOST, &t, pdMS_TO_TICKS(100)) == ESP_OK;
// The slave driver routes its CS input back to SCS when it loads the queued transaction, so the radio's CS is
// routed in again each time, after giving the driver time to load it
esp_rom_delay_us(300);
esp_rom_gpio_connect_in_signal(CS_PAD, spi_periph_signal[SPI3_HOST].spics_in, false);
CHIP.start();
this->hw_->begin_transaction();
}
void end_transaction() override {
this->hw_->end_transaction();
spi_slave_transaction_t *r;
int bits = -1;
if (this->queued_ && spi_slave_get_trans_result(SPI3_HOST, &r, pdMS_TO_TICKS(200)) == ESP_OK)
bits = r->trans_len;
const auto &sent = CHIP.cur;
if (bits < 0) {
WIRE_FAIL++;
} else if (sent.size() > 64) {
WIRE_LONG++;
} else if ((size_t) bits == sent.size() * 8 && memcmp(s_rx, sent.data(), sent.size()) == 0) {
WIRE_OK++;
} else {
if (WIRE_BAD++ < 10)
ESP_LOGW(TAG, "wire differs: sent %u bytes, wire %d bits, first sent %02X wire %02X", (unsigned) sent.size(),
bits, sent.empty() ? 0 : sent[0], s_rx[0]);
}
CHIP.end_locked();
}
uint8_t transfer(uint8_t data) override {
IDF_BYTE_CALLS++;
this->hw_->transfer(data);
return CHIP.exchange(data);
}
void write_array(const uint8_t *ptr, size_t length) override {
IDF_ARRAY_CALLS++;
this->hw_->write_array(ptr, length); // the ESP-IDF path under test: one SPI transaction
for (size_t i = 0; i < length; i++)
CHIP.exchange(ptr[i]);
}
void read_array(uint8_t *ptr, size_t length) override {
uint8_t zeros[64]{};
for (size_t off = 0; off < length; off += sizeof(zeros))
this->hw_->write_array(zeros, std::min(sizeof(zeros), length - off));
for (size_t i = 0; i < length; i++)
ptr[i] = CHIP.exchange(0);
}
bool is_ready() override { return this->hw_->is_ready(); }
protected:
spi::SPIDelegate *hw_;
bool queued_{false};
};
// on_boot 800: after the bus's setup, before the radio's. The radio's own spi_setup() then finds a delegate and keeps
// it ("Device already registered" is logged once).
inline void install_hw(spi::SPIComponent *bus, sx127x::SX127x *radio, int dio) {
auto *client = static_cast<spi::SPIClient *>(radio);
static spi::SPIClient owner(spi::BIT_ORDER_MSB_FIRST, spi::MODE0, 8000000);
GPIOPin *cs = client->*get(CsTag());
spi::SPIDelegate *hw = bus->register_device(&owner, spi::MODE0, spi::BIT_ORDER_MSB_FIRST, 8000000, cs, false, false);
client->*get(DelegateTag()) = new Tee(hw);
slave_setup();
CHIP.dio = dio;
ESP_LOGI(TAG, "hardware bus tee and SPI3 sniffer installed");
}
#endif
inline void late_pins() {
gpio_set_direction((gpio_num_t) CHIP.dio, GPIO_MODE_INPUT_OUTPUT);
CHIP.drive = true;
gpio_set_level((gpio_num_t) CHIP.dio, 0);
}
// One "REC <prefix> XX XX ..." record, continued on "REC + ..." lines when it does not fit one log line
inline void log_bytes(const char *prefix, const uint8_t *data, size_t n) {
char line[400];
size_t o = snprintf(line, sizeof(line), "REC %s", prefix);
for (size_t i = 0; i < n; i++) {
if (o + 4 >= sizeof(line)) {
ESP_LOGI(TAG, "%s", line);
o = snprintf(line, sizeof(line), "REC +");
}
o += snprintf(line + o, sizeof(line) - o, " %02X", data[i]);
}
ESP_LOGI(TAG, "%s", line);
}
inline void dump(const char *label) {
static int index = 0;
ESP_LOGI(TAG, "REC CASE %d %s", index++, label);
for (auto &t : CHIP.log)
log_bytes("T", t.data(), t.size());
log_bytes("REGS", CHIP.regs, sizeof(CHIP.regs));
log_bytes("FIFO", CHIP.fifo.data(), CHIP.fifo.size());
ESP_LOGI(TAG, "REC END");
delay(30); // let the log drain
App.feed_wdt(); // the sweep runs in one lambda
CHIP.log.clear();
}
inline void settings(sx127x::SX127x *r, bool lora, uint32_t freq, uint32_t br, uint32_t dev, uint16_t pre) {
using namespace sx127x;
r->set_modulation(lora ? MOD_LORA : MOD_FSK);
r->set_frequency(freq);
r->set_bandwidth(lora ? SX127X_BW_125_0 : SX127X_BW_50_0);
r->set_bitrate(br);
r->set_deviation(dev);
r->set_preamble_size(pre);
r->set_packet_mode(true);
r->set_bitsync(true);
r->set_payload_length(0);
r->set_sync_value(lora ? std::vector<uint8_t>{0x12} : std::vector<uint8_t>{0x33, 0x33});
}
// Runs the whole sweep once, from a lambda after boot
inline void sweep(sx127x::SX127x *r) {
dump("setup from yaml");
const uint32_t freqs[] = {137000000, 433920000, 868100000, 1020000000};
const uint32_t rates[] = {500, 4800, 38400, 300000};
const uint32_t devs[] = {0, 5000, 100000, 600};
const uint16_t fsk_pre[] = {0, 2, 256, 65535};
const uint16_t lora_pre[] = {5, 8, 256, 65535};
char label[96];
for (bool lora : {false, true}) {
for (int fi = 0; fi < 4; fi++) {
for (int pi = 0; pi < 4; pi++) {
const uint16_t pre = lora ? lora_pre[pi] : fsk_pre[pi];
const uint32_t br = rates[(fi + pi) % 4], dv = devs[(fi * 3 + pi) % 4];
settings(r, lora, freqs[fi], br, dv, pre);
CHIP.log.clear();
r->configure();
snprintf(label, sizeof(label), "%s configure f=%u br=%u dev=%u pre=%u failed=%d", lora ? "lora" : "fsk",
(unsigned) freqs[fi], (unsigned) br, (unsigned) dv, pre, r->is_failed());
dump(label);
if (pi == 0 || pi == 3) {
for (size_t len : {(size_t) 1, (size_t) 63}) {
std::vector<uint8_t> pkt(len);
for (size_t i = 0; i < len; i++)
pkt[i] = (uint8_t) (i * 13 + len);
CHIP.fifo.clear();
auto e = r->transmit_packet(pkt);
snprintf(label, sizeof(label), "%s send len=%u result=%d", lora ? "lora" : "fsk", (unsigned) len, (int) e);
dump(label);
}
}
}
}
}
#ifdef SXREC_HW
ESP_LOGI(TAG, "WIRE ok=%u differ=%u over64=%u no_capture=%u write_array_calls=%u transfer_calls=%u",
(unsigned) WIRE_OK, (unsigned) WIRE_BAD, (unsigned) WIRE_LONG, (unsigned) WIRE_FAIL,
(unsigned) IDF_ARRAY_CALLS, (unsigned) IDF_BYTE_CALLS);
#endif
ESP_LOGI(TAG, "SWEEP DONE");
}
} // namespace sxrec
@@ -0,0 +1,462 @@
#pragma once
// Scenario runner for the fake SX1276 (sxsim.hpp), esphome#22733. Called every 20 ms from an interval; each step logs
// "RES ..." lines. Build with -DSXSIM_PR for a driver with the asynchronous API (start_transmit_async and friends).
// Boot cases run through software restarts (RTC_NOINIT): 0 main LoRa+FSK sequence ending with a runtime mode change
// failure, 1 blocking send that never completes, 2 wrong chip version at setup, 3 mode change failure at setup,
// 4 done.
#include <cmath>
#include <esp_attr.h>
#include <esp_system.h>
#include "sxsim.hpp"
namespace sxrun {
using namespace esphome;
using namespace esphome::sx127x;
using sxsim::M;
static const char *const TAG = "ev";
RTC_NOINIT_ATTR static uint32_t g_case;
RTC_NOINIT_ATTR static uint32_t g_magic;
static constexpr uint32_t MAGIC = 0x5A127A55;
static uint32_t boot_case = 0;
inline const char *ename(SX127xError e) {
static const char *const N[] = {"NONE", "TIMEOUT", "INVALID_PARAMS", "NOT_READY", "BUSY", "CANCELLED"};
int i = (int) e;
return (i >= 0 && i < 6) ? N[i] : "?";
}
struct PktL : SX127xListener {
uint32_t n{0};
void on_packet(const std::vector<uint8_t> &p, float rssi, float snr) override {
n++;
char h[80];
sxsim::hex(h, sizeof(h), p.data(), std::min<size_t>(p.size(), 32));
ESP_LOGI(TAG, "EV packet n=%u len=%u data=[%s] rssi=%.1f snr=%s%.2f", (unsigned) n, (unsigned) p.size(), h, rssi,
std::isnan(snr) ? "NAN:" : "", snr);
}
} P;
#ifdef SXSIM_PR
struct TxL : SX127xTransmitListener {
const char *name;
uint32_t n{0}, t0{0};
SX127xError last{SX127xError::SX127X_ERROR_NONE};
explicit TxL(const char *nm) : name(nm) {}
void on_transmit_complete(SX127xError r) override {
n++;
last = r;
ESP_LOGI(TAG, "EV txdone %s result=%s n=%u after=%ums chip=%s", name, ename(r), (unsigned) n,
(unsigned) (millis() - t0), sxsim::mode_name());
}
} L("L"), L2("L2");
inline SX127xError start_async(SX127x *r, TxL &l, std::initializer_list<uint8_t> d, uint32_t timeout = 4000) {
std::vector<uint8_t> v(d);
l.t0 = millis();
uint32_t us = micros();
SX127xError e = r->start_transmit_async(&l, v.data(), v.size(), timeout);
us = micros() - us;
ESP_LOGI(TAG, "RES async_start %s timeout=%u -> %s in %uus is_transmitting=%d chip=%s", l.name, (unsigned) timeout,
ename(e), (unsigned) us, r->is_transmitting(), sxsim::mode_name());
return e;
}
#endif
inline SX127xError blocking(SX127x *r, const char *label, std::initializer_list<uint8_t> d, bool trace = false) {
std::vector<uint8_t> v(d);
if (trace)
sxsim::trace_start();
uint32_t t = millis();
SX127xError e = r->transmit_packet(v);
t = millis() - t;
if (trace) {
sxsim::trace_stop();
sxsim::log_trace(label);
}
ESP_LOGI(TAG, "RES %s blocking -> %s in %ums warn=%d failed=%d", label, ename(e), (unsigned) t,
r->status_has_warning(), r->is_failed());
sxsim::log_state(label);
return e;
}
inline void to_fsk(SX127x *r) {
r->set_modulation(MOD_FSK);
r->set_bandwidth(SX127X_BW_50_0);
r->set_bitrate(4800);
r->set_deviation(5000);
r->set_packet_mode(true);
r->set_bitsync(true);
r->set_sync_value({0x33, 0x33});
r->configure();
}
inline void reboot_to(uint32_t c) {
g_case = c;
g_magic = MAGIC;
ESP_LOGI(TAG, "REBOOT to case %u", (unsigned) c);
delay(200);
esp_restart();
}
// on_boot priority 800, after sxsim::install
inline void boot(SX127x *r) {
boot_case = (esp_reset_reason() == ESP_RST_SW && g_magic == MAGIC) ? g_case : 0;
g_magic = 0;
ESP_LOGI(TAG, "BOOT case %u", (unsigned) boot_case);
if (boot_case == 2)
M.version = 0x22;
if (boot_case == 3)
M.fail_mode_writes = 1000000;
r->register_listener(&P);
}
static int step = 0;
static uint32_t next_ms = 0;
static uint32_t prev_warn = 2, prev_failed = 2;
static uint32_t watch_until = 0;
inline void watch(SX127x *r) {
uint32_t w = r->status_has_warning(), f = r->is_failed();
if (w != prev_warn || f != prev_failed) {
ESP_LOGI(TAG, "RES watch warn=%u failed=%u chip=%s ign_fault=%u resets=%u", (unsigned) w, (unsigned) f,
sxsim::mode_name(), (unsigned) M.mode_ignored_fault, (unsigned) M.resets);
prev_warn = w;
prev_failed = f;
}
}
#define WAIT(ms) next_ms = millis() + (ms)
inline void run(SX127x *r, bool *want_action) {
if (millis() < watch_until)
watch(r);
if ((int32_t) (millis() - next_ms) < 0)
return;
const int s = step++;
if (boot_case == 0) {
switch (s) {
case 0:
ESP_LOGI(TAG, "RES setup failed=%d warn=%d", r->is_failed(), r->status_has_warning());
sxsim::log_state("boot");
WAIT(500);
return;
// ---------------- LoRa ----------------
case 1:
ESP_LOGI(TAG, "PHASE lora_blocking");
blocking(r, "L1", {0x01, 0x02, 0x03, 0x04, 0x05}, true);
WAIT(200);
return;
case 2: {
int ok = 0;
for (int i = 0; i < 5; i++)
ok += blocking(r, "L1x", {(uint8_t) i, 0xAA, 0x55}) == SX127xError(0);
ESP_LOGI(TAG, "RES L1x ok=%d of 5", ok);
WAIT(200);
return;
}
case 3:
ESP_LOGI(TAG, "PHASE lora_action");
*want_action = true;
WAIT(500);
return;
case 4:
sxsim::log_state("L2action");
ESP_LOGI(TAG, "PHASE lora_receive");
M.pkt_rssi = 100;
M.pkt_snr = 22;
sxsim::inject({0xA1, 0xA2, 0xA3, 0xA4});
WAIT(300);
return;
case 5:
ESP_LOGI(TAG, "RES L3 packets=%u", (unsigned) P.n);
sxsim::inject({0xC1, 0xC2}, true); // CRC error: no packet
WAIT(300);
return;
case 6:
ESP_LOGI(TAG, "RES L3crc packets=%u", (unsigned) P.n);
sxsim::log_state("L3");
ESP_LOGI(TAG, "PHASE lora_rx_before_send");
sxsim::arm_rx_on_standby({0xB1, 0xB2, 0xB3});
blocking(r, "L4", {0x07, 0x07, 0x07}, true);
WAIT(500);
return;
case 7:
ESP_LOGI(TAG, "RES L4 after packets=%u", (unsigned) P.n);
sxsim::log_state("L4after");
// the next send works and the radio receives again
blocking(r, "L4next", {0x08, 0x08});
sxsim::inject({0xD1, 0xD2});
WAIT(300);
return;
case 8:
ESP_LOGI(TAG, "RES L4next packets=%u", (unsigned) P.n);
ESP_LOGI(TAG, "PHASE lora_raw_tx_mode");
sxsim::trace_start();
r->set_mode_tx();
WAIT(400);
return;
case 9:
sxsim::trace_stop();
sxsim::log_trace("L5");
ESP_LOGI(TAG, "RES L5 packets=%u", (unsigned) P.n);
sxsim::log_state("L5");
r->set_mode_rx();
sxsim::log_state("L5rx");
WAIT(200);
return;
#ifdef SXSIM_PR
case 10:
ESP_LOGI(TAG, "PHASE lora_async");
M.tx_ms = 80;
start_async(r, L, {0x11, 0x12, 0x13, 0x14, 0x15});
start_async(r, L2, {0x21});
ESP_LOGI(TAG, "RES second_blocking -> %s", ename(r->transmit_packet(std::vector<uint8_t>{0x31})));
WAIT(400);
return;
case 11:
ESP_LOGI(TAG, "RES A1 L.n=%u L.last=%s L2.n=%u is_tx=%d", (unsigned) L.n, ename(L.last), (unsigned) L2.n,
r->is_transmitting());
sxsim::log_state("A1");
start_async(r, L2, {0x41}, 0);
start_async(r, L2, {0x41}, 262144);
ESP_LOGI(TAG, "RES null_listener -> %s", ename(r->start_transmit_async(nullptr, std::vector<uint8_t>{1})));
start_async(r, L2, {0x42}, 262143);
WAIT(400);
return;
case 12:
ESP_LOGI(TAG, "RES A2 L2.n=%u L2.last=%s", (unsigned) L2.n, ename(L2.last));
ESP_LOGI(TAG, "PHASE lora_async_timeout");
M.tx_never = true;
start_async(r, L, {0x51, 0x52}, 300);
WAIT(800);
return;
case 13:
M.tx_never = false;
ESP_LOGI(TAG, "RES A3 L.n=%u L.last=%s warn=%d", (unsigned) L.n, ename(L.last), r->status_has_warning());
sxsim::log_state("A3");
ESP_LOGI(TAG, "PHASE lora_async_cancel");
M.tx_ms = 1000;
start_async(r, L, {0x61, 0x62});
WAIT(50);
return;
case 14:
ESP_LOGI(TAG, "RES cancel_other -> %d", r->cancel_transmit_async(&L2));
ESP_LOGI(TAG, "RES cancel_owner -> %d (L.n=%u last=%s) is_tx=%d chip=%s", r->cancel_transmit_async(&L),
(unsigned) L.n, ename(L.last), r->is_transmitting(), sxsim::mode_name());
ESP_LOGI(TAG, "RES cancel_again -> %d", r->cancel_transmit_async(&L));
WAIT(1300);
return;
case 15:
ESP_LOGI(TAG, "RES A4 L.n=%u last=%s warn=%d", (unsigned) L.n, ename(L.last), r->status_has_warning());
sxsim::log_state("A4");
ESP_LOGI(TAG, "PHASE lora_actions_during_send");
M.tx_ms = 500;
start_async(r, L, {0x71});
{
uint32_t w0 = M.mode_writes;
r->set_mode_standby();
r->set_mode_sleep();
r->set_mode_rx();
r->set_mode_tx();
r->run_image_cal();
r->configure();
ESP_LOGI(TAG, "RES A5 mode_writes_during=%u chip=%s is_tx=%d warn=%d", (unsigned) (M.mode_writes - w0),
sxsim::mode_name(), r->is_transmitting(), r->status_has_warning());
}
WAIT(800);
return;
case 16:
ESP_LOGI(TAG, "RES A5 L.n=%u last=%s", (unsigned) L.n, ename(L.last));
sxsim::log_state("A5");
ESP_LOGI(TAG, "PHASE lora_async_rx_before_send");
M.tx_ms = 50;
sxsim::arm_rx_on_standby({0xE1, 0xE2, 0xE3});
{
uint32_t n0 = L.n, p0 = P.n;
start_async(r, L, {0x81});
uint32_t w0 = M.mode_writes;
r->set_mode_sleep(); // ignored while the packet is held
ESP_LOGI(TAG, "RES A6 completions=%u packets_now=%u mode_writes_by_action=%u chip=%s", (unsigned) (L.n - n0),
(unsigned) (P.n - p0), (unsigned) (M.mode_writes - w0), sxsim::mode_name());
}
WAIT(300);
return;
case 17:
ESP_LOGI(TAG, "RES A6 after packets=%u L.n=%u", (unsigned) P.n, (unsigned) L.n);
sxsim::log_state("A6");
start_async(r, L, {0x82});
WAIT(300);
return;
case 18:
ESP_LOGI(TAG, "RES A6b L.n=%u last=%s", (unsigned) L.n, ename(L.last));
sxsim::log_state("A6b");
WAIT(10);
return;
#endif
// ---------------- FSK ----------------
case 20:
ESP_LOGI(TAG, "PHASE fsk_configure");
M.tx_ms = 50;
to_fsk(r);
sxsim::log_state("F0");
ESP_LOGI(TAG, "RES F0 warn=%d failed=%d", r->status_has_warning(), r->is_failed());
WAIT(300);
return;
case 21:
ESP_LOGI(TAG, "PHASE fsk_blocking");
blocking(r, "F1", {0x01, 0x02, 0x03, 0x04, 0x05}, true);
WAIT(200);
return;
case 22:
*want_action = true;
WAIT(500);
return;
case 23:
sxsim::log_state("F2action");
ESP_LOGI(TAG, "PHASE fsk_receive");
sxsim::inject({0xA5, 0xA6, 0xA7});
WAIT(300);
return;
case 24:
ESP_LOGI(TAG, "RES F3 packets=%u", (unsigned) P.n);
ESP_LOGI(TAG, "PHASE fsk_rx_before_send");
sxsim::arm_rx_on_standby({0xB4, 0xB5, 0xB6});
blocking(r, "F4", {0x09, 0x09, 0x09, 0x09}, true);
WAIT(500);
return;
case 25:
ESP_LOGI(TAG, "RES F4 after packets=%u", (unsigned) P.n);
sxsim::log_state("F4after");
blocking(r, "F4next", {0x0A});
WAIT(200);
return;
case 26:
ESP_LOGI(TAG, "PHASE fsk_raw_tx_mode");
r->set_mode_tx();
WAIT(400);
return;
case 27:
ESP_LOGI(TAG, "RES F5 packets=%u", (unsigned) P.n);
sxsim::log_state("F5");
r->set_mode_rx();
sxsim::log_state("F5rx");
WAIT(200);
return;
#ifdef SXSIM_PR
case 28:
ESP_LOGI(TAG, "PHASE fsk_async");
M.tx_ms = 80;
start_async(r, L, {0x91, 0x92});
start_async(r, L2, {0x93});
WAIT(400);
return;
case 29:
ESP_LOGI(TAG, "RES FA1 L.n=%u last=%s L2.n=%u", (unsigned) L.n, ename(L.last), (unsigned) L2.n);
sxsim::log_state("FA1");
M.tx_never = true;
start_async(r, L, {0x94}, 300);
WAIT(800);
return;
case 30:
M.tx_never = false;
ESP_LOGI(TAG, "RES FA2 L.n=%u last=%s warn=%d", (unsigned) L.n, ename(L.last), r->status_has_warning());
sxsim::log_state("FA2");
M.tx_ms = 1000;
start_async(r, L, {0x95});
WAIT(50);
return;
case 31:
ESP_LOGI(TAG, "RES FA3 cancel -> %d L.n=%u last=%s chip=%s", r->cancel_transmit_async(&L), (unsigned) L.n,
ename(L.last), sxsim::mode_name());
WAIT(1300);
return;
case 32:
ESP_LOGI(TAG, "RES FA3 later L.n=%u warn=%d", (unsigned) L.n, r->status_has_warning());
sxsim::log_state("FA3");
M.tx_ms = 50;
sxsim::arm_rx_on_standby({0xE4, 0xE5});
start_async(r, L, {0x96});
WAIT(300);
return;
case 33:
ESP_LOGI(TAG, "RES FA4 packets=%u L.n=%u", (unsigned) P.n, (unsigned) L.n);
sxsim::log_state("FA4");
WAIT(10);
return;
#endif
// ---------------- runtime mode change failure ----------------
case 40:
ESP_LOGI(TAG, "PHASE runtime_mode_fail");
M.tx_ms = 50;
prev_warn = prev_failed = 2;
watch_until = millis() + 6000;
M.fail_mode_writes = 3;
r->set_mode_standby();
ESP_LOGI(TAG, "RES R1 right_after warn=%d failed=%d", r->status_has_warning(), r->is_failed());
ESP_LOGI(TAG, "RES R1 send_while_broken -> %s", ename(r->transmit_packet(std::vector<uint8_t>{0x01})));
WAIT(6000);
return;
case 41:
ESP_LOGI(TAG, "RES R1 end warn=%d failed=%d", r->status_has_warning(), r->is_failed());
sxsim::log_state("R1");
blocking(r, "R1send", {0x0B, 0x0C});
sxsim::inject({0xF1});
WAIT(300);
return;
case 42:
ESP_LOGI(TAG, "RES R1rx packets=%u", (unsigned) P.n);
reboot_to(1);
return;
default:
if (s > 42)
step = 43;
return;
}
}
if (boot_case == 1) {
switch (s) {
case 0:
ESP_LOGI(TAG, "PHASE blocking_timeout");
M.tx_never = true;
blocking(r, "T1", {0x01, 0x02});
M.tx_never = false;
ESP_LOGI(TAG, "RES T1 chip=%s", sxsim::mode_name());
WAIT(1500);
return;
case 1:
ESP_LOGI(TAG, "RES T1 later warn=%d failed=%d chip=%s", r->status_has_warning(), r->is_failed(),
sxsim::mode_name());
blocking(r, "T2", {0x03});
WAIT(300);
return;
case 2:
reboot_to(2);
return;
default:
return;
}
}
if (boot_case == 2 || boot_case == 3) {
switch (s) {
case 0:
ESP_LOGI(TAG, "RES S%u setup failed=%d warn=%d", (unsigned) boot_case, r->is_failed(), r->status_has_warning());
blocking(r, boot_case == 2 ? "S2" : "S3", {0x01});
sxsim::log_state("S");
WAIT(3500);
return;
case 1:
ESP_LOGI(TAG, "RES S%u later failed=%d", (unsigned) boot_case, r->is_failed());
sxsim::log_state("Slater");
reboot_to(boot_case + 1);
return;
default:
return;
}
}
if (boot_case == 4 && s == 0) {
ESP_LOGI(TAG, "RES S4 setup failed=%d", r->is_failed());
ESP_LOGI(TAG, "ALLDONE");
}
}
} // namespace sxrun
@@ -0,0 +1,459 @@
#pragma once
// Fake SX1276 behind a software SPI bus (ESP32, ESP-IDF). The bus's CLK/MOSI/MISO pins and the radio's CS pin are
// swapped for fake GPIOPin objects at on_boot priority 800 (after the bus setup at 1000, before the radio at 400), so
// the real SPIDelegateBitBash clocks every bit into this register model. DIO0 is a real pad: after the radio's setup
// it is switched to input/output and driven by the model, so the driver's rising edge interrupt fires as on a chip.
// Works with any SX127x driver revision because only public setters are used (set_clk/set_mosi/set_miso/set_cs_pin).
//
// Modelled: banked FSK/LoRa registers (LongRangeMode changes only in sleep), RegOpMode with instant transitions,
// the LoRa 256 byte FIFO with FifoAddrPtr/TxBase/RxBase, the FSK 64 byte FIFO (one FIFO for TX and RX, variable
// length byte first), RegIrqFlags (write 1 to clear) with RX_DONE/TX_DONE, RegIrqFlags2 PayloadReady/PacketSent,
// NbRxBytes/RxCurrAddr/PktRssi/PktSnr, version 0x12, DIO0 mapping 00 (RxDone, FSK PayloadReady/PacketSent) and 01
// (TxDone). A LoRa send ends in standby by itself; FSK stays in TX with PacketSent. While a send is still going out
// the chip ignores any mode request except standby and sleep (the SX1276 stays in TX when asked for RX).
// A read of RegVersion resets the model: the driver reads it first after every pulse of the reset pin.
//
// Faults: fail_mode_writes (the next N RegOpMode writes are ignored, so the mode reads back the old value),
// tx_never (a send never completes), rx_on_standby (a received packet completes just as a send puts the chip in
// standby), version (a wrong chip).
#include <driver/gpio.h>
#include <esp_timer.h>
#include <freertos/FreeRTOS.h>
#include "esphome/components/spi/spi.h"
#include "esphome/components/sx127x/sx127x.h"
#include "esphome/core/hal.h"
#include "esphome/core/log.h"
#include "spibits.hpp"
namespace sxsim {
using namespace esphome;
static const char *const TAG = "sxsim";
enum : uint8_t {
OP_SLEEP = 0,
OP_STDBY = 1,
OP_TX = 3,
OP_RX = 5,
IRQ_RX_DONE = 0x40,
IRQ_CRC_ERR = 0x20,
IRQ_TX_DONE = 0x08,
};
struct Model {
uint8_t fsk[128]{}; // FSK bank and the common registers
uint8_t lora[128]{}; // LoRa bank
uint8_t opmode{0x09};
uint8_t lfifo[256]{};
uint8_t ffifo[64]{};
uint8_t fhead{0}, fcount{0};
uint8_t irq{0};
bool payload_ready{false}, packet_sent{false}, tx_active{false};
uint8_t rx_len{0}, pkt_rssi{100};
int8_t pkt_snr{20};
// faults
uint32_t fail_mode_writes{0};
bool tx_never{false};
uint32_t tx_ms{50};
bool rx_on_standby{false};
uint8_t pend_rx[64]{};
uint8_t pend_rx_len{0};
uint8_t version{0x12};
// records
uint8_t last_tx[256]{};
uint16_t last_tx_len{0};
uint32_t tx_started{0}, tx_done{0}, tx_aborted{0}, rx_injected{0}, rx_dropped{0}, mode_writes{0},
mode_ignored_fault{0}, mode_ignored_tx{0}, resets{0}, fifo_underflow{0}, rx_lost_in_fifo{0};
int dio0_gpio{-1};
bool drive{false};
bool dio0{false};
// trace of SPI transactions
bool tracing{false};
char trace[4096]{};
size_t tlen{0};
int timer_action{0}; // 1 start, 2 stop; done outside the lock
esp_timer_handle_t timer{nullptr};
} M;
static portMUX_TYPE MUX = portMUX_INITIALIZER_UNLOCKED;
inline bool lora_mode() { return (M.opmode & 0x80) != 0; }
inline uint8_t mode() { return M.opmode & 0x07; }
inline bool is_common(uint8_t a) { return a == 0x01 || (a >= 0x06 && a <= 0x0C) || a >= 0x40; }
inline uint8_t *bank(uint8_t a) { return (is_common(a) || !lora_mode()) ? &M.fsk[a] : &M.lora[a]; }
inline const char *mode_name() {
static const char *const NAMES[8] = {"SLEEP", "STDBY", "FSTX", "TX", "FSRX", "RX", "RXSINGLE", "CAD"};
return NAMES[mode()];
}
inline void ffifo_push(uint8_t v) {
if (M.fcount < 64) {
M.ffifo[(M.fhead + M.fcount) % 64] = v;
M.fcount++;
}
}
inline uint8_t ffifo_pop() {
if (M.fcount == 0) {
M.fifo_underflow++;
return 0;
}
uint8_t v = M.ffifo[M.fhead];
M.fhead = (M.fhead + 1) % 64;
M.fcount--;
return v;
}
inline void ffifo_clear() {
if (M.payload_ready && M.fcount > 0)
M.rx_lost_in_fifo++;
M.fhead = M.fcount = 0;
M.payload_ready = false;
}
inline void update_dio0() {
bool lvl;
if (lora_mode()) {
const uint8_t map = M.fsk[0x40] & 0xC0;
lvl = map == 0x40 ? (M.irq & IRQ_TX_DONE) != 0 : map == 0x00 ? (M.irq & IRQ_RX_DONE) != 0 : false;
} else {
lvl = mode() == OP_TX ? M.packet_sent : M.payload_ready;
}
M.dio0 = lvl;
if (M.drive)
gpio_set_level((gpio_num_t) M.dio0_gpio, lvl);
}
inline void abort_tx_locked() {
if (M.tx_active) {
M.tx_active = false;
M.tx_aborted++;
M.timer_action = 2;
}
}
inline void reset_locked() {
abort_tx_locked();
memset(M.fsk, 0, sizeof(M.fsk));
memset(M.lora, 0, sizeof(M.lora));
M.opmode = 0x09;
M.irq = 0;
M.fhead = M.fcount = 0;
M.payload_ready = M.packet_sent = false;
M.resets++;
}
// A packet completes in the current modulation; dropped unless the chip is receiving
inline void inject_locked(const uint8_t *data, uint8_t len, bool crc_error) {
if (mode() != OP_RX) {
M.rx_dropped++;
return;
}
M.rx_injected++;
if (lora_mode()) {
const uint8_t base = M.lora[0x0F];
for (uint8_t i = 0; i < len; i++)
M.lfifo[(uint8_t) (base + i)] = data[i];
M.lora[0x10] = base;
M.rx_len = len;
M.irq |= IRQ_RX_DONE | 0x10 | (crc_error ? IRQ_CRC_ERR : 0);
} else if (!crc_error) {
ffifo_clear();
if (M.fsk[0x30] & 0x80)
ffifo_push(len); // variable length: length byte first
for (uint8_t i = 0; i < len; i++)
ffifo_push(data[i]);
M.payload_ready = true;
}
update_dio0();
}
inline void start_tx_locked() {
M.tx_started++;
M.last_tx_len = 0;
if (lora_mode()) {
const uint8_t base = M.lora[0x0E];
const uint8_t n = M.lora[0x22];
for (uint16_t i = 0; i < n; i++)
M.last_tx[M.last_tx_len++] = M.lfifo[(uint8_t) (base + i)];
} else {
uint8_t n;
if (M.fsk[0x30] & 0x80) {
n = ffifo_pop();
M.last_tx[M.last_tx_len++] = n;
} else {
n = M.fsk[0x32];
}
for (uint16_t i = 0; i < n && M.last_tx_len < sizeof(M.last_tx); i++)
M.last_tx[M.last_tx_len++] = ffifo_pop();
if (M.fcount == 0)
M.payload_ready = false;
}
M.tx_active = true;
M.timer_action = M.tx_never ? 0 : 1;
}
inline void write_mode_locked(uint8_t v) {
M.mode_writes++;
if (M.fail_mode_writes > 0) {
M.fail_mode_writes--;
M.mode_ignored_fault++;
return;
}
const uint8_t prev = mode(), nm = v & 0x07;
if (prev == OP_TX && M.tx_active && nm != OP_STDBY && nm != OP_SLEEP && nm != OP_TX) {
M.mode_ignored_tx++; // still sending: only standby or sleep stop it
return;
}
if (nm == OP_STDBY && prev == OP_RX && M.rx_on_standby) {
M.rx_on_standby = false;
inject_locked(M.pend_rx, M.pend_rx_len, false); // completed just before the standby took effect
}
const uint8_t lr = prev == OP_SLEEP ? (v & 0x80) : (M.opmode & 0x80);
M.opmode = lr | (v & 0x78) | nm;
if (prev == OP_TX && nm != OP_TX) {
abort_tx_locked();
M.packet_sent = false;
}
if (nm == OP_SLEEP || (nm == OP_RX && prev != OP_RX && !lora_mode()))
ffifo_clear();
if (nm == OP_TX && prev != OP_TX)
start_tx_locked();
update_dio0();
}
inline uint8_t read_locked(uint8_t a) {
if (a == 0x00) {
if (lora_mode())
return M.lfifo[M.lora[0x0D]++];
uint8_t v = ffifo_pop();
if (M.fcount == 0)
M.payload_ready = false;
return v;
}
if (a == 0x01)
return M.opmode;
if (a == 0x42) {
reset_locked();
return M.version;
}
if (lora_mode()) {
switch (a) {
case 0x12:
return M.irq;
case 0x13:
return M.rx_len;
case 0x19:
return (uint8_t) M.pkt_snr;
case 0x1A:
return M.pkt_rssi;
default:
break;
}
} else {
if (a == 0x3F)
return (M.payload_ready ? 0x04 : 0) | (M.packet_sent ? 0x08 : 0) | (M.fcount == 0 ? 0x40 : 0);
if (a == 0x3B)
return M.fsk[0x3B] & ~0x20; // image calibration finishes at once
}
return *bank(a);
}
inline void write_locked(uint8_t a, uint8_t v) {
if (a == 0x00) {
if (lora_mode())
M.lfifo[M.lora[0x0D]++] = v;
else
ffifo_push(v);
return;
}
if (a == 0x01) {
write_mode_locked(v);
return;
}
if (lora_mode() && a == 0x12) {
M.irq &= ~v;
return;
}
*bank(a) = v;
}
// One SPI transaction as the trace shows it: the address byte and up to 16 data bytes
struct TraceTxn {
uint8_t addr{0}, first{0};
uint16_t nbytes{0};
uint8_t bytes[16]{};
void add(uint8_t v) {
if (nbytes == 0)
first = v;
if (nbytes < 16)
bytes[nbytes] = v;
nbytes++;
}
};
inline void trace_add(const TraceTxn &t) {
const bool wr = t.addr & 0x80;
const uint8_t a = t.addr & 0x7F;
char buf[64];
int n;
if (a == 0 && t.nbytes != 1) {
n = snprintf(buf, sizeof(buf), " %cF[%u]", wr ? 'W' : 'R', t.nbytes);
for (uint16_t i = 0; i < t.nbytes && i < 16 && n < 56; i++)
n += snprintf(buf + n, sizeof(buf) - n, "%02X", t.bytes[i]);
} else {
n = snprintf(buf, sizeof(buf), " %c%02X%c%02X", wr ? 'W' : 'R', a, wr ? '=' : ':', t.first);
}
if (M.tlen + n < sizeof(M.trace) - 1) {
memcpy(M.trace + M.tlen, buf, n);
M.tlen += n;
M.trace[M.tlen] = 0;
}
}
inline void timer_action() {
int act;
portENTER_CRITICAL(&MUX);
act = M.timer_action;
M.timer_action = 0;
portEXIT_CRITICAL(&MUX);
if (act != 0)
esp_timer_stop(M.timer);
if (act == 1)
ESP_ERROR_CHECK(esp_timer_start_once(M.timer, (uint64_t) M.tx_ms * 1000));
}
inline void tx_timer_cb(void *) {
portENTER_CRITICAL(&MUX);
if (M.tx_active && mode() == OP_TX) {
M.tx_active = false;
M.tx_done++;
if (lora_mode()) {
M.irq |= IRQ_TX_DONE;
M.opmode = (M.opmode & ~0x07) | OP_STDBY; // LoRa returns to standby by itself
} else {
M.packet_sent = true;
}
update_dio0();
}
portEXIT_CRITICAL(&MUX);
}
// The SPI side of the model: register reads and writes per byte, the trace capture, and the TX timer after each
// transaction (esp_timer calls assert inside a critical section)
struct SimChip : spibits::SpiChip {
uint8_t addr{0};
TraceTxn txn, done;
bool pending{false};
void lock() override { portENTER_CRITICAL(&MUX); }
void unlock() override { portEXIT_CRITICAL(&MUX); }
uint8_t reply_locked(uint16_t p) override {
if (p == 0 || (addr & 0x80))
return 0;
const uint8_t v = read_locked(addr & 0x7F);
txn.add(v);
return v;
}
void byte_locked(uint16_t p, uint8_t v) override {
if (p == 0) {
addr = txn.addr = v;
} else if (addr & 0x80) {
txn.add(v);
write_locked(addr & 0x7F, v);
}
}
void begin_locked() override { txn.nbytes = 0; }
void end_locked() override {
pending = M.tracing && pos > 0;
if (pending)
done = txn;
update_dio0();
}
void after_end() override {
if (pending) {
pending = false;
trace_add(done);
}
timer_action();
}
};
static SimChip CHIP;
static spibits::PinSet PINS;
// on_boot priority 800: after the SPI bus setup, before the radio's
inline void install(spi::SPIComponent *bus, sx127x::SX127x *radio, int dio0_gpio) {
PINS.bind(&CHIP);
bus->set_clk(&PINS.clk);
bus->set_mosi(&PINS.mosi);
bus->set_miso(&PINS.miso);
radio->set_cs_pin(&PINS.cs);
M.dio0_gpio = dio0_gpio;
esp_timer_create_args_t args{};
args.callback = tx_timer_cb;
args.name = "sxsim_tx";
ESP_ERROR_CHECK(esp_timer_create(&args, &M.timer));
ESP_LOGI(TAG, "fake SX1276 installed");
}
// After the radio's setup(): DIO0 becomes an output the model drives, keeping the input and its interrupt
inline void late_pin_setup() {
gpio_set_direction((gpio_num_t) M.dio0_gpio, GPIO_MODE_INPUT_OUTPUT);
portENTER_CRITICAL(&MUX);
M.drive = true;
update_dio0();
portEXIT_CRITICAL(&MUX);
}
inline void inject(std::initializer_list<uint8_t> data, bool crc_error = false) {
uint8_t buf[64];
uint8_t n = 0;
for (auto b : data)
buf[n++] = b;
portENTER_CRITICAL(&MUX);
inject_locked(buf, n, crc_error);
portEXIT_CRITICAL(&MUX);
}
inline void arm_rx_on_standby(std::initializer_list<uint8_t> data) {
portENTER_CRITICAL(&MUX);
M.pend_rx_len = 0;
for (auto b : data)
M.pend_rx[M.pend_rx_len++] = b;
M.rx_on_standby = true;
portEXIT_CRITICAL(&MUX);
}
inline void trace_start() {
M.tlen = 0;
M.trace[0] = 0;
M.tracing = true;
}
inline void trace_stop() { M.tracing = false; }
inline void log_trace(const char *label) {
for (size_t off = 0; off < M.tlen; off += 180) {
char chunk[181];
size_t n = std::min<size_t>(180, M.tlen - off);
memcpy(chunk, M.trace + off, n);
chunk[n] = 0;
ESP_LOGI("tr", "TRACE %s %u:%s", label, (unsigned) off, chunk);
}
}
inline void hex(char *out, size_t cap, const uint8_t *d, size_t n) {
size_t o = 0;
for (size_t i = 0; i < n && o + 3 < cap; i++)
o += snprintf(out + o, cap - o, "%02X", d[i]);
out[o] = 0;
}
inline void log_state(const char *label) {
char tx[80];
hex(tx, sizeof(tx), M.last_tx, std::min<size_t>(M.last_tx_len, 32));
ESP_LOGI(TAG,
"STATE %s mode=%s lora=%d dio0=%d irq=%02X fsk_fifo=%u tx_started=%u tx_done=%u tx_aborted=%u last_tx=[%s] "
"rx_inj=%u rx_drop=%u rx_lost=%u mode_writes=%u ign_fault=%u ign_tx=%u resets=%u underflow=%u",
label, mode_name(), lora_mode(), M.dio0, M.irq, M.fcount, (unsigned) M.tx_started, (unsigned) M.tx_done,
(unsigned) M.tx_aborted, tx, (unsigned) M.rx_injected, (unsigned) M.rx_dropped, (unsigned) M.rx_lost_in_fifo,
(unsigned) M.mode_writes, (unsigned) M.mode_ignored_fault, (unsigned) M.mode_ignored_tx, (unsigned) M.resets,
(unsigned) M.fifo_underflow);
}
} // namespace sxsim
@@ -0,0 +1,70 @@
# ESP32-PICO-D4 (board pico32, ESP-IDF): sx127x driven by the fake SX1276 in sxsim.hpp, scenarios in sxrun.hpp.
# Build sxpr.yaml (a driver with the async API) or sxbase.yaml with `-s name <name>`; they set `flags` and include this.
# No Wi-Fi: everything is judged from the serial log ("RES", "EV", "STATE", "TRACE" lines, "ALLDONE" at the end).
# The SPI pins and CS are replaced by fake pins at on_boot 800; only DIO0 (GPIO4) is a real pad, driven by the model.
substitutions:
flags: -DSXSIM_BASE
esphome:
name: ${name}
includes: [spibits.hpp, sxsim.hpp, sxrun.hpp]
platformio_options:
build_flags: ${flags}
on_boot:
- priority: 800
then:
- lambda: |-
sxsim::install(id(spibus), id(radio), 4);
sxrun::boot(id(radio));
- priority: 399
then:
- lambda: sxsim::late_pin_setup();
esp32:
board: pico32
framework:
type: esp-idf
logger:
level: DEBUG
tx_buffer_size: 1024
spi:
id: spibus
interface: software
clk_pin: GPIO18
mosi_pin: GPIO23
miso_pin: GPIO19
sx127x:
id: radio
cs_pin: GPIO5
rst_pin: GPIO21
dio0_pin:
number: GPIO4
mode:
input: true
pulldown: true
frequency: 433920000
modulation: LORA
bandwidth: 125_0kHz
spreading_factor: 7
coding_rate: CR_4_5
preamble_size: 8
crc_enable: true
sync_value: [0x12]
rx_start: true
on_packet:
then:
- lambda: 'ESP_LOGI("ev", "EV trigger len=%u rssi=%.1f snr=%.2f", (unsigned) x.size(), rssi, snr);'
globals:
- id: want_action
type: bool
initial_value: "false"
interval:
- interval: 20ms
then:
- lambda: sxrun::run(id(radio), &id(want_action));
- if:
condition:
lambda: return id(want_action);
then:
- lambda: id(want_action) = false;
- sx127x.send_packet:
data: [0xC5, 0x51, 0x78, 0x82, 0xB7]
- lambda: sxsim::log_state("action");
@@ -0,0 +1,53 @@
"""apiwatch.py HOST PORT SECONDS [LOGFILE] (API_KEY in the env): print every state change of a device's entities with a
timestamp, starting with the states sent on connect (missing_state shows as 'unknown'). Used to check the tcp link's
`connected` binary sensor reads OFF before the first connection, not unknown. With LOGFILE, the device's log lines
(DEBUG and above, the level of the CONN and ETRX markers, no colours) go there too. Exits 1 when the API connection
drops during the watch."""
import asyncio
import pathlib
import sys
import time
sys.path.insert(0, str(pathlib.Path(__file__).resolve().parents[1]))
from aioesphomeapi import LogLevel # noqa: E402
from apiconn import connect, log_text # noqa: E402
HOST, PORT, SECS = sys.argv[1], int(sys.argv[2]), float(sys.argv[3])
LOGFILE = sys.argv[4] if len(sys.argv) > 4 else None
T0 = time.monotonic()
async def main():
lost = asyncio.Event()
async def on_stop(expected: bool) -> None:
if not expected:
print(f"{time.monotonic() - T0:8.3f} API connection lost", flush=True)
lost.set()
c = await connect(HOST, tries=60, timeout=5, port=PORT, on_stop=on_stop)
ents, _ = await c.list_entities_services()
names = {e.key: e.object_id for e in ents}
last = {}
def cb(s):
oid = names.get(s.key, s.key)
v = "unknown" if getattr(s, "missing_state", False) else s.state
if last.get(oid) != v:
last[oid] = v
print(f"{time.monotonic() - T0:8.3f} {oid} = {v}", flush=True)
c.subscribe_states(cb)
if LOGFILE:
logs = pathlib.Path(LOGFILE).open("w", encoding="utf-8", buffering=1) # noqa: SIM115
c.subscribe_logs(
lambda m: logs.write(log_text(m) + "\n"), log_level=LogLevel.LOG_LEVEL_DEBUG
)
await asyncio.sleep(SECS)
if lost.is_set():
sys.exit("FAIL the API connection dropped")
await c.disconnect()
asyncio.run(main())
@@ -0,0 +1,37 @@
substitutions:
name: tcpe1
peer: tcph3
peer_host: HOST_LAN_IP
srv_port: "46501"
raw_port: "46502"
probe_port: "46503"
peer_srv_port: "46301"
peer_raw_port: "46302"
probe_host: HOST_LAN_IP
probe_cli_port: "46504"
probe_allow: HOST_LAN_IP/32
et_key: Johnny Cash
api_port: "6053"
long_id: xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
esp32:
board: esp32dev
framework:
type: esp-idf
wifi:
ssid: !secret wifi_ssid
password: !secret wifi_password
power_save_mode: none
ota:
- platform: esphome
packages:
link: !include tcplink.yaml
packet_interface:
- platform: tcp
id: deny_srv
role: server
port: 46505
allowed_ips: 10.255.255.1/32
connected:
name: deny_srv connected
on_state:
- logger.log: {format: "CONN deny_srv %d", args: ["x ? 1 : 0"]}
@@ -0,0 +1,19 @@
substitutions:
name: tcph1
peer: tcph2
peer_host: 127.0.0.1
srv_port: "46101"
raw_port: "46102"
probe_port: "46103"
peer_srv_port: "46201"
peer_raw_port: "46202"
probe_host: 127.0.0.1
probe_cli_port: "46104"
probe_allow: 127.0.0.0/8
et_key: Johnny Cash
api_port: "46151"
long_id: xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
host:
mac_address: "62:23:45:AF:B3:01"
packages:
link: !include tcplink.yaml
+106
View File
@@ -0,0 +1,106 @@
#!/bin/bash
# run_pair.sh host|esp32 [RUN]: the tcp link timeline against one device under test (boot states, SIGSTOP/SIGCONT of
# the host peer, kill -9 and restart, silent and oversize raw servers, one client at a time, a refused client, oversize
# from a client). Logs go to <RUN>-*.txt (RUN defaults to the mode), with ANSI colours stripped. Exits 1 when a job ends
# with the wrong status, or the device logs show bad raw packets, an accepted 1023 byte send or a lost probe packet.
# host: the device is host build tcph1 here, its peer tcph2; the refused client connects from the computer's LAN
# address (LAN, macOS `ipconfig getifaddr en0` otherwise).
# esp32: the device is the ESP32 board NAME (default tcpe1, found with resolve.sh), its peer host build tcph3; the
# refused client connects to its deny_srv. The board's log is taken over the API into <RUN>-dut.txt.
# Build the host configs here first. env: API_KEY (apikey.txt otherwise), PY, and the ports of the example configs:
# API_PORT, PROBE_PORT, PROBE_CLI_PORT, DENY_PORT.
D=$(cd "$(dirname "$0")" && pwd); S=$(dirname "$D"); cd "$D" || exit 1; B=$D/.esphome/build; P=python3
M=${1:?host or esp32}; R=${2:-$M}
if [ "$M" = host ]; then
E=127.0.0.1; PEER=tcph2; API_PORT=${API_PORT:-46151}; PROBE_PORT=${PROBE_PORT:-46103}
PROBE_CLI_PORT=${PROBE_CLI_PORT:-46104}; LAN=${LAN:-$(ipconfig getifaddr en0 2>/dev/null)}
: "${LAN:?set LAN to the LAN address of this computer}"
WARM=8; RUNS=17; HOLD=15; WATCH=135; SILENT=58
REFUSED=(client "$LAN" "$PROBE_PORT" --bind "$LAN")
else
E=$(bash "$S/resolve.sh" "${NAME:-tcpe1}") || exit 1; PEER=tcph3; API_PORT=${API_PORT:-6053}
PROBE_PORT=${PROBE_PORT:-46503}; PROBE_CLI_PORT=${PROBE_CLI_PORT:-46504}
WARM=10; RUNS=55; HOLD=20; WATCH=200; SILENT=50
REFUSED=(client "$E" "${DENY_PORT:-46505}")
fi
for b in $( [ "$M" = host ] && echo tcph1) $PEER; do
[ -x "$B/$b/.pioenvs/$b/program" ] || { echo "build $b here first"; exit 1; }
done
API_KEY=${API_KEY:-$(cat apikey.txt 2>/dev/null)}
export API_KEY=${API_KEY:?set API_KEY or write apikey.txt}
ts() { echo "$(date +%T) $*" >> "$R-events.txt"; }
JOBS=(); FAIL=0
job() { # NAME EXIT CMD...: run CMD in the background into <RUN>-NAME.txt; its exit status must be EXIT at the end
local name=$1 want=$2; shift 2
(exec "$@" > "$R-$name.txt" 2>&1 < /dev/null) &
JOBS+=("$name:$want:$!")
}
run() { # NAME CMD...: run CMD into <RUN>-NAME.txt, which must exit 0
local name=$1; shift
"$@" > "$R-$name.txt" 2>&1 < /dev/null || { echo "JOB FAILED $name exit=$?"; FAIL=1; }
}
sig() { kill "-$1" "$H" || { echo "kill -$1 $PEER failed: it is not running"; FAIL=1; }; ts "$1" $PEER; }
: > "$R-events.txt"
if [ "$M" = host ]; then
job dut 124 timeout 140 "$B/tcph1/.pioenvs/tcph1/program"
ts start tcph1
fi
# The board's own log comes over the API in esp32 mode; a host build logs to its stdout
DUTLOG=(); [ "$M" = esp32 ] && DUTLOG=("$R-dut.txt")
job api 0 ${PY:-python3} apiwatch.py "$E" "$API_PORT" $WATCH "${DUTLOG[@]}"
job ps-silent 0 $P -I tcp_peer.py server "$PROBE_CLI_PORT" --duration $SILENT --silent-after 8
ts api and silent server
sleep $WARM
job peer-a 137 "$B/$PEER/.pioenvs/$PEER/program"; H=$!
ts start $PEER
sleep $RUNS; sig STOP
sleep 15; sig CONT
sleep $HOLD; sig 9
sleep 7
job peer-b 124 timeout 70 "$B/$PEER/.pioenvs/$PEER/program"
ts restart $PEER
sleep 1
job ps-over 0 $P -I tcp_peer.py server "$PROBE_CLI_PORT" --duration 20 --oversize-after 3 1100
ts probe server oversize
sleep 12
job pcA 0 $P -I tcp_peer.py client "$E" "$PROBE_PORT" --duration 30 --once --hold 15 --send 7
ts client A
sleep 5
job pcB 0 $P -I tcp_peer.py client "$E" "$PROBE_PORT" --duration 25 --once --send 5,1022
ts client B
sleep 25
ts client C refused
run pcC $P -I tcp_peer.py "${REFUSED[@]}" --duration 6 --once --expect-reject
sleep 2
ts client D oversize
run pcD $P -I tcp_peer.py client "$E" "$PROBE_PORT" --duration 10 --once --oversize-after 2 1100
ts done
for j in "${JOBS[@]}"; do
IFS=: read -r name want pid <<< "$j"
wait "$pid"; rc=$?
[ "$rc" = "$want" ] || { echo "JOB FAILED $name exit=$rc (expected $want), see $R-$name.txt"; FAIL=1; }
done
perl -pi -e 's/\e\[[0-9;]*m//g' "$R"-*.txt
# Every device log, the device under test's first: the raw streams carried no bad packet, and the 1023 byte send was
# refused while connected
for f in "$R-dut.txt" "$R-peer-a.txt" "$R-peer-b.txt"; do
st=$(grep -a 'STAT raw_srv' "$f" | tail -1)
if [ -z "$st" ]; then
echo "CHECK FAILED $f: no STAT line"; FAIL=1
elif ! grep -Eq 'raw_srv .* bad=0 gap=.*raw_cli .* bad=0 gap=' <<< "$st"; then
echo "CHECK FAILED $f: bad raw packets: $st"; FAIL=1
fi
n=$(grep -a -c -E 'bad payload|short packet|oversize packet' "$f")
[ "$n" = 0 ] || { echo "CHECK FAILED $f: $n bad packet warnings"; FAIL=1; }
! grep -a -q '1023 byte send returned 1' "$f" || { echo "CHECK FAILED $f: the 1023 byte send was accepted"; FAIL=1; }
grep -a -q '1023 byte send returned 0 connected=1' "$f" || echo "note $f: no 1023 byte send while connected"
done
# Every checked size the raw peer clients sent reached the device's probe server intact
for f in "$R-pcA.txt" "$R-pcB.txt"; do
while read -r size sum; do
grep -a -q "probe_srv RX size=$size sum=$sum" "$R-dut.txt" ||
{ echo "CHECK FAILED $f: size $size sum $sum never reached probe_srv"; FAIL=1; }
done < <(grep -a 'SENT size' "$f" | sed -E 's/.*SENT size ([0-9]+) sum ([0-9]+).*/\1 \2/')
done
echo "RUN $R done failed=$FAIL"
exit $FAIL
@@ -0,0 +1,248 @@
"""Raw peer for the tcp packet_interface link (esphome#21119): 2 byte little endian length, then the packet; a zero
length packet is a keep-alive. Logs every connection, frame and close with times, so keep-alive pacing, the receive
timeout, reconnects and oversized-length handling can be read from the output.
tcp_peer.py client HOST PORT [opts] connect (again after each close until --duration ends)
tcp_peer.py server PORT [opts] accept connections, one thread each
--duration S stop after S seconds (default 60)
--keepalive S send a keep-alive every S seconds (0 = never; default 2)
--silent-after S stop sending anything S seconds into each connection (keep reading)
--oversize-after S N S seconds into the first connection send a header announcing N bytes, then hold the socket
--send SIZES comma list of packet sizes sent once after connecting, payload byte i = (seq + i) & 0xff
--bind ADDR source address (client)
--once client: do not reconnect after a close
--hold S client: close after S seconds of connection (0 = keep)
--expect-reject client: pass only when the peer accepts the connection, then closes or resets it before
sending anything (how an allowed_ips server rejects a source); a refused connect fails
Exits 1 when no connection was made, a --send size did not go out, or the peer stayed connected after --oversize-after.
"""
import argparse
import contextlib
import socket
import struct
import sys
import threading
import time
T0 = time.monotonic()
LOCK = threading.Lock()
def log(*a):
with LOCK:
print(f"{time.monotonic() - T0:8.3f}", *a, flush=True)
def recv_exact(sock, n):
buf = b""
while len(buf) < n:
chunk = sock.recv(n - len(buf))
if not chunk:
return None
buf += chunk
return buf
# what the exit status checks: connections made, and whether the peer closed after an oversize header
RESULT = {"conns": 0, "over_closed": False, "rejected": False, "sent_all": False}
def run_conn(sock, tag, args, first):
start = time.monotonic()
stop = threading.Event()
stats = {
"frames": 0,
"ka": [],
"sizes": [],
"last_ka": None,
"over": False,
"local_close": False,
}
RESULT["conns"] += 1
def sender():
last = 0.0
sent_sizes = False
over_done = False
while not stop.is_set() and time.monotonic() - T0 < args.duration:
el = time.monotonic() - start
silent = args.silent_after is not None and el >= args.silent_after
try:
if (
first
and args.oversize_after
and not over_done
and el >= args.oversize_after[0]
):
sock.sendall(struct.pack("<H", int(args.oversize_after[1])))
log(tag, "SENT oversize header", int(args.oversize_after[1]))
over_done = stats["over"] = True
if not silent and not sent_sizes and args.send:
for seq, size in enumerate(int(x) for x in args.send.split(",")):
payload = bytes([seq & 0xFF, seq >> 8]) + bytes(
(seq + i) & 0xFF for i in range(2, size)
)
sock.sendall(struct.pack("<H", size) + payload[:size])
log(tag, "SENT size", size, "sum", sum(payload[:size]))
time.sleep(0.2)
sent_sizes = RESULT["sent_all"] = True
if (
not silent
and not over_done
and args.keepalive
and el - last >= args.keepalive
):
sock.sendall(b"\x00\x00")
last = el
if args.hold and el >= args.hold:
log(tag, "HOLD over, closing")
stats["local_close"] = True
sock.shutdown(socket.SHUT_RDWR)
return
except OSError as e:
log(tag, "send error", e)
return
time.sleep(0.05)
th = threading.Thread(target=sender, daemon=True)
th.start()
reason = "duration"
remote = False # the peer closed or reset the connection (not a timeout or our own shutdown)
sock.settimeout(0.5)
while time.monotonic() - T0 < args.duration:
try:
first_byte = recv_exact(sock, 1)
except TimeoutError:
continue # nothing of the next header has arrived yet
except OSError as e:
reason = f"error {e}"
remote = isinstance(e, (ConnectionResetError, BrokenPipeError))
break
if first_byte is None:
reason = "EOF (peer closed)"
remote = True
break
# the rest of the frame is read with a long timeout, so a slow header byte is not lost
sock.settimeout(5)
try:
rest = recv_exact(sock, 1)
if rest is None:
reason = "EOF in header"
remote = True
break
(n,) = struct.unpack("<H", first_byte + rest)
body = recv_exact(sock, n) if n else b""
except OSError as e:
reason = f"error in frame {e}"
remote = isinstance(e, (ConnectionResetError, BrokenPipeError))
break
sock.settimeout(0.5)
if body is None:
reason = "EOF in body"
remote = True
break
stats["frames"] += 1
now = time.monotonic()
if n == 0:
if stats["last_ka"] is not None:
stats["ka"].append(now - stats["last_ka"])
stats["last_ka"] = now
else:
stats["sizes"].append(n)
stats["last_ka"] = (
None # a keep-alive is only due after an idle keep_alive interval
)
log(tag, "RX size", n)
stop.set()
dur = time.monotonic() - start
ka = stats["ka"]
kas = (
f"keepalive gaps n={len(ka)} min={min(ka):.2f} max={max(ka):.2f}"
if ka
else "keepalive gaps n=0"
)
log(
tag,
f"CLOSE after {dur:.2f}s: {reason}; frames={stats['frames']} data={len(stats['sizes'])} {kas}",
)
remote = remote and not stats["local_close"]
if stats["over"] and remote:
RESULT["over_closed"] = True
if remote and stats["frames"] == 0:
RESULT["rejected"] = True # closed by the peer before it sent anything
with contextlib.suppress(OSError):
sock.close()
def main():
p = argparse.ArgumentParser()
p.add_argument("role", choices=["client", "server"])
p.add_argument("a")
p.add_argument("b", nargs="?")
p.add_argument("--duration", type=float, default=60)
p.add_argument("--keepalive", type=float, default=2)
p.add_argument("--silent-after", type=float)
p.add_argument("--oversize-after", nargs=2, type=float)
p.add_argument("--send")
p.add_argument("--bind")
p.add_argument("--once", action="store_true")
p.add_argument("--hold", type=float, default=0)
p.add_argument("--expect-reject", action="store_true")
args = p.parse_args()
if args.role == "client":
host, port = args.a, int(args.b)
first = True
while time.monotonic() - T0 < args.duration:
s = socket.socket()
if args.bind:
s.bind((args.bind, 0))
s.settimeout(5)
try:
s.connect((host, port))
except OSError as e:
log("client connect failed", e)
s.close()
if args.once:
break
time.sleep(1)
continue
log("client CONNECT from", s.getsockname()[0])
run_conn(s, "client", args, first)
first = False
if args.once:
break
time.sleep(0.5)
else:
port = int(args.a)
ls = socket.socket()
ls.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
ls.bind(("0.0.0.0", port))
ls.listen(4)
ls.settimeout(0.5)
n = 0
while time.monotonic() - T0 < args.duration:
try:
c, addr = ls.accept()
except TimeoutError:
continue
n += 1
log(f"server ACCEPT #{n}")
threading.Thread(
target=run_conn, args=(c, f"conn{n}", args, n == 1), daemon=True
).start()
ls.close()
time.sleep(0.6)
if not RESULT["conns"]:
sys.exit("FAIL no connection")
if args.send and not RESULT["sent_all"]:
sys.exit("FAIL not every --send size went out")
if args.expect_reject and not RESULT["rejected"]:
sys.exit("FAIL the peer kept the connection")
if args.oversize_after and not RESULT["over_closed"]:
sys.exit("FAIL the peer did not close after the oversize header")
if __name__ == "__main__":
main()
@@ -0,0 +1,262 @@
# Shared part of a tcp packet_interface peer (esphome#21119). Each device runs:
# et_srv / et_cli : entity_transport both ways over xxtea + fragmentation, one as server, one as client
# raw_srv / raw_cli : no layers, max_packet_size 1022, a size sweep with checked payloads both ways
# probe_srv : server for the raw Python peer (allowed_ips: ${probe_allow})
# probe_cli : client of the raw Python peer's server
# substitutions: name, peer, peer_host, srv_port, raw_port, probe_port, peer_srv_port, peer_raw_port, probe_host,
# probe_cli_port, probe_allow, et_key, api_port, long_id
esphome:
name: ${name}
on_boot:
- priority: 600
then:
- lambda: |-
using esphome::packet_interface::PacketBuffer;
using esphome::packet_interface::PacketMetaData;
auto check = [](const char *link, uint32_t *rx, uint32_t *bad, uint32_t *gaps, int32_t *last) {
return [=](const PacketBuffer &d, const PacketMetaData &) {
static uint8_t buf[1100];
size_t n = d.size();
(*rx)++;
if (n > sizeof(buf)) { (*bad)++; ESP_LOGW("raw", "%s oversize packet %u", link, (unsigned) n); return; }
d.copy_to(buf, n);
if (n < 2) { (*bad)++; ESP_LOGW("raw", "%s short packet %u", link, (unsigned) n); return; }
uint16_t seq = buf[0] | (buf[1] << 8);
bool ok = true;
for (size_t i = 2; i < n; i++)
if (buf[i] != (uint8_t) (seq + i)) { ok = false; break; }
if (!ok) { (*bad)++; ESP_LOGW("raw", "%s bad payload seq=%u size=%u", link, seq, (unsigned) n); }
if (*last >= 0 && seq != (uint16_t) (*last + 1)) {
(*gaps)++;
ESP_LOGI("raw", "%s seq gap %d -> %u", link, (int) *last, seq);
}
*last = seq;
if (n >= 1000) ESP_LOGD("raw", "%s RX seq=%u size=%u ok=%d", link, seq, (unsigned) n, ok);
};
};
id(raw_srv)->add_packet_interface_listener(check("raw_srv", &id(rs_rx), &id(rs_bad), &id(rs_gap), &id(rs_last)));
id(raw_cli)->add_packet_interface_listener(check("raw_cli", &id(rc_rx), &id(rc_bad), &id(rc_gap), &id(rc_last)));
auto probe = [](const char *link) {
return [=](const PacketBuffer &d, const PacketMetaData &) {
static uint8_t buf[1100];
size_t n = d.size();
if (n > sizeof(buf)) { ESP_LOGW("probe", "%s oversize packet %u", link, (unsigned) n); return; }
d.copy_to(buf, n);
uint32_t sum = 0;
for (size_t i = 0; i < n; i++) sum += buf[i];
ESP_LOGI("probe", "%s RX size=%u sum=%u", link, (unsigned) n, (unsigned) sum);
};
};
id(probe_srv)->add_packet_interface_listener(probe("probe_srv"));
id(probe_cli)->add_packet_interface_listener(probe("probe_cli"));
- priority: -100
then:
- delay: 20s
- script.execute: oversend
logger:
level: DEBUG
api:
port: ${api_port}
reboot_timeout: 0s
encryption:
key: !secret api_key
globals:
- {id: counter, type: int, initial_value: "0"}
- {id: rs_rx, type: uint32_t, initial_value: "0"}
- {id: rs_bad, type: uint32_t, initial_value: "0"}
- {id: rs_gap, type: uint32_t, initial_value: "0"}
- {id: rs_last, type: int32_t, initial_value: "-1"}
- {id: rc_rx, type: uint32_t, initial_value: "0"}
- {id: rc_bad, type: uint32_t, initial_value: "0"}
- {id: rc_gap, type: uint32_t, initial_value: "0"}
- {id: rc_last, type: int32_t, initial_value: "-1"}
- {id: rs_tx, type: uint32_t, initial_value: "0"}
- {id: rs_ref, type: uint32_t, initial_value: "0"}
- {id: rc_tx, type: uint32_t, initial_value: "0"}
- {id: rc_ref, type: uint32_t, initial_value: "0"}
- {id: rs_seq, type: uint16_t, initial_value: "0"}
- {id: rc_seq, type: uint16_t, initial_value: "0"}
- {id: sweep_idx, type: uint32_t, initial_value: "0"}
packet_interface:
- platform: tcp
id: et_srv
role: server
port: ${srv_port}
<<: &timing {keep_alive: 2s, timeout: 6s, reconnect_interval: 3s}
encryption: ${et_key}
fragmentation:
max_packet_size: 2048
connected:
name: et_srv connected
on_state:
- logger.log: {format: "CONN et_srv %d", args: ["x ? 1 : 0"]}
- platform: tcp
id: et_cli
role: client
host: ${peer_host}
port: ${peer_srv_port}
<<: *timing
encryption: ${et_key}
fragmentation:
max_packet_size: 2048
connected:
name: et_cli connected
on_state:
- logger.log: {format: "CONN et_cli %d", args: ["x ? 1 : 0"]}
- platform: tcp
id: raw_srv
role: server
port: ${raw_port}
<<: *timing
connected:
name: raw_srv connected
on_state:
- logger.log: {format: "CONN raw_srv %d", args: ["x ? 1 : 0"]}
- platform: tcp
id: raw_cli
role: client
host: ${peer_host}
port: ${peer_raw_port}
<<: *timing
connected:
name: raw_cli connected
on_state:
- logger.log: {format: "CONN raw_cli %d", args: ["x ? 1 : 0"]}
- platform: tcp
id: probe_srv
role: server
port: ${probe_port}
<<: *timing
allowed_ips: ${probe_allow}
connected:
name: probe_srv connected
on_state:
- logger.log: {format: "CONN probe_srv %d", args: ["x ? 1 : 0"]}
- platform: tcp
id: probe_cli
role: client
host: ${probe_host}
port: ${probe_cli_port}
<<: *timing
connected:
name: probe_cli connected
on_state:
- logger.log: {format: "CONN probe_cli %d", args: ["x ? 1 : 0"]}
interval:
- interval: 1s
then:
- lambda: id(counter)++;
- interval: 250ms
then:
- lambda: |-
using esphome::packet_interface::PacketBuffer;
static const uint16_t SIZES[] = {2, 3, 16, 64, 255, 256, 511, 512, 700, 1000, 1020, 1021, 1022};
static uint8_t buf[1100];
uint16_t size = SIZES[id(sweep_idx) % (sizeof(SIZES) / sizeof(SIZES[0]))];
id(sweep_idx)++;
auto send = [&](esphome::tcp::TcpPacketInterface *link, uint16_t &seq, uint32_t &tx, uint32_t &ref) {
buf[0] = seq; buf[1] = seq >> 8;
for (size_t i = 2; i < size; i++) buf[i] = (uint8_t) (seq + i);
if (link->send_to_interface(PacketBuffer(buf, size), {})) { seq++; tx++; } else { ref++; }
};
send(id(raw_srv), id(rs_seq), id(rs_tx), id(rs_ref));
send(id(raw_cli), id(rc_seq), id(rc_tx), id(rc_ref));
- interval: 10s
then:
- lambda: |-
ESP_LOGI("stat", "STAT raw_srv tx=%u ref=%u rx=%u bad=%u gap=%u | raw_cli tx=%u ref=%u rx=%u bad=%u gap=%u | cnt=%d",
id(rs_tx), id(rs_ref), id(rs_rx), id(rs_bad), id(rs_gap), id(rc_tx), id(rc_ref), id(rc_rx), id(rc_bad),
id(rc_gap), id(counter));
script:
- id: oversend
then:
- lambda: |-
using esphome::packet_interface::PacketBuffer;
static uint8_t big[1023];
bool r = id(raw_cli)->send_to_interface(PacketBuffer(big, sizeof(big)), {});
// refused is only meaningful while the link is up
ESP_LOGI("raw", "1023 byte send returned %d connected=%d", r, id(raw_cli)->is_connected());
sensor:
- platform: template
id: cnt
lambda: return id(counter);
update_interval: 1s
- platform: template
id: longa
lambda: return id(counter) * 0.5f;
update_interval: 5s
- platform: entity_transport
id: r_srv_cnt
entity_transport_id: ets
provider: ${peer}
remote_id: cnt
on_value:
- logger.log: {format: "ETRX et_srv cnt=%.0f", args: [x]}
- platform: entity_transport
id: r_cli_cnt
entity_transport_id: etc
provider: ${peer}
remote_id: cnt
on_value:
- logger.log: {format: "ETRX et_cli cnt=%.0f", args: [x]}
- platform: entity_transport
id: r_srv_long
entity_transport_id: ets
provider: ${peer}
remote_id: ${long_id}9
on_value:
- logger.log: {format: "ETRX et_srv long=%.1f", args: [x]}
- platform: entity_transport
id: r_cli_long
entity_transport_id: etc
provider: ${peer}
remote_id: ${long_id}9
on_value:
- logger.log: {format: "ETRX et_cli long=%.1f", args: [x]}
binary_sensor:
- platform: template
id: bin
lambda: return (id(counter) / 3) % 2;
- platform: entity_transport
id: r_srv_bin
entity_transport_id: ets
provider: ${peer}
remote_id: bin
on_state:
- logger.log: {format: "ETRX et_srv bin=%d", args: ["x ? 1 : 0"]}
- platform: entity_transport
id: r_cli_bin
entity_transport_id: etc
provider: ${peer}
remote_id: bin
on_state:
- logger.log: {format: "ETRX et_cli bin=%d", args: ["x ? 1 : 0"]}
entity_transport:
- id: ets
packet_interface: et_srv
sensors: &et_sensors
- cnt
- {id: longa, broadcast_id: "${long_id}1"}
- {id: longa, broadcast_id: "${long_id}2"}
- {id: longa, broadcast_id: "${long_id}3"}
- {id: longa, broadcast_id: "${long_id}4"}
- {id: longa, broadcast_id: "${long_id}5"}
- {id: longa, broadcast_id: "${long_id}6"}
- {id: longa, broadcast_id: "${long_id}7"}
- {id: longa, broadcast_id: "${long_id}8"}
- {id: longa, broadcast_id: "${long_id}9"}
binary_sensors: &et_bins
- bin
- id: etc
packet_interface: et_cli
sensors: *et_sensors
binary_sensors: *et_bins
@@ -0,0 +1,47 @@
"""Writes one host config per tcp link validation rule into out-valid/; expected outcome in the name prefix (ok_/bad_).
Run `esphome config` on each from the tree under test.
"""
import pathlib
OUT = pathlib.Path(__file__).parent / "out-valid"
LINK = " - platform: tcp\n id: l\n"
HOST = " host: 10.1.2.3\n port: 6060\n"
ET = (
"sensor:\n - platform: template\n id: s\n lambda: return 1;\n"
"entity_transport:\n - packet_interface: l\n timeout: 1min\n sensors: [s]\n"
)
def write(name: str, link: str, host: str = HOST, tail: str = "") -> None:
node = name.replace("_", "-")[:31]
text = f"esphome:\n name: {node}\nhost:\nlogger:\npacket_interface:\n{LINK}{host}{link}{tail}"
(OUT / f"{name}.yaml").write_text(text, encoding="utf-8")
OUT.mkdir(exist_ok=True)
write("bad_reliability", " reliability:\n timeout: 200ms\n")
write("bad_session", " session:\n keep_alive: 2s\n")
write("bad_addressing", " addressing:\n address: 1\n destination: 2\n")
write("bad_maxpkt1023", " max_packet_size: 1023\n")
write("ok_maxpkt1022", " max_packet_size: 1022\n")
write("bad_ka500ms", " keep_alive: 500ms\n")
write("ok_ka1s", " keep_alive: 1s\n")
write("bad_ka2h", " keep_alive: 2h\n")
write("ok_ka1h", " keep_alive: 1h\n")
write("bad_to_eq_ka", " keep_alive: 5s\n timeout: 5s\n")
write("ok_to_gt_ka", " keep_alive: 5s\n timeout: 5001ms\n")
write("bad_to4h", " keep_alive: 1h\n timeout: 4h\n")
write("ok_to3h", " keep_alive: 1h\n timeout: 3h\n")
write(
"bad_rolling", " encryption:\n key: abc\n rolling_code_enable: true\n"
)
write(
"ok_enc_frag",
" encryption: abc\n fragmentation:\n max_packet_size: 4096\n",
)
write("bad_client_allowed", " allowed_ips: [10.0.0.0/8]\n")
write("bad_client_nohost", "", host=" port: 6060\n")
write("bad_ipv6host", "", host=" host: fe80::1\n port: 6060\n")
write("bad_et_timeout", "", tail=ET)
+19 -628
View File
@@ -1,9 +1,7 @@
"""Memory usage analyzer for ESPHome compiled binaries."""
from collections import Counter, defaultdict
from collections.abc import Container
from dataclasses import dataclass, field
from functools import cached_property
import logging
from pathlib import Path
import re
@@ -57,7 +55,6 @@ _COMPONENT_PREFIX_EXTERNAL = "[external]"
_COMPONENT_PREFIX_LIB = "[lib]"
_COMPONENT_CORE = f"{_COMPONENT_PREFIX_ESPHOME}core"
_COMPONENT_API = f"{_COMPONENT_PREFIX_ESPHOME}api"
_COMPONENT_ARDUINO_CORE = "arduino_core"
# Placement new storage suffix (generated by codegen Pvariable)
_PSTORAGE_SUFFIX = "__pstorage"
@@ -86,28 +83,6 @@ _NAMESPACE_STD = "std::"
# Type alias for symbol information: (symbol_name, size, component)
SymbolInfoType = tuple[str, int, str]
# Only ESP8266 has this section; its .rodata is copied to RAM at boot like .data
_ESP8266_FLASH_CODE_SECTION = ".irom0.text"
# readelf -SW row: [Nr] Name Type Address Off Size ES Flg
_OBJ_SECTION_PATTERN = re.compile(
r"^\s*\[\s*(\d+)\]\s+(\S+)\s+(\S+)\s+[\da-f]+\s+[\da-f]+\s+([\da-f]+)\s+[\da-f]+\s+(\S*)"
)
_RELOC_SECTION_PATTERN = re.compile(r"^Relocation section '\.rela(\S+)'")
# String literal section; group 1 is the function for -fdata-sections names
_STRING_SECTION_PATTERN = re.compile(r"^\.rodata(?:\.(.+))?\.str\d+\.\d+$")
# Data section of one symbol; group 1 is the symbol
_DATA_SECTION_PATTERN = re.compile(r"^\.(?:rodata|data)\.(.+)$")
# Code section; group 1 is the function for -ffunction-sections names, after any
# placement prefix (.text.startup.<fn>, .iram.text.<fn> for IRAM_ATTR)
_CODE_SECTION_PATTERN = re.compile(
r"^\.(?:literal|text|irom0\.text|iram\.text|iram0\.text)"
r"(?:\.(?:startup|unlikely|hot|exit)(?=\.|$))?(?:\.(.+))?$"
)
# readelf -x row: " 0x00000000 0000803f 0000803f 0000803f 0000803f ...?...?...?...?"
_HEX_DUMP_PREFIX = " 0x"
_HEX_DUMP_COLUMNS = slice(13, 48)
# RAM sections - symbols in these sections consume RAM
RAM_SECTIONS = frozenset([".data", ".bss"])
@@ -144,31 +119,6 @@ class SDKSymbol:
demangled: str = "" # Demangled name (populated after analysis)
@dataclass
class UnnamedConstant:
"""Constant without a symbol in an object's .rodata (RAM on ESP8266)."""
size: int
source_file: str
component: str
functions: list[str] # Functions that reference it, mangled
jump_table: bool # Holds code addresses (a switch table), not a value
data: bytes # Leading bytes, to recognize the value
strings: list[str] = field(default_factory=list) # String literals it points to
# Every function that uses the section, which decides whether the link kept it
users: list[str] = field(default_factory=list)
@dataclass
class StringLiteral:
"""A string literal kept in RAM (.rodata on ESP8266)."""
text: str
size: int # Bytes in RAM; strings shared or merged into a longer one count once
# Component -> functions or objects that use it, mangled
users: dict[str, set[str]] = field(default_factory=lambda: defaultdict(set))
@dataclass
class ComponentMemory:
"""Tracks memory usage for a component."""
@@ -202,201 +152,6 @@ class ComponentMemory:
return self.data_size + self.bss_size
@dataclass
class _RodataSection:
"""An object's .rodata section and the byte ranges its symbols cover."""
name: str
size: int
covered: list[tuple[int, int]] = field(default_factory=list) # (offset, size)
@cached_property
def _named(self) -> frozenset[int]:
return frozenset(
offset
for start, size in self.covered
for offset in range(start, min(start + size, self.size))
)
def is_unnamed(self, offset: int) -> bool:
return 0 <= offset < self.size and offset not in self._named
@property
def unnamed_size(self) -> int:
return self.size - len(self._named)
def _parse_string_sections(output: str) -> dict[str, dict[str, tuple[str, str | None]]]:
"""Find string literal sections in ``readelf -SW`` output.
Returns:
Object path -> section index -> (section name, function it is named after)
"""
sections: dict[str, dict[str, tuple[str, str | None]]] = defaultdict(dict)
file_path = ""
for line in output.splitlines():
if line.startswith("File: "):
file_path = line[6:]
elif (match := _OBJ_SECTION_PATTERN.match(line)) is not None:
index, name, section_type, size_hex, flags = match.groups()
if (
section_type == "PROGBITS"
and "S" in flags
and int(size_hex, 16)
and (string := _STRING_SECTION_PATTERN.match(name))
):
sections[file_path][index] = (name, string.group(1))
return sections
def _ram_string_size(text: bytes, rodata: bytes, literals: Container[bytes]) -> int:
"""Bytes a literal takes in RAM: 0 if dropped or only the tail of another literal."""
needle = text + b"\0"
start = rodata.find(needle)
while start != -1:
head = rodata.rfind(b"\0", 0, start) + 1
end = start + len(text)
# Only the tail of a longer literal of ours if one starts before it; other
# bytes there are data, or a literal from an object that was not scanned
if not any(rodata[k:end] in literals for k in range(head, start)):
return len(needle)
start = rodata.find(needle, start + 1)
return 0
def _parse_hex_dumps(output: str) -> dict[str, bytearray]:
"""Collect the bytes of each ``readelf -x`` section dump by section name."""
contents: dict[str, bytearray] = defaultdict(bytearray)
section: str | None = None
for line in output.splitlines():
if line.startswith("Hex dump of section"):
section = line.split("'")[1]
elif section is not None and line.startswith(_HEX_DUMP_PREFIX):
contents[section] += bytes.fromhex(line[_HEX_DUMP_COLUMNS].replace(" ", ""))
elif line and not line.startswith(" "):
section = None
return contents
def _parse_relocations(output: str) -> list[tuple[str, int, str, int]]:
"""List (relocated section, offset, target, addend) from ``readelf -rW`` output."""
relocations: list[tuple[str, int, str, int]] = []
section: str | None = None
for line in output.splitlines():
if line.startswith("Relocation section"):
match = _RELOC_SECTION_PATTERN.match(line)
section = match.group(1) if match else None
elif line[:1].isdigit() and section is not None:
# Offset Info Type Sym.Value Sym.Name + Addend
fields = line.split()
if len(fields) < 5:
continue
addend = int(fields[6], 16) if len(fields) > 6 else 0
if len(fields) > 6 and fields[5] == "-":
addend = -addend
relocations.append((section, int(fields[0], 16), fields[4], addend))
elif line and line[0] != " ":
section = None
return relocations
def _pointer_target(referrer: bytes, offset: int, addend: int) -> int | None:
"""Offset a relocated pointer at ``offset`` points to, or None if not dumped."""
if offset < 0 or offset + 4 > len(referrer):
return None
# Xtensa keeps part of the addend in the relocated word
return addend + int.from_bytes(referrer[offset : offset + 4], "little")
def _pointed_literal(
referrer: bytes, offset: int, addend: int, strings: bytes
) -> bytes | None:
"""Read the string literal a relocated pointer refers to, or None if out of range."""
start = _pointer_target(referrer, offset, addend)
if start is None or not 0 <= start < len(strings):
return None
end = strings.find(b"\0", start)
return bytes(strings[start : end if end != -1 else len(strings)])
def _section_user(section: str) -> str | None:
"""The function or data symbol a -ffunction-sections/-fdata-sections name is for."""
match = _CODE_SECTION_PATTERN.match(section) or _DATA_SECTION_PATTERN.match(section)
return match.group(1) if match else None
@dataclass
class _FunctionDefinitions:
"""Where each function is defined across the scanned objects."""
weak: dict[str, set[str]] = field(default_factory=lambda: defaultdict(set))
strong: set[str] = field(default_factory=set) # Global, non-weak
count: Counter[str] = field(default_factory=Counter) # Objects defining it
def linked_copy_unknown(self, file_path: str, functions: list[str]) -> bool:
"""True if another object's copy of these functions may be the linked one."""
if not functions:
return False
if self.weak[file_path].issuperset(functions):
# Inline copies: dropped only once a strong definition replaces every user
return self.strong.issuperset(functions)
# Same-named local functions in several objects; the ELF can't tell them apart
return all(f not in self.strong and self.count[f] > 1 for f in functions)
def _parse_unnamed_rodata(
output: str,
) -> tuple[dict[str, dict[str, _RodataSection]], _FunctionDefinitions]:
"""Find .rodata sections with bytes no symbol covers in ``readelf -SW -sW`` output.
Returns:
Object path -> section index -> section, and where functions are defined
"""
sections: dict[str, dict[str, _RodataSection]] = defaultdict(dict)
definitions = _FunctionDefinitions()
file_path = ""
for line in output.splitlines():
if line.startswith("File: "):
file_path = line[6:]
elif match := _OBJ_SECTION_PATTERN.match(line):
index, name, section_type, size_hex, flags = match.groups()
# Merged sections (string literals) are shared between objects
if (
section_type == "PROGBITS"
and name.startswith(".rodata")
and "A" in flags
and "M" not in flags
):
sections[file_path][index] = _RodataSection(name, int(size_hex, 16))
else:
# Num: Value Size Type Bind Vis Ndx Name
fields = line.split()
if len(fields) < 8:
continue
if fields[3] == "OBJECT" and (
section := sections[file_path].get(fields[6])
):
section.covered.append((int(fields[1], 16), int(fields[2], 0)))
elif fields[3] == "FUNC" and fields[6] != "UND":
definitions.count[fields[7]] += 1
if fields[4] == "WEAK":
definitions.weak[file_path].add(fields[7])
elif fields[4] == "GLOBAL":
definitions.strong.add(fields[7])
unnamed = {
path: kept
for path, file_sections in sections.items()
if (
kept := {
index: section
for index, section in file_sections.items()
if section.unnamed_size
}
)
}
return unnamed, definitions
class MemoryAnalyzer:
"""Analyzes memory usage from ELF files."""
@@ -476,10 +231,6 @@ class MemoryAnalyzer:
self._heuristic_to_lib: dict[str, str] = {}
# Function call counts: mangled_name -> call_count
self._function_call_counts: Counter[str] = Counter()
# True on ESP8266, where .rodata is RAM; it is then counted as .data
self.rodata_in_ram = False
self._unnamed_constants: list[UnnamedConstant] = []
self._string_literals: list[StringLiteral] = []
def analyze(self) -> dict[str, ComponentMemory]:
"""Analyze the ELF file and return component memory usage."""
@@ -489,7 +240,6 @@ class MemoryAnalyzer:
self._scan_source_symbols()
self._categorize_symbols()
self._analyze_cswtch_symbols()
self._analyze_object_rodata()
self._analyze_sdk_libraries()
self._analyze_function_calls()
return dict(self.components)
@@ -503,17 +253,18 @@ class MemoryAnalyzer:
check=True,
)
raw_sections = [
(match.group(1), int(match.group(2), 16))
for line in result.stdout.splitlines()
if (match := _READELF_SECTION_PATTERN.match(line))
]
self.rodata_in_ram = any(
name == _ESP8266_FLASH_CODE_SECTION for name, _ in raw_sections
)
# Parse section headers
for line in result.stdout.splitlines():
# Look for section entries
if not (match := _READELF_SECTION_PATTERN.match(line)):
continue
for section_name, size in raw_sections:
mapped_section = self._standard_section(map_section_name(section_name))
section_name = match.group(1)
size_hex = match.group(2)
size = int(size_hex, 16)
# Map to standard section name
mapped_section = map_section_name(section_name)
if not mapped_section:
continue
@@ -521,12 +272,6 @@ class MemoryAnalyzer:
self.sections[mapped_section] = MemorySection(mapped_section)
self.sections[mapped_section].total_size += size
def _standard_section(self, section: str | None) -> str | None:
"""Count .rodata as .data where it is RAM (ESP8266)."""
if section == ".rodata" and self.rodata_in_ram:
return ".data"
return section
def _parse_symbols(self) -> None:
"""Parse symbols from ELF file."""
result = subprocess.run(
@@ -544,18 +289,14 @@ class MemoryAnalyzer:
continue
section, name, size, address = symbol_info
section = self._standard_section(section)
if section not in self.sections:
continue
# Every alias is in the ELF, even when its size is counted once
self._elf_symbol_names.add(name)
# Skip duplicate symbols at the same address (e.g., C1/C2 constructors)
if address in seen_addresses:
if address in seen_addresses or section not in self.sections:
continue
self.sections[section].symbols.append((name, size, ""))
self.sections[section].symbol_size += size
self._elf_symbol_names.add(name)
seen_addresses.add(address)
def _categorize_symbols(self) -> None:
@@ -1184,12 +925,10 @@ class MemoryAnalyzer:
Path to the directory containing .o files, or None if not found.
"""
# The ELF is typically at .pioenvs/<env>/firmware.elf
# PlatformIO puts object files in .pioenvs/<env>/src/ and .pioenvs/<env>/lib*/,
# the native ESP8266 build in .pioenvs/<env>/obj/src/ and obj/lib/
# Object files are in .pioenvs/<env>/src/ and .pioenvs/<env>/lib*/
pioenvs_dir = self.elf_path.parent
for obj_dir in (pioenvs_dir, pioenvs_dir / "obj"):
if any(obj_dir.glob("src/*.o")):
return obj_dir
if pioenvs_dir.exists() and any(pioenvs_dir.glob("src/*.o")):
return pioenvs_dir
return None
@staticmethod
@@ -1371,12 +1110,6 @@ class MemoryAnalyzer:
):
return f"{_COMPONENT_PREFIX_LIB}{self._lib_hash_to_name[parts[1]]}"
# Native ESP8266 build: lib/<owner>__<Name>/... and the Arduino core in core/
if len(parts) >= 3 and parts[0] == "lib":
return f"{_COMPONENT_PREFIX_LIB}{parts[1].rsplit('__', 1)[-1].lower()}"
if len(parts) >= 2 and parts[0] == "core":
return _COMPONENT_ARDUINO_CORE
# Other framework/library files - return the first path component
# e.g., FrameworkArduino/... -> FrameworkArduino
return parts[0] if parts else source_file
@@ -1458,7 +1191,9 @@ class MemoryAnalyzer:
and other_mem is not None
):
other_mem.add_section_size(section_name, -size)
self._add_component_size(component, section_name, size)
if component not in self.components:
self.components[component] = ComponentMemory(component)
self.components[component].add_section_size(section_name, size)
# Sort by size descending
self._cswtch_symbols.sort(key=lambda x: x[1], reverse=True)
@@ -1470,350 +1205,6 @@ class MemoryAnalyzer:
total_size,
)
def _analyze_object_rodata(self) -> None:
"""Attribute .rodata without a symbol, which is RAM on ESP8266, to components."""
if not self.rodata_in_ram:
return
if (obj_dir := self._find_object_files_dir()) is None:
_LOGGER.warning(
"No object files next to the ELF, so constants in RAM are not listed"
)
return
objects = [str(f) for f in sorted(obj_dir.rglob("*.o"))]
result = run_tool([self.readelf_path, "-SW", "-sW", *objects])
if result is None:
_LOGGER.warning("readelf failed, so constants in RAM are not listed")
return
if result.returncode != 0:
# readelf still prints every object it could read
_LOGGER.warning(
"readelf could not read every object: %s", result.stderr.strip()
)
output = result.stdout
# readelf names each file only when given several
if len(objects) == 1:
output = f"File: {objects[0]}\n{output}"
self._attribute_unnamed_constants(output, obj_dir)
self._attribute_string_literals(output, obj_dir)
names = {n for c in self._unnamed_constants for n in c.functions} | {
n
for literal in self._string_literals
for users in literal.users.values()
for n in users
}
self._batch_demangle_symbols(sorted(names - self._demangle_cache.keys()))
def _attribute_unnamed_constants(self, output: str, obj_dir: Path) -> None:
"""Attribute constants GCC copies into objects built from constant initializers."""
unnamed, definitions = _parse_unnamed_rodata(output)
# Inline (weak) functions are emitted, with their constants, in every
# object that uses them; the linker keeps one copy
inline_copies: dict[tuple[tuple[str, ...], int], str] = {}
for file_path, sections in unnamed.items():
rel_path = Path(file_path).relative_to(obj_dir).as_posix()
component = self._source_file_to_component(rel_path)
for constant in self._scan_object_constants(
file_path, sections, rel_path, component
):
# Only linked users can show which object's copy survived
live = [u for u in constant.users if u in self._elf_symbol_names]
if definitions.linked_copy_unknown(file_path, live):
continue
if definitions.weak[file_path].issuperset(live):
key = (tuple(live), constant.size)
if inline_copies.setdefault(key, file_path) != file_path:
continue
self._unnamed_constants.append(constant)
self._charge_ram(component, constant.size)
self._unnamed_constants.sort(key=lambda c: c.size, reverse=True)
def _attribute_string_literals(self, output: str, obj_dir: Path) -> None:
"""Attribute string literals in RAM to the components whose code uses them.
The linker merges identical literals across objects, so each is counted
once, from the bytes it really takes in the ELF's .rodata.
"""
result = run_tool([self.readelf_path, "-x", ".rodata", str(self.elf_path)])
rodata = _parse_hex_dumps(result.stdout).get(".rodata") if result else None
if not rodata:
_LOGGER.warning(
"No .rodata dump of %s, so string literals are not listed",
self.elf_path,
)
return
rodata = bytes(rodata)
_, definitions = _parse_unnamed_rodata(output)
# Literal -> component -> functions or data using it
users: dict[bytes, dict[str, set[str]]] = defaultdict(lambda: defaultdict(set))
for file_path, sections in _parse_string_sections(output).items():
component = self._source_file_to_component(
Path(file_path).relative_to(obj_dir).as_posix()
)
for text, found in self._scan_object_strings(file_path, sections).items():
kept = {
user
for user in found & self._elf_symbol_names
if not definitions.linked_copy_unknown(file_path, [user])
}
if text and kept:
users[text][component] |= kept
literals = (
StringLiteral(
text.decode("utf-8", "backslashreplace"),
_ram_string_size(text, rodata, users),
by_component,
)
for text, by_component in users.items()
)
self._string_literals = sorted(
(s for s in literals if s.size), key=lambda s: (-s.size, s.text)
)
for literal in self._string_literals:
# A literal used by several components is not charged to any of them
if len(literal.users) == 1:
self._charge_ram(next(iter(literal.users)), literal.size)
def _scan_object_strings(
self, file_path: str, sections: dict[str, tuple[str, str | None]]
) -> dict[bytes, set[str]]:
"""Map each string literal of one object to the functions or data using it.
Args:
file_path: Object file to read
sections: String sections by index, with the function each is named after
"""
result = run_tool(
[self.readelf_path, "-rW"]
+ [arg for index in sections for arg in ("-x", index)]
+ [file_path]
)
if result is None or result.returncode != 0:
_LOGGER.warning(
"readelf failed reading strings of %s: %s",
file_path,
result.stderr.strip() if result else "not run",
)
return {}
contents = _parse_hex_dumps(result.stdout)
users: dict[bytes, set[str]] = defaultdict(set)
# Section -> the function it is named after, None for the shared section
owners = dict(sections.values())
for name, function in owners.items():
# A section named after a function holds its literals; others may use them too
if function:
for text in bytes(contents[name]).split(b"\0")[:-1]:
users[text].add(function)
relocations = _parse_relocations(result.stdout)
# Functions using each unnamed table, for literals reached through a pointer in it
table_users: dict[str, set[str]] = defaultdict(set)
for section, _, target, _ in relocations:
if (
target.startswith(".rodata")
and not _STRING_SECTION_PATTERN.match(target)
and (function := _section_user(section))
and _CODE_SECTION_PATTERN.match(section)
):
table_users[target].add(function)
# Other users are found from the pointers they hold
refs = [
(section, found, offset, target, addend)
for section, offset, target, addend in relocations
if target in owners
and (
found := {u} if (u := _section_user(section)) else table_users[section]
)
and found != {owners[target]}
]
unnamed = sum(
1
for section, _, target, _ in relocations
if target in owners
and owners[target] is None
and not _section_user(section)
and not table_users[section]
)
if unnamed:
_LOGGER.debug(
"%s: %d string pointers with no named user", file_path, unnamed
)
if not refs:
return users
result = run_tool(
[self.readelf_path]
+ [arg for name in sorted({r[0] for r in refs}) for arg in ("-x", name)]
+ [file_path]
)
if result is None or result.returncode != 0:
_LOGGER.warning(
"readelf failed reading string users of %s: %s",
file_path,
result.stderr.strip() if result else "not run",
)
return users
referrers = _parse_hex_dumps(result.stdout)
for section, found, offset, target, addend in refs:
# A pointer that can't be read names no literal
text = _pointed_literal(
referrers.get(section, b""), offset, addend, contents[target]
)
if text is not None:
users[text] |= found
return users
def _scan_object_constants(
self,
file_path: str,
sections: dict[str, _RodataSection],
rel_path: str,
component: str,
) -> list[UnnamedConstant]:
"""Return the unnamed constants of one object that the link kept.
Args:
file_path: Object file to read
sections: Candidate sections by index
rel_path: Object path shown in the report
component: Component the object belongs to
"""
result = run_tool(
[self.readelf_path, "-rW"]
+ [arg for index in sections for arg in ("-x", index)]
+ [file_path]
)
if result is None or result.returncode != 0:
_LOGGER.warning(
"readelf failed reading constants of %s: %s",
file_path,
result.stderr.strip() if result else "not run",
)
return []
by_name = {section.name: section for section in sections.values()}
contents = _parse_hex_dumps(result.stdout)
# Every function using the section decides whether the link kept it
users: dict[str, set[str]] = defaultdict(set)
# Functions shown as using the unnamed bytes
functions: dict[str, set[str]] = defaultdict(set)
# (code section, offset, addend, target, function) into sections with named bytes
mixed: list[tuple[str, int, int, str, str]] = []
jump_tables: set[str] = set()
# Section -> (offset, string section, addend) of each pointer to a literal
pointers: dict[str, list[tuple[int, str, int]]] = defaultdict(list)
for section, offset, target, addend in _parse_relocations(result.stdout):
if target in by_name:
code = _CODE_SECTION_PATTERN.match(section)
if code and code.group(1):
users[target].add(code.group(1))
if by_name[target].covered:
mixed.append((section, offset, addend, target, code.group(1)))
else:
functions[target].add(code.group(1))
elif section in by_name and by_name[section].is_unnamed(offset):
if _CODE_SECTION_PATTERN.match(target):
jump_tables.add(section)
elif ".str" in target:
pointers[section].append((offset, target, addend))
self._add_mixed_section_users(file_path, mixed, by_name, functions)
strings = self._read_pointed_strings(file_path, pointers, contents)
return [
UnnamedConstant(
size=section.unnamed_size,
source_file=rel_path,
component=component,
functions=sorted(functions[name]),
jump_table=name in jump_tables,
data=bytes(
byte
for offset, byte in enumerate(contents[name])
if section.is_unnamed(offset)
)[:16],
strings=strings.get(name, []),
users=sorted(users[name]),
)
for name, section in by_name.items()
# Dropped by --gc-sections unless a function that uses it was kept
if not self._elf_symbol_names.isdisjoint(users[name])
]
def _add_mixed_section_users(
self,
file_path: str,
mixed: list[tuple[str, int, int, str, str]],
by_name: dict[str, _RodataSection],
functions: dict[str, set[str]],
) -> None:
"""Name only the functions that point at the unnamed bytes of a mixed section."""
if not mixed:
return
result = run_tool(
[self.readelf_path]
+ [arg for name in sorted({m[0] for m in mixed}) for arg in ("-x", name)]
+ [file_path]
)
code = _parse_hex_dumps(result.stdout) if result else {}
for section, offset, addend, target, function in mixed:
start = _pointer_target(code.get(section, b""), offset, addend)
# A pointer that can't be read, or lands outside the section, keeps the user
section_size = by_name[target].size
if (
start is None
or not 0 <= start < section_size
or by_name[target].is_unnamed(start)
):
functions[target].add(function)
def _read_pointed_strings(
self,
file_path: str,
pointers: dict[str, list[tuple[int, str, int]]],
contents: dict[str, bytearray],
) -> dict[str, list[str]]:
"""Resolve each constant's pointers to string literals, in offset order."""
if not pointers:
return {}
targets = sorted({t for refs in pointers.values() for _, t, _ in refs})
result = run_tool(
[self.readelf_path]
+ [arg for target in targets for arg in ("-x", target)]
+ [file_path]
)
if result is None or result.returncode != 0:
_LOGGER.warning(
"readelf failed reading strings of %s: %s",
file_path,
result.stderr.strip() if result else "not run",
)
return {}
literals = _parse_hex_dumps(result.stdout)
resolved: dict[str, list[str]] = {}
for name, refs in pointers.items():
texts = [
_pointed_literal(
contents[name], offset, addend, literals.get(target, b"")
)
for offset, target, addend in sorted(refs)
]
# One pointer that can't be read makes the list misleading; show bytes instead
if all(text is not None for text in texts):
resolved[name] = [t.decode("utf-8", "backslashreplace") for t in texts]
return resolved
def _charge_ram(self, component: str, size: int) -> None:
"""Charge RAM bytes that have no ELF symbol to a component."""
self._add_component_size(component, ".data", size)
if (data_section := self.sections.get(".data")) is not None:
data_section.symbol_size += size
def _add_component_size(self, component: str, section: str, size: int) -> None:
"""Add size to a component, creating it under its own name."""
if component not in self.components:
self.components[component] = ComponentMemory(component)
self.components[component].add_section_size(section, size)
def _analyze_function_calls(self) -> None:
"""Count function call sites by parsing disassembly output.
+2 -88
View File
@@ -5,7 +5,6 @@ from __future__ import annotations
from collections import defaultdict
from collections.abc import Callable
import heapq
import json
from operator import itemgetter
from pathlib import Path
import sys
@@ -24,7 +23,7 @@ from . import (
from .toolchain import find_elf_path, find_idedata_path, idedata_candidates
if TYPE_CHECKING:
from . import ComponentMemory, StringLiteral
from . import ComponentMemory
def _format_pstorage_name(name: str) -> str:
@@ -47,8 +46,6 @@ class MemoryAnalyzerCLI(MemoryAnalyzer):
)
# Lower threshold for RAM symbols (RAM is more constrained)
RAM_SYMBOL_SIZE_THRESHOLD: int = 24
# String literals listed per component in the RAM strings report
STRINGS_PER_COMPONENT: int = 10
# Number of top symbols to show in the largest symbols report
TOP_SYMBOLS_LIMIT: int = 30
# Width for symbol name display in top symbols report
@@ -207,79 +204,6 @@ class MemoryAnalyzerCLI(MemoryAnalyzer):
f"{i + 1:>2}. {size:>7,} B {section_label:<8} {demangled_display:<{self.COL_TOP_SYMBOL_NAME}} {component}"
)
def _add_unnamed_constants_analysis(self, lines: list[str]) -> None:
"""Add the unnamed constants (ESP8266 .rodata in RAM) section."""
self._add_section_header(lines, "Unnamed Constants in RAM (.rodata)")
total_size = sum(c.size for c in self._unnamed_constants)
lines.append(
f"Total: {len(self._unnamed_constants)} constant(s), {total_size:,} B. "
"GCC copies these into objects built from constant initializers;"
)
lines.append(
"PROGMEM tables, assigned fields or a user-provided constructor avoid the copy."
)
lines.append("")
for constant in self._unnamed_constants:
kind = "switch table" if constant.jump_table else "constant"
lines.append(
f"{constant.size:>6,} B {kind:<12} {constant.component} ({constant.source_file})"
)
lines.extend(
f"{'':>9} used by {self._demangle_symbol(function)}"
for function in constant.functions
)
if constant.strings:
# Escaped, so control characters in a literal can't disturb the terminal
shown = ", ".join(
json.dumps(text[:40]) for text in constant.strings[:4]
)
more = ", ..." if len(constant.strings) > 4 else ""
lines.append(f"{'':>9} points to {shown}{more}")
elif constant.data and not constant.jump_table:
more = " ..." if constant.size > len(constant.data) else ""
lines.append(f"{'':>9} bytes: {constant.data.hex(' ')}{more}")
lines.append("")
def _add_string_literals_analysis(self, lines: list[str]) -> None:
"""Add the string literals in RAM (ESP8266 .rodata) section, by component."""
self._add_section_header(lines, "String Literals in RAM (.rodata)")
total_size = sum(s.size for s in self._string_literals)
lines.append(
f"Total: {len(self._string_literals)} string(s), {total_size:,} B. "
"LOG_STR, ESPHOME_F or a PROGMEM table keeps a literal in flash."
)
lines.append(
"Only literals whose user is known are counted; one used by several components is "
"listed under each and charged to none."
)
lines.append("")
by_component: dict[str, list[StringLiteral]] = defaultdict(list)
for literal in self._string_literals:
for component in literal.users:
by_component[component].append(literal)
totals = {c: sum(s.size for s in ls) for c, ls in by_component.items()}
for component, literals in sorted(
by_component.items(), key=lambda x: (-totals[x[0]], x[0])
):
comp_total = totals[component]
lines.append(f"{component} ({comp_total:,} B, {len(literals)} strings):")
for literal in literals[: self.STRINGS_PER_COMPONENT]:
text = json.dumps(literal.text)
if len(text) > 50:
text = f'{text[:47]}..."'
users = sorted(
self._demangle_symbol(u) for u in literal.users[component]
)
used_by = users[0][:60] + (
f" (+{len(users) - 1})" if len(users) > 1 else ""
)
lines.append(f" {literal.size:>5,} B {text:<50} {used_by}")
if len(literals) > self.STRINGS_PER_COMPONENT:
lines.append(
f" ... and {len(literals) - self.STRINGS_PER_COMPONENT} more"
)
lines.append("")
def _add_cswtch_analysis(self, lines: list[str]) -> None:
"""Add CSWTCH (GCC switch table lookup) analysis section."""
self._add_section_header(lines, "CSWTCH Analysis (GCC Switch Table Lookups)")
@@ -470,12 +394,6 @@ class MemoryAnalyzerCLI(MemoryAnalyzer):
f"{total_flash:>{self.COL_TOTAL_FLASH - 2},} B | {total_ram:>{self.COL_TOTAL_RAM - 2},} B"
)
if self.rodata_in_ram:
lines.append("")
lines.append(
"ESP8266 keeps .rodata in RAM, so constants are counted in RAM (data)."
)
# Show unattributed RAM (SDK/framework overhead)
unattributed_bss, unattributed_data, unattributed_total = (
self.get_unattributed_ram()
@@ -739,11 +657,6 @@ class MemoryAnalyzerCLI(MemoryAnalyzer):
lines.append(f" ... and {len(large_ram_syms) - 10} more")
lines.append("")
if self._unnamed_constants:
self._add_unnamed_constants_analysis(lines)
if self._string_literals:
self._add_string_literals_analysis(lines)
# CSWTCH (GCC switch table) analysis
if self._cswtch_symbols:
self._add_cswtch_analysis(lines)
@@ -825,6 +738,7 @@ def main():
build_dir = sys.argv[1]
# Load build directory
import json
from esphome.platformio.toolchain import IDEData
+2 -2
View File
@@ -96,7 +96,7 @@ void AHT10Component::read_data_() {
this->restart_read_();
return;
}
if (data[1] == 0x0 && data[2] == 0x0 && high_nibble(data[3]) == 0x0) {
if (data[1] == 0x0 && data[2] == 0x0 && (data[3] >> 4) == 0x0) {
// Invalid humidity (0x0)
if (this->humidity_sensor_ == nullptr) {
ESP_LOGV(TAG, "Invalid humidity (reading not required)");
@@ -110,7 +110,7 @@ void AHT10Component::read_data_() {
if (this->read_count_ > 1) {
ESP_LOGD(TAG, "Success at %ums", (unsigned) (millis() - this->start_time_));
}
uint32_t raw_temperature = encode_uint24(low_nibble(data[3]), data[4], data[5]);
uint32_t raw_temperature = encode_uint24(data[3] & 0xF, data[4], data[5]);
uint32_t raw_humidity = encode_uint24(data[1], data[2], data[3]) >> 4;
PUBLISH_STATE(this->temperature_sensor_, ((200.0f * static_cast<float>(raw_temperature)) / AHT10_DIVISOR) - 50.0f);
+2 -2
View File
@@ -62,9 +62,9 @@ bool AM2315C::reset_register_(uint8_t reg) {
bool AM2315C::convert_(uint8_t *data, float &humidity, float &temperature) {
uint32_t raw;
raw = encode_uint24(data[1], data[2], data[3]) >> 4;
raw = (data[1] << 12) | (data[2] << 4) | (data[3] >> 4);
humidity = raw * 9.5367431640625e-5;
raw = encode_uint24(low_nibble(data[3]), data[4], data[5]);
raw = encode_uint24(data[3] & 0x0F, data[4], data[5]);
temperature = raw * 1.9073486328125e-4 - 50;
return crc8(data, 6, 0xFF, 0x31, true) == data[6];
}
-8
View File
@@ -70,10 +70,6 @@ void APIServer::setup() {
#if defined(USE_API_HOMEASSISTANT_STATES) && defined(HOMEASSISTANT_STATE_SUBSCRIPTION_COUNT)
this->state_subs_.reserve(HOMEASSISTANT_STATE_SUBSCRIPTION_COUNT);
#endif
#ifdef USE_API_HOMEASSISTANT_IMPORTS
if (this->homeassistant_subscribe_publisher_ != nullptr)
this->homeassistant_subscribe_publisher_();
#endif
#ifdef USE_API_NOISE
// Always reserve the slot: flash preferences are positional on esp8266, so
// a yaml key build must keep the layout of a runtime key build
@@ -393,10 +389,6 @@ void APIServer::dump_config() {
#ifdef USE_API_OUTGOING_CONNECTION
this->outgoing_conn_.dump_config();
#endif
#ifdef USE_API_HOMEASSISTANT_IMPORTS
if (this->homeassistant_dump_publisher_ != nullptr)
this->homeassistant_dump_publisher_();
#endif
}
void APIServer::handle_disconnect(APIConnection *conn) {}
-5
View File
@@ -272,11 +272,6 @@ class APIServer final : public Component
/// Measure again the subscriptions whose entity id is this buffer, after the wizard wrote a new id into it.
void update_state_subscription_entity_id(const char *entity_id);
#endif
#ifdef USE_API_HOMEASSISTANT_IMPORTS
// Generated: subscribes the imported Home Assistant entities, and prints their dumps
HUB_PUBLISHER(homeassistant_subscribe)
HUB_PUBLISHER(homeassistant_dump)
#endif
#endif
#ifdef USE_API_USER_DEFINED_ACTIONS
const std::vector<UserServiceDescriptor *> &get_user_services() const { return this->user_services_; }
+1 -1
View File
@@ -24,7 +24,7 @@ enum class AranetDeviceType : uint8_t {
// https://github.com/Anrijs/Aranet4-Python/blob/master/aranet4/client.py
// https://github.com/Anrijs/Aranet4-ESP32/blob/main/src/Aranet4.h
void Aranet::dump_child_config() {
void Aranet::dump_config() {
ESP_LOGCONFIG(TAG, "Aranet");
LOG_SENSOR(" ", "Carbon Dioxide", this->co2_sensor_);
LOG_SENSOR(" ", "Temperature", this->temperature_sensor_);
+3 -2
View File
@@ -2,15 +2,16 @@
#include "esphome/components/ble_device_base/ble_device.h"
#include "esphome/components/sensor/sensor.h"
#include "esphome/core/component.h"
namespace esphome::aranet {
class Aranet final : public ble_device_base::ESPBTDeviceListener {
class Aranet final : public Component, public ble_device_base::ESPBTDeviceListener {
public:
explicit Aranet(uint64_t address) : address_(address) {}
bool parse_device(const ble_device_base::ESPBTDevice &device) override;
void dump_child_config() override;
void dump_config() override;
protected:
static constexpr uint8_t WARNED_INVALID_LENGTH = 1 << 0;
+78 -72
View File
@@ -43,7 +43,7 @@ CONF_RADIATION_DURATION = "radiation_duration"
UNIT_NANOSIEVERT = "nSv"
aranet_ns = cg.esphome_ns.namespace("aranet")
Aranet = aranet_ns.class_("Aranet", ble_device_base.ESPBTDeviceListener)
Aranet = aranet_ns.class_("Aranet", ble_device_base.ESPBTDeviceListener, cg.Component)
DIAGNOSTIC_DURATION_SCHEMA = sensor.sensor_schema(
unit_of_measurement=UNIT_SECOND,
@@ -53,80 +53,86 @@ DIAGNOSTIC_DURATION_SCHEMA = sensor.sensor_schema(
entity_category=ENTITY_CATEGORY_DIAGNOSTIC,
)
CONFIG_SCHEMA = cv.Schema(
{
cv.GenerateID(): cv.declare_id(Aranet),
cv.Required(CONF_MAC_ADDRESS): cv.mac_address,
cv.Optional(CONF_CO2): sensor.sensor_schema(
unit_of_measurement=UNIT_PARTS_PER_MILLION,
accuracy_decimals=0,
device_class=DEVICE_CLASS_CARBON_DIOXIDE,
state_class=STATE_CLASS_MEASUREMENT,
),
cv.Optional(CONF_TEMPERATURE): sensor.sensor_schema(
unit_of_measurement=UNIT_CELSIUS,
accuracy_decimals=1,
device_class=DEVICE_CLASS_TEMPERATURE,
state_class=STATE_CLASS_MEASUREMENT,
),
cv.Optional(CONF_HUMIDITY): sensor.sensor_schema(
unit_of_measurement=UNIT_PERCENT,
accuracy_decimals=1,
device_class=DEVICE_CLASS_HUMIDITY,
state_class=STATE_CLASS_MEASUREMENT,
),
cv.Optional(CONF_PRESSURE): sensor.sensor_schema(
unit_of_measurement=UNIT_HECTOPASCAL,
accuracy_decimals=1,
device_class=DEVICE_CLASS_PRESSURE,
state_class=STATE_CLASS_MEASUREMENT,
),
cv.Optional(CONF_BATTERY_LEVEL): sensor.sensor_schema(
unit_of_measurement=UNIT_PERCENT,
accuracy_decimals=0,
device_class=DEVICE_CLASS_BATTERY,
state_class=STATE_CLASS_MEASUREMENT,
entity_category=ENTITY_CATEGORY_DIAGNOSTIC,
),
cv.Optional(CONF_MEASUREMENT_INTERVAL): DIAGNOSTIC_DURATION_SCHEMA,
cv.Optional(CONF_MEASUREMENT_AGE): DIAGNOSTIC_DURATION_SCHEMA,
cv.Optional(CONF_SIGNAL_STRENGTH): sensor.sensor_schema(
unit_of_measurement=UNIT_DECIBEL_MILLIWATT,
accuracy_decimals=0,
device_class=DEVICE_CLASS_SIGNAL_STRENGTH,
state_class=STATE_CLASS_MEASUREMENT,
entity_category=ENTITY_CATEGORY_DIAGNOSTIC,
),
cv.Optional(CONF_RADON): sensor.sensor_schema(
unit_of_measurement=UNIT_BECQUEREL_PER_CUBIC_METER,
accuracy_decimals=0,
device_class=DEVICE_CLASS_RADON,
state_class=STATE_CLASS_MEASUREMENT,
),
cv.Optional(CONF_RADIATION_RATE): sensor.sensor_schema(
unit_of_measurement=UNIT_MICROSILVERTS_PER_HOUR,
accuracy_decimals=3,
icon=ICON_RADIOACTIVE,
state_class=STATE_CLASS_MEASUREMENT,
),
cv.Optional(CONF_RADIATION_TOTAL): sensor.sensor_schema(
unit_of_measurement=UNIT_NANOSIEVERT,
accuracy_decimals=0,
icon=ICON_RADIOACTIVE,
state_class=STATE_CLASS_TOTAL_INCREASING,
),
cv.Optional(CONF_RADIATION_DURATION): sensor.sensor_schema(
unit_of_measurement=UNIT_SECOND,
accuracy_decimals=0,
device_class=DEVICE_CLASS_DURATION,
state_class=STATE_CLASS_TOTAL_INCREASING,
),
}
).extend(ble_device_base.BLE_DEVICE_SCHEMA)
CONFIG_SCHEMA = (
cv.Schema(
{
cv.GenerateID(): cv.declare_id(Aranet),
cv.Required(CONF_MAC_ADDRESS): cv.mac_address,
cv.Optional(CONF_CO2): sensor.sensor_schema(
unit_of_measurement=UNIT_PARTS_PER_MILLION,
accuracy_decimals=0,
device_class=DEVICE_CLASS_CARBON_DIOXIDE,
state_class=STATE_CLASS_MEASUREMENT,
),
cv.Optional(CONF_TEMPERATURE): sensor.sensor_schema(
unit_of_measurement=UNIT_CELSIUS,
accuracy_decimals=1,
device_class=DEVICE_CLASS_TEMPERATURE,
state_class=STATE_CLASS_MEASUREMENT,
),
cv.Optional(CONF_HUMIDITY): sensor.sensor_schema(
unit_of_measurement=UNIT_PERCENT,
accuracy_decimals=1,
device_class=DEVICE_CLASS_HUMIDITY,
state_class=STATE_CLASS_MEASUREMENT,
),
cv.Optional(CONF_PRESSURE): sensor.sensor_schema(
unit_of_measurement=UNIT_HECTOPASCAL,
accuracy_decimals=1,
device_class=DEVICE_CLASS_PRESSURE,
state_class=STATE_CLASS_MEASUREMENT,
),
cv.Optional(CONF_BATTERY_LEVEL): sensor.sensor_schema(
unit_of_measurement=UNIT_PERCENT,
accuracy_decimals=0,
device_class=DEVICE_CLASS_BATTERY,
state_class=STATE_CLASS_MEASUREMENT,
entity_category=ENTITY_CATEGORY_DIAGNOSTIC,
),
cv.Optional(CONF_MEASUREMENT_INTERVAL): DIAGNOSTIC_DURATION_SCHEMA,
cv.Optional(CONF_MEASUREMENT_AGE): DIAGNOSTIC_DURATION_SCHEMA,
cv.Optional(CONF_SIGNAL_STRENGTH): sensor.sensor_schema(
unit_of_measurement=UNIT_DECIBEL_MILLIWATT,
accuracy_decimals=0,
device_class=DEVICE_CLASS_SIGNAL_STRENGTH,
state_class=STATE_CLASS_MEASUREMENT,
entity_category=ENTITY_CATEGORY_DIAGNOSTIC,
),
cv.Optional(CONF_RADON): sensor.sensor_schema(
unit_of_measurement=UNIT_BECQUEREL_PER_CUBIC_METER,
accuracy_decimals=0,
device_class=DEVICE_CLASS_RADON,
state_class=STATE_CLASS_MEASUREMENT,
),
cv.Optional(CONF_RADIATION_RATE): sensor.sensor_schema(
unit_of_measurement=UNIT_MICROSILVERTS_PER_HOUR,
accuracy_decimals=3,
icon=ICON_RADIOACTIVE,
state_class=STATE_CLASS_MEASUREMENT,
),
cv.Optional(CONF_RADIATION_TOTAL): sensor.sensor_schema(
unit_of_measurement=UNIT_NANOSIEVERT,
accuracy_decimals=0,
icon=ICON_RADIOACTIVE,
state_class=STATE_CLASS_TOTAL_INCREASING,
),
cv.Optional(CONF_RADIATION_DURATION): sensor.sensor_schema(
unit_of_measurement=UNIT_SECOND,
accuracy_decimals=0,
device_class=DEVICE_CLASS_DURATION,
state_class=STATE_CLASS_TOTAL_INCREASING,
),
}
)
.extend(cv.COMPONENT_SCHEMA)
.extend(ble_device_base.BLE_DEVICE_SCHEMA)
)
async def to_code(config: ConfigType) -> None:
var = await ble_device_base.new_ble_device(config, config[CONF_MAC_ADDRESS].as_hex)
var = await ble_device_base.new_ble_device_component(
config, config[CONF_MAC_ADDRESS].as_hex
)
sensors = sensor.sub_sensors(config)
await sensors(CONF_CO2, var.set_co2_sensor)
@@ -6,7 +6,7 @@ namespace esphome::atc_mithermometer {
ESPHOME_LOG_TAG(TAG, "atc_mithermometer");
void ATCMiThermometer::dump_child_config() {
void ATCMiThermometer::dump_config() {
ESP_LOGCONFIG(TAG, "ATC MiThermometer");
LOG_SENSOR(" ", "Temperature", this->temperature_sensor_);
LOG_SENSOR(" ", "Humidity", this->humidity_sensor_);
@@ -1,5 +1,6 @@
#pragma once
#include "esphome/core/component.h"
#include "esphome/components/sensor/sensor.h"
#include "esphome/components/ble_device_base/ble_device.h"
@@ -15,7 +16,7 @@ struct ParseResult {
int raw_offset;
};
class ATCMiThermometer final : public ble_device_base::ESPBTDeviceListener {
class ATCMiThermometer final : public Component, public ble_device_base::ESPBTDeviceListener {
SUB_SENSOR(temperature)
SUB_SENSOR(humidity)
SUB_SENSOR(battery_level)
@@ -26,7 +27,7 @@ class ATCMiThermometer final : public ble_device_base::ESPBTDeviceListener {
void set_address(uint64_t address) { address_ = address; };
bool parse_device(const ble_device_base::ESPBTDevice &device) override;
void dump_child_config() override;
void dump_config() override;
protected:
uint64_t address_;
@@ -28,7 +28,7 @@ AUTO_LOAD = ["ble_device_base"]
atc_mithermometer_ns = cg.esphome_ns.namespace("atc_mithermometer")
ATCMiThermometer = atc_mithermometer_ns.class_(
"ATCMiThermometer", ble_device_base.ESPBTDeviceListener
"ATCMiThermometer", ble_device_base.ESPBTDeviceListener, cg.Component
)
CONFIG_SCHEMA = cv.All(
@@ -71,12 +71,14 @@ CONFIG_SCHEMA = cv.All(
entity_category=ENTITY_CATEGORY_DIAGNOSTIC,
),
}
).extend(ble_device_base.BLE_DEVICE_SCHEMA),
)
.extend(cv.COMPONENT_SCHEMA)
.extend(ble_device_base.BLE_DEVICE_SCHEMA),
)
async def to_code(config: ConfigType) -> None:
var = await ble_device_base.new_ble_device(config)
var = await ble_device_base.new_ble_device_component(config)
cg.add(var.set_address(config[CONF_MAC_ADDRESS].as_hex))
@@ -39,8 +39,8 @@ void AXS15231Touchscreen::update_touches() {
this->status_clear_warning();
if (data[0] != 0 || data[1] == 0) // no touches
return;
uint16_t x = encode_uint16(low_nibble(data[2]), data[3]);
uint16_t y = encode_uint16(low_nibble(data[4]), data[5]);
uint16_t x = encode_uint16(data[2] & 0xF, data[3]);
uint16_t y = encode_uint16(data[4] & 0xF, data[5]);
this->add_raw_touch_position_(0, x, y);
}
+1 -1
View File
@@ -6,7 +6,7 @@ namespace esphome::b_parasite {
ESPHOME_LOG_TAG(TAG, "b_parasite");
void BParasite::dump_child_config() {
void BParasite::dump_config() {
ESP_LOGCONFIG(TAG, "b_parasite");
LOG_SENSOR(" ", "Battery Voltage", this->battery_voltage_sensor_);
LOG_SENSOR(" ", "Temperature", this->temperature_sensor_);
+3 -2
View File
@@ -1,11 +1,12 @@
#pragma once
#include "esphome/core/component.h"
#include "esphome/components/sensor/sensor.h"
#include "esphome/components/ble_device_base/ble_device.h"
namespace esphome::b_parasite {
class BParasite final : public ble_device_base::ESPBTDeviceListener {
class BParasite final : public Component, public ble_device_base::ESPBTDeviceListener {
SUB_SENSOR(battery_voltage)
SUB_SENSOR(temperature)
SUB_SENSOR(humidity)
@@ -17,7 +18,7 @@ class BParasite final : public ble_device_base::ESPBTDeviceListener {
void set_bindkey(const std::string &bindkey);
bool parse_device(const ble_device_base::ESPBTDevice &device) override;
void dump_child_config() override;
void dump_config() override;
protected:
// The received advertisement packet contains an unsigned 4 bits wrap-around counter
+7 -3
View File
@@ -26,7 +26,9 @@ CODEOWNERS = ["@rbaron"]
AUTO_LOAD = ["ble_device_base"]
b_parasite_ns = cg.esphome_ns.namespace("b_parasite")
BParasite = b_parasite_ns.class_("BParasite", ble_device_base.ESPBTDeviceListener)
BParasite = b_parasite_ns.class_(
"BParasite", ble_device_base.ESPBTDeviceListener, cg.Component
)
CONFIG_SCHEMA = cv.All(
ble_device_base.rename_legacy_hub_id("b_parasite"),
@@ -66,12 +68,14 @@ CONFIG_SCHEMA = cv.All(
state_class=STATE_CLASS_MEASUREMENT,
),
}
).extend(ble_device_base.BLE_DEVICE_SCHEMA),
)
.extend(cv.COMPONENT_SCHEMA)
.extend(ble_device_base.BLE_DEVICE_SCHEMA),
)
async def to_code(config: ConfigType) -> None:
var = await ble_device_base.new_ble_device(config)
var = await ble_device_base.new_ble_device_component(config)
cg.add(var.set_address(config[CONF_MAC_ADDRESS].as_hex))
@@ -206,7 +206,6 @@ void BK72xxBLETracker::dump_config() {
this->scan_duration_ / 1000, ble_units_to_ms(this->scan_interval_), this->scan_interval_,
ble_units_to_ms(this->scan_window_), this->scan_window_, this->scan_active_ ? "ACTIVE" : "PASSIVE",
this->scan_active_configured_ ? "ACTIVE" : "PASSIVE", YESNO(this->scan_continuous_));
this->dispatcher_.dump_child_configs();
}
// ---------------------------------------------------------------------------
@@ -35,7 +35,6 @@ from esphome.const import (
CONF_ACTIVE,
CONF_CONTINUOUS,
CONF_DURATION,
CONF_ID,
CONF_INTERVAL,
KEY_TARGET_PLATFORM,
)
@@ -194,11 +193,6 @@ def new_ble_device_component(
return cg.new_bus_component(register_ble_device, config, *args)
async def new_ble_device(config: ConfigType, *args: Any) -> cg.MockObj:
"""Construct a listener that is not a Component and attach it to the BLE hub."""
return await register_ble_device(cg.new_Pvariable(config[CONF_ID], *args), config)
# ---- shared validation / codegen helpers (platform-neutral) ----
@@ -323,8 +323,6 @@ class ESPBTDeviceListener {
/// Called at the end of each scan duration period.
virtual void on_scan_end() {}
virtual bool parse_device(const ESPBTDevice &device) = 0;
/// Called from the tracker's dump_config(); not dump_config() so Component listeners don't print twice.
virtual void dump_child_config() {}
};
} // namespace esphome::ble_device_base
@@ -58,13 +58,6 @@ class AdvDispatcher {
bool raw_only, const char *log_unclaimed_tag);
/// Fire listeners' on_scan_end and reset the per-scan discovered-log dedup.
void on_scan_end();
/// Print each listener's dump_child_config(); called from the tracker's dump_config().
void dump_child_configs() {
#ifdef ESPHOME_BLE_DEVICE_BASE_LISTENER_COUNT
for (auto *listener : this->listeners_)
listener->dump_child_config();
#endif
}
protected:
RawAdvertisementCallback raw_callback_{};
@@ -5,6 +5,6 @@ namespace esphome::ble_rssi {
ESPHOME_LOG_TAG(TAG, "ble_rssi");
void BLERSSISensor::dump_child_config() { LOG_SENSOR("", "BLE RSSI Sensor", this); }
void BLERSSISensor::dump_config() { LOG_SENSOR("", "BLE RSSI Sensor", this); }
} // namespace esphome::ble_rssi
@@ -1,5 +1,6 @@
#pragma once
#include "esphome/core/component.h"
#include "esphome/components/ble_device_base/ble_device.h"
#include "esphome/components/sensor/sensor.h"
@@ -9,7 +10,7 @@
// guard.
namespace esphome::ble_rssi {
class BLERSSISensor final : public sensor::Sensor, public ble_device_base::ESPBTDeviceListener {
class BLERSSISensor final : public sensor::Sensor, public ble_device_base::ESPBTDeviceListener, public Component {
public:
void set_address(uint64_t address) {
this->match_by_ = MATCH_BY_MAC_ADDRESS;
@@ -99,7 +100,7 @@ class BLERSSISensor final : public sensor::Sensor, public ble_device_base::ESPBT
}
return false;
}
void dump_child_config() override;
void dump_config() override;
protected:
enum MatchType { MATCH_BY_MAC_ADDRESS, MATCH_BY_IRK, MATCH_BY_SERVICE_UUID, MATCH_BY_IBEACON_UUID };
+3 -1
View File
@@ -19,7 +19,7 @@ AUTO_LOAD = ["ble_device_base"]
ble_rssi_ns = cg.esphome_ns.namespace("ble_rssi")
BLERSSISensor = ble_rssi_ns.class_(
"BLERSSISensor", sensor.Sensor, ble_device_base.ESPBTDeviceListener
"BLERSSISensor", sensor.Sensor, cg.Component, ble_device_base.ESPBTDeviceListener
)
@@ -50,6 +50,7 @@ CONFIG_SCHEMA = cv.All(
cv.Optional(CONF_IBEACON_UUID): ble_device_base.bt_uuid,
}
)
.extend(cv.COMPONENT_SCHEMA)
.extend(ble_device_base.BLE_DEVICE_SCHEMA),
cv.has_exactly_one_key(
CONF_MAC_ADDRESS, CONF_IRK, CONF_SERVICE_UUID, CONF_IBEACON_UUID
@@ -60,6 +61,7 @@ CONFIG_SCHEMA = cv.All(
async def to_code(config: ConfigType) -> None:
var = await sensor.new_sensor(config)
await cg.register_component(var, config)
await ble_device_base.register_ble_device(var, config)
if mac_address := config.get(CONF_MAC_ADDRESS):
@@ -5,6 +5,6 @@ namespace esphome::ble_scanner {
ESPHOME_LOG_TAG(TAG, "ble_scanner");
void BLEScanner::dump_child_config() { LOG_TEXT_SENSOR("", "BLE Scanner", this); }
void BLEScanner::dump_config() { LOG_TEXT_SENSOR("", "BLE Scanner", this); }
} // namespace esphome::ble_scanner
+3 -2
View File
@@ -4,6 +4,7 @@
#include <cstdio>
#include <ctime>
#include "esphome/core/component.h"
#include "esphome/core/helpers.h"
#include "esphome/core/string_ref.h"
#include "esphome/components/ble_device_base/ble_device.h"
@@ -15,7 +16,7 @@
// guard.
namespace esphome::ble_scanner {
class BLEScanner final : public text_sensor::TextSensor, public ble_device_base::ESPBTDeviceListener {
class BLEScanner final : public text_sensor::TextSensor, public ble_device_base::ESPBTDeviceListener, public Component {
public:
bool parse_device(const ble_device_base::ESPBTDevice &device) override {
char addr_buf[MAC_ADDRESS_PRETTY_BUFFER_SIZE];
@@ -30,7 +31,7 @@ class BLEScanner final : public text_sensor::TextSensor, public ble_device_base:
this->publish_state(buf);
return true;
}
void dump_child_config() override;
void dump_config() override;
};
} // namespace esphome::ble_scanner
@@ -9,17 +9,19 @@ ble_scanner_ns = cg.esphome_ns.namespace("ble_scanner")
BLEScanner = ble_scanner_ns.class_(
"BLEScanner",
text_sensor.TextSensor,
cg.Component,
ble_device_base.ESPBTDeviceListener,
)
CONFIG_SCHEMA = cv.All(
ble_device_base.rename_legacy_hub_id("ble_scanner"),
text_sensor.text_sensor_schema(BLEScanner).extend(
ble_device_base.BLE_DEVICE_SCHEMA
),
text_sensor.text_sensor_schema(BLEScanner)
.extend(cv.COMPONENT_SCHEMA)
.extend(ble_device_base.BLE_DEVICE_SCHEMA),
)
async def to_code(config: ConfigType) -> None:
var = await text_sensor.new_text_sensor(config)
await cg.register_component(var, config)
await ble_device_base.register_ble_device(var, config)
@@ -114,10 +114,10 @@ void BME280Component::setup() {
this->calibration_.h1 = read_u8_(BME280_REGISTER_DIG_H1);
this->calibration_.h2 = read_s16_le_(BME280_REGISTER_DIG_H2);
this->calibration_.h3 = read_u8_(BME280_REGISTER_DIG_H3);
this->calibration_.h4 = sign_extend<12>(
encode_uint12(read_u8_(BME280_REGISTER_DIG_H4), low_nibble(read_u8_(BME280_REGISTER_DIG_H4 + 1))));
this->calibration_.h5 = sign_extend<12>(
encode_uint12(read_u8_(BME280_REGISTER_DIG_H5 + 1), high_nibble(read_u8_(BME280_REGISTER_DIG_H5))));
this->calibration_.h4 =
sign_extend<12>(read_u8_(BME280_REGISTER_DIG_H4) << 4 | (read_u8_(BME280_REGISTER_DIG_H4 + 1) & 0x0F));
this->calibration_.h5 =
sign_extend<12>(read_u8_(BME280_REGISTER_DIG_H5 + 1) << 4 | (read_u8_(BME280_REGISTER_DIG_H5) >> 4));
this->calibration_.h6 = read_u8_(BME280_REGISTER_DIG_H6);
uint8_t humid_control_val = 0;
+3 -4
View File
@@ -70,9 +70,8 @@ bool BME680Component::setup_registers_() {
this->calibration_.t2 = encode_value_le<int16_t>(&coeff1[1]);
this->calibration_.t3 = coeff1[3];
// H1 and H2 are 12-bit values sharing byte 1: its low nibble ends H1, its high nibble ends H2
this->calibration_.h1 = encode_uint12(coeff2[2], low_nibble(coeff2[1]));
this->calibration_.h2 = encode_uint12(coeff2[0], high_nibble(coeff2[1]));
this->calibration_.h1 = coeff2[2] << 4 | (coeff2[1] & 0x0F);
this->calibration_.h2 = encode_uint16(coeff2[0], coeff2[1]) >> 4;
this->calibration_.h3 = coeff2[3];
this->calibration_.h4 = coeff2[4];
this->calibration_.h5 = coeff2[5];
@@ -244,7 +243,7 @@ void BME680Component::read_data_() {
uint32_t raw_pressure = encode_uint24(data[2], data[3], data[4]) >> 4;
uint32_t raw_humidity = encode_uint16(data[8], data[9]);
uint16_t raw_gas = encode_uint16(data[13], data[14]) >> 6;
uint8_t gas_range = low_nibble(data[14]);
uint8_t gas_range = data[14] & 0x0F;
float temperature = this->calc_temperature_(raw_temperature);
float pressure = this->calc_pressure_(raw_pressure);
@@ -15,7 +15,7 @@ CONF_REPLAY_PROTECTION = "replay_protection"
bthome_mithermometer_ns = cg.esphome_ns.namespace("bthome_mithermometer")
BTHomeMiThermometer = bthome_mithermometer_ns.class_(
"BTHomeMiThermometer", ble_device_base.ESPBTDeviceListener
"BTHomeMiThermometer", ble_device_base.ESPBTDeviceListener, cg.Component
)
@@ -34,6 +34,7 @@ def bthome_mithermometer_base_schema(
cv.Optional(CONF_REPLAY_PROTECTION, default=False): cv.boolean,
}
)
.extend(cv.COMPONENT_SCHEMA)
.extend(extra_schema)
.extend(ble_device_base.BLE_DEVICE_SCHEMA),
_validate_replay_protection,
@@ -50,6 +51,7 @@ def _validate_replay_protection(config: ConfigType) -> ConfigType:
async def setup_bthome_mithermometer(var: MockObj, config: ConfigType) -> None:
await cg.register_component(var, config)
await ble_device_base.register_ble_device(var, config)
cg.add(var.set_address(config[CONF_MAC_ADDRESS].as_hex))
if bindkey := config.get(CONF_BINDKEY):
@@ -154,7 +154,7 @@ static bool get_bthome_value_length(uint8_t obj_type, size_t &value_length) {
}
}
void BTHomeMiThermometer::dump_child_config() {
void BTHomeMiThermometer::dump_config() {
char addr_buf[MAC_ADDRESS_PRETTY_BUFFER_SIZE];
ESP_LOGCONFIG(TAG, "BTHome MiThermometer");
ESP_LOGCONFIG(TAG, " MAC Address: %s", format_mac_address(addr_buf, this->address_));
@@ -2,6 +2,7 @@
#include "esphome/components/ble_device_base/ble_device.h"
#include "esphome/components/sensor/sensor.h"
#include "esphome/core/component.h"
#include <cstdint>
#include <initializer_list>
@@ -16,7 +17,7 @@ namespace testing {
class ReplayClockAccess;
} // namespace testing
class BTHomeMiThermometer final : public ble_device_base::ESPBTDeviceListener {
class BTHomeMiThermometer final : public ble_device_base::ESPBTDeviceListener, public Component {
SUB_SENSOR(temperature)
SUB_SENSOR(humidity)
SUB_SENSOR(battery_level)
@@ -30,7 +31,7 @@ class BTHomeMiThermometer final : public ble_device_base::ESPBTDeviceListener {
void set_replay_protection(bool replay_protection) { this->replay_protection_ = replay_protection; }
#endif
void dump_child_config() override;
void dump_config() override;
bool parse_device(const ble_device_base::ESPBTDevice &device) override;
protected:
+10 -4
View File
@@ -515,12 +515,18 @@ void CC1101Component::dump_config() {
}
void CC1101Component::begin_tx() {
if (this->state_.PKT_FORMAT == static_cast<uint8_t>(PacketFormat::PACKET_FORMAT_FIFO)) {
ESP_LOGW(TAG, "begin_tx is for async serial mode; use send_packet in packet mode");
return;
}
this->wake_up();
// Read the mode before the PKTCTRL0 write below overwrites it in state_
const bool packet_mode = this->state_.PKT_FORMAT == static_cast<uint8_t>(PacketFormat::PACKET_FORMAT_FIFO);
// Ensure Packet Format is 3 (Async Serial)
this->write_(Register::PKTCTRL0, 0x32);
ESP_LOGV(TAG, "Beginning TX sequence");
if (this->gdo0_pin_ != nullptr) {
// setup() only attaches the interrupt in packet mode. In async serial mode the pin may be
// shared with remote_receiver, and detaching here would remove the receiver's interrupt.
if (packet_mode) {
this->gdo0_pin_->detach_interrupt();
}
this->gdo0_pin_->pin_mode(gpio::FLAG_OUTPUT);
}
// Transition through IDLE to bypass CCA (Clear Channel Assessment) which can
-1
View File
@@ -36,7 +36,6 @@ CONF_CONVERSION_RATE = "conversion_rate"
CONF_CRC_ENABLE = "crc_enable"
CONF_DATA_BITS = "data_bits"
CONF_DESCRIPTION = "description"
CONF_DESTINATION = "destination"
CONF_DEVICE_TYPE = "device_type"
CONF_DISCONNECTS = "disconnects"
CONF_DRAW_ROUNDING = "draw_rounding"
@@ -68,7 +68,7 @@ void CST3240Touchscreen::update_touches() {
return;
}
const uint8_t count = low_nibble(data[cstxxx::FINGER_COUNT_IDX]);
const uint8_t count = data[cstxxx::FINGER_COUNT_IDX] & 0x0F;
if (count <= CST3240_MAX_TOUCHES) {
for (uint8_t i = 0; i < count; i++) {
auto touch = cstxxx::decode_touch(data, i);
@@ -1,5 +1,4 @@
#include "cst328_touchscreen.h"
#include "esphome/core/helpers.h"
#include "esphome/core/log.h"
namespace esphome::cst328 {
@@ -100,7 +99,7 @@ void CST328Touchscreen::update_touches() {
return;
}
uint8_t touch_cnt = low_nibble(touch_data[CST_REG_FINGER_COUNT_IDX]);
uint8_t touch_cnt = touch_data[CST_REG_FINGER_COUNT_IDX] & 0x0F;
if (touch_cnt == 0 || touch_cnt > CST328_TOUCH_MAX_POINTS) {
this->update_button_state_(false);
} else {
@@ -68,8 +68,8 @@ void CST816Touchscreen::update_touches() {
return;
}
uint16_t x = encode_uint16(low_nibble(data[REG_XPOS_HIGH]), data[REG_XPOS_LOW]);
uint16_t y = encode_uint16(low_nibble(data[REG_YPOS_HIGH]), data[REG_YPOS_LOW]);
uint16_t x = encode_uint16(data[REG_XPOS_HIGH] & 0xF, data[REG_XPOS_LOW]);
uint16_t y = encode_uint16(data[REG_YPOS_HIGH] & 0xF, data[REG_YPOS_LOW]);
ESP_LOGV(TAG, "Read touch %d/%d", x, y);
this->add_raw_touch_position_(0, x, y);
}
+3 -10
View File
@@ -37,18 +37,11 @@ struct TouchRecord {
uint8_t z;
};
/// Decodes record `index` of a touch report. Each record is 5 bytes:
/// [id:4 | event:4] [x 11..4] [y 11..4] [x 3..0 | y 3..0] [z].
/// The first record is followed by 2 extra bytes, so later records start 2 bytes further on.
/// Decodes record `index` of a touch report.
inline TouchRecord decode_touch(const uint8_t *report, uint8_t index) {
const uint8_t *r = report + index * 5 + (index == 0 ? 0 : 2);
return {
.id = high_nibble(r[0]),
.event = low_nibble(r[0]),
.x = encode_uint12(r[1], high_nibble(r[3])),
.y = encode_uint12(r[2], low_nibble(r[3])),
.z = r[4],
};
return {static_cast<uint8_t>(r[0] >> 4), static_cast<uint8_t>(r[0] & 0x0F),
static_cast<uint16_t>(r[1] << 4 | r[3] >> 4), static_cast<uint16_t>(r[2] << 4 | (r[3] & 0x0F)), r[4]};
}
class CSTTouchscreen : public touchscreen::Touchscreen, public i2c::I2CDevice {
+7 -15
View File
@@ -4,7 +4,6 @@
#include "esphome/components/remote_base/remote_base.h"
#include "esphome/core/log.h"
#include "esphome/core/hal.h"
#include "esphome/core/helpers.h"
namespace esphome::daikin_arc {
@@ -24,10 +23,7 @@ void DaikinArcClimate::setup() {
}
void DaikinArcClimate::transmit_query_() {
// In flash: an initialized local array is copied from a RAM constant on ESP8266
static constexpr uint8_t REMOTE_HEADER_TEMPLATE[8] PROGMEM = {0x11, 0xDA, 0x27, 0x00, 0x84, 0x87, 0x20, 0x00};
uint8_t remote_header[sizeof(REMOTE_HEADER_TEMPLATE)];
progmem_memcpy(remote_header, REMOTE_HEADER_TEMPLATE, sizeof(remote_header));
uint8_t remote_header[8] = {0x11, 0xDA, 0x27, 0x00, 0x84, 0x87, 0x20, 0x00};
// Calculate checksum
for (size_t i = 0; i < sizeof(remote_header) - 1; i++) {
@@ -54,13 +50,11 @@ void DaikinArcClimate::transmit_query_() {
void DaikinArcClimate::transmit_state() {
// 0x11, 0xDA, 0x27, 0x00, 0xC5, 0x00, 0x00, 0xD7, 0x11, 0xDA, 0x27, 0x00,
// 0x42, 0x49, 0x05, 0xA2,
static constexpr uint8_t REMOTE_HEADER_TEMPLATE[20] PROGMEM = {
0x11, 0xDA, 0x27, 0x00, 0x02, 0xd0, 0x02, 0x03, 0x80, 0x03, 0x82, 0x30, 0x41, 0x1f, 0x82, 0xf4,
/* とつど */
/* 0x13 */
0x00, 0x24, 0x00, 0x00};
uint8_t remote_header[sizeof(REMOTE_HEADER_TEMPLATE)];
progmem_memcpy(remote_header, REMOTE_HEADER_TEMPLATE, sizeof(remote_header));
uint8_t remote_header[20] = {0x11, 0xDA, 0x27, 0x00, 0x02, 0xd0, 0x02, 0x03, 0x80, 0x03, 0x82, 0x30, 0x41, 0x1f, 0x82,
0xf4,
/* とつど */
/* 0x13 */
0x00, 0x24, 0x00, 0x00};
// 05 0 [1:3]MODE 1 [OFF TMR] [ON TMR] Power
// 06-07 TEMP
@@ -74,7 +68,7 @@ void DaikinArcClimate::transmit_state() {
// 16 [0:3] 8 00 [6:7] SENSOR WIND = 11 / NORMAL = 00
// 17 24
static constexpr uint8_t REMOTE_STATE_TEMPLATE[DAIKIN_STATE_FRAME_SIZE] PROGMEM = {
uint8_t remote_state[19] = {
0x11, 0xDA, 0x27, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x06, 0x60, 0x00, 0x0a, 0xC4,
/* MODE TEMP HUMD FANH FANL
パワフル音声応答 */
@@ -87,8 +81,6 @@ void DaikinArcClimate::transmit_state() {
/* ON 0x83 */
/* OF 0x80 */
};
uint8_t remote_state[sizeof(REMOTE_STATE_TEMPLATE)];
progmem_memcpy(remote_state, REMOTE_STATE_TEMPLATE, sizeof(remote_state));
remote_state[5] = this->operation_mode_() | 0x08;
remote_state[6] = this->temperature_();
+1 -3
View File
@@ -605,9 +605,7 @@ async def to_code(config: ConfigType) -> None:
cg.add(var.set_type(conf[CONF_TYPE]))
for conf in config[CONF_SELECTS]:
var = await select.new_select(
conf, options=conf[CONF_OPTIONS], fixed_options=True
)
var = await select.new_select(conf, options=conf[CONF_OPTIONS])
await cg.register_component(var, conf)
for conf in config[CONF_SENSORS]:
+1 -1
View File
@@ -41,5 +41,5 @@ CONFIG_SCHEMA = (
async def to_code(config: ConfigType) -> None:
var = await select.new_select(config, options=DUCO_MODE_OPTIONS, fixed_options=True)
var = await select.new_select(config, options=DUCO_MODE_OPTIONS)
await register_duco_device(var, config, config[CONF_DUCO_ID])
@@ -35,12 +35,12 @@ void EKTF2232Touchscreen::setup() {
if (err == i2c::ERROR_OK) {
err = this->read(received, 4);
if (err == i2c::ERROR_OK) {
this->x_raw_max_ = encode_uint16(high_nibble(received[3]), received[2]);
this->x_raw_max_ = ((received[2])) | ((received[3] & 0xf0) << 4);
err = this->write(GET_Y_RES, 4);
if (err == i2c::ERROR_OK) {
err = this->read(received, 4);
if (err == i2c::ERROR_OK) {
this->y_raw_max_ = encode_uint16(high_nibble(received[3]), received[2]);
this->y_raw_max_ = ((received[2])) | ((received[3] & 0xf0) << 4);
}
}
}
@@ -74,9 +74,8 @@ void EKTF2232Touchscreen::update_touches() {
for (int i = 0; i < touch_count; i++) {
uint8_t *d = raw + 1 + (i * 3);
// d[0] holds the top 4 bits of X (high nibble) and of Y (low nibble)
x_raw = encode_uint16(high_nibble(d[0]), d[1]);
y_raw = encode_uint16(low_nibble(d[0]), d[2]);
x_raw = (d[0] & 0xF0) << 4 | d[1];
y_raw = (d[0] & 0x0F) << 8 | d[2];
this->add_raw_touch_position_(i, x_raw, y_raw);
}
}
@@ -485,14 +485,6 @@ void ESP32BLETracker::dump_config() {
if (this->scan_start_fail_count_) {
ESP_LOGCONFIG(TAG, " Scan Start Fail Count: %d", this->scan_start_fail_count_);
}
#ifdef ESPHOME_ESP32_BLE_TRACKER_LISTENER_COUNT
for (auto *listener : this->listeners_)
listener->dump_child_config();
#endif
#ifdef ESPHOME_BLE_DEVICE_BASE_LISTENER_COUNT
for (auto *listener : this->neutral_listeners_)
listener->dump_child_config();
#endif
}
#ifdef USE_ESP32_BLE_DEVICE
@@ -69,8 +69,8 @@ void FT5x06Touchscreen::update_touches() {
for (uint8_t i = 0; i != touch_cnt; i++) {
uint8_t status = data[i][0] >> 6;
uint8_t id = data[i][2] >> 3;
uint16_t x = encode_uint16(low_nibble(data[i][0]), data[i][1]);
uint16_t y = encode_uint16(low_nibble(data[i][2]), data[i][3]);
uint16_t x = encode_uint16(data[i][0] & 0x0F, data[i][1]);
uint16_t y = encode_uint16(data[i][2] & 0xF, data[i][3]);
ESP_LOGV(TAG, "Read %X status, id: %d, pos %d/%d", status, id, x, y);
if (status == 0 || status == 2) {
+5 -5
View File
@@ -90,31 +90,31 @@ void FT63X6Touchscreen::update_touches() {
}
}
uint8_t FT63X6Touchscreen::read_touch_number_() { return low_nibble(this->read_byte_(FT63X6_ADDR_TD_STATUS)); }
uint8_t FT63X6Touchscreen::read_touch_number_() { return this->read_byte_(FT63X6_ADDR_TD_STATUS) & 0x0F; }
// Touch 1 functions
uint16_t FT63X6Touchscreen::read_touch_x_(uint8_t touch) {
uint8_t read_buf[2];
read_buf[0] = this->read_byte_(FT63X6_ADDR_TOUCH1_X + (touch * 6));
read_buf[1] = this->read_byte_(FT63X6_ADDR_TOUCH1_X + 1 + (touch * 6));
return encode_uint16(low_nibble(read_buf[0]), read_buf[1]);
return encode_uint16(read_buf[0] & 0x0f, read_buf[1]);
}
uint16_t FT63X6Touchscreen::read_touch_y_(uint8_t touch) {
uint8_t read_buf[2];
read_buf[0] = this->read_byte_(FT63X6_ADDR_TOUCH1_Y + (touch * 6));
read_buf[1] = this->read_byte_(FT63X6_ADDR_TOUCH1_Y + 1 + (touch * 6));
return encode_uint16(low_nibble(read_buf[0]), read_buf[1]);
return encode_uint16(read_buf[0] & 0x0f, read_buf[1]);
}
uint8_t FT63X6Touchscreen::read_touch_event_(uint8_t touch) {
return this->read_byte_(FT63X6_ADDR_TOUCH1_X + (touch * 6)) >> 6;
}
uint8_t FT63X6Touchscreen::read_touch_id_(uint8_t touch) {
return high_nibble(this->read_byte_(FT63X6_ADDR_TOUCH1_ID + (touch * 6)));
return this->read_byte_(FT63X6_ADDR_TOUCH1_ID + (touch * 6)) >> 4;
}
uint8_t FT63X6Touchscreen::read_touch_weight_(uint8_t touch) {
return this->read_byte_(FT63X6_ADDR_TOUCH1_WEIGHT + (touch * 6));
}
uint8_t FT63X6Touchscreen::read_touch_misc_(uint8_t touch) {
return high_nibble(this->read_byte_(FT63X6_ADDR_TOUCH1_MISC + (touch * 6)));
return this->read_byte_(FT63X6_ADDR_TOUCH1_MISC + (touch * 6)) >> 4;
}
uint8_t FT63X6Touchscreen::read_byte_(uint8_t addr) {
@@ -1,7 +1,6 @@
#include "gsl3670_touchscreen.h"
#include "esphome/core/log.h"
#include "esphome/core/hal.h"
#include "esphome/core/helpers.h"
namespace esphome::gsl3670 {
@@ -67,11 +66,10 @@ void GSL3670Touchscreen::update_touches() {
// Build gsl_touch_info exactly as the Seeed driver does
for (uint8_t j = 0; j != finger_num; j++) {
// Record: y_lo, y_hi, x_lo, then the id (high nibble) and the top 4 bits of X (low nibble)
const uint8_t *record = &buf[(j + 1) * 4];
auto x = encode_uint16(low_nibble(record[3]), record[2]);
auto y = encode_uint16(record[1], record[0]);
auto id = high_nibble(record[3]);
// buf[(j+1)*4 + 0..3]: byte0=y_lo, byte1=y_hi, byte2=x_lo, byte3=id|x_hi
auto x = (uint16_t) (((buf[(j + 1) * 4 + 3] & 0x0f) << 8) | buf[(j + 1) * 4 + 2]);
auto y = (uint16_t) ((buf[(j + 1) * 4 + 1] << 8) | buf[(j + 1) * 4 + 0]);
auto id = (buf[(j + 1) * 4 + 3] >> 4) & 0x0f;
ESP_LOGV(TAG, "Touch id=%u, x=%u y=%u", id, x, y);
if (x <= 8192 && y <= 8192)
this->add_raw_touch_position_(id, x, y);
+1 -1
View File
@@ -1,6 +1,5 @@
from esphome import automation, pins
import esphome.codegen as cg
from esphome.components.const import CONF_DESTINATION
from esphome.components.esp32 import include_builtin_idf_component
import esphome.config_validation as cv
from esphome.const import (
@@ -27,6 +26,7 @@ MAX_LOGICAL_ADDRESS = 15
MAX_PAYLOAD_LENGTH = 15
CONF_PROMISCUOUS_MODE = "promiscuous_mode"
CONF_MONITOR_MODE = "monitor_mode"
CONF_DESTINATION = "destination"
CONF_OPCODE = "opcode"
hdmi_cec_ns = cg.esphome_ns.namespace("hdmi_cec")
+6 -28
View File
@@ -1,7 +1,6 @@
from collections.abc import Callable, Iterable
from dataclasses import dataclass
from esphome.automation import parent_ref
import esphome.codegen as cg
import esphome.config_validation as cv
from esphome.const import CONF_ATTRIBUTE, CONF_ENTITY_ID, CONF_ID, CONF_INTERNAL
@@ -15,14 +14,6 @@ CODEOWNERS = ["@OttoWinter", "@esphome/core"]
DOMAIN = "homeassistant"
homeassistant_ns = cg.esphome_ns.namespace("homeassistant")
# The API server subscribes the imported entities in its setup() and prints their dumps
IMPORT_SUBSCRIBE = cg.HubFunction(
"homeassistant_subscribe", cg.void, [], define="USE_API_HOMEASSISTANT_IMPORTS"
)
IMPORT_DUMP = cg.HubFunction(
"homeassistant_dump", cg.void, [], define="USE_API_HOMEASSISTANT_IMPORTS"
)
def validate_entity_domain(
platform: str, supported_domains: Iterable[str]
@@ -40,16 +31,18 @@ def validate_entity_domain(
return validator
HOME_ASSISTANT_IMPORT_CONTROL_SCHEMA = cv.Schema(
HOME_ASSISTANT_IMPORT_SCHEMA = cv.Schema(
{
cv.Optional(CONF_ENTITY_ID): cv.entity_id,
cv.Optional(CONF_ATTRIBUTE): cv.string,
cv.Optional(CONF_INTERNAL, default=True): cv.boolean,
}
)
HOME_ASSISTANT_IMPORT_SCHEMA = HOME_ASSISTANT_IMPORT_CONTROL_SCHEMA.extend(
HOME_ASSISTANT_IMPORT_CONTROL_SCHEMA = cv.Schema(
{
cv.Optional(CONF_ATTRIBUTE): cv.string,
cv.Optional(CONF_ENTITY_ID): cv.entity_id,
cv.Optional(CONF_INTERNAL, default=True): cv.boolean,
}
)
@@ -89,14 +82,8 @@ async def _emit_state_subscription_count_define() -> None:
def setup_home_assistant_entity(
var: MockObj,
config: ConfigType,
state_subscription_count: int = 1,
services: bool = False,
var: MockObj, config: ConfigType, state_subscription_count: int = 1
) -> None:
"""Set up an imported entity; services for one that sends actions."""
if services:
cg.add_define("USE_API_HOMEASSISTANT_SERVICES")
# A wizard input has no entity_id; the api wizard sets it
cg.add_optional(var.set_entity_id, config.get(CONF_ENTITY_ID))
cg.add_optional(var.set_attribute, config.get(CONF_ATTRIBUTE))
@@ -106,12 +93,3 @@ def setup_home_assistant_entity(
if data.state_subscription_count == 0:
CORE.add_job(_emit_state_subscription_count_define)
data.state_subscription_count += state_subscription_count
async def register_home_assistant_import(var: MockObj) -> None:
"""Attach an imported entity to the API server, which subscribes it and prints its dump."""
from esphome.components.api import DOMAIN as API_DOMAIN
api = await cg.get_variable(CORE.config[API_DOMAIN][CONF_ID])
IMPORT_SUBSCRIBE.add(api, parent_ref(var).subscribe())
IMPORT_DUMP.add(api, parent_ref(var).dump_config())
@@ -1,3 +1,4 @@
import esphome.codegen as cg
from esphome.components import binary_sensor
from esphome.types import ConfigType
@@ -5,7 +6,6 @@ from .. import (
HOME_ASSISTANT_IMPORT_SCHEMA,
final_validate_entity_id,
homeassistant_ns,
register_home_assistant_import,
setup_home_assistant_entity,
)
@@ -14,7 +14,7 @@ DEPENDENCIES = ["api"]
AUTO_LOAD = ["homeassistant"]
HomeassistantBinarySensor = homeassistant_ns.class_(
"HomeassistantBinarySensor", binary_sensor.BinarySensor
"HomeassistantBinarySensor", binary_sensor.BinarySensor, cg.Component
)
CONFIG_SCHEMA = binary_sensor.binary_sensor_schema(HomeassistantBinarySensor).extend(
@@ -26,5 +26,5 @@ FINAL_VALIDATE_SCHEMA = final_validate_entity_id
async def to_code(config: ConfigType) -> None:
var = await binary_sensor.new_binary_sensor(config)
await cg.register_component(var, config)
setup_home_assistant_entity(var, config)
await register_home_assistant_import(var)
Loaded 100 of 319 files, more files were not shown because too many files have changed in this diff. Show more