diff options
author | Dario Nieuwenhuis <dirbaio@dirbaio.net> | 2020-11-10 15:22:22 +0100 |
---|---|---|
committer | Dario Nieuwenhuis <dirbaio@dirbaio.net> | 2020-11-10 15:22:22 +0100 |
commit | 271eb2224230f31349ff3e69f76bde9db594d0e4 (patch) | |
tree | e991a84634567e57c7843aa1ba3fc523777ff9ae /nrf-softdevice/src/ble/peripheral.rs | |
parent | 1c15bd7677a77ba8a453ac3723d0153b0120d473 (diff) | |
download | nrf-softdevice-271eb2224230f31349ff3e69f76bde9db594d0e4.zip |
Properly handle future drops. Add on/off example showcasing it.
Diffstat (limited to 'nrf-softdevice/src/ble/peripheral.rs')
-rw-r--r-- | nrf-softdevice/src/ble/peripheral.rs | 24 |
1 files changed, 10 insertions, 14 deletions
diff --git a/nrf-softdevice/src/ble/peripheral.rs b/nrf-softdevice/src/ble/peripheral.rs index 0bf78c6..809760b 100644 --- a/nrf-softdevice/src/ble/peripheral.rs +++ b/nrf-softdevice/src/ble/peripheral.rs @@ -16,7 +16,7 @@ pub(crate) unsafe fn on_adv_set_terminated( "peripheral on_adv_set_terminated conn_handle={:u16}", gap_evt.conn_handle ); - ADV_SIGNAL.signal(Err(AdvertiseError::Stopped)) + ADV_PORTAL.call(Err(AdvertiseError::Stopped)) } pub(crate) unsafe fn on_scan_req_report( @@ -96,7 +96,7 @@ impl From<RawError> for AdvertiseStopError { } static mut ADV_HANDLE: u8 = raw::BLE_GAP_ADV_SET_HANDLE_NOT_SET as u8; -pub(crate) static ADV_SIGNAL: Signal<Result<Connection, AdvertiseError>> = Signal::new(); +pub(crate) static ADV_PORTAL: Portal<Result<Connection, AdvertiseError>> = Portal::new(); // Begins an ATT MTU exchange procedure, followed by a data length update request as necessary. pub async fn advertise( @@ -149,6 +149,11 @@ pub async fn advertise( scan_rsp_data: map_data(scan_data), }; + let d = OnDrop::new(|| { + let ret = unsafe { raw::sd_ble_gap_adv_stop(ADV_HANDLE) }; + let _ = RawError::convert(ret).dewarn(intern!("sd_ble_gap_adv_stop")); + }); + let ret = unsafe { raw::sd_ble_gap_adv_set_configure(&mut ADV_HANDLE as _, &datas as _, &adv_params as _) }; @@ -157,27 +162,18 @@ pub async fn advertise( let ret = unsafe { raw::sd_ble_gap_adv_start(ADV_HANDLE, 1 as u8) }; RawError::convert(ret).dewarn(intern!("sd_ble_gap_adv_start"))?; - // TODO handle future drop - info!("Advertising started!"); // The advertising data needs to be kept alive for the entire duration of the advertising procedure. - let conn = ADV_SIGNAL.wait().await?; + let conn = ADV_PORTAL.wait_once(|res| res).await?; let state = conn.state(); state.set_att_mtu_desired(config.att_mtu_desired); - Ok(conn) -} + d.defuse(); -pub fn advertise_stop(sd: &Softdevice) -> Result<(), AdvertiseStopError> { - let ret = unsafe { raw::sd_ble_gap_adv_stop(ADV_HANDLE) }; - match RawError::convert(ret).dewarn(intern!("sd_ble_gap_adv_stop")) { - Ok(()) => Ok(()), - Err(RawError::InvalidState) => Err(AdvertiseStopError::NotRunning), - Err(e) => Err(e.into()), - } + Ok(conn) } #[derive(Copy, Clone)] |