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 improv_setup::improv::{
CurrentState,
RawPacket,
ImprovPacket,
SendWiFiSettingsPacket,
RequestCurrentStatePacket,
@ -95,15 +94,16 @@ async fn main() -> Result<()>{
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")?;
let raw_packet = RawPacket::try_from_bytes(&result_bytes).context("Failed to deserialize packet")?;
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);
}
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);
match serial_interface.recv().context("Couldn't receive any improv packet")? {
ImprovPacket::CurrentState(current_state_response) => {
println!("Current state: {}", &current_state_response.current_state);
},
ImprovPacket::ErrorState(error_state) => {
println!("Error state: {}", &error_state.error_state);
},
e => {
println!("Unexpected response: {}", e);
}
}
},
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")?;
loop {
let result_bytes = serial_interface.recv_bytes().context("Couldn't receive any improv packet")?;
let improv_packet = ImprovPacket::try_from_bytes(&result_bytes).context("Failed to read packet")?;
match improv_packet {
match serial_interface.recv().context("Couldn't receive any improv packet")? {
ImprovPacket::RPCResult(rpc_result) => {
for r in rpc_result.results {
println!("{}", &r);
@ -138,9 +135,11 @@ async fn main() -> Result<()>{
},
ImprovPacket::ErrorState(error_state) => {
println!("Error: {}", &error_state.error_state);
break;
},
e => {
println!("Unexpected response: {}", e);
break;
}
}
}
@ -154,12 +153,17 @@ async fn main() -> Result<()>{
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")?;
let raw_packet = RawPacket::try_from_bytes(&result_bytes).context("Failed to deserialize packet")?;
if let ImprovPacket::RPCResult(rpc_result) = ImprovPacket::try_from_raw_packet(&raw_packet).context("Failed to read packet")? {
for r in rpc_result.results {
println!("{}", &r);
match serial_interface.recv().context("Couldn't receive any improv packet")? {
ImprovPacket::RPCResult(rpc_result) => {
for r in rpc_result.results {
println!("{}", &r);
}
},
ImprovPacket::ErrorState(error_state) => {
println!("Error: {}", &error_state.error_state);
},
e => {
println!("Unexpected response: {}", e);
}
}
},
@ -173,18 +177,25 @@ async fn main() -> Result<()>{
serial_interface.send(&request_scanned_wifi_networks_packet).context("Failed to send improv packet")?;
loop {
let result_bytes = serial_interface.recv_bytes().context("Couldn't receive any improv packet")?;
let raw_packet = RawPacket::try_from_bytes(&result_bytes).context("Failed to deserialize packet")?;
if let ImprovPacket::RPCResult(rpc_result) = ImprovPacket::try_from_raw_packet(&raw_packet).context("Failed to read packet")? {
if rpc_result.results.len() <= 0 {
match serial_interface.recv().context("Couldn't receive any improv packet")? {
ImprovPacket::RPCResult(rpc_result) => {
if rpc_result.results.len() <= 0 {
break;
}
println!("{:<25} {:>3} {}", &rpc_result.results[0], &rpc_result.results[1], match rpc_result.results[2].as_str() {
"YES" => "secure",
"NO" => "open",
_ => "",
});
},
ImprovPacket::ErrorState(error_state) => {
println!("Error: {}", &error_state.error_state);
break;
},
e => {
println!("Unexpected response: {}", e);
break;
}
println!("{:<25} {:>3} {}", &rpc_result.results[0], &rpc_result.results[1], match rpc_result.results[2].as_str() {
"YES" => "secure",
"NO" => "open",
_ => "",
});
}
}
},

View File

@ -12,6 +12,7 @@ use log::{
use crate::improv::{
IMPROV_HEADER,
RawPacket,
ImprovPacket,
ImprovDataToPacket,
};
use crate::utils::{
@ -166,4 +167,10 @@ impl SerialInterface {
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);
}
}