diff --git a/examples/nfc-dep-initiator.c b/examples/nfc-dep-initiator.c index ae90495..e7ad00e 100644 --- a/examples/nfc-dep-initiator.c +++ b/examples/nfc-dep-initiator.c @@ -86,7 +86,7 @@ main (int argc, const char *argv[]) return EXIT_FAILURE; } - if(!nfc_initiator_select_dep_target (pnd, NDM_PASSIVE, NBR_212, NULL, &nt)) { + if(!nfc_initiator_select_dep_target (pnd, NDM_PASSIVE, NBR_212, NULL, &nt, 1000)) { nfc_perror(pnd, "nfc_initiator_select_dep_target"); goto error; } diff --git a/include/nfc/nfc.h b/include/nfc/nfc.h index b168d6a..6d34b2f 100644 --- a/include/nfc/nfc.h +++ b/include/nfc/nfc.h @@ -76,7 +76,7 @@ extern "C" { NFC_EXPORT bool nfc_initiator_select_passive_target (nfc_device *pnd, const nfc_modulation nm, const uint8_t *pbtInitData, const size_t szInitData, nfc_target *pnt); NFC_EXPORT bool nfc_initiator_list_passive_targets (nfc_device *pnd, const nfc_modulation nm, nfc_target ant[], const size_t szTargets, size_t *pszTargetFound); NFC_EXPORT bool nfc_initiator_poll_target (nfc_device *pnd, const nfc_modulation *pnmTargetTypes, const size_t szTargetTypes, const uint8_t uiPollNr, const uint8_t uiPeriod, nfc_target *pnt); - NFC_EXPORT bool nfc_initiator_select_dep_target (nfc_device *pnd, const nfc_dep_mode ndm, const nfc_baud_rate nbr, const nfc_dep_info *pndiInitiator, nfc_target *pnt); + NFC_EXPORT bool nfc_initiator_select_dep_target (nfc_device *pnd, const nfc_dep_mode ndm, const nfc_baud_rate nbr, const nfc_dep_info *pndiInitiator, nfc_target *pnt, const int timeout); NFC_EXPORT bool nfc_initiator_deselect_target (nfc_device *pnd); NFC_EXPORT bool nfc_initiator_transceive_bytes (nfc_device *pnd, const uint8_t *pbtTx, const size_t szTx, uint8_t *pbtRx, size_t *pszRx, int timeout); NFC_EXPORT bool nfc_initiator_transceive_bits (nfc_device *pnd, const uint8_t *pbtTx, const size_t szTxBits, const uint8_t *pbtTxPar, uint8_t *pbtRx, size_t *pszRxBits, uint8_t *pbtRxPar); diff --git a/libnfc/chips/pn53x.c b/libnfc/chips/pn53x.c index 8627622..befa800 100644 --- a/libnfc/chips/pn53x.c +++ b/libnfc/chips/pn53x.c @@ -1038,7 +1038,8 @@ bool pn53x_initiator_select_dep_target(nfc_device *pnd, const nfc_dep_mode ndm, const nfc_baud_rate nbr, const nfc_dep_info *pndiInitiator, - nfc_target *pnt) + nfc_target *pnt, + const int timeout) { const uint8_t abtPassiveInitiatorData[] = { 0x00, 0xff, 0xff, 0x00, 0x00 }; // Only for 212/424 kpbs: First 4 bytes shall be set like this according to NFCIP-1, last byte is TSN (Time Slot Number) const uint8_t * pbtPassiveInitiatorData = NULL; @@ -1056,9 +1057,9 @@ pn53x_initiator_select_dep_target(nfc_device *pnd, } if (pndiInitiator) { - return pn53x_InJumpForDEP (pnd, ndm, nbr, pbtPassiveInitiatorData, pndiInitiator->abtNFCID3, pndiInitiator->abtGB, pndiInitiator->szGB, pnt); + return pn53x_InJumpForDEP (pnd, ndm, nbr, pbtPassiveInitiatorData, pndiInitiator->abtNFCID3, pndiInitiator->abtGB, pndiInitiator->szGB, pnt, timeout); } else { - return pn53x_InJumpForDEP (pnd, ndm, nbr, pbtPassiveInitiatorData, NULL, NULL, 0, pnt); + return pn53x_InJumpForDEP (pnd, ndm, nbr, pbtPassiveInitiatorData, NULL, NULL, 0, pnt, timeout); } } @@ -2199,7 +2200,8 @@ pn53x_InJumpForDEP (nfc_device *pnd, const uint8_t *pbtPassiveInitiatorData, const uint8_t *pbtNFCID3i, const uint8_t *pbtGBi, const size_t szGBi, - nfc_target *pnt) + nfc_target *pnt, + const int timeout) { // Max frame size = 1 (Command) + 1 (ActPass) + 1 (Baud rate) + 1 (Next) + 5 (PassiveInitiatorData) + 10 (NFCID3) + 48 (General bytes) = 67 bytes uint8_t abtCmd[67] = { InJumpForDEP, (ndm == NDM_ACTIVE) ? 0x01 : 0x00 }; @@ -2259,7 +2261,7 @@ pn53x_InJumpForDEP (nfc_device *pnd, uint8_t abtRx[PN53x_EXTENDED_FRAME__DATA_MAX_LEN]; size_t szRx = sizeof (abtRx); // Try to find a target, call the transceive callback function of the current device - if (!pn53x_transceive (pnd, abtCmd, offset, abtRx, &szRx, 0)) + if (!pn53x_transceive (pnd, abtCmd, offset, abtRx, &szRx, timeout)) return false; // Make sure one target has been found, the PN53X returns 0x00 if none was available @@ -2270,6 +2272,7 @@ pn53x_InJumpForDEP (nfc_device *pnd, if (pnt) { pnt->nm.nmt = NMT_DEP; pnt->nm.nbr = nbr; + pnt->nti.ndi.ndm = ndm; memcpy (pnt->nti.ndi.abtNFCID3, abtRx + 2, 10); pnt->nti.ndi.btDID = abtRx[12]; pnt->nti.ndi.btBS = abtRx[13]; diff --git a/libnfc/chips/pn53x.h b/libnfc/chips/pn53x.h index e892f0b..872e449 100644 --- a/libnfc/chips/pn53x.h +++ b/libnfc/chips/pn53x.h @@ -287,7 +287,8 @@ bool pn53x_initiator_poll_target (nfc_device *pnd, bool pn53x_initiator_select_dep_target (nfc_device *pnd, const nfc_dep_mode ndm, const nfc_baud_rate nbr, const nfc_dep_info *pndiInitiator, - nfc_target *pnt); + nfc_target *pnt, + const int timeout); bool pn53x_initiator_transceive_bits (nfc_device *pnd, const uint8_t *pbtTx, const size_t szTxBits, const uint8_t *pbtTxPar, uint8_t *pbtRx, size_t *pszRxBits, uint8_t *pbtRxPar); @@ -328,7 +329,8 @@ bool pn53x_InJumpForDEP (nfc_device *pnd, const uint8_t *pbtPassiveInitiatorData, const uint8_t *pbtNFCID3i, const uint8_t *pbtGB, const size_t szGB, - nfc_target *pnt); + nfc_target *pnt, + const int timeout); bool pn53x_TgInitAsTarget (nfc_device *pnd, pn53x_target_mode ptm, const uint8_t *pbtMifareParams, const uint8_t *pbtTkt, size_t szTkt, diff --git a/libnfc/drivers/pn53x_usb.c b/libnfc/drivers/pn53x_usb.c index cb8ac34..2f22b7a 100644 --- a/libnfc/drivers/pn53x_usb.c +++ b/libnfc/drivers/pn53x_usb.c @@ -571,6 +571,7 @@ read: if (timeout == USB_INFINITE_TIMEOUT) { usb_timeout = USB_TIMEOUT_PER_PASS; } else { + // A user-provided timeout is set, we have to cut it in multiple chunk to be able to keep an nfc_abort_command() mecanism remaining_time -= USB_TIMEOUT_PER_PASS; if (remaining_time <= 0) { pnd->iLastError = ECOMTIMEOUT; diff --git a/libnfc/nfc-internal.h b/libnfc/nfc-internal.h index d34c706..e59edc4 100644 --- a/libnfc/nfc-internal.h +++ b/libnfc/nfc-internal.h @@ -134,7 +134,7 @@ struct nfc_driver_t { bool (*initiator_init) (nfc_device *pnd); bool (*initiator_select_passive_target) (nfc_device *pnd, const nfc_modulation nm, const uint8_t * pbtInitData, const size_t szInitData, nfc_target * pnt); bool (*initiator_poll_target) (nfc_device *pnd, const nfc_modulation * pnmModulations, const size_t szModulations, const uint8_t uiPollNr, const uint8_t btPeriod, nfc_target * pnt); - bool (*initiator_select_dep_target) (nfc_device *pnd, const nfc_dep_mode ndm, const nfc_baud_rate nbr, const nfc_dep_info * pndiInitiator, nfc_target * pnt); + bool (*initiator_select_dep_target) (nfc_device *pnd, const nfc_dep_mode ndm, const nfc_baud_rate nbr, const nfc_dep_info * pndiInitiator, nfc_target * pnt, const int timeout); bool (*initiator_deselect_target) (nfc_device *pnd); bool (*initiator_transceive_bytes) (nfc_device *pnd, const uint8_t * pbtTx, const size_t szTx, uint8_t * pbtRx, size_t * pszRx, int timeout); bool (*initiator_transceive_bits) (nfc_device *pnd, const uint8_t * pbtTx, const size_t szTxBits, const uint8_t * pbtTxPar, uint8_t * pbtRx, size_t * pszRxBits, uint8_t * pbtRxPar); diff --git a/libnfc/nfc.c b/libnfc/nfc.c index 8c4c627..975a7b3 100644 --- a/libnfc/nfc.c +++ b/libnfc/nfc.c @@ -432,9 +432,9 @@ nfc_initiator_poll_target (nfc_device *pnd, bool nfc_initiator_select_dep_target (nfc_device *pnd, const nfc_dep_mode ndm, const nfc_baud_rate nbr, - const nfc_dep_info *pndiInitiator, nfc_target *pnt) + const nfc_dep_info *pndiInitiator, nfc_target *pnt, const int timeout) { - HAL (initiator_select_dep_target, pnd, ndm, nbr, pndiInitiator, pnt); + HAL (initiator_select_dep_target, pnd, ndm, nbr, pndiInitiator, pnt, timeout); } /** diff --git a/test/test_dep.c b/test/test_dep.c index a065ff6..bf81893 100644 --- a/test/test_dep.c +++ b/test/test_dep.c @@ -55,10 +55,10 @@ void * target_thread (void *arg) { intptr_t thread_res = 0; -// nfc_device *device = ((struct thread_data *) arg)->device; + nfc_device *device = ((struct thread_data *) arg)->device; cut_set_current_test_context (((struct thread_data *) arg)->cut_test_context); -#if 0 + printf ("=========== TARGET %s =========\n", nfc_device_name (device)); nfc_target nt = { .nm = { .nmt = NMT_DEP, @@ -69,7 +69,7 @@ target_thread (void *arg) .abtNFCID3 = { 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77, 0x88, 0x99, 0xAA }, .szGB = 4, .abtGB = { 0x12, 0x34, 0x56, 0x78 }, - .ndm = NDM_UNDEFINED, + .ndm = NDM_PASSIVE, /* These bytes are not used by nfc_target_init: the chip will provide them automatically to the initiator */ .btDID = 0x00, .btBS = 0x00, @@ -83,21 +83,46 @@ target_thread (void *arg) uint8_t abtRx[1024]; size_t szRx = sizeof (abtRx); bool res = nfc_target_init (device, &nt, abtRx, &szRx); - // cut_assert_true (res, cut_message ("Can't initialize NFC device as target")); + cut_assert_true (res, cut_message ("Can't initialize NFC device as target: %s", nfc_strerror (device))); - uint8_t abtAtrRes[] = "\x11\xd4\x00\x01\xfe\x12\x34\x56\x78\x90\x12\x00\x00\x00\x00\x00\x00"; - // cut_assert_equal_memory (abtAtrRes, sizeof (abtAtrRes) - 1, abtRx, szRx, cut_message ("Invalid received ATR_RES")); + const uint8_t abtAtrRes[] = "\x11\xd4\x00\x01\xfe\x12\x34\x56\x78\x90\x12\x00\x00\x00\x00\x00\x00"; + cut_assert_equal_memory (abtAtrRes, sizeof (abtAtrRes) - 1, abtRx, szRx, cut_message ("Invalid received ATR_RES")); + if (!res) { thread_res = -1; return (void*) thread_res; } - res = nfc_target_receive_bytes (device, abtRx, &szRx); - // cut_assert_true (res, cut_message ("Can't receive bytes from initiator")); + // First pass + res = nfc_target_receive_bytes (device, abtRx, &szRx, 500); + cut_assert_true (res, cut_message ("Can't receive bytes from initiator: %s", nfc_strerror (device))); - uint8_t abtAttRx[] = "Hello DEP target!"; - // cut_assert_equal_memory (abtAttRx, sizeof (abtAttRx), abtRx, szRx, cut_message ("Invalid received data")); + const uint8_t abtAttRx[] = "Hello DEP target!"; + cut_assert_equal_memory (abtAttRx, sizeof (abtAttRx), abtRx, szRx, cut_message ("Invalid received data")); + if (!res) { thread_res = -1; return (void*) thread_res; } - uint8_t abtTx[] = "Hello DEP initiator!"; - res = nfc_target_send_bytes (device, abtTx, sizeof(abtTx)); - // cut_assert_true (res, cut_message ("Can't send bytes to initiator")); -#endif + const uint8_t abtTx[] = "Hello DEP initiator!"; + res = nfc_target_send_bytes (device, abtTx, sizeof(abtTx), 500); + cut_assert_true (res, cut_message ("Can't send bytes to initiator: %s", nfc_strerror (device))); + if (!res) { thread_res = -1; return (void*) thread_res; } + + // Second pass + res = nfc_target_receive_bytes (device, abtRx, &szRx, 500); + cut_assert_true (res, cut_message ("Can't receive bytes from initiator: %s", nfc_strerror (device))); + + cut_assert_equal_memory (abtAttRx, sizeof (abtAttRx), abtRx, szRx, cut_message ("Invalid received data")); + if (!res) { thread_res = -1; return (void*) thread_res; } + + res = nfc_target_send_bytes (device, abtTx, sizeof(abtTx), 500); + cut_assert_true (res, cut_message ("Can't send bytes to initiator: %s", nfc_strerror (device))); + if (!res) { thread_res = -1; return (void*) thread_res; } + + // Third pass + res = nfc_target_receive_bytes (device, abtRx, &szRx, 500); + cut_assert_true (res, cut_message ("Can't receive bytes from initiator: %s", nfc_strerror (device))); + + cut_assert_equal_memory (abtAttRx, sizeof (abtAttRx), abtRx, szRx, cut_message ("Invalid received data")); + if (!res) { thread_res = -1; return (void*) thread_res; } + + res = nfc_target_send_bytes (device, abtTx, sizeof(abtTx), 500); + cut_assert_true (res, cut_message ("Can't send bytes to initiator: %s", nfc_strerror (device))); + if (!res) { thread_res = -1; return (void*) thread_res; } return (void *) thread_res; } @@ -106,45 +131,90 @@ void * initiator_thread (void *arg) { intptr_t thread_res = 0; -// nfc_device *device = ((struct thread_data *) arg)->device; + nfc_device *device = ((struct thread_data *) arg)->device; cut_set_current_test_context (((struct thread_data *) arg)->cut_test_context); - cut_fail("plop"); - -#if 0 /* * Wait some time for the other thread to initialise NFC device as target */ sleep (1); - printf ("====================================\n"); - printf ("Activating initiator...\n"); + printf ("=========== INITIATOR %s =========\n", nfc_device_name (device)); bool res = nfc_initiator_init (device); - // cut_assert_true (res, cut_message ("Can't initialize NFC device as initiator")); + cut_assert_true (res, cut_message ("Can't initialize NFC device as initiator: %s", nfc_strerror (device))); + if (!res) { thread_res = -1; return (void*) thread_res; } nfc_target nt; // Passive mode / 212Kbps - res = nfc_initiator_select_dep_target (device, NDM_PASSIVE, NBR_212, NULL, &nt); - // cut_assert_true (res, cut_message ("Can't select any DEP target")); - // cut_assert_equal_int (NMT_DEP, nt.nm.nmt, cut_message ("Invalid target modulation")); - // cut_assert_equal_int (NBR_212, nt.nm.nbr, cut_message ("Invalid target baud rate")); - // cut_assert_equal_memory ("\x11\x22\x33\x44\x55\x66\x77\x88\x99\xAA", 10, nt.nti.ndi.abtNFCID3, 10, cut_message ("Invalid target NFCID3")); - // cut_assert_equal_int (NDM_PASSIVE, nt.nti.ndi.ndm, cut_message ("Invalid target DEP mode")); - // cut_assert_equal_memory ("\x12\x34\x56\x78", 4, nt.nti.ndi.abtGB, nt.nti.ndi.szGB, cut_message ("Invalid target general bytes")); + printf ("=========== INITIATOR %s (Passive mode / 212Kbps) =========\n", nfc_device_name (device)); + res = nfc_initiator_select_dep_target (device, NDM_PASSIVE, NBR_212, NULL, &nt, 5000); + cut_assert_true (res, cut_message ("Can't select any DEP target: %s", nfc_strerror (device))); + cut_assert_equal_int (NMT_DEP, nt.nm.nmt, cut_message ("Invalid target modulation")); + cut_assert_equal_int (NBR_212, nt.nm.nbr, cut_message ("Invalid target baud rate")); + cut_assert_equal_memory ("\x11\x22\x33\x44\x55\x66\x77\x88\x99\xAA", 10, nt.nti.ndi.abtNFCID3, 10, cut_message ("Invalid target NFCID3")); + cut_assert_equal_int (NDM_PASSIVE, nt.nti.ndi.ndm, cut_message ("Invalid target DEP mode")); + cut_assert_equal_memory ("\x12\x34\x56\x78", 4, nt.nti.ndi.abtGB, nt.nti.ndi.szGB, cut_message ("Invalid target general bytes")); + if (!res) { thread_res = -1; return (void*) thread_res; } - uint8_t abtTx[] = "Hello DEP target!"; + const uint8_t abtTx[] = "Hello DEP target!"; uint8_t abtRx[1024]; size_t szRx = sizeof (abtRx); - res = nfc_initiator_transceive_bytes (device, abtTx, sizeof (abtTx), abtRx, &szRx); - // cut_assert_true (res, cut_message ("Can't transceive bytes to target")); + res = nfc_initiator_transceive_bytes (device, abtTx, sizeof (abtTx), abtRx, &szRx, 500); + cut_assert_true (res, cut_message ("Can't transceive bytes to target: %s", nfc_strerror (device))); - uint8_t abtAttRx[] = "Hello DEP initiator!"; - // cut_assert_equal_memory (abtAttRx, sizeof (abtAttRx), abtRx, szRx, cut_message ("Invalid received data")); + const uint8_t abtAttRx[] = "Hello DEP initiator!"; + cut_assert_equal_memory (abtAttRx, sizeof (abtAttRx), abtRx, szRx, cut_message ("Invalid received data")); + if (!res) { thread_res = -1; return (void*) thread_res; } res = nfc_initiator_deselect_target (device); - // cut_assert_true (res, cut_message ("Can't deselect target")); -#endif + cut_assert_true (res, cut_message ("Can't deselect target: %s", nfc_strerror (device))); + if (!res) { thread_res = -1; return (void*) thread_res; } + + // Passive mode / 212Kbps (second pass) + printf ("=========== INITIATOR %s (Passive mode / 212Kbps, second pass) =========\n", nfc_device_name (device)); + res = nfc_initiator_select_dep_target (device, NDM_PASSIVE, NBR_212, NULL, &nt, 1000); + cut_assert_true (res, cut_message ("Can't select any DEP target: %s", nfc_strerror (device))); + cut_assert_equal_int (NMT_DEP, nt.nm.nmt, cut_message ("Invalid target modulation")); + cut_assert_equal_int (NBR_212, nt.nm.nbr, cut_message ("Invalid target baud rate")); + cut_assert_equal_memory ("\x11\x22\x33\x44\x55\x66\x77\x88\x99\xAA", 10, nt.nti.ndi.abtNFCID3, 10, cut_message ("Invalid target NFCID3")); + cut_assert_equal_int (NDM_PASSIVE, nt.nti.ndi.ndm, cut_message ("Invalid target DEP mode")); + cut_assert_equal_memory ("\x12\x34\x56\x78", 4, nt.nti.ndi.abtGB, nt.nti.ndi.szGB, cut_message ("Invalid target general bytes")); + if (!res) { thread_res = -1; return (void*) thread_res; } + + szRx = sizeof (abtRx); + res = nfc_initiator_transceive_bytes (device, abtTx, sizeof (abtTx), abtRx, &szRx, 1000); + cut_assert_true (res, cut_message ("Can't transceive bytes to target: %s", nfc_strerror (device))); + + cut_assert_equal_memory (abtAttRx, sizeof (abtAttRx), abtRx, szRx, cut_message ("Invalid received data")); + if (!res) { thread_res = -1; return (void*) thread_res; } + + res = nfc_initiator_deselect_target (device); + cut_assert_true (res, cut_message ("Can't deselect target: %s", nfc_strerror (device))); + if (!res) { thread_res = -1; return (void*) thread_res; } + + // Passive mode / 424Kbps + printf ("=========== INITIATOR %s (Passive mode / 424Kbps) =========\n", nfc_device_name (device)); + res = nfc_initiator_select_dep_target (device, NDM_PASSIVE, NBR_424, NULL, &nt, 1000); + cut_assert_true (res, cut_message ("Can't select any DEP target: %s", nfc_strerror (device))); + cut_assert_equal_int (NMT_DEP, nt.nm.nmt, cut_message ("Invalid target modulation")); + cut_assert_equal_int (NBR_424, nt.nm.nbr, cut_message ("Invalid target baud rate")); + cut_assert_equal_memory ("\x11\x22\x33\x44\x55\x66\x77\x88\x99\xAA", 10, nt.nti.ndi.abtNFCID3, 10, cut_message ("Invalid target NFCID3")); + cut_assert_equal_int (NDM_PASSIVE, nt.nti.ndi.ndm, cut_message ("Invalid target DEP mode")); + cut_assert_equal_memory ("\x12\x34\x56\x78", 4, nt.nti.ndi.abtGB, nt.nti.ndi.szGB, cut_message ("Invalid target general bytes")); + if (!res) { thread_res = -1; return (void*) thread_res; } + + szRx = sizeof (abtRx); + res = nfc_initiator_transceive_bytes (device, abtTx, sizeof (abtTx), abtRx, &szRx, 5000); + cut_assert_true (res, cut_message ("Can't transceive bytes to target: %s", nfc_strerror (device))); + + cut_assert_equal_memory (abtAttRx, sizeof (abtAttRx), abtRx, szRx, cut_message ("Invalid received data")); + if (!res) { thread_res = -1; return (void*) thread_res; } + + res = nfc_initiator_deselect_target (device); + cut_assert_true (res, cut_message ("Can't deselect target: %s", nfc_strerror (device))); + if (!res) { thread_res = -1; return (void*) thread_res; } + return (void *) thread_res; }