Changing bitrate of SocketCAN device in user space

This forum covers PCAN-Linux and Linux development issues concerning our products
Post Reply
abaldur
Posts: 4
Joined: Tue 21. Apr 2020, 13:36

Changing bitrate of SocketCAN device in user space

Post by abaldur » Tue 21. Apr 2020, 14:24

I have a use case where I need to change the bitrate of my CAN device on-the-fly.

I would like to use SocketCAN if possible, but it seems to be best suited for use cases where the bus configuration is fixed from the start, i.e. fixed bitrate and then just started.

I'm currently experimenting with the libsocketcan library to use can_do_start, can_do_stop and can_set_bitrate, but they seem to require super user permissions to execute.

Is there any ways of allowing the normal user to run start/stop and set bitrate without super user?

Best Regards
abaldur

M.Heidemann
Sales & Support
Sales & Support
Posts: 1083
Joined: Fri 20. Sep 2019, 13:31

Re: Changing bitrate of SocketCAN device in user space

Post by M.Heidemann » Tue 21. Apr 2020, 15:09

Hello,

For changing device configurations SocketCAN requires a user to have root priviliges.

You can specify certain commands in the sudoers file (/etc/sudoers) which the user is allowed to execute as sudo:

You can group the commands you want the user to use as sudo, for example:

Code: Select all

Cmnd_Alias SocketCANCommands = /PathToYouSocketCANApplication/YourApplication, /PathToAnotherSocketCANApplication/YourOtherApplication
Then you can even disable the password-prompt, if needed.

Code: Select all

 Username ALL=(ALL) NOPASSWD: SocketCANCommands

Alternatively you can simply edit /etc/networking/interfaces(Debian based Distros):

Code: Select all


auto can0

iface can0 inet manual

pre-up /sbin/ip link set $IFACE type can bitrate 250000

up /sbin/ifconfig $IFACE up

down /sbin/ifconfig $IFACE down
        
Or try to use a sudoer by default

Best Regards

Marvin
---
Marvin Heidemann
PEAK-Support Team

abaldur
Posts: 4
Joined: Tue 21. Apr 2020, 13:36

Re: Changing bitrate of SocketCAN device in user space

Post by abaldur » Tue 21. Apr 2020, 18:34

Hi Marvin,

Thank you for the answer, it was very helpful.

The proposed solution could work, although it feels a bit clumsy and I'm not sure its the right way to go for me.

To clarify, I'm working on an application that use CAN bus in two different ways.

In the startup phase I need to do fw update of the nodes connected to the bus. It is a very delicate phase where a lot of control is required. Here I would prefer using libpcan or libpcanbasic as it seems like I get all that.

But when that phase is completed, I would like to use the SocketCAN as it is very nice to write applications on top of and has some good command line utilies that comes for free.

So right now I even consider the possibility of loading the chardev driver for the fw update phase, and when that is done, loading the netdev driver.

What are your thoughts on that? Have you heard about this kind of use cases?

Best Regards,
/abaldur

M.Heidemann
Sales & Support
Sales & Support
Posts: 1083
Joined: Fri 20. Sep 2019, 13:31

Re: Changing bitrate of SocketCAN device in user space

Post by M.Heidemann » Wed 22. Apr 2020, 07:55

Hello,

Your proposal would require you to automate the build and installation for the target driver type,
this also would require you to have root privileges.

The drivers (chardev/netdev) can not be installed at the same time and need to be swapped by uninstalling the current and reinstalling the intended driver.

Other than that there is no reason why this should'nt work.

Also consider not swapping to netdev at all, if not necessary. Have a look at the PCANBasic examples supplied with the driver download.

The examples for c++ represent the most basic functions and can be used with little to no modifcation at all.

If you want an example with a UI that shows the whole range of functionality PCANBasic has to offer the python examples are the right choice.


Best Regards

Marvin
---
Marvin Heidemann
PEAK-Support Team

Post Reply