]>
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" |
806dc075 | 27 | #include "cmdscript.h" |
7fe9b0b7 | 28 | |
29 | unsigned int current_command = CMD_UNKNOWN; | |
30 | unsigned int received_command = CMD_UNKNOWN; | |
534983d7 | 31 | UsbCommand current_response; |
50193c1e | 32 | UsbCommand current_response_user; |
7fe9b0b7 | 33 | |
34 | static int CmdHelp(const char *Cmd); | |
35 | static int CmdQuit(const char *Cmd); | |
36 | ||
37 | static command_t CommandTable[] = | |
38 | { | |
c37d2e70 | 39 | {"help", CmdHelp, 1, "This help. Use '<command> help' for details of the following commands:\n"}, |
37239a7c | 40 | {"data", CmdData, 1, "{ Plot window / data buffer manipulation... }"}, |
7fe9b0b7 | 41 | {"exit", CmdQuit, 1, "Exit program"}, |
37239a7c | 42 | {"hf", CmdHF, 1, "{ HF commands... }"}, |
43 | {"hw", CmdHW, 1, "{ Hardware commands... }"}, | |
44 | {"lf", CmdLF, 1, "{ LF commands... }"}, | |
7fe9b0b7 | 45 | {"quit", CmdQuit, 1, "Quit program"}, |
806dc075 | 46 | {"script", CmdScript, 1,"Run script"}, |
7fe9b0b7 | 47 | {NULL, NULL, 0, NULL} |
48 | }; | |
49 | ||
50 | int CmdHelp(const char *Cmd) | |
51 | { | |
52 | CmdsHelp(CommandTable); | |
53 | return 0; | |
54 | } | |
55 | ||
56 | int CmdQuit(const char *Cmd) | |
57 | { | |
58 | exit(0); | |
59 | return 0; | |
60 | } | |
61 | ||
902cb3c0 | 62 | bool WaitForResponseTimeout(uint32_t cmd, UsbCommand* response, size_t ms_timeout) { |
534983d7 | 63 | |
902cb3c0 | 64 | // Wait until the command is received |
fe7bfa78 | 65 | for(size_t i=0; received_command != cmd && i < ms_timeout/10; i++) { |
66 | msleep(10); // XXX ugh | |
67 | if (i == 200) { // Two seconds elapsed | |
902cb3c0 | 68 | PrintAndLog("Waiting for a response from the proxmark..."); |
69 | PrintAndLog("Don't forget to cancel its operation first by pressing on the button"); | |
70 | } | |
534983d7 | 71 | } |
50193c1e | 72 | |
902cb3c0 | 73 | // Check if timeout occured |
74 | if(received_command != cmd) return false; | |
534983d7 | 75 | |
902cb3c0 | 76 | // Copy the received response (if supplied) |
77 | if (response) { | |
78 | memcpy(response, ¤t_response, sizeof(UsbCommand)); | |
79 | } | |
534983d7 | 80 | |
902cb3c0 | 81 | // Reset the received command |
82 | received_command = CMD_UNKNOWN; | |
534983d7 | 83 | |
902cb3c0 | 84 | return true; |
534983d7 | 85 | } |
86 | ||
902cb3c0 | 87 | bool WaitForResponse(uint32_t cmd, UsbCommand* response) { |
88 | return WaitForResponseTimeout(cmd,response,-1); | |
7fe9b0b7 | 89 | } |
90 | ||
91 | //----------------------------------------------------------------------------- | |
92 | // Entry point into our code: called whenever the user types a command and | |
93 | // then presses Enter, which the full command line that they typed. | |
94 | //----------------------------------------------------------------------------- | |
902cb3c0 | 95 | void CommandReceived(char *Cmd) { |
7fe9b0b7 | 96 | CmdsParse(CommandTable, Cmd); |
97 | } | |
98 | ||
99 | //----------------------------------------------------------------------------- | |
100 | // Entry point into our code: called whenever we received a packet over USB | |
101 | // that we weren't necessarily expecting, for example a debug print. | |
102 | //----------------------------------------------------------------------------- | |
103 | void UsbCommandReceived(UsbCommand *UC) | |
104 | { | |
79a73ab2 | 105 | /* |
9440213d | 106 | // Debug |
79a73ab2 | 107 | printf("UsbCommand length[len=%zd]\n",sizeof(UsbCommand)); |
125a98a1 | 108 | printf(" cmd[len=%zd]: %"llx"\n",sizeof(UC->cmd),UC->cmd); |
109 | printf(" arg0[len=%zd]: %"llx"\n",sizeof(UC->arg[0]),UC->arg[0]); | |
110 | printf(" arg1[len=%zd]: %"llx"\n",sizeof(UC->arg[1]),UC->arg[1]); | |
111 | printf(" arg2[len=%zd]: %"llx"\n",sizeof(UC->arg[2]),UC->arg[2]); | |
79a73ab2 | 112 | printf(" data[len=%zd]: %02x%02x%02x...\n",sizeof(UC->d.asBytes),UC->d.asBytes[0],UC->d.asBytes[1],UC->d.asBytes[2]); |
113 | */ | |
9440213d | 114 | |
7fe9b0b7 | 115 | // printf("%s(%x) current cmd = %x\n", __FUNCTION__, c->cmd, current_command); |
9440213d | 116 | // If we recognize a response, return to avoid further processing |
7fe9b0b7 | 117 | switch(UC->cmd) { |
9440213d | 118 | // First check if we are handling a debug message |
7fe9b0b7 | 119 | case CMD_DEBUG_PRINT_STRING: { |
9440213d | 120 | char s[USB_CMD_DATA_SIZE+1]; |
121 | size_t len = MIN(UC->arg[0],USB_CMD_DATA_SIZE); | |
122 | memcpy(s,UC->d.asBytes,len); | |
123 | s[len] = 0x00; | |
ab8b654e | 124 | PrintAndLog("#db# %s ", s); |
7fe9b0b7 | 125 | return; |
db09cb3a | 126 | } break; |
7fe9b0b7 | 127 | |
db09cb3a | 128 | case CMD_DEBUG_PRINT_INTEGERS: { |
ab8b654e | 129 | PrintAndLog("#db# %08x, %08x, %08x \r\n", UC->arg[0], UC->arg[1], UC->arg[2]); |
7fe9b0b7 | 130 | return; |
db09cb3a | 131 | } break; |
7fe9b0b7 | 132 | |
133 | case CMD_MEASURED_ANTENNA_TUNING: { | |
134 | int peakv, peakf; | |
135 | int vLf125, vLf134, vHf; | |
136 | vLf125 = UC->arg[0] & 0xffff; | |
137 | vLf134 = UC->arg[0] >> 16; | |
138 | vHf = UC->arg[1] & 0xffff;; | |
139 | peakf = UC->arg[2] & 0xffff; | |
140 | peakv = UC->arg[2] >> 16; | |
141 | PrintAndLog(""); | |
7fe9b0b7 | 142 | PrintAndLog("# LF antenna: %5.2f V @ 125.00 kHz", vLf125/1000.0); |
143 | PrintAndLog("# LF antenna: %5.2f V @ 134.00 kHz", vLf134/1000.0); | |
144 | PrintAndLog("# LF optimal: %5.2f V @%9.2f kHz", peakv/1000.0, 12000.0/(peakf+1)); | |
145 | PrintAndLog("# HF antenna: %5.2f V @ 13.56 MHz", vHf/1000.0); | |
146 | if (peakv<2000) | |
147 | PrintAndLog("# Your LF antenna is unusable."); | |
148 | else if (peakv<10000) | |
149 | PrintAndLog("# Your LF antenna is marginal."); | |
150 | if (vHf<2000) | |
151 | PrintAndLog("# Your HF antenna is unusable."); | |
152 | else if (vHf<5000) | |
153 | PrintAndLog("# Your HF antenna is marginal."); | |
db09cb3a | 154 | } break; |
155 | ||
902cb3c0 | 156 | case CMD_DOWNLOADED_RAW_ADC_SAMPLES_125K: { |
157 | // printf("received samples: "); | |
158 | // print_hex(UC->d.asBytes,512); | |
159 | sample_buf_len += UC->arg[1]; | |
160 | // printf("samples: %zd offset: %d\n",sample_buf_len,UC->arg[0]); | |
161 | memcpy(sample_buf+(UC->arg[0]),UC->d.asBytes,UC->arg[1]); | |
162 | } break; | |
163 | ||
164 | ||
165 | // case CMD_ACK: { | |
166 | // PrintAndLog("Receive ACK\n"); | |
167 | // } break; | |
168 | ||
db09cb3a | 169 | default: { |
170 | // Maybe it's a response | |
171 | switch(current_command) { | |
172 | case CMD_DOWNLOAD_RAW_ADC_SAMPLES_125K: { | |
173 | if (UC->cmd != CMD_DOWNLOADED_RAW_ADC_SAMPLES_125K) { | |
174 | PrintAndLog("unrecognized command %08x\n", UC->cmd); | |
175 | break; | |
176 | } | |
902cb3c0 | 177 | // int i; |
178 | PrintAndLog("received samples %d\n",UC->arg[0]); | |
179 | memcpy(sample_buf+UC->arg[0],UC->d.asBytes,48); | |
180 | sample_buf_len += 48; | |
181 | // for(i=0; i<48; i++) sample_buf[i] = UC->d.asBytes[i]; | |
db09cb3a | 182 | received_command = UC->cmd; |
183 | } break; | |
184 | ||
185 | default: { | |
186 | } break; | |
187 | } | |
902cb3c0 | 188 | // // Store the last received command |
189 | // memcpy(¤t_response, UC, sizeof(UsbCommand)); | |
190 | // received_command = UC->cmd; | |
db09cb3a | 191 | } break; |
7fe9b0b7 | 192 | } |
902cb3c0 | 193 | // Store the last received command |
194 | memcpy(¤t_response, UC, sizeof(UsbCommand)); | |
db09cb3a | 195 | received_command = UC->cmd; |
196 | /* | |
197 | // Maybe it's a response: | |
7fe9b0b7 | 198 | switch(current_command) { |
199 | case CMD_DOWNLOAD_RAW_ADC_SAMPLES_125K: | |
200 | if (UC->cmd != CMD_DOWNLOADED_RAW_ADC_SAMPLES_125K) goto unexpected_response; | |
201 | int i; | |
202 | for(i=0; i<48; i++) sample_buf[i] = UC->d.asBytes[i]; | |
203 | received_command = UC->cmd; | |
204 | return; | |
bdd1de1b | 205 | case CMD_ACQUIRE_RAW_ADC_SAMPLES_125K: |
7fe9b0b7 | 206 | case CMD_DOWNLOADED_SIM_SAMPLES_125K: |
207 | if (UC->cmd != CMD_ACK) goto unexpected_response; | |
208 | // got ACK | |
209 | received_command = UC->cmd; | |
210 | return; | |
211 | default: | |
212 | unexpected_response: | |
534983d7 | 213 | |
214 | if(UC->cmd != CMD_ACK) | |
ab8b654e | 215 | PrintAndLog("unrecognized command %08x \n", UC->cmd); |
534983d7 | 216 | else |
217 | memcpy(¤t_response, UC, sizeof(UsbCommand)); | |
218 | received_command = UC->cmd; | |
7fe9b0b7 | 219 | } |
db09cb3a | 220 | */ |
c37d2e70 | 221 | } |