]> sigrok.org Git - sigrok-util.git/blob - cross-compile/android/libusb-1.0.patch
sigrok-cross-android: READE: Add missing dependencies.
[sigrok-util.git] / cross-compile / android / libusb-1.0.patch
1 diff -urp libusb-1.0.9.orig/libusb/io.c libusb-1.0.9/libusb/io.c
2 --- libusb-1.0.9.orig/libusb/io.c       2013-08-28 14:33:15.000000000 +0200
3 +++ libusb-1.0.9/libusb/io.c    2013-08-28 14:31:25.000000000 +0200
4 @@ -36,6 +36,14 @@
5  
6  #include "libusbi.h"
7  
8 +#ifndef TIMESPEC_TO_TIMEVAL
9 +#define TIMESPEC_TO_TIMEVAL(tv, ts)                                     \
10 +        do {                                                            \
11 +                (tv)->tv_sec = (ts)->tv_sec;                            \
12 +                (tv)->tv_usec = (ts)->tv_nsec / 1000;                   \
13 +        } while (0)
14 +#endif
15 +
16  /**
17   * \page io Synchronous and asynchronous device I/O
18   *
19 diff -urp libusbx-1.0.17.orig/libusb/os/linux_usbfs.c libusbx-1.0.17/libusb/os/linux_usbfs.c
20 --- libusbx-1.0.17.orig/libusb/os/linux_usbfs.c 2014-07-05 23:14:45.487803220 +0200
21 +++ libusbx-1.0.17/libusb/os/linux_usbfs.c      2014-07-06 16:42:17.013481236 +0200
22 @@ -37,11 +37,21 @@
23  #include <sys/types.h>
24  #include <sys/utsname.h>
25  #include <unistd.h>
26 +#ifdef __ANDROID__
27 +#include <jni.h>
28 +#endif
29  
30  #include "libusb.h"
31  #include "libusbi.h"
32  #include "linux_usbfs.h"
33  
34 +#ifdef __ANDROID__
35 +static JavaVM *g_jvm = NULL;
36 +static jclass usb_helper_class;
37 +static jmethodID usb_helper_open_mid;
38 +static int usb_helper_open(const char *pathname, int flags);
39 +#endif
40 +
41  /* sysfs vs usbfs:
42   * opening a usbfs node causes the device to be resumed, so we attempt to
43   * avoid this during enumeration.
44 @@ -192,6 +202,11 @@
45                 snprintf(path, PATH_MAX, "%s/%03d/%03d",
46                         usbfs_path, dev->bus_number, dev->device_address);
47  
48 +#ifdef __ANDROID__
49 +       if (g_jvm)
50 +               fd = usb_helper_open(path, mode);
51 +       else
52 +#endif
53         fd = open(path, mode);
54         if (fd != -1)
55                 return fd; /* Success */
56 @@ -2603,3 +2618,79 @@
57         .transfer_priv_size = sizeof(struct linux_transfer_priv),
58         .add_iso_packet_size = 0,
59  };
60 +
61 +#ifdef __ANDROID__
62 +jint JNI_OnLoad(JavaVM *vm, void *reserved)
63 +{
64 +       JNIEnv* env;
65 +       g_jvm = vm;
66 +       if ((*vm)->GetEnv(vm, (void **)&env, JNI_VERSION_1_6) != JNI_OK) {
67 +               return -1;
68 +       }
69 +
70 +       jclass helper = (*env)->FindClass(env, "org/sigrok/androidutils/UsbHelper");
71 +
72 +       if (helper) {
73 +
74 +               usb_helper_class = (jclass)(*env)->NewGlobalRef(env, helper);
75 +
76 +               usb_helper_open_mid = (*env)->GetStaticMethodID(env, helper, "open",
77 +                                                               "(Ljava/lang/String;I)I");
78 +               (*env)->DeleteLocalRef(env, helper);
79 +
80 +       } else {
81 +               if ((*env)->ExceptionCheck(env)) {
82 +                       (*env)->ExceptionClear(env);
83 +               }
84 +               g_jvm = NULL;
85 +       }
86 +
87 +       return JNI_VERSION_1_6;
88 +}
89 +
90 +void JNI_OnUnload(JavaVM *vm, void *reserved)
91 +{
92 +       JNIEnv* env;
93 +       if ((*vm)->GetEnv(vm, (void **)&env, JNI_VERSION_1_6) != JNI_OK) {
94 +               return;
95 +       }
96 +
97 +       jclass helper = usb_helper_class;
98 +       usb_helper_class = NULL;
99 +       if (helper)
100 +               (*env)->DeleteGlobalRef(env, helper);
101 +       g_jvm = NULL;
102 +}
103 +
104 +static int usb_helper_open(const char *pathname, int flags)
105 +{
106 +       JNIEnv* env;
107 +       int res;
108 +       jint st;
109 +       int do_detach = 0;
110 +
111 +       if (g_jvm == NULL) {
112 +               return -1;
113 +       }
114 +       st = (*g_jvm)->GetEnv(g_jvm, (void **)&env, JNI_VERSION_1_6);
115 +
116 +       if (st == JNI_EDETACHED) {
117 +               st = (*g_jvm)->AttachCurrentThread(g_jvm, &env, NULL);
118 +               do_detach = 1;
119 +       }
120 +
121 +       if (st != JNI_OK) {
122 +               return -1;
123 +       }
124 +
125 +       jstring string = (*env)->NewStringUTF(env, pathname);
126 +       res = (*env)->CallStaticIntMethod(env, usb_helper_class, usb_helper_open_mid, string, (jint)flags);
127 +       (*env)->DeleteLocalRef(env, string);
128 +
129 +       if (do_detach) {
130 +               (*g_jvm)->DetachCurrentThread(g_jvm);
131 +       }
132 +
133 +       return res;
134 +}
135 +#endif