USB comms: part 2 towards @micolous PR#463 (#595)

* change variable 'offline' from global to static
* change variable 'FlushAfterWrite' from global to static
* remove unused global variable 'current_command'
* WaitForResponseTimeoutW(CMD_UNKNOWN, ...) waits for any command
* #include "printf.h" or <stdio.h> in iso15693tools.c to define sprintf()
* and some minor changes/comments
This commit is contained in:
pwpiwi 2018-04-24 08:27:29 +02:00 committed by GitHub
parent e069547c27
commit 61aaee35cc
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
13 changed files with 61 additions and 35 deletions

View file

@ -327,7 +327,7 @@ int CmdLFSetConfig(const char *Cmd)
} }
bool lf_read(bool silent, uint32_t samples) { bool lf_read(bool silent, uint32_t samples) {
if (offline) return false; if (IsOffline()) return false;
UsbCommand c = {CMD_ACQUIRE_RAW_ADC_SAMPLES_125K, {silent,samples,0}}; UsbCommand c = {CMD_ACQUIRE_RAW_ADC_SAMPLES_125K, {silent,samples,0}};
clearCommandBuffer(); clearCommandBuffer();
//And ship it to device //And ship it to device
@ -870,7 +870,7 @@ int CmdVchDemod(const char *Cmd)
int CheckChipType(char cmdp) { int CheckChipType(char cmdp) {
uint32_t wordData = 0; uint32_t wordData = 0;
if (offline || cmdp == '1') return 0; if (IsOffline() || cmdp == '1') return 0;
save_restoreGB(GRAPH_SAVE); save_restoreGB(GRAPH_SAVE);
save_restoreDB(GRAPH_SAVE); save_restoreDB(GRAPH_SAVE);
@ -915,7 +915,7 @@ int CmdLFfind(const char *Cmd)
return 0; return 0;
} }
if (!offline && (cmdp != '1')) { if (!IsOffline() && (cmdp != '1')) {
lf_read(true, 30000); lf_read(true, 30000);
} else if (GraphTraceLen < minLength) { } else if (GraphTraceLen < minLength) {
PrintAndLog("Data in Graphbuffer was too small."); PrintAndLog("Data in Graphbuffer was too small.");
@ -931,7 +931,7 @@ int CmdLFfind(const char *Cmd)
// only run if graphbuffer is just noise as it should be for hitag/cotag // only run if graphbuffer is just noise as it should be for hitag/cotag
if (graphJustNoise(GraphBuffer, testLen)) { if (graphJustNoise(GraphBuffer, testLen)) {
// only run these tests if we are in online mode // only run these tests if we are in online mode
if (!offline && (cmdp != '1')) { if (!IsOffline() && (cmdp != '1')) {
// test for em4x05 in reader talk first mode. // test for em4x05 in reader talk first mode.
if (EM4x05Block0Test(&wordData)) { if (EM4x05Block0Test(&wordData)) {
PrintAndLog("\nValid EM4x05/EM4x69 Chip Found\nUse lf em 4x05readword/dump commands to read\n"); PrintAndLog("\nValid EM4x05/EM4x69 Chip Found\nUse lf em 4x05readword/dump commands to read\n");

View file

@ -29,8 +29,6 @@
#include "cmdscript.h" #include "cmdscript.h"
unsigned int current_command = CMD_UNKNOWN;
static int CmdHelp(const char *Cmd); static int CmdHelp(const char *Cmd);
static int CmdQuit(const char *Cmd); static int CmdQuit(const char *Cmd);

View file

@ -25,7 +25,7 @@ void CmdsHelp(const command_t Commands[])
int i = 0; int i = 0;
while (Commands[i].Name) while (Commands[i].Name)
{ {
if (!offline || Commands[i].Offline) if (!IsOffline() || Commands[i].Offline)
PrintAndLog("%-16s %s", Commands[i].Name, Commands[i].Help); PrintAndLog("%-16s %s", Commands[i].Name, Commands[i].Help);
++i; ++i;
} }

View file

@ -24,7 +24,7 @@
serial_port sp; 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.
bool offline; static bool offline;
// Transmit buffer. // Transmit buffer.
// TODO: Use locks and execute this on the main thread, rather than the receiver // TODO: Use locks and execute this on the main thread, rather than the receiver
@ -47,10 +47,20 @@ static int cmd_tail = 0;
// to lock cmdBuffer operations from different threads // to lock cmdBuffer operations from different threads
static pthread_mutex_t cmdBufferMutex = PTHREAD_MUTEX_INITIALIZER; static pthread_mutex_t cmdBufferMutex = PTHREAD_MUTEX_INITIALIZER;
// These wrappers are required because it is not possible to access a static
// global variable outside of the context of a single file.
void SetOffline(bool new_offline) {
offline = new_offline;
}
bool IsOffline() {
return offline;
}
void SendCommand(UsbCommand *c) { void SendCommand(UsbCommand *c) {
#if 0 #ifdef COMMS_DEBUG
printf("Sending %d bytes\n", sizeof(UsbCommand)); printf("Sending %04x cmd\n", c->cmd);
#endif #endif
if (offline) { if (offline) {
@ -153,6 +163,8 @@ void UsbCommandReceived(UsbCommand *UC)
} break; } break;
case CMD_DOWNLOADED_RAW_ADC_SAMPLES_125K: { case CMD_DOWNLOADED_RAW_ADC_SAMPLES_125K: {
// FIXME: This does unsanitised copies into memory when we don't know
// the size of the buffer.
memcpy(sample_buf+(UC->arg[0]),UC->d.asBytes,UC->arg[1]); memcpy(sample_buf+(UC->arg[0]),UC->d.asBytes,UC->arg[1]);
return; return;
} break; } break;
@ -205,15 +217,20 @@ __attribute__((force_align_arg_pointer))
* Waits for a certain response type. This method waits for a maximum of * Waits for a certain response type. This method waits for a maximum of
* ms_timeout milliseconds for a specified response command. * ms_timeout milliseconds for a specified response command.
*@brief WaitForResponseTimeout *@brief WaitForResponseTimeout
* @param cmd command to wait for * @param cmd command to wait for, or CMD_UNKNOWN to take any command.
* @param response struct to copy received command into. * @param response struct to copy received command into.
* @param ms_timeout * @param ms_timeout
* @param show_warning
* @return true if command was returned, otherwise false * @return true if command was returned, otherwise false
*/ */
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) {
UsbCommand resp; UsbCommand resp;
#ifdef COMMS_DEBUG
printf("Waiting for %04x cmd\n", cmd);
#endif
if (response == NULL) { if (response == NULL) {
response = &resp; response = &resp;
} }
@ -223,7 +240,7 @@ bool WaitForResponseTimeoutW(uint32_t cmd, UsbCommand* response, size_t ms_timeo
// Wait until the command is received // Wait until the command is received
while (true) { while (true) {
while(getCommand(response)) { while(getCommand(response)) {
if(response->cmd == cmd){ if (cmd == CMD_UNKNOWN || response->cmd == cmd) {
return true; return true;
} }
} }
@ -233,6 +250,7 @@ bool WaitForResponseTimeoutW(uint32_t cmd, UsbCommand* response, size_t ms_timeo
} }
if (msclock() - start_time > 2000 && show_warning) { if (msclock() - start_time > 2000 && show_warning) {
// 2 seconds elapsed (but this doesn't mean the timeout was exceeded)
PrintAndLog("Waiting for a response from the proxmark..."); PrintAndLog("Waiting for a response from the proxmark...");
PrintAndLog("You can cancel this operation by pressing the pm3 button"); PrintAndLog("You can cancel this operation by pressing the pm3 button");
show_warning = false; show_warning = false;

View file

@ -30,6 +30,11 @@ typedef struct {
pthread_mutex_t recv_lock; pthread_mutex_t recv_lock;
} receiver_arg; } receiver_arg;
// Wrappers required as static variables can only be used in one file.
void SetOffline(bool new_offline);
bool IsOffline();
void SendCommand(UsbCommand *c); void SendCommand(UsbCommand *c);
void *uart_receiver(void *targ); void *uart_receiver(void *targ);
@ -40,6 +45,5 @@ bool WaitForResponseTimeout(uint32_t cmd, UsbCommand* response, size_t ms_timeou
bool WaitForResponse(uint32_t cmd, UsbCommand* response); bool WaitForResponse(uint32_t cmd, UsbCommand* response);
extern serial_port sp; extern serial_port sp;
extern bool offline;
#endif // COMMS_H_ #endif // COMMS_H_

View file

@ -27,9 +27,6 @@ void ReceiveCommand(UsbCommand* rxcmd);
serial_port sp; serial_port sp;
// FIXME: what the fuckity fuck
unsigned int current_command = CMD_UNKNOWN;
#define FLASH_START 0x100000 #define FLASH_START 0x100000
#define FLASH_SIZE (256*1024) #define FLASH_SIZE (256*1024)
#define FLASH_END (FLASH_START + FLASH_SIZE) #define FLASH_END (FLASH_START + FLASH_SIZE)
@ -52,13 +49,14 @@ void CloseProxmark(const char *serial_port_name) {
unlink(serial_port_name); unlink(serial_port_name);
} }
int OpenProxmark(size_t i, const char *serial_port_name) { bool OpenProxmark(size_t i, const char *serial_port_name) {
sp = uart_open(serial_port_name); sp = uart_open(serial_port_name);
if (sp == INVALID_SERIAL_PORT || sp == CLAIMED_SERIAL_PORT) { if (sp == INVALID_SERIAL_PORT || sp == CLAIMED_SERIAL_PORT) {
//poll once a second //poll once a second
return 0; return false;
} }
return 1;
return true;
} }
// Turn PHDRs into flasher segments, checking for PHDR sanity and merging adjacent // Turn PHDRs into flasher segments, checking for PHDR sanity and merging adjacent
@ -355,6 +353,7 @@ static int enter_bootloader(char *serial_port_name)
SendCommand(&c); SendCommand(&c);
fprintf(stderr,"Press and hold down button NOW if your bootloader requires it.\n"); fprintf(stderr,"Press and hold down button NOW if your bootloader requires it.\n");
} }
msleep(100); msleep(100);
CloseProxmark(serial_port_name); CloseProxmark(serial_port_name);
@ -363,6 +362,7 @@ static int enter_bootloader(char *serial_port_name)
sleep(1); sleep(1);
fprintf(stderr, "."); fprintf(stderr, ".");
} while (!OpenProxmark(0, serial_port_name)); } while (!OpenProxmark(0, serial_port_name));
fprintf(stderr," Found.\n"); fprintf(stderr," Found.\n");
return 0; return 0;

View file

@ -32,7 +32,7 @@ int flash_write(flash_file_t *ctx);
void flash_free(flash_file_t *ctx); void flash_free(flash_file_t *ctx);
int flash_stop_flashing(void); int flash_stop_flashing(void);
void CloseProxmark(const char *serial_port_name); void CloseProxmark(const char *serial_port_name);
int OpenProxmark(size_t i, const char *serial_port_name); bool OpenProxmark(size_t i, const char *serial_port_name);
extern serial_port sp; extern serial_port sp;
#endif #endif

View file

@ -31,7 +31,6 @@ usb_dev_handle *devh = NULL;
static unsigned int claimed_iface = 0; static unsigned int claimed_iface = 0;
unsigned char return_on_error = 0; unsigned char return_on_error = 0;
unsigned char error_occured = 0; unsigned char error_occured = 0;
extern unsigned int current_command;
void SendCommand(UsbCommand *c) void SendCommand(UsbCommand *c)
{ {
@ -40,7 +39,6 @@ void SendCommand(UsbCommand *c)
#if 0 #if 0
printf("Sending %d bytes\n", sizeof(UsbCommand)); printf("Sending %d bytes\n", sizeof(UsbCommand));
#endif #endif
current_command = c->cmd;
ret = usb_bulk_write(devh, 0x01, (char*)c, sizeof(UsbCommand), 1000); ret = usb_bulk_write(devh, 0x01, (char*)c, sizeof(UsbCommand), 1000);
if (ret<0) { if (ret<0) {
error_occured = 1; error_occured = 1;

View file

@ -46,9 +46,12 @@ main_loop(char *script_cmds_file, char *script_cmd, bool usb_present) {
if (usb_present) { if (usb_present) {
conn.run = true; conn.run = true;
SetOffline(false);
pthread_create(&reader_thread, NULL, &uart_receiver, &conn); pthread_create(&reader_thread, NULL, &uart_receiver, &conn);
// cache Version information now: // cache Version information now:
CmdVersion(NULL); CmdVersion(NULL);
} else {
SetOffline(true);
} }
// file with script // file with script
@ -64,7 +67,7 @@ main_loop(char *script_cmds_file, char *script_cmd, bool usb_present) {
read_history(".history"); read_history(".history");
while (1) { while (1) {
// If there is a script file // If there is a script file
if (script_file) if (script_file)
{ {
@ -235,7 +238,7 @@ int main(int argc, char* argv[]) {
if(strcmp(argv[i],"-f") == 0 || strcmp(argv[i],"-flush") == 0){ if(strcmp(argv[i],"-f") == 0 || strcmp(argv[i],"-flush") == 0){
printf("Output will be flushed after every print.\n"); printf("Output will be flushed after every print.\n");
flushAfterWrite = 1; SetFlushAfterWrite(true);
} }
if(strcmp(argv[i],"-w") == 0 || strcmp(argv[i],"-wait") == 0){ if(strcmp(argv[i],"-w") == 0 || strcmp(argv[i],"-wait") == 0){
@ -311,14 +314,11 @@ int main(int argc, char* argv[]) {
if (sp == INVALID_SERIAL_PORT) { if (sp == INVALID_SERIAL_PORT) {
printf("ERROR: invalid serial port\n"); printf("ERROR: invalid serial port\n");
usb_present = false; usb_present = false;
offline = 1;
} else if (sp == CLAIMED_SERIAL_PORT) { } else if (sp == CLAIMED_SERIAL_PORT) {
printf("ERROR: serial port is claimed by another process\n"); printf("ERROR: serial port is claimed by another process\n");
usb_present = false; usb_present = false;
offline = 1;
} else { } else {
usb_present = true; usb_present = true;
offline = 0;
} }
#ifdef HAVE_GUI #ifdef HAVE_GUI

View file

@ -22,8 +22,7 @@
double CursorScaleFactor = 1; double CursorScaleFactor = 1;
int PlotGridX=0, PlotGridY=0, PlotGridXdefault= 64, PlotGridYdefault= 64, CursorCPos= 0, CursorDPos= 0; int PlotGridX=0, PlotGridY=0, PlotGridXdefault= 64, PlotGridYdefault= 64, CursorCPos= 0, CursorDPos= 0;
int offline; bool flushAfterWrite = false; //buzzy
int flushAfterWrite = 0; //buzzy
int GridOffset = 0; int GridOffset = 0;
bool GridLocked = false; bool GridLocked = false;
bool showDemod = true; bool showDemod = true;
@ -93,7 +92,7 @@ void PrintAndLog(char *fmt, ...)
} }
va_end(argptr2); va_end(argptr2);
if (flushAfterWrite == 1) //buzzy if (flushAfterWrite) //buzzy
{ {
fflush(NULL); fflush(NULL);
} }
@ -106,3 +105,8 @@ void SetLogFilename(char *fn)
{ {
logfilename = fn; logfilename = fn;
} }
void SetFlushAfterWrite(bool flush_after_write) {
flushAfterWrite = flush_after_write;
}

View file

@ -20,10 +20,10 @@ void ShowGraphWindow(void);
void RepaintGraphWindow(void); void RepaintGraphWindow(void);
void PrintAndLog(char *fmt, ...); void PrintAndLog(char *fmt, ...);
void SetLogFilename(char *fn); void SetLogFilename(char *fn);
void SetFlushAfterWrite(bool flush_after_write);
extern double CursorScaleFactor; extern double CursorScaleFactor;
extern int PlotGridX, PlotGridY, PlotGridXdefault, PlotGridYdefault, CursorCPos, CursorDPos, GridOffset; extern int PlotGridX, PlotGridY, PlotGridXdefault, PlotGridYdefault, CursorCPos, CursorDPos, GridOffset;
extern int flushAfterWrite; //buzzy
extern bool GridLocked; extern bool GridLocked;
extern bool showDemod; extern bool showDemod;

View file

@ -29,7 +29,7 @@ GZIP=gzip
OBJDIR = obj OBJDIR = obj
INCLUDE = -I../include -I../common INCLUDE = -I../include -I../common -I.
TAR=tar TAR=tar
TARFLAGS = -C .. -rvf TARFLAGS = -C .. -rvf

View file

@ -11,6 +11,12 @@
#include <stdint.h> #include <stdint.h>
#include <stdlib.h> #include <stdlib.h>
//#include "iso15693tools.h" //#include "iso15693tools.h"
#ifdef ON_DEVICE
#include "printf.h"
#else
#include <stdio.h>
#endif
#define POLY 0x8408 #define POLY 0x8408
@ -51,8 +57,6 @@ int Iso15693AddCrc(uint8_t *req, int n) {
} }
int sprintf(char *str, const char *format, ...);
// returns a string representation of the UID // returns a string representation of the UID
// UID is transmitted and stored LSB first, displayed MSB first // UID is transmitted and stored LSB first, displayed MSB first
// target char* buffer, where to put the UID, if NULL a static buffer is returned // target char* buffer, where to put the UID, if NULL a static buffer is returned