diff options
author | Sven Gothel <[email protected]> | 2020-05-28 19:23:48 +0200 |
---|---|---|
committer | Sven Gothel <[email protected]> | 2020-05-28 19:23:48 +0200 |
commit | a85b9cd5eff54558733770665d96f40f9562856d (patch) | |
tree | 0acc61708397cebeefaa0027b09ad55a39606c8a /java/jni/direct_bt/DBTDevice.cxx | |
parent | 94588c93be703c1e9ccf57b6c41c32b788c6f8a2 (diff) |
JNI Callback listener issued from a native thread must not mute exceptions (forward to java exception)
All JNI Callback listener which are issued from a native thread don't return to the JVM,
but its native caller thread.
Catching C++ exceptions and forwarding them to the JVM simply mutes the whole exception,
as it will never get checked and processed.
Therefor remove this 'exception muting'
in the JNI AdapterStatusListener and GATTCharacteristicListener instances.
Instead we decorate all listener/callback invocations with exception handling
to properly report this 'user exception' and allow the native thread to continue!
On the native side this has been completed for
AdapterStatusListener, GATTCharacteristicListener and MgmtAdapterEventCallback.
Hence all potential user exceptions covered in the native implementation.
Diffstat (limited to 'java/jni/direct_bt/DBTDevice.cxx')
-rw-r--r-- | java/jni/direct_bt/DBTDevice.cxx | 44 |
1 files changed, 18 insertions, 26 deletions
diff --git a/java/jni/direct_bt/DBTDevice.cxx b/java/jni/direct_bt/DBTDevice.cxx index 8ac344b5..2e727d52 100644 --- a/java/jni/direct_bt/DBTDevice.cxx +++ b/java/jni/direct_bt/DBTDevice.cxx @@ -102,44 +102,36 @@ class JNICharacteristicListener : public GATTCharacteristicListener { void notificationReceived(GATTCharacteristicRef charDecl, std::shared_ptr<TROOctets> charValue, const uint64_t timestamp) override { JNIEnv *env = *jni_env; - try { - JavaGlobalObj::check(charDecl->getJavaObject(), E_FILE_LINE); - jobject jCharDecl = JavaGlobalObj::GetObject(charDecl->getJavaObject()); + JavaGlobalObj::check(charDecl->getJavaObject(), E_FILE_LINE); + jobject jCharDecl = JavaGlobalObj::GetObject(charDecl->getJavaObject()); - const size_t value_size = charValue->getSize(); - jbyteArray jvalue = env->NewByteArray((jsize)value_size); - env->SetByteArrayRegion(jvalue, 0, (jsize)value_size, (const jbyte *)charValue->get_ptr()); - java_exception_check_and_throw(env, E_FILE_LINE); + const size_t value_size = charValue->getSize(); + jbyteArray jvalue = env->NewByteArray((jsize)value_size); + env->SetByteArrayRegion(jvalue, 0, (jsize)value_size, (const jbyte *)charValue->get_ptr()); + java_exception_check_and_throw(env, E_FILE_LINE); - env->CallVoidMethod(listenerObjRef->getObject(), mNotificationReceived, - jCharDecl, jvalue, (jlong)timestamp); - java_exception_check_and_throw(env, E_FILE_LINE); - } catch(...) { - rethrow_and_raise_java_exception(env); - } + env->CallVoidMethod(listenerObjRef->getObject(), mNotificationReceived, + jCharDecl, jvalue, (jlong)timestamp); + java_exception_check_and_throw(env, E_FILE_LINE); } void indicationReceived(GATTCharacteristicRef charDecl, std::shared_ptr<TROOctets> charValue, const uint64_t timestamp, const bool confirmationSent) override { JNIEnv *env = *jni_env; - try { - JavaGlobalObj::check(charDecl->getJavaObject(), E_FILE_LINE); - jobject jCharDecl = JavaGlobalObj::GetObject(charDecl->getJavaObject()); + JavaGlobalObj::check(charDecl->getJavaObject(), E_FILE_LINE); + jobject jCharDecl = JavaGlobalObj::GetObject(charDecl->getJavaObject()); - const size_t value_size = charValue->getSize(); - jbyteArray jvalue = env->NewByteArray((jsize)value_size); - env->SetByteArrayRegion(jvalue, 0, (jsize)value_size, (const jbyte *)charValue->get_ptr()); - java_exception_check_and_throw(env, E_FILE_LINE); + const size_t value_size = charValue->getSize(); + jbyteArray jvalue = env->NewByteArray((jsize)value_size); + env->SetByteArrayRegion(jvalue, 0, (jsize)value_size, (const jbyte *)charValue->get_ptr()); + java_exception_check_and_throw(env, E_FILE_LINE); - env->CallVoidMethod(listenerObjRef->getObject(), mIndicationReceived, - jCharDecl, jvalue, (jlong)timestamp, (jboolean)confirmationSent); - java_exception_check_and_throw(env, E_FILE_LINE); - } catch(...) { - rethrow_and_raise_java_exception(env); - } + env->CallVoidMethod(listenerObjRef->getObject(), mIndicationReceived, + jCharDecl, jvalue, (jlong)timestamp, (jboolean)confirmationSent); + java_exception_check_and_throw(env, E_FILE_LINE); } }; |