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"
28 unsigned int current_command
= CMD_UNKNOWN
;
29 unsigned int received_command
= CMD_UNKNOWN
;
30 UsbCommand current_response
;
31 UsbCommand current_response_user
;
33 static int CmdHelp(const char *Cmd
);
34 static int CmdQuit(const char *Cmd
);
36 //For storing command that are received from the device
37 #define CMD_BUFFER_SIZE 50
38 static UsbCommand cmdBuffer
[CMD_BUFFER_SIZE
];
39 //Points to the next empty position to write to
40 static int cmd_head
;//Starts as 0
41 //Points to the position of the last unread command
42 static int cmd_tail
= -1;//Starts as -1
44 static command_t CommandTable
[] =
46 {"help", CmdHelp
, 1, "This help. Use '<command> help' for details of the following commands:\n"},
47 {"data", CmdData
, 1, "{ Plot window / data buffer manipulation... }"},
48 {"exit", CmdQuit
, 1, "Exit program"},
49 {"hf", CmdHF
, 1, "{ HF commands... }"},
50 {"hw", CmdHW
, 1, "{ Hardware commands... }"},
51 {"lf", CmdLF
, 1, "{ LF commands... }"},
52 {"quit", CmdQuit
, 1, "Quit program"},
56 int CmdHelp(const char *Cmd
)
58 CmdsHelp(CommandTable
);
62 int CmdQuit(const char *Cmd
)
68 * Waits for a certain response type. This method waits for a maximum of
69 * ms_timeout milliseconds for a specified response command.
70 *@brief WaitForResponseTimeout
71 * @param cmd command to wait for
72 * @param response struct to copy received command into.
74 * @return true if command was returned, otherwise false
76 bool WaitForResponseTimeout(uint32_t cmd
, UsbCommand
* response
, size_t ms_timeout
) {
78 // Wait until the command is received
79 for(size_t dm_seconds
=0; dm_seconds
< ms_timeout
/10; dm_seconds
++) {
81 if(getCommand(response
) && response
->cmd
== cmd
){
82 //We got what we expected
85 msleep(10); // XXX ugh
86 if (dm_seconds
== 200) { // Two seconds elapsed
87 PrintAndLog("Waiting for a response from the proxmark...");
88 PrintAndLog("Don't forget to cancel its operation first by pressing on the button");
94 bool WaitForResponse(uint32_t cmd
, UsbCommand
* response
) {
95 return WaitForResponseTimeout(cmd
,response
,-1);
98 //-----------------------------------------------------------------------------
99 // Entry point into our code: called whenever the user types a command and
100 // then presses Enter, which the full command line that they typed.
101 //-----------------------------------------------------------------------------
102 void CommandReceived(char *Cmd
) {
103 CmdsParse(CommandTable
, Cmd
);
106 //-----------------------------------------------------------------------------
107 // Entry point into our code: called whenever we received a packet over USB
108 // that we weren't necessarily expecting, for example a debug print.
109 //-----------------------------------------------------------------------------
110 void UsbCommandReceived(UsbCommand
*UC
)
114 printf("UsbCommand length[len=%zd]\n",sizeof(UsbCommand));
115 printf(" cmd[len=%zd]: %"llx"\n",sizeof(UC->cmd),UC->cmd);
116 printf(" arg0[len=%zd]: %"llx"\n",sizeof(UC->arg[0]),UC->arg[0]);
117 printf(" arg1[len=%zd]: %"llx"\n",sizeof(UC->arg[1]),UC->arg[1]);
118 printf(" arg2[len=%zd]: %"llx"\n",sizeof(UC->arg[2]),UC->arg[2]);
119 printf(" data[len=%zd]: %02x%02x%02x...\n",sizeof(UC->d.asBytes),UC->d.asBytes[0],UC->d.asBytes[1],UC->d.asBytes[2]);
122 // printf("%s(%x) current cmd = %x\n", __FUNCTION__, c->cmd, current_command);
123 // If we recognize a response, return to avoid further processing
125 // First check if we are handling a debug message
126 case CMD_DEBUG_PRINT_STRING
: {
127 char s
[USB_CMD_DATA_SIZE
+1];
128 size_t len
= MIN(UC
->arg
[0],USB_CMD_DATA_SIZE
);
129 memcpy(s
,UC
->d
.asBytes
,len
);
131 PrintAndLog("#db# %s ", s
);
135 case CMD_DEBUG_PRINT_INTEGERS
: {
136 PrintAndLog("#db# %08x, %08x, %08x \r\n", UC
->arg
[0], UC
->arg
[1], UC
->arg
[2]);
140 case CMD_MEASURED_ANTENNA_TUNING
: {
142 int vLf125
, vLf134
, vHf
;
143 vLf125
= UC
->arg
[0] & 0xffff;
144 vLf134
= UC
->arg
[0] >> 16;
145 vHf
= UC
->arg
[1] & 0xffff;;
146 peakf
= UC
->arg
[2] & 0xffff;
147 peakv
= UC
->arg
[2] >> 16;
149 PrintAndLog("# LF antenna: %5.2f V @ 125.00 kHz", vLf125
/1000.0);
150 PrintAndLog("# LF antenna: %5.2f V @ 134.00 kHz", vLf134
/1000.0);
151 PrintAndLog("# LF optimal: %5.2f V @%9.2f kHz", peakv
/1000.0, 12000.0/(peakf
+1));
152 PrintAndLog("# HF antenna: %5.2f V @ 13.56 MHz", vHf
/1000.0);
154 PrintAndLog("# Your LF antenna is unusable.");
155 else if (peakv
<10000)
156 PrintAndLog("# Your LF antenna is marginal.");
158 PrintAndLog("# Your HF antenna is unusable.");
160 PrintAndLog("# Your HF antenna is marginal.");
163 case CMD_DOWNLOADED_RAW_ADC_SAMPLES_125K
: {
164 // printf("received samples: ");
165 // print_hex(UC->d.asBytes,512);
166 sample_buf_len
+= UC
->arg
[1];
167 // printf("samples: %zd offset: %d\n",sample_buf_len,UC->arg[0]);
168 memcpy(sample_buf
+(UC
->arg
[0]),UC
->d
.asBytes
,UC
->arg
[1]);
173 // PrintAndLog("Receive ACK\n");
177 // Maybe it's a response
178 switch(current_command
) {
179 case CMD_DOWNLOAD_RAW_ADC_SAMPLES_125K
: {
180 if (UC
->cmd
!= CMD_DOWNLOADED_RAW_ADC_SAMPLES_125K
) {
181 PrintAndLog("unrecognized command %08x\n", UC
->cmd
);
185 PrintAndLog("received samples %d\n",UC
->arg
[0]);
186 memcpy(sample_buf
+UC
->arg
[0],UC
->d
.asBytes
,48);
187 sample_buf_len
+= 48;
188 // for(i=0; i<48; i++) sample_buf[i] = UC->d.asBytes[i];
189 received_command
= UC
->cmd
;
202 * @brief storeCommand stores a USB command in a circular buffer
205 void storeCommand(UsbCommand
*command
)
207 if(cmd_head
== cmd_tail
)
209 //If these two are equal, we're about to overwrite in the
211 PrintAndLog("WARNING: Command buffer about to overwrite command! This needs to be fixed!");
213 //Store the command at the 'head' location
214 UsbCommand
* destination
= &cmdBuffer
[cmd_head
];
215 memcpy(destination
, command
, sizeof(UsbCommand
));
217 //Also, if cmd_tail is still -1 because the buffer was
218 // previously empty, set it to head
223 cmd_head
++; //increment head
224 cmd_head
%= CMD_BUFFER_SIZE
;//wrap around
228 * @brief getCommand gets a command from an internal circular buffer.
229 * @param response location to write command
230 * @return 1 if response was returned, 0 if nothing has been received
232 int getCommand(UsbCommand
* response
)
234 //If head == tail, there's nothing to read, or if we just got initialized
235 if(cmd_head
== cmd_tail
|| cmd_tail
== -1){
238 //Pick out the next unread command
239 UsbCommand
* last_unread
= &cmdBuffer
[cmd_tail
];
240 memcpy(response
, last_unread
, sizeof(UsbCommand
));
241 //Increment tail - this is a circular buffer, so modulo buffer size
242 cmd_tail
= (cmd_tail
+1 ) % CMD_BUFFER_SIZE
;
243 //In order to detect when the buffer overwrites itself, we set the
244 // tail to -1 whenever it 'catches up' with head : this means the buffer is empty.
245 // Otherwise, head==tail could mean both: either empty or full.
246 if(cmd_tail
== cmd_head
){