From 269578ba2aa97aa2e9781e8e08b1281fb2b9e5fd Mon Sep 17 00:00:00 2001 From: Fabian Kaczmarczyck Date: Thu, 5 Sep 2024 12:22:47 +0200 Subject: [PATCH] Stable blinking pattern Before, if the device gets a CTAP2 UP request and another channel sends packets, the blinking pattern would have sped up. The patch for the Tock USB implementation fixes a panic after a timeout in send. --- patches/tock/03-add-ctap-modules.patch | 7 +++---- src/env/tock/mod.rs | 15 +++++++++++++-- 2 files changed, 16 insertions(+), 6 deletions(-) diff --git a/patches/tock/03-add-ctap-modules.patch b/patches/tock/03-add-ctap-modules.patch index d4adb6e9..f4d41350 100644 --- a/patches/tock/03-add-ctap-modules.patch +++ b/patches/tock/03-add-ctap-modules.patch @@ -559,10 +559,10 @@ index 000000000..30cac1323 +} diff --git a/capsules/src/usb/usbc_ctap_hid.rs b/capsules/src/usb/usbc_ctap_hid.rs new file mode 100644 -index 000000000..5ad2c44b3 +index 000000000..fdf92a28e --- /dev/null +++ b/capsules/src/usb/usbc_ctap_hid.rs -@@ -0,0 +1,554 @@ +@@ -0,0 +1,553 @@ +//! A USB HID client of the USB hardware interface + +use super::app::App; @@ -927,9 +927,8 @@ index 000000000..5ad2c44b3 + if self + .client + .map_or(false, |client| client.can_receive_packet(&app)) ++ && self.pending_out.take() + { -+ assert!(self.pending_out.take()); -+ + // Clear any pending packet on the transmitting side. + // It's up to the client to handle the received packet and decide if this packet + // should be re-transmitted or not. diff --git a/src/env/tock/mod.rs b/src/env/tock/mod.rs index cbdf1b49..edb4b50c 100644 --- a/src/env/tock/mod.rs +++ b/src/env/tock/mod.rs @@ -17,6 +17,7 @@ use alloc::vec::Vec; use clock::TockClock; use core::convert::TryFrom; use core::marker::PhantomData; +use core::mem; #[cfg(all(target_has_atomic = "8", not(feature = "std")))] use core::sync::atomic::{AtomicBool, Ordering}; use libtock_console::{Console, ConsoleWriter}; @@ -26,6 +27,7 @@ use libtock_drivers::{rng, usb_ctap_hid}; use libtock_leds::Leds; use libtock_platform as platform; use libtock_platform::Syscalls; +use opensk::api::clock::Clock; use opensk::api::connection::{HidConnection, RecvStatus, UsbEndpoint}; use opensk::api::crypto::software_crypto::SoftwareCrypto; use opensk::api::customization::{CustomizationImpl, AAGUID_LENGTH, DEFAULT_CUSTOMIZATION}; @@ -118,6 +120,7 @@ pub struct TockEnv< store: Store>, upgrade_storage: Option>, blink_pattern: usize, + blink_timer: as Clock>::Timer, clock: TockClock, c: PhantomData, } @@ -141,6 +144,7 @@ impl D store, upgrade_storage, blink_pattern: 0, + blink_timer: as Clock>::Timer::default(), clock: TockClock::default(), c: PhantomData, } @@ -295,8 +299,14 @@ where packet: &mut [u8; 64], timeout_ms: usize, ) -> UserPresenceWaitResult { - blink_leds::(self.blink_pattern); - self.blink_pattern += 1; + let mut new_timer = self.clock.make_timer(timeout_ms); + mem::swap(&mut self.blink_timer, &mut new_timer); + if self.clock().is_elapsed(&new_timer) { + blink_leds::(self.blink_pattern); + self.blink_pattern += 1; + } else { + mem::swap(&mut self.blink_timer, &mut new_timer); + } let result = UsbCtapHid::::recv_with_buttons(packet, Duration::from_ms(timeout_ms as isize)); @@ -323,6 +333,7 @@ where fn check_complete(&mut self) { switch_off_leds::(); + self.blink_timer = as Clock>::Timer::default(); } }