From 0f5cc5683d63bedeb9d2381acbf87dbcda4e79f7 Mon Sep 17 00:00:00 2001 From: Audrey Diacre Date: Tue, 20 Dec 2011 15:46:35 +0000 Subject: [PATCH] nfc_initiator_poll_target() function returns now libnfc error code. --- examples/nfc-poll.c | 4 ++-- include/nfc/nfc.h | 2 +- libnfc/chips/pn53x.c | 22 ++++++++++++---------- libnfc/chips/pn53x.h | 4 ++-- libnfc/nfc-internal.h | 6 +++--- libnfc/nfc.c | 4 ++-- 6 files changed, 22 insertions(+), 20 deletions(-) diff --git a/examples/nfc-poll.c b/examples/nfc-poll.c index fbb1893..7c7d206 100644 --- a/examples/nfc-poll.c +++ b/examples/nfc-poll.c @@ -90,7 +90,7 @@ main (int argc, const char *argv[]) const size_t szModulations = 5; nfc_target nt; - bool res; + int res = 0; pnd = nfc_connect (NULL); @@ -109,7 +109,7 @@ main (int argc, const char *argv[]) exit (EXIT_FAILURE); } - if (res > 0) { + if (res == 0) { print_nfc_target ( nt, verbose ); } else { printf ("No target found.\n"); diff --git a/include/nfc/nfc.h b/include/nfc/nfc.h index 61e7410..9ef9bd0 100644 --- a/include/nfc/nfc.h +++ b/include/nfc/nfc.h @@ -74,7 +74,7 @@ extern "C" { NFC_EXPORT int nfc_initiator_init (nfc_device *pnd); NFC_EXPORT int nfc_initiator_select_passive_target (nfc_device *pnd, const nfc_modulation nm, const uint8_t *pbtInitData, const size_t szInitData, nfc_target *pnt); NFC_EXPORT int nfc_initiator_list_passive_targets (nfc_device *pnd, const nfc_modulation nm, nfc_target ant[], const size_t szTargets); - NFC_EXPORT bool nfc_initiator_poll_target (nfc_device *pnd, const nfc_modulation *pnmTargetTypes, const size_t szTargetTypes, const uint8_t uiPollNr, const uint8_t uiPeriod, nfc_target *pnt); + NFC_EXPORT int nfc_initiator_poll_target (nfc_device *pnd, const nfc_modulation *pnmTargetTypes, const size_t szTargetTypes, const uint8_t uiPollNr, const uint8_t uiPeriod, nfc_target *pnt); NFC_EXPORT bool nfc_initiator_select_dep_target (nfc_device *pnd, const nfc_dep_mode ndm, const nfc_baud_rate nbr, const nfc_dep_info *pndiInitiator, nfc_target *pnt, const int timeout); NFC_EXPORT bool nfc_initiator_deselect_target (nfc_device *pnd); NFC_EXPORT int nfc_initiator_transceive_bytes (nfc_device *pnd, const uint8_t *pbtTx, const size_t szTx, uint8_t *pbtRx, size_t *pszRx, int timeout); diff --git a/libnfc/chips/pn53x.c b/libnfc/chips/pn53x.c index 9ddce62..82b8943 100644 --- a/libnfc/chips/pn53x.c +++ b/libnfc/chips/pn53x.c @@ -1016,12 +1016,14 @@ pn53x_initiator_select_passive_target (struct nfc_device *pnd, return pn53x_initiator_select_passive_target_ext (pnd, nm, pbtInitData, szInitData, pnt, 0); } -bool +int pn53x_initiator_poll_target (struct nfc_device *pnd, const nfc_modulation *pnmModulations, const size_t szModulations, const uint8_t uiPollNr, const uint8_t uiPeriod, nfc_target *pnt) { + int res = 0; + if (CHIP_DATA(pnd)->type == PN532) { size_t szTargetTypes = 0; pn53x_target_type apttTargetTypes[32]; @@ -1029,7 +1031,7 @@ pn53x_initiator_poll_target (struct nfc_device *pnd, const pn53x_target_type ptt = pn53x_nm_to_ptt(pnmModulations[n]); if (PTT_UNDEFINED == ptt) { pnd->last_error = NFC_EINVARG; - return false; + return pnd->last_error; } apttTargetTypes[szTargetTypes] = ptt; if ((pnd->bAutoIso14443_4) && (ptt == PTT_MIFARE)) { // Hack to have ATS @@ -1041,19 +1043,19 @@ pn53x_initiator_poll_target (struct nfc_device *pnd, } size_t szTargetFound = 0; nfc_target ntTargets[2]; - if (pn53x_InAutoPoll (pnd, apttTargetTypes, szTargetTypes, uiPollNr, uiPeriod, ntTargets, &szTargetFound, 0) < 0) - return false; + if ((res = pn53x_InAutoPoll (pnd, apttTargetTypes, szTargetTypes, uiPollNr, uiPeriod, ntTargets, &szTargetFound, 0)) < 0) + return res; switch (szTargetFound) { case 1: *pnt = ntTargets[0]; - return true; + return NFC_SUCCESS; break; case 2: *pnt = ntTargets[1]; // We keep the selected one - return true; + return NFC_SUCCESS; break; default: - return false; + return NFC_ECHIP; break; } } else { @@ -1069,15 +1071,15 @@ pn53x_initiator_poll_target (struct nfc_device *pnd, if (pn53x_initiator_select_passive_target_ext (pnd, pnmModulations[n], pbtInitiatorData, szInitiatorData, pnt, timeout_ms) < 0) { if (pnd->last_error != NFC_ETIMEOUT) - return false; + return pnd->last_error; } else { - return true; + return NFC_SUCCESS; } } } } while (uiPollNr==0xff); // uiPollNr==0xff means infinite polling } - return false; + return NFC_ECHIP; } bool diff --git a/libnfc/chips/pn53x.h b/libnfc/chips/pn53x.h index 39597eb..4010a77 100644 --- a/libnfc/chips/pn53x.h +++ b/libnfc/chips/pn53x.h @@ -285,12 +285,12 @@ bool pn53x_check_communication (struct nfc_device *pnd); bool pn53x_idle (struct nfc_device *pnd); // NFC device as Initiator functions -int pn53x_initiator_init (struct nfc_device *pnd); +int pn53x_initiator_init (struct nfc_device *pnd); int pn53x_initiator_select_passive_target (struct nfc_device *pnd, const nfc_modulation nm, const uint8_t *pbtInitData, const size_t szInitData, nfc_target *pnt); -bool pn53x_initiator_poll_target (struct nfc_device *pnd, +int pn53x_initiator_poll_target (struct nfc_device *pnd, const nfc_modulation *pnmModulations, const size_t szModulations, const uint8_t uiPollNr, const uint8_t uiPeriod, nfc_target *pnt); diff --git a/libnfc/nfc-internal.h b/libnfc/nfc-internal.h index a5846c5..3f2919f 100644 --- a/libnfc/nfc-internal.h +++ b/libnfc/nfc-internal.h @@ -132,12 +132,12 @@ struct nfc_driver_t { void (*disconnect) (struct nfc_device *pnd); const char *(*strerror) (const struct nfc_device *pnd); - int (*initiator_init) (struct nfc_device *pnd); + int (*initiator_init) (struct nfc_device *pnd); int (*initiator_select_passive_target) (struct nfc_device *pnd, const nfc_modulation nm, const uint8_t * pbtInitData, const size_t szInitData, nfc_target * pnt); - bool (*initiator_poll_target) (struct nfc_device *pnd, const nfc_modulation * pnmModulations, const size_t szModulations, const uint8_t uiPollNr, const uint8_t btPeriod, nfc_target * pnt); + int (*initiator_poll_target) (struct nfc_device *pnd, const nfc_modulation * pnmModulations, const size_t szModulations, const uint8_t uiPollNr, const uint8_t btPeriod, nfc_target * pnt); bool (*initiator_select_dep_target) (struct nfc_device *pnd, const nfc_dep_mode ndm, const nfc_baud_rate nbr, const nfc_dep_info * pndiInitiator, nfc_target * pnt, const int timeout); bool (*initiator_deselect_target) (struct nfc_device *pnd); - int (*initiator_transceive_bytes) (struct nfc_device *pnd, const uint8_t * pbtTx, const size_t szTx, uint8_t * pbtRx, size_t * pszRx, int timeout); + int (*initiator_transceive_bytes) (struct nfc_device *pnd, const uint8_t * pbtTx, const size_t szTx, uint8_t * pbtRx, size_t * pszRx, int timeout); bool (*initiator_transceive_bits) (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); bool (*initiator_transceive_bytes_timed) (struct nfc_device *pnd, const uint8_t * pbtTx, const size_t szTx, uint8_t * pbtRx, size_t * pszRx, uint32_t * cycles); 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); diff --git a/libnfc/nfc.c b/libnfc/nfc.c index fbbfc40..e2eb1cd 100644 --- a/libnfc/nfc.c +++ b/libnfc/nfc.c @@ -410,7 +410,7 @@ nfc_initiator_list_passive_targets (nfc_device *pnd, /** * @brief Polling for NFC targets - * @return Returns \c true if action was successfully performed; otherwise returns \c false. + * @return Returns 0 on success, otherwise returns libnfc's error code (negative value). * * @param pnd \a nfc_device struct pointer that represent currently used device * @param ppttTargetTypes array of desired target types @@ -421,7 +421,7 @@ nfc_initiator_list_passive_targets (nfc_device *pnd, * @note e.g. if uiPeriod=10, it will poll each desired target type during 1.5s * @param[out] pnt pointer on \a nfc_target (over)writable struct */ -bool +int nfc_initiator_poll_target (nfc_device *pnd, const nfc_modulation *pnmModulations, const size_t szModulations, const uint8_t uiPollNr, const uint8_t uiPeriod,