diff options
author | alexmoon <alex.r.moon@gmail.com> | 2022-07-13 12:01:14 -0400 |
---|---|---|
committer | GitHub <noreply@github.com> | 2022-07-13 12:01:14 -0400 |
commit | d54aceb4fce464f23dcedbff94f659b558124b9c (patch) | |
tree | 23479874cbb201b801be6e772181e5700983af43 | |
parent | 6a9b6a5da3e2a96e93d55d6a5dd85a217cd4682c (diff) | |
parent | 4f7dab6fe82f92760ae81c804807e50e91df9028 (diff) | |
download | nrf-softdevice-d54aceb4fce464f23dcedbff94f659b558124b9c.zip |
Merge pull request #116 from embassy-rs/ignore_slave_latency
Add Conn::ignore_slave_latency.
-rw-r--r-- | .vscode/settings.json | 6 | ||||
-rw-r--r-- | nrf-softdevice/src/ble/connection.rs | 67 |
2 files changed, 68 insertions, 5 deletions
diff --git a/.vscode/settings.json b/.vscode/settings.json index 56c37a2..94e2ea4 100644 --- a/.vscode/settings.json +++ b/.vscode/settings.json @@ -4,10 +4,7 @@ "rust-analyzer.checkOnSave.allFeatures": false, "rust-analyzer.cargo.target": "thumbv7em-none-eabihf", "rust-analyzer.checkOnSave.allTargets": false, - "rust-analyzer.cargo.runBuildScripts": true, - "rust-analyzer.experimental.procAttrMacros": false, "rust-analyzer.procMacro.enable": true, - "rust-analyzer.assist.importGranularity": "module", "rust-analyzer.linkedProjects": [ "examples/Cargo.toml" ], @@ -16,4 +13,7 @@ "**/.git/subtree-cache/**": true, "**/target/**": true, }, + "rust-analyzer.imports.granularity.group": "module", + "rust-analyzer.cargo.buildScripts.enable": true, + "rust-analyzer.procMacro.attributes.enable": false, }
\ No newline at end of file diff --git a/nrf-softdevice/src/ble/connection.rs b/nrf-softdevice/src/ble/connection.rs index 79e0f8f..feb1c3c 100644 --- a/nrf-softdevice/src/ble/connection.rs +++ b/nrf-softdevice/src/ble/connection.rs @@ -28,13 +28,35 @@ pub enum SetConnParamsError { impl From<DisconnectedError> for SetConnParamsError { fn from(_err: DisconnectedError) -> Self { - SetConnParamsError::Disconnected + Self::Disconnected } } impl From<RawError> for SetConnParamsError { fn from(err: RawError) -> Self { - SetConnParamsError::Raw(err) + Self::Raw(err) + } +} + +#[derive(Debug, PartialEq, Eq, Clone, Copy)] +#[cfg_attr(feature = "defmt", derive(defmt::Format))] +#[cfg(feature = "ble-peripheral")] +pub enum IgnoreSlaveLatencyError { + Disconnected, + Raw(RawError), +} + +#[cfg(feature = "ble-peripheral")] +impl From<DisconnectedError> for IgnoreSlaveLatencyError { + fn from(_err: DisconnectedError) -> Self { + Self::Disconnected + } +} + +#[cfg(feature = "ble-peripheral")] +impl From<RawError> for IgnoreSlaveLatencyError { + fn from(err: RawError) -> Self { + Self::Raw(err) } } @@ -298,6 +320,47 @@ impl Connection { Ok(()) } + /// Temporarily ignore slave latency for peripehral connections. + /// + /// "Slave latency" is a setting in the conn params that allows the peripheral + /// to intentionally sleep through and miss up to N connection events if it doesn't + /// have any data to send to the central. + /// + /// Slave latency is useful because it can yield the same power savings on the peripheral + /// as increasing the conn interval, but it only impacts latency in the central->peripheral + /// direction, not both. + /// + /// However, in some cases, if the peripheral knows the central will send it some data soon + /// it might be useful to temporarily force ignoring the slave latency setting, ie waking up + /// at every single conn interval, to lower the latency. + /// + /// This only works on peripheral connections. + #[cfg(feature = "ble-peripheral")] + pub fn ignore_slave_latency(&mut self, ignore: bool) -> Result<(), IgnoreSlaveLatencyError> { + let conn_handle = self.with_state(|state| state.check_connected())?; + + let mut disable: raw::ble_gap_opt_slave_latency_disable_t = unsafe { core::mem::zeroed() }; + disable.conn_handle = conn_handle; + disable.set_disable(ignore as u8); // 0 or 1 + + let ret = unsafe { + raw::sd_ble_opt_set( + raw::BLE_GAP_OPTS_BLE_GAP_OPT_SLAVE_LATENCY_DISABLE, + &raw::ble_opt_t { + gap_opt: raw::ble_gap_opt_t { + slave_latency_disable: disable, + }, + }, + ) + }; + if let Err(err) = RawError::convert(ret) { + warn!("ignore_slave_latency sd_ble_opt_set err {:?}", err); + return Err(err.into()); + } + + Ok(()) + } + pub(crate) fn with_state<T>(&self, f: impl FnOnce(&mut ConnectionState) -> T) -> T { with_state(self.index, f) } |