Skip to content

canutils: read candump error frames of every class as error frames - #2146

Merged
zariiii9003 merged 2 commits into
hardbyte:mainfrom
bIackr0se:fix/canutils-error-frames
Nov 22, 2026
Merged

zariiii9003 merged 2 commits into
hardbyte:mainfrom
bIackr0se:fix/canutils-error-frames

Conversation

@bIackr0se

@bIackr0se bIackr0se commented Nov 21, 2026 •

Copy link
Copy Markdown
Contributor

Summary of Changes

CanutilsLogReader treats a candump -L line as an error frame only when both CAN_ERR_FLAG (0x20000000) and CAN_ERR_BUSERROR (0x80) are set. An error frame whose class bits lack CAN_ERR_BUSERROR, such as a controller problem (0x04) or bus off (0x40), is read as an extended data frame. can.logconvert writes a controller-problem frame (0x04) as an ASC data frame instead of ErrorFrame. can.player without --error-frames transmits that frame onto the bus as a data frame. For bus error frames, CanutilsLogReader drops channel, direction, dlc, and the 8 data bytes. CanutilsLogWriter also ignores arbitration_id on error frames and always writes the bus error class.

In can/io/canutils.py, CanutilsLogReader now sets is_error_frame=bool(can_id & CAN_ERR_FLAG), which is how can/interfaces/socketcan/socketcan.py and can-utils' log2asc recognise an error frame. The reader builds one Message for every frame line. Error frames keep the low 29 bits of the candump ID in arbitration_id (for the defined error classes this matches the SocketCAN interface), along with the data bytes, dlc, channel and direction. CanutilsLogWriter encodes error frames that have SocketCAN error class bits as CAN_ERR_FLAG | (msg.arbitration_id & 0x1FFFFFFF). When arbitration_id & 0x1FFFFFFF is 0, as for can.Message(is_error_frame=True), ASC ErrorFrame lines and BLF error frames without an ID, it writes CAN_ERR_BUSERROR, so the line is 20000080# as on main. Writing 20000080# avoids 20000000#, which released python-can versions (2.0.0 to 4.6.1) read as a data frame with ID 0.

Input (err.log):

(1700000000.000000) can0 20000004#0004000000000000
(1700000000.100000) can0 20000080#0000000000000000

Before (main):

# CanutilsLogReader
err=False id=0x4 ext=True dlc=8 data=0004000000000000 channel='can0'
err=True id=0x0 dlc=0 data= channel=None

# python -m can.logconvert err.log err.asc (controller-problem line)
 0.000000 1  4x              Rx   d 8 00 04 00 00 00 00 00 00

# python -m can.player -i virtual -c x -v --ignore-timestamps err.log
Timestamp: 1700000000.000000    ID: 00000004    X Rx                DL:  8    00 04 00 00 00 00 00 00     Channel: can0

After:

# CanutilsLogReader
err=True id=0x4 ext=True dlc=8 data=0004000000000000 channel='can0' rx=True
err=True id=0x80 ext=True dlc=8 data=0000000000000000 channel='can0' rx=True

# python -m can.logconvert err.log err.asc (controller-problem line)
 0.000000 1  ErrorFrame

# python -m can.player -i virtual -c x -v --ignore-timestamps err.log
(nothing sent)

TestCanutilsFileFormat in test/logformats_test.py now includes test_read_error_frames, test_write_error_frame_class, and test_write_error_frame_without_class. test_read_error_frames verifies that CanutilsLogReader preserves class, data, and channel on candump error lines. test_write_error_frame_class verifies that an error frame with class 0x204 writes as 20000204#0004000000000102 and reads back equal. test_write_error_frame_without_class verifies that Message(timestamp=1.0, is_error_frame=True, channel="can0") writes (1.000000) can0 20000080# and reads back as an error frame with arbitration_id 0x080. The first two tests fail on main and pass after the change. test_write_error_frame_without_class fails on the previous PR head 4c172a7, which writes 20000000#, and passes on 0a360c5. TestCanutilsFileFormat now passes check_error_frames=False to the shared reader/writer round-trip helper, as TestTrcFileFormatBase already does. The shared fixtures include Message(is_error_frame=True) with arbitration_id 0, which now reads back with arbitration_id equal to CAN_ERR_BUSERROR, so the generic round trip cannot hold for it. Dedicated canutils tests cover error frames instead.

