kopia lustrzana https://github.com/felHR85/UsbSerial
Merge branch 'master' into okio
commit
3ad63c9311
|
@ -12,6 +12,7 @@ import android.hardware.usb.UsbManager;
|
||||||
import android.os.Binder;
|
import android.os.Binder;
|
||||||
import android.os.Handler;
|
import android.os.Handler;
|
||||||
import android.os.IBinder;
|
import android.os.IBinder;
|
||||||
|
import android.util.Log;
|
||||||
|
|
||||||
import com.felhr.usbserial.CDCSerialDevice;
|
import com.felhr.usbserial.CDCSerialDevice;
|
||||||
import com.felhr.usbserial.UsbSerialDevice;
|
import com.felhr.usbserial.UsbSerialDevice;
|
||||||
|
@ -23,6 +24,8 @@ import java.util.Map;
|
||||||
|
|
||||||
public class UsbService extends Service {
|
public class UsbService extends Service {
|
||||||
|
|
||||||
|
public static final String TAG = "UsbService";
|
||||||
|
|
||||||
public static final String ACTION_USB_READY = "com.felhr.connectivityservices.USB_READY";
|
public static final String ACTION_USB_READY = "com.felhr.connectivityservices.USB_READY";
|
||||||
public static final String ACTION_USB_ATTACHED = "android.hardware.usb.action.USB_DEVICE_ATTACHED";
|
public static final String ACTION_USB_ATTACHED = "android.hardware.usb.action.USB_DEVICE_ATTACHED";
|
||||||
public static final String ACTION_USB_DETACHED = "android.hardware.usb.action.USB_DEVICE_DETACHED";
|
public static final String ACTION_USB_DETACHED = "android.hardware.usb.action.USB_DEVICE_DETACHED";
|
||||||
|
@ -155,6 +158,8 @@ public class UsbService extends Service {
|
||||||
@Override
|
@Override
|
||||||
public void onDestroy() {
|
public void onDestroy() {
|
||||||
super.onDestroy();
|
super.onDestroy();
|
||||||
|
serialPort.close();
|
||||||
|
unregisterReceiver(usbReceiver);
|
||||||
UsbService.SERVICE_CONNECTED = false;
|
UsbService.SERVICE_CONNECTED = false;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -174,31 +179,39 @@ public class UsbService extends Service {
|
||||||
// This snippet will try to open the first encountered usb device connected, excluding usb root hubs
|
// This snippet will try to open the first encountered usb device connected, excluding usb root hubs
|
||||||
HashMap<String, UsbDevice> usbDevices = usbManager.getDeviceList();
|
HashMap<String, UsbDevice> usbDevices = usbManager.getDeviceList();
|
||||||
if (!usbDevices.isEmpty()) {
|
if (!usbDevices.isEmpty()) {
|
||||||
boolean keep = true;
|
|
||||||
|
// first, dump the hashmap for diagnostic purposes
|
||||||
|
for (Map.Entry<String, UsbDevice> entry : usbDevices.entrySet()) {
|
||||||
|
device = entry.getValue();
|
||||||
|
Log.d(TAG, String.format("USBDevice.HashMap (vid:pid) (%X:%X)-%b class:%X:%X name:%s",
|
||||||
|
device.getVendorId(), device.getProductId(),
|
||||||
|
UsbSerialDevice.isSupported(device),
|
||||||
|
device.getDeviceClass(), device.getDeviceSubclass(),
|
||||||
|
device.getDeviceName()));
|
||||||
|
}
|
||||||
|
|
||||||
for (Map.Entry<String, UsbDevice> entry : usbDevices.entrySet()) {
|
for (Map.Entry<String, UsbDevice> entry : usbDevices.entrySet()) {
|
||||||
device = entry.getValue();
|
device = entry.getValue();
|
||||||
int deviceVID = device.getVendorId();
|
int deviceVID = device.getVendorId();
|
||||||
int devicePID = device.getProductId();
|
int devicePID = device.getProductId();
|
||||||
|
|
||||||
if (deviceVID != 0x1d6b && (devicePID != 0x0001 && devicePID != 0x0002 && devicePID != 0x0003) && deviceVID != 0x5c6 && devicePID != 0x904c) {
|
// if (deviceVID != 0x1d6b && (devicePID != 0x0001 && devicePID != 0x0002 && devicePID != 0x0003) && deviceVID != 0x5c6 && devicePID != 0x904c) {
|
||||||
|
if (UsbSerialDevice.isSupported(device)) {
|
||||||
// There is a device connected to our Android device. Try to open it as a Serial Port.
|
// There is a supported device connected - request permission to access it.
|
||||||
requestUserPermission();
|
requestUserPermission();
|
||||||
keep = false;
|
break;
|
||||||
} else {
|
} else {
|
||||||
connection = null;
|
connection = null;
|
||||||
device = null;
|
device = null;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!keep)
|
|
||||||
break;
|
|
||||||
}
|
}
|
||||||
if (!keep) {
|
if (device==null) {
|
||||||
// There is no USB devices connected (but usb host were listed). Send an intent to MainActivity.
|
// There are no USB devices connected (but usb host were listed). Send an intent to MainActivity.
|
||||||
Intent intent = new Intent(ACTION_NO_USB);
|
Intent intent = new Intent(ACTION_NO_USB);
|
||||||
sendBroadcast(intent);
|
sendBroadcast(intent);
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
|
Log.d(TAG, "findSerialPortDevice() usbManager returned empty device list." );
|
||||||
// There is no USB devices connected. Send an intent to MainActivity
|
// There is no USB devices connected. Send an intent to MainActivity
|
||||||
Intent intent = new Intent(ACTION_NO_USB);
|
Intent intent = new Intent(ACTION_NO_USB);
|
||||||
sendBroadcast(intent);
|
sendBroadcast(intent);
|
||||||
|
@ -217,6 +230,7 @@ public class UsbService extends Service {
|
||||||
* Request user permission. The response will be received in the BroadcastReceiver
|
* Request user permission. The response will be received in the BroadcastReceiver
|
||||||
*/
|
*/
|
||||||
private void requestUserPermission() {
|
private void requestUserPermission() {
|
||||||
|
Log.d(TAG, String.format("requestUserPermission(%X:%X)", device.getVendorId(), device.getProductId() ) );
|
||||||
PendingIntent mPendingIntent = PendingIntent.getBroadcast(this, 0, new Intent(ACTION_USB_PERMISSION), 0);
|
PendingIntent mPendingIntent = PendingIntent.getBroadcast(this, 0, new Intent(ACTION_USB_PERMISSION), 0);
|
||||||
usbManager.requestPermission(device, mPendingIntent);
|
usbManager.requestPermission(device, mPendingIntent);
|
||||||
}
|
}
|
||||||
|
|
|
@ -12,6 +12,7 @@ import android.hardware.usb.UsbManager;
|
||||||
import android.os.Binder;
|
import android.os.Binder;
|
||||||
import android.os.Handler;
|
import android.os.Handler;
|
||||||
import android.os.IBinder;
|
import android.os.IBinder;
|
||||||
|
import android.util.Log;
|
||||||
|
|
||||||
import com.felhr.usbserial.CDCSerialDevice;
|
import com.felhr.usbserial.CDCSerialDevice;
|
||||||
import com.felhr.usbserial.SerialInputStream;
|
import com.felhr.usbserial.SerialInputStream;
|
||||||
|
@ -25,6 +26,8 @@ import java.util.concurrent.atomic.AtomicBoolean;
|
||||||
|
|
||||||
public class UsbService extends Service {
|
public class UsbService extends Service {
|
||||||
|
|
||||||
|
public static final String TAG = "UsbService";
|
||||||
|
|
||||||
public static final String ACTION_USB_READY = "com.felhr.connectivityservices.USB_READY";
|
public static final String ACTION_USB_READY = "com.felhr.connectivityservices.USB_READY";
|
||||||
public static final String ACTION_USB_ATTACHED = "android.hardware.usb.action.USB_DEVICE_ATTACHED";
|
public static final String ACTION_USB_ATTACHED = "android.hardware.usb.action.USB_DEVICE_ATTACHED";
|
||||||
public static final String ACTION_USB_DETACHED = "android.hardware.usb.action.USB_DEVICE_DETACHED";
|
public static final String ACTION_USB_DETACHED = "android.hardware.usb.action.USB_DEVICE_DETACHED";
|
||||||
|
@ -126,6 +129,8 @@ public class UsbService extends Service {
|
||||||
@Override
|
@Override
|
||||||
public void onDestroy() {
|
public void onDestroy() {
|
||||||
super.onDestroy();
|
super.onDestroy();
|
||||||
|
serialPort.close();
|
||||||
|
unregisterReceiver(usbReceiver);
|
||||||
UsbService.SERVICE_CONNECTED = false;
|
UsbService.SERVICE_CONNECTED = false;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -154,30 +159,39 @@ public class UsbService extends Service {
|
||||||
// This snippet will try to open the first encountered usb device connected, excluding usb root hubs
|
// This snippet will try to open the first encountered usb device connected, excluding usb root hubs
|
||||||
HashMap<String, UsbDevice> usbDevices = usbManager.getDeviceList();
|
HashMap<String, UsbDevice> usbDevices = usbManager.getDeviceList();
|
||||||
if (!usbDevices.isEmpty()) {
|
if (!usbDevices.isEmpty()) {
|
||||||
boolean keep = true;
|
|
||||||
|
// first, dump the map for diagnostic purposes
|
||||||
|
for (Map.Entry<String, UsbDevice> entry : usbDevices.entrySet()) {
|
||||||
|
device = entry.getValue();
|
||||||
|
Log.d(TAG, String.format("USBDevice.HashMap (vid:pid) (%X:%X)-%b class:%X:%X name:%s",
|
||||||
|
device.getVendorId(), device.getProductId(),
|
||||||
|
UsbSerialDevice.isSupported(device),
|
||||||
|
device.getDeviceClass(), device.getDeviceSubclass(),
|
||||||
|
device.getDeviceName()));
|
||||||
|
}
|
||||||
|
|
||||||
for (Map.Entry<String, UsbDevice> entry : usbDevices.entrySet()) {
|
for (Map.Entry<String, UsbDevice> entry : usbDevices.entrySet()) {
|
||||||
device = entry.getValue();
|
device = entry.getValue();
|
||||||
int deviceVID = device.getVendorId();
|
int deviceVID = device.getVendorId();
|
||||||
int devicePID = device.getProductId();
|
int devicePID = device.getProductId();
|
||||||
|
|
||||||
if (deviceVID != 0x1d6b && (devicePID != 0x0001 && devicePID != 0x0002 && devicePID != 0x0003)) {
|
// if (deviceVID != 0x1d6b && (devicePID != 0x0001 && devicePID != 0x0002 && devicePID != 0x0003)) {
|
||||||
|
if (UsbSerialDevice.isSupported(device)) {
|
||||||
// There is a device connected to our Android device. Try to open it as a Serial Port.
|
// There is a device connected to our Android device. Try to open it as a Serial Port.
|
||||||
requestUserPermission();
|
requestUserPermission();
|
||||||
keep = false;
|
break;
|
||||||
} else {
|
} else {
|
||||||
connection = null;
|
connection = null;
|
||||||
device = null;
|
device = null;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!keep)
|
|
||||||
break;
|
|
||||||
}
|
}
|
||||||
if (!keep) {
|
if (device==null) {
|
||||||
// There is no USB devices connected (but usb host were listed). Send an intent to MainActivity.
|
// There are no USB devices connected (but usb host were listed). Send an intent to MainActivity.
|
||||||
Intent intent = new Intent(ACTION_NO_USB);
|
Intent intent = new Intent(ACTION_NO_USB);
|
||||||
sendBroadcast(intent);
|
sendBroadcast(intent);
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
|
Log.d(TAG, "findSerialPortDevice() usbManager returned empty device list." );
|
||||||
// There is no USB devices connected. Send an intent to MainActivity
|
// There is no USB devices connected. Send an intent to MainActivity
|
||||||
Intent intent = new Intent(ACTION_NO_USB);
|
Intent intent = new Intent(ACTION_NO_USB);
|
||||||
sendBroadcast(intent);
|
sendBroadcast(intent);
|
||||||
|
@ -196,6 +210,7 @@ public class UsbService extends Service {
|
||||||
* Request user permission. The response will be received in the BroadcastReceiver
|
* Request user permission. The response will be received in the BroadcastReceiver
|
||||||
*/
|
*/
|
||||||
private void requestUserPermission() {
|
private void requestUserPermission() {
|
||||||
|
Log.d(TAG, String.format("requestUserPermission(%X:%X)", device.getVendorId(), device.getProductId() ) );
|
||||||
PendingIntent mPendingIntent = PendingIntent.getBroadcast(this, 0, new Intent(ACTION_USB_PERMISSION), 0);
|
PendingIntent mPendingIntent = PendingIntent.getBroadcast(this, 0, new Intent(ACTION_USB_PERMISSION), 0);
|
||||||
usbManager.requestPermission(device, mPendingIntent);
|
usbManager.requestPermission(device, mPendingIntent);
|
||||||
}
|
}
|
||||||
|
|
|
@ -12,6 +12,7 @@ import android.hardware.usb.UsbManager;
|
||||||
import android.os.Binder;
|
import android.os.Binder;
|
||||||
import android.os.Handler;
|
import android.os.Handler;
|
||||||
import android.os.IBinder;
|
import android.os.IBinder;
|
||||||
|
import android.util.Log;
|
||||||
|
|
||||||
import com.felhr.usbserial.CDCSerialDevice;
|
import com.felhr.usbserial.CDCSerialDevice;
|
||||||
import com.felhr.usbserial.UsbSerialDevice;
|
import com.felhr.usbserial.UsbSerialDevice;
|
||||||
|
@ -23,6 +24,8 @@ import java.util.Map;
|
||||||
|
|
||||||
public class UsbService extends Service {
|
public class UsbService extends Service {
|
||||||
|
|
||||||
|
public static final String TAG = "UsbService";
|
||||||
|
|
||||||
public static final String ACTION_USB_READY = "com.felhr.connectivityservices.USB_READY";
|
public static final String ACTION_USB_READY = "com.felhr.connectivityservices.USB_READY";
|
||||||
public static final String ACTION_USB_ATTACHED = "android.hardware.usb.action.USB_DEVICE_ATTACHED";
|
public static final String ACTION_USB_ATTACHED = "android.hardware.usb.action.USB_DEVICE_ATTACHED";
|
||||||
public static final String ACTION_USB_DETACHED = "android.hardware.usb.action.USB_DEVICE_DETACHED";
|
public static final String ACTION_USB_DETACHED = "android.hardware.usb.action.USB_DEVICE_DETACHED";
|
||||||
|
@ -156,6 +159,8 @@ public class UsbService extends Service {
|
||||||
@Override
|
@Override
|
||||||
public void onDestroy() {
|
public void onDestroy() {
|
||||||
super.onDestroy();
|
super.onDestroy();
|
||||||
|
serialPort.close();
|
||||||
|
unregisterReceiver(usbReceiver);
|
||||||
UsbService.SERVICE_CONNECTED = false;
|
UsbService.SERVICE_CONNECTED = false;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -184,30 +189,39 @@ public class UsbService extends Service {
|
||||||
// This snippet will try to open the first encountered usb device connected, excluding usb root hubs
|
// This snippet will try to open the first encountered usb device connected, excluding usb root hubs
|
||||||
HashMap<String, UsbDevice> usbDevices = usbManager.getDeviceList();
|
HashMap<String, UsbDevice> usbDevices = usbManager.getDeviceList();
|
||||||
if (!usbDevices.isEmpty()) {
|
if (!usbDevices.isEmpty()) {
|
||||||
boolean keep = true;
|
|
||||||
|
// first, dump the map for diagnostic purposes
|
||||||
|
for (Map.Entry<String, UsbDevice> entry : usbDevices.entrySet()) {
|
||||||
|
device = entry.getValue();
|
||||||
|
Log.d(TAG, String.format("USBDevice.HashMap (vid:pid) (%X:%X)-%b class:%X:%X name:%s",
|
||||||
|
device.getVendorId(), device.getProductId(),
|
||||||
|
UsbSerialDevice.isSupported(device),
|
||||||
|
device.getDeviceClass(), device.getDeviceSubclass(),
|
||||||
|
device.getDeviceName()));
|
||||||
|
}
|
||||||
|
|
||||||
for (Map.Entry<String, UsbDevice> entry : usbDevices.entrySet()) {
|
for (Map.Entry<String, UsbDevice> entry : usbDevices.entrySet()) {
|
||||||
device = entry.getValue();
|
device = entry.getValue();
|
||||||
int deviceVID = device.getVendorId();
|
int deviceVID = device.getVendorId();
|
||||||
int devicePID = device.getProductId();
|
int devicePID = device.getProductId();
|
||||||
|
|
||||||
if (deviceVID != 0x1d6b && (devicePID != 0x0001 && devicePID != 0x0002 && devicePID != 0x0003)) {
|
// if (deviceVID != 0x1d6b && (devicePID != 0x0001 && devicePID != 0x0002 && devicePID != 0x0003)) {
|
||||||
|
if (UsbSerialDevice.isSupported(device)) {
|
||||||
// There is a device connected to our Android device. Try to open it as a Serial Port.
|
// There is a device connected to our Android device. Try to open it as a Serial Port.
|
||||||
requestUserPermission();
|
requestUserPermission();
|
||||||
keep = false;
|
break;
|
||||||
} else {
|
} else {
|
||||||
connection = null;
|
connection = null;
|
||||||
device = null;
|
device = null;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!keep)
|
|
||||||
break;
|
|
||||||
}
|
}
|
||||||
if (!keep) {
|
if (device==null) {
|
||||||
// There is no USB devices connected (but usb host were listed). Send an intent to MainActivity.
|
// There is no USB devices connected (but usb host were listed). Send an intent to MainActivity.
|
||||||
Intent intent = new Intent(ACTION_NO_USB);
|
Intent intent = new Intent(ACTION_NO_USB);
|
||||||
sendBroadcast(intent);
|
sendBroadcast(intent);
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
|
Log.d(TAG, "findSerialPortDevice() usbManager returned empty device list." );
|
||||||
// There is no USB devices connected. Send an intent to MainActivity
|
// There is no USB devices connected. Send an intent to MainActivity
|
||||||
Intent intent = new Intent(ACTION_NO_USB);
|
Intent intent = new Intent(ACTION_NO_USB);
|
||||||
sendBroadcast(intent);
|
sendBroadcast(intent);
|
||||||
|
@ -226,6 +240,7 @@ public class UsbService extends Service {
|
||||||
* Request user permission. The response will be received in the BroadcastReceiver
|
* Request user permission. The response will be received in the BroadcastReceiver
|
||||||
*/
|
*/
|
||||||
private void requestUserPermission() {
|
private void requestUserPermission() {
|
||||||
|
Log.d(TAG, String.format("requestUserPermission(%X:%X)", device.getVendorId(), device.getProductId() ) );
|
||||||
PendingIntent mPendingIntent = PendingIntent.getBroadcast(this, 0, new Intent(ACTION_USB_PERMISSION), 0);
|
PendingIntent mPendingIntent = PendingIntent.getBroadcast(this, 0, new Intent(ACTION_USB_PERMISSION), 0);
|
||||||
usbManager.requestPermission(device, mPendingIntent);
|
usbManager.requestPermission(device, mPendingIntent);
|
||||||
}
|
}
|
||||||
|
|
|
@ -0,0 +1,18 @@
|
||||||
|
package com.felhr.usbserial;
|
||||||
|
|
||||||
|
abstract class AbstractWorkerThread extends Thread {
|
||||||
|
boolean firstTime = true;
|
||||||
|
private volatile boolean keep = true;
|
||||||
|
|
||||||
|
void stopThread() {
|
||||||
|
keep = false;
|
||||||
|
}
|
||||||
|
|
||||||
|
public final void run() {
|
||||||
|
while (keep) {
|
||||||
|
doRun();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
abstract void doRun();
|
||||||
|
}
|
|
@ -41,10 +41,9 @@ public class BLED112SerialDevice extends UsbSerialDevice
|
||||||
private static final int BLED112_DEFAULT_CONTROL_LINE = 0x0003;
|
private static final int BLED112_DEFAULT_CONTROL_LINE = 0x0003;
|
||||||
private static final int BLED112_DISCONNECT_CONTROL_LINE = 0x0002;
|
private static final int BLED112_DISCONNECT_CONTROL_LINE = 0x0002;
|
||||||
|
|
||||||
private UsbInterface mInterface;
|
private final UsbInterface mInterface;
|
||||||
private UsbEndpoint inEndpoint;
|
private UsbEndpoint inEndpoint;
|
||||||
private UsbEndpoint outEndpoint;
|
private UsbEndpoint outEndpoint;
|
||||||
private UsbRequest requestIN;
|
|
||||||
|
|
||||||
@Deprecated
|
@Deprecated
|
||||||
public BLED112SerialDevice(UsbDevice device, UsbDeviceConnection connection)
|
public BLED112SerialDevice(UsbDevice device, UsbDeviceConnection connection)
|
||||||
|
@ -88,7 +87,7 @@ public class BLED112SerialDevice extends UsbSerialDevice
|
||||||
setControlCommand(BLED112_SET_CONTROL_LINE_STATE, BLED112_DEFAULT_CONTROL_LINE, null);
|
setControlCommand(BLED112_SET_CONTROL_LINE_STATE, BLED112_DEFAULT_CONTROL_LINE, null);
|
||||||
|
|
||||||
// Initialize UsbRequest
|
// Initialize UsbRequest
|
||||||
requestIN = new UsbRequest();
|
UsbRequest requestIN = new UsbRequest();
|
||||||
requestIN.initialize(connection, inEndpoint);
|
requestIN.initialize(connection, inEndpoint);
|
||||||
|
|
||||||
// Pass references to the threads
|
// Pass references to the threads
|
||||||
|
|
|
@ -46,10 +46,9 @@ public class CDCSerialDevice extends UsbSerialDevice
|
||||||
private static final int CDC_CONTROL_LINE_ON = 0x0003;
|
private static final int CDC_CONTROL_LINE_ON = 0x0003;
|
||||||
private static final int CDC_CONTROL_LINE_OFF = 0x0000;
|
private static final int CDC_CONTROL_LINE_OFF = 0x0000;
|
||||||
|
|
||||||
private UsbInterface mInterface;
|
private final UsbInterface mInterface;
|
||||||
private UsbEndpoint inEndpoint;
|
private UsbEndpoint inEndpoint;
|
||||||
private UsbEndpoint outEndpoint;
|
private UsbEndpoint outEndpoint;
|
||||||
private UsbRequest requestIN;
|
|
||||||
|
|
||||||
private int initialBaudRate = 0;
|
private int initialBaudRate = 0;
|
||||||
|
|
||||||
|
@ -84,7 +83,7 @@ public class CDCSerialDevice extends UsbSerialDevice
|
||||||
if(ret)
|
if(ret)
|
||||||
{
|
{
|
||||||
// Initialize UsbRequest
|
// Initialize UsbRequest
|
||||||
requestIN = new SafeUsbRequest();
|
UsbRequest requestIN = new SafeUsbRequest();
|
||||||
requestIN.initialize(connection, inEndpoint);
|
requestIN.initialize(connection, inEndpoint);
|
||||||
|
|
||||||
// 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
|
||||||
|
|
|
@ -16,8 +16,6 @@ import android.util.Log;
|
||||||
|
|
||||||
import com.felhr.utils.SafeUsbRequest;
|
import com.felhr.utils.SafeUsbRequest;
|
||||||
|
|
||||||
import java.util.concurrent.atomic.AtomicBoolean;
|
|
||||||
|
|
||||||
public class CH34xSerialDevice extends UsbSerialDevice
|
public class CH34xSerialDevice extends UsbSerialDevice
|
||||||
{
|
{
|
||||||
private static final String CLASS_ID = CH34xSerialDevice.class.getSimpleName();
|
private static final String CLASS_ID = CH34xSerialDevice.class.getSimpleName();
|
||||||
|
@ -85,7 +83,6 @@ public class CH34xSerialDevice extends UsbSerialDevice
|
||||||
private UsbInterface mInterface;
|
private UsbInterface mInterface;
|
||||||
private UsbEndpoint inEndpoint;
|
private UsbEndpoint inEndpoint;
|
||||||
private UsbEndpoint outEndpoint;
|
private UsbEndpoint outEndpoint;
|
||||||
private UsbRequest requestIN;
|
|
||||||
|
|
||||||
private FlowControlThread flowControlThread;
|
private FlowControlThread flowControlThread;
|
||||||
private UsbCTSCallback ctsCallback;
|
private UsbCTSCallback ctsCallback;
|
||||||
|
@ -118,7 +115,7 @@ public class CH34xSerialDevice extends UsbSerialDevice
|
||||||
if(ret)
|
if(ret)
|
||||||
{
|
{
|
||||||
// Initialize UsbRequest
|
// Initialize UsbRequest
|
||||||
requestIN = new SafeUsbRequest();
|
UsbRequest requestIN = new SafeUsbRequest();
|
||||||
requestIN.initialize(connection, inEndpoint);
|
requestIN.initialize(connection, inEndpoint);
|
||||||
|
|
||||||
// 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
|
||||||
|
@ -609,24 +606,12 @@ public class CH34xSerialDevice extends UsbSerialDevice
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private class FlowControlThread extends Thread
|
private class FlowControlThread extends AbstractWorkerThread
|
||||||
{
|
{
|
||||||
private long time = 100; // 100ms
|
private final long time = 100; // 100ms
|
||||||
|
|
||||||
private boolean firstTime;
|
|
||||||
|
|
||||||
private AtomicBoolean keep;
|
|
||||||
|
|
||||||
public FlowControlThread()
|
|
||||||
{
|
|
||||||
keep = new AtomicBoolean(true);
|
|
||||||
firstTime = true;
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void run()
|
public void doRun()
|
||||||
{
|
|
||||||
while(keep.get())
|
|
||||||
{
|
{
|
||||||
if(!firstTime)
|
if(!firstTime)
|
||||||
{
|
{
|
||||||
|
@ -664,12 +649,6 @@ public class CH34xSerialDevice extends UsbSerialDevice
|
||||||
firstTime = false;
|
firstTime = false;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
public void stopThread()
|
|
||||||
{
|
|
||||||
keep.set(false);
|
|
||||||
}
|
|
||||||
|
|
||||||
public boolean pollForCTS()
|
public boolean pollForCTS()
|
||||||
{
|
{
|
||||||
|
|
|
@ -10,8 +10,6 @@ import android.util.Log;
|
||||||
|
|
||||||
import com.felhr.utils.SafeUsbRequest;
|
import com.felhr.utils.SafeUsbRequest;
|
||||||
|
|
||||||
import java.util.concurrent.atomic.AtomicBoolean;
|
|
||||||
|
|
||||||
public class CP2102SerialDevice extends UsbSerialDevice
|
public class CP2102SerialDevice extends UsbSerialDevice
|
||||||
{
|
{
|
||||||
private static final String CLASS_ID = CP2102SerialDevice.class.getSimpleName();
|
private static final String CLASS_ID = CP2102SerialDevice.class.getSimpleName();
|
||||||
|
@ -67,10 +65,9 @@ public class CP2102SerialDevice extends UsbSerialDevice
|
||||||
private UsbCTSCallback ctsCallback;
|
private UsbCTSCallback ctsCallback;
|
||||||
private UsbDSRCallback dsrCallback;
|
private UsbDSRCallback dsrCallback;
|
||||||
|
|
||||||
private UsbInterface mInterface;
|
private final UsbInterface mInterface;
|
||||||
private UsbEndpoint inEndpoint;
|
private UsbEndpoint inEndpoint;
|
||||||
private UsbEndpoint outEndpoint;
|
private UsbEndpoint outEndpoint;
|
||||||
private UsbRequest requestIN;
|
|
||||||
|
|
||||||
private FlowControlThread flowControlThread;
|
private FlowControlThread flowControlThread;
|
||||||
|
|
||||||
|
@ -103,7 +100,7 @@ public class CP2102SerialDevice extends UsbSerialDevice
|
||||||
if(ret)
|
if(ret)
|
||||||
{
|
{
|
||||||
// Initialize UsbRequest
|
// Initialize UsbRequest
|
||||||
requestIN = new SafeUsbRequest();
|
UsbRequest requestIN = new SafeUsbRequest();
|
||||||
requestIN.initialize(connection, inEndpoint);
|
requestIN.initialize(connection, inEndpoint);
|
||||||
|
|
||||||
// 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
|
||||||
|
@ -407,24 +404,12 @@ public class CP2102SerialDevice extends UsbSerialDevice
|
||||||
/*
|
/*
|
||||||
Thread to check every X time if flow signals CTS or DSR have been raised
|
Thread to check every X time if flow signals CTS or DSR have been raised
|
||||||
*/
|
*/
|
||||||
private class FlowControlThread extends Thread
|
private class FlowControlThread extends AbstractWorkerThread
|
||||||
{
|
{
|
||||||
private long time = 40; // 40ms
|
private final long time = 40; // 40ms
|
||||||
|
|
||||||
private boolean firstTime;
|
|
||||||
|
|
||||||
private AtomicBoolean keep;
|
|
||||||
|
|
||||||
public FlowControlThread()
|
|
||||||
{
|
|
||||||
keep = new AtomicBoolean(true);
|
|
||||||
firstTime = true;
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void run()
|
public void doRun()
|
||||||
{
|
|
||||||
while(keep.get())
|
|
||||||
{
|
{
|
||||||
if(!firstTime) // Only execute the callback when the status change
|
if(!firstTime) // Only execute the callback when the status change
|
||||||
{
|
{
|
||||||
|
@ -502,12 +487,6 @@ public class CP2102SerialDevice extends UsbSerialDevice
|
||||||
firstTime = false;
|
firstTime = false;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
public void stopThread()
|
|
||||||
{
|
|
||||||
keep.set(false);
|
|
||||||
}
|
|
||||||
|
|
||||||
private byte[] pollLines()
|
private byte[] pollLines()
|
||||||
{
|
{
|
||||||
|
|
|
@ -11,7 +11,7 @@ import android.util.Log;
|
||||||
|
|
||||||
public class CP2130SpiDevice extends UsbSpiDevice
|
public class CP2130SpiDevice extends UsbSpiDevice
|
||||||
{
|
{
|
||||||
private static String CLASS_ID = CP2130SpiDevice.class.getSimpleName();
|
private static final String CLASS_ID = CP2130SpiDevice.class.getSimpleName();
|
||||||
|
|
||||||
public static final int CLOCK_12MHz = 0;
|
public static final int CLOCK_12MHz = 0;
|
||||||
public static final int CLOCK_6MHz = 1;
|
public static final int CLOCK_6MHz = 1;
|
||||||
|
@ -29,7 +29,7 @@ public class CP2130SpiDevice extends UsbSpiDevice
|
||||||
private static final int SET_GPIO_CHIP_SELECT = 0x25;
|
private static final int SET_GPIO_CHIP_SELECT = 0x25;
|
||||||
private static final int GET_SPI_WORD = 0x30;
|
private static final int GET_SPI_WORD = 0x30;
|
||||||
|
|
||||||
private UsbInterface mInterface;
|
private final UsbInterface mInterface;
|
||||||
private UsbEndpoint inEndpoint;
|
private UsbEndpoint inEndpoint;
|
||||||
private UsbEndpoint outEndpoint;
|
private UsbEndpoint outEndpoint;
|
||||||
private UsbRequest requestIN;
|
private UsbRequest requestIN;
|
||||||
|
|
|
@ -79,10 +79,9 @@ public class FTDISerialDevice extends UsbSerialDevice
|
||||||
private UsbCTSCallback ctsCallback;
|
private UsbCTSCallback ctsCallback;
|
||||||
private UsbDSRCallback dsrCallback;
|
private UsbDSRCallback dsrCallback;
|
||||||
|
|
||||||
private UsbInterface mInterface;
|
private final UsbInterface mInterface;
|
||||||
private UsbEndpoint inEndpoint;
|
private UsbEndpoint inEndpoint;
|
||||||
private UsbEndpoint outEndpoint;
|
private UsbEndpoint outEndpoint;
|
||||||
private UsbRequest requestIN;
|
|
||||||
|
|
||||||
public FTDIUtilities ftdiUtilities;
|
public FTDIUtilities ftdiUtilities;
|
||||||
|
|
||||||
|
@ -117,7 +116,7 @@ public class FTDISerialDevice extends UsbSerialDevice
|
||||||
if(ret)
|
if(ret)
|
||||||
{
|
{
|
||||||
// Initialize UsbRequest
|
// Initialize UsbRequest
|
||||||
requestIN = new SafeUsbRequest();
|
UsbRequest requestIN = new SafeUsbRequest();
|
||||||
requestIN.initialize(connection, inEndpoint);
|
requestIN.initialize(connection, inEndpoint);
|
||||||
|
|
||||||
// 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
|
||||||
|
|
|
@ -22,7 +22,7 @@ public class PL2303SerialDevice extends UsbSerialDevice
|
||||||
private static final int PL2303_SET_LINE_CODING = 0x20;
|
private static final int PL2303_SET_LINE_CODING = 0x20;
|
||||||
private static final int PL2303_SET_CONTROL_REQUEST = 0x22;
|
private static final int PL2303_SET_CONTROL_REQUEST = 0x22;
|
||||||
|
|
||||||
private byte[] defaultSetLine = new byte[]{
|
private final byte[] defaultSetLine = new byte[]{
|
||||||
(byte) 0x80, // [0:3] Baud rate (reverse hex encoding 9600:00 00 25 80 -> 80 25 00 00)
|
(byte) 0x80, // [0:3] Baud rate (reverse hex encoding 9600:00 00 25 80 -> 80 25 00 00)
|
||||||
(byte) 0x25,
|
(byte) 0x25,
|
||||||
(byte) 0x00,
|
(byte) 0x00,
|
||||||
|
@ -33,10 +33,9 @@ public class PL2303SerialDevice extends UsbSerialDevice
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
||||||
private UsbInterface mInterface;
|
private final UsbInterface mInterface;
|
||||||
private UsbEndpoint inEndpoint;
|
private UsbEndpoint inEndpoint;
|
||||||
private UsbEndpoint outEndpoint;
|
private UsbEndpoint outEndpoint;
|
||||||
private UsbRequest requestIN;
|
|
||||||
|
|
||||||
|
|
||||||
public PL2303SerialDevice(UsbDevice device, UsbDeviceConnection connection)
|
public PL2303SerialDevice(UsbDevice device, UsbDeviceConnection connection)
|
||||||
|
@ -64,7 +63,7 @@ public class PL2303SerialDevice extends UsbSerialDevice
|
||||||
if(ret)
|
if(ret)
|
||||||
{
|
{
|
||||||
// Initialize UsbRequest
|
// Initialize UsbRequest
|
||||||
requestIN = new SafeUsbRequest();
|
UsbRequest requestIN = new SafeUsbRequest();
|
||||||
requestIN.initialize(connection, inEndpoint);
|
requestIN.initialize(connection, inEndpoint);
|
||||||
|
|
||||||
// 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
|
||||||
|
|
|
@ -11,6 +11,7 @@ public class SerialBuffer
|
||||||
static final int DEFAULT_READ_BUFFER_SIZE = 16 * 1024;
|
static final int DEFAULT_READ_BUFFER_SIZE = 16 * 1024;
|
||||||
static final int MAX_BULK_BUFFER = 16 * 1024;
|
static final int MAX_BULK_BUFFER = 16 * 1024;
|
||||||
private ByteBuffer readBuffer;
|
private ByteBuffer readBuffer;
|
||||||
|
|
||||||
private SynchronizedBuffer writeBuffer;
|
private SynchronizedBuffer writeBuffer;
|
||||||
private byte[] readBufferCompatible; // Read buffer for android < 4.2
|
private byte[] readBufferCompatible; // Read buffer for android < 4.2
|
||||||
private boolean debugging = false;
|
private boolean debugging = false;
|
||||||
|
|
|
@ -9,7 +9,7 @@ public class SerialInputStream extends InputStream
|
||||||
|
|
||||||
private int maxBufferSize = 16 * 1024;
|
private int maxBufferSize = 16 * 1024;
|
||||||
|
|
||||||
private byte[] buffer;
|
private final byte[] buffer;
|
||||||
private int pointer;
|
private int pointer;
|
||||||
private int bufferSize;
|
private int bufferSize;
|
||||||
|
|
||||||
|
|
|
@ -18,7 +18,6 @@ import java.util.ArrayList;
|
||||||
import java.util.HashMap;
|
import java.util.HashMap;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
import java.util.concurrent.ArrayBlockingQueue;
|
import java.util.concurrent.ArrayBlockingQueue;
|
||||||
import java.util.concurrent.atomic.AtomicBoolean;
|
|
||||||
|
|
||||||
public class SerialPortBuilder {
|
public class SerialPortBuilder {
|
||||||
private static final String ACTION_USB_PERMISSION = "com.felhr.usbserial.USB_PERMISSION";
|
private static final String ACTION_USB_PERMISSION = "com.felhr.usbserial.USB_PERMISSION";
|
||||||
|
@ -30,14 +29,12 @@ public class SerialPortBuilder {
|
||||||
private List<UsbDeviceStatus> devices;
|
private List<UsbDeviceStatus> devices;
|
||||||
private List<UsbSerialDevice> serialDevices = new ArrayList<>();
|
private List<UsbSerialDevice> serialDevices = new ArrayList<>();
|
||||||
|
|
||||||
private ArrayBlockingQueue<PendingUsbPermission> queuedPermissions = new ArrayBlockingQueue<>(100);
|
private final ArrayBlockingQueue<PendingUsbPermission> queuedPermissions = new ArrayBlockingQueue<>(100);
|
||||||
private AtomicBoolean processingPermission = new AtomicBoolean(false);
|
private volatile boolean processingPermission = false;
|
||||||
private PendingUsbPermission currentPendingPermission;
|
private PendingUsbPermission currentPendingPermission;
|
||||||
|
|
||||||
private UsbManager usbManager;
|
private UsbManager usbManager;
|
||||||
private SerialPortCallback serialPortCallback;
|
private final SerialPortCallback serialPortCallback;
|
||||||
|
|
||||||
private InitSerialPortThread initSerialPortThread;
|
|
||||||
|
|
||||||
private int baudRate, dataBits, stopBits, parity, flowControl;
|
private int baudRate, dataBits, stopBits, parity, flowControl;
|
||||||
private int mode = 0;
|
private int mode = 0;
|
||||||
|
@ -87,7 +84,7 @@ public class SerialPortBuilder {
|
||||||
queuedPermissions.add(createUsbPermission(context, deviceStatus));
|
queuedPermissions.add(createUsbPermission(context, deviceStatus));
|
||||||
}
|
}
|
||||||
|
|
||||||
if(!processingPermission.get()){
|
if(!processingPermission){
|
||||||
launchPermission();
|
launchPermission();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -107,7 +104,7 @@ public class SerialPortBuilder {
|
||||||
|
|
||||||
devices.addAll(newDevices);
|
devices.addAll(newDevices);
|
||||||
|
|
||||||
if(!processingPermission.get()){
|
if(!processingPermission){
|
||||||
launchPermission();
|
launchPermission();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -164,13 +161,13 @@ public class SerialPortBuilder {
|
||||||
|
|
||||||
private void launchPermission(){
|
private void launchPermission(){
|
||||||
try {
|
try {
|
||||||
processingPermission.set(true);
|
processingPermission = true;
|
||||||
currentPendingPermission = queuedPermissions.take();
|
currentPendingPermission = queuedPermissions.take();
|
||||||
usbManager.requestPermission(currentPendingPermission.usbDeviceStatus.usbDevice,
|
usbManager.requestPermission(currentPendingPermission.usbDeviceStatus.usbDevice,
|
||||||
currentPendingPermission.pendingIntent);
|
currentPendingPermission.pendingIntent);
|
||||||
} catch (InterruptedException e) {
|
} catch (InterruptedException e) {
|
||||||
e.printStackTrace();
|
e.printStackTrace();
|
||||||
processingPermission.set(false);
|
processingPermission = false;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -204,12 +201,13 @@ public class SerialPortBuilder {
|
||||||
public void onReceive(Context context, Intent intent) {
|
public void onReceive(Context context, Intent intent) {
|
||||||
if (intent.getAction().equals(ACTION_USB_PERMISSION)) {
|
if (intent.getAction().equals(ACTION_USB_PERMISSION)) {
|
||||||
boolean granted = intent.getExtras().getBoolean(UsbManager.EXTRA_PERMISSION_GRANTED);
|
boolean granted = intent.getExtras().getBoolean(UsbManager.EXTRA_PERMISSION_GRANTED);
|
||||||
|
InitSerialPortThread initSerialPortThread;
|
||||||
if (granted) {
|
if (granted) {
|
||||||
createAllPorts(currentPendingPermission.usbDeviceStatus);
|
createAllPorts(currentPendingPermission.usbDeviceStatus);
|
||||||
if(queuedPermissions.size() > 0) {
|
if(queuedPermissions.size() > 0) {
|
||||||
launchPermission();
|
launchPermission();
|
||||||
}else{
|
}else{
|
||||||
processingPermission.set(false);
|
processingPermission = false;
|
||||||
if(mode == MODE_START) {
|
if(mode == MODE_START) {
|
||||||
serialPortCallback.onSerialPortsDetected(serialDevices);
|
serialPortCallback.onSerialPortsDetected(serialDevices);
|
||||||
}else{
|
}else{
|
||||||
|
@ -221,7 +219,7 @@ public class SerialPortBuilder {
|
||||||
if(queuedPermissions.size() > 0) {
|
if(queuedPermissions.size() > 0) {
|
||||||
launchPermission();
|
launchPermission();
|
||||||
}else{
|
}else{
|
||||||
processingPermission.set(false);
|
processingPermission = false;
|
||||||
if(mode == MODE_START) {
|
if(mode == MODE_START) {
|
||||||
serialPortCallback.onSerialPortsDetected(serialDevices);
|
serialPortCallback.onSerialPortsDetected(serialDevices);
|
||||||
}else{
|
}else{
|
||||||
|
@ -236,7 +234,7 @@ public class SerialPortBuilder {
|
||||||
|
|
||||||
private class InitSerialPortThread extends Thread {
|
private class InitSerialPortThread extends Thread {
|
||||||
|
|
||||||
private List<UsbSerialDevice> usbSerialDevices;
|
private final List<UsbSerialDevice> usbSerialDevices;
|
||||||
|
|
||||||
public InitSerialPortThread(List<UsbSerialDevice> usbSerialDevices) {
|
public InitSerialPortThread(List<UsbSerialDevice> usbSerialDevices) {
|
||||||
this.usbSerialDevices = usbSerialDevices;
|
this.usbSerialDevices = usbSerialDevices;
|
||||||
|
|
|
@ -1,7 +1,5 @@
|
||||||
package com.felhr.usbserial;
|
package com.felhr.usbserial;
|
||||||
|
|
||||||
import java.util.concurrent.atomic.AtomicBoolean;
|
|
||||||
|
|
||||||
import com.felhr.deviceids.CH34xIds;
|
import com.felhr.deviceids.CH34xIds;
|
||||||
import com.felhr.deviceids.CP210xIds;
|
import com.felhr.deviceids.CP210xIds;
|
||||||
import com.felhr.deviceids.FTDISioIds;
|
import com.felhr.deviceids.FTDISioIds;
|
||||||
|
@ -22,10 +20,9 @@ public abstract class UsbSerialDevice implements UsbSerialInterface
|
||||||
public static final String FTDI = "ftdi";
|
public static final String FTDI = "ftdi";
|
||||||
public static final String PL2303 = "pl2303";
|
public static final String PL2303 = "pl2303";
|
||||||
|
|
||||||
private static final String CLASS_ID = UsbSerialDevice.class.getSimpleName();
|
|
||||||
protected static final String COM_PORT = "COM ";
|
protected static final String COM_PORT = "COM ";
|
||||||
|
|
||||||
private static boolean mr1Version;
|
private static final boolean mr1Version;
|
||||||
protected final UsbDevice device;
|
protected final UsbDevice device;
|
||||||
protected final UsbDeviceConnection connection;
|
protected final UsbDeviceConnection connection;
|
||||||
|
|
||||||
|
@ -304,24 +301,20 @@ public abstract class UsbSerialDevice implements UsbSerialInterface
|
||||||
/*
|
/*
|
||||||
* WorkerThread waits for request notifications from IN endpoint
|
* WorkerThread waits for request notifications from IN endpoint
|
||||||
*/
|
*/
|
||||||
protected class WorkerThread extends Thread
|
protected class WorkerThread extends AbstractWorkerThread
|
||||||
{
|
{
|
||||||
private UsbSerialDevice usbSerialDevice;
|
private final UsbSerialDevice usbSerialDevice;
|
||||||
|
|
||||||
private UsbReadCallback callback;
|
private UsbReadCallback callback;
|
||||||
private UsbRequest requestIN;
|
private UsbRequest requestIN;
|
||||||
private AtomicBoolean working;
|
|
||||||
|
|
||||||
public WorkerThread(UsbSerialDevice usbSerialDevice)
|
public WorkerThread(UsbSerialDevice usbSerialDevice)
|
||||||
{
|
{
|
||||||
this.usbSerialDevice = usbSerialDevice;
|
this.usbSerialDevice = usbSerialDevice;
|
||||||
working = new AtomicBoolean(true);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void run()
|
public void doRun()
|
||||||
{
|
|
||||||
while(working.get())
|
|
||||||
{
|
{
|
||||||
UsbRequest request = connection.requestWait();
|
UsbRequest request = connection.requestWait();
|
||||||
if(request != null && request.getEndpoint().getType() == UsbConstants.USB_ENDPOINT_XFER_BULK
|
if(request != null && request.getEndpoint().getType() == UsbConstants.USB_ENDPOINT_XFER_BULK
|
||||||
|
@ -351,7 +344,6 @@ public abstract class UsbSerialDevice implements UsbSerialInterface
|
||||||
requestIN.queue(serialBuffer.getReadBuffer(), SerialBuffer.DEFAULT_READ_BUFFER_SIZE);
|
requestIN.queue(serialBuffer.getReadBuffer(), SerialBuffer.DEFAULT_READ_BUFFER_SIZE);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
public void setCallback(UsbReadCallback callback)
|
public void setCallback(UsbReadCallback callback)
|
||||||
{
|
{
|
||||||
|
@ -373,57 +365,36 @@ public abstract class UsbSerialDevice implements UsbSerialInterface
|
||||||
if(callback != null)
|
if(callback != null)
|
||||||
callback.onReceivedData(data);
|
callback.onReceivedData(data);
|
||||||
}
|
}
|
||||||
|
|
||||||
public void stopWorkingThread()
|
|
||||||
{
|
|
||||||
working.set(false);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
protected class WriteThread extends Thread
|
private class WriteThread extends AbstractWorkerThread
|
||||||
{
|
{
|
||||||
private UsbEndpoint outEndpoint;
|
private UsbEndpoint outEndpoint;
|
||||||
private AtomicBoolean working;
|
|
||||||
|
|
||||||
public WriteThread()
|
|
||||||
{
|
|
||||||
working = new AtomicBoolean(true);
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void run()
|
public void doRun()
|
||||||
{
|
|
||||||
while(working.get())
|
|
||||||
{
|
{
|
||||||
byte[] data = serialBuffer.getWriteBuffer();
|
byte[] data = serialBuffer.getWriteBuffer();
|
||||||
if(data.length > 0)
|
if(data.length > 0)
|
||||||
connection.bulkTransfer(outEndpoint, data, data.length, USB_TIMEOUT);
|
connection.bulkTransfer(outEndpoint, data, data.length, USB_TIMEOUT);
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
public void setUsbEndpoint(UsbEndpoint outEndpoint)
|
public void setUsbEndpoint(UsbEndpoint outEndpoint)
|
||||||
{
|
{
|
||||||
this.outEndpoint = outEndpoint;
|
this.outEndpoint = outEndpoint;
|
||||||
}
|
}
|
||||||
|
|
||||||
public void stopWriteThread()
|
|
||||||
{
|
|
||||||
working.set(false);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
protected class ReadThread extends Thread
|
protected class ReadThread extends AbstractWorkerThread
|
||||||
{
|
{
|
||||||
private UsbSerialDevice usbSerialDevice;
|
private final UsbSerialDevice usbSerialDevice;
|
||||||
|
|
||||||
private UsbReadCallback callback;
|
private UsbReadCallback callback;
|
||||||
private UsbEndpoint inEndpoint;
|
private UsbEndpoint inEndpoint;
|
||||||
private AtomicBoolean working;
|
|
||||||
|
|
||||||
public ReadThread(UsbSerialDevice usbSerialDevice)
|
public ReadThread(UsbSerialDevice usbSerialDevice)
|
||||||
{
|
{
|
||||||
this.usbSerialDevice = usbSerialDevice;
|
this.usbSerialDevice = usbSerialDevice;
|
||||||
working = new AtomicBoolean(true);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public void setCallback(UsbReadCallback callback)
|
public void setCallback(UsbReadCallback callback)
|
||||||
|
@ -432,12 +403,9 @@ public abstract class UsbSerialDevice implements UsbSerialInterface
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void run()
|
public void doRun()
|
||||||
{
|
{
|
||||||
byte[] dataReceived = null;
|
byte[] dataReceived = null;
|
||||||
|
|
||||||
while(working.get())
|
|
||||||
{
|
|
||||||
int numberBytes;
|
int numberBytes;
|
||||||
if(inEndpoint != null)
|
if(inEndpoint != null)
|
||||||
numberBytes = connection.bulkTransfer(inEndpoint, serialBuffer.getBufferCompatible(),
|
numberBytes = connection.bulkTransfer(inEndpoint, serialBuffer.getBufferCompatible(),
|
||||||
|
@ -466,18 +434,12 @@ public abstract class UsbSerialDevice implements UsbSerialInterface
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
public void setUsbEndpoint(UsbEndpoint inEndpoint)
|
public void setUsbEndpoint(UsbEndpoint inEndpoint)
|
||||||
{
|
{
|
||||||
this.inEndpoint = inEndpoint;
|
this.inEndpoint = inEndpoint;
|
||||||
}
|
}
|
||||||
|
|
||||||
public void stopReadThread()
|
|
||||||
{
|
|
||||||
working.set(false);
|
|
||||||
}
|
|
||||||
|
|
||||||
private void onReceivedData(byte[] data)
|
private void onReceivedData(byte[] data)
|
||||||
{
|
{
|
||||||
if(callback != null)
|
if(callback != null)
|
||||||
|
@ -511,11 +473,11 @@ public abstract class UsbSerialDevice implements UsbSerialInterface
|
||||||
{
|
{
|
||||||
if(mr1Version && workerThread != null)
|
if(mr1Version && workerThread != null)
|
||||||
{
|
{
|
||||||
workerThread.stopWorkingThread();
|
workerThread.stopThread();
|
||||||
workerThread = null;
|
workerThread = null;
|
||||||
}else if(!mr1Version && readThread != null)
|
}else if(!mr1Version && readThread != null)
|
||||||
{
|
{
|
||||||
readThread.stopReadThread();
|
readThread.stopThread();
|
||||||
readThread = null;
|
readThread = null;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -542,7 +504,7 @@ public abstract class UsbSerialDevice implements UsbSerialInterface
|
||||||
{
|
{
|
||||||
if(writeThread != null)
|
if(writeThread != null)
|
||||||
{
|
{
|
||||||
writeThread.stopWriteThread();
|
writeThread.stopThread();
|
||||||
writeThread = null;
|
writeThread = null;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -6,11 +6,8 @@ import android.hardware.usb.UsbEndpoint;
|
||||||
|
|
||||||
import com.felhr.deviceids.CP2130Ids;
|
import com.felhr.deviceids.CP2130Ids;
|
||||||
|
|
||||||
import java.util.concurrent.atomic.AtomicBoolean;
|
|
||||||
|
|
||||||
public abstract class UsbSpiDevice implements UsbSpiInterface
|
public abstract class UsbSpiDevice implements UsbSpiInterface
|
||||||
{
|
{
|
||||||
private static final String CLASS_ID = UsbSerialDevice.class.getSimpleName();
|
|
||||||
|
|
||||||
protected static final int USB_TIMEOUT = 5000;
|
protected static final int USB_TIMEOUT = 5000;
|
||||||
|
|
||||||
|
@ -83,48 +80,28 @@ public abstract class UsbSpiDevice implements UsbSpiInterface
|
||||||
@Override
|
@Override
|
||||||
public abstract void closeSPI();
|
public abstract void closeSPI();
|
||||||
|
|
||||||
protected class WriteThread extends Thread
|
protected class WriteThread extends AbstractWorkerThread
|
||||||
{
|
{
|
||||||
private UsbEndpoint outEndpoint;
|
private UsbEndpoint outEndpoint;
|
||||||
private AtomicBoolean working;
|
|
||||||
|
|
||||||
public WriteThread()
|
|
||||||
{
|
|
||||||
working = new AtomicBoolean(true);
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void run()
|
public void doRun()
|
||||||
{
|
|
||||||
while(working.get())
|
|
||||||
{
|
{
|
||||||
byte[] data = serialBuffer.getWriteBuffer();
|
byte[] data = serialBuffer.getWriteBuffer();
|
||||||
if(data.length > 0)
|
if(data.length > 0)
|
||||||
connection.bulkTransfer(outEndpoint, data, data.length, USB_TIMEOUT);
|
connection.bulkTransfer(outEndpoint, data, data.length, USB_TIMEOUT);
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
public void setUsbEndpoint(UsbEndpoint outEndpoint)
|
public void setUsbEndpoint(UsbEndpoint outEndpoint)
|
||||||
{
|
{
|
||||||
this.outEndpoint = outEndpoint;
|
this.outEndpoint = outEndpoint;
|
||||||
}
|
}
|
||||||
|
|
||||||
public void stopWriteThread()
|
|
||||||
{
|
|
||||||
working.set(false);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
protected class ReadThread extends Thread
|
protected class ReadThread extends AbstractWorkerThread
|
||||||
{
|
{
|
||||||
private UsbMISOCallback misoCallback;
|
private UsbMISOCallback misoCallback;
|
||||||
private UsbEndpoint inEndpoint;
|
private UsbEndpoint inEndpoint;
|
||||||
private AtomicBoolean working;
|
|
||||||
|
|
||||||
public ReadThread()
|
|
||||||
{
|
|
||||||
working = new AtomicBoolean(true);
|
|
||||||
}
|
|
||||||
|
|
||||||
public void setCallback(UsbMISOCallback misoCallback)
|
public void setCallback(UsbMISOCallback misoCallback)
|
||||||
{
|
{
|
||||||
|
@ -132,12 +109,9 @@ public abstract class UsbSpiDevice implements UsbSpiInterface
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void run()
|
public void doRun()
|
||||||
{
|
{
|
||||||
byte[] dataReceived = null;
|
byte[] dataReceived = null;
|
||||||
|
|
||||||
while(working.get())
|
|
||||||
{
|
|
||||||
int numberBytes;
|
int numberBytes;
|
||||||
if(inEndpoint != null)
|
if(inEndpoint != null)
|
||||||
numberBytes = connection.bulkTransfer(inEndpoint, serialBuffer.getBufferCompatible(),
|
numberBytes = connection.bulkTransfer(inEndpoint, serialBuffer.getBufferCompatible(),
|
||||||
|
@ -152,18 +126,12 @@ public abstract class UsbSpiDevice implements UsbSpiInterface
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
public void setUsbEndpoint(UsbEndpoint inEndpoint)
|
public void setUsbEndpoint(UsbEndpoint inEndpoint)
|
||||||
{
|
{
|
||||||
this.inEndpoint = inEndpoint;
|
this.inEndpoint = inEndpoint;
|
||||||
}
|
}
|
||||||
|
|
||||||
public void stopReadThread()
|
|
||||||
{
|
|
||||||
working.set(false);
|
|
||||||
}
|
|
||||||
|
|
||||||
private void onReceivedData(byte[] data)
|
private void onReceivedData(byte[] data)
|
||||||
{
|
{
|
||||||
if(misoCallback != null)
|
if(misoCallback != null)
|
||||||
|
@ -187,7 +155,7 @@ public abstract class UsbSpiDevice implements UsbSpiInterface
|
||||||
{
|
{
|
||||||
if(readThread != null)
|
if(readThread != null)
|
||||||
{
|
{
|
||||||
readThread.stopReadThread();
|
readThread.stopThread();
|
||||||
readThread = null;
|
readThread = null;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -206,7 +174,7 @@ public abstract class UsbSpiDevice implements UsbSpiInterface
|
||||||
{
|
{
|
||||||
if(writeThread != null)
|
if(writeThread != null)
|
||||||
{
|
{
|
||||||
writeThread.stopWriteThread();
|
writeThread.stopThread();
|
||||||
writeThread = null;
|
writeThread = null;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -49,10 +49,9 @@ public class XdcVcpSerialDevice extends UsbSerialDevice
|
||||||
private static final int XDCVCP_XOFF = 0x0000;
|
private static final int XDCVCP_XOFF = 0x0000;
|
||||||
private static final int DEFAULT_BAUDRATE = 115200;
|
private static final int DEFAULT_BAUDRATE = 115200;
|
||||||
|
|
||||||
private UsbInterface mInterface;
|
private final UsbInterface mInterface;
|
||||||
private UsbEndpoint inEndpoint;
|
private UsbEndpoint inEndpoint;
|
||||||
private UsbEndpoint outEndpoint;
|
private UsbEndpoint outEndpoint;
|
||||||
private UsbRequest requestIN;
|
|
||||||
|
|
||||||
public XdcVcpSerialDevice(UsbDevice device, UsbDeviceConnection connection)
|
public XdcVcpSerialDevice(UsbDevice device, UsbDeviceConnection connection)
|
||||||
{
|
{
|
||||||
|
@ -110,7 +109,7 @@ public class XdcVcpSerialDevice extends UsbSerialDevice
|
||||||
return false;
|
return false;
|
||||||
|
|
||||||
// Initialize UsbRequest
|
// Initialize UsbRequest
|
||||||
requestIN = new UsbRequest();
|
UsbRequest requestIN = new UsbRequest();
|
||||||
requestIN.initialize(connection, inEndpoint);
|
requestIN.initialize(connection, inEndpoint);
|
||||||
|
|
||||||
// Pass references to the threads
|
// Pass references to the threads
|
||||||
|
|
Ładowanie…
Reference in New Issue