Fix unused parameter warnings
[platform/upstream/libusb.git] / examples / hotplugtest.c
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
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
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                 fprintf (stderr, "Error getting device descriptor\n");
42         }
43
44         printf ("Device attached: %04x:%04x\n", desc.idVendor, desc.idProduct);
45
46         if (handle) {
47                 libusb_close (handle);
48                 handle = NULL;
49         }
50
51         rc = libusb_open (dev, &handle);
52         if (LIBUSB_SUCCESS != rc) {
53                 fprintf (stderr, "Error opening device\n");
54         }
55
56         done++;
57
58         return 0;
59 }
60
61 static int LIBUSB_CALL hotplug_callback_detach(libusb_context *ctx, libusb_device *dev, libusb_hotplug_event event, void *user_data)
62 {
63         (void)ctx;
64         (void)dev;
65         (void)event;
66         (void)user_data;
67
68         printf ("Device detached\n");
69
70         if (handle) {
71                 libusb_close (handle);
72                 handle = NULL;
73         }
74
75         done++;
76
77         return 0;
78 }
79
80 int main(int argc, char *argv[])
81 {
82         libusb_hotplug_callback_handle hp[2];
83         int product_id, vendor_id, class_id;
84         int rc;
85
86         vendor_id  = (argc > 1) ? (int)strtol (argv[1], NULL, 0) : 0x045a;
87         product_id = (argc > 2) ? (int)strtol (argv[2], NULL, 0) : 0x5005;
88         class_id   = (argc > 3) ? (int)strtol (argv[3], NULL, 0) : LIBUSB_HOTPLUG_MATCH_ANY;
89
90         rc = libusb_init (NULL);
91         if (rc < 0)
92         {
93                 printf("failed to initialise libusb: %s\n", libusb_error_name(rc));
94                 return EXIT_FAILURE;
95         }
96
97         if (!libusb_has_capability (LIBUSB_CAP_HAS_HOTPLUG)) {
98                 printf ("Hotplug capabilites are not supported on this platform\n");
99                 libusb_exit (NULL);
100                 return EXIT_FAILURE;
101         }
102
103         rc = libusb_hotplug_register_callback (NULL, LIBUSB_HOTPLUG_EVENT_DEVICE_ARRIVED, 0, vendor_id,
104                 product_id, class_id, hotplug_callback, NULL, &hp[0]);
105         if (LIBUSB_SUCCESS != rc) {
106                 fprintf (stderr, "Error registering callback 0\n");
107                 libusb_exit (NULL);
108                 return EXIT_FAILURE;
109         }
110
111         rc = libusb_hotplug_register_callback (NULL, LIBUSB_HOTPLUG_EVENT_DEVICE_LEFT, 0, vendor_id,
112                 product_id,class_id, hotplug_callback_detach, NULL, &hp[1]);
113         if (LIBUSB_SUCCESS != rc) {
114                 fprintf (stderr, "Error registering callback 1\n");
115                 libusb_exit (NULL);
116                 return EXIT_FAILURE;
117         }
118
119         while (done < 2) {
120                 rc = libusb_handle_events (NULL);
121                 if (rc < 0)
122                         printf("libusb_handle_events() failed: %s\n", libusb_error_name(rc));
123         }
124
125         if (handle) {
126                 libusb_close (handle);
127         }
128
129         libusb_exit (NULL);
130
131         return EXIT_SUCCESS;
132 }