-
Notifications
You must be signed in to change notification settings - Fork 151
/
UsbAccessor.java
268 lines (239 loc) · 8.22 KB
/
UsbAccessor.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
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
/*
* Copyright (C) 2013 Keisuke SUZUKI
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* Distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.physicaloid.lib.usb;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
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 android.util.SparseArray;
/*
* USB Hierarchy Accessor
* Enum singleton pattern
*/
public enum UsbAccessor {
INSTANCE; // enum singleton
private static final boolean DEBUG_SHOW = true;
private static final String TAG = UsbAccessor.class.getSimpleName();
private UsbManager mManager = null;
private PendingIntent mPermissionIntent = null;
private SparseArray<UsbDeviceConnection> mConnection;
private UsbAccessor() {
mConnection = new SparseArray<UsbDeviceConnection>();
}
/**
* Initializes USB Manager
* @param context MainActivity's context
*/
@SuppressWarnings("static-access")
public void init(Context context) {
if(mManager == null) {
mManager = (UsbManager) context.getSystemService(context.USB_SERVICE);
}
if(mPermissionIntent == null) {
mPermissionIntent = PendingIntent.getBroadcast(context, 0, new Intent("USB_PERMISSION"), 0);
}
}
public UsbManager manager() {
return mManager;
}
/**
* Gets UsbDevice by a hierarchy device number
* @param devNum hierarchy device number
* @return UsbDevice or null
*/
public UsbDevice device(int devNum) {
if(mManager == null || mPermissionIntent == null) return null;
if(mManager.getDeviceList().size() <= devNum) return null;
int count=0;
for (UsbDevice device : mManager.getDeviceList().values()) {
if(DEBUG_SHOW){ Log.d(TAG, "Device("+count+") : " + device.toString()); }
if(count == devNum) {
getPermission(device);
if (!mManager.hasPermission(device)) {
if(DEBUG_SHOW){ Log.d(TAG, "Doesn't have permission device("+count+") : " + device.toString()); }
return null;
} else {
return device;
}
}
count++;
}
if(DEBUG_SHOW){ Log.d(TAG, "Cannot find device("+devNum+")"); }
return null;
}
/**
* Gets UsbInterface by a hierarchy device and interface number
* @param devNum hierarchy device number
* @param intfNum hierarchy interface number
* @return UsbInterface or null
*/
public UsbInterface intface(int devNum, int intfNum) {
UsbDevice dev = device(devNum);
if(dev == null) { return null; }
int devCount = dev.getInterfaceCount();
for(int i=0; i<devCount; i++) {
if(i==intfNum) {
if(DEBUG_SHOW){ Log.d(TAG, "Interface("+devNum+","+intfNum+") : " + dev.getInterface(i).toString()); }
return dev.getInterface(i);
}
}
if(DEBUG_SHOW){ Log.d(TAG, "Cannot find interface("+devNum+","+intfNum+")"); }
return null;
}
/**
* Gets UsbEndpoint by a hierarchy device, interface and endpoint number
* @param devNum hierarchy device number
* @param intfNum hierarchy interface number
* @param epNum hierarchy endpoint number
* @return UsbEndpoint or null
*/
public UsbEndpoint endpoint(int devNum, int intfNum, int epNum) {
UsbInterface intf = intface(devNum, intfNum);
if(intf == null) {return null;}
int epCount = intf.getEndpointCount();
for(int i=0; i<epCount; i++) {
if(i==epNum) {
if(DEBUG_SHOW){ Log.d(TAG, "Endpoint("+devNum+","+intfNum+","+epNum+") : " + intf.getEndpoint(i).toString()); }
return intf.getEndpoint(i);
}
}
if(DEBUG_SHOW){ Log.d(TAG, "Cannot find endpoint("+devNum+","+intfNum+","+epNum+")"); }
return null;
}
/**
* Gets UsbDeviceConnection by a hierarchy device number
* @param devNum hierarchy device number
* @return UsbDeviceConnection or null
*/
public UsbDeviceConnection connection(int ch) {
if(ch >= mConnection.size()) { return null; }
return mConnection.get(ch);
}
/**
* Check whether a device is connected or not
* @param devNum hierarchy device number
* @return true:connected, false:not connected
*/
public boolean deviceIsConnected(int devNum) {
if(connection(devNum) == null) return false;
return true;
}
/**
* Connect a USB device
* @return UsbDeviceConnection or null
*/
public boolean openDevice() {
return openDevice(0, 0, 0);
}
/**
* Connect a USB device by a hierarchy device and interface number
* @param devNum hierarchy device number
* @return UsbDeviceConnection or null
*/
public boolean openDevice(int devNum) {
return openDevice(devNum, 0, 0);
}
/**
* Connect a USB device by a hierarchy device and interface number
* @param devNum hierarchy device number
* @param intfNum hierarchy interface number
* @param ch channel number
* @return UsbDeviceConnection or null
*/
public boolean openDevice(int devNum, int intfNum, int ch) {
UsbDevice dev = device(devNum);
if(dev == null) { return false; }
UsbDeviceConnection con = mManager.openDevice(dev);
if(con == null) { return false; }
if(con.claimInterface(intface(devNum, intfNum), true)) {
mConnection.put(ch,con);
return true;
} else {
if(DEBUG_SHOW){ Log.d(TAG, "Cannot get claim interface("+devNum+","+intfNum+")"); }
mConnection.remove(devNum);
return false;
}
}
public boolean close(int devNum) {
UsbDeviceConnection con = connection(devNum);
if(con == null) return false;
mConnection.remove(devNum);
con.close();
if(DEBUG_SHOW){ Log.d(TAG, "Close("+devNum+")"); }
return true;
}
public boolean closeAll() {
boolean ret = false;
UsbDeviceConnection con;
for(int i=0; i < mConnection.size(); i++) {
con = mConnection.valueAt(i);
if(con == null) continue;
if(DEBUG_SHOW){ Log.d(TAG, "Close("+mConnection.keyAt(i)+")"); }
con.close();
ret = true;
}
mConnection.clear();
return ret;
}
/**
* Gets devNum device's VID
* @param devNum
* @return
*/
public int getVid(int devNum) {
UsbDevice dev = device(devNum);
if(dev == null) return 0;
return dev.getVendorId();
}
/**
* Gets devNum device's PID
* @param devNum
* @return
*/
public int getPid(int devNum) {
UsbDevice dev = device(devNum);
if(dev == null) return 0;
return dev.getProductId();
}
/**
* Gets devNum device's SerialID
* @param devNum
* @return
*/
public String getSerial(int devNum) {
if(connection(devNum) == null) return "";
return connection(devNum).getSerial();
}
/**
* Gets an USB permission if no permission
*
* @param device
*/
public void getPermission(UsbDevice device) {
if(mManager == null) return;
if (device != null && mPermissionIntent != null) {
if (!mManager.hasPermission(device)) {
if(DEBUG_SHOW){ Log.d(TAG, "Request permission : "+device.toString()); }
mManager.requestPermission(device, mPermissionIntent);
}
}
}
}