Commit 6243dbac authored by Don Gagne's avatar Don Gagne

parent 6580a544
......@@ -41,8 +41,8 @@ public class CdcAcmSerialDriver extends CommonUsbSerialDriver {
private static final int SET_CONTROL_LINE_STATE = 0x22;
private static final int SEND_BREAK = 0x23;
public CdcAcmSerialDriver(UsbDevice device, UsbDeviceConnection connection) {
super(device, connection);
public CdcAcmSerialDriver(UsbDevice device) {
super(device);
}
@Override
......
......@@ -36,25 +36,41 @@ abstract class CommonUsbSerialDriver implements UsbSerialDriver {
public static final int DEFAULT_WRITE_BUFFER_SIZE = 16 * 1024;
protected final UsbDevice mDevice;
protected final UsbDeviceConnection mConnection;
protected final Object mReadBufferLock = new Object();
protected final Object mWriteBufferLock = new Object();
protected UsbDeviceConnection mConnection = null;
private int _permissionStatus = permissionStatusRequestRequired;
/** Internal read buffer. Guarded by {@link #mReadBufferLock}. */
protected byte[] mReadBuffer;
/** Internal write buffer. Guarded by {@link #mWriteBufferLock}. */
protected byte[] mWriteBuffer;
public CommonUsbSerialDriver(UsbDevice device, UsbDeviceConnection connection) {
public CommonUsbSerialDriver(UsbDevice device) {
mDevice = device;
mConnection = connection;
mReadBuffer = new byte[DEFAULT_READ_BUFFER_SIZE];
mWriteBuffer = new byte[DEFAULT_WRITE_BUFFER_SIZE];
}
@Override
public void setConnection(UsbDeviceConnection connection) {
mConnection = connection;
}
@Override
public int permissionStatus() {
return _permissionStatus;
}
@Override
public void setPermissionStatus(int permissionStatus) {
_permissionStatus = permissionStatus;
}
/**
* Returns the currently-bound USB device.
*
......
......@@ -61,8 +61,8 @@ public class Cp2102SerialDriver extends CommonUsbSerialDriver {
private UsbEndpoint mReadEndpoint;
private UsbEndpoint mWriteEndpoint;
public Cp2102SerialDriver(UsbDevice device, UsbDeviceConnection connection) {
super(device, connection);
public Cp2102SerialDriver(UsbDevice device) {
super(device);
}
private int setConfigSingle(int request, int value) {
......
......@@ -211,8 +211,8 @@ public class FtdiSerialDriver extends CommonUsbSerialDriver {
* @throws UsbSerialRuntimeException if the given device is incompatible
* with this driver
*/
public FtdiSerialDriver(UsbDevice usbDevice, UsbDeviceConnection usbConnection) {
super(usbDevice, usbConnection);
public FtdiSerialDriver(UsbDevice usbDevice) {
super(usbDevice);
mType = null;
}
......
......@@ -231,8 +231,8 @@ public class ProlificSerialDriver extends CommonUsbSerialDriver {
return ((getStatus() & flag) == flag);
}
public ProlificSerialDriver(UsbDevice device, UsbDeviceConnection connection) {
super(device, connection);
public ProlificSerialDriver(UsbDevice device) {
super(device);
}
@Override
......
......@@ -83,6 +83,16 @@ public interface UsbSerialDriver {
/** 2 stop bits. */
public static final int STOPBITS_2 = 2;
public static final int permissionStatusSuccess = 0;
public static final int permissionStatusDenied = 1;
public static final int permissionStatusRequested = 2;
public static final int permissionStatusRequestRequired = 3;
public static final int permissionStatusOpen = 4;
public int permissionStatus();
public void setPermissionStatus(int permissionStatus);
public void setConnection(UsbDeviceConnection connection);
/**
* Returns the currently-bound USB device.
*
......
......@@ -65,11 +65,7 @@ public enum UsbSerialProber {
if (!testIfSupported(usbDevice, FtdiSerialDriver.getSupportedDevices())) {
return Collections.emptyList();
}
final UsbDeviceConnection connection = manager.openDevice(usbDevice);
if (connection == null) {
return Collections.emptyList();
}
final UsbSerialDriver driver = new FtdiSerialDriver(usbDevice, connection);
final UsbSerialDriver driver = new FtdiSerialDriver(usbDevice);
return Collections.singletonList(driver);
}
},
......@@ -80,11 +76,7 @@ public enum UsbSerialProber {
if (!testIfSupported(usbDevice, CdcAcmSerialDriver.getSupportedDevices())) {
return Collections.emptyList();
}
final UsbDeviceConnection connection = manager.openDevice(usbDevice);
if (connection == null) {
return Collections.emptyList();
}
final UsbSerialDriver driver = new CdcAcmSerialDriver(usbDevice, connection);
final UsbSerialDriver driver = new CdcAcmSerialDriver(usbDevice);
return Collections.singletonList(driver);
}
},
......@@ -95,11 +87,7 @@ public enum UsbSerialProber {
if (!testIfSupported(usbDevice, Cp2102SerialDriver.getSupportedDevices())) {
return Collections.emptyList();
}
final UsbDeviceConnection connection = manager.openDevice(usbDevice);
if (connection == null) {
return Collections.emptyList();
}
final UsbSerialDriver driver = new Cp2102SerialDriver(usbDevice, connection);
final UsbSerialDriver driver = new Cp2102SerialDriver(usbDevice);
return Collections.singletonList(driver);
}
},
......@@ -110,11 +98,7 @@ public enum UsbSerialProber {
if (!testIfSupported(usbDevice, ProlificSerialDriver.getSupportedDevices())) {
return Collections.emptyList();
}
final UsbDeviceConnection connection = manager.openDevice(usbDevice);
if (connection == null) {
return Collections.emptyList();
}
final UsbSerialDriver driver = new ProlificSerialDriver(usbDevice, connection);
final UsbSerialDriver driver = new ProlificSerialDriver(usbDevice);
return Collections.singletonList(driver);
}
};
......
......@@ -31,11 +31,13 @@ package org.mavlink.qgroundcontrol;
import java.util.HashMap;
import java.util.List;
import java.util.ArrayList;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.io.IOException;
import android.app.Activity;
import android.app.PendingIntent;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
......@@ -53,81 +55,136 @@ import org.qtproject.qt5.android.bindings.QtApplication;
public class QGCActivity extends QtActivity
{
public static int BAD_PORT = 0;
private static QGCActivity m_instance;
private static UsbManager m_manager; // ANDROID USB HOST CLASS
private static List<UsbSerialDriver> m_devices; // LIST OF CURRENT DEVICES
private static HashMap<Integer, UsbSerialDriver> m_openedDevices; // LIST OF OPENED DEVICES
private static HashMap<Integer, UsbIoManager> m_ioManager; // THREADS FOR LISTENING FOR INCOMING DATA
private static HashMap<Integer, Integer> m_userData; // CORRESPONDING USER DATA FOR OPENED DEVICES. USED IN DISCONNECT CALLBACK
// USED TO DETECT WHEN A DEVICE HAS BEEN UNPLUGGED
private BroadcastReceiver m_UsbReceiver = null;
private final static ExecutorService m_Executor = Executors.newSingleThreadExecutor();
public static int BAD_DEVICE_ID = 0;
private static QGCActivity _instance = null;
private static UsbManager _usbManager = null;
private static List<UsbSerialDriver> _drivers;
private static HashMap<Integer, UsbIoManager> m_ioManager;
private static HashMap<Integer, Integer> _userDataHashByDeviceId;
private static final String TAG = "QGC_QGCActivity";
private static PowerManager.WakeLock m_wl;
private static PowerManager.WakeLock _wakeLock;
private static final String ACTION_USB_PERMISSION = "com.android.example.USB_PERMISSION";
private static PendingIntent _usbPermissionIntent = null;
public static Context m_context;
private final static ExecutorService m_Executor = Executors.newSingleThreadExecutor();
private final static UsbIoManager.Listener m_Listener =
new UsbIoManager.Listener()
{
@Override
public void onRunError(Exception eA, int userDataA)
public void onRunError(Exception eA, int userData)
{
Log.e(TAG, "onRunError Exception");
nativeDeviceException(userDataA, eA.getMessage());
nativeDeviceException(userData, eA.getMessage());
}
@Override
public void onNewData(final byte[] dataA, int userDataA)
public void onNewData(final byte[] dataA, int userData)
{
nativeDeviceNewData(userDataA, dataA);
nativeDeviceNewData(userData, dataA);
}
};
private static UsbSerialDriver _findDriverByDeviceId(int deviceId) {
for (UsbSerialDriver driver: _drivers) {
if (driver.getDevice().getDeviceId() == deviceId) {
return driver;
}
}
return null;
}
private static UsbSerialDriver _findDriverByDeviceName(String deviceName) {
for (UsbSerialDriver driver: _drivers) {
if (driver.getDevice().getDeviceName().equals(deviceName)) {
return driver;
}
}
return null;
}
private final static BroadcastReceiver _usbReceiver = new BroadcastReceiver() {
public void onReceive(Context context, Intent intent) {
String action = intent.getAction();
Log.i(TAG, "BroadcastReceiver USB action " + action);
if (ACTION_USB_PERMISSION.equals(action)) {
synchronized (_instance) {
UsbDevice device = (UsbDevice)intent.getParcelableExtra(UsbManager.EXTRA_DEVICE);
if (device != null) {
UsbSerialDriver driver = _findDriverByDeviceId(device.getDeviceId());
if (intent.getBooleanExtra(UsbManager.EXTRA_PERMISSION_GRANTED, false)) {
qgcLogDebug("Permission granted to " + device.getDeviceName());
driver.setPermissionStatus(UsbSerialDriver.permissionStatusSuccess);
} else {
qgcLogDebug("Permission denied for " + device.getDeviceName());
driver.setPermissionStatus(UsbSerialDriver.permissionStatusDenied);
}
}
}
} else if (UsbManager.ACTION_USB_DEVICE_DETACHED.equals(action)) {
UsbDevice device = (UsbDevice)intent.getParcelableExtra(UsbManager.EXTRA_DEVICE);
if (device != null) {
if (_userDataHashByDeviceId.containsKey(device.getDeviceId())) {
nativeDeviceHasDisconnected(_userDataHashByDeviceId.get(device.getDeviceId()));
}
}
}
}
};
// NATIVE C++ FUNCTION THAT WILL BE CALLED IF THE DEVICE IS UNPLUGGED
private static native void nativeDeviceHasDisconnected(int userDataA);
private static native void nativeDeviceException(int userDataA, String messageA);
private static native void nativeDeviceNewData(int userDataA, byte[] dataA);
// Native C++ functions which connect back to QSerialPort code
private static native void nativeDeviceHasDisconnected(int userData);
private static native void nativeDeviceException(int userData, String messageA);
private static native void nativeDeviceNewData(int userData, byte[] dataA);
// Native C++ functions called to log output
public static native void qgcLogDebug(String message);
public static native void qgcLogWarning(String message);
////////////////////////////////////////////////////////////////////////////////////////////////
//
// Constructor. Only used once to create the initial instance for the static functions.
//
////////////////////////////////////////////////////////////////////////////////////////////////
// QGCActivity singleton
public QGCActivity()
{
m_instance = this;
m_openedDevices = new HashMap<Integer, UsbSerialDriver>();
m_userData = new HashMap<Integer, Integer>();
_instance = this;
_drivers = new ArrayList<UsbSerialDriver>();
_userDataHashByDeviceId = new HashMap<Integer, Integer>();
m_ioManager = new HashMap<Integer, UsbIoManager>();
Log.i(TAG, "Instance created");
}
@Override
public void onCreate(Bundle savedInstanceState) {
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
PowerManager pm = (PowerManager)m_instance.getSystemService(Context.POWER_SERVICE);
m_wl = pm.newWakeLock(PowerManager.SCREEN_BRIGHT_WAKE_LOCK, "QGroundControl");
if(m_wl != null) {
m_wl.acquire();
Log.i(TAG, "SCREEN_BRIGHT_WAKE_LOCK acquired.");
PowerManager pm = (PowerManager)_instance.getSystemService(Context.POWER_SERVICE);
_wakeLock = pm.newWakeLock(PowerManager.SCREEN_BRIGHT_WAKE_LOCK, "QGroundControl");
if(_wakeLock != null) {
_wakeLock.acquire();
} else {
Log.i(TAG, "SCREEN_BRIGHT_WAKE_LOCK not acquired!!!");
}
m_instance.getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
_instance.getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
_usbManager = (UsbManager)_instance.getSystemService(Context.USB_SERVICE);
// Register for USB Detach and USB Permission intent
IntentFilter filter = new IntentFilter();
filter.addAction(UsbManager.ACTION_USB_DEVICE_DETACHED);
filter.addAction(ACTION_USB_PERMISSION);
_instance.registerReceiver(_instance._usbReceiver, filter);
// Create intent for usb permission request
_usbPermissionIntent = PendingIntent.getBroadcast(_instance, 0, new Intent(ACTION_USB_PERMISSION), 0);
}
@Override
protected void onDestroy() {
protected void onDestroy()
{
try {
if(m_wl != null) {
m_wl.release();
Log.i(TAG, "SCREEN_BRIGHT_WAKE_LOCK released.");
if(_wakeLock != null) {
_wakeLock.release();
}
} catch(Exception e) {
Log.e(TAG, "Exception onDestroy()");
......@@ -138,225 +195,156 @@ public class QGCActivity extends QtActivity
public void onInit(int status) {
}
/////////////////////////////////////////////////////////////////////////////////////////////////////////
//
// Find all current devices that match the device filter described in the androidmanifest.xml and the
// device_filter.xml
//
/////////////////////////////////////////////////////////////////////////////////////////////////////////
private static boolean getCurrentDevices()
/// Incrementally updates the list of drivers connected to the device
private static void updateCurrentDrivers()
{
if (m_instance == null)
return false;
if (m_manager == null)
m_manager = (UsbManager)m_instance.getSystemService(Context.USB_SERVICE);
List<UsbSerialDriver> currentDrivers = UsbSerialProber.findAllDevices(_usbManager);
if (m_devices != null)
m_devices.clear();
m_devices = UsbSerialProber.findAllDevices(m_manager);
return true;
// Remove stale drivers
for (int i=_drivers.size()-1; i>=0; i--) {
boolean found = false;
for (UsbSerialDriver currentDriver: currentDrivers) {
if (_drivers.get(i).getDevice().getDeviceId() == currentDriver.getDevice().getDeviceId()) {
found = true;
break;
}
/////////////////////////////////////////////////////////////////////////////////////////////////////////
//
// List all available devices that are not already open. It returns the serial port info
// in a : separated string array. Each string entry consists of the following:
//
// DeviceName:Company:ProductId:VendorId
//
/////////////////////////////////////////////////////////////////////////////////////////////////////////
public static String[] availableDevicesInfo()
{
// GET THE LIST OF CURRENT DEVICES
if (!getCurrentDevices())
{
Log.e(TAG, "QGCActivity instance not present");
return null;
}
// MAKE SURE WE HAVE ENTRIES
if (m_devices.size() <= 0)
{
//Log.e(TAG, "No USB devices found");
return null;
if (!found) {
qgcLogDebug("Remove stale driver " + _drivers.get(i).getDevice().getDeviceName());
_drivers.remove(i);
}
if (m_openedDevices == null)
{
Log.e(TAG, "m_openedDevices is null");
return null;
}
int countL = 0;
int iL;
// CHECK FOR ALREADY OPENED DEVICES AND DON"T INCLUDE THEM IN THE COUNT
for (iL=0; iL<m_devices.size(); iL++)
{
if (m_openedDevices.get(m_devices.get(iL).getDevice().getDeviceId()) != null)
{
countL++;
// Add new drivers
for (int i=0; i<currentDrivers.size(); i++) {
boolean found = false;
for (int j=0; j<_drivers.size(); j++) {
if (currentDrivers.get(i).getDevice().getDeviceId() == _drivers.get(j).getDevice().getDeviceId()) {
found = true;
break;
}
}
if (m_devices.size() - countL <= 0)
{
//Log.e(TAG, "No open USB devices found");
return null;
}
String[] listL = new String[m_devices.size() - countL];
UsbSerialDriver driverL;
String tempL;
// GET THE DATA ON THE INDIVIDUAL DEVICES SKIPPING THE ONES THAT ARE ALREADY OPEN
countL = 0;
for (iL=0; iL<m_devices.size(); iL++)
{
driverL = m_devices.get(iL);
if (m_openedDevices.get(driverL.getDevice().getDeviceId()) == null)
{
UsbDevice deviceL = driverL.getDevice();
tempL = deviceL.getDeviceName() + ":";
if (!found) {
UsbSerialDriver newDriver = currentDrivers.get(i);
UsbDevice device = newDriver.getDevice();
String deviceName = device.getDeviceName();
if (driverL instanceof FtdiSerialDriver)
tempL = tempL + "FTDI:";
else if (driverL instanceof CdcAcmSerialDriver)
tempL = tempL + "Cdc Acm:";
else if (driverL instanceof Cp2102SerialDriver)
tempL = tempL + "Cp2102:";
else if (driverL instanceof ProlificSerialDriver)
tempL = tempL + "Prolific:";
else
tempL = tempL + "Unknown:";
_drivers.add(newDriver);
qgcLogDebug("Adding new driver " + deviceName);
tempL = tempL + Integer.toString(deviceL.getProductId()) + ":";
tempL = tempL + Integer.toString(deviceL.getVendorId()) + ":";
listL[countL] = tempL;
countL++;
qgcLogDebug("Found " + tempL);
// Request permission if needed
if (_usbManager.hasPermission(device)) {
qgcLogDebug("Already have permission to use device " + deviceName);
newDriver.setPermissionStatus(UsbSerialDriver.permissionStatusSuccess);
} else {
qgcLogDebug("Requesting permission to use device " + deviceName);
newDriver.setPermissionStatus(UsbSerialDriver.permissionStatusRequested);
_usbManager.requestPermission(device, _usbPermissionIntent);
}
}
}
}
return listL;
/// Returns array of device info for each unopened device.
/// @return Device info format DeviceName:Company:ProductId:VendorId
public static String[] availableDevicesInfo()
{
updateCurrentDrivers();
if (_drivers.size() <= 0) {
return null;
}
List<String> deviceInfoList = new ArrayList<String>();
for (int i=0; i<_drivers.size(); i++) {
String deviceInfo;
UsbSerialDriver driver = _drivers.get(i);
/////////////////////////////////////////////////////////////////////////////////////////////////
//
// Open a device based on the name.
//
// Args: nameA - name of the device to open
// userDataA - C++ pointer to the QSerialPort that is trying to open the device. This is
// used by the detector to inform the C++ side if it is unplugged
//
// Returns: ID number of opened port. This number is used to reference the correct port in subsequent
// calls like close(), read(), and write().
//
/////////////////////////////////////////////////////////////////////////////////////////////////
public static int open(Context parentContext, String nameA, int userDataA)
{
int idL = BAD_PORT;
if (driver.permissionStatus() != UsbSerialDriver.permissionStatusSuccess) {
continue;
}
m_context = parentContext;
UsbDevice device = driver.getDevice();
//qgcLogDebug("Getting device list");
if (!getCurrentDevices())
return BAD_PORT;
deviceInfo = device.getDeviceName() + ":";
// CHECK THAT PORT IS NOT ALREADY OPENED
if (m_openedDevices != null)
{
for (UsbSerialDriver driverL: m_openedDevices.values())
{
if (nameA.equals(driverL.getDevice().getDeviceName()))
{
Log.e(TAG, "Device already opened");
return BAD_PORT;
if (driver instanceof FtdiSerialDriver) {
deviceInfo = deviceInfo + "FTDI:";
} else if (driver instanceof CdcAcmSerialDriver) {
deviceInfo = deviceInfo + "Cdc Acm:";
} else if (driver instanceof Cp2102SerialDriver) {
deviceInfo = deviceInfo + "Cp2102:";
} else if (driver instanceof ProlificSerialDriver) {
deviceInfo = deviceInfo + "Prolific:";
} else {
deviceInfo = deviceInfo + "Unknown:";
}
deviceInfo = deviceInfo + Integer.toString(device.getProductId()) + ":";
deviceInfo = deviceInfo + Integer.toString(device.getVendorId()) + ":";
deviceInfoList.add(deviceInfo);
}
String[] rgDeviceInfo = new String[deviceInfoList.size()];
for (int i=0; i<deviceInfoList.size(); i++) {
rgDeviceInfo[i] = deviceInfoList.get(i);
}
else
return BAD_PORT;
if (m_devices == null)
return BAD_PORT;
return rgDeviceInfo;
}
// OPEN THE DEVICE
try
{
for (int iL=0; iL<m_devices.size(); iL++)
{
Log.i(TAG, "Checking device " + m_devices.get(iL).getDevice().getDeviceName() + " id: " + m_devices.get(iL).getDevice().getDeviceId());
if (nameA.equals(m_devices.get(iL).getDevice().getDeviceName()))
/// Open the specified device
/// @param userData Data to associate with device and pass back through to native calls.
/// @return Device id
public static int open(Context parentContext, String deviceName, int userData)
{
idL = m_devices.get(iL).getDevice().getDeviceId();
m_openedDevices.put(idL, m_devices.get(iL));
m_userData.put(idL, userDataA);
int deviceId = BAD_DEVICE_ID;
if (m_instance.m_UsbReceiver == null)
{
Log.i(TAG, "Creating new broadcast receiver");
m_instance.m_UsbReceiver= new BroadcastReceiver()
{
public void onReceive(Context contextA, Intent intentA)
{
String actionL = intentA.getAction();
m_context = parentContext;
if (UsbManager.ACTION_USB_DEVICE_DETACHED.equals(actionL))
{
UsbDevice deviceL = (UsbDevice)intentA.getParcelableExtra(UsbManager.EXTRA_DEVICE);
if (deviceL != null)
{
if (m_userData.containsKey(deviceL.getDeviceId()))
{
nativeDeviceHasDisconnected(m_userData.get(deviceL.getDeviceId()));
}
}
UsbSerialDriver driver = _findDriverByDeviceName(deviceName);
if (driver == null) {
qgcLogWarning("Attempt to open unknown device " + deviceName);
return BAD_DEVICE_ID;
}
}
};
// TURN ON THE INTENT FILTER SO IT WILL DETECT AN UNPLUG SIGNAL
IntentFilter filterL = new IntentFilter();
filterL.addAction(UsbManager.ACTION_USB_DEVICE_DETACHED);
m_instance.registerReceiver(m_instance.m_UsbReceiver, filterL);
if (driver.permissionStatus() != UsbSerialDriver.permissionStatusSuccess) {
qgcLogWarning("Attempt to open device with incorrect permission status " + deviceName + " " + driver.permissionStatus());
return BAD_DEVICE_ID;
}
m_openedDevices.get(idL).open();
UsbDevice device = driver.getDevice();
deviceId = device.getDeviceId();
// START UP THE IO MANAGER TO READ/WRITE DATA TO THE DEVICE
UsbIoManager managerL = new UsbIoManager(m_openedDevices.get(idL), m_Listener, userDataA);
if (managerL == null)
Log.e(TAG, "UsbIoManager instance is null");
m_ioManager.put(idL, managerL);
m_Executor.submit(managerL);
Log.i(TAG, "Port open successful");
return idL;
}
}
try {
driver.setConnection(_usbManager.openDevice(device));
driver.open();
driver.setPermissionStatus(UsbSerialDriver.permissionStatusOpen);
return BAD_PORT;
}
catch(IOException exA)
{
if (idL != BAD_PORT)
{
m_openedDevices.remove(idL);
m_userData.remove(idL);
_userDataHashByDeviceId.put(deviceId, userData);
UsbIoManager ioManager = new UsbIoManager(driver, m_Listener, userData);
m_ioManager.put(deviceId, ioManager);
m_Executor.submit(ioManager);
if(m_ioManager.get(idL) != null)
m_ioManager.get(idL).stop();
qgcLogDebug("Port open successful");
} catch(IOException exA) {
driver.setPermissionStatus(UsbSerialDriver.permissionStatusRequestRequired);
_userDataHashByDeviceId.remove(deviceId);
m_ioManager.remove(idL);
if(m_ioManager.get(deviceId) != null) {
m_ioManager.get(deviceId).stop();
m_ioManager.remove(deviceId);
}
qgcLogWarning("Port open exception: " + exA.getMessage());
return BAD_PORT;
return BAD_DEVICE_ID;
}
return deviceId;
}
public static void startIoManager(int idA)
......@@ -364,12 +352,12 @@ public class QGCActivity extends QtActivity
if (m_ioManager.get(idA) != null)
return;
UsbSerialDriver driverL = m_openedDevices.get(idA);
UsbSerialDriver driverL = _findDriverByDeviceId(idA);
if (driverL == null)
return;
UsbIoManager managerL = new UsbIoManager(driverL, m_Listener, m_userData.get(idA));
UsbIoManager managerL = new UsbIoManager(driverL, m_Listener, _userDataHashByDeviceId.get(idA));
m_ioManager.put(idA, managerL);
m_Executor.submit(managerL);
}
......@@ -398,7 +386,7 @@ public class QGCActivity extends QtActivity
////////////////////////////////////////////////////////////////////////////////////////////////////////
public static boolean setParameters(int idA, int baudRateA, int dataBitsA, int stopBitsA, int parityA)
{
UsbSerialDriver driverL = m_openedDevices.get(idA);
UsbSerialDriver driverL = _findDriverByDeviceId(idA);
if (driverL == null)
return false;
......@@ -427,7 +415,7 @@ public class QGCActivity extends QtActivity
////////////////////////////////////////////////////////////////////////////////////////////////////////
public static boolean close(int idA)
{
UsbSerialDriver driverL = m_openedDevices.get(idA);
UsbSerialDriver driverL = _findDriverByDeviceId(idA);
if (driverL == null)
return false;
......@@ -435,8 +423,8 @@ public class QGCActivity extends QtActivity
try
{
stopIoManager(idA);
m_userData.remove(idA);
m_openedDevices.remove(idA);
_userDataHashByDeviceId.remove(idA);
driverL.setPermissionStatus(UsbSerialDriver.permissionStatusRequestRequired);
driverL.close();
return true;
......@@ -462,7 +450,7 @@ public class QGCActivity extends QtActivity
/////////////////////////////////////////////////////////////////////////////////////////////////////
public static int write(int idA, byte[] sourceA, int timeoutMSecA)
{
UsbSerialDriver driverL = m_openedDevices.get(idA);
UsbSerialDriver driverL = _findDriverByDeviceId(idA);
if (driverL == null)
return 0;
......@@ -488,52 +476,23 @@ public class QGCActivity extends QtActivity
*/
}
/////////////////////////////////////////////////////////////////////////////////////////////////////
//
// Determine if a device name if valid. Note, it does not look for devices that are already open
//
// Args: nameA - name of device to look for
//
// Returns: T/F
//
////////////////////////////////////////////////////////////////////////////////////////////////////
public static boolean isDeviceNameValid(String nameA)
{
if (m_devices.size() <= 0)
return false;
for (int iL=0; iL<m_devices.size(); iL++)
{
if (m_devices.get(iL).getDevice().getDeviceName() == nameA)
for (UsbSerialDriver driver: _drivers) {
if (driver.getDevice().getDeviceName() == nameA)
return true;
}
return false;
}
/////////////////////////////////////////////////////////////////////////////////////////////////////
//
// Check if the device is open
//
// Args: nameA - name of device
//
// Returns: T/F
//
//////////////////////////////////////////////////////////////////////////////////////////////////////
public static boolean isDeviceNameOpen(String nameA)
{
if (m_openedDevices == null)
return false;
for (UsbSerialDriver driverL: m_openedDevices.values())
{
if (nameA.equals(driverL.getDevice().getDeviceName()))
for (UsbSerialDriver driverL: _drivers) {
if (nameA.equals(driverL.getDevice().getDeviceName()) && driverL.permissionStatus() == UsbSerialDriver.permissionStatusOpen) {
return true;
}
}
return false;
}
......@@ -554,7 +513,7 @@ public class QGCActivity extends QtActivity
{
try
{
UsbSerialDriver driverL = m_openedDevices.get(idA);
UsbSerialDriver driverL = _findDriverByDeviceId(idA);
if (driverL == null)
return false;
......@@ -584,7 +543,7 @@ public class QGCActivity extends QtActivity
{
try
{
UsbSerialDriver driverL = m_openedDevices.get(idA);
UsbSerialDriver driverL = _findDriverByDeviceId(idA);
if (driverL == null)
return false;
......@@ -615,7 +574,7 @@ public class QGCActivity extends QtActivity
{
try
{
UsbSerialDriver driverL = m_openedDevices.get(idA);
UsbSerialDriver driverL = _findDriverByDeviceId(idA);
if (driverL == null)
return false;
......@@ -641,7 +600,7 @@ public class QGCActivity extends QtActivity
///////////////////////////////////////////////////////////////////////////////////////////
public static int getDeviceHandle(int idA)
{
UsbSerialDriver driverL = m_openedDevices.get(idA);
UsbSerialDriver driverL = _findDriverByDeviceId(idA);
if (driverL == null)
return -1;
......@@ -652,21 +611,5 @@ public class QGCActivity extends QtActivity
else
return connectL.getFileDescriptor();
}
//////////////////////////////////////////////////////////////////////////////////////////////
//
// Get the open usb serial driver for the given id
//
// Args: idA - ID number from the open command
//
// Returns: usb device driver
//
/////////////////////////////////////////////////////////////////////////////////////////////
public static UsbSerialDriver getUsbSerialDriver(int idA)
{
return m_openedDevices.get(idA);
}
}
......@@ -197,7 +197,7 @@ bool QSerialPortPrivate::open(QIODevice::OpenMode mode)
if (deviceId == BAD_PORT)
{
qWarning() << "Error opening %s" << systemLocation.toLatin1().data();
qWarning() << "Error opening" << systemLocation.toLatin1().data();
q_ptr->setError(QSerialPort::DeviceNotFoundError);
return false;
}
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment