From ecb4639a047e9855a54c7a3eadd61ca097e3943c Mon Sep 17 00:00:00 2001 From: dxl <64101226@qq.com> Date: Fri, 8 May 2020 18:10:07 +0800 Subject: [PATCH] PN53X communication bug fixed. --- app_main/build.gradle | 2 +- chameleon/build.gradle | 2 +- communication/build.gradle | 2 +- libnfc_pn53x/build.gradle | 2 +- .../src/main/cpp/libnfc/buses/com_uart.c | 2 - .../src/main/cpp/libnfc/buses/uart_posix.c | 55 +++---------------- .../src/main/cpp/libnfc/include/uart.h | 4 -- pm3flasher/build.gradle | 2 +- pm3rdv4rrg/build.gradle | 2 +- 9 files changed, 14 insertions(+), 59 deletions(-) diff --git a/app_main/build.gradle b/app_main/build.gradle index 44e0830a..8dace2e2 100644 --- a/app_main/build.gradle +++ b/app_main/build.gradle @@ -58,7 +58,7 @@ dependencies { implementation 'com.github.xianglin1998:nfctag:1.0' implementation 'com.github.xianglin1998:nfc_console:1.0' - implementation 'com.github.xianglin1998:ComBridge:1.0.3' + implementation 'com.github.xianglin1998:ComBridge:v1.0.4' implementation project(':utils') implementation project(':libnfc_pn53x') diff --git a/chameleon/build.gradle b/chameleon/build.gradle index f36a0e13..ab2e9c05 100644 --- a/chameleon/build.gradle +++ b/chameleon/build.gradle @@ -24,7 +24,7 @@ dependencies { implementation 'com.github.xianglin1998:mfkey:1.1' implementation 'com.github.xianglin1998:nfctag:1.0' - implementation 'com.github.xianglin1998:ComBridge:1.0.3' + implementation 'com.github.xianglin1998:ComBridge:v1.0.4' implementation project(path: ':utils') implementation project(path: ':communication') diff --git a/communication/build.gradle b/communication/build.gradle index 3b6dfc72..afbb8ca3 100644 --- a/communication/build.gradle +++ b/communication/build.gradle @@ -24,5 +24,5 @@ dependencies { implementation fileTree(dir: 'libs', include: ['*.jar']) implementation 'com.github.felHR85:UsbSerial:6.1.0' implementation 'androidx.annotation:annotation:1.1.0' - implementation 'com.github.xianglin1998:ComBridge:1.0.3' + implementation 'com.github.xianglin1998:ComBridge:v1.0.4' } diff --git a/libnfc_pn53x/build.gradle b/libnfc_pn53x/build.gradle index 08034824..2e6a6116 100644 --- a/libnfc_pn53x/build.gradle +++ b/libnfc_pn53x/build.gradle @@ -38,7 +38,7 @@ dependencies { implementation fileTree(dir: 'libs', include: ['*.jar']) implementation 'com.github.xianglin1998:nfctag:1.0' implementation 'com.github.xianglin1998:nfc_console:1.0' - implementation 'com.github.xianglin1998:ComBridge:1.0.3' + implementation 'com.github.xianglin1998:ComBridge:v1.0.4' implementation project(path: ':communication') diff --git a/libnfc_pn53x/src/main/cpp/libnfc/buses/com_uart.c b/libnfc_pn53x/src/main/cpp/libnfc/buses/com_uart.c index f3d765d6..f427ba0b 100644 --- a/libnfc_pn53x/src/main/cpp/libnfc/buses/com_uart.c +++ b/libnfc_pn53x/src/main/cpp/libnfc/buses/com_uart.c @@ -21,7 +21,6 @@ void c_close() { int c_read(uint8_t *pbtRx, size_t szRx, int timeout) { if (sp != INVALID_SERIAL_PORT) { - uart_reconfigure_timeouts(388); size_t recvLen = 0; size_t *pRecvLen = &recvLen; if (uart_receive(sp, pbtRx, szRx, pRecvLen) == NFC_SUCCESS) { @@ -34,7 +33,6 @@ int c_read(uint8_t *pbtRx, size_t szRx, int timeout) { int c_write(const uint8_t *pbtTx, size_t szTx, int timeout) { if (sp != INVALID_SERIAL_PORT) { - uart_reconfigure_timeouts((uint32_t) timeout); if (uart_send(sp, pbtTx, szTx) == NFC_SUCCESS) { return szTx; } diff --git a/libnfc_pn53x/src/main/cpp/libnfc/buses/uart_posix.c b/libnfc_pn53x/src/main/cpp/libnfc/buses/uart_posix.c index 02e164e4..e251fcb5 100644 --- a/libnfc_pn53x/src/main/cpp/libnfc/buses/uart_posix.c +++ b/libnfc_pn53x/src/main/cpp/libnfc/buses/uart_posix.c @@ -68,19 +68,12 @@ typedef struct { #define UART_FPC_CLIENT_RX_TIMEOUT_MS 200 #define UART_TCP_CLIENT_RX_TIMEOUT_MS 500 -// see pm3_cmd.h -struct timeval timeout = { - .tv_sec = 0, // 0 second - .tv_usec = UART_FPC_CLIENT_RX_TIMEOUT_MS * 1000 -}; - uint32_t newtimeout_value = 0; bool newtimeout_pending = false; int uart_reconfigure_timeouts(uint32_t value) { - - newtimeout_value = value; newtimeout_pending = true; + newtimeout_value = value; return NFC_SUCCESS; } @@ -88,9 +81,6 @@ serial_port uart_open(const char *pcPortName, uint32_t speed) { serial_port_unix *sp = calloc(sizeof(serial_port_unix), sizeof(uint8_t)); if (sp == 0) return INVALID_SERIAL_PORT; - // init timeouts - timeout.tv_usec = UART_FPC_CLIENT_RX_TIMEOUT_MS * 1000; - if (memcmp(pcPortName, "tcp:", 4) == 0) { struct addrinfo *addr = NULL, *rp; char *addrstr = strdup(pcPortName + 4); @@ -101,8 +91,6 @@ serial_port uart_open(const char *pcPortName, uint32_t speed) { return INVALID_SERIAL_PORT; } - timeout.tv_usec = UART_TCP_CLIENT_RX_TIMEOUT_MS * 1000; - char *colon = strrchr(addrstr, ':'); const char *portstr; if (colon) { @@ -172,9 +160,6 @@ serial_port uart_open(const char *pcPortName, uint32_t speed) { return INVALID_SERIAL_PORT; } - // we must use max timeout! - timeout.tv_usec = UART_TCP_CLIENT_RX_TIMEOUT_MS * 1000; - size_t servernameLen = (strlen(pcPortName) - 7) + 1; char serverNameBuf[servernameLen]; memset(serverNameBuf, '\0', servernameLen); @@ -295,20 +280,17 @@ void uart_close(const serial_port sp) { int uart_receive(const serial_port sp, uint8_t *pbtRx, uint32_t pszMaxRxLen, uint32_t *pszRxLen) { uint32_t byteCount; // FIONREAD returns size on 32b fd_set rfds; - struct timeval tv; - if (newtimeout_pending) { - timeout.tv_usec = newtimeout_value * 1000; - newtimeout_pending = false; - } // Reset the output count *pszRxLen = 0; do { // Reset file descriptor FD_ZERO(&rfds); FD_SET(((serial_port_unix *) sp)->fd, &rfds); - tv = timeout; - int res = select(((serial_port_unix *) sp)->fd + 1, &rfds, NULL, NULL, &tv); + + // int res = select(((serial_port_unix *) sp)->fd + 1, &rfds, NULL, NULL, NULL); + struct timeval timeval = {.tv_sec = 2}; + int res = select(((serial_port_unix *) sp)->fd + 1, &rfds, NULL, NULL, &timeval); // Read error if (res < 0) { @@ -346,7 +328,7 @@ int uart_receive(const serial_port sp, uint8_t *pbtRx, uint32_t pszMaxRxLen, uin // Stop if the OS has some troubles reading the data if (res <= 0) { return NFC_EIO; - } else if (res > 0 && pszMaxRxLen > 255) { // 只有USB通信下才有大于255个字节的通信请求 + } else if (res > 0 && pszMaxRxLen >= 255) { // 只有USB通信下才有大于255个字节的通信请求 *pszRxLen += res; /* * TODO Look me! @@ -355,7 +337,7 @@ int uart_receive(const serial_port sp, uint8_t *pbtRx, uint32_t pszMaxRxLen, uin * The communication process of PM3 is very different from the implementation of libnfc * (libnfc always requires about 265 bytes of data). * If we keep blocking and waiting, the program will be very slow, - * so we need to return the data directly without waiting for enough 265 bytes. + * so we need to return the data directly without waiting for enough 255 bytes. * * 这个UART实现是从PM3的开源库里拷贝过来的。 * PM3的通信过程跟LIBNFC的实现有非常大的区别(LIBNFC某些驱动实现总是要求255个字节左右的数据), @@ -378,30 +360,9 @@ int uart_receive(const serial_port sp, uint8_t *pbtRx, uint32_t pszMaxRxLen, uin int uart_send(const serial_port sp, const uint8_t *pbtTx, const uint32_t len) { uint32_t pos = 0; - fd_set rfds; - struct timeval tv; - while (pos < len) { - // Reset file descriptor - FD_ZERO(&rfds); - FD_SET(((serial_port_unix *) sp)->fd, &rfds); - tv = timeout; - int res = select(((serial_port_unix *) sp)->fd + 1, NULL, &rfds, NULL, &tv); - - // Write error - if (res < 0) { - printf("UART:: write error (%d)\n", res); - return NFC_EIO; - } - - // Write time-out - if (res == 0) { - printf("UART:: write time-out\n"); - return NFC_ETIMEOUT; - } - // Send away the bytes - res = write(((serial_port_unix *) sp)->fd, pbtTx + pos, len - pos); + int res = write(((serial_port_unix *) sp)->fd, pbtTx + pos, len - pos); // Stop if the OS has some troubles sending the data if (res <= 0) diff --git a/libnfc_pn53x/src/main/cpp/libnfc/include/uart.h b/libnfc_pn53x/src/main/cpp/libnfc/include/uart.h index d94d874b..2a72dde3 100644 --- a/libnfc_pn53x/src/main/cpp/libnfc/include/uart.h +++ b/libnfc_pn53x/src/main/cpp/libnfc/include/uart.h @@ -87,9 +87,5 @@ bool uart_set_speed(serial_port sp, const uint32_t uiPortSpeed); /* Gets the current speed of the serial port, in baud. */ uint32_t uart_get_speed(const serial_port sp); - -/* Reconfigure timeouts - */ -int uart_reconfigure_timeouts(uint32_t value); #endif // _UART_H_ diff --git a/pm3flasher/build.gradle b/pm3flasher/build.gradle index 3fe18b53..340752fd 100644 --- a/pm3flasher/build.gradle +++ b/pm3flasher/build.gradle @@ -41,6 +41,6 @@ android { dependencies { implementation fileTree(dir: 'libs', include: ['*.jar']) implementation project(':communication') - implementation 'com.github.xianglin1998:ComBridge:1.0.3' + implementation 'com.github.xianglin1998:ComBridge:v1.0.4' } diff --git a/pm3rdv4rrg/build.gradle b/pm3rdv4rrg/build.gradle index 6aa0902b..be2e3b99 100644 --- a/pm3rdv4rrg/build.gradle +++ b/pm3rdv4rrg/build.gradle @@ -41,7 +41,7 @@ dependencies { implementation 'com.github.xianglin1998:nfctag:1.0' implementation 'com.github.xianglin1998:nfc_console:1.0' - implementation 'com.github.xianglin1998:ComBridge:1.0.3' + implementation 'com.github.xianglin1998:ComBridge:v1.0.4' implementation project(':communication') implementation project(path: ':utils')