Skip to content

Commit

Permalink
better asynccan debugging
Browse files Browse the repository at this point in the history
  • Loading branch information
pd0wm committed Mar 15, 2024
1 parent 5c32593 commit cdd8724
Show file tree
Hide file tree
Showing 4 changed files with 52 additions and 21 deletions.
4 changes: 1 addition & 3 deletions examples/isotp.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,7 @@ async fn main() {
tracing_subscriber::fmt::init();

let adapter = automotive::adapter::get_adapter().unwrap();
let mut config = IsoTPConfig::new(0, Identifier::Standard(0x7a1));
config.timeout = std::time::Duration::from_secs(1);

let config = IsoTPConfig::new(0, Identifier::Standard(0x7a1));
let isotp = IsoTPAdapter::new(&adapter, config);

let mut stream = isotp.recv();
Expand Down
10 changes: 10 additions & 0 deletions src/async_can.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<()>);
Expand All @@ -27,6 +29,10 @@ fn process<T: CanAdapter>(
while shutdown_receiver.try_recv().is_err() {
let frames: Vec<Frame> = adapter.recv().unwrap();
for frame in frames {
if DEBUG {
debug! {"RX {:?}", frame};
}

// Wake up sender
if frame.loopback {
let callback = callbacks
Expand Down Expand Up @@ -60,6 +66,10 @@ fn process<T: CanAdapter>(
.or_insert_with(VecDeque::new)
.push_back((loopback_frame, callback));

if DEBUG {
debug! {"TX {:?}", frame};
}

buffer.push(frame);
}
if !buffer.is_empty() {
Expand Down
58 changes: 40 additions & 18 deletions src/can.rs
Original file line number Diff line number Diff line change
@@ -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),
Expand All @@ -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<u32> for Identifier {
fn from(id: u32) -> Identifier {
if id <= 0x7ff {
Identifier::Standard(id)
} else {
Identifier::Extended(id)
}
}
}

impl From<Identifier> 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,
Expand All @@ -45,22 +75,14 @@ impl Frame {
}
}

impl From<u32> for Identifier {
fn from(id: u32) -> Identifier {
if id <= 0x7ff {
Identifier::Standard(id)
} else {
Identifier::Extended(id)
}
}
}

impl From<Identifier> 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()
}
}

Expand Down
1 change: 1 addition & 0 deletions src/isotp/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -279,6 +279,7 @@ impl<'a> IsoTPAdapter<'a> {
let mut buf = Vec::new();
let mut len: Option<usize> = None;
let mut idx: u8 = 1;
println!("RX");

while let Some(frame) = stream.next().await {
let frame = frame?;
Expand Down

0 comments on commit cdd8724

Please sign in to comment.