]> git.zerfleddert.de Git - proxmark3-svn/blobdiff - client/comms.c
modify USB communications
[proxmark3-svn] / client / comms.c
index be0cfd103ba610c4b35e225bf28cf96949b739b2..d7f4265f9b76dd6f129ad35bbfce6b07d5659c2d 100644 (file)
 #include "comms.h"
 
 #include <pthread.h>
-#ifdef __linux__
+#include <inttypes.h>
+
+#if defined(__linux__) && !defined(NO_UNLINK)
 #include <unistd.h>            // for unlink()
 #endif
 #include "uart.h"
 #include "ui.h"
 #include "common.h"
+#include "util_darwin.h"
 #include "util_posix.h"
 
 
@@ -44,6 +47,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
@@ -186,6 +190,22 @@ static void UsbCommandReceived(UsbCommand *UC)
 }
 
 
+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) {
+               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);
+               }
+               *received_len += bytes_read;
+               bytes_read = 0;
+       }
+       return (*received_len == len);
+}
+       
+
 static void
 #ifdef __has_attribute
 #if __has_attribute(force_align_arg_pointer)
@@ -194,25 +214,49 @@ __attribute__((force_align_arg_pointer))
 #endif
 *uart_communication(void *targ) {
        communication_arg_t *conn = (communication_arg_t*)targ;
-       size_t rxlen;
-       UsbCommand rx;
-       UsbCommand *prx = &rx;
+       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
+                               // printf("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;
+                                       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.
+                               // printf("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 = &rx;
-
                
                pthread_mutex_lock(&txBufferMutex);
 
@@ -236,6 +280,10 @@ __attribute__((force_align_arg_pointer))
                pthread_mutex_unlock(&txBufferMutex);
        }
 
+#if defined(__MACH__) && defined(__APPLE__)
+       enableAppNap();
+#endif
+
        pthread_exit(NULL);
        return NULL;
 }
@@ -292,6 +340,39 @@ bool GetFromBigBuf(uint8_t *dest, int bytes, int start_index, UsbCommand *respon
 }
 
        
+bool GetFromFpgaRAM(uint8_t *dest, int bytes)
+{
+       UsbCommand c = {CMD_HF_PLOT, {0, 0, 0}};
+       SendCommand(&c);
+
+       uint64_t start_time = msclock();
+
+       UsbCommand response;
+       
+       int bytes_completed = 0;
+       bool show_warning = true;
+       while(true) {
+               if (getCommand(&response)) {
+                       if (response.cmd == CMD_DOWNLOADED_RAW_ADC_SAMPLES_125K) {
+                               int copy_bytes = MIN(bytes - bytes_completed, response.arg[1]);
+                               memcpy(dest + response.arg[0], response.d.asBytes, copy_bytes);
+                               bytes_completed += copy_bytes;
+                       } else if (response.cmd == CMD_ACK) {
+                               return true;
+                       }
+               }
+
+               if (msclock() - start_time > 2000 && show_warning) {
+                       PrintAndLog("Waiting for a response from the proxmark...");
+                       PrintAndLog("You can cancel this operation by pressing the pm3 button");
+                       show_warning = false;
+               }
+       }
+
+       return false;
+}
+
+
 bool OpenProxmark(void *port, bool wait_for_port, int timeout, bool flash_mode) {
        char *portname = (char *)port;
        if (!wait_for_port) {
@@ -333,14 +414,29 @@ bool OpenProxmark(void *port, bool wait_for_port, int timeout, bool flash_mode)
 
 void CloseProxmark(void) {
        conn.run = false;
+
+#ifdef __BIONIC__
+       // In Android O and later, if an invalid pthread_t is passed to pthread_join, it calls fatal().
+       // https://github.com/aosp-mirror/platform_bionic/blob/ed16b344e75f422fb36fbfd91fb30de339475880/libc/bionic/pthread_internal.cpp#L116-L128
+       //
+       // In Bionic libc, pthread_t is an integer.
+
+       if (USB_communication_thread != 0) {
+               pthread_join(USB_communication_thread, NULL);
+       }
+#else
+       // pthread_t is a struct on other libc, treat as an opaque memory reference
        pthread_join(USB_communication_thread, NULL);
+#endif
 
        if (sp) {
                uart_close(sp);
        }
 
-#ifdef __linux__
+#if defined(__linux__) && !defined(NO_UNLINK)
        // Fix for linux, it seems that it is extremely slow to release the serial port file descriptor /dev/*
+       //
+       // This may be disabled at compile-time with -DNO_UNLINK (used for a JNI-based serial port on Android).
        if (serial_port_name) {
                unlink(serial_port_name);
        }
@@ -349,6 +445,9 @@ void CloseProxmark(void) {
        // Clean up our state
        sp = NULL;
        serial_port_name = NULL;
+#ifdef __BIONIC__
+       memset(&USB_communication_thread, 0, sizeof(pthread_t));
+#endif
 }
 
 
Impressum, Datenschutz