Add request device information and implement base of RPCResult

This commit is contained in:
clerie 2024-12-18 21:59:55 +01:00
parent 945c8386ad
commit 5bfe2bde5a
2 changed files with 68 additions and 3 deletions

View File

@ -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<u8> {
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<String>,
}
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<Self, Self::Error>{
if raw_packet.r#type != Self::packet_type {
return Err("Packet is not RPCResult");
}
let results: Vec<String> = Vec::new();
return Ok(Self {
results: results,
})
}
}

View File

@ -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<u8> = 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 + <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();
if let ImprovPacket::RPCResult(rpc_result) = ImprovPacket::try_from_raw_packet(&raw_packet).unwrap() {
println!("Received RPCResult");
}
},
};
},