nfc_target_init() function returns now 0 on succes and libnfc error code on failure.
This commit is contained in:
parent
145cc4b2ad
commit
ba2a7cfe2e
14 changed files with 36 additions and 36 deletions
|
@ -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;
|
||||
}
|
||||
|
|
|
@ -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;
|
||||
}
|
||||
|
|
|
@ -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;
|
||||
|
|
|
@ -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;
|
||||
|
|
|
@ -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;
|
||||
}
|
||||
|
|
|
@ -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);
|
||||
|
|
|
@ -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
|
||||
|
|
|
@ -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);
|
||||
|
|
|
@ -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;
|
||||
}
|
||||
|
||||
|
|
|
@ -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);
|
||||
|
|
21
libnfc/nfc.c
21
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);
|
||||
|
|
|
@ -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!";
|
||||
|
|
|
@ -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!";
|
||||
|
|
|
@ -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);
|
||||
|
|
Loading…
Reference in a new issue