]> git.zerfleddert.de Git - proxmark3-svn/blame - client/cmdmain.c
Applied Holiman's fixes for iclass.c and CSNs
[proxmark3-svn] / client / cmdmain.c
CommitLineData
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"
f38a1528 19#include "../include/usb_cmd.h"
7fe9b0b7 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"
1d59cd8d 27#include "cmdscript.h"
7fe9b0b7 28
72e930ef 29int delta125[2];
30int delta134[2];
31int deltahf[2];
32int deltaReset = 0;
e772353f 33
7fe9b0b7 34unsigned int current_command = CMD_UNKNOWN;
fd368d18 35//unsigned int received_command = CMD_UNKNOWN;
36//UsbCommand current_response;
37//UsbCommand current_response_user;
7fe9b0b7 38
39static int CmdHelp(const char *Cmd);
40static int CmdQuit(const char *Cmd);
41
fd368d18 42//For storing command that are received from the device
43#define CMD_BUFFER_SIZE 50
44static UsbCommand cmdBuffer[CMD_BUFFER_SIZE];
45//Points to the next empty position to write to
46static int cmd_head;//Starts as 0
47//Points to the position of the last unread command
48static int cmd_tail;//Starts as 0
49
7fe9b0b7 50static command_t CommandTable[] =
51{
57c69556 52 {"help", CmdHelp, 1, "This help. Use '<command> help' for details of a particular command."},
37239a7c 53 {"data", CmdData, 1, "{ Plot window / data buffer manipulation... }"},
c6be64da 54 {"hf", CmdHF, 1, "{ High Frequency commands... }"},
37239a7c 55 {"hw", CmdHW, 1, "{ Hardware commands... }"},
c6be64da 56 {"lf", CmdLF, 1, "{ Low Frequency commands... }"},
57c69556
MHS
57 {"script", CmdScript, 1,"{ Scripting commands }"},
58 {"quit", CmdQuit, 1, "Exit program"},
59 {"exit", CmdQuit, 1, "Exit program"},
7fe9b0b7 60 {NULL, NULL, 0, NULL}
61};
62
57c69556
MHS
63command_t* getTopLevelCommandTable()
64{
65 return CommandTable;
66}
7fe9b0b7 67int CmdHelp(const char *Cmd)
68{
69 CmdsHelp(CommandTable);
70 return 0;
71}
72
73int CmdQuit(const char *Cmd)
74{
75 exit(0);
76 return 0;
77}
42daa759 78/**
79 * @brief This method should be called when sending a new command to the pm3. In case any old
80 * responses from previous commands are stored in the buffer, a call to this method should clear them.
81 * A better method could have been to have explicit command-ACKS, so we can know which ACK goes to which
82 * operation. Right now we'll just have to live with this.
83 */
84void clearCommandBuffer()
85{
86 //This is a very simple operation
87 cmd_tail = cmd_head;
88}
89
90/**
91 * @brief storeCommand stores a USB command in a circular buffer
92 * @param UC
93 */
94void storeCommand(UsbCommand *command)
95{
96 if( ( cmd_head+1) % CMD_BUFFER_SIZE == cmd_tail)
97 {
98 //If these two are equal, we're about to overwrite in the
99 // circular buffer.
100 PrintAndLog("WARNING: Command buffer about to overwrite command! This needs to be fixed!");
101 }
102 //Store the command at the 'head' location
103 UsbCommand* destination = &cmdBuffer[cmd_head];
104 memcpy(destination, command, sizeof(UsbCommand));
105
106 cmd_head = (cmd_head +1) % CMD_BUFFER_SIZE; //increment head and wrap
107
108}
109/**
110 * @brief getCommand gets a command from an internal circular buffer.
111 * @param response location to write command
112 * @return 1 if response was returned, 0 if nothing has been received
113 */
114int getCommand(UsbCommand* response)
115{
116 //If head == tail, there's nothing to read, or if we just got initialized
117 if(cmd_head == cmd_tail){
118 return 0;
119 }
120 //Pick out the next unread command
121 UsbCommand* last_unread = &cmdBuffer[cmd_tail];
122 memcpy(response, last_unread, sizeof(UsbCommand));
123 //Increment tail - this is a circular buffer, so modulo buffer size
124 cmd_tail = (cmd_tail +1 ) % CMD_BUFFER_SIZE;
125
126 return 1;
127
128}
129
fd368d18 130/**
131 * Waits for a certain response type. This method waits for a maximum of
132 * ms_timeout milliseconds for a specified response command.
133 *@brief WaitForResponseTimeout
134 * @param cmd command to wait for
135 * @param response struct to copy received command into.
136 * @param ms_timeout
137 * @return true if command was returned, otherwise false
138 */
902cb3c0 139bool WaitForResponseTimeout(uint32_t cmd, UsbCommand* response, size_t ms_timeout) {
a61b4976 140
141 UsbCommand resp;
142
fd368d18 143 if (response == NULL) {
a61b4976 144
fd368d18 145 response = &resp;
902cb3c0 146 }
534983d7 147
fd368d18 148 // Wait until the command is received
149 for(size_t dm_seconds=0; dm_seconds < ms_timeout/10; dm_seconds++) {
150
151 while(getCommand(response))
152 {
153 if(response->cmd == cmd){
154 //We got what we expected
155 return true;
156 }
534983d7 157
fd368d18 158 }
159 msleep(10); // XXX ugh
160 if (dm_seconds == 200) { // Two seconds elapsed
161 PrintAndLog("Waiting for a response from the proxmark...");
162 PrintAndLog("Don't forget to cancel its operation first by pressing on the button");
163 }
164 }
165 return false;
534983d7 166}
167
902cb3c0 168bool WaitForResponse(uint32_t cmd, UsbCommand* response) {
169 return WaitForResponseTimeout(cmd,response,-1);
7fe9b0b7 170}
171
172//-----------------------------------------------------------------------------
173// Entry point into our code: called whenever the user types a command and
174// then presses Enter, which the full command line that they typed.
175//-----------------------------------------------------------------------------
902cb3c0 176void CommandReceived(char *Cmd) {
7fe9b0b7 177 CmdsParse(CommandTable, Cmd);
178}
179
180//-----------------------------------------------------------------------------
181// Entry point into our code: called whenever we received a packet over USB
182// that we weren't necessarily expecting, for example a debug print.
183//-----------------------------------------------------------------------------
184void UsbCommandReceived(UsbCommand *UC)
185{
79a73ab2 186 /*
9440213d 187 // Debug
79a73ab2 188 printf("UsbCommand length[len=%zd]\n",sizeof(UsbCommand));
125a98a1 189 printf(" cmd[len=%zd]: %"llx"\n",sizeof(UC->cmd),UC->cmd);
190 printf(" arg0[len=%zd]: %"llx"\n",sizeof(UC->arg[0]),UC->arg[0]);
191 printf(" arg1[len=%zd]: %"llx"\n",sizeof(UC->arg[1]),UC->arg[1]);
192 printf(" arg2[len=%zd]: %"llx"\n",sizeof(UC->arg[2]),UC->arg[2]);
79a73ab2 193 printf(" data[len=%zd]: %02x%02x%02x...\n",sizeof(UC->d.asBytes),UC->d.asBytes[0],UC->d.asBytes[1],UC->d.asBytes[2]);
194 */
9440213d 195
7fe9b0b7 196 // printf("%s(%x) current cmd = %x\n", __FUNCTION__, c->cmd, current_command);
9440213d 197 // If we recognize a response, return to avoid further processing
7fe9b0b7 198 switch(UC->cmd) {
9440213d 199 // First check if we are handling a debug message
7fe9b0b7 200 case CMD_DEBUG_PRINT_STRING: {
a501c82b 201 char s[USB_CMD_DATA_SIZE+1] = {0x00};
9440213d 202 size_t len = MIN(UC->arg[0],USB_CMD_DATA_SIZE);
203 memcpy(s,UC->d.asBytes,len);
ab8b654e 204 PrintAndLog("#db# %s ", s);
7fe9b0b7 205 return;
db09cb3a 206 } break;
7fe9b0b7 207
db09cb3a 208 case CMD_DEBUG_PRINT_INTEGERS: {
ab8b654e 209 PrintAndLog("#db# %08x, %08x, %08x \r\n", UC->arg[0], UC->arg[1], UC->arg[2]);
7fe9b0b7 210 return;
db09cb3a 211 } break;
7fe9b0b7 212
213 case CMD_MEASURED_ANTENNA_TUNING: {
214 int peakv, peakf;
215 int vLf125, vLf134, vHf;
216 vLf125 = UC->arg[0] & 0xffff;
217 vLf134 = UC->arg[0] >> 16;
72e930ef 218 vHf = UC->arg[1] & 0xffff;;
219 peakf = UC->arg[2] & 0xffff;
220 peakv = UC->arg[2] >> 16;
221
222 //Reset delta trigger every 3:d time
223
224 if ( deltaReset == 4){
225 delta125[0] = vLf125;
226 delta134[0] = vLf134;
227 deltahf[0] = vHf;
228 } else if ( deltaReset == 2){
229 delta125[1] = vLf125;
230 delta134[1] = vLf134;
231 deltahf[1] = vHf;
232 }
233
234 if ( deltaReset == 0){
235
236 }
237
7fe9b0b7 238 PrintAndLog("");
7fe9b0b7 239 PrintAndLog("# LF antenna: %5.2f V @ 125.00 kHz", vLf125/1000.0);
240 PrintAndLog("# LF antenna: %5.2f V @ 134.00 kHz", vLf134/1000.0);
72e930ef 241 PrintAndLog("# LF optimal: %5.2f V @ %9.2f kHz", peakv/1000.0, 12000.0/(peakf+1));
7fe9b0b7 242 PrintAndLog("# HF antenna: %5.2f V @ 13.56 MHz", vHf/1000.0);
243 if (peakv<2000)
244 PrintAndLog("# Your LF antenna is unusable.");
245 else if (peakv<10000)
246 PrintAndLog("# Your LF antenna is marginal.");
247 if (vHf<2000)
248 PrintAndLog("# Your HF antenna is unusable.");
249 else if (vHf<5000)
250 PrintAndLog("# Your HF antenna is marginal.");
72e930ef 251 }
252
253 deltaReset = (deltaReset == 0) ? 4 : deltaReset>>1;
254 break;
db09cb3a 255
902cb3c0 256 case CMD_DOWNLOADED_RAW_ADC_SAMPLES_125K: {
257// printf("received samples: ");
258// print_hex(UC->d.asBytes,512);
259 sample_buf_len += UC->arg[1];
260// printf("samples: %zd offset: %d\n",sample_buf_len,UC->arg[0]);
261 memcpy(sample_buf+(UC->arg[0]),UC->d.asBytes,UC->arg[1]);
262 } break;
263
264
265// case CMD_ACK: {
266// PrintAndLog("Receive ACK\n");
267// } break;
268
db09cb3a 269 default: {
270 // Maybe it's a response
fd368d18 271 /*
db09cb3a 272 switch(current_command) {
273 case CMD_DOWNLOAD_RAW_ADC_SAMPLES_125K: {
274 if (UC->cmd != CMD_DOWNLOADED_RAW_ADC_SAMPLES_125K) {
275 PrintAndLog("unrecognized command %08x\n", UC->cmd);
276 break;
277 }
902cb3c0 278// int i;
279 PrintAndLog("received samples %d\n",UC->arg[0]);
280 memcpy(sample_buf+UC->arg[0],UC->d.asBytes,48);
281 sample_buf_len += 48;
282// for(i=0; i<48; i++) sample_buf[i] = UC->d.asBytes[i];
fd368d18 283 //received_command = UC->cmd;
db09cb3a 284 } break;
285
286 default: {
287 } break;
fd368d18 288 }*/
289 }
290 break;
7fe9b0b7 291 }
fd368d18 292
293 storeCommand(UC);
294
295}
296
Impressum, Datenschutz