From 5bfe2bde5a73c047cb59b7e1f558538c2c73da48 Mon Sep 17 00:00:00 2001 From: clerie Date: Wed, 18 Dec 2024 21:59:55 +0100 Subject: [PATCH] Add request device information and implement base of RPCResult --- src/improv.rs | 47 +++++++++++++++++++++++++++++++++++++++++++++-- src/main.rs | 24 +++++++++++++++++++++++- 2 files changed, 68 insertions(+), 3 deletions(-) diff --git a/src/improv.rs b/src/improv.rs index cb04bd1..a98f950 100644 --- a/src/improv.rs +++ b/src/improv.rs @@ -117,6 +117,7 @@ impl std::fmt::Display for ErrorState { pub enum RPCCommand { SendWifiSettings = 0x01, RequestCurrentState = 0x02, + RequestDeviceInformation = 0x03, } pub fn calculate_checksum(data: &[u8]) -> u8 { @@ -216,6 +217,8 @@ pub enum ImprovPacket { CurrentStateResponse(CurrentStateResponse), ErrorState(ErrorStatePacket), RequestCurrentStateCommand(RequestCurrentStateCommand), + RequestDeviceInformation(RequestDeviceInformationPacket), + RPCResult(RPCResultPacket), } impl ImprovPacket { @@ -224,8 +227,8 @@ impl ImprovPacket { PacketType::CurrentState => Ok(ImprovPacket::CurrentStateResponse(CurrentStateResponse::try_from_raw_packet(raw_packet)?)), PacketType::ErrorState => Ok(ImprovPacket::ErrorState(ErrorStatePacket::try_from_raw_packet(raw_packet)?)), //PacketType::RPCCommand => _, - //PacketType::RPCResult => _, - _ => Err("Conversion into packet type {} not implemented"), + PacketType::RPCResult => Ok(ImprovPacket::RPCResult(RPCResultPacket::try_from_raw_packet(raw_packet)?)), + _ => Err("Conversion into packet type not implemented"), } } } @@ -297,3 +300,43 @@ impl ImprovDataToPacket for RequestCurrentStateCommand { return out; } } + +pub struct RequestDeviceInformationPacket { +} + +impl ImprovDataPacketType for RequestDeviceInformationPacket { + const packet_type: PacketType = PacketType::RPCCommand; +} + +impl ImprovDataToPacket for RequestDeviceInformationPacket { + fn to_bytes(self: &Self) -> Vec { + let mut out = Vec::new(); + out.push(RPCCommand::RequestDeviceInformation as u8); + out.push(0x00); // rpc command payload length + return out; + } +} + +pub struct RPCResultPacket { + results: Vec, +} + +impl ImprovDataPacketType for RPCResultPacket { + const packet_type: PacketType = PacketType::RPCResult; +} + +impl ImprovDataFromPacket for RPCResultPacket { + type Error = &'static str; + + fn try_from_raw_packet(raw_packet: &RawPacket) -> Result{ + if raw_packet.r#type != Self::packet_type { + return Err("Packet is not RPCResult"); + } + + let results: Vec = Vec::new(); + + return Ok(Self { + results: results, + }) + } +} diff --git a/src/main.rs b/src/main.rs index effe89d..9702d25 100644 --- a/src/main.rs +++ b/src/main.rs @@ -19,6 +19,7 @@ use improv_setup::improv::{ RawPacket, ImprovPacket, RequestCurrentStateCommand, + RequestDeviceInformationPacket, }; use log::{ debug, @@ -39,6 +40,8 @@ enum DeviceCommands { /// Password for the SSID password: String, }, + /// Request device info + Info, } impl Default for DeviceCommands { @@ -168,7 +171,26 @@ async fn main() -> Result<()>{ }, DeviceCommands::SetWifi {ssid, password} => { println!("Not implemented"); - } + }, + 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 buffer: Vec = Vec::new(); + serial_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 + >::into(buffer[improv_packet_offset+8]); + let raw_packet = RawPacket::try_from_bytes(&buffer[improv_packet_offset..improv_packet_end].to_vec()).unwrap(); + + + if let ImprovPacket::RPCResult(rpc_result) = ImprovPacket::try_from_raw_packet(&raw_packet).unwrap() { + println!("Received RPCResult"); + } + }, }; },