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

Socketcand ext ID bug fixes, and implement channel{_info} and Message attributes #1508

Merged

Conversation

faisal-shah
Copy link
Contributor

See #1504 for bug description

This pull request combines:
#1505
#1503

@@ -40,11 +45,15 @@ def convert_can_message_to_ascii_message(can_message: can.Message) -> str:
# Note: socketcan bus adds extended flag, remote_frame_flag & error_flag to id
# not sure if that is necessary here
can_id = can_message.arbitration_id
if can_message.is_extended_id or (can_id > 0x7FF):
Copy link
Collaborator

Choose a reason for hiding this comment

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

I'm sorry but this check for (can_id > 0x7FF) is still wrong.

If you want to create robust code you might mask the can_id after the check for is_extended_id.
(masking ext ID with & 1FFFFFFF and a 11 bit ID with & 0x7FF)

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Absolutely correct .. fixed and force pushed.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I added Message.is_rx attribute in the convert_ascii_message_to_can_message function, instead of after it is called.

socketcand uses the length of the arbitration id field to indicate
whether a frame is using extended id or not. 3 characters for standard,
8 for extended.

Prior to this fix, the arbitration id would be truncated to the lower 11
bits, or at times even garbage.
@hartkopp
Copy link
Collaborator

@zariiii9003 I'm definitely fine with 6ddbfa9 now. Can you please check (and commit?) the two more Python specific patches? Thx!

if can_message.is_extended_id:
can_id_string = f"{(can_id&0x1FFFFFFF):08X}"
else:
can_id_string = f"{(can_id&0x7FF):03X}"
# Note: seems like we cannot add CANFD_BRS (bitrate_switch) and CANFD_ESI (error_state_indicator) flags
data = can_message.data
length = can_message.dlc
bytes_string = " ".join(f"{x:x}" for x in data[0:length])
Copy link
Collaborator

Choose a reason for hiding this comment

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

@hartkopp Shouldn't the hex bytes here be zero-padded, e.g. 0c instead of c?
If so, the format string should be f"{x:02x}". (I know nothing about socketcand...)

Copy link
Collaborator

Choose a reason for hiding this comment

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

Doesn't f"{(can_id&0x1FFFFFFF):08X}" mean it is zero padded with a total length of 8 -> 00000123 ??
Because this is what would be needed here.

f"{x:02x}" would be the right approach for data bytes (not CAN identifier), right?

Copy link
Collaborator

Choose a reason for hiding this comment

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

I'm talking about the data bytes here:

bytes_string = " ".join(f"{x:x}" for x in data[0:length])

For example, should it be 2 4 6 8 a or 02 04 06 08 0a? Does it make a difference for socketcand? Or is it okay as long as there is spacing between the bytes?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

For < send >, it's supposed to be lowercase unpadded (%hhx) with spaces in-between data bytes:
https://github.com/linux-can/socketcand/blob/master/state_raw.c#L158-L171

Copy link
Contributor Author

Choose a reason for hiding this comment

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

And on the receive side (< frame > message), the data is padded uppercase (%02X) without spaces in between:
https://github.com/linux-can/socketcand/blob/master/state_raw.c#L125-L135

Copy link
Collaborator

Choose a reason for hiding this comment

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

Ouch! I wasn't aware of this inconsistency.
But I assume changing this in socketcand is not the way to go as it would potentially break other applications.

Copy link
Collaborator

Choose a reason for hiding this comment

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

Alright, then i will merge this.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Ouch! I wasn't aware of this inconsistency.
But I assume changing this in socketcand is not the way to go as it would potentially break other applications.

Sent PR to correct the docs in socketcand:

linux-can/socketcand#27

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants