From 921d28d9765c6affa16b8376980d362d443053f3 Mon Sep 17 00:00:00 2001 From: Romuald Conty Date: Mon, 9 May 2011 11:14:43 +0000 Subject: [PATCH] Attempt to provide an abort mecanism for windows users... --- libnfc/buses/uart.h | 2 +- libnfc/buses/uart_posix.c | 3 ++- libnfc/buses/uart_win32.c | 13 ++++++++----- libnfc/drivers/arygon.c | 26 ++++++++++++++++++++++---- libnfc/drivers/pn532_uart.c | 26 ++++++++++++++++++++++---- 5 files changed, 55 insertions(+), 15 deletions(-) diff --git a/libnfc/buses/uart.h b/libnfc/buses/uart.h index b899f28..9508782 100644 --- a/libnfc/buses/uart.h +++ b/libnfc/buses/uart.h @@ -46,7 +46,7 @@ void uart_close (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, int iAbortFd); +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); char **uart_list_ports (void); diff --git a/libnfc/buses/uart_posix.c b/libnfc/buses/uart_posix.c index c45afe9..c320431 100644 --- a/libnfc/buses/uart_posix.c +++ b/libnfc/buses/uart_posix.c @@ -224,8 +224,9 @@ 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, int iAbortFd) +uart_receive (serial_port sp, byte_t * pbtRx, const size_t szRx, void * abort_p) { + int iAbortFd = *((int*)abort_p); struct timeval tv = tvTimeout; struct timeval *ptv = &tv; int received_bytes_count = 0; diff --git a/libnfc/buses/uart_win32.c b/libnfc/buses/uart_win32.c index f8ae352..74c6e81 100644 --- a/libnfc/buses/uart_win32.c +++ b/libnfc/buses/uart_win32.c @@ -142,13 +142,16 @@ uart_get_speed (const serial_port sp) } int -uart_receive (serial_port sp, byte_t * pbtRx, const size_t szRx, int iAbortFd) +uart_receive (serial_port sp, byte_t * pbtRx, const size_t szRx, void * abort_p) { - // TODO: Implement abort mecanism (using iAbortFd) + // TODO Test me with abort_p + volatile bool * abort_flag_p = (volatile bool *)abort_p; DWORD dwRxLen = szRx; - if (!ReadFile (((serial_port_windows *) sp)->hPort, pbtRx, dwRxLen, &dwRxLen, NULL)) { - return DEIO; - } + do { + if (!ReadFile (((serial_port_windows *) sp)->hPort, pbtRx, dwRxLen, &dwRxLen, NULL)) { + return DEIO; + } + } while ( (dwRxLen != (DWORD) szRx) && ((abort_flag_p) && !(*abort_flag_p)) ); return (dwRxLen == (DWORD) szRx) ? 0 : DEIO; } diff --git a/libnfc/drivers/arygon.c b/libnfc/drivers/arygon.c index f58c423..1726628 100644 --- a/libnfc/drivers/arygon.c +++ b/libnfc/drivers/arygon.c @@ -72,7 +72,11 @@ const struct pn53x_io arygon_tama_io; struct arygon_data { serial_port port; +#ifndef WIN32 int iAbortFds[2]; +#else + volatile bool abort_flag; +#endif }; static const byte_t arygon_error_none[] = "FF000000\x0d\x0a"; @@ -186,8 +190,12 @@ arygon_connect (const nfc_device_desc_t * pndd) return NULL; } +#ifndef WIN32 // pipe-based abort mecanism pipe (DRIVER_DATA (pnd)->iAbortFds); +#else + DRIVER_DATA (pnd)->abort_flag = false; +#endif char arygon_firmware_version[10]; arygon_firmware (pnd, arygon_firmware_version); @@ -206,9 +214,11 @@ arygon_disconnect (nfc_device_t * pnd) // Release UART port uart_close (DRIVER_DATA (pnd)->port); +#ifndef WIN32 // Release file descriptors used for abort mecanism close (DRIVER_DATA (pnd)->iAbortFds[0]); close (DRIVER_DATA (pnd)->iAbortFds[1]); +#endif nfc_device_free (pnd); } @@ -280,7 +290,7 @@ arygon_tama_receive (nfc_device_t * pnd, byte_t * pbtData, const size_t szDataLe { byte_t abtRxBuf[5]; size_t len; - int abort_fd = 0; + void * abort_p = NULL; switch (CHIP_DATA (pnd)->ui8LastCommand) { case InAutoPoll: @@ -288,15 +298,19 @@ arygon_tama_receive (nfc_device_t * pnd, byte_t * pbtData, const size_t szDataLe case InJumpForDEP: case TgGetData: case TgInitAsTarget: - abort_fd = DRIVER_DATA (pnd)->iAbortFds[1]; +#ifndef WIN32 + abort_p = &(DRIVER_DATA (pnd)->iAbortFds[1]); +#else + abort_p = &(DRIVER_DATA (pnd)->abort_flag); +#endif break; default: break; } - pnd->iLastError = uart_receive (DRIVER_DATA (pnd)->port, abtRxBuf, 5, abort_fd); + pnd->iLastError = uart_receive (DRIVER_DATA (pnd)->port, abtRxBuf, 5, abort_p); - if (abort_fd && (DEABORT == pnd->iLastError)) { + if (abort_p && (DEABORT == pnd->iLastError)) { arygon_abort (pnd); /* iLastError got reset by arygon_abort() */ @@ -456,8 +470,12 @@ bool arygon_abort_command (nfc_device_t * pnd) { if (pnd) { +#ifndef WIN32 close (DRIVER_DATA (pnd)->iAbortFds[0]); pipe (DRIVER_DATA (pnd)->iAbortFds); +#else + DRIVER_DATA (pnd)->abort_flag = true; +#endif } return true; } diff --git a/libnfc/drivers/pn532_uart.c b/libnfc/drivers/pn532_uart.c index b8db826..763c277 100644 --- a/libnfc/drivers/pn532_uart.c +++ b/libnfc/drivers/pn532_uart.c @@ -56,7 +56,11 @@ const struct pn53x_io pn532_uart_io; struct pn532_uart_data { serial_port port; +#ifndef WIN32 int iAbortFds[2]; +#else + volatile bool abort_flag; +#endif }; #define DRIVER_DATA(pnd) ((struct pn532_uart_data*)(pnd->driver_data)) @@ -173,8 +177,12 @@ pn532_uart_connect (const nfc_device_desc_t * pndd) return NULL; } +#ifndef WIN32 // pipe-based abort mecanism pipe (DRIVER_DATA (pnd)->iAbortFds); +#else + abort_flag = false; +#endif pn53x_init(pnd); return pnd; @@ -186,9 +194,11 @@ pn532_uart_disconnect (nfc_device_t * pnd) // Release UART port uart_close (DRIVER_DATA(pnd)->port); +#ifndef WIN32 // Release file descriptors used for abort mecanism close (DRIVER_DATA (pnd)->iAbortFds[0]); close (DRIVER_DATA (pnd)->iAbortFds[1]); +#endif nfc_device_free (pnd); } @@ -267,7 +277,7 @@ pn532_uart_receive (nfc_device_t * pnd, byte_t * pbtData, const size_t szDataLen { byte_t abtRxBuf[5]; size_t len; - int abort_fd = 0; + void * abort_p = NULL; switch (CHIP_DATA (pnd)->ui8LastCommand) { case InAutoPoll: @@ -275,15 +285,19 @@ pn532_uart_receive (nfc_device_t * pnd, byte_t * pbtData, const size_t szDataLen case InJumpForDEP: case TgGetData: case TgInitAsTarget: - abort_fd = DRIVER_DATA (pnd)->iAbortFds[1]; +#ifndef WIN32 + abort_p = &(DRIVER_DATA (pnd)->iAbortFds[1]); +#else + abort_p = &(DRIVER_DATA (pnd)->abort_flag); +#endif break; default: break; } - pnd->iLastError = uart_receive (DRIVER_DATA(pnd)->port, abtRxBuf, 5, abort_fd); + pnd->iLastError = uart_receive (DRIVER_DATA(pnd)->port, abtRxBuf, 5, abort_p); - if (abort_fd && (DEABORT == pnd->iLastError)) { + if (abort_p && (DEABORT == pnd->iLastError)) { pn532_uart_ack (pnd); return -1; } @@ -407,8 +421,12 @@ bool pn532_uart_abort_command (nfc_device_t * pnd) { if (pnd) { +#ifndef WIN32 close (DRIVER_DATA (pnd)->iAbortFds[0]); pipe (DRIVER_DATA (pnd)->iAbortFds); +#else + DRIVER_DATA (pnd)->abort_flag = true; +#endif } return true; }