From ffc7fd5232a6984d283d786a28922ccef594763c Mon Sep 17 00:00:00 2001
From: tamara <tamaf96@zedat.fu-berlin.de>
Date: Fri, 19 Jun 2020 12:14:54 +0200
Subject: [PATCH] replace TockValue with TockReturn and replace
 timer::with_callback to timer::DriverContext::with_callback (and create
 context)

---
 src/main.rs         |  9 ++++++++-
 src/usb_ctap_hid.rs | 44 ++++++++++++++++++++++++++++++--------------
 2 files changed, 38 insertions(+), 15 deletions(-)

diff --git a/src/main.rs b/src/main.rs
index d000fcd..7557711 100644
--- a/src/main.rs
+++ b/src/main.rs
@@ -349,7 +349,14 @@ fn check_user_presence(cid: ChannelID) -> Result<(), Ctap2StatusCode> {
 
         // Setup a keep-alive callback.
         let keepalive_expired = Cell::new(false);
-        let mut keepalive_callback = timer::with_callback(|_, _| {
+        let mut drivers = match libtock::retrieve_drivers() {
+            Ok(x) => x,
+            Err(err) => {
+                panic!("Drivers were already taken when attempting to retrieve them");
+            }
+        };
+        let mut timer_context = drivers.timer;
+        let mut keepalive_callback = timer_context.with_callback(|_, _| {
             keepalive_expired.set(true);
         });
         let mut keepalive = keepalive_callback.init().unwrap();
diff --git a/src/usb_ctap_hid.rs b/src/usb_ctap_hid.rs
index 36c5118..8e35151 100644
--- a/src/usb_ctap_hid.rs
+++ b/src/usb_ctap_hid.rs
@@ -17,10 +17,8 @@ use core::cell::Cell;
 use core::fmt::Write;
 #[cfg(feature = "debug_ctap")]
 use libtock::console::Console;
-//use libtock::result::TockValue;
 use libtock::result::{EALREADY, EBUSY, SUCCESS};
 use libtock::syscalls;
-use libtock::timer;
 use libtock::timer::{Duration};
 
 const DRIVER_NUMBER: usize = 0x20009;
@@ -238,7 +236,14 @@ fn recv_with_timeout_detail(
 
     // Setup a time-out callback.
     let timeout_expired = Cell::new(false);
-    let mut timeout_callback = timer::with_callback(|_, _| {
+    let mut drivers = match libtock::retrieve_drivers() {
+        Ok(x) => x,
+        Err(err) => {
+            panic!("Drivers were already taken when attempting to retrieve them");
+        }
+    };
+    let mut timer_context = drivers.timer;
+    let mut timeout_callback = timer_context.with_callback(|_, _| {
         timeout_expired.set(true);
     });
     let mut timeout = match timeout_callback.init() {
@@ -331,7 +336,14 @@ fn send_or_recv_with_timeout_detail(
 
     // Setup a time-out callback.
     let timeout_expired = Cell::new(false);
-    let mut timeout_callback = timer::with_callback(|_, _| {
+    let mut drivers = match libtock::retrieve_drivers() {
+        Ok(x) => x,
+        Err(err) => {
+            panic!("Drivers were already taken when attempting to retrieve them");
+        }
+    };
+    let mut timer_context = drivers.timer;
+    let mut timeout_callback = timer_context.with_callback(|_, _| {
         timeout_expired.set(true);
     });
     let mut timeout = match timeout_callback.init() {
@@ -355,17 +367,21 @@ fn send_or_recv_with_timeout_detail(
     // Cleanup alarm callback.
     match timeout.stop_alarm(timeout_alarm) {
         Ok(()) => (),
-        Err(TockValue::Expected(StopAlarmError::AlreadyDisabled)) => {
-            if !timeout_expired.get() {
-                #[cfg(feature = "debug_ctap")]
-                writeln!(
-                    Console::new(),
-                    "The send/receive timeout already expired, but the callback wasn't executed."
-                )
-                .unwrap();
+        Err(err) => {
+            match err.return_code {
+                EALREADY => {
+                    if !timeout_expired.get() {
+                        #[cfg(feature = "debug_ctap")]
+                        writeln!(
+                            Console::new(),
+                            "The send/receive timeout already expired, but the callback wasn't executed."
+                        )
+                            .unwrap();
+                    }
+                },
+                _ => panic!("Unexpected error when stopping alarm: {:?}", err.return_code),
             }
-        }
-        Err(e) => panic!("Unexpected error when stopping alarm: {:?}", e),
+        },
     }
 
     // Cancel USB transaction if necessary.
-- 
GitLab