kopia lustrzana https://github.com/mik3y/usb-serial-for-android
iomanager with configurable threadpriority and higher default to prevent data loss
rodzic
f443d1f012
commit
80e8eb8a60
|
@ -14,6 +14,7 @@ import android.content.Context;
|
|||
import android.hardware.usb.UsbDevice;
|
||||
import android.hardware.usb.UsbDeviceConnection;
|
||||
import android.hardware.usb.UsbManager;
|
||||
import android.os.Process;
|
||||
import android.support.test.InstrumentationRegistry;
|
||||
import android.support.test.runner.AndroidJUnit4;
|
||||
import android.util.Log;
|
||||
|
@ -1025,6 +1026,8 @@ public class DeviceTest {
|
|||
usb.ioManager = new SerialInputOutputManager(usb.serialPort, usb);
|
||||
assertEquals(usb, usb.ioManager.getListener());
|
||||
|
||||
usb.ioManager.setThreadPriority(Process.THREAD_PRIORITY_AUDIO);
|
||||
|
||||
assertEquals(0, usb.ioManager.getReadTimeout());
|
||||
usb.ioManager.setReadTimeout(10);
|
||||
assertEquals(10, usb.ioManager.getReadTimeout());
|
||||
|
@ -1043,6 +1046,10 @@ public class DeviceTest {
|
|||
usb.setParameters(19200, 8, 1, UsbSerialPort.PARITY_NONE);
|
||||
telnet.setParameters(19200, 8, 1, UsbSerialPort.PARITY_NONE);
|
||||
usb.waitForIoManagerStarted();
|
||||
try {
|
||||
usb.ioManager.setThreadPriority(Process.THREAD_PRIORITY_LOWEST);
|
||||
fail("setThreadPriority IllegalStateException expected");
|
||||
} catch (IllegalStateException ignored) {}
|
||||
try {
|
||||
usb.ioManager.setReadTimeout(20);
|
||||
fail("setReadTimeout IllegalStateException expected");
|
||||
|
|
|
@ -35,7 +35,6 @@ public class UsbWrapper implements SerialInputOutputManager.Listener {
|
|||
|
||||
private final static int USB_READ_WAIT = 500;
|
||||
private final static int USB_WRITE_WAIT = 500;
|
||||
private final static Integer SERIAL_INPUT_OUTPUT_MANAGER_THREAD_PRIORITY = Process.THREAD_PRIORITY_URGENT_AUDIO;
|
||||
private static final String TAG = UsbWrapper.class.getSimpleName();
|
||||
|
||||
public enum OpenCloseFlags { NO_IOMANAGER_THREAD, NO_CONTROL_LINE_INIT, NO_DEVICE_CONNECTION };
|
||||
|
@ -157,14 +156,7 @@ public class UsbWrapper implements SerialInputOutputManager.Listener {
|
|||
serialPort.setRTS(true);
|
||||
}
|
||||
if(!flags.contains(OpenCloseFlags.NO_IOMANAGER_THREAD)) {
|
||||
ioManager = new SerialInputOutputManager(serialPort, this) {
|
||||
@Override
|
||||
public void run() {
|
||||
if (SERIAL_INPUT_OUTPUT_MANAGER_THREAD_PRIORITY != null)
|
||||
Process.setThreadPriority(SERIAL_INPUT_OUTPUT_MANAGER_THREAD_PRIORITY);
|
||||
super.run();
|
||||
}
|
||||
};
|
||||
ioManager = new SerialInputOutputManager(serialPort, this);
|
||||
ioManager.setReadTimeout(ioManagerTimeout);
|
||||
ioManager.setWriteTimeout(ioManagerTimeout);
|
||||
Executors.newSingleThreadExecutor().submit(ioManager);
|
||||
|
|
|
@ -6,6 +6,7 @@
|
|||
|
||||
package com.hoho.android.usbserial.util;
|
||||
|
||||
import android.os.Process;
|
||||
import android.util.Log;
|
||||
|
||||
import com.hoho.android.usbserial.driver.UsbSerialPort;
|
||||
|
@ -42,6 +43,7 @@ public class SerialInputOutputManager implements Runnable {
|
|||
STOPPING
|
||||
}
|
||||
|
||||
private int mThreadPriority = Process.THREAD_PRIORITY_URGENT_AUDIO;
|
||||
private State mState = State.STOPPED; // Synchronized by 'this'
|
||||
private Listener mListener; // Synchronized by 'this'
|
||||
private final UsbSerialPort mSerialPort;
|
||||
|
@ -75,6 +77,17 @@ public class SerialInputOutputManager implements Runnable {
|
|||
return mListener;
|
||||
}
|
||||
|
||||
/**
|
||||
* setThreadPriority. By default use higher priority than UI thread to prevent data loss
|
||||
*
|
||||
* @param threadPriority see {@link Process#setThreadPriority(int)}
|
||||
* */
|
||||
public void setThreadPriority(int threadPriority) {
|
||||
if (mState != State.STOPPED)
|
||||
throw new IllegalStateException("threadPriority only configurable before SerialInputOutputManager is started");
|
||||
mThreadPriority = threadPriority;
|
||||
}
|
||||
|
||||
/**
|
||||
* read/write timeout
|
||||
*/
|
||||
|
@ -154,6 +167,9 @@ public class SerialInputOutputManager implements Runnable {
|
|||
*/
|
||||
@Override
|
||||
public void run() {
|
||||
if(mThreadPriority != Process.THREAD_PRIORITY_DEFAULT)
|
||||
setThreadPriority(mThreadPriority);
|
||||
|
||||
synchronized (this) {
|
||||
if (getState() != State.STOPPED) {
|
||||
throw new IllegalStateException("Already running");
|
||||
|
|
Ładowanie…
Reference in New Issue