lsusb.c revision fd1e8553232aa6f3bfbb609158b24fa2e1c3d40b
1/*
2 * Copyright (C) 2010 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 *      http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#include <errno.h>
18#include <stdio.h>
19#include <stdint.h>
20#include <string.h>
21
22#include <usbhost/usbhost.h>
23
24static int lsusb_device_added(const char *dev_name, void *client_data)
25{
26    struct usb_device *dev = usb_device_open(dev_name);
27    uint16_t vid, pid;
28    char *mfg_name, *product_name, *serial;
29
30    if (!dev) {
31        fprintf(stderr, "can't open device %s: %s\n", dev_name, strerror(errno));
32        return 0;
33    }
34
35    vid = usb_device_get_vendor_id(dev);
36    pid = usb_device_get_product_id(dev);
37    mfg_name = usb_device_get_manufacturer_name(dev);
38    product_name = usb_device_get_product_name(dev);
39    serial = usb_device_get_serial(dev);
40
41    printf("%s: %04x:%04x %s %s %s\n", dev_name, vid, pid,
42           mfg_name, product_name, serial);
43
44    free(mfg_name);
45    free(product_name);
46    free(serial);
47
48    usb_device_close(dev);
49
50    return 0;
51}
52
53static int lsusb_device_removed(const char *dev_name, void *client_data)
54{
55    return 0;
56}
57
58
59static int lsusb_discovery_done(void *client_data)
60{
61    return 1;
62}
63
64
65
66int lsusb_main(int argc, char **argv)
67{
68    struct usb_host_context *ctx = usb_host_init();
69    if (!ctx) {
70        perror("usb_host_init:");
71        return 1;
72    }
73
74    usb_host_run(ctx,
75                 lsusb_device_added,
76                 lsusb_device_removed,
77                 lsusb_discovery_done,
78                 NULL);
79
80    usb_host_cleanup(ctx);
81
82    return 0;
83}
84
85