From e69b168492428fcdf6fdf83922ab6f6826cb7b42 Mon Sep 17 00:00:00 2001 From: zariiii9003 Date: Wed, 1 Jan 2025 13:09:11 +0100 Subject: [PATCH 1/2] add padding to CAN_FD_MESSAGE_64 data --- can/io/blf.py | 18 ++++++++++++++---- test/data/issue_1905.blf | Bin 0 -> 524 bytes test/logformats_test.py | 24 ++++++++++++++++++++++++ 3 files changed, 38 insertions(+), 4 deletions(-) create mode 100644 test/data/issue_1905.blf diff --git a/can/io/blf.py b/can/io/blf.py index 81146233d..64649ebaa 100644 --- a/can/io/blf.py +++ b/can/io/blf.py @@ -239,7 +239,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() @@ -334,10 +334,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, @@ -348,7 +358,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, ) diff --git a/test/data/issue_1905.blf b/test/data/issue_1905.blf new file mode 100644 index 0000000000000000000000000000000000000000..a896a6d7ca36d8842a0958d26bcf4f66c0593916 GIT binary patch literal 524 zcmebAcXyw_z`*b)!Iy!JlaYak3CID0E!+@V3`j8o@e6hy1||l120jK(1`URLNPKPv z8HP8E9Uw(O5Cg;-U>13VkH3?b0MN#7KAtAwo zHA#()BVyyzYl;GA&%HmeVN!47LB=D72?sd;P3EX6xDh>}@ICKih8x-+{&Y%wX1MWs z;m=czsSIy?U;LhN)QaJoSxf!wLuL%$wB97&IB&=B&5t9Y_M{=hH!qJ)u43MXz4l*H za(A*Hh+}4JEt$u-!ThjWuF`vk8^4_nWtrb+xKaIJ?cLyij2p@s=WFkO;BX`QU9zT$ zz_)*&y~X4@zMPS|s%<3jwXX0<*~P{a?hgz3kFp)O$KQJMju5lpe#4*3CYfvzXK?#8 zrIdjI=I Date: Wed, 1 Jan 2025 13:26:06 +0100 Subject: [PATCH 2/2] set timezone to utc --- can/io/blf.py | 3 ++- test/logformats_test.py | 2 +- 2 files changed, 3 insertions(+), 2 deletions(-) diff --git a/can/io/blf.py b/can/io/blf.py index 64649ebaa..7d944a846 100644 --- a/can/io/blf.py +++ b/can/io/blf.py @@ -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, @@ -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: diff --git a/test/logformats_test.py b/test/logformats_test.py index ef446e5d2..a2fd0fe09 100644 --- a/test/logformats_test.py +++ b/test/logformats_test.py @@ -791,7 +791,7 @@ def test_timestamp_to_systemtime(self): def test_issue_1905(self): expected = can.Message( - timestamp=1735650583.491113, + timestamp=1735654183.491113, channel=6, arbitration_id=0x6A9, is_extended_id=False,