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 88d2852 commit 6025153
Show file tree
Hide file tree
Showing 4 changed files with 59 additions and 26 deletions.
16 changes: 8 additions & 8 deletions examples/isotp.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
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 @@ -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?;
Expand Down

0 comments on commit 6025153

Please sign in to comment.