]> cvs.zerfleddert.de Git - proxmark3-svn/blob - client/cmdmain.c
Finalized migration to new USB CDC interface
[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 unsigned int current_command = CMD_UNKNOWN;
29 unsigned int received_command = CMD_UNKNOWN;
30 UsbCommand current_response;
31 UsbCommand current_response_user;
32
33 static int CmdHelp(const char *Cmd);
34 static int CmdQuit(const char *Cmd);
35
36 static command_t CommandTable[] =
37 {
38 {"help", CmdHelp, 1, "This help. Use '<command> help' for details of the following commands:\n"},
39 {"data", CmdData, 1, "{ Plot window / data buffer manipulation... }"},
40 {"exit", CmdQuit, 1, "Exit program"},
41 {"hf", CmdHF, 1, "{ HF commands... }"},
42 {"hw", CmdHW, 1, "{ Hardware commands... }"},
43 {"lf", CmdLF, 1, "{ LF commands... }"},
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
60 bool WaitForResponseTimeout(uint32_t cmd, UsbCommand* response, size_t ms_timeout) {
61
62 // Wait until the command is received
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
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 }
69 }
70
71 // Check if timeout occured
72 if(received_command != cmd) return false;
73
74 // Copy the received response (if supplied)
75 if (response) {
76 memcpy(response, &current_response, sizeof(UsbCommand));
77 }
78
79 // Reset the received command
80 received_command = CMD_UNKNOWN;
81
82 return true;
83 }
84
85 bool WaitForResponse(uint32_t cmd, UsbCommand* response) {
86 return WaitForResponseTimeout(cmd,response,-1);
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 //-----------------------------------------------------------------------------
93 void CommandReceived(char *Cmd) {
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 {
103 /*
104 // Debug
105 printf("UsbCommand length[len=%zd]\n",sizeof(UsbCommand));
106 printf(" cmd[len=%zd]: %"llx"\n",sizeof(UC->cmd),UC->cmd);
107 printf(" arg0[len=%zd]: %"llx"\n",sizeof(UC->arg[0]),UC->arg[0]);
108 printf(" arg1[len=%zd]: %"llx"\n",sizeof(UC->arg[1]),UC->arg[1]);
109 printf(" arg2[len=%zd]: %"llx"\n",sizeof(UC->arg[2]),UC->arg[2]);
110 printf(" data[len=%zd]: %02x%02x%02x...\n",sizeof(UC->d.asBytes),UC->d.asBytes[0],UC->d.asBytes[1],UC->d.asBytes[2]);
111 */
112
113 // printf("%s(%x) current cmd = %x\n", __FUNCTION__, c->cmd, current_command);
114 // If we recognize a response, return to avoid further processing
115 switch(UC->cmd) {
116 // First check if we are handling a debug message
117 case CMD_DEBUG_PRINT_STRING: {
118 char s[USB_CMD_DATA_SIZE+1];
119 size_t len = MIN(UC->arg[0],USB_CMD_DATA_SIZE);
120 memcpy(s,UC->d.asBytes,len);
121 s[len] = 0x00;
122 PrintAndLog("#db# %s ", s);
123 return;
124 } break;
125
126 case CMD_DEBUG_PRINT_INTEGERS: {
127 PrintAndLog("#db# %08x, %08x, %08x \r\n", UC->arg[0], UC->arg[1], UC->arg[2]);
128 return;
129 } break;
130
131 case CMD_MEASURED_ANTENNA_TUNING: {
132 int peakv, peakf;
133 int vLf125, vLf134, vHf;
134 vLf125 = UC->arg[0] & 0xffff;
135 vLf134 = UC->arg[0] >> 16;
136 vHf = UC->arg[1] & 0xffff;;
137 peakf = UC->arg[2] & 0xffff;
138 peakv = UC->arg[2] >> 16;
139 PrintAndLog("");
140 PrintAndLog("# LF antenna: %5.2f V @ 125.00 kHz", vLf125/1000.0);
141 PrintAndLog("# LF antenna: %5.2f V @ 134.00 kHz", vLf134/1000.0);
142 PrintAndLog("# LF optimal: %5.2f V @%9.2f kHz", peakv/1000.0, 12000.0/(peakf+1));
143 PrintAndLog("# HF antenna: %5.2f V @ 13.56 MHz", vHf/1000.0);
144 if (peakv<2000)
145 PrintAndLog("# Your LF antenna is unusable.");
146 else if (peakv<10000)
147 PrintAndLog("# Your LF antenna is marginal.");
148 if (vHf<2000)
149 PrintAndLog("# Your HF antenna is unusable.");
150 else if (vHf<5000)
151 PrintAndLog("# Your HF antenna is marginal.");
152 } break;
153
154 case CMD_DOWNLOADED_RAW_ADC_SAMPLES_125K: {
155 // printf("received samples: ");
156 // print_hex(UC->d.asBytes,512);
157 sample_buf_len += UC->arg[1];
158 // printf("samples: %zd offset: %d\n",sample_buf_len,UC->arg[0]);
159 memcpy(sample_buf+(UC->arg[0]),UC->d.asBytes,UC->arg[1]);
160 } break;
161
162
163 // case CMD_ACK: {
164 // PrintAndLog("Receive ACK\n");
165 // } break;
166
167 default: {
168 // Maybe it's a response
169 switch(current_command) {
170 case CMD_DOWNLOAD_RAW_ADC_SAMPLES_125K: {
171 if (UC->cmd != CMD_DOWNLOADED_RAW_ADC_SAMPLES_125K) {
172 PrintAndLog("unrecognized command %08x\n", UC->cmd);
173 break;
174 }
175 // int i;
176 PrintAndLog("received samples %d\n",UC->arg[0]);
177 memcpy(sample_buf+UC->arg[0],UC->d.asBytes,48);
178 sample_buf_len += 48;
179 // for(i=0; i<48; i++) sample_buf[i] = UC->d.asBytes[i];
180 received_command = UC->cmd;
181 } break;
182
183 default: {
184 } break;
185 }
186 // // Store the last received command
187 // memcpy(&current_response, UC, sizeof(UsbCommand));
188 // received_command = UC->cmd;
189 } break;
190 }
191 // Store the last received command
192 memcpy(&current_response, UC, sizeof(UsbCommand));
193 received_command = UC->cmd;
194 /*
195 // Maybe it's a response:
196 switch(current_command) {
197 case CMD_DOWNLOAD_RAW_ADC_SAMPLES_125K:
198 if (UC->cmd != CMD_DOWNLOADED_RAW_ADC_SAMPLES_125K) goto unexpected_response;
199 int i;
200 for(i=0; i<48; i++) sample_buf[i] = UC->d.asBytes[i];
201 received_command = UC->cmd;
202 return;
203 case CMD_ACQUIRE_RAW_ADC_SAMPLES_125K:
204 case CMD_DOWNLOADED_SIM_SAMPLES_125K:
205 if (UC->cmd != CMD_ACK) goto unexpected_response;
206 // got ACK
207 received_command = UC->cmd;
208 return;
209 default:
210 unexpected_response:
211
212 if(UC->cmd != CMD_ACK)
213 PrintAndLog("unrecognized command %08x \n", UC->cmd);
214 else
215 memcpy(&current_response, UC, sizeof(UsbCommand));
216 received_command = UC->cmd;
217 }
218 */
219 }
Impressum, Datenschutz