add timeout on nfc_target_init() and this function returns now received bytes count on success.

This commit is contained in:
Audrey Diacre 2012-01-05 15:10:11 +00:00
parent c80ebdca25
commit 239fd750c4
14 changed files with 22 additions and 21 deletions

View file

@ -119,7 +119,7 @@ main (int argc, const char *argv[])
print_nfc_target (nt, false); print_nfc_target (nt, false);
printf ("Waiting for initiator request...\n"); 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"); nfc_perror(pnd, "nfc_target_init");
goto error; goto error;
} }

View file

@ -140,7 +140,7 @@ nfc_target_emulate_tag(nfc_device *pnd, nfc_target *pnt)
uint8_t abtTx[MAX_FRAME_LEN]; uint8_t abtTx[MAX_FRAME_LEN];
bool loop = true; 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"); nfc_perror (pnd, "nfc_target_init");
return false; return false;
} }

View file

@ -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"); nfc_perror (pnd, "nfc_target_init");
ERR ("Could not come out of auto-emulation, no command was received"); ERR ("Could not come out of auto-emulation, no command was received");
goto error; goto error;

View file

@ -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"); ERR ("%s", "Initialization of NFC emulator failed");
nfc_disconnect (pndTag); nfc_disconnect (pndTag);
return EXIT_FAILURE; return EXIT_FAILURE;

View file

@ -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 ("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"); 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"); nfc_perror(pnd, "nfc_target_init");
return EXIT_FAILURE; return EXIT_FAILURE;
} }

View file

@ -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_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 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_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_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); NFC_EXPORT int nfc_target_send_bits (nfc_device *pnd, const uint8_t *pbtTx, const size_t szTxBits, const uint8_t *pbtTxPar);

View file

@ -1589,7 +1589,7 @@ pn53x_initiator_deselect_target (struct nfc_device *pnd)
#define SAK_ISO14443_4_COMPLIANT 0x20 #define SAK_ISO14443_4_COMPLIANT 0x20
#define SAK_ISO18092_COMPLIANT 0x40 #define SAK_ISO18092_COMPLIANT 0x40
int 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); pn53x_reset_settings(pnd);
@ -1742,7 +1742,7 @@ pn53x_target_init (struct nfc_device *pnd, nfc_target *pnt, uint8_t *pbtRx, size
while (!targetActivated) { while (!targetActivated) {
uint8_t btActivatedMode; 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; 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 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 *pbtTkt, size_t szTkt,
const uint8_t *pbtFeliCaParams, const uint8_t *pbtFeliCaParams,
const uint8_t *pbtNFCID3t, const uint8_t *pbtGBt, const size_t szGBt, 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 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; size_t szOptionalBytes = 0;
@ -2469,7 +2469,7 @@ pn53x_TgInitAsTarget (struct nfc_device *pnd, pn53x_target_mode ptm,
// Request the initialization as a target // Request the initialization as a target
uint8_t abtRx[PN53x_EXTENDED_FRAME__DATA_MAX_LEN]; uint8_t abtRx[PN53x_EXTENDED_FRAME__DATA_MAX_LEN];
size_t szRx = sizeof (abtRx); 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; return res;
// Note: the first byte is skip: // Note: the first byte is skip:

View file

@ -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); int pn53x_initiator_deselect_target (struct nfc_device *pnd);
// NFC device as Target functions // 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_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_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); 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 *pbtTkt, size_t szTkt,
const uint8_t *pbtFeliCaParams, const uint8_t *pbtFeliCaParams,
const uint8_t *pbtNFCID3t, const uint8_t *pbtGB, const size_t szGB, 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 // RFConfiguration
int pn53x_RFConfiguration__RF_field (struct nfc_device *pnd, bool bEnable); int pn53x_RFConfiguration__RF_field (struct nfc_device *pnd, bool bEnable);

View file

@ -35,7 +35,7 @@ nfc_emulate_target (nfc_device *pnd, struct nfc_emulator *emulator)
uint8_t abtTx[ISO7816_SHORT_C_APDU_MAX_LEN]; uint8_t abtTx[ISO7816_SHORT_C_APDU_MAX_LEN];
int res = 0; 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; return -1;
} }

View file

@ -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_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 (*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_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_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); int (*target_send_bits) (struct nfc_device *pnd, const uint8_t * pbtTx, const size_t szTxBits, const uint8_t * pbtTxPar);

View file

@ -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 * @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 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) * @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] pbtRx Rx buffer pointer
* @param[out] pszRx received bytes count * @param[out] pszRx received bytes count
* @param timeout in milliseconds
* *
* This function initializes NFC device in \e target mode in order to emulate a * This function initializes NFC device in \e target mode in order to emulate a
* tag using the specified \a nfc_target_mode_t. * 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. * receive functions can be used.
*/ */
int 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; int res = 0;
// Disallow invalid frame // 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) if ((res = nfc_device_set_property_bool (pnd, NP_ACTIVATE_FIELD, false)) < 0)
return res; return res;
HAL (target_init, pnd, pnt, pbtRx, pszRx); HAL (target_init, pnd, pnt, pbtRx, pszRx, timeout);
} }
/** /**

View file

@ -84,7 +84,7 @@ target_thread (void *arg)
uint8_t abtRx[1024]; uint8_t abtRx[1024];
size_t szRx = sizeof (abtRx); 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))); 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; } if (res < 0) { thread_res = -1; return (void*) thread_res; }

View file

@ -82,7 +82,7 @@ target_thread (void *arg)
uint8_t abtRx[1024]; uint8_t abtRx[1024];
size_t szRx = sizeof (abtRx); 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))); 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; } if (res < 0) { thread_res = -1; return (void*) thread_res; }

View file

@ -354,7 +354,7 @@ main (int argc, char *argv[])
printf ("Connected to the NFC emulator device: %s\n", nfc_device_get_name (pndTarget)); 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"); ERR ("%s", "Initialization of NFC emulator failed");
if (!target_only_mode) { if (!target_only_mode) {
nfc_disconnect (pndInitiator); nfc_disconnect (pndInitiator);