]> cvs.zerfleddert.de Git - proxmark3-svn/commitdiff
lean up event model so that this will work under OS X (and hopefully Linux)
authorbushing <bushing@ef4ab9da-24cd-11de-8aaa-f3a34680c41f>
Tue, 22 Dec 2009 12:46:04 +0000 (12:46 +0000)
committerbushing <bushing@ef4ab9da-24cd-11de-8aaa-f3a34680c41f>
Tue, 22 Dec 2009 12:46:04 +0000 (12:46 +0000)
still need to refactor some stuff -- lots of code duplication here that
we can get rid of

client/command.c
client/flasher.c
client/prox.c
client/prox.h
client/usb.c

index 47fe6a25eeb064f063d1d4b3b42e67c468d5ebc6..3370748ef198457a8e6dd8ddf0ecff2de86f30bd 100644 (file)
@@ -3,74 +3,76 @@
 // Jonathan Westhues, Sept 2005\r
 // Edits by Gerhard de Koning Gans, Sep 2007 (##)\r
 //-----------------------------------------------------------------------------\r
-#ifdef _WIN32\r
+#ifdef WIN32\r
 #include <windows.h>\r
 #endif\r
+#include <unistd.h>\r
 #include <stdlib.h>\r
 #include <string.h>\r
 #include <stdio.h>\r
 #include <limits.h>\r
 #include <math.h>\r
-#ifdef _MSC_VER\r
-typedef DWORD uint32_t;\r
-typedef BYTE uint8_t;\r
-typedef WORD uint16_t;\r
-#define bool BOOL\r
-#define true TRUE\r
-#define false FALSE\r
-#else\r
-#include <stdint.h>\r
-#include <stdbool.h>\r
-#endif\r
 \r
 #include "prox.h"\r
 #include "../common/iso14443_crc.c"\r
 #include "../common/crc16.c"\r
+#include "../include/usb_cmd.h"\r
 \r
 #define arraylen(x) (sizeof(x)/sizeof((x)[0]))\r
 #define BIT(x) GraphBuffer[x * clock]\r
 #define BITS (GraphTraceLen / clock)\r
+#define SAMPLE_BUFFER_SIZE 64 // XXX check this\r
 \r
 int go = 0;\r
 static int CmdHisamplest(char *str, int nrlow);\r
+unsigned int current_command = CMD_UNKNOWN;\r
+unsigned int received_command = CMD_UNKNOWN;\r
+static uint8_t sample_buf[SAMPLE_BUFFER_SIZE];\r
+\r
+void wait_for_response(uint32_t response_type)\r
+{\r
+       while (received_command != response_type) {\r
+#ifdef WIN32\r
+               UsbCommand c;\r
+               if (ReceiveCommandPoll(&c))\r
+                       UsbCommandReceived(&c);\r
+               Sleep(0);\r
+#else\r
+               usleep(10000); // XXX ugh\r
+#endif\r
+       }\r
+       received_command = CMD_UNKNOWN;\r
+}\r
 \r
 static void GetFromBigBuf(uint8_t *dest, int bytes)\r
 {\r
        int n = bytes/4;\r
-       int i;\r
 \r
        if(n % 48 != 0) {\r
                PrintToScrollback("bad len in GetFromBigBuf");\r
                return;\r
        }\r
 \r
+       int i;\r
        for(i = 0; i < n; i += 12) {\r
-               UsbCommand c;\r
-               c.cmd = CMD_DOWNLOAD_RAW_ADC_SAMPLES_125K;\r
-               c.arg[0] = i;\r
-               SendCommand(&c, false);\r
-               ReceiveCommand(&c);\r
-               if(c.cmd != CMD_DOWNLOADED_RAW_ADC_SAMPLES_125K) {\r
-                       PrintToScrollback("bad resp");\r
-                       return;\r
-               }\r
+               UsbCommand c = {CMD_DOWNLOAD_RAW_ADC_SAMPLES_125K, {i, 0, 0}};\r
+               SendCommand(&c);\r
+               wait_for_response(CMD_DOWNLOADED_RAW_ADC_SAMPLES_125K);\r
 \r
-               memcpy(dest+(i*4), c.d.asBytes, 48);\r
+               memcpy(dest+(i*4), sample_buf, 48);\r
        }\r
 }\r
 \r
 static void CmdReset(char *str)\r
 {\r
-       UsbCommand c;\r
-       c.cmd = CMD_HARDWARE_RESET;\r
-       SendCommand(&c, false);\r
+       UsbCommand c = {CMD_HARDWARE_RESET};\r
+       SendCommand(&c);\r
 }\r
 \r
 static void CmdBuffClear(char *str)\r
 {\r
-       UsbCommand c;\r
-       c.cmd = CMD_BUFF_CLEAR;\r
-       SendCommand(&c, false);\r
+       UsbCommand c = {CMD_BUFF_CLEAR};\r
+       SendCommand(&c);\r
        CmdClearGraph(true);\r
 }\r
 \r
@@ -81,44 +83,37 @@ static void CmdQuit(char *str)
 \r
 static void CmdHIDdemodFSK(char *str)\r
 {\r
-       UsbCommand c;\r
-       c.cmd = CMD_HID_DEMOD_FSK;\r
-       SendCommand(&c, false);\r
+       UsbCommand c={CMD_HID_DEMOD_FSK};\r
+       SendCommand(&c);\r
 }\r
 \r
 static void CmdTune(char *str)\r
 {\r
-       UsbCommand c;\r
-       c.cmd = CMD_MEASURE_ANTENNA_TUNING;\r
-       SendCommand(&c, false);\r
+       UsbCommand c={CMD_MEASURE_ANTENNA_TUNING};\r
+       SendCommand(&c);\r
 }\r
 \r
 static void CmdHi15read(char *str)\r
 {\r
-       UsbCommand c;\r
-       c.cmd = CMD_ACQUIRE_RAW_ADC_SAMPLES_ISO_15693;\r
-       SendCommand(&c, false);\r
+       UsbCommand c={CMD_ACQUIRE_RAW_ADC_SAMPLES_ISO_15693};\r
+       SendCommand(&c);\r
 }\r
 \r
 static void CmdHi14read(char *str)\r
 {\r
-       UsbCommand c;\r
-       c.cmd = CMD_ACQUIRE_RAW_ADC_SAMPLES_ISO_14443;\r
-       c.arg[0] = atoi(str);\r
-       SendCommand(&c, false);\r
+       UsbCommand c = {CMD_ACQUIRE_RAW_ADC_SAMPLES_ISO_14443, {strtol(str, NULL, 0), 0, 0}};\r
+       SendCommand(&c);\r
 }\r
 \r
 \r
 /* New command to read the contents of a SRI512 tag\r
  * SRI512 tags are ISO14443-B modulated memory tags,\r
- * this command just dumps the contents of the memory/\r
+ * this command just dumps the contents of the memory\r
  */\r
 static void CmdSri512read(char *str)\r
 {\r
-       UsbCommand c;\r
-       c.cmd = CMD_READ_SRI512_TAG;\r
-       c.arg[0] = atoi(str);\r
-       SendCommand(&c, false);\r
+       UsbCommand c={CMD_READ_SRI512_TAG, {strtol(str, NULL, 0), 0, 0}};\r
+       SendCommand(&c);\r
 }\r
 \r
 /* New command to read the contents of a SRIX4K tag\r
@@ -127,77 +122,56 @@ static void CmdSri512read(char *str)
  */\r
 static void CmdSrix4kread(char *str)\r
 {\r
-        UsbCommand c;\r
-        c.cmd = CMD_READ_SRIX4K_TAG;\r
-        c.arg[0] = atoi(str);\r
-        SendCommand(&c, false);\r
+       UsbCommand c={CMD_READ_SRIX4K_TAG, {strtol(str, NULL, 0), 0, 0}};\r
+        SendCommand(&c);\r
 }\r
 \r
-\r
-\r
-// ## New command\r
 static void CmdHi14areader(char *str)\r
 {\r
-       UsbCommand c;\r
-       c.cmd = CMD_READER_ISO_14443a;\r
-       c.arg[0] = atoi(str);\r
-       SendCommand(&c, false);\r
+       UsbCommand c={CMD_READER_ISO_14443a, {strtol(str, NULL, 0), 0, 0}};\r
+       SendCommand(&c);\r
 }\r
 \r
-// ## New command\r
 static void CmdHi15reader(char *str)\r
 {\r
-       UsbCommand c;\r
-       c.cmd = CMD_READER_ISO_15693;\r
-       c.arg[0] = atoi(str);\r
-       SendCommand(&c, false);\r
+       UsbCommand c={CMD_READER_ISO_15693, {strtol(str, NULL, 0), 0, 0}};\r
+       SendCommand(&c);\r
 }\r
 \r
