Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added phy_update method to Connection #203

Merged
merged 2 commits into from
Nov 8, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 38 additions & 0 deletions nrf-softdevice/src/ble/connection.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ use core::iter::FusedIterator;

use raw::ble_gap_conn_params_t;

use super::PhySet;
#[cfg(feature = "ble-sec")]
use crate::ble::security::SecurityHandler;
use crate::ble::types::{Address, AddressType, Role, SecurityMode};
Expand Down Expand Up @@ -60,6 +61,23 @@ impl From<RawError> for IgnoreSlaveLatencyError {
}
}

pub enum PhyUpdateError {
Disconnected,
Raw(RawError),
}

impl From<DisconnectedError> for PhyUpdateError {
fn from(_err: DisconnectedError) -> Self {
Self::Disconnected
}
}

impl From<RawError> for PhyUpdateError {
fn from(err: RawError) -> Self {
Self::Raw(err)
}
}

// Highest ever the softdevice can support.
pub(crate) const CONNS_MAX: usize = 20;

Expand Down Expand Up @@ -474,6 +492,26 @@ impl Connection {
pub fn iter() -> ConnectionIter {
ConnectionIter(0)
}

/// Send a request to the connected device to change the PHY.
///
/// Note that this just initiates the PHY change, it does not wait for completion.
/// Immediately after return, the active PHYs will still be the old ones, and after some time
/// they should change to the new ones.
pub fn phy_update(&mut self, tx_phys: PhySet, rx_phys: PhySet) -> Result<(), PhyUpdateError> {
let conn_handle = self.with_state(|state| state.check_connected())?;
let p_gap_phys = raw::ble_gap_phys_t {
tx_phys: tx_phys as u8,
rx_phys: rx_phys as u8,
};
let ret = unsafe { raw::sd_ble_gap_phy_update(conn_handle, &p_gap_phys) };
if let Err(err) = RawError::convert(ret) {
warn!("sd_ble_gap_phy_update err {:?}", err);
return Err(err.into());
}

Ok(())
}
}

pub struct ConnectionIter(u8);
Expand Down
Loading