From cd2b03a08a4f73004c32031b358b48e99044af5f Mon Sep 17 00:00:00 2001 From: Romuald Conty Date: Wed, 30 Jan 2013 00:10:24 +0100 Subject: [PATCH] Add timeout parameter to nfc_emulate_target() function. Fixes issue 219. Thanks to raidolinja. --- ChangeLog | 6 ++++++ NEWS | 7 +++++++ examples/nfc-emulate-forum-tag2.c | 2 +- include/nfc/nfc-emulation.h | 2 +- libnfc/nfc-emulation.c | 25 ++++++++++++++++++------- utils/nfc-emulate-forum-tag4.c | 2 +- 6 files changed, 34 insertions(+), 10 deletions(-) diff --git a/ChangeLog b/ChangeLog index da1a693..0c837f1 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,3 +1,9 @@ +Jan TBD, 2013 - 1.7.0-rc3 (release candidate) +-------------------------------------------- + +Changes: + - nfc_emulate_target() now takes timeout parameter + Jan 20, 2013 - 1.7.0-rc2 (release candidate) -------------------------------------------- diff --git a/NEWS b/NEWS index 62f7aa0..055aa98 100644 --- a/NEWS +++ b/NEWS @@ -1,3 +1,10 @@ +New in 1.7.0-rc3: + +API Changes: + + * Functions + - Add timeout param to nfc_emulate_target() + New in 1.7.0-rc2: Configuration: diff --git a/examples/nfc-emulate-forum-tag2.c b/examples/nfc-emulate-forum-tag2.c index 325e256..8240a4e 100644 --- a/examples/nfc-emulate-forum-tag2.c +++ b/examples/nfc-emulate-forum-tag2.c @@ -197,7 +197,7 @@ main(int argc, char *argv[]) printf("NFC device: %s opened\n", nfc_device_get_name(pnd)); printf("Emulating NDEF tag now, please touch it with a second NFC device\n"); - if (nfc_emulate_target(pnd, &emulator) < 0) { + if (nfc_emulate_target(pnd, &emulator, 0) < 0) { goto error; } diff --git a/include/nfc/nfc-emulation.h b/include/nfc/nfc-emulation.h index 1a8576f..430f5b1 100644 --- a/include/nfc/nfc-emulation.h +++ b/include/nfc/nfc-emulation.h @@ -55,7 +55,7 @@ extern "C" { void *data; }; - NFC_EXPORT int nfc_emulate_target(nfc_device *pnd, struct nfc_emulator *emulator); + NFC_EXPORT int nfc_emulate_target(nfc_device *pnd, struct nfc_emulator *emulator, const int timeout); #ifdef __cplusplus } diff --git a/libnfc/nfc-emulation.c b/libnfc/nfc-emulation.c index 1085e32..e98d625 100644 --- a/libnfc/nfc-emulation.c +++ b/libnfc/nfc-emulation.c @@ -19,23 +19,33 @@ */ /** -* @file nfc-emulation.c -* @brief Provide a small API to ease emulation in libnfc -*/ + * @file nfc-emulation.c + * @brief Provide a small API to ease emulation in libnfc + */ #include #include #include "iso7816.h" +/** @ingroup emulation + * @brief Emulate a target + * @return Returns 0 on success, otherwise returns libnfc's error code (negative value). + * + * @param pnd \a nfc_device struct pointer that represents currently used device + * @param emulator \nfc_emulator struct point that handles input/output functions + * + * If timeout equals to 0, the function blocks indefinitely (until an error is raised or function is completed) + * If timeout equals to -1, the default timeout will be used + */ int -nfc_emulate_target(nfc_device *pnd, struct nfc_emulator *emulator) +nfc_emulate_target(nfc_device *pnd, struct nfc_emulator *emulator, const int timeout) { uint8_t abtRx[ISO7816_SHORT_R_APDU_MAX_LEN]; uint8_t abtTx[ISO7816_SHORT_C_APDU_MAX_LEN]; int res; - if ((res = nfc_target_init(pnd, emulator->target, abtRx, sizeof(abtRx), 0)) < 0) { + if ((res = nfc_target_init(pnd, emulator->target, abtRx, sizeof(abtRx), timeout)) < 0) { return res; } @@ -44,12 +54,12 @@ nfc_emulate_target(nfc_device *pnd, struct nfc_emulator *emulator) while (io_res >= 0) { io_res = emulator->state_machine->io(emulator, abtRx, szRx, abtTx, sizeof(abtTx)); if (io_res > 0) { - if ((res = nfc_target_send_bytes(pnd, abtTx, io_res, 0)) < 0) { + if ((res = nfc_target_send_bytes(pnd, abtTx, io_res, timeout)) < 0) { return res; } } if (io_res >= 0) { - if ((res = nfc_target_receive_bytes(pnd, abtRx, sizeof(abtRx), 0)) < 0) { + if ((res = nfc_target_receive_bytes(pnd, abtRx, sizeof(abtRx), timeout)) < 0) { return res; } szRx = res; @@ -57,3 +67,4 @@ nfc_emulate_target(nfc_device *pnd, struct nfc_emulator *emulator) } return (io_res < 0) ? io_res : 0; } + diff --git a/utils/nfc-emulate-forum-tag4.c b/utils/nfc-emulate-forum-tag4.c index 8404cfb..e17f090 100644 --- a/utils/nfc-emulate-forum-tag4.c +++ b/utils/nfc-emulate-forum-tag4.c @@ -382,7 +382,7 @@ main(int argc, char *argv[]) printf("NFC device: %s opened\n", nfc_device_get_name(pnd)); printf("Emulating NDEF tag now, please touch it with a second NFC device\n"); - if (0 != nfc_emulate_target(pnd, &emulator)) { // contains already nfc_target_init() call + if (0 != nfc_emulate_target(pnd, &emulator, 0)) { // contains already nfc_target_init() call nfc_perror(pnd, "nfc_emulate_target"); }