add input-side key injection over usb serial

This commit is contained in:
bunnie 2023-04-16 02:23:10 +08:00
parent af5b54aba2
commit dde6addc18
4 changed files with 53 additions and 2 deletions

View file

@ -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"

View file

@ -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,

View file

@ -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};

View file

@ -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();