15 unsigned int current_command 
= CMD_UNKNOWN
; 
  16 unsigned int received_command 
= CMD_UNKNOWN
; 
  18 static int CmdHelp(const char *Cmd
); 
  19 static int CmdQuit(const char *Cmd
); 
  21 static command_t CommandTable
[] =  
  23   {"help",  CmdHelp
,  1, "This help. Use '<command> help' for details of the following commands:\n"}, 
  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"}, 
  33 int CmdHelp(const char *Cmd
) 
  35   CmdsHelp(CommandTable
); 
  39 int CmdQuit(const char *Cmd
) 
  45 void WaitForResponse(uint32_t response_type
) 
  47   while (received_command 
!= response_type
) { 
  50     if (ReceiveCommandPoll(&c
)) 
  51       UsbCommandReceived(&c
); 
  54     usleep(10000); // XXX ugh 
  57   received_command 
= CMD_UNKNOWN
; 
  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 //----------------------------------------------------------------------------- 
  64 void CommandReceived(char *Cmd
) 
  66   CmdsParse(CommandTable
, Cmd
); 
  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 //----------------------------------------------------------------------------- 
  73 void UsbCommandReceived(UsbCommand 
*UC
) 
  75   //    printf("%s(%x) current cmd = %x\n", __FUNCTION__, c->cmd, current_command); 
  76   /* If we recognize a response, return to avoid further processing */ 
  78     case CMD_DEBUG_PRINT_STRING
: { 
  80       if(UC
->arg
[0] > 70 || UC
->arg
[0] < 0) { 
  83       memcpy(s
, UC
->d
.asBytes
, UC
->arg
[0]); 
  85       PrintAndLog("#db# %s", s
); 
  89     case CMD_DEBUG_PRINT_INTEGERS
: 
  90       PrintAndLog("#db# %08x, %08x, %08x\r\n", UC
->arg
[0], UC
->arg
[1], UC
->arg
[2]); 
  93     case CMD_MEASURED_ANTENNA_TUNING
: { 
  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; 
 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); 
 108         PrintAndLog("# Your LF antenna is unusable."); 
 109       else if (peakv
<10000) 
 110         PrintAndLog("# Your LF antenna is marginal."); 
 112         PrintAndLog("# Your HF antenna is unusable."); 
 114         PrintAndLog("# Your HF antenna is marginal."); 
 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
; 
 125       for(i
=0; i
<48; i
++) sample_buf
[i
] = UC
->d
.asBytes
[i
]; 
 126       received_command 
= UC
->cmd
; 
 128     case CMD_ACQUIRE_RAW_ADC_SAMPLES_125K
: 
 129     case CMD_DOWNLOADED_SIM_SAMPLES_125K
: 
 130       if (UC
->cmd 
!= CMD_ACK
) goto unexpected_response
; 
 132       received_command 
= UC
->cmd
; 
 136     PrintAndLog("unrecognized command %08x\n", UC
->cmd
);