From 41f3a3c39fb18664626a4aaa69e3ab477222cb58 Mon Sep 17 00:00:00 2001 From: Julian Schmid Date: Mon, 23 Jan 2023 19:32:45 +0100 Subject: [PATCH] Added support for version 0 --- src/dlt_header.rs | 17 +++++++++++------ src/dlt_packet_slice.rs | 12 +++++++++--- 2 files changed, 20 insertions(+), 9 deletions(-) diff --git a/src/dlt_header.rs b/src/dlt_header.rs index 47752d8..77b3fe6 100644 --- a/src/dlt_header.rs +++ b/src/dlt_header.rs @@ -16,7 +16,7 @@ pub struct DltHeader { impl DltHeader { /// Versions of the DLT header that can be decoded by the decoding /// functions in this library. - pub const SUPPORTED_DECODABLE_VERSIONS: [u8; 1] = [1]; + pub const SUPPORTED_DECODABLE_VERSIONS: [u8; 2] = [0, 1]; /// The maximum size in bytes/octets a V1 DLT header can be when encoded. /// @@ -29,7 +29,7 @@ impl DltHeader { /// * 10 bytes for the extended header pub const MAX_SERIALIZED_SIZE: usize = 4 + 4 + 4 + 4 + 10; - /// Supported dlt version. + /// Version that will be written into the DLT header version field when writing this header. pub const VERSION: u8 = 1; pub fn from_slice(slice: &[u8]) -> Result { @@ -50,7 +50,7 @@ impl DltHeader { // check version let version = (header_type >> 5) & MAX_VERSION; - if DltHeader::VERSION != version { + if 0 != version && 1 != version { return Err(UnsupportedDltVersion(UnsupportedDltVersionError { unsupported_version: version, })); @@ -322,7 +322,7 @@ impl DltHeader { // check version let version = (header_type >> 5) & MAX_VERSION; - if 1 != version { + if 0 != version && 1 != version { return Err(error::ReadError::UnsupportedDltVersion( UnsupportedDltVersionError { unsupported_version: version, @@ -487,6 +487,7 @@ mod dlt_header_tests { proptest! { #[test] fn to_bytes_from_slice( + version in 0..=1u8, ref dlt_header in dlt_header_any(), unsupported_version in (0u8..0b111u8).prop_filter( "version must be unknown", @@ -496,7 +497,12 @@ mod dlt_header_tests { use error::PacketSliceError::*; // ok case { - let bytes = dlt_header.to_bytes(); + let bytes = { + let mut bytes = dlt_header.to_bytes(); + // inject the supported version number + bytes[0] = (bytes[0] & 0b0001_1111) | ((version << 5) & 0b1110_0000); + bytes + }; assert_eq!( dlt_header.clone(), DltHeader::from_slice(&bytes[..]).unwrap() @@ -506,7 +512,6 @@ mod dlt_header_tests { { for l in 0..dlt_header.header_len() as usize { let bytes = dlt_header.to_bytes(); - assert_eq!( UnexpectedEndOfSlice( error::UnexpectedEndOfSliceError{ diff --git a/src/dlt_packet_slice.rs b/src/dlt_packet_slice.rs index 10e21c0..939decb 100644 --- a/src/dlt_packet_slice.rs +++ b/src/dlt_packet_slice.rs @@ -27,7 +27,7 @@ impl<'a> DltPacketSlice<'a> { // check version let version = (header_type >> 5) & MAX_VERSION; - if DltHeader::VERSION != version { + if 0 != version && 1 != version { return Err(UnsupportedDltVersion(UnsupportedDltVersionError { unsupported_version: version, })); @@ -429,14 +429,20 @@ mod dlt_packet_slice_tests { proptest! { #[test] fn from_slice( - ref packet in dlt_header_with_payload_any() + ref packet in dlt_header_with_payload_any(), + version in 0..=1u8, ) { use error::PacketSliceError::*; let mut buffer = Vec::with_capacity( packet.1.len() + usize::from(packet.0.header_len()) ); - buffer.extend_from_slice(&packet.0.to_bytes()); + buffer.extend_from_slice(&{ + let mut bytes = packet.0.to_bytes(); + // inject the supported version number + bytes[0] = (bytes[0] & 0b0001_1111) | ((version << 5) & 0b1110_0000); + bytes + }); buffer.extend_from_slice(&packet.1[..]); //read the slice let slice = DltPacketSlice::from_slice(&buffer[..]).unwrap();