From 8f7834c6252992ef5d32e3543b890202e0509f50 Mon Sep 17 00:00:00 2001 From: Romuald Conty Date: Thu, 29 Sep 2011 14:53:52 +0000 Subject: [PATCH] drivers/arygon,pn532_uart,pn53x_usb: all PN53x commands are now cancelable --- examples/pn53x-sam.c | 5 +++-- libnfc/buses/uart_posix.c | 15 +------------- libnfc/chips/pn53x.c | 10 ++++++--- libnfc/chips/pn53x.h | 2 +- libnfc/drivers/arygon.c | 41 +++++++++++++++++++------------------ libnfc/drivers/pn532_uart.c | 40 ++++++++++++++++++------------------ libnfc/drivers/pn53x_usb.c | 14 +------------ 7 files changed, 54 insertions(+), 73 deletions(-) diff --git a/examples/pn53x-sam.c b/examples/pn53x-sam.c index 525308d..0b40175 100644 --- a/examples/pn53x-sam.c +++ b/examples/pn53x-sam.c @@ -109,7 +109,8 @@ main (int argc, const char *argv[]) pn532_sam_mode mode = iMode; // Connect with the SAM - if (!pn53x_SAMConfiguration (pnd, mode)) { + // FIXME: Its a private pn53x function + if (!pn53x_SAMConfiguration (pnd, mode, NULL)) { nfc_perror (pnd, "pn53x_SAMConfiguration"); exit (EXIT_FAILURE); } @@ -184,7 +185,7 @@ main (int argc, const char *argv[]) } // Disconnect from the SAM - pn53x_SAMConfiguration (pnd, PSM_NORMAL); + pn53x_SAMConfiguration (pnd, PSM_NORMAL, NULL); // Disconnect from NFC device nfc_disconnect (pnd); diff --git a/libnfc/buses/uart_posix.c b/libnfc/buses/uart_posix.c index ddbd5dd..044e9c9 100644 --- a/libnfc/buses/uart_posix.c +++ b/libnfc/buses/uart_posix.c @@ -243,11 +243,6 @@ uart_close (const serial_port sp) uart_close_ext (sp, true); } -static const struct timeval tvTimeout = { - .tv_sec = 1, - .tv_usec = 0 -}; - /** * @brief Receive data from UART and copy data to \a pbtRx * @@ -257,12 +252,6 @@ int 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; - if (timeout) - tv = *timeout; - else - tv = tvTimeout; - struct timeval *ptv = &tv; int received_bytes_count = 0; int available_bytes_count = 0; const int expected_bytes_count = (int)szRx; @@ -276,11 +265,9 @@ select: if (iAbortFd) { FD_SET (iAbortFd, &rfds); - 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); + res = select (MAX(((serial_port_unix *) sp)->fd, iAbortFd) + 1, &rfds, NULL, NULL, timeout); if ((res < 0) && (EINTR == errno)) { // The system call was interupted by a signal and a signal handler was diff --git a/libnfc/chips/pn53x.c b/libnfc/chips/pn53x.c index c1797d0..831a01c 100644 --- a/libnfc/chips/pn53x.c +++ b/libnfc/chips/pn53x.c @@ -831,7 +831,11 @@ 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, NULL)) + struct timeval timeout; + timeout.tv_sec = 1; + timeout.tv_usec = 0; + + if (!pn53x_transceive (pnd, abtCmd, sizeof (abtCmd), abtRx, &szRx, &timeout)) return false; return ((sizeof(abtExpectedRx) == szRx) && (0 == memcmp (abtRx, abtExpectedRx, sizeof(abtExpectedRx)))); @@ -1935,7 +1939,7 @@ pn53x_SetParameters (nfc_device_t * pnd, const uint8_t ui8Value) } bool -pn53x_SAMConfiguration (nfc_device_t * pnd, const pn532_sam_mode ui8Mode) +pn53x_SAMConfiguration (nfc_device_t * pnd, const pn532_sam_mode ui8Mode, struct timeval *timeout) { byte_t abtCmd[] = { SAMConfiguration, ui8Mode, 0x00, 0x00 }; size_t szCmd = sizeof(abtCmd); @@ -1960,7 +1964,7 @@ pn53x_SAMConfiguration (nfc_device_t * pnd, const pn532_sam_mode ui8Mode) pnd->iLastError = EINVALARG; return false; } - return (pn53x_transceive (pnd, abtCmd, szCmd, NULL, NULL, NULL)); + return (pn53x_transceive (pnd, abtCmd, szCmd, NULL, NULL, timeout)); } bool diff --git a/libnfc/chips/pn53x.h b/libnfc/chips/pn53x.h index 1beae4e..f7323af 100644 --- a/libnfc/chips/pn53x.h +++ b/libnfc/chips/pn53x.h @@ -313,7 +313,7 @@ const char *pn53x_strerror (const nfc_device_t * pnd); // C wrappers for PN53x commands bool pn53x_SetParameters (nfc_device_t * pnd, const uint8_t ui8Value); -bool pn53x_SAMConfiguration (nfc_device_t * pnd, const pn532_sam_mode mode); +bool pn53x_SAMConfiguration (nfc_device_t * pnd, const pn532_sam_mode mode, struct timeval *timeout); bool pn53x_PowerDown (nfc_device_t * pnd); bool pn53x_InListPassiveTarget (nfc_device_t * pnd, const pn53x_modulation_t pmInitModulation, const byte_t szMaxTargets, const byte_t * pbtInitiatorData, diff --git a/libnfc/drivers/arygon.c b/libnfc/drivers/arygon.c index b04d021..35b2a6a 100644 --- a/libnfc/drivers/arygon.c +++ b/libnfc/drivers/arygon.c @@ -124,6 +124,13 @@ arygon_probe (nfc_device_desc_t pnddDevices[], size_t szDevices, size_t * pszDev // Alloc and init chip's data pn53x_data_new (pnd, &arygon_tama_io); +#ifndef WIN32 + // pipe-based abort mecanism + pipe (DRIVER_DATA (pnd)->iAbortFds); +#else + DRIVER_DATA (pnd)->abort_flag = false; +#endif + bool res = arygon_reset_tama (pnd); pn53x_data_free (pnd); nfc_device_free (pnd); @@ -193,12 +200,6 @@ arygon_connect (const nfc_device_desc_t * pndd) CHIP_DATA (pnd)->timer_correction = 46; pnd->driver = &arygon_driver; - // Check communication using "Reset TAMA" command - if (!arygon_reset_tama(pnd)) { - nfc_device_free (pnd); - return NULL; - } - #ifndef WIN32 // pipe-based abort mecanism pipe (DRIVER_DATA (pnd)->iAbortFds); @@ -206,6 +207,12 @@ arygon_connect (const nfc_device_desc_t * pndd) DRIVER_DATA (pnd)->abort_flag = false; #endif + // Check communication using "Reset TAMA" command + if (!arygon_reset_tama(pnd)) { + nfc_device_free (pnd); + return NULL; + } + char arygon_firmware_version[10]; arygon_firmware (pnd, arygon_firmware_version); char *pcName; @@ -304,21 +311,11 @@ arygon_tama_receive (nfc_device_t * pnd, byte_t * pbtData, const size_t szDataLe size_t len; void * abort_p = NULL; - switch (CHIP_DATA (pnd)->ui8LastCommand) { - case InAutoPoll: - case InDataExchange: - case InJumpForDEP: - case TgGetData: - case TgInitAsTarget: #ifndef WIN32 - abort_p = &(DRIVER_DATA (pnd)->iAbortFds[1]); + abort_p = &(DRIVER_DATA (pnd)->iAbortFds[1]); #else - abort_p = &(DRIVER_DATA (pnd)->abort_flag); + abort_p = &(DRIVER_DATA (pnd)->abort_flag); #endif - break; - default: - break; - } pnd->iLastError = uart_receive (DRIVER_DATA (pnd)->port, abtRxBuf, 5, abort_p, timeout); @@ -461,11 +458,15 @@ 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), NULL); + struct timeval tv; + tv.tv_sec = 1; + tv.tv_usec = 0; + + uart_send (DRIVER_DATA (pnd)->port, arygon_reset_tama_cmd, sizeof (arygon_reset_tama_cmd), &tv); // 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, NULL); + res = uart_receive (DRIVER_DATA (pnd)->port, abtRx, szRx, 0, &tv); if (res != 0) { log_put (LOG_CATEGORY, NFC_PRIORITY_TRACE, "No reply to 'reset TAMA' command."); return false; diff --git a/libnfc/drivers/pn532_uart.c b/libnfc/drivers/pn532_uart.c index 76ffae3..b5f4aff 100644 --- a/libnfc/drivers/pn532_uart.c +++ b/libnfc/drivers/pn532_uart.c @@ -105,6 +105,13 @@ pn532_uart_probe (nfc_device_desc_t pnddDevices[], size_t szDevices, size_t * ps // This device starts in LowVBat power mode CHIP_DATA (pnd)->power_mode = LOWVBAT; +#ifndef WIN32 + // pipe-based abort mecanism + pipe (DRIVER_DATA (pnd)->iAbortFds); +#else + DRIVER_DATA (pnd)->abort_flag = false; +#endif + // Check communication using "Diagnose" command, with "Communication test" (0x00) bool res = pn53x_check_communication (pnd); if(!res) { @@ -176,13 +183,6 @@ pn532_uart_connect (const nfc_device_desc_t * pndd) CHIP_DATA(pnd)->timer_correction = 48; pnd->driver = &pn532_uart_driver; - // Check communication using "Diagnose" command, with "Communication test" (0x00) - if (!pn53x_check_communication (pnd)) { - nfc_perror (pnd, "pn53x_check_communication"); - pn532_uart_disconnect(pnd); - return NULL; - } - #ifndef WIN32 // pipe-based abort mecanism pipe (DRIVER_DATA (pnd)->iAbortFds); @@ -190,6 +190,13 @@ pn532_uart_connect (const nfc_device_desc_t * pndd) DRIVER_DATA (pnd)->abort_flag = false; #endif + // Check communication using "Diagnose" command, with "Communication test" (0x00) + if (!pn53x_check_communication (pnd)) { + nfc_perror (pnd, "pn53x_check_communication"); + pn532_uart_disconnect(pnd); + return NULL; + } + pn53x_init(pnd); return pnd; } @@ -234,7 +241,10 @@ pn532_uart_send (nfc_device_t * pnd, const byte_t * pbtData, const size_t szData return false; } // According to PN532 application note, C106 appendix: to go out Low Vbat mode and enter in normal mode we need to send a SAMConfiguration command - if (!pn53x_SAMConfiguration (pnd, 0x01)) { + struct timeval tv; + tv.tv_sec = 1; + tv.tv_usec = 0; + if (!pn53x_SAMConfiguration (pnd, 0x01, &tv)) { return false; } } @@ -288,21 +298,11 @@ pn532_uart_receive (nfc_device_t * pnd, byte_t * pbtData, const size_t szDataLen size_t len; void * abort_p = NULL; - switch (CHIP_DATA (pnd)->ui8LastCommand) { - case InAutoPoll: - case InDataExchange: - case InJumpForDEP: - case TgGetData: - case TgInitAsTarget: #ifndef WIN32 - abort_p = &(DRIVER_DATA (pnd)->iAbortFds[1]); + abort_p = &(DRIVER_DATA (pnd)->iAbortFds[1]); #else - abort_p = (void*)&(DRIVER_DATA (pnd)->abort_flag); + abort_p = (void*)&(DRIVER_DATA (pnd)->abort_flag); #endif - break; - default: - break; - } pnd->iLastError = uart_receive (DRIVER_DATA(pnd)->port, abtRxBuf, 5, abort_p, timeout); diff --git a/libnfc/drivers/pn53x_usb.c b/libnfc/drivers/pn53x_usb.c index d11e396..1372888 100644 --- a/libnfc/drivers/pn53x_usb.c +++ b/libnfc/drivers/pn53x_usb.c @@ -489,18 +489,6 @@ pn53x_usb_receive (nfc_device_t * pnd, byte_t * pbtData, const size_t szDataLen, { size_t len; off_t offset = 0; - bool delayed_reply = false; - - switch (CHIP_DATA (pnd)->ui8LastCommand) { - case InDataExchange: - case TgGetData: - case InJumpForDEP: - case TgInitAsTarget: - delayed_reply = true; - break; - default: - break; - } byte_t abtRxBuf[PN53X_USB_BUFFER_LEN]; int res; @@ -537,7 +525,7 @@ read: } res = pn53x_usb_bulk_read (DRIVER_DATA (pnd), abtRxBuf, sizeof (abtRxBuf), &usb_timeout); - if (delayed_reply && (res == -USB_TIMEDOUT)) { + if (res == -USB_TIMEDOUT) { if (DRIVER_DATA (pnd)->abort_flag) { DRIVER_DATA (pnd)->abort_flag = false; pn53x_usb_ack (pnd);