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 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738
//! 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 & parsing of verbose messages.
//!
//! # Usage:
//!
//! By default `serde` is disabled and `std` is enabled if you add `dlt_parse` as dependency to your `Cargo.toml`:
//!
//! ```toml
//! [dependencies]
//! dlt_parse = "0.10.0"
//! ```
//!
//! If you additionally want `serde` support you will have to activate the `serde` feature in your `Cargo.toml`:
//!
//! ```toml
//! [dependencies]
//! dlt_parse = { version = "0.10.0", features = ["serde"] }
//! ```
//!
//! If you want to use the crate in `no_std` mode you will have to disable the default features:
//!
//! ```toml
//! [dependencies]
//! dlt_parse = { version = "0.10.0", default-features = false }
//! ```
//!
//! # 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.
//!
//! # 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};
//! use std::io::Write;
//! use dlt_parse::{LogNvPayload, ControlNvPayload, LogVPayload};
//!
//! let header = {
//! let mut header = DltHeader {
//! is_big_endian: true, // payload & message id are encoded with big endian
//! message_counter: 0,
//! length: 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));
//! buffer.extend_from_slice(&header.to_bytes());
//!
//! // 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 what type of message was received
//! match dlt_slice.typed_payload() {
//! Ok(typed_payload) => {
//! use dlt_parse::DltTypedPayload::*;
//! match typed_payload {
//! UnknownNv(p) => {
//! println!(
//! "non verbose message 0x{:x} (unknown) with {} bytes of payload.",
//! p.msg_id,
//! p.payload.len(),
//! );
//! },
//! LogNv(p) => {
//! println!(
//! "non verbose log message 0x{:x} with log level {:?} and {} bytes of payload.",
//! p.msg_id,
//! p.log_level,
//! p.payload.len(),
//! );
//! }
//! LogV(p) => {
//! println!("verbose log message with log level {:?} and values:", p.log_level);
//! for value in p.iter {
//! println!(" {:?}", value);
//! }
//! }
//! TraceNv(p) => {
//! println!(
//! "non verbose trace message 0x{:x} of type {:?} and {} bytes of payload.",
//! p.msg_id,
//! p.trace_type,
//! p.payload.len(),
//! );
//! },
//! TraceV(p) => {
//! println!("verbose trace message with of type {:?} and values:", p.trace_type);
//! for value in p.iter {
//! println!(" {:?}", value);
//! }
//! },
//! NetworkNv(p) => {
//! println!(
//! "non verbose network message 0x{:x} of type {:?} and {} bytes of payload.",
//! p.msg_id,
//! p.net_type,
//! p.payload.len(),
//! );
//! },
//! NetworkV(p) => {
//! println!("verbose network message with of type {:?} and values:", p.net_type);
//! for value in p.iter {
//! println!(" {:?}", value);
//! }
//! },
//! ControlNv(p) => {
//! println!(
//! "non verbose control message {:?} with service id: {} and {} bytes of payload.",
//! p.msg_type,
//! p.service_id,
//! p.payload.len(),
//! );
//! }
//! ControlV(p) => {
//! println!("verbose control message {:?} with values:", p.msg_type);
//! for value in p.iter {
//! println!(" {:?}", value);
//! }
//! },
//! }
//! }
//! Err(err) => {
//! println!("message with payload error received: {}", err);
//! }
//! }
//! },
//! 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](https://github.com/JulianSchmid/dlt-parse-rs/blob/v0.8.2/examples/print_messages_ids.rs)
//!
//! # References
//! * [Log and Trace Protocol Specification](https://www.autosar.org/fileadmin/standards/foundation/1-3/AUTOSAR_PRS_LogAndTraceProtocol.pdf)
// # Reason for 'bool_comparison' disable:
//
// Clippy triggers triggers errors like the following if the warning stays enabled:
//
// warning: equality checks against false can be replaced by a negation
// --> src/packet_decoder.rs:131:20
// |
// 131 | if false == fragmented {
// | ^^^^^^^^^^^^^^^^^^^ help: try simplifying it as shown: `!fragmented`
//
//
// I prefer to write `false == value` instead of `!value` as it
// is more visually striking and is not as easy to overlook as the single
// character '!'.
#![allow(clippy::bool_comparison)]
#![no_std]
#[cfg(test)]
extern crate alloc;
#[cfg(test)]
extern crate proptest;
#[cfg(any(feature = "std", test))]
extern crate std;
#[cfg(test)]
#[macro_use]
extern crate assert_matches;
mod dlt_extended_header;
pub use dlt_extended_header::*;
mod dlt_header;
pub use dlt_header::*;
mod dlt_typed_payload;
pub use dlt_typed_payload::*;
mod dlt_message_info;
pub use dlt_message_info::*;
mod dlt_packet_slice;
pub use dlt_packet_slice::*;
mod dlt_slice_iterator;
pub use dlt_slice_iterator::*;
mod nv_payload;
pub use nv_payload::*;
/// Control message related types & functions.
pub mod control;
/// Errors that can be returned by functions in dlt_parse.
pub mod error;
/// Module containing "verbose DLT" encoding & decoding structs & functions.
pub mod verbose;
/// Module for decoding .dlt files or other formats that use the DLT storage header.
pub mod storage;
#[cfg(test)]
use alloc::{format, vec, vec::Vec};
use arrayvec::ArrayVec;
use core::slice::from_raw_parts;
#[cfg(feature = "std")]
use std::io;
#[cfg(test)]
mod proptest_generators;
/// Maximum value that can be encoded in the DLT header version field (has only 3 bits).
const MAX_VERSION: u8 = 0b111;
const EXTDENDED_HEADER_FLAG: u8 = 0b1;
const BIG_ENDIAN_FLAG: u8 = 0b10;
const ECU_ID_FLAG: u8 = 0b100;
const SESSION_ID_FLAG: u8 = 0b1000;
const TIMESTAMP_FLAG: u8 = 0b10000;
/// Flag in extended header message info indicating that the message is verbose.
pub const EXT_MSIN_VERB_FLAG: u8 = 0b0000_0001;
///Shifted value in the msin extended header field for dlt "log" messages.
const EXT_MSIN_MSTP_TYPE_LOG: u8 = 0x0 << 1;
///Shifted value in the msin extended header field for dlt "trace" messages.
const EXT_MSIN_MSTP_TYPE_TRACE: u8 = 0x1 << 1;
///Shifted value in the msin extended header field for dlt "network trace" messages.
const EXT_MSIN_MSTP_TYPE_NW_TRACE: u8 = 0x2 << 1;
///Shifted value in the msin extended header field for dlt "control" messages.
const EXT_MSIN_MSTP_TYPE_CONTROL: u8 = 0x3 << 1;
///Log level for dlt log messages.
#[derive(Copy, Clone, Debug, Eq, PartialEq, Ord, PartialOrd, Hash)]
pub enum DltLogLevel {
///Fatal system error.
Fatal = 0x1,
///SWC error.
Error = 0x2,
///Correct behavior cannot be ensured.
Warn = 0x3,
///Message of LogLevel type “Information”.
Info = 0x4,
///Message of LogLevel type “Debug”.
Debug = 0x5,
///Message of LogLevel type "Verbose".
Verbose = 0x6,
}
///Types of application trace messages that can be sent via dlt if the message type
///is specified as "trace".
#[derive(Copy, Clone, Debug, Eq, PartialEq, Ord, PartialOrd, Hash)]
pub enum DltTraceType {
///Value of variable.
Variable = 0x1,
///Call of a function.
FunctionIn = 0x2,
///Return of a function.
FunctionOut = 0x3,
///State of a state machine.
State = 0x4,
///RTE Events.
Vfb = 0x5,
}
///Network type specified in a network trace dlt message.
#[derive(Copy, Clone, Debug, Eq, PartialEq, Ord, PartialOrd, Hash)]
pub enum DltNetworkType {
///Inter-Process-Communication.
Ipc,
///CAN communication bus.
Can,
///FlexRay communication bus.
Flexray,
///Most communication bus.
Most,
///Ethernet communication bus.
Ethernet,
///SOME/IP communication.
SomeIp,
///User defined settings (note that the maximum allowed value is 0xf or 15).
UserDefined(u8),
}
#[derive(Copy, Clone, Debug, Eq, PartialEq, Ord, PartialOrd, Hash)]
pub enum DltControlMessageType {
///Request control message.
Request = 0x1,
///Respond control message.
Response = 0x2,
}
///Message type info field (contains the the information of the message type & message type info field)
#[derive(Copy, Clone, Debug, Eq, PartialEq, Ord, PartialOrd, Hash)]
pub enum DltMessageType {
///Dlt log message with a log level
Log(DltLogLevel),
///Message tracing the value of a function, variable or other parts of the system.
Trace(DltTraceType),
///Message containing tracing informations from a networking system.
NetworkTrace(DltNetworkType),
///A dlt control message (e.g. for setting the log level).
Control(DltControlMessageType),
}
impl DltMessageType {
/// Attempts to read the message type from the first byte of
/// the dlt extended message header.
fn from_byte(value: u8) -> Option<DltMessageType> {
use DltMessageType::*;
const MSIN_MASK: u8 = 0b1111_0000;
match value & 0b0000_1110 {
EXT_MSIN_MSTP_TYPE_LOG => {
use DltLogLevel::*;
match (value & MSIN_MASK) >> 4 {
0x1 => Some(Log(Fatal)),
0x2 => Some(Log(Error)),
0x3 => Some(Log(Warn)),
0x4 => Some(Log(Info)),
0x5 => Some(Log(Debug)),
0x6 => Some(Log(Verbose)),
//undefined values
_ => None,
}
}
EXT_MSIN_MSTP_TYPE_TRACE => {
use DltTraceType::*;
match (value & MSIN_MASK) >> 4 {
0x1 => Some(Trace(Variable)),
0x2 => Some(Trace(FunctionIn)),
0x3 => Some(Trace(FunctionOut)),
0x4 => Some(Trace(State)),
0x5 => Some(Trace(Vfb)),
//undefined values
_ => None,
}
}
EXT_MSIN_MSTP_TYPE_NW_TRACE => {
use DltNetworkType::*;
match (value & MSIN_MASK) >> 4 {
0x1 => Some(NetworkTrace(Ipc)),
0x2 => Some(NetworkTrace(Can)),
0x3 => Some(NetworkTrace(Flexray)),
0x4 => Some(NetworkTrace(Most)),
0x5 => Some(NetworkTrace(Ethernet)),
0x6 => Some(NetworkTrace(SomeIp)),
//user defined
other => Some(NetworkTrace(UserDefined(other))),
}
}
EXT_MSIN_MSTP_TYPE_CONTROL => {
use DltControlMessageType::*;
match (value & MSIN_MASK) >> 4 {
0x1 => Some(Control(Request)),
0x2 => Some(Control(Response)),
//undefined values
_ => None,
}
}
_ => None,
}
}
///Set message type info and based on that the message type.
pub fn to_byte(&self) -> Result<u8, error::RangeError> {
use error::RangeError::NetworkTypekUserDefinedOutsideOfRange;
use DltMessageType::*;
use DltNetworkType::UserDefined;
//check ranges
if let NetworkTrace(UserDefined(user_defined_value)) = *self {
if !(7..=0xf).contains(&user_defined_value) {
return Err(NetworkTypekUserDefinedOutsideOfRange(user_defined_value));
}
}
//determine message type & message type info
let (message_type, message_type_info) = match self {
Log(ref level) => (EXT_MSIN_MSTP_TYPE_LOG, *level as u8),
Trace(ref trace_type) => (EXT_MSIN_MSTP_TYPE_TRACE, *trace_type as u8),
NetworkTrace(ref nw_trace_type) => {
use DltNetworkType::*;
(
EXT_MSIN_MSTP_TYPE_NW_TRACE,
match *nw_trace_type {
Ipc => 0x1,
Can => 0x2,
Flexray => 0x3,
Most => 0x4,
Ethernet => 0x5,
SomeIp => 0x6,
UserDefined(value) => value,
},
)
}
Control(ref control_msg_type) => (EXT_MSIN_MSTP_TYPE_CONTROL, *control_msg_type as u8),
};
Ok(message_type | ((message_type_info << 4) & 0b1111_0000))
}
}
#[cfg(test)]
mod tests {
use super::*;
mod dlt_log_level {
use super::*;
use DltLogLevel::*;
#[test]
fn clone_eq() {
const VALUES: [(DltLogLevel, u8); 6] = [
(Fatal, 1),
(Error, 2),
(Warn, 3),
(Info, 4),
(Debug, 5),
(Verbose, 6),
];
for v0 in &VALUES {
// identity property
assert_eq!(v0.0, v0.0.clone());
assert_eq!(v0.0 as u8, v0.1);
for v1 in &VALUES {
assert_eq!(v0.0 != v1.0, v0.1 != v1.1,);
}
}
}
#[test]
fn debug() {
const VALUES: [(DltLogLevel, &str); 6] = [
(Fatal, "Fatal"),
(Error, "Error"),
(Warn, "Warn"),
(Info, "Info"),
(Debug, "Debug"),
(Verbose, "Verbose"),
];
for v in &VALUES {
assert_eq!(v.1, format!("{:?}", v.0));
}
}
}
mod dlt_trace_type {
use super::*;
use DltTraceType::*;
#[test]
fn clone_eq() {
const VALUES: [(DltTraceType, u8); 5] = [
(Variable, 1),
(FunctionIn, 2),
(FunctionOut, 3),
(State, 4),
(Vfb, 5),
];
for v0 in &VALUES {
// identity property
assert_eq!(v0.0, v0.0.clone());
assert_eq!(v0.0 as u8, v0.1);
for v1 in &VALUES {
assert_eq!(v0.0 != v1.0, v0.1 != v1.1,);
}
}
}
#[test]
fn debug() {
const VALUES: [(DltTraceType, &str); 5] = [
(Variable, "Variable"),
(FunctionIn, "FunctionIn"),
(FunctionOut, "FunctionOut"),
(State, "State"),
(Vfb, "Vfb"),
];
for v in &VALUES {
assert_eq!(v.1, format!("{:?}", v.0));
}
}
}
mod dlt_network_type {
use super::*;
use DltNetworkType::*;
#[test]
fn clone_eq() {
const VALUES: [(DltNetworkType, u8); 8] = [
(Ipc, 1),
(Can, 2),
(Flexray, 3),
(Most, 4),
(Ethernet, 5),
(SomeIp, 6),
(UserDefined(0x7), 0x7),
(UserDefined(0xf), 0xf),
];
for v0 in &VALUES {
assert_eq!(v0.0, v0.0.clone());
for v1 in &VALUES {
assert_eq!(v0.0 != v1.0, v0.1 != v1.1,);
}
}
}
#[test]
fn debug() {
const VALUES: [(DltNetworkType, &str); 8] = [
(Ipc, "Ipc"),
(Can, "Can"),
(Flexray, "Flexray"),
(Most, "Most"),
(Ethernet, "Ethernet"),
(SomeIp, "SomeIp"),
(UserDefined(0x7), "UserDefined(7)"),
(UserDefined(0xf), "UserDefined(15)"),
];
for v in &VALUES {
assert_eq!(v.1, format!("{:?}", v.0));
}
}
}
mod dlt_control_message_type {
use super::*;
use DltControlMessageType::*;
#[test]
fn clone_eq() {
const VALUES: [(DltControlMessageType, u8); 2] = [(Request, 1), (Response, 2)];
for v0 in &VALUES {
// identity property
assert_eq!(v0.0, v0.0.clone());
assert_eq!(v0.0.clone() as u8, v0.1);
for v1 in &VALUES {
assert_eq!(v0.0 != v1.0, v0.1 != v1.1,);
}
}
}
#[test]
fn debug() {
const VALUES: [(DltControlMessageType, &str); 2] =
[(Request, "Request"), (Response, "Response")];
for v in &VALUES {
assert_eq!(v.1, format!("{:?}", v.0));
}
}
}
mod dlt_message_type {
use super::*;
use DltControlMessageType::*;
use DltLogLevel::*;
use DltMessageType::*;
use DltNetworkType::*;
use DltTraceType::*;
const VALUES: [(DltMessageType, u8); 28] = [
(Log(Fatal), 0b0001_0000),
(Log(Error), 0b0010_0000),
(Log(Warn), 0b0011_0000),
(Log(Info), 0b0100_0000),
(Log(Debug), 0b0101_0000),
(Log(Verbose), 0b0110_0000),
(Trace(Variable), 0b0001_0010),
(Trace(FunctionIn), 0b0010_0010),
(Trace(FunctionOut), 0b0011_0010),
(Trace(State), 0b0100_0010),
(Trace(Vfb), 0b0101_0010),
(NetworkTrace(Ipc), 0b0001_0100),
(NetworkTrace(Can), 0b0010_0100),
(NetworkTrace(Flexray), 0b0011_0100),
(NetworkTrace(Most), 0b0100_0100),
(NetworkTrace(Ethernet), 0b0101_0100),
(NetworkTrace(SomeIp), 0b0110_0100),
(NetworkTrace(UserDefined(0x7)), 0b0111_0100),
(NetworkTrace(UserDefined(0x8)), 0b1000_0100),
(NetworkTrace(UserDefined(0x9)), 0b1001_0100),
(NetworkTrace(UserDefined(0xA)), 0b1010_0100),
(NetworkTrace(UserDefined(0xB)), 0b1011_0100),
(NetworkTrace(UserDefined(0xC)), 0b1100_0100),
(NetworkTrace(UserDefined(0xD)), 0b1101_0100),
(NetworkTrace(UserDefined(0xE)), 0b1110_0100),
(NetworkTrace(UserDefined(0xF)), 0b1111_0100),
(Control(Request), 0b0001_0110),
(Control(Response), 0b0010_0110),
];
#[test]
fn clone_eq() {
for v0 in &VALUES {
// identity property
assert_eq!(v0.0, v0.0.clone());
for v1 in &VALUES {
assert_eq!(v0.0 != v1.0, v0.1 != v1.1,);
}
}
}
#[test]
fn debug() {
const DBG_VALUES: [(DltMessageType, &str); 5] = [
(Log(Fatal), "Log(Fatal)"),
(Trace(Variable), "Trace(Variable)"),
(NetworkTrace(Ipc), "NetworkTrace(Ipc)"),
(
NetworkTrace(UserDefined(0x7)),
"NetworkTrace(UserDefined(7))",
),
(Control(Request), "Control(Request)"),
];
for v in &DBG_VALUES {
assert_eq!(v.1, format!("{:?}", v.0));
}
}
#[test]
fn from_byte() {
// valid values
for value in &VALUES {
assert_eq!(value.0, DltMessageType::from_byte(value.1).unwrap());
// with verbose flag set
assert_eq!(value.0, DltMessageType::from_byte(value.1 | 1).unwrap());
}
// invalid log
assert!(DltMessageType::from_byte(0).is_none());
assert!(DltMessageType::from_byte(1).is_none()); // with verbose
for i in 7..=0b1111 {
assert!(DltMessageType::from_byte(i << 4).is_none());
// with verbose
assert!(DltMessageType::from_byte((i << 4) | 1).is_none());
}
// invalid trace
assert!(DltMessageType::from_byte(0b0000_0010).is_none());
assert!(DltMessageType::from_byte(0b0000_0011).is_none());
for i in 6..=0b1111 {
assert!(DltMessageType::from_byte((i << 4) | 0b0010).is_none());
// with verbose
assert!(DltMessageType::from_byte((i << 4) | 0b0011).is_none());
}
// invalid control
assert!(DltMessageType::from_byte(0b0000_0110).is_none());
assert!(DltMessageType::from_byte(0b0000_0111).is_none());
for i in 3..=0b1111 {
assert!(DltMessageType::from_byte((i << 4) | 0b0110).is_none());
// with verbose
assert!(DltMessageType::from_byte((i << 4) | 0b0111).is_none());
}
}
#[test]
fn to_byte() {
// valid values
for value in &VALUES {
assert_eq!(value.0.to_byte().unwrap(), value.1);
}
// invalid user defined errors
// first run two explicitly to check the error contains the
// actual value
use error::RangeError::NetworkTypekUserDefinedOutsideOfRange;
assert_matches!(
NetworkTrace(UserDefined(0)).to_byte().unwrap_err(),
NetworkTypekUserDefinedOutsideOfRange(0)
);
assert_matches!(
NetworkTrace(UserDefined(1)).to_byte().unwrap_err(),
NetworkTypekUserDefinedOutsideOfRange(1)
);
// check the rest of the range of invalid values
for value in 0..7 {
assert_matches!(
NetworkTrace(UserDefined(value)).to_byte().unwrap_err(),
NetworkTypekUserDefinedOutsideOfRange(_)
);
}
for value in 16..=0xff {
assert_matches!(
NetworkTrace(UserDefined(value)).to_byte().unwrap_err(),
NetworkTypekUserDefinedOutsideOfRange(_)
);
}
}
}
} // mod tests