Added safeguards to not synchronously write or read when using asynchronous api and viceversa

pull/29/head
Felipe Herranz 2016-02-21 17:52:31 +01:00
rodzic c686d79383
commit c8cda2f6b2
6 zmienionych plików z 59 dodań i 19 usunięć

Wyświetl plik

@ -74,6 +74,8 @@ public class CDCSerialDevice extends UsbSerialDevice
// Pass references to the threads
setThreadsParams(requestIN, outEndpoint);
asyncMode = true;
return true;
}else
{
@ -98,6 +100,7 @@ public class CDCSerialDevice extends UsbSerialDevice
if(ret)
{
setSyncParams(inEndpoint, outEndpoint);
asyncMode = false;
return true;
}else
{

Wyświetl plik

@ -84,6 +84,8 @@ public class CH34xSerialDevice extends UsbSerialDevice
// Pass references to the threads
setThreadsParams(requestIN, outEndpoint);
asyncMode = true;
return true;
}else
{
@ -107,6 +109,7 @@ public class CH34xSerialDevice extends UsbSerialDevice
{
setBaudRate(DEFAULT_BAUDRATE);
setSyncParams(inEndpoint, outEndpoint);
asyncMode = false;
return true;
}else
{

Wyświetl plik

@ -108,6 +108,8 @@ public class CP2102SerialDevice extends UsbSerialDevice
// Pass references to the threads
setThreadsParams(requestIN, outEndpoint);
asyncMode = true;
return true;
}else
{
@ -134,6 +136,7 @@ public class CP2102SerialDevice extends UsbSerialDevice
// Create Flow control thread but it will only be started if necessary
createFlowControlThread();
setSyncParams(inEndpoint, outEndpoint);
asyncMode = false;
return true;
}else
{

Wyświetl plik

@ -120,6 +120,8 @@ public class FTDISerialDevice extends UsbSerialDevice
// Pass references to the threads
setThreadsParams(requestIN, outEndpoint);
asyncMode = true;
return true;
}else
{
@ -145,6 +147,7 @@ public class FTDISerialDevice extends UsbSerialDevice
if(ret)
{
setSyncParams(inEndpoint, outEndpoint);
asyncMode = false;
return true;
}else
{

Wyświetl plik

@ -72,6 +72,8 @@ public class PL2303SerialDevice extends UsbSerialDevice
// Pass references to the threads
setThreadsParams(requestIN, outEndpoint);
asyncMode = true;
return true;
}else
{
@ -94,6 +96,7 @@ public class PL2303SerialDevice extends UsbSerialDevice
if(ret)
{
setSyncParams(inEndpoint, outEndpoint);
asyncMode = false;
return true;
}else
{

Wyświetl plik

@ -34,6 +34,8 @@ public abstract class UsbSerialDevice implements UsbSerialInterface
private UsbEndpoint inEndpoint;
private UsbEndpoint outEndpoint;
protected boolean asyncMode;
// Get Android version if version < 4.3 It is not going to be asynchronous read operations
static
{
@ -47,6 +49,7 @@ public abstract class UsbSerialDevice implements UsbSerialInterface
{
this.device = device;
this.connection = connection;
this.asyncMode = true;
serialBuffer = new SerialBuffer(mr1Version);
}
@ -86,30 +89,16 @@ public abstract class UsbSerialDevice implements UsbSerialInterface
@Override
public void write(byte[] buffer)
{
serialBuffer.putWriteBuffer(buffer);
}
// Common Usb Serial Operations (I/O Synchronous)
@Override
public int syncWrite(byte[] buffer, int timeout)
{
if(buffer == null)
return 0;
return connection.bulkTransfer(outEndpoint, buffer, buffer.length, timeout);
}
@Override
public int syncRead(byte[] buffer, int timeout)
{
if(buffer == null)
return 0;
return connection.bulkTransfer(inEndpoint, buffer, buffer.length, timeout);
if(asyncMode)
serialBuffer.putWriteBuffer(buffer);
}
@Override
public int read(UsbReadCallback mCallback)
{
if(!asyncMode)
return -1;
if(mr1Version)
{
workerThread.setCallback(mCallback);
@ -126,6 +115,42 @@ public abstract class UsbSerialDevice implements UsbSerialInterface
@Override
public abstract void close();
// Common Usb Serial Operations (I/O Synchronous)
@Override
public abstract boolean syncOpen();
@Override
public abstract void syncClose();
@Override
public int syncWrite(byte[] buffer, int timeout)
{
if(!asyncMode)
{
if(buffer == null)
return 0;
return connection.bulkTransfer(outEndpoint, buffer, buffer.length, timeout);
}else
{
return -1;
}
}
@Override
public int syncRead(byte[] buffer, int timeout)
{
if(!asyncMode)
{
if(buffer == null)
return 0;
return connection.bulkTransfer(inEndpoint, buffer, buffer.length, timeout);
}else
{
return -1;
}
}
// Serial port configuration
@Override
public abstract void setBaudRate(int baudRate);