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

interface for changing bitrate #1030

Draft
wants to merge 11 commits into
base: main
Choose a base branch
from

Conversation

cperkulator
Copy link
Contributor

Quick implementation of changing the bitrate at runtime. I just want to get a feel if this is the interface we would like or if there is something better.

I just implemented the Vector interface for now but I can add socketcan eventually (would close #549 ).

This might be a start for implementing autobaud as well.

TODO

  • need to add error handling

Caleb Perkinson added 2 commits April 23, 2021 17:26
implemented in vector. will add to socketcan eventually.
just want to get some thoughts on this interface
@mergify mergify bot requested a review from hardbyte April 23, 2021 22:31
@codecov
Copy link

codecov bot commented Apr 23, 2021

Codecov Report

Merging #1030 (48b8f8c) into develop (7389b23) will decrease coverage by 2.12%.
The diff coverage is 39.21%.

@@             Coverage Diff             @@
##           develop    #1030      +/-   ##
===========================================
- Coverage    66.51%   64.38%   -2.13%     
===========================================
  Files           78       79       +1     
  Lines         7561     7651      +90     
===========================================
- Hits          5029     4926     -103     
- Misses        2532     2725     +193     

@cperkulator cperkulator marked this pull request as draft April 24, 2021 13:24
@felixdivo
Copy link
Collaborator

Wouldn't it be nice to implement a more generic change_parameters() method that takes any parameters (just like on bus init)? The method can then complain whether that specific setting is supported to be changed or not.

@@ -515,6 +516,15 @@ def handle_canfd_event(self, event: xlclass.XLcanRxEvent) -> None:
def send(self, msg: Message, timeout: typing.Optional[float] = None):
self._send_sequence([msg])

@property
def bitrate(self) -> int:
return self.bitrate
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 this recuse infinitely? Usually, I see this being implemented with a private attribute self._bitrate.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

oops yeah I'll get that fixed. Just tossed this together quick to get some feedback.

@felixdivo
Copy link
Collaborator

felixdivo commented Apr 26, 2021

Your proposal for implementing it as a property looks really nice! However, as a change to the core class of the library this should get more feedback.

(Side note: This could also be used in other places since it modularizes the configuration of the bus. Sometimes, that might make things more readable.)

@zariiii9003
Copy link
Collaborator

Some questions:

  • How would you deal with CAN FD?
  • Regarding the Vector interface: what happens if you do not have Init Access?

@felixdivo
Copy link
Collaborator

  • How would you deal with CAN FD?

I guess we would not have to implement it right away. Besides, wouldn't the bitrate be handled automatically?

  • Regarding the Vector interface: what happens if you do not have Init Access?

I'd propose to simply throw one of the new exceptions, like a CanOperationError.

@cperkulator
Copy link
Contributor Author

cperkulator commented May 14, 2021

  • How would you deal with CAN FD?

I guess we would not have to implement it right away. Besides, wouldn't the bitrate be handled automatically?

This will only set the arbitration bitrate. Would probably need a separate property for the data bitrate.

  • Regarding the Vector interface: what happens if you do not have Init Access?

I'd propose to simply throw one of the new exceptions, like a CanOperationError.

Check out the latest commit. I think it should handle these issues. I still need to add unit tests but testing from home this all works fine. I'll need to test on my work setup too next week.

can/interfaces/socketcan/constants.py Show resolved Hide resolved
@@ -21,6 +21,29 @@
log_tx = log.getChild("tx")
log_rx = log.getChild("rx")

can_config_lib = ctypes.util.find_library("socketcan")
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Simpler to use this library rather than parsing output from ip but adds a dependency. Thoughts?

@@ -233,6 +233,8 @@ def __init__(
)

if permission_mask.value == self.mask:
self._init_access = True
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'm not a huge fan of this flag but I would rather not check for init access everytime.

@zariiii9003
Copy link
Collaborator

@hartkopp and @lumagi : What do you think about the socketcan part of this PR? Is that a viable solution to implement bit timing configuration for socketcan?

@hartkopp
Copy link
Collaborator

hartkopp commented Jun 4, 2023

@hartkopp and @lumagi : What do you think about the socketcan part of this PR? Is that a viable solution to implement bit timing configuration for socketcan?

The bitrate setting of 'real' CAN interfaces can only be performed as network-admin (aka root) on Linux. That's why the ip tool is mostly used with sudo ip ...

And the setting affects all users on the system (not only python-can users), as the interface has to be shut down to change the bitrates (and has to be set to "up" afterwards). Shutting down the interfaces leads to -ENETDOWN return values for all user space applications working on that interface. Additionally the python-can application needs to be run as root to manipulate the interface status and the bitrate.

So reading the interface properties via ip tool or the netlink socket is fine - but setting new bitrates from python-can is probably not as feasible as suggested by the patch.

@zariiii9003
Copy link
Collaborator

@hartkopp Thank you for your input. It's always neat to have the expert here 👍
It also seems like libsocketcan is not maintained anymore. The last commit is from 2020 and it does not support CAN FD. So it's probably not a good foundation to build upon.

@hartkopp
Copy link
Collaborator

hartkopp commented Jun 4, 2023

It also seems like libsocketcan is not maintained anymore. The last commit is from 2020 and it does not support CAN FD. So it's probably not a good foundation to build upon.

@marckleinebudde : Is there any ongoing activity planned for libsocketcan ?

@lumagi
Copy link
Collaborator

lumagi commented Jun 4, 2023

I agree with @hartkopp. The executing process must be root or at least have the NET_ADMIN capability to change the interface. Neither one of those things is something you want to do to your Python interpreter on a regular basis.

@marckleinebudde
Copy link

marckleinebudde commented Jun 6, 2023

There is no immediate activity planned for libsocketcan, but patches are welcome. However I'd rather have a proper (dbus?) interface for systemd-networkd to configure CAN (and networking in general).

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.

How to change SocketCan bitrate at runtime?
6 participants