| OLD | NEW |
| (Empty) | |
| 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 |
| 21 #include <stdlib.h> |
| 22 #include <stdio.h> |
| 23 |
| 24 #include "libusb.h" |
| 25 |
| 26 int done = 0; |
| 27 libusb_device_handle *handle; |
| 28 |
| 29 static int LIBUSB_CALL hotplug_callback(libusb_context *ctx, libusb_device *dev,
libusb_hotplug_event event, void *user_data) |
| 30 { |
| 31 struct libusb_device_descriptor desc; |
| 32 int rc; |
| 33 |
| 34 rc = libusb_get_device_descriptor(dev, &desc); |
| 35 if (LIBUSB_SUCCESS != rc) { |
| 36 fprintf (stderr, "Error getting device descriptor\n"); |
| 37 } |
| 38 |
| 39 printf ("Device attached: %04x:%04x\n", desc.idVendor, desc.idProduct); |
| 40 |
| 41 libusb_open (dev, &handle); |
| 42 |
| 43 done++; |
| 44 |
| 45 return 0; |
| 46 } |
| 47 |
| 48 static int LIBUSB_CALL hotplug_callback_detach(libusb_context *ctx, libusb_devic
e *dev, libusb_hotplug_event event, void *user_data) |
| 49 { |
| 50 printf ("Device detached\n"); |
| 51 |
| 52 libusb_close (handle); |
| 53 |
| 54 done++; |
| 55 return 0; |
| 56 } |
| 57 |
| 58 int main(int argc, char *argv[]) |
| 59 { |
| 60 libusb_hotplug_callback_handle hp[2]; |
| 61 int product_id, vendor_id, class_id; |
| 62 int rc; |
| 63 |
| 64 vendor_id = (argc > 1) ? strtol (argv[1], NULL, 0) : 0x045a; |
| 65 product_id = (argc > 2) ? strtol (argv[2], NULL, 0) : 0x5005; |
| 66 class_id = (argc > 3) ? strtol (argv[3], NULL, 0) : LIBUSB_HOTPLUG_MAT
CH_ANY; |
| 67 |
| 68 libusb_init (NULL); |
| 69 |
| 70 if (!libusb_has_capability (LIBUSB_CAP_HAS_HOTPLUG)) { |
| 71 printf ("Hotplug capabilites are not supported on this platform\
n"); |
| 72 libusb_exit (NULL); |
| 73 return EXIT_FAILURE; |
| 74 } |
| 75 |
| 76 rc = libusb_hotplug_register_callback (NULL, LIBUSB_HOTPLUG_EVENT_DEVICE
_ARRIVED, 0, vendor_id, |
| 77 product_id, class_id, hotplug_callback, NULL, &hp[0]); |
| 78 if (LIBUSB_SUCCESS != rc) { |
| 79 fprintf (stderr, "Error registering callback 0\n"); |
| 80 libusb_exit (NULL); |
| 81 return EXIT_FAILURE; |
| 82 } |
| 83 |
| 84 rc = libusb_hotplug_register_callback (NULL, LIBUSB_HOTPLUG_EVENT_DEVICE
_LEFT, 0, vendor_id, |
| 85 product_id,class_id, hotplug_callback_detach, NULL, &hp[1]); |
| 86 if (LIBUSB_SUCCESS != rc) { |
| 87 fprintf (stderr, "Error registering callback 1\n"); |
| 88 libusb_exit (NULL); |
| 89 return EXIT_FAILURE; |
| 90 } |
| 91 |
| 92 while (done < 2) { |
| 93 libusb_handle_events (NULL); |
| 94 } |
| 95 |
| 96 libusb_exit (NULL); |
| 97 } |
| OLD | NEW |