diff options
author | Sven Gothel <[email protected]> | 2020-05-18 12:53:49 +0200 |
---|---|---|
committer | Sven Gothel <[email protected]> | 2020-05-18 12:53:49 +0200 |
commit | d2aba7577f74408ee0a9a35d1a96c6f74dcd55a5 (patch) | |
tree | d5137ce5c03f1d7bf9a30d7468108c74df64481d | |
parent | 55ab45201dc7035487da5286302494bbf840ea67 (diff) |
Expose UUID strings in TinyB compatible fashion (unified uuid128_t) as an option (default = true)
Via DBTManager.setUnifyUUID128Bit(boolean):
Enables or disables uuid128_t consolidation
for native uuid16_t and uuid32_t values before string conversion.
Default is {@code true}, as this represent compatibility with original TinyB D-Bus behavior.
If desired, this value should be set once before the first call of {@link #getBluetoothManager()}!
-rw-r--r-- | examples/java/ScannerTinyB01.java | 37 | ||||
-rw-r--r-- | java/direct_bt/tinyb/DBTManager.java | 25 | ||||
-rw-r--r-- | java/jni/direct_bt/DBTDevice.cxx | 4 | ||||
-rw-r--r-- | java/jni/direct_bt/DBTGattCharacteristic.cxx | 4 | ||||
-rw-r--r-- | java/jni/direct_bt/DBTGattService.cxx | 4 | ||||
-rw-r--r-- | java/jni/direct_bt/DBTManager.cxx | 3 | ||||
-rw-r--r-- | java/jni/direct_bt/helper_dbt.cxx | 2 | ||||
-rw-r--r-- | java/jni/direct_bt/helper_dbt.hpp | 17 | ||||
-rw-r--r-- | src/direct_bt/UUID.cpp | 6 |
9 files changed, 72 insertions, 30 deletions
diff --git a/examples/java/ScannerTinyB01.java b/examples/java/ScannerTinyB01.java index 5cbb81a6..7ffe8ccb 100644 --- a/examples/java/ScannerTinyB01.java +++ b/examples/java/ScannerTinyB01.java @@ -49,22 +49,23 @@ public class ScannerTinyB01 { /** 10,000 milliseconds */ static long TO_DISCOVER = 10000; + static final String EUI48_ANY_DEVICE = "00:00:00:00:00:00"; + static String waitForDevice = EUI48_ANY_DEVICE; + public static void main(final String[] args) throws InterruptedException { long t0_discovery = TO_DISCOVER; int factory = 0; int dev_id = 0; // default int mode = 0; boolean forever = false; - final String mac; { - String _mac = null; for(int i=0; i< args.length; i++) { final String arg = args[i]; if( arg.equals("-dev_id") && args.length > (i+1) ) { dev_id = Integer.valueOf(args[++i]).intValue(); } else if( arg.equals("-mac") && args.length > (i+1) ) { - _mac = args[++i]; + waitForDevice = args[++i]; } else if( arg.equals("-mode") && args.length > (i+1) ) { mode = Integer.valueOf(args[++i]).intValue(); } else if( arg.equals("-factory") && args.length > (i+1) ) { @@ -76,13 +77,15 @@ public class ScannerTinyB01 { } } - if ( null == _mac ) { + if ( EUI48_ANY_DEVICE.equals(waitForDevice) ) { System.err.println("Run with '-mac <device_address> [-dev_id <adapter-index>] [-mode <mode>] [-factory <BluetoothManager-Factory-Implementation-Class>]'"); System.exit(-1); } - mac = _mac; } + System.err.println("dev_id "+dev_id); + System.err.println("waitForDevice: "+waitForDevice); + final BluetoothFactory.ImplementationIdentifier implID = 0 == factory ? BluetoothFactory.DirectBTImplementationID : BluetoothFactory.DBusImplementationID; final BluetoothManager manager; { @@ -124,7 +127,7 @@ public class ScannerTinyB01 { @Override public void deviceFound(final BluetoothDevice device, final long timestamp) { - final boolean matches = device.getAddress().equals(mac); + final boolean matches = device.getAddress().equals(waitForDevice); System.err.println("****** FOUND__: "+device.toString()+" - match "+matches); System.err.println("Status Adapter:"); System.err.println(device.getAdapter().toString()); @@ -139,7 +142,7 @@ public class ScannerTinyB01 { @Override public void deviceUpdated(final BluetoothDevice device, final long timestamp, final EIRDataTypeSet updateMask) { - final boolean matches = device.getAddress().equals(mac); + final boolean matches = device.getAddress().equals(waitForDevice); System.err.println("****** UPDATED: "+updateMask+" of "+device+" - match "+matches); System.err.println("Status Adapter:"); System.err.println(device.getAdapter().toString()); @@ -147,7 +150,7 @@ public class ScannerTinyB01 { @Override public void deviceConnected(final BluetoothDevice device, final long timestamp) { - final boolean matches = device.getAddress().equals(mac); + final boolean matches = device.getAddress().equals(waitForDevice); System.err.println("****** CONNECTED: "+device.toString()+" - match "+matches); System.err.println("Status Adapter:"); System.err.println(device.getAdapter().toString()); @@ -155,7 +158,7 @@ public class ScannerTinyB01 { @Override public void deviceDisconnected(final BluetoothDevice device, final long timestamp) { - final boolean matches = device.getAddress().equals(mac); + final boolean matches = device.getAddress().equals(waitForDevice); System.err.println("****** DISCONNECTED: "+device.toString()+" - match "+matches); System.err.println("Status Adapter:"); System.err.println(device.getAdapter().toString()); @@ -213,7 +216,7 @@ public class ScannerTinyB01 { final boolean discoveryStarted = adapter.startDiscovery(); - System.err.println("The discovery started: " + (discoveryStarted ? "true" : "false") + " for mac "+mac+", mode "+mode); + System.err.println("The discovery started: " + (discoveryStarted ? "true" : "false") + " for mac "+waitForDevice+", mode "+mode); if( !discoveryStarted ) { break; } @@ -231,14 +234,14 @@ public class ScannerTinyB01 { matchingDiscoveredDeviceBucket[0] = null; } } else if( 1 == mode ) { - sensor = adapter.find(null, mac, t0_discovery); + sensor = adapter.find(null, waitForDevice, t0_discovery); } else { boolean timeout = false; while( null == sensor && !timeout ) { final List<BluetoothDevice> devices = adapter.getDevices(); for(final Iterator<BluetoothDevice> id = devices.iterator(); id.hasNext() && !timeout; ) { final BluetoothDevice d = id.next(); - if(d.getAddress().equals(mac)) { + if(d.getAddress().equals(waitForDevice)) { sensor = d; break; } @@ -348,15 +351,7 @@ public class ScannerTinyB01 { for (final BluetoothGattDescriptor d : descriptors) { System.err.println(" Descriptor: " + d); } - final String uuid; - { - final String _uuid = c.getUUID().toLowerCase(); - if( 4 == _uuid.length() ) { - uuid = _uuid + "-"; // FIXME: uuid16_t -> uuid128_t string?? - } else { - uuid = _uuid; - } - } + final String uuid = c.getUUID(); System.err.println("**** Quering: " + uuid); if (uuid.contains("2a29-")) { diff --git a/java/direct_bt/tinyb/DBTManager.java b/java/direct_bt/tinyb/DBTManager.java index 4a5212ba..e1c669b9 100644 --- a/java/direct_bt/tinyb/DBTManager.java +++ b/java/direct_bt/tinyb/DBTManager.java @@ -45,6 +45,7 @@ public class DBTManager implements BluetoothManager private static volatile boolean isJVMShuttingDown = false; private static final List<Runnable> userShutdownHooks = new ArrayList<Runnable>(); + private static boolean unifyUUID128Bit = true; static { AccessController.doPrivileged(new PrivilegedAction<Object>() { @@ -122,6 +123,26 @@ public class DBTManager implements BluetoothManager } } + /** + * Returns whether uuid128_t consolidation is enabled + * for native uuid16_t and uuid32_t values before string conversion. + * @see #setUnifyUUID128Bit(boolean) + */ + public static boolean getUnifyUUID128Bit() { return unifyUUID128Bit; } + + /** + * Enables or disables uuid128_t consolidation + * for native uuid16_t and uuid32_t values before string conversion. + * <p> + * Default is {@code true}, as this represent compatibility with original TinyB D-Bus behavior. + * </p> + * <p> + * If desired, this value should be set once before the first call of {@link #getBluetoothManager()}! + * </p> + * @see #getUnifyUUID128Bit() + */ + public static void setUnifyUUID128Bit(final boolean v) { unifyUUID128Bit=v; } + private long nativeInstance; private static DBTManager inst; private final List<BluetoothAdapter> adapters = new ArrayList<BluetoothAdapter>(); @@ -243,11 +264,11 @@ public class DBTManager implements BluetoothManager private native List<BluetoothAdapter> getAdapterListImpl(); - private native void initImpl() throws BluetoothException; + private native void initImpl(final boolean unifyUUID128Bit) throws BluetoothException; private native void deleteImpl(); private DBTManager() { - initImpl(); + initImpl(unifyUUID128Bit); try { adapters.addAll(getAdapterListImpl()); } catch (final BluetoothException be) { diff --git a/java/jni/direct_bt/DBTDevice.cxx b/java/jni/direct_bt/DBTDevice.cxx index cb249d4b..726a7ad4 100644 --- a/java/jni/direct_bt/DBTDevice.cxx +++ b/java/jni/direct_bt/DBTDevice.cxx @@ -343,7 +343,9 @@ jobject Java_direct_1bt_tinyb_DBTDevice_getServices(JNIEnv *env, jobject obj) { JavaGlobalObj::check(service->device->getJavaObject(), E_FILE_LINE); jobject jdevice = JavaGlobalObj::GetObject(service->device->getJavaObject()); const jboolean isPrimary = service->isPrimary; - const jstring uuid = from_string_to_jstring(env, service->type->toString()); + const jstring uuid = from_string_to_jstring(env, + directBTJNISettings.getUnifyUUID128Bit() ? service->type->toUUID128String() : + service->type->toString()); java_exception_check_and_throw(env, E_FILE_LINE); jobject jservice = env->NewObject(clazz, clazz_ctor, (jlong)service, jdevice, isPrimary, diff --git a/java/jni/direct_bt/DBTGattCharacteristic.cxx b/java/jni/direct_bt/DBTGattCharacteristic.cxx index 2222e435..637b57e9 100644 --- a/java/jni/direct_bt/DBTGattCharacteristic.cxx +++ b/java/jni/direct_bt/DBTGattCharacteristic.cxx @@ -79,7 +79,9 @@ jobject Java_direct_1bt_tinyb_DBTGattCharacteristic_getDescriptorsImpl(JNIEnv *e JavaGlobalObj::check(descriptor->characteristic->getJavaObject(), E_FILE_LINE); jobject jcharacteristic = JavaGlobalObj::GetObject(descriptor->characteristic->getJavaObject()); - const jstring uuid = from_string_to_jstring(env, descriptor->type->toString()); + const jstring uuid = from_string_to_jstring(env, + directBTJNISettings.getUnifyUUID128Bit() ? descriptor->type->toUUID128String() : + descriptor->type->toString()); java_exception_check_and_throw(env, E_FILE_LINE); const size_t value_size = descriptor->value.getSize(); diff --git a/java/jni/direct_bt/DBTGattService.cxx b/java/jni/direct_bt/DBTGattService.cxx index 6b3668e1..4ded7658 100644 --- a/java/jni/direct_bt/DBTGattService.cxx +++ b/java/jni/direct_bt/DBTGattService.cxx @@ -92,7 +92,9 @@ jobject Java_direct_1bt_tinyb_DBTGattService_getCharacteristicsImpl(JNIEnv *env, } java_exception_check_and_throw(env, E_FILE_LINE); - const jstring uuid = from_string_to_jstring(env, characteristic->value_type->toString()); + const jstring uuid = from_string_to_jstring(env, + directBTJNISettings.getUnifyUUID128Bit() ? characteristic->value_type->toUUID128String() : + characteristic->value_type->toString()); java_exception_check_and_throw(env, E_FILE_LINE); jobject jchar = env->NewObject(clazz, clazz_ctor, (jlong)characteristic, jservice, diff --git a/java/jni/direct_bt/DBTManager.cxx b/java/jni/direct_bt/DBTManager.cxx index d9fc55bf..58478f3e 100644 --- a/java/jni/direct_bt/DBTManager.cxx +++ b/java/jni/direct_bt/DBTManager.cxx @@ -38,8 +38,9 @@ using namespace direct_bt; -void Java_direct_1bt_tinyb_DBTManager_initImpl(JNIEnv *env, jobject obj) +void Java_direct_1bt_tinyb_DBTManager_initImpl(JNIEnv *env, jobject obj, jboolean unifyUUID128Bit) { + directBTJNISettings.setUnifyUUID128Bit(unifyUUID128Bit); try { DBTManager *manager = &DBTManager::get(BTMode::BT_MODE_LE); // special: static singleton setInstance<DBTManager>(env, obj, manager); diff --git a/java/jni/direct_bt/helper_dbt.cxx b/java/jni/direct_bt/helper_dbt.cxx index b0158809..b1690edf 100644 --- a/java/jni/direct_bt/helper_dbt.cxx +++ b/java/jni/direct_bt/helper_dbt.cxx @@ -32,6 +32,8 @@ 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()); diff --git a/java/jni/direct_bt/helper_dbt.hpp b/java/jni/direct_bt/helper_dbt.hpp index 7cd33f5c..b2f002d7 100644 --- a/java/jni/direct_bt/helper_dbt.hpp +++ b/java/jni/direct_bt/helper_dbt.hpp @@ -35,6 +35,23 @@ namespace direct_bt { + class DirectBTJNISettings { + private: + bool unifyUUID128Bit = true; + + public: + /** + * Enables or disables uuid128_t consolidation + * for native uuid16_t and uuid32_t values before string conversion. + * <p> + * Default is {@code true}, as this represent compatibility with original TinyB D-Bus behavior. + * </p> + */ + bool getUnifyUUID128Bit() { return unifyUUID128Bit; } + void setUnifyUUID128Bit(bool v) { unifyUUID128Bit = v; } + }; + extern DirectBTJNISettings directBTJNISettings; + class JavaGlobalObj : public JavaAnonObj { private: JNIGlobalRef javaObjectRef; diff --git a/src/direct_bt/UUID.cpp b/src/direct_bt/UUID.cpp index 83cec826..7e9b912f 100644 --- a/src/direct_bt/UUID.cpp +++ b/src/direct_bt/UUID.cpp @@ -81,7 +81,7 @@ std::string uuid16_t::toString() const { str.reserve(length+1); // including EOS for snprintf str.resize(length); - const int count = snprintf(&str[0], str.capacity(), "%.4X", value); + const int count = snprintf(&str[0], str.capacity(), "%.4x", value); if( length != count ) { throw InternalError("UUID16 string not of length "+std::to_string(length)+" but "+std::to_string(count), E_FILE_LINE); } @@ -100,7 +100,7 @@ std::string uuid32_t::toString() const { str.reserve(length+1); // including EOS for snprintf str.resize(length); - const int count = snprintf(&str[0], str.capacity(), "%.8X", value); + const int count = snprintf(&str[0], str.capacity(), "%.8x", value); if( length != count ) { throw InternalError("UUID32 string not of length "+std::to_string(length)+" but "+std::to_string(count), E_FILE_LINE); } @@ -148,7 +148,7 @@ std::string uuid128_t::toString() const { #else #error "Unexpected __BYTE_ORDER" #endif - const int count = snprintf(&str[0], str.capacity(), "%.8X-%.4X-%.4X-%.4X-%.8X%.4X", + const int count = snprintf(&str[0], str.capacity(), "%.8x-%.4x-%.4x-%.4x-%.8x%.4x", part0, part1, part2, part3, part4, part5); if( length != count ) { throw InternalError("UUID128 string not of length "+std::to_string(length)+" but "+std::to_string(count), E_FILE_LINE); |