-
Notifications
You must be signed in to change notification settings - Fork 8.2k
CAN ISO-TP updates to allow padding and fixed addressing #31078
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
Conversation
3402567 to
4012b59
Compare
|
Thanks for the fix and extension! |
|
Can you add a test for the fixed addressing feature? |
Yep, can do that. Just identified a length calculation error in the padding implementation in |
|
Would it make sense to add a fag to the address struct for padding instead of compile time setting? |
AUTOSAR spec chapter 7.3.8 suggests to select it at compile-time. Also this library which I use in an ESP32 has a compile-time flag (with padding enabled by default, though). I think it's fine to select it at compile-time depending on the higher-level protocol in use and save some memory if we don't need the checks and can accept padded and non-padded frames. |
4012b59 to
06a205b
Compare
|
Added new unit-tests for the fixed addressing feature and updated existing tests to work with and without padding enabled.
|
|
Beside my comment, LGTM. |
Cool. Which comment do you mean? I think I have addressed both of your comments. The tests have been added and I stated why I suggest to keep the padding switch as a compile-time option. The unit test which fails on some platforms is not related to this patch and seems to be a more generic issue, see also #31149 and #31150. |
subsys/canbus/isotp/isotp.c
Outdated
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why do you remove this check? According to the standard, the length in the frame must match if there is no padding, IIRC
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@martinjaeger any thoughts here?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I can't see any statement that it must match. Some quotes from ISO 15765-2:2004:
6.5.2.2 (SF_DL error handling)
If the network layer receives an SF with an SF_DL greater than 7 when using normal addressing, or greater than 6 for extended or mixed addressing, then the network layer shall ignore the received SF N_PDU.
7.4.3 CAN frame data optimization
[...] The DLC parameter of the CAN frame is set by the sender and read by the receiver to determine the number of data bytes per CAN frame to be processed by the network layer. The DLC parameter cannot be used to determine the message length; this information shall be extracted from the N_PCI information in the beginning of a message.
7.4.4 Data Length Code error handling
[...] The reception of a CAN frame with a DLC value smaller than expected [...] shall be ignored by the network layer without any further action.
So in my understanding we should ignore any bytes beyond ISOTP_PCI_SF_DL, i.e. allow padded and non-padded messages. It is not clearly stated what to do with a message that is padded to less than 8 bytes.
However, we might still want to check if the DLC is too small to fit the message with the length calculated based on ISOTP_PCI_SF_DL. I can add that check. What do you think?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
That makes sense!
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done. And rebased.
|
Sry, forgot to submit them (it was in pending sine two weeks) 🤦♂️ |
Previous implementation contained a typo so that only the rx address was checked. Signed-off-by: Martin Jäger <[email protected]>
ISO 15765-2 specifies that the CAN frame length for single frames and the last consecutive frame may or may not be optimized to the actual length of the PDU payload. The ISO-TP implementation should only consider the information from the N_PCI section. The previous implementation did not allow padding. With this commit, padding is allowed by default and can even be required to be more compliant to AUTOSAR standards. Signed-off-by: Martin Jäger <[email protected]>
Fixed addressing as specified in ISO 15765-2 encodes the source and target address of a device or function inside the CAN ID according to SAE J1939. In order to allow to receive incoming requests from different nodes, the CAN filter mask has to be set such that the source address is ignored in the receive context. In addition to that, flow control frames have to be sent back to the actual source of the request, which requires adjustments to the TX CAN ID. This commit implements above features and thus allows ISO-TP to be used in a network based on SAE J1939 or NMEA-2000. Signed-off-by: Martin Jäger <[email protected]>
During debugging I got "Got unexpected PDU" errors because a new CF was received before the state was set to ISOTP_TX_WAIT_FC in the send state machine. Setting the state before printing the debug information fixes the issue. Signed-off-by: Martin Jäger <[email protected]>
06a205b to
6f89259
Compare
|
@carlescufi and @galak: Unfortunately we don't find additional reviewers for this PR as it covers a very specific topic. We've asked for additional reviews on slack multiple times already. @legoabram has worked with ISO-TP and is already working on a PR to port it to CAN-FD. Can we accept his review as a second approval even though he is not (yet) an official collaborator? |
This PR fixes some small issues and adds two new features to the ISO-TP implementation:
Allow Padding in SF, FF and last CF
Some protocols including OBD require that CAN messages always have a length of 8 bytes, which requires padding in the ISO-TP implementation.
Similar to AUTOSAR requirements, I added two build-time configuration flags that allow to activate padding in received and sent frames. If padding is activated for RX, a frame with length < 8 leads to an error, as specified in AUTOSAR. Otherwise, both types of frames are now accepted (default setting).
Padding is mandatory for CAN-FD, as the frame length cannot be adjusted to perfectly match the actual data. However, proper CAN-FD support will require some further modifications which are not part of this PR.
Add support for fixed addressing
ISO 15765-2 specifies how to use ISO-TP for CAN ID layouts according to SAE J1939 and NMEA-2000, where a source (N_SA) and target address (N_TA) is encoded inside the ID (called "fixed addressing"):
In order to support this message type, a CAN filter mask that ignores the source address and the priority during reception was added. In addition to that, FC frames are sent back to the correct source address with the same priority, which requires an automatic modification of the TX CAN ID.