Skip to content

Commit

Permalink
Added phy_update method to Connection.
Browse files Browse the repository at this point in the history
  • Loading branch information
kext committed Nov 8, 2023
1 parent fb4a084 commit 8d08ea5
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 12 deletions.
12 changes: 0 additions & 12 deletions nrf-softdevice/src/ble/central.rs
Original file line number Diff line number Diff line change
Expand Up @@ -68,18 +68,6 @@ pub async fn connect(_sd: &Softdevice, config: &ConnectConfig<'_>) -> Result<Con

match Connection::new(conn_handle, role, peer_address, conn_params) {
Ok(conn) => {
let phys = config.scan_config.phys;
if phys as u8 & PhySet::M2 as u8 != 0 {
let p_gap_phys = raw::ble_gap_phys_t {
tx_phys: phys as u8,
rx_phys: phys as u8,
};
let ret = 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);
}
}

#[cfg(any(feature = "s113", feature = "s132", feature = "s140"))]
crate::ble::gap::do_data_length_update(conn_handle, ptr::null());

Expand Down
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

0 comments on commit 8d08ea5

Please sign in to comment.