]> git.zerfleddert.de Git - proxmark3-svn/blame - client/proxmark3.c
fixed missing header
[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) {
902cb3c0 70 if (uart_receive(sp,rx,&rxlen)) {
71 if ((rxlen % sizeof(UsbCommand)) != 0) {
72 PrintAndLog("ERROR: received %zd bytes, which does not seem to be one or more command(s)\n",rxlen);
73 continue;
74 }
75 cmd_count = rxlen / sizeof(UsbCommand);
76// printf("received %zd bytes, which represents %zd commands\n",rxlen, cmd_count);
77 for (size_t i=0; i<cmd_count; i++) {
78 UsbCommandReceived((UsbCommand*)(rx+(i*sizeof(UsbCommand))));
79 }
7fe9b0b7 80 }
81 }
902cb3c0 82
7fe9b0b7 83 pthread_exit(NULL);
4cd41f34 84 return NULL;
6658905f 85}
86
1f947c4b 87
902cb3c0 88static void *main_loop(void *targ) {
89 struct main_loop_arg *arg = (struct main_loop_arg*)targ;
90 struct receiver_arg rarg;
91 char *cmd = NULL;
92 pthread_t reader_thread;
93
94 if (arg->usb_present == 1) {
95 rarg.run=1;
96 // pthread_create(&reader_thread, NULL, &usb_receiver, &rarg);
97 pthread_create(&reader_thread, NULL, &uart_receiver, &rarg);
98 }
99
100 FILE *script_file = NULL;
101 char script_cmd_buf[256];
102
103 if (arg->script_cmds_file)
104 {
105 script_file = fopen(arg->script_cmds_file, "r");
106 if (script_file)
1f947c4b 107 {
902cb3c0 108 printf("using 'scripting' commands file %s\n", arg->script_cmds_file);
1f947c4b 109 }
902cb3c0 110 }
7fe9b0b7 111
8556b852 112 read_history(".history");
1f947c4b 113 while(1)
902cb3c0 114 {
115 // If there is a script file
116 if (script_file)
117 {
118 if (!fgets(script_cmd_buf, sizeof(script_cmd_buf), script_file))
119 {
120 fclose(script_file);
121 script_file = NULL;
122 }
123 else
124 {
125 char *nl;
126 nl = strrchr(script_cmd_buf, '\r');
127 if (nl) *nl = '\0';
128 nl = strrchr(script_cmd_buf, '\n');
129 if (nl) *nl = '\0';
130
131 if ((cmd = (char*) malloc(strlen(script_cmd_buf))) != NULL)
1f947c4b 132 {
902cb3c0 133 memset(cmd, 0, strlen(script_cmd_buf));
134 strcpy(cmd, script_cmd_buf);
135 printf("%s\n", cmd);
136 }
137 }
138 }
1f947c4b 139
140 if (!script_file)
141 {
902cb3c0 142 cmd = readline(PROXPROMPT);
1f947c4b 143 }
144
8556b852
M
145 if (cmd) {
146 while(cmd[strlen(cmd) - 1] == ' ')
902cb3c0 147 cmd[strlen(cmd) - 1] = 0x00;
8556b852
M
148
149 if (cmd[0] != 0x00) {
150 if (strncmp(cmd, "quit", 4) == 0) {
8556b852
M
151 break;
152 }
153
154 CommandReceived(cmd);
155 add_history(cmd);
156 }
157 free(cmd);
158 } else {
159 printf("\n");
160 break;
161 }
162 }
902cb3c0 163
51969283 164 write_history(".history");
902cb3c0 165
166 if (arg->usb_present == 1) {
167 rarg.run = 0;
168 pthread_join(reader_thread, NULL);
169 }
170
171 if (script_file)
172 {
173 fclose(script_file);
174 script_file = NULL;
175 }
176
177 ExitGraphics();
178 pthread_exit(NULL);
179 return NULL;
6658905f 180}
181
902cb3c0 182int main(int argc, char* argv[]) {
183
184 if (argc < 2) {
185 printf("syntax: %s <port>\n\n",argv[0]);
186 return 1;
187 }
188
1f947c4b 189 // Make sure to initialize
190 struct main_loop_arg marg = {
191 .usb_present = 0,
192 .script_cmds_file = NULL
193 };
7fe9b0b7 194 pthread_t main_loop_t;
1f947c4b 195
902cb3c0 196/*
197 usb_init();
7fe9b0b7 198 if (!OpenProxmark(1)) {
199 fprintf(stderr,"PROXMARK3: NOT FOUND!\n");
200 marg.usb_present = 0;
201 offline = 1;
202 } else {
203 marg.usb_present = 1;
204 offline = 0;
205 }
902cb3c0 206*/
207 sp = uart_open(argv[1]);
208 if (sp == INVALID_SERIAL_PORT) {
209 printf("ERROR: invalid serial port\n");
210 marg.usb_present = 0;
211 offline = 1;
212 } else {
213 marg.usb_present = 1;
214 offline = 0;
215 }
7fe9b0b7 216
902cb3c0 217 // If the user passed the filename of the 'script' to execute, get it
218 if (argc > 2 && argv[2]) {
219 marg.script_cmds_file = argv[2];
220 }
221
7fe9b0b7 222 pthread_create(&main_loop_t, NULL, &main_loop, &marg);
223 InitGraphics(argc, argv);
224
225 MainGraphics();
226
227 pthread_join(main_loop_t, NULL);
228
902cb3c0 229// if (marg.usb_present == 1) {
230// CloseProxmark();
231// }
232
233 // Clean up the port
234 uart_close(sp);
235
7fe9b0b7 236 return 0;
6658905f 237}
Impressum, Datenschutz