Additional checks:

  • All 59 error lines built from can-utils' format (single classes, class pairs, bare CAN_ERR_FLAG, empty-data bus errors, and R/ T suffixes) fail on main and pass after the change.
  • All 15 data and remote frame lines (including extended ID 0x00000080) read identically on main and after the change.
  • A public 2,170-line log in this format from an onboard-charger test project, converted .log to .log with can.logconvert, comes back line for line apart from the writer's R direction suffix. On main its two bus error frames come back as vcan0 20000080#, without channel or data bytes. Its ASC conversion is the same on main and after the change, apart from the generated date header.
  • Converting the three ASC fixtures with error frames (test/data/logfile.asc, logfile_errorframes.asc, and test_CanErrorFrames.asc, 11 error frames in total, all with arbitration_id 0) to candump format and reading the output with main's reader recognises 11 of 11 as error frames on 0a360c5 (0 of 11 on 4c172a7).

Related Issues / Pull Requests

Type of Change

  • Bug fix
  • New feature
  • Documentation update
  • Refactoring
  • Other (please describe):

Checklist

  • I have followed the contribution guide.
  • I have added or updated tests as appropriate.
  • I have added or updated documentation as appropriate (news fragment; no API change).
  • I have added a news fragment for towncrier.
  • All checks and tests pass (tox).

Additional Notes

Compatibility:

  • An error frame without class bits is written as 20000080#, as on main, and reads back with arbitration_id 0x080 (on main it reads back with arbitration_id 0). An error frame whose class bits lack CAN_ERR_BUSERROR (for example 20000204#) is still read as a data frame by released python-can versions, as main already does for candump's own error frames of those classes.
  • An error frame read from another format keeps its arbitration_id, and CanutilsLogWriter writes its low 29 bits after CAN_ERR_FLAG as the error class when non-zero (and CAN_ERR_BUSERROR when 0).
  • An error frame written by older python-can releases now reads back with arbitration_id equal to CAN_ERR_BUSERROR.

Local checks (0a360c5, macOS):

  • pytest test/logformats_test.py (Python 3.12 venv, without asammdf): 86 passed, 18 skipped.
  • tox -e type: passed.
  • tox -e lint: black and ruff passed; pylint reports only the five select no-member messages (epoll, EPOLLIN, EPOLLOUT) in can/interfaces/socketcan/socketcan.py, identical on main on macOS.
  • tox -e py: 536 passed, 117 skipped, 6 failed; the 6 are the BasicTestUdpMulticastBusIPv4 tests in test/back2back_test.py, which also fail on main on macOS.

