diff --git a/src/main.rs b/src/main.rs index 697355cae55f84612a5a2364839a2afe1a615768..661ecf841dd9fcdff8fd383da2175de590c41689 100644 --- a/src/main.rs +++ b/src/main.rs @@ -43,7 +43,7 @@ use libtock::buttons::ButtonState; #[cfg(feature = "debug_ctap")] use libtock::console::Console; use libtock::leds::LedsDriver; -use libtock::result::TockResult; +use libtock::result::{TockResult, TockError}; use libtock::timer; #[cfg(feature = "debug_ctap")] use libtock::timer::Timer; @@ -373,9 +373,12 @@ fn check_user_presence(cid: ChannelID) -> Result<(), Ctap2StatusCode> { match keepalive.stop_alarm(keepalive_alarm) { Ok(()) => (), Err(err) => { - match err.return_code { - EALREADY => assert!(keepalive_expired.get()), - _ => panic!("Unexpected error when stopping alarm: {:?}", err.return_code), + match err { + TockError::Command(command_err) => match command_err.return_code { + EALREADY => assert!(keepalive_expired.get()), + _ => panic!("Unexpected error when stopping alarm: {:?}", command_err.return_code), + }, + _ => panic!("Unreachable"), } } } diff --git a/src/usb_ctap_hid.rs b/src/usb_ctap_hid.rs index 7006748a772924c7e3e58e2b844db0af895513d5..f72e3d9f7af09c82fcec7e491de5e3b9728dd930 100644 --- a/src/usb_ctap_hid.rs +++ b/src/usb_ctap_hid.rs @@ -17,7 +17,7 @@ use core::cell::Cell; use core::fmt::Write; #[cfg(feature = "debug_ctap")] use libtock::console::Console; -use libtock::result::{EALREADY, EBUSY, SUCCESS, TockResult}; +use libtock::result::{EALREADY, EBUSY, TockResult, TockError}; use libtock::syscalls; use libtock::timer::{Duration}; @@ -281,19 +281,22 @@ fn recv_with_timeout_detail( // Cleanup alarm callback. match timeout.stop_alarm(timeout_alarm) { Ok(()) => (), - Err(command_err) => { - match command_err.return_code { - EALREADY => { - if !timeout_expired.get() { - #[cfg(feature = "debug_ctap")] - writeln!( - Console::new(), - "The receive timeout already expired, but the callback wasn't executed." - ) - .unwrap(); - } + Err(err) => { + match err { + TockError::Command(command_err) => match command_err.return_code { + EALREADY => { + if !timeout_expired.get() { + #[cfg(feature = "debug_ctap")] + writeln!( + Console::new(), + "The receive timeout already expired, but the callback wasn't executed." + ) + .unwrap(); + } + }, + _ => panic!("Unexpected error when stopping alarm: {:?}", command_err.return_code), }, - _ => panic!("Unexpected error when stopping alarm: {:?}", command_err.return_code), + _ => panic!("Unreachable"), } } } @@ -302,23 +305,26 @@ fn recv_with_timeout_detail( if status.get().is_none() { #[cfg(feature = "verbose")] writeln!(Console::new(), "Cancelling USB receive due to timeout").unwrap(); - let result_code = unsafe { syscalls::command(DRIVER_NUMBER, command_nr::CANCEL, 0, 0) }; - match result_code { - // - SUCCESS means that we successfully cancelled the transaction. - // - EALREADY means that the transaction was already completed. - SUCCESS | EALREADY => (), - // - EBUSY means that the transaction is in progress. - EBUSY => { - // The app should wait for it, but it may never happen if the remote app crashes. - // We just return to avoid a deadlock. - #[cfg(feature = "debug_ctap")] - writeln!(Console::new(), "Couldn't cancel the USB receive").unwrap(); - } - _ => panic!( - "Unexpected error when cancelling USB receive: {:?}", - result_code - ), - } + let result = unsafe { syscalls::command(DRIVER_NUMBER, command_nr::CANCEL, 0, 0) }; + match result { + // - Ok = SUCCESS means that we successfully cancelled the transaction. + Ok(x) => (), + Err(err) => match err.return_code { + // - EALREADY means that the transaction was already completed. + EALREADY => (), + // - EBUSY means that the transaction is in progress. + EBUSY => { + // The app should wait for it, but it may never happen if the remote app crashes. + // We just return to avoid a deadlock. + #[cfg(feature = "debug_ctap")] + writeln!(Console::new(), "Couldn't cancel the USB receive").unwrap(); + } + _ => panic!( + "Unexpected error when cancelling USB receive: {:?}", + result_code + ), + }, + }; } status.get() @@ -386,18 +392,21 @@ fn send_or_recv_with_timeout_detail( match timeout.stop_alarm(timeout_alarm) { Ok(()) => (), 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(); - } + match err { + TockError::Command(command_err) => match command_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: {:?}", command_err.return_code), }, - _ => panic!("Unexpected error when stopping alarm: {:?}", err.return_code), + _ => panic!("Unreachable"), } }, } @@ -406,23 +415,27 @@ fn send_or_recv_with_timeout_detail( if status.get().is_none() { #[cfg(feature = "verbose")] writeln!(Console::new(), "Cancelling USB transaction due to timeout").unwrap(); - let result_code = unsafe { syscalls::command(DRIVER_NUMBER, command_nr::CANCEL, 0, 0) }; - match result_code { - // - SUCCESS means that we successfully cancelled the transaction. - // - EALREADY means that the transaction was already completed. - SUCCESS | EALREADY => (), - // - EBUSY means that the transaction is in progress. - EBUSY => { - // The app should wait for it, but it may never happen if the remote app crashes. - // We just return to avoid a deadlock. - #[cfg(feature = "debug_ctap")] - writeln!(Console::new(), "Couldn't cancel the transaction").unwrap(); - } - _ => panic!( - "Unexpected error when cancelling USB transaction: {:?}", - result_code - ), - } + let result = unsafe { syscalls::command(DRIVER_NUMBER, command_nr::CANCEL, 0, 0) }; + match result { + // - Ok = SUCCESS means that we successfully cancelled the transaction. + Ok(x) => (), + Err(err) => match err.return_code { + // - EALREADY means that the transaction was already completed. + EALREADY => (), + // - EBUSY means that the transaction is in progress. + EBUSY => { + // The app should wait for it, but it may never happen if the remote app crashes. + // We just return to avoid a deadlock. + #[cfg(feature = "debug_ctap")] + writeln!(Console::new(), "Couldn't cancel the transaction").unwrap(); + } + _ => panic!( + "Unexpected error when cancelling USB transaction: {:?}", + result_code + ), + }, + }; + #[cfg(feature = "debug_ctap")] writeln!(Console::new(), "Cancelled USB transaction!").unwrap(); }