nfc_target_send_bytes() function returns now sent bytes count on success and libnfc error code on failure.

This commit is contained in:
Audrey Diacre 2011-12-22 15:59:08 +00:00
parent ac6f652368
commit 9c1371dcca
11 changed files with 51 additions and 50 deletions

View file

@ -133,7 +133,7 @@ main (int argc, const char *argv[])
printf ("Received: %s\n", abtRx);
printf ("Sending: %s\n", abtTx);
if (!nfc_target_send_bytes (pnd, abtTx, sizeof(abtTx), 0)) {
if (nfc_target_send_bytes (pnd, abtTx, sizeof(abtTx), 0) < 0) {
nfc_perror(pnd, "nfc_target_send_bytes");
goto error;
}

View file

@ -148,7 +148,7 @@ nfc_target_emulate_tag(nfc_device *pnd, nfc_target *pnt)
while ( loop ) {
loop = target_io( pnt, abtRx, szRx, abtTx, &szTx );
if (szTx) {
if (!nfc_target_send_bytes(pnd, abtTx, szTx, 0)) {
if (nfc_target_send_bytes(pnd, abtTx, szTx, 0) < 0) {
nfc_perror (pnd, "nfc_target_send_bytes");
return false;
}

View file

@ -84,7 +84,7 @@ extern "C" {
/* NFC target: act as tag (i.e. MIFARE Classic) or NFC target device. */
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 int nfc_target_send_bytes (nfc_device *pnd, const uint8_t *pbtTx, const size_t szTx, int timeout);
NFC_EXPORT int 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_receive_bits (nfc_device *pnd, uint8_t *pbtRx, size_t *pszRxBits, uint8_t *pbtRxPar);

View file

@ -1866,14 +1866,15 @@ pn53x_target_send_bits (struct nfc_device *pnd, const uint8_t *pbtTx, const size
return true;
}
bool
int
pn53x_target_send_bytes (struct nfc_device *pnd, const uint8_t *pbtTx, const size_t szTx, int timeout)
{
uint8_t abtCmd[PN53x_EXTENDED_FRAME__DATA_MAX_LEN];
int res = 0;
// We can not just send bytes without parity if while the PN53X expects we handled them
if (!pnd->bPar)
return false;
return NFC_ECHIP;
// XXX I think this is not a clean way to provide some kind of "EasyFraming"
// but at the moment I have no more better than this
@ -1892,7 +1893,7 @@ pn53x_target_send_bytes (struct nfc_device *pnd, const uint8_t *pbtTx, const siz
} else {
// TODO Support EasyFraming for other cases by software
pnd->last_error = NFC_ENOTIMPL;
return false;
return pnd->last_error;
}
}
default:
@ -1907,11 +1908,11 @@ pn53x_target_send_bytes (struct nfc_device *pnd, const uint8_t *pbtTx, const siz
memcpy (abtCmd + 1, pbtTx, szTx);
// Try to send the bits to the reader
if (pn53x_transceive (pnd, abtCmd, szTx + 1, NULL, NULL, timeout) < 0)
return false;
if ((res = pn53x_transceive (pnd, abtCmd, szTx + 1, NULL, NULL, timeout)) < 0)
return res;
// Everyting seems ok, return true
return true;
// Everyting seems ok, return sent byte count
return szTx;
}
static struct sErrorMessage {

View file

@ -316,7 +316,7 @@ int pn53x_target_init (struct nfc_device *pnd, nfc_target *pnt, uint8_t *pbtR
bool pn53x_target_receive_bits (struct nfc_device *pnd, uint8_t *pbtRx, size_t *pszRxBits, uint8_t *pbtRxPar);
int 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_bytes (struct nfc_device *pnd, const uint8_t *pbtTx, const size_t szTx, int timeout);
int pn53x_target_send_bytes (struct nfc_device *pnd, const uint8_t *pbtTx, const size_t szTx, int timeout);
// Error handling functions
const char *pn53x_strerror (const struct nfc_device *pnd);

View file

@ -42,7 +42,7 @@ nfc_emulate_target (nfc_device *pnd, struct nfc_emulator *emulator)
while (res >= 0) {
res = emulator->state_machine->io (emulator, abtRx, szRx, abtTx, sizeof (abtTx));
if (res > 0) {
if (!nfc_target_send_bytes(pnd, abtTx, res, 0)) {
if (nfc_target_send_bytes(pnd, abtTx, res, 0) < 0) {
return -1;
}
}

View file

@ -143,7 +143,7 @@ struct nfc_driver_t {
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);
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);
int (*target_send_bytes) (struct nfc_device *pnd, const uint8_t * pbtTx, const size_t szTx, int timeout);
int (*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_receive_bits) (struct nfc_device *pnd, uint8_t * pbtRx, size_t * pszRxBits, uint8_t * pbtRxPar);

View file

@ -695,7 +695,7 @@ nfc_abort_command (nfc_device *pnd)
/**
* @brief Send bytes and APDU frames
* @return Returns \c true if action was successfully performed; otherwise returns \c false.
* @return Returns sent bytes count on success, otherwise returns libnfc's error code
*
* @param pnd \a nfc_device struct pointer that represent currently used device
* @param pbtTx pointer to Tx buffer
@ -708,7 +708,7 @@ nfc_abort_command (nfc_device *pnd)
* If timeout is not a null pointer, it specifies the maximum interval to wait for the function to be executed.
* If timeout is a null pointer, the function blocks indefinitely (until an error is raised or function is completed).
*/
bool
int
nfc_target_send_bytes (nfc_device *pnd, const uint8_t *pbtTx, const size_t szTx, int timeout)
{
HAL (target_send_bytes, pnd, pbtTx, szTx, timeout);

View file

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

View file

@ -82,55 +82,55 @@ target_thread (void *arg)
uint8_t abtRx[1024];
size_t szRx = sizeof (abtRx);
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; }
int res = nfc_target_init (device, &nt, abtRx, &szRx);
cut_assert_equal_int (0, res, cut_message ("Can't initialize NFC device as target: %s", nfc_strerror (device)));
if (res < 0) { thread_res = -1; return (void*) thread_res; }
// First pass
ires = nfc_target_receive_bytes (device, abtRx, &szRx, 500);
cut_assert_operator_int (ires, >, 0, cut_message ("Can't receive bytes from initiator: %s", nfc_strerror (device)));
res = nfc_target_receive_bytes (device, abtRx, &szRx, 500);
cut_assert_operator_int (res, >, 0, cut_message ("Can't receive bytes from initiator: %s", nfc_strerror (device)));
const uint8_t abtAttRx[] = "Hello DEP target!";
cut_assert_equal_memory (abtAttRx, sizeof (abtAttRx), abtRx, szRx, cut_message ("Invalid received data"));
if (ires <= 0) { thread_res = -1; return (void*) thread_res; }
if (res <= 0) { thread_res = -1; return (void*) thread_res; }
const uint8_t abtTx[] = "Hello DEP initiator!";
bool res = nfc_target_send_bytes (device, abtTx, sizeof(abtTx), 500);
cut_assert_true (res, cut_message ("Can't send bytes to initiator: %s", nfc_strerror (device)));
if (!res) { thread_res = -1; return (void*) thread_res; }
res = nfc_target_send_bytes (device, abtTx, sizeof(abtTx), 500);
cut_assert_operator_int (res, >, 0, cut_message ("Can't send bytes to initiator: %s", nfc_strerror (device)));
if (res <= 0) { thread_res = -1; return (void*) thread_res; }
// Second pass
ires = nfc_target_receive_bytes (device, abtRx, &szRx, 500);
cut_assert_operator_int (ires, >, 0, cut_message ("Can't receive bytes from initiator: %s", nfc_strerror (device)));
res = nfc_target_receive_bytes (device, abtRx, &szRx, 500);
cut_assert_operator_int (res, >, 0, cut_message ("Can't receive bytes from initiator: %s", nfc_strerror (device)));
cut_assert_equal_memory (abtAttRx, sizeof (abtAttRx), abtRx, szRx, cut_message ("Invalid received data"));
if (ires <= 0) { thread_res = -1; return (void*) thread_res; }
if (res <= 0) { thread_res = -1; return (void*) thread_res; }
res = nfc_target_send_bytes (device, abtTx, sizeof(abtTx), 500);
cut_assert_true (res, cut_message ("Can't send bytes to initiator: %s", nfc_strerror (device)));
if (!res) { thread_res = -1; return (void*) thread_res; }
cut_assert_operator_int (res, >, 0, cut_message ("Can't send bytes to initiator: %s", nfc_strerror (device)));
if (res <= 0) { thread_res = -1; return (void*) thread_res; }
// Third pass
ires = nfc_target_receive_bytes (device, abtRx, &szRx, 500);
cut_assert_operator_int (ires, >, 0, cut_message ("Can't receive bytes from initiator: %s", nfc_strerror (device)));
res = nfc_target_receive_bytes (device, abtRx, &szRx, 500);
cut_assert_operator_int (res, >, 0, cut_message ("Can't receive bytes from initiator: %s", nfc_strerror (device)));
cut_assert_equal_memory (abtAttRx, sizeof (abtAttRx), abtRx, szRx, cut_message ("Invalid received data"));
if (ires <= 0) { thread_res = -1; return (void*) thread_res; }
if (res <= 0) { thread_res = -1; return (void*) thread_res; }
res = nfc_target_send_bytes (device, abtTx, sizeof(abtTx), 500);
cut_assert_true (res, cut_message ("Can't send bytes to initiator: %s", nfc_strerror (device)));
if (!res) { thread_res = -1; return (void*) thread_res; }
cut_assert_operator_int (res, >, 0, cut_message ("Can't send bytes to initiator: %s", nfc_strerror (device)));
if (res <= 0) { thread_res = -1; return (void*) thread_res; }
// Fourth pass
ires = nfc_target_receive_bytes (device, abtRx, &szRx, 500);
cut_assert_operator_int (ires, >, 0, cut_message ("Can't receive bytes from initiator: %s", nfc_strerror (device)));
res = nfc_target_receive_bytes (device, abtRx, &szRx, 500);
cut_assert_operator_int (res, >, 0, cut_message ("Can't receive bytes from initiator: %s", nfc_strerror (device)));
cut_assert_equal_memory (abtAttRx, sizeof (abtAttRx), abtRx, szRx, cut_message ("Invalid received data"));
if (ires <= 0) { thread_res = -1; return (void*) thread_res; }
if (res <= 0) { thread_res = -1; return (void*) thread_res; }
res = nfc_target_send_bytes (device, abtTx, sizeof(abtTx), 500);
cut_assert_true (res, cut_message ("Can't send bytes to initiator: %s", nfc_strerror (device)));
if (!res) { thread_res = -1; return (void*) thread_res; }
cut_assert_operator_int (res, >, 0, cut_message ("Can't send bytes to initiator: %s", nfc_strerror (device)));
if (res <= 0) { thread_res = -1; return (void*) thread_res; }
return (void *) thread_res;
}

View file

@ -425,7 +425,7 @@ main (int argc, char *argv[])
}
if (!initiator_only_mode) {
// Transmit the response bytes
if (!nfc_target_send_bytes(pndTarget, abtRapdu, szRapduLen, 0)) {
if (nfc_target_send_bytes(pndTarget, abtRapdu, szRapduLen, 0) < 0) {
nfc_perror (pndTarget, "nfc_target_send_bytes");
if (!target_only_mode) {
nfc_disconnect (pndInitiator);