diff options
author | Sven Gothel <[email protected]> | 2022-05-09 01:52:39 +0200 |
---|---|---|
committer | Sven Gothel <[email protected]> | 2022-05-09 01:52:39 +0200 |
commit | aa96b3082f6ac446cea6f68f0cf4c3513fa64e79 (patch) | |
tree | 8cfae38288561373334d0f1a4c181397e5f1d9bc | |
parent | 57c8e5916dd8b83fc4fb720aee417a138a0d9e19 (diff) |
JNI Lifecycle Fix: Make Java/Native object relation inherently thread safe by using shared_ptr_ref<T>
-rw-r--r-- | java/jni/direct_bt/DBGattServer.cxx | 190 | ||||
-rw-r--r-- | java/jni/direct_bt/DBTAdapter.cxx | 253 | ||||
-rw-r--r-- | java/jni/direct_bt/DBTDevice.cxx | 368 | ||||
-rw-r--r-- | java/jni/direct_bt/DBTGattChar.cxx | 56 | ||||
-rw-r--r-- | java/jni/direct_bt/DBTGattDesc.cxx | 28 | ||||
-rw-r--r-- | java/jni/direct_bt/DBTGattService.cxx | 40 | ||||
-rw-r--r-- | java/jni/direct_bt/DBTManager.cxx | 45 | ||||
-rw-r--r-- | java/jni/direct_bt/EInfoReport.cxx | 197 |
8 files changed, 657 insertions, 520 deletions
diff --git a/java/jni/direct_bt/DBGattServer.cxx b/java/jni/direct_bt/DBGattServer.cxx index 9975cef5..f6719748 100644 --- a/java/jni/direct_bt/DBGattServer.cxx +++ b/java/jni/direct_bt/DBGattServer.cxx @@ -51,7 +51,7 @@ using namespace direct_bt; static const std::string _dbGattValueClazzName("org/direct_bt/DBGattValue"); static const std::string _dbGattValueClazzCtorArgs("([BIZ)V"); -static jobject _createDBGattValueFromDesc(JNIEnv *env_, jclass clazz, jmethodID clazz_ctor, DBGattDesc* valueHolder) { +static jobject _createDBGattValueFromDesc(JNIEnv *env_, jclass clazz, jmethodID clazz_ctor, const DBGattDescRef& valueHolder) { const jau::POctets& value = valueHolder->getValue(); jbyteArray jval = env_->NewByteArray(value.size()); env_->SetByteArrayRegion(jval, 0, value.size(), (const jbyte*)(value.get_ptr())); @@ -65,7 +65,7 @@ static jobject _createDBGattValueFromDesc(JNIEnv *env_, jclass clazz, jmethodID return jDBGattValue; }; -static jobject _createDBGattValueFromChar(JNIEnv *env_, jclass clazz, jmethodID clazz_ctor, DBGattChar* valueHolder) { +static jobject _createDBGattValueFromChar(JNIEnv *env_, jclass clazz, jmethodID clazz_ctor, const DBGattCharRef& valueHolder) { const jau::POctets& value = valueHolder->getValue(); jbyteArray jval = env_->NewByteArray(value.size()); env_->SetByteArrayRegion(jval, 0, value.size(), (const jbyte*)(value.get_ptr())); @@ -87,9 +87,9 @@ static jobject _createDBGattValueFromChar(JNIEnv *env_, jclass clazz, jmethodID jobject Java_org_direct_1bt_DBGattDesc_getValue(JNIEnv *env, jobject obj) { try { - std::shared_ptr<DBGattDesc> * ref_ptr = jau::getInstance<std::shared_ptr<DBGattDesc>>(env, obj); + jau::shared_ptr_ref<DBGattDesc> ref(env, obj); // hold until done jclass clazz = jau::search_class(env, _dbGattValueClazzName.c_str()); - return jau::convert_instance_to_jobject<DBGattDesc>(env, clazz, _dbGattValueClazzCtorArgs.c_str(), _createDBGattValueFromDesc, ref_ptr->get()); + return jau::convert_instance_to_jobject<DBGattDesc>(env, clazz, _dbGattValueClazzCtorArgs.c_str(), _createDBGattValueFromDesc, ref.shared_ptr()); } catch(...) { rethrow_and_raise_java_exception(env); } @@ -98,7 +98,7 @@ jobject Java_org_direct_1bt_DBGattDesc_getValue(JNIEnv *env, jobject obj) { jboolean Java_org_direct_1bt_DBGattDesc_setValue(JNIEnv *env, jobject obj, jbyteArray jsource, jint jsource_pos, jint jsource_len, jint jdest_pos) { try { - std::shared_ptr<DBGattDesc>& ref = *jau::getInstance<std::shared_ptr<DBGattDesc>>(env, obj); + jau::shared_ptr_ref<DBGattDesc> ref(env, obj); // hold until done if( nullptr == jsource ) { return JNI_FALSE; @@ -154,14 +154,12 @@ jlong Java_org_direct_1bt_DBGattDesc_ctorImpl(JNIEnv *env, jobject obj, std::shared_ptr<const jau::uuid_t> type( jau::uuid_t::create(stype) ); // new instance - std::shared_ptr<DBGattDesc> ref = std::make_shared<DBGattDesc>( - type, std::move(value), JNI_TRUE == jvariable_length_); + jau::shared_ptr_ref<DBGattDesc> ref( new DBGattDesc(type, std::move(value), JNI_TRUE == jvariable_length_) ); ref->setJavaObject( std::make_shared<jau::JavaGlobalObj>( std::move(global_obj), nullptr ) ); jau::JavaGlobalObj::check(ref->getJavaObject(), E_FILE_LINE); - std::shared_ptr<DBGattDesc> * ref_ptr = new std::shared_ptr<DBGattDesc>(std::move(ref)); - return (jlong)(intptr_t)ref_ptr; + return ref.release_to_jlong(); } catch(...) { rethrow_and_raise_java_exception(env); } @@ -171,10 +169,15 @@ jlong Java_org_direct_1bt_DBGattDesc_ctorImpl(JNIEnv *env, jobject obj, void Java_org_direct_1bt_DBGattDesc_dtorImpl(JNIEnv *env, jclass clazz, jlong nativeInstance) { (void)clazz; try { - if( 0 != nativeInstance ) { - std::shared_ptr<DBGattDesc> * ref_ptr = reinterpret_cast<std::shared_ptr<DBGattDesc> *>(nativeInstance); - (*ref_ptr)->setJavaObject(); - delete ref_ptr; + jau::shared_ptr_ref<DBGattDesc> sref(nativeInstance, false /* throw_on_nullptr */); // hold copy until done + if( nullptr != sref.pointer() ) { + if( !sref.is_null() ) { + jau::JavaAnonRef sref_java = sref->getJavaObject(); // hold until done! + jau::JavaGlobalObj::check(sref_java, E_FILE_LINE); + sref->setJavaObject(); + } + std::shared_ptr<DBGattDesc>* sref_ptr = jau::castInstance<DBGattDesc>(nativeInstance); + delete sref_ptr; } } catch(...) { rethrow_and_raise_java_exception(env); @@ -184,8 +187,8 @@ void Java_org_direct_1bt_DBGattDesc_dtorImpl(JNIEnv *env, jclass clazz, jlong na jshort Java_org_direct_1bt_DBGattDesc_getHandle(JNIEnv *env, jobject obj) { try { - std::shared_ptr<DBGattDesc> * ref_ptr = jau::getInstance<std::shared_ptr<DBGattDesc>>(env, obj); - return (jshort)( (*ref_ptr)->getHandle() ); + jau::shared_ptr_ref<DBGattDesc> ref(env, obj); // hold until done + return (jshort)( ref->getHandle() ); } catch(...) { rethrow_and_raise_java_exception(env); } @@ -196,8 +199,8 @@ jshort Java_org_direct_1bt_DBGattDesc_getHandle(JNIEnv *env, jobject obj) void Java_org_direct_1bt_DBGattDesc_bzero(JNIEnv *env, jobject obj) { try { - std::shared_ptr<DBGattDesc> * ref_ptr = jau::getInstance<std::shared_ptr<DBGattDesc>>(env, obj); - (*ref_ptr)->bzero(); + jau::shared_ptr_ref<DBGattDesc> ref(env, obj); // hold until done + ref->bzero(); } catch(...) { rethrow_and_raise_java_exception(env); } @@ -205,8 +208,8 @@ void Java_org_direct_1bt_DBGattDesc_bzero(JNIEnv *env, jobject obj) jstring Java_org_direct_1bt_DBGattDesc_toString(JNIEnv *env, jobject obj) { try { - std::shared_ptr<DBGattDesc> * ref_ptr = jau::getInstance<std::shared_ptr<DBGattDesc>>(env, obj); - return jau::from_string_to_jstring(env, (*ref_ptr)->toString()); + jau::shared_ptr_ref<DBGattDesc> ref(env, obj); // hold until done + return jau::from_string_to_jstring(env, ref->toString()); } catch(...) { rethrow_and_raise_java_exception(env); } @@ -222,9 +225,9 @@ jstring Java_org_direct_1bt_DBGattDesc_toString(JNIEnv *env, jobject obj) { jobject Java_org_direct_1bt_DBGattChar_getValue(JNIEnv *env, jobject obj) { try { - std::shared_ptr<DBGattChar> * ref_ptr = jau::getInstance<std::shared_ptr<DBGattChar>>(env, obj); + jau::shared_ptr_ref<DBGattChar> ref(env, obj); // hold until done jclass clazz = jau::search_class(env, _dbGattValueClazzName.c_str()); - return jau::convert_instance_to_jobject<DBGattChar>(env, clazz, _dbGattValueClazzCtorArgs.c_str(), _createDBGattValueFromChar, ref_ptr->get()); + return jau::convert_instance_to_jobject<DBGattChar>(env, clazz, _dbGattValueClazzCtorArgs.c_str(), _createDBGattValueFromChar, ref.shared_ptr()); } catch(...) { rethrow_and_raise_java_exception(env); } @@ -233,7 +236,7 @@ jobject Java_org_direct_1bt_DBGattChar_getValue(JNIEnv *env, jobject obj) { jboolean Java_org_direct_1bt_DBGattChar_setValue(JNIEnv *env, jobject obj, jbyteArray jsource, jint jsource_pos, jint jsource_len, jint jdest_pos) { try { - std::shared_ptr<DBGattChar>& ref = *jau::getInstance<std::shared_ptr<DBGattChar>>(env, obj); + jau::shared_ptr_ref<DBGattChar> ref(env, obj); // hold until done if( nullptr == jsource ) { return JNI_FALSE; @@ -299,8 +302,8 @@ jlong Java_org_direct_1bt_DBGattChar_ctorImpl(JNIEnv *env, jobject obj, throw jau::InternalError("GetPrimitiveArrayCritical(DBGattDesc* array) is null", E_FILE_LINE); } for(jau::nsize_t i=0; i < count; ++i) { - std::shared_ptr<DBGattDesc> *desc_ref = (std::shared_ptr<DBGattDesc> *) (intptr_t) jlong_desc_ref_array[i]; - descriptors.push_back( *desc_ref ); + std::shared_ptr<DBGattDesc> desc_ref = *( (std::shared_ptr<DBGattDesc> *) (intptr_t) jlong_desc_ref_array[i] ); + descriptors.push_back( desc_ref ); } } @@ -312,16 +315,14 @@ jlong Java_org_direct_1bt_DBGattChar_ctorImpl(JNIEnv *env, jobject obj, std::shared_ptr<const jau::uuid_t> type( jau::uuid_t::create(stype) ); // new instance - std::shared_ptr<DBGattChar> ref = std::make_shared<DBGattChar>( - type, properties, - std::move(descriptors), - std::move(value), JNI_TRUE == jvariable_length_); + jau::shared_ptr_ref<DBGattChar> ref( new DBGattChar(type, properties, + std::move(descriptors), + std::move(value), JNI_TRUE == jvariable_length_) ); ref->setJavaObject( std::make_shared<jau::JavaGlobalObj>( std::move(global_obj), nullptr ) ); jau::JavaGlobalObj::check(ref->getJavaObject(), E_FILE_LINE); - std::shared_ptr<DBGattChar> * ref_ptr = new std::shared_ptr<DBGattChar>(std::move(ref)); - return (jlong) (intptr_t) ref_ptr; + return ref.release_to_jlong(); } catch(...) { rethrow_and_raise_java_exception(env); } @@ -331,10 +332,15 @@ jlong Java_org_direct_1bt_DBGattChar_ctorImpl(JNIEnv *env, jobject obj, void Java_org_direct_1bt_DBGattChar_dtorImpl(JNIEnv *env, jclass clazz, jlong nativeInstance) { (void)clazz; try { - if( 0 != nativeInstance ) { - std::shared_ptr<DBGattChar> * ref_ptr = reinterpret_cast<std::shared_ptr<DBGattChar> *>(nativeInstance); - (*ref_ptr)->setJavaObject(); - delete ref_ptr; + jau::shared_ptr_ref<BTGattChar> sref(nativeInstance, false /* throw_on_nullptr */); // hold copy until done + if( nullptr != sref.pointer() ) { + if( !sref.is_null() ) { + jau::JavaAnonRef sref_java = sref->getJavaObject(); // hold until done! + jau::JavaGlobalObj::check(sref_java, E_FILE_LINE); + sref->setJavaObject(); + } + std::shared_ptr<BTGattChar>* sref_ptr = jau::castInstance<BTGattChar>(nativeInstance); + delete sref_ptr; } } catch(...) { rethrow_and_raise_java_exception(env); @@ -344,8 +350,8 @@ void Java_org_direct_1bt_DBGattChar_dtorImpl(JNIEnv *env, jclass clazz, jlong na jshort Java_org_direct_1bt_DBGattChar_getHandle(JNIEnv *env, jobject obj) { try { - std::shared_ptr<DBGattChar> * ref_ptr = jau::getInstance<std::shared_ptr<DBGattChar>>(env, obj); - return (jshort)( (*ref_ptr)->getHandle() ); + jau::shared_ptr_ref<DBGattChar> ref(env, obj); // hold until done + return (jshort)( ref->getHandle() ); } catch(...) { rethrow_and_raise_java_exception(env); } @@ -355,8 +361,8 @@ jshort Java_org_direct_1bt_DBGattChar_getHandle(JNIEnv *env, jobject obj) jshort Java_org_direct_1bt_DBGattChar_getEndHandle(JNIEnv *env, jobject obj) { try { - std::shared_ptr<DBGattChar> * ref_ptr = jau::getInstance<std::shared_ptr<DBGattChar>>(env, obj); - return (jshort)( (*ref_ptr)->getEndHandle() ); + jau::shared_ptr_ref<DBGattChar> ref(env, obj); // hold until done + return (jshort)( ref->getEndHandle() ); } catch(...) { rethrow_and_raise_java_exception(env); } @@ -366,8 +372,8 @@ jshort Java_org_direct_1bt_DBGattChar_getEndHandle(JNIEnv *env, jobject obj) jshort Java_org_direct_1bt_DBGattChar_getValueHandle(JNIEnv *env, jobject obj) { try { - std::shared_ptr<DBGattChar> * ref_ptr = jau::getInstance<std::shared_ptr<DBGattChar>>(env, obj); - return (jshort)( (*ref_ptr)->getValueHandle() ); + jau::shared_ptr_ref<DBGattChar> ref(env, obj); // hold until done + return (jshort)( ref->getValueHandle() ); } catch(...) { rethrow_and_raise_java_exception(env); } @@ -378,8 +384,8 @@ jshort Java_org_direct_1bt_DBGattChar_getValueHandle(JNIEnv *env, jobject obj) void Java_org_direct_1bt_DBGattChar_bzero(JNIEnv *env, jobject obj) { try { - std::shared_ptr<DBGattChar> * ref_ptr = jau::getInstance<std::shared_ptr<DBGattChar>>(env, obj); - (*ref_ptr)->bzero(); + jau::shared_ptr_ref<DBGattChar> ref(env, obj); // hold until done + ref->bzero(); } catch(...) { rethrow_and_raise_java_exception(env); } @@ -387,8 +393,8 @@ void Java_org_direct_1bt_DBGattChar_bzero(JNIEnv *env, jobject obj) jstring Java_org_direct_1bt_DBGattChar_toString(JNIEnv *env, jobject obj) { try { - std::shared_ptr<DBGattChar> * ref_ptr = jau::getInstance<std::shared_ptr<DBGattChar>>(env, obj); - return jau::from_string_to_jstring(env, (*ref_ptr)->toString()); + jau::shared_ptr_ref<DBGattChar> ref(env, obj); // hold until done + return jau::from_string_to_jstring(env, ref->toString()); } catch(...) { rethrow_and_raise_java_exception(env); } @@ -424,8 +430,8 @@ jlong Java_org_direct_1bt_DBGattService_ctorImpl(JNIEnv *env, jobject obj, throw jau::InternalError("GetPrimitiveArrayCritical(DBGattChar* array) is null", E_FILE_LINE); } for(jau::nsize_t i=0; i < count; ++i) { - std::shared_ptr<DBGattChar> *char_ref = (std::shared_ptr<DBGattChar> *) (intptr_t) jlong_char_ref_array[i]; - characteristics.push_back( *char_ref ); + std::shared_ptr<DBGattChar> char_ref = *( (std::shared_ptr<DBGattChar> *) (intptr_t) jlong_char_ref_array[i] ); + characteristics.push_back( char_ref ); } } @@ -434,15 +440,12 @@ jlong Java_org_direct_1bt_DBGattService_ctorImpl(JNIEnv *env, jobject obj, std::shared_ptr<const jau::uuid_t> type( jau::uuid_t::create(stype) ); // new instance - std::shared_ptr<DBGattService> ref = std::make_shared<DBGattService>( - JNI_TRUE == jprimary, type, - std::move(characteristics)); + jau::shared_ptr_ref<DBGattService> ref( new DBGattService( JNI_TRUE == jprimary, type, std::move(characteristics) ) ); ref->setJavaObject( std::make_shared<jau::JavaGlobalObj>( std::move(global_obj), nullptr ) ); jau::JavaGlobalObj::check(ref->getJavaObject(), E_FILE_LINE); - std::shared_ptr<DBGattService> * ref_ptr = new std::shared_ptr<DBGattService>(std::move(ref)); - return (jlong) (intptr_t) ref_ptr; + return ref.release_to_jlong(); } catch(...) { rethrow_and_raise_java_exception(env); } @@ -452,10 +455,15 @@ jlong Java_org_direct_1bt_DBGattService_ctorImpl(JNIEnv *env, jobject obj, void Java_org_direct_1bt_DBGattService_dtorImpl(JNIEnv *env, jclass clazz, jlong nativeInstance) { (void)clazz; try { - if( 0 != nativeInstance ) { - std::shared_ptr<DBGattService> * ref_ptr = reinterpret_cast<std::shared_ptr<DBGattService> *>(nativeInstance); - (*ref_ptr)->setJavaObject(); - delete ref_ptr; + jau::shared_ptr_ref<DBGattService> sref(nativeInstance, false /* throw_on_nullptr */); // hold copy until done + if( nullptr != sref.pointer() ) { + if( !sref.is_null() ) { + jau::JavaAnonRef sref_java = sref->getJavaObject(); // hold until done! + jau::JavaGlobalObj::check(sref_java, E_FILE_LINE); + sref->setJavaObject(); + } + std::shared_ptr<DBGattService>* sref_ptr = jau::castInstance<DBGattService>(nativeInstance); + delete sref_ptr; } } catch(...) { rethrow_and_raise_java_exception(env); @@ -465,8 +473,8 @@ void Java_org_direct_1bt_DBGattService_dtorImpl(JNIEnv *env, jclass clazz, jlong jshort Java_org_direct_1bt_DBGattService_getHandle(JNIEnv *env, jobject obj) { try { - std::shared_ptr<DBGattService> * ref_ptr = jau::getInstance<std::shared_ptr<DBGattService>>(env, obj); - return (jshort)( (*ref_ptr)->getHandle() ); + jau::shared_ptr_ref<DBGattService> ref(env, obj); // hold until done + return (jshort)( ref->getHandle() ); } catch(...) { rethrow_and_raise_java_exception(env); } @@ -476,8 +484,8 @@ jshort Java_org_direct_1bt_DBGattService_getHandle(JNIEnv *env, jobject obj) jshort Java_org_direct_1bt_DBGattService_getEndHandle(JNIEnv *env, jobject obj) { try { - std::shared_ptr<DBGattService> * ref_ptr = jau::getInstance<std::shared_ptr<DBGattService>>(env, obj); - return (jshort)( (*ref_ptr)->getEndHandle() ); + jau::shared_ptr_ref<DBGattService> ref(env, obj); // hold until done + return (jshort)( ref->getEndHandle() ); } catch(...) { rethrow_and_raise_java_exception(env); } @@ -486,8 +494,8 @@ jshort Java_org_direct_1bt_DBGattService_getEndHandle(JNIEnv *env, jobject obj) jstring Java_org_direct_1bt_DBGattService_toString(JNIEnv *env, jobject obj) { try { - std::shared_ptr<DBGattService> * ref_ptr = jau::getInstance<std::shared_ptr<DBGattService>>(env, obj); - return jau::from_string_to_jstring(env, (*ref_ptr)->toString()); + jau::shared_ptr_ref<DBGattService> ref(env, obj); // hold until done + return jau::from_string_to_jstring(env, ref->toString()); } catch(...) { rethrow_and_raise_java_exception(env); } @@ -522,21 +530,18 @@ jlong Java_org_direct_1bt_DBGattServer_ctorImpl(JNIEnv *env, jobject obj, throw jau::InternalError("GetPrimitiveArrayCritical(DBGattService* array) is null", E_FILE_LINE); } for(jau::nsize_t i=0; i < count; ++i) { - std::shared_ptr<DBGattService> *service_ref = (std::shared_ptr<DBGattService> *) (intptr_t) jlong_service_ref_array[i]; - services.push_back( *service_ref ); + std::shared_ptr<DBGattService> service_ref = *( (std::shared_ptr<DBGattService> *) (intptr_t) jlong_service_ref_array[i] ); + services.push_back( service_ref ); } } // new instance - std::shared_ptr<DBGattServer> ref = std::make_shared<DBGattServer>( - jmax_att_mtu, - std::move(services)); + jau::shared_ptr_ref<DBGattServer> ref( new DBGattServer( jmax_att_mtu, std::move(services) ) ); ref->setJavaObject( std::make_shared<jau::JavaGlobalObj>( std::move(global_obj), nullptr ) ); jau::JavaGlobalObj::check(ref->getJavaObject(), E_FILE_LINE); - std::shared_ptr<DBGattServer> * ref_ptr = new std::shared_ptr<DBGattServer>(std::move(ref)); - return (jlong) (intptr_t) ref_ptr; + return ref.release_to_jlong(); } catch(...) { rethrow_and_raise_java_exception(env); } @@ -546,10 +551,15 @@ jlong Java_org_direct_1bt_DBGattServer_ctorImpl(JNIEnv *env, jobject obj, void Java_org_direct_1bt_DBGattServer_dtorImpl(JNIEnv *env, jclass clazz, jlong nativeInstance) { (void)clazz; try { - if( 0 != nativeInstance ) { - std::shared_ptr<DBGattServer> * ref_ptr = reinterpret_cast<std::shared_ptr<DBGattServer> *>(nativeInstance); - (*ref_ptr)->setJavaObject(); - delete ref_ptr; + jau::shared_ptr_ref<DBGattServer> sref(nativeInstance, false /* throw_on_nullptr */); // hold copy until done + if( nullptr != sref.pointer() ) { + if( !sref.is_null() ) { + jau::JavaAnonRef sref_java = sref->getJavaObject(); // hold until done! + jau::JavaGlobalObj::check(sref_java, E_FILE_LINE); + sref->setJavaObject(); + } + std::shared_ptr<DBGattServer>* sref_ptr = jau::castInstance<DBGattServer>(nativeInstance); + delete sref_ptr; } } catch(...) { rethrow_and_raise_java_exception(env); @@ -559,8 +569,8 @@ void Java_org_direct_1bt_DBGattServer_dtorImpl(JNIEnv *env, jclass clazz, jlong jint Java_org_direct_1bt_DBGattServer_getMaxAttMTU(JNIEnv *env, jobject obj) { try { - std::shared_ptr<DBGattServer> * ref_ptr = jau::getInstance<std::shared_ptr<DBGattServer>>(env, obj); - return (jint)( (*ref_ptr)->getMaxAttMTU() ); + jau::shared_ptr_ref<DBGattServer> ref(env, obj); // hold until done + return (jint)( ref->getMaxAttMTU() ); } catch(...) { rethrow_and_raise_java_exception(env); } @@ -570,8 +580,8 @@ jint Java_org_direct_1bt_DBGattServer_getMaxAttMTU(JNIEnv *env, jobject obj) void Java_org_direct_1bt_DBGattServer_setMaxAttMTU(JNIEnv *env, jobject obj, jint v) { try { - std::shared_ptr<DBGattServer> * ref_ptr = jau::getInstance<std::shared_ptr<DBGattServer>>(env, obj); - (*ref_ptr)->setMaxAttMTU(v); + jau::shared_ptr_ref<DBGattServer> ref(env, obj); // hold until done + ref->setMaxAttMTU(v); } catch(...) { rethrow_and_raise_java_exception(env); } @@ -579,8 +589,8 @@ void Java_org_direct_1bt_DBGattServer_setMaxAttMTU(JNIEnv *env, jobject obj, jin jstring Java_org_direct_1bt_DBGattServer_toString(JNIEnv *env, jobject obj) { try { - std::shared_ptr<DBGattServer> * ref_ptr = jau::getInstance<std::shared_ptr<DBGattServer>>(env, obj); - return jau::from_string_to_jstring(env, (*ref_ptr)->toString()); + jau::shared_ptr_ref<DBGattServer> ref(env, obj); // hold until done + return jau::from_string_to_jstring(env, ref->toString()); } catch(...) { rethrow_and_raise_java_exception(env); } @@ -748,9 +758,9 @@ class JNIDBGattServerListener : public DBGattServer::Listener { */ jboolean Java_org_direct_1bt_DBGattServer_addListenerImpl(JNIEnv *env, jobject obj, jobject jlistener) { try { - std::shared_ptr<DBGattServer> * ref_ptr = jau::getInstance<std::shared_ptr<DBGattServer>>(env, obj); - std::shared_ptr<JNIDBGattServerListener> * listener_ref_ptr = jau::getInstance<std::shared_ptr<JNIDBGattServerListener>>(env, jlistener); - bool res = (*ref_ptr)->addListener(*listener_ref_ptr); + jau::shared_ptr_ref<DBGattServer> ref(env, obj); // hold until done + jau::shared_ptr_ref<JNIDBGattServerListener> listener_ref(env, jlistener); // hold until done + bool res = ref->addListener(listener_ref.shared_ptr()); return res ? JNI_TRUE : JNI_FALSE; } catch(...) { rethrow_and_raise_java_exception(env); @@ -765,9 +775,9 @@ jboolean Java_org_direct_1bt_DBGattServer_addListenerImpl(JNIEnv *env, jobject o */ jboolean Java_org_direct_1bt_DBGattServer_removeListenerImpl(JNIEnv *env, jobject obj, jobject jlistener) { try { - std::shared_ptr<DBGattServer> * ref_ptr = jau::getInstance<std::shared_ptr<DBGattServer>>(env, obj); - std::shared_ptr<JNIDBGattServerListener> * listener_ref_ptr = jau::getInstance<std::shared_ptr<JNIDBGattServerListener>>(env, jlistener); - bool res = (*ref_ptr)->removeListener(*listener_ref_ptr); + jau::shared_ptr_ref<DBGattServer> ref(env, obj); // hold until done + jau::shared_ptr_ref<JNIDBGattServerListener> listener_ref(env, jlistener); // hold until done + bool res = ref->removeListener(listener_ref.shared_ptr()); return res ? JNI_TRUE : JNI_FALSE; } catch(...) { rethrow_and_raise_java_exception(env); @@ -784,11 +794,10 @@ jboolean Java_org_direct_1bt_DBGattServer_removeListenerImpl(JNIEnv *env, jobjec jlong Java_org_direct_1bt_DBGattServer_00024Listener_ctorImpl(JNIEnv *env, jobject obj) { try { jclass clazz = jau::search_class(env, obj); - std::shared_ptr<JNIDBGattServerListener> ref = std::make_shared<JNIDBGattServerListener>(env, clazz, obj); + jau::shared_ptr_ref<JNIDBGattServerListener> ref( new JNIDBGattServerListener(env, clazz, obj) ); env->DeleteLocalRef(clazz); - std::shared_ptr<JNIDBGattServerListener> * ref_ptr = new std::shared_ptr<JNIDBGattServerListener>(std::move(ref)); - return (jlong)(intptr_t)ref_ptr; + return ref.release_to_jlong(); } catch(...) { rethrow_and_raise_java_exception(env); } @@ -803,9 +812,10 @@ jlong Java_org_direct_1bt_DBGattServer_00024Listener_ctorImpl(JNIEnv *env, jobje void Java_org_direct_1bt_DBGattServer_00024Listener_dtorImpl(JNIEnv *env, jclass clazz, jlong nativeInstance) { (void)clazz; try { - if( 0 != nativeInstance ) { - std::shared_ptr<JNIDBGattServerListener> * ref_ptr = reinterpret_cast<std::shared_ptr<JNIDBGattServerListener> *>(nativeInstance); - delete ref_ptr; + jau::shared_ptr_ref<JNIDBGattServerListener> sref(nativeInstance, false /* throw_on_nullptr */); // hold copy until done + if( nullptr != sref.pointer() ) { + std::shared_ptr<JNIDBGattServerListener>* sref_ptr = jau::castInstance<JNIDBGattServerListener>(nativeInstance); + delete sref_ptr; } } catch(...) { rethrow_and_raise_java_exception(env); diff --git a/java/jni/direct_bt/DBTAdapter.cxx b/java/jni/direct_bt/DBTAdapter.cxx index 461cab35..51023378 100644 --- a/java/jni/direct_bt/DBTAdapter.cxx +++ b/java/jni/direct_bt/DBTAdapter.cxx @@ -567,8 +567,9 @@ jint Java_jau_direct_1bt_DBTAdapter_removeAllStatusListenerImpl(JNIEnv *env, job jboolean Java_jau_direct_1bt_DBTAdapter_isDeviceWhitelisted(JNIEnv *env, jobject obj, jbyteArray jaddress, jbyte jaddressType) { try { - BTAdapter *adapter = jau::getJavaUplinkObject<BTAdapter>(env, obj); - jau::JavaGlobalObj::check(adapter->getJavaObject(), E_FILE_LINE); + jau::shared_ptr_ref<BTAdapter> adapter(env, obj); // hold until done + jau::JavaAnonRef adapter_java = adapter->getJavaObject(); // hold until done! + jau::JavaGlobalObj::check(adapter_java, E_FILE_LINE); if( nullptr == jaddress ) { throw jau::IllegalArgumentException("address null", E_FILE_LINE); @@ -596,8 +597,9 @@ jboolean Java_jau_direct_1bt_DBTAdapter_addDeviceToWhitelistImpl1(JNIEnv *env, j jshort min_interval, jshort max_interval, jshort latency, jshort timeout) { try { - BTAdapter *adapter = jau::getJavaUplinkObject<BTAdapter>(env, obj); - jau::JavaGlobalObj::check(adapter->getJavaObject(), E_FILE_LINE); + jau::shared_ptr_ref<BTAdapter> adapter(env, obj); // hold until done + jau::JavaAnonRef adapter_java = adapter->getJavaObject(); // hold until done! + jau::JavaGlobalObj::check(adapter_java, E_FILE_LINE); if( nullptr == jaddress ) { throw jau::IllegalArgumentException("address null", E_FILE_LINE); @@ -624,8 +626,9 @@ jboolean Java_jau_direct_1bt_DBTAdapter_addDeviceToWhitelistImpl1(JNIEnv *env, j jboolean Java_jau_direct_1bt_DBTAdapter_addDeviceToWhitelistImpl2(JNIEnv *env, jobject obj, jbyteArray jaddress, jbyte jaddressType, int jctype) { try { - BTAdapter *adapter = jau::getJavaUplinkObject<BTAdapter>(env, obj); - jau::JavaGlobalObj::check(adapter->getJavaObject(), E_FILE_LINE); + jau::shared_ptr_ref<BTAdapter> adapter(env, obj); // hold until done + jau::JavaAnonRef adapter_java = adapter->getJavaObject(); // hold until done! + jau::JavaGlobalObj::check(adapter_java, E_FILE_LINE); if( nullptr == jaddress ) { throw jau::IllegalArgumentException("address null", E_FILE_LINE); @@ -651,8 +654,9 @@ jboolean Java_jau_direct_1bt_DBTAdapter_addDeviceToWhitelistImpl2(JNIEnv *env, j } jboolean Java_jau_direct_1bt_DBTAdapter_removeDeviceFromWhitelistImpl(JNIEnv *env, jobject obj, jbyteArray jaddress, jbyte jaddressType) { try { - BTAdapter *adapter = jau::getJavaUplinkObject<BTAdapter>(env, obj); - jau::JavaGlobalObj::check(adapter->getJavaObject(), E_FILE_LINE); + jau::shared_ptr_ref<BTAdapter> adapter(env, obj); // hold until done + jau::JavaAnonRef adapter_java = adapter->getJavaObject(); // hold until done! + jau::JavaGlobalObj::check(adapter_java, E_FILE_LINE); if( nullptr == jaddress ) { throw jau::IllegalArgumentException("address null", E_FILE_LINE); @@ -678,33 +682,24 @@ jboolean Java_jau_direct_1bt_DBTAdapter_removeDeviceFromWhitelistImpl(JNIEnv *en jstring Java_jau_direct_1bt_DBTAdapter_toStringImpl(JNIEnv *env, jobject obj) { try { - BTAdapter *nativePtr = jau::getJavaUplinkObject<BTAdapter>(env, obj); - jau::JavaGlobalObj::check(nativePtr->getJavaObject(), E_FILE_LINE); - return jau::from_string_to_jstring(env, nativePtr->toString()); - } catch(...) { - rethrow_and_raise_java_exception(env); - } - return nullptr; -} + jau::shared_ptr_ref<BTAdapter> adapter(env, obj); // hold until done + jau::JavaAnonRef adapter_java = adapter->getJavaObject(); // hold until done! + jau::JavaGlobalObj::check(adapter_java, E_FILE_LINE); -void Java_jau_direct_1bt_DBTAdapter_deleteImpl(JNIEnv *env, jobject obj, jlong nativeInstance) -{ - (void)obj; - try { - BTAdapter *adapter = jau::castInstance<BTAdapter>(nativeInstance); - DBG_PRINT("Java_jau_direct_1bt_DBTAdapter_deleteImpl (close only) %s", adapter->toString().c_str()); - adapter->close(); - // No delete: BTAdapter instance owned by DBTManager - // However, adapter->close() cleans up most.. + return jau::from_string_to_jstring(env, adapter->toString()); } catch(...) { rethrow_and_raise_java_exception(env); } + return nullptr; } jint Java_jau_direct_1bt_DBTAdapter_getBTMajorVersion(JNIEnv *env, jobject obj) { try { - BTAdapter *adapter = jau::getJavaUplinkObject<BTAdapter>(env, obj); + jau::shared_ptr_ref<BTAdapter> adapter(env, obj); // hold until done + jau::JavaAnonRef adapter_java = adapter->getJavaObject(); // hold until done! + jau::JavaGlobalObj::check(adapter_java, E_FILE_LINE); + return (jint) adapter->getBTMajorVersion(); } catch(...) { rethrow_and_raise_java_exception(env); @@ -715,7 +710,10 @@ jint Java_jau_direct_1bt_DBTAdapter_getBTMajorVersion(JNIEnv *env, jobject obj) jboolean Java_jau_direct_1bt_DBTAdapter_isPoweredImpl(JNIEnv *env, jobject obj) { try { - BTAdapter *adapter = jau::getJavaUplinkObject<BTAdapter>(env, obj); + jau::shared_ptr_ref<BTAdapter> adapter(env, obj); // hold until done + jau::JavaAnonRef adapter_java = adapter->getJavaObject(); // hold until done! + jau::JavaGlobalObj::check(adapter_java, E_FILE_LINE); + return adapter->isPowered(); } catch(...) { rethrow_and_raise_java_exception(env); @@ -726,7 +724,10 @@ jboolean Java_jau_direct_1bt_DBTAdapter_isPoweredImpl(JNIEnv *env, jobject obj) jboolean Java_jau_direct_1bt_DBTAdapter_isSuspendedImpl(JNIEnv *env, jobject obj) { try { - BTAdapter *adapter = jau::getJavaUplinkObject<BTAdapter>(env, obj); + jau::shared_ptr_ref<BTAdapter> adapter(env, obj); // hold until done + jau::JavaAnonRef adapter_java = adapter->getJavaObject(); // hold until done! + jau::JavaGlobalObj::check(adapter_java, E_FILE_LINE); + return adapter->isSuspended(); } catch(...) { rethrow_and_raise_java_exception(env); @@ -737,7 +738,10 @@ jboolean Java_jau_direct_1bt_DBTAdapter_isSuspendedImpl(JNIEnv *env, jobject obj jboolean Java_jau_direct_1bt_DBTAdapter_isValidImpl(JNIEnv *env, jobject obj) { try { - BTAdapter *adapter = jau::getJavaUplinkObject<BTAdapter>(env, obj); + jau::shared_ptr_ref<BTAdapter> adapter(env, obj); // hold until done + jau::JavaAnonRef adapter_java = adapter->getJavaObject(); // hold until done! + jau::JavaGlobalObj::check(adapter_java, E_FILE_LINE); + return adapter->isValid(); } catch(...) { rethrow_and_raise_java_exception(env); @@ -748,7 +752,10 @@ jboolean Java_jau_direct_1bt_DBTAdapter_isValidImpl(JNIEnv *env, jobject obj) jlong Java_jau_direct_1bt_DBTAdapter_getLEFeaturesImpl(JNIEnv *env, jobject obj) { try { - BTAdapter *adapter = jau::getJavaUplinkObject<BTAdapter>(env, obj); + jau::shared_ptr_ref<BTAdapter> adapter(env, obj); // hold until done + jau::JavaAnonRef adapter_java = adapter->getJavaObject(); // hold until done! + jau::JavaGlobalObj::check(adapter_java, E_FILE_LINE); + return (jlong) number( adapter->getLEFeatures() ); } catch(...) { rethrow_and_raise_java_exception(env); @@ -762,7 +769,10 @@ jbyte Java_jau_direct_1bt_DBTAdapter_startDiscoveryImpl(JNIEnv *env, jobject obj jboolean filter_dup) { try { - BTAdapter *adapter = jau::getJavaUplinkObject<BTAdapter>(env, obj); + jau::shared_ptr_ref<BTAdapter> adapter(env, obj); // hold until done + jau::JavaAnonRef adapter_java = adapter->getJavaObject(); // hold until done! + jau::JavaGlobalObj::check(adapter_java, E_FILE_LINE); + return (jbyte) number( adapter->startDiscovery(static_cast<DiscoveryPolicy>(policy), le_scan_active, le_scan_interval, le_scan_window, filter_policy, filter_dup==JNI_TRUE) ); } catch(...) { rethrow_and_raise_java_exception(env); @@ -773,7 +783,10 @@ jbyte Java_jau_direct_1bt_DBTAdapter_startDiscoveryImpl(JNIEnv *env, jobject obj jbyte Java_jau_direct_1bt_DBTAdapter_stopDiscoveryImpl(JNIEnv *env, jobject obj) { try { - BTAdapter *adapter = jau::getJavaUplinkObject<BTAdapter>(env, obj); + jau::shared_ptr_ref<BTAdapter> adapter(env, obj); // hold until done + jau::JavaAnonRef adapter_java = adapter->getJavaObject(); // hold until done! + jau::JavaGlobalObj::check(adapter_java, E_FILE_LINE); + return (jbyte) number( adapter->stopDiscovery() ); } catch(...) { rethrow_and_raise_java_exception(env); @@ -784,7 +797,10 @@ jbyte Java_jau_direct_1bt_DBTAdapter_stopDiscoveryImpl(JNIEnv *env, jobject obj) jbyte Java_jau_direct_1bt_DBTAdapter_getCurrentDiscoveryPolicyImpl(JNIEnv *env, jobject obj) { DiscoveryPolicy current = DiscoveryPolicy::AUTO_OFF; try { - BTAdapter *adapter = jau::getJavaUplinkObject<BTAdapter>(env, obj); + jau::shared_ptr_ref<BTAdapter> adapter(env, obj); // hold until done + jau::JavaAnonRef adapter_java = adapter->getJavaObject(); // hold until done! + jau::JavaGlobalObj::check(adapter_java, E_FILE_LINE); + current = adapter->getCurrentDiscoveryPolicy(); } catch(...) { rethrow_and_raise_java_exception(env); @@ -794,10 +810,13 @@ jbyte Java_jau_direct_1bt_DBTAdapter_getCurrentDiscoveryPolicyImpl(JNIEnv *env, jboolean Java_jau_direct_1bt_DBTAdapter_removeDevicePausingDiscovery(JNIEnv *env, jobject obj, jobject jdevice) { try { - BTAdapter *adapter = jau::getJavaUplinkObject<BTAdapter>(env, obj); + jau::shared_ptr_ref<BTAdapter> adapter(env, obj); // hold until done + jau::JavaAnonRef adapter_java = adapter->getJavaObject(); // hold until done! + jau::JavaGlobalObj::check(adapter_java, E_FILE_LINE); - BTDevice *device = jau::getJavaUplinkObject<BTDevice>(env, jdevice); - jau::JavaGlobalObj::check(device->getJavaObject(), E_FILE_LINE); + jau::shared_ptr_ref<BTDevice> device(env, jdevice); // hold until done + jau::JavaAnonRef device_java = device->getJavaObject(); // hold until done! + jau::JavaGlobalObj::check(device_java, E_FILE_LINE); return adapter->removeDevicePausingDiscovery(*device) ? JNI_TRUE : JNI_FALSE; } catch(...) { @@ -809,7 +828,10 @@ jboolean Java_jau_direct_1bt_DBTAdapter_removeDevicePausingDiscovery(JNIEnv *env jbyte Java_jau_direct_1bt_DBTAdapter_getRoleImpl(JNIEnv *env, jobject obj) { try { - BTAdapter *adapter = jau::getJavaUplinkObject<BTAdapter>(env, obj); + jau::shared_ptr_ref<BTAdapter> adapter(env, obj); // hold until done + jau::JavaAnonRef adapter_java = adapter->getJavaObject(); // hold until done! + jau::JavaGlobalObj::check(adapter_java, E_FILE_LINE); + return (jbyte) number( adapter->getRole() ); } catch(...) { rethrow_and_raise_java_exception(env); @@ -820,7 +842,10 @@ jbyte Java_jau_direct_1bt_DBTAdapter_getRoleImpl(JNIEnv *env, jobject obj) jbyte Java_jau_direct_1bt_DBTAdapter_getBTModeImpl(JNIEnv *env, jobject obj) { try { - BTAdapter *adapter = jau::getJavaUplinkObject<BTAdapter>(env, obj); + jau::shared_ptr_ref<BTAdapter> adapter(env, obj); // hold until done + jau::JavaAnonRef adapter_java = adapter->getJavaObject(); // hold until done! + jau::JavaGlobalObj::check(adapter_java, E_FILE_LINE); + return (jbyte) number( adapter->getBTMode() ); } catch(...) { rethrow_and_raise_java_exception(env); @@ -831,7 +856,10 @@ jbyte Java_jau_direct_1bt_DBTAdapter_getBTModeImpl(JNIEnv *env, jobject obj) jobject Java_jau_direct_1bt_DBTAdapter_getDiscoveredDevicesImpl(JNIEnv *env, jobject obj) { try { - BTAdapter *adapter = jau::getJavaUplinkObject<BTAdapter>(env, obj); + jau::shared_ptr_ref<BTAdapter> adapter(env, obj); // hold until done + jau::JavaAnonRef adapter_java = adapter->getJavaObject(); // hold until done! + jau::JavaGlobalObj::check(adapter_java, E_FILE_LINE); + jau::darray<BTDeviceRef> array = adapter->getDiscoveredDevices(); return convert_vector_sharedptr_to_jarraylist(env, array); } catch(...) { @@ -843,7 +871,10 @@ jobject Java_jau_direct_1bt_DBTAdapter_getDiscoveredDevicesImpl(JNIEnv *env, job jint Java_jau_direct_1bt_DBTAdapter_removeDiscoveredDevicesImpl1(JNIEnv *env, jobject obj) { try { - BTAdapter *adapter = jau::getJavaUplinkObject<BTAdapter>(env, obj); + jau::shared_ptr_ref<BTAdapter> adapter(env, obj); // hold until done + jau::JavaAnonRef adapter_java = adapter->getJavaObject(); // hold until done! + jau::JavaGlobalObj::check(adapter_java, E_FILE_LINE); + return adapter->removeDiscoveredDevices(); } catch(...) { rethrow_and_raise_java_exception(env); @@ -854,8 +885,9 @@ jint Java_jau_direct_1bt_DBTAdapter_removeDiscoveredDevicesImpl1(JNIEnv *env, jo jboolean Java_jau_direct_1bt_DBTAdapter_removeDiscoveredDeviceImpl1(JNIEnv *env, jobject obj, jbyteArray jaddress, jbyte jaddressType) { try { - BTAdapter *adapter = jau::getJavaUplinkObject<BTAdapter>(env, obj); - jau::JavaGlobalObj::check(adapter->getJavaObject(), E_FILE_LINE); + jau::shared_ptr_ref<BTAdapter> adapter(env, obj); // hold until done + jau::JavaAnonRef adapter_java = adapter->getJavaObject(); // hold until done! + jau::JavaGlobalObj::check(adapter_java, E_FILE_LINE); if( nullptr == jaddress ) { throw jau::IllegalArgumentException("address null", E_FILE_LINE); @@ -885,8 +917,10 @@ jboolean Java_jau_direct_1bt_DBTAdapter_removeDiscoveredDeviceImpl1(JNIEnv *env, jboolean Java_jau_direct_1bt_DBTAdapter_setPowered(JNIEnv *env, jobject obj, jboolean power_on) { try { - BTAdapter *adapter = jau::getJavaUplinkObject<BTAdapter>(env, obj); - jau::JavaGlobalObj::check(adapter->getJavaObject(), E_FILE_LINE); + jau::shared_ptr_ref<BTAdapter> adapter(env, obj); // hold until done + jau::JavaAnonRef adapter_java = adapter->getJavaObject(); // hold until done! + jau::JavaGlobalObj::check(adapter_java, E_FILE_LINE); + return adapter->setPowered(JNI_TRUE == power_on ? true : false) ? JNI_TRUE : JNI_FALSE; } catch(...) { rethrow_and_raise_java_exception(env); @@ -896,8 +930,10 @@ jboolean Java_jau_direct_1bt_DBTAdapter_setPowered(JNIEnv *env, jobject obj, jbo jboolean Java_jau_direct_1bt_DBTAdapter_getSecureConnectionsEnabled(JNIEnv *env, jobject obj) { try { - BTAdapter *adapter = jau::getJavaUplinkObject<BTAdapter>(env, obj); - jau::JavaGlobalObj::check(adapter->getJavaObject(), E_FILE_LINE); + jau::shared_ptr_ref<BTAdapter> adapter(env, obj); // hold until done + jau::JavaAnonRef adapter_java = adapter->getJavaObject(); // hold until done! + jau::JavaGlobalObj::check(adapter_java, E_FILE_LINE); + return adapter->getSecureConnectionsEnabled() ? JNI_TRUE : JNI_FALSE; } catch(...) { rethrow_and_raise_java_exception(env); @@ -907,8 +943,10 @@ jboolean Java_jau_direct_1bt_DBTAdapter_getSecureConnectionsEnabled(JNIEnv *env, jbyte Java_jau_direct_1bt_DBTAdapter_setSecureConnectionsImpl(JNIEnv *env, jobject obj, jboolean enable) { try { - BTAdapter *adapter = jau::getJavaUplinkObject<BTAdapter>(env, obj); - jau::JavaGlobalObj::check(adapter->getJavaObject(), E_FILE_LINE); + jau::shared_ptr_ref<BTAdapter> adapter(env, obj); // hold until done + jau::JavaAnonRef adapter_java = adapter->getJavaObject(); // hold until done! + jau::JavaGlobalObj::check(adapter_java, E_FILE_LINE); + HCIStatusCode res = adapter->setSecureConnections(JNI_TRUE == enable ? true : false); return (jbyte) number(res); } catch(...) { @@ -921,8 +959,10 @@ jbyte Java_jau_direct_1bt_DBTAdapter_setDefaultConnParamImpl(JNIEnv *env, jobjec jshort conn_interval_min, jshort conn_interval_max, jshort conn_latency, jshort supervision_timeout) { try { - BTAdapter *adapter = jau::getJavaUplinkObject<BTAdapter>(env, obj); - jau::JavaGlobalObj::check(adapter->getJavaObject(), E_FILE_LINE); + jau::shared_ptr_ref<BTAdapter> adapter(env, obj); // hold until done + jau::JavaAnonRef adapter_java = adapter->getJavaObject(); // hold until done! + jau::JavaGlobalObj::check(adapter_java, E_FILE_LINE); + HCIStatusCode res = adapter->setDefaultConnParam(static_cast<uint16_t>(conn_interval_min), static_cast<uint16_t>(conn_interval_max), static_cast<uint16_t>(conn_latency), @@ -936,8 +976,9 @@ jbyte Java_jau_direct_1bt_DBTAdapter_setDefaultConnParamImpl(JNIEnv *env, jobjec void Java_jau_direct_1bt_DBTAdapter_setServerConnSecurityImpl(JNIEnv *env, jobject obj, jbyte jsec_level, jbyte jio_cap) { try { - BTAdapter *adapter = jau::getJavaUplinkObject<BTAdapter>(env, obj); - jau::JavaGlobalObj::check(adapter->getJavaObject(), E_FILE_LINE); + jau::shared_ptr_ref<BTAdapter> adapter(env, obj); // hold until done + jau::JavaAnonRef adapter_java = adapter->getJavaObject(); // hold until done! + jau::JavaGlobalObj::check(adapter_java, E_FILE_LINE); adapter->setServerConnSecurity( to_BTSecurityLevel( static_cast<uint8_t>(jsec_level) ), to_SMPIOCapability( static_cast<uint8_t>(jio_cap) ) ); @@ -948,8 +989,10 @@ void Java_jau_direct_1bt_DBTAdapter_setServerConnSecurityImpl(JNIEnv *env, jobje void Java_jau_direct_1bt_DBTAdapter_setSMPKeyPath(JNIEnv *env, jobject obj, jstring jpath) { try { - BTAdapter *adapter = jau::getJavaUplinkObject<BTAdapter>(env, obj); - jau::JavaGlobalObj::check(adapter->getJavaObject(), E_FILE_LINE); + jau::shared_ptr_ref<BTAdapter> adapter(env, obj); // hold until done + jau::JavaAnonRef adapter_java = adapter->getJavaObject(); // hold until done! + jau::JavaGlobalObj::check(adapter_java, E_FILE_LINE); + std::string path = jau::from_jstring_to_string(env, jpath); adapter->setSMPKeyPath(path); } catch(...) { @@ -959,8 +1002,10 @@ void Java_jau_direct_1bt_DBTAdapter_setSMPKeyPath(JNIEnv *env, jobject obj, jstr jbyte Java_jau_direct_1bt_DBTAdapter_initializeImpl(JNIEnv *env, jobject obj, jbyte jbtMode) { try { - BTAdapter *adapter = jau::getJavaUplinkObject<BTAdapter>(env, obj); - jau::JavaGlobalObj::check(adapter->getJavaObject(), E_FILE_LINE); + jau::shared_ptr_ref<BTAdapter> adapter(env, obj); // hold until done + jau::JavaAnonRef adapter_java = adapter->getJavaObject(); // hold until done! + jau::JavaGlobalObj::check(adapter_java, E_FILE_LINE); + const BTMode btMode = static_cast<BTMode>(jbtMode); HCIStatusCode res = adapter->initialize(btMode); return (jbyte) number(res); @@ -972,8 +1017,10 @@ jbyte Java_jau_direct_1bt_DBTAdapter_initializeImpl(JNIEnv *env, jobject obj, jb jboolean Java_jau_direct_1bt_DBTAdapter_isInitialized(JNIEnv *env, jobject obj) { try { - BTAdapter *adapter = jau::getJavaUplinkObject<BTAdapter>(env, obj); - jau::JavaGlobalObj::check(adapter->getJavaObject(), E_FILE_LINE); + jau::shared_ptr_ref<BTAdapter> adapter(env, obj); // hold until done + jau::JavaAnonRef adapter_java = adapter->getJavaObject(); // hold until done! + jau::JavaGlobalObj::check(adapter_java, E_FILE_LINE); + return adapter->isInitialized(); } catch(...) { rethrow_and_raise_java_exception(env); @@ -983,8 +1030,10 @@ jboolean Java_jau_direct_1bt_DBTAdapter_isInitialized(JNIEnv *env, jobject obj) jbyte Java_jau_direct_1bt_DBTAdapter_resetImpl(JNIEnv *env, jobject obj) { try { - BTAdapter *adapter = jau::getJavaUplinkObject<BTAdapter>(env, obj); - jau::JavaGlobalObj::check(adapter->getJavaObject(), E_FILE_LINE); + jau::shared_ptr_ref<BTAdapter> adapter(env, obj); // hold until done + jau::JavaAnonRef adapter_java = adapter->getJavaObject(); // hold until done! + jau::JavaGlobalObj::check(adapter_java, E_FILE_LINE); + HCIStatusCode res = adapter->reset(); return (jbyte) number(res); } catch(...) { @@ -996,8 +1045,9 @@ jbyte Java_jau_direct_1bt_DBTAdapter_resetImpl(JNIEnv *env, jobject obj) { jbyte Java_jau_direct_1bt_DBTAdapter_setDefaultLE_1PHYImpl(JNIEnv *env, jobject obj, jbyte jTx, jbyte jRx) { try { - BTAdapter *adapter = jau::getJavaUplinkObject<BTAdapter>(env, obj); - jau::JavaGlobalObj::check(adapter->getJavaObject(), E_FILE_LINE); + jau::shared_ptr_ref<BTAdapter> adapter(env, obj); // hold until done + jau::JavaAnonRef adapter_java = adapter->getJavaObject(); // hold until done! + jau::JavaGlobalObj::check(adapter_java, E_FILE_LINE); const LE_PHYs Tx = static_cast<LE_PHYs>(jTx); const LE_PHYs Rx = static_cast<LE_PHYs>(jRx); @@ -1011,8 +1061,10 @@ jbyte Java_jau_direct_1bt_DBTAdapter_setDefaultLE_1PHYImpl(JNIEnv *env, jobject jstring Java_jau_direct_1bt_DBTAdapter_getNameImpl(JNIEnv *env, jobject obj) { try { - BTAdapter *adapter = jau::getJavaUplinkObject<BTAdapter>(env, obj); - jau::JavaGlobalObj::check(adapter->getJavaObject(), E_FILE_LINE); + jau::shared_ptr_ref<BTAdapter> adapter(env, obj); // hold until done + jau::JavaAnonRef adapter_java = adapter->getJavaObject(); // hold until done! + jau::JavaGlobalObj::check(adapter_java, E_FILE_LINE); + return jau::from_string_to_jstring(env, adapter->getName()); } catch(...) { rethrow_and_raise_java_exception(env); @@ -1022,8 +1074,10 @@ jstring Java_jau_direct_1bt_DBTAdapter_getNameImpl(JNIEnv *env, jobject obj) { jstring Java_jau_direct_1bt_DBTAdapter_getShortNameImpl(JNIEnv *env, jobject obj) { try { - BTAdapter *adapter = jau::getJavaUplinkObject<BTAdapter>(env, obj); - jau::JavaGlobalObj::check(adapter->getJavaObject(), E_FILE_LINE); + jau::shared_ptr_ref<BTAdapter> adapter(env, obj); // hold until done + jau::JavaAnonRef adapter_java = adapter->getJavaObject(); // hold until done! + jau::JavaGlobalObj::check(adapter_java, E_FILE_LINE); + return jau::from_string_to_jstring(env, adapter->getShortName()); } catch(...) { rethrow_and_raise_java_exception(env); @@ -1033,8 +1087,10 @@ jstring Java_jau_direct_1bt_DBTAdapter_getShortNameImpl(JNIEnv *env, jobject obj jbyte Java_jau_direct_1bt_DBTAdapter_setNameImpl(JNIEnv *env, jobject obj, jstring jname, jstring jshort_name) { try { - BTAdapter *adapter = jau::getJavaUplinkObject<BTAdapter>(env, obj); - jau::JavaGlobalObj::check(adapter->getJavaObject(), E_FILE_LINE); + jau::shared_ptr_ref<BTAdapter> adapter(env, obj); // hold until done + jau::JavaAnonRef adapter_java = adapter->getJavaObject(); // hold until done! + jau::JavaGlobalObj::check(adapter_java, E_FILE_LINE); + std::string name = jau::from_jstring_to_string(env, jname); std::string short_name = jau::from_jstring_to_string(env, jshort_name); return (jbyte) number( adapter->setName(name, short_name) ); @@ -1046,8 +1102,9 @@ jbyte Java_jau_direct_1bt_DBTAdapter_setNameImpl(JNIEnv *env, jobject obj, jstri jobject Java_jau_direct_1bt_DBTAdapter_connectDeviceImpl(JNIEnv *env, jobject obj, jbyteArray jaddress, jbyte jaddressType) { try { - BTAdapter *adapter = jau::getJavaUplinkObject<BTAdapter>(env, obj); - jau::JavaGlobalObj::check(adapter->getJavaObject(), E_FILE_LINE); + jau::shared_ptr_ref<BTAdapter> adapter(env, obj); // hold until done + jau::JavaAnonRef adapter_java = adapter->getJavaObject(); // hold until done! + jau::JavaGlobalObj::check(adapter_java, E_FILE_LINE); if( nullptr == jaddress ) { throw jau::IllegalArgumentException("address null", E_FILE_LINE); @@ -1073,7 +1130,7 @@ jobject Java_jau_direct_1bt_DBTAdapter_connectDeviceImpl(JNIEnv *env, jobject ob if( !hci.isOpen() ) { throw BTException("Adapter's HCI closed "+adapter->toString(), E_FILE_LINE); } - std::shared_ptr<jau::JavaAnon> jDeviceRef = device->getJavaObject(); + jau::JavaAnonRef jDeviceRef = device->getJavaObject(); jau::JavaGlobalObj::check(jDeviceRef, E_FILE_LINE); device->connectDefault(); @@ -1087,8 +1144,10 @@ jobject Java_jau_direct_1bt_DBTAdapter_connectDeviceImpl(JNIEnv *env, jobject ob void Java_jau_direct_1bt_DBTAdapter_printDeviceListsImpl(JNIEnv *env, jobject obj) { try { - BTAdapter *adapter = jau::getJavaUplinkObject<BTAdapter>(env, obj); - jau::JavaGlobalObj::check(adapter->getJavaObject(), E_FILE_LINE); + jau::shared_ptr_ref<BTAdapter> adapter(env, obj); // hold until done + jau::JavaAnonRef adapter_java = adapter->getJavaObject(); // hold until done! + jau::JavaGlobalObj::check(adapter_java, E_FILE_LINE); + adapter->printDeviceLists(); } catch(...) { rethrow_and_raise_java_exception(env); @@ -1103,22 +1162,18 @@ jbyte Java_jau_direct_1bt_DBTAdapter_startAdvertising1Impl(JNIEnv *env, jobject jshort adv_interval_min, jshort adv_interval_max, jbyte jadv_type, jbyte adv_chan_map, jbyte filter_policy) { try { - DBGattServerRef gattServerRef; // nullptr - if( nullptr != jgattServerData ) { - std::shared_ptr<DBGattServer> * ref_ptr = jau::getInstance<std::shared_ptr<DBGattServer>>(env, jgattServerData); - gattServerRef = *ref_ptr; - } - if( nullptr == jeir ) { - throw jau::IllegalArgumentException("eir null", E_FILE_LINE); - } - std::shared_ptr<EInfoReport>& eir_ptr = *jau::getInstance<std::shared_ptr<EInfoReport>>(env, jeir); - BTAdapter *adapter = jau::getJavaUplinkObject<BTAdapter>(env, obj); - jau::JavaGlobalObj::check(adapter->getJavaObject(), E_FILE_LINE); + jau::shared_ptr_ref<BTAdapter> adapter(env, obj); // hold until done + jau::JavaAnonRef adapter_java = adapter->getJavaObject(); // hold until done! + jau::JavaGlobalObj::check(adapter_java, E_FILE_LINE); + + jau::shared_ptr_ref<DBGattServer> gatt_server_ref(env, jgattServerData, false /** throw_on_null */); // hold until done + gatt_server_ref.null_check1(); + jau::shared_ptr_ref<EInfoReport> eir_ref(env, jeir); // hold until done const EIRDataType adv_mask = static_cast<EIRDataType>(jadv_mask); const EIRDataType scanrsp_mask = static_cast<EIRDataType>(jscanrsp_mask); const AD_PDU_Type adv_type = static_cast<AD_PDU_Type>(jadv_type); - HCIStatusCode res = adapter->startAdvertising(gattServerRef, *eir_ptr, adv_mask, scanrsp_mask, adv_interval_min, adv_interval_max, adv_type, adv_chan_map, filter_policy); + HCIStatusCode res = adapter->startAdvertising(gatt_server_ref.shared_ptr(), *eir_ref, adv_mask, scanrsp_mask, adv_interval_min, adv_interval_max, adv_type, adv_chan_map, filter_policy); return (jbyte) number(res); } catch(...) { rethrow_and_raise_java_exception(env); @@ -1131,15 +1186,15 @@ jbyte Java_jau_direct_1bt_DBTAdapter_startAdvertising2Impl(JNIEnv *env, jobject jshort adv_interval_min, jshort adv_interval_max, jbyte jadv_type, jbyte adv_chan_map, jbyte filter_policy) { try { - DBGattServerRef gattServerRef; // nullptr - if( nullptr != jgattServerData ) { - std::shared_ptr<DBGattServer> * ref_ptr = jau::getInstance<std::shared_ptr<DBGattServer>>(env, jgattServerData); - gattServerRef = *ref_ptr; - } - BTAdapter *adapter = jau::getJavaUplinkObject<BTAdapter>(env, obj); - jau::JavaGlobalObj::check(adapter->getJavaObject(), E_FILE_LINE); + jau::shared_ptr_ref<BTAdapter> adapter(env, obj); // hold until done + jau::JavaAnonRef adapter_java = adapter->getJavaObject(); // hold until done! + jau::JavaGlobalObj::check(adapter_java, E_FILE_LINE); + + jau::shared_ptr_ref<DBGattServer> gatt_server_ref(env, jgattServerData, false /** throw_on_null */); // hold until done + gatt_server_ref.null_check1(); + const AD_PDU_Type adv_type = static_cast<AD_PDU_Type>(jadv_type); - HCIStatusCode res = adapter->startAdvertising(gattServerRef, adv_interval_min, adv_interval_max, adv_type, adv_chan_map, filter_policy); + HCIStatusCode res = adapter->startAdvertising(gatt_server_ref.shared_ptr(), adv_interval_min, adv_interval_max, adv_type, adv_chan_map, filter_policy); return (jbyte) number(res); } catch(...) { rethrow_and_raise_java_exception(env); @@ -1149,8 +1204,10 @@ jbyte Java_jau_direct_1bt_DBTAdapter_startAdvertising2Impl(JNIEnv *env, jobject jbyte Java_jau_direct_1bt_DBTAdapter_stopAdvertisingImpl(JNIEnv *env, jobject obj) { try { - BTAdapter *adapter = jau::getJavaUplinkObject<BTAdapter>(env, obj); - jau::JavaGlobalObj::check(adapter->getJavaObject(), E_FILE_LINE); + jau::shared_ptr_ref<BTAdapter> adapter(env, obj); // hold until done + jau::JavaAnonRef adapter_java = adapter->getJavaObject(); // hold until done! + jau::JavaGlobalObj::check(adapter_java, E_FILE_LINE); + HCIStatusCode res = adapter->stopAdvertising(); return (jbyte) number(res); } catch(...) { @@ -1161,8 +1218,10 @@ jbyte Java_jau_direct_1bt_DBTAdapter_stopAdvertisingImpl(JNIEnv *env, jobject ob jboolean Java_jau_direct_1bt_DBTAdapter_isAdvertising(JNIEnv *env, jobject obj) { try { - BTAdapter *adapter = jau::getJavaUplinkObject<BTAdapter>(env, obj); - jau::JavaGlobalObj::check(adapter->getJavaObject(), E_FILE_LINE); + jau::shared_ptr_ref<BTAdapter> adapter(env, obj); // hold until done + jau::JavaAnonRef adapter_java = adapter->getJavaObject(); // hold until done! + jau::JavaGlobalObj::check(adapter_java, E_FILE_LINE); + return adapter->isAdvertising() ? JNI_TRUE : JNI_FALSE; } catch(...) { rethrow_and_raise_java_exception(env); diff --git a/java/jni/direct_bt/DBTDevice.cxx b/java/jni/direct_bt/DBTDevice.cxx index d28e50db..86d940f9 100644 --- a/java/jni/direct_bt/DBTDevice.cxx +++ b/java/jni/direct_bt/DBTDevice.cxx @@ -59,7 +59,7 @@ class JNIGattCharListener : public BTGattCharListener { }; */ - const BTGattChar * associatedCharacteristicRef; + std::shared_ptr<BTGattChar> associatedCharacteristicRef; JNIGlobalRef listenerObj; // keep listener instance alive JNIGlobalRef associatedCharacteristicObj; // keeps associated characteristic alive, if not null jmethodID mNotificationReceived = nullptr; @@ -67,7 +67,7 @@ class JNIGattCharListener : public BTGattCharListener { public: - JNIGattCharListener(JNIEnv *env, BTDevice *device, jobject listener, BTGattChar * associatedCharacteristicRef_) + JNIGattCharListener(JNIEnv *env, const std::shared_ptr<BTDevice>& device, jobject listener, const std::shared_ptr<BTGattChar>& associatedCharacteristicRef_) : associatedCharacteristicRef(associatedCharacteristicRef_), listenerObj(listener) { @@ -103,7 +103,7 @@ class JNIGattCharListener : public BTGattCharListener { void notificationReceived(BTGattCharRef charDecl, const TROOctets& charValue, const uint64_t timestamp) override { - std::shared_ptr<jau::JavaAnon> jCharDeclRef = charDecl->getJavaObject(); + jau::JavaAnonRef jCharDeclRef = charDecl->getJavaObject(); if( !jau::JavaGlobalObj::isValid(jCharDeclRef) ) { return; // java object has been pulled } @@ -124,7 +124,7 @@ class JNIGattCharListener : public BTGattCharListener { void indicationReceived(BTGattCharRef charDecl, const TROOctets& charValue, const uint64_t timestamp, const bool confirmationSent) override { - std::shared_ptr<jau::JavaAnon> jCharDeclRef = charDecl->getJavaObject(); + jau::JavaAnonRef jCharDeclRef = charDecl->getJavaObject(); if( !jau::JavaGlobalObj::isValid(jCharDeclRef) ) { return; // java object has been pulled } @@ -144,11 +144,34 @@ class JNIGattCharListener : public BTGattCharListener { }; +void Java_jau_direct_1bt_DBTDevice_deleteImpl(JNIEnv *env, jobject obj, jlong nativeInstance) +{ + (void)obj; + try { + jau::shared_ptr_ref<BTDevice> sref(nativeInstance, false /* throw_on_nullptr */); // hold copy until done + if( nullptr != sref.pointer() ) { + if( !sref.is_null() ) { + DBG_PRINT("Java_jau_direct_1bt_DBTDevice_deleteImpl (w/ remove) %s", sref->toString().c_str()); + sref->remove(); + } else { + DBG_PRINT("Java_jau_direct_1bt_DBTDevice_deleteImpl null reference"); + } + std::shared_ptr<BTDevice>* sref_ptr = jau::castInstance<BTDevice>(nativeInstance); + delete sref_ptr; + } else { + DBG_PRINT("Java_jau_direct_1bt_DBTDevice_deleteImpl null reference store"); + } + } catch(...) { + rethrow_and_raise_java_exception(env); + } +} + void Java_jau_direct_1bt_DBTDevice_initImpl(JNIEnv *env, jobject obj) { try { - BTDevice *device = getJavaUplinkObject<BTDevice>(env, obj); - JavaGlobalObj::check(device->getJavaObject(), E_FILE_LINE); + shared_ptr_ref<BTDevice> device(env, obj); // hold until done + jau::JavaAnonRef device_java = device->getJavaObject(); // hold until done! + JavaGlobalObj::check(device_java, E_FILE_LINE); } catch(...) { rethrow_and_raise_java_exception(env); } @@ -156,9 +179,10 @@ void Java_jau_direct_1bt_DBTDevice_initImpl(JNIEnv *env, jobject obj) jstring Java_jau_direct_1bt_DBTDevice_getNameImpl(JNIEnv *env, jobject obj) { try { - BTDevice *nativePtr = getJavaUplinkObject<BTDevice>(env, obj); - JavaGlobalObj::check(nativePtr->getJavaObject(), E_FILE_LINE); - return from_string_to_jstring(env, nativePtr->getName()); + shared_ptr_ref<BTDevice> device(env, obj); // hold until done + jau::JavaAnonRef device_java = device->getJavaObject(); // hold until done! + JavaGlobalObj::check(device_java, E_FILE_LINE); + return from_string_to_jstring(env, device->getName()); } catch(...) { rethrow_and_raise_java_exception(env); } @@ -167,9 +191,10 @@ jstring Java_jau_direct_1bt_DBTDevice_getNameImpl(JNIEnv *env, jobject obj) { jstring Java_jau_direct_1bt_DBTDevice_toStringImpl(JNIEnv *env, jobject obj) { try { - BTDevice *nativePtr = getJavaUplinkObject<BTDevice>(env, obj); - JavaGlobalObj::check(nativePtr->getJavaObject(), E_FILE_LINE); - return from_string_to_jstring(env, nativePtr->toString()); + shared_ptr_ref<BTDevice> device(env, obj); // hold until done + jau::JavaAnonRef device_java = device->getJavaObject(); // hold until done! + JavaGlobalObj::check(device_java, E_FILE_LINE); + return from_string_to_jstring(env, device->toString()); } catch(...) { rethrow_and_raise_java_exception(env); } @@ -178,34 +203,31 @@ jstring Java_jau_direct_1bt_DBTDevice_toStringImpl(JNIEnv *env, jobject obj) { jboolean Java_jau_direct_1bt_DBTDevice_addCharListener(JNIEnv *env, jobject obj, jobject listener, jobject jAssociatedCharacteristic) { try { + shared_ptr_ref<BTDevice> device(env, obj); // hold until done + jau::JavaAnonRef device_java = device->getJavaObject(); // hold until done! + JavaGlobalObj::check(device_java, E_FILE_LINE); + if( nullptr == listener ) { throw IllegalArgumentException("BTGattCharListener argument is null", E_FILE_LINE); } { - JNIGattCharListener * pre = - getObjectRef<JNIGattCharListener>(env, listener, "nativeInstance"); - if( nullptr != pre ) { + std::shared_ptr<JNIGattCharListener>* pre_orig = jau::getInstance<JNIGattCharListener>(env, listener, false /* throw_on_nullptr */); + if( !( nullptr == pre_orig || nullptr == *pre_orig ) ) { throw IllegalStateException("BTGattCharListener's nativeInstance not null, already in use", E_FILE_LINE); return false; } } - BTDevice *device = getJavaUplinkObject<BTDevice>(env, obj); - JavaGlobalObj::check(device->getJavaObject(), E_FILE_LINE); std::shared_ptr<BTGattHandler> gatt = device->getGattHandler(); if( nullptr == gatt ) { throw IllegalStateException("BTGattChar's device GATTHandle not connected: "+ device->toString(), E_FILE_LINE); } - BTGattChar * associatedCharacteristicRef = nullptr; - if( nullptr != jAssociatedCharacteristic ) { - associatedCharacteristicRef = getJavaUplinkObject<BTGattChar>(env, jAssociatedCharacteristic); - } - - std::shared_ptr<BTGattCharListener> l = - std::shared_ptr<BTGattCharListener>( new JNIGattCharListener(env, device, listener, associatedCharacteristicRef) ); + jau::shared_ptr_ref<BTGattChar> associatedCharacteristicRef(env, jAssociatedCharacteristic, false /* throw_on_nullptr */); - if( gatt->addCharListener(l) ) { - setInstance(env, listener, l.get()); + shared_ptr_ref<JNIGattCharListener> l( + new JNIGattCharListener(env, device.shared_ptr(), listener, associatedCharacteristicRef.shared_ptr()) ); + if( gatt->addCharListener( l.shared_ptr() ) ) { + l.release_into_object(env, listener); return JNI_TRUE; } } catch(...) { @@ -216,23 +238,24 @@ jboolean Java_jau_direct_1bt_DBTDevice_addCharListener(JNIEnv *env, jobject obj, jboolean Java_jau_direct_1bt_DBTDevice_removeCharListener(JNIEnv *env, jobject obj, jobject jlistener) { try { + shared_ptr_ref<BTDevice> device(env, obj, false /* throw_on_nullptr */); // hold until done + if( device.is_null() ) { + // OK to have device being deleted already @ shutdown + return JNI_FALSE; + } + jau::JavaAnonRef device_java = device->getJavaObject(); // hold until done! + JavaGlobalObj::check(device_java, E_FILE_LINE); + if( nullptr == jlistener ) { throw IllegalArgumentException("BTGattCharListener argument is null", E_FILE_LINE); } - JNIGattCharListener * pre = - getObjectRef<JNIGattCharListener>(env, jlistener, "nativeInstance"); - if( nullptr == pre ) { + shared_ptr_ref<JNIGattCharListener> pre( env, jlistener, false /* throw_on_nullptr */ ); // hold until done + if( pre.is_null() ) { WARN_PRINT("BTGattCharListener's nativeInstance is null, not in use"); return false; } - setObjectRef<JNIGattCharListener>(env, jlistener, nullptr, "nativeInstance"); + jau::clearInstance<JNIGattCharListener>(env, jlistener); - BTDevice *device = getJavaUplinkObjectUnchecked<BTDevice>(env, obj); - if( nullptr == device ) { - // OK to have device being deleted already @ shutdown - return 0; - } - JavaGlobalObj::check(device->getJavaObject(), E_FILE_LINE); std::shared_ptr<BTGattHandler> gatt = device->getGattHandler(); if( nullptr == gatt ) { // OK to have BTGattHandler being shutdown @ disable @@ -240,8 +263,8 @@ jboolean Java_jau_direct_1bt_DBTDevice_removeCharListener(JNIEnv *env, jobject o return false; } - if( ! gatt->removeCharListener(pre) ) { - WARN_PRINT("Failed to remove BTGattCharListener with nativeInstance: %p at %s", pre, device->toString().c_str()); + if( ! gatt->removeCharListener(pre.shared_ptr()) ) { + WARN_PRINT("Failed to remove BTGattCharListener with nativeInstance: %p at %s", pre.shared_ptr().get(), device->toString().c_str()); return false; } return true; @@ -253,15 +276,18 @@ jboolean Java_jau_direct_1bt_DBTDevice_removeCharListener(JNIEnv *env, jobject o jint Java_jau_direct_1bt_DBTDevice_removeAllAssociatedCharListener(JNIEnv *env, jobject obj, jobject jAssociatedCharacteristic) { try { - if( nullptr == jAssociatedCharacteristic ) { - throw IllegalArgumentException("associatedCharacteristic argument is null", E_FILE_LINE); - } - BTDevice *device = getJavaUplinkObjectUnchecked<BTDevice>(env, obj); - if( nullptr == device ) { + shared_ptr_ref<BTDevice> device(env, obj, false /* throw_on_nullptr */); // hold until done + if( device.is_null() ) { // OK to have device being deleted already @ shutdown return 0; } - JavaGlobalObj::check(device->getJavaObject(), E_FILE_LINE); + jau::JavaAnonRef device_java = device->getJavaObject(); // hold until done! + JavaGlobalObj::check(device_java, E_FILE_LINE); + + shared_ptr_ref<BTGattChar> associatedCharacteristicRef(env, jAssociatedCharacteristic); + jau::JavaAnonRef device_gattchar = associatedCharacteristicRef->getJavaObject(); // hold until done! + JavaGlobalObj::check(device_gattchar, E_FILE_LINE); + std::shared_ptr<BTGattHandler> gatt = device->getGattHandler(); if( nullptr == gatt ) { // OK to have BTGattHandler being shutdown @ disable @@ -269,10 +295,7 @@ jint Java_jau_direct_1bt_DBTDevice_removeAllAssociatedCharListener(JNIEnv *env, return 0; } - BTGattChar * associatedCharacteristicRef = getJavaUplinkObject<BTGattChar>(env, jAssociatedCharacteristic); - JavaGlobalObj::check(associatedCharacteristicRef->getJavaObject(), E_FILE_LINE); - - return gatt->removeAllAssociatedCharListener(associatedCharacteristicRef); + return gatt->removeAllAssociatedCharListener(associatedCharacteristicRef.shared_ptr()); } catch(...) { rethrow_and_raise_java_exception(env); } @@ -281,12 +304,14 @@ jint Java_jau_direct_1bt_DBTDevice_removeAllAssociatedCharListener(JNIEnv *env, jint Java_jau_direct_1bt_DBTDevice_removeAllCharListener(JNIEnv *env, jobject obj) { try { - BTDevice *device = getJavaUplinkObjectUnchecked<BTDevice>(env, obj); - if( nullptr == device ) { + shared_ptr_ref<BTDevice> device(env, obj, false /* throw_on_nullptr */); // hold until done + if( device.is_null() ) { // OK to have device being deleted already @ shutdown return 0; } - JavaGlobalObj::check(device->getJavaObject(), E_FILE_LINE); + jau::JavaAnonRef device_java = device->getJavaObject(); // hold until done! + JavaGlobalObj::check(device_java, E_FILE_LINE); + std::shared_ptr<BTGattHandler> gatt = device->getGattHandler(); if( nullptr == gatt ) { // OK to have BTGattHandler being shutdown @ disable @@ -300,24 +325,12 @@ jint Java_jau_direct_1bt_DBTDevice_removeAllCharListener(JNIEnv *env, jobject ob return 0; } -void Java_jau_direct_1bt_DBTDevice_deleteImpl(JNIEnv *env, jobject obj, jlong nativeInstance) -{ - (void)obj; - try { - BTDevice *device = castInstance<BTDevice>(nativeInstance); - DBG_PRINT("Java_jau_direct_1bt_DBTDevice_deleteImpl (remove only) %s", device->toString().c_str()); - device->remove(); - // No delete: BTDevice instance owned by BTAdapter - // However, device->remove() might issue destruction - } catch(...) { - rethrow_and_raise_java_exception(env); - } -} - jbyte Java_jau_direct_1bt_DBTDevice_getRoleImpl(JNIEnv *env, jobject obj) { try { - BTDevice *device = getJavaUplinkObject<BTDevice>(env, obj); + shared_ptr_ref<BTDevice> device(env, obj); // hold until done + jau::JavaAnonRef device_java = device->getJavaObject(); // hold until done! + JavaGlobalObj::check(device_java, E_FILE_LINE); return (jbyte) number( device->getRole() ); } catch(...) { rethrow_and_raise_java_exception(env); @@ -328,8 +341,9 @@ jbyte Java_jau_direct_1bt_DBTDevice_getRoleImpl(JNIEnv *env, jobject obj) jbyte Java_jau_direct_1bt_DBTDevice_getConnectedLE_1PHYImpl(JNIEnv *env, jobject obj, jbyteArray jresTx, jbyteArray jresRx) { try { - BTDevice *device = getJavaUplinkObject<BTDevice>(env, obj); - JavaGlobalObj::check(device->getJavaObject(), E_FILE_LINE); + shared_ptr_ref<BTDevice> device(env, obj); // hold until done + jau::JavaAnonRef device_java = device->getJavaObject(); // hold until done! + JavaGlobalObj::check(device_java, E_FILE_LINE); if( nullptr == jresTx ) { throw IllegalArgumentException("resTx byte array null", E_FILE_LINE); @@ -371,8 +385,9 @@ jbyte Java_jau_direct_1bt_DBTDevice_getConnectedLE_1PHYImpl(JNIEnv *env, jobject jbyte Java_jau_direct_1bt_DBTDevice_setConnectedLE_1PHYImpl(JNIEnv *env, jobject obj, jbyte jTx, jbyte jRx) { try { - BTDevice *device = getJavaUplinkObject<BTDevice>(env, obj); - JavaGlobalObj::check(device->getJavaObject(), E_FILE_LINE); + shared_ptr_ref<BTDevice> device(env, obj); // hold until done + jau::JavaAnonRef device_java = device->getJavaObject(); // hold until done! + JavaGlobalObj::check(device_java, E_FILE_LINE); const LE_PHYs Tx = static_cast<LE_PHYs>(jTx); const LE_PHYs Rx = static_cast<LE_PHYs>(jRx); @@ -386,8 +401,9 @@ jbyte Java_jau_direct_1bt_DBTDevice_setConnectedLE_1PHYImpl(JNIEnv *env, jobject jbyte Java_jau_direct_1bt_DBTDevice_getTxPhysImpl(JNIEnv *env, jobject obj) { try { - BTDevice *device = getJavaUplinkObject<BTDevice>(env, obj); - JavaGlobalObj::check(device->getJavaObject(), E_FILE_LINE); + shared_ptr_ref<BTDevice> device(env, obj); // hold until done + jau::JavaAnonRef device_java = device->getJavaObject(); // hold until done! + JavaGlobalObj::check(device_java, E_FILE_LINE); return number( device->getTxPhys() ); } catch(...) { @@ -398,8 +414,9 @@ jbyte Java_jau_direct_1bt_DBTDevice_getTxPhysImpl(JNIEnv *env, jobject obj) { jbyte Java_jau_direct_1bt_DBTDevice_getRxPhysImpl(JNIEnv *env, jobject obj) { try { - BTDevice *device = getJavaUplinkObject<BTDevice>(env, obj); - JavaGlobalObj::check(device->getJavaObject(), E_FILE_LINE); + shared_ptr_ref<BTDevice> device(env, obj); // hold until done + jau::JavaAnonRef device_java = device->getJavaObject(); // hold until done! + JavaGlobalObj::check(device_java, E_FILE_LINE); return number( device->getRxPhys() ); } catch(...) { @@ -411,8 +428,9 @@ jbyte Java_jau_direct_1bt_DBTDevice_getRxPhysImpl(JNIEnv *env, jobject obj) { jbyte Java_jau_direct_1bt_DBTDevice_disconnectImpl(JNIEnv *env, jobject obj) { try { - BTDevice *device = getJavaUplinkObject<BTDevice>(env, obj); - JavaGlobalObj::check(device->getJavaObject(), E_FILE_LINE); + shared_ptr_ref<BTDevice> device(env, obj); // hold until done + jau::JavaAnonRef device_java = device->getJavaObject(); // hold until done! + JavaGlobalObj::check(device_java, E_FILE_LINE); return (jint) number( device->disconnect() ); } catch(...) { rethrow_and_raise_java_exception(env); @@ -423,8 +441,9 @@ jbyte Java_jau_direct_1bt_DBTDevice_disconnectImpl(JNIEnv *env, jobject obj) jboolean Java_jau_direct_1bt_DBTDevice_removeImpl(JNIEnv *env, jobject obj) { try { - BTDevice *device = getJavaUplinkObject<BTDevice>(env, obj); - JavaGlobalObj::check(device->getJavaObject(), E_FILE_LINE); + shared_ptr_ref<BTDevice> device(env, obj); // hold until done + jau::JavaAnonRef device_java = device->getJavaObject(); // hold until done! + JavaGlobalObj::check(device_java, E_FILE_LINE); device->remove(); } catch(...) { rethrow_and_raise_java_exception(env); @@ -435,8 +454,9 @@ jboolean Java_jau_direct_1bt_DBTDevice_removeImpl(JNIEnv *env, jobject obj) jbyte Java_jau_direct_1bt_DBTDevice_connectDefaultImpl(JNIEnv *env, jobject obj) { try { - BTDevice *device = getJavaUplinkObject<BTDevice>(env, obj); - JavaGlobalObj::check(device->getJavaObject(), E_FILE_LINE); + shared_ptr_ref<BTDevice> device(env, obj); // hold until done + jau::JavaAnonRef device_java = device->getJavaObject(); // hold until done! + JavaGlobalObj::check(device_java, E_FILE_LINE); return (jbyte) number( device->connectDefault() ); } catch(...) { rethrow_and_raise_java_exception(env); @@ -447,8 +467,9 @@ jbyte Java_jau_direct_1bt_DBTDevice_connectDefaultImpl(JNIEnv *env, jobject obj) jbyte Java_jau_direct_1bt_DBTDevice_connectLEImpl0(JNIEnv *env, jobject obj) { try { - BTDevice *device = getJavaUplinkObject<BTDevice>(env, obj); - JavaGlobalObj::check(device->getJavaObject(), E_FILE_LINE); + shared_ptr_ref<BTDevice> device(env, obj); // hold until done + jau::JavaAnonRef device_java = device->getJavaObject(); // hold until done! + JavaGlobalObj::check(device_java, E_FILE_LINE); HCIStatusCode res = device->connectLE(); return (jbyte) number(res); } catch(...) { @@ -463,8 +484,9 @@ jbyte Java_jau_direct_1bt_DBTDevice_connectLEImpl1(JNIEnv *env, jobject obj, jshort latency, jshort timeout) { try { - BTDevice *device = getJavaUplinkObject<BTDevice>(env, obj); - JavaGlobalObj::check(device->getJavaObject(), E_FILE_LINE); + shared_ptr_ref<BTDevice> device(env, obj); // hold until done + jau::JavaAnonRef device_java = device->getJavaObject(); // hold until done! + JavaGlobalObj::check(device_java, E_FILE_LINE); HCIStatusCode res = device->connectLE(interval, window, min_interval, max_interval, latency, timeout); return (jbyte) number(res); } catch(...) { @@ -475,8 +497,9 @@ jbyte Java_jau_direct_1bt_DBTDevice_connectLEImpl1(JNIEnv *env, jobject obj, jbyte Java_jau_direct_1bt_DBTDevice_getAvailableSMPKeysImpl(JNIEnv *env, jobject obj, jboolean responder) { try { - BTDevice *device = getJavaUplinkObject<BTDevice>(env, obj); - JavaGlobalObj::check(device->getJavaObject(), E_FILE_LINE); + shared_ptr_ref<BTDevice> device(env, obj); // hold until done + jau::JavaAnonRef device_java = device->getJavaObject(); // hold until done! + JavaGlobalObj::check(device_java, E_FILE_LINE); return number( device->getAvailableSMPKeys(JNI_TRUE == responder) ); } catch(...) { @@ -487,8 +510,9 @@ jbyte Java_jau_direct_1bt_DBTDevice_getAvailableSMPKeysImpl(JNIEnv *env, jobject jbyte Java_jau_direct_1bt_DBTDevice_uploadKeysImpl(JNIEnv *env, jobject obj) { try { - BTDevice *device = getJavaUplinkObject<BTDevice>(env, obj); - JavaGlobalObj::check(device->getJavaObject(), E_FILE_LINE); + shared_ptr_ref<BTDevice> device(env, obj); // hold until done + jau::JavaAnonRef device_java = device->getJavaObject(); // hold until done! + JavaGlobalObj::check(device_java, E_FILE_LINE); return number( device->uploadKeys() ); } catch(...) { @@ -499,8 +523,9 @@ jbyte Java_jau_direct_1bt_DBTDevice_uploadKeysImpl(JNIEnv *env, jobject obj) { void Java_jau_direct_1bt_DBTDevice_getLongTermKeyImpl(JNIEnv *env, jobject obj, jboolean responder, jbyteArray jsink) { try { - BTDevice *device = getJavaUplinkObject<BTDevice>(env, obj); - JavaGlobalObj::check(device->getJavaObject(), E_FILE_LINE); + shared_ptr_ref<BTDevice> device(env, obj); // hold until done + jau::JavaAnonRef device_java = device->getJavaObject(); // hold until done! + JavaGlobalObj::check(device_java, E_FILE_LINE); if( nullptr == jsink ) { throw IllegalArgumentException("byte array null", E_FILE_LINE); @@ -523,8 +548,9 @@ void Java_jau_direct_1bt_DBTDevice_getLongTermKeyImpl(JNIEnv *env, jobject obj, void Java_jau_direct_1bt_DBTDevice_setLongTermKeyImpl(JNIEnv *env, jobject obj, jbyteArray jsource) { try { - BTDevice *device = getJavaUplinkObject<BTDevice>(env, obj); - JavaGlobalObj::check(device->getJavaObject(), E_FILE_LINE); + shared_ptr_ref<BTDevice> device(env, obj); // hold until done + jau::JavaAnonRef device_java = device->getJavaObject(); // hold until done! + JavaGlobalObj::check(device_java, E_FILE_LINE); if( nullptr == jsource ) { throw IllegalArgumentException("byte array null", E_FILE_LINE); @@ -548,8 +574,9 @@ void Java_jau_direct_1bt_DBTDevice_setLongTermKeyImpl(JNIEnv *env, jobject obj, void Java_jau_direct_1bt_DBTDevice_getIdentityResolvingKeyImpl(JNIEnv *env, jobject obj, jboolean responder, jbyteArray jsink) { try { - BTDevice *device = getJavaUplinkObject<BTDevice>(env, obj); - JavaGlobalObj::check(device->getJavaObject(), E_FILE_LINE); + shared_ptr_ref<BTDevice> device(env, obj); // hold until done + jau::JavaAnonRef device_java = device->getJavaObject(); // hold until done! + JavaGlobalObj::check(device_java, E_FILE_LINE); if( nullptr == jsink ) { throw IllegalArgumentException("byte array null", E_FILE_LINE); @@ -572,8 +599,9 @@ void Java_jau_direct_1bt_DBTDevice_getIdentityResolvingKeyImpl(JNIEnv *env, jobj void Java_jau_direct_1bt_DBTDevice_setIdentityResolvingKeyImpl(JNIEnv *env, jobject obj, jbyteArray jsource) { try { - BTDevice *device = getJavaUplinkObject<BTDevice>(env, obj); - JavaGlobalObj::check(device->getJavaObject(), E_FILE_LINE); + shared_ptr_ref<BTDevice> device(env, obj); // hold until done + jau::JavaAnonRef device_java = device->getJavaObject(); // hold until done! + JavaGlobalObj::check(device_java, E_FILE_LINE); if( nullptr == jsource ) { throw IllegalArgumentException("byte array null", E_FILE_LINE); @@ -597,8 +625,9 @@ void Java_jau_direct_1bt_DBTDevice_setIdentityResolvingKeyImpl(JNIEnv *env, jobj void Java_jau_direct_1bt_DBTDevice_getSignatureResolvingKeyImpl(JNIEnv *env, jobject obj, jboolean responder, jbyteArray jsink) { try { - BTDevice *device = getJavaUplinkObject<BTDevice>(env, obj); - JavaGlobalObj::check(device->getJavaObject(), E_FILE_LINE); + shared_ptr_ref<BTDevice> device(env, obj); // hold until done + jau::JavaAnonRef device_java = device->getJavaObject(); // hold until done! + JavaGlobalObj::check(device_java, E_FILE_LINE); if( nullptr == jsink ) { throw IllegalArgumentException("byte array null", E_FILE_LINE); @@ -621,8 +650,9 @@ void Java_jau_direct_1bt_DBTDevice_getSignatureResolvingKeyImpl(JNIEnv *env, job void Java_jau_direct_1bt_DBTDevice_setSignatureResolvingKeyImpl(JNIEnv *env, jobject obj, jbyteArray jsource) { try { - BTDevice *device = getJavaUplinkObject<BTDevice>(env, obj); - JavaGlobalObj::check(device->getJavaObject(), E_FILE_LINE); + shared_ptr_ref<BTDevice> device(env, obj); // hold until done + jau::JavaAnonRef device_java = device->getJavaObject(); // hold until done! + JavaGlobalObj::check(device_java, E_FILE_LINE); if( nullptr == jsource ) { throw IllegalArgumentException("byte array null", E_FILE_LINE); @@ -646,8 +676,9 @@ void Java_jau_direct_1bt_DBTDevice_setSignatureResolvingKeyImpl(JNIEnv *env, job void Java_jau_direct_1bt_DBTDevice_getLinkKeyImpl(JNIEnv *env, jobject obj, jboolean responder, jbyteArray jsink) { try { - BTDevice *device = getJavaUplinkObject<BTDevice>(env, obj); - JavaGlobalObj::check(device->getJavaObject(), E_FILE_LINE); + shared_ptr_ref<BTDevice> device(env, obj); // hold until done + jau::JavaAnonRef device_java = device->getJavaObject(); // hold until done! + JavaGlobalObj::check(device_java, E_FILE_LINE); if( nullptr == jsink ) { throw IllegalArgumentException("byte array null", E_FILE_LINE); @@ -670,8 +701,9 @@ void Java_jau_direct_1bt_DBTDevice_getLinkKeyImpl(JNIEnv *env, jobject obj, jboo void Java_jau_direct_1bt_DBTDevice_setLinkKeyImpl(JNIEnv *env, jobject obj, jbyteArray jsource) { try { - BTDevice *device = getJavaUplinkObject<BTDevice>(env, obj); - JavaGlobalObj::check(device->getJavaObject(), E_FILE_LINE); + shared_ptr_ref<BTDevice> device(env, obj); // hold until done + jau::JavaAnonRef device_java = device->getJavaObject(); // hold until done! + JavaGlobalObj::check(device_java, E_FILE_LINE); if( nullptr == jsource ) { throw IllegalArgumentException("byte array null", E_FILE_LINE); @@ -695,8 +727,9 @@ void Java_jau_direct_1bt_DBTDevice_setLinkKeyImpl(JNIEnv *env, jobject obj, jbyt jbyte Java_jau_direct_1bt_DBTDevice_unpairImpl(JNIEnv *env, jobject obj) { try { - BTDevice *device = getJavaUplinkObject<BTDevice>(env, obj); - JavaGlobalObj::check(device->getJavaObject(), E_FILE_LINE); + shared_ptr_ref<BTDevice> device(env, obj); // hold until done + jau::JavaAnonRef device_java = device->getJavaObject(); // hold until done! + JavaGlobalObj::check(device_java, E_FILE_LINE); HCIStatusCode res = device->unpair(); return (jbyte) number(res); } catch(...) { @@ -707,8 +740,9 @@ jbyte Java_jau_direct_1bt_DBTDevice_unpairImpl(JNIEnv *env, jobject obj) { jbyte Java_jau_direct_1bt_DBTDevice_getConnSecurityLevelImpl(JNIEnv *env, jobject obj) { try { - BTDevice *device = getJavaUplinkObject<BTDevice>(env, obj); - JavaGlobalObj::check(device->getJavaObject(), E_FILE_LINE); + shared_ptr_ref<BTDevice> device(env, obj); // hold until done + jau::JavaAnonRef device_java = device->getJavaObject(); // hold until done! + JavaGlobalObj::check(device_java, E_FILE_LINE); return number( device->getConnSecurityLevel() ); } catch(...) { @@ -719,8 +753,9 @@ jbyte Java_jau_direct_1bt_DBTDevice_getConnSecurityLevelImpl(JNIEnv *env, jobjec jbyte Java_jau_direct_1bt_DBTDevice_getConnIOCapabilityImpl(JNIEnv *env, jobject obj) { try { - BTDevice *device = getJavaUplinkObject<BTDevice>(env, obj); - JavaGlobalObj::check(device->getJavaObject(), E_FILE_LINE); + shared_ptr_ref<BTDevice> device(env, obj); // hold until done + jau::JavaAnonRef device_java = device->getJavaObject(); // hold until done! + JavaGlobalObj::check(device_java, E_FILE_LINE); return number( device->getConnIOCapability() ); } catch(...) { @@ -731,8 +766,9 @@ jbyte Java_jau_direct_1bt_DBTDevice_getConnIOCapabilityImpl(JNIEnv *env, jobject jboolean Java_jau_direct_1bt_DBTDevice_setConnSecurityImpl(JNIEnv *env, jobject obj, jbyte jsec_level, jbyte jio_cap) { try { - BTDevice *device = getJavaUplinkObject<BTDevice>(env, obj); - JavaGlobalObj::check(device->getJavaObject(), E_FILE_LINE); + shared_ptr_ref<BTDevice> device(env, obj); // hold until done + jau::JavaAnonRef device_java = device->getJavaObject(); // hold until done! + JavaGlobalObj::check(device_java, E_FILE_LINE); return device->setConnSecurity( to_BTSecurityLevel( static_cast<uint8_t>(jsec_level) ), to_SMPIOCapability( static_cast<uint8_t>(jio_cap) ) ); @@ -744,8 +780,9 @@ jboolean Java_jau_direct_1bt_DBTDevice_setConnSecurityImpl(JNIEnv *env, jobject jboolean Java_jau_direct_1bt_DBTDevice_setConnSecurityAutoImpl(JNIEnv *env, jobject obj, jbyte jio_cap) { try { - BTDevice *device = getJavaUplinkObject<BTDevice>(env, obj); - JavaGlobalObj::check(device->getJavaObject(), E_FILE_LINE); + shared_ptr_ref<BTDevice> device(env, obj); // hold until done + jau::JavaAnonRef device_java = device->getJavaObject(); // hold until done! + JavaGlobalObj::check(device_java, E_FILE_LINE); return device->setConnSecurityAuto( to_SMPIOCapability( static_cast<uint8_t>(jio_cap) ) ); } catch(...) { @@ -756,8 +793,9 @@ jboolean Java_jau_direct_1bt_DBTDevice_setConnSecurityAutoImpl(JNIEnv *env, jobj jboolean Java_jau_direct_1bt_DBTDevice_isConnSecurityAutoEnabled(JNIEnv *env, jobject obj) { try { - BTDevice *device = getJavaUplinkObject<BTDevice>(env, obj); - JavaGlobalObj::check(device->getJavaObject(), E_FILE_LINE); + shared_ptr_ref<BTDevice> device(env, obj); // hold until done + jau::JavaAnonRef device_java = device->getJavaObject(); // hold until done! + JavaGlobalObj::check(device_java, E_FILE_LINE); return device->isConnSecurityAutoEnabled(); } catch(...) { @@ -768,8 +806,9 @@ jboolean Java_jau_direct_1bt_DBTDevice_isConnSecurityAutoEnabled(JNIEnv *env, jo jbyte Java_jau_direct_1bt_DBTDevice_getPairingModeImpl(JNIEnv *env, jobject obj) { try { - BTDevice *device = getJavaUplinkObject<BTDevice>(env, obj); - JavaGlobalObj::check(device->getJavaObject(), E_FILE_LINE); + shared_ptr_ref<BTDevice> device(env, obj); // hold until done + jau::JavaAnonRef device_java = device->getJavaObject(); // hold until done! + JavaGlobalObj::check(device_java, E_FILE_LINE); return number( device->getPairingMode() ); } catch(...) { @@ -780,8 +819,9 @@ jbyte Java_jau_direct_1bt_DBTDevice_getPairingModeImpl(JNIEnv *env, jobject obj) jbyte Java_jau_direct_1bt_DBTDevice_getPairingStateImpl(JNIEnv *env, jobject obj) { try { - BTDevice *device = getJavaUplinkObject<BTDevice>(env, obj); - JavaGlobalObj::check(device->getJavaObject(), E_FILE_LINE); + shared_ptr_ref<BTDevice> device(env, obj); // hold until done + jau::JavaAnonRef device_java = device->getJavaObject(); // hold until done! + JavaGlobalObj::check(device_java, E_FILE_LINE); return static_cast<uint8_t>( device->getPairingState() ); } catch(...) { @@ -792,8 +832,9 @@ jbyte Java_jau_direct_1bt_DBTDevice_getPairingStateImpl(JNIEnv *env, jobject obj jbyte Java_jau_direct_1bt_DBTDevice_setPairingPasskeyImpl(JNIEnv *env, jobject obj, jint jpasskey) { try { - BTDevice *device = getJavaUplinkObject<BTDevice>(env, obj); - JavaGlobalObj::check(device->getJavaObject(), E_FILE_LINE); + shared_ptr_ref<BTDevice> device(env, obj); // hold until done + jau::JavaAnonRef device_java = device->getJavaObject(); // hold until done! + JavaGlobalObj::check(device_java, E_FILE_LINE); return number( device->setPairingPasskey( static_cast<uint32_t>(jpasskey) ) ); } catch(...) { @@ -804,8 +845,9 @@ jbyte Java_jau_direct_1bt_DBTDevice_setPairingPasskeyImpl(JNIEnv *env, jobject o jbyte Java_jau_direct_1bt_DBTDevice_setPairingPasskeyNegativeImpl(JNIEnv *env, jobject obj) { try { - BTDevice *device = getJavaUplinkObject<BTDevice>(env, obj); - JavaGlobalObj::check(device->getJavaObject(), E_FILE_LINE); + shared_ptr_ref<BTDevice> device(env, obj); // hold until done + jau::JavaAnonRef device_java = device->getJavaObject(); // hold until done! + JavaGlobalObj::check(device_java, E_FILE_LINE); return number( device->setPairingPasskeyNegative() ); } catch(...) { @@ -816,8 +858,9 @@ jbyte Java_jau_direct_1bt_DBTDevice_setPairingPasskeyNegativeImpl(JNIEnv *env, j jbyte Java_jau_direct_1bt_DBTDevice_setPairingNumericComparisonImpl(JNIEnv *env, jobject obj, jboolean jequal) { try { - BTDevice *device = getJavaUplinkObject<BTDevice>(env, obj); - JavaGlobalObj::check(device->getJavaObject(), E_FILE_LINE); + shared_ptr_ref<BTDevice> device(env, obj); // hold until done + jau::JavaAnonRef device_java = device->getJavaObject(); // hold until done! + JavaGlobalObj::check(device_java, E_FILE_LINE); return number( device->setPairingNumericComparison( JNI_TRUE == jequal ? true : false ) ); } catch(...) { @@ -834,8 +877,8 @@ static const std::string _serviceClazzCtorArgs("(JLjau/direct_bt/DBTDevice;ZLjav jobject Java_jau_direct_1bt_DBTDevice_getGattServicesImpl(JNIEnv *env, jobject obj) { try { - BTDevice *device = getJavaUplinkObject<BTDevice>(env, obj); - std::shared_ptr<JavaAnon> device_java = device->getJavaObject(); // hold until done! + shared_ptr_ref<BTDevice> device(env, obj); // hold until done + jau::JavaAnonRef device_java = device->getJavaObject(); // hold until done! JavaGlobalObj::check(device_java, E_FILE_LINE); jau::darray<BTGattServiceRef> services = device->getGattServices(); // implicit GATT connect and discovery if required incl GenericAccess retrieval @@ -846,14 +889,14 @@ jobject Java_jau_direct_1bt_DBTDevice_getGattServicesImpl(JNIEnv *env, jobject o // BTGattService(final long nativeInstance, final BTDevice device, final boolean isPrimary, // final String type_uuid, final short handleStart, final short handleEnd) - std::function<jobject(JNIEnv*, jclass, jmethodID, BTGattService*)> ctor_service = - [](JNIEnv *env_, jclass clazz, jmethodID clazz_ctor, BTGattService *service)->jobject { + std::function<jobject(JNIEnv*, jclass, jmethodID, const BTGattServiceRef&)> ctor_service = + [](JNIEnv *env_, jclass clazz, jmethodID clazz_ctor, const BTGattServiceRef& service)->jobject { // prepare adapter ctor std::shared_ptr<BTDevice> _device = service->getDeviceUnchecked(); if( nullptr == _device ) { throw jau::RuntimeException("Service's device null: "+service->toString(), E_FILE_LINE); } - std::shared_ptr<JavaAnon> _device_java = _device->getJavaObject(); // hold until done! + jau::JavaAnonRef _device_java = _device->getJavaObject(); // hold until done! JavaGlobalObj::check(_device_java, E_FILE_LINE); jobject jdevice = JavaGlobalObj::GetObject(_device_java); @@ -861,11 +904,12 @@ jobject Java_jau_direct_1bt_DBTDevice_getGattServicesImpl(JNIEnv *env, jobject o const jstring juuid = from_string_to_jstring(env_, service->type->toUUID128String()); java_exception_check_and_throw(env_, E_FILE_LINE); - jobject jservice = env_->NewObject(clazz, clazz_ctor, (jlong)service, jdevice, isPrimary, + shared_ptr_ref<BTGattService> service_sref(service); // new instance to be released into new jobject + jobject jservice = env_->NewObject(clazz, clazz_ctor, service_sref.release_to_jlong(), jdevice, isPrimary, juuid, service->handle, service->end_handle); java_exception_check_and_throw(env_, E_FILE_LINE); JNIGlobalRef::check(jservice, E_FILE_LINE); - std::shared_ptr<JavaAnon> jServiceRef = service->getJavaObject(); // GlobalRef + jau::JavaAnonRef jServiceRef = service->getJavaObject(); // GlobalRef JavaGlobalObj::check(jServiceRef, E_FILE_LINE); env_->DeleteLocalRef(juuid); env_->DeleteLocalRef(jservice); @@ -881,8 +925,9 @@ jobject Java_jau_direct_1bt_DBTDevice_getGattServicesImpl(JNIEnv *env, jobject o jboolean Java_jau_direct_1bt_DBTDevice_sendNotification(JNIEnv *env, jobject obj, jshort char_value_handle, jbyteArray jval) { try { - BTDevice *device = getJavaUplinkObject<BTDevice>(env, obj); - JavaGlobalObj::check(device->getJavaObject(), E_FILE_LINE); + shared_ptr_ref<BTDevice> device(env, obj); // hold until done + jau::JavaAnonRef device_java = device->getJavaObject(); // hold until done! + JavaGlobalObj::check(device_java, E_FILE_LINE); if( nullptr == jval ) { throw IllegalArgumentException("byte array null", E_FILE_LINE); @@ -907,8 +952,9 @@ jboolean Java_jau_direct_1bt_DBTDevice_sendNotification(JNIEnv *env, jobject obj jboolean Java_jau_direct_1bt_DBTDevice_sendIndication(JNIEnv *env, jobject obj, jshort char_value_handle, jbyteArray jval) { try { - BTDevice *device = getJavaUplinkObject<BTDevice>(env, obj); - JavaGlobalObj::check(device->getJavaObject(), E_FILE_LINE); + shared_ptr_ref<BTDevice> device(env, obj); // hold until done + jau::JavaAnonRef device_java = device->getJavaObject(); // hold until done! + JavaGlobalObj::check(device_java, E_FILE_LINE); if( nullptr == jval ) { throw IllegalArgumentException("byte array null", E_FILE_LINE); @@ -934,8 +980,9 @@ jboolean Java_jau_direct_1bt_DBTDevice_sendIndication(JNIEnv *env, jobject obj, jboolean Java_jau_direct_1bt_DBTDevice_pingGATTImpl(JNIEnv *env, jobject obj) { try { - BTDevice *device = getJavaUplinkObject<BTDevice>(env, obj); - JavaGlobalObj::check(device->getJavaObject(), E_FILE_LINE); + shared_ptr_ref<BTDevice> device(env, obj); // hold until done + jau::JavaAnonRef device_java = device->getJavaObject(); // hold until done! + JavaGlobalObj::check(device_java, E_FILE_LINE); return device->pingGATT() ? JNI_TRUE : JNI_FALSE; } catch(...) { @@ -947,8 +994,9 @@ jboolean Java_jau_direct_1bt_DBTDevice_pingGATTImpl(JNIEnv *env, jobject obj) jshort Java_jau_direct_1bt_DBTDevice_getRSSI(JNIEnv *env, jobject obj) { try { - BTDevice *device = getJavaUplinkObject<BTDevice>(env, obj); - JavaGlobalObj::check(device->getJavaObject(), E_FILE_LINE); + shared_ptr_ref<BTDevice> device(env, obj); // hold until done + jau::JavaAnonRef device_java = device->getJavaObject(); // hold until done! + JavaGlobalObj::check(device_java, E_FILE_LINE); return (jshort) device->getRSSI(); } catch(...) { rethrow_and_raise_java_exception(env); @@ -958,14 +1006,15 @@ jshort Java_jau_direct_1bt_DBTDevice_getRSSI(JNIEnv *env, jobject obj) void Java_jau_direct_1bt_DBTDevice_getEIRImpl(JNIEnv *env, jobject obj, jobject jeir_sink) { try { - BTDevice *device = getJavaUplinkObject<BTDevice>(env, obj); - std::shared_ptr<EInfoReport>* eir_sink_ptr_ref = jau::getInstance<std::shared_ptr<EInfoReport>>(env, jeir_sink); + shared_ptr_ref<BTDevice> device(env, obj); // hold until done + jau::JavaAnonRef device_java = device->getJavaObject(); // hold until done! + JavaGlobalObj::check(device_java, E_FILE_LINE); - std::shared_ptr<EInfoReport> eir = device->getEIR(); + shared_ptr_ref<EInfoReport> eir_sink(env, jeir_sink); // replace the shared managed object - *eir_sink_ptr_ref = eir; - + eir_sink = device->getEIR(); + eir_sink.release_into_object(env, jeir_sink); } catch(...) { rethrow_and_raise_java_exception(env); } @@ -973,14 +1022,15 @@ void Java_jau_direct_1bt_DBTDevice_getEIRImpl(JNIEnv *env, jobject obj, jobject void Java_jau_direct_1bt_DBTDevice_getEIRIndImpl(JNIEnv *env, jobject obj, jobject jeir_sink) { try { - BTDevice *device = getJavaUplinkObject<BTDevice>(env, obj); - std::shared_ptr<EInfoReport>* eir_sink_ptr_ref = jau::getInstance<std::shared_ptr<EInfoReport>>(env, jeir_sink); + shared_ptr_ref<BTDevice> device(env, obj); // hold until done + jau::JavaAnonRef device_java = device->getJavaObject(); // hold until done! + JavaGlobalObj::check(device_java, E_FILE_LINE); - std::shared_ptr<EInfoReport> eir = device->getEIRInd(); + shared_ptr_ref<EInfoReport> eir_sink(env, jeir_sink); // replace the shared managed object - *eir_sink_ptr_ref = eir; - + eir_sink = device->getEIRInd(); + eir_sink.release_into_object(env, jeir_sink); } catch(...) { rethrow_and_raise_java_exception(env); } @@ -988,14 +1038,15 @@ void Java_jau_direct_1bt_DBTDevice_getEIRIndImpl(JNIEnv *env, jobject obj, jobje void Java_jau_direct_1bt_DBTDevice_getEIRScanRspImpl(JNIEnv *env, jobject obj, jobject jeir_sink) { try { - BTDevice *device = getJavaUplinkObject<BTDevice>(env, obj); - std::shared_ptr<EInfoReport>* eir_sink_ptr_ref = jau::getInstance<std::shared_ptr<EInfoReport>>(env, jeir_sink); + shared_ptr_ref<BTDevice> device(env, obj); // hold until done + jau::JavaAnonRef device_java = device->getJavaObject(); // hold until done! + JavaGlobalObj::check(device_java, E_FILE_LINE); - std::shared_ptr<EInfoReport> eir = device->getEIRScanRsp(); + shared_ptr_ref<EInfoReport> eir_sink(env, jeir_sink); // replace the shared managed object - *eir_sink_ptr_ref = eir; - + eir_sink = device->getEIRScanRsp(); + eir_sink.release_into_object(env, jeir_sink); } catch(...) { rethrow_and_raise_java_exception(env); } @@ -1004,8 +1055,9 @@ void Java_jau_direct_1bt_DBTDevice_getEIRScanRspImpl(JNIEnv *env, jobject obj, j jshort Java_jau_direct_1bt_DBTDevice_getTxPower(JNIEnv *env, jobject obj) { try { - BTDevice *device = getJavaUplinkObject<BTDevice>(env, obj); - JavaGlobalObj::check(device->getJavaObject(), E_FILE_LINE); + shared_ptr_ref<BTDevice> device(env, obj); // hold until done + jau::JavaAnonRef device_java = device->getJavaObject(); // hold until done! + JavaGlobalObj::check(device_java, E_FILE_LINE); return (jshort) device->getTxPower(); } catch(...) { rethrow_and_raise_java_exception(env); diff --git a/java/jni/direct_bt/DBTGattChar.cxx b/java/jni/direct_bt/DBTGattChar.cxx index f99c5b0f..c438851a 100644 --- a/java/jni/direct_bt/DBTGattChar.cxx +++ b/java/jni/direct_bt/DBTGattChar.cxx @@ -36,34 +36,37 @@ using namespace direct_bt; using namespace jau; -jstring Java_jau_direct_1bt_DBTGattChar_toStringImpl(JNIEnv *env, jobject obj) { +void Java_jau_direct_1bt_DBTGattChar_deleteImpl(JNIEnv *env, jobject obj, jlong nativeInstance) { + (void)obj; try { - BTGattChar *nativePtr = getJavaUplinkObject<BTGattChar>(env, obj); - JavaGlobalObj::check(nativePtr->getJavaObject(), E_FILE_LINE); - return from_string_to_jstring(env, nativePtr->toString()); + jau::shared_ptr_ref<BTGattChar> sref(nativeInstance, false /* throw_on_nullptr */); // hold copy until done + if( nullptr != sref.pointer() ) { + std::shared_ptr<BTGattChar>* sref_ptr = jau::castInstance<BTGattChar>(nativeInstance); + delete sref_ptr; + } } catch(...) { rethrow_and_raise_java_exception(env); } - return nullptr; } -void Java_jau_direct_1bt_DBTGattChar_deleteImpl(JNIEnv *env, jobject obj, jlong nativeInstance) { - (void)obj; +jstring Java_jau_direct_1bt_DBTGattChar_toStringImpl(JNIEnv *env, jobject obj) { try { - BTGattChar *characteristic = castInstance<BTGattChar>(nativeInstance); - (void)characteristic; - // No delete: Service instance owned by BTGattService -> BTDevice + shared_ptr_ref<BTGattChar> characteristic(env, obj); // hold until done + jau::JavaAnonRef characteristic_java = characteristic->getJavaObject(); // hold until done! + JavaGlobalObj::check(characteristic_java, E_FILE_LINE); + return from_string_to_jstring(env, characteristic->toString()); } catch(...) { rethrow_and_raise_java_exception(env); } + return nullptr; } static const std::string _descriptorClazzCtorArgs("(JLjau/direct_bt/DBTGattChar;Ljava/lang/String;S[B)V"); jobject Java_jau_direct_1bt_DBTGattChar_getDescriptorsImpl(JNIEnv *env, jobject obj) { try { - BTGattChar *characteristic = getJavaUplinkObject<BTGattChar>(env, obj); - std::shared_ptr<JavaAnon> characteristic_java = characteristic->getJavaObject(); // hold until done! + shared_ptr_ref<BTGattChar> characteristic(env, obj); // hold until done + jau::JavaAnonRef characteristic_java = characteristic->getJavaObject(); // hold until done! JavaGlobalObj::check(characteristic_java, E_FILE_LINE); jau::darray<BTGattDescRef> & descriptorList = characteristic->descriptorList; @@ -74,14 +77,14 @@ jobject Java_jau_direct_1bt_DBTGattChar_getDescriptorsImpl(JNIEnv *env, jobject // BTGattDesc(final long nativeInstance, final BTGattChar characteristic, // final String type_uuid, final short handle, final byte[] value) - std::function<jobject(JNIEnv*, jclass, jmethodID, BTGattDesc *)> ctor_desc = - [](JNIEnv *env_, jclass clazz, jmethodID clazz_ctor, BTGattDesc *descriptor)->jobject { + std::function<jobject(JNIEnv*, jclass, jmethodID, const BTGattDescRef&)> ctor_desc = + [](JNIEnv *env_, jclass clazz, jmethodID clazz_ctor, const BTGattDescRef& descriptor)->jobject { // prepare adapter ctor std::shared_ptr<BTGattChar> _characteristic = descriptor->getGattCharUnchecked(); if( nullptr == _characteristic ) { throw jau::RuntimeException("Descriptor's characteristic null: "+descriptor->toString(), E_FILE_LINE); } - std::shared_ptr<JavaAnon> _characteristic_java = _characteristic->getJavaObject(); // hold until done! + jau::JavaAnonRef _characteristic_java = _characteristic->getJavaObject(); // hold until done! JavaGlobalObj::check(_characteristic_java, E_FILE_LINE); jobject jcharacteristic = JavaGlobalObj::GetObject(_characteristic_java); @@ -93,11 +96,12 @@ jobject Java_jau_direct_1bt_DBTGattChar_getDescriptorsImpl(JNIEnv *env, jobject env_->SetByteArrayRegion(jval, 0, (jsize)value_size, (const jbyte *)descriptor->value.get_ptr()); java_exception_check_and_throw(env_, E_FILE_LINE); - jobject jdesc = env_->NewObject(clazz, clazz_ctor, (jlong)descriptor, jcharacteristic, + shared_ptr_ref<BTGattDesc> descriptor_sref(descriptor); // new instance to be released into new jobject + jobject jdesc = env_->NewObject(clazz, clazz_ctor, descriptor_sref.release_to_jlong(), jcharacteristic, juuid, (jshort)descriptor->handle, jval); java_exception_check_and_throw(env_, E_FILE_LINE); JNIGlobalRef::check(jdesc, E_FILE_LINE); - std::shared_ptr<JavaAnon> jDescRef = descriptor->getJavaObject(); // GlobalRef + jau::JavaAnonRef jDescRef = descriptor->getJavaObject(); // GlobalRef JavaGlobalObj::check(jDescRef, E_FILE_LINE); env_->DeleteLocalRef(juuid); env_->DeleteLocalRef(jval); @@ -114,8 +118,9 @@ jobject Java_jau_direct_1bt_DBTGattChar_getDescriptorsImpl(JNIEnv *env, jobject jbyteArray Java_jau_direct_1bt_DBTGattChar_readValueImpl(JNIEnv *env, jobject obj) { try { - BTGattChar *characteristic = getJavaUplinkObject<BTGattChar>(env, obj); - JavaGlobalObj::check(characteristic->getJavaObject(), E_FILE_LINE); + shared_ptr_ref<BTGattChar> characteristic(env, obj); // hold until done + jau::JavaAnonRef characteristic_java = characteristic->getJavaObject(); // hold until done! + JavaGlobalObj::check(characteristic_java, E_FILE_LINE); POctets res(BTGattHandler::number(BTGattHandler::Defaults::MAX_ATT_MTU), 0, jau::endian::little); if( !characteristic->readValue(res) ) { @@ -137,6 +142,10 @@ jbyteArray Java_jau_direct_1bt_DBTGattChar_readValueImpl(JNIEnv *env, jobject ob jboolean Java_jau_direct_1bt_DBTGattChar_writeValueImpl(JNIEnv *env, jobject obj, jbyteArray jval, jboolean withResponse) { try { + shared_ptr_ref<BTGattChar> characteristic(env, obj); // hold until done + jau::JavaAnonRef characteristic_java = characteristic->getJavaObject(); // hold until done! + JavaGlobalObj::check(characteristic_java, E_FILE_LINE); + if( nullptr == jval ) { throw IllegalArgumentException("byte array null", E_FILE_LINE); } @@ -144,8 +153,6 @@ jboolean Java_jau_direct_1bt_DBTGattChar_writeValueImpl(JNIEnv *env, jobject obj if( 0 == value_size ) { return JNI_TRUE; } - BTGattChar *characteristic = getJavaUplinkObject<BTGattChar>(env, obj); - JavaGlobalObj::check(characteristic->getJavaObject(), E_FILE_LINE); JNICriticalArray<uint8_t, jbyteArray> criticalArray(env); // RAII - release uint8_t * value_ptr = criticalArray.get(jval, criticalArray.Mode::NO_UPDATE_AND_RELEASE); @@ -174,8 +181,8 @@ jboolean Java_jau_direct_1bt_DBTGattChar_writeValueImpl(JNIEnv *env, jobject obj jboolean Java_jau_direct_1bt_DBTGattChar_configNotificationIndicationImpl(JNIEnv *env, jobject obj, jboolean enableNotification, jboolean enableIndication, jbooleanArray jEnabledState) { try { - BTGattChar *characteristic = getJavaUplinkObjectUnchecked<BTGattChar>(env, obj); - if( nullptr == characteristic ) { + shared_ptr_ref<BTGattChar> characteristic(env, obj, false /* throw_on_nullptr */); // hold until done + if( characteristic.is_null() ) { if( !enableNotification && !enableIndication ) { // OK to have native characteristic being shutdown @ disable DBG_PRINT("Characteristic's native instance has been deleted"); @@ -183,7 +190,8 @@ jboolean Java_jau_direct_1bt_DBTGattChar_configNotificationIndicationImpl(JNIEnv } throw IllegalStateException("Characteristic's native instance deleted", E_FILE_LINE); } - JavaGlobalObj::check(characteristic->getJavaObject(), E_FILE_LINE); + jau::JavaAnonRef characteristic_java = characteristic->getJavaObject(); // hold until done! + JavaGlobalObj::check(characteristic_java, E_FILE_LINE); if( nullptr == jEnabledState ) { throw IllegalArgumentException("boolean array null", E_FILE_LINE); diff --git a/java/jni/direct_bt/DBTGattDesc.cxx b/java/jni/direct_bt/DBTGattDesc.cxx index d144aa17..ad12259c 100644 --- a/java/jni/direct_bt/DBTGattDesc.cxx +++ b/java/jni/direct_bt/DBTGattDesc.cxx @@ -37,11 +37,14 @@ using namespace direct_bt; using namespace jau; -void Java_jau_direct_1bt_DBTGattDesc_deleteImpl(JNIEnv *env, jobject obj) { +void Java_jau_direct_1bt_DBTGattDesc_deleteImpl(JNIEnv *env, jobject obj, jlong nativeInstance) { + (void)obj; try { - BTGattDesc *descriptor = getJavaUplinkObject<BTGattDesc>(env, obj); - (void)descriptor; - // No delete: Service instance owned by GATTService -> BTDevice + jau::shared_ptr_ref<BTGattDesc> sref(nativeInstance, false /* throw_on_nullptr */); // hold copy until done + if( nullptr != sref.pointer() ) { + std::shared_ptr<BTGattDesc>* sref_ptr = jau::castInstance<BTGattDesc>(nativeInstance); + delete sref_ptr; + } } catch(...) { rethrow_and_raise_java_exception(env); } @@ -50,8 +53,9 @@ void Java_jau_direct_1bt_DBTGattDesc_deleteImpl(JNIEnv *env, jobject obj) { jstring Java_jau_direct_1bt_DBTGattDesc_toStringImpl(JNIEnv *env, jobject obj) { (void)obj; try { - BTGattDesc *descriptor = getJavaUplinkObject<BTGattDesc>(env, obj); - JavaGlobalObj::check(descriptor->getJavaObject(), E_FILE_LINE); + shared_ptr_ref<BTGattDesc> descriptor(env, obj); // hold until done + jau::JavaAnonRef descriptor_java = descriptor->getJavaObject(); // hold until done! + JavaGlobalObj::check(descriptor_java, E_FILE_LINE); return from_string_to_jstring(env, descriptor->toString()); } catch(...) { rethrow_and_raise_java_exception(env); @@ -61,8 +65,9 @@ jstring Java_jau_direct_1bt_DBTGattDesc_toStringImpl(JNIEnv *env, jobject obj) { jbyteArray Java_jau_direct_1bt_DBTGattDesc_readValueImpl(JNIEnv *env, jobject obj) { try { - BTGattDesc *descriptor = getJavaUplinkObject<BTGattDesc>(env, obj); - JavaGlobalObj::check(descriptor->getJavaObject(), E_FILE_LINE); + shared_ptr_ref<BTGattDesc> descriptor(env, obj); // hold until done + jau::JavaAnonRef descriptor_java = descriptor->getJavaObject(); // hold until done! + JavaGlobalObj::check(descriptor_java, E_FILE_LINE); if( !descriptor->readValue() ) { ERR_PRINT("Characteristic readValue failed: %s", descriptor->toString().c_str()); @@ -82,6 +87,10 @@ jbyteArray Java_jau_direct_1bt_DBTGattDesc_readValueImpl(JNIEnv *env, jobject ob jboolean Java_jau_direct_1bt_DBTGattDesc_writeValueImpl(JNIEnv *env, jobject obj, jbyteArray jval) { try { + shared_ptr_ref<BTGattDesc> descriptor(env, obj); // hold until done + jau::JavaAnonRef descriptor_java = descriptor->getJavaObject(); // hold until done! + JavaGlobalObj::check(descriptor_java, E_FILE_LINE); + if( nullptr == jval ) { throw IllegalArgumentException("byte array null", E_FILE_LINE); } @@ -89,9 +98,6 @@ jboolean Java_jau_direct_1bt_DBTGattDesc_writeValueImpl(JNIEnv *env, jobject obj if( 0 == value_size ) { return JNI_TRUE; } - BTGattDesc *descriptor = getJavaUplinkObject<BTGattDesc>(env, obj); - JavaGlobalObj::check(descriptor->getJavaObject(), E_FILE_LINE); - JNICriticalArray<uint8_t, jbyteArray> criticalArray(env); // RAII - release uint8_t * value_ptr = criticalArray.get(jval, criticalArray.Mode::NO_UPDATE_AND_RELEASE); if( NULL == value_ptr ) { diff --git a/java/jni/direct_bt/DBTGattService.cxx b/java/jni/direct_bt/DBTGattService.cxx index b3c9c793..0873626f 100644 --- a/java/jni/direct_bt/DBTGattService.cxx +++ b/java/jni/direct_bt/DBTGattService.cxx @@ -38,37 +38,40 @@ using namespace direct_bt; using namespace jau; -jstring Java_jau_direct_1bt_DBTGattService_toStringImpl(JNIEnv *env, jobject obj) { +void Java_jau_direct_1bt_DBTGattService_deleteImpl(JNIEnv *env, jobject obj, jlong nativeInstance) { + (void)obj; try { - BTGattService *nativePtr = getJavaUplinkObject<BTGattService>(env, obj); - JavaGlobalObj::check(nativePtr->getJavaObject(), E_FILE_LINE); - return from_string_to_jstring(env, nativePtr->toString()); + jau::shared_ptr_ref<BTGattService> sref(nativeInstance, false /* throw_on_nullptr */); // hold copy until done + if( nullptr != sref.pointer() ) { + std::shared_ptr<BTGattService>* sref_ptr = jau::castInstance<BTGattService>(nativeInstance); + delete sref_ptr; + } } catch(...) { rethrow_and_raise_java_exception(env); } - return nullptr; } - -void Java_jau_direct_1bt_DBTGattService_deleteImpl(JNIEnv *env, jobject obj, jlong nativeInstance) { - (void)obj; +jstring Java_jau_direct_1bt_DBTGattService_toStringImpl(JNIEnv *env, jobject obj) { try { - BTGattService *service = castInstance<BTGattService>(nativeInstance); - (void)service; - // No delete: Service instance owned by BTDevice + shared_ptr_ref<BTGattService> service(env, obj); // hold until done + jau::JavaAnonRef service_java = service->getJavaObject(); // hold until done! + JavaGlobalObj::check(service_java, E_FILE_LINE); + return from_string_to_jstring(env, service->toString()); } catch(...) { rethrow_and_raise_java_exception(env); } + return nullptr; } + static const std::string _characteristicClazzCtorArgs("(JLjau/direct_bt/DBTGattService;SLorg/direct_bt/GattCharPropertySet;Ljava/lang/String;SII)V"); static const std::string _gattCharPropSetClassName("org/direct_bt/GattCharPropertySet"); static const std::string _gattCharPropSetClazzCtorArgs("(B)V"); jobject Java_jau_direct_1bt_DBTGattService_getCharsImpl(JNIEnv *env, jobject obj) { try { - BTGattService *service = getJavaUplinkObject<BTGattService>(env, obj); - std::shared_ptr<JavaAnon> service_java = service->getJavaObject(); // hold until done! + shared_ptr_ref<BTGattService> service(env, obj); // hold until done + jau::JavaAnonRef service_java = service->getJavaObject(); // hold until done! JavaGlobalObj::check(service_java, E_FILE_LINE); jau::darray<std::shared_ptr<BTGattChar>> & characteristics = service->characteristicList; @@ -96,14 +99,14 @@ jobject Java_jau_direct_1bt_DBTGattService_getCharsImpl(JNIEnv *env, jobject obj final int clientCharacteristicsConfigIndex, final int userDescriptionIndex) */ - std::function<jobject(JNIEnv*, jclass, jmethodID, BTGattChar *)> ctor_char = - [&gattCharPropSetClazz, &gattCharPropSetClazzCtor](JNIEnv *env_, jclass clazz, jmethodID clazz_ctor, BTGattChar *characteristic)->jobject { + std::function<jobject(JNIEnv*, jclass, jmethodID, const BTGattCharRef&)> ctor_char = + [&gattCharPropSetClazz, &gattCharPropSetClazzCtor](JNIEnv *env_, jclass clazz, jmethodID clazz_ctor, const BTGattCharRef& characteristic)->jobject { // prepare adapter ctor std::shared_ptr<BTGattService> _service = characteristic->getServiceUnchecked(); if( nullptr == _service ) { throw jau::RuntimeException("Characteristic's service null: "+characteristic->toString(), E_FILE_LINE); } - std::shared_ptr<JavaAnon> _service_java = _service->getJavaObject(); // hold until done! + jau::JavaAnonRef _service_java = _service->getJavaObject(); // hold until done! JavaGlobalObj::check(_service_java, E_FILE_LINE); jobject jservice = JavaGlobalObj::GetObject(_service_java); @@ -116,14 +119,15 @@ jobject Java_jau_direct_1bt_DBTGattService_getCharsImpl(JNIEnv *env, jobject obj const jstring uuid = from_string_to_jstring(env_, characteristic->value_type->toUUID128String()); java_exception_check_and_throw(env_, E_FILE_LINE); - jobject jcharVal = env_->NewObject(clazz, clazz_ctor, (jlong)characteristic, jservice, + shared_ptr_ref<BTGattChar> characteristic_sref(characteristic); // new instance to be released into new jobject + jobject jcharVal = env_->NewObject(clazz, clazz_ctor, characteristic_sref.release_to_jlong(), jservice, characteristic->handle, jGattCharPropSet, uuid, characteristic->value_handle, characteristic->clientCharConfigIndex, characteristic->userDescriptionIndex); java_exception_check_and_throw(env_, E_FILE_LINE); JNIGlobalRef::check(jcharVal, E_FILE_LINE); - std::shared_ptr<JavaAnon> jCharRef = characteristic->getJavaObject(); // GlobalRef + jau::JavaAnonRef jCharRef = characteristic->getJavaObject(); // GlobalRef JavaGlobalObj::check(jCharRef, E_FILE_LINE); env_->DeleteLocalRef(jGattCharPropSet); env_->DeleteLocalRef(jcharVal); diff --git a/java/jni/direct_bt/DBTManager.cxx b/java/jni/direct_bt/DBTManager.cxx index a8fa2f72..6d7f9137 100644 --- a/java/jni/direct_bt/DBTManager.cxx +++ b/java/jni/direct_bt/DBTManager.cxx @@ -109,12 +109,18 @@ static void _addMgmtCBOnce(JNIEnv *env, BTManager & mgmt, JNIGlobalRef jmgmtRef, } } +// special NOP deleted for shared_ptr<BTManager>: static singleton +static void nodelete_BTManager(BTManager* p) { (void)p; } + void Java_jau_direct_1bt_DBTManager_initImpl(JNIEnv *env, jobject obj) { try { - BTManager *manager = &BTManager::get(); // special: static singleton + std::shared_ptr<BTManager> manager( &BTManager::get(), nodelete_BTManager ); // special: static singleton jau::JNIGlobalRef global_obj(obj); // lock instance first (global reference), inserted below - setInstance<BTManager>(env, global_obj.getObject(), manager); + { + jau::shared_ptr_ref<BTManager> ref( manager ); + ref.release_into_object(env, global_obj.getObject()); + } java_exception_check_and_throw(env, E_FILE_LINE); manager->setJavaObject( std::make_shared<jau::JavaGlobalObj>( std::move(global_obj), nullptr ) ); JavaGlobalObj::check(manager->getJavaObject(), E_FILE_LINE); @@ -133,17 +139,23 @@ void Java_jau_direct_1bt_DBTManager_deleteImpl(JNIEnv *env, jobject obj, jlong n { (void)obj; try { - BTManager *manager = castInstance<BTManager>(nativeInstance); // special: static singleton - manager->close(); - manager->setJavaObject(); - (void) manager; + jau::shared_ptr_ref<BTManager> manager(nativeInstance, false /* throw_on_nullptr */); // hold copy until done + if( nullptr != manager.pointer() ) { + if( !manager.is_null() ) { + jau::JavaAnonRef manager_java = manager->getJavaObject(); // hold until done! + jau::JavaGlobalObj::check(manager_java, E_FILE_LINE); + manager->setJavaObject(); + } + std::shared_ptr<BTManager>* ref_ptr = jau::castInstance<BTManager>(nativeInstance); // special: static singleton + delete ref_ptr; // will leave singleton BTManager instance untouched -> nodelete_BTManager() + } } catch(...) { rethrow_and_raise_java_exception(env); } } static const std::string _adapterClazzCtorArgs("(J[BBLjava/lang/String;I)V"); -static jobject _createJavaAdapter(JNIEnv *env_, jclass clazz, jmethodID clazz_ctor, BTAdapter* adapter) { +static jobject _createJavaAdapter(JNIEnv *env_, jclass clazz, jmethodID clazz_ctor, const std::shared_ptr<BTAdapter>& adapter) { // prepare adapter ctor const EUI48 addr = adapter->getAddressAndType().address; jbyteArray jaddr = env_->NewByteArray(sizeof(addr)); @@ -151,26 +163,27 @@ static jobject _createJavaAdapter(JNIEnv *env_, jclass clazz, jmethodID clazz_ct jau::java_exception_check_and_throw(env_, E_FILE_LINE); const jstring name = from_string_to_jstring(env_, adapter->getName()); java_exception_check_and_throw(env_, E_FILE_LINE); - jobject jAdapter = env_->NewObject(clazz, clazz_ctor, (jlong)adapter, jaddr, adapter->getAddressAndType().type, name, adapter->dev_id); + jau::shared_ptr_ref<BTAdapter> adapter_ref( adapter ); + jobject jAdapter = env_->NewObject(clazz, clazz_ctor, adapter_ref.release_to_jlong(), jaddr, adapter->getAddressAndType().type, name, adapter->dev_id); java_exception_check_and_throw(env_, E_FILE_LINE); JNIGlobalRef::check(jAdapter, E_FILE_LINE); - std::shared_ptr<JavaAnon> jAdapterRef = adapter->getJavaObject(); // GlobalRef + jau::JavaAnonRef jAdapterRef = adapter->getJavaObject(); // GlobalRef JavaGlobalObj::check(jAdapterRef, E_FILE_LINE); env_->DeleteLocalRef(jaddr); env_->DeleteLocalRef(name); env_->DeleteLocalRef(jAdapter); - DBG_PRINT("Java_jau_direct_1bt_DBTManager_createJavaAdapter: New Adapter %p %s", adapter, adapter->toString().c_str()); + DBG_PRINT("Java_jau_direct_1bt_DBTManager_createJavaAdapter: New Adapter %p %s", adapter.get(), adapter->toString().c_str()); return JavaGlobalObj::GetObject(jAdapterRef); }; jobject Java_jau_direct_1bt_DBTManager_getAdapterListImpl(JNIEnv *env, jobject obj) { try { - BTManager *manager = getInstance<BTManager>(env, obj); - DBG_PRINT("Java_jau_direct_1bt_DBTManager_getAdapterListImpl: Manager %s", manager->toString().c_str()); + jau::shared_ptr_ref<BTManager> ref(env, obj); // hold until done + DBG_PRINT("Java_jau_direct_1bt_DBTManager_getAdapterListImpl: Manager %s", ref->toString().c_str()); - jau::darray<std::shared_ptr<BTAdapter>> adapters = manager->getAdapters(); + jau::darray<std::shared_ptr<BTAdapter>> adapters = ref->getAdapters(); return convert_vector_sharedptr_to_jarraylist<jau::darray<std::shared_ptr<BTAdapter>>, BTAdapter>( env, adapters, _adapterClazzCtorArgs.c_str(), _createJavaAdapter); } catch(...) { @@ -182,16 +195,16 @@ jobject Java_jau_direct_1bt_DBTManager_getAdapterListImpl(JNIEnv *env, jobject o jobject Java_jau_direct_1bt_DBTManager_getAdapterImpl(JNIEnv *env, jobject obj, jint dev_id) { try { - BTManager *manager = getInstance<BTManager>(env, obj); + jau::shared_ptr_ref<BTManager> ref(env, obj); // hold until done - std::shared_ptr<BTAdapter> adapter = manager->getAdapter(dev_id); + std::shared_ptr<BTAdapter> adapter = ref->getAdapter(dev_id); if( nullptr == adapter ) { ERR_PRINT("BTManager::getAdapterImpl: Adapter dev_id %d: Not found", dev_id); return nullptr; } DBG_PRINT("BTManager::getAdapterImpl: Adapter dev_id %d: %s", dev_id, adapter->toString().c_str()); - return jau::convert_instance_to_jobject<BTAdapter>(env, adapter.get(), _adapterClazzCtorArgs.c_str(), _createJavaAdapter); + return jau::convert_instance_to_jobject<BTAdapter>(env, adapter, _adapterClazzCtorArgs.c_str(), _createJavaAdapter); } catch(...) { rethrow_and_raise_java_exception(env); } diff --git a/java/jni/direct_bt/EInfoReport.cxx b/java/jni/direct_bt/EInfoReport.cxx index 75dd5965..47a69981 100644 --- a/java/jni/direct_bt/EInfoReport.cxx +++ b/java/jni/direct_bt/EInfoReport.cxx @@ -44,10 +44,9 @@ jlong Java_org_direct_1bt_EInfoReport_ctorImpl1(JNIEnv *env, jobject obj) { try { (void)obj; // new instance - std::shared_ptr<EInfoReport> ref = std::make_shared<EInfoReport>(); + jau::shared_ptr_ref<EInfoReport> ref( new EInfoReport() ); - std::shared_ptr<EInfoReport> * ref_ptr = new std::shared_ptr<EInfoReport>(std::move(ref)); - return (jlong)(intptr_t)ref_ptr; + return ref.release_to_jlong(); } catch(...) { rethrow_and_raise_java_exception(env); } @@ -62,18 +61,8 @@ jlong Java_org_direct_1bt_EInfoReport_ctorImpl1(JNIEnv *env, jobject obj) { jlong Java_org_direct_1bt_EInfoReport_ctorImpl2(JNIEnv *env, jobject obj, jlong nativeInstanceOther) { try { (void)obj; - std::shared_ptr<EInfoReport> * ref_ptr_other = reinterpret_cast<std::shared_ptr<EInfoReport> *>(nativeInstanceOther); - if (ref_ptr_other == nullptr) { - throw jau::RuntimeException("Given std::shared_ptr<EInfoReport> * nullptr", E_FILE_LINE); - } - if (*ref_ptr_other == nullptr) { - throw jau::RuntimeException("Given std::shared_ptr<EInfoReport> nullptr", E_FILE_LINE); - } - // shared instance with other - std::shared_ptr<EInfoReport> ref( *ref_ptr_other ); - - std::shared_ptr<EInfoReport> * ref_ptr = new std::shared_ptr<EInfoReport>(std::move(ref)); - return (jlong)(intptr_t)ref_ptr; + jau::shared_ptr_ref<EInfoReport> ref_other_cpy(nativeInstanceOther); + return ref_other_cpy.release_to_jlong(); } catch(...) { rethrow_and_raise_java_exception(env); } @@ -82,17 +71,12 @@ jlong Java_org_direct_1bt_EInfoReport_ctorImpl2(JNIEnv *env, jobject obj, jlong void Java_org_direct_1bt_EInfoReport_replace_nativeImpl(JNIEnv *env, jobject obj, jlong nativeInstanceOther) { try { - std::shared_ptr<EInfoReport> * ref_ptr_other = reinterpret_cast<std::shared_ptr<EInfoReport> *>(nativeInstanceOther); - if (ref_ptr_other == nullptr) { - throw jau::RuntimeException("Given std::shared_ptr<EInfoReport> * nullptr", E_FILE_LINE); - } - if (*ref_ptr_other == nullptr) { - throw jau::RuntimeException("Given std::shared_ptr<EInfoReport> nullptr", E_FILE_LINE); - } - std::shared_ptr<EInfoReport>* eir_ptr_ref = jau::getInstance<std::shared_ptr<EInfoReport>>(env, obj); + jau::shared_ptr_ref<EInfoReport> ref_other(nativeInstanceOther); + jau::shared_ptr_ref<EInfoReport> ref(env, obj); // replace the shared managed object - *eir_ptr_ref = *ref_ptr_other; + ref = ref_other.shared_ptr(); + ref.release_into_object(env, obj); } catch(...) { rethrow_and_raise_java_exception(env); } @@ -106,9 +90,10 @@ void Java_org_direct_1bt_EInfoReport_replace_nativeImpl(JNIEnv *env, jobject obj void Java_org_direct_1bt_EInfoReport_dtorImpl(JNIEnv *env, jclass clazz, jlong nativeInstance) { (void)clazz; try { - if( 0 != nativeInstance ) { - std::shared_ptr<EInfoReport> * ref_ptr = reinterpret_cast<std::shared_ptr<EInfoReport> *>(nativeInstance); - delete ref_ptr; + jau::shared_ptr_ref<EInfoReport> sref(nativeInstance, false /* throw_on_nullptr */); // hold copy until done + if( nullptr != sref.pointer() ) { + std::shared_ptr<EInfoReport>* sref_ptr = jau::castInstance<EInfoReport>(nativeInstance); + delete sref_ptr; } } catch(...) { rethrow_and_raise_java_exception(env); @@ -117,8 +102,8 @@ void Java_org_direct_1bt_EInfoReport_dtorImpl(JNIEnv *env, jclass clazz, jlong n void Java_org_direct_1bt_EInfoReport_clear(JNIEnv *env, jobject obj) { try { - std::shared_ptr<EInfoReport>& eir_ptr = *jau::getInstance<std::shared_ptr<EInfoReport>>(env, obj); - eir_ptr->clear(); + jau::shared_ptr_ref<EInfoReport> ref(env, obj); // hold until done + ref->clear(); } catch(...) { rethrow_and_raise_java_exception(env); } @@ -126,9 +111,9 @@ void Java_org_direct_1bt_EInfoReport_clear(JNIEnv *env, jobject obj) { jint Java_org_direct_1bt_EInfoReport_setImpl(JNIEnv *env, jobject obj, jobject jeir_other) { try { - std::shared_ptr<EInfoReport>& eir_ptr = *jau::getInstance<std::shared_ptr<EInfoReport>>(env, obj); - std::shared_ptr<EInfoReport>& eir_other_ptr = *jau::getInstance<std::shared_ptr<EInfoReport>>(env, jeir_other); - return static_cast<jint>( number( eir_ptr->set(*eir_other_ptr) ) ); + jau::shared_ptr_ref<EInfoReport> ref(env, obj); // hold until done + jau::shared_ptr_ref<EInfoReport> ref_other(env, jeir_other); // hold until done + return static_cast<jint>( number( ref->set(*ref_other) ) ); } catch(...) { rethrow_and_raise_java_exception(env); } @@ -142,8 +127,8 @@ jint Java_org_direct_1bt_EInfoReport_setImpl(JNIEnv *env, jobject obj, jobject j */ void Java_org_direct_1bt_EInfoReport_setAddressTypeImpl(JNIEnv *env, jobject obj, jbyte jat) { try { - std::shared_ptr<EInfoReport>& eir_ptr = *jau::getInstance<std::shared_ptr<EInfoReport>>(env, obj); - eir_ptr->setAddressType(static_cast<BDAddressType>(jat)); + jau::shared_ptr_ref<EInfoReport> ref(env, obj); // hold until done + ref->setAddressType(static_cast<BDAddressType>(jat)); } catch(...) { rethrow_and_raise_java_exception(env); } @@ -156,7 +141,7 @@ void Java_org_direct_1bt_EInfoReport_setAddressTypeImpl(JNIEnv *env, jobject obj */ void Java_org_direct_1bt_EInfoReport_setAddressImpl(JNIEnv *env, jobject obj, jbyteArray jaddress) { try { - std::shared_ptr<EInfoReport>& eir_ptr = *jau::getInstance<std::shared_ptr<EInfoReport>>(env, obj); + jau::shared_ptr_ref<EInfoReport> ref(env, obj); // hold until done if( nullptr == jaddress ) { throw jau::IllegalArgumentException("address null", E_FILE_LINE); @@ -172,7 +157,7 @@ void Java_org_direct_1bt_EInfoReport_setAddressImpl(JNIEnv *env, jobject obj, jb } const EUI48& address = *reinterpret_cast<EUI48 *>(address_ptr); - eir_ptr->setAddress(address); + ref->setAddress(address); } catch(...) { rethrow_and_raise_java_exception(env); } @@ -185,8 +170,8 @@ void Java_org_direct_1bt_EInfoReport_setAddressImpl(JNIEnv *env, jobject obj, jb */ void Java_org_direct_1bt_EInfoReport_setRSSI(JNIEnv *env, jobject obj, jbyte jrssi) { try { - std::shared_ptr<EInfoReport>& eir_ptr = *jau::getInstance<std::shared_ptr<EInfoReport>>(env, obj); - eir_ptr->setRSSI(static_cast<int8_t>(jrssi)); + jau::shared_ptr_ref<EInfoReport> ref(env, obj); // hold until done + ref->setRSSI(static_cast<int8_t>(jrssi)); } catch(...) { rethrow_and_raise_java_exception(env); } @@ -199,8 +184,8 @@ void Java_org_direct_1bt_EInfoReport_setRSSI(JNIEnv *env, jobject obj, jbyte jrs */ void Java_org_direct_1bt_EInfoReport_setTxPower(JNIEnv *env, jobject obj, jbyte jtxp) { try { - std::shared_ptr<EInfoReport>& eir_ptr = *jau::getInstance<std::shared_ptr<EInfoReport>>(env, obj); - eir_ptr->setTxPower(static_cast<int8_t>(jtxp)); + jau::shared_ptr_ref<EInfoReport> ref(env, obj); // hold until done + ref->setTxPower(static_cast<int8_t>(jtxp)); } catch(...) { rethrow_and_raise_java_exception(env); } @@ -213,8 +198,8 @@ void Java_org_direct_1bt_EInfoReport_setTxPower(JNIEnv *env, jobject obj, jbyte */ void Java_org_direct_1bt_EInfoReport_setFlagsImpl(JNIEnv *env, jobject obj, jbyte jf) { try { - std::shared_ptr<EInfoReport>& eir_ptr = *jau::getInstance<std::shared_ptr<EInfoReport>>(env, obj); - eir_ptr->setFlags(static_cast<GAPFlags>(jf)); + jau::shared_ptr_ref<EInfoReport> ref(env, obj); // hold until done + ref->setFlags(static_cast<GAPFlags>(jf)); } catch(...) { rethrow_and_raise_java_exception(env); } @@ -227,8 +212,8 @@ void Java_org_direct_1bt_EInfoReport_setFlagsImpl(JNIEnv *env, jobject obj, jbyt */ void Java_org_direct_1bt_EInfoReport_addFlagImpl(JNIEnv *env, jobject obj, jbyte jf) { try { - std::shared_ptr<EInfoReport>& eir_ptr = *jau::getInstance<std::shared_ptr<EInfoReport>>(env, obj); - eir_ptr->addFlags(static_cast<GAPFlags>(jf)); + jau::shared_ptr_ref<EInfoReport> ref(env, obj); // hold until done + ref->addFlags(static_cast<GAPFlags>(jf)); } catch(...) { rethrow_and_raise_java_exception(env); } @@ -241,9 +226,9 @@ void Java_org_direct_1bt_EInfoReport_addFlagImpl(JNIEnv *env, jobject obj, jbyte */ void Java_org_direct_1bt_EInfoReport_setName(JNIEnv *env, jobject obj, jstring jname) { try { - std::shared_ptr<EInfoReport>& eir_ptr = *jau::getInstance<std::shared_ptr<EInfoReport>>(env, obj); + jau::shared_ptr_ref<EInfoReport> ref(env, obj); // hold until done std::string name = jau::from_jstring_to_string(env, jname); - eir_ptr->setName(name); + ref->setName(name); } catch(...) { rethrow_and_raise_java_exception(env); } @@ -256,9 +241,9 @@ void Java_org_direct_1bt_EInfoReport_setName(JNIEnv *env, jobject obj, jstring j */ void Java_org_direct_1bt_EInfoReport_setShortName(JNIEnv *env, jobject obj, jstring jsname) { try { - std::shared_ptr<EInfoReport>& eir_ptr = *jau::getInstance<std::shared_ptr<EInfoReport>>(env, obj); + jau::shared_ptr_ref<EInfoReport> ref(env, obj); // hold until done std::string sname = jau::from_jstring_to_string(env, jsname); - eir_ptr->setShortName(sname); + ref->setShortName(sname); } catch(...) { rethrow_and_raise_java_exception(env); } @@ -271,10 +256,10 @@ void Java_org_direct_1bt_EInfoReport_setShortName(JNIEnv *env, jobject obj, jstr */ void Java_org_direct_1bt_EInfoReport_addService(JNIEnv *env, jobject obj, jstring juuid) { try { - std::shared_ptr<EInfoReport>& eir_ptr = *jau::getInstance<std::shared_ptr<EInfoReport>>(env, obj); + jau::shared_ptr_ref<EInfoReport> ref(env, obj); // hold until done std::string uuid_s = jau::from_jstring_to_string(env, juuid); std::shared_ptr<const jau::uuid_t> uuid = jau::uuid_t::create(uuid_s); - eir_ptr->addService(uuid); + ref->addService(uuid); } catch(...) { rethrow_and_raise_java_exception(env); } @@ -287,8 +272,8 @@ void Java_org_direct_1bt_EInfoReport_addService(JNIEnv *env, jobject obj, jstrin */ void Java_org_direct_1bt_EInfoReport_setServicesComplete(JNIEnv *env, jobject obj, jboolean jv) { try { - std::shared_ptr<EInfoReport>& eir_ptr = *jau::getInstance<std::shared_ptr<EInfoReport>>(env, obj); - eir_ptr->setServicesComplete(JNI_TRUE==jv); + jau::shared_ptr_ref<EInfoReport> ref(env, obj); // hold until done + ref->setServicesComplete(JNI_TRUE==jv); } catch(...) { rethrow_and_raise_java_exception(env); } @@ -301,8 +286,8 @@ void Java_org_direct_1bt_EInfoReport_setServicesComplete(JNIEnv *env, jobject ob */ void Java_org_direct_1bt_EInfoReport_setDeviceClass(JNIEnv *env, jobject obj, jint jv) { try { - std::shared_ptr<EInfoReport>& eir_ptr = *jau::getInstance<std::shared_ptr<EInfoReport>>(env, obj); - eir_ptr->setDeviceClass(static_cast<uint32_t>(jv)); + jau::shared_ptr_ref<EInfoReport> ref(env, obj); // hold until done + ref->setDeviceClass(static_cast<uint32_t>(jv)); } catch(...) { rethrow_and_raise_java_exception(env); } @@ -315,8 +300,8 @@ void Java_org_direct_1bt_EInfoReport_setDeviceClass(JNIEnv *env, jobject obj, ji */ void Java_org_direct_1bt_EInfoReport_setDeviceID(JNIEnv *env, jobject obj, jshort jsource, jshort jvendor, jshort jproduct, jshort jversion) { try { - std::shared_ptr<EInfoReport>& eir_ptr = *jau::getInstance<std::shared_ptr<EInfoReport>>(env, obj); - eir_ptr->setDeviceID(static_cast<uint16_t>(jsource), static_cast<uint16_t>(jvendor), static_cast<uint16_t>(jproduct), static_cast<uint16_t>(jversion)); + jau::shared_ptr_ref<EInfoReport> ref(env, obj); // hold until done + ref->setDeviceID(static_cast<uint16_t>(jsource), static_cast<uint16_t>(jvendor), static_cast<uint16_t>(jproduct), static_cast<uint16_t>(jversion)); } catch(...) { rethrow_and_raise_java_exception(env); } @@ -329,8 +314,8 @@ void Java_org_direct_1bt_EInfoReport_setDeviceID(JNIEnv *env, jobject obj, jshor */ void Java_org_direct_1bt_EInfoReport_setConnInterval(JNIEnv *env, jobject obj, jshort jmin, jshort jmax) { try { - std::shared_ptr<EInfoReport>& eir_ptr = *jau::getInstance<std::shared_ptr<EInfoReport>>(env, obj); - eir_ptr->setConnInterval(static_cast<uint16_t>(jmin), static_cast<uint16_t>(jmax)); + jau::shared_ptr_ref<EInfoReport> ref(env, obj); // hold until done + ref->setConnInterval(static_cast<uint16_t>(jmin), static_cast<uint16_t>(jmax)); } catch(...) { rethrow_and_raise_java_exception(env); } @@ -343,8 +328,8 @@ void Java_org_direct_1bt_EInfoReport_setConnInterval(JNIEnv *env, jobject obj, j */ jlong Java_org_direct_1bt_EInfoReport_getTimestamp(JNIEnv *env, jobject obj) { try { - std::shared_ptr<EInfoReport>& eir_ptr = *jau::getInstance<std::shared_ptr<EInfoReport>>(env, obj); - return static_cast<jlong>( eir_ptr->getTimestamp() ); + jau::shared_ptr_ref<EInfoReport> ref(env, obj); // hold until done + return static_cast<jlong>( ref->getTimestamp() ); } catch(...) { rethrow_and_raise_java_exception(env); } @@ -358,8 +343,8 @@ jlong Java_org_direct_1bt_EInfoReport_getTimestamp(JNIEnv *env, jobject obj) { */ jint Java_org_direct_1bt_EInfoReport_getEIRDataMaskImpl(JNIEnv *env, jobject obj) { try { - std::shared_ptr<EInfoReport>& eir_ptr = *jau::getInstance<std::shared_ptr<EInfoReport>>(env, obj); - return static_cast<jint>( number( eir_ptr->getEIRDataMask() ) ); + jau::shared_ptr_ref<EInfoReport> ref(env, obj); // hold until done + return static_cast<jint>( number( ref->getEIRDataMask() ) ); } catch(...) { rethrow_and_raise_java_exception(env); } @@ -368,8 +353,8 @@ jint Java_org_direct_1bt_EInfoReport_getEIRDataMaskImpl(JNIEnv *env, jobject obj jint Java_org_direct_1bt_EInfoReport_getSourceImpl(JNIEnv *env, jobject obj) { try { - std::shared_ptr<EInfoReport>& eir_ptr = *jau::getInstance<std::shared_ptr<EInfoReport>>(env, obj); - return static_cast<jint>( EInfoReport::number( eir_ptr->getSource() ) ); + jau::shared_ptr_ref<EInfoReport> ref(env, obj); // hold until done + return static_cast<jint>( EInfoReport::number( ref->getSource() ) ); } catch(...) { rethrow_and_raise_java_exception(env); } @@ -383,8 +368,8 @@ jint Java_org_direct_1bt_EInfoReport_getSourceImpl(JNIEnv *env, jobject obj) { */ jbyte Java_org_direct_1bt_EInfoReport_getFlagsImpl(JNIEnv *env, jobject obj) { try { - std::shared_ptr<EInfoReport>& eir_ptr = *jau::getInstance<std::shared_ptr<EInfoReport>>(env, obj); - return static_cast<jbyte>( number( eir_ptr->getFlags() ) ); + jau::shared_ptr_ref<EInfoReport> ref(env, obj); // hold until done + return static_cast<jbyte>( number( ref->getFlags() ) ); } catch(...) { rethrow_and_raise_java_exception(env); } @@ -398,8 +383,8 @@ jbyte Java_org_direct_1bt_EInfoReport_getFlagsImpl(JNIEnv *env, jobject obj) { */ jbyte Java_org_direct_1bt_EInfoReport_getADAddressType(JNIEnv *env, jobject obj) { try { - std::shared_ptr<EInfoReport>& eir_ptr = *jau::getInstance<std::shared_ptr<EInfoReport>>(env, obj); - return static_cast<jbyte>( eir_ptr->getADAddressType() ); + jau::shared_ptr_ref<EInfoReport> ref(env, obj); // hold until done + return static_cast<jbyte>( ref->getADAddressType() ); } catch(...) { rethrow_and_raise_java_exception(env); } @@ -413,8 +398,8 @@ jbyte Java_org_direct_1bt_EInfoReport_getADAddressType(JNIEnv *env, jobject obj) */ jbyte Java_org_direct_1bt_EInfoReport_getAddressTypeImpl(JNIEnv *env, jobject obj) { try { - std::shared_ptr<EInfoReport>& eir_ptr = *jau::getInstance<std::shared_ptr<EInfoReport>>(env, obj); - return static_cast<jbyte>( number( eir_ptr->getAddressType() ) ); + jau::shared_ptr_ref<EInfoReport> ref(env, obj); // hold until done + return static_cast<jbyte>( number( ref->getAddressType() ) ); } catch(...) { rethrow_and_raise_java_exception(env); } @@ -428,8 +413,8 @@ jbyte Java_org_direct_1bt_EInfoReport_getAddressTypeImpl(JNIEnv *env, jobject ob */ jbyteArray Java_org_direct_1bt_EInfoReport_getAddressImpl(JNIEnv *env, jobject obj) { try { - std::shared_ptr<EInfoReport>& eir_ptr = *jau::getInstance<std::shared_ptr<EInfoReport>>(env, obj); - const EUI48 & addr = eir_ptr->getAddress(); + jau::shared_ptr_ref<EInfoReport> ref(env, obj); // hold until done + const EUI48 & addr = ref->getAddress(); jbyteArray jaddr = env->NewByteArray(sizeof(addr)); env->SetByteArrayRegion(jaddr, 0, sizeof(addr), (const jbyte*)(addr.b)); return jaddr; @@ -446,8 +431,8 @@ jbyteArray Java_org_direct_1bt_EInfoReport_getAddressImpl(JNIEnv *env, jobject o */ jstring Java_org_direct_1bt_EInfoReport_getName(JNIEnv *env, jobject obj) { try { - std::shared_ptr<EInfoReport>& eir_ptr = *jau::getInstance<std::shared_ptr<EInfoReport>>(env, obj); - return jau::from_string_to_jstring(env, eir_ptr->getName()); + jau::shared_ptr_ref<EInfoReport> ref(env, obj); // hold until done + return jau::from_string_to_jstring(env, ref->getName()); } catch(...) { rethrow_and_raise_java_exception(env); } @@ -461,8 +446,8 @@ jstring Java_org_direct_1bt_EInfoReport_getName(JNIEnv *env, jobject obj) { */ jstring Java_org_direct_1bt_EInfoReport_getShortName(JNIEnv *env, jobject obj) { try { - std::shared_ptr<EInfoReport>& eir_ptr = *jau::getInstance<std::shared_ptr<EInfoReport>>(env, obj); - return jau::from_string_to_jstring(env, eir_ptr->getShortName()); + jau::shared_ptr_ref<EInfoReport> ref(env, obj); // hold until done + return jau::from_string_to_jstring(env, ref->getShortName()); } catch(...) { rethrow_and_raise_java_exception(env); } @@ -476,8 +461,8 @@ jstring Java_org_direct_1bt_EInfoReport_getShortName(JNIEnv *env, jobject obj) { */ jbyte Java_org_direct_1bt_EInfoReport_getRSSI(JNIEnv *env, jobject obj) { try { - std::shared_ptr<EInfoReport>& eir_ptr = *jau::getInstance<std::shared_ptr<EInfoReport>>(env, obj); - return static_cast<jbyte>( eir_ptr->getRSSI() ); + jau::shared_ptr_ref<EInfoReport> ref(env, obj); // hold until done + return static_cast<jbyte>( ref->getRSSI() ); } catch(...) { rethrow_and_raise_java_exception(env); } @@ -491,8 +476,8 @@ jbyte Java_org_direct_1bt_EInfoReport_getRSSI(JNIEnv *env, jobject obj) { */ jbyte Java_org_direct_1bt_EInfoReport_getTxPower(JNIEnv *env, jobject obj) { try { - std::shared_ptr<EInfoReport>& eir_ptr = *jau::getInstance<std::shared_ptr<EInfoReport>>(env, obj); - return static_cast<jbyte>( eir_ptr->getTxPower() ); + jau::shared_ptr_ref<EInfoReport> ref(env, obj); // hold until done + return static_cast<jbyte>( ref->getTxPower() ); } catch(...) { rethrow_and_raise_java_exception(env); } @@ -502,8 +487,8 @@ jbyte Java_org_direct_1bt_EInfoReport_getTxPower(JNIEnv *env, jobject obj) { jobject Java_org_direct_1bt_EInfoReport_getManufacturerData(JNIEnv *env, jobject obj) { try { - std::shared_ptr<EInfoReport>& eir_ptr = *jau::getInstance<std::shared_ptr<EInfoReport>>(env, obj); - std::shared_ptr<ManufactureSpecificData> mdata = eir_ptr->getManufactureSpecificData(); + jau::shared_ptr_ref<EInfoReport> ref(env, obj); // hold until done + std::shared_ptr<ManufactureSpecificData> mdata = ref->getManufactureSpecificData(); jclass map_cls = jau::search_class(env, "java/util/HashMap"); jmethodID map_ctor = jau::search_method(env, map_cls, "<init>", "(I)V", false); @@ -542,10 +527,10 @@ jobject Java_org_direct_1bt_EInfoReport_getManufacturerData(JNIEnv *env, jobject */ jobject Java_org_direct_1bt_EInfoReport_getServices(JNIEnv *env, jobject obj) { try { - std::shared_ptr<EInfoReport>& eir_ptr = *jau::getInstance<std::shared_ptr<EInfoReport>>(env, obj); - jau::darray<std::shared_ptr<const jau::uuid_t>> service_uuids = eir_ptr->getServices(); - std::function<jobject(JNIEnv*, const jau::uuid_t*)> ctor_uuid2string = - [](JNIEnv *env_, const jau::uuid_t* uuid_ptr)->jobject { + jau::shared_ptr_ref<EInfoReport> ref(env, obj); // hold until done + jau::darray<std::shared_ptr<const jau::uuid_t>> service_uuids = ref->getServices(); + std::function<jobject(JNIEnv*, const std::shared_ptr<const jau::uuid_t>&)> ctor_uuid2string = + [](JNIEnv *env_, const std::shared_ptr<const jau::uuid_t>& uuid_ptr)->jobject { return jau::from_string_to_jstring(env_, uuid_ptr->toUUID128String()); }; return jau::convert_vector_sharedptr_to_jarraylist<jau::darray<std::shared_ptr<const jau::uuid_t>>, const jau::uuid_t>(env, service_uuids, ctor_uuid2string); @@ -562,8 +547,8 @@ jobject Java_org_direct_1bt_EInfoReport_getServices(JNIEnv *env, jobject obj) { */ jboolean Java_org_direct_1bt_EInfoReport_getServicesComplete(JNIEnv *env, jobject obj) { try { - std::shared_ptr<EInfoReport>& eir_ptr = *jau::getInstance<std::shared_ptr<EInfoReport>>(env, obj); - return eir_ptr->getServicesComplete() ? JNI_TRUE : JNI_FALSE; + jau::shared_ptr_ref<EInfoReport> ref(env, obj); // hold until done + return ref->getServicesComplete() ? JNI_TRUE : JNI_FALSE; } catch(...) { rethrow_and_raise_java_exception(env); } @@ -577,8 +562,8 @@ jboolean Java_org_direct_1bt_EInfoReport_getServicesComplete(JNIEnv *env, jobjec */ jint Java_org_direct_1bt_EInfoReport_getDeviceClass(JNIEnv *env, jobject obj) { try { - std::shared_ptr<EInfoReport>& eir_ptr = *jau::getInstance<std::shared_ptr<EInfoReport>>(env, obj); - return static_cast<jint>( eir_ptr->getDeviceClass() ); + jau::shared_ptr_ref<EInfoReport> ref(env, obj); // hold until done + return static_cast<jint>( ref->getDeviceClass() ); } catch(...) { rethrow_and_raise_java_exception(env); } @@ -592,8 +577,8 @@ jint Java_org_direct_1bt_EInfoReport_getDeviceClass(JNIEnv *env, jobject obj) { */ jshort Java_org_direct_1bt_EInfoReport_getDeviceIDSource(JNIEnv *env, jobject obj) { try { - std::shared_ptr<EInfoReport>& eir_ptr = *jau::getInstance<std::shared_ptr<EInfoReport>>(env, obj); - return static_cast<jshort>( eir_ptr->getDeviceIDSource() ); + jau::shared_ptr_ref<EInfoReport> ref(env, obj); // hold until done + return static_cast<jshort>( ref->getDeviceIDSource() ); } catch(...) { rethrow_and_raise_java_exception(env); } @@ -607,8 +592,8 @@ jshort Java_org_direct_1bt_EInfoReport_getDeviceIDSource(JNIEnv *env, jobject ob */ jshort Java_org_direct_1bt_EInfoReport_getDeviceIDVendor(JNIEnv *env, jobject obj) { try { - std::shared_ptr<EInfoReport>& eir_ptr = *jau::getInstance<std::shared_ptr<EInfoReport>>(env, obj); - return static_cast<jshort>( eir_ptr->getDeviceIDVendor() ); + jau::shared_ptr_ref<EInfoReport> ref(env, obj); // hold until done + return static_cast<jshort>( ref->getDeviceIDVendor() ); } catch(...) { rethrow_and_raise_java_exception(env); } @@ -622,8 +607,8 @@ jshort Java_org_direct_1bt_EInfoReport_getDeviceIDVendor(JNIEnv *env, jobject ob */ jshort Java_org_direct_1bt_EInfoReport_getDeviceIDProduct(JNIEnv *env, jobject obj) { try { - std::shared_ptr<EInfoReport>& eir_ptr = *jau::getInstance<std::shared_ptr<EInfoReport>>(env, obj); - return static_cast<jshort>( eir_ptr->getDeviceIDProduct() ); + jau::shared_ptr_ref<EInfoReport> ref(env, obj); // hold until done + return static_cast<jshort>( ref->getDeviceIDProduct() ); } catch(...) { rethrow_and_raise_java_exception(env); } @@ -637,8 +622,8 @@ jshort Java_org_direct_1bt_EInfoReport_getDeviceIDProduct(JNIEnv *env, jobject o */ jshort Java_org_direct_1bt_EInfoReport_getDeviceIDVersion(JNIEnv *env, jobject obj) { try { - std::shared_ptr<EInfoReport>& eir_ptr = *jau::getInstance<std::shared_ptr<EInfoReport>>(env, obj); - return static_cast<jshort>( eir_ptr->getDeviceIDVersion() ); + jau::shared_ptr_ref<EInfoReport> ref(env, obj); // hold until done + return static_cast<jshort>( ref->getDeviceIDVersion() ); } catch(...) { rethrow_and_raise_java_exception(env); } @@ -652,7 +637,7 @@ jshort Java_org_direct_1bt_EInfoReport_getDeviceIDVersion(JNIEnv *env, jobject o */ void Java_org_direct_1bt_EInfoReport_getConnInterval(JNIEnv *env, jobject obj, jshortArray jminmax) { try { - std::shared_ptr<EInfoReport>& eir_ptr = *jau::getInstance<std::shared_ptr<EInfoReport>>(env, obj); + jau::shared_ptr_ref<EInfoReport> ref(env, obj); // hold until done if( nullptr == jminmax ) { throw jau::IllegalArgumentException("address null", E_FILE_LINE); @@ -666,7 +651,7 @@ void Java_org_direct_1bt_EInfoReport_getConnInterval(JNIEnv *env, jobject obj, j if( NULL == array_ptr ) { throw jau::InternalError("GetPrimitiveArrayCritical(short array) is null", E_FILE_LINE); } - eir_ptr->getConnInterval(array_ptr[0], array_ptr[1]); + ref->getConnInterval(array_ptr[0], array_ptr[1]); } catch(...) { rethrow_and_raise_java_exception(env); } @@ -679,8 +664,8 @@ void Java_org_direct_1bt_EInfoReport_getConnInterval(JNIEnv *env, jobject obj, j */ jstring Java_org_direct_1bt_EInfoReport_getDeviceIDModalias(JNIEnv *env, jobject obj) { try { - std::shared_ptr<EInfoReport>& eir_ptr = *jau::getInstance<std::shared_ptr<EInfoReport>>(env, obj); - return jau::from_string_to_jstring(env, eir_ptr->getDeviceIDModalias()); + jau::shared_ptr_ref<EInfoReport> ref(env, obj); // hold until done + return jau::from_string_to_jstring(env, ref->getDeviceIDModalias()); } catch(...) { rethrow_and_raise_java_exception(env); } @@ -694,8 +679,8 @@ jstring Java_org_direct_1bt_EInfoReport_getDeviceIDModalias(JNIEnv *env, jobject */ jstring Java_org_direct_1bt_EInfoReport_eirDataMaskToString(JNIEnv *env, jobject obj) { try { - std::shared_ptr<EInfoReport>& eir_ptr = *jau::getInstance<std::shared_ptr<EInfoReport>>(env, obj); - return jau::from_string_to_jstring(env, eir_ptr->eirDataMaskToString()); + jau::shared_ptr_ref<EInfoReport> ref(env, obj); // hold until done + return jau::from_string_to_jstring(env, ref->eirDataMaskToString()); } catch(...) { rethrow_and_raise_java_exception(env); } @@ -709,8 +694,8 @@ jstring Java_org_direct_1bt_EInfoReport_eirDataMaskToString(JNIEnv *env, jobject */ jstring Java_org_direct_1bt_EInfoReport_toString(JNIEnv *env, jobject obj, jboolean includeServices) { try { - std::shared_ptr<EInfoReport>& eir_ptr = *jau::getInstance<std::shared_ptr<EInfoReport>>(env, obj); - return jau::from_string_to_jstring(env, eir_ptr->toString(JNI_TRUE==includeServices)); + jau::shared_ptr_ref<EInfoReport> ref(env, obj); // hold until done + return jau::from_string_to_jstring(env, ref->toString(JNI_TRUE==includeServices)); } catch(...) { rethrow_and_raise_java_exception(env); } |