]> cvs.zerfleddert.de Git - proxmark3-svn/blame - client/proxmark3.c
fix compilation issues with gcc 4.6: anonymous unions are not c99
[proxmark3-svn] / client / proxmark3.c
CommitLineData
a553f267 1//-----------------------------------------------------------------------------
212ef3a0 2// Copyright (C) 2009 Michael Gernoth <michael at gernoth.net>
a553f267 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
6658905f 12#include <stdio.h>
590f8ff9 13#include <stdlib.h>
6658905f 14#include <string.h>
7fe9b0b7 15#include <pthread.h>
8556b852 16#include <unistd.h>
6658905f 17#include <readline/readline.h>
18#include <readline/history.h>
902cb3c0 19//#include "proxusb.h"
6658905f 20#include "proxmark3.h"
21#include "proxgui.h"
7fe9b0b7 22#include "cmdmain.h"
902cb3c0 23#include "uart.h"
902cb3c0 24#include "ui.h"
759c16b3 25#include "sleep.h"
902cb3c0 26
9492e0b0 27// a global mutex to prevent interlaced printing from different threads
28pthread_mutex_t print_lock;
29
902cb3c0 30static serial_port sp;
f0ba6342 31static UsbCommand txcmd;
c3385024 32volatile static bool txcmd_pending = false;
902cb3c0 33
9492e0b0 34
902cb3c0 35void SendCommand(UsbCommand *c) {
36#if 0
37 printf("Sending %d bytes\n", sizeof(UsbCommand));
38#endif
f0ba6342 39/*
40 if (txcmd_pending) {
41 ERR("Sending command failed, previous command is still pending");
902cb3c0 42 }
f0ba6342 43*/
44 while(txcmd_pending);
45 txcmd = *c;
46 txcmd_pending = true;
902cb3c0 47}
6658905f 48
902cb3c0 49struct receiver_arg {
7fe9b0b7 50 int run;
6658905f 51};
52
902cb3c0 53struct main_loop_arg {
7fe9b0b7 54 int usb_present;
1f947c4b 55 char *script_cmds_file;
a60612db 56};
57
902cb3c0 58//static void *usb_receiver(void *targ) {
59// struct receiver_arg *arg = (struct receiver_arg*)targ;
60// UsbCommand cmdbuf;
61//
62// while (arg->run) {
63// if (ReceiveCommandPoll(&cmdbuf)) {
64// UsbCommandReceived(&cmdbuf);
65// fflush(NULL);
66// }
67// }
68//
69// pthread_exit(NULL);
70// return NULL;
71//}
7fe9b0b7 72
902cb3c0 73byte_t rx[0x1000000];
fe7bfa78 74byte_t* prx = rx;
902cb3c0 75
76static void *uart_receiver(void *targ) {
77 struct receiver_arg *arg = (struct receiver_arg*)targ;
78 size_t rxlen;
79 size_t cmd_count;
80
7fe9b0b7 81 while (arg->run) {
af65f5f7 82 rxlen = sizeof(UsbCommand);
fe7bfa78 83 if (uart_receive(sp,prx,&rxlen)) {
84 prx += rxlen;
85 if (((prx-rx) % sizeof(UsbCommand)) != 0) {
902cb3c0 86 continue;
87 }
fe7bfa78 88 cmd_count = (prx-rx) / sizeof(UsbCommand);
89 // printf("received %d bytes, which represents %d commands\n",(prx-rx), cmd_count);
902cb3c0 90 for (size_t i=0; i<cmd_count; i++) {
91 UsbCommandReceived((UsbCommand*)(rx+(i*sizeof(UsbCommand))));
92 }
7fe9b0b7 93 }
fe7bfa78 94 prx = rx;
95
f0ba6342 96 if(txcmd_pending) {
97 if (!uart_send(sp,(byte_t*)&txcmd,sizeof(UsbCommand))) {
98 PrintAndLog("Sending bytes to proxmark failed");
99 }
100 txcmd_pending = false;
101 }
7fe9b0b7 102 }
902cb3c0 103
7fe9b0b7 104 pthread_exit(NULL);
4cd41f34 105 return NULL;
6658905f 106}
107
902cb3c0 108static void *main_loop(void *targ) {
109 struct main_loop_arg *arg = (struct main_loop_arg*)targ;
110 struct receiver_arg rarg;
111 char *cmd = NULL;
112 pthread_t reader_thread;
113
114 if (arg->usb_present == 1) {
115 rarg.run=1;
116 // pthread_create(&reader_thread, NULL, &usb_receiver, &rarg);
117 pthread_create(&reader_thread, NULL, &uart_receiver, &rarg);
118 }
119
120 FILE *script_file = NULL;
121 char script_cmd_buf[256];
122
123 if (arg->script_cmds_file)
124 {
125 script_file = fopen(arg->script_cmds_file, "r");
126 if (script_file)
1f947c4b 127 {
902cb3c0 128 printf("using 'scripting' commands file %s\n", arg->script_cmds_file);
1f947c4b 129 }
902cb3c0 130 }
7fe9b0b7 131
8556b852 132 read_history(".history");
1f947c4b 133 while(1)
902cb3c0 134 {
135 // If there is a script file
136 if (script_file)
137 {
138 if (!fgets(script_cmd_buf, sizeof(script_cmd_buf), script_file))
139 {
140 fclose(script_file);
141 script_file = NULL;
142 }
143 else
144 {
145 char *nl;
146 nl = strrchr(script_cmd_buf, '\r');
147 if (nl) *nl = '\0';
148 nl = strrchr(script_cmd_buf, '\n');
149 if (nl) *nl = '\0';
150
50d6e4ab 151 if ((cmd = (char*) malloc(strlen(script_cmd_buf) + 1)) != NULL)
1f947c4b 152 {
902cb3c0 153 memset(cmd, 0, strlen(script_cmd_buf));
154 strcpy(cmd, script_cmd_buf);
155 printf("%s\n", cmd);
156 }
157 }
158 }
1f947c4b 159
160 if (!script_file)
161 {
902cb3c0 162 cmd = readline(PROXPROMPT);
1f947c4b 163 }
164
8556b852
M
165 if (cmd) {
166 while(cmd[strlen(cmd) - 1] == ' ')
902cb3c0 167 cmd[strlen(cmd) - 1] = 0x00;
8556b852
M
168
169 if (cmd[0] != 0x00) {
170 if (strncmp(cmd, "quit", 4) == 0) {
981bd429 171 exit(0);
8556b852
M
172 break;
173 }
174
175 CommandReceived(cmd);
176 add_history(cmd);
177 }
178 free(cmd);
179 } else {
180 printf("\n");
181 break;
182 }
183 }
902cb3c0 184
51969283 185 write_history(".history");
902cb3c0 186
187 if (arg->usb_present == 1) {
188 rarg.run = 0;
189 pthread_join(reader_thread, NULL);
190 }
191
192 if (script_file)
193 {
194 fclose(script_file);
195 script_file = NULL;
196 }
197
198 ExitGraphics();
199 pthread_exit(NULL);
200 return NULL;
6658905f 201}
202
902cb3c0 203int main(int argc, char* argv[]) {
9492e0b0 204 srand(time(0));
125a98a1 205
9492e0b0 206 if (argc < 2) {
207 printf("syntax: %s <port>\n\n",argv[0]);
208 printf("\tLinux example:'%s /dev/ttyACM0'\n\n", argv[0]);
209 return 1;
210 }
902cb3c0 211
9492e0b0 212 // Make sure to initialize
213 struct main_loop_arg marg = {
214 .usb_present = 0,
215 .script_cmds_file = NULL
216 };
217 pthread_t main_loop_t;
1f947c4b 218
902cb3c0 219/*
220 usb_init();
7fe9b0b7 221 if (!OpenProxmark(1)) {
222 fprintf(stderr,"PROXMARK3: NOT FOUND!\n");
223 marg.usb_present = 0;
224 offline = 1;
225 } else {
226 marg.usb_present = 1;
227 offline = 0;
228 }
902cb3c0 229*/
4890730a 230
9492e0b0 231 sp = uart_open(argv[1]);
232 if (sp == INVALID_SERIAL_PORT) {
233 printf("ERROR: invalid serial port\n");
234 marg.usb_present = 0;
235 offline = 1;
236 } else if (sp == CLAIMED_SERIAL_PORT) {
237 printf("ERROR: serial port is claimed by another process\n");
238 marg.usb_present = 0;
239 offline = 1;
240 } else {
241 marg.usb_present = 1;
242 offline = 0;
243 }
7fe9b0b7 244
9492e0b0 245 // If the user passed the filename of the 'script' to execute, get it
246 if (argc > 2 && argv[2]) {
ed77aabe 247 if (argv[2][0] == 'f' && //buzzy, if a word 'flush' passed, flush the output after every log entry.
248 argv[2][1] == 'l' &&
249 argv[2][2] == 'u' &&
250 argv[2][3] == 's' &&
251 argv[2][4] == 'h')
252 {
253 printf("Output will be flushed after every print.\n");
254 flushAfterWrite = 1;
255 }
256 else
9492e0b0 257 marg.script_cmds_file = argv[2];
258 }
ed77aabe 259
9492e0b0 260 // create a mutex to avoid interlacing print commands from our different threads
261 pthread_mutex_init(&print_lock, NULL);
7fe9b0b7 262
9492e0b0 263 pthread_create(&main_loop_t, NULL, &main_loop, &marg);
264 InitGraphics(argc, argv);
7fe9b0b7 265
9492e0b0 266 MainGraphics();
267
268 pthread_join(main_loop_t, NULL);
7fe9b0b7 269
902cb3c0 270// if (marg.usb_present == 1) {
271// CloseProxmark();
272// }
273
9492e0b0 274 // Clean up the port
275 uart_close(sp);
276
277 // clean up mutex
278 pthread_mutex_destroy(&print_lock);
902cb3c0 279
7fe9b0b7 280 return 0;
6658905f 281}
Impressum, Datenschutz