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