]> git.zerfleddert.de Git - proxmark3-svn/blame - client/proxmark3.c
fixed read uart for windows
[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"
24#include "messages.h"
25#include "ui.h"
26
27static serial_port sp;
28
29void 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}
6658905f 37
902cb3c0 38struct receiver_arg {
7fe9b0b7 39 int run;
6658905f 40};
41
902cb3c0 42struct main_loop_arg {
7fe9b0b7 43 int usb_present;
1f947c4b 44 char *script_cmds_file;
a60612db 45};
46
902cb3c0 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//}
7fe9b0b7 61
902cb3c0 62byte_t rx[0x1000000];
63
64static void *uart_receiver(void *targ) {
65 struct receiver_arg *arg = (struct receiver_arg*)targ;
66 size_t rxlen;
67 size_t cmd_count;
68
7fe9b0b7 69 while (arg->run) {
af65f5f7 70 rxlen = sizeof(UsbCommand);
902cb3c0 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 }
7fe9b0b7 81 }
82 }
902cb3c0 83
7fe9b0b7 84 pthread_exit(NULL);
4cd41f34 85 return NULL;
6658905f 86}
87
1f947c4b 88
902cb3c0 89static 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)
1f947c4b 108 {
902cb3c0 109 printf("using 'scripting' commands file %s\n", arg->script_cmds_file);
1f947c4b 110 }
902cb3c0 111 }
7fe9b0b7 112
8556b852 113 read_history(".history");
1f947c4b 114 while(1)
902cb3c0 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)
1f947c4b 133 {
902cb3c0 134 memset(cmd, 0, strlen(script_cmd_buf));
135 strcpy(cmd, script_cmd_buf);
136 printf("%s\n", cmd);
137 }
138 }
139 }
1f947c4b 140
141 if (!script_file)
142 {
902cb3c0 143 cmd = readline(PROXPROMPT);
1f947c4b 144 }
145
8556b852
M
146 if (cmd) {
147 while(cmd[strlen(cmd) - 1] == ' ')
902cb3c0 148 cmd[strlen(cmd) - 1] = 0x00;
8556b852
M
149
150 if (cmd[0] != 0x00) {
151 if (strncmp(cmd, "quit", 4) == 0) {
8556b852
M
152 break;
153 }
154
155 CommandReceived(cmd);
156 add_history(cmd);
157 }
158 free(cmd);
159 } else {
160 printf("\n");
161 break;
162 }
163 }
902cb3c0 164
51969283 165 write_history(".history");
902cb3c0 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;
6658905f 181}
182
902cb3c0 183int main(int argc, char* argv[]) {
184
185 if (argc < 2) {
186 printf("syntax: %s <port>\n\n",argv[0]);
187 return 1;
188 }
189
1f947c4b 190 // Make sure to initialize
191 struct main_loop_arg marg = {
192 .usb_present = 0,
193 .script_cmds_file = NULL
194 };
7fe9b0b7 195 pthread_t main_loop_t;
1f947c4b 196
902cb3c0 197/*
198 usb_init();
7fe9b0b7 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 }
902cb3c0 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 }
7fe9b0b7 217
902cb3c0 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
7fe9b0b7 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
902cb3c0 230// if (marg.usb_present == 1) {
231// CloseProxmark();
232// }
233
234 // Clean up the port
235 uart_close(sp);
236
7fe9b0b7 237 return 0;
6658905f 238}
Impressum, Datenschutz