diff --git a/armsrc/appmain.c b/armsrc/appmain.c index 7d0adaf06..2ac40c255 100644 --- a/armsrc/appmain.c +++ b/armsrc/appmain.c @@ -1115,7 +1115,19 @@ static void SendCapabilities(void) { #ifdef WITH_SMARTCARD capabilities.compiled_with_smartcard = true; uint8_t maj, min; - capabilities.hw_available_smartcard = I2C_get_version(&maj, &min) == PM3_SUCCESS; + + // The module may still be finishing a card exchange from a previous + // session when this runs, and a single missed answer used to report the + // hardware as absent for the whole of the next one - every smartcard + // command then said it was not available in this mode. + capabilities.hw_available_smartcard = false; + for (uint8_t i = 0; i < 3; i++) { + if (I2C_get_version(&maj, &min) == PM3_SUCCESS) { + capabilities.hw_available_smartcard = true; + break; + } + SpinDelay(50); + } #else capabilities.compiled_with_smartcard = false; capabilities.hw_available_smartcard = false; diff --git a/client/src/comms.c b/client/src/comms.c index a45bf3962..a4052471e 100644 --- a/client/src/comms.c +++ b/client/src/comms.c @@ -138,6 +138,10 @@ void SendCommandOLD(uint64_t cmd, uint64_t arg0, uint64_t arg1, uint64_t arg2, c pthread_mutex_unlock(&txBufferMutex); + // and interrupt its receive, which it would otherwise sit out in full + // before looking at the send buffer at all + uart_wakeup(); + //__atomic_test_and_set(&txcmd_pending, __ATOMIC_SEQ_CST); } @@ -202,6 +206,10 @@ static void SendCommandNG_internal(uint16_t cmd, uint8_t *data, size_t len, bool pthread_mutex_unlock(&txBufferMutex); + // and interrupt its receive, which it would otherwise sit out in full + // before looking at the send buffer at all + uart_wakeup(); + //__atomic_test_and_set(&txcmd_pending, __ATOMIC_SEQ_CST); } diff --git a/client/src/uart/uart.h b/client/src/uart/uart.h index 22aab5556..902fa4a87 100644 --- a/client/src/uart/uart.h +++ b/client/src/uart/uart.h @@ -76,6 +76,13 @@ bool uart_set_speed(serial_port sp, const uint32_t uiPortSpeed); */ uint32_t uart_get_speed(const serial_port sp); +/* Wake a thread blocked in uart_receive() so it can get on and service a + * queued send. The comm thread receives before it transmits, so without this a + * command sits in the buffer until the receive phase times out. + * Safe to call from another thread; a no-op where it is not implemented. + */ +void uart_wakeup(void); + /* Reconfigure timeouts (ms) */ int uart_reconfigure_timeouts(uint32_t value); diff --git a/client/src/uart/uart_posix.c b/client/src/uart/uart_posix.c index b04a4c208..3111eb466 100644 --- a/client/src/uart/uart_posix.c +++ b/client/src/uart/uart_posix.c @@ -36,6 +36,7 @@ #include #include #include +#include #ifdef HAVE_BLUEZ #include @@ -74,6 +75,36 @@ static uint32_t newtimeout_value = 0; static bool newtimeout_pending = false; static uint8_t rx_empty_counter = 0; +// Self pipe used to interrupt the select() in uart_receive(). Created once, on +// first use, and never closed - two descriptors for the life of the process, +// and both ends have to stay valid for whichever thread touches them. +static int wakeup_pipe[2] = { -1, -1 }; +static pthread_once_t wakeup_once = PTHREAD_ONCE_INIT; + +static void uart_wakeup_create(void) { + if (pipe(wakeup_pipe) != 0) { + wakeup_pipe[0] = -1; + wakeup_pipe[1] = -1; + return; + } + // Neither end may ever block: a full pipe already means a wakeup is + // pending, which is all the reader needs to know. + fcntl(wakeup_pipe[0], F_SETFL, O_NONBLOCK); + fcntl(wakeup_pipe[1], F_SETFL, O_NONBLOCK); +} + +void uart_wakeup(void) { + pthread_once(&wakeup_once, uart_wakeup_create); + + if (wakeup_pipe[1] < 0) { + return; + } + + const uint8_t b = 1; + ssize_t res = write(wakeup_pipe[1], &b, 1); + (void)res; // EAGAIN just means a wakeup is already queued +} + int uart_reconfigure_timeouts(uint32_t value) { newtimeout_value = value; newtimeout_pending = true; @@ -616,8 +647,22 @@ int uart_receive(const serial_port sp, uint8_t *pbtRx, uint32_t pszMaxRxLen, uin // Reset file descriptor FD_ZERO(&rfds); FD_SET(spu->fd, &rfds); + int maxfd = spu->fd; + + // Only watch the wakeup while no part of a frame is in hand. Cutting a + // frame short would hand the caller a partial packet, and leaving the + // pipe readable while mid frame would spin. + pthread_once(&wakeup_once, uart_wakeup_create); + bool watch_wakeup = ((*pszRxLen == 0) && (wakeup_pipe[0] >= 0)); + if (watch_wakeup) { + FD_SET(wakeup_pipe[0], &rfds); + if (wakeup_pipe[0] > maxfd) { + maxfd = wakeup_pipe[0]; + } + } + tv = timeout; - res = select(spu->fd + 1, &rfds, NULL, NULL, &tv); + res = select(maxfd + 1, &rfds, NULL, NULL, &tv); // Read error if (res < 0) { @@ -635,6 +680,19 @@ int uart_receive(const serial_port sp, uint8_t *pbtRx, uint32_t pszMaxRxLen, uin } } + if (watch_wakeup && FD_ISSET(wakeup_pipe[0], &rfds)) { + + uint8_t drain[64]; + while (read(wakeup_pipe[0], drain, sizeof(drain)) > 0) { }; + + // Nothing on the wire, only a send waiting. Return before the + // FIONREAD below, which counts empty reads towards declaring the + // device gone. + if (FD_ISSET(spu->fd, &rfds) == false) { + return PM3_ENODATA; + } + } + // Retrieve the count of the incoming bytes res = ioctl(spu->fd, FIONREAD, &byteCount); // PrintAndLogEx(ERR, "UART:: RX ioctl res %d byteCount %u", res, byteCount); diff --git a/client/src/uart/uart_win32.c b/client/src/uart/uart_win32.c index 75bd156c7..7613f785f 100644 --- a/client/src/uart/uart_win32.c +++ b/client/src/uart/uart_win32.c @@ -51,6 +51,10 @@ uint32_t newtimeout_value = 0; bool newtimeout_pending = false; uint8_t rx_empty_counter = 0; +// Not implemented here: the win32 receive path would need an event object in +// its wait instead of a pipe. Leaving it a no-op keeps that platform as it was. +void uart_wakeup(void) { } + int uart_reconfigure_timeouts(uint32_t value) { newtimeout_value = value; newtimeout_pending = true;