]> cvs.zerfleddert.de Git - proxmark3-svn/blobdiff - client/proxmark3.c
Comms refactor (prerequisite of libproxmark work) (#371)
[proxmark3-svn] / client / proxmark3.c
index 99ba9fbad77ec9dbcea40bc0b36074ea7914b0e5..0acf34f682d23946ab8495b620de943d0af7e996 100644 (file)
 #include "cmdparser.h"
 #include "cmdhw.h"
 #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"
 #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 *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 main_loop(char *script_cmds_file, char *script_cmd, bool usb_present) {
-       struct receiver_arg rarg;
+void main_loop(char *script_cmds_file, char* script_cmd, bool usb_present, serial_port* sp) {
+       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));
+       pthread_mutex_init(&conn.recv_lock, NULL);
+
+
+       // TODO: Move this into comms.c
+       PlotGridXdefault = 64;
+       PlotGridYdefault = 64;
+       showDemod = true;
+       CursorScaleFactor = 1;
+
        if (usb_present) {
-               rarg.run = 1;
-               pthread_create(&reader_thread, NULL, &uart_receiver, &rarg);
+               conn.run = true;
+               SetSerialPort(sp);
+               SetOffline(false);
+               pthread_create(&reader_thread, NULL, &uart_receiver, &conn);
                // cache Version information now:
                CmdVersion(NULL);
+       } else {
+               SetOffline(true);
        }
 
        // file with script
@@ -122,7 +78,7 @@ void 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)
                {
@@ -194,7 +150,7 @@ void 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);
        }
        
@@ -202,6 +158,8 @@ void main_loop(char *script_cmds_file, char *script_cmd, bool usb_present) {
                fclose(script_file);
                script_file = NULL;
        }
+
+       pthread_mutex_destroy(&conn.recv_lock);
 }
 
 static void dumpAllHelp(int markdown)
@@ -274,6 +232,8 @@ 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]);
@@ -370,14 +330,11 @@ int main(int argc, char* argv[]) {
        if (sp == INVALID_SERIAL_PORT) {
                printf("ERROR: invalid serial port\n");
                usb_present = false;
-               offline = 1;
        } else if (sp == CLAIMED_SERIAL_PORT) {
                printf("ERROR: serial port is claimed by another process\n");
                usb_present = false;
-               offline = 1;
        } else {
                usb_present = true;
-               offline = 0;
        }
        
        // create a mutex to avoid interlacing print commands from our different threads
@@ -385,23 +342,23 @@ int main(int argc, char* argv[]) {
 
 #ifdef HAVE_GUI
 #ifdef _WIN32
-       InitGraphics(argc, argv, script_cmds_file, script_cmd, usb_present);
+       InitGraphics(argc, argv, script_cmds_file, script_cmd, usb_present, sp);
        MainGraphics();
 #else
        char* display = getenv("DISPLAY");
 
        if (display && strlen(display) > 1)
        {
-               InitGraphics(argc, argv, script_cmds_file, script_cmd, usb_present);
+               InitGraphics(argc, argv, script_cmds_file, script_cmd, usb_present, sp);
                MainGraphics();
        }
        else
        {
-               main_loop(script_cmds_file, script_cmd, usb_present);
+               main_loop(script_cmds_file, script_cmd, usb_present, sp);
        }
 #endif
 #else
-       main_loop(script_cmds_file, script_cmd, usb_present);
+       main_loop(script_cmds_file, script_cmd, usb_present, sp);
 #endif 
 
        // Clean up the port
Impressum, Datenschutz