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