Skip to content

Commit

Permalink
Added support for version 0
Browse files Browse the repository at this point in the history
  • Loading branch information
JulianSchmid committed Jan 23, 2023
1 parent 3c50255 commit 41f3a3c
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 9 deletions.
17 changes: 11 additions & 6 deletions src/dlt_header.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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.
///
Expand All @@ -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<DltHeader, error::PacketSliceError> {
Expand All @@ -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,
}));
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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",
Expand All @@ -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()
Expand All @@ -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{
Expand Down
12 changes: 9 additions & 3 deletions src/dlt_packet_slice.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
}));
Expand Down Expand Up @@ -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();
Expand Down

0 comments on commit 41f3a3c

Please sign in to comment.