Skip to contentSkip to conversation

Setting bitrate from user-space application

DISCUSSION Guillaume Picquet 6 participants 10 messages Nov 25, 2015 – Dec 23, 2015 CAN network drivers

lore
Share & tools
lore
#0 lore raw

Hi all,

I prefer to ask first to those who know.

What is the 'normal' way of setting CAN bitrate from user-space application ? Is there a library for that ?
If no shall I use netlink ?
if yes Can I use libnl ?

Thanks in advance for showing me where to dig internet.

regards.

#1 lore raw

Hi Picquet,

You can use can-utils.

Best regards

Pankaj Kumar Misra
RBEI/EEA2

Tel. +91(80)6783-6643

-----Original Message-----
From: [email protected] [mailto:[email protected]] On Behalf Of Guillaume Picquet
Sent: Wednesday, November 25, 2015 3:02 PM
To: [email protected]
Subject: Setting bitrate from user-space application

Hi all,

I prefer to ask first to those who know.

What is the 'normal' way of setting CAN bitrate from user-space application ?
Is there a library for that ?
If no shall I use netlink ?
if yes Can I use libnl ?

Thanks in advance for showing me where to dig internet.

regards.
--
To unsubscribe from this list: send the line "unsubscribe linux-can" in the body of a message to [email protected]
More majordomo info at http://vger.kernel.org/majordomo-info.html

#2 lore raw
In reply to Guillaume Picquet
Quoted 7 lines · Guillaume Picquet

Hi all,

I prefer to ask first to those who know.

What is the 'normal' way of setting CAN bitrate from user-space application ?
Is there a library for that ?

Yes -
http://www.pengutronix.de/software/libsocketcan/download/libsocketcan-0.0.10.tar.bz2

Quoted 1 line

If no shall I use netlink ?

No - netlink parsing makes your brain hurt :D

Marc

Signature · 4 lines
Pengutronix e.K.                  | Marc Kleine-Budde           |
Industrial Linux Solutions        | Phone: +49-231-2826-924     |
Vertretung West/Dortmund          | Fax:   +49-5121-206917-5555 |
Amtsgericht Hildesheim, HRA 2686  | http://www.pengutronix.de   |
#3 lore raw

Hi Guillaume,

Quoted 9 lines · Marc Kleine-Budde
Quoted 7 lines · Guillaume Picquet

Hi all,

I prefer to ask first to those who know.

What is the 'normal' way of setting CAN bitrate from user-space application ?
Is there a library for that ?

Quoted 7 lines

Yes -
http://www.pengutronix.de/software/libsocketcan/download/libsocketcan-0.0.10.tar.bz2

Quoted 1 line

If no shall I use netlink ?

No - netlink parsing makes your brain hurt :D

Using libsocketcan is the way to control the interfaces by an application (which internally uses netlink).

The usual way to configure the interfaces system wide is the 'ip' tool from the iproute2 package which comes with your preferred Linux distribution.

The way to configure CAN interfaces with 'ip' is described here:

https://www.kernel.org/doc/Documentation/networking/can.txt (see chapter 6.5)

E.g. you can use this script in Debian/Ubuntu setups then:

https://github.com/linux-can/can-misc/blob/master/etc/can_if

Regards,
Oliver

#4 lore raw
Quoted 6 lines · Oliver Hartkopp

Using libsocketcan is the way to control the interfaces by an application (which internally uses netlink).

The usual way to configure the interfaces system wide is the 'ip' tool from the iproute2 package which comes with your preferred Linux distribution.

Quoted 7 lines

The way to configure CAN interfaces with 'ip' is described here:

https://www.kernel.org/doc/Documentation/networking/can.txt (see chapter 6.5)

E.g. you can use this script in Debian/Ubuntu setups then:

https://github.com/linux-can/can-misc/blob/master/etc/can_if

'seems we need systemd-networkd support for CAN.

Marc

Signature · 4 lines
Pengutronix e.K.                  | Marc Kleine-Budde           |
Industrial Linux Solutions        | Phone: +49-231-2826-924     |
Vertretung West/Dortmund          | Fax:   +49-5121-206917-5555 |
Amtsgericht Hildesheim, HRA 2686  | http://www.pengutronix.de   |
#5 lore raw
In reply to Oliver Hartkopp
Quoted 11 lines · Oliver Hartkopp

Hi Guillaume,

Quoted 8 lines · Marc Kleine-Budde
Quoted 7 lines · Guillaume Picquet

Hi all,

I prefer to ask first to those who know.

What is the 'normal' way of setting CAN bitrate from user-space application ?
Is there a library for that ?

Quoted 8 lines
Quoted 6 lines

Yes -
http://www.pengutronix.de/software/libsocketcan/download/libsocketcan-0.0.10.tar.bz2

Quoted 1 line

If no shall I use netlink ?

No - netlink parsing makes your brain hurt :D

Using libsocketcan is the way to control the interfaces by an application (which internally uses netlink).

I've downloaded it, cross-compile it and made a test program. I read bitrate successfully.

Quoted 3 lines

The usual way to configure the interfaces system wide is the 'ip' tool from the iproute2 package which comes with your preferred Linux distribution.

