From 66e4d9f803b9f8fec7acc975b116ba84efb387d7 Mon Sep 17 00:00:00 2001 From: bunnie Date: Tue, 17 May 2022 02:25:59 +0800 Subject: [PATCH] move test commands for USB status checkup from keys to USB --- services/shellchat/src/cmds/keys.rs | 10 +---- services/shellchat/src/cmds/usb.rs | 66 +++++++++++++++++++++++------ 2 files changed, 53 insertions(+), 23 deletions(-) diff --git a/services/shellchat/src/cmds/keys.rs b/services/shellchat/src/cmds/keys.rs index 061574e2f..f4ac055b9 100644 --- a/services/shellchat/src/cmds/keys.rs +++ b/services/shellchat/src/cmds/keys.rs @@ -61,7 +61,7 @@ impl Keys { impl<'a> ShellCmdApi<'a> for Keys { cmd_api!(keys); // inserts boilerplate for command API - fn process(&mut self, args: String::<1024>, env: &mut CommonEnv) -> Result>, xous::Error> { + fn process(&mut self, args: String::<1024>, _env: &mut CommonEnv) -> Result>, xous::Error> { use core::fmt::Write; let mut ret = String::<1024>::new(); let helpstring = "keys [usblock] [usbunlock] [pddbrecycle]"; @@ -106,14 +106,6 @@ impl<'a> ShellCmdApi<'a> for Keys { write!(ret, "aes test failed").unwrap(); } } - "usblock" => { - env.llio.debug_usb(Some(true)).unwrap(); - write!(ret, "USB debug port locked out; one word at 0x80000000 is disclosable via USB.").unwrap(); - } - "usbunlock" => { - env.llio.debug_usb(Some(false)).unwrap(); - write!(ret, "USB debug port unlocked: all secrets are readable via USB!").unwrap(); - } "pddbrecycle" => { // erase the page table, which should effectively trigger a reformat on the next boot self.spinor.bulk_erase(xous::PDDB_LOC, 1024 * 1024).expect("couldn't erase page table"); diff --git a/services/shellchat/src/cmds/usb.rs b/services/shellchat/src/cmds/usb.rs index ca05672e8..d613a7759 100644 --- a/services/shellchat/src/cmds/usb.rs +++ b/services/shellchat/src/cmds/usb.rs @@ -1,5 +1,5 @@ use crate::{ShellCmdApi, CommonEnv}; -use usb_device_xous::{UsbHid, UsbDeviceState}; +use usb_device_xous::{UsbHid, UsbDeviceState, UsbDeviceType}; use std::fmt::Write; #[derive(Debug)] @@ -20,33 +20,71 @@ impl<'a> ShellCmdApi<'a> for Usb { fn process(&mut self, args: xous_ipc::String::<1024>, _env: &mut CommonEnv) -> Result>, xous::Error> { let mut ret = xous_ipc::String::<1024>::new(); - let helpstring = "usb [send ] [status] [leds]"; + let helpstring = "usb [hid] [debug] [send ] [status] [leds] [lock] [unlock]"; let mut tokens = args.as_str().unwrap().split(' '); if let Some(sub_cmd) = tokens.next() { match sub_cmd { + "hid" => { + self.usb_dev.switch_to_core(usb_device_xous::UsbDeviceType::Hid).unwrap(); + write!(ret, "USB connected to HID core").unwrap(); + } + "debug" => { + self.usb_dev.switch_to_core(usb_device_xous::UsbDeviceType::Debug).unwrap(); + write!(ret, "USB connected to Debug core").unwrap(); + } "send" => { - let mut val = String::new(); - join_tokens(&mut val, &mut tokens); - match self.usb_dev.send_str(&val) { - Ok(n) => write!(ret, "Sent {} chars", n).unwrap(), - Err(_e) => write!(ret, "Can't send: are we connected to a host?").unwrap(), + match self.usb_dev.get_current_core() { + Ok(UsbDeviceType::Hid) => { + let mut val = String::new(); + join_tokens(&mut val, &mut tokens); + match self.usb_dev.send_str(&val) { + Ok(n) => write!(ret, "Sent {} chars", n).unwrap(), + Err(_e) => write!(ret, "Can't send: are we connected to a host?").unwrap(), + } + } + Ok(UsbDeviceType::Debug) => { + write!(ret, "HID core not connected: please issue 'usb hid' first").unwrap(); + } + _ => write!(ret, "Invalid response checking status").unwrap(), } } "status" => { - match self.usb_dev.status() { - UsbDeviceState::Configured => write!(ret, "USB connected to host").unwrap(), - UsbDeviceState::Suspend => write!(ret, "Host put us in suspend").unwrap(), - _ => write!(ret, "Not connected to USB host").unwrap(), + match self.usb_dev.get_current_core() { + Ok(UsbDeviceType::Debug) => write!(ret, "Debug core connected").unwrap(), + Ok(UsbDeviceType::Hid) => { + match self.usb_dev.status() { + UsbDeviceState::Configured => write!(ret, "HID core connected to host").unwrap(), + UsbDeviceState::Suspend => write!(ret, "HID in suspend").unwrap(), + _ => write!(ret, "HID not connected to USB host").unwrap(), + } + } + _ => write!(ret, "Invalid response checking status").unwrap(), } } "leds" => { - match self.usb_dev.get_led_state() { - Ok(leds) => write!(ret, "LEDs: {:?}", leds).unwrap(), - _ => write!(ret, "Not connected to USB host or other error").unwrap(), + match self.usb_dev.get_current_core() { + Ok(UsbDeviceType::Hid) => { + match self.usb_dev.get_led_state() { + Ok(leds) => write!(ret, "LEDs: {:?}", leds).unwrap(), + _ => write!(ret, "Not connected to USB host or other error").unwrap(), + } + } + Ok(UsbDeviceType::Debug) => { + write!(ret, "HID core not connected: please issue 'usb hid' first").unwrap(); + } + _ => write!(ret, "Invalid response checking status").unwrap(), } } + "lock" => { + self.usb_dev.restrict_debug_access(true).unwrap(); + write!(ret, "USB debug port locked out; one word at 0x80000000 is disclosable via USB.").unwrap(); + } + "unlock" => { + self.usb_dev.restrict_debug_access(false).unwrap(); + write!(ret, "USB debug port unlocked: all secrets are readable via USB!").unwrap(); + } _ => { write!(ret, "{}", helpstring).unwrap(); }