diff --git a/services/usb-device-xous/Cargo.toml b/services/usb-device-xous/Cargo.toml index 939d4b044..daa1954e0 100644 --- a/services/usb-device-xous/Cargo.toml +++ b/services/usb-device-xous/Cargo.toml @@ -19,7 +19,7 @@ num-traits = {version = "0.2.14", default-features = false} rkyv = {version = "0.4.3", default-features = false, features = ["const_generics"]} susres = {package = "xous-api-susres", version = "0.9.35"} modals = {path = "../modals"} -keyboard = {path = "../keyboard"} +keyboard = {path = "../keyboard", feature="inject-api"} bitfield = "0.13.2" vcell = "0.1.3" xous-semver = "0.1.2" diff --git a/services/usb-device-xous/src/api.rs b/services/usb-device-xous/src/api.rs index cff065a1b..62a85a4ae 100644 --- a/services/usb-device-xous/src/api.rs +++ b/services/usb-device-xous/src/api.rs @@ -43,6 +43,10 @@ pub(crate) enum Opcode { SerialFlush = 514, /// Hook eager serial sender. This is mean for e.g. using Precursor as an infinite source of random data. SerialHookEagerSender = 515, + /// Hook serial to the console input + SerialHookConsole = 516, + /// Clear any hooks + SerialClearHooks = 517, #[cfg(feature="mass-storage")] SetBlockDevice = 1024, diff --git a/services/usb-device-xous/src/lib.rs b/services/usb-device-xous/src/lib.rs index 8f9ab3d14..670a8c025 100644 --- a/services/usb-device-xous/src/lib.rs +++ b/services/usb-device-xous/src/lib.rs @@ -376,7 +376,24 @@ impl UsbHid { ) ).map(|_| ()) } - + /// Inject serial input over USB to the debug console. Dangerous! + /// This will also override/discard any existing hooked listeners. + pub fn serial_console_input_injection(&self) { + send_message( + self.conn, + Message::new_scalar( + Opcode::SerialHookConsole.to_usize().unwrap(), 0, 0, 0, 0 + ) + ).unwrap(); + } + pub fn serial_clear_input_hooks(&self) { + send_message( + self.conn, + Message::new_scalar( + Opcode::SerialClearHooks.to_usize().unwrap(), 0, 0, 0, 0 + ) + ).unwrap(); + } } use core::sync::atomic::{AtomicU32, Ordering}; diff --git a/services/usb-device-xous/src/main_hw.rs b/services/usb-device-xous/src/main_hw.rs index 67bd8ac29..37870a282 100644 --- a/services/usb-device-xous/src/main_hw.rs +++ b/services/usb-device-xous/src/main_hw.rs @@ -78,6 +78,8 @@ enum SerialListenMode { // hooked listener. If this mode is set and there is no listener, it will buffer data "indefinitely" // (e.g. until local heap is exhausted and the system panics) BinaryListener, + // this will take any serial input and pass it on as if one was typing at the console + ConsoleListener, } pub(crate) fn main_hw() -> ! { @@ -590,6 +592,25 @@ pub(crate) fn main_hw() -> ! { } } } + SerialListenMode::ConsoleListener => { + match serial_port.read(&mut data) { + Ok(len) => { + match std::str::from_utf8(&data[..len]) { + Ok(s) => { + for c in s.chars() { + native_kbd.inject_key(c); + } + }, + Err(_) => { + log::info!("Non UTF-8 received on console: {:x?}", &data[..len]); + } + } + } + Err(e) => { + log::info!("Serial read error: {:?}", e); + } + } + } SerialListenMode::AsciiListener(maybe_delimiter) => { let readlen = serial_port.read(&mut data).unwrap_or(0); if readlen == 0 { @@ -1072,6 +1093,15 @@ pub(crate) fn main_hw() -> ! { serial_listen_mode = SerialListenMode::BinaryListener; serial_listener = Some(msg); }, + Some(Opcode::SerialHookConsole) => msg_scalar_unpack!(msg, _, _, _, _, { + serial_listen_mode = SerialListenMode::ConsoleListener; + // unhook any previous pending listener + serial_listener.take(); + }), + Some(Opcode::SerialClearHooks) => { + serial_listen_mode = SerialListenMode::NoListener; + serial_listener.take(); + } Some(Opcode::SerialFlush) => msg_scalar_unpack!(msg, _, _, _, _, { // this will hardware flush any pending items in usb_serial driver serial_port.flush().ok();