Make more idiomatic

This commit is contained in:
Matthew Garrett
2024-01-10 20:28:14 -08:00
parent ea84d74f9d
commit 23edacc877

View File

@@ -27,7 +27,7 @@ fn main() {
fn send_command<T: UsbContext>(
handle: &mut DeviceHandle<T>,
command: &String,
command: &str,
) {
let mut data = String::new();
data.push_str("\r\n");
@@ -36,25 +36,22 @@ fn send_command<T: UsbContext>(
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<T: UsbContext>(
) {
// 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<T: UsbContext>(
context: &mut T,
) -> Option<DeviceHandle<T>> {
// 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<T: UsbContext>(
}
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))
}