xref: /aosp_15_r20/external/libusb/examples/hotplugtest.c (revision 86b64dcb59b3a0b37502ecd56e119234366a6f7e)
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 <[email protected]>
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 = NULL;
28 
hotplug_callback(libusb_context * ctx,libusb_device * dev,libusb_hotplug_event event,void * user_data)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 	(void)ctx;
35 	(void)dev;
36 	(void)event;
37 	(void)user_data;
38 
39 	rc = libusb_get_device_descriptor(dev, &desc);
40 	if (LIBUSB_SUCCESS == rc) {
41 		printf ("Device attached: %04x:%04x\n", desc.idVendor, desc.idProduct);
42 	} else {
43 		printf ("Device attached\n");
44 		fprintf (stderr, "Error getting device descriptor: %s\n",
45 			 libusb_strerror((enum libusb_error)rc));
46 	}
47 
48 	if (handle) {
49 		libusb_close (handle);
50 		handle = NULL;
51 	}
52 
53 	rc = libusb_open (dev, &handle);
54 	if (LIBUSB_SUCCESS != rc) {
55 		fprintf (stderr, "No access to device: %s\n",
56 			 libusb_strerror((enum libusb_error)rc));
57 	}
58 
59 	done++;
60 
61 	return 0;
62 }
63 
hotplug_callback_detach(libusb_context * ctx,libusb_device * dev,libusb_hotplug_event event,void * user_data)64 static int LIBUSB_CALL hotplug_callback_detach(libusb_context *ctx, libusb_device *dev, libusb_hotplug_event event, void *user_data)
65 {
66 	struct libusb_device_descriptor desc;
67 	int rc;
68 
69 	(void)ctx;
70 	(void)dev;
71 	(void)event;
72 	(void)user_data;
73 
74 	rc = libusb_get_device_descriptor(dev, &desc);
75 	if (LIBUSB_SUCCESS == rc) {
76 		printf ("Device detached: %04x:%04x\n", desc.idVendor, desc.idProduct);
77 	} else {
78 		printf ("Device detached\n");
79 		fprintf (stderr, "Error getting device descriptor: %s\n",
80 			 libusb_strerror((enum libusb_error)rc));
81 	}
82 
83 	if (handle) {
84 		libusb_close (handle);
85 		handle = NULL;
86 	}
87 
88 	done++;
89 
90 	return 0;
91 }
92 
main(int argc,char * argv[])93 int main(int argc, char *argv[])
94 {
95 	libusb_hotplug_callback_handle hp[2];
96 	int product_id, vendor_id, class_id;
97 	int rc;
98 
99 	vendor_id  = (argc > 1) ? (int)strtol (argv[1], NULL, 0) : LIBUSB_HOTPLUG_MATCH_ANY;
100 	product_id = (argc > 2) ? (int)strtol (argv[2], NULL, 0) : LIBUSB_HOTPLUG_MATCH_ANY;
101 	class_id   = (argc > 3) ? (int)strtol (argv[3], NULL, 0) : LIBUSB_HOTPLUG_MATCH_ANY;
102 
103 	rc = libusb_init_context(/*ctx=*/NULL, /*options=*/NULL, /*num_options=*/0);
104 	if (LIBUSB_SUCCESS != rc)
105 	{
106 		printf ("failed to initialise libusb: %s\n",
107 			libusb_strerror((enum libusb_error)rc));
108 		return EXIT_FAILURE;
109 	}
110 
111 	if (!libusb_has_capability (LIBUSB_CAP_HAS_HOTPLUG)) {
112 		printf ("Hotplug capabilities are not supported on this platform\n");
113 		libusb_exit (NULL);
114 		return EXIT_FAILURE;
115 	}
116 
117 	rc = libusb_hotplug_register_callback (NULL, LIBUSB_HOTPLUG_EVENT_DEVICE_ARRIVED, 0, vendor_id,
118 		product_id, class_id, hotplug_callback, NULL, &hp[0]);
119 	if (LIBUSB_SUCCESS != rc) {
120 		fprintf (stderr, "Error registering callback 0\n");
121 		libusb_exit (NULL);
122 		return EXIT_FAILURE;
123 	}
124 
125 	rc = libusb_hotplug_register_callback (NULL, LIBUSB_HOTPLUG_EVENT_DEVICE_LEFT, 0, vendor_id,
126 		product_id,class_id, hotplug_callback_detach, NULL, &hp[1]);
127 	if (LIBUSB_SUCCESS != rc) {
128 		fprintf (stderr, "Error registering callback 1\n");
129 		libusb_exit (NULL);
130 		return EXIT_FAILURE;
131 	}
132 
133 	while (done < 2) {
134 		rc = libusb_handle_events (NULL);
135 		if (LIBUSB_SUCCESS != rc)
136 			printf ("libusb_handle_events() failed: %s\n",
137 				libusb_strerror((enum libusb_error)rc));
138 	}
139 
140 	if (handle) {
141 		libusb_close (handle);
142 	}
143 
144 	libusb_exit (NULL);
145 
146 	return EXIT_SUCCESS;
147 }
148