Search code examples
linuxcan-bussocketcancanutils

Heavily confused by candump (SocketCAN) ID filtering feature


In a nutshell, I want candump to show me ONLY frames with IDs 0x00200200 or 0x255.

So I do this:

candump can0,00200200:0,255:0

But this gives ALL frames, and each frame is shown twice. i.e. the output of:

cansend can0 256#112233

would be this:

can0      256  [3] 11 22 33
can0      256  [3] 11 22 33

Aside from the filter not behaving like I expected and passing through 0x256, the fact that it shows up twice suggests that this frames is actually matched by both filters, which makes even less sense to me. Can anyone explain why this is happening, and perhaps show me the correct way to do it?


Solution

  • From the help of candump:

    <can_id>:<can_mask> (matches when <received_can_id> & mask == can_id & mask)
    

    Now, when the mask is 0, every CAN ID will match it. So the can_id has no real effect, this is why all messages pass, each required bit in the can id should be set to 1 in the mask.

    Regarding the duplication problem, it probably happens because you use two filters, although I am not sure about this.

    What you want to do is:

    candump can0,00200200:1fffffff,255:7ff
    

    Example (provided by OP):

    enyquist:~$ candump vcan0,00200200:1fffffff,255:7ff &
    [1] 7339 
    enyquist:~$ cansend vcan0 002001fe#1122 
    enyquist:~$ cansend vcan0 002001ff#1122 
    enyquist:~$ cansend vcan0 00200200#1122
    vcan0 00200200 [2] 11 22 
    enyquist:~$ cansend vcan0 00200201#1122 
    enyquist:~$ cansend vcan0 00200202#1122 
    enyquist:~$ 
    enyquist:~$ cansend vcan0 253#1122 
    enyquist:~$ cansend vcan0 254#1122 
    enyquist:~$ cansend vcan0 255#1122
    vcan0 255 [2] 11 22 
    enyquist:~$ cansend vcan0 256#1122 
    enyquist:~$ cansend vcan0 257#1122 
    enyquist:~$