python-can icon indicating copy to clipboard operation
python-can copied to clipboard

interface for changing bitrate

Open cperkulator opened this issue 3 years ago • 12 comments

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

cperkulator avatar Apr 23 '21 22:04 cperkulator

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     

codecov[bot] avatar Apr 23 '21 22:04 codecov[bot]

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.

felixdivo avatar Apr 26 '21 09:04 felixdivo

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.)

felixdivo avatar Apr 26 '21 16:04 felixdivo

Some questions:

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

zariiii9003 avatar Apr 28 '21 14:04 zariiii9003

  • 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.

felixdivo avatar May 11 '21 07:05 felixdivo

  • 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.

cperkulator avatar May 14 '21 16:05 cperkulator

@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?

zariiii9003 avatar Jun 04 '23 12:06 zariiii9003

@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.

hartkopp avatar Jun 04 '23 13:06 hartkopp

@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.

zariiii9003 avatar Jun 04 '23 14:06 zariiii9003

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 ?

hartkopp avatar Jun 04 '23 15:06 hartkopp

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.

lumagi avatar Jun 04 '23 17:06 lumagi

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).

marckleinebudde avatar Jun 06 '23 07:06 marckleinebudde