add a U2F USB event observer hook to the USB stack

the intent of this is to send a message to listeners that
want to be aware of the fact that a U2F packet has arrived.
This commit is contained in:
bunnie 2024-01-29 23:14:44 +08:00
parent 795738a2f2
commit b2208850b2
3 changed files with 49 additions and 0 deletions

View file

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

View file

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

View file

@ -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<xous::CID> = None;
let mut observer_op: Option<usize> = 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::<UsbListenerRegistration, _>().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;