]>
cvs.zerfleddert.de Git - proxmark3-svn/blob - client/proxmark3.c
38a977169396263377774030d910fe077c65b58d
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 static serial_port sp
;
28 static UsbCommand txcmd
;
29 static bool txcmd_pending
= false;
31 void SendCommand(UsbCommand
*c
) {
33 printf("Sending %d bytes\n", sizeof(UsbCommand
));
37 ERR("Sending command failed, previous command is still pending");
49 struct main_loop_arg
{
51 char *script_cmds_file
;
54 //static void *usb_receiver(void *targ) {
55 // struct receiver_arg *arg = (struct receiver_arg*)targ;
59 // if (ReceiveCommandPoll(&cmdbuf)) {
60 // UsbCommandReceived(&cmdbuf);
65 // pthread_exit(NULL);
71 static void *uart_receiver(void *targ
) {
72 struct receiver_arg
*arg
= (struct receiver_arg
*)targ
;
77 rxlen
= sizeof(UsbCommand
);
78 if (uart_receive(sp
,rx
,&rxlen
)) {
79 if ((rxlen
% sizeof(UsbCommand
)) != 0) {
80 PrintAndLog("ERROR: received %03zd bytes, which does not seem to be one or more command(s)\n",rxlen
);
83 cmd_count
= rxlen
/ sizeof(UsbCommand
);
84 // printf("received %zd bytes, which represents %zd commands\n",rxlen, cmd_count);
85 for (size_t i
=0; i
<cmd_count
; i
++) {
86 UsbCommandReceived((UsbCommand
*)(rx
+(i
*sizeof(UsbCommand
))));
91 if (!uart_send(sp
,(byte_t
*)&txcmd
,sizeof(UsbCommand
))) {
92 PrintAndLog("Sending bytes to proxmark failed");
94 txcmd_pending
= false;
103 static void *main_loop(void *targ
) {
104 struct main_loop_arg
*arg
= (struct main_loop_arg
*)targ
;
105 struct receiver_arg rarg
;
107 pthread_t reader_thread
;
109 if (arg
->usb_present
== 1) {
111 // pthread_create(&reader_thread, NULL, &usb_receiver, &rarg);
112 pthread_create(&reader_thread
, NULL
, &uart_receiver
, &rarg
);
115 FILE *script_file
= NULL
;
116 char script_cmd_buf
[256];
118 if (arg
->script_cmds_file
)
120 script_file
= fopen(arg
->script_cmds_file
, "r");
123 printf("using 'scripting' commands file %s\n", arg
->script_cmds_file
);
127 read_history(".history");
130 // If there is a script file
133 if (!fgets(script_cmd_buf
, sizeof(script_cmd_buf
), script_file
))
141 nl
= strrchr(script_cmd_buf
, '\r');
143 nl
= strrchr(script_cmd_buf
, '\n');
146 if ((cmd
= (char*) malloc(strlen(script_cmd_buf
))) != NULL
)
148 memset(cmd
, 0, strlen(script_cmd_buf
));
149 strcpy(cmd
, script_cmd_buf
);
157 cmd
= readline(PROXPROMPT
);
161 while(cmd
[strlen(cmd
) - 1] == ' ')
162 cmd
[strlen(cmd
) - 1] = 0x00;
164 if (cmd
[0] != 0x00) {
165 if (strncmp(cmd
, "quit", 4) == 0) {
169 CommandReceived(cmd
);
179 write_history(".history");
181 if (arg
->usb_present
== 1) {
183 pthread_join(reader_thread
, NULL
);
197 int main(int argc
, char* argv
[]) {
200 printf("syntax: %s <port>\n\n",argv
[0]);
204 // Make sure to initialize
205 struct main_loop_arg marg
= {
207 .script_cmds_file
= NULL
209 pthread_t main_loop_t
;
213 if (!OpenProxmark(1)) {
214 fprintf(stderr,"PROXMARK3: NOT FOUND!\n");
215 marg.usb_present = 0;
218 marg.usb_present = 1;
222 sp
= uart_open(argv
[1]);
223 if (sp
== INVALID_SERIAL_PORT
) {
224 printf("ERROR: invalid serial port\n");
225 marg
.usb_present
= 0;
228 marg
.usb_present
= 1;
232 // If the user passed the filename of the 'script' to execute, get it
233 if (argc
> 2 && argv
[2]) {
234 marg
.script_cmds_file
= argv
[2];
237 pthread_create(&main_loop_t
, NULL
, &main_loop
, &marg
);
238 InitGraphics(argc
, argv
);
242 pthread_join(main_loop_t
, NULL
);
244 // if (marg.usb_present == 1) {