From 4ac70150d6af5af9e33a09ced626beb377b75142 Mon Sep 17 00:00:00 2001 From: Julian Schmid Date: Mon, 25 Sep 2023 20:45:16 +0200 Subject: [PATCH] Add new strict storage reader mode constructor --- src/lib.rs | 1 - src/storage/dlt_storage_reader.rs | 106 ++++++++++++++++++++++++++++++ 2 files changed, 106 insertions(+), 1 deletion(-) diff --git a/src/lib.rs b/src/lib.rs index f934a09..d9a6ab6 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -138,7 +138,6 @@ // is more visually striking and is not as easy to overlook as the single // character '!'. #![allow(clippy::bool_comparison)] - #![no_std] #[cfg(test)] diff --git a/src/storage/dlt_storage_reader.rs b/src/storage/dlt_storage_reader.rs index 339b0a3..72299d5 100644 --- a/src/storage/dlt_storage_reader.rs +++ b/src/storage/dlt_storage_reader.rs @@ -54,6 +54,20 @@ impl DltStorageReader { } } + /// Creates a new reader that does not allow corrupted data + /// and does NOT seek to the next storage pattern whenever + /// corrupted data is encountered. + pub fn new_strict(reader: R) -> DltStorageReader { + DltStorageReader { + reader, + is_seeking_storage_pattern: false, + last_packet: Vec::with_capacity(u16::MAX as usize), + read_error: false, + num_read_packets: 0, + num_pattern_seeks: 0, + } + } + /// Returns if the reader will seek storage headers if corrupted /// data is present between packets. #[inline] @@ -376,6 +390,98 @@ mod dlt_storage_reader_tests { assert!(reader.next_packet().is_none()); } + // reader with working packets (strict) + { + // build two packets + let storage_header0 = StorageHeader { + timestamp_seconds: 1, + timestamp_microseconds: 2, + ecu_id: [0, 0, 0, 0], + }; + let packet0 = { + let mut packet = Vec::new(); + let mut header = DltHeader { + is_big_endian: true, + message_counter: 1, + length: 0, // set afterwords + ecu_id: None, + session_id: None, + timestamp: None, + extended_header: None, + }; + header.length = header.header_len() + 4; + header.write(&mut packet).unwrap(); + // set version to 0 + packet[0] = packet[0] & 0b0001_1111; + packet.extend_from_slice(&[1, 2, 3, 4]); + packet + }; + + let storage_header1 = StorageHeader { + timestamp_seconds: 3, + timestamp_microseconds: 4, + ecu_id: [5, 6, 7, 8], + }; + let packet1 = { + let mut packet = Vec::new(); + let mut header = DltHeader { + is_big_endian: true, + message_counter: 2, + length: 0, // set afterwords + ecu_id: None, + session_id: None, + timestamp: None, + extended_header: None, + }; + header.length = header.header_len() + 6; + header.write(&mut packet).unwrap(); + packet.extend_from_slice(&[10, 11, 12, 13, 14, 15]); + packet + }; + + // compose data + let mut v = Vec::new(); + v.extend_from_slice(&storage_header0.to_bytes()); + v.extend_from_slice(&packet0); + v.extend_from_slice(&storage_header1.to_bytes()); + v.extend_from_slice(&packet1); + // add some dummy data to test that an error gets triggered + v.extend_from_slice(&[0u8; StorageHeader::BYTE_LEN]); + + // check result + let mut reader = DltStorageReader::new_strict(BufReader::new(Cursor::new(&v[..]))); + assert!(false == reader.is_seeking_storage_pattern()); + assert_eq!(0, reader.num_read_packets()); + assert_eq!(0, reader.num_pattern_seeks()); + + assert_eq!( + reader.next_packet().unwrap().unwrap(), + StorageSlice { + storage_header: storage_header0, + packet: DltPacketSlice::from_slice(&packet0).unwrap() + } + ); + assert_eq!(1, reader.num_read_packets()); + assert_eq!(0, reader.num_pattern_seeks()); + + assert_eq!( + reader.next_packet().unwrap().unwrap(), + StorageSlice { + storage_header: storage_header1.clone(), + packet: DltPacketSlice::from_slice(&packet1).unwrap() + } + ); + assert_eq!(2, reader.num_read_packets()); + assert_eq!(0, reader.num_pattern_seeks()); + + assert_matches!( + reader.next_packet(), + Some(Err(ReadError::StorageHeaderStartPattern(_))) + ); + + assert!(reader.next_packet().is_none()); + } + // reader with error during buffering (non seeking) { let mut buf = BufferFillErrorReader {};