diff --git a/client/comms.c b/client/comms.c index 2cb397007..ab39cfb6b 100644 --- a/client/comms.c +++ b/client/comms.c @@ -577,7 +577,6 @@ bool OpenProxmark(void *port, bool wait_for_port, int timeout, bool flash_mode, printf("."); fflush(stdout); } while (++openCount < timeout && (sp == INVALID_SERIAL_PORT || sp == CLAIMED_SERIAL_PORT)); - //PrintAndLogEx(NORMAL, "\n"); } // check result of uart opening @@ -641,8 +640,15 @@ int TestProxmark(void) { conn.send_via_fpc = pm3_capabilities.via_fpc; conn.uart_speed = pm3_capabilities.baudrate; PrintAndLogEx(INFO, "Communicating with PM3 over %s", conn.send_via_fpc ? _YELLOW_("FPC UART") : _YELLOW_("USB-CDC")); - if (conn.send_via_fpc) + if (conn.send_via_fpc) { PrintAndLogEx(INFO, "UART Serial baudrate: " _YELLOW_("%u") "\n", conn.uart_speed); + } + + // reconfigure. + if ( conn.send_via_fpc == false ) { + uart_reconfigure_timeouts(sp, UART_USB_CLIENT_RX_TIMEOUT_MS ); + } + return PM3_SUCCESS; } else { return PM3_ETIMEOUT; diff --git a/include/usb_cmd.h b/include/usb_cmd.h index f40a71dbc..4d4a9e8dd 100644 --- a/include/usb_cmd.h +++ b/include/usb_cmd.h @@ -440,6 +440,21 @@ extern capabilities_t pm3_capabilities; #define PM3_EFATAL -99 +// Receiving from USART need more than 30ms as we used on USB +// else we get errors about partial packet reception +// FTDI 9600 hw status -> we need 20ms +// FTDI 115200 hw status -> we need 50ms +// FTDI 460800 hw status -> we need 30ms +// BT 115200 hf mf fchk 1 dic -> we need 140ms +// all zero's configure: no timeout for read/write used. +// took settings from libnfc/buses/uart.c + +// uart_windows.c +# define UART_FPC_CLIENT_RX_TIMEOUT_MS 170 +# define UART_USB_CLIENT_RX_TIMEOUT_MS 20 +# define UART_TCP_CLIENT_RX_TIMEOUT_MS 300 + + // CMD_DEVICE_INFO response packet has flags in arg[0], flag definitions: /* Whether a bootloader that understands the common_area is present */ #define DEVICE_INFO_FLAG_BOOTROM_PRESENT (1<<0) diff --git a/uart/uart.h b/uart/uart.h index f97327953..28be233f0 100644 --- a/uart/uart.h +++ b/uart/uart.h @@ -105,5 +105,8 @@ bool uart_set_speed(serial_port sp, const uint32_t uiPortSpeed); */ uint32_t uart_get_speed(const serial_port sp); +/* Reconfigure timeouts + */ + bool uart_reconfigure_timeouts(serial_port *sp, uint32_t value ); #endif // _UART_H_ diff --git a/uart/uart_posix.c b/uart/uart_posix.c index 30a9ba098..67802645a 100644 --- a/uart/uart_posix.c +++ b/uart/uart_posix.c @@ -67,17 +67,16 @@ typedef struct { term_info tiNew; // Terminal info during the transaction } serial_port_unix; -// Receiving from USART need more than 30ms as we used on USB -// else we get errors about partial packet reception -// FTDI 9600 hw status -> we need 20ms -// FTDI 115200 hw status -> we need 50ms -// FTDI 460800 hw status -> we need 30ms -// BT 115200 hf mf fchk 1 dic -> we need 140ms +// see usb_cmd.h struct timeval timeout = { - .tv_sec = 0, // 0 second - .tv_usec = 200000 // 200 000 micro seconds + .tv_sec = 0, // 0 second + .tv_usec = UART_FPC_CLIENT_RX_TIMEOUT_US }; +bool uart_reconfigure_timeouts(serial_port *sp, uint32_t value ) { + timeout.usec = value * 1000; +} + 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; @@ -92,7 +91,7 @@ serial_port uart_open(const char *pcPortName, uint32_t speed) { return INVALID_SERIAL_PORT; } - timeout.tv_usec = 300000; // 300 000 micro seconds + timeout.tv_usec = UART_TCP_CLIENT_RX_TIMEOUT_US; char *colon = strrchr(addrstr, ':'); const char *portstr; diff --git a/uart/uart_win32.c b/uart/uart_win32.c index a3ac22ebe..6baa5eda4 100644 --- a/uart/uart_win32.c +++ b/uart/uart_win32.c @@ -48,6 +48,24 @@ typedef struct { COMMTIMEOUTS ct; // Serial port time-out configuration } serial_port_windows; +bool uart_reconfigure_timeouts(serial_port *sp, uint32_t value) { + + serial_port_windows *spw = (serial_port_windows*)sp; + spw->ct.ReadIntervalTimeout = value; + spw->ct.ReadTotalTimeoutMultiplier = 0; + spw->ct.ReadTotalTimeoutConstant = value; + spw->ct.WriteTotalTimeoutMultiplier = value; + spw->ct.WriteTotalTimeoutConstant = 0; + + if (!SetCommTimeouts(spw->hPort, &spw->ct)) { + uart_close(spw); + printf("[!] UART error while setting comm time outs\n"); + return INVALID_SERIAL_PORT; + } + + PurgeComm(spw->hPort, PURGE_RXABORT | PURGE_RXCLEAR); +} + serial_port uart_open(const char *pcPortName, uint32_t speed) { char acPortName[255] = {0}; serial_port_windows *sp = calloc(sizeof(serial_port_windows), sizeof(uint8_t)); @@ -84,30 +102,8 @@ serial_port uart_open(const char *pcPortName, uint32_t speed) { printf("[!] UART error while setting com state\n"); return INVALID_SERIAL_PORT; } - // all zero's configure: no timeout for read/write used. - // took settings from libnfc/buses/uart.c -#ifdef WITH_FPC - // Still relevant? - sp->ct.ReadIntervalTimeout = 150; //200; - sp->ct.ReadTotalTimeoutMultiplier = 0; - sp->ct.ReadTotalTimeoutConstant = 150; //200; - sp->ct.WriteTotalTimeoutMultiplier = 150; //200; - sp->ct.WriteTotalTimeoutConstant = 0; -#else - sp->ct.ReadIntervalTimeout = 30; - sp->ct.ReadTotalTimeoutMultiplier = 0; - sp->ct.ReadTotalTimeoutConstant = 30; - sp->ct.WriteTotalTimeoutMultiplier = 30; - sp->ct.WriteTotalTimeoutConstant = 0; -#endif - if (!SetCommTimeouts(sp->hPort, &sp->ct)) { - uart_close(sp); - printf("[!] UART error while setting comm time outs\n"); - return INVALID_SERIAL_PORT; - } - - PurgeComm(sp->hPort, PURGE_RXABORT | PURGE_RXCLEAR); + uart_reconfigure_timeouts(sp, UART_FPC_CLIENT_RX_TIMEOUT_MS); if (!uart_set_speed(sp, speed)) { // trying some fallbacks automatically @@ -154,6 +150,7 @@ bool uart_set_speed(serial_port sp, const uint32_t uiPortSpeed) { PurgeComm(spw->hPort, PURGE_RXABORT | PURGE_RXCLEAR); if (result) conn.uart_speed = uiPortSpeed; + return result; }