diff --git a/examples/isotp.rs b/examples/isotp.rs index 5f43f9a..bc0eab4 100644 --- a/examples/isotp.rs +++ b/examples/isotp.rs @@ -12,15 +12,15 @@ async fn main() { let isotp = IsoTPAdapter::new(&adapter, config); - let response = isotp.recv(); - isotp.send(&[0x3e, 0x00]).await.unwrap(); - let response = response.await.unwrap(); - println!("RX: {}", hex::encode(response)); + // let response = isotp.recv(); + // isotp.send(&[0x3e, 0x00]).await.unwrap(); + // let response = response.await.unwrap(); + // println!("RX: {}", hex::encode(response)); - let response = isotp.recv(); - isotp.send(&[0x22, 0xf1, 0x81]).await.unwrap(); - let response = response.await.unwrap(); - println!("RX: {}", hex::encode(response)); + // let response = isotp.recv(); + // isotp.send(&[0x22, 0xf1, 0x81]).await.unwrap(); + // let response = response.await.unwrap(); + // println!("RX: {}", hex::encode(response)); let mut long_request: [u8; 32] = [0; 32]; long_request[0] = 0x10; diff --git a/src/async_can.rs b/src/async_can.rs index 7e27898..6d424aa 100644 --- a/src/async_can.rs +++ b/src/async_can.rs @@ -8,9 +8,11 @@ use crate::can::Identifier; use async_stream::stream; use futures_core::stream::Stream; use tokio::sync::{broadcast, mpsc, oneshot}; +use tracing::debug; const CAN_TX_BUFFER_SIZE: usize = 128; const CAN_RX_BUFFER_SIZE: usize = 1024; +const DEBUG: bool = false; type BusIdentifier = (u8, Identifier); type FrameCallback = (Frame, oneshot::Sender<()>); @@ -27,6 +29,10 @@ fn process( while shutdown_receiver.try_recv().is_err() { let frames: Vec = adapter.recv().unwrap(); for frame in frames { + if DEBUG { + debug! {"RX {:?}", frame}; + } + // Wake up sender if frame.loopback { let callback = callbacks @@ -60,6 +66,10 @@ fn process( .or_insert_with(VecDeque::new) .push_back((loopback_frame, callback)); + if DEBUG { + debug! {"TX {:?}", frame}; + } + buffer.push(frame); } if !buffer.is_empty() { diff --git a/src/can.rs b/src/can.rs index 1a2f5b9..de2d458 100644 --- a/src/can.rs +++ b/src/can.rs @@ -1,7 +1,9 @@ //! Generic CAN types and traits +use std::fmt; + /// Identifier for a CAN frame -#[derive(Debug, Copy, Clone, PartialOrd, Eq, PartialEq, Hash)] +#[derive(Copy, Clone, PartialOrd, Eq, PartialEq, Hash)] pub enum Identifier { Standard(u32), Extended(u32), @@ -19,8 +21,36 @@ impl Identifier { } } +impl fmt::Debug for Identifier { + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { + match self { + Identifier::Extended(id) => write!(f, "0x{:08x}", id), + Identifier::Standard(id) => write!(f, "0x{:03x}", id), + } + } +} + +impl From for Identifier { + fn from(id: u32) -> Identifier { + if id <= 0x7ff { + Identifier::Standard(id) + } else { + Identifier::Extended(id) + } + } +} + +impl From for u32 { + fn from(val: Identifier) -> u32 { + match val { + Identifier::Standard(id) => id, + Identifier::Extended(id) => id, + } + } +} + /// A CAN frame -#[derive(Debug, Clone, PartialEq)] +#[derive(Clone, PartialEq)] pub struct Frame { /// The bus index for adapters supporting multiple CAN busses pub bus: u8, @@ -45,22 +75,14 @@ impl Frame { } } -impl From for Identifier { - fn from(id: u32) -> Identifier { - if id <= 0x7ff { - Identifier::Standard(id) - } else { - Identifier::Extended(id) - } - } -} - -impl From for u32 { - fn from(val: Identifier) -> u32 { - match val { - Identifier::Standard(id) => id, - Identifier::Extended(id) => id, - } +impl fmt::Debug for Frame { + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { + f.debug_struct("Frame") + .field("bus", &self.bus) + .field("id", &self.id) + .field("data", &hex::encode(&self.data)) + .field("loopback", &self.loopback) + .finish() } } diff --git a/src/isotp/mod.rs b/src/isotp/mod.rs index 2f349db..72a3a41 100644 --- a/src/isotp/mod.rs +++ b/src/isotp/mod.rs @@ -291,6 +291,7 @@ impl<'a> IsoTPAdapter<'a> { let mut buf = Vec::new(); let mut len: usize = 0; let mut idx: u8 = 1; + println!("RX"); while let Some(frame) = stream.next().await { let frame = frame?;