diff --git a/examples/nfc-dep-target.c b/examples/nfc-dep-target.c index 1d59910..28a7fd3 100644 --- a/examples/nfc-dep-target.c +++ b/examples/nfc-dep-target.c @@ -119,7 +119,7 @@ main (int argc, const char *argv[]) print_nfc_target (nt, false); printf ("Waiting for initiator request...\n"); - if(!nfc_target_init (pnd, &nt, abtRx, &szRx)) { + if(nfc_target_init (pnd, &nt, abtRx, &szRx) < 0) { nfc_perror(pnd, "nfc_target_init"); goto error; } diff --git a/examples/nfc-emulate-tag.c b/examples/nfc-emulate-tag.c index 8a459cc..8ef6db9 100644 --- a/examples/nfc-emulate-tag.c +++ b/examples/nfc-emulate-tag.c @@ -140,7 +140,7 @@ nfc_target_emulate_tag(nfc_device *pnd, nfc_target *pnt) uint8_t abtTx[MAX_FRAME_LEN]; bool loop = true; - if (!nfc_target_init (pnd, pnt, abtRx, &szRx)) { + if (nfc_target_init (pnd, pnt, abtRx, &szRx) < 0) { nfc_perror (pnd, "nfc_target_init"); return false; } diff --git a/examples/nfc-emulate-uid.c b/examples/nfc-emulate-uid.c index d536339..0835034 100644 --- a/examples/nfc-emulate-uid.c +++ b/examples/nfc-emulate-uid.c @@ -155,7 +155,7 @@ main (int argc, char *argv[]) }, }, }; - if (!nfc_target_init (pnd, &nt, abtRecv, &szRecvBits)) { + if (nfc_target_init (pnd, &nt, abtRecv, &szRecvBits) < 0) { nfc_perror (pnd, "nfc_target_init"); ERR ("Could not come out of auto-emulation, no command was received"); goto error; diff --git a/examples/nfc-relay.c b/examples/nfc-relay.c index b275628..7b3f803 100644 --- a/examples/nfc-relay.c +++ b/examples/nfc-relay.c @@ -146,7 +146,7 @@ main (int argc, char *argv[]) }, }; - if (!nfc_target_init (pndTag, &nt, abtReaderRx, &szReaderRxBits)) { + if (nfc_target_init (pndTag, &nt, abtReaderRx, &szReaderRxBits) < 0) { ERR ("%s", "Initialization of NFC emulator failed"); nfc_disconnect (pndTag); return EXIT_FAILURE; diff --git a/examples/pn53x-sam.c b/examples/pn53x-sam.c index 2b19db9..9899d6f 100644 --- a/examples/pn53x-sam.c +++ b/examples/pn53x-sam.c @@ -173,7 +173,7 @@ main (int argc, const char *argv[]) }; printf ("Now both, NFC device (configured as target) and SAM are readables from an external NFC initiator.\n"); printf ("Please note that NFC device (configured as target) stay in target mode until it receive RATS, ATR_REQ or proprietary command.\n"); - if (!nfc_target_init (pnd, &nt, abtRx, &szRx)) { + if (nfc_target_init (pnd, &nt, abtRx, &szRx) < 0) { nfc_perror(pnd, "nfc_target_init"); return EXIT_FAILURE; } diff --git a/include/nfc/nfc.h b/include/nfc/nfc.h index 1fb5ca9..4c51604 100644 --- a/include/nfc/nfc.h +++ b/include/nfc/nfc.h @@ -83,7 +83,7 @@ extern "C" { NFC_EXPORT bool nfc_initiator_transceive_bits_timed (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); /* NFC target: act as tag (i.e. MIFARE Classic) or NFC target device. */ - NFC_EXPORT bool nfc_target_init (nfc_device *pnd, nfc_target *pnt, uint8_t *pbtRx, size_t *pszRx); + 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 bool nfc_target_send_bits (nfc_device *pnd, const uint8_t *pbtTx, const size_t szTxBits, const uint8_t *pbtTxPar); diff --git a/libnfc/chips/pn53x.c b/libnfc/chips/pn53x.c index 34933b0..6c56ec0 100644 --- a/libnfc/chips/pn53x.c +++ b/libnfc/chips/pn53x.c @@ -1534,7 +1534,7 @@ pn53x_initiator_deselect_target (struct nfc_device *pnd) #define SAK_ISO14443_4_COMPLIANT 0x20 #define SAK_ISO18092_COMPLIANT 0x40 -bool +int pn53x_target_init (struct nfc_device *pnd, nfc_target *pnt, uint8_t *pbtRx, size_t *pszRx) { pn53x_reset_settings(pnd); @@ -1548,7 +1548,7 @@ pn53x_target_init (struct nfc_device *pnd, nfc_target *pnt, uint8_t *pbtRx, size ptm = PTM_PASSIVE_ONLY; if ((pnt->nti.nai.abtUid[0] != 0x08) || (pnt->nti.nai.szUidLen != 4)) { pnd->last_error = NFC_EINVARG; - return false; + return pnd->last_error; } pn53x_set_parameters (pnd, PARAM_AUTO_ATR_RES, false); if (CHIP_DATA(pnd)->type == PN532) { // We have a PN532 @@ -1577,13 +1577,13 @@ pn53x_target_init (struct nfc_device *pnd, nfc_target *pnt, uint8_t *pbtRx, size case NMT_ISO14443B2CT: case NMT_JEWEL: pnd->last_error = NFC_EDEVNOTSUPP; - return false; + return pnd->last_error; break; } // Let the PN53X be activated by the RF level detector from power down mode if (!pn53x_write_register (pnd, PN53X_REG_CIU_TxAuto, SYMBOL_INITIAL_RF_ON, 0x04)) - return false; + return NFC_ECHIP; uint8_t abtMifareParams[6]; uint8_t *pbtMifareParams = NULL; @@ -1679,7 +1679,7 @@ pn53x_target_init (struct nfc_device *pnd, nfc_target *pnt, uint8_t *pbtRx, size case NMT_ISO14443B2CT: case NMT_JEWEL: pnd->last_error = NFC_EDEVNOTSUPP; - return false; + return pnd->last_error; break; } @@ -1688,7 +1688,7 @@ pn53x_target_init (struct nfc_device *pnd, nfc_target *pnt, uint8_t *pbtRx, size uint8_t btActivatedMode; if(!pn53x_TgInitAsTarget(pnd, ptm, pbtMifareParams, pbtTkt, szTkt, pbtFeliCaParams, pbtNFCID3t, pbtGBt, szGBt, pbtRx, pszRx, &btActivatedMode)) { - return false; + return NFC_ECHIP; } nfc_modulation nm = { @@ -1752,7 +1752,7 @@ pn53x_target_init (struct nfc_device *pnd, nfc_target *pnt, uint8_t *pbtRx, size } } - return true; + return NFC_SUCCESS; } bool diff --git a/libnfc/chips/pn53x.h b/libnfc/chips/pn53x.h index 91cb353..f453f9a 100644 --- a/libnfc/chips/pn53x.h +++ b/libnfc/chips/pn53x.h @@ -312,7 +312,7 @@ bool pn53x_initiator_transceive_bytes_timed (struct nfc_device *pnd, const ui bool pn53x_initiator_deselect_target (struct nfc_device *pnd); // NFC device as Target functions -bool pn53x_target_init (struct nfc_device *pnd, nfc_target *pnt, uint8_t *pbtRx, size_t *pszRx); +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); bool pn53x_target_send_bits (struct nfc_device *pnd, const uint8_t *pbtTx, const size_t szTxBits, const uint8_t *pbtTxPar); diff --git a/libnfc/nfc-emulation.c b/libnfc/nfc-emulation.c index c8a7af9..592c555 100644 --- a/libnfc/nfc-emulation.c +++ b/libnfc/nfc-emulation.c @@ -35,7 +35,7 @@ nfc_emulate_target (nfc_device *pnd, struct nfc_emulator *emulator) uint8_t abtTx[ISO7816_SHORT_C_APDU_MAX_LEN]; int res = 0; - if (!nfc_target_init (pnd, emulator->target, abtRx, &szRx)) { + if (nfc_target_init (pnd, emulator->target, abtRx, &szRx) < 0) { return -1; } diff --git a/libnfc/nfc-internal.h b/libnfc/nfc-internal.h index 7a5320d..f5d054b 100644 --- a/libnfc/nfc-internal.h +++ b/libnfc/nfc-internal.h @@ -142,7 +142,7 @@ struct nfc_driver_t { 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); - bool (*target_init) (struct nfc_device *pnd, nfc_target * pnt, uint8_t * pbtRx, size_t * pszRx); + 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); bool (*target_send_bits) (struct nfc_device *pnd, const uint8_t * pbtTx, const size_t szTxBits, const uint8_t * pbtTxPar); diff --git a/libnfc/nfc.c b/libnfc/nfc.c index 6c5e0fc..b92735b 100644 --- a/libnfc/nfc.c +++ b/libnfc/nfc.c @@ -601,7 +601,7 @@ nfc_initiator_transceive_bits_timed (nfc_device *pnd, const uint8_t *pbtTx, cons /** * @brief Initialize NFC device as an emulated tag - * @return Returns \c true if action was successfully performed; otherwise returns \c false. + * @return Returns 0 on success, otherwise returns libnfc's error code * * @param pnd \a nfc_device struct pointer that represent currently used device * @param ntm target mode restriction that you want to emulate (eg. NTM_PASSIVE_ONLY) @@ -629,31 +629,32 @@ nfc_initiator_transceive_bits_timed (nfc_device *pnd, const uint8_t *pbtTx, cons * example would wake up the emulator. After this is received, the send and * receive functions can be used. */ -bool +int nfc_target_init (nfc_device *pnd, nfc_target *pnt, uint8_t *pbtRx, size_t * pszRx) { + int res = 0; // Disallow invalid frame - if (nfc_device_set_property_bool (pnd, NP_ACCEPT_INVALID_FRAMES, false) < 0) + if ((res = nfc_device_set_property_bool (pnd, NP_ACCEPT_INVALID_FRAMES, false)) < 0) return false; // Disallow multiple frames - if (nfc_device_set_property_bool (pnd, NP_ACCEPT_MULTIPLE_FRAMES, false) < 0) + if ((res = nfc_device_set_property_bool (pnd, NP_ACCEPT_MULTIPLE_FRAMES, false)) < 0) return false; // Make sure we reset the CRC and parity to chip handling. - if (nfc_device_set_property_bool (pnd, NP_HANDLE_CRC, true) < 0) + if ((res = 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) + if ((res = nfc_device_set_property_bool (pnd, NP_HANDLE_PARITY, true)) < 0) return false; // Activate auto ISO14443-4 switching by default - if (nfc_device_set_property_bool (pnd, NP_AUTO_ISO14443_4, true) < 0) + if ((res = nfc_device_set_property_bool (pnd, NP_AUTO_ISO14443_4, true)) < 0) return false; // Activate "easy framing" feature by default - if (nfc_device_set_property_bool (pnd, NP_EASY_FRAMING, true) < 0) + if ((res = nfc_device_set_property_bool (pnd, NP_EASY_FRAMING, true)) < 0) return false; // Deactivate the CRYPTO1 cipher, it may could cause problems when still active - if (nfc_device_set_property_bool (pnd, NP_ACTIVATE_CRYPTO1, false) < 0) + if ((res = nfc_device_set_property_bool (pnd, NP_ACTIVATE_CRYPTO1, false)) < 0) return false; // Drop explicitely the field - if (nfc_device_set_property_bool (pnd, NP_ACTIVATE_FIELD, false) < 0) + if ((res = nfc_device_set_property_bool (pnd, NP_ACTIVATE_FIELD, false)) < 0) return false; HAL (target_init, pnd, pnt, pbtRx, pszRx); diff --git a/test/test_dep_active.c b/test/test_dep_active.c index b9101e8..a80eefa 100644 --- a/test/test_dep_active.c +++ b/test/test_dep_active.c @@ -84,11 +84,11 @@ target_thread (void *arg) uint8_t abtRx[1024]; size_t szRx = sizeof (abtRx); - bool res = nfc_target_init (device, &nt, abtRx, &szRx); - cut_assert_true (res, cut_message ("Can't initialize NFC device as target: %s", nfc_strerror (device))); - if (!res) { thread_res = -1; return (void*) thread_res; } + 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; } - res = nfc_target_receive_bytes (device, abtRx, &szRx, 500); + 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))); const uint8_t abtAttRx[] = "Hello DEP target!"; diff --git a/test/test_dep_passive.c b/test/test_dep_passive.c index a071517..cc80ece 100644 --- a/test/test_dep_passive.c +++ b/test/test_dep_passive.c @@ -82,13 +82,12 @@ target_thread (void *arg) uint8_t abtRx[1024]; size_t szRx = sizeof (abtRx); - bool res = nfc_target_init (device, &nt, abtRx, &szRx); - cut_assert_true (res, cut_message ("Can't initialize NFC device as target: %s", nfc_strerror (device))); - - if (!res) { thread_res = -1; return (void*) thread_res; } + 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; } // First pass - res = nfc_target_receive_bytes (device, abtRx, &szRx, 500); + 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))); const uint8_t abtAttRx[] = "Hello DEP target!"; diff --git a/utils/nfc-relay-picc.c b/utils/nfc-relay-picc.c index 06c0433..c150825 100644 --- a/utils/nfc-relay-picc.c +++ b/utils/nfc-relay-picc.c @@ -354,7 +354,7 @@ main (int argc, char *argv[]) printf ("Connected to the NFC emulator device: %s\n", nfc_device_get_name (pndTarget)); - if (!nfc_target_init (pndTarget, &ntEmulatedTarget, abtCapdu, &szCapduLen)) { + if (nfc_target_init (pndTarget, &ntEmulatedTarget, abtCapdu, &szCapduLen) < 0) { ERR ("%s", "Initialization of NFC emulator failed"); if (!target_only_mode) { nfc_disconnect (pndInitiator);