]>
Commit | Line | Data |
---|---|---|
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> | |
7fe9b0b7 | 19 | #include "proxusb.h" |
6658905f | 20 | #include "proxmark3.h" |
21 | #include "proxgui.h" | |
7fe9b0b7 | 22 | #include "cmdmain.h" |
6658905f | 23 | |
7fe9b0b7 | 24 | struct usb_receiver_arg |
25 | { | |
26 | int run; | |
6658905f | 27 | }; |
28 | ||
7fe9b0b7 | 29 | struct main_loop_arg |
30 | { | |
31 | int usb_present; | |
a60612db | 32 | }; |
33 | ||
7fe9b0b7 | 34 | static void *usb_receiver(void *targ) |
35 | { | |
36 | struct usb_receiver_arg *arg = (struct usb_receiver_arg*)targ; | |
37 | UsbCommand cmdbuf; | |
38 | ||
39 | while (arg->run) { | |
40 | if (ReceiveCommandPoll(&cmdbuf)) { | |
7fe9b0b7 | 41 | UsbCommandReceived(&cmdbuf); |
7fe9b0b7 | 42 | fflush(NULL); |
43 | } | |
44 | } | |
45 | ||
46 | pthread_exit(NULL); | |
4cd41f34 | 47 | return NULL; |
6658905f | 48 | } |
49 | ||
50 | static void *main_loop(void *targ) | |
51 | { | |
7fe9b0b7 | 52 | struct main_loop_arg *arg = (struct main_loop_arg*)targ; |
53 | struct usb_receiver_arg rarg; | |
54 | char *cmd = NULL; | |
55 | pthread_t reader_thread; | |
56 | ||
57 | if (arg->usb_present == 1) { | |
58 | rarg.run=1; | |
59 | pthread_create(&reader_thread, NULL, &usb_receiver, &rarg); | |
60 | } | |
61 | ||
8556b852 M |
62 | read_history(".history"); |
63 | while(1) { | |
64 | cmd = readline(PROXPROMPT); | |
65 | if (cmd) { | |
66 | while(cmd[strlen(cmd) - 1] == ' ') | |
67 | cmd[strlen(cmd) - 1] = 0x00; | |
68 | ||
69 | if (cmd[0] != 0x00) { | |
70 | if (strncmp(cmd, "quit", 4) == 0) { | |
8556b852 M |
71 | break; |
72 | } | |
73 | ||
74 | CommandReceived(cmd); | |
75 | add_history(cmd); | |
76 | } | |
77 | free(cmd); | |
78 | } else { | |
79 | printf("\n"); | |
80 | break; | |
81 | } | |
82 | } | |
7fe9b0b7 | 83 | |
51969283 M |
84 | write_history(".history"); |
85 | ||
7fe9b0b7 | 86 | if (arg->usb_present == 1) { |
87 | rarg.run = 0; | |
88 | pthread_join(reader_thread, NULL); | |
89 | } | |
90 | ||
91 | ExitGraphics(); | |
92 | pthread_exit(NULL); | |
4cd41f34 | 93 | return NULL; |
6658905f | 94 | } |
95 | ||
96 | int main(int argc, char **argv) | |
97 | { | |
7fe9b0b7 | 98 | struct main_loop_arg marg; |
99 | pthread_t main_loop_t; | |
100 | usb_init(); | |
101 | ||
102 | if (!OpenProxmark(1)) { | |
103 | fprintf(stderr,"PROXMARK3: NOT FOUND!\n"); | |
104 | marg.usb_present = 0; | |
105 | offline = 1; | |
106 | } else { | |
107 | marg.usb_present = 1; | |
108 | offline = 0; | |
109 | } | |
110 | ||
111 | pthread_create(&main_loop_t, NULL, &main_loop, &marg); | |
112 | InitGraphics(argc, argv); | |
113 | ||
114 | MainGraphics(); | |
115 | ||
116 | pthread_join(main_loop_t, NULL); | |
117 | ||
118 | if (marg.usb_present == 1) { | |
119 | CloseProxmark(); | |
120 | } | |
121 | return 0; | |
6658905f | 122 | } |