diff options
Diffstat (limited to 'examples')
-rw-r--r-- | examples/src/bin/ble_advertise.rs | 98 | ||||
-rw-r--r-- | examples/src/bin/ble_bas_peripheral.rs | 2 | ||||
-rw-r--r-- | examples/src/bin/ble_l2cap_peripheral.rs | 2 | ||||
-rw-r--r-- | examples/src/bin/ble_peripheral_onoff.rs | 4 |
4 files changed, 102 insertions, 4 deletions
diff --git a/examples/src/bin/ble_advertise.rs b/examples/src/bin/ble_advertise.rs new file mode 100644 index 0000000..b65ae79 --- /dev/null +++ b/examples/src/bin/ble_advertise.rs @@ -0,0 +1,98 @@ +#![no_std] +#![no_main] +#![feature(type_alias_impl_trait)] +#![feature(min_type_alias_impl_trait)] +#![feature(impl_trait_in_bindings)] +#![feature(alloc_error_handler)] +#![allow(incomplete_features)] + +#[path = "../example_common.rs"] +mod example_common; + +use core::mem; +use cortex_m_rt::entry; +use defmt::{info, unreachable, *}; +use embassy::executor::Executor; +use embassy::util::Forever; + +use nrf_softdevice::ble::peripheral; +use nrf_softdevice::{raw, Softdevice}; + +static EXECUTOR: Forever<Executor> = Forever::new(); + +#[embassy::task] +async fn softdevice_task(sd: &'static Softdevice) { + sd.run().await; +} + +#[embassy::task] +async fn bluetooth_task(sd: &'static Softdevice) { + #[rustfmt::skip] + let adv_data = &[ + 0x02, 0x01, raw::BLE_GAP_ADV_FLAGS_LE_ONLY_GENERAL_DISC_MODE as u8, + 0x03, 0x03, 0x09, 0x18, + 0x0a, 0x09, b'H', b'e', b'l', b'l', b'o', b'R', b'u', b's', b't', + ]; + #[rustfmt::skip] + let scan_data = &[ + 0x03, 0x03, 0x09, 0x18, + ]; + + let mut config = peripheral::Config::default(); + config.interval = 50; + let adv = peripheral::NonconnectableAdvertisement::ScannableUndirected { + adv_data, + scan_data, + }; + unwrap!(peripheral::advertise(sd, adv, &config).await); + + // advertise never returns + unreachable!(); +} + +#[entry] +fn main() -> ! { + info!("Hello World!"); + + let config = nrf_softdevice::Config { + clock: Some(raw::nrf_clock_lf_cfg_t { + source: raw::NRF_CLOCK_LF_SRC_XTAL as u8, + rc_ctiv: 0, + rc_temp_ctiv: 0, + accuracy: 7, + }), + conn_gap: Some(raw::ble_gap_conn_cfg_t { + conn_count: 6, + event_length: 24, + }), + conn_gatt: Some(raw::ble_gatt_conn_cfg_t { att_mtu: 256 }), + gatts_attr_tab_size: Some(raw::ble_gatts_cfg_attr_tab_size_t { + attr_tab_size: 32768, + }), + gap_role_count: Some(raw::ble_gap_cfg_role_count_t { + adv_set_count: 1, + periph_role_count: 3, + central_role_count: 3, + central_sec_count: 0, + _bitfield_1: raw::ble_gap_cfg_role_count_t::new_bitfield_1(0), + }), + gap_device_name: Some(raw::ble_gap_cfg_device_name_t { + p_value: b"HelloRust" as *const u8 as _, + current_len: 9, + max_len: 9, + write_perm: unsafe { mem::zeroed() }, + _bitfield_1: raw::ble_gap_cfg_device_name_t::new_bitfield_1( + raw::BLE_GATTS_VLOC_STACK as u8, + ), + }), + ..Default::default() + }; + + let sd = Softdevice::enable(&config); + + let executor = EXECUTOR.put(Executor::new()); + executor.run(|spawner| { + unwrap!(spawner.spawn(softdevice_task(sd))); + unwrap!(spawner.spawn(bluetooth_task(sd))); + }); +} diff --git a/examples/src/bin/ble_bas_peripheral.rs b/examples/src/bin/ble_bas_peripheral.rs index 949a5cc..b9b1221 100644 --- a/examples/src/bin/ble_bas_peripheral.rs +++ b/examples/src/bin/ble_bas_peripheral.rs @@ -54,7 +54,7 @@ async fn bluetooth_task(sd: &'static Softdevice) { adv_data, scan_data, }; - let conn = unwrap!(peripheral::advertise(sd, adv, &config).await); + let conn = unwrap!(peripheral::advertise_connectable(sd, adv, &config).await); info!("advertising done!"); diff --git a/examples/src/bin/ble_l2cap_peripheral.rs b/examples/src/bin/ble_l2cap_peripheral.rs index 0ab7369..d752ef7 100644 --- a/examples/src/bin/ble_l2cap_peripheral.rs +++ b/examples/src/bin/ble_l2cap_peripheral.rs @@ -51,7 +51,7 @@ async fn bluetooth_task(sd: &'static Softdevice) { adv_data, scan_data, }; - let conn = unwrap!(peripheral::advertise(sd, adv, &config).await); + let conn = unwrap!(peripheral::advertise_connectable(sd, adv, &config).await); info!("advertising done!"); diff --git a/examples/src/bin/ble_peripheral_onoff.rs b/examples/src/bin/ble_peripheral_onoff.rs index f61d1b3..eb74814 100644 --- a/examples/src/bin/ble_peripheral_onoff.rs +++ b/examples/src/bin/ble_peripheral_onoff.rs @@ -54,7 +54,7 @@ async fn run_bluetooth(sd: &'static Softdevice, server: &FooService) { adv_data, scan_data, }; - let conn = unwrap!(peripheral::advertise(sd, adv, &config).await); + let conn = unwrap!(peripheral::advertise_connectable(sd, adv, &config).await); info!("advertising done!"); @@ -111,7 +111,7 @@ async fn bluetooth_task(sd: &'static Softdevice, button1: AnyPin, button2: AnyPi // Since the bluetooth future never finishes, this can only happen when the Off button is pressed. // This will cause the bluetooth future to be dropped. // - // If it was advertising, the nested `peripheral::advertise` future will be dropped, which will cause + // If it was advertising, the nested `peripheral::advertise_connectable` future will be dropped, which will cause // the softdevice to stop advertising. // If it was connected, it will drop everything including the `Connection` instance, which // will tell the softdevice to disconnect it. |