hotplugtest.c 2.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. /*
  2. * libusb example program for hotplug API
  3. * Copyright (C) 2012-2013 Nathan Hjelm <hjelmn@mac.ccom>
  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 <stdlib.h>
  20. #include <stdio.h>
  21. #include <sched.h>
  22. #include <unistd.h>
  23. #include <libusb.h>
  24. int done = 0;
  25. libusb_device_handle *handle;
  26. static int hotplug_callback (libusb_context *ctx, libusb_device *dev, libusb_hotplug_event event, void *user_data) {
  27. struct libusb_device_descriptor desc;
  28. int rc;
  29. rc = libusb_get_device_descriptor(dev, &desc);
  30. if (LIBUSB_SUCCESS != rc) {
  31. fprintf (stderr, "Error getting device descriptor\n");
  32. }
  33. printf ("Device attach: %04x:%04x\n", desc.idVendor, desc.idProduct);
  34. libusb_open (dev, &handle);
  35. done++;
  36. return 0;
  37. }
  38. static int hotplug_callback_detach (libusb_context *ctx, libusb_device *dev, libusb_hotplug_event event, void *user_data) {
  39. printf ("Device detached\n");
  40. libusb_close (handle);
  41. done++;
  42. return 0;
  43. }
  44. int main (int argc, char *argv[]) {
  45. libusb_hotplug_callback_handle hp[2];
  46. int product_id, vendor_id, class_id;
  47. int rc;
  48. vendor_id = (argc > 1) ? strtol (argv[1], NULL, 0) : 0x045a;
  49. product_id = (argc > 2) ? strtol (argv[2], NULL, 0) : 0x5005;
  50. class_id = (argc > 3) ? strtol (argv[3], NULL, 0) : LIBUSB_HOTPLUG_MATCH_ANY;
  51. libusb_init (NULL);
  52. if (!libusb_has_capability (LIBUSB_CAP_HAS_HOTPLUG)) {
  53. printf ("Hotplug not supported by this build of libusb\n");
  54. libusb_exit (NULL);
  55. return EXIT_FAILURE;
  56. }
  57. rc = libusb_hotplug_register_callback (NULL, LIBUSB_HOTPLUG_EVENT_DEVICE_ARRIVED, 0, vendor_id,
  58. product_id, class_id, hotplug_callback, NULL, &hp[0]);
  59. if (LIBUSB_SUCCESS != rc) {
  60. fprintf (stderr, "Error registering callback 0\n");
  61. libusb_exit (NULL);
  62. return EXIT_FAILURE;
  63. }
  64. rc = libusb_hotplug_register_callback (NULL, LIBUSB_HOTPLUG_EVENT_DEVICE_LEFT, 0, vendor_id,
  65. product_id,class_id, hotplug_callback_detach, NULL, &hp[1]);
  66. if (LIBUSB_SUCCESS != rc) {
  67. fprintf (stderr, "Error registering callback 1\n");
  68. libusb_exit (NULL);
  69. return EXIT_FAILURE;
  70. }
  71. while (done < 2) {
  72. libusb_handle_events (NULL);
  73. }
  74. libusb_exit (NULL);
  75. }