From b41b8b57d7b81a44a538e9fa33e8f1476a20313c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Patrick=20Jos=C3=A9=20Pereira?= Date: Tue, 15 Apr 2025 17:17:28 -0300 Subject: [PATCH 01/14] Cargo: Add tracing-subscriber MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Patrick José Pereira --- Cargo.toml | 1 + 1 file changed, 1 insertion(+) 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] From 191bc3e4299563a5e148266f742b8b71d6c75c45 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Patrick=20Jos=C3=A9=20Pereira?= Date: Tue, 15 Apr 2025 17:17:42 -0300 Subject: [PATCH 02/14] examples: ping_1d: Add tracing MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Patrick José Pereira --- examples/ping_1d.rs | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) 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; From 599e80cc10608b562b04bcecd79b3f05d03a4dd8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Patrick=20Jos=C3=A9=20Pereira?= Date: Tue, 15 Apr 2025 17:17:53 -0300 Subject: [PATCH 03/14] examples: ping_360: Add tracing MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Patrick José Pereira --- examples/ping_360.rs | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/examples/ping_360.rs b/examples/ping_360.rs index 0a71c9cd48..81dcfa82f2 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; From 845f43c45c96974cbb1d8ec3b1adf45ebd0f31a3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Patrick=20Jos=C3=A9=20Pereira?= Date: Tue, 15 Apr 2025 17:18:04 -0300 Subject: [PATCH 04/14] examples: ping_common: Add tracing MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Patrick José Pereira --- examples/ping_common.rs | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/examples/ping_common.rs b/examples/ping_common.rs index 4612bb2964..63e7cceca7 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; From 2e2f1441366c2c673eafedfb282b3258786416a2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Patrick=20Jos=C3=A9=20Pereira?= Date: Tue, 15 Apr 2025 17:19:02 -0300 Subject: [PATCH 05/14] codec: Add info about decoder state MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Patrick José Pereira --- src/codec.rs | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) 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) From d4df6390f70ec4670fce0e5f11fd031f6055a909 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Patrick=20Jos=C3=A9=20Pereira?= Date: Tue, 15 Apr 2025 17:19:53 -0300 Subject: [PATCH 06/14] decoder: Add state inside InProgress MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Patrick José Pereira --- src/decoder.rs | 32 +++++++++++++++++--------------- 1 file changed, 17 insertions(+), 15 deletions(-) diff --git a/src/decoder.rs b/src/decoder.rs index 7913b2e40e..e5d007fedd 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,7 +80,7 @@ impl Decoder { } self.buffer.clear(); } - return DecoderResult::InProgress; + return DecoderResult::InProgress(self.state.clone()); } DecoderState::ReadingPayload => { self.buffer.push(byte); @@ -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()); } } } From 7dae433726f45e1e7bdfc3c220f6a102ceebd662 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Patrick=20Jos=C3=A9=20Pereira?= Date: Tue, 15 Apr 2025 17:20:13 -0300 Subject: [PATCH 07/14] decoder: Make text more clear MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Patrick José Pereira --- src/decoder.rs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/decoder.rs b/src/decoder.rs index e5d007fedd..371d28ad8d 100644 --- a/src/decoder.rs +++ b/src/decoder.rs @@ -84,8 +84,8 @@ impl Decoder { } DecoderState::ReadingPayload => { self.buffer.push(byte); - info!( - "DecoderState : ReadingPayload {:?} {:?}", + debug!( + "DecoderState : ReadingPayload {:?} of {:?}", self.buffer.len(), self.message.payload_length ); From 5af3f747a59e7f45be2ecd4894739a52d1867e99 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Patrick=20Jos=C3=A9=20Pereira?= Date: Tue, 15 Apr 2025 17:20:26 -0300 Subject: [PATCH 08/14] tests: deserialize: Remove lost debug code MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Patrick José Pereira --- tests/deserialize.rs | 1 - 1 file changed, 1 deletion(-) diff --git a/tests/deserialize.rs b/tests/deserialize.rs index fd1129aaef..e0993469b2 100644 --- a/tests/deserialize.rs +++ b/tests/deserialize.rs @@ -44,7 +44,6 @@ fn test_simple_deserialization() { // 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 From ba22d9b82993dcdd5099d8498428979083fd92b1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Patrick=20Jos=C3=A9=20Pereira?= Date: Tue, 15 Apr 2025 17:21:53 -0300 Subject: [PATCH 09/14] examples: ping_360: Print device_information as debug MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Patrick José Pereira --- examples/ping_360.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/examples/ping_360.rs b/examples/ping_360.rs index 81dcfa82f2..f66a1864b8 100644 --- a/examples/ping_360.rs +++ b/examples/ping_360.rs @@ -46,7 +46,7 @@ async fn main() -> Result<(), PingError> { ); println!("Protocol version is: {version}"); - println!("Device information: {device_information:?}"); + println!("Device information: {device_information:#?}"); Ok(()) } From b5e2e9cd2677c4321cf77cb97519efe4e94fc255 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Patrick=20Jos=C3=A9=20Pereira?= Date: Tue, 15 Apr 2025 17:22:06 -0300 Subject: [PATCH 10/14] examples: ping_common: Print device information as debug MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Patrick José Pereira --- examples/ping_common.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/examples/ping_common.rs b/examples/ping_common.rs index 63e7cceca7..4f4acf5e2f 100644 --- a/examples/ping_common.rs +++ b/examples/ping_common.rs @@ -56,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..."); From 4b03d64d1c4e075474ffbce65d4b4974f45e03f0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Patrick=20Jos=C3=A9=20Pereira?= Date: Tue, 15 Apr 2025 17:32:54 -0300 Subject: [PATCH 11/14] tests: deserialize: Update to use new api MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Patrick José Pereira --- tests/deserialize.rs | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/tests/deserialize.rs b/tests/deserialize.rs index e0993469b2..f2a2bc0cf9 100644 --- a/tests/deserialize.rs +++ b/tests/deserialize.rs @@ -30,12 +30,12 @@ fn test_simple_deserialization() { info!("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); @@ -46,12 +46,12 @@ fn test_simple_deserialization() { for byte in &buffer[0..buffer.len() - 2] { 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 From 7c556a38e945e60d432cc7e28acd7c539e03a7cb Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Patrick=20Jos=C3=A9=20Pereira?= Date: Wed, 16 Apr 2025 10:53:33 -0300 Subject: [PATCH 12/14] device: Change message from error to trace MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Patrick José Pereira --- src/device.rs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) 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:?}"); } } } From f1cddd2f7f3cd43f48f8daa842387921e7ec2a90 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Patrick=20Jos=C3=A9=20Pereira?= Date: Wed, 16 Apr 2025 10:54:07 -0300 Subject: [PATCH 13/14] tests: deserialize: Change from info to debug MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Patrick José Pereira --- tests/deserialize.rs | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/tests/deserialize.rs b/tests/deserialize.rs index f2a2bc0cf9..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,7 +27,7 @@ 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(_) @@ -38,7 +38,7 @@ fn test_simple_deserialization() { 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"); }; From a05f3037e626445c2b8c6771e5b17e0e0245387a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Patrick=20Jos=C3=A9=20Pereira?= Date: Wed, 16 Apr 2025 13:13:56 -0300 Subject: [PATCH 14/14] examples: common: Add configure_tracing MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Patrick José Pereira --- examples/common/mod.rs | 9 +++++++++ 1 file changed, 9 insertions(+) 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();