Daniel Drake | 852bba4 | 2007-11-28 13:48:45 +0000 | [diff] [blame] | 1 | /* |
hjelmn@cs.unm.edu | 1eff220 | 2014-01-08 23:50:34 +0000 | [diff] [blame] | 2 | * libusb example program to list devices on the bus |
Pete Batard | a544e59 | 2012-05-23 18:25:01 +0100 | [diff] [blame] | 3 | * Copyright © 2007 Daniel Drake <dsd@gentoo.org> |
Daniel Drake | 852bba4 | 2007-11-28 13:48:45 +0000 | [diff] [blame] | 4 | * |
| 5 | * This library is free software; you can redistribute it and/or |
| 6 | * modify it under the terms of the GNU Lesser General Public |
| 7 | * License as published by the Free Software Foundation; either |
| 8 | * version 2.1 of the License, or (at your option) any later version. |
| 9 | * |
| 10 | * This library is distributed in the hope that it will be useful, |
| 11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 13 | * Lesser General Public License for more details. |
| 14 | * |
| 15 | * You should have received a copy of the GNU Lesser General Public |
| 16 | * License along with this library; if not, write to the Free Software |
| 17 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA |
| 18 | */ |
| 19 | |
| 20 | #include <stdio.h> |
| 21 | |
Ludovic Rousseau | 38e6eb8 | 2012-10-12 23:28:51 +0200 | [diff] [blame] | 22 | #include "libusb.h" |
Daniel Drake | 852bba4 | 2007-11-28 13:48:45 +0000 | [diff] [blame] | 23 | |
Ludovic Rousseau | 8392ff2 | 2009-10-25 10:06:41 +0100 | [diff] [blame] | 24 | static void print_devs(libusb_device **devs) |
Daniel Drake | 852bba4 | 2007-11-28 13:48:45 +0000 | [diff] [blame] | 25 | { |
Daniel Drake | 9cfdb49 | 2008-03-06 23:25:20 +0000 | [diff] [blame] | 26 | libusb_device *dev; |
Hans de Goede | ebac6ac | 2013-05-16 22:36:26 +0200 | [diff] [blame] | 27 | int i = 0, j = 0; |
| 28 | uint8_t path[8]; |
Daniel Drake | 852bba4 | 2007-11-28 13:48:45 +0000 | [diff] [blame] | 29 | |
Daniel Drake | 9cfdb49 | 2008-03-06 23:25:20 +0000 | [diff] [blame] | 30 | while ((dev = devs[i++]) != NULL) { |
Daniel Drake | fe4adcc | 2008-05-09 14:34:31 +0100 | [diff] [blame] | 31 | struct libusb_device_descriptor desc; |
| 32 | int r = libusb_get_device_descriptor(dev, &desc); |
| 33 | if (r < 0) { |
| 34 | fprintf(stderr, "failed to get device descriptor"); |
| 35 | return; |
| 36 | } |
| 37 | |
Hans de Goede | ebac6ac | 2013-05-16 22:36:26 +0200 | [diff] [blame] | 38 | printf("%04x:%04x (bus %d, device %d)", |
Daniel Drake | fe4adcc | 2008-05-09 14:34:31 +0100 | [diff] [blame] | 39 | desc.idVendor, desc.idProduct, |
Daniel Drake | 3675e97 | 2008-05-02 12:28:31 +0100 | [diff] [blame] | 40 | libusb_get_bus_number(dev), libusb_get_device_address(dev)); |
Hans de Goede | ebac6ac | 2013-05-16 22:36:26 +0200 | [diff] [blame] | 41 | |
Hans de Goede | f3fcf84 | 2013-05-17 10:42:47 +0200 | [diff] [blame] | 42 | r = libusb_get_port_numbers(dev, path, sizeof(path)); |
| 43 | if (r > 0) { |
| 44 | printf(" path: %d", path[0]); |
| 45 | for (j = 1; j < r; j++) |
| 46 | printf(".%d", path[j]); |
Hans de Goede | ebac6ac | 2013-05-16 22:36:26 +0200 | [diff] [blame] | 47 | } |
| 48 | printf("\n"); |
Daniel Drake | 852bba4 | 2007-11-28 13:48:45 +0000 | [diff] [blame] | 49 | } |
| 50 | } |
| 51 | |
| 52 | int main(void) |
| 53 | { |
Daniel Drake | 9cfdb49 | 2008-03-06 23:25:20 +0000 | [diff] [blame] | 54 | libusb_device **devs; |
Daniel Drake | 637a8d7 | 2008-01-30 13:19:10 +0000 | [diff] [blame] | 55 | int r; |
Daniel Drake | 5741bfe | 2008-05-09 14:36:14 +0100 | [diff] [blame] | 56 | ssize_t cnt; |
Daniel Drake | 637a8d7 | 2008-01-30 13:19:10 +0000 | [diff] [blame] | 57 | |
Daniel Drake | 1df713d | 2008-06-24 23:01:51 -0500 | [diff] [blame] | 58 | r = libusb_init(NULL); |
Daniel Drake | 637a8d7 | 2008-01-30 13:19:10 +0000 | [diff] [blame] | 59 | if (r < 0) |
| 60 | return r; |
| 61 | |
Daniel Drake | 1df713d | 2008-06-24 23:01:51 -0500 | [diff] [blame] | 62 | cnt = libusb_get_device_list(NULL, &devs); |
gudenau | 18a5f99 | 2018-06-19 20:12:52 -0500 | [diff] [blame] | 63 | if (cnt < 0){ |
| 64 | libusb_exit(NULL); |
Daniel Drake | 5878daa | 2008-05-04 00:51:59 +0100 | [diff] [blame] | 65 | return (int) cnt; |
gudenau | 18a5f99 | 2018-06-19 20:12:52 -0500 | [diff] [blame] | 66 | } |
Daniel Drake | 852bba4 | 2007-11-28 13:48:45 +0000 | [diff] [blame] | 67 | |
| 68 | print_devs(devs); |
Daniel Drake | 9cfdb49 | 2008-03-06 23:25:20 +0000 | [diff] [blame] | 69 | libusb_free_device_list(devs, 1); |
Daniel Drake | 852bba4 | 2007-11-28 13:48:45 +0000 | [diff] [blame] | 70 | |
Daniel Drake | 1df713d | 2008-06-24 23:01:51 -0500 | [diff] [blame] | 71 | libusb_exit(NULL); |
Daniel Drake | 852bba4 | 2007-11-28 13:48:45 +0000 | [diff] [blame] | 72 | return 0; |
| 73 | } |