Skip to content

Commit 89e5805

Browse files
committed
Represent CAN message IDs as embedded_can::Id to support extended IDs
1 parent b3fcfb7 commit 89e5805

File tree

12 files changed

+443
-165
lines changed

12 files changed

+443
-165
lines changed

Cargo.lock

Lines changed: 23 additions & 4 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,10 +18,11 @@ rust-version = "1.78.0"
1818
std = []
1919

2020
[dependencies]
21-
can-dbc = "5.0.0"
21+
can-dbc = "6.0.0"
2222
anyhow = "1.0.68"
2323
heck = "0.4.0"
2424
typed-builder = "0.18.0"
25+
embedded-can = "0.4.1"
2526

2627
[workspace]
2728
members = [

fuzz/Cargo.lock

Lines changed: 21 additions & 4 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/includes/errors.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,17 @@
11
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
22
pub enum CanError {
3-
UnknownMessageId(u32),
3+
UnknownMessageId(embedded_can::Id),
44
/// Signal parameter is not within the range
55
/// defined in the dbc
66
ParameterOutOfRange {
77
/// dbc message id
8-
message_id: u32,
8+
message_id: embedded_can::Id,
99
},
1010
InvalidPayloadSize,
1111
/// Multiplexor value not defined in the dbc
1212
InvalidMultiplexor {
1313
/// dbc message id
14-
message_id: u32,
14+
message_id: embedded_can::Id,
1515
/// Multiplexor value not defined in the dbc
1616
multiplexor: u16,
1717
},

src/lib.rs

Lines changed: 25 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -143,6 +143,7 @@ pub fn codegen(config: Config<'_>, out: impl Write) -> Result<()> {
143143
writeln!(&mut w)?;
144144
writeln!(&mut w, "use core::ops::BitOr;")?;
145145
writeln!(&mut w, "use bitvec::prelude::*;")?;
146+
writeln!(&mut w, "use embedded_can::{{Id, StandardId, ExtendedId}};")?;
146147

147148
writeln!(w, r##"#[cfg(feature = "arb")]"##)?;
148149
writeln!(&mut w, "use arbitrary::{{Arbitrary, Unstructured}};")?;
@@ -210,7 +211,7 @@ fn render_root_enum(mut w: impl Write, dbc: &DBC, config: &Config<'_>) -> Result
210211
writeln!(w, "#[inline(never)]")?;
211212
writeln!(
212213
&mut w,
213-
"pub fn from_can_message(id: u32, payload: &[u8]) -> Result<Self, CanError> {{",
214+
"pub fn from_can_message(id: Id, payload: &[u8]) -> Result<Self, CanError> {{",
214215
)?;
215216
{
216217
let mut w = PadAdapter::wrap(&mut w);
@@ -221,12 +222,11 @@ fn render_root_enum(mut w: impl Write, dbc: &DBC, config: &Config<'_>) -> Result
221222
for msg in get_relevant_messages(dbc) {
222223
writeln!(
223224
w,
224-
"{} => Messages::{1}({1}::try_from(payload)?),",
225-
msg.message_id().0,
225+
"{0}::MESSAGE_ID => Messages::{0}({0}::try_from(payload)?),",
226226
type_name(msg.message_name())
227227
)?;
228228
}
229-
writeln!(w, r#"n => return Err(CanError::UnknownMessageId(n)),"#)?;
229+
writeln!(w, r#"id => return Err(CanError::UnknownMessageId(id)),"#)?;
230230
}
231231
writeln!(&mut w, "}};")?;
232232
writeln!(&mut w, "Ok(res)")?;
@@ -243,7 +243,10 @@ fn render_root_enum(mut w: impl Write, dbc: &DBC, config: &Config<'_>) -> Result
243243
fn render_message(mut w: impl Write, config: &Config<'_>, msg: &Message, dbc: &DBC) -> Result<()> {
244244
writeln!(w, "/// {}", msg.message_name())?;
245245
writeln!(w, "///")?;
246-
writeln!(w, "/// - ID: {0} (0x{0:x})", msg.message_id().0)?;
246+
match msg.message_id() {
247+
can_dbc::MessageId::Standard(id) => writeln!(w, "/// - Standard ID: {0} (0x{0:x})", id),
248+
can_dbc::MessageId::Extended(id) => writeln!(w, "/// - Extended ID: {0} (0x{0:x})", id),
249+
}?;
247250
writeln!(w, "/// - Size: {} bytes", msg.message_size())?;
248251
if let can_dbc::Transmitter::NodeName(transmitter) = msg.transmitter() {
249252
writeln!(w, "/// - Transmitter: {}", transmitter)?;
@@ -274,8 +277,18 @@ fn render_message(mut w: impl Write, config: &Config<'_>, msg: &Message, dbc: &D
274277

275278
writeln!(
276279
&mut w,
277-
"pub const MESSAGE_ID: u32 = {};",
278-
msg.message_id().0
280+
"pub const MESSAGE_ID: embedded_can::Id = {};",
281+
match msg.message_id() {
282+
// use StandardId::new().unwrap() once const_option is stable
283+
can_dbc::MessageId::Standard(id) => format!(
284+
"Id::Standard(unsafe {{ StandardId::new_unchecked({0:#x})}})",
285+
id
286+
),
287+
can_dbc::MessageId::Extended(id) => format!(
288+
"Id::Extended(unsafe {{ ExtendedId::new_unchecked({0:#x})}})",
289+
id
290+
),
291+
}
279292
)?;
280293
writeln!(w)?;
281294

@@ -623,8 +636,8 @@ fn render_set_signal(
623636
let mut w = PadAdapter::wrap(&mut w);
624637
writeln!(
625638
w,
626-
r##"return Err(CanError::ParameterOutOfRange {{ message_id: {message_id} }});"##,
627-
message_id = msg.message_id().0,
639+
r##"return Err(CanError::ParameterOutOfRange {{ message_id: {}::MESSAGE_ID }});"##,
640+
type_name(msg.message_name())
628641
)?;
629642
}
630643
writeln!(w, r"}}")?;
@@ -742,8 +755,8 @@ fn render_multiplexor_signal(
742755
}
743756
writeln!(
744757
&mut w,
745-
"multiplexor => Err(CanError::InvalidMultiplexor {{ message_id: {}, multiplexor: multiplexor.into() }}),",
746-
msg.message_id().0
758+
"multiplexor => Err(CanError::InvalidMultiplexor {{ message_id: {}::MESSAGE_ID, multiplexor: multiplexor.into() }}),",
759+
type_name(msg.message_name())
747760
)?;
748761
}
749762

@@ -914,8 +927,7 @@ fn signal_to_payload(mut w: impl Write, signal: &Signal, msg: &Message) -> Resul
914927
}
915928
writeln!(
916929
&mut w,
917-
" .ok_or(CanError::ParameterOutOfRange {{ message_id: {} }})?;",
918-
msg.message_id().0,
930+
" .ok_or(CanError::ParameterOutOfRange {{ message_id: Self::MESSAGE_ID }})?;",
919931
)?;
920932
writeln!(
921933
&mut w,

testing/can-embedded/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ build-messages = ["dep:can-messages"]
1010

1111
[dependencies]
1212
bitvec = { version = "1.0", default-features = false }
13+
embedded-can = "0.4.1"
1314

1415

1516
# This is optional and default so we can turn it off for the embedded target.

testing/can-messages/Cargo.toml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ edition = "2021"
77
[dependencies]
88
bitvec = { version = "1.0", default-features = false }
99
arbitrary = { version = "1.0", optional = true }
10+
embedded-can = "0.4.1"
1011

1112
[build-dependencies]
1213
anyhow = "1.0"
@@ -15,4 +16,4 @@ dbc-codegen = { path = "../../" }
1516
[features]
1617
default = ["arb", "std"]
1718
arb = ["arbitrary"]
18-
std = []
19+
std = []

0 commit comments

Comments
 (0)