commit 8a78bc8d0ea877223a2729000ff1cc8340e9a272 from: Thomas Böhler date: Wed Dec 7 16:41:28 2022 UTC Extract setting PHY and PKT_DATA_LEN to functions commit - 74f9786fa7029f1b31cac153273cb3cb43c27f53 commit + 8a78bc8d0ea877223a2729000ff1cc8340e9a272 blob - 331b6953336add9937314b99cecd96d917aa8753 blob + 2b3daad4595a0d2a2c264785a4b5f1c7c1e8a4e6 --- src/gap.rs +++ src/gap.rs @@ -2,6 +2,51 @@ use esp_idf_sys::*; use super::STATE; +pub enum PreferredPHY { + Phy1M, + Phy2M, + // TODO: others +} + +// unable to implement `From` for `esp_ble_gap_phy_mask_t` as it is in a different crate +#[allow(clippy::from_over_into)] +impl Into for PreferredPHY { + fn into(self) -> esp_ble_gap_phy_mask_t { + match self { + Self::Phy1M => ESP_BLE_GAP_PHY_1M_PREF_MASK as u8, + Self::Phy2M => ESP_BLE_GAP_PHY_2M_PREF_MASK as u8, + } + } +} + +pub fn set_preferred_phy( + addr: &mut esp_bd_addr_t, + tx: PreferredPHY, + rx: PreferredPHY, +) -> Result<(), EspError> { + let tx: esp_ble_gap_phy_mask_t = tx.into(); + let rx: esp_ble_gap_phy_mask_t = rx.into(); + + // TODO: wait if other transactions are ongoing + + esp!(unsafe { + esp_ble_gap_set_prefered_phy( + addr.as_mut_ptr(), + 0u8, + tx, + rx, + ESP_BLE_GAP_PHY_OPTIONS_NO_PREF as u16, + ) + }) +} + +pub fn set_pkt_data_len(addr: &mut esp_bd_addr_t, len: usize) -> Result<(), EspError> { + // BTM_BLE_DATA_SIZE_MAX = 0x00fb = 251 + assert!(len <= 0x00FB, "A packet can't be bigger than 251"); + // TODO: wait if other transactions are ongoing + esp!(unsafe { esp_ble_gap_set_pkt_data_len(addr.as_mut_ptr(), len as u16) }) +} + #[allow(non_snake_case)] pub unsafe extern "C" fn ble_gap_event_handler( event: esp_gap_ble_cb_event_t, blob - 6e4795bf46c193e94ee533ef22a6e56d5b1d7e6a blob + 4ced6a811d6b136d3e55fa51abe33b2bad4963cf --- src/lib.rs +++ src/lib.rs @@ -6,7 +6,7 @@ mod gap; pub mod gatt; pub use error::Error; -use gap::ble_gap_event_handler; +use gap::{ble_gap_event_handler, set_pkt_data_len, set_preferred_phy, PreferredPHY}; use gatt::*; type Result = std::result::Result; @@ -36,40 +36,32 @@ impl BleState { fn set_addr(&mut self, mut addr: esp_bd_addr_t) { *self = Self::Connected(addr); - // NOTE: updating to 2M PHY when connecting to prevent `LL_PHY_UPDATE_IND` possibly being - // sent by the master before `LL_FEATURE_RSP` arrived. if this is not prevented, the master - // will *not* upgrade the connection to 2M because it is not yet aware that the slave - // supports this mode. - if let Err(e) = esp!(unsafe { - esp_ble_gap_set_prefered_phy( - addr.as_mut_ptr(), - 0u8, - ESP_BLE_GAP_PHY_2M_PREF_MASK as u8, - ESP_BLE_GAP_PHY_2M_PREF_MASK as u8, - ESP_BLE_GAP_PHY_OPTIONS_NO_PREF as u16, - ) - }) { + if let Err(e) = set_preferred_phy(&mut addr, PreferredPHY::Phy2M, PreferredPHY::Phy2M) { log::error!("Unable to set preferred PHY for connection: {}", e); } - // TODO: tidy up - // set maximum LE data packet size to theoretical maximum - // BTM_BLE_DATA_SIZE_MAX = 0x00fb = 251 - log::debug!("Setting BLE DLE to 251"); - if let Err(e) = esp!(unsafe { esp_ble_gap_set_pkt_data_len(addr.as_mut_ptr(), 251) }) { + // TODO: replace timeout with waiting for ongoing transactions to end + // 50ms is about the time it takes to exchange preferred phy and migrate if needed + std::thread::sleep(std::time::Duration::from_millis(50)); + + if let Err(e) = set_pkt_data_len(&mut addr, 251) { log::error!("Unable to set BLE DLE to 251: {}", e); } - //let mut params: esp_ble_conn_update_params_t = esp_ble_conn_update_params_t { - // bda: addr, - // latency: 0, - // min_int: 0x10, - // max_int: 0x20, - // timeout: 400, - //}; - //if let Err(e) = esp!(unsafe { esp_ble_gap_update_conn_params(&mut params) }) { - // log::error!("Unable to update connection parameters: {}", e); - //} + // TODO: replace timeout with waiting for ongoing transactions to end + // 50ms is about the time it takes to exchange packet lengths + std::thread::sleep(std::time::Duration::from_millis(50)); + + let mut params: esp_ble_conn_update_params_t = esp_ble_conn_update_params_t { + bda: addr, + latency: 0, + min_int: 0x30, + max_int: 0x30, + timeout: 400, + }; + if let Err(e) = esp!(unsafe { esp_ble_gap_update_conn_params(&mut params) }) { + log::error!("Unable to update connection parameters: {}", e); + } } fn set_rssi(&mut self, rssi: i8) {