fix: hw status (and USB comm in general) PR #434 : (@pwpiwi)

This commit is contained in:
iceman1001 2017-10-20 16:32:42 +02:00
parent 3108293c1f
commit 53333f22c8
5 changed files with 65 additions and 51 deletions

View file

@ -8,6 +8,7 @@
// Main command parser entry point
//-----------------------------------------------------------------------------
#include <pthread.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
@ -42,8 +43,10 @@ static int cmd_head;//Starts as 0
//Points to the position of the last unread command
static int cmd_tail;//Starts as 0
static command_t CommandTable[] =
{
// to lock cmdBuffer operations from different threads
static pthread_mutex_t cmdBufferMutex = PTHREAD_MUTEX_INITIALIZER;
static command_t CommandTable[] = {
{"help", CmdHelp, 1, "This help. Use '<command> help' for details of a particular command."},
{"analyse", CmdAnalyse, 1, "{ Analyse bytes... }"},
{"data", CmdData, 1, "{ Plot window / data buffer manipulation... }"},
@ -57,25 +60,22 @@ static command_t CommandTable[] =
{NULL, NULL, 0, NULL}
};
command_t* getTopLevelCommandTable()
{
return CommandTable;
}
int CmdHelp(const char *Cmd)
{
CmdsHelp(CommandTable);
return 0;
command_t* getTopLevelCommandTable() {
return CommandTable;
}
int CmdQuit(const char *Cmd)
{
return 99;
int CmdHelp(const char *Cmd) {
CmdsHelp(CommandTable);
return 0;
}
int CmdRev(const char *Cmd)
{
CmdCrc(Cmd);
return 0;
int CmdQuit(const char *Cmd) {
return 99;
}
int CmdRev(const char *Cmd) {
CmdCrc(Cmd);
return 0;
}
/**
* @brief This method should be called when sending a new command to the pm3. In case any old
@ -83,18 +83,20 @@ int CmdRev(const char *Cmd)
* A better method could have been to have explicit command-ACKS, so we can know which ACK goes to which
* operation. Right now we'll just have to live with this.
*/
void clearCommandBuffer()
{
void clearCommandBuffer() {
//This is a very simple operation
pthread_mutex_lock(&cmdBufferMutex);
cmd_tail = cmd_head;
pthread_mutex_unlock(&cmdBufferMutex);
}
/**
* @brief storeCommand stores a USB command in a circular buffer
* @param UC
*/
void storeCommand(UsbCommand *command)
{
void storeCommand(UsbCommand *command) {
pthread_mutex_lock(&cmdBufferMutex);
if( ( cmd_head+1) % CMD_BUFFER_SIZE == cmd_tail)
{
//If these two are equal, we're about to overwrite in the
@ -106,17 +108,20 @@ void storeCommand(UsbCommand *command)
memcpy(destination, command, sizeof(UsbCommand));
cmd_head = (cmd_head +1) % CMD_BUFFER_SIZE; //increment head and wrap
pthread_mutex_unlock(&cmdBufferMutex);
}
/**
* @brief getCommand gets a command from an internal circular buffer.
* @param response location to write command
* @return 1 if response was returned, 0 if nothing has been received
*/
int getCommand(UsbCommand* response)
{
int getCommand(UsbCommand* response) {
pthread_mutex_lock(&cmdBufferMutex);
//If head == tail, there's nothing to read, or if we just got initialized
if(cmd_head == cmd_tail) return 0;
if(cmd_head == cmd_tail) {
pthread_mutex_unlock(&cmdBufferMutex);
return 0;
}
//Pick out the next unread command
UsbCommand* last_unread = &cmdBuffer[cmd_tail];
@ -125,6 +130,7 @@ int getCommand(UsbCommand* response)
//Increment tail - this is a circular buffer, so modulo buffer size
cmd_tail = (cmd_tail +1 ) % CMD_BUFFER_SIZE;
pthread_mutex_unlock(&cmdBufferMutex);
return 1;
}
@ -137,32 +143,42 @@ int getCommand(UsbCommand* response)
* @param ms_timeout
* @return true if command was returned, otherwise false
*/
bool WaitForResponseTimeout(uint32_t cmd, UsbCommand* response, size_t ms_timeout) {
bool WaitForResponseTimeout(uint32_t cmd, UsbCommand* response, size_t ms_timeout, bool show_warning)) {
UsbCommand resp;
if (response == NULL)
response = &resp;
// Wait until the command is received
for ( size_t dm_seconds = 0; dm_seconds < ms_timeout/10; dm_seconds++ ) {
uint64_t start_time = msclock();
while( getCommand(response) ) {
if(response->cmd == cmd)
// Wait until the command is received
while (true) {
while ( getCommand(response) ) {
if (response->cmd == cmd)
return true;
}
msleep(10); // XXX ugh
if (dm_seconds == 250) { // 2.5 seconds elapsed
if (msclock() - start_time > ms_timeout) {
break;
}
if (msclock() - start_time > 2000 && show_warning) {
PrintAndLog("Waiting for a response from the proxmark...");
PrintAndLog("Don't forget to cancel its operation first by pressing on the button");
break;
}
}
return false;
}
bool WaitForResponseTimeout(uint32_t cmd, UsbCommand* response, size_t ms_timeout) {
return WaitForResponseTimeoutW(cmd, response, ms_timeout, true);
}
bool WaitForResponse(uint32_t cmd, UsbCommand* response) {
return WaitForResponseTimeout(cmd, response, -1);
return WaitForResponseTimeoutW(cmd, response, -1);
}
//-----------------------------------------------------------------------------
@ -177,18 +193,17 @@ int CommandReceived(char *Cmd) {
// Entry point into our code: called whenever we received a packet over USB
// that we weren't necessarily expecting, for example a debug print.
//-----------------------------------------------------------------------------
void UsbCommandReceived(UsbCommand *UC)
{
switch(UC->cmd) {
void UsbCommandReceived(UsbCommand *c) {
switch(c->cmd) {
// First check if we are handling a debug message
case CMD_DEBUG_PRINT_STRING: {
char s[USB_CMD_DATA_SIZE+1];
memset(s, 0x00, sizeof(s));
size_t len = MIN(UC->arg[0],USB_CMD_DATA_SIZE);
memcpy(s, UC->d.asBytes, len);
size_t len = MIN(c->arg[0],USB_CMD_DATA_SIZE);
memcpy(s, c->d.asBytes, len);
// test
if ( UC->arg[1] == CMD_MEASURE_ANTENNA_TUNING_HF) {
if ( c->arg[1] == CMD_MEASURE_ANTENNA_TUNING_HF) {
printf("\r#db# %s", s);
fflush(stdout);
}
@ -199,7 +214,7 @@ void UsbCommandReceived(UsbCommand *UC)
} break;
case CMD_DEBUG_PRINT_INTEGERS: {
PrintAndLog("#db# %08x, %08x, %08x", UC->arg[0], UC->arg[1], UC->arg[2]);
PrintAndLog("#db# %08x, %08x, %08x", c->arg[0], c->arg[1], c->arg[2]);
break;
}
case CMD_DOWNLOADED_RAW_ADC_SAMPLES_125K:
@ -208,17 +223,16 @@ void UsbCommandReceived(UsbCommand *UC)
// arg0 = offset in transfer. Startindex of this chunk
// arg1 = length bytes to transfer
// arg2 = bigbuff tracelength (?)
uint32_t offset = UC->arg[0];
uint32_t len = UC->arg[1];
//uint32_t tracelen = UC->arg[2];
memcpy( sample_buf + offset, UC->d.asBytes, len);
//printf("ICE:: Download from device. chunk %" PRIu32 " | size %" PRIu32 " | tracelen:%" PRIu32 " \n", offset, len, UC->arg[2]);
uint32_t offset = c->arg[0];
uint32_t len = c->arg[1];
//uint32_t tracelen = c->arg[2];
memcpy( sample_buf + offset, c->d.asBytes, len);
//printf("ICE:: Download from device. chunk %" PRIu32 " | size %" PRIu32 " | tracelen:%" PRIu32 " \n", offset, len, c->arg[2]);
break;
}
default: {
storeCommand(UC);
storeCommand(c);
break;
}
}
}

View file

@ -17,6 +17,7 @@
#include "cmdparser.h"
extern void UsbCommandReceived(UsbCommand *c);
extern int CommandReceived(char *Cmd);
extern bool WaitForResponseTimeoutW(uint32_t cmd, UsbCommand* response, size_t ms_timeout, bool show_warning);
extern bool WaitForResponseTimeout(uint32_t cmd, UsbCommand* response, size_t ms_timeout);
extern bool WaitForResponse(uint32_t cmd, UsbCommand* response);
extern void clearCommandBuffer();

View file

@ -19,7 +19,7 @@ int GridOffset = 0;
bool GridLocked = false;
bool showDemod = true;
pthread_mutex_t print_lock;
pthread_mutex_t print_lock = PTHREAD_MUTEX_INITIALIZER;
static char *logfilename = "proxmark3.log";

View file

@ -753,7 +753,7 @@ void AT91F_USB_SendZlp(AT91PS_UDP pUdp) {
void AT91F_USB_SendStall(AT91PS_UDP pUdp) {
UDP_SET_EP_FLAGS(AT91C_EP_CONTROL, AT91C_UDP_FORCESTALL);
while ( !(pUdp->UDP_CSR[AT91C_EP_CONTROL] & AT91C_UDP_ISOERROR) );
UDP_CLEAR_EP_FLAGS(AT91C_EP_CONTROL, AT91C_UDP_FORCESTALL | AT91C_UDP_ISOERROR);
UDP_CLEAR_EP_FLAGS(AT91C_EP_CONTROL, (AT91C_UDP_FORCESTALL | AT91C_UDP_ISOERROR) );
while (pUdp->UDP_CSR[AT91C_EP_CONTROL] & (AT91C_UDP_FORCESTALL | AT91C_UDP_ISOERROR));
}

View file

@ -118,8 +118,7 @@ void uart_close(const serial_port sp) {
}
bool uart_receive(const serial_port sp, byte_t* p_rx, size_t pszMaxRxLen, size_t* p_rxlen) {
ReadFile(((serial_port_windows*)sp)->hPort, p_rx, pszMaxRxLen, (LPDWORD)p_rxlen, NULL);
return (*p_rxlen != 0);
return ReadFile(((serial_port_windows*)sp)->hPort, p_rx, pszMaxRxLen, (LPDWORD)p_rxlen, NULL);
}
bool uart_send(const serial_port sp, const byte_t* p_tx, const size_t len) {