This repository has been archived by the owner on May 14, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 311
/
UsbHidDriver.java
98 lines (76 loc) · 3.12 KB
/
UsbHidDriver.java
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
package info.nightscout.android.USB;
import android.hardware.usb.UsbDevice;
import android.hardware.usb.UsbDeviceConnection;
import android.hardware.usb.UsbEndpoint;
import android.hardware.usb.UsbInterface;
import android.hardware.usb.UsbManager;
import android.util.Log;
import java.io.IOException;
// simplified usb driver targeting CNL comms
// keeps overhead to a minimum allowing for high speed transfer of bulk packets from pump to cnl to uploader
public class UsbHidDriver {
private final String TAG = UsbHidDriver.class.getSimpleName();
private UsbInterface mInterface;
private UsbEndpoint mReadEndpoint;
private UsbEndpoint mWriteEndpoint;
private UsbDevice mDevice;
private UsbDeviceConnection mConnection;
private boolean isConnectionOpen = false;
public UsbHidDriver(UsbDevice device, UsbDeviceConnection connection) {
mDevice = device;
mConnection = connection;
}
public static UsbDevice getUsbDevice(UsbManager usbManager, int vendorId, int productId) {
// Iterate all the available devices and find ours.
for (UsbDevice device : usbManager.getDeviceList().values()) {
if (device.getProductId() == productId && device.getVendorId() == vendorId) {
return device;
}
}
return null;
}
public static UsbHidDriver acquire(UsbManager usbManager, UsbDevice device) {
if (device != null) {
final UsbDeviceConnection mConnection = usbManager.openDevice(device);
return new UsbHidDriver(device, mConnection);
}
return null;
}
public void open() throws IOException {
Log.d(TAG, "Claiming HID interface.");
mInterface = mDevice.getInterface(0);
if (!mConnection.claimInterface(mInterface, true)) {
isConnectionOpen = false;
throw new IOException("Could not claim data interface.");
}
mReadEndpoint = mInterface.getEndpoint(1);
Log.d(TAG, "Read endpoint direction: " + mReadEndpoint.getDirection());
mWriteEndpoint = mInterface.getEndpoint(0);
Log.d(TAG, "Write endpoint direction: " + mWriteEndpoint.getDirection());
isConnectionOpen = true;
}
public void close() {
synchronized (UsbHidDriver.class) {
if (mConnection != null && isConnectionOpen) {
Log.d(TAG, "Releasing HID interface.");
if (!mConnection.releaseInterface(mInterface))
Log.w(TAG, "releaseInterface returned false");
mConnection.close();
}
isConnectionOpen = false;
}
}
public int read(byte[] dest, int timeoutMillis) {
return mConnection.bulkTransfer(mReadEndpoint, dest, dest.length,
timeoutMillis);
}
public void write(byte[] src, int timeoutMillis) throws IOException {
if (mConnection.bulkTransfer(mWriteEndpoint, src, src.length,
timeoutMillis) <= 0) {
throw new IOException("Error writing to usb endpoint");
}
}
public boolean isConnectionOpen() {
return isConnectionOpen;
}
}