Skip to content

fix(mf4): convert 1-based BusChannel to 0-based Message.channel - #2166

Merged
zariiii9003 merged 2 commits into
hardbyte:mainfrom
rvyyv-n:fix/issue-2164
Nov 29, 2026
Merged

zariiii9003 merged 2 commits into
hardbyte:mainfrom
rvyyv-n:fix/issue-2164

Conversation

@rvyyv-n

@rvyyv-n rvyyv-n commented Nov 29, 2026

Copy link
Copy Markdown
Contributor

Summary of Changes

  • Updated MF4Reader (_CANDataFrameIterator, _CANErrorFrameIterator, _CANRemoteFrameIterator) to convert 1-based ASAM MDF4 BusChannel values (>= 1) to 0-based Message.channel values (bus_channel - 1), leaving Message.channel as None when BusChannel is 0 (unspecified).
  • Updated MF4Writer to accept a default 1-based channel: int = 1 parameter and write 1-based BusChannel values (channel + 1 for integer channels, or self.channel when unset), matching ASCReader/ASCWriter, BLFReader/BLFWriter, and TRCWriter.
  • Added regression tests covering MF4 1-based BusChannel reading, MF4Reader -> TRCWriter/ASCWriter conversion, and MF4Writer 1-based writing, and updated the BusChannel = 2 expectation in test_error_frame_payload from 2 to 1.
  • Added towncrier news fragment doc/changelog.d/2164.fixed.rst.

Root Cause

ASCReader/ASCWriter, BLFReader/BLFWriter, and TRCWriter all treat Message.channel as 0-based internally and convert to/from 1-based file bus numbers. MF4Reader previously passed 1-based ASAM MDF4 BusChannel values through unchanged (e.g., BusChannel = 1 -> Message.channel = 1), so converting .mf4 to .trc or .asc caused the writer's channel += 1 conversion to shift every bus number up by one (producing 2, 3, ... instead of 1, 2, ...).

Related Issues / Pull Requests

Type of Change

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

Checklist

  • Contribution guidelines followed.
  • Tests added and updated.
  • Documentation/docstrings updated.
  • News fragment added in doc/changelog.d/2164.fixed.rst.
  • Targeted test suite and linters pass.

Verification

  • pytest -o addopts="" test/logformats_test.py test/test_scripts.py: 164 passed, 6 skipped.
  • black --check can/io/mf4.py test/logformats_test.py: passed.
  • ruff check can/io/mf4.py: passed.
  • mypy --no-site-packages can/io/mf4.py: 0 errors in can/io/mf4.py.

@zariiii9003

Copy link
Copy Markdown
Collaborator

AI Review: fix(mf4): convert 1-based BusChannel to 0-based Message.channel

The fix is correct. BusChannel is 1-based in practice: asammdf's MDF.extract_bus_logging names decoded signals CAN{BusChannel} and treats bus_channel=0 as "all buses". This PR reads BusChannel = 1 as channel=0, consistent with the ASC and BLF readers, and converting an MF4 with BusChannel 1/2 via can.logconvert now yields buses 1/2 instead of 2/3. The writer now emits 1-based values; on main it wrote 0-based numbers and 0 for messages without a channel. Every changed reader/writer branch is guarded by a test that goes red when broken.

Requested changes

  • doc/changelog.d/2164.fixed.rst – document the compatibility impact and the new parameter. Files written by earlier MF4Writer versions contain 0-based channel numbers; with this PR they read back one lower (channel=0 → None, 1 → 0). doc/file_io.rst states that MF4Reader targets files created by MF4Writer, so users re-reading old logs are affected. Please state this, and mention the new MF4Writer(channel=...) parameter (e.g. in an additional 2164.added.rst).
  • test/logformats_test.py::test_bus_channel_one_based_conversion – drop the TRC/ASC section (trc_path/asc_path and the TRCWriter/ASCWriter/ASCReader assertions). It mostly re-tests the TRC and ASC writers; the [0, 1, None, 1] assertion already pins the reader behaviour.
  • test_writer_writes_one_based_bus_channel – drop the final MF4Reader read-back. The reader conversion is covered by the test above and by test_error_frame_payload. With both removals, breaking any of the reader - 1, the > 0 guard, the writer + 1, or the None default still fails a test.

Verdict

request changes

@rvyyv-n

rvyyv-n commented Nov 29, 2026

Copy link
Copy Markdown
Contributor Author

Thanks for the review! Addressed all three items in 05929ad:

  • Updated \doc/changelog.d/2164.fixed.rst\ to note the compatibility impact on .mf4\ files written by earlier \MF4Writer\ versions, and added \doc/changelog.d/2164.added.rst\ documenting the new \MF4Writer(channel=...)\ parameter.
  • Removed the redundant TRC/ASC section from \ est_bus_channel_one_based_conversion.
  • Removed the redundant \MF4Reader\ read-back from \ est_writer_writes_one_based_bus_channel.

@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

@zariiii9003
zariiii9003 merged commit d5800d0 into hardbyte:main Nov 29, 2026
26 of 27 checks passed
@rvyyv-n

rvyyv-n commented Dec 1, 2026

Copy link
Copy Markdown
Contributor Author

you're welcome! thanks for the quick review.

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.

can_logconvert from mf4 to trc produces bus# raised by one

2 participants