listdevs.c 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  1. /*
  2. * libusb example program to list devices on the bus
  3. * Copyright (C) 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 <sys/types.h>
  21. #include <libusb.h>
  22. static void print_devs(libusb_device **devs)
  23. {
  24. libusb_device *dev;
  25. int i = 0;
  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)\n",
  34. desc.idVendor, desc.idProduct,
  35. libusb_get_bus_number(dev), libusb_get_device_address(dev));
  36. }
  37. }
  38. int main(void)
  39. {
  40. libusb_device **devs;
  41. int r;
  42. ssize_t cnt;
  43. r = libusb_init(NULL);
  44. if (r < 0)
  45. return r;
  46. cnt = libusb_get_device_list(NULL, &devs);
  47. if (cnt < 0)
  48. return (int) cnt;
  49. print_devs(devs);
  50. libusb_free_device_list(devs, 1);
  51. libusb_exit(NULL);
  52. return 0;
  53. }