USB comm: prepare for @micolous change (PR#463) (#587)

* move communication related code from proxmark3.c and cmdmain.c to new file comms.c
* replace byte_t by uint8_t in uart_posix.c and uart_win32.c
* move OpenProxmark() and CloseProxmark() from flasher.c to flash.c
* move print_lock mutex including initializer to ui.c
* minor changes in printing help texts
* no changes in comms functionality yet
This commit is contained in:
pwpiwi 2018-03-31 09:52:43 +02:00 committed by GitHub
parent 3bcc4d77e1
commit f5ecd97b15
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
16 changed files with 384 additions and 307 deletions

View file

@ -28,79 +28,6 @@
#include "cmdhw.h"
#include "whereami.h"
#ifdef _WIN32
#define SERIAL_PORT_H "com3"
#else
#define SERIAL_PORT_H "/dev/ttyACM0"
#endif
// a global mutex to prevent interlaced printing from different threads
pthread_mutex_t print_lock;
static serial_port sp;
static UsbCommand txcmd;
volatile static bool txcmd_pending = false;
void SendCommand(UsbCommand *c) {
#if 0
printf("Sending %d bytes\n", sizeof(UsbCommand));
#endif
if (offline) {
PrintAndLog("Sending bytes to proxmark failed - offline");
return;
}
/**
The while-loop below causes hangups at times, when the pm3 unit is unresponsive
or disconnected. The main console thread is alive, but comm thread just spins here.
Not good.../holiman
**/
while(txcmd_pending);
txcmd = *c;
txcmd_pending = true;
}
struct receiver_arg {
int run;
};
byte_t rx[sizeof(UsbCommand)];
byte_t* prx = rx;
static void
#ifdef __has_attribute
#if __has_attribute(force_align_arg_pointer)
__attribute__((force_align_arg_pointer))
#endif
#endif
*uart_receiver(void *targ) {
struct receiver_arg *arg = (struct receiver_arg*)targ;
size_t rxlen;
while (arg->run) {
rxlen = 0;
if (uart_receive(sp, prx, sizeof(UsbCommand) - (prx-rx), &rxlen) && rxlen) {
prx += rxlen;
if (prx-rx < sizeof(UsbCommand)) {
continue;
}
UsbCommandReceived((UsbCommand*)rx);
}
prx = rx;
if(txcmd_pending) {
if (!uart_send(sp, (byte_t*) &txcmd, sizeof(UsbCommand))) {
PrintAndLog("Sending bytes to proxmark failed");
}
txcmd_pending = false;
}
}
pthread_exit(NULL);
return NULL;
}
void
#ifdef __has_attribute
@ -109,15 +36,17 @@ __attribute__((force_align_arg_pointer))
#endif
#endif
main_loop(char *script_cmds_file, char *script_cmd, bool usb_present) {
struct receiver_arg rarg;
receiver_arg conn;
char *cmd = NULL;
pthread_t reader_thread;
bool execCommand = (script_cmd != NULL);
bool stdinOnPipe = !isatty(STDIN_FILENO);
memset(&conn, 0, sizeof(receiver_arg));
if (usb_present) {
rarg.run = 1;
pthread_create(&reader_thread, NULL, &uart_receiver, &rarg);
conn.run = true;
pthread_create(&reader_thread, NULL, &uart_receiver, &conn);
// cache Version information now:
CmdVersion(NULL);
}
@ -135,7 +64,7 @@ main_loop(char *script_cmds_file, char *script_cmd, bool usb_present) {
read_history(".history");
while(1) {
while (1) {
// If there is a script file
if (script_file)
{
@ -207,7 +136,7 @@ main_loop(char *script_cmds_file, char *script_cmd, bool usb_present) {
write_history(".history");
if (usb_present) {
rarg.run = 0;
conn.run = false;
pthread_join(reader_thread, NULL);
}
@ -257,9 +186,8 @@ static void set_my_executable_path(void)
static void show_help(bool showFullHelp, char *command_line){
printf("syntax: %s <port> [-h|-help|-m|-f|-flush|-w|-wait|-c|-command|-l|-lua] [cmd_script_file_name] [command][lua_script_name]\n", command_line);
printf("\tLinux example:'%s /dev/ttyACM0'\n", command_line);
printf("\tWindows example:'%s com3'\n\n", command_line);
printf("\texample: %s "SERIAL_PORT_H"\n\n", command_line);
if (showFullHelp){
printf("help: <-h|-help> Dump all interactive command's help at once.\n");
printf("\t%s -h\n\n", command_line);
@ -287,7 +215,7 @@ int main(int argc, char* argv[]) {
bool addLuaExec = false;
char *script_cmds_file = NULL;
char *script_cmd = NULL;
if (argc < 2) {
show_help(true, argv[0]);
return 1;
@ -392,9 +320,6 @@ int main(int argc, char* argv[]) {
usb_present = true;
offline = 0;
}
// create a mutex to avoid interlacing print commands from our different threads
pthread_mutex_init(&print_lock, NULL);
#ifdef HAVE_GUI
#ifdef _WIN32
@ -422,8 +347,5 @@ int main(int argc, char* argv[]) {
uart_close(sp);
}
// clean up mutex
pthread_mutex_destroy(&print_lock);
exit(0);
}