diff --git a/services/usb-device-xous/src/api.rs b/services/usb-device-xous/src/api.rs index 14e0f7470..f68a41f43 100644 --- a/services/usb-device-xous/src/api.rs +++ b/services/usb-device-xous/src/api.rs @@ -27,6 +27,8 @@ pub(crate) enum Opcode { DebugUsbOp = 9, /// Set autotype rate SetAutotypeRate = 10, + /// Register a USB event observer + RegisterUsbObserver = 11, /// Send a U2F message U2fTx = 128, @@ -139,3 +141,10 @@ pub struct UsbSerialBinary { pub d: [u8; SERIAL_BINARY_BUFLEN], pub len: usize, } + +/// this structure is used to register a USB listener. +#[derive(Debug, rkyv::Archive, rkyv::Serialize, rkyv::Deserialize, Copy, Clone)] +pub(crate) struct UsbListenerRegistration { + pub server_name: xous_ipc::String<64>, + pub listener_op_id: usize, +} diff --git a/services/usb-device-xous/src/lib.rs b/services/usb-device-xous/src/lib.rs index f84a49ed9..9425e30cb 100644 --- a/services/usb-device-xous/src/lib.rs +++ b/services/usb-device-xous/src/lib.rs @@ -370,6 +370,16 @@ impl UsbHid { ) .unwrap(); } + + pub fn register_u2f_observer(&self, server_name: &str, action_opcode: usize) { + let kr = UsbListenerRegistration { + server_name: xous_ipc::String::<64>::from_str(server_name), + listener_op_id: action_opcode, + }; + let buf = Buffer::into_buf(kr).unwrap(); + buf.lend(self.conn, Opcode::RegisterUsbObserver.to_u32().unwrap()) + .expect("couldn't register listener"); + } } 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 f0a4396f4..fd783c213 100644 --- a/services/usb-device-xous/src/main_hw.rs +++ b/services/usb-device-xous/src/main_hw.rs @@ -294,6 +294,10 @@ pub(crate) fn main_hw() -> ! { let mut was_suspend = true; let mut autotype_delay_ms = 30; + // event observer connection + let mut observer_conn: Option = None; + let mut observer_op: Option = None; + #[cfg(feature = "minimal")] std::thread::spawn(move || { // this keeps the watchdog alive in minimal mode; if there's no event, eventually the watchdog times @@ -492,6 +496,15 @@ pub(crate) fn main_hw() -> ! { xous::return_scalar(msg.sender, 1).expect("couldn't return compatibility status") }), Some(Opcode::U2fRxDeferred) => { + // notify the event listener, if any + if observer_conn.is_some() && observer_op.is_some() { + xous::try_send_message( + observer_conn.unwrap(), + xous::Message::new_scalar(observer_op.unwrap(), 0, 0, 0, 0), + ) + .ok(); + } + if fido_listener_pid.is_none() { fido_listener_pid = msg.sender.pid(); } @@ -1537,6 +1550,23 @@ pub(crate) fn main_hw() -> ! { serial_trng_interval.store(TRNG_REFILL_DELAY_MS, Ordering::SeqCst); } } + Some(Opcode::RegisterUsbObserver) => { + let buffer = unsafe { Buffer::from_memory_message(msg.body.memory_message().unwrap()) }; + let ur = buffer.as_flat::().unwrap(); + if observer_conn.is_none() { + match xns.request_connection_blocking(ur.server_name.as_str()) { + Ok(cid) => { + observer_conn = Some(cid); + observer_op = Some(ur.listener_op_id as usize); + } + Err(e) => { + log::error!("couldn't connect to observer: {:?}", e); + observer_conn = None; + observer_op = None; + } + } + } + } Some(Opcode::Quit) => { log::warn!("Quit received, goodbye world!"); break;