]> git.zerfleddert.de Git - proxmark3-svn/blob - client/cmdmain.c
18515f6589a123a4050ce586d75391cb7c5a3c99
[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 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 static command_t CommandTable[] =
38 {
39 {"help", CmdHelp, 1, "This help. Use '<command> help' for details of the following commands:\n"},
40 {"data", CmdData, 1, "{ Plot window / data buffer manipulation... }"},
41 {"exit", CmdQuit, 1, "Exit program"},
42 {"hf", CmdHF, 1, "{ HF commands... }"},
43 {"hw", CmdHW, 1, "{ Hardware commands... }"},
44 {"lf", CmdLF, 1, "{ LF commands... }"},
45 {"quit", CmdQuit, 1, "Quit program"},
46 {"script", CmdScript, 1,"Run script"},
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
62 bool WaitForResponseTimeout(uint32_t cmd, UsbCommand* response, size_t ms_timeout) {
63
64 // Wait until the command is received
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
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 }
71 }
72
73 // Check if timeout occured
74 if(received_command != cmd) return false;
75
76 // Copy the received response (if supplied)
77 if (response) {
78 memcpy(response, &current_response, sizeof(UsbCommand));
79 }
80
81 // Reset the received command
82 received_command = CMD_UNKNOWN;
83
84 return true;
85 }
86
87 bool WaitForResponse(uint32_t cmd, UsbCommand* response) {
88 return WaitForResponseTimeout(cmd,response,-1);
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 //-----------------------------------------------------------------------------
95 void CommandReceived(char *Cmd) {
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 {
105 /*
106 // Debug
107 printf("UsbCommand length[len=%zd]\n",sizeof(UsbCommand));
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]);
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 */
114
115 // printf("%s(%x) current cmd = %x\n", __FUNCTION__, c->cmd, current_command);
116 // If we recognize a response, return to avoid further processing
117 switch(UC->cmd) {
118 // First check if we are handling a debug message
119 case CMD_DEBUG_PRINT_STRING: {
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;
124 PrintAndLog("#db# %s ", s);
125 return;
126 } break;
127
128 case CMD_DEBUG_PRINT_INTEGERS: {
129 PrintAndLog("#db# %08x, %08x, %08x \r\n", UC->arg[0], UC->arg[1], UC->arg[2]);
130 return;
131 } break;
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("");
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.");
154 } break;
155
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
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 }
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];
182 received_command = UC->cmd;
183 } break;
184
185 default: {
186 } break;
187 }
188 // // Store the last received command
189 // memcpy(&current_response, UC, sizeof(UsbCommand));
190 // received_command = UC->cmd;
191 } break;
192 }
193 // Store the last received command
194 memcpy(&current_response, UC, sizeof(UsbCommand));
195 received_command = UC->cmd;
196 /*
197 // Maybe it's a response:
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;
205 case CMD_ACQUIRE_RAW_ADC_SAMPLES_125K:
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:
213
214 if(UC->cmd != CMD_ACK)
215 PrintAndLog("unrecognized command %08x \n", UC->cmd);
216 else
217 memcpy(&current_response, UC, sizeof(UsbCommand));
218 received_command = UC->cmd;
219 }
220 */
221 }
Impressum, Datenschutz