revert field on/off in hf 14a reader
[proxmark3-svn] / client / proxmark3.c
index 0acf34f682d23946ab8495b620de943d0af7e996..99ba9fbad77ec9dbcea40bc0b36074ea7914b0e5 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
 
-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);
+// 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;
 
-       memset(&conn, 0, sizeof(receiver_arg));
-       pthread_mutex_init(&conn.recv_lock, NULL);
+               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;
+}
 
-       // TODO: Move this into comms.c
-       PlotGridXdefault = 64;
-       PlotGridYdefault = 64;
-       showDemod = true;
-       CursorScaleFactor = 1;
 
+void main_loop(char *script_cmds_file, char *script_cmd, bool usb_present) {
+       struct receiver_arg rarg;
+       char *cmd = NULL;
+       pthread_t reader_thread;
+       bool execCommand = (script_cmd != NULL);
+       bool stdinOnPipe = !isatty(STDIN_FILENO);
+       
        if (usb_present) {
-               conn.run = true;
-               SetSerialPort(sp);
-               SetOffline(false);
-               pthread_create(&reader_thread, NULL, &uart_receiver, &conn);
+               rarg.run = 1;
+               pthread_create(&reader_thread, NULL, &uart_receiver, &rarg);
                // cache Version information now:
                CmdVersion(NULL);
-       } else {
-               SetOffline(true);
        }
 
        // file with script
@@ -78,7 +122,7 @@ void main_loop(char *script_cmds_file, char* script_cmd, bool usb_present, seria
        
        read_history(".history");
 
-       while (1) {
+       while(1)  {
                // If there is a script file
                if (script_file)
                {
@@ -150,7 +194,7 @@ void main_loop(char *script_cmds_file, char* script_cmd, bool usb_present, seria
        write_history(".history");
   
        if (usb_present) {
-               conn.run = false;
+               rarg.run = 0;
                pthread_join(reader_thread, NULL);
        }
        
@@ -158,8 +202,6 @@ void main_loop(char *script_cmds_file, char* script_cmd, bool usb_present, seria
                fclose(script_file);
                script_file = NULL;
        }
-
-       pthread_mutex_destroy(&conn.recv_lock);
 }
 
 static void dumpAllHelp(int markdown)
@@ -232,8 +274,6 @@ 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]);
@@ -330,11 +370,14 @@ 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
@@ -342,23 +385,23 @@ int main(int argc, char* argv[]) {
 
 #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
Impressum, Datenschutz