From f8553a1424aa170d67bab16b3c4a52cda8578bd8 Mon Sep 17 00:00:00 2001 From: Cheng Zhong Date: Mon, 25 Jan 2016 14:21:14 +0800 Subject: [PATCH] Removed non-C++ code Removed android specific files: - Android JNI bridges C++ and java codes (will move to a new repository) - Android library project and sample project - Build files for android --- android/build.gradle | 24 - android/jni/Android.mk | 15 + .../{library/src/main => }/jni/Application.mk | 0 android/{library/src/main => }/jni/glob.c | 0 .../{library/src/main => }/jni/include/glob.h | 0 .../src/main => }/jni/include/jni_utility.h | 0 .../src/main => }/jni/include/linux/serial.h | 0 .../main => }/jni/include/linux/tty_flags.h | 0 .../{library/src/main => }/jni/include/log.h | 0 .../{library/src/main => }/jni/include/serial | 0 .../src/main => }/jni/include/sys/signal.h | 0 .../src/main => }/jni/include/sysexits.h | 0 android/library/build.gradle | 49 - android/library/proguard-rules.pro | 17 - android/library/src/main/AndroidManifest.xml | 7 - .../src/main/java/serial/ByteSize.java | 50 - .../src/main/java/serial/FlowControl.java | 21 - android/library/src/main/java/serial/Log.java | 599 --------- .../library/src/main/java/serial/Parity.java | 31 - .../src/main/java/serial/PortInfo.java | 33 - .../java/serial/PortNotOpenedException.java | 27 - .../library/src/main/java/serial/Serial.java | 1055 --------------- .../src/main/java/serial/SerialException.java | 27 - .../main/java/serial/SerialIOException.java | 31 - .../src/main/java/serial/Stopbits.java | 43 - .../library/src/main/java/serial/Timeout.java | 77 -- android/library/src/main/jni/Android.mk | 25 - android/library/src/main/jni/build-jni.sh | 25 - android/library/src/main/jni/jni_main.cc | 60 - android/library/src/main/jni/jni_utility.cc | 278 ---- .../main/jni/libs/nativehelper/ALog-priv.h | 66 - .../src/main/jni/libs/nativehelper/Android.mk | 33 - .../main/jni/libs/nativehelper/JNIHelp.cpp | 341 ----- .../jni/libs/nativehelper/JniConstants.cpp | 141 -- .../jni/libs/nativehelper/JniInvocation.cpp | 166 --- .../libs/nativehelper/MODULE_LICENSE_APACHE2 | 0 .../src/main/jni/libs/nativehelper/NOTICE | 190 --- .../src/main/jni/libs/nativehelper/README | 11 - .../include/nativehelper/JNIHelp.h | 192 --- .../include/nativehelper/JniConstants.h | 100 -- .../include/nativehelper/JniInvocation.h | 66 - .../include/nativehelper/ScopedBytes.h | 81 -- .../include/nativehelper/ScopedFd.h | 60 - .../include/nativehelper/ScopedLocalFrame.h | 40 - .../include/nativehelper/ScopedLocalRef.h | 63 - .../nativehelper/ScopedPrimitiveArray.h | 122 -- .../include/nativehelper/ScopedStringChars.h | 74 -- .../include/nativehelper/ScopedUtfChars.h | 71 - .../include/nativehelper/UniquePtr.h | 241 ---- .../nativehelper/include/nativehelper/jni.h | 1141 ----------------- .../include/nativehelper/toStringArray.h | 71 - .../jni/libs/nativehelper/tests/Android.mk | 35 - .../nativehelper/tests/JniInvocation_test.cpp | 144 --- .../jni/libs/nativehelper/toStringArray.cpp | 48 - .../library/src/main/jni/list_ports_linux.cc | 1 - android/library/src/main/jni/serial.cc | 1 - android/library/src/main/jni/serial_jni.cc | 466 ------- android/library/src/main/jni/serial_unix.cc | 1 - .../java/serial/android/ExampleUnitTest.java | 15 - android/sample/build.gradle | 29 - android/sample/proguard-rules.pro | 17 - .../android/sample/ApplicationTest.java | 13 - android/sample/src/main/AndroidManifest.xml | 23 - .../serial/android/sample/MainActivity.java | 250 ---- .../src/main/res/layout/activity_main.xml | 27 - .../src/main/res/layout/content_main.xml | 144 --- .../sample/src/main/res/menu/menu_main.xml | 14 - .../src/main/res/mipmap-hdpi/ic_launcher.png | Bin 3418 -> 0 bytes .../src/main/res/mipmap-mdpi/ic_launcher.png | Bin 2206 -> 0 bytes .../src/main/res/mipmap-xhdpi/ic_launcher.png | Bin 4842 -> 0 bytes .../main/res/mipmap-xxhdpi/ic_launcher.png | Bin 7718 -> 0 bytes .../main/res/mipmap-xxxhdpi/ic_launcher.png | Bin 10486 -> 0 bytes .../sample/src/main/res/values-v21/styles.xml | 8 - .../src/main/res/values-w820dp/dimens.xml | 6 - .../sample/src/main/res/values-zh/strings.xml | 20 - android/sample/src/main/res/values/colors.xml | 6 - android/sample/src/main/res/values/dimens.xml | 6 - .../sample/src/main/res/values/strings.xml | 29 - android/sample/src/main/res/values/styles.xml | 17 - .../android/sample/ExampleUnitTest.java | 15 - android/settings.gradle | 1 - 81 files changed, 15 insertions(+), 7085 deletions(-) delete mode 100644 android/build.gradle create mode 100644 android/jni/Android.mk rename android/{library/src/main => }/jni/Application.mk (100%) rename android/{library/src/main => }/jni/glob.c (100%) rename android/{library/src/main => }/jni/include/glob.h (100%) rename android/{library/src/main => }/jni/include/jni_utility.h (100%) rename android/{library/src/main => }/jni/include/linux/serial.h (100%) rename android/{library/src/main => }/jni/include/linux/tty_flags.h (100%) rename android/{library/src/main => }/jni/include/log.h (100%) rename android/{library/src/main => }/jni/include/serial (100%) rename android/{library/src/main => }/jni/include/sys/signal.h (100%) rename android/{library/src/main => }/jni/include/sysexits.h (100%) delete mode 100644 android/library/build.gradle delete mode 100644 android/library/proguard-rules.pro delete mode 100644 android/library/src/main/AndroidManifest.xml delete mode 100644 android/library/src/main/java/serial/ByteSize.java delete mode 100644 android/library/src/main/java/serial/FlowControl.java delete mode 100644 android/library/src/main/java/serial/Log.java delete mode 100644 android/library/src/main/java/serial/Parity.java delete mode 100644 android/library/src/main/java/serial/PortInfo.java delete mode 100644 android/library/src/main/java/serial/PortNotOpenedException.java delete mode 100644 android/library/src/main/java/serial/Serial.java delete mode 100644 android/library/src/main/java/serial/SerialException.java delete mode 100644 android/library/src/main/java/serial/SerialIOException.java delete mode 100644 android/library/src/main/java/serial/Stopbits.java delete mode 100644 android/library/src/main/java/serial/Timeout.java delete mode 100644 android/library/src/main/jni/Android.mk delete mode 100755 android/library/src/main/jni/build-jni.sh delete mode 100644 android/library/src/main/jni/jni_main.cc delete mode 100644 android/library/src/main/jni/jni_utility.cc delete mode 100644 android/library/src/main/jni/libs/nativehelper/ALog-priv.h delete mode 100644 android/library/src/main/jni/libs/nativehelper/Android.mk delete mode 100644 android/library/src/main/jni/libs/nativehelper/JNIHelp.cpp delete mode 100644 android/library/src/main/jni/libs/nativehelper/JniConstants.cpp delete mode 100644 android/library/src/main/jni/libs/nativehelper/JniInvocation.cpp delete mode 100644 android/library/src/main/jni/libs/nativehelper/MODULE_LICENSE_APACHE2 delete mode 100644 android/library/src/main/jni/libs/nativehelper/NOTICE delete mode 100644 android/library/src/main/jni/libs/nativehelper/README delete mode 100644 android/library/src/main/jni/libs/nativehelper/include/nativehelper/JNIHelp.h delete mode 100644 android/library/src/main/jni/libs/nativehelper/include/nativehelper/JniConstants.h delete mode 100644 android/library/src/main/jni/libs/nativehelper/include/nativehelper/JniInvocation.h delete mode 100644 android/library/src/main/jni/libs/nativehelper/include/nativehelper/ScopedBytes.h delete mode 100644 android/library/src/main/jni/libs/nativehelper/include/nativehelper/ScopedFd.h delete mode 100644 android/library/src/main/jni/libs/nativehelper/include/nativehelper/ScopedLocalFrame.h delete mode 100644 android/library/src/main/jni/libs/nativehelper/include/nativehelper/ScopedLocalRef.h delete mode 100644 android/library/src/main/jni/libs/nativehelper/include/nativehelper/ScopedPrimitiveArray.h delete mode 100644 android/library/src/main/jni/libs/nativehelper/include/nativehelper/ScopedStringChars.h delete mode 100644 android/library/src/main/jni/libs/nativehelper/include/nativehelper/ScopedUtfChars.h delete mode 100644 android/library/src/main/jni/libs/nativehelper/include/nativehelper/UniquePtr.h delete mode 100644 android/library/src/main/jni/libs/nativehelper/include/nativehelper/jni.h delete mode 100644 android/library/src/main/jni/libs/nativehelper/include/nativehelper/toStringArray.h delete mode 100644 android/library/src/main/jni/libs/nativehelper/tests/Android.mk delete mode 100644 android/library/src/main/jni/libs/nativehelper/tests/JniInvocation_test.cpp delete mode 100644 android/library/src/main/jni/libs/nativehelper/toStringArray.cpp delete mode 120000 android/library/src/main/jni/list_ports_linux.cc delete mode 120000 android/library/src/main/jni/serial.cc delete mode 100644 android/library/src/main/jni/serial_jni.cc delete mode 120000 android/library/src/main/jni/serial_unix.cc delete mode 100644 android/library/src/test/java/serial/android/ExampleUnitTest.java delete mode 100644 android/sample/build.gradle delete mode 100644 android/sample/proguard-rules.pro delete mode 100644 android/sample/src/androidTest/java/serial/android/sample/ApplicationTest.java delete mode 100644 android/sample/src/main/AndroidManifest.xml delete mode 100644 android/sample/src/main/java/serial/android/sample/MainActivity.java delete mode 100644 android/sample/src/main/res/layout/activity_main.xml delete mode 100644 android/sample/src/main/res/layout/content_main.xml delete mode 100644 android/sample/src/main/res/menu/menu_main.xml delete mode 100644 android/sample/src/main/res/mipmap-hdpi/ic_launcher.png delete mode 100644 android/sample/src/main/res/mipmap-mdpi/ic_launcher.png delete mode 100644 android/sample/src/main/res/mipmap-xhdpi/ic_launcher.png delete mode 100644 android/sample/src/main/res/mipmap-xxhdpi/ic_launcher.png delete mode 100644 android/sample/src/main/res/mipmap-xxxhdpi/ic_launcher.png delete mode 100644 android/sample/src/main/res/values-v21/styles.xml delete mode 100644 android/sample/src/main/res/values-w820dp/dimens.xml delete mode 100644 android/sample/src/main/res/values-zh/strings.xml delete mode 100644 android/sample/src/main/res/values/colors.xml delete mode 100644 android/sample/src/main/res/values/dimens.xml delete mode 100644 android/sample/src/main/res/values/strings.xml delete mode 100644 android/sample/src/main/res/values/styles.xml delete mode 100644 android/sample/src/test/java/serial/android/sample/ExampleUnitTest.java delete mode 100644 android/settings.gradle diff --git a/android/build.gradle b/android/build.gradle deleted file mode 100644 index e17542c..0000000 --- a/android/build.gradle +++ /dev/null @@ -1,24 +0,0 @@ -// Top-level build file where you can add configuration options common to all sub-projects/modules. - -buildscript { - repositories { - jcenter() - } - dependencies { - classpath 'com.android.tools.build:gradle:1.5.0' - //classpath "com.android.tools.build:gradle-experimental:0.6.0-alpha5" - - // NOTE: Do not place your application dependencies here; they belong - // in the individual module build.gradle files - } -} - -allprojects { - repositories { - jcenter() - } -} - -task clean(type: Delete) { - delete rootProject.buildDir -} diff --git a/android/jni/Android.mk b/android/jni/Android.mk new file mode 100644 index 0000000..e4b26de --- /dev/null +++ b/android/jni/Android.mk @@ -0,0 +1,15 @@ +LOCAL_PATH := $(call my-dir) + +include $(CLEAR_VARS) + +LOCAL_MODULE := serial +LOCAL_C_INCLUDES := \ + $(LOCAL_PATH)/include\ + $(LOCAL_PATH)/../../include + +LOCAL_SRC_FILES := glob.c \ + ../../src/serial.cc \ + ../../src/impl/unix.cc \ + ../../src/impl/list_ports/list_ports_linux.cc + +include $(BUILD_SHARED_LIBRARY) diff --git a/android/library/src/main/jni/Application.mk b/android/jni/Application.mk similarity index 100% rename from android/library/src/main/jni/Application.mk rename to android/jni/Application.mk diff --git a/android/library/src/main/jni/glob.c b/android/jni/glob.c similarity index 100% rename from android/library/src/main/jni/glob.c rename to android/jni/glob.c diff --git a/android/library/src/main/jni/include/glob.h b/android/jni/include/glob.h similarity index 100% rename from android/library/src/main/jni/include/glob.h rename to android/jni/include/glob.h diff --git a/android/library/src/main/jni/include/jni_utility.h b/android/jni/include/jni_utility.h similarity index 100% rename from android/library/src/main/jni/include/jni_utility.h rename to android/jni/include/jni_utility.h diff --git a/android/library/src/main/jni/include/linux/serial.h b/android/jni/include/linux/serial.h similarity index 100% rename from android/library/src/main/jni/include/linux/serial.h rename to android/jni/include/linux/serial.h diff --git a/android/library/src/main/jni/include/linux/tty_flags.h b/android/jni/include/linux/tty_flags.h similarity index 100% rename from android/library/src/main/jni/include/linux/tty_flags.h rename to android/jni/include/linux/tty_flags.h diff --git a/android/library/src/main/jni/include/log.h b/android/jni/include/log.h similarity index 100% rename from android/library/src/main/jni/include/log.h rename to android/jni/include/log.h diff --git a/android/library/src/main/jni/include/serial b/android/jni/include/serial similarity index 100% rename from android/library/src/main/jni/include/serial rename to android/jni/include/serial diff --git a/android/library/src/main/jni/include/sys/signal.h b/android/jni/include/sys/signal.h similarity index 100% rename from android/library/src/main/jni/include/sys/signal.h rename to android/jni/include/sys/signal.h diff --git a/android/library/src/main/jni/include/sysexits.h b/android/jni/include/sysexits.h similarity index 100% rename from android/library/src/main/jni/include/sysexits.h rename to android/jni/include/sysexits.h diff --git a/android/library/build.gradle b/android/library/build.gradle deleted file mode 100644 index ed3d07e..0000000 --- a/android/library/build.gradle +++ /dev/null @@ -1,49 +0,0 @@ -apply plugin: 'com.android.library' -//apply plugin: 'com.android.model.library' - -android { - compileSdkVersion 23 - buildToolsVersion "23.0.2" - - //useDeprecatedNdk true - - /* - ndk { - moduleName "serial" - } - */ - - sourceSets { - main { - jni.srcDirs = [] //disable automatic ndk-build call - } - } - - defaultConfig { - minSdkVersion 16 - targetSdkVersion 23 - versionCode 1 - versionName "1.0" - } - - buildTypes { - release { - minifyEnabled false - proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro' - } - debug { - jniDebuggable true - } - } - dexOptions { - incremental true - } - compileOptions { - sourceCompatibility JavaVersion.VERSION_1_7 - targetCompatibility JavaVersion.VERSION_1_7 - } -} - -dependencies { - compile fileTree(dir: 'libs', include: ['*.jar']) -} diff --git a/android/library/proguard-rules.pro b/android/library/proguard-rules.pro deleted file mode 100644 index 9aa02b1..0000000 --- a/android/library/proguard-rules.pro +++ /dev/null @@ -1,17 +0,0 @@ -# Add project specific ProGuard rules here. -# By default, the flags in this file are appended to flags specified -# in /mnt/android/android-sdk-linux_x86/tools/proguard/proguard-android.txt -# You can edit the include path and order by changing the proguardFiles -# directive in build.gradle. -# -# For more details, see -# http://developer.android.com/guide/developing/tools/proguard.html - -# Add any project specific keep options here: - -# If your project uses WebView with JS, uncomment the following -# and specify the fully qualified class name to the JavaScript interface -# class: -#-keepclassmembers class fqcn.of.javascript.interface.for.webview { -# public *; -#} diff --git a/android/library/src/main/AndroidManifest.xml b/android/library/src/main/AndroidManifest.xml deleted file mode 100644 index 2896a14..0000000 --- a/android/library/src/main/AndroidManifest.xml +++ /dev/null @@ -1,7 +0,0 @@ - - - - - diff --git a/android/library/src/main/java/serial/ByteSize.java b/android/library/src/main/java/serial/ByteSize.java deleted file mode 100644 index 0856465..0000000 --- a/android/library/src/main/java/serial/ByteSize.java +++ /dev/null @@ -1,50 +0,0 @@ -package serial; - -/** - * Enumeration defines the possible bytesizes for the serial port. - */ -public enum ByteSize { - /** - * Use 5 bits in a byte. - */ - FiveBits(5), - /** - * Use 6 bits in a byte. - */ - SixBits(6), - /** - * Use 7 bits in a byte. - */ - SevenBits(7), - /** - * Use 8 bits in a byte. - * - * This is the default. - */ - EightBits(8); - - /** - * Number of bits in a single byte. - */ - public final int bitLength; - - ByteSize(int bitLength) { - this.bitLength = bitLength; - } - - static ByteSize fromValue(int value) { - switch (value) { - case 5: - return FiveBits; - case 6: - return SixBits; - case 7: - return SevenBits; - case 8: - return EightBits; - default: - throw new IllegalArgumentException("Invalid value."); - } - } - -} diff --git a/android/library/src/main/java/serial/FlowControl.java b/android/library/src/main/java/serial/FlowControl.java deleted file mode 100644 index c5417b5..0000000 --- a/android/library/src/main/java/serial/FlowControl.java +++ /dev/null @@ -1,21 +0,0 @@ -package serial; - -/** - * Enumeration defines the possible flowcontrol types for the serial port. - */ -public enum FlowControl { - /** - * Use none flow control. - * - * This is the default. - */ - None, - /** - * Use software flow control. - */ - Software, - /** - * Use hardware flow control. - */ - Hardware; -} diff --git a/android/library/src/main/java/serial/Log.java b/android/library/src/main/java/serial/Log.java deleted file mode 100644 index 8aed4c2..0000000 --- a/android/library/src/main/java/serial/Log.java +++ /dev/null @@ -1,599 +0,0 @@ -package serial; - -import android.text.TextUtils; - -import java.io.PrintWriter; -import java.io.StringWriter; -import java.lang.reflect.Method; -import java.util.Locale; - -import serial.android.BuildConfig; - -/** - * Log replacement for {@link android.util.Log}. - * - * @author chzhong - */ -public final class Log { - - /** - * - */ - private static final String MESSAGE_TEMPLATE = "[%s]%s"; - - private static Object sSyncObject = new Object(); - - /** - * Priority constant for enable all loggings. - */ - public static final int ALL = -1; - - /** - * Priority constant for {@link #println(int, String, String)} or - * {@link #setFilterLevel(int)} methods; use Log.v. - */ - public static final int VERBOSE = 2; - - /** - * Priority constant for the {@link #println(int, String, String)} or - * {@link #setFilterLevel(int)} method; use Log.d. - */ - public static final int DEBUG = 3; - - /** - * Priority constant for the {@link #println(int, String, String)} or - * {@link #setFilterLevel(int)} method; use Log.i. - */ - public static final int INFO = 4; - - /** - * Priority constant for the {@link #println(int, String, String)} or - * {@link #setFilterLevel(int)} method; use Log.w. - */ - public static final int WARN = 5; - - /** - * Priority constant for the {@link #println(int, String, String)} or - * {@link #setFilterLevel(int)} method; use Log.e. - */ - public static final int ERROR = 6; - - /** - * Priority constant for the {@link #println(int, String, String)} or - * {@link #setFilterLevel(int)} method. - */ - public static final int ASSERT = 7; - - /** - * Priority constant for disable all loggings. - */ - public static final int NONE = Integer.MAX_VALUE; - - /** - * Filter level of logs. Only levels greater or equals this level will be - * output to LogCat. - */ - private static int sFilterLevel = BuildConfig.DEBUG ? ALL : WARN; - - private static String sApplicationTag; - - /** - * Set the default tag for this application. - * - * @param tag The tag of the application. - */ - public static void setApplicationTag(String tag) { - sApplicationTag = tag; - } - - /** - * Gets the default tag of the application. - * - * @return The default tag of the application. - */ - public static String getApplicationTag() { - return sApplicationTag; - } - - /** - * Sets the filter level of logs. Only levels greater or equals this level - * will be output to LogCat. - * - * @param level The filter level. - */ - public static void setFilterLevel(int level) { - synchronized (sSyncObject) { - sFilterLevel = level; - } - } - - /** - * Gets the filter level of logs. Only levels greater or equals this level - * will be output to LogCat. - * - * @return Current filter level. - */ - public static int getFilterLevel() { - return sFilterLevel; - } - - private Log() { - } - - /** - * Send a {@link #VERBOSE} log message. - * - * @param msg The message you would like logged. - */ - public static int v(String msg) { - return println(VERBOSE, null, msg); - } - - /** - * Send a {@link #VERBOSE} log message. - * - * @param tag Used to identify the source of a log message. It usually - * identifies the class or activity where the log call occurs. - * @param msg The message you would like logged. - */ - public static int v(String tag, String msg) { - return println(VERBOSE, tag, msg); - } - - /** - * Send a {@link #VERBOSE} log message. - * - * @param tag Used to identify the source of a log message. It usually - * identifies the class or activity where the log call occurs. - * @param format The format of the message you would like logged. - * @param args The arguments used to format the message. - */ - public static int v(String tag, String format, Object... args) { - if (VERBOSE < sFilterLevel) - return 0; - String msg = formatString(format, args); - return println(VERBOSE, tag, msg); - } - - /** - * Send a {@link #VERBOSE} log message and log the exception. - * - * @param tag Used to identify the source of a log message. It usually - * identifies the class or activity where the log call occurs. - * @param msg The message you would like logged. - * @param tr An exception to log - */ - public static int v(String tag, String msg, Throwable tr) { - return println(VERBOSE, tag, msg + '\n' + getStackTraceString(tr)); - } - - /** - * Send a {@link #DEBUG} log message. - * - * @param msg The message you would like logged. - */ - public static int d(String msg) { - return println(DEBUG, null, msg); - } - - /** - * Send a {@link #DEBUG} log message. - * - * @param tag Used to identify the source of a log message. It usually - * identifies the class or activity where the log call occurs. - * @param msg The message you would like logged. - */ - public static int d(String tag, String msg) { - return println(DEBUG, tag, msg); - } - - /** - * Send a {@link #DEBUG} log message. - * - * @param tag Used to identify the source of a log message. It usually - * identifies the class or activity where the log call occurs. - * @param format The format of the message you would like logged. - * @param args The arguments used to format the message. - */ - public static int d(String tag, String format, Object... args) { - if (DEBUG < sFilterLevel) - return 0; - String msg = formatString(format, args); - return println(DEBUG, tag, msg); - } - - /** - * Send a {@link #DEBUG} log message and log the exception. - * - * @param tag Used to identify the source of a log message. It usually - * identifies the class or activity where the log call occurs. - * @param msg The message you would like logged. - * @param tr An exception to log - */ - public static int d(String tag, String msg, Throwable tr) { - return println(DEBUG, tag, msg + '\n' + getStackTraceString(tr)); - } - - /** - * Send an {@link #INFO} log message. - * - * @param msg The message you would like logged. - */ - public static int i(String msg) { - return println(INFO, null, msg); - } - - /** - * Send an {@link #INFO} log message. - * - * @param tag Used to identify the source of a log message. It usually - * identifies the class or activity where the log call occurs. - * @param msg The message you would like logged. - */ - public static int i(String tag, String msg) { - return println(INFO, tag, msg); - } - - /** - * Send an {@link #INFO} log message. - * - * @param tag Used to identify the source of a log message. It usually - * identifies the class or activity where the log call occurs. - * @param format The format of the message you would like logged. - * @param args The arguments used to format the message. - */ - public static int i(String tag, String format, Object... args) { - if (INFO < sFilterLevel) - return 0; - String msg = formatString(format, args); - return println(INFO, tag, msg); - } - - /** - * Send a {@link #INFO} log message and log the exception. - * - * @param tag Used to identify the source of a log message. It usually - * identifies the class or activity where the log call occurs. - * @param msg The message you would like logged. - * @param tr An exception to log - */ - public static int i(String tag, String msg, Throwable tr) { - return println(INFO, tag, msg + '\n' + getStackTraceString(tr)); - } - - /** - * Send a {@link #WARN} log message. - * - * @param msg The message you would like logged. - */ - public static int w(String msg) { - return println(WARN, null, msg); - } - - /** - * Send a {@link #WARN} log message. - * - * @param tag Used to identify the source of a log message. It usually - * identifies the class or activity where the log call occurs. - * @param msg The message you would like logged. - */ - public static int w(String tag, String msg) { - return println(WARN, tag, msg); - } - - /** - * Send a {@link #WARN} log message. - * - * @param tag Used to identify the source of a log message. It usually - * identifies the class or activity where the log call occurs. - * @param format The format of the message you would like logged. - * @param args The arguments used to format the message. - */ - public static int w(String tag, String format, Object... args) { - if (WARN < sFilterLevel) - return 0; - String msg = formatString(format, args); - return println(WARN, tag, msg); - } - - /** - * Send a {@link #WARN} log message and log the exception. - * - * @param tag Used to identify the source of a log message. It usually - * identifies the class or activity where the log call occurs. - * @param msg The message you would like logged. - * @param tr An exception to log - */ - public static int w(String tag, String msg, Throwable tr) { - return println(WARN, tag, msg + '\n' + getStackTraceString(tr)); - } - - /** - * Checks to see whether or not a log for the specified tag is loggable at - * the specified level. The default level of any tag is set to INFO. This - * means that any level above and including INFO will be logged. Before you - * make any calls to a logging method you should check to see if your tag - * should be logged. You can change the default level by setting a system - * property: 'setprop log.tag.<YOUR_LOG_TAG> <LEVEL>' Where level is - * either VERBOSE, DEBUG, INFO, WARN, ERROR, ASSERT, or SUPPRESS. SUPRESS - * will turn off all logging for your tag. You can also create a local.prop - * file that with the following in it: - * 'log.tag.<YOUR_LOG_TAG>=<LEVEL>' and place that in - * /data/local.prop. - * - * @param tag The tag to check. - * @param level The level to check. - * @return Whether or not that this is allowed to be logged. - * @throws IllegalArgumentException is thrown if the tag.length() > 23. - */ - public static boolean isLoggable(String tag, int level) { - return android.util.Log.isLoggable(tag, level); - } - - /* - * Send a {@link #WARN} log message and log the exception. - * @param tag Used to identify the source of a log message. It usually - * identifies the class or activity where the log call occurs. - * @param tr An exception to log - */ - public static int w(String tag, Throwable tr) { - return println(WARN, tag, getStackTraceString(tr)); - } - - public static int w(Throwable tr) { - return println(WARN, null, getStackTraceString(tr)); - } - - /** - * Send an {@link #ERROR} log message. - * - * @param tag Used to identify the source of a log message. It usually - * identifies the class or activity where the log call occurs. - * @param format The format of the message you would like logged. - * @param args The arguments used to format the message. - */ - public static int e(String tag, String format, Object... args) { - if (ERROR < sFilterLevel) - return 0; - String msg = formatString(format, args); - return println(ERROR, tag, msg); - } - - private static String formatString(String format, Object... args) { - try { - return String.format(Locale.US, format, args); - } catch (Exception e) { - StringBuilder builder = new StringBuilder(); - builder.append(format); - for (Object arg : args) { - builder.append(arg.toString()); - } - return builder.toString(); - } - } - - /** - * Send an {@link #ERROR} log message. - * - * @param msg The message you would like logged. - */ - public static int e(String msg) { - return println(ERROR, null, msg); - } - - /** - * Send an {@link #ERROR} log message. - * - * @param tr The throwable you would like logged. - */ - public static int e(Throwable tr) { - return println(ERROR, null, getStackTraceString(tr)); - } - - /** - * Send an {@link #ERROR} log message. - * - * @param tag Used to identify the source of a log message. It usually - * identifies the class or activity where the log call occurs. - * @param msg The message you would like logged. - */ - public static int e(String tag, String msg) { - return println(ERROR, tag, msg); - } - - /** - * Send a {@link #ERROR} log message and log the exception. - * - * @param msg The message you would like logged. - * @param tr An exception to log - */ - public static int e(String msg, Throwable tr) { - return println(ERROR, null, msg + '\n' + getStackTraceString(tr)); - } - - /** - * Send a {@link #ERROR} log message and log the exception. - * - * @param tag Used to identify the source of a log message. It usually - * identifies the class or activity where the log call occurs. - * @param msg The message you would like logged. - * @param tr An exception to log - */ - public static int e(String tag, String msg, Throwable tr) { - return println(ERROR, tag, msg + '\n' + getStackTraceString(tr)); - } - - /** - * Handy function to get a loggable stack trace from a Throwable - * - * @param tr An exception to log - */ - public static String getStackTraceString(Throwable tr) { - if (tr == null) { - return ""; - } - StringWriter sw = new StringWriter(); - PrintWriter pw = new PrintWriter(sw); - tr.printStackTrace(pw); - return sw.toString(); - } - - /** - * Low-level logging call. - * - * @param priority The priority/type of this log message - * @param tag Used to identify the source of a log message. It usually - * identifies the class or activity where the log call occurs. - * @param msg The message you would like logged. - * @return The number of bytes written. - */ - public static int println(int priority, String tag, String msg) { - msg = (msg == null) ? "" : msg; - - int result = 0; - if (priority >= sFilterLevel) { - if (TextUtils.isEmpty(sApplicationTag) || sApplicationTag.equals(tag)) { - result = android.util.Log.println(priority, tag, msg); - } else if (TextUtils.isEmpty(tag) && !TextUtils.isEmpty(sApplicationTag)) { - result = android.util.Log.println(priority, sApplicationTag, msg); - } else { - String message = formatString(MESSAGE_TEMPLATE, tag, msg); - result = android.util.Log.println(priority, sApplicationTag, message); - } - } - return result; - } - - public static void myAssert(boolean condition, String message) { - if (sFilterLevel == ALL && !condition) { - throw new AssertionError(message); - } - } - - /** - * Return the {@link StackTraceElement} for current method's caller. - * - *

For example:

- *
-     * void foo() {
-     *     bar();
-     * }
-     *
-     * void bar() {
-     *     StackTraceElement caller = getCallerFrame();
-     *
-     *     // Will print "foo".
-     *     System.println(caller.getMethodName());
-     * }
-     * 
- * - *

