]>
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)) { | |
41 | for (int i = 0; i < strlen(PROXPROMPT); i++) | |
42 | putchar(0x08); | |
43 | UsbCommandReceived(&cmdbuf); | |
8556b852 M |
44 | // there is a big bug ) |
45 | if (cmdbuf.cmd > 0x0100 && cmdbuf.cmd < 0x0110) { // debug commands | |
46 | rl_on_new_line_with_prompt(); | |
47 | rl_forced_update_display(); | |
48 | } | |
7fe9b0b7 | 49 | fflush(NULL); |
50 | } | |
51 | } | |
52 | ||
53 | pthread_exit(NULL); | |
4cd41f34 | 54 | return NULL; |
6658905f | 55 | } |
56 | ||
57 | static void *main_loop(void *targ) | |
58 | { | |
7fe9b0b7 | 59 | struct main_loop_arg *arg = (struct main_loop_arg*)targ; |
60 | struct usb_receiver_arg rarg; | |
61 | char *cmd = NULL; | |
62 | pthread_t reader_thread; | |
63 | ||
64 | if (arg->usb_present == 1) { | |
65 | rarg.run=1; | |
66 | pthread_create(&reader_thread, NULL, &usb_receiver, &rarg); | |
67 | } | |
68 | ||
8556b852 M |
69 | read_history(".history"); |
70 | while(1) { | |
71 | cmd = readline(PROXPROMPT); | |
72 | if (cmd) { | |
73 | while(cmd[strlen(cmd) - 1] == ' ') | |
74 | cmd[strlen(cmd) - 1] = 0x00; | |
75 | ||
76 | if (cmd[0] != 0x00) { | |
77 | if (strncmp(cmd, "quit", 4) == 0) { | |
78 | write_history(".history"); | |
79 | break; | |
80 | } | |
81 | ||
82 | CommandReceived(cmd); | |
83 | add_history(cmd); | |
84 | } | |
85 | free(cmd); | |
86 | } else { | |
87 | printf("\n"); | |
88 | break; | |
89 | } | |
90 | } | |
7fe9b0b7 | 91 | |
92 | if (arg->usb_present == 1) { | |
93 | rarg.run = 0; | |
94 | pthread_join(reader_thread, NULL); | |
95 | } | |
96 | ||
97 | ExitGraphics(); | |
98 | pthread_exit(NULL); | |
4cd41f34 | 99 | return NULL; |
6658905f | 100 | } |
101 | ||
102 | int main(int argc, char **argv) | |
103 | { | |
7fe9b0b7 | 104 | struct main_loop_arg marg; |
105 | pthread_t main_loop_t; | |
106 | usb_init(); | |
107 | ||
108 | if (!OpenProxmark(1)) { | |
109 | fprintf(stderr,"PROXMARK3: NOT FOUND!\n"); | |
110 | marg.usb_present = 0; | |
111 | offline = 1; | |
112 | } else { | |
113 | marg.usb_present = 1; | |
114 | offline = 0; | |
115 | } | |
116 | ||
117 | pthread_create(&main_loop_t, NULL, &main_loop, &marg); | |
118 | InitGraphics(argc, argv); | |
119 | ||
120 | MainGraphics(); | |
121 | ||
122 | pthread_join(main_loop_t, NULL); | |
123 | ||
124 | if (marg.usb_present == 1) { | |
125 | CloseProxmark(); | |
126 | } | |
127 | return 0; | |
6658905f | 128 | } |