diff --git a/examples/nfc-emulate-uid.c b/examples/nfc-emulate-uid.c index 80339bf..90c01aa 100644 --- a/examples/nfc-emulate-uid.c +++ b/examples/nfc-emulate-uid.c @@ -172,7 +172,7 @@ main (int argc, char *argv[]) while (true) { // Test if we received a frame - if (nfc_target_receive_bits (pnd, abtRecv, &szRecvBits, NULL)) { + if (nfc_target_receive_bits (pnd, abtRecv, &szRecvBits, NULL) > 0) { // Prepare the command to send back for the anti-collision request switch (szRecvBits) { case 7: // Request or Wakeup diff --git a/examples/nfc-relay.c b/examples/nfc-relay.c index fc7230d..865ff60 100644 --- a/examples/nfc-relay.c +++ b/examples/nfc-relay.c @@ -175,7 +175,7 @@ main (int argc, char *argv[]) while (!quitting) { // Test if we received a frame from the reader - if (nfc_target_receive_bits (pndTag, abtReaderRx, &szReaderRxBits, abtReaderRxPar)) { + if (nfc_target_receive_bits (pndTag, abtReaderRx, &szReaderRxBits, abtReaderRxPar) > 0) { // Drop down the field before sending a REQA command and start a new session if (szReaderRxBits == 7 && abtReaderRx[0] == 0x26) { // Drop down field for a very short time (original tag will reboot) diff --git a/include/nfc/nfc.h b/include/nfc/nfc.h index 29b3199..006cfe1 100644 --- a/include/nfc/nfc.h +++ b/include/nfc/nfc.h @@ -87,7 +87,7 @@ extern "C" { 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); - NFC_EXPORT bool nfc_target_receive_bits (nfc_device *pnd, uint8_t *pbtRx, size_t *pszRxBits, uint8_t *pbtRxPar); + NFC_EXPORT int nfc_target_receive_bits (nfc_device *pnd, uint8_t *pbtRx, size_t *pszRxBits, uint8_t *pbtRxPar); /* Error reporting */ NFC_EXPORT const char *nfc_strerror (const nfc_device *pnd); diff --git a/libnfc/chips/pn53x.c b/libnfc/chips/pn53x.c index 9267b03..5ef63f2 100644 --- a/libnfc/chips/pn53x.c +++ b/libnfc/chips/pn53x.c @@ -1747,21 +1747,23 @@ pn53x_target_init (struct nfc_device *pnd, nfc_target *pnt, uint8_t *pbtRx, size return NFC_SUCCESS; } -bool +int pn53x_target_receive_bits (struct nfc_device *pnd, uint8_t *pbtRx, size_t *pszRxBits, uint8_t *pbtRxPar) { uint8_t abtCmd[] = { TgGetInitiatorCommand }; uint8_t abtRx[PN53x_EXTENDED_FRAME__DATA_MAX_LEN]; size_t szRx = sizeof (abtRx); + int res = 0; + // Try to gather a received frame from the reader - if (pn53x_transceive (pnd, abtCmd, sizeof (abtCmd), abtRx, &szRx, -1) < 0) - return false; + if ((res = pn53x_transceive (pnd, abtCmd, sizeof (abtCmd), abtRx, &szRx, -1)) < 0) + return res; // Get the last bit-count that is stored in the received byte uint8_t ui8rcc; - if (pn53x_read_register (pnd, PN53X_REG_CIU_Control, &ui8rcc) < 0) - return false; + if ((res = pn53x_read_register (pnd, PN53X_REG_CIU_Control, &ui8rcc)) < 0) + return res; uint8_t ui8Bits = ui8rcc & SYMBOL_RX_LAST_BITS; // Recover the real frame length in bits @@ -1778,8 +1780,8 @@ pn53x_target_receive_bits (struct nfc_device *pnd, uint8_t *pbtRx, size_t *pszRx // Copy the received bytes memcpy (pbtRx, abtRx + 1, szRx - 1); } - // Everyting seems ok, return true - return true; + // Everyting seems ok, return received bits count + return *pszRxBits; } int @@ -1821,13 +1823,13 @@ pn53x_target_receive_bytes (struct nfc_device *pnd, uint8_t *pbtRx, size_t *pszR if (pn53x_transceive (pnd, abtCmd, sizeof (abtCmd), abtRx, &szRx, timeout) < 0) return pnd->last_error; - // Save the received byte count + // Save the received bytes count *pszRx = szRx - 1; // Copy the received bytes memcpy (pbtRx, abtRx + 1, *pszRx); - // Everyting seems ok, return true + // Everyting seems ok, return received bytes count return *pszRx; } diff --git a/libnfc/chips/pn53x.h b/libnfc/chips/pn53x.h index 42eeb9c..8fd96ab 100644 --- a/libnfc/chips/pn53x.h +++ b/libnfc/chips/pn53x.h @@ -313,7 +313,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); +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); int pn53x_target_send_bytes (struct nfc_device *pnd, const uint8_t *pbtTx, const size_t szTx, int timeout); diff --git a/libnfc/nfc-internal.h b/libnfc/nfc-internal.h index 51ac12c..adff35c 100644 --- a/libnfc/nfc-internal.h +++ b/libnfc/nfc-internal.h @@ -146,7 +146,7 @@ struct nfc_driver_t { 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); - bool (*target_receive_bits) (struct nfc_device *pnd, uint8_t * pbtRx, size_t * pszRxBits, uint8_t * pbtRxPar); + int (*target_receive_bits) (struct nfc_device *pnd, uint8_t * pbtRx, size_t * pszRxBits, uint8_t * pbtRxPar); int (*device_set_property_bool) (struct nfc_device *pnd, const nfc_property property, const bool bEnable); int (*device_set_property_int) (struct nfc_device *pnd, const nfc_property property, const int value); diff --git a/libnfc/nfc.c b/libnfc/nfc.c index 439d795..371331d 100644 --- a/libnfc/nfc.c +++ b/libnfc/nfc.c @@ -749,7 +749,7 @@ nfc_target_send_bits (nfc_device *pnd, const uint8_t *pbtTx, const size_t szTxBi /** * @brief Receive bit-frames - * @return Returns \c true if action was successfully performed; otherwise returns \c false. + * @return Returns received bits count on success, otherwise returns libnfc's error code * * This function makes it possible to receive (raw) bit-frames. It returns all * the messages that are stored in the FIFO buffer of the \e PN53x chip. It @@ -758,7 +758,7 @@ nfc_target_send_bits (nfc_device *pnd, const uint8_t *pbtTx, const size_t szTxBi * NP_ACCEPT_MULTIPLE_FRAMES configuration option to avoid losing transmitted * frames. */ -bool +int nfc_target_receive_bits (nfc_device *pnd, uint8_t *pbtRx, size_t *pszRxBits, uint8_t *pbtRxPar) { HAL (target_receive_bits, pnd, pbtRx, pszRxBits, pbtRxPar);