From 7bb4f4597d3352af82c5af64752f2a330ddf1f83 Mon Sep 17 00:00:00 2001 From: Romuald Conty Date: Tue, 10 May 2011 13:26:57 +0000 Subject: [PATCH] windows port: implement abort mecanism in pn532_uart driver (Based on provided patch: many thanks to Edwin Evans) --- libnfc/buses/uart_win32.c | 28 +++++++++++++++++++++++----- libnfc/drivers/pn532_uart.c | 4 ++-- 2 files changed, 25 insertions(+), 7 deletions(-) diff --git a/libnfc/buses/uart_win32.c b/libnfc/buses/uart_win32.c index 74c6e81..19acc1f 100644 --- a/libnfc/buses/uart_win32.c +++ b/libnfc/buses/uart_win32.c @@ -144,15 +144,33 @@ uart_get_speed (const serial_port sp) int uart_receive (serial_port sp, byte_t * pbtRx, const size_t szRx, void * abort_p) { - // TODO Test me with abort_p + DWORD dwBytesToGet = (DWORD)szRx; + DWORD dwBytesReceived = 0; + DWORD dwTotalBytesReceived = 0; + BOOL res; + volatile bool * abort_flag_p = (volatile bool *)abort_p; - DWORD dwRxLen = szRx; do { - if (!ReadFile (((serial_port_windows *) sp)->hPort, pbtRx, dwRxLen, &dwRxLen, NULL)) { + res = ReadFile (((serial_port_windows *) sp)->hPort, pbtRx + received_bytes_count, + dwBytesToGet, + &dwBytesReceived, NULL); + + dwTotalBytesReceived += dwBytesReceived; + + if (!res) { + WARN("ReadFile returned error\n"); return DEIO; } - } while ( (dwRxLen != (DWORD) szRx) && ((abort_flag_p) && !(*abort_flag_p)) ); - return (dwRxLen == (DWORD) szRx) ? 0 : DEIO; + if (((DWORD)szRx) > dwTotalBytesReceived) { + dwBytesToGet -= dwBytesReceived; + } + + if (abort_flag_p != NULL && (*abort_flag_p) && dwTotalBytesReceived == 0) { + return DEABORT; + } + } while (((DWORD)szRx) > dwTotalBytesReceived); + + return (dwTotalBytesReceived == (DWORD) szRx) ? 0 : DEIO; } int diff --git a/libnfc/drivers/pn532_uart.c b/libnfc/drivers/pn532_uart.c index 0f99c17..5c011b6 100644 --- a/libnfc/drivers/pn532_uart.c +++ b/libnfc/drivers/pn532_uart.c @@ -182,7 +182,7 @@ pn532_uart_connect (const nfc_device_desc_t * pndd) // pipe-based abort mecanism pipe (DRIVER_DATA (pnd)->iAbortFds); #else - abort_flag = false; + DRIVER_DATA (pnd)->abort_flag = false; #endif pn53x_init(pnd); @@ -288,7 +288,7 @@ pn532_uart_receive (nfc_device_t * pnd, byte_t * pbtData, const size_t szDataLen #ifndef WIN32 abort_p = &(DRIVER_DATA (pnd)->iAbortFds[1]); #else - abort_p = &(DRIVER_DATA (pnd)->abort_flag); + abort_p = (void*)&(DRIVER_DATA (pnd)->abort_flag); #endif break; default: