]> cvs.zerfleddert.de Git - proxmark3-svn/blob - client/cmdmain.c
Fixed error pointed out here: https://code.google.com/p/proxmark3/issues/detail?id...
[proxmark3-svn] / client / cmdmain.c
1 //-----------------------------------------------------------------------------
2 // Copyright (C) 2010 iZsh <izsh at fail0verflow.com>
3 //
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
6 // the license.
7 //-----------------------------------------------------------------------------
8 // Main command parser entry point
9 //-----------------------------------------------------------------------------
10
11 #include <stdio.h>
12 #include <stdlib.h>
13 #include <unistd.h>
14 #include <string.h>
15 #include "sleep.h"
16 #include "cmdparser.h"
17 #include "proxmark3.h"
18 #include "data.h"
19 #include "usb_cmd.h"
20 #include "ui.h"
21 #include "cmdhf.h"
22 #include "cmddata.h"
23 #include "cmdhw.h"
24 #include "cmdlf.h"
25 #include "cmdmain.h"
26 #include "util.h"
27
28 unsigned int current_command = CMD_UNKNOWN;
29 //unsigned int received_command = CMD_UNKNOWN;
30 //UsbCommand current_response;
31 //UsbCommand current_response_user;
32
33 static int CmdHelp(const char *Cmd);
34 static int CmdQuit(const char *Cmd);
35
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
43
44 static command_t CommandTable[] =
45 {
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"},
53 {NULL, NULL, 0, NULL}
54 };
55
56 int CmdHelp(const char *Cmd)
57 {
58 CmdsHelp(CommandTable);
59 return 0;
60 }
61
62 int CmdQuit(const char *Cmd)
63 {
64 exit(0);
65 return 0;
66 }
67 /**
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.
73 * @param ms_timeout
74 * @return true if command was returned, otherwise false
75 */
76 bool WaitForResponseTimeout(uint32_t cmd, UsbCommand* response, size_t ms_timeout) {
77
78 if (response == NULL) {
79 UsbCommand resp;
80 response = &resp;
81 }
82
83 // Wait until the command is received
84 for(size_t dm_seconds=0; dm_seconds < ms_timeout/10; dm_seconds++) {
85
86 if(getCommand(response) && response->cmd == cmd){
87 //We got what we expected
88 return true;
89 }
90 msleep(10); // XXX ugh
91 if (dm_seconds == 200) { // Two seconds elapsed
92 PrintAndLog("Waiting for a response from the proxmark...");
93 PrintAndLog("Don't forget to cancel its operation first by pressing on the button");
94 }
95 }
96 return false;
97 }
98
99 bool WaitForResponse(uint32_t cmd, UsbCommand* response) {
100 return WaitForResponseTimeout(cmd,response,-1);
101 }
102
103 //-----------------------------------------------------------------------------
104 // Entry point into our code: called whenever the user types a command and
105 // then presses Enter, which the full command line that they typed.
106 //-----------------------------------------------------------------------------
107 void CommandReceived(char *Cmd) {
108 CmdsParse(CommandTable, Cmd);
109 }
110
111 //-----------------------------------------------------------------------------
112 // Entry point into our code: called whenever we received a packet over USB
113 // that we weren't necessarily expecting, for example a debug print.
114 //-----------------------------------------------------------------------------
115 void UsbCommandReceived(UsbCommand *UC)
116 {
117 /*
118 // Debug
119 printf("UsbCommand length[len=%zd]\n",sizeof(UsbCommand));
120 printf(" cmd[len=%zd]: %"llx"\n",sizeof(UC->cmd),UC->cmd);
121 printf(" arg0[len=%zd]: %"llx"\n",sizeof(UC->arg[0]),UC->arg[0]);
122 printf(" arg1[len=%zd]: %"llx"\n",sizeof(UC->arg[1]),UC->arg[1]);
123 printf(" arg2[len=%zd]: %"llx"\n",sizeof(UC->arg[2]),UC->arg[2]);
124 printf(" data[len=%zd]: %02x%02x%02x...\n",sizeof(UC->d.asBytes),UC->d.asBytes[0],UC->d.asBytes[1],UC->d.asBytes[2]);
125 */
126
127 // printf("%s(%x) current cmd = %x\n", __FUNCTION__, c->cmd, current_command);
128 // If we recognize a response, return to avoid further processing
129 switch(UC->cmd) {
130 // First check if we are handling a debug message
131 case CMD_DEBUG_PRINT_STRING: {
132 char s[USB_CMD_DATA_SIZE+1];
133 size_t len = MIN(UC->arg[0],USB_CMD_DATA_SIZE);
134 memcpy(s,UC->d.asBytes,len);
135 s[len] = 0x00;
136 PrintAndLog("#db# %s ", s);
137 return;
138 } break;
139
140 case CMD_DEBUG_PRINT_INTEGERS: {
141 PrintAndLog("#db# %08x, %08x, %08x \r\n", UC->arg[0], UC->arg[1], UC->arg[2]);
142 return;
143 } break;
144
145 case CMD_MEASURED_ANTENNA_TUNING: {
146 int peakv, peakf;
147 int vLf125, vLf134, vHf;
148 vLf125 = UC->arg[0] & 0xffff;
149 vLf134 = UC->arg[0] >> 16;
150 vHf = UC->arg[1] & 0xffff;;
151 peakf = UC->arg[2] & 0xffff;
152 peakv = UC->arg[2] >> 16;
153 PrintAndLog("");
154 PrintAndLog("# LF antenna: %5.2f V @ 125.00 kHz", vLf125/1000.0);
155 PrintAndLog("# LF antenna: %5.2f V @ 134.00 kHz", vLf134/1000.0);
156 PrintAndLog("# LF optimal: %5.2f V @%9.2f kHz", peakv/1000.0, 12000.0/(peakf+1));
157 PrintAndLog("# HF antenna: %5.2f V @ 13.56 MHz", vHf/1000.0);
158 if (peakv<2000)
159 PrintAndLog("# Your LF antenna is unusable.");
160 else if (peakv<10000)
161 PrintAndLog("# Your LF antenna is marginal.");
162 if (vHf<2000)
163 PrintAndLog("# Your HF antenna is unusable.");
164 else if (vHf<5000)
165 PrintAndLog("# Your HF antenna is marginal.");
166 } break;
167
168 case CMD_DOWNLOADED_RAW_ADC_SAMPLES_125K: {
169 // printf("received samples: ");
170 // print_hex(UC->d.asBytes,512);
171 sample_buf_len += UC->arg[1];
172 // printf("samples: %zd offset: %d\n",sample_buf_len,UC->arg[0]);
173 memcpy(sample_buf+(UC->arg[0]),UC->d.asBytes,UC->arg[1]);
174 } break;
175
176
177 // case CMD_ACK: {
178 // PrintAndLog("Receive ACK\n");
179 // } break;
180
181 default: {
182 // Maybe it's a response
183 /*
184 switch(current_command) {
185 case CMD_DOWNLOAD_RAW_ADC_SAMPLES_125K: {
186 if (UC->cmd != CMD_DOWNLOADED_RAW_ADC_SAMPLES_125K) {
187 PrintAndLog("unrecognized command %08x\n", UC->cmd);
188 break;
189 }
190 // int i;
191 PrintAndLog("received samples %d\n",UC->arg[0]);
192 memcpy(sample_buf+UC->arg[0],UC->d.asBytes,48);
193 sample_buf_len += 48;
194 // for(i=0; i<48; i++) sample_buf[i] = UC->d.asBytes[i];
195 //received_command = UC->cmd;
196 } break;
197
198 default: {
199 } break;
200 }*/
201 }
202 break;
203 }
204
205 storeCommand(UC);
206
207 }
208 /**
209 * @brief storeCommand stores a USB command in a circular buffer
210 * @param UC
211 */
212 void storeCommand(UsbCommand *command)
213 {
214 if(cmd_head == cmd_tail)
215 {
216 //If these two are equal, we're about to overwrite in the
217 // circular buffer.
218 PrintAndLog("WARNING: Command buffer about to overwrite command! This needs to be fixed!");
219 }
220 //Store the command at the 'head' location
221 UsbCommand* destination = &cmdBuffer[cmd_head];
222 memcpy(destination, command, sizeof(UsbCommand));
223
224 //Also, if cmd_tail is still -1 because the buffer was
225 // previously empty, set it to head
226 if(cmd_tail < 0) {
227 cmd_tail = cmd_head;
228 }
229
230 cmd_head++; //increment head
231 cmd_head %= CMD_BUFFER_SIZE;//wrap around
232
233 }
234 /**
235 * @brief getCommand gets a command from an internal circular buffer.
236 * @param response location to write command
237 * @return 1 if response was returned, 0 if nothing has been received
238 */
239 int getCommand(UsbCommand* response)
240 {
241 //If head == tail, there's nothing to read, or if we just got initialized
242 if(cmd_head == cmd_tail || cmd_tail == -1){
243 return 0;
244 }
245 //Pick out the next unread command
246 UsbCommand* last_unread = &cmdBuffer[cmd_tail];
247 memcpy(response, last_unread, sizeof(UsbCommand));
248 //Increment tail - this is a circular buffer, so modulo buffer size
249 cmd_tail = (cmd_tail +1 ) % CMD_BUFFER_SIZE;
250 //In order to detect when the buffer overwrites itself, we set the
251 // tail to -1 whenever it 'catches up' with head : this means the buffer is empty.
252 // Otherwise, head==tail could mean both: either empty or full.
253 if(cmd_tail == cmd_head){
254 cmd_tail = -1 ;
255 }
256
257 return 1;
258
259 }
Impressum, Datenschutz