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