From c181709e29499afb7607cc87dfea5b7bb724f524 Mon Sep 17 00:00:00 2001 From: clerie Date: Mon, 23 Dec 2024 15:34:51 +0100 Subject: [PATCH] Move serial interface to seperate module --- src/lib.rs | 1 + src/main.rs | 63 ++++++++------------------------------------------- src/serial.rs | 59 +++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 69 insertions(+), 54 deletions(-) create mode 100644 src/serial.rs diff --git a/src/lib.rs b/src/lib.rs index 8db7725..8a68c0f 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -1 +1,2 @@ pub mod improv; +pub mod serial; diff --git a/src/main.rs b/src/main.rs index 574793e..2d22d56 100644 --- a/src/main.rs +++ b/src/main.rs @@ -21,6 +21,7 @@ use improv_setup::improv::{ RequestCurrentStateCommand, RequestDeviceInformationPacket, }; +use improv_setup::serial; use log::{ debug, log_enabled, @@ -105,29 +106,6 @@ fn to_bytewise_debug(bytes: &Vec) -> String { } -fn find_begin_of_improv_packet(buffer: &Vec) -> Result { - - let mut improv_header_char: usize = 0; - - for (i, b) in buffer.iter().enumerate() { - if b == &IMPROV_HEADER[improv_header_char] { - improv_header_char += 1; - - if improv_header_char >= IMPROV_HEADER.len() { - return Ok(i - (IMPROV_HEADER.len() - 1)); - } - } - else { - improv_header_char = 0; - if b == &IMPROV_HEADER[improv_header_char] { - improv_header_char += 1; - } - } - } - - return Err(String::from("Improv header not found")); -} - #[tokio::main] async fn main() -> Result<()>{ env_logger::init(); @@ -152,31 +130,12 @@ async fn main() -> Result<()>{ DeviceCommands::State => { let request_current_state_packet = (RequestCurrentStateCommand {}).to_raw_packet(); - println!("{}", hex::encode(&request_current_state_packet.to_bytes())); - println!("{}", to_ascii_debug(&request_current_state_packet.to_bytes())); + let mut serial_interface = serial::SerialInterface::new(path, *baud_rate).expect("Couldn't init serial interface"); - let mut serial_interface = tokio_serial::new(path, *baud_rate).open().unwrap(); + serial_interface.send_bytes(&request_current_state_packet.to_bytes()); - serial_interface.write(&request_current_state_packet.to_bytes()).unwrap(); - - let mut buffer: Vec = Vec::new(); - serial_interface.read_to_end(&mut buffer); - - println!("{}", hex::encode(&buffer)); - println!("{}", to_ascii_debug(&buffer)); - println!("{}", std::str::from_utf8(&buffer).unwrap_or("")); - - let improv_packet_offset = find_begin_of_improv_packet(&buffer).unwrap(); - println!("{}", improv_packet_offset); - - let improv_packet_end = improv_packet_offset + 10 + >::into(buffer[improv_packet_offset+8]); - let raw_packet = RawPacket::try_from_bytes(&buffer[improv_packet_offset..improv_packet_end].to_vec()).unwrap(); - - // version - println!("Version: {}", &raw_packet.version); - - // type - println!("Type: {}", &raw_packet.r#type); + let result_bytes = serial_interface.recv_bytes().expect("Couldn't receive any improv packet"); + let raw_packet = RawPacket::try_from_bytes(&result_bytes).unwrap(); if let ImprovPacket::CurrentStateResponse(current_state_response) = ImprovPacket::try_from_raw_packet(&raw_packet).unwrap() { println!("Current state: {}", ¤t_state_response.current_state); @@ -192,16 +151,12 @@ async fn main() -> Result<()>{ DeviceCommands::Info => { let request_device_information_packet = (RequestDeviceInformationPacket {}).to_raw_packet(); - let mut serial_interface = tokio_serial::new(path, *baud_rate).open().unwrap(); - serial_interface.write(&request_device_information_packet.to_bytes()).unwrap(); + let mut serial_interface = serial::SerialInterface::new(path, *baud_rate).expect("Couldn't init serial interface"); - let mut buffer: Vec = Vec::new(); - serial_interface.read_to_end(&mut buffer); + serial_interface.send_bytes(&request_device_information_packet.to_bytes()); - 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]); - let raw_packet = RawPacket::try_from_bytes(&buffer[improv_packet_offset..improv_packet_end].to_vec()).unwrap(); + let result_bytes = serial_interface.recv_bytes().expect("Couldn't receive any improv packet"); + let raw_packet = RawPacket::try_from_bytes(&result_bytes).unwrap(); if let ImprovPacket::RPCResult(rpc_result) = ImprovPacket::try_from_raw_packet(&raw_packet).unwrap() { for r in rpc_result.results { diff --git a/src/serial.rs b/src/serial.rs new file mode 100644 index 0000000..752d4ee --- /dev/null +++ b/src/serial.rs @@ -0,0 +1,59 @@ +use anyhow::{ + Result, +}; + +use crate::improv::{ + IMPROV_HEADER, +}; + +pub fn find_begin_of_improv_packet(buffer: &Vec) -> Result { + let mut improv_header_char: usize = 0; + + for (i, b) in buffer.iter().enumerate() { + if b == &IMPROV_HEADER[improv_header_char] { + improv_header_char += 1; + + if improv_header_char >= IMPROV_HEADER.len() { + return Ok(i - (IMPROV_HEADER.len() - 1)); + } + } + else { + improv_header_char = 0; + if b == &IMPROV_HEADER[improv_header_char] { + improv_header_char += 1; + } + } + } + + return Err(String::from("Improv header not found")); +} + +pub struct SerialInterface { + interface: Box, +} + +impl SerialInterface { + + pub fn new(path: &str, baud_rate: u32) -> Result { + let interface = tokio_serial::new(path, baud_rate).open().unwrap(); + + return Ok(Self { + interface: interface, + }); + } + + pub fn send_bytes(&mut self, packet_bytes: &[u8]) { + self.interface.write(packet_bytes).unwrap(); + } + + pub fn recv_bytes(&mut self) -> Result> { + let mut buffer: Vec = Vec::new(); + self.interface.read_to_end(&mut 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()); + } +}