#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
read_history(".history");
- while(1) {
+ while (1) {
// If there is a script file
if (script_file)
{
write_history(".history");
if (usb_present) {
- rarg.run = 0;
+ conn.run = false;
pthread_join(reader_thread, NULL);
}
fclose(script_file);
script_file = NULL;
}
+
+ pthread_mutex_destroy(&conn.recv_lock);
}
static void dumpAllHelp(int markdown)
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]);
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
#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