#![allow(async_fn_in_trait)]
use thiserror::Error;
pub trait UsbDeviceInfo {
type Device;
async fn open(self) -> Result<Self::Device, Error>;
async fn product_id(&self) -> u16;
async fn vendor_id(&self) -> u16;
async fn class(&self) -> u8;
async fn subclass(&self) -> u8;
async fn manufacturer_string(&self) -> Option<String>;
async fn product_string(&self) -> Option<String>;
}
pub trait UsbDevice {
type Interface;
async fn open_interface(&self, number: u8) -> Result<Self::Interface, Error>;
async fn detach_and_open_interface(&self, number: u8) -> Result<Self::Interface, Error>;
async fn reset(&self) -> Result<(), Error>;
async fn forget(&self) -> Result<(), Error>;
async fn product_id(&self) -> u16;
async fn vendor_id(&self) -> u16;
async fn class(&self) -> u8;
async fn subclass(&self) -> u8;
async fn manufacturer_string(&self) -> Option<String>;
async fn product_string(&self) -> Option<String>;
}
pub trait UsbInterface<'a> {
async fn control_in(&self, data: ControlIn) -> Result<Vec<u8>, Error>;
async fn control_out(&self, data: ControlOut<'a>) -> Result<usize, Error>;
async fn bulk_in(&self, endpoint: u8, length: usize) -> Result<Vec<u8>, Error>;
async fn bulk_out(&self, endpoint: u8, data: &[u8]) -> Result<usize, Error>;
}
#[derive(Error, Debug, PartialEq, Eq, PartialOrd, Ord)]
pub enum Error {
#[error("device not found")]
DeviceNotFound,
#[error("device transfer failed")]
TransferError,
#[error("device communication failed")]
CommunicationError(String),
#[error("device disconnected")]
Disconnected,
#[error("device no longer valid")]
Invalid,
}
pub enum ControlType {
Standard = 0,
Class = 1,
Vendor = 2,
}
pub enum Recipient {
Device = 0,
Interface = 1,
Endpoint = 2,
Other = 3,
}
pub struct ControlIn {
pub control_type: ControlType,
pub recipient: Recipient,
pub request: u8,
pub value: u16,
pub index: u16,
pub length: u16,
}
pub struct ControlOut<'a> {
pub control_type: ControlType,
pub recipient: Recipient,
pub request: u8,
pub value: u16,
pub index: u16,
pub data: &'a [u8],
}