summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorSven Gothel <[email protected]>2020-07-26 05:35:58 +0200
committerSven Gothel <[email protected]>2020-07-26 05:35:58 +0200
commit083df38df99a0f771388867a8c03b856813ce854 (patch)
treefbe53a1c70151e5d09aa57ce67ea838a85e1db9c
parent9feddbb1dee50fcb8c90c2a257cbe0498849b567 (diff)
Have GATT[Service,Characteristic,Descriptor] derived from DBTObject for valid check; Use new JNI getInstance() -> getDBTObject() w/ valid check
-rw-r--r--api/direct_bt/DBTAdapter.hpp4
-rw-r--r--api/direct_bt/DBTTypes.hpp13
-rw-r--r--api/direct_bt/GATTCharacteristic.hpp4
-rw-r--r--api/direct_bt/GATTDescriptor.hpp4
-rw-r--r--api/direct_bt/GATTService.hpp4
-rw-r--r--java/jni/direct_bt/DBTAdapter.cxx38
-rw-r--r--java/jni/direct_bt/DBTDevice.cxx61
-rw-r--r--java/jni/direct_bt/DBTGattCharacteristic.cxx10
-rw-r--r--java/jni/direct_bt/DBTGattDescriptor.cxx6
-rw-r--r--java/jni/direct_bt/DBTGattService.cxx4
-rw-r--r--java/jni/direct_bt/helper_dbt.hpp31
-rw-r--r--src/direct_bt/DBTAdapter.cpp16
12 files changed, 118 insertions, 77 deletions
diff --git a/api/direct_bt/DBTAdapter.hpp b/api/direct_bt/DBTAdapter.hpp
index 727f70f1..682c0e0b 100644
--- a/api/direct_bt/DBTAdapter.hpp
+++ b/api/direct_bt/DBTAdapter.hpp
@@ -247,9 +247,9 @@ namespace direct_bt {
/**
* Throws an IllegalStateException if isValid() == false
*/
- inline void checkValid() const {
+ inline void checkValidAdapter() const {
if( !isValid() ) {
- throw IllegalStateException("Adapter state invalid: "+toString(), E_FILE_LINE);
+ throw IllegalStateException("Adapter state invalid: "+aptrHexString(this)+", "+toString(), E_FILE_LINE);
}
}
diff --git a/api/direct_bt/DBTTypes.hpp b/api/direct_bt/DBTTypes.hpp
index c5efa3cd..c900e1d0 100644
--- a/api/direct_bt/DBTTypes.hpp
+++ b/api/direct_bt/DBTTypes.hpp
@@ -46,8 +46,8 @@ namespace direct_bt {
class DBTObject : public JavaUplink
{
protected:
- std::mutex lk;
std::atomic_bool valid;
+ std::mutex lk;
DBTObject() : valid(true) {}
@@ -69,7 +69,16 @@ namespace direct_bt {
valid = false;
}
- bool isValid() const { return valid; }
+ inline bool isValid() const { return valid.load(); }
+
+ /**
+ * Throws an IllegalStateException if isValid() == false
+ */
+ inline void checkValid() const {
+ if( !isValid() ) {
+ throw IllegalStateException("DBTObject state invalid: "+aptrHexString(this), E_FILE_LINE);
+ }
+ }
};
/**
diff --git a/api/direct_bt/GATTCharacteristic.hpp b/api/direct_bt/GATTCharacteristic.hpp
index 67274a32..56d00f3b 100644
--- a/api/direct_bt/GATTCharacteristic.hpp
+++ b/api/direct_bt/GATTCharacteristic.hpp
@@ -40,6 +40,8 @@
#include "OctetTypes.hpp"
#include "ATTPDUTypes.hpp"
+#include "DBTTypes.hpp"
+
#include "GATTDescriptor.hpp"
#include "JavaUplink.hpp"
@@ -71,7 +73,7 @@ namespace direct_bt {
* and the value the Characteristics Property, Characteristics Value Handle _and_ Characteristics UUID.
* </p>
*/
- class GATTCharacteristic : public JavaUplink {
+ class GATTCharacteristic : public DBTObject {
private:
/** Characteristics's service weak back-reference */
std::weak_ptr<GATTService> wbr_service;
diff --git a/api/direct_bt/GATTDescriptor.hpp b/api/direct_bt/GATTDescriptor.hpp
index 97231526..0eaca70e 100644
--- a/api/direct_bt/GATTDescriptor.hpp
+++ b/api/direct_bt/GATTDescriptor.hpp
@@ -40,7 +40,7 @@
#include "OctetTypes.hpp"
#include "ATTPDUTypes.hpp"
-#include "JavaUplink.hpp"
+#include "DBTTypes.hpp"
/**
* - - - - - - - - - - - - - - -
@@ -59,7 +59,7 @@ namespace direct_bt {
/**
* BT Core Spec v5.2: Vol 3, Part G GATT: 3.3.3 Characteristic Descriptor
*/
- class GATTDescriptor : public JavaUplink {
+ class GATTDescriptor : public DBTObject {
private:
/** Descriptor's characteristic weak back-reference */
std::weak_ptr<GATTCharacteristic> wbr_characteristic;
diff --git a/api/direct_bt/GATTService.hpp b/api/direct_bt/GATTService.hpp
index 6f0bc71c..763298bc 100644
--- a/api/direct_bt/GATTService.hpp
+++ b/api/direct_bt/GATTService.hpp
@@ -40,6 +40,8 @@
#include "OctetTypes.hpp"
#include "ATTPDUTypes.hpp"
+#include "DBTTypes.hpp"
+
#include "GATTCharacteristic.hpp"
#include "JavaUplink.hpp"
@@ -61,7 +63,7 @@ namespace direct_bt {
* including its list of Characteristic Declarations,
* which also may include its client config if available.
*/
- class GATTService : public JavaUplink {
+ class GATTService : public DBTObject {
private:
/** Service's device weak back-reference */
std::weak_ptr<DBTDevice> wbr_device;
diff --git a/java/jni/direct_bt/DBTAdapter.cxx b/java/jni/direct_bt/DBTAdapter.cxx
index 26b3fbcb..c05c997a 100644
--- a/java/jni/direct_bt/DBTAdapter.cxx
+++ b/java/jni/direct_bt/DBTAdapter.cxx
@@ -373,12 +373,12 @@ jboolean Java_direct_1bt_tinyb_DBTAdapter_addStatusListener(JNIEnv *env, jobject
return false;
}
}
- DBTAdapter *adapter = getInstance<DBTAdapter>(env, obj);
+ DBTAdapter *adapter = getDBTObject<DBTAdapter>(env, obj);
JavaGlobalObj::check(adapter->getJavaObject(), E_FILE_LINE);
DBTDevice * deviceMatchRef = nullptr;
if( nullptr != jdeviceMatch ) {
- deviceMatchRef = getInstance<DBTDevice>(env, jdeviceMatch);
+ deviceMatchRef = getDBTObject<DBTDevice>(env, jdeviceMatch);
JavaGlobalObj::check(deviceMatchRef->getJavaObject(), E_FILE_LINE);
}
@@ -412,7 +412,7 @@ jboolean Java_direct_1bt_tinyb_DBTAdapter_removeStatusListener(JNIEnv *env, jobj
}
setObjectRef<JNIAdapterStatusListener>(env, statusListener, nullptr, "nativeInstance");
- DBTAdapter *adapter = getInstance<DBTAdapter>(env, obj);
+ DBTAdapter *adapter = getDBTObject<DBTAdapter>(env, obj);
JavaGlobalObj::check(adapter->getJavaObject(), E_FILE_LINE);
if( ! adapter->removeStatusListener( pre ) ) {
@@ -428,7 +428,7 @@ jboolean Java_direct_1bt_tinyb_DBTAdapter_removeStatusListener(JNIEnv *env, jobj
jint Java_direct_1bt_tinyb_DBTAdapter_removeAllStatusListener(JNIEnv *env, jobject obj) {
try {
- DBTAdapter *adapter = getInstance<DBTAdapter>(env, obj);
+ DBTAdapter *adapter = getDBTObject<DBTAdapter>(env, obj);
JavaGlobalObj::check(adapter->getJavaObject(), E_FILE_LINE);
return adapter->removeAllStatusListener();
@@ -440,7 +440,7 @@ 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 = getInstance<DBTAdapter>(env, obj);
+ DBTAdapter *adapter = getDBTObject<DBTAdapter>(env, obj);
JavaGlobalObj::check(adapter->getJavaObject(), E_FILE_LINE);
std::string saddress = from_jstring_to_string(env, jaddress);
@@ -456,7 +456,7 @@ jboolean Java_direct_1bt_tinyb_DBTAdapter_addDeviceToWhitelist__Ljava_lang_Strin
jshort min_interval, jshort max_interval,
jshort latency, jshort timeout) {
try {
- DBTAdapter *adapter = getInstance<DBTAdapter>(env, obj);
+ DBTAdapter *adapter = getDBTObject<DBTAdapter>(env, obj);
JavaGlobalObj::check(adapter->getJavaObject(), E_FILE_LINE);
std::string saddress = from_jstring_to_string(env, jaddress);
@@ -472,7 +472,7 @@ 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 = getInstance<DBTAdapter>(env, obj);
+ DBTAdapter *adapter = getDBTObject<DBTAdapter>(env, obj);
JavaGlobalObj::check(adapter->getJavaObject(), E_FILE_LINE);
std::string saddress = from_jstring_to_string(env, jaddress);
@@ -487,7 +487,7 @@ 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 = getInstance<DBTAdapter>(env, obj);
+ DBTAdapter *adapter = getDBTObject<DBTAdapter>(env, obj);
JavaGlobalObj::check(adapter->getJavaObject(), E_FILE_LINE);
std::string saddress = from_jstring_to_string(env, jaddress);
@@ -502,7 +502,7 @@ jboolean Java_direct_1bt_tinyb_DBTAdapter_removeDeviceFromWhitelist(JNIEnv *env,
jstring Java_direct_1bt_tinyb_DBTAdapter_toStringImpl(JNIEnv *env, jobject obj) {
try {
- DBTAdapter *nativePtr = getInstance<DBTAdapter>(env, obj);
+ DBTAdapter *nativePtr = getDBTObject<DBTAdapter>(env, obj);
JavaGlobalObj::check(nativePtr->getJavaObject(), E_FILE_LINE);
return from_string_to_jstring(env, nativePtr->toString());
} catch(...) {
@@ -526,7 +526,7 @@ void Java_direct_1bt_tinyb_DBTAdapter_deleteImpl(JNIEnv *env, jobject obj, jlong
jboolean Java_direct_1bt_tinyb_DBTAdapter_startDiscoveryImpl(JNIEnv *env, jobject obj, jboolean keepAlive)
{
try {
- DBTAdapter *adapter = getInstance<DBTAdapter>(env, obj);
+ DBTAdapter *adapter = getDBTObject<DBTAdapter>(env, obj);
return adapter->startDiscovery(keepAlive);
} catch(...) {
rethrow_and_raise_java_exception(env);
@@ -537,7 +537,7 @@ jboolean Java_direct_1bt_tinyb_DBTAdapter_startDiscoveryImpl(JNIEnv *env, jobjec
jboolean Java_direct_1bt_tinyb_DBTAdapter_stopDiscoveryImpl(JNIEnv *env, jobject obj)
{
try {
- DBTAdapter *adapter = getInstance<DBTAdapter>(env, obj);
+ DBTAdapter *adapter = getDBTObject<DBTAdapter>(env, obj);
adapter->stopDiscovery();
return JNI_TRUE;
} catch(...) {
@@ -549,7 +549,7 @@ jboolean Java_direct_1bt_tinyb_DBTAdapter_stopDiscoveryImpl(JNIEnv *env, jobject
jobject Java_direct_1bt_tinyb_DBTAdapter_getDiscoveredDevicesImpl(JNIEnv *env, jobject obj)
{
try {
- DBTAdapter *adapter = getInstance<DBTAdapter>(env, obj);
+ DBTAdapter *adapter = getDBTObject<DBTAdapter>(env, obj);
std::vector<std::shared_ptr<DBTDevice>> array = adapter->getDiscoveredDevices();
return convert_vector_sharedptr_to_jarraylist(env, array);
} catch(...) {
@@ -561,7 +561,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 = getInstance<DBTAdapter>(env, obj);
+ DBTAdapter *adapter = getDBTObject<DBTAdapter>(env, obj);
return adapter->removeDiscoveredDevices();
} catch(...) {
rethrow_and_raise_java_exception(env);
@@ -575,7 +575,7 @@ jint Java_direct_1bt_tinyb_DBTAdapter_removeDevicesImpl(JNIEnv *env, jobject obj
void Java_direct_1bt_tinyb_DBTAdapter_setPowered(JNIEnv *env, jobject obj, jboolean value) {
try {
- DBTAdapter *adapter = getInstance<DBTAdapter>(env, obj);
+ DBTAdapter *adapter = getDBTObject<DBTAdapter>(env, obj);
JavaGlobalObj::check(adapter->getJavaObject(), E_FILE_LINE);
adapter->setPowered(JNI_TRUE == value ? true : false);
} catch(...) {
@@ -585,7 +585,7 @@ void Java_direct_1bt_tinyb_DBTAdapter_setPowered(JNIEnv *env, jobject obj, jbool
jstring Java_direct_1bt_tinyb_DBTAdapter_getAlias(JNIEnv *env, jobject obj) {
try {
- DBTAdapter *adapter = getInstance<DBTAdapter>(env, obj);
+ DBTAdapter *adapter = getDBTObject<DBTAdapter>(env, obj);
JavaGlobalObj::check(adapter->getJavaObject(), E_FILE_LINE);
return from_string_to_jstring(env, adapter->getLocalName().getName());
} catch(...) {
@@ -596,7 +596,7 @@ 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 = getInstance<DBTAdapter>(env, obj);
+ DBTAdapter *adapter = getDBTObject<DBTAdapter>(env, obj);
JavaGlobalObj::check(adapter->getJavaObject(), E_FILE_LINE);
std::string newalias = from_jstring_to_string(env, jnewalias);
adapter->setLocalName(newalias, std::string());
@@ -607,7 +607,7 @@ void Java_direct_1bt_tinyb_DBTAdapter_setAlias(JNIEnv *env, jobject obj, jstring
void Java_direct_1bt_tinyb_DBTAdapter_setDiscoverable(JNIEnv *env, jobject obj, jboolean value) {
try {
- DBTAdapter *adapter = getInstance<DBTAdapter>(env, obj);
+ DBTAdapter *adapter = getDBTObject<DBTAdapter>(env, obj);
JavaGlobalObj::check(adapter->getJavaObject(), E_FILE_LINE);
adapter->setDiscoverable(JNI_TRUE == value ? true : false);
} catch(...) {
@@ -617,7 +617,7 @@ void Java_direct_1bt_tinyb_DBTAdapter_setDiscoverable(JNIEnv *env, jobject obj,
jobject Java_direct_1bt_tinyb_DBTAdapter_connectDevice(JNIEnv *env, jobject obj, jstring jaddress, jstring jaddressType) {
try {
- DBTAdapter *adapter = getInstance<DBTAdapter>(env, obj);
+ DBTAdapter *adapter = getDBTObject<DBTAdapter>(env, obj);
JavaGlobalObj::check(adapter->getJavaObject(), E_FILE_LINE);
std::string saddress = from_jstring_to_string(env, jaddress);
EUI48 address(saddress);
@@ -642,7 +642,7 @@ jobject Java_direct_1bt_tinyb_DBTAdapter_connectDevice(JNIEnv *env, jobject obj,
void Java_direct_1bt_tinyb_DBTAdapter_setPairable(JNIEnv *env, jobject obj, jboolean value) {
try {
- DBTAdapter *adapter = getInstance<DBTAdapter>(env, obj);
+ DBTAdapter *adapter = getDBTObject<DBTAdapter>(env, obj);
JavaGlobalObj::check(adapter->getJavaObject(), E_FILE_LINE);
adapter->setBondable(JNI_TRUE == value ? true : false);
} catch(...) {
diff --git a/java/jni/direct_bt/DBTDevice.cxx b/java/jni/direct_bt/DBTDevice.cxx
index ae57644a..d28ac0c2 100644
--- a/java/jni/direct_bt/DBTDevice.cxx
+++ b/java/jni/direct_bt/DBTDevice.cxx
@@ -141,7 +141,7 @@ class JNICharacteristicListener : public GATTCharacteristicListener {
void Java_direct_1bt_tinyb_DBTDevice_initImpl(JNIEnv *env, jobject obj)
{
try {
- DBTDevice *device = getInstance<DBTDevice>(env, obj);
+ DBTDevice *device = getDBTObject<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 = getInstance<DBTDevice>(env, obj);
+ DBTDevice *nativePtr = getDBTObject<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 = getInstance<DBTDevice>(env, obj);
+ DBTDevice *nativePtr = getDBTObject<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 = getInstance<DBTDevice>(env, obj);
+ DBTDevice *device = getDBTObject<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 = getInstance<GATTCharacteristic>(env, jAssociatedCharacteristic);
+ associatedCharacteristicRef = getDBTObject<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 = getInstance<DBTDevice>(env, obj);
+ DBTDevice *device = getDBTObject<DBTDevice>(env, obj);
JavaGlobalObj::check(device->getJavaObject(), E_FILE_LINE);
std::shared_ptr<GATTHandler> gatt = device->getGATTHandler();
if( nullptr == gatt ) {
@@ -246,7 +246,7 @@ jint Java_direct_1bt_tinyb_DBTDevice_removeAllAssociatedCharacteristicListener(J
if( nullptr == jAssociatedCharacteristic ) {
throw IllegalArgumentException("associatedCharacteristic argument is null", E_FILE_LINE);
}
- DBTDevice *device = getInstance<DBTDevice>(env, obj);
+ DBTDevice *device = getDBTObject<DBTDevice>(env, obj);
JavaGlobalObj::check(device->getJavaObject(), E_FILE_LINE);
std::shared_ptr<GATTHandler> gatt = device->getGATTHandler();
if( nullptr == gatt ) {
@@ -255,7 +255,7 @@ jint Java_direct_1bt_tinyb_DBTDevice_removeAllAssociatedCharacteristicListener(J
return 0;
}
- GATTCharacteristic * associatedCharacteristicRef = getInstance<GATTCharacteristic>(env, jAssociatedCharacteristic);
+ GATTCharacteristic * associatedCharacteristicRef = getDBTObject<GATTCharacteristic>(env, jAssociatedCharacteristic);
JavaGlobalObj::check(associatedCharacteristicRef->getJavaObject(), E_FILE_LINE);
return gatt->removeAllAssociatedCharacteristicListener(associatedCharacteristicRef);
@@ -267,7 +267,7 @@ jint Java_direct_1bt_tinyb_DBTDevice_removeAllAssociatedCharacteristicListener(J
jint Java_direct_1bt_tinyb_DBTDevice_removeAllCharacteristicListener(JNIEnv *env, jobject obj) {
try {
- DBTDevice *device = getInstance<DBTDevice>(env, obj);
+ DBTDevice *device = getDBTObject<DBTDevice>(env, obj);
JavaGlobalObj::check(device->getJavaObject(), E_FILE_LINE);
std::shared_ptr<GATTHandler> gatt = device->getGATTHandler();
if( nullptr == gatt ) {
@@ -298,7 +298,7 @@ void Java_direct_1bt_tinyb_DBTDevice_deleteImpl(JNIEnv *env, jobject obj, jlong
jboolean Java_direct_1bt_tinyb_DBTDevice_disconnectImpl(JNIEnv *env, jobject obj)
{
try {
- DBTDevice *device = getInstance<DBTDevice>(env, obj);
+ DBTDevice *device = getDBTObject<DBTDevice>(env, obj);
JavaGlobalObj::check(device->getJavaObject(), E_FILE_LINE);
return device->disconnect() ? JNI_TRUE : JNI_FALSE;
} catch(...) {
@@ -310,7 +310,7 @@ jboolean Java_direct_1bt_tinyb_DBTDevice_disconnectImpl(JNIEnv *env, jobject obj
jboolean Java_direct_1bt_tinyb_DBTDevice_removeImpl(JNIEnv *env, jobject obj)
{
try {
- DBTDevice *device = getInstance<DBTDevice>(env, obj);
+ DBTDevice *device = getDBTObject<DBTDevice>(env, obj);
JavaGlobalObj::check(device->getJavaObject(), E_FILE_LINE);
device->remove();
} catch(...) {
@@ -322,7 +322,7 @@ jboolean Java_direct_1bt_tinyb_DBTDevice_removeImpl(JNIEnv *env, jobject obj)
jboolean Java_direct_1bt_tinyb_DBTDevice_connectImpl__(JNIEnv *env, jobject obj)
{
try {
- DBTDevice *device = getInstance<DBTDevice>(env, obj);
+ DBTDevice *device = getDBTObject<DBTDevice>(env, obj);
JavaGlobalObj::check(device->getJavaObject(), E_FILE_LINE);
bool res = device->connectDefault();
return res ? JNI_TRUE : JNI_FALSE;
@@ -338,7 +338,7 @@ jboolean Java_direct_1bt_tinyb_DBTDevice_connectImpl__SSSSSS(JNIEnv *env, jobjec
jshort latency, jshort timeout)
{
try {
- DBTDevice *device = getInstance<DBTDevice>(env, obj);
+ DBTDevice *device = getDBTObject<DBTDevice>(env, obj);
JavaGlobalObj::check(device->getJavaObject(), E_FILE_LINE);
bool res;
switch( device->addressType ) {
@@ -366,7 +366,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 = getInstance<DBTDevice>(env, obj);
+ DBTDevice *device = getDBTObject<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
@@ -387,10 +387,7 @@ jobject Java_direct_1bt_tinyb_DBTDevice_getServicesImpl(JNIEnv *env, jobject obj
std::function<jobject(JNIEnv*, jclass, jmethodID, GATTService*)> ctor_service =
[](JNIEnv *env, jclass clazz, jmethodID clazz_ctor, GATTService *service)->jobject {
// prepare adapter ctor
- std::shared_ptr<DBTDevice> device = service->getDevice();
- if( nullptr == device ) {
- throw IllegalStateException("Service's DBTDevice destructed: "+service->toString(), E_FILE_LINE);
- }
+ std::shared_ptr<DBTDevice> device = service->getDeviceChecked();
JavaGlobalObj::check(device->getJavaObject(), E_FILE_LINE);
jobject jdevice = JavaGlobalObj::GetObject(device->getJavaObject());
const jboolean isPrimary = service->isPrimary;
@@ -418,7 +415,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 = getInstance<DBTDevice>(env, obj);
+ DBTDevice *device = getDBTObject<DBTDevice>(env, obj);
JavaGlobalObj::check(device->getJavaObject(), E_FILE_LINE);
return device->pingGATT() ? JNI_TRUE : JNI_FALSE;
@@ -431,7 +428,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 = getInstance<DBTDevice>(env, obj);
+ DBTDevice *device = getDBTObject<DBTDevice>(env, obj);
JavaGlobalObj::check(device->getJavaObject(), E_FILE_LINE);
return nullptr; // FIXME
} catch(...) {
@@ -443,7 +440,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 = getInstance<DBTDevice>(env, obj);
+ DBTDevice *device = getDBTObject<DBTDevice>(env, obj);
JavaGlobalObj::check(device->getJavaObject(), E_FILE_LINE);
(void)value;
// FIXME
@@ -455,7 +452,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 = getInstance<DBTDevice>(env, obj);
+ DBTDevice *device = getDBTObject<DBTDevice>(env, obj);
JavaGlobalObj::check(device->getJavaObject(), E_FILE_LINE);
(void)value;
// FIXME
@@ -467,7 +464,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 = getInstance<DBTDevice>(env, obj);
+ DBTDevice *device = getDBTObject<DBTDevice>(env, obj);
JavaGlobalObj::check(device->getJavaObject(), E_FILE_LINE);
return JNI_FALSE; // FIXME
} catch(...) {
@@ -479,7 +476,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 = getInstance<DBTDevice>(env, obj);
+ DBTDevice *device = getDBTObject<DBTDevice>(env, obj);
JavaGlobalObj::check(device->getJavaObject(), E_FILE_LINE);
return (jshort) device->getRSSI();
} catch(...) {
@@ -492,7 +489,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 = getInstance<DBTDevice>(env, obj);
+ DBTDevice *device = getDBTObject<DBTDevice>(env, obj);
JavaGlobalObj::check(device->getJavaObject(), E_FILE_LINE);
return nullptr; // FIXME
} catch(...) {
@@ -504,7 +501,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 = getInstance<DBTDevice>(env, obj);
+ DBTDevice *device = getDBTObject<DBTDevice>(env, obj);
JavaGlobalObj::check(device->getJavaObject(), E_FILE_LINE);
return nullptr; // FIXME
} catch(...) {
@@ -516,7 +513,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 = getInstance<DBTDevice>(env, obj);
+ DBTDevice *device = getDBTObject<DBTDevice>(env, obj);
JavaGlobalObj::check(device->getJavaObject(), E_FILE_LINE);
std::shared_ptr<ManufactureSpecificData> mdata = device->getManufactureSpecificData();
@@ -553,7 +550,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 = getInstance<DBTDevice>(env, obj);
+ DBTDevice *device = getDBTObject<DBTDevice>(env, obj);
JavaGlobalObj::check(device->getJavaObject(), E_FILE_LINE);
return (jshort) device->getTxPower();
} catch(...) {
@@ -613,7 +610,7 @@ static void disableBlockedNotifications(JNIEnv *env, jobject obj, DBTManager &mg
void Java_direct_1bt_tinyb_DBTDevice_disableBlockedNotificationsImpl(JNIEnv *env, jobject obj)
{
try {
- DBTDevice *device = getInstance<DBTDevice>(env, obj);
+ DBTDevice *device = getDBTObject<DBTDevice>(env, obj);
JavaGlobalObj::check(device->getJavaObject(), E_FILE_LINE);
DBTManager & mgmt = device->getAdapter().getManager();
@@ -625,7 +622,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= getInstance<DBTDevice>(env, obj);
+ DBTDevice *device= getDBTObject<DBTDevice>(env, obj);
JavaGlobalObj::check(device->getJavaObject(), E_FILE_LINE);
DBTAdapter & adapter = device->getAdapter();
DBTManager & mgmt = adapter.getManager();
@@ -700,7 +697,7 @@ static void disablePairedNotifications(JNIEnv *env, jobject obj, DBTManager &mgm
void Java_direct_1bt_tinyb_DBTDevice_disablePairedNotificationsImpl(JNIEnv *env, jobject obj)
{
try {
- DBTDevice *device = getInstance<DBTDevice>(env, obj);
+ DBTDevice *device = getDBTObject<DBTDevice>(env, obj);
JavaGlobalObj::check(device->getJavaObject(), E_FILE_LINE);
DBTManager & mgmt = device->getAdapter().getManager();
@@ -712,7 +709,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= getInstance<DBTDevice>(env, obj);
+ DBTDevice *device= getDBTObject<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 84483d6a..6260818a 100644
--- a/java/jni/direct_bt/DBTGattCharacteristic.cxx
+++ b/java/jni/direct_bt/DBTGattCharacteristic.cxx
@@ -39,7 +39,7 @@ using namespace direct_bt;
jstring Java_direct_1bt_tinyb_DBTGattCharacteristic_toStringImpl(JNIEnv *env, jobject obj) {
try {
- GATTCharacteristic *nativePtr = getInstance<GATTCharacteristic>(env, obj);
+ GATTCharacteristic *nativePtr = getDBTObject<GATTCharacteristic>(env, obj);
JavaGlobalObj::check(nativePtr->getJavaObject(), E_FILE_LINE);
return from_string_to_jstring(env, nativePtr->toString());
} catch(...) {
@@ -63,7 +63,7 @@ static const std::string _descriptorClazzCtorArgs("(JLdirect_bt/tinyb/DBTGattCha
jobject Java_direct_1bt_tinyb_DBTGattCharacteristic_getDescriptorsImpl(JNIEnv *env, jobject obj) {
try {
- GATTCharacteristic *characteristic = getInstance<GATTCharacteristic>(env, obj);
+ GATTCharacteristic *characteristic = getDBTObject<GATTCharacteristic>(env, obj);
JavaGlobalObj::check(characteristic->getJavaObject(), E_FILE_LINE);
std::vector<GATTDescriptorRef> & descriptorList = characteristic->descriptorList;
@@ -112,7 +112,7 @@ jobject Java_direct_1bt_tinyb_DBTGattCharacteristic_getDescriptorsImpl(JNIEnv *e
jbyteArray Java_direct_1bt_tinyb_DBTGattCharacteristic_readValueImpl(JNIEnv *env, jobject obj) {
try {
- GATTCharacteristic *characteristic = getInstance<GATTCharacteristic>(env, obj);
+ GATTCharacteristic *characteristic = getDBTObject<GATTCharacteristic>(env, obj);
JavaGlobalObj::check(characteristic->getJavaObject(), E_FILE_LINE);
POctets res(GATTHandler::number(GATTHandler::Defaults::MAX_ATT_MTU), 0);
@@ -142,7 +142,7 @@ jboolean Java_direct_1bt_tinyb_DBTGattCharacteristic_writeValueImpl(JNIEnv *env,
if( 0 == value_size ) {
return JNI_TRUE;
}
- GATTCharacteristic *characteristic = getInstance<GATTCharacteristic>(env, obj);
+ GATTCharacteristic *characteristic = getDBTObject<GATTCharacteristic>(env, obj);
JavaGlobalObj::check(characteristic->getJavaObject(), E_FILE_LINE);
JNICriticalArray<uint8_t, jbyteArray> criticalArray(env); // RAII - release
@@ -165,7 +165,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 = getInstanceUnchecked<GATTCharacteristic>(env, obj);
+ GATTCharacteristic *characteristic = getDBTObjectUnchecked<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 066e9f5f..d1462825 100644
--- a/java/jni/direct_bt/DBTGattDescriptor.cxx
+++ b/java/jni/direct_bt/DBTGattDescriptor.cxx
@@ -39,7 +39,7 @@ using namespace direct_bt;
void Java_direct_1bt_tinyb_DBTGattDescriptor_deleteImpl(JNIEnv *env, jobject obj) {
try {
- GATTDescriptor *descriptor = getInstance<GATTDescriptor>(env, obj);
+ GATTDescriptor *descriptor = getDBTObject<GATTDescriptor>(env, obj);
(void)descriptor;
// No delete: Service instance owned by GATTService -> DBTDevice
} 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 = getInstance<GATTDescriptor>(env, obj);
+ GATTDescriptor *descriptor = getDBTObject<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 = getInstance<GATTDescriptor>(env, obj);
+ GATTDescriptor *descriptor = getDBTObject<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 7023d755..ef25f486 100644
--- a/java/jni/direct_bt/DBTGattService.cxx
+++ b/java/jni/direct_bt/DBTGattService.cxx
@@ -39,7 +39,7 @@ using namespace direct_bt;
jstring Java_direct_1bt_tinyb_DBTGattService_toStringImpl(JNIEnv *env, jobject obj) {
try {
- GATTService *nativePtr = getInstance<GATTService>(env, obj);
+ GATTService *nativePtr = getDBTObject<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 = getInstance<GATTService>(env, obj);
+ GATTService *service = getDBTObject<GATTService>(env, obj);
JavaGlobalObj::check(service->getJavaObject(), E_FILE_LINE);
std::vector<std::shared_ptr<GATTCharacteristic>> & characteristics = service->characteristicList;
diff --git a/java/jni/direct_bt/helper_dbt.hpp b/java/jni/direct_bt/helper_dbt.hpp
index abd78b50..f644d58c 100644
--- a/java/jni/direct_bt/helper_dbt.hpp
+++ b/java/jni/direct_bt/helper_dbt.hpp
@@ -149,6 +149,37 @@ namespace direct_bt {
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_ */
diff --git a/src/direct_bt/DBTAdapter.cpp b/src/direct_bt/DBTAdapter.cpp
index 8e2b858e..ef64e2dc 100644
--- a/src/direct_bt/DBTAdapter.cpp
+++ b/src/direct_bt/DBTAdapter.cpp
@@ -32,7 +32,7 @@
#include <algorithm>
-// #define VERBOSE_ON 1
+#define VERBOSE_ON 1
#include <dbt_debug.hpp>
#include "BasicAlgos.hpp"
@@ -262,7 +262,7 @@ bool DBTAdapter::isDeviceWhitelisted(const EUI48 &address) {
bool DBTAdapter::addDeviceToWhitelist(const EUI48 &address, const BDAddressType address_type, const HCIWhitelistConnectType ctype,
const uint16_t conn_interval_min, const uint16_t conn_interval_max,
const uint16_t conn_latency, const uint16_t timeout) {
- checkValid();
+ checkValidAdapter();
if( mgmt.isDeviceWhitelisted(dev_id, address) ) {
ERR_PRINT("DBTAdapter::addDeviceToWhitelist: device already listed: dev_id %d, address %s", dev_id, address.toString().c_str());
return true;
@@ -276,12 +276,12 @@ bool DBTAdapter::addDeviceToWhitelist(const EUI48 &address, const BDAddressType
}
bool DBTAdapter::removeDeviceFromWhitelist(const EUI48 &address, const BDAddressType address_type) {
- checkValid();
+ checkValidAdapter();
return mgmt.removeDeviceFromWhitelist(dev_id, address, address_type);
}
bool DBTAdapter::addStatusListener(std::shared_ptr<AdapterStatusListener> l) {
- checkValid();
+ checkValidAdapter();
if( nullptr == l ) {
throw IllegalArgumentException("DBTAdapterStatusListener ref is null", E_FILE_LINE);
}
@@ -298,7 +298,7 @@ bool DBTAdapter::addStatusListener(std::shared_ptr<AdapterStatusListener> l) {
}
bool DBTAdapter::removeStatusListener(std::shared_ptr<AdapterStatusListener> l) {
- checkValid();
+ checkValidAdapter();
if( nullptr == l ) {
throw IllegalArgumentException("DBTAdapterStatusListener ref is null", E_FILE_LINE);
}
@@ -315,7 +315,7 @@ bool DBTAdapter::removeStatusListener(std::shared_ptr<AdapterStatusListener> l)
}
bool DBTAdapter::removeStatusListener(const AdapterStatusListener * l) {
- checkValid();
+ checkValidAdapter();
if( nullptr == l ) {
throw IllegalArgumentException("DBTAdapterStatusListener ref is null", E_FILE_LINE);
}
@@ -332,7 +332,7 @@ bool DBTAdapter::removeStatusListener(const AdapterStatusListener * l) {
}
int DBTAdapter::removeAllStatusListener() {
- checkValid();
+ checkValidAdapter();
const std::lock_guard<std::recursive_mutex> lock(mtx_statusListenerList); // RAII-style acquire and relinquish via destructor
int count = statusListenerList.size();
statusListenerList.clear();
@@ -342,7 +342,7 @@ int DBTAdapter::removeAllStatusListener() {
bool DBTAdapter::startDiscovery(const bool keepAlive, const HCILEOwnAddressType own_mac_type,
const uint16_t le_scan_interval, const uint16_t le_scan_window)
{
- checkValid();
+ checkValidAdapter();
const std::lock_guard<std::recursive_mutex> lock(mtx_discovery); // RAII-style acquire and relinquish via destructor
if( ScanType::SCAN_TYPE_NONE != currentScanType ) {
removeDiscoveredDevices();