From 481fb4943fa5d8c2f487851f0c488d620a38e10d Mon Sep 17 00:00:00 2001 From: Romain Tartiere Date: Thu, 22 Sep 2011 13:03:47 +0000 Subject: [PATCH] - Add timeout capablities to nfc_initiator_transceive_bytes(), nfc_target_send_bytes() and nfc_target_receive_bytes(); - Bump version to 1.5.1. --- AUTHORS | 1 + CMakeLists.txt | 2 +- ChangeLog | 18 ++++++- Makefile.am | 2 +- configure.ac | 2 +- examples/mifare.c | 2 +- examples/nfc-anticol.c | 2 +- examples/nfc-dep-initiator.c | 2 +- examples/nfc-dep-target.c | 4 +- examples/nfc-emulate-tag.c | 4 +- examples/nfc-mfclassic.c | 2 +- examples/nfc-mfsetuid.c | 2 +- examples/nfc-relay-picc.c | 6 +-- examples/pn53x-diagnose.c | 6 +-- examples/pn53x-tamashell.c | 2 +- include/nfc/nfc.h | 8 ++-- libnfc/buses/uart.h | 6 ++- libnfc/buses/uart_posix.c | 12 +++-- libnfc/chips/pn53x.c | 92 ++++++++++++++++++------------------ libnfc/chips/pn53x.h | 12 ++--- libnfc/drivers/acr122.c | 10 +++- libnfc/drivers/acr122.h | 4 +- libnfc/drivers/arygon.c | 32 ++++++------- libnfc/drivers/arygon.h | 6 ++- libnfc/drivers/pn532_uart.c | 26 +++++----- libnfc/drivers/pn532_uart.h | 6 ++- libnfc/drivers/pn53x_usb.c | 65 +++++++++++++------------ libnfc/drivers/pn53x_usb.h | 6 ++- libnfc/log.h | 8 +++- libnfc/nfc-emulation.c | 4 +- libnfc/nfc-internal.h | 6 +-- libnfc/nfc.c | 12 ++--- 32 files changed, 210 insertions(+), 162 deletions(-) diff --git a/AUTHORS b/AUTHORS index 4d0ad85..13e8b34 100644 --- a/AUTHORS +++ b/AUTHORS @@ -1,2 +1,3 @@ Roel Verdult +Romain Tartière Romuald Conty diff --git a/CMakeLists.txt b/CMakeLists.txt index 74185e7..222577b 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -2,7 +2,7 @@ PROJECT(libnfc C) CMAKE_MINIMUM_REQUIRED(VERSION 2.6) SET(VERSION_MAJOR "1") SET(VERSION_MINOR "5") -SET(VERSION_PATCH "0") +SET(VERSION_PATCH "1") SET(VERSION "${VERSION_MAJOR}.${VERSION_MINOR}.${VERSION_PATCH}") diff --git a/ChangeLog b/ChangeLog index 8bc80bd..696c2ee 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,4 +1,14 @@ +Sept 21, 2011 - 1.5.1 (unstable) ------------------------------- + +Improvements: + - libnfc: add logging facility using log4c. + +Changes: + - libnfc: add 'struct timeval *timeout' parameter for pn53x_transceive(), pn53x_target_receive_bytes() and pn53x_target_send_bytes(). + + + Apr 29, 2011 - 1.5.0 (unstable) ------------------------------- @@ -64,7 +74,8 @@ Changes: - drivers/pn53x_usb: pn531_usb and pn533_usb drivers are now merged and use the pn53x IC version autodetection - buses/uart: use a smart way to determine available ports on POSIX systems (tested on Linux and FreeBSD) --------------------- + + Feb 21, 2011 - 1.4.2 -------------------- @@ -87,6 +98,7 @@ Improvements: - documentation: add more pcsc-lite related instruction in README + Feb 1, 2011 - 1.4.1 ------------------- @@ -110,6 +122,7 @@ Improvements: Experimental: Windows platform support (Thanks to Glenn Ergeerts) + Nov 17, 2010 - 1.4.0 -------------------- @@ -163,6 +176,7 @@ Changes: - examples: change examples license for the sake of consistency: LGPL covers library, re-usable examples code is now under BSD license. + Aug 31, 2010 - 1.3.9 -------------------- @@ -268,7 +282,7 @@ Fixes: - libnfc: Fix tag re-selection with UID length > 4 bytes (like DESFire or Ultralight); - nfc-mfclassic: Fix authenticated only with KEYA; -Improvments: +Improvements: - build: Build on FreeBSD; - build: Add alternative build system (CMake); - build: Add new files usefull for desktop GNU/Linux users: some rules for udev to allow non-root access to PN53x USB devices; diff --git a/Makefile.am b/Makefile.am index 43aacf2..43d4ab6 100644 --- a/Makefile.am +++ b/Makefile.am @@ -14,7 +14,7 @@ EXTRA_DIST = \ pn53x.conf \ pn53x.rules -CLEANFILES = Doxygen.log coverage.info +CLEANFILES = Doxygen.log coverage.info libnfc.pc clean-local: clean-local-doc clean-local-coverage diff --git a/configure.ac b/configure.ac index 36a75b5..f09e000 100644 --- a/configure.ac +++ b/configure.ac @@ -1,7 +1,7 @@ # General init # /!\ Don't forget to update 'CMakeLists.txt' too /!\ -AC_INIT(libnfc, 1.5.0, info@libnfc.org) +AC_INIT(libnfc, 1.5.1, info@libnfc.org) AC_CONFIG_MACRO_DIR([m4]) diff --git a/examples/mifare.c b/examples/mifare.c index 6f94420..ef9d147 100644 --- a/examples/mifare.c +++ b/examples/mifare.c @@ -100,7 +100,7 @@ nfc_initiator_mifare_cmd (nfc_device_t * pnd, const mifare_cmd mc, const uint8_t return false; } // Fire the mifare command - if (!nfc_initiator_transceive_bytes (pnd, abtCmd, 2 + szParamLen, abtRx, &szRx)) { + if (!nfc_initiator_transceive_bytes (pnd, abtCmd, 2 + szParamLen, abtRx, &szRx, NULL)) { if (pnd->iLastError == EINVRXFRAM) { // "Invalid received frame" AKA EINVRXFRAM, usual means we are // authenticated on a sector but the requested MIFARE cmd (read, write) diff --git a/examples/nfc-anticol.c b/examples/nfc-anticol.c index 1e444bc..068527b 100644 --- a/examples/nfc-anticol.c +++ b/examples/nfc-anticol.c @@ -105,7 +105,7 @@ transmit_bytes (const byte_t * pbtTx, const size_t szTx) print_hex (pbtTx, szTx); } // Transmit the command bytes - if (!nfc_initiator_transceive_bytes (pnd, pbtTx, szTx, abtRx, &szRx)) + if (!nfc_initiator_transceive_bytes (pnd, pbtTx, szTx, abtRx, &szRx, NULL)) return false; // Show received answer diff --git a/examples/nfc-dep-initiator.c b/examples/nfc-dep-initiator.c index e3061b8..c4a9bcd 100644 --- a/examples/nfc-dep-initiator.c +++ b/examples/nfc-dep-initiator.c @@ -93,7 +93,7 @@ main (int argc, const char *argv[]) print_nfc_target (nt, false); printf ("Sending: %s\n", abtTx); - if (!nfc_initiator_transceive_bytes (pnd, abtTx, sizeof(abtTx), abtRx, &szRx)) { + if (!nfc_initiator_transceive_bytes (pnd, abtTx, sizeof(abtTx), abtRx, &szRx, NULL)) { nfc_perror(pnd, "nfc_initiator_transceive_bytes"); goto error; } diff --git a/examples/nfc-dep-target.c b/examples/nfc-dep-target.c index 19eede6..c81edba 100644 --- a/examples/nfc-dep-target.c +++ b/examples/nfc-dep-target.c @@ -125,7 +125,7 @@ main (int argc, const char *argv[]) } printf("Initiator request received. Waiting for data...\n"); - if (!nfc_target_receive_bytes (pnd, abtRx, &szRx)) { + if (!nfc_target_receive_bytes (pnd, abtRx, &szRx, NULL)) { nfc_perror(pnd, "nfc_target_receive_bytes"); goto error; } @@ -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))) { + if (!nfc_target_send_bytes (pnd, abtTx, sizeof(abtTx), NULL)) { nfc_perror(pnd, "nfc_target_send_bytes"); goto error; } diff --git a/examples/nfc-emulate-tag.c b/examples/nfc-emulate-tag.c index f4765c8..4937c54 100644 --- a/examples/nfc-emulate-tag.c +++ b/examples/nfc-emulate-tag.c @@ -148,7 +148,7 @@ nfc_target_emulate_tag(nfc_device_t* pnd, nfc_target_t * pnt) while ( loop ) { loop = target_io( pnt, abtRx, szRx, abtTx, &szTx ); if (szTx) { - if (!nfc_target_send_bytes(pnd, abtTx, szTx)) { + if (!nfc_target_send_bytes(pnd, abtTx, szTx, NULL)) { nfc_perror (pnd, "nfc_target_send_bytes"); return false; } @@ -158,7 +158,7 @@ nfc_target_emulate_tag(nfc_device_t* pnd, nfc_target_t * pnt) nfc_configure (pnd, NDO_HANDLE_CRC, false); init_mfc_auth = false; } - if (!nfc_target_receive_bytes(pnd, abtRx, &szRx)) { + if (!nfc_target_receive_bytes(pnd, abtRx, &szRx, NULL)) { nfc_perror (pnd, "nfc_target_receive_bytes"); return false; } diff --git a/examples/nfc-mfclassic.c b/examples/nfc-mfclassic.c index 6f0708f..c3d7d95 100644 --- a/examples/nfc-mfclassic.c +++ b/examples/nfc-mfclassic.c @@ -116,7 +116,7 @@ transmit_bytes (const byte_t * pbtTx, const size_t szTx) printf ("Sent bits: "); print_hex (pbtTx, szTx); // Transmit the command bytes - if (!nfc_initiator_transceive_bytes (pnd, pbtTx, szTx, abtRx, &szRx)) + if (!nfc_initiator_transceive_bytes (pnd, pbtTx, szTx, abtRx, &szRx, NULL)) return false; // Show received answer diff --git a/examples/nfc-mfsetuid.c b/examples/nfc-mfsetuid.c index 0c65bd1..4134acc 100644 --- a/examples/nfc-mfsetuid.c +++ b/examples/nfc-mfsetuid.c @@ -118,7 +118,7 @@ transmit_bytes (const byte_t * pbtTx, const size_t szTx) print_hex (pbtTx, szTx); } // Transmit the command bytes - if (!nfc_initiator_transceive_bytes (pnd, pbtTx, szTx, abtRx, &szRx)) + if (!nfc_initiator_transceive_bytes (pnd, pbtTx, szTx, abtRx, &szRx, NULL)) return false; // Show received answer diff --git a/examples/nfc-relay-picc.c b/examples/nfc-relay-picc.c index b72b940..614e787 100644 --- a/examples/nfc-relay-picc.c +++ b/examples/nfc-relay-picc.c @@ -375,7 +375,7 @@ main (int argc, char *argv[]) bool ret; if (!initiator_only_mode) { // Receive external reader command through target - if (!nfc_target_receive_bytes(pndTarget,abtCapdu,&szCapduLen)) { + if (!nfc_target_receive_bytes(pndTarget,abtCapdu,&szCapduLen, NULL)) { nfc_perror (pndTarget, "nfc_target_receive_bytes"); if (!target_only_mode) { nfc_disconnect (pndInitiator); @@ -406,7 +406,7 @@ main (int argc, char *argv[]) if (!target_only_mode) { // Forward the frame to the original tag ret = nfc_initiator_transceive_bytes - (pndInitiator, abtCapdu, szCapduLen, abtRapdu, &szRapduLen); + (pndInitiator, abtCapdu, szCapduLen, abtRapdu, &szRapduLen, NULL); } else { if (scan_hex_fd3(abtRapdu, &szRapduLen, "R-APDU") != EXIT_SUCCESS) { fprintf (stderr, "Error while scanning R-APDU from FD3\n"); @@ -430,7 +430,7 @@ main (int argc, char *argv[]) } if (!initiator_only_mode) { // Transmit the response bytes - if (!nfc_target_send_bytes(pndTarget, abtRapdu, szRapduLen)) { + if (!nfc_target_send_bytes(pndTarget, abtRapdu, szRapduLen, NULL)) { nfc_perror (pndTarget, "nfc_target_send_bytes"); if (!target_only_mode) { nfc_disconnect (pndInitiator); diff --git a/examples/pn53x-diagnose.c b/examples/pn53x-diagnose.c index d6c83e5..2bfbb2a 100644 --- a/examples/pn53x-diagnose.c +++ b/examples/pn53x-diagnose.c @@ -93,7 +93,7 @@ main (int argc, const char *argv[]) printf ("NFC device [%s] connected.\n", pnd->acName); - result = pn53x_transceive (pnd, pncmd_diagnose_communication_line_test, sizeof (pncmd_diagnose_communication_line_test), abtRx, &szRx); + result = pn53x_transceive (pnd, pncmd_diagnose_communication_line_test, sizeof (pncmd_diagnose_communication_line_test), abtRx, &szRx, NULL); if (result) { // Result of Diagnose ping for RC-S360 doesn't contain status byte so we've to handle both cases result = (memcmp (pncmd_diagnose_communication_line_test + 1, abtRx, sizeof (pncmd_diagnose_communication_line_test) - 1) == 0) || @@ -103,7 +103,7 @@ main (int argc, const char *argv[]) } printf (" Communication line test: %s\n", result ? "OK" : "Failed"); - result = pn53x_transceive (pnd, pncmd_diagnose_rom_test, sizeof (pncmd_diagnose_rom_test), abtRx, &szRx); + result = pn53x_transceive (pnd, pncmd_diagnose_rom_test, sizeof (pncmd_diagnose_rom_test), abtRx, &szRx, NULL); if (result) { result = ((szRx == 1) && (abtRx[0] == 0x00)); } else { @@ -111,7 +111,7 @@ main (int argc, const char *argv[]) } printf (" ROM test: %s\n", result ? "OK" : "Failed"); - result = pn53x_transceive (pnd, pncmd_diagnose_ram_test, sizeof (pncmd_diagnose_ram_test), abtRx, &szRx); + result = pn53x_transceive (pnd, pncmd_diagnose_ram_test, sizeof (pncmd_diagnose_ram_test), abtRx, &szRx, NULL); if (result) { result = ((szRx == 1) && (abtRx[0] == 0x00)); } else { diff --git a/examples/pn53x-tamashell.c b/examples/pn53x-tamashell.c index e874905..fd87bbc 100644 --- a/examples/pn53x-tamashell.c +++ b/examples/pn53x-tamashell.c @@ -179,7 +179,7 @@ int main(int argc, const char* argv[]) print_hex((byte_t*)abtTx,szTx); szRx = sizeof(abtRx); - if (!pn53x_transceive (pnd, abtTx, szTx, abtRx, &szRx)) { + if (!pn53x_transceive (pnd, abtTx, szTx, abtRx, &szRx, NULL)) { free(cmd); nfc_perror (pnd, "Rx"); continue; diff --git a/include/nfc/nfc.h b/include/nfc/nfc.h index 51b427d..6cde485 100644 --- a/include/nfc/nfc.h +++ b/include/nfc/nfc.h @@ -28,6 +28,8 @@ #ifndef _LIBNFC_H_ # define _LIBNFC_H_ +# include + # include # include @@ -75,15 +77,15 @@ extern "C" { NFC_EXPORT bool nfc_initiator_poll_targets (nfc_device_t * pnd, const nfc_modulation_t * pnmTargetTypes, const size_t szTargetTypes, const byte_t btPollNr, const byte_t btPeriod, nfc_target_t * pntTargets, size_t * pszTargetFound); NFC_EXPORT bool nfc_initiator_select_dep_target (nfc_device_t * pnd, const nfc_dep_mode_t ndm, const nfc_baud_rate_t nbr, const nfc_dep_info_t * pndiInitiator, nfc_target_t * pnt); NFC_EXPORT bool nfc_initiator_deselect_target (nfc_device_t * pnd); - NFC_EXPORT bool nfc_initiator_transceive_bytes (nfc_device_t * pnd, const byte_t * pbtTx, const size_t szTx, byte_t * pbtRx, size_t * pszRx); + NFC_EXPORT bool nfc_initiator_transceive_bytes (nfc_device_t * pnd, const byte_t * pbtTx, const size_t szTx, byte_t * pbtRx, size_t * pszRx, struct timeval *timeout); NFC_EXPORT bool nfc_initiator_transceive_bits (nfc_device_t * pnd, const byte_t * pbtTx, const size_t szTxBits, const byte_t * pbtTxPar, byte_t * pbtRx, size_t * pszRxBits, byte_t * pbtRxPar); NFC_EXPORT bool nfc_initiator_transceive_bytes_timed (nfc_device_t * pnd, const byte_t * pbtTx, const size_t szTx, byte_t * pbtRx, size_t * pszRx, uint32_t * cycles); NFC_EXPORT bool nfc_initiator_transceive_bits_timed (nfc_device_t * pnd, const byte_t * pbtTx, const size_t szTxBits, const byte_t * pbtTxPar, byte_t * pbtRx, size_t * pszRxBits, byte_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_t * pnd, nfc_target_t * pnt, byte_t * pbtRx, size_t * pszRx); - NFC_EXPORT bool nfc_target_send_bytes (nfc_device_t * pnd, const byte_t * pbtTx, const size_t szTx); - NFC_EXPORT bool nfc_target_receive_bytes (nfc_device_t * pnd, byte_t * pbtRx, size_t * pszRx); + NFC_EXPORT bool nfc_target_send_bytes (nfc_device_t * pnd, const byte_t * pbtTx, const size_t szTx, struct timeval *timout); + NFC_EXPORT bool nfc_target_receive_bytes (nfc_device_t * pnd, byte_t * pbtRx, size_t * pszRx, struct timeval *timout); NFC_EXPORT bool nfc_target_send_bits (nfc_device_t * pnd, const byte_t * pbtTx, const size_t szTxBits, const byte_t * pbtTxPar); NFC_EXPORT bool nfc_target_receive_bits (nfc_device_t * pnd, byte_t * pbtRx, size_t * pszRxBits, byte_t * pbtRxPar); diff --git a/libnfc/buses/uart.h b/libnfc/buses/uart.h index 5d52305..a2fbd96 100644 --- a/libnfc/buses/uart.h +++ b/libnfc/buses/uart.h @@ -28,6 +28,8 @@ #ifndef __NFC_BUS_UART_H__ # define __NFC_BUS_UART_H__ +# include + # include # include # include @@ -47,8 +49,8 @@ void uart_flush_input (const serial_port sp); void uart_set_speed (serial_port sp, const uint32_t uiPortSpeed); uint32_t uart_get_speed (const serial_port sp); -int uart_receive (serial_port sp, byte_t * pbtRx, const size_t szRx, void * abort_p); -int uart_send (serial_port sp, const byte_t * pbtTx, const size_t szTx); +int uart_receive (serial_port sp, byte_t * pbtRx, const size_t szRx, void * abort_p, struct timeval *timeout); +int uart_send (serial_port sp, const byte_t * pbtTx, const size_t szTx, struct timeval *timeout); char **uart_list_ports (void); diff --git a/libnfc/buses/uart_posix.c b/libnfc/buses/uart_posix.c index a27a783..ddbd5dd 100644 --- a/libnfc/buses/uart_posix.c +++ b/libnfc/buses/uart_posix.c @@ -254,10 +254,14 @@ static const struct timeval tvTimeout = { * @return 0 on success, otherwise driver error code */ int -uart_receive (serial_port sp, byte_t * pbtRx, const size_t szRx, void * abort_p) +uart_receive (serial_port sp, byte_t * pbtRx, const size_t szRx, void * abort_p, struct timeval *timeout) { int iAbortFd = abort_p ? *((int*)abort_p) : 0; - struct timeval tv = tvTimeout; + struct timeval tv; + if (timeout) + tv = *timeout; + else + tv = tvTimeout; struct timeval *ptv = &tv; int received_bytes_count = 0; int available_bytes_count = 0; @@ -275,6 +279,7 @@ select: ptv = NULL; } + log_put (LOG_CATEGORY, NFC_PRIORITY_ERROR, "timeout = %p", ptv); res = select (MAX(((serial_port_unix *) sp)->fd, iAbortFd) + 1, &rfds, NULL, NULL, ptv); if ((res < 0) && (EINTR == errno)) { @@ -325,8 +330,9 @@ select: * @return 0 on success, otherwise a driver error is returned */ int -uart_send (serial_port sp, const byte_t * pbtTx, const size_t szTx) +uart_send (serial_port sp, const byte_t * pbtTx, const size_t szTx, struct timeval *timeout) { + (void) timeout; LOG_HEX ("TX", pbtTx, szTx); if ((int) szTx == write (((serial_port_unix *) sp)->fd, pbtTx, szTx)) return 0; diff --git a/libnfc/chips/pn53x.c b/libnfc/chips/pn53x.c index 58c7c62..c1797d0 100644 --- a/libnfc/chips/pn53x.c +++ b/libnfc/chips/pn53x.c @@ -102,7 +102,7 @@ pn53x_reset_settings(nfc_device_t * pnd) } bool -pn53x_transceive (nfc_device_t * pnd, const byte_t * pbtTx, const size_t szTx, byte_t * pbtRx, size_t *pszRx) +pn53x_transceive (nfc_device_t * pnd, const byte_t * pbtTx, const size_t szTx, byte_t * pbtRx, size_t *pszRx, struct timeval *timeout) { if (CHIP_DATA (pnd)->wb_trigged) { if (!pn53x_writeback_register (pnd)) { @@ -121,7 +121,7 @@ pn53x_transceive (nfc_device_t * pnd, const byte_t * pbtTx, const size_t szTx, b } // Call the send/receice callback functions of the current driver - if (!CHIP_DATA (pnd)->io->send (pnd, pbtTx, szTx)) + if (!CHIP_DATA (pnd)->io->send (pnd, pbtTx, szTx, timeout)) return false; // Command is sent, we store the command @@ -132,7 +132,7 @@ pn53x_transceive (nfc_device_t * pnd, const byte_t * pbtTx, const size_t szTx, b CHIP_DATA (pnd)->power_mode = POWERDOWN; } - int res = CHIP_DATA(pnd)->io->receive (pnd, pbtRx, *pszRx); + int res = CHIP_DATA(pnd)->io->receive (pnd, pbtRx, *pszRx, timeout); if (res < 0) { return false; } @@ -468,7 +468,7 @@ pn53x_ReadRegister (nfc_device_t * pnd, uint16_t ui16RegisterAddress, uint8_t * size_t szRegValue = sizeof (abtRegValue); PNREG_TRACE (ui16RegisterAddress); - if (!pn53x_transceive (pnd, abtCmd, sizeof (abtCmd), abtRegValue, &szRegValue)) { + if (!pn53x_transceive (pnd, abtCmd, sizeof (abtCmd), abtRegValue, &szRegValue, NULL)) { return false; } if (CHIP_DATA(pnd)->type == PN533) { @@ -490,7 +490,7 @@ pn53x_WriteRegister (nfc_device_t * pnd, const uint16_t ui16RegisterAddress, con { byte_t abtCmd[] = { WriteRegister, ui16RegisterAddress >> 8, ui16RegisterAddress & 0xff, ui8Value }; PNREG_TRACE (ui16RegisterAddress); - return pn53x_transceive (pnd, abtCmd, sizeof (abtCmd), NULL, NULL); + return pn53x_transceive (pnd, abtCmd, sizeof (abtCmd), NULL, NULL, NULL); } bool @@ -542,7 +542,7 @@ pn53x_writeback_register (nfc_device_t * pnd) uint8_t abtRes[PN53x_EXTENDED_FRAME__DATA_MAX_LEN]; size_t szRes = sizeof(abtRes); // It transceives the previously constructed ReadRegister command - if (!pn53x_transceive (pnd, abtReadRegisterCmd, BUFFER_SIZE (abtReadRegisterCmd), abtRes, &szRes)) { + if (!pn53x_transceive (pnd, abtReadRegisterCmd, BUFFER_SIZE (abtReadRegisterCmd), abtRes, &szRes, NULL)) { return false; } size_t i = 0; @@ -580,7 +580,7 @@ pn53x_writeback_register (nfc_device_t * pnd) if (BUFFER_SIZE (abtWriteRegisterCmd) > 1) { // We need to write some registers - if (!pn53x_transceive (pnd, abtWriteRegisterCmd, BUFFER_SIZE (abtWriteRegisterCmd), NULL, NULL)) { + if (!pn53x_transceive (pnd, abtWriteRegisterCmd, BUFFER_SIZE (abtWriteRegisterCmd), NULL, NULL, NULL)) { return false; } } @@ -593,7 +593,7 @@ pn53x_get_firmware_version (nfc_device_t * pnd, char abtFirmwareText[22]) const byte_t abtCmd[] = { GetFirmwareVersion }; byte_t abtFw[4]; size_t szFwLen = sizeof (abtFw); - if (!pn53x_transceive (pnd, abtCmd, sizeof (abtCmd), abtFw, &szFwLen)) { + if (!pn53x_transceive (pnd, abtCmd, sizeof (abtCmd), abtFw, &szFwLen, NULL)) { return false; } // Determine which version of chip it is: PN531 will return only 2 bytes, while others return 4 bytes and have the first to tell the version IC @@ -831,7 +831,7 @@ pn53x_check_communication (nfc_device_t *pnd) byte_t abtRx[sizeof(abtExpectedRx)]; size_t szRx = sizeof (abtRx); - if (!pn53x_transceive (pnd, abtCmd, sizeof (abtCmd), abtRx, &szRx)) + if (!pn53x_transceive (pnd, abtCmd, sizeof (abtCmd), abtRx, &szRx, NULL)) return false; return ((sizeof(abtExpectedRx) == szRx) && (0 == memcmp (abtRx, abtExpectedRx, sizeof(abtExpectedRx)))); @@ -885,11 +885,11 @@ pn53x_initiator_select_passive_target (nfc_device_t * pnd, byte_t abtRx[1]; size_t szRxLen = 1; // Getting random Chip_ID - if (!pn53x_initiator_transceive_bytes (pnd, abtInitiate, szInitiateLen, abtRx, &szRxLen)) { + if (!pn53x_initiator_transceive_bytes (pnd, abtInitiate, szInitiateLen, abtRx, &szRxLen, NULL)) { return false; } abtSelect[1] = abtRx[0]; - if (!pn53x_initiator_transceive_bytes (pnd, abtSelect, szSelectLen, abtRx, &szRxLen)) { + if (!pn53x_initiator_transceive_bytes (pnd, abtSelect, szSelectLen, abtRx, &szRxLen, NULL)) { return false; } } @@ -898,11 +898,11 @@ pn53x_initiator_select_passive_target (nfc_device_t * pnd, byte_t abtReqt[]="\x10"; size_t szReqtLen = 1; // Getting product code / fab code & store it in output buffer after the serial nr we'll obtain later - if (!pn53x_initiator_transceive_bytes (pnd, abtReqt, szReqtLen, abtTargetsData+2, &szTargetsData) || szTargetsData != 2) { + if (!pn53x_initiator_transceive_bytes (pnd, abtReqt, szReqtLen, abtTargetsData+2, &szTargetsData, NULL) || szTargetsData != 2) { return false; } } - if (!pn53x_initiator_transceive_bytes (pnd, pbtInitData, szInitData, abtTargetsData, &szTargetsData)) { + if (!pn53x_initiator_transceive_bytes (pnd, pbtInitData, szInitData, abtTargetsData, &szTargetsData, NULL)) { return false; } if (nm.nmt == NMT_ISO14443B2CT) { @@ -910,7 +910,7 @@ pn53x_initiator_select_passive_target (nfc_device_t * pnd, return false; byte_t abtRead[]="\xC4"; // Reading UID_MSB (Read address 4) size_t szReadLen = 1; - if (!pn53x_initiator_transceive_bytes (pnd, abtRead, szReadLen, abtTargetsData+4, &szTargetsData) || szTargetsData != 2) { + if (!pn53x_initiator_transceive_bytes (pnd, abtRead, szReadLen, abtTargetsData+4, &szTargetsData, NULL) || szTargetsData != 2) { return false; } szTargetsData = 6; // u16 UID_LSB, u8 prod code, u8 fab code, u16 UID_MSB @@ -928,7 +928,7 @@ pn53x_initiator_select_passive_target (nfc_device_t * pnd, size_t szAttribLen = sizeof(abtAttrib); memcpy(abtAttrib, abtTargetsData, szAttribLen); abtAttrib[1] = 0x0f; // ATTRIB - if (!pn53x_initiator_transceive_bytes (pnd, abtAttrib, szAttribLen, NULL, NULL)) { + if (!pn53x_initiator_transceive_bytes (pnd, abtAttrib, szAttribLen, NULL, NULL, NULL)) { return false; } } @@ -1049,7 +1049,7 @@ pn53x_initiator_transceive_bits (nfc_device_t * pnd, const byte_t * pbtTx, const // We have to give the amount of bytes + (the command byte 0x42) byte_t abtRx[PN53x_EXTENDED_FRAME__DATA_MAX_LEN]; size_t szRx = sizeof(abtRx); - if (!pn53x_transceive (pnd, abtCmd, szFrameBytes + 1, abtRx, &szRx)) + if (!pn53x_transceive (pnd, abtCmd, szFrameBytes + 1, abtRx, &szRx, NULL)) return false; // Get the last bit-count that is stored in the received byte @@ -1079,7 +1079,7 @@ pn53x_initiator_transceive_bits (nfc_device_t * pnd, const byte_t * pbtTx, const bool pn53x_initiator_transceive_bytes (nfc_device_t * pnd, const byte_t * pbtTx, const size_t szTx, byte_t * pbtRx, - size_t * pszRx) + size_t * pszRx, struct timeval *timeout) { size_t szExtraTxLen; byte_t abtCmd[PN53x_EXTENDED_FRAME__DATA_MAX_LEN]; @@ -1110,7 +1110,7 @@ pn53x_initiator_transceive_bytes (nfc_device_t * pnd, const byte_t * pbtTx, cons // We have to give the amount of bytes + (the two command bytes 0xD4, 0x42) byte_t abtRx[PN53x_EXTENDED_FRAME__DATA_MAX_LEN]; size_t szRx = sizeof(abtRx); - if (!pn53x_transceive (pnd, abtCmd, szTx + szExtraTxLen, abtRx, &szRx)) + if (!pn53x_transceive (pnd, abtCmd, szTx + szExtraTxLen, abtRx, &szRx, timeout)) return false; if (pbtRx != NULL) { @@ -1166,7 +1166,7 @@ uint32_t __pn53x_get_timer(nfc_device_t * pnd, const uint8_t last_cmd_byte) uint8_t abtRes[PN53x_EXTENDED_FRAME__DATA_MAX_LEN]; size_t szRes = sizeof(abtRes); // Let's send the previously constructed ReadRegister command - if (!pn53x_transceive (pnd, abtReadRegisterCmd, BUFFER_SIZE (abtReadRegisterCmd), abtRes, &szRes)) { + if (!pn53x_transceive (pnd, abtReadRegisterCmd, BUFFER_SIZE (abtReadRegisterCmd), abtRes, &szRes, NULL)) { return false; } counter_hi = abtRes[off]; @@ -1256,7 +1256,7 @@ pn53x_initiator_transceive_bits_timed (nfc_device_t * pnd, const byte_t * pbtTx, BUFFER_APPEND (abtWriteRegisterCmd, PN53X_REG_CIU_BitFraming & 0xff); BUFFER_APPEND (abtWriteRegisterCmd, SYMBOL_START_SEND | ((szTxBits % 8) & SYMBOL_TX_LAST_BITS)); // Let's send the previously constructed WriteRegister command - if (!pn53x_transceive (pnd, abtWriteRegisterCmd, BUFFER_SIZE (abtWriteRegisterCmd), NULL, NULL)) { + if (!pn53x_transceive (pnd, abtWriteRegisterCmd, BUFFER_SIZE (abtWriteRegisterCmd), NULL, NULL, NULL)) { return false; } @@ -1288,7 +1288,7 @@ pn53x_initiator_transceive_bits_timed (nfc_device_t * pnd, const byte_t * pbtTx, uint8_t abtRes[PN53x_EXTENDED_FRAME__DATA_MAX_LEN]; size_t szRes = sizeof(abtRes); // Let's send the previously constructed ReadRegister command - if (!pn53x_transceive (pnd, abtReadRegisterCmd, BUFFER_SIZE (abtReadRegisterCmd), abtRes, &szRes)) { + if (!pn53x_transceive (pnd, abtReadRegisterCmd, BUFFER_SIZE (abtReadRegisterCmd), abtRes, &szRes, NULL)) { return false; } for (i = 0; i < sz; i++) { @@ -1351,7 +1351,7 @@ pn53x_initiator_transceive_bytes_timed (nfc_device_t * pnd, const byte_t * pbtTx BUFFER_APPEND (abtWriteRegisterCmd, PN53X_REG_CIU_BitFraming & 0xff); BUFFER_APPEND (abtWriteRegisterCmd, SYMBOL_START_SEND); // Let's send the previously constructed WriteRegister command - if (!pn53x_transceive (pnd, abtWriteRegisterCmd, BUFFER_SIZE (abtWriteRegisterCmd), NULL, NULL)) { + if (!pn53x_transceive (pnd, abtWriteRegisterCmd, BUFFER_SIZE (abtWriteRegisterCmd), NULL, NULL, NULL)) { return false; } @@ -1383,7 +1383,7 @@ pn53x_initiator_transceive_bytes_timed (nfc_device_t * pnd, const byte_t * pbtTx uint8_t abtRes[PN53x_EXTENDED_FRAME__DATA_MAX_LEN]; size_t szRes = sizeof(abtRes); // Let's send the previously constructed ReadRegister command - if (!pn53x_transceive (pnd, abtReadRegisterCmd, BUFFER_SIZE (abtReadRegisterCmd), abtRes, &szRes)) { + if (!pn53x_transceive (pnd, abtReadRegisterCmd, BUFFER_SIZE (abtReadRegisterCmd), abtRes, &szRes, NULL)) { return false; } for (i = 0; i < sz; i++) { @@ -1647,7 +1647,7 @@ pn53x_target_receive_bits (nfc_device_t * pnd, byte_t * pbtRx, size_t * pszRxBit byte_t abtRx[PN53x_EXTENDED_FRAME__DATA_MAX_LEN]; size_t szRx = sizeof (abtRx); // Try to gather a received frame from the reader - if (!pn53x_transceive (pnd, abtCmd, sizeof (abtCmd), abtRx, &szRx)) + if (!pn53x_transceive (pnd, abtCmd, sizeof (abtCmd), abtRx, &szRx, NULL)) return false; // Get the last bit-count that is stored in the received byte @@ -1675,7 +1675,7 @@ pn53x_target_receive_bits (nfc_device_t * pnd, byte_t * pbtRx, size_t * pszRxBit } bool -pn53x_target_receive_bytes (nfc_device_t * pnd, byte_t * pbtRx, size_t * pszRx) +pn53x_target_receive_bytes (nfc_device_t * pnd, byte_t * pbtRx, size_t * pszRx, struct timeval *timeout) { byte_t abtCmd[1]; @@ -1710,7 +1710,7 @@ pn53x_target_receive_bytes (nfc_device_t * pnd, byte_t * pbtRx, size_t * pszRx) // Try to gather a received frame from the reader byte_t abtRx[PN53x_EXTENDED_FRAME__DATA_MAX_LEN]; size_t szRx = sizeof (abtRx); - if (!pn53x_transceive (pnd, abtCmd, sizeof (abtCmd), abtRx, &szRx)) + if (!pn53x_transceive (pnd, abtCmd, sizeof (abtCmd), abtRx, &szRx, timeout)) return false; // Save the received byte count @@ -1754,7 +1754,7 @@ pn53x_target_send_bits (nfc_device_t * pnd, const byte_t * pbtTx, const size_t s return false; // Try to send the bits to the reader - if (!pn53x_transceive (pnd, abtCmd, szFrameBytes + 1, NULL, NULL)) + if (!pn53x_transceive (pnd, abtCmd, szFrameBytes + 1, NULL, NULL, NULL)) return false; // Everyting seems ok, return true @@ -1762,7 +1762,7 @@ pn53x_target_send_bits (nfc_device_t * pnd, const byte_t * pbtTx, const size_t s } bool -pn53x_target_send_bytes (nfc_device_t * pnd, const byte_t * pbtTx, const size_t szTx) +pn53x_target_send_bytes (nfc_device_t * pnd, const byte_t * pbtTx, const size_t szTx, struct timeval *timeout) { byte_t abtCmd[PN53x_EXTENDED_FRAME__DATA_MAX_LEN]; @@ -1802,7 +1802,7 @@ pn53x_target_send_bytes (nfc_device_t * pnd, const byte_t * pbtTx, const size_t memcpy (abtCmd + 1, pbtTx, szTx); // Try to send the bits to the reader - if (!pn53x_transceive (pnd, abtCmd, szTx + 1, NULL, NULL)) + if (!pn53x_transceive (pnd, abtCmd, szTx + 1, NULL, NULL, timeout)) return false; // Everyting seems ok, return true @@ -1880,7 +1880,7 @@ bool pn53x_RFConfiguration__RF_field (nfc_device_t * pnd, bool bEnable) { byte_t abtCmd[] = { RFConfiguration, RFCI_FIELD, (bEnable) ? 0x01 : 0x00 }; - return pn53x_transceive (pnd, abtCmd, sizeof (abtCmd), NULL, NULL); + return pn53x_transceive (pnd, abtCmd, sizeof (abtCmd), NULL, NULL, NULL); } bool @@ -1893,7 +1893,7 @@ pn53x_RFConfiguration__Various_timings (nfc_device_t * pnd, const uint8_t fATR_R fATR_RES_Timeout, // ATR_RES timeout (default: 0x0B 102.4 ms) fRetryTimeout // TimeOut during non-DEP communications (default: 0x0A 51.2 ms) }; - return pn53x_transceive (pnd, abtCmd, sizeof (abtCmd), NULL, NULL); + return pn53x_transceive (pnd, abtCmd, sizeof (abtCmd), NULL, NULL, NULL); } bool @@ -1904,7 +1904,7 @@ pn53x_RFConfiguration__MaxRtyCOM (nfc_device_t * pnd, const uint8_t MaxRtyCOM) RFCI_RETRY_DATA, MaxRtyCOM // MaxRtyCOM, default: 0x00 (no retry, only one try), inifite: 0xff }; - return pn53x_transceive (pnd, abtCmd, sizeof (abtCmd), NULL, NULL); + return pn53x_transceive (pnd, abtCmd, sizeof (abtCmd), NULL, NULL, NULL); } bool @@ -1918,7 +1918,7 @@ pn53x_RFConfiguration__MaxRetries (nfc_device_t * pnd, const uint8_t MxRtyATR, c MxRtyPSL, // MxRtyPSL, default: 0x01 MxRtyPassiveActivation // MxRtyPassiveActivation, default: 0xff (0x00 leads to problems with PN531) }; - return pn53x_transceive (pnd, abtCmd, sizeof (abtCmd), NULL, NULL); + return pn53x_transceive (pnd, abtCmd, sizeof (abtCmd), NULL, NULL, NULL); } bool @@ -1926,7 +1926,7 @@ pn53x_SetParameters (nfc_device_t * pnd, const uint8_t ui8Value) { byte_t abtCmd[] = { SetParameters, ui8Value }; - if(!pn53x_transceive (pnd, abtCmd, sizeof (abtCmd), NULL, NULL)) { + if(!pn53x_transceive (pnd, abtCmd, sizeof (abtCmd), NULL, NULL, NULL)) { return false; } // We save last parameters in register cache @@ -1960,14 +1960,14 @@ pn53x_SAMConfiguration (nfc_device_t * pnd, const pn532_sam_mode ui8Mode) pnd->iLastError = EINVALARG; return false; } - return (pn53x_transceive (pnd, abtCmd, szCmd, NULL, NULL)); + return (pn53x_transceive (pnd, abtCmd, szCmd, NULL, NULL, NULL)); } bool pn53x_PowerDown (nfc_device_t * pnd) { byte_t abtCmd[] = { PowerDown, 0xf0 }; - return (pn53x_transceive (pnd, abtCmd, sizeof (abtCmd), NULL, NULL)); + return (pn53x_transceive (pnd, abtCmd, sizeof (abtCmd), NULL, NULL, NULL)); } /** @@ -2033,7 +2033,7 @@ pn53x_InListPassiveTarget (nfc_device_t * pnd, if (pbtInitiatorData) memcpy (abtCmd + 3, pbtInitiatorData, szInitiatorData); - return pn53x_transceive (pnd, abtCmd, 3 + szInitiatorData, pbtTargetsData, pszTargetsData); + return pn53x_transceive (pnd, abtCmd, 3 + szInitiatorData, pbtTargetsData, pszTargetsData, NULL); } bool @@ -2044,7 +2044,7 @@ pn53x_InDeselect (nfc_device_t * pnd, const uint8_t ui8Target) byte_t abtStatus[PN53x_EXTENDED_FRAME__DATA_MAX_LEN]; size_t szStatus = sizeof(abtStatus); byte_t abtCmdGetStatus[] = { GetGeneralStatus }; - if (!pn53x_transceive (pnd, abtCmdGetStatus, sizeof (abtCmdGetStatus), abtStatus, &szStatus)) { + if (!pn53x_transceive (pnd, abtCmdGetStatus, sizeof (abtCmdGetStatus), abtStatus, &szStatus, NULL)) { return false; } if ((szStatus < 3) || (abtStatus[2] == 0)) { @@ -2052,10 +2052,10 @@ pn53x_InDeselect (nfc_device_t * pnd, const uint8_t ui8Target) } // No much choice what to deselect actually... byte_t abtCmdRcs360[] = { InDeselect, 0x01, 0x01 }; - return (pn53x_transceive (pnd, abtCmdRcs360, sizeof (abtCmdRcs360), NULL, NULL)); + return (pn53x_transceive (pnd, abtCmdRcs360, sizeof (abtCmdRcs360), NULL, NULL, NULL)); } byte_t abtCmd[] = { InDeselect, ui8Target }; - return (pn53x_transceive (pnd, abtCmd, sizeof (abtCmd), NULL, NULL)); + return (pn53x_transceive (pnd, abtCmd, sizeof (abtCmd), NULL, NULL, NULL)); } bool @@ -2066,7 +2066,7 @@ pn53x_InRelease (nfc_device_t * pnd, const uint8_t ui8Target) byte_t abtStatus[PN53x_EXTENDED_FRAME__DATA_MAX_LEN]; size_t szStatus = sizeof(abtStatus); byte_t abtCmdGetStatus[] = { GetGeneralStatus }; - if (!pn53x_transceive (pnd, abtCmdGetStatus, sizeof (abtCmdGetStatus), abtStatus, &szStatus)) { + if (!pn53x_transceive (pnd, abtCmdGetStatus, sizeof (abtCmdGetStatus), abtStatus, &szStatus, NULL)) { return false; } if ((szStatus < 3) || (abtStatus[2] == 0)) { @@ -2074,10 +2074,10 @@ pn53x_InRelease (nfc_device_t * pnd, const uint8_t ui8Target) } // No much choice what to release actually... byte_t abtCmdRcs360[] = { InRelease, 0x01, 0x01 }; - return (pn53x_transceive (pnd, abtCmdRcs360, sizeof (abtCmdRcs360), NULL, NULL)); + return (pn53x_transceive (pnd, abtCmdRcs360, sizeof (abtCmdRcs360), NULL, NULL, NULL)); } byte_t abtCmd[] = { InRelease, ui8Target }; - return (pn53x_transceive (pnd, abtCmd, sizeof (abtCmd), NULL, NULL)); + return (pn53x_transceive (pnd, abtCmd, sizeof (abtCmd), NULL, NULL, NULL)); } bool @@ -2100,7 +2100,7 @@ pn53x_InAutoPoll (nfc_device_t * pnd, byte_t abtRx[PN53x_EXTENDED_FRAME__DATA_MAX_LEN]; size_t szRx = sizeof(abtRx); - bool res = pn53x_transceive (pnd, abtCmd, szTxInAutoPoll, abtRx, &szRx); + bool res = pn53x_transceive (pnd, abtCmd, szTxInAutoPoll, abtRx, &szRx, NULL); if (res == false) { return false; @@ -2209,7 +2209,7 @@ pn53x_InJumpForDEP (nfc_device_t * pnd, byte_t abtRx[PN53x_EXTENDED_FRAME__DATA_MAX_LEN]; size_t szRx = sizeof (abtRx); // Try to find a target, call the transceive callback function of the current device - if (!pn53x_transceive (pnd, abtCmd, offset, abtRx, &szRx)) + if (!pn53x_transceive (pnd, abtCmd, offset, abtRx, &szRx, NULL)) return false; // Make sure one target has been found, the PN53X returns 0x00 if none was available @@ -2290,7 +2290,7 @@ pn53x_TgInitAsTarget (nfc_device_t * pnd, pn53x_target_mode_t ptm, // Request the initialization as a target byte_t abtRx[PN53x_EXTENDED_FRAME__DATA_MAX_LEN]; size_t szRx = sizeof (abtRx); - if (!pn53x_transceive (pnd, abtCmd, 36 + szOptionalBytes, abtRx, &szRx)) + if (!pn53x_transceive (pnd, abtCmd, 36 + szOptionalBytes, abtRx, &szRx, NULL)) return false; // Note: the first byte is skip: diff --git a/libnfc/chips/pn53x.h b/libnfc/chips/pn53x.h index 1a21a0e..1beae4e 100644 --- a/libnfc/chips/pn53x.h +++ b/libnfc/chips/pn53x.h @@ -127,8 +127,8 @@ typedef enum { } pn53x_operating_mode; struct pn53x_io { - bool (*send)(nfc_device_t * pnd, const byte_t * pbtData, const size_t szData); - int (*receive)(nfc_device_t * pnd, byte_t * pbtData, const size_t szDataLen); + bool (*send)(nfc_device_t * pnd, const byte_t * pbtData, const size_t szData, struct timeval *timeout); + int (*receive)(nfc_device_t * pnd, byte_t * pbtData, const size_t szDataLen, struct timeval *timeout); }; /* defines */ @@ -256,7 +256,7 @@ extern const byte_t pn53x_ack_frame[6]; extern const byte_t pn53x_nack_frame[6]; bool pn53x_init(nfc_device_t * pnd); -bool pn53x_transceive (nfc_device_t * pnd, const byte_t * pbtTx, const size_t szTx, byte_t * pbtRx, size_t *pszRx); +bool pn53x_transceive (nfc_device_t * pnd, const byte_t * pbtTx, const size_t szTx, byte_t * pbtRx, size_t *pszRx, struct timeval *timeout); bool pn53x_set_parameters (nfc_device_t * pnd, const uint8_t ui8Value, const bool bEnable); bool pn53x_set_tx_bits (nfc_device_t * pnd, const uint8_t ui8Bits); @@ -292,7 +292,7 @@ bool pn53x_initiator_transceive_bits (nfc_device_t * pnd, const byte_t * pbtT const byte_t * pbtTxPar, byte_t * pbtRx, size_t * pszRxBits, byte_t * pbtRxPar); bool pn53x_initiator_transceive_bytes (nfc_device_t * pnd, const byte_t * pbtTx, const size_t szTx, - byte_t * pbtRx, size_t * pszRx); + byte_t * pbtRx, size_t * pszRx, struct timeval *timeout); bool pn53x_initiator_transceive_bits_timed (nfc_device_t * pnd, const byte_t * pbtTx, const size_t szTxBits, const byte_t * pbtTxPar, byte_t * pbtRx, size_t * pszRxBits, byte_t * pbtRxPar, uint32_t * cycles); @@ -303,10 +303,10 @@ bool pn53x_initiator_deselect_target (nfc_device_t * pnd); // NFC device as Target functions bool pn53x_target_init (nfc_device_t * pnd, nfc_target_t * pnt, byte_t * pbtRx, size_t * pszRx); bool pn53x_target_receive_bits (nfc_device_t * pnd, byte_t * pbtRx, size_t * pszRxBits, byte_t * pbtRxPar); -bool pn53x_target_receive_bytes (nfc_device_t * pnd, byte_t * pbtRx, size_t * pszRx); +bool pn53x_target_receive_bytes (nfc_device_t * pnd, byte_t * pbtRx, size_t * pszRx, struct timeval *timeout); bool pn53x_target_send_bits (nfc_device_t * pnd, const byte_t * pbtTx, const size_t szTxBits, const byte_t * pbtTxPar); -bool pn53x_target_send_bytes (nfc_device_t * pnd, const byte_t * pbtTx, const size_t szTx); +bool pn53x_target_send_bytes (nfc_device_t * pnd, const byte_t * pbtTx, const size_t szTx, struct timeval *timeout); // Error handling functions const char *pn53x_strerror (const nfc_device_t * pnd); diff --git a/libnfc/drivers/acr122.c b/libnfc/drivers/acr122.c index b32ce9d..ba36e07 100644 --- a/libnfc/drivers/acr122.c +++ b/libnfc/drivers/acr122.c @@ -252,8 +252,11 @@ acr122_disconnect (nfc_device_t * pnd) } bool -acr122_send (nfc_device_t * pnd, const byte_t * pbtData, const size_t szData) +acr122_send (nfc_device_t * pnd, const byte_t * pbtData, const size_t szData, struct timeval *timeout) { + // FIXME: timeout is not handled + (void) timeout; + // Make sure the command does not overflow the send buffer if (szData > ACR122_COMMAND_LEN) { pnd->iLastError = EINVALARG; @@ -320,8 +323,11 @@ acr122_send (nfc_device_t * pnd, const byte_t * pbtData, const size_t szData) } int -acr122_receive (nfc_device_t * pnd, byte_t * pbtData, const size_t szData) +acr122_receive (nfc_device_t * pnd, byte_t * pbtData, const size_t szData, struct timeval *timeout) { + // FIXME: timeout is not handled + (void) timeout; + int len; byte_t abtRxCmd[5] = { 0xFF, 0xC0, 0x00, 0x00 }; diff --git a/libnfc/drivers/acr122.h b/libnfc/drivers/acr122.h index 610370a..db05e2f 100644 --- a/libnfc/drivers/acr122.h +++ b/libnfc/drivers/acr122.h @@ -30,8 +30,8 @@ bool acr122_probe (nfc_device_desc_t pnddDevices[], size_t szDevices, size_t // Functions used by developer to handle connection to this device nfc_device_t *acr122_connect (const nfc_device_desc_t * pndd); -bool acr122_send (nfc_device_t * pnd, const byte_t * pbtData, const size_t szData); -int acr122_receive (nfc_device_t * pnd, byte_t * pbtData, const size_t szData); +bool acr122_send (nfc_device_t * pnd, const byte_t * pbtData, const size_t szData, struct timeval *timeout); +int acr122_receive (nfc_device_t * pnd, byte_t * pbtData, const size_t szData, struct timeval *timeout); void acr122_disconnect (nfc_device_t * pnd); extern const struct nfc_driver_t acr122_driver; diff --git a/libnfc/drivers/arygon.c b/libnfc/drivers/arygon.c index dcfa574..b04d021 100644 --- a/libnfc/drivers/arygon.c +++ b/libnfc/drivers/arygon.c @@ -236,7 +236,7 @@ arygon_disconnect (nfc_device_t * pnd) #define ARYGON_TX_BUFFER_LEN (PN53x_NORMAL_FRAME__DATA_MAX_LEN + PN53x_NORMAL_FRAME__OVERHEAD + 1) #define ARYGON_RX_BUFFER_LEN (PN53x_EXTENDED_FRAME__DATA_MAX_LEN + PN53x_EXTENDED_FRAME__OVERHEAD) bool -arygon_tama_send (nfc_device_t * pnd, const byte_t * pbtData, const size_t szData) +arygon_tama_send (nfc_device_t * pnd, const byte_t * pbtData, const size_t szData, struct timeval *timeout) { // Before sending anything, we need to discard from any junk bytes uart_flush_input (DRIVER_DATA(pnd)->port); @@ -256,7 +256,7 @@ arygon_tama_send (nfc_device_t * pnd, const byte_t * pbtData, const size_t szDat return false; } - int res = uart_send (DRIVER_DATA (pnd)->port, abtFrame, szFrame + 1); + int res = uart_send (DRIVER_DATA (pnd)->port, abtFrame, szFrame + 1, timeout); if (res != 0) { log_put (LOG_CATEGORY, NFC_PRIORITY_ERROR, "%s", "Unable to transmit data. (TX)"); pnd->iLastError = res; @@ -264,7 +264,7 @@ arygon_tama_send (nfc_device_t * pnd, const byte_t * pbtData, const size_t szDat } byte_t abtRxBuf[6]; - res = uart_receive (DRIVER_DATA (pnd)->port, abtRxBuf, sizeof (abtRxBuf), 0); + res = uart_receive (DRIVER_DATA (pnd)->port, abtRxBuf, sizeof (abtRxBuf), 0, timeout); if (res != 0) { log_put (LOG_CATEGORY, NFC_PRIORITY_ERROR, "%s", "Unable to read ACK"); pnd->iLastError = res; @@ -277,7 +277,7 @@ arygon_tama_send (nfc_device_t * pnd, const byte_t * pbtData, const size_t szDat log_put (LOG_CATEGORY, NFC_PRIORITY_ERROR, "Bad frame format." ); // We have already read 6 bytes and arygon_error_unknown_mode is 10 bytes long // so we have to read 4 remaining bytes to be synchronized at the next receiving pass. - uart_receive (DRIVER_DATA (pnd)->port, abtRxBuf, 4, 0); + uart_receive (DRIVER_DATA (pnd)->port, abtRxBuf, 4, 0, timeout); return false; } else { return false; @@ -291,14 +291,14 @@ arygon_abort (nfc_device_t *pnd) // Send a valid TAMA packet to wakup the PN53x (we will not have an answer, according to Arygon manual) byte_t dummy[] = { 0x32, 0x00, 0x00, 0xff, 0x09, 0xf7, 0xd4, 0x00, 0x00, 0x6c, 0x69, 0x62, 0x6e, 0x66, 0x63, 0xbe, 0x00 }; - uart_send (DRIVER_DATA (pnd)->port, dummy, sizeof (dummy)); + uart_send (DRIVER_DATA (pnd)->port, dummy, sizeof (dummy), NULL); // Using Arygon device we can't send ACK frame to abort the running command return (pn53x_check_communication (pnd)) ? 0 : -1; } int -arygon_tama_receive (nfc_device_t * pnd, byte_t * pbtData, const size_t szDataLen) +arygon_tama_receive (nfc_device_t * pnd, byte_t * pbtData, const size_t szDataLen, struct timeval *timeout) { byte_t abtRxBuf[5]; size_t len; @@ -320,7 +320,7 @@ arygon_tama_receive (nfc_device_t * pnd, byte_t * pbtData, const size_t szDataLe break; } - pnd->iLastError = uart_receive (DRIVER_DATA (pnd)->port, abtRxBuf, 5, abort_p); + pnd->iLastError = uart_receive (DRIVER_DATA (pnd)->port, abtRxBuf, 5, abort_p, timeout); if (abort_p && (EOPABORT == pnd->iLastError)) { arygon_abort (pnd); @@ -344,7 +344,7 @@ arygon_tama_receive (nfc_device_t * pnd, byte_t * pbtData, const size_t szDataLe if ((0x01 == abtRxBuf[3]) && (0xff == abtRxBuf[4])) { // Error frame - uart_receive (DRIVER_DATA (pnd)->port, abtRxBuf, 3, 0); + uart_receive (DRIVER_DATA (pnd)->port, abtRxBuf, 3, 0, timeout); log_put (LOG_CATEGORY, NFC_PRIORITY_ERROR, "%s", "Application level error detected"); pnd->iLastError = EFRAISERRFRAME; return -1; @@ -372,7 +372,7 @@ arygon_tama_receive (nfc_device_t * pnd, byte_t * pbtData, const size_t szDataLe } // TFI + PD0 (CC+1) - pnd->iLastError = uart_receive (DRIVER_DATA (pnd)->port, abtRxBuf, 2, 0); + pnd->iLastError = uart_receive (DRIVER_DATA (pnd)->port, abtRxBuf, 2, 0, timeout); if (pnd->iLastError != 0) { log_put (LOG_CATEGORY, NFC_PRIORITY_ERROR, "%s", "Unable to receive data. (RX)"); return -1; @@ -391,14 +391,14 @@ arygon_tama_receive (nfc_device_t * pnd, byte_t * pbtData, const size_t szDataLe } if (len) { - pnd->iLastError = uart_receive (DRIVER_DATA (pnd)->port, pbtData, len, 0); + pnd->iLastError = uart_receive (DRIVER_DATA (pnd)->port, pbtData, len, 0, timeout); if (pnd->iLastError != 0) { log_put (LOG_CATEGORY, NFC_PRIORITY_ERROR, "%s", "Unable to receive data. (RX)"); return -1; } } - pnd->iLastError = uart_receive (DRIVER_DATA (pnd)->port, abtRxBuf, 2, 0); + pnd->iLastError = uart_receive (DRIVER_DATA (pnd)->port, abtRxBuf, 2, 0, timeout); if (pnd->iLastError != 0) { log_put (LOG_CATEGORY, NFC_PRIORITY_ERROR, "%s", "Unable to receive data. (RX)"); return -1; @@ -428,17 +428,17 @@ arygon_tama_receive (nfc_device_t * pnd, byte_t * pbtData, const size_t szDataLe void arygon_firmware (nfc_device_t * pnd, char * str) { - const byte_t arygon_firmware_version_cmd[] = { DEV_ARYGON_PROTOCOL_ARYGON_ASCII, 'a', 'v' }; + const byte_t arygon_firmware_version_cmd[] = { DEV_ARYGON_PROTOCOL_ARYGON_ASCII, 'a', 'v' }; byte_t abtRx[16]; size_t szRx = sizeof(abtRx); - int res = uart_send (DRIVER_DATA (pnd)->port, arygon_firmware_version_cmd, sizeof (arygon_firmware_version_cmd)); + int res = uart_send (DRIVER_DATA (pnd)->port, arygon_firmware_version_cmd, sizeof (arygon_firmware_version_cmd), NULL); if (res != 0) { log_put (LOG_CATEGORY, NFC_PRIORITY_TRACE, "Unable to send ARYGON firmware command."); return; } - res = uart_receive (DRIVER_DATA (pnd)->port, abtRx, szRx, 0); + res = uart_receive (DRIVER_DATA (pnd)->port, abtRx, szRx, 0, NULL); if (res != 0) { log_put (LOG_CATEGORY, NFC_PRIORITY_TRACE, "Unable to retrieve ARYGON firmware version."); return; @@ -461,11 +461,11 @@ arygon_reset_tama (nfc_device_t * pnd) size_t szRx = sizeof(abtRx); int res; - uart_send (DRIVER_DATA (pnd)->port, arygon_reset_tama_cmd, sizeof (arygon_reset_tama_cmd)); + uart_send (DRIVER_DATA (pnd)->port, arygon_reset_tama_cmd, sizeof (arygon_reset_tama_cmd), NULL); // Two reply are possible from ARYGON device: arygon_error_none (ie. in case the byte is well-sent) // or arygon_error_unknown_mode (ie. in case of the first byte was bad-transmitted) - res = uart_receive (DRIVER_DATA (pnd)->port, abtRx, szRx, 0); + res = uart_receive (DRIVER_DATA (pnd)->port, abtRx, szRx, 0, NULL); if (res != 0) { log_put (LOG_CATEGORY, NFC_PRIORITY_TRACE, "No reply to 'reset TAMA' command."); return false; diff --git a/libnfc/drivers/arygon.h b/libnfc/drivers/arygon.h index c314788..11318b8 100644 --- a/libnfc/drivers/arygon.h +++ b/libnfc/drivers/arygon.h @@ -26,6 +26,8 @@ #ifndef __NFC_DRIVER_ARYGON_H__ # define __NFC_DRIVER_ARYGON_H__ +# include + # include bool arygon_probe (nfc_device_desc_t pnddDevices[], size_t szDevices, size_t * pszDeviceFound); @@ -33,8 +35,8 @@ bool arygon_probe (nfc_device_desc_t pnddDevices[], size_t szDevices, size_t nfc_device_t *arygon_connect (const nfc_device_desc_t * pndd); void arygon_disconnect (nfc_device_t * pnd); -bool arygon_tama_send (nfc_device_t * pnd, const byte_t * pbtData, const size_t szData); -int arygon_tama_receive (nfc_device_t * pnd, byte_t * pbtData, const size_t szData); +bool arygon_tama_send (nfc_device_t * pnd, const byte_t * pbtData, const size_t szData, struct timeval *timeout); +int arygon_tama_receive (nfc_device_t * pnd, byte_t * pbtData, const size_t szDat, struct timeval *timeouta); extern const struct nfc_driver_t arygon_driver; diff --git a/libnfc/drivers/pn532_uart.c b/libnfc/drivers/pn532_uart.c index e8f14af..76ffae3 100644 --- a/libnfc/drivers/pn532_uart.c +++ b/libnfc/drivers/pn532_uart.c @@ -215,14 +215,14 @@ pn532_uart_wakeup (nfc_device_t * pnd) { /* High Speed Unit (HSU) wake up consist to send 0x55 and wait a "long" delay for PN532 being wakeup. */ const byte_t pn532_wakeup_preamble[] = { 0x55, 0x55, 0x00, 0x00, 0x00 }; - int res = uart_send (DRIVER_DATA(pnd)->port, pn532_wakeup_preamble, sizeof (pn532_wakeup_preamble)); + int res = uart_send (DRIVER_DATA(pnd)->port, pn532_wakeup_preamble, sizeof (pn532_wakeup_preamble), NULL); CHIP_DATA(pnd)->power_mode = NORMAL; // PN532 should now be awake return res; } #define PN532_BUFFER_LEN (PN53x_EXTENDED_FRAME__DATA_MAX_LEN + PN53x_EXTENDED_FRAME__OVERHEAD) bool -pn532_uart_send (nfc_device_t * pnd, const byte_t * pbtData, const size_t szData) +pn532_uart_send (nfc_device_t * pnd, const byte_t * pbtData, const size_t szData, struct timeval *timeout) { // Before sending anything, we need to discard from any junk bytes uart_flush_input (DRIVER_DATA(pnd)->port); @@ -258,7 +258,7 @@ pn532_uart_send (nfc_device_t * pnd, const byte_t * pbtData, const size_t szData return false; } - int res = uart_send (DRIVER_DATA(pnd)->port, abtFrame, szFrame); + int res = uart_send (DRIVER_DATA(pnd)->port, abtFrame, szFrame, timeout); if (res != 0) { log_put (LOG_CATEGORY, NFC_PRIORITY_ERROR, "%s", "Unable to transmit data. (TX)"); pnd->iLastError = res; @@ -266,7 +266,7 @@ pn532_uart_send (nfc_device_t * pnd, const byte_t * pbtData, const size_t szData } byte_t abtRxBuf[6]; - res = uart_receive (DRIVER_DATA(pnd)->port, abtRxBuf, 6, 0); + res = uart_receive (DRIVER_DATA(pnd)->port, abtRxBuf, 6, 0, timeout); if (res != 0) { log_put (LOG_CATEGORY, NFC_PRIORITY_ERROR, "%s", "Unable to read ACK"); pnd->iLastError = res; @@ -282,7 +282,7 @@ pn532_uart_send (nfc_device_t * pnd, const byte_t * pbtData, const size_t szData } int -pn532_uart_receive (nfc_device_t * pnd, byte_t * pbtData, const size_t szDataLen) +pn532_uart_receive (nfc_device_t * pnd, byte_t * pbtData, const size_t szDataLen, struct timeval *timeout) { byte_t abtRxBuf[5]; size_t len; @@ -304,7 +304,7 @@ pn532_uart_receive (nfc_device_t * pnd, byte_t * pbtData, const size_t szDataLen break; } - pnd->iLastError = uart_receive (DRIVER_DATA(pnd)->port, abtRxBuf, 5, abort_p); + pnd->iLastError = uart_receive (DRIVER_DATA(pnd)->port, abtRxBuf, 5, abort_p, timeout); if (abort_p && (EOPABORT == pnd->iLastError)) { pn532_uart_ack (pnd); @@ -325,13 +325,13 @@ pn532_uart_receive (nfc_device_t * pnd, byte_t * pbtData, const size_t szDataLen if ((0x01 == abtRxBuf[3]) && (0xff == abtRxBuf[4])) { // Error frame - uart_receive (DRIVER_DATA(pnd)->port, abtRxBuf, 3, 0); + uart_receive (DRIVER_DATA(pnd)->port, abtRxBuf, 3, 0, timeout); log_put (LOG_CATEGORY, NFC_PRIORITY_ERROR, "%s", "Application level error detected"); pnd->iLastError = EFRAISERRFRAME; return -1; } else if ((0xff == abtRxBuf[3]) && (0xff == abtRxBuf[4])) { // Extended frame - pnd->iLastError = uart_receive (DRIVER_DATA(pnd)->port, abtRxBuf, 3, 0); + pnd->iLastError = uart_receive (DRIVER_DATA(pnd)->port, abtRxBuf, 3, 0, timeout); if (pnd->iLastError) return -1; // (abtRxBuf[0] << 8) + abtRxBuf[1] (LEN) include TFI + (CC+1) @@ -362,7 +362,7 @@ pn532_uart_receive (nfc_device_t * pnd, byte_t * pbtData, const size_t szDataLen } // TFI + PD0 (CC+1) - pnd->iLastError = uart_receive (DRIVER_DATA(pnd)->port, abtRxBuf, 2, 0); + pnd->iLastError = uart_receive (DRIVER_DATA(pnd)->port, abtRxBuf, 2, 0, timeout); if (pnd->iLastError != 0) { log_put (LOG_CATEGORY, NFC_PRIORITY_ERROR, "%s", "Unable to receive data. (RX)"); return -1; @@ -381,14 +381,14 @@ pn532_uart_receive (nfc_device_t * pnd, byte_t * pbtData, const size_t szDataLen } if (len) { - pnd->iLastError = uart_receive (DRIVER_DATA(pnd)->port, pbtData, len, 0); + pnd->iLastError = uart_receive (DRIVER_DATA(pnd)->port, pbtData, len, 0, timeout); if (pnd->iLastError != 0) { log_put (LOG_CATEGORY, NFC_PRIORITY_ERROR, "%s", "Unable to receive data. (RX)"); return -1; } } - pnd->iLastError = uart_receive (DRIVER_DATA(pnd)->port, abtRxBuf, 2, 0); + pnd->iLastError = uart_receive (DRIVER_DATA(pnd)->port, abtRxBuf, 2, 0, timeout); if (pnd->iLastError != 0) { log_put (LOG_CATEGORY, NFC_PRIORITY_ERROR, "%s", "Unable to receive data. (RX)"); return -1; @@ -423,10 +423,10 @@ pn532_uart_ack (nfc_device_t * pnd) return -1; } } - return (0 == uart_send (DRIVER_DATA(pnd)->port, pn53x_ack_frame, sizeof (pn53x_ack_frame))) ? 0 : -1; + return (0 == uart_send (DRIVER_DATA(pnd)->port, pn53x_ack_frame, sizeof (pn53x_ack_frame), NULL)) ? 0 : -1; } -bool +bool pn532_uart_abort_command (nfc_device_t * pnd) { if (pnd) { diff --git a/libnfc/drivers/pn532_uart.h b/libnfc/drivers/pn532_uart.h index c8505be..5ecb21c 100644 --- a/libnfc/drivers/pn532_uart.h +++ b/libnfc/drivers/pn532_uart.h @@ -25,14 +25,16 @@ #ifndef __NFC_DRIVER_PN532_UART_H__ # define __NFC_DRIVER_PN532_UART_H__ +# include + # include bool pn532_uart_probe (nfc_device_desc_t pnddDevices[], size_t szDevices, size_t * pszDeviceFound); nfc_device_t *pn532_uart_connect (const nfc_device_desc_t * pndd); void pn532_uart_disconnect (nfc_device_t * pnd); -bool pn532_uart_send (nfc_device_t * pnd, const byte_t * pbtData, const size_t szData); -int pn532_uart_receive (nfc_device_t * pnd, byte_t * pbtData, const size_t szData); +bool pn532_uart_send (nfc_device_t * pnd, const byte_t * pbtData, const size_t szData, struct timeval *timeout); +int pn532_uart_receive (nfc_device_t * pnd, byte_t * pbtData, const size_t szData, struct timeval *timeout); extern const struct nfc_driver_t pn532_uart_driver; diff --git a/libnfc/drivers/pn53x_usb.c b/libnfc/drivers/pn53x_usb.c index f08192f..504e53d 100644 --- a/libnfc/drivers/pn53x_usb.c +++ b/libnfc/drivers/pn53x_usb.c @@ -89,40 +89,34 @@ bool pn53x_usb_get_usb_device_name (struct usb_device *dev, usb_dev_handle *udev bool pn53x_usb_init (nfc_device_t *pnd); int -pn53x_usb_bulk_read (struct pn53x_usb_data *data, byte_t abtRx[], const size_t szRx) +pn53x_usb_bulk_read (struct pn53x_usb_data *data, byte_t abtRx[], const size_t szRx, struct timeval *timeout) { - int res = usb_bulk_read (data->pudh, data->uiEndPointIn, (char *) abtRx, szRx, USB_INFINITE_TIMEOUT); - if (res > 0) { - LOG_HEX ("RX", abtRx, res); - } else if (res < 0) { - log_put (LOG_CATEGORY, NFC_PRIORITY_ERROR, "Unable to read from USB (%s", _usb_strerror (res)); - } - return res; -} -int + int timeout_ms = USB_INFINITE_TIMEOUT; + if (timeout) + timeout_ms = timeout->tv_sec * 1000 + timeout->tv_usec / 1000; -pn53x_usb_bulk_read_ex (struct pn53x_usb_data *data, byte_t abtRx[], const size_t szRx, int timeout) -{ - int res = usb_bulk_read (data->pudh, data->uiEndPointIn, (char *) abtRx, szRx, timeout); + int res = usb_bulk_read (data->pudh, data->uiEndPointIn, (char *) abtRx, szRx, timeout_ms); if (res > 0) { LOG_HEX ("RX", abtRx, res); } else if (res < 0) { - if (-res != USB_TIMEDOUT) { - log_put (LOG_CATEGORY, NFC_PRIORITY_ERROR, "Unable to read from USB (%s)", _usb_strerror (res)); - } + log_put (LOG_CATEGORY, NFC_PRIORITY_ERROR, "Unable to read from USB (%s)", _usb_strerror (res)); } return res; } int -pn53x_usb_bulk_write (struct pn53x_usb_data *data, byte_t abtTx[], const size_t szTx) +pn53x_usb_bulk_write (struct pn53x_usb_data *data, byte_t abtTx[], const size_t szTx, struct timeval *timeout) { LOG_HEX ("TX", abtTx, szTx); - int res = usb_bulk_write (data->pudh, data->uiEndPointOut, (char *) abtTx, szTx, USB_INFINITE_TIMEOUT); + int timeout_ms = USB_INFINITE_TIMEOUT; + if (timeout) + timeout_ms = timeout->tv_sec * 1000 + timeout->tv_usec / 1000; + + int res = usb_bulk_write (data->pudh, data->uiEndPointOut, (char *) abtTx, szTx, timeout_ms); if (res > 0) { // HACK This little hack is a well know problem of USB, see http://www.libusb.org/ticket/6 for more details if ((res % data->uiMaxPacketSize) == 0) { - usb_bulk_write (data->pudh, data->uiEndPointOut, "\0", 0, USB_INFINITE_TIMEOUT); + usb_bulk_write (data->pudh, data->uiEndPointOut, "\0", 0, timeout_ms); } } else { log_put (LOG_CATEGORY, NFC_PRIORITY_ERROR, "Unable to write to USB (%s)", _usb_strerror (res)); @@ -154,7 +148,7 @@ pn53x_usb_get_device_model (uint16_t vendor_id, uint16_t product_id) (product_id == pn53x_usb_supported_devices[n].product_id)) return pn53x_usb_supported_devices[n].model; } - + return UNKNOWN; } @@ -414,14 +408,14 @@ pn53x_usb_disconnect (nfc_device_t * pnd) #define PN53X_USB_BUFFER_LEN (PN53x_EXTENDED_FRAME__DATA_MAX_LEN + PN53x_EXTENDED_FRAME__OVERHEAD) bool -pn53x_usb_send (nfc_device_t * pnd, const byte_t * pbtData, const size_t szData) +pn53x_usb_send (nfc_device_t * pnd, const byte_t * pbtData, const size_t szData, struct timeval *timeout) { byte_t abtFrame[PN53X_USB_BUFFER_LEN] = { 0x00, 0x00, 0xff }; // Every packet must start with "00 00 ff" size_t szFrame = 0; pn53x_build_frame (abtFrame, &szFrame, pbtData, szData); - int res = pn53x_usb_bulk_write (DRIVER_DATA (pnd), abtFrame, szFrame); + int res = pn53x_usb_bulk_write (DRIVER_DATA (pnd), abtFrame, szFrame, timeout); if (res < 0) { pnd->iLastError = ECOMIO; @@ -429,7 +423,7 @@ pn53x_usb_send (nfc_device_t * pnd, const byte_t * pbtData, const size_t szData) } byte_t abtRxBuf[PN53X_USB_BUFFER_LEN]; - res = pn53x_usb_bulk_read (DRIVER_DATA (pnd), abtRxBuf, sizeof (abtRxBuf)); + res = pn53x_usb_bulk_read (DRIVER_DATA (pnd), abtRxBuf, sizeof (abtRxBuf), timeout); if (res < 0) { pnd->iLastError = ECOMIO; // try to interrupt current device state @@ -446,7 +440,7 @@ pn53x_usb_send (nfc_device_t * pnd, const byte_t * pbtData, const size_t szData) // response packet. With this hack, the nextly executed function (ie. // pn53x_usb_receive()) will be able to retreive the correct response // packet. - int res = pn53x_usb_bulk_write (DRIVER_DATA (pnd), (byte_t *)pn53x_nack_frame, sizeof(pn53x_nack_frame)); + int res = pn53x_usb_bulk_write (DRIVER_DATA (pnd), (byte_t *)pn53x_nack_frame, sizeof(pn53x_nack_frame), timeout); if (res < 0) { pnd->iLastError = ECOMIO; // try to interrupt current device state @@ -459,7 +453,7 @@ pn53x_usb_send (nfc_device_t * pnd, const byte_t * pbtData, const size_t szData) } int -pn53x_usb_receive (nfc_device_t * pnd, byte_t * pbtData, const size_t szDataLen) +pn53x_usb_receive (nfc_device_t * pnd, byte_t * pbtData, const size_t szDataLen, struct timeval *timeout) { size_t len; off_t offset = 0; @@ -467,8 +461,11 @@ pn53x_usb_receive (nfc_device_t * pnd, byte_t * pbtData, const size_t szDataLen) switch (CHIP_DATA (pnd)->ui8LastCommand) { case InDataExchange: - case InJumpForDEP: case TgGetData: + if (!timeout) + delayed_reply = true; + break; + case InJumpForDEP: case TgInitAsTarget: delayed_reply = true; break; @@ -479,8 +476,16 @@ pn53x_usb_receive (nfc_device_t * pnd, byte_t * pbtData, const size_t szDataLen) byte_t abtRxBuf[PN53X_USB_BUFFER_LEN]; int res; + /* + * If no timeout is specified but the command is blocking, force a 250ms + * timeout to allow breaking the loop if the user wants to stop it. + */ + struct timeval fixed_timeout = { + .tv_sec = 0, + .tv_usec = 250000, + }; read: - res = pn53x_usb_bulk_read_ex (DRIVER_DATA (pnd), abtRxBuf, sizeof (abtRxBuf), delayed_reply ? 250 : USB_INFINITE_TIMEOUT); + res = pn53x_usb_bulk_read (DRIVER_DATA (pnd), abtRxBuf, sizeof (abtRxBuf), (delayed_reply && !timeout) ? &fixed_timeout : timeout); if (delayed_reply && (res == -USB_TIMEDOUT)) { if (DRIVER_DATA (pnd)->abort_flag) { @@ -590,7 +595,7 @@ read: int pn53x_usb_ack (nfc_device_t * pnd) { - return pn53x_usb_bulk_write (DRIVER_DATA (pnd), (byte_t *) pn53x_ack_frame, sizeof (pn53x_ack_frame)); + return pn53x_usb_bulk_write (DRIVER_DATA (pnd), (byte_t *) pn53x_ack_frame, sizeof (pn53x_ack_frame), NULL); } bool @@ -599,13 +604,13 @@ pn53x_usb_init (nfc_device_t *pnd) // Sometimes PN53x USB doesn't reply ACK one the first frame, so we need to send a dummy one... //pn53x_check_communication (pnd); // Sony RC-S360 doesn't support this command for now so let's use a get_firmware_version instead: const byte_t abtCmd[] = { GetFirmwareVersion }; - pn53x_transceive (pnd, abtCmd, sizeof (abtCmd), NULL, NULL); + pn53x_transceive (pnd, abtCmd, sizeof (abtCmd), NULL, NULL, NULL); // ...and we don't care about error pnd->iLastError = 0; if (SONY_RCS360 == DRIVER_DATA (pnd)->model) { log_put (LOG_CATEGORY, NFC_PRIORITY_TRACE, "SONY RC-S360 initialization."); const byte_t abtCmd2[] = { 0x18, 0x01 }; - pn53x_transceive (pnd, abtCmd2, sizeof (abtCmd2), NULL, NULL); + pn53x_transceive (pnd, abtCmd2, sizeof (abtCmd2), NULL, NULL, NULL); pn53x_usb_ack (pnd); } diff --git a/libnfc/drivers/pn53x_usb.h b/libnfc/drivers/pn53x_usb.h index a3e912c..b2bff4c 100644 --- a/libnfc/drivers/pn53x_usb.h +++ b/libnfc/drivers/pn53x_usb.h @@ -25,12 +25,14 @@ #ifndef __NFC_DRIVER_PN53X_USB_H__ # define __NFC_DRIVER_PN53X_USB_H__ +# include + # include bool pn53x_usb_probe (nfc_device_desc_t pnddDevices[], size_t szDevices, size_t * pszDeviceFound); nfc_device_t *pn53x_usb_connect (const nfc_device_desc_t * pndd); -bool pn53x_usb_send (nfc_device_t * pnd, const byte_t * pbtData, const size_t szData); -int pn53x_usb_receive (nfc_device_t * pnd, byte_t * pbtData, const size_t szData); +bool pn53x_usb_send (nfc_device_t * pnd, const byte_t * pbtData, const size_t szData, struct timeval *timeout); +int pn53x_usb_receive (nfc_device_t * pnd, byte_t * pbtData, const size_t szData, struct timeval *timeout); void pn53x_usb_disconnect (nfc_device_t * pnd); extern const struct nfc_driver_t pn53x_usb_driver; diff --git a/libnfc/log.h b/libnfc/log.h index 6b21602..90cb747 100644 --- a/libnfc/log.h +++ b/libnfc/log.h @@ -18,7 +18,7 @@ #ifndef __LOG_H__ #define __LOG_H__ -#ifdef HAS_LOG4C +#if defined(HAS_LOG4C) && HAS_LOG4C #define LOGGING 1 @@ -67,6 +67,12 @@ void log_put (char *category, int priority, char *format, ...); size_t __szPos; \ char __acBuf[1024]; \ size_t __szBuf = 0; \ + if ((int)szBytes < 0) { \ + fprintf (stderr, "%s:%d: Attempt to print %d bytes!\n", __FILE__, __LINE__, (int)szBytes); \ + log_put (LOG_CATEGORY, NFC_PRIORITY_FATAL, "%s:%d: Attempt to print %d bytes!\n", __FILE__, __LINE__, (int)szBytes); \ + abort(); \ + break; \ + } \ snprintf (__acBuf + __szBuf, sizeof(__acBuf) - __szBuf, "%s: ", pcTag); \ __szBuf += strlen (pcTag) + 2; \ for (__szPos=0; __szPos < (size_t)(szBytes); __szPos++) { \ diff --git a/libnfc/nfc-emulation.c b/libnfc/nfc-emulation.c index 98bc16a..264f651 100644 --- a/libnfc/nfc-emulation.c +++ b/libnfc/nfc-emulation.c @@ -42,12 +42,12 @@ nfc_emulate_target (nfc_device_t* 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)) { + if (!nfc_target_send_bytes(pnd, abtTx, res, NULL)) { return -1; } } if (res >= 0) { - if (!nfc_target_receive_bytes(pnd, abtRx, &szRx)) { + if (!nfc_target_receive_bytes(pnd, abtRx, &szRx, NULL)) { return -1; } } diff --git a/libnfc/nfc-internal.h b/libnfc/nfc-internal.h index 09247e4..f9ce952 100644 --- a/libnfc/nfc-internal.h +++ b/libnfc/nfc-internal.h @@ -136,14 +136,14 @@ struct nfc_driver_t { bool (*initiator_poll_targets) (nfc_device_t * pnd, const nfc_modulation_t * pnmModulations, const size_t szModulations, const byte_t btPollNr, const byte_t btPeriod, nfc_target_t * pntTargets, size_t * pszTargetFound); bool (*initiator_select_dep_target) (nfc_device_t * pnd, const nfc_dep_mode_t ndm, const nfc_baud_rate_t nbr, const nfc_dep_info_t * pndiInitiator, nfc_target_t * pnt); bool (*initiator_deselect_target) (nfc_device_t * pnd); - bool (*initiator_transceive_bytes) (nfc_device_t * pnd, const byte_t * pbtTx, const size_t szTx, byte_t * pbtRx, size_t * pszRx); + bool (*initiator_transceive_bytes) (nfc_device_t * pnd, const byte_t * pbtTx, const size_t szTx, byte_t * pbtRx, size_t * pszRx, struct timeval *timeout); bool (*initiator_transceive_bits) (nfc_device_t * pnd, const byte_t * pbtTx, const size_t szTxBits, const byte_t * pbtTxPar, byte_t * pbtRx, size_t * pszRxBits, byte_t * pbtRxPar); bool (*initiator_transceive_bytes_timed) (nfc_device_t * pnd, const byte_t * pbtTx, const size_t szTx, byte_t * pbtRx, size_t * pszRx, uint32_t * cycles); bool (*initiator_transceive_bits_timed) (nfc_device_t * pnd, const byte_t * pbtTx, const size_t szTxBits, const byte_t * pbtTxPar, byte_t * pbtRx, size_t * pszRxBits, byte_t * pbtRxPar, uint32_t * cycles); bool (*target_init) (nfc_device_t * pnd, nfc_target_t * pnt, byte_t * pbtRx, size_t * pszRx); - bool (*target_send_bytes) (nfc_device_t * pnd, const byte_t * pbtTx, const size_t szTx); - bool (*target_receive_bytes) (nfc_device_t * pnd, byte_t * pbtRx, size_t * pszRx); + bool (*target_send_bytes) (nfc_device_t * pnd, const byte_t * pbtTx, const size_t szTx, struct timeval *timeout); + bool (*target_receive_bytes) (nfc_device_t * pnd, byte_t * pbtRx, size_t * pszRx, struct timeval *timeout); bool (*target_send_bits) (nfc_device_t * pnd, const byte_t * pbtTx, const size_t szTxBits, const byte_t * pbtTxPar); bool (*target_receive_bits) (nfc_device_t * pnd, byte_t * pbtRx, size_t * pszRxBits, byte_t * pbtRxPar); diff --git a/libnfc/nfc.c b/libnfc/nfc.c index ef96ae3..76378ed 100644 --- a/libnfc/nfc.c +++ b/libnfc/nfc.c @@ -472,9 +472,9 @@ nfc_initiator_deselect_target (nfc_device_t * pnd) */ bool nfc_initiator_transceive_bytes (nfc_device_t * pnd, const byte_t * pbtTx, const size_t szTx, byte_t * pbtRx, - size_t * pszRx) + size_t * pszRx, struct timeval *timeout) { - HAL (initiator_transceive_bytes, pnd, pbtTx, szTx, pbtRx, pszRx) + HAL (initiator_transceive_bytes, pnd, pbtTx, szTx, pbtRx, pszRx, timeout) } /** @@ -679,9 +679,9 @@ nfc_abort_command (nfc_device_t * pnd) * (e.g. APDU responses) to the \e initiator. */ bool -nfc_target_send_bytes (nfc_device_t * pnd, const byte_t * pbtTx, const size_t szTx) +nfc_target_send_bytes (nfc_device_t * pnd, const byte_t * pbtTx, const size_t szTx, struct timeval *timeout) { - HAL (target_send_bytes, pnd, pbtTx, szTx); + HAL (target_send_bytes, pnd, pbtTx, szTx, timeout); } /** @@ -694,9 +694,9 @@ nfc_target_send_bytes (nfc_device_t * pnd, const byte_t * pbtTx, const size_t sz * This function retrieves bytes frames (e.g. ADPU) sent by the \e initiator to the NFC device (configured as \e target). */ bool -nfc_target_receive_bytes (nfc_device_t * pnd, byte_t * pbtRx, size_t * pszRx) +nfc_target_receive_bytes (nfc_device_t * pnd, byte_t * pbtRx, size_t * pszRx, struct timeval *timeout) { - HAL (target_receive_bytes, pnd, pbtRx, pszRx); + HAL (target_receive_bytes, pnd, pbtRx, pszRx, timeout); } /**