]> cvs.zerfleddert.de Git - proxmark3-svn/blame - client/cmdmain.c
Merge pull request #524 from Proxmark/iceman1001-patch-1
[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
23af9327 11#include "cmdmain.h"
12
13#include <pthread.h>
7fe9b0b7 14#include <stdio.h>
15#include <stdlib.h>
16#include <unistd.h>
17#include <string.h>
18#include "cmdparser.h"
902cb3c0 19#include "proxmark3.h"
7fe9b0b7 20#include "data.h"
21#include "usb_cmd.h"
22#include "ui.h"
23#include "cmdhf.h"
24#include "cmddata.h"
25#include "cmdhw.h"
26#include "cmdlf.h"
9440213d 27#include "util.h"
ec9c7112 28#include "util_posix.h"
1d59cd8d 29#include "cmdscript.h"
fe81b478 30#include "cmdcrc.h"
7fe9b0b7 31
e772353f 32
7fe9b0b7 33unsigned int current_command = CMD_UNKNOWN;
7fe9b0b7 34
35static int CmdHelp(const char *Cmd);
36static int CmdQuit(const char *Cmd);
f46c3663 37static int CmdRev(const char *Cmd);
cf9aa77d 38
3851172d 39//For storing command that are received from the device
40#define CMD_BUFFER_SIZE 50
41static UsbCommand cmdBuffer[CMD_BUFFER_SIZE];
42//Points to the next empty position to write to
43static int cmd_head;//Starts as 0
44//Points to the position of the last unread command
45static int cmd_tail;//Starts as 0
46// to lock cmdBuffer operations from different threads
47static pthread_mutex_t cmdBufferMutex = PTHREAD_MUTEX_INITIALIZER;
fd368d18 48
7fe9b0b7 49static command_t CommandTable[] =
50{
57c69556 51 {"help", CmdHelp, 1, "This help. Use '<command> help' for details of a particular command."},
37239a7c 52 {"data", CmdData, 1, "{ Plot window / data buffer manipulation... }"},
fe81b478 53 {"hf", CmdHF, 1, "{ High Frequency commands... }"},
37239a7c 54 {"hw", CmdHW, 1, "{ Hardware commands... }"},
fe81b478 55 {"lf", CmdLF, 1, "{ Low Frequency commands... }"},
f46c3663 56 {"reveng",CmdRev, 1, "Crc calculations from the software reveng1-30"},
57 {"script",CmdScript,1, "{ Scripting commands }"},
57c69556
MHS
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{
2487dfeb 75 return 99;
7fe9b0b7 76}
f46c3663 77
78int CmdRev(const char *Cmd)
79{
80 CmdCrc(Cmd);
81 return 0;
82}
d2219cae 83
3851172d 84/**
85 * @brief This method should be called when sending a new command to the pm3. In case any old
86 * responses from previous commands are stored in the buffer, a call to this method should clear them.
87 * A better method could have been to have explicit command-ACKS, so we can know which ACK goes to which
88 * operation. Right now we'll just have to live with this.
89 */
90void clearCommandBuffer()
91{
92 //This is a very simple operation
93 pthread_mutex_lock(&cmdBufferMutex);
94 cmd_tail = cmd_head;
95 pthread_mutex_unlock(&cmdBufferMutex);
96}
97
98/**
99 * @brief storeCommand stores a USB command in a circular buffer
100 * @param UC
101 */
102void storeCommand(UsbCommand *command)
103{
104 pthread_mutex_lock(&cmdBufferMutex);
105 if( ( cmd_head+1) % CMD_BUFFER_SIZE == cmd_tail)
106 {
107 //If these two are equal, we're about to overwrite in the
108 // circular buffer.
109 PrintAndLog("WARNING: Command buffer about to overwrite command! This needs to be fixed!");
110 }
111 //Store the command at the 'head' location
112 UsbCommand* destination = &cmdBuffer[cmd_head];
113 memcpy(destination, command, sizeof(UsbCommand));
114
115 cmd_head = (cmd_head +1) % CMD_BUFFER_SIZE; //increment head and wrap
116 pthread_mutex_unlock(&cmdBufferMutex);
117}
118
119
120/**
121 * @brief getCommand gets a command from an internal circular buffer.
122 * @param response location to write command
123 * @return 1 if response was returned, 0 if nothing has been received
124 */
125int getCommand(UsbCommand* response)
126{
127 pthread_mutex_lock(&cmdBufferMutex);
128 //If head == tail, there's nothing to read, or if we just got initialized
129 if(cmd_head == cmd_tail){
130 pthread_mutex_unlock(&cmdBufferMutex);
131 return 0;
132 }
133 //Pick out the next unread command
134 UsbCommand* last_unread = &cmdBuffer[cmd_tail];
135 memcpy(response, last_unread, sizeof(UsbCommand));
136 //Increment tail - this is a circular buffer, so modulo buffer size
137 cmd_tail = (cmd_tail +1 ) % CMD_BUFFER_SIZE;
138 pthread_mutex_unlock(&cmdBufferMutex);
139 return 1;
140}
141
142
143/**
144 * Waits for a certain response type. This method waits for a maximum of
145 * ms_timeout milliseconds for a specified response command.
146 *@brief WaitForResponseTimeout
147 * @param cmd command to wait for
148 * @param response struct to copy received command into.
149 * @param ms_timeout
150 * @return true if command was returned, otherwise false
151 */
152bool WaitForResponseTimeoutW(uint32_t cmd, UsbCommand* response, size_t ms_timeout, bool show_warning) {
153
154 UsbCommand resp;
155
156 if (response == NULL) {
157 response = &resp;
158 }
159
160 uint64_t start_time = msclock();
161
162 // Wait until the command is received
163 while (true) {
164 while(getCommand(response)) {
165 if(response->cmd == cmd){
166 return true;
167 }
168 }
169 if (msclock() - start_time > ms_timeout) {
170 break;
171 }
172 if (msclock() - start_time > 2000 && show_warning) {
173 PrintAndLog("Waiting for a response from the proxmark...");
174 PrintAndLog("Don't forget to cancel its operation first by pressing on the button");
175 break;
176 }
177 }
178 return false;
179}
180
181
182bool WaitForResponseTimeout(uint32_t cmd, UsbCommand* response, size_t ms_timeout) {
183 return WaitForResponseTimeoutW(cmd, response, ms_timeout, true);
184}
185
186bool WaitForResponse(uint32_t cmd, UsbCommand* response) {
187 return WaitForResponseTimeoutW(cmd, response, -1, true);
188}
189
190
7fe9b0b7 191//-----------------------------------------------------------------------------
192// Entry point into our code: called whenever the user types a command and
193// then presses Enter, which the full command line that they typed.
194//-----------------------------------------------------------------------------
2487dfeb 195int CommandReceived(char *Cmd) {
196 return CmdsParse(CommandTable, Cmd);
7fe9b0b7 197}
198
3851172d 199
200//-----------------------------------------------------------------------------
201// Entry point into our code: called whenever we received a packet over USB
202// that we weren't necessarily expecting, for example a debug print.
203//-----------------------------------------------------------------------------
204void UsbCommandReceived(UsbCommand *UC)
205{
206 switch(UC->cmd) {
207 // First check if we are handling a debug message
208 case CMD_DEBUG_PRINT_STRING: {
209 char s[USB_CMD_DATA_SIZE+1];
210 memset(s, 0x00, sizeof(s));
211 size_t len = MIN(UC->arg[0],USB_CMD_DATA_SIZE);
212 memcpy(s,UC->d.asBytes,len);
213 PrintAndLog("#db# %s", s);
214 return;
215 } break;
216
217 case CMD_DEBUG_PRINT_INTEGERS: {
218 PrintAndLog("#db# %08x, %08x, %08x \r\n", UC->arg[0], UC->arg[1], UC->arg[2]);
219 return;
220 } break;
221
222 case CMD_DOWNLOADED_RAW_ADC_SAMPLES_125K: {
223 memcpy(sample_buf+(UC->arg[0]),UC->d.asBytes,UC->arg[1]);
224 return;
225 } break;
226
227 default:
228 storeCommand(UC);
229 break;
230 }
231
232}
233
Impressum, Datenschutz