]>
Commit | Line | Data |
---|---|---|
1 | //----------------------------------------------------------------------------- | |
2 | // Copyright (C) 2009 Michael Gernoth <michael at gernoth.net> | |
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 | ||
12 | #include <stdio.h> | |
13 | #include <stdlib.h> | |
14 | #include <string.h> | |
15 | #include <pthread.h> | |
16 | #include <unistd.h> | |
17 | #include <readline/readline.h> | |
18 | #include <readline/history.h> | |
19 | #include "proxusb.h" | |
20 | #include "proxmark3.h" | |
21 | #include "proxgui.h" | |
22 | #include "cmdmain.h" | |
23 | ||
24 | struct usb_receiver_arg | |
25 | { | |
26 | int run; | |
27 | }; | |
28 | ||
29 | struct main_loop_arg | |
30 | { | |
31 | int usb_present; | |
32 | }; | |
33 | ||
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 | UsbCommandReceived(&cmdbuf); | |
42 | fflush(NULL); | |
43 | } | |
44 | } | |
45 | ||
46 | pthread_exit(NULL); | |
47 | return NULL; | |
48 | } | |
49 | ||
50 | static void *main_loop(void *targ) | |
51 | { | |
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 | ||
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) { | |
71 | break; | |
72 | } | |
73 | ||
74 | CommandReceived(cmd); | |
75 | add_history(cmd); | |
76 | } | |
77 | free(cmd); | |
78 | } else { | |
79 | printf("\n"); | |
80 | break; | |
81 | } | |
82 | } | |
83 | ||
84 | write_history(".history"); | |
85 | ||
86 | if (arg->usb_present == 1) { | |
87 | rarg.run = 0; | |
88 | pthread_join(reader_thread, NULL); | |
89 | } | |
90 | ||
91 | ExitGraphics(); | |
92 | pthread_exit(NULL); | |
93 | return NULL; | |
94 | } | |
95 | ||
96 | int main(int argc, char **argv) | |
97 | { | |
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; | |
122 | } |