Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
104 changes: 104 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

45 changes: 41 additions & 4 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,18 @@ default-adapters = ["panda", "socketcan"]
all-adapters = ["default-adapters", "vector-xl", "j2534", "peak"]
serde = ["dep:serde"]

# USB backends (transports). These are orthogonal to the adapters below: an adapter that
# speaks USB picks `RusbBackend` on native and `WebUsbBackend` on wasm via the target.
rusb-backend = ["dep:rusb"]
webusb = ["dep:web-sys", "dep:js-sys", "dep:console_error_panic_hook"]

# adapters
vector-xl = ["dep:bindgen", "dep:libloading"]
j2534 = ["dep:libloading", "dep:winreg"]
panda = ["dep:rusb"]
peak = ["dep:rusb"]
# `panda`/`peak` default to the rusb backend so `--features panda` is a working native
# build. For the browser, add the WebUSB transport: `--features panda,webusb`.
panda = ["rusb-backend"]
peak = ["rusb-backend"]
socketcan = ["dep:libc", "dep:socket2"]

# adapter tests
Expand All @@ -35,14 +42,44 @@ test-vector = ["vector-xl"]
async-stream = "0.3.5"
bstr = "1.9.0"
hex = "0.4"
rusb = { version = "0.9", optional = true }
serde = { version = "1.0.197", features = ["derive"], optional = true }
strum = "0.26"
strum_macros = "0.26"
thiserror = "1.0.58"
# Base (wasm-compatible) tokio. Native re-declares tokio with the "full" feature below.
tokio = { version = "1.36.0", default-features = false, features = ["sync"] }
tokio-stream = { version = "0.1.14", default-features = false }
tracing = "0.1"

# WebUSB backend dependencies (only built with the `webusb` feature, on wasm targets).
web-sys = { version = "0.3", optional = true, features = [
"Window",
"Navigator",
"Usb",
"UsbDevice",
"UsbDeviceRequestOptions",
"UsbControlTransferParameters",
"UsbRequestType",
"UsbRecipient",
"UsbInTransferResult",
"UsbOutTransferResult",
] }
js-sys = { version = "0.3", optional = true }
console_error_panic_hook = { version = "0.1", optional = true }

# Native-only: rusb (libusb won't cross-compile to wasm, so it lives here and the
# `rusb-backend`/`panda` features only pull it on native), a minimal block_on (used by
# AsyncCanAdapter's processing thread), and the full tokio runtime/stream features.
[target.'cfg(not(target_arch = "wasm32"))'.dependencies]
rusb = { version = "0.9", optional = true }
pollster = "0.3"
tokio = { version = "1.36.0", features = ["full"] }
tokio-stream = "0.1.14"
tracing = "0.1"

# Browser-only: wasm-bindgen glue used to drive the adapter via spawn_local.
[target.'cfg(target_arch = "wasm32")'.dependencies]
wasm-bindgen = "0.2"
wasm-bindgen-futures = "0.4"

[target.'cfg(target_os = "linux")'.dependencies]
socket2 = { version = "0.6.0", optional = true }
Expand Down
27 changes: 27 additions & 0 deletions scripts/release_peak_usb.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
#!/usr/bin/env bash
# Release PEAK PCAN-USB FD adapters from the in-kernel `peak_usb` driver so a browser
# (WebUSB) or raw libusb can claim them.
#
# Re-bind later by replugging the device, or:
# sudo modprobe -r peak_usb && sudo modprobe peak_usb
set -euo pipefail

DRIVER=/sys/bus/usb/drivers/peak_usb

if [ ! -d "$DRIVER" ]; then
echo "peak_usb driver not loaded; nothing bound."
exit 0
fi

found=0
for iface in "$DRIVER"/*:*; do
[ -e "$iface" ] || continue
name=$(basename "$iface")
echo "Unbinding $name from peak_usb"
echo -n "$name" | sudo tee "$DRIVER/unbind" > /dev/null
found=1
done

if [ "$found" -eq 0 ]; then
echo "No PEAK devices currently bound to peak_usb."
fi
4 changes: 2 additions & 2 deletions src/can/adapter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
/// Adapter backends are feature-gated. If no adapter features are enabled, or no
/// supported hardware/interface is available, this returns [`crate::error::Error::NotFound`].
pub fn get_adapter() -> Result<crate::can::AsyncCanAdapter, crate::error::Error> {
#[cfg(feature = "panda")]
#[cfg(all(not(target_arch = "wasm32"), feature = "panda"))]
{
let bitrate_cfg = crate::can::bitrate::BitrateBuilder::new::<crate::panda::Panda>()
.bitrate(500_000)
Expand All @@ -20,7 +20,7 @@ pub fn get_adapter() -> Result<crate::can::AsyncCanAdapter, crate::error::Error>
}
}

#[cfg(feature = "peak")]
#[cfg(all(not(target_arch = "wasm32"), feature = "peak"))]
{
let bitrate_cfg = crate::can::bitrate::BitrateBuilder::new::<crate::peak::Peak>()
.bitrate(500_000)
Expand Down
Loading
Loading