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 | |
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().
-rw-r--r-- | java/CMakeLists.txt | 1 | ||||
-rw-r--r-- | java/jni/BluetoothFactory.cxx | 51 | ||||
-rw-r--r-- | java/jni/DBusAdapter.cxx | 2 | ||||
-rw-r--r-- | java/jni/DBusDevice.cxx | 2 | ||||
-rw-r--r-- | java/jni/DBusGattCharacteristic.cxx | 2 | ||||
-rw-r--r-- | java/jni/DBusGattDescriptor.cxx | 2 | ||||
-rw-r--r-- | java/jni/DBusGattService.cxx | 2 | ||||
-rw-r--r-- | java/jni/DBusManager.cxx | 2 | ||||
-rw-r--r-- | java/jni/DBusObject.cxx | 2 | ||||
-rw-r--r-- | java/jni/helper_base.cxx (renamed from java/jni/helper.cxx) | 82 | ||||
-rw-r--r-- | java/jni/helper_base.hpp (renamed from java/jni/helper.hpp) | 11 | ||||
-rw-r--r-- | java/jni/helper_tinyb.cxx | 109 | ||||
-rw-r--r-- | java/jni/helper_tinyb.hpp | 39 | ||||
-rw-r--r-- | java/org/tinyb/BluetoothFactory.java | 2 |
14 files changed, 220 insertions, 89 deletions
diff --git a/java/CMakeLists.txt b/java/CMakeLists.txt index 4a17f8c5..0d7f80fb 100644 --- a/java/CMakeLists.txt +++ b/java/CMakeLists.txt @@ -27,6 +27,7 @@ set(JAVA_CLASSES org.tinyb.BluetoothAdapter org.tinyb.BluetoothCallback org.tinyb.BluetoothDevice org.tinyb.BluetoothEvent + org.tinyb.BluetoothFactory org.tinyb.BluetoothGattCharacteristic org.tinyb.BluetoothGattService org.tinyb.BluetoothGattDescriptor diff --git a/java/jni/BluetoothFactory.cxx b/java/jni/BluetoothFactory.cxx new file mode 100644 index 00000000..c8a61b6f --- /dev/null +++ b/java/jni/BluetoothFactory.cxx @@ -0,0 +1,51 @@ +/* + * 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 "org_tinyb_BluetoothFactory.h" + +#include "version.h" + +#include "JNIMem.hpp" +#include "helper_base.hpp" + +jstring Java_org_tinyb_BluetoothFactory_getNativeAPIVersion(JNIEnv *env, jclass clazz) +{ + try { + (void) clazz; + + std::string api_version = std::string(gVERSION_API); + return env->NewStringUTF(api_version.c_str()); + } catch (std::bad_alloc &e) { + raise_java_oom_exception(env, e); + } catch (std::runtime_error &e) { + raise_java_runtime_exception(env, e); + } catch (std::invalid_argument &e) { + raise_java_invalid_arg_exception(env, e); + } catch (std::exception &e) { + raise_java_exception(env, e); + } + return nullptr; +} + diff --git a/java/jni/DBusAdapter.cxx b/java/jni/DBusAdapter.cxx index 0c73a94f..9f43c4b8 100644 --- a/java/jni/DBusAdapter.cxx +++ b/java/jni/DBusAdapter.cxx @@ -29,7 +29,7 @@ #include "tinyb_dbus_DBusAdapter.h" #include "JNIMem.hpp" -#include "helper.hpp" +#include "helper_tinyb.hpp" using namespace tinyb; diff --git a/java/jni/DBusDevice.cxx b/java/jni/DBusDevice.cxx index 896f1544..6a833356 100644 --- a/java/jni/DBusDevice.cxx +++ b/java/jni/DBusDevice.cxx @@ -30,7 +30,7 @@ #include "tinyb_dbus_DBusDevice.h" #include "JNIMem.hpp" -#include "helper.hpp" +#include "helper_tinyb.hpp" using namespace tinyb; diff --git a/java/jni/DBusGattCharacteristic.cxx b/java/jni/DBusGattCharacteristic.cxx index a3e9428f..43ac754f 100644 --- a/java/jni/DBusGattCharacteristic.cxx +++ b/java/jni/DBusGattCharacteristic.cxx @@ -30,7 +30,7 @@ #include "tinyb_dbus_DBusGattCharacteristic.h" #include "JNIMem.hpp" -#include "helper.hpp" +#include "helper_tinyb.hpp" using namespace tinyb; diff --git a/java/jni/DBusGattDescriptor.cxx b/java/jni/DBusGattDescriptor.cxx index bdb83fc0..2118205c 100644 --- a/java/jni/DBusGattDescriptor.cxx +++ b/java/jni/DBusGattDescriptor.cxx @@ -29,7 +29,7 @@ #include "tinyb_dbus_DBusGattDescriptor.h" #include "JNIMem.hpp" -#include "helper.hpp" +#include "helper_tinyb.hpp" using namespace tinyb; diff --git a/java/jni/DBusGattService.cxx b/java/jni/DBusGattService.cxx index 6ac0963f..0ab089f2 100644 --- a/java/jni/DBusGattService.cxx +++ b/java/jni/DBusGattService.cxx @@ -29,7 +29,7 @@ #include "tinyb_dbus_DBusGattService.h" -#include "helper.hpp" +#include "helper_tinyb.hpp" using namespace tinyb; diff --git a/java/jni/DBusManager.cxx b/java/jni/DBusManager.cxx index 85f7fd75..1680e866 100644 --- a/java/jni/DBusManager.cxx +++ b/java/jni/DBusManager.cxx @@ -29,7 +29,7 @@ #include "tinyb_dbus_DBusManager.h" -#include "helper.hpp" +#include "helper_tinyb.hpp" using namespace tinyb; diff --git a/java/jni/DBusObject.cxx b/java/jni/DBusObject.cxx index 623ab26e..8e3b93a2 100644 --- a/java/jni/DBusObject.cxx +++ b/java/jni/DBusObject.cxx @@ -26,7 +26,7 @@ #include "tinyb_dbus_DBusObject.h" -#include "helper.hpp" +#include "helper_tinyb.hpp" using namespace tinyb; diff --git a/java/jni/helper.cxx b/java/jni/helper_base.cxx index dda3c136..2c0c23e6 100644 --- a/java/jni/helper.cxx +++ b/java/jni/helper_base.cxx @@ -2,6 +2,10 @@ * 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 @@ -27,7 +31,7 @@ #include <stdexcept> #include <vector> -#include "helper.hpp" +#include "helper_tinyb.hpp" jfieldID getInstanceField(JNIEnv *env, jobject obj) { @@ -36,11 +40,6 @@ jfieldID getInstanceField(JNIEnv *env, jobject obj) return env->GetFieldID(clazz, "nativeInstance", "J"); } -jclass search_class(JNIEnv *env, tinyb::BluetoothObject &object) -{ - return search_class(env, object.get_java_class().c_str()); -} - jclass search_class(JNIEnv *env, const char *clazz_name) { jclass clazz = env->FindClass(clazz_name); @@ -145,70 +144,6 @@ std::string from_jstring_to_string(JNIEnv *env, jstring str) return string_to_write; } -tinyb::BluetoothType from_int_to_btype(int type) -{ - tinyb::BluetoothType result = tinyb::BluetoothType::NONE; - - switch (type) - { - case 0: - result = tinyb::BluetoothType::NONE; - break; - - case 1: - result = tinyb::BluetoothType::ADAPTER; - break; - - case 2: - result = tinyb::BluetoothType::DEVICE; - break; - - case 3: - result = tinyb::BluetoothType::GATT_SERVICE; - break; - - case 4: - result = tinyb::BluetoothType::GATT_CHARACTERISTIC; - break; - - case 5: - result = tinyb::BluetoothType::GATT_CHARACTERISTIC; - break; - - default: - result = tinyb::BluetoothType::NONE; - break; - } - - return result; -} - -tinyb::TransportType from_int_to_transport_type(int type) -{ - tinyb::TransportType result = tinyb::TransportType::AUTO; - - switch (type) - { - case 0: - result = tinyb::TransportType::AUTO; - break; - - case 1: - result = tinyb::TransportType::BREDR; - break; - - case 2: - result = tinyb::TransportType::LE; - break; - - default: - result = tinyb::TransportType::AUTO; - break; - } - - return result; -} - jobject get_bluetooth_type(JNIEnv *env, const char *field_name) { jclass b_type_enum = search_class(env, JAVA_MAIN_PACKAGE "/BluetoothType"); @@ -247,12 +182,6 @@ void raise_java_runtime_exception(JNIEnv *env, std::runtime_error &e) env->ThrowNew(env->FindClass("java/lang/RuntimeException"), e.what()); } -void raise_java_bluetooth_exception(JNIEnv *env, tinyb::BluetoothException &e) -{ - env->ThrowNew(env->FindClass("tinyb/BluetoothException"), e.what()); -} - - void raise_java_oom_exception(JNIEnv *env, std::bad_alloc &e) { env->ThrowNew(env->FindClass("java/lang/OutOfMemoryException"), e.what()); @@ -263,3 +192,4 @@ void raise_java_invalid_arg_exception(JNIEnv *env, std::invalid_argument &e) env->ThrowNew(env->FindClass("java/lang/IllegalArgumentException"), e.what()); } + diff --git a/java/jni/helper.hpp b/java/jni/helper_base.hpp index 2f297734..153faa9b 100644 --- a/java/jni/helper.hpp +++ b/java/jni/helper_base.hpp @@ -2,6 +2,10 @@ * 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 @@ -25,13 +29,11 @@ #pragma once #include <vector> -#include "tinyb/BluetoothObject.hpp" -#include "tinyb/BluetoothException.hpp" +#include <memory> jfieldID getInstanceField(JNIEnv *env, jobject obj); jclass search_class(JNIEnv *env, const char *clazz_name); -jclass search_class(JNIEnv *env, tinyb::BluetoothObject &object); jclass search_class(JNIEnv *env, jobject obj); jmethodID search_method(JNIEnv *env, jclass clazz, const char *method_name, const char *prototype, bool is_static); @@ -39,10 +41,8 @@ jfieldID search_field(JNIEnv *env, jclass clazz, const char *field_name, const char *type, bool is_static); bool from_jboolean_to_bool(jboolean val); std::string from_jstring_to_string(JNIEnv *env, jstring str); -tinyb::BluetoothType from_int_to_btype(int type); jobject get_bluetooth_type(JNIEnv *env, const char *field_name); jobject get_new_arraylist(JNIEnv *env, unsigned int size, jmethodID *add); -tinyb::TransportType from_int_to_transport_type(int type); template <typename T> T *getInstance(JNIEnv *env, jobject obj) @@ -113,6 +113,5 @@ jobject convert_vector_to_jobject(JNIEnv *env, std::vector<std::unique_ptr<T>>& void raise_java_exception(JNIEnv *env, std::exception &e); void raise_java_runtime_exception(JNIEnv *env, std::runtime_error &e); -void raise_java_bluetooth_exception(JNIEnv *env, tinyb::BluetoothException &e); void raise_java_oom_exception(JNIEnv *env, std::bad_alloc &e); void raise_java_invalid_arg_exception(JNIEnv *env, std::invalid_argument &e); diff --git a/java/jni/helper_tinyb.cxx b/java/jni/helper_tinyb.cxx new file mode 100644 index 00000000..865d55e5 --- /dev/null +++ b/java/jni/helper_tinyb.cxx @@ -0,0 +1,109 @@ +/* + * 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" + +jclass search_class(JNIEnv *env, tinyb::BluetoothObject &object) +{ + return search_class(env, object.get_java_class().c_str()); +} + +tinyb::BluetoothType from_int_to_btype(int type) +{ + tinyb::BluetoothType result = tinyb::BluetoothType::NONE; + + switch (type) + { + case 0: + result = tinyb::BluetoothType::NONE; + break; + + case 1: + result = tinyb::BluetoothType::ADAPTER; + break; + + case 2: + result = tinyb::BluetoothType::DEVICE; + break; + + case 3: + result = tinyb::BluetoothType::GATT_SERVICE; + break; + + case 4: + result = tinyb::BluetoothType::GATT_CHARACTERISTIC; + break; + + case 5: + result = tinyb::BluetoothType::GATT_CHARACTERISTIC; + break; + + default: + result = tinyb::BluetoothType::NONE; + break; + } + + return result; +} + +tinyb::TransportType from_int_to_transport_type(int type) +{ + tinyb::TransportType result = tinyb::TransportType::AUTO; + + switch (type) + { + case 0: + result = tinyb::TransportType::AUTO; + break; + + case 1: + result = tinyb::TransportType::BREDR; + break; + + case 2: + result = tinyb::TransportType::LE; + break; + + default: + result = tinyb::TransportType::AUTO; + break; + } + + return result; +} + +void raise_java_bluetooth_exception(JNIEnv *env, tinyb::BluetoothException &e) +{ + env->ThrowNew(env->FindClass("org/tinyb/BluetoothException"), e.what()); +} + diff --git a/java/jni/helper_tinyb.hpp b/java/jni/helper_tinyb.hpp new file mode 100644 index 00000000..f1873a7f --- /dev/null +++ b/java/jni/helper_tinyb.hpp @@ -0,0 +1,39 @@ +/* + * 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. + */ + +#pragma once + +#include "helper_base.hpp" +#include "tinyb/BluetoothObject.hpp" +#include "tinyb/BluetoothException.hpp" + +jclass search_class(JNIEnv *env, tinyb::BluetoothObject &object); +tinyb::BluetoothType from_int_to_btype(int type); +tinyb::TransportType from_int_to_transport_type(int type); + +void raise_java_bluetooth_exception(JNIEnv *env, tinyb::BluetoothException &e); diff --git a/java/org/tinyb/BluetoothFactory.java b/java/org/tinyb/BluetoothFactory.java index f54ffe14..b2b90384 100644 --- a/java/org/tinyb/BluetoothFactory.java +++ b/java/org/tinyb/BluetoothFactory.java @@ -104,4 +104,6 @@ public class BluetoothFactory { { return tinyb.dbus.DBusManager.getBluetoothManager(); } + + private native static String getNativeAPIVersion(); } |