#[allow(unused_imports)]
use crate::error::Result;
#[allow(unused_imports)]
use moteus_protocol::CanFdFrame;
#[allow(unused_imports)]
use std::time::Duration;
#[allow(unused_imports)]
use crate::transport::transaction::Request;
#[cfg(target_os = "linux")]
macro_rules! forward_transport {
($t:ty) => {
impl $crate::transport::Transport for $t {
fn cycle(&mut self, requests: &mut [Request]) -> Result<()> {
self.0.cycle(requests)
}
fn write(&mut self, frame: &CanFdFrame) -> Result<()> {
self.0.write(frame)
}
fn read(&mut self, channel: Option<usize>) -> Result<Option<CanFdFrame>> {
self.0.read(channel)
}
fn flush_read(&mut self, channel: Option<usize>) -> Result<()> {
self.0.flush_read(channel)
}
fn set_timeout(&mut self, timeout: Duration) {
self.0.set_timeout(timeout)
}
fn timeout(&self) -> Duration {
self.0.timeout()
}
}
};
}
#[cfg(feature = "tokio")]
macro_rules! forward_async_transport {
($t:ty) => {
impl $crate::transport::async_transport::AsyncTransport for $t {
fn cycle<'a>(
&'a mut self,
requests: &'a mut [Request],
) -> $crate::transport::async_transport::BoxFuture<'a, Result<()>> {
Box::pin(self.0.cycle(requests))
}
fn write<'a>(
&'a mut self,
frame: &'a CanFdFrame,
) -> $crate::transport::async_transport::BoxFuture<'a, Result<()>> {
Box::pin(self.0.write(frame))
}
fn read(
&mut self,
channel: Option<usize>,
) -> $crate::transport::async_transport::BoxFuture<'_, Result<Option<CanFdFrame>>> {
Box::pin(self.0.read(channel))
}
fn flush_read(
&mut self,
channel: Option<usize>,
) -> $crate::transport::async_transport::BoxFuture<'_, Result<()>> {
Box::pin(self.0.flush_read(channel))
}
}
};
}
#[cfg(target_os = "linux")]
pub struct Fdcanusb(crate::transport::Router);
#[cfg(target_os = "linux")]
impl Fdcanusb {
pub fn open(path: &str) -> Result<Self> {
let device = crate::transport::fdcanusb::FdcanusbDevice::new(path)?;
Ok(Self(crate::transport::Router::from_device(device)))
}
pub fn with_options(path: &str, timeout: Duration, disable_brs: bool) -> Result<Self> {
let device =
crate::transport::fdcanusb::FdcanusbDevice::with_options(path, timeout, disable_brs)?;
Ok(Self(crate::transport::Router::from_device(device)))
}
}
#[cfg(target_os = "linux")]
forward_transport!(Fdcanusb);
#[cfg(target_os = "linux")]
pub struct SocketCan(crate::transport::Router);
#[cfg(target_os = "linux")]
impl SocketCan {
pub fn open(interface: &str) -> Result<Self> {
let device = crate::transport::socketcan::SocketCanDevice::new(interface)?;
Ok(Self(crate::transport::Router::from_device(device)))
}
pub fn with_options(interface: &str, timeout: Duration, disable_brs: bool) -> Result<Self> {
let device = crate::transport::socketcan::SocketCanDevice::with_options(
interface,
timeout,
disable_brs,
)?;
Ok(Self(crate::transport::Router::from_device(device)))
}
}
#[cfg(target_os = "linux")]
forward_transport!(SocketCan);
#[cfg(feature = "tokio")]
pub struct AsyncFdcanusb(crate::transport::async_transport::AsyncRouter);
#[cfg(feature = "tokio")]
impl AsyncFdcanusb {
pub async fn open(path: &str) -> Result<Self> {
let device = crate::transport::async_fdcanusb::AsyncFdcanusbDevice::open(path).await?;
Ok(Self(
crate::transport::async_transport::AsyncRouter::from_device(device),
))
}
pub async fn open_with_brs(path: &str, disable_brs: bool) -> Result<Self> {
let device =
crate::transport::async_fdcanusb::AsyncFdcanusbDevice::open_with_brs(path, disable_brs)
.await?;
Ok(Self(
crate::transport::async_transport::AsyncRouter::from_device(device),
))
}
}
#[cfg(feature = "tokio")]
forward_async_transport!(AsyncFdcanusb);
#[cfg(all(feature = "tokio", target_os = "linux"))]
pub struct AsyncSocketCan(crate::transport::async_transport::AsyncRouter);
#[cfg(all(feature = "tokio", target_os = "linux"))]
impl AsyncSocketCan {
pub async fn open(interface: &str) -> Result<Self> {
let device =
crate::transport::async_socketcan::AsyncSocketCanDevice::new(interface).await?;
Ok(Self(
crate::transport::async_transport::AsyncRouter::from_device(device),
))
}
}
#[cfg(all(feature = "tokio", target_os = "linux"))]
forward_async_transport!(AsyncSocketCan);