1 //-----------------------------------------------------------------------------
2 // Copyright (C) 2010 iZsh <izsh at fail0verflow.com>
4 // This code is licensed to you under the terms of the GNU GPL, version 2 or,
5 // at your option, any later version. See the LICENSE.txt file for the text of
7 //-----------------------------------------------------------------------------
8 // Main command parser entry point
9 //-----------------------------------------------------------------------------
16 #include "cmdparser.h"
17 #include "proxmark3.h"
27 unsigned int current_command
= CMD_UNKNOWN
;
28 unsigned int received_command
= CMD_UNKNOWN
;
29 UsbCommand current_response
;
30 UsbCommand current_response_user
;
32 static int CmdHelp(const char *Cmd
);
33 static int CmdQuit(const char *Cmd
);
35 static command_t CommandTable
[] =
37 {"help", CmdHelp
, 1, "This help. Use '<command> help' for details of the following commands:\n"},
38 {"data", CmdData
, 1, "{ Plot window / data buffer manipulation... }"},
39 {"exit", CmdQuit
, 1, "Exit program"},
40 {"hf", CmdHF
, 1, "{ HF commands... }"},
41 {"hw", CmdHW
, 1, "{ Hardware commands... }"},
42 {"lf", CmdLF
, 1, "{ LF commands... }"},
43 {"quit", CmdQuit
, 1, "Quit program"},
47 int CmdHelp(const char *Cmd
)
49 CmdsHelp(CommandTable
);
53 int CmdQuit(const char *Cmd
)
59 bool WaitForResponseTimeout(uint32_t cmd
, UsbCommand
* response
, size_t ms_timeout
) {
61 // Wait until the command is received
62 for(size_t i
=0; received_command
!= cmd
&& i
< ms_timeout
/10; i
++) {
63 msleep(10); // XXX ugh
64 if (i
== 200) { // Two seconds elapsed
65 PrintAndLog("Waiting for a response from the proxmark...");
66 PrintAndLog("Don't forget to cancel its operation first by pressing on the button");
70 // Check if timeout occured
71 if(received_command
!= cmd
) return false;
73 // Copy the received response (if supplied)
75 memcpy(response
, ¤t_response
, sizeof(UsbCommand
));
78 // Reset the received command
79 received_command
= CMD_UNKNOWN
;
84 bool WaitForResponse(uint32_t cmd
, UsbCommand
* response
) {
85 return WaitForResponseTimeout(cmd
,response
,-1);
88 //-----------------------------------------------------------------------------
89 // Entry point into our code: called whenever the user types a command and
90 // then presses Enter, which the full command line that they typed.
91 //-----------------------------------------------------------------------------
92 void CommandReceived(char *Cmd
) {
93 CmdsParse(CommandTable
, Cmd
);
96 //-----------------------------------------------------------------------------
97 // Entry point into our code: called whenever we received a packet over USB
98 // that we weren't necessarily expecting, for example a debug print.
99 //-----------------------------------------------------------------------------
100 void UsbCommandReceived(UsbCommand
*UC
)
102 // printf("%s(%x) current cmd = %x\n", __FUNCTION__, c->cmd, current_command);
103 /* If we recognize a response, return to avoid further processing */
105 // First check if we are handling a debug message
106 case CMD_DEBUG_PRINT_STRING
: {
108 if(UC
->arg
[0] > 70 || UC
->arg
[0] < 0) {
111 memcpy(s
, UC
->d
.asBytes
, UC
->arg
[0]);
112 s
[UC
->arg
[0]] = '\0';
113 PrintAndLog("#db# %s ", s
);
117 case CMD_DEBUG_PRINT_INTEGERS
: {
118 PrintAndLog("#db# %08x, %08x, %08x \r\n", UC
->arg
[0], UC
->arg
[1], UC
->arg
[2]);
122 case CMD_MEASURED_ANTENNA_TUNING
: {
124 int vLf125
, vLf134
, vHf
;
125 vLf125
= UC
->arg
[0] & 0xffff;
126 vLf134
= UC
->arg
[0] >> 16;
127 vHf
= UC
->arg
[1] & 0xffff;;
128 peakf
= UC
->arg
[2] & 0xffff;
129 peakv
= UC
->arg
[2] >> 16;
131 PrintAndLog("# LF antenna: %5.2f V @ 125.00 kHz", vLf125
/1000.0);
132 PrintAndLog("# LF antenna: %5.2f V @ 134.00 kHz", vLf134
/1000.0);
133 PrintAndLog("# LF optimal: %5.2f V @%9.2f kHz", peakv
/1000.0, 12000.0/(peakf
+1));
134 PrintAndLog("# HF antenna: %5.2f V @ 13.56 MHz", vHf
/1000.0);
136 PrintAndLog("# Your LF antenna is unusable.");
137 else if (peakv
<10000)
138 PrintAndLog("# Your LF antenna is marginal.");
140 PrintAndLog("# Your HF antenna is unusable.");
142 PrintAndLog("# Your HF antenna is marginal.");
145 case CMD_DOWNLOADED_RAW_ADC_SAMPLES_125K
: {
146 // printf("received samples: ");
147 // print_hex(UC->d.asBytes,512);
148 sample_buf_len
+= UC
->arg
[1];
149 // printf("samples: %zd offset: %d\n",sample_buf_len,UC->arg[0]);
150 memcpy(sample_buf
+(UC
->arg
[0]),UC
->d
.asBytes
,UC
->arg
[1]);
155 // PrintAndLog("Receive ACK\n");
159 // Maybe it's a response
160 switch(current_command
) {
161 case CMD_DOWNLOAD_RAW_ADC_SAMPLES_125K
: {
162 if (UC
->cmd
!= CMD_DOWNLOADED_RAW_ADC_SAMPLES_125K
) {
163 PrintAndLog("unrecognized command %08x\n", UC
->cmd
);
167 PrintAndLog("received samples %d\n",UC
->arg
[0]);
168 memcpy(sample_buf
+UC
->arg
[0],UC
->d
.asBytes
,48);
169 sample_buf_len
+= 48;
170 // for(i=0; i<48; i++) sample_buf[i] = UC->d.asBytes[i];
171 received_command
= UC
->cmd
;
177 // // Store the last received command
178 // memcpy(¤t_response, UC, sizeof(UsbCommand));
179 // received_command = UC->cmd;
182 // Store the last received command
183 memcpy(¤t_response
, UC
, sizeof(UsbCommand
));
184 received_command
= UC
->cmd
;
186 // Maybe it's a response:
187 switch(current_command) {
188 case CMD_DOWNLOAD_RAW_ADC_SAMPLES_125K:
189 if (UC->cmd != CMD_DOWNLOADED_RAW_ADC_SAMPLES_125K) goto unexpected_response;
191 for(i=0; i<48; i++) sample_buf[i] = UC->d.asBytes[i];
192 received_command = UC->cmd;
194 case CMD_ACQUIRE_RAW_ADC_SAMPLES_125K:
195 case CMD_DOWNLOADED_SIM_SAMPLES_125K:
196 if (UC->cmd != CMD_ACK) goto unexpected_response;
198 received_command = UC->cmd;
203 if(UC->cmd != CMD_ACK)
204 PrintAndLog("unrecognized command %08x \n", UC->cmd);
206 memcpy(¤t_response, UC, sizeof(UsbCommand));
207 received_command = UC->cmd;