exceeddata-sdk-vdata 2.9.2__tar.gz → 2.9.6__tar.gz
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- {exceeddata-sdk-vdata-2.9.2/exceeddata_sdk_vdata.egg-info → exceeddata-sdk-vdata-2.9.6}/PKG-INFO +1 -1
- {exceeddata-sdk-vdata-2.9.2 → exceeddata-sdk-vdata-2.9.6}/exceeddata/sdk/__init__.py +1 -1
- exceeddata-sdk-vdata-2.9.6/exceeddata/sdk/blf.py +594 -0
- exceeddata-sdk-vdata-2.9.6/exceeddata/sdk/converter.py +140 -0
- {exceeddata-sdk-vdata-2.9.2 → exceeddata-sdk-vdata-2.9.6}/exceeddata/sdk/vdata.py +335 -37
- {exceeddata-sdk-vdata-2.9.2 → exceeddata-sdk-vdata-2.9.6/exceeddata_sdk_vdata.egg-info}/PKG-INFO +1 -1
- {exceeddata-sdk-vdata-2.9.2 → exceeddata-sdk-vdata-2.9.6}/exceeddata_sdk_vdata.egg-info/SOURCES.txt +2 -0
- {exceeddata-sdk-vdata-2.9.2 → exceeddata-sdk-vdata-2.9.6}/setup.py +1 -1
- {exceeddata-sdk-vdata-2.9.2 → exceeddata-sdk-vdata-2.9.6}/LICENSE +0 -0
- {exceeddata-sdk-vdata-2.9.2 → exceeddata-sdk-vdata-2.9.6}/README.rst +0 -0
- {exceeddata-sdk-vdata-2.9.2 → exceeddata-sdk-vdata-2.9.6}/exceeddata_sdk_vdata.egg-info/dependency_links.txt +0 -0
- {exceeddata-sdk-vdata-2.9.2 → exceeddata-sdk-vdata-2.9.6}/exceeddata_sdk_vdata.egg-info/not-zip-safe +0 -0
- {exceeddata-sdk-vdata-2.9.2 → exceeddata-sdk-vdata-2.9.6}/exceeddata_sdk_vdata.egg-info/requires.txt +0 -0
- {exceeddata-sdk-vdata-2.9.2 → exceeddata-sdk-vdata-2.9.6}/exceeddata_sdk_vdata.egg-info/top_level.txt +0 -0
- {exceeddata-sdk-vdata-2.9.2 → exceeddata-sdk-vdata-2.9.6}/setup.cfg +0 -0
|
@@ -0,0 +1,594 @@
|
|
|
1
|
+
"""
|
|
2
|
+
Copied from open source can.
|
|
3
|
+
Fix CanMessage CanFDMessage Padding problem.
|
|
4
|
+
|
|
5
|
+
Implements support for BLF (Binary Logging Format) which is a proprietary
|
|
6
|
+
CAN log format from Vector Informatik GmbH (Germany).
|
|
7
|
+
|
|
8
|
+
No official specification of the binary logging format is available.
|
|
9
|
+
This implementation is based on Toby Lorenz' C++ library "Vector BLF" which is
|
|
10
|
+
licensed under GPLv3. https://bitbucket.org/tobylorenz/vector_blf.
|
|
11
|
+
|
|
12
|
+
The file starts with a header. The rest is one or more "log containers"
|
|
13
|
+
which consists of a header and some zlib compressed data, usually up to 128 kB
|
|
14
|
+
of uncompressed data each. This data contains the actual CAN messages and other
|
|
15
|
+
objects types.
|
|
16
|
+
"""
|
|
17
|
+
|
|
18
|
+
import datetime
|
|
19
|
+
import logging
|
|
20
|
+
import struct
|
|
21
|
+
import time
|
|
22
|
+
import zlib
|
|
23
|
+
from typing import Any, BinaryIO, Generator, List, Optional, Tuple, Union, cast
|
|
24
|
+
|
|
25
|
+
from can.message import Message
|
|
26
|
+
from can.typechecking import StringPathLike
|
|
27
|
+
from can.util import channel2int, dlc2len, len2dlc
|
|
28
|
+
from can.io.generic import BinaryIOMessageReader, FileIOMessageWriter
|
|
29
|
+
|
|
30
|
+
TSystemTime = Tuple[int, int, int, int, int, int, int, int]
|
|
31
|
+
|
|
32
|
+
|
|
33
|
+
class BLFParseError(Exception):
|
|
34
|
+
"""BLF file could not be parsed correctly."""
|
|
35
|
+
|
|
36
|
+
|
|
37
|
+
LOG = logging.getLogger(__name__)
|
|
38
|
+
|
|
39
|
+
# signature ("LOGG"), header size,
|
|
40
|
+
# application ID, application major, application minor, application build,
|
|
41
|
+
# bin log major, bin log minor, bin log build, bin log patch,
|
|
42
|
+
# file size, uncompressed size, count of objects, count of objects read,
|
|
43
|
+
# time start (SYSTEMTIME), time stop (SYSTEMTIME)
|
|
44
|
+
FILE_HEADER_STRUCT = struct.Struct("<4sLBBBBBBBBQQLL8H8H")
|
|
45
|
+
|
|
46
|
+
# Pad file header to this size
|
|
47
|
+
FILE_HEADER_SIZE = 144
|
|
48
|
+
|
|
49
|
+
# signature ("LOBJ"), header size, header version, object size, object type
|
|
50
|
+
OBJ_HEADER_BASE_STRUCT = struct.Struct("<4sHHLL")
|
|
51
|
+
|
|
52
|
+
# flags, client index, object version, timestamp
|
|
53
|
+
OBJ_HEADER_V1_STRUCT = struct.Struct("<LHHQ")
|
|
54
|
+
|
|
55
|
+
# flags, timestamp status, object version, timestamp, (original timestamp)
|
|
56
|
+
OBJ_HEADER_V2_STRUCT = struct.Struct("<LBxHQ8x")
|
|
57
|
+
|
|
58
|
+
# compression method, size uncompressed
|
|
59
|
+
LOG_CONTAINER_STRUCT = struct.Struct("<H6xL4x")
|
|
60
|
+
|
|
61
|
+
# channel, flags, dlc, arbitration id, data
|
|
62
|
+
CAN_MSG_STRUCT = struct.Struct("<HBBL8s")
|
|
63
|
+
|
|
64
|
+
# channel, flags, dlc, arbitration id, frame length, bit count, FD flags,
|
|
65
|
+
# valid data bytes, data
|
|
66
|
+
CAN_FD_MSG_STRUCT = struct.Struct("<HBBLLBBB5x64s")
|
|
67
|
+
|
|
68
|
+
# channel, dlc, valid payload length of data, tx count, arbitration id,
|
|
69
|
+
# frame length, flags, bit rate used in arbitration phase,
|
|
70
|
+
# bit rate used in data phase, time offset of brs field,
|
|
71
|
+
# time offset of crc delimiter field, bit count, direction,
|
|
72
|
+
# offset if extDataOffset is used, crc
|
|
73
|
+
CAN_FD_MSG_64_STRUCT = struct.Struct("<BBBBLLLLLLLHBBL")
|
|
74
|
+
|
|
75
|
+
# channel, length, flags, ecc, position, dlc, frame length, id, flags ext, data
|
|
76
|
+
CAN_ERROR_EXT_STRUCT = struct.Struct("<HHLBBBxLLH2x8s")
|
|
77
|
+
|
|
78
|
+
# commented event type, foreground color, background color, relocatable,
|
|
79
|
+
# group name length, marker name length, description length
|
|
80
|
+
GLOBAL_MARKER_STRUCT = struct.Struct("<LLL3xBLLL12x")
|
|
81
|
+
|
|
82
|
+
|
|
83
|
+
CAN_MESSAGE = 1
|
|
84
|
+
LOG_CONTAINER = 10
|
|
85
|
+
CAN_ERROR_EXT = 73
|
|
86
|
+
CAN_MESSAGE2 = 86
|
|
87
|
+
GLOBAL_MARKER = 96
|
|
88
|
+
CAN_FD_MESSAGE = 100
|
|
89
|
+
CAN_FD_MESSAGE_64 = 101
|
|
90
|
+
|
|
91
|
+
NO_COMPRESSION = 0
|
|
92
|
+
ZLIB_DEFLATE = 2
|
|
93
|
+
|
|
94
|
+
CAN_MSG_EXT = 0x80000000
|
|
95
|
+
REMOTE_FLAG = 0x80
|
|
96
|
+
EDL = 0x1
|
|
97
|
+
BRS = 0x2
|
|
98
|
+
ESI = 0x4
|
|
99
|
+
DIR = 0x1
|
|
100
|
+
|
|
101
|
+
TIME_TEN_MICS = 0x00000001
|
|
102
|
+
TIME_ONE_NANS = 0x00000002
|
|
103
|
+
|
|
104
|
+
|
|
105
|
+
def timestamp_to_systemtime(timestamp: float) -> TSystemTime:
|
|
106
|
+
if timestamp is None or timestamp < 631152000:
|
|
107
|
+
# Probably not a Unix timestamp
|
|
108
|
+
return 0, 0, 0, 0, 0, 0, 0, 0
|
|
109
|
+
t = datetime.datetime.fromtimestamp(round(timestamp, 3))
|
|
110
|
+
return (
|
|
111
|
+
t.year,
|
|
112
|
+
t.month,
|
|
113
|
+
t.isoweekday() % 7,
|
|
114
|
+
t.day,
|
|
115
|
+
t.hour,
|
|
116
|
+
t.minute,
|
|
117
|
+
t.second,
|
|
118
|
+
t.microsecond // 1000,
|
|
119
|
+
)
|
|
120
|
+
|
|
121
|
+
|
|
122
|
+
def systemtime_to_timestamp(systemtime: TSystemTime) -> float:
|
|
123
|
+
try:
|
|
124
|
+
t = datetime.datetime(
|
|
125
|
+
systemtime[0],
|
|
126
|
+
systemtime[1],
|
|
127
|
+
systemtime[3],
|
|
128
|
+
systemtime[4],
|
|
129
|
+
systemtime[5],
|
|
130
|
+
systemtime[6],
|
|
131
|
+
systemtime[7] * 1000,
|
|
132
|
+
)
|
|
133
|
+
return t.timestamp()
|
|
134
|
+
except ValueError:
|
|
135
|
+
return 0
|
|
136
|
+
|
|
137
|
+
|
|
138
|
+
class BLFReader(BinaryIOMessageReader):
|
|
139
|
+
"""
|
|
140
|
+
Iterator of CAN messages from a Binary Logging File.
|
|
141
|
+
|
|
142
|
+
Only CAN messages and error frames are supported. Other object types are
|
|
143
|
+
silently ignored.
|
|
144
|
+
"""
|
|
145
|
+
|
|
146
|
+
file: BinaryIO
|
|
147
|
+
|
|
148
|
+
def __init__(
|
|
149
|
+
self,
|
|
150
|
+
file: Union[StringPathLike, BinaryIO],
|
|
151
|
+
**kwargs: Any,
|
|
152
|
+
) -> None:
|
|
153
|
+
"""
|
|
154
|
+
:param file: a path-like object or as file-like object to read from
|
|
155
|
+
If this is a file-like object, is has to opened in binary
|
|
156
|
+
read mode, not text read mode.
|
|
157
|
+
"""
|
|
158
|
+
super().__init__(file, mode="rb")
|
|
159
|
+
data = self.file.read(FILE_HEADER_STRUCT.size)
|
|
160
|
+
header = FILE_HEADER_STRUCT.unpack(data)
|
|
161
|
+
if header[0] != b"LOGG":
|
|
162
|
+
raise BLFParseError("Unexpected file format")
|
|
163
|
+
self.file_size = header[10]
|
|
164
|
+
self.uncompressed_size = header[11]
|
|
165
|
+
self.object_count = header[12]
|
|
166
|
+
self.start_timestamp = systemtime_to_timestamp(cast(TSystemTime, header[14:22]))
|
|
167
|
+
self.stop_timestamp = systemtime_to_timestamp(cast(TSystemTime, header[22:30]))
|
|
168
|
+
# Read rest of header
|
|
169
|
+
self.file.read(header[1] - FILE_HEADER_STRUCT.size)
|
|
170
|
+
self._tail = b""
|
|
171
|
+
self._pos = 0
|
|
172
|
+
|
|
173
|
+
def __iter__(self) -> Generator[Message, None, None]:
|
|
174
|
+
while True:
|
|
175
|
+
data = self.file.read(OBJ_HEADER_BASE_STRUCT.size)
|
|
176
|
+
if not data:
|
|
177
|
+
# EOF
|
|
178
|
+
break
|
|
179
|
+
|
|
180
|
+
signature, _, _, obj_size, obj_type = OBJ_HEADER_BASE_STRUCT.unpack(data)
|
|
181
|
+
if signature != b"LOBJ":
|
|
182
|
+
raise BLFParseError()
|
|
183
|
+
obj_data = self.file.read(obj_size - OBJ_HEADER_BASE_STRUCT.size)
|
|
184
|
+
# Read padding bytes
|
|
185
|
+
self.file.read(obj_size % 4)
|
|
186
|
+
|
|
187
|
+
if obj_type == LOG_CONTAINER:
|
|
188
|
+
method, _ = LOG_CONTAINER_STRUCT.unpack_from(obj_data)
|
|
189
|
+
container_data = obj_data[LOG_CONTAINER_STRUCT.size :]
|
|
190
|
+
if method == NO_COMPRESSION:
|
|
191
|
+
data = container_data
|
|
192
|
+
elif method == ZLIB_DEFLATE:
|
|
193
|
+
zobj = zlib.decompressobj()
|
|
194
|
+
data = zobj.decompress(container_data)
|
|
195
|
+
else:
|
|
196
|
+
# Unknown compression method
|
|
197
|
+
LOG.warning("Unknown compression method (%d)", method)
|
|
198
|
+
continue
|
|
199
|
+
yield from self._parse_container(data)
|
|
200
|
+
self.stop()
|
|
201
|
+
|
|
202
|
+
def _parse_container(self, data):
|
|
203
|
+
if self._tail:
|
|
204
|
+
data = b"".join((self._tail, data))
|
|
205
|
+
try:
|
|
206
|
+
yield from self._parse_data(data)
|
|
207
|
+
except struct.error:
|
|
208
|
+
# There was not enough data in the container to unpack a struct
|
|
209
|
+
pass
|
|
210
|
+
# Save the remaining data that could not be processed
|
|
211
|
+
self._tail = data[self._pos :]
|
|
212
|
+
|
|
213
|
+
def _parse_data(self, data):
|
|
214
|
+
"""Optimized inner loop by making local copies of global variables
|
|
215
|
+
and class members and hardcoding some values."""
|
|
216
|
+
unpack_obj_header_base = OBJ_HEADER_BASE_STRUCT.unpack_from
|
|
217
|
+
obj_header_base_size = OBJ_HEADER_BASE_STRUCT.size
|
|
218
|
+
unpack_obj_header_v1 = OBJ_HEADER_V1_STRUCT.unpack_from
|
|
219
|
+
obj_header_v1_size = OBJ_HEADER_V1_STRUCT.size
|
|
220
|
+
unpack_obj_header_v2 = OBJ_HEADER_V2_STRUCT.unpack_from
|
|
221
|
+
obj_header_v2_size = OBJ_HEADER_V2_STRUCT.size
|
|
222
|
+
unpack_can_msg = CAN_MSG_STRUCT.unpack_from
|
|
223
|
+
unpack_can_fd_msg = CAN_FD_MSG_STRUCT.unpack_from
|
|
224
|
+
unpack_can_fd_64_msg = CAN_FD_MSG_64_STRUCT.unpack_from
|
|
225
|
+
can_fd_64_msg_size = CAN_FD_MSG_64_STRUCT.size
|
|
226
|
+
unpack_can_error_ext = CAN_ERROR_EXT_STRUCT.unpack_from
|
|
227
|
+
|
|
228
|
+
start_timestamp = self.start_timestamp
|
|
229
|
+
max_pos = len(data)
|
|
230
|
+
pos = 0
|
|
231
|
+
|
|
232
|
+
# Loop until a struct unpack raises an exception
|
|
233
|
+
while True:
|
|
234
|
+
self._pos = pos
|
|
235
|
+
# Find next object after padding (depends on object type)
|
|
236
|
+
try:
|
|
237
|
+
pos = data.index(b"LOBJ", pos, pos + 8)
|
|
238
|
+
except ValueError:
|
|
239
|
+
if pos + 8 > max_pos:
|
|
240
|
+
# Not enough data in container
|
|
241
|
+
return
|
|
242
|
+
raise BLFParseError("Could not find next object") from None
|
|
243
|
+
header = unpack_obj_header_base(data, pos)
|
|
244
|
+
# print(header)
|
|
245
|
+
signature, _, header_version, obj_size, obj_type = header
|
|
246
|
+
if signature != b"LOBJ":
|
|
247
|
+
raise BLFParseError()
|
|
248
|
+
|
|
249
|
+
# Calculate position of next object
|
|
250
|
+
next_pos = pos + obj_size
|
|
251
|
+
if next_pos > max_pos:
|
|
252
|
+
# This object continues in the next container
|
|
253
|
+
return
|
|
254
|
+
pos += obj_header_base_size
|
|
255
|
+
|
|
256
|
+
# Read rest of header
|
|
257
|
+
if header_version == 1:
|
|
258
|
+
flags, _, _, timestamp = unpack_obj_header_v1(data, pos)
|
|
259
|
+
pos += obj_header_v1_size
|
|
260
|
+
elif header_version == 2:
|
|
261
|
+
flags, _, _, timestamp = unpack_obj_header_v2(data, pos)
|
|
262
|
+
pos += obj_header_v2_size
|
|
263
|
+
else:
|
|
264
|
+
LOG.warning("Unknown object header version (%d)", header_version)
|
|
265
|
+
pos = next_pos
|
|
266
|
+
continue
|
|
267
|
+
|
|
268
|
+
# Calculate absolute timestamp in seconds
|
|
269
|
+
factor = 1e-5 if flags == 1 else 1e-9
|
|
270
|
+
timestamp = timestamp * factor + start_timestamp
|
|
271
|
+
|
|
272
|
+
if obj_type in (CAN_MESSAGE, CAN_MESSAGE2):
|
|
273
|
+
channel, flags, dlc, can_id, can_data = unpack_can_msg(data, pos)
|
|
274
|
+
yield Message(
|
|
275
|
+
timestamp=timestamp,
|
|
276
|
+
arbitration_id=can_id & 0x1FFFFFFF,
|
|
277
|
+
is_extended_id=bool(can_id & CAN_MSG_EXT),
|
|
278
|
+
is_remote_frame=bool(flags & REMOTE_FLAG),
|
|
279
|
+
is_rx=not bool(flags & DIR),
|
|
280
|
+
dlc=dlc,
|
|
281
|
+
data=can_data[:dlc],
|
|
282
|
+
channel=channel - 1,
|
|
283
|
+
)
|
|
284
|
+
elif obj_type == CAN_ERROR_EXT:
|
|
285
|
+
members = unpack_can_error_ext(data, pos)
|
|
286
|
+
channel = members[0]
|
|
287
|
+
dlc = members[5]
|
|
288
|
+
can_id = members[7]
|
|
289
|
+
can_data = members[9]
|
|
290
|
+
yield Message(
|
|
291
|
+
timestamp=timestamp,
|
|
292
|
+
is_error_frame=True,
|
|
293
|
+
is_extended_id=bool(can_id & CAN_MSG_EXT),
|
|
294
|
+
arbitration_id=can_id & 0x1FFFFFFF,
|
|
295
|
+
dlc=dlc,
|
|
296
|
+
data=can_data[:dlc],
|
|
297
|
+
channel=channel - 1,
|
|
298
|
+
)
|
|
299
|
+
elif obj_type == CAN_FD_MESSAGE:
|
|
300
|
+
members = unpack_can_fd_msg(data, pos)
|
|
301
|
+
(
|
|
302
|
+
channel,
|
|
303
|
+
flags,
|
|
304
|
+
dlc,
|
|
305
|
+
can_id,
|
|
306
|
+
_,
|
|
307
|
+
_,
|
|
308
|
+
fd_flags,
|
|
309
|
+
valid_bytes,
|
|
310
|
+
can_data,
|
|
311
|
+
) = members
|
|
312
|
+
yield Message(
|
|
313
|
+
timestamp=timestamp,
|
|
314
|
+
arbitration_id=can_id & 0x1FFFFFFF,
|
|
315
|
+
is_extended_id=bool(can_id & CAN_MSG_EXT),
|
|
316
|
+
is_remote_frame=bool(flags & REMOTE_FLAG),
|
|
317
|
+
is_fd=bool(fd_flags & 0x1),
|
|
318
|
+
is_rx=not bool(flags & DIR),
|
|
319
|
+
bitrate_switch=bool(fd_flags & 0x2),
|
|
320
|
+
error_state_indicator=bool(fd_flags & 0x4),
|
|
321
|
+
dlc=dlc2len(dlc),
|
|
322
|
+
data=can_data[:valid_bytes],
|
|
323
|
+
channel=channel - 1,
|
|
324
|
+
)
|
|
325
|
+
elif obj_type == CAN_FD_MESSAGE_64:
|
|
326
|
+
(
|
|
327
|
+
channel,
|
|
328
|
+
dlc,
|
|
329
|
+
valid_bytes,
|
|
330
|
+
_,
|
|
331
|
+
can_id,
|
|
332
|
+
_,
|
|
333
|
+
fd_flags,
|
|
334
|
+
_,
|
|
335
|
+
_,
|
|
336
|
+
_,
|
|
337
|
+
_,
|
|
338
|
+
_,
|
|
339
|
+
direction,
|
|
340
|
+
_,
|
|
341
|
+
_,
|
|
342
|
+
) = unpack_can_fd_64_msg(data, pos)
|
|
343
|
+
pos += can_fd_64_msg_size
|
|
344
|
+
yield Message(
|
|
345
|
+
timestamp=timestamp,
|
|
346
|
+
arbitration_id=can_id & 0x1FFFFFFF,
|
|
347
|
+
is_extended_id=bool(can_id & CAN_MSG_EXT),
|
|
348
|
+
is_remote_frame=bool(fd_flags & 0x0010),
|
|
349
|
+
is_fd=bool(fd_flags & 0x1000),
|
|
350
|
+
is_rx=not direction,
|
|
351
|
+
bitrate_switch=bool(fd_flags & 0x2000),
|
|
352
|
+
error_state_indicator=bool(fd_flags & 0x4000),
|
|
353
|
+
dlc=dlc2len(dlc),
|
|
354
|
+
data=data[pos : pos + valid_bytes],
|
|
355
|
+
channel=channel - 1,
|
|
356
|
+
)
|
|
357
|
+
|
|
358
|
+
pos = next_pos
|
|
359
|
+
|
|
360
|
+
|
|
361
|
+
class BLFWriter(FileIOMessageWriter):
|
|
362
|
+
"""
|
|
363
|
+
Logs CAN data to a Binary Logging File compatible with Vector's tools.
|
|
364
|
+
"""
|
|
365
|
+
|
|
366
|
+
file: BinaryIO
|
|
367
|
+
|
|
368
|
+
#: Max log container size of uncompressed data
|
|
369
|
+
max_container_size = 128 * 1024
|
|
370
|
+
|
|
371
|
+
#: Application identifier for the log writer
|
|
372
|
+
application_id = 5
|
|
373
|
+
|
|
374
|
+
def __init__(
|
|
375
|
+
self,
|
|
376
|
+
file: Union[StringPathLike, BinaryIO],
|
|
377
|
+
append: bool = False,
|
|
378
|
+
channel: int = 1,
|
|
379
|
+
compression_level: int = -1,
|
|
380
|
+
**kwargs: Any,
|
|
381
|
+
) -> None:
|
|
382
|
+
"""
|
|
383
|
+
:param file: a path-like object or as file-like object to write to
|
|
384
|
+
If this is a file-like object, is has to opened in mode "wb+".
|
|
385
|
+
:param channel:
|
|
386
|
+
Default channel to log as if not specified by the interface.
|
|
387
|
+
:param append:
|
|
388
|
+
Append messages to an existing log file.
|
|
389
|
+
:param compression_level:
|
|
390
|
+
An integer from 0 to 9 or -1 controlling the level of compression.
|
|
391
|
+
1 (Z_BEST_SPEED) is fastest and produces the least compression.
|
|
392
|
+
9 (Z_BEST_COMPRESSION) is slowest and produces the most.
|
|
393
|
+
0 means that data will be stored without processing.
|
|
394
|
+
The default value is -1 (Z_DEFAULT_COMPRESSION).
|
|
395
|
+
Z_DEFAULT_COMPRESSION represents a default compromise between
|
|
396
|
+
speed and compression (currently equivalent to level 6).
|
|
397
|
+
"""
|
|
398
|
+
mode = "rb+" if append else "wb"
|
|
399
|
+
try:
|
|
400
|
+
super().__init__(file, mode=mode)
|
|
401
|
+
except FileNotFoundError:
|
|
402
|
+
# Trying to append to a non-existing file, create a new one
|
|
403
|
+
append = False
|
|
404
|
+
mode = "wb"
|
|
405
|
+
super().__init__(file, mode=mode)
|
|
406
|
+
assert self.file is not None
|
|
407
|
+
self.channel = channel
|
|
408
|
+
self.compression_level = compression_level
|
|
409
|
+
self._buffer: List[bytes] = []
|
|
410
|
+
self._buffer_size = 0
|
|
411
|
+
# If max container size is located in kwargs, then update the instance
|
|
412
|
+
if kwargs.get("max_container_size", False):
|
|
413
|
+
self.max_container_size = kwargs["max_container_size"]
|
|
414
|
+
if append:
|
|
415
|
+
# Parse file header
|
|
416
|
+
data = self.file.read(FILE_HEADER_STRUCT.size)
|
|
417
|
+
header = FILE_HEADER_STRUCT.unpack(data)
|
|
418
|
+
if header[0] != b"LOGG":
|
|
419
|
+
raise BLFParseError("Unexpected file format")
|
|
420
|
+
self.uncompressed_size = header[11]
|
|
421
|
+
self.object_count = header[12]
|
|
422
|
+
self.start_timestamp: Optional[float] = systemtime_to_timestamp(
|
|
423
|
+
cast(TSystemTime, header[14:22])
|
|
424
|
+
)
|
|
425
|
+
self.stop_timestamp: Optional[float] = systemtime_to_timestamp(
|
|
426
|
+
cast(TSystemTime, header[22:30])
|
|
427
|
+
)
|
|
428
|
+
# Jump to the end of the file
|
|
429
|
+
self.file.seek(0, 2)
|
|
430
|
+
else:
|
|
431
|
+
self.object_count = 0
|
|
432
|
+
self.uncompressed_size = FILE_HEADER_SIZE
|
|
433
|
+
self.start_timestamp = None
|
|
434
|
+
self.stop_timestamp = None
|
|
435
|
+
# Write a default header which will be updated when stopped
|
|
436
|
+
self._write_header(FILE_HEADER_SIZE)
|
|
437
|
+
|
|
438
|
+
def _write_header(self, filesize):
|
|
439
|
+
header = [b"LOGG", FILE_HEADER_SIZE, self.application_id, 0, 0, 0, 2, 6, 8, 1]
|
|
440
|
+
# The meaning of "count of objects read" is unknown
|
|
441
|
+
header.extend([filesize, self.uncompressed_size, self.object_count, 0])
|
|
442
|
+
header.extend(timestamp_to_systemtime(self.start_timestamp))
|
|
443
|
+
header.extend(timestamp_to_systemtime(self.stop_timestamp))
|
|
444
|
+
self.file.write(FILE_HEADER_STRUCT.pack(*header))
|
|
445
|
+
# Pad to header size
|
|
446
|
+
self.file.write(b"\x00" * (FILE_HEADER_SIZE - FILE_HEADER_STRUCT.size))
|
|
447
|
+
|
|
448
|
+
def on_message_received(self, msg):
|
|
449
|
+
channel = channel2int(msg.channel)
|
|
450
|
+
if channel is None:
|
|
451
|
+
channel = self.channel
|
|
452
|
+
else:
|
|
453
|
+
# Many interfaces start channel numbering at 0 which is invalid
|
|
454
|
+
channel += 1
|
|
455
|
+
|
|
456
|
+
arb_id = msg.arbitration_id
|
|
457
|
+
if msg.is_extended_id:
|
|
458
|
+
arb_id |= CAN_MSG_EXT
|
|
459
|
+
flags = REMOTE_FLAG if msg.is_remote_frame else 0
|
|
460
|
+
if not msg.is_rx:
|
|
461
|
+
flags |= DIR
|
|
462
|
+
can_data = bytes(msg.data)
|
|
463
|
+
|
|
464
|
+
if msg.is_error_frame:
|
|
465
|
+
data = CAN_ERROR_EXT_STRUCT.pack(
|
|
466
|
+
channel,
|
|
467
|
+
0, # length
|
|
468
|
+
0, # flags
|
|
469
|
+
0, # ecc
|
|
470
|
+
0, # position
|
|
471
|
+
len2dlc(msg.dlc),
|
|
472
|
+
0, # frame length
|
|
473
|
+
arb_id,
|
|
474
|
+
0, # ext flags
|
|
475
|
+
can_data,
|
|
476
|
+
)
|
|
477
|
+
self._add_object(CAN_ERROR_EXT, data, msg.timestamp)
|
|
478
|
+
elif msg.is_fd:
|
|
479
|
+
fd_flags = EDL
|
|
480
|
+
if msg.bitrate_switch:
|
|
481
|
+
fd_flags |= BRS
|
|
482
|
+
if msg.error_state_indicator:
|
|
483
|
+
fd_flags |= ESI
|
|
484
|
+
data = CAN_FD_MSG_STRUCT.pack(
|
|
485
|
+
channel,
|
|
486
|
+
flags,
|
|
487
|
+
len2dlc(msg.dlc),
|
|
488
|
+
arb_id,
|
|
489
|
+
0,
|
|
490
|
+
0,
|
|
491
|
+
fd_flags,
|
|
492
|
+
len(can_data),
|
|
493
|
+
can_data,
|
|
494
|
+
)
|
|
495
|
+
self._add_object(CAN_FD_MESSAGE, data, msg.timestamp)
|
|
496
|
+
else:
|
|
497
|
+
data = CAN_MSG_STRUCT.pack(channel, flags, msg.dlc, arb_id, can_data)
|
|
498
|
+
self._add_object(CAN_MESSAGE, data, msg.timestamp)
|
|
499
|
+
|
|
500
|
+
def log_event(self, text, timestamp=None):
|
|
501
|
+
"""Add an arbitrary message to the log file as a global marker.
|
|
502
|
+
|
|
503
|
+
:param str text:
|
|
504
|
+
The group name of the marker.
|
|
505
|
+
:param float timestamp:
|
|
506
|
+
Absolute timestamp in Unix timestamp format. If not given, the
|
|
507
|
+
marker will be placed along the last message.
|
|
508
|
+
"""
|
|
509
|
+
try:
|
|
510
|
+
# Only works on Windows
|
|
511
|
+
text = text.encode("mbcs")
|
|
512
|
+
except LookupError:
|
|
513
|
+
text = text.encode("ascii")
|
|
514
|
+
comment = b"Added by python-can"
|
|
515
|
+
marker = b"python-can"
|
|
516
|
+
data = GLOBAL_MARKER_STRUCT.pack(
|
|
517
|
+
0, 0xFFFFFF, 0xFF3300, 0, len(text), len(marker), len(comment)
|
|
518
|
+
)
|
|
519
|
+
self._add_object(GLOBAL_MARKER, data + text + marker + comment, timestamp)
|
|
520
|
+
|
|
521
|
+
def _add_object(self, obj_type, data, timestamp=None):
|
|
522
|
+
if timestamp is None:
|
|
523
|
+
timestamp = self.stop_timestamp or time.time()
|
|
524
|
+
if self.start_timestamp is None:
|
|
525
|
+
self.start_timestamp = timestamp
|
|
526
|
+
self.stop_timestamp = timestamp
|
|
527
|
+
timestamp = int((timestamp - self.start_timestamp) * 1e9)
|
|
528
|
+
header_size = OBJ_HEADER_BASE_STRUCT.size + OBJ_HEADER_V1_STRUCT.size
|
|
529
|
+
obj_size = header_size + len(data)
|
|
530
|
+
base_header = OBJ_HEADER_BASE_STRUCT.pack(
|
|
531
|
+
b"LOBJ", header_size, 1, obj_size, obj_type
|
|
532
|
+
)
|
|
533
|
+
obj_header = OBJ_HEADER_V1_STRUCT.pack(TIME_ONE_NANS, 0, 0, max(timestamp, 0))
|
|
534
|
+
|
|
535
|
+
self._buffer.append(base_header)
|
|
536
|
+
self._buffer.append(obj_header)
|
|
537
|
+
self._buffer.append(data)
|
|
538
|
+
padding_size = 0 #len(data) % 4
|
|
539
|
+
if padding_size:
|
|
540
|
+
self._buffer.append(b"\x00" * padding_size)
|
|
541
|
+
|
|
542
|
+
self._buffer_size += obj_size + padding_size
|
|
543
|
+
self.object_count += 1
|
|
544
|
+
if self._buffer_size >= self.max_container_size:
|
|
545
|
+
self._flush()
|
|
546
|
+
|
|
547
|
+
def _flush(self):
|
|
548
|
+
"""Compresses and writes data in the buffer to file."""
|
|
549
|
+
if self.file.closed:
|
|
550
|
+
return
|
|
551
|
+
buffer = b"".join(self._buffer)
|
|
552
|
+
if not buffer:
|
|
553
|
+
# Nothing to write
|
|
554
|
+
return
|
|
555
|
+
uncompressed_data = memoryview(buffer)[: self.max_container_size]
|
|
556
|
+
# Save data that comes after max size to next container
|
|
557
|
+
tail = buffer[self.max_container_size :]
|
|
558
|
+
self._buffer = [tail]
|
|
559
|
+
self._buffer_size = len(tail)
|
|
560
|
+
if not self.compression_level:
|
|
561
|
+
data = uncompressed_data
|
|
562
|
+
method = NO_COMPRESSION
|
|
563
|
+
else:
|
|
564
|
+
data = zlib.compress(uncompressed_data, self.compression_level)
|
|
565
|
+
method = ZLIB_DEFLATE
|
|
566
|
+
obj_size = OBJ_HEADER_BASE_STRUCT.size + LOG_CONTAINER_STRUCT.size + len(data)
|
|
567
|
+
base_header = OBJ_HEADER_BASE_STRUCT.pack(
|
|
568
|
+
b"LOBJ", OBJ_HEADER_BASE_STRUCT.size, 1, obj_size, LOG_CONTAINER
|
|
569
|
+
)
|
|
570
|
+
container_header = LOG_CONTAINER_STRUCT.pack(method, len(uncompressed_data))
|
|
571
|
+
self.file.write(base_header)
|
|
572
|
+
self.file.write(container_header)
|
|
573
|
+
self.file.write(data)
|
|
574
|
+
# Write padding bytes
|
|
575
|
+
self.file.write(b"\x00" * (obj_size % 4))
|
|
576
|
+
self.uncompressed_size += OBJ_HEADER_BASE_STRUCT.size
|
|
577
|
+
self.uncompressed_size += LOG_CONTAINER_STRUCT.size
|
|
578
|
+
self.uncompressed_size += len(uncompressed_data)
|
|
579
|
+
|
|
580
|
+
def file_size(self) -> int:
|
|
581
|
+
"""Return an estimate of the current file size in bytes."""
|
|
582
|
+
return self.file.tell() + self._buffer_size
|
|
583
|
+
|
|
584
|
+
def stop(self):
|
|
585
|
+
"""Stops logging and closes the file."""
|
|
586
|
+
self._flush()
|
|
587
|
+
if self.file.seekable():
|
|
588
|
+
filesize = self.file.tell()
|
|
589
|
+
# Write header in the beginning of the file
|
|
590
|
+
self.file.seek(0)
|
|
591
|
+
self._write_header(filesize)
|
|
592
|
+
else:
|
|
593
|
+
LOG.error("Could not write BLF header since file is not seekable")
|
|
594
|
+
super().stop()
|