forked from gary-rowe/hid4java
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathHidDevice.java
More file actions
372 lines (332 loc) · 10.6 KB
/
HidDevice.java
File metadata and controls
372 lines (332 loc) · 10.6 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
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
/*
* 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.jna.HidApi;
import org.hid4java.jna.HidDeviceInfoStructure;
import org.hid4java.jna.HidDeviceStructure;
/**
* <p>
* High level wrapper to provide the following to API consumers:
* </p>
* <ul>
* <li>Simplified access to the underlying JNA HidDeviceStructure</li>
* </ul>
*
* @since 0.0.1
*/
public class HidDevice {
private final HidDeviceManager hidDeviceManager;
private HidDeviceStructure hidDeviceStructure;
private String path;
private short vendorId;
private short productId;
private String serialNumber;
private int releaseNumber;
private String manufacturer;
private String product;
private int usagePage;
private int usage;
private int interfaceNumber;
/**
* @param infoStructure The HID device info structure providing details
* @param hidDeviceManager The HID device manager providing access to device enumeration for post IO scanning
*/
public HidDevice(HidDeviceInfoStructure infoStructure, HidDeviceManager hidDeviceManager) {
this.hidDeviceManager = hidDeviceManager;
this.hidDeviceStructure = null;
this.path = infoStructure.path;
this.vendorId = infoStructure.vendor_id;
this.productId = infoStructure.product_id;
this.releaseNumber = infoStructure.release_number;
if (infoStructure.serial_number != null) {
this.serialNumber = infoStructure.serial_number.toString();
}
if (infoStructure.manufacturer_string != null) {
this.manufacturer = infoStructure.manufacturer_string.toString();
}
if (infoStructure.product_string != null) {
this.product = infoStructure.product_string.toString();
}
this.usagePage = infoStructure.usage_page;
this.usage = infoStructure.usage;
this.interfaceNumber = infoStructure.interface_number;
}
/**
* The "path" is well-supported across Windows, Mac and Linux so makes a
* better choice for a unique ID
*
* See #8 for details
*
* @return A unique device ID made up from vendor ID, product ID and serial number
*/
public String getId() {
return path;
}
public String getPath() {
return path;
}
public short getVendorId() {
return vendorId;
}
public short getProductId() {
return productId;
}
public String getSerialNumber() {
return serialNumber;
}
public int getReleaseNumber() {
return releaseNumber;
}
public String getManufacturer() {
return manufacturer;
}
public String getProduct() {
return product;
}
public int getUsagePage() {
return usagePage;
}
public int getUsage() {
return usage;
}
public int getInterfaceNumber() {
return interfaceNumber;
}
/**
* <p>Open this device and obtain a device structure</p>
* @return True if the device was successfully opened
*/
public boolean open() {
hidDeviceStructure = HidApi.open(path);
return hidDeviceStructure != null;
}
/**
* @return True if the device structure is present
*/
public boolean isOpen() {
return hidDeviceStructure != null;
}
/**
* <p>
* Close this device freeing the HidApi resources
* </p>
*/
public void close() {
if (!isOpen()) {
return;
}
HidApi.close(hidDeviceStructure);
hidDeviceStructure = null;
}
/**
* <p>
* Set the device handle to be non-blocking
* </p>
*
* <p>
* In non-blocking mode calls to hid_read() will return immediately with a
* value of 0 if there is no data to be read. In blocking mode, hid_read()
* will wait (block) until there is data to read before returning
* </p>
*
* <p>
* Non-blocking can be turned on and off at any time
* </p>
*
* @param nonBlocking True if non-blocking mode is required
*/
public void setNonBlocking(boolean nonBlocking) {
if (!isOpen()) {
throw new IllegalStateException("Device has not been opened");
}
HidApi.setNonBlocking(hidDeviceStructure, nonBlocking);
}
/**
* <p>
* Read an Input report from a HID device
* </p>
* <p>
* Input reports are returned to the host through the INTERRUPT IN endpoint.
* The first byte will contain the Report number if the device uses numbered
* reports
* </p>
*
* @param data The buffer to read into
*
* @return The actual number of bytes read and -1 on error. If no packet was
* available to be read and the handle is in non-blocking mode, this
* function returns 0.
*/
public int read(byte[] data) {
if (!isOpen()) {
throw new IllegalStateException("Device has not been opened");
}
return HidApi.read(hidDeviceStructure, data);
}
/**
* <p>
* Read an Input report from a HID device with timeout
* </p>
*
* @param bytes The buffer to read into
* @param timeoutMillis The number of milliseconds to wait before giving up
*
* @return The actual number of bytes read and -1 on error. If no packet was
* available to be read within the timeout period returns 0.
*/
public int read(byte[] bytes, int timeoutMillis) {
if (!isOpen()) {
throw new IllegalStateException("Device has not been opened");
}
return HidApi.read(hidDeviceStructure, bytes, timeoutMillis);
}
/**
* <p>
* Get a feature report from a HID device
* </p>
* <p>
* Under the covers the HID library will set the first byte of data[] to the
* Report ID of the report to be read. Upon return, the first byte will
* still contain the Report ID, and the report data will start in data[1]
* </p>
* <p>
* This method handles all the wide string and array manipulation for you
* </p>
*
* @param data The buffer to contain the report
* @param reportId The report ID (or (byte) 0x00)
*
* @return The number of bytes read plus one for the report ID (which has
* been removed from the first byte), or -1 on error.
*/
public int getFeatureReport(byte[] data, byte reportId) {
if (!isOpen()) {
throw new IllegalStateException("Device has not been opened");
}
return HidApi.getFeatureReport(hidDeviceStructure, data, reportId);
}
/**
* <p>
* Send a Feature report to the device
* </p>
*
* <p>
* Under the covers, feature reports are sent over the Control endpoint as a
* Set_Report transfer. The first byte of data[] must contain the Report ID.
* For devices which only support a single report, this must be set to 0x0.
* The remaining bytes contain the report data
* </p>
* <p>
* Since the Report ID is mandatory, calls to hid_send_feature_report() will
* always contain one more byte than the report contains. For example, if a
* hid report is 16 bytes long, 17 bytes must be passed to
* hid_send_feature_report(): the Report ID (or 0x0, for devices which do
* not use numbered reports), followed by the report data (16 bytes). In
* this example, the length passed in would be 17
* </p>
*
* <p>
* This method handles all the array manipulation for you
* </p>
*
* @param data The feature report data (will be widened and have the report
* ID pre-pended)
* @param reportId The report ID (or (byte) 0x00)
*
* @return This function returns the actual number of bytes written and -1
* on error.
*/
public int sendFeatureReport(byte[] data, byte reportId) {
if (!isOpen()) {
throw new IllegalStateException("Device has not been opened");
}
return HidApi.sendFeatureReport(hidDeviceStructure, data, reportId);
}
/**
* <p>
* Get a string from a HID device, based on its string index
* </p>
*
* @param index The index
*
* @return The string
*/
public String getIndexedString(int index) {
return HidApi.getIndexedString(hidDeviceStructure, index);
}
public int write(byte[] message, int packetLength, byte reportId) {
if (!isOpen()) {
throw new IllegalStateException("Device has not been opened");
}
int result = HidApi.write(hidDeviceStructure, message, packetLength, reportId);
// Update HID manager
hidDeviceManager.afterDeviceWrite();
return result;
}
public String getLastErrorMessage() {
return HidApi.getLastErrorMessage(hidDeviceStructure);
}
/**
* @param vendorId The vendor ID
* @param productId The product ID
* @param serialNumber The serial number
*
* @return True if the device matches the given the combination
*/
public boolean isVidPidSerial(int vendorId, int productId, String serialNumber) {
if(serialNumber == null)
return (vendorId == 0 || this.vendorId == vendorId)
&& (productId == 0 || this.productId == productId);
else
return (vendorId == 0 || this.vendorId == vendorId)
&& (productId == 0 || this.productId == productId)
&& ( this.serialNumber.equals(serialNumber));
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
HidDevice hidDevice = (HidDevice) o;
return path.equals(hidDevice.path);
}
@Override
public int hashCode() {
return path.hashCode();
}
@Override
public String toString() {
return "HidDevice [path=" + path
+ ", vendorId=0x" + Integer.toHexString(vendorId)
+ ", productId=0x" + Integer.toHexString(productId)
+ ", serialNumber=" + serialNumber
+ ", releaseNumber=0x" + Integer.toHexString(releaseNumber)
+ ", manufacturer=" + manufacturer
+ ", product=" + product
+ ", usagePage=0x" + Integer.toHexString(usagePage)
+ ", usage=0x" + Integer.toHexString(usage)
+ ", interfaceNumber=" + interfaceNumber
+ "]";
}
}