hotplugtest.c 2.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. /* -*- Mode: C; indent-tabs-mode:t ; c-basic-offset:8 -*- */
  2. /*
  3. * libusb example program for hotplug API
  4. * Copyright © 2012-2013 Nathan Hjelm <hjelmn@mac.ccom>
  5. *
  6. * This library is free software; you can redistribute it and/or
  7. * modify it under the terms of the GNU Lesser General Public
  8. * License as published by the Free Software Foundation; either
  9. * version 2.1 of the License, or (at your option) any later version.
  10. *
  11. * This library is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  14. * Lesser General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU Lesser General Public
  17. * License along with this library; if not, write to the Free Software
  18. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  19. */
  20. #include <stdlib.h>
  21. #include <stdio.h>
  22. #include "libusb.h"
  23. int done = 0;
  24. libusb_device_handle *handle;
  25. static int LIBUSB_CALL hotplug_callback(libusb_context *ctx, libusb_device *dev, libusb_hotplug_event event, void *user_data)
  26. {
  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 attached: %04x:%04x\n", desc.idVendor, desc.idProduct);
  34. libusb_open (dev, &handle);
  35. done++;
  36. return 0;
  37. }
  38. static int LIBUSB_CALL hotplug_callback_detach(libusb_context *ctx, libusb_device *dev, libusb_hotplug_event event, void *user_data)
  39. {
  40. printf ("Device detached\n");
  41. libusb_close (handle);
  42. done++;
  43. return 0;
  44. }
  45. int main(int argc, char *argv[])
  46. {
  47. libusb_hotplug_callback_handle hp[2];
  48. int product_id, vendor_id, class_id;
  49. int rc;
  50. vendor_id = (argc > 1) ? strtol (argv[1], NULL, 0) : 0x045a;
  51. product_id = (argc > 2) ? strtol (argv[2], NULL, 0) : 0x5005;
  52. class_id = (argc > 3) ? strtol (argv[3], NULL, 0) : LIBUSB_HOTPLUG_MATCH_ANY;
  53. libusb_init (NULL);
  54. if (!libusb_has_capability (LIBUSB_CAP_HAS_HOTPLUG)) {
  55. printf ("Hotplug capabilites are not supported on this platform\n");
  56. libusb_exit (NULL);
  57. return EXIT_FAILURE;
  58. }
  59. rc = libusb_hotplug_register_callback (NULL, LIBUSB_HOTPLUG_EVENT_DEVICE_ARRIVED, 0, vendor_id,
  60. product_id, class_id, hotplug_callback, NULL, &hp[0]);
  61. if (LIBUSB_SUCCESS != rc) {
  62. fprintf (stderr, "Error registering callback 0\n");
  63. libusb_exit (NULL);
  64. return EXIT_FAILURE;
  65. }
  66. rc = libusb_hotplug_register_callback (NULL, LIBUSB_HOTPLUG_EVENT_DEVICE_LEFT, 0, vendor_id,
  67. product_id,class_id, hotplug_callback_detach, NULL, &hp[1]);
  68. if (LIBUSB_SUCCESS != rc) {
  69. fprintf (stderr, "Error registering callback 1\n");
  70. libusb_exit (NULL);
  71. return EXIT_FAILURE;
  72. }
  73. while (done < 2) {
  74. libusb_handle_events (NULL);
  75. }
  76. libusb_exit (NULL);
  77. }