added a wake up pipeline to see if sim module actually is doing things or not. Might not work well on Windows machines... proxspace I am looking at you. but in short this would make comms between device and client faster
This commit is contained in:
1 parent
98c5897bbe
commit
5c38ce0a1d
5 files changed
+91
-2
No files matched your search
+13
-1
@@ -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;
|
||||
|
||||
@@ -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);
|
||||
}
|
||||
|
||||
|
||||
@@ -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);
|
||||
|
||||
@@ -36,6 +36,7 @@
|
||||
#include <sys/socket.h>
|
||||
#include <sys/un.h>
|
||||
#include <errno.h>
|
||||
#include <pthread.h>
|
||||
|
||||
#ifdef HAVE_BLUEZ
|
||||
#include <bluetooth/bluetooth.h>
|
||||
@@ -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);
|
||||
|
||||
@@ -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;
|
||||
|
||||
Reference in new issue
Block a user