listdevs.c 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. /*
  2. * libusb example program to list devices on the bus
  3. * Copyright © 2007 Daniel Drake <dsd@gentoo.org>
  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. #include <stdio.h>
  20. #include "libusb.h"
  21. static void print_devs(libusb_device **devs)
  22. {
  23. libusb_device *dev;
  24. int i = 0, j = 0;
  25. uint8_t path[8];
  26. while ((dev = devs[i++]) != NULL) {
  27. struct libusb_device_descriptor desc;
  28. int r = libusb_get_device_descriptor(dev, &desc);
  29. if (r < 0) {
  30. fprintf(stderr, "failed to get device descriptor");
  31. return;
  32. }
  33. printf("%04x:%04x (bus %d, device %d)",
  34. desc.idVendor, desc.idProduct,
  35. libusb_get_bus_number(dev), libusb_get_device_address(dev));
  36. r = libusb_get_port_numbers(dev, path, sizeof(path));
  37. if (r > 0) {
  38. printf(" path: %d", path[0]);
  39. for (j = 1; j < r; j++)
  40. printf(".%d", path[j]);
  41. }
  42. printf("\n");
  43. }
  44. }
  45. int main(void)
  46. {
  47. libusb_device **devs;
  48. int r;
  49. ssize_t cnt;
  50. r = libusb_init(NULL);
  51. if (r < 0)
  52. return r;
  53. cnt = libusb_get_device_list(NULL, &devs);
  54. if (cnt < 0){
  55. libusb_exit(NULL);
  56. return (int) cnt;
  57. }
  58. print_devs(devs);
  59. libusb_free_device_list(devs, 1);
  60. libusb_exit(NULL);
  61. return 0;
  62. }