From 9a7700dee4d5f22f1c446ea7d4abc4e43d16ff9f Mon Sep 17 00:00:00 2001 From: Blizzard Finnegan Date: Mon, 19 Jun 2023 14:16:14 -0400 Subject: [PATCH] First rough shot at using built-in serial --- src/device.rs | 36 +++++++++++++++++++++++++++--------- src/main.rs | 17 ++++++----------- src/tty.rs | 30 ++++++++++++++++++++---------- 3 files changed, 53 insertions(+), 30 deletions(-) diff --git a/src/device.rs b/src/device.rs index 5f07a5d..076aaf8 100755 --- a/src/device.rs +++ b/src/device.rs @@ -8,6 +8,7 @@ const BP_SECTION: &str = "Successful BP tests: "; const TEMP_SECTION: &str = "Successful temp tests: "; const OUTPUT_FOLDER: &str = "output/"; const UNINITIALISED_SERIAL: &str = "uninitialised"; +const SERIAL_HEADER: &str = "DtCtrlCfgDeviceSerialNum"; #[derive(PartialEq,Debug)] pub enum State{ Shutdown, @@ -130,7 +131,7 @@ impl Device{ } }; }, - Response::EmptyNewline => { + Response::Serial(_) | Response::EmptyNewline => { log::error!("Unknown state for TTY {:?}!!! Consult logs immediately.",usb_port); return Err("Failed TTY init. Unknown state, cannot trust.".to_string()); }, @@ -345,8 +346,25 @@ impl Device{ } return true } - pub fn set_serial(&mut self, serial:&str) -> &mut Self{ - self.serial = serial.to_string(); + pub fn set_serial(&mut self) -> &mut Self{ + self.reboot(); + self.usb_tty.write_to_device(Command::Login); + while self.usb_tty.read_from_device(None) != Response::ShellPrompt {} + self.usb_tty.write_to_device(Command::GetSerial); + match self.usb_tty.read_from_device(None){ + Response::Serial(Some(contains_serial)) =>{ + for line in contains_serial.split("\n").collect::>(){ + if !line.contains(':') { continue; } + let (section,value) = line.split_once(':').unwrap(); + if section.contains(SERIAL_HEADER){ + self.serial = value.replace("\"",""); + } + } + }, + _ => todo!(), + } + self.reboot(); + //self.serial = serial.to_string(); self.load_values(); self.save_values(); return self; @@ -405,14 +423,14 @@ impl Device{ self.usb_tty.write_to_device(Command::ReadTemp); for _ in 0..10 { match self.usb_tty.read_from_device(None){ - Response::TempCount(count) => return count != self.init_temps , + Response::TempCount(Some(count)) => return count != self.init_temps , _ => {}, } } self.usb_tty.write_to_device(Command::ReadTemp); for _ in 0..10{ match self.usb_tty.read_from_device(None){ - Response::TempCount(count) => return count != self.init_temps , + Response::TempCount(Some(count)) => return count != self.init_temps , _ => {}, } } @@ -425,7 +443,7 @@ impl Device{ self.usb_tty.write_to_device(Command::ReadTemp); for _ in 0..10 { match self.usb_tty.read_from_device(None){ - Response::TempCount(count) => { + Response::TempCount(Some(count)) => { log::trace!("Count for device {} updated to {}",self.serial,count); self.temps = count; return count @@ -436,7 +454,7 @@ impl Device{ self.usb_tty.write_to_device(Command::ReadTemp); for _ in 0..10{ match self.usb_tty.read_from_device(None){ - Response::TempCount(count) => { + Response::TempCount(Some(count)) => { log::trace!("Count for device {} updated to {}",self.serial,count); self.temps = count; return count @@ -453,7 +471,7 @@ impl Device{ self.usb_tty.write_to_device(Command::ReadTemp); for _ in 0..10 { match self.usb_tty.read_from_device(None){ - Response::TempCount(count) => { + Response::TempCount(Some(count)) => { log::trace!("init temp count set to {} on device {}",count,self.serial); self.init_temps = count; return @@ -464,7 +482,7 @@ impl Device{ self.usb_tty.write_to_device(Command::ReadTemp); for _ in 0..10{ match self.usb_tty.read_from_device(None){ - Response::TempCount(count) => { + Response::TempCount(Some(count)) => { log::trace!("init temp count set to {} on device {}",count,self.serial); self.init_temps = count; return diff --git a/src/main.rs b/src/main.rs index 3505a67..665204b 100755 --- a/src/main.rs +++ b/src/main.rs @@ -115,20 +115,15 @@ fn main(){ log::info!("--------------------------------------\n\n"); for device in devices.iter_mut(){ - device.brighten_screen(); - if args.debug{ - let location = device.get_location(); - log::info!("Init device {}...", location); - device.set_serial(&location); - } - else{ - device.set_serial(&input_filtering(Some("Enter the serial of the device with the bright screen: ")).to_string()); - } - device.darken_screen(); + //device.brighten_screen(); + device.set_serial(); + //device.darken_screen(); log::debug!("Number of unassigned addresses: {}",gpio.get_unassigned_addresses().len()); if !find_gpio(device, gpio){ device.set_pin_address(21); - log::error!("Unable to find GPIO for device {}. Please ensure that the probe well is installed properly, and the calibration key is plugged in.",device.get_location()); + log::error!("Unable to find GPIO for device with bright screen. Please ensure that the probe well is installed properly, and the calibration key is plugged in."); + device.brighten_screen(); + return; } } diff --git a/src/tty.rs b/src/tty.rs index 335f69f..d9156d5 100755 --- a/src/tty.rs +++ b/src/tty.rs @@ -1,4 +1,7 @@ -use std::{collections::HashMap, io::{BufReader, Write, Read}, time::Duration}; +use std::{collections::HashMap, + io::{BufReader, Write, Read}, + boxed::Box, + time::Duration}; use once_cell::sync::Lazy; use serialport::SerialPort; use derivative::Derivative; @@ -24,16 +27,17 @@ pub enum Command{ DebugMenu, Newline, Shutdown, + GetSerial, } #[derive(Clone,Eq,Derivative,Debug)] -#[derivative(Copy,PartialEq, Hash)] +#[derivative(PartialEq, Hash)] pub enum Response{ PasswordPrompt, ShellPrompt, BPOn, BPOff, - TempCount(u64), + TempCount(Option), LoginPrompt, DebugMenu, Rebooting, @@ -44,6 +48,7 @@ pub enum Response{ PreShellPrompt, EmptyNewline, DebugInit, + Serial(Option), } @@ -62,21 +67,23 @@ const COMMAND_MAP:Lazy> = Lazy::new(||HashMap::from([ (Command::DebugMenu," python3 -m debugmenu; shutdown -r now\n"), (Command::Newline,"\n"), (Command::Shutdown,"shutdown -r now\n"), + (Command::GetSerial,"echo 'y1q' | python3 -m debugmenu\n"), ])); -const RESPONSES:[(&str,Response);12] = [ +const RESPONSES:[(&str,Response);13] = [ ("Last login:",Response::PreShellPrompt), ("reboot: Restarting",Response::Rebooting), ("command not found",Response::FailedDebugMenu), ("login:",Response::LoginPrompt), ("Password:",Response::PasswordPrompt), - ("EXIT Debug menu",Response::ShuttingDown), ("root@",Response::ShellPrompt), + ("DtCtrlCfgDeviceSerialNum",Response::Serial(None)), + ("EXIT Debug menu",Response::ShuttingDown), ("Check NIBP In Progress: True",Response::BPOn), ("Check NIBP In Progress: False",Response::BPOff), - ("SureTemp Probe Pulls:",Response::TempCount(0)), + ("SureTemp Probe Pulls:",Response::TempCount(None)), (">",Response::DebugMenu), - ("Loading App-Framework",Response::DebugInit) + ("Loading App-Framework",Response::DebugInit), ]; pub struct TTY{ @@ -144,7 +151,7 @@ impl TTY{ log::trace!("Successful read of {:?} from tty {}, which matches pattern {:?}",read_line,self.tty.name().unwrap_or("unknown shell".to_string()),enum_value); }; self.failed_read_count = 0; - if enum_value == Response::TempCount(0){ + if enum_value == Response::TempCount(None){ let mut lines = read_line.lines(); while let Some(single_line) = lines.next(){ if single_line.contains(string){ @@ -155,12 +162,12 @@ impl TTY{ match temp_count.trim().parse::(){ Err(_) => { log::error!("String {} from device {} unable to be parsed!",temp_count,self.tty.name().unwrap_or("unknown shell".to_string())); - return Response::TempCount(0) + return Response::TempCount(None) }, Ok(parsed_temp_count) => { //log::trace!("Header: {}",header); log::trace!("parsed temp count for device {}: {}",self.tty.name().unwrap_or("unknown shell".to_string()),temp_count); - return Response::TempCount(parsed_temp_count) + return Response::TempCount(Some(parsed_temp_count)) } } } @@ -168,6 +175,9 @@ impl TTY{ } } } + else if enum_value == Response::Serial(None) { + return Response::Serial(Some(read_line)); + } else if enum_value == Response::PasswordPrompt { log::error!("Recieved password prompt on device {}! Something fell apart here. Check preceeding log lines.",self.tty.name().unwrap_or("unknown shell".to_string())); self.write_to_device(Command::Newline);