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