1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
/// todos
/// [] check timezone/time shift
/// [] check extended frames
/// [] CANFD frame_name (or brs) support
use crate::{
dlt::{
DltChar4, DltExtendedHeader, DltMessage, DltMessageIndexType, DltStandardHeader,
DLT_EXT_HEADER_SIZE, DLT_MIN_STD_HEADER_SIZE, DLT_STD_HDR_BIG_ENDIAN,
DLT_STD_HDR_HAS_ECU_ID, DLT_STD_HDR_HAS_EXT_HDR, DLT_STD_HDR_HAS_TIMESTAMP,
DLT_STD_HDR_VERSION, SERVICE_ID_GET_LOG_INFO,
},
utils::{hex_to_bytes, US_PER_SEC},
};
use chrono::NaiveDateTime;
use lazy_static::lazy_static;
use regex::{CaptureLocations, Regex};
use slog::{debug, error, trace};
use std::{
collections::HashMap,
io::{BufRead, Lines},
str::FromStr,
};
/// an iterator that creates/iterates over dlt messages created from an .asc CAN file.
///
/// The CAN messages get encoded as
/// - reception time is the time from .asc date lines plus the timestamp
/// - timestamp_dms is the timestamp truncated/rounded down to 0.1ms
/// - non verbose DLT msgs
/// - noar:2
/// - payload consists of
/// - a u32 with the frame_id
/// - the data bytes received from CAN
///
/// - ECU, APID, CTID: tbd (use CAN ID)
/// - session_id not set
/// - endianess used: host endianess
///
/// Example line that gets parsed:
///
/// **0.985210** *1* **36f** Rx d *5* **f2 f7 fe ff 14** Length = 0 BitCount = 0 ID = 879
///
/// ### Note
/// ASC can format timestamps can be negative. This is usually used to have the timestamp 0
/// at a trigger time and have messages before the trigger as well.
/// As DLT doesn't support negative timestamps they are converted:
/// - the recording time is adjusted backwards and the first neg. message gets the timestamp 0.
/// - the following neg. timestamps get the difference to the prev. msg
pub struct Asc2DltMsgIterator<'a, R> {
lines: Lines<R>, // todo could optimize with e.g. stream_iterator for &str instead of string copies!
pub index: DltMessageIndexType,
pub lines_processed: usize,
pub lines_skipped: usize,
pub log: Option<&'a slog::Logger>,
date_us: u64, // will be parsed from first asc line "date ..."
first_neg_timestamp_us: i64, // first neg. timestamp after date line
capture_locations_can: CaptureLocations,
capture_locations_canfd: CaptureLocations,
capture_locations_canfd_errorframe: CaptureLocations,
htyp: u8, // std hdr htyp
len_wo_payload: u16,
can_id_ecu_map: HashMap<u8, DltChar4>,
apid: DltChar4,
ctid: DltChar4,
}
impl<'a, R: BufRead> Asc2DltMsgIterator<'a, R> {
pub fn new(
start_index: DltMessageIndexType,
reader: R,
log: Option<&'a slog::Logger>,
) -> Asc2DltMsgIterator<'a, R> {
let htyp = DLT_STD_HDR_VERSION
| DLT_STD_HDR_HAS_ECU_ID
| DLT_STD_HDR_HAS_TIMESTAMP
| DLT_STD_HDR_HAS_EXT_HDR
| if 1u32.to_be() == 1u32 {
DLT_STD_HDR_BIG_ENDIAN
} else {
0u8
};
let len_wo_payload = (DLT_MIN_STD_HEADER_SIZE + 4 + 4 + DLT_EXT_HEADER_SIZE) as u16;
Asc2DltMsgIterator {
lines: reader.lines(),
index: start_index,
lines_processed: 0,
lines_skipped: 0,
log,
date_us: (chrono::Utc::now().naive_utc().timestamp_micros()) as u64, // init to avoid underrun if only neg timestamps are provided
first_neg_timestamp_us: 0,
capture_locations_can: RE_MSG.capture_locations(),
capture_locations_canfd: RE_MSG_CANFD.capture_locations(),
capture_locations_canfd_errorframe: RE_MSG_CANFD_ERRORFRAME.capture_locations(),
htyp,
len_wo_payload,
can_id_ecu_map: HashMap::new(),
apid: DltChar4::from_buf(b"CAN\0"),
ctid: DltChar4::from_buf(b"TC\0\0"),
}
}
fn get_ecu(&mut self, can_id: u8) -> &DltChar4 {
self.can_id_ecu_map.entry(can_id).or_insert_with(|| {
if can_id < 10 {
DltChar4::from_str(format!("CAN{}", can_id).as_str()).unwrap()
} else if can_id < 100 {
DltChar4::from_str(format!("CA{}", can_id).as_str()).unwrap()
} else {
DltChar4::from_str(format!("C{}", can_id).as_str()).unwrap()
}
})
}
}
lazy_static! {
pub(crate) static ref RE_COMMENT: Regex = Regex::new(r"^//").unwrap();
pub(crate) static ref RE_DATE: Regex = Regex::new(r"^date (.*)$").unwrap();
pub(crate) static ref RE_MSG: Regex =
// timestamp channel_id can/frame_id Rx|Tx data_len
Regex::new(r"^(-?\d+\.\d{6}) (\d+) ([0-9a-fx]+) (Rx|Tx) d (\d+)").unwrap();
pub(crate) static ref RE_MSG_CANFD: Regex =
// timestamp CANFD channel_id Rx|Tx can_id(hex) frame_name&brs_or_brs (todo!) esi dlc(hex) data_length(dec) data (rest ignored)
Regex::new(r"^(-?\d+\.\d{6}) CANFD (\d+) (Rx|Tx) ([0-9a-fx]+)\s+(\d+) (\d+) ([0-9a-fx]+) (\d+)").unwrap();
pub(crate) static ref RE_MSG_CANFD_ERRORFRAME: Regex =
// timestamp CANFD channel_id Rx|Tx ErrorFrame (rest ignored)
Regex::new(r"^(-?\d+\.\d{6}) CANFD (\d+) (Rx|Tx) ErrorFrame").unwrap();
}
fn asc_parse_date(date_str: &str) -> Result<NaiveDateTime, chrono::ParseError> {
// we expect them in the following format:
NaiveDateTime::parse_from_str(date_str, "%a %b %d %I:%M:%S %p %Y")
}
/// Parse a timestamp in can asc format to a time in us.
///
/// Timestamps in asc can be negative thus a i64 is returned.
/// Expected format is (-)x.yyyyyy (x any number, y exactly 6 digits)
///
/// In case of any parsing errors 0 is returned.
/// ### Note
/// The function is slow if the the fraction is not 6 digits!
fn parse_signed_time_str(timestamp: &str) -> i64 {
let timestamp_is_neg = timestamp.starts_with('-');
let offset_timestamp = if timestamp_is_neg { 1_usize } else { 0 };
let dot_idx = timestamp.find('.').unwrap_or(timestamp.len());
let timestamp_secs_us: i64 = timestamp[offset_timestamp..dot_idx]
.parse::<i64>()
.unwrap_or_default()
* (US_PER_SEC as i64);
let timestamp_fraction_us = if dot_idx < timestamp.len() {
let timestamp_fraction_str = ×tamp[dot_idx + 1..];
let mut len_fraction = timestamp_fraction_str.len();
let mut timestamp_fraction_us =
timestamp_fraction_str.parse::<u64>().unwrap_or_default() as i64;
if len_fraction != 6 {
while len_fraction < 6 {
timestamp_fraction_us *= 10; // todo optimize by pow10...
len_fraction += 1;
}
while len_fraction > 6 {
timestamp_fraction_us /= 10;
len_fraction -= 1;
}
}
timestamp_fraction_us
} else {
0
};
let timestamp_us = timestamp_secs_us + timestamp_fraction_us;
if timestamp_is_neg {
-timestamp_us
} else {
timestamp_us
}
}
impl<'a, R> Iterator for Asc2DltMsgIterator<'a, R>
where
R: BufRead,
{
type Item = DltMessage;
fn next(&mut self) -> Option<Self::Item> {
for line in self.lines.by_ref() {
self.lines_processed += 1;
match &line {
Ok(line) => {
// expect "base hex timestamps absolute"
// matches can msg regex?
if let Some(captures) =
RE_MSG.captures_read(&mut self.capture_locations_can, line)
{
let cap_str = captures.as_str();
let loc_timestamp = self.capture_locations_can.get(1).unwrap();
let timestamp = &cap_str[loc_timestamp.0..loc_timestamp.1];
let timestamp_us = parse_signed_time_str(timestamp);
if timestamp_us.is_negative() && self.first_neg_timestamp_us == 0 {
self.first_neg_timestamp_us = timestamp_us;
}
let loc_can_id = self.capture_locations_can.get(2).unwrap();
// we map the can_id to the ECU to be used:
let can_id = &cap_str[loc_can_id.0..loc_can_id.1]
.parse::<u8>()
.unwrap_or_default();
let ecu = self.can_id_ecu_map.entry(*can_id).or_insert_with(|| {
if *can_id < 10 {
DltChar4::from_str(format!("CAN{}", can_id).as_str()).unwrap()
} else if *can_id < 100 {
DltChar4::from_str(format!("CA{}", can_id).as_str()).unwrap()
} else {
DltChar4::from_str(format!("C{}", can_id).as_str()).unwrap()
}
});
let loc_id = self.capture_locations_can.get(3).unwrap();
let id = &cap_str[loc_id.0..loc_id.1];
let frame_id = if let Some(stripped) = id.strip_suffix('x') {
let frame_id = u32::from_str_radix(stripped, 16).unwrap_or_default();
if let Some(log) = self.log {
trace!(
log,
"Asc2DltMsgIterator.next got msg with extended id={} {} at line #{}",
id,
frame_id,
self.lines_processed
);
}
frame_id
} else {
u32::from_str_radix(id, 16).unwrap_or_default()
};
//let loc_rxtx = self.capture_locations.get(4).unwrap();
//let rxtx = &cap_str[loc_rxtx.0..loc_rxtx.1];
let loc_d = self.capture_locations_can.get(5).unwrap();
let data_len =
&cap_str[loc_d.0..loc_d.1].parse::<u16>().unwrap_or_default();
// now the data itself:
let loc_d_start = loc_d.1 + 1;
let loc_d_end = loc_d_start + (3 * (*data_len as usize)) - 1;
let data = if *data_len > 0 && loc_d_end < line.len() {
hex_to_bytes(&line.as_str()[loc_d_start..loc_d_end])
} else {
None
};
let mut payload: Vec<u8> =
Vec::with_capacity((u32::BITS / 8) as usize + (*data_len as usize));
payload.extend(frame_id.to_ne_bytes());
if let Some(mut data) = data {
payload.append(&mut data);
}
/*if let Some(log) = self.log {
debug!(
log,
"Asc2DltMsgIterator.next got msg frame_id={} timestamp_us={} can_id={} id={} rxtx={} d={} payload={:?} at line #{}",
frame_id,
timestamp_us,
can_id,
id,
rxtx,
data_len,
payload,
self.lines_processed
);
}*/
// return a DltMessage
let index = self.index;
self.index += 1;
return Some(DltMessage {
index,
reception_time_us: self.date_us.saturating_add_signed(timestamp_us),
ecu: ecu.to_owned(),
timestamp_dms: if timestamp_us >= 0 {
(timestamp_us / 100) as u32
} else {
// for neg timestamps the reception time is correct but the timestamp needs to be corrected
// to always be monotonicaly increasing so we convert e.g. -100...-0.,0.1... to 0..100,0.1...
// i.e. timestamp - first_neg_timestamp... (so neg --(=+) first_neg_timestamp)
(timestamp_us.saturating_sub(self.first_neg_timestamp_us) / 100)
as u32
}, // rounding? or prefer round down to not move into the future? (could do w.o. timestamp_dms as well)
standard_header: DltStandardHeader {
htyp: self.htyp,
mcnt: (index & 0xff) as u8,
len: self.len_wo_payload + (payload.len() as u16),
},
extended_header: Some(DltExtendedHeader {
verb_mstp_mtin: (2u8 << 1) | (2u8 << 4), // NwTrace CAN, non verb.
noar: 2,
apid: self.apid.to_owned(),
ctid: self.ctid.to_owned(),
}),
payload,
payload_text: None,
lifecycle: 0,
});
} else if let Some(captures) =
RE_MSG_CANFD.captures_read(&mut self.capture_locations_canfd, line)
{
// capture groups:
// 1 = timestamp
// 2 = channel_id
// 3 = dir
// 4 = frame_id
// 5 = brs
// 6 = esi
// 7 = dlc
// 8 = data_length
let cap_str = captures.as_str();
let loc_timestamp = self.capture_locations_canfd.get(1).unwrap();
let timestamp = &cap_str[loc_timestamp.0..loc_timestamp.1];
let timestamp_us = parse_signed_time_str(timestamp);
if timestamp_us.is_negative() && self.first_neg_timestamp_us == 0 {
self.first_neg_timestamp_us = timestamp_us;
}
let loc_can_id = self.capture_locations_canfd.get(2).unwrap();
// we map the can_id to the ECU to be used:
let can_id = &cap_str[loc_can_id.0..loc_can_id.1]
.parse::<u8>()
.unwrap_or_default();
let ecu = self.can_id_ecu_map.entry(*can_id).or_insert_with(|| {
if *can_id < 10 {
DltChar4::from_str(format!("CAN{}", can_id).as_str()).unwrap()
} else if *can_id < 100 {
DltChar4::from_str(format!("CA{}", can_id).as_str()).unwrap()
} else {
DltChar4::from_str(format!("C{}", can_id).as_str()).unwrap()
}
});
let loc_id = self.capture_locations_canfd.get(4).unwrap();
let id = &cap_str[loc_id.0..loc_id.1];
let frame_id = if let Some(stripped) = id.strip_suffix('x') {
let frame_id = u32::from_str_radix(stripped, 16).unwrap_or_default();
if let Some(log) = self.log {
trace!(
log,
"Asc2DltMsgIterator.next got canfd msg with extended id={} {} at line #{}",
id,
frame_id,
self.lines_processed
);
}
frame_id
} else {
u32::from_str_radix(id, 16).unwrap_or_default()
};
//let loc_rxtx = self.capture_locations.get(4).unwrap();
//let rxtx = &cap_str[loc_rxtx.0..loc_rxtx.1];
let loc_d = self.capture_locations_canfd.get(8).unwrap();
let data_len =
&cap_str[loc_d.0..loc_d.1].parse::<u16>().unwrap_or_default();
// now the data itself:
let loc_d_start = loc_d.1 + 1;
let loc_d_end = loc_d_start + (3 * (*data_len as usize)) - 1;
let data = if *data_len > 0 && loc_d_end < line.len() {
hex_to_bytes(&line.as_str()[loc_d_start..loc_d_end])
} else {
None
};
let mut payload: Vec<u8> =
Vec::with_capacity((u32::BITS / 8) as usize + (*data_len as usize));
payload.extend(frame_id.to_ne_bytes());
if let Some(mut data) = data {
payload.append(&mut data);
}
// return a DltMessage
let index = self.index;
self.index += 1;
return Some(DltMessage {
index,
reception_time_us: self.date_us.saturating_add_signed(timestamp_us),
ecu: ecu.to_owned(),
timestamp_dms: if timestamp_us >= 0 {
(timestamp_us / 100) as u32
} else {
// for neg timestamps the reception time is correct but the timestamp needs to be corrected
// to always be monotonicaly increasing so we convert e.g. -100...-0.,0.1... to 0..100,0.1...
// i.e. timestamp - first_neg_timestamp... (so neg --(=+) first_neg_timestamp)
(timestamp_us.saturating_sub(self.first_neg_timestamp_us) / 100)
as u32
}, // rounding? or prefer round down to not move into the future? (could do w.o. timestamp_dms as well)
standard_header: DltStandardHeader {
htyp: self.htyp,
mcnt: (index & 0xff) as u8,
len: self.len_wo_payload + (payload.len() as u16),
},
extended_header: Some(DltExtendedHeader {
verb_mstp_mtin: (2u8 << 1) | (2u8 << 4), // NwTrace CAN, non verb.
noar: 2,
apid: self.apid.to_owned(),
ctid: self.ctid.to_owned(),
}),
payload,
payload_text: None,
lifecycle: 0,
});
} else if let Some(captures) = RE_MSG_CANFD_ERRORFRAME
.captures_read(&mut self.capture_locations_canfd_errorframe, line)
{
// capture groups:
// 1 = timestamp
// 2 = channel_id
// 3 = dir
let cap_str = captures.as_str();
let loc_timestamp = self.capture_locations_canfd_errorframe.get(1).unwrap();
let timestamp = &cap_str[loc_timestamp.0..loc_timestamp.1];
let timestamp_us = parse_signed_time_str(timestamp);
if timestamp_us.is_negative() && self.first_neg_timestamp_us == 0 {
self.first_neg_timestamp_us = timestamp_us;
}
let loc_can_id = self.capture_locations_canfd_errorframe.get(2).unwrap();
// we map the can_id to the ECU to be used:
let can_id = &cap_str[loc_can_id.0..loc_can_id.1]
.parse::<u8>()
.unwrap_or_default();
let ecu = self.can_id_ecu_map.entry(*can_id).or_insert_with(|| {
if *can_id < 10 {
DltChar4::from_str(format!("CAN{}", can_id).as_str()).unwrap()
} else if *can_id < 100 {
DltChar4::from_str(format!("CA{}", can_id).as_str()).unwrap()
} else {
DltChar4::from_str(format!("C{}", can_id).as_str()).unwrap()
}
});
let payload = vec![];
// return a DltMessage
let index = self.index;
self.index += 1;
return Some(DltMessage {
index,
reception_time_us: self.date_us.saturating_add_signed(timestamp_us),
ecu: ecu.to_owned(),
timestamp_dms: if timestamp_us >= 0 {
(timestamp_us / 100) as u32
} else {
// for neg timestamps the reception time is correct but the timestamp needs to be corrected
// to always be monotonicaly increasing so we convert e.g. -100...-0.,0.1... to 0..100,0.1...
// i.e. timestamp - first_neg_timestamp... (so neg --(=+) first_neg_timestamp)
(timestamp_us.saturating_sub(self.first_neg_timestamp_us) / 100)
as u32
}, // rounding? or prefer round down to not move into the future? (could do w.o. timestamp_dms as well)
standard_header: DltStandardHeader {
htyp: self.htyp,
mcnt: (index & 0xff) as u8,
len: self.len_wo_payload + (payload.len() as u16),
},
extended_header: Some(DltExtendedHeader {
verb_mstp_mtin: (2u8 << 1) | (2u8 << 4), // NwTrace CAN, non verb.
noar: 2,
apid: self.apid.to_owned(),
ctid: self.ctid.to_owned(),
}),
payload,
payload_text: Some("Error Frame".to_owned()),
lifecycle: 0,
});
} else if let Some(captures) = RE_DATE.captures(line) {
if let Some(date) = captures.get(1) {
let nt = asc_parse_date(date.as_str());
if let Ok(nt) = nt {
self.date_us = nt.timestamp_micros() as u64;
self.first_neg_timestamp_us = 0; // reset here if mult. files get concatenated
}
if let Some(log) = self.log {
trace!(
log,
"Asc2DltMsgIterator.next got date {} as {:?} at line #{}",
date.as_str(),
nt,
self.lines_processed
);
}
}
} else if RE_COMMENT.is_match(line) {
let comment = &line[2..].trim();
if comment.starts_with("BusMapping: CAN") {
// use BusMapping: CAN x = <name> and send the name as ECU name?
// or CANFD x = <name>
let id_idx = 14 + comment[14..].find(' ').unwrap_or(1);
if let Some((id, name)) = comment[id_idx..].split_once('=') {
if let Ok(id) = id.trim().parse::<u8>() {
let name = name.trim();
if let Some(log) = self.log {
debug!(
log,
"Asc2DltMsgIterator.next got BusMapping {} = {} at line #{}",
id,
name,
self.lines_processed
);
}
let apid = self.apid.to_owned(); // or special ones DA1 DA1?
let ctid = self.ctid.to_owned(); // CAN plugin checks for that apid as well!
let mut payload: Vec<u8> =
SERVICE_ID_GET_LOG_INFO.to_ne_bytes().into();
let apid_buf = apid.as_buf();
payload.extend(
[7u8]
.into_iter()
.chain(1u16.to_ne_bytes().into_iter()) // 1 app id, CAN plugin expects == 1
.chain(apid_buf.iter().copied())
.chain(0u16.to_ne_bytes().into_iter()) // 0 ctx ids
.chain((name.len() as u16).to_ne_bytes().into_iter()) // len of apid desc
.chain(name.as_bytes().iter().copied()),
);
// return a DltMessage with the LOG INFO APID incl. the BusMapping name
let index = self.index;
self.index += 1;
return Some(DltMessage {
index,
reception_time_us: self.date_us,
ecu: self.get_ecu(id).to_owned(),
timestamp_dms: 0u32,
standard_header: DltStandardHeader {
htyp: self.htyp,
mcnt: (index & 0xff) as u8,
len: self.len_wo_payload + (payload.len() as u16),
},
extended_header: Some(DltExtendedHeader {
verb_mstp_mtin: (3u8 << 1) | (2u8 << 4), // Control Resp., non verb
noar: 2,
apid,
ctid,
}),
payload,
payload_text: None,
lifecycle: 0,
});
}
}
}
} else if !line.is_empty() {
self.lines_skipped += 1;
if let Some(log) = self.log {
debug!(
log,
"Asc2DltMsgIterator.next unknown line {} at line #{}",
line,
self.lines_processed
);
}
}
}
Err(e) => {
if let Some(log) = self.log {
error!(
log,
"Asc2DltMsgIterator.next got err {} at line #{}",
e,
self.lines_processed
);
}
}
}
}
None
}
}
#[cfg(test)]
mod tests {
use super::{asc_parse_date, parse_signed_time_str, Asc2DltMsgIterator};
use crate::{
dlt::{DltMessageControlType, DltMessageNwType, DltMessageType, DLT_MAX_STORAGE_MSG_SIZE},
utils::LowMarkBufReader,
};
use chrono::{NaiveDate, NaiveDateTime, NaiveTime};
use slog::{o, Drain, Logger};
use std::fs::File;
fn new_logger() -> Logger {
let decorator = slog_term::PlainSyncDecorator::new(slog_term::TestStdoutWriter);
let drain = slog_term::FullFormat::new(decorator).build().fuse();
Logger::root(drain, o!())
}
#[test]
fn date1() {
assert_eq!(
Ok(NaiveDateTime::new(
NaiveDate::from_ymd_opt(2022, 4, 12).unwrap(),
NaiveTime::from_hms_micro_opt(8, 55, 37, 0).unwrap()
)),
asc_parse_date("Tue Apr 12 08:55:37 AM 2022")
);
let nt = NaiveDateTime::new(
NaiveDate::from_ymd_opt(2022, 5, 25).unwrap(),
NaiveTime::from_hms_micro_opt(15, 7, 31, 0).unwrap(),
);
// println!("nt formatted = '{}'", nt.format("%a %b %d %I:%M:%S %p %Y"));
assert_eq!(Ok(nt), asc_parse_date("Wed May 25 03:07:31 PM 2022"));
}
#[test]
fn asc_basic1() {
let mut test_dir = std::path::PathBuf::from(env!("CARGO_MANIFEST_DIR"));
test_dir.push("tests");
test_dir.push("can_example1.asc");
let fi = File::open(&test_dir).unwrap();
let start_index = 1000;
let log = new_logger();
let mut it = Asc2DltMsgIterator::new(
start_index,
LowMarkBufReader::new(fi, 512 * 1024, DLT_MAX_STORAGE_MSG_SIZE),
Some(&log),
);
let mut iterated_msgs = 0;
for m in &mut it {
assert_eq!(m.index, start_index + iterated_msgs);
assert!(!m.is_verbose());
assert_eq!(m.mcnt(), (m.index & 0xff) as u8);
match m.index {
1000 => assert_eq!(
m.mstp(),
DltMessageType::Control(DltMessageControlType::Response)
),
_ => assert_eq!(
m.mstp(),
DltMessageType::NwTrace(DltMessageNwType::Can),
"m.index={}",
m.index
),
}
iterated_msgs += 1;
if m.index == start_index + 1 {
// check some static data from example:
assert_eq!(
m.reception_time(),
NaiveDateTime::new(
NaiveDate::from_ymd_opt(2022, 4, 12).unwrap(),
NaiveTime::from_hms_micro_opt(8, 55, 37, 985210).unwrap()
)
);
assert_eq!(m.timestamp_dms, 9852);
assert_eq!(m.noar(), 2);
let exp_payload: Vec<u8> = 0x36fu32
.to_ne_bytes()
.into_iter()
.chain(vec![0xf2, 0xf7, 0xfe, 0xff, 0x14].into_iter())
.collect::<Vec<u8>>();
assert_eq!(
m.payload,
exp_payload //vec![111u8, 3, 0, 0, 0xf2, 0xf7, 0xfe, 0xff, 0x14] // will fail on different endian (as the frame_id as u32 has different enc. there)
);
}
}
assert_eq!(iterated_msgs, 101);
}
#[test]
fn asc_canfd1() {
let reader = r##"
//BusMapping: CAN 1 = ECU_CAN_FD
-0.169843 CANFD 1 Rx 135 1 0 8 8 f0 1a 7d 00 a6 ff ff ff 0 0 3000 0 0 0 0 0
-0.159843 CANFD 1 Rx 135 1 0 8 8 f1 1a 7d 00 a6 ff ff ff 0 0 3000 0 0 0 0 0
0.169843 CANFD 1 Rx 135 1 0 8 8 f2 1a 7d 00 a6 ff ff ff 0 0 3000 0 0 0 0 0"##
.as_bytes();
let mut it = Asc2DltMsgIterator::new(0, reader, None);
let mut iterated_msgs: u32 = 0;
for m in &mut it {
iterated_msgs += 1;
// todo verify payload println!("m={:?}", m);
match iterated_msgs {
2 => assert_eq!(m.timestamp_dms, 0_u32), // the first neg. gets a 0 timestamp
3 => assert_eq!(m.timestamp_dms, 100_u32), // the 10ms = 100dms apart
4 => assert_eq!(m.timestamp_dms, 1698_u32),
_ => {}
}
}
assert_eq!(iterated_msgs, 4); // one ctrl and three canfd msgs
}
#[test]
fn asc_canfd_errorframe() {
let reader = r##"
//BusMapping: CANFD 1 = ECU_CAN_FD 559
-0.017230 CANFD 1 Rx ErrorFrame 0 0 0 Data 0 0 0 0 0 0 0 11 0 0 0 0 0
0.017230 CANFD 1 Rx ErrorFrame 0 0 0 Data 0 0 0 0 0 0 0 11 0 0 0 0 0"##
.as_bytes();
let mut it = Asc2DltMsgIterator::new(0, reader, None);
let mut iterated_msgs: u32 = 0;
for _m in &mut it {
iterated_msgs += 1;
// todo verify payload println!("m={:?}", m);
}
assert_eq!(iterated_msgs, 3); // one ctrl and two canfd msgs
}
#[test]
fn asc_can1() {
let reader = r##"
//BusMapping: CAN 1 = ECU_CAN 431
-0.985210 1 36f Rx d 5 f2 f7 fe ff 14 Length = 0 BitCount = 0 ID = 879
-0.000100 1 36f Rx d 5 f2 f7 fe ff 14 Length = 0 BitCount = 0 ID = 879
0.000000 1 36f Rx d 5 f3 f7 fe ff 14 Length = 0 BitCount = 0 ID = 879
0.000100 1 36f Rx d 5 f4 f7 fe ff 14 Length = 0 BitCount = 0 ID = 879"##
.as_bytes();
let mut it = Asc2DltMsgIterator::new(0, reader, None);
let mut iterated_msgs: u32 = 0;
for m in &mut it {
iterated_msgs += 1;
// todo verify payload println!("m={:?}", m);
match iterated_msgs {
2 => assert_eq!(m.timestamp_dms, 0_u32), // the first neg. gets a 0 timestamp
3 => assert_eq!(m.timestamp_dms, 9851_u32), // 9851dms after the first one
4 => assert_eq!(m.timestamp_dms, 0_u32),
5 => assert_eq!(m.timestamp_dms, 1_u32),
_ => {}
}
}
assert_eq!(iterated_msgs, 5); // one ctrl and four can msgs
}
#[test]
fn parse_signed_time_str_1() {
assert_eq!(parse_signed_time_str("-0.123456"), -123456_i64);
assert_eq!(parse_signed_time_str("0.123456"), 123456_i64);
assert_eq!(parse_signed_time_str("0.12345"), 123450_i64); // even though one digit missing!
assert_eq!(parse_signed_time_str("-42.1"), -42100000_i64);
assert_eq!(parse_signed_time_str("4242.123456"), 4242123456_i64);
assert_eq!(parse_signed_time_str("42"), 42000000_i64);
assert_eq!(parse_signed_time_str(""), 0_i64);
assert_eq!(parse_signed_time_str("-"), 0_i64);
assert_eq!(parse_signed_time_str(".1"), 100000_i64);
assert_eq!(parse_signed_time_str("-.1"), -100000_i64);
}
}