From 9b15774114416b9d5c39533c872624d07f6ecf5d Mon Sep 17 00:00:00 2001 From: clerie Date: Wed, 25 Dec 2024 16:21:50 +0100 Subject: [PATCH] Add debug options for bytes transmission --- src/lib.rs | 1 + src/main.rs | 38 -------------------------------------- src/serial.rs | 23 ++++++++++++++++++++++- src/utils.rs | 30 ++++++++++++++++++++++++++++++ 4 files changed, 53 insertions(+), 39 deletions(-) create mode 100644 src/utils.rs diff --git a/src/lib.rs b/src/lib.rs index 8a68c0f..6acb909 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -1,2 +1,3 @@ pub mod improv; pub mod serial; +pub mod utils; diff --git a/src/main.rs b/src/main.rs index f219691..8c93256 100644 --- a/src/main.rs +++ b/src/main.rs @@ -7,7 +7,6 @@ use clap::{ Subcommand, }; use env_logger; -use hex; use improv_setup::improv::{ RawPacket, ImprovPacket, @@ -15,12 +14,6 @@ use improv_setup::improv::{ RequestDeviceInformationPacket, }; use improv_setup::serial; -use log::{ - debug, - log_enabled, - info, - Level, -}; use tokio_serial; #[derive(Subcommand, Clone)] @@ -69,37 +62,6 @@ struct Cli { command: Option, } -fn to_ascii_debug(bytes: &Vec) -> String { - let mut out = String::new(); - - for b in bytes { - if b.is_ascii_graphic() { - out += &b.escape_ascii().to_string(); - } - else { - out.push_str("."); - } - } - - return out; -} - -fn to_bytewise_debug(bytes: &Vec) -> String { - let mut out = String::new(); - - for b in bytes { - out += &hex::encode(&[b.clone()]); - out += " "; - - if b.is_ascii_graphic() { - out += &b.escape_ascii().to_string(); - } - out += "\n"; - } - - return out; -} - #[tokio::main] async fn main() -> Result<()>{ diff --git a/src/serial.rs b/src/serial.rs index fed4c74..5b43eee 100644 --- a/src/serial.rs +++ b/src/serial.rs @@ -3,12 +3,20 @@ use anyhow::{ Context, Result, }; +use log::{ + debug, + log_enabled, + Level, +}; use crate::improv::{ IMPROV_HEADER, RawPacket, ImprovDataToPacket, }; +use crate::utils::{ + to_ascii_debug, +}; pub fn find_begin_of_improv_packet(buffer: &Vec) -> Result { let mut improv_header_char: usize = 0; @@ -47,6 +55,9 @@ impl SerialInterface { } pub fn send_bytes(&mut self, packet_bytes: &[u8]) -> Result<()> { + if log_enabled!(Level::Debug) { + debug!("Sending packet: \n{}\n{}", hex::encode(packet_bytes), to_ascii_debug(packet_bytes)); + } self.interface.write(packet_bytes).context("Unable to write bytes to serial interface")?; Ok(()) @@ -69,10 +80,20 @@ impl SerialInterface { let mut buffer: Vec = vec![0; available_bytes]; self.interface.read(&mut buffer)?; + if log_enabled!(Level::Debug) { + debug!("Received bytes: \n{}\n{}", hex::encode(&buffer), to_ascii_debug(&buffer)); + } + let improv_packet_offset = find_begin_of_improv_packet(&buffer).unwrap(); let improv_packet_end = improv_packet_offset + 10 + >::into(buffer[improv_packet_offset+8]); - return Ok(buffer[improv_packet_offset..improv_packet_end].to_vec()); + let packet_bytes = buffer[improv_packet_offset..improv_packet_end].to_vec(); + + if log_enabled!(Level::Debug) { + debug!("Received packet: \n{}\n{}", hex::encode(&packet_bytes), to_ascii_debug(&packet_bytes)); + } + + return Ok(packet_bytes); } } diff --git a/src/utils.rs b/src/utils.rs new file mode 100644 index 0000000..11403f4 --- /dev/null +++ b/src/utils.rs @@ -0,0 +1,30 @@ +pub fn to_ascii_debug(bytes: &[u8]) -> String { + let mut out = String::new(); + + for b in bytes { + if b.is_ascii_graphic() { + out += &b.escape_ascii().to_string(); + } + else { + out.push_str("."); + } + } + + return out; +} + +pub fn to_bytewise_debug(bytes: &[u8]) -> String { + let mut out = String::new(); + + for b in bytes { + out += &hex::encode(&[b.clone()]); + out += " "; + + if b.is_ascii_graphic() { + out += &b.escape_ascii().to_string(); + } + out += "\n"; + } + + return out; +}