]> git.zerfleddert.de Git - proxmark3-svn/blob - client/cmdmain.c
Fixed indentation
[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 #include "cmdscript.h"
28
29
30 unsigned int current_command = CMD_UNKNOWN;
31 //unsigned int received_command = CMD_UNKNOWN;
32 //UsbCommand current_response;
33 //UsbCommand current_response_user;
34
35 static int CmdHelp(const char *Cmd);
36 static int CmdQuit(const char *Cmd);
37
38 //For storing command that are received from the device
39 #define CMD_BUFFER_SIZE 50
40 static UsbCommand cmdBuffer[CMD_BUFFER_SIZE];
41 //Points to the next empty position to write to
42 static int cmd_head;//Starts as 0
43 //Points to the position of the last unread command
44 static int cmd_tail;//Starts as 0
45
46 static command_t CommandTable[] =
47 {
48 {"help", CmdHelp, 1, "This help. Use '<command> help' for details of a particular command."},
49 {"data", CmdData, 1, "{ Plot window / data buffer manipulation... }"},
50 {"hf", CmdHF, 1, "{ HF commands... }"},
51 {"hw", CmdHW, 1, "{ Hardware commands... }"},
52 {"lf", CmdLF, 1, "{ LF commands... }"},
53 {"script", CmdScript, 1,"{ Scripting commands }"},
54 {"quit", CmdQuit, 1, "Exit program"},
55 {"exit", CmdQuit, 1, "Exit program"},
56 {NULL, NULL, 0, NULL}
57 };
58
59 command_t* getTopLevelCommandTable()
60 {
61 return CommandTable;
62 }
63 int CmdHelp(const char *Cmd)
64 {
65 CmdsHelp(CommandTable);
66 return 0;
67 }
68
69 int CmdQuit(const char *Cmd)
70 {
71 exit(0);
72 return 0;
73 }
74 /**
75 * @brief This method should be called when sending a new command to the pm3. In case any old
76 * responses from previous commands are stored in the buffer, a call to this method should clear them.
77 * A better method could have been to have explicit command-ACKS, so we can know which ACK goes to which
78 * operation. Right now we'll just have to live with this.
79 */
80 void clearCommandBuffer()
81 {
82 //This is a very simple operation
83 cmd_tail = cmd_head;
84 }
85
86 /**
87 * @brief storeCommand stores a USB command in a circular buffer
88 * @param UC
89 */
90 void storeCommand(UsbCommand *command)
91 {
92 if( ( cmd_head+1) % CMD_BUFFER_SIZE == cmd_tail)
93 {
94 //If these two are equal, we're about to overwrite in the
95 // circular buffer.
96 PrintAndLog("WARNING: Command buffer about to overwrite command! This needs to be fixed!");
97 }
98 //Store the command at the 'head' location
99 UsbCommand* destination = &cmdBuffer[cmd_head];
100 memcpy(destination, command, sizeof(UsbCommand));
101
102 cmd_head = (cmd_head +1) % CMD_BUFFER_SIZE; //increment head and wrap
103
104 }
105 /**
106 * @brief getCommand gets a command from an internal circular buffer.
107 * @param response location to write command
108 * @return 1 if response was returned, 0 if nothing has been received
109 */
110 int getCommand(UsbCommand* response)
111 {
112 //If head == tail, there's nothing to read, or if we just got initialized
113 if(cmd_head == cmd_tail){
114 return 0;
115 }
116 //Pick out the next unread command
117 UsbCommand* last_unread = &cmdBuffer[cmd_tail];
118 memcpy(response, last_unread, sizeof(UsbCommand));
119 //Increment tail - this is a circular buffer, so modulo buffer size
120 cmd_tail = (cmd_tail +1 ) % CMD_BUFFER_SIZE;
121
122 return 1;
123
124 }
125
126 /**
127 * Waits for a certain response type. This method waits for a maximum of
128 * ms_timeout milliseconds for a specified response command.
129 *@brief WaitForResponseTimeout
130 * @param cmd command to wait for
131 * @param response struct to copy received command into.
132 * @param ms_timeout
133 * @return true if command was returned, otherwise false
134 */
135 bool WaitForResponseTimeout(uint32_t cmd, UsbCommand* response, size_t ms_timeout) {
136
137 UsbCommand resp;
138
139 if (response == NULL) {
140 response = &resp;
141 }
142
143 // Wait until the command is received
144 for(size_t dm_seconds=0; dm_seconds < ms_timeout/10; dm_seconds++) {
145
146 while(getCommand(response))
147 {
148 if(response->cmd == cmd){
149 //We got what we expected
150 return true;
151 }
152
153 }
154 msleep(10); // XXX ugh
155 if (dm_seconds == 200) { // Two seconds elapsed
156 PrintAndLog("Waiting for a response from the proxmark...");
157 PrintAndLog("Don't forget to cancel its operation first by pressing on the button");
158 }
159 }
160 return false;
161 }
162
163 bool WaitForResponse(uint32_t cmd, UsbCommand* response) {
164 return WaitForResponseTimeout(cmd,response,-1);
165 }
166
167 //-----------------------------------------------------------------------------
168 // Entry point into our code: called whenever the user types a command and
169 // then presses Enter, which the full command line that they typed.
170 //-----------------------------------------------------------------------------
171 void CommandReceived(char *Cmd) {
172 CmdsParse(CommandTable, Cmd);
173 }
174
175 //-----------------------------------------------------------------------------
176 // Entry point into our code: called whenever we received a packet over USB
177 // that we weren't necessarily expecting, for example a debug print.
178 //-----------------------------------------------------------------------------
179 void UsbCommandReceived(UsbCommand *UC)
180 {
181 /*
182 // Debug
183 printf("UsbCommand length[len=%zd]\n",sizeof(UsbCommand));
184 printf(" cmd[len=%zd]: %"llx"\n",sizeof(UC->cmd),UC->cmd);
185 printf(" arg0[len=%zd]: %"llx"\n",sizeof(UC->arg[0]),UC->arg[0]);
186 printf(" arg1[len=%zd]: %"llx"\n",sizeof(UC->arg[1]),UC->arg[1]);
187 printf(" arg2[len=%zd]: %"llx"\n",sizeof(UC->arg[2]),UC->arg[2]);
188 printf(" data[len=%zd]: %02x%02x%02x...\n",sizeof(UC->d.asBytes),UC->d.asBytes[0],UC->d.asBytes[1],UC->d.asBytes[2]);
189 */
190
191 // printf("%s(%x) current cmd = %x\n", __FUNCTION__, c->cmd, current_command);
192 // If we recognize a response, return to avoid further processing
193 switch(UC->cmd) {
194 // First check if we are handling a debug message
195 case CMD_DEBUG_PRINT_STRING: {
196 char s[USB_CMD_DATA_SIZE+1];
197 size_t len = MIN(UC->arg[0],USB_CMD_DATA_SIZE);
198 memcpy(s,UC->d.asBytes,len);
199 s[len] = 0x00;
200 PrintAndLog("#db# %s ", s);
201 return;
202 } break;
203
204 case CMD_DEBUG_PRINT_INTEGERS: {
205 PrintAndLog("#db# %08x, %08x, %08x \r\n", UC->arg[0], UC->arg[1], UC->arg[2]);
206 return;
207 } break;
208
209 // case CMD_MEASURED_ANTENNA_TUNING: {
210 // int peakv, peakf;
211 // int vLf125, vLf134, vHf;
212 // vLf125 = UC->arg[0] & 0xffff;
213 // vLf134 = UC->arg[0] >> 16;
214 // vHf = UC->arg[1] & 0xffff;;
215 // peakf = UC->arg[2] & 0xffff;
216 // peakv = UC->arg[2] >> 16;
217 // PrintAndLog("");
218 // PrintAndLog("# LF antenna: %5.2f V @ 125.00 kHz", vLf125/1000.0);
219 // PrintAndLog("# LF antenna: %5.2f V @ 134.00 kHz", vLf134/1000.0);
220 // PrintAndLog("# LF optimal: %5.2f V @%9.2f kHz", peakv/1000.0, 12000.0/(peakf+1));
221 // PrintAndLog("# HF antenna: %5.2f V @ 13.56 MHz", vHf/1000.0);
222 // if (peakv<2000)
223 // PrintAndLog("# Your LF antenna is unusable.");
224 // else if (peakv<10000)
225 // PrintAndLog("# Your LF antenna is marginal.");
226 // if (vHf<2000)
227 // PrintAndLog("# Your HF antenna is unusable.");
228 // else if (vHf<5000)
229 // PrintAndLog("# Your HF antenna is marginal.");
230 // } break;
231
232 case CMD_DOWNLOADED_RAW_ADC_SAMPLES_125K: {
233 // printf("received samples: ");
234 // print_hex(UC->d.asBytes,512);
235 sample_buf_len += UC->arg[1];
236 // printf("samples: %zd offset: %d\n",sample_buf_len,UC->arg[0]);
237 memcpy(sample_buf+(UC->arg[0]),UC->d.asBytes,UC->arg[1]);
238 } break;
239
240
241 // case CMD_ACK: {
242 // PrintAndLog("Receive ACK\n");
243 // } break;
244
245 default: {
246 // Maybe it's a response
247 /*
248 switch(current_command) {
249 case CMD_DOWNLOAD_RAW_ADC_SAMPLES_125K: {
250 if (UC->cmd != CMD_DOWNLOADED_RAW_ADC_SAMPLES_125K) {
251 PrintAndLog("unrecognized command %08x\n", UC->cmd);
252 break;
253 }
254 // int i;
255 PrintAndLog("received samples %d\n",UC->arg[0]);
256 memcpy(sample_buf+UC->arg[0],UC->d.asBytes,48);
257 sample_buf_len += 48;
258 // for(i=0; i<48; i++) sample_buf[i] = UC->d.asBytes[i];
259 //received_command = UC->cmd;
260 } break;
261
262 default: {
263 } break;
264 }*/
265 }
266 break;
267 }
268
269 storeCommand(UC);
270
271 }
272
Impressum, Datenschutz