From 823543ff036f050bbbb313e9b0a491065ca4b08c Mon Sep 17 00:00:00 2001 From: Blizzard Finnegan Date: Fri, 16 Jun 2023 09:53:46 -0400 Subject: [PATCH] Update init and project name Project is no longer Proof Of Concept. Modified Device init to be more consistent. Started explicit typing of variables. --- Cargo.lock | 2 +- Cargo.toml | 2 +- src/device.rs | 56 +++++++++++++++++++++++++++++++++++---------------- src/main.rs | 6 +++--- src/tty.rs | 18 +++++++++++++---- 5 files changed, 58 insertions(+), 26 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 5612343..dc445d8 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -638,7 +638,7 @@ dependencies = [ ] [[package]] -name = "seymour_poc_rust" +name = "seymour_life_rust" version = "2.2.0" dependencies = [ "chrono", diff --git a/Cargo.toml b/Cargo.toml index f1ede23..ab9d269 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,7 +1,7 @@ #cargo-features = ["per-package-target"] [package] -name = "seymour_poc_rust" +name = "seymour_life_rust" version = "2.2.0" edition = "2021" diff --git a/src/device.rs b/src/device.rs index 9732720..c9fc610 100755 --- a/src/device.rs +++ b/src/device.rs @@ -40,10 +40,10 @@ impl Device{ _ = fs::create_dir(&OUTPUT_FOLDER); }; log::debug!("{:?}",&self.serial); - let output_path = OUTPUT_FOLDER.to_owned() + &self.serial + ".txt"; + let output_path:String = OUTPUT_FOLDER.to_owned() + &self.serial + ".txt"; if ! Path::new(&output_path).exists(){ log::debug!("Creating file {}",&output_path); - let temp = fs::File::create(&output_path); + let temp:Result = fs::File::create(&output_path); match temp{ Ok(file) => { self.output_file = Some(file); @@ -55,17 +55,17 @@ impl Device{ } } else { - let temp = std::fs::read_to_string(output_path); + let temp:Result = std::fs::read_to_string(output_path); match temp{ Ok(file_contents) =>{ - let file_lines = file_contents.split("\n"); + let file_lines:Vec<&str> = file_contents.split("\n").collect(); log::trace!("{:?}",file_contents); for line in file_lines { if line.len() > 0{ log::trace!("{:?}",line); let section_and_data:Vec<&str> = line.split(": ").collect(); - let section = section_and_data[0]; - let possible_value = section_and_data[1].trim().parse::(); + let section:&str = section_and_data[0]; + let possible_value:Result = section_and_data[1].trim().parse::(); match possible_value{ Ok(value) => { log::trace!("{:?} value: [{:?}]",section,value); @@ -83,7 +83,7 @@ impl Device{ }; } Err(_) => { - log::warn!("Unable to parse value [{}] into integer",section_and_data[1]); + log::warn!("Unable to parse value [{:?}] into integer",section_and_data); } } }; @@ -107,14 +107,32 @@ impl Device{ _ = usb_port.read_from_device(None); initial_state = State::LoginPrompt; }, + //Response::Empty parsing here is potentially in bad faith Response::Other | Response::Empty | Response::ShellPrompt | Response::LoginPrompt | Response::ShuttingDown | Response::Rebooting => - initial_state = State::LoginPrompt, - Response::BPOn | Response::BPOff | Response::TempCount(_) => - initial_state = State::LifecycleMenu, - Response::DebugMenuReady | Response::DebugMenuWithContinuedMessage=> - initial_state = State::DebugMenu, - } + initial_state = State::LoginPrompt, + Response::BPOn | Response::BPOff | Response::TempCount(_) | + Response::DebugMenuReady | Response::DebugMenuWithContinuedMessage=>{ + usb_port.write_to_device(Command::Quit); + _ = usb_port.read_from_device(None); + usb_port.write_to_device(Command::Newline); + match usb_port.read_from_device(None){ + Response::Rebooting => { + while usb_port.read_from_device(None) != Response::LoginPrompt {} + initial_state = State::LoginPrompt; + }, + Response::ShellPrompt => { + usb_port.write_to_device(Command::Shutdown); + while usb_port.read_from_device(None) != Response::LoginPrompt {} + initial_state = State::LoginPrompt; + }, + _ => { + log::error!("Unknown state for TTY {:?}!!! Consult logs immediately.",usb_port); + return Err("Failed TTY init. Unknown state, cannot trust.".to_string()); + } + }; + }, + }; }, None => initial_state = State::LoginPrompt }; @@ -259,8 +277,8 @@ impl Device{ output_data.push_str(&self.bps.to_string()); output_data.push_str("\n"); output_data.push_str(TEMP_SECTION); - log::trace!("Current temps: {}",self.temps); - log::trace!("Initial temps: {}",self.init_temps); + log::trace!("Current temps: [{}]",self.temps); + log::trace!("Initial temps: [{}]",self.init_temps); let saved_temps = self.temps - self.init_temps; output_data.push_str(&saved_temps.to_string()); output_data.push_str("\n"); @@ -359,6 +377,7 @@ impl Device{ match self.usb_tty.read_from_device(None){ Response::TempCount(count) => { log::trace!("Count for device {} updated to {}",self.serial,count); + self.temps = count; return count }, _ => {}, @@ -369,6 +388,7 @@ impl Device{ match self.usb_tty.read_from_device(None){ Response::TempCount(count) => { log::trace!("Count for device {} updated to {}",self.serial,count); + self.temps = count; return count }, _ => {}, @@ -420,16 +440,18 @@ impl Device{ pub fn reboot(&mut self) -> () { self.usb_tty.write_to_device(Command::Quit); let mut successful_reboot:bool = false; + let mut exited_menu:bool = false; loop{ match self.usb_tty.read_from_device(None){ Response::LoginPrompt => break, Response::Rebooting => { log::trace!("Successful reboot detected for device {}.",self.serial); successful_reboot = true; + if !exited_menu { log::info!("Unusual reboot detected for device {}. Please check logs.",self.serial); } }, Response::ShuttingDown => { - log::warn!("Failed reboot on device {}!",self.serial); - successful_reboot = false; + log::trace!("Exiting debug menu on device {}.",self.serial); + exited_menu = true; }, _ => {} } diff --git a/src/main.rs b/src/main.rs index a3e7e82..3505a67 100755 --- a/src/main.rs +++ b/src/main.rs @@ -1,6 +1,6 @@ -use seymour_poc_rust::{device::Device, - tty::{self,TTY,Response}, - gpio_facade::GpioPins}; +use seymour_life_rust::{device::Device, + tty::{self,TTY,Response}, + gpio_facade::GpioPins}; use std::{io::{stdin,stdout,Write}, thread::{self, JoinHandle}, path::Path, diff --git a/src/tty.rs b/src/tty.rs index ceed27a..415e0ba 100755 --- a/src/tty.rs +++ b/src/tty.rs @@ -23,6 +23,7 @@ pub enum Command{ Login, DebugMenu, Newline, + Shutdown, } #[derive(Clone,Eq,Derivative,Debug)] @@ -57,6 +58,7 @@ const COMMAND_MAP:Lazy> = Lazy::new(||HashMap::from([ (Command::RedrawMenu,"?"), (Command::DebugMenu," python3 -m debugmenu; shutdown -r now\n"), (Command::Newline,"\n"), + (Command::Shutdown,"shutdown -r now\n"), ])); const RESPONSES:[(&str,Response);10] = [ @@ -135,10 +137,18 @@ impl TTY{ let trimmed_line = single_line.trim(); match trimmed_line.rsplit_once(' '){ None => return enum_value, - Some((header,temp_count)) => { - log::trace!("Header: {}",header); - log::trace!("Temp count: {}",temp_count); - return Response::TempCount(temp_count.parse().unwrap_or(0)) + Some((_header,temp_count)) => { + 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) + }, + 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) + } + } } } }