Skip to content

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

Open
bIackr0se wants to merge 1 commit into
hardbyte:mainfrom
bIackr0se:fix/canutils-error-frames
Open

bIackr0se wants to merge 1 commit into
hardbyte:mainfrom
bIackr0se:fix/canutils-error-frames

Conversation

@bIackr0se

Copy link
Copy Markdown

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. Every other error class, 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 their class bits in arbitration_id, as the SocketCAN interface does, along with the data bytes, dlc, channel and direction. CanutilsLogWriter now encodes error frame IDs as CAN_ERR_FLAG | (msg.arbitration_id & 0x1FFFFFFF) while keeping CAN_ERR_BUSERROR defined.

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 and test_write_error_frame_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. Both tests fail on main and pass after the change. The existing shared error-frame round-trip fixtures continue to pass.

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.

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 with no class bits, such as Message(is_error_frame=True) or an ASC ErrorFrame, is now written as 20000000# instead of 20000080#. can-utils' log2asc reads it as an error frame. Older python-can releases read it as a data frame with ID 0, as they already do for every candump error frame without CAN_ERR_BUSERROR.
  • 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.
  • An error frame written by older python-can releases now reads back with arbitration_id equal to CAN_ERR_BUSERROR.

Local checks (macOS):

  • pytest test/logformats_test.py (Python 3.12, without asammdf): 85 passed, 18 skipped; 83 passed on main.
  • tox -e type (Python 3.14 environment): passed (mypy 3.11 to 3.15).
  • tox -e lint: black and ruff passed; pylint reports only five select no-member messages (epoll, EPOLLIN, EPOLLOUT) in can/interfaces/socketcan/socketcan.py, identical on main on macOS.
  • tox -e py: 505 passed, 36 failed; the same 36 tests (24 PCAN, 6 UDP multicast, 5 TRC, 1 SocketCAN capture) 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.

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.

1 participant