1 //-----------------------------------------------------------------------------
2 // Copyright (C) 2009 Michael Gernoth <michael at gernoth.net>
3 // Copyright (C) 2010 iZsh <izsh at fail0verflow.com>
5 // This code is licensed to you under the terms of the GNU GPL, version 2 or,
6 // at your option, any later version. See the LICENSE.txt file for the text of
8 //-----------------------------------------------------------------------------
10 //-----------------------------------------------------------------------------
17 #include <readline/readline.h>
18 #include <readline/history.h>
19 //#include "proxusb.h"
20 #include "proxmark3.h"
27 // a global mutex to prevent interlaced printing from different threads
28 pthread_mutex_t print_lock
;
30 static serial_port sp
;
31 static UsbCommand txcmd
;
32 volatile static bool txcmd_pending
= false;
35 void SendCommand(UsbCommand
*c
) {
37 printf("Sending %d bytes\n", sizeof(UsbCommand
));
41 ERR("Sending command failed, previous command is still pending");
53 struct main_loop_arg
{
55 char *script_cmds_file
;
58 //static void *usb_receiver(void *targ) {
59 // struct receiver_arg *arg = (struct receiver_arg*)targ;
63 // if (ReceiveCommandPoll(&cmdbuf)) {
64 // UsbCommandReceived(&cmdbuf);
69 // pthread_exit(NULL);
76 static void *uart_receiver(void *targ
) {
77 struct receiver_arg
*arg
= (struct receiver_arg
*)targ
;
82 rxlen
= sizeof(UsbCommand
);
83 if (uart_receive(sp
,prx
,&rxlen
)) {
85 if (((prx
-rx
) % sizeof(UsbCommand
)) != 0) {
88 cmd_count
= (prx
-rx
) / sizeof(UsbCommand
);
89 // printf("received %d bytes, which represents %d commands\n",(prx-rx), cmd_count);
90 for (size_t i
=0; i
<cmd_count
; i
++) {
91 UsbCommandReceived((UsbCommand
*)(rx
+(i
*sizeof(UsbCommand
))));
97 if (!uart_send(sp
,(byte_t
*)&txcmd
,sizeof(UsbCommand
))) {
98 PrintAndLog("Sending bytes to proxmark failed");
100 txcmd_pending
= false;
108 static void *main_loop(void *targ
) {
109 struct main_loop_arg
*arg
= (struct main_loop_arg
*)targ
;
110 struct receiver_arg rarg
;
112 pthread_t reader_thread
;
114 if (arg
->usb_present
== 1) {
116 // pthread_create(&reader_thread, NULL, &usb_receiver, &rarg);
117 pthread_create(&reader_thread
, NULL
, &uart_receiver
, &rarg
);
120 FILE *script_file
= NULL
;
121 char script_cmd_buf
[256];
123 if (arg
->script_cmds_file
)
125 script_file
= fopen(arg
->script_cmds_file
, "r");
128 printf("using 'scripting' commands file %s\n", arg
->script_cmds_file
);
132 read_history(".history");
135 // If there is a script file
138 if (!fgets(script_cmd_buf
, sizeof(script_cmd_buf
), script_file
))
146 nl
= strrchr(script_cmd_buf
, '\r');
148 nl
= strrchr(script_cmd_buf
, '\n');
151 if ((cmd
= (char*) malloc(strlen(script_cmd_buf
) + 1)) != NULL
)
153 memset(cmd
, 0, strlen(script_cmd_buf
));
154 strcpy(cmd
, script_cmd_buf
);
162 cmd
= readline(PROXPROMPT
);
166 while(cmd
[strlen(cmd
) - 1] == ' ')
167 cmd
[strlen(cmd
) - 1] = 0x00;
169 if (cmd
[0] != 0x00) {
170 if (strncmp(cmd
, "quit", 4) == 0) {
174 CommandReceived(cmd
);
184 write_history(".history");
186 if (arg
->usb_present
== 1) {
188 pthread_join(reader_thread
, NULL
);
202 int main(int argc
, char* argv
[]) {
206 printf("syntax: %s <port>\n\n",argv
[0]);
207 printf("\tLinux example:'%s /dev/ttyACM0'\n\n", argv
[0]);
211 // Make sure to initialize
212 struct main_loop_arg marg
= {
214 .script_cmds_file
= NULL
216 pthread_t main_loop_t
;
220 if (!OpenProxmark(1)) {
221 fprintf(stderr,"PROXMARK3: NOT FOUND!\n");
222 marg.usb_present = 0;
225 marg.usb_present = 1;
230 sp
= uart_open(argv
[1]);
231 if (sp
== INVALID_SERIAL_PORT
) {
232 printf("ERROR: invalid serial port\n");
233 marg
.usb_present
= 0;
235 } else if (sp
== CLAIMED_SERIAL_PORT
) {
236 printf("ERROR: serial port is claimed by another process\n");
237 marg
.usb_present
= 0;
240 marg
.usb_present
= 1;
244 // If the user passed the filename of the 'script' to execute, get it
245 if (argc
> 2 && argv
[2]) {
246 marg
.script_cmds_file
= argv
[2];
249 // create a mutex to avoid interlacing print commands from our different threads
250 pthread_mutex_init(&print_lock
, NULL
);
252 pthread_create(&main_loop_t
, NULL
, &main_loop
, &marg
);
253 InitGraphics(argc
, argv
);
257 pthread_join(main_loop_t
, NULL
);
259 // if (marg.usb_present == 1) {
267 pthread_mutex_destroy(&print_lock
);