diff --git a/examples/nfc-dep-target.c b/examples/nfc-dep-target.c index 0597706..ad2e275 100644 --- a/examples/nfc-dep-target.c +++ b/examples/nfc-dep-target.c @@ -119,7 +119,7 @@ main (int argc, const char *argv[]) print_nfc_target (nt, false); printf ("Waiting for initiator request...\n"); - if(nfc_target_init (pnd, &nt, abtRx, &szRx) < 0) { + if(nfc_target_init (pnd, &nt, abtRx, &szRx, 0) < 0) { nfc_perror(pnd, "nfc_target_init"); goto error; } diff --git a/examples/nfc-emulate-tag.c b/examples/nfc-emulate-tag.c index 828bc86..9936a7d 100644 --- a/examples/nfc-emulate-tag.c +++ b/examples/nfc-emulate-tag.c @@ -140,7 +140,7 @@ nfc_target_emulate_tag(nfc_device *pnd, nfc_target *pnt) uint8_t abtTx[MAX_FRAME_LEN]; bool loop = true; - if (nfc_target_init (pnd, pnt, abtRx, &szRx) < 0) { + if (nfc_target_init (pnd, pnt, abtRx, &szRx, 0) < 0) { nfc_perror (pnd, "nfc_target_init"); return false; } diff --git a/examples/nfc-emulate-uid.c b/examples/nfc-emulate-uid.c index 90c01aa..ffd7bc2 100644 --- a/examples/nfc-emulate-uid.c +++ b/examples/nfc-emulate-uid.c @@ -155,7 +155,7 @@ main (int argc, char *argv[]) }, }, }; - if (nfc_target_init (pnd, &nt, abtRecv, &szRecvBits) < 0) { + if (nfc_target_init (pnd, &nt, abtRecv, &szRecvBits, 0) < 0) { nfc_perror (pnd, "nfc_target_init"); ERR ("Could not come out of auto-emulation, no command was received"); goto error; diff --git a/examples/nfc-relay.c b/examples/nfc-relay.c index 677e8d9..992f698 100644 --- a/examples/nfc-relay.c +++ b/examples/nfc-relay.c @@ -146,7 +146,7 @@ main (int argc, char *argv[]) }, }; - if (nfc_target_init (pndTag, &nt, abtReaderRx, &szReaderRxBits) < 0) { + if (nfc_target_init (pndTag, &nt, abtReaderRx, &szReaderRxBits, 0) < 0) { ERR ("%s", "Initialization of NFC emulator failed"); nfc_disconnect (pndTag); return EXIT_FAILURE; diff --git a/examples/pn53x-sam.c b/examples/pn53x-sam.c index 713cb58..023a2dd 100644 --- a/examples/pn53x-sam.c +++ b/examples/pn53x-sam.c @@ -176,7 +176,7 @@ main (int argc, const char *argv[]) }; printf ("Now both, NFC device (configured as target) and SAM are readables from an external NFC initiator.\n"); printf ("Please note that NFC device (configured as target) stay in target mode until it receive RATS, ATR_REQ or proprietary command.\n"); - if (nfc_target_init (pnd, &nt, abtRx, &szRx) < 0) { + if (nfc_target_init (pnd, &nt, abtRx, &szRx, 0) < 0) { nfc_perror(pnd, "nfc_target_init"); return EXIT_FAILURE; } diff --git a/include/nfc/nfc.h b/include/nfc/nfc.h index 0f05a02..9bcff7d 100644 --- a/include/nfc/nfc.h +++ b/include/nfc/nfc.h @@ -83,7 +83,7 @@ extern "C" { NFC_EXPORT int nfc_initiator_transceive_bits_timed (nfc_device *pnd, const uint8_t *pbtTx, const size_t szTxBits, const uint8_t *pbtTxPar, uint8_t *pbtRx, uint8_t *pbtRxPar, uint32_t *cycles); /* NFC target: act as tag (i.e. MIFARE Classic) or NFC target device. */ - NFC_EXPORT int nfc_target_init (nfc_device *pnd, nfc_target *pnt, uint8_t *pbtRx, size_t *pszRx); + NFC_EXPORT int nfc_target_init (nfc_device *pnd, nfc_target *pnt, uint8_t *pbtRx, size_t *pszRx, int timeout); NFC_EXPORT int nfc_target_send_bytes (nfc_device *pnd, const uint8_t *pbtTx, const size_t szTx, int timeout); NFC_EXPORT int nfc_target_receive_bytes (nfc_device *pnd, uint8_t *pbtRx, size_t *pszRx, int timeout); NFC_EXPORT int nfc_target_send_bits (nfc_device *pnd, const uint8_t *pbtTx, const size_t szTxBits, const uint8_t *pbtTxPar); diff --git a/libnfc/chips/pn53x.c b/libnfc/chips/pn53x.c index 99b2e6f..8d02d33 100644 --- a/libnfc/chips/pn53x.c +++ b/libnfc/chips/pn53x.c @@ -1589,7 +1589,7 @@ pn53x_initiator_deselect_target (struct nfc_device *pnd) #define SAK_ISO14443_4_COMPLIANT 0x20 #define SAK_ISO18092_COMPLIANT 0x40 int -pn53x_target_init (struct nfc_device *pnd, nfc_target *pnt, uint8_t *pbtRx, size_t *pszRx) +pn53x_target_init (struct nfc_device *pnd, nfc_target *pnt, uint8_t *pbtRx, size_t *pszRx, int timeout) { pn53x_reset_settings(pnd); @@ -1742,7 +1742,7 @@ pn53x_target_init (struct nfc_device *pnd, nfc_target *pnt, uint8_t *pbtRx, size while (!targetActivated) { uint8_t btActivatedMode; - if((res = pn53x_TgInitAsTarget(pnd, ptm, pbtMifareParams, pbtTkt, szTkt, pbtFeliCaParams, pbtNFCID3t, pbtGBt, szGBt, pbtRx, pszRx, &btActivatedMode)) < 0) { + if((res = pn53x_TgInitAsTarget(pnd, ptm, pbtMifareParams, pbtTkt, szTkt, pbtFeliCaParams, pbtNFCID3t, pbtGBt, szGBt, pbtRx, pszRx, &btActivatedMode, timeout)) < 0) { return res; } @@ -1807,7 +1807,7 @@ pn53x_target_init (struct nfc_device *pnd, nfc_target *pnt, uint8_t *pbtRx, size } } - return NFC_SUCCESS; + return *pszRx; } int @@ -2420,7 +2420,7 @@ pn53x_TgInitAsTarget (struct nfc_device *pnd, pn53x_target_mode ptm, const uint8_t *pbtTkt, size_t szTkt, const uint8_t *pbtFeliCaParams, const uint8_t *pbtNFCID3t, const uint8_t *pbtGBt, const size_t szGBt, - uint8_t *pbtRx, size_t *pszRx, uint8_t *pbtModeByte) + uint8_t *pbtRx, size_t *pszRx, uint8_t *pbtModeByte, int timeout) { uint8_t abtCmd[39 + 47 + 48] = { TgInitAsTarget }; // Worst case: 39-byte base, 47 bytes max. for General Bytes, 48 bytes max. for Historical Bytes size_t szOptionalBytes = 0; @@ -2469,7 +2469,7 @@ pn53x_TgInitAsTarget (struct nfc_device *pnd, pn53x_target_mode ptm, // Request the initialization as a target uint8_t abtRx[PN53x_EXTENDED_FRAME__DATA_MAX_LEN]; size_t szRx = sizeof (abtRx); - if ((res = pn53x_transceive (pnd, abtCmd, 36 + szOptionalBytes, abtRx, &szRx, -1)) < 0) + if ((res = pn53x_transceive (pnd, abtCmd, 36 + szOptionalBytes, abtRx, &szRx, timeout)) < 0) return res; // Note: the first byte is skip: diff --git a/libnfc/chips/pn53x.h b/libnfc/chips/pn53x.h index e1b07a5..0e6af42 100644 --- a/libnfc/chips/pn53x.h +++ b/libnfc/chips/pn53x.h @@ -308,7 +308,7 @@ int pn53x_initiator_transceive_bytes_timed (struct nfc_device *pnd, const uin int pn53x_initiator_deselect_target (struct nfc_device *pnd); // NFC device as Target functions -int pn53x_target_init (struct nfc_device *pnd, nfc_target *pnt, uint8_t *pbtRx, size_t *pszRx); +int pn53x_target_init (struct nfc_device *pnd, nfc_target *pnt, uint8_t *pbtRx, size_t *pszRx, int timeout); int pn53x_target_receive_bits (struct nfc_device *pnd, uint8_t *pbtRx, size_t *pszRxBits, uint8_t *pbtRxPar); int pn53x_target_receive_bytes (struct nfc_device *pnd, uint8_t *pbtRx, size_t *pszRx, int timeout); int pn53x_target_send_bits (struct nfc_device *pnd, const uint8_t *pbtTx, const size_t szTxBits, const uint8_t *pbtTxPar); @@ -342,7 +342,7 @@ int pn53x_TgInitAsTarget (struct nfc_device *pnd, pn53x_target_mode ptm, const uint8_t *pbtTkt, size_t szTkt, const uint8_t *pbtFeliCaParams, const uint8_t *pbtNFCID3t, const uint8_t *pbtGB, const size_t szGB, - uint8_t *pbtRx, size_t *pszRx, uint8_t *pbtModeByte); + uint8_t *pbtRx, size_t *pszRx, uint8_t *pbtModeByte, int timeout); // RFConfiguration int pn53x_RFConfiguration__RF_field (struct nfc_device *pnd, bool bEnable); diff --git a/libnfc/nfc-emulation.c b/libnfc/nfc-emulation.c index 287632c..435363c 100644 --- a/libnfc/nfc-emulation.c +++ b/libnfc/nfc-emulation.c @@ -35,7 +35,7 @@ nfc_emulate_target (nfc_device *pnd, struct nfc_emulator *emulator) uint8_t abtTx[ISO7816_SHORT_C_APDU_MAX_LEN]; int res = 0; - if (nfc_target_init (pnd, emulator->target, abtRx, &szRx) < 0) { + if (nfc_target_init (pnd, emulator->target, abtRx, &szRx, 0) < 0) { return -1; } diff --git a/libnfc/nfc-internal.h b/libnfc/nfc-internal.h index fd15dba..cab8581 100644 --- a/libnfc/nfc-internal.h +++ b/libnfc/nfc-internal.h @@ -142,7 +142,7 @@ struct nfc_driver_t { int (*initiator_transceive_bytes_timed) (struct nfc_device *pnd, const uint8_t * pbtTx, const size_t szTx, uint8_t * pbtRx, uint32_t * cycles); int (*initiator_transceive_bits_timed) (struct nfc_device *pnd, const uint8_t * pbtTx, const size_t szTxBits, const uint8_t * pbtTxPar, uint8_t * pbtRx, uint8_t * pbtRxPar, uint32_t * cycles); - int (*target_init) (struct nfc_device *pnd, nfc_target * pnt, uint8_t * pbtRx, size_t * pszRx); + int (*target_init) (struct nfc_device *pnd, nfc_target * pnt, uint8_t * pbtRx, size_t * pszRx, int timeout); int (*target_send_bytes) (struct nfc_device *pnd, const uint8_t * pbtTx, const size_t szTx, int timeout); int (*target_receive_bytes) (struct nfc_device *pnd, uint8_t * pbtRx, size_t * pszRx, int timeout); int (*target_send_bits) (struct nfc_device *pnd, const uint8_t * pbtTx, const size_t szTxBits, const uint8_t * pbtTxPar); diff --git a/libnfc/nfc.c b/libnfc/nfc.c index a042b05..da683c5 100644 --- a/libnfc/nfc.c +++ b/libnfc/nfc.c @@ -599,7 +599,7 @@ nfc_initiator_transceive_bits_timed (nfc_device *pnd, const uint8_t *pbtTx, cons /** * @brief Initialize NFC device as an emulated tag - * @return Returns 0 on success, otherwise returns libnfc's error code + * @return Returns received bytes count on success, otherwise returns libnfc's error code * * @param pnd \a nfc_device struct pointer that represent currently used device * @param ntm target mode restriction that you want to emulate (eg. NTM_PASSIVE_ONLY) @@ -610,6 +610,7 @@ nfc_initiator_transceive_bits_timed (nfc_device *pnd, const uint8_t *pbtTx, cons * * @param[out] pbtRx Rx buffer pointer * @param[out] pszRx received bytes count + * @param timeout in milliseconds * * This function initializes NFC device in \e target mode in order to emulate a * tag using the specified \a nfc_target_mode_t. @@ -628,7 +629,7 @@ nfc_initiator_transceive_bits_timed (nfc_device *pnd, const uint8_t *pbtTx, cons * receive functions can be used. */ int -nfc_target_init (nfc_device *pnd, nfc_target *pnt, uint8_t *pbtRx, size_t * pszRx) +nfc_target_init (nfc_device *pnd, nfc_target *pnt, uint8_t *pbtRx, size_t * pszRx, int timeout) { int res = 0; // Disallow invalid frame @@ -655,7 +656,7 @@ nfc_target_init (nfc_device *pnd, nfc_target *pnt, uint8_t *pbtRx, size_t * pszR if ((res = nfc_device_set_property_bool (pnd, NP_ACTIVATE_FIELD, false)) < 0) return res; - HAL (target_init, pnd, pnt, pbtRx, pszRx); + HAL (target_init, pnd, pnt, pbtRx, pszRx, timeout); } /** diff --git a/test/test_dep_active.c b/test/test_dep_active.c index c2af320..85d992b 100644 --- a/test/test_dep_active.c +++ b/test/test_dep_active.c @@ -84,7 +84,7 @@ target_thread (void *arg) uint8_t abtRx[1024]; size_t szRx = sizeof (abtRx); - int res = nfc_target_init (device, &nt, abtRx, &szRx); + int res = nfc_target_init (device, &nt, abtRx, &szRx, 0); cut_assert_equal_int (0, res, cut_message ("Can't initialize NFC device as target: %s", nfc_strerror (device))); if (res < 0) { thread_res = -1; return (void*) thread_res; } diff --git a/test/test_dep_passive.c b/test/test_dep_passive.c index 442c911..9db5151 100644 --- a/test/test_dep_passive.c +++ b/test/test_dep_passive.c @@ -82,7 +82,7 @@ target_thread (void *arg) uint8_t abtRx[1024]; size_t szRx = sizeof (abtRx); - int res = nfc_target_init (device, &nt, abtRx, &szRx); + int res = nfc_target_init (device, &nt, abtRx, &szRx, 0); cut_assert_equal_int (0, res, cut_message ("Can't initialize NFC device as target: %s", nfc_strerror (device))); if (res < 0) { thread_res = -1; return (void*) thread_res; } diff --git a/utils/nfc-relay-picc.c b/utils/nfc-relay-picc.c index 234ea73..3c5caf5 100644 --- a/utils/nfc-relay-picc.c +++ b/utils/nfc-relay-picc.c @@ -354,7 +354,7 @@ main (int argc, char *argv[]) printf ("Connected to the NFC emulator device: %s\n", nfc_device_get_name (pndTarget)); - if (nfc_target_init (pndTarget, &ntEmulatedTarget, abtCapdu, &szCapduLen) < 0) { + if (nfc_target_init (pndTarget, &ntEmulatedTarget, abtCapdu, &szCapduLen, 0) < 0) { ERR ("%s", "Initialization of NFC emulator failed"); if (!target_only_mode) { nfc_disconnect (pndInitiator);