mod io;
pub use io::{UsbSink, UsbSource, UsbTransport};
use crate::error::{Error, Result};
use nusb::descriptors::TransferType;
use nusb::transfer::{Bulk, Direction, In, Out};
use nusb::{DeviceInfo, ErrorKind};
pub const ADB_CLASS: u8 = 0xFF;
pub const ADB_SUBCLASS: u8 = 0x42;
pub const ADB_PROTOCOL: u8 = 0x01;
#[derive(Debug, Clone)]
pub struct UsbDeviceInfo {
info: DeviceInfo,
interface_number: u8,
}
impl UsbDeviceInfo {
pub fn from_parts(info: DeviceInfo, interface_number: u8) -> Self {
Self {
info,
interface_number,
}
}
pub fn serial(&self) -> Option<&str> {
self.info.serial_number()
}
pub fn product(&self) -> Option<&str> {
self.info.product_string()
}
pub fn manufacturer(&self) -> Option<&str> {
self.info.manufacturer_string()
}
pub fn vendor_id(&self) -> u16 {
self.info.vendor_id()
}
pub fn product_id(&self) -> u16 {
self.info.product_id()
}
pub fn interface_number(&self) -> u8 {
self.interface_number
}
pub fn id(&self) -> nusb::DeviceId {
self.info.id()
}
pub fn nusb_info(&self) -> &DeviceInfo {
&self.info
}
}
pub fn adb_interface(info: &DeviceInfo) -> Option<u8> {
info.interfaces()
.find(|i| {
i.class() == ADB_CLASS && i.subclass() == ADB_SUBCLASS && i.protocol() == ADB_PROTOCOL
})
.map(nusb::InterfaceInfo::interface_number)
}
fn map_usb(e: &nusb::Error) -> Error {
match e.kind() {
ErrorKind::Busy | ErrorKind::PermissionDenied => Error::ClaimFailed(e.to_string()),
ErrorKind::Disconnected => Error::Disconnected,
_ => Error::Usb(e.to_string()),
}
}
pub async fn list() -> Result<Vec<UsbDeviceInfo>> {
let devices = nusb::list_devices().await.map_err(|e| map_usb(&e))?;
Ok(devices
.filter_map(|info| {
adb_interface(&info).map(|interface_number| UsbDeviceInfo {
info,
interface_number,
})
})
.collect())
}
pub async fn find(serial: Option<&str>) -> Result<UsbDeviceInfo> {
let mut devices = list().await?;
match serial {
Some(wanted) => devices
.into_iter()
.find(|d| d.serial() == Some(wanted))
.ok_or_else(|| Error::NoDevice(Some(format!("serial {wanted}")))),
None => match devices.len() {
0 => Err(Error::NoDevice(None)),
1 => devices.pop().ok_or(Error::NoDevice(None)),
n => Err(Error::NoDevice(Some(format!(
"{n} devices attached, pass a serial"
)))),
},
}
}
pub async fn open(device: &UsbDeviceInfo) -> Result<UsbTransport> {
let handle = device.info.open().await.map_err(|e| map_usb(&e))?;
let interface = handle
.detach_and_claim_interface(device.interface_number)
.await
.map_err(|e| map_usb(&e))?;
let descriptor = interface
.descriptor()
.ok_or_else(|| Error::Usb("interface has no active descriptor".into()))?;
let mut in_addr = None;
let mut out_addr = None;
for ep in descriptor.endpoints() {
if ep.transfer_type() != TransferType::Bulk {
continue;
}
match ep.direction() {
Direction::In => in_addr.get_or_insert(ep.address()),
Direction::Out => out_addr.get_or_insert(ep.address()),
};
}
let (in_addr, out_addr) = in_addr
.zip(out_addr)
.ok_or_else(|| Error::Usb("ADB interface lacks bulk IN/OUT endpoints".into()))?;
let bulk_in = interface
.endpoint::<Bulk, In>(in_addr)
.map_err(|e| map_usb(&e))?;
let bulk_out = interface
.endpoint::<Bulk, Out>(out_addr)
.map_err(|e| map_usb(&e))?;
Ok(UsbTransport::new(handle, interface, bulk_in, bulk_out))
}