From dd814b8ded7cbe8fa2bf00d8e8d970974feb5cd4 Mon Sep 17 00:00:00 2001 From: Mirna Date: Mon, 2 Nov 2020 10:49:00 +0200 Subject: [PATCH] Remove duplicate code --- examples/nfct_test.rs | 40 +++++++++++++++++----------------------- 1 file changed, 17 insertions(+), 23 deletions(-) diff --git a/examples/nfct_test.rs b/examples/nfct_test.rs index 82a1625..90d2dfa 100644 --- a/examples/nfct_test.rs +++ b/examples/nfct_test.rs @@ -36,6 +36,16 @@ fn print_rx_buffer(buf: &mut [u8], amount: usize) { writeln!(console, " {:02x?}", buf[amount - 1]).unwrap(); } +#[allow(dead_code)] +/// Helper function to write on console the received packet. +fn transmit_slice(buf: &mut [u8] { + let amount = buf.len(); + match NfcTag::transmit(&mut buf, amount) { + Ok(_) => (), + Err(_) => writeln!(console, " -- tx error!").unwrap(), + } +} + #[cfg(feature = "with_nfc")] #[derive(PartialEq, Eq)] /// enum for reserving the NFC tag state. @@ -72,46 +82,30 @@ fn main() { Err(TockError::Command(CommandError { return_code: value, .. })) => writeln!(console, " -- Err({})!", value).unwrap(), - Err(_) => writeln!(console, " -- RX ERROR").unwrap(), + Err(_) => writeln!(console, " -- RX Err").unwrap(), } match rx_buf[0] { 0xe0 /* RATS */=> { let mut answer_to_select = [0x05, 0x78, 0x80, 0xB1, 0x00]; - let amount = answer_to_select.len(); - match NfcTag::transmit(&mut answer_to_select, amount) { - Ok(_) => (), - Err(_) => writeln!(console, " -- tx error!").unwrap(), - } + transmit_slice(&mut answer_to_select); } 0xc2 /* DESELECT */ => { // Ignore the request let mut command_error = [0x6A, 0x81]; - let amount = command_error.len(); - match NfcTag::transmit(&mut command_error, amount) { - Ok(_) => (), - Err(_) => writeln!(console, " -- tx error!").unwrap(), - } + transmit_slice(&mut command_error); } 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" + /// Vesion: "U2F_V2" // let mut reply = [rx_buf[0], 0x55, 0x32, 0x46, 0x5f, 0x56, 0x32, 0x90, 0x00,]; - // Vesion: "FIDO_2_0" + /// 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(), - } + transmit_slice(&mut reply); } else { let mut reply = [rx_buf[0], 0x90, 0x00]; - let amount = reply.len(); - match NfcTag::transmit(&mut reply, amount) { - Ok(_) => (), - Err(_) => writeln!(console, " -- tx error!").unwrap(), - } + transmit_slice(&mut reply); } } 0x52 | 0x50 /* WUPA | Halt */ => {