summaryrefslogtreecommitdiff
path: root/examples
diff options
context:
space:
mode:
authorNathan Hjelm <hjelmn@me.com>2012-11-29 14:23:26 -0700
committerHans de Goede <hdegoede@redhat.com>2013-05-15 17:28:06 +0200
commit7801ff94fa6e49fe98433eccc7f2e461590a6f7c (patch)
tree7d75d9c8f40471b45bbd77e8352ff602d5497c6f /examples
parent29480089519f0b52da7697c34a3bbb67d3e74a3f (diff)
downloadlibusb-7801ff94fa6e49fe98433eccc7f2e461590a6f7c.tar.gz
Add hotplug support.
The internal API is changing as follows: - Adding two new functions. usbi_connect_device, and usbi_disconnect_device. Backends must call these functions to add them to the context's device list at one of two places: initial enumeration (done at init), and on device attach and removal. These functions need to be called once per context. - Backends that support hotplug should not provide a get_device_list funtion. This function is now deprecated and will likely be removed once all backends support hotplug. The external API is changing as follows: - Two new functions have been added to register and deregister callbacks for hotplug notification: libusb_hotplug_register_callback(), libusb_hotplug_deregister_callback(). Hotplug callbacks are called by libusb_handle_events(). Details of the new API can be found in libusb.h. - A new capability check has been added to check for hotplug support. See LIBUSB_CAP_HAS_HOTPLUG. Aa suggested by Xiaofan add new example has been added to show how to use the new external hotplug API. See examples/hotplugtest.c. Signed-off-by: Hans de Goede <hdegoede@redhat.com>
Diffstat (limited to 'examples')
-rw-r--r--examples/Makefile.am2
-rw-r--r--examples/hotplugtest.c96
2 files changed, 97 insertions, 1 deletions
diff --git a/examples/Makefile.am b/examples/Makefile.am
index a28a300..380e13f 100644
--- a/examples/Makefile.am
+++ b/examples/Makefile.am
@@ -1,7 +1,7 @@
AM_CPPFLAGS = -I$(top_srcdir)/libusb
LDADD = ../libusb/libusb-1.0.la
-noinst_PROGRAMS = listdevs xusb fxload
+noinst_PROGRAMS = listdevs xusb fxload hotplugtest
if HAVE_SIGACTION
noinst_PROGRAMS += dpfp
diff --git a/examples/hotplugtest.c b/examples/hotplugtest.c
new file mode 100644
index 0000000..6f16827
--- /dev/null
+++ b/examples/hotplugtest.c
@@ -0,0 +1,96 @@
+/*
+ * libusb example program for hotplug API
+ * Copyright © 2012-2013 Nathan Hjelm <hjelmn@mac.ccom>
+ *
+ * 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 <stdlib.h>
+#include <stdio.h>
+
+#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;
+
+ libusb_init (NULL);
+
+ 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) {
+ libusb_handle_events (NULL);
+ }
+
+ libusb_exit (NULL);
+}