Skip to content

Commit

Permalink
Added get_device_list for wasm, changed Descriptor to DeviceInfo
Browse files Browse the repository at this point in the history
  • Loading branch information
G2-Games committed Aug 14, 2024
1 parent fea600b commit f1fa9dd
Show file tree
Hide file tree
Showing 5 changed files with 35 additions and 42 deletions.
9 changes: 3 additions & 6 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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)'] }
18 changes: 9 additions & 9 deletions src/backend/native.rs
Original file line number Diff line number Diff line change
@@ -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,
}

Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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();
Expand Down Expand Up @@ -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> {
Expand Down
24 changes: 11 additions & 13 deletions src/backend/wasm.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
}

Expand Down Expand Up @@ -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();
Expand Down Expand Up @@ -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 });
}
}

Expand Down Expand Up @@ -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();
Expand Down Expand Up @@ -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 });
}
}

Expand Down Expand Up @@ -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> {
Expand All @@ -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 {
Expand Down
23 changes: 11 additions & 12 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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;
}
Expand All @@ -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
Expand All @@ -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
Expand All @@ -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
Expand Down
3 changes: 1 addition & 2 deletions src/usb.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

use thiserror::Error;

pub trait UsbDescriptor {
pub trait UsbDeviceInfo {
/// A unique USB Device
type Device;

Expand Down Expand Up @@ -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>;
Expand Down

0 comments on commit f1fa9dd

Please sign in to comment.