From d36cac1a33e2d895305e7f0af27636d6d469c5f5 Mon Sep 17 00:00:00 2001 From: Mirna Date: Mon, 12 Oct 2020 11:07:57 +0200 Subject: [PATCH 01/19] Add NFC example application --- deploy.py | 7 +++++++ examples/nfct_test.rs | 20 ++++++++++++++++++++ 2 files changed, 27 insertions(+) create mode 100644 examples/nfct_test.rs diff --git a/deploy.py b/deploy.py index 5598797..b1a1de8 100755 --- a/deploy.py +++ b/deploy.py @@ -921,6 +921,13 @@ if __name__ == "__main__": const="console_test", help=("Compiles and installs the console_test example that tests the " "console driver with messages of various lengths.")) + apps_group.add_argument( + "--nfct_test", + dest="application", + action="store_const", + const="console_test", + help=("Compiles and installs the console_test example that tests the " + "console driver with messages of various lengths.")) main_parser.set_defaults(features=["with_ctap1"]) diff --git a/examples/nfct_test.rs b/examples/nfct_test.rs new file mode 100644 index 0000000..a27d7c5 --- /dev/null +++ b/examples/nfct_test.rs @@ -0,0 +1,20 @@ +extern crate lang_items; + +use libtock_drivers::console::Console; +use libtock_drivers::nfc; + +fn main(){ + writeln!(console, "****************************************").unwrap(); + writeln!(console, "nfct_test application is installed").unwrap(); + writeln!(console, "****************************************").unwrap(); + + // 1. Subscribe to a SELECTED CALLBACK + // 2. Configure Type 4 tag + // [_.] Enable Tag emulation (currently the tag is always activated) + // loop { + // 1. Allow Receive Buffer + // 2. Subscribe to RECEIVE CALLBACK + // 3. Allow TX buffer + // 4. Subscribe to TX CALLBACK + // } +} From 0f6eeff486642e26750fa364a488d25d63acd93b Mon Sep 17 00:00:00 2001 From: Mirna Date: Mon, 12 Oct 2020 11:08:33 +0200 Subject: [PATCH 02/19] Add NFC driver to third_party/libtock-drivers/ --- third_party/libtock-drivers/src/lib.rs | 1 + third_party/libtock-drivers/src/nfc.rs | 66 ++++++++++++++++++++++++++ 2 files changed, 67 insertions(+) create mode 100644 third_party/libtock-drivers/src/nfc.rs diff --git a/third_party/libtock-drivers/src/lib.rs b/third_party/libtock-drivers/src/lib.rs index bf62d05..e498a37 100644 --- a/third_party/libtock-drivers/src/lib.rs +++ b/third_party/libtock-drivers/src/lib.rs @@ -8,3 +8,4 @@ pub mod rng; pub mod timer; pub mod usb_ctap_hid; pub mod util; +pub mod nfc; diff --git a/third_party/libtock-drivers/src/nfc.rs b/third_party/libtock-drivers/src/nfc.rs new file mode 100644 index 0000000..ee2dda3 --- /dev/null +++ b/third_party/libtock-drivers/src/nfc.rs @@ -0,0 +1,66 @@ +use crate::util; +use core::cell::Cell; +use libtock_core::{callback, syscalls}; + +const DRIVER_NUMBER: usize = 0x30003; + +mod command_nr { + pub const TRANSMIT: usize = 1; + pub const RECEIVE: usize = 2; + pub const EMULATE: usize = 3; + pub const CONFIGURE: usize = 4; +} + +mod subscribe_nr { + pub const TRANSMIT: usize = 1; + pub const RECEIVE: usize = 2; + pub const SELECT: usize = 3; +} + +mod allow_nr { + pub const TRANSMIT: usize = 1; + pub const RECEIVE: usize = 2; +} + +pub const TX_BUFFER_SIZE: usize = 256; +pub const RX_BUFFER_SIZE: usize = 256; + +pub struct NfcTag { + tx_buffer: [u8; BUFFER_SIZE], + rx_buffer: [u8; BUFFER_SIZE], + tag_type: u8, +} + +impl NfcTag { + pub fn new() -> Console { + Console { + tx_buffer: [0; TX_BUFFER_SIZE], + tx_buffer: [0; RX_BUFFER_SIZE], + tag_type: 0, + } + } + + pub fn set_tag_type(&mut self, type: u8) { + self.tag_type = type; + } + + pub fn selected(&self) -> bool { + let is_selected = Cell::new(false); + let mut is_selected_alarm = || is_selected.set(true); + let subscription = syscalls::subscribe::( + DRIVER_NUMBER, + subscribe_nr::SELECT, + &mut is_selected_alarm, + ); + if subscription.is_err() { + return; + } + + util::yieldk_for(|| is_selected.get()); + true + } + + pub fn receive(&self) { + + } +} From 929aaf3ba24d4523d021a92fdf3fa33a743e936a Mon Sep 17 00:00:00 2001 From: Mirna Date: Mon, 12 Oct 2020 13:31:06 +0200 Subject: [PATCH 03/19] Update in libtock-drivers/ and nfct_test.rs application --- deploy.py | 6 +- examples/nfct_test.rs | 2 + third_party/libtock-drivers/src/nfc.rs | 139 ++++++++++++++++++------- 3 files changed, 105 insertions(+), 42 deletions(-) diff --git a/deploy.py b/deploy.py index b1a1de8..5cf210d 100755 --- a/deploy.py +++ b/deploy.py @@ -925,9 +925,9 @@ if __name__ == "__main__": "--nfct_test", dest="application", action="store_const", - const="console_test", - help=("Compiles and installs the console_test example that tests the " - "console driver with messages of various lengths.")) + const="nfct_test", + help=("Compiles and installs the nfct_test example that tests the " + "NFC driver.")) main_parser.set_defaults(features=["with_ctap1"]) diff --git a/examples/nfct_test.rs b/examples/nfct_test.rs index a27d7c5..900bcd7 100644 --- a/examples/nfct_test.rs +++ b/examples/nfct_test.rs @@ -4,6 +4,8 @@ use libtock_drivers::console::Console; use libtock_drivers::nfc; fn main(){ + let mut console = Console::new(); + writeln!(console, "****************************************").unwrap(); writeln!(console, "nfct_test application is installed").unwrap(); writeln!(console, "****************************************").unwrap(); diff --git a/third_party/libtock-drivers/src/nfc.rs b/third_party/libtock-drivers/src/nfc.rs index ee2dda3..9fa950c 100644 --- a/third_party/libtock-drivers/src/nfc.rs +++ b/third_party/libtock-drivers/src/nfc.rs @@ -22,45 +22,106 @@ mod allow_nr { pub const RECEIVE: usize = 2; } -pub const TX_BUFFER_SIZE: usize = 256; -pub const RX_BUFFER_SIZE: usize = 256; - -pub struct NfcTag { - tx_buffer: [u8; BUFFER_SIZE], - rx_buffer: [u8; BUFFER_SIZE], - tag_type: u8, +pub fn enable_emulation() { + emulate(true); } -impl NfcTag { - pub fn new() -> Console { - Console { - tx_buffer: [0; TX_BUFFER_SIZE], - tx_buffer: [0; RX_BUFFER_SIZE], - tag_type: 0, - } - } - - pub fn set_tag_type(&mut self, type: u8) { - self.tag_type = type; - } - - pub fn selected(&self) -> bool { - let is_selected = Cell::new(false); - let mut is_selected_alarm = || is_selected.set(true); - let subscription = syscalls::subscribe::( - DRIVER_NUMBER, - subscribe_nr::SELECT, - &mut is_selected_alarm, - ); - if subscription.is_err() { - return; - } - - util::yieldk_for(|| is_selected.get()); - true - } - - pub fn receive(&self) { - - } +pub fn disable_emulation() { + emulate(false); +} + +pub fn emulate(enabled: bool) -> bool { + let result_code = syscalls::command( + DRIVER_NUMBER, + command_nr::EMULATE, + enabled as usize, + 0 + ); + if result_code.is_err() { + return false; + } + + true +} + +pub fn selected() -> bool { + let is_selected = Cell::new(false); + let mut is_selected_alarm = || is_selected.set(true); + let subscription = syscalls::subscribe::( + DRIVER_NUMBER, + subscribe_nr::SELECT, + &mut is_selected_alarm, + ); + if subscription.is_err() { + return false; + } + + util::yieldk_for(|| is_selected.get()); + true +} + +pub fn configure(tag_type: u8) -> bool { + let result_code = syscalls::command( + DRIVER_NUMBER, + command_nr::CONFIGURE, + tag_type as usize, + 0 + ); + if result_code.is_err() { + return false; + } + + true +} + +pub fn receive(buf: &mut [u8]) -> bool { + let result = syscalls::allow(DRIVER_NUMBER, allow_nr::RECEIVE, buf); + if result.is_err() { + return false; + } + + let done = Cell::new(false); + let mut alarm = || done.set(true); + let subscription = syscalls::subscribe::( + DRIVER_NUMBER, + subscribe_nr::RECEIVE, + &mut alarm, + ); + if subscription.is_err() { + return false; + } + + let result_code = syscalls::command(DRIVER_NUMBER, command_nr::RECEIVE, 0, 0); + if result_code.is_err() { + return false; + } + + util::yieldk_for(|| done.get()); + true +} + +pub fn transmit(buf: &mut [u8]) -> bool { + let result = syscalls::allow(DRIVER_NUMBER, allow_nr::TRANSMIT, buf); + if result.is_err() { + return false; + } + + let done = Cell::new(false); + let mut alarm = || done.set(true); + let subscription = syscalls::subscribe::( + DRIVER_NUMBER, + subscribe_nr::TRANSMIT, + &mut alarm, + ); + if subscription.is_err() { + return false; + } + + let result_code = syscalls::command(DRIVER_NUMBER, command_nr::TRANSMIT, 0, 0); + if result_code.is_err() { + return false; + } + + util::yieldk_for(|| done.get()); + true } From 00ab3c8877a91f23ae72fb981063308adb5b52b7 Mon Sep 17 00:00:00 2001 From: Mirna Date: Mon, 12 Oct 2020 16:01:22 +0200 Subject: [PATCH 04/19] Fixes for some unsuccessful checks --- examples/nfct_test.rs | 14 ++++++++------ third_party/libtock-drivers/src/lib.rs | 2 +- third_party/libtock-drivers/src/nfc.rs | 14 ++------------ 3 files changed, 11 insertions(+), 19 deletions(-) diff --git a/examples/nfct_test.rs b/examples/nfct_test.rs index 900bcd7..1432dfd 100644 --- a/examples/nfct_test.rs +++ b/examples/nfct_test.rs @@ -1,9 +1,11 @@ +extern crate alloc; extern crate lang_items; +use core::fmt::Write; use libtock_drivers::console::Console; -use libtock_drivers::nfc; +// use libtock_drivers::nfc; -fn main(){ +fn main() { let mut console = Console::new(); writeln!(console, "****************************************").unwrap(); @@ -14,9 +16,9 @@ fn main(){ // 2. Configure Type 4 tag // [_.] Enable Tag emulation (currently the tag is always activated) // loop { - // 1. Allow Receive Buffer - // 2. Subscribe to RECEIVE CALLBACK - // 3. Allow TX buffer - // 4. Subscribe to TX CALLBACK + // 1. Allow Receive Buffer + // 2. Subscribe to RECEIVE CALLBACK + // 3. Allow TX buffer + // 4. Subscribe to TX CALLBACK // } } diff --git a/third_party/libtock-drivers/src/lib.rs b/third_party/libtock-drivers/src/lib.rs index e498a37..ec8a2d7 100644 --- a/third_party/libtock-drivers/src/lib.rs +++ b/third_party/libtock-drivers/src/lib.rs @@ -3,9 +3,9 @@ pub mod buttons; pub mod console; pub mod led; +pub mod nfc; pub mod result; pub mod rng; pub mod timer; pub mod usb_ctap_hid; pub mod util; -pub mod nfc; diff --git a/third_party/libtock-drivers/src/nfc.rs b/third_party/libtock-drivers/src/nfc.rs index 9fa950c..977b49b 100644 --- a/third_party/libtock-drivers/src/nfc.rs +++ b/third_party/libtock-drivers/src/nfc.rs @@ -31,12 +31,7 @@ pub fn disable_emulation() { } pub fn emulate(enabled: bool) -> bool { - let result_code = syscalls::command( - DRIVER_NUMBER, - command_nr::EMULATE, - enabled as usize, - 0 - ); + let result_code = syscalls::command(DRIVER_NUMBER, command_nr::EMULATE, enabled as usize, 0); if result_code.is_err() { return false; } @@ -61,12 +56,7 @@ pub fn selected() -> bool { } pub fn configure(tag_type: u8) -> bool { - let result_code = syscalls::command( - DRIVER_NUMBER, - command_nr::CONFIGURE, - tag_type as usize, - 0 - ); + let result_code = syscalls::command(DRIVER_NUMBER, command_nr::CONFIGURE, tag_type as usize, 0); if result_code.is_err() { return false; } From 9205974b509937abd0f587c80736a2807533fa18 Mon Sep 17 00:00:00 2001 From: Mirna Date: Thu, 15 Oct 2020 19:54:34 +0200 Subject: [PATCH 05/19] Update nfct_test example to test NFC driver. --- examples/nfct_test.rs | 67 ++++++++++++++++++++++++++++++++++++------- 1 file changed, 56 insertions(+), 11 deletions(-) diff --git a/examples/nfct_test.rs b/examples/nfct_test.rs index 1432dfd..42176cd 100644 --- a/examples/nfct_test.rs +++ b/examples/nfct_test.rs @@ -1,24 +1,69 @@ +#![no_std] + extern crate alloc; extern crate lang_items; use core::fmt::Write; use libtock_drivers::console::Console; -// use libtock_drivers::nfc; +use libtock_drivers::nfc::NfcTag; + +#[allow(dead_code)] +/// Helper function to write a slice into a fixed +/// length transmission buffer. +fn write_tx_buffer(buf: &mut [u8], slice: &[u8]) { + for (i, &byte) in slice.iter().enumerate() { + buf[i] = byte; + } +} fn main() { let mut console = Console::new(); writeln!(console, "****************************************").unwrap(); writeln!(console, "nfct_test application is installed").unwrap(); - writeln!(console, "****************************************").unwrap(); - // 1. Subscribe to a SELECTED CALLBACK - // 2. Configure Type 4 tag - // [_.] Enable Tag emulation (currently the tag is always activated) - // loop { - // 1. Allow Receive Buffer - // 2. Subscribe to RECEIVE CALLBACK - // 3. Allow TX buffer - // 4. Subscribe to TX CALLBACK - // } + // 1. Configure Type 4 tag + if NfcTag::configure(4) { + writeln!(console, " -- TAG CONFIGURED").unwrap(); + } + // 2. Subscribe to a SELECTED CALLBACK + if NfcTag::selected() { + writeln!(console, " -- TAG SELECTED").unwrap(); + // 0xfffff results in 1048575 / 13.56e6 = 77ms + NfcTag::set_framedelaymax(0xfffff); + } + /* + [_.] TODO: Enable Tag emulation (currently the tag is always activated) + needs field detection support in the driver level. + */ + let mut rx_buf = [0; 64]; + let mut unknown_cmd_cntr = 0; + loop { + NfcTag::receive(&mut rx_buf); + match rx_buf[0] { + 0xe0 /* RATS */=> { + let mut answer_to_select = [0x05, 0x78, 0x80, 0xB1, 0x00]; + let amount = answer_to_select.len(); + NfcTag::transmit(&mut answer_to_select, amount); + } + 0xc2 /* DESELECT */ => { + // Ignore the request + let mut command_error = [0x6A, 0x81]; + let amount = command_error.len(); + NfcTag::transmit(&mut command_error, amount); + } + 0x02 | 0x03 /* APDU Prefix */ => { + let mut reply = [rx_buf[0], 0x90, 0x00]; + let amount = reply.len(); + NfcTag::transmit(&mut reply, amount); + } + _ => { + unknown_cmd_cntr += 1; + } + } + if unknown_cmd_cntr > 50 { + break; + } + } + writeln!(console, "****************************************").unwrap(); } From f09a9b4fae4b2f79a1d5575659f3dd7d0df00048 Mon Sep 17 00:00:00 2001 From: Mirna Date: Thu, 15 Oct 2020 19:55:39 +0200 Subject: [PATCH 06/19] Update libtock-drivers/ NFC driver --- third_party/libtock-drivers/src/nfc.rs | 211 ++++++++++++++----------- 1 file changed, 119 insertions(+), 92 deletions(-) diff --git a/third_party/libtock-drivers/src/nfc.rs b/third_party/libtock-drivers/src/nfc.rs index 977b49b..8a0c7c6 100644 --- a/third_party/libtock-drivers/src/nfc.rs +++ b/third_party/libtock-drivers/src/nfc.rs @@ -9,6 +9,7 @@ mod command_nr { pub const RECEIVE: usize = 2; pub const EMULATE: usize = 3; pub const CONFIGURE: usize = 4; + pub const FRAMEDELAYMAX: usize = 5; } mod subscribe_nr { @@ -22,96 +23,122 @@ mod allow_nr { pub const RECEIVE: usize = 2; } -pub fn enable_emulation() { - emulate(true); -} - -pub fn disable_emulation() { - emulate(false); -} - -pub fn emulate(enabled: bool) -> bool { - let result_code = syscalls::command(DRIVER_NUMBER, command_nr::EMULATE, enabled as usize, 0); - if result_code.is_err() { - return false; - } - - true -} - -pub fn selected() -> bool { - let is_selected = Cell::new(false); - let mut is_selected_alarm = || is_selected.set(true); - let subscription = syscalls::subscribe::( - DRIVER_NUMBER, - subscribe_nr::SELECT, - &mut is_selected_alarm, - ); - if subscription.is_err() { - return false; - } - - util::yieldk_for(|| is_selected.get()); - true -} - -pub fn configure(tag_type: u8) -> bool { - let result_code = syscalls::command(DRIVER_NUMBER, command_nr::CONFIGURE, tag_type as usize, 0); - if result_code.is_err() { - return false; - } - - true -} - -pub fn receive(buf: &mut [u8]) -> bool { - let result = syscalls::allow(DRIVER_NUMBER, allow_nr::RECEIVE, buf); - if result.is_err() { - return false; - } - - let done = Cell::new(false); - let mut alarm = || done.set(true); - let subscription = syscalls::subscribe::( - DRIVER_NUMBER, - subscribe_nr::RECEIVE, - &mut alarm, - ); - if subscription.is_err() { - return false; - } - - let result_code = syscalls::command(DRIVER_NUMBER, command_nr::RECEIVE, 0, 0); - if result_code.is_err() { - return false; - } - - util::yieldk_for(|| done.get()); - true -} - -pub fn transmit(buf: &mut [u8]) -> bool { - let result = syscalls::allow(DRIVER_NUMBER, allow_nr::TRANSMIT, buf); - if result.is_err() { - return false; - } - - let done = Cell::new(false); - let mut alarm = || done.set(true); - let subscription = syscalls::subscribe::( - DRIVER_NUMBER, - subscribe_nr::TRANSMIT, - &mut alarm, - ); - if subscription.is_err() { - return false; - } - - let result_code = syscalls::command(DRIVER_NUMBER, command_nr::TRANSMIT, 0, 0); - if result_code.is_err() { - return false; - } - - util::yieldk_for(|| done.get()); - true +pub struct NfcTag {} + +impl NfcTag { + pub fn enable_emulation() { + NfcTag::emulate(true); + } + + pub fn disable_emulation() { + NfcTag::emulate(false); + } + + pub fn emulate(enabled: bool) -> bool { + let result_code = + syscalls::command(DRIVER_NUMBER, command_nr::EMULATE, enabled as usize, 0); + if result_code.is_err() { + return false; + } + + true + } + + /// Subscribe to the tag being SELECTED callback. + pub fn selected() -> bool { + let is_selected = Cell::new(false); + let mut is_selected_alarm = || is_selected.set(true); + let subscription = syscalls::subscribe::( + DRIVER_NUMBER, + subscribe_nr::SELECT, + &mut is_selected_alarm, + ); + if subscription.is_err() { + return false; + } + + util::yieldk_for(|| is_selected.get()); + true + } + + /// Configure the tag type command. + pub fn configure(tag_type: u8) -> bool { + let result_code = + syscalls::command(DRIVER_NUMBER, command_nr::CONFIGURE, tag_type as usize, 0); + if result_code.is_err() { + return false; + } + + true + } + + /// Set the maximum frame delay value to support + /// transmission with the reader. + pub fn set_framedelaymax(delay: u32) -> bool { + let result_code = + syscalls::command(DRIVER_NUMBER, command_nr::FRAMEDELAYMAX, delay as usize, 0); + if result_code.is_err() { + return false; + } + + true + } + + /// 1. Share with the driver a buffer. + /// 2. Subscribe to having a successful receive callback. + /// 3. Issue the request for reception. + pub fn receive(buf: &mut [u8]) -> bool { + let result = syscalls::allow(DRIVER_NUMBER, allow_nr::RECEIVE, buf); + if result.is_err() { + return false; + } + + let done = Cell::new(false); + let mut alarm = || done.set(true); + let subscription = syscalls::subscribe::( + DRIVER_NUMBER, + subscribe_nr::RECEIVE, + &mut alarm, + ); + if subscription.is_err() { + return false; + } + + let result_code = syscalls::command(DRIVER_NUMBER, command_nr::RECEIVE, 0, 0); + if result_code.is_err() { + return false; + } + + util::yieldk_for(|| done.get()); + true + } + + /// 1. Share with the driver a buffer containing the app's reply. + /// 2. Subscribe to having a successful transmission callback. + /// 3. Issue the request for transmitting. + pub fn transmit(buf: &mut [u8], amount: usize) -> bool { + let result = syscalls::allow(DRIVER_NUMBER, allow_nr::TRANSMIT, buf); + if result.is_err() { + return false; + } + + let done = Cell::new(false); + let mut alarm = || done.set(true); + let subscription = syscalls::subscribe::( + DRIVER_NUMBER, + subscribe_nr::TRANSMIT, + &mut alarm, + ); + if subscription.is_err() { + return false; + } + + let result_code = syscalls::command(DRIVER_NUMBER, command_nr::TRANSMIT, amount, 0); + if result_code.is_err() { + return false; + } + + util::yieldk_for(|| done.get()); + true + } } From 6b2fde242d45dcb2b1ed806f4b34690c24179f4b Mon Sep 17 00:00:00 2001 From: Mirna <29131616+MirnaMuhammad98@users.noreply.github.com> Date: Fri, 16 Oct 2020 08:22:40 +0200 Subject: [PATCH 07/19] Delete nfct_test.rs This branch is for the driver interface. --- examples/nfct_test.rs | 69 ------------------------------------------- 1 file changed, 69 deletions(-) delete mode 100644 examples/nfct_test.rs diff --git a/examples/nfct_test.rs b/examples/nfct_test.rs deleted file mode 100644 index 42176cd..0000000 --- a/examples/nfct_test.rs +++ /dev/null @@ -1,69 +0,0 @@ -#![no_std] - -extern crate alloc; -extern crate lang_items; - -use core::fmt::Write; -use libtock_drivers::console::Console; -use libtock_drivers::nfc::NfcTag; - -#[allow(dead_code)] -/// Helper function to write a slice into a fixed -/// length transmission buffer. -fn write_tx_buffer(buf: &mut [u8], slice: &[u8]) { - for (i, &byte) in slice.iter().enumerate() { - buf[i] = byte; - } -} - -fn main() { - let mut console = Console::new(); - - writeln!(console, "****************************************").unwrap(); - writeln!(console, "nfct_test application is installed").unwrap(); - - // 1. Configure Type 4 tag - if NfcTag::configure(4) { - writeln!(console, " -- TAG CONFIGURED").unwrap(); - } - // 2. Subscribe to a SELECTED CALLBACK - if NfcTag::selected() { - writeln!(console, " -- TAG SELECTED").unwrap(); - // 0xfffff results in 1048575 / 13.56e6 = 77ms - NfcTag::set_framedelaymax(0xfffff); - } - /* - [_.] TODO: Enable Tag emulation (currently the tag is always activated) - needs field detection support in the driver level. - */ - let mut rx_buf = [0; 64]; - let mut unknown_cmd_cntr = 0; - loop { - NfcTag::receive(&mut rx_buf); - match rx_buf[0] { - 0xe0 /* RATS */=> { - let mut answer_to_select = [0x05, 0x78, 0x80, 0xB1, 0x00]; - let amount = answer_to_select.len(); - NfcTag::transmit(&mut answer_to_select, amount); - } - 0xc2 /* DESELECT */ => { - // Ignore the request - let mut command_error = [0x6A, 0x81]; - let amount = command_error.len(); - NfcTag::transmit(&mut command_error, amount); - } - 0x02 | 0x03 /* APDU Prefix */ => { - let mut reply = [rx_buf[0], 0x90, 0x00]; - let amount = reply.len(); - NfcTag::transmit(&mut reply, amount); - } - _ => { - unknown_cmd_cntr += 1; - } - } - if unknown_cmd_cntr > 50 { - break; - } - } - writeln!(console, "****************************************").unwrap(); -} From 7e0a48ae1cbddd8b67937c0cde9ecaf18217e92b Mon Sep 17 00:00:00 2001 From: Mirna Date: Wed, 21 Oct 2020 10:39:17 +0200 Subject: [PATCH 08/19] Resolved Review Comments --- deploy.py | 7 --- third_party/libtock-drivers/src/nfc.rs | 72 ++++++++++++-------------- 2 files changed, 32 insertions(+), 47 deletions(-) diff --git a/deploy.py b/deploy.py index 5cf210d..5598797 100755 --- a/deploy.py +++ b/deploy.py @@ -921,13 +921,6 @@ if __name__ == "__main__": const="console_test", help=("Compiles and installs the console_test example that tests the " "console driver with messages of various lengths.")) - apps_group.add_argument( - "--nfct_test", - dest="application", - action="store_const", - const="nfct_test", - help=("Compiles and installs the nfct_test example that tests the " - "NFC driver.")) main_parser.set_defaults(features=["with_ctap1"]) diff --git a/third_party/libtock-drivers/src/nfc.rs b/third_party/libtock-drivers/src/nfc.rs index 8a0c7c6..8e90b6b 100644 --- a/third_party/libtock-drivers/src/nfc.rs +++ b/third_party/libtock-drivers/src/nfc.rs @@ -1,5 +1,6 @@ use crate::util; use core::cell::Cell; +use libtock_core::result::{ENOMEM, SUCCESS}; use libtock_core::{callback, syscalls}; const DRIVER_NUMBER: usize = 0x30003; @@ -23,25 +24,26 @@ mod allow_nr { pub const RECEIVE: usize = 2; } +pub enum TransOrRecvStatus { + Error, + InvalidBuffer, + OOM, + Success, +} + pub struct NfcTag {} impl NfcTag { - pub fn enable_emulation() { - NfcTag::emulate(true); + pub fn enable_emulation() -> bool { + NfcTag::emulate(true) } - pub fn disable_emulation() { - NfcTag::emulate(false); + pub fn disable_emulation() -> bool { + NfcTag::emulate(false) } - pub fn emulate(enabled: bool) -> bool { - let result_code = - syscalls::command(DRIVER_NUMBER, command_nr::EMULATE, enabled as usize, 0); - if result_code.is_err() { - return false; - } - - true + fn emulate(enabled: bool) -> bool { + syscalls::command(DRIVER_NUMBER, command_nr::EMULATE, enabled as usize, 0).is_ok() } /// Subscribe to the tag being SELECTED callback. @@ -63,34 +65,21 @@ impl NfcTag { /// Configure the tag type command. pub fn configure(tag_type: u8) -> bool { - let result_code = - syscalls::command(DRIVER_NUMBER, command_nr::CONFIGURE, tag_type as usize, 0); - if result_code.is_err() { - return false; - } - - true + syscalls::command(DRIVER_NUMBER, command_nr::CONFIGURE, tag_type as usize, 0).is_ok() } - /// Set the maximum frame delay value to support - /// transmission with the reader. + /// Set the maximum frame delay value to support transmission with the reader. pub fn set_framedelaymax(delay: u32) -> bool { - let result_code = - syscalls::command(DRIVER_NUMBER, command_nr::FRAMEDELAYMAX, delay as usize, 0); - if result_code.is_err() { - return false; - } - - true + syscalls::command(DRIVER_NUMBER, command_nr::FRAMEDELAYMAX, delay as usize, 0).is_ok() } /// 1. Share with the driver a buffer. /// 2. Subscribe to having a successful receive callback. /// 3. Issue the request for reception. - pub fn receive(buf: &mut [u8]) -> bool { + pub fn receive(buf: &mut [u8; 256]) -> TransOrRecvStatus { let result = syscalls::allow(DRIVER_NUMBER, allow_nr::RECEIVE, buf); if result.is_err() { - return false; + return TransOrRecvStatus::InvalidBuffer; } let done = Cell::new(false); @@ -101,25 +90,28 @@ impl NfcTag { &mut alarm, ); if subscription.is_err() { - return false; + return TransOrRecvStatus::Error; } - let result_code = syscalls::command(DRIVER_NUMBER, command_nr::RECEIVE, 0, 0); - if result_code.is_err() { - return false; + let result_code = + unsafe { syscalls::raw::command(DRIVER_NUMBER, command_nr::RECEIVE, 0, 0) }; + match result_code { + SUCCESS => (), + ENOMEM => return TransOrRecvStatus::OOM, + _ => return TransOrRecvStatus::Error, } util::yieldk_for(|| done.get()); - true + TransOrRecvStatus::Success } /// 1. Share with the driver a buffer containing the app's reply. /// 2. Subscribe to having a successful transmission callback. /// 3. Issue the request for transmitting. - pub fn transmit(buf: &mut [u8], amount: usize) -> bool { + pub fn transmit(buf: &mut [u8], amount: usize) -> TransOrRecvStatus { let result = syscalls::allow(DRIVER_NUMBER, allow_nr::TRANSMIT, buf); if result.is_err() { - return false; + return TransOrRecvStatus::InvalidBuffer; } let done = Cell::new(false); @@ -130,15 +122,15 @@ impl NfcTag { &mut alarm, ); if subscription.is_err() { - return false; + return TransOrRecvStatus::Error; } let result_code = syscalls::command(DRIVER_NUMBER, command_nr::TRANSMIT, amount, 0); if result_code.is_err() { - return false; + return TransOrRecvStatus::Error; } util::yieldk_for(|| done.get()); - true + TransOrRecvStatus::Success } } From 8cb85563da781aa8a3e0f9c57269fe03a96a4f9a Mon Sep 17 00:00:00 2001 From: Mirna Date: Wed, 21 Oct 2020 19:30:58 +0200 Subject: [PATCH 09/19] Updated Functions Return Types --- third_party/libtock-drivers/src/nfc.rs | 90 ++++++++++++-------------- 1 file changed, 41 insertions(+), 49 deletions(-) diff --git a/third_party/libtock-drivers/src/nfc.rs b/third_party/libtock-drivers/src/nfc.rs index 8e90b6b..6bf685b 100644 --- a/third_party/libtock-drivers/src/nfc.rs +++ b/third_party/libtock-drivers/src/nfc.rs @@ -1,6 +1,7 @@ +use crate::result::TockError; use crate::util; use core::cell::Cell; -use libtock_core::result::{ENOMEM, SUCCESS}; +use core::mem; use libtock_core::{callback, syscalls}; const DRIVER_NUMBER: usize = 0x30003; @@ -24,11 +25,10 @@ mod allow_nr { pub const RECEIVE: usize = 2; } -pub enum TransOrRecvStatus { - Error, - InvalidBuffer, - OOM, - Success, +#[allow(dead_code)] +pub struct RecvOp { + pub result_code: usize, + pub recv_amount: usize, } pub struct NfcTag {} @@ -76,61 +76,53 @@ impl NfcTag { /// 1. Share with the driver a buffer. /// 2. Subscribe to having a successful receive callback. /// 3. Issue the request for reception. - pub fn receive(buf: &mut [u8; 256]) -> TransOrRecvStatus { - let result = syscalls::allow(DRIVER_NUMBER, allow_nr::RECEIVE, buf); - if result.is_err() { - return TransOrRecvStatus::InvalidBuffer; - } - + pub fn receive(buf: &mut [u8; 256]) -> Result { + let result = syscalls::allow(DRIVER_NUMBER, allow_nr::RECEIVE, buf)?; + // set callback with 2 arguments, to receive ReturnCode and RX Amount let done = Cell::new(false); - let mut alarm = || done.set(true); - let subscription = syscalls::subscribe::( + let result_code = Cell::new(None); + let recv_amount = Cell::new(None); + let mut callback = |result, amount| { + result_code.set(Some(result)); + recv_amount.set(Some(amount)); + done.set(true) + }; + let subscription = syscalls::subscribe::( DRIVER_NUMBER, subscribe_nr::RECEIVE, - &mut alarm, - ); - if subscription.is_err() { - return TransOrRecvStatus::Error; - } - - let result_code = - unsafe { syscalls::raw::command(DRIVER_NUMBER, command_nr::RECEIVE, 0, 0) }; - match result_code { - SUCCESS => (), - ENOMEM => return TransOrRecvStatus::OOM, - _ => return TransOrRecvStatus::Error, - } - + &mut callback, + )?; + syscalls::command(DRIVER_NUMBER, command_nr::RECEIVE, 0, 0)?; util::yieldk_for(|| done.get()); - TransOrRecvStatus::Success + mem::drop(subscription); + mem::drop(result); + Ok(RecvOp { + result_code: result_code.get().unwrap(), + recv_amount: recv_amount.get().unwrap(), + }) } /// 1. Share with the driver a buffer containing the app's reply. /// 2. Subscribe to having a successful transmission callback. /// 3. Issue the request for transmitting. - pub fn transmit(buf: &mut [u8], amount: usize) -> TransOrRecvStatus { - let result = syscalls::allow(DRIVER_NUMBER, allow_nr::TRANSMIT, buf); - if result.is_err() { - return TransOrRecvStatus::InvalidBuffer; - } - + pub fn transmit(buf: &mut [u8], amount: usize) -> Result { + let result = syscalls::allow(DRIVER_NUMBER, allow_nr::TRANSMIT, buf)?; + // set callback with 1 argument, to receive ReturnCode let done = Cell::new(false); - let mut alarm = || done.set(true); - let subscription = syscalls::subscribe::( + let result_code = Cell::new(None); + let mut callback = |result| { + result_code.set(Some(result)); + done.set(true) + }; + let subscription = syscalls::subscribe::( DRIVER_NUMBER, subscribe_nr::TRANSMIT, - &mut alarm, - ); - if subscription.is_err() { - return TransOrRecvStatus::Error; - } - - let result_code = syscalls::command(DRIVER_NUMBER, command_nr::TRANSMIT, amount, 0); - if result_code.is_err() { - return TransOrRecvStatus::Error; - } - + &mut callback, + )?; + syscalls::command(DRIVER_NUMBER, command_nr::TRANSMIT, amount, 0)?; util::yieldk_for(|| done.get()); - TransOrRecvStatus::Success + mem::drop(subscription); + mem::drop(result); + Ok(result_code.get().unwrap()) } } From 59af769c6962ac154197546044c3523c54835a1e Mon Sep 17 00:00:00 2001 From: Mirna Date: Thu, 22 Oct 2020 12:10:43 +0200 Subject: [PATCH 10/19] Remove unnecessary `done.get()` --- third_party/libtock-drivers/src/nfc.rs | 14 ++++---------- 1 file changed, 4 insertions(+), 10 deletions(-) diff --git a/third_party/libtock-drivers/src/nfc.rs b/third_party/libtock-drivers/src/nfc.rs index 6bf685b..e18e283 100644 --- a/third_party/libtock-drivers/src/nfc.rs +++ b/third_party/libtock-drivers/src/nfc.rs @@ -79,13 +79,11 @@ impl NfcTag { pub fn receive(buf: &mut [u8; 256]) -> Result { let result = syscalls::allow(DRIVER_NUMBER, allow_nr::RECEIVE, buf)?; // set callback with 2 arguments, to receive ReturnCode and RX Amount - let done = Cell::new(false); let result_code = Cell::new(None); let recv_amount = Cell::new(None); let mut callback = |result, amount| { result_code.set(Some(result)); - recv_amount.set(Some(amount)); - done.set(true) + recv_amount.set(Some(amount)) }; let subscription = syscalls::subscribe::( DRIVER_NUMBER, @@ -93,7 +91,7 @@ impl NfcTag { &mut callback, )?; syscalls::command(DRIVER_NUMBER, command_nr::RECEIVE, 0, 0)?; - util::yieldk_for(|| done.get()); + util::yieldk_for(|| recv_amount.get().is_some()); mem::drop(subscription); mem::drop(result); Ok(RecvOp { @@ -108,19 +106,15 @@ impl NfcTag { pub fn transmit(buf: &mut [u8], amount: usize) -> Result { let result = syscalls::allow(DRIVER_NUMBER, allow_nr::TRANSMIT, buf)?; // set callback with 1 argument, to receive ReturnCode - let done = Cell::new(false); let result_code = Cell::new(None); - let mut callback = |result| { - result_code.set(Some(result)); - done.set(true) - }; + let mut callback = |result| result_code.set(Some(result)); let subscription = syscalls::subscribe::( DRIVER_NUMBER, subscribe_nr::TRANSMIT, &mut callback, )?; syscalls::command(DRIVER_NUMBER, command_nr::TRANSMIT, amount, 0)?; - util::yieldk_for(|| done.get()); + util::yieldk_for(|| result_code.get().is_some()); mem::drop(subscription); mem::drop(result); Ok(result_code.get().unwrap()) From 18c6406031b3c4b217990886ed7338ecfdbe7cd4 Mon Sep 17 00:00:00 2001 From: Mirna Date: Thu, 22 Oct 2020 15:10:36 +0200 Subject: [PATCH 11/19] Use return type `TockResult` --- third_party/libtock-drivers/src/nfc.rs | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/third_party/libtock-drivers/src/nfc.rs b/third_party/libtock-drivers/src/nfc.rs index e18e283..06dd748 100644 --- a/third_party/libtock-drivers/src/nfc.rs +++ b/third_party/libtock-drivers/src/nfc.rs @@ -1,4 +1,4 @@ -use crate::result::TockError; +use crate::result::TockResult; use crate::util; use core::cell::Cell; use core::mem; @@ -76,7 +76,7 @@ impl NfcTag { /// 1. Share with the driver a buffer. /// 2. Subscribe to having a successful receive callback. /// 3. Issue the request for reception. - pub fn receive(buf: &mut [u8; 256]) -> Result { + pub fn receive(buf: &mut [u8; 256]) -> TockResult { let result = syscalls::allow(DRIVER_NUMBER, allow_nr::RECEIVE, buf)?; // set callback with 2 arguments, to receive ReturnCode and RX Amount let result_code = Cell::new(None); @@ -103,7 +103,7 @@ impl NfcTag { /// 1. Share with the driver a buffer containing the app's reply. /// 2. Subscribe to having a successful transmission callback. /// 3. Issue the request for transmitting. - pub fn transmit(buf: &mut [u8], amount: usize) -> Result { + pub fn transmit(buf: &mut [u8], amount: usize) -> TockResult { let result = syscalls::allow(DRIVER_NUMBER, allow_nr::TRANSMIT, buf)?; // set callback with 1 argument, to receive ReturnCode let result_code = Cell::new(None); From b95b0682f9893de999e25ac978907a9229a5ac7c Mon Sep 17 00:00:00 2001 From: Mirna Date: Fri, 23 Oct 2020 19:25:36 +0200 Subject: [PATCH 12/19] Added NFC compile flag --- Cargo.toml | 1 + deploy.py | 7 +++++++ 2 files changed, 8 insertions(+) diff --git a/Cargo.toml b/Cargo.toml index a11d0f7..00bd7d2 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -28,6 +28,7 @@ ram_storage = [] verbose = ["debug_ctap", "libtock_drivers/verbose_usb"] with_ctap1 = ["crypto/with_ctap1"] with_ctap2_1 = [] +with_nfc = [] [dev-dependencies] elf2tab = "0.6.0" diff --git a/deploy.py b/deploy.py index 5598797..358355d 100755 --- a/deploy.py +++ b/deploy.py @@ -846,6 +846,13 @@ if __name__ == "__main__": help=("Compiles the OpenSK application with backward compatible " "support for CTAP2.1 protocol."), ) + main_parser.add_argument( + "--nfc", + action="append_const", + const="with_nfc", + dest="features", + help=("Compiles the OpenSK application with support for nfc."), + ) main_parser.add_argument( "--regen-keys", action="store_true", From a53569007d115252c6afea9ca014cb400974dade Mon Sep 17 00:00:00 2001 From: Mirna Date: Mon, 26 Oct 2020 12:56:53 +0200 Subject: [PATCH 13/19] Hide NFC behind feature flag --- third_party/libtock-drivers/src/lib.rs | 1 + 1 file changed, 1 insertion(+) diff --git a/third_party/libtock-drivers/src/lib.rs b/third_party/libtock-drivers/src/lib.rs index ec8a2d7..8b8983c 100644 --- a/third_party/libtock-drivers/src/lib.rs +++ b/third_party/libtock-drivers/src/lib.rs @@ -3,6 +3,7 @@ pub mod buttons; pub mod console; pub mod led; +#[cfg(feature = "with_nfc")] pub mod nfc; pub mod result; pub mod rng; From e37f63396c625d556b10a475e082d61f7638c3af Mon Sep 17 00:00:00 2001 From: Mirna Date: Mon, 26 Oct 2020 14:01:27 +0200 Subject: [PATCH 14/19] Revert #cfg change --- third_party/libtock-drivers/src/lib.rs | 1 - 1 file changed, 1 deletion(-) diff --git a/third_party/libtock-drivers/src/lib.rs b/third_party/libtock-drivers/src/lib.rs index 8b8983c..ec8a2d7 100644 --- a/third_party/libtock-drivers/src/lib.rs +++ b/third_party/libtock-drivers/src/lib.rs @@ -3,7 +3,6 @@ pub mod buttons; pub mod console; pub mod led; -#[cfg(feature = "with_nfc")] pub mod nfc; pub mod result; pub mod rng; From 64c66b91f526ee7e501d730687fa8b1d1378eeb7 Mon Sep 17 00:00:00 2001 From: Mirna Date: Tue, 27 Oct 2020 10:05:06 +0200 Subject: [PATCH 15/19] Fixed NFC feature flag --- Cargo.toml | 2 +- third_party/libtock-drivers/Cargo.toml | 1 + third_party/libtock-drivers/src/lib.rs | 1 + 3 files changed, 3 insertions(+), 1 deletion(-) diff --git a/Cargo.toml b/Cargo.toml index 00bd7d2..8faf8dd 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -28,7 +28,7 @@ ram_storage = [] verbose = ["debug_ctap", "libtock_drivers/verbose_usb"] with_ctap1 = ["crypto/with_ctap1"] with_ctap2_1 = [] -with_nfc = [] +with_nfc = ["libtock_drivers/with_nfc"] [dev-dependencies] elf2tab = "0.6.0" diff --git a/third_party/libtock-drivers/Cargo.toml b/third_party/libtock-drivers/Cargo.toml index 74fe7ac..41e5c32 100644 --- a/third_party/libtock-drivers/Cargo.toml +++ b/third_party/libtock-drivers/Cargo.toml @@ -14,3 +14,4 @@ libtock_core = { path = "../../third_party/libtock-rs/core" } [features] debug_ctap = [] verbose_usb = ["debug_ctap"] +with_nfc=[] diff --git a/third_party/libtock-drivers/src/lib.rs b/third_party/libtock-drivers/src/lib.rs index ec8a2d7..8b8983c 100644 --- a/third_party/libtock-drivers/src/lib.rs +++ b/third_party/libtock-drivers/src/lib.rs @@ -3,6 +3,7 @@ pub mod buttons; pub mod console; pub mod led; +#[cfg(feature = "with_nfc")] pub mod nfc; pub mod result; pub mod rng; From 31449fcc0df6cdeb54083b6ebed45bafd7a1d957 Mon Sep 17 00:00:00 2001 From: Mirna Date: Tue, 27 Oct 2020 10:06:11 +0200 Subject: [PATCH 16/19] Added setup function for driver existence checking --- third_party/libtock-drivers/src/nfc.rs | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/third_party/libtock-drivers/src/nfc.rs b/third_party/libtock-drivers/src/nfc.rs index 06dd748..5902160 100644 --- a/third_party/libtock-drivers/src/nfc.rs +++ b/third_party/libtock-drivers/src/nfc.rs @@ -7,6 +7,7 @@ use libtock_core::{callback, syscalls}; const DRIVER_NUMBER: usize = 0x30003; mod command_nr { + pub const CHECK: usize = 0; pub const TRANSMIT: usize = 1; pub const RECEIVE: usize = 2; pub const EMULATE: usize = 3; @@ -34,6 +35,11 @@ pub struct RecvOp { pub struct NfcTag {} impl NfcTag { + /// Check the existence of an NFC driver. + pub fn setup() -> bool { + syscalls::command(DRIVER_NUMBER, command_nr::CHECK, 0, 0).is_ok() + } + pub fn enable_emulation() -> bool { NfcTag::emulate(true) } From ce446f571cee0185fa3a7b4612f94f7846f2c022 Mon Sep 17 00:00:00 2001 From: Mirna Date: Wed, 28 Oct 2020 19:11:09 +0200 Subject: [PATCH 17/19] Resolve Comments --- third_party/libtock-drivers/src/nfc.rs | 29 ++++++++++++++------------ 1 file changed, 16 insertions(+), 13 deletions(-) diff --git a/third_party/libtock-drivers/src/nfc.rs b/third_party/libtock-drivers/src/nfc.rs index 5902160..bf2a249 100644 --- a/third_party/libtock-drivers/src/nfc.rs +++ b/third_party/libtock-drivers/src/nfc.rs @@ -13,6 +13,7 @@ mod command_nr { pub const EMULATE: usize = 3; pub const CONFIGURE: usize = 4; pub const FRAMEDELAYMAX: usize = 5; + pub const SELECTED: usize = 6; } mod subscribe_nr { @@ -27,6 +28,7 @@ mod allow_nr { } #[allow(dead_code)] +#[derive(Clone, Copy)] pub struct RecvOp { pub result_code: usize, pub recv_amount: usize, @@ -65,6 +67,11 @@ impl NfcTag { return false; } + let result = syscalls::command(DRIVER_NUMBER, command_nr::SELECTED, 0, 0); + if result.is_err() { + return false; + } + util::yieldk_for(|| is_selected.get()); true } @@ -83,27 +90,23 @@ impl NfcTag { /// 2. Subscribe to having a successful receive callback. /// 3. Issue the request for reception. pub fn receive(buf: &mut [u8; 256]) -> TockResult { - let result = syscalls::allow(DRIVER_NUMBER, allow_nr::RECEIVE, buf)?; + let _result = syscalls::allow(DRIVER_NUMBER, allow_nr::RECEIVE, buf)?; // set callback with 2 arguments, to receive ReturnCode and RX Amount - let result_code = Cell::new(None); - let recv_amount = Cell::new(None); + let recv_data = Cell::new(None); let mut callback = |result, amount| { - result_code.set(Some(result)); - recv_amount.set(Some(amount)) + recv_data.set(Some(RecvOp { + result_code: result, + recv_amount: amount, + })) }; - let subscription = syscalls::subscribe::( + let _subscription = syscalls::subscribe::( DRIVER_NUMBER, subscribe_nr::RECEIVE, &mut callback, )?; syscalls::command(DRIVER_NUMBER, command_nr::RECEIVE, 0, 0)?; - util::yieldk_for(|| recv_amount.get().is_some()); - mem::drop(subscription); - mem::drop(result); - Ok(RecvOp { - result_code: result_code.get().unwrap(), - recv_amount: recv_amount.get().unwrap(), - }) + util::yieldk_for(|| recv_data.get().is_some()); + Ok(recv_data.get().unwrap()) } /// 1. Share with the driver a buffer containing the app's reply. From 3d71da0bf5e0fe27ae00ee685351cd5e0d1771fc Mon Sep 17 00:00:00 2001 From: Mirna Date: Wed, 28 Oct 2020 20:28:53 +0200 Subject: [PATCH 18/19] Unify drop pattern --- third_party/libtock-drivers/src/nfc.rs | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/third_party/libtock-drivers/src/nfc.rs b/third_party/libtock-drivers/src/nfc.rs index bf2a249..24ff50b 100644 --- a/third_party/libtock-drivers/src/nfc.rs +++ b/third_party/libtock-drivers/src/nfc.rs @@ -90,7 +90,7 @@ impl NfcTag { /// 2. Subscribe to having a successful receive callback. /// 3. Issue the request for reception. pub fn receive(buf: &mut [u8; 256]) -> TockResult { - let _result = syscalls::allow(DRIVER_NUMBER, allow_nr::RECEIVE, buf)?; + let result = syscalls::allow(DRIVER_NUMBER, allow_nr::RECEIVE, buf)?; // set callback with 2 arguments, to receive ReturnCode and RX Amount let recv_data = Cell::new(None); let mut callback = |result, amount| { @@ -99,13 +99,15 @@ impl NfcTag { recv_amount: amount, })) }; - let _subscription = syscalls::subscribe::( + let subscription = syscalls::subscribe::( DRIVER_NUMBER, subscribe_nr::RECEIVE, &mut callback, )?; syscalls::command(DRIVER_NUMBER, command_nr::RECEIVE, 0, 0)?; util::yieldk_for(|| recv_data.get().is_some()); + mem::drop(subscription); + mem::drop(result); Ok(recv_data.get().unwrap()) } From 9e89803cb8f6031cd52224c7c9eb3540a46828dc Mon Sep 17 00:00:00 2001 From: Mirna Date: Thu, 29 Oct 2020 22:51:51 +0200 Subject: [PATCH 19/19] Removed Select syscalls, handled in the driver --- third_party/libtock-drivers/src/nfc.rs | 30 -------------------------- 1 file changed, 30 deletions(-) diff --git a/third_party/libtock-drivers/src/nfc.rs b/third_party/libtock-drivers/src/nfc.rs index 24ff50b..a67e944 100644 --- a/third_party/libtock-drivers/src/nfc.rs +++ b/third_party/libtock-drivers/src/nfc.rs @@ -12,14 +12,11 @@ mod command_nr { pub const RECEIVE: usize = 2; pub const EMULATE: usize = 3; pub const CONFIGURE: usize = 4; - pub const FRAMEDELAYMAX: usize = 5; - pub const SELECTED: usize = 6; } mod subscribe_nr { pub const TRANSMIT: usize = 1; pub const RECEIVE: usize = 2; - pub const SELECT: usize = 3; } mod allow_nr { @@ -54,38 +51,11 @@ impl NfcTag { syscalls::command(DRIVER_NUMBER, command_nr::EMULATE, enabled as usize, 0).is_ok() } - /// Subscribe to the tag being SELECTED callback. - pub fn selected() -> bool { - let is_selected = Cell::new(false); - let mut is_selected_alarm = || is_selected.set(true); - let subscription = syscalls::subscribe::( - DRIVER_NUMBER, - subscribe_nr::SELECT, - &mut is_selected_alarm, - ); - if subscription.is_err() { - return false; - } - - let result = syscalls::command(DRIVER_NUMBER, command_nr::SELECTED, 0, 0); - if result.is_err() { - return false; - } - - util::yieldk_for(|| is_selected.get()); - true - } - /// Configure the tag type command. pub fn configure(tag_type: u8) -> bool { syscalls::command(DRIVER_NUMBER, command_nr::CONFIGURE, tag_type as usize, 0).is_ok() } - /// Set the maximum frame delay value to support transmission with the reader. - pub fn set_framedelaymax(delay: u32) -> bool { - syscalls::command(DRIVER_NUMBER, command_nr::FRAMEDELAYMAX, delay as usize, 0).is_ok() - } - /// 1. Share with the driver a buffer. /// 2. Subscribe to having a successful receive callback. /// 3. Issue the request for reception.