receiving/sending moved to one thread
[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;
f0ba6342 28static UsbCommand txcmd;
29static bool txcmd_pending = false;
902cb3c0 30
31void SendCommand(UsbCommand *c) {
32#if 0
33 printf("Sending %d bytes\n", sizeof(UsbCommand));
34#endif
f0ba6342 35/*
36 if (txcmd_pending) {
37 ERR("Sending command failed, previous command is still pending");
902cb3c0 38 }
f0ba6342 39*/
40 while(txcmd_pending);
41 txcmd = *c;
42 txcmd_pending = true;
902cb3c0 43}
6658905f 44
902cb3c0 45struct receiver_arg {
7fe9b0b7 46 int run;
6658905f 47};
48
902cb3c0 49struct main_loop_arg {
7fe9b0b7 50 int usb_present;
1f947c4b 51 char *script_cmds_file;
a60612db 52};
53
902cb3c0 54//static void *usb_receiver(void *targ) {
55// struct receiver_arg *arg = (struct receiver_arg*)targ;
56// UsbCommand cmdbuf;
57//
58// while (arg->run) {
59// if (ReceiveCommandPoll(&cmdbuf)) {
60// UsbCommandReceived(&cmdbuf);
61// fflush(NULL);
62// }
63// }
64//
65// pthread_exit(NULL);
66// return NULL;
67//}
7fe9b0b7 68
902cb3c0 69byte_t rx[0x1000000];
70
71static void *uart_receiver(void *targ) {
72 struct receiver_arg *arg = (struct receiver_arg*)targ;
73 size_t rxlen;
74 size_t cmd_count;
75
7fe9b0b7 76 while (arg->run) {
af65f5f7 77 rxlen = sizeof(UsbCommand);
902cb3c0 78 if (uart_receive(sp,rx,&rxlen)) {
79 if ((rxlen % sizeof(UsbCommand)) != 0) {
1282b0e6 80 PrintAndLog("ERROR: received %03zd bytes, which does not seem to be one or more command(s)\n",rxlen );
902cb3c0 81 continue;
82 }
83 cmd_count = rxlen / sizeof(UsbCommand);
84// printf("received %zd bytes, which represents %zd commands\n",rxlen, cmd_count);
85 for (size_t i=0; i<cmd_count; i++) {
86 UsbCommandReceived((UsbCommand*)(rx+(i*sizeof(UsbCommand))));
87 }
7fe9b0b7 88 }
f0ba6342 89
90 if(txcmd_pending) {
91 if (!uart_send(sp,(byte_t*)&txcmd,sizeof(UsbCommand))) {
92 PrintAndLog("Sending bytes to proxmark failed");
93 }
94 txcmd_pending = false;
95 }
7fe9b0b7 96 }
902cb3c0 97
7fe9b0b7 98 pthread_exit(NULL);
4cd41f34 99 return NULL;
6658905f 100}
101
1f947c4b 102
902cb3c0 103static void *main_loop(void *targ) {
104 struct main_loop_arg *arg = (struct main_loop_arg*)targ;
105 struct receiver_arg rarg;
106 char *cmd = NULL;
107 pthread_t reader_thread;
108
109 if (arg->usb_present == 1) {
110 rarg.run=1;
111 // pthread_create(&reader_thread, NULL, &usb_receiver, &rarg);
112 pthread_create(&reader_thread, NULL, &uart_receiver, &rarg);
113 }
114
115 FILE *script_file = NULL;
116 char script_cmd_buf[256];
117
118 if (arg->script_cmds_file)
119 {
120 script_file = fopen(arg->script_cmds_file, "r");
121 if (script_file)
1f947c4b 122 {
902cb3c0 123 printf("using 'scripting' commands file %s\n", arg->script_cmds_file);
1f947c4b 124 }
902cb3c0 125 }
7fe9b0b7 126
8556b852 127 read_history(".history");
1f947c4b 128 while(1)
902cb3c0 129 {
130 // If there is a script file
131 if (script_file)
132 {
133 if (!fgets(script_cmd_buf, sizeof(script_cmd_buf), script_file))
134 {
135 fclose(script_file);
136 script_file = NULL;
137 }
138 else
139 {
140 char *nl;
141 nl = strrchr(script_cmd_buf, '\r');
142 if (nl) *nl = '\0';
143 nl = strrchr(script_cmd_buf, '\n');
144 if (nl) *nl = '\0';
145
146 if ((cmd = (char*) malloc(strlen(script_cmd_buf))) != NULL)
1f947c4b 147 {
902cb3c0 148 memset(cmd, 0, strlen(script_cmd_buf));
149 strcpy(cmd, script_cmd_buf);
150 printf("%s\n", cmd);
151 }
152 }
153 }
1f947c4b 154
155 if (!script_file)
156 {
902cb3c0 157 cmd = readline(PROXPROMPT);
1f947c4b 158 }
159
8556b852
M
160 if (cmd) {
161 while(cmd[strlen(cmd) - 1] == ' ')
902cb3c0 162 cmd[strlen(cmd) - 1] = 0x00;
8556b852
M
163
164 if (cmd[0] != 0x00) {
165 if (strncmp(cmd, "quit", 4) == 0) {
8556b852
M
166 break;
167 }
168
169 CommandReceived(cmd);
170 add_history(cmd);
171 }
172 free(cmd);
173 } else {
174 printf("\n");
175 break;
176 }
177 }
902cb3c0 178
51969283 179 write_history(".history");
902cb3c0 180
181 if (arg->usb_present == 1) {
182 rarg.run = 0;
183 pthread_join(reader_thread, NULL);
184 }
185
186 if (script_file)
187 {
188 fclose(script_file);
189 script_file = NULL;
190 }
191
192 ExitGraphics();
193 pthread_exit(NULL);
194 return NULL;
6658905f 195}
196
902cb3c0 197int main(int argc, char* argv[]) {
198
199 if (argc < 2) {
200 printf("syntax: %s <port>\n\n",argv[0]);
201 return 1;
202 }
203
1f947c4b 204 // Make sure to initialize
205 struct main_loop_arg marg = {
206 .usb_present = 0,
207 .script_cmds_file = NULL
208 };
7fe9b0b7 209 pthread_t main_loop_t;
1f947c4b 210
902cb3c0 211/*
212 usb_init();
7fe9b0b7 213 if (!OpenProxmark(1)) {
214 fprintf(stderr,"PROXMARK3: NOT FOUND!\n");
215 marg.usb_present = 0;
216 offline = 1;
217 } else {
218 marg.usb_present = 1;
219 offline = 0;
220 }
902cb3c0 221*/
222 sp = uart_open(argv[1]);
223 if (sp == INVALID_SERIAL_PORT) {
224 printf("ERROR: invalid serial port\n");
225 marg.usb_present = 0;
226 offline = 1;
227 } else {
228 marg.usb_present = 1;
229 offline = 0;
230 }
7fe9b0b7 231
902cb3c0 232 // If the user passed the filename of the 'script' to execute, get it
233 if (argc > 2 && argv[2]) {
234 marg.script_cmds_file = argv[2];
235 }
236
7fe9b0b7 237 pthread_create(&main_loop_t, NULL, &main_loop, &marg);
238 InitGraphics(argc, argv);
239
240 MainGraphics();
241
242 pthread_join(main_loop_t, NULL);
243
902cb3c0 244// if (marg.usb_present == 1) {
245// CloseProxmark();
246// }
247
248 // Clean up the port
249 uart_close(sp);
250
7fe9b0b7 251 return 0;
6658905f 252}
Impressum, Datenschutz