Send improv packets directly

This commit is contained in:
clerie 2024-12-23 15:56:35 +01:00
parent c181709e29
commit bc5b0959b2
2 changed files with 25 additions and 7 deletions

View File

@ -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();

View File

@ -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<u8>) -> Result<usize, String> {
@ -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<Vec<u8>> {
@ -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 + <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());
}