Add debug options for bytes transmission
This commit is contained in:
		@@ -1,2 +1,3 @@
 | 
				
			|||||||
pub mod improv;
 | 
					pub mod improv;
 | 
				
			||||||
pub mod serial;
 | 
					pub mod serial;
 | 
				
			||||||
 | 
					pub mod utils;
 | 
				
			||||||
 
 | 
				
			|||||||
							
								
								
									
										38
									
								
								src/main.rs
									
									
									
									
									
								
							
							
						
						
									
										38
									
								
								src/main.rs
									
									
									
									
									
								
							@@ -7,7 +7,6 @@ use clap::{
 | 
				
			|||||||
    Subcommand,
 | 
					    Subcommand,
 | 
				
			||||||
};
 | 
					};
 | 
				
			||||||
use env_logger;
 | 
					use env_logger;
 | 
				
			||||||
use hex;
 | 
					 | 
				
			||||||
use improv_setup::improv::{
 | 
					use improv_setup::improv::{
 | 
				
			||||||
    RawPacket,
 | 
					    RawPacket,
 | 
				
			||||||
    ImprovPacket,
 | 
					    ImprovPacket,
 | 
				
			||||||
@@ -15,12 +14,6 @@ use improv_setup::improv::{
 | 
				
			|||||||
    RequestDeviceInformationPacket,
 | 
					    RequestDeviceInformationPacket,
 | 
				
			||||||
};
 | 
					};
 | 
				
			||||||
use improv_setup::serial;
 | 
					use improv_setup::serial;
 | 
				
			||||||
use log::{
 | 
					 | 
				
			||||||
    debug,
 | 
					 | 
				
			||||||
    log_enabled,
 | 
					 | 
				
			||||||
    info,
 | 
					 | 
				
			||||||
    Level,
 | 
					 | 
				
			||||||
};
 | 
					 | 
				
			||||||
use tokio_serial;
 | 
					use tokio_serial;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
#[derive(Subcommand, Clone)]
 | 
					#[derive(Subcommand, Clone)]
 | 
				
			||||||
@@ -69,37 +62,6 @@ struct Cli {
 | 
				
			|||||||
    command: Option<Commands>,
 | 
					    command: Option<Commands>,
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
fn to_ascii_debug(bytes: &Vec<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;
 | 
					 | 
				
			||||||
}
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
fn to_bytewise_debug(bytes: &Vec<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;
 | 
					 | 
				
			||||||
}
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
 | 
					
 | 
				
			||||||
#[tokio::main]
 | 
					#[tokio::main]
 | 
				
			||||||
async fn main() -> Result<()>{
 | 
					async fn main() -> Result<()>{
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -3,12 +3,20 @@ use anyhow::{
 | 
				
			|||||||
    Context,
 | 
					    Context,
 | 
				
			||||||
    Result,
 | 
					    Result,
 | 
				
			||||||
};
 | 
					};
 | 
				
			||||||
 | 
					use log::{
 | 
				
			||||||
 | 
					    debug,
 | 
				
			||||||
 | 
					    log_enabled,
 | 
				
			||||||
 | 
					    Level,
 | 
				
			||||||
 | 
					};
 | 
				
			||||||
 | 
					
 | 
				
			||||||
use crate::improv::{
 | 
					use crate::improv::{
 | 
				
			||||||
    IMPROV_HEADER,
 | 
					    IMPROV_HEADER,
 | 
				
			||||||
    RawPacket,
 | 
					    RawPacket,
 | 
				
			||||||
    ImprovDataToPacket,
 | 
					    ImprovDataToPacket,
 | 
				
			||||||
};
 | 
					};
 | 
				
			||||||
 | 
					use crate::utils::{
 | 
				
			||||||
 | 
					    to_ascii_debug,
 | 
				
			||||||
 | 
					};
 | 
				
			||||||
 | 
					
 | 
				
			||||||
pub fn find_begin_of_improv_packet(buffer: &Vec<u8>) -> Result<usize> {
 | 
					pub fn find_begin_of_improv_packet(buffer: &Vec<u8>) -> Result<usize> {
 | 
				
			||||||
    let mut improv_header_char: usize = 0;
 | 
					    let mut improv_header_char: usize = 0;
 | 
				
			||||||
@@ -47,6 +55,9 @@ impl SerialInterface {
 | 
				
			|||||||
    }
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    pub fn send_bytes(&mut self, packet_bytes: &[u8]) -> Result<()> {
 | 
					    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")?;
 | 
					        self.interface.write(packet_bytes).context("Unable to write bytes to serial interface")?;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
        Ok(())
 | 
					        Ok(())
 | 
				
			||||||
@@ -69,10 +80,20 @@ impl SerialInterface {
 | 
				
			|||||||
        let mut buffer: Vec<u8> = vec![0; available_bytes];
 | 
					        let mut buffer: Vec<u8> = vec![0; available_bytes];
 | 
				
			||||||
        self.interface.read(&mut buffer)?;
 | 
					        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_offset = find_begin_of_improv_packet(&buffer).unwrap();
 | 
				
			||||||
 | 
					
 | 
				
			||||||
        let improv_packet_end = improv_packet_offset + 10 + <u8 as Into<usize>>::into(buffer[improv_packet_offset+8]);
 | 
					        let improv_packet_end = improv_packet_offset + 10 + <u8 as Into<usize>>::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);
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 
 | 
				
			|||||||
							
								
								
									
										30
									
								
								src/utils.rs
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										30
									
								
								src/utils.rs
									
									
									
									
									
										Normal file
									
								
							@@ -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;
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
		Reference in New Issue
	
	Block a user