-
Notifications
You must be signed in to change notification settings - Fork 37
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Support webusb as a backend #99
Open
Yatekii
wants to merge
29
commits into
kevinmehall:main
Choose a base branch
from
Yatekii:task/webusb
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+1,355
−304
Open
Changes from 7 commits
Commits
Show all changes
29 commits
Select commit
Hold shift + click to select a range
5296d4b
WIP: Webusb
Yatekii bcb2753
Add webusb p1
Yatekii 9508eed
Basic webusb backend works for flashing with probe-rs
Yatekii f6a6bea
Clean up some code
Yatekii 2fe07ed
Remove unnecessary methods in webusb
Yatekii a15b7b7
Fix device enumeration for WebUSB
Yatekii 45d0d8e
Some general clean up
Yatekii 154bacc
Make x86 build again
Yatekii ed04a4e
Fix more build errors
Yatekii 53875b3
Make regular platforms build again
Yatekii f0ebbc4
Fix more issues
Yatekii cc8af41
Fix more issues
Yatekii fe04061
Fix doctests
Yatekii a8b6a7d
Fix doctests more
Yatekii d251863
Fix doctests
Yatekii 060e972
Fixes for windows
Yatekii 7a054a6
Clean up some more
Yatekii 784ca18
Clean up transfer functions
Yatekii 92c4370
Implement clear halt and set alt
Yatekii f0909ca
Implement remaining functions but hotplugging
Yatekii 1ab30cc
Implement hotplug events
Yatekii 87da2ca
Run against wasm toolchain in CI
Yatekii 59a7b3e
Make macOS build again
Yatekii 0b3cd02
Fix compilation
Yatekii fe01b06
Remove unsafe impl of Send/Sync
Yatekii 09505c4
Fix hotplug waker
Yatekii f969bea
Move webusb code to the webusb module
Yatekii dde4fed
Remove unwraps in enumeration
Yatekii d7b95cb
Make WebUSB work on WebWorkers
Yatekii File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
[target.wasm32-unknown-unknown] | ||
rustflags = "--cfg=web_sys_unstable_apis" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
{ | ||
"rust-analyzer.check.targets": ["wasm32-unknown-unknown"], | ||
"rust-analyzer.cargo.target": "wasm32-unknown-unknown", | ||
"rust-analyzer.showUnlinkedFileNotification": false | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,37 +1,69 @@ | ||
[package] | ||
name = "nusb" | ||
version = "0.1.10" | ||
version = "0.1.12" | ||
description = "Cross-platform low-level access to USB devices in pure Rust" | ||
categories = ["hardware-support"] | ||
keywords = ["usb", "hardware"] | ||
authors = ["Kevin Mehall <[email protected]>"] | ||
edition = "2021" | ||
license = "Apache-2.0 OR MIT" | ||
repository = "https://github.com/kevinmehall/nusb" | ||
rust-version = "1.76" # keep in sync with .github/workflows/rust.yml | ||
rust-version = "1.80" # keep in sync with .github/workflows/rust.yml | ||
|
||
[dependencies] | ||
atomic-waker = "1.1.2" | ||
futures-core = "0.3.29" | ||
log = "0.4.20" | ||
once_cell = "1.18.0" | ||
slab = "0.4.9" | ||
tracing = "0.1" | ||
|
||
[dev-dependencies] | ||
env_logger = "0.10.0" | ||
futures-lite = "1.13.0" | ||
pollster = { version = "0.4", features = ["macro"] } | ||
|
||
[target.'cfg(any(target_os="linux", target_os="android"))'.dependencies] | ||
rustix = { version = "0.38.17", features = ["fs", "event", "net"] } | ||
libc = "0.2.155" | ||
# [target.'cfg(all(any(target_os="linux", target_os="android"), not(target_arch="wasm32")))'.dependencies] | ||
# rustix = { version = "0.38.17", features = ["fs", "event", "net"] } | ||
# libc = "0.2.155" | ||
|
||
[target.'cfg(target_os="windows")'.dependencies] | ||
windows-sys = { version = "0.48.0", features = ["Win32_Devices_Usb", "Win32_Devices_DeviceAndDriverInstallation", "Win32_Foundation", "Win32_Devices_Properties", "Win32_Storage_FileSystem", "Win32_Security", "Win32_System_IO", "Win32_System_Registry", "Win32_System_Com"] } | ||
windows-sys = { version = "0.48.0", features = [ | ||
"Win32_Devices_Usb", | ||
"Win32_Devices_DeviceAndDriverInstallation", | ||
"Win32_Foundation", | ||
"Win32_Devices_Properties", | ||
"Win32_Storage_FileSystem", | ||
"Win32_Security", | ||
"Win32_System_IO", | ||
"Win32_System_Registry", | ||
"Win32_System_Com", | ||
] } | ||
|
||
[target.'cfg(target_os="macos")'.dependencies] | ||
core-foundation = "0.9.3" | ||
core-foundation-sys = "0.8.4" | ||
io-kit-sys = "0.4.0" | ||
|
||
[target.'cfg(target_arch="wasm32")'.dependencies] | ||
web-sys = { version = "*", features = [ | ||
"Document", | ||
"Window", | ||
"Navigator", | ||
"Usb", | ||
"UsbDevice", | ||
"UsbDeviceRequestOptions", | ||
"UsbConfiguration", | ||
"UsbInterface", | ||
"UsbAlternateInterface", | ||
"UsbControlTransferParameters", | ||
"UsbRequestType", | ||
"UsbRecipient", | ||
"UsbInTransferResult", | ||
"UsbTransferStatus", | ||
"UsbOutTransferResult", | ||
] } | ||
wasm-bindgen-futures = { version = "*" } | ||
|
||
[lints.rust] | ||
unexpected_cfgs = { level = "warn", check-cfg = ['cfg(fuzzing)'] } |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,61 +1,70 @@ | ||
use futures_lite::future::block_on; | ||
use nusb::transfer::{ControlIn, ControlOut, ControlType, Recipient}; | ||
|
||
fn main() { | ||
#[pollster::main] | ||
async fn main() { | ||
env_logger::init(); | ||
let di = nusb::list_devices() | ||
.await | ||
.unwrap() | ||
.find(|d| d.vendor_id() == 0x59e3 && d.product_id() == 0x0a23) | ||
.expect("device should be connected"); | ||
|
||
println!("Device info: {di:?}"); | ||
|
||
let device = di.open().unwrap(); | ||
let device = di.open().await.unwrap(); | ||
|
||
// Linux can make control transfers without claiming an interface | ||
#[cfg(any(target_os = "linux", target_os = "macos"))] | ||
{ | ||
let result = block_on(device.control_out(ControlOut { | ||
let result = device | ||
.control_out(ControlOut { | ||
control_type: ControlType::Vendor, | ||
recipient: Recipient::Device, | ||
request: 0x81, | ||
value: 0x9999, | ||
index: 0x9999, | ||
data: &[1, 2, 3, 4], | ||
}) | ||
.await; | ||
println!("{result:?}"); | ||
|
||
let result = device | ||
.control_in(ControlIn { | ||
control_type: ControlType::Vendor, | ||
recipient: Recipient::Device, | ||
request: 0x81, | ||
value: 0x9999, | ||
index: 0x9999, | ||
length: 256, | ||
}) | ||
.await; | ||
println!("{result:?}"); | ||
} | ||
|
||
// but we also provide an API on the `Interface` to support Windows | ||
let interface = device.claim_interface(0).await.unwrap(); | ||
|
||
let result = interface | ||
.control_out(ControlOut { | ||
control_type: ControlType::Vendor, | ||
recipient: Recipient::Device, | ||
request: 0x81, | ||
value: 0x9999, | ||
index: 0x9999, | ||
data: &[1, 2, 3, 4], | ||
})); | ||
println!("{result:?}"); | ||
}) | ||
.await; | ||
println!("{result:?}"); | ||
|
||
let result = block_on(device.control_in(ControlIn { | ||
let result = interface | ||
.control_in(ControlIn { | ||
control_type: ControlType::Vendor, | ||
recipient: Recipient::Device, | ||
request: 0x81, | ||
value: 0x9999, | ||
index: 0x9999, | ||
length: 256, | ||
})); | ||
println!("{result:?}"); | ||
} | ||
|
||
// but we also provide an API on the `Interface` to support Windows | ||
let interface = device.claim_interface(0).unwrap(); | ||
|
||
let result = block_on(interface.control_out(ControlOut { | ||
control_type: ControlType::Vendor, | ||
recipient: Recipient::Device, | ||
request: 0x81, | ||
value: 0x9999, | ||
index: 0x9999, | ||
data: &[1, 2, 3, 4], | ||
})); | ||
println!("{result:?}"); | ||
|
||
let result = block_on(interface.control_in(ControlIn { | ||
control_type: ControlType::Vendor, | ||
recipient: Recipient::Device, | ||
request: 0x81, | ||
value: 0x9999, | ||
index: 0x9999, | ||
length: 256, | ||
})); | ||
}) | ||
.await; | ||
println!("{result:?}"); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Even with the conditional it fails to compile. Not sure how to do this one.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ok consider me confused. Having this last time I tried made it break. It works fine now.