- 
                Notifications
    
You must be signed in to change notification settings  - Fork 8.2k
 
drivers: can: use flags fields for can_frame and can_filter structs #51361
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
drivers: can: use flags fields for can_frame and can_filter structs #51361
Conversation
Avoid reusing the CAN_EXTENDED_IDENTIFIER and CAN_STANDARD_IDENTIFIER definitions from the CAN controller driver API for ISO-TP structure members as this is a fragile design. The ISO-TP layer must be responsible for its own definitions where needed. Replace the "id_type" ISO-TP struct member with a well-known abbreviated "ide" bit (Identifier Extension Bit) struct member. Signed-off-by: Henrik Brix Andersen <[email protected]>
d713447    to
    67c9895      
    Compare
  
    67c9895    to
    2d2bd56      
    Compare
  
    | 
           The main driver behind the change from discrete struct members to using a flag bitfields was the discoveries made by testing the proposed change in #50769. This revealed that older CAN code (written before the introduction of CAN-FD support in Zephyr) did not explicitly set the value of the  Moving to a single   | 
    
e9c3e5d    to
    4a0f783      
    Compare
  
    The can_frame and can_filter structs support a number of different flags (standard/extended CAN ID type, Remote Transmission Request, CAN-FD format, Bit Rate Switch, ...). Each of these flags is represented as a discrete bit in the given structure. This design pattern requires every user of these structs to initialize all of these flags to either 0 or 1, which does not scale well for future flag additions. Some of these flags have associated enumerations to be used for assignment, some do not. CAN drivers and protocols tend to rely on the logical value of the flag instead of using the enumeration, leading to a very fragile API. The enumerations are used inconsistently between the can_frame and can_filter structures, which further complicates the API. Instead, convert these flags to bitfields with separate flag definitions for the can_frame and can_filter structures. This API allows for future extensions without having to revisit existing users of the two structures. Furthermore, this allows driver to easily check for unsupported flags in the respective API calls. As this change leads to the "id_mask" field of the can_filter to be the only mask present in that structure, rename it to "mask" for simplicity. Fixes: zephyrproject-rtos#50776 Signed-off-by: Henrik Brix Andersen <[email protected]>
Add test case for verifying that CAN-FD format frames cannot be sent in non-FD mode. Signed-off-by: Henrik Brix Andersen <[email protected]>
Set the CAN controller to CAN-FD mode before attempting to send FD format frames. Signed-off-by: Henrik Brix Andersen <[email protected]>
4a0f783    to
    5093239      
    Compare
  
    The member variable was renamed from id_mask to mask in zephyrproject-rtos#51361, but the docs were not adjusted accordingly. Signed-off-by: Martin Jäger <[email protected]>
The member variable was renamed from id_mask to mask in #51361, but the docs were not adjusted accordingly. Signed-off-by: Martin Jäger <[email protected]>
The can_frame and can_filter structs support a number of different flags (standard/extended CAN ID type, Remote Transmission Request, CAN-FD format, Bit Rate Switch, ...). Each of these flags is represented as a discrete bit in the given structure.
This design pattern requires every user of these structs to initialize all of these flags to either 0 or 1, which does not scale well for future flag additions.
Some of these flags have associated enumerations to be used for assignment, some do not. CAN drivers and protocols tend to rely on the logical value of the flag instead of using the enumeration, leading to a very fragile API. The enumerations are used inconsistently between the can_frame and can_filter structures, which further complicates the API.
The CAN_EXTENDED_IDENTIFIER and CAN_STANDARD_IDENTIFIER definitions from the CAN controller driver API are reused directly for ISO-TP structure members, which leads to a very fragile design of this API. The ISO-TP layer must be responsible for its own definitions where needed. This PR replaces the "id_type" ISO-TP struct member with a well-known abbreviated "ide" bit (Identifier Extension Bit) struct member.
The discrete flags are converted to bitfields with separate flag definitions for the can_frame and can_filter structures. This API allows for future extensions without having to revisit existing users of the two structures. Furthermore, this allows driver to easily check for unsupported flags in the respective API calls.
As this change leads to the "id_mask" field of the can_filter to be the only mask present in that structure, rename it to "mask" for simplicity.
With the above changes in place, we can add test case for verifying that CAN-FD format frames cannot be sent in non-FD mode, which fixes #50776.