From 23edacc877744e0721737498a6183d9e2e2cdec3 Mon Sep 17 00:00:00 2001 From: Matthew Garrett Date: Wed, 10 Jan 2024 20:28:14 -0800 Subject: [PATCH] Make more idiomatic --- serial/src/main.rs | 71 +++++++++++++++++++++------------------------- 1 file changed, 32 insertions(+), 39 deletions(-) diff --git a/serial/src/main.rs b/serial/src/main.rs index f727aa8..c6f2ee2 100644 --- a/serial/src/main.rs +++ b/serial/src/main.rs @@ -27,7 +27,7 @@ fn main() { fn send_command( handle: &mut DeviceHandle, - command: &String, + command: &str, ) { let mut data = String::new(); data.push_str("\r\n"); @@ -36,25 +36,22 @@ fn send_command( let timeout = Duration::from_secs(1); let mut response = [0; 256]; - match handle.write_control(0x21, 0x22, 3, 1, &[], timeout) { - Ok(_) => match handle.write_bulk(0x2, data.as_bytes(), timeout) { - // Consume the echoed back command - Ok(_) => match handle.read_bulk(0x82, &mut response, timeout) { - // Read the actual response - Ok(_) => match handle.read_bulk(0x82, &mut response, timeout) { - Ok(_) => { - let responsestr = str::from_utf8(&response).unwrap(); - if !responsestr.starts_with("\r\nOK\r\n") { - println!("Received unexpected response{0}", responsestr); - } - }, - Err(e) => panic!("Failed to read response: {0}", e), - }, - Err(e) => panic!("Failed to read submitted command: {0}", e), - } - Err(e) => panic!("Failed to write command: {0}", e), - }, - Err(e) => panic!("Failed to send control request: {0}", e), + + // Set up the serial port appropriately + handle.write_control(0x21, 0x22, 3, 1, &[], timeout).expect("Failed to send control request"); + + // Send the command + handle.write_bulk(0x2, data.as_bytes(), timeout).expect("Failed to write command"); + + // Consume the echoed command + handle.read_bulk(0x82, &mut response, timeout).expect("Failed to read submitted command"); + + // Read the actual response + handle.read_bulk(0x82, &mut response, timeout).expect("Failed to read response"); + + let responsestr = str::from_utf8(&response).expect("Failed to parse response"); + if !responsestr.starts_with("\r\nOK\r\n") { + println!("Received unexpected response{0}", responsestr) } } @@ -63,16 +60,14 @@ fn switch_device( ) { // Send a command to switch the device into generic mode, exposing serial let timeout = Duration::from_secs(1); - match handle.write_control(0x40, 0xa0, 0, 0, &[], timeout) { - Ok(_) => (), - Err(e) => { - // If the device reboots while the command is still executing we - // may get a pipe error here - if e == rusb::Error::Pipe { - return; - } - panic!("Failed to send device switch control request: {0}", e); - }, + + if let Err(e) = handle.write_control(0x40, 0xa0, 0, 0, &[], timeout) { + // If the device reboots while the command is still executing we + // may get a pipe error here + if e == rusb::Error::Pipe { + return + } + panic!("Failed to send device switch control request: {0}", e) } } @@ -80,14 +75,13 @@ fn open_orbic( context: &mut T, ) -> Option> { // Device after initial mode switch - match open_device(context, 0x05c6, 0xf601) { - Some(handle) => return Some(handle), - None => (), + if let Some(handle) = open_device(context, 0x05c6, 0xf601) { + return Some(handle) } + // Device with rndis enabled as well - match open_device(context, 0x05c6, 0xf622) { - Some(handle) => return Some(handle), - None => (), + if let Some(handle) = open_device(context, 0x05c6, 0xf622) { + return Some(handle) } // Device in out-of-the-box state, need to switch to diag mode @@ -97,9 +91,8 @@ fn open_orbic( } for _ in 1..10 { - match open_device(context, 0x05c6, 0xf601) { - Some(handle) => return Some(handle), - None => (), + if let Some(handle) = open_device(context, 0x05c6, 0xf601) { + return Some(handle) } sleep(Duration::from_secs(10)) }