Skip to content

Commit fcdf4ed

Browse files
committed
move nominal_bitrate_from_config to common
1 parent ad17487 commit fcdf4ed

3 files changed

Lines changed: 32 additions & 41 deletions

File tree

src/j2534/can_adapter.rs

Lines changed: 16 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
//! on [`crate::can::AsyncCanAdapter`] for background polling, retry logic, and
55
//! async send/receive orchestration.
66
7+
use super::common::nominal_bitrate_from_config;
78
use super::common::{self, can_id_flags, parse_can_id, J2534Channel, J2534Device, PassThruMsg};
89
use super::constants::{Protocol, Status, CAN_ID_BOTH};
910
use super::error::Error as J2534Error;
@@ -16,22 +17,20 @@ use std::collections::VecDeque;
1617
/// Poll the J2534 channel without blocking the `AsyncCanAdapter` process loop.
1718
const IO_TIMEOUT_MS: u32 = 0;
1819

19-
// J2534 does not expose portable timing metadata for raw CAN channels, so use a
20-
// permissive nominal-only timing range to drive BitrateBuilder.
21-
const J2534_NOMINAL_BTC: BitTimingConst = BitTimingConst {
22-
clock_hz: 80_000_000,
23-
tseg1_min: 1,
24-
tseg1_max: 1 << 8,
25-
tseg2_min: 1,
26-
tseg2_max: 1 << 7,
27-
sjw_max: 1 << 7,
28-
brp_min: 1,
29-
brp_max: 1 << 10,
30-
brp_inc: 1,
31-
};
32-
20+
// J2534 does not expose portable timing metadata for CAN channel setup, so
21+
// use a permissive nominal-only timing range to drive BitrateBuilder.
3322
const J2534_TIMING_CONST: AdapterTimingConst = AdapterTimingConst {
34-
nominal: J2534_NOMINAL_BTC,
23+
nominal: BitTimingConst {
24+
clock_hz: 80_000_000,
25+
tseg1_min: 1,
26+
tseg1_max: 1 << 8,
27+
tseg2_min: 1,
28+
tseg2_max: 1 << 7,
29+
sjw_max: 1 << 7,
30+
brp_min: 1,
31+
brp_max: 1 << 10,
32+
brp_inc: 1,
33+
},
3534
data: None,
3635
};
3736

@@ -68,7 +67,7 @@ impl J2534CanAdapter {
6867
/// * `bitrate_cfg` — nominal CAN bitrate configuration. CAN-FD data phase
6968
/// settings are currently rejected.
7069
pub fn new(dll_path: Option<&str>, bitrate_cfg: BitrateConfig) -> Result<Self> {
71-
let bitrate = Self::nominal_bitrate(&bitrate_cfg)?;
70+
let bitrate = nominal_bitrate_from_config(&bitrate_cfg)?;
7271
let device = common::open_device(dll_path)?;
7372
Self::new_on_device_with_bitrate(device, bitrate)
7473
}
@@ -80,20 +79,10 @@ impl J2534CanAdapter {
8079
/// channel was closed).
8180
///
8281
pub fn new_on_device(device: J2534Device, bitrate_cfg: BitrateConfig) -> Result<Self> {
83-
let bitrate = Self::nominal_bitrate(&bitrate_cfg)?;
82+
let bitrate = nominal_bitrate_from_config(&bitrate_cfg)?;
8483
Self::new_on_device_with_bitrate(device, bitrate)
8584
}
8685

87-
fn nominal_bitrate(bitrate_cfg: &BitrateConfig) -> Result<u32> {
88-
if bitrate_cfg.data.is_some() {
89-
return Err(crate::Error::InvalidBitrate(
90-
"J2534 CAN adapter does not support CAN-FD bitrate configuration".to_string(),
91-
));
92-
}
93-
94-
Ok(bitrate_cfg.nominal.bitrate)
95-
}
96-
9786
fn new_on_device_with_bitrate(device: J2534Device, bitrate: u32) -> Result<Self> {
9887
let channel =
9988
common::connect_channel_with_flags(&device, Protocol::Can, CAN_ID_BOTH, bitrate)?;

src/j2534/common.rs

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
55
use super::constants::{FilterType, IoctlId, IoctlParam, Protocol, Status, CAN_29BIT_ID_FLAG};
66
use super::error::Error as J2534Error;
7+
use crate::can::bitrate::BitrateConfig;
78
use crate::can::Identifier;
89
use crate::Result;
910

@@ -426,3 +427,13 @@ fn protocol_name(protocol: Protocol) -> &'static str {
426427
Protocol::Iso15765 => "ISO15765",
427428
}
428429
}
430+
431+
pub(crate) fn nominal_bitrate_from_config(bitrate_cfg: &BitrateConfig) -> crate::Result<u32> {
432+
if bitrate_cfg.data.is_some() {
433+
return Err(crate::Error::InvalidBitrate(format!(
434+
"J2534 does not support CAN-FD bitrate configuration"
435+
)));
436+
}
437+
438+
Ok(bitrate_cfg.nominal.bitrate)
439+
}

src/j2534/isotp_adapter.rs

Lines changed: 5 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,9 @@ use crate::can::Identifier;
3737
use crate::isotp::{duration_to_stmin_byte, IsoTPConfig};
3838
use crate::{Result, TransportLayer};
3939

40-
use super::common::{self, parse_can_id, J2534Channel, J2534Device, PassThruMsg};
40+
use super::common::{
41+
self, nominal_bitrate_from_config, parse_can_id, J2534Channel, J2534Device, PassThruMsg,
42+
};
4143
use super::constants::{
4244
IoctlParam, Protocol, Status, CAN_29BIT_ID_FLAG, CAN_ID_BOTH, ISO15765_ADDR_TYPE,
4345
ISO15765_FRAME_PAD, ISO15765_PADDING_ERROR,
@@ -86,7 +88,7 @@ impl J2534NativeIsoTpTransport {
8688
bitrate_cfg: BitrateConfig,
8789
config: IsoTPConfig,
8890
) -> Result<Self> {
89-
let bitrate = Self::nominal_bitrate(&bitrate_cfg)?;
91+
let bitrate = nominal_bitrate_from_config(&bitrate_cfg)?;
9092
let device = common::open_device(dll_path)?;
9193
Self::new_on_device_with_bitrate(device, bitrate, config)
9294
}
@@ -105,21 +107,10 @@ impl J2534NativeIsoTpTransport {
105107
bitrate_cfg: BitrateConfig,
106108
config: IsoTPConfig,
107109
) -> Result<Self> {
108-
let bitrate = Self::nominal_bitrate(&bitrate_cfg)?;
110+
let bitrate = nominal_bitrate_from_config(&bitrate_cfg)?;
109111
Self::new_on_device_with_bitrate(device, bitrate, config)
110112
}
111113

112-
fn nominal_bitrate(bitrate_cfg: &BitrateConfig) -> Result<u32> {
113-
if bitrate_cfg.data.is_some() {
114-
return Err(crate::Error::InvalidBitrate(
115-
"J2534 native ISO-TP transport does not support CAN-FD bitrate configuration"
116-
.to_string(),
117-
));
118-
}
119-
120-
Ok(bitrate_cfg.nominal.bitrate)
121-
}
122-
123114
fn new_on_device_with_bitrate(
124115
device: J2534Device,
125116
bitrate: u32,

0 commit comments

Comments
 (0)