diff --git a/examples/nfc-dep-target.c b/examples/nfc-dep-target.c index 28a7fd3..9ec6cf9 100644 --- a/examples/nfc-dep-target.c +++ b/examples/nfc-dep-target.c @@ -125,7 +125,7 @@ main (int argc, const char *argv[]) } printf("Initiator request received. Waiting for data...\n"); - if (!nfc_target_receive_bytes (pnd, abtRx, &szRx, 0)) { + if (nfc_target_receive_bytes (pnd, abtRx, &szRx, 0) < 0) { nfc_perror(pnd, "nfc_target_receive_bytes"); goto error; } diff --git a/examples/nfc-emulate-tag.c b/examples/nfc-emulate-tag.c index 8ef6db9..e69c77c 100644 --- a/examples/nfc-emulate-tag.c +++ b/examples/nfc-emulate-tag.c @@ -158,7 +158,7 @@ nfc_target_emulate_tag(nfc_device *pnd, nfc_target *pnt) nfc_device_set_property_bool (pnd, NP_HANDLE_CRC, false); init_mfc_auth = false; } - if (!nfc_target_receive_bytes(pnd, abtRx, &szRx, 0)) { + if (nfc_target_receive_bytes(pnd, abtRx, &szRx, 0) < 0) { nfc_perror (pnd, "nfc_target_receive_bytes"); return false; } diff --git a/include/nfc/nfc.h b/include/nfc/nfc.h index 579a98f..3c61e36 100644 --- a/include/nfc/nfc.h +++ b/include/nfc/nfc.h @@ -85,7 +85,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 bool 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 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 dfae861..71a4065 100644 --- a/libnfc/chips/pn53x.c +++ b/libnfc/chips/pn53x.c @@ -1779,7 +1779,7 @@ pn53x_target_receive_bits (struct nfc_device *pnd, uint8_t *pbtRx, size_t *pszRx return true; } -bool +int pn53x_target_receive_bytes (struct nfc_device *pnd, uint8_t *pbtRx, size_t *pszRx, int timeout) { uint8_t abtCmd[1]; @@ -1801,7 +1801,7 @@ pn53x_target_receive_bytes (struct nfc_device *pnd, uint8_t *pbtRx, size_t *pszR } else { // TODO Support EasyFraming for other cases by software pnd->last_error = NFC_ENOTIMPL; - return false; + return pnd->last_error; } } default: @@ -1816,7 +1816,7 @@ pn53x_target_receive_bytes (struct nfc_device *pnd, uint8_t *pbtRx, size_t *pszR uint8_t abtRx[PN53x_EXTENDED_FRAME__DATA_MAX_LEN]; size_t szRx = sizeof (abtRx); if (pn53x_transceive (pnd, abtCmd, sizeof (abtCmd), abtRx, &szRx, timeout) < 0) - return false; + return pnd->last_error; // Save the received byte count *pszRx = szRx - 1; @@ -1825,7 +1825,7 @@ pn53x_target_receive_bytes (struct nfc_device *pnd, uint8_t *pbtRx, size_t *pszR memcpy (pbtRx, abtRx + 1, *pszRx); // Everyting seems ok, return true - return true; + return *pszRx; } bool diff --git a/libnfc/chips/pn53x.h b/libnfc/chips/pn53x.h index dd651c0..6628d9e 100644 --- a/libnfc/chips/pn53x.h +++ b/libnfc/chips/pn53x.h @@ -314,7 +314,7 @@ 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); bool pn53x_target_receive_bits (struct nfc_device *pnd, uint8_t *pbtRx, size_t *pszRxBits, uint8_t *pbtRxPar); -bool 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); 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); diff --git a/libnfc/nfc-emulation.c b/libnfc/nfc-emulation.c index 592c555..a5ed90c 100644 --- a/libnfc/nfc-emulation.c +++ b/libnfc/nfc-emulation.c @@ -47,7 +47,7 @@ nfc_emulate_target (nfc_device *pnd, struct nfc_emulator *emulator) } } if (res >= 0) { - if (!nfc_target_receive_bytes(pnd, abtRx, &szRx, 0)) { + if (nfc_target_receive_bytes(pnd, abtRx, &szRx, 0) < 0) { return -1; } } diff --git a/libnfc/nfc-internal.h b/libnfc/nfc-internal.h index 808d861..2304b2c 100644 --- a/libnfc/nfc-internal.h +++ b/libnfc/nfc-internal.h @@ -144,7 +144,7 @@ struct nfc_driver_t { 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); - bool (*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); 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 9de8049..1cbad87 100644 --- a/libnfc/nfc.c +++ b/libnfc/nfc.c @@ -716,7 +716,7 @@ nfc_target_send_bytes (nfc_device *pnd, const uint8_t *pbtTx, const size_t szTx, /** * @brief Receive bytes and APDU frames - * @return Returns \c true if action was successfully performed; otherwise returns \c false. + * @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[out] pbtRx pointer to Rx buffer @@ -728,7 +728,7 @@ nfc_target_send_bytes (nfc_device *pnd, const uint8_t *pbtTx, const size_t szTx, * 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_receive_bytes (nfc_device *pnd, uint8_t *pbtRx, size_t *pszRx, int timeout) { HAL (target_receive_bytes, pnd, pbtRx, pszRx, timeout); diff --git a/test/test_dep_active.c b/test/test_dep_active.c index a6b5749..1bc5cae 100644 --- a/test/test_dep_active.c +++ b/test/test_dep_active.c @@ -88,15 +88,15 @@ target_thread (void *arg) 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; } - bool 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))); + 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))); 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; } + if (ires <= 0) { thread_res = -1; return (void*) thread_res; } const uint8_t abtTx[] = "Hello DEP initiator!"; - res = nfc_target_send_bytes (device, abtTx, sizeof(abtTx), 500); + 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; } @@ -131,7 +131,7 @@ initiator_thread (void *arg) 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_ACTIVE, 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 < 0) { thread_res = -1; return (void*) thread_res; } + if (res <= 0) { thread_res = -1; return (void*) thread_res; } const uint8_t abtTx[] = "Hello DEP target!"; uint8_t abtRx[1024]; diff --git a/test/test_dep_passive.c b/test/test_dep_passive.c index fdc91c8..32d4653 100644 --- a/test/test_dep_passive.c +++ b/test/test_dep_passive.c @@ -87,46 +87,46 @@ target_thread (void *arg) if (ires < 0) { thread_res = -1; return (void*) thread_res; } // First pass - bool 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))); + 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))); 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; } + if (ires <= 0) { thread_res = -1; return (void*) thread_res; } const uint8_t abtTx[] = "Hello DEP initiator!"; - res = nfc_target_send_bytes (device, abtTx, sizeof(abtTx), 500); + 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; } // 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))); + 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))); cut_assert_equal_memory (abtAttRx, sizeof (abtAttRx), abtRx, szRx, cut_message ("Invalid received data")); - if (!res) { thread_res = -1; return (void*) thread_res; } + if (ires <= 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; } // 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))); + 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))); cut_assert_equal_memory (abtAttRx, sizeof (abtAttRx), abtRx, szRx, cut_message ("Invalid received data")); - if (!res) { thread_res = -1; return (void*) thread_res; } + if (ires <= 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; } // Fourth 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))); + 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))); cut_assert_equal_memory (abtAttRx, sizeof (abtAttRx), abtRx, szRx, cut_message ("Invalid received data")); - if (!res) { thread_res = -1; return (void*) thread_res; } + if (ires <= 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))); @@ -163,7 +163,7 @@ initiator_thread (void *arg) 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 < 0) { thread_res = -1; return (void*) thread_res; } + if (res <= 0) { thread_res = -1; return (void*) thread_res; } const uint8_t abtTx[] = "Hello DEP target!"; uint8_t abtRx[1024]; @@ -188,7 +188,7 @@ initiator_thread (void *arg) 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 < 0) { thread_res = -1; return (void*) thread_res; } + if (res <= 0) { thread_res = -1; return (void*) thread_res; } szRx = sizeof (abtRx); res = nfc_initiator_transceive_bytes (device, abtTx, sizeof (abtTx), abtRx, &szRx, 1000); @@ -210,7 +210,7 @@ initiator_thread (void *arg) 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 < 0) { thread_res = -1; return (void*) thread_res; } + if (res <= 0) { thread_res = -1; return (void*) thread_res; } szRx = sizeof (abtRx); res = nfc_initiator_transceive_bytes (device, abtTx, sizeof (abtTx), abtRx, &szRx, 5000); @@ -232,7 +232,7 @@ initiator_thread (void *arg) 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 < 0) { thread_res = -1; return (void*) thread_res; } + if (res <= 0) { thread_res = -1; return (void*) thread_res; } szRx = sizeof (abtRx); res = nfc_initiator_transceive_bytes (device, abtTx, sizeof (abtTx), abtRx, &szRx, 5000); diff --git a/utils/nfc-relay-picc.c b/utils/nfc-relay-picc.c index ec10084..18e3544 100644 --- a/utils/nfc-relay-picc.c +++ b/utils/nfc-relay-picc.c @@ -370,7 +370,7 @@ main (int argc, char *argv[]) bool ret; if (!initiator_only_mode) { // Receive external reader command through target - if (!nfc_target_receive_bytes(pndTarget,abtCapdu,&szCapduLen, 0)) { + if (nfc_target_receive_bytes(pndTarget,abtCapdu,&szCapduLen, 0) < 0) { nfc_perror (pndTarget, "nfc_target_receive_bytes"); if (!target_only_mode) { nfc_disconnect (pndInitiator);