From dc0b0495e4e80627ac78f6f0f7ca761a5a6ec924 Mon Sep 17 00:00:00 2001 From: Mateusz Faderewski Date: Fri, 16 Aug 2024 13:26:10 +0200 Subject: [PATCH] little cleanup --- sw/deployer/src/main.rs | 8 +++---- sw/deployer/src/sc64/mod.rs | 19 +++++++-------- sw/deployer/src/sc64/types.rs | 44 +++++++++++++++++++++++------------ 3 files changed, 42 insertions(+), 29 deletions(-) diff --git a/sw/deployer/src/main.rs b/sw/deployer/src/main.rs index 4372c4c..3915324 100644 --- a/sw/deployer/src/main.rs +++ b/sw/deployer/src/main.rs @@ -373,7 +373,7 @@ fn handle_list_command() -> Result<(), sc64::Error> { fn handle_upload_command(connection: Connection, args: &UploadArgs) -> Result<(), sc64::Error> { let mut sc64 = init_sc64(connection, true)?; - if args.reboot && !sc64.aux_try_notify(sc64::AuxMessage::Halt)? { + if args.reboot && !sc64.try_notify_via_aux(sc64::AuxMessage::Halt)? { println!( "{}", "Warning: no response for [Halt] AUX message".bright_yellow() @@ -425,7 +425,7 @@ fn handle_upload_command(connection: Connection, args: &UploadArgs) -> Result<() sc64.calculate_cic_parameters(args.cic_seed)?; - if args.reboot && !sc64.aux_try_notify(sc64::AuxMessage::Reboot)? { + if args.reboot && !sc64.try_notify_via_aux(sc64::AuxMessage::Reboot)? { println!( "{}", "Warning: no response for [Reboot] AUX message".bright_yellow() @@ -470,7 +470,7 @@ fn handle_64dd_command(connection: Connection, args: &_64DDArgs) -> Result<(), s .bright_green() ); - if args.reboot && !sc64.aux_try_notify(sc64::AuxMessage::Halt)? { + if args.reboot && !sc64.try_notify_via_aux(sc64::AuxMessage::Halt)? { println!( "{}", "Warning: no response for [Halt] AUX message".bright_yellow() @@ -587,7 +587,7 @@ fn handle_64dd_command(connection: Connection, args: &_64DDArgs) -> Result<(), s sc64.set_save_writeback(true)?; - if args.reboot && !sc64.aux_try_notify(sc64::AuxMessage::Reboot)? { + if args.reboot && !sc64.try_notify_via_aux(sc64::AuxMessage::Reboot)? { println!( "{}", "Warning: no response for [Reboot] AUX message".bright_yellow() diff --git a/sw/deployer/src/sc64/mod.rs b/sw/deployer/src/sc64/mod.rs index 4410e8b..6729616 100644 --- a/sw/deployer/src/sc64/mod.rs +++ b/sw/deployer/src/sc64/mod.rs @@ -571,16 +571,16 @@ impl SC64 { self.command_usb_write(debug_packet.datatype, &debug_packet.data) } - pub fn aux_send(&mut self, data: u32) -> Result<(), Error> { - self.command_aux_write(data) + pub fn send_aux_packet(&mut self, data: AuxMessage) -> Result<(), Error> { + self.command_aux_write(data.into()) } - pub fn aux_send_and_receive( + pub fn send_and_receive_aux_packet( &mut self, - data: u32, + data: AuxMessage, timeout: std::time::Duration, - ) -> Result, Error> { - self.aux_send(data)?; + ) -> Result, Error> { + self.send_aux_packet(data)?; let reply_timeout = std::time::Instant::now(); loop { match self.receive_data_packet()? { @@ -598,11 +598,10 @@ impl SC64 { } } - pub fn aux_try_notify(&mut self, message: AuxMessage) -> Result { - let value: u32 = message.into(); + pub fn try_notify_via_aux(&mut self, message: AuxMessage) -> Result { let timeout = std::time::Duration::from_millis(500); - if let Some(response) = self.aux_send_and_receive(value, timeout)? { - return Ok(value == response); + if let Some(response) = self.send_and_receive_aux_packet(message, timeout)? { + return Ok(message == response); } Ok(false) } diff --git a/sw/deployer/src/sc64/types.rs b/sw/deployer/src/sc64/types.rs index 427ff51..585acee 100644 --- a/sw/deployer/src/sc64/types.rs +++ b/sw/deployer/src/sc64/types.rs @@ -614,7 +614,7 @@ impl From for [u32; 2] { } pub enum DataPacket { - AuxData(u32), + AuxData(AuxMessage), Button, DataFlushed, DebugData(DebugPacket), @@ -628,32 +628,25 @@ impl TryFrom for DataPacket { type Error = Error; fn try_from(value: AsynchronousPacket) -> Result { Ok(match value.id { - b'X' => Self::AuxData(u32::from_be_bytes(value.data[0..4].try_into().unwrap())), + b'X' => Self::AuxData(value.data.try_into()?), b'B' => Self::Button, b'G' => Self::DataFlushed, b'U' => Self::DebugData(value.data.try_into()?), b'D' => Self::DiskRequest(value.data.try_into()?), b'I' => Self::IsViewer64(value.data), b'S' => Self::SaveWriteback(value.data.try_into()?), - b'F' => { - if value.data.len() != 4 { - return Err(Error::new( - "Incorrect data length for update status data packet", - )); - } - Self::UpdateStatus( - u32::from_be_bytes(value.data[0..4].try_into().unwrap()).try_into()?, - ) - } + b'F' => Self::UpdateStatus(value.data.try_into()?), _ => return Err(Error::new("Unknown data packet code")), }) } } +#[derive(Clone, Copy, PartialEq, Eq)] pub enum AuxMessage { Ping, Halt, Reboot, + Other(u32), } impl From for u32 { @@ -662,10 +655,26 @@ impl From for u32 { AuxMessage::Ping => 0xFF000000, AuxMessage::Halt => 0xFF000001, AuxMessage::Reboot => 0xFF000002, + AuxMessage::Other(message) => message, } } } +impl TryFrom> for AuxMessage { + type Error = Error; + fn try_from(value: Vec) -> Result { + if value.len() != 4 { + return Err(Error::new("Invalid data length for AUX data packet")); + } + Ok(match u32::from_be_bytes(value[0..4].try_into().unwrap()) { + 0xFF000000 => AuxMessage::Ping, + 0xFF000001 => AuxMessage::Halt, + 0xFF000002 => AuxMessage::Reboot, + message => AuxMessage::Other(message), + }) + } +} + pub struct DebugPacket { pub datatype: u8, pub data: Vec, @@ -821,10 +830,15 @@ impl Display for UpdateStatus { } } -impl TryFrom for UpdateStatus { +impl TryFrom> for UpdateStatus { type Error = Error; - fn try_from(value: u32) -> Result { - Ok(match value { + fn try_from(value: Vec) -> Result { + if value.len() != 4 { + return Err(Error::new( + "Incorrect data length for update status data packet", + )); + } + Ok(match u32::from_be_bytes(value[0..4].try_into().unwrap()) { 1 => Self::MCU, 2 => Self::FPGA, 3 => Self::Bootloader,