use std::sync::Arc;
use can_transport::CanBus;
pub async fn open_bus(iface: &str) -> anyhow::Result<Arc<dyn CanBus>> {
if let Some(channel) = gs_usb_channel(iface) {
use can_transport::gs_usb::{GsUsbBus, GsUsbConfig};
log::info!("opening gs_usb backend channel {channel} (CAN-FD 1M/5M)");
let bus = GsUsbBus::open(GsUsbConfig::fd_1m_5m().with_channel(channel)).await?;
log::info!("gs_usb opened: {:?}", bus.capabilities());
Ok(Arc::new(bus))
} else {
use can_transport::socketcan::SocketCanBus;
log::info!("opening SocketCAN interface {iface}");
Ok(Arc::new(SocketCanBus::open(iface)?))
}
}
fn gs_usb_channel(iface: &str) -> Option<u16> {
let s = iface.trim().to_ascii_lowercase();
let rest = s.strip_prefix("gs_usb").or_else(|| s.strip_prefix("gsusb"))?;
let rest = rest.strip_prefix(':').unwrap_or(rest);
if rest.is_empty() {
Some(0)
} else {
rest.parse().ok()
}
}