dlt-core 0.20.1

Parser and writer for autosar DLT Diagnostic Log and Trace
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
// Copyright 2021 by Accenture ESR
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//     http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
#[cfg(test)]
mod tests {
    use crate::{dlt::*, proptest_strategies::argument_strategy};
    use byteorder::ByteOrder;
    use proptest::prelude::*;

    use byteorder::{BigEndian, LittleEndian};
    use pretty_assertions::assert_eq;

    proptest! {
        #[test]
        fn convert_type_info_to_bytes_doesnt_crash(type_info: TypeInfo) {
            let _ = type_info.as_bytes::<BigEndian>();
        }
        #[test]
        fn length_of_dlt_arg_is_number_of_bytes(arg in argument_strategy()) {
            let expected = arg.as_bytes::<BigEndian>().len();
            let expected_using_little_endian = arg.as_bytes::<LittleEndian>().len();
            let calculated = arg.len();
            assert_eq!(
                expected,
                calculated
            );
            assert_eq!(
                expected_using_little_endian,
                calculated
            );
        }
    }

    // string coding .......................^^.^|||      ||||
    // type struct .............................^||      ||||
    // trace info ...............................^|      ||||
    // fixed point ...............................^      ||||
    #[test]
    fn test_convert_header_to_bytes() {
        // 0x37 =  0b0011 0111
        //           |||| ||||
        // version...^^^| |||| == 1
        // timestamp....^ |||| == true

        // with ses.id....^||| == false
        // with ecuid......^|| == true
        // big endian...... ^| == true
        // ext.head..........^ == true
        let header: StandardHeader = StandardHeader {
            version: 1,
            endianness: Endianness::Big,
            has_extended_header: true,
            message_counter: 0x33,
            payload_length: 0x1,
            ecu_id: Some("abc".to_string()),
            session_id: None,
            timestamp: Some(5),
        };
        assert_eq!(
            vec![
                0x37, // header-type
                0x33, // message-counter
                0x0, 0x17, // overall length
                0x61, 0x62, 0x63, 0x0, // ecu id "abc"
                0x0, 0x0, 0x0, 0x5, // timestamp
            ],
            header.as_bytes()
        );
    }

    #[test]
    fn test_filter_nothing_with_invalid_level() {
        let extended_header = ExtendedHeader {
            argument_count: 1,
            verbose: true,
            message_type: MessageType::Log(LogLevel::Invalid(1)),
            application_id: "abc".to_string(),
            context_id: "CON".to_string(),
        };
        assert!(!extended_header.skip_with_level(LogLevel::Verbose));
        assert!(!extended_header.skip_with_level(LogLevel::Invalid(0)));
        assert!(!extended_header.skip_with_level(LogLevel::Invalid(1)));
        assert!(extended_header.skip_with_level(LogLevel::Invalid(2)));
    }

    #[test]
    fn test_filter_out_non_relevant_ext_headers() {
        let extended_header = ExtendedHeader {
            argument_count: 1,
            verbose: true,
            message_type: MessageType::Log(LogLevel::Debug),
            application_id: "abc".to_string(),
            context_id: "CON".to_string(),
        };
        assert!(!extended_header.skip_with_level(LogLevel::Verbose));
        assert!(!extended_header.skip_with_level(LogLevel::Debug));
        assert!(extended_header.skip_with_level(LogLevel::Info));
        assert!(extended_header.skip_with_level(LogLevel::Warn));
        assert!(extended_header.skip_with_level(LogLevel::Error));
        assert!(extended_header.skip_with_level(LogLevel::Fatal));
        let extended_header = ExtendedHeader {
            argument_count: 1,
            verbose: true,
            message_type: MessageType::Control(ControlType::Request),
            application_id: "abc".to_string(),
            context_id: "CON".to_string(),
        };
        // other message types should not be fitered
        assert!(!extended_header.skip_with_level(LogLevel::Fatal));
    }