-// ## New command\r
 static void CmdHi15tag(char *str)\r
 {\r
-       UsbCommand c;\r
-       c.cmd = CMD_SIMTAG_ISO_15693;\r
-       c.arg[0] = atoi(str);\r
-       SendCommand(&c, false);\r
+       UsbCommand c={CMD_SIMTAG_ISO_15693, {strtol(str, NULL, 0), 0, 0}};\r
+       SendCommand(&c);\r
 }\r
 \r
 static void CmdHi14read_sim(char *str)\r
 {\r
-       UsbCommand c;\r
-       c.cmd = CMD_ACQUIRE_RAW_ADC_SAMPLES_ISO_14443_SIM;\r
-       c.arg[0] = atoi(str);\r
-       SendCommand(&c, false);\r
+       UsbCommand c={CMD_ACQUIRE_RAW_ADC_SAMPLES_ISO_14443_SIM, {strtol(str, NULL, 0), 0, 0}};\r
+       SendCommand(&c);\r
 }\r
 \r
 static void CmdHi14readt(char *str)\r
 {\r
-       UsbCommand c;\r
-       c.cmd = CMD_ACQUIRE_RAW_ADC_SAMPLES_ISO_14443;\r
-       c.arg[0] = atoi(str);\r
-       SendCommand(&c, false);\r
+       UsbCommand c={CMD_ACQUIRE_RAW_ADC_SAMPLES_ISO_14443, {strtol(str, NULL, 0), 0, 0}};\r
+       SendCommand(&c);\r
 \r
        //CmdHisamplest(str);\r
-       while(CmdHisamplest(str,atoi(str))==0) {\r
-               c.cmd = CMD_ACQUIRE_RAW_ADC_SAMPLES_ISO_14443;\r
-               c.arg[0] = atoi(str);\r
-               SendCommand(&c, false);\r
+       while(CmdHisamplest(str,strtol(str, NULL, 0))==0) {\r
+               SendCommand(&c);\r
        }\r
        RepaintGraphWindow();\r
 }\r
 \r
 static void CmdHisimlisten(char *str)\r
 {\r
-       UsbCommand c;\r
-       c.cmd = CMD_SIMULATE_TAG_HF_LISTEN;\r
-       SendCommand(&c, false);\r
+       UsbCommand c={CMD_SIMULATE_TAG_HF_LISTEN};\r
+       SendCommand(&c);\r
 }\r
 \r
 static void CmdHi14sim(char *str)\r
 {\r
-       UsbCommand c;\r
-       c.cmd = CMD_SIMULATE_TAG_ISO_14443;\r
-       SendCommand(&c, false);\r
+       UsbCommand c={CMD_SIMULATE_TAG_ISO_14443};\r
+       SendCommand(&c);\r
 }\r
 \r
 static void CmdHi14asim(char *str)     // ## simulate iso14443a tag\r
@@ -205,54 +179,45 @@ static void CmdHi14asim(char *str)        // ## simulate iso14443a tag
 \r
        unsigned int hi=0, lo=0;\r
        int n=0, i=0;\r
-       UsbCommand c;\r
-\r
        while (sscanf(&str[i++], "%1x", &n ) == 1) {\r
                hi=(hi<<4)|(lo>>28);\r
                lo=(lo<<4)|(n&0xf);\r
        }\r
 \r
-       c.cmd = CMD_SIMULATE_TAG_ISO_14443a;\r
-       // c.ext should be set to *str or convert *str to the correct format for a uid\r
-       c.arg[0] = hi;\r
-       c.arg[1] = lo;\r
+       // c.arg should be set to *str or convert *str to the correct format for a uid\r
+       UsbCommand c = {CMD_SIMULATE_TAG_ISO_14443a, {hi, lo, 0}};\r
        PrintToScrollback("Emulating 14443A TAG with UID %x%16x", hi, lo);\r
-       SendCommand(&c, false);\r
+       SendCommand(&c);\r
 }\r
 \r
 static void CmdHi14snoop(char *str)\r
 {\r
-       UsbCommand c;\r
-       c.cmd = CMD_SNOOP_ISO_14443;\r
-       SendCommand(&c, false);\r
+       UsbCommand c={CMD_SNOOP_ISO_14443};\r
+       SendCommand(&c);\r
 }\r
 \r
 static void CmdHi14asnoop(char *str)\r
 {\r
-       UsbCommand c;\r
-       c.cmd = CMD_SNOOP_ISO_14443a;\r
-       SendCommand(&c, false);\r
+       UsbCommand c={CMD_SNOOP_ISO_14443a};\r
+       SendCommand(&c);\r
 }\r
 \r
 static void CmdLegicRfSim(char *str)\r
 {\r
-       UsbCommand c;\r
-       c.cmd = CMD_SIMULATE_TAG_LEGIC_RF;\r
-       SendCommand(&c, false);\r
+       UsbCommand c={CMD_SIMULATE_TAG_LEGIC_RF};\r
+       SendCommand(&c);\r
 }\r
 \r
 static void CmdLegicRfRead(char *str)\r
 {\r
-       UsbCommand c;\r
-       c.cmd = CMD_READER_LEGIC_RF;\r
-       SendCommand(&c, false);\r
+       UsbCommand c={CMD_READER_LEGIC_RF};\r
+       SendCommand(&c);\r
 }\r
 \r
 static void CmdFPGAOff(char *str)              // ## FPGA Control\r
 {\r
-       UsbCommand c;\r
-       c.cmd = CMD_FPGA_MAJOR_MODE_OFF;\r
-       SendCommand(&c, false);\r
+       UsbCommand c={CMD_FPGA_MAJOR_MODE_OFF};\r
+       SendCommand(&c);\r
 }\r
 \r
 /* clear out our graph window */\r
@@ -683,39 +648,33 @@ static void ChkBitstream(char *str)
 static void CmdLosim(char *str)\r
 {\r
        int i;\r
-       UsbCommand c;\r
 \r
        /* convert to bitstream if necessary */\r
        ChkBitstream(str);\r
 \r
        for (i = 0; i < GraphTraceLen; i += 48) {\r
-               UsbCommand c;\r
+               UsbCommand c={CMD_DOWNLOADED_SIM_SAMPLES_125K, {i, 0, 0}};\r
                int j;\r
                for(j = 0; j < 48; j++) {\r
                        c.d.asBytes[j] = GraphBuffer[i+j];\r
                }\r
-               c.cmd = CMD_DOWNLOADED_SIM_SAMPLES_125K;\r
-               c.arg[0] = i;\r
-               SendCommand(&c, false);\r
+               SendCommand(&c);\r
        }\r
 \r
-       c.cmd = CMD_SIMULATE_TAG_125K;\r
-       c.arg[0] = GraphTraceLen;\r
-       SendCommand(&c, false);\r
+       UsbCommand c={CMD_SIMULATE_TAG_125K, {GraphTraceLen, 0, 0}};\r
+       SendCommand(&c);\r
 }\r
 \r
 static void CmdLosimBidir(char *str)\r
 {\r
-       UsbCommand c;\r
-       c.cmd = CMD_LF_SIMULATE_BIDIR;\r
-       c.arg[0] = 47; /* Set ADC to twice the carrier for a slight supersampling */\r
-       c.arg[1] = 384;\r
-       SendCommand(&c, false);\r
+       /* Set ADC to twice the carrier for a slight supersampling */\r
+       UsbCommand c={CMD_LF_SIMULATE_BIDIR, {47, 384, 0}};\r
+       SendCommand(&c);\r
 }\r
 \r
 static void CmdLoread(char *str)\r
 {\r
-       UsbCommand c;\r
+       UsbCommand c={CMD_ACQUIRE_RAW_ADC_SAMPLES_125K};\r
        // 'h' means higher-low-frequency, 134 kHz\r
        if(*str == 'h') {\r
                c.arg[0] = 1;\r
@@ -725,13 +684,12 @@ static void CmdLoread(char *str)
                PrintToScrollback("use 'loread' or 'loread h'");\r
                return;\r
        }\r
-       c.cmd = CMD_ACQUIRE_RAW_ADC_SAMPLES_125K;\r
-       SendCommand(&c, false);\r
+       SendCommand(&c);\r
 }\r
 \r
 static void CmdDetectReader(char *str)\r
 {\r
-       UsbCommand c;\r
+       UsbCommand c={CMD_LISTEN_READER_FIELD};\r
        // 'l' means LF - 125/134 kHz\r
        if(*str == 'l') {\r
                c.arg[0] = 1;\r
@@ -741,51 +699,42 @@ static void CmdDetectReader(char *str)
                PrintToScrollback("use 'detectreader' or 'detectreader l' or 'detectreader h'");\r
                return;\r
        }\r
-       c.cmd = CMD_LISTEN_READER_FIELD;\r
-        SendCommand(&c, false);\r
+       SendCommand(&c);\r
 }\r
 \r
 /* send a command before reading */\r
 static void CmdLoCommandRead(char *str)\r
 {\r
        static char dummy[3];\r
-       UsbCommand c;\r
 \r
        dummy[0]= ' ';\r
 \r
-       c.cmd = CMD_MOD_THEN_ACQUIRE_RAW_ADC_SAMPLES_125K;\r
+       UsbCommand c={CMD_MOD_THEN_ACQUIRE_RAW_ADC_SAMPLES_125K};\r
        sscanf(str, "%i %i %i %s %s", &c.arg[0], &c.arg[1], &c.arg[2], (char *) &c.d.asBytes,(char *) &dummy+1);\r
        // in case they specified 'h'\r
        strcpy((char *)&c.d.asBytes + strlen((char *)c.d.asBytes), dummy);\r
-       SendCommand(&c, false);\r
+       SendCommand(&c);\r
 }\r
 \r
 static void CmdLosamples(char *str)\r
 {\r
        int cnt = 0;\r
-       int i;\r
-       int n;\r
-       int j;\r
+       int i, j, n;\r
 \r
-       n=atoi(str);\r
+       n=strtol(str, NULL, 0);\r
        if (n==0) n=128;\r
        if (n>16000) n=16000;\r
 \r
+       PrintToScrollback("Reading %d samples\n", n);\r
        for(i = 0; i < n; i += 12) {\r
-               UsbCommand c;\r
-               c.cmd = CMD_DOWNLOAD_RAW_ADC_SAMPLES_125K;\r
-               c.arg[0] = i;\r
-               SendCommand(&c, false);\r
-               ReceiveCommand(&c);\r
-               if(c.cmd != CMD_DOWNLOADED_RAW_ADC_SAMPLES_125K) {\r
-                       if (!go)\r
-                               PrintToScrollback("bad resp");\r
-                       return;\r
-               }\r
+               UsbCommand c = {CMD_DOWNLOAD_RAW_ADC_SAMPLES_125K, {i, 0, 0}};\r
+               SendCommand(&c);\r
+               wait_for_response(CMD_DOWNLOADED_RAW_ADC_SAMPLES_125K);\r
                for(j = 0; j < 48; j++) {\r
-                       GraphBuffer[cnt++] = ((int)c.d.asBytes[j]) - 128;\r
+                       GraphBuffer[cnt++] = ((int)sample_buf[j]) - 128;\r
                }\r
        }\r
+       PrintToScrollback("Done!\n");\r
        GraphTraceLen = n*4;\r
        RepaintGraphWindow();\r
 }\r
