]> git.zerfleddert.de Git - proxmark3-svn/blame - client/cmdmain.c
oops. in the spirit of cleaner code, don't do stuff twice!
[proxmark3-svn] / client / cmdmain.c
CommitLineData
7fe9b0b7 1#include <stdio.h>
2#include <stdlib.h>
3#include <unistd.h>
4#include <string.h>
5#include "cmdparser.h"
6#include "data.h"
7#include "usb_cmd.h"
8#include "ui.h"
9#include "cmdhf.h"
10#include "cmddata.h"
11#include "cmdhw.h"
12#include "cmdlf.h"
13#include "cmdmain.h"
14
15unsigned int current_command = CMD_UNKNOWN;
16unsigned int received_command = CMD_UNKNOWN;
17
18static int CmdHelp(const char *Cmd);
19static int CmdQuit(const char *Cmd);
20
21static command_t CommandTable[] =
22{
c37d2e70 23 {"help", CmdHelp, 1, "This help. Use '<command> help' for details of the following commands:\n"},
7fe9b0b7 24 {"data", CmdData, 1, "Plot window / data buffer manipulation"},
25 {"exit", CmdQuit, 1, "Exit program"},
26 {"hf", CmdHF, 1, "HF commands"},
27 {"hw", CmdHW, 1, "Hardware commands"},
28 {"lf", CmdLF, 1, "LF commands"},
29 {"quit", CmdQuit, 1, "Quit program"},
30 {NULL, NULL, 0, NULL}
31};
32
33int CmdHelp(const char *Cmd)
34{
35 CmdsHelp(CommandTable);
36 return 0;
37}
38
39int CmdQuit(const char *Cmd)
40{
41 exit(0);
42 return 0;
43}
44
45void WaitForResponse(uint32_t response_type)
46{
47 while (received_command != response_type) {
48#ifdef WIN32
49 UsbCommand c;
50 if (ReceiveCommandPoll(&c))
51 UsbCommandReceived(&c);
52 Sleep(0);
53#else
54 usleep(10000); // XXX ugh
55#endif
56 }
57 received_command = CMD_UNKNOWN;
58}
59
60//-----------------------------------------------------------------------------
61// Entry point into our code: called whenever the user types a command and
62// then presses Enter, which the full command line that they typed.
63//-----------------------------------------------------------------------------
64void CommandReceived(char *Cmd)
65{
66 CmdsParse(CommandTable, Cmd);
67}
68
69//-----------------------------------------------------------------------------
70// Entry point into our code: called whenever we received a packet over USB
71// that we weren't necessarily expecting, for example a debug print.
72//-----------------------------------------------------------------------------
73void UsbCommandReceived(UsbCommand *UC)
74{
75 // printf("%s(%x) current cmd = %x\n", __FUNCTION__, c->cmd, current_command);
76 /* If we recognize a response, return to avoid further processing */
77 switch(UC->cmd) {
78 case CMD_DEBUG_PRINT_STRING: {
79 char s[100];
80 if(UC->arg[0] > 70 || UC->arg[0] < 0) {
81 UC->arg[0] = 0;
82 }
83 memcpy(s, UC->d.asBytes, UC->arg[0]);
84 s[UC->arg[0]] = '\0';
85 PrintAndLog("#db# %s", s);
86 return;
87 }
88
89 case CMD_DEBUG_PRINT_INTEGERS:
90 PrintAndLog("#db# %08x, %08x, %08x\r\n", UC->arg[0], UC->arg[1], UC->arg[2]);
91 return;
92
93 case CMD_MEASURED_ANTENNA_TUNING: {
94 int peakv, peakf;
95 int vLf125, vLf134, vHf;
96 vLf125 = UC->arg[0] & 0xffff;
97 vLf134 = UC->arg[0] >> 16;
98 vHf = UC->arg[1] & 0xffff;;
99 peakf = UC->arg[2] & 0xffff;
100 peakv = UC->arg[2] >> 16;
101 PrintAndLog("");
102 PrintAndLog("");
103 PrintAndLog("# LF antenna: %5.2f V @ 125.00 kHz", vLf125/1000.0);
104 PrintAndLog("# LF antenna: %5.2f V @ 134.00 kHz", vLf134/1000.0);
105 PrintAndLog("# LF optimal: %5.2f V @%9.2f kHz", peakv/1000.0, 12000.0/(peakf+1));
106 PrintAndLog("# HF antenna: %5.2f V @ 13.56 MHz", vHf/1000.0);
107 if (peakv<2000)
108 PrintAndLog("# Your LF antenna is unusable.");
109 else if (peakv<10000)
110 PrintAndLog("# Your LF antenna is marginal.");
111 if (vHf<2000)
112 PrintAndLog("# Your HF antenna is unusable.");
113 else if (vHf<5000)
114 PrintAndLog("# Your HF antenna is marginal.");
115 return;
116 }
117 default:
118 break;
119 }
120 /* Maybe it's a response: */
121 switch(current_command) {
122 case CMD_DOWNLOAD_RAW_ADC_SAMPLES_125K:
123 if (UC->cmd != CMD_DOWNLOADED_RAW_ADC_SAMPLES_125K) goto unexpected_response;
124 int i;
125 for(i=0; i<48; i++) sample_buf[i] = UC->d.asBytes[i];
126 received_command = UC->cmd;
127 return;
bdd1de1b 128 case CMD_ACQUIRE_RAW_ADC_SAMPLES_125K:
7fe9b0b7 129 case CMD_DOWNLOADED_SIM_SAMPLES_125K:
130 if (UC->cmd != CMD_ACK) goto unexpected_response;
131 // got ACK
132 received_command = UC->cmd;
133 return;
134 default:
135 unexpected_response:
136 PrintAndLog("unrecognized command %08x\n", UC->cmd);
137 break;
138 }
c37d2e70 139}
Impressum, Datenschutz