alexanderaffink / dlt-parse-rs

Rust DLT (Diagnostic Log and Trace) packet parser

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Crates.io docs.rs build status github build status appveyor build status gitlab codecov

dlt_parse

A zero allocation rust library for basic parsing & writing DLT (Diagnostic Log and Trace) packets. Currently only the parsing and writing of the header is supported (excluding the verbose packet definitions).

Usage:

First, add the following to your Cargo.toml:

[dependencies]
dlt_parse = "0.7.2"

Next, add this to your crate:

use dlt_parse;

What is dlt_parse?

dlt_parse is a library that aims to provide serialisation & deserialisation funtions for DLT (Diagnostic Log and Trace) packets. It should make it possible to anlyse recordings of DLT packets as fast as possible, as well as writing servers that send DLT packets to the network.

Some key points are:

  • It is completly written in Rust and thoroughly tested.
  • Special attention has been paid to not use allocations or syscalls.
  • It is possible to use the crate in an no-std environment.
  • The package is still in development and can & will still change.
  • Methods for parsing verbose DLT packets are still missing (but maybe will be implemented in future versions).

Example: Serializing & Slicing/Deserializing DLT Packets

In this example a non verbose DLT packet is serialized and deserialized again. Specificly the serialized packet is converted into a DltPacketSlice. This has the advantage, that not all fields have to be deserialied to access the payload or specific fields in the header. Note that it is also possible to completely deserialize DLT headers with the DltHeader::read function. This can make sense, if most fields of the header are used anyways.

use self::dlt_parse::{DltHeader, DltLogLevel, DltExtendedHeader, SliceIterator};

let header = {
    let mut header = DltHeader {
        is_big_endian: true, //payload & message id are encoded with big endian
        version: 0,
        message_counter: 0,
        ecu_id: None,
        session_id: None,
        timestamp: None,
        extended_header: Some(DltExtendedHeader::new_non_verbose_log(
            DltLogLevel::Debug,
            [b'a', b'p', b'p', b'i'],//application id
            [b'c', b't', b'x', b'i'],//context id
        ))
    };
    header.length = header.header_len() + 4 + 4; //header + message id + payload

    header
};

//buffer to store serialized header & payload
let mut buffer = Vec::<u8>::with_capacity(usize::from(header.length));
header.write(&mut buffer).unwrap();

//write payload (message id 1234 & non verbose payload)
{
    //for write_all
    use std::io::Write;

    //write the message id & payload
    buffer.write_all(&1234u32.to_be_bytes()).unwrap(); //message id
    buffer.write_all(&[5,6,7,9]); //payload
}

//packets can contain multiple dlt messages, iterate through them
for dlt_message in SliceIterator::new(&buffer) {
    match dlt_message {
        Ok(dlt_slice) => {
            //check if the message is verbose or non verbose (non verbose messages have message ids)
            if let Some((message_id, non_verbose_payload)) = dlt_slice.message_id_and_payload() {
                println!("non verbose message {:x}", message_id);
                println!("  with payload {:?}", non_verbose_payload);
            } else {
                println!("verbose message (parsing not yet supported)");
            }
        },
        Err(err) => {
            //error parsing the dlt packet
            println!("ERROR: {:?}", err);
        }
    }
}

An complete example which includes the parsing of the ethernet & udp headers can be found in examples/print_messages_ids.rs

References

License

Licensed under either of Apache License, Version 2.0 or MIT license at your option. The corresponding license texts can be found in the LICENSE-APACHE file and the LICENSE-MIT file.

Contribution

Unless you explicitly state otherwise, any contribution intentionally submitted for inclusion in the work by you shall be licensed as above, without any additional terms or conditions.

About

Rust DLT (Diagnostic Log and Trace) packet parser

License:Apache License 2.0


Languages

Language:Rust 97.4%Language:Shell 2.6%