hotplugtest.c 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  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.com>
  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 = NULL;
  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. (void)ctx;
  30. (void)dev;
  31. (void)event;
  32. (void)user_data;
  33. rc = libusb_get_device_descriptor(dev, &desc);
  34. if (LIBUSB_SUCCESS != rc) {
  35. fprintf (stderr, "Error getting device descriptor\n");
  36. }
  37. printf ("Device attached: %04x:%04x\n", desc.idVendor, desc.idProduct);
  38. if (handle) {
  39. libusb_close (handle);
  40. handle = NULL;
  41. }
  42. rc = libusb_open (dev, &handle);
  43. if (LIBUSB_SUCCESS != rc) {
  44. fprintf (stderr, "Error opening device\n");
  45. }
  46. done++;
  47. return 0;
  48. }
  49. static int LIBUSB_CALL hotplug_callback_detach(libusb_context *ctx, libusb_device *dev, libusb_hotplug_event event, void *user_data)
  50. {
  51. (void)ctx;
  52. (void)dev;
  53. (void)event;
  54. (void)user_data;
  55. printf ("Device detached\n");
  56. if (handle) {
  57. libusb_close (handle);
  58. handle = NULL;
  59. }
  60. done++;
  61. return 0;
  62. }
  63. int main(int argc, char *argv[])
  64. {
  65. libusb_hotplug_callback_handle hp[2];
  66. int product_id, vendor_id, class_id;
  67. int rc;
  68. vendor_id = (argc > 1) ? (int)strtol (argv[1], NULL, 0) : 0x045a;
  69. product_id = (argc > 2) ? (int)strtol (argv[2], NULL, 0) : 0x5005;
  70. class_id = (argc > 3) ? (int)strtol (argv[3], NULL, 0) : LIBUSB_HOTPLUG_MATCH_ANY;
  71. rc = libusb_init (NULL);
  72. if (rc < 0)
  73. {
  74. printf("failed to initialise libusb: %s\n", libusb_error_name(rc));
  75. return EXIT_FAILURE;
  76. }
  77. if (!libusb_has_capability (LIBUSB_CAP_HAS_HOTPLUG)) {
  78. printf ("Hotplug capabilities are not supported on this platform\n");
  79. libusb_exit (NULL);
  80. return EXIT_FAILURE;
  81. }
  82. rc = libusb_hotplug_register_callback (NULL, LIBUSB_HOTPLUG_EVENT_DEVICE_ARRIVED, 0, vendor_id,
  83. product_id, class_id, hotplug_callback, NULL, &hp[0]);
  84. if (LIBUSB_SUCCESS != rc) {
  85. fprintf (stderr, "Error registering callback 0\n");
  86. libusb_exit (NULL);
  87. return EXIT_FAILURE;
  88. }
  89. rc = libusb_hotplug_register_callback (NULL, LIBUSB_HOTPLUG_EVENT_DEVICE_LEFT, 0, vendor_id,
  90. product_id,class_id, hotplug_callback_detach, NULL, &hp[1]);
  91. if (LIBUSB_SUCCESS != rc) {
  92. fprintf (stderr, "Error registering callback 1\n");
  93. libusb_exit (NULL);
  94. return EXIT_FAILURE;
  95. }
  96. while (done < 2) {
  97. rc = libusb_handle_events (NULL);
  98. if (rc < 0)
  99. printf("libusb_handle_events() failed: %s\n", libusb_error_name(rc));
  100. }
  101. if (handle) {
  102. libusb_close (handle);
  103. }
  104. libusb_exit (NULL);
  105. return EXIT_SUCCESS;
  106. }