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