Might be {@code null}. If you call this from static void main(String[]). - * - * @return the stack frame of current method's caller. - */ - public static StackTraceElement getCallerFrame() { - /* - * traces = { - * nativeGetStackTrace, - * Thread.getStackTrace, - * getStackFrameAt(2), - * getCurrentFrame, ... [3] - * , ... [3 + 1] - * , ... [3 + 2] - * ... - * } - */ - return getStackFrameAt(2); - } - - /** - * Return the {@link StackTraceElement} for current method. - * - *

For example:

- *
-     * void foo() {
-     *     bar();
-     * }
-     *
-     * void bar() {
-     *     StackTraceElement current = getCurrentFrame();
-     *
-     *     // Will print "bar".
-     *     System.println(current.getMethodName());
-     * }
-     * 
- * - * @return the stack frame of current method's caller. - */ - public static StackTraceElement getCurrentFrame() { - /* - * traces = { - * nativeGetStackTrace, - * Thread.getStackTrace, - * getStackFrameAt(1), - * getCurrentFrame, ... [3] - * , ... [3 + 1] - * ... - * } - */ - return getStackFrameAt(1); - } - - /** - * Return specified {@link StackTraceElement} on the call stack, relative to current method. - * - *

For example:

- *
-     * void main() {
-     *     stub();
-     * }
-     *
-     * void stub() {
-     *     foo();
-     * }
-     * void foo() {
-     *     bar();
-     * }
-     *
-     * void bar() {
-     *     // Will print "bar"
-     *     System.println(getStackFrameAt(0).getMethodName());
-     *     // Will print "foo"
-     *     System.println(getStackFrameAt(1).getMethodName());
-     *     // Will print "stub"
-     *     System.println(getStackFrameAt(2).getMethodName());
-     *     // Will print "main"
-     *     System.println(getStackFrameAt(3).getMethodName());
-     *     // Will print "null"
-     *     System.println(getStackFrameAt(4));
-     *     System.println(getStackFrameAt(-1));
-     * }
-     * 
- * - * @param offset the offset of the call stack from current method. 0 means current methods, 1 means caller, etc. - * @return the stack frame of current method's caller. - */ - public static StackTraceElement getStackFrameAt(int offset) { - if (offset < 0) { - return null; - } - /* - * traces = { - * nativeGetStackTrace, - * Thread.getStackTrace, - * getStackFrameAt(offset), - * , ... [3 + 0], offset 0 - * , ... [3 + 1], offset 1 - * , ... [3 + 2], offset 2 - * , ... [3 + 3], offset 3 - * ... - * } - */ - StackTraceElement[] traces = Thread.currentThread().getStackTrace(); - if (traces.length < 4 + offset) { - // The stack at offset is underflow... - return null; - } - return traces[3 + offset]; - } -} diff --git a/android/library/src/main/java/serial/Parity.java b/android/library/src/main/java/serial/Parity.java deleted file mode 100644 index b262a5f..0000000 --- a/android/library/src/main/java/serial/Parity.java +++ /dev/null @@ -1,31 +0,0 @@ -package serial; - -/** - * Enumeration defines the possible parity types for the serial port. - */ -public enum Parity { - /** - * Use none parity type. - * - * This is the default. - */ - None, - /** - * Use odd parity types - */ - Odd, - /** - * Use even parity type - */ - Even, - /** - * Use mark parity type. - * - */ - Mark, - /** - * Use space parity type. - * - */ - Space; -} diff --git a/android/library/src/main/java/serial/PortInfo.java b/android/library/src/main/java/serial/PortInfo.java deleted file mode 100644 index 97481b2..0000000 --- a/android/library/src/main/java/serial/PortInfo.java +++ /dev/null @@ -1,33 +0,0 @@ -package serial; - -/** - * Created by chzhong on 1/20/16. - */ -public final class PortInfo { - - /** - * Address of the serial port (this can be passed to the constructor of Serial). - */ - public final String port; - - /** - * Human readable description of serial device if available. - */ - public final String description; - - /** - * Hardware ID (e.g. VID:PID of USB serial devices) or "n/a" if not available. - */ - public final String hardwareId; - - PortInfo(String port, String description, String hardwareId) { - this.port = port; - this.description = description; - this.hardwareId = hardwareId; - } - - @Override - public String toString() { - return String.format("%s (%s)", description, port); - } -} diff --git a/android/library/src/main/java/serial/PortNotOpenedException.java b/android/library/src/main/java/serial/PortNotOpenedException.java deleted file mode 100644 index 2277216..0000000 --- a/android/library/src/main/java/serial/PortNotOpenedException.java +++ /dev/null @@ -1,27 +0,0 @@ -package serial; - -import java.util.Locale; - -/** - * Created by chzhong on 1/20/16. - */ -public class PortNotOpenedException extends IllegalStateException { - - static final String formatMessage(String message, StackTraceElement frame) { - return String.format(Locale.getDefault(), "%s. (%s.%s @ %s:%d)", - message, frame.getClassName(), frame.getMethodName(), - frame.getFileName(), frame.getLineNumber()); - } - - /** - * Constructs a new {@code PortNotOpenedException} with the current stack trace - * and the specified detail message. - * - * @param detailMessage the detail message for this exception. - * @param frame the frame of the wrong call. - */ - PortNotOpenedException(String detailMessage, StackTraceElement frame) { - super(formatMessage(detailMessage, frame)); - } - -} diff --git a/android/library/src/main/java/serial/Serial.java b/android/library/src/main/java/serial/Serial.java deleted file mode 100644 index f3b9ece..0000000 --- a/android/library/src/main/java/serial/Serial.java +++ /dev/null @@ -1,1055 +0,0 @@ -package serial; - -import java.io.Closeable; -import java.io.IOException; -import java.nio.ByteBuffer; -import java.nio.CharBuffer; -import java.nio.charset.Charset; -import java.util.regex.Pattern; - -/** - * Serial Port. - * - * Created by chzhong on 1/20/16. - */ -public class Serial implements Closeable { - - /** - * Baudrate 110 bits/s. - */ - public static final int BAUDRATE_110 = 110; - /** - * Baudrate 300 bits/s. - */ - public static final int BAUDRATE_300 = 300; - /** - * Baudrate 600 bits/s. - */ - public static final int BAUDRATE_600 = 600; - /** - * Baudrate 1200 bits/s. - */ - public static final int BAUDRATE_1200 = 1200; - /** - * Baudrate 2400 bits/s. - */ - public static final int BAUDRATE_2400 = 2400; - /** - * Baudrate 4800 bits/s. - */ - public static final int BAUDRATE_4800 = 4800; - /** - * Baudrate 9600 bits/s. This is the default. - */ - public static final int BAUDRATE_9600 = 9600; - /** - * Baudrate 14400 bits/s. - */ - public static final int BAUDRATE_14400 = 14400; - /** - * Baudrate 28800 bits/s. - */ - public static final int BAUDRATE_28800 = 28800; - /** - * Baudrate 38400 bits/s. - */ - public static final int BAUDRATE_38400 = 38400; - /** - * Baudrate 56000 bits/s. - */ - public static final int BAUDRATE_56000 = 56000; - /** - * Baudrate 57600 bits/s. - */ - public static final int BAUDRATE_57600 = 57600; - /** - * Baudrate 115200 bits/s. - */ - public static final int BAUDRATE_115200 = 115200; - /** - * Baudrate 128000 bits/s. - */ - public static final int BAUDRATE_128000 = 128000; - /** - * Baudrate 153600 bits/s. - */ - public static final int BAUDRATE_153600 = 153600; - /** - * Baudrate 230400 bits/s. - */ - public static final int BAUDRATE_230400 = 230400; - /** - * Baudrate 256000 bits/s. - */ - public static final int BAUDRATE_256000 = 256000; - /** - * Baudrate 460800 bits/s. - */ - public static final int BAUDRATE_460800 = 460800; - /** - * Baudrate 921600 bits/s. - */ - public static final int BAUDRATE_921600 = 921600; - - /** - * End-of-line: line-feed (\n). This is the default. - */ - public static final String EOL_LF = "\n"; - /** - * End-of-line: carriage-return (\n). - */ - public static final String EOL_CR = "\r"; - /** - * End-of-line: CR & LF (\r\n). - */ - public static final String EOL_CRLF = "\r\n"; - - /** - * Default charset. - */ - public static final Charset CHARSET_DEFAULT; - - /** - * UTF-8 charset. - */ - public static final Charset CHARSET_UTF8; - - /** - * ISO-8859-1 charset. - */ - public static final Charset CHARSET_ISO_8859_1; - - /** - * US-ASCII charset. - */ - public static final Charset CHARSET_US_ASCII; - - /** - * UTF-16 charset. - */ - public static final Charset CHARSET_UTF16; - - /** - * UTF-16LE charset. - */ - public static final Charset CHARSET_UTF16LE; - /** - * UTF-16BE charset. - */ - public static final Charset CHARSET_UTF16BE; - - - private static final Pattern FIELD_DELIM = Pattern.compile("\\t"); - - static { - CHARSET_DEFAULT = Charset.defaultCharset(); - CHARSET_UTF8 = Charset.forName("UTF-8"); - CHARSET_ISO_8859_1 = Charset.forName("ISO-8859-1"); - CHARSET_US_ASCII = Charset.forName("US-ASCII"); - CHARSET_UTF16 = Charset.forName("UTF-16"); - CHARSET_UTF16LE = Charset.forName("UTF-16LE"); - CHARSET_UTF16BE = Charset.forName("UTF-16BE"); - - System.loadLibrary("serial"); - } - /** - * List available ports on this device. - * - * @return an array of available ports. - */ - public static PortInfo[] listPorts() { - PortInfo[] ports; - String[] portDescs = native_listPorts(); - if (portDescs != null && portDescs.length > 0) { - ports = new PortInfo[portDescs.length]; - int index = 0; - for (String portDesc : portDescs) { - String[] parts = FIELD_DELIM.split(portDesc); - ports[index++] = new PortInfo(parts[0], parts[1], parts[2]); - } - } else { - ports = null; - } - return ports; - } - - /** - * Builder for {@link Serial}. - */ - public static class Builder { - - private String mPort; - private int mBaudrate = BAUDRATE_9600; - private Timeout mTimeout = Timeout.simpleTimeout(1000); - private ByteSize mBytesize = ByteSize.EightBits; - private Parity mParity = Parity.None; - private Stopbits mStopbits = Stopbits.One; - private FlowControl mFlowcontrol = FlowControl.None; - - /** - * Initial a {@link serial.Serial.Builder} for serial port. - * - */ - public Builder() { - } - - /** - * Initial a {@link serial.Serial.Builder} for serial port. - * - * @param port the path of the serial port. - */ - public Builder(String port) { - this.mPort = port; - } - - /** - * Initial a {@link serial.Serial.Builder} for serial port. - * - * @param port the path of the serial port. - * @param baudrate the baudrate to use. - */ - public Builder(String port, int baudrate) { - this.mPort = port; - this.mBaudrate = baudrate; - } - - /** - * @see Serial#setPort(String) - */ - public Builder setPort(String port) { - mPort = port; - return this; - } - - /** - * @see Serial#setBaudrate(int) - */ - public Builder setBaudrate(int baudrate) { - mBaudrate = baudrate; - return this; - } - - /** - * @see Serial#setTimeout(Timeout) - */ - public Builder setTimeout(Timeout timeout) { - mTimeout = timeout; - return this; - } - - /** - * @see Serial#setBytesize(ByteSize) - */ - public Builder setBytesize(ByteSize bytesize) { - mBytesize = bytesize; - return this; - } - - /** - * @see Serial#setParity(Parity) - */ - public Builder setParity(Parity parity) { - mParity = parity; - return this; - } - - /** - * @see Serial#setStopbits(Stopbits) - */ - public Builder setStopbits(Stopbits stopbits) { - mStopbits = stopbits; - return this; - } - - /** - * @see Serial#setFlowcontrol(FlowControl) - */ - public Builder setFlowcontrol(FlowControl flowcontrol) { - mFlowcontrol = flowcontrol; - return this; - } - - /** - * Create the serial object with give parameters. - * - * @return an instance of {@link Serial}. - * - * @throws SerialIOException I/O error. - */ - public Serial create() throws SerialIOException { - return new Serial(mPort, mBaudrate, mTimeout, mBytesize, mParity, mStopbits, mFlowcontrol); - } - } - - /** - * Creates a Serial object and opens the port if a port is specified, - * otherwise it remains closed until serial::Serial::open is called. - * - * @param port A string containing the address of the serial port, - * which would be something like 'COM1' on Windows and '/dev/ttyS0' - * on Linux. - * - * @param baudrate An unsigned 32-bit integer that represents the baudrate - * - * @param timeout A serial::Timeout struct that defines the timeout - * conditions for the serial port. \see serial::Timeout - * - * @\param bytesize Size of each byte in the serial transmission of data, - * default is eightbits, possible values are: fivebits, sixbits, sevenbits, - * eightbits - * - * @param parity Method of parity, default is parity_none, possible values - * are: parity_none, parity_odd, parity_even - * - * @param stopbits Number of stop bits used, default is stopbits_one, - * possible values are: stopbits_one, stopbits_one_point_five, stopbits_two - * - * @param flowcontrol Type of flowcontrol used, default is - * flowcontrol_none, possible values are: flowcontrol_none, - * flowcontrol_software, flowcontrol_hardware - * - * @throws SerialException Generic serial error. - * @throws SerialIOException I/O error. - * @throws IllegalArgumentException Invalid arguments are given. - */ - private Serial(String port, - int baudrate, - Timeout timeout, - ByteSize bytesize, - Parity parity, - Stopbits stopbits, - FlowControl flowcontrol) throws SerialIOException { - this.mTimeout = timeout; - this.mNativeSerial = native_create(port, baudrate, - new int[]{ - timeout.inter_byte_timeout, - timeout.read_timeout_constant, - timeout.read_timeout_multiplier, - timeout.write_timeout_constant, - timeout.read_timeout_multiplier - }, - bytesize.bitLength, parity.ordinal(), stopbits.value, flowcontrol.ordinal() - ); - if (mNativeSerial != 0) - this.mOpened = native_isOpen(mNativeSerial); - } - - private boolean mOpened; - private long mNativeSerial; - private Timeout mTimeout; - - @Override - protected void finalize() throws Throwable { - if (mNativeSerial != 0) { - native_close(mNativeSerial); - native_destory(mNativeSerial); - mNativeSerial = 0; - mOpened = false; - } - super.finalize(); - } - - private void checkValid() { - if (0 == mNativeSerial) { - /* - ... - serial.checkValid : 0 - serial.xxx : 1 - : 2 - */ - StackTraceElement frame = Log.getStackFrameAt(2); - String msg = PortNotOpenedException.formatMessage("Native port is invalid, please create another instance.", frame); - throw new IllegalStateException(msg); - } - } - - private void checkOpened() { - if (!mOpened) { - /* - ... - serial.checkOpened : 0 - serial.xxx : 1 - : 2 - */ - StackTraceElement frame = Log.getStackFrameAt(2); - throw new PortNotOpenedException("Native port is not opened.", frame); - } - } - - /** - * Determine whether this instance has a valid port. - * - * @return true if native port is valid, false otherwise. - */ - public boolean isValid() { - return mNativeSerial != 0; - } - - /** - * Opens the serial port as long as the port is set and the port isn't - * already open. - * - * If the port is provided to the constructor then an explicit call to open - * is not needed. - * - * @see #Serial(String, int, Timeout, ByteSize, Parity, Stopbits, FlowControl) - * - * @throws SerialException Generic serial error. - * @throws SerialIOException I/O error. - * @throws IllegalArgumentException Invalid arguments are given. - */ - public void open() throws SerialIOException { - if (mOpened) - return; - checkValid(); - native_open(mNativeSerial); - } - - - /** Gets the open status of the serial port. - * - * @return Returns true if the port is open, false otherwise. - */ - public boolean isOpen () { - checkValid(); - return native_isOpen(mNativeSerial); - } - - /** - * Closes the serial port. - * - * @throws IOException I/O error. - */ - public void close () throws IOException { - if (mNativeSerial == 0 || !mOpened) - return; - native_close(mNativeSerial); - mOpened = false; - } - - /** - * Return the number of characters in the buffer. - * - * @throws SerialIOException I/O Error. - */ - public int available () throws SerialIOException { - checkOpened(); - return native_available(mNativeSerial); - } - - /** - * Block until there is serial data to read or read_timeout_constant - * number of milliseconds have elapsed. The return value is true when - * the function exits with the port in a readable state, false otherwise - * (due to timeout or select interruption). - * - * @throws SerialIOException I/O Error. - */ - public boolean waitReadable () throws SerialIOException { - checkOpened(); - return native_waitReadable(mNativeSerial); - } - - /** - * Block for a period of time corresponding to the transmission time of - * count characters at present serial settings. This may be used in con- - * junction with waitReadable to read larger blocks of data from the - * port. - */ - public void waitByteTimes (int count) { - checkOpened(); - native_waitByteTimes(mNativeSerial, count); - } - - /** - * Read a given amount of bytes from the serial port into a given buffer. - * - * The read function will return in one of three cases: - * * The number of requested bytes was read. - * * In this case the number of bytes requested will match the size_t - * returned by read. - * * A timeout occurred, in this case the number of bytes read will not - * match the amount requested, but no exception will be thrown. One of - * two possible timeouts occurred: - * * The inter byte timeout expired, this means that number of - * milliseconds elapsed between receiving bytes from the serial port - * exceeded the inter byte timeout. - * * The total timeout expired, which is calculated by multiplying the - * read timeout multiplier by the number of requested bytes and then - * added to the read timeout constant. If that total number of - * milliseconds elapses after the initial call to read a timeout will - * occur. - * * An exception occurred, in this case an actual exception will be thrown. - * - * @param buffer An array of at least the requested size. - * @param offset the offset of the buffer to receive data. - * @param size how many bytes to be read. - * - * @return A size_t representing the number of bytes read as a result of the - * call to read. - * - * @throws SerialIOException I/O Error. - */ - public int read (byte[] buffer, int offset, int size) throws SerialIOException { - checkOpened(); - return native_read(mNativeSerial, buffer, offset, size); - } - - /** - * Read all data available from the serial port. - * - * @return A buffer that contains all available data. - * - * @throws SerialIOException I/O Error. - */ - public byte[] read () throws SerialIOException { - checkOpened(); - int available = native_available(mNativeSerial); - if (available < 0) - return null; - else if (available == 0) - return new byte[0]; - byte[] buffer = new byte[available]; - int bytesRead = native_read(mNativeSerial, buffer, 0, buffer.length); - if (bytesRead < buffer.length) { - byte[] out = new byte[bytesRead]; - System.arraycopy(buffer, 0, out, 0, bytesRead); - buffer = out; - } - return buffer; - } - - /** Read a given amount of bytes from the serial port into a give buffer. - * - * @param buffer A reference to a std::vector of uint8_t. - * @param size A size_t defining how many bytes to be read. - * - * @return A size_t representing the number of bytes read as a result of the - * call to read. - * - * @throws SerialIOException I/O Error. - */ - public int read (ByteBuffer buffer, int size /*= 1*/) throws SerialIOException { - checkOpened(); - byte[] buf = new byte[size]; - int bytesRead = read(buf, 0, buf.length); - buffer.put(buf, 0, bytesRead); - return bytesRead; - } - - /** - * Read a given amount of bytes from the serial port into a give buffer. - * - * @param buffer A reference to a std::string. - * @param size A size_t defining how many bytes to be read. - * @param charset The charset of the data. - * - * @return A size_t representing the number of bytes read as a result of the - * call to read. - * - * @throws SerialIOException I/O Error. - */ - public int read(StringBuilder buffer, int size /*= 1*/, Charset charset) throws SerialIOException { - checkOpened(); - if (charset == null) - charset = CHARSET_DEFAULT; - byte[] buf = new byte[size]; - int bytesRead = read(buf, 0, buf.length); - CharBuffer chars = charset.decode(ByteBuffer.wrap(buf, 0, bytesRead)); - buffer.append(chars.toString()); - return bytesRead; - } - - /** Read a given amount of bytes from the serial port and return a string - * containing the data. - * - * @param size A size_t defining how many bytes to be read. - * - * @return A std::string containing the data read from the port. - * - * @throws SerialIOException I/O Error. - * - */ - public String read(int size, Charset charset) throws SerialIOException { - checkOpened(); - if (charset == null) - charset = CHARSET_DEFAULT; - StringBuilder sb = new StringBuilder(); - read(sb, size, charset); - return sb.toString(); - } - - /** Reads in a line or until a given delimiter has been processed. - * - * Reads from the serial port until a single line has been read. - * - * @param buffer A std::string reference used to store the data. - * @param size A maximum length of a line, defaults to 65536 (2^16) - * @param eol A string to match against for the EOL. - * - * @return A size_t representing the number of bytes read. - * - * @throws SerialIOException I/O Error. - */ - public int readline (StringBuilder buffer, int size /*= 65536*/, String eol /*= "\n"*/) throws SerialIOException { - checkOpened(); - if (eol == null) - eol = EOL_LF; - String line = native_readline(mNativeSerial, size, eol); - buffer.append(line); - return line.getBytes().length; - } - - /** Reads in a line or until a given delimiter has been processed. - * - * Reads from the serial port until a single line has been read. - * - * @param size A maximum length of a line, defaults to 65536 (2^16) - * @param eol A string to match against for the EOL. - * - * @return A std::string containing the line. - * - * @throws SerialIOException I/O Error. - */ - public String readline (int size /*= 65536*/, String eol /*= "\n"*/) throws SerialIOException { - checkOpened(); - if (eol == null) - eol = EOL_LF; - return native_readline(mNativeSerial, size, eol); - } - - /** Reads in multiple lines until the serial port times out. - * - * This requires a timeout > 0 before it can be run. It will read until a - * timeout occurs and return a list of strings. - * - * @param size A maximum length of combined lines, defaults to 65536 (2^16) - * - * @param eol A string to match against for the EOL. - * - * @return A array containing the lines. - * - * @throws SerialIOException I/O Error. - */ - public String[] readlines (int size /*= 65536*/, String eol /*= "\n"*/) throws SerialIOException { - checkOpened(); - if (eol == null) - eol = EOL_LF; - return native_readlines(mNativeSerial, size, eol); - } - - /** Write a string to the serial port. - * - * @param data A const reference containing the data to be written - * to the serial port. - * - * @param size A size_t that indicates how many bytes should be written from - * the given data buffer. - * - * @return A size_t representing the number of bytes actually written to - * the serial port. - * - * @throws SerialIOException I/O Error. - */ - public int write (byte[] data, int size) throws SerialIOException { - checkOpened(); - return native_write(mNativeSerial, data, size); - } - - /** Write a string to the serial port. - * - * @param s A const reference containing the data to be written - * to the serial port. - * - * @return A size_t representing the number of bytes actually written to - * the serial port. - * - * @throws SerialIOException I/O Error. - */ - public int write (String s) throws SerialIOException { - checkOpened(); - byte[] data = s.getBytes(); - return native_write(mNativeSerial, data, data.length); - } - - /** Sets the serial port identifier. - * - * @param port A const std::string reference containing the address of the - * serial port, which would be something like 'COM1' on Windows and - * '/dev/ttyS0' on Linux. - * - * @throws IllegalArgumentException - */ - public void setPort (String port) { - checkValid(); - native_setPort(mNativeSerial, port); - } - - /** Gets the serial port identifier. - * - * @see #setPort(String) - * - */ - public String getPort () { - checkValid(); - return native_getPort(mNativeSerial); - } - - /** Sets the timeout for reads and writes using the Timeout struct. - * - * There are two timeout conditions described here: - * * The inter byte timeout: - * * The inter_byte_timeout component of serial::Timeout defines the - * maximum amount of time, in milliseconds, between receiving bytes on - * the serial port that can pass before a timeout occurs. Setting this - * to zero will prevent inter byte timeouts from occurring. - * * Total time timeout: - * * The constant and multiplier component of this timeout condition, - * for both read and write, are defined in serial::Timeout. This - * timeout occurs if the total time since the read or write call was - * made exceeds the specified time in milliseconds. - * * The limit is defined by multiplying the multiplier component by the - * number of requested bytes and adding that product to the constant - * component. In this way if you want a read call, for example, to - * timeout after exactly one second regardless of the number of bytes - * you asked for then set the read_timeout_constant component of - * serial::Timeout to 1000 and the read_timeout_multiplier to zero. - * This timeout condition can be used in conjunction with the inter - * byte timeout condition with out any problems, timeout will simply - * occur when one of the two timeout conditions is met. This allows - * users to have maximum control over the trade-off between - * responsiveness and efficiency. - * - * Read and write functions will return in one of three cases. When the - * reading or writing is complete, when a timeout occurs, or when an - * exception occurs. - * - * @param timeout A serial::Timeout struct containing the inter byte - * timeout, and the read and write timeout constants and multipliers. - * - * @see Timeout - */ - public void setTimeout (Timeout timeout) { - checkValid(); - if (null == timeout) - timeout = new Timeout(); - this.mTimeout = timeout; - native_setTimeout(mNativeSerial, new int[] { - timeout.inter_byte_timeout, - timeout.read_timeout_constant, - timeout.read_timeout_multiplier, - timeout.write_timeout_constant, - timeout.read_timeout_multiplier - }); - } - - /** - * Sets the timeout for reads and writes. - */ - public void setTimeout (int inter_byte_timeout, int read_timeout_constant, - int read_timeout_multiplier, int write_timeout_constant, - int write_timeout_multiplier) - { - Timeout timeout = new Timeout(inter_byte_timeout, read_timeout_constant, - read_timeout_multiplier, write_timeout_constant, - write_timeout_multiplier); - setTimeout(timeout); - } - - /** Gets the timeout for reads in seconds. - * - * @return A Timeout struct containing the inter_byte_timeout, and read - * and write timeout constants and multipliers. - * - * @see #setTimeout(Timeout) - */ - public Timeout getTimeout () { - return mTimeout; - } - - /** Sets the baudrate for the serial port. - * - * Possible baudrates depends on the system but some safe baudrates include: - * 110, 300, 600, 1200, 2400, 4800, 9600 (default), 14400, 19200, 28800, 38400, 56000, - * 57600, 115200 - * Some other baudrates that are supported by some comports: - * 128000, 153600, 230400, 256000, 460800, 921600 - * - * @param baudrate An integer that sets the baud rate for the serial port. - * - * @throws SerialIOException I/O Error. - */ - public void setBaudrate (int baudrate) throws SerialIOException { - checkValid(); - native_setBaudrate(mNativeSerial, baudrate); - } - - /** Gets the baudrate for the serial port. - * - * @return An integer that sets the baud rate for the serial port. - * - * @see #setBaudrate(int) - * - */ - public int getBaudrate () { - checkValid(); - return native_getBaudrate(mNativeSerial); - } - - /** - * Sets the bytesize for the serial port. - * - * @param bytesize Size of each byte in the serial transmission of data, - * default is eightbits, possible values are: fivebits, sixbits, sevenbits, - * eightbits - * - * @throws SerialIOException I/O Error. - */ - public void setBytesize (ByteSize bytesize) throws SerialIOException { - checkValid(); - if (null == bytesize) - bytesize = ByteSize.EightBits; - native_setBytesize(mNativeSerial, bytesize.bitLength); - } - - /** - * Gets the bytesize for the serial port. - * - * @see #setBytesize(ByteSize) - * - */ - public ByteSize getBytesize () { - checkValid(); - return ByteSize.fromValue(native_getBytesize(mNativeSerial)); - } - - /** - * Sets the parity for the serial port. - * - * @param parity Method of parity, default is parity_none, possible values - * are: parity_none, parity_odd, parity_even - * @throws SerialIOException I/O Error. - * - */ - public void setParity (Parity parity) throws SerialIOException { - checkValid(); - if (null == parity) - parity = Parity.None; - native_setParity(mNativeSerial, parity.ordinal()); - } - - /** Gets the parity for the serial port. - * - * @see #setParity(Parity) - * - */ - public Parity getParity() { - checkValid(); - return Parity.values()[native_getParity(mNativeSerial)]; - } - - /** - * Sets the stopbits for the serial port. - * - * @param stopbits Number of stop bits used, default is stopbits_one, - * possible values are: stopbits_one, stopbits_one_point_five, stopbits_two - * @throws SerialIOException I/O Error. - */ - public void setStopbits (Stopbits stopbits) throws SerialIOException { - checkValid(); - if (null == stopbits) - stopbits = Stopbits.One; - native_setStopbits(mNativeSerial, stopbits.value); - } - - /** - * Gets the stopbits for the serial port. - * - * @see #setStopbits(Stopbits) - * - */ - public Stopbits getStopbits () { - checkValid(); - return Stopbits.fromValue(native_getStopbits(mNativeSerial)); - } - - /** - * Sets the flow control for the serial port. - * - * @param flowcontrol Type of flowcontrol used, default is flowcontrol_none, - * possible values are: flowcontrol_none, flowcontrol_software, - * flowcontrol_hardware - * - * @throws SerialIOException I/O Error. - */ - public void setFlowcontrol (FlowControl flowcontrol) throws SerialIOException { - checkValid(); - if (null == flowcontrol) - flowcontrol = FlowControl.None; - native_setFlowcontrol(mNativeSerial, flowcontrol.ordinal()); - } - - /** - * Gets the flow control for the serial port. - * - * @see #setFlowcontrol(FlowControl) - * - */ - public FlowControl getFlowcontrol () { - checkValid(); - return FlowControl.values()[native_getFlowcontrol(mNativeSerial)]; - } - - /** Flush the input and output buffers */ - public void flush () { - checkOpened(); - native_flush(mNativeSerial); - } - - /** Flush only the input buffer */ - public void flushInput () { - checkOpened(); - native_flushInput(mNativeSerial); - } - - /** Flush only the output buffer */ - public void flushOutput () { - checkOpened(); - native_flushOutput(mNativeSerial); - } - - /** Sends the RS-232 break signal. See tcsendbreak(3). */ - public void sendBreak (int duration) { - checkOpened(); - native_sendBreak(mNativeSerial, duration); - } - - /** Set the break condition to a given level. */ - public void setBreak (boolean level) { - checkOpened(); - native_setBreak(mNativeSerial, level); - } - - /** Set the break condition to true level. */ - public void setBreak () { - checkOpened(); - setBreak(true); - } - - /** Set the RTS handshaking line to the given level.*/ - public void setRTS (boolean level) { - checkOpened(); - native_setRTS(mNativeSerial, level); - } - - /** Set the RTS handshaking line to the true level.*/ - public void setRTS () { - checkOpened(); - setRTS(true); - } - - /** Set the DTR handshaking line to the given level. */ - public void setDTR (boolean level) { - checkOpened(); - native_setDTR(mNativeSerial, level); - } - - /** Set the DTR handshaking line to the given level. */ - public void setDTR () { - checkOpened(); - setDTR(true); - } - - /** - * Blocks until CTS, DSR, RI, CD changes or something interrupts it. - * - * Can throw an exception if an error occurs while waiting. - * You can check the status of CTS, DSR, RI, and CD once this returns. - * Uses TIOCMIWAIT via ioctl if available (mostly only on Linux) with a - * resolution of less than +-1ms and as good as +-0.2ms. Otherwise a - * polling method is used which can give +-2ms. - * - * @return Returns true if one of the lines changed, false if something else - * occurred. - * - * @throws SerialException - */ - public boolean waitForChange () { - checkOpened(); - return native_waitForChange(mNativeSerial); - } - - /*! Returns the current status of the CTS line. */ - public boolean getCTS () { - checkOpened(); - return native_getCTS(mNativeSerial); - } - - /** Returns the current status of the DSR line. */ - public boolean getDSR () { - checkOpened(); - return native_getDSR(mNativeSerial); - } - - /** Returns the current status of the RI line. */ - public boolean getRI () { - checkOpened(); - return native_getRI(mNativeSerial); - } - - /** Returns the current status of the CD line. */ - public boolean getCD () { - checkOpened(); - return native_getCD(mNativeSerial); - } - - - private static native String[] native_listPorts(); - - private static native long native_create(String port, int baudrate, int[] ints, int bytesize, int parity, int stopbits, int flowcontrol) throws IllegalArgumentException, SerialException, SerialIOException; - private static native void native_destory(long nativePtr); - private static native void native_open(long nativePtr) throws IllegalArgumentException, SerialException, SerialIOException; - private static native boolean native_isOpen(long nativePtr); - private static native void native_close(long nativePtr) throws SerialIOException; - private static native int native_available(long nativePtr) throws SerialIOException; - private static native boolean native_waitReadable(long nativePtr) throws SerialIOException; - private static native void native_waitByteTimes(long nativePtr, int count); - private static native int native_read(long nativePtr, byte[] buffer, int offset, int size) throws IllegalArgumentException, SerialException, SerialIOException; - private static native String native_readline(long nativePtr, int size, String eol) throws IllegalArgumentException, SerialException, SerialIOException; - private static native String[] native_readlines(long nativePtr, int size, String eol) throws IllegalArgumentException, SerialException, SerialIOException; - private static native int native_write(long nativePtr, byte[] buffer,int size) throws IllegalArgumentException, SerialException, SerialIOException; - - private static native void native_setPort(long nativePtr, String port); - private static native String native_getPort(long nativePtr); - - private static native void native_setBaudrate(long nativePtr, int baudrate) throws IllegalArgumentException, SerialException, SerialIOException; - private static native int native_getBaudrate(long nativePtr); - private static native void native_setTimeout(long nativePtr, int[] timeouts); - private static native int[] native_getTimeout(long nativePtr); - - private static native void native_setBytesize(long nativePtr, int bytesize) throws IllegalArgumentException, SerialException, SerialIOException; - private static native int native_getBytesize(long nativePtr); - private static native void native_setParity(long nativePtr, int parity) throws IllegalArgumentException, SerialException, SerialIOException; - private static native int native_getParity(long nativePtr); - private static native void native_setStopbits(long nativePtr, int stopbits) throws IllegalArgumentException, SerialException, SerialIOException; - private static native int native_getStopbits(long nativePtr); - private static native void native_setFlowcontrol(long nativePtr, int flowcontrol) throws IllegalArgumentException, SerialException, SerialIOException; - private static native int native_getFlowcontrol(long nativePtr); - - private static native void native_flush(long nativePtr); - private static native void native_flushInput(long nativePtr); - private static native void native_flushOutput(long nativePtr); - - private static native void native_sendBreak(long nativePtr, int duration) throws SerialException; - private static native void native_setBreak(long nativePtr, boolean level) throws SerialException; - private static native void native_setRTS(long nativePtr, boolean level) throws SerialException; - private static native void native_setDTR(long nativePtr, boolean level) throws SerialException; - - private static native boolean native_waitForChange(long nativePtr) throws SerialException; - - private static native boolean native_getCTS(long nativePtr) throws SerialException; - private static native boolean native_getDSR(long nativePtr) throws SerialException; - private static native boolean native_getRI(long nativePtr) throws SerialException; - private static native boolean native_getCD(long nativePtr) throws SerialException; - -} diff --git a/android/library/src/main/java/serial/SerialException.java b/android/library/src/main/java/serial/SerialException.java deleted file mode 100644 index e2bde59..0000000 --- a/android/library/src/main/java/serial/SerialException.java +++ /dev/null @@ -1,27 +0,0 @@ -package serial; - -/** - * Created by chzhong on 1/20/16. - */ -public class SerialException extends RuntimeException { - /** - * Constructs a new {@code SerialException} with the current stack trace - * and the specified detail message. - * - * @param detailMessage the detail message for this exception. - */ - public SerialException(String detailMessage) { - super(detailMessage); - } - - /** - * Constructs a new {@code PortNotOpenedException} with the current stack trace, - * the specified detail message and the specified cause. - * - * @param detailMessage the detail message for this exception. - * @param throwable - */ - public SerialException(String detailMessage, Throwable throwable) { - super(detailMessage, throwable); - } -} diff --git a/android/library/src/main/java/serial/SerialIOException.java b/android/library/src/main/java/serial/SerialIOException.java deleted file mode 100644 index 5156458..0000000 --- a/android/library/src/main/java/serial/SerialIOException.java +++ /dev/null @@ -1,31 +0,0 @@ -package serial; - -import java.io.IOException; - -/** - * Created by chzhong on 1/20/16. - */ -public class SerialIOException extends IOException { - - /** - * Constructs a new {@code SerialIOException} with its stack trace and detail - * message filled in. - * - * @param detailMessage the detail message for this exception. - */ - public SerialIOException(String detailMessage) { - super(detailMessage); - } - - /** - * Constructs a new instance of this class with detail message and cause - * filled in. - * - * @param message The detail message for the exception. - * @param cause The detail cause for the exception. - * @since 1.6 - */ - public SerialIOException(String message, Throwable cause) { - super(message, cause); - } -} diff --git a/android/library/src/main/java/serial/Stopbits.java b/android/library/src/main/java/serial/Stopbits.java deleted file mode 100644 index f2b4334..0000000 --- a/android/library/src/main/java/serial/Stopbits.java +++ /dev/null @@ -1,43 +0,0 @@ -package serial; - -/** - * Enumeration defines the possible stopbit types for the serial port. - */ -public enum Stopbits { - /** - * Use 1 as stop bit. - * - * This is the default. - */ - One(1), - /** - * Use 2 as stop bit. - */ - Two(2), - /** - * Use 1.5 as stopbit. - */ - OnePointFive(3); - - /** - * Internal value of the enumeration. - */ - public final int value; - - Stopbits(int value) { - this.value = value; - } - - static Stopbits fromValue(int value) { - switch (value) { - case 1: - return One; - case 2: - return Two; - case 3: - return OnePointFive; - default: - throw new IllegalArgumentException("Invalid value."); - } - } -} diff --git a/android/library/src/main/java/serial/Timeout.java b/android/library/src/main/java/serial/Timeout.java deleted file mode 100644 index d7cd49a..0000000 --- a/android/library/src/main/java/serial/Timeout.java +++ /dev/null @@ -1,77 +0,0 @@ -package serial; - -/** - * Structure for setting the timeout of the serial port, times are - * in milliseconds. - * - * In order to disable the interbyte timeout, set it to {@link #MAX}. - */ -public final class Timeout { - - /** - * Value that represents interbyte timeout should be disabled. - * - * This is an unsigned value. - */ - public static final int MAX = -1; - - /** - * Convenience function to generate Timeout structs using a - * single absolute timeout. - * - * @param timeout A long that defines the time in milliseconds until a - * timeout occurs after a call to read or write is made. - * @return Timeout struct that represents this simple timeout provided. - */ - public static final Timeout simpleTimeout(int timeout) { - return new Timeout(MAX, timeout, 0, timeout, 0); - } - - /** - * Number of milliseconds between bytes received to timeout on. - */ - int inter_byte_timeout; - /** - * A constant number of milliseconds to wait after calling read. - */ - int read_timeout_constant; - /** - * A multiplier against the number of requested bytes to wait after - * calling read. - */ - int read_timeout_multiplier; - /** - * A constant number of milliseconds to wait after calling write. - */ - int write_timeout_constant; - /** - * A multiplier against the number of requested bytes to wait after - * calling write. - */ - int write_timeout_multiplier; - - public Timeout() { - this(0, 0, 0, 0, 0); - } - - /** - * Convenience function to generate Timeout structs using a - * single absolute timeout. - * - * @param timeout A long that defines the time in milliseconds until a - * timeout occurs after a call to read or write is made. - * @return Timeout struct that represents this simple timeout provided. - */ - public Timeout(int timeout) { - this(MAX, timeout, 0, timeout, 0); - } - - public Timeout(int inter_byte_timeout, int read_timeout_constant, int read_timeout_multiplier, int write_timeout_constant, int write_timeout_multiplier) { - this.inter_byte_timeout = inter_byte_timeout; - this.read_timeout_constant = read_timeout_constant; - this.read_timeout_multiplier = read_timeout_multiplier; - this.write_timeout_constant = write_timeout_constant; - this.write_timeout_multiplier = write_timeout_multiplier; - } - -} diff --git a/android/library/src/main/jni/Android.mk b/android/library/src/main/jni/Android.mk deleted file mode 100644 index ca6ce22..0000000 --- a/android/library/src/main/jni/Android.mk +++ /dev/null @@ -1,25 +0,0 @@ -LOCAL_PATH := $(call my-dir) - -$(call import-add-path,$(LOCAL_PATH)/libs) - -include $(CLEAR_VARS) - -LOCAL_MODULE := serial -LOCAL_C_INCLUDES := \ - $(LOCAL_PATH)/include \ - $(LOCAL_PATH)/libs - -LOCAL_SRC_FILES := glob.c \ - serial.cc \ - serial_unix.cc \ - serial_jni.cc \ - list_ports_linux.cc \ - jni_utility.cc \ - jni_main.cc - -LOCAL_STATIC_LIBRARIES += nativehelper -LOCAL_LDLIBS := -llog -lz - -include $(BUILD_SHARED_LIBRARY) - -$(call import-module,nativehelper) diff --git a/android/library/src/main/jni/build-jni.sh b/android/library/src/main/jni/build-jni.sh deleted file mode 100755 index 2376fe6..0000000 --- a/android/library/src/main/jni/build-jni.sh +++ /dev/null @@ -1,25 +0,0 @@ -#!/bin/bash - -ARGS=$(getopt -o "d" -l "debug" -n "build-jni.sh" -- "$@"); - -eval set -- "$ARGS"; - -JNI_ARGS="NDK_LIBS_OUT=../jniLibs" - -while true; do - case "$1" in - -d|--debug) - shift - JNI_ARGS="$JNI_ARGS NDK_DEBUG=1" - break; - ;; - --) - shift - ;; - esac -done - -# Remove '--' -shift - -ndk-build $JNI_ARGS \ No newline at end of file diff --git a/android/library/src/main/jni/jni_main.cc b/android/library/src/main/jni/jni_main.cc deleted file mode 100644 index 539983f..0000000 --- a/android/library/src/main/jni/jni_main.cc +++ /dev/null @@ -1,60 +0,0 @@ -#include -#include - -#include "jni_utility.h" -#include "log.h" - -using namespace std; - -#ifdef __cplusplus -extern "C" { -#endif - -struct RegistrationMethod { - const char* name; - int (*func)(JNIEnv*); -}; - -extern int registerSerial(JNIEnv* env); - -static RegistrationMethod gRegMethods[] = { - { "Serial", registerSerial }, -}; - -JNIEXPORT jint JNI_OnLoad(JavaVM* vm, void* reserved) -{ - LOGV("JNI_OnLoad"); - JNIEnv* env = NULL; - jint result = -1; - - if (vm->GetEnv((void**) &env, JNI_VERSION_1_4) != JNI_OK) { - LOGE("GetEnv failed!"); - return result; - } - if (!env) - LOGE("Could not retrieve the env!"); - - // Save the JavaVM pointer for use globally. - setJavaVM(vm); - - const RegistrationMethod* method = gRegMethods; - const RegistrationMethod* end = method + sizeof(gRegMethods) / sizeof(RegistrationMethod); - while (method != end) { - if (method->func(env) < 0) { - LOGE("%s registration failed!", method->name); - return result; - } - //LOGV("%s registration done.", method->name); - method++; - } - - // Initialize rand() function. The rand() function is used in - // FileSystemAndroid to create a random temporary filename. - srand(time(NULL)); - LOGV("JNI_OnLoad done."); - return JNI_VERSION_1_4; -} - -#ifdef __cplusplus -} -#endif diff --git a/android/library/src/main/jni/jni_utility.cc b/android/library/src/main/jni/jni_utility.cc deleted file mode 100644 index 4bb29f2..0000000 --- a/android/library/src/main/jni/jni_utility.cc +++ /dev/null @@ -1,278 +0,0 @@ -/* - * Copyright (C) 2003, 2004, 2005, 2007, 2008, 2009, 2010 Apple Inc. All rights reserved. - * - * Redistribution and use in source and binary forms, with or without - * modification, are permitted provided that the following conditions - * are met: - * 1. Redistributions of source code must retain the above copyright - * notice, this list of conditions and the following disclaimer. - * 2. Redistributions in binary form must reproduce the above copyright - * notice, this list of conditions and the following disclaimer in the - * documentation and/or other materials provided with the distribution. - * - * THIS SOFTWARE IS PROVIDED BY APPLE COMPUTER, INC. ``AS IS'' AND ANY - * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE - * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR - * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE COMPUTER, INC. OR - * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, - * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, - * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR - * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY - * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT - * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE - * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. - */ - -#include - -static JavaVM* jvm = 0; -static jobject gClassLoader; -static jmethodID gFindClassMethod; - -static jclass gStringClass = 0; -static jmethodID gGetBytesMid = 0; -static jmethodID gNewStringMid = 0; - -static jobject gTrue = 0; -static jobject gFalse = 0; -static jclass gLongClass = 0; -static jmethodID gLongValueOf = 0; -static jclass gIntClass = 0; -static jmethodID gIntValueOf = 0; - -void setupGlobalClassLoader() -{ - JNIEnv* env = getJNIEnv(); - ScopedLocalRef coreJniClass(env, env->FindClass("serial/Serial")); - ScopedLocalRef classClass(env, env->GetObjectClass(coreJniClass.get())); - ScopedLocalRef classLoaderClass(env, env->FindClass("java/lang/ClassLoader")); - jmethodID getClassLoaderMethod = env->GetMethodID(classClass.get(), "getClassLoader", - "()Ljava/lang/ClassLoader;"); - ScopedLocalRef classLoader(env, env->CallObjectMethod(coreJniClass.get(), getClassLoaderMethod)); - gClassLoader = env->NewGlobalRef(classLoader.get()); - //LOGV("gClassLoader is %p", gClassLoader); - gFindClassMethod = env->GetMethodID(classLoaderClass.get(), "findClass", - "(Ljava/lang/String;)Ljava/lang/Class;"); - //LOGV("gFindClassMethod is %p", gFindClassMethod); - - ScopedLocalRef stringClass(env, env->FindClass("java/lang/String")); - gStringClass = (jclass)env->NewGlobalRef(stringClass.get()); - //LOGV("gStringClass is %p", gStringClass); - gGetBytesMid = env->GetMethodID (gStringClass, "getBytes", "()[B"); - //LOGV("gGetBytesMid is %p", gGetBytesMid); - gNewStringMid = env->GetMethodID(gStringClass, "", "([B)V"); - //LOGV("gNewStringMid is %p", gNewStringMid); - - ScopedLocalRef booleanCls(env, env->FindClass("java/lang/Boolean")); - ScopedLocalRef t(env, callJNIStaticMethod( - booleanCls.get(), "valueOf", "(Z)Ljava/lang/Boolean;", JNI_TRUE)); - gTrue = env->NewGlobalRef(t.get()); - //LOGV("gTrue is %p", gTrue); - ScopedLocalRef f(env, callJNIStaticMethod( - booleanCls.get(), "valueOf", "(Z)Ljava/lang/Boolean;", JNI_FALSE)); - gFalse = env->NewGlobalRef(f.get()); - //LOGV("gFalse is %p", gFalse); - - ScopedLocalRef longCls(env, env->FindClass("java/lang/Long")); - gLongClass = (jclass)env->NewGlobalRef(longCls.get()); - gLongValueOf = env->GetStaticMethodID(gLongClass, "valueOf", "(J)Ljava/lang/Long;"); - - ScopedLocalRef intCls(env, env->FindClass("java/lang/Integer")); - gIntClass = (jclass)env->NewGlobalRef(intCls.get()); - gIntValueOf = env->GetStaticMethodID(gIntClass, "valueOf", "(I)Ljava/lang/Integer;"); -} - -jclass findClass(const char* name, int flags /*= FIND_CLASS_USE_CLASS_LOADER*/) -{ - JNIEnv *env = getJNIEnv(); - jclass cls; - if ((flags & FIND_CLASS_USE_CLASS_LOADER) == FIND_CLASS_USE_CLASS_LOADER) { - ScopedLocalRef jname(env, env->NewStringUTF(name)); - cls = static_cast(env->CallObjectMethod(gClassLoader, gFindClassMethod, jname.get())); - } else { - cls = env->FindClass(name); - } - checkException(env); - - if ((flags & FIND_CLASS_RETURN_GLOBAL_REF) == FIND_CLASS_RETURN_GLOBAL_REF) { - jclass localClass = cls; - cls = (jclass)env->NewGlobalRef(localClass); - env->DeleteLocalRef(localClass); - } - return cls; -} - -// Provide the ability for an outside component to specify the JavaVM to use -// If the jvm value is set, the getJavaVM function below will just return. -// In getJNIEnv(), if AttachCurrentThread is called to a VM that is already -// attached, the result is a no-op. -void setJavaVM(JavaVM* javaVM) -{ - jvm = javaVM; - setupGlobalClassLoader(); -} - -JavaVM* getJavaVM() -{ - if (jvm) - return jvm; - LOGE("JavaVM is null"); - return NULL; -} - -static void detachCurrentThread(void *data) -{ - getJavaVM()->DetachCurrentThread(); -} - -JNIEnv* getJNIEnv() -{ - union { - JNIEnv* env; - void* dummy; - } u; - jint jniError = 0; - - jniError = getJavaVM()->AttachCurrentThread(&u.env, 0); - if (jniError == JNI_OK) { - return u.env; - } - LOGE("AttachCurrentThread failed, returned %ld", static_cast(jniError)); - return 0; -} - -bool checkException(JNIEnv* env) { - if (env->ExceptionCheck() != 0) { - LOGE("*** Uncaught exception returned from Java call!\n"); - env->ExceptionDescribe(); - return true; - } - return false; -} - -std::string jstringToStdString(JNIEnv* env, jstring jstr) { - if (!jstr || !env) - return std::string(); - - std::string result; - ScopedLocalRef barr(env, - (jbyteArray) env->CallObjectMethod(jstr, gGetBytesMid)); - jsize alen = env->GetArrayLength(barr.get()); - jbyte * ba = env->GetByteArrayElements(barr.get(), JNI_FALSE); - if (alen> 0) { - char* rtn = (char *) malloc (alen + 1); - memcpy(rtn, ba, alen); - rtn [alen] = 0; - result.assign(rtn); - } - env->ReleaseByteArrayElements(barr.get(), ba, 0); - return result; -} - -jstring stdStringToJstring(JNIEnv* env, const std::string& str) { - //return env->NewStringUTF(str.c_str()); - const char* bytes = str.c_str(); - size_t size = str.size(); - ScopedLocalRef result(env, env->NewByteArray(size)); - env->SetByteArrayRegion(result.get(), 0, size, (jbyte*)bytes); - jstring ret = (jstring) env->NewObject(gStringClass, gNewStringMid, result.get()); - return ret; -} - -jobjectArray createStringArray(JNIEnv *env, int size) { - return env->NewObjectArray(size, gStringClass, NULL); -} - -void callJNIVoidMethod(jobject obj, const char* methodName, const char* methodSignature, ...) -{ - va_list args; - va_start(args, methodSignature); - - callJNIMethodV(obj, methodName, methodSignature, args); - - va_end(args); -} - -void callJNIVoidMethodID(jobject obj, jmethodID mid, ...) -{ - va_list args; - va_start(args, mid); - - callJNIMethodIDV(obj, mid, args); - - va_end(args); -} - -void callJNIStaticVoidMethod(const char* className, const char* methodName, const char* methodSignature, ...) -{ - va_list args; - va_start(args, methodSignature); - - JNIEnv *env = getJNIEnv(); - ScopedLocalRef cls(env, findClass(className)); - if (checkException(env)) { - return; - } - if (cls.get() && env) { - jmethodID mid = env->GetStaticMethodID(cls.get(), methodName, methodSignature); - if (mid) - JNICaller::callStaticV(cls.get(), mid, args); - else { - env->ExceptionDescribe(); - LOGE("Could not find method: %s for %p", methodName, cls.get()); - } - } - va_end(args); -} - -jobject toJavaBoolean(bool value) -{ - if (value) { - return gTrue; - } else { - return gFalse; - } -} - -jobject toJavaLong(jlong value) -{ - return callJNIStaticMethod(gLongClass, gLongValueOf, value); -} - -jobject toJavaInt(jint value) -{ - return callJNIStaticMethod(gIntClass, gIntValueOf, value); -} - -jobject newJavaObject(const char* className) -{ - JNIEnv *env = getJNIEnv(); - ScopedLocalRef cls(env, findClass(className)); - if (cls.get()) { - jmethodID mid = env->GetMethodID(cls.get(), "", "()V"); - if (mid) { - return env->NewObject(cls.get(), mid); - } else { - LOGE("Could not find method %s", "()V"); - } - env->ExceptionDescribe(); - } else { - env->ExceptionDescribe(); - LOGE("Could not find class %s", className); - } - return 0; -} - -std::list jstringArrayToStdStringList(jobjectArray jstringArray) -{ - std::list list; - JNIEnv *env = getJNIEnv(); - jsize size = env->GetArrayLength(jstringArray); - for (int i = 0; i < size; i++) { - ScopedLocalRef item(env, env->GetObjectArrayElement(jstringArray, i)); - if (item.get()) { - list.push_back(jstringToStdString((jstring)item.get())); - } - } - return list; -} diff --git a/android/library/src/main/jni/libs/nativehelper/ALog-priv.h b/android/library/src/main/jni/libs/nativehelper/ALog-priv.h deleted file mode 100644 index 7b3ed97..0000000 --- a/android/library/src/main/jni/libs/nativehelper/ALog-priv.h +++ /dev/null @@ -1,66 +0,0 @@ -/* - * Copyright 2013 The Android Open Source Project - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -#ifndef NATIVEHELPER_ALOGPRIV_H_ -#define NATIVEHELPER_ALOGPRIV_H_ - -#include - -#ifndef LOG_NDEBUG -#ifdef NDEBUG -#define LOG_NDEBUG 1 -#else -#define LOG_NDEBUG 0 -#endif -#endif - - -/* - * Basic log message macros intended to emulate the behavior of log/log.h - * in system core. This should be dependent only on ndk exposed logging - * functionality. - */ - -#ifndef ALOG -#define ALOG(priority, tag, fmt...) \ - __android_log_print(ANDROID_##priority, tag, fmt) -#endif - -#ifndef ALOGV -#if LOG_NDEBUG -#define ALOGV(...) ((void)0) -#else -#define ALOGV(...) ((void)ALOG(LOG_VERBOSE, LOG_TAG, __VA_ARGS__)) -#endif -#endif - -#ifndef ALOGD -#define ALOGD(...) ((void)ALOG(LOG_DEBUG, LOG_TAG, __VA_ARGS__)) -#endif - -#ifndef ALOGI -#define ALOGI(...) ((void)ALOG(LOG_INFO, LOG_TAG, __VA_ARGS__)) -#endif - -#ifndef ALOGW -#define ALOGW(...) ((void)ALOG(LOG_WARN, LOG_TAG, __VA_ARGS__)) -#endif - -#ifndef ALOGE -#define ALOGE(...) ((void)ALOG(LOG_ERROR, LOG_TAG, __VA_ARGS__)) -#endif - -#endif diff --git a/android/library/src/main/jni/libs/nativehelper/Android.mk b/android/library/src/main/jni/libs/nativehelper/Android.mk deleted file mode 100644 index c19bfb5..0000000 --- a/android/library/src/main/jni/libs/nativehelper/Android.mk +++ /dev/null @@ -1,33 +0,0 @@ -# Copyright (C) 2009 The Android Open Source Project -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. - - -LOCAL_PATH := $(call my-dir) - -include $(CLEAR_VARS) -LOCAL_MODULE_TAGS := optional -LOCAL_MODULE := nativehelper -LOCAL_CLANG := true -LOCAL_C_INCLUDES := \ - $(LOCAL_PATH)/include/nativehelper -LOCAL_SRC_FILES := \ - JNIHelp.cpp \ - JniConstants.cpp \ - toStringArray.cpp - -LOCAL_EXPORT_CPPFLAGS := -I$(LOCAL_PATH)/include -LOCAL_CFLAGS := -Werror -LOCAL_SRC_FILES := $(LOCAL_SRC_FILES) -#LOCAL_LDFLAGS := -llog -ldl -include $(BUILD_STATIC_LIBRARY) diff --git a/android/library/src/main/jni/libs/nativehelper/JNIHelp.cpp b/android/library/src/main/jni/libs/nativehelper/JNIHelp.cpp deleted file mode 100644 index fcad2f4..0000000 --- a/android/library/src/main/jni/libs/nativehelper/JNIHelp.cpp +++ /dev/null @@ -1,341 +0,0 @@ -/* - * Copyright (C) 2006 The Android Open Source Project - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -#define LOG_TAG "JNIHelp" - -#include "JniConstants.h" -#include "JNIHelp.h" -#include "ALog-priv.h" - -#include -#include -#include -#include - -#include - -/** - * Equivalent to ScopedLocalRef, but for C_JNIEnv instead. (And slightly more powerful.) - */ -template -class scoped_local_ref { -public: - scoped_local_ref(C_JNIEnv* env, T localRef = NULL) - : mEnv(env), mLocalRef(localRef) - { - } - - ~scoped_local_ref() { - reset(); - } - - void reset(T localRef = NULL) { - if (mLocalRef != NULL) { - (*mEnv)->DeleteLocalRef(reinterpret_cast(mEnv), mLocalRef); - mLocalRef = localRef; - } - } - - T get() const { - return mLocalRef; - } - -private: - C_JNIEnv* mEnv; - T mLocalRef; - - // Disallow copy and assignment. - scoped_local_ref(const scoped_local_ref&); - void operator=(const scoped_local_ref&); -}; - -static jclass findClass(C_JNIEnv* env, const char* className) { - JNIEnv* e = reinterpret_cast(env); - return (*env)->FindClass(e, className); -} - -extern "C" int jniRegisterNativeMethods(C_JNIEnv* env, const char* className, - const JNINativeMethod* gMethods, int numMethods) -{ - JNIEnv* e = reinterpret_cast(env); - - ALOGV("Registering %s's %d native methods...", className, numMethods); - - scoped_local_ref c(env, findClass(env, className)); - if (c.get() == NULL) { - char* msg; - asprintf(&msg, "Native registration unable to find class '%s'; aborting...", className); - e->FatalError(msg); - } - - if ((*env)->RegisterNatives(e, c.get(), gMethods, numMethods) < 0) { - char* msg; - asprintf(&msg, "RegisterNatives failed for '%s'; aborting...", className); - e->FatalError(msg); - } - - return 0; -} - -/* - * Returns a human-readable summary of an exception object. The buffer will - * be populated with the "binary" class name and, if present, the - * exception message. - */ -static bool getExceptionSummary(C_JNIEnv* env, jthrowable exception, std::string& result) { - JNIEnv* e = reinterpret_cast(env); - - /* get the name of the exception's class */ - scoped_local_ref exceptionClass(env, (*env)->GetObjectClass(e, exception)); // can't fail - scoped_local_ref classClass(env, - (*env)->GetObjectClass(e, exceptionClass.get())); // java.lang.Class, can't fail - jmethodID classGetNameMethod = - (*env)->GetMethodID(e, classClass.get(), "getName", "()Ljava/lang/String;"); - scoped_local_ref classNameStr(env, - (jstring) (*env)->CallObjectMethod(e, exceptionClass.get(), classGetNameMethod)); - if (classNameStr.get() == NULL) { - (*env)->ExceptionClear(e); - result = ""; - return false; - } - const char* classNameChars = (*env)->GetStringUTFChars(e, classNameStr.get(), NULL); - if (classNameChars == NULL) { - (*env)->ExceptionClear(e); - result = ""; - return false; - } - result += classNameChars; - (*env)->ReleaseStringUTFChars(e, classNameStr.get(), classNameChars); - - /* if the exception has a detail message, get that */ - jmethodID getMessage = - (*env)->GetMethodID(e, exceptionClass.get(), "getMessage", "()Ljava/lang/String;"); - scoped_local_ref messageStr(env, - (jstring) (*env)->CallObjectMethod(e, exception, getMessage)); - if (messageStr.get() == NULL) { - return true; - } - - result += ": "; - - const char* messageChars = (*env)->GetStringUTFChars(e, messageStr.get(), NULL); - if (messageChars != NULL) { - result += messageChars; - (*env)->ReleaseStringUTFChars(e, messageStr.get(), messageChars); - } else { - result += ""; - (*env)->ExceptionClear(e); // clear OOM - } - - return true; -} - -/* - * Returns an exception (with stack trace) as a string. - */ -static bool getStackTrace(C_JNIEnv* env, jthrowable exception, std::string& result) { - JNIEnv* e = reinterpret_cast(env); - - scoped_local_ref stringWriterClass(env, findClass(env, "java/io/StringWriter")); - if (stringWriterClass.get() == NULL) { - return false; - } - - jmethodID stringWriterCtor = (*env)->GetMethodID(e, stringWriterClass.get(), "", "()V"); - jmethodID stringWriterToStringMethod = - (*env)->GetMethodID(e, stringWriterClass.get(), "toString", "()Ljava/lang/String;"); - - scoped_local_ref printWriterClass(env, findClass(env, "java/io/PrintWriter")); - if (printWriterClass.get() == NULL) { - return false; - } - - jmethodID printWriterCtor = - (*env)->GetMethodID(e, printWriterClass.get(), "", "(Ljava/io/Writer;)V"); - - scoped_local_ref stringWriter(env, - (*env)->NewObject(e, stringWriterClass.get(), stringWriterCtor)); - if (stringWriter.get() == NULL) { - return false; - } - - scoped_local_ref printWriter(env, - (*env)->NewObject(e, printWriterClass.get(), printWriterCtor, stringWriter.get())); - if (printWriter.get() == NULL) { - return false; - } - - scoped_local_ref exceptionClass(env, (*env)->GetObjectClass(e, exception)); // can't fail - jmethodID printStackTraceMethod = - (*env)->GetMethodID(e, exceptionClass.get(), "printStackTrace", "(Ljava/io/PrintWriter;)V"); - (*env)->CallVoidMethod(e, exception, printStackTraceMethod, printWriter.get()); - - if ((*env)->ExceptionCheck(e)) { - return false; - } - - scoped_local_ref messageStr(env, - (jstring) (*env)->CallObjectMethod(e, stringWriter.get(), stringWriterToStringMethod)); - if (messageStr.get() == NULL) { - return false; - } - - const char* utfChars = (*env)->GetStringUTFChars(e, messageStr.get(), NULL); - if (utfChars == NULL) { - return false; - } - - result = utfChars; - - (*env)->ReleaseStringUTFChars(e, messageStr.get(), utfChars); - return true; -} - -extern "C" int jniThrowException(C_JNIEnv* env, const char* className, const char* msg) { - JNIEnv* e = reinterpret_cast(env); - - if ((*env)->ExceptionCheck(e)) { - /* TODO: consider creating the new exception with this as "cause" */ - scoped_local_ref exception(env, (*env)->ExceptionOccurred(e)); - (*env)->ExceptionClear(e); - - if (exception.get() != NULL) { - std::string text; - getExceptionSummary(env, exception.get(), text); - ALOGW("Discarding pending exception (%s) to throw %s", text.c_str(), className); - } - } - - scoped_local_ref exceptionClass(env, findClass(env, className)); - if (exceptionClass.get() == NULL) { - ALOGE("Unable to find exception class %s", className); - /* ClassNotFoundException now pending */ - return -1; - } - - if ((*env)->ThrowNew(e, exceptionClass.get(), msg) != JNI_OK) { - ALOGE("Failed throwing '%s' '%s'", className, msg); - /* an exception, most likely OOM, will now be pending */ - return -1; - } - - return 0; -} - -int jniThrowExceptionFmt(C_JNIEnv* env, const char* className, const char* fmt, va_list args) { - char msgBuf[512]; - vsnprintf(msgBuf, sizeof(msgBuf), fmt, args); - return jniThrowException(env, className, msgBuf); -} - -int jniThrowNullPointerException(C_JNIEnv* env, const char* msg) { - return jniThrowException(env, "java/lang/NullPointerException", msg); -} - -int jniThrowRuntimeException(C_JNIEnv* env, const char* msg) { - return jniThrowException(env, "java/lang/RuntimeException", msg); -} - -int jniThrowIOException(C_JNIEnv* env, int errnum) { - char buffer[80]; - const char* message = jniStrError(errnum, buffer, sizeof(buffer)); - return jniThrowException(env, "java/io/IOException", message); -} - -static std::string jniGetStackTrace(C_JNIEnv* env, jthrowable exception) { - JNIEnv* e = reinterpret_cast(env); - - scoped_local_ref currentException(env, (*env)->ExceptionOccurred(e)); - if (exception == NULL) { - exception = currentException.get(); - if (exception == NULL) { - return ""; - } - } - - if (currentException.get() != NULL) { - (*env)->ExceptionClear(e); - } - - std::string trace; - if (!getStackTrace(env, exception, trace)) { - (*env)->ExceptionClear(e); - getExceptionSummary(env, exception, trace); - } - - if (currentException.get() != NULL) { - (*env)->Throw(e, currentException.get()); // rethrow - } - - return trace; -} - -void jniLogException(C_JNIEnv* env, int priority, const char* tag, jthrowable exception) { - std::string trace(jniGetStackTrace(env, exception)); - __android_log_write(priority, tag, trace.c_str()); -} - -const char* jniStrError(int errnum, char* buf, size_t buflen) { -#if __GLIBC__ - // Note: glibc has a nonstandard strerror_r that returns char* rather than POSIX's int. - // char *strerror_r(int errnum, char *buf, size_t n); - return strerror_r(errnum, buf, buflen); -#else - int rc = strerror_r(errnum, buf, buflen); - if (rc != 0) { - // (POSIX only guarantees a value other than 0. The safest - // way to implement this function is to use C++ and overload on the - // type of strerror_r to accurately distinguish GNU from POSIX.) - snprintf(buf, buflen, "errno %d", errnum); - } - return buf; -#endif -} - -jobject jniCreateFileDescriptor(C_JNIEnv* env, int fd) { - JNIEnv* e = reinterpret_cast(env); - static jmethodID ctor = e->GetMethodID(JniConstants::fileDescriptorClass, "", "()V"); - jobject fileDescriptor = (*env)->NewObject(e, JniConstants::fileDescriptorClass, ctor); - // NOTE: NewObject ensures that an OutOfMemoryError will be seen by the Java - // caller if the alloc fails, so we just return NULL when that happens. - if (fileDescriptor != NULL) { - jniSetFileDescriptorOfFD(env, fileDescriptor, fd); - } - return fileDescriptor; -} - -int jniGetFDFromFileDescriptor(C_JNIEnv* env, jobject fileDescriptor) { - JNIEnv* e = reinterpret_cast(env); - static jfieldID fid = e->GetFieldID(JniConstants::fileDescriptorClass, "descriptor", "I"); - if (fileDescriptor != NULL) { - return (*env)->GetIntField(e, fileDescriptor, fid); - } else { - return -1; - } -} - -void jniSetFileDescriptorOfFD(C_JNIEnv* env, jobject fileDescriptor, int value) { - JNIEnv* e = reinterpret_cast(env); - static jfieldID fid = e->GetFieldID(JniConstants::fileDescriptorClass, "descriptor", "I"); - (*env)->SetIntField(e, fileDescriptor, fid, value); -} - -jobject jniGetReferent(C_JNIEnv* env, jobject ref) { - JNIEnv* e = reinterpret_cast(env); - static jmethodID get = e->GetMethodID(JniConstants::referenceClass, "get", "()Ljava/lang/Object;"); - return (*env)->CallObjectMethod(e, ref, get); -} - diff --git a/android/library/src/main/jni/libs/nativehelper/JniConstants.cpp b/android/library/src/main/jni/libs/nativehelper/JniConstants.cpp deleted file mode 100644 index b2b966f..0000000 --- a/android/library/src/main/jni/libs/nativehelper/JniConstants.cpp +++ /dev/null @@ -1,141 +0,0 @@ -/* - * Copyright (C) 2010 The Android Open Source Project - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -#define LOG_TAG "JniConstants" - -#include "ALog-priv.h" -#include "JniConstants.h" -#include "ScopedLocalRef.h" - -#include - -jclass JniConstants::bidiRunClass; -jclass JniConstants::bigDecimalClass; -jclass JniConstants::booleanClass; -jclass JniConstants::byteArrayClass; -jclass JniConstants::byteClass; -jclass JniConstants::calendarClass; -jclass JniConstants::characterClass; -jclass JniConstants::charsetICUClass; -jclass JniConstants::constructorClass; -jclass JniConstants::deflaterClass; -jclass JniConstants::doubleClass; -jclass JniConstants::errnoExceptionClass; -jclass JniConstants::fieldClass; -jclass JniConstants::fieldPositionIteratorClass; -jclass JniConstants::fileDescriptorClass; -jclass JniConstants::floatClass; -jclass JniConstants::gaiExceptionClass; -jclass JniConstants::inet6AddressClass; -jclass JniConstants::inetAddressClass; -jclass JniConstants::inetSocketAddressClass; -jclass JniConstants::inetUnixAddressClass; -jclass JniConstants::inflaterClass; -jclass JniConstants::inputStreamClass; -jclass JniConstants::integerClass; -jclass JniConstants::localeDataClass; -jclass JniConstants::longClass; -jclass JniConstants::methodClass; -jclass JniConstants::mutableIntClass; -jclass JniConstants::mutableLongClass; -jclass JniConstants::objectClass; -jclass JniConstants::objectArrayClass; -jclass JniConstants::outputStreamClass; -jclass JniConstants::parsePositionClass; -jclass JniConstants::patternSyntaxExceptionClass; -jclass JniConstants::realToStringClass; -jclass JniConstants::referenceClass; -jclass JniConstants::shortClass; -jclass JniConstants::socketClass; -jclass JniConstants::socketImplClass; -jclass JniConstants::stringClass; -jclass JniConstants::structAddrinfoClass; -jclass JniConstants::structFlockClass; -jclass JniConstants::structGroupReqClass; -jclass JniConstants::structGroupSourceReqClass; -jclass JniConstants::structLingerClass; -jclass JniConstants::structPasswdClass; -jclass JniConstants::structPollfdClass; -jclass JniConstants::structStatClass; -jclass JniConstants::structStatVfsClass; -jclass JniConstants::structTimevalClass; -jclass JniConstants::structUcredClass; -jclass JniConstants::structUtsnameClass; - -static jclass findClass(JNIEnv* env, const char* name) { - ScopedLocalRef localClass(env, env->FindClass(name)); - jclass result = reinterpret_cast(env->NewGlobalRef(localClass.get())); - if (result == NULL) { - ALOGE("failed to find class '%s'", name); - abort(); - } - return result; -} - -void JniConstants::init(JNIEnv* env) { - bidiRunClass = findClass(env, "java/text/Bidi$Run"); - bigDecimalClass = findClass(env, "java/math/BigDecimal"); - booleanClass = findClass(env, "java/lang/Boolean"); - byteClass = findClass(env, "java/lang/Byte"); - byteArrayClass = findClass(env, "[B"); - calendarClass = findClass(env, "java/util/Calendar"); - characterClass = findClass(env, "java/lang/Character"); - charsetICUClass = findClass(env, "java/nio/charset/CharsetICU"); - constructorClass = findClass(env, "java/lang/reflect/Constructor"); - floatClass = findClass(env, "java/lang/Float"); - deflaterClass = findClass(env, "java/util/zip/Deflater"); - doubleClass = findClass(env, "java/lang/Double"); - errnoExceptionClass = findClass(env, "android/system/ErrnoException"); - fieldClass = findClass(env, "java/lang/reflect/Field"); - fieldPositionIteratorClass = findClass(env, "libcore/icu/NativeDecimalFormat$FieldPositionIterator"); - fileDescriptorClass = findClass(env, "java/io/FileDescriptor"); - gaiExceptionClass = findClass(env, "android/system/GaiException"); - inet6AddressClass = findClass(env, "java/net/Inet6Address"); - inetAddressClass = findClass(env, "java/net/InetAddress"); - inetSocketAddressClass = findClass(env, "java/net/InetSocketAddress"); - inetUnixAddressClass = findClass(env, "java/net/InetUnixAddress"); - inflaterClass = findClass(env, "java/util/zip/Inflater"); - inputStreamClass = findClass(env, "java/io/InputStream"); - integerClass = findClass(env, "java/lang/Integer"); - localeDataClass = findClass(env, "libcore/icu/LocaleData"); - longClass = findClass(env, "java/lang/Long"); - methodClass = findClass(env, "java/lang/reflect/Method"); - mutableIntClass = findClass(env, "android/util/MutableInt"); - mutableLongClass = findClass(env, "android/util/MutableLong"); - objectClass = findClass(env, "java/lang/Object"); - objectArrayClass = findClass(env, "[Ljava/lang/Object;"); - outputStreamClass = findClass(env, "java/io/OutputStream"); - parsePositionClass = findClass(env, "java/text/ParsePosition"); - patternSyntaxExceptionClass = findClass(env, "java/util/regex/PatternSyntaxException"); - realToStringClass = findClass(env, "java/lang/RealToString"); - referenceClass = findClass(env, "java/lang/ref/Reference"); - shortClass = findClass(env, "java/lang/Short"); - socketClass = findClass(env, "java/net/Socket"); - socketImplClass = findClass(env, "java/net/SocketImpl"); - stringClass = findClass(env, "java/lang/String"); - structAddrinfoClass = findClass(env, "android/system/StructAddrinfo"); - structFlockClass = findClass(env, "android/system/StructFlock"); - structGroupReqClass = findClass(env, "android/system/StructGroupReq"); - structGroupSourceReqClass = findClass(env, "android/system/StructGroupSourceReq"); - structLingerClass = findClass(env, "android/system/StructLinger"); - structPasswdClass = findClass(env, "android/system/StructPasswd"); - structPollfdClass = findClass(env, "android/system/StructPollfd"); - structStatClass = findClass(env, "android/system/StructStat"); - structStatVfsClass = findClass(env, "android/system/StructStatVfs"); - structTimevalClass = findClass(env, "android/system/StructTimeval"); - structUcredClass = findClass(env, "android/system/StructUcred"); - structUtsnameClass = findClass(env, "android/system/StructUtsname"); -} diff --git a/android/library/src/main/jni/libs/nativehelper/JniInvocation.cpp b/android/library/src/main/jni/libs/nativehelper/JniInvocation.cpp deleted file mode 100644 index f4dd24e..0000000 --- a/android/library/src/main/jni/libs/nativehelper/JniInvocation.cpp +++ /dev/null @@ -1,166 +0,0 @@ -/* - * Copyright (C) 2013 The Android Open Source Project - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -#include "JniInvocation.h" - -#include -#include -#include - -#include - -#define LOG_TAG "JniInvocation" -#include "cutils/log.h" - -#ifdef HAVE_ANDROID_OS -#include "cutils/properties.h" -#endif - -JniInvocation* JniInvocation::jni_invocation_ = NULL; - -JniInvocation::JniInvocation() : - handle_(NULL), - JNI_GetDefaultJavaVMInitArgs_(NULL), - JNI_CreateJavaVM_(NULL), - JNI_GetCreatedJavaVMs_(NULL) { - - LOG_ALWAYS_FATAL_IF(jni_invocation_ != NULL, "JniInvocation instance already initialized"); - jni_invocation_ = this; -} - -JniInvocation::~JniInvocation() { - jni_invocation_ = NULL; - if (handle_ != NULL) { - dlclose(handle_); - } -} - -#ifdef HAVE_ANDROID_OS -static const char* kLibrarySystemProperty = "persist.sys.dalvik.vm.lib.2"; -static const char* kDebuggableSystemProperty = "ro.debuggable"; -static const char* kDebuggableFallback = "0"; // Not debuggable. -#endif -static const char* kLibraryFallback = "libart.so"; - -const char* JniInvocation::GetLibrary(const char* library) { -#ifdef HAVE_ANDROID_OS - char default_library[PROPERTY_VALUE_MAX]; - - char debuggable[PROPERTY_VALUE_MAX]; - property_get(kDebuggableSystemProperty, debuggable, kDebuggableFallback); - - if (strcmp(debuggable, "1") != 0) { - // Not a debuggable build. - // Do not allow arbitrary library. Ignore the library parameter. This - // will also ignore the default library, but initialize to empty string - // for cleanliness. - library = kLibraryFallback; - default_library[0] = 0; - } else { - // Debuggable build. - // Accept the library parameter. For the case it is NULL, load the default - // library from the system property. - property_get(kLibrarySystemProperty, default_library, kLibraryFallback); - } -#else - const char* default_library = kLibraryFallback; -#endif - if (library == NULL) { - library = default_library; - } - - return library; -} - -bool JniInvocation::Init(const char* library) { - library = GetLibrary(library); - - handle_ = dlopen(library, RTLD_NOW); - if (handle_ == NULL) { - if (strcmp(library, kLibraryFallback) == 0) { - // Nothing else to try. - ALOGE("Failed to dlopen %s: %s", library, dlerror()); - return false; - } - // Note that this is enough to get something like the zygote - // running, we can't property_set here to fix this for the future - // because we are root and not the system user. See - // RuntimeInit.commonInit for where we fix up the property to - // avoid future fallbacks. http://b/11463182 - ALOGW("Falling back from %s to %s after dlopen error: %s", - library, kLibraryFallback, dlerror()); - library = kLibraryFallback; - handle_ = dlopen(library, RTLD_NOW); - if (handle_ == NULL) { - ALOGE("Failed to dlopen %s: %s", library, dlerror()); - return false; - } - } - if (!FindSymbol(reinterpret_cast(&JNI_GetDefaultJavaVMInitArgs_), - "JNI_GetDefaultJavaVMInitArgs")) { - return false; - } - if (!FindSymbol(reinterpret_cast(&JNI_CreateJavaVM_), - "JNI_CreateJavaVM")) { - return false; - } - if (!FindSymbol(reinterpret_cast(&JNI_GetCreatedJavaVMs_), - "JNI_GetCreatedJavaVMs")) { - return false; - } - return true; -} - -jint JniInvocation::JNI_GetDefaultJavaVMInitArgs(void* vmargs) { - return JNI_GetDefaultJavaVMInitArgs_(vmargs); -} - -jint JniInvocation::JNI_CreateJavaVM(JavaVM** p_vm, JNIEnv** p_env, void* vm_args) { - return JNI_CreateJavaVM_(p_vm, p_env, vm_args); -} - -jint JniInvocation::JNI_GetCreatedJavaVMs(JavaVM** vms, jsize size, jsize* vm_count) { - return JNI_GetCreatedJavaVMs_(vms, size, vm_count); -} - -bool JniInvocation::FindSymbol(void** pointer, const char* symbol) { - *pointer = dlsym(handle_, symbol); - if (*pointer == NULL) { - ALOGE("Failed to find symbol %s: %s\n", symbol, dlerror()); - dlclose(handle_); - handle_ = NULL; - return false; - } - return true; -} - -JniInvocation& JniInvocation::GetJniInvocation() { - LOG_ALWAYS_FATAL_IF(jni_invocation_ == NULL, - "Failed to create JniInvocation instance before using JNI invocation API"); - return *jni_invocation_; -} - -extern "C" jint JNI_GetDefaultJavaVMInitArgs(void* vm_args) { - return JniInvocation::GetJniInvocation().JNI_GetDefaultJavaVMInitArgs(vm_args); -} - -extern "C" jint JNI_CreateJavaVM(JavaVM** p_vm, JNIEnv** p_env, void* vm_args) { - return JniInvocation::GetJniInvocation().JNI_CreateJavaVM(p_vm, p_env, vm_args); -} - -extern "C" jint JNI_GetCreatedJavaVMs(JavaVM** vms, jsize size, jsize* vm_count) { - return JniInvocation::GetJniInvocation().JNI_GetCreatedJavaVMs(vms, size, vm_count); -} diff --git a/android/library/src/main/jni/libs/nativehelper/MODULE_LICENSE_APACHE2 b/android/library/src/main/jni/libs/nativehelper/MODULE_LICENSE_APACHE2 deleted file mode 100644 index e69de29..0000000 diff --git a/android/library/src/main/jni/libs/nativehelper/NOTICE b/android/library/src/main/jni/libs/nativehelper/NOTICE deleted file mode 100644 index c5b1efa..0000000 --- a/android/library/src/main/jni/libs/nativehelper/NOTICE +++ /dev/null @@ -1,190 +0,0 @@ - - Copyright (c) 2005-2008, The Android Open Source Project - - Licensed under the Apache License, Version 2.0 (the "License"); - you may not use this file except in compliance with the License. - - Unless required by applicable law or agreed to in writing, software - distributed under the License is distributed on an "AS IS" BASIS, - WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - See the License for the specific language governing permissions and - limitations under the License. - - - Apache License - Version 2.0, January 2004 - http://www.apache.org/licenses/ - - TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION - - 1. Definitions. - - "License" shall mean the terms and conditions for use, reproduction, - and distribution as defined by Sections 1 through 9 of this document. - - "Licensor" shall mean the copyright owner or entity authorized by - the copyright owner that is granting the License. - - "Legal Entity" shall mean the union of the acting entity and all - other entities that control, are controlled by, or are under common - control with that entity. For the purposes of this definition, - "control" means (i) the power, direct or indirect, to cause the - direction or management of such entity, whether by contract or - otherwise, or (ii) ownership of fifty percent (50%) or more of the - outstanding shares, or (iii) beneficial ownership of such entity. - - "You" (or "Your") shall mean an individual or Legal Entity - exercising permissions granted by this License. - - "Source" form shall mean the preferred form for making modifications, - including but not limited to software source code, documentation - source, and configuration files. - - "Object" form shall mean any form resulting from mechanical - transformation or translation of a Source form, including but - not limited to compiled object code, generated documentation, - and conversions to other media types. - - "Work" shall mean the work of authorship, whether in Source or - Object form, made available under the License, as indicated by a - copyright notice that is included in or attached to the work - (an example is provided in the Appendix below). - - "Derivative Works" shall mean any work, whether in Source or Object - form, that is based on (or derived from) the Work and for which the - editorial revisions, annotations, elaborations, or other modifications - represent, as a whole, an original work of authorship. For the purposes - of this License, Derivative Works shall not include works that remain - separable from, or merely link (or bind by name) to the interfaces of, - the Work and Derivative Works thereof. - - "Contribution" shall mean any work of authorship, including - the original version of the Work and any modifications or additions - to that Work or Derivative Works thereof, that is intentionally - submitted to Licensor for inclusion in the Work by the copyright owner - or by an individual or Legal Entity authorized to submit on behalf of - the copyright owner. For the purposes of this definition, "submitted" - means any form of electronic, verbal, or written communication sent - to the Licensor or its representatives, including but not limited to - communication on electronic mailing lists, source code control systems, - and issue tracking systems that are managed by, or on behalf of, the - Licensor for the purpose of discussing and improving the Work, but - excluding communication that is conspicuously marked or otherwise - designated in writing by the copyright owner as "Not a Contribution." - - "Contributor" shall mean Licensor and any individual or Legal Entity - on behalf of whom a Contribution has been received by Licensor and - subsequently incorporated within the Work. - - 2. Grant of Copyright License. Subject to the terms and conditions of - this License, each Contributor hereby grants to You a perpetual, - worldwide, non-exclusive, no-charge, royalty-free, irrevocable - copyright license to reproduce, prepare Derivative Works of, - publicly display, publicly perform, sublicense, and distribute the - Work and such Derivative Works in Source or Object form. - - 3. Grant of Patent License. Subject to the terms and conditions of - this License, each Contributor hereby grants to You a perpetual, - worldwide, non-exclusive, no-charge, royalty-free, irrevocable - (except as stated in this section) patent license to make, have made, - use, offer to sell, sell, import, and otherwise transfer the Work, - where such license applies only to those patent claims licensable - by such Contributor that are necessarily infringed by their - Contribution(s) alone or by combination of their Contribution(s) - with the Work to which such Contribution(s) was submitted. If You - institute patent litigation against any entity (including a - cross-claim or counterclaim in a lawsuit) alleging that the Work - or a Contribution incorporated within the Work constitutes direct - or contributory patent infringement, then any patent licenses - granted to You under this License for that Work shall terminate - as of the date such litigation is filed. - - 4. Redistribution. You may reproduce and distribute copies of the - Work or Derivative Works thereof in any medium, with or without - modifications, and in Source or Object form, provided that You - meet the following conditions: - - (a) You must give any other recipients of the Work or - Derivative Works a copy of this License; and - - (b) You must cause any modified files to carry prominent notices - stating that You changed the files; and - - (c) You must retain, in the Source form of any Derivative Works - that You distribute, all copyright, patent, trademark, and - attribution notices from the Source form of the Work, - excluding those notices that do not pertain to any part of - the Derivative Works; and - - (d) If the Work includes a "NOTICE" text file as part of its - distribution, then any Derivative Works that You distribute must - include a readable copy of the attribution notices contained - within such NOTICE file, excluding those notices that do not - pertain to any part of the Derivative Works, in at least one - of the following places: within a NOTICE text file distributed - as part of the Derivative Works; within the Source form or - documentation, if provided along with the Derivative Works; or, - within a display generated by the Derivative Works, if and - wherever such third-party notices normally appear. The contents - of the NOTICE file are for informational purposes only and - do not modify the License. You may add Your own attribution - notices within Derivative Works that You distribute, alongside - or as an addendum to the NOTICE text from the Work, provided - that such additional attribution notices cannot be construed - as modifying the License. - - You may add Your own copyright statement to Your modifications and - may provide additional or different license terms and conditions - for use, reproduction, or distribution of Your modifications, or - for any such Derivative Works as a whole, provided Your use, - reproduction, and distribution of the Work otherwise complies with - the conditions stated in this License. - - 5. Submission of Contributions. Unless You explicitly state otherwise, - any Contribution intentionally submitted for inclusion in the Work - by You to the Licensor shall be under the terms and conditions of - this License, without any additional terms or conditions. - Notwithstanding the above, nothing herein shall supersede or modify - the terms of any separate license agreement you may have executed - with Licensor regarding such Contributions. - - 6. Trademarks. This License does not grant permission to use the trade - names, trademarks, service marks, or product names of the Licensor, - except as required for reasonable and customary use in describing the - origin of the Work and reproducing the content of the NOTICE file. - - 7. Disclaimer of Warranty. Unless required by applicable law or - agreed to in writing, Licensor provides the Work (and each - Contributor provides its Contributions) on an "AS IS" BASIS, - WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or - implied, including, without limitation, any warranties or conditions - of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A - PARTICULAR PURPOSE. You are solely responsible for determining the - appropriateness of using or redistributing the Work and assume any - risks associated with Your exercise of permissions under this License. - - 8. Limitation of Liability. In no event and under no legal theory, - whether in tort (including negligence), contract, or otherwise, - unless required by applicable law (such as deliberate and grossly - negligent acts) or agreed to in writing, shall any Contributor be - liable to You for damages, including any direct, indirect, special, - incidental, or consequential damages of any character arising as a - result of this License or out of the use or inability to use the - Work (including but not limited to damages for loss of goodwill, - work stoppage, computer failure or malfunction, or any and all - other commercial damages or losses), even if such Contributor - has been advised of the possibility of such damages. - - 9. Accepting Warranty or Additional Liability. While redistributing - the Work or Derivative Works thereof, You may choose to offer, - and charge a fee for, acceptance of support, warranty, indemnity, - or other liability obligations and/or rights consistent with this - License. However, in accepting such obligations, You may act only - on Your own behalf and on Your sole responsibility, not on behalf - of any other Contributor, and only if You agree to indemnify, - defend, and hold each Contributor harmless for any liability - incurred by, or claims asserted against, such Contributor by reason - of your accepting any such warranty or additional liability. - - END OF TERMS AND CONDITIONS - diff --git a/android/library/src/main/jni/libs/nativehelper/README b/android/library/src/main/jni/libs/nativehelper/README deleted file mode 100644 index 5a5f5d4..0000000 --- a/android/library/src/main/jni/libs/nativehelper/README +++ /dev/null @@ -1,11 +0,0 @@ -Support functions for Android's class libraries - - -These are VM-agnostic native functions that implement methods for system -class libraries. All code here: - - - MUST not be associated with an android.* class (that code lives in - frameworks/base/). - - SHOULD be written in C rather than C++ where possible. - -Some helper functions are defined in include/nativehelper/JNIHelp.h. diff --git a/android/library/src/main/jni/libs/nativehelper/include/nativehelper/JNIHelp.h b/android/library/src/main/jni/libs/nativehelper/include/nativehelper/JNIHelp.h deleted file mode 100644 index cfab8f7..0000000 --- a/android/library/src/main/jni/libs/nativehelper/include/nativehelper/JNIHelp.h +++ /dev/null @@ -1,192 +0,0 @@ -/* - * Copyright (C) 2007 The Android Open Source Project - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -/* - * JNI helper functions. - * - * This file may be included by C or C++ code, which is trouble because jni.h - * uses different typedefs for JNIEnv in each language. - * - * TODO: remove C support. - */ -#ifndef NATIVEHELPER_JNIHELP_H_ -#define NATIVEHELPER_JNIHELP_H_ - -#include "jni.h" -#include -#include - -#ifndef NELEM -# define NELEM(x) ((int) (sizeof(x) / sizeof((x)[0]))) -#endif - -#ifdef __cplusplus -extern "C" { -#endif - -/* - * Register one or more native methods with a particular class. - * "className" looks like "java/lang/String". Aborts on failure. - * TODO: fix all callers and change the return type to void. - */ -int jniRegisterNativeMethods(C_JNIEnv* env, const char* className, const JNINativeMethod* gMethods, int numMethods); - -/* - * Throw an exception with the specified class and an optional message. - * - * The "className" argument will be passed directly to FindClass, which - * takes strings with slashes (e.g. "java/lang/Object"). - * - * If an exception is currently pending, we log a warning message and - * clear it. - * - * Returns 0 on success, nonzero if something failed (e.g. the exception - * class couldn't be found, so *an* exception will still be pending). - * - * Currently aborts the VM if it can't throw the exception. - */ -int jniThrowException(C_JNIEnv* env, const char* className, const char* msg); - -/* - * Throw a java.lang.NullPointerException, with an optional message. - */ -int jniThrowNullPointerException(C_JNIEnv* env, const char* msg); - -/* - * Throw a java.lang.RuntimeException, with an optional message. - */ -int jniThrowRuntimeException(C_JNIEnv* env, const char* msg); - -/* - * Throw a java.io.IOException, generating the message from errno. - */ -int jniThrowIOException(C_JNIEnv* env, int errnum); - -/* - * Return a pointer to a locale-dependent error string explaining errno - * value 'errnum'. The returned pointer may or may not be equal to 'buf'. - * This function is thread-safe (unlike strerror) and portable (unlike - * strerror_r). - */ -const char* jniStrError(int errnum, char* buf, size_t buflen); - -/* - * Returns a new java.io.FileDescriptor for the given int fd. - */ -jobject jniCreateFileDescriptor(C_JNIEnv* env, int fd); - -/* - * Returns the int fd from a java.io.FileDescriptor. - */ -int jniGetFDFromFileDescriptor(C_JNIEnv* env, jobject fileDescriptor); - -/* - * Sets the int fd in a java.io.FileDescriptor. - */ -void jniSetFileDescriptorOfFD(C_JNIEnv* env, jobject fileDescriptor, int value); - -/* - * Returns the reference from a java.lang.ref.Reference. - */ -jobject jniGetReferent(C_JNIEnv* env, jobject ref); - -/* - * Log a message and an exception. - * If exception is NULL, logs the current exception in the JNI environment. - */ -void jniLogException(C_JNIEnv* env, int priority, const char* tag, jthrowable exception); - -#ifdef __cplusplus -} -#endif - - -/* - * For C++ code, we provide inlines that map to the C functions. g++ always - * inlines these, even on non-optimized builds. - */ -#if defined(__cplusplus) -inline int jniRegisterNativeMethods(JNIEnv* env, const char* className, const JNINativeMethod* gMethods, int numMethods) { - return jniRegisterNativeMethods(&env->functions, className, gMethods, numMethods); -} - -inline int jniThrowException(JNIEnv* env, const char* className, const char* msg) { - return jniThrowException(&env->functions, className, msg); -} - -extern "C" int jniThrowExceptionFmt(C_JNIEnv* env, const char* className, const char* fmt, va_list args); - -/* - * Equivalent to jniThrowException but with a printf-like format string and - * variable-length argument list. This is only available in C++. - */ -inline int jniThrowExceptionFmt(JNIEnv* env, const char* className, const char* fmt, ...) { - va_list args; - va_start(args, fmt); - return jniThrowExceptionFmt(&env->functions, className, fmt, args); - va_end(args); -} - -inline int jniThrowNullPointerException(JNIEnv* env, const char* msg) { - return jniThrowNullPointerException(&env->functions, msg); -} - -inline int jniThrowRuntimeException(JNIEnv* env, const char* msg) { - return jniThrowRuntimeException(&env->functions, msg); -} - -inline int jniThrowIOException(JNIEnv* env, int errnum) { - return jniThrowIOException(&env->functions, errnum); -} - -inline jobject jniCreateFileDescriptor(JNIEnv* env, int fd) { - return jniCreateFileDescriptor(&env->functions, fd); -} - -inline int jniGetFDFromFileDescriptor(JNIEnv* env, jobject fileDescriptor) { - return jniGetFDFromFileDescriptor(&env->functions, fileDescriptor); -} - -inline void jniSetFileDescriptorOfFD(JNIEnv* env, jobject fileDescriptor, int value) { - jniSetFileDescriptorOfFD(&env->functions, fileDescriptor, value); -} - -inline jobject jniGetReferent(JNIEnv* env, jobject ref) { - return jniGetReferent(&env->functions, ref); -} - -inline void jniLogException(JNIEnv* env, int priority, const char* tag, jthrowable exception = NULL) { - jniLogException(&env->functions, priority, tag, exception); -} - -#endif - -/* - * TEMP_FAILURE_RETRY is defined by some, but not all, versions of - * . (Alas, it is not as standard as we'd hoped!) So, if it's - * not already defined, then define it here. - */ -#ifndef TEMP_FAILURE_RETRY -/* Used to retry syscalls that can return EINTR. */ -#define TEMP_FAILURE_RETRY(exp) ({ \ - typeof (exp) _rc; \ - do { \ - _rc = (exp); \ - } while (_rc == -1 && errno == EINTR); \ - _rc; }) -#endif - -#endif /* NATIVEHELPER_JNIHELP_H_ */ diff --git a/android/library/src/main/jni/libs/nativehelper/include/nativehelper/JniConstants.h b/android/library/src/main/jni/libs/nativehelper/include/nativehelper/JniConstants.h deleted file mode 100644 index f5498c7..0000000 --- a/android/library/src/main/jni/libs/nativehelper/include/nativehelper/JniConstants.h +++ /dev/null @@ -1,100 +0,0 @@ -/* - * Copyright (C) 2010 The Android Open Source Project - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -#ifndef JNI_CONSTANTS_H_included -#define JNI_CONSTANTS_H_included - -#include "JNIHelp.h" - -/** - * A cache to avoid calling FindClass at runtime. - * - * Class lookup is relatively expensive (2.5us on passion-eng at the time of writing), so we do - * all such lookups eagerly at startup. This means that code that never uses, say, - * java.util.zip.Deflater still has to pay for the lookup, but it means that on a device the cost - * is definitely paid during boot and amortized. A central cache also removes the temptation to - * dynamically call FindClass rather than add a small cache to each file that needs one. Another - * cost is that each class cached here requires a global reference, though in practice we save - * enough by not having a global reference for each file that uses a class such as java.lang.String - * which is used in several files. - * - * FindClass is still called in a couple of situations: when throwing exceptions, and in some of - * the serialization code. The former is clearly not a performance case, and we're currently - * assuming that neither is the latter. - * - * TODO: similar arguments hold for field and method IDs; we should cache them centrally too. - */ -struct JniConstants { - static void init(JNIEnv* env); - - static jclass bidiRunClass; - static jclass bigDecimalClass; - static jclass booleanClass; - static jclass byteArrayClass; - static jclass byteClass; - static jclass calendarClass; - static jclass characterClass; - static jclass charsetICUClass; - static jclass constructorClass; - static jclass deflaterClass; - static jclass doubleClass; - static jclass errnoExceptionClass; - static jclass fieldClass; - static jclass fieldPositionIteratorClass; - static jclass fileDescriptorClass; - static jclass floatClass; - static jclass gaiExceptionClass; - static jclass inet6AddressClass; - static jclass inetAddressClass; - static jclass inetSocketAddressClass; - static jclass inetUnixAddressClass; - static jclass inflaterClass; - static jclass inputStreamClass; - static jclass integerClass; - static jclass localeDataClass; - static jclass longClass; - static jclass methodClass; - static jclass mutableIntClass; - static jclass mutableLongClass; - static jclass objectClass; - static jclass objectArrayClass; - static jclass outputStreamClass; - static jclass parsePositionClass; - static jclass patternSyntaxExceptionClass; - static jclass realToStringClass; - static jclass referenceClass; - static jclass shortClass; - static jclass socketClass; - static jclass socketImplClass; - static jclass stringClass; - static jclass structAddrinfoClass; - static jclass structFlockClass; - static jclass structGroupReqClass; - static jclass structGroupSourceReqClass; - static jclass structLingerClass; - static jclass structPasswdClass; - static jclass structPollfdClass; - static jclass structStatClass; - static jclass structStatVfsClass; - static jclass structTimevalClass; - static jclass structUcredClass; - static jclass structUtsnameClass; -}; - -#define NATIVE_METHOD(className, functionName, signature) \ - { #functionName, signature, reinterpret_cast(className ## _ ## functionName) } - -#endif // JNI_CONSTANTS_H_included diff --git a/android/library/src/main/jni/libs/nativehelper/include/nativehelper/JniInvocation.h b/android/library/src/main/jni/libs/nativehelper/include/nativehelper/JniInvocation.h deleted file mode 100644 index b5198ff..0000000 --- a/android/library/src/main/jni/libs/nativehelper/include/nativehelper/JniInvocation.h +++ /dev/null @@ -1,66 +0,0 @@ -/* - * Copyright (C) 2013 The Android Open Source Project - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -#ifndef JNI_INVOCATION_H_included -#define JNI_INVOCATION_H_included - -#include - -// JniInvocation adds a layer of indirection for applications using -// the JNI invocation API to allow the JNI implementation to be -// selected dynamically. Apps can specify a specific implementation to -// be used by calling InitJniInvocation. If this is not done, the -// library will chosen based on the value of Android system property -// persist.sys.dalvik.vm.lib on the device, and otherwise fall back to -// a hard-coded default implementation. -class JniInvocation { - public: - JniInvocation(); - - ~JniInvocation(); - - // Initialize JNI invocation API. library should specifiy a valid - // shared library for opening via dlopen providing a JNI invocation - // implementation, or null to allow defaulting via - // persist.sys.dalvik.vm.lib. - bool Init(const char* library); - - // Exposes which library is actually loaded from the given name. - static const char* GetLibrary(const char* library); - - private: - - bool FindSymbol(void** pointer, const char* symbol); - - static JniInvocation& GetJniInvocation(); - - jint JNI_GetDefaultJavaVMInitArgs(void* vmargs); - jint JNI_CreateJavaVM(JavaVM** p_vm, JNIEnv** p_env, void* vm_args); - jint JNI_GetCreatedJavaVMs(JavaVM** vms, jsize size, jsize* vm_count); - - static JniInvocation* jni_invocation_; - - void* handle_; - jint (*JNI_GetDefaultJavaVMInitArgs_)(void*); - jint (*JNI_CreateJavaVM_)(JavaVM**, JNIEnv**, void*); - jint (*JNI_GetCreatedJavaVMs_)(JavaVM**, jsize, jsize*); - - friend jint JNI_GetDefaultJavaVMInitArgs(void* vm_args); - friend jint JNI_CreateJavaVM(JavaVM** p_vm, JNIEnv** p_env, void* vm_args); - friend jint JNI_GetCreatedJavaVMs(JavaVM** vms, jsize size, jsize* vm_count); -}; - -#endif // JNI_INVOCATION_H_included diff --git a/android/library/src/main/jni/libs/nativehelper/include/nativehelper/ScopedBytes.h b/android/library/src/main/jni/libs/nativehelper/include/nativehelper/ScopedBytes.h deleted file mode 100644 index 0aaefe2..0000000 --- a/android/library/src/main/jni/libs/nativehelper/include/nativehelper/ScopedBytes.h +++ /dev/null @@ -1,81 +0,0 @@ -/* - * Copyright (C) 2010 The Android Open Source Project - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -#ifndef SCOPED_BYTES_H_included -#define SCOPED_BYTES_H_included - -#include "JNIHelp.h" - -/** - * ScopedBytesRO and ScopedBytesRW attempt to paper over the differences between byte[]s and - * ByteBuffers. This in turn helps paper over the differences between non-direct ByteBuffers backed - * by byte[]s, direct ByteBuffers backed by bytes[]s, and direct ByteBuffers not backed by byte[]s. - * (On Android, this last group only contains MappedByteBuffers.) - */ -template -class ScopedBytes { -public: - ScopedBytes(JNIEnv* env, jbyteArray object) - : mEnv(env), mByteArray(object), mPtr(NULL) - { - if (mByteArray == NULL) { - jniThrowNullPointerException(mEnv, NULL); - } else { - mPtr = mEnv->GetByteArrayElements(mByteArray, NULL); - mLength = mEnv->GetArrayLength(mByteArray); - } - } - - ~ScopedBytes() { - if (mByteArray != NULL) { - mEnv->ReleaseByteArrayElements(mByteArray, mPtr, readOnly ? JNI_ABORT : 0); - } - } - jsize length() { - return mLength; - } - -private: - JNIEnv* mEnv; - jbyteArray mByteArray; - -protected: - jbyte* mPtr; - jsize mLength; - -private: - // Disallow copy and assignment. - ScopedBytes(const ScopedBytes&); - void operator=(const ScopedBytes&); -}; - -class ScopedBytesRO : public ScopedBytes { -public: - ScopedBytesRO(JNIEnv* env, jbyteArray object) : ScopedBytes(env, object) {} - const jbyte* get() const { - return mPtr; - } -}; - -class ScopedBytesRW : public ScopedBytes { -public: - ScopedBytesRW(JNIEnv* env, jbyteArray object) : ScopedBytes(env, object) {} - jbyte* get() { - return mPtr; - } -}; - -#endif // SCOPED_BYTES_H_included diff --git a/android/library/src/main/jni/libs/nativehelper/include/nativehelper/ScopedFd.h b/android/library/src/main/jni/libs/nativehelper/include/nativehelper/ScopedFd.h deleted file mode 100644 index 795f50c..0000000 --- a/android/library/src/main/jni/libs/nativehelper/include/nativehelper/ScopedFd.h +++ /dev/null @@ -1,60 +0,0 @@ -/* - * Copyright (C) 2009 The Android Open Source Project - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -#ifndef SCOPED_FD_H_included -#define SCOPED_FD_H_included - -#include -#include "JNIHelp.h" // for TEMP_FAILURE_RETRY - -// A smart pointer that closes the given fd on going out of scope. -// Use this when the fd is incidental to the purpose of your function, -// but needs to be cleaned up on exit. -class ScopedFd { -public: - explicit ScopedFd(int fd) : fd(fd) { - } - - ~ScopedFd() { - reset(); - } - - int get() const { - return fd; - } - - int release() __attribute__((warn_unused_result)) { - int localFd = fd; - fd = -1; - return localFd; - } - - void reset(int new_fd = -1) { - if (fd != -1) { - TEMP_FAILURE_RETRY(close(fd)); - } - fd = new_fd; - } - -private: - int fd; - - // Disallow copy and assignment. - ScopedFd(const ScopedFd&); - void operator=(const ScopedFd&); -}; - -#endif // SCOPED_FD_H_included diff --git a/android/library/src/main/jni/libs/nativehelper/include/nativehelper/ScopedLocalFrame.h b/android/library/src/main/jni/libs/nativehelper/include/nativehelper/ScopedLocalFrame.h deleted file mode 100644 index 35b6ad8..0000000 --- a/android/library/src/main/jni/libs/nativehelper/include/nativehelper/ScopedLocalFrame.h +++ /dev/null @@ -1,40 +0,0 @@ -/* - * Copyright (C) 2010 The Android Open Source Project - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -#ifndef SCOPED_LOCAL_FRAME_H_included -#define SCOPED_LOCAL_FRAME_H_included - -#include "JNIHelp.h" - -class ScopedLocalFrame { -public: - ScopedLocalFrame(JNIEnv* env) : mEnv(env) { - mEnv->PushLocalFrame(128); - } - - ~ScopedLocalFrame() { - mEnv->PopLocalFrame(NULL); - } - -private: - JNIEnv* mEnv; - - // Disallow copy and assignment. - ScopedLocalFrame(const ScopedLocalFrame&); - void operator=(const ScopedLocalFrame&); -}; - -#endif // SCOPED_LOCAL_FRAME_H_included diff --git a/android/library/src/main/jni/libs/nativehelper/include/nativehelper/ScopedLocalRef.h b/android/library/src/main/jni/libs/nativehelper/include/nativehelper/ScopedLocalRef.h deleted file mode 100644 index 71d5776..0000000 --- a/android/library/src/main/jni/libs/nativehelper/include/nativehelper/ScopedLocalRef.h +++ /dev/null @@ -1,63 +0,0 @@ -/* - * Copyright (C) 2010 The Android Open Source Project - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -#ifndef SCOPED_LOCAL_REF_H_included -#define SCOPED_LOCAL_REF_H_included - -#include "jni.h" - -#include - -// A smart pointer that deletes a JNI local reference when it goes out of scope. -template -class ScopedLocalRef { -public: - ScopedLocalRef(JNIEnv* env, T localRef) : mEnv(env), mLocalRef(localRef) { - } - - ~ScopedLocalRef() { - reset(); - } - - void reset(T ptr = NULL) { - if (ptr != mLocalRef) { - if (mLocalRef != NULL) { - mEnv->DeleteLocalRef(mLocalRef); - } - mLocalRef = ptr; - } - } - - T release() __attribute__((warn_unused_result)) { - T localRef = mLocalRef; - mLocalRef = NULL; - return localRef; - } - - T get() const { - return mLocalRef; - } - -private: - JNIEnv* mEnv; - T mLocalRef; - - // Disallow copy and assignment. - ScopedLocalRef(const ScopedLocalRef&); - void operator=(const ScopedLocalRef&); -}; - -#endif // SCOPED_LOCAL_REF_H_included diff --git a/android/library/src/main/jni/libs/nativehelper/include/nativehelper/ScopedPrimitiveArray.h b/android/library/src/main/jni/libs/nativehelper/include/nativehelper/ScopedPrimitiveArray.h deleted file mode 100644 index d797b9d..0000000 --- a/android/library/src/main/jni/libs/nativehelper/include/nativehelper/ScopedPrimitiveArray.h +++ /dev/null @@ -1,122 +0,0 @@ -/* - * Copyright (C) 2010 The Android Open Source Project - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -#ifndef SCOPED_PRIMITIVE_ARRAY_H_included -#define SCOPED_PRIMITIVE_ARRAY_H_included - -#include "JNIHelp.h" - -// ScopedBooleanArrayRO, ScopedByteArrayRO, ScopedCharArrayRO, ScopedDoubleArrayRO, -// ScopedFloatArrayRO, ScopedIntArrayRO, ScopedLongArrayRO, and ScopedShortArrayRO provide -// convenient read-only access to Java arrays from JNI code. This is cheaper than read-write -// access and should be used by default. -#define INSTANTIATE_SCOPED_PRIMITIVE_ARRAY_RO(PRIMITIVE_TYPE, NAME) \ - class Scoped ## NAME ## ArrayRO { \ - public: \ - explicit Scoped ## NAME ## ArrayRO(JNIEnv* env) \ - : mEnv(env), mJavaArray(NULL), mRawArray(NULL) {} \ - Scoped ## NAME ## ArrayRO(JNIEnv* env, PRIMITIVE_TYPE ## Array javaArray) \ - : mEnv(env), mJavaArray(javaArray), mRawArray(NULL) { \ - if (mJavaArray == NULL) { \ - jniThrowNullPointerException(mEnv, NULL); \ - } else { \ - mRawArray = mEnv->Get ## NAME ## ArrayElements(mJavaArray, NULL); \ - } \ - } \ - ~Scoped ## NAME ## ArrayRO() { \ - if (mRawArray) { \ - mEnv->Release ## NAME ## ArrayElements(mJavaArray, mRawArray, JNI_ABORT); \ - } \ - } \ - void reset(PRIMITIVE_TYPE ## Array javaArray) { \ - mJavaArray = javaArray; \ - mRawArray = mEnv->Get ## NAME ## ArrayElements(mJavaArray, NULL); \ - } \ - const PRIMITIVE_TYPE* get() const { return mRawArray; } \ - PRIMITIVE_TYPE ## Array getJavaArray() const { return mJavaArray; } \ - const PRIMITIVE_TYPE& operator[](size_t n) const { return mRawArray[n]; } \ - size_t size() const { return mEnv->GetArrayLength(mJavaArray); } \ - private: \ - JNIEnv* mEnv; \ - PRIMITIVE_TYPE ## Array mJavaArray; \ - PRIMITIVE_TYPE* mRawArray; \ - Scoped ## NAME ## ArrayRO(const Scoped ## NAME ## ArrayRO&); \ - void operator=(const Scoped ## NAME ## ArrayRO&); \ - } - -INSTANTIATE_SCOPED_PRIMITIVE_ARRAY_RO(jboolean, Boolean); -INSTANTIATE_SCOPED_PRIMITIVE_ARRAY_RO(jbyte, Byte); -INSTANTIATE_SCOPED_PRIMITIVE_ARRAY_RO(jchar, Char); -INSTANTIATE_SCOPED_PRIMITIVE_ARRAY_RO(jdouble, Double); -INSTANTIATE_SCOPED_PRIMITIVE_ARRAY_RO(jfloat, Float); -INSTANTIATE_SCOPED_PRIMITIVE_ARRAY_RO(jint, Int); -INSTANTIATE_SCOPED_PRIMITIVE_ARRAY_RO(jlong, Long); -INSTANTIATE_SCOPED_PRIMITIVE_ARRAY_RO(jshort, Short); - -#undef INSTANTIATE_SCOPED_PRIMITIVE_ARRAY_RO - -// ScopedBooleanArrayRW, ScopedByteArrayRW, ScopedCharArrayRW, ScopedDoubleArrayRW, -// ScopedFloatArrayRW, ScopedIntArrayRW, ScopedLongArrayRW, and ScopedShortArrayRW provide -// convenient read-write access to Java arrays from JNI code. These are more expensive, -// since they entail a copy back onto the Java heap, and should only be used when necessary. -#define INSTANTIATE_SCOPED_PRIMITIVE_ARRAY_RW(PRIMITIVE_TYPE, NAME) \ - class Scoped ## NAME ## ArrayRW { \ - public: \ - explicit Scoped ## NAME ## ArrayRW(JNIEnv* env) \ - : mEnv(env), mJavaArray(NULL), mRawArray(NULL) {} \ - Scoped ## NAME ## ArrayRW(JNIEnv* env, PRIMITIVE_TYPE ## Array javaArray) \ - : mEnv(env), mJavaArray(javaArray), mRawArray(NULL) { \ - if (mJavaArray == NULL) { \ - jniThrowNullPointerException(mEnv, NULL); \ - } else { \ - mRawArray = mEnv->Get ## NAME ## ArrayElements(mJavaArray, NULL); \ - } \ - } \ - ~Scoped ## NAME ## ArrayRW() { \ - if (mRawArray) { \ - mEnv->Release ## NAME ## ArrayElements(mJavaArray, mRawArray, 0); \ - } \ - } \ - void reset(PRIMITIVE_TYPE ## Array javaArray) { \ - mJavaArray = javaArray; \ - mRawArray = mEnv->Get ## NAME ## ArrayElements(mJavaArray, NULL); \ - } \ - const PRIMITIVE_TYPE* get() const { return mRawArray; } \ - PRIMITIVE_TYPE ## Array getJavaArray() const { return mJavaArray; } \ - const PRIMITIVE_TYPE& operator[](size_t n) const { return mRawArray[n]; } \ - PRIMITIVE_TYPE* get() { return mRawArray; } \ - PRIMITIVE_TYPE& operator[](size_t n) { return mRawArray[n]; } \ - size_t size() const { return mEnv->GetArrayLength(mJavaArray); } \ - private: \ - JNIEnv* mEnv; \ - PRIMITIVE_TYPE ## Array mJavaArray; \ - PRIMITIVE_TYPE* mRawArray; \ - Scoped ## NAME ## ArrayRW(const Scoped ## NAME ## ArrayRW&); \ - void operator=(const Scoped ## NAME ## ArrayRW&); \ - } - -INSTANTIATE_SCOPED_PRIMITIVE_ARRAY_RW(jboolean, Boolean); -INSTANTIATE_SCOPED_PRIMITIVE_ARRAY_RW(jbyte, Byte); -INSTANTIATE_SCOPED_PRIMITIVE_ARRAY_RW(jchar, Char); -INSTANTIATE_SCOPED_PRIMITIVE_ARRAY_RW(jdouble, Double); -INSTANTIATE_SCOPED_PRIMITIVE_ARRAY_RW(jfloat, Float); -INSTANTIATE_SCOPED_PRIMITIVE_ARRAY_RW(jint, Int); -INSTANTIATE_SCOPED_PRIMITIVE_ARRAY_RW(jlong, Long); -INSTANTIATE_SCOPED_PRIMITIVE_ARRAY_RW(jshort, Short); - -#undef INSTANTIATE_SCOPED_PRIMITIVE_ARRAY_RW - -#endif // SCOPED_PRIMITIVE_ARRAY_H_included diff --git a/android/library/src/main/jni/libs/nativehelper/include/nativehelper/ScopedStringChars.h b/android/library/src/main/jni/libs/nativehelper/include/nativehelper/ScopedStringChars.h deleted file mode 100644 index cfbd247..0000000 --- a/android/library/src/main/jni/libs/nativehelper/include/nativehelper/ScopedStringChars.h +++ /dev/null @@ -1,74 +0,0 @@ -/* - * Copyright (C) 2011 The Android Open Source Project - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -#ifndef SCOPED_STRING_CHARS_H_included -#define SCOPED_STRING_CHARS_H_included - -#include "JNIHelp.h" - -// A smart pointer that provides access to a jchar* given a JNI jstring. -// Unlike GetStringChars, we throw NullPointerException rather than abort if -// passed a null jstring, and get will return NULL. -// This makes the correct idiom very simple: -// -// ScopedStringChars name(env, java_name); -// if (name.get() == NULL) { -// return NULL; -// } -class ScopedStringChars { - public: - ScopedStringChars(JNIEnv* env, jstring s) : env_(env), string_(s), size_(0) { - if (s == NULL) { - chars_ = NULL; - jniThrowNullPointerException(env, NULL); - } else { - chars_ = env->GetStringChars(string_, NULL); - if (chars_ != NULL) { - size_ = env->GetStringLength(string_); - } - } - } - - ~ScopedStringChars() { - if (chars_ != NULL) { - env_->ReleaseStringChars(string_, chars_); - } - } - - const jchar* get() const { - return chars_; - } - - size_t size() const { - return size_; - } - - const jchar& operator[](size_t n) const { - return chars_[n]; - } - - private: - JNIEnv* env_; - jstring string_; - const jchar* chars_; - size_t size_; - - // Disallow copy and assignment. - ScopedStringChars(const ScopedStringChars&); - void operator=(const ScopedStringChars&); -}; - -#endif // SCOPED_STRING_CHARS_H_included diff --git a/android/library/src/main/jni/libs/nativehelper/include/nativehelper/ScopedUtfChars.h b/android/library/src/main/jni/libs/nativehelper/include/nativehelper/ScopedUtfChars.h deleted file mode 100644 index 7761450..0000000 --- a/android/library/src/main/jni/libs/nativehelper/include/nativehelper/ScopedUtfChars.h +++ /dev/null @@ -1,71 +0,0 @@ -/* - * Copyright (C) 2010 The Android Open Source Project - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -#ifndef SCOPED_UTF_CHARS_H_included -#define SCOPED_UTF_CHARS_H_included - -#include "JNIHelp.h" -#include - -// A smart pointer that provides read-only access to a Java string's UTF chars. -// Unlike GetStringUTFChars, we throw NullPointerException rather than abort if -// passed a null jstring, and c_str will return NULL. -// This makes the correct idiom very simple: -// -// ScopedUtfChars name(env, java_name); -// if (name.c_str() == NULL) { -// return NULL; -// } -class ScopedUtfChars { - public: - ScopedUtfChars(JNIEnv* env, jstring s) : env_(env), string_(s) { - if (s == NULL) { - utf_chars_ = NULL; - jniThrowNullPointerException(env, NULL); - } else { - utf_chars_ = env->GetStringUTFChars(s, NULL); - } - } - - ~ScopedUtfChars() { - if (utf_chars_) { - env_->ReleaseStringUTFChars(string_, utf_chars_); - } - } - - const char* c_str() const { - return utf_chars_; - } - - size_t size() const { - return strlen(utf_chars_); - } - - const char& operator[](size_t n) const { - return utf_chars_[n]; - } - - private: - JNIEnv* env_; - jstring string_; - const char* utf_chars_; - - // Disallow copy and assignment. - ScopedUtfChars(const ScopedUtfChars&); - void operator=(const ScopedUtfChars&); -}; - -#endif // SCOPED_UTF_CHARS_H_included diff --git a/android/library/src/main/jni/libs/nativehelper/include/nativehelper/UniquePtr.h b/android/library/src/main/jni/libs/nativehelper/include/nativehelper/UniquePtr.h deleted file mode 100644 index 50f75b2..0000000 --- a/android/library/src/main/jni/libs/nativehelper/include/nativehelper/UniquePtr.h +++ /dev/null @@ -1,241 +0,0 @@ -/* - * Copyright (C) 2010 The Android Open Source Project - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -#ifndef UNIQUE_PTR_H_included -#define UNIQUE_PTR_H_included - -#include // For NULL. - -// This is a fake declaration of std::swap to avoid including -namespace std { -template void swap(T&, T&); -} - -// Default deleter for pointer types. -template -struct DefaultDelete { - enum { type_must_be_complete = sizeof(T) }; - DefaultDelete() {} - void operator()(T* p) const { - delete p; - } -}; - -// Default deleter for array types. -template -struct DefaultDelete { - enum { type_must_be_complete = sizeof(T) }; - void operator()(T* p) const { - delete[] p; - } -}; - -// A smart pointer that deletes the given pointer on destruction. -// Equivalent to C++0x's std::unique_ptr (a combination of boost::scoped_ptr -// and boost::scoped_array). -// Named to be in keeping with Android style but also to avoid -// collision with any other implementation, until we can switch over -// to unique_ptr. -// Use thus: -// UniquePtr c(new C); -template > -class UniquePtr { -public: - // Construct a new UniquePtr, taking ownership of the given raw pointer. - explicit UniquePtr(T* ptr = NULL) : mPtr(ptr) { - } - - ~UniquePtr() { - reset(); - } - - // Accessors. - T& operator*() const { return *mPtr; } - T* operator->() const { return mPtr; } - T* get() const { return mPtr; } - - // Returns the raw pointer and hands over ownership to the caller. - // The pointer will not be deleted by UniquePtr. - T* release() __attribute__((warn_unused_result)) { - T* result = mPtr; - mPtr = NULL; - return result; - } - - // Takes ownership of the given raw pointer. - // If this smart pointer previously owned a different raw pointer, that - // raw pointer will be freed. - void reset(T* ptr = NULL) { - if (ptr != mPtr) { - D()(mPtr); - mPtr = ptr; - } - } - - // Swap with another unique pointer. - void swap(UniquePtr& other) { - std::swap(mPtr, other.mPtr); - } - -private: - // The raw pointer. - T* mPtr; - - // Comparing unique pointers is probably a mistake, since they're unique. - template bool operator==(const UniquePtr& p) const; - template bool operator!=(const UniquePtr& p) const; - - // Disallow copy and assignment. - UniquePtr(const UniquePtr&); - void operator=(const UniquePtr&); -}; - -// Partial specialization for array types. Like std::unique_ptr, this removes -// operator* and operator-> but adds operator[]. -template -class UniquePtr { -public: - explicit UniquePtr(T* ptr = NULL) : mPtr(ptr) { - } - - ~UniquePtr() { - reset(); - } - - T& operator[](size_t i) const { - return mPtr[i]; - } - T* get() const { return mPtr; } - - T* release() __attribute__((warn_unused_result)) { - T* result = mPtr; - mPtr = NULL; - return result; - } - - void reset(T* ptr = NULL) { - if (ptr != mPtr) { - D()(mPtr); - mPtr = ptr; - } - } - -private: - T* mPtr; - - // Disallow copy and assignment. - UniquePtr(const UniquePtr&); - void operator=(const UniquePtr&); -}; - -#if UNIQUE_PTR_TESTS - -// Run these tests with: -// g++ -g -DUNIQUE_PTR_TESTS -x c++ UniquePtr.h && ./a.out - -#include - -static void assert(bool b) { - if (!b) { - fprintf(stderr, "FAIL\n"); - abort(); - } - fprintf(stderr, "OK\n"); -} -static int cCount = 0; -struct C { - C() { ++cCount; } - ~C() { --cCount; } -}; -static bool freed = false; -struct Freer { - void operator()(int* p) { - assert(*p == 123); - free(p); - freed = true; - } -}; - -int main(int argc, char* argv[]) { - // - // UniquePtr tests... - // - - // Can we free a single object? - { - UniquePtr c(new C); - assert(cCount == 1); - } - assert(cCount == 0); - // Does release work? - C* rawC; - { - UniquePtr c(new C); - assert(cCount == 1); - rawC = c.release(); - } - assert(cCount == 1); - delete rawC; - // Does reset work? - { - UniquePtr c(new C); - assert(cCount == 1); - c.reset(new C); - assert(cCount == 1); - } - assert(cCount == 0); - - // - // UniquePtr tests... - // - - // Can we free an array? - { - UniquePtr cs(new C[4]); - assert(cCount == 4); - } - assert(cCount == 0); - // Does release work? - { - UniquePtr c(new C[4]); - assert(cCount == 4); - rawC = c.release(); - } - assert(cCount == 4); - delete[] rawC; - // Does reset work? - { - UniquePtr c(new C[4]); - assert(cCount == 4); - c.reset(new C[2]); - assert(cCount == 2); - } - assert(cCount == 0); - - // - // Custom deleter tests... - // - assert(!freed); - { - UniquePtr i(reinterpret_cast(malloc(sizeof(int)))); - *i = 123; - } - assert(freed); - return 0; -} -#endif - -#endif // UNIQUE_PTR_H_included diff --git a/android/library/src/main/jni/libs/nativehelper/include/nativehelper/jni.h b/android/library/src/main/jni/libs/nativehelper/include/nativehelper/jni.h deleted file mode 100644 index 1c2fb0c..0000000 --- a/android/library/src/main/jni/libs/nativehelper/include/nativehelper/jni.h +++ /dev/null @@ -1,1141 +0,0 @@ -/* - * Copyright (C) 2006 The Android Open Source Project - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -/* - * JNI specification, as defined by Sun: - * http://java.sun.com/javase/6/docs/technotes/guides/jni/spec/jniTOC.html - * - * Everything here is expected to be VM-neutral. - */ - -#ifndef JNI_H_ -#define JNI_H_ - -#include -#include - -/* Primitive types that match up with Java equivalents. */ -typedef uint8_t jboolean; /* unsigned 8 bits */ -typedef int8_t jbyte; /* signed 8 bits */ -typedef uint16_t jchar; /* unsigned 16 bits */ -typedef int16_t jshort; /* signed 16 bits */ -typedef int32_t jint; /* signed 32 bits */ -typedef int64_t jlong; /* signed 64 bits */ -typedef float jfloat; /* 32-bit IEEE 754 */ -typedef double jdouble; /* 64-bit IEEE 754 */ - -/* "cardinal indices and sizes" */ -typedef jint jsize; - -#ifdef __cplusplus -/* - * Reference types, in C++ - */ -class _jobject {}; -class _jclass : public _jobject {}; -class _jstring : public _jobject {}; -class _jarray : public _jobject {}; -class _jobjectArray : public _jarray {}; -class _jbooleanArray : public _jarray {}; -class _jbyteArray : public _jarray {}; -class _jcharArray : public _jarray {}; -class _jshortArray : public _jarray {}; -class _jintArray : public _jarray {}; -class _jlongArray : public _jarray {}; -class _jfloatArray : public _jarray {}; -class _jdoubleArray : public _jarray {}; -class _jthrowable : public _jobject {}; - -typedef _jobject* jobject; -typedef _jclass* jclass; -typedef _jstring* jstring; -typedef _jarray* jarray; -typedef _jobjectArray* jobjectArray; -typedef _jbooleanArray* jbooleanArray; -typedef _jbyteArray* jbyteArray; -typedef _jcharArray* jcharArray; -typedef _jshortArray* jshortArray; -typedef _jintArray* jintArray; -typedef _jlongArray* jlongArray; -typedef _jfloatArray* jfloatArray; -typedef _jdoubleArray* jdoubleArray; -typedef _jthrowable* jthrowable; -typedef _jobject* jweak; - - -#else /* not __cplusplus */ - -/* - * Reference types, in C. - */ -typedef void* jobject; -typedef jobject jclass; -typedef jobject jstring; -typedef jobject jarray; -typedef jarray jobjectArray; -typedef jarray jbooleanArray; -typedef jarray jbyteArray; -typedef jarray jcharArray; -typedef jarray jshortArray; -typedef jarray jintArray; -typedef jarray jlongArray; -typedef jarray jfloatArray; -typedef jarray jdoubleArray; -typedef jobject jthrowable; -typedef jobject jweak; - -#endif /* not __cplusplus */ - -struct _jfieldID; /* opaque structure */ -typedef struct _jfieldID* jfieldID; /* field IDs */ - -struct _jmethodID; /* opaque structure */ -typedef struct _jmethodID* jmethodID; /* method IDs */ - -struct JNIInvokeInterface; - -typedef union jvalue { - jboolean z; - jbyte b; - jchar c; - jshort s; - jint i; - jlong j; - jfloat f; - jdouble d; - jobject l; -} jvalue; - -typedef enum jobjectRefType { - JNIInvalidRefType = 0, - JNILocalRefType = 1, - JNIGlobalRefType = 2, - JNIWeakGlobalRefType = 3 -} jobjectRefType; - -typedef struct { - const char* name; - const char* signature; - void* fnPtr; -} JNINativeMethod; - -struct _JNIEnv; -struct _JavaVM; -typedef const struct JNINativeInterface* C_JNIEnv; - -#if defined(__cplusplus) -typedef _JNIEnv JNIEnv; -typedef _JavaVM JavaVM; -#else -typedef const struct JNINativeInterface* JNIEnv; -typedef const struct JNIInvokeInterface* JavaVM; -#endif - -/* - * Table of interface function pointers. - */ -struct JNINativeInterface { - void* reserved0; - void* reserved1; - void* reserved2; - void* reserved3; - - jint (*GetVersion)(JNIEnv *); - - jclass (*DefineClass)(JNIEnv*, const char*, jobject, const jbyte*, - jsize); - jclass (*FindClass)(JNIEnv*, const char*); - - jmethodID (*FromReflectedMethod)(JNIEnv*, jobject); - jfieldID (*FromReflectedField)(JNIEnv*, jobject); - /* spec doesn't show jboolean parameter */ - jobject (*ToReflectedMethod)(JNIEnv*, jclass, jmethodID, jboolean); - - jclass (*GetSuperclass)(JNIEnv*, jclass); - jboolean (*IsAssignableFrom)(JNIEnv*, jclass, jclass); - - /* spec doesn't show jboolean parameter */ - jobject (*ToReflectedField)(JNIEnv*, jclass, jfieldID, jboolean); - - jint (*Throw)(JNIEnv*, jthrowable); - jint (*ThrowNew)(JNIEnv *, jclass, const char *); - jthrowable (*ExceptionOccurred)(JNIEnv*); - void (*ExceptionDescribe)(JNIEnv*); - void (*ExceptionClear)(JNIEnv*); - void (*FatalError)(JNIEnv*, const char*); - - jint (*PushLocalFrame)(JNIEnv*, jint); - jobject (*PopLocalFrame)(JNIEnv*, jobject); - - jobject (*NewGlobalRef)(JNIEnv*, jobject); - void (*DeleteGlobalRef)(JNIEnv*, jobject); - void (*DeleteLocalRef)(JNIEnv*, jobject); - jboolean (*IsSameObject)(JNIEnv*, jobject, jobject); - - jobject (*NewLocalRef)(JNIEnv*, jobject); - jint (*EnsureLocalCapacity)(JNIEnv*, jint); - - jobject (*AllocObject)(JNIEnv*, jclass); - jobject (*NewObject)(JNIEnv*, jclass, jmethodID, ...); - jobject (*NewObjectV)(JNIEnv*, jclass, jmethodID, va_list); - jobject (*NewObjectA)(JNIEnv*, jclass, jmethodID, jvalue*); - - jclass (*GetObjectClass)(JNIEnv*, jobject); - jboolean (*IsInstanceOf)(JNIEnv*, jobject, jclass); - jmethodID (*GetMethodID)(JNIEnv*, jclass, const char*, const char*); - - jobject (*CallObjectMethod)(JNIEnv*, jobject, jmethodID, ...); - jobject (*CallObjectMethodV)(JNIEnv*, jobject, jmethodID, va_list); - jobject (*CallObjectMethodA)(JNIEnv*, jobject, jmethodID, jvalue*); - jboolean (*CallBooleanMethod)(JNIEnv*, jobject, jmethodID, ...); - jboolean (*CallBooleanMethodV)(JNIEnv*, jobject, jmethodID, va_list); - jboolean (*CallBooleanMethodA)(JNIEnv*, jobject, jmethodID, jvalue*); - jbyte (*CallByteMethod)(JNIEnv*, jobject, jmethodID, ...); - jbyte (*CallByteMethodV)(JNIEnv*, jobject, jmethodID, va_list); - jbyte (*CallByteMethodA)(JNIEnv*, jobject, jmethodID, jvalue*); - jchar (*CallCharMethod)(JNIEnv*, jobject, jmethodID, ...); - jchar (*CallCharMethodV)(JNIEnv*, jobject, jmethodID, va_list); - jchar (*CallCharMethodA)(JNIEnv*, jobject, jmethodID, jvalue*); - jshort (*CallShortMethod)(JNIEnv*, jobject, jmethodID, ...); - jshort (*CallShortMethodV)(JNIEnv*, jobject, jmethodID, va_list); - jshort (*CallShortMethodA)(JNIEnv*, jobject, jmethodID, jvalue*); - jint (*CallIntMethod)(JNIEnv*, jobject, jmethodID, ...); - jint (*CallIntMethodV)(JNIEnv*, jobject, jmethodID, va_list); - jint (*CallIntMethodA)(JNIEnv*, jobject, jmethodID, jvalue*); - jlong (*CallLongMethod)(JNIEnv*, jobject, jmethodID, ...); - jlong (*CallLongMethodV)(JNIEnv*, jobject, jmethodID, va_list); - jlong (*CallLongMethodA)(JNIEnv*, jobject, jmethodID, jvalue*); - jfloat (*CallFloatMethod)(JNIEnv*, jobject, jmethodID, ...); - jfloat (*CallFloatMethodV)(JNIEnv*, jobject, jmethodID, va_list); - jfloat (*CallFloatMethodA)(JNIEnv*, jobject, jmethodID, jvalue*); - jdouble (*CallDoubleMethod)(JNIEnv*, jobject, jmethodID, ...); - jdouble (*CallDoubleMethodV)(JNIEnv*, jobject, jmethodID, va_list); - jdouble (*CallDoubleMethodA)(JNIEnv*, jobject, jmethodID, jvalue*); - void (*CallVoidMethod)(JNIEnv*, jobject, jmethodID, ...); - void (*CallVoidMethodV)(JNIEnv*, jobject, jmethodID, va_list); - void (*CallVoidMethodA)(JNIEnv*, jobject, jmethodID, jvalue*); - - jobject (*CallNonvirtualObjectMethod)(JNIEnv*, jobject, jclass, - jmethodID, ...); - jobject (*CallNonvirtualObjectMethodV)(JNIEnv*, jobject, jclass, - jmethodID, va_list); - jobject (*CallNonvirtualObjectMethodA)(JNIEnv*, jobject, jclass, - jmethodID, jvalue*); - jboolean (*CallNonvirtualBooleanMethod)(JNIEnv*, jobject, jclass, - jmethodID, ...); - jboolean (*CallNonvirtualBooleanMethodV)(JNIEnv*, jobject, jclass, - jmethodID, va_list); - jboolean (*CallNonvirtualBooleanMethodA)(JNIEnv*, jobject, jclass, - jmethodID, jvalue*); - jbyte (*CallNonvirtualByteMethod)(JNIEnv*, jobject, jclass, - jmethodID, ...); - jbyte (*CallNonvirtualByteMethodV)(JNIEnv*, jobject, jclass, - jmethodID, va_list); - jbyte (*CallNonvirtualByteMethodA)(JNIEnv*, jobject, jclass, - jmethodID, jvalue*); - jchar (*CallNonvirtualCharMethod)(JNIEnv*, jobject, jclass, - jmethodID, ...); - jchar (*CallNonvirtualCharMethodV)(JNIEnv*, jobject, jclass, - jmethodID, va_list); - jchar (*CallNonvirtualCharMethodA)(JNIEnv*, jobject, jclass, - jmethodID, jvalue*); - jshort (*CallNonvirtualShortMethod)(JNIEnv*, jobject, jclass, - jmethodID, ...); - jshort (*CallNonvirtualShortMethodV)(JNIEnv*, jobject, jclass, - jmethodID, va_list); - jshort (*CallNonvirtualShortMethodA)(JNIEnv*, jobject, jclass, - jmethodID, jvalue*); - jint (*CallNonvirtualIntMethod)(JNIEnv*, jobject, jclass, - jmethodID, ...); - jint (*CallNonvirtualIntMethodV)(JNIEnv*, jobject, jclass, - jmethodID, va_list); - jint (*CallNonvirtualIntMethodA)(JNIEnv*, jobject, jclass, - jmethodID, jvalue*); - jlong (*CallNonvirtualLongMethod)(JNIEnv*, jobject, jclass, - jmethodID, ...); - jlong (*CallNonvirtualLongMethodV)(JNIEnv*, jobject, jclass, - jmethodID, va_list); - jlong (*CallNonvirtualLongMethodA)(JNIEnv*, jobject, jclass, - jmethodID, jvalue*); - jfloat (*CallNonvirtualFloatMethod)(JNIEnv*, jobject, jclass, - jmethodID, ...); - jfloat (*CallNonvirtualFloatMethodV)(JNIEnv*, jobject, jclass, - jmethodID, va_list); - jfloat (*CallNonvirtualFloatMethodA)(JNIEnv*, jobject, jclass, - jmethodID, jvalue*); - jdouble (*CallNonvirtualDoubleMethod)(JNIEnv*, jobject, jclass, - jmethodID, ...); - jdouble (*CallNonvirtualDoubleMethodV)(JNIEnv*, jobject, jclass, - jmethodID, va_list); - jdouble (*CallNonvirtualDoubleMethodA)(JNIEnv*, jobject, jclass, - jmethodID, jvalue*); - void (*CallNonvirtualVoidMethod)(JNIEnv*, jobject, jclass, - jmethodID, ...); - void (*CallNonvirtualVoidMethodV)(JNIEnv*, jobject, jclass, - jmethodID, va_list); - void (*CallNonvirtualVoidMethodA)(JNIEnv*, jobject, jclass, - jmethodID, jvalue*); - - jfieldID (*GetFieldID)(JNIEnv*, jclass, const char*, const char*); - - jobject (*GetObjectField)(JNIEnv*, jobject, jfieldID); - jboolean (*GetBooleanField)(JNIEnv*, jobject, jfieldID); - jbyte (*GetByteField)(JNIEnv*, jobject, jfieldID); - jchar (*GetCharField)(JNIEnv*, jobject, jfieldID); - jshort (*GetShortField)(JNIEnv*, jobject, jfieldID); - jint (*GetIntField)(JNIEnv*, jobject, jfieldID); - jlong (*GetLongField)(JNIEnv*, jobject, jfieldID); - jfloat (*GetFloatField)(JNIEnv*, jobject, jfieldID); - jdouble (*GetDoubleField)(JNIEnv*, jobject, jfieldID); - - void (*SetObjectField)(JNIEnv*, jobject, jfieldID, jobject); - void (*SetBooleanField)(JNIEnv*, jobject, jfieldID, jboolean); - void (*SetByteField)(JNIEnv*, jobject, jfieldID, jbyte); - void (*SetCharField)(JNIEnv*, jobject, jfieldID, jchar); - void (*SetShortField)(JNIEnv*, jobject, jfieldID, jshort); - void (*SetIntField)(JNIEnv*, jobject, jfieldID, jint); - void (*SetLongField)(JNIEnv*, jobject, jfieldID, jlong); - void (*SetFloatField)(JNIEnv*, jobject, jfieldID, jfloat); - void (*SetDoubleField)(JNIEnv*, jobject, jfieldID, jdouble); - - jmethodID (*GetStaticMethodID)(JNIEnv*, jclass, const char*, const char*); - - jobject (*CallStaticObjectMethod)(JNIEnv*, jclass, jmethodID, ...); - jobject (*CallStaticObjectMethodV)(JNIEnv*, jclass, jmethodID, va_list); - jobject (*CallStaticObjectMethodA)(JNIEnv*, jclass, jmethodID, jvalue*); - jboolean (*CallStaticBooleanMethod)(JNIEnv*, jclass, jmethodID, ...); - jboolean (*CallStaticBooleanMethodV)(JNIEnv*, jclass, jmethodID, - va_list); - jboolean (*CallStaticBooleanMethodA)(JNIEnv*, jclass, jmethodID, - jvalue*); - jbyte (*CallStaticByteMethod)(JNIEnv*, jclass, jmethodID, ...); - jbyte (*CallStaticByteMethodV)(JNIEnv*, jclass, jmethodID, va_list); - jbyte (*CallStaticByteMethodA)(JNIEnv*, jclass, jmethodID, jvalue*); - jchar (*CallStaticCharMethod)(JNIEnv*, jclass, jmethodID, ...); - jchar (*CallStaticCharMethodV)(JNIEnv*, jclass, jmethodID, va_list); - jchar (*CallStaticCharMethodA)(JNIEnv*, jclass, jmethodID, jvalue*); - jshort (*CallStaticShortMethod)(JNIEnv*, jclass, jmethodID, ...); - jshort (*CallStaticShortMethodV)(JNIEnv*, jclass, jmethodID, va_list); - jshort (*CallStaticShortMethodA)(JNIEnv*, jclass, jmethodID, jvalue*); - jint (*CallStaticIntMethod)(JNIEnv*, jclass, jmethodID, ...); - jint (*CallStaticIntMethodV)(JNIEnv*, jclass, jmethodID, va_list); - jint (*CallStaticIntMethodA)(JNIEnv*, jclass, jmethodID, jvalue*); - jlong (*CallStaticLongMethod)(JNIEnv*, jclass, jmethodID, ...); - jlong (*CallStaticLongMethodV)(JNIEnv*, jclass, jmethodID, va_list); - jlong (*CallStaticLongMethodA)(JNIEnv*, jclass, jmethodID, jvalue*); - jfloat (*CallStaticFloatMethod)(JNIEnv*, jclass, jmethodID, ...); - jfloat (*CallStaticFloatMethodV)(JNIEnv*, jclass, jmethodID, va_list); - jfloat (*CallStaticFloatMethodA)(JNIEnv*, jclass, jmethodID, jvalue*); - jdouble (*CallStaticDoubleMethod)(JNIEnv*, jclass, jmethodID, ...); - jdouble (*CallStaticDoubleMethodV)(JNIEnv*, jclass, jmethodID, va_list); - jdouble (*CallStaticDoubleMethodA)(JNIEnv*, jclass, jmethodID, jvalue*); - void (*CallStaticVoidMethod)(JNIEnv*, jclass, jmethodID, ...); - void (*CallStaticVoidMethodV)(JNIEnv*, jclass, jmethodID, va_list); - void (*CallStaticVoidMethodA)(JNIEnv*, jclass, jmethodID, jvalue*); - - jfieldID (*GetStaticFieldID)(JNIEnv*, jclass, const char*, - const char*); - - jobject (*GetStaticObjectField)(JNIEnv*, jclass, jfieldID); - jboolean (*GetStaticBooleanField)(JNIEnv*, jclass, jfieldID); - jbyte (*GetStaticByteField)(JNIEnv*, jclass, jfieldID); - jchar (*GetStaticCharField)(JNIEnv*, jclass, jfieldID); - jshort (*GetStaticShortField)(JNIEnv*, jclass, jfieldID); - jint (*GetStaticIntField)(JNIEnv*, jclass, jfieldID); - jlong (*GetStaticLongField)(JNIEnv*, jclass, jfieldID); - jfloat (*GetStaticFloatField)(JNIEnv*, jclass, jfieldID); - jdouble (*GetStaticDoubleField)(JNIEnv*, jclass, jfieldID); - - void (*SetStaticObjectField)(JNIEnv*, jclass, jfieldID, jobject); - void (*SetStaticBooleanField)(JNIEnv*, jclass, jfieldID, jboolean); - void (*SetStaticByteField)(JNIEnv*, jclass, jfieldID, jbyte); - void (*SetStaticCharField)(JNIEnv*, jclass, jfieldID, jchar); - void (*SetStaticShortField)(JNIEnv*, jclass, jfieldID, jshort); - void (*SetStaticIntField)(JNIEnv*, jclass, jfieldID, jint); - void (*SetStaticLongField)(JNIEnv*, jclass, jfieldID, jlong); - void (*SetStaticFloatField)(JNIEnv*, jclass, jfieldID, jfloat); - void (*SetStaticDoubleField)(JNIEnv*, jclass, jfieldID, jdouble); - - jstring (*NewString)(JNIEnv*, const jchar*, jsize); - jsize (*GetStringLength)(JNIEnv*, jstring); - const jchar* (*GetStringChars)(JNIEnv*, jstring, jboolean*); - void (*ReleaseStringChars)(JNIEnv*, jstring, const jchar*); - jstring (*NewStringUTF)(JNIEnv*, const char*); - jsize (*GetStringUTFLength)(JNIEnv*, jstring); - /* JNI spec says this returns const jbyte*, but that's inconsistent */ - const char* (*GetStringUTFChars)(JNIEnv*, jstring, jboolean*); - void (*ReleaseStringUTFChars)(JNIEnv*, jstring, const char*); - jsize (*GetArrayLength)(JNIEnv*, jarray); - jobjectArray (*NewObjectArray)(JNIEnv*, jsize, jclass, jobject); - jobject (*GetObjectArrayElement)(JNIEnv*, jobjectArray, jsize); - void (*SetObjectArrayElement)(JNIEnv*, jobjectArray, jsize, jobject); - - jbooleanArray (*NewBooleanArray)(JNIEnv*, jsize); - jbyteArray (*NewByteArray)(JNIEnv*, jsize); - jcharArray (*NewCharArray)(JNIEnv*, jsize); - jshortArray (*NewShortArray)(JNIEnv*, jsize); - jintArray (*NewIntArray)(JNIEnv*, jsize); - jlongArray (*NewLongArray)(JNIEnv*, jsize); - jfloatArray (*NewFloatArray)(JNIEnv*, jsize); - jdoubleArray (*NewDoubleArray)(JNIEnv*, jsize); - - jboolean* (*GetBooleanArrayElements)(JNIEnv*, jbooleanArray, jboolean*); - jbyte* (*GetByteArrayElements)(JNIEnv*, jbyteArray, jboolean*); - jchar* (*GetCharArrayElements)(JNIEnv*, jcharArray, jboolean*); - jshort* (*GetShortArrayElements)(JNIEnv*, jshortArray, jboolean*); - jint* (*GetIntArrayElements)(JNIEnv*, jintArray, jboolean*); - jlong* (*GetLongArrayElements)(JNIEnv*, jlongArray, jboolean*); - jfloat* (*GetFloatArrayElements)(JNIEnv*, jfloatArray, jboolean*); - jdouble* (*GetDoubleArrayElements)(JNIEnv*, jdoubleArray, jboolean*); - - void (*ReleaseBooleanArrayElements)(JNIEnv*, jbooleanArray, - jboolean*, jint); - void (*ReleaseByteArrayElements)(JNIEnv*, jbyteArray, - jbyte*, jint); - void (*ReleaseCharArrayElements)(JNIEnv*, jcharArray, - jchar*, jint); - void (*ReleaseShortArrayElements)(JNIEnv*, jshortArray, - jshort*, jint); - void (*ReleaseIntArrayElements)(JNIEnv*, jintArray, - jint*, jint); - void (*ReleaseLongArrayElements)(JNIEnv*, jlongArray, - jlong*, jint); - void (*ReleaseFloatArrayElements)(JNIEnv*, jfloatArray, - jfloat*, jint); - void (*ReleaseDoubleArrayElements)(JNIEnv*, jdoubleArray, - jdouble*, jint); - - void (*GetBooleanArrayRegion)(JNIEnv*, jbooleanArray, - jsize, jsize, jboolean*); - void (*GetByteArrayRegion)(JNIEnv*, jbyteArray, - jsize, jsize, jbyte*); - void (*GetCharArrayRegion)(JNIEnv*, jcharArray, - jsize, jsize, jchar*); - void (*GetShortArrayRegion)(JNIEnv*, jshortArray, - jsize, jsize, jshort*); - void (*GetIntArrayRegion)(JNIEnv*, jintArray, - jsize, jsize, jint*); - void (*GetLongArrayRegion)(JNIEnv*, jlongArray, - jsize, jsize, jlong*); - void (*GetFloatArrayRegion)(JNIEnv*, jfloatArray, - jsize, jsize, jfloat*); - void (*GetDoubleArrayRegion)(JNIEnv*, jdoubleArray, - jsize, jsize, jdouble*); - - /* spec shows these without const; some jni.h do, some don't */ - void (*SetBooleanArrayRegion)(JNIEnv*, jbooleanArray, - jsize, jsize, const jboolean*); - void (*SetByteArrayRegion)(JNIEnv*, jbyteArray, - jsize, jsize, const jbyte*); - void (*SetCharArrayRegion)(JNIEnv*, jcharArray, - jsize, jsize, const jchar*); - void (*SetShortArrayRegion)(JNIEnv*, jshortArray, - jsize, jsize, const jshort*); - void (*SetIntArrayRegion)(JNIEnv*, jintArray, - jsize, jsize, const jint*); - void (*SetLongArrayRegion)(JNIEnv*, jlongArray, - jsize, jsize, const jlong*); - void (*SetFloatArrayRegion)(JNIEnv*, jfloatArray, - jsize, jsize, const jfloat*); - void (*SetDoubleArrayRegion)(JNIEnv*, jdoubleArray, - jsize, jsize, const jdouble*); - - jint (*RegisterNatives)(JNIEnv*, jclass, const JNINativeMethod*, - jint); - jint (*UnregisterNatives)(JNIEnv*, jclass); - jint (*MonitorEnter)(JNIEnv*, jobject); - jint (*MonitorExit)(JNIEnv*, jobject); - jint (*GetJavaVM)(JNIEnv*, JavaVM**); - - void (*GetStringRegion)(JNIEnv*, jstring, jsize, jsize, jchar*); - void (*GetStringUTFRegion)(JNIEnv*, jstring, jsize, jsize, char*); - - void* (*GetPrimitiveArrayCritical)(JNIEnv*, jarray, jboolean*); - void (*ReleasePrimitiveArrayCritical)(JNIEnv*, jarray, void*, jint); - - const jchar* (*GetStringCritical)(JNIEnv*, jstring, jboolean*); - void (*ReleaseStringCritical)(JNIEnv*, jstring, const jchar*); - - jweak (*NewWeakGlobalRef)(JNIEnv*, jobject); - void (*DeleteWeakGlobalRef)(JNIEnv*, jweak); - - jboolean (*ExceptionCheck)(JNIEnv*); - - jobject (*NewDirectByteBuffer)(JNIEnv*, void*, jlong); - void* (*GetDirectBufferAddress)(JNIEnv*, jobject); - jlong (*GetDirectBufferCapacity)(JNIEnv*, jobject); - - /* added in JNI 1.6 */ - jobjectRefType (*GetObjectRefType)(JNIEnv*, jobject); -}; - -/* - * C++ object wrapper. - * - * This is usually overlaid on a C struct whose first element is a - * JNINativeInterface*. We rely somewhat on compiler behavior. - */ -struct _JNIEnv { - /* do not rename this; it does not seem to be entirely opaque */ - const struct JNINativeInterface* functions; - -#if defined(__cplusplus) - - jint GetVersion() - { return functions->GetVersion(this); } - - jclass DefineClass(const char *name, jobject loader, const jbyte* buf, - jsize bufLen) - { return functions->DefineClass(this, name, loader, buf, bufLen); } - - jclass FindClass(const char* name) - { return functions->FindClass(this, name); } - - jmethodID FromReflectedMethod(jobject method) - { return functions->FromReflectedMethod(this, method); } - - jfieldID FromReflectedField(jobject field) - { return functions->FromReflectedField(this, field); } - - jobject ToReflectedMethod(jclass cls, jmethodID methodID, jboolean isStatic) - { return functions->ToReflectedMethod(this, cls, methodID, isStatic); } - - jclass GetSuperclass(jclass clazz) - { return functions->GetSuperclass(this, clazz); } - - jboolean IsAssignableFrom(jclass clazz1, jclass clazz2) - { return functions->IsAssignableFrom(this, clazz1, clazz2); } - - jobject ToReflectedField(jclass cls, jfieldID fieldID, jboolean isStatic) - { return functions->ToReflectedField(this, cls, fieldID, isStatic); } - - jint Throw(jthrowable obj) - { return functions->Throw(this, obj); } - - jint ThrowNew(jclass clazz, const char* message) - { return functions->ThrowNew(this, clazz, message); } - - jthrowable ExceptionOccurred() - { return functions->ExceptionOccurred(this); } - - void ExceptionDescribe() - { functions->ExceptionDescribe(this); } - - void ExceptionClear() - { functions->ExceptionClear(this); } - - void FatalError(const char* msg) - { functions->FatalError(this, msg); } - - jint PushLocalFrame(jint capacity) - { return functions->PushLocalFrame(this, capacity); } - - jobject PopLocalFrame(jobject result) - { return functions->PopLocalFrame(this, result); } - - jobject NewGlobalRef(jobject obj) - { return functions->NewGlobalRef(this, obj); } - - void DeleteGlobalRef(jobject globalRef) - { functions->DeleteGlobalRef(this, globalRef); } - - void DeleteLocalRef(jobject localRef) - { functions->DeleteLocalRef(this, localRef); } - - jboolean IsSameObject(jobject ref1, jobject ref2) - { return functions->IsSameObject(this, ref1, ref2); } - - jobject NewLocalRef(jobject ref) - { return functions->NewLocalRef(this, ref); } - - jint EnsureLocalCapacity(jint capacity) - { return functions->EnsureLocalCapacity(this, capacity); } - - jobject AllocObject(jclass clazz) - { return functions->AllocObject(this, clazz); } - - jobject NewObject(jclass clazz, jmethodID methodID, ...) - { - va_list args; - va_start(args, methodID); - jobject result = functions->NewObjectV(this, clazz, methodID, args); - va_end(args); - return result; - } - - jobject NewObjectV(jclass clazz, jmethodID methodID, va_list args) - { return functions->NewObjectV(this, clazz, methodID, args); } - - jobject NewObjectA(jclass clazz, jmethodID methodID, jvalue* args) - { return functions->NewObjectA(this, clazz, methodID, args); } - - jclass GetObjectClass(jobject obj) - { return functions->GetObjectClass(this, obj); } - - jboolean IsInstanceOf(jobject obj, jclass clazz) - { return functions->IsInstanceOf(this, obj, clazz); } - - jmethodID GetMethodID(jclass clazz, const char* name, const char* sig) - { return functions->GetMethodID(this, clazz, name, sig); } - -#define CALL_TYPE_METHOD(_jtype, _jname) \ - _jtype Call##_jname##Method(jobject obj, jmethodID methodID, ...) \ - { \ - _jtype result; \ - va_list args; \ - va_start(args, methodID); \ - result = functions->Call##_jname##MethodV(this, obj, methodID, \ - args); \ - va_end(args); \ - return result; \ - } -#define CALL_TYPE_METHODV(_jtype, _jname) \ - _jtype Call##_jname##MethodV(jobject obj, jmethodID methodID, \ - va_list args) \ - { return functions->Call##_jname##MethodV(this, obj, methodID, args); } -#define CALL_TYPE_METHODA(_jtype, _jname) \ - _jtype Call##_jname##MethodA(jobject obj, jmethodID methodID, \ - jvalue* args) \ - { return functions->Call##_jname##MethodA(this, obj, methodID, args); } - -#define CALL_TYPE(_jtype, _jname) \ - CALL_TYPE_METHOD(_jtype, _jname) \ - CALL_TYPE_METHODV(_jtype, _jname) \ - CALL_TYPE_METHODA(_jtype, _jname) - - CALL_TYPE(jobject, Object) - CALL_TYPE(jboolean, Boolean) - CALL_TYPE(jbyte, Byte) - CALL_TYPE(jchar, Char) - CALL_TYPE(jshort, Short) - CALL_TYPE(jint, Int) - CALL_TYPE(jlong, Long) - CALL_TYPE(jfloat, Float) - CALL_TYPE(jdouble, Double) - - void CallVoidMethod(jobject obj, jmethodID methodID, ...) - { - va_list args; - va_start(args, methodID); - functions->CallVoidMethodV(this, obj, methodID, args); - va_end(args); - } - void CallVoidMethodV(jobject obj, jmethodID methodID, va_list args) - { functions->CallVoidMethodV(this, obj, methodID, args); } - void CallVoidMethodA(jobject obj, jmethodID methodID, jvalue* args) - { functions->CallVoidMethodA(this, obj, methodID, args); } - -#define CALL_NONVIRT_TYPE_METHOD(_jtype, _jname) \ - _jtype CallNonvirtual##_jname##Method(jobject obj, jclass clazz, \ - jmethodID methodID, ...) \ - { \ - _jtype result; \ - va_list args; \ - va_start(args, methodID); \ - result = functions->CallNonvirtual##_jname##MethodV(this, obj, \ - clazz, methodID, args); \ - va_end(args); \ - return result; \ - } -#define CALL_NONVIRT_TYPE_METHODV(_jtype, _jname) \ - _jtype CallNonvirtual##_jname##MethodV(jobject obj, jclass clazz, \ - jmethodID methodID, va_list args) \ - { return functions->CallNonvirtual##_jname##MethodV(this, obj, clazz, \ - methodID, args); } -#define CALL_NONVIRT_TYPE_METHODA(_jtype, _jname) \ - _jtype CallNonvirtual##_jname##MethodA(jobject obj, jclass clazz, \ - jmethodID methodID, jvalue* args) \ - { return functions->CallNonvirtual##_jname##MethodA(this, obj, clazz, \ - methodID, args); } - -#define CALL_NONVIRT_TYPE(_jtype, _jname) \ - CALL_NONVIRT_TYPE_METHOD(_jtype, _jname) \ - CALL_NONVIRT_TYPE_METHODV(_jtype, _jname) \ - CALL_NONVIRT_TYPE_METHODA(_jtype, _jname) - - CALL_NONVIRT_TYPE(jobject, Object) - CALL_NONVIRT_TYPE(jboolean, Boolean) - CALL_NONVIRT_TYPE(jbyte, Byte) - CALL_NONVIRT_TYPE(jchar, Char) - CALL_NONVIRT_TYPE(jshort, Short) - CALL_NONVIRT_TYPE(jint, Int) - CALL_NONVIRT_TYPE(jlong, Long) - CALL_NONVIRT_TYPE(jfloat, Float) - CALL_NONVIRT_TYPE(jdouble, Double) - - void CallNonvirtualVoidMethod(jobject obj, jclass clazz, - jmethodID methodID, ...) - { - va_list args; - va_start(args, methodID); - functions->CallNonvirtualVoidMethodV(this, obj, clazz, methodID, args); - va_end(args); - } - void CallNonvirtualVoidMethodV(jobject obj, jclass clazz, - jmethodID methodID, va_list args) - { functions->CallNonvirtualVoidMethodV(this, obj, clazz, methodID, args); } - void CallNonvirtualVoidMethodA(jobject obj, jclass clazz, - jmethodID methodID, jvalue* args) - { functions->CallNonvirtualVoidMethodA(this, obj, clazz, methodID, args); } - - jfieldID GetFieldID(jclass clazz, const char* name, const char* sig) - { return functions->GetFieldID(this, clazz, name, sig); } - - jobject GetObjectField(jobject obj, jfieldID fieldID) - { return functions->GetObjectField(this, obj, fieldID); } - jboolean GetBooleanField(jobject obj, jfieldID fieldID) - { return functions->GetBooleanField(this, obj, fieldID); } - jbyte GetByteField(jobject obj, jfieldID fieldID) - { return functions->GetByteField(this, obj, fieldID); } - jchar GetCharField(jobject obj, jfieldID fieldID) - { return functions->GetCharField(this, obj, fieldID); } - jshort GetShortField(jobject obj, jfieldID fieldID) - { return functions->GetShortField(this, obj, fieldID); } - jint GetIntField(jobject obj, jfieldID fieldID) - { return functions->GetIntField(this, obj, fieldID); } - jlong GetLongField(jobject obj, jfieldID fieldID) - { return functions->GetLongField(this, obj, fieldID); } - jfloat GetFloatField(jobject obj, jfieldID fieldID) - { return functions->GetFloatField(this, obj, fieldID); } - jdouble GetDoubleField(jobject obj, jfieldID fieldID) - { return functions->GetDoubleField(this, obj, fieldID); } - - void SetObjectField(jobject obj, jfieldID fieldID, jobject value) - { functions->SetObjectField(this, obj, fieldID, value); } - void SetBooleanField(jobject obj, jfieldID fieldID, jboolean value) - { functions->SetBooleanField(this, obj, fieldID, value); } - void SetByteField(jobject obj, jfieldID fieldID, jbyte value) - { functions->SetByteField(this, obj, fieldID, value); } - void SetCharField(jobject obj, jfieldID fieldID, jchar value) - { functions->SetCharField(this, obj, fieldID, value); } - void SetShortField(jobject obj, jfieldID fieldID, jshort value) - { functions->SetShortField(this, obj, fieldID, value); } - void SetIntField(jobject obj, jfieldID fieldID, jint value) - { functions->SetIntField(this, obj, fieldID, value); } - void SetLongField(jobject obj, jfieldID fieldID, jlong value) - { functions->SetLongField(this, obj, fieldID, value); } - void SetFloatField(jobject obj, jfieldID fieldID, jfloat value) - { functions->SetFloatField(this, obj, fieldID, value); } - void SetDoubleField(jobject obj, jfieldID fieldID, jdouble value) - { functions->SetDoubleField(this, obj, fieldID, value); } - - jmethodID GetStaticMethodID(jclass clazz, const char* name, const char* sig) - { return functions->GetStaticMethodID(this, clazz, name, sig); } - -#define CALL_STATIC_TYPE_METHOD(_jtype, _jname) \ - _jtype CallStatic##_jname##Method(jclass clazz, jmethodID methodID, \ - ...) \ - { \ - _jtype result; \ - va_list args; \ - va_start(args, methodID); \ - result = functions->CallStatic##_jname##MethodV(this, clazz, \ - methodID, args); \ - va_end(args); \ - return result; \ - } -#define CALL_STATIC_TYPE_METHODV(_jtype, _jname) \ - _jtype CallStatic##_jname##MethodV(jclass clazz, jmethodID methodID, \ - va_list args) \ - { return functions->CallStatic##_jname##MethodV(this, clazz, methodID, \ - args); } -#define CALL_STATIC_TYPE_METHODA(_jtype, _jname) \ - _jtype CallStatic##_jname##MethodA(jclass clazz, jmethodID methodID, \ - jvalue* args) \ - { return functions->CallStatic##_jname##MethodA(this, clazz, methodID, \ - args); } - -#define CALL_STATIC_TYPE(_jtype, _jname) \ - CALL_STATIC_TYPE_METHOD(_jtype, _jname) \ - CALL_STATIC_TYPE_METHODV(_jtype, _jname) \ - CALL_STATIC_TYPE_METHODA(_jtype, _jname) - - CALL_STATIC_TYPE(jobject, Object) - CALL_STATIC_TYPE(jboolean, Boolean) - CALL_STATIC_TYPE(jbyte, Byte) - CALL_STATIC_TYPE(jchar, Char) - CALL_STATIC_TYPE(jshort, Short) - CALL_STATIC_TYPE(jint, Int) - CALL_STATIC_TYPE(jlong, Long) - CALL_STATIC_TYPE(jfloat, Float) - CALL_STATIC_TYPE(jdouble, Double) - - void CallStaticVoidMethod(jclass clazz, jmethodID methodID, ...) - { - va_list args; - va_start(args, methodID); - functions->CallStaticVoidMethodV(this, clazz, methodID, args); - va_end(args); - } - void CallStaticVoidMethodV(jclass clazz, jmethodID methodID, va_list args) - { functions->CallStaticVoidMethodV(this, clazz, methodID, args); } - void CallStaticVoidMethodA(jclass clazz, jmethodID methodID, jvalue* args) - { functions->CallStaticVoidMethodA(this, clazz, methodID, args); } - - jfieldID GetStaticFieldID(jclass clazz, const char* name, const char* sig) - { return functions->GetStaticFieldID(this, clazz, name, sig); } - - jobject GetStaticObjectField(jclass clazz, jfieldID fieldID) - { return functions->GetStaticObjectField(this, clazz, fieldID); } - jboolean GetStaticBooleanField(jclass clazz, jfieldID fieldID) - { return functions->GetStaticBooleanField(this, clazz, fieldID); } - jbyte GetStaticByteField(jclass clazz, jfieldID fieldID) - { return functions->GetStaticByteField(this, clazz, fieldID); } - jchar GetStaticCharField(jclass clazz, jfieldID fieldID) - { return functions->GetStaticCharField(this, clazz, fieldID); } - jshort GetStaticShortField(jclass clazz, jfieldID fieldID) - { return functions->GetStaticShortField(this, clazz, fieldID); } - jint GetStaticIntField(jclass clazz, jfieldID fieldID) - { return functions->GetStaticIntField(this, clazz, fieldID); } - jlong GetStaticLongField(jclass clazz, jfieldID fieldID) - { return functions->GetStaticLongField(this, clazz, fieldID); } - jfloat GetStaticFloatField(jclass clazz, jfieldID fieldID) - { return functions->GetStaticFloatField(this, clazz, fieldID); } - jdouble GetStaticDoubleField(jclass clazz, jfieldID fieldID) - { return functions->GetStaticDoubleField(this, clazz, fieldID); } - - void SetStaticObjectField(jclass clazz, jfieldID fieldID, jobject value) - { functions->SetStaticObjectField(this, clazz, fieldID, value); } - void SetStaticBooleanField(jclass clazz, jfieldID fieldID, jboolean value) - { functions->SetStaticBooleanField(this, clazz, fieldID, value); } - void SetStaticByteField(jclass clazz, jfieldID fieldID, jbyte value) - { functions->SetStaticByteField(this, clazz, fieldID, value); } - void SetStaticCharField(jclass clazz, jfieldID fieldID, jchar value) - { functions->SetStaticCharField(this, clazz, fieldID, value); } - void SetStaticShortField(jclass clazz, jfieldID fieldID, jshort value) - { functions->SetStaticShortField(this, clazz, fieldID, value); } - void SetStaticIntField(jclass clazz, jfieldID fieldID, jint value) - { functions->SetStaticIntField(this, clazz, fieldID, value); } - void SetStaticLongField(jclass clazz, jfieldID fieldID, jlong value) - { functions->SetStaticLongField(this, clazz, fieldID, value); } - void SetStaticFloatField(jclass clazz, jfieldID fieldID, jfloat value) - { functions->SetStaticFloatField(this, clazz, fieldID, value); } - void SetStaticDoubleField(jclass clazz, jfieldID fieldID, jdouble value) - { functions->SetStaticDoubleField(this, clazz, fieldID, value); } - - jstring NewString(const jchar* unicodeChars, jsize len) - { return functions->NewString(this, unicodeChars, len); } - - jsize GetStringLength(jstring string) - { return functions->GetStringLength(this, string); } - - const jchar* GetStringChars(jstring string, jboolean* isCopy) - { return functions->GetStringChars(this, string, isCopy); } - - void ReleaseStringChars(jstring string, const jchar* chars) - { functions->ReleaseStringChars(this, string, chars); } - - jstring NewStringUTF(const char* bytes) - { return functions->NewStringUTF(this, bytes); } - - jsize GetStringUTFLength(jstring string) - { return functions->GetStringUTFLength(this, string); } - - const char* GetStringUTFChars(jstring string, jboolean* isCopy) - { return functions->GetStringUTFChars(this, string, isCopy); } - - void ReleaseStringUTFChars(jstring string, const char* utf) - { functions->ReleaseStringUTFChars(this, string, utf); } - - jsize GetArrayLength(jarray array) - { return functions->GetArrayLength(this, array); } - - jobjectArray NewObjectArray(jsize length, jclass elementClass, - jobject initialElement) - { return functions->NewObjectArray(this, length, elementClass, - initialElement); } - - jobject GetObjectArrayElement(jobjectArray array, jsize index) - { return functions->GetObjectArrayElement(this, array, index); } - - void SetObjectArrayElement(jobjectArray array, jsize index, jobject value) - { functions->SetObjectArrayElement(this, array, index, value); } - - jbooleanArray NewBooleanArray(jsize length) - { return functions->NewBooleanArray(this, length); } - jbyteArray NewByteArray(jsize length) - { return functions->NewByteArray(this, length); } - jcharArray NewCharArray(jsize length) - { return functions->NewCharArray(this, length); } - jshortArray NewShortArray(jsize length) - { return functions->NewShortArray(this, length); } - jintArray NewIntArray(jsize length) - { return functions->NewIntArray(this, length); } - jlongArray NewLongArray(jsize length) - { return functions->NewLongArray(this, length); } - jfloatArray NewFloatArray(jsize length) - { return functions->NewFloatArray(this, length); } - jdoubleArray NewDoubleArray(jsize length) - { return functions->NewDoubleArray(this, length); } - - jboolean* GetBooleanArrayElements(jbooleanArray array, jboolean* isCopy) - { return functions->GetBooleanArrayElements(this, array, isCopy); } - jbyte* GetByteArrayElements(jbyteArray array, jboolean* isCopy) - { return functions->GetByteArrayElements(this, array, isCopy); } - jchar* GetCharArrayElements(jcharArray array, jboolean* isCopy) - { return functions->GetCharArrayElements(this, array, isCopy); } - jshort* GetShortArrayElements(jshortArray array, jboolean* isCopy) - { return functions->GetShortArrayElements(this, array, isCopy); } - jint* GetIntArrayElements(jintArray array, jboolean* isCopy) - { return functions->GetIntArrayElements(this, array, isCopy); } - jlong* GetLongArrayElements(jlongArray array, jboolean* isCopy) - { return functions->GetLongArrayElements(this, array, isCopy); } - jfloat* GetFloatArrayElements(jfloatArray array, jboolean* isCopy) - { return functions->GetFloatArrayElements(this, array, isCopy); } - jdouble* GetDoubleArrayElements(jdoubleArray array, jboolean* isCopy) - { return functions->GetDoubleArrayElements(this, array, isCopy); } - - void ReleaseBooleanArrayElements(jbooleanArray array, jboolean* elems, - jint mode) - { functions->ReleaseBooleanArrayElements(this, array, elems, mode); } - void ReleaseByteArrayElements(jbyteArray array, jbyte* elems, - jint mode) - { functions->ReleaseByteArrayElements(this, array, elems, mode); } - void ReleaseCharArrayElements(jcharArray array, jchar* elems, - jint mode) - { functions->ReleaseCharArrayElements(this, array, elems, mode); } - void ReleaseShortArrayElements(jshortArray array, jshort* elems, - jint mode) - { functions->ReleaseShortArrayElements(this, array, elems, mode); } - void ReleaseIntArrayElements(jintArray array, jint* elems, - jint mode) - { functions->ReleaseIntArrayElements(this, array, elems, mode); } - void ReleaseLongArrayElements(jlongArray array, jlong* elems, - jint mode) - { functions->ReleaseLongArrayElements(this, array, elems, mode); } - void ReleaseFloatArrayElements(jfloatArray array, jfloat* elems, - jint mode) - { functions->ReleaseFloatArrayElements(this, array, elems, mode); } - void ReleaseDoubleArrayElements(jdoubleArray array, jdouble* elems, - jint mode) - { functions->ReleaseDoubleArrayElements(this, array, elems, mode); } - - void GetBooleanArrayRegion(jbooleanArray array, jsize start, jsize len, - jboolean* buf) - { functions->GetBooleanArrayRegion(this, array, start, len, buf); } - void GetByteArrayRegion(jbyteArray array, jsize start, jsize len, - jbyte* buf) - { functions->GetByteArrayRegion(this, array, start, len, buf); } - void GetCharArrayRegion(jcharArray array, jsize start, jsize len, - jchar* buf) - { functions->GetCharArrayRegion(this, array, start, len, buf); } - void GetShortArrayRegion(jshortArray array, jsize start, jsize len, - jshort* buf) - { functions->GetShortArrayRegion(this, array, start, len, buf); } - void GetIntArrayRegion(jintArray array, jsize start, jsize len, - jint* buf) - { functions->GetIntArrayRegion(this, array, start, len, buf); } - void GetLongArrayRegion(jlongArray array, jsize start, jsize len, - jlong* buf) - { functions->GetLongArrayRegion(this, array, start, len, buf); } - void GetFloatArrayRegion(jfloatArray array, jsize start, jsize len, - jfloat* buf) - { functions->GetFloatArrayRegion(this, array, start, len, buf); } - void GetDoubleArrayRegion(jdoubleArray array, jsize start, jsize len, - jdouble* buf) - { functions->GetDoubleArrayRegion(this, array, start, len, buf); } - - void SetBooleanArrayRegion(jbooleanArray array, jsize start, jsize len, - const jboolean* buf) - { functions->SetBooleanArrayRegion(this, array, start, len, buf); } - void SetByteArrayRegion(jbyteArray array, jsize start, jsize len, - const jbyte* buf) - { functions->SetByteArrayRegion(this, array, start, len, buf); } - void SetCharArrayRegion(jcharArray array, jsize start, jsize len, - const jchar* buf) - { functions->SetCharArrayRegion(this, array, start, len, buf); } - void SetShortArrayRegion(jshortArray array, jsize start, jsize len, - const jshort* buf) - { functions->SetShortArrayRegion(this, array, start, len, buf); } - void SetIntArrayRegion(jintArray array, jsize start, jsize len, - const jint* buf) - { functions->SetIntArrayRegion(this, array, start, len, buf); } - void SetLongArrayRegion(jlongArray array, jsize start, jsize len, - const jlong* buf) - { functions->SetLongArrayRegion(this, array, start, len, buf); } - void SetFloatArrayRegion(jfloatArray array, jsize start, jsize len, - const jfloat* buf) - { functions->SetFloatArrayRegion(this, array, start, len, buf); } - void SetDoubleArrayRegion(jdoubleArray array, jsize start, jsize len, - const jdouble* buf) - { functions->SetDoubleArrayRegion(this, array, start, len, buf); } - - jint RegisterNatives(jclass clazz, const JNINativeMethod* methods, - jint nMethods) - { return functions->RegisterNatives(this, clazz, methods, nMethods); } - - jint UnregisterNatives(jclass clazz) - { return functions->UnregisterNatives(this, clazz); } - - jint MonitorEnter(jobject obj) - { return functions->MonitorEnter(this, obj); } - - jint MonitorExit(jobject obj) - { return functions->MonitorExit(this, obj); } - - jint GetJavaVM(JavaVM** vm) - { return functions->GetJavaVM(this, vm); } - - void GetStringRegion(jstring str, jsize start, jsize len, jchar* buf) - { functions->GetStringRegion(this, str, start, len, buf); } - - void GetStringUTFRegion(jstring str, jsize start, jsize len, char* buf) - { return functions->GetStringUTFRegion(this, str, start, len, buf); } - - void* GetPrimitiveArrayCritical(jarray array, jboolean* isCopy) - { return functions->GetPrimitiveArrayCritical(this, array, isCopy); } - - void ReleasePrimitiveArrayCritical(jarray array, void* carray, jint mode) - { functions->ReleasePrimitiveArrayCritical(this, array, carray, mode); } - - const jchar* GetStringCritical(jstring string, jboolean* isCopy) - { return functions->GetStringCritical(this, string, isCopy); } - - void ReleaseStringCritical(jstring string, const jchar* carray) - { functions->ReleaseStringCritical(this, string, carray); } - - jweak NewWeakGlobalRef(jobject obj) - { return functions->NewWeakGlobalRef(this, obj); } - - void DeleteWeakGlobalRef(jweak obj) - { functions->DeleteWeakGlobalRef(this, obj); } - - jboolean ExceptionCheck() - { return functions->ExceptionCheck(this); } - - jobject NewDirectByteBuffer(void* address, jlong capacity) - { return functions->NewDirectByteBuffer(this, address, capacity); } - - void* GetDirectBufferAddress(jobject buf) - { return functions->GetDirectBufferAddress(this, buf); } - - jlong GetDirectBufferCapacity(jobject buf) - { return functions->GetDirectBufferCapacity(this, buf); } - - /* added in JNI 1.6 */ - jobjectRefType GetObjectRefType(jobject obj) - { return functions->GetObjectRefType(this, obj); } -#endif /*__cplusplus*/ -}; - - -/* - * JNI invocation interface. - */ -struct JNIInvokeInterface { - void* reserved0; - void* reserved1; - void* reserved2; - - jint (*DestroyJavaVM)(JavaVM*); - jint (*AttachCurrentThread)(JavaVM*, JNIEnv**, void*); - jint (*DetachCurrentThread)(JavaVM*); - jint (*GetEnv)(JavaVM*, void**, jint); - jint (*AttachCurrentThreadAsDaemon)(JavaVM*, JNIEnv**, void*); -}; - -/* - * C++ version. - */ -struct _JavaVM { - const struct JNIInvokeInterface* functions; - -#if defined(__cplusplus) - jint DestroyJavaVM() - { return functions->DestroyJavaVM(this); } - jint AttachCurrentThread(JNIEnv** p_env, void* thr_args) - { return functions->AttachCurrentThread(this, p_env, thr_args); } - jint DetachCurrentThread() - { return functions->DetachCurrentThread(this); } - jint GetEnv(void** env, jint version) - { return functions->GetEnv(this, env, version); } - jint AttachCurrentThreadAsDaemon(JNIEnv** p_env, void* thr_args) - { return functions->AttachCurrentThreadAsDaemon(this, p_env, thr_args); } -#endif /*__cplusplus*/ -}; - -struct JavaVMAttachArgs { - jint version; /* must be >= JNI_VERSION_1_2 */ - const char* name; /* NULL or name of thread as modified UTF-8 str */ - jobject group; /* global ref of a ThreadGroup object, or NULL */ -}; -typedef struct JavaVMAttachArgs JavaVMAttachArgs; - -/* - * JNI 1.2+ initialization. (As of 1.6, the pre-1.2 structures are no - * longer supported.) - */ -typedef struct JavaVMOption { - const char* optionString; - void* extraInfo; -} JavaVMOption; - -typedef struct JavaVMInitArgs { - jint version; /* use JNI_VERSION_1_2 or later */ - - jint nOptions; - JavaVMOption* options; - jboolean ignoreUnrecognized; -} JavaVMInitArgs; - -#ifdef __cplusplus -extern "C" { -#endif -/* - * VM initialization functions. - * - * Note these are the only symbols exported for JNI by the VM. - */ -jint JNI_GetDefaultJavaVMInitArgs(void*); -jint JNI_CreateJavaVM(JavaVM**, JNIEnv**, void*); -jint JNI_GetCreatedJavaVMs(JavaVM**, jsize, jsize*); - -#define JNIIMPORT -#define JNIEXPORT __attribute__ ((visibility ("default"))) -#define JNICALL - -/* - * Prototypes for functions exported by loadable shared libs. These are - * called by JNI, not provided by JNI. - */ -JNIEXPORT jint JNI_OnLoad(JavaVM* vm, void* reserved); -JNIEXPORT void JNI_OnUnload(JavaVM* vm, void* reserved); - -#ifdef __cplusplus -} -#endif - - -/* - * Manifest constants. - */ -#define JNI_FALSE 0 -#define JNI_TRUE 1 - -#define JNI_VERSION_1_1 0x00010001 -#define JNI_VERSION_1_2 0x00010002 -#define JNI_VERSION_1_4 0x00010004 -#define JNI_VERSION_1_6 0x00010006 - -#define JNI_OK (0) /* no error */ -#define JNI_ERR (-1) /* generic error */ -#define JNI_EDETACHED (-2) /* thread detached from the VM */ -#define JNI_EVERSION (-3) /* JNI version error */ - -#define JNI_COMMIT 1 /* copy content, do not free buffer */ -#define JNI_ABORT 2 /* free buffer w/o copying back */ - -#endif /* JNI_H_ */ diff --git a/android/library/src/main/jni/libs/nativehelper/include/nativehelper/toStringArray.h b/android/library/src/main/jni/libs/nativehelper/include/nativehelper/toStringArray.h deleted file mode 100644 index 9dc7a16..0000000 --- a/android/library/src/main/jni/libs/nativehelper/include/nativehelper/toStringArray.h +++ /dev/null @@ -1,71 +0,0 @@ -/* - * Copyright (C) 2011 The Android Open Source Project - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -#ifndef TO_STRING_ARRAY_H_included -#define TO_STRING_ARRAY_H_included - -#include "jni.h" -#include "ScopedLocalRef.h" - -#include -#include - -jobjectArray newStringArray(JNIEnv* env, size_t count); - -template -jobjectArray toStringArray(JNIEnv* env, Counter* counter, Getter* getter) { - size_t count = (*counter)(); - jobjectArray result = newStringArray(env, count); - if (result == NULL) { - return NULL; - } - for (size_t i = 0; i < count; ++i) { - ScopedLocalRef s(env, env->NewStringUTF((*getter)(i))); - if (env->ExceptionCheck()) { - return NULL; - } - env->SetObjectArrayElement(result, i, s.get()); - if (env->ExceptionCheck()) { - return NULL; - } - } - return result; -} - -struct VectorCounter { - const std::vector& strings; - VectorCounter(const std::vector& strings) : strings(strings) {} - size_t operator()() { - return strings.size(); - } -}; -struct VectorGetter { - const std::vector& strings; - VectorGetter(const std::vector& strings) : strings(strings) {} - const char* operator()(size_t i) { - return strings[i].c_str(); - } -}; - -inline jobjectArray toStringArray(JNIEnv* env, const std::vector& strings) { - VectorCounter counter(strings); - VectorGetter getter(strings); - return toStringArray(env, &counter, &getter); -} - -JNIEXPORT jobjectArray toStringArray(JNIEnv* env, const char* const* strings); - -#endif // TO_STRING_ARRAY_H_included diff --git a/android/library/src/main/jni/libs/nativehelper/tests/Android.mk b/android/library/src/main/jni/libs/nativehelper/tests/Android.mk deleted file mode 100644 index c357b24..0000000 --- a/android/library/src/main/jni/libs/nativehelper/tests/Android.mk +++ /dev/null @@ -1,35 +0,0 @@ -# Build the unit tests. -LOCAL_PATH := $(call my-dir) -include $(CLEAR_VARS) - -# Target unit test. - -include $(CLEAR_VARS) -LOCAL_MODULE := JniInvocation_test -LOCAL_CLANG := true -LOCAL_SRC_FILES := JniInvocation_test.cpp -LOCAL_SHARED_LIBRARIES := \ - libnativehelper - -include external/libcxx/libcxx.mk - -LOCAL_MULTILIB := both -LOCAL_MODULE_STEM_32 := $(LOCAL_MODULE)32 -LOCAL_MODULE_STEM_64 := $(LOCAL_MODULE)64 -include $(BUILD_NATIVE_TEST) - -# Host unit test. - -include $(CLEAR_VARS) -LOCAL_MODULE := JniInvocation_test -LOCAL_CLANG := true -LOCAL_SRC_FILES := JniInvocation_test.cpp -LOCAL_SHARED_LIBRARIES := \ - libnativehelper - -include external/libcxx/libcxx.mk - -LOCAL_MULTILIB := both -LOCAL_MODULE_STEM_32 := $(LOCAL_MODULE)32 -LOCAL_MODULE_STEM_64 := $(LOCAL_MODULE)64 -include $(BUILD_HOST_NATIVE_TEST) diff --git a/android/library/src/main/jni/libs/nativehelper/tests/JniInvocation_test.cpp b/android/library/src/main/jni/libs/nativehelper/tests/JniInvocation_test.cpp deleted file mode 100644 index 6eb0267..0000000 --- a/android/library/src/main/jni/libs/nativehelper/tests/JniInvocation_test.cpp +++ /dev/null @@ -1,144 +0,0 @@ -/* - * Copyright (C) 2014 The Android Open Source Project - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -#define LOG_TAG "NativeBridge_test" - -#include -#include - - -#include "string.h" - -#if defined(HAVE_ANDROID_OS) && defined(__BIONIC__) -#define HAVE_TEST_STUFF 1 -#else -#undef HAVE_TEST_STUFF -#endif - -#ifdef HAVE_TEST_STUFF - -// Ability to have fake local system properties. -#define _REALLY_INCLUDE_SYS__SYSTEM_PROPERTIES_H_ -#include - -extern void *__system_property_area__; - -struct LocalPropertyTestState { - LocalPropertyTestState() : valid(false) { - const char* ANDROID_DATA = getenv("ANDROID_DATA"); - char dir_template[PATH_MAX]; - snprintf(dir_template, sizeof(dir_template), "%s/local/tmp/prop-XXXXXX", ANDROID_DATA); - char* dirname = mkdtemp(dir_template); - if (!dirname) { - fprintf(stderr, "making temp file for test state failed (is %s writable?): %s", - dir_template, strerror(errno)); - return; - } - - old_pa = __system_property_area__; - __system_property_area__ = NULL; - - pa_dirname = dirname; - pa_filename = pa_dirname + "/__properties__"; - - __system_property_set_filename(pa_filename.c_str()); - __system_property_area_init(); - valid = true; - } - - ~LocalPropertyTestState() { - if (!valid) { - return; - } - - __system_property_area__ = old_pa; - - __system_property_set_filename(PROP_FILENAME); - unlink(pa_filename.c_str()); - rmdir(pa_dirname.c_str()); - } -public: - bool valid; -private: - std::string pa_dirname; - std::string pa_filename; - void *old_pa; -}; -#endif - -namespace android { - -class JNIInvocationTest : public testing::Test { -}; - -#ifdef HAVE_TEST_STUFF -static const char* kDebuggableSystemProperty = "ro.debuggable"; -static const char* kIsDebuggableValue = "1"; -static const char* kIsNotDebuggableValue = "0"; - -static const char* kLibrarySystemProperty = "persist.sys.dalvik.vm.lib.2"; -static const char* kTestNonNull = "libartd.so"; -static const char* kTestNonNull2 = "libartd2.so"; -static const char* kExpected = "libart.so"; -#endif - -TEST_F(JNIInvocationTest, Debuggable) { -#ifdef HAVE_TEST_STUFF - LocalPropertyTestState pa; - ASSERT_TRUE(pa.valid); - ASSERT_EQ(0, __system_property_add(kDebuggableSystemProperty, 13, kIsDebuggableValue, 1)); - ASSERT_EQ(0, __system_property_add(kLibrarySystemProperty, 27, kTestNonNull2, 11)); - - const char* result = JniInvocation::GetLibrary(NULL); - EXPECT_FALSE(result == NULL); - if (result != NULL) { - EXPECT_TRUE(strcmp(result, kTestNonNull2) == 0); - EXPECT_FALSE(strcmp(result, kExpected) == 0); - } - - result = JniInvocation::GetLibrary(kTestNonNull); - EXPECT_FALSE(result == NULL); - if (result != NULL) { - EXPECT_TRUE(strcmp(result, kTestNonNull) == 0); - EXPECT_FALSE(strcmp(result, kTestNonNull2) == 0); - } -#endif -} - -TEST_F(JNIInvocationTest, NonDebuggable) { -#ifdef HAVE_TEST_STUFF - LocalPropertyTestState pa; - ASSERT_TRUE(pa.valid); - ASSERT_EQ(0, __system_property_add(kDebuggableSystemProperty, 13, kIsNotDebuggableValue, 1)); - - const char* result = JniInvocation::GetLibrary(NULL); - EXPECT_FALSE(result == NULL); - if (result != NULL) { - EXPECT_TRUE(strcmp(result, kExpected) == 0); - EXPECT_FALSE(strcmp(result, kTestNonNull) == 0); - EXPECT_FALSE(strcmp(result, kTestNonNull2) == 0); - } - - result = JniInvocation::GetLibrary(kTestNonNull); - EXPECT_FALSE(result == NULL); - if (result != NULL) { - EXPECT_TRUE(strcmp(result, kExpected) == 0); - EXPECT_FALSE(strcmp(result, kTestNonNull) == 0); - } -#endif -} - -} // namespace android diff --git a/android/library/src/main/jni/libs/nativehelper/toStringArray.cpp b/android/library/src/main/jni/libs/nativehelper/toStringArray.cpp deleted file mode 100644 index f2fa53c..0000000 --- a/android/library/src/main/jni/libs/nativehelper/toStringArray.cpp +++ /dev/null @@ -1,48 +0,0 @@ -/* - * Copyright (C) 2011 The Android Open Source Project - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -#include "JniConstants.h" -#include "toStringArray.h" - -jobjectArray newStringArray(JNIEnv* env, size_t count) { - return env->NewObjectArray(count, JniConstants::stringClass, NULL); -} - -struct ArrayCounter { - const char* const* strings; - ArrayCounter(const char* const* strings) : strings(strings) {} - size_t operator()() { - size_t count = 0; - while (strings[count] != NULL) { - ++count; - } - return count; - } -}; - -struct ArrayGetter { - const char* const* strings; - ArrayGetter(const char* const* strings) : strings(strings) {} - const char* operator()(size_t i) { - return strings[i]; - } -}; - -jobjectArray toStringArray(JNIEnv* env, const char* const* strings) { - ArrayCounter counter(strings); - ArrayGetter getter(strings); - return toStringArray(env, &counter, &getter); -} diff --git a/android/library/src/main/jni/list_ports_linux.cc b/android/library/src/main/jni/list_ports_linux.cc deleted file mode 120000 index 1ae477d..0000000 --- a/android/library/src/main/jni/list_ports_linux.cc +++ /dev/null @@ -1 +0,0 @@ -../../../../../src/impl/list_ports/list_ports_linux.cc \ No newline at end of file diff --git a/android/library/src/main/jni/serial.cc b/android/library/src/main/jni/serial.cc deleted file mode 120000 index d3a5558..0000000 --- a/android/library/src/main/jni/serial.cc +++ /dev/null @@ -1 +0,0 @@ -../../../../../src/serial.cc \ No newline at end of file diff --git a/android/library/src/main/jni/serial_jni.cc b/android/library/src/main/jni/serial_jni.cc deleted file mode 100644 index 37d93a4..0000000 --- a/android/library/src/main/jni/serial_jni.cc +++ /dev/null @@ -1,466 +0,0 @@ -#include -#include "jni_utility.h" -#include - -using namespace std; -using namespace serial; - -#define MAKE_TIMEOUT(env,in,out) jint* _array = (env)->GetIntArrayElements((in), NULL); \ - if (_array[0] == -1) {\ - _array[0] = Timeout::max();\ - }\ - Timeout out(_array[0], _array[1], _array[2], _array[3], _array[4]);\ - env->ReleaseIntArrayElements((in), _array, JNI_ABORT) - - -#define _BEGIN_TRY try { -#define _CATCH(cpp_ex) } catch (cpp_ex& _ex) { -#define _CATCH_AND_THROW(env, cpp_ex, java_ex) } catch (cpp_ex& _ex) { \ - LOGE("%s", _ex.what());\ - (env)->ThrowNew(java_ex, _ex.what()); -#define _END_TRY } - -static jclass gSerialExceptionClass = 0; -static jclass gSerialIOExceptionClass = 0; -static jclass gIllegalArgumentException = 0; - -static jobjectArray native_listPorts(JNIEnv *env, jobject) -{ - std::vector ports = list_ports(); - jobjectArray portDescs = createStringArray(env, ports.size()); - int i = 0; - for(std::vector::iterator it = ports.begin(); it < ports.end(); ++it, ++i) { - std::string desc; - desc += it->port; - desc.push_back('\t'); - desc += it->description; - desc.push_back('\t'); - desc += it->hardware_id; - env->SetObjectArrayElement(portDescs, i, stdStringToJstring(desc)); - } - - return portDescs; -} - -static jlong native_create(JNIEnv *env, jobject, jstring jport, jint baudrate, jintArray jtimeout, jint bytesize, jint parity, jint stopbits, jint flowcontrol) -{ - std::string port = jstringToStdString(jport); - MAKE_TIMEOUT(env, jtimeout, timeout); - - Serial * com = NULL; - _BEGIN_TRY - Serial * com = new Serial(port, (uint32_t)baudrate, timeout, - bytesize_t(bytesize), parity_t(parity), stopbits_t(stopbits), flowcontrol_t(flowcontrol)); - LOGD("Native serial port object %p.", com); - return (jlong)com; - _CATCH_AND_THROW(env, invalid_argument, gIllegalArgumentException) - _CATCH_AND_THROW(env, IOException, gSerialIOExceptionClass) - _CATCH_AND_THROW(env, SerialException, gSerialExceptionClass) - _END_TRY - return (jlong)com; -} - -static void native_destory(JNIEnv *env, jobject, jlong ptr) -{ - Serial * com = (Serial *)ptr; - if (com) - delete com; -} - -static void native_open(JNIEnv *env, jobject, jlong ptr) -{ - Serial * com = (Serial *)ptr; - _BEGIN_TRY - com->open(); - _CATCH_AND_THROW(env, SerialException, gSerialExceptionClass) - _CATCH_AND_THROW(env, IOException, gSerialIOExceptionClass) - _CATCH_AND_THROW(env, invalid_argument, gIllegalArgumentException) - _END_TRY -} - -static jboolean native_isOpen(JNIEnv *env, jobject, jlong ptr) -{ - Serial * com = (Serial *)ptr; - return com->isOpen() ? JNI_TRUE : JNI_FALSE; -} - -static void native_close(JNIEnv *env, jobject, jlong ptr) -{ - Serial * com = (Serial *)ptr; - _BEGIN_TRY - com->close(); - _CATCH_AND_THROW(env, IOException, gSerialIOExceptionClass) - _END_TRY -} - -static jint native_available(JNIEnv *env, jobject, jlong ptr) -{ - Serial * com = (Serial *)ptr; - _BEGIN_TRY - return (jint)com->available(); - _CATCH_AND_THROW(env, IOException, gSerialIOExceptionClass) - _END_TRY - return -1; -} - -static jboolean native_waitReadable(JNIEnv *env, jobject, jlong ptr) -{ - Serial * com = (Serial *)ptr; - _BEGIN_TRY - return com->waitReadable() ? JNI_TRUE : JNI_FALSE; - _CATCH_AND_THROW(env, IOException, gSerialIOExceptionClass) - _END_TRY - return JNI_FALSE; -} - -static void native_waitByteTimes(JNIEnv *env, jobject, jlong ptr, jint count) -{ - Serial * com = (Serial *)ptr; - com->waitByteTimes(count); -} - -static jint native_read(JNIEnv *env, jobject, jlong ptr, jbyteArray jbuffer, jint offset, jint size) -{ - LOGI("native_read(0x%08llx,%p,%d,%d)", ptr, jbuffer, offset, size); - Serial * com = (Serial *)ptr; - jbyte* jarray = env->GetByteArrayElements(jbuffer, NULL); - if (jarray) { - uint8_t * buffer = (uint8_t *)(jarray + offset); - LOGI("buffer = %p, size=%d", buffer, size); - _BEGIN_TRY - int bytesRead = com->read(buffer, (size_t)size); - LOGI("bytes read = %d", bytesRead); - env->ReleaseByteArrayElements(jbuffer, jarray, 0); - return (jint)bytesRead; - _CATCH_AND_THROW(env, invalid_argument, gIllegalArgumentException) - env->ReleaseByteArrayElements(jbuffer, jarray, JNI_ABORT); - _CATCH_AND_THROW(env, IOException, gSerialIOExceptionClass) - env->ReleaseByteArrayElements(jbuffer, jarray, JNI_ABORT); - _CATCH_AND_THROW(env, SerialException, gSerialExceptionClass) - env->ReleaseByteArrayElements(jbuffer, jarray, JNI_ABORT); - _END_TRY - return -1; - } - return -1; // Failed -} - -static jstring native_readline(JNIEnv *env, jobject, jlong ptr, jint size, jstring jeol) -{ - Serial * com = (Serial *)ptr; - std::string eol = jstringToStdString(jeol); - _BEGIN_TRY - std::string line = com->readline(size, eol); - return stdStringToJstring(line); - _CATCH_AND_THROW(env, SerialException, gSerialExceptionClass) - _CATCH_AND_THROW(env, IOException, gSerialIOExceptionClass) - _END_TRY - return NULL; -} - -static jobjectArray native_readlines(JNIEnv *env, jobject, jlong ptr, jint size, jstring jeol) -{ - Serial * com = (Serial *)ptr; - std::string eol = jstringToStdString(jeol); - jobjectArray jlines = NULL; - _BEGIN_TRY - vector lines = com->readlines(size, eol); - jlines = createStringArray(env, lines.size()); - int i = 0; - for(vector::iterator it = lines.begin(); it < lines.end(); ++it, ++i) { - env->SetObjectArrayElement(jlines, i, stdStringToJstring(*it)); - } - _CATCH_AND_THROW(env, SerialException, gSerialExceptionClass) - _CATCH_AND_THROW(env, IOException, gSerialIOExceptionClass) - _END_TRY - return jlines; -} - -static jint native_write(JNIEnv *env, jobject, jlong ptr, jbyteArray jdata, jint size) -{ - LOGI("native_write(0x%08llx,%p,%d)", ptr, jdata, size); - Serial * com = (Serial *)ptr; - jbyte* jarray = env->GetByteArrayElements(jdata, NULL); - if (jarray) { - uint8_t * data = (uint8_t *)jarray; - LOGI("data = %p, size=%d", data, size); - _BEGIN_TRY - int bytesWritten = com->write(data, (size_t)size); - LOGI("bytes written = %d", bytesWritten); - env->ReleaseByteArrayElements(jdata, jarray, JNI_ABORT); - return (jint)bytesWritten; - _CATCH_AND_THROW(env, invalid_argument, gIllegalArgumentException) - env->ReleaseByteArrayElements(jdata, jarray, JNI_ABORT); - _CATCH_AND_THROW(env, IOException, gSerialIOExceptionClass) - env->ReleaseByteArrayElements(jdata, jarray, JNI_ABORT); - _CATCH_AND_THROW(env, SerialException, gSerialExceptionClass) - env->ReleaseByteArrayElements(jdata, jarray, JNI_ABORT); - _END_TRY - } - return -1; -} - -static void native_setPort(JNIEnv *env, jobject, jlong ptr, jstring jport) -{ - Serial * com = (Serial *)ptr; - std::string port = jstringToStdString(jport); - com->setPort(port); -} - -static jstring native_getPort(JNIEnv *env, jobject, jlong ptr) -{ - Serial * com = (Serial *)ptr; - std::string port = com->getPort(); - return stdStringToJstring(port); -} - -static void native_setBaudrate(JNIEnv *env, jobject, jlong ptr, jint baudrate) -{ - Serial * com = (Serial *)ptr; - _BEGIN_TRY - com->setBaudrate((uint32_t)baudrate); - _CATCH_AND_THROW(env, invalid_argument, gIllegalArgumentException) - _CATCH_AND_THROW(env, IOException, gSerialIOExceptionClass) - _CATCH_AND_THROW(env, SerialException, gSerialExceptionClass) - _END_TRY -} - -static jint native_getBaudrate(JNIEnv *env, jobject, jlong ptr) -{ - Serial * com = (Serial *)ptr; - return (jint)com->getBaudrate(); -} - -static void native_setTimeout(JNIEnv *env, jobject, jlong ptr, jintArray jtimeout) -{ - Serial * com = (Serial *)ptr; - MAKE_TIMEOUT(env, jtimeout, timeout); - com->setTimeout(timeout); -} - -static void native_setBytesize(JNIEnv *env, jobject, jlong ptr, jint bytesize) -{ - Serial * com = (Serial *)ptr; - _BEGIN_TRY - com->setBytesize(bytesize_t(bytesize)); - _CATCH_AND_THROW(env, invalid_argument, gIllegalArgumentException) - _CATCH_AND_THROW(env, IOException, gSerialIOExceptionClass) - _CATCH_AND_THROW(env, SerialException, gSerialExceptionClass) - _END_TRY -} - -static jint native_getBytesize(JNIEnv *env, jobject, jlong ptr) -{ - Serial * com = (Serial *)ptr; - return (jint)com->getBytesize(); -} - -static void native_setParity(JNIEnv *env, jobject, jlong ptr, jint parity) -{ - Serial * com = (Serial *)ptr; - _BEGIN_TRY - com->setParity(parity_t(parity)); - _CATCH_AND_THROW(env, invalid_argument, gIllegalArgumentException) - _CATCH_AND_THROW(env, IOException, gSerialIOExceptionClass) - _CATCH_AND_THROW(env, SerialException, gSerialExceptionClass) - _END_TRY -} - -static jint native_getParity(JNIEnv *env, jobject, jlong ptr) -{ - Serial * com = (Serial *)ptr; - return (jint)com->getParity(); -} - -static void native_setStopbits(JNIEnv *env, jobject, jlong ptr, jint stopbits) -{ - Serial * com = (Serial *)ptr; - _BEGIN_TRY - com->setStopbits(stopbits_t(stopbits)); - _CATCH_AND_THROW(env, SerialException, gSerialExceptionClass) - _CATCH_AND_THROW(env, IOException, gSerialIOExceptionClass) - _END_TRY -} - -static jint native_getStopbits(JNIEnv *env, jobject, jlong ptr) -{ - Serial * com = (Serial *)ptr; - return (jint)com->getStopbits(); -} - -static void native_setFlowcontrol(JNIEnv *env, jobject, jlong ptr, jint flowcontrol) -{ - Serial * com = (Serial *)ptr; - _BEGIN_TRY - com->setFlowcontrol(flowcontrol_t(flowcontrol)); - _CATCH_AND_THROW(env, invalid_argument, gIllegalArgumentException) - _CATCH_AND_THROW(env, IOException, gSerialIOExceptionClass) - _CATCH_AND_THROW(env, SerialException, gSerialExceptionClass) - _END_TRY -} - -static jint native_getFlowcontrol(JNIEnv *env, jobject, jlong ptr) -{ - Serial * com = (Serial *)ptr; - return (jint)com->getFlowcontrol(); -} - -static void native_flush(JNIEnv *env, jobject, jlong ptr) -{ - Serial * com = (Serial *)ptr; - com->flush(); -} - -static void native_flushInput(JNIEnv *env, jobject, jlong ptr) -{ - Serial * com = (Serial *)ptr; - com->flushInput(); -} - -static void native_flushOutput(JNIEnv *env, jobject, jlong ptr) -{ - Serial * com = (Serial *)ptr; - com->flushOutput(); -} - -static void native_sendBreak(JNIEnv *env, jobject, jlong ptr, jint duration) -{ - Serial * com = (Serial *)ptr; - com->sendBreak((int)duration); -} - -static void native_setBreak(JNIEnv *env, jobject, jlong ptr, jboolean level) -{ - Serial * com = (Serial *)ptr; - _BEGIN_TRY - com->setBreak((bool)level); - _CATCH_AND_THROW(env, IOException, gSerialIOExceptionClass) - _CATCH_AND_THROW(env, SerialException, gSerialExceptionClass) - _END_TRY -} - -static void native_setRTS(JNIEnv *env, jobject, jlong ptr, jboolean level) -{ - Serial * com = (Serial *)ptr; - _BEGIN_TRY - com->setRTS((bool)level); - _CATCH_AND_THROW(env, IOException, gSerialIOExceptionClass) - _CATCH_AND_THROW(env, SerialException, gSerialExceptionClass) - _END_TRY -} - -static void native_setDTR(JNIEnv *env, jobject, jlong ptr, jboolean level) -{ - Serial * com = (Serial *)ptr; - _BEGIN_TRY - com->setDTR((bool)level); - _CATCH_AND_THROW(env, IOException, gSerialIOExceptionClass) - _CATCH_AND_THROW(env, SerialException, gSerialExceptionClass) - _END_TRY -} - -static jboolean native_waitForChange(JNIEnv *env, jobject, jlong ptr) -{ - Serial * com = (Serial *)ptr; - _BEGIN_TRY - return com->waitForChange() ? JNI_TRUE : JNI_FALSE; - _CATCH_AND_THROW(env, SerialException, gSerialExceptionClass) - _END_TRY - return JNI_FALSE; -} - -static jboolean native_getCTS(JNIEnv *env, jobject, jlong ptr) -{ - Serial * com = (Serial *)ptr; - _BEGIN_TRY - return com->getCTS() ? JNI_TRUE : JNI_FALSE; - _CATCH_AND_THROW(env, SerialException, gSerialExceptionClass) - _END_TRY - return JNI_FALSE; -} - -static jboolean native_getDSR(JNIEnv *env, jobject, jlong ptr) -{ - Serial * com = (Serial *)ptr; - _BEGIN_TRY - return com->getDSR() ? JNI_TRUE : JNI_FALSE; - _CATCH_AND_THROW(env, SerialException, gSerialExceptionClass) - _END_TRY - return JNI_FALSE; -} - -static jboolean native_getRI(JNIEnv *env, jobject, jlong ptr) -{ - Serial * com = (Serial *)ptr; - _BEGIN_TRY - return com->getRI() ? JNI_TRUE : JNI_FALSE; - _CATCH_AND_THROW(env, SerialException, gSerialExceptionClass) - _END_TRY - return JNI_FALSE; -} - -static jboolean native_getCD(JNIEnv *env, jobject, jlong ptr) -{ - Serial * com = (Serial *)ptr; - _BEGIN_TRY - return com->getCD() ? JNI_TRUE : JNI_FALSE; - _CATCH_AND_THROW(env, SerialException, gSerialExceptionClass) - _END_TRY - return JNI_FALSE; -} - - -#ifdef __cplusplus -extern "C" { -#endif - -static JNINativeMethod gSerialMethods[] = { - { "native_listPorts", "()[Ljava/lang/String;", (void*) native_listPorts }, - { "native_create", "(Ljava/lang/String;I[IIIII)J", (void*) native_create }, - { "native_destory", "(J)V", (void*) native_destory }, - { "native_open", "(J)V", (void*) native_open }, - { "native_isOpen", "(J)Z", (void*) native_isOpen }, - { "native_close", "(J)V", (void*) native_close }, - { "native_available", "(J)I", (void*) native_available }, - { "native_waitReadable", "(J)Z", (void*) native_waitReadable }, - { "native_waitByteTimes", "(JI)V", (void*) native_waitByteTimes }, - { "native_read", "(J[BII)I", (void*) native_read }, - { "native_readline", "(JILjava/lang/String;)Ljava/lang/String;", (void*) native_readline }, - { "native_readlines", "(JILjava/lang/String;)[Ljava/lang/String;", (void*) native_readlines }, - { "native_write", "(J[BI)I", (void*) native_write }, - { "native_setPort", "(JLjava/lang/String;)V", (void*) native_setPort }, - { "native_getPort", "(J)Ljava/lang/String;", (void*) native_getPort }, - { "native_setBaudrate", "(JI)V", (void*) native_setBaudrate }, - { "native_getBaudrate", "(J)I", (void*) native_getBaudrate }, - { "native_setTimeout", "(J[I)V", (void*) native_setTimeout }, - { "native_setBytesize", "(JI)V", (void*) native_setBytesize }, - { "native_getBytesize", "(J)I", (void*) native_getBytesize }, - { "native_setParity", "(JI)V", (void*) native_setParity }, - { "native_getParity", "(J)I", (void*) native_getParity }, - { "native_setStopbits", "(JI)V", (void*) native_setStopbits }, - { "native_getStopbits", "(J)I", (void*) native_getStopbits }, - { "native_setFlowcontrol", "(JI)V", (void*) native_setFlowcontrol }, - { "native_getFlowcontrol", "(J)I", (void*) native_getFlowcontrol }, - { "native_flush", "(J)V", (void*) native_flush }, - { "native_flushInput", "(J)V", (void*) native_flushInput }, - { "native_flushOutput", "(J)V", (void*) native_flushOutput }, - { "native_sendBreak", "(JI)V", (void*) native_sendBreak }, - { "native_setBreak", "(JZ)V", (void*) native_setBreak }, - { "native_setRTS", "(JZ)V", (void*) native_setRTS }, - { "native_setDTR", "(JZ)V", (void*) native_setDTR }, - { "native_waitForChange", "(J)Z", (void*) native_waitForChange }, - { "native_getCTS", "(J)Z", (void*) native_getCTS }, - { "native_getDSR", "(J)Z", (void*) native_getDSR }, - { "native_getRI", "(J)Z", (void*) native_getRI }, - { "native_getCD", "(J)Z", (void*) native_getCD }, -}; - -int registerSerial(JNIEnv* env) -{ - gSerialExceptionClass = findClass("serial/SerialException", FIND_CLASS_RETURN_GLOBAL_REF); - gSerialIOExceptionClass = findClass("serial/SerialIOException", FIND_CLASS_RETURN_GLOBAL_REF); - gIllegalArgumentException = findClass("java/lang/IllegalArgumentException", FIND_CLASS_RETURN_GLOBAL_REF); - return jniRegisterNativeMethods(env, "serial/Serial", gSerialMethods, NELEM(gSerialMethods)); -} -#ifdef __cplusplus -} -#endif diff --git a/android/library/src/main/jni/serial_unix.cc b/android/library/src/main/jni/serial_unix.cc deleted file mode 120000 index d09f7b2..0000000 --- a/android/library/src/main/jni/serial_unix.cc +++ /dev/null @@ -1 +0,0 @@ -../../../../../src/impl/unix.cc \ No newline at end of file diff --git a/android/library/src/test/java/serial/android/ExampleUnitTest.java b/android/library/src/test/java/serial/android/ExampleUnitTest.java deleted file mode 100644 index ced0e2d..0000000 --- a/android/library/src/test/java/serial/android/ExampleUnitTest.java +++ /dev/null @@ -1,15 +0,0 @@ -package serial.android; - -import org.junit.Test; - -import static org.junit.Assert.*; - -/** - * To work on unit tests, switch the Test Artifact in the Build Variants view. - */ -public class ExampleUnitTest { - @Test - public void addition_isCorrect() throws Exception { - assertEquals(4, 2 + 2); - } -} \ No newline at end of file diff --git a/android/sample/build.gradle b/android/sample/build.gradle deleted file mode 100644 index 6349830..0000000 --- a/android/sample/build.gradle +++ /dev/null @@ -1,29 +0,0 @@ -apply plugin: 'com.android.application' - -android { - compileSdkVersion 23 - buildToolsVersion "23.0.2" - - defaultConfig { - applicationId "serial.android.sample" - minSdkVersion 16 - targetSdkVersion 23 - versionCode 1 - versionName "1.0" - } - buildTypes { - release { - minifyEnabled false - proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro' - } - } -} - -dependencies { - compile fileTree(dir: 'libs', include: ['*.jar']) - testCompile 'junit:junit:4.12' - compile 'com.android.support:appcompat-v7:23.1.1' - compile 'com.android.support:design:23.1.1' - - compile project(':library') -} diff --git a/android/sample/proguard-rules.pro b/android/sample/proguard-rules.pro deleted file mode 100644 index 9aa02b1..0000000 --- a/android/sample/proguard-rules.pro +++ /dev/null @@ -1,17 +0,0 @@ -# Add project specific ProGuard rules here. -# By default, the flags in this file are appended to flags specified -# in /mnt/android/android-sdk-linux_x86/tools/proguard/proguard-android.txt -# You can edit the include path and order by changing the proguardFiles -# directive in build.gradle. -# -# For more details, see -# http://developer.android.com/guide/developing/tools/proguard.html - -# Add any project specific keep options here: - -# If your project uses WebView with JS, uncomment the following -# and specify the fully qualified class name to the JavaScript interface -# class: -#-keepclassmembers class fqcn.of.javascript.interface.for.webview { -# public *; -#} diff --git a/android/sample/src/androidTest/java/serial/android/sample/ApplicationTest.java b/android/sample/src/androidTest/java/serial/android/sample/ApplicationTest.java deleted file mode 100644 index 7f18a97..0000000 --- a/android/sample/src/androidTest/java/serial/android/sample/ApplicationTest.java +++ /dev/null @@ -1,13 +0,0 @@ -package serial.android.sample; - -import android.app.Application; -import android.test.ApplicationTestCase; - -/** - * Testing Fundamentals - */ -public class ApplicationTest extends ApplicationTestCase { - public ApplicationTest() { - super(Application.class); - } -} \ No newline at end of file diff --git a/android/sample/src/main/AndroidManifest.xml b/android/sample/src/main/AndroidManifest.xml deleted file mode 100644 index 03099b2..0000000 --- a/android/sample/src/main/AndroidManifest.xml +++ /dev/null @@ -1,23 +0,0 @@ - - - - - - - - - - - - - - diff --git a/android/sample/src/main/java/serial/android/sample/MainActivity.java b/android/sample/src/main/java/serial/android/sample/MainActivity.java deleted file mode 100644 index 728cd46..0000000 --- a/android/sample/src/main/java/serial/android/sample/MainActivity.java +++ /dev/null @@ -1,250 +0,0 @@ -package serial.android.sample; - -import android.content.Context; -import android.graphics.Color; -import android.os.Bundle; -import android.os.Handler; -import android.support.design.widget.FloatingActionButton; -import android.support.design.widget.Snackbar; -import android.support.v7.app.AppCompatActivity; -import android.support.v7.widget.Toolbar; -import android.view.View; -import android.view.Menu; -import android.view.MenuItem; -import android.view.ViewGroup; -import android.widget.ArrayAdapter; -import android.widget.Button; -import android.widget.Spinner; -import android.widget.TextView; -import android.widget.Toast; -import android.widget.ToggleButton; - -import java.io.IOException; - -import serial.PortInfo; -import serial.Serial; -import serial.SerialIOException; - -public class MainActivity extends AppCompatActivity { - - private Spinner mSpinnerPorts; - private boolean mHexMode; - private TextView mTextAvailable; - private TextView mTextOutput; - private TextView mTextInput; - private Button mButtonSend; - private Button mButtonRead; - private ToggleButton mButtonConnectDisconnect; - private Serial mPort; - - private Handler mHandler = new Handler(); - - @Override - protected void onCreate(Bundle savedInstanceState) { - super.onCreate(savedInstanceState); - setContentView(R.layout.activity_main); - Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar); - setSupportActionBar(toolbar); - - mSpinnerPorts = (Spinner) findViewById(R.id.spinnerPorts); - mTextOutput = (TextView) findViewById(R.id.dataReceived); - mTextInput = (TextView) findViewById(R.id.dataToSend); - mTextAvailable = (TextView) findViewById(R.id.lblAvailable); - - mButtonSend = (Button) findViewById(R.id.btnSend); - mButtonRead = (Button) findViewById(R.id.btnRead); - mButtonConnectDisconnect = (ToggleButton) findViewById(R.id.tbConnect); - } - - @Override - protected void onStart() { - super.onStart(); - loadPorts(); - } - - @Override - public boolean onCreateOptionsMenu(Menu menu) { - // Inflate the menu; this adds items to the action bar if it is present. - getMenuInflater().inflate(R.menu.menu_main, menu); - return true; - } - - @Override - public boolean onPrepareOptionsMenu(Menu menu) { - MenuItem refreshItem = menu.findItem(R.id.action_reload); - refreshItem.setEnabled(mPort == null); - return super.onPrepareOptionsMenu(menu); - } - - @Override - public boolean onOptionsItemSelected(MenuItem item) { - // Handle action bar item clicks here. The action bar will - // automatically handle clicks on the Home/Up button, so long - // as you specify a parent activity in AndroidManifest.xml. - int id = item.getItemId(); - - boolean handled = true; - switch (id) { - case R.id.action_reload: - loadPorts(); - break; - case R.id.action_hex: - item.setChecked(!item.isChecked()); - mHexMode = item.isChecked(); - clearInput(); - clearOutput(); - Toast.makeText(MainActivity.this, "Not implemented.", Toast.LENGTH_SHORT).show(); - break; - default: - handled = false; - break; - } - - - return handled || super.onOptionsItemSelected(item); - } - - private void clearOutput() { - mTextOutput.setText(""); - } - - private void clearInput() { - mTextInput.setText(""); - } - - private void loadPorts() { - PortInfo[] ports = Serial.listPorts(); - PortAdapter adapter = new PortAdapter(getApplicationContext(), ports); - mSpinnerPorts.setAdapter(adapter); - mButtonConnectDisconnect.setChecked(false); - mButtonConnectDisconnect.setEnabled(!adapter.isEmpty()); - } - - public void onConnectDisconnectClick(View view) { - if (mPort != null) { - try { - mPort.close(); - } catch (IOException e) { - // Never get here - } - mSpinnerPorts.setEnabled(true); - mButtonRead.setEnabled(false); - mButtonSend.setEnabled(false); - mTextInput.setEnabled(false); - mPort = null; - } else { - clearInput(); - clearOutput(); - PortInfo portInfo = (PortInfo) mSpinnerPorts.getSelectedItem(); - Serial port; - try { - port = new Serial.Builder(portInfo.port, Serial.BAUDRATE_115200).create(); - if (port.isValid() && port.isOpen()) { - mPort = port; - mSpinnerPorts.setEnabled(false); - mButtonRead.setEnabled(true); - mButtonSend.setEnabled(true); - mTextInput.setEnabled(true); - checkAvailableData(); - } - } catch (SerialIOException e) { - Toast.makeText(MainActivity.this, getString(R.string.toast_connect_failed, portInfo.port), Toast.LENGTH_SHORT).show(); - mTextOutput.setText(e.getMessage()); - mButtonConnectDisconnect.setChecked(false); - } - } - } - - public void onSendClick(View view) { - if (mPort != null) { - String text = mTextInput.getText().toString(); - try { - int bytesWritten = mPort.write(text); - updateAvailableData(); - clearInput(); - Toast.makeText(MainActivity.this, getString(R.string.toast_send_success, bytesWritten, mPort.getPort()), Toast.LENGTH_SHORT).show(); - } catch (SerialIOException e) { - Toast.makeText(MainActivity.this, getString(R.string.toast_send_failed, mPort.getPort()), Toast.LENGTH_SHORT).show(); - mTextOutput.setText(e.getMessage()); - } - } - } - - public void onReadClick(View view) { - if (mPort != null) { - String[] lines; - try { - clearOutput(); - byte[] data = mPort.read(); - String s = new String(data); - mTextOutput.append(s); - Toast.makeText(MainActivity.this, getString(R.string.toast_read_success, data.length, mPort.getPort()), Toast.LENGTH_SHORT).show(); - updateAvailableData(); - } catch (SerialIOException e) { - Toast.makeText(MainActivity.this, getString(R.string.toast_send_failed, mPort.getPort()), Toast.LENGTH_SHORT).show(); - mTextOutput.setText(e.getMessage()); - } - } - } - - private void updateAvailableData() { - updateAvailableData(100L); - } - - private void updateAvailableData(long timeout) { - if (timeout <= 0) { - checkAvailableData(); - } else { - mHandler.postDelayed(new Runnable() { - @Override - public void run() { - checkAvailableData(); - } - }, timeout); - } - } - - private void checkAvailableData() { - try { - mTextAvailable.setText(getString(R.string.hint_available_bytes, mPort.available())); - } catch (SerialIOException e) { - mTextAvailable.setText(R.string.hint_unknown_bytes); - e.printStackTrace(); - } - } - - private static class PortAdapter extends ArrayAdapter { - - public PortAdapter(Context context, PortInfo[] ports) { - super(context, android.R.layout.simple_spinner_item, android.R.id.text1, ports); - setDropDownViewResource(android.R.layout.simple_list_item_2); - } - - @Override - public View getView(int position, View convertView, ViewGroup parent) { - View view = super.getView(position, convertView, parent); - if (view != convertView) { - // We created a new view. - TextView textView = (TextView)view; - textView.setTextColor(Color.BLACK); - } - return view; - } - - @Override - public View getDropDownView(int position, View convertView, ViewGroup parent) { - View view = super.getDropDownView(position, convertView, parent); - if (view != convertView) { - // We created a new view. - TextView descriptionView = (TextView)view.findViewById(android.R.id.text1); - TextView portView = (TextView)view.findViewById(android.R.id.text2); - PortInfo portInfo = getItem(position); - descriptionView.setText(portInfo.description); - descriptionView.setTextColor(Color.BLACK); - portView.setText(portInfo.port); - portView.setTextColor(Color.BLACK); - } - return view; - } - } -} diff --git a/android/sample/src/main/res/layout/activity_main.xml b/android/sample/src/main/res/layout/activity_main.xml deleted file mode 100644 index a37a5c3..0000000 --- a/android/sample/src/main/res/layout/activity_main.xml +++ /dev/null @@ -1,27 +0,0 @@ - - - - - - - - - - - - diff --git a/android/sample/src/main/res/layout/content_main.xml b/android/sample/src/main/res/layout/content_main.xml deleted file mode 100644 index 125f59c..0000000 --- a/android/sample/src/main/res/layout/content_main.xml +++ /dev/null @@ -1,144 +0,0 @@ - - - - - - - - - -