X-Git-Url: http://cvs.zerfleddert.de/cgi-bin/gitweb.cgi/proxmark3-svn/blobdiff_plain/afdcb8c159a73aba95a017f1cfec98e8fa2b93c1..refs/pull/671/head:/client/proxmark3.c

diff --git a/client/proxmark3.c b/client/proxmark3.c
index 0acf34f6..6fb066e8 100644
--- a/client/proxmark3.c
+++ b/client/proxmark3.c
@@ -9,6 +9,8 @@
 // Main binary
 //-----------------------------------------------------------------------------
 
+#include "proxmark3.h"
+
 #include <stdio.h>
 #include <stdlib.h>
 #include <string.h>
@@ -17,11 +19,9 @@
 #include <readline/readline.h>
 #include <readline/history.h>
 
-#include "proxmark3.h"
 #include "util_posix.h"
 #include "proxgui.h"
 #include "cmdmain.h"
-#include "uart.h"
 #include "ui.h"
 #include "util.h"
 #include "cmdparser.h"
@@ -29,36 +29,19 @@
 #include "whereami.h"
 #include "comms.h"
 
-#ifdef _WIN32
-#define SERIAL_PORT_H	"com3"
-#elif __APPLE__
-#define SERIAL_PORT_H	"/dev/tty.usbmodem*"
-#else
-#define SERIAL_PORT_H	"/dev/ttyACM0"
+void
+#ifdef __has_attribute
+#if __has_attribute(force_align_arg_pointer)
+__attribute__((force_align_arg_pointer)) 
 #endif
-
-void main_loop(char *script_cmds_file, char* script_cmd, bool usb_present, serial_port* sp) {
-	receiver_arg conn;
+#endif
+main_loop(char *script_cmds_file, char *script_cmd, bool usb_present) {
 	char *cmd = NULL;
-	pthread_t reader_thread;
 	bool execCommand = (script_cmd != NULL);
 	bool stdinOnPipe = !isatty(STDIN_FILENO);
 
-	memset(&conn, 0, sizeof(receiver_arg));
-	pthread_mutex_init(&conn.recv_lock, NULL);
-
-
-	// TODO: Move this into comms.c
-	PlotGridXdefault = 64;
-	PlotGridYdefault = 64;
-	showDemod = true;
-	CursorScaleFactor = 1;
-
 	if (usb_present) {
-		conn.run = true;
-		SetSerialPort(sp);
 		SetOffline(false);
-		pthread_create(&reader_thread, NULL, &uart_receiver, &conn);
 		// cache Version information now:
 		CmdVersion(NULL);
 	} else {
@@ -75,7 +58,7 @@ void main_loop(char *script_cmds_file, char* script_cmd, bool usb_present, seria
 			printf("executing commands from file: %s\n", script_cmds_file);
 		}
 	}
-	
+
 	read_history(".history");
 
 	while (1) {
@@ -148,18 +131,11 @@ void main_loop(char *script_cmds_file, char* script_cmd, bool usb_present, seria
 	}
 
 	write_history(".history");
-  
-	if (usb_present) {
-		conn.run = false;
-		pthread_join(reader_thread, NULL);
-	}
 	
 	if (script_file) {
 		fclose(script_file);
 		script_file = NULL;
 	}
-
-	pthread_mutex_destroy(&conn.recv_lock);
 }
 
 static void dumpAllHelp(int markdown)
@@ -202,9 +178,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);
@@ -232,9 +207,7 @@ int main(int argc, char* argv[]) {
 	bool addLuaExec = false;
 	char *script_cmds_file = NULL;
 	char *script_cmd = NULL;
-	serial_port *sp = NULL;
-	g_debugMode = 0;
-  
+
 	if (argc < 2) {
 		show_help(true, argv[0]);
 		return 1;
@@ -254,7 +227,7 @@ int main(int argc, char* argv[]) {
 		
 		if(strcmp(argv[i],"-f") == 0 || strcmp(argv[i],"-flush") == 0){
 			printf("Output will be flushed after every print.\n");
-			flushAfterWrite = 1;
+			SetFlushAfterWrite(true);
 		}
 		
 		if(strcmp(argv[i],"-w") == 0 || strcmp(argv[i],"-wait") == 0){
@@ -309,65 +282,35 @@ int main(int argc, char* argv[]) {
 	
 	// set global variables
 	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
-	if (sp == INVALID_SERIAL_PORT) {
-		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;
-	}
-	
-	// create a mutex to avoid interlacing print commands from our different threads
-	pthread_mutex_init(&print_lock, NULL);
+	// try to open USB connection to Proxmark
+	usb_present = OpenProxmark(argv[1], waitCOMPort, 20, false);
 
 #ifdef HAVE_GUI
 #ifdef _WIN32
-	InitGraphics(argc, argv, script_cmds_file, script_cmd, usb_present, sp);
+	InitGraphics(argc, argv, script_cmds_file, script_cmd, usb_present);
 	MainGraphics();
 #else
 	char* display = getenv("DISPLAY");
 
 	if (display && strlen(display) > 1)
 	{
-		InitGraphics(argc, argv, script_cmds_file, script_cmd, usb_present, sp);
+		InitGraphics(argc, argv, script_cmds_file, script_cmd, usb_present);
 		MainGraphics();
 	}
 	else
 	{
-		main_loop(script_cmds_file, script_cmd, usb_present, sp);
+		main_loop(script_cmds_file, script_cmd, usb_present);
 	}
 #endif
 #else
-	main_loop(script_cmds_file, script_cmd, usb_present, sp);
+	main_loop(script_cmds_file, script_cmd, usb_present);
 #endif	
 
 	// Clean up the port
 	if (usb_present) {
-		uart_close(sp);
+		CloseProxmark();
 	}
 
-	// clean up mutex
-	pthread_mutex_destroy(&print_lock);
-
 	exit(0);
 }