Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

CAN-FD, choose smallest DLC that fits message. Related bugfixes. #71

Merged
merged 2 commits into from
Sep 2, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 11 additions & 9 deletions src/isotp/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -127,19 +127,21 @@ 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 {
if len < CAN_MAX_DLEN {
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 +162,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 Expand Up @@ -208,7 +210,7 @@ impl<'a> IsoTPAdapter<'a> {
pub async fn send_single_frame(&self, data: &[u8]) -> Result<()> {
let mut buf;

if data.len() < 0xf {
if data.len() < 0x8 {
// Len fits in single nibble
buf = vec![FrameType::Single as u8 | data.len() as u8];
} else {
Expand Down
55 changes: 54 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 @@ -161,6 +163,33 @@ async fn isotp_test_fd() {
..Default::default()
};

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

// 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(218, config).await;
isotp_test_echo(256, config).await;

// First frame escape
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;

Expand Down Expand Up @@ -214,3 +243,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;
}
Loading