Skip to content

Commit

Permalink
can-fd do not pad to 64 bytes
Browse files Browse the repository at this point in the history
  • Loading branch information
pd0wm committed Sep 2, 2024
1 parent ee5162c commit 23fdcce
Show file tree
Hide file tree
Showing 2 changed files with 58 additions and 9 deletions.
16 changes: 8 additions & 8 deletions src/isotp/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -127,19 +127,19 @@ impl<'a> IsoTPAdapter<'a> {
let offset = self.config.ext_address.is_some() as usize;
let len = data.len() + offset;

// Pad to next valid DLC
// Pad to at least 8 bytes if padding is enabled
if let Some(padding) = self.config.padding {
let padding_len = CAN_MAX_DLEN - len; // Offset for extended address is already accounted for
data.extend(std::iter::repeat(padding).take(padding_len));
}

// Pad to next valid DLC for CAN-FD
if !DLC_TO_LEN.contains(&len) {
let idx = DLC_TO_LEN.iter().position(|&x| x > data.len()).unwrap();
let padding = self.config.padding.unwrap_or(DEFAULT_PADDING_BYTE);
let padding_len = DLC_TO_LEN[idx] - len;
data.extend(std::iter::repeat(padding).take(padding_len));
}

// Pad to full length if padding is enabled
if let Some(padding) = self.config.padding {
let padding_len = self.max_can_data_length() - len;
data.extend(std::iter::repeat(padding).take(padding_len));
}
}

/// Ofset from the start of the frame. 1 in case of extended address, 0 otherwise.
Expand All @@ -160,7 +160,7 @@ impl<'a> IsoTPAdapter<'a> {
/// Maximum data length for a CAN frame based on the current config
fn max_can_data_length(&self) -> usize {
match self.config.max_dlen {
Some(dlen) => dlen,
Some(dlen) => dlen - self.offset(),
None => {
if self.config.fd {
self.can_fd_max_dlen()
Expand Down
51 changes: 50 additions & 1 deletion tests/isotp_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ struct VECUConfig {
pub padding: Option<u8>,
pub fd: bool,
pub ext_address: Option<u8>,
pub max_dlen: Option<usize>,
}

impl VECUConfig {
Expand Down Expand Up @@ -78,11 +79,12 @@ async fn isotp_test_echo(msg_len: usize, config: VECUConfig) {
isotp_config.fd = config.fd;
isotp_config.ext_address = config.ext_address;
isotp_config.timeout = std::time::Duration::from_millis(1000);
isotp_config.max_dlen = config.max_dlen;

let isotp = IsoTPAdapter::new(&adapter, isotp_config);

let mut stream = isotp.recv();
let request = vec![0xaa; msg_len];
let request = vec![0xcc; msg_len];
isotp.send(&request).await.unwrap();
let response = stream.next().await.unwrap().unwrap();

Expand Down Expand Up @@ -174,6 +176,29 @@ async fn isotp_test_fd() {
isotp_test_echo(5000, config).await;
}

#[cfg(feature = "test_vcan")]
#[tokio::test]
#[serial_test::serial]
async fn isotp_test_fd_max_dlen() {
let config = VECUConfig {
fd: true,
max_dlen: Some(8),
..Default::default()
};

// Single frame escape
isotp_test_echo(62, config).await;

// Single frame with some padding to reach next DLC
isotp_test_echo(50, config).await;

// Multiple frames
isotp_test_echo(256, config).await;

// First frame escape
isotp_test_echo(5000, config).await;
}

#[cfg(feature = "test_vcan")]
#[tokio::test]
#[serial_test::serial]
Expand Down Expand Up @@ -214,3 +239,27 @@ async fn isotp_test_fd_extended() {
// First frame escape
isotp_test_echo(5000, config).await;
}

#[cfg(feature = "test_vcan")]
#[tokio::test]
#[serial_test::serial]
async fn isotp_test_fd_extended_max_dlen() {
let config = VECUConfig {
fd: true,
ext_address: Some(0xff),
max_dlen: Some(8),
..Default::default()
};

// Single frame escape
isotp_test_echo(62, config).await;

// Single frame with some padding to reach next DLC
isotp_test_echo(50, config).await;

// Multiple frames
isotp_test_echo(256, config).await;

// First frame escape
isotp_test_echo(5000, config).await;
}

0 comments on commit 23fdcce

Please sign in to comment.