]> cvs.zerfleddert.de Git - proxmark3-svn/blame - client/proxmark3.c
fixed some more compiler warnings
[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) {
8556b852
M
171 break;
172 }
173
174 CommandReceived(cmd);
175 add_history(cmd);
176 }
177 free(cmd);
178 } else {
179 printf("\n");
180 break;
181 }
182 }
902cb3c0 183
51969283 184 write_history(".history");
902cb3c0 185
186 if (arg->usb_present == 1) {
187 rarg.run = 0;
188 pthread_join(reader_thread, NULL);
189 }
190
191 if (script_file)
192 {
193 fclose(script_file);
194 script_file = NULL;
195 }
196
197 ExitGraphics();
198 pthread_exit(NULL);
199 return NULL;
6658905f 200}
201
902cb3c0 202int main(int argc, char* argv[]) {
9492e0b0 203 srand(time(0));
125a98a1 204
9492e0b0 205 if (argc < 2) {
206 printf("syntax: %s <port>\n\n",argv[0]);
207 printf("\tLinux example:'%s /dev/ttyACM0'\n\n", argv[0]);
208 return 1;
209 }
902cb3c0 210
9492e0b0 211 // Make sure to initialize
212 struct main_loop_arg marg = {
213 .usb_present = 0,
214 .script_cmds_file = NULL
215 };
216 pthread_t main_loop_t;
1f947c4b 217
902cb3c0 218/*
219 usb_init();
7fe9b0b7 220 if (!OpenProxmark(1)) {
221 fprintf(stderr,"PROXMARK3: NOT FOUND!\n");
222 marg.usb_present = 0;
223 offline = 1;
224 } else {
225 marg.usb_present = 1;
226 offline = 0;
227 }
902cb3c0 228*/
4890730a 229
9492e0b0 230 sp = uart_open(argv[1]);
231 if (sp == INVALID_SERIAL_PORT) {
232 printf("ERROR: invalid serial port\n");
233 marg.usb_present = 0;
234 offline = 1;
235 } else if (sp == CLAIMED_SERIAL_PORT) {
236 printf("ERROR: serial port is claimed by another process\n");
237 marg.usb_present = 0;
238 offline = 1;
239 } else {
240 marg.usb_present = 1;
241 offline = 0;
242 }
7fe9b0b7 243
9492e0b0 244 // If the user passed the filename of the 'script' to execute, get it
245 if (argc > 2 && argv[2]) {
246 marg.script_cmds_file = argv[2];
247 }
902cb3c0 248
9492e0b0 249 // create a mutex to avoid interlacing print commands from our different threads
250 pthread_mutex_init(&print_lock, NULL);
7fe9b0b7 251
9492e0b0 252 pthread_create(&main_loop_t, NULL, &main_loop, &marg);
253 InitGraphics(argc, argv);
7fe9b0b7 254
9492e0b0 255 MainGraphics();
256
257 pthread_join(main_loop_t, NULL);
7fe9b0b7 258
902cb3c0 259// if (marg.usb_present == 1) {
260// CloseProxmark();
261// }
262
9492e0b0 263 // Clean up the port
264 uart_close(sp);
265
266 // clean up mutex
267 pthread_mutex_destroy(&print_lock);
902cb3c0 268
7fe9b0b7 269 return 0;
6658905f 270}
Impressum, Datenschutz