Unifying improv packet names

This commit is contained in:
clerie 2024-12-25 16:06:35 +01:00
parent 8bb40c7462
commit e189d1b37f
2 changed files with 14 additions and 14 deletions

View File

@ -236,9 +236,9 @@ impl RawPacket {
}
pub enum ImprovPacket {
CurrentStateResponse(CurrentStateResponse),
CurrentState(CurrentStatePacket),
ErrorState(ErrorStatePacket),
RequestCurrentStateCommand(RequestCurrentStateCommand),
RequestCurrentState(RequestCurrentStatePacket),
RequestDeviceInformation(RequestDeviceInformationPacket),
RPCResult(RPCResultPacket),
}
@ -246,7 +246,7 @@ pub enum ImprovPacket {
impl ImprovPacket {
pub fn try_from_raw_packet(raw_packet: &RawPacket) -> Result<Self> {
match raw_packet.r#type {
PacketType::CurrentState => Ok(ImprovPacket::CurrentStateResponse(CurrentStateResponse::try_from_raw_packet(raw_packet)?)),
PacketType::CurrentState => Ok(ImprovPacket::CurrentState(CurrentStatePacket::try_from_raw_packet(raw_packet)?)),
PacketType::ErrorState => Ok(ImprovPacket::ErrorState(ErrorStatePacket::try_from_raw_packet(raw_packet)?)),
//PacketType::RPCCommand => _,
PacketType::RPCResult => Ok(ImprovPacket::RPCResult(RPCResultPacket::try_from_raw_packet(raw_packet)?)),
@ -255,15 +255,15 @@ impl ImprovPacket {
}
}
pub struct CurrentStateResponse {
pub struct CurrentStatePacket {
pub current_state: CurrentState,
}
impl ImprovDataPacketType for CurrentStateResponse {
impl ImprovDataPacketType for CurrentStatePacket {
const packet_type: PacketType = PacketType::CurrentState;
}
impl ImprovDataToPacket for CurrentStateResponse {
impl ImprovDataToPacket for CurrentStatePacket {
fn to_bytes(self: &Self) -> Vec<u8> {
let mut out = Vec::new();
out.push(self.current_state.clone() as u8);
@ -271,12 +271,12 @@ impl ImprovDataToPacket for CurrentStateResponse {
}
}
impl ImprovDataFromPacket for CurrentStateResponse {
impl ImprovDataFromPacket for CurrentStatePacket {
type Error = Error;
fn try_from_raw_packet(raw_packet: &RawPacket) -> Result<Self, Self::Error>{
if raw_packet.r#type != Self::packet_type {
return Err(anyhow!("Packet is not CurrentStateResponse"));
return Err(anyhow!("Packet is not CurrentStatePacket"));
}
return Ok(Self {
@ -307,14 +307,14 @@ impl ImprovDataFromPacket for ErrorStatePacket {
}
}
pub struct RequestCurrentStateCommand {
pub struct RequestCurrentStatePacket {
}
impl ImprovDataPacketType for RequestCurrentStateCommand {
impl ImprovDataPacketType for RequestCurrentStatePacket {
const packet_type: PacketType = PacketType::RPCCommand;
}
impl ImprovDataToPacket for RequestCurrentStateCommand {
impl ImprovDataToPacket for RequestCurrentStatePacket {
fn to_bytes(self: &Self) -> Vec<u8> {
let mut out = Vec::new();
out.push(RPCCommand::RequestCurrentState as u8);

View File

@ -11,7 +11,7 @@ use hex;
use improv_setup::improv::{
RawPacket,
ImprovPacket,
RequestCurrentStateCommand,
RequestCurrentStatePacket,
RequestDeviceInformationPacket,
};
use improv_setup::serial;
@ -120,7 +120,7 @@ async fn main() -> Result<()>{
Commands::Device {path, baud_rate, device_command} => {
match &device_command.clone().unwrap_or_default() {
DeviceCommands::State => {
let request_current_state_packet = RequestCurrentStateCommand {};
let request_current_state_packet = RequestCurrentStatePacket {};
let mut serial_interface = serial::SerialInterface::new(path, *baud_rate).context("Couldn't init serial interface")?;
@ -129,7 +129,7 @@ async fn main() -> Result<()>{
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::CurrentStateResponse(current_state_response) = ImprovPacket::try_from_raw_packet(&raw_packet).context("Failed to read 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);
}