@@ -793,24 +742,17 @@ static void CmdLosamples(char *str)
 static void CmdBitsamples(char *str)\r
 {\r
        int cnt = 0;\r
-       int i;\r
-       int n;\r
-       int j, k;\r
+       int i, j, k, n;\r
 \r
        n = 3072;\r
        for(i = 0; i < n; i += 12) {\r
-               UsbCommand c;\r
-               c.cmd = CMD_DOWNLOAD_RAW_ADC_SAMPLES_125K;\r
-               c.arg[0] = i;\r
-               SendCommand(&c, false);\r
-               ReceiveCommand(&c);\r
-               if(c.cmd != CMD_DOWNLOADED_RAW_ADC_SAMPLES_125K) {\r
-                       PrintToScrollback("bad resp");\r
-                       return;\r
-               }\r
+               UsbCommand c = {CMD_DOWNLOAD_RAW_ADC_SAMPLES_125K, {i, 0, 0}};\r
+               SendCommand(&c);\r
+               wait_for_response(CMD_DOWNLOADED_RAW_ADC_SAMPLES_125K);\r
+\r
                for(j = 0; j < 48; j++) {\r
                        for(k = 0; k < 8; k++) {\r
-                               if(c.d.asBytes[j] & (1 << (7 - k))) {\r
+                               if(sample_buf[j] & (1 << (7 - k))) {\r
                                        GraphBuffer[cnt++] = 1;\r
                                } else {\r
                                        GraphBuffer[cnt++] = 0;\r
@@ -825,26 +767,19 @@ static void CmdBitsamples(char *str)
 static void CmdHisamples(char *str)\r
 {\r
        int cnt = 0;\r
-       int i;\r
-       int n;\r
-       int j;\r
+       int i, j, n;\r
+\r
        n = 1000;\r
        for(i = 0; i < n; i += 12) {\r
-               UsbCommand c;\r
-               c.cmd = CMD_DOWNLOAD_RAW_ADC_SAMPLES_125K;\r
-               c.arg[0] = i;\r
-               SendCommand(&c, false);\r
-               ReceiveCommand(&c);\r
-               if(c.cmd != CMD_DOWNLOADED_RAW_ADC_SAMPLES_125K) {\r
-                       PrintToScrollback("bad resp");\r
-                       return;\r
-               }\r
+               UsbCommand c = {CMD_DOWNLOAD_RAW_ADC_SAMPLES_125K, {i, 0, 0}};\r
+               SendCommand(&c);\r
+               wait_for_response(CMD_DOWNLOADED_RAW_ADC_SAMPLES_125K);\r
                for(j = 0; j < 48; j++) {\r
-                       GraphBuffer[cnt++] = (int)((uint8_t)c.d.asBytes[j]);\r
+                       GraphBuffer[cnt++] = (int)(sample_buf[j]);\r
                }\r
        }\r
-       GraphTraceLen = n*4;\r
 \r
+       GraphTraceLen = n*4;\r
        RepaintGraphWindow();\r
 }\r
 \r
@@ -852,27 +787,18 @@ static int CmdHisamplest(char *str, int nrlow)
 {\r
        int cnt = 0;\r
        int t1, t2;\r
-       int i;\r
-       int n;\r
+       int i, j, n;\r
        int hasbeennull;\r
        int show;\r
-       int j;\r
-\r
 \r
        n = 1000;\r
        hasbeennull = 0;\r
        for(i = 0; i < n; i += 12) {\r
-               UsbCommand c;\r
-               c.cmd = CMD_DOWNLOAD_RAW_ADC_SAMPLES_125K;\r
-               c.arg[0] = i;\r
-               SendCommand(&c, false);\r
-               ReceiveCommand(&c);\r
-               if(c.cmd != CMD_DOWNLOADED_RAW_ADC_SAMPLES_125K) {\r
-                       PrintToScrollback("bad resp");\r
-                       return 0;\r
-               }\r
+               UsbCommand c = {CMD_DOWNLOAD_RAW_ADC_SAMPLES_125K, {i, 0, 0}};\r
+               SendCommand(&c);\r
+               wait_for_response(CMD_DOWNLOADED_RAW_ADC_SAMPLES_125K);\r
                for(j = 0; j < 48; j++) {\r
-                       t2 = (int)((uint8_t)c.d.asBytes[j]);\r
+                       t2 = (int)(sample_buf[j]);\r
                        if((t2 ^ 0xC0) & 0xC0) { hasbeennull++; }\r
 \r
                        show = 0;\r
@@ -920,46 +846,38 @@ static int CmdHisamplest(char *str, int nrlow)
 \r
 static void CmdHexsamples(char *str)\r
 {\r
-       int i;\r
-       int n;\r
-       int requested = atoi(str);\r
+       int i, j, n;\r
+       int requested = strtol(str, NULL, 0);\r
        int delivered = 0;\r
-       int j;\r
 \r
-       if(atoi(str) == 0) {\r
+       if (requested == 0) {\r
                n = 12;\r
                requested = 12;\r
        } else {\r
-               n = atoi(str)/4;\r
+               n = requested/4;\r
        }\r
 \r
        for(i = 0; i < n; i += 12) {\r
-               UsbCommand c;\r
-               c.cmd = CMD_DOWNLOAD_RAW_ADC_SAMPLES_125K;\r
-               c.arg[0] = i;\r
-               SendCommand(&c, false);\r
-               ReceiveCommand(&c);\r
-               if(c.cmd != CMD_DOWNLOADED_RAW_ADC_SAMPLES_125K) {\r
-                       PrintToScrollback("bad resp");\r
-                       return;\r
-               }\r
-               for(j = 0; j < 48; j += 8) {\r
+               UsbCommand c = {CMD_DOWNLOAD_RAW_ADC_SAMPLES_125K, {i, 0, 0}};\r
+               SendCommand(&c);\r
+               wait_for_response(CMD_DOWNLOADED_RAW_ADC_SAMPLES_125K);\r
+               for (j = 0; j < 48; j += 8) {\r
                        PrintToScrollback("%02x %02x %02x %02x %02x %02x %02x %02x",\r
-                               c.d.asBytes[j+0],\r
-                               c.d.asBytes[j+1],\r
-                               c.d.asBytes[j+2],\r
-                               c.d.asBytes[j+3],\r
-                               c.d.asBytes[j+4],\r
-                               c.d.asBytes[j+5],\r
-                               c.d.asBytes[j+6],\r
-                               c.d.asBytes[j+7],\r
-                               c.d.asBytes[j+8]\r
+                               sample_buf[j+0],\r
+                               sample_buf[j+1],\r
+                               sample_buf[j+2],\r
+                               sample_buf[j+3],\r
+                               sample_buf[j+4],\r
+                               sample_buf[j+5],\r
+                               sample_buf[j+6],\r
+                               sample_buf[j+7],\r
+                               sample_buf[j+8]\r
                        );\r
                        delivered += 8;\r
-                       if(delivered >= requested)\r
+                       if (delivered >= requested)\r
                                break;\r
                }\r
-               if(delivered >= requested)\r
+               if (delivered >= requested)\r
                        break;\r
        }\r
 }\r
@@ -967,28 +885,21 @@ static void CmdHexsamples(char *str)
 static void CmdHisampless(char *str)\r
 {\r
        int cnt = 0;\r
-       int i;\r
-       int n;\r
-       int j;\r
+       int i, j;\r
+       int n = strtol(str, NULL, 0);\r
 \r
-       if(atoi(str) == 0) {\r
+       if(n == 0) {\r
                n = 1000;\r
        } else {\r
-               n = atoi(str)/4;\r
+               n/= 4;\r
        }\r
 \r
        for(i = 0; i < n; i += 12) {\r
-               UsbCommand c;\r
-               c.cmd = CMD_DOWNLOAD_RAW_ADC_SAMPLES_125K;\r
-               c.arg[0] = i;\r
-               SendCommand(&c, false);\r
-               ReceiveCommand(&c);\r
-               if(c.cmd != CMD_DOWNLOADED_RAW_ADC_SAMPLES_125K) {\r
-                       PrintToScrollback("bad resp");\r
-                       return;\r
-               }\r
+               UsbCommand c = {CMD_DOWNLOAD_RAW_ADC_SAMPLES_125K, {i, 0, 0}};\r
+               SendCommand(&c);\r
+               wait_for_response(CMD_DOWNLOADED_RAW_ADC_SAMPLES_125K);\r
                for(j = 0; j < 48; j++) {\r
-                       GraphBuffer[cnt++] = (int)((signed char)c.d.asBytes[j]);\r
+                       GraphBuffer[cnt++] = (int)(sample_buf[j]);\r
                }\r
        }\r
        GraphTraceLen = cnt;\r
@@ -1022,7 +933,6 @@ static void CmdHi14bdemod(char *str)
        int isum, qsum;\r
        int outOfWeakAt;\r
        bool negateI, negateQ;\r
-       uint8_t first, second;\r
 \r
        uint8_t data[256];\r
        int dataLen=0;\r
@@ -1080,13 +990,13 @@ static void CmdHi14bdemod(char *str)
        PrintToScrollback("make it to demod loop");\r
 \r
        for(;;) {\r
-               uint16_t shiftReg = 0;\r
                iold = i;\r
                while(GraphBuffer[i] >= 0 && i < GraphTraceLen)\r
                        i++;\r
                if(i >= GraphTraceLen) goto demodError;\r
                if((i - iold) > 6) goto demodError;\r
 \r
+               uint16_t shiftReg = 0;\r
                if(i + 20 >= GraphTraceLen) goto demodError;\r
 \r
                for(j = 0; j < 10; j++) {\r
@@ -1121,6 +1031,7 @@ static void CmdHi14bdemod(char *str)
                }\r
        }\r
 \r
+       uint8_t first, second;\r
        ComputeCrc14443(CRC_14443_B, data, dataLen-2, &first, &second);\r
        PrintToScrollback("CRC: %02x %02x (%s)\n", first, second,\r
                (first == data[dataLen-2] && second == data[dataLen-1]) ?\r
@@ -1137,33 +1048,31 @@ demodError:
 static void CmdHi14list(char *str)\r
 {\r
        uint8_t got[960];\r
-       int i = 0;\r
-       int prev = -1;\r
        GetFromBigBuf(got, sizeof(got));\r
 \r
        PrintToScrollback("recorded activity:");\r
        PrintToScrollback(" time        :rssi: who bytes");\r
        PrintToScrollback("---------+----+----+-----------");\r
 \r
-       for(i=0; i<900;) {\r
-               bool isResponse;\r
-               int len = got[i+8];\r
-               int metric = *((uint32_t *)(got+i+4));\r
-\r
-               int timestamp = *((uint32_t *)(got+i));\r
-               uint8_t *frame = (got+i+9);\r
-               char *crc;\r
-               char metricString[100];\r
+       int i = 0;\r
+       int prev = -1;\r
 \r
-               char line[1000] = "";\r
-               int j;\r
+       for(;;) {\r
+               if(i >= 900) {\r
+                       break;\r
+               }\r
 \r
+               bool isResponse;\r
+               int timestamp = *((uint32_t *)(got+i));\r
                if(timestamp & 0x80000000) {\r
                        timestamp &= 0x7fffffff;\r
                        isResponse = 1;\r
                } else {\r
                        isResponse = 0;\r
                }\r
+               int metric = *((uint32_t *)(got+i+4));\r
+\r
+               int len = got[i+8];\r
 \r
                if(len > 100) {\r
                        break;\r
@@ -1172,10 +1081,15 @@ static void CmdHi14list(char *str)
                        break;\r
                }\r
 \r
+               uint8_t *frame = (got+i+9);\r
+\r
+               char line[1000] = "";\r
+               int j;\r
                for(j = 0; j < len; j++) {\r
                        sprintf(line+(j*3), "%02x  ", frame[j]);\r
                }\r
 \r
+               char *crc;\r
                if(len > 2) {\r
                        uint8_t b1, b2;\r
                        ComputeCrc14443(CRC_14443_B, frame, len-2, &b1, &b2);\r
@@ -1188,6 +1102,7 @@ static void CmdHi14list(char *str)
                        crc = "(SHORT)";\r
                }\r
 \r
+               char metricString[100];\r
                if(isResponse) {\r
                        sprintf(metricString, "%3d", metric);\r
                } else {\r
@@ -1207,27 +1122,22 @@ static void CmdHi14list(char *str)
 static void CmdHi14alist(char *str)\r
 {\r
        uint8_t got[1920];\r
-       int i = 0;\r
-       int prev = -1;\r
-\r
        GetFromBigBuf(got, sizeof(got));\r
 \r
        PrintToScrollback("recorded activity:");\r
        PrintToScrollback(" ETU     :rssi: who bytes");\r
        PrintToScrollback("---------+----+----+-----------");\r
 \r
-       for(i=0;i<1900;) {\r
+       int i = 0;\r
+       int prev = -1;\r
+\r
+       for(;;) {\r
+               if(i >= 1900) {\r
+                       break;\r
+               }\r
+\r
                bool isResponse;\r
                int timestamp = *((uint32_t *)(got+i));\r
-               int metric = 0;\r
-               int parityBits = *((uint32_t *)(got+i+4));\r
-               int len = got[i+8];\r
-               char line[1000] = "";\r
-               int j;\r
-               uint8_t *frame = (got+i+9);\r
-               const char *crc;\r
-               char metricString[100];\r
-\r
                if(timestamp & 0x80000000) {\r
                        timestamp &= 0x7fffffff;\r
                        isResponse = 1;\r
@@ -1235,6 +1145,8 @@ static void CmdHi14alist(char *str)
                        isResponse = 0;\r
                }\r
 \r
+               int metric = 0;\r
+               int parityBits = *((uint32_t *)(got+i+4));\r
                // 4 bytes of additional information...\r
                // maximum of 32 additional parity bit information\r
                //\r
@@ -1243,6 +1155,8 @@ static void CmdHi14alist(char *str)
                // or each half bit period in 256 levels.\r
 \r
 \r
+               int len = got[i+8];\r
+\r
                if(len > 100) {\r
                        break;\r
                }\r
@@ -1250,9 +1164,13 @@ static void CmdHi14alist(char *str)
                        break;\r
                }\r
 \r
+               uint8_t *frame = (got+i+9);\r
+\r
                // Break and stick with current result if buffer was not completely full\r
                if(frame[0] == 0x44 && frame[1] == 0x44 && frame[3] == 0x44) { break; }\r
 \r
+               char line[1000] = "";\r
+               int j;\r
                for(j = 0; j < len; j++) {\r
                        int oddparity = 0x01;\r
                        int k;\r
@@ -1270,6 +1188,7 @@ static void CmdHi14alist(char *str)
                        }\r
                }\r
 \r
+               char *crc;\r
                crc = "";\r
                if(len > 2) {\r
                        uint8_t b1, b2;\r
@@ -1309,6 +1228,7 @@ static void CmdHi14alist(char *str)
                        crc = ""; // SHORT\r
                }\r
 \r
+               char metricString[100];\r
                if(isResponse) {\r
                        sprintf(metricString, "%3d", metric);\r
                } else {\r
@@ -1378,9 +1298,6 @@ static void CmdHi15demod(char *str)
        int max = 0, maxPos;\r
 \r
        int skip = 4;\r
-       int k = 0;\r
-       uint8_t outBuf[20];\r
-       uint8_t mask = 0x01;\r
 \r
        if(GraphTraceLen < 1000) return;\r
 \r
@@ -1399,7 +1316,10 @@ static void CmdHi15demod(char *str)
                max/(arraylen(FrameSOF)/skip));\r
 \r
        i = maxPos + arraylen(FrameSOF)/skip;\r
+       int k = 0;\r
+       uint8_t outBuf[20];\r
        memset(outBuf, 0, sizeof(outBuf));\r
+       uint8_t mask = 0x01;\r
        for(;;) {\r
                int corr0 = 0, corr1 = 0, corrEOF = 0;\r
                for(j = 0; j < arraylen(Logic0); j += skip) {\r
@@ -1471,8 +1391,6 @@ static void CmdFSKdemod(char *cmdline)
 \r
        int i, j;\r
        int minMark=0, maxMark=0;\r
-       int max = 0, maxPos = 0;\r
-       uint8_t bits[46];\r
 \r
        for(i = 0; i < GraphTraceLen - convLen; i++) {\r
                int lowSum = 0, highSum = 0;\r
@@ -1508,6 +1426,7 @@ static void CmdFSKdemod(char *cmdline)
        RepaintGraphWindow();\r
 \r
        // Find bit-sync (3 lo followed by 3 high)\r
+       int max = 0, maxPos = 0;\r
        for(i = 0; i < 6000; i++) {\r
                int dec = 0;\r
                for(j = 0; j < 3*lowLen; j++) {\r
@@ -1535,6 +1454,7 @@ static void CmdFSKdemod(char *cmdline)
        PrintToScrollback("actual data bits start at sample %d", maxPos);\r
        PrintToScrollback("length %d/%d", highLen, lowLen);\r
 \r
+       uint8_t bits[46];\r
        bits[sizeof(bits)-1] = '\0';\r
 \r
        // find bit pairs and manchester decode them\r
@@ -1569,24 +1489,22 @@ static void CmdFSKdemod(char *cmdline)
 // read a TI tag and return its ID\r
 static void CmdTIRead(char *str)\r
 {\r
-       UsbCommand c;\r
-       c.cmd = CMD_READ_TI_TYPE;\r
-       SendCommand(&c, false);\r
+       UsbCommand c={CMD_READ_TI_TYPE};\r
+       SendCommand(&c);\r
 }\r
 \r
 // write new data to a r/w TI tag\r
 static void CmdTIWrite(char *str)\r
 {\r
-       UsbCommand c;\r
+       UsbCommand c={CMD_WRITE_TI_TYPE};\r
        int res=0;\r
 \r
-       c.cmd = CMD_WRITE_TI_TYPE;\r
        res = sscanf(str, "0x%x 0x%x 0x%x ", &c.arg[0], &c.arg[1], &c.arg[2]);\r
        if (res == 2) c.arg[2]=0;\r
        if (res<2)\r
                PrintToScrollback("Please specify the data as two hex strings, optionally the CRC as a third");\r
        else\r
-               SendCommand(&c, false);\r
+               SendCommand(&c);\r
 }\r
 \r
 static void CmdTIDemod(char *cmdline)\r
@@ -1650,12 +1568,8 @@ h = sign(sin(cumsum(h)));
        int convLen = (highLen>lowLen)?highLen:lowLen;\r
        uint16_t crc;\r
        int i, j, TagType;\r
-       int lowSum = 0, highSum = 0;\r
+       int lowSum = 0, highSum = 0;;\r
        int lowTot = 0, highTot = 0;\r
-       int max = 0, maxPos = 0;\r
-       uint8_t bits[1+64+16+8+16];\r
-       uint32_t shift3 = 0x7e000000, shift2 = 0, shift1 = 0, shift0 = 0;\r
-\r
 \r
        for(i = 0; i < GraphTraceLen - convLen; i++) {\r
                lowSum = 0;\r
@@ -1705,6 +1619,7 @@ h = sign(sin(cumsum(h)));
        // Okay, so now we have unsliced soft decisions;\r
        // find bit-sync, and then get some bits.\r
        // look for 17 low bits followed by 6 highs (common pattern for ro and rw tags)\r
+       int max = 0, maxPos = 0;\r
        for(i = 0; i < 6000; i++) {\r
                int j;\r
                int dec = 0;\r
@@ -1740,8 +1655,11 @@ h = sign(sin(cumsum(h)));
 \r
        PrintToScrollback("length %d/%d", highLen, lowLen);\r
 \r
+       uint8_t bits[1+64+16+8+16];\r
        bits[sizeof(bits)-1] = '\0';\r
 \r
+       uint32_t shift3 = 0x7e000000, shift2 = 0, shift1 = 0, shift0 = 0;\r
+\r
        for(i = 0; i < arraylen(bits)-1; i++) {\r
                int high = 0;\r
                int low = 0;\r
@@ -1911,12 +1829,12 @@ static void CmdHpf(char *str)
 static void CmdZerocrossings(char *str)\r
 {\r
        int i;\r
-       int sign = 1;\r
-       int zc = 0;\r
-       int lastZc = 0;\r
        // Zero-crossings aren't meaningful unless the signal is zero-mean.\r
        CmdHpf("");\r
 \r
+       int sign = 1;\r
+       int zc = 0;\r
+       int lastZc = 0;\r
        for(i = 0; i < GraphTraceLen; i++) {\r
                if(GraphBuffer[i]*sign >= 0) {\r
                        // No change in sign, reproduce the previous sample count.\r
@@ -1966,7 +1884,6 @@ static void CmdLtrim(char *str)
 static void CmdAutoCorr(char *str)\r
 {\r
        static int CorrelBuffer[MAX_GRAPH_TRACE_LEN];\r
-       int i;\r
 \r
        int window = atoi(str);\r
 \r
@@ -1983,6 +1900,7 @@ static void CmdAutoCorr(char *str)
 \r
        PrintToScrollback("performing %d correlations", GraphTraceLen - window);\r
 \r
+       int i;\r
        for(i = 0; i < GraphTraceLen - window; i++) {\r
                int sum = 0;\r
                int j;\r
@@ -2018,10 +1936,6 @@ static void CmdVchdemod(char *str)
        // So first, we correlate for the sync pattern, and mark that.\r
        int bestCorrel = 0, bestPos = 0;\r
        int i;\r
-       char bits[257];\r
-       int worst = INT_MAX;\r
-       int worstPos;\r
-\r
        // It does us no good to find the sync pattern, with fewer than\r
        // 2048 samples after it...\r
        for(i = 0; i < (GraphTraceLen-2048); i++) {\r
@@ -2037,8 +1951,12 @@ static void CmdVchdemod(char *str)
        }\r
        PrintToScrollback("best sync at %d [metric %d]", bestPos, bestCorrel);\r
 \r
+       char bits[257];\r
        bits[256] = '\0';\r
 \r
+       int worst = INT_MAX;\r
+       int worstPos;\r
+\r
        for(i = 0; i < 2048; i += 8) {\r
                int sum = 0;\r
                int j;\r
@@ -2060,8 +1978,8 @@ static void CmdVchdemod(char *str)
        PrintToScrollback("worst metric: %d at pos %d", worst, worstPos);\r
 \r
        if(strcmp(str, "clone")==0) {\r
-               char *s;\r
                GraphTraceLen = 0;\r
+               char *s;\r
                for(s = bits; *s; s++) {\r
                        int j;\r
                        for(j = 0; j < 16; j++) {\r
@@ -2079,20 +1997,11 @@ static void CmdIndalademod(char *str)
        int state = -1;\r
        int count = 0;\r
        int i, j;\r
-       int uidlen, long_wait;\r
        // worst case with GraphTraceLen=64000 is < 4096\r
        // under normal conditions it's < 2048\r
        uint8_t rawbits[4096];\r
        int rawbit = 0;\r
        int worst = 0, worstPos = 0;\r
-       int start;\r
-       int first = 0;\r
-       uint8_t bits[224];\r
-       char showbits[225];\r
-       int bit;\r
-       int times = 0;\r
-       int phase = 0;\r
-\r
        PrintToScrollback("Expecting a bit less than %d raw bits", GraphTraceLen/32);\r
        for(i = 0; i < GraphTraceLen-1; i += 2) {\r
                count+=1;\r
@@ -2126,6 +2035,7 @@ static void CmdIndalademod(char *str)
        PrintToScrollback("worst metric (0=best..7=worst): %d at pos %d", worst, worstPos);\r
 \r
        // Finding the start of a UID\r
+       int uidlen, long_wait;\r
        if(strcmp(str, "224") == 0) {\r
                uidlen=224;\r
                long_wait=30;\r
@@ -2133,6 +2043,8 @@ static void CmdIndalademod(char *str)
                uidlen=64;\r
                long_wait=29;\r
        }\r
+       int start;\r
+       int first = 0;\r
        for(start = 0; start <= rawbit - uidlen; start++) {\r
                first = rawbits[start];\r
                for(i = start; i < start + long_wait; i++) {\r
@@ -2157,8 +2069,12 @@ static void CmdIndalademod(char *str)
        }\r
 \r
        // Dumping UID\r
+       uint8_t bits[224];\r
+       char showbits[225];\r
        showbits[uidlen]='\0';\r
+       int bit;\r
        i = start;\r
+       int times = 0;\r
        if(uidlen > rawbit) {\r
                PrintToScrollback("Warning: not enough raw bits to get a full UID");\r
                for(bit = 0; bit < rawbit; bit++) {\r
@@ -2197,13 +2113,14 @@ static void CmdIndalademod(char *str)
        // Remodulating for tag cloning\r
        GraphTraceLen = 32*uidlen;\r
        i = 0;\r
+       int phase = 0;\r
        for(bit = 0; bit < uidlen; bit++) {\r
-               int j;\r
                if(bits[bit] == 0) {\r
                        phase = 0;\r
                } else {\r
                        phase = 1;\r
                }\r
+               int j;\r
                for(j = 0; j < 32; j++) {\r
                        GraphBuffer[i++] = phase;\r
                        phase = !phase;\r
@@ -2216,12 +2133,6 @@ static void CmdIndalademod(char *str)
 static void CmdFlexdemod(char *str)\r
 {\r
        int i;\r
-       int start;\r
-       uint8_t bits[64];\r
-\r
-       int bit;\r
-       int phase = 0;\r
-       \r
        for(i = 0; i < GraphTraceLen; i++) {\r
                if(GraphBuffer[i] < 0) {\r
                        GraphBuffer[i] = -1;\r
@@ -2231,6 +2142,7 @@ static void CmdFlexdemod(char *str)
        }\r
 \r
 #define LONG_WAIT 100\r
+       int start;\r
        for(start = 0; start < GraphTraceLen - LONG_WAIT; start++) {\r
                int first = GraphBuffer[start];\r
                for(i = start; i < start + LONG_WAIT; i++) {\r
@@ -2250,6 +2162,9 @@ static void CmdFlexdemod(char *str)
        GraphBuffer[start] = 2;\r
        GraphBuffer[start+1] = -2;\r
 \r
+       uint8_t bits[64];\r
+\r
+       int bit;\r
        i = start;\r
        for(bit = 0; bit < 64; bit++) {\r
                int j;\r
@@ -2281,13 +2196,14 @@ static void CmdFlexdemod(char *str)
 \r
        GraphTraceLen = 32*64;\r
        i = 0;\r
+       int phase = 0;\r
        for(bit = 0; bit < 64; bit++) {\r
-               int j;\r
                if(bits[bit] == 0) {\r
                        phase = 0;\r
                } else {\r
                        phase = 1;\r
                }\r
+               int j;\r
                for(j = 0; j < 32; j++) {\r
                        GraphBuffer[i++] = phase;\r
                        phase = !phase;\r
@@ -2538,16 +2454,6 @@ static void Cmdmanchesterdemod(char *str) {
        int bitidx = 0;\r
        int bit2idx = 0;\r
        int warnings = 0;\r
-       int tolerance;\r
-       /* Holds the decoded bitstream: each clock period contains 2 bits       */\r
-       /* later simplified to 1 bit after manchester decoding.                 */\r
-       /* Add 10 bits to allow for noisy / uncertain traces without aborting   */\r
-       /* int BitStream[GraphTraceLen*2/clock+10]; */\r
-\r
-       /* But it does not work if compiling on WIndows: therefore we just allocate a */\r
-       /* large array */\r
-       int BitStream[MAX_GRAPH_TRACE_LEN];\r
-\r
 \r
        /* check if we're inverting output */\r
        if(*str == 'i')\r
@@ -2559,6 +2465,15 @@ static void Cmdmanchesterdemod(char *str) {
                while(*str == ' '); // in case a 2nd argument was given\r
        }\r
 \r
+       /* Holds the decoded bitstream: each clock period contains 2 bits       */\r
+       /* later simplified to 1 bit after manchester decoding.                 */\r
+       /* Add 10 bits to allow for noisy / uncertain traces without aborting   */\r
+       /* int BitStream[GraphTraceLen*2/clock+10]; */\r
+\r
+       /* But it does not work if compiling on WIndows: therefore we just allocate a */\r
+       /* large array */\r
+       int BitStream[MAX_GRAPH_TRACE_LEN];\r
+\r
        /* Detect high and lows */\r
        for (i = 0; i < GraphTraceLen; i++)\r
        {\r
@@ -2571,7 +2486,7 @@ static void Cmdmanchesterdemod(char *str) {
        /* Get our clock */\r
        clock = GetClock(str, high);\r
 \r
-       tolerance = clock/4;\r
+       int tolerance = clock/4;\r
 \r
        /* Detect first transition */\r
        /* Lo-Hi (arbitrary)       */\r
@@ -2725,13 +2640,13 @@ static void Cmdmanchesterdemod(char *str) {
  */\r
 static void CmdHiddemod(char *str)\r
 {\r
-       int i;\r
        if(GraphTraceLen < 4800) {\r
                PrintToScrollback("too short; need at least 4800 samples");\r
                return;\r
        }\r
 \r
        GraphTraceLen = 4800;\r
+       int i;\r
        for(i = 0; i < GraphTraceLen; i++) {\r
                if(GraphBuffer[i] < 0) {\r
                        GraphBuffer[i] = 0;\r
@@ -2770,12 +2685,12 @@ static void CmdScale(char *str)
 \r
 static void CmdSave(char *str)\r
 {\r
-       int i;\r
        FILE *f = fopen(str, "w");\r
        if(!f) {\r
                PrintToScrollback("couldn't open '%s'", str);\r
                return;\r
        }\r
+       int i;\r
        for(i = 0; i < GraphTraceLen; i++) {\r
                fprintf(f, "%d\n", GraphBuffer[i]);\r
        }\r
@@ -2785,7 +2700,6 @@ static void CmdSave(char *str)
 \r
 static void CmdLoad(char *str)\r
 {\r
-       char line[80];\r
        FILE *f = fopen(str, "r");\r
        if(!f) {\r
                PrintToScrollback("couldn't open '%s'", str);\r
@@ -2793,6 +2707,7 @@ static void CmdLoad(char *str)
        }\r
 \r
        GraphTraceLen = 0;\r
+       char line[80];\r
        while(fgets(line, sizeof(line), f)) {\r
                GraphBuffer[GraphTraceLen] = atoi(line);\r
                GraphTraceLen++;\r
@@ -2806,7 +2721,6 @@ static void CmdHIDsimTAG(char *str)
 {\r
        unsigned int hi=0, lo=0;\r
        int n=0, i=0;\r
-       UsbCommand c;\r
 \r
        while (sscanf(&str[i++], "%1x", &n ) == 1) {\r
                hi=(hi<<4)|(lo>>28);\r
@@ -2815,44 +2729,36 @@ static void CmdHIDsimTAG(char *str)
 \r
        PrintToScrollback("Emulating tag with ID %x%16x", hi, lo);\r
 \r
-       c.cmd = CMD_HID_SIM_TAG;\r
-       c.arg[0] = hi;\r
-       c.arg[1] = lo;\r
-       SendCommand(&c, false);\r
+       UsbCommand c={CMD_HID_SIM_TAG, {hi, lo, 0}};\r
+       SendCommand(&c);\r
 }\r
 \r
 static void CmdReadmem(char *str)\r
 {\r
-       UsbCommand c;\r
-       c.cmd = CMD_READ_MEM;\r
-       c.arg[0] = atoi(str);\r
-       SendCommand(&c, false);\r
+       UsbCommand c={CMD_READ_MEM, {strtol(str, NULL, 0), 0, 0}};\r
+       SendCommand(&c);\r
 }\r
 \r
 static void CmdVersion(char *str)\r
 {\r
-       UsbCommand c;\r
-       c.cmd = CMD_VERSION;\r
-       SendCommand(&c, false);\r
+       UsbCommand c={CMD_VERSION};\r
+       SendCommand(&c);\r
 }\r
 \r
 static void CmdLcdReset(char *str)\r
 {\r
-       UsbCommand c;\r
-       c.cmd = CMD_LCD_RESET;\r
-       c.arg[0] = atoi(str);\r
-       SendCommand(&c, false);\r
+       UsbCommand c={CMD_LCD_RESET, {strtol(str, NULL, 0), 0, 0}};\r
+       SendCommand(&c);\r
 }\r
 \r
 static void CmdLcd(char *str)\r
 {\r
        int i, j;\r
-       UsbCommand c;\r
-       c.cmd = CMD_LCD;\r
+       UsbCommand c={CMD_LCD};\r
        sscanf(str, "%x %d", &i, &j);\r
        while (j--) {\r
                c.arg[0] = i&0x1ff;\r
-               SendCommand(&c, false);\r
+               SendCommand(&c);\r
        }\r
 }\r
 \r
@@ -2862,21 +2768,18 @@ static void CmdLcd(char *str)
  */\r
 static void CmdSetDivisor(char *str)\r
 {\r
-       UsbCommand c;\r
-       c.cmd = CMD_SET_LF_DIVISOR;\r
-       c.arg[0] = atoi(str);\r
+       UsbCommand c={CMD_SET_LF_DIVISOR, {strtol(str, NULL, 0), 0, 0}};\r
        if (( c.arg[0]<0) || (c.arg[0]>255)) {\r
                        PrintToScrollback("divisor must be between 19 and 255");\r
        } else {\r
-                       SendCommand(&c, false);\r
+                       SendCommand(&c);\r
                        PrintToScrollback("Divisor set, expected freq=%dHz", 12000000/(c.arg[0]+1));\r
        }\r
 }\r
 \r
 static void CmdSetMux(char *str)\r
 {\r
-       UsbCommand c;\r
-       c.cmd = CMD_SET_ADC_MUX;\r
+       UsbCommand c={CMD_SET_ADC_MUX};\r
        if(strcmp(str, "lopkd") == 0) {\r
                c.arg[0] = 0;\r
        } else if(strcmp(str, "loraw") == 0) {\r
@@ -2886,7 +2789,7 @@ static void CmdSetMux(char *str)
        } else if(strcmp(str, "hiraw") == 0) {\r
                c.arg[0] = 3;\r
        }\r
-       SendCommand(&c, false);\r
+       SendCommand(&c);\r
 }\r
 \r
 typedef void HandlerFunction(char *cmdline);\r
@@ -2898,81 +2801,81 @@ static struct {
        int             offline;  // 1 if the command can be used when in offline mode\r
        char            *docString;\r
 } CommandTable[] = {\r
-       {"amp",                                 CmdAmp,                                         1, "Amplify peaks"},\r
-       {"askdemod",                    Cmdaskdemod,                            1, "<0|1> -- Attempt to demodulate simple ASK tags"},\r
-       {"autocorr",                    CmdAutoCorr,                            1, "<window length> -- Autocorrelation over window"},\r
-       {"bitsamples",          CmdBitsamples,                  0, "Get raw samples as bitstring"},\r
-       {"bitstream",                   Cmdbitstream,                           1, "[clock rate] -- Convert waveform into a bitstream"},\r
-       {"buffclear",                   CmdBuffClear,                           1, "Clear sample buffer and graph window"},\r
-       {"dec",                                         CmdDec,                                                 1, "Decimate samples"},\r
+       {"amp",                 CmdAmp,                 1, "Amplify peaks"},\r
+       {"askdemod",            Cmdaskdemod,            1, "<0|1> -- Attempt to demodulate simple ASK tags"},\r
+       {"autocorr",            CmdAutoCorr,            1, "<window length> -- Autocorrelation over window"},\r
+       {"bitsamples",          CmdBitsamples,          0, "Get raw samples as bitstring"},\r
+       {"bitstream",           Cmdbitstream,           1, "[clock rate] -- Convert waveform into a bitstream"},\r
+       {"buffclear",           CmdBuffClear,           1, "Clear sample buffer and graph window"},\r
+       {"dec",                 CmdDec,                 1, "Decimate samples"},\r
        {"detectclock",         Cmddetectclockrate,     1, "Detect clock rate"},\r
-       {"detectreader",        CmdDetectReader,                0, "['l'|'h'] -- Detect external reader field (option 'l' or 'h' to limit to LF or HF)"},\r
-       {"em410xsim",                   CmdEM410xsim,                           1, "<UID> -- Simulate EM410x tag"},\r
-       {"em410xread",          CmdEM410xread,                  1, "[clock rate] -- Extract ID from EM410x tag"},\r
-       {"em410xwatch",         CmdEM410xwatch,                 0, "Watches for EM410x tags"},\r
-       {"em4x50read",          CmdEM4x50read,                  1, "Extract data from EM4x50 tag"},\r
-       {"exit",                                        CmdQuit,                                                1, "Exit program"},\r
-       {"flexdemod",                   CmdFlexdemod,                           1, "Demodulate samples for FlexPass"},\r
-       {"fpgaoff",                             CmdFPGAOff,                                     0, "Set FPGA off"},\r
-       {"fskdemod",                    CmdFSKdemod,                            1, "Demodulate graph window as a HID FSK"},\r
-       {"grid",                                        CmdGrid,                                                1, "<x> <y> -- overlay grid on graph window, use zero value to turn off either"},\r
-       {"hexsamples",          CmdHexsamples,                  0, "<blocks> -- Dump big buffer as hex bytes"},\r
-       {"hi14alist",                   CmdHi14alist,                           0, "List ISO 14443a history"},\r
-       {"hi14areader",         CmdHi14areader,                 0, "Act like an ISO14443 Type A reader"},\r
-       {"hi14asim",                    CmdHi14asim,                            0, "<UID> -- Fake ISO 14443a tag"},\r
-       {"hi14asnoop",          CmdHi14asnoop,                  0, "Eavesdrop ISO 14443 Type A"},\r
-       {"hi14bdemod",          CmdHi14bdemod,                  1, "Demodulate ISO14443 Type B from tag"},\r
-       {"hi14list",                    CmdHi14list,                            0, "List ISO 14443 history"},\r
-       {"hi14read",                    CmdHi14read,                            0, "Read HF tag (ISO 14443)"},\r
-       {"hi14sim",                             CmdHi14sim,                                     0, "Fake ISO 14443 tag"},\r
-       {"hi14snoop",                   CmdHi14snoop,                           0, "Eavesdrop ISO 14443"},\r
-       {"hi15demod",                   CmdHi15demod,                           1, "Demodulate ISO15693 from tag"},\r
-       {"hi15read",                    CmdHi15read,                            0, "Read HF tag (ISO 15693)"},\r
-       {"hi15reader",          CmdHi15reader,                  0, "Act like an ISO15693 reader"},\r
-       {"hi15sim",                             CmdHi15tag,                                     0, "Fake an ISO15693 tag"},\r
-       {"hiddemod",                    CmdHiddemod,                            1, "Demodulate HID Prox Card II (not optimal)"},\r
-       {"hide",                                        CmdHide,                                                1, "Hide graph window"},\r
-       {"hidfskdemod",         CmdHIDdemodFSK,                 0, "Realtime HID FSK demodulator"},\r
-       {"hidsimtag",                   CmdHIDsimTAG,                           0, "<ID> -- HID tag simulator"},\r
-       {"higet",                                       CmdHi14read_sim,                0, "<samples> -- Get samples HF, 'analog'"},\r
-       {"hisamples",                   CmdHisamples,                           0, "Get raw samples for HF tag"},\r
-       {"hisampless",          CmdHisampless,                  0, "<samples> -- Get signed raw samples, HF tag"},\r
-       {"hisamplest",          CmdHi14readt,                           0, "Get samples HF, for testing"},\r
-       {"hisimlisten",         CmdHisimlisten,                 0, "Get HF samples as fake tag"},\r
-       {"hpf",                                         CmdHpf,                                                 1, "Remove DC offset from trace"},\r
-       {"indalademod",         CmdIndalademod,                 0, "['224'] -- Demodulate samples for Indala 64 bit UID (option '224' for 224 bit)"},\r
-       {"lcd",                                         CmdLcd,                                                 0, "<HEX command> <count> -- Send command/data to LCD"},\r
-       {"lcdreset",                    CmdLcdReset,                            0, "Hardware reset LCD"},\r
-       {"legicrfsim",                  CmdLegicRfSim,                                                  0, "Start the LEGIC RF tag simulator"},\r
-       {"legicrfread",                 CmdLegicRfRead,                                                 0, "Start the LEGIC RF reader"},\r
-       {"load",                                        CmdLoad,                                                1, "<filename> -- Load trace (to graph window"},\r
-       {"locomread",                   CmdLoCommandRead,               0, "<off period> <'0' period> <'1' period> <command> ['h'] -- Modulate LF reader field to send command before read (all periods in microseconds) (option 'h' for 134)"},\r
-       {"loread",                              CmdLoread,                                      0, "['h'] -- Read 125/134 kHz LF ID-only tag (option 'h' for 134)"},\r
-       {"losamples",                   CmdLosamples,                           0, "[128 - 16000] -- Get raw samples for LF tag"},\r
-       {"losim",                                       CmdLosim,                                               0, "Simulate LF tag"},\r
-       {"losimbidir",                                  CmdLosimBidir,                                          0, "Simulate LF tag (with bidirectional data transmission between reader and tag)"},\r
-       {"ltrim",                                       CmdLtrim,                                               1, "<samples> -- Trim samples from left of trace"},\r
-       {"mandemod",                    Cmdmanchesterdemod,     1, "[i] [clock rate] -- Manchester demodulate binary stream (option 'i' to invert output)"},\r
-       {"manmod",                              Cmdmanchestermod,               1, "[clock rate] -- Manchester modulate a binary stream"},\r
-       {"norm",                                        CmdNorm,                                                1, "Normalize max/min to +/-500"},\r
-       {"plot",                                        CmdPlot,                                                1, "Show graph window"},\r
-       {"quit",                                        CmdQuit,                                                1, "Quit program"},\r
-       {"readmem",                             CmdReadmem,                                     0, "[address] -- Read memory at decimal address from flash"},\r
-       {"reset",                                       CmdReset,                                               0, "Reset the Proxmark3"},\r
-       {"save",                                        CmdSave,                                                1, "<filename> -- Save trace (from graph window)"},\r
-       {"scale",                                       CmdScale,                                               1, "<int> -- Set cursor display scale"},\r
-       {"setlfdivisor",        CmdSetDivisor,                  0, "<19 - 255> -- Drive LF antenna at 12Mhz/(divisor+1)"},\r
-       {"setmux",              CmdSetMux,                      0, "<loraw|hiraw|lopkd|hipkd> -- Set the ADC mux to a specific value"},\r
-       {"sri512read",          CmdSri512read,                  0, "<int> -- Read contents of a SRI512 tag"},\r
-       {"srix4kread",          CmdSrix4kread,                  0, "<int> -- Read contents of a SRIX4K tag"},\r
-       {"tidemod",                             CmdTIDemod,                                     1, "Demodulate raw bits for TI-type LF tag"},\r
-       {"tiread",                              CmdTIRead,                                      0, "Read and decode a TI 134 kHz tag"},\r
-       {"tiwrite",                             CmdTIWrite,                                     0, "Write new data to a r/w TI 134 kHz tag"},\r
-       {"threshold",                   CmdThreshold,                           1, "Maximize/minimize every value in the graph window depending on threshold"},\r
-       {"tune",                                        CmdTune,                                                0, "Measure antenna tuning"},\r
-       {"vchdemod",                    CmdVchdemod,                            0, "['clone'] -- Demodulate samples for VeriChip"},\r
-       {"version",                     CmdVersion,                             0, "Show version inforation about the connected Proxmark"},\r
-       {"zerocrossings",       CmdZerocrossings,               1, "Count time between zero-crossings"},\r
+       {"detectreader",        CmdDetectReader,        0, "['l'|'h'] -- Detect external reader field (option 'l' or 'h' to limit to LF or HF)"},\r
+       {"em410xsim",           CmdEM410xsim,           1, "<UID> -- Simulate EM410x tag"},\r
+       {"em410xread",          CmdEM410xread,          1, "[clock rate] -- Extract ID from EM410x tag"},\r
+       {"em410xwatch",         CmdEM410xwatch,         0, "Watches for EM410x tags"},\r
+       {"em4x50read",          CmdEM4x50read,          1, "Extract data from EM4x50 tag"},\r
+       {"exit",                CmdQuit,                1, "Exit program"},\r
+       {"flexdemod",           CmdFlexdemod,           1, "Demodulate samples for FlexPass"},\r
+       {"fpgaoff",             CmdFPGAOff,             0, "Set FPGA off"},\r
+       {"fskdemod",            CmdFSKdemod,            1, "Demodulate graph window as a HID FSK"},\r
+       {"grid",                CmdGrid,                1, "<x> <y> -- overlay grid on graph window, use zero value to turn off either"},\r
+       {"hexsamples",          CmdHexsamples,          0, "<blocks> -- Dump big buffer as hex bytes"},\r
+       {"hi14alist",           CmdHi14alist,           0, "List ISO 14443a history"},\r
+       {"hi14areader",         CmdHi14areader,         0, "Act like an ISO14443 Type A reader"},\r
+       {"hi14asim",            CmdHi14asim,            0, "<UID> -- Fake ISO 14443a tag"},\r
+       {"hi14asnoop",          CmdHi14asnoop,          0, "Eavesdrop ISO 14443 Type A"},\r
+       {"hi14bdemod",          CmdHi14bdemod,          1, "Demodulate ISO14443 Type B from tag"},\r
+       {"hi14list",            CmdHi14list,            0, "List ISO 14443 history"},\r
+       {"hi14read",            CmdHi14read,            0, "Read HF tag (ISO 14443)"},\r
+       {"hi14sim",             CmdHi14sim,             0, "Fake ISO 14443 tag"},\r
+       {"hi14snoop",           CmdHi14snoop,           0, "Eavesdrop ISO 14443"},\r
+       {"hi15demod",           CmdHi15demod,           1, "Demodulate ISO15693 from tag"},\r
+       {"hi15read",            CmdHi15read,            0, "Read HF tag (ISO 15693)"},\r
+       {"hi15reader",          CmdHi15reader,          0, "Act like an ISO15693 reader"},\r
+       {"hi15sim",             CmdHi15tag,             0, "Fake an ISO15693 tag"},\r
+       {"hiddemod",            CmdHiddemod,            1, "Demodulate HID Prox Card II (not optimal)"},\r
+       {"hide",                CmdHide,                1, "Hide graph window"},\r
+       {"hidfskdemod",         CmdHIDdemodFSK,         0, "Realtime HID FSK demodulator"},\r
+       {"hidsimtag",           CmdHIDsimTAG,           0, "<ID> -- HID tag simulator"},\r
+       {"higet",               CmdHi14read_sim,        0, "<samples> -- Get samples HF, 'analog'"},\r
+       {"hisamples",           CmdHisamples,           0, "Get raw samples for HF tag"},\r
+       {"hisampless",          CmdHisampless,          0, "<samples> -- Get signed raw samples, HF tag"},\r
+       {"hisamplest",          CmdHi14readt,           0, "Get samples HF, for testing"},\r
+       {"hisimlisten",         CmdHisimlisten,         0, "Get HF samples as fake tag"},\r
+       {"hpf",                 CmdHpf,                 1, "Remove DC offset from trace"},\r
+       {"indalademod",         CmdIndalademod,         0, "['224'] -- Demodulate samples for Indala 64 bit UID (option '224' for 224 bit)"},\r
+       {"lcd",                 CmdLcd,                 0, "<HEX command> <count> -- Send command/data to LCD"},\r
+       {"lcdreset",            CmdLcdReset,            0, "Hardware reset LCD"},\r
+       {"legicrfsim",          CmdLegicRfSim,          0, "Start the LEGIC RF tag simulator"},\r
+       {"legicrfread",         CmdLegicRfRead,         0, "Start the LEGIC RF reader"},\r
+       {"load",                CmdLoad,                1, "<filename> -- Load trace (to graph window"},\r
+       {"locomread",           CmdLoCommandRead,       0, "<off period> <'0' period> <'1' period> <command> ['h'] -- Modulate LF reader field to send command before read (all periods in microseconds) (option 'h' for 134)"},\r
+       {"loread",              CmdLoread,              0, "['h'] -- Read 125/134 kHz LF ID-only tag (option 'h' for 134)"},\r
+       {"losamples",           CmdLosamples,           0, "[128 - 16000] -- Get raw samples for LF tag"},\r
+       {"losim",               CmdLosim,               0, "Simulate LF tag"},\r
+       {"losimbidir",          CmdLosimBidir,          0, "Simulate LF tag (with bidirectional data transmission between reader and tag)"},\r
+       {"ltrim",               CmdLtrim,               1, "<samples> -- Trim samples from left of trace"},\r
+       {"mandemod",            Cmdmanchesterdemod,     1, "[i] [clock rate] -- Manchester demodulate binary stream (option 'i' to invert output)"},\r
+       {"manmod",              Cmdmanchestermod,       1, "[clock rate] -- Manchester modulate a binary stream"},\r
+       {"norm",                CmdNorm,                1, "Normalize max/min to +/-500"},\r
+       {"plot",                CmdPlot,                1, "Show graph window"},\r
+       {"quit",                CmdQuit,                1, "Quit program"},\r
+       {"readmem",             CmdReadmem,             0, "[address] -- Read memory at decimal address from flash"},\r
+       {"reset",               CmdReset,               0, "Reset the Proxmark3"},\r
+       {"save",                CmdSave,                1, "<filename> -- Save trace (from graph window)"},\r
+       {"scale",               CmdScale,               1, "<int> -- Set cursor display scale"},\r
+       {"setlfdivisor",        CmdSetDivisor,          0, "<19 - 255> -- Drive LF antenna at 12Mhz/(divisor+1)"},\r
+       {"setmux",              CmdSetMux,              0, "<loraw|hiraw|lopkd|hipkd> -- Set the ADC mux to a specific value"},\r
+       {"sri512read",          CmdSri512read,          0, "<int> -- Read contents of a SRI512 tag"},\r
+       {"srix4kread",          CmdSrix4kread,          0, "<int> -- Read contents of a SRIX4K tag"},\r
+       {"tidemod",             CmdTIDemod,             1, "Demodulate raw bits for TI-type LF tag"},\r
+       {"tiread",              CmdTIRead,              0, "Read and decode a TI 134 kHz tag"},\r
+       {"tiwrite",             CmdTIWrite,             0, "Write new data to a r/w TI 134 kHz tag"},\r
+       {"threshold",           CmdThreshold,           1, "Maximize/minimize every value in the graph window depending on threshold"},\r
+       {"tune",                CmdTune,                0, "Measure antenna tuning"},\r
+       {"vchdemod",            CmdVchdemod,            0, "['clone'] -- Demodulate samples for VeriChip"},\r
+       {"version",             CmdVersion,             0, "Show version inforation about the connected Proxmark"},\r
+       {"zerocrossings",       CmdZerocrossings,       1, "Count time between zero-crossings"},\r
 };\r
 \r
 static struct {\r
@@ -3053,6 +2956,8 @@ void CommandReceived(char *cmd)
 //-----------------------------------------------------------------------------\r
 void UsbCommandReceived(UsbCommand *c)\r
 {\r
+//     printf("%s(%x) current cmd = %x\n", __FUNCTION__, c->cmd, current_command);\r
+/* If we recognize a response, return to avoid further processing */\r
        switch(c->cmd) {\r
                case CMD_DEBUG_PRINT_STRING: {\r
                        char s[100];\r
@@ -3062,12 +2967,12 @@ void UsbCommandReceived(UsbCommand *c)
                        memcpy(s, c->d.asBytes, c->arg[0]);\r
                        s[c->arg[0]] = '\0';\r
                        PrintToScrollback("#db# %s", s);\r
-                       break;\r
+                       return;\r
                }\r
 \r
                case CMD_DEBUG_PRINT_INTEGERS:\r
                        PrintToScrollback("#db# %08x, %08x, %08x\r\n", c->arg[0], c->arg[1], c->arg[2]);\r
-                       break;\r
+                       return;\r
 \r
                case CMD_MEASURED_ANTENNA_TUNING: {\r
                        int peakv, peakf;\r
@@ -3091,10 +2996,23 @@ void UsbCommandReceived(UsbCommand *c)
                                PrintToScrollback("# Your HF antenna is unusable.");\r
                        else if (vHf<5000)\r
                                PrintToScrollback("# Your HF antenna is marginal.");\r
-                       break;\r
+                       return;\r
                }\r
                default:\r
-                       PrintToScrollback("unrecognized command %08x\n", c->cmd);\r
                        break;\r
        }\r
+       /* Maybe it's a response: */\r
+       switch(current_command) {\r
+               case CMD_DOWNLOAD_RAW_ADC_SAMPLES_125K:\r
+               if (c->cmd != CMD_DOWNLOADED_RAW_ADC_SAMPLES_125K) goto unexpected_response;\r
+               int i;\r
+               for(i=0; i<48; i++) sample_buf[i] = c->d.asBytes[i];\r
+               printf("stored 48 samples\n");\r
+               received_command = c->cmd;\r
+               return;\r
+       default:\r
+       unexpected_response:\r
+               PrintToScrollback("unrecognized command %08x\n", c->cmd);\r
+               break;\r
+       }\r
 }\r
index 82810b0a676167a395e22aee31f6abc003a07c26..ebcd3391880e0323750a3e584535854c5a13e5d7 100644 (file)
@@ -16,6 +16,16 @@ static uint32_t ExpectedAddr;
 static uint8_t QueuedToSend[256];
 static bool AllWritten;
 #define PHYSICAL_FLASH_START 0x100000
+unsigned int current_command = CMD_UNKNOWN;
+
+void WaitForAck(void) {
+       UsbCommand ack;
+       ReceiveCommand(&ack);
+       if(ack.cmd != CMD_ACK) {
+               printf("bad ACK\n");
+               exit(-1);
+       }
+}
 
 struct partition {
        int start;
@@ -44,7 +54,8 @@ static void FlushPrevious(int translate)
                c.cmd = CMD_SETUP_WRITE;
                memcpy(c.d.asBytes, QueuedToSend+i, 48);
                c.arg[0] = (i/4);
-               SendCommand(&c, true);
+               SendCommand(&c);
+               WaitForAck();
        }
 
        c.cmd = CMD_FINISH_WRITE;
@@ -54,7 +65,8 @@ static void FlushPrevious(int translate)
        }
        printf("c.arg[0] = %08x\r", c.arg[0]);
        memcpy(c.d.asBytes, QueuedToSend+240, 16);
-       SendCommand(&c, true);
+       SendCommand(&c);
+       WaitForAck();
 
        AllWritten = true;
 }
@@ -161,7 +173,8 @@ static int PrepareFlash(struct partition *p, const char *filename, unsigned int
                } else {
                        c.arg[2] = 0;
                }
-               SendCommand(&c, true);
+               SendCommand(&c);
+               WaitForAck();
                translate = 0;
        } else {
                fprintf(stderr, "Warning: Your bootloader does not understand the new START_FLASH command\n");
@@ -179,7 +192,7 @@ static unsigned int GetProxmarkState(void)
        
        UsbCommand c;
        c.cmd = CMD_DEVICE_INFO;
-       SendCommand(&c, false);
+       SendCommand(&c);
        
        UsbCommand resp;
        ReceiveCommand(&resp);
@@ -234,13 +247,13 @@ static unsigned int EnterFlashState(void)
                         * enter the bootrom on the next boot.
                         */
                        c.cmd = CMD_START_FLASH;
-                       SendCommand(&c, false);
+                       SendCommand(&c);
                        fprintf(stderr,"(You don't have to do anything. Press and release the button only if you want to abort)\n");
                        fprintf(stderr,"Waiting for Proxmark to reappear on USB... ");
                } else {
                        /* Old style handover: Ask the user to press the button, then reset the board */
                        c.cmd = CMD_HARDWARE_RESET;
-                       SendCommand(&c, false);
+                       SendCommand(&c);
                        fprintf(stderr,"(Press and hold down button NOW if your bootloader requires it)\n");
                        fprintf(stderr,"Waiting for Proxmark to reappear on USB... ");
                }
@@ -346,7 +359,7 @@ int main(int argc, char **argv) {
        UsbCommand c;
        bzero(&c, sizeof(c));
        c.cmd = CMD_HARDWARE_RESET;
-       SendCommand(&c, false);
+       SendCommand(&c);
 
        CloseProxmark();
 
index b284e0f7a3be7d28a1b197893f00471e333819fb..58704494f5b076352b1ef1b9b9a86acd52b2f93c 100644 (file)
@@ -17,6 +17,7 @@
 \r
 int offline = 0;\r
 HANDLE UsbHandle;\r
+extern unsigned int current_command;\r
 \r
 static void ShowError(void)\r
 {\r
@@ -188,7 +189,7 @@ void ReceiveCommand(UsbCommand *c)
        }\r
 }\r
 \r
-void SendCommand(UsbCommand *c, bool wantAck)\r
+void SendCommand(UsbCommand *c)\r
 {\r
        BYTE buf[65];\r
        buf[0] = 0;\r
@@ -212,14 +213,15 @@ void SendCommand(UsbCommand *c, bool wantAck)
                ShowError();\r
                exit(-1);\r
        }\r
+       current_command = c->cmd;\r
+}\r
 \r
-       if(wantAck) {\r
-               UsbCommand ack;\r
-               ReceiveCommand(&ack);\r
-               if(ack.cmd != CMD_ACK) {\r
-                       printf("bad ACK\n");\r
-                       exit(-1);\r
-               }\r
+void WaitForAck(void) {\r
+       UsbCommand ack;\r
+       ReceiveCommand(&ack);\r
+       if(ack.cmd != CMD_ACK) {\r
+               printf("bad ACK\n");\r
+               exit(-1);\r
        }\r
 }\r
 \r
@@ -255,7 +257,8 @@ static void FlushPrevious(int translate)
                c.cmd = CMD_SETUP_WRITE;\r
                memcpy(c.d.asBytes, QueuedToSend+i, 48);\r
                c.arg[0] = (i/4);\r
-               SendCommand(&c, TRUE);\r
+               SendCommand(&c);\r
+               WaitForAck();\r
        }\r
 \r
        c.cmd = CMD_FINISH_WRITE;\r
@@ -265,8 +268,9 @@ static void FlushPrevious(int translate)
        }\r
        printf("Flashing address: %08x\r", c.arg[0]);\r
        memcpy(c.d.asBytes, QueuedToSend+240, 16);\r
-       SendCommand(&c, TRUE);\r
-\r
+       SendCommand(&c);\r
+       WaitForAck();\r
+       \r
        AllWritten = TRUE;\r
 }\r
 \r
@@ -372,7 +376,8 @@ static int PrepareFlash(struct partition *p, const char *filename, unsigned int
                } else {\r
                        c.arg[2] = 0;\r
                }\r
-               SendCommand(&c, TRUE);\r
+               SendCommand(&c);\r
+               WaitForAck();\r
                translate = 0;\r
        } else {\r
                fprintf(stderr, "Warning: Your bootloader does not understand the new START_FLASH command\n");\r
@@ -390,7 +395,7 @@ static unsigned int GetProxmarkState(void)
 \r
        UsbCommand c;\r
        c.cmd = CMD_DEVICE_INFO;\r
-       SendCommand(&c, FALSE);\r
+       SendCommand(&c);\r
 \r
        UsbCommand resp;\r
        ReceiveCommand(&resp);\r
@@ -445,13 +450,13 @@ static unsigned int EnterFlashState(void)
                         * enter the bootrom on the next boot.\r
                         */\r
                        c.cmd = CMD_START_FLASH;\r
-                       SendCommand(&c, FALSE);\r
+                       SendCommand(&c);\r
                        fprintf(stderr,"(You don't have to do anything. Press and release the button only if you want to abort)\n");\r
                        fprintf(stderr,"Waiting for Proxmark to reappear on USB... ");\r
                } else {\r
                        /* Old style handover: Ask the user to press the button, then reset the board */\r
                        c.cmd = CMD_HARDWARE_RESET;\r
-                       SendCommand(&c, FALSE);\r
+                       SendCommand(&c);\r
                        fprintf(stderr,"(Press and hold down button NOW if your bootloader requires it)\n");\r
                        fprintf(stderr,"Waiting for Proxmark to reappear on USB... ");\r
                }\r
index 21ee8c1a4cd6496eae5be044cd15c4ac80a91e97..10429dc1dbdf8d11729e586b6b38a51b0e5ee8b6 100644 (file)
@@ -14,7 +14,8 @@ typedef WORD uint16_t;
 // prox.cpp\r
 void ReceiveCommand(UsbCommand *c);\r
 bool ReceiveCommandPoll(UsbCommand *c);\r
-void SendCommand(UsbCommand *c, bool);\r
+void SendCommand(UsbCommand *c);\r
+void WaitForAck(void);\r
 void wait_for_response(uint32_t command_type);\r
 \r
 // gui.cpp\r
index e7573feafdad1c05eea39dc7766f3dd742d5f965..4f4bd2cadb53a2db9a3ba6f81fc4476b92b1d848 100644 (file)
@@ -1,6 +1,5 @@
 #include <stdio.h>
 #include <stdlib.h>
-#include <stdint.h>
 #include <stdbool.h>
 #include <unistd.h>
 #include <usb.h>
@@ -9,18 +8,20 @@
 
 #include "prox.h"
 #include "proxmark3.h"
-
+#include "../include/usb_cmd.h"
 usb_dev_handle *devh = NULL;
 static unsigned int claimed_iface = 0;
 unsigned char return_on_error = 0;
 unsigned char error_occured = 0;
+extern unsigned int current_command;
 
-void SendCommand(UsbCommand *c, bool wantAck) {
+void SendCommand(UsbCommand *c) {
        int ret;
 
 #if 0
        printf("Sending %d bytes\n", sizeof(UsbCommand));
 #endif
+       current_command = c->cmd;
        ret = usb_bulk_write(devh, 0x01, (char*)c, sizeof(UsbCommand), 1000);
        if (ret<0) {
                error_occured = 1;
@@ -40,15 +41,19 @@ void SendCommand(UsbCommand *c, bool wantAck) {
                
                return;
        }
-
+/*
        if(wantAck) {
                UsbCommand ack;
+               printf("waiting for ack\n");
                ReceiveCommand(&ack);
                if(ack.cmd != CMD_ACK) {
                        printf("bad ACK\n");
                        exit(-1);
                }
+       } else {
+               printf("not waiting for ack\n");
        }
+*/
 }
 
 bool ReceiveCommandPoll(UsbCommand *c) {
@@ -60,7 +65,7 @@ bool ReceiveCommandPoll(UsbCommand *c) {
                if (ret != -ETIMEDOUT) {
                        error_occured = 1;
                        if (return_on_error)
-                               return 0;
+                               return false;
 
                        fprintf(stderr, "read failed: %s(%d)!\nTrying to reopen device...\n",
                                usb_strerror(), ret);
@@ -73,7 +78,7 @@ bool ReceiveCommandPoll(UsbCommand *c) {
                        printf(PROXPROMPT);
                        fflush(NULL);
 
-                       return 0;
+                       return false;
                }
        } else {
                if (ret && (ret < sizeof(UsbCommand))) {
@@ -96,11 +101,17 @@ bool ReceiveCommandPoll(UsbCommand *c) {
 #endif
        }
 
-       return ret > 0;
+       return ret != 0;
 }
 
 void ReceiveCommand(UsbCommand *c) {
-       while(!ReceiveCommandPoll(c)) {}
+//     printf("%s()\n", __FUNCTION__);
+       int retval = 0;
+       do {
+               retval = ReceiveCommandPoll(c);
+               if (retval != 1) printf("ReceiveCommandPoll returned %d\n", retval);
+       } while(retval<0);
+//     printf("recv %x\n", c->cmd);
 }
 
 usb_dev_handle* findProxmark(int verbose, unsigned int *iface) {
Impressum, Datenschutz