Directly receive ImprovPacket from serial device

This commit is contained in:
clerie 2025-01-01 16:21:06 +01:00
parent 833901aecb
commit 197ba19520
2 changed files with 48 additions and 30 deletions

View File

@ -9,7 +9,6 @@ use clap::{
use env_logger; use env_logger;
use improv_setup::improv::{ use improv_setup::improv::{
CurrentState, CurrentState,
RawPacket,
ImprovPacket, ImprovPacket,
SendWiFiSettingsPacket, SendWiFiSettingsPacket,
RequestCurrentStatePacket, RequestCurrentStatePacket,
@ -95,15 +94,16 @@ async fn main() -> Result<()>{
serial_interface.send(&request_current_state_packet).context("Failed to send improv packet")?; serial_interface.send(&request_current_state_packet).context("Failed to send improv packet")?;
let result_bytes = serial_interface.recv_bytes().context("Couldn't receive any improv packet")?; match serial_interface.recv().context("Couldn't receive any improv packet")? {
let raw_packet = RawPacket::try_from_bytes(&result_bytes).context("Failed to deserialize packet")?; ImprovPacket::CurrentState(current_state_response) => {
if let ImprovPacket::CurrentState(current_state_response) = ImprovPacket::try_from_raw_packet(&raw_packet).context("Failed to read packet")? {
println!("Current state: {}", &current_state_response.current_state); println!("Current state: {}", &current_state_response.current_state);
} },
ImprovPacket::ErrorState(error_state) => {
if let ImprovPacket::ErrorState(error_state) = ImprovPacket::try_from_raw_packet(&raw_packet).context("Failed to read packet")? {
println!("Error state: {}", &error_state.error_state); println!("Error state: {}", &error_state.error_state);
},
e => {
println!("Unexpected response: {}", e);
}
} }
}, },
DeviceCommands::Connect{ssid, password} => { DeviceCommands::Connect{ssid, password} => {
@ -119,10 +119,7 @@ async fn main() -> Result<()>{
serial_interface.send(&send_wifi_settings_packet).context("Failed to send improv packet")?; serial_interface.send(&send_wifi_settings_packet).context("Failed to send improv packet")?;
loop { loop {
let result_bytes = serial_interface.recv_bytes().context("Couldn't receive any improv packet")?; match serial_interface.recv().context("Couldn't receive any improv packet")? {
let improv_packet = ImprovPacket::try_from_bytes(&result_bytes).context("Failed to read packet")?;
match improv_packet {
ImprovPacket::RPCResult(rpc_result) => { ImprovPacket::RPCResult(rpc_result) => {
for r in rpc_result.results { for r in rpc_result.results {
println!("{}", &r); println!("{}", &r);
@ -138,9 +135,11 @@ async fn main() -> Result<()>{
}, },
ImprovPacket::ErrorState(error_state) => { ImprovPacket::ErrorState(error_state) => {
println!("Error: {}", &error_state.error_state); println!("Error: {}", &error_state.error_state);
break;
}, },
e => { e => {
println!("Unexpected response: {}", e); println!("Unexpected response: {}", e);
break;
} }
} }
} }
@ -154,13 +153,18 @@ async fn main() -> Result<()>{
serial_interface.send(&request_device_information_packet).context("Failed to send improv packet")?; serial_interface.send(&request_device_information_packet).context("Failed to send improv packet")?;
let result_bytes = serial_interface.recv_bytes().context("Couldn't receive any improv packet")?; match serial_interface.recv().context("Couldn't receive any improv packet")? {
let raw_packet = RawPacket::try_from_bytes(&result_bytes).context("Failed to deserialize packet")?; ImprovPacket::RPCResult(rpc_result) => {
if let ImprovPacket::RPCResult(rpc_result) = ImprovPacket::try_from_raw_packet(&raw_packet).context("Failed to read packet")? {
for r in rpc_result.results { for r in rpc_result.results {
println!("{}", &r); println!("{}", &r);
} }
},
ImprovPacket::ErrorState(error_state) => {
println!("Error: {}", &error_state.error_state);
},
e => {
println!("Unexpected response: {}", e);
}
} }
}, },
DeviceCommands::Scan => { DeviceCommands::Scan => {
@ -173,10 +177,8 @@ async fn main() -> Result<()>{
serial_interface.send(&request_scanned_wifi_networks_packet).context("Failed to send improv packet")?; serial_interface.send(&request_scanned_wifi_networks_packet).context("Failed to send improv packet")?;
loop { loop {
let result_bytes = serial_interface.recv_bytes().context("Couldn't receive any improv packet")?; match serial_interface.recv().context("Couldn't receive any improv packet")? {
let raw_packet = RawPacket::try_from_bytes(&result_bytes).context("Failed to deserialize packet")?; ImprovPacket::RPCResult(rpc_result) => {
if let ImprovPacket::RPCResult(rpc_result) = ImprovPacket::try_from_raw_packet(&raw_packet).context("Failed to read packet")? {
if rpc_result.results.len() <= 0 { if rpc_result.results.len() <= 0 {
break; break;
} }
@ -185,6 +187,15 @@ async fn main() -> Result<()>{
"NO" => "open", "NO" => "open",
_ => "", _ => "",
}); });
},
ImprovPacket::ErrorState(error_state) => {
println!("Error: {}", &error_state.error_state);
break;
},
e => {
println!("Unexpected response: {}", e);
break;
}
} }
} }
}, },

View File

@ -12,6 +12,7 @@ use log::{
use crate::improv::{ use crate::improv::{
IMPROV_HEADER, IMPROV_HEADER,
RawPacket, RawPacket,
ImprovPacket,
ImprovDataToPacket, ImprovDataToPacket,
}; };
use crate::utils::{ use crate::utils::{
@ -166,4 +167,10 @@ impl SerialInterface {
return Ok(packet_bytes); return Ok(packet_bytes);
} }
pub fn recv(&mut self) -> Result<ImprovPacket> {
let result_bytes = self.recv_bytes().context("Failed to receive improv packet bytes")?;
let improv_packet = ImprovPacket::try_from_bytes(&result_bytes).context("Failed to read improv packet bytes into ImprovPacket struct")?;
return Ok(improv_packet);
}
} }