diff --git a/Cargo.toml b/Cargo.toml index 3b68242ff7..5327287027 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -26,6 +26,7 @@ serde_bytes = "0.11" [dev-dependencies] tracing-test = "0.2.4" +tracing-subscriber = "0.3.19" udp-stream = "0.0.12" [build-dependencies] diff --git a/examples/common/mod.rs b/examples/common/mod.rs index faaaf74c16..d06082e860 100644 --- a/examples/common/mod.rs +++ b/examples/common/mod.rs @@ -5,6 +5,7 @@ use std::{ str::FromStr, }; use tokio_serial::{SerialPort, SerialPortBuilderExt}; +use tracing_subscriber; use udp_stream::UdpStream; #[derive(Parser, Debug)] @@ -25,6 +26,14 @@ pub enum Port { Udp(udp_stream::UdpStream), } +pub fn configure_tracing() { + tracing_subscriber::fmt() + .with_env_filter(tracing_subscriber::EnvFilter::from_default_env()) + .with_line_number(true) + .with_file(true) + .init(); +} + pub async fn create_port() -> Port { let args = Args::parse(); diff --git a/examples/ping_1d.rs b/examples/ping_1d.rs index cea8b2536a..a6677039ab 100644 --- a/examples/ping_1d.rs +++ b/examples/ping_1d.rs @@ -1,5 +1,5 @@ mod common; -use common::{create_port, Port}; +use common::{configure_tracing, create_port, Port}; use std::convert::TryFrom; use bluerobotics_ping::{ @@ -12,6 +12,8 @@ use bluerobotics_ping::{ #[tokio::main] async fn main() -> Result<(), PingError> { + configure_tracing(); + println!("Parsing user provided values and creating port..."); let port = create_port().await; diff --git a/examples/ping_360.rs b/examples/ping_360.rs index 0a71c9cd48..f66a1864b8 100644 --- a/examples/ping_360.rs +++ b/examples/ping_360.rs @@ -1,5 +1,5 @@ mod common; -use common::{create_port, Port}; +use common::{configure_tracing, create_port, Port}; use bluerobotics_ping::{ device::{Ping360, PingDevice}, @@ -8,6 +8,8 @@ use bluerobotics_ping::{ #[tokio::main] async fn main() -> Result<(), PingError> { + configure_tracing(); + println!("Parsing user provided values and creating port..."); let port = create_port().await; @@ -44,7 +46,7 @@ async fn main() -> Result<(), PingError> { ); println!("Protocol version is: {version}"); - println!("Device information: {device_information:?}"); + println!("Device information: {device_information:#?}"); Ok(()) } diff --git a/examples/ping_common.rs b/examples/ping_common.rs index 4612bb2964..4f4acf5e2f 100644 --- a/examples/ping_common.rs +++ b/examples/ping_common.rs @@ -1,5 +1,5 @@ mod common; -use common::{create_port, Port}; +use common::{configure_tracing, create_port, Port}; use bluerobotics_ping::{ common::Device, device::PingDevice, error::PingError, message::ProtocolMessage, @@ -7,6 +7,8 @@ use bluerobotics_ping::{ #[tokio::main] async fn main() -> Result<(), PingError> { + configure_tracing(); + println!("Parsing user provided values and creating port..."); let port = create_port().await; @@ -54,7 +56,7 @@ async fn main() -> Result<(), PingError> { ); println!("Protocol version is: {version}"); - println!("Device information: \n {device_information_struct:?}"); + println!("Device information: \n {device_information_struct:#?}"); // Read the same 2 packages from previous requests, but from subscriber task, all above tasks have success, we did it! println!("Checking if subscriber returns with 2 same packages..."); diff --git a/src/codec.rs b/src/codec.rs index 8b3c5d1905..85b54616e2 100644 --- a/src/codec.rs +++ b/src/codec.rs @@ -1,6 +1,7 @@ use crate::{decoder::Decoder as PingDecoder, error::PingError, message::ProtocolMessage}; use bytes::{Buf, BytesMut}; use tokio_util::codec::{Decoder, Encoder}; +use tracing::debug; pub struct PingCodec { decoder: PingDecoder, @@ -27,8 +28,10 @@ impl Decoder for PingCodec { return Ok(None); }; - match decoder.parse_byte(*byte) { - crate::decoder::DecoderResult::InProgress => { + let state = decoder.parse_byte(*byte); + debug!("Decoder state: {:?}", state); + match state { + crate::decoder::DecoderResult::InProgress(_) => { consumed += 1; if consumed == src.len() { src.advance(consumed) diff --git a/src/decoder.rs b/src/decoder.rs index 7913b2e40e..371d28ad8d 100644 --- a/src/decoder.rs +++ b/src/decoder.rs @@ -1,4 +1,4 @@ -use tracing::info; +use tracing::debug; use crate::message::{ProtocolMessage, HEADER}; #[cfg(feature = "serde")] @@ -12,14 +12,7 @@ pub enum ParseError { ChecksumError(ProtocolMessage), } -#[derive(Debug)] -pub enum DecoderResult { - Success(ProtocolMessage), - InProgress, - Error(ParseError), -} - -#[derive(Debug)] +#[derive(Clone, Debug)] pub enum DecoderState { AwaitingStart1, AwaitingStart2, @@ -28,6 +21,13 @@ pub enum DecoderState { ReadingChecksum, } +#[derive(Debug)] +pub enum DecoderResult { + Success(ProtocolMessage), + InProgress(DecoderState), + Error(ParseError), +} + pub struct Decoder { pub state: DecoderState, buffer: Vec, @@ -44,11 +44,13 @@ impl Decoder { } pub fn parse_byte(&mut self, byte: u8) -> DecoderResult { - match self.state { + debug!("Parsing byte: 0x{byte:02x} ({byte})"); + let state = &self.state; + match state { DecoderState::AwaitingStart1 => { if byte == HEADER[0] { self.state = DecoderState::AwaitingStart2; - return DecoderResult::InProgress; + return DecoderResult::InProgress(self.state.clone()); } return DecoderResult::Error(ParseError::InvalidStartByte); } @@ -56,7 +58,7 @@ impl Decoder { if byte == HEADER[1] { self.state = DecoderState::ReadingHeader; self.buffer.clear(); - return DecoderResult::InProgress; + return DecoderResult::InProgress(self.state.clone()); } self.state = DecoderState::AwaitingStart1; return DecoderResult::Error(ParseError::InvalidStartByte); @@ -78,12 +80,12 @@ impl Decoder { } self.buffer.clear(); } - return DecoderResult::InProgress; + return DecoderResult::InProgress(self.state.clone()); } DecoderState::ReadingPayload => { self.buffer.push(byte); - info!( - "DecoderState : ReadingPayload {:?} {:?}", + debug!( + "DecoderState : ReadingPayload {:?} of {:?}", self.buffer.len(), self.message.payload_length ); @@ -92,7 +94,7 @@ impl Decoder { self.state = DecoderState::ReadingChecksum; self.buffer.clear(); } - return DecoderResult::InProgress; + return DecoderResult::InProgress(self.state.clone()); } DecoderState::ReadingChecksum => { self.buffer.push(byte); @@ -106,7 +108,7 @@ impl Decoder { } return DecoderResult::Success(message); } - return DecoderResult::InProgress; + return DecoderResult::InProgress(self.state.clone()); } } } diff --git a/src/device.rs b/src/device.rs index 119565b439..d1dbb8f9f7 100644 --- a/src/device.rs +++ b/src/device.rs @@ -13,7 +13,7 @@ use tokio::{ task::JoinHandle, }; use tokio_util::codec::{Decoder, Framed}; -use tracing::{error, info}; +use tracing::{error, info, trace}; use crate::{ codec::PingCodec, @@ -90,7 +90,7 @@ impl Common { }; } Err(e) => { - error!("{e:?}"); + trace!("{e:?}"); } } } diff --git a/tests/deserialize.rs b/tests/deserialize.rs index fd1129aaef..a7d78160d9 100644 --- a/tests/deserialize.rs +++ b/tests/deserialize.rs @@ -3,7 +3,7 @@ use std::convert::TryFrom; use bluerobotics_ping::common::Messages as common_messages; use bluerobotics_ping::decoder::*; use bluerobotics_ping::{common, Messages}; -use tracing::info; +use tracing::debug; use tracing_test::traced_test; #[traced_test] @@ -27,32 +27,31 @@ fn test_simple_deserialization() { assert_eq!(general_request, parsed); for byte in &buffer[0..buffer.len() - 2] { - info!("byte : {byte}, {:?}", &decoder.state); + debug!("byte : {byte}, {:?}", &decoder.state); assert!(matches!( decoder.parse_byte(byte.clone()), - DecoderResult::InProgress + DecoderResult::InProgress(_) )); } assert!(matches!( decoder.parse_byte(buffer[buffer.len() - 2]), - DecoderResult::InProgress + DecoderResult::InProgress(_) )); let DecoderResult::Success(_message) = decoder.parse_byte(buffer[buffer.len() - 1]) else { - info!("Decoder state: {:?}", decoder.state); + debug!("Decoder state: {:?}", decoder.state); panic!("Failed to use decoder with valid message"); }; // Retry with a wrong receipt CRC for byte in &buffer[0..buffer.len() - 2] { - dbg!(byte, &decoder.state); assert!(matches!( decoder.parse_byte(byte.clone()), - DecoderResult::InProgress + DecoderResult::InProgress(_) )); } assert!(matches!( decoder.parse_byte(buffer[buffer.len() - 2]), - DecoderResult::InProgress + DecoderResult::InProgress(_) )); assert!(matches!( decoder.parse_byte(0x01), // force a crc error