Updates in the application logic

This commit is contained in:
Mirna
2020-10-29 23:07:23 +02:00
parent 16870d52c5
commit 69194f3960

View File

@@ -1,23 +1,44 @@
#![no_std] #![no_std]
#![allow(unused_imports)]
extern crate alloc; extern crate alloc;
extern crate lang_items; extern crate lang_items;
extern crate libtock_drivers;
use core::fmt::Write; use core::fmt::Write;
use libtock_core::result::CommandError;
use libtock_drivers::console::Console; use libtock_drivers::console::Console;
#[cfg(feature = "with_nfc")]
use libtock_drivers::nfc::NfcTag; use libtock_drivers::nfc::NfcTag;
#[cfg(feature = "with_nfc")]
use libtock_drivers::nfc::RecvOp; use libtock_drivers::nfc::RecvOp;
use libtock_drivers::result::TockError;
#[allow(dead_code)] #[allow(dead_code)]
/// Helper function to write a slice into a fixed /// Helper function to write a slice into a transmission buffer.
/// length transmission buffer.
fn write_tx_buffer(buf: &mut [u8], slice: &[u8]) { fn write_tx_buffer(buf: &mut [u8], slice: &[u8]) {
for (i, &byte) in slice.iter().enumerate() { for (i, &byte) in slice.iter().enumerate() {
buf[i] = byte; buf[i] = byte;
} }
} }
#[allow(dead_code)]
/// Helper function to write on console the received packet.
fn print_rx_buffer(buf: &mut [u8], amount: usize) {
if amount < 1 || amount > buf.len() {
return;
}
let mut console = Console::new();
write!(console, " -- RX Packet:").unwrap();
for byte in buf.iter().take(amount - 1) {
write!(console, " {:02x?}", byte).unwrap();
}
writeln!(console, " {:02x?}", buf[amount - 1]).unwrap();
}
#[cfg(feature = "with_nfc")]
#[derive(PartialEq, Eq)] #[derive(PartialEq, Eq)]
/// enum for reserving the NFC tag state.
enum State { enum State {
Enabled, Enabled,
Disabled, Disabled,
@@ -29,8 +50,11 @@ fn main() {
writeln!(console, "****************************************").unwrap(); writeln!(console, "****************************************").unwrap();
writeln!(console, "nfct_test application is installed").unwrap(); writeln!(console, "nfct_test application is installed").unwrap();
#[cfg(feature = "with_nfc")]
let mut state = State::Disabled; let mut state = State::Disabled;
#[cfg(feature = "with_nfc")]
let mut state_change_cntr = 0; let mut state_change_cntr = 0;
#[cfg(feature = "with_nfc")]
loop { loop {
match state { match state {
State::Enabled => { State::Enabled => {
@@ -40,28 +64,15 @@ fn main() {
Ok(RecvOp { Ok(RecvOp {
recv_amount: amount, recv_amount: amount,
.. ..
}) => match amount { }) => print_rx_buffer(&mut rx_buf, amount),
1 => writeln!(console, " -- RX Packet: {:02x?}", rx_buf[0],).unwrap(), Err(TockError::Command(CommandError {
2 => writeln!( return_code: -4, /* EOFF: Not Ready */
console, ..
" -- RX Packet: {:02x?} {:02x?}", })) => (),
rx_buf[0], rx_buf[1], Err(TockError::Command(CommandError {
) return_code: value, ..
.unwrap(), })) => writeln!(console, " -- Err({})!", value).unwrap(),
3 => writeln!( Err(_) => writeln!(console, " -- RX ERROR").unwrap(),
console,
" -- RX Packet: {:02x?} {:02x?} {:02x?}",
rx_buf[0], rx_buf[1], rx_buf[2],
)
.unwrap(),
_ => writeln!(
console,
" -- RX Packet: {:02x?} {:02x?} {:02x?} {:02x?}",
rx_buf[0], rx_buf[1], rx_buf[2], rx_buf[3],
)
.unwrap(),
},
Err(_) => writeln!(console, " -- rx error!").unwrap(),
} }
match rx_buf[0] { match rx_buf[0] {
@@ -83,6 +94,18 @@ fn main() {
} }
} }
0x02 | 0x03 /* APDU Prefix */ => { 0x02 | 0x03 /* APDU Prefix */ => {
// If the received packet is applet selection command (FIDO 2)
if rx_buf[1] == 0x00 && rx_buf[2] == 0xa4 && rx_buf[3] == 0x04 {
// Vesion: "U2F_V2"
// let mut reply = [rx_buf[0], 0x55, 0x32, 0x46, 0x5f, 0x56, 0x32, 0x90, 0x00,];
// Vesion: "FIDO_2_0"
let mut reply = [rx_buf[0], 0x46, 0x49, 0x44, 0x4f, 0x5f, 0x32, 0x5f, 0x30, 0x90, 0x00,];
let amount = reply.len();
match NfcTag::transmit(&mut reply, amount) {
Ok(_) => (),
Err(_) => writeln!(console, " -- tx error!").unwrap(),
}
} else {
let mut reply = [rx_buf[0], 0x90, 0x00]; let mut reply = [rx_buf[0], 0x90, 0x00];
let amount = reply.len(); let amount = reply.len();
match NfcTag::transmit(&mut reply, amount) { match NfcTag::transmit(&mut reply, amount) {
@@ -90,6 +113,7 @@ fn main() {
Err(_) => writeln!(console, " -- tx error!").unwrap(), Err(_) => writeln!(console, " -- tx error!").unwrap(),
} }
} }
}
0x52 | 0x50 /* WUPA | Halt */ => { 0x52 | 0x50 /* WUPA | Halt */ => {
if NfcTag::disable_emulation() { if NfcTag::disable_emulation() {
writeln!(console, " -- TAG DISABLED").unwrap(); writeln!(console, " -- TAG DISABLED").unwrap();
@@ -97,30 +121,20 @@ fn main() {
state = State::Disabled; state = State::Disabled;
break; break;
} }
_ => { _ => (),
}
} }
} }
} }
State::Disabled => { State::Disabled => {
if NfcTag::enable_emulation() { NfcTag::enable_emulation();
writeln!(console, " -- TAG ENABLED").unwrap(); // Configure Type 4 tag
}
// 1. Configure Type 4 tag
if NfcTag::configure(4) { 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);
}
state = State::Enabled; state = State::Enabled;
} }
} }
}
state_change_cntr += 1; state_change_cntr += 1;
if state_change_cntr > 10 && state == State::Disabled { if state_change_cntr > 100 && state == State::Disabled {
break; break;
} }
} }