Repository navigation
cli: keep the mask of inverted --filter arguments - #2182
thomas0829 wants to merge 1 commit into
Conversation
AI Review: cli: keep the mask of inverted --filter argumentsThe mask fix is correct for SocketCAN: Findings
Verdictrequest changes |
|
The socketcan side looks right to me, candump does the same One thing I noticed on non socketcan interfaces though. Those use the software filter in BusABC._matches_filters, which doesn't know about CAN_INV_FILTER. I tried
So on those interfaces the inverted filter now acts like a normal |
_CanFilterAction ANDed the mask of "<can_id>~<can_mask>" with CAN_ERR_FLAG instead of ~CAN_ERR_FLAG, so the mask was always 0 and the inverted SocketCAN filter blocked every frame. Clear only the error flag, as candump does. CAN_INV_FILTER is only understood by SocketCAN. For any other interface, create_bus_from_namespace now sets the mask of such a filter to 0, so the filter lets every frame through as before instead of matching only the excluded ID. Fixes hardbyte#2181
1cc7a60 to
2a3758a
Compare
|
Thank you both for the careful review, and thanks to @DanShinde for checking the virtual bus. You are right that passing the corrected mask to interfaces other than SocketCAN would make
Results: full The interface-agnostic version from the review (an |
Summary of Changes
CAN_ERR_FLAGfrom the mask of an inverted--filter <can_id>~<can_mask>argument instead of keeping only that bit, so123~7FFgivescan_mask=0x7FFinstead of0. On SocketCAN the kernel now inverts the filter as intended.CAN_INV_FILTERis only understood by SocketCAN. For every other interface,create_bus_from_namespacesets the mask of such a filter to 0 and logs a warning, so the filter lets every frame through exactly as onmain(no behaviour change there).test/test_cli.pyandtest/test_viewer.py, which asserted the old value, and add tests for both cases.doc/changelog.d/2181.fixed.rst.Why
With
can_mask=0, the Linux kernel stores the inverted filter ascan_id & 0and never delivers a frame, because(rx_id & 0) != 0is never true.candumpparses the same syntax withcan_mask &= ~CAN_ERR_FLAG, and the--filterhelp text describes the!=match.Software filtering in
BusABC._matches_filtersand the hardware filters of other backends do not knowCAN_INV_FILTER. Passing the corrected mask to them would make--filter 123~7FFaccept only 0x123 (reported in review), so this PR keeps them at their current behaviour. A genericinvertedfilter key honoured by every backend would be a separate follow-up.Validation
pytest(CPython 3.13, all extras): 998 tests, 0 failures, 99 skippedtest/test_cli.pyfail without the interface check: SocketCAN gets the inverted mask, other interfaces get mask 0 (interface given as argument or viaCAN_INTERFACE), and on a virtual bus--filter 123~7FFreceives both 0x123 and 0x456 as onmain;123~200007FFgives mask0x7FFblack --check .,ruff check can examples doc,pylint can/cli.py can/logger.py can/viewer.py: cleanmypy --python-version 3.12 .: no new errors (the 7 incan/io/mf4.pyalso show onmain)Related Issues / Pull Requests
Type of Change
Checklist
tox).Additional Notes
Tested by parsing and with mocked/virtual buses only; no SocketCAN device was available.