diff options
author | Sven Gothel <[email protected]> | 2020-05-25 03:51:37 +0200 |
---|---|---|
committer | Sven Gothel <[email protected]> | 2020-05-25 03:51:37 +0200 |
commit | 98fd49f1a5989dd2da466e0058daa626e6f51d58 (patch) | |
tree | d72c16fdf01ffb37ace6cc7b0bf2d4544861ec2f | |
parent | 771ce6d37c05fec2fa7b573645ad745f34ad855b (diff) |
Expose Whitelist (auto-)connect management to BluetoothAdapter.java, impl in DBTAdapter.java only at this point.
Instead of using the "string definition" of addressType,
we introduce a universal BluetoothAddressType (java) which corresponds with BDAddressType (C++).
BluetoothAddressType (java) supports a string conversion, which also accepts the D-Bus names
"", "public" and "random" - as also used in TinyB.
++++
Further adds HCIWhitelistConnectType (java) representing the same C++ type.
-rw-r--r-- | java/direct_bt/tinyb/DBTAdapter.java | 30 | ||||
-rw-r--r-- | java/jni/direct_bt/DBTAdapter.cxx | 60 | ||||
-rw-r--r-- | java/org/tinyb/BluetoothAdapter.java | 66 | ||||
-rw-r--r-- | java/org/tinyb/BluetoothAddressType.java | 104 | ||||
-rw-r--r-- | java/org/tinyb/HCIWhitelistConnectType.java | 46 | ||||
-rw-r--r-- | java/tinyb/dbus/DBusAdapter.java | 26 |
6 files changed, 331 insertions, 1 deletions
diff --git a/java/direct_bt/tinyb/DBTAdapter.java b/java/direct_bt/tinyb/DBTAdapter.java index 8f8ca0c6..e719752a 100644 --- a/java/direct_bt/tinyb/DBTAdapter.java +++ b/java/direct_bt/tinyb/DBTAdapter.java @@ -34,12 +34,14 @@ import java.util.concurrent.atomic.AtomicInteger; import org.tinyb.AdapterSettings; import org.tinyb.BluetoothAdapter; +import org.tinyb.BluetoothAddressType; import org.tinyb.BluetoothDevice; import org.tinyb.BluetoothException; import org.tinyb.BluetoothManager; import org.tinyb.BluetoothNotification; import org.tinyb.BluetoothType; import org.tinyb.EIRDataTypeSet; +import org.tinyb.HCIWhitelistConnectType; import org.tinyb.AdapterStatusListener; import org.tinyb.TransportType; @@ -133,6 +135,34 @@ public class DBTAdapter extends DBTObject implements BluetoothAdapter return find(name, address, 0); } + @Override + public native boolean isDeviceWhitelisted(final String address); + + @Override + public boolean addDeviceToWhitelist(final String address, final BluetoothAddressType address_type, + final HCIWhitelistConnectType ctype, + final short min_interval, final short max_interval, + final short latency, final short timeout) { + return addDeviceToWhitelist(address, address_type.value, ctype.value, + min_interval, max_interval, latency, timeout); + } + private native boolean addDeviceToWhitelist(final String address, final int address_type, final int ctype, + final short min_interval, final short max_interval, + final short latency, final short timeout); + + @Override + public boolean addDeviceToWhitelist(final String address, final BluetoothAddressType address_type, + final HCIWhitelistConnectType ctype) { + return addDeviceToWhitelist(address, address_type.value, ctype.value); + } + private native boolean addDeviceToWhitelist(final String address, final int address_type, final int ctype); + + @Override + public boolean removeDeviceFromWhitelist(final String address, final BluetoothAddressType address_type) { + return removeDeviceFromWhitelist(address, address_type.value); + } + private native boolean removeDeviceFromWhitelist(final String address, final int address_type); + /* Unsupported */ @Override diff --git a/java/jni/direct_bt/DBTAdapter.cxx b/java/jni/direct_bt/DBTAdapter.cxx index 390c4839..cdca300d 100644 --- a/java/jni/direct_bt/DBTAdapter.cxx +++ b/java/jni/direct_bt/DBTAdapter.cxx @@ -388,6 +388,66 @@ jint Java_direct_1bt_tinyb_DBTAdapter_removeAllStatusListener(JNIEnv *env, jobje return 0; } +jboolean Java_direct_1bt_tinyb_DBTAdapter_isDeviceWhitelisted(JNIEnv *env, jobject obj, jstring jaddress) { + try { + DBTAdapter *adapter = getInstance<DBTAdapter>(env, obj); + JavaGlobalObj::check(adapter->getJavaObject(), E_FILE_LINE); + + std::string saddress = from_jstring_to_string(env, jaddress); + EUI48 address(saddress); + return adapter->isDeviceWhitelisted(address); + } catch(...) { + rethrow_and_raise_java_exception(env); + } + return JNI_FALSE; +} +jboolean Java_direct_1bt_tinyb_DBTAdapter_addDeviceToWhitelist(JNIEnv *env, jobject obj, jstring jaddress, int jaddressType, int jctype, + jshort min_interval, jshort max_interval, + jshort latency, jshort timeout) { + try { + DBTAdapter *adapter = getInstance<DBTAdapter>(env, obj); + JavaGlobalObj::check(adapter->getJavaObject(), E_FILE_LINE); + + std::string saddress = from_jstring_to_string(env, jaddress); + EUI48 address(saddress); + const BDAddressType addressType = static_cast<BDAddressType>( jaddressType ); + const HCIWhitelistConnectType ctype = static_cast<HCIWhitelistConnectType>( jctype ); + return adapter->addDeviceToWhitelist(address, addressType, ctype, min_interval, max_interval, latency, timeout); + } catch(...) { + rethrow_and_raise_java_exception(env); + } + return JNI_FALSE; +} +jboolean Java_direct_1bt_tinyb_DBTAdapter_addDeviceToWhitelist(JNIEnv *env, jobject obj, jstring jaddress, int jaddressType, int jctype) { + try { + DBTAdapter *adapter = getInstance<DBTAdapter>(env, obj); + JavaGlobalObj::check(adapter->getJavaObject(), E_FILE_LINE); + + std::string saddress = from_jstring_to_string(env, jaddress); + EUI48 address(saddress); + const BDAddressType addressType = static_cast<BDAddressType>( jaddressType ); + const HCIWhitelistConnectType ctype = static_cast<HCIWhitelistConnectType>( jctype ); + return adapter->addDeviceToWhitelist(address, addressType, ctype); + } catch(...) { + rethrow_and_raise_java_exception(env); + } + return JNI_FALSE; +} +jboolean Java_direct_1bt_tinyb_DBTAdapter_removeDeviceFromWhitelist(JNIEnv *env, jobject obj, jstring jaddress, int jaddressType) { + try { + DBTAdapter *adapter = getInstance<DBTAdapter>(env, obj); + JavaGlobalObj::check(adapter->getJavaObject(), E_FILE_LINE); + + std::string saddress = from_jstring_to_string(env, jaddress); + EUI48 address(saddress); + const BDAddressType addressType = static_cast<BDAddressType>( jaddressType ); + return adapter->removeDeviceFromWhitelist(address, addressType); + } catch(...) { + rethrow_and_raise_java_exception(env); + } + return JNI_FALSE; +} + jstring Java_direct_1bt_tinyb_DBTAdapter_toStringImpl(JNIEnv *env, jobject obj) { try { DBTAdapter *nativePtr = getInstance<DBTAdapter>(env, obj); diff --git a/java/org/tinyb/BluetoothAdapter.java b/java/org/tinyb/BluetoothAdapter.java index a017b4b9..9276859c 100644 --- a/java/org/tinyb/BluetoothAdapter.java +++ b/java/org/tinyb/BluetoothAdapter.java @@ -67,7 +67,71 @@ public interface BluetoothAdapter extends BluetoothObject */ public BluetoothDevice find(String name, String address); - /* D-Bus specified API */ + /* Bluetooth specific API */ + + /** + * Returns true, if the adapter's device is already whitelisted. + * @since 2.0.0 + */ + public boolean isDeviceWhitelisted(final String address); + + /** + * Add the given device to the adapter's autoconnect whitelist. + * <p> + * The given connection parameter will be uploaded to the kernel for the given device first. + * </p> + * <p> + * Method will reject duplicate devices, in which case it should be removed first. + * </p> + * + * @param address + * @param address_type + * @param ctype + * @param min_interval default value 0x000F + * @param max_interval default value 0x000F + * @param latency default value 0x0000 + * @param timeout default value 0x0C80 + * @return {@code true} if successful, otherwise {@code false}. + * + * @see #addDeviceToWhitelist(String, BluetoothAddressType, HCIWhitelistConnectType) + * @since 2.0.0 + */ + public boolean addDeviceToWhitelist(final String address, final BluetoothAddressType address_type, + final HCIWhitelistConnectType ctype, + final short min_interval, final short max_interval, + final short latency, final short timeout); + + /** + * Add the given device to the adapter's autoconnect whitelist. + * <p> + * This variant of {@link #addDeviceToWhitelist(String, BluetoothAddressType, HCIWhitelistConnectType, short, short, short, short)} + * uses default connection parameter, which will be uploaded to the kernel for the given device first. + * </p> + * <p> + * Method will reject duplicate devices, in which case it should be removed first. + * </p> + * + * @param address + * @param address_type + * @param ctype + * @param min_interval default value 0x000F + * @param max_interval default value 0x000F + * @param latency default value 0x0000 + * @param timeout default value 0x0C80 + * @return {@code true} if successful, otherwise {@code false}. + * + * @since 2.0.0 + */ + public boolean addDeviceToWhitelist(final String address, final BluetoothAddressType address_type, + final HCIWhitelistConnectType ctype); + + + /** + * Remove the given device from the adapter's autoconnect whitelist. + * @since 2.0.0 + */ + public boolean removeDeviceFromWhitelist(final String address, final BluetoothAddressType address_type); + /** Turns on device discovery if it is disabled. * @return TRUE if discovery was successfully enabled diff --git a/java/org/tinyb/BluetoothAddressType.java b/java/org/tinyb/BluetoothAddressType.java new file mode 100644 index 00000000..3556798a --- /dev/null +++ b/java/org/tinyb/BluetoothAddressType.java @@ -0,0 +1,104 @@ +/** + * Author: Sven Gothel <[email protected]> + * Copyright (c) 2020 Gothel Software e.K. + * Copyright (c) 2020 ZAFENA AB + * + * Permission is hereby granted, free of charge, to any person obtaining + * a copy of this software and associated documentation files (the + * "Software"), to deal in the Software without restriction, including + * without limitation the rights to use, copy, modify, merge, publish, + * distribute, sublicense, and/or sell copies of the Software, and to + * permit persons to whom the Software is furnished to do so, subject to + * the following conditions: + * + * The above copyright notice and this permission notice shall be + * included in all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, + * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF + * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND + * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE + * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION + * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION + * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. + */ +package org.tinyb; + +/** + * Bluetooth address type constants. + * See {@link #get(String)} for its string mapping. + * <pre> + * BT Core Spec v5.2: Vol 3, Part C Generic Access Profile (GAP): 15.1.1.1 Public Bluetooth address + * 1) BT public address used as BD_ADDR for BR/EDR physical channel is defined in Vol 2, Part B 1.2 + * - EUI-48 or MAC (6 octets) + * + * 2) BT public address used as BD_ADDR for the LE physical channel is defined in Vol 6, Part B 1.3 + * + * BT Core Spec v5.2: Vol 3, Part C Generic Access Profile (GAP): 15.1.1.2 Random Bluetooth address + * 3) BT random address used as BD_ADDR on the LE physical channel is defined in Vol 3, Part C 10.8 + * + * +++ + * + * For HCI LE Address-Type it is: PUBLIC: 0x00, RANDOM: 0x01 + * + * BT Core Spec v5.2: Vol 4, Part E Host Controller Interface (HCI) Functionality: + * + * > 7.8.5: LE Set Advertising Parameters command + * -- Own_Address_Type: public: 0x00 (default), random: 0x01, resolvable-1: 0x02, resolvable-2: 0x03 + * > 7.8.10: LE Set Scan Parameters command + * -- Own_Address_Type: public: 0x00 (default), random: 0x01, resolvable-1: 0x02, resolvable-2: 0x03 + * + * +++ + * </pre> + * + * @since 2.0.0 + */ +public enum BluetoothAddressType { + BDADDR_BREDR (0x00), + BDADDR_LE_PUBLIC (0x01), + BDADDR_LE_RANDOM (0x02), + BDADDR_UNDEFINED (0xff); + + public final int value; + + private static String _public = "public"; + private static String _random = "random"; + + /** + * Maps the specified name to a constant of BluetoothAddressType. + * <p> + * According to BlueZ's D-Bus protocol, which is also followed by TinyB, + * the following mappings are valid: + * <ul> + * <li>"{@code public}" -> {@link #BDADDR_LE_PUBLIC}</li> + * <li>"{@code random}" -> {@link #BDADDR_LE_RANDOM}</li> + * <li>{@code null or empty} -> {@link #BDADDR_BREDR}</li> + * </ul> + * </p> + * <p> + * If the above mappings are not resolving, + * {@link #valueOf(String)} is being returned. + * This maps the constant names itself to their respective constant. + * </p> + * @param name the string name to be mapped to a constant of this enum type. + * @return the corresponding constant of this enum type. + * @throws IllegalArgumentException if the specified name can't be mapped to a constant of this enum type + * as described above. + */ + public static BluetoothAddressType get(final String name) { + if( null == name || name.length() == 0 ) { + return BDADDR_BREDR; + } + if( _public.equals(name) ) { + return BDADDR_LE_PUBLIC; + } + if( _random.equals(name) ) { + return BDADDR_LE_RANDOM; + } + return valueOf(name); + } + + BluetoothAddressType(final int v) { + value = v; + } +} diff --git a/java/org/tinyb/HCIWhitelistConnectType.java b/java/org/tinyb/HCIWhitelistConnectType.java new file mode 100644 index 00000000..efe884c6 --- /dev/null +++ b/java/org/tinyb/HCIWhitelistConnectType.java @@ -0,0 +1,46 @@ +/** + * Author: Sven Gothel <[email protected]> + * Copyright (c) 2020 Gothel Software e.K. + * Copyright (c) 2020 ZAFENA AB + * + * Permission is hereby granted, free of charge, to any person obtaining + * a copy of this software and associated documentation files (the + * "Software"), to deal in the Software without restriction, including + * without limitation the rights to use, copy, modify, merge, publish, + * distribute, sublicense, and/or sell copies of the Software, and to + * permit persons to whom the Software is furnished to do so, subject to + * the following conditions: + * + * The above copyright notice and this permission notice shall be + * included in all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, + * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF + * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND + * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE + * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION + * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION + * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. + */ +package org.tinyb; + +/** + * HCI Whitelist connection type. + * + * @since 2.0.0 + */ +public enum HCIWhitelistConnectType { + /** Report Connection: Only supported for LE on Linux .. */ + HCI_AUTO_CONN_REPORT (0x00), + /** Incoming Connections: Only supported type for BDADDR_BREDR (!LE) on Linux .. */ + HCI_AUTO_CONN_DIRECT (0x01), + /** Auto Connect: Only supported for LE on Linux .. */ + HCI_AUTO_CONN_ALWAYS (0x02); + + public final int value; + + HCIWhitelistConnectType(final int v) { + value = v; + } + +}
\ No newline at end of file diff --git a/java/tinyb/dbus/DBusAdapter.java b/java/tinyb/dbus/DBusAdapter.java index 8666e8d3..b8866534 100644 --- a/java/tinyb/dbus/DBusAdapter.java +++ b/java/tinyb/dbus/DBusAdapter.java @@ -34,12 +34,14 @@ import java.util.List; import java.util.UUID; import org.tinyb.BluetoothAdapter; +import org.tinyb.BluetoothAddressType; import org.tinyb.BluetoothDevice; import org.tinyb.AdapterStatusListener; import org.tinyb.BluetoothException; import org.tinyb.BluetoothManager; import org.tinyb.BluetoothNotification; import org.tinyb.BluetoothType; +import org.tinyb.HCIWhitelistConnectType; import org.tinyb.TransportType; public class DBusAdapter extends DBusObject implements BluetoothAdapter @@ -63,6 +65,30 @@ public class DBusAdapter extends DBusObject implements BluetoothAdapter return find(name, address, 0); } + @Override + public boolean isDeviceWhitelisted(final String address) { + return false; // FIXME + } + + @Override + public boolean addDeviceToWhitelist(final String address, final BluetoothAddressType address_type, + final HCIWhitelistConnectType ctype, + final short min_interval, final short max_interval, + final short latency, final short timeout) { + return false; // FIXME + } + + @Override + public boolean addDeviceToWhitelist(final String address, final BluetoothAddressType address_type, + final HCIWhitelistConnectType ctype) { + return false; // FIXME + } + + @Override + public boolean removeDeviceFromWhitelist(final String address, final BluetoothAddressType address_type) { + return false; // FIXME + } + /* D-Bus method calls: */ @Override |