1
0
mirror of https://bitbucket.org/librepilot/librepilot.git synced 2025-02-05 21:52:10 +01:00

AndroidGCS HID: Go back to a read and write thread but now use synchronous

bultTransfer for write which gets rid of the segfaults with running two
asynchronous transfers.
This commit is contained in:
James Cotton 2012-08-13 01:07:09 -05:00
parent f012248fd1
commit daab45d14d

View File

@ -28,7 +28,7 @@ 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 final int LOGLEVEL = 0; public static final int LOGLEVEL = 1;
public static final boolean DEBUG = LOGLEVEL > 2; public static final boolean DEBUG = LOGLEVEL > 2;
public static final boolean WARN = LOGLEVEL > 1; public static final boolean WARN = LOGLEVEL > 1;
public static final boolean ERROR = LOGLEVEL > 0; public static final boolean ERROR = LOGLEVEL > 0;
@ -57,9 +57,10 @@ public class HidUAVTalk extends TelemetryTask {
private UsbInterface usbInterface = null; private UsbInterface usbInterface = null;
private TalkInputStream inTalkStream; private TalkInputStream inTalkStream;
private TalkOutputStream outTalkStream; private TalkOutputStream outTalkStream;
private UsbRequest writeRequest = null; private final UsbRequest writeRequest = null;
private UsbRequest readRequest = null; private UsbRequest readRequest = null;
private Thread readWriteThread; private Thread readThread;
private Thread writeThread;
private boolean readPending = false; private boolean readPending = false;
private boolean writePending = false; private boolean writePending = false;
@ -78,9 +79,15 @@ public class HidUAVTalk extends TelemetryTask {
super.disconnect(); super.disconnect();
try { try {
if(readWriteThread != null) { if(readThread != null) {
readWriteThread.join(); readThread.interrupt(); // Make sure not blocking for data
readWriteThread = null; readThread.join();
readThread = null;
}
if(writeThread != null) {
writeThread.interrupt();
writeThread.join();
writeThread = null;
} }
} catch (InterruptedException e) { } catch (InterruptedException e) {
e.printStackTrace(); e.printStackTrace();
@ -91,13 +98,6 @@ public class HidUAVTalk extends TelemetryTask {
readRequest.close(); readRequest.close();
readRequest = null; readRequest = null;
} }
if (writeRequest != null) {
writeRequest.cancel();
writeRequest.close();
writeRequest = null;
}
} }
@Override @Override
@ -293,9 +293,6 @@ public class HidUAVTalk extends TelemetryTask {
readRequest = new UsbRequest(); readRequest = new UsbRequest();
readRequest.initialize(usbDeviceConnection, usbEndpointRead); readRequest.initialize(usbDeviceConnection, usbEndpointRead);
writeRequest = new UsbRequest();
writeRequest.initialize(usbDeviceConnection, usbEndpointWrite);
inTalkStream = new TalkInputStream(); inTalkStream = new TalkInputStream();
outTalkStream = new TalkOutputStream(); outTalkStream = new TalkOutputStream();
inStream = inTalkStream; inStream = inTalkStream;
@ -307,31 +304,39 @@ public class HidUAVTalk extends TelemetryTask {
} }
}); });
readWriteThread = new Thread(new Runnable() { readThread = new Thread(new Runnable() {
@Override @Override
public void run() { public void run() {
// Enqueue the first read // Enqueue the first read
queueRead(); queueRead();
while (!shutdown) { while (!shutdown) {
// If there are no request
// Enqueue requests appropriately first
UsbRequest returned = usbDeviceConnection.requestWait(); UsbRequest returned = usbDeviceConnection.requestWait();
if (returned == readRequest) { if (returned == readRequest) {
if (DEBUG) Log.d(TAG, "Received read request"); if (DEBUG) Log.d(TAG, "Received read request");
readData(); readData();
} else if (returned == writeRequest) { } else
if (DEBUG) Log.d(TAG, "Received write completed request"); Log.e(TAG, "Received unknown USB response");
writePending = false;
sendData();
}
} }
} }
}, "HID Read Write"); }, "HID Read");
readWriteThread.start(); readThread.start();
writeThread = new Thread(new Runnable() {
@Override
public void run() {
if (DEBUG) Log.d(TAG, "Starting HID write thread");
while(!shutdown) {
try {
sendDataSynchronous();
} catch (InterruptedException e) {
break;
}
}
if (DEBUG) Log.d(TAG, "Ending HID write thread");
}
}, "HID Write");
writeThread.start();
return true; return true;
} }
@ -402,6 +407,7 @@ public class HidUAVTalk extends TelemetryTask {
} }
} }
/** /**
* Send a packet if data is available * Send a packet if data is available
*/ */
@ -426,6 +432,21 @@ public class HidUAVTalk extends TelemetryTask {
} }
} }
/**
* Send a packet if data is available
* @throws InterruptedException
*/
public void sendDataSynchronous() throws InterruptedException {
ByteBuffer packet = outTalkStream.getHIDpacketBlocking();
if (packet != null) {
if (DEBUG) Log.d(TAG, "sendDataSynchronous() Writing to device()");
if (usbDeviceConnection.bulkTransfer(usbEndpointWrite, packet.array(), MAX_HID_PACKET_SIZE, 1000) < 0)
Log.e(TAG, "Failed to perform bult write");
}
}
/*********** Helper classes for telemetry streams ************/ /*********** Helper classes for telemetry streams ************/
class TalkOutputStream extends OutputStream { class TalkOutputStream extends OutputStream {
@ -433,6 +454,20 @@ public class HidUAVTalk extends TelemetryTask {
// and ByteFifo.put(byte []) // and ByteFifo.put(byte [])
ByteFifo data = new ByteFifo(); ByteFifo data = new ByteFifo();
/**
* Blocks until data is available and then returns a properly formatted HID packet
*/
public ByteBuffer getHIDpacketBlocking() throws InterruptedException {
synchronized(data) {
if (data.remaining() == 0)
data.wait();
return getHIDpacket();
}
}
/**
* Gets data from the ByteFifo in a properly formatted HID packet
*/
public ByteBuffer getHIDpacket() { public ByteBuffer getHIDpacket() {
ByteBuffer packet = null; ByteBuffer packet = null;
synchronized(data) { synchronized(data) {
@ -461,9 +496,6 @@ public class HidUAVTalk extends TelemetryTask {
data.put((byte) oneByte); data.put((byte) oneByte);
data.notify(); data.notify();
} }
// If there is not a write request queued, add one
sendData();
} }
@Override @Override
@ -475,8 +507,6 @@ public class HidUAVTalk extends TelemetryTask {
data.put(b); data.put(b);
data.notify(); data.notify();
} }
sendData();
} }
}; };