make keyboard composite device optional

just to make debugging a bit easier
This commit is contained in:
bunnie 2022-06-03 18:17:39 +08:00
parent 6ff5924cea
commit 4dca789d22
2 changed files with 40 additions and 23 deletions

View file

@ -41,4 +41,5 @@ rand_chacha = "0.3.1"
utralib = { path = "../../utralib"} utralib = { path = "../../utralib"}
[features] [features]
default = [] keyboard = [] # handy for debugging composite device issues
default = ["keyboard"]

View file

@ -142,7 +142,7 @@ fn main() -> ! {
let usb_alloc = UsbBusAllocator::new(usbdev); let usb_alloc = UsbBusAllocator::new(usbdev);
#[cfg(any(target_os = "none", target_os = "xous"))] #[cfg(any(target_os = "none", target_os = "xous"))]
let clock = EmbeddedClock::new(); let clock = EmbeddedClock::new();
#[cfg(any(target_os = "none", target_os = "xous"))] #[cfg(all(any(target_os = "none", target_os = "xous"), feature="keyboard"))]
let mut composite = UsbHidClassBuilder::new() let mut composite = UsbHidClassBuilder::new()
.add_interface( .add_interface(
NKROBootKeyboardInterface::default_config(&clock), NKROBootKeyboardInterface::default_config(&clock),
@ -151,6 +151,12 @@ fn main() -> ! {
FidoInterface::default_config() FidoInterface::default_config()
) )
.build(&usb_alloc); .build(&usb_alloc);
#[cfg(all(any(target_os = "none", target_os = "xous"), not(feature="keyboard")))]
let mut composite = UsbHidClassBuilder::new()
.add_interface(
FidoInterface::default_config()
)
.build(&usb_alloc);
#[cfg(any(target_os = "none", target_os = "xous"))] #[cfg(any(target_os = "none", target_os = "xous"))]
let mut usb_dev = UsbDeviceBuilder::new(&usb_alloc, UsbVidPid(0x1209, 0x0001)) let mut usb_dev = UsbDeviceBuilder::new(&usb_alloc, UsbVidPid(0x1209, 0x0001))
@ -158,7 +164,7 @@ fn main() -> ! {
.product("Precursor") .product("Precursor")
.serial_number(&serial_number) .serial_number(&serial_number)
.build(); .build();
#[cfg(any(target_os = "none", target_os = "xous"))] #[cfg(all(any(target_os = "none", target_os = "xous"), feature="keyboard"))]
{ {
let keyboard = composite.interface::<NKROBootKeyboardInterface<'_, _, _,>, _>(); let keyboard = composite.interface::<NKROBootKeyboardInterface<'_, _, _,>, _>();
keyboard.write_report(&Vec::<Keyboard>::new()).ok(); keyboard.write_report(&Vec::<Keyboard>::new()).ok();
@ -195,6 +201,7 @@ fn main() -> ! {
if fido_listener_pid == msg.sender.pid() { if fido_listener_pid == msg.sender.pid() {
// preferentially pull from the rx queue if it has elements // preferentially pull from the rx queue if it has elements
if let Some(data) = fido_rx_queue.pop_front() { if let Some(data) = fido_rx_queue.pop_front() {
log::info!("returning queued data: {:?}", &data);
let mut response = unsafe { let mut response = unsafe {
Buffer::from_memory_message_mut(msg.body.memory_message_mut().unwrap()) Buffer::from_memory_message_mut(msg.body.memory_message_mut().unwrap())
}; };
@ -207,7 +214,7 @@ fn main() -> ! {
{ {
let buffer = unsafe { Buffer::from_memory_message(msg.body.memory_message().unwrap()) }; let buffer = unsafe { Buffer::from_memory_message(msg.body.memory_message().unwrap()) };
let u2f_ipc = buffer.to_original::<U2fMsgIpc, _>().unwrap(); let u2f_ipc = buffer.to_original::<U2fMsgIpc, _>().unwrap();
log::trace!("registering listener: {:?}", u2f_ipc); log::info!("registering listener: {:?}", u2f_ipc);
} }
fido_listener = Some(msg); fido_listener = Some(msg);
} }
@ -229,11 +236,11 @@ fn main() -> ! {
let mut u2f_msg = FidoMsg::default(); let mut u2f_msg = FidoMsg::default();
assert_eq!(u2f_ipc.code, U2fCode::Tx, "Expected U2fCode::Tx in wrapper"); assert_eq!(u2f_ipc.code, U2fCode::Tx, "Expected U2fCode::Tx in wrapper");
u2f_msg.packet.copy_from_slice(&u2f_ipc.data); u2f_msg.packet.copy_from_slice(&u2f_ipc.data);
log::info!("send U2F packet {:x?}", u2f_ipc.data);
#[cfg(any(target_os = "none", target_os = "xous"))] #[cfg(any(target_os = "none", target_os = "xous"))]
let u2f = composite.interface::<FidoInterface<'_, _>, _>(); let u2f = composite.interface::<FidoInterface<'_, _>, _>();
#[cfg(any(target_os = "none", target_os = "xous"))] #[cfg(any(target_os = "none", target_os = "xous"))]
u2f.write_report(&u2f_msg).ok(); u2f.write_report(&u2f_msg).ok();
log::info!("sent U2F packet {:x?}", u2f_ipc.data);
u2f_ipc.code = U2fCode::TxAck; u2f_ipc.code = U2fCode::TxAck;
} else { } else {
u2f_ipc.code = U2fCode::Denied; u2f_ipc.code = U2fCode::Denied;
@ -243,12 +250,15 @@ fn main() -> ! {
Some(Opcode::UsbIrqHandler) => { Some(Opcode::UsbIrqHandler) => {
#[cfg(any(target_os = "none", target_os = "xous"))] #[cfg(any(target_os = "none", target_os = "xous"))]
if usb_dev.poll(&mut [&mut composite]) { if usb_dev.poll(&mut [&mut composite]) {
let keyboard = composite.interface::<NKROBootKeyboardInterface<'_, _, _,>, _>(); #[cfg(feature="keyboard")]
match keyboard.read_report() { {
Ok(l) => { let keyboard = composite.interface::<NKROBootKeyboardInterface<'_, _, _,>, _>();
led_state = l; match keyboard.read_report() {
Ok(l) => {
led_state = l;
}
Err(e) => log::trace!("KEYB ERR: {:?}", e),
} }
Err(e) => log::trace!("KEYB ERR: {:?}", e),
} }
let u2f = composite.interface::<FidoInterface<'_, _>, _>(); let u2f = composite.interface::<FidoInterface<'_, _>, _>();
match u2f.read_report() { match u2f.read_report() {
@ -355,14 +365,17 @@ fn main() -> ! {
codes.push(Keyboard::from_primitive(code2 as u8)); codes.push(Keyboard::from_primitive(code2 as u8));
} }
let auto_up = if autoup == 1 {true} else {false}; let auto_up = if autoup == 1 {true} else {false};
let keyboard = composite.interface::<NKROBootKeyboardInterface<'_, _, _,>, _>(); #[cfg(feature="keyboard")]
keyboard.write_report(&codes).ok(); {
keyboard.tick().unwrap(); let keyboard = composite.interface::<NKROBootKeyboardInterface<'_, _, _,>, _>();
tt.sleep_ms(30).ok(); keyboard.write_report(&codes).ok();
if auto_up {
keyboard.write_report(&[]).ok(); // this is the key-up
keyboard.tick().unwrap(); keyboard.tick().unwrap();
tt.sleep_ms(30).ok(); tt.sleep_ms(30).ok();
if auto_up {
keyboard.write_report(&[]).ok(); // this is the key-up
keyboard.tick().unwrap();
tt.sleep_ms(30).ok();
}
} }
xous::return_scalar(msg.sender, 0).unwrap(); xous::return_scalar(msg.sender, 0).unwrap();
} else { } else {
@ -388,13 +401,16 @@ fn main() -> ! {
KeyMap::Dvorak => mappings::char_to_hid_code_dvorak(ch), KeyMap::Dvorak => mappings::char_to_hid_code_dvorak(ch),
_ => mappings::char_to_hid_code_us101(ch), _ => mappings::char_to_hid_code_us101(ch),
}; };
let keyboard = composite.interface::<NKROBootKeyboardInterface<'_, _, _,>, _>(); #[cfg(feature="keyboard")]
keyboard.write_report(&codes).ok(); {
keyboard.tick().unwrap(); let keyboard = composite.interface::<NKROBootKeyboardInterface<'_, _, _,>, _>();
tt.sleep_ms(30).ok(); keyboard.write_report(&codes).ok();
keyboard.write_report(&[]).ok(); // this is the key-up keyboard.tick().unwrap();
keyboard.tick().unwrap(); tt.sleep_ms(30).ok();
tt.sleep_ms(30).ok(); keyboard.write_report(&[]).ok(); // this is the key-up
keyboard.tick().unwrap();
tt.sleep_ms(30).ok();
}
sent += 1; sent += 1;
} }
usb_send.sent = Some(sent); usb_send.sent = Some(sent);