Wire up get_usb_fd to libusb_set_android_open_callback

Signed-off-by: Anton Lundin <glance@acc.umu.se>
Signed-off-by: Dirk Hohndel <dirk@hohndel.org>
This commit is contained in:
Anton Lundin 2015-08-21 00:19:40 +02:00 committed by Dirk Hohndel
parent b629088dfa
commit 9ddc21a476

View file

@ -5,6 +5,8 @@
#include <sys/types.h> #include <sys/types.h>
#include <dirent.h> #include <dirent.h>
#include <fcntl.h> #include <fcntl.h>
#include <libusb.h>
#include <errno.h>
#include <QtAndroidExtras/QtAndroidExtras> #include <QtAndroidExtras/QtAndroidExtras>
#include <QtAndroidExtras/QAndroidJniObject> #include <QtAndroidExtras/QAndroidJniObject>
@ -19,9 +21,16 @@ const char android_system_divelist_default_font[] = "Roboto";
const char *system_divelist_default_font = android_system_divelist_default_font; const char *system_divelist_default_font = android_system_divelist_default_font;
double system_divelist_default_font_size = 8.0; double system_divelist_default_font_size = 8.0;
int get_usb_fd(uint16_t idVendor, uint16_t idProduct);
void subsurface_OS_pref_setup(void) void subsurface_OS_pref_setup(void)
{ {
// nothing // Abusing this function to get a decent place where we can wire in
// our open callback into libusb
#ifdef libusb_android_open_callback_func
libusb_set_android_open_callback(get_usb_fd);
#elif __ANDROID__
#error we need libusb_android_open_callback
#endif
} }
bool subsurface_ignore_font(const char *font) bool subsurface_ignore_font(const char *font)
@ -56,17 +65,11 @@ int enumerate_devices(device_callback_t callback, void *userdata, int dc_type)
} }
/** /**
* Get the file descriptor of first available ftdi device attached to usb in android. * Get the file descriptor of first available matching device attached to usb in android.
* This is needed for dc_device_open on android.
* *
* return * returns a fd to the device, or -1 and errno is set.
* -1 : No Usb Device is attached. */
* -2 : No ftdi device found. int get_usb_fd(uint16_t idVendor, uint16_t idProduct)
* -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; int i;
jint fd, vendorid, productid; jint fd, vendorid, productid;
@ -97,12 +100,13 @@ int get_usb_fd()
usbDevice = deviceMap.callObjectMethod ("get", "(Ljava/lang/Object;)Ljava/lang/Object;", usbName.object()); usbDevice = deviceMap.callObjectMethod ("get", "(Ljava/lang/Object;)Ljava/lang/Object;", usbName.object());
vendorid = usbDevice.callMethod<jint>("getVendorId", "()I"); vendorid = usbDevice.callMethod<jint>("getVendorId", "()I");
productid = usbDevice.callMethod<jint>("getProductId", "()I"); productid = usbDevice.callMethod<jint>("getProductId", "()I");
if(vendorid == FTDI_VID) // Found a FTDI device. Break. if(vendorid == idVendor && productid == idProduct) // Found the requested device
break; break;
} }
if (i == num_devices) { if (i == num_devices) {
// No ftdi device found. // No device found.
return -2; errno = ENOENT;
return -1;
} }
jboolean hasPermission = usbManager.callMethod<jboolean>("hasPermission", "(Landroid/hardware/usb/UsbDevice;)Z", usbDevice.object()); jboolean hasPermission = usbManager.callMethod<jboolean>("hasPermission", "(Landroid/hardware/usb/UsbDevice;)Z", usbDevice.object());
@ -110,22 +114,25 @@ int get_usb_fd()
// You do not have permission to use the usbDevice. // You do not have permission to use the usbDevice.
// Please remove and reinsert the USB device. // Please remove and reinsert the USB device.
// Could also give an dialogbox asking for permission. // Could also give an dialogbox asking for permission.
return -3; errno = EPERM;
return -1;
} }
// An FTDI device is present and we also have permission to use the device. // An device is present and we also have permission to use the device.
// Open the device and get its file descriptor. // Open the device and get its file descriptor.
QAndroidJniObject usbDeviceConnection = usbManager.callObjectMethod("openDevice", "(Landroid/hardware/usb/UsbDevice;)Landroid/hardware/usb/UsbDeviceConnection;", usbDevice.object()); QAndroidJniObject usbDeviceConnection = usbManager.callObjectMethod("openDevice", "(Landroid/hardware/usb/UsbDevice;)Landroid/hardware/usb/UsbDeviceConnection;", usbDevice.object());
if (usbDeviceConnection.object() == NULL) { if (usbDeviceConnection.object() == NULL) {
// Some error occurred while opening the device. Exit. // Some error occurred while opening the device. Exit.
return -4; errno = EINVAL;
return -1;
} }
// Finally get the required file descriptor. // Finally get the required file descriptor.
fd = usbDeviceConnection.callMethod<jint>("getFileDescriptor", "()I"); fd = usbDeviceConnection.callMethod<jint>("getFileDescriptor", "()I");
if (fd == -1) { if (fd == -1) {
// The device is not opened. Some error. // The device is not opened. Some error.
return -4; errno = ENODEV;
return -1;
} }
return fd; return fd;
} }