Skip to content

Commit

Permalink
Moved NvPayload and added tests
Browse files Browse the repository at this point in the history
  • Loading branch information
JulianSchmid committed May 2, 2024
1 parent c786686 commit 4554c71
Show file tree
Hide file tree
Showing 3 changed files with 76 additions and 44 deletions.
45 changes: 1 addition & 44 deletions src/dlt_typed_payload.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
use crate::{
verbose::VerboseIter, DltControlMessageType, DltLogLevel, DltNetworkType, DltTraceType,
};
use crate::{*, verbose::VerboseIter};

/// Typed payload of a DLT log message based on the message info in the DLT
/// extended header.
Expand Down Expand Up @@ -48,47 +46,6 @@ pub enum DltTypedPayload<'a> {
ControlV(ControlVPayload<'a>),
}

/// A non verbose message of unknown type.
///
/// This type is used if the DLT message did not contain an extended
/// header and the type of message can only be determined via the
/// message id. In this the type of th message can only be determined
/// based on the message id and an additional information source describing
/// how to decode the message payloads and what type of message it is
/// (e.g. a Fibex file).
#[derive(Debug, Clone, Eq, PartialEq, Hash)]
pub struct NvPayload<'a> {
pub msg_id: u32,
pub payload: &'a [u8],
}

impl<'a> From<LogNvPayload<'a>> for NvPayload<'a> {
fn from(value: LogNvPayload<'a>) -> Self {
NvPayload {
msg_id: value.msg_id,
payload: value.payload,
}
}
}

impl<'a> From<TraceNvPayload<'a>> for NvPayload<'a> {
fn from(value: TraceNvPayload<'a>) -> Self {
NvPayload {
msg_id: value.msg_id,
payload: value.payload,
}
}
}

impl<'a> From<NetworkNvPayload<'a>> for NvPayload<'a> {
fn from(value: NetworkNvPayload<'a>) -> Self {
NvPayload {
msg_id: value.msg_id,
payload: value.payload,
}
}
}

/// Non verbose log message (does not contain a description of it's contents).
///
/// Non verbose log messages cannot be decoded without additional
Expand Down
3 changes: 3 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -218,6 +218,9 @@ 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;

Expand Down
72 changes: 72 additions & 0 deletions src/nv_payload.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
use crate::*;

/// A non verbose message of unknown type.
///
/// This type is used if the DLT message did not contain an extended
/// header and the type of message can only be determined via the
/// message id. In this the type of th message can only be determined
/// based on the message id and an additional information source describing
/// how to decode the message payloads and what type of message it is
/// (e.g. a Fibex file).
#[derive(Debug, Clone, Eq, PartialEq, Hash)]
pub struct NvPayload<'a> {
pub msg_id: u32,
pub payload: &'a [u8],
}

impl<'a> From<LogNvPayload<'a>> for NvPayload<'a> {
fn from(value: LogNvPayload<'a>) -> Self {
NvPayload {
msg_id: value.msg_id,
payload: value.payload,
}
}
}

impl<'a> From<TraceNvPayload<'a>> for NvPayload<'a> {
fn from(value: TraceNvPayload<'a>) -> Self {
NvPayload {
msg_id: value.msg_id,
payload: value.payload,
}
}
}

impl<'a> From<NetworkNvPayload<'a>> for NvPayload<'a> {
fn from(value: NetworkNvPayload<'a>) -> Self {
NvPayload {
msg_id: value.msg_id,
payload: value.payload,
}
}
}

#[cfg(test)]
mod tests {
use super::*;

#[test]
fn from() {
let data = [5,6,7,8];
let msg_id = 1234_5678u32;
let payload = &data;

// LogNvPayload
assert_eq!(
NvPayload::from(LogNvPayload{ msg_id, payload, log_level: DltLogLevel::Info }),
NvPayload{ msg_id, payload }
);

// TraceNvPayload
assert_eq!(
NvPayload::from(TraceNvPayload{ msg_id, payload, trace_type: DltTraceType::State }),
NvPayload{ msg_id, payload }
);

// TraceNvPayload
assert_eq!(
NvPayload::from(NetworkNvPayload{msg_id,payload, net_type: DltNetworkType::Flexray }),
NvPayload{ msg_id, payload }
);
}
}

0 comments on commit 4554c71

Please sign in to comment.