Asynchronous write operation (tests needed)

pull/4/head
Felipe Herranz 2014-04-22 14:23:34 +02:00
rodzic 5ae3c05250
commit 1b4c2dac77
12 zmienionych plików z 74 dodań i 6 usunięć

Plik binarny nie jest wyświetlany.

Plik binarny nie jest wyświetlany.

Wyświetl plik

@ -55,6 +55,7 @@ public class BLED112SerialDevice extends UsbSerialDevice
{ {
// Restart the working thread if it has been killed before and get and claim interface // Restart the working thread if it has been killed before and get and claim interface
restartWorkingThread(); restartWorkingThread();
restartWriteThread();
mInterface = device.getInterface(1); // BLED112 Interface 0: Communications | Interface 1: CDC Data mInterface = device.getInterface(1); // BLED112 Interface 0: Communications | Interface 1: CDC Data
if(connection.claimInterface(mInterface, true)) if(connection.claimInterface(mInterface, true))
@ -88,15 +89,15 @@ public class BLED112SerialDevice extends UsbSerialDevice
requestIN = new UsbRequest(); requestIN = new UsbRequest();
requestIN.initialize(connection, inEndpoint); requestIN.initialize(connection, inEndpoint);
// Pass reference to the thread // Pass references to the threads
workerThread.setUsbRequest(requestIN); workerThread.setUsbRequest(requestIN);
writeThread.setUsbEndpoint(outEndpoint);
} }
@Override @Override
public void write(byte[] buffer) public void write(byte[] buffer)
{ {
connection.bulkTransfer(outEndpoint, buffer, buffer.length, USB_TIMEOUT); serialBuffer.putWriteBuffer(buffer);
} }
@Override @Override
@ -112,6 +113,7 @@ public class BLED112SerialDevice extends UsbSerialDevice
{ {
setControlCommand(BLED112_SET_CONTROL_LINE_STATE, BLED112_DISCONNECT_CONTROL_LINE , null); setControlCommand(BLED112_SET_CONTROL_LINE_STATE, BLED112_DISCONNECT_CONTROL_LINE , null);
killWorkingThread(); killWorkingThread();
killWriteThread();
connection.close(); connection.close();
} }

Wyświetl plik

@ -59,8 +59,9 @@ public class CP2102SerialDevice extends UsbSerialDevice
public void open() public void open()
{ {
// Restart the working thread if it has been killed before and get and claim interface // Restart the working thread and writeThread if it has been killed before and get and claim interface
restartWorkingThread(); restartWorkingThread();
restartWriteThread();
mInterface = device.getInterface(0); // CP2102 has only one interface mInterface = device.getInterface(0); // CP2102 has only one interface
if(connection.claimInterface(mInterface, true)) if(connection.claimInterface(mInterface, true))
@ -98,14 +99,15 @@ public class CP2102SerialDevice extends UsbSerialDevice
requestIN = new UsbRequest(); requestIN = new UsbRequest();
requestIN.initialize(connection, inEndpoint); requestIN.initialize(connection, inEndpoint);
// Pass references to the thread // Pass references to the threads
workerThread.setUsbRequest(requestIN); workerThread.setUsbRequest(requestIN);
writeThread.setUsbEndpoint(outEndpoint);
} }
@Override @Override
public void write(byte[] buffer) public void write(byte[] buffer)
{ {
connection.bulkTransfer(outEndpoint, buffer, buffer.length, USB_TIMEOUT); serialBuffer.putWriteBuffer(buffer);
} }
@Override @Override
@ -121,6 +123,7 @@ public class CP2102SerialDevice extends UsbSerialDevice
{ {
setControlCommand(CP210x_IFC_ENABLE, CP210x_UART_DISABLE, null); setControlCommand(CP210x_IFC_ENABLE, CP210x_UART_DISABLE, null);
killWorkingThread(); killWorkingThread();
killWriteThread();
connection.close(); connection.close();
} }

Wyświetl plik

@ -68,6 +68,11 @@ public class SerialBuffer
writeBuffer.put(data); writeBuffer.put(data);
} }
public void resetWriteBuffer()
{
writeBuffer.reset();
}
private class WriteBuffer private class WriteBuffer
{ {
private byte[] writeBuffer; private byte[] writeBuffer;
@ -104,6 +109,11 @@ public class SerialBuffer
position = -1; position = -1;
return dst; return dst;
} }
public synchronized void reset()
{
position = -1;
}
} }
} }

Wyświetl plik

@ -21,6 +21,7 @@ public abstract class UsbSerialDevice implements UsbSerialInterface
protected SerialBuffer serialBuffer; protected SerialBuffer serialBuffer;
protected WorkerThread workerThread; protected WorkerThread workerThread;
protected WriteThread writeThread;
public UsbSerialDevice(UsbDevice device, UsbDeviceConnection connection) public UsbSerialDevice(UsbDevice device, UsbDeviceConnection connection)
{ {
@ -28,7 +29,9 @@ public abstract class UsbSerialDevice implements UsbSerialInterface
this.connection = connection; this.connection = connection;
serialBuffer = new SerialBuffer(); serialBuffer = new SerialBuffer();
workerThread = new WorkerThread(); workerThread = new WorkerThread();
writeThread = new WriteThread();
workerThread.start(); workerThread.start();
writeThread.start();
} }
// Common Usb Serial Operations (I/O Asynchronous) // Common Usb Serial Operations (I/O Asynchronous)
@ -106,6 +109,37 @@ public abstract class UsbSerialDevice implements UsbSerialInterface
} }
protected class WriteThread extends Thread
{
private UsbEndpoint outEndpoint;
private AtomicBoolean working;
public WriteThread()
{
working = new AtomicBoolean(true);
}
@Override
public void run()
{
while(working.get())
{
byte[] data = serialBuffer.getWriteBuffer();
connection.bulkTransfer(outEndpoint, data, data.length, USB_TIMEOUT);
}
}
public void setUsbEndpoint(UsbEndpoint outEndpoint)
{
this.outEndpoint = outEndpoint;
}
public void stopWriteThread()
{
working.set(false);
}
}
/* /*
* Kill workingThread; This must be called when closing a device * Kill workingThread; This must be called when closing a device
*/ */
@ -129,4 +163,23 @@ public abstract class UsbSerialDevice implements UsbSerialInterface
workerThread.start(); workerThread.start();
} }
} }
protected void killWriteThread()
{
if(writeThread != null)
{
writeThread.stopWriteThread();
writeThread = null;
serialBuffer.resetWriteBuffer();
}
}
protected void restartWriteThread()
{
if(writeThread == null)
{
writeThread = new WriteThread();
writeThread.start();
}
}
} }