1 //-----------------------------------------------------------------------------
2 // Copyright (C) 2010 iZsh <izsh at fail0verflow.com>
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
7 //-----------------------------------------------------------------------------
8 // Main command parser entry point
9 //-----------------------------------------------------------------------------
16 #include "cmdparser.h"
26 unsigned int current_command
= CMD_UNKNOWN
;
27 unsigned int received_command
= CMD_UNKNOWN
;
28 UsbCommand current_response
;
30 static int CmdHelp(const char *Cmd
);
31 static int CmdQuit(const char *Cmd
);
33 static command_t CommandTable
[] =
35 {"help", CmdHelp
, 1, "This help. Use '<command> help' for details of the following commands:\n"},
36 {"data", CmdData
, 1, "{ Plot window / data buffer manipulation... }"},
37 {"exit", CmdQuit
, 1, "Exit program"},
38 {"hf", CmdHF
, 1, "{ HF commands... }"},
39 {"hw", CmdHW
, 1, "{ Hardware commands... }"},
40 {"lf", CmdLF
, 1, "{ LF commands... }"},
41 {"quit", CmdQuit
, 1, "Quit program"},
45 int CmdHelp(const char *Cmd
)
47 CmdsHelp(CommandTable
);
51 int CmdQuit(const char *Cmd
)
57 UsbCommand
* WaitForResponseTimeout(uint32_t response_type
, uint32_t ms_timeout
) {
58 UsbCommand
* ret
= ¤t_response
;
61 for(i
=0; received_command
!= response_type
&& i
< ms_timeout
/ 10; i
++) {
62 msleep(10); // XXX ugh
65 if(received_command
!= response_type
)
68 received_command
= CMD_UNKNOWN
;
73 UsbCommand
* WaitForResponse(uint32_t response_type
)
75 return WaitForResponseTimeout(response_type
, -1);
78 //-----------------------------------------------------------------------------
79 // Entry point into our code: called whenever the user types a command and
80 // then presses Enter, which the full command line that they typed.
81 //-----------------------------------------------------------------------------
82 void CommandReceived(char *Cmd
)
84 CmdsParse(CommandTable
, Cmd
);
87 //-----------------------------------------------------------------------------
88 // Entry point into our code: called whenever we received a packet over USB
89 // that we weren't necessarily expecting, for example a debug print.
90 //-----------------------------------------------------------------------------
91 void UsbCommandReceived(UsbCommand
*UC
)
93 // printf("%s(%x) current cmd = %x\n", __FUNCTION__, c->cmd, current_command);
94 /* If we recognize a response, return to avoid further processing */
96 case CMD_DEBUG_PRINT_STRING
: {
98 if(UC
->arg
[0] > 70 || UC
->arg
[0] < 0) {
101 memcpy(s
, UC
->d
.asBytes
, UC
->arg
[0]);
102 s
[UC
->arg
[0]] = '\0';
103 PrintAndLog("#db# %s", s
);
107 case CMD_DEBUG_PRINT_INTEGERS
:
108 PrintAndLog("#db# %08x, %08x, %08x\r\n", UC
->arg
[0], UC
->arg
[1], UC
->arg
[2]);
111 case CMD_MEASURED_ANTENNA_TUNING
: {
113 int vLf125
, vLf134
, vHf
;
114 vLf125
= UC
->arg
[0] & 0xffff;
115 vLf134
= UC
->arg
[0] >> 16;
116 vHf
= UC
->arg
[1] & 0xffff;;
117 peakf
= UC
->arg
[2] & 0xffff;
118 peakv
= UC
->arg
[2] >> 16;
121 PrintAndLog("# LF antenna: %5.2f V @ 125.00 kHz", vLf125
/1000.0);
122 PrintAndLog("# LF antenna: %5.2f V @ 134.00 kHz", vLf134
/1000.0);
123 PrintAndLog("# LF optimal: %5.2f V @%9.2f kHz", peakv
/1000.0, 12000.0/(peakf
+1));
124 PrintAndLog("# HF antenna: %5.2f V @ 13.56 MHz", vHf
/1000.0);
126 PrintAndLog("# Your LF antenna is unusable.");
127 else if (peakv
<10000)
128 PrintAndLog("# Your LF antenna is marginal.");
130 PrintAndLog("# Your HF antenna is unusable.");
132 PrintAndLog("# Your HF antenna is marginal.");
138 /* Maybe it's a response: */
139 switch(current_command
) {
140 case CMD_DOWNLOAD_RAW_ADC_SAMPLES_125K
:
141 if (UC
->cmd
!= CMD_DOWNLOADED_RAW_ADC_SAMPLES_125K
) goto unexpected_response
;
143 for(i
=0; i
<48; i
++) sample_buf
[i
] = UC
->d
.asBytes
[i
];
144 received_command
= UC
->cmd
;
146 case CMD_ACQUIRE_RAW_ADC_SAMPLES_125K
:
147 case CMD_DOWNLOADED_SIM_SAMPLES_125K
:
148 if (UC
->cmd
!= CMD_ACK
) goto unexpected_response
;
150 received_command
= UC
->cmd
;
155 if(UC
->cmd
!= CMD_ACK
)
156 PrintAndLog("unrecognized command %08x\n", UC
->cmd
);
158 memcpy(¤t_response
, UC
, sizeof(UsbCommand
));
159 received_command
= UC
->cmd
;