From a49ae2d0ceb4ab0b302e6b8e1c728c6d8386b77d Mon Sep 17 00:00:00 2001 From: iceman1001 Date: Fri, 3 May 2019 21:33:00 +0200 Subject: [PATCH] FIX: slow reconfigure on mingw of serial port --- client/comms.c | 23 ++++++++++++++++++++--- client/proxmark3.c | 1 + uart/uart_win32.c | 2 -- 3 files changed, 21 insertions(+), 5 deletions(-) diff --git a/client/comms.c b/client/comms.c index 1497d4d7a..7a0d9db8c 100644 --- a/client/comms.c +++ b/client/comms.c @@ -49,6 +49,8 @@ static int cmd_tail = 0; // to lock rxBuffer operations from different threads static pthread_mutex_t rxBufferMutex = PTHREAD_MUTEX_INITIALIZER; +// serial port access from different threads +static pthread_mutex_t spMutex = PTHREAD_MUTEX_INITIALIZER; // Global start time for WaitForResponseTimeout & dl_it, so we can reset timeout when we get packets // as sending lot of these packets can slow down things wuite a lot on slow links (e.g. hw status or lf read at 9600) @@ -352,6 +354,10 @@ __attribute__((force_align_arg_pointer)) rxlen = 0; bool ACK_received = false; bool error = false; + + + pthread_mutex_lock(&spMutex); + if (uart_receive(sp, (uint8_t *)&rx_raw.pre, sizeof(PacketResponseNGPreamble), &rxlen) && (rxlen == sizeof(PacketResponseNGPreamble))) { rx.magic = rx_raw.pre.magic; uint16_t length = rx_raw.pre.length; @@ -459,6 +465,9 @@ __attribute__((force_align_arg_pointer)) error = true; } } + + pthread_mutex_unlock(&spMutex); + // TODO if error, shall we resync ? pthread_mutex_lock(&txBufferMutex); @@ -592,12 +601,20 @@ int TestProxmark(void) { 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); - } + + pthread_mutex_lock(&spMutex); + int res = uart_reconfigure_timeouts(sp, UART_USB_CLIENT_RX_TIMEOUT_MS); + pthread_mutex_unlock(&spMutex); + if ( res != PM3_SUCCESS ) { + PrintAndLogEx(ERR, "UART reconfigure failed"); + return res; + } + + } return PM3_SUCCESS; } else { return PM3_ETIMEOUT; diff --git a/client/proxmark3.c b/client/proxmark3.c index 1dde3d61e..58b6a5325 100644 --- a/client/proxmark3.c +++ b/client/proxmark3.c @@ -496,6 +496,7 @@ int main(int argc, char *argv[]) { CloseProxmark(); session.pm3_present = false; } + if (!session.pm3_present) PrintAndLogEx(INFO, "Running in " _YELLOW_("OFFLINE") "mode. Check \"%s -h\" if it's not what you want.\n", exec_name); diff --git a/uart/uart_win32.c b/uart/uart_win32.c index 9939eee1c..b5f7b9302 100644 --- a/uart/uart_win32.c +++ b/uart/uart_win32.c @@ -49,7 +49,6 @@ typedef struct { } serial_port_windows; int uart_reconfigure_timeouts(serial_port sp, uint32_t value) { - serial_port_windows *spw; spw = (serial_port_windows *)sp; spw->ct.ReadIntervalTimeout = value; @@ -60,7 +59,6 @@ int uart_reconfigure_timeouts(serial_port sp, uint32_t value) { if (!SetCommTimeouts(spw->hPort, &spw->ct)) { uart_close(spw); - printf("[!] UART error while setting comm time outs\n"); return PM3_EIO; }