]> git.zerfleddert.de Git - proxmark3-svn/blobdiff - client/comms.c
Merge pull request #926 from pwpiwi/fix_iso15693_fpga
[proxmark3-svn] / client / comms.c
index c8d72b8a5cf036eb733231716fbb71324fc3de0e..1391641d569ce9a3183933378e3983c3268ba657 100644 (file)
@@ -9,6 +9,8 @@
 // Code for communicating with the proxmark3 hardware.
 //-----------------------------------------------------------------------------
 
+#define _POSIX_C_SOURCE 199309L // need clock_gettime()
+
 #include "comms.h"
 
 #include <stdio.h>
@@ -17,6 +19,7 @@
 #include <pthread.h>
 #include <inttypes.h>
 #include <time.h>
+#include <sys/time.h>
 
 #include "uart.h"
 #include "ui.h"
@@ -48,6 +51,8 @@ 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
+#define CMD_BUFFER_CHECK_TIME 10 // maximum time (in ms) to wait in getCommand()
+
 static UsbCommand rxBuffer[CMD_BUFFER_SIZE];
 
 // Points to the next empty position to write to
@@ -316,7 +321,6 @@ __attribute__((force_align_arg_pointer))
 bool GetFromBigBuf(uint8_t *dest, int bytes, int start_index, UsbCommand *response, size_t ms_timeout, bool show_warning) {
 
        uint64_t start_time = msclock();
-       uint32_t poll_time = 100; // loop every 100ms
 
        UsbCommand c = {CMD_DOWNLOAD_RAW_ADC_SAMPLES_125K, {start_index, bytes, 0}};
        SendCommand(&c);
@@ -337,7 +341,7 @@ bool GetFromBigBuf(uint8_t *dest, int bytes, int start_index, UsbCommand *respon
                        PrintAndLog("You can cancel this operation by pressing the pm3 button");
                        show_warning = false;
                }
-               if (getCommand(response, poll_time)) {
+               if (getCommand(response, CMD_BUFFER_CHECK_TIME)) {
                        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);
@@ -355,7 +359,6 @@ bool GetFromBigBuf(uint8_t *dest, int bytes, int start_index, UsbCommand *respon
 bool GetFromFpgaRAM(uint8_t *dest, int bytes) {
 
        uint64_t start_time = msclock();
-       uint32_t poll_time = 100; // loop every 100ms
 
        UsbCommand c = {CMD_HF_PLOT, {0, 0, 0}};
        SendCommand(&c);
@@ -370,7 +373,7 @@ bool GetFromFpgaRAM(uint8_t *dest, int bytes) {
                        PrintAndLog("You can cancel this operation by pressing the pm3 button");
                        show_warning = false;
                }
-               if (getCommand(&response, poll_time)) {
+               if (getCommand(&response, CMD_BUFFER_CHECK_TIME)) {
                        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);
@@ -472,7 +475,6 @@ bool WaitForResponseTimeoutW(uint32_t cmd, UsbCommand* response, size_t ms_timeo
        #endif
 
        uint64_t start_time = msclock();
-       uint64_t end_time = start_time + ms_timeout;
 
        if (response == NULL) {
                response = &resp;
@@ -480,8 +482,7 @@ bool WaitForResponseTimeoutW(uint32_t cmd, UsbCommand* response, size_t ms_timeo
 
        // Wait until the command is received
        while (true) {
-               int32_t remaining_time = end_time - msclock();
-               if (remaining_time <= 0) {
+               if (ms_timeout != -1 && msclock() > start_time + ms_timeout) {
                        break; // timeout
                }
                if (msclock() - start_time > 2000 && show_warning) {
@@ -490,7 +491,7 @@ bool WaitForResponseTimeoutW(uint32_t cmd, UsbCommand* response, size_t ms_timeo
                        PrintAndLog("You can cancel this operation by pressing the pm3 button");
                        show_warning = false;
                }
-               if (getCommand(response, remaining_time)) {
+               if (getCommand(response, CMD_BUFFER_CHECK_TIME)) {
                        if (cmd == CMD_UNKNOWN || response->cmd == cmd) {
                                return true;
                        }
Impressum, Datenschutz