]> cvs.zerfleddert.de Git - proxmark3-svn/blame - client/cmdmain.c
Windows7 working now! ugly fix for inaccurate Windows timer, quickfix for receiving...
[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"
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
27unsigned int current_command = CMD_UNKNOWN;
28unsigned int received_command = CMD_UNKNOWN;
534983d7 29UsbCommand current_response;
50193c1e 30UsbCommand current_response_user;
7fe9b0b7 31
32static int CmdHelp(const char *Cmd);
33static int CmdQuit(const char *Cmd);
34
35static command_t CommandTable[] =
36{
c37d2e70 37 {"help", CmdHelp, 1, "This help. Use '<command> help' for details of the following commands:\n"},
37239a7c 38 {"data", CmdData, 1, "{ Plot window / data buffer manipulation... }"},
7fe9b0b7 39 {"exit", CmdQuit, 1, "Exit program"},
37239a7c 40 {"hf", CmdHF, 1, "{ HF commands... }"},
41 {"hw", CmdHW, 1, "{ Hardware commands... }"},
42 {"lf", CmdLF, 1, "{ LF commands... }"},
7fe9b0b7 43 {"quit", CmdQuit, 1, "Quit program"},
44 {NULL, NULL, 0, NULL}
45};
46
47int CmdHelp(const char *Cmd)
48{
49 CmdsHelp(CommandTable);
50 return 0;
51}
52
53int CmdQuit(const char *Cmd)
54{
55 exit(0);
56 return 0;
57}
58
902cb3c0 59bool WaitForResponseTimeout(uint32_t cmd, UsbCommand* response, size_t ms_timeout) {
534983d7 60
902cb3c0 61 // Wait until the command is received
fe7bfa78 62 for(size_t i=0; received_command != cmd && i < ms_timeout/10; i++) {
63 msleep(10); // XXX ugh
64 if (i == 200) { // Two seconds elapsed
902cb3c0 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 }
534983d7 68 }
50193c1e 69
902cb3c0 70 // Check if timeout occured
71 if(received_command != cmd) return false;
534983d7 72
902cb3c0 73 // Copy the received response (if supplied)
74 if (response) {
75 memcpy(response, &current_response, sizeof(UsbCommand));
76 }
534983d7 77
902cb3c0 78 // Reset the received command
79 received_command = CMD_UNKNOWN;
534983d7 80
902cb3c0 81 return true;
534983d7 82}
83
902cb3c0 84bool WaitForResponse(uint32_t cmd, UsbCommand* response) {
85 return WaitForResponseTimeout(cmd,response,-1);
7fe9b0b7 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//-----------------------------------------------------------------------------
902cb3c0 92void CommandReceived(char *Cmd) {
7fe9b0b7 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//-----------------------------------------------------------------------------
100void 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) {
db09cb3a 105 // First check if we are handling a debug message
7fe9b0b7 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';
ab8b654e 113 PrintAndLog("#db# %s ", s);
7fe9b0b7 114 return;
db09cb3a 115 } break;
7fe9b0b7 116
db09cb3a 117 case CMD_DEBUG_PRINT_INTEGERS: {
ab8b654e 118 PrintAndLog("#db# %08x, %08x, %08x \r\n", UC->arg[0], UC->arg[1], UC->arg[2]);
7fe9b0b7 119 return;
db09cb3a 120 } break;
7fe9b0b7 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("");
7fe9b0b7 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.");
db09cb3a 143 } break;
144
902cb3c0 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
db09cb3a 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 }
902cb3c0 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];
db09cb3a 171 received_command = UC->cmd;
172 } break;
173
174 default: {
175 } break;
176 }
902cb3c0 177// // Store the last received command
178// memcpy(&current_response, UC, sizeof(UsbCommand));
179// received_command = UC->cmd;
db09cb3a 180 } break;
7fe9b0b7 181 }
902cb3c0 182 // Store the last received command
183 memcpy(&current_response, UC, sizeof(UsbCommand));
db09cb3a 184 received_command = UC->cmd;
185/*
186 // Maybe it's a response:
7fe9b0b7 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;
bdd1de1b 194 case CMD_ACQUIRE_RAW_ADC_SAMPLES_125K:
7fe9b0b7 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:
534983d7 202
203 if(UC->cmd != CMD_ACK)
ab8b654e 204 PrintAndLog("unrecognized command %08x \n", UC->cmd);
534983d7 205 else
206 memcpy(&current_response, UC, sizeof(UsbCommand));
207 received_command = UC->cmd;
7fe9b0b7 208 }
db09cb3a 209 */
c37d2e70 210}
Impressum, Datenschutz