mirror of
https://github.com/G2-Games/cross-usb.git
synced 2025-05-03 03:42:54 -05:00
Added get_device_list
for wasm, changed Descriptor
to DeviceInfo
This commit is contained in:
parent
fea600bff7
commit
f1fa9dd19d
5 changed files with 35 additions and 42 deletions
|
@ -48,14 +48,11 @@ features = [
|
|||
[target.'cfg(not(target_family = "wasm"))'.dependencies]
|
||||
nusb = "0.1"
|
||||
|
||||
[profile.release]
|
||||
lto = true
|
||||
strip = true
|
||||
opt-level = "z"
|
||||
codegen-units = 1
|
||||
|
||||
[package.metadata.wasm-pack.profile.dev.wasm-bindgen]
|
||||
dwarf-debug-info = true
|
||||
|
||||
[package.metadata.docs.rs]
|
||||
targets = ["x86_64-unknown-linux-gnu", "x86_64-pc-windows-msvc", "x86_64-apple-darwin", "aarch64-apple-darwin", "wasm32-unknown-unknown"]
|
||||
|
||||
[lints.rust]
|
||||
unexpected_cfgs = { level = "warn", check-cfg = ['cfg(web_sys_unstable_apis)'] }
|
||||
|
|
|
@ -1,15 +1,15 @@
|
|||
use crate::usb::{
|
||||
ControlIn, ControlOut, ControlType, UsbDescriptor, UsbDevice, UsbInterface, Recipient, UsbError,
|
||||
ControlIn, ControlOut, ControlType, UsbDeviceInfo, UsbDevice, UsbInterface, Recipient, UsbError,
|
||||
};
|
||||
|
||||
#[derive(Clone, Debug)]
|
||||
pub struct Descriptor {
|
||||
pub struct DeviceInfo {
|
||||
device_info: nusb::DeviceInfo,
|
||||
}
|
||||
|
||||
#[derive(Clone)]
|
||||
pub struct Device {
|
||||
device_info: Descriptor,
|
||||
device_info: DeviceInfo,
|
||||
device: nusb::Device,
|
||||
}
|
||||
|
||||
|
@ -60,7 +60,7 @@ impl DeviceFilter {
|
|||
|
||||
pub async fn get_device(
|
||||
device_filters: Vec<DeviceFilter>
|
||||
) -> Result<Descriptor, UsbError> {
|
||||
) -> Result<DeviceInfo, UsbError> {
|
||||
let devices = nusb::list_devices().unwrap();
|
||||
|
||||
let mut device_info = None;
|
||||
|
@ -101,12 +101,12 @@ pub async fn get_device(
|
|||
None => return Err(UsbError::DeviceNotFound),
|
||||
};
|
||||
|
||||
Ok(Descriptor { device_info })
|
||||
Ok(DeviceInfo { device_info })
|
||||
}
|
||||
|
||||
pub async fn get_device_list(
|
||||
device_filters: Vec<DeviceFilter>,
|
||||
) -> Result<impl Iterator<Item = Descriptor>, UsbError> {
|
||||
) -> Result<impl Iterator<Item = DeviceInfo>, UsbError> {
|
||||
let devices_info = nusb::list_devices().unwrap();
|
||||
|
||||
let mut devices = Vec::new();
|
||||
|
@ -145,15 +145,15 @@ pub async fn get_device_list(
|
|||
return Err(UsbError::DeviceNotFound);
|
||||
}
|
||||
|
||||
let devices_opened: Vec<Descriptor> = devices
|
||||
let devices_opened: Vec<DeviceInfo> = devices
|
||||
.into_iter()
|
||||
.map(|d| Descriptor { device_info: d })
|
||||
.map(|d| DeviceInfo { device_info: d })
|
||||
.collect();
|
||||
|
||||
Ok(devices_opened.into_iter())
|
||||
}
|
||||
|
||||
impl UsbDescriptor for Descriptor {
|
||||
impl UsbDeviceInfo for DeviceInfo {
|
||||
type Device = Device;
|
||||
|
||||
async fn open(self) -> Result<Self::Device, UsbError> {
|
||||
|
|
|
@ -10,12 +10,12 @@ use web_sys::{
|
|||
|
||||
// Crate stuff
|
||||
use crate::usb::{
|
||||
ControlIn, ControlOut, ControlType, UsbDescriptor, UsbDevice, UsbInterface, Recipient, UsbError,
|
||||
ControlIn, ControlOut, ControlType, UsbDeviceInfo, UsbDevice, UsbInterface, Recipient, UsbError,
|
||||
};
|
||||
|
||||
#[wasm_bindgen]
|
||||
#[derive(Debug)]
|
||||
pub struct Descriptor {
|
||||
pub struct DeviceInfo {
|
||||
device: WasmUsbDevice,
|
||||
}
|
||||
|
||||
|
@ -61,7 +61,7 @@ impl DeviceFilter {
|
|||
}
|
||||
|
||||
#[wasm_bindgen]
|
||||
pub async fn get_device(device_filter: Vec<DeviceFilter>) -> Result<Descriptor, js_sys::Error> {
|
||||
pub async fn get_device(device_filter: Vec<DeviceFilter>) -> Result<DeviceInfo, js_sys::Error> {
|
||||
let window = web_sys::window().unwrap();
|
||||
|
||||
let navigator = window.navigator();
|
||||
|
@ -102,7 +102,7 @@ pub async fn get_device(device_filter: Vec<DeviceFilter>) -> Result<Descriptor,
|
|||
result
|
||||
}) {
|
||||
let _open_promise = JsFuture::from(Promise::resolve(&device.open())).await?;
|
||||
return Ok(Descriptor { device });
|
||||
return Ok(DeviceInfo { device });
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -161,12 +161,11 @@ pub async fn get_device(device_filter: Vec<DeviceFilter>) -> Result<Descriptor,
|
|||
|
||||
let _open_promise = JsFuture::from(Promise::resolve(&device.open())).await?;
|
||||
|
||||
Ok(Descriptor { device })
|
||||
Ok(DeviceInfo { device })
|
||||
}
|
||||
|
||||
/*
|
||||
#[wasm_bindgen]
|
||||
pub async fn get_device_list(device_filter: Vec<DeviceFilter>) -> Result<Vec<UsbDescriptor>, js_sys::Error> {
|
||||
pub async fn get_device_list(device_filter: Vec<DeviceFilter>) -> Result<Vec<DeviceInfo>, js_sys::Error> {
|
||||
let window = web_sys::window().unwrap();
|
||||
|
||||
let navigator = window.navigator();
|
||||
|
@ -208,7 +207,7 @@ pub async fn get_device_list(device_filter: Vec<DeviceFilter>) -> Result<Vec<Usb
|
|||
result
|
||||
}) {
|
||||
let _open_promise = JsFuture::from(Promise::resolve(&device.open())).await?;
|
||||
devices.push(UsbDescriptor { device });
|
||||
devices.push(DeviceInfo { device });
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -267,13 +266,12 @@ pub async fn get_device_list(device_filter: Vec<DeviceFilter>) -> Result<Vec<Usb
|
|||
|
||||
let _open_promise = JsFuture::from(Promise::resolve(&device.open())).await?;
|
||||
|
||||
devices.push(UsbDescriptor { device });
|
||||
devices.push(DeviceInfo { device });
|
||||
|
||||
return Ok(devices);
|
||||
}
|
||||
*/
|
||||
|
||||
impl UsbDescriptor for Descriptor {
|
||||
impl UsbDeviceInfo for DeviceInfo {
|
||||
type Device = Device;
|
||||
|
||||
async fn open(self) -> Result<Self::Device, UsbError> {
|
||||
|
@ -283,11 +281,11 @@ impl UsbDescriptor for Descriptor {
|
|||
}
|
||||
|
||||
async fn product_id(&self) -> u16 {
|
||||
self.device.vendor_id()
|
||||
self.device.product_id()
|
||||
}
|
||||
|
||||
async fn vendor_id(&self) -> u16 {
|
||||
self.device.product_id()
|
||||
self.device.vendor_id()
|
||||
}
|
||||
|
||||
async fn class(&self) -> u8 {
|
||||
|
|
23
src/lib.rs
23
src/lib.rs
|
@ -9,16 +9,12 @@
|
|||
//!
|
||||
//! When an [`Interface`] is dropped, it is automatically released.
|
||||
//!
|
||||
//! ### CURRENT LIMITATIONS:
|
||||
//! ## CURRENT LIMITATIONS:
|
||||
//! * Hotplug support is not implemented. Waiting on [hotplug support in nusb](https://github.com/kevinmehall/nusb/pull/20).
|
||||
//!
|
||||
//! * Until [this pull request](https://github.com/rustwasm/wasm-bindgen/issues/3155)
|
||||
//! is merged into wasm bindgen, getting a list of USB devices is not possible on WASM
|
||||
//! targets. However, this isn't a huge deal as the user gets a list to select from anyway.
|
||||
//!
|
||||
//! * When compiling this crate on a WASM target, you must use either
|
||||
//! * When compiling this crate on a WASM target, you **must** use either
|
||||
//! `RUSTFLAGS=--cfg=web_sys_unstable_apis` or by passing the argument in a
|
||||
//! `.cargo/config.toml` file. Read more here: https://rustwasm.github.io/wasm-bindgen/web-sys/unstable-apis.html
|
||||
//! `.cargo/config.toml` file. Read more here: <https://rustwasm.github.io/wasm-bindgen/web-sys/unstable-apis.html>
|
||||
//!
|
||||
//! ## Example:
|
||||
//! ```no_run
|
||||
|
@ -63,7 +59,7 @@ pub mod usb;
|
|||
/// use cross_usb::prelude::*;
|
||||
/// ```
|
||||
pub mod prelude {
|
||||
pub use crate::usb::UsbDescriptor;
|
||||
pub use crate::usb::UsbDeviceInfo;
|
||||
pub use crate::usb::UsbDevice;
|
||||
pub use crate::usb::UsbInterface;
|
||||
}
|
||||
|
@ -79,9 +75,12 @@ mod context;
|
|||
mod context;
|
||||
|
||||
#[doc(inline)]
|
||||
/// A descriptor of a USB device, containing information about a device
|
||||
/// Information about a USB device, containing information about a device
|
||||
/// without claiming it
|
||||
pub use crate::context::Descriptor;
|
||||
///
|
||||
/// **Note:** On WASM targets, a device *must* be claimed in order to get
|
||||
/// information about it in normal circumstances.
|
||||
pub use crate::context::DeviceInfo;
|
||||
|
||||
#[doc(inline)]
|
||||
/// A USB device, you must open an [`Interface`] to perform transfers
|
||||
|
@ -96,7 +95,7 @@ pub use crate::context::Interface;
|
|||
#[doc(inline)]
|
||||
pub use crate::context::DeviceFilter;
|
||||
|
||||
/// Gets a single (the first found) device as a [`Descriptor`] from a list of VendorID
|
||||
/// Gets a single (the first found) device as a [`DeviceInfo`] from a list of VendorID
|
||||
/// and ProductIDs
|
||||
///
|
||||
/// ## Example
|
||||
|
@ -115,7 +114,7 @@ pub use crate::context::DeviceFilter;
|
|||
#[doc(inline)]
|
||||
pub use crate::context::get_device;
|
||||
|
||||
/// Gets a list of [`Descriptor`]s from a list of VendorID and ProductIDs
|
||||
/// Gets a list of [`DeviceInfo`]s from a list of VendorID and ProductIDs
|
||||
///
|
||||
/// ## Example
|
||||
/// ```no_run
|
||||
|
|
|
@ -4,7 +4,7 @@
|
|||
|
||||
use thiserror::Error;
|
||||
|
||||
pub trait UsbDescriptor {
|
||||
pub trait UsbDeviceInfo {
|
||||
/// A unique USB Device
|
||||
type Device;
|
||||
|
||||
|
@ -96,7 +96,6 @@ pub trait UsbInterface<'a> {
|
|||
async fn bulk_out(&self, endpoint: u8, data: &[u8]) -> Result<usize, UsbError>;
|
||||
|
||||
/* TODO: Figure out interrupt transfers on Web USB
|
||||
|
||||
/// A USB interrupt in transfer (device to host).
|
||||
/// Takes in an endpoint and a buffer to fill
|
||||
async fn interrupt_in(&self, endpoint: u8, length: usize) -> Result<Vec<u8>, UsbError>;
|
||||
|
|
Loading…
Reference in a new issue