diff options
author | Albert Skog <mail@albertskog.se> | 2021-10-15 12:34:47 +0200 |
---|---|---|
committer | Albert Skog <mail@albertskog.se> | 2021-10-15 12:34:47 +0200 |
commit | 182b9001189deca80e3840b6c989cbc37623991d (patch) | |
tree | 358d1d5ed6f9aaef0a6b622bb9b36f65b4a115d5 /examples/src/bin/ble_bas_peripheral.rs | |
parent | fd48489c0ffc58c8ae02d32d9904162a2777df3c (diff) | |
parent | bf4f7a143d38713922d9bd5ff1a983c4c7290c10 (diff) | |
download | nrf-softdevice-182b9001189deca80e3840b6c989cbc37623991d.zip |
Merge branch 'master' into feature/indications
Diffstat (limited to 'examples/src/bin/ble_bas_peripheral.rs')
-rw-r--r-- | examples/src/bin/ble_bas_peripheral.rs | 61 |
1 files changed, 35 insertions, 26 deletions
diff --git a/examples/src/bin/ble_bas_peripheral.rs b/examples/src/bin/ble_bas_peripheral.rs index 8d76ec3..e3e6233 100644 --- a/examples/src/bin/ble_bas_peripheral.rs +++ b/examples/src/bin/ble_bas_peripheral.rs @@ -24,12 +24,16 @@ async fn softdevice_task(sd: &'static Softdevice) { sd.run().await; } -#[nrf_softdevice::gatt_server(uuid = "180f")] +#[nrf_softdevice::gatt_service(uuid = "180f")] struct BatteryService { - #[characteristic(uuid = "2a19", read, write, notify)] + #[characteristic(uuid = "2a19", read, notify)] battery_level: u8, +} + +#[nrf_softdevice::gatt_service(uuid = "9e7312e0-2354-11eb-9f10-fbc30a62cf38")] +struct FooService { #[characteristic( - uuid = "3a4a1f7e-22d8-11eb-a3aa-1b3b1d4e4a0d", + uuid = "9e7312e0-2354-11eb-9f10-fbc30a63cf38", read, write, notify, @@ -38,9 +42,16 @@ struct BatteryService { foo: u16, } +#[nrf_softdevice::gatt_server] +struct Server { + bas: BatteryService, + foo: FooService, +} + #[embassy::task] async fn bluetooth_task(sd: &'static Softdevice) { - let server: BatteryService = unwrap!(gatt_server::register(sd)); + let server: Server = unwrap!(gatt_server::register(sd)); + #[rustfmt::skip] let adv_data = &[ 0x02, 0x01, raw::BLE_GAP_ADV_FLAGS_LE_ONLY_GENERAL_DISC_MODE as u8, @@ -64,30 +75,28 @@ async fn bluetooth_task(sd: &'static Softdevice) { // Run the GATT server on the connection. This returns when the connection gets disconnected. let res = gatt_server::run(&conn, &server, |e| match e { - BatteryServiceEvent::BatteryLevelWrite(val) => { - info!("wrote battery level: {}", val); - if let Err(e) = server.battery_level_notify(&conn, val + 1) { - info!("send notification error: {:?}", e); + ServerEvent::Bas(e) => match e { + BatteryServiceEvent::BatteryLevelCccdWrite { notifications } => { + info!("battery notifications: {}", notifications) + } + }, + ServerEvent::Foo(e) => match e { + FooServiceEvent::FooWrite(val) => { + info!("wrote foo: {}", val); + if let Err(e) = server.foo.foo_notify(&conn, val + 1) { + info!("send notification error: {:?}", e); + } } - } - BatteryServiceEvent::FooWrite(val) => { - info!("wrote battery level: {}", val); - if let Err(e) = server.foo_notify(&conn, val + 1) { - info!("send notification error: {:?}", e); + FooServiceEvent::FooCccdWrite { + indications, + notifications, + } => { + info!( + "foo indications: {}, notifications: {}", + indications, notifications + ) } - } - BatteryServiceEvent::BatteryLevelCccdWrite { notifications } => { - info!("battery notifications: {}", notifications) - } - BatteryServiceEvent::FooCccdWrite { - indications, - notifications, - } => { - info!( - "foo indications: {}, notifications: {}", - indications, notifications - ) - } + }, }) .await; |