X-Git-Url: http://47.100.26.94:8080/?a=blobdiff_plain;f=app%2Fsrc%2Fmain%2Fjni%2Flibusb%2Fexamples%2Fhotplugtest.c;fp=app%2Fsrc%2Fmain%2Fjni%2Flibusb%2Fexamples%2Fhotplugtest.c;h=368011beca8a8d03cf5032f44ce55c41629a6e0a;hb=831cc09829bc6e18d8d0d8bb78063e89ea565ce9;hp=0000000000000000000000000000000000000000;hpb=061580c83656bf358b01a6b78fd22ae9bd497728;p=rtmpclient.git diff --git a/app/src/main/jni/libusb/examples/hotplugtest.c b/app/src/main/jni/libusb/examples/hotplugtest.c new file mode 100644 index 0000000..368011b --- /dev/null +++ b/app/src/main/jni/libusb/examples/hotplugtest.c @@ -0,0 +1,104 @@ +/* -*- Mode: C; indent-tabs-mode:t ; c-basic-offset:8 -*- */ +/* + * libusb example program for hotplug API + * Copyright © 2012-2013 Nathan Hjelm + * + * This library is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 2.1 of the License, or (at your option) any later version. + * + * This library is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with this library; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA + */ + +#include +#include + +#include "libusb.h" + +int done = 0; +libusb_device_handle *handle; + +static int LIBUSB_CALL hotplug_callback(libusb_context *ctx, libusb_device *dev, libusb_hotplug_event event, void *user_data) +{ + struct libusb_device_descriptor desc; + int rc; + + rc = libusb_get_device_descriptor(dev, &desc); + if (LIBUSB_SUCCESS != rc) { + fprintf (stderr, "Error getting device descriptor\n"); + } + + printf ("Device attached: %04x:%04x\n", desc.idVendor, desc.idProduct); + + libusb_open (dev, &handle); + + done++; + + return 0; +} + +static int LIBUSB_CALL hotplug_callback_detach(libusb_context *ctx, libusb_device *dev, libusb_hotplug_event event, void *user_data) +{ + printf ("Device detached\n"); + + libusb_close (handle); + + done++; + return 0; +} + +int main(int argc, char *argv[]) +{ + libusb_hotplug_callback_handle hp[2]; + int product_id, vendor_id, class_id; + int rc; + + vendor_id = (argc > 1) ? strtol (argv[1], NULL, 0) : 0x045a; + product_id = (argc > 2) ? strtol (argv[2], NULL, 0) : 0x5005; + class_id = (argc > 3) ? strtol (argv[3], NULL, 0) : LIBUSB_HOTPLUG_MATCH_ANY; + + rc = libusb_init (NULL); + if (rc < 0) + { + printf("failed to initialise libusb: %s\n", libusb_error_name(rc)); + return EXIT_FAILURE; + } + + if (!libusb_has_capability (LIBUSB_CAP_HAS_HOTPLUG)) { + printf ("Hotplug capabilites are not supported on this platform\n"); + libusb_exit (NULL); + return EXIT_FAILURE; + } + + rc = libusb_hotplug_register_callback (NULL, LIBUSB_HOTPLUG_EVENT_DEVICE_ARRIVED, 0, vendor_id, + product_id, class_id, hotplug_callback, NULL, &hp[0]); + if (LIBUSB_SUCCESS != rc) { + fprintf (stderr, "Error registering callback 0\n"); + libusb_exit (NULL); + return EXIT_FAILURE; + } + + rc = libusb_hotplug_register_callback (NULL, LIBUSB_HOTPLUG_EVENT_DEVICE_LEFT, 0, vendor_id, + product_id,class_id, hotplug_callback_detach, NULL, &hp[1]); + if (LIBUSB_SUCCESS != rc) { + fprintf (stderr, "Error registering callback 1\n"); + libusb_exit (NULL); + return EXIT_FAILURE; + } + + while (done < 2) { + rc = libusb_handle_events (NULL); + if (rc < 0) + printf("libusb_handle_events() failed: %s\n", libusb_error_name(rc)); + } + + libusb_exit (NULL); +}