use crate::tio::{proto::DeviceRoute, proxy};
use std::time::Duration;
#[derive(Debug, Clone)]
pub enum PortInterface {
FTDI,
STM32,
Unknown(u16, u16),
}
#[derive(Debug, Clone)]
pub struct DiscoveredDevice {
pub url: String,
pub interface: PortInterface,
}
pub fn enumerate_serial(include_unknown: bool) -> Vec<DiscoveredDevice> {
let mut ports: Vec<DiscoveredDevice> = Vec::new();
if let Ok(avail_ports) = serialport::available_ports() {
for p in avail_ports.iter() {
if let serialport::SerialPortType::UsbPort(info) = &p.port_type {
let interface = match (info.vid, info.pid) {
(0x0403, 0x6015) => PortInterface::FTDI,
(0x0483, 0x5740) => PortInterface::STM32,
(vid, pid) => {
if !include_unknown {
continue;
}
PortInterface::Unknown(vid, pid)
}
};
#[cfg(target_os = "macos")]
if p.port_name.starts_with("/dev/tty.") && !include_unknown {
continue;
}
ports.push(DiscoveredDevice {
url: format!("serial://{}", p.port_name),
interface,
});
}
}
}
ports
}
pub fn query_name(url: &str, timeout: Duration) -> Option<String> {
let interface = proxy::Interface::new_proxy(url, Some(timeout), None);
let port = interface
.new_port(Some(timeout), DeviceRoute::root(), 0, false, false)
.ok()?;
port.rpc("dev.name", ()).ok()
}