-
Notifications
You must be signed in to change notification settings - Fork 818
/
Copy pathCommunicationHandler.java
227 lines (192 loc) · 8.19 KB
/
CommunicationHandler.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
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
package io.pslab.communication;
import android.hardware.usb.UsbConstants;
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.os.Build;
import android.os.SystemClock;
import android.util.Log;
import java.io.IOException;
public class CommunicationHandler {
private final String TAG = this.getClass().getSimpleName();
private static final int PSLAB_VENDOR_ID = 1240;
private static final int PSLAB_PRODUCT_ID = 223;
private UsbInterface mControlInterface;
private UsbInterface mDataInterface;
private UsbEndpoint mControlEndpoint;
private UsbEndpoint mReadEndpoint;
private UsbEndpoint mWriteEndpoint;
private boolean mRts = false;
private boolean mDtr = false;
private boolean connected = false, device_found = false;
private static final int USB_RECIP_INTERFACE = 0x01;
private static final int USB_RT_ACM = UsbConstants.USB_TYPE_CLASS | USB_RECIP_INTERFACE;
private static final int SET_LINE_CODING = 0x20; // USB CDC 1.1 section 6.2
private static final int GET_LINE_CODING = 0x21;
private static final int SET_CONTROL_LINE_STATE = 0x22;
private static final int SEND_BREAK = 0x23;
private static final int DEFAULT_READ_BUFFER_SIZE = 32 * 1024;
private static final int DEFAULT_WRITE_BUFFER_SIZE = 32 * 1024;
public UsbDevice mUsbDevice = null;
protected final Object mReadBufferLock = new Object();
protected final Object mWriteBufferLock = new Object();
private byte[] mReadBuffer;
private byte[] mWriteBuffer;
private UsbDeviceConnection mConnection;
private UsbManager mUsbManager;
public CommunicationHandler(UsbManager usbManager) {
this.mUsbManager = usbManager;
mUsbDevice = null;
for (final UsbDevice device : mUsbManager.getDeviceList().values()) {
Log.d(TAG, "VID : " + device.getVendorId() + "PID : " + device.getProductId());
if (device.getVendorId() == PSLAB_VENDOR_ID && device.getProductId() == PSLAB_PRODUCT_ID) {
Log.d(TAG, "Found PSLAB Device");
mUsbDevice = device;
device_found = true;
break;
}
}
mReadBuffer = new byte[DEFAULT_READ_BUFFER_SIZE];
mWriteBuffer = new byte[DEFAULT_WRITE_BUFFER_SIZE];
}
public void open() throws IOException {
if (!device_found) {
throw new IOException("Device not Connected");
}
mConnection = mUsbManager.openDevice(mUsbDevice);
Log.d(TAG, "Claiming interfaces, count=" + mUsbDevice.getInterfaceCount());
mConnection.controlTransfer(0x21, 0x22, 0x1, 0, null, 0, 0);
mControlInterface = mUsbDevice.getInterface(0);
Log.d(TAG, "Control interface=" + mControlInterface);
if (!mConnection.claimInterface(mControlInterface, true)) {
throw new IOException("Could not claim control interface.");
}
mControlEndpoint = mControlInterface.getEndpoint(0);
Log.d(TAG, "Control endpoint direction: " + mControlEndpoint.getDirection());
Log.d(TAG, "Claiming data interface.");
mDataInterface = mUsbDevice.getInterface(1);
Log.d(TAG, "data interface=" + mDataInterface);
if (!mConnection.claimInterface(mDataInterface, true)) {
throw new IOException("Could not claim data interface.");
}
mReadEndpoint = mDataInterface.getEndpoint(1);
Log.d(TAG, "Read endpoint direction: " + mReadEndpoint.getDirection());
mWriteEndpoint = mDataInterface.getEndpoint(0);
Log.d(TAG, "Write endpoint direction: " + mWriteEndpoint.getDirection());
connected = true;
setBaudRate(1000000);
//Thread.sleep(1000);
clear();
}
public boolean isDeviceFound() {
return device_found;
}
public boolean isConnected() {
return connected;
}
public void close() throws IOException {
if (mConnection == null) {
return;
}
mConnection.releaseInterface(mDataInterface);
mConnection.releaseInterface(mControlInterface);
mConnection.close();
connected = false;
mConnection = null;
}
public int read(byte[] dest, int bytesToBeRead, int timeoutMillis) throws IOException {
int numBytesRead = 0;
//synchronized (mReadBufferLock) {
int readNow;
Log.v(TAG, "TO read : " + bytesToBeRead);
int bytesToBeReadTemp = bytesToBeRead;
while (numBytesRead < bytesToBeRead) {
readNow = mConnection.bulkTransfer(mReadEndpoint, mReadBuffer, bytesToBeReadTemp, timeoutMillis);
if (readNow < 0) {
Log.e(TAG, "Read Error: " + bytesToBeReadTemp);
return numBytesRead;
} else {
//Log.v(TAG, "Read something" + mReadBuffer);
System.arraycopy(mReadBuffer, 0, dest, numBytesRead, readNow);
numBytesRead += readNow;
bytesToBeReadTemp -= readNow;
//Log.v(TAG, "READ : " + numBytesRead);
//Log.v(TAG, "REMAINING: " + bytesToBeRead);
}
}
//}
Log.v("Bytes Read", "" + numBytesRead);
return numBytesRead;
}
public int write(byte[] src, int timeoutMillis) throws IOException {
if (Build.VERSION.SDK_INT < 18) {
return writeSupportAPI(src, timeoutMillis);
}
int written = 0;
while (written < src.length) {
int writeLength, amtWritten;
//synchronized (mWriteBufferLock) {
writeLength = Math.min(mWriteBuffer.length, src.length - written);
// bulk transfer supports offset from API 18
amtWritten = mConnection.bulkTransfer(mWriteEndpoint, src, written, writeLength, timeoutMillis);
//}
if (amtWritten < 0) {
throw new IOException("Error writing " + writeLength
+ " bytes at offset " + written + " length=" + src.length);
}
written += amtWritten;
}
return written;
}
// For supporting devices with API version < 18
private int writeSupportAPI(byte[] src, int timeoutMillis) throws IOException {
int written = 0;
while (written < src.length) {
final int writeLength;
final int amtWritten;
//synchronized (mWriteBufferLock) {
final byte[] writeBuffer;
writeLength = Math.min(src.length - written, mWriteBuffer.length);
if (written == 0) {
writeBuffer = src;
} else {
// bulkTransfer does not support offsets for API level < 18, so make a copy.
System.arraycopy(src, written, mWriteBuffer, 0, writeLength);
writeBuffer = mWriteBuffer;
}
amtWritten = mConnection.bulkTransfer(mWriteEndpoint, writeBuffer, writeLength, timeoutMillis);
//}
if (amtWritten <= 0) {
throw new IOException("Error writing " + writeLength
+ " bytes at offset " + written + " length=" + src.length);
}
written += amtWritten;
}
return written;
}
private void clear() {
mConnection.bulkTransfer(mReadEndpoint, mReadBuffer, 100, 50);
}
private void setBaudRate(int baudRate) {
byte[] msg = {
(byte) (baudRate & 0xff),
(byte) ((baudRate >> 8) & 0xff),
(byte) ((baudRate >> 16) & 0xff),
(byte) ((baudRate >> 24) & 0xff),
(byte) 0,
(byte) 0,
(byte) 8};
sendAcmControlMessage(SET_LINE_CODING, 0, msg);
SystemClock.sleep(100);
clear();
}
private int sendAcmControlMessage(int request, int value, byte[] buf) {
return mConnection.controlTransfer(USB_RT_ACM, request, value, 0, buf, buf != null ? buf.length : 0, 5000);
}
public void setDtrRts() {
int value = (mRts ? 0x2 : 0) | (mDtr ? 0x1 : 0);
sendAcmControlMessage(SET_CONTROL_LINE_STATE, value, null);
}
}