On the platform I'am working it's done in a rc script at start-up

Quoted 11 lines

The way to configure CAN interfaces with 'ip' is described here:

https://www.kernel.org/doc/Documentation/networking/can.txt (see chapter 6.5)

E.g. you can use this script in Debian/Ubuntu setups then:

https://github.com/linux-can/can-misc/blob/master/etc/can_if

Regards,
Oliver

Regards,

guillaume

#6 lore raw
Quoted 11 lines · Guillaume Picquet
Quoted 10 lines · Oliver Hartkopp

Hi Guillaume,

Quoted 7 lines · Marc Kleine-Budde
Quoted 6 lines · Guillaume Picquet

Hi all,

I prefer to ask first to those who know.

What is the 'normal' way of setting CAN bitrate from user-space application ?

The "abnormal" way I do it is basically:

char *pName = (a_eDevice == CAN_BUS_CAN_0) ? "can0" : "can1"; sprintf(vBuf,
"/usr/local/sbin/canconfig %s stop > /dev/null ; " "/usr/local/sbin/canconfig %s bitrate %d > /dev/null ; " "/usr/local/sbin/canconfig %s start > /dev/null\n", pName, pName, a_nKbaud * 1000, pName);
nRc = system(vBuf);

Or if using Freescale's 2.6.35 driver, "ifconfig can0 down/up" and
"echo 500000 > /sys/class/devices/platform/FlexCAN.0/bitrate" in the above "system()" call.

I realise the above may be horrible, but I'd like some more information on why it might be bad.

Tom

#7 lore raw
Quoted 6 lines

Or if using Freescale's 2.6.35 driver, "ifconfig can0 down/up" and "echo 500000 > /sys/class/devices/platform/FlexCAN.0/bitrate" in the above "system()" call.

I realise the above may be horrible, but I'd like some more information on why it might be bad.

a) it will only work with this specific FlexCAN driver from this proprietary kernel
b) if you ever are forced to upgrade to, say, 4.4, it won't work

Whereas "ip link set can0 type can bitrate 500000" will work with any card (even USB dongles that you might use as test equipment), and with any kernel.

As an added bonus, it can even set things like "restart-ms" :-)

#8 lore raw
In reply to Guillaume Picquet

Hi all!

A status update, thanks to libsocketcan I am able to get and set bitrate.
remarks:
Before setting bitrate a stop must be performed.
I use poll() on CAN socket and it returns and set errno to ENETDOWN during the stop but sockets stay valid. (no need to close and reopen). I Suppose it is normal behaviour, right ?

Thanks for support.

Regards.

Quoted 48 lines · Guillaume Picquet
Quoted 15 lines · Oliver Hartkopp

Hi Guillaume,

Quoted 12 lines · Marc Kleine-Budde
Quoted 7 lines · Guillaume Picquet

Hi all,

I prefer to ask first to those who know.

What is the 'normal' way of setting CAN bitrate from user-space application ?
Is there a library for that ?

Yes -
http://www.pengutronix.de/software/libsocketcan/download/libsocketcan-0.0.10.tar.bz2

Quoted 5 lines
Quoted 3 lines
Quoted 1 line

If no shall I use netlink ?

No - netlink parsing makes your brain hurt :D

Using libsocketcan is the way to control the interfaces by an application (which internally uses netlink).

I've downloaded it, cross-compile it and made a test program. I read bitrate successfully.

Quoted 3 lines

The usual way to configure the interfaces system wide is the 'ip' tool from the iproute2 package which comes with your preferred Linux distribution.

On the platform I'am working it's done in a rc script at start-up

Quoted 11 lines

The way to configure CAN interfaces with 'ip' is described here:

https://www.kernel.org/doc/Documentation/networking/can.txt (see chapter 6.5)

E.g. you can use this script in Debian/Ubuntu setups then:

https://github.com/linux-can/can-misc/blob/master/etc/can_if

Regards,
Oliver

Regards,

guillaume

Signature · 3 lines
To unsubscribe from this list: send the line "unsubscribe linux-can" in
the body of a message to [email protected]
More majordomo info at  http://vger.kernel.org/majordomo-info.html
#9 lore raw
Quoted 1 line · Guillaume Picquet

A status update, thanks to libsocketcan I am able to get and set bitrate.

\o/

Quoted 2 lines

remarks:
Before setting bitrate a stop must be performed.

Yes - this way you can be sure that the new frames are send with the new bitrate. Also think of a multi application CAN environment.

Quoted 3 lines

I use poll() on CAN socket and it returns and set errno to ENETDOWN during the stop but sockets stay valid. (no need to close and reopen). I Suppose it is normal behaviour, right ?

ENETDOWN is expected behaviour. That you don't need to close and reopen the socket is.....nice, isn't it :) Let's say it's implementation defined behaviour. Better close and reopen the socket.

Marc

Signature · 4 lines
Pengutronix e.K.                  | Marc Kleine-Budde           |
Industrial Linux Solutions        | Phone: +49-231-2826-924     |
Vertretung West/Dortmund          | Fax:   +49-5121-206917-5555 |
Amtsgericht Hildesheim, HRA 2686  | http://www.pengutronix.de   |