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
;
29 UsbCommand current_response_user
;
31 static int CmdHelp(const char *Cmd
);
32 static int CmdQuit(const char *Cmd
);
34 static command_t CommandTable
[] =
36 {"help", CmdHelp
, 1, "This help. Use '<command> help' for details of the following commands:\n"},
37 {"data", CmdData
, 1, "{ Plot window / data buffer manipulation... }"},
38 {"exit", CmdQuit
, 1, "Exit program"},
39 {"hf", CmdHF
, 1, "{ HF commands... }"},
40 {"hw", CmdHW
, 1, "{ Hardware commands... }"},
41 {"lf", CmdLF
, 1, "{ LF commands... }"},
42 {"quit", CmdQuit
, 1, "Quit program"},
46 int CmdHelp(const char *Cmd
)
48 CmdsHelp(CommandTable
);
52 int CmdQuit(const char *Cmd
)
58 UsbCommand
* WaitForResponseTimeout(uint32_t response_type
, uint32_t ms_timeout
) {
59 UsbCommand
* ret
= NULL
;
62 for(i
=0; received_command
!= response_type
&& i
< ms_timeout
/ 10; i
++) {
63 msleep(10); // XXX ugh
67 memcpy(¤t_response_user
, ¤t_response
, sizeof(UsbCommand
));
68 ret
= ¤t_response_user
;
70 if(received_command
!= response_type
)
73 received_command
= CMD_UNKNOWN
;
78 UsbCommand
* WaitForResponse(uint32_t response_type
)
80 return WaitForResponseTimeout(response_type
, -1);
83 //-----------------------------------------------------------------------------
84 // Entry point into our code: called whenever the user types a command and
85 // then presses Enter, which the full command line that they typed.
86 //-----------------------------------------------------------------------------
87 void CommandReceived(char *Cmd
)
89 CmdsParse(CommandTable
, Cmd
);
92 //-----------------------------------------------------------------------------
93 // Entry point into our code: called whenever we received a packet over USB
94 // that we weren't necessarily expecting, for example a debug print.
95 //-----------------------------------------------------------------------------
96 void UsbCommandReceived(UsbCommand
*UC
)
98 // printf("%s(%x) current cmd = %x\n", __FUNCTION__, c->cmd, current_command);
99 /* If we recognize a response, return to avoid further processing */
101 case CMD_DEBUG_PRINT_STRING
: {
103 if(UC
->arg
[0] > 70 || UC
->arg
[0] < 0) {
106 memcpy(s
, UC
->d
.asBytes
, UC
->arg
[0]);
107 s
[UC
->arg
[0]] = '\0';
108 PrintAndLog("#db# %s", s
);
112 case CMD_DEBUG_PRINT_INTEGERS
:
113 PrintAndLog("#db# %08x, %08x, %08x\r\n", UC
->arg
[0], UC
->arg
[1], UC
->arg
[2]);
116 case CMD_MEASURED_ANTENNA_TUNING
: {
118 int vLf125
, vLf134
, vHf
;
119 vLf125
= UC
->arg
[0] & 0xffff;
120 vLf134
= UC
->arg
[0] >> 16;
121 vHf
= UC
->arg
[1] & 0xffff;;
122 peakf
= UC
->arg
[2] & 0xffff;
123 peakv
= UC
->arg
[2] >> 16;
126 PrintAndLog("# LF antenna: %5.2f V @ 125.00 kHz", vLf125
/1000.0);
127 PrintAndLog("# LF antenna: %5.2f V @ 134.00 kHz", vLf134
/1000.0);
128 PrintAndLog("# LF optimal: %5.2f V @%9.2f kHz", peakv
/1000.0, 12000.0/(peakf
+1));
129 PrintAndLog("# HF antenna: %5.2f V @ 13.56 MHz", vHf
/1000.0);
131 PrintAndLog("# Your LF antenna is unusable.");
132 else if (peakv
<10000)
133 PrintAndLog("# Your LF antenna is marginal.");
135 PrintAndLog("# Your HF antenna is unusable.");
137 PrintAndLog("# Your HF antenna is marginal.");
143 /* Maybe it's a response: */
144 switch(current_command
) {
145 case CMD_DOWNLOAD_RAW_ADC_SAMPLES_125K
:
146 if (UC
->cmd
!= CMD_DOWNLOADED_RAW_ADC_SAMPLES_125K
) goto unexpected_response
;
148 for(i
=0; i
<48; i
++) sample_buf
[i
] = UC
->d
.asBytes
[i
];
149 received_command
= UC
->cmd
;
151 case CMD_ACQUIRE_RAW_ADC_SAMPLES_125K
:
152 case CMD_DOWNLOADED_SIM_SAMPLES_125K
:
153 if (UC
->cmd
!= CMD_ACK
) goto unexpected_response
;
155 received_command
= UC
->cmd
;
160 if(UC
->cmd
!= CMD_ACK
)
161 PrintAndLog("unrecognized command %08x\n", UC
->cmd
);
163 memcpy(¤t_response
, UC
, sizeof(UsbCommand
));
164 received_command
= UC
->cmd
;