From ee46ff63417a0b99db4abf0eab328e92b50caa2c Mon Sep 17 00:00:00 2001 From: Liam Hays Date: Mon, 15 Aug 2022 11:03:56 -0600 Subject: [PATCH] Removed a couple TODOs that were actually already complete. --- src/kermit.rs | 66 ++++++++++++++++++++++++++++----------------------- src/main.rs | 9 ++++--- src/xmodem.rs | 1 - 3 files changed, 40 insertions(+), 36 deletions(-) diff --git a/src/kermit.rs b/src/kermit.rs index d4d2158..8ffa9b2 100644 --- a/src/kermit.rs +++ b/src/kermit.rs @@ -158,14 +158,14 @@ fn make_generic_packet(seq: &mut u32, ptype: char) -> Vec { // do with how we read the packet (3 bytes then rest of packet). fn read_packet(port: &mut Box) -> Result { // have to sleep, probably because the calculator is slow - std::thread::sleep(std::time::Duration::from_millis(300)); + std::thread::sleep(std::time::Duration::from_millis(400)); // it seems we have to read 3 bytes, then the rest of the packet let mut header: [u8; 3] = [0; 3]; match port.read(header.as_mut_slice()) { Ok(_) => {}, Err(e) => return Err("failed to read header of packet: ".to_owned() + &e.to_string()), } - + //println!("header is {:x?}", header); if header[0] != SOH { return Err("malformed Kermit packet (SOH missing)".to_owned()); } @@ -174,12 +174,12 @@ fn read_packet(port: &mut Box) -> Result {}, Err(e) => return Err("failed to read packet data: ".to_owned() + &e.to_string()), } - + //println!("rest of packet is {:x?}", rest_of_packet); // subtract 2 to drop 0x0d and check field, to isolate just data // portion and assemble KermitPacket struct. let data_field = rest_of_packet[1..(len as usize - 2)].to_vec(); @@ -210,7 +210,7 @@ fn send_packet(p: KermitPacket, bar: &ProgressBar, port: &mut Box {}, Err(e) => { bar.abandon(); - crate::helpers::error_handler(format!("Error: failed to write data packet: {:?}", e)); + crate::helpers::error_handler(format!("Error: failed to write data packet: {}", e)); }, } match read_packet(port) { @@ -223,7 +223,7 @@ fn send_packet(p: KermitPacket, bar: &ProgressBar, port: &mut Box { bar.abandon(); - crate::helpers::error_handler(format!("Error: bad \"D\" packet response: {:?}.", e)); + crate::helpers::error_handler(format!("Error: bad \"D\" packet response: {}.", e)); }, } } @@ -286,6 +286,29 @@ fn make_packet_list(f: Vec, seq: &mut u32) -> Vec { return packet_list; } +fn finish_server(port: &mut Box) { + // "I" packet is identical to "S" except for the packet type. + + // seq can and probably should be 0, and Rust lets you do `&mut 0` + // legally. Funky, for sure. + let i_packet = make_init_packet(&mut 0, 'I'); + match port.write(&i_packet) { + Ok(_) => {}, + Err(e) => crate::helpers::error_handler(format!("Error: failed to write \"I\" packet: {}", e)), + } + // could wait for ack but probably don't need to. + std::thread::sleep(std::time::Duration::from_millis(300)); + + // we are sending a 'G' packet with 'F' in the data field, + // which tells the server to finish. + // we use 0 as the seq number even though the I packet was also 0. + let f_packet = vec![SOH, 0x24, tochar(0), 'G' as u8, 'F' as u8, 0x34, CR]; // hardcoded CRC + match port.write(&f_packet) { + Ok(_) => {}, + Err(e) => crate::helpers::error_handler(format!("Error: failed to write \"GF\" packet: {}", e)), + } +} + // TODO: this is pretty unreliable and doesn't work with x48 at full // speed. It has to do with the read_packet() function. @@ -300,7 +323,7 @@ pub fn send_file(path: &PathBuf, port: &mut Box, fin let s_packet = make_init_packet(&mut seq, 'S'); match port.write(&s_packet) { Ok(_) => {}, - Err(e) => crate::helpers::error_handler(format!("Error: failed to write \"S\" packet: {:?}", e)), + Err(e) => crate::helpers::error_handler(format!("Error: failed to write \"S\" packet: {}", e)), } match read_packet(port) { Ok(packet) => { @@ -308,13 +331,13 @@ pub fn send_file(path: &PathBuf, port: &mut Box, fin crate::helpers::error_handler("Error: no ACK for \"S\" packet. Try sending again.".to_string()); } }, - Err(e) => crate::helpers::error_handler(format!("Error: bad \"S\" packet response: {:?}.", e)), + Err(e) => crate::helpers::error_handler(format!("Error: bad \"S\" packet response: {}.", e)), } let f_packet = make_f_packet(&mut seq, path.file_name().unwrap()); match port.write(&f_packet) { Ok(_) => {}, - Err(e) => crate::helpers::error_handler(format!("Error: failed to write \"F\" packet: {:?}", e)), + Err(e) => crate::helpers::error_handler(format!("Error: failed to write \"F\" packet: {}", e)), } match read_packet(port) { Ok(packet) => { @@ -322,7 +345,7 @@ pub fn send_file(path: &PathBuf, port: &mut Box, fin crate::helpers::error_handler("Error: no ACK for \"F\" packet. Try sending again.".to_string()); } }, - Err(e) => crate::helpers::error_handler(format!("Error: bad \"F\" packet response: {:?}", e)), + Err(e) => crate::helpers::error_handler(format!("Error: bad \"F\" packet response: {}", e)), } let packet_list = make_packet_list(file_contents, &mut seq); @@ -340,7 +363,7 @@ pub fn send_file(path: &PathBuf, port: &mut Box, fin // abondon() leaves the progress bar in place, finish() clears it. bar.abandon(); crate::helpers::error_handler( - format!("Error: failed to write \"Z\" (end-of-file) packet: {:?}", e)); + format!("Error: failed to write \"Z\" (end-of-file) packet: {}", e)); }, } @@ -353,29 +376,12 @@ pub fn send_file(path: &PathBuf, port: &mut Box, fin Err(e) => { bar.abandon(); crate::helpers::error_handler( - format!("Error: failed to write \"B\" (end-of-transmission) packet: {:?}", e)); + format!("Error: failed to write \"B\" (end-of-transmission) packet: {}", e)); }, } bar.finish(); if finish { - // "I" packet is identical to "S" except for the packet type. - let i_packet = make_init_packet(&mut seq, 'I'); - match port.write(&i_packet) { - Ok(_) => {}, - Err(e) => crate::helpers::error_handler(format!("Error: failed to write \"I\" packet: {:?}", e)), - } - // could wait for ack but probably don't need to. - std::thread::sleep(std::time::Duration::from_millis(300)); - // note: sending the I packet resets the seq number. - - // we are sending a 'G' packet with 'F' in the data field, - // which tells the server to finish. - let f_packet = vec![SOH, 0x24, tochar(0), 'G' as u8, 'F' as u8, 0x34, CR]; // hardcoded CRC - match port.write(&f_packet) { - Ok(_) => {}, - Err(e) => crate::helpers::error_handler(format!("Error: failed to write \"GF\" packet: {:?}", e)), - } - + finish_server(port); } } diff --git a/src/main.rs b/src/main.rs index 8889bd6..58d5e93 100644 --- a/src/main.rs +++ b/src/main.rs @@ -93,15 +93,14 @@ enum Commands { } -// TODO: this needs to find USB serial ports, because other ports -// might be present. On Windows, I think they're generated by Intel ME -// or other hardware. fn get_serial_port(cli_port: Option, cli_baud: Option) -> Box { let discovered_ports = serialport::available_ports().expect("No ports found!"); let mut usb_serial_ports: Vec = Vec::new(); - // Sort through the ports and find only USB serial ports. + // Sort through the ports and find only USB serial + // ports. Sometimes other ports are present, and it's quite + // unlikely that they would be for the calculator for p in &discovered_ports { match p.port_type { serialport::SerialPortType::UsbPort(..) => { @@ -172,7 +171,7 @@ fn main() { // send file directly to XRECV if cli.finish { println!("{}: {}{}{}", - style("warning").on_yellow().bold(), + style("warning").yellow().bright(), "ignoring flag ", style("-f").green(), " (finish) used in XModem direct mode"); } diff --git a/src/xmodem.rs b/src/xmodem.rs index 3d81027..e00a02d 100644 --- a/src/xmodem.rs +++ b/src/xmodem.rs @@ -409,7 +409,6 @@ pub fn get_file(path: &PathBuf, port: &mut Box, dire } let pb = crate::helpers::get_spinner( - // TODO: this should say the actual file being written to format!("Receiving {} as {} on {}...", style(original_fname).yellow().bright(), style(final_fname).yellow().bright(),