diff options
author | Sven Gothel <[email protected]> | 2020-02-09 14:45:46 +0100 |
---|---|---|
committer | Sven Gothel <[email protected]> | 2020-02-09 14:45:46 +0100 |
commit | b253c12a17b4501dbc44ad0400e7edae619ec895 (patch) | |
tree | d776144c6a2074339e88a3c3df9a73066f5cf3e4 /java/jni/helper_base.cxx | |
parent | f5a759db12fc33b4890427e81103a1740e82c6c0 (diff) |
java jni: Split helper to helper_base + helper_tinyb (modularization)
C++ tinyb namespace is for the original D-Bus implementation,
hence providing the base helper explicitly.
Add static BluetoothFactory.getNativeAPIVersion().
Diffstat (limited to 'java/jni/helper_base.cxx')
-rw-r--r-- | java/jni/helper_base.cxx | 195 |
1 files changed, 195 insertions, 0 deletions
diff --git a/java/jni/helper_base.cxx b/java/jni/helper_base.cxx new file mode 100644 index 00000000..2c0c23e6 --- /dev/null +++ b/java/jni/helper_base.cxx @@ -0,0 +1,195 @@ +/* + * Author: Andrei Vasiliu <[email protected]> + * Copyright (c) 2016 Intel Corporation. + * + * Author: Sven Gothel <[email protected]> + * Copyright (c) 2020 Gothel Software e.K. + * Copyright (c) 2020 ZAFENA AB + * + * Permission is hereby granted, free of charge, to any person obtaining + * a copy of this software and associated documentation files (the + * "Software"), to deal in the Software without restriction, including + * without limitation the rights to use, copy, modify, merge, publish, + * distribute, sublicense, and/or sell copies of the Software, and to + * permit persons to whom the Software is furnished to do so, subject to + * the following conditions: + * + * The above copyright notice and this permission notice shall be + * included in all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, + * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF + * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND + * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE + * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION + * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION + * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. + */ + +#include <jni.h> +#include <memory> +#include <stdexcept> +#include <vector> + +#include "helper_tinyb.hpp" + +jfieldID getInstanceField(JNIEnv *env, jobject obj) +{ + jclass clazz = env->GetObjectClass(obj); + // J == long + return env->GetFieldID(clazz, "nativeInstance", "J"); +} + +jclass search_class(JNIEnv *env, const char *clazz_name) +{ + jclass clazz = env->FindClass(clazz_name); + if (!clazz) + { + std::string error = "no class found: "; error += clazz_name; + throw std::runtime_error(error); + } + return clazz; +} + +jclass search_class(JNIEnv *env, jobject obj) +{ + jclass clazz = env->GetObjectClass(obj); + if (!clazz) + { + std::string error = "no class found: "; + throw std::runtime_error(error); + } + return clazz; +} + +jmethodID search_method(JNIEnv *env, jclass clazz, const char *method_name, + const char *prototype, bool is_static) +{ + jmethodID method; + if (is_static) + { + method = env->GetStaticMethodID(clazz, method_name, prototype); + } + else + { + method = env->GetMethodID(clazz, method_name, prototype); + } + + if (!method) + { + throw std::runtime_error("no method found\n"); + } + + return method; +} + +jfieldID search_field(JNIEnv *env, jclass clazz, const char *field_name, + const char *type, bool is_static) +{ + jfieldID field; + if (is_static) + { + field = env->GetStaticFieldID(clazz, field_name, type); + } + else + { + field = env->GetFieldID(clazz, field_name, type); + } + + if (!field) + { + throw std::runtime_error("no method found\n"); + } + + return field; +} + +bool from_jboolean_to_bool(jboolean val) +{ + bool result; + + if (val == JNI_TRUE) + { + result = true; + } + else + { + if (val == JNI_FALSE) + { + result = false; + } + else + { + throw std::invalid_argument("the jboolean value is not true/false\n"); + } + } + + return result; +} + +std::string from_jstring_to_string(JNIEnv *env, jstring str) +{ + jboolean is_copy = JNI_TRUE; + if (!str) { + throw std::invalid_argument("String should not be null"); + } + const char *str_chars = (char *)env->GetStringUTFChars(str, &is_copy); + if (!str_chars) { + throw std::bad_alloc(); + } + const std::string string_to_write = std::string(str_chars); + + env->ReleaseStringUTFChars(str, str_chars); + + return string_to_write; +} + +jobject get_bluetooth_type(JNIEnv *env, const char *field_name) +{ + jclass b_type_enum = search_class(env, JAVA_MAIN_PACKAGE "/BluetoothType"); + + jfieldID b_type_field = search_field(env, b_type_enum, field_name, "L" JAVA_MAIN_PACKAGE "/BluetoothType;", true); + + jobject result = env->GetStaticObjectField(b_type_enum, b_type_field); + env->DeleteLocalRef(b_type_enum); + return result; +} + +jobject get_new_arraylist(JNIEnv *env, unsigned int size, jmethodID *add) +{ + jclass arraylist_class = search_class(env, "Ljava/util/ArrayList;"); + jmethodID arraylist_ctor = search_method(env, arraylist_class, "<init>", "(I)V", false); + + jobject result = env->NewObject(arraylist_class, arraylist_ctor, size); + if (!result) + { + throw std::runtime_error("cannot create instance of class\n"); + } + + *add = search_method(env, arraylist_class, "add", "(Ljava/lang/Object;)Z", false); + + env->DeleteLocalRef(arraylist_class); + return result; +} + +void raise_java_exception(JNIEnv *env, std::exception &e) +{ + env->ThrowNew(env->FindClass("java/lang/Error"), e.what()); +} + +void raise_java_runtime_exception(JNIEnv *env, std::runtime_error &e) +{ + env->ThrowNew(env->FindClass("java/lang/RuntimeException"), e.what()); +} + +void raise_java_oom_exception(JNIEnv *env, std::bad_alloc &e) +{ + env->ThrowNew(env->FindClass("java/lang/OutOfMemoryException"), e.what()); +} + +void raise_java_invalid_arg_exception(JNIEnv *env, std::invalid_argument &e) +{ + env->ThrowNew(env->FindClass("java/lang/IllegalArgumentException"), e.what()); +} + + |