diff options
Diffstat (limited to 'utils/hwstub/tools/hwstub_test.cpp')
-rw-r--r-- | utils/hwstub/tools/hwstub_test.cpp | 219 |
1 files changed, 219 insertions, 0 deletions
diff --git a/utils/hwstub/tools/hwstub_test.cpp b/utils/hwstub/tools/hwstub_test.cpp new file mode 100644 index 0000000000..c93e601c36 --- /dev/null +++ b/utils/hwstub/tools/hwstub_test.cpp | |||
@@ -0,0 +1,219 @@ | |||
1 | /*************************************************************************** | ||
2 | * __________ __ ___. | ||
3 | * Open \______ \ ____ ____ | | _\_ |__ _______ ___ | ||
4 | * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ / | ||
5 | * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < < | ||
6 | * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \ | ||
7 | * \/ \/ \/ \/ \/ | ||
8 | * $Id$ | ||
9 | * | ||
10 | * Copyright (C) 2015 by Amaury Pouly | ||
11 | * | ||
12 | * This program is free software; you can redistribute it and/or | ||
13 | * modify it under the terms of the GNU General Public License | ||
14 | * as published by the Free Software Foundation; either version 2 | ||
15 | * of the License, or (at your option) any later version. | ||
16 | * | ||
17 | * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY | ||
18 | * KIND, either express or implied. | ||
19 | * | ||
20 | ****************************************************************************/ | ||
21 | #include <cstdio> | ||
22 | #include <thread> | ||
23 | #include <chrono> | ||
24 | #include <cstring> | ||
25 | #include <iostream> | ||
26 | #include "hwstub.hpp" | ||
27 | #include "hwstub_usb.hpp" | ||
28 | #include "hwstub_uri.hpp" | ||
29 | #include <signal.h> | ||
30 | #include <getopt.h> | ||
31 | |||
32 | /* capture CTRL+C */ | ||
33 | volatile sig_atomic_t g_exit_loop = 0; | ||
34 | |||
35 | void do_signal(int sig) | ||
36 | { | ||
37 | g_exit_loop = 1; | ||
38 | } | ||
39 | |||
40 | std::shared_ptr<hwstub::context> g_ctx; | ||
41 | |||
42 | void print_error(hwstub::error err, bool nl = true) | ||
43 | { | ||
44 | switch(err) | ||
45 | { | ||
46 | case hwstub::error::SUCCESS: printf("success"); break; | ||
47 | case hwstub::error::ERROR: printf("error"); break; | ||
48 | default: printf("unknown(%d)", (int)err); break; | ||
49 | } | ||
50 | if(nl) | ||
51 | printf("\n"); | ||
52 | } | ||
53 | |||
54 | const char *target_string(uint32_t id) | ||
55 | { | ||
56 | switch(id) | ||
57 | { | ||
58 | case HWSTUB_TARGET_UNK: return "unknown"; | ||
59 | case HWSTUB_TARGET_STMP: return "stmp"; | ||
60 | case HWSTUB_TARGET_RK27: return "rk27"; | ||
61 | case HWSTUB_TARGET_PP: return "pp"; | ||
62 | case HWSTUB_TARGET_ATJ: return "atj"; | ||
63 | case HWSTUB_TARGET_JZ: return "jz"; | ||
64 | default: return "unknown"; | ||
65 | } | ||
66 | } | ||
67 | |||
68 | void print_dev_details(std::shared_ptr<hwstub::device> dev) | ||
69 | { | ||
70 | std::shared_ptr<hwstub::handle> h; | ||
71 | hwstub::error err = dev->open(h); | ||
72 | if(err != hwstub::error::SUCCESS) | ||
73 | { | ||
74 | printf(" [cannot open dev: %s]\n", error_string(err).c_str()); | ||
75 | return; | ||
76 | } | ||
77 | /* version */ | ||
78 | struct hwstub_version_desc_t ver_desc; | ||
79 | err = h->get_version_desc(ver_desc); | ||
80 | if(err != hwstub::error::SUCCESS) | ||
81 | { | ||
82 | printf(" [cannot get version descriptor: %s]\n", error_string(err).c_str()); | ||
83 | return; | ||
84 | } | ||
85 | printf(" [version %d.%d.%d]\n", ver_desc.bMajor, ver_desc.bMinor, ver_desc.bRevision); | ||
86 | /* target */ | ||
87 | struct hwstub_target_desc_t target_desc; | ||
88 | err = h->get_target_desc(target_desc); | ||
89 | if(err != hwstub::error::SUCCESS) | ||
90 | { | ||
91 | printf(" [cannot get target descriptor: %s]\n", error_string(err).c_str()); | ||
92 | return; | ||
93 | } | ||
94 | std::string name(target_desc.bName, sizeof(target_desc.bName)); | ||
95 | printf(" [target %s: %s]\n", target_string(target_desc.dID), name.c_str()); | ||
96 | /* layout */ | ||
97 | struct hwstub_layout_desc_t layout_desc; | ||
98 | err = h->get_layout_desc(layout_desc); | ||
99 | if(err != hwstub::error::SUCCESS) | ||
100 | { | ||
101 | printf(" [cannot get layout descriptor: %s]\n", error_string(err).c_str()); | ||
102 | return; | ||
103 | } | ||
104 | printf(" [code layout %#x bytes @ %#x]\n", layout_desc.dCodeSize, layout_desc.dCodeStart); | ||
105 | printf(" [stack layout %#x bytes @ %#x]\n", layout_desc.dStackSize, layout_desc.dStackStart); | ||
106 | printf(" [buffer layout %#x bytes @ %#x]\n", layout_desc.dBufferSize, layout_desc.dBufferStart); | ||
107 | } | ||
108 | |||
109 | void print_device(std::shared_ptr<hwstub::device> dev, bool arrived = true) | ||
110 | { | ||
111 | hwstub::usb::device *udev = dynamic_cast< hwstub::usb::device* >(dev.get()); | ||
112 | if(arrived) | ||
113 | printf("--> "); | ||
114 | else | ||
115 | printf("<-- "); | ||
116 | if(udev) | ||
117 | { | ||
118 | libusb_device *uudev = udev->native_device(); | ||
119 | struct libusb_device_descriptor dev_desc; | ||
120 | libusb_get_device_descriptor(uudev, &dev_desc); | ||
121 | printf("USB device @ %d.%u: ID %04x:%04x\n", udev->get_bus_number(), | ||
122 | udev->get_address(), dev_desc.idVendor, dev_desc.idProduct); | ||
123 | } | ||
124 | else | ||
125 | printf("Unknown device\n"); | ||
126 | if(arrived) | ||
127 | print_dev_details(dev); | ||
128 | } | ||
129 | |||
130 | void dev_changed(std::shared_ptr<hwstub::context> ctx, bool arrived, std::shared_ptr<hwstub::device> dev) | ||
131 | { | ||
132 | print_device(dev, arrived); | ||
133 | } | ||
134 | |||
135 | void print_list() | ||
136 | { | ||
137 | std::vector<std::shared_ptr<hwstub::device>> list; | ||
138 | hwstub::error ret = g_ctx->get_device_list(list); | ||
139 | if(ret != hwstub::error::SUCCESS) | ||
140 | { | ||
141 | printf("Cannot get device list: %s\n", error_string(ret).c_str()); | ||
142 | return; | ||
143 | } | ||
144 | for(auto d : list) | ||
145 | print_device(d); | ||
146 | } | ||
147 | |||
148 | int usage() | ||
149 | { | ||
150 | printf("usage: hwstub_test [options]\n"); | ||
151 | printf(" --help/-h Display this help\n"); | ||
152 | printf(" --verbose/-v Verbose output\n"); | ||
153 | printf(" --context/-c <uri> Context URI (see below)\n"); | ||
154 | printf("\n"); | ||
155 | hwstub::uri::print_usage(stdout, true, false); | ||
156 | return 1; | ||
157 | } | ||
158 | |||
159 | int main(int argc, char **argv) | ||
160 | { | ||
161 | hwstub::uri::uri uri = hwstub::uri::default_uri(); | ||
162 | bool verbose = false; | ||
163 | |||
164 | while(1) | ||
165 | { | ||
166 | static struct option long_options[] = | ||
167 | { | ||
168 | {"help", no_argument, 0, 'h'}, | ||
169 | {"verbose", no_argument, 0, 'v'}, | ||
170 | {"context", required_argument, 0, 'c'}, | ||
171 | {0, 0, 0, 0} | ||
172 | }; | ||
173 | |||
174 | int c = getopt_long(argc, argv, "hvc:", long_options, NULL); | ||
175 | if(c == -1) | ||
176 | break; | ||
177 | switch(c) | ||
178 | { | ||
179 | case -1: | ||
180 | break; | ||
181 | case 'v': | ||
182 | verbose = true; | ||
183 | break; | ||
184 | case 'h': | ||
185 | return usage(); | ||
186 | case 'c': | ||
187 | uri = hwstub::uri::uri(optarg); | ||
188 | break; | ||
189 | default: | ||
190 | abort(); | ||
191 | } | ||
192 | } | ||
193 | |||
194 | if(optind != argc) | ||
195 | return usage(); | ||
196 | |||
197 | /* intercept CTRL+C */ | ||
198 | signal(SIGINT, do_signal); | ||
199 | |||
200 | std::string error; | ||
201 | g_ctx = hwstub::uri::create_context(uri, &error); | ||
202 | if(!g_ctx) | ||
203 | { | ||
204 | printf("Cannot create context: %s\n", error.c_str()); | ||
205 | return 1; | ||
206 | } | ||
207 | if(verbose) | ||
208 | g_ctx->set_debug(std::cout); | ||
209 | print_list(); | ||
210 | g_ctx->register_callback(dev_changed); | ||
211 | g_ctx->start_polling(); | ||
212 | while(!g_exit_loop) | ||
213 | std::this_thread::sleep_for(std::chrono::seconds(1)); | ||
214 | printf("Shutting down...\n"); | ||
215 | g_ctx.reset(); /* will cleanup */ | ||
216 | |||
217 | return 0; | ||
218 | } | ||
219 | |||