From 9c1371dcca5202f1abdfd99a31599b85c057f13c Mon Sep 17 00:00:00 2001 From: Audrey Diacre Date: Thu, 22 Dec 2011 15:59:08 +0000 Subject: [PATCH] nfc_target_send_bytes() function returns now sent bytes count on success and libnfc error code on failure. --- examples/nfc-dep-target.c | 2 +- examples/nfc-emulate-tag.c | 2 +- include/nfc/nfc.h | 2 +- libnfc/chips/pn53x.c | 17 +++++++------- libnfc/chips/pn53x.h | 2 +- libnfc/nfc-emulation.c | 2 +- libnfc/nfc-internal.h | 2 +- libnfc/nfc.c | 4 ++-- test/test_dep_active.c | 18 +++++++------- test/test_dep_passive.c | 48 +++++++++++++++++++------------------- utils/nfc-relay-picc.c | 2 +- 11 files changed, 51 insertions(+), 50 deletions(-) diff --git a/examples/nfc-dep-target.c b/examples/nfc-dep-target.c index 9ec6cf9..0597706 100644 --- a/examples/nfc-dep-target.c +++ b/examples/nfc-dep-target.c @@ -133,7 +133,7 @@ main (int argc, const char *argv[]) printf ("Received: %s\n", abtRx); printf ("Sending: %s\n", abtTx); - if (!nfc_target_send_bytes (pnd, abtTx, sizeof(abtTx), 0)) { + if (nfc_target_send_bytes (pnd, abtTx, sizeof(abtTx), 0) < 0) { nfc_perror(pnd, "nfc_target_send_bytes"); goto error; } diff --git a/examples/nfc-emulate-tag.c b/examples/nfc-emulate-tag.c index e69c77c..828bc86 100644 --- a/examples/nfc-emulate-tag.c +++ b/examples/nfc-emulate-tag.c @@ -148,7 +148,7 @@ nfc_target_emulate_tag(nfc_device *pnd, nfc_target *pnt) while ( loop ) { loop = target_io( pnt, abtRx, szRx, abtTx, &szTx ); if (szTx) { - if (!nfc_target_send_bytes(pnd, abtTx, szTx, 0)) { + if (nfc_target_send_bytes(pnd, abtTx, szTx, 0) < 0) { nfc_perror (pnd, "nfc_target_send_bytes"); return false; } diff --git a/include/nfc/nfc.h b/include/nfc/nfc.h index 3c61e36..d02f451 100644 --- a/include/nfc/nfc.h +++ b/include/nfc/nfc.h @@ -84,7 +84,7 @@ extern "C" { /* 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 bool 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 bool nfc_target_send_bits (nfc_device *pnd, const uint8_t *pbtTx, const size_t szTxBits, const uint8_t *pbtTxPar); NFC_EXPORT bool nfc_target_receive_bits (nfc_device *pnd, uint8_t *pbtRx, size_t *pszRxBits, uint8_t *pbtRxPar); diff --git a/libnfc/chips/pn53x.c b/libnfc/chips/pn53x.c index 71a4065..1ef18d5 100644 --- a/libnfc/chips/pn53x.c +++ b/libnfc/chips/pn53x.c @@ -1866,14 +1866,15 @@ pn53x_target_send_bits (struct nfc_device *pnd, const uint8_t *pbtTx, const size return true; } -bool +int pn53x_target_send_bytes (struct nfc_device *pnd, const uint8_t *pbtTx, const size_t szTx, int timeout) { uint8_t abtCmd[PN53x_EXTENDED_FRAME__DATA_MAX_LEN]; + int res = 0; // We can not just send bytes without parity if while the PN53X expects we handled them if (!pnd->bPar) - return false; + return NFC_ECHIP; // XXX I think this is not a clean way to provide some kind of "EasyFraming" // but at the moment I have no more better than this @@ -1892,7 +1893,7 @@ pn53x_target_send_bytes (struct nfc_device *pnd, const uint8_t *pbtTx, const siz } else { // TODO Support EasyFraming for other cases by software pnd->last_error = NFC_ENOTIMPL; - return false; + return pnd->last_error; } } default: @@ -1905,13 +1906,13 @@ pn53x_target_send_bytes (struct nfc_device *pnd, const uint8_t *pbtTx, const siz // Copy the data into the command frame memcpy (abtCmd + 1, pbtTx, szTx); - + // Try to send the bits to the reader - if (pn53x_transceive (pnd, abtCmd, szTx + 1, NULL, NULL, timeout) < 0) - return false; + if ((res = pn53x_transceive (pnd, abtCmd, szTx + 1, NULL, NULL, timeout)) < 0) + return res; - // Everyting seems ok, return true - return true; + // Everyting seems ok, return sent byte count + return szTx; } static struct sErrorMessage { diff --git a/libnfc/chips/pn53x.h b/libnfc/chips/pn53x.h index 6628d9e..aa33453 100644 --- a/libnfc/chips/pn53x.h +++ b/libnfc/chips/pn53x.h @@ -316,7 +316,7 @@ int pn53x_target_init (struct nfc_device *pnd, nfc_target *pnt, uint8_t *pbtR bool 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); bool pn53x_target_send_bits (struct nfc_device *pnd, const uint8_t *pbtTx, const size_t szTxBits, const uint8_t *pbtTxPar); -bool pn53x_target_send_bytes (struct nfc_device *pnd, const uint8_t *pbtTx, const size_t szTx, int timeout); +int pn53x_target_send_bytes (struct nfc_device *pnd, const uint8_t *pbtTx, const size_t szTx, int timeout); // Error handling functions const char *pn53x_strerror (const struct nfc_device *pnd); diff --git a/libnfc/nfc-emulation.c b/libnfc/nfc-emulation.c index a5ed90c..287632c 100644 --- a/libnfc/nfc-emulation.c +++ b/libnfc/nfc-emulation.c @@ -42,7 +42,7 @@ nfc_emulate_target (nfc_device *pnd, struct nfc_emulator *emulator) while (res >= 0) { res = emulator->state_machine->io (emulator, abtRx, szRx, abtTx, sizeof (abtTx)); if (res > 0) { - if (!nfc_target_send_bytes(pnd, abtTx, res, 0)) { + if (nfc_target_send_bytes(pnd, abtTx, res, 0) < 0) { return -1; } } diff --git a/libnfc/nfc-internal.h b/libnfc/nfc-internal.h index 2304b2c..359140b 100644 --- a/libnfc/nfc-internal.h +++ b/libnfc/nfc-internal.h @@ -143,7 +143,7 @@ struct nfc_driver_t { bool (*initiator_transceive_bits_timed) (struct nfc_device *pnd, const uint8_t * pbtTx, const size_t szTxBits, const uint8_t * pbtTxPar, uint8_t * pbtRx, size_t * pszRxBits, uint8_t * pbtRxPar, uint32_t * cycles); int (*target_init) (struct nfc_device *pnd, nfc_target * pnt, uint8_t * pbtRx, size_t * pszRx); - bool (*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); bool (*target_send_bits) (struct nfc_device *pnd, const uint8_t * pbtTx, const size_t szTxBits, const uint8_t * pbtTxPar); bool (*target_receive_bits) (struct nfc_device *pnd, uint8_t * pbtRx, size_t * pszRxBits, uint8_t * pbtRxPar); diff --git a/libnfc/nfc.c b/libnfc/nfc.c index 1cbad87..c6d40d4 100644 --- a/libnfc/nfc.c +++ b/libnfc/nfc.c @@ -695,7 +695,7 @@ nfc_abort_command (nfc_device *pnd) /** * @brief Send bytes and APDU frames - * @return Returns \c true if action was successfully performed; otherwise returns \c false. + * @return Returns sent bytes count on success, otherwise returns libnfc's error code * * @param pnd \a nfc_device struct pointer that represent currently used device * @param pbtTx pointer to Tx buffer @@ -708,7 +708,7 @@ nfc_abort_command (nfc_device *pnd) * If timeout is not a null pointer, it specifies the maximum interval to wait for the function to be executed. * If timeout is a null pointer, the function blocks indefinitely (until an error is raised or function is completed). */ -bool +int nfc_target_send_bytes (nfc_device *pnd, const uint8_t *pbtTx, const size_t szTx, int timeout) { HAL (target_send_bytes, pnd, pbtTx, szTx, timeout); diff --git a/test/test_dep_active.c b/test/test_dep_active.c index 1bc5cae..c2af320 100644 --- a/test/test_dep_active.c +++ b/test/test_dep_active.c @@ -84,21 +84,21 @@ target_thread (void *arg) uint8_t abtRx[1024]; size_t szRx = sizeof (abtRx); - int ires = nfc_target_init (device, &nt, abtRx, &szRx); - cut_assert_equal_int (0, ires, cut_message ("Can't initialize NFC device as target: %s", nfc_strerror (device))); - if (ires < 0) { thread_res = -1; return (void*) thread_res; } + int res = nfc_target_init (device, &nt, abtRx, &szRx); + 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; } - ires = nfc_target_receive_bytes (device, abtRx, &szRx, 500); - cut_assert_operator_int (ires, >, 0, cut_message ("Can't receive bytes from initiator: %s", nfc_strerror (device))); + res = nfc_target_receive_bytes (device, abtRx, &szRx, 500); + cut_assert_operator_int (res, >, 0, cut_message ("Can't receive bytes from initiator: %s", nfc_strerror (device))); const uint8_t abtAttRx[] = "Hello DEP target!"; cut_assert_equal_memory (abtAttRx, sizeof (abtAttRx), abtRx, szRx, cut_message ("Invalid received data")); - if (ires <= 0) { thread_res = -1; return (void*) thread_res; } + if (res <= 0) { thread_res = -1; return (void*) thread_res; } const uint8_t abtTx[] = "Hello DEP initiator!"; - bool 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; } + res = nfc_target_send_bytes (device, abtTx, sizeof(abtTx), 500); + cut_assert_operator_int (res, >, 0, cut_message ("Can't send bytes to initiator: %s", nfc_strerror (device))); + if (res <= 0) { thread_res = -1; return (void*) thread_res; } return (void *) thread_res; } diff --git a/test/test_dep_passive.c b/test/test_dep_passive.c index 32d4653..442c911 100644 --- a/test/test_dep_passive.c +++ b/test/test_dep_passive.c @@ -82,55 +82,55 @@ target_thread (void *arg) uint8_t abtRx[1024]; size_t szRx = sizeof (abtRx); - int ires = nfc_target_init (device, &nt, abtRx, &szRx); - cut_assert_equal_int (0, ires, cut_message ("Can't initialize NFC device as target: %s", nfc_strerror (device))); - if (ires < 0) { thread_res = -1; return (void*) thread_res; } + int res = nfc_target_init (device, &nt, abtRx, &szRx); + 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; } // First pass - ires = nfc_target_receive_bytes (device, abtRx, &szRx, 500); - cut_assert_operator_int (ires, >, 0, cut_message ("Can't receive bytes from initiator: %s", nfc_strerror (device))); + res = nfc_target_receive_bytes (device, abtRx, &szRx, 500); + cut_assert_operator_int (res, >, 0, cut_message ("Can't receive bytes from initiator: %s", nfc_strerror (device))); const uint8_t abtAttRx[] = "Hello DEP target!"; cut_assert_equal_memory (abtAttRx, sizeof (abtAttRx), abtRx, szRx, cut_message ("Invalid received data")); - if (ires <= 0) { thread_res = -1; return (void*) thread_res; } + if (res <= 0) { thread_res = -1; return (void*) thread_res; } const uint8_t abtTx[] = "Hello DEP initiator!"; - bool 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; } + res = nfc_target_send_bytes (device, abtTx, sizeof(abtTx), 500); + cut_assert_operator_int (res, >, 0, cut_message ("Can't send bytes to initiator: %s", nfc_strerror (device))); + if (res <= 0) { thread_res = -1; return (void*) thread_res; } // Second pass - ires = nfc_target_receive_bytes (device, abtRx, &szRx, 500); - cut_assert_operator_int (ires, >, 0, cut_message ("Can't receive bytes from initiator: %s", nfc_strerror (device))); + res = nfc_target_receive_bytes (device, abtRx, &szRx, 500); + cut_assert_operator_int (res, >, 0, 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 (ires <= 0) { thread_res = -1; return (void*) thread_res; } + if (res <= 0) { 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; } + cut_assert_operator_int (res, >, 0, cut_message ("Can't send bytes to initiator: %s", nfc_strerror (device))); + if (res <= 0) { thread_res = -1; return (void*) thread_res; } // Third pass - ires = nfc_target_receive_bytes (device, abtRx, &szRx, 500); - cut_assert_operator_int (ires, >, 0, cut_message ("Can't receive bytes from initiator: %s", nfc_strerror (device))); + res = nfc_target_receive_bytes (device, abtRx, &szRx, 500); + cut_assert_operator_int (res, >, 0, 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 (ires <= 0) { thread_res = -1; return (void*) thread_res; } + if (res <= 0) { 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; } + cut_assert_operator_int (res, >, 0, cut_message ("Can't send bytes to initiator: %s", nfc_strerror (device))); + if (res <= 0) { thread_res = -1; return (void*) thread_res; } // Fourth pass - ires = nfc_target_receive_bytes (device, abtRx, &szRx, 500); - cut_assert_operator_int (ires, >, 0, cut_message ("Can't receive bytes from initiator: %s", nfc_strerror (device))); + res = nfc_target_receive_bytes (device, abtRx, &szRx, 500); + cut_assert_operator_int (res, >, 0, 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 (ires <= 0) { thread_res = -1; return (void*) thread_res; } + if (res <= 0) { 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; } + cut_assert_operator_int (res, >, 0, cut_message ("Can't send bytes to initiator: %s", nfc_strerror (device))); + if (res <= 0) { thread_res = -1; return (void*) thread_res; } return (void *) thread_res; } diff --git a/utils/nfc-relay-picc.c b/utils/nfc-relay-picc.c index 18e3544..234ea73 100644 --- a/utils/nfc-relay-picc.c +++ b/utils/nfc-relay-picc.c @@ -425,7 +425,7 @@ main (int argc, char *argv[]) } if (!initiator_only_mode) { // Transmit the response bytes - if (!nfc_target_send_bytes(pndTarget, abtRapdu, szRapduLen, 0)) { + if (nfc_target_send_bytes(pndTarget, abtRapdu, szRapduLen, 0) < 0) { nfc_perror (pndTarget, "nfc_target_send_bytes"); if (!target_only_mode) { nfc_disconnect (pndInitiator);