1
0
mirror of https://bitbucket.org/librepilot/librepilot.git synced 2025-01-29 14:52:12 +01:00

Remove legacy code for supporting two USB interfaces

This commit is contained in:
James Cotton 2012-08-12 13:41:15 -05:00
parent 050ec8096b
commit 6b4a14fa87

View File

@ -28,9 +28,10 @@ import android.util.Log;
public class HidUAVTalk extends TelemetryTask { public class HidUAVTalk extends TelemetryTask {
private static final String TAG = HidUAVTalk.class.getSimpleName(); private static final String TAG = HidUAVTalk.class.getSimpleName();
public static int LOGLEVEL = 2; public static final int LOGLEVEL = 2;
public static boolean WARN = LOGLEVEL > 1; public static final boolean DEBUG = LOGLEVEL > 2;
public static boolean DEBUG = LOGLEVEL > 0; public static final boolean WARN = LOGLEVEL > 1;
public static final boolean ERROR = LOGLEVEL > 0;
//! USB constants //! USB constants
private static final int MAX_HID_PACKET_SIZE = 64; private static final int MAX_HID_PACKET_SIZE = 64;
@ -46,19 +47,14 @@ public class HidUAVTalk extends TelemetryTask {
private static final String ACTION_USB_PERMISSION = "com.access.device.USB_PERMISSION"; private static final String ACTION_USB_PERMISSION = "com.access.device.USB_PERMISSION";
//! Define whether to use a single interface for reading and writing
private final boolean UsingSingleInterface = true;
private UsbDevice currentDevice; private UsbDevice currentDevice;
private UsbEndpoint usbEndpointRead; private UsbEndpoint usbEndpointRead;
private UsbEndpoint usbEndpointWrite; private UsbEndpoint usbEndpointWrite;
private UsbManager usbManager; private UsbManager usbManager;
private PendingIntent permissionIntent; private PendingIntent permissionIntent;
private UsbDeviceConnection connectionRead; private UsbDeviceConnection usbDeviceConnection;
private UsbDeviceConnection connectionWrite;
private IntentFilter permissionFilter; private IntentFilter permissionFilter;
private UsbInterface usbInterfaceRead = null; private UsbInterface usbInterface = null;
private UsbInterface usbInterfaceWrite = null;
private TalkInputStream inTalkStream; private TalkInputStream inTalkStream;
private TalkOutputStream outTalkStream; private TalkOutputStream outTalkStream;
private UsbRequest writeRequest = null; private UsbRequest writeRequest = null;
@ -218,19 +214,9 @@ public class HidUAVTalk extends TelemetryTask {
protected void CleanUpAndClose() { protected void CleanUpAndClose() {
if (UsingSingleInterface) { if(usbDeviceConnection != null && usbInterface != null)
if(connectionRead != null && usbInterfaceRead != null) usbDeviceConnection.releaseInterface(usbInterface);
connectionRead.releaseInterface(usbInterfaceRead); usbInterface = null;
usbInterfaceRead = null;
}
else {
if(connectionRead != null && usbInterfaceRead != null)
connectionRead.releaseInterface(usbInterfaceRead);
if(connectionWrite != null && usbInterfaceWrite != null)
connectionWrite.releaseInterface(usbInterfaceWrite);
usbInterfaceWrite = null;
usbInterfaceRead = null;
}
} }
//Validating the Connected Device - Before asking for permission to connect to the device, it is essential that you ensure that this is a device that you support or expect to connect to. This can be done by validating the devices Vendor ID and Product ID. //Validating the Connected Device - Before asking for permission to connect to the device, it is essential that you ensure that this is a device that you support or expect to connect to. This can be done by validating the devices Vendor ID and Product ID.
@ -258,87 +244,54 @@ public class HidUAVTalk extends TelemetryTask {
UsbEndpoint ep1 = null; UsbEndpoint ep1 = null;
UsbEndpoint ep2 = null; UsbEndpoint ep2 = null;
// Using the same interface for reading and writing
if (UsingSingleInterface) usbInterface = connectDevice.getInterface(0x2);
if (usbInterface.getEndpointCount() == 2)
{ {
// Using the same interface for reading and writing ep1 = usbInterface.getEndpoint(0);
usbInterfaceRead = connectDevice.getInterface(0x2); ep2 = usbInterface.getEndpoint(1);
usbInterfaceWrite = usbInterfaceRead;
if (usbInterfaceRead.getEndpointCount() == 2)
{
ep1 = usbInterfaceRead.getEndpoint(0);
ep2 = usbInterfaceRead.getEndpoint(1);
}
} }
else // if (!UsingSingleInterface)
{
usbInterfaceRead = connectDevice.getInterface(0x01);
usbInterfaceWrite = connectDevice.getInterface(0x02);
if ((usbInterfaceRead.getEndpointCount() == 1) && (usbInterfaceWrite.getEndpointCount() == 1))
{
ep1 = usbInterfaceRead.getEndpoint(0);
ep2 = usbInterfaceWrite.getEndpoint(0);
}
}
if ((ep1 == null) || (ep2 == null)) if ((ep1 == null) || (ep2 == null))
{ {
if (DEBUG) Log.d(TAG, "Null endpoints"); if (ERROR) Log.e(TAG, "Null endpoints");
return false; return false;
} }
// Determine which endpoint is the read, and which is the write // Determine which endpoint is the read, and which is the write
if (ep1.getType() == UsbConstants.USB_ENDPOINT_XFER_INT) if (ep1.getType() == UsbConstants.USB_ENDPOINT_XFER_INT)
{ {
if (ep1.getDirection() == UsbConstants.USB_DIR_IN) if (ep1.getDirection() == UsbConstants.USB_DIR_IN)
{
usbEndpointRead = ep1; usbEndpointRead = ep1;
}
else if (ep1.getDirection() == UsbConstants.USB_DIR_OUT) else if (ep1.getDirection() == UsbConstants.USB_DIR_OUT)
{
usbEndpointWrite = ep1; usbEndpointWrite = ep1;
}
} }
if (ep2.getType() == UsbConstants.USB_ENDPOINT_XFER_INT) if (ep2.getType() == UsbConstants.USB_ENDPOINT_XFER_INT)
{ {
if (ep2.getDirection() == UsbConstants.USB_DIR_IN) if (ep2.getDirection() == UsbConstants.USB_DIR_IN)
{
usbEndpointRead = ep2; usbEndpointRead = ep2;
}
else if (ep2.getDirection() == UsbConstants.USB_DIR_OUT) else if (ep2.getDirection() == UsbConstants.USB_DIR_OUT)
{
usbEndpointWrite = ep2; usbEndpointWrite = ep2;
}
} }
if ((usbEndpointRead == null) || (usbEndpointWrite == null)) if ((usbEndpointRead == null) || (usbEndpointWrite == null))
{ {
if (DEBUG) Log.d(TAG, "Endpoints wrong way around"); if (ERROR) Log.e(TAG, "Could not find write and read endpoint");
return false; return false;
} }
connectionRead = usbManager.openDevice(connectDevice);
connectionRead.claimInterface(usbInterfaceRead, true);
// Claim the interface
usbDeviceConnection = usbManager.openDevice(connectDevice);
usbDeviceConnection.claimInterface(usbInterface, true);
if (UsingSingleInterface)
{
connectionWrite = connectionRead;
}
else // if (!UsingSingleInterface)
{
connectionWrite = usbManager.openDevice(connectDevice);
connectionWrite.claimInterface(usbInterfaceWrite, true);
}
if (DEBUG) Log.d(TAG, "Opened endpoints"); if (DEBUG) Log.d(TAG, "Opened endpoints");
// Create the USB requests // Create the USB requests
readRequest = new UsbRequest(); readRequest = new UsbRequest();
readRequest.initialize(connectionRead, usbEndpointRead); readRequest.initialize(usbDeviceConnection, usbEndpointRead);
writeRequest = new UsbRequest(); writeRequest = new UsbRequest();
writeRequest.initialize(connectionWrite, usbEndpointWrite); writeRequest.initialize(usbDeviceConnection, usbEndpointWrite);
inTalkStream = new TalkInputStream(); inTalkStream = new TalkInputStream();
outTalkStream = new TalkOutputStream(); outTalkStream = new TalkOutputStream();
@ -393,7 +346,7 @@ public class HidUAVTalk extends TelemetryTask {
int dataSize; int dataSize;
// wait for status event // wait for status event
if (connectionRead.requestWait() == readRequest) { if (usbDeviceConnection.requestWait() == readRequest) {
// Packet format: // Packet format:
// 0: Report ID (1) // 0: Report ID (1)
// 1: Number of valid bytes // 1: Number of valid bytes
@ -435,7 +388,7 @@ public class HidUAVTalk extends TelemetryTask {
writeRequest.queue(packet, bufferDataLength); writeRequest.queue(packet, bufferDataLength);
try try
{ {
if (!writeRequest.equals(connectionWrite.requestWait())) if (!writeRequest.equals(usbDeviceConnection.requestWait()))
Log.e(TAG, "writeRequest failed"); Log.e(TAG, "writeRequest failed");
} }
catch (Exception ex) catch (Exception ex)