diff --git a/src/improv.rs b/src/improv.rs index 8bb9fcd..e9ca91d 100644 --- a/src/improv.rs +++ b/src/improv.rs @@ -263,6 +263,19 @@ impl ImprovPacket { } } +impl std::fmt::Display for ImprovPacket { + fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { + match self { + Self::CurrentState(_) => write!(f, "CurrentStatePacket"), + Self::ErrorState(_) => write!(f, "ErrorStatePacket"), + Self::RequestCurrentState(_) => write!(f, "RequestCurrentStatePacket"), + Self::RequestDeviceInformation(_) => write!(f, "RequestDeviceInformationPacket"), + Self::RequestScannedWiFiNetworks(_) => write!(f, "RequestScannedWiFiNetworksPacket"), + Self::RPCResult(_) => write!(f, "RPCResultPacket"), + } + } +} + pub struct CurrentStatePacket { pub current_state: CurrentState, } diff --git a/src/main.rs b/src/main.rs index 3f92bd5..f66684a 100644 --- a/src/main.rs +++ b/src/main.rs @@ -8,6 +8,7 @@ use clap::{ }; use env_logger; use improv_setup::improv::{ + CurrentState, RawPacket, ImprovPacket, SendWiFiSettingsPacket, @@ -117,12 +118,30 @@ async fn main() -> Result<()>{ serial_interface.send(&send_wifi_settings_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")?; + 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")?; - 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 improv_packet { + ImprovPacket::RPCResult(rpc_result) => { + for r in rpc_result.results { + println!("{}", &r); + } + break; + }, + ImprovPacket::CurrentState(current_state) => { + match current_state.current_state { + CurrentState::Ready => println!("Device not configured"), + CurrentState::Provisioning => println!("Trying to connect to network…"), + CurrentState::Provisioned => println!("Connected"), + } + }, + ImprovPacket::ErrorState(error_state) => { + println!("Error: {}", &error_state.error_state); + }, + e => { + println!("Unexpected response: {}", e); + } } } },