diff options
author | Sven Gothel <[email protected]> | 2022-01-25 18:25:23 +0100 |
---|---|---|
committer | Sven Gothel <[email protected]> | 2022-01-25 18:25:23 +0100 |
commit | fed5a977d7d9f2a26b5ef462d13146de1bfaffec (patch) | |
tree | 14f260a7094f1cf768a505ea81bd21cedf187180 | |
parent | 4ceee4cb4d2d34e7d24db33f12c05bb617f288ca (diff) |
Expose BTManager::setDefaultConnParam(..) via BTAdapter::setDefaultConnParam(..), essential in server mode
-rw-r--r-- | api/direct_bt/BTAdapter.hpp | 27 | ||||
-rw-r--r-- | api/direct_bt/BTManager.hpp | 13 | ||||
-rw-r--r-- | examples/dbt_peripheral00.cpp | 15 | ||||
-rw-r--r-- | examples/java/DBTPeripheral00.java | 15 | ||||
-rw-r--r-- | java/jau/direct_bt/DBTAdapter.java | 9 | ||||
-rw-r--r-- | java/jni/direct_bt/DBTAdapter.cxx | 16 | ||||
-rw-r--r-- | java/org/direct_bt/BTAdapter.java | 27 | ||||
-rw-r--r-- | src/direct_bt/BTAdapter.cpp | 8 | ||||
-rw-r--r-- | src/direct_bt/BTManager.cpp | 12 |
9 files changed, 125 insertions, 17 deletions
diff --git a/api/direct_bt/BTAdapter.hpp b/api/direct_bt/BTAdapter.hpp index cfe1794a..de6a2f86 100644 --- a/api/direct_bt/BTAdapter.hpp +++ b/api/direct_bt/BTAdapter.hpp @@ -757,6 +757,33 @@ namespace direct_bt { HCIStatusCode setSecureConnections(const bool enable) noexcept; /** + * Set default connection parameter of incoming connections for this adapter when in server mode, i.e. BTRole::Slave. + * + * In case the incoming connection's parameter don't lie within the given default values, + * a reconnect is being requested. + * + * Shall be called while adapter is powered off, see setPowered(). + * If adapter is powered, method returns HCIStatusCode::COMMAND_DISALLOWED. + * + * BlueZ/Linux LE connection defaults are + * - conn_interval_min 24 -> 30ms + * - conn_interval_max 40 -> 50ms + * - conn_latency 0 + * - supervision_timeout 42 -> 420ms + * + * @param dev_id + * @param conn_interval_min in units of 1.25ms, default value 8 for 10ms; Value range [6 .. 3200] for [7.5ms .. 4000ms]. BlueZ/Linux 24 -> + * @param conn_interval_max in units of 1.25ms, default value 40 for 50ms; Value range [6 .. 3200] for [7.5ms .. 4000ms] + * @param conn_latency slave latency in units of connection events, default value 0; Value range [0 .. 0x01F3]. + * @param supervision_timeout in units of 10ms, default value 500ms >= 10 x conn_interval_max, we use HCIConstInt::LE_CONN_MIN_TIMEOUT_MS minimum; Value range [0xA-0x0C80] for [100ms - 32s]. + * @return HCIStatusCode::SUCCESS or an error state on failure + * @see setPowered() + * @since 2.5.3 + */ + HCIStatusCode setDefaultConnParam(const uint16_t conn_interval_min=8, const uint16_t conn_interval_max=40, + const uint16_t conn_latency=0, const uint16_t supervision_timeout=getHCIConnSupervisorTimeout(0, 50)) noexcept; + + /** * Sets the given ::BTSecurityLevel and ::SMPIOCapability for connecting device when in server (peripheral) mode, see [adapter's role](@ref BTAdapterRoles). * <p> * Method either changes both parameter for the upcoming connection or none at all. diff --git a/api/direct_bt/BTManager.hpp b/api/direct_bt/BTManager.hpp index 559ba0c7..f525d36a 100644 --- a/api/direct_bt/BTManager.hpp +++ b/api/direct_bt/BTManager.hpp @@ -403,18 +403,19 @@ namespace direct_bt { const BTRole btRole, const BTMode btMode) noexcept; /** - * Set default connection parameter for given device to the kernel. + * Set default connection parameter for given adapter to the kernel. * * @param dev_id - * @param conn_interval_min in units of 1.25ms, default value 12 for 15ms; Value range [6 .. 3200] for [7.5ms .. 4000ms] - * @param conn_interval_max in units of 1.25ms, default value 12 for 15ms; Value range [6 .. 3200] for [7.5ms .. 4000ms] + * @param conn_interval_min in units of 1.25ms, default value 8 for 10ms; Value range [6 .. 3200] for [7.5ms .. 4000ms]. BlueZ/Linux 24 -> + * @param conn_interval_max in units of 1.25ms, default value 40 for 50ms; Value range [6 .. 3200] for [7.5ms .. 4000ms] * @param conn_latency slave latency in units of connection events, default value 0; Value range [0 .. 0x01F3]. - * @param supervision_timeout in units of 10ms, default value >= 10 x conn_interval_max, we use HCIConstInt::LE_CONN_MIN_TIMEOUT_MS minimum; Value range [0xA-0x0C80] for [100ms - 32s]. + * @param supervision_timeout in units of 10ms, default value 500ms >= 10 x conn_interval_max, we use HCIConstInt::LE_CONN_MIN_TIMEOUT_MS minimum; Value range [0xA-0x0C80] for [100ms - 32s]. * @return + * @since 2.5.3 */ bool setDefaultConnParam(const uint16_t dev_id, - const uint16_t conn_interval_min=12, const uint16_t conn_interval_max=12, - const uint16_t conn_latency=0, const uint16_t supervision_timeout=getHCIConnSupervisorTimeout(0, 15)) noexcept; + const uint16_t conn_interval_min=8, const uint16_t conn_interval_max=40, + const uint16_t conn_latency=0, const uint16_t supervision_timeout=getHCIConnSupervisorTimeout(0, 50)) noexcept; /** * Uploads given connection parameter for given device to the kernel. diff --git a/examples/dbt_peripheral00.cpp b/examples/dbt_peripheral00.cpp index a6a13373..383a6e74 100644 --- a/examples/dbt_peripheral00.cpp +++ b/examples/dbt_peripheral00.cpp @@ -589,6 +589,7 @@ static bool initAdapter(std::shared_ptr<BTAdapter>& adapter) { fprintf_td(stderr, "initAdapter: setLocalName OK: %s\n", adapter->toString().c_str()); } else { fprintf_td(stderr, "initAdapter: setLocalName failed: %s\n", adapter->toString().c_str()); + return false; } status = adapter->setSecureConnections( use_SC ); @@ -596,7 +597,21 @@ static bool initAdapter(std::shared_ptr<BTAdapter>& adapter) { fprintf_td(stderr, "initAdapter: setSecureConnections OK: %s\n", adapter->toString().c_str()); } else { fprintf_td(stderr, "initAdapter: setSecureConnections failed: %s\n", adapter->toString().c_str()); + return false; + } + + const uint16_t conn_min_interval = 8; // 10ms + const uint16_t conn_max_interval = 40; // 50ms + const uint16_t conn_latency = 0; + const uint16_t supervision_timeout = 50; // 500ms + status = adapter->setDefaultConnParam(conn_min_interval, conn_max_interval, conn_latency, supervision_timeout); + if( HCIStatusCode::SUCCESS == status ) { + fprintf_td(stderr, "initAdapter: setDefaultConnParam OK: %s\n", adapter->toString().c_str()); + } else { + fprintf_td(stderr, "initAdapter: setDefaultConnParam failed: %s\n", adapter->toString().c_str()); + return false; } + if( !adapter->setPowered( true ) ) { fprintf_td(stderr, "initAdapter: setPower.2 on failed: %s\n", adapter->toString().c_str()); return false; diff --git a/examples/java/DBTPeripheral00.java b/examples/java/DBTPeripheral00.java index 97cb8f12..6cde923a 100644 --- a/examples/java/DBTPeripheral00.java +++ b/examples/java/DBTPeripheral00.java @@ -637,6 +637,7 @@ public class DBTPeripheral00 { BTUtils.fprintf_td(System.err, "initAdapter: setLocalName OK: %s\n", adapter.toString()); } else { BTUtils.fprintf_td(System.err, "initAdapter: setLocalName failed: %s\n", adapter.toString()); + return false; } status = adapter.setSecureConnections( use_SC ); @@ -644,7 +645,21 @@ public class DBTPeripheral00 { BTUtils.fprintf_td(System.err, "initAdapter: setSecureConnections OK: %s\n", adapter.toString()); } else { BTUtils.fprintf_td(System.err, "initAdapter: setSecureConnections failed: %s\n", adapter.toString()); + return false; + } + + final short conn_min_interval = 8; // 10ms + final short conn_max_interval = 40; // 50ms + final short conn_latency = 0; + final short supervision_timeout = 50; // 500ms + status = adapter.setDefaultConnParam(conn_min_interval, conn_max_interval, conn_latency, supervision_timeout); + if( HCIStatusCode.SUCCESS == status ) { + BTUtils.fprintf_td(System.err, "initAdapter: setDefaultConnParam OK: %s\n", adapter.toString()); + } else { + BTUtils.fprintf_td(System.err, "initAdapter: setDefaultConnParam failed: %s\n", adapter.toString()); + return false; } + if( !adapter.setPowered( true ) ) { BTUtils.fprintf_td(System.err, "initAdapter: setPower.2 on failed: %s\n", adapter.toString()); return false; diff --git a/java/jau/direct_bt/DBTAdapter.java b/java/jau/direct_bt/DBTAdapter.java index e7967fda..fc68ecf3 100644 --- a/java/jau/direct_bt/DBTAdapter.java +++ b/java/jau/direct_bt/DBTAdapter.java @@ -289,6 +289,15 @@ public class DBTAdapter extends DBTObject implements BTAdapter private native byte setSecureConnectionsImpl(final boolean enable); @Override + public final HCIStatusCode setDefaultConnParam(final short conn_interval_min, final short conn_interval_max, + final short conn_latency, final short supervision_timeout) { + return HCIStatusCode.get( setDefaultConnParamImpl(conn_interval_min, conn_interval_max, + conn_latency, supervision_timeout) ); + } + private native byte setDefaultConnParamImpl(final short conn_interval_min, final short conn_interval_max, + final short conn_latency, final short supervision_timeout); + + @Override public final void setServerConnSecurity(final BTSecurityLevel sec_level, final SMPIOCapability io_cap) { setServerConnSecurityImpl(sec_level.value, io_cap.value); } diff --git a/java/jni/direct_bt/DBTAdapter.cxx b/java/jni/direct_bt/DBTAdapter.cxx index 6d26e1ec..f932bef3 100644 --- a/java/jni/direct_bt/DBTAdapter.cxx +++ b/java/jni/direct_bt/DBTAdapter.cxx @@ -845,6 +845,22 @@ jbyte Java_jau_direct_1bt_DBTAdapter_setSecureConnectionsImpl(JNIEnv *env, jobje } return (jbyte) number(HCIStatusCode::INTERNAL_FAILURE); } + +jbyte Java_jau_direct_1bt_DBTAdapter_setDefaultConnParamImpl(JNIEnv *env, jobject obj, + jshort conn_interval_min, jshort conn_interval_max, + jshort conn_latency, jshort supervision_timeout) { + try { + BTAdapter *adapter = jau::getJavaUplinkObject<BTAdapter>(env, obj); + jau::JavaGlobalObj::check(adapter->getJavaObject(), E_FILE_LINE); + HCIStatusCode res = adapter->setDefaultConnParam(static_cast<uint16_t>(conn_interval_min), + static_cast<uint16_t>(conn_interval_max), + static_cast<uint16_t>(conn_latency), + static_cast<uint16_t>(supervision_timeout)); + return (jbyte) number(res); + } catch(...) { + rethrow_and_raise_java_exception(env); + } + return (jbyte) number(HCIStatusCode::INTERNAL_FAILURE); } void Java_jau_direct_1bt_DBTAdapter_setServerConnSecurityImpl(JNIEnv *env, jobject obj, jbyte jsec_level, jbyte jio_cap) { diff --git a/java/org/direct_bt/BTAdapter.java b/java/org/direct_bt/BTAdapter.java index fcbd8165..af8fd55e 100644 --- a/java/org/direct_bt/BTAdapter.java +++ b/java/org/direct_bt/BTAdapter.java @@ -585,6 +585,33 @@ public interface BTAdapter extends BTObject */ HCIStatusCode setSecureConnections(final boolean enable); + /** + * Set default connection parameter of incoming connections for this adapter when in server mode, i.e. BTRole::Slave. + * + * In case the incoming connection's parameter don't lie within the given default values, + * a reconnect is being requested. + * + * Shall be called while adapter is powered off, see setPowered(). + * If adapter is powered, method returns HCIStatusCode::COMMAND_DISALLOWED. + * + * BlueZ/Linux LE connection defaults are + * - conn_interval_min 24 -> 30ms + * - conn_interval_max 40 -> 50ms + * - conn_latency 0 + * - supervision_timeout 42 -> 420ms + * + * @param dev_id + * @param conn_interval_min in units of 1.25ms, default value 8 for 10ms; Value range [6 .. 3200] for [7.5ms .. 4000ms]. BlueZ/Linux 24 -> + * @param conn_interval_max in units of 1.25ms, default value 40 for 50ms; Value range [6 .. 3200] for [7.5ms .. 4000ms] + * @param conn_latency slave latency in units of connection events, default value 0; Value range [0 .. 0x01F3]. + * @param supervision_timeout in units of 10ms, default value 500ms >= 10 x conn_interval_max, we use HCIConstInt::LE_CONN_MIN_TIMEOUT_MS minimum; Value range [0xA-0x0C80] for [100ms - 32s]. + * @return HCIStatusCode::SUCCESS or an error state on failure + * @see setPowered() + * @since 2.5.3 + */ + HCIStatusCode setDefaultConnParam(final short conn_interval_min, final short conn_interval_max, + final short conn_latency, final short supervision_timeout); + /** * Sets the given ::BTSecurityLevel and ::SMPIOCapability for connecting device when in server (peripheral) mode, see [adapter's role](@ref BTAdapterRoles). diff --git a/src/direct_bt/BTAdapter.cpp b/src/direct_bt/BTAdapter.cpp index 8a632433..1f8d873a 100644 --- a/src/direct_bt/BTAdapter.cpp +++ b/src/direct_bt/BTAdapter.cpp @@ -601,6 +601,14 @@ HCIStatusCode BTAdapter::setSecureConnections(const bool enable) noexcept { updateAdapterSettings(false /* off_thread */, new_settings, false /* sendEvent */, 0); return ( enable == isAdapterSettingBitSet(new_settings, AdapterSetting::SECURE_CONN) ) ? HCIStatusCode::SUCCESS : HCIStatusCode::FAILED; } + +HCIStatusCode BTAdapter::setDefaultConnParam(const uint16_t conn_interval_min, const uint16_t conn_interval_max, + const uint16_t conn_latency, const uint16_t supervision_timeout) noexcept { + if( isAdapterSettingBitSet(adapterInfo.getCurrentSettingMask(), AdapterSetting::POWERED) ) { + return HCIStatusCode::COMMAND_DISALLOWED; + } + const bool res = mgmt.setDefaultConnParam(dev_id, conn_interval_min, conn_interval_max, conn_latency, supervision_timeout); + return res ? HCIStatusCode::SUCCESS : HCIStatusCode::FAILED; } void BTAdapter::setServerConnSecurity(const BTSecurityLevel sec_level, const SMPIOCapability io_cap) noexcept { diff --git a/src/direct_bt/BTManager.cpp b/src/direct_bt/BTManager.cpp index d27db7b7..a0253d73 100644 --- a/src/direct_bt/BTManager.cpp +++ b/src/direct_bt/BTManager.cpp @@ -308,17 +308,7 @@ HCIStatusCode BTManager::initializeAdapter(AdapterInfo& adapterInfo, const uint1 removeDeviceFromWhitelist(dev_id, BDAddressAndType::ANY_BREDR_DEVICE); // flush whitelist! - { - // FIXME: Configurable ??? - const uint16_t conn_min_interval = 8; // 10ms - const uint16_t conn_max_interval = 40; // 50ms - const uint16_t conn_latency = 0; - const uint16_t supervision_timeout = 300; // 3s - - setDefaultConnParam(dev_id, - conn_min_interval, conn_max_interval, - conn_latency, supervision_timeout); - } + setDefaultConnParam(dev_id); // using our defaults, exceeding BlueZ/Linux on the lower-end a bit setMode(dev_id, MgmtCommand::Opcode::SET_POWERED, 1, current_settings); |