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