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

MessageId Standard vs Extended enum #13

Merged
merged 2 commits into from
Feb 8, 2024
Merged

Conversation

erzoe
Copy link
Contributor

@erzoe erzoe commented Jan 10, 2024

This is a continuation of #10.

In dbc files the information whether a message is an extended frame or standard frame is encoded in bit 31 of the message ID. This is an implementation detail that I believe most users are not familiar with and that I have not seen in the documentation of this crate. Therefore I think it is very unintuitive to expose this to the users of this crate. Therefore I have replaced the previous MessageId struct which only contained the u32 as it was saved in the file with the MessageId enum proposed by schphil. #10 (comment)

The message id is now masked and can be directly compared to the message ids of incoming CAN bus messages. The information whether it's a standard frame or extended frame is intuitively available. This does not only make the usage of the crate more intuitive but is also helpful in case this crate ever wants to support more file formats such as sym files where this information is encoded differently.

For more information on dbc file specifics see the DBC_File_Format_Documentation: http://mcu.so/Microcontroller/Automotive/dbc-file-format-documentation_compress.pdf

This commit is, of course, a breaking change. But given that this crate is at version 5.0.0 it does not seem like it has a strict policy of avoiding breaking changes. If breaking changes are a concern we could rename the new enum to FrameId, restore the old struct and add both of them to the Message struct. That might confuse users, though, which field to use and would require good documentation.

Thank you for this useful crate.

In dbc files the information whether a message is an extended frame or standard frame is encoded in bit 31 of the message ID. This is an implementation detail that I believe most users are not familiar with and that I have not seen in the documentation of this crate. Therefore I think it is very unintuitive to expose this to the users of this crate.
Therefore I have replaced the previous MessageId struct which only contained the u32 as it was saved in the file with the MessageId enum proposed by schphil.
marcelbuesing#10 (comment)

The message id is now masked and can be directly compared to the message ids of incoming CAN bus messages. The information whether it's a standard frame or extended frame is intuitively available.
This does not only make the usage of the crate more intuitive but is also helpful in case this crate ever wants to support more file formats such as sym files where this information is encoded differently.

For more information on dbc file specifics see the DBC_File_Format_Documentation:
http://mcu.so/Microcontroller/Automotive/dbc-file-format-documentation_compress.pdf

This commit is, of course, a breaking change. But given that this crate is at version 5.0.0 it does not seem like it has a strict policy of avoiding breaking changes.
If breaking changes are a concern we could rename the new enum to FrameId, restore the old struct and add both of them to the Message struct. That might confuse users, though, which field to use and would require good documentation.
@erzoe
Copy link
Contributor Author

erzoe commented Jan 10, 2024

I have tested my changes with cargo test and all tests are passing.

@erzoe erzoe mentioned this pull request Jan 10, 2024
@kistenklaus
Copy link

kistenklaus commented Jan 15, 2024

i believe it would be nice to follow the socket can standard here the IDE flag is stored in the 30 bits, RTR at 31 and ERR at 29. But maybe a stupid idea because it would brake backwards compatibility.

I believe the simplest solution is to add a simple getter: is_ext and is_std for the MessageId.

Edit:
CAN_DBC standard actually specified the IDE flag to be at the 31 location, so the statement above doesn't make sense anymore. Whoops

@mbrieske
Copy link

I recently encountered the same issue, and I believe the solution proposed here by @erzoe is ideal. In my opinion, it is a much better approach than the one in #14, which appears to be more of a workaround. Handling Standard and Extended CAN IDs is a fundamental aspect of the CAN standard and should be treated accordingly.

src/parser.rs Outdated
map(complete::u32, MessageId)(s)
let (s, parsed_value) = complete::u32(s)?;

if parsed_value > u16::MAX as u32 {
Copy link
Owner

Choose a reason for hiding this comment

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

Would you mind taking from #14 this check for extended id:

self.0 & 0x80000000 != 0 // id & (1 << 31)

Copy link
Contributor Author

Choose a reason for hiding this comment

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

sure, I have changed this in commit 5d20fa8. Thanks for the review. I am happy to see this is going forward.

@marcelbuesing marcelbuesing merged commit dc85420 into marcelbuesing:dev Feb 8, 2024
3 of 5 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants