From e47d093adfe228e4863b21910d76380fa6138427 Mon Sep 17 00:00:00 2001 From: John Goerzen Date: Sun, 20 Sep 2020 20:49:28 -0500 Subject: [PATCH] fixing --- src/ser.rs | 7 ++++--- src/xb.rs | 29 +++++++++++++++-------------- src/xbpacket.rs | 4 ++-- 3 files changed, 21 insertions(+), 19 deletions(-) diff --git a/src/ser.rs b/src/ser.rs index e52e225..d5a0883 100644 --- a/src/ser.rs +++ b/src/ser.rs @@ -68,12 +68,13 @@ impl XBSer { } /// Transmits a command with terminating EOL characters - pub fn writeln(&mut self, mut data: String) -> io::Result<()> { + pub fn writeln(&mut self, mut data: &str) -> io::Result<()> { trace!("{:?} SEROUT: {}", self.portname, data); - data.push_str("\r\n"); + let mut data = ByteMut::from(data.as_bytes()); + data.push(b"\r\n"); // Give the receiver a chance to process // FIXME: lock this only once - self.swrite.lock().unwrap().write_all(data.as_bytes())?; + self.swrite.lock().unwrap().write_all(data)?; self.swrite.lock().unwrap().flush() } } diff --git a/src/xb.rs b/src/xb.rs index ec4fb27..de18d38 100644 --- a/src/xb.rs +++ b/src/xb.rs @@ -28,6 +28,7 @@ use std::time::{Duration, Instant}; use format_escape_default::format_escape_default; use std::path::PathBuf; use bytes::Bytes; +use std::convert::TryInto; pub fn mkerror(msg: &str) -> Error { Error::new(ErrorKind::Other, msg) @@ -77,51 +78,51 @@ impl XB { let (writertx, writerrx) = crossbeam_channel::bounded(5); debug!("Configuring radio"); - thread::sleep(Duration::from_msecs(1100)); + thread::sleep(Duration::from_secs(2)); ser.swrite.lock().unwrap().write_all(b"+++").unwrap(); ser.swrite.lock().unwrap().flush(); - assert_response(ser.readln().unwrap(), "OK"); + assert_response(ser.readln().unwrap().unwrap(), String::from("OK")); if let Some(file) = initfile { let f = fs::File::open(file).unwrap(); let reader = BufReader::new(f); for line in reader.lines() { - if line.len() > 0 { - self.ser.writeln(line).unwrap(); - assert_response(ser.readln().unwrap(), "OK") + if line.unwrap().len() > 0 { + self.ser.writeln(line.unwrap()).unwrap(); + assert_response(ser.readln().unwrap().unwrap(), String::from("OK")); } } } // Enter API mode ser.writeln("ATAP 1").unwrap(); - assert_response(ser.readln().unwrap, "OK"); + assert_response(ser.readln().unwrap().unwrap(), String::from("OK")); // Standard API output mode ser.writeln("ATAO 0").unwrap(); - assert_response(ser.readln().unwrap(), "OK"); + assert_response(ser.readln().unwrap().unwrap(), String::from("OK")); // Get our own MAC address ser.writeln("ATSH").unwrap(); - let serialhigh = ser.readln().unwrap(); - let serialhighu64 = u64::from(u32::from_be_bytes(hex::decode(serialhigh).unwrap())); + let serialhigh = ser.readln().unwrap().unwrap(); + let serialhighu64 = u64::from(u32::from_be_bytes(hex::decode(serialhigh).unwrap().as_slice().try_into().unwrap())); ser.writeln("ATSL").unwrap(); - let seriallow = ser.readln().unwrap(); - let seriallowu64 = u64::from(u32::from_be_bytes(hex::decode(seriallow).unwrap())); + let seriallow = ser.readln().unwrap().unwrap(); + let seriallowu64 = u64::from(u32::from_be_bytes(hex::decode(seriallow).unwrap().as_slice().try_into().unwrap())); let mymac = serialhighu64 << 32 | seriallowu64; // Get maximum packet size ser.writeln("ATNP").unwrap(); - let maxpacket = ser.readln().unwrap(); - let maxpacketsize = usize::from(u16::from_be_bytes(hex::decode(maxpacket).unwrap())); + let maxpacket = ser.readln().unwrap().unwrap(); + let maxpacketsize = usize::from(u16::from_be_bytes(hex::decode(maxpacket).unwrap().as_slice().try_into().unwrap())); // Exit command mode ser.writeln("ATCN").unwrap(); - assert_response(ser.readln().unwrap(), "OK"); + assert_response(ser.readln().unwrap().unwrap(), String::from("OK")); let ser2 = ser.clone(); thread::spawn(move || writerthread(ser2, maxpacketsize, writerrx)); diff --git a/src/xbpacket.rs b/src/xbpacket.rs index 3e9ac8e..6195158 100644 --- a/src/xbpacket.rs +++ b/src/xbpacket.rs @@ -89,10 +89,10 @@ impl XBTXRequest { innerframe.put_u8(self.broadcast_radius); innerframe.put_u8(self.transmit_options); - innerframe.put_slice(self.payload); + innerframe.put_slice(&self.payload); // That's it for the inner frame. Now fill in the outer frame. - if let Some(lenu16) = u16::try_from(self.payload.len()) { + if let Ok(lenu16) = u16::try_from(self.payload.len()) { fullframe.put_u16(lenu16); fullframe.put_slice(&innerframe); fullframe.put_u8(xbchecksum(&innerframe));