mirror of
https://github.com/G2-Games/cross-usb.git
synced 2025-04-19 05:12:53 -05:00
Added macro for DeviceFilter
This commit is contained in:
parent
2ea7fd68c8
commit
847367c9a9
3 changed files with 75 additions and 65 deletions
|
@ -9,6 +9,33 @@ pub struct UsbInterface {
|
|||
interface: nusb::Interface,
|
||||
}
|
||||
|
||||
#[derive(PartialEq, Clone, Default)]
|
||||
pub struct DeviceFilter {
|
||||
pub vendor_id: Option<u16>,
|
||||
pub product_id: Option<u16>,
|
||||
pub class: Option<u8>,
|
||||
pub subclass: Option<u8>,
|
||||
pub protocol: Option<u8>,
|
||||
}
|
||||
|
||||
impl DeviceFilter {
|
||||
pub fn new(
|
||||
vendor_id: Option<u16>,
|
||||
product_id: Option<u16>,
|
||||
class: Option<u8>,
|
||||
subclass: Option<u8>,
|
||||
protocol: Option<u8>,
|
||||
) -> Self {
|
||||
Self {
|
||||
vendor_id,
|
||||
product_id,
|
||||
class,
|
||||
subclass,
|
||||
protocol,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
pub async fn get_device(vendor_id: u16, product_id: u16) -> Result<UsbDevice, UsbError> {
|
||||
let devices = nusb::list_devices().unwrap();
|
||||
|
||||
|
@ -36,35 +63,6 @@ pub async fn get_device(vendor_id: u16, product_id: u16) -> Result<UsbDevice, Us
|
|||
})
|
||||
}
|
||||
|
||||
#[derive(PartialEq, Clone)]
|
||||
pub struct DeviceFilter {
|
||||
pub vendor_id: Option<u16>,
|
||||
pub product_id: Option<u16>,
|
||||
pub class: Option<u8>,
|
||||
pub subclass: Option<u8>,
|
||||
pub protocol: Option<u8>,
|
||||
serial_number: Option<String>,
|
||||
}
|
||||
|
||||
impl DeviceFilter {
|
||||
pub fn serial_number(&self) -> &Option<String> {
|
||||
&self.serial_number
|
||||
}
|
||||
}
|
||||
|
||||
impl Default for DeviceFilter {
|
||||
fn default() -> Self {
|
||||
Self {
|
||||
vendor_id: None,
|
||||
product_id: None,
|
||||
class: None,
|
||||
subclass: None,
|
||||
protocol: None,
|
||||
serial_number: None,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
pub async fn get_device_filter(
|
||||
device_filter: Vec<DeviceFilter>,
|
||||
) -> Result<UsbDevice, UsbError> {
|
||||
|
@ -97,10 +95,6 @@ pub async fn get_device_filter(
|
|||
result = info.protocol.unwrap() == prelim_dev_inf.protocol();
|
||||
}
|
||||
|
||||
if info.serial_number().is_some() && prelim_dev_inf.serial_number().is_some() {
|
||||
result = info.serial_number().as_ref().unwrap() == &prelim_dev_inf.serial_number().unwrap().to_owned();
|
||||
}
|
||||
|
||||
result
|
||||
}
|
||||
).is_some() {
|
||||
|
|
|
@ -22,31 +22,29 @@ pub struct UsbInterface {
|
|||
}
|
||||
|
||||
#[wasm_bindgen]
|
||||
#[derive(PartialEq, Clone)]
|
||||
#[derive(PartialEq, Clone, Default)]
|
||||
pub struct DeviceFilter {
|
||||
pub vendor_id: Option<u16>,
|
||||
pub product_id: Option<u16>,
|
||||
pub class: Option<u8>,
|
||||
pub subclass: Option<u8>,
|
||||
pub protocol: Option<u8>,
|
||||
serial_number: Option<String>,
|
||||
}
|
||||
|
||||
impl DeviceFilter {
|
||||
pub fn serial_number(&self) -> &Option<String> {
|
||||
&self.serial_number
|
||||
}
|
||||
}
|
||||
|
||||
impl Default for DeviceFilter {
|
||||
fn default() -> Self {
|
||||
pub fn new(
|
||||
vendor_id: Option<u16>,
|
||||
product_id: Option<u16>,
|
||||
class: Option<u8>,
|
||||
subclass: Option<u8>,
|
||||
protocol: Option<u8>,
|
||||
) -> Self {
|
||||
Self {
|
||||
vendor_id: None,
|
||||
product_id: None,
|
||||
class: None,
|
||||
subclass: None,
|
||||
protocol: None,
|
||||
serial_number: None,
|
||||
vendor_id,
|
||||
product_id,
|
||||
class,
|
||||
subclass,
|
||||
protocol,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -140,10 +138,6 @@ pub async fn get_device_filter(
|
|||
result = info.protocol.unwrap() == device.device_protocol();
|
||||
}
|
||||
|
||||
if info.serial_number().is_some() && device.serial_number().is_some() {
|
||||
result = info.serial_number().as_ref().unwrap() == &device.serial_number().unwrap().to_owned();
|
||||
}
|
||||
|
||||
result
|
||||
}
|
||||
).is_some() {
|
||||
|
@ -194,14 +188,6 @@ pub async fn get_device_filter(
|
|||
)
|
||||
.unwrap();
|
||||
}
|
||||
if let Some(serial) = filter.serial_number {
|
||||
js_sys::Reflect::set(
|
||||
&js_filter,
|
||||
&JsValue::from_str("serialNumber"),
|
||||
&JsValue::from(serial),
|
||||
)
|
||||
.unwrap();
|
||||
}
|
||||
arr.push(&js_filter);
|
||||
}
|
||||
|
||||
|
|
40
src/lib.rs
40
src/lib.rs
|
@ -48,8 +48,8 @@ pub use crate::context::UsbDevice;
|
|||
/// An implementation of a USB interface
|
||||
pub use crate::context::UsbInterface;
|
||||
|
||||
/// A single Device ID and Product ID pair to find when looking
|
||||
/// for new USB devices using [get_device_filter]
|
||||
/// Information about a USB device for finding it while trying
|
||||
/// to look for new USB devices using [get_device_filter]
|
||||
#[doc(inline)]
|
||||
pub use crate::context::DeviceFilter;
|
||||
|
||||
|
@ -62,11 +62,12 @@ pub use crate::context::get_device;
|
|||
/// ## Example
|
||||
/// ```no_run
|
||||
/// # tokio_test::block_on(async {
|
||||
/// use cross_usb::{get_device_filter, FilterTuple};
|
||||
/// use cross_usb::{get_device_filter, DeviceFilter, device_filter};
|
||||
///
|
||||
///
|
||||
/// let filter = vec![
|
||||
/// FilterTuple(0x054c, 0x00c9),
|
||||
/// FilterTuple(0x054c, 0x0186),
|
||||
/// device_filter!{vendor_id: 0x054c, product_id: 0x00c9},
|
||||
/// device_filter!{vendor_id: 0x054c},
|
||||
/// ];
|
||||
///
|
||||
/// let device = get_device_filter(filter).await.expect("Could not find device in list");
|
||||
|
@ -74,3 +75,32 @@ pub use crate::context::get_device;
|
|||
/// ```
|
||||
#[doc(inline)]
|
||||
pub use crate::context::get_device_filter;
|
||||
|
||||
/// Macro to create a device filter easily from data.
|
||||
///
|
||||
/// The only valid keys are fields of the [DeviceFilter] struct.
|
||||
/// You may use as many or as few of them as you need, the rest
|
||||
/// of the values will be filled with [None].
|
||||
///
|
||||
/// ## Usage
|
||||
/// ```
|
||||
/// use cross_usb::device_filter;
|
||||
///
|
||||
/// // Example with all fields filled
|
||||
/// device_filter!{
|
||||
/// vendor_id: 0x054c,
|
||||
/// product_id: 0x0186,
|
||||
/// class: 0xFF,
|
||||
/// subclass: 0x02,
|
||||
/// protocol: 0x15,
|
||||
/// };
|
||||
/// ```
|
||||
#[macro_export]
|
||||
macro_rules! device_filter {
|
||||
($($field:ident: $val:expr),+ $(,)?) => {
|
||||
cross_usb::DeviceFilter {
|
||||
$($field: Some($val),)*
|
||||
..cross_usb::DeviceFilter::default()
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue