# CAN filter configuration to use along libcanard V1

**URL:** <https://forum.opencyphal.org/t/can-filter-configuration-to-use-along-libcanard-v1/1021>\
**Category:** Libcanard\
**Created:** [December 11, 2020, 2:56pm UTC](https://forum.opencyphal.org/t/can-filter-configuration-to-use-along-libcanard-v1/1021 "2020-12-11T14:56:28Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![noxuz](https://forum.opencyphal.org/user_avatar/forum.opencyphal.org/noxuz/32/397_2.png) [@noxuz](https://forum.opencyphal.org/u/noxuz)\
**Post date:** [December 11, 2020, 2:56pm UTC](https://forum.opencyphal.org/t/can-filter-configuration-to-use-along-libcanard-v1/1021/1 "2020-12-11T14:56:28Z")

</div>

Hello, I’m trying to configure the CAN reception filters for receiving 7609.Heartbeat.V1 messages in a mask + id filtering scheme.

For the simplest case of uniquely allowing this subject throughout the hardware filters, my first thought is to use (0x1FFF \<\< 8) for the mask and (0x1D55 \<\< 8) for the corresponding ID.

This comes from the CAN bit layout of V1 spec.

 ![image](https://forum.opencyphal.org/uploads/default/original/1X/a6bedca7d9aec5a6ea8226e35abf358102b5564f.png)

Is this approach accurate or which is the mask intented to use when using messages? The intrigue started when reviewing the below section of libuavcan v0, and found that the default mask as 0xFFFF80, which covers more than the 13 subject-ID bits on both sides; but couldn’t find the v0 pdf to compare the bit layout with v1.

> <https://github.com/OpenCyphal-Garage/libcyphal/blob/legacy-v0/libuavcan/include/uavcan/transport/can_acceptance_filter_configurator.hpp#L66>

Thanks beforehand.

---

<div class="post-metadata">

**Author:** ![pavel.kirienko](https://forum.opencyphal.org/user_avatar/forum.opencyphal.org/pavel.kirienko/32/9_2.png) [@pavel.kirienko](https://forum.opencyphal.org/u/pavel.kirienko)\
**Post date:** [December 11, 2020, 5:23pm UTC](https://forum.opencyphal.org/t/can-filter-configuration-to-use-along-libcanard-v1/1021/2 "2020-12-11T17:23:12Z")

</div>

Hey Abraham,

> [@noxuz](#):
>
> Is this approach accurate or which is the mask intented to use when using messages?

Yep, sounds right, should work.

> [@noxuz](#):
>
> The intrigue started when reviewing the below section of libuavcan v0, and found that the default mask as 0xFFFF80, which covers more than the 13 subject-ID bits on both sides; but couldn’t find the v0 pdf to compare the bit layout with v1.

The old bit layout is documented here: [4. CAN bus transport layer - UAVCAN](https://legacy.uavcan.org/Specification/4._CAN_bus_transport_layer/#can-frame-format). It is no longer valid so I don’t recommend looking at it.

Instead, you can refer to PyUAVCAN; in the internals under `pyuavcan.transport.can` you will find a function that computes the set of acceptance filter configurations for a given list of subject-identifiers:

> <https://github.com/OpenCyphal/pycyphal/blob/f43afb54e944e766a052957ac6f03310ecc97c82/pyuavcan/transport/can/_identifier.py#L163-L197>

Further, you may need to handle the case where the set of subject-IDs you are supposed to receive is larger than the set of the available acceptance filters. In this case, the coalescing algorithm that is described in the Spec in _“Automatic hardware acceptance filter configuration”_ and implemented in `pyuavcan.transport.can.media` may be of help:

> <https://github.com/OpenCyphal/pycyphal/blob/f43afb54e944e766a052957ac6f03310ecc97c82/pyuavcan/transport/can/media/_filter.py#L85-L115>

---

<div class="post-metadata">

**Author:** ![noxuz](https://forum.opencyphal.org/user_avatar/forum.opencyphal.org/noxuz/32/397_2.png) [@noxuz](https://forum.opencyphal.org/u/noxuz)\
**Post date:** [December 11, 2020, 8:27pm UTC](https://forum.opencyphal.org/t/can-filter-configuration-to-use-along-libcanard-v1/1021/3 "2020-12-11T20:27:26Z")

</div>

Perfect!

The default filter config I was looking for will end up with the ID field as (0x1FFF \<\< 8), and in the case of the mask, same as line #195 from the PyUAVCAN function you shared, adding 3 bits to the mask aside from the (0x1D55 \<\< 8).  
[https://github.com/UAVCAN/pyuavcan/blob/f43afb54e944e766a052957ac6f03310ecc97c82/pyuavcan/transport/can/\_identifier.py#L194](https://github.com/UAVCAN/pyuavcan/blob/f43afb54e944e766a052957ac6f03310ecc97c82/pyuavcan/transport/can/_identifier.py#L194)

I also found the migration guide below that clarifies why the bits 22 and 21 are being ignored in the mask. This completes the explanation of all the bits particular when dealing solely with messages, as in the simplest case mentioned earlier.

> [@Libcanard for UAVCAN/CAN v1 is released](https://forum.opencyphal.org/t/libcanard-for-uavcan-can-v1-is-released/774/30):
>
> Here is the migration guide from Libcanard v0.100 to the stable v1.0.0. It is super short: Update your subject-ID idenfiers by clearing the two most significant bits. The maximum subject-ID was 32767 (15 bits), it was contracted down to 8191 (13 bits) in the Specification v1.0-beta. For example, the subject-ID of 32085 (0x7d55) would become 7509 (0x1d55). Notice that in many cases, this change does not really affect wire compatiblity between v0.100 and v1.0.0 (unless low subject-ID values are…

I think this step of configuring the filter is the only one that is skipped from the libcanard example provided in the repo’s readme; and adding this step to the section that demonstrates how to setup reception could be of support for the documentation.

 ![image](https://forum.opencyphal.org/uploads/default/original/1X/a3e8419b14f0b127b03b4cc420e6b4eccab1b80b.png)  
Quick idea is a single line just below the previous section:

```auto
configureHWfilter(7509, 2**13 - 1); // ID-Mask pair for receiving Hearbeat messages 

```

Thank you Pavel for the explanation.

---

<div class="post-metadata">

**Author:** ![pavel.kirienko](https://forum.opencyphal.org/user_avatar/forum.opencyphal.org/pavel.kirienko/32/9_2.png) [@pavel.kirienko](https://forum.opencyphal.org/u/pavel.kirienko)\
**Post date:** [December 12, 2020, 1:03pm UTC](https://forum.opencyphal.org/t/can-filter-configuration-to-use-along-libcanard-v1/1021/4 "2020-12-12T13:03:54Z")

</div>

> [@noxuz](#):
>
> I think this step of configuring the filter is the only one that is skipped from the libcanard example provided in the repo’s readme; and adding this step to the section that demonstrates how to setup reception could be of support for the documentation.

I think it would be more valuable to add a short paragaph of text about acceptance filters. It is better than code because libcanard itself does not provide any facilities for acceptance filter configuration. A pull request with that change to the README would be accepted.
