1 package org.libsdl.app;
3 import android.hardware.usb.*;
4 import android.os.Build;
5 import android.util.Log;
6 import java.util.Arrays;
8 class HIDDeviceUSB
implements HIDDevice {
10 private static final String
TAG =
"hidapi";
12 protected HIDDeviceManager mManager;
13 protected UsbDevice mDevice;
14 protected int mInterface;
15 protected int mDeviceId;
16 protected UsbDeviceConnection mConnection;
17 protected UsbEndpoint mInputEndpoint;
18 protected UsbEndpoint mOutputEndpoint;
19 protected InputThread mInputThread;
20 protected boolean mRunning;
21 protected boolean mFrozen;
23 public HIDDeviceUSB(HIDDeviceManager manager, UsbDevice usbDevice,
int interface_number) {
26 mInterface = interface_number;
27 mDeviceId = manager.getDeviceIDForIdentifier(getIdentifier());
31 public String getIdentifier() {
32 return String.format(
"%s/%x/%x", mDevice.getDeviceName(), mDevice.getVendorId(), mDevice.getProductId());
41 public int getVendorId() {
42 return mDevice.getVendorId();
46 public int getProductId() {
47 return mDevice.getProductId();
51 public String getSerialNumber() {
53 if (Build.VERSION.SDK_INT >= 21) {
54 result = mDevice.getSerialNumber();
63 public int getVersion() {
68 public String getManufacturerName() {
70 if (Build.VERSION.SDK_INT >= 21) {
71 result = mDevice.getManufacturerName();
74 result = String.format(
"%x", getVendorId());
80 public String getProductName() {
82 if (Build.VERSION.SDK_INT >= 21) {
83 result = mDevice.getProductName();
86 result = String.format(
"%x", getProductId());
91 public UsbDevice getDevice() {
95 public String getDeviceName() {
96 return getManufacturerName() +
" " + getProductName() +
"(0x" + String.format(
"%x", getVendorId()) +
"/0x" + String.format(
"%x", getProductId()) +
")";
100 public boolean open() {
101 mConnection = mManager.getUSBManager().openDevice(mDevice);
102 if (mConnection == null) {
103 Log.w(TAG,
"Unable to open USB device " + getDeviceName());
108 for (
int i = 0;
i < mDevice.getInterfaceCount();
i++) {
109 UsbInterface iface = mDevice.getInterface(
i);
111 if (!mConnection.claimInterface(iface,
true)) {
112 Log.w(TAG,
"Failed to claim interfaces on USB device " + getDeviceName());
119 UsbInterface iface = mDevice.getInterface(mInterface);
120 for (
int j = 0;
j < iface.getEndpointCount();
j++) {
121 UsbEndpoint endpt = iface.getEndpoint(
j);
122 switch (endpt.getDirection()) {
123 case UsbConstants.USB_DIR_IN:
124 if (mInputEndpoint == null) {
125 mInputEndpoint = endpt;
128 case UsbConstants.USB_DIR_OUT:
129 if (mOutputEndpoint == null) {
130 mOutputEndpoint = endpt;
137 if (mInputEndpoint == null || mOutputEndpoint == null) {
138 Log.w(TAG,
"Missing required endpoint on USB device " + getDeviceName());
145 mInputThread =
new InputThread();
146 mInputThread.start();
152 public int sendFeatureReport(byte[] report) {
155 int length = report.length;
156 boolean skipped_report_id =
false;
157 byte report_number = report[0];
159 if (report_number == 0
x0) {
162 skipped_report_id =
true;
165 res = mConnection.controlTransfer(
166 UsbConstants.USB_TYPE_CLASS | 0x01 | UsbConstants.USB_DIR_OUT,
168 (3 << 8) | report_number,
170 report, offset, length,
174 Log.w(TAG,
"sendFeatureReport() returned " + res +
" on device " + getDeviceName());
178 if (skipped_report_id) {
185 public int sendOutputReport(byte[] report) {
186 int r = mConnection.bulkTransfer(mOutputEndpoint, report, report.length, 1000);
187 if (r != report.length) {
188 Log.w(TAG,
"sendOutputReport() returned " + r +
" on device " + getDeviceName());
194 public boolean getFeatureReport(byte[] report) {
197 int length = report.length;
198 boolean skipped_report_id =
false;
199 byte report_number = report[0];
201 if (report_number == 0
x0) {
206 skipped_report_id =
true;
209 res = mConnection.controlTransfer(
210 UsbConstants.USB_TYPE_CLASS | 0x01 | UsbConstants.USB_DIR_IN,
212 (3 << 8) | report_number,
214 report, offset, length,
218 Log.w(TAG,
"getFeatureReport() returned " + res +
" on device " + getDeviceName());
222 if (skipped_report_id) {
231 data = Arrays.copyOfRange(report, 0, res);
233 mManager.HIDDeviceFeatureReport(mDeviceId, data);
239 public void close() {
241 if (mInputThread != null) {
242 while (mInputThread.isAlive()) {
243 mInputThread.interrupt();
246 }
catch (InterruptedException
e) {
252 if (mConnection != null) {
253 for (
int i = 0;
i < mDevice.getInterfaceCount();
i++) {
254 UsbInterface iface = mDevice.getInterface(
i);
255 mConnection.releaseInterface(iface);
263 public void shutdown() {
269 public void setFrozen(
boolean frozen) {
276 int packetSize = mInputEndpoint.getMaxPacketSize();
277 byte[] packet =
new byte[packetSize];
282 r = mConnection.bulkTransfer(mInputEndpoint, packet, packetSize, 1000);
286 Log.v(TAG,
"Exception in UsbDeviceConnection bulktransfer: " + e);
294 if (r == packetSize) {
297 data = Arrays.copyOfRange(packet, 0, r);
301 mManager.HIDDeviceInputReport(mDeviceId, data);
GLdouble GLdouble GLdouble r
SDL_PRINTF_FORMAT_STRING const char int SDL_PRINTF_FORMAT_STRING const char int SDL_PRINTF_FORMAT_STRING const char int SDL_PRINTF_FORMAT_STRING const char const char SDL_SCANF_FORMAT_STRING const char return SDL_ThreadFunction const char void return Uint32 return Uint32 SDL_AssertionHandler void SDL_SpinLock SDL_atomic_t int int return SDL_atomic_t return void void void return void return int return SDL_AudioSpec SDL_AudioSpec return int int return return int SDL_RWops int SDL_AudioSpec Uint8 Uint32 * e
GLint GLenum GLsizei GLsizei GLsizei GLint GLsizei const GLvoid * data
return Display return Display Bool Bool int int int return Display XEvent Bool(*) XPointer return Display return Display Drawable _Xconst char unsigned int unsigned int return Display Pixmap Pixmap XColor XColor unsigned int unsigned int return Display _Xconst char char int char return Display Visual unsigned int int int char unsigned int unsigned int int in j)
return Display return Display Bool Bool int int int return Display XEvent Bool(*) XPointer return Display return Display Drawable _Xconst char unsigned int unsigned int return Display Pixmap Pixmap XColor XColor unsigned int unsigned int return Display _Xconst char char int char return Display Visual unsigned int int int char unsigned int unsigned int in i)
GLuint GLsizei GLsizei * length