diff options
Diffstat (limited to 'examples/src/bin/ble_dis_bas_peripheral_builder.rs')
-rw-r--r-- | examples/src/bin/ble_dis_bas_peripheral_builder.rs | 75 |
1 files changed, 31 insertions, 44 deletions
diff --git a/examples/src/bin/ble_dis_bas_peripheral_builder.rs b/examples/src/bin/ble_dis_bas_peripheral_builder.rs index 71ae849..b73b6d8 100644 --- a/examples/src/bin/ble_dis_bas_peripheral_builder.rs +++ b/examples/src/bin/ble_dis_bas_peripheral_builder.rs @@ -7,10 +7,8 @@ mod example_common; use core::mem; -use cortex_m_rt::entry; use defmt::{info, *}; -use embassy_executor::executor::Executor; -use embassy_util::Forever; +use embassy_executor::Spawner; use nrf_softdevice::ble::gatt_server::builder::ServiceBuilder; use nrf_softdevice::ble::gatt_server::characteristic::{Attribute, Metadata, Properties}; use nrf_softdevice::ble::gatt_server::{CharacteristicHandles, RegisterError}; @@ -29,11 +27,9 @@ const SOFTWARE_REVISION: Uuid = Uuid::new_16(0x2a28); const MANUFACTURER_NAME: Uuid = Uuid::new_16(0x2a29); const PNP_ID: Uuid = Uuid::new_16(0x2a50); -static EXECUTOR: Forever<Executor> = Forever::new(); - #[embassy_executor::task] -async fn softdevice_task(sd: &'static Softdevice) { - sd.run().await; +async fn softdevice_task(sd: &'static Softdevice) -> ! { + sd.run().await } #[repr(u8)] @@ -186,37 +182,8 @@ impl gatt_server::Server for Server { } } -#[embassy_executor::task] -async fn bluetooth_task(sd: &'static Softdevice, server: Server) { - #[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, - ]; - - loop { - let config = peripheral::Config::default(); - let adv = peripheral::ConnectableAdvertisement::ScannableUndirected { adv_data, scan_data }; - let conn = unwrap!(peripheral::advertise_connectable(sd, adv, &config).await); - - info!("advertising done!"); - - // Run the GATT server on the connection. This returns when the connection gets disconnected. - let res = gatt_server::run(&conn, &server, |_| {}).await; - - if let Err(e) = res { - info!("gatt_server run exited with error: {:?}", e); - } - } -} - -#[entry] -fn main() -> ! { +#[embassy_executor::main] +async fn main(spawner: Spawner) { info!("Hello World!"); let config = nrf_softdevice::Config { @@ -250,12 +217,32 @@ fn main() -> ! { }; let sd = Softdevice::enable(&config); - let server = unwrap!(Server::new(sd, "12345678")); + unwrap!(spawner.spawn(softdevice_task(sd))); + + #[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 executor = EXECUTOR.put(Executor::new()); - executor.run(move |spawner| { - unwrap!(spawner.spawn(softdevice_task(sd))); - unwrap!(spawner.spawn(bluetooth_task(sd, server))); - }); + loop { + let config = peripheral::Config::default(); + let adv = peripheral::ConnectableAdvertisement::ScannableUndirected { adv_data, scan_data }; + let conn = unwrap!(peripheral::advertise_connectable(sd, adv, &config).await); + + info!("advertising done!"); + + // Run the GATT server on the connection. This returns when the connection gets disconnected. + let res = gatt_server::run(&conn, &server, |_| {}).await; + + if let Err(e) = res { + info!("gatt_server run exited with error: {:?}", e); + } + } } |