candump writes an error frame as CAN_ERR_FLAG plus the error class bits,
followed by the error data bytes. The reader only recognised the bus error
class, so a controller problem (20000004#...) or bus off frame was read as
an extended data frame, and a bus error frame lost its channel, direction
and data bytes.

Test CAN_ERR_FLAG alone and keep the class bits in arbitration_id, as the
SocketCAN interface does, together with the data bytes, channel and
direction. The writer now writes the message's error class instead of
always writing the bus error class, so an error frame round-trips.
@chatgpt-codex-connector

Copy link
Copy Markdown
Codex Review Summary: ❌ Failed

This comment shows the latest Codex review activity on this pull request.

Review Status Commit Review trigger
📝 Code Review ❌ Failed 2026-11-21T21:26:57.908886Z
We couldn’t complete this review. Comment @codex review to try again.
4c172a7 PR opened
ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review" or "@codex security review".

Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings.

@chatgpt-codex-connector

Copy link
Copy Markdown

You have reached your Codex usage limits for code reviews. You can see your limits in the Codex usage dashboard.
To continue using code reviews, you can upgrade your account or add credits to your account and enable them for code reviews in your settings.

@zariiii9003

Copy link
Copy Markdown
Collaborator

AI Review:

On main, the reader only treats a candump line as an error frame if its ID has the CAN_ERR_BUSERROR bit (0x080) set. Error frames with any other SocketCAN error-class bit, such as CAN_ERR_CRTL (0x004, controller problem) or CAN_ERR_BUSOFF (0x040), are read as extended data frames, so can.logconvert writes them as data and can.player sends them onto the bus. Recognising error frames by CAN_ERR_FLAG alone and keeping the error-class bits in arbitration_id matches the SocketCAN interface. Tests pass, and both new tests fail on main.

Regression in the writer

Problem: an error frame whose arbitration_id is 0 has no SocketCAN error-class bits set. That is the case for can.Message(is_error_frame=True) and for error frames read from ASC or BLF logs. This branch writes such a frame as 20000000#, where main writes 20000080#. Every released python-can reads 20000000# as a data frame with ID 0. I checked this by converting test/data/logfile_errorframes.asc: main's reader finds 3 error frames in today's output and none in this branch's.

Fix: if no error-class bits are set, write CAN_ERR_BUSERROR, as main does today:

error_class = msg.arbitration_id & 0x1FFFFFFF
if not error_class:
    # no SocketCAN error-class bits: write CAN_ERR_BUSERROR (0x080),
    # so older readers still recognise the line as an error frame
    error_class = CAN_ERR_BUSERROR
framestr += f" {CAN_ERR_FLAG | error_class:08X}#"

Also

  • Rename the changelog fragment to 2146.fixed.rst.

Older python-can readers recognise a candump error frame only by
CAN_ERR_BUSERROR, so an error frame without class bits (as read from
ASC or BLF) keeps the bus error class on write. The shared error frame
round trip no longer holds for such frames; dedicated tests cover it.
Rename the news fragment to the PR number.
@bIackr0se

Copy link
Copy Markdown
Contributor Author

I applied your writer fix in 0a360c5 so that error frames with no error class bits write CAN_ERR_BUSERROR. I renamed the changelog fragment to 2146.fixed.rst, which now also states the bus-error fallback. I updated the PR description to remove the 20000000# compatibility note.

The previous head (4c172a7) wrote 20000000# for logfile_errorframes.asc, and main's reader recognised 0 of 3 error frames. 0a360c5 writes 20000080#, and main's reader recognises 3 of 3. Across the three ASC fixtures with error frames, main's reader recognises 11 of 11 after the fix compared with 0 of 11 before.

I added test_write_error_frame_without_class to check that Message(is_error_frame=True) writes 20000080# and reads back as an error frame with arbitration_id 0x080. It fails on 4c172a7 and passes now.

With the fix, the shared round-trip fixtures' Message(is_error_frame=True) (ID 0) reads back with arbitration_id 0x080, so 5 generic round-trip tests in TestCanutilsFileFormat failed on this one mismatch. I updated TestCanutilsFileFormat to pass check_error_frames=False, as TestTrcFileFormatBase already does, and the dedicated canutils tests cover error frames. If you prefer another approach, such as reading a bare 20000080# back with ID 0, I can change it.

On 0a360c5, test/logformats_test.py reports 86 passed and 18 skipped. The tox type check passes. Lint and the full test run show only failures that main has on macOS (pylint select.epoll, UDP multicast tests).

@zariiii9003 zariiii9003 left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you. Although this highlights a problem: some parts of python-can use arbitration_id to communicate error types while it was never a documented part of the API. So these changes might cause issues with non-linux interfaces.

@zariiii9003
zariiii9003 merged commit 71c8da0 into hardbyte:main Nov 22, 2026
28 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants