]>
Commit | Line | Data |
---|---|---|
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" |
17 | #include "data.h" | |
18 | #include "usb_cmd.h" | |
19 | #include "ui.h" | |
20 | #include "cmdhf.h" | |
21 | #include "cmddata.h" | |
22 | #include "cmdhw.h" | |
23 | #include "cmdlf.h" | |
24 | #include "cmdmain.h" | |
25 | ||
26 | unsigned int current_command = CMD_UNKNOWN; | |
27 | unsigned int received_command = CMD_UNKNOWN; | |
534983d7 | 28 | UsbCommand current_response; |
7fe9b0b7 | 29 | |
30 | static int CmdHelp(const char *Cmd); | |
31 | static int CmdQuit(const char *Cmd); | |
32 | ||
33 | static command_t CommandTable[] = | |
34 | { | |
c37d2e70 | 35 | {"help", CmdHelp, 1, "This help. Use '<command> help' for details of the following commands:\n"}, |
37239a7c | 36 | {"data", CmdData, 1, "{ Plot window / data buffer manipulation... }"}, |
7fe9b0b7 | 37 | {"exit", CmdQuit, 1, "Exit program"}, |
37239a7c | 38 | {"hf", CmdHF, 1, "{ HF commands... }"}, |
39 | {"hw", CmdHW, 1, "{ Hardware commands... }"}, | |
40 | {"lf", CmdLF, 1, "{ LF commands... }"}, | |
7fe9b0b7 | 41 | {"quit", CmdQuit, 1, "Quit program"}, |
42 | {NULL, NULL, 0, NULL} | |
43 | }; | |
44 | ||
45 | int CmdHelp(const char *Cmd) | |
46 | { | |
47 | CmdsHelp(CommandTable); | |
48 | return 0; | |
49 | } | |
50 | ||
51 | int CmdQuit(const char *Cmd) | |
52 | { | |
53 | exit(0); | |
54 | return 0; | |
55 | } | |
56 | ||
534983d7 | 57 | UsbCommand * WaitForResponseTimeout(uint32_t response_type, uint32_t ms_timeout) { |
58 | UsbCommand * ret = ¤t_response; | |
59 | int i=0; | |
60 | ||
61 | for(i=0; received_command != response_type && i < ms_timeout / 10; i++) { | |
62 | msleep(10); // XXX ugh | |
63 | } | |
64 | ||
65 | if(received_command != response_type) | |
66 | ret = NULL; | |
67 | ||
68 | received_command = CMD_UNKNOWN; | |
69 | ||
70 | return ret; | |
71 | } | |
72 | ||
73 | UsbCommand * WaitForResponse(uint32_t response_type) | |
7fe9b0b7 | 74 | { |
534983d7 | 75 | return WaitForResponseTimeout(response_type, -1); |
7fe9b0b7 | 76 | } |
77 | ||
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) | |
83 | { | |
84 | CmdsParse(CommandTable, Cmd); | |
85 | } | |
86 | ||
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) | |
92 | { | |
93 | // printf("%s(%x) current cmd = %x\n", __FUNCTION__, c->cmd, current_command); | |
94 | /* If we recognize a response, return to avoid further processing */ | |
95 | switch(UC->cmd) { | |
96 | case CMD_DEBUG_PRINT_STRING: { | |
97 | char s[100]; | |
98 | if(UC->arg[0] > 70 || UC->arg[0] < 0) { | |
99 | UC->arg[0] = 0; | |
100 | } | |
101 | memcpy(s, UC->d.asBytes, UC->arg[0]); | |
102 | s[UC->arg[0]] = '\0'; | |
103 | PrintAndLog("#db# %s", s); | |
104 | return; | |
105 | } | |
106 | ||
107 | case CMD_DEBUG_PRINT_INTEGERS: | |
108 | PrintAndLog("#db# %08x, %08x, %08x\r\n", UC->arg[0], UC->arg[1], UC->arg[2]); | |
109 | return; | |
110 | ||
111 | case CMD_MEASURED_ANTENNA_TUNING: { | |
112 | int peakv, peakf; | |
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; | |
119 | PrintAndLog(""); | |
120 | PrintAndLog(""); | |
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); | |
125 | if (peakv<2000) | |
126 | PrintAndLog("# Your LF antenna is unusable."); | |
127 | else if (peakv<10000) | |
128 | PrintAndLog("# Your LF antenna is marginal."); | |
129 | if (vHf<2000) | |
130 | PrintAndLog("# Your HF antenna is unusable."); | |
131 | else if (vHf<5000) | |
132 | PrintAndLog("# Your HF antenna is marginal."); | |
133 | return; | |
134 | } | |
135 | default: | |
136 | break; | |
137 | } | |
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; | |
142 | int i; | |
143 | for(i=0; i<48; i++) sample_buf[i] = UC->d.asBytes[i]; | |
144 | received_command = UC->cmd; | |
145 | return; | |
bdd1de1b | 146 | case CMD_ACQUIRE_RAW_ADC_SAMPLES_125K: |
7fe9b0b7 | 147 | case CMD_DOWNLOADED_SIM_SAMPLES_125K: |
148 | if (UC->cmd != CMD_ACK) goto unexpected_response; | |
149 | // got ACK | |
150 | received_command = UC->cmd; | |
151 | return; | |
152 | default: | |
153 | unexpected_response: | |
534983d7 | 154 | |
155 | if(UC->cmd != CMD_ACK) | |
156 | PrintAndLog("unrecognized command %08x\n", UC->cmd); | |
157 | else | |
158 | memcpy(¤t_response, UC, sizeof(UsbCommand)); | |
159 | received_command = UC->cmd; | |
7fe9b0b7 | 160 | } |
c37d2e70 | 161 | } |