nfc_target_init() function returns now 0 on succes and libnfc error code on failure.

This commit is contained in:
Audrey Diacre 2011-12-19 15:35:37 +00:00
parent 145cc4b2ad
commit ba2a7cfe2e
14 changed files with 36 additions and 36 deletions

View file

@ -119,7 +119,7 @@ main (int argc, const char *argv[])
print_nfc_target (nt, false); print_nfc_target (nt, false);
printf ("Waiting for initiator request...\n"); 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"); nfc_perror(pnd, "nfc_target_init");
goto error; goto error;
} }

View file

@ -140,7 +140,7 @@ nfc_target_emulate_tag(nfc_device *pnd, nfc_target *pnt)
uint8_t abtTx[MAX_FRAME_LEN]; uint8_t abtTx[MAX_FRAME_LEN];
bool loop = true; 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"); nfc_perror (pnd, "nfc_target_init");
return false; return false;
} }

View file

@ -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"); nfc_perror (pnd, "nfc_target_init");
ERR ("Could not come out of auto-emulation, no command was received"); ERR ("Could not come out of auto-emulation, no command was received");
goto error; goto error;

View file

@ -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"); ERR ("%s", "Initialization of NFC emulator failed");
nfc_disconnect (pndTag); nfc_disconnect (pndTag);
return EXIT_FAILURE; return EXIT_FAILURE;

View file

@ -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 ("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"); 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"); nfc_perror(pnd, "nfc_target_init");
return EXIT_FAILURE; return EXIT_FAILURE;
} }

View file

@ -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_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 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_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_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); NFC_EXPORT bool nfc_target_send_bits (nfc_device *pnd, const uint8_t *pbtTx, const size_t szTxBits, const uint8_t *pbtTxPar);

View file

