source: mainline/uspace/app/usbinfo/main.c@ db71e2a

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since db71e2a was 5debe97, checked in by Jan Vesely <jano.vesely@…>, 13 years ago

libusb: DCE

  • Property mode set to 100644
File size: 5.8 KB
Line 
1/*
2 * Copyright (c) 2010-2011 Vojtech Horky
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 *
9 * - Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * - Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
14 * - The name of the author may not be used to endorse or promote products
15 * derived from this software without specific prior written permission.
16 *
17 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
18 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
19 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
20 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
21 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
22 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
26 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27 */
28
29/** @addtogroup usbinfo
30 * @{
31 */
32/**
33 * @file
34 * USB querying.
35 */
36
37#include <stdio.h>
38#include <stdlib.h>
39#include <errno.h>
40#include <str_error.h>
41#include <stdbool.h>
42#include <getopt.h>
43#include <devman.h>
44#include <loc.h>
45#include <usb/hc.h>
46#include <usb/dev.h>
47#include <usb/dev/pipes.h>
48#include "usbinfo.h"
49
50static void print_usage(char *app_name)
51{
52#define _INDENT " "
53#define _OPTION(opt, description) \
54 printf(_INDENT opt "\n" _INDENT _INDENT description "\n")
55
56 printf(NAME ": query USB devices for descriptors\n\n");
57 printf("Usage: %s [options] device [device [device [ ... ]]]\n",
58 app_name);
59 printf(_INDENT "The device can be specified in two ways.\n");
60 printf(_INDENT " o Using its devman path, e.g. /hw/pci0/.../usb00_a1.\n");
61 printf(_INDENT " o Or using BUS.ADDR numbers as printed by lsusb.\n");
62
63 _OPTION("-h --help", "Print this help and exit.");
64 _OPTION("-l --list", "Print a list of host controllers and devices.");
65 _OPTION("-i --identification", "Brief device identification.");
66 _OPTION("-m --match-ids", "Print match ids generated for the device.");
67 _OPTION("-t --descriptor-tree", "Print descriptor tree.");
68 _OPTION("-T --descriptor-tree-full", "Print detailed descriptor tree");
69 _OPTION("-s --strings", "Try to print all string descriptors.");
70 _OPTION("-S --status", "Get status of the device.");
71 _OPTION("-r --hid-report", "Dump HID report descriptor.");
72 _OPTION("-r --hid-report-usages", "Dump usages of HID report.");
73
74 printf("\n");
75 printf("If no option is specified, `-i' is considered default.\n");
76 printf("\n");
77
78#undef _OPTION
79#undef _INDENT
80}
81
82static struct option long_options[] = {
83 {"help", no_argument, NULL, 'h'},
84 {"identification", no_argument, NULL, 'i'},
85 {"list", no_argument, NULL, 'l'},
86 {"match-ids", no_argument, NULL, 'm'},
87 {"descriptor-tree", no_argument, NULL, 't'},
88 {"descriptor-tree-full", no_argument, NULL, 'T'},
89 {"strings", no_argument, NULL, 's'},
90 {"status", no_argument, NULL, 'S'},
91 {"hid-report", no_argument, NULL, 'r'},
92 {"hid-report-usages", no_argument, NULL, 'R'},
93 {0, 0, NULL, 0}
94};
95static const char *short_options = "hilmtTsSrR";
96
97static usbinfo_action_t actions[] = {
98 {
99 .opt = 'i',
100 .action = dump_short_device_identification,
101 .active = false
102 },
103 {
104 .opt = 'm',
105 .action = dump_device_match_ids,
106 .active = false
107 },
108 {
109 .opt = 't',
110 .action = dump_descriptor_tree_brief,
111 .active = false
112 },
113 {
114 .opt = 'T',
115 .action = dump_descriptor_tree_full,
116 .active = false
117 },
118 {
119 .opt = 's',
120 .action = dump_strings,
121 .active = false
122 },
123 {
124 .opt = 'S',
125 .action = dump_status,
126 .active = false
127 },
128 {
129 .opt = 'r',
130 .action = dump_hidreport_raw,
131 .active = false
132 },
133 {
134 .opt = 'R',
135 .action = dump_hidreport_usages,
136 .active = false
137 },
138 {
139 .opt = 0
140 }
141};
142
143int main(int argc, char *argv[])
144{
145 if (argc <= 1) {
146 print_usage(argv[0]);
147 return -1;
148 }
149
150 bool something_active = false;
151 /*
152 * Process command-line options. They determine what shall be
153 * done with the device.
154 */
155 int opt;
156 do {
157 opt = getopt_long(argc, argv,
158 short_options, long_options, NULL);
159 switch (opt) {
160 case -1:
161 break;
162 case 'l':
163 list();
164 break;
165 case '?':
166 print_usage(argv[0]);
167 return 1;
168 case 'h':
169 print_usage(argv[0]);
170 return 0;
171 default: {
172 int idx = 0;
173 while (actions[idx].opt != 0) {
174 if (actions[idx].opt == opt) {
175 actions[idx].active = true;
176 something_active = true;
177 break;
178 }
179 idx++;
180 }
181 break;
182 }
183 }
184 } while (opt > 0);
185
186 /* Set the default action. */
187 if (!something_active) {
188 actions[0].active = true;
189 }
190
191 /*
192 * Go through all devices given on the command line and run the
193 * specified actions.
194 */
195 int i;
196 for (i = optind; i < argc; i++) {
197 char *devpath = argv[i];
198
199 /* The initialization is here only to make compiler happy. */
200 devman_handle_t handle = 0;
201 int rc = usb_resolve_device_handle(devpath, &handle);
202 if (rc != EOK) {
203 fprintf(stderr, NAME ": device `%s' not found "
204 "or not of USB kind, skipping.\n",
205 devpath);
206 continue;
207 }
208
209 usb_device_t *usb_dev = usb_device_create(handle);
210
211 if (usb_dev == NULL) {
212 continue;
213 }
214
215 /* Run actions the user specified. */
216 printf("%s\n", devpath);
217
218 int action = 0;
219 while (actions[action].opt != 0) {
220 if (actions[action].active) {
221 actions[action].action(usb_dev);
222 }
223 action++;
224 }
225
226 usb_device_destroy(usb_dev);
227 }
228
229 return 0;
230}
231
232
233/** @}
234 */
Note: See TracBrowser for help on using the repository browser.