diff --git a/examples/nfc-dep-initiator.c b/examples/nfc-dep-initiator.c index e7ad00e..4de2da5 100644 --- a/examples/nfc-dep-initiator.c +++ b/examples/nfc-dep-initiator.c @@ -81,7 +81,7 @@ main (int argc, const char *argv[]) signal (SIGINT, stop_dep_communication); - if (!nfc_initiator_init (pnd)) { + if (nfc_initiator_init (pnd) < 0) { nfc_perror(pnd, "nfc_initiator_init"); return EXIT_FAILURE; } diff --git a/include/nfc/nfc.h b/include/nfc/nfc.h index 825da4c..b55c1b8 100644 --- a/include/nfc/nfc.h +++ b/include/nfc/nfc.h @@ -71,9 +71,9 @@ extern "C" { NFC_EXPORT bool nfc_idle (nfc_device *pnd); /* NFC initiator: act as "reader" */ - NFC_EXPORT bool nfc_initiator_init (nfc_device *pnd); + NFC_EXPORT int nfc_initiator_init (nfc_device *pnd); NFC_EXPORT bool 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 bool nfc_initiator_list_passive_targets (nfc_device *pnd, const nfc_modulation nm, nfc_target ant[], const size_t szTargets, size_t *pszTargetFound); + 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 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); diff --git a/libnfc/chips/pn53x.c b/libnfc/chips/pn53x.c index 4498695..0a9c2ca 100644 --- a/libnfc/chips/pn53x.c +++ b/libnfc/chips/pn53x.c @@ -878,17 +878,17 @@ pn53x_check_communication (nfc_device *pnd) return ((sizeof(abtExpectedRx) == szRx) && (0 == memcmp (abtRx, abtExpectedRx, sizeof(abtExpectedRx)))); } -bool +int pn53x_initiator_init (nfc_device *pnd) { pn53x_reset_settings(pnd); // Configure the PN53X to be an Initiator or Reader/Writer if (!pn53x_write_register (pnd, PN53X_REG_CIU_Control, SYMBOL_INITIATOR, 0x10)) - return false; + return NFC_DEVICE_ERROR; CHIP_DATA (pnd)->operating_mode = INITIATOR; - return true; + return NFC_SUCCESS; } bool diff --git a/libnfc/chips/pn53x.h b/libnfc/chips/pn53x.h index e727b8f..d5f8ce4 100644 --- a/libnfc/chips/pn53x.h +++ b/libnfc/chips/pn53x.h @@ -283,7 +283,7 @@ bool pn53x_check_communication (nfc_device *pnd); bool pn53x_idle (nfc_device *pnd); // NFC device as Initiator functions -bool pn53x_initiator_init (nfc_device *pnd); +int pn53x_initiator_init (nfc_device *pnd); bool pn53x_initiator_select_passive_target (nfc_device *pnd, const nfc_modulation nm, const uint8_t *pbtInitData, const size_t szInitData, diff --git a/libnfc/nfc-internal.h b/libnfc/nfc-internal.h index 6cdad51..bfd703e 100644 --- a/libnfc/nfc-internal.h +++ b/libnfc/nfc-internal.h @@ -131,7 +131,7 @@ struct nfc_driver_t { void (*disconnect) (nfc_device *pnd); const char *(*strerror) (const nfc_device *pnd); - bool (*initiator_init) (nfc_device *pnd); + int (*initiator_init) (nfc_device *pnd); bool (*initiator_select_passive_target) (nfc_device *pnd, const nfc_modulation nm, const uint8_t * pbtInitData, const size_t szInitData, nfc_target * pnt); bool (*initiator_poll_target) (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) (nfc_device *pnd, const nfc_dep_mode ndm, const nfc_baud_rate nbr, const nfc_dep_info * pndiInitiator, nfc_target * pnt, const int timeout); diff --git a/libnfc/nfc.c b/libnfc/nfc.c index 4761886..48641f3 100644 --- a/libnfc/nfc.c +++ b/libnfc/nfc.c @@ -241,7 +241,7 @@ nfc_device_set_property_bool (nfc_device *pnd, const nfc_property property, cons /** * @brief Initialize NFC device as initiator (reader) - * @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 * * The NFC device is configured to function as RFID reader. @@ -259,44 +259,45 @@ nfc_device_set_property_bool (nfc_device *pnd, const nfc_property property, cons * - Let the device try forever to find a target (NP_INFINITE_SELECT = true) * - RF field is shortly dropped (if it was enabled) then activated again */ -bool +int nfc_initiator_init (nfc_device *pnd) { + int res = 0; // Drop the field for a while - if (nfc_device_set_property_bool (pnd, NP_ACTIVATE_FIELD, false) < 0) - return false; + if ((res = nfc_device_set_property_bool (pnd, NP_ACTIVATE_FIELD, false)) < 0) + return res; // Enable field so more power consuming cards can power themselves up - if (nfc_device_set_property_bool (pnd, NP_ACTIVATE_FIELD, true) < 0) - return false; + if ((res = nfc_device_set_property_bool (pnd, NP_ACTIVATE_FIELD, true)) < 0) + return res; // Let the device try forever to find a target/tag - if (nfc_device_set_property_bool (pnd, NP_INFINITE_SELECT, true) < 0) - return false; + if ((res = nfc_device_set_property_bool (pnd, NP_INFINITE_SELECT, true)) < 0) + return res; // Activate auto ISO14443-4 switching by default - if (nfc_device_set_property_bool (pnd, NP_AUTO_ISO14443_4, true) < 0) - return false; + if ((res = nfc_device_set_property_bool (pnd, NP_AUTO_ISO14443_4, true)) < 0) + return res; // Force 14443-A mode - if (nfc_device_set_property_bool (pnd, NP_FORCE_ISO14443_A, true) < 0) - return false; + if ((res = nfc_device_set_property_bool (pnd, NP_FORCE_ISO14443_A, true)) < 0) + return res; // Force speed at 106kbps - if (nfc_device_set_property_bool (pnd, NP_FORCE_SPEED_106, true) < 0) - return false; + if ((res = nfc_device_set_property_bool (pnd, NP_FORCE_SPEED_106, true)) < 0) + return res; // Disallow invalid frame - if (nfc_device_set_property_bool (pnd, NP_ACCEPT_INVALID_FRAMES, false) < 0) - return false; + if ((res = nfc_device_set_property_bool (pnd, NP_ACCEPT_INVALID_FRAMES, false)) < 0) + return res; // Disallow multiple frames - if (nfc_device_set_property_bool (pnd, NP_ACCEPT_MULTIPLE_FRAMES, false) < 0) - return false; + if ((res = nfc_device_set_property_bool (pnd, NP_ACCEPT_MULTIPLE_FRAMES, false)) < 0) + return res; // Make sure we reset the CRC and parity to chip handling. - if (nfc_device_set_property_bool (pnd, NP_HANDLE_CRC, true) < 0) - return false; - if (nfc_device_set_property_bool (pnd, NP_HANDLE_PARITY, true) < 0) - return false; + if ((res = nfc_device_set_property_bool (pnd, NP_HANDLE_CRC, true)) < 0) + return res; + if ((res = nfc_device_set_property_bool (pnd, NP_HANDLE_PARITY, true)) < 0) + return res; // Activate "easy framing" feature by default - if (nfc_device_set_property_bool (pnd, NP_EASY_FRAMING, true) < 0) - return false; + if ((res = nfc_device_set_property_bool (pnd, NP_EASY_FRAMING, true)) < 0) + return res; // Deactivate the CRYPTO1 cipher, it may could cause problems when still active - if (nfc_device_set_property_bool (pnd, NP_ACTIVATE_CRYPTO1, false) < 0) - return false; + if ((res = nfc_device_set_property_bool (pnd, NP_ACTIVATE_CRYPTO1, false)) < 0) + return res; HAL (initiator_init, pnd); } @@ -346,13 +347,12 @@ nfc_initiator_select_passive_target (nfc_device *pnd, /** * @brief List passive or emulated tags - * @return Returns \c true if action was successfully performed; otherwise returns \c false. + * @return Returns the number of targets found on success, otherwise returns libnfc's error code (negative value) * * @param pnd \a nfc_device struct pointer that represent currently used device * @param nm desired modulation * @param[out] ant array of \a nfc_target that will be filled with targets info * @param szTargets size of \a ant (will be the max targets listed) - * @param[out] pszTargetFound pointer where target found counter will be stored * * The NFC device will try to find the available passive tags. Some NFC devices * are capable to emulate passive tags. The standards (ISO18092 and ECMA-340) @@ -361,21 +361,22 @@ nfc_initiator_select_passive_target (nfc_device *pnd, * with, therefore the initial modulation and speed (106, 212 or 424 kbps) * should be supplied. */ -bool +int nfc_initiator_list_passive_targets (nfc_device *pnd, const nfc_modulation nm, - nfc_target ant[], const size_t szTargets, size_t *pszTargetFound) + nfc_target ant[], const size_t szTargets) { nfc_target nt; size_t szTargetFound = 0; uint8_t *pbtInitData = NULL; size_t szInitDataLen = 0; + int res = 0; pnd->iLastError = 0; // Let the reader only try once to find a tag - if (nfc_device_set_property_bool (pnd, NP_INFINITE_SELECT, false) < 0) { - return false; + if ((res = nfc_device_set_property_bool (pnd, NP_INFINITE_SELECT, false)) < 0) { + return res; } prepare_initiator_data (nm, &pbtInitData, &szInitDataLen); @@ -404,9 +405,7 @@ nfc_initiator_list_passive_targets (nfc_device *pnd, break; } } - *pszTargetFound = szTargetFound; - - return true; + return szTargetFound; } /** diff --git a/test/test_access_storm.c b/test/test_access_storm.c index 0fdc253..2646e21 100644 --- a/test/test_access_storm.c +++ b/test/test_access_storm.c @@ -15,8 +15,8 @@ test_access_storm (void) { int n = NTESTS; nfc_connstring connstrings[MAX_DEVICE_COUNT]; - size_t device_count, ref_device_count, target_count; - bool res; + size_t device_count, ref_device_count; + int res = 0; nfc_list_devices (connstrings, MAX_DEVICE_COUNT, &ref_device_count); if (!ref_device_count) @@ -36,14 +36,14 @@ test_access_storm (void) cut_assert_not_null (device, cut_message ("nfc_connect")); res = nfc_initiator_init(device); - cut_assert_true (res, cut_message ("nfc_initiator_init")); + cut_assert_equal_int (0, res, cut_message ("nfc_initiator_init")); const nfc_modulation nm = { .nmt = NMT_ISO14443A, .nbr = NBR_106, }; - res = nfc_initiator_list_passive_targets(device, nm, ant, MAX_TARGET_COUNT, &target_count); - cut_assert_true (res, cut_message ("nfc_initiator_list_passive_targets")); + res = nfc_initiator_list_passive_targets(device, nm, ant, MAX_TARGET_COUNT); + cut_assert_operator_int (res, >=, 0, cut_message ("nfc_initiator_list_passive_targets")); nfc_disconnect (device); } diff --git a/test/test_dep_active.c b/test/test_dep_active.c index 9528de3..0b1cc20 100644 --- a/test/test_dep_active.c +++ b/test/test_dep_active.c @@ -117,7 +117,7 @@ initiator_thread (void *arg) sleep (1); printf ("=========== INITIATOR %s =========\n", nfc_device_name (device)); bool res = nfc_initiator_init (device); - cut_assert_true (res, cut_message ("Can't initialize NFC device as initiator: %s", nfc_strerror (device))); + cut_assert_equal_int (0, res, cut_message ("Can't initialize NFC device as initiator: %s", nfc_strerror (device))); if (!res) { thread_res = -1; return (void*) thread_res; } nfc_target nt; diff --git a/test/test_dep_passive.c b/test/test_dep_passive.c index 71ce58f..e17c1f6 100644 --- a/test/test_dep_passive.c +++ b/test/test_dep_passive.c @@ -150,7 +150,7 @@ initiator_thread (void *arg) printf ("=========== INITIATOR %s =========\n", nfc_device_name (device)); bool res = nfc_initiator_init (device); - cut_assert_true (res, cut_message ("Can't initialize NFC device as initiator: %s", nfc_strerror (device))); + cut_assert_equal_int (0, res, cut_message ("Can't initialize NFC device as initiator: %s", nfc_strerror (device))); if (!res) { thread_res = -1; return (void*) thread_res; } nfc_target nt; diff --git a/utils/nfc-list.c b/utils/nfc-list.c index 9c7f62e..8172a4e 100644 --- a/utils/nfc-list.c +++ b/utils/nfc-list.c @@ -64,9 +64,9 @@ main (int argc, const char *argv[]) { (void) argc; const char *acLibnfcVersion; - size_t szTargetFound; size_t i; bool verbose = false; + int res = 0; // Display libnfc version acLibnfcVersion = nfc_version (); @@ -124,12 +124,12 @@ main (int argc, const char *argv[]) nm.nmt = NMT_ISO14443A; nm.nbr = NBR_106; // List ISO14443A targets - if (nfc_initiator_list_passive_targets (pnd, nm, ant, MAX_TARGET_COUNT, &szTargetFound)) { - size_t n; - if (verbose || (szTargetFound > 0)) { - printf ("%d ISO14443A passive target(s) found%s\n", (int) szTargetFound, (szTargetFound == 0) ? ".\n" : ":"); + if ((res = nfc_initiator_list_passive_targets (pnd, nm, ant, MAX_TARGET_COUNT)) >= 0) { + int n; + if (verbose) { + printf ("%d ISO14443A passive target(s) found%s\n", res, (res == 0) ? ".\n" : ":"); } - for (n = 0; n < szTargetFound; n++) { + for (n = 0; n < res; n++) { print_nfc_iso14443a_info (ant[n].nti.nai, verbose); printf ("\n"); } @@ -138,26 +138,24 @@ main (int argc, const char *argv[]) nm.nmt = NMT_FELICA; nm.nbr = NBR_212; // List Felica tags - if (nfc_initiator_list_passive_targets (pnd, nm, ant, MAX_TARGET_COUNT, &szTargetFound)) { - size_t n; - if (verbose || (szTargetFound > 0)) { - printf ("%d Felica (212 kbps) passive target(s) found%s\n", (int) szTargetFound, - (szTargetFound == 0) ? ".\n" : ":"); + if (nfc_initiator_list_passive_targets (pnd, nm, ant, MAX_TARGET_COUNT) >= 0) { + int n; + if (verbose) { + printf ("%d Felica (212 kbps) passive target(s) found%s\n", res, (res == 0) ? ".\n" : ":"); } - for (n = 0; n < szTargetFound; n++) { + for (n = 0; n < res; n++) { print_nfc_felica_info (ant[n].nti.nfi, verbose); printf ("\n"); } } nm.nbr = NBR_424; - if (nfc_initiator_list_passive_targets (pnd, nm, ant, MAX_TARGET_COUNT, &szTargetFound)) { - size_t n; - if (verbose || (szTargetFound > 0)) { - printf ("%d Felica (424 kbps) passive target(s) found%s\n", (int) szTargetFound, - (szTargetFound == 0) ? ".\n" : ":"); + if (nfc_initiator_list_passive_targets (pnd, nm, ant, MAX_TARGET_COUNT) >= 0) { + int n; + if (verbose) { + printf ("%d Felica (424 kbps) passive target(s) found%s\n", res, (res == 0) ? ".\n" : ":"); } - for (n = 0; n < szTargetFound; n++) { + for (n = 0; n < res; n++) { print_nfc_felica_info (ant[n].nti.nfi, verbose); printf ("\n"); } @@ -166,12 +164,12 @@ main (int argc, const char *argv[]) nm.nmt = NMT_ISO14443B; nm.nbr = NBR_106; // List ISO14443B targets - if (nfc_initiator_list_passive_targets (pnd, nm, ant, MAX_TARGET_COUNT, &szTargetFound)) { - size_t n; - if (verbose || (szTargetFound > 0)) { - printf ("%d ISO14443B passive target(s) found%s\n", (int) szTargetFound, (szTargetFound == 0) ? ".\n" : ":"); + if (nfc_initiator_list_passive_targets (pnd, nm, ant, MAX_TARGET_COUNT) >= 0) { + int n; + if (verbose) { + printf ("%d ISO14443B passive target(s) found%s\n", res, (res == 0) ? ".\n" : ":"); } - for (n = 0; n < szTargetFound; n++) { + for (n = 0; n < res; n++) { print_nfc_iso14443b_info (ant[n].nti.nbi, verbose); printf ("\n"); } @@ -180,12 +178,12 @@ main (int argc, const char *argv[]) nm.nmt = NMT_ISO14443BI; nm.nbr = NBR_106; // List ISO14443B' targets - if (nfc_initiator_list_passive_targets (pnd, nm, ant, MAX_TARGET_COUNT, &szTargetFound)) { - size_t n; - if (verbose || (szTargetFound > 0)) { - printf ("%d ISO14443B' passive target(s) found%s\n", (int) szTargetFound, (szTargetFound == 0) ? ".\n" : ":"); + if (nfc_initiator_list_passive_targets (pnd, nm, ant, MAX_TARGET_COUNT) >= 0) { + int n; + if (verbose) { + printf ("%d ISO14443B' passive target(s) found%s\n", res, (res == 0) ? ".\n" : ":"); } - for (n = 0; n < szTargetFound; n++) { + for (n = 0; n < res; n++) { print_nfc_iso14443bi_info (ant[n].nti.nii, verbose); printf ("\n"); } @@ -194,12 +192,12 @@ main (int argc, const char *argv[]) nm.nmt = NMT_ISO14443B2SR; nm.nbr = NBR_106; // List ISO14443B-2 ST SRx family targets - if (nfc_initiator_list_passive_targets (pnd, nm, ant, MAX_TARGET_COUNT, &szTargetFound)) { - size_t n; - if (verbose || (szTargetFound > 0)) { - printf ("%d ISO14443B-2 ST SRx passive target(s) found%s\n", (int) szTargetFound, (szTargetFound == 0) ? ".\n" : ":"); + if (nfc_initiator_list_passive_targets (pnd, nm, ant, MAX_TARGET_COUNT) >= 0) { + int n; + if (verbose) { + printf ("%d ISO14443B-2 ST SRx passive target(s) found%s\n", res, (res == 0) ? ".\n" : ":"); } - for (n = 0; n < szTargetFound; n++) { + for (n = 0; n < res; n++) { print_nfc_iso14443b2sr_info (ant[n].nti.nsi, verbose); printf ("\n"); } @@ -208,12 +206,12 @@ main (int argc, const char *argv[]) nm.nmt = NMT_ISO14443B2CT; nm.nbr = NBR_106; // List ISO14443B-2 ASK CTx family targets - if (nfc_initiator_list_passive_targets (pnd, nm, ant, MAX_TARGET_COUNT, &szTargetFound)) { - size_t n; - if (verbose || (szTargetFound > 0)) { - printf ("%d ISO14443B-2 ASK CTx passive target(s) found%s\n", (int) szTargetFound, (szTargetFound == 0) ? ".\n" : ":"); + if (nfc_initiator_list_passive_targets (pnd, nm, ant, MAX_TARGET_COUNT) >= 0) { + int n; + if (verbose) { + printf ("%d ISO14443B-2 ASK CTx passive target(s) found%s\n", res, (res == 0) ? ".\n" : ":"); } - for (n = 0; n < szTargetFound; n++) { + for (n = 0; n < res; n++) { print_nfc_iso14443b2ct_info (ant[n].nti.nci, verbose); printf ("\n"); } @@ -222,12 +220,12 @@ main (int argc, const char *argv[]) nm.nmt = NMT_JEWEL; nm.nbr = NBR_106; // List Jewel targets - if (nfc_initiator_list_passive_targets(pnd, nm, ant, MAX_TARGET_COUNT, &szTargetFound )) { - size_t n; - if (verbose || (szTargetFound > 0)) { - printf("%d Jewel passive target(s) found%s\n", (int)szTargetFound, (szTargetFound==0)?".\n":":"); + if (nfc_initiator_list_passive_targets(pnd, nm, ant, MAX_TARGET_COUNT) >= 0) { + int n; + if (verbose) { + printf("%d Jewel passive target(s) found%s\n", res, (res == 0)?".\n":":"); } - for(n=0; nacName); - if (!nfc_initiator_init (pndInitiator)) { + if (nfc_initiator_init (pndInitiator) < 0) { printf ("Error: fail initializing initiator\n"); nfc_disconnect (pndInitiator); exit (EXIT_FAILURE);