summaryrefslogtreecommitdiffstats
path: root/android.cpp
diff options
context:
space:
mode:
authorGravatar Venkatesh Shukla <venkatesh.shukla.eee11@iitbhu.ac.in>2015-08-21 00:19:39 +0200
committerGravatar Dirk Hohndel <dirk@hohndel.org>2015-08-20 22:53:14 -0700
commitb629088dfae1c2e942f02799612840382be351d2 (patch)
treeb7f8c7ad236c3d5bdb2dd23c5ea13b8c83879188 /android.cpp
parent72872bd7fdd84d714cb38fb4e1e47034511b3ae6 (diff)
downloadsubsurface-b629088dfae1c2e942f02799612840382be351d2.tar.gz
Extract usb file descriptor of ftdi on Android
Using JNI implementation of QtAndroid, usb file descriptor of attached ftdi usb device is extracted. This would then be used for opening usb device on android. This implementation assumes that permission for usage of device is already present. This is because of the device_filter due to which on attaching a recognised usb device (Only ftdi for now) to android, a dialog pops up asking for permission to use it with subsurface. Not an ideal assumption but works sufficiently well. Signed-off-by: Venkatesh Shukla <venkatesh.shukla.eee11@iitbhu.ac.in> Signed-off-by: Anton Lundin <glance@acc.umu.se> Signed-off-by: Dirk Hohndel <dirk@hohndel.org>
Diffstat (limited to 'android.cpp')
-rw-r--r--android.cpp79
1 files changed, 79 insertions, 0 deletions
diff --git a/android.cpp b/android.cpp
index 0b4aadb27..4e2b2b1d3 100644
--- a/android.cpp
+++ b/android.cpp
@@ -8,6 +8,10 @@
#include <QtAndroidExtras/QtAndroidExtras>
#include <QtAndroidExtras/QAndroidJniObject>
+#include <QtAndroid>
+
+#define FTDI_VID 0x0403
+#define USB_SERVICE "usb"
extern "C" {
@@ -51,6 +55,81 @@ int enumerate_devices(device_callback_t callback, void *userdata, int dc_type)
return -1;
}
+/**
+ * Get the file descriptor of first available ftdi device attached to usb in android.
+ * This is needed for dc_device_open on android.
+ *
+ * return
+ * -1 : No Usb Device is attached.
+ * -2 : No ftdi device found.
+ * -3 : No permission for using the device
+ * -4 : Error while opening the device.
+ * +ve num : Successfully extracted file descriptor is returned.
+ * */
+int get_usb_fd()
+{
+ int i;
+ jint fd, vendorid, productid;
+ QAndroidJniObject usbName, usbDevice;
+
+ // Get the current main activity of the application.
+ QAndroidJniObject activity = QtAndroid::androidActivity();
+
+ QAndroidJniObject usb_service = QAndroidJniObject::fromString(USB_SERVICE);
+
+ // Get UsbManager from activity
+ QAndroidJniObject usbManager = activity.callObjectMethod("getSystemService", "(Ljava/lang/String;)Ljava/lang/Object;", usb_service.object());
+
+ // Get a HashMap<Name, UsbDevice> of all USB devices attached to Android
+ QAndroidJniObject deviceMap = usbManager.callObjectMethod("getDeviceList", "()Ljava/util/HashMap;");
+ jint num_devices = deviceMap.callMethod<jint>("size", "()I");
+ if (num_devices == 0) {
+ // No USB device is attached.
+ return -1;
+ }
+
+ // Iterate over all the devices and find the first available FTDI device.
+ QAndroidJniObject keySet = deviceMap.callObjectMethod("keySet", "()Ljava/util/Set;");
+ QAndroidJniObject iterator = keySet.callObjectMethod("iterator", "()Ljava/util/Iterator;");
+
+ for (i = 0; i < num_devices; i++) {
+ usbName = iterator.callObjectMethod("next", "()Ljava/lang/Object;");
+ usbDevice = deviceMap.callObjectMethod ("get", "(Ljava/lang/Object;)Ljava/lang/Object;", usbName.object());
+ vendorid = usbDevice.callMethod<jint>("getVendorId", "()I");
+ productid = usbDevice.callMethod<jint>("getProductId", "()I");
+ if(vendorid == FTDI_VID) // Found a FTDI device. Break.
+ break;
+ }
+ if (i == num_devices) {
+ // No ftdi device found.
+ return -2;
+ }
+
+ jboolean hasPermission = usbManager.callMethod<jboolean>("hasPermission", "(Landroid/hardware/usb/UsbDevice;)Z", usbDevice.object());
+ if (!hasPermission) {
+ // You do not have permission to use the usbDevice.
+ // Please remove and reinsert the USB device.
+ // Could also give an dialogbox asking for permission.
+ return -3;
+ }
+
+ // An FTDI device is present and we also have permission to use the device.
+ // Open the device and get its file descriptor.
+ QAndroidJniObject usbDeviceConnection = usbManager.callObjectMethod("openDevice", "(Landroid/hardware/usb/UsbDevice;)Landroid/hardware/usb/UsbDeviceConnection;", usbDevice.object());
+ if (usbDeviceConnection.object() == NULL) {
+ // Some error occurred while opening the device. Exit.
+ return -4;
+ }
+
+ // Finally get the required file descriptor.
+ fd = usbDeviceConnection.callMethod<jint>("getFileDescriptor", "()I");
+ if (fd == -1) {
+ // The device is not opened. Some error.
+ return -4;
+ }
+ return fd;
+}
+
/* NOP wrappers to comform with windows.c */
int subsurface_rename(const char *path, const char *newpath)
{