X-Git-Url: http://git.zerfleddert.de/cgi-bin/gitweb.cgi/proxmark3-svn/blobdiff_plain/fc52fbd42f576d889826f4a0c60d18fad41bc3af..ac37ee816b0f00a5d02d9a860dc6180f71132675:/client/comms.c diff --git a/client/comms.c b/client/comms.c index 5af53715..5bb7e69e 100644 --- a/client/comms.c +++ b/client/comms.c @@ -11,9 +11,14 @@ #include "comms.h" +#include +#include +#include #include +#include + #if defined(__linux__) && !defined(NO_UNLINK) -#include // for unlink() +#include // for unlink() #endif #include "uart.h" #include "ui.h" @@ -31,7 +36,6 @@ static bool offline; typedef struct { bool run; // If TRUE, continue running the uart_communication thread - bool block_after_ACK; // if true, block after receiving an ACK package } communication_arg_t; static communication_arg_t conn; @@ -45,6 +49,7 @@ static pthread_cond_t txBufferSig = PTHREAD_COND_INITIALIZER; // Used by UsbReceiveCommand as a ring buffer for messages that are yet to be // processed by a command handler (WaitForResponse{,Timeout}) +#define CMD_BUFFER_SIZE 50 static UsbCommand rxBuffer[CMD_BUFFER_SIZE]; // Points to the next empty position to write to @@ -75,11 +80,11 @@ void SendCommand(UsbCommand *c) { if (offline) { PrintAndLog("Sending bytes to proxmark failed - offline"); return; - } + } pthread_mutex_lock(&txBufferMutex); /** - This causes hangups at times, when the pm3 unit is unresponsive or disconnected. The main console thread is alive, + This causes hangups at times, when the pm3 unit is unresponsive or disconnected. The main console thread is alive, but comm thread just spins here. Not good.../holiman **/ while (txBuffer_pending) { @@ -180,64 +185,99 @@ static void UsbCommandReceived(UsbCommand *UC) } break; default: - storeCommand(UC); + storeCommand(UC); break; } } +static bool receive_from_serial(serial_port sp, uint8_t *rx_buf, size_t len, size_t *received_len) { + size_t bytes_read = 0; + *received_len = 0; + // we eventually need to call uart_receive several times if it times out in the middle of a transfer + while (uart_receive(sp, rx_buf + *received_len, len - *received_len, &bytes_read) && bytes_read && *received_len < len) { + #ifdef COMMS_DEBUG + if (bytes_read != len - *received_len) { + printf("uart_receive() returned true but not enough bytes could be received. received: %d, wanted to receive: %d, already received before: %d\n", + bytes_read, len - *received_len, *received_len); + } + #endif + *received_len += bytes_read; + bytes_read = 0; + } + return (*received_len == len); +} + + static void #ifdef __has_attribute #if __has_attribute(force_align_arg_pointer) -__attribute__((force_align_arg_pointer)) +__attribute__((force_align_arg_pointer)) #endif #endif *uart_communication(void *targ) { communication_arg_t *conn = (communication_arg_t*)targ; - size_t rxlen; - UsbCommand rx; - UsbCommand *prx = ℞ + uint8_t rx[sizeof(UsbCommand)]; + size_t rxlen = 0; + uint8_t *prx = rx; + UsbCommand *command = (UsbCommand*)rx; + UsbResponse *response = (UsbResponse*)rx; #if defined(__MACH__) && defined(__APPLE__) disableAppNap("Proxmark3 polling UART"); #endif while (conn->run) { - rxlen = 0; bool ACK_received = false; - if (uart_receive(sp, (uint8_t *)prx, sizeof(UsbCommand) - (prx-&rx), &rxlen) && rxlen) { + prx = rx; + size_t bytes_to_read = offsetof(UsbResponse, d); // the fixed part of a new style UsbResponse. Otherwise this will be cmd and arg[0] (64 bit each) + if (receive_from_serial(sp, prx, bytes_to_read, &rxlen)) { prx += rxlen; - if (prx-&rx < sizeof(UsbCommand)) { - continue; - } - UsbCommandReceived(&rx); - if (rx.cmd == CMD_ACK) { - ACK_received = true; + if (response->cmd & CMD_VARIABLE_SIZE_FLAG) { // new style response with variable size + // PrintAndLog("received new style response %04" PRIx16 ", datalen = %d, arg[0] = %08" PRIx32 ", arg[1] = %08" PRIx32 ", arg[2] = %08" PRIx32 "\n", + // response->cmd, response->datalen, response->arg[0], response->arg[1], response->arg[2]); + bytes_to_read = response->datalen; + if (receive_from_serial(sp, prx, bytes_to_read, &rxlen)) { + UsbCommand resp; + resp.cmd = response->cmd & ~CMD_VARIABLE_SIZE_FLAG; // remove the flag + resp.arg[0] = response->arg[0]; + resp.arg[1] = response->arg[1]; + resp.arg[2] = response->arg[2]; + memcpy(&resp.d.asBytes, &response->d.asBytes, response->datalen); + UsbCommandReceived(&resp); + if (resp.cmd == CMD_ACK) { + ACK_received = true; + } + } + } else { // old style response uses same data structure as commands. Fixed size. + // PrintAndLog("received old style response %016" PRIx64 ", arg[0] = %016" PRIx64 "\n", command->cmd, command->arg[0]); + bytes_to_read = sizeof(UsbCommand) - bytes_to_read; + if (receive_from_serial(sp, prx, bytes_to_read, &rxlen)) { + UsbCommandReceived(command); + if (command->cmd == CMD_ACK) { + ACK_received = true; + } + } } } - prx = ℞ - pthread_mutex_lock(&txBufferMutex); - - if (conn->block_after_ACK) { - // if we just received an ACK, wait here until a new command is to be transmitted - if (ACK_received) { - while (!txBuffer_pending) { - pthread_cond_wait(&txBufferSig, &txBufferMutex); - } + // if we received an ACK the PM has done its job and waits for another command. + // We therefore can wait here as well until a new command is to be transmitted. + // The advantage is that the next command will be transmitted immediately without the need to wait for a receive timeout + if (ACK_received) { + while (!txBuffer_pending) { + pthread_cond_wait(&txBufferSig, &txBufferMutex); } } - - if(txBuffer_pending) { + if (txBuffer_pending) { if (!uart_send(sp, (uint8_t*) &txBuffer, sizeof(UsbCommand))) { PrintAndLog("Sending bytes to proxmark failed"); } txBuffer_pending = false; - pthread_cond_signal(&txBufferSig); // tell main thread that txBuffer is empty } - + pthread_cond_signal(&txBufferSig); // tell main thread that txBuffer is empty pthread_mutex_unlock(&txBufferMutex); } @@ -270,7 +310,7 @@ bool GetFromBigBuf(uint8_t *dest, int bytes, int start_index, UsbCommand *respon uint64_t start_time = msclock(); UsbCommand resp; - if (response == NULL) { + if (response == NULL) { response = &resp; } @@ -300,7 +340,7 @@ bool GetFromBigBuf(uint8_t *dest, int bytes, int start_index, UsbCommand *respon return false; } - + bool GetFromFpgaRAM(uint8_t *dest, int bytes) { UsbCommand c = {CMD_HF_PLOT, {0, 0, 0}}; @@ -309,7 +349,7 @@ bool GetFromFpgaRAM(uint8_t *dest, int bytes) uint64_t start_time = msclock(); UsbCommand response; - + int bytes_completed = 0; bool show_warning = true; while(true) { @@ -334,7 +374,7 @@ bool GetFromFpgaRAM(uint8_t *dest, int bytes) } -bool OpenProxmark(void *port, bool wait_for_port, int timeout, bool flash_mode) { +bool OpenProxmark(void *port, bool wait_for_port, int timeout) { char *portname = (char *)port; if (!wait_for_port) { sp = uart_open(portname); @@ -366,7 +406,6 @@ bool OpenProxmark(void *port, bool wait_for_port, int timeout, bool flash_mode) // start the USB communication thread serial_port_name = portname; conn.run = true; - conn.block_after_ACK = flash_mode; pthread_create(&USB_communication_thread, NULL, &uart_communication, &conn); return true; } @@ -438,7 +477,7 @@ bool WaitForResponseTimeoutW(uint32_t cmd, UsbCommand* response, size_t ms_timeo // Wait until the command is received while (true) { - while(getCommand(response)) { + while (getCommand(response)) { if (cmd == CMD_UNKNOWN || response->cmd == cmd) { return true; }