Move serial interface to seperate module
This commit is contained in:
parent
fe11e47c67
commit
c181709e29
@ -1 +1,2 @@
|
||||
pub mod improv;
|
||||
pub mod serial;
|
||||
|
63
src/main.rs
63
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<u8>) -> String {
|
||||
}
|
||||
|
||||
|
||||
fn find_begin_of_improv_packet(buffer: &Vec<u8>) -> Result<usize, String> {
|
||||
|
||||
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<u8> = 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 + <u8 as Into<usize>>::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<u8> = 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 + <u8 as Into<usize>>::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 {
|
||||
|
59
src/serial.rs
Normal file
59
src/serial.rs
Normal file
@ -0,0 +1,59 @@
|
||||
use anyhow::{
|
||||
Result,
|
||||
};
|
||||
|
||||
use crate::improv::{
|
||||
IMPROV_HEADER,
|
||||
};
|
||||
|
||||
pub fn find_begin_of_improv_packet(buffer: &Vec<u8>) -> Result<usize, String> {
|
||||
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<dyn tokio_serial::SerialPort>,
|
||||
}
|
||||
|
||||
impl SerialInterface {
|
||||
|
||||
pub fn new(path: &str, baud_rate: u32) -> Result<Self> {
|
||||
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<Vec<u8>> {
|
||||
let mut buffer: Vec<u8> = 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 + <u8 as Into<usize>>::into(buffer[improv_packet_offset+8])
|
||||
|
||||
return Ok(buffer[improv_packet_offset..improv_packet_end].to_vec());
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user