little cleanup

This commit is contained in:
Mateusz Faderewski 2024-08-16 13:26:10 +02:00
parent d630315058
commit dc0b0495e4
3 changed files with 42 additions and 29 deletions

View File

@ -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()

View File

@ -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<Option<u32>, Error> {
self.aux_send(data)?;
) -> Result<Option<AuxMessage>, 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<bool, Error> {
let value: u32 = message.into();
pub fn try_notify_via_aux(&mut self, message: AuxMessage) -> Result<bool, Error> {
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)
}

View File

@ -614,7 +614,7 @@ impl From<Setting> for [u32; 2] {
}
pub enum DataPacket {
AuxData(u32),
AuxData(AuxMessage),
Button,
DataFlushed,
DebugData(DebugPacket),
@ -628,32 +628,25 @@ impl TryFrom<AsynchronousPacket> for DataPacket {
type Error = Error;
fn try_from(value: AsynchronousPacket) -> Result<Self, Self::Error> {
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<AuxMessage> for u32 {
@ -662,10 +655,26 @@ impl From<AuxMessage> for u32 {
AuxMessage::Ping => 0xFF000000,
AuxMessage::Halt => 0xFF000001,
AuxMessage::Reboot => 0xFF000002,
AuxMessage::Other(message) => message,
}
}
}
impl TryFrom<Vec<u8>> for AuxMessage {
type Error = Error;
fn try_from(value: Vec<u8>) -> Result<Self, Self::Error> {
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<u8>,
@ -821,10 +830,15 @@ impl Display for UpdateStatus {
}
}
impl TryFrom<u32> for UpdateStatus {
impl TryFrom<Vec<u8>> for UpdateStatus {
type Error = Error;
fn try_from(value: u32) -> Result<Self, Self::Error> {
Ok(match value {
fn try_from(value: Vec<u8>) -> Result<Self, Self::Error> {
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,