Add debug options for bytes transmission

This commit is contained in:
clerie 2024-12-25 16:21:50 +01:00
parent e189d1b37f
commit 9b15774114
4 changed files with 53 additions and 39 deletions

View File

@ -1,2 +1,3 @@
pub mod improv;
pub mod serial;
pub mod utils;

View File

@ -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<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]
async fn main() -> Result<()>{

View File

@ -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<u8>) -> Result<usize> {
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<u8> = 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 + <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
View 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;
}