Skip to content

espradio: add ESP32 BLE support using espradio VHCI transport - #464

Open
deadprogram wants to merge 5 commits into
devfrom
esp32-hci
Open

espradio: add ESP32 BLE support using espradio VHCI transport#464
deadprogram wants to merge 5 commits into
devfrom
esp32-hci

Conversation

@deadprogram

@deadprogram deadprogram commented Jul 29, 2026

Copy link
Copy Markdown
Member

Adds a new espradio build tag that wires the existing HCI stack up to the ESP32's virtual HCI controller via tinygo.org/x/espradio. The new Adapter calls espradio.BLEInit() and hands a VHCI-backed transport to newBLEStack(); for WiFi+BLE co-existence, espradio.Enable() should be called first.

The shared HCI implementation files pick up the espradio build tag, and go.mod is updated for the espradio and drivers dependencies.

Works on ESP32C3 for both central and advertiser roles!

tinygo flash -target xiao-esp32c3 -tags espradio -monitor ./examples/heartrate
tinygo flash -target xiao-esp32c3 -tags espradio -ldflags="-X main.DeviceAddress=6B:C3:F8:21:99:0D" -monitor ./examples/heartrate-monitor/

DONE: Requires that both tinygo-org/espradio#58 and tinygo-org/espradio#59 be merged, which is why this is in draft. It also requires the current dev branch of TinyGo.

@marcofeltmann marcofeltmann left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Local build with v1.24.4 but github workflow on macos with v1.25.0 seems odd, better sync the versions.

Comment thread .github/workflows/macos.yml Outdated
Signed-off-by: deadprogram <ron@hybridgroup.com>
@deadprogram

Copy link
Copy Markdown
Member Author

Test failure until the next TinyGo release, since this repo builds that use the current release, but this PR needs the upcoming one.

Adds a new `espradio` build tag that wires the existing HCI stack up to
the ESP32's virtual HCI controller via tinygo.org/x/espradio. The new
Adapter calls espradio.BLEInit() and hands a VHCI-backed transport to
newBLEStack(); for WiFi+BLE co-existence, espradio.Enable() should be
called first.

The shared HCI implementation files pick up the espradio build tag, and
go.mod is updated for the espradio and drivers dependencies.

Signed-off-by: deadprogram <ron@hybridgroup.com>
Advertisement payload parsing and the HCI receive path both used
wire-supplied lengths to index fixed-size buffers without validating
them, panicking with "slice out of range" while scanning on ESP32.

Add nextADField as a single bounds-checked AD field iterator, clamp the
read size in poll after the 4-byte rounding, reject packets larger than
the read buffer, and validate lengths before indexing in the event and
ACL handlers. Add tests for malformed advertisement payloads.

Signed-off-by: deadprogram <ron@hybridgroup.com>
poll advanced its parse cursor a byte at a time while reading another
packet on every iteration, so reads outpaced parsing and the buffer
ratcheted full. Once it did, the read was clamped to the space left,
which the CYW43439 rejects because it hands back whole packets.

Advance the cursor to everything that has been read, only read when
there is room for a full packet, and size the buffer for the largest
packet plus the transport's header and alignment padding.

Signed-off-by: deadprogram <ron@hybridgroup.com>
@deadprogram

Copy link
Copy Markdown
Member Author

This PR is now working, just awaiting the release of TinyGo 0.42 which will be right after Go 1.27

@deadprogram
deadprogram marked this pull request as ready for review August 4, 2026 01:31
Scanning was hardcoded to passive, so peripherals that only put their
complete local name in the scan response were reported without one.
Default to active scanning instead, and pass our own address type in the
scan parameters so the controller can fill in the SCAN_REQ.

Add Adapter.SetScanType to select ScanTypeActive or ScanTypePassive
before calling Scan, for callers that want the lower power use and
silence of listening only.

Signed-off-by: deadprogram <ron@hybridgroup.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants