diff options
Diffstat (limited to 'java/jni/direct_bt')
-rw-r--r-- | java/jni/direct_bt/CMakeLists.txt | 4 | ||||
-rw-r--r-- | java/jni/direct_bt/DBTAdapter.cxx | 325 | ||||
-rw-r--r-- | java/jni/direct_bt/DBTDevice.cxx | 74 | ||||
-rw-r--r-- | java/jni/direct_bt/DBTGattCharacteristic.cxx | 17 | ||||
-rw-r--r-- | java/jni/direct_bt/DBTGattDescriptor.cxx | 12 | ||||
-rw-r--r-- | java/jni/direct_bt/DBTGattService.cxx | 10 | ||||
-rw-r--r-- | java/jni/direct_bt/DBTManager.cxx | 8 | ||||
-rw-r--r-- | java/jni/direct_bt/DBTNativeDownlink.cxx | 4 | ||||
-rw-r--r-- | java/jni/direct_bt/DBTObject.cxx | 1 | ||||
-rw-r--r-- | java/jni/direct_bt/helper_dbt.cxx | 23 | ||||
-rw-r--r-- | java/jni/direct_bt/helper_dbt.hpp | 128 |
11 files changed, 231 insertions, 375 deletions
diff --git a/java/jni/direct_bt/CMakeLists.txt b/java/jni/direct_bt/CMakeLists.txt index bfe35734..b98bf943 100644 --- a/java/jni/direct_bt/CMakeLists.txt +++ b/java/jni/direct_bt/CMakeLists.txt @@ -6,6 +6,7 @@ if (JNI_FOUND) endif (JNI_FOUND) set (direct_bt_LIB_INCLUDE_DIRS + ${PROJECT_SOURCE_DIR}/jaucpp/include ${PROJECT_SOURCE_DIR}/api ${PROJECT_SOURCE_DIR}/api/direct_bt ${PROJECT_SOURCE_DIR}/include @@ -19,7 +20,8 @@ include_directories( ) set (direct_bt_JNI_SRCS - ${PROJECT_SOURCE_DIR}/java/jni/JNIMem.cxx + ${PROJECT_SOURCE_DIR}/jaucpp/java/jni/jni_mem.cxx + ${PROJECT_SOURCE_DIR}/jaucpp/java/jni/helper_jni.cxx ${PROJECT_SOURCE_DIR}/java/jni/helper_base.cxx ${PROJECT_SOURCE_DIR}/java/jni/BluetoothFactory.cxx ${PROJECT_SOURCE_DIR}/java/jni/BluetoothUtils.cxx diff --git a/java/jni/direct_bt/DBTAdapter.cxx b/java/jni/direct_bt/DBTAdapter.cxx index beb153c2..ce0b451c 100644 --- a/java/jni/direct_bt/DBTAdapter.cxx +++ b/java/jni/direct_bt/DBTAdapter.cxx @@ -26,9 +26,8 @@ #include "direct_bt_tinyb_DBTAdapter.h" // #define VERBOSE_ON 1 -#include <dbt_debug.hpp> +#include <jau/debug.hpp> -#include "JNIMem.hpp" #include "helper_base.hpp" #include "helper_dbt.hpp" @@ -73,7 +72,7 @@ class JNIAdapterStatusListener : public AdapterStatusListener { static std::atomic<int> iname_next; int const iname; DBTDevice const * const deviceMatchRef; - std::shared_ptr<JavaAnonObj> adapterObjRef; + std::shared_ptr<jau::JavaAnon> adapterObjRef; JNIGlobalRef adapterSettingsClazzRef; jmethodID adapterSettingsClazzCtor; JNIGlobalRef eirDataTypeSetClazzRef; @@ -97,129 +96,129 @@ class JNIAdapterStatusListener : public AdapterStatusListener { std::string toString() const override { const std::string devMatchAddr = nullptr != deviceMatchRef ? deviceMatchRef->address.toString() : "nil"; - return "JNIAdapterStatusListener[this "+aptrHexString(this)+", iname "+std::to_string(iname)+", devMatchAddr "+devMatchAddr+"]"; + return "JNIAdapterStatusListener[this "+jau::aptrHexString(this)+", iname "+std::to_string(iname)+", devMatchAddr "+devMatchAddr+"]"; } JNIAdapterStatusListener(JNIEnv *env, DBTAdapter *adapter, jobject statusListener, const DBTDevice * _deviceMatchRef) : iname(iname_next.fetch_add(1)), deviceMatchRef(_deviceMatchRef), listenerObjRef(statusListener) { adapterObjRef = adapter->getJavaObject(); - JavaGlobalObj::check(adapterObjRef, E_FILE_LINE); + jau::JavaGlobalObj::check(adapterObjRef, E_FILE_LINE); - jclass listenerClazz = search_class(env, listenerObjRef.getObject()); - java_exception_check_and_throw(env, E_FILE_LINE); + jclass listenerClazz = jau::search_class(env, listenerObjRef.getObject()); + jau::java_exception_check_and_throw(env, E_FILE_LINE); if( nullptr == listenerClazz ) { - throw InternalError("AdapterStatusListener not found", E_FILE_LINE); + throw jau::InternalError("AdapterStatusListener not found", E_FILE_LINE); } // adapterSettingsClazzRef, adapterSettingsClazzCtor { - jclass adapterSettingsClazz = search_class(env, _adapterSettingsClassName.c_str()); - java_exception_check_and_throw(env, E_FILE_LINE); + jclass adapterSettingsClazz = jau::search_class(env, _adapterSettingsClassName.c_str()); + jau::java_exception_check_and_throw(env, E_FILE_LINE); if( nullptr == adapterSettingsClazz ) { - throw InternalError("DBTDevice::java_class not found: "+_adapterSettingsClassName, E_FILE_LINE); + throw jau::InternalError("DBTDevice::java_class not found: "+_adapterSettingsClassName, E_FILE_LINE); } adapterSettingsClazzRef = JNIGlobalRef(adapterSettingsClazz); env->DeleteLocalRef(adapterSettingsClazz); } - adapterSettingsClazzCtor = search_method(env, adapterSettingsClazzRef.getClass(), "<init>", _adapterSettingsClazzCtorArgs.c_str(), false); - java_exception_check_and_throw(env, E_FILE_LINE); + adapterSettingsClazzCtor = jau::search_method(env, adapterSettingsClazzRef.getClass(), "<init>", _adapterSettingsClazzCtorArgs.c_str(), false); + jau::java_exception_check_and_throw(env, E_FILE_LINE); if( nullptr == adapterSettingsClazzCtor ) { - throw InternalError("AdapterSettings ctor not found: "+_adapterSettingsClassName+".<init>"+_adapterSettingsClazzCtorArgs, E_FILE_LINE); + throw jau::InternalError("AdapterSettings ctor not found: "+_adapterSettingsClassName+".<init>"+_adapterSettingsClazzCtorArgs, E_FILE_LINE); } // eirDataTypeSetClazzRef, eirDataTypeSetClazzCtor { - jclass eirDataTypeSetClazz = search_class(env, _eirDataTypeSetClassName.c_str()); - java_exception_check_and_throw(env, E_FILE_LINE); + jclass eirDataTypeSetClazz = jau::search_class(env, _eirDataTypeSetClassName.c_str()); + jau::java_exception_check_and_throw(env, E_FILE_LINE); if( nullptr == eirDataTypeSetClazz ) { - throw InternalError("DBTDevice::java_class not found: "+_eirDataTypeSetClassName, E_FILE_LINE); + throw jau::InternalError("DBTDevice::java_class not found: "+_eirDataTypeSetClassName, E_FILE_LINE); } eirDataTypeSetClazzRef = JNIGlobalRef(eirDataTypeSetClazz); env->DeleteLocalRef(eirDataTypeSetClazz); } - eirDataTypeSetClazzCtor = search_method(env, eirDataTypeSetClazzRef.getClass(), "<init>", _eirDataTypeSetClazzCtorArgs.c_str(), false); - java_exception_check_and_throw(env, E_FILE_LINE); + eirDataTypeSetClazzCtor = jau::search_method(env, eirDataTypeSetClazzRef.getClass(), "<init>", _eirDataTypeSetClazzCtorArgs.c_str(), false); + jau::java_exception_check_and_throw(env, E_FILE_LINE); if( nullptr == eirDataTypeSetClazzCtor ) { - throw InternalError("EIRDataType ctor not found: "+_eirDataTypeSetClassName+".<init>"+_eirDataTypeSetClazzCtorArgs, E_FILE_LINE); + throw jau::InternalError("EIRDataType ctor not found: "+_eirDataTypeSetClassName+".<init>"+_eirDataTypeSetClazzCtorArgs, E_FILE_LINE); } // hciErrorCodeClazzRef, hciErrorCodeClazzGet { - jclass hciErrorCodeClazz = search_class(env, _hciStatusCodeClassName.c_str()); - java_exception_check_and_throw(env, E_FILE_LINE); + jclass hciErrorCodeClazz = jau::search_class(env, _hciStatusCodeClassName.c_str()); + jau::java_exception_check_and_throw(env, E_FILE_LINE); if( nullptr == hciErrorCodeClazz ) { - throw InternalError("DBTDevice::java_class not found: "+_hciStatusCodeClassName, E_FILE_LINE); + throw jau::InternalError("DBTDevice::java_class not found: "+_hciStatusCodeClassName, E_FILE_LINE); } hciErrorCodeClazzRef = JNIGlobalRef(hciErrorCodeClazz); env->DeleteLocalRef(hciErrorCodeClazz); } - hciErrorCodeClazzGet = search_method(env, hciErrorCodeClazzRef.getClass(), "get", _hciStatusCodeClazzGetArgs.c_str(), true); - java_exception_check_and_throw(env, E_FILE_LINE); + hciErrorCodeClazzGet = jau::search_method(env, hciErrorCodeClazzRef.getClass(), "get", _hciStatusCodeClazzGetArgs.c_str(), true); + jau::java_exception_check_and_throw(env, E_FILE_LINE); if( nullptr == hciErrorCodeClazzGet ) { - throw InternalError("EIRDataType ctor not found: "+_hciStatusCodeClassName+".get"+_hciStatusCodeClazzGetArgs, E_FILE_LINE); + throw jau::InternalError("EIRDataType ctor not found: "+_hciStatusCodeClassName+".get"+_hciStatusCodeClazzGetArgs, E_FILE_LINE); } // deviceClazzRef, deviceClazzCtor { - jclass deviceClazz = search_class(env, DBTDevice::java_class().c_str()); - java_exception_check_and_throw(env, E_FILE_LINE); + jclass deviceClazz = jau::search_class(env, DBTDevice::java_class().c_str()); + jau::java_exception_check_and_throw(env, E_FILE_LINE); if( nullptr == deviceClazz ) { - throw InternalError("DBTDevice::java_class not found: "+DBTDevice::java_class(), E_FILE_LINE); + throw jau::InternalError("DBTDevice::java_class not found: "+DBTDevice::java_class(), E_FILE_LINE); } deviceClazzRef = JNIGlobalRef(deviceClazz); env->DeleteLocalRef(deviceClazz); } - deviceClazzCtor = search_method(env, deviceClazzRef.getClass(), "<init>", _deviceClazzCtorArgs.c_str(), false); - java_exception_check_and_throw(env, E_FILE_LINE); + deviceClazzCtor = jau::search_method(env, deviceClazzRef.getClass(), "<init>", _deviceClazzCtorArgs.c_str(), false); + jau::java_exception_check_and_throw(env, E_FILE_LINE); if( nullptr == deviceClazzCtor ) { - throw InternalError("DBTDevice::java_class ctor not found: "+DBTDevice::java_class()+".<init>"+_deviceClazzCtorArgs, E_FILE_LINE); + throw jau::InternalError("DBTDevice::java_class ctor not found: "+DBTDevice::java_class()+".<init>"+_deviceClazzCtorArgs, E_FILE_LINE); } deviceClazzTSLastDiscoveryField = env->GetFieldID(deviceClazzRef.getClass(), "ts_last_discovery", "J"); - java_exception_check_and_throw(env, E_FILE_LINE); + jau::java_exception_check_and_throw(env, E_FILE_LINE); if( nullptr == deviceClazzTSLastDiscoveryField ) { - throw InternalError("DBTDevice::java_class field not found: "+DBTDevice::java_class()+".ts_last_discovery", E_FILE_LINE); + throw jau::InternalError("DBTDevice::java_class field not found: "+DBTDevice::java_class()+".ts_last_discovery", E_FILE_LINE); } deviceClazzTSLastUpdateField = env->GetFieldID(deviceClazzRef.getClass(), "ts_last_update", "J"); - java_exception_check_and_throw(env, E_FILE_LINE); + jau::java_exception_check_and_throw(env, E_FILE_LINE); if( nullptr == deviceClazzTSLastUpdateField ) { - throw InternalError("DBTDevice::java_class field not found: "+DBTDevice::java_class()+".ts_last_update", E_FILE_LINE); + throw jau::InternalError("DBTDevice::java_class field not found: "+DBTDevice::java_class()+".ts_last_update", E_FILE_LINE); } deviceClazzConnectionHandleField = env->GetFieldID(deviceClazzRef.getClass(), "hciConnHandle", "S"); - java_exception_check_and_throw(env, E_FILE_LINE); + jau::java_exception_check_and_throw(env, E_FILE_LINE); if( nullptr == deviceClazzConnectionHandleField ) { - throw InternalError("DBTDevice::java_class field not found: "+DBTDevice::java_class()+".hciConnHandle", E_FILE_LINE); + throw jau::InternalError("DBTDevice::java_class field not found: "+DBTDevice::java_class()+".hciConnHandle", E_FILE_LINE); } - mAdapterSettingsChanged = search_method(env, listenerClazz, "adapterSettingsChanged", _adapterSettingsChangedMethodArgs.c_str(), false); - java_exception_check_and_throw(env, E_FILE_LINE); + mAdapterSettingsChanged = jau::search_method(env, listenerClazz, "adapterSettingsChanged", _adapterSettingsChangedMethodArgs.c_str(), false); + jau::java_exception_check_and_throw(env, E_FILE_LINE); if( nullptr == mAdapterSettingsChanged ) { - throw InternalError("AdapterStatusListener has no adapterSettingsChanged"+_adapterSettingsChangedMethodArgs+" method, for "+adapter->toString(), E_FILE_LINE); + throw jau::InternalError("AdapterStatusListener has no adapterSettingsChanged"+_adapterSettingsChangedMethodArgs+" method, for "+adapter->toString(), E_FILE_LINE); } - mDiscoveringChanged = search_method(env, listenerClazz, "discoveringChanged", _discoveringChangedMethodArgs.c_str(), false); - java_exception_check_and_throw(env, E_FILE_LINE); + mDiscoveringChanged = jau::search_method(env, listenerClazz, "discoveringChanged", _discoveringChangedMethodArgs.c_str(), false); + jau::java_exception_check_and_throw(env, E_FILE_LINE); if( nullptr == mDiscoveringChanged ) { - throw InternalError("AdapterStatusListener has no discoveringChanged"+_discoveringChangedMethodArgs+" method, for "+adapter->toString(), E_FILE_LINE); + throw jau::InternalError("AdapterStatusListener has no discoveringChanged"+_discoveringChangedMethodArgs+" method, for "+adapter->toString(), E_FILE_LINE); } - mDeviceFound = search_method(env, listenerClazz, "deviceFound", _deviceFoundMethodArgs.c_str(), false); - java_exception_check_and_throw(env, E_FILE_LINE); + mDeviceFound = jau::search_method(env, listenerClazz, "deviceFound", _deviceFoundMethodArgs.c_str(), false); + jau::java_exception_check_and_throw(env, E_FILE_LINE); if( nullptr == mDeviceFound ) { - throw InternalError("AdapterStatusListener has no deviceFound"+_deviceFoundMethodArgs+" method, for "+adapter->toString(), E_FILE_LINE); + throw jau::InternalError("AdapterStatusListener has no deviceFound"+_deviceFoundMethodArgs+" method, for "+adapter->toString(), E_FILE_LINE); } - mDeviceUpdated = search_method(env, listenerClazz, "deviceUpdated", _deviceUpdatedMethodArgs.c_str(), false); - java_exception_check_and_throw(env, E_FILE_LINE); + mDeviceUpdated = jau::search_method(env, listenerClazz, "deviceUpdated", _deviceUpdatedMethodArgs.c_str(), false); + jau::java_exception_check_and_throw(env, E_FILE_LINE); if( nullptr == mDeviceUpdated ) { - throw InternalError("AdapterStatusListener has no deviceUpdated"+_deviceUpdatedMethodArgs+" method, for "+adapter->toString(), E_FILE_LINE); + throw jau::InternalError("AdapterStatusListener has no deviceUpdated"+_deviceUpdatedMethodArgs+" method, for "+adapter->toString(), E_FILE_LINE); } - mDeviceConnected = search_method(env, listenerClazz, "deviceConnected", _deviceConnectedMethodArgs.c_str(), false); - java_exception_check_and_throw(env, E_FILE_LINE); + mDeviceConnected = jau::search_method(env, listenerClazz, "deviceConnected", _deviceConnectedMethodArgs.c_str(), false); + jau::java_exception_check_and_throw(env, E_FILE_LINE); if( nullptr == mDeviceConnected ) { - throw InternalError("AdapterStatusListener has no deviceConnected"+_deviceConnectedMethodArgs+" method, for "+adapter->toString(), E_FILE_LINE); + throw jau::InternalError("AdapterStatusListener has no deviceConnected"+_deviceConnectedMethodArgs+" method, for "+adapter->toString(), E_FILE_LINE); } - mDeviceDisconnected = search_method(env, listenerClazz, "deviceDisconnected", _deviceDisconnectedMethodArgs.c_str(), false); - java_exception_check_and_throw(env, E_FILE_LINE); + mDeviceDisconnected = jau::search_method(env, listenerClazz, "deviceDisconnected", _deviceDisconnectedMethodArgs.c_str(), false); + jau::java_exception_check_and_throw(env, E_FILE_LINE); if( nullptr == mDeviceDisconnected ) { - throw InternalError("AdapterStatusListener has no deviceDisconnected"+_deviceDisconnectedMethodArgs+" method, for "+adapter->toString(), E_FILE_LINE); + throw jau::InternalError("AdapterStatusListener has no deviceDisconnected"+_deviceDisconnectedMethodArgs+" method, for "+adapter->toString(), E_FILE_LINE); } } @@ -235,20 +234,20 @@ class JNIAdapterStatusListener : public AdapterStatusListener { JNIEnv *env = *jni_env; (void)a; jobject adapterSettingOld = env->NewObject(adapterSettingsClazzRef.getClass(), adapterSettingsClazzCtor, (jint)oldmask); - java_exception_check_and_throw(env, E_FILE_LINE); + jau::java_exception_check_and_throw(env, E_FILE_LINE); JNIGlobalRef::check(adapterSettingOld, E_FILE_LINE); jobject adapterSettingNew = env->NewObject(adapterSettingsClazzRef.getClass(), adapterSettingsClazzCtor, (jint)newmask); - java_exception_check_and_throw(env, E_FILE_LINE); + jau::java_exception_check_and_throw(env, E_FILE_LINE); JNIGlobalRef::check(adapterSettingNew, E_FILE_LINE); jobject adapterSettingChanged = env->NewObject(adapterSettingsClazzRef.getClass(), adapterSettingsClazzCtor, (jint)changedmask); - java_exception_check_and_throw(env, E_FILE_LINE); + jau::java_exception_check_and_throw(env, E_FILE_LINE); JNIGlobalRef::check(adapterSettingChanged, E_FILE_LINE); env->CallVoidMethod(listenerObjRef.getObject(), mAdapterSettingsChanged, - JavaGlobalObj::GetObject(adapterObjRef), adapterSettingOld, adapterSettingNew, adapterSettingChanged, (jlong)timestamp); - java_exception_check_and_throw(env, E_FILE_LINE); + jau::JavaGlobalObj::GetObject(adapterObjRef), adapterSettingOld, adapterSettingNew, adapterSettingChanged, (jlong)timestamp); + jau::java_exception_check_and_throw(env, E_FILE_LINE); env->DeleteLocalRef(adapterSettingOld); env->DeleteLocalRef(adapterSettingNew); env->DeleteLocalRef(adapterSettingChanged); @@ -257,56 +256,56 @@ class JNIAdapterStatusListener : public AdapterStatusListener { void discoveringChanged(DBTAdapter &a, const bool enabled, const bool keepAlive, const uint64_t timestamp) override { JNIEnv *env = *jni_env; (void)a; - env->CallVoidMethod(listenerObjRef.getObject(), mDiscoveringChanged, JavaGlobalObj::GetObject(adapterObjRef), + env->CallVoidMethod(listenerObjRef.getObject(), mDiscoveringChanged, jau::JavaGlobalObj::GetObject(adapterObjRef), (jboolean)enabled, (jboolean)keepAlive, (jlong)timestamp); - java_exception_check_and_throw(env, E_FILE_LINE); + jau::java_exception_check_and_throw(env, E_FILE_LINE); } void deviceFound(std::shared_ptr<DBTDevice> device, const uint64_t timestamp) override { JNIEnv *env = *jni_env; jobject jdevice; - std::shared_ptr<JavaAnonObj> jDeviceRef0 = device->getJavaObject(); - if( JavaGlobalObj::isValid(jDeviceRef0) ) { + std::shared_ptr<jau::JavaAnon> jDeviceRef0 = device->getJavaObject(); + if( jau::JavaGlobalObj::isValid(jDeviceRef0) ) { // Reuse Java instance - jdevice = JavaGlobalObj::GetObject(jDeviceRef0); + jdevice = jau::JavaGlobalObj::GetObject(jDeviceRef0); } else { // New Java instance // Device(final long nativeInstance, final Adapter adptr, final String address, final int intAddressType, final String name) - const jstring addr = from_string_to_jstring(env, device->getAddressString()); - const jstring name = from_string_to_jstring(env, device->getName()); - java_exception_check_and_throw(env, E_FILE_LINE); + const jstring addr = jau::from_string_to_jstring(env, device->getAddressString()); + const jstring name = jau::from_string_to_jstring(env, device->getName()); + jau::java_exception_check_and_throw(env, E_FILE_LINE); jobject tmp_jdevice = env->NewObject(deviceClazzRef.getClass(), deviceClazzCtor, - (jlong)device.get(), JavaGlobalObj::GetObject(adapterObjRef), addr, + (jlong)device.get(), jau::JavaGlobalObj::GetObject(adapterObjRef), addr, device->getAddressType(), device->getBLERandomAddressType(), name, (jlong)timestamp); - java_exception_check_and_throw(env, E_FILE_LINE); + jau::java_exception_check_and_throw(env, E_FILE_LINE); JNIGlobalRef::check(tmp_jdevice, E_FILE_LINE); - std::shared_ptr<JavaAnonObj> jDeviceRef1 = device->getJavaObject(); - JavaGlobalObj::check(jDeviceRef1, E_FILE_LINE); - jdevice = JavaGlobalObj::GetObject(jDeviceRef1); + std::shared_ptr<jau::JavaAnon> jDeviceRef1 = device->getJavaObject(); + jau::JavaGlobalObj::check(jDeviceRef1, E_FILE_LINE); + jdevice = jau::JavaGlobalObj::GetObject(jDeviceRef1); env->DeleteLocalRef(addr); env->DeleteLocalRef(name); env->DeleteLocalRef(tmp_jdevice); } env->SetLongField(jdevice, deviceClazzTSLastDiscoveryField, (jlong)device->getLastDiscoveryTimestamp()); - java_exception_check_and_throw(env, E_FILE_LINE); + jau::java_exception_check_and_throw(env, E_FILE_LINE); env->CallVoidMethod(listenerObjRef.getObject(), mDeviceFound, jdevice, (jlong)timestamp); - java_exception_check_and_throw(env, E_FILE_LINE); + jau::java_exception_check_and_throw(env, E_FILE_LINE); } void deviceUpdated(std::shared_ptr<DBTDevice> device, const EIRDataType updateMask, const uint64_t timestamp) override { JNIEnv *env = *jni_env; - std::shared_ptr<JavaAnonObj> jDeviceRef = device->getJavaObject(); - JavaGlobalObj::check(jDeviceRef, E_FILE_LINE); - env->SetLongField(JavaGlobalObj::GetObject(jDeviceRef), deviceClazzTSLastUpdateField, (jlong)timestamp); - java_exception_check_and_throw(env, E_FILE_LINE); + std::shared_ptr<jau::JavaAnon> jDeviceRef = device->getJavaObject(); + jau::JavaGlobalObj::check(jDeviceRef, E_FILE_LINE); + env->SetLongField(jau::JavaGlobalObj::GetObject(jDeviceRef), deviceClazzTSLastUpdateField, (jlong)timestamp); + jau::java_exception_check_and_throw(env, E_FILE_LINE); jobject eirDataTypeSet = env->NewObject(eirDataTypeSetClazzRef.getClass(), eirDataTypeSetClazzCtor, (jint)updateMask); - java_exception_check_and_throw(env, E_FILE_LINE); + jau::java_exception_check_and_throw(env, E_FILE_LINE); JNIGlobalRef::check(eirDataTypeSet, E_FILE_LINE); - env->CallVoidMethod(listenerObjRef.getObject(), mDeviceUpdated, JavaGlobalObj::GetObject(jDeviceRef), eirDataTypeSet, (jlong)timestamp); - java_exception_check_and_throw(env, E_FILE_LINE); + env->CallVoidMethod(listenerObjRef.getObject(), mDeviceUpdated, jau::JavaGlobalObj::GetObject(jDeviceRef), eirDataTypeSet, (jlong)timestamp); + jau::java_exception_check_and_throw(env, E_FILE_LINE); env->DeleteLocalRef(eirDataTypeSet); } @@ -314,59 +313,59 @@ class JNIAdapterStatusListener : public AdapterStatusListener { JNIEnv *env = *jni_env; jobject jdevice; - std::shared_ptr<JavaAnonObj> jDeviceRef0 = device->getJavaObject(); - if( JavaGlobalObj::isValid(jDeviceRef0) ) { + std::shared_ptr<jau::JavaAnon> jDeviceRef0 = device->getJavaObject(); + if( jau::JavaGlobalObj::isValid(jDeviceRef0) ) { // Reuse Java instance - jdevice = JavaGlobalObj::GetObject(jDeviceRef0); + jdevice = jau::JavaGlobalObj::GetObject(jDeviceRef0); } else { // New Java instance // Device(final long nativeInstance, final Adapter adptr, final String address, final int intAddressType, final String name) - const jstring addr = from_string_to_jstring(env, device->getAddressString()); - const jstring name = from_string_to_jstring(env, device->getName()); - java_exception_check_and_throw(env, E_FILE_LINE); + const jstring addr = jau::from_string_to_jstring(env, device->getAddressString()); + const jstring name = jau::from_string_to_jstring(env, device->getName()); + jau::java_exception_check_and_throw(env, E_FILE_LINE); jobject tmp_jdevice = env->NewObject(deviceClazzRef.getClass(), deviceClazzCtor, - (jlong)device.get(), JavaGlobalObj::GetObject(adapterObjRef), addr, + (jlong)device.get(), jau::JavaGlobalObj::GetObject(adapterObjRef), addr, device->getAddressType(), device->getBLERandomAddressType(), name, (jlong)timestamp); - java_exception_check_and_throw(env, E_FILE_LINE); + jau::java_exception_check_and_throw(env, E_FILE_LINE); JNIGlobalRef::check(tmp_jdevice, E_FILE_LINE); - std::shared_ptr<JavaAnonObj> jDeviceRef1 = device->getJavaObject(); - JavaGlobalObj::check(jDeviceRef1, E_FILE_LINE); - jdevice = JavaGlobalObj::GetObject(jDeviceRef1); + std::shared_ptr<jau::JavaAnon> jDeviceRef1 = device->getJavaObject(); + jau::JavaGlobalObj::check(jDeviceRef1, E_FILE_LINE); + jdevice = jau::JavaGlobalObj::GetObject(jDeviceRef1); env->DeleteLocalRef(addr); env->DeleteLocalRef(name); env->DeleteLocalRef(tmp_jdevice); } env->SetShortField(jdevice, deviceClazzConnectionHandleField, (jshort)handle); - java_exception_check_and_throw(env, E_FILE_LINE); + jau::java_exception_check_and_throw(env, E_FILE_LINE); env->SetLongField(jdevice, deviceClazzTSLastDiscoveryField, (jlong)device->getLastDiscoveryTimestamp()); - java_exception_check_and_throw(env, E_FILE_LINE); + jau::java_exception_check_and_throw(env, E_FILE_LINE); env->SetLongField(jdevice, deviceClazzTSLastUpdateField, (jlong)timestamp); - java_exception_check_and_throw(env, E_FILE_LINE); + jau::java_exception_check_and_throw(env, E_FILE_LINE); env->CallVoidMethod(listenerObjRef.getObject(), mDeviceConnected, jdevice, (jshort)handle, (jlong)timestamp); - java_exception_check_and_throw(env, E_FILE_LINE); + jau::java_exception_check_and_throw(env, E_FILE_LINE); } void deviceDisconnected(std::shared_ptr<DBTDevice> device, const HCIStatusCode reason, const uint16_t handle, const uint64_t timestamp) override { JNIEnv *env = *jni_env; - std::shared_ptr<JavaAnonObj> jDeviceRef = device->getJavaObject(); - JavaGlobalObj::check(jDeviceRef, E_FILE_LINE); - jobject jdevice = JavaGlobalObj::GetObject(jDeviceRef); + std::shared_ptr<jau::JavaAnon> jDeviceRef = device->getJavaObject(); + jau::JavaGlobalObj::check(jDeviceRef, E_FILE_LINE); + jobject jdevice = jau::JavaGlobalObj::GetObject(jDeviceRef); env->SetLongField(jdevice, deviceClazzTSLastUpdateField, (jlong)timestamp); - java_exception_check_and_throw(env, E_FILE_LINE); + jau::java_exception_check_and_throw(env, E_FILE_LINE); jobject hciErrorCode = env->CallStaticObjectMethod(hciErrorCodeClazzRef.getClass(), hciErrorCodeClazzGet, (jbyte)static_cast<uint8_t>(reason)); - java_exception_check_and_throw(env, E_FILE_LINE); + jau::java_exception_check_and_throw(env, E_FILE_LINE); JNIGlobalRef::check(hciErrorCode, E_FILE_LINE); env->SetShortField(jdevice, deviceClazzConnectionHandleField, (jshort)0); // zero out, disconnected - java_exception_check_and_throw(env, E_FILE_LINE); + jau::java_exception_check_and_throw(env, E_FILE_LINE); env->SetLongField(jdevice, deviceClazzTSLastUpdateField, (jlong)timestamp); - java_exception_check_and_throw(env, E_FILE_LINE); + jau::java_exception_check_and_throw(env, E_FILE_LINE); env->CallVoidMethod(listenerObjRef.getObject(), mDeviceDisconnected, jdevice, hciErrorCode, (jshort)handle, (jlong)timestamp); - java_exception_check_and_throw(env, E_FILE_LINE); + jau::java_exception_check_and_throw(env, E_FILE_LINE); } }; std::atomic<int> JNIAdapterStatusListener::iname_next(0); @@ -375,35 +374,35 @@ jboolean Java_direct_1bt_tinyb_DBTAdapter_addStatusListener(JNIEnv *env, jobject { try { if( nullptr == statusListener ) { - throw IllegalArgumentException("JNIAdapterStatusListener::addStatusListener: statusListener is null", E_FILE_LINE); + throw jau::IllegalArgumentException("JNIAdapterStatusListener::addStatusListener: statusListener is null", E_FILE_LINE); } { - JNIAdapterStatusListener * pre = getInstanceUnchecked<JNIAdapterStatusListener>(env, statusListener); + JNIAdapterStatusListener * pre = jau::getInstanceUnchecked<JNIAdapterStatusListener>(env, statusListener); if( nullptr != pre ) { WARN_PRINT("JNIAdapterStatusListener::addStatusListener: statusListener's nativeInstance not null, already in use"); return false; } } - DBTAdapter *adapter = getDBTObject<DBTAdapter>(env, obj); - JavaGlobalObj::check(adapter->getJavaObject(), E_FILE_LINE); + DBTAdapter *adapter = jau::getJavaUplinkObject<DBTAdapter>(env, obj); + jau::JavaGlobalObj::check(adapter->getJavaObject(), E_FILE_LINE); DBTDevice * deviceMatchRef = nullptr; if( nullptr != jdeviceMatch ) { - deviceMatchRef = getDBTObject<DBTDevice>(env, jdeviceMatch); - JavaGlobalObj::check(deviceMatchRef->getJavaObject(), E_FILE_LINE); + deviceMatchRef = jau::getJavaUplinkObject<DBTDevice>(env, jdeviceMatch); + jau::JavaGlobalObj::check(deviceMatchRef->getJavaObject(), E_FILE_LINE); } std::shared_ptr<AdapterStatusListener> l = std::shared_ptr<AdapterStatusListener>( new JNIAdapterStatusListener(env, adapter, statusListener, deviceMatchRef) ); - setInstance(env, statusListener, l.get()); + jau::setInstance(env, statusListener, l.get()); if( adapter->addStatusListener( l ) ) { return JNI_TRUE; } - clearInstance(env, statusListener); + jau::clearInstance(env, statusListener); ERR_PRINT("JNIAdapterStatusListener::addStatusListener: FAILED: %s", l->toString().c_str()); } catch(...) { - clearInstance(env, statusListener); + jau::clearInstance(env, statusListener); rethrow_and_raise_java_exception(env); } ERR_PRINT("JNIAdapterStatusListener::addStatusListener: FAILED XX"); @@ -414,17 +413,17 @@ jboolean Java_direct_1bt_tinyb_DBTAdapter_removeStatusListenerImpl(JNIEnv *env, { try { if( nullptr == statusListener ) { - throw IllegalArgumentException("statusListener is null", E_FILE_LINE); + throw jau::IllegalArgumentException("statusListener is null", E_FILE_LINE); } - JNIAdapterStatusListener * pre = getInstanceUnchecked<JNIAdapterStatusListener>(env, statusListener); + JNIAdapterStatusListener * pre = jau::getInstanceUnchecked<JNIAdapterStatusListener>(env, statusListener); if( nullptr == pre ) { DBG_PRINT("statusListener's nativeInstance is null, not in use"); return false; } - clearInstance(env, statusListener); + jau::clearInstance(env, statusListener); - DBTAdapter *adapter = getDBTObject<DBTAdapter>(env, obj); - JavaGlobalObj::check(adapter->getJavaObject(), E_FILE_LINE); + DBTAdapter *adapter = jau::getJavaUplinkObject<DBTAdapter>(env, obj); + jau::JavaGlobalObj::check(adapter->getJavaObject(), E_FILE_LINE); if( ! adapter->removeStatusListener( pre ) ) { WARN_PRINT("Failed to remove statusListener with nativeInstance: %p at %s", pre, adapter->toString().c_str()); @@ -439,8 +438,8 @@ jboolean Java_direct_1bt_tinyb_DBTAdapter_removeStatusListenerImpl(JNIEnv *env, jint Java_direct_1bt_tinyb_DBTAdapter_removeAllStatusListener(JNIEnv *env, jobject obj) { try { - DBTAdapter *adapter = getDBTObject<DBTAdapter>(env, obj); - JavaGlobalObj::check(adapter->getJavaObject(), E_FILE_LINE); + DBTAdapter *adapter = jau::getJavaUplinkObject<DBTAdapter>(env, obj); + jau::JavaGlobalObj::check(adapter->getJavaObject(), E_FILE_LINE); return adapter->removeAllStatusListener(); } catch(...) { @@ -451,10 +450,10 @@ jint Java_direct_1bt_tinyb_DBTAdapter_removeAllStatusListener(JNIEnv *env, jobje jboolean Java_direct_1bt_tinyb_DBTAdapter_isDeviceWhitelisted(JNIEnv *env, jobject obj, jstring jaddress) { try { - DBTAdapter *adapter = getDBTObject<DBTAdapter>(env, obj); - JavaGlobalObj::check(adapter->getJavaObject(), E_FILE_LINE); + DBTAdapter *adapter = jau::getJavaUplinkObject<DBTAdapter>(env, obj); + jau::JavaGlobalObj::check(adapter->getJavaObject(), E_FILE_LINE); - std::string saddress = from_jstring_to_string(env, jaddress); + std::string saddress = jau::from_jstring_to_string(env, jaddress); EUI48 address(saddress); return adapter->isDeviceWhitelisted(address); } catch(...) { @@ -467,10 +466,10 @@ jboolean Java_direct_1bt_tinyb_DBTAdapter_addDeviceToWhitelist__Ljava_lang_Strin jshort min_interval, jshort max_interval, jshort latency, jshort timeout) { try { - DBTAdapter *adapter = getDBTObject<DBTAdapter>(env, obj); - JavaGlobalObj::check(adapter->getJavaObject(), E_FILE_LINE); + DBTAdapter *adapter = jau::getJavaUplinkObject<DBTAdapter>(env, obj); + jau::JavaGlobalObj::check(adapter->getJavaObject(), E_FILE_LINE); - std::string saddress = from_jstring_to_string(env, jaddress); + std::string saddress = jau::from_jstring_to_string(env, jaddress); EUI48 address(saddress); const BDAddressType addressType = static_cast<BDAddressType>( jaddressType ); const HCIWhitelistConnectType ctype = static_cast<HCIWhitelistConnectType>( jctype ); @@ -483,10 +482,10 @@ jboolean Java_direct_1bt_tinyb_DBTAdapter_addDeviceToWhitelist__Ljava_lang_Strin jboolean Java_direct_1bt_tinyb_DBTAdapter_addDeviceToWhitelist__Ljava_lang_String_2II(JNIEnv *env, jobject obj, jstring jaddress, int jaddressType, int jctype) { try { - DBTAdapter *adapter = getDBTObject<DBTAdapter>(env, obj); - JavaGlobalObj::check(adapter->getJavaObject(), E_FILE_LINE); + DBTAdapter *adapter = jau::getJavaUplinkObject<DBTAdapter>(env, obj); + jau::JavaGlobalObj::check(adapter->getJavaObject(), E_FILE_LINE); - std::string saddress = from_jstring_to_string(env, jaddress); + std::string saddress = jau::from_jstring_to_string(env, jaddress); EUI48 address(saddress); const BDAddressType addressType = static_cast<BDAddressType>( jaddressType ); const HCIWhitelistConnectType ctype = static_cast<HCIWhitelistConnectType>( jctype ); @@ -498,10 +497,10 @@ jboolean Java_direct_1bt_tinyb_DBTAdapter_addDeviceToWhitelist__Ljava_lang_Strin } jboolean Java_direct_1bt_tinyb_DBTAdapter_removeDeviceFromWhitelist(JNIEnv *env, jobject obj, jstring jaddress, int jaddressType) { try { - DBTAdapter *adapter = getDBTObject<DBTAdapter>(env, obj); - JavaGlobalObj::check(adapter->getJavaObject(), E_FILE_LINE); + DBTAdapter *adapter = jau::getJavaUplinkObject<DBTAdapter>(env, obj); + jau::JavaGlobalObj::check(adapter->getJavaObject(), E_FILE_LINE); - std::string saddress = from_jstring_to_string(env, jaddress); + std::string saddress = jau::from_jstring_to_string(env, jaddress); EUI48 address(saddress); const BDAddressType addressType = static_cast<BDAddressType>( jaddressType ); return adapter->removeDeviceFromWhitelist(address, addressType); @@ -513,9 +512,9 @@ jboolean Java_direct_1bt_tinyb_DBTAdapter_removeDeviceFromWhitelist(JNIEnv *env, jstring Java_direct_1bt_tinyb_DBTAdapter_toStringImpl(JNIEnv *env, jobject obj) { try { - DBTAdapter *nativePtr = getDBTObject<DBTAdapter>(env, obj); - JavaGlobalObj::check(nativePtr->getJavaObject(), E_FILE_LINE); - return from_string_to_jstring(env, nativePtr->toString()); + DBTAdapter *nativePtr = jau::getJavaUplinkObject<DBTAdapter>(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); } @@ -526,7 +525,7 @@ void Java_direct_1bt_tinyb_DBTAdapter_deleteImpl(JNIEnv *env, jobject obj, jlong { (void)obj; try { - DBTAdapter *adapter = castInstance<DBTAdapter>(nativeInstance); + DBTAdapter *adapter = jau::castInstance<DBTAdapter>(nativeInstance); DBG_PRINT("Java_direct_1bt_tinyb_DBTAdapter_deleteImpl %s", adapter->toString().c_str()); delete adapter; } catch(...) { @@ -537,7 +536,7 @@ void Java_direct_1bt_tinyb_DBTAdapter_deleteImpl(JNIEnv *env, jobject obj, jlong jboolean Java_direct_1bt_tinyb_DBTAdapter_isEnabled(JNIEnv *env, jobject obj) { try { - DBTAdapter *adapter = getDBTObject<DBTAdapter>(env, obj); + DBTAdapter *adapter = jau::getJavaUplinkObject<DBTAdapter>(env, obj); return adapter->isEnabled(); } catch(...) { rethrow_and_raise_java_exception(env); @@ -548,7 +547,7 @@ jboolean Java_direct_1bt_tinyb_DBTAdapter_isEnabled(JNIEnv *env, jobject obj) jbyte Java_direct_1bt_tinyb_DBTAdapter_startDiscoveryImpl(JNIEnv *env, jobject obj, jboolean keepAlive) { try { - DBTAdapter *adapter = getDBTObject<DBTAdapter>(env, obj); + DBTAdapter *adapter = jau::getJavaUplinkObject<DBTAdapter>(env, obj); return (jbyte) number( adapter->startDiscovery(keepAlive) ); } catch(...) { rethrow_and_raise_java_exception(env); @@ -559,7 +558,7 @@ jbyte Java_direct_1bt_tinyb_DBTAdapter_startDiscoveryImpl(JNIEnv *env, jobject o jbyte Java_direct_1bt_tinyb_DBTAdapter_stopDiscoveryImpl(JNIEnv *env, jobject obj) { try { - DBTAdapter *adapter = getDBTObject<DBTAdapter>(env, obj); + DBTAdapter *adapter = jau::getJavaUplinkObject<DBTAdapter>(env, obj); return (jbyte) number( adapter->stopDiscovery() ); } catch(...) { rethrow_and_raise_java_exception(env); @@ -570,7 +569,7 @@ jbyte Java_direct_1bt_tinyb_DBTAdapter_stopDiscoveryImpl(JNIEnv *env, jobject ob jobject Java_direct_1bt_tinyb_DBTAdapter_getDiscoveredDevicesImpl(JNIEnv *env, jobject obj) { try { - DBTAdapter *adapter = getDBTObject<DBTAdapter>(env, obj); + DBTAdapter *adapter = jau::getJavaUplinkObject<DBTAdapter>(env, obj); std::vector<std::shared_ptr<DBTDevice>> array = adapter->getDiscoveredDevices(); return convert_vector_sharedptr_to_jarraylist(env, array); } catch(...) { @@ -582,7 +581,7 @@ jobject Java_direct_1bt_tinyb_DBTAdapter_getDiscoveredDevicesImpl(JNIEnv *env, j jint Java_direct_1bt_tinyb_DBTAdapter_removeDevicesImpl(JNIEnv *env, jobject obj) { try { - DBTAdapter *adapter = getDBTObject<DBTAdapter>(env, obj); + DBTAdapter *adapter = jau::getJavaUplinkObject<DBTAdapter>(env, obj); return adapter->removeDiscoveredDevices(); } catch(...) { rethrow_and_raise_java_exception(env); @@ -596,8 +595,8 @@ jint Java_direct_1bt_tinyb_DBTAdapter_removeDevicesImpl(JNIEnv *env, jobject obj jboolean Java_direct_1bt_tinyb_DBTAdapter_setPowered(JNIEnv *env, jobject obj, jboolean value) { try { - DBTAdapter *adapter = getDBTObject<DBTAdapter>(env, obj); - JavaGlobalObj::check(adapter->getJavaObject(), E_FILE_LINE); + DBTAdapter *adapter = jau::getJavaUplinkObject<DBTAdapter>(env, obj); + jau::JavaGlobalObj::check(adapter->getJavaObject(), E_FILE_LINE); return adapter->setPowered(JNI_TRUE == value ? true : false) ? JNI_TRUE : JNI_FALSE; } catch(...) { rethrow_and_raise_java_exception(env); @@ -607,8 +606,8 @@ jboolean Java_direct_1bt_tinyb_DBTAdapter_setPowered(JNIEnv *env, jobject obj, j jbyte Java_direct_1bt_tinyb_DBTAdapter_resetImpl(JNIEnv *env, jobject obj) { try { - DBTAdapter *adapter = getDBTObject<DBTAdapter>(env, obj); - JavaGlobalObj::check(adapter->getJavaObject(), E_FILE_LINE); + DBTAdapter *adapter = jau::getJavaUplinkObject<DBTAdapter>(env, obj); + jau::JavaGlobalObj::check(adapter->getJavaObject(), E_FILE_LINE); HCIStatusCode res = adapter->reset(); return (jbyte) number(res); } catch(...) { @@ -619,9 +618,9 @@ jbyte Java_direct_1bt_tinyb_DBTAdapter_resetImpl(JNIEnv *env, jobject obj) { jstring Java_direct_1bt_tinyb_DBTAdapter_getAlias(JNIEnv *env, jobject obj) { try { - DBTAdapter *adapter = getDBTObject<DBTAdapter>(env, obj); - JavaGlobalObj::check(adapter->getJavaObject(), E_FILE_LINE); - return from_string_to_jstring(env, adapter->getLocalName().getName()); + DBTAdapter *adapter = jau::getJavaUplinkObject<DBTAdapter>(env, obj); + jau::JavaGlobalObj::check(adapter->getJavaObject(), E_FILE_LINE); + return jau::from_string_to_jstring(env, adapter->getLocalName().getName()); } catch(...) { rethrow_and_raise_java_exception(env); } @@ -630,9 +629,9 @@ jstring Java_direct_1bt_tinyb_DBTAdapter_getAlias(JNIEnv *env, jobject obj) { void Java_direct_1bt_tinyb_DBTAdapter_setAlias(JNIEnv *env, jobject obj, jstring jnewalias) { try { - DBTAdapter *adapter = getDBTObject<DBTAdapter>(env, obj); - JavaGlobalObj::check(adapter->getJavaObject(), E_FILE_LINE); - std::string newalias = from_jstring_to_string(env, jnewalias); + DBTAdapter *adapter = jau::getJavaUplinkObject<DBTAdapter>(env, obj); + jau::JavaGlobalObj::check(adapter->getJavaObject(), E_FILE_LINE); + std::string newalias = jau::from_jstring_to_string(env, jnewalias); adapter->setLocalName(newalias, std::string()); } catch(...) { rethrow_and_raise_java_exception(env); @@ -641,8 +640,8 @@ void Java_direct_1bt_tinyb_DBTAdapter_setAlias(JNIEnv *env, jobject obj, jstring jboolean Java_direct_1bt_tinyb_DBTAdapter_setDiscoverable(JNIEnv *env, jobject obj, jboolean value) { try { - DBTAdapter *adapter = getDBTObject<DBTAdapter>(env, obj); - JavaGlobalObj::check(adapter->getJavaObject(), E_FILE_LINE); + DBTAdapter *adapter = jau::getJavaUplinkObject<DBTAdapter>(env, obj); + jau::JavaGlobalObj::check(adapter->getJavaObject(), E_FILE_LINE); return adapter->setDiscoverable(JNI_TRUE == value ? true : false) ? JNI_TRUE : JNI_FALSE; } catch(...) { rethrow_and_raise_java_exception(env); @@ -652,9 +651,9 @@ jboolean Java_direct_1bt_tinyb_DBTAdapter_setDiscoverable(JNIEnv *env, jobject o jobject Java_direct_1bt_tinyb_DBTAdapter_connectDevice(JNIEnv *env, jobject obj, jstring jaddress, jstring jaddressType) { try { - DBTAdapter *adapter = getDBTObject<DBTAdapter>(env, obj); - JavaGlobalObj::check(adapter->getJavaObject(), E_FILE_LINE); - std::string saddress = from_jstring_to_string(env, jaddress); + DBTAdapter *adapter = jau::getJavaUplinkObject<DBTAdapter>(env, obj); + jau::JavaGlobalObj::check(adapter->getJavaObject(), E_FILE_LINE); + std::string saddress = jau::from_jstring_to_string(env, jaddress); EUI48 address(saddress); const BDAddressType addressType = fromJavaAdressTypeToBDAddressType(env, jaddressType); std::shared_ptr<DBTDevice> device = adapter->findDiscoveredDevice(address, addressType); @@ -663,11 +662,11 @@ jobject Java_direct_1bt_tinyb_DBTAdapter_connectDevice(JNIEnv *env, jobject obj, if( !hci.isOpen() ) { throw BluetoothException("Adapter's HCI closed "+adapter->toString(), E_FILE_LINE); } - std::shared_ptr<JavaAnonObj> jDeviceRef = device->getJavaObject(); - JavaGlobalObj::check(jDeviceRef, E_FILE_LINE); + std::shared_ptr<jau::JavaAnon> jDeviceRef = device->getJavaObject(); + jau::JavaGlobalObj::check(jDeviceRef, E_FILE_LINE); device->connectDefault(); - return JavaGlobalObj::GetObject(jDeviceRef); + return jau::JavaGlobalObj::GetObject(jDeviceRef); } } catch(...) { rethrow_and_raise_java_exception(env); @@ -677,8 +676,8 @@ jobject Java_direct_1bt_tinyb_DBTAdapter_connectDevice(JNIEnv *env, jobject obj, jboolean Java_direct_1bt_tinyb_DBTAdapter_setPairable(JNIEnv *env, jobject obj, jboolean value) { try { - DBTAdapter *adapter = getDBTObject<DBTAdapter>(env, obj); - JavaGlobalObj::check(adapter->getJavaObject(), E_FILE_LINE); + DBTAdapter *adapter = jau::getJavaUplinkObject<DBTAdapter>(env, obj); + jau::JavaGlobalObj::check(adapter->getJavaObject(), E_FILE_LINE); return adapter->setBondable(JNI_TRUE == value ? true : false) ? 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 8bf1f463..54c7aab0 100644 --- a/java/jni/direct_bt/DBTDevice.cxx +++ b/java/jni/direct_bt/DBTDevice.cxx @@ -26,9 +26,8 @@ #include "direct_bt_tinyb_DBTDevice.h" // #define VERBOSE_ON 1 -#include <dbt_debug.hpp> +#include <jau/debug.hpp> -#include "JNIMem.hpp" #include "helper_base.hpp" #include "helper_dbt.hpp" @@ -36,6 +35,7 @@ #include "direct_bt/DBTAdapter.hpp" using namespace direct_bt; +using namespace jau; static const std::string _notificationReceivedMethodArgs("(Lorg/tinyb/BluetoothGattCharacteristic;[BJ)V"); static const std::string _indicationReceivedMethodArgs("(Lorg/tinyb/BluetoothGattCharacteristic;[BJZ)V"); @@ -141,7 +141,7 @@ class JNICharacteristicListener : public GATTCharacteristicListener { void Java_direct_1bt_tinyb_DBTDevice_initImpl(JNIEnv *env, jobject obj) { try { - DBTDevice *device = getDBTObject<DBTDevice>(env, obj); + DBTDevice *device = getJavaUplinkObject<DBTDevice>(env, obj); JavaGlobalObj::check(device->getJavaObject(), E_FILE_LINE); } catch(...) { rethrow_and_raise_java_exception(env); @@ -150,7 +150,7 @@ void Java_direct_1bt_tinyb_DBTDevice_initImpl(JNIEnv *env, jobject obj) jstring Java_direct_1bt_tinyb_DBTDevice_getNameImpl(JNIEnv *env, jobject obj) { try { - DBTDevice *nativePtr = getDBTObject<DBTDevice>(env, obj); + DBTDevice *nativePtr = getJavaUplinkObject<DBTDevice>(env, obj); JavaGlobalObj::check(nativePtr->getJavaObject(), E_FILE_LINE); return from_string_to_jstring(env, nativePtr->getName()); } catch(...) { @@ -161,7 +161,7 @@ jstring Java_direct_1bt_tinyb_DBTDevice_getNameImpl(JNIEnv *env, jobject obj) { jstring Java_direct_1bt_tinyb_DBTDevice_toStringImpl(JNIEnv *env, jobject obj) { try { - DBTDevice *nativePtr = getDBTObject<DBTDevice>(env, obj); + DBTDevice *nativePtr = getJavaUplinkObject<DBTDevice>(env, obj); JavaGlobalObj::check(nativePtr->getJavaObject(), E_FILE_LINE); return from_string_to_jstring(env, nativePtr->toString()); } catch(...) { @@ -183,7 +183,7 @@ jboolean Java_direct_1bt_tinyb_DBTDevice_addCharacteristicListener(JNIEnv *env, return false; } } - DBTDevice *device = getDBTObject<DBTDevice>(env, obj); + DBTDevice *device = getJavaUplinkObject<DBTDevice>(env, obj); JavaGlobalObj::check(device->getJavaObject(), E_FILE_LINE); std::shared_ptr<GATTHandler> gatt = device->getGATTHandler(); if( nullptr == gatt ) { @@ -192,7 +192,7 @@ jboolean Java_direct_1bt_tinyb_DBTDevice_addCharacteristicListener(JNIEnv *env, GATTCharacteristic * associatedCharacteristicRef = nullptr; if( nullptr != jAssociatedCharacteristic ) { - associatedCharacteristicRef = getDBTObject<GATTCharacteristic>(env, jAssociatedCharacteristic); + associatedCharacteristicRef = getJavaUplinkObject<GATTCharacteristic>(env, jAssociatedCharacteristic); } std::shared_ptr<GATTCharacteristicListener> l = @@ -221,7 +221,7 @@ jboolean Java_direct_1bt_tinyb_DBTDevice_removeCharacteristicListener(JNIEnv *en } setObjectRef<JNICharacteristicListener>(env, jlistener, nullptr, "nativeInstance"); - DBTDevice *device = getDBTObjectUnchecked<DBTDevice>(env, obj); + DBTDevice *device = getJavaUplinkObjectUnchecked<DBTDevice>(env, obj); if( nullptr == device ) { // OK to have device being deleted already @ shutdown return 0; @@ -250,7 +250,7 @@ jint Java_direct_1bt_tinyb_DBTDevice_removeAllAssociatedCharacteristicListener(J if( nullptr == jAssociatedCharacteristic ) { throw IllegalArgumentException("associatedCharacteristic argument is null", E_FILE_LINE); } - DBTDevice *device = getDBTObjectUnchecked<DBTDevice>(env, obj); + DBTDevice *device = getJavaUplinkObjectUnchecked<DBTDevice>(env, obj); if( nullptr == device ) { // OK to have device being deleted already @ shutdown return 0; @@ -263,7 +263,7 @@ jint Java_direct_1bt_tinyb_DBTDevice_removeAllAssociatedCharacteristicListener(J return 0; } - GATTCharacteristic * associatedCharacteristicRef = getDBTObject<GATTCharacteristic>(env, jAssociatedCharacteristic); + GATTCharacteristic * associatedCharacteristicRef = getJavaUplinkObject<GATTCharacteristic>(env, jAssociatedCharacteristic); JavaGlobalObj::check(associatedCharacteristicRef->getJavaObject(), E_FILE_LINE); return gatt->removeAllAssociatedCharacteristicListener(associatedCharacteristicRef); @@ -275,7 +275,7 @@ jint Java_direct_1bt_tinyb_DBTDevice_removeAllAssociatedCharacteristicListener(J jint Java_direct_1bt_tinyb_DBTDevice_removeAllCharacteristicListener(JNIEnv *env, jobject obj) { try { - DBTDevice *device = getDBTObjectUnchecked<DBTDevice>(env, obj); + DBTDevice *device = getJavaUplinkObjectUnchecked<DBTDevice>(env, obj); if( nullptr == device ) { // OK to have device being deleted already @ shutdown return 0; @@ -310,7 +310,7 @@ void Java_direct_1bt_tinyb_DBTDevice_deleteImpl(JNIEnv *env, jobject obj, jlong jbyte Java_direct_1bt_tinyb_DBTDevice_disconnectImpl(JNIEnv *env, jobject obj) { try { - DBTDevice *device = getDBTObject<DBTDevice>(env, obj); + DBTDevice *device = getJavaUplinkObject<DBTDevice>(env, obj); JavaGlobalObj::check(device->getJavaObject(), E_FILE_LINE); return (jint) number( device->disconnect() ); } catch(...) { @@ -322,7 +322,7 @@ jbyte Java_direct_1bt_tinyb_DBTDevice_disconnectImpl(JNIEnv *env, jobject obj) jboolean Java_direct_1bt_tinyb_DBTDevice_removeImpl(JNIEnv *env, jobject obj) { try { - DBTDevice *device = getDBTObject<DBTDevice>(env, obj); + DBTDevice *device = getJavaUplinkObject<DBTDevice>(env, obj); JavaGlobalObj::check(device->getJavaObject(), E_FILE_LINE); device->remove(); } catch(...) { @@ -334,7 +334,7 @@ jboolean Java_direct_1bt_tinyb_DBTDevice_removeImpl(JNIEnv *env, jobject obj) jbyte Java_direct_1bt_tinyb_DBTDevice_connectImpl__(JNIEnv *env, jobject obj) { try { - DBTDevice *device = getDBTObject<DBTDevice>(env, obj); + DBTDevice *device = getJavaUplinkObject<DBTDevice>(env, obj); JavaGlobalObj::check(device->getJavaObject(), E_FILE_LINE); return (jbyte) number( device->connectDefault() ); } catch(...) { @@ -349,7 +349,7 @@ jbyte Java_direct_1bt_tinyb_DBTDevice_connectImpl__SSSSSS(JNIEnv *env, jobject o jshort latency, jshort timeout) { try { - DBTDevice *device = getDBTObject<DBTDevice>(env, obj); + DBTDevice *device = getJavaUplinkObject<DBTDevice>(env, obj); JavaGlobalObj::check(device->getJavaObject(), E_FILE_LINE); HCIStatusCode res; switch( device->addressType ) { @@ -372,7 +372,7 @@ jbyte Java_direct_1bt_tinyb_DBTDevice_connectImpl__SSSSSS(JNIEnv *env, jobject o jbyte Java_direct_1bt_tinyb_DBTDevice_pairImpl(JNIEnv *env, jobject obj, jstring jpasskey) { try { - DBTDevice *device = getDBTObject<DBTDevice>(env, obj); + DBTDevice *device = getJavaUplinkObject<DBTDevice>(env, obj); JavaGlobalObj::check(device->getJavaObject(), E_FILE_LINE); const std::string passkey = nullptr != jpasskey ? from_jstring_to_string(env, jpasskey) : std::string(); @@ -387,7 +387,7 @@ jbyte Java_direct_1bt_tinyb_DBTDevice_pairImpl(JNIEnv *env, jobject obj, jstring jbyteArray Java_direct_1bt_tinyb_DBTDevice_getSupportedPairingModesImpl(JNIEnv *env, jobject obj) { try { - DBTDevice *device = getDBTObject<DBTDevice>(env, obj); + DBTDevice *device = getJavaUplinkObject<DBTDevice>(env, obj); JavaGlobalObj::check(device->getJavaObject(), E_FILE_LINE); std::vector<PairingMode> res0 = device->getSupportedPairingModes(); @@ -409,7 +409,7 @@ jbyteArray Java_direct_1bt_tinyb_DBTDevice_getSupportedPairingModesImpl(JNIEnv * jbyteArray Java_direct_1bt_tinyb_DBTDevice_getRequiredPairingModesImpl(JNIEnv *env, jobject obj) { try { - DBTDevice *device = getDBTObject<DBTDevice>(env, obj); + DBTDevice *device = getJavaUplinkObject<DBTDevice>(env, obj); JavaGlobalObj::check(device->getJavaObject(), E_FILE_LINE); std::vector<PairingMode> res0 = device->getRequiredPairingModes(); @@ -437,7 +437,7 @@ static const std::string _serviceClazzCtorArgs("(JLdirect_bt/tinyb/DBTDevice;ZLj jobject Java_direct_1bt_tinyb_DBTDevice_getServicesImpl(JNIEnv *env, jobject obj) { try { - DBTDevice *device = getDBTObject<DBTDevice>(env, obj); + DBTDevice *device = getJavaUplinkObject<DBTDevice>(env, obj); JavaGlobalObj::check(device->getJavaObject(), E_FILE_LINE); std::vector<GATTServiceRef> services = device->getGATTServices(); // implicit GATT connect and discovery if required incl GenericAccess retrieval @@ -471,7 +471,7 @@ jobject Java_direct_1bt_tinyb_DBTDevice_getServicesImpl(JNIEnv *env, jobject obj juuid, service->startHandle, service->endHandle); java_exception_check_and_throw(env, E_FILE_LINE); JNIGlobalRef::check(jservice, E_FILE_LINE); - std::shared_ptr<JavaAnonObj> jServiceRef = service->getJavaObject(); // GlobalRef + std::shared_ptr<JavaAnon> jServiceRef = service->getJavaObject(); // GlobalRef JavaGlobalObj::check(jServiceRef, E_FILE_LINE); env->DeleteLocalRef(juuid); env->DeleteLocalRef(jservice); @@ -487,7 +487,7 @@ jobject Java_direct_1bt_tinyb_DBTDevice_getServicesImpl(JNIEnv *env, jobject obj jboolean Java_direct_1bt_tinyb_DBTDevice_pingGATTImpl(JNIEnv *env, jobject obj) { try { - DBTDevice *device = getDBTObject<DBTDevice>(env, obj); + DBTDevice *device = getJavaUplinkObject<DBTDevice>(env, obj); JavaGlobalObj::check(device->getJavaObject(), E_FILE_LINE); return device->pingGATT() ? JNI_TRUE : JNI_FALSE; @@ -500,7 +500,7 @@ jboolean Java_direct_1bt_tinyb_DBTDevice_pingGATTImpl(JNIEnv *env, jobject obj) jstring Java_direct_1bt_tinyb_DBTDevice_getIcon(JNIEnv *env, jobject obj) { try { - DBTDevice *device = getDBTObject<DBTDevice>(env, obj); + DBTDevice *device = getJavaUplinkObject<DBTDevice>(env, obj); JavaGlobalObj::check(device->getJavaObject(), E_FILE_LINE); return nullptr; // FIXME } catch(...) { @@ -512,7 +512,7 @@ jstring Java_direct_1bt_tinyb_DBTDevice_getIcon(JNIEnv *env, jobject obj) void Java_direct_1bt_tinyb_DBTDevice_setTrustedImpl(JNIEnv *env, jobject obj, jboolean value) { try { - DBTDevice *device = getDBTObject<DBTDevice>(env, obj); + DBTDevice *device = getJavaUplinkObject<DBTDevice>(env, obj); JavaGlobalObj::check(device->getJavaObject(), E_FILE_LINE); (void)value; // FIXME @@ -524,7 +524,7 @@ void Java_direct_1bt_tinyb_DBTDevice_setTrustedImpl(JNIEnv *env, jobject obj, jb void Java_direct_1bt_tinyb_DBTDevice_setBlockedImpl(JNIEnv *env, jobject obj, jboolean value) { try { - DBTDevice *device = getDBTObject<DBTDevice>(env, obj); + DBTDevice *device = getJavaUplinkObject<DBTDevice>(env, obj); JavaGlobalObj::check(device->getJavaObject(), E_FILE_LINE); (void)value; // FIXME @@ -536,7 +536,7 @@ void Java_direct_1bt_tinyb_DBTDevice_setBlockedImpl(JNIEnv *env, jobject obj, jb jboolean JNICALL Java_direct_1bt_tinyb_DBTDevice_getLegacyPairing(JNIEnv *env, jobject obj) { try { - DBTDevice *device = getDBTObject<DBTDevice>(env, obj); + DBTDevice *device = getJavaUplinkObject<DBTDevice>(env, obj); JavaGlobalObj::check(device->getJavaObject(), E_FILE_LINE); return JNI_FALSE; // FIXME } catch(...) { @@ -548,7 +548,7 @@ jboolean JNICALL Java_direct_1bt_tinyb_DBTDevice_getLegacyPairing(JNIEnv *env, j jshort Java_direct_1bt_tinyb_DBTDevice_getRSSI(JNIEnv *env, jobject obj) { try { - DBTDevice *device = getDBTObject<DBTDevice>(env, obj); + DBTDevice *device = getJavaUplinkObject<DBTDevice>(env, obj); JavaGlobalObj::check(device->getJavaObject(), E_FILE_LINE); return (jshort) device->getRSSI(); } catch(...) { @@ -561,7 +561,7 @@ jshort Java_direct_1bt_tinyb_DBTDevice_getRSSI(JNIEnv *env, jobject obj) jobjectArray Java_direct_1bt_tinyb_DBTDevice_getUUIDs(JNIEnv *env, jobject obj) { try { - DBTDevice *device = getDBTObject<DBTDevice>(env, obj); + DBTDevice *device = getJavaUplinkObject<DBTDevice>(env, obj); JavaGlobalObj::check(device->getJavaObject(), E_FILE_LINE); return nullptr; // FIXME } catch(...) { @@ -573,7 +573,7 @@ jobjectArray Java_direct_1bt_tinyb_DBTDevice_getUUIDs(JNIEnv *env, jobject obj) jstring Java_direct_1bt_tinyb_DBTDevice_getModalias(JNIEnv *env, jobject obj) { try { - DBTDevice *device = getDBTObject<DBTDevice>(env, obj); + DBTDevice *device = getJavaUplinkObject<DBTDevice>(env, obj); JavaGlobalObj::check(device->getJavaObject(), E_FILE_LINE); return nullptr; // FIXME } catch(...) { @@ -585,7 +585,7 @@ jstring Java_direct_1bt_tinyb_DBTDevice_getModalias(JNIEnv *env, jobject obj) jobject Java_direct_1bt_tinyb_DBTDevice_getManufacturerData(JNIEnv *env, jobject obj) { try { - DBTDevice *device = getDBTObject<DBTDevice>(env, obj); + DBTDevice *device = getJavaUplinkObject<DBTDevice>(env, obj); JavaGlobalObj::check(device->getJavaObject(), E_FILE_LINE); std::shared_ptr<ManufactureSpecificData> mdata = device->getManufactureSpecificData(); @@ -622,7 +622,7 @@ jobject Java_direct_1bt_tinyb_DBTDevice_getManufacturerData(JNIEnv *env, jobject jshort Java_direct_1bt_tinyb_DBTDevice_getTxPower(JNIEnv *env, jobject obj) { try { - DBTDevice *device = getDBTObject<DBTDevice>(env, obj); + DBTDevice *device = getJavaUplinkObject<DBTDevice>(env, obj); JavaGlobalObj::check(device->getJavaObject(), E_FILE_LINE); return (jshort) device->getTxPower(); } catch(...) { @@ -672,17 +672,17 @@ static void disableBlockedNotifications(JNIEnv *env, jobject obj, DBTManager &mg setObjectRef(env, obj, funcptr, "blockedNotificationRef"); // clear java ref int count; if( 1 != ( count = mgmt.removeMgmtEventCallback(MgmtEvent::Opcode::DEVICE_BLOCKED, funcDef) ) ) { - throw direct_bt::InternalError(std::string("removeMgmtEventCallback of ")+funcDef.toString()+" not 1 but "+std::to_string(count), E_FILE_LINE); + throw InternalError(std::string("removeMgmtEventCallback of ")+funcDef.toString()+" not 1 but "+std::to_string(count), E_FILE_LINE); } if( 1 != ( count = mgmt.removeMgmtEventCallback(MgmtEvent::Opcode::DEVICE_UNBLOCKED, funcDef) ) ) { - throw direct_bt::InternalError(std::string("removeMgmtEventCallback of ")+funcDef.toString()+" not 1 but "+std::to_string(count), E_FILE_LINE); + throw InternalError(std::string("removeMgmtEventCallback of ")+funcDef.toString()+" not 1 but "+std::to_string(count), E_FILE_LINE); } } } void Java_direct_1bt_tinyb_DBTDevice_disableBlockedNotificationsImpl(JNIEnv *env, jobject obj) { try { - DBTDevice *device = getDBTObject<DBTDevice>(env, obj); + DBTDevice *device = getJavaUplinkObject<DBTDevice>(env, obj); JavaGlobalObj::check(device->getJavaObject(), E_FILE_LINE); DBTManager & mgmt = device->getAdapter().getManager(); @@ -694,7 +694,7 @@ void Java_direct_1bt_tinyb_DBTDevice_disableBlockedNotificationsImpl(JNIEnv *env void Java_direct_1bt_tinyb_DBTDevice_enableBlockedNotificationsImpl(JNIEnv *env, jobject obj, jobject javaCallback) { try { - DBTDevice *device= getDBTObject<DBTDevice>(env, obj); + DBTDevice *device= getJavaUplinkObject<DBTDevice>(env, obj); JavaGlobalObj::check(device->getJavaObject(), E_FILE_LINE); DBTAdapter & adapter = device->getAdapter(); DBTManager & mgmt = adapter.getManager(); @@ -762,14 +762,14 @@ static void disablePairedNotifications(JNIEnv *env, jobject obj, DBTManager &mgm setObjectRef(env, obj, funcptr, "pairedNotificationRef"); // clear java ref int count; if( 1 != ( count = mgmt.removeMgmtEventCallback(MgmtEvent::Opcode::DEVICE_UNPAIRED, funcDef) ) ) { - throw direct_bt::InternalError(std::string("removeMgmtEventCallback of ")+funcDef.toString()+" not 1 but "+std::to_string(count), E_FILE_LINE); + throw InternalError(std::string("removeMgmtEventCallback of ")+funcDef.toString()+" not 1 but "+std::to_string(count), E_FILE_LINE); } } } void Java_direct_1bt_tinyb_DBTDevice_disablePairedNotificationsImpl(JNIEnv *env, jobject obj) { try { - DBTDevice *device = getDBTObject<DBTDevice>(env, obj); + DBTDevice *device = getJavaUplinkObject<DBTDevice>(env, obj); JavaGlobalObj::check(device->getJavaObject(), E_FILE_LINE); DBTManager & mgmt = device->getAdapter().getManager(); @@ -781,7 +781,7 @@ void Java_direct_1bt_tinyb_DBTDevice_disablePairedNotificationsImpl(JNIEnv *env, void Java_direct_1bt_tinyb_DBTDevice_enablePairedNotificationsImpl(JNIEnv *env, jobject obj, jobject javaCallback) { try { - DBTDevice *device= getDBTObject<DBTDevice>(env, obj); + DBTDevice *device= getJavaUplinkObject<DBTDevice>(env, obj); JavaGlobalObj::check(device->getJavaObject(), E_FILE_LINE); DBTAdapter & adapter = device->getAdapter(); DBTManager & mgmt = adapter.getManager(); diff --git a/java/jni/direct_bt/DBTGattCharacteristic.cxx b/java/jni/direct_bt/DBTGattCharacteristic.cxx index e22602c1..d166a77d 100644 --- a/java/jni/direct_bt/DBTGattCharacteristic.cxx +++ b/java/jni/direct_bt/DBTGattCharacteristic.cxx @@ -25,10 +25,8 @@ #include "direct_bt_tinyb_DBTGattCharacteristic.h" -// #define VERBOSE_ON 1 -#include <dbt_debug.hpp> +#include <jau/debug.hpp> -#include "JNIMem.hpp" #include "helper_base.hpp" #include "helper_dbt.hpp" @@ -36,10 +34,11 @@ #include "direct_bt/DBTAdapter.hpp" using namespace direct_bt; +using namespace jau; jstring Java_direct_1bt_tinyb_DBTGattCharacteristic_toStringImpl(JNIEnv *env, jobject obj) { try { - GATTCharacteristic *nativePtr = getDBTObject<GATTCharacteristic>(env, obj); + GATTCharacteristic *nativePtr = getJavaUplinkObject<GATTCharacteristic>(env, obj); JavaGlobalObj::check(nativePtr->getJavaObject(), E_FILE_LINE); return from_string_to_jstring(env, nativePtr->toString()); } catch(...) { @@ -63,7 +62,7 @@ static const std::string _descriptorClazzCtorArgs("(JLdirect_bt/tinyb/DBTGattCha jobject Java_direct_1bt_tinyb_DBTGattCharacteristic_getDescriptorsImpl(JNIEnv *env, jobject obj) { try { - GATTCharacteristic *characteristic = getDBTObject<GATTCharacteristic>(env, obj); + GATTCharacteristic *characteristic = getJavaUplinkObject<GATTCharacteristic>(env, obj); JavaGlobalObj::check(characteristic->getJavaObject(), E_FILE_LINE); std::vector<GATTDescriptorRef> & descriptorList = characteristic->descriptorList; @@ -95,7 +94,7 @@ jobject Java_direct_1bt_tinyb_DBTGattCharacteristic_getDescriptorsImpl(JNIEnv *e juuid, (jshort)descriptor->handle, jvalue); java_exception_check_and_throw(env, E_FILE_LINE); JNIGlobalRef::check(jdesc, E_FILE_LINE); - std::shared_ptr<JavaAnonObj> jDescRef = descriptor->getJavaObject(); // GlobalRef + std::shared_ptr<JavaAnon> jDescRef = descriptor->getJavaObject(); // GlobalRef JavaGlobalObj::check(jDescRef, E_FILE_LINE); env->DeleteLocalRef(juuid); env->DeleteLocalRef(jvalue); @@ -111,7 +110,7 @@ jobject Java_direct_1bt_tinyb_DBTGattCharacteristic_getDescriptorsImpl(JNIEnv *e jbyteArray Java_direct_1bt_tinyb_DBTGattCharacteristic_readValueImpl(JNIEnv *env, jobject obj) { try { - GATTCharacteristic *characteristic = getDBTObject<GATTCharacteristic>(env, obj); + GATTCharacteristic *characteristic = getJavaUplinkObject<GATTCharacteristic>(env, obj); JavaGlobalObj::check(characteristic->getJavaObject(), E_FILE_LINE); POctets res(GATTHandler::number(GATTHandler::Defaults::MAX_ATT_MTU), 0); @@ -141,7 +140,7 @@ jboolean Java_direct_1bt_tinyb_DBTGattCharacteristic_writeValueImpl(JNIEnv *env, if( 0 == value_size ) { return JNI_TRUE; } - GATTCharacteristic *characteristic = getDBTObject<GATTCharacteristic>(env, obj); + GATTCharacteristic *characteristic = getJavaUplinkObject<GATTCharacteristic>(env, obj); JavaGlobalObj::check(characteristic->getJavaObject(), E_FILE_LINE); JNICriticalArray<uint8_t, jbyteArray> criticalArray(env); // RAII - release @@ -171,7 +170,7 @@ jboolean Java_direct_1bt_tinyb_DBTGattCharacteristic_writeValueImpl(JNIEnv *env, jboolean Java_direct_1bt_tinyb_DBTGattCharacteristic_configNotificationIndicationImpl(JNIEnv *env, jobject obj, jboolean enableNotification, jboolean enableIndication, jbooleanArray jEnabledState) { try { - GATTCharacteristic *characteristic = getDBTObjectUnchecked<GATTCharacteristic>(env, obj); + GATTCharacteristic *characteristic = getJavaUplinkObjectUnchecked<GATTCharacteristic>(env, obj); if( nullptr == characteristic ) { if( !enableNotification && !enableIndication ) { // OK to have native characteristic being shutdown @ disable diff --git a/java/jni/direct_bt/DBTGattDescriptor.cxx b/java/jni/direct_bt/DBTGattDescriptor.cxx index 1dfc372f..615ba60d 100644 --- a/java/jni/direct_bt/DBTGattDescriptor.cxx +++ b/java/jni/direct_bt/DBTGattDescriptor.cxx @@ -26,9 +26,8 @@ #include "direct_bt_tinyb_DBTGattDescriptor.h" // #define VERBOSE_ON 1 -#include <dbt_debug.hpp> +#include <jau/debug.hpp> -#include "JNIMem.hpp" #include "helper_base.hpp" #include "helper_dbt.hpp" @@ -36,10 +35,11 @@ #include "direct_bt/DBTAdapter.hpp" using namespace direct_bt; +using namespace jau; void Java_direct_1bt_tinyb_DBTGattDescriptor_deleteImpl(JNIEnv *env, jobject obj) { try { - GATTDescriptor *descriptor = getDBTObject<GATTDescriptor>(env, obj); + GATTDescriptor *descriptor = getJavaUplinkObject<GATTDescriptor>(env, obj); (void)descriptor; // No delete: Service instance owned by GATTService -> DBTDevice } catch(...) { @@ -50,7 +50,7 @@ void Java_direct_1bt_tinyb_DBTGattDescriptor_deleteImpl(JNIEnv *env, jobject obj jstring Java_direct_1bt_tinyb_DBTGattDescriptor_toStringImpl(JNIEnv *env, jobject obj) { (void)obj; try { - GATTDescriptor *descriptor = getDBTObject<GATTDescriptor>(env, obj); + GATTDescriptor *descriptor = getJavaUplinkObject<GATTDescriptor>(env, obj); JavaGlobalObj::check(descriptor->getJavaObject(), E_FILE_LINE); return from_string_to_jstring(env, descriptor->toString()); } catch(...) { @@ -61,7 +61,7 @@ jstring Java_direct_1bt_tinyb_DBTGattDescriptor_toStringImpl(JNIEnv *env, jobjec jbyteArray Java_direct_1bt_tinyb_DBTGattDescriptor_readValueImpl(JNIEnv *env, jobject obj) { try { - GATTDescriptor *descriptor = getDBTObject<GATTDescriptor>(env, obj); + GATTDescriptor *descriptor = getJavaUplinkObject<GATTDescriptor>(env, obj); JavaGlobalObj::check(descriptor->getJavaObject(), E_FILE_LINE); if( !descriptor->readValue() ) { @@ -89,7 +89,7 @@ jboolean Java_direct_1bt_tinyb_DBTGattDescriptor_writeValueImpl(JNIEnv *env, job if( 0 == value_size ) { return JNI_TRUE; } - GATTDescriptor *descriptor = getDBTObject<GATTDescriptor>(env, obj); + GATTDescriptor *descriptor = getJavaUplinkObject<GATTDescriptor>(env, obj); JavaGlobalObj::check(descriptor->getJavaObject(), E_FILE_LINE); JNICriticalArray<uint8_t, jbyteArray> criticalArray(env); // RAII - release diff --git a/java/jni/direct_bt/DBTGattService.cxx b/java/jni/direct_bt/DBTGattService.cxx index 8b18110e..24dbc20e 100644 --- a/java/jni/direct_bt/DBTGattService.cxx +++ b/java/jni/direct_bt/DBTGattService.cxx @@ -26,9 +26,8 @@ #include "direct_bt_tinyb_DBTGattService.h" // #define VERBOSE_ON 1 -#include <dbt_debug.hpp> +#include <jau/debug.hpp> -#include "JNIMem.hpp" #include "helper_base.hpp" #include "helper_dbt.hpp" @@ -36,10 +35,11 @@ #include "direct_bt/DBTAdapter.hpp" using namespace direct_bt; +using namespace jau; jstring Java_direct_1bt_tinyb_DBTGattService_toStringImpl(JNIEnv *env, jobject obj) { try { - GATTService *nativePtr = getDBTObject<GATTService>(env, obj); + GATTService *nativePtr = getJavaUplinkObject<GATTService>(env, obj); JavaGlobalObj::check(nativePtr->getJavaObject(), E_FILE_LINE); return from_string_to_jstring(env, nativePtr->toString()); } catch(...) { @@ -64,7 +64,7 @@ static const std::string _characteristicClazzCtorArgs("(JLdirect_bt/tinyb/DBTGat jobject Java_direct_1bt_tinyb_DBTGattService_getCharacteristicsImpl(JNIEnv *env, jobject obj) { try { - GATTService *service = getDBTObject<GATTService>(env, obj); + GATTService *service = getJavaUplinkObject<GATTService>(env, obj); JavaGlobalObj::check(service->getJavaObject(), E_FILE_LINE); std::vector<std::shared_ptr<GATTCharacteristic>> & characteristics = service->characteristicList; @@ -112,7 +112,7 @@ jobject Java_direct_1bt_tinyb_DBTGattService_getCharacteristicsImpl(JNIEnv *env, uuid, characteristic->value_handle, characteristic->clientCharacteristicsConfigIndex); java_exception_check_and_throw(env, E_FILE_LINE); JNIGlobalRef::check(jchar, E_FILE_LINE); - std::shared_ptr<JavaAnonObj> jCharRef = characteristic->getJavaObject(); // GlobalRef + std::shared_ptr<JavaAnon> jCharRef = characteristic->getJavaObject(); // GlobalRef JavaGlobalObj::check(jCharRef, E_FILE_LINE); env->DeleteLocalRef(jproperties); env->DeleteLocalRef(jchar); diff --git a/java/jni/direct_bt/DBTManager.cxx b/java/jni/direct_bt/DBTManager.cxx index 393cc126..2cd71e64 100644 --- a/java/jni/direct_bt/DBTManager.cxx +++ b/java/jni/direct_bt/DBTManager.cxx @@ -26,9 +26,8 @@ #include "direct_bt_tinyb_DBTManager.h" // #define VERBOSE_ON 1 -#include <dbt_debug.hpp> +#include <jau/debug.hpp> -#include "JNIMem.hpp" #include "helper_base.hpp" #include "helper_dbt.hpp" @@ -37,6 +36,7 @@ #include "direct_bt/DBTAdapter.hpp" using namespace direct_bt; +using namespace jau; void Java_direct_1bt_tinyb_DBTManager_initImpl(JNIEnv *env, jobject obj, jboolean unifyUUID128Bit, jint jbtMode) { @@ -46,7 +46,7 @@ void Java_direct_1bt_tinyb_DBTManager_initImpl(JNIEnv *env, jobject obj, jboolea DBTManager *manager = &DBTManager::get(btMode); // special: static singleton setInstance<DBTManager>(env, obj, manager); java_exception_check_and_throw(env, E_FILE_LINE); - manager->setJavaObject( std::shared_ptr<JavaAnonObj>( new JavaGlobalObj(obj, nullptr) ) ); + manager->setJavaObject( std::shared_ptr<JavaAnon>( new JavaGlobalObj(obj, nullptr) ) ); JavaGlobalObj::check(manager->getJavaObject(), E_FILE_LINE); DBG_PRINT("Java_direct_1bt_tinyb_DBTManager_init: Manager %s", manager->toString().c_str()); } catch(...) { @@ -100,7 +100,7 @@ jobject Java_direct_1bt_tinyb_DBTManager_getAdapterListImpl(JNIEnv *env, jobject jobject jAdapter = env->NewObject(clazz, clazz_ctor, (jlong)adapter, addr, name); java_exception_check_and_throw(env, E_FILE_LINE); JNIGlobalRef::check(jAdapter, E_FILE_LINE); - std::shared_ptr<JavaAnonObj> jAdapterRef = adapter->getJavaObject(); // GlobalRef + std::shared_ptr<JavaAnon> jAdapterRef = adapter->getJavaObject(); // GlobalRef JavaGlobalObj::check(jAdapterRef, E_FILE_LINE); env->DeleteLocalRef(addr); env->DeleteLocalRef(name); diff --git a/java/jni/direct_bt/DBTNativeDownlink.cxx b/java/jni/direct_bt/DBTNativeDownlink.cxx index 0ba8fe51..0202d78c 100644 --- a/java/jni/direct_bt/DBTNativeDownlink.cxx +++ b/java/jni/direct_bt/DBTNativeDownlink.cxx @@ -25,15 +25,15 @@ #include "direct_bt_tinyb_DBTNativeDownlink.h" -#include <dbt_debug.hpp> +#include <jau/debug.hpp> -#include "JNIMem.hpp" #include "helper_base.hpp" #include "helper_dbt.hpp" #include "direct_bt/DBTTypes.hpp" using namespace direct_bt; +using namespace jau; void Java_direct_1bt_tinyb_DBTNativeDownlink_initNativeJavaObject(JNIEnv *env, jobject obj, jlong nativeInstance) { diff --git a/java/jni/direct_bt/DBTObject.cxx b/java/jni/direct_bt/DBTObject.cxx index 25b578a0..d1116022 100644 --- a/java/jni/direct_bt/DBTObject.cxx +++ b/java/jni/direct_bt/DBTObject.cxx @@ -25,7 +25,6 @@ // #include "direct_bt_tinyb_DBTObject.h" -#include "JNIMem.hpp" #include "helper_base.hpp" #include "direct_bt/DBTTypes.hpp" diff --git a/java/jni/direct_bt/helper_dbt.cxx b/java/jni/direct_bt/helper_dbt.cxx index 5ce0489d..23d54105 100644 --- a/java/jni/direct_bt/helper_dbt.cxx +++ b/java/jni/direct_bt/helper_dbt.cxx @@ -34,18 +34,13 @@ using namespace direct_bt; DirectBTJNISettings direct_bt::directBTJNISettings; -jclass direct_bt::search_class(JNIEnv *env, JavaUplink &object) -{ - return search_class(env, object.get_java_class().c_str()); -} - static std::string jStringEmpty(""); static std::string jAddressTypePublic("public"); static std::string jAddressTypeRandom("random"); BDAddressType direct_bt::fromJavaAdressTypeToBDAddressType(JNIEnv *env, jstring jAddressType) { if( nullptr != jAddressType ) { - std::string saddressType = from_jstring_to_string(env, jAddressType); + std::string saddressType = jau::from_jstring_to_string(env, jAddressType); if( jAddressTypePublic == saddressType ) { return BDAddressType::BDADDR_LE_PUBLIC; } @@ -58,23 +53,13 @@ BDAddressType direct_bt::fromJavaAdressTypeToBDAddressType(JNIEnv *env, jstring jstring direct_bt::fromBDAddressTypeToJavaAddressType(JNIEnv *env, BDAddressType bdAddressType) { switch( bdAddressType ) { case BDAddressType::BDADDR_LE_PUBLIC: - return from_string_to_jstring(env, jAddressTypePublic); + return jau::from_string_to_jstring(env, jAddressTypePublic); case BDAddressType::BDADDR_LE_RANDOM: - return from_string_to_jstring(env, jAddressTypeRandom); + return jau::from_string_to_jstring(env, jAddressTypeRandom); case BDAddressType::BDADDR_BREDR: // fall through intended default: - return from_string_to_jstring(env, jStringEmpty); - } -} - -JavaGlobalObj::~JavaGlobalObj() noexcept { - jobject obj = javaObjectRef.getObject(); - if( nullptr == obj || nullptr == mNotifyDeleted ) { - return; + return jau::from_string_to_jstring(env, jStringEmpty); } - JNIEnv *env = *jni_env; - env->CallVoidMethod(obj, mNotifyDeleted); - java_exception_check_and_throw(env, E_FILE_LINE); // would abort() if thrown } diff --git a/java/jni/direct_bt/helper_dbt.hpp b/java/jni/direct_bt/helper_dbt.hpp index 45d92d7a..584e842d 100644 --- a/java/jni/direct_bt/helper_dbt.hpp +++ b/java/jni/direct_bt/helper_dbt.hpp @@ -26,11 +26,8 @@ #ifndef HELPER_DBT_HPP_ #define HELPER_DBT_HPP_ -#include "JNIMem.hpp" #include "helper_base.hpp" -#include "direct_bt/JavaUplink.hpp" -#include "direct_bt/BasicTypes.hpp" #include "direct_bt/BTAddress.hpp" namespace direct_bt { @@ -52,134 +49,9 @@ namespace direct_bt { }; extern DirectBTJNISettings directBTJNISettings; - /** - * Implementation for JavaAnonObj, - * by simply wrapping a JNIGlobalRef instance. - */ - class JavaGlobalObj : public JavaAnonObj { - private: - JNIGlobalRef javaObjectRef; - jmethodID mNotifyDeleted; - - public: - static inline void check(const std::shared_ptr<JavaAnonObj> & shref, const char* file, int line) { - if( nullptr == shref ) { - throw direct_bt::RuntimeException("JavaGlobalObj::check: Null shared-JavaAnonObj", file, line); - } - const jobject obj = static_cast<const JavaGlobalObj*>(shref.get())->getObject(); - if( nullptr == obj ) { - throw direct_bt::RuntimeException("JavaGlobalObj::check: Null object", file, line); - } - } - static bool isValid(const std::shared_ptr<JavaAnonObj> & shref) noexcept { - if( nullptr == shref ) { - return false; - } - const jobject obj = static_cast<const JavaGlobalObj*>(shref.get())->getObject(); - if( nullptr == obj ) { - return false; - } - return true; - } - JavaGlobalObj(jobject obj, jmethodID mNotifyDeleted) noexcept - : javaObjectRef(obj), mNotifyDeleted(mNotifyDeleted) { } - - JavaGlobalObj(const JavaGlobalObj &o) noexcept = default; - JavaGlobalObj(JavaGlobalObj &&o) noexcept = default; - JavaGlobalObj& operator=(const JavaGlobalObj &o) noexcept = default; - JavaGlobalObj& operator=(JavaGlobalObj &&o) noexcept = default; - - virtual ~JavaGlobalObj() noexcept; - - std::string toString() const noexcept override { - const uint64_t ref = (uint64_t)(void*)javaObjectRef.getObject(); - return "JavaGlobalObj["+uint64HexString(ref, true)+"]"; - } - - /** Clears the java reference, i.e. nulling it, without deleting the global reference via JNI. */ - void clear() noexcept override { javaObjectRef.clear(); } - - JNIGlobalRef & getJavaObject() noexcept { return javaObjectRef; } - - /* Provides access to the stored GlobalRef as an jobject. */ - jobject getObject() const noexcept { return javaObjectRef.getObject(); } - /* Provides access to the stored GlobalRef as a jclass. */ - jclass getClass() const noexcept { return javaObjectRef.getClass(); } - - /* Provides access to the stored GlobalRef as an getJavaObject. */ - static JNIGlobalRef GetJavaObject(const std::shared_ptr<JavaAnonObj> & shref) noexcept { - return static_cast<JavaGlobalObj*>(shref.get())->getJavaObject(); - } - /* Provides access to the stored GlobalRef as an jobject. */ - static jobject GetObject(const std::shared_ptr<JavaAnonObj> & shref) noexcept { - return static_cast<JavaGlobalObj*>(shref.get())->getObject(); - } - - /* Provides access to the stored GlobalRef as a jclass. */ - static jclass GetClass(const std::shared_ptr<JavaAnonObj> & shref) noexcept { - return static_cast<JavaGlobalObj*>(shref.get())->getClass(); - } - }; - - jclass search_class(JNIEnv *env, JavaUplink &object); - - template <typename T> - jobject convert_vector_sharedptr_to_jarraylist(JNIEnv *env, std::vector<std::shared_ptr<T>>& array) - { - unsigned int array_size = array.size(); - - jmethodID arraylist_add; - jobject result = get_new_arraylist(env, array_size, &arraylist_add); - - if (0 == array_size) { - return result; - } - - for (unsigned int i = 0; i < array_size; ++i) { - std::shared_ptr<T> elem = array[i]; - std::shared_ptr<JavaAnonObj> objref = elem->getJavaObject(); - if ( nullptr == objref ) { - throw InternalError("JavaUplink element of array has no valid java-object: "+elem->toString(), E_FILE_LINE); - } - env->CallBooleanMethod(result, arraylist_add, JavaGlobalObj::GetObject(objref)); - } - return result; - } - BDAddressType fromJavaAdressTypeToBDAddressType(JNIEnv *env, jstring jAddressType); jstring fromBDAddressTypeToJavaAddressType(JNIEnv *env, BDAddressType bdAddressType); - template <typename T> - T *getDBTObject(JNIEnv *env, jobject obj) - { - jlong instance = env->GetLongField(obj, getInstanceField(env, obj)); - T *t = reinterpret_cast<T *>(instance); - if (t == nullptr) { - throw std::runtime_error("Trying to acquire null DBTObject"); - } - t->checkValid(); - return t; - } - - template <typename T> - T *getDBTObjectUnchecked(JNIEnv *env, jobject obj) - { - jlong instance = env->GetLongField(obj, getInstanceField(env, obj)); - return reinterpret_cast<T *>(instance); - } - - template <typename T> - void setDBTObject(JNIEnv *env, jobject obj, T *t) - { - if (t == nullptr) { - throw std::runtime_error("Trying to create null DBTObject"); - } - jlong instance = reinterpret_cast<jlong>(t); - env->SetLongField(obj, getInstanceField(env, obj), instance); - } - - - } // namespace direct_bt #endif /* HELPER_DBT_HPP_ */ |