USB comms: part 3 towards @micolous PR#463

* change variable 'sp' from global to static
* move code to open and close USB port to comms.c (OpenProxmark() and CloseProxmark())
* change scope of USBCommandReceived() to static
* (flasher still unchanged)
This commit is contained in:
pwpiwi 2018-04-28 10:09:16 +02:00 committed by GitHub
parent 61aaee35cc
commit 818efbebb8
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 45 additions and 42 deletions

View file

@ -21,7 +21,7 @@
// Declare globals. // Declare globals.
// Serial port that we are communicating with the PM3 on. // Serial port that we are communicating with the PM3 on.
serial_port sp; static serial_port sp;
// If TRUE, then there is no active connection to the PM3, and we will drop commands sent. // If TRUE, then there is no active connection to the PM3, and we will drop commands sent.
static bool offline; static bool offline;
@ -58,6 +58,38 @@ bool IsOffline() {
return offline; return offline;
} }
bool OpenProxmark(char *portname, bool waitCOMPort, int timeout) {
if (!waitCOMPort) {
sp = uart_open(portname);
} else {
printf("Waiting for Proxmark to appear on %s ", portname);
fflush(stdout);
int openCount = 0;
do {
sp = uart_open(portname);
msleep(1000);
printf(".");
fflush(stdout);
} while(++openCount < timeout && (sp == INVALID_SERIAL_PORT || sp == CLAIMED_SERIAL_PORT));
printf("\n");
}
// check result of uart opening
if (sp == INVALID_SERIAL_PORT) {
printf("ERROR: invalid serial port\n");
return false;
} else if (sp == CLAIMED_SERIAL_PORT) {
printf("ERROR: serial port is claimed by another process\n");
return false;
} else {
return true;
}
}
void CloseProxmark(void) {
uart_close(sp);
}
void SendCommand(UsbCommand *c) { void SendCommand(UsbCommand *c) {
#ifdef COMMS_DEBUG #ifdef COMMS_DEBUG
printf("Sending %04x cmd\n", c->cmd); printf("Sending %04x cmd\n", c->cmd);
@ -73,6 +105,7 @@ void SendCommand(UsbCommand *c) {
Not good.../holiman Not good.../holiman
**/ **/
while(txcmd_pending); while(txcmd_pending);
txcmd = *c; txcmd = *c;
txcmd_pending = true; txcmd_pending = true;
} }
@ -140,11 +173,7 @@ int getCommand(UsbCommand* response)
} }
//----------------------------------------------------------------------------- static void UsbCommandReceived(UsbCommand *UC)
// 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) { switch(UC->cmd) {
// First check if we are handling a debug message // First check if we are handling a debug message
@ -170,7 +199,7 @@ void UsbCommandReceived(UsbCommand *UC)
} break; } break;
default: default:
storeCommand(UC); storeCommand(UC);
break; break;
} }
@ -184,12 +213,12 @@ __attribute__((force_align_arg_pointer))
#endif #endif
#endif #endif
*uart_receiver(void *targ) { *uart_receiver(void *targ) {
receiver_arg *arg = (receiver_arg*)targ; receiver_arg *conn = (receiver_arg*)targ;
size_t rxlen; size_t rxlen;
uint8_t rx[sizeof(UsbCommand)]; uint8_t rx[sizeof(UsbCommand)];
uint8_t *prx = rx; uint8_t *prx = rx;
while (arg->run) { while (conn->run) {
rxlen = 0; rxlen = 0;
if (uart_receive(sp, prx, sizeof(UsbCommand) - (prx-rx), &rxlen) && rxlen) { if (uart_receive(sp, prx, sizeof(UsbCommand) - (prx-rx), &rxlen) && rxlen) {
prx += rxlen; prx += rxlen;

View file

@ -31,19 +31,18 @@ typedef struct {
} receiver_arg; } receiver_arg;
// Wrappers required as static variables can only be used in one file.
void SetOffline(bool new_offline); void SetOffline(bool new_offline);
bool IsOffline(); bool IsOffline();
bool OpenProxmark(char *portname, bool waitCOMPort, int timeout);
void CloseProxmark(void);
void SendCommand(UsbCommand *c); void SendCommand(UsbCommand *c);
void *uart_receiver(void *targ); void *uart_receiver(void *targ);
void UsbCommandReceived(UsbCommand *UC);
void clearCommandBuffer(); void clearCommandBuffer();
bool WaitForResponseTimeoutW(uint32_t cmd, UsbCommand* response, size_t ms_timeout, bool show_warning); bool WaitForResponseTimeoutW(uint32_t cmd, UsbCommand* response, size_t ms_timeout, bool show_warning);
bool WaitForResponseTimeout(uint32_t cmd, UsbCommand* response, size_t ms_timeout); bool WaitForResponseTimeout(uint32_t cmd, UsbCommand* response, size_t ms_timeout);
bool WaitForResponse(uint32_t cmd, UsbCommand* response); bool WaitForResponse(uint32_t cmd, UsbCommand* response);
extern serial_port sp;
#endif // COMMS_H_ #endif // COMMS_H_

View file

@ -21,7 +21,6 @@
#include "util_posix.h" #include "util_posix.h"
#include "proxgui.h" #include "proxgui.h"
#include "cmdmain.h" #include "cmdmain.h"
#include "uart.h"
#include "ui.h" #include "ui.h"
#include "util.h" #include "util.h"
#include "cmdparser.h" #include "cmdparser.h"
@ -137,7 +136,7 @@ main_loop(char *script_cmds_file, char *script_cmd, bool usb_present) {
} }
write_history(".history"); write_history(".history");
if (usb_present) { if (usb_present) {
conn.run = false; conn.run = false;
pthread_join(reader_thread, NULL); pthread_join(reader_thread, NULL);
@ -293,33 +292,9 @@ int main(int argc, char* argv[]) {
// set global variables // set global variables
set_my_executable_path(); set_my_executable_path();
// open uart
if (!waitCOMPort) {
sp = uart_open(argv[1]);
} else {
printf("Waiting for Proxmark to appear on %s ", argv[1]);
fflush(stdout);
int openCount = 0;
do {
sp = uart_open(argv[1]);
msleep(1000);
printf(".");
fflush(stdout);
} while(++openCount < 20 && (sp == INVALID_SERIAL_PORT || sp == CLAIMED_SERIAL_PORT));
printf("\n");
}
// check result of uart opening // try to open USB connection to Proxmark
if (sp == INVALID_SERIAL_PORT) { usb_present = OpenProxmark(argv[1], waitCOMPort, 20);
printf("ERROR: invalid serial port\n");
usb_present = false;
} else if (sp == CLAIMED_SERIAL_PORT) {
printf("ERROR: serial port is claimed by another process\n");
usb_present = false;
} else {
usb_present = true;
}
#ifdef HAVE_GUI #ifdef HAVE_GUI
#ifdef _WIN32 #ifdef _WIN32
@ -344,7 +319,7 @@ int main(int argc, char* argv[]) {
// Clean up the port // Clean up the port
if (usb_present) { if (usb_present) {
uart_close(sp); CloseProxmark();
} }
exit(0); exit(0);