#![cfg(all(
feature = "hardware",
any(target_os = "linux", target_os = "macos", target_os = "windows")
))]
use super::registry;
use crate::error::{Result, ZeptoError};
use nusb::MaybeFuture;
use serde::Serialize;
#[derive(Debug, Clone, Serialize)]
pub struct UsbDeviceInfo {
pub bus_id: String,
pub device_address: u8,
pub vid: u16,
pub pid: u16,
pub product_string: Option<String>,
pub board_name: Option<String>,
pub architecture: Option<String>,
}
pub fn list_usb_devices() -> Result<Vec<UsbDeviceInfo>> {
let mut devices = Vec::new();
let iter = nusb::list_devices()
.wait()
.map_err(|e| ZeptoError::Tool(format!("USB enumeration failed: {e}")))?;
for dev in iter {
let vid = dev.vendor_id();
let pid = dev.product_id();
let board = registry::lookup_board(vid, pid);
devices.push(UsbDeviceInfo {
bus_id: dev.bus_id().to_string(),
device_address: dev.device_address(),
vid,
pid,
product_string: dev.product_string().map(String::from),
board_name: board.map(|b| b.name.to_string()),
architecture: board.and_then(|b| b.architecture.map(String::from)),
});
}
Ok(devices)
}