@ -1534,7 +1534,7 @@ pn53x_initiator_deselect_target (struct nfc_device *pnd)
#define SAK_ISO14443_4_COMPLIANT 0x20 #define SAK_ISO14443_4_COMPLIANT 0x20
#define SAK_ISO18092_COMPLIANT 0x40 #define SAK_ISO18092_COMPLIANT 0x40
bool int
pn53x_target_init (struct nfc_device *pnd, nfc_target *pnt, uint8_t *pbtRx, size_t *pszRx) pn53x_target_init (struct nfc_device *pnd, nfc_target *pnt, uint8_t *pbtRx, size_t *pszRx)
{ {
pn53x_reset_settings(pnd); 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; ptm = PTM_PASSIVE_ONLY;
if ((pnt->nti.nai.abtUid[0] != 0x08) || (pnt->nti.nai.szUidLen != 4)) { if ((pnt->nti.nai.abtUid[0] != 0x08) || (pnt->nti.nai.szUidLen != 4)) {
pnd->last_error = NFC_EINVARG; pnd->last_error = NFC_EINVARG;
return false; return pnd->last_error;
} }
pn53x_set_parameters (pnd, PARAM_AUTO_ATR_RES, false); pn53x_set_parameters (pnd, PARAM_AUTO_ATR_RES, false);
if (CHIP_DATA(pnd)->type == PN532) { // We have a PN532 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_ISO14443B2CT:
case NMT_JEWEL: case NMT_JEWEL:
pnd->last_error = NFC_EDEVNOTSUPP; pnd->last_error = NFC_EDEVNOTSUPP;
return false; return pnd->last_error;
break; break;
} }
// Let the PN53X be activated by the RF level detector from power down mode // 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)) 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 abtMifareParams[6];
uint8_t *pbtMifareParams = NULL; 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_ISO14443B2CT:
case NMT_JEWEL: case NMT_JEWEL:
pnd->last_error = NFC_EDEVNOTSUPP; pnd->last_error = NFC_EDEVNOTSUPP;
return false; return pnd->last_error;
break; break;
} }
@ -1688,7 +1688,7 @@ pn53x_target_init (struct nfc_device *pnd, nfc_target *pnt, uint8_t *pbtRx, size
uint8_t btActivatedMode; uint8_t btActivatedMode;
if(!pn53x_TgInitAsTarget(pnd, ptm, pbtMifareParams, pbtTkt, szTkt, pbtFeliCaParams, pbtNFCID3t, pbtGBt, szGBt, pbtRx, pszRx, &btActivatedMode)) { if(!pn53x_TgInitAsTarget(pnd, ptm, pbtMifareParams, pbtTkt, szTkt, pbtFeliCaParams, pbtNFCID3t, pbtGBt, szGBt, pbtRx, pszRx, &btActivatedMode)) {
return false; return NFC_ECHIP;
} }
nfc_modulation nm = { 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 bool

View file

@ -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); bool pn53x_initiator_deselect_target (struct nfc_device *pnd);
// NFC device as Target functions // 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_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_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); bool pn53x_target_send_bits (struct nfc_device *pnd, const uint8_t *pbtTx, const size_t szTxBits, const uint8_t *pbtTxPar);

View file

@ -35,7 +35,7 @@ nfc_emulate_target (nfc_device *pnd, struct nfc_emulator *emulator)
uint8_t abtTx[ISO7816_SHORT_C_APDU_MAX_LEN]; uint8_t abtTx[ISO7816_SHORT_C_APDU_MAX_LEN];
int res = 0; int res = 0;
if (!nfc_target_init (pnd, emulator->target, abtRx, &szRx)) { if (nfc_target_init (pnd, emulator->target, abtRx, &szRx) < 0) {
return -1; return -1;
} }

View file

@ -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_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 (*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_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_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); bool (*target_send_bits) (struct nfc_device *pnd, const uint8_t * pbtTx, const size_t szTxBits, const uint8_t * pbtTxPar);

View file

@ -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 * @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 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) * @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 * example would wake up the emulator. After this is received, the send and
* receive functions can be used. * receive functions can be used.
*/ */
bool int
nfc_target_init (nfc_device *pnd, nfc_target *pnt, uint8_t *pbtRx, size_t * pszRx) nfc_target_init (nfc_device *pnd, nfc_target *pnt, uint8_t *pbtRx, size_t * pszRx)
{ {
int res = 0;
// Disallow invalid frame // 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; return false;
// Disallow multiple frames // 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; return false;
// Make sure we reset the CRC and parity to chip handling. // 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; 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; return false;
// Activate auto ISO14443-4 switching by default // 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; return false;
// Activate "easy framing" feature by default // 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; return false;
// Deactivate the CRYPTO1 cipher, it may could cause problems when still active // 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; return false;
// Drop explicitely the field // 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; return false;
HAL (target_init, pnd, pnt, pbtRx, pszRx); HAL (target_init, pnd, pnt, pbtRx, pszRx);

View file

@ -84,11 +84,11 @@ target_thread (void *arg)
uint8_t abtRx[1024]; uint8_t abtRx[1024];
size_t szRx = sizeof (abtRx); size_t szRx = sizeof (abtRx);
bool res = nfc_target_init (device, &nt, abtRx, &szRx); int ires = nfc_target_init (device, &nt, abtRx, &szRx);
cut_assert_true (res, cut_message ("Can't initialize NFC device as target: %s", nfc_strerror (device))); cut_assert_equal_int (0, ires, cut_message ("Can't initialize NFC device as target: %s", nfc_strerror (device)));
if (!res) { thread_res = -1; return (void*) thread_res; } 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))); cut_assert_true (res, cut_message ("Can't receive bytes from initiator: %s", nfc_strerror (device)));
const uint8_t abtAttRx[] = "Hello DEP target!"; const uint8_t abtAttRx[] = "Hello DEP target!";

View file

@ -82,13 +82,12 @@ target_thread (void *arg)
uint8_t abtRx[1024]; uint8_t abtRx[1024];
size_t szRx = sizeof (abtRx); size_t szRx = sizeof (abtRx);
bool res = nfc_target_init (device, &nt, abtRx, &szRx); int ires = nfc_target_init (device, &nt, abtRx, &szRx);
cut_assert_true (res, cut_message ("Can't initialize NFC device as target: %s", nfc_strerror (device))); 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; }
if (!res) { thread_res = -1; return (void*) thread_res; }
// First pass // 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))); cut_assert_true (res, cut_message ("Can't receive bytes from initiator: %s", nfc_strerror (device)));
const uint8_t abtAttRx[] = "Hello DEP target!"; const uint8_t abtAttRx[] = "Hello DEP target!";

View file

@ -354,7 +354,7 @@ main (int argc, char *argv[])
printf ("Connected to the NFC emulator device: %s\n", nfc_device_get_name (pndTarget)); 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"); ERR ("%s", "Initialization of NFC emulator failed");
if (!target_only_mode) { if (!target_only_mode) {
nfc_disconnect (pndInitiator); nfc_disconnect (pndInitiator);