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