]> cvs.zerfleddert.de Git - proxmark3-svn/blame_incremental - client/proxmark3.c
fixed some more compiler warnings
[proxmark3-svn] / client / proxmark3.c
... / ...
CommitLineData
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 "ui.h"
25#include "sleep.h"
26
27// a global mutex to prevent interlaced printing from different threads
28pthread_mutex_t print_lock;
29
30static serial_port sp;
31static UsbCommand txcmd;
32volatile static bool txcmd_pending = false;
33
34
35void SendCommand(UsbCommand *c) {
36#if 0
37 printf("Sending %d bytes\n", sizeof(UsbCommand));
38#endif
39/*
40 if (txcmd_pending) {
41 ERR("Sending command failed, previous command is still pending");
42 }
43*/
44 while(txcmd_pending);
45 txcmd = *c;
46 txcmd_pending = true;
47}
48
49struct receiver_arg {
50 int run;
51};
52
53struct main_loop_arg {
54 int usb_present;
55 char *script_cmds_file;
56};
57
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//}
72
73byte_t rx[0x1000000];
74byte_t* prx = rx;
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
81 while (arg->run) {
82 rxlen = sizeof(UsbCommand);
83 if (uart_receive(sp,prx,&rxlen)) {
84 prx += rxlen;
85 if (((prx-rx) % sizeof(UsbCommand)) != 0) {
86 continue;
87 }
88 cmd_count = (prx-rx) / sizeof(UsbCommand);
89 // printf("received %d bytes, which represents %d commands\n",(prx-rx), cmd_count);
90 for (size_t i=0; i<cmd_count; i++) {
91 UsbCommandReceived((UsbCommand*)(rx+(i*sizeof(UsbCommand))));
92 }
93 }
94 prx = rx;
95
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 }
102 }
103
104 pthread_exit(NULL);
105 return NULL;
106}
107
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)
127 {
128 printf("using 'scripting' commands file %s\n", arg->script_cmds_file);
129 }
130 }
131
132 read_history(".history");
133 while(1)
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
151 if ((cmd = (char*) malloc(strlen(script_cmd_buf) + 1)) != NULL)
152 {
153 memset(cmd, 0, strlen(script_cmd_buf));
154 strcpy(cmd, script_cmd_buf);
155 printf("%s\n", cmd);
156 }
157 }
158 }
159
160 if (!script_file)
161 {
162 cmd = readline(PROXPROMPT);
163 }
164
165 if (cmd) {
166 while(cmd[strlen(cmd) - 1] == ' ')
167 cmd[strlen(cmd) - 1] = 0x00;
168
169 if (cmd[0] != 0x00) {
170 if (strncmp(cmd, "quit", 4) == 0) {
171 break;
172 }
173
174 CommandReceived(cmd);
175 add_history(cmd);
176 }
177 free(cmd);
178 } else {
179 printf("\n");
180 break;
181 }
182 }
183
184 write_history(".history");
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;
200}
201
202int main(int argc, char* argv[]) {
203 srand(time(0));
204
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 }
210
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;
217
218/*
219 usb_init();
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 }
228*/
229
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 }
243
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 }
248
249 // create a mutex to avoid interlacing print commands from our different threads
250 pthread_mutex_init(&print_lock, NULL);
251
252 pthread_create(&main_loop_t, NULL, &main_loop, &marg);
253 InitGraphics(argc, argv);
254
255 MainGraphics();
256
257 pthread_join(main_loop_t, NULL);
258
259// if (marg.usb_present == 1) {
260// CloseProxmark();
261// }
262
263 // Clean up the port
264 uart_close(sp);
265
266 // clean up mutex
267 pthread_mutex_destroy(&print_lock);
268
269 return 0;
270}
Impressum, Datenschutz