#[cfg(target_os = "linux")]
mod imp {
use socketcan::{CanFrame, CanSocket, EmbeddedFrame, Id, Socket, StandardId};
pub struct CanBus {
sock: CanSocket,
}
impl CanBus {
pub fn open(iface: &str) -> std::io::Result<Self> {
let sock = CanSocket::open(iface)?;
Ok(Self { sock })
}
pub fn send_std(&self, id: u16, data: &[u8]) -> std::io::Result<()> {
let sid = StandardId::new(id).ok_or_else(|| {
std::io::Error::new(std::io::ErrorKind::InvalidInput, "id out of 11-bit range")
})?;
let frame = CanFrame::new(sid, data).ok_or_else(|| {
std::io::Error::new(std::io::ErrorKind::InvalidInput, "data too long")
})?;
self.sock.write_frame(&frame)?;
Ok(())
}
pub fn recv(&self) -> std::io::Result<(u32, Vec<u8>)> {
let frame = self.sock.read_frame()?;
let id = match frame.id() {
Id::Standard(s) => s.as_raw() as u32,
Id::Extended(e) => e.as_raw(),
};
Ok((id, frame.data().to_vec()))
}
}
}
#[cfg(target_os = "linux")]
pub use imp::CanBus;