5

I want to read the currently configured CAN bitrate of my socketcan socket in C++.

I can see the bitrate with ip -det link show can0:

9: can0: <NOARP,UP,LOWER_UP,ECHO> mtu 16 qdisc pfifo_fast state UP mode DEFAULT group default qlen 10
    link/can  promiscuity 0 
    can state ERROR-ACTIVE restart-ms 100 
    bitrate 1000000 sample-point 0.750 
    tq 125 prop-seg 2 phase-seg1 3 phase-seg2 2 sjw 1
    pcan_usb: tseg1 1..16 tseg2 1..8 sjw 1..4 brp 1..64 brp-inc 1
    clock 8000000

The bitrate was set via /etc/network/interfaces, but the user could manually change it.

libsocketcan seems to only support setting the bitrate, but not reading it.

The code of iproute2 that produces the output above uses rtnetlink.

How could I use libnetlink to read the corresponding attribute? Or is there another way of reading the current bitrate?

Laurenz
  • 1,810
  • 12
  • 25

2 Answers2

4

For now, I went with intepreting the output of a system call to ip -det link show can0 | grep bitrate | awk '{print $2}', which is ugly but works.

Surely there is a more elegant solution?

Laurenz
  • 1,810
  • 12
  • 25
1

You can use can_get_bittiming() to get the set bitrate.

BЈовић
  • 62,405
  • 41
  • 173
  • 273