    #[test]
    fn test_convert_extended_header_to_bytes() {
        let extended_header = ExtendedHeader {
            argument_count: 2,
            verbose: true,
            message_type: MessageType::Log(LogLevel::Warn),
            application_id: "abc".to_string(),
            context_id: "CON".to_string(),
        };
        assert_eq!(
            vec![
                0b0011_0001, // message info MSIN
                0x2,         // arg-count
                0x61,
                0x62,
                0x63,
                0x0, // app id
                0x43,
                0x4F,
                0x4E,
                0x0, // context id
            ],
            extended_header.as_bytes()
        );
    }

    #[test]
    fn test_convert_storage_header_to_bytes() {
        let timestamp = DltTimeStamp {
            seconds: 0x4DC9_2C26,
            microseconds: 0x000C_A2D8,
        };
        let storage_header = StorageHeader {
            timestamp,
            ecu_id: "abc".to_string(),
        };
        assert_eq!(
            vec![
                0x44, 0x4C, 0x54, 0x01, // dlt tag
                0x26, 0x2C, 0xC9, 0x4D, // timestamp seconds
                0xD8, 0xA2, 0x0C, 0x0, // timestamp microseconds
                0x61, 0x62, 0x63, 0x0, // ecu id "abc"
            ],
            storage_header.as_bytes()
        );
    }

    #[test]
    fn test_convert_typeinfo_to_bytes() {
        let type_info = TypeInfo {
            kind: TypeInfoKind::Bool,
            coding: StringCoding::UTF8,
            has_variable_info: true,
            has_trace_info: false,
        };
        let type_info2 = TypeInfo {
            kind: TypeInfoKind::UnsignedFixedPoint(FloatWidth::Width32),
            coding: StringCoding::ASCII,
            has_variable_info: false,
            has_trace_info: false,
        };
        let type_info3 = TypeInfo {
            kind: TypeInfoKind::StringType,
            coding: StringCoding::UTF8,
            has_variable_info: false,
            has_trace_info: false,
        };
        //                                                        vvvv..type lenght
        // type array .....................................v      ||||
        // type string ...................................v|      ||||
        // type raw .....................................v||      ||||
        // variable info................................v|||      ||||
        //0b 1000 0100 0101
        let expected1: u32 = 0b0000_0000_0000_0000_1000_1000_0001_0000;
        let expected2: u32 = 0b0000_0000_0000_0000_0001_0000_0100_0011;
        let expected3: u32 = 0b0000_0000_0000_0000_1000_0010_0000_0000;
        // string coding .......................^^.^|||      ||||
        // type struct .............................^||      ||||
        // trace info ...............................^|      ||||
        // fixed point ...............................^      ||||
        //                                         float.....^|||
        //                                         unsigned...^||
        //                                         signed......^|
        //                                         bool.........^
        println!("expected: {:#b}", expected2);
        println!(
            "got     : {:#b}",
            BigEndian::read_u32(&type_info2.as_bytes::<BigEndian>()[..])
        );
        assert_eq!(
            expected1,
            BigEndian::read_u32(&type_info.as_bytes::<BigEndian>()[..])
        );
        assert_eq!(
            expected1,
            LittleEndian::read_u32(&type_info.as_bytes::<LittleEndian>()[..])
        );
        assert_eq!(
            expected2,
            BigEndian::read_u32(&type_info2.as_bytes::<BigEndian>()[..])
        );
        assert_eq!(
            expected2,
            LittleEndian::read_u32(&type_info2.as_bytes::<LittleEndian>()[..])
        );
        assert_eq!(
            expected3,
            BigEndian::read_u32(&type_info3.as_bytes::<BigEndian>()[..])
        );
        assert_eq!(
            expected3,
            LittleEndian::read_u32(&type_info3.as_bytes::<LittleEndian>()[..])
        );
    }

    #[test]
    fn test_convert_bool_argument_to_bytes() {
        let type_info = TypeInfo {
            kind: TypeInfoKind::Bool,
            coding: StringCoding::UTF8,
            has_variable_info: true,
            has_trace_info: false,
        };
        let argument = Argument {
            type_info: type_info.clone(),
            name: Some("foo".to_string()),
            unit: None,
            fixed_point: None,
            value: Value::Bool(0x1),
        };
        let mut expected = type_info.as_bytes::<BigEndian>();
        expected.extend(vec![0x0, 0x4]); // length of name + zero
        expected.extend(b"foo\0");
        expected.extend(vec![0x1]); // value for bool (true == 1)
        assert_eq!(expected, argument.as_bytes::<BigEndian>());

        // now without variable info
        let type_info2 = TypeInfo {
            kind: TypeInfoKind::Bool,
            coding: StringCoding::UTF8,
            has_variable_info: true,
            has_trace_info: false,
        };
        let mut expected2 = type_info2.as_bytes::<BigEndian>();
        let argument2 = Argument {
            type_info: type_info2,
            name: None,
            unit: None,
            fixed_point: None,
            value: Value::Bool(0x1),
        };
        expected2.extend(vec![0x1]); // value for bool (true == 1)
        assert_eq!(expected2, argument2.as_bytes::<BigEndian>());
    }

    #[test]
    fn test_convert_uint_argument_to_bytes() {
        let type_info = TypeInfo {
            kind: TypeInfoKind::Unsigned(TypeLength::BitLength32),
            coding: StringCoding::UTF8,
            has_variable_info: true,
            has_trace_info: false,
        };
        let mut expected = type_info.as_bytes::<BigEndian>();
        let argument = Argument {
            type_info,
            name: Some("speed".to_string()),
            unit: Some("mph".to_string()),
            fixed_point: None,
            value: Value::U32(0x33),
        };
        expected.extend(vec![0x0, 0x6]); // length of name + zero
        expected.extend(vec![0x0, 0x4]); // length of unit + zero
        expected.extend(b"speed\0");
        expected.extend(b"mph\0");
        let mut buf = [0; 4];
        BigEndian::write_u32(&mut buf, 0x33);
        expected.extend(&buf); // value
        assert_eq!(expected, argument.as_bytes::<BigEndian>());

        // now without variable info
        let type_info = TypeInfo {
            kind: TypeInfoKind::Unsigned(TypeLength::BitLength32),
            coding: StringCoding::UTF8,
            has_variable_info: false,
            has_trace_info: false,
        };
        let mut expected = type_info.as_bytes::<BigEndian>();
        let argument = Argument {
            type_info,
            name: None,
            unit: None,
            fixed_point: None,
            value: Value::U32(0x33),
        };
        let mut buf = [0; 4];
        BigEndian::write_u32(&mut buf, 0x33);
        expected.extend(&buf); // value
        assert_eq!(expected, argument.as_bytes::<BigEndian>());
    }

    #[test]
    fn test_convert_sint_argument_to_bytes() {
        let type_info = TypeInfo {
            kind: TypeInfoKind::Signed(TypeLength::BitLength32),
            coding: StringCoding::UTF8,
            has_variable_info: true,
            has_trace_info: false,
        };
        let mut expected = type_info.as_bytes::<BigEndian>();
        let argument = Argument {
            type_info,
            name: Some("speed".to_string()),
            unit: Some("mph".to_string()),
            fixed_point: None,
            value: Value::I32(-0x33),
        };
        expected.extend(vec![0x0, 0x6]); // length of name + zero
        expected.extend(vec![0x0, 0x4]); // length of unit + zero
        expected.extend(b"speed\0");
        expected.extend(b"mph\0");
        let mut buf = [0; 4];
        BigEndian::write_i32(&mut buf, -0x33);
        expected.extend(&buf); // value
        assert_eq!(expected, argument.as_bytes::<BigEndian>());

        // now without variable info
        let type_info = TypeInfo {
            kind: TypeInfoKind::Signed(TypeLength::BitLength32),
            coding: StringCoding::UTF8,
            has_variable_info: false,
            has_trace_info: false,
        };
        let mut expected = type_info.as_bytes::<BigEndian>();
        let argument = Argument {
            type_info,
            name: None,
            unit: None,
            fixed_point: None,
            value: Value::I32(-0x33),
        };
        let mut buf = [0; 4];
        BigEndian::write_i32(&mut buf, -0x33);
        expected.extend(&buf); // value
        assert_eq!(expected, argument.as_bytes::<BigEndian>());
    }

    #[test]
    fn test_convert_float_argument_to_bytes() {
        let type_info = TypeInfo {
            kind: TypeInfoKind::Float(FloatWidth::Width32),
            coding: StringCoding::UTF8,
            has_variable_info: true,
            has_trace_info: false,
        };
        let mut expected = type_info.as_bytes::<BigEndian>();
        let argument = Argument {
            type_info,
            name: Some("speed".to_string()),
            unit: Some("mph".to_string()),
            fixed_point: None,
            value: Value::F32(123.98f32),
        };
        expected.extend(vec![0x0, 0x6]); // length of name + zero
        expected.extend(vec![0x0, 0x4]); // length of unit + zero
        expected.extend(b"speed\0");
        expected.extend(b"mph\0");
        let mut buf = [0; 4];
        BigEndian::write_f32(&mut buf, 123.98f32);
        expected.extend(&buf); // value
        assert_eq!(expected, argument.as_bytes::<BigEndian>());

        // now without variable info
        let type_info = TypeInfo {
            kind: TypeInfoKind::Float(FloatWidth::Width64),
            coding: StringCoding::UTF8,
            has_variable_info: false,
            has_trace_info: false,
        };
        let mut expected = type_info.as_bytes::<BigEndian>();
        let argument = Argument {
            type_info,
            name: None,
            unit: None,
            fixed_point: None,
            value: Value::F64(123.98f64),
        };
        let mut buf = [0; 8];
        BigEndian::write_f64(&mut buf, 123.98f64);
        expected.extend(&buf); // value
        assert_eq!(expected, argument.as_bytes::<BigEndian>());
    }

    #[test]
    fn test_convert_string_argument_to_bytes() {
        let type_info = TypeInfo {
            kind: TypeInfoKind::StringType,
            coding: StringCoding::UTF8,
            has_variable_info: true,
            has_trace_info: false,
        };
        let mut expected = type_info.as_bytes::<BigEndian>();
        let argument = Argument {
            type_info,
            name: Some("speed".to_string()),
            unit: None,
            fixed_point: None,
            value: Value::StringVal("foo".to_string()),
        };
        expected.extend(vec![0x0, 0x4]); // length of value + zero
        expected.extend(vec![0x0, 0x6]); // length of name + zero
        expected.extend(b"speed\0"); // name
        expected.extend(b"foo\0"); // value
        assert_eq!(expected, argument.as_bytes::<BigEndian>());

        // now without variable info
        let type_info = TypeInfo {
            kind: TypeInfoKind::StringType,
            coding: StringCoding::UTF8,
            has_variable_info: false,
            has_trace_info: false,
        };
        let mut expected = type_info.as_bytes::<BigEndian>();
        let argument = Argument {
            type_info,
            name: None,
            unit: None,
            fixed_point: None,
            value: Value::StringVal("foo".to_string()),
        };
        expected.extend(vec![0x0, 0x4]); // length of value + zero
        expected.extend(b"foo\0"); // value
        let argument_bytes = argument.as_bytes::<BigEndian>();
        println!("{:02X?}", argument_bytes);
        assert_eq!(expected, argument_bytes);
    }

    #[test]
    fn test_convert_fixedpoint_argument_to_bytes() {
        let type_info = TypeInfo {
            kind: TypeInfoKind::SignedFixedPoint(FloatWidth::Width32),
            coding: StringCoding::UTF8,
            has_variable_info: true,
            has_trace_info: false,
        };
        let mut expected = type_info.as_bytes::<BigEndian>();
        let argument = Argument {
            type_info,
            name: Some("speed".to_string()),
            unit: Some("mph".to_string()),
            value: Value::I32(-44),
            fixed_point: Some(FixedPoint {
                quantization: 1.5,
                offset: FixedPointValue::I32(-200),
            }),
        };
        expected.extend(vec![0x0, 0x6]); // length of name + zero
        expected.extend(vec![0x0, 0x4]); // length of unit + zero
        expected.extend(b"speed\0");
        expected.extend(b"mph\0");
        let mut buf = [0; 4];
        BigEndian::write_f32(&mut buf, 1.5f32);
        expected.extend(&buf); // value
        BigEndian::write_i32(&mut buf, -200);
        expected.extend(&buf); // value
        BigEndian::write_i32(&mut buf, -44);
        expected.extend(&buf); // value
        assert_eq!(expected, argument.as_bytes::<BigEndian>());

        // now without variable info
        let type_info = TypeInfo {
            kind: TypeInfoKind::SignedFixedPoint(FloatWidth::Width32),
            coding: StringCoding::UTF8,
            has_variable_info: false,
            has_trace_info: false,
        };
        let mut expected = type_info.as_bytes::<BigEndian>();
        let argument = Argument {
            type_info,
            name: None,
            unit: None,
            value: Value::I32(-44),
            fixed_point: Some(FixedPoint {
                quantization: 1.5,
                offset: FixedPointValue::I32(-200),
            }),
        };
        let mut buf = [0; 4];
        BigEndian::write_f32(&mut buf, 1.5f32);
        expected.extend(&buf); // value
        BigEndian::write_i32(&mut buf, -200);
        expected.extend(&buf); // value
        BigEndian::write_i32(&mut buf, -44);
        expected.extend(&buf); // value
        assert_eq!(expected, argument.as_bytes::<BigEndian>());
    }

    #[test]
    fn test_convert_raw_argument_to_bytes() {
        let type_info = TypeInfo {
            kind: TypeInfoKind::Raw,
            coding: StringCoding::UTF8,
            has_variable_info: true,
            has_trace_info: false,
        };
        let mut expected = type_info.as_bytes::<BigEndian>();
        let argument = Argument {
            type_info,
            name: Some("foo".to_string()),
            unit: None,
            value: Value::Raw(vec![0xD, 0xE, 0xA, 0xD]),
            fixed_point: Some(FixedPoint {
                quantization: 1.5,
                offset: FixedPointValue::I32(-200),
            }),
        };
        expected.extend(vec![0x0, 0x4]); // length of raw data bytes
        expected.extend(vec![0x0, 0x4]); // length of name + zero
        expected.extend(b"foo\0");
        expected.extend(vec![0xD, 0xE, 0xA, 0xD]);
        assert_eq!(expected, argument.as_bytes::<BigEndian>());

        // now without variable info
        let type_info = TypeInfo {
            kind: TypeInfoKind::Raw,
            coding: StringCoding::UTF8,
            has_variable_info: false,
            has_trace_info: false,
        };
        let mut expected = type_info.as_bytes::<BigEndian>();
        let argument = Argument {
            type_info,
            name: None,
            unit: None,
            value: Value::Raw(vec![0xD, 0xE, 0xA, 0xD]),
            fixed_point: Some(FixedPoint {
                quantization: 1.5,
                offset: FixedPointValue::I32(-200),
            }),
        };
        expected.extend(vec![0x0, 0x4]); // length of raw data bytes
        expected.extend(vec![0xD, 0xE, 0xA, 0xD]);
        assert_eq!(expected, argument.as_bytes::<BigEndian>());
    }
}