#[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(crate) 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(crate) trait HidDeviceBackend: Send + Sync {
fn write(&self, data: &[u8]) -> HidResult<usize>;
fn read_async<'a>(
&'a self,
buf: &'a mut [u8],
) -> impl Future<Output = HidResult<usize>> + Send + '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(all(
not(target_arch = "wasm32"),
any(feature = "nusb", target_os = "macos")
))]
pub(crate) fn payload_after_report_id(data: &[u8]) -> &[u8] {
if data.first() == Some(&0) {
&data[1..]
} else {
data
}
}
#[cfg(all(
not(target_arch = "wasm32"),
any(feature = "nusb", target_os = "macos")
))]
pub(crate) mod queue;
#[cfg(target_arch = "wasm32")]
pub(crate) mod webhid;
#[cfg(all(feature = "nusb", not(target_arch = "wasm32")))]
pub(crate) mod nusb;
#[cfg(all(feature = "nusb", not(target_arch = "wasm32")))]
pub(crate) use nusb::{NusbApi as PlatformApi, NusbDevice as PlatformDevice};
#[cfg(all(not(feature = "nusb"), target_os = "linux"))]
pub(crate) mod reactor;
#[cfg(all(not(feature = "nusb"), target_os = "linux"))]
pub(crate) mod hidraw;
#[cfg(all(not(feature = "nusb"), target_os = "linux"))]
pub(crate) use hidraw::{HidrawApi as PlatformApi, HidrawDevice as PlatformDevice};
#[cfg(all(not(feature = "nusb"), target_os = "windows"))]
pub(crate) mod windows;
#[cfg(all(not(feature = "nusb"), target_os = "windows"))]
pub(crate) use windows::{WinApi as PlatformApi, WinDevice as PlatformDevice};
#[cfg(all(not(feature = "nusb"), target_os = "macos"))]
pub(crate) mod macos;
#[cfg(all(not(feature = "nusb"), target_os = "macos"))]
pub(crate) use macos::{MacApi as PlatformApi, MacDevice as PlatformDevice};