diff options
author | Ulf Lilleengen <lulf@redhat.com> | 2021-10-11 11:41:24 +0200 |
---|---|---|
committer | Ulf Lilleengen <lulf@redhat.com> | 2021-10-11 11:41:24 +0200 |
commit | 1f73a93a6ddce925ff749d63f4528e50e9efae02 (patch) | |
tree | d0fa96325b5d7ec3a6bbcc472b6df06fb7c1a86d /examples/src/bin/ble_peripheral_onoff.rs | |
parent | c9a0fbf3b7d9537463756efdad7a819cfff6756e (diff) | |
download | nrf-softdevice-1f73a93a6ddce925ff749d63f4528e50e9efae02.zip |
Add gatt_server macro for registering a GATT server
Generate event type and on_write handling for server type, which in turn
dispatches events to its services.
Register and run now takes a valid Server instance as argument.
Diffstat (limited to 'examples/src/bin/ble_peripheral_onoff.rs')
-rw-r--r-- | examples/src/bin/ble_peripheral_onoff.rs | 29 |
1 files changed, 17 insertions, 12 deletions
diff --git a/examples/src/bin/ble_peripheral_onoff.rs b/examples/src/bin/ble_peripheral_onoff.rs index 3666055..82d0599 100644 --- a/examples/src/bin/ble_peripheral_onoff.rs +++ b/examples/src/bin/ble_peripheral_onoff.rs @@ -18,10 +18,7 @@ use embassy_nrf::gpiote::PortInput; use embassy_nrf::interrupt::Priority; use futures::pin_mut; -use nrf_softdevice::ble::{ - gatt_server::{self, Service}, - peripheral, -}; +use nrf_softdevice::ble::{gatt_server, peripheral}; use nrf_softdevice::{raw, Softdevice}; static EXECUTOR: Forever<Executor> = Forever::new(); @@ -37,7 +34,12 @@ struct FooService { foo: u16, } -async fn run_bluetooth(sd: &'static Softdevice, server: &FooService) { +#[nrf_softdevice::gatt_server] +struct Server { + foo: FooService, +} + +async fn run_bluetooth(sd: &'static Softdevice, server: &Server) { #[rustfmt::skip] let adv_data = &[ 0x02, 0x01, raw::BLE_GAP_ADV_FLAGS_LE_ONLY_GENERAL_DISC_MODE as u8, @@ -59,16 +61,19 @@ async fn run_bluetooth(sd: &'static Softdevice, server: &FooService) { info!("advertising done!"); - let res = gatt_server::run(&conn, |e| match server.on_write(e) { - Some(FooServiceEvent::FooWrite(val)) => { + let res = gatt_server::run(&conn, server, |e| match e { + ServerEvent::FooService(FooServiceEvent::FooWrite(val)) => { info!("wrote foo level: {}", val); - if let Err(e) = server.foo_notify(&conn, val + 1) { + if let Err(e) = server.foo.foo_notify(&conn, val + 1) { info!("send notification error: {:?}", e); } } - Some(FooServiceEvent::FooNotificationsEnabled) => info!("notifications enabled"), - Some(FooServiceEvent::FooNotificationsDisabled) => info!("notifications disabled"), - None => {} + ServerEvent::FooService(FooServiceEvent::FooNotificationsEnabled) => { + info!("notifications enabled") + } + ServerEvent::FooService(FooServiceEvent::FooNotificationsDisabled) => { + info!("notifications disabled") + } }) .await; @@ -80,7 +85,7 @@ async fn run_bluetooth(sd: &'static Softdevice, server: &FooService) { #[embassy::task] async fn bluetooth_task(sd: &'static Softdevice, button1: AnyPin, button2: AnyPin) { - let server: FooService = unwrap!(gatt_server::register(sd)); + let server: Server = unwrap!(gatt_server::register(sd)); info!("Bluetooth is OFF"); info!("Press nrf52840-dk button 1 to enable, button 2 to disable"); |