forked from gary-rowe/hid4java
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathHidServices.java
More file actions
159 lines (139 loc) · 4.53 KB
/
HidServices.java
File metadata and controls
159 lines (139 loc) · 4.53 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
/*
* The MIT License (MIT)
*
* Copyright (c) 2014-2015 Gary Rowe
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*
*/
package org.hid4java;
import org.hid4java.event.HidServicesListenerList;
import org.hid4java.jna.HidApi;
import java.util.List;
/**
* <p>JNA bridge class to provide the following to USB HID:</p>
* <ul>
* <li>Access to the <code>signal11/hidapi</code> via JNA</li>
* </ul>
* <p>Requires the hidapi to be present on the classpath or the system library search path.</p>
*
* @since 0.0.1
*
*/
public class HidServices {
/**
* The HID services listeners for receiving attach/detach events etc
*/
private final HidServicesListenerList listeners = new HidServicesListenerList();
/**
* The HID device manager handles scanning operations
*/
private final HidDeviceManager hidDeviceManager;
/**
* Initialise with a default HID specification
*
* @throws HidException If something goes wrong (see {@link HidDeviceManager#HidDeviceManager(HidServicesListenerList, HidServicesSpecification)}
*/
public HidServices() throws HidException {
this(new HidServicesSpecification());
}
/**
* @param hidServicesSpecification Provides various parameters for configuring HID services
*
* @throws HidException If something goes wrong (see {@link HidDeviceManager#HidDeviceManager(HidServicesListenerList, HidServicesSpecification)}
*/
public HidServices(HidServicesSpecification hidServicesSpecification) {
hidDeviceManager = new HidDeviceManager(listeners, hidServicesSpecification);
hidDeviceManager.start();
if (hidServicesSpecification.isAutoShutdown()) {
// Ensure we release resources during shutdown
Runtime.getRuntime().addShutdownHook(
new Thread() {
@Override
public void run() {
shutdown();
}
});
}
}
/**
* Stop scanning for devices and shut down the {@link HidApi}
*/
public void shutdown() {
stop();
try {
HidApi.exit();
} catch (Throwable e) {
// Silently fail (user will already have been given an exception)
}
}
/**
* Stop scanning for devices
*/
public void stop() {
hidDeviceManager.stop();
}
/**
* Start scanning for devices (if not already scanning)
*/
public void start() {
hidDeviceManager.start();
}
/**
* @param listener The listener to add
*/
public void addHidServicesListener(final HidServicesListener listener) {
this.listeners.add(listener);
}
/**
* @param listener The listener to remove
*/
public void removeUsbServicesListener(final HidServicesListener listener) {
this.listeners.remove(listener);
}
/**
* Manually scans for HID device connection changes and triggers listener events as required
*/
public void scan() {
this.hidDeviceManager.scan();
}
/**
* @return A list of all attached HID devices
*/
public List<HidDevice> getAttachedHidDevices() {
return hidDeviceManager.getAttachedHidDevices();
}
/**
* @param vendorId The vendor ID
* @param productId The product ID
* @param serialNumber The serial number (use null for wildcard)
*
* @return The device if attached, null if detached
*/
public HidDevice getHidDevice(int vendorId, int productId, String serialNumber) {
List<HidDevice> devices = hidDeviceManager.getAttachedHidDevices();
for (HidDevice device : devices) {
if (device.isVidPidSerial(vendorId, productId, serialNumber)) {
device.open();
return device;
}
}
return null;
}
}