diff --git a/examples/doc/quick_start_example1.c b/examples/doc/quick_start_example1.c index 51dbf6f..fe67f33 100644 --- a/examples/doc/quick_start_example1.c +++ b/examples/doc/quick_start_example1.c @@ -27,9 +27,6 @@ main (int argc, const char *argv[]) // Set connected NFC device to initiator mode nfc_initiator_init (pnd); - // Enable field so more power consuming cards can power themselves up - nfc_configure (pnd, NDO_ACTIVATE_FIELD, true); - printf ("Connected to NFC reader: %s\n", pnd->acName); // Poll for a ISO14443A (MIFARE) tag diff --git a/examples/nfc-anticol.c b/examples/nfc-anticol.c index 5b2769e..938e4fb 100644 --- a/examples/nfc-anticol.c +++ b/examples/nfc-anticol.c @@ -152,22 +152,11 @@ main (int argc, char *argv[]) // Initialise NFC device as "initiator" nfc_initiator_init (pnd); - // Drop the field for a while - if (!nfc_configure (pnd, NDO_ACTIVATE_FIELD, false)) { - nfc_perror (pnd, "nfc_configure"); - exit (EXIT_FAILURE); - } - // Configure the CRC if (!nfc_configure (pnd, NDO_HANDLE_CRC, false)) { nfc_perror (pnd, "nfc_configure"); exit (EXIT_FAILURE); } - // Configure parity settings - if (!nfc_configure (pnd, NDO_HANDLE_PARITY, true)) { - nfc_perror (pnd, "nfc_configure"); - exit (EXIT_FAILURE); - } // Use raw send/receive methods if (!nfc_configure (pnd, NDO_EASY_FRAMING, false)) { nfc_perror (pnd, "nfc_configure"); @@ -178,17 +167,6 @@ main (int argc, char *argv[]) nfc_perror (pnd, "nfc_configure"); exit (EXIT_FAILURE); } - // Force 14443-A mode - if (!nfc_configure (pnd, NDO_FORCE_ISO14443_A, true)) { - nfc_perror (pnd, "nfc_configure"); - exit (EXIT_FAILURE); - } - - // Enable field so more power consuming cards can power themselves up - if (!nfc_configure (pnd, NDO_ACTIVATE_FIELD, true)) { - nfc_perror (pnd, "nfc_configure"); - exit (EXIT_FAILURE); - } printf ("Connected to NFC reader: %s\n\n", pnd->acName); diff --git a/examples/nfc-emulate-forum-tag4.c b/examples/nfc-emulate-forum-tag4.c index cbc187d..2e5502c 100644 --- a/examples/nfc-emulate-forum-tag4.c +++ b/examples/nfc-emulate-forum-tag4.c @@ -352,7 +352,7 @@ main (int argc, char *argv[]) printf ("Connected to NFC device: %s\n", pnd->acName); printf ("Emulating NDEF tag now, please touch it with a second NFC device\n"); - nfc_emulate_target (pnd, &emulator); + nfc_emulate_target (pnd, &emulator); // contains already nfc_target_init() call nfc_disconnect(pnd); diff --git a/examples/nfc-mfclassic.c b/examples/nfc-mfclassic.c index b42efec..9a0574e 100644 --- a/examples/nfc-mfclassic.c +++ b/examples/nfc-mfclassic.c @@ -436,29 +436,11 @@ main (int argc, const char *argv[]) nfc_initiator_init (pnd); - // Drop the field for a while - if (!nfc_configure (pnd, NDO_ACTIVATE_FIELD, false)) { - nfc_perror (pnd, "nfc_configure"); - exit (EXIT_FAILURE); - } // Let the reader only try once to find a tag if (!nfc_configure (pnd, NDO_INFINITE_SELECT, false)) { nfc_perror (pnd, "nfc_configure"); exit (EXIT_FAILURE); } - if (!nfc_configure (pnd, NDO_HANDLE_CRC, true)) { - nfc_perror (pnd, "nfc_configure"); - exit (EXIT_FAILURE); - } - if (!nfc_configure (pnd, NDO_HANDLE_PARITY, true)) { - nfc_perror (pnd, "nfc_configure"); - exit (EXIT_FAILURE); - } - // Enable field so more power consuming cards can power themselves up - if (!nfc_configure (pnd, NDO_ACTIVATE_FIELD, true)) { - nfc_perror (pnd, "nfc_configure"); - exit (EXIT_FAILURE); - } // Disable ISO14443-4 switching in order to read devices that emulate Mifare Classic with ISO14443-4 compliance. nfc_configure (pnd, NDO_AUTO_ISO14443_4, false); diff --git a/examples/nfc-mfultralight.c b/examples/nfc-mfultralight.c index d0480e6..4ae8ccf 100644 --- a/examples/nfc-mfultralight.c +++ b/examples/nfc-mfultralight.c @@ -213,29 +213,11 @@ main (int argc, const char *argv[]) nfc_initiator_init (pnd); - // Drop the field for a while - if (!nfc_configure (pnd, NDO_ACTIVATE_FIELD, false)) { - nfc_perror (pnd, "nfc_configure"); - exit (EXIT_FAILURE); - } // Let the device only try once to find a tag if (!nfc_configure (pnd, NDO_INFINITE_SELECT, false)) { nfc_perror (pnd, "nfc_configure"); exit (EXIT_FAILURE); } - if (!nfc_configure (pnd, NDO_HANDLE_CRC, true)) { - nfc_perror (pnd, "nfc_configure"); - exit (EXIT_FAILURE); - } - if (!nfc_configure (pnd, NDO_HANDLE_PARITY, true)) { - nfc_perror (pnd, "nfc_configure"); - exit (EXIT_FAILURE); - } - // Enable field so more power consuming cards can power themselves up - if (!nfc_configure (pnd, NDO_ACTIVATE_FIELD, true)) { - nfc_perror (pnd, "nfc_configure"); - exit (EXIT_FAILURE); - } printf ("Connected to NFC device: %s\n", pnd->acName); diff --git a/examples/nfc-poll.c b/examples/nfc-poll.c index 5aeb72f..bc5e4c8 100644 --- a/examples/nfc-poll.c +++ b/examples/nfc-poll.c @@ -121,21 +121,11 @@ main (int argc, const char *argv[]) } nfc_initiator_init (pnd); - // Drop the field for a while - if (!nfc_configure (pnd, NDO_ACTIVATE_FIELD, false)) { - nfc_perror (pnd, "nfc_configure"); - exit (EXIT_FAILURE); - } // Let the reader only try once to find a tag if (!nfc_configure (pnd, NDO_INFINITE_SELECT, false)) { nfc_perror (pnd, "nfc_configure"); exit (EXIT_FAILURE); } - // Enable field so more power consuming cards can power themselves up - if (!nfc_configure (pnd, NDO_ACTIVATE_FIELD, true)) { - nfc_perror (pnd, "nfc_configure"); - exit (EXIT_FAILURE); - } printf ("Connected to NFC reader: %s\n", pnd->acName); diff --git a/examples/nfc-relay-picc.c b/examples/nfc-relay-picc.c index 45e3eef..b1d4da4 100644 --- a/examples/nfc-relay-picc.c +++ b/examples/nfc-relay-picc.c @@ -245,6 +245,12 @@ main (int argc, char *argv[]) printf ("Connected to the NFC reader device: %s\n", pndInitiator->acName); + if (!nfc_initiator_init (pndInitiator) { + printf ("Error: fail initializing initiator\n"); + nfc_disconnect (pndInitiator); + exit (EXIT_FAILURE); + } + // Try to find a ISO 14443-4A tag nfc_modulation_t nm = { .nmt = NMT_ISO14443A, @@ -253,7 +259,7 @@ main (int argc, char *argv[]) if (!nfc_initiator_select_passive_target (pndInitiator, nm, NULL, 0, &ntRealTarget)) { printf ("Error: no tag was found\n"); nfc_disconnect (pndInitiator); - exit (EXIT_FAILURE); + exit (EXIT_FAILURE); } printf("Found tag:\n"); diff --git a/examples/pn53x-sam.c b/examples/pn53x-sam.c index 2d0ac6d..6b707a9 100644 --- a/examples/pn53x-sam.c +++ b/examples/pn53x-sam.c @@ -174,21 +174,11 @@ main (int argc, const char *argv[]) // Set connected NFC device to initiator mode nfc_initiator_init (pnd); - // Drop the field for a while - if (!nfc_configure (pnd, NDO_ACTIVATE_FIELD, false)) { - nfc_perror (pnd, "nfc_configure"); - exit (EXIT_FAILURE); - } // Let the reader only try once to find a tag if (!nfc_configure (pnd, NDO_INFINITE_SELECT, false)) { nfc_perror (pnd, "nfc_configure"); exit (EXIT_FAILURE); } - // Enable field so more power consuming cards can power themselves up - if (!nfc_configure (pnd, NDO_ACTIVATE_FIELD, true)) { - nfc_perror (pnd, "nfc_configure"); - exit (EXIT_FAILURE); - } // Read the SAM's info const nfc_modulation_t nmSAM = { .nmt = NMT_ISO14443A, diff --git a/libnfc/chips/pn53x.c b/libnfc/chips/pn53x.c index 78cf632..6bfbbe2 100644 --- a/libnfc/chips/pn53x.c +++ b/libnfc/chips/pn53x.c @@ -53,6 +53,7 @@ static const byte_t pn53x_nack_frame[] = { 0x00, 0x00, 0xff, 0xff, 0x00, 0x00 }; static const byte_t pn53x_error_frame[] = { 0x00, 0x00, 0xff, 0x01, 0xff, 0x7f, 0x81, 0x00 }; /* prototypes */ +bool pn53x_reset_settings(nfc_device_t * pnd); nfc_modulation_t pn53x_ptt_to_nm (const pn53x_target_type_t ptt); pn53x_modulation_t pn53x_nm_to_pm (const nfc_modulation_t nm); pn53x_target_type_t pn53x_nm_to_ptt (const nfc_modulation_t nm); @@ -74,18 +75,14 @@ pn53x_init(nfc_device_t * pnd) // Parity handling is enabled by default pnd->bPar = true; - // Reset the ending transmission bits register, it is unknown what the last tranmission used there - CHIP_DATA (pnd)->ui8TxBits = 0; - if (!pn53x_write_register (pnd, REG_CIU_BIT_FRAMING, SYMBOL_TX_LAST_BITS, 0x00)) { - return false; - } - // We can't read these parameters, so we set a default config by using the SetParameters wrapper // Note: pn53x_SetParameters() will save the sent value in pnd->ui8Parameters cache if(!pn53x_SetParameters(pnd, PARAM_AUTO_ATR_RES | PARAM_AUTO_RATS)) { return false; } + pn53x_reset_settings(pnd); + // Add the firmware revision to the device name char *pcName; pcName = strdup (pnd->acName); @@ -94,6 +91,16 @@ pn53x_init(nfc_device_t * pnd) return true; } +bool +pn53x_reset_settings(nfc_device_t * pnd) +{ + // Reset the ending transmission bits register, it is unknown what the last tranmission used there + CHIP_DATA (pnd)->ui8TxBits = 0; + if (!pn53x_write_register (pnd, REG_CIU_BIT_FRAMING, SYMBOL_TX_LAST_BITS, 0x00)) { + return false; + } +} + bool pn53x_transceive (nfc_device_t * pnd, const byte_t * pbtTx, const size_t szTx, byte_t * pbtRx, size_t *pszRx) { @@ -571,6 +578,10 @@ pn53x_configure (nfc_device_t * pnd, const nfc_device_option_t ndo, const bool b if (!pn53x_write_register (pnd, REG_CIU_RX_MODE, SYMBOL_RX_FRAMING, 0x00)) { return false; } + // Set the PN53X to force 100% ASK Modified miller decoding (default for 14443A cards) + if (!pn53x_write_register (pnd, REG_CIU_TX_AUTO, SYMBOL_FORCE_100_ASK, 0x40)) + return false; + return true; break; } @@ -596,9 +607,7 @@ pn53x_check_communication (nfc_device_t *pnd) bool pn53x_initiator_init (nfc_device_t * pnd) { - // Set the PN53X to force 100% ASK Modified miller decoding (default for 14443A cards) - if (!pn53x_write_register (pnd, REG_CIU_TX_AUTO, SYMBOL_FORCE_100_ASK, 0x40)) - return false; + pn53x_reset_settings(pnd); // Configure the PN53X to be an Initiator or Reader/Writer if (!pn53x_write_register (pnd, REG_CIU_CONTROL, SYMBOL_INITIATOR, 0x10)) @@ -1001,10 +1010,7 @@ pn53x_initiator_deselect_target (nfc_device_t * pnd) bool pn53x_target_init (nfc_device_t * pnd, nfc_target_t * pnt, byte_t * pbtRx, size_t * pszRx) { - // Save the current configuration settings - bool bCrc = pnd->bCrc; - bool bPar = pnd->bPar; - + pn53x_reset_settings(pnd); pn53x_target_mode_t ptm = PTM_NORMAL; switch (pnt->nm.nmt) { case NMT_ISO14443A: @@ -1041,12 +1047,6 @@ pn53x_target_init (nfc_device_t * pnd, nfc_target_t * pnt, byte_t * pbtRx, size_ break; } - // Make sure the CRC & parity are handled by the device, this is needed for target_init to work properly - if (!bCrc) - pn53x_configure (pnd, NDO_HANDLE_CRC, true); - if (!bPar) - pn53x_configure (pnd, NDO_HANDLE_PARITY, true); - // Let the PN53X be activated by the RF level detector from power down mode if (!pn53x_write_register (pnd, REG_CIU_TX_AUTO, SYMBOL_INITIAL_RF_ON, 0x04)) return false; @@ -1170,12 +1170,6 @@ pn53x_target_init (nfc_device_t * pnd, nfc_target_t * pnt, byte_t * pbtRx, size_ } } - // Restore the CRC & parity setting to the original value (if needed) - if (!bCrc) - pn53x_configure ((nfc_device_t *) pnd, NDO_HANDLE_CRC, false); - if (!bPar) - pn53x_configure ((nfc_device_t *) pnd, NDO_HANDLE_PARITY, false); - return true; } diff --git a/libnfc/nfc.c b/libnfc/nfc.c index acc5c32..4ffebb7 100644 --- a/libnfc/nfc.c +++ b/libnfc/nfc.c @@ -74,7 +74,7 @@ const struct nfc_driver_t *nfc_drivers[] = { * When it has successfully claimed a NFC device, memory is allocated to save the device information. It will return a pointer to a \a nfc_device_t struct. * This pointer should be supplied by every next functions of libnfc that should perform an action with this device. * - * @note During this function, the device will be configured with default options: + * @note During this function, the device will be configured with default initiator options, cf nfc_initiator_init: * - Crc is handled by the device (NDO_HANDLE_CRC = true) * - Parity is handled the device (NDO_HANDLE_PARITY = true) * - Cryto1 cipher is disabled (NDO_ACTIVATE_CRYPTO1 = false) @@ -82,6 +82,9 @@ const struct nfc_driver_t *nfc_drivers[] = { * - Auto-switching in ISO14443-4 mode is enabled (NDO_AUTO_ISO14443_4 = true) * - Invalid frames are not accepted (NDO_ACCEPT_INVALID_FRAMES = false) * - Multiple frames are not accepted (NDO_ACCEPT_MULTIPLE_FRAMES = false) + * - 14443-A mode is activated (NDO_FORCE_ISO14443_A = true) + * - Let the device try forever to find a target (NDO_INFINITE_SELECT = true) + * - RF field is shortly dropped (if it was enabled) then activated again */ nfc_device_t * nfc_connect (nfc_device_desc_t * pndd) @@ -111,30 +114,7 @@ nfc_connect (nfc_device_desc_t * pndd) DBG ("[%s] has been claimed.", pnd->acName); // Set default configuration options - // Make sure we reset the CRC and parity to chip handling. - if (!nfc_configure (pnd, NDO_HANDLE_CRC, true)) - return NULL; - if (!nfc_configure (pnd, NDO_HANDLE_PARITY, true)) - return NULL; - - // Deactivate the CRYPTO1 cipher, it may could cause problems when still active - if (!nfc_configure (pnd, NDO_ACTIVATE_CRYPTO1, false)) - return NULL; - - // Activate "easy framing" feature by default - if (!nfc_configure (pnd, NDO_EASY_FRAMING, true)) - return NULL; - - // Activate auto ISO14443-4 switching by default - if (!nfc_configure (pnd, NDO_AUTO_ISO14443_4, true)) - return NULL; - - // Disallow invalid frame - if (!nfc_configure (pnd, NDO_ACCEPT_INVALID_FRAMES, false)) - return NULL; - - // Disallow multiple frames - if (!nfc_configure (pnd, NDO_ACCEPT_MULTIPLE_FRAMES, false)) + if (!nfc_initiator_init(pnd)) return NULL; return pnd; @@ -255,7 +235,41 @@ nfc_configure (nfc_device_t * pnd, const nfc_device_option_t ndo, const bool bEn bool nfc_initiator_init (nfc_device_t * pnd) { + // Drop the field for a while + if (!nfc_configure (pnd, NDO_ACTIVATE_FIELD, false)) + return false; + // Force 14443-A mode + if (!nfc_configure (pnd, NDO_FORCE_ISO14443_A, true)) + return false; + // Disallow invalid frame + if (!nfc_configure (pnd, NDO_ACCEPT_INVALID_FRAMES, false)) + return false; + // Disallow multiple frames + if (!nfc_configure (pnd, NDO_ACCEPT_MULTIPLE_FRAMES, false)) + return false; + // Make sure we reset the CRC and parity to chip handling. + if (!nfc_configure (pnd, NDO_HANDLE_CRC, true)) + return false; + if (!nfc_configure (pnd, NDO_HANDLE_PARITY, true)) + return false; + // Activate "easy framing" feature by default + if (!nfc_configure (pnd, NDO_EASY_FRAMING, true)) + return false; + // Activate auto ISO14443-4 switching by default + if (!nfc_configure (pnd, NDO_AUTO_ISO14443_4, true)) + return false; + // Deactivate the CRYPTO1 cipher, it may could cause problems when still active + if (!nfc_configure (pnd, NDO_ACTIVATE_CRYPTO1, false)) + return false; + // Let the device try forever to find a target/tag + if (!nfc_configure (pnd, NDO_INFINITE_SELECT, true)) + return false; + // Enable field so more power consuming cards can power themselves up + if (!nfc_configure (pnd, NDO_ACTIVATE_FIELD, true)) + return false; + HAL (initiator_init, pnd); + return true; } /** @@ -351,18 +365,10 @@ nfc_initiator_list_passive_targets (nfc_device_t * pnd, pnd->iLastError = 0; - // Drop the field for a while - if (!nfc_configure (pnd, NDO_ACTIVATE_FIELD, false)) { - return false; - } // Let the reader only try once to find a tag if (!nfc_configure (pnd, NDO_INFINITE_SELECT, false)) { return false; } - // Enable field so more power consuming cards can power themselves up - if (!nfc_configure (pnd, NDO_ACTIVATE_FIELD, true)) { - return false; - } switch (nm.nmt) { case NMT_ISO14443B: { @@ -600,7 +606,26 @@ nfc_initiator_transceive_bits_timed (nfc_device_t * pnd, const byte_t * pbtTx, c bool nfc_target_init (nfc_device_t * pnd, nfc_target_t * pnt, byte_t * pbtRx, size_t * pszRx) { + // Disallow invalid frame + if (!nfc_configure (pnd, NDO_ACCEPT_INVALID_FRAMES, false)) + return false; + // Disallow multiple frames + if (!nfc_configure (pnd, NDO_ACCEPT_MULTIPLE_FRAMES, false)) + return false; + // Make sure we reset the CRC and parity to chip handling. + if (!nfc_configure (pnd, NDO_HANDLE_CRC, true)) + return false; + if (!nfc_configure (pnd, NDO_HANDLE_PARITY, true)) + return false; + // Activate "easy framing" feature by default + if (!nfc_configure (pnd, NDO_EASY_FRAMING, true)) + return false; + // Deactivate the CRYPTO1 cipher, it may could cause problems when still active + if (!nfc_configure (pnd, NDO_ACTIVATE_CRYPTO1, false)) + return false; + HAL (target_init, pnd, pnt, pbtRx, pszRx); + return true; } /* TODO Document this function */ diff --git a/test/test_access_storm.c b/test/test_access_storm.c index cb327bc..7e8408b 100644 --- a/test/test_access_storm.c +++ b/test/test_access_storm.c @@ -38,24 +38,6 @@ test_access_storm (void) res = nfc_initiator_init(device); cut_assert_true (res, cut_message ("nfc_initiator_init")); - // Drop the field for a while - res = nfc_configure(device,NDO_ACTIVATE_FIELD,false); - cut_assert_true (res, cut_message ("nfc_configure")); - - // Let the reader only try once to find a tag - res = nfc_configure(device,NDO_INFINITE_SELECT,false); - cut_assert_true (res, cut_message ("nfc_configure")); - - // Configure the CRC and Parity settings - res = nfc_configure(device,NDO_HANDLE_CRC,true); - cut_assert_true (res, cut_message ("nfc_configure")); - res = nfc_configure(device,NDO_HANDLE_PARITY,true); - cut_assert_true (res, cut_message ("nfc_configure")); - - // Enable field so more power consuming cards can power themselves - res = nfc_configure(device,NDO_ACTIVATE_FIELD,true); - cut_assert_true (res, cut_message ("nfc_configure")); - const nfc_modulation_t nm = { .nmt = NMT_ISO14443A, .nbr = NBR_106,