On busy CAN buses, it can be desirable to filter for only frames that are of importance to the the receiving controller. This filtering is done by arbitration identifier of the CAN frame. This technique is also used when monitoring a CAN bus, in order to focus in on messages of importance.
The most common method of filtering CAN frames is by using an identifier and mask. These allow a range of IDs to be accepted with a single filter rule.
When a CAN frame is received, the mask is applied. This determines which bits of the identifier will be used to determine if the frame matches the filter. All bits set to '1' in the mask will be used in the comparison.
After the mask is applied, the specified bits of the received frame's identifier is compared to the filter's identifier. If there is a match, the controller receives the message.
The simplest example is a filter which excepts exactly one identifier. For this filter, the identifier will be set to the identifier we want to receive, and the mask will be set to all ones (0x7FF for standard identifier frames). For example
identifier = 0x123, mask = 0x7FF -> matches only frames with identifier 0x123
If we want to match more than one ID, we will need to set some bits in the mask to zero. For example:
identifier = 0x123, mask = 0x7F0 -> matches frames with identifiers 0x120, 0x121, 0x122, 0x123, ..., 0x12F
Setting the mask to 0 implies we care about none of the bits. In practice, this filter will match every frame received.
Filtering with candump
When using the
candump utility, you must specify filters as an identifier and mask. For example, to receive only frames with identifier 0x100 on the can0 interface:
To receive all frames between 0x100 and 0x1FF inclusive:
candump utility also allows for applying negated filters, which receive a message when the filter rule is not matched. For example, to matche all frames except those with identifiers between 0x100 and 0x1FF inclusive: