use std::time::Duration;
use tokio_serial::{SerialPortType, available_ports};
use tracing::info;
pub const USB_VID: u16 = 0x1209;
pub const USB_PID: u16 = 0x5741;
const BRIDGE_VID_PIDS: &[(u16, u16)] = &[
(0x10C4, 0xEA60), (0x1A86, 0x55D4), (0x1A86, 0x7522), (0x1A86, 0x7523), (0x0403, 0x6001), ];
const USB_SETTLE: Duration = Duration::from_millis(300);
const POLL_INTERVAL: Duration = Duration::from_millis(500);
pub fn find_port() -> Option<String> {
let ports = available_ports().ok()?;
if let Some(port) = ports.iter().find(|p| {
matches!(
&p.port_type,
SerialPortType::UsbPort(info) if info.vid == USB_VID && info.pid == USB_PID
)
}) {
return Some(port.port_name.clone());
}
ports
.into_iter()
.find(|p| {
matches!(
&p.port_type,
SerialPortType::UsbPort(info) if BRIDGE_VID_PIDS.contains(&(info.vid, info.pid))
)
})
.map(|p| p.port_name)
}
pub async fn wait_for_device() -> String {
info!("waiting for DongLoRa device...");
loop {
if let Some(port) = find_port() {
info!("found device at {port}");
tokio::time::sleep(USB_SETTLE).await;
return port;
}
tokio::time::sleep(POLL_INTERVAL).await;
}
}