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

Multiplexed signal implementation #33

Merged
merged 27 commits into from
Apr 15, 2021
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
27 commits
Select commit Hold shift + click to select a range
9d7c103
Start multiplexed signal implementation
marcelbuesing Mar 25, 2021
6d1f71b
Avoid debug fields for multiplexed signals
marcelbuesing Mar 26, 2021
d8cb3ab
Add lifetimes to enum structs
marcelbuesing Mar 26, 2021
8f90116
Fix lifetimes
marcelbuesing Mar 26, 2021
7b8e344
Include message name in enum name
marcelbuesing Mar 27, 2021
628974b
Render multiplexed signals
marcelbuesing Mar 27, 2021
172bbaf
Exchange HashSet, BtreeMap for BTreeSet, BTreeMap
marcelbuesing Apr 12, 2021
c619171
Update generated output
marcelbuesing Apr 12, 2021
e2d4cff
Shorten multiplexed enum variant name
marcelbuesing Apr 12, 2021
f3a2cc0
Update cantools generated code after dbc update
marcelbuesing Apr 12, 2021
689f4dc
Add missing newlines
marcelbuesing Apr 12, 2021
3f8ed45
Add switch index to generated multiplexed enum
marcelbuesing Apr 12, 2021
8f0d4c5
Extract multiplexor signal rendering
marcelbuesing Apr 12, 2021
1a3077f
Extract signal setter render fn
marcelbuesing Apr 12, 2021
1abdffc
Add _raw suffix to signal setters
marcelbuesing Apr 12, 2021
4c924ad
Add setters for multiplexed signals
marcelbuesing Apr 12, 2021
04cc40b
Revert "Add _raw suffix to signal setters"
marcelbuesing Apr 13, 2021
2599500
Exchange raw ref for owned type in multiplexer
marcelbuesing Apr 13, 2021
c47d708
Remove returning mut ref in setters
marcelbuesing Apr 13, 2021
d0573d3
Cleanup clippy warnings
marcelbuesing Apr 13, 2021
28c33d8
Add cantools multiplexer packing test
marcelbuesing Apr 13, 2021
863116a
Make multiplexor setter private
marcelbuesing Apr 13, 2021
d04d362
Return error on unknown multiplexor value
marcelbuesing Apr 13, 2021
1ab3a37
Add multiplexor value to error
marcelbuesing Apr 14, 2021
8dea322
Replace or_insert with or_insert_with
marcelbuesing Apr 14, 2021
e495937
Derive Default fur multiplexed structs
marcelbuesing Apr 14, 2021
c335bdf
Remove MULTIPLEXED_SWITCH_INDEX associated constant
marcelbuesing Apr 14, 2021
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
5 changes: 5 additions & 0 deletions src/includes/errors.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,11 @@ pub enum CanError {
message_id: u32,
},
InvalidPayloadSize,
/// Multiplexor value not defined in the dbc
InvalidMultiplexor {
/// dbc message id
message_id: u32,
},
}

#[cfg(feature = "std")]
Expand Down
10 changes: 7 additions & 3 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -520,7 +520,7 @@ fn render_multiplexor_signal(mut w: impl Write, signal: &Signal, msg: &Message)

writeln!(
w,
"pub fn {}(&mut self) -> {} {{",
"pub fn {}(&mut self) -> Result<{}, CanError> {{",
field_name(signal.name()),
multiplex_enum_name(msg, signal)?
)?;
Expand All @@ -547,15 +547,19 @@ fn render_multiplexor_signal(mut w: impl Write, signal: &Signal, msg: &Message)
for multiplexer_index in multiplexer_indexes.iter() {
writeln!(
&mut w,
"{idx} => {enum_name}::{multiplexed_wrapper_name}({multiplexed_name}{{ raw: self.raw }}),",
"{idx} => Ok({enum_name}::{multiplexed_wrapper_name}({multiplexed_name}{{ raw: self.raw }})),",
idx = multiplexer_index,
enum_name = multiplex_enum_name(msg, signal)?,
multiplexed_wrapper_name = multiplexed_enum_variant_wrapper_name(*multiplexer_index),
multiplexed_name =
multiplexed_enum_variant_name(msg, signal, *multiplexer_index)?
)?;
}
writeln!(&mut w, "_ => unreachable!(),")?;
writeln!(
&mut w,
"_ => Err(CanError::InvalidMultiplexor {{ message_id: {}}}),",
msg.message_id().0
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hmm, looking at this again, do you think we should include the mulitplexor value? Can we always convert it to a u16 or something? Upside: Better debugging. Downside: Larger size of CanError type.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes I think it's safe to say that it should be possible to convert into u16. I would probably even assume you'll never need more than u8. I think it certainly helps to add the value, otherwise it's hard to debug. So I think it's worth the downside. Went with u16 now, but like i said i can not imagine a use case that requires more than u8 and if there is such a use case that would at least to a compile time error with the generated code.

)?;
}

writeln!(w, "}}")?;
Expand Down
17 changes: 13 additions & 4 deletions testing/can-messages/src/messages.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1036,11 +1036,15 @@ impl MultiplexTest {
signal
}

pub fn multiplexor(&mut self) -> MultiplexTestMultiplexor {
pub fn multiplexor(&mut self) -> Result<MultiplexTestMultiplexor, CanError> {
match self.multiplexor_raw() {
0 => MultiplexTestMultiplexor::M0(MultiplexTestMultiplexorM0 { raw: self.raw }),
1 => MultiplexTestMultiplexor::M1(MultiplexTestMultiplexorM1 { raw: self.raw }),
_ => unreachable!(),
0 => Ok(MultiplexTestMultiplexor::M0(MultiplexTestMultiplexorM0 {
raw: self.raw,
})),
1 => Ok(MultiplexTestMultiplexor::M1(MultiplexTestMultiplexorM1 {
raw: self.raw,
})),
_ => Err(CanError::InvalidMultiplexor { message_id: 200 }),
}
}
/// Set value of Multiplexor
Expand Down Expand Up @@ -1366,6 +1370,11 @@ pub enum CanError {
message_id: u32,
},
InvalidPayloadSize,
/// Multiplexor value not defined in the dbc
InvalidMultiplexor {
/// dbc message id
message_id: u32,
},
}

#[cfg(feature = "std")]
Expand Down
2 changes: 1 addition & 1 deletion testing/can-messages/tests/all.rs
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ fn pack_unpack_message_containing_multiplexed_signals() {

assert_eq!(result.unmultiplexed_signal(), 2);
assert_eq!(result.multiplexor_raw(), 0);
let multiplexor = result.multiplexor();
let multiplexor = result.multiplexor().unwrap();
if let MultiplexTestMultiplexor::M0(m0) = multiplexor {
assert_eq!(m0.multiplexed_signal_zero_a(), 1.2);
assert_eq!(m0.multiplexed_signal_zero_b(), 2.0);
Expand Down