Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

BlfReader: Fix CAN_FD_MESSAGE_64 data padding #1906

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 16 additions & 5 deletions can/io/blf.py
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ def timestamp_to_systemtime(timestamp: float) -> TSystemTime:
if timestamp is None or timestamp < 631152000:
# Probably not a Unix timestamp
return 0, 0, 0, 0, 0, 0, 0, 0
t = datetime.datetime.fromtimestamp(round(timestamp, 3))
t = datetime.datetime.fromtimestamp(round(timestamp, 3), tz=datetime.timezone.utc)
return (
t.year,
t.month,
Expand All @@ -126,6 +126,7 @@ def systemtime_to_timestamp(systemtime: TSystemTime) -> float:
systemtime[5],
systemtime[6],
systemtime[7] * 1000,
tzinfo=datetime.timezone.utc,
)
return t.timestamp()
except ValueError:
Expand Down Expand Up @@ -239,7 +240,7 @@ def _parse_data(self, data):
raise BLFParseError("Could not find next object") from None
header = unpack_obj_header_base(data, pos)
# print(header)
signature, _, header_version, obj_size, obj_type = header
signature, header_size, header_version, obj_size, obj_type = header
if signature != b"LOBJ":
raise BLFParseError()

Expand Down Expand Up @@ -334,10 +335,20 @@ def _parse_data(self, data):
_,
_,
direction,
_,
ext_data_offset,
_,
) = unpack_can_fd_64_msg(data, pos)
pos += can_fd_64_msg_size

# :issue:`1905`: `valid_bytes` can be higher than the actually available data.
# Add zero-byte padding to mimic behavior of CANoe and binlog.dll.
data_field_length = min(
valid_bytes,
(ext_data_offset or obj_size) - header_size - can_fd_64_msg_size,
)
msg_data_offset = pos + can_fd_64_msg_size
msg_data = data[msg_data_offset : msg_data_offset + data_field_length]
msg_data = msg_data.ljust(valid_bytes, b"\x00")

yield Message(
timestamp=timestamp,
arbitration_id=can_id & 0x1FFFFFFF,
Expand All @@ -348,7 +359,7 @@ def _parse_data(self, data):
bitrate_switch=bool(fd_flags & 0x2000),
error_state_indicator=bool(fd_flags & 0x4000),
dlc=dlc2len(dlc),
data=data[pos : pos + valid_bytes],
data=msg_data,
channel=channel - 1,
)

Expand Down
Binary file added test/data/issue_1905.blf
Binary file not shown.
24 changes: 24 additions & 0 deletions test/logformats_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -789,6 +789,30 @@ def test_timestamp_to_systemtime(self):
places=3,
)

def test_issue_1905(self):
expected = can.Message(
timestamp=1735654183.491113,
channel=6,
arbitration_id=0x6A9,
is_extended_id=False,
is_fd=True,
bitrate_switch=True,
error_state_indicator=False,
dlc=64,
data=bytearray(
b"\xff\xff\xff\xff\xff\xff\xff\xff"
b"\xff\xff\xff\xff\xff\xff\xff\xff"
b"\xff\xff\xff\xff\xff\xff\xff\xff"
b"\xff\xff\xff\xff\xff\xff\xff\xff"
b"\xff\xff\xff\xff\xff\xff\xff\xff"
b"\xff\xff\xff\xff\xff\xff\xff\xff"
b"\x00\x00\x00\x00\x00\x00\x00\x00"
b"\x00\x00\x00\x00\x00\x00\x00\x00"
),
)
msgs = self._read_log_file("issue_1905.blf")
self.assertMessageEqual(expected, msgs[0])


class TestCanutilsFileFormat(ReaderWriterTest):
"""Tests can.CanutilsLogWriter and can.CanutilsLogReader"""
Expand Down
Loading