#[cfg(not(target_arch = "wasm32"))]
use core::future::Future;
#[cfg(not(target_arch = "wasm32"))]
use crate::{DeviceInfo, HidError, HidResult};
#[cfg(not(target_arch = "wasm32"))]
pub trait HidBackend: Sized + Send + Sync {
type Device: HidDeviceBackend;
fn new() -> HidResult<Self>;
fn enumerate(&self, vendor_id: u16, product_id: u16) -> HidResult<Vec<DeviceInfo>>;
fn open_path(&self, path: &str) -> HidResult<Self::Device>;
fn open(
&self,
vendor_id: u16,
product_id: u16,
serial: Option<&str>,
) -> HidResult<Self::Device> {
let info = self
.enumerate(vendor_id, product_id)?
.into_iter()
.find(|info| match serial {
Some(serial) => info.serial_number.as_deref() == Some(serial),
None => true,
})
.ok_or(HidError::DeviceNotFound)?;
self.open_path(&info.path)
}
}
#[cfg(not(target_arch = "wasm32"))]
pub trait HidDeviceBackend: Send + Sync {
type Read<'a>: Future<Output = HidResult<usize>> + Send + Unpin + 'a
where
Self: 'a;
fn write(&self, data: &[u8]) -> HidResult<usize>;
fn read_async<'a>(&'a self, buf: &'a mut [u8]) -> Self::Read<'a>;
fn send_feature_report(&self, data: &[u8]) -> HidResult<()>;
fn get_feature_report(&self, buf: &mut [u8]) -> HidResult<usize>;
fn get_input_report(&self, buf: &mut [u8]) -> HidResult<usize>;
fn get_manufacturer_string(&self) -> HidResult<Option<String>>;
fn get_product_string(&self) -> HidResult<Option<String>>;
fn get_serial_number_string(&self) -> HidResult<Option<String>>;
fn get_indexed_string(&self, index: u32) -> HidResult<Option<String>>;
fn get_report_descriptor(&self, buf: &mut [u8]) -> HidResult<usize>;
fn get_device_info(&self) -> HidResult<DeviceInfo>;
}
#[cfg(any(
target_os = "macos",
all(feature = "nusb", any(target_os = "linux", target_os = "windows"))
))]
pub(crate) fn payload_after_report_id(data: &[u8]) -> &[u8] {
if data.first() == Some(&0) {
&data[1..]
} else {
data
}
}
#[cfg(any(
target_os = "macos",
all(feature = "nusb", any(target_os = "linux", target_os = "windows"))
))]
pub(crate) mod queue;
#[cfg(target_arch = "wasm32")]
pub(crate) mod webhid;
#[cfg(all(
feature = "nusb",
any(target_os = "linux", target_os = "macos", target_os = "windows")
))]
pub(crate) mod nusb;
#[cfg(target_os = "linux")]
pub(crate) mod reactor;
#[cfg(target_os = "linux")]
pub(crate) mod hidraw;
#[cfg(target_os = "windows")]
pub(crate) mod windows;
#[cfg(target_os = "macos")]
pub(crate) mod macos;
#[cfg(target_os = "linux")]
pub use hidraw::{HidrawApi as Native, HidrawDevice as NativeDevice};
#[cfg(target_os = "macos")]
pub use macos::{MacApi as Native, MacDevice as NativeDevice};
#[cfg(target_os = "windows")]
pub use windows::{WinApi as Native, WinDevice as NativeDevice};
#[cfg(all(
feature = "nusb",
any(target_os = "linux", target_os = "macos", target_os = "windows")
))]
pub use nusb::{NusbApi as Nusb, NusbDevice};