From bc5b0959b2e1231d81b9d01cae041c9c909c6ee1 Mon Sep 17 00:00:00 2001 From: clerie Date: Mon, 23 Dec 2024 15:56:35 +0100 Subject: [PATCH] Send improv packets directly --- src/main.rs | 8 ++++---- src/serial.rs | 24 +++++++++++++++++++++--- 2 files changed, 25 insertions(+), 7 deletions(-) diff --git a/src/main.rs b/src/main.rs index 2d22d56..f101552 100644 --- a/src/main.rs +++ b/src/main.rs @@ -128,11 +128,11 @@ async fn main() -> Result<()>{ Commands::Device {path, baud_rate, device_command} => { match &device_command.clone().unwrap_or_default() { DeviceCommands::State => { - let request_current_state_packet = (RequestCurrentStateCommand {}).to_raw_packet(); + let request_current_state_packet = RequestCurrentStateCommand {}; let mut serial_interface = serial::SerialInterface::new(path, *baud_rate).expect("Couldn't init serial interface"); - serial_interface.send_bytes(&request_current_state_packet.to_bytes()); + serial_interface.send(&request_current_state_packet).expect("Failed to send improv packet"); let result_bytes = serial_interface.recv_bytes().expect("Couldn't receive any improv packet"); let raw_packet = RawPacket::try_from_bytes(&result_bytes).unwrap(); @@ -149,11 +149,11 @@ async fn main() -> Result<()>{ println!("Not implemented"); }, DeviceCommands::Info => { - let request_device_information_packet = (RequestDeviceInformationPacket {}).to_raw_packet(); + let request_device_information_packet = RequestDeviceInformationPacket {}; let mut serial_interface = serial::SerialInterface::new(path, *baud_rate).expect("Couldn't init serial interface"); - serial_interface.send_bytes(&request_device_information_packet.to_bytes()); + serial_interface.send(&request_device_information_packet).expect("Failed to send improv packet"); let result_bytes = serial_interface.recv_bytes().expect("Couldn't receive any improv packet"); let raw_packet = RawPacket::try_from_bytes(&result_bytes).unwrap(); diff --git a/src/serial.rs b/src/serial.rs index 752d4ee..3ed01a3 100644 --- a/src/serial.rs +++ b/src/serial.rs @@ -1,9 +1,13 @@ use anyhow::{ + Context, Result, }; use crate::improv::{ IMPROV_HEADER, + RawPacket, + ImprovPacket, + ImprovDataToPacket, }; pub fn find_begin_of_improv_packet(buffer: &Vec) -> Result { @@ -42,8 +46,22 @@ impl SerialInterface { }); } - pub fn send_bytes(&mut self, packet_bytes: &[u8]) { - self.interface.write(packet_bytes).unwrap(); + pub fn send_bytes(&mut self, packet_bytes: &[u8]) -> Result<()> { + self.interface.write(packet_bytes).context("Unable to write bytes to serial interface")?; + + Ok(()) + } + + pub fn send_raw_packet(&mut self, raw_packet: RawPacket) -> Result<()> { + self.send_bytes(&raw_packet.to_bytes())?; + + Ok(()) + } + + pub fn send(&mut self, packet: &impl ImprovDataToPacket) -> Result<()> { + self.send_raw_packet(packet.to_raw_packet())?; + + Ok(()) } pub fn recv_bytes(&mut self) -> Result> { @@ -52,7 +70,7 @@ impl SerialInterface { 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 improv_packet_end = improv_packet_offset + 10 + >::into(buffer[improv_packet_offset+8]); return Ok(buffer[improv_packet_offset..improv_packet_end].to_vec()); }