]> git.zerfleddert.de Git - proxmark3-svn/blob - client/proxmark3.c
(dirty) fix for linux to clean up the /dev/ttyACM* quicker
[proxmark3-svn] / client / proxmark3.c
1 //-----------------------------------------------------------------------------
2 // Copyright (C) 2009 Michael Gernoth <michael at gernoth.net>
3 // Copyright (C) 2010 iZsh <izsh at fail0verflow.com>
4 //
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
7 // the license.
8 //-----------------------------------------------------------------------------
9 // Main binary
10 //-----------------------------------------------------------------------------
11
12 #include <stdio.h>
13 #include <stdlib.h>
14 #include <string.h>
15 #include <pthread.h>
16 #include <unistd.h>
17 #include <readline/readline.h>
18 #include <readline/history.h>
19 //#include "proxusb.h"
20 #include "proxmark3.h"
21 #include "proxgui.h"
22 #include "cmdmain.h"
23 #include "uart.h"
24 #include "messages.h"
25 #include "ui.h"
26 #include "sleep.h"
27
28 static serial_port sp;
29 static UsbCommand txcmd;
30 static bool txcmd_pending = false;
31
32 void SendCommand(UsbCommand *c) {
33 #if 0
34 printf("Sending %d bytes\n", sizeof(UsbCommand));
35 #endif
36 /*
37 if (txcmd_pending) {
38 ERR("Sending command failed, previous command is still pending");
39 }
40 */
41 while(txcmd_pending);
42 txcmd = *c;
43 txcmd_pending = true;
44 }
45
46 struct receiver_arg {
47 int run;
48 };
49
50 struct main_loop_arg {
51 int usb_present;
52 char *script_cmds_file;
53 };
54
55 //static void *usb_receiver(void *targ) {
56 // struct receiver_arg *arg = (struct receiver_arg*)targ;
57 // UsbCommand cmdbuf;
58 //
59 // while (arg->run) {
60 // if (ReceiveCommandPoll(&cmdbuf)) {
61 // UsbCommandReceived(&cmdbuf);
62 // fflush(NULL);
63 // }
64 // }
65 //
66 // pthread_exit(NULL);
67 // return NULL;
68 //}
69
70 byte_t rx[0x1000000];
71 byte_t* prx = rx;
72
73 static void *uart_receiver(void *targ) {
74 struct receiver_arg *arg = (struct receiver_arg*)targ;
75 size_t rxlen;
76 size_t cmd_count;
77
78 while (arg->run) {
79 rxlen = sizeof(UsbCommand);
80 if (uart_receive(sp,prx,&rxlen)) {
81 prx += rxlen;
82 if (((prx-rx) % sizeof(UsbCommand)) != 0) {
83 continue;
84 }
85 cmd_count = (prx-rx) / sizeof(UsbCommand);
86 // printf("received %d bytes, which represents %d commands\n",(prx-rx), cmd_count);
87 for (size_t i=0; i<cmd_count; i++) {
88 UsbCommandReceived((UsbCommand*)(rx+(i*sizeof(UsbCommand))));
89 }
90 }
91 prx = rx;
92
93 if(txcmd_pending) {
94 if (!uart_send(sp,(byte_t*)&txcmd,sizeof(UsbCommand))) {
95 PrintAndLog("Sending bytes to proxmark failed");
96 }
97 txcmd_pending = false;
98 }
99 }
100
101 pthread_exit(NULL);
102 return NULL;
103 }
104
105 static void *main_loop(void *targ) {
106 struct main_loop_arg *arg = (struct main_loop_arg*)targ;
107 struct receiver_arg rarg;
108 char *cmd = NULL;
109 pthread_t reader_thread;
110
111 if (arg->usb_present == 1) {
112 rarg.run=1;
113 // pthread_create(&reader_thread, NULL, &usb_receiver, &rarg);
114 pthread_create(&reader_thread, NULL, &uart_receiver, &rarg);
115 }
116
117 FILE *script_file = NULL;
118 char script_cmd_buf[256];
119
120 if (arg->script_cmds_file)
121 {
122 script_file = fopen(arg->script_cmds_file, "r");
123 if (script_file)
124 {
125 printf("using 'scripting' commands file %s\n", arg->script_cmds_file);
126 }
127 }
128
129 read_history(".history");
130 while(1)
131 {
132 // If there is a script file
133 if (script_file)
134 {
135 if (!fgets(script_cmd_buf, sizeof(script_cmd_buf), script_file))
136 {
137 fclose(script_file);
138 script_file = NULL;
139 }
140 else
141 {
142 char *nl;
143 nl = strrchr(script_cmd_buf, '\r');
144 if (nl) *nl = '\0';
145 nl = strrchr(script_cmd_buf, '\n');
146 if (nl) *nl = '\0';
147
148 if ((cmd = (char*) malloc(strlen(script_cmd_buf) + 1)) != NULL)
149 {
150 memset(cmd, 0, strlen(script_cmd_buf));
151 strcpy(cmd, script_cmd_buf);
152 printf("%s\n", cmd);
153 }
154 }
155 }
156
157 if (!script_file)
158 {
159 cmd = readline(PROXPROMPT);
160 }
161
162 if (cmd) {
163 while(cmd[strlen(cmd) - 1] == ' ')
164 cmd[strlen(cmd) - 1] = 0x00;
165
166 if (cmd[0] != 0x00) {
167 if (strncmp(cmd, "quit", 4) == 0) {
168 break;
169 }
170
171 CommandReceived(cmd);
172 add_history(cmd);
173 }
174 free(cmd);
175 } else {
176 printf("\n");
177 break;
178 }
179 }
180
181 write_history(".history");
182
183 if (arg->usb_present == 1) {
184 rarg.run = 0;
185 pthread_join(reader_thread, NULL);
186 }
187
188 if (script_file)
189 {
190 fclose(script_file);
191 script_file = NULL;
192 }
193
194 ExitGraphics();
195 pthread_exit(NULL);
196 return NULL;
197 }
198
199 int main(int argc, char* argv[]) {
200 srand(time(0));
201
202 if (argc < 2) {
203 printf("syntax: %s <port>\n\n",argv[0]);
204 return 1;
205 }
206
207 // Make sure to initialize
208 struct main_loop_arg marg = {
209 .usb_present = 0,
210 .script_cmds_file = NULL
211 };
212 pthread_t main_loop_t;
213
214 /*
215 usb_init();
216 if (!OpenProxmark(1)) {
217 fprintf(stderr,"PROXMARK3: NOT FOUND!\n");
218 marg.usb_present = 0;
219 offline = 1;
220 } else {
221 marg.usb_present = 1;
222 offline = 0;
223 }
224 */
225 sp = uart_open(argv[1]);
226 if (sp == INVALID_SERIAL_PORT) {
227 printf("ERROR: invalid serial port\n");
228 marg.usb_present = 0;
229 offline = 1;
230 } else {
231 marg.usb_present = 1;
232 offline = 0;
233 }
234
235 // If the user passed the filename of the 'script' to execute, get it
236 if (argc > 2 && argv[2]) {
237 marg.script_cmds_file = argv[2];
238 }
239
240 pthread_create(&main_loop_t, NULL, &main_loop, &marg);
241 InitGraphics(argc, argv);
242
243 MainGraphics();
244
245 pthread_join(main_loop_t, NULL);
246
247 // if (marg.usb_present == 1) {
248 // CloseProxmark();
249 // }
250
251 // Clean up the port
252 uart_close(sp);
253
254 return 0;
255 }
Impressum